* cp-tree.h (struct cp_evaluated): New.
[official-gcc.git] / gcc / cp / parser.c
blob465ab8fdbaed0efbaa68a8583dac908ce6c72f9e
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2018 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 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
50 /* The lexer. */
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53 and c-lex.c) and the C++ parser. */
55 static cp_token eof_token =
57 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
60 /* The various kinds of non integral constant we encounter. */
61 enum non_integral_constant {
62 NIC_NONE,
63 /* floating-point literal */
64 NIC_FLOAT,
65 /* %<this%> */
66 NIC_THIS,
67 /* %<__FUNCTION__%> */
68 NIC_FUNC_NAME,
69 /* %<__PRETTY_FUNCTION__%> */
70 NIC_PRETTY_FUNC,
71 /* %<__func__%> */
72 NIC_C99_FUNC,
73 /* "%<va_arg%> */
74 NIC_VA_ARG,
75 /* a cast */
76 NIC_CAST,
77 /* %<typeid%> operator */
78 NIC_TYPEID,
79 /* non-constant compound literals */
80 NIC_NCC,
81 /* a function call */
82 NIC_FUNC_CALL,
83 /* an increment */
84 NIC_INC,
85 /* an decrement */
86 NIC_DEC,
87 /* an array reference */
88 NIC_ARRAY_REF,
89 /* %<->%> */
90 NIC_ARROW,
91 /* %<.%> */
92 NIC_POINT,
93 /* the address of a label */
94 NIC_ADDR_LABEL,
95 /* %<*%> */
96 NIC_STAR,
97 /* %<&%> */
98 NIC_ADDR,
99 /* %<++%> */
100 NIC_PREINCREMENT,
101 /* %<--%> */
102 NIC_PREDECREMENT,
103 /* %<new%> */
104 NIC_NEW,
105 /* %<delete%> */
106 NIC_DEL,
107 /* calls to overloaded operators */
108 NIC_OVERLOADED,
109 /* an assignment */
110 NIC_ASSIGNMENT,
111 /* a comma operator */
112 NIC_COMMA,
113 /* a call to a constructor */
114 NIC_CONSTRUCTOR,
115 /* a transaction expression */
116 NIC_TRANSACTION
119 /* The various kinds of errors about name-lookup failing. */
120 enum name_lookup_error {
121 /* NULL */
122 NLE_NULL,
123 /* is not a type */
124 NLE_TYPE,
125 /* is not a class or namespace */
126 NLE_CXX98,
127 /* is not a class, namespace, or enumeration */
128 NLE_NOT_CXX98
131 /* The various kinds of required token */
132 enum required_token {
133 RT_NONE,
134 RT_SEMICOLON, /* ';' */
135 RT_OPEN_PAREN, /* '(' */
136 RT_CLOSE_BRACE, /* '}' */
137 RT_OPEN_BRACE, /* '{' */
138 RT_CLOSE_SQUARE, /* ']' */
139 RT_OPEN_SQUARE, /* '[' */
140 RT_COMMA, /* ',' */
141 RT_SCOPE, /* '::' */
142 RT_LESS, /* '<' */
143 RT_GREATER, /* '>' */
144 RT_EQ, /* '=' */
145 RT_ELLIPSIS, /* '...' */
146 RT_MULT, /* '*' */
147 RT_COMPL, /* '~' */
148 RT_COLON, /* ':' */
149 RT_COLON_SCOPE, /* ':' or '::' */
150 RT_CLOSE_PAREN, /* ')' */
151 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
152 RT_PRAGMA_EOL, /* end of line */
153 RT_NAME, /* identifier */
155 /* The type is CPP_KEYWORD */
156 RT_NEW, /* new */
157 RT_DELETE, /* delete */
158 RT_RETURN, /* return */
159 RT_WHILE, /* while */
160 RT_EXTERN, /* extern */
161 RT_STATIC_ASSERT, /* static_assert */
162 RT_DECLTYPE, /* decltype */
163 RT_OPERATOR, /* operator */
164 RT_CLASS, /* class */
165 RT_TEMPLATE, /* template */
166 RT_NAMESPACE, /* namespace */
167 RT_USING, /* using */
168 RT_ASM, /* asm */
169 RT_TRY, /* try */
170 RT_CATCH, /* catch */
171 RT_THROW, /* throw */
172 RT_LABEL, /* __label__ */
173 RT_AT_TRY, /* @try */
174 RT_AT_SYNCHRONIZED, /* @synchronized */
175 RT_AT_THROW, /* @throw */
177 RT_SELECT, /* selection-statement */
178 RT_ITERATION, /* iteration-statement */
179 RT_JUMP, /* jump-statement */
180 RT_CLASS_KEY, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL /* __transaction_cancel */
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188 reverting it on destruction. */
190 class type_id_in_expr_sentinel
192 cp_parser *parser;
193 bool saved;
194 public:
195 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
196 : parser (parser),
197 saved (parser->in_type_id_in_expr_p)
198 { parser->in_type_id_in_expr_p = set; }
199 ~type_id_in_expr_sentinel ()
200 { parser->in_type_id_in_expr_p = saved; }
203 /* Prototypes. */
205 static cp_lexer *cp_lexer_new_main
206 (void);
207 static cp_lexer *cp_lexer_new_from_tokens
208 (cp_token_cache *tokens);
209 static void cp_lexer_destroy
210 (cp_lexer *);
211 static int cp_lexer_saving_tokens
212 (const cp_lexer *);
213 static cp_token *cp_lexer_token_at
214 (cp_lexer *, cp_token_position);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer *, cp_token *);
217 static inline cp_token *cp_lexer_peek_token
218 (cp_lexer *);
219 static cp_token *cp_lexer_peek_nth_token
220 (cp_lexer *, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer *, enum rid);
227 static cp_token *cp_lexer_consume_token
228 (cp_lexer *);
229 static void cp_lexer_purge_token
230 (cp_lexer *);
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer *, cp_token_position);
233 static void cp_lexer_save_tokens
234 (cp_lexer *);
235 static void cp_lexer_commit_tokens
236 (cp_lexer *);
237 static void cp_lexer_rollback_tokens
238 (cp_lexer *);
239 static void cp_lexer_print_token
240 (FILE *, cp_token *);
241 static inline bool cp_lexer_debugging_p
242 (cp_lexer *);
243 static void cp_lexer_start_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static void cp_lexer_stop_debugging
246 (cp_lexer *) ATTRIBUTE_UNUSED;
248 static cp_token_cache *cp_token_cache_new
249 (cp_token *, cp_token *);
251 static void cp_parser_initial_pragma
252 (cp_token *);
254 static bool cp_parser_omp_declare_reduction_exprs
255 (tree, cp_parser *);
256 static void cp_finalize_oacc_routine
257 (cp_parser *, tree, bool);
259 /* Manifest constants. */
260 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
261 #define CP_SAVED_TOKEN_STACK 5
263 /* Variables. */
265 /* The stream to which debugging output should be written. */
266 static FILE *cp_lexer_debug_stream;
268 /* Nonzero if we are parsing an unevaluated operand: an operand to
269 sizeof, typeof, or alignof. */
270 int cp_unevaluated_operand;
272 /* Dump up to NUM tokens in BUFFER to FILE starting with token
273 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
274 first token in BUFFER. If NUM is 0, dump all the tokens. If
275 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
276 highlighted by surrounding it in [[ ]]. */
278 static void
279 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
280 cp_token *start_token, unsigned num,
281 cp_token *curr_token)
283 unsigned i, nprinted;
284 cp_token *token;
285 bool do_print;
287 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
289 if (buffer == NULL)
290 return;
292 if (num == 0)
293 num = buffer->length ();
295 if (start_token == NULL)
296 start_token = buffer->address ();
298 if (start_token > buffer->address ())
300 cp_lexer_print_token (file, &(*buffer)[0]);
301 fprintf (file, " ... ");
304 do_print = false;
305 nprinted = 0;
306 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
308 if (token == start_token)
309 do_print = true;
311 if (!do_print)
312 continue;
314 nprinted++;
315 if (token == curr_token)
316 fprintf (file, "[[");
318 cp_lexer_print_token (file, token);
320 if (token == curr_token)
321 fprintf (file, "]]");
323 switch (token->type)
325 case CPP_SEMICOLON:
326 case CPP_OPEN_BRACE:
327 case CPP_CLOSE_BRACE:
328 case CPP_EOF:
329 fputc ('\n', file);
330 break;
332 default:
333 fputc (' ', file);
337 if (i == num && i < buffer->length ())
339 fprintf (file, " ... ");
340 cp_lexer_print_token (file, &buffer->last ());
343 fprintf (file, "\n");
347 /* Dump all tokens in BUFFER to stderr. */
349 void
350 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
352 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
355 DEBUG_FUNCTION void
356 debug (vec<cp_token, va_gc> &ref)
358 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
361 DEBUG_FUNCTION void
362 debug (vec<cp_token, va_gc> *ptr)
364 if (ptr)
365 debug (*ptr);
366 else
367 fprintf (stderr, "<nil>\n");
371 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
372 description for T. */
374 static void
375 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
377 if (t)
379 fprintf (file, "%s: ", desc);
380 print_node_brief (file, "", t, 0);
385 /* Dump parser context C to FILE. */
387 static void
388 cp_debug_print_context (FILE *file, cp_parser_context *c)
390 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
391 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
392 print_node_brief (file, "", c->object_type, 0);
393 fprintf (file, "}\n");
397 /* Print the stack of parsing contexts to FILE starting with FIRST. */
399 static void
400 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
402 unsigned i;
403 cp_parser_context *c;
405 fprintf (file, "Parsing context stack:\n");
406 for (i = 0, c = first; c; c = c->next, i++)
408 fprintf (file, "\t#%u: ", i);
409 cp_debug_print_context (file, c);
414 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
416 static void
417 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
419 if (flag)
420 fprintf (file, "%s: true\n", desc);
424 /* Print an unparsed function entry UF to FILE. */
426 static void
427 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
429 unsigned i;
430 cp_default_arg_entry *default_arg_fn;
431 tree fn;
433 fprintf (file, "\tFunctions with default args:\n");
434 for (i = 0;
435 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
436 i++)
438 fprintf (file, "\t\tClass type: ");
439 print_node_brief (file, "", default_arg_fn->class_type, 0);
440 fprintf (file, "\t\tDeclaration: ");
441 print_node_brief (file, "", default_arg_fn->decl, 0);
442 fprintf (file, "\n");
445 fprintf (file, "\n\tFunctions with definitions that require "
446 "post-processing\n\t\t");
447 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
449 print_node_brief (file, "", fn, 0);
450 fprintf (file, " ");
452 fprintf (file, "\n");
454 fprintf (file, "\n\tNon-static data members with initializers that require "
455 "post-processing\n\t\t");
456 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
458 print_node_brief (file, "", fn, 0);
459 fprintf (file, " ");
461 fprintf (file, "\n");
465 /* Print the stack of unparsed member functions S to FILE. */
467 static void
468 cp_debug_print_unparsed_queues (FILE *file,
469 vec<cp_unparsed_functions_entry, va_gc> *s)
471 unsigned i;
472 cp_unparsed_functions_entry *uf;
474 fprintf (file, "Unparsed functions\n");
475 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
477 fprintf (file, "#%u:\n", i);
478 cp_debug_print_unparsed_function (file, uf);
483 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
484 the given PARSER. If FILE is NULL, the output is printed on stderr. */
486 static void
487 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
489 cp_token *next_token, *first_token, *start_token;
491 if (file == NULL)
492 file = stderr;
494 next_token = parser->lexer->next_token;
495 first_token = parser->lexer->buffer->address ();
496 start_token = (next_token > first_token + window_size / 2)
497 ? next_token - window_size / 2
498 : first_token;
499 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
500 next_token);
504 /* Dump debugging information for the given PARSER. If FILE is NULL,
505 the output is printed on stderr. */
507 void
508 cp_debug_parser (FILE *file, cp_parser *parser)
510 const size_t window_size = 20;
511 cp_token *token;
512 expanded_location eloc;
514 if (file == NULL)
515 file = stderr;
517 fprintf (file, "Parser state\n\n");
518 fprintf (file, "Number of tokens: %u\n",
519 vec_safe_length (parser->lexer->buffer));
520 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
521 cp_debug_print_tree_if_set (file, "Object scope",
522 parser->object_scope);
523 cp_debug_print_tree_if_set (file, "Qualifying scope",
524 parser->qualifying_scope);
525 cp_debug_print_context_stack (file, parser->context);
526 cp_debug_print_flag (file, "Allow GNU extensions",
527 parser->allow_gnu_extensions_p);
528 cp_debug_print_flag (file, "'>' token is greater-than",
529 parser->greater_than_is_operator_p);
530 cp_debug_print_flag (file, "Default args allowed in current "
531 "parameter list", parser->default_arg_ok_p);
532 cp_debug_print_flag (file, "Parsing integral constant-expression",
533 parser->integral_constant_expression_p);
534 cp_debug_print_flag (file, "Allow non-constant expression in current "
535 "constant-expression",
536 parser->allow_non_integral_constant_expression_p);
537 cp_debug_print_flag (file, "Seen non-constant expression",
538 parser->non_integral_constant_expression_p);
539 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
540 "current context",
541 parser->local_variables_forbidden_p);
542 cp_debug_print_flag (file, "In unbraced linkage specification",
543 parser->in_unbraced_linkage_specification_p);
544 cp_debug_print_flag (file, "Parsing a declarator",
545 parser->in_declarator_p);
546 cp_debug_print_flag (file, "In template argument list",
547 parser->in_template_argument_list_p);
548 cp_debug_print_flag (file, "Parsing an iteration statement",
549 parser->in_statement & IN_ITERATION_STMT);
550 cp_debug_print_flag (file, "Parsing a switch statement",
551 parser->in_statement & IN_SWITCH_STMT);
552 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
553 parser->in_statement & IN_OMP_BLOCK);
554 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
555 parser->in_statement & IN_OMP_FOR);
556 cp_debug_print_flag (file, "Parsing an if statement",
557 parser->in_statement & IN_IF_STMT);
558 cp_debug_print_flag (file, "Parsing a type-id in an expression "
559 "context", parser->in_type_id_in_expr_p);
560 cp_debug_print_flag (file, "String expressions should be translated "
561 "to execution character set",
562 parser->translate_strings_p);
563 cp_debug_print_flag (file, "Parsing function body outside of a "
564 "local class", parser->in_function_body);
565 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
566 parser->colon_corrects_to_scope_p);
567 cp_debug_print_flag (file, "Colon doesn't start a class definition",
568 parser->colon_doesnt_start_class_def_p);
569 if (parser->type_definition_forbidden_message)
570 fprintf (file, "Error message for forbidden type definitions: %s\n",
571 parser->type_definition_forbidden_message);
572 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
573 fprintf (file, "Number of class definitions in progress: %u\n",
574 parser->num_classes_being_defined);
575 fprintf (file, "Number of template parameter lists for the current "
576 "declaration: %u\n", parser->num_template_parameter_lists);
577 cp_debug_parser_tokens (file, parser, window_size);
578 token = parser->lexer->next_token;
579 fprintf (file, "Next token to parse:\n");
580 fprintf (file, "\tToken: ");
581 cp_lexer_print_token (file, token);
582 eloc = expand_location (token->location);
583 fprintf (file, "\n\tFile: %s\n", eloc.file);
584 fprintf (file, "\tLine: %d\n", eloc.line);
585 fprintf (file, "\tColumn: %d\n", eloc.column);
588 DEBUG_FUNCTION void
589 debug (cp_parser &ref)
591 cp_debug_parser (stderr, &ref);
594 DEBUG_FUNCTION void
595 debug (cp_parser *ptr)
597 if (ptr)
598 debug (*ptr);
599 else
600 fprintf (stderr, "<nil>\n");
603 /* Allocate memory for a new lexer object and return it. */
605 static cp_lexer *
606 cp_lexer_alloc (void)
608 cp_lexer *lexer;
610 c_common_no_more_pch ();
612 /* Allocate the memory. */
613 lexer = ggc_cleared_alloc<cp_lexer> ();
615 /* Initially we are not debugging. */
616 lexer->debugging_p = false;
618 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
620 /* Create the buffer. */
621 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
623 return lexer;
627 /* Create a new main C++ lexer, the lexer that gets tokens from the
628 preprocessor. */
630 static cp_lexer *
631 cp_lexer_new_main (void)
633 cp_lexer *lexer;
634 cp_token token;
636 /* It's possible that parsing the first pragma will load a PCH file,
637 which is a GC collection point. So we have to do that before
638 allocating any memory. */
639 cp_parser_initial_pragma (&token);
641 lexer = cp_lexer_alloc ();
643 /* Put the first token in the buffer. */
644 lexer->buffer->quick_push (token);
646 /* Get the remaining tokens from the preprocessor. */
647 while (token.type != CPP_EOF)
649 cp_lexer_get_preprocessor_token (lexer, &token);
650 vec_safe_push (lexer->buffer, token);
653 lexer->last_token = lexer->buffer->address ()
654 + lexer->buffer->length ()
655 - 1;
656 lexer->next_token = lexer->buffer->length ()
657 ? lexer->buffer->address ()
658 : &eof_token;
660 /* Subsequent preprocessor diagnostics should use compiler
661 diagnostic functions to get the compiler source location. */
662 done_lexing = true;
664 gcc_assert (!lexer->next_token->purged_p);
665 return lexer;
668 /* Create a new lexer whose token stream is primed with the tokens in
669 CACHE. When these tokens are exhausted, no new tokens will be read. */
671 static cp_lexer *
672 cp_lexer_new_from_tokens (cp_token_cache *cache)
674 cp_token *first = cache->first;
675 cp_token *last = cache->last;
676 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
678 /* We do not own the buffer. */
679 lexer->buffer = NULL;
680 lexer->next_token = first == last ? &eof_token : first;
681 lexer->last_token = last;
683 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
685 /* Initially we are not debugging. */
686 lexer->debugging_p = false;
688 gcc_assert (!lexer->next_token->purged_p);
689 return lexer;
692 /* Frees all resources associated with LEXER. */
694 static void
695 cp_lexer_destroy (cp_lexer *lexer)
697 vec_free (lexer->buffer);
698 lexer->saved_tokens.release ();
699 ggc_free (lexer);
702 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
703 be used. The point of this flag is to help the compiler to fold away calls
704 to cp_lexer_debugging_p within this source file at compile time, when the
705 lexer is not being debugged. */
707 #define LEXER_DEBUGGING_ENABLED_P false
709 /* Returns nonzero if debugging information should be output. */
711 static inline bool
712 cp_lexer_debugging_p (cp_lexer *lexer)
714 if (!LEXER_DEBUGGING_ENABLED_P)
715 return false;
717 return lexer->debugging_p;
721 static inline cp_token_position
722 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
724 gcc_assert (!previous_p || lexer->next_token != &eof_token);
726 return lexer->next_token - previous_p;
729 static inline cp_token *
730 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
732 return pos;
735 static inline void
736 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
738 lexer->next_token = cp_lexer_token_at (lexer, pos);
741 static inline cp_token_position
742 cp_lexer_previous_token_position (cp_lexer *lexer)
744 if (lexer->next_token == &eof_token)
745 return lexer->last_token - 1;
746 else
747 return cp_lexer_token_position (lexer, true);
750 static inline cp_token *
751 cp_lexer_previous_token (cp_lexer *lexer)
753 cp_token_position tp = cp_lexer_previous_token_position (lexer);
755 /* Skip past purged tokens. */
756 while (tp->purged_p)
758 gcc_assert (tp != vec_safe_address (lexer->buffer));
759 tp--;
762 return cp_lexer_token_at (lexer, tp);
765 /* nonzero if we are presently saving tokens. */
767 static inline int
768 cp_lexer_saving_tokens (const cp_lexer* lexer)
770 return lexer->saved_tokens.length () != 0;
773 /* Store the next token from the preprocessor in *TOKEN. Return true
774 if we reach EOF. If LEXER is NULL, assume we are handling an
775 initial #pragma pch_preprocess, and thus want the lexer to return
776 processed strings. */
778 static void
779 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
781 static int is_extern_c = 0;
783 /* Get a new token from the preprocessor. */
784 token->type
785 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
786 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
787 token->keyword = RID_MAX;
788 token->purged_p = false;
789 token->error_reported = false;
791 /* On some systems, some header files are surrounded by an
792 implicit extern "C" block. Set a flag in the token if it
793 comes from such a header. */
794 is_extern_c += pending_lang_change;
795 pending_lang_change = 0;
796 token->implicit_extern_c = is_extern_c > 0;
798 /* Check to see if this token is a keyword. */
799 if (token->type == CPP_NAME)
801 if (IDENTIFIER_KEYWORD_P (token->u.value))
803 /* Mark this token as a keyword. */
804 token->type = CPP_KEYWORD;
805 /* Record which keyword. */
806 token->keyword = C_RID_CODE (token->u.value);
808 else
810 if (warn_cxx11_compat
811 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
812 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
814 /* Warn about the C++0x keyword (but still treat it as
815 an identifier). */
816 warning (OPT_Wc__11_compat,
817 "identifier %qE is a keyword in C++11",
818 token->u.value);
820 /* Clear out the C_RID_CODE so we don't warn about this
821 particular identifier-turned-keyword again. */
822 C_SET_RID_CODE (token->u.value, RID_MAX);
825 token->keyword = RID_MAX;
828 else if (token->type == CPP_AT_NAME)
830 /* This only happens in Objective-C++; it must be a keyword. */
831 token->type = CPP_KEYWORD;
832 switch (C_RID_CODE (token->u.value))
834 /* Replace 'class' with '@class', 'private' with '@private',
835 etc. This prevents confusion with the C++ keyword
836 'class', and makes the tokens consistent with other
837 Objective-C 'AT' keywords. For example '@class' is
838 reported as RID_AT_CLASS which is consistent with
839 '@synchronized', which is reported as
840 RID_AT_SYNCHRONIZED.
842 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
843 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
844 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
845 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
846 case RID_THROW: token->keyword = RID_AT_THROW; break;
847 case RID_TRY: token->keyword = RID_AT_TRY; break;
848 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
849 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
850 default: token->keyword = C_RID_CODE (token->u.value);
855 /* Update the globals input_location and the input file stack from TOKEN. */
856 static inline void
857 cp_lexer_set_source_position_from_token (cp_token *token)
859 if (token->type != CPP_EOF)
861 input_location = token->location;
865 /* Update the globals input_location and the input file stack from LEXER. */
866 static inline void
867 cp_lexer_set_source_position (cp_lexer *lexer)
869 cp_token *token = cp_lexer_peek_token (lexer);
870 cp_lexer_set_source_position_from_token (token);
873 /* Return a pointer to the next token in the token stream, but do not
874 consume it. */
876 static inline cp_token *
877 cp_lexer_peek_token (cp_lexer *lexer)
879 if (cp_lexer_debugging_p (lexer))
881 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
882 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
883 putc ('\n', cp_lexer_debug_stream);
885 return lexer->next_token;
888 /* Return true if the next token has the indicated TYPE. */
890 static inline bool
891 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
893 return cp_lexer_peek_token (lexer)->type == type;
896 /* Return true if the next token does not have the indicated TYPE. */
898 static inline bool
899 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
901 return !cp_lexer_next_token_is (lexer, type);
904 /* Return true if the next token is the indicated KEYWORD. */
906 static inline bool
907 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
909 return cp_lexer_peek_token (lexer)->keyword == keyword;
912 static inline bool
913 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
915 return cp_lexer_peek_nth_token (lexer, n)->type == type;
918 static inline bool
919 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
921 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
924 /* Return true if KEYWORD can start a decl-specifier. */
926 bool
927 cp_keyword_starts_decl_specifier_p (enum rid keyword)
929 switch (keyword)
931 /* auto specifier: storage-class-specifier in C++,
932 simple-type-specifier in C++0x. */
933 case RID_AUTO:
934 /* Storage classes. */
935 case RID_REGISTER:
936 case RID_STATIC:
937 case RID_EXTERN:
938 case RID_MUTABLE:
939 case RID_THREAD:
940 /* Elaborated type specifiers. */
941 case RID_ENUM:
942 case RID_CLASS:
943 case RID_STRUCT:
944 case RID_UNION:
945 case RID_TYPENAME:
946 /* Simple type specifiers. */
947 case RID_CHAR:
948 case RID_CHAR16:
949 case RID_CHAR32:
950 case RID_WCHAR:
951 case RID_BOOL:
952 case RID_SHORT:
953 case RID_INT:
954 case RID_LONG:
955 case RID_SIGNED:
956 case RID_UNSIGNED:
957 case RID_FLOAT:
958 case RID_DOUBLE:
959 case RID_VOID:
960 /* GNU extensions. */
961 case RID_ATTRIBUTE:
962 case RID_TYPEOF:
963 /* C++0x extensions. */
964 case RID_DECLTYPE:
965 case RID_UNDERLYING_TYPE:
966 case RID_CONSTEXPR:
967 return true;
969 default:
970 if (keyword >= RID_FIRST_INT_N
971 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
972 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
973 return true;
974 return false;
978 /* Return true if the next token is a keyword for a decl-specifier. */
980 static bool
981 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
983 cp_token *token;
985 token = cp_lexer_peek_token (lexer);
986 return cp_keyword_starts_decl_specifier_p (token->keyword);
989 /* Returns TRUE iff the token T begins a decltype type. */
991 static bool
992 token_is_decltype (cp_token *t)
994 return (t->keyword == RID_DECLTYPE
995 || t->type == CPP_DECLTYPE);
998 /* Returns TRUE iff the next token begins a decltype type. */
1000 static bool
1001 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1003 cp_token *t = cp_lexer_peek_token (lexer);
1004 return token_is_decltype (t);
1007 /* Called when processing a token with tree_check_value; perform or defer the
1008 associated checks and return the value. */
1010 static tree
1011 saved_checks_value (struct tree_check *check_value)
1013 /* Perform any access checks that were deferred. */
1014 vec<deferred_access_check, va_gc> *checks;
1015 deferred_access_check *chk;
1016 checks = check_value->checks;
1017 if (checks)
1019 int i;
1020 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1021 perform_or_defer_access_check (chk->binfo,
1022 chk->decl,
1023 chk->diag_decl, tf_warning_or_error);
1025 /* Return the stored value. */
1026 return check_value->value;
1029 /* Return a pointer to the Nth token in the token stream. If N is 1,
1030 then this is precisely equivalent to cp_lexer_peek_token (except
1031 that it is not inline). One would like to disallow that case, but
1032 there is one case (cp_parser_nth_token_starts_template_id) where
1033 the caller passes a variable for N and it might be 1. */
1035 static cp_token *
1036 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1038 cp_token *token;
1040 /* N is 1-based, not zero-based. */
1041 gcc_assert (n > 0);
1043 if (cp_lexer_debugging_p (lexer))
1044 fprintf (cp_lexer_debug_stream,
1045 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1047 --n;
1048 token = lexer->next_token;
1049 gcc_assert (!n || token != &eof_token);
1050 while (n != 0)
1052 ++token;
1053 if (token == lexer->last_token)
1055 token = &eof_token;
1056 break;
1059 if (!token->purged_p)
1060 --n;
1063 if (cp_lexer_debugging_p (lexer))
1065 cp_lexer_print_token (cp_lexer_debug_stream, token);
1066 putc ('\n', cp_lexer_debug_stream);
1069 return token;
1072 /* Return the next token, and advance the lexer's next_token pointer
1073 to point to the next non-purged token. */
1075 static cp_token *
1076 cp_lexer_consume_token (cp_lexer* lexer)
1078 cp_token *token = lexer->next_token;
1080 gcc_assert (token != &eof_token);
1081 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1085 lexer->next_token++;
1086 if (lexer->next_token == lexer->last_token)
1088 lexer->next_token = &eof_token;
1089 break;
1093 while (lexer->next_token->purged_p);
1095 cp_lexer_set_source_position_from_token (token);
1097 /* Provide debugging output. */
1098 if (cp_lexer_debugging_p (lexer))
1100 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1101 cp_lexer_print_token (cp_lexer_debug_stream, token);
1102 putc ('\n', cp_lexer_debug_stream);
1105 return token;
1108 /* Permanently remove the next token from the token stream, and
1109 advance the next_token pointer to refer to the next non-purged
1110 token. */
1112 static void
1113 cp_lexer_purge_token (cp_lexer *lexer)
1115 cp_token *tok = lexer->next_token;
1117 gcc_assert (tok != &eof_token);
1118 tok->purged_p = true;
1119 tok->location = UNKNOWN_LOCATION;
1120 tok->u.value = NULL_TREE;
1121 tok->keyword = RID_MAX;
1125 tok++;
1126 if (tok == lexer->last_token)
1128 tok = &eof_token;
1129 break;
1132 while (tok->purged_p);
1133 lexer->next_token = tok;
1136 /* Permanently remove all tokens after TOK, up to, but not
1137 including, the token that will be returned next by
1138 cp_lexer_peek_token. */
1140 static void
1141 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1143 cp_token *peek = lexer->next_token;
1145 if (peek == &eof_token)
1146 peek = lexer->last_token;
1148 gcc_assert (tok < peek);
1150 for ( tok += 1; tok != peek; tok += 1)
1152 tok->purged_p = true;
1153 tok->location = UNKNOWN_LOCATION;
1154 tok->u.value = NULL_TREE;
1155 tok->keyword = RID_MAX;
1159 /* Begin saving tokens. All tokens consumed after this point will be
1160 preserved. */
1162 static void
1163 cp_lexer_save_tokens (cp_lexer* lexer)
1165 /* Provide debugging output. */
1166 if (cp_lexer_debugging_p (lexer))
1167 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1169 lexer->saved_tokens.safe_push (lexer->next_token);
1172 /* Commit to the portion of the token stream most recently saved. */
1174 static void
1175 cp_lexer_commit_tokens (cp_lexer* lexer)
1177 /* Provide debugging output. */
1178 if (cp_lexer_debugging_p (lexer))
1179 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1181 lexer->saved_tokens.pop ();
1184 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1185 to the token stream. Stop saving tokens. */
1187 static void
1188 cp_lexer_rollback_tokens (cp_lexer* lexer)
1190 /* Provide debugging output. */
1191 if (cp_lexer_debugging_p (lexer))
1192 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1194 lexer->next_token = lexer->saved_tokens.pop ();
1197 /* RAII wrapper around the above functions, with sanity checking. Creating
1198 a variable saves tokens, which are committed when the variable is
1199 destroyed unless they are explicitly rolled back by calling the rollback
1200 member function. */
1202 struct saved_token_sentinel
1204 cp_lexer *lexer;
1205 unsigned len;
1206 bool commit;
1207 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1209 len = lexer->saved_tokens.length ();
1210 cp_lexer_save_tokens (lexer);
1212 void rollback ()
1214 cp_lexer_rollback_tokens (lexer);
1215 commit = false;
1217 ~saved_token_sentinel()
1219 if (commit)
1220 cp_lexer_commit_tokens (lexer);
1221 gcc_assert (lexer->saved_tokens.length () == len);
1225 /* Print a representation of the TOKEN on the STREAM. */
1227 static void
1228 cp_lexer_print_token (FILE * stream, cp_token *token)
1230 /* We don't use cpp_type2name here because the parser defines
1231 a few tokens of its own. */
1232 static const char *const token_names[] = {
1233 /* cpplib-defined token types */
1234 #define OP(e, s) #e,
1235 #define TK(e, s) #e,
1236 TTYPE_TABLE
1237 #undef OP
1238 #undef TK
1239 /* C++ parser token types - see "Manifest constants", above. */
1240 "KEYWORD",
1241 "TEMPLATE_ID",
1242 "NESTED_NAME_SPECIFIER",
1245 /* For some tokens, print the associated data. */
1246 switch (token->type)
1248 case CPP_KEYWORD:
1249 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1250 For example, `struct' is mapped to an INTEGER_CST. */
1251 if (!identifier_p (token->u.value))
1252 break;
1253 /* fall through */
1254 case CPP_NAME:
1255 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1256 break;
1258 case CPP_STRING:
1259 case CPP_STRING16:
1260 case CPP_STRING32:
1261 case CPP_WSTRING:
1262 case CPP_UTF8STRING:
1263 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1264 break;
1266 case CPP_NUMBER:
1267 print_generic_expr (stream, token->u.value);
1268 break;
1270 default:
1271 /* If we have a name for the token, print it out. Otherwise, we
1272 simply give the numeric code. */
1273 if (token->type < ARRAY_SIZE(token_names))
1274 fputs (token_names[token->type], stream);
1275 else
1276 fprintf (stream, "[%d]", token->type);
1277 break;
1281 DEBUG_FUNCTION void
1282 debug (cp_token &ref)
1284 cp_lexer_print_token (stderr, &ref);
1285 fprintf (stderr, "\n");
1288 DEBUG_FUNCTION void
1289 debug (cp_token *ptr)
1291 if (ptr)
1292 debug (*ptr);
1293 else
1294 fprintf (stderr, "<nil>\n");
1298 /* Start emitting debugging information. */
1300 static void
1301 cp_lexer_start_debugging (cp_lexer* lexer)
1303 if (!LEXER_DEBUGGING_ENABLED_P)
1304 fatal_error (input_location,
1305 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1307 lexer->debugging_p = true;
1308 cp_lexer_debug_stream = stderr;
1311 /* Stop emitting debugging information. */
1313 static void
1314 cp_lexer_stop_debugging (cp_lexer* lexer)
1316 if (!LEXER_DEBUGGING_ENABLED_P)
1317 fatal_error (input_location,
1318 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1320 lexer->debugging_p = false;
1321 cp_lexer_debug_stream = NULL;
1324 /* Create a new cp_token_cache, representing a range of tokens. */
1326 static cp_token_cache *
1327 cp_token_cache_new (cp_token *first, cp_token *last)
1329 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1330 cache->first = first;
1331 cache->last = last;
1332 return cache;
1335 /* Diagnose if #pragma omp declare simd isn't followed immediately
1336 by function declaration or definition. */
1338 static inline void
1339 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1341 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1343 error ("%<#pragma omp declare simd%> not immediately followed by "
1344 "function declaration or definition");
1345 parser->omp_declare_simd = NULL;
1349 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1350 and put that into "omp declare simd" attribute. */
1352 static inline void
1353 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1355 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1357 if (fndecl == error_mark_node)
1359 parser->omp_declare_simd = NULL;
1360 return;
1362 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1364 cp_ensure_no_omp_declare_simd (parser);
1365 return;
1370 /* Diagnose if #pragma acc routine isn't followed immediately by function
1371 declaration or definition. */
1373 static inline void
1374 cp_ensure_no_oacc_routine (cp_parser *parser)
1376 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1378 error_at (parser->oacc_routine->loc,
1379 "%<#pragma acc routine%> not immediately followed by "
1380 "function declaration or definition");
1381 parser->oacc_routine = NULL;
1385 /* Decl-specifiers. */
1387 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1389 static void
1390 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1392 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1395 /* Declarators. */
1397 /* Nothing other than the parser should be creating declarators;
1398 declarators are a semi-syntactic representation of C++ entities.
1399 Other parts of the front end that need to create entities (like
1400 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1402 static cp_declarator *make_call_declarator
1403 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1404 static cp_declarator *make_array_declarator
1405 (cp_declarator *, tree);
1406 static cp_declarator *make_pointer_declarator
1407 (cp_cv_quals, cp_declarator *, tree);
1408 static cp_declarator *make_reference_declarator
1409 (cp_cv_quals, cp_declarator *, bool, tree);
1410 static cp_declarator *make_ptrmem_declarator
1411 (cp_cv_quals, tree, cp_declarator *, tree);
1413 /* An erroneous declarator. */
1414 static cp_declarator *cp_error_declarator;
1416 /* The obstack on which declarators and related data structures are
1417 allocated. */
1418 static struct obstack declarator_obstack;
1420 /* Alloc BYTES from the declarator memory pool. */
1422 static inline void *
1423 alloc_declarator (size_t bytes)
1425 return obstack_alloc (&declarator_obstack, bytes);
1428 /* Allocate a declarator of the indicated KIND. Clear fields that are
1429 common to all declarators. */
1431 static cp_declarator *
1432 make_declarator (cp_declarator_kind kind)
1434 cp_declarator *declarator;
1436 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1437 declarator->kind = kind;
1438 declarator->parenthesized = UNKNOWN_LOCATION;
1439 declarator->attributes = NULL_TREE;
1440 declarator->std_attributes = NULL_TREE;
1441 declarator->declarator = NULL;
1442 declarator->parameter_pack_p = false;
1443 declarator->id_loc = UNKNOWN_LOCATION;
1445 return declarator;
1448 /* Make a declarator for a generalized identifier. If
1449 QUALIFYING_SCOPE is non-NULL, the identifier is
1450 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1451 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1452 is, if any. */
1454 static cp_declarator *
1455 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1456 special_function_kind sfk)
1458 cp_declarator *declarator;
1460 /* It is valid to write:
1462 class C { void f(); };
1463 typedef C D;
1464 void D::f();
1466 The standard is not clear about whether `typedef const C D' is
1467 legal; as of 2002-09-15 the committee is considering that
1468 question. EDG 3.0 allows that syntax. Therefore, we do as
1469 well. */
1470 if (qualifying_scope && TYPE_P (qualifying_scope))
1471 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1473 gcc_assert (identifier_p (unqualified_name)
1474 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1475 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1477 declarator = make_declarator (cdk_id);
1478 declarator->u.id.qualifying_scope = qualifying_scope;
1479 declarator->u.id.unqualified_name = unqualified_name;
1480 declarator->u.id.sfk = sfk;
1482 return declarator;
1485 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1486 of modifiers such as const or volatile to apply to the pointer
1487 type, represented as identifiers. ATTRIBUTES represent the attributes that
1488 appertain to the pointer or reference. */
1490 cp_declarator *
1491 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1492 tree attributes)
1494 cp_declarator *declarator;
1496 declarator = make_declarator (cdk_pointer);
1497 declarator->declarator = target;
1498 declarator->u.pointer.qualifiers = cv_qualifiers;
1499 declarator->u.pointer.class_type = NULL_TREE;
1500 if (target)
1502 declarator->id_loc = target->id_loc;
1503 declarator->parameter_pack_p = target->parameter_pack_p;
1504 target->parameter_pack_p = false;
1506 else
1507 declarator->parameter_pack_p = false;
1509 declarator->std_attributes = attributes;
1511 return declarator;
1514 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1515 represent the attributes that appertain to the pointer or
1516 reference. */
1518 cp_declarator *
1519 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1520 bool rvalue_ref, tree attributes)
1522 cp_declarator *declarator;
1524 declarator = make_declarator (cdk_reference);
1525 declarator->declarator = target;
1526 declarator->u.reference.qualifiers = cv_qualifiers;
1527 declarator->u.reference.rvalue_ref = rvalue_ref;
1528 if (target)
1530 declarator->id_loc = target->id_loc;
1531 declarator->parameter_pack_p = target->parameter_pack_p;
1532 target->parameter_pack_p = false;
1534 else
1535 declarator->parameter_pack_p = false;
1537 declarator->std_attributes = attributes;
1539 return declarator;
1542 /* Like make_pointer_declarator -- but for a pointer to a non-static
1543 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1544 appertain to the pointer or reference. */
1546 cp_declarator *
1547 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1548 cp_declarator *pointee,
1549 tree attributes)
1551 cp_declarator *declarator;
1553 declarator = make_declarator (cdk_ptrmem);
1554 declarator->declarator = pointee;
1555 declarator->u.pointer.qualifiers = cv_qualifiers;
1556 declarator->u.pointer.class_type = class_type;
1558 if (pointee)
1560 declarator->parameter_pack_p = pointee->parameter_pack_p;
1561 pointee->parameter_pack_p = false;
1563 else
1564 declarator->parameter_pack_p = false;
1566 declarator->std_attributes = attributes;
1568 return declarator;
1571 /* Make a declarator for the function given by TARGET, with the
1572 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1573 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1574 indicates what exceptions can be thrown. */
1576 cp_declarator *
1577 make_call_declarator (cp_declarator *target,
1578 tree parms,
1579 cp_cv_quals cv_qualifiers,
1580 cp_virt_specifiers virt_specifiers,
1581 cp_ref_qualifier ref_qualifier,
1582 tree tx_qualifier,
1583 tree exception_specification,
1584 tree late_return_type,
1585 tree requires_clause)
1587 cp_declarator *declarator;
1589 declarator = make_declarator (cdk_function);
1590 declarator->declarator = target;
1591 declarator->u.function.parameters = parms;
1592 declarator->u.function.qualifiers = cv_qualifiers;
1593 declarator->u.function.virt_specifiers = virt_specifiers;
1594 declarator->u.function.ref_qualifier = ref_qualifier;
1595 declarator->u.function.tx_qualifier = tx_qualifier;
1596 declarator->u.function.exception_specification = exception_specification;
1597 declarator->u.function.late_return_type = late_return_type;
1598 declarator->u.function.requires_clause = requires_clause;
1599 if (target)
1601 declarator->id_loc = target->id_loc;
1602 declarator->parameter_pack_p = target->parameter_pack_p;
1603 target->parameter_pack_p = false;
1605 else
1606 declarator->parameter_pack_p = false;
1608 return declarator;
1611 /* Make a declarator for an array of BOUNDS elements, each of which is
1612 defined by ELEMENT. */
1614 cp_declarator *
1615 make_array_declarator (cp_declarator *element, tree bounds)
1617 cp_declarator *declarator;
1619 declarator = make_declarator (cdk_array);
1620 declarator->declarator = element;
1621 declarator->u.array.bounds = bounds;
1622 if (element)
1624 declarator->id_loc = element->id_loc;
1625 declarator->parameter_pack_p = element->parameter_pack_p;
1626 element->parameter_pack_p = false;
1628 else
1629 declarator->parameter_pack_p = false;
1631 return declarator;
1634 /* Determine whether the declarator we've seen so far can be a
1635 parameter pack, when followed by an ellipsis. */
1636 static bool
1637 declarator_can_be_parameter_pack (cp_declarator *declarator)
1639 if (declarator && declarator->parameter_pack_p)
1640 /* We already saw an ellipsis. */
1641 return false;
1643 /* Search for a declarator name, or any other declarator that goes
1644 after the point where the ellipsis could appear in a parameter
1645 pack. If we find any of these, then this declarator can not be
1646 made into a parameter pack. */
1647 bool found = false;
1648 while (declarator && !found)
1650 switch ((int)declarator->kind)
1652 case cdk_id:
1653 case cdk_array:
1654 case cdk_decomp:
1655 found = true;
1656 break;
1658 case cdk_error:
1659 return true;
1661 default:
1662 declarator = declarator->declarator;
1663 break;
1667 return !found;
1670 cp_parameter_declarator *no_parameters;
1672 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1673 DECLARATOR and DEFAULT_ARGUMENT. */
1675 cp_parameter_declarator *
1676 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1677 cp_declarator *declarator,
1678 tree default_argument,
1679 location_t loc,
1680 bool template_parameter_pack_p = false)
1682 cp_parameter_declarator *parameter;
1684 parameter = ((cp_parameter_declarator *)
1685 alloc_declarator (sizeof (cp_parameter_declarator)));
1686 parameter->next = NULL;
1687 if (decl_specifiers)
1688 parameter->decl_specifiers = *decl_specifiers;
1689 else
1690 clear_decl_specs (&parameter->decl_specifiers);
1691 parameter->declarator = declarator;
1692 parameter->default_argument = default_argument;
1693 parameter->template_parameter_pack_p = template_parameter_pack_p;
1694 parameter->loc = loc;
1696 return parameter;
1699 /* Returns true iff DECLARATOR is a declaration for a function. */
1701 static bool
1702 function_declarator_p (const cp_declarator *declarator)
1704 while (declarator)
1706 if (declarator->kind == cdk_function
1707 && declarator->declarator->kind == cdk_id)
1708 return true;
1709 if (declarator->kind == cdk_id
1710 || declarator->kind == cdk_decomp
1711 || declarator->kind == cdk_error)
1712 return false;
1713 declarator = declarator->declarator;
1715 return false;
1718 /* The parser. */
1720 /* Overview
1721 --------
1723 A cp_parser parses the token stream as specified by the C++
1724 grammar. Its job is purely parsing, not semantic analysis. For
1725 example, the parser breaks the token stream into declarators,
1726 expressions, statements, and other similar syntactic constructs.
1727 It does not check that the types of the expressions on either side
1728 of an assignment-statement are compatible, or that a function is
1729 not declared with a parameter of type `void'.
1731 The parser invokes routines elsewhere in the compiler to perform
1732 semantic analysis and to build up the abstract syntax tree for the
1733 code processed.
1735 The parser (and the template instantiation code, which is, in a
1736 way, a close relative of parsing) are the only parts of the
1737 compiler that should be calling push_scope and pop_scope, or
1738 related functions. The parser (and template instantiation code)
1739 keeps track of what scope is presently active; everything else
1740 should simply honor that. (The code that generates static
1741 initializers may also need to set the scope, in order to check
1742 access control correctly when emitting the initializers.)
1744 Methodology
1745 -----------
1747 The parser is of the standard recursive-descent variety. Upcoming
1748 tokens in the token stream are examined in order to determine which
1749 production to use when parsing a non-terminal. Some C++ constructs
1750 require arbitrary look ahead to disambiguate. For example, it is
1751 impossible, in the general case, to tell whether a statement is an
1752 expression or declaration without scanning the entire statement.
1753 Therefore, the parser is capable of "parsing tentatively." When the
1754 parser is not sure what construct comes next, it enters this mode.
1755 Then, while we attempt to parse the construct, the parser queues up
1756 error messages, rather than issuing them immediately, and saves the
1757 tokens it consumes. If the construct is parsed successfully, the
1758 parser "commits", i.e., it issues any queued error messages and
1759 the tokens that were being preserved are permanently discarded.
1760 If, however, the construct is not parsed successfully, the parser
1761 rolls back its state completely so that it can resume parsing using
1762 a different alternative.
1764 Future Improvements
1765 -------------------
1767 The performance of the parser could probably be improved substantially.
1768 We could often eliminate the need to parse tentatively by looking ahead
1769 a little bit. In some places, this approach might not entirely eliminate
1770 the need to parse tentatively, but it might still speed up the average
1771 case. */
1773 /* Flags that are passed to some parsing functions. These values can
1774 be bitwise-ored together. */
1776 enum
1778 /* No flags. */
1779 CP_PARSER_FLAGS_NONE = 0x0,
1780 /* The construct is optional. If it is not present, then no error
1781 should be issued. */
1782 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1783 /* When parsing a type-specifier, treat user-defined type-names
1784 as non-type identifiers. */
1785 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1786 /* When parsing a type-specifier, do not try to parse a class-specifier
1787 or enum-specifier. */
1788 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1789 /* When parsing a decl-specifier-seq, only allow type-specifier or
1790 constexpr. */
1791 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1792 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1793 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1796 /* This type is used for parameters and variables which hold
1797 combinations of the above flags. */
1798 typedef int cp_parser_flags;
1800 /* The different kinds of declarators we want to parse. */
1802 enum cp_parser_declarator_kind
1804 /* We want an abstract declarator. */
1805 CP_PARSER_DECLARATOR_ABSTRACT,
1806 /* We want a named declarator. */
1807 CP_PARSER_DECLARATOR_NAMED,
1808 /* We don't mind, but the name must be an unqualified-id. */
1809 CP_PARSER_DECLARATOR_EITHER
1812 /* The precedence values used to parse binary expressions. The minimum value
1813 of PREC must be 1, because zero is reserved to quickly discriminate
1814 binary operators from other tokens. */
1816 enum cp_parser_prec
1818 PREC_NOT_OPERATOR,
1819 PREC_LOGICAL_OR_EXPRESSION,
1820 PREC_LOGICAL_AND_EXPRESSION,
1821 PREC_INCLUSIVE_OR_EXPRESSION,
1822 PREC_EXCLUSIVE_OR_EXPRESSION,
1823 PREC_AND_EXPRESSION,
1824 PREC_EQUALITY_EXPRESSION,
1825 PREC_RELATIONAL_EXPRESSION,
1826 PREC_SHIFT_EXPRESSION,
1827 PREC_ADDITIVE_EXPRESSION,
1828 PREC_MULTIPLICATIVE_EXPRESSION,
1829 PREC_PM_EXPRESSION,
1830 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1833 /* A mapping from a token type to a corresponding tree node type, with a
1834 precedence value. */
1836 struct cp_parser_binary_operations_map_node
1838 /* The token type. */
1839 enum cpp_ttype token_type;
1840 /* The corresponding tree code. */
1841 enum tree_code tree_type;
1842 /* The precedence of this operator. */
1843 enum cp_parser_prec prec;
1846 struct cp_parser_expression_stack_entry
1848 /* Left hand side of the binary operation we are currently
1849 parsing. */
1850 cp_expr lhs;
1851 /* Original tree code for left hand side, if it was a binary
1852 expression itself (used for -Wparentheses). */
1853 enum tree_code lhs_type;
1854 /* Tree code for the binary operation we are parsing. */
1855 enum tree_code tree_type;
1856 /* Precedence of the binary operation we are parsing. */
1857 enum cp_parser_prec prec;
1858 /* Location of the binary operation we are parsing. */
1859 location_t loc;
1862 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1863 entries because precedence levels on the stack are monotonically
1864 increasing. */
1865 typedef struct cp_parser_expression_stack_entry
1866 cp_parser_expression_stack[NUM_PREC_VALUES];
1868 /* Prototypes. */
1870 /* Constructors and destructors. */
1872 static cp_parser_context *cp_parser_context_new
1873 (cp_parser_context *);
1875 /* Class variables. */
1877 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1879 /* The operator-precedence table used by cp_parser_binary_expression.
1880 Transformed into an associative array (binops_by_token) by
1881 cp_parser_new. */
1883 static const cp_parser_binary_operations_map_node binops[] = {
1884 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1885 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1887 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1888 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1889 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1891 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1892 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1894 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1895 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1897 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1898 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1899 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1900 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1902 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1903 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1905 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1907 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1909 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1911 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1913 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1916 /* The same as binops, but initialized by cp_parser_new so that
1917 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1918 for speed. */
1919 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1921 /* Constructors and destructors. */
1923 /* Construct a new context. The context below this one on the stack
1924 is given by NEXT. */
1926 static cp_parser_context *
1927 cp_parser_context_new (cp_parser_context* next)
1929 cp_parser_context *context;
1931 /* Allocate the storage. */
1932 if (cp_parser_context_free_list != NULL)
1934 /* Pull the first entry from the free list. */
1935 context = cp_parser_context_free_list;
1936 cp_parser_context_free_list = context->next;
1937 memset (context, 0, sizeof (*context));
1939 else
1940 context = ggc_cleared_alloc<cp_parser_context> ();
1942 /* No errors have occurred yet in this context. */
1943 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1944 /* If this is not the bottommost context, copy information that we
1945 need from the previous context. */
1946 if (next)
1948 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1949 expression, then we are parsing one in this context, too. */
1950 context->object_type = next->object_type;
1951 /* Thread the stack. */
1952 context->next = next;
1955 return context;
1958 /* Managing the unparsed function queues. */
1960 #define unparsed_funs_with_default_args \
1961 parser->unparsed_queues->last ().funs_with_default_args
1962 #define unparsed_funs_with_definitions \
1963 parser->unparsed_queues->last ().funs_with_definitions
1964 #define unparsed_nsdmis \
1965 parser->unparsed_queues->last ().nsdmis
1966 #define unparsed_classes \
1967 parser->unparsed_queues->last ().classes
1969 static void
1970 push_unparsed_function_queues (cp_parser *parser)
1972 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1973 vec_safe_push (parser->unparsed_queues, e);
1976 static void
1977 pop_unparsed_function_queues (cp_parser *parser)
1979 release_tree_vector (unparsed_funs_with_definitions);
1980 parser->unparsed_queues->pop ();
1983 /* Prototypes. */
1985 /* Constructors and destructors. */
1987 static cp_parser *cp_parser_new
1988 (void);
1990 /* Routines to parse various constructs.
1992 Those that return `tree' will return the error_mark_node (rather
1993 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1994 Sometimes, they will return an ordinary node if error-recovery was
1995 attempted, even though a parse error occurred. So, to check
1996 whether or not a parse error occurred, you should always use
1997 cp_parser_error_occurred. If the construct is optional (indicated
1998 either by an `_opt' in the name of the function that does the
1999 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2000 the construct is not present. */
2002 /* Lexical conventions [gram.lex] */
2004 static cp_expr cp_parser_identifier
2005 (cp_parser *);
2006 static cp_expr cp_parser_string_literal
2007 (cp_parser *, bool, bool, bool);
2008 static cp_expr cp_parser_userdef_char_literal
2009 (cp_parser *);
2010 static tree cp_parser_userdef_string_literal
2011 (tree);
2012 static cp_expr cp_parser_userdef_numeric_literal
2013 (cp_parser *);
2015 /* Basic concepts [gram.basic] */
2017 static void cp_parser_translation_unit (cp_parser *);
2019 /* Expressions [gram.expr] */
2021 static cp_expr cp_parser_primary_expression
2022 (cp_parser *, bool, bool, bool, cp_id_kind *);
2023 static cp_expr cp_parser_id_expression
2024 (cp_parser *, bool, bool, bool *, bool, bool);
2025 static cp_expr cp_parser_unqualified_id
2026 (cp_parser *, bool, bool, bool, bool);
2027 static tree cp_parser_nested_name_specifier_opt
2028 (cp_parser *, bool, bool, bool, bool, bool = false);
2029 static tree cp_parser_nested_name_specifier
2030 (cp_parser *, bool, bool, bool, bool);
2031 static tree cp_parser_qualifying_entity
2032 (cp_parser *, bool, bool, bool, bool, bool);
2033 static cp_expr cp_parser_postfix_expression
2034 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2035 static tree cp_parser_postfix_open_square_expression
2036 (cp_parser *, tree, bool, bool);
2037 static tree cp_parser_postfix_dot_deref_expression
2038 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2039 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2040 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2041 bool = false);
2042 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2043 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2044 static void cp_parser_pseudo_destructor_name
2045 (cp_parser *, tree, tree *, tree *);
2046 static cp_expr cp_parser_unary_expression
2047 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2048 static enum tree_code cp_parser_unary_operator
2049 (cp_token *);
2050 static tree cp_parser_new_expression
2051 (cp_parser *);
2052 static vec<tree, va_gc> *cp_parser_new_placement
2053 (cp_parser *);
2054 static tree cp_parser_new_type_id
2055 (cp_parser *, tree *);
2056 static cp_declarator *cp_parser_new_declarator_opt
2057 (cp_parser *);
2058 static cp_declarator *cp_parser_direct_new_declarator
2059 (cp_parser *);
2060 static vec<tree, va_gc> *cp_parser_new_initializer
2061 (cp_parser *);
2062 static tree cp_parser_delete_expression
2063 (cp_parser *);
2064 static cp_expr cp_parser_cast_expression
2065 (cp_parser *, bool, bool, bool, cp_id_kind *);
2066 static cp_expr cp_parser_binary_expression
2067 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2068 static tree cp_parser_question_colon_clause
2069 (cp_parser *, cp_expr);
2070 static cp_expr cp_parser_assignment_expression
2071 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2072 static enum tree_code cp_parser_assignment_operator_opt
2073 (cp_parser *);
2074 static cp_expr cp_parser_expression
2075 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2076 static cp_expr cp_parser_constant_expression
2077 (cp_parser *, bool = false, bool * = NULL, bool = false);
2078 static cp_expr cp_parser_builtin_offsetof
2079 (cp_parser *);
2080 static cp_expr cp_parser_lambda_expression
2081 (cp_parser *);
2082 static void cp_parser_lambda_introducer
2083 (cp_parser *, tree);
2084 static bool cp_parser_lambda_declarator_opt
2085 (cp_parser *, tree);
2086 static void cp_parser_lambda_body
2087 (cp_parser *, tree);
2089 /* Statements [gram.stmt.stmt] */
2091 static void cp_parser_statement
2092 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2093 static void cp_parser_label_for_labeled_statement
2094 (cp_parser *, tree);
2095 static tree cp_parser_expression_statement
2096 (cp_parser *, tree);
2097 static tree cp_parser_compound_statement
2098 (cp_parser *, tree, int, bool);
2099 static void cp_parser_statement_seq_opt
2100 (cp_parser *, tree);
2101 static tree cp_parser_selection_statement
2102 (cp_parser *, bool *, vec<tree> *);
2103 static tree cp_parser_condition
2104 (cp_parser *);
2105 static tree cp_parser_iteration_statement
2106 (cp_parser *, bool *, bool, unsigned short);
2107 static bool cp_parser_init_statement
2108 (cp_parser *, tree *decl);
2109 static tree cp_parser_for
2110 (cp_parser *, bool, unsigned short);
2111 static tree cp_parser_c_for
2112 (cp_parser *, tree, tree, bool, unsigned short);
2113 static tree cp_parser_range_for
2114 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2115 static void do_range_for_auto_deduction
2116 (tree, tree);
2117 static tree cp_parser_perform_range_for_lookup
2118 (tree, tree *, tree *);
2119 static tree cp_parser_range_for_member_function
2120 (tree, tree);
2121 static tree cp_parser_jump_statement
2122 (cp_parser *);
2123 static void cp_parser_declaration_statement
2124 (cp_parser *);
2126 static tree cp_parser_implicitly_scoped_statement
2127 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2128 static void cp_parser_already_scoped_statement
2129 (cp_parser *, bool *, const token_indent_info &);
2131 /* Declarations [gram.dcl.dcl] */
2133 static void cp_parser_declaration_seq_opt
2134 (cp_parser *);
2135 static void cp_parser_declaration
2136 (cp_parser *);
2137 static void cp_parser_toplevel_declaration
2138 (cp_parser *);
2139 static void cp_parser_block_declaration
2140 (cp_parser *, bool);
2141 static void cp_parser_simple_declaration
2142 (cp_parser *, bool, tree *);
2143 static void cp_parser_decl_specifier_seq
2144 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2145 static tree cp_parser_storage_class_specifier_opt
2146 (cp_parser *);
2147 static tree cp_parser_function_specifier_opt
2148 (cp_parser *, cp_decl_specifier_seq *);
2149 static tree cp_parser_type_specifier
2150 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2151 int *, bool *);
2152 static tree cp_parser_simple_type_specifier
2153 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2154 static tree cp_parser_type_name
2155 (cp_parser *, bool);
2156 static tree cp_parser_type_name
2157 (cp_parser *);
2158 static tree cp_parser_nonclass_name
2159 (cp_parser* parser);
2160 static tree cp_parser_elaborated_type_specifier
2161 (cp_parser *, bool, bool);
2162 static tree cp_parser_enum_specifier
2163 (cp_parser *);
2164 static void cp_parser_enumerator_list
2165 (cp_parser *, tree);
2166 static void cp_parser_enumerator_definition
2167 (cp_parser *, tree);
2168 static tree cp_parser_namespace_name
2169 (cp_parser *);
2170 static void cp_parser_namespace_definition
2171 (cp_parser *);
2172 static void cp_parser_namespace_body
2173 (cp_parser *);
2174 static tree cp_parser_qualified_namespace_specifier
2175 (cp_parser *);
2176 static void cp_parser_namespace_alias_definition
2177 (cp_parser *);
2178 static bool cp_parser_using_declaration
2179 (cp_parser *, bool);
2180 static void cp_parser_using_directive
2181 (cp_parser *);
2182 static tree cp_parser_alias_declaration
2183 (cp_parser *);
2184 static void cp_parser_asm_definition
2185 (cp_parser *);
2186 static void cp_parser_linkage_specification
2187 (cp_parser *);
2188 static void cp_parser_static_assert
2189 (cp_parser *, bool);
2190 static tree cp_parser_decltype
2191 (cp_parser *);
2192 static tree cp_parser_decomposition_declaration
2193 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2195 /* Declarators [gram.dcl.decl] */
2197 static tree cp_parser_init_declarator
2198 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2199 bool, bool, int, bool *, tree *, location_t *, tree *);
2200 static cp_declarator *cp_parser_declarator
2201 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2202 static cp_declarator *cp_parser_direct_declarator
2203 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2204 static enum tree_code cp_parser_ptr_operator
2205 (cp_parser *, tree *, cp_cv_quals *, tree *);
2206 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2207 (cp_parser *);
2208 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2209 (cp_parser *);
2210 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2211 (cp_parser *);
2212 static tree cp_parser_tx_qualifier_opt
2213 (cp_parser *);
2214 static tree cp_parser_late_return_type_opt
2215 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2216 static tree cp_parser_declarator_id
2217 (cp_parser *, bool);
2218 static tree cp_parser_type_id
2219 (cp_parser *, location_t * = NULL);
2220 static tree cp_parser_template_type_arg
2221 (cp_parser *);
2222 static tree cp_parser_trailing_type_id (cp_parser *);
2223 static tree cp_parser_type_id_1
2224 (cp_parser *, bool, bool, location_t *);
2225 static void cp_parser_type_specifier_seq
2226 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2227 static tree cp_parser_parameter_declaration_clause
2228 (cp_parser *);
2229 static tree cp_parser_parameter_declaration_list
2230 (cp_parser *);
2231 static cp_parameter_declarator *cp_parser_parameter_declaration
2232 (cp_parser *, bool, bool *);
2233 static tree cp_parser_default_argument
2234 (cp_parser *, bool);
2235 static void cp_parser_function_body
2236 (cp_parser *, bool);
2237 static tree cp_parser_initializer
2238 (cp_parser *, bool *, bool *, bool = false);
2239 static cp_expr cp_parser_initializer_clause
2240 (cp_parser *, bool *);
2241 static cp_expr cp_parser_braced_list
2242 (cp_parser*, bool*);
2243 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2244 (cp_parser *, bool *);
2246 static void cp_parser_ctor_initializer_opt_and_function_body
2247 (cp_parser *, bool);
2249 static tree cp_parser_late_parsing_omp_declare_simd
2250 (cp_parser *, tree);
2252 static tree cp_parser_late_parsing_oacc_routine
2253 (cp_parser *, tree);
2255 static tree synthesize_implicit_template_parm
2256 (cp_parser *, tree);
2257 static tree finish_fully_implicit_template
2258 (cp_parser *, tree);
2259 static void abort_fully_implicit_template
2260 (cp_parser *);
2262 /* Classes [gram.class] */
2264 static tree cp_parser_class_name
2265 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2266 static tree cp_parser_class_specifier
2267 (cp_parser *);
2268 static tree cp_parser_class_head
2269 (cp_parser *, bool *);
2270 static enum tag_types cp_parser_class_key
2271 (cp_parser *);
2272 static void cp_parser_type_parameter_key
2273 (cp_parser* parser);
2274 static void cp_parser_member_specification_opt
2275 (cp_parser *);
2276 static void cp_parser_member_declaration
2277 (cp_parser *);
2278 static tree cp_parser_pure_specifier
2279 (cp_parser *);
2280 static tree cp_parser_constant_initializer
2281 (cp_parser *);
2283 /* Derived classes [gram.class.derived] */
2285 static tree cp_parser_base_clause
2286 (cp_parser *);
2287 static tree cp_parser_base_specifier
2288 (cp_parser *);
2290 /* Special member functions [gram.special] */
2292 static tree cp_parser_conversion_function_id
2293 (cp_parser *);
2294 static tree cp_parser_conversion_type_id
2295 (cp_parser *);
2296 static cp_declarator *cp_parser_conversion_declarator_opt
2297 (cp_parser *);
2298 static void cp_parser_ctor_initializer_opt
2299 (cp_parser *);
2300 static void cp_parser_mem_initializer_list
2301 (cp_parser *);
2302 static tree cp_parser_mem_initializer
2303 (cp_parser *);
2304 static tree cp_parser_mem_initializer_id
2305 (cp_parser *);
2307 /* Overloading [gram.over] */
2309 static cp_expr cp_parser_operator_function_id
2310 (cp_parser *);
2311 static cp_expr cp_parser_operator
2312 (cp_parser *);
2314 /* Templates [gram.temp] */
2316 static void cp_parser_template_declaration
2317 (cp_parser *, bool);
2318 static tree cp_parser_template_parameter_list
2319 (cp_parser *);
2320 static tree cp_parser_template_parameter
2321 (cp_parser *, bool *, bool *);
2322 static tree cp_parser_type_parameter
2323 (cp_parser *, bool *);
2324 static tree cp_parser_template_id
2325 (cp_parser *, bool, bool, enum tag_types, bool);
2326 static tree cp_parser_template_name
2327 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2328 static tree cp_parser_template_argument_list
2329 (cp_parser *);
2330 static tree cp_parser_template_argument
2331 (cp_parser *);
2332 static void cp_parser_explicit_instantiation
2333 (cp_parser *);
2334 static void cp_parser_explicit_specialization
2335 (cp_parser *);
2337 /* Exception handling [gram.exception] */
2339 static tree cp_parser_try_block
2340 (cp_parser *);
2341 static void cp_parser_function_try_block
2342 (cp_parser *);
2343 static void cp_parser_handler_seq
2344 (cp_parser *);
2345 static void cp_parser_handler
2346 (cp_parser *);
2347 static tree cp_parser_exception_declaration
2348 (cp_parser *);
2349 static tree cp_parser_throw_expression
2350 (cp_parser *);
2351 static tree cp_parser_exception_specification_opt
2352 (cp_parser *);
2353 static tree cp_parser_type_id_list
2354 (cp_parser *);
2356 /* GNU Extensions */
2358 static tree cp_parser_asm_specification_opt
2359 (cp_parser *);
2360 static tree cp_parser_asm_operand_list
2361 (cp_parser *);
2362 static tree cp_parser_asm_clobber_list
2363 (cp_parser *);
2364 static tree cp_parser_asm_label_list
2365 (cp_parser *);
2366 static bool cp_next_tokens_can_be_attribute_p
2367 (cp_parser *);
2368 static bool cp_next_tokens_can_be_gnu_attribute_p
2369 (cp_parser *);
2370 static bool cp_next_tokens_can_be_std_attribute_p
2371 (cp_parser *);
2372 static bool cp_nth_tokens_can_be_std_attribute_p
2373 (cp_parser *, size_t);
2374 static bool cp_nth_tokens_can_be_gnu_attribute_p
2375 (cp_parser *, size_t);
2376 static bool cp_nth_tokens_can_be_attribute_p
2377 (cp_parser *, size_t);
2378 static tree cp_parser_attributes_opt
2379 (cp_parser *);
2380 static tree cp_parser_gnu_attributes_opt
2381 (cp_parser *);
2382 static tree cp_parser_gnu_attribute_list
2383 (cp_parser *);
2384 static tree cp_parser_std_attribute
2385 (cp_parser *, tree);
2386 static tree cp_parser_std_attribute_spec
2387 (cp_parser *);
2388 static tree cp_parser_std_attribute_spec_seq
2389 (cp_parser *);
2390 static size_t cp_parser_skip_attributes_opt
2391 (cp_parser *, size_t);
2392 static bool cp_parser_extension_opt
2393 (cp_parser *, int *);
2394 static void cp_parser_label_declaration
2395 (cp_parser *);
2397 /* Concept Extensions */
2399 static tree cp_parser_requires_clause
2400 (cp_parser *);
2401 static tree cp_parser_requires_clause_opt
2402 (cp_parser *);
2403 static tree cp_parser_requires_expression
2404 (cp_parser *);
2405 static tree cp_parser_requirement_parameter_list
2406 (cp_parser *);
2407 static tree cp_parser_requirement_body
2408 (cp_parser *);
2409 static tree cp_parser_requirement_list
2410 (cp_parser *);
2411 static tree cp_parser_requirement
2412 (cp_parser *);
2413 static tree cp_parser_simple_requirement
2414 (cp_parser *);
2415 static tree cp_parser_compound_requirement
2416 (cp_parser *);
2417 static tree cp_parser_type_requirement
2418 (cp_parser *);
2419 static tree cp_parser_nested_requirement
2420 (cp_parser *);
2422 /* Transactional Memory Extensions */
2424 static tree cp_parser_transaction
2425 (cp_parser *, cp_token *);
2426 static tree cp_parser_transaction_expression
2427 (cp_parser *, enum rid);
2428 static void cp_parser_function_transaction
2429 (cp_parser *, enum rid);
2430 static tree cp_parser_transaction_cancel
2431 (cp_parser *);
2433 enum pragma_context {
2434 pragma_external,
2435 pragma_member,
2436 pragma_objc_icode,
2437 pragma_stmt,
2438 pragma_compound
2440 static bool cp_parser_pragma
2441 (cp_parser *, enum pragma_context, bool *);
2443 /* Objective-C++ Productions */
2445 static tree cp_parser_objc_message_receiver
2446 (cp_parser *);
2447 static tree cp_parser_objc_message_args
2448 (cp_parser *);
2449 static tree cp_parser_objc_message_expression
2450 (cp_parser *);
2451 static cp_expr cp_parser_objc_encode_expression
2452 (cp_parser *);
2453 static tree cp_parser_objc_defs_expression
2454 (cp_parser *);
2455 static tree cp_parser_objc_protocol_expression
2456 (cp_parser *);
2457 static tree cp_parser_objc_selector_expression
2458 (cp_parser *);
2459 static cp_expr cp_parser_objc_expression
2460 (cp_parser *);
2461 static bool cp_parser_objc_selector_p
2462 (enum cpp_ttype);
2463 static tree cp_parser_objc_selector
2464 (cp_parser *);
2465 static tree cp_parser_objc_protocol_refs_opt
2466 (cp_parser *);
2467 static void cp_parser_objc_declaration
2468 (cp_parser *, tree);
2469 static tree cp_parser_objc_statement
2470 (cp_parser *);
2471 static bool cp_parser_objc_valid_prefix_attributes
2472 (cp_parser *, tree *);
2473 static void cp_parser_objc_at_property_declaration
2474 (cp_parser *) ;
2475 static void cp_parser_objc_at_synthesize_declaration
2476 (cp_parser *) ;
2477 static void cp_parser_objc_at_dynamic_declaration
2478 (cp_parser *) ;
2479 static tree cp_parser_objc_struct_declaration
2480 (cp_parser *) ;
2482 /* Utility Routines */
2484 static cp_expr cp_parser_lookup_name
2485 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2486 static tree cp_parser_lookup_name_simple
2487 (cp_parser *, tree, location_t);
2488 static tree cp_parser_maybe_treat_template_as_class
2489 (tree, bool);
2490 static bool cp_parser_check_declarator_template_parameters
2491 (cp_parser *, cp_declarator *, location_t);
2492 static bool cp_parser_check_template_parameters
2493 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2494 static cp_expr cp_parser_simple_cast_expression
2495 (cp_parser *);
2496 static tree cp_parser_global_scope_opt
2497 (cp_parser *, bool);
2498 static bool cp_parser_constructor_declarator_p
2499 (cp_parser *, bool);
2500 static tree cp_parser_function_definition_from_specifiers_and_declarator
2501 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2502 static tree cp_parser_function_definition_after_declarator
2503 (cp_parser *, bool);
2504 static bool cp_parser_template_declaration_after_export
2505 (cp_parser *, bool);
2506 static void cp_parser_perform_template_parameter_access_checks
2507 (vec<deferred_access_check, va_gc> *);
2508 static tree cp_parser_single_declaration
2509 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2510 static cp_expr cp_parser_functional_cast
2511 (cp_parser *, tree);
2512 static tree cp_parser_save_member_function_body
2513 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2514 static tree cp_parser_save_nsdmi
2515 (cp_parser *);
2516 static tree cp_parser_enclosed_template_argument_list
2517 (cp_parser *);
2518 static void cp_parser_save_default_args
2519 (cp_parser *, tree);
2520 static void cp_parser_late_parsing_for_member
2521 (cp_parser *, tree);
2522 static tree cp_parser_late_parse_one_default_arg
2523 (cp_parser *, tree, tree, tree);
2524 static void cp_parser_late_parsing_nsdmi
2525 (cp_parser *, tree);
2526 static void cp_parser_late_parsing_default_args
2527 (cp_parser *, tree);
2528 static tree cp_parser_sizeof_operand
2529 (cp_parser *, enum rid);
2530 static cp_expr cp_parser_trait_expr
2531 (cp_parser *, enum rid);
2532 static bool cp_parser_declares_only_class_p
2533 (cp_parser *);
2534 static void cp_parser_set_storage_class
2535 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2536 static void cp_parser_set_decl_spec_type
2537 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2538 static void set_and_check_decl_spec_loc
2539 (cp_decl_specifier_seq *decl_specs,
2540 cp_decl_spec ds, cp_token *);
2541 static bool cp_parser_friend_p
2542 (const cp_decl_specifier_seq *);
2543 static void cp_parser_required_error
2544 (cp_parser *, required_token, bool, location_t);
2545 static cp_token *cp_parser_require
2546 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2547 static cp_token *cp_parser_require_keyword
2548 (cp_parser *, enum rid, required_token);
2549 static bool cp_parser_token_starts_function_definition_p
2550 (cp_token *);
2551 static bool cp_parser_next_token_starts_class_definition_p
2552 (cp_parser *);
2553 static bool cp_parser_next_token_ends_template_argument_p
2554 (cp_parser *);
2555 static bool cp_parser_nth_token_starts_template_argument_list_p
2556 (cp_parser *, size_t);
2557 static enum tag_types cp_parser_token_is_class_key
2558 (cp_token *);
2559 static enum tag_types cp_parser_token_is_type_parameter_key
2560 (cp_token *);
2561 static void cp_parser_check_class_key
2562 (enum tag_types, tree type);
2563 static void cp_parser_check_access_in_redeclaration
2564 (tree type, location_t location);
2565 static bool cp_parser_optional_template_keyword
2566 (cp_parser *);
2567 static void cp_parser_pre_parsed_nested_name_specifier
2568 (cp_parser *);
2569 static bool cp_parser_cache_group
2570 (cp_parser *, enum cpp_ttype, unsigned);
2571 static tree cp_parser_cache_defarg
2572 (cp_parser *parser, bool nsdmi);
2573 static void cp_parser_parse_tentatively
2574 (cp_parser *);
2575 static void cp_parser_commit_to_tentative_parse
2576 (cp_parser *);
2577 static void cp_parser_commit_to_topmost_tentative_parse
2578 (cp_parser *);
2579 static void cp_parser_abort_tentative_parse
2580 (cp_parser *);
2581 static bool cp_parser_parse_definitely
2582 (cp_parser *);
2583 static inline bool cp_parser_parsing_tentatively
2584 (cp_parser *);
2585 static bool cp_parser_uncommitted_to_tentative_parse_p
2586 (cp_parser *);
2587 static void cp_parser_error
2588 (cp_parser *, const char *);
2589 static void cp_parser_name_lookup_error
2590 (cp_parser *, tree, tree, name_lookup_error, location_t);
2591 static bool cp_parser_simulate_error
2592 (cp_parser *);
2593 static bool cp_parser_check_type_definition
2594 (cp_parser *);
2595 static void cp_parser_check_for_definition_in_return_type
2596 (cp_declarator *, tree, location_t type_location);
2597 static void cp_parser_check_for_invalid_template_id
2598 (cp_parser *, tree, enum tag_types, location_t location);
2599 static bool cp_parser_non_integral_constant_expression
2600 (cp_parser *, non_integral_constant);
2601 static void cp_parser_diagnose_invalid_type_name
2602 (cp_parser *, tree, location_t);
2603 static bool cp_parser_parse_and_diagnose_invalid_type_name
2604 (cp_parser *);
2605 static int cp_parser_skip_to_closing_parenthesis
2606 (cp_parser *, bool, bool, bool);
2607 static void cp_parser_skip_to_end_of_statement
2608 (cp_parser *);
2609 static void cp_parser_consume_semicolon_at_end_of_statement
2610 (cp_parser *);
2611 static void cp_parser_skip_to_end_of_block_or_statement
2612 (cp_parser *);
2613 static bool cp_parser_skip_to_closing_brace
2614 (cp_parser *);
2615 static void cp_parser_skip_to_end_of_template_parameter_list
2616 (cp_parser *);
2617 static void cp_parser_skip_to_pragma_eol
2618 (cp_parser*, cp_token *);
2619 static bool cp_parser_error_occurred
2620 (cp_parser *);
2621 static bool cp_parser_allow_gnu_extensions_p
2622 (cp_parser *);
2623 static bool cp_parser_is_pure_string_literal
2624 (cp_token *);
2625 static bool cp_parser_is_string_literal
2626 (cp_token *);
2627 static bool cp_parser_is_keyword
2628 (cp_token *, enum rid);
2629 static tree cp_parser_make_typename_type
2630 (cp_parser *, tree, location_t location);
2631 static cp_declarator * cp_parser_make_indirect_declarator
2632 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2633 static bool cp_parser_compound_literal_p
2634 (cp_parser *);
2635 static bool cp_parser_array_designator_p
2636 (cp_parser *);
2637 static bool cp_parser_init_statement_p
2638 (cp_parser *);
2639 static bool cp_parser_skip_to_closing_square_bracket
2640 (cp_parser *);
2642 /* Concept-related syntactic transformations */
2644 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2645 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2647 // -------------------------------------------------------------------------- //
2648 // Unevaluated Operand Guard
2650 // Implementation of an RAII helper for unevaluated operand parsing.
2651 cp_unevaluated::cp_unevaluated ()
2653 ++cp_unevaluated_operand;
2654 ++c_inhibit_evaluation_warnings;
2657 cp_unevaluated::~cp_unevaluated ()
2659 --c_inhibit_evaluation_warnings;
2660 --cp_unevaluated_operand;
2663 // -------------------------------------------------------------------------- //
2664 // Tentative Parsing
2666 /* Returns nonzero if we are parsing tentatively. */
2668 static inline bool
2669 cp_parser_parsing_tentatively (cp_parser* parser)
2671 return parser->context->next != NULL;
2674 /* Returns nonzero if TOKEN is a string literal. */
2676 static bool
2677 cp_parser_is_pure_string_literal (cp_token* token)
2679 return (token->type == CPP_STRING ||
2680 token->type == CPP_STRING16 ||
2681 token->type == CPP_STRING32 ||
2682 token->type == CPP_WSTRING ||
2683 token->type == CPP_UTF8STRING);
2686 /* Returns nonzero if TOKEN is a string literal
2687 of a user-defined string literal. */
2689 static bool
2690 cp_parser_is_string_literal (cp_token* token)
2692 return (cp_parser_is_pure_string_literal (token) ||
2693 token->type == CPP_STRING_USERDEF ||
2694 token->type == CPP_STRING16_USERDEF ||
2695 token->type == CPP_STRING32_USERDEF ||
2696 token->type == CPP_WSTRING_USERDEF ||
2697 token->type == CPP_UTF8STRING_USERDEF);
2700 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2702 static bool
2703 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2705 return token->keyword == keyword;
2708 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2709 PRAGMA_NONE. */
2711 static enum pragma_kind
2712 cp_parser_pragma_kind (cp_token *token)
2714 if (token->type != CPP_PRAGMA)
2715 return PRAGMA_NONE;
2716 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2717 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2720 /* Helper function for cp_parser_error.
2721 Having peeked a token of kind TOK1_KIND that might signify
2722 a conflict marker, peek successor tokens to determine
2723 if we actually do have a conflict marker.
2724 Specifically, we consider a run of 7 '<', '=' or '>' characters
2725 at the start of a line as a conflict marker.
2726 These come through the lexer as three pairs and a single,
2727 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2728 If it returns true, *OUT_LOC is written to with the location/range
2729 of the marker. */
2731 static bool
2732 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2733 location_t *out_loc)
2735 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2736 if (token2->type != tok1_kind)
2737 return false;
2738 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2739 if (token3->type != tok1_kind)
2740 return false;
2741 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2742 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2743 return false;
2745 /* It must be at the start of the line. */
2746 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2747 if (LOCATION_COLUMN (start_loc) != 1)
2748 return false;
2750 /* We have a conflict marker. Construct a location of the form:
2751 <<<<<<<
2752 ^~~~~~~
2753 with start == caret, finishing at the end of the marker. */
2754 location_t finish_loc = get_finish (token4->location);
2755 *out_loc = make_location (start_loc, start_loc, finish_loc);
2757 return true;
2760 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2761 RT_CLOSE_PAREN. */
2763 static const char *
2764 get_matching_symbol (required_token token_desc)
2766 switch (token_desc)
2768 default:
2769 gcc_unreachable ();
2770 return "";
2771 case RT_CLOSE_BRACE:
2772 return "{";
2773 case RT_CLOSE_PAREN:
2774 return "(";
2778 /* Attempt to convert TOKEN_DESC from a required_token to an
2779 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2781 static enum cpp_ttype
2782 get_required_cpp_ttype (required_token token_desc)
2784 switch (token_desc)
2786 case RT_SEMICOLON:
2787 return CPP_SEMICOLON;
2788 case RT_OPEN_PAREN:
2789 return CPP_OPEN_PAREN;
2790 case RT_CLOSE_BRACE:
2791 return CPP_CLOSE_BRACE;
2792 case RT_OPEN_BRACE:
2793 return CPP_OPEN_BRACE;
2794 case RT_CLOSE_SQUARE:
2795 return CPP_CLOSE_SQUARE;
2796 case RT_OPEN_SQUARE:
2797 return CPP_OPEN_SQUARE;
2798 case RT_COMMA:
2799 return CPP_COMMA;
2800 case RT_COLON:
2801 return CPP_COLON;
2802 case RT_CLOSE_PAREN:
2803 return CPP_CLOSE_PAREN;
2805 default:
2806 /* Use CPP_EOF as a "no completions possible" code. */
2807 return CPP_EOF;
2812 /* Subroutine of cp_parser_error and cp_parser_required_error.
2814 Issue a diagnostic of the form
2815 FILE:LINE: MESSAGE before TOKEN
2816 where TOKEN is the next token in the input stream. MESSAGE
2817 (specified by the caller) is usually of the form "expected
2818 OTHER-TOKEN".
2820 This bypasses the check for tentative passing, and potentially
2821 adds material needed by cp_parser_required_error.
2823 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2824 suggesting insertion of the missing token.
2826 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2827 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2828 location. */
2830 static void
2831 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2832 required_token missing_token_desc,
2833 location_t matching_location)
2835 cp_token *token = cp_lexer_peek_token (parser->lexer);
2836 /* This diagnostic makes more sense if it is tagged to the line
2837 of the token we just peeked at. */
2838 cp_lexer_set_source_position_from_token (token);
2840 if (token->type == CPP_PRAGMA)
2842 error_at (token->location,
2843 "%<#pragma%> is not allowed here");
2844 cp_parser_skip_to_pragma_eol (parser, token);
2845 return;
2848 /* If this is actually a conflict marker, report it as such. */
2849 if (token->type == CPP_LSHIFT
2850 || token->type == CPP_RSHIFT
2851 || token->type == CPP_EQ_EQ)
2853 location_t loc;
2854 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2856 error_at (loc, "version control conflict marker in file");
2857 expanded_location token_exploc = expand_location (token->location);
2858 /* Consume tokens until the end of the source line. */
2859 while (1)
2861 cp_lexer_consume_token (parser->lexer);
2862 cp_token *next = cp_lexer_peek_token (parser->lexer);
2863 if (next == NULL)
2864 break;
2865 expanded_location next_exploc = expand_location (next->location);
2866 if (next_exploc.file != token_exploc.file)
2867 break;
2868 if (next_exploc.line != token_exploc.line)
2869 break;
2871 return;
2875 gcc_rich_location richloc (input_location);
2877 bool added_matching_location = false;
2879 if (missing_token_desc != RT_NONE)
2881 /* Potentially supply a fix-it hint, suggesting to add the
2882 missing token immediately after the *previous* token.
2883 This may move the primary location within richloc. */
2884 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2885 location_t prev_token_loc
2886 = cp_lexer_previous_token (parser->lexer)->location;
2887 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2889 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2890 Attempt to consolidate diagnostics by printing it as a
2891 secondary range within the main diagnostic. */
2892 if (matching_location != UNKNOWN_LOCATION)
2893 added_matching_location
2894 = richloc.add_location_if_nearby (matching_location);
2897 /* Actually emit the error. */
2898 c_parse_error (gmsgid,
2899 /* Because c_parser_error does not understand
2900 CPP_KEYWORD, keywords are treated like
2901 identifiers. */
2902 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2903 token->u.value, token->flags, &richloc);
2905 if (missing_token_desc != RT_NONE)
2907 /* If we weren't able to consolidate matching_location, then
2908 print it as a secondary diagnostic. */
2909 if (matching_location != UNKNOWN_LOCATION
2910 && !added_matching_location)
2911 inform (matching_location, "to match this %qs",
2912 get_matching_symbol (missing_token_desc));
2916 /* If not parsing tentatively, issue a diagnostic of the form
2917 FILE:LINE: MESSAGE before TOKEN
2918 where TOKEN is the next token in the input stream. MESSAGE
2919 (specified by the caller) is usually of the form "expected
2920 OTHER-TOKEN". */
2922 static void
2923 cp_parser_error (cp_parser* parser, const char* gmsgid)
2925 if (!cp_parser_simulate_error (parser))
2926 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2929 /* Issue an error about name-lookup failing. NAME is the
2930 IDENTIFIER_NODE DECL is the result of
2931 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2932 the thing that we hoped to find. */
2934 static void
2935 cp_parser_name_lookup_error (cp_parser* parser,
2936 tree name,
2937 tree decl,
2938 name_lookup_error desired,
2939 location_t location)
2941 /* If name lookup completely failed, tell the user that NAME was not
2942 declared. */
2943 if (decl == error_mark_node)
2945 if (parser->scope && parser->scope != global_namespace)
2946 error_at (location, "%<%E::%E%> has not been declared",
2947 parser->scope, name);
2948 else if (parser->scope == global_namespace)
2949 error_at (location, "%<::%E%> has not been declared", name);
2950 else if (parser->object_scope
2951 && !CLASS_TYPE_P (parser->object_scope))
2952 error_at (location, "request for member %qE in non-class type %qT",
2953 name, parser->object_scope);
2954 else if (parser->object_scope)
2955 error_at (location, "%<%T::%E%> has not been declared",
2956 parser->object_scope, name);
2957 else
2958 error_at (location, "%qE has not been declared", name);
2960 else if (parser->scope && parser->scope != global_namespace)
2962 switch (desired)
2964 case NLE_TYPE:
2965 error_at (location, "%<%E::%E%> is not a type",
2966 parser->scope, name);
2967 break;
2968 case NLE_CXX98:
2969 error_at (location, "%<%E::%E%> is not a class or namespace",
2970 parser->scope, name);
2971 break;
2972 case NLE_NOT_CXX98:
2973 error_at (location,
2974 "%<%E::%E%> is not a class, namespace, or enumeration",
2975 parser->scope, name);
2976 break;
2977 default:
2978 gcc_unreachable ();
2982 else if (parser->scope == global_namespace)
2984 switch (desired)
2986 case NLE_TYPE:
2987 error_at (location, "%<::%E%> is not a type", name);
2988 break;
2989 case NLE_CXX98:
2990 error_at (location, "%<::%E%> is not a class or namespace", name);
2991 break;
2992 case NLE_NOT_CXX98:
2993 error_at (location,
2994 "%<::%E%> is not a class, namespace, or enumeration",
2995 name);
2996 break;
2997 default:
2998 gcc_unreachable ();
3001 else
3003 switch (desired)
3005 case NLE_TYPE:
3006 error_at (location, "%qE is not a type", name);
3007 break;
3008 case NLE_CXX98:
3009 error_at (location, "%qE is not a class or namespace", name);
3010 break;
3011 case NLE_NOT_CXX98:
3012 error_at (location,
3013 "%qE is not a class, namespace, or enumeration", name);
3014 break;
3015 default:
3016 gcc_unreachable ();
3021 /* If we are parsing tentatively, remember that an error has occurred
3022 during this tentative parse. Returns true if the error was
3023 simulated; false if a message should be issued by the caller. */
3025 static bool
3026 cp_parser_simulate_error (cp_parser* parser)
3028 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3030 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3031 return true;
3033 return false;
3036 /* This function is called when a type is defined. If type
3037 definitions are forbidden at this point, an error message is
3038 issued. */
3040 static bool
3041 cp_parser_check_type_definition (cp_parser* parser)
3043 /* If types are forbidden here, issue a message. */
3044 if (parser->type_definition_forbidden_message)
3046 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3047 in the message need to be interpreted. */
3048 error (parser->type_definition_forbidden_message);
3049 return false;
3051 return true;
3054 /* This function is called when the DECLARATOR is processed. The TYPE
3055 was a type defined in the decl-specifiers. If it is invalid to
3056 define a type in the decl-specifiers for DECLARATOR, an error is
3057 issued. TYPE_LOCATION is the location of TYPE and is used
3058 for error reporting. */
3060 static void
3061 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3062 tree type, location_t type_location)
3064 /* [dcl.fct] forbids type definitions in return types.
3065 Unfortunately, it's not easy to know whether or not we are
3066 processing a return type until after the fact. */
3067 while (declarator
3068 && (declarator->kind == cdk_pointer
3069 || declarator->kind == cdk_reference
3070 || declarator->kind == cdk_ptrmem))
3071 declarator = declarator->declarator;
3072 if (declarator
3073 && declarator->kind == cdk_function)
3075 error_at (type_location,
3076 "new types may not be defined in a return type");
3077 inform (type_location,
3078 "(perhaps a semicolon is missing after the definition of %qT)",
3079 type);
3083 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3084 "<" in any valid C++ program. If the next token is indeed "<",
3085 issue a message warning the user about what appears to be an
3086 invalid attempt to form a template-id. LOCATION is the location
3087 of the type-specifier (TYPE) */
3089 static void
3090 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3091 tree type,
3092 enum tag_types tag_type,
3093 location_t location)
3095 cp_token_position start = 0;
3097 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3099 if (TREE_CODE (type) == TYPE_DECL)
3100 type = TREE_TYPE (type);
3101 if (TYPE_P (type) && !template_placeholder_p (type))
3102 error_at (location, "%qT is not a template", type);
3103 else if (identifier_p (type))
3105 if (tag_type != none_type)
3106 error_at (location, "%qE is not a class template", type);
3107 else
3108 error_at (location, "%qE is not a template", type);
3110 else
3111 error_at (location, "invalid template-id");
3112 /* Remember the location of the invalid "<". */
3113 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3114 start = cp_lexer_token_position (parser->lexer, true);
3115 /* Consume the "<". */
3116 cp_lexer_consume_token (parser->lexer);
3117 /* Parse the template arguments. */
3118 cp_parser_enclosed_template_argument_list (parser);
3119 /* Permanently remove the invalid template arguments so that
3120 this error message is not issued again. */
3121 if (start)
3122 cp_lexer_purge_tokens_after (parser->lexer, start);
3126 /* If parsing an integral constant-expression, issue an error message
3127 about the fact that THING appeared and return true. Otherwise,
3128 return false. In either case, set
3129 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3131 static bool
3132 cp_parser_non_integral_constant_expression (cp_parser *parser,
3133 non_integral_constant thing)
3135 parser->non_integral_constant_expression_p = true;
3136 if (parser->integral_constant_expression_p)
3138 if (!parser->allow_non_integral_constant_expression_p)
3140 const char *msg = NULL;
3141 switch (thing)
3143 case NIC_FLOAT:
3144 pedwarn (input_location, OPT_Wpedantic,
3145 "ISO C++ forbids using a floating-point literal "
3146 "in a constant-expression");
3147 return true;
3148 case NIC_CAST:
3149 error ("a cast to a type other than an integral or "
3150 "enumeration type cannot appear in a "
3151 "constant-expression");
3152 return true;
3153 case NIC_TYPEID:
3154 error ("%<typeid%> operator "
3155 "cannot appear in a constant-expression");
3156 return true;
3157 case NIC_NCC:
3158 error ("non-constant compound literals "
3159 "cannot appear in a constant-expression");
3160 return true;
3161 case NIC_FUNC_CALL:
3162 error ("a function call "
3163 "cannot appear in a constant-expression");
3164 return true;
3165 case NIC_INC:
3166 error ("an increment "
3167 "cannot appear in a constant-expression");
3168 return true;
3169 case NIC_DEC:
3170 error ("an decrement "
3171 "cannot appear in a constant-expression");
3172 return true;
3173 case NIC_ARRAY_REF:
3174 error ("an array reference "
3175 "cannot appear in a constant-expression");
3176 return true;
3177 case NIC_ADDR_LABEL:
3178 error ("the address of a label "
3179 "cannot appear in a constant-expression");
3180 return true;
3181 case NIC_OVERLOADED:
3182 error ("calls to overloaded operators "
3183 "cannot appear in a constant-expression");
3184 return true;
3185 case NIC_ASSIGNMENT:
3186 error ("an assignment cannot appear in a constant-expression");
3187 return true;
3188 case NIC_COMMA:
3189 error ("a comma operator "
3190 "cannot appear in a constant-expression");
3191 return true;
3192 case NIC_CONSTRUCTOR:
3193 error ("a call to a constructor "
3194 "cannot appear in a constant-expression");
3195 return true;
3196 case NIC_TRANSACTION:
3197 error ("a transaction expression "
3198 "cannot appear in a constant-expression");
3199 return true;
3200 case NIC_THIS:
3201 msg = "this";
3202 break;
3203 case NIC_FUNC_NAME:
3204 msg = "__FUNCTION__";
3205 break;
3206 case NIC_PRETTY_FUNC:
3207 msg = "__PRETTY_FUNCTION__";
3208 break;
3209 case NIC_C99_FUNC:
3210 msg = "__func__";
3211 break;
3212 case NIC_VA_ARG:
3213 msg = "va_arg";
3214 break;
3215 case NIC_ARROW:
3216 msg = "->";
3217 break;
3218 case NIC_POINT:
3219 msg = ".";
3220 break;
3221 case NIC_STAR:
3222 msg = "*";
3223 break;
3224 case NIC_ADDR:
3225 msg = "&";
3226 break;
3227 case NIC_PREINCREMENT:
3228 msg = "++";
3229 break;
3230 case NIC_PREDECREMENT:
3231 msg = "--";
3232 break;
3233 case NIC_NEW:
3234 msg = "new";
3235 break;
3236 case NIC_DEL:
3237 msg = "delete";
3238 break;
3239 default:
3240 gcc_unreachable ();
3242 if (msg)
3243 error ("%qs cannot appear in a constant-expression", msg);
3244 return true;
3247 return false;
3250 /* Emit a diagnostic for an invalid type name. This function commits
3251 to the current active tentative parse, if any. (Otherwise, the
3252 problematic construct might be encountered again later, resulting
3253 in duplicate error messages.) LOCATION is the location of ID. */
3255 static void
3256 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3257 location_t location)
3259 tree decl, ambiguous_decls;
3260 cp_parser_commit_to_tentative_parse (parser);
3261 /* Try to lookup the identifier. */
3262 decl = cp_parser_lookup_name (parser, id, none_type,
3263 /*is_template=*/false,
3264 /*is_namespace=*/false,
3265 /*check_dependency=*/true,
3266 &ambiguous_decls, location);
3267 if (ambiguous_decls)
3268 /* If the lookup was ambiguous, an error will already have
3269 been issued. */
3270 return;
3271 /* If the lookup found a template-name, it means that the user forgot
3272 to specify an argument list. Emit a useful error message. */
3273 if (DECL_TYPE_TEMPLATE_P (decl))
3275 auto_diagnostic_group d;
3276 error_at (location,
3277 "invalid use of template-name %qE without an argument list",
3278 decl);
3279 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3280 inform (location, "class template argument deduction is only available "
3281 "with -std=c++17 or -std=gnu++17");
3282 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3284 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3285 error_at (location, "invalid use of destructor %qD as a type", id);
3286 else if (TREE_CODE (decl) == TYPE_DECL)
3287 /* Something like 'unsigned A a;' */
3288 error_at (location, "invalid combination of multiple type-specifiers");
3289 else if (!parser->scope)
3291 /* Issue an error message. */
3292 auto_diagnostic_group d;
3293 name_hint hint;
3294 if (TREE_CODE (id) == IDENTIFIER_NODE)
3295 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3296 if (const char *suggestion = hint.suggestion ())
3298 gcc_rich_location richloc (location);
3299 richloc.add_fixit_replace (suggestion);
3300 error_at (&richloc,
3301 "%qE does not name a type; did you mean %qs?",
3302 id, suggestion);
3304 else
3305 error_at (location, "%qE does not name a type", id);
3306 /* If we're in a template class, it's possible that the user was
3307 referring to a type from a base class. For example:
3309 template <typename T> struct A { typedef T X; };
3310 template <typename T> struct B : public A<T> { X x; };
3312 The user should have said "typename A<T>::X". */
3313 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3314 inform (location, "C++11 %<constexpr%> only available with "
3315 "-std=c++11 or -std=gnu++11");
3316 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3317 inform (location, "C++11 %<noexcept%> only available with "
3318 "-std=c++11 or -std=gnu++11");
3319 else if (cxx_dialect < cxx11
3320 && TREE_CODE (id) == IDENTIFIER_NODE
3321 && id_equal (id, "thread_local"))
3322 inform (location, "C++11 %<thread_local%> only available with "
3323 "-std=c++11 or -std=gnu++11");
3324 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3325 inform (location, "%<concept%> only available with -fconcepts");
3326 else if (processing_template_decl && current_class_type
3327 && TYPE_BINFO (current_class_type))
3329 tree b;
3331 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3333 b = TREE_CHAIN (b))
3335 tree base_type = BINFO_TYPE (b);
3336 if (CLASS_TYPE_P (base_type)
3337 && dependent_type_p (base_type))
3339 tree field;
3340 /* Go from a particular instantiation of the
3341 template (which will have an empty TYPE_FIELDs),
3342 to the main version. */
3343 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3344 for (field = TYPE_FIELDS (base_type);
3345 field;
3346 field = DECL_CHAIN (field))
3347 if (TREE_CODE (field) == TYPE_DECL
3348 && DECL_NAME (field) == id)
3350 inform (location,
3351 "(perhaps %<typename %T::%E%> was intended)",
3352 BINFO_TYPE (b), id);
3353 break;
3355 if (field)
3356 break;
3361 /* Here we diagnose qualified-ids where the scope is actually correct,
3362 but the identifier does not resolve to a valid type name. */
3363 else if (parser->scope != error_mark_node)
3365 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3367 auto_diagnostic_group d;
3368 name_hint hint;
3369 if (decl == error_mark_node)
3370 hint = suggest_alternative_in_explicit_scope (location, id,
3371 parser->scope);
3372 const char *suggestion = hint.suggestion ();
3373 gcc_rich_location richloc (location_of (id));
3374 if (suggestion)
3375 richloc.add_fixit_replace (suggestion);
3376 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3378 if (suggestion)
3379 error_at (&richloc,
3380 "%qE in namespace %qE does not name a template"
3381 " type; did you mean %qs?",
3382 id, parser->scope, suggestion);
3383 else
3384 error_at (&richloc,
3385 "%qE in namespace %qE does not name a template type",
3386 id, parser->scope);
3388 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3390 if (suggestion)
3391 error_at (&richloc,
3392 "%qE in namespace %qE does not name a template"
3393 " type; did you mean %qs?",
3394 TREE_OPERAND (id, 0), parser->scope, suggestion);
3395 else
3396 error_at (&richloc,
3397 "%qE in namespace %qE does not name a template"
3398 " type",
3399 TREE_OPERAND (id, 0), parser->scope);
3401 else
3403 if (suggestion)
3404 error_at (&richloc,
3405 "%qE in namespace %qE does not name a type"
3406 "; did you mean %qs?",
3407 id, parser->scope, suggestion);
3408 else
3409 error_at (&richloc,
3410 "%qE in namespace %qE does not name a type",
3411 id, parser->scope);
3413 if (DECL_P (decl))
3414 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3416 else if (CLASS_TYPE_P (parser->scope)
3417 && constructor_name_p (id, parser->scope))
3419 /* A<T>::A<T>() */
3420 auto_diagnostic_group d;
3421 error_at (location, "%<%T::%E%> names the constructor, not"
3422 " the type", parser->scope, id);
3423 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3424 error_at (location, "and %qT has no template constructors",
3425 parser->scope);
3427 else if (TYPE_P (parser->scope)
3428 && dependent_scope_p (parser->scope))
3430 gcc_rich_location richloc (location);
3431 richloc.add_fixit_insert_before ("typename ");
3432 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3433 error_at (&richloc,
3434 "need %<typename%> before %<%T::%D::%E%> because "
3435 "%<%T::%D%> is a dependent scope",
3436 TYPE_CONTEXT (parser->scope),
3437 TYPENAME_TYPE_FULLNAME (parser->scope),
3439 TYPE_CONTEXT (parser->scope),
3440 TYPENAME_TYPE_FULLNAME (parser->scope));
3441 else
3442 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3443 "%qT is a dependent scope",
3444 parser->scope, id, parser->scope);
3446 else if (TYPE_P (parser->scope))
3448 auto_diagnostic_group d;
3449 if (!COMPLETE_TYPE_P (parser->scope))
3450 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3451 parser->scope);
3452 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3453 error_at (location_of (id),
3454 "%qE in %q#T does not name a template type",
3455 id, parser->scope);
3456 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3457 error_at (location_of (id),
3458 "%qE in %q#T does not name a template type",
3459 TREE_OPERAND (id, 0), parser->scope);
3460 else
3461 error_at (location_of (id),
3462 "%qE in %q#T does not name a type",
3463 id, parser->scope);
3464 if (DECL_P (decl))
3465 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3467 else
3468 gcc_unreachable ();
3472 /* Check for a common situation where a type-name should be present,
3473 but is not, and issue a sensible error message. Returns true if an
3474 invalid type-name was detected.
3476 The situation handled by this function are variable declarations of the
3477 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3478 Usually, `ID' should name a type, but if we got here it means that it
3479 does not. We try to emit the best possible error message depending on
3480 how exactly the id-expression looks like. */
3482 static bool
3483 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3485 tree id;
3486 cp_token *token = cp_lexer_peek_token (parser->lexer);
3488 /* Avoid duplicate error about ambiguous lookup. */
3489 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3491 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3492 if (next->type == CPP_NAME && next->error_reported)
3493 goto out;
3496 cp_parser_parse_tentatively (parser);
3497 id = cp_parser_id_expression (parser,
3498 /*template_keyword_p=*/false,
3499 /*check_dependency_p=*/true,
3500 /*template_p=*/NULL,
3501 /*declarator_p=*/false,
3502 /*optional_p=*/false);
3503 /* If the next token is a (, this is a function with no explicit return
3504 type, i.e. constructor, destructor or conversion op. */
3505 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3506 || TREE_CODE (id) == TYPE_DECL)
3508 cp_parser_abort_tentative_parse (parser);
3509 return false;
3511 if (!cp_parser_parse_definitely (parser))
3512 return false;
3514 /* Emit a diagnostic for the invalid type. */
3515 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3516 out:
3517 /* If we aren't in the middle of a declarator (i.e. in a
3518 parameter-declaration-clause), skip to the end of the declaration;
3519 there's no point in trying to process it. */
3520 if (!parser->in_declarator_p)
3521 cp_parser_skip_to_end_of_block_or_statement (parser);
3522 return true;
3525 /* Consume tokens up to, and including, the next non-nested closing `)'.
3526 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3527 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3528 found an unnested token of that type. */
3530 static int
3531 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3532 bool recovering,
3533 cpp_ttype or_ttype,
3534 bool consume_paren)
3536 unsigned paren_depth = 0;
3537 unsigned brace_depth = 0;
3538 unsigned square_depth = 0;
3539 unsigned condop_depth = 0;
3541 if (recovering && or_ttype == CPP_EOF
3542 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3543 return 0;
3545 while (true)
3547 cp_token * token = cp_lexer_peek_token (parser->lexer);
3549 /* Have we found what we're looking for before the closing paren? */
3550 if (token->type == or_ttype && or_ttype != CPP_EOF
3551 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3552 return -1;
3554 switch (token->type)
3556 case CPP_EOF:
3557 case CPP_PRAGMA_EOL:
3558 /* If we've run out of tokens, then there is no closing `)'. */
3559 return 0;
3561 /* This is good for lambda expression capture-lists. */
3562 case CPP_OPEN_SQUARE:
3563 ++square_depth;
3564 break;
3565 case CPP_CLOSE_SQUARE:
3566 if (!square_depth--)
3567 return 0;
3568 break;
3570 case CPP_SEMICOLON:
3571 /* This matches the processing in skip_to_end_of_statement. */
3572 if (!brace_depth)
3573 return 0;
3574 break;
3576 case CPP_OPEN_BRACE:
3577 ++brace_depth;
3578 break;
3579 case CPP_CLOSE_BRACE:
3580 if (!brace_depth--)
3581 return 0;
3582 break;
3584 case CPP_OPEN_PAREN:
3585 if (!brace_depth)
3586 ++paren_depth;
3587 break;
3589 case CPP_CLOSE_PAREN:
3590 if (!brace_depth && !paren_depth--)
3592 if (consume_paren)
3593 cp_lexer_consume_token (parser->lexer);
3594 return 1;
3596 break;
3598 case CPP_QUERY:
3599 if (!brace_depth && !paren_depth && !square_depth)
3600 ++condop_depth;
3601 break;
3603 case CPP_COLON:
3604 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3605 condop_depth--;
3606 break;
3608 default:
3609 break;
3612 /* Consume the token. */
3613 cp_lexer_consume_token (parser->lexer);
3617 /* Consume tokens up to, and including, the next non-nested closing `)'.
3618 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3619 are doing error recovery. Returns -1 if OR_COMMA is true and we
3620 found an unnested token of that type. */
3622 static int
3623 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3624 bool recovering,
3625 bool or_comma,
3626 bool consume_paren)
3628 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3629 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3630 ttype, consume_paren);
3633 /* Consume tokens until we reach the end of the current statement.
3634 Normally, that will be just before consuming a `;'. However, if a
3635 non-nested `}' comes first, then we stop before consuming that. */
3637 static void
3638 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3640 unsigned nesting_depth = 0;
3642 /* Unwind generic function template scope if necessary. */
3643 if (parser->fully_implicit_function_template_p)
3644 abort_fully_implicit_template (parser);
3646 while (true)
3648 cp_token *token = cp_lexer_peek_token (parser->lexer);
3650 switch (token->type)
3652 case CPP_EOF:
3653 case CPP_PRAGMA_EOL:
3654 /* If we've run out of tokens, stop. */
3655 return;
3657 case CPP_SEMICOLON:
3658 /* If the next token is a `;', we have reached the end of the
3659 statement. */
3660 if (!nesting_depth)
3661 return;
3662 break;
3664 case CPP_CLOSE_BRACE:
3665 /* If this is a non-nested '}', stop before consuming it.
3666 That way, when confronted with something like:
3668 { 3 + }
3670 we stop before consuming the closing '}', even though we
3671 have not yet reached a `;'. */
3672 if (nesting_depth == 0)
3673 return;
3675 /* If it is the closing '}' for a block that we have
3676 scanned, stop -- but only after consuming the token.
3677 That way given:
3679 void f g () { ... }
3680 typedef int I;
3682 we will stop after the body of the erroneously declared
3683 function, but before consuming the following `typedef'
3684 declaration. */
3685 if (--nesting_depth == 0)
3687 cp_lexer_consume_token (parser->lexer);
3688 return;
3690 break;
3692 case CPP_OPEN_BRACE:
3693 ++nesting_depth;
3694 break;
3696 default:
3697 break;
3700 /* Consume the token. */
3701 cp_lexer_consume_token (parser->lexer);
3705 /* This function is called at the end of a statement or declaration.
3706 If the next token is a semicolon, it is consumed; otherwise, error
3707 recovery is attempted. */
3709 static void
3710 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3712 /* Look for the trailing `;'. */
3713 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3715 /* If there is additional (erroneous) input, skip to the end of
3716 the statement. */
3717 cp_parser_skip_to_end_of_statement (parser);
3718 /* If the next token is now a `;', consume it. */
3719 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3720 cp_lexer_consume_token (parser->lexer);
3724 /* Skip tokens until we have consumed an entire block, or until we
3725 have consumed a non-nested `;'. */
3727 static void
3728 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3730 int nesting_depth = 0;
3732 /* Unwind generic function template scope if necessary. */
3733 if (parser->fully_implicit_function_template_p)
3734 abort_fully_implicit_template (parser);
3736 while (nesting_depth >= 0)
3738 cp_token *token = cp_lexer_peek_token (parser->lexer);
3740 switch (token->type)
3742 case CPP_EOF:
3743 case CPP_PRAGMA_EOL:
3744 /* If we've run out of tokens, stop. */
3745 return;
3747 case CPP_SEMICOLON:
3748 /* Stop if this is an unnested ';'. */
3749 if (!nesting_depth)
3750 nesting_depth = -1;
3751 break;
3753 case CPP_CLOSE_BRACE:
3754 /* Stop if this is an unnested '}', or closes the outermost
3755 nesting level. */
3756 nesting_depth--;
3757 if (nesting_depth < 0)
3758 return;
3759 if (!nesting_depth)
3760 nesting_depth = -1;
3761 break;
3763 case CPP_OPEN_BRACE:
3764 /* Nest. */
3765 nesting_depth++;
3766 break;
3768 default:
3769 break;
3772 /* Consume the token. */
3773 cp_lexer_consume_token (parser->lexer);
3777 /* Skip tokens until a non-nested closing curly brace is the next
3778 token, or there are no more tokens. Return true in the first case,
3779 false otherwise. */
3781 static bool
3782 cp_parser_skip_to_closing_brace (cp_parser *parser)
3784 unsigned nesting_depth = 0;
3786 while (true)
3788 cp_token *token = cp_lexer_peek_token (parser->lexer);
3790 switch (token->type)
3792 case CPP_EOF:
3793 case CPP_PRAGMA_EOL:
3794 /* If we've run out of tokens, stop. */
3795 return false;
3797 case CPP_CLOSE_BRACE:
3798 /* If the next token is a non-nested `}', then we have reached
3799 the end of the current block. */
3800 if (nesting_depth-- == 0)
3801 return true;
3802 break;
3804 case CPP_OPEN_BRACE:
3805 /* If it the next token is a `{', then we are entering a new
3806 block. Consume the entire block. */
3807 ++nesting_depth;
3808 break;
3810 default:
3811 break;
3814 /* Consume the token. */
3815 cp_lexer_consume_token (parser->lexer);
3819 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3820 parameter is the PRAGMA token, allowing us to purge the entire pragma
3821 sequence. */
3823 static void
3824 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3826 cp_token *token;
3828 parser->lexer->in_pragma = false;
3831 token = cp_lexer_consume_token (parser->lexer);
3832 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3834 /* Ensure that the pragma is not parsed again. */
3835 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3838 /* Require pragma end of line, resyncing with it as necessary. The
3839 arguments are as for cp_parser_skip_to_pragma_eol. */
3841 static void
3842 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3844 parser->lexer->in_pragma = false;
3845 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3846 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3849 /* This is a simple wrapper around make_typename_type. When the id is
3850 an unresolved identifier node, we can provide a superior diagnostic
3851 using cp_parser_diagnose_invalid_type_name. */
3853 static tree
3854 cp_parser_make_typename_type (cp_parser *parser, tree id,
3855 location_t id_location)
3857 tree result;
3858 if (identifier_p (id))
3860 result = make_typename_type (parser->scope, id, typename_type,
3861 /*complain=*/tf_none);
3862 if (result == error_mark_node)
3863 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3864 return result;
3866 return make_typename_type (parser->scope, id, typename_type, tf_error);
3869 /* This is a wrapper around the
3870 make_{pointer,ptrmem,reference}_declarator functions that decides
3871 which one to call based on the CODE and CLASS_TYPE arguments. The
3872 CODE argument should be one of the values returned by
3873 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3874 appertain to the pointer or reference. */
3876 static cp_declarator *
3877 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3878 cp_cv_quals cv_qualifiers,
3879 cp_declarator *target,
3880 tree attributes)
3882 if (code == ERROR_MARK || target == cp_error_declarator)
3883 return cp_error_declarator;
3885 if (code == INDIRECT_REF)
3886 if (class_type == NULL_TREE)
3887 return make_pointer_declarator (cv_qualifiers, target, attributes);
3888 else
3889 return make_ptrmem_declarator (cv_qualifiers, class_type,
3890 target, attributes);
3891 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3892 return make_reference_declarator (cv_qualifiers, target,
3893 false, attributes);
3894 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3895 return make_reference_declarator (cv_qualifiers, target,
3896 true, attributes);
3897 gcc_unreachable ();
3900 /* Create a new C++ parser. */
3902 static cp_parser *
3903 cp_parser_new (void)
3905 cp_parser *parser;
3906 cp_lexer *lexer;
3907 unsigned i;
3909 /* cp_lexer_new_main is called before doing GC allocation because
3910 cp_lexer_new_main might load a PCH file. */
3911 lexer = cp_lexer_new_main ();
3913 /* Initialize the binops_by_token so that we can get the tree
3914 directly from the token. */
3915 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3916 binops_by_token[binops[i].token_type] = binops[i];
3918 parser = ggc_cleared_alloc<cp_parser> ();
3919 parser->lexer = lexer;
3920 parser->context = cp_parser_context_new (NULL);
3922 /* For now, we always accept GNU extensions. */
3923 parser->allow_gnu_extensions_p = 1;
3925 /* The `>' token is a greater-than operator, not the end of a
3926 template-id. */
3927 parser->greater_than_is_operator_p = true;
3929 parser->default_arg_ok_p = true;
3931 /* We are not parsing a constant-expression. */
3932 parser->integral_constant_expression_p = false;
3933 parser->allow_non_integral_constant_expression_p = false;
3934 parser->non_integral_constant_expression_p = false;
3936 /* Local variable names are not forbidden. */
3937 parser->local_variables_forbidden_p = false;
3939 /* We are not processing an `extern "C"' declaration. */
3940 parser->in_unbraced_linkage_specification_p = false;
3942 /* We are not processing a declarator. */
3943 parser->in_declarator_p = false;
3945 /* We are not processing a template-argument-list. */
3946 parser->in_template_argument_list_p = false;
3948 /* We are not in an iteration statement. */
3949 parser->in_statement = 0;
3951 /* We are not in a switch statement. */
3952 parser->in_switch_statement_p = false;
3954 /* We are not parsing a type-id inside an expression. */
3955 parser->in_type_id_in_expr_p = false;
3957 /* String literals should be translated to the execution character set. */
3958 parser->translate_strings_p = true;
3960 /* We are not parsing a function body. */
3961 parser->in_function_body = false;
3963 /* We can correct until told otherwise. */
3964 parser->colon_corrects_to_scope_p = true;
3966 /* The unparsed function queue is empty. */
3967 push_unparsed_function_queues (parser);
3969 /* There are no classes being defined. */
3970 parser->num_classes_being_defined = 0;
3972 /* No template parameters apply. */
3973 parser->num_template_parameter_lists = 0;
3975 /* Special parsing data structures. */
3976 parser->omp_declare_simd = NULL;
3977 parser->oacc_routine = NULL;
3979 /* Not declaring an implicit function template. */
3980 parser->auto_is_implicit_function_template_parm_p = false;
3981 parser->fully_implicit_function_template_p = false;
3982 parser->implicit_template_parms = 0;
3983 parser->implicit_template_scope = 0;
3985 /* Allow constrained-type-specifiers. */
3986 parser->prevent_constrained_type_specifiers = 0;
3988 /* We haven't yet seen an 'extern "C"'. */
3989 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3991 return parser;
3994 /* Create a cp_lexer structure which will emit the tokens in CACHE
3995 and push it onto the parser's lexer stack. This is used for delayed
3996 parsing of in-class method bodies and default arguments, and should
3997 not be confused with tentative parsing. */
3998 static void
3999 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4001 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4002 lexer->next = parser->lexer;
4003 parser->lexer = lexer;
4005 /* Move the current source position to that of the first token in the
4006 new lexer. */
4007 cp_lexer_set_source_position_from_token (lexer->next_token);
4010 /* Pop the top lexer off the parser stack. This is never used for the
4011 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4012 static void
4013 cp_parser_pop_lexer (cp_parser *parser)
4015 cp_lexer *lexer = parser->lexer;
4016 parser->lexer = lexer->next;
4017 cp_lexer_destroy (lexer);
4019 /* Put the current source position back where it was before this
4020 lexer was pushed. */
4021 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4024 /* Lexical conventions [gram.lex] */
4026 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4027 identifier. */
4029 static cp_expr
4030 cp_parser_identifier (cp_parser* parser)
4032 cp_token *token;
4034 /* Look for the identifier. */
4035 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4036 /* Return the value. */
4037 if (token)
4038 return cp_expr (token->u.value, token->location);
4039 else
4040 return error_mark_node;
4043 /* Parse a sequence of adjacent string constants. Returns a
4044 TREE_STRING representing the combined, nul-terminated string
4045 constant. If TRANSLATE is true, translate the string to the
4046 execution character set. If WIDE_OK is true, a wide string is
4047 invalid here.
4049 C++98 [lex.string] says that if a narrow string literal token is
4050 adjacent to a wide string literal token, the behavior is undefined.
4051 However, C99 6.4.5p4 says that this results in a wide string literal.
4052 We follow C99 here, for consistency with the C front end.
4054 This code is largely lifted from lex_string() in c-lex.c.
4056 FUTURE: ObjC++ will need to handle @-strings here. */
4057 static cp_expr
4058 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4059 bool lookup_udlit = true)
4061 tree value;
4062 size_t count;
4063 struct obstack str_ob;
4064 struct obstack loc_ob;
4065 cpp_string str, istr, *strs;
4066 cp_token *tok;
4067 enum cpp_ttype type, curr_type;
4068 int have_suffix_p = 0;
4069 tree string_tree;
4070 tree suffix_id = NULL_TREE;
4071 bool curr_tok_is_userdef_p = false;
4073 tok = cp_lexer_peek_token (parser->lexer);
4074 if (!cp_parser_is_string_literal (tok))
4076 cp_parser_error (parser, "expected string-literal");
4077 return error_mark_node;
4080 location_t loc = tok->location;
4082 if (cpp_userdef_string_p (tok->type))
4084 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4085 curr_type = cpp_userdef_string_remove_type (tok->type);
4086 curr_tok_is_userdef_p = true;
4088 else
4090 string_tree = tok->u.value;
4091 curr_type = tok->type;
4093 type = curr_type;
4095 /* Try to avoid the overhead of creating and destroying an obstack
4096 for the common case of just one string. */
4097 if (!cp_parser_is_string_literal
4098 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4100 cp_lexer_consume_token (parser->lexer);
4102 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4103 str.len = TREE_STRING_LENGTH (string_tree);
4104 count = 1;
4106 if (curr_tok_is_userdef_p)
4108 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4109 have_suffix_p = 1;
4110 curr_type = cpp_userdef_string_remove_type (tok->type);
4112 else
4113 curr_type = tok->type;
4115 strs = &str;
4117 else
4119 location_t last_tok_loc = tok->location;
4120 gcc_obstack_init (&str_ob);
4121 gcc_obstack_init (&loc_ob);
4122 count = 0;
4126 cp_lexer_consume_token (parser->lexer);
4127 count++;
4128 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4129 str.len = TREE_STRING_LENGTH (string_tree);
4131 if (curr_tok_is_userdef_p)
4133 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4134 if (have_suffix_p == 0)
4136 suffix_id = curr_suffix_id;
4137 have_suffix_p = 1;
4139 else if (have_suffix_p == 1
4140 && curr_suffix_id != suffix_id)
4142 error ("inconsistent user-defined literal suffixes"
4143 " %qD and %qD in string literal",
4144 suffix_id, curr_suffix_id);
4145 have_suffix_p = -1;
4147 curr_type = cpp_userdef_string_remove_type (tok->type);
4149 else
4150 curr_type = tok->type;
4152 if (type != curr_type)
4154 if (type == CPP_STRING)
4155 type = curr_type;
4156 else if (curr_type != CPP_STRING)
4158 rich_location rich_loc (line_table, tok->location);
4159 rich_loc.add_range (last_tok_loc);
4160 error_at (&rich_loc,
4161 "unsupported non-standard concatenation "
4162 "of string literals");
4166 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4167 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4169 last_tok_loc = tok->location;
4171 tok = cp_lexer_peek_token (parser->lexer);
4172 if (cpp_userdef_string_p (tok->type))
4174 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4175 curr_type = cpp_userdef_string_remove_type (tok->type);
4176 curr_tok_is_userdef_p = true;
4178 else
4180 string_tree = tok->u.value;
4181 curr_type = tok->type;
4182 curr_tok_is_userdef_p = false;
4185 while (cp_parser_is_string_literal (tok));
4187 /* A string literal built by concatenation has its caret=start at
4188 the start of the initial string, and its finish at the finish of
4189 the final string literal. */
4190 loc = make_location (loc, loc, get_finish (last_tok_loc));
4192 strs = (cpp_string *) obstack_finish (&str_ob);
4195 if (type != CPP_STRING && !wide_ok)
4197 cp_parser_error (parser, "a wide string is invalid in this context");
4198 type = CPP_STRING;
4201 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4202 (parse_in, strs, count, &istr, type))
4204 value = build_string (istr.len, (const char *)istr.text);
4205 free (CONST_CAST (unsigned char *, istr.text));
4206 if (count > 1)
4208 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4209 gcc_assert (g_string_concat_db);
4210 g_string_concat_db->record_string_concatenation (count, locs);
4213 switch (type)
4215 default:
4216 case CPP_STRING:
4217 case CPP_UTF8STRING:
4218 TREE_TYPE (value) = char_array_type_node;
4219 break;
4220 case CPP_STRING16:
4221 TREE_TYPE (value) = char16_array_type_node;
4222 break;
4223 case CPP_STRING32:
4224 TREE_TYPE (value) = char32_array_type_node;
4225 break;
4226 case CPP_WSTRING:
4227 TREE_TYPE (value) = wchar_array_type_node;
4228 break;
4231 value = fix_string_type (value);
4233 if (have_suffix_p)
4235 tree literal = build_userdef_literal (suffix_id, value,
4236 OT_NONE, NULL_TREE);
4237 if (lookup_udlit)
4238 value = cp_parser_userdef_string_literal (literal);
4239 else
4240 value = literal;
4243 else
4244 /* cpp_interpret_string has issued an error. */
4245 value = error_mark_node;
4247 if (count > 1)
4249 obstack_free (&str_ob, 0);
4250 obstack_free (&loc_ob, 0);
4253 return cp_expr (value, loc);
4256 /* Look up a literal operator with the name and the exact arguments. */
4258 static tree
4259 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4261 tree decl;
4262 decl = lookup_name (name);
4263 if (!decl || !is_overloaded_fn (decl))
4264 return error_mark_node;
4266 for (lkp_iterator iter (decl); iter; ++iter)
4268 unsigned int ix;
4269 bool found = true;
4270 tree fn = *iter;
4271 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4272 if (parmtypes != NULL_TREE)
4274 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4275 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4277 tree tparm = TREE_VALUE (parmtypes);
4278 tree targ = TREE_TYPE ((*args)[ix]);
4279 bool ptr = TYPE_PTR_P (tparm);
4280 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4281 if ((ptr || arr || !same_type_p (tparm, targ))
4282 && (!ptr || !arr
4283 || !same_type_p (TREE_TYPE (tparm),
4284 TREE_TYPE (targ))))
4285 found = false;
4287 if (found
4288 && ix == vec_safe_length (args)
4289 /* May be this should be sufficient_parms_p instead,
4290 depending on how exactly should user-defined literals
4291 work in presence of default arguments on the literal
4292 operator parameters. */
4293 && parmtypes == void_list_node)
4294 return decl;
4298 return error_mark_node;
4301 /* Parse a user-defined char constant. Returns a call to a user-defined
4302 literal operator taking the character as an argument. */
4304 static cp_expr
4305 cp_parser_userdef_char_literal (cp_parser *parser)
4307 cp_token *token = cp_lexer_consume_token (parser->lexer);
4308 tree literal = token->u.value;
4309 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4310 tree value = USERDEF_LITERAL_VALUE (literal);
4311 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4312 tree decl, result;
4314 /* Build up a call to the user-defined operator */
4315 /* Lookup the name we got back from the id-expression. */
4316 vec<tree, va_gc> *args = make_tree_vector ();
4317 vec_safe_push (args, value);
4318 decl = lookup_literal_operator (name, args);
4319 if (!decl || decl == error_mark_node)
4321 error ("unable to find character literal operator %qD with %qT argument",
4322 name, TREE_TYPE (value));
4323 release_tree_vector (args);
4324 return error_mark_node;
4326 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4327 release_tree_vector (args);
4328 return result;
4331 /* A subroutine of cp_parser_userdef_numeric_literal to
4332 create a char... template parameter pack from a string node. */
4334 static tree
4335 make_char_string_pack (tree value)
4337 tree charvec;
4338 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4339 const char *str = TREE_STRING_POINTER (value);
4340 int i, len = TREE_STRING_LENGTH (value) - 1;
4341 tree argvec = make_tree_vec (1);
4343 /* Fill in CHARVEC with all of the parameters. */
4344 charvec = make_tree_vec (len);
4345 for (i = 0; i < len; ++i)
4347 unsigned char s[3] = { '\'', str[i], '\'' };
4348 cpp_string in = { 3, s };
4349 cpp_string out = { 0, 0 };
4350 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4351 return NULL_TREE;
4352 gcc_assert (out.len == 2);
4353 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4354 out.text[0]);
4357 /* Build the argument packs. */
4358 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4360 TREE_VEC_ELT (argvec, 0) = argpack;
4362 return argvec;
4365 /* A subroutine of cp_parser_userdef_numeric_literal to
4366 create a char... template parameter pack from a string node. */
4368 static tree
4369 make_string_pack (tree value)
4371 tree charvec;
4372 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4373 const unsigned char *str
4374 = (const unsigned char *) TREE_STRING_POINTER (value);
4375 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4376 int len = TREE_STRING_LENGTH (value) / sz - 1;
4377 tree argvec = make_tree_vec (2);
4379 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4380 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4382 /* First template parm is character type. */
4383 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4385 /* Fill in CHARVEC with all of the parameters. */
4386 charvec = make_tree_vec (len);
4387 for (int i = 0; i < len; ++i)
4388 TREE_VEC_ELT (charvec, i)
4389 = double_int_to_tree (str_char_type_node,
4390 double_int::from_buffer (str + i * sz, sz));
4392 /* Build the argument packs. */
4393 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4395 TREE_VEC_ELT (argvec, 1) = argpack;
4397 return argvec;
4400 /* Parse a user-defined numeric constant. returns a call to a user-defined
4401 literal operator. */
4403 static cp_expr
4404 cp_parser_userdef_numeric_literal (cp_parser *parser)
4406 cp_token *token = cp_lexer_consume_token (parser->lexer);
4407 tree literal = token->u.value;
4408 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4409 tree value = USERDEF_LITERAL_VALUE (literal);
4410 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4411 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4412 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4413 tree decl, result;
4414 vec<tree, va_gc> *args;
4416 /* Look for a literal operator taking the exact type of numeric argument
4417 as the literal value. */
4418 args = make_tree_vector ();
4419 vec_safe_push (args, value);
4420 decl = lookup_literal_operator (name, args);
4421 if (decl && decl != error_mark_node)
4423 result = finish_call_expr (decl, &args, false, true,
4424 tf_warning_or_error);
4426 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4428 warning_at (token->location, OPT_Woverflow,
4429 "integer literal exceeds range of %qT type",
4430 long_long_unsigned_type_node);
4432 else
4434 if (overflow > 0)
4435 warning_at (token->location, OPT_Woverflow,
4436 "floating literal exceeds range of %qT type",
4437 long_double_type_node);
4438 else if (overflow < 0)
4439 warning_at (token->location, OPT_Woverflow,
4440 "floating literal truncated to zero");
4443 release_tree_vector (args);
4444 return result;
4446 release_tree_vector (args);
4448 /* If the numeric argument didn't work, look for a raw literal
4449 operator taking a const char* argument consisting of the number
4450 in string format. */
4451 args = make_tree_vector ();
4452 vec_safe_push (args, num_string);
4453 decl = lookup_literal_operator (name, args);
4454 if (decl && decl != error_mark_node)
4456 result = finish_call_expr (decl, &args, false, true,
4457 tf_warning_or_error);
4458 release_tree_vector (args);
4459 return result;
4461 release_tree_vector (args);
4463 /* If the raw literal didn't work, look for a non-type template
4464 function with parameter pack char.... Call the function with
4465 template parameter characters representing the number. */
4466 args = make_tree_vector ();
4467 decl = lookup_literal_operator (name, args);
4468 if (decl && decl != error_mark_node)
4470 tree tmpl_args = make_char_string_pack (num_string);
4471 if (tmpl_args == NULL_TREE)
4473 error ("failed to translate literal to execution character set %qT",
4474 num_string);
4475 return error_mark_node;
4477 decl = lookup_template_function (decl, tmpl_args);
4478 result = finish_call_expr (decl, &args, false, true,
4479 tf_warning_or_error);
4480 release_tree_vector (args);
4481 return result;
4484 release_tree_vector (args);
4486 /* In C++14 the standard library defines complex number suffixes that
4487 conflict with GNU extensions. Prefer them if <complex> is #included. */
4488 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4489 bool i14 = (cxx_dialect > cxx11
4490 && (id_equal (suffix_id, "i")
4491 || id_equal (suffix_id, "if")
4492 || id_equal (suffix_id, "il")));
4493 diagnostic_t kind = DK_ERROR;
4494 int opt = 0;
4496 if (i14 && ext)
4498 tree cxlit = lookup_qualified_name (std_node,
4499 get_identifier ("complex_literals"),
4500 0, false, false);
4501 if (cxlit == error_mark_node)
4503 /* No <complex>, so pedwarn and use GNU semantics. */
4504 kind = DK_PEDWARN;
4505 opt = OPT_Wpedantic;
4509 bool complained
4510 = emit_diagnostic (kind, input_location, opt,
4511 "unable to find numeric literal operator %qD", name);
4513 if (!complained)
4514 /* Don't inform either. */;
4515 else if (i14)
4517 inform (token->location, "add %<using namespace std::complex_literals%> "
4518 "(from <complex>) to enable the C++14 user-defined literal "
4519 "suffixes");
4520 if (ext)
4521 inform (token->location, "or use %<j%> instead of %<i%> for the "
4522 "GNU built-in suffix");
4524 else if (!ext)
4525 inform (token->location, "use -fext-numeric-literals "
4526 "to enable more built-in suffixes");
4528 if (kind == DK_ERROR)
4529 value = error_mark_node;
4530 else
4532 /* Use the built-in semantics. */
4533 tree type;
4534 if (id_equal (suffix_id, "i"))
4536 if (TREE_CODE (value) == INTEGER_CST)
4537 type = integer_type_node;
4538 else
4539 type = double_type_node;
4541 else if (id_equal (suffix_id, "if"))
4542 type = float_type_node;
4543 else /* if (id_equal (suffix_id, "il")) */
4544 type = long_double_type_node;
4546 value = build_complex (build_complex_type (type),
4547 fold_convert (type, integer_zero_node),
4548 fold_convert (type, value));
4551 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4552 /* Avoid repeated diagnostics. */
4553 token->u.value = value;
4554 return value;
4557 /* Parse a user-defined string constant. Returns a call to a user-defined
4558 literal operator taking a character pointer and the length of the string
4559 as arguments. */
4561 static tree
4562 cp_parser_userdef_string_literal (tree literal)
4564 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4565 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4566 tree value = USERDEF_LITERAL_VALUE (literal);
4567 int len = TREE_STRING_LENGTH (value)
4568 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4569 tree decl;
4571 /* Build up a call to the user-defined operator. */
4572 /* Lookup the name we got back from the id-expression. */
4573 releasing_vec rargs;
4574 vec<tree, va_gc> *&args = rargs.get_ref();
4575 vec_safe_push (args, value);
4576 vec_safe_push (args, build_int_cst (size_type_node, len));
4577 decl = lookup_literal_operator (name, args);
4579 if (decl && decl != error_mark_node)
4580 return finish_call_expr (decl, &args, false, true,
4581 tf_warning_or_error);
4583 /* Look for a suitable template function, either (C++20) with a single
4584 parameter of class type, or (N3599) with typename parameter CharT and
4585 parameter pack CharT... */
4586 args->truncate (0);
4587 decl = lookup_literal_operator (name, args);
4588 if (decl && decl != error_mark_node)
4590 /* Use resolve_nondeduced_context to try to choose one form of template
4591 or the other. */
4592 tree tmpl_args = make_tree_vec (1);
4593 TREE_VEC_ELT (tmpl_args, 0) = value;
4594 decl = lookup_template_function (decl, tmpl_args);
4595 tree res = resolve_nondeduced_context (decl, tf_none);
4596 if (DECL_P (res))
4597 decl = res;
4598 else
4600 TREE_OPERAND (decl, 1) = make_string_pack (value);
4601 res = resolve_nondeduced_context (decl, tf_none);
4602 if (DECL_P (res))
4603 decl = res;
4605 if (!DECL_P (decl) && cxx_dialect > cxx17)
4606 TREE_OPERAND (decl, 1) = tmpl_args;
4607 return finish_call_expr (decl, &args, false, true,
4608 tf_warning_or_error);
4611 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4612 name, TREE_TYPE (value), size_type_node);
4613 return error_mark_node;
4617 /* Basic concepts [gram.basic] */
4619 /* Parse a translation-unit.
4621 translation-unit:
4622 declaration-seq [opt] */
4624 static void
4625 cp_parser_translation_unit (cp_parser* parser)
4627 gcc_checking_assert (!cp_error_declarator);
4629 /* Create the declarator obstack. */
4630 gcc_obstack_init (&declarator_obstack);
4631 /* Create the error declarator. */
4632 cp_error_declarator = make_declarator (cdk_error);
4633 /* Create the empty parameter list. */
4634 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4635 UNKNOWN_LOCATION);
4636 /* Remember where the base of the declarator obstack lies. */
4637 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4639 bool implicit_extern_c = false;
4641 for (;;)
4643 cp_token *token = cp_lexer_peek_token (parser->lexer);
4645 /* If we're entering or exiting a region that's implicitly
4646 extern "C", modify the lang context appropriately. */
4647 if (implicit_extern_c
4648 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4650 implicit_extern_c = !implicit_extern_c;
4651 if (implicit_extern_c)
4652 push_lang_context (lang_name_c);
4653 else
4654 pop_lang_context ();
4657 if (token->type == CPP_EOF)
4658 break;
4660 if (token->type == CPP_CLOSE_BRACE)
4662 cp_parser_error (parser, "expected declaration");
4663 cp_lexer_consume_token (parser->lexer);
4664 /* If the next token is now a `;', consume it. */
4665 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4666 cp_lexer_consume_token (parser->lexer);
4668 else
4669 cp_parser_toplevel_declaration (parser);
4672 /* Get rid of the token array; we don't need it any more. */
4673 cp_lexer_destroy (parser->lexer);
4674 parser->lexer = NULL;
4676 /* The EOF should have reset this. */
4677 gcc_checking_assert (!implicit_extern_c);
4679 /* Make sure the declarator obstack was fully cleaned up. */
4680 gcc_assert (obstack_next_free (&declarator_obstack)
4681 == declarator_obstack_base);
4684 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4685 decltype context. */
4687 static inline tsubst_flags_t
4688 complain_flags (bool decltype_p)
4690 tsubst_flags_t complain = tf_warning_or_error;
4691 if (decltype_p)
4692 complain |= tf_decltype;
4693 return complain;
4696 /* We're about to parse a collection of statements. If we're currently
4697 parsing tentatively, set up a firewall so that any nested
4698 cp_parser_commit_to_tentative_parse won't affect the current context. */
4700 static cp_token_position
4701 cp_parser_start_tentative_firewall (cp_parser *parser)
4703 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4704 return 0;
4706 cp_parser_parse_tentatively (parser);
4707 cp_parser_commit_to_topmost_tentative_parse (parser);
4708 return cp_lexer_token_position (parser->lexer, false);
4711 /* We've finished parsing the collection of statements. Wrap up the
4712 firewall and replace the relevant tokens with the parsed form. */
4714 static void
4715 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4716 tree expr)
4718 if (!start)
4719 return;
4721 /* Finish the firewall level. */
4722 cp_parser_parse_definitely (parser);
4723 /* And remember the result of the parse for when we try again. */
4724 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4725 token->type = CPP_PREPARSED_EXPR;
4726 token->u.value = expr;
4727 token->keyword = RID_MAX;
4728 cp_lexer_purge_tokens_after (parser->lexer, start);
4731 /* Like the above functions, but let the user modify the tokens. Used by
4732 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4733 later parses, so it makes sense to localize the effects of
4734 cp_parser_commit_to_tentative_parse. */
4736 struct tentative_firewall
4738 cp_parser *parser;
4739 bool set;
4741 tentative_firewall (cp_parser *p): parser(p)
4743 /* If we're currently parsing tentatively, start a committed level as a
4744 firewall and then an inner tentative parse. */
4745 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4747 cp_parser_parse_tentatively (parser);
4748 cp_parser_commit_to_topmost_tentative_parse (parser);
4749 cp_parser_parse_tentatively (parser);
4753 ~tentative_firewall()
4755 if (set)
4757 /* Finish the inner tentative parse and the firewall, propagating any
4758 uncommitted error state to the outer tentative parse. */
4759 bool err = cp_parser_error_occurred (parser);
4760 cp_parser_parse_definitely (parser);
4761 cp_parser_parse_definitely (parser);
4762 if (err)
4763 cp_parser_simulate_error (parser);
4768 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4769 This class is for tracking such a matching pair of symbols.
4770 In particular, it tracks the location of the first token,
4771 so that if the second token is missing, we can highlight the
4772 location of the first token when notifying the user about the
4773 problem. */
4775 template <typename traits_t>
4776 class token_pair
4778 public:
4779 /* token_pair's ctor. */
4780 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4782 /* If the next token is the opening symbol for this pair, consume it and
4783 return true.
4784 Otherwise, issue an error and return false.
4785 In either case, record the location of the opening token. */
4787 bool require_open (cp_parser *parser)
4789 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4790 return cp_parser_require (parser, traits_t::open_token_type,
4791 traits_t::required_token_open);
4794 /* Consume the next token from PARSER, recording its location as
4795 that of the opening token within the pair. */
4797 cp_token * consume_open (cp_parser *parser)
4799 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4800 gcc_assert (tok->type == traits_t::open_token_type);
4801 m_open_loc = tok->location;
4802 return tok;
4805 /* If the next token is the closing symbol for this pair, consume it
4806 and return it.
4807 Otherwise, issue an error, highlighting the location of the
4808 corresponding opening token, and return NULL. */
4810 cp_token *require_close (cp_parser *parser) const
4812 return cp_parser_require (parser, traits_t::close_token_type,
4813 traits_t::required_token_close,
4814 m_open_loc);
4817 private:
4818 location_t m_open_loc;
4821 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4823 struct matching_paren_traits
4825 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4826 static const enum required_token required_token_open = RT_OPEN_PAREN;
4827 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4828 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4831 /* "matching_parens" is a token_pair<T> class for tracking matching
4832 pairs of parentheses. */
4834 typedef token_pair<matching_paren_traits> matching_parens;
4836 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4838 struct matching_brace_traits
4840 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4841 static const enum required_token required_token_open = RT_OPEN_BRACE;
4842 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4843 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4846 /* "matching_braces" is a token_pair<T> class for tracking matching
4847 pairs of braces. */
4849 typedef token_pair<matching_brace_traits> matching_braces;
4852 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4853 enclosing parentheses. */
4855 static cp_expr
4856 cp_parser_statement_expr (cp_parser *parser)
4858 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4860 /* Consume the '('. */
4861 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4862 matching_parens parens;
4863 parens.consume_open (parser);
4864 /* Start the statement-expression. */
4865 tree expr = begin_stmt_expr ();
4866 /* Parse the compound-statement. */
4867 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4868 /* Finish up. */
4869 expr = finish_stmt_expr (expr, false);
4870 /* Consume the ')'. */
4871 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4872 if (!parens.require_close (parser))
4873 cp_parser_skip_to_end_of_statement (parser);
4875 cp_parser_end_tentative_firewall (parser, start, expr);
4876 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4877 return cp_expr (expr, combined_loc);
4880 /* Expressions [gram.expr] */
4882 /* Parse a fold-operator.
4884 fold-operator:
4885 - * / % ^ & | = < > << >>
4886 = -= *= /= %= ^= &= |= <<= >>=
4887 == != <= >= && || , .* ->*
4889 This returns the tree code corresponding to the matched operator
4890 as an int. When the current token matches a compound assignment
4891 opertor, the resulting tree code is the negative value of the
4892 non-assignment operator. */
4894 static int
4895 cp_parser_fold_operator (cp_token *token)
4897 switch (token->type)
4899 case CPP_PLUS: return PLUS_EXPR;
4900 case CPP_MINUS: return MINUS_EXPR;
4901 case CPP_MULT: return MULT_EXPR;
4902 case CPP_DIV: return TRUNC_DIV_EXPR;
4903 case CPP_MOD: return TRUNC_MOD_EXPR;
4904 case CPP_XOR: return BIT_XOR_EXPR;
4905 case CPP_AND: return BIT_AND_EXPR;
4906 case CPP_OR: return BIT_IOR_EXPR;
4907 case CPP_LSHIFT: return LSHIFT_EXPR;
4908 case CPP_RSHIFT: return RSHIFT_EXPR;
4910 case CPP_EQ: return -NOP_EXPR;
4911 case CPP_PLUS_EQ: return -PLUS_EXPR;
4912 case CPP_MINUS_EQ: return -MINUS_EXPR;
4913 case CPP_MULT_EQ: return -MULT_EXPR;
4914 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4915 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4916 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4917 case CPP_AND_EQ: return -BIT_AND_EXPR;
4918 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4919 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4920 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4922 case CPP_EQ_EQ: return EQ_EXPR;
4923 case CPP_NOT_EQ: return NE_EXPR;
4924 case CPP_LESS: return LT_EXPR;
4925 case CPP_GREATER: return GT_EXPR;
4926 case CPP_LESS_EQ: return LE_EXPR;
4927 case CPP_GREATER_EQ: return GE_EXPR;
4929 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4930 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4932 case CPP_COMMA: return COMPOUND_EXPR;
4934 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4935 case CPP_DEREF_STAR: return MEMBER_REF;
4937 default: return ERROR_MARK;
4941 /* Returns true if CODE indicates a binary expression, which is not allowed in
4942 the LHS of a fold-expression. More codes will need to be added to use this
4943 function in other contexts. */
4945 static bool
4946 is_binary_op (tree_code code)
4948 switch (code)
4950 case PLUS_EXPR:
4951 case POINTER_PLUS_EXPR:
4952 case MINUS_EXPR:
4953 case MULT_EXPR:
4954 case TRUNC_DIV_EXPR:
4955 case TRUNC_MOD_EXPR:
4956 case BIT_XOR_EXPR:
4957 case BIT_AND_EXPR:
4958 case BIT_IOR_EXPR:
4959 case LSHIFT_EXPR:
4960 case RSHIFT_EXPR:
4962 case MODOP_EXPR:
4964 case EQ_EXPR:
4965 case NE_EXPR:
4966 case LE_EXPR:
4967 case GE_EXPR:
4968 case LT_EXPR:
4969 case GT_EXPR:
4971 case TRUTH_ANDIF_EXPR:
4972 case TRUTH_ORIF_EXPR:
4974 case COMPOUND_EXPR:
4976 case DOTSTAR_EXPR:
4977 case MEMBER_REF:
4978 return true;
4980 default:
4981 return false;
4985 /* If the next token is a suitable fold operator, consume it and return as
4986 the function above. */
4988 static int
4989 cp_parser_fold_operator (cp_parser *parser)
4991 cp_token* token = cp_lexer_peek_token (parser->lexer);
4992 int code = cp_parser_fold_operator (token);
4993 if (code != ERROR_MARK)
4994 cp_lexer_consume_token (parser->lexer);
4995 return code;
4998 /* Parse a fold-expression.
5000 fold-expression:
5001 ( ... folding-operator cast-expression)
5002 ( cast-expression folding-operator ... )
5003 ( cast-expression folding operator ... folding-operator cast-expression)
5005 Note that the '(' and ')' are matched in primary expression. */
5007 static cp_expr
5008 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5010 cp_id_kind pidk;
5012 // Left fold.
5013 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5015 cp_lexer_consume_token (parser->lexer);
5016 int op = cp_parser_fold_operator (parser);
5017 if (op == ERROR_MARK)
5019 cp_parser_error (parser, "expected binary operator");
5020 return error_mark_node;
5023 tree expr = cp_parser_cast_expression (parser, false, false,
5024 false, &pidk);
5025 if (expr == error_mark_node)
5026 return error_mark_node;
5027 return finish_left_unary_fold_expr (expr, op);
5030 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5031 int op = cp_parser_fold_operator (parser);
5032 if (op == ERROR_MARK)
5034 cp_parser_error (parser, "expected binary operator");
5035 return error_mark_node;
5038 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5040 cp_parser_error (parser, "expected ...");
5041 return error_mark_node;
5043 cp_lexer_consume_token (parser->lexer);
5045 /* The operands of a fold-expression are cast-expressions, so binary or
5046 conditional expressions are not allowed. We check this here to avoid
5047 tentative parsing. */
5048 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5049 /* OK, the expression was parenthesized. */;
5050 else if (is_binary_op (TREE_CODE (expr1)))
5051 error_at (location_of (expr1),
5052 "binary expression in operand of fold-expression");
5053 else if (TREE_CODE (expr1) == COND_EXPR
5054 || (REFERENCE_REF_P (expr1)
5055 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5056 error_at (location_of (expr1),
5057 "conditional expression in operand of fold-expression");
5059 // Right fold.
5060 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5061 return finish_right_unary_fold_expr (expr1, op);
5063 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5065 cp_parser_error (parser, "mismatched operator in fold-expression");
5066 return error_mark_node;
5068 cp_lexer_consume_token (parser->lexer);
5070 // Binary left or right fold.
5071 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5072 if (expr2 == error_mark_node)
5073 return error_mark_node;
5074 return finish_binary_fold_expr (expr1, expr2, op);
5077 /* Parse a primary-expression.
5079 primary-expression:
5080 literal
5081 this
5082 ( expression )
5083 id-expression
5084 lambda-expression (C++11)
5086 GNU Extensions:
5088 primary-expression:
5089 ( compound-statement )
5090 __builtin_va_arg ( assignment-expression , type-id )
5091 __builtin_offsetof ( type-id , offsetof-expression )
5093 C++ Extensions:
5094 __has_nothrow_assign ( type-id )
5095 __has_nothrow_constructor ( type-id )
5096 __has_nothrow_copy ( type-id )
5097 __has_trivial_assign ( type-id )
5098 __has_trivial_constructor ( type-id )
5099 __has_trivial_copy ( type-id )
5100 __has_trivial_destructor ( type-id )
5101 __has_virtual_destructor ( type-id )
5102 __is_abstract ( type-id )
5103 __is_base_of ( type-id , type-id )
5104 __is_class ( type-id )
5105 __is_empty ( type-id )
5106 __is_enum ( type-id )
5107 __is_final ( type-id )
5108 __is_literal_type ( type-id )
5109 __is_pod ( type-id )
5110 __is_polymorphic ( type-id )
5111 __is_std_layout ( type-id )
5112 __is_trivial ( type-id )
5113 __is_union ( type-id )
5115 Objective-C++ Extension:
5117 primary-expression:
5118 objc-expression
5120 literal:
5121 __null
5123 ADDRESS_P is true iff this expression was immediately preceded by
5124 "&" and therefore might denote a pointer-to-member. CAST_P is true
5125 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5126 true iff this expression is a template argument.
5128 Returns a representation of the expression. Upon return, *IDK
5129 indicates what kind of id-expression (if any) was present. */
5131 static cp_expr
5132 cp_parser_primary_expression (cp_parser *parser,
5133 bool address_p,
5134 bool cast_p,
5135 bool template_arg_p,
5136 bool decltype_p,
5137 cp_id_kind *idk)
5139 cp_token *token = NULL;
5141 /* Assume the primary expression is not an id-expression. */
5142 *idk = CP_ID_KIND_NONE;
5144 /* Peek at the next token. */
5145 token = cp_lexer_peek_token (parser->lexer);
5146 switch ((int) token->type)
5148 /* literal:
5149 integer-literal
5150 character-literal
5151 floating-literal
5152 string-literal
5153 boolean-literal
5154 pointer-literal
5155 user-defined-literal */
5156 case CPP_CHAR:
5157 case CPP_CHAR16:
5158 case CPP_CHAR32:
5159 case CPP_WCHAR:
5160 case CPP_UTF8CHAR:
5161 case CPP_NUMBER:
5162 case CPP_PREPARSED_EXPR:
5163 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5164 return cp_parser_userdef_numeric_literal (parser);
5165 token = cp_lexer_consume_token (parser->lexer);
5166 if (TREE_CODE (token->u.value) == FIXED_CST)
5168 error_at (token->location,
5169 "fixed-point types not supported in C++");
5170 return error_mark_node;
5172 /* Floating-point literals are only allowed in an integral
5173 constant expression if they are cast to an integral or
5174 enumeration type. */
5175 if (TREE_CODE (token->u.value) == REAL_CST
5176 && parser->integral_constant_expression_p
5177 && pedantic)
5179 /* CAST_P will be set even in invalid code like "int(2.7 +
5180 ...)". Therefore, we have to check that the next token
5181 is sure to end the cast. */
5182 if (cast_p)
5184 cp_token *next_token;
5186 next_token = cp_lexer_peek_token (parser->lexer);
5187 if (/* The comma at the end of an
5188 enumerator-definition. */
5189 next_token->type != CPP_COMMA
5190 /* The curly brace at the end of an enum-specifier. */
5191 && next_token->type != CPP_CLOSE_BRACE
5192 /* The end of a statement. */
5193 && next_token->type != CPP_SEMICOLON
5194 /* The end of the cast-expression. */
5195 && next_token->type != CPP_CLOSE_PAREN
5196 /* The end of an array bound. */
5197 && next_token->type != CPP_CLOSE_SQUARE
5198 /* The closing ">" in a template-argument-list. */
5199 && (next_token->type != CPP_GREATER
5200 || parser->greater_than_is_operator_p)
5201 /* C++0x only: A ">>" treated like two ">" tokens,
5202 in a template-argument-list. */
5203 && (next_token->type != CPP_RSHIFT
5204 || (cxx_dialect == cxx98)
5205 || parser->greater_than_is_operator_p))
5206 cast_p = false;
5209 /* If we are within a cast, then the constraint that the
5210 cast is to an integral or enumeration type will be
5211 checked at that point. If we are not within a cast, then
5212 this code is invalid. */
5213 if (!cast_p)
5214 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5216 return cp_expr (token->u.value, token->location);
5218 case CPP_CHAR_USERDEF:
5219 case CPP_CHAR16_USERDEF:
5220 case CPP_CHAR32_USERDEF:
5221 case CPP_WCHAR_USERDEF:
5222 case CPP_UTF8CHAR_USERDEF:
5223 return cp_parser_userdef_char_literal (parser);
5225 case CPP_STRING:
5226 case CPP_STRING16:
5227 case CPP_STRING32:
5228 case CPP_WSTRING:
5229 case CPP_UTF8STRING:
5230 case CPP_STRING_USERDEF:
5231 case CPP_STRING16_USERDEF:
5232 case CPP_STRING32_USERDEF:
5233 case CPP_WSTRING_USERDEF:
5234 case CPP_UTF8STRING_USERDEF:
5235 /* ??? Should wide strings be allowed when parser->translate_strings_p
5236 is false (i.e. in attributes)? If not, we can kill the third
5237 argument to cp_parser_string_literal. */
5238 return cp_parser_string_literal (parser,
5239 parser->translate_strings_p,
5240 true);
5242 case CPP_OPEN_PAREN:
5243 /* If we see `( { ' then we are looking at the beginning of
5244 a GNU statement-expression. */
5245 if (cp_parser_allow_gnu_extensions_p (parser)
5246 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5248 /* Statement-expressions are not allowed by the standard. */
5249 pedwarn (token->location, OPT_Wpedantic,
5250 "ISO C++ forbids braced-groups within expressions");
5252 /* And they're not allowed outside of a function-body; you
5253 cannot, for example, write:
5255 int i = ({ int j = 3; j + 1; });
5257 at class or namespace scope. */
5258 if (!parser->in_function_body
5259 || parser->in_template_argument_list_p)
5261 error_at (token->location,
5262 "statement-expressions are not allowed outside "
5263 "functions nor in template-argument lists");
5264 cp_parser_skip_to_end_of_block_or_statement (parser);
5265 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5266 cp_lexer_consume_token (parser->lexer);
5267 return error_mark_node;
5269 else
5270 return cp_parser_statement_expr (parser);
5272 /* Otherwise it's a normal parenthesized expression. */
5274 cp_expr expr;
5275 bool saved_greater_than_is_operator_p;
5277 location_t open_paren_loc = token->location;
5279 /* Consume the `('. */
5280 matching_parens parens;
5281 parens.consume_open (parser);
5282 /* Within a parenthesized expression, a `>' token is always
5283 the greater-than operator. */
5284 saved_greater_than_is_operator_p
5285 = parser->greater_than_is_operator_p;
5286 parser->greater_than_is_operator_p = true;
5288 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5289 /* Left fold expression. */
5290 expr = NULL_TREE;
5291 else
5292 /* Parse the parenthesized expression. */
5293 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5295 token = cp_lexer_peek_token (parser->lexer);
5296 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5298 expr = cp_parser_fold_expression (parser, expr);
5299 if (expr != error_mark_node
5300 && cxx_dialect < cxx17
5301 && !in_system_header_at (input_location))
5302 pedwarn (input_location, 0, "fold-expressions only available "
5303 "with -std=c++17 or -std=gnu++17");
5305 else
5306 /* Let the front end know that this expression was
5307 enclosed in parentheses. This matters in case, for
5308 example, the expression is of the form `A::B', since
5309 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5310 not. */
5311 expr = finish_parenthesized_expr (expr);
5313 /* DR 705: Wrapping an unqualified name in parentheses
5314 suppresses arg-dependent lookup. We want to pass back
5315 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5316 (c++/37862), but none of the others. */
5317 if (*idk != CP_ID_KIND_QUALIFIED)
5318 *idk = CP_ID_KIND_NONE;
5320 /* The `>' token might be the end of a template-id or
5321 template-parameter-list now. */
5322 parser->greater_than_is_operator_p
5323 = saved_greater_than_is_operator_p;
5325 /* Consume the `)'. */
5326 token = cp_lexer_peek_token (parser->lexer);
5327 location_t close_paren_loc = token->location;
5328 expr.set_range (open_paren_loc, close_paren_loc);
5329 if (!parens.require_close (parser)
5330 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5331 cp_parser_skip_to_end_of_statement (parser);
5333 return expr;
5336 case CPP_OPEN_SQUARE:
5338 if (c_dialect_objc ())
5340 /* We might have an Objective-C++ message. */
5341 cp_parser_parse_tentatively (parser);
5342 tree msg = cp_parser_objc_message_expression (parser);
5343 /* If that works out, we're done ... */
5344 if (cp_parser_parse_definitely (parser))
5345 return msg;
5346 /* ... else, fall though to see if it's a lambda. */
5348 cp_expr lam = cp_parser_lambda_expression (parser);
5349 /* Don't warn about a failed tentative parse. */
5350 if (cp_parser_error_occurred (parser))
5351 return error_mark_node;
5352 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5353 return lam;
5356 case CPP_OBJC_STRING:
5357 if (c_dialect_objc ())
5358 /* We have an Objective-C++ string literal. */
5359 return cp_parser_objc_expression (parser);
5360 cp_parser_error (parser, "expected primary-expression");
5361 return error_mark_node;
5363 case CPP_KEYWORD:
5364 switch (token->keyword)
5366 /* These two are the boolean literals. */
5367 case RID_TRUE:
5368 cp_lexer_consume_token (parser->lexer);
5369 return cp_expr (boolean_true_node, token->location);
5370 case RID_FALSE:
5371 cp_lexer_consume_token (parser->lexer);
5372 return cp_expr (boolean_false_node, token->location);
5374 /* The `__null' literal. */
5375 case RID_NULL:
5376 cp_lexer_consume_token (parser->lexer);
5377 return cp_expr (null_node, token->location);
5379 /* The `nullptr' literal. */
5380 case RID_NULLPTR:
5381 cp_lexer_consume_token (parser->lexer);
5382 return cp_expr (nullptr_node, token->location);
5384 /* Recognize the `this' keyword. */
5385 case RID_THIS:
5386 cp_lexer_consume_token (parser->lexer);
5387 if (parser->local_variables_forbidden_p)
5389 error_at (token->location,
5390 "%<this%> may not be used in this context");
5391 return error_mark_node;
5393 /* Pointers cannot appear in constant-expressions. */
5394 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5395 return error_mark_node;
5396 return cp_expr (finish_this_expr (), token->location);
5398 /* The `operator' keyword can be the beginning of an
5399 id-expression. */
5400 case RID_OPERATOR:
5401 goto id_expression;
5403 case RID_FUNCTION_NAME:
5404 case RID_PRETTY_FUNCTION_NAME:
5405 case RID_C99_FUNCTION_NAME:
5407 non_integral_constant name;
5409 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5410 __func__ are the names of variables -- but they are
5411 treated specially. Therefore, they are handled here,
5412 rather than relying on the generic id-expression logic
5413 below. Grammatically, these names are id-expressions.
5415 Consume the token. */
5416 token = cp_lexer_consume_token (parser->lexer);
5418 switch (token->keyword)
5420 case RID_FUNCTION_NAME:
5421 name = NIC_FUNC_NAME;
5422 break;
5423 case RID_PRETTY_FUNCTION_NAME:
5424 name = NIC_PRETTY_FUNC;
5425 break;
5426 case RID_C99_FUNCTION_NAME:
5427 name = NIC_C99_FUNC;
5428 break;
5429 default:
5430 gcc_unreachable ();
5433 if (cp_parser_non_integral_constant_expression (parser, name))
5434 return error_mark_node;
5436 /* Look up the name. */
5437 return finish_fname (token->u.value);
5440 case RID_VA_ARG:
5442 tree expression;
5443 tree type;
5444 source_location type_location;
5445 location_t start_loc
5446 = cp_lexer_peek_token (parser->lexer)->location;
5447 /* The `__builtin_va_arg' construct is used to handle
5448 `va_arg'. Consume the `__builtin_va_arg' token. */
5449 cp_lexer_consume_token (parser->lexer);
5450 /* Look for the opening `('. */
5451 matching_parens parens;
5452 parens.require_open (parser);
5453 /* Now, parse the assignment-expression. */
5454 expression = cp_parser_assignment_expression (parser);
5455 /* Look for the `,'. */
5456 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5457 type_location = cp_lexer_peek_token (parser->lexer)->location;
5458 /* Parse the type-id. */
5460 type_id_in_expr_sentinel s (parser);
5461 type = cp_parser_type_id (parser);
5463 /* Look for the closing `)'. */
5464 location_t finish_loc
5465 = cp_lexer_peek_token (parser->lexer)->location;
5466 parens.require_close (parser);
5467 /* Using `va_arg' in a constant-expression is not
5468 allowed. */
5469 if (cp_parser_non_integral_constant_expression (parser,
5470 NIC_VA_ARG))
5471 return error_mark_node;
5472 /* Construct a location of the form:
5473 __builtin_va_arg (v, int)
5474 ~~~~~~~~~~~~~~~~~~~~~^~~~
5475 with the caret at the type, ranging from the start of the
5476 "__builtin_va_arg" token to the close paren. */
5477 location_t combined_loc
5478 = make_location (type_location, start_loc, finish_loc);
5479 return build_x_va_arg (combined_loc, expression, type);
5482 case RID_OFFSETOF:
5483 return cp_parser_builtin_offsetof (parser);
5485 case RID_HAS_NOTHROW_ASSIGN:
5486 case RID_HAS_NOTHROW_CONSTRUCTOR:
5487 case RID_HAS_NOTHROW_COPY:
5488 case RID_HAS_TRIVIAL_ASSIGN:
5489 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5490 case RID_HAS_TRIVIAL_COPY:
5491 case RID_HAS_TRIVIAL_DESTRUCTOR:
5492 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5493 case RID_HAS_VIRTUAL_DESTRUCTOR:
5494 case RID_IS_ABSTRACT:
5495 case RID_IS_AGGREGATE:
5496 case RID_IS_BASE_OF:
5497 case RID_IS_CLASS:
5498 case RID_IS_EMPTY:
5499 case RID_IS_ENUM:
5500 case RID_IS_FINAL:
5501 case RID_IS_LITERAL_TYPE:
5502 case RID_IS_POD:
5503 case RID_IS_POLYMORPHIC:
5504 case RID_IS_SAME_AS:
5505 case RID_IS_STD_LAYOUT:
5506 case RID_IS_TRIVIAL:
5507 case RID_IS_TRIVIALLY_ASSIGNABLE:
5508 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5509 case RID_IS_TRIVIALLY_COPYABLE:
5510 case RID_IS_UNION:
5511 case RID_IS_ASSIGNABLE:
5512 case RID_IS_CONSTRUCTIBLE:
5513 return cp_parser_trait_expr (parser, token->keyword);
5515 // C++ concepts
5516 case RID_REQUIRES:
5517 return cp_parser_requires_expression (parser);
5519 /* Objective-C++ expressions. */
5520 case RID_AT_ENCODE:
5521 case RID_AT_PROTOCOL:
5522 case RID_AT_SELECTOR:
5523 return cp_parser_objc_expression (parser);
5525 case RID_TEMPLATE:
5526 if (parser->in_function_body
5527 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5528 == CPP_LESS))
5530 error_at (token->location,
5531 "a template declaration cannot appear at block scope");
5532 cp_parser_skip_to_end_of_block_or_statement (parser);
5533 return error_mark_node;
5535 /* FALLTHRU */
5536 default:
5537 cp_parser_error (parser, "expected primary-expression");
5538 return error_mark_node;
5541 /* An id-expression can start with either an identifier, a
5542 `::' as the beginning of a qualified-id, or the "operator"
5543 keyword. */
5544 case CPP_NAME:
5545 case CPP_SCOPE:
5546 case CPP_TEMPLATE_ID:
5547 case CPP_NESTED_NAME_SPECIFIER:
5549 id_expression:
5550 cp_expr id_expression;
5551 cp_expr decl;
5552 const char *error_msg;
5553 bool template_p;
5554 bool done;
5555 cp_token *id_expr_token;
5557 /* Parse the id-expression. */
5558 id_expression
5559 = cp_parser_id_expression (parser,
5560 /*template_keyword_p=*/false,
5561 /*check_dependency_p=*/true,
5562 &template_p,
5563 /*declarator_p=*/false,
5564 /*optional_p=*/false);
5565 if (id_expression == error_mark_node)
5566 return error_mark_node;
5567 id_expr_token = token;
5568 token = cp_lexer_peek_token (parser->lexer);
5569 done = (token->type != CPP_OPEN_SQUARE
5570 && token->type != CPP_OPEN_PAREN
5571 && token->type != CPP_DOT
5572 && token->type != CPP_DEREF
5573 && token->type != CPP_PLUS_PLUS
5574 && token->type != CPP_MINUS_MINUS);
5575 /* If we have a template-id, then no further lookup is
5576 required. If the template-id was for a template-class, we
5577 will sometimes have a TYPE_DECL at this point. */
5578 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5579 || TREE_CODE (id_expression) == TYPE_DECL)
5580 decl = id_expression;
5581 /* Look up the name. */
5582 else
5584 tree ambiguous_decls;
5586 /* If we already know that this lookup is ambiguous, then
5587 we've already issued an error message; there's no reason
5588 to check again. */
5589 if (id_expr_token->type == CPP_NAME
5590 && id_expr_token->error_reported)
5592 cp_parser_simulate_error (parser);
5593 return error_mark_node;
5596 decl = cp_parser_lookup_name (parser, id_expression,
5597 none_type,
5598 template_p,
5599 /*is_namespace=*/false,
5600 /*check_dependency=*/true,
5601 &ambiguous_decls,
5602 id_expr_token->location);
5603 /* If the lookup was ambiguous, an error will already have
5604 been issued. */
5605 if (ambiguous_decls)
5606 return error_mark_node;
5608 /* In Objective-C++, we may have an Objective-C 2.0
5609 dot-syntax for classes here. */
5610 if (c_dialect_objc ()
5611 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5612 && TREE_CODE (decl) == TYPE_DECL
5613 && objc_is_class_name (decl))
5615 tree component;
5616 cp_lexer_consume_token (parser->lexer);
5617 component = cp_parser_identifier (parser);
5618 if (component == error_mark_node)
5619 return error_mark_node;
5621 tree result = objc_build_class_component_ref (id_expression,
5622 component);
5623 /* Build a location of the form:
5624 expr.component
5625 ~~~~~^~~~~~~~~
5626 with caret at the start of the component name (at
5627 input_location), ranging from the start of the id_expression
5628 to the end of the component name. */
5629 location_t combined_loc
5630 = make_location (input_location, id_expression.get_start (),
5631 get_finish (input_location));
5632 protected_set_expr_location (result, combined_loc);
5633 return result;
5636 /* In Objective-C++, an instance variable (ivar) may be preferred
5637 to whatever cp_parser_lookup_name() found.
5638 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5639 rest of c-family, we have to do a little extra work to preserve
5640 any location information in cp_expr "decl". Given that
5641 objc_lookup_ivar is implemented in "c-family" and "objc", we
5642 have a trip through the pure "tree" type, rather than cp_expr.
5643 Naively copying it back to "decl" would implicitly give the
5644 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5645 store an EXPR_LOCATION. Hence we only update "decl" (and
5646 hence its location_t) if we get back a different tree node. */
5647 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5648 id_expression);
5649 if (decl_tree != decl.get_value ())
5650 decl = cp_expr (decl_tree);
5652 /* If name lookup gives us a SCOPE_REF, then the
5653 qualifying scope was dependent. */
5654 if (TREE_CODE (decl) == SCOPE_REF)
5656 /* At this point, we do not know if DECL is a valid
5657 integral constant expression. We assume that it is
5658 in fact such an expression, so that code like:
5660 template <int N> struct A {
5661 int a[B<N>::i];
5664 is accepted. At template-instantiation time, we
5665 will check that B<N>::i is actually a constant. */
5666 return decl;
5668 /* Check to see if DECL is a local variable in a context
5669 where that is forbidden. */
5670 if (parser->local_variables_forbidden_p
5671 && local_variable_p (decl))
5673 error_at (id_expr_token->location,
5674 "local variable %qD may not appear in this context",
5675 decl.get_value ());
5676 return error_mark_node;
5680 if (processing_template_decl)
5681 if (tree fns = maybe_get_fns (decl))
5682 /* It's too difficult to mark ths in all the places where
5683 we know for sure we need to keep the lookup, so do it
5684 now. The cost is extra GC to recycle the lookups
5685 resolved at parse time. */
5686 lookup_keep (fns);
5688 decl = (finish_id_expression
5689 (id_expression, decl, parser->scope,
5690 idk,
5691 parser->integral_constant_expression_p,
5692 parser->allow_non_integral_constant_expression_p,
5693 &parser->non_integral_constant_expression_p,
5694 template_p, done, address_p,
5695 template_arg_p,
5696 &error_msg,
5697 id_expression.get_location ()));
5698 if (error_msg)
5699 cp_parser_error (parser, error_msg);
5700 decl.set_location (id_expr_token->location);
5701 return decl;
5704 /* Anything else is an error. */
5705 default:
5706 cp_parser_error (parser, "expected primary-expression");
5707 return error_mark_node;
5711 static inline cp_expr
5712 cp_parser_primary_expression (cp_parser *parser,
5713 bool address_p,
5714 bool cast_p,
5715 bool template_arg_p,
5716 cp_id_kind *idk)
5718 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5719 /*decltype*/false, idk);
5722 /* Parse an id-expression.
5724 id-expression:
5725 unqualified-id
5726 qualified-id
5728 qualified-id:
5729 :: [opt] nested-name-specifier template [opt] unqualified-id
5730 :: identifier
5731 :: operator-function-id
5732 :: template-id
5734 Return a representation of the unqualified portion of the
5735 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5736 a `::' or nested-name-specifier.
5738 Often, if the id-expression was a qualified-id, the caller will
5739 want to make a SCOPE_REF to represent the qualified-id. This
5740 function does not do this in order to avoid wastefully creating
5741 SCOPE_REFs when they are not required.
5743 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5744 `template' keyword.
5746 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5747 uninstantiated templates.
5749 If *TEMPLATE_P is non-NULL, it is set to true iff the
5750 `template' keyword is used to explicitly indicate that the entity
5751 named is a template.
5753 If DECLARATOR_P is true, the id-expression is appearing as part of
5754 a declarator, rather than as part of an expression. */
5756 static cp_expr
5757 cp_parser_id_expression (cp_parser *parser,
5758 bool template_keyword_p,
5759 bool check_dependency_p,
5760 bool *template_p,
5761 bool declarator_p,
5762 bool optional_p)
5764 bool global_scope_p;
5765 bool nested_name_specifier_p;
5767 /* Assume the `template' keyword was not used. */
5768 if (template_p)
5769 *template_p = template_keyword_p;
5771 /* Look for the optional `::' operator. */
5772 global_scope_p
5773 = (!template_keyword_p
5774 && (cp_parser_global_scope_opt (parser,
5775 /*current_scope_valid_p=*/false)
5776 != NULL_TREE));
5778 /* Look for the optional nested-name-specifier. */
5779 nested_name_specifier_p
5780 = (cp_parser_nested_name_specifier_opt (parser,
5781 /*typename_keyword_p=*/false,
5782 check_dependency_p,
5783 /*type_p=*/false,
5784 declarator_p,
5785 template_keyword_p)
5786 != NULL_TREE);
5788 /* If there is a nested-name-specifier, then we are looking at
5789 the first qualified-id production. */
5790 if (nested_name_specifier_p)
5792 tree saved_scope;
5793 tree saved_object_scope;
5794 tree saved_qualifying_scope;
5795 cp_expr unqualified_id;
5796 bool is_template;
5798 /* See if the next token is the `template' keyword. */
5799 if (!template_p)
5800 template_p = &is_template;
5801 *template_p = cp_parser_optional_template_keyword (parser);
5802 /* Name lookup we do during the processing of the
5803 unqualified-id might obliterate SCOPE. */
5804 saved_scope = parser->scope;
5805 saved_object_scope = parser->object_scope;
5806 saved_qualifying_scope = parser->qualifying_scope;
5807 /* Process the final unqualified-id. */
5808 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5809 check_dependency_p,
5810 declarator_p,
5811 /*optional_p=*/false);
5812 /* Restore the SAVED_SCOPE for our caller. */
5813 parser->scope = saved_scope;
5814 parser->object_scope = saved_object_scope;
5815 parser->qualifying_scope = saved_qualifying_scope;
5817 return unqualified_id;
5819 /* Otherwise, if we are in global scope, then we are looking at one
5820 of the other qualified-id productions. */
5821 else if (global_scope_p)
5823 cp_token *token;
5824 tree id;
5826 /* Peek at the next token. */
5827 token = cp_lexer_peek_token (parser->lexer);
5829 /* If it's an identifier, and the next token is not a "<", then
5830 we can avoid the template-id case. This is an optimization
5831 for this common case. */
5832 if (token->type == CPP_NAME
5833 && !cp_parser_nth_token_starts_template_argument_list_p
5834 (parser, 2))
5835 return cp_parser_identifier (parser);
5837 cp_parser_parse_tentatively (parser);
5838 /* Try a template-id. */
5839 id = cp_parser_template_id (parser,
5840 /*template_keyword_p=*/false,
5841 /*check_dependency_p=*/true,
5842 none_type,
5843 declarator_p);
5844 /* If that worked, we're done. */
5845 if (cp_parser_parse_definitely (parser))
5846 return id;
5848 /* Peek at the next token. (Changes in the token buffer may
5849 have invalidated the pointer obtained above.) */
5850 token = cp_lexer_peek_token (parser->lexer);
5852 switch (token->type)
5854 case CPP_NAME:
5855 return cp_parser_identifier (parser);
5857 case CPP_KEYWORD:
5858 if (token->keyword == RID_OPERATOR)
5859 return cp_parser_operator_function_id (parser);
5860 /* Fall through. */
5862 default:
5863 cp_parser_error (parser, "expected id-expression");
5864 return error_mark_node;
5867 else
5868 return cp_parser_unqualified_id (parser, template_keyword_p,
5869 /*check_dependency_p=*/true,
5870 declarator_p,
5871 optional_p);
5874 /* Parse an unqualified-id.
5876 unqualified-id:
5877 identifier
5878 operator-function-id
5879 conversion-function-id
5880 ~ class-name
5881 template-id
5883 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5884 keyword, in a construct like `A::template ...'.
5886 Returns a representation of unqualified-id. For the `identifier'
5887 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5888 production a BIT_NOT_EXPR is returned; the operand of the
5889 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5890 other productions, see the documentation accompanying the
5891 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5892 names are looked up in uninstantiated templates. If DECLARATOR_P
5893 is true, the unqualified-id is appearing as part of a declarator,
5894 rather than as part of an expression. */
5896 static cp_expr
5897 cp_parser_unqualified_id (cp_parser* parser,
5898 bool template_keyword_p,
5899 bool check_dependency_p,
5900 bool declarator_p,
5901 bool optional_p)
5903 cp_token *token;
5905 /* Peek at the next token. */
5906 token = cp_lexer_peek_token (parser->lexer);
5908 switch ((int) token->type)
5910 case CPP_NAME:
5912 tree id;
5914 /* We don't know yet whether or not this will be a
5915 template-id. */
5916 cp_parser_parse_tentatively (parser);
5917 /* Try a template-id. */
5918 id = cp_parser_template_id (parser, template_keyword_p,
5919 check_dependency_p,
5920 none_type,
5921 declarator_p);
5922 /* If it worked, we're done. */
5923 if (cp_parser_parse_definitely (parser))
5924 return id;
5925 /* Otherwise, it's an ordinary identifier. */
5926 return cp_parser_identifier (parser);
5929 case CPP_TEMPLATE_ID:
5930 return cp_parser_template_id (parser, template_keyword_p,
5931 check_dependency_p,
5932 none_type,
5933 declarator_p);
5935 case CPP_COMPL:
5937 tree type_decl;
5938 tree qualifying_scope;
5939 tree object_scope;
5940 tree scope;
5941 bool done;
5943 /* Consume the `~' token. */
5944 cp_lexer_consume_token (parser->lexer);
5945 /* Parse the class-name. The standard, as written, seems to
5946 say that:
5948 template <typename T> struct S { ~S (); };
5949 template <typename T> S<T>::~S() {}
5951 is invalid, since `~' must be followed by a class-name, but
5952 `S<T>' is dependent, and so not known to be a class.
5953 That's not right; we need to look in uninstantiated
5954 templates. A further complication arises from:
5956 template <typename T> void f(T t) {
5957 t.T::~T();
5960 Here, it is not possible to look up `T' in the scope of `T'
5961 itself. We must look in both the current scope, and the
5962 scope of the containing complete expression.
5964 Yet another issue is:
5966 struct S {
5967 int S;
5968 ~S();
5971 S::~S() {}
5973 The standard does not seem to say that the `S' in `~S'
5974 should refer to the type `S' and not the data member
5975 `S::S'. */
5977 /* DR 244 says that we look up the name after the "~" in the
5978 same scope as we looked up the qualifying name. That idea
5979 isn't fully worked out; it's more complicated than that. */
5980 scope = parser->scope;
5981 object_scope = parser->object_scope;
5982 qualifying_scope = parser->qualifying_scope;
5984 /* Check for invalid scopes. */
5985 if (scope == error_mark_node)
5987 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5988 cp_lexer_consume_token (parser->lexer);
5989 return error_mark_node;
5991 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5993 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5994 error_at (token->location,
5995 "scope %qT before %<~%> is not a class-name",
5996 scope);
5997 cp_parser_simulate_error (parser);
5998 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5999 cp_lexer_consume_token (parser->lexer);
6000 return error_mark_node;
6002 gcc_assert (!scope || TYPE_P (scope));
6004 /* If the name is of the form "X::~X" it's OK even if X is a
6005 typedef. */
6006 token = cp_lexer_peek_token (parser->lexer);
6007 if (scope
6008 && token->type == CPP_NAME
6009 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6010 != CPP_LESS)
6011 && (token->u.value == TYPE_IDENTIFIER (scope)
6012 || (CLASS_TYPE_P (scope)
6013 && constructor_name_p (token->u.value, scope))))
6015 cp_lexer_consume_token (parser->lexer);
6016 return build_nt (BIT_NOT_EXPR, scope);
6019 /* ~auto means the destructor of whatever the object is. */
6020 if (cp_parser_is_keyword (token, RID_AUTO))
6022 if (cxx_dialect < cxx14)
6023 pedwarn (input_location, 0,
6024 "%<~auto%> only available with "
6025 "-std=c++14 or -std=gnu++14");
6026 cp_lexer_consume_token (parser->lexer);
6027 return build_nt (BIT_NOT_EXPR, make_auto ());
6030 /* If there was an explicit qualification (S::~T), first look
6031 in the scope given by the qualification (i.e., S).
6033 Note: in the calls to cp_parser_class_name below we pass
6034 typename_type so that lookup finds the injected-class-name
6035 rather than the constructor. */
6036 done = false;
6037 type_decl = NULL_TREE;
6038 if (scope)
6040 cp_parser_parse_tentatively (parser);
6041 type_decl = cp_parser_class_name (parser,
6042 /*typename_keyword_p=*/false,
6043 /*template_keyword_p=*/false,
6044 typename_type,
6045 /*check_dependency=*/false,
6046 /*class_head_p=*/false,
6047 declarator_p);
6048 if (cp_parser_parse_definitely (parser))
6049 done = true;
6051 /* In "N::S::~S", look in "N" as well. */
6052 if (!done && scope && qualifying_scope)
6054 cp_parser_parse_tentatively (parser);
6055 parser->scope = qualifying_scope;
6056 parser->object_scope = NULL_TREE;
6057 parser->qualifying_scope = NULL_TREE;
6058 type_decl
6059 = cp_parser_class_name (parser,
6060 /*typename_keyword_p=*/false,
6061 /*template_keyword_p=*/false,
6062 typename_type,
6063 /*check_dependency=*/false,
6064 /*class_head_p=*/false,
6065 declarator_p);
6066 if (cp_parser_parse_definitely (parser))
6067 done = true;
6069 /* In "p->S::~T", look in the scope given by "*p" as well. */
6070 else if (!done && object_scope)
6072 cp_parser_parse_tentatively (parser);
6073 parser->scope = object_scope;
6074 parser->object_scope = NULL_TREE;
6075 parser->qualifying_scope = NULL_TREE;
6076 type_decl
6077 = cp_parser_class_name (parser,
6078 /*typename_keyword_p=*/false,
6079 /*template_keyword_p=*/false,
6080 typename_type,
6081 /*check_dependency=*/false,
6082 /*class_head_p=*/false,
6083 declarator_p);
6084 if (cp_parser_parse_definitely (parser))
6085 done = true;
6087 /* Look in the surrounding context. */
6088 if (!done)
6090 parser->scope = NULL_TREE;
6091 parser->object_scope = NULL_TREE;
6092 parser->qualifying_scope = NULL_TREE;
6093 if (processing_template_decl)
6094 cp_parser_parse_tentatively (parser);
6095 type_decl
6096 = cp_parser_class_name (parser,
6097 /*typename_keyword_p=*/false,
6098 /*template_keyword_p=*/false,
6099 typename_type,
6100 /*check_dependency=*/false,
6101 /*class_head_p=*/false,
6102 declarator_p);
6103 if (processing_template_decl
6104 && ! cp_parser_parse_definitely (parser))
6106 /* We couldn't find a type with this name. If we're parsing
6107 tentatively, fail and try something else. */
6108 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6110 cp_parser_simulate_error (parser);
6111 return error_mark_node;
6113 /* Otherwise, accept it and check for a match at instantiation
6114 time. */
6115 type_decl = cp_parser_identifier (parser);
6116 if (type_decl != error_mark_node)
6117 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6118 return type_decl;
6121 /* If an error occurred, assume that the name of the
6122 destructor is the same as the name of the qualifying
6123 class. That allows us to keep parsing after running
6124 into ill-formed destructor names. */
6125 if (type_decl == error_mark_node && scope)
6126 return build_nt (BIT_NOT_EXPR, scope);
6127 else if (type_decl == error_mark_node)
6128 return error_mark_node;
6130 /* Check that destructor name and scope match. */
6131 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6133 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6134 error_at (token->location,
6135 "declaration of %<~%T%> as member of %qT",
6136 type_decl, scope);
6137 cp_parser_simulate_error (parser);
6138 return error_mark_node;
6141 /* [class.dtor]
6143 A typedef-name that names a class shall not be used as the
6144 identifier in the declarator for a destructor declaration. */
6145 if (declarator_p
6146 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6147 && !DECL_SELF_REFERENCE_P (type_decl)
6148 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6149 error_at (token->location,
6150 "typedef-name %qD used as destructor declarator",
6151 type_decl);
6153 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6156 case CPP_KEYWORD:
6157 if (token->keyword == RID_OPERATOR)
6159 cp_expr id;
6161 /* This could be a template-id, so we try that first. */
6162 cp_parser_parse_tentatively (parser);
6163 /* Try a template-id. */
6164 id = cp_parser_template_id (parser, template_keyword_p,
6165 /*check_dependency_p=*/true,
6166 none_type,
6167 declarator_p);
6168 /* If that worked, we're done. */
6169 if (cp_parser_parse_definitely (parser))
6170 return id;
6171 /* We still don't know whether we're looking at an
6172 operator-function-id or a conversion-function-id. */
6173 cp_parser_parse_tentatively (parser);
6174 /* Try an operator-function-id. */
6175 id = cp_parser_operator_function_id (parser);
6176 /* If that didn't work, try a conversion-function-id. */
6177 if (!cp_parser_parse_definitely (parser))
6178 id = cp_parser_conversion_function_id (parser);
6180 return id;
6182 /* Fall through. */
6184 default:
6185 if (optional_p)
6186 return NULL_TREE;
6187 cp_parser_error (parser, "expected unqualified-id");
6188 return error_mark_node;
6192 /* Parse an (optional) nested-name-specifier.
6194 nested-name-specifier: [C++98]
6195 class-or-namespace-name :: nested-name-specifier [opt]
6196 class-or-namespace-name :: template nested-name-specifier [opt]
6198 nested-name-specifier: [C++0x]
6199 type-name ::
6200 namespace-name ::
6201 nested-name-specifier identifier ::
6202 nested-name-specifier template [opt] simple-template-id ::
6204 PARSER->SCOPE should be set appropriately before this function is
6205 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6206 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6207 in name lookups.
6209 Sets PARSER->SCOPE to the class (TYPE) or namespace
6210 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6211 it unchanged if there is no nested-name-specifier. Returns the new
6212 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6214 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6215 part of a declaration and/or decl-specifier. */
6217 static tree
6218 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6219 bool typename_keyword_p,
6220 bool check_dependency_p,
6221 bool type_p,
6222 bool is_declaration,
6223 bool template_keyword_p /* = false */)
6225 bool success = false;
6226 cp_token_position start = 0;
6227 cp_token *token;
6229 /* Remember where the nested-name-specifier starts. */
6230 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6232 start = cp_lexer_token_position (parser->lexer, false);
6233 push_deferring_access_checks (dk_deferred);
6236 while (true)
6238 tree new_scope;
6239 tree old_scope;
6240 tree saved_qualifying_scope;
6242 /* Spot cases that cannot be the beginning of a
6243 nested-name-specifier. */
6244 token = cp_lexer_peek_token (parser->lexer);
6246 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6247 the already parsed nested-name-specifier. */
6248 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6250 /* Grab the nested-name-specifier and continue the loop. */
6251 cp_parser_pre_parsed_nested_name_specifier (parser);
6252 /* If we originally encountered this nested-name-specifier
6253 with IS_DECLARATION set to false, we will not have
6254 resolved TYPENAME_TYPEs, so we must do so here. */
6255 if (is_declaration
6256 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6258 new_scope = resolve_typename_type (parser->scope,
6259 /*only_current_p=*/false);
6260 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6261 parser->scope = new_scope;
6263 success = true;
6264 continue;
6267 /* Spot cases that cannot be the beginning of a
6268 nested-name-specifier. On the second and subsequent times
6269 through the loop, we look for the `template' keyword. */
6270 if (success && token->keyword == RID_TEMPLATE)
6272 /* A template-id can start a nested-name-specifier. */
6273 else if (token->type == CPP_TEMPLATE_ID)
6275 /* DR 743: decltype can be used in a nested-name-specifier. */
6276 else if (token_is_decltype (token))
6278 else
6280 /* If the next token is not an identifier, then it is
6281 definitely not a type-name or namespace-name. */
6282 if (token->type != CPP_NAME)
6283 break;
6284 /* If the following token is neither a `<' (to begin a
6285 template-id), nor a `::', then we are not looking at a
6286 nested-name-specifier. */
6287 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6289 if (token->type == CPP_COLON
6290 && parser->colon_corrects_to_scope_p
6291 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6293 gcc_rich_location richloc (token->location);
6294 richloc.add_fixit_replace ("::");
6295 error_at (&richloc,
6296 "found %<:%> in nested-name-specifier, "
6297 "expected %<::%>");
6298 token->type = CPP_SCOPE;
6301 if (token->type != CPP_SCOPE
6302 && !cp_parser_nth_token_starts_template_argument_list_p
6303 (parser, 2))
6304 break;
6307 /* The nested-name-specifier is optional, so we parse
6308 tentatively. */
6309 cp_parser_parse_tentatively (parser);
6311 /* Look for the optional `template' keyword, if this isn't the
6312 first time through the loop. */
6313 if (success)
6314 template_keyword_p = cp_parser_optional_template_keyword (parser);
6316 /* Save the old scope since the name lookup we are about to do
6317 might destroy it. */
6318 old_scope = parser->scope;
6319 saved_qualifying_scope = parser->qualifying_scope;
6320 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6321 look up names in "X<T>::I" in order to determine that "Y" is
6322 a template. So, if we have a typename at this point, we make
6323 an effort to look through it. */
6324 if (is_declaration
6325 && !typename_keyword_p
6326 && parser->scope
6327 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6328 parser->scope = resolve_typename_type (parser->scope,
6329 /*only_current_p=*/false);
6330 /* Parse the qualifying entity. */
6331 new_scope
6332 = cp_parser_qualifying_entity (parser,
6333 typename_keyword_p,
6334 template_keyword_p,
6335 check_dependency_p,
6336 type_p,
6337 is_declaration);
6338 /* Look for the `::' token. */
6339 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6341 /* If we found what we wanted, we keep going; otherwise, we're
6342 done. */
6343 if (!cp_parser_parse_definitely (parser))
6345 bool error_p = false;
6347 /* Restore the OLD_SCOPE since it was valid before the
6348 failed attempt at finding the last
6349 class-or-namespace-name. */
6350 parser->scope = old_scope;
6351 parser->qualifying_scope = saved_qualifying_scope;
6353 /* If the next token is a decltype, and the one after that is a
6354 `::', then the decltype has failed to resolve to a class or
6355 enumeration type. Give this error even when parsing
6356 tentatively since it can't possibly be valid--and we're going
6357 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6358 won't get another chance.*/
6359 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6360 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6361 == CPP_SCOPE))
6363 token = cp_lexer_consume_token (parser->lexer);
6364 error_at (token->location, "decltype evaluates to %qT, "
6365 "which is not a class or enumeration type",
6366 token->u.tree_check_value->value);
6367 parser->scope = error_mark_node;
6368 error_p = true;
6369 /* As below. */
6370 success = true;
6371 cp_lexer_consume_token (parser->lexer);
6374 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6375 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6377 /* If we have a non-type template-id followed by ::, it can't
6378 possibly be valid. */
6379 token = cp_lexer_peek_token (parser->lexer);
6380 tree tid = token->u.tree_check_value->value;
6381 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6382 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6384 tree tmpl = NULL_TREE;
6385 if (is_overloaded_fn (tid))
6387 tree fns = get_fns (tid);
6388 if (OVL_SINGLE_P (fns))
6389 tmpl = OVL_FIRST (fns);
6390 error_at (token->location, "function template-id %qD "
6391 "in nested-name-specifier", tid);
6393 else
6395 /* Variable template. */
6396 tmpl = TREE_OPERAND (tid, 0);
6397 gcc_assert (variable_template_p (tmpl));
6398 error_at (token->location, "variable template-id %qD "
6399 "in nested-name-specifier", tid);
6401 if (tmpl)
6402 inform (DECL_SOURCE_LOCATION (tmpl),
6403 "%qD declared here", tmpl);
6405 parser->scope = error_mark_node;
6406 error_p = true;
6407 /* As below. */
6408 success = true;
6409 cp_lexer_consume_token (parser->lexer);
6410 cp_lexer_consume_token (parser->lexer);
6414 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6415 break;
6416 /* If the next token is an identifier, and the one after
6417 that is a `::', then any valid interpretation would have
6418 found a class-or-namespace-name. */
6419 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6420 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6421 == CPP_SCOPE)
6422 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6423 != CPP_COMPL))
6425 token = cp_lexer_consume_token (parser->lexer);
6426 if (!error_p)
6428 if (!token->error_reported)
6430 tree decl;
6431 tree ambiguous_decls;
6433 decl = cp_parser_lookup_name (parser, token->u.value,
6434 none_type,
6435 /*is_template=*/false,
6436 /*is_namespace=*/false,
6437 /*check_dependency=*/true,
6438 &ambiguous_decls,
6439 token->location);
6440 if (TREE_CODE (decl) == TEMPLATE_DECL)
6441 error_at (token->location,
6442 "%qD used without template arguments",
6443 decl);
6444 else if (ambiguous_decls)
6446 // cp_parser_lookup_name has the same diagnostic,
6447 // thus make sure to emit it at most once.
6448 if (cp_parser_uncommitted_to_tentative_parse_p
6449 (parser))
6451 error_at (token->location,
6452 "reference to %qD is ambiguous",
6453 token->u.value);
6454 print_candidates (ambiguous_decls);
6456 decl = error_mark_node;
6458 else
6460 if (cxx_dialect != cxx98)
6461 cp_parser_name_lookup_error
6462 (parser, token->u.value, decl, NLE_NOT_CXX98,
6463 token->location);
6464 else
6465 cp_parser_name_lookup_error
6466 (parser, token->u.value, decl, NLE_CXX98,
6467 token->location);
6470 parser->scope = error_mark_node;
6471 error_p = true;
6472 /* Treat this as a successful nested-name-specifier
6473 due to:
6475 [basic.lookup.qual]
6477 If the name found is not a class-name (clause
6478 _class_) or namespace-name (_namespace.def_), the
6479 program is ill-formed. */
6480 success = true;
6482 cp_lexer_consume_token (parser->lexer);
6484 break;
6486 /* We've found one valid nested-name-specifier. */
6487 success = true;
6488 /* Name lookup always gives us a DECL. */
6489 if (TREE_CODE (new_scope) == TYPE_DECL)
6490 new_scope = TREE_TYPE (new_scope);
6491 /* Uses of "template" must be followed by actual templates. */
6492 if (template_keyword_p
6493 && !(CLASS_TYPE_P (new_scope)
6494 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6495 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6496 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6497 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6498 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6499 == TEMPLATE_ID_EXPR)))
6500 permerror (input_location, TYPE_P (new_scope)
6501 ? G_("%qT is not a template")
6502 : G_("%qD is not a template"),
6503 new_scope);
6504 /* If it is a class scope, try to complete it; we are about to
6505 be looking up names inside the class. */
6506 if (TYPE_P (new_scope)
6507 /* Since checking types for dependency can be expensive,
6508 avoid doing it if the type is already complete. */
6509 && !COMPLETE_TYPE_P (new_scope)
6510 /* Do not try to complete dependent types. */
6511 && !dependent_type_p (new_scope))
6513 new_scope = complete_type (new_scope);
6514 /* If it is a typedef to current class, use the current
6515 class instead, as the typedef won't have any names inside
6516 it yet. */
6517 if (!COMPLETE_TYPE_P (new_scope)
6518 && currently_open_class (new_scope))
6519 new_scope = TYPE_MAIN_VARIANT (new_scope);
6521 /* Make sure we look in the right scope the next time through
6522 the loop. */
6523 parser->scope = new_scope;
6526 /* If parsing tentatively, replace the sequence of tokens that makes
6527 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6528 token. That way, should we re-parse the token stream, we will
6529 not have to repeat the effort required to do the parse, nor will
6530 we issue duplicate error messages. */
6531 if (success && start)
6533 cp_token *token;
6535 token = cp_lexer_token_at (parser->lexer, start);
6536 /* Reset the contents of the START token. */
6537 token->type = CPP_NESTED_NAME_SPECIFIER;
6538 /* Retrieve any deferred checks. Do not pop this access checks yet
6539 so the memory will not be reclaimed during token replacing below. */
6540 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6541 token->u.tree_check_value->value = parser->scope;
6542 token->u.tree_check_value->checks = get_deferred_access_checks ();
6543 token->u.tree_check_value->qualifying_scope =
6544 parser->qualifying_scope;
6545 token->keyword = RID_MAX;
6547 /* Purge all subsequent tokens. */
6548 cp_lexer_purge_tokens_after (parser->lexer, start);
6551 if (start)
6552 pop_to_parent_deferring_access_checks ();
6554 return success ? parser->scope : NULL_TREE;
6557 /* Parse a nested-name-specifier. See
6558 cp_parser_nested_name_specifier_opt for details. This function
6559 behaves identically, except that it will an issue an error if no
6560 nested-name-specifier is present. */
6562 static tree
6563 cp_parser_nested_name_specifier (cp_parser *parser,
6564 bool typename_keyword_p,
6565 bool check_dependency_p,
6566 bool type_p,
6567 bool is_declaration)
6569 tree scope;
6571 /* Look for the nested-name-specifier. */
6572 scope = cp_parser_nested_name_specifier_opt (parser,
6573 typename_keyword_p,
6574 check_dependency_p,
6575 type_p,
6576 is_declaration);
6577 /* If it was not present, issue an error message. */
6578 if (!scope)
6580 cp_parser_error (parser, "expected nested-name-specifier");
6581 parser->scope = NULL_TREE;
6584 return scope;
6587 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6588 this is either a class-name or a namespace-name (which corresponds
6589 to the class-or-namespace-name production in the grammar). For
6590 C++0x, it can also be a type-name that refers to an enumeration
6591 type or a simple-template-id.
6593 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6594 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6595 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6596 TYPE_P is TRUE iff the next name should be taken as a class-name,
6597 even the same name is declared to be another entity in the same
6598 scope.
6600 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6601 specified by the class-or-namespace-name. If neither is found the
6602 ERROR_MARK_NODE is returned. */
6604 static tree
6605 cp_parser_qualifying_entity (cp_parser *parser,
6606 bool typename_keyword_p,
6607 bool template_keyword_p,
6608 bool check_dependency_p,
6609 bool type_p,
6610 bool is_declaration)
6612 tree saved_scope;
6613 tree saved_qualifying_scope;
6614 tree saved_object_scope;
6615 tree scope;
6616 bool only_class_p;
6617 bool successful_parse_p;
6619 /* DR 743: decltype can appear in a nested-name-specifier. */
6620 if (cp_lexer_next_token_is_decltype (parser->lexer))
6622 scope = cp_parser_decltype (parser);
6623 if (TREE_CODE (scope) != ENUMERAL_TYPE
6624 && !MAYBE_CLASS_TYPE_P (scope))
6626 cp_parser_simulate_error (parser);
6627 return error_mark_node;
6629 if (TYPE_NAME (scope))
6630 scope = TYPE_NAME (scope);
6631 return scope;
6634 /* Before we try to parse the class-name, we must save away the
6635 current PARSER->SCOPE since cp_parser_class_name will destroy
6636 it. */
6637 saved_scope = parser->scope;
6638 saved_qualifying_scope = parser->qualifying_scope;
6639 saved_object_scope = parser->object_scope;
6640 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6641 there is no need to look for a namespace-name. */
6642 only_class_p = template_keyword_p
6643 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6644 if (!only_class_p)
6645 cp_parser_parse_tentatively (parser);
6646 scope = cp_parser_class_name (parser,
6647 typename_keyword_p,
6648 template_keyword_p,
6649 type_p ? class_type : none_type,
6650 check_dependency_p,
6651 /*class_head_p=*/false,
6652 is_declaration,
6653 /*enum_ok=*/cxx_dialect > cxx98);
6654 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6655 /* If that didn't work, try for a namespace-name. */
6656 if (!only_class_p && !successful_parse_p)
6658 /* Restore the saved scope. */
6659 parser->scope = saved_scope;
6660 parser->qualifying_scope = saved_qualifying_scope;
6661 parser->object_scope = saved_object_scope;
6662 /* If we are not looking at an identifier followed by the scope
6663 resolution operator, then this is not part of a
6664 nested-name-specifier. (Note that this function is only used
6665 to parse the components of a nested-name-specifier.) */
6666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6667 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6668 return error_mark_node;
6669 scope = cp_parser_namespace_name (parser);
6672 return scope;
6675 /* Return true if we are looking at a compound-literal, false otherwise. */
6677 static bool
6678 cp_parser_compound_literal_p (cp_parser *parser)
6680 cp_lexer_save_tokens (parser->lexer);
6682 /* Skip tokens until the next token is a closing parenthesis.
6683 If we find the closing `)', and the next token is a `{', then
6684 we are looking at a compound-literal. */
6685 bool compound_literal_p
6686 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6687 /*consume_paren=*/true)
6688 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6690 /* Roll back the tokens we skipped. */
6691 cp_lexer_rollback_tokens (parser->lexer);
6693 return compound_literal_p;
6696 /* Return true if EXPR is the integer constant zero or a complex constant
6697 of zero, without any folding, but ignoring location wrappers. */
6699 bool
6700 literal_integer_zerop (const_tree expr)
6702 return (location_wrapper_p (expr)
6703 && integer_zerop (TREE_OPERAND (expr, 0)));
6706 /* Parse a postfix-expression.
6708 postfix-expression:
6709 primary-expression
6710 postfix-expression [ expression ]
6711 postfix-expression ( expression-list [opt] )
6712 simple-type-specifier ( expression-list [opt] )
6713 typename :: [opt] nested-name-specifier identifier
6714 ( expression-list [opt] )
6715 typename :: [opt] nested-name-specifier template [opt] template-id
6716 ( expression-list [opt] )
6717 postfix-expression . template [opt] id-expression
6718 postfix-expression -> template [opt] id-expression
6719 postfix-expression . pseudo-destructor-name
6720 postfix-expression -> pseudo-destructor-name
6721 postfix-expression ++
6722 postfix-expression --
6723 dynamic_cast < type-id > ( expression )
6724 static_cast < type-id > ( expression )
6725 reinterpret_cast < type-id > ( expression )
6726 const_cast < type-id > ( expression )
6727 typeid ( expression )
6728 typeid ( type-id )
6730 GNU Extension:
6732 postfix-expression:
6733 ( type-id ) { initializer-list , [opt] }
6735 This extension is a GNU version of the C99 compound-literal
6736 construct. (The C99 grammar uses `type-name' instead of `type-id',
6737 but they are essentially the same concept.)
6739 If ADDRESS_P is true, the postfix expression is the operand of the
6740 `&' operator. CAST_P is true if this expression is the target of a
6741 cast.
6743 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6744 class member access expressions [expr.ref].
6746 Returns a representation of the expression. */
6748 static cp_expr
6749 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6750 bool member_access_only_p, bool decltype_p,
6751 cp_id_kind * pidk_return)
6753 cp_token *token;
6754 location_t loc;
6755 enum rid keyword;
6756 cp_id_kind idk = CP_ID_KIND_NONE;
6757 cp_expr postfix_expression = NULL_TREE;
6758 bool is_member_access = false;
6760 /* Peek at the next token. */
6761 token = cp_lexer_peek_token (parser->lexer);
6762 loc = token->location;
6763 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6765 /* Some of the productions are determined by keywords. */
6766 keyword = token->keyword;
6767 switch (keyword)
6769 case RID_DYNCAST:
6770 case RID_STATCAST:
6771 case RID_REINTCAST:
6772 case RID_CONSTCAST:
6774 tree type;
6775 cp_expr expression;
6776 const char *saved_message;
6777 bool saved_in_type_id_in_expr_p;
6779 /* All of these can be handled in the same way from the point
6780 of view of parsing. Begin by consuming the token
6781 identifying the cast. */
6782 cp_lexer_consume_token (parser->lexer);
6784 /* New types cannot be defined in the cast. */
6785 saved_message = parser->type_definition_forbidden_message;
6786 parser->type_definition_forbidden_message
6787 = G_("types may not be defined in casts");
6789 /* Look for the opening `<'. */
6790 cp_parser_require (parser, CPP_LESS, RT_LESS);
6791 /* Parse the type to which we are casting. */
6792 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6793 parser->in_type_id_in_expr_p = true;
6794 type = cp_parser_type_id (parser);
6795 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6796 /* Look for the closing `>'. */
6797 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6798 /* Restore the old message. */
6799 parser->type_definition_forbidden_message = saved_message;
6801 bool saved_greater_than_is_operator_p
6802 = parser->greater_than_is_operator_p;
6803 parser->greater_than_is_operator_p = true;
6805 /* And the expression which is being cast. */
6806 matching_parens parens;
6807 parens.require_open (parser);
6808 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6809 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6810 RT_CLOSE_PAREN);
6811 location_t end_loc = close_paren ?
6812 close_paren->location : UNKNOWN_LOCATION;
6814 parser->greater_than_is_operator_p
6815 = saved_greater_than_is_operator_p;
6817 /* Only type conversions to integral or enumeration types
6818 can be used in constant-expressions. */
6819 if (!cast_valid_in_integral_constant_expression_p (type)
6820 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6822 postfix_expression = error_mark_node;
6823 break;
6826 switch (keyword)
6828 case RID_DYNCAST:
6829 postfix_expression
6830 = build_dynamic_cast (type, expression, tf_warning_or_error);
6831 break;
6832 case RID_STATCAST:
6833 postfix_expression
6834 = build_static_cast (type, expression, tf_warning_or_error);
6835 break;
6836 case RID_REINTCAST:
6837 postfix_expression
6838 = build_reinterpret_cast (type, expression,
6839 tf_warning_or_error);
6840 break;
6841 case RID_CONSTCAST:
6842 postfix_expression
6843 = build_const_cast (type, expression, tf_warning_or_error);
6844 break;
6845 default:
6846 gcc_unreachable ();
6849 /* Construct a location e.g. :
6850 reinterpret_cast <int *> (expr)
6851 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6852 ranging from the start of the "*_cast" token to the final closing
6853 paren, with the caret at the start. */
6854 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6855 postfix_expression.set_location (cp_cast_loc);
6857 break;
6859 case RID_TYPEID:
6861 tree type;
6862 const char *saved_message;
6863 bool saved_in_type_id_in_expr_p;
6865 /* Consume the `typeid' token. */
6866 cp_lexer_consume_token (parser->lexer);
6867 /* Look for the `(' token. */
6868 matching_parens parens;
6869 parens.require_open (parser);
6870 /* Types cannot be defined in a `typeid' expression. */
6871 saved_message = parser->type_definition_forbidden_message;
6872 parser->type_definition_forbidden_message
6873 = G_("types may not be defined in a %<typeid%> expression");
6874 /* We can't be sure yet whether we're looking at a type-id or an
6875 expression. */
6876 cp_parser_parse_tentatively (parser);
6877 /* Try a type-id first. */
6878 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6879 parser->in_type_id_in_expr_p = true;
6880 type = cp_parser_type_id (parser);
6881 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6882 /* Look for the `)' token. Otherwise, we can't be sure that
6883 we're not looking at an expression: consider `typeid (int
6884 (3))', for example. */
6885 cp_token *close_paren = parens.require_close (parser);
6886 /* If all went well, simply lookup the type-id. */
6887 if (cp_parser_parse_definitely (parser))
6888 postfix_expression = get_typeid (type, tf_warning_or_error);
6889 /* Otherwise, fall back to the expression variant. */
6890 else
6892 tree expression;
6894 /* Look for an expression. */
6895 expression = cp_parser_expression (parser, & idk);
6896 /* Compute its typeid. */
6897 postfix_expression = build_typeid (expression, tf_warning_or_error);
6898 /* Look for the `)' token. */
6899 close_paren = parens.require_close (parser);
6901 /* Restore the saved message. */
6902 parser->type_definition_forbidden_message = saved_message;
6903 /* `typeid' may not appear in an integral constant expression. */
6904 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6905 postfix_expression = error_mark_node;
6907 /* Construct a location e.g. :
6908 typeid (expr)
6909 ^~~~~~~~~~~~~
6910 ranging from the start of the "typeid" token to the final closing
6911 paren, with the caret at the start. */
6912 if (close_paren)
6914 location_t typeid_loc
6915 = make_location (start_loc, start_loc, close_paren->location);
6916 postfix_expression.set_location (typeid_loc);
6917 postfix_expression.maybe_add_location_wrapper ();
6920 break;
6922 case RID_TYPENAME:
6924 tree type;
6925 /* The syntax permitted here is the same permitted for an
6926 elaborated-type-specifier. */
6927 ++parser->prevent_constrained_type_specifiers;
6928 type = cp_parser_elaborated_type_specifier (parser,
6929 /*is_friend=*/false,
6930 /*is_declaration=*/false);
6931 --parser->prevent_constrained_type_specifiers;
6932 postfix_expression = cp_parser_functional_cast (parser, type);
6934 break;
6936 case RID_ADDRESSOF:
6937 case RID_BUILTIN_SHUFFLE:
6938 case RID_BUILTIN_LAUNDER:
6940 vec<tree, va_gc> *vec;
6941 unsigned int i;
6942 tree p;
6944 cp_lexer_consume_token (parser->lexer);
6945 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6946 /*cast_p=*/false, /*allow_expansion_p=*/true,
6947 /*non_constant_p=*/NULL);
6948 if (vec == NULL)
6950 postfix_expression = error_mark_node;
6951 break;
6954 FOR_EACH_VEC_ELT (*vec, i, p)
6955 mark_exp_read (p);
6957 switch (keyword)
6959 case RID_ADDRESSOF:
6960 if (vec->length () == 1)
6961 postfix_expression
6962 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6963 else
6965 error_at (loc, "wrong number of arguments to "
6966 "%<__builtin_addressof%>");
6967 postfix_expression = error_mark_node;
6969 break;
6971 case RID_BUILTIN_LAUNDER:
6972 if (vec->length () == 1)
6973 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6974 tf_warning_or_error);
6975 else
6977 error_at (loc, "wrong number of arguments to "
6978 "%<__builtin_launder%>");
6979 postfix_expression = error_mark_node;
6981 break;
6983 case RID_BUILTIN_SHUFFLE:
6984 if (vec->length () == 2)
6985 postfix_expression
6986 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6987 (*vec)[1], tf_warning_or_error);
6988 else if (vec->length () == 3)
6989 postfix_expression
6990 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6991 (*vec)[2], tf_warning_or_error);
6992 else
6994 error_at (loc, "wrong number of arguments to "
6995 "%<__builtin_shuffle%>");
6996 postfix_expression = error_mark_node;
6998 break;
7000 default:
7001 gcc_unreachable ();
7003 break;
7006 default:
7008 tree type;
7010 /* If the next thing is a simple-type-specifier, we may be
7011 looking at a functional cast. We could also be looking at
7012 an id-expression. So, we try the functional cast, and if
7013 that doesn't work we fall back to the primary-expression. */
7014 cp_parser_parse_tentatively (parser);
7015 /* Look for the simple-type-specifier. */
7016 ++parser->prevent_constrained_type_specifiers;
7017 type = cp_parser_simple_type_specifier (parser,
7018 /*decl_specs=*/NULL,
7019 CP_PARSER_FLAGS_NONE);
7020 --parser->prevent_constrained_type_specifiers;
7021 /* Parse the cast itself. */
7022 if (!cp_parser_error_occurred (parser))
7023 postfix_expression
7024 = cp_parser_functional_cast (parser, type);
7025 /* If that worked, we're done. */
7026 if (cp_parser_parse_definitely (parser))
7027 break;
7029 /* If the functional-cast didn't work out, try a
7030 compound-literal. */
7031 if (cp_parser_allow_gnu_extensions_p (parser)
7032 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7034 cp_expr initializer = NULL_TREE;
7036 cp_parser_parse_tentatively (parser);
7038 matching_parens parens;
7039 parens.consume_open (parser);
7041 /* Avoid calling cp_parser_type_id pointlessly, see comment
7042 in cp_parser_cast_expression about c++/29234. */
7043 if (!cp_parser_compound_literal_p (parser))
7044 cp_parser_simulate_error (parser);
7045 else
7047 /* Parse the type. */
7048 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7049 parser->in_type_id_in_expr_p = true;
7050 type = cp_parser_type_id (parser);
7051 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7052 parens.require_close (parser);
7055 /* If things aren't going well, there's no need to
7056 keep going. */
7057 if (!cp_parser_error_occurred (parser))
7059 bool non_constant_p;
7060 /* Parse the brace-enclosed initializer list. */
7061 initializer = cp_parser_braced_list (parser,
7062 &non_constant_p);
7064 /* If that worked, we're definitely looking at a
7065 compound-literal expression. */
7066 if (cp_parser_parse_definitely (parser))
7068 /* Warn the user that a compound literal is not
7069 allowed in standard C++. */
7070 pedwarn (input_location, OPT_Wpedantic,
7071 "ISO C++ forbids compound-literals");
7072 /* For simplicity, we disallow compound literals in
7073 constant-expressions. We could
7074 allow compound literals of integer type, whose
7075 initializer was a constant, in constant
7076 expressions. Permitting that usage, as a further
7077 extension, would not change the meaning of any
7078 currently accepted programs. (Of course, as
7079 compound literals are not part of ISO C++, the
7080 standard has nothing to say.) */
7081 if (cp_parser_non_integral_constant_expression (parser,
7082 NIC_NCC))
7084 postfix_expression = error_mark_node;
7085 break;
7087 /* Form the representation of the compound-literal. */
7088 postfix_expression
7089 = finish_compound_literal (type, initializer,
7090 tf_warning_or_error, fcl_c99);
7091 postfix_expression.set_location (initializer.get_location ());
7092 break;
7096 /* It must be a primary-expression. */
7097 postfix_expression
7098 = cp_parser_primary_expression (parser, address_p, cast_p,
7099 /*template_arg_p=*/false,
7100 decltype_p,
7101 &idk);
7103 break;
7106 /* Note that we don't need to worry about calling build_cplus_new on a
7107 class-valued CALL_EXPR in decltype when it isn't the end of the
7108 postfix-expression; unary_complex_lvalue will take care of that for
7109 all these cases. */
7111 /* Keep looping until the postfix-expression is complete. */
7112 while (true)
7114 if (idk == CP_ID_KIND_UNQUALIFIED
7115 && identifier_p (postfix_expression)
7116 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7117 /* It is not a Koenig lookup function call. */
7118 postfix_expression
7119 = unqualified_name_lookup_error (postfix_expression);
7121 /* Peek at the next token. */
7122 token = cp_lexer_peek_token (parser->lexer);
7124 switch (token->type)
7126 case CPP_OPEN_SQUARE:
7127 if (cp_next_tokens_can_be_std_attribute_p (parser))
7129 cp_parser_error (parser,
7130 "two consecutive %<[%> shall "
7131 "only introduce an attribute");
7132 return error_mark_node;
7134 postfix_expression
7135 = cp_parser_postfix_open_square_expression (parser,
7136 postfix_expression,
7137 false,
7138 decltype_p);
7139 postfix_expression.set_range (start_loc,
7140 postfix_expression.get_location ());
7142 idk = CP_ID_KIND_NONE;
7143 is_member_access = false;
7144 break;
7146 case CPP_OPEN_PAREN:
7147 /* postfix-expression ( expression-list [opt] ) */
7149 bool koenig_p;
7150 bool is_builtin_constant_p;
7151 bool saved_integral_constant_expression_p = false;
7152 bool saved_non_integral_constant_expression_p = false;
7153 tsubst_flags_t complain = complain_flags (decltype_p);
7154 vec<tree, va_gc> *args;
7155 location_t close_paren_loc = UNKNOWN_LOCATION;
7157 is_member_access = false;
7159 is_builtin_constant_p
7160 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7161 if (is_builtin_constant_p)
7163 /* The whole point of __builtin_constant_p is to allow
7164 non-constant expressions to appear as arguments. */
7165 saved_integral_constant_expression_p
7166 = parser->integral_constant_expression_p;
7167 saved_non_integral_constant_expression_p
7168 = parser->non_integral_constant_expression_p;
7169 parser->integral_constant_expression_p = false;
7171 args = (cp_parser_parenthesized_expression_list
7172 (parser, non_attr,
7173 /*cast_p=*/false, /*allow_expansion_p=*/true,
7174 /*non_constant_p=*/NULL,
7175 /*close_paren_loc=*/&close_paren_loc,
7176 /*wrap_locations_p=*/true));
7177 if (is_builtin_constant_p)
7179 parser->integral_constant_expression_p
7180 = saved_integral_constant_expression_p;
7181 parser->non_integral_constant_expression_p
7182 = saved_non_integral_constant_expression_p;
7185 if (args == NULL)
7187 postfix_expression = error_mark_node;
7188 break;
7191 /* Function calls are not permitted in
7192 constant-expressions. */
7193 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7194 && cp_parser_non_integral_constant_expression (parser,
7195 NIC_FUNC_CALL))
7197 postfix_expression = error_mark_node;
7198 release_tree_vector (args);
7199 break;
7202 koenig_p = false;
7203 if (idk == CP_ID_KIND_UNQUALIFIED
7204 || idk == CP_ID_KIND_TEMPLATE_ID)
7206 if (identifier_p (postfix_expression)
7207 /* In C++2A, we may need to perform ADL for a template
7208 name. */
7209 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7210 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7212 if (!args->is_empty ())
7214 koenig_p = true;
7215 if (!any_type_dependent_arguments_p (args))
7216 postfix_expression
7217 = perform_koenig_lookup (postfix_expression, args,
7218 complain);
7220 else
7221 postfix_expression
7222 = unqualified_fn_lookup_error (postfix_expression);
7224 /* We do not perform argument-dependent lookup if
7225 normal lookup finds a non-function, in accordance
7226 with the expected resolution of DR 218. */
7227 else if (!args->is_empty ()
7228 && is_overloaded_fn (postfix_expression))
7230 tree fn = get_first_fn (postfix_expression);
7231 fn = STRIP_TEMPLATE (fn);
7233 /* Do not do argument dependent lookup if regular
7234 lookup finds a member function or a block-scope
7235 function declaration. [basic.lookup.argdep]/3 */
7236 if (!DECL_FUNCTION_MEMBER_P (fn)
7237 && !DECL_LOCAL_FUNCTION_P (fn))
7239 koenig_p = true;
7240 if (!any_type_dependent_arguments_p (args))
7241 postfix_expression
7242 = perform_koenig_lookup (postfix_expression, args,
7243 complain);
7248 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7250 tree instance = TREE_OPERAND (postfix_expression, 0);
7251 tree fn = TREE_OPERAND (postfix_expression, 1);
7253 if (processing_template_decl
7254 && (type_dependent_object_expression_p (instance)
7255 || (!BASELINK_P (fn)
7256 && TREE_CODE (fn) != FIELD_DECL)
7257 || type_dependent_expression_p (fn)
7258 || any_type_dependent_arguments_p (args)))
7260 maybe_generic_this_capture (instance, fn);
7261 postfix_expression
7262 = build_min_nt_call_vec (postfix_expression, args);
7263 release_tree_vector (args);
7264 break;
7267 if (BASELINK_P (fn))
7269 postfix_expression
7270 = (build_new_method_call
7271 (instance, fn, &args, NULL_TREE,
7272 (idk == CP_ID_KIND_QUALIFIED
7273 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7274 : LOOKUP_NORMAL),
7275 /*fn_p=*/NULL,
7276 complain));
7278 else
7279 postfix_expression
7280 = finish_call_expr (postfix_expression, &args,
7281 /*disallow_virtual=*/false,
7282 /*koenig_p=*/false,
7283 complain);
7285 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7286 || TREE_CODE (postfix_expression) == MEMBER_REF
7287 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7288 postfix_expression = (build_offset_ref_call_from_tree
7289 (postfix_expression, &args,
7290 complain));
7291 else if (idk == CP_ID_KIND_QUALIFIED)
7292 /* A call to a static class member, or a namespace-scope
7293 function. */
7294 postfix_expression
7295 = finish_call_expr (postfix_expression, &args,
7296 /*disallow_virtual=*/true,
7297 koenig_p,
7298 complain);
7299 else
7300 /* All other function calls. */
7301 postfix_expression
7302 = finish_call_expr (postfix_expression, &args,
7303 /*disallow_virtual=*/false,
7304 koenig_p,
7305 complain);
7307 if (close_paren_loc != UNKNOWN_LOCATION)
7309 location_t combined_loc = make_location (token->location,
7310 start_loc,
7311 close_paren_loc);
7312 postfix_expression.set_location (combined_loc);
7315 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7316 idk = CP_ID_KIND_NONE;
7318 release_tree_vector (args);
7320 break;
7322 case CPP_DOT:
7323 case CPP_DEREF:
7324 /* postfix-expression . template [opt] id-expression
7325 postfix-expression . pseudo-destructor-name
7326 postfix-expression -> template [opt] id-expression
7327 postfix-expression -> pseudo-destructor-name */
7329 /* Consume the `.' or `->' operator. */
7330 cp_lexer_consume_token (parser->lexer);
7332 postfix_expression
7333 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7334 postfix_expression,
7335 false, &idk, loc);
7337 is_member_access = true;
7338 break;
7340 case CPP_PLUS_PLUS:
7341 /* postfix-expression ++ */
7342 /* Consume the `++' token. */
7343 cp_lexer_consume_token (parser->lexer);
7344 /* Generate a representation for the complete expression. */
7345 postfix_expression
7346 = finish_increment_expr (postfix_expression,
7347 POSTINCREMENT_EXPR);
7348 /* Increments may not appear in constant-expressions. */
7349 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7350 postfix_expression = error_mark_node;
7351 idk = CP_ID_KIND_NONE;
7352 is_member_access = false;
7353 break;
7355 case CPP_MINUS_MINUS:
7356 /* postfix-expression -- */
7357 /* Consume the `--' token. */
7358 cp_lexer_consume_token (parser->lexer);
7359 /* Generate a representation for the complete expression. */
7360 postfix_expression
7361 = finish_increment_expr (postfix_expression,
7362 POSTDECREMENT_EXPR);
7363 /* Decrements may not appear in constant-expressions. */
7364 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7365 postfix_expression = error_mark_node;
7366 idk = CP_ID_KIND_NONE;
7367 is_member_access = false;
7368 break;
7370 default:
7371 if (pidk_return != NULL)
7372 * pidk_return = idk;
7373 if (member_access_only_p)
7374 return is_member_access
7375 ? postfix_expression
7376 : cp_expr (error_mark_node);
7377 else
7378 return postfix_expression;
7382 /* We should never get here. */
7383 gcc_unreachable ();
7384 return error_mark_node;
7387 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7388 by cp_parser_builtin_offsetof. We're looking for
7390 postfix-expression [ expression ]
7391 postfix-expression [ braced-init-list ] (C++11)
7393 FOR_OFFSETOF is set if we're being called in that context, which
7394 changes how we deal with integer constant expressions. */
7396 static tree
7397 cp_parser_postfix_open_square_expression (cp_parser *parser,
7398 tree postfix_expression,
7399 bool for_offsetof,
7400 bool decltype_p)
7402 tree index = NULL_TREE;
7403 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7404 bool saved_greater_than_is_operator_p;
7406 /* Consume the `[' token. */
7407 cp_lexer_consume_token (parser->lexer);
7409 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7410 parser->greater_than_is_operator_p = true;
7412 /* Parse the index expression. */
7413 /* ??? For offsetof, there is a question of what to allow here. If
7414 offsetof is not being used in an integral constant expression context,
7415 then we *could* get the right answer by computing the value at runtime.
7416 If we are in an integral constant expression context, then we might
7417 could accept any constant expression; hard to say without analysis.
7418 Rather than open the barn door too wide right away, allow only integer
7419 constant expressions here. */
7420 if (for_offsetof)
7421 index = cp_parser_constant_expression (parser);
7422 else
7424 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7426 bool expr_nonconst_p;
7427 cp_lexer_set_source_position (parser->lexer);
7428 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7429 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7431 else
7432 index = cp_parser_expression (parser);
7435 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7437 /* Look for the closing `]'. */
7438 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7440 /* Build the ARRAY_REF. */
7441 postfix_expression = grok_array_decl (loc, postfix_expression,
7442 index, decltype_p);
7444 /* When not doing offsetof, array references are not permitted in
7445 constant-expressions. */
7446 if (!for_offsetof
7447 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7448 postfix_expression = error_mark_node;
7450 return postfix_expression;
7453 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7454 dereference of incomplete type, returns true if error_mark_node should
7455 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7456 and *DEPENDENT_P. */
7458 bool
7459 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7460 bool *dependent_p)
7462 /* In a template, be permissive by treating an object expression
7463 of incomplete type as dependent (after a pedwarn). */
7464 diagnostic_t kind = (processing_template_decl
7465 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7467 switch (TREE_CODE (*postfix_expression))
7469 case CAST_EXPR:
7470 case REINTERPRET_CAST_EXPR:
7471 case CONST_CAST_EXPR:
7472 case STATIC_CAST_EXPR:
7473 case DYNAMIC_CAST_EXPR:
7474 case IMPLICIT_CONV_EXPR:
7475 case VIEW_CONVERT_EXPR:
7476 case NON_LVALUE_EXPR:
7477 kind = DK_ERROR;
7478 break;
7479 case OVERLOAD:
7480 /* Don't emit any diagnostic for OVERLOADs. */
7481 kind = DK_IGNORED;
7482 break;
7483 default:
7484 /* Avoid clobbering e.g. DECLs. */
7485 if (!EXPR_P (*postfix_expression))
7486 kind = DK_ERROR;
7487 break;
7490 if (kind == DK_IGNORED)
7491 return false;
7493 location_t exploc = location_of (*postfix_expression);
7494 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7495 if (!MAYBE_CLASS_TYPE_P (*scope))
7496 return true;
7497 if (kind == DK_ERROR)
7498 *scope = *postfix_expression = error_mark_node;
7499 else if (processing_template_decl)
7501 *dependent_p = true;
7502 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7504 return false;
7507 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7508 by cp_parser_builtin_offsetof. We're looking for
7510 postfix-expression . template [opt] id-expression
7511 postfix-expression . pseudo-destructor-name
7512 postfix-expression -> template [opt] id-expression
7513 postfix-expression -> pseudo-destructor-name
7515 FOR_OFFSETOF is set if we're being called in that context. That sorta
7516 limits what of the above we'll actually accept, but nevermind.
7517 TOKEN_TYPE is the "." or "->" token, which will already have been
7518 removed from the stream. */
7520 static tree
7521 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7522 enum cpp_ttype token_type,
7523 cp_expr postfix_expression,
7524 bool for_offsetof, cp_id_kind *idk,
7525 location_t location)
7527 tree name;
7528 bool dependent_p;
7529 bool pseudo_destructor_p;
7530 tree scope = NULL_TREE;
7531 location_t start_loc = postfix_expression.get_start ();
7533 /* If this is a `->' operator, dereference the pointer. */
7534 if (token_type == CPP_DEREF)
7535 postfix_expression = build_x_arrow (location, postfix_expression,
7536 tf_warning_or_error);
7537 /* Check to see whether or not the expression is type-dependent and
7538 not the current instantiation. */
7539 dependent_p = type_dependent_object_expression_p (postfix_expression);
7540 /* The identifier following the `->' or `.' is not qualified. */
7541 parser->scope = NULL_TREE;
7542 parser->qualifying_scope = NULL_TREE;
7543 parser->object_scope = NULL_TREE;
7544 *idk = CP_ID_KIND_NONE;
7546 /* Enter the scope corresponding to the type of the object
7547 given by the POSTFIX_EXPRESSION. */
7548 if (!dependent_p)
7550 scope = TREE_TYPE (postfix_expression);
7551 /* According to the standard, no expression should ever have
7552 reference type. Unfortunately, we do not currently match
7553 the standard in this respect in that our internal representation
7554 of an expression may have reference type even when the standard
7555 says it does not. Therefore, we have to manually obtain the
7556 underlying type here. */
7557 scope = non_reference (scope);
7558 /* The type of the POSTFIX_EXPRESSION must be complete. */
7559 /* Unlike the object expression in other contexts, *this is not
7560 required to be of complete type for purposes of class member
7561 access (5.2.5) outside the member function body. */
7562 if (postfix_expression != current_class_ref
7563 && scope != error_mark_node
7564 && !currently_open_class (scope))
7566 scope = complete_type (scope);
7567 if (!COMPLETE_TYPE_P (scope)
7568 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7569 &dependent_p))
7570 return error_mark_node;
7573 if (!dependent_p)
7575 /* Let the name lookup machinery know that we are processing a
7576 class member access expression. */
7577 parser->context->object_type = scope;
7578 /* If something went wrong, we want to be able to discern that case,
7579 as opposed to the case where there was no SCOPE due to the type
7580 of expression being dependent. */
7581 if (!scope)
7582 scope = error_mark_node;
7583 /* If the SCOPE was erroneous, make the various semantic analysis
7584 functions exit quickly -- and without issuing additional error
7585 messages. */
7586 if (scope == error_mark_node)
7587 postfix_expression = error_mark_node;
7591 if (dependent_p)
7592 /* Tell cp_parser_lookup_name that there was an object, even though it's
7593 type-dependent. */
7594 parser->context->object_type = unknown_type_node;
7596 /* Assume this expression is not a pseudo-destructor access. */
7597 pseudo_destructor_p = false;
7599 /* If the SCOPE is a scalar type, then, if this is a valid program,
7600 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7601 is type dependent, it can be pseudo-destructor-name or something else.
7602 Try to parse it as pseudo-destructor-name first. */
7603 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7605 tree s;
7606 tree type;
7608 cp_parser_parse_tentatively (parser);
7609 /* Parse the pseudo-destructor-name. */
7610 s = NULL_TREE;
7611 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7612 &s, &type);
7613 if (dependent_p
7614 && (cp_parser_error_occurred (parser)
7615 || !SCALAR_TYPE_P (type)))
7616 cp_parser_abort_tentative_parse (parser);
7617 else if (cp_parser_parse_definitely (parser))
7619 pseudo_destructor_p = true;
7620 postfix_expression
7621 = finish_pseudo_destructor_expr (postfix_expression,
7622 s, type, location);
7626 if (!pseudo_destructor_p)
7628 /* If the SCOPE is not a scalar type, we are looking at an
7629 ordinary class member access expression, rather than a
7630 pseudo-destructor-name. */
7631 bool template_p;
7632 cp_token *token = cp_lexer_peek_token (parser->lexer);
7633 /* Parse the id-expression. */
7634 name = (cp_parser_id_expression
7635 (parser,
7636 cp_parser_optional_template_keyword (parser),
7637 /*check_dependency_p=*/true,
7638 &template_p,
7639 /*declarator_p=*/false,
7640 /*optional_p=*/false));
7641 /* In general, build a SCOPE_REF if the member name is qualified.
7642 However, if the name was not dependent and has already been
7643 resolved; there is no need to build the SCOPE_REF. For example;
7645 struct X { void f(); };
7646 template <typename T> void f(T* t) { t->X::f(); }
7648 Even though "t" is dependent, "X::f" is not and has been resolved
7649 to a BASELINK; there is no need to include scope information. */
7651 /* But we do need to remember that there was an explicit scope for
7652 virtual function calls. */
7653 if (parser->scope)
7654 *idk = CP_ID_KIND_QUALIFIED;
7656 /* If the name is a template-id that names a type, we will get a
7657 TYPE_DECL here. That is invalid code. */
7658 if (TREE_CODE (name) == TYPE_DECL)
7660 error_at (token->location, "invalid use of %qD", name);
7661 postfix_expression = error_mark_node;
7663 else
7665 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7667 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7669 error_at (token->location, "%<%D::%D%> is not a class member",
7670 parser->scope, name);
7671 postfix_expression = error_mark_node;
7673 else
7674 name = build_qualified_name (/*type=*/NULL_TREE,
7675 parser->scope,
7676 name,
7677 template_p);
7678 parser->scope = NULL_TREE;
7679 parser->qualifying_scope = NULL_TREE;
7680 parser->object_scope = NULL_TREE;
7682 if (parser->scope && name && BASELINK_P (name))
7683 adjust_result_of_qualified_name_lookup
7684 (name, parser->scope, scope);
7685 postfix_expression
7686 = finish_class_member_access_expr (postfix_expression, name,
7687 template_p,
7688 tf_warning_or_error);
7689 /* Build a location e.g.:
7690 ptr->access_expr
7691 ~~~^~~~~~~~~~~~~
7692 where the caret is at the deref token, ranging from
7693 the start of postfix_expression to the end of the access expr. */
7694 location_t end_loc
7695 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7696 location_t combined_loc
7697 = make_location (input_location, start_loc, end_loc);
7698 protected_set_expr_location (postfix_expression, combined_loc);
7702 /* We no longer need to look up names in the scope of the object on
7703 the left-hand side of the `.' or `->' operator. */
7704 parser->context->object_type = NULL_TREE;
7706 /* Outside of offsetof, these operators may not appear in
7707 constant-expressions. */
7708 if (!for_offsetof
7709 && (cp_parser_non_integral_constant_expression
7710 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7711 postfix_expression = error_mark_node;
7713 return postfix_expression;
7716 /* Parse a parenthesized expression-list.
7718 expression-list:
7719 assignment-expression
7720 expression-list, assignment-expression
7722 attribute-list:
7723 expression-list
7724 identifier
7725 identifier, expression-list
7727 CAST_P is true if this expression is the target of a cast.
7729 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7730 argument pack.
7732 WRAP_LOCATIONS_P is true if expressions within this list for which
7733 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7734 their source locations.
7736 Returns a vector of trees. Each element is a representation of an
7737 assignment-expression. NULL is returned if the ( and or ) are
7738 missing. An empty, but allocated, vector is returned on no
7739 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7740 if we are parsing an attribute list for an attribute that wants a
7741 plain identifier argument, normal_attr for an attribute that wants
7742 an expression, or non_attr if we aren't parsing an attribute list. If
7743 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7744 not all of the expressions in the list were constant.
7745 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7746 will be written to with the location of the closing parenthesis. If
7747 an error occurs, it may or may not be written to. */
7749 static vec<tree, va_gc> *
7750 cp_parser_parenthesized_expression_list (cp_parser* parser,
7751 int is_attribute_list,
7752 bool cast_p,
7753 bool allow_expansion_p,
7754 bool *non_constant_p,
7755 location_t *close_paren_loc,
7756 bool wrap_locations_p)
7758 vec<tree, va_gc> *expression_list;
7759 bool fold_expr_p = is_attribute_list != non_attr;
7760 tree identifier = NULL_TREE;
7761 bool saved_greater_than_is_operator_p;
7763 /* Assume all the expressions will be constant. */
7764 if (non_constant_p)
7765 *non_constant_p = false;
7767 matching_parens parens;
7768 if (!parens.require_open (parser))
7769 return NULL;
7771 expression_list = make_tree_vector ();
7773 /* Within a parenthesized expression, a `>' token is always
7774 the greater-than operator. */
7775 saved_greater_than_is_operator_p
7776 = parser->greater_than_is_operator_p;
7777 parser->greater_than_is_operator_p = true;
7779 cp_expr expr (NULL_TREE);
7781 /* Consume expressions until there are no more. */
7782 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7783 while (true)
7785 /* At the beginning of attribute lists, check to see if the
7786 next token is an identifier. */
7787 if (is_attribute_list == id_attr
7788 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7790 cp_token *token;
7792 /* Consume the identifier. */
7793 token = cp_lexer_consume_token (parser->lexer);
7794 /* Save the identifier. */
7795 identifier = token->u.value;
7797 else
7799 bool expr_non_constant_p;
7801 /* Parse the next assignment-expression. */
7802 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7804 /* A braced-init-list. */
7805 cp_lexer_set_source_position (parser->lexer);
7806 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7807 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7808 if (non_constant_p && expr_non_constant_p)
7809 *non_constant_p = true;
7811 else if (non_constant_p)
7813 expr = (cp_parser_constant_expression
7814 (parser, /*allow_non_constant_p=*/true,
7815 &expr_non_constant_p));
7816 if (expr_non_constant_p)
7817 *non_constant_p = true;
7819 else
7820 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7821 cast_p);
7823 if (fold_expr_p)
7824 expr = instantiate_non_dependent_expr (expr);
7826 /* If we have an ellipsis, then this is an expression
7827 expansion. */
7828 if (allow_expansion_p
7829 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7831 /* Consume the `...'. */
7832 cp_lexer_consume_token (parser->lexer);
7834 /* Build the argument pack. */
7835 expr = make_pack_expansion (expr);
7838 if (wrap_locations_p)
7839 expr.maybe_add_location_wrapper ();
7841 /* Add it to the list. We add error_mark_node
7842 expressions to the list, so that we can still tell if
7843 the correct form for a parenthesized expression-list
7844 is found. That gives better errors. */
7845 vec_safe_push (expression_list, expr.get_value ());
7847 if (expr == error_mark_node)
7848 goto skip_comma;
7851 /* After the first item, attribute lists look the same as
7852 expression lists. */
7853 is_attribute_list = non_attr;
7855 get_comma:;
7856 /* If the next token isn't a `,', then we are done. */
7857 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7858 break;
7860 /* Otherwise, consume the `,' and keep going. */
7861 cp_lexer_consume_token (parser->lexer);
7864 if (close_paren_loc)
7865 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7867 if (!parens.require_close (parser))
7869 int ending;
7871 skip_comma:;
7872 /* We try and resync to an unnested comma, as that will give the
7873 user better diagnostics. */
7874 ending = cp_parser_skip_to_closing_parenthesis (parser,
7875 /*recovering=*/true,
7876 /*or_comma=*/true,
7877 /*consume_paren=*/true);
7878 if (ending < 0)
7879 goto get_comma;
7880 if (!ending)
7882 parser->greater_than_is_operator_p
7883 = saved_greater_than_is_operator_p;
7884 return NULL;
7888 parser->greater_than_is_operator_p
7889 = saved_greater_than_is_operator_p;
7891 if (identifier)
7892 vec_safe_insert (expression_list, 0, identifier);
7894 return expression_list;
7897 /* Parse a pseudo-destructor-name.
7899 pseudo-destructor-name:
7900 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7901 :: [opt] nested-name-specifier template template-id :: ~ type-name
7902 :: [opt] nested-name-specifier [opt] ~ type-name
7904 If either of the first two productions is used, sets *SCOPE to the
7905 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7906 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7907 or ERROR_MARK_NODE if the parse fails. */
7909 static void
7910 cp_parser_pseudo_destructor_name (cp_parser* parser,
7911 tree object,
7912 tree* scope,
7913 tree* type)
7915 bool nested_name_specifier_p;
7917 /* Handle ~auto. */
7918 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7919 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7920 && !type_dependent_expression_p (object))
7922 if (cxx_dialect < cxx14)
7923 pedwarn (input_location, 0,
7924 "%<~auto%> only available with "
7925 "-std=c++14 or -std=gnu++14");
7926 cp_lexer_consume_token (parser->lexer);
7927 cp_lexer_consume_token (parser->lexer);
7928 *scope = NULL_TREE;
7929 *type = TREE_TYPE (object);
7930 return;
7933 /* Assume that things will not work out. */
7934 *type = error_mark_node;
7936 /* Look for the optional `::' operator. */
7937 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7938 /* Look for the optional nested-name-specifier. */
7939 nested_name_specifier_p
7940 = (cp_parser_nested_name_specifier_opt (parser,
7941 /*typename_keyword_p=*/false,
7942 /*check_dependency_p=*/true,
7943 /*type_p=*/false,
7944 /*is_declaration=*/false)
7945 != NULL_TREE);
7946 /* Now, if we saw a nested-name-specifier, we might be doing the
7947 second production. */
7948 if (nested_name_specifier_p
7949 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7951 /* Consume the `template' keyword. */
7952 cp_lexer_consume_token (parser->lexer);
7953 /* Parse the template-id. */
7954 cp_parser_template_id (parser,
7955 /*template_keyword_p=*/true,
7956 /*check_dependency_p=*/false,
7957 class_type,
7958 /*is_declaration=*/true);
7959 /* Look for the `::' token. */
7960 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7962 /* If the next token is not a `~', then there might be some
7963 additional qualification. */
7964 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7966 /* At this point, we're looking for "type-name :: ~". The type-name
7967 must not be a class-name, since this is a pseudo-destructor. So,
7968 it must be either an enum-name, or a typedef-name -- both of which
7969 are just identifiers. So, we peek ahead to check that the "::"
7970 and "~" tokens are present; if they are not, then we can avoid
7971 calling type_name. */
7972 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7973 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7974 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7976 cp_parser_error (parser, "non-scalar type");
7977 return;
7980 /* Look for the type-name. */
7981 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7982 if (*scope == error_mark_node)
7983 return;
7985 /* Look for the `::' token. */
7986 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7988 else
7989 *scope = NULL_TREE;
7991 /* Look for the `~'. */
7992 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7994 /* Once we see the ~, this has to be a pseudo-destructor. */
7995 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7996 cp_parser_commit_to_topmost_tentative_parse (parser);
7998 /* Look for the type-name again. We are not responsible for
7999 checking that it matches the first type-name. */
8000 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8003 /* Parse a unary-expression.
8005 unary-expression:
8006 postfix-expression
8007 ++ cast-expression
8008 -- cast-expression
8009 unary-operator cast-expression
8010 sizeof unary-expression
8011 sizeof ( type-id )
8012 alignof ( type-id ) [C++0x]
8013 new-expression
8014 delete-expression
8016 GNU Extensions:
8018 unary-expression:
8019 __extension__ cast-expression
8020 __alignof__ unary-expression
8021 __alignof__ ( type-id )
8022 alignof unary-expression [C++0x]
8023 __real__ cast-expression
8024 __imag__ cast-expression
8025 && identifier
8026 sizeof ( type-id ) { initializer-list , [opt] }
8027 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8028 __alignof__ ( type-id ) { initializer-list , [opt] }
8030 ADDRESS_P is true iff the unary-expression is appearing as the
8031 operand of the `&' operator. CAST_P is true if this expression is
8032 the target of a cast.
8034 Returns a representation of the expression. */
8036 static cp_expr
8037 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8038 bool address_p, bool cast_p, bool decltype_p)
8040 cp_token *token;
8041 enum tree_code unary_operator;
8043 /* Peek at the next token. */
8044 token = cp_lexer_peek_token (parser->lexer);
8045 /* Some keywords give away the kind of expression. */
8046 if (token->type == CPP_KEYWORD)
8048 enum rid keyword = token->keyword;
8050 switch (keyword)
8052 case RID_ALIGNOF:
8053 case RID_SIZEOF:
8055 tree operand, ret;
8056 enum tree_code op;
8057 location_t start_loc = token->location;
8059 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8060 bool std_alignof = id_equal (token->u.value, "alignof");
8062 /* Consume the token. */
8063 cp_lexer_consume_token (parser->lexer);
8064 /* Parse the operand. */
8065 operand = cp_parser_sizeof_operand (parser, keyword);
8067 if (TYPE_P (operand))
8068 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8069 true);
8070 else
8072 /* ISO C++ defines alignof only with types, not with
8073 expressions. So pedwarn if alignof is used with a non-
8074 type expression. However, __alignof__ is ok. */
8075 if (std_alignof)
8076 pedwarn (token->location, OPT_Wpedantic,
8077 "ISO C++ does not allow %<alignof%> "
8078 "with a non-type");
8080 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8082 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8083 SIZEOF_EXPR with the original operand. */
8084 if (op == SIZEOF_EXPR && ret != error_mark_node)
8086 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8088 if (!processing_template_decl && TYPE_P (operand))
8090 ret = build_min (SIZEOF_EXPR, size_type_node,
8091 build1 (NOP_EXPR, operand,
8092 error_mark_node));
8093 SIZEOF_EXPR_TYPE_P (ret) = 1;
8095 else
8096 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8097 TREE_SIDE_EFFECTS (ret) = 0;
8098 TREE_READONLY (ret) = 1;
8102 /* Construct a location e.g. :
8103 alignof (expr)
8104 ^~~~~~~~~~~~~~
8105 with start == caret at the start of the "alignof"/"sizeof"
8106 token, with the endpoint at the final closing paren. */
8107 location_t finish_loc
8108 = cp_lexer_previous_token (parser->lexer)->location;
8109 location_t compound_loc
8110 = make_location (start_loc, start_loc, finish_loc);
8112 cp_expr ret_expr (ret);
8113 ret_expr.set_location (compound_loc);
8114 ret_expr = ret_expr.maybe_add_location_wrapper ();
8115 return ret_expr;
8118 case RID_NEW:
8119 return cp_parser_new_expression (parser);
8121 case RID_DELETE:
8122 return cp_parser_delete_expression (parser);
8124 case RID_EXTENSION:
8126 /* The saved value of the PEDANTIC flag. */
8127 int saved_pedantic;
8128 tree expr;
8130 /* Save away the PEDANTIC flag. */
8131 cp_parser_extension_opt (parser, &saved_pedantic);
8132 /* Parse the cast-expression. */
8133 expr = cp_parser_simple_cast_expression (parser);
8134 /* Restore the PEDANTIC flag. */
8135 pedantic = saved_pedantic;
8137 return expr;
8140 case RID_REALPART:
8141 case RID_IMAGPART:
8143 tree expression;
8145 /* Consume the `__real__' or `__imag__' token. */
8146 cp_lexer_consume_token (parser->lexer);
8147 /* Parse the cast-expression. */
8148 expression = cp_parser_simple_cast_expression (parser);
8149 /* Create the complete representation. */
8150 return build_x_unary_op (token->location,
8151 (keyword == RID_REALPART
8152 ? REALPART_EXPR : IMAGPART_EXPR),
8153 expression,
8154 tf_warning_or_error);
8156 break;
8158 case RID_TRANSACTION_ATOMIC:
8159 case RID_TRANSACTION_RELAXED:
8160 return cp_parser_transaction_expression (parser, keyword);
8162 case RID_NOEXCEPT:
8164 tree expr;
8165 const char *saved_message;
8166 bool saved_integral_constant_expression_p;
8167 bool saved_non_integral_constant_expression_p;
8168 bool saved_greater_than_is_operator_p;
8170 location_t start_loc = token->location;
8172 cp_lexer_consume_token (parser->lexer);
8173 matching_parens parens;
8174 parens.require_open (parser);
8176 saved_message = parser->type_definition_forbidden_message;
8177 parser->type_definition_forbidden_message
8178 = G_("types may not be defined in %<noexcept%> expressions");
8180 saved_integral_constant_expression_p
8181 = parser->integral_constant_expression_p;
8182 saved_non_integral_constant_expression_p
8183 = parser->non_integral_constant_expression_p;
8184 parser->integral_constant_expression_p = false;
8186 saved_greater_than_is_operator_p
8187 = parser->greater_than_is_operator_p;
8188 parser->greater_than_is_operator_p = true;
8190 ++cp_unevaluated_operand;
8191 ++c_inhibit_evaluation_warnings;
8192 ++cp_noexcept_operand;
8193 expr = cp_parser_expression (parser);
8194 --cp_noexcept_operand;
8195 --c_inhibit_evaluation_warnings;
8196 --cp_unevaluated_operand;
8198 parser->greater_than_is_operator_p
8199 = saved_greater_than_is_operator_p;
8201 parser->integral_constant_expression_p
8202 = saved_integral_constant_expression_p;
8203 parser->non_integral_constant_expression_p
8204 = saved_non_integral_constant_expression_p;
8206 parser->type_definition_forbidden_message = saved_message;
8208 location_t finish_loc
8209 = cp_lexer_peek_token (parser->lexer)->location;
8210 parens.require_close (parser);
8212 /* Construct a location of the form:
8213 noexcept (expr)
8214 ^~~~~~~~~~~~~~~
8215 with start == caret, finishing at the close-paren. */
8216 location_t noexcept_loc
8217 = make_location (start_loc, start_loc, finish_loc);
8219 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8220 noexcept_loc);
8223 default:
8224 break;
8228 /* Look for the `:: new' and `:: delete', which also signal the
8229 beginning of a new-expression, or delete-expression,
8230 respectively. If the next token is `::', then it might be one of
8231 these. */
8232 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8234 enum rid keyword;
8236 /* See if the token after the `::' is one of the keywords in
8237 which we're interested. */
8238 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8239 /* If it's `new', we have a new-expression. */
8240 if (keyword == RID_NEW)
8241 return cp_parser_new_expression (parser);
8242 /* Similarly, for `delete'. */
8243 else if (keyword == RID_DELETE)
8244 return cp_parser_delete_expression (parser);
8247 /* Look for a unary operator. */
8248 unary_operator = cp_parser_unary_operator (token);
8249 /* The `++' and `--' operators can be handled similarly, even though
8250 they are not technically unary-operators in the grammar. */
8251 if (unary_operator == ERROR_MARK)
8253 if (token->type == CPP_PLUS_PLUS)
8254 unary_operator = PREINCREMENT_EXPR;
8255 else if (token->type == CPP_MINUS_MINUS)
8256 unary_operator = PREDECREMENT_EXPR;
8257 /* Handle the GNU address-of-label extension. */
8258 else if (cp_parser_allow_gnu_extensions_p (parser)
8259 && token->type == CPP_AND_AND)
8261 tree identifier;
8262 tree expression;
8263 location_t start_loc = token->location;
8265 /* Consume the '&&' token. */
8266 cp_lexer_consume_token (parser->lexer);
8267 /* Look for the identifier. */
8268 location_t finish_loc
8269 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8270 identifier = cp_parser_identifier (parser);
8271 /* Construct a location of the form:
8272 &&label
8273 ^~~~~~~
8274 with caret==start at the "&&", finish at the end of the label. */
8275 location_t combined_loc
8276 = make_location (start_loc, start_loc, finish_loc);
8277 /* Create an expression representing the address. */
8278 expression = finish_label_address_expr (identifier, combined_loc);
8279 if (cp_parser_non_integral_constant_expression (parser,
8280 NIC_ADDR_LABEL))
8281 expression = error_mark_node;
8282 return expression;
8285 if (unary_operator != ERROR_MARK)
8287 cp_expr cast_expression;
8288 cp_expr expression = error_mark_node;
8289 non_integral_constant non_constant_p = NIC_NONE;
8290 location_t loc = token->location;
8291 tsubst_flags_t complain = complain_flags (decltype_p);
8293 /* Consume the operator token. */
8294 token = cp_lexer_consume_token (parser->lexer);
8295 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8297 /* Parse the cast-expression. */
8298 cast_expression
8299 = cp_parser_cast_expression (parser,
8300 unary_operator == ADDR_EXPR,
8301 /*cast_p=*/false,
8302 /*decltype*/false,
8303 pidk);
8305 /* Make a location:
8306 OP_TOKEN CAST_EXPRESSION
8307 ^~~~~~~~~~~~~~~~~~~~~~~~~
8308 with start==caret at the operator token, and
8309 extending to the end of the cast_expression. */
8310 loc = make_location (loc, loc, cast_expression.get_finish ());
8312 /* Now, build an appropriate representation. */
8313 switch (unary_operator)
8315 case INDIRECT_REF:
8316 non_constant_p = NIC_STAR;
8317 expression = build_x_indirect_ref (loc, cast_expression,
8318 RO_UNARY_STAR,
8319 complain);
8320 /* TODO: build_x_indirect_ref does not always honor the
8321 location, so ensure it is set. */
8322 expression.set_location (loc);
8323 break;
8325 case ADDR_EXPR:
8326 non_constant_p = NIC_ADDR;
8327 /* Fall through. */
8328 case BIT_NOT_EXPR:
8329 expression = build_x_unary_op (loc, unary_operator,
8330 cast_expression,
8331 complain);
8332 /* TODO: build_x_unary_op does not always honor the location,
8333 so ensure it is set. */
8334 expression.set_location (loc);
8335 break;
8337 case PREINCREMENT_EXPR:
8338 case PREDECREMENT_EXPR:
8339 non_constant_p = unary_operator == PREINCREMENT_EXPR
8340 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8341 /* Fall through. */
8342 case NEGATE_EXPR:
8343 /* Immediately fold negation of a constant, unless the constant is 0
8344 (since -0 == 0) or it would overflow. */
8345 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8346 && CONSTANT_CLASS_P (cast_expression)
8347 && !integer_zerop (cast_expression)
8348 && !TREE_OVERFLOW (cast_expression))
8350 tree folded = fold_build1 (unary_operator,
8351 TREE_TYPE (cast_expression),
8352 cast_expression);
8353 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8355 expression = cp_expr (folded, loc);
8356 break;
8359 /* Fall through. */
8360 case UNARY_PLUS_EXPR:
8361 case TRUTH_NOT_EXPR:
8362 expression = finish_unary_op_expr (loc, unary_operator,
8363 cast_expression, complain);
8364 break;
8366 default:
8367 gcc_unreachable ();
8370 if (non_constant_p != NIC_NONE
8371 && cp_parser_non_integral_constant_expression (parser,
8372 non_constant_p))
8373 expression = error_mark_node;
8375 return expression;
8378 return cp_parser_postfix_expression (parser, address_p, cast_p,
8379 /*member_access_only_p=*/false,
8380 decltype_p,
8381 pidk);
8384 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8385 unary-operator, the corresponding tree code is returned. */
8387 static enum tree_code
8388 cp_parser_unary_operator (cp_token* token)
8390 switch (token->type)
8392 case CPP_MULT:
8393 return INDIRECT_REF;
8395 case CPP_AND:
8396 return ADDR_EXPR;
8398 case CPP_PLUS:
8399 return UNARY_PLUS_EXPR;
8401 case CPP_MINUS:
8402 return NEGATE_EXPR;
8404 case CPP_NOT:
8405 return TRUTH_NOT_EXPR;
8407 case CPP_COMPL:
8408 return BIT_NOT_EXPR;
8410 default:
8411 return ERROR_MARK;
8415 /* Parse a new-expression.
8417 new-expression:
8418 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8419 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8421 Returns a representation of the expression. */
8423 static tree
8424 cp_parser_new_expression (cp_parser* parser)
8426 bool global_scope_p;
8427 vec<tree, va_gc> *placement;
8428 tree type;
8429 vec<tree, va_gc> *initializer;
8430 tree nelts = NULL_TREE;
8431 tree ret;
8433 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8435 /* Look for the optional `::' operator. */
8436 global_scope_p
8437 = (cp_parser_global_scope_opt (parser,
8438 /*current_scope_valid_p=*/false)
8439 != NULL_TREE);
8440 /* Look for the `new' operator. */
8441 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8442 /* There's no easy way to tell a new-placement from the
8443 `( type-id )' construct. */
8444 cp_parser_parse_tentatively (parser);
8445 /* Look for a new-placement. */
8446 placement = cp_parser_new_placement (parser);
8447 /* If that didn't work out, there's no new-placement. */
8448 if (!cp_parser_parse_definitely (parser))
8450 if (placement != NULL)
8451 release_tree_vector (placement);
8452 placement = NULL;
8455 /* If the next token is a `(', then we have a parenthesized
8456 type-id. */
8457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8459 cp_token *token;
8460 const char *saved_message = parser->type_definition_forbidden_message;
8462 /* Consume the `('. */
8463 matching_parens parens;
8464 parens.consume_open (parser);
8466 /* Parse the type-id. */
8467 parser->type_definition_forbidden_message
8468 = G_("types may not be defined in a new-expression");
8470 type_id_in_expr_sentinel s (parser);
8471 type = cp_parser_type_id (parser);
8473 parser->type_definition_forbidden_message = saved_message;
8475 /* Look for the closing `)'. */
8476 parens.require_close (parser);
8477 token = cp_lexer_peek_token (parser->lexer);
8478 /* There should not be a direct-new-declarator in this production,
8479 but GCC used to allowed this, so we check and emit a sensible error
8480 message for this case. */
8481 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8483 error_at (token->location,
8484 "array bound forbidden after parenthesized type-id");
8485 inform (token->location,
8486 "try removing the parentheses around the type-id");
8487 cp_parser_direct_new_declarator (parser);
8490 /* Otherwise, there must be a new-type-id. */
8491 else
8492 type = cp_parser_new_type_id (parser, &nelts);
8494 /* If the next token is a `(' or '{', then we have a new-initializer. */
8495 cp_token *token = cp_lexer_peek_token (parser->lexer);
8496 if (token->type == CPP_OPEN_PAREN
8497 || token->type == CPP_OPEN_BRACE)
8498 initializer = cp_parser_new_initializer (parser);
8499 else
8500 initializer = NULL;
8502 /* A new-expression may not appear in an integral constant
8503 expression. */
8504 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8505 ret = error_mark_node;
8506 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8507 of a new-type-id or type-id of a new-expression, the new-expression shall
8508 contain a new-initializer of the form ( assignment-expression )".
8509 Additionally, consistently with the spirit of DR 1467, we want to accept
8510 'new auto { 2 }' too. */
8511 else if ((ret = type_uses_auto (type))
8512 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8513 && (vec_safe_length (initializer) != 1
8514 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8515 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8517 error_at (token->location,
8518 "initialization of new-expression for type %<auto%> "
8519 "requires exactly one element");
8520 ret = error_mark_node;
8522 else
8524 /* Construct a location e.g.:
8525 ptr = new int[100]
8526 ^~~~~~~~~~~~
8527 with caret == start at the start of the "new" token, and the end
8528 at the end of the final token we consumed. */
8529 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8530 location_t end_loc = get_finish (end_tok->location);
8531 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8533 /* Create a representation of the new-expression. */
8534 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8535 tf_warning_or_error);
8536 protected_set_expr_location (ret, combined_loc);
8539 if (placement != NULL)
8540 release_tree_vector (placement);
8541 if (initializer != NULL)
8542 release_tree_vector (initializer);
8544 return ret;
8547 /* Parse a new-placement.
8549 new-placement:
8550 ( expression-list )
8552 Returns the same representation as for an expression-list. */
8554 static vec<tree, va_gc> *
8555 cp_parser_new_placement (cp_parser* parser)
8557 vec<tree, va_gc> *expression_list;
8559 /* Parse the expression-list. */
8560 expression_list = (cp_parser_parenthesized_expression_list
8561 (parser, non_attr, /*cast_p=*/false,
8562 /*allow_expansion_p=*/true,
8563 /*non_constant_p=*/NULL));
8565 if (expression_list && expression_list->is_empty ())
8566 error ("expected expression-list or type-id");
8568 return expression_list;
8571 /* Parse a new-type-id.
8573 new-type-id:
8574 type-specifier-seq new-declarator [opt]
8576 Returns the TYPE allocated. If the new-type-id indicates an array
8577 type, *NELTS is set to the number of elements in the last array
8578 bound; the TYPE will not include the last array bound. */
8580 static tree
8581 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8583 cp_decl_specifier_seq type_specifier_seq;
8584 cp_declarator *new_declarator;
8585 cp_declarator *declarator;
8586 cp_declarator *outer_declarator;
8587 const char *saved_message;
8589 /* The type-specifier sequence must not contain type definitions.
8590 (It cannot contain declarations of new types either, but if they
8591 are not definitions we will catch that because they are not
8592 complete.) */
8593 saved_message = parser->type_definition_forbidden_message;
8594 parser->type_definition_forbidden_message
8595 = G_("types may not be defined in a new-type-id");
8596 /* Parse the type-specifier-seq. */
8597 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8598 /*is_trailing_return=*/false,
8599 &type_specifier_seq);
8600 /* Restore the old message. */
8601 parser->type_definition_forbidden_message = saved_message;
8603 if (type_specifier_seq.type == error_mark_node)
8604 return error_mark_node;
8606 /* Parse the new-declarator. */
8607 new_declarator = cp_parser_new_declarator_opt (parser);
8609 /* Determine the number of elements in the last array dimension, if
8610 any. */
8611 *nelts = NULL_TREE;
8612 /* Skip down to the last array dimension. */
8613 declarator = new_declarator;
8614 outer_declarator = NULL;
8615 while (declarator && (declarator->kind == cdk_pointer
8616 || declarator->kind == cdk_ptrmem))
8618 outer_declarator = declarator;
8619 declarator = declarator->declarator;
8621 while (declarator
8622 && declarator->kind == cdk_array
8623 && declarator->declarator
8624 && declarator->declarator->kind == cdk_array)
8626 outer_declarator = declarator;
8627 declarator = declarator->declarator;
8630 if (declarator && declarator->kind == cdk_array)
8632 *nelts = declarator->u.array.bounds;
8633 if (*nelts == error_mark_node)
8634 *nelts = integer_one_node;
8636 if (outer_declarator)
8637 outer_declarator->declarator = declarator->declarator;
8638 else
8639 new_declarator = NULL;
8642 return groktypename (&type_specifier_seq, new_declarator, false);
8645 /* Parse an (optional) new-declarator.
8647 new-declarator:
8648 ptr-operator new-declarator [opt]
8649 direct-new-declarator
8651 Returns the declarator. */
8653 static cp_declarator *
8654 cp_parser_new_declarator_opt (cp_parser* parser)
8656 enum tree_code code;
8657 tree type, std_attributes = NULL_TREE;
8658 cp_cv_quals cv_quals;
8660 /* We don't know if there's a ptr-operator next, or not. */
8661 cp_parser_parse_tentatively (parser);
8662 /* Look for a ptr-operator. */
8663 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8664 /* If that worked, look for more new-declarators. */
8665 if (cp_parser_parse_definitely (parser))
8667 cp_declarator *declarator;
8669 /* Parse another optional declarator. */
8670 declarator = cp_parser_new_declarator_opt (parser);
8672 declarator = cp_parser_make_indirect_declarator
8673 (code, type, cv_quals, declarator, std_attributes);
8675 return declarator;
8678 /* If the next token is a `[', there is a direct-new-declarator. */
8679 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8680 return cp_parser_direct_new_declarator (parser);
8682 return NULL;
8685 /* Parse a direct-new-declarator.
8687 direct-new-declarator:
8688 [ expression ]
8689 direct-new-declarator [constant-expression]
8693 static cp_declarator *
8694 cp_parser_direct_new_declarator (cp_parser* parser)
8696 cp_declarator *declarator = NULL;
8698 while (true)
8700 tree expression;
8701 cp_token *token;
8703 /* Look for the opening `['. */
8704 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8706 token = cp_lexer_peek_token (parser->lexer);
8707 expression = cp_parser_expression (parser);
8708 /* The standard requires that the expression have integral
8709 type. DR 74 adds enumeration types. We believe that the
8710 real intent is that these expressions be handled like the
8711 expression in a `switch' condition, which also allows
8712 classes with a single conversion to integral or
8713 enumeration type. */
8714 if (!processing_template_decl)
8716 expression
8717 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8718 expression,
8719 /*complain=*/true);
8720 if (!expression)
8722 error_at (token->location,
8723 "expression in new-declarator must have integral "
8724 "or enumeration type");
8725 expression = error_mark_node;
8729 /* Look for the closing `]'. */
8730 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8732 /* Add this bound to the declarator. */
8733 declarator = make_array_declarator (declarator, expression);
8735 /* If the next token is not a `[', then there are no more
8736 bounds. */
8737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8738 break;
8741 return declarator;
8744 /* Parse a new-initializer.
8746 new-initializer:
8747 ( expression-list [opt] )
8748 braced-init-list
8750 Returns a representation of the expression-list. */
8752 static vec<tree, va_gc> *
8753 cp_parser_new_initializer (cp_parser* parser)
8755 vec<tree, va_gc> *expression_list;
8757 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8759 tree t;
8760 bool expr_non_constant_p;
8761 cp_lexer_set_source_position (parser->lexer);
8762 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8763 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8764 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8765 expression_list = make_tree_vector_single (t);
8767 else
8768 expression_list = (cp_parser_parenthesized_expression_list
8769 (parser, non_attr, /*cast_p=*/false,
8770 /*allow_expansion_p=*/true,
8771 /*non_constant_p=*/NULL));
8773 return expression_list;
8776 /* Parse a delete-expression.
8778 delete-expression:
8779 :: [opt] delete cast-expression
8780 :: [opt] delete [ ] cast-expression
8782 Returns a representation of the expression. */
8784 static tree
8785 cp_parser_delete_expression (cp_parser* parser)
8787 bool global_scope_p;
8788 bool array_p;
8789 tree expression;
8791 /* Look for the optional `::' operator. */
8792 global_scope_p
8793 = (cp_parser_global_scope_opt (parser,
8794 /*current_scope_valid_p=*/false)
8795 != NULL_TREE);
8796 /* Look for the `delete' keyword. */
8797 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8798 /* See if the array syntax is in use. */
8799 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8801 /* Consume the `[' token. */
8802 cp_lexer_consume_token (parser->lexer);
8803 /* Look for the `]' token. */
8804 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8805 /* Remember that this is the `[]' construct. */
8806 array_p = true;
8808 else
8809 array_p = false;
8811 /* Parse the cast-expression. */
8812 expression = cp_parser_simple_cast_expression (parser);
8814 /* A delete-expression may not appear in an integral constant
8815 expression. */
8816 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8817 return error_mark_node;
8819 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8820 tf_warning_or_error);
8823 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8824 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8825 0 otherwise. */
8827 static int
8828 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8830 cp_token *token = cp_lexer_peek_token (parser->lexer);
8831 switch (token->type)
8833 case CPP_COMMA:
8834 case CPP_SEMICOLON:
8835 case CPP_QUERY:
8836 case CPP_COLON:
8837 case CPP_CLOSE_SQUARE:
8838 case CPP_CLOSE_PAREN:
8839 case CPP_CLOSE_BRACE:
8840 case CPP_OPEN_BRACE:
8841 case CPP_DOT:
8842 case CPP_DOT_STAR:
8843 case CPP_DEREF:
8844 case CPP_DEREF_STAR:
8845 case CPP_DIV:
8846 case CPP_MOD:
8847 case CPP_LSHIFT:
8848 case CPP_RSHIFT:
8849 case CPP_LESS:
8850 case CPP_GREATER:
8851 case CPP_LESS_EQ:
8852 case CPP_GREATER_EQ:
8853 case CPP_EQ_EQ:
8854 case CPP_NOT_EQ:
8855 case CPP_EQ:
8856 case CPP_MULT_EQ:
8857 case CPP_DIV_EQ:
8858 case CPP_MOD_EQ:
8859 case CPP_PLUS_EQ:
8860 case CPP_MINUS_EQ:
8861 case CPP_RSHIFT_EQ:
8862 case CPP_LSHIFT_EQ:
8863 case CPP_AND_EQ:
8864 case CPP_XOR_EQ:
8865 case CPP_OR_EQ:
8866 case CPP_XOR:
8867 case CPP_OR:
8868 case CPP_OR_OR:
8869 case CPP_EOF:
8870 case CPP_ELLIPSIS:
8871 return 0;
8873 case CPP_OPEN_PAREN:
8874 /* In ((type ()) () the last () isn't a valid cast-expression,
8875 so the whole must be parsed as postfix-expression. */
8876 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8877 != CPP_CLOSE_PAREN;
8879 case CPP_OPEN_SQUARE:
8880 /* '[' may start a primary-expression in obj-c++ and in C++11,
8881 as a lambda-expression, eg, '(void)[]{}'. */
8882 if (cxx_dialect >= cxx11)
8883 return -1;
8884 return c_dialect_objc ();
8886 case CPP_PLUS_PLUS:
8887 case CPP_MINUS_MINUS:
8888 /* '++' and '--' may or may not start a cast-expression:
8890 struct T { void operator++(int); };
8891 void f() { (T())++; }
8895 int a;
8896 (int)++a; */
8897 return -1;
8899 default:
8900 return 1;
8904 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8905 in the order: const_cast, static_cast, reinterpret_cast.
8907 Don't suggest dynamic_cast.
8909 Return the first legal cast kind found, or NULL otherwise. */
8911 static const char *
8912 get_cast_suggestion (tree dst_type, tree orig_expr)
8914 tree trial;
8916 /* Reuse the parser logic by attempting to build the various kinds of
8917 cast, with "complain" disabled.
8918 Identify the first such cast that is valid. */
8920 /* Don't attempt to run such logic within template processing. */
8921 if (processing_template_decl)
8922 return NULL;
8924 /* First try const_cast. */
8925 trial = build_const_cast (dst_type, orig_expr, tf_none);
8926 if (trial != error_mark_node)
8927 return "const_cast";
8929 /* If that fails, try static_cast. */
8930 trial = build_static_cast (dst_type, orig_expr, tf_none);
8931 if (trial != error_mark_node)
8932 return "static_cast";
8934 /* Finally, try reinterpret_cast. */
8935 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8936 if (trial != error_mark_node)
8937 return "reinterpret_cast";
8939 /* No such cast possible. */
8940 return NULL;
8943 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8944 suggesting how to convert a C-style cast of the form:
8946 (DST_TYPE)ORIG_EXPR
8948 to a C++-style cast.
8950 The primary range of RICHLOC is asssumed to be that of the original
8951 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8952 of the parens in the C-style cast. */
8954 static void
8955 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8956 location_t close_paren_loc, tree orig_expr,
8957 tree dst_type)
8959 /* This function is non-trivial, so bail out now if the warning isn't
8960 going to be emitted. */
8961 if (!warn_old_style_cast)
8962 return;
8964 /* Try to find a legal C++ cast, trying them in order:
8965 const_cast, static_cast, reinterpret_cast. */
8966 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8967 if (!cast_suggestion)
8968 return;
8970 /* Replace the open paren with "CAST_SUGGESTION<". */
8971 pretty_printer pp;
8972 pp_printf (&pp, "%s<", cast_suggestion);
8973 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8975 /* Replace the close paren with "> (". */
8976 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8978 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8979 rich_loc->add_fixit_insert_after (")");
8983 /* Parse a cast-expression.
8985 cast-expression:
8986 unary-expression
8987 ( type-id ) cast-expression
8989 ADDRESS_P is true iff the unary-expression is appearing as the
8990 operand of the `&' operator. CAST_P is true if this expression is
8991 the target of a cast.
8993 Returns a representation of the expression. */
8995 static cp_expr
8996 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8997 bool decltype_p, cp_id_kind * pidk)
8999 /* If it's a `(', then we might be looking at a cast. */
9000 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9002 tree type = NULL_TREE;
9003 cp_expr expr (NULL_TREE);
9004 int cast_expression = 0;
9005 const char *saved_message;
9007 /* There's no way to know yet whether or not this is a cast.
9008 For example, `(int (3))' is a unary-expression, while `(int)
9009 3' is a cast. So, we resort to parsing tentatively. */
9010 cp_parser_parse_tentatively (parser);
9011 /* Types may not be defined in a cast. */
9012 saved_message = parser->type_definition_forbidden_message;
9013 parser->type_definition_forbidden_message
9014 = G_("types may not be defined in casts");
9015 /* Consume the `('. */
9016 matching_parens parens;
9017 cp_token *open_paren = parens.consume_open (parser);
9018 location_t open_paren_loc = open_paren->location;
9019 location_t close_paren_loc = UNKNOWN_LOCATION;
9021 /* A very tricky bit is that `(struct S) { 3 }' is a
9022 compound-literal (which we permit in C++ as an extension).
9023 But, that construct is not a cast-expression -- it is a
9024 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9025 is legal; if the compound-literal were a cast-expression,
9026 you'd need an extra set of parentheses.) But, if we parse
9027 the type-id, and it happens to be a class-specifier, then we
9028 will commit to the parse at that point, because we cannot
9029 undo the action that is done when creating a new class. So,
9030 then we cannot back up and do a postfix-expression.
9032 Another tricky case is the following (c++/29234):
9034 struct S { void operator () (); };
9036 void foo ()
9038 ( S()() );
9041 As a type-id we parse the parenthesized S()() as a function
9042 returning a function, groktypename complains and we cannot
9043 back up in this case either.
9045 Therefore, we scan ahead to the closing `)', and check to see
9046 if the tokens after the `)' can start a cast-expression. Otherwise
9047 we are dealing with an unary-expression, a postfix-expression
9048 or something else.
9050 Yet another tricky case, in C++11, is the following (c++/54891):
9052 (void)[]{};
9054 The issue is that usually, besides the case of lambda-expressions,
9055 the parenthesized type-id cannot be followed by '[', and, eg, we
9056 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9057 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9058 we don't commit, we try a cast-expression, then an unary-expression.
9060 Save tokens so that we can put them back. */
9061 cp_lexer_save_tokens (parser->lexer);
9063 /* We may be looking at a cast-expression. */
9064 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9065 /*consume_paren=*/true))
9066 cast_expression
9067 = cp_parser_tokens_start_cast_expression (parser);
9069 /* Roll back the tokens we skipped. */
9070 cp_lexer_rollback_tokens (parser->lexer);
9071 /* If we aren't looking at a cast-expression, simulate an error so
9072 that the call to cp_parser_error_occurred below returns true. */
9073 if (!cast_expression)
9074 cp_parser_simulate_error (parser);
9075 else
9077 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9078 parser->in_type_id_in_expr_p = true;
9079 /* Look for the type-id. */
9080 type = cp_parser_type_id (parser);
9081 /* Look for the closing `)'. */
9082 cp_token *close_paren = parens.require_close (parser);
9083 if (close_paren)
9084 close_paren_loc = close_paren->location;
9085 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9088 /* Restore the saved message. */
9089 parser->type_definition_forbidden_message = saved_message;
9091 /* At this point this can only be either a cast or a
9092 parenthesized ctor such as `(T ())' that looks like a cast to
9093 function returning T. */
9094 if (!cp_parser_error_occurred (parser))
9096 /* Only commit if the cast-expression doesn't start with
9097 '++', '--', or '[' in C++11. */
9098 if (cast_expression > 0)
9099 cp_parser_commit_to_topmost_tentative_parse (parser);
9101 expr = cp_parser_cast_expression (parser,
9102 /*address_p=*/false,
9103 /*cast_p=*/true,
9104 /*decltype_p=*/false,
9105 pidk);
9107 if (cp_parser_parse_definitely (parser))
9109 /* Warn about old-style casts, if so requested. */
9110 if (warn_old_style_cast
9111 && !in_system_header_at (input_location)
9112 && !VOID_TYPE_P (type)
9113 && current_lang_name != lang_name_c)
9115 gcc_rich_location rich_loc (input_location);
9116 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9117 expr, type);
9118 warning_at (&rich_loc, OPT_Wold_style_cast,
9119 "use of old-style cast to %q#T", type);
9122 /* Only type conversions to integral or enumeration types
9123 can be used in constant-expressions. */
9124 if (!cast_valid_in_integral_constant_expression_p (type)
9125 && cp_parser_non_integral_constant_expression (parser,
9126 NIC_CAST))
9127 return error_mark_node;
9129 /* Perform the cast. */
9130 /* Make a location:
9131 (TYPE) EXPR
9132 ^~~~~~~~~~~
9133 with start==caret at the open paren, extending to the
9134 end of "expr". */
9135 location_t cast_loc = make_location (open_paren_loc,
9136 open_paren_loc,
9137 expr.get_finish ());
9138 expr = build_c_cast (cast_loc, type, expr);
9139 return expr;
9142 else
9143 cp_parser_abort_tentative_parse (parser);
9146 /* If we get here, then it's not a cast, so it must be a
9147 unary-expression. */
9148 return cp_parser_unary_expression (parser, pidk, address_p,
9149 cast_p, decltype_p);
9152 /* Parse a binary expression of the general form:
9154 pm-expression:
9155 cast-expression
9156 pm-expression .* cast-expression
9157 pm-expression ->* cast-expression
9159 multiplicative-expression:
9160 pm-expression
9161 multiplicative-expression * pm-expression
9162 multiplicative-expression / pm-expression
9163 multiplicative-expression % pm-expression
9165 additive-expression:
9166 multiplicative-expression
9167 additive-expression + multiplicative-expression
9168 additive-expression - multiplicative-expression
9170 shift-expression:
9171 additive-expression
9172 shift-expression << additive-expression
9173 shift-expression >> additive-expression
9175 relational-expression:
9176 shift-expression
9177 relational-expression < shift-expression
9178 relational-expression > shift-expression
9179 relational-expression <= shift-expression
9180 relational-expression >= shift-expression
9182 GNU Extension:
9184 relational-expression:
9185 relational-expression <? shift-expression
9186 relational-expression >? shift-expression
9188 equality-expression:
9189 relational-expression
9190 equality-expression == relational-expression
9191 equality-expression != relational-expression
9193 and-expression:
9194 equality-expression
9195 and-expression & equality-expression
9197 exclusive-or-expression:
9198 and-expression
9199 exclusive-or-expression ^ and-expression
9201 inclusive-or-expression:
9202 exclusive-or-expression
9203 inclusive-or-expression | exclusive-or-expression
9205 logical-and-expression:
9206 inclusive-or-expression
9207 logical-and-expression && inclusive-or-expression
9209 logical-or-expression:
9210 logical-and-expression
9211 logical-or-expression || logical-and-expression
9213 All these are implemented with a single function like:
9215 binary-expression:
9216 simple-cast-expression
9217 binary-expression <token> binary-expression
9219 CAST_P is true if this expression is the target of a cast.
9221 The binops_by_token map is used to get the tree codes for each <token> type.
9222 binary-expressions are associated according to a precedence table. */
9224 #define TOKEN_PRECEDENCE(token) \
9225 (((token->type == CPP_GREATER \
9226 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9227 && !parser->greater_than_is_operator_p) \
9228 ? PREC_NOT_OPERATOR \
9229 : binops_by_token[token->type].prec)
9231 static cp_expr
9232 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9233 bool no_toplevel_fold_p,
9234 bool decltype_p,
9235 enum cp_parser_prec prec,
9236 cp_id_kind * pidk)
9238 cp_parser_expression_stack stack;
9239 cp_parser_expression_stack_entry *sp = &stack[0];
9240 cp_parser_expression_stack_entry current;
9241 cp_expr rhs;
9242 cp_token *token;
9243 enum tree_code rhs_type;
9244 enum cp_parser_prec new_prec, lookahead_prec;
9245 tree overload;
9247 /* Parse the first expression. */
9248 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9249 ? TRUTH_NOT_EXPR : ERROR_MARK);
9250 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9251 cast_p, decltype_p, pidk);
9252 current.prec = prec;
9254 if (cp_parser_error_occurred (parser))
9255 return error_mark_node;
9257 for (;;)
9259 /* Get an operator token. */
9260 token = cp_lexer_peek_token (parser->lexer);
9262 if (warn_cxx11_compat
9263 && token->type == CPP_RSHIFT
9264 && !parser->greater_than_is_operator_p)
9266 if (warning_at (token->location, OPT_Wc__11_compat,
9267 "%<>>%> operator is treated"
9268 " as two right angle brackets in C++11"))
9269 inform (token->location,
9270 "suggest parentheses around %<>>%> expression");
9273 new_prec = TOKEN_PRECEDENCE (token);
9274 if (new_prec != PREC_NOT_OPERATOR
9275 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9276 /* This is a fold-expression; handle it later. */
9277 new_prec = PREC_NOT_OPERATOR;
9279 /* Popping an entry off the stack means we completed a subexpression:
9280 - either we found a token which is not an operator (`>' where it is not
9281 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9282 will happen repeatedly;
9283 - or, we found an operator which has lower priority. This is the case
9284 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9285 parsing `3 * 4'. */
9286 if (new_prec <= current.prec)
9288 if (sp == stack)
9289 break;
9290 else
9291 goto pop;
9294 get_rhs:
9295 current.tree_type = binops_by_token[token->type].tree_type;
9296 current.loc = token->location;
9298 /* We used the operator token. */
9299 cp_lexer_consume_token (parser->lexer);
9301 /* For "false && x" or "true || x", x will never be executed;
9302 disable warnings while evaluating it. */
9303 if (current.tree_type == TRUTH_ANDIF_EXPR)
9304 c_inhibit_evaluation_warnings +=
9305 cp_fully_fold (current.lhs) == truthvalue_false_node;
9306 else if (current.tree_type == TRUTH_ORIF_EXPR)
9307 c_inhibit_evaluation_warnings +=
9308 cp_fully_fold (current.lhs) == truthvalue_true_node;
9310 /* Extract another operand. It may be the RHS of this expression
9311 or the LHS of a new, higher priority expression. */
9312 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9313 ? TRUTH_NOT_EXPR : ERROR_MARK);
9314 rhs = cp_parser_simple_cast_expression (parser);
9316 /* Get another operator token. Look up its precedence to avoid
9317 building a useless (immediately popped) stack entry for common
9318 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9319 token = cp_lexer_peek_token (parser->lexer);
9320 lookahead_prec = TOKEN_PRECEDENCE (token);
9321 if (lookahead_prec != PREC_NOT_OPERATOR
9322 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9323 lookahead_prec = PREC_NOT_OPERATOR;
9324 if (lookahead_prec > new_prec)
9326 /* ... and prepare to parse the RHS of the new, higher priority
9327 expression. Since precedence levels on the stack are
9328 monotonically increasing, we do not have to care about
9329 stack overflows. */
9330 *sp = current;
9331 ++sp;
9332 current.lhs = rhs;
9333 current.lhs_type = rhs_type;
9334 current.prec = new_prec;
9335 new_prec = lookahead_prec;
9336 goto get_rhs;
9338 pop:
9339 lookahead_prec = new_prec;
9340 /* If the stack is not empty, we have parsed into LHS the right side
9341 (`4' in the example above) of an expression we had suspended.
9342 We can use the information on the stack to recover the LHS (`3')
9343 from the stack together with the tree code (`MULT_EXPR'), and
9344 the precedence of the higher level subexpression
9345 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9346 which will be used to actually build the additive expression. */
9347 rhs = current.lhs;
9348 rhs_type = current.lhs_type;
9349 --sp;
9350 current = *sp;
9353 /* Undo the disabling of warnings done above. */
9354 if (current.tree_type == TRUTH_ANDIF_EXPR)
9355 c_inhibit_evaluation_warnings -=
9356 cp_fully_fold (current.lhs) == truthvalue_false_node;
9357 else if (current.tree_type == TRUTH_ORIF_EXPR)
9358 c_inhibit_evaluation_warnings -=
9359 cp_fully_fold (current.lhs) == truthvalue_true_node;
9361 if (warn_logical_not_paren
9362 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9363 && current.lhs_type == TRUTH_NOT_EXPR
9364 /* Avoid warning for !!x == y. */
9365 && (TREE_CODE (current.lhs) != NE_EXPR
9366 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9367 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9368 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9369 /* Avoid warning for !b == y where b is boolean. */
9370 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9371 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9372 != BOOLEAN_TYPE))))
9373 /* Avoid warning for !!b == y where b is boolean. */
9374 && (!DECL_P (current.lhs)
9375 || TREE_TYPE (current.lhs) == NULL_TREE
9376 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9377 warn_logical_not_parentheses (current.loc, current.tree_type,
9378 current.lhs, maybe_constant_value (rhs));
9380 overload = NULL;
9382 location_t combined_loc = make_location (current.loc,
9383 current.lhs.get_start (),
9384 rhs.get_finish ());
9386 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9387 ERROR_MARK for everything that is not a binary expression.
9388 This makes warn_about_parentheses miss some warnings that
9389 involve unary operators. For unary expressions we should
9390 pass the correct tree_code unless the unary expression was
9391 surrounded by parentheses.
9393 if (no_toplevel_fold_p
9394 && lookahead_prec <= current.prec
9395 && sp == stack)
9397 if (current.lhs == error_mark_node || rhs == error_mark_node)
9398 current.lhs = error_mark_node;
9399 else
9401 current.lhs
9402 = build_min (current.tree_type,
9403 TREE_CODE_CLASS (current.tree_type)
9404 == tcc_comparison
9405 ? boolean_type_node : TREE_TYPE (current.lhs),
9406 current.lhs.get_value (), rhs.get_value ());
9407 SET_EXPR_LOCATION (current.lhs, combined_loc);
9410 else
9412 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9413 current.lhs, current.lhs_type,
9414 rhs, rhs_type, &overload,
9415 complain_flags (decltype_p));
9416 /* TODO: build_x_binary_op doesn't always honor the location. */
9417 current.lhs.set_location (combined_loc);
9419 current.lhs_type = current.tree_type;
9421 /* If the binary operator required the use of an overloaded operator,
9422 then this expression cannot be an integral constant-expression.
9423 An overloaded operator can be used even if both operands are
9424 otherwise permissible in an integral constant-expression if at
9425 least one of the operands is of enumeration type. */
9427 if (overload
9428 && cp_parser_non_integral_constant_expression (parser,
9429 NIC_OVERLOADED))
9430 return error_mark_node;
9433 return current.lhs;
9436 static cp_expr
9437 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9438 bool no_toplevel_fold_p,
9439 enum cp_parser_prec prec,
9440 cp_id_kind * pidk)
9442 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9443 /*decltype*/false, prec, pidk);
9446 /* Parse the `? expression : assignment-expression' part of a
9447 conditional-expression. The LOGICAL_OR_EXPR is the
9448 logical-or-expression that started the conditional-expression.
9449 Returns a representation of the entire conditional-expression.
9451 This routine is used by cp_parser_assignment_expression.
9453 ? expression : assignment-expression
9455 GNU Extensions:
9457 ? : assignment-expression */
9459 static tree
9460 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9462 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9463 cp_expr assignment_expr;
9464 struct cp_token *token;
9465 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9467 /* Consume the `?' token. */
9468 cp_lexer_consume_token (parser->lexer);
9469 token = cp_lexer_peek_token (parser->lexer);
9470 if (cp_parser_allow_gnu_extensions_p (parser)
9471 && token->type == CPP_COLON)
9473 pedwarn (token->location, OPT_Wpedantic,
9474 "ISO C++ does not allow ?: with omitted middle operand");
9475 /* Implicit true clause. */
9476 expr = NULL_TREE;
9477 c_inhibit_evaluation_warnings +=
9478 folded_logical_or_expr == truthvalue_true_node;
9479 warn_for_omitted_condop (token->location, logical_or_expr);
9481 else
9483 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9484 parser->colon_corrects_to_scope_p = false;
9485 /* Parse the expression. */
9486 c_inhibit_evaluation_warnings +=
9487 folded_logical_or_expr == truthvalue_false_node;
9488 expr = cp_parser_expression (parser);
9489 c_inhibit_evaluation_warnings +=
9490 ((folded_logical_or_expr == truthvalue_true_node)
9491 - (folded_logical_or_expr == truthvalue_false_node));
9492 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9495 /* The next token should be a `:'. */
9496 cp_parser_require (parser, CPP_COLON, RT_COLON);
9497 /* Parse the assignment-expression. */
9498 assignment_expr = cp_parser_assignment_expression (parser);
9499 c_inhibit_evaluation_warnings -=
9500 folded_logical_or_expr == truthvalue_true_node;
9502 /* Make a location:
9503 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9504 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9505 with the caret at the "?", ranging from the start of
9506 the logical_or_expr to the end of the assignment_expr. */
9507 loc = make_location (loc,
9508 logical_or_expr.get_start (),
9509 assignment_expr.get_finish ());
9511 /* Build the conditional-expression. */
9512 return build_x_conditional_expr (loc, logical_or_expr,
9513 expr,
9514 assignment_expr,
9515 tf_warning_or_error);
9518 /* Parse an assignment-expression.
9520 assignment-expression:
9521 conditional-expression
9522 logical-or-expression assignment-operator assignment_expression
9523 throw-expression
9525 CAST_P is true if this expression is the target of a cast.
9526 DECLTYPE_P is true if this expression is the operand of decltype.
9528 Returns a representation for the expression. */
9530 static cp_expr
9531 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9532 bool cast_p, bool decltype_p)
9534 cp_expr expr;
9536 /* If the next token is the `throw' keyword, then we're looking at
9537 a throw-expression. */
9538 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9539 expr = cp_parser_throw_expression (parser);
9540 /* Otherwise, it must be that we are looking at a
9541 logical-or-expression. */
9542 else
9544 /* Parse the binary expressions (logical-or-expression). */
9545 expr = cp_parser_binary_expression (parser, cast_p, false,
9546 decltype_p,
9547 PREC_NOT_OPERATOR, pidk);
9548 /* If the next token is a `?' then we're actually looking at a
9549 conditional-expression. */
9550 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9551 return cp_parser_question_colon_clause (parser, expr);
9552 else
9554 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9556 /* If it's an assignment-operator, we're using the second
9557 production. */
9558 enum tree_code assignment_operator
9559 = cp_parser_assignment_operator_opt (parser);
9560 if (assignment_operator != ERROR_MARK)
9562 bool non_constant_p;
9564 /* Parse the right-hand side of the assignment. */
9565 cp_expr rhs = cp_parser_initializer_clause (parser,
9566 &non_constant_p);
9568 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9569 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9571 /* An assignment may not appear in a
9572 constant-expression. */
9573 if (cp_parser_non_integral_constant_expression (parser,
9574 NIC_ASSIGNMENT))
9575 return error_mark_node;
9576 /* Build the assignment expression. Its default
9577 location:
9578 LHS = RHS
9579 ~~~~^~~~~
9580 is the location of the '=' token as the
9581 caret, ranging from the start of the lhs to the
9582 end of the rhs. */
9583 loc = make_location (loc,
9584 expr.get_start (),
9585 rhs.get_finish ());
9586 expr = build_x_modify_expr (loc, expr,
9587 assignment_operator,
9588 rhs,
9589 complain_flags (decltype_p));
9590 /* TODO: build_x_modify_expr doesn't honor the location,
9591 so we must set it here. */
9592 expr.set_location (loc);
9597 return expr;
9600 /* Parse an (optional) assignment-operator.
9602 assignment-operator: one of
9603 = *= /= %= += -= >>= <<= &= ^= |=
9605 GNU Extension:
9607 assignment-operator: one of
9608 <?= >?=
9610 If the next token is an assignment operator, the corresponding tree
9611 code is returned, and the token is consumed. For example, for
9612 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9613 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9614 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9615 operator, ERROR_MARK is returned. */
9617 static enum tree_code
9618 cp_parser_assignment_operator_opt (cp_parser* parser)
9620 enum tree_code op;
9621 cp_token *token;
9623 /* Peek at the next token. */
9624 token = cp_lexer_peek_token (parser->lexer);
9626 switch (token->type)
9628 case CPP_EQ:
9629 op = NOP_EXPR;
9630 break;
9632 case CPP_MULT_EQ:
9633 op = MULT_EXPR;
9634 break;
9636 case CPP_DIV_EQ:
9637 op = TRUNC_DIV_EXPR;
9638 break;
9640 case CPP_MOD_EQ:
9641 op = TRUNC_MOD_EXPR;
9642 break;
9644 case CPP_PLUS_EQ:
9645 op = PLUS_EXPR;
9646 break;
9648 case CPP_MINUS_EQ:
9649 op = MINUS_EXPR;
9650 break;
9652 case CPP_RSHIFT_EQ:
9653 op = RSHIFT_EXPR;
9654 break;
9656 case CPP_LSHIFT_EQ:
9657 op = LSHIFT_EXPR;
9658 break;
9660 case CPP_AND_EQ:
9661 op = BIT_AND_EXPR;
9662 break;
9664 case CPP_XOR_EQ:
9665 op = BIT_XOR_EXPR;
9666 break;
9668 case CPP_OR_EQ:
9669 op = BIT_IOR_EXPR;
9670 break;
9672 default:
9673 /* Nothing else is an assignment operator. */
9674 op = ERROR_MARK;
9677 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9678 if (op != ERROR_MARK
9679 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9680 op = ERROR_MARK;
9682 /* If it was an assignment operator, consume it. */
9683 if (op != ERROR_MARK)
9684 cp_lexer_consume_token (parser->lexer);
9686 return op;
9689 /* Parse an expression.
9691 expression:
9692 assignment-expression
9693 expression , assignment-expression
9695 CAST_P is true if this expression is the target of a cast.
9696 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9697 except possibly parenthesized or on the RHS of a comma (N3276).
9699 Returns a representation of the expression. */
9701 static cp_expr
9702 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9703 bool cast_p, bool decltype_p)
9705 cp_expr expression = NULL_TREE;
9706 location_t loc = UNKNOWN_LOCATION;
9708 while (true)
9710 cp_expr assignment_expression;
9712 /* Parse the next assignment-expression. */
9713 assignment_expression
9714 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9716 /* We don't create a temporary for a call that is the immediate operand
9717 of decltype or on the RHS of a comma. But when we see a comma, we
9718 need to create a temporary for a call on the LHS. */
9719 if (decltype_p && !processing_template_decl
9720 && TREE_CODE (assignment_expression) == CALL_EXPR
9721 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9722 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9723 assignment_expression
9724 = build_cplus_new (TREE_TYPE (assignment_expression),
9725 assignment_expression, tf_warning_or_error);
9727 /* If this is the first assignment-expression, we can just
9728 save it away. */
9729 if (!expression)
9730 expression = assignment_expression;
9731 else
9733 /* Create a location with caret at the comma, ranging
9734 from the start of the LHS to the end of the RHS. */
9735 loc = make_location (loc,
9736 expression.get_start (),
9737 assignment_expression.get_finish ());
9738 expression = build_x_compound_expr (loc, expression,
9739 assignment_expression,
9740 complain_flags (decltype_p));
9741 expression.set_location (loc);
9743 /* If the next token is not a comma, or we're in a fold-expression, then
9744 we are done with the expression. */
9745 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9746 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9747 break;
9748 /* Consume the `,'. */
9749 loc = cp_lexer_peek_token (parser->lexer)->location;
9750 cp_lexer_consume_token (parser->lexer);
9751 /* A comma operator cannot appear in a constant-expression. */
9752 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9753 expression = error_mark_node;
9756 return expression;
9759 /* Parse a constant-expression.
9761 constant-expression:
9762 conditional-expression
9764 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9765 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9766 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9767 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9768 only parse a conditional-expression, otherwise parse an
9769 assignment-expression. See below for rationale. */
9771 static cp_expr
9772 cp_parser_constant_expression (cp_parser* parser,
9773 bool allow_non_constant_p,
9774 bool *non_constant_p,
9775 bool strict_p)
9777 bool saved_integral_constant_expression_p;
9778 bool saved_allow_non_integral_constant_expression_p;
9779 bool saved_non_integral_constant_expression_p;
9780 cp_expr expression;
9782 /* It might seem that we could simply parse the
9783 conditional-expression, and then check to see if it were
9784 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9785 one that the compiler can figure out is constant, possibly after
9786 doing some simplifications or optimizations. The standard has a
9787 precise definition of constant-expression, and we must honor
9788 that, even though it is somewhat more restrictive.
9790 For example:
9792 int i[(2, 3)];
9794 is not a legal declaration, because `(2, 3)' is not a
9795 constant-expression. The `,' operator is forbidden in a
9796 constant-expression. However, GCC's constant-folding machinery
9797 will fold this operation to an INTEGER_CST for `3'. */
9799 /* Save the old settings. */
9800 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9801 saved_allow_non_integral_constant_expression_p
9802 = parser->allow_non_integral_constant_expression_p;
9803 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9804 /* We are now parsing a constant-expression. */
9805 parser->integral_constant_expression_p = true;
9806 parser->allow_non_integral_constant_expression_p
9807 = (allow_non_constant_p || cxx_dialect >= cxx11);
9808 parser->non_integral_constant_expression_p = false;
9809 /* Although the grammar says "conditional-expression", when not STRICT_P,
9810 we parse an "assignment-expression", which also permits
9811 "throw-expression" and the use of assignment operators. In the case
9812 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9813 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9814 actually essential that we look for an assignment-expression.
9815 For example, cp_parser_initializer_clauses uses this function to
9816 determine whether a particular assignment-expression is in fact
9817 constant. */
9818 if (strict_p)
9820 /* Parse the binary expressions (logical-or-expression). */
9821 expression = cp_parser_binary_expression (parser, false, false, false,
9822 PREC_NOT_OPERATOR, NULL);
9823 /* If the next token is a `?' then we're actually looking at
9824 a conditional-expression; otherwise we're done. */
9825 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9826 expression = cp_parser_question_colon_clause (parser, expression);
9828 else
9829 expression = cp_parser_assignment_expression (parser);
9830 /* Restore the old settings. */
9831 parser->integral_constant_expression_p
9832 = saved_integral_constant_expression_p;
9833 parser->allow_non_integral_constant_expression_p
9834 = saved_allow_non_integral_constant_expression_p;
9835 if (cxx_dialect >= cxx11)
9837 /* Require an rvalue constant expression here; that's what our
9838 callers expect. Reference constant expressions are handled
9839 separately in e.g. cp_parser_template_argument. */
9840 tree decay = expression;
9841 if (TREE_TYPE (expression)
9842 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9843 decay = build_address (expression);
9844 bool is_const = potential_rvalue_constant_expression (decay);
9845 parser->non_integral_constant_expression_p = !is_const;
9846 if (!is_const && !allow_non_constant_p)
9847 require_potential_rvalue_constant_expression (decay);
9849 if (allow_non_constant_p)
9850 *non_constant_p = parser->non_integral_constant_expression_p;
9851 parser->non_integral_constant_expression_p
9852 = saved_non_integral_constant_expression_p;
9854 return expression;
9857 /* Parse __builtin_offsetof.
9859 offsetof-expression:
9860 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9862 offsetof-member-designator:
9863 id-expression
9864 | offsetof-member-designator "." id-expression
9865 | offsetof-member-designator "[" expression "]"
9866 | offsetof-member-designator "->" id-expression */
9868 static cp_expr
9869 cp_parser_builtin_offsetof (cp_parser *parser)
9871 int save_ice_p, save_non_ice_p;
9872 tree type;
9873 cp_expr expr;
9874 cp_id_kind dummy;
9875 cp_token *token;
9876 location_t finish_loc;
9878 /* We're about to accept non-integral-constant things, but will
9879 definitely yield an integral constant expression. Save and
9880 restore these values around our local parsing. */
9881 save_ice_p = parser->integral_constant_expression_p;
9882 save_non_ice_p = parser->non_integral_constant_expression_p;
9884 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9886 /* Consume the "__builtin_offsetof" token. */
9887 cp_lexer_consume_token (parser->lexer);
9888 /* Consume the opening `('. */
9889 matching_parens parens;
9890 parens.require_open (parser);
9891 /* Parse the type-id. */
9892 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9894 const char *saved_message = parser->type_definition_forbidden_message;
9895 parser->type_definition_forbidden_message
9896 = G_("types may not be defined within __builtin_offsetof");
9897 type = cp_parser_type_id (parser);
9898 parser->type_definition_forbidden_message = saved_message;
9900 /* Look for the `,'. */
9901 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9902 token = cp_lexer_peek_token (parser->lexer);
9904 /* Build the (type *)null that begins the traditional offsetof macro. */
9905 tree object_ptr
9906 = build_static_cast (build_pointer_type (type), null_pointer_node,
9907 tf_warning_or_error);
9909 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9910 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9911 true, &dummy, token->location);
9912 while (true)
9914 token = cp_lexer_peek_token (parser->lexer);
9915 switch (token->type)
9917 case CPP_OPEN_SQUARE:
9918 /* offsetof-member-designator "[" expression "]" */
9919 expr = cp_parser_postfix_open_square_expression (parser, expr,
9920 true, false);
9921 break;
9923 case CPP_DEREF:
9924 /* offsetof-member-designator "->" identifier */
9925 expr = grok_array_decl (token->location, expr,
9926 integer_zero_node, false);
9927 /* FALLTHRU */
9929 case CPP_DOT:
9930 /* offsetof-member-designator "." identifier */
9931 cp_lexer_consume_token (parser->lexer);
9932 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9933 expr, true, &dummy,
9934 token->location);
9935 break;
9937 case CPP_CLOSE_PAREN:
9938 /* Consume the ")" token. */
9939 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9940 cp_lexer_consume_token (parser->lexer);
9941 goto success;
9943 default:
9944 /* Error. We know the following require will fail, but
9945 that gives the proper error message. */
9946 parens.require_close (parser);
9947 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9948 expr = error_mark_node;
9949 goto failure;
9953 success:
9954 /* Make a location of the form:
9955 __builtin_offsetof (struct s, f)
9956 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9957 with caret at the type-id, ranging from the start of the
9958 "_builtin_offsetof" token to the close paren. */
9959 loc = make_location (loc, start_loc, finish_loc);
9960 /* The result will be an INTEGER_CST, so we need to explicitly
9961 preserve the location. */
9962 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9964 failure:
9965 parser->integral_constant_expression_p = save_ice_p;
9966 parser->non_integral_constant_expression_p = save_non_ice_p;
9968 expr = expr.maybe_add_location_wrapper ();
9969 return expr;
9972 /* Parse a trait expression.
9974 Returns a representation of the expression, the underlying type
9975 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9977 static cp_expr
9978 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9980 cp_trait_kind kind;
9981 tree type1, type2 = NULL_TREE;
9982 bool binary = false;
9983 bool variadic = false;
9985 switch (keyword)
9987 case RID_HAS_NOTHROW_ASSIGN:
9988 kind = CPTK_HAS_NOTHROW_ASSIGN;
9989 break;
9990 case RID_HAS_NOTHROW_CONSTRUCTOR:
9991 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9992 break;
9993 case RID_HAS_NOTHROW_COPY:
9994 kind = CPTK_HAS_NOTHROW_COPY;
9995 break;
9996 case RID_HAS_TRIVIAL_ASSIGN:
9997 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9998 break;
9999 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10000 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10001 break;
10002 case RID_HAS_TRIVIAL_COPY:
10003 kind = CPTK_HAS_TRIVIAL_COPY;
10004 break;
10005 case RID_HAS_TRIVIAL_DESTRUCTOR:
10006 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10007 break;
10008 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10009 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10010 break;
10011 case RID_HAS_VIRTUAL_DESTRUCTOR:
10012 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10013 break;
10014 case RID_IS_ABSTRACT:
10015 kind = CPTK_IS_ABSTRACT;
10016 break;
10017 case RID_IS_AGGREGATE:
10018 kind = CPTK_IS_AGGREGATE;
10019 break;
10020 case RID_IS_BASE_OF:
10021 kind = CPTK_IS_BASE_OF;
10022 binary = true;
10023 break;
10024 case RID_IS_CLASS:
10025 kind = CPTK_IS_CLASS;
10026 break;
10027 case RID_IS_EMPTY:
10028 kind = CPTK_IS_EMPTY;
10029 break;
10030 case RID_IS_ENUM:
10031 kind = CPTK_IS_ENUM;
10032 break;
10033 case RID_IS_FINAL:
10034 kind = CPTK_IS_FINAL;
10035 break;
10036 case RID_IS_LITERAL_TYPE:
10037 kind = CPTK_IS_LITERAL_TYPE;
10038 break;
10039 case RID_IS_POD:
10040 kind = CPTK_IS_POD;
10041 break;
10042 case RID_IS_POLYMORPHIC:
10043 kind = CPTK_IS_POLYMORPHIC;
10044 break;
10045 case RID_IS_SAME_AS:
10046 kind = CPTK_IS_SAME_AS;
10047 binary = true;
10048 break;
10049 case RID_IS_STD_LAYOUT:
10050 kind = CPTK_IS_STD_LAYOUT;
10051 break;
10052 case RID_IS_TRIVIAL:
10053 kind = CPTK_IS_TRIVIAL;
10054 break;
10055 case RID_IS_TRIVIALLY_ASSIGNABLE:
10056 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10057 binary = true;
10058 break;
10059 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10060 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10061 variadic = true;
10062 break;
10063 case RID_IS_TRIVIALLY_COPYABLE:
10064 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10065 break;
10066 case RID_IS_UNION:
10067 kind = CPTK_IS_UNION;
10068 break;
10069 case RID_UNDERLYING_TYPE:
10070 kind = CPTK_UNDERLYING_TYPE;
10071 break;
10072 case RID_BASES:
10073 kind = CPTK_BASES;
10074 break;
10075 case RID_DIRECT_BASES:
10076 kind = CPTK_DIRECT_BASES;
10077 break;
10078 case RID_IS_ASSIGNABLE:
10079 kind = CPTK_IS_ASSIGNABLE;
10080 binary = true;
10081 break;
10082 case RID_IS_CONSTRUCTIBLE:
10083 kind = CPTK_IS_CONSTRUCTIBLE;
10084 variadic = true;
10085 break;
10086 default:
10087 gcc_unreachable ();
10090 /* Get location of initial token. */
10091 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10093 /* Consume the token. */
10094 cp_lexer_consume_token (parser->lexer);
10096 matching_parens parens;
10097 parens.require_open (parser);
10100 type_id_in_expr_sentinel s (parser);
10101 type1 = cp_parser_type_id (parser);
10104 if (type1 == error_mark_node)
10105 return error_mark_node;
10107 if (binary)
10109 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10112 type_id_in_expr_sentinel s (parser);
10113 type2 = cp_parser_type_id (parser);
10116 if (type2 == error_mark_node)
10117 return error_mark_node;
10119 else if (variadic)
10121 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10123 cp_lexer_consume_token (parser->lexer);
10124 tree elt = cp_parser_type_id (parser);
10125 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10127 cp_lexer_consume_token (parser->lexer);
10128 elt = make_pack_expansion (elt);
10130 if (elt == error_mark_node)
10131 return error_mark_node;
10132 type2 = tree_cons (NULL_TREE, elt, type2);
10136 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10137 parens.require_close (parser);
10139 /* Construct a location of the form:
10140 __is_trivially_copyable(_Tp)
10141 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10142 with start == caret, finishing at the close-paren. */
10143 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10145 /* Complete the trait expression, which may mean either processing
10146 the trait expr now or saving it for template instantiation. */
10147 switch (kind)
10149 case CPTK_UNDERLYING_TYPE:
10150 return cp_expr (finish_underlying_type (type1), trait_loc);
10151 case CPTK_BASES:
10152 return cp_expr (finish_bases (type1, false), trait_loc);
10153 case CPTK_DIRECT_BASES:
10154 return cp_expr (finish_bases (type1, true), trait_loc);
10155 default:
10156 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10160 /* Parse a lambda expression.
10162 lambda-expression:
10163 lambda-introducer lambda-declarator [opt] compound-statement
10165 Returns a representation of the expression. */
10167 static cp_expr
10168 cp_parser_lambda_expression (cp_parser* parser)
10170 tree lambda_expr = build_lambda_expr ();
10171 tree type;
10172 bool ok = true;
10173 cp_token *token = cp_lexer_peek_token (parser->lexer);
10174 cp_token_position start = 0;
10176 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10178 if (cp_unevaluated_operand)
10180 if (!token->error_reported)
10182 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10183 "lambda-expression in unevaluated context");
10184 token->error_reported = true;
10186 ok = false;
10188 else if (parser->in_template_argument_list_p)
10190 if (!token->error_reported)
10192 error_at (token->location, "lambda-expression in template-argument");
10193 token->error_reported = true;
10195 ok = false;
10198 /* We may be in the middle of deferred access check. Disable
10199 it now. */
10200 push_deferring_access_checks (dk_no_deferred);
10202 cp_parser_lambda_introducer (parser, lambda_expr);
10204 type = begin_lambda_type (lambda_expr);
10205 if (type == error_mark_node)
10206 return error_mark_node;
10208 record_lambda_scope (lambda_expr);
10210 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10211 determine_visibility (TYPE_NAME (type));
10213 /* Now that we've started the type, add the capture fields for any
10214 explicit captures. */
10215 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10218 /* Inside the class, surrounding template-parameter-lists do not apply. */
10219 unsigned int saved_num_template_parameter_lists
10220 = parser->num_template_parameter_lists;
10221 unsigned char in_statement = parser->in_statement;
10222 bool in_switch_statement_p = parser->in_switch_statement_p;
10223 bool fully_implicit_function_template_p
10224 = parser->fully_implicit_function_template_p;
10225 tree implicit_template_parms = parser->implicit_template_parms;
10226 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10227 bool auto_is_implicit_function_template_parm_p
10228 = parser->auto_is_implicit_function_template_parm_p;
10230 parser->num_template_parameter_lists = 0;
10231 parser->in_statement = 0;
10232 parser->in_switch_statement_p = false;
10233 parser->fully_implicit_function_template_p = false;
10234 parser->implicit_template_parms = 0;
10235 parser->implicit_template_scope = 0;
10236 parser->auto_is_implicit_function_template_parm_p = false;
10238 /* By virtue of defining a local class, a lambda expression has access to
10239 the private variables of enclosing classes. */
10241 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10243 if (ok && cp_parser_error_occurred (parser))
10244 ok = false;
10246 if (ok)
10248 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10249 && cp_parser_start_tentative_firewall (parser))
10250 start = token;
10251 cp_parser_lambda_body (parser, lambda_expr);
10253 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10255 if (cp_parser_skip_to_closing_brace (parser))
10256 cp_lexer_consume_token (parser->lexer);
10259 /* The capture list was built up in reverse order; fix that now. */
10260 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10261 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10263 if (ok)
10264 maybe_add_lambda_conv_op (type);
10266 type = finish_struct (type, /*attributes=*/NULL_TREE);
10268 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10269 parser->in_statement = in_statement;
10270 parser->in_switch_statement_p = in_switch_statement_p;
10271 parser->fully_implicit_function_template_p
10272 = fully_implicit_function_template_p;
10273 parser->implicit_template_parms = implicit_template_parms;
10274 parser->implicit_template_scope = implicit_template_scope;
10275 parser->auto_is_implicit_function_template_parm_p
10276 = auto_is_implicit_function_template_parm_p;
10279 /* This field is only used during parsing of the lambda. */
10280 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10282 /* This lambda shouldn't have any proxies left at this point. */
10283 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10284 /* And now that we're done, push proxies for an enclosing lambda. */
10285 insert_pending_capture_proxies ();
10287 /* Update the lambda expression to a range. */
10288 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10289 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10290 token->location,
10291 end_tok->location);
10293 if (ok)
10294 lambda_expr = build_lambda_object (lambda_expr);
10295 else
10296 lambda_expr = error_mark_node;
10298 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10300 pop_deferring_access_checks ();
10302 return lambda_expr;
10305 /* Parse the beginning of a lambda expression.
10307 lambda-introducer:
10308 [ lambda-capture [opt] ]
10310 LAMBDA_EXPR is the current representation of the lambda expression. */
10312 static void
10313 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10315 /* Need commas after the first capture. */
10316 bool first = true;
10318 /* Eat the leading `['. */
10319 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10321 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10322 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10323 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10324 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10325 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10326 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10328 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10330 cp_lexer_consume_token (parser->lexer);
10331 first = false;
10333 if (!(at_function_scope_p () || parsing_nsdmi ()))
10334 error ("non-local lambda expression cannot have a capture-default");
10337 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10339 cp_token* capture_token;
10340 tree capture_id;
10341 tree capture_init_expr;
10342 cp_id_kind idk = CP_ID_KIND_NONE;
10343 bool explicit_init_p = false;
10345 enum capture_kind_type
10347 BY_COPY,
10348 BY_REFERENCE
10350 enum capture_kind_type capture_kind = BY_COPY;
10352 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10354 error ("expected end of capture-list");
10355 return;
10358 if (first)
10359 first = false;
10360 else
10361 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10363 /* Possibly capture `this'. */
10364 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10366 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10367 if (cxx_dialect < cxx2a
10368 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10369 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10370 "with by-copy capture default");
10371 cp_lexer_consume_token (parser->lexer);
10372 add_capture (lambda_expr,
10373 /*id=*/this_identifier,
10374 /*initializer=*/finish_this_expr (),
10375 /*by_reference_p=*/true,
10376 explicit_init_p);
10377 continue;
10380 /* Possibly capture `*this'. */
10381 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10382 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10384 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10385 if (cxx_dialect < cxx17)
10386 pedwarn (loc, 0, "%<*this%> capture only available with "
10387 "-std=c++17 or -std=gnu++17");
10388 cp_lexer_consume_token (parser->lexer);
10389 cp_lexer_consume_token (parser->lexer);
10390 add_capture (lambda_expr,
10391 /*id=*/this_identifier,
10392 /*initializer=*/finish_this_expr (),
10393 /*by_reference_p=*/false,
10394 explicit_init_p);
10395 continue;
10398 /* Remember whether we want to capture as a reference or not. */
10399 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10401 capture_kind = BY_REFERENCE;
10402 cp_lexer_consume_token (parser->lexer);
10405 /* Get the identifier. */
10406 capture_token = cp_lexer_peek_token (parser->lexer);
10407 capture_id = cp_parser_identifier (parser);
10409 if (capture_id == error_mark_node)
10410 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10411 delimiters, but I modified this to stop on unnested ']' as well. It
10412 was already changed to stop on unnested '}', so the
10413 "closing_parenthesis" name is no more misleading with my change. */
10415 cp_parser_skip_to_closing_parenthesis (parser,
10416 /*recovering=*/true,
10417 /*or_comma=*/true,
10418 /*consume_paren=*/true);
10419 break;
10422 /* Find the initializer for this capture. */
10423 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10424 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10425 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10427 bool direct, non_constant;
10428 /* An explicit initializer exists. */
10429 if (cxx_dialect < cxx14)
10430 pedwarn (input_location, 0,
10431 "lambda capture initializers "
10432 "only available with -std=c++14 or -std=gnu++14");
10433 capture_init_expr = cp_parser_initializer (parser, &direct,
10434 &non_constant, true);
10435 explicit_init_p = true;
10436 if (capture_init_expr == NULL_TREE)
10438 error ("empty initializer for lambda init-capture");
10439 capture_init_expr = error_mark_node;
10442 else
10444 const char* error_msg;
10446 /* Turn the identifier into an id-expression. */
10447 capture_init_expr
10448 = cp_parser_lookup_name_simple (parser, capture_id,
10449 capture_token->location);
10451 if (capture_init_expr == error_mark_node)
10453 unqualified_name_lookup_error (capture_id);
10454 continue;
10456 else if (!VAR_P (capture_init_expr)
10457 && TREE_CODE (capture_init_expr) != PARM_DECL)
10459 error_at (capture_token->location,
10460 "capture of non-variable %qE",
10461 capture_init_expr);
10462 if (DECL_P (capture_init_expr))
10463 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10464 "%q#D declared here", capture_init_expr);
10465 continue;
10467 if (VAR_P (capture_init_expr)
10468 && decl_storage_duration (capture_init_expr) != dk_auto)
10470 if (pedwarn (capture_token->location, 0, "capture of variable "
10471 "%qD with non-automatic storage duration",
10472 capture_init_expr))
10473 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10474 "%q#D declared here", capture_init_expr);
10475 continue;
10478 capture_init_expr
10479 = finish_id_expression
10480 (capture_id,
10481 capture_init_expr,
10482 parser->scope,
10483 &idk,
10484 /*integral_constant_expression_p=*/false,
10485 /*allow_non_integral_constant_expression_p=*/false,
10486 /*non_integral_constant_expression_p=*/NULL,
10487 /*template_p=*/false,
10488 /*done=*/true,
10489 /*address_p=*/false,
10490 /*template_arg_p=*/false,
10491 &error_msg,
10492 capture_token->location);
10494 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10496 cp_lexer_consume_token (parser->lexer);
10497 capture_init_expr = make_pack_expansion (capture_init_expr);
10501 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10502 && !explicit_init_p)
10504 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10505 && capture_kind == BY_COPY)
10506 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10507 "of %qD redundant with by-copy capture default",
10508 capture_id);
10509 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10510 && capture_kind == BY_REFERENCE)
10511 pedwarn (capture_token->location, 0, "explicit by-reference "
10512 "capture of %qD redundant with by-reference capture "
10513 "default", capture_id);
10516 add_capture (lambda_expr,
10517 capture_id,
10518 capture_init_expr,
10519 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10520 explicit_init_p);
10522 /* If there is any qualification still in effect, clear it
10523 now; we will be starting fresh with the next capture. */
10524 parser->scope = NULL_TREE;
10525 parser->qualifying_scope = NULL_TREE;
10526 parser->object_scope = NULL_TREE;
10529 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10532 /* Parse the (optional) middle of a lambda expression.
10534 lambda-declarator:
10535 < template-parameter-list [opt] >
10536 ( parameter-declaration-clause [opt] )
10537 attribute-specifier [opt]
10538 decl-specifier-seq [opt]
10539 exception-specification [opt]
10540 lambda-return-type-clause [opt]
10542 LAMBDA_EXPR is the current representation of the lambda expression. */
10544 static bool
10545 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10547 /* 5.1.1.4 of the standard says:
10548 If a lambda-expression does not include a lambda-declarator, it is as if
10549 the lambda-declarator were ().
10550 This means an empty parameter list, no attributes, and no exception
10551 specification. */
10552 tree param_list = void_list_node;
10553 tree attributes = NULL_TREE;
10554 tree exception_spec = NULL_TREE;
10555 tree template_param_list = NULL_TREE;
10556 tree tx_qual = NULL_TREE;
10557 tree return_type = NULL_TREE;
10558 cp_decl_specifier_seq lambda_specs;
10559 clear_decl_specs (&lambda_specs);
10561 /* The template-parameter-list is optional, but must begin with
10562 an opening angle if present. */
10563 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10565 if (cxx_dialect < cxx14)
10566 pedwarn (parser->lexer->next_token->location, 0,
10567 "lambda templates are only available with "
10568 "-std=c++14 or -std=gnu++14");
10569 else if (cxx_dialect < cxx2a)
10570 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10571 "lambda templates are only available with "
10572 "-std=c++2a or -std=gnu++2a");
10574 cp_lexer_consume_token (parser->lexer);
10576 template_param_list = cp_parser_template_parameter_list (parser);
10578 cp_parser_skip_to_end_of_template_parameter_list (parser);
10580 /* We just processed one more parameter list. */
10581 ++parser->num_template_parameter_lists;
10584 /* The parameter-declaration-clause is optional (unless
10585 template-parameter-list was given), but must begin with an
10586 opening parenthesis if present. */
10587 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10589 matching_parens parens;
10590 parens.consume_open (parser);
10592 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10594 /* Parse parameters. */
10595 param_list = cp_parser_parameter_declaration_clause (parser);
10597 /* Default arguments shall not be specified in the
10598 parameter-declaration-clause of a lambda-declarator. */
10599 if (cxx_dialect < cxx14)
10600 for (tree t = param_list; t; t = TREE_CHAIN (t))
10601 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10602 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10603 "default argument specified for lambda parameter");
10605 parens.require_close (parser);
10607 /* In the decl-specifier-seq of the lambda-declarator, each
10608 decl-specifier shall either be mutable or constexpr. */
10609 int declares_class_or_enum;
10610 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10611 cp_parser_decl_specifier_seq (parser,
10612 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10613 &lambda_specs, &declares_class_or_enum);
10614 if (lambda_specs.storage_class == sc_mutable)
10616 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10617 if (lambda_specs.conflicting_specifiers_p)
10618 error_at (lambda_specs.locations[ds_storage_class],
10619 "duplicate %<mutable%>");
10622 tx_qual = cp_parser_tx_qualifier_opt (parser);
10624 /* Parse optional exception specification. */
10625 exception_spec = cp_parser_exception_specification_opt (parser);
10627 attributes = cp_parser_std_attribute_spec_seq (parser);
10629 /* Parse optional trailing return type. */
10630 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10632 cp_lexer_consume_token (parser->lexer);
10633 return_type = cp_parser_trailing_type_id (parser);
10636 /* The function parameters must be in scope all the way until after the
10637 trailing-return-type in case of decltype. */
10638 pop_bindings_and_leave_scope ();
10640 else if (template_param_list != NULL_TREE) // generate diagnostic
10641 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10643 /* Create the function call operator.
10645 Messing with declarators like this is no uglier than building up the
10646 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10647 other code. */
10649 cp_decl_specifier_seq return_type_specs;
10650 cp_declarator* declarator;
10651 tree fco;
10652 int quals;
10653 void *p;
10655 clear_decl_specs (&return_type_specs);
10656 return_type_specs.type = make_auto ();
10658 if (lambda_specs.locations[ds_constexpr])
10660 if (cxx_dialect >= cxx17)
10661 return_type_specs.locations[ds_constexpr]
10662 = lambda_specs.locations[ds_constexpr];
10663 else
10664 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10665 "lambda only available with -std=c++17 or -std=gnu++17");
10668 p = obstack_alloc (&declarator_obstack, 0);
10670 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10672 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10673 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10674 declarator = make_call_declarator (declarator, param_list, quals,
10675 VIRT_SPEC_UNSPECIFIED,
10676 REF_QUAL_NONE,
10677 tx_qual,
10678 exception_spec,
10679 return_type,
10680 /*requires_clause*/NULL_TREE);
10681 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10682 declarator->std_attributes = attributes;
10684 fco = grokmethod (&return_type_specs,
10685 declarator,
10686 NULL_TREE);
10687 if (fco != error_mark_node)
10689 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10690 DECL_ARTIFICIAL (fco) = 1;
10691 /* Give the object parameter a different name. */
10692 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10693 DECL_LAMBDA_FUNCTION (fco) = 1;
10695 if (template_param_list)
10697 fco = finish_member_template_decl (fco);
10698 finish_template_decl (template_param_list);
10699 --parser->num_template_parameter_lists;
10701 else if (parser->fully_implicit_function_template_p)
10702 fco = finish_fully_implicit_template (parser, fco);
10704 finish_member_declaration (fco);
10706 obstack_free (&declarator_obstack, p);
10708 return (fco != error_mark_node);
10712 /* Parse the body of a lambda expression, which is simply
10714 compound-statement
10716 but which requires special handling.
10717 LAMBDA_EXPR is the current representation of the lambda expression. */
10719 static void
10720 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10722 bool nested = (current_function_decl != NULL_TREE);
10723 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10724 bool in_function_body = parser->in_function_body;
10726 if (nested)
10727 push_function_context ();
10728 else
10729 /* Still increment function_depth so that we don't GC in the
10730 middle of an expression. */
10731 ++function_depth;
10733 vec<tree> omp_privatization_save;
10734 save_omp_privatization_clauses (omp_privatization_save);
10735 /* Clear this in case we're in the middle of a default argument. */
10736 parser->local_variables_forbidden_p = false;
10737 parser->in_function_body = true;
10740 local_specialization_stack s (lss_copy);
10741 tree fco = lambda_function (lambda_expr);
10742 tree body = start_lambda_function (fco, lambda_expr);
10743 matching_braces braces;
10745 if (braces.require_open (parser))
10747 tree compound_stmt = begin_compound_stmt (0);
10749 /* Originally C++11 required us to peek for 'return expr'; and
10750 process it specially here to deduce the return type. N3638
10751 removed the need for that. */
10753 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10754 cp_parser_label_declaration (parser);
10755 cp_parser_statement_seq_opt (parser, NULL_TREE);
10756 braces.require_close (parser);
10758 finish_compound_stmt (compound_stmt);
10761 finish_lambda_function (body);
10764 restore_omp_privatization_clauses (omp_privatization_save);
10765 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10766 parser->in_function_body = in_function_body;
10767 if (nested)
10768 pop_function_context();
10769 else
10770 --function_depth;
10773 /* Statements [gram.stmt.stmt] */
10775 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10777 static void
10778 add_debug_begin_stmt (location_t loc)
10780 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10781 return;
10782 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10783 /* A concept is never expanded normally. */
10784 return;
10786 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10787 SET_EXPR_LOCATION (stmt, loc);
10788 add_stmt (stmt);
10791 /* Parse a statement.
10793 statement:
10794 labeled-statement
10795 expression-statement
10796 compound-statement
10797 selection-statement
10798 iteration-statement
10799 jump-statement
10800 declaration-statement
10801 try-block
10803 C++11:
10805 statement:
10806 labeled-statement
10807 attribute-specifier-seq (opt) expression-statement
10808 attribute-specifier-seq (opt) compound-statement
10809 attribute-specifier-seq (opt) selection-statement
10810 attribute-specifier-seq (opt) iteration-statement
10811 attribute-specifier-seq (opt) jump-statement
10812 declaration-statement
10813 attribute-specifier-seq (opt) try-block
10815 init-statement:
10816 expression-statement
10817 simple-declaration
10819 TM Extension:
10821 statement:
10822 atomic-statement
10824 IN_COMPOUND is true when the statement is nested inside a
10825 cp_parser_compound_statement; this matters for certain pragmas.
10827 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10828 is a (possibly labeled) if statement which is not enclosed in braces
10829 and has an else clause. This is used to implement -Wparentheses.
10831 CHAIN is a vector of if-else-if conditions. */
10833 static void
10834 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10835 bool in_compound, bool *if_p, vec<tree> *chain,
10836 location_t *loc_after_labels)
10838 tree statement, std_attrs = NULL_TREE;
10839 cp_token *token;
10840 location_t statement_location, attrs_location;
10842 restart:
10843 if (if_p != NULL)
10844 *if_p = false;
10845 /* There is no statement yet. */
10846 statement = NULL_TREE;
10848 saved_token_sentinel saved_tokens (parser->lexer);
10849 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10850 if (c_dialect_objc ())
10851 /* In obj-c++, seeing '[[' might be the either the beginning of
10852 c++11 attributes, or a nested objc-message-expression. So
10853 let's parse the c++11 attributes tentatively. */
10854 cp_parser_parse_tentatively (parser);
10855 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10856 if (c_dialect_objc ())
10858 if (!cp_parser_parse_definitely (parser))
10859 std_attrs = NULL_TREE;
10862 /* Peek at the next token. */
10863 token = cp_lexer_peek_token (parser->lexer);
10864 /* Remember the location of the first token in the statement. */
10865 statement_location = token->location;
10866 add_debug_begin_stmt (statement_location);
10867 /* If this is a keyword, then that will often determine what kind of
10868 statement we have. */
10869 if (token->type == CPP_KEYWORD)
10871 enum rid keyword = token->keyword;
10873 switch (keyword)
10875 case RID_CASE:
10876 case RID_DEFAULT:
10877 /* Looks like a labeled-statement with a case label.
10878 Parse the label, and then use tail recursion to parse
10879 the statement. */
10880 cp_parser_label_for_labeled_statement (parser, std_attrs);
10881 in_compound = false;
10882 goto restart;
10884 case RID_IF:
10885 case RID_SWITCH:
10886 statement = cp_parser_selection_statement (parser, if_p, chain);
10887 break;
10889 case RID_WHILE:
10890 case RID_DO:
10891 case RID_FOR:
10892 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10893 break;
10895 case RID_BREAK:
10896 case RID_CONTINUE:
10897 case RID_RETURN:
10898 case RID_GOTO:
10899 statement = cp_parser_jump_statement (parser);
10900 break;
10902 /* Objective-C++ exception-handling constructs. */
10903 case RID_AT_TRY:
10904 case RID_AT_CATCH:
10905 case RID_AT_FINALLY:
10906 case RID_AT_SYNCHRONIZED:
10907 case RID_AT_THROW:
10908 statement = cp_parser_objc_statement (parser);
10909 break;
10911 case RID_TRY:
10912 statement = cp_parser_try_block (parser);
10913 break;
10915 case RID_NAMESPACE:
10916 /* This must be a namespace alias definition. */
10917 cp_parser_declaration_statement (parser);
10918 return;
10920 case RID_TRANSACTION_ATOMIC:
10921 case RID_TRANSACTION_RELAXED:
10922 case RID_SYNCHRONIZED:
10923 case RID_ATOMIC_NOEXCEPT:
10924 case RID_ATOMIC_CANCEL:
10925 statement = cp_parser_transaction (parser, token);
10926 break;
10927 case RID_TRANSACTION_CANCEL:
10928 statement = cp_parser_transaction_cancel (parser);
10929 break;
10931 default:
10932 /* It might be a keyword like `int' that can start a
10933 declaration-statement. */
10934 break;
10937 else if (token->type == CPP_NAME)
10939 /* If the next token is a `:', then we are looking at a
10940 labeled-statement. */
10941 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10942 if (token->type == CPP_COLON)
10944 /* Looks like a labeled-statement with an ordinary label.
10945 Parse the label, and then use tail recursion to parse
10946 the statement. */
10948 cp_parser_label_for_labeled_statement (parser, std_attrs);
10949 in_compound = false;
10950 goto restart;
10953 /* Anything that starts with a `{' must be a compound-statement. */
10954 else if (token->type == CPP_OPEN_BRACE)
10955 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10956 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10957 a statement all its own. */
10958 else if (token->type == CPP_PRAGMA)
10960 /* Only certain OpenMP pragmas are attached to statements, and thus
10961 are considered statements themselves. All others are not. In
10962 the context of a compound, accept the pragma as a "statement" and
10963 return so that we can check for a close brace. Otherwise we
10964 require a real statement and must go back and read one. */
10965 if (in_compound)
10966 cp_parser_pragma (parser, pragma_compound, if_p);
10967 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10968 goto restart;
10969 return;
10971 else if (token->type == CPP_EOF)
10973 cp_parser_error (parser, "expected statement");
10974 return;
10977 /* Everything else must be a declaration-statement or an
10978 expression-statement. Try for the declaration-statement
10979 first, unless we are looking at a `;', in which case we know that
10980 we have an expression-statement. */
10981 if (!statement)
10983 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10985 if (std_attrs != NULL_TREE)
10987 /* Attributes should be parsed as part of the the
10988 declaration, so let's un-parse them. */
10989 saved_tokens.rollback();
10990 std_attrs = NULL_TREE;
10993 cp_parser_parse_tentatively (parser);
10994 /* Try to parse the declaration-statement. */
10995 cp_parser_declaration_statement (parser);
10996 /* If that worked, we're done. */
10997 if (cp_parser_parse_definitely (parser))
10998 return;
11000 /* All preceding labels have been parsed at this point. */
11001 if (loc_after_labels != NULL)
11002 *loc_after_labels = statement_location;
11004 /* Look for an expression-statement instead. */
11005 statement = cp_parser_expression_statement (parser, in_statement_expr);
11007 /* Handle [[fallthrough]];. */
11008 if (attribute_fallthrough_p (std_attrs))
11010 /* The next token after the fallthrough attribute is ';'. */
11011 if (statement == NULL_TREE)
11013 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11014 statement = build_call_expr_internal_loc (statement_location,
11015 IFN_FALLTHROUGH,
11016 void_type_node, 0);
11017 finish_expr_stmt (statement);
11019 else
11020 warning_at (statement_location, OPT_Wattributes,
11021 "%<fallthrough%> attribute not followed by %<;%>");
11022 std_attrs = NULL_TREE;
11026 /* Set the line number for the statement. */
11027 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11028 SET_EXPR_LOCATION (statement, statement_location);
11030 /* Allow "[[fallthrough]];", but warn otherwise. */
11031 if (std_attrs != NULL_TREE)
11032 warning_at (attrs_location,
11033 OPT_Wattributes,
11034 "attributes at the beginning of statement are ignored");
11037 /* Append ATTR to attribute list ATTRS. */
11039 static tree
11040 attr_chainon (tree attrs, tree attr)
11042 if (attrs == error_mark_node)
11043 return error_mark_node;
11044 if (attr == error_mark_node)
11045 return error_mark_node;
11046 return chainon (attrs, attr);
11049 /* Parse the label for a labeled-statement, i.e.
11051 identifier :
11052 case constant-expression :
11053 default :
11055 GNU Extension:
11056 case constant-expression ... constant-expression : statement
11058 When a label is parsed without errors, the label is added to the
11059 parse tree by the finish_* functions, so this function doesn't
11060 have to return the label. */
11062 static void
11063 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11065 cp_token *token;
11066 tree label = NULL_TREE;
11067 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11069 /* The next token should be an identifier. */
11070 token = cp_lexer_peek_token (parser->lexer);
11071 if (token->type != CPP_NAME
11072 && token->type != CPP_KEYWORD)
11074 cp_parser_error (parser, "expected labeled-statement");
11075 return;
11078 /* Remember whether this case or a user-defined label is allowed to fall
11079 through to. */
11080 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11082 parser->colon_corrects_to_scope_p = false;
11083 switch (token->keyword)
11085 case RID_CASE:
11087 tree expr, expr_hi;
11088 cp_token *ellipsis;
11090 /* Consume the `case' token. */
11091 cp_lexer_consume_token (parser->lexer);
11092 /* Parse the constant-expression. */
11093 expr = cp_parser_constant_expression (parser);
11094 if (check_for_bare_parameter_packs (expr))
11095 expr = error_mark_node;
11097 ellipsis = cp_lexer_peek_token (parser->lexer);
11098 if (ellipsis->type == CPP_ELLIPSIS)
11100 /* Consume the `...' token. */
11101 cp_lexer_consume_token (parser->lexer);
11102 expr_hi = cp_parser_constant_expression (parser);
11103 if (check_for_bare_parameter_packs (expr_hi))
11104 expr_hi = error_mark_node;
11106 /* We don't need to emit warnings here, as the common code
11107 will do this for us. */
11109 else
11110 expr_hi = NULL_TREE;
11112 if (parser->in_switch_statement_p)
11114 tree l = finish_case_label (token->location, expr, expr_hi);
11115 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11116 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11118 else
11119 error_at (token->location,
11120 "case label %qE not within a switch statement",
11121 expr);
11123 break;
11125 case RID_DEFAULT:
11126 /* Consume the `default' token. */
11127 cp_lexer_consume_token (parser->lexer);
11129 if (parser->in_switch_statement_p)
11131 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11132 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11133 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11135 else
11136 error_at (token->location, "case label not within a switch statement");
11137 break;
11139 default:
11140 /* Anything else must be an ordinary label. */
11141 label = finish_label_stmt (cp_parser_identifier (parser));
11142 if (label && TREE_CODE (label) == LABEL_DECL)
11143 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11144 break;
11147 /* Require the `:' token. */
11148 cp_parser_require (parser, CPP_COLON, RT_COLON);
11150 /* An ordinary label may optionally be followed by attributes.
11151 However, this is only permitted if the attributes are then
11152 followed by a semicolon. This is because, for backward
11153 compatibility, when parsing
11154 lab: __attribute__ ((unused)) int i;
11155 we want the attribute to attach to "i", not "lab". */
11156 if (label != NULL_TREE
11157 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11159 tree attrs;
11160 cp_parser_parse_tentatively (parser);
11161 attrs = cp_parser_gnu_attributes_opt (parser);
11162 if (attrs == NULL_TREE
11163 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11164 cp_parser_abort_tentative_parse (parser);
11165 else if (!cp_parser_parse_definitely (parser))
11167 else
11168 attributes = attr_chainon (attributes, attrs);
11171 if (attributes != NULL_TREE)
11172 cplus_decl_attributes (&label, attributes, 0);
11174 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11177 /* Parse an expression-statement.
11179 expression-statement:
11180 expression [opt] ;
11182 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11183 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11184 indicates whether this expression-statement is part of an
11185 expression statement. */
11187 static tree
11188 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11190 tree statement = NULL_TREE;
11191 cp_token *token = cp_lexer_peek_token (parser->lexer);
11192 location_t loc = token->location;
11194 /* There might be attribute fallthrough. */
11195 tree attr = cp_parser_gnu_attributes_opt (parser);
11197 /* If the next token is a ';', then there is no expression
11198 statement. */
11199 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11201 statement = cp_parser_expression (parser);
11202 if (statement == error_mark_node
11203 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11205 cp_parser_skip_to_end_of_block_or_statement (parser);
11206 return error_mark_node;
11210 /* Handle [[fallthrough]];. */
11211 if (attribute_fallthrough_p (attr))
11213 /* The next token after the fallthrough attribute is ';'. */
11214 if (statement == NULL_TREE)
11215 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11216 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11217 void_type_node, 0);
11218 else
11219 warning_at (loc, OPT_Wattributes,
11220 "%<fallthrough%> attribute not followed by %<;%>");
11221 attr = NULL_TREE;
11224 /* Allow "[[fallthrough]];", but warn otherwise. */
11225 if (attr != NULL_TREE)
11226 warning_at (loc, OPT_Wattributes,
11227 "attributes at the beginning of statement are ignored");
11229 /* Give a helpful message for "A<T>::type t;" and the like. */
11230 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11231 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11233 if (TREE_CODE (statement) == SCOPE_REF)
11234 error_at (token->location, "need %<typename%> before %qE because "
11235 "%qT is a dependent scope",
11236 statement, TREE_OPERAND (statement, 0));
11237 else if (is_overloaded_fn (statement)
11238 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11240 /* A::A a; */
11241 tree fn = get_first_fn (statement);
11242 error_at (token->location,
11243 "%<%T::%D%> names the constructor, not the type",
11244 DECL_CONTEXT (fn), DECL_NAME (fn));
11248 /* Consume the final `;'. */
11249 cp_parser_consume_semicolon_at_end_of_statement (parser);
11251 if (in_statement_expr
11252 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11253 /* This is the final expression statement of a statement
11254 expression. */
11255 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11256 else if (statement)
11257 statement = finish_expr_stmt (statement);
11259 return statement;
11262 /* Parse a compound-statement.
11264 compound-statement:
11265 { statement-seq [opt] }
11267 GNU extension:
11269 compound-statement:
11270 { label-declaration-seq [opt] statement-seq [opt] }
11272 label-declaration-seq:
11273 label-declaration
11274 label-declaration-seq label-declaration
11276 Returns a tree representing the statement. */
11278 static tree
11279 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11280 int bcs_flags, bool function_body)
11282 tree compound_stmt;
11283 matching_braces braces;
11285 /* Consume the `{'. */
11286 if (!braces.require_open (parser))
11287 return error_mark_node;
11288 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11289 && !function_body && cxx_dialect < cxx14)
11290 pedwarn (input_location, OPT_Wpedantic,
11291 "compound-statement in %<constexpr%> function");
11292 /* Begin the compound-statement. */
11293 compound_stmt = begin_compound_stmt (bcs_flags);
11294 /* If the next keyword is `__label__' we have a label declaration. */
11295 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11296 cp_parser_label_declaration (parser);
11297 /* Parse an (optional) statement-seq. */
11298 cp_parser_statement_seq_opt (parser, in_statement_expr);
11299 /* Finish the compound-statement. */
11300 finish_compound_stmt (compound_stmt);
11301 /* Consume the `}'. */
11302 braces.require_close (parser);
11304 return compound_stmt;
11307 /* Parse an (optional) statement-seq.
11309 statement-seq:
11310 statement
11311 statement-seq [opt] statement */
11313 static void
11314 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11316 /* Scan statements until there aren't any more. */
11317 while (true)
11319 cp_token *token = cp_lexer_peek_token (parser->lexer);
11321 /* If we are looking at a `}', then we have run out of
11322 statements; the same is true if we have reached the end
11323 of file, or have stumbled upon a stray '@end'. */
11324 if (token->type == CPP_CLOSE_BRACE
11325 || token->type == CPP_EOF
11326 || token->type == CPP_PRAGMA_EOL
11327 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11328 break;
11330 /* If we are in a compound statement and find 'else' then
11331 something went wrong. */
11332 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11334 if (parser->in_statement & IN_IF_STMT)
11335 break;
11336 else
11338 token = cp_lexer_consume_token (parser->lexer);
11339 error_at (token->location, "%<else%> without a previous %<if%>");
11343 /* Parse the statement. */
11344 cp_parser_statement (parser, in_statement_expr, true, NULL);
11348 /* Return true if this is the C++20 version of range-based-for with
11349 init-statement. */
11351 static bool
11352 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11354 bool r = false;
11356 /* Save tokens so that we can put them back. */
11357 cp_lexer_save_tokens (parser->lexer);
11359 /* There has to be an unnested ; followed by an unnested :. */
11360 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11361 /*recovering=*/false,
11362 CPP_SEMICOLON,
11363 /*consume_paren=*/false) != -1)
11364 goto out;
11366 /* We found the semicolon, eat it now. */
11367 cp_lexer_consume_token (parser->lexer);
11369 /* Now look for ':' that is not nested in () or {}. */
11370 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11371 /*recovering=*/false,
11372 CPP_COLON,
11373 /*consume_paren=*/false) == -1);
11375 out:
11376 /* Roll back the tokens we skipped. */
11377 cp_lexer_rollback_tokens (parser->lexer);
11379 return r;
11382 /* Return true if we're looking at (init; cond), false otherwise. */
11384 static bool
11385 cp_parser_init_statement_p (cp_parser *parser)
11387 /* Save tokens so that we can put them back. */
11388 cp_lexer_save_tokens (parser->lexer);
11390 /* Look for ';' that is not nested in () or {}. */
11391 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11392 /*recovering=*/false,
11393 CPP_SEMICOLON,
11394 /*consume_paren=*/false);
11396 /* Roll back the tokens we skipped. */
11397 cp_lexer_rollback_tokens (parser->lexer);
11399 return ret == -1;
11402 /* Parse a selection-statement.
11404 selection-statement:
11405 if ( init-statement [opt] condition ) statement
11406 if ( init-statement [opt] condition ) statement else statement
11407 switch ( init-statement [opt] condition ) statement
11409 Returns the new IF_STMT or SWITCH_STMT.
11411 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11412 is a (possibly labeled) if statement which is not enclosed in
11413 braces and has an else clause. This is used to implement
11414 -Wparentheses.
11416 CHAIN is a vector of if-else-if conditions. This is used to implement
11417 -Wduplicated-cond. */
11419 static tree
11420 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11421 vec<tree> *chain)
11423 cp_token *token;
11424 enum rid keyword;
11425 token_indent_info guard_tinfo;
11427 if (if_p != NULL)
11428 *if_p = false;
11430 /* Peek at the next token. */
11431 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11432 guard_tinfo = get_token_indent_info (token);
11434 /* See what kind of keyword it is. */
11435 keyword = token->keyword;
11436 switch (keyword)
11438 case RID_IF:
11439 case RID_SWITCH:
11441 tree statement;
11442 tree condition;
11444 bool cx = false;
11445 if (keyword == RID_IF
11446 && cp_lexer_next_token_is_keyword (parser->lexer,
11447 RID_CONSTEXPR))
11449 cx = true;
11450 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11451 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11452 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11453 "with -std=c++17 or -std=gnu++17");
11456 /* Look for the `('. */
11457 matching_parens parens;
11458 if (!parens.require_open (parser))
11460 cp_parser_skip_to_end_of_statement (parser);
11461 return error_mark_node;
11464 /* Begin the selection-statement. */
11465 if (keyword == RID_IF)
11467 statement = begin_if_stmt ();
11468 IF_STMT_CONSTEXPR_P (statement) = cx;
11470 else
11471 statement = begin_switch_stmt ();
11473 /* Parse the optional init-statement. */
11474 if (cp_parser_init_statement_p (parser))
11476 tree decl;
11477 if (cxx_dialect < cxx17)
11478 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11479 "init-statement in selection statements only available "
11480 "with -std=c++17 or -std=gnu++17");
11481 cp_parser_init_statement (parser, &decl);
11484 /* Parse the condition. */
11485 condition = cp_parser_condition (parser);
11486 /* Look for the `)'. */
11487 if (!parens.require_close (parser))
11488 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11489 /*consume_paren=*/true);
11491 if (keyword == RID_IF)
11493 bool nested_if;
11494 unsigned char in_statement;
11496 /* Add the condition. */
11497 condition = finish_if_stmt_cond (condition, statement);
11499 if (warn_duplicated_cond)
11500 warn_duplicated_cond_add_or_warn (token->location, condition,
11501 &chain);
11503 /* Parse the then-clause. */
11504 in_statement = parser->in_statement;
11505 parser->in_statement |= IN_IF_STMT;
11507 /* Outside a template, the non-selected branch of a constexpr
11508 if is a 'discarded statement', i.e. unevaluated. */
11509 bool was_discarded = in_discarded_stmt;
11510 bool discard_then = (cx && !processing_template_decl
11511 && integer_zerop (condition));
11512 if (discard_then)
11514 in_discarded_stmt = true;
11515 ++c_inhibit_evaluation_warnings;
11518 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11519 guard_tinfo);
11521 parser->in_statement = in_statement;
11523 finish_then_clause (statement);
11525 if (discard_then)
11527 THEN_CLAUSE (statement) = NULL_TREE;
11528 in_discarded_stmt = was_discarded;
11529 --c_inhibit_evaluation_warnings;
11532 /* If the next token is `else', parse the else-clause. */
11533 if (cp_lexer_next_token_is_keyword (parser->lexer,
11534 RID_ELSE))
11536 bool discard_else = (cx && !processing_template_decl
11537 && integer_nonzerop (condition));
11538 if (discard_else)
11540 in_discarded_stmt = true;
11541 ++c_inhibit_evaluation_warnings;
11544 guard_tinfo
11545 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11546 /* Consume the `else' keyword. */
11547 cp_lexer_consume_token (parser->lexer);
11548 if (warn_duplicated_cond)
11550 if (cp_lexer_next_token_is_keyword (parser->lexer,
11551 RID_IF)
11552 && chain == NULL)
11554 /* We've got "if (COND) else if (COND2)". Start
11555 the condition chain and add COND as the first
11556 element. */
11557 chain = new vec<tree> ();
11558 if (!CONSTANT_CLASS_P (condition)
11559 && !TREE_SIDE_EFFECTS (condition))
11561 /* Wrap it in a NOP_EXPR so that we can set the
11562 location of the condition. */
11563 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11564 condition);
11565 SET_EXPR_LOCATION (e, token->location);
11566 chain->safe_push (e);
11569 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11570 RID_IF))
11572 /* This is if-else without subsequent if. Zap the
11573 condition chain; we would have already warned at
11574 this point. */
11575 delete chain;
11576 chain = NULL;
11579 begin_else_clause (statement);
11580 /* Parse the else-clause. */
11581 cp_parser_implicitly_scoped_statement (parser, NULL,
11582 guard_tinfo, chain);
11584 finish_else_clause (statement);
11586 /* If we are currently parsing a then-clause, then
11587 IF_P will not be NULL. We set it to true to
11588 indicate that this if statement has an else clause.
11589 This may trigger the Wparentheses warning below
11590 when we get back up to the parent if statement. */
11591 if (if_p != NULL)
11592 *if_p = true;
11594 if (discard_else)
11596 ELSE_CLAUSE (statement) = NULL_TREE;
11597 in_discarded_stmt = was_discarded;
11598 --c_inhibit_evaluation_warnings;
11601 else
11603 /* This if statement does not have an else clause. If
11604 NESTED_IF is true, then the then-clause has an if
11605 statement which does have an else clause. We warn
11606 about the potential ambiguity. */
11607 if (nested_if)
11608 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11609 "suggest explicit braces to avoid ambiguous"
11610 " %<else%>");
11611 if (warn_duplicated_cond)
11613 /* We don't need the condition chain anymore. */
11614 delete chain;
11615 chain = NULL;
11619 /* Now we're all done with the if-statement. */
11620 finish_if_stmt (statement);
11622 else
11624 bool in_switch_statement_p;
11625 unsigned char in_statement;
11627 /* Add the condition. */
11628 finish_switch_cond (condition, statement);
11630 /* Parse the body of the switch-statement. */
11631 in_switch_statement_p = parser->in_switch_statement_p;
11632 in_statement = parser->in_statement;
11633 parser->in_switch_statement_p = true;
11634 parser->in_statement |= IN_SWITCH_STMT;
11635 cp_parser_implicitly_scoped_statement (parser, if_p,
11636 guard_tinfo);
11637 parser->in_switch_statement_p = in_switch_statement_p;
11638 parser->in_statement = in_statement;
11640 /* Now we're all done with the switch-statement. */
11641 finish_switch_stmt (statement);
11644 return statement;
11646 break;
11648 default:
11649 cp_parser_error (parser, "expected selection-statement");
11650 return error_mark_node;
11654 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11655 If we have seen at least one decl-specifier, and the next token
11656 is not a parenthesis, then we must be looking at a declaration.
11657 (After "int (" we might be looking at a functional cast.) */
11659 static void
11660 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11661 bool any_specifiers_p)
11663 if (any_specifiers_p
11664 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11665 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11666 && !cp_parser_error_occurred (parser))
11667 cp_parser_commit_to_tentative_parse (parser);
11670 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11671 The declarator shall not specify a function or an array. Returns
11672 TRUE if the declarator is valid, FALSE otherwise. */
11674 static bool
11675 cp_parser_check_condition_declarator (cp_parser* parser,
11676 cp_declarator *declarator,
11677 location_t loc)
11679 if (declarator == cp_error_declarator
11680 || function_declarator_p (declarator)
11681 || declarator->kind == cdk_array)
11683 if (declarator == cp_error_declarator)
11684 /* Already complained. */;
11685 else if (declarator->kind == cdk_array)
11686 error_at (loc, "condition declares an array");
11687 else
11688 error_at (loc, "condition declares a function");
11689 if (parser->fully_implicit_function_template_p)
11690 abort_fully_implicit_template (parser);
11691 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11692 /*or_comma=*/false,
11693 /*consume_paren=*/false);
11694 return false;
11696 else
11697 return true;
11700 /* Parse a condition.
11702 condition:
11703 expression
11704 type-specifier-seq declarator = initializer-clause
11705 type-specifier-seq declarator braced-init-list
11707 GNU Extension:
11709 condition:
11710 type-specifier-seq declarator asm-specification [opt]
11711 attributes [opt] = assignment-expression
11713 Returns the expression that should be tested. */
11715 static tree
11716 cp_parser_condition (cp_parser* parser)
11718 cp_decl_specifier_seq type_specifiers;
11719 const char *saved_message;
11720 int declares_class_or_enum;
11722 /* Try the declaration first. */
11723 cp_parser_parse_tentatively (parser);
11724 /* New types are not allowed in the type-specifier-seq for a
11725 condition. */
11726 saved_message = parser->type_definition_forbidden_message;
11727 parser->type_definition_forbidden_message
11728 = G_("types may not be defined in conditions");
11729 /* Parse the type-specifier-seq. */
11730 cp_parser_decl_specifier_seq (parser,
11731 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11732 &type_specifiers,
11733 &declares_class_or_enum);
11734 /* Restore the saved message. */
11735 parser->type_definition_forbidden_message = saved_message;
11737 cp_parser_maybe_commit_to_declaration (parser,
11738 type_specifiers.any_specifiers_p);
11740 /* If all is well, we might be looking at a declaration. */
11741 if (!cp_parser_error_occurred (parser))
11743 tree decl;
11744 tree asm_specification;
11745 tree attributes;
11746 cp_declarator *declarator;
11747 tree initializer = NULL_TREE;
11748 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11750 /* Parse the declarator. */
11751 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11752 /*ctor_dtor_or_conv_p=*/NULL,
11753 /*parenthesized_p=*/NULL,
11754 /*member_p=*/false,
11755 /*friend_p=*/false);
11756 /* Parse the attributes. */
11757 attributes = cp_parser_attributes_opt (parser);
11758 /* Parse the asm-specification. */
11759 asm_specification = cp_parser_asm_specification_opt (parser);
11760 /* If the next token is not an `=' or '{', then we might still be
11761 looking at an expression. For example:
11763 if (A(a).x)
11765 looks like a decl-specifier-seq and a declarator -- but then
11766 there is no `=', so this is an expression. */
11767 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11768 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11769 cp_parser_simulate_error (parser);
11771 /* If we did see an `=' or '{', then we are looking at a declaration
11772 for sure. */
11773 if (cp_parser_parse_definitely (parser))
11775 tree pushed_scope;
11776 bool non_constant_p = false;
11777 int flags = LOOKUP_ONLYCONVERTING;
11779 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11780 return error_mark_node;
11782 /* Create the declaration. */
11783 decl = start_decl (declarator, &type_specifiers,
11784 /*initialized_p=*/true,
11785 attributes, /*prefix_attributes=*/NULL_TREE,
11786 &pushed_scope);
11788 /* Parse the initializer. */
11789 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11791 initializer = cp_parser_braced_list (parser, &non_constant_p);
11792 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11793 flags = 0;
11795 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11797 /* Consume the `='. */
11798 cp_lexer_consume_token (parser->lexer);
11799 initializer = cp_parser_initializer_clause (parser,
11800 &non_constant_p);
11802 else
11804 cp_parser_error (parser, "expected initializer");
11805 initializer = error_mark_node;
11807 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11808 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11810 /* Process the initializer. */
11811 cp_finish_decl (decl,
11812 initializer, !non_constant_p,
11813 asm_specification,
11814 flags);
11816 if (pushed_scope)
11817 pop_scope (pushed_scope);
11819 return convert_from_reference (decl);
11822 /* If we didn't even get past the declarator successfully, we are
11823 definitely not looking at a declaration. */
11824 else
11825 cp_parser_abort_tentative_parse (parser);
11827 /* Otherwise, we are looking at an expression. */
11828 return cp_parser_expression (parser);
11831 /* Parses a for-statement or range-for-statement until the closing ')',
11832 not included. */
11834 static tree
11835 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11837 tree init, scope, decl;
11838 bool is_range_for;
11840 /* Begin the for-statement. */
11841 scope = begin_for_scope (&init);
11843 /* Parse the initialization. */
11844 is_range_for = cp_parser_init_statement (parser, &decl);
11846 if (is_range_for)
11847 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
11848 false);
11849 else
11850 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11853 static tree
11854 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11855 unsigned short unroll)
11857 /* Normal for loop */
11858 tree condition = NULL_TREE;
11859 tree expression = NULL_TREE;
11860 tree stmt;
11862 stmt = begin_for_stmt (scope, init);
11863 /* The init-statement has already been parsed in
11864 cp_parser_init_statement, so no work is needed here. */
11865 finish_init_stmt (stmt);
11867 /* If there's a condition, process it. */
11868 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11869 condition = cp_parser_condition (parser);
11870 else if (ivdep)
11872 cp_parser_error (parser, "missing loop condition in loop with "
11873 "%<GCC ivdep%> pragma");
11874 condition = error_mark_node;
11876 else if (unroll)
11878 cp_parser_error (parser, "missing loop condition in loop with "
11879 "%<GCC unroll%> pragma");
11880 condition = error_mark_node;
11882 finish_for_cond (condition, stmt, ivdep, unroll);
11883 /* Look for the `;'. */
11884 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11886 /* If there's an expression, process it. */
11887 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11888 expression = cp_parser_expression (parser);
11889 finish_for_expr (expression, stmt);
11891 return stmt;
11894 /* Tries to parse a range-based for-statement:
11896 range-based-for:
11897 decl-specifier-seq declarator : expression
11899 The decl-specifier-seq declarator and the `:' are already parsed by
11900 cp_parser_init_statement. If processing_template_decl it returns a
11901 newly created RANGE_FOR_STMT; if not, it is converted to a
11902 regular FOR_STMT. */
11904 static tree
11905 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11906 bool ivdep, unsigned short unroll, bool is_omp)
11908 tree stmt, range_expr;
11909 auto_vec <cxx_binding *, 16> bindings;
11910 auto_vec <tree, 16> names;
11911 tree decomp_first_name = NULL_TREE;
11912 unsigned int decomp_cnt = 0;
11914 /* Get the range declaration momentarily out of the way so that
11915 the range expression doesn't clash with it. */
11916 if (range_decl != error_mark_node)
11918 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11920 tree v = DECL_VALUE_EXPR (range_decl);
11921 /* For decomposition declaration get all of the corresponding
11922 declarations out of the way. */
11923 if (TREE_CODE (v) == ARRAY_REF
11924 && VAR_P (TREE_OPERAND (v, 0))
11925 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11927 tree d = range_decl;
11928 range_decl = TREE_OPERAND (v, 0);
11929 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11930 decomp_first_name = d;
11931 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11933 tree name = DECL_NAME (d);
11934 names.safe_push (name);
11935 bindings.safe_push (IDENTIFIER_BINDING (name));
11936 IDENTIFIER_BINDING (name)
11937 = IDENTIFIER_BINDING (name)->previous;
11941 if (names.is_empty ())
11943 tree name = DECL_NAME (range_decl);
11944 names.safe_push (name);
11945 bindings.safe_push (IDENTIFIER_BINDING (name));
11946 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11950 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11952 bool expr_non_constant_p;
11953 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11955 else
11956 range_expr = cp_parser_expression (parser);
11958 /* Put the range declaration(s) back into scope. */
11959 for (unsigned int i = 0; i < names.length (); i++)
11961 cxx_binding *binding = bindings[i];
11962 binding->previous = IDENTIFIER_BINDING (names[i]);
11963 IDENTIFIER_BINDING (names[i]) = binding;
11966 /* finish_omp_for has its own code for the following, so just
11967 return the range_expr instead. */
11968 if (is_omp)
11969 return range_expr;
11971 /* If in template, STMT is converted to a normal for-statement
11972 at instantiation. If not, it is done just ahead. */
11973 if (processing_template_decl)
11975 if (check_for_bare_parameter_packs (range_expr))
11976 range_expr = error_mark_node;
11977 stmt = begin_range_for_stmt (scope, init);
11978 if (ivdep)
11979 RANGE_FOR_IVDEP (stmt) = 1;
11980 if (unroll)
11981 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11982 finish_range_for_decl (stmt, range_decl, range_expr);
11983 if (!type_dependent_expression_p (range_expr)
11984 /* do_auto_deduction doesn't mess with template init-lists. */
11985 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11986 do_range_for_auto_deduction (range_decl, range_expr);
11988 else
11990 stmt = begin_for_stmt (scope, init);
11991 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11992 decomp_first_name, decomp_cnt, ivdep,
11993 unroll);
11995 return stmt;
11998 /* Subroutine of cp_convert_range_for: given the initializer expression,
11999 builds up the range temporary. */
12001 static tree
12002 build_range_temp (tree range_expr)
12004 tree range_type, range_temp;
12006 /* Find out the type deduced by the declaration
12007 `auto &&__range = range_expr'. */
12008 range_type = cp_build_reference_type (make_auto (), true);
12009 range_type = do_auto_deduction (range_type, range_expr,
12010 type_uses_auto (range_type));
12012 /* Create the __range variable. */
12013 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12014 range_type);
12015 TREE_USED (range_temp) = 1;
12016 DECL_ARTIFICIAL (range_temp) = 1;
12018 return range_temp;
12021 /* Used by cp_parser_range_for in template context: we aren't going to
12022 do a full conversion yet, but we still need to resolve auto in the
12023 type of the for-range-declaration if present. This is basically
12024 a shortcut version of cp_convert_range_for. */
12026 static void
12027 do_range_for_auto_deduction (tree decl, tree range_expr)
12029 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12030 if (auto_node)
12032 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12033 range_temp = convert_from_reference (build_range_temp (range_expr));
12034 iter_type = (cp_parser_perform_range_for_lookup
12035 (range_temp, &begin_dummy, &end_dummy));
12036 if (iter_type)
12038 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12039 iter_type);
12040 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12041 RO_UNARY_STAR,
12042 tf_warning_or_error);
12043 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12044 iter_decl, auto_node);
12049 /* Converts a range-based for-statement into a normal
12050 for-statement, as per the definition.
12052 for (RANGE_DECL : RANGE_EXPR)
12053 BLOCK
12055 should be equivalent to:
12058 auto &&__range = RANGE_EXPR;
12059 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12060 __begin != __end;
12061 ++__begin)
12063 RANGE_DECL = *__begin;
12064 BLOCK
12068 If RANGE_EXPR is an array:
12069 BEGIN_EXPR = __range
12070 END_EXPR = __range + ARRAY_SIZE(__range)
12071 Else if RANGE_EXPR has a member 'begin' or 'end':
12072 BEGIN_EXPR = __range.begin()
12073 END_EXPR = __range.end()
12074 Else:
12075 BEGIN_EXPR = begin(__range)
12076 END_EXPR = end(__range);
12078 If __range has a member 'begin' but not 'end', or vice versa, we must
12079 still use the second alternative (it will surely fail, however).
12080 When calling begin()/end() in the third alternative we must use
12081 argument dependent lookup, but always considering 'std' as an associated
12082 namespace. */
12084 tree
12085 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12086 tree decomp_first_name, unsigned int decomp_cnt,
12087 bool ivdep, unsigned short unroll)
12089 tree begin, end;
12090 tree iter_type, begin_expr, end_expr;
12091 tree condition, expression;
12093 range_expr = mark_lvalue_use (range_expr);
12095 if (range_decl == error_mark_node || range_expr == error_mark_node)
12096 /* If an error happened previously do nothing or else a lot of
12097 unhelpful errors would be issued. */
12098 begin_expr = end_expr = iter_type = error_mark_node;
12099 else
12101 tree range_temp;
12103 if (VAR_P (range_expr)
12104 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12105 /* Can't bind a reference to an array of runtime bound. */
12106 range_temp = range_expr;
12107 else
12109 range_temp = build_range_temp (range_expr);
12110 pushdecl (range_temp);
12111 cp_finish_decl (range_temp, range_expr,
12112 /*is_constant_init*/false, NULL_TREE,
12113 LOOKUP_ONLYCONVERTING);
12114 range_temp = convert_from_reference (range_temp);
12116 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12117 &begin_expr, &end_expr);
12120 /* The new for initialization statement. */
12121 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12122 iter_type);
12123 TREE_USED (begin) = 1;
12124 DECL_ARTIFICIAL (begin) = 1;
12125 pushdecl (begin);
12126 cp_finish_decl (begin, begin_expr,
12127 /*is_constant_init*/false, NULL_TREE,
12128 LOOKUP_ONLYCONVERTING);
12130 if (cxx_dialect >= cxx17)
12131 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12132 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12133 TREE_USED (end) = 1;
12134 DECL_ARTIFICIAL (end) = 1;
12135 pushdecl (end);
12136 cp_finish_decl (end, end_expr,
12137 /*is_constant_init*/false, NULL_TREE,
12138 LOOKUP_ONLYCONVERTING);
12140 finish_init_stmt (statement);
12142 /* The new for condition. */
12143 condition = build_x_binary_op (input_location, NE_EXPR,
12144 begin, ERROR_MARK,
12145 end, ERROR_MARK,
12146 NULL, tf_warning_or_error);
12147 finish_for_cond (condition, statement, ivdep, unroll);
12149 /* The new increment expression. */
12150 expression = finish_unary_op_expr (input_location,
12151 PREINCREMENT_EXPR, begin,
12152 tf_warning_or_error);
12153 finish_for_expr (expression, statement);
12155 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12156 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12158 /* The declaration is initialized with *__begin inside the loop body. */
12159 cp_finish_decl (range_decl,
12160 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12161 tf_warning_or_error),
12162 /*is_constant_init*/false, NULL_TREE,
12163 LOOKUP_ONLYCONVERTING);
12164 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12165 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12167 return statement;
12170 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12171 We need to solve both at the same time because the method used
12172 depends on the existence of members begin or end.
12173 Returns the type deduced for the iterator expression. */
12175 static tree
12176 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12178 if (error_operand_p (range))
12180 *begin = *end = error_mark_node;
12181 return error_mark_node;
12184 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12186 error ("range-based %<for%> expression of type %qT "
12187 "has incomplete type", TREE_TYPE (range));
12188 *begin = *end = error_mark_node;
12189 return error_mark_node;
12191 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12193 /* If RANGE is an array, we will use pointer arithmetic. */
12194 *begin = decay_conversion (range, tf_warning_or_error);
12195 *end = build_binary_op (input_location, PLUS_EXPR,
12196 range,
12197 array_type_nelts_top (TREE_TYPE (range)),
12198 false);
12199 return TREE_TYPE (*begin);
12201 else
12203 /* If it is not an array, we must do a bit of magic. */
12204 tree id_begin, id_end;
12205 tree member_begin, member_end;
12207 *begin = *end = error_mark_node;
12209 id_begin = get_identifier ("begin");
12210 id_end = get_identifier ("end");
12211 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12212 /*protect=*/2, /*want_type=*/false,
12213 tf_warning_or_error);
12214 member_end = lookup_member (TREE_TYPE (range), id_end,
12215 /*protect=*/2, /*want_type=*/false,
12216 tf_warning_or_error);
12218 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12220 /* Use the member functions. */
12221 *begin = cp_parser_range_for_member_function (range, id_begin);
12222 *end = cp_parser_range_for_member_function (range, id_end);
12224 else
12226 /* Use global functions with ADL. */
12227 vec<tree, va_gc> *vec;
12228 vec = make_tree_vector ();
12230 vec_safe_push (vec, range);
12232 member_begin = perform_koenig_lookup (id_begin, vec,
12233 tf_warning_or_error);
12234 *begin = finish_call_expr (member_begin, &vec, false, true,
12235 tf_warning_or_error);
12236 member_end = perform_koenig_lookup (id_end, vec,
12237 tf_warning_or_error);
12238 *end = finish_call_expr (member_end, &vec, false, true,
12239 tf_warning_or_error);
12241 release_tree_vector (vec);
12244 /* Last common checks. */
12245 if (*begin == error_mark_node || *end == error_mark_node)
12247 /* If one of the expressions is an error do no more checks. */
12248 *begin = *end = error_mark_node;
12249 return error_mark_node;
12251 else if (type_dependent_expression_p (*begin)
12252 || type_dependent_expression_p (*end))
12253 /* Can happen, when, eg, in a template context, Koenig lookup
12254 can't resolve begin/end (c++/58503). */
12255 return NULL_TREE;
12256 else
12258 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12259 /* The unqualified type of the __begin and __end temporaries should
12260 be the same, as required by the multiple auto declaration. */
12261 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12263 if (cxx_dialect >= cxx17
12264 && (build_x_binary_op (input_location, NE_EXPR,
12265 *begin, ERROR_MARK,
12266 *end, ERROR_MARK,
12267 NULL, tf_none)
12268 != error_mark_node))
12269 /* P0184R0 allows __begin and __end to have different types,
12270 but make sure they are comparable so we can give a better
12271 diagnostic. */;
12272 else
12273 error ("inconsistent begin/end types in range-based %<for%> "
12274 "statement: %qT and %qT",
12275 TREE_TYPE (*begin), TREE_TYPE (*end));
12277 return iter_type;
12282 /* Helper function for cp_parser_perform_range_for_lookup.
12283 Builds a tree for RANGE.IDENTIFIER(). */
12285 static tree
12286 cp_parser_range_for_member_function (tree range, tree identifier)
12288 tree member, res;
12289 vec<tree, va_gc> *vec;
12291 member = finish_class_member_access_expr (range, identifier,
12292 false, tf_warning_or_error);
12293 if (member == error_mark_node)
12294 return error_mark_node;
12296 vec = make_tree_vector ();
12297 res = finish_call_expr (member, &vec,
12298 /*disallow_virtual=*/false,
12299 /*koenig_p=*/false,
12300 tf_warning_or_error);
12301 release_tree_vector (vec);
12302 return res;
12305 /* Parse an iteration-statement.
12307 iteration-statement:
12308 while ( condition ) statement
12309 do statement while ( expression ) ;
12310 for ( init-statement condition [opt] ; expression [opt] )
12311 statement
12313 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12315 static tree
12316 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12317 unsigned short unroll)
12319 cp_token *token;
12320 enum rid keyword;
12321 tree statement;
12322 unsigned char in_statement;
12323 token_indent_info guard_tinfo;
12325 /* Peek at the next token. */
12326 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12327 if (!token)
12328 return error_mark_node;
12330 guard_tinfo = get_token_indent_info (token);
12332 /* Remember whether or not we are already within an iteration
12333 statement. */
12334 in_statement = parser->in_statement;
12336 /* See what kind of keyword it is. */
12337 keyword = token->keyword;
12338 switch (keyword)
12340 case RID_WHILE:
12342 tree condition;
12344 /* Begin the while-statement. */
12345 statement = begin_while_stmt ();
12346 /* Look for the `('. */
12347 matching_parens parens;
12348 parens.require_open (parser);
12349 /* Parse the condition. */
12350 condition = cp_parser_condition (parser);
12351 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12352 /* Look for the `)'. */
12353 parens.require_close (parser);
12354 /* Parse the dependent statement. */
12355 parser->in_statement = IN_ITERATION_STMT;
12356 bool prev = note_iteration_stmt_body_start ();
12357 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12358 note_iteration_stmt_body_end (prev);
12359 parser->in_statement = in_statement;
12360 /* We're done with the while-statement. */
12361 finish_while_stmt (statement);
12363 break;
12365 case RID_DO:
12367 tree expression;
12369 /* Begin the do-statement. */
12370 statement = begin_do_stmt ();
12371 /* Parse the body of the do-statement. */
12372 parser->in_statement = IN_ITERATION_STMT;
12373 bool prev = note_iteration_stmt_body_start ();
12374 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12375 note_iteration_stmt_body_end (prev);
12376 parser->in_statement = in_statement;
12377 finish_do_body (statement);
12378 /* Look for the `while' keyword. */
12379 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12380 /* Look for the `('. */
12381 matching_parens parens;
12382 parens.require_open (parser);
12383 /* Parse the expression. */
12384 expression = cp_parser_expression (parser);
12385 /* We're done with the do-statement. */
12386 finish_do_stmt (expression, statement, ivdep, unroll);
12387 /* Look for the `)'. */
12388 parens.require_close (parser);
12389 /* Look for the `;'. */
12390 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12392 break;
12394 case RID_FOR:
12396 /* Look for the `('. */
12397 matching_parens parens;
12398 parens.require_open (parser);
12400 statement = cp_parser_for (parser, ivdep, unroll);
12402 /* Look for the `)'. */
12403 parens.require_close (parser);
12405 /* Parse the body of the for-statement. */
12406 parser->in_statement = IN_ITERATION_STMT;
12407 bool prev = note_iteration_stmt_body_start ();
12408 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12409 note_iteration_stmt_body_end (prev);
12410 parser->in_statement = in_statement;
12412 /* We're done with the for-statement. */
12413 finish_for_stmt (statement);
12415 break;
12417 default:
12418 cp_parser_error (parser, "expected iteration-statement");
12419 statement = error_mark_node;
12420 break;
12423 return statement;
12426 /* Parse a init-statement or the declarator of a range-based-for.
12427 Returns true if a range-based-for declaration is seen.
12429 init-statement:
12430 expression-statement
12431 simple-declaration */
12433 static bool
12434 cp_parser_init_statement (cp_parser *parser, tree *decl)
12436 /* If the next token is a `;', then we have an empty
12437 expression-statement. Grammatically, this is also a
12438 simple-declaration, but an invalid one, because it does not
12439 declare anything. Therefore, if we did not handle this case
12440 specially, we would issue an error message about an invalid
12441 declaration. */
12442 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12444 bool is_range_for = false;
12445 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12447 /* Try to parse the init-statement. */
12448 if (cp_parser_range_based_for_with_init_p (parser))
12450 tree dummy;
12451 cp_parser_parse_tentatively (parser);
12452 /* Parse the declaration. */
12453 cp_parser_simple_declaration (parser,
12454 /*function_definition_allowed_p=*/false,
12455 &dummy);
12456 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12457 if (!cp_parser_parse_definitely (parser))
12458 /* That didn't work, try to parse it as an expression-statement. */
12459 cp_parser_expression_statement (parser, NULL_TREE);
12461 if (cxx_dialect < cxx2a)
12463 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12464 "range-based %<for%> loops with initializer only "
12465 "available with -std=c++2a or -std=gnu++2a");
12466 *decl = error_mark_node;
12470 /* A colon is used in range-based for. */
12471 parser->colon_corrects_to_scope_p = false;
12473 /* We're going to speculatively look for a declaration, falling back
12474 to an expression, if necessary. */
12475 cp_parser_parse_tentatively (parser);
12476 /* Parse the declaration. */
12477 cp_parser_simple_declaration (parser,
12478 /*function_definition_allowed_p=*/false,
12479 decl);
12480 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12481 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12483 /* It is a range-for, consume the ':'. */
12484 cp_lexer_consume_token (parser->lexer);
12485 is_range_for = true;
12486 if (cxx_dialect < cxx11)
12487 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12488 "range-based %<for%> loops only available with "
12489 "-std=c++11 or -std=gnu++11");
12491 else
12492 /* The ';' is not consumed yet because we told
12493 cp_parser_simple_declaration not to. */
12494 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12496 if (cp_parser_parse_definitely (parser))
12497 return is_range_for;
12498 /* If the tentative parse failed, then we shall need to look for an
12499 expression-statement. */
12501 /* If we are here, it is an expression-statement. */
12502 cp_parser_expression_statement (parser, NULL_TREE);
12503 return false;
12506 /* Parse a jump-statement.
12508 jump-statement:
12509 break ;
12510 continue ;
12511 return expression [opt] ;
12512 return braced-init-list ;
12513 goto identifier ;
12515 GNU extension:
12517 jump-statement:
12518 goto * expression ;
12520 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12522 static tree
12523 cp_parser_jump_statement (cp_parser* parser)
12525 tree statement = error_mark_node;
12526 cp_token *token;
12527 enum rid keyword;
12528 unsigned char in_statement;
12530 /* Peek at the next token. */
12531 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12532 if (!token)
12533 return error_mark_node;
12535 /* See what kind of keyword it is. */
12536 keyword = token->keyword;
12537 switch (keyword)
12539 case RID_BREAK:
12540 in_statement = parser->in_statement & ~IN_IF_STMT;
12541 switch (in_statement)
12543 case 0:
12544 error_at (token->location, "break statement not within loop or switch");
12545 break;
12546 default:
12547 gcc_assert ((in_statement & IN_SWITCH_STMT)
12548 || in_statement == IN_ITERATION_STMT);
12549 statement = finish_break_stmt ();
12550 if (in_statement == IN_ITERATION_STMT)
12551 break_maybe_infinite_loop ();
12552 break;
12553 case IN_OMP_BLOCK:
12554 error_at (token->location, "invalid exit from OpenMP structured block");
12555 break;
12556 case IN_OMP_FOR:
12557 error_at (token->location, "break statement used with OpenMP for loop");
12558 break;
12560 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12561 break;
12563 case RID_CONTINUE:
12564 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12566 case 0:
12567 error_at (token->location, "continue statement not within a loop");
12568 break;
12569 /* Fall through. */
12570 case IN_ITERATION_STMT:
12571 case IN_OMP_FOR:
12572 statement = finish_continue_stmt ();
12573 break;
12574 case IN_OMP_BLOCK:
12575 error_at (token->location, "invalid exit from OpenMP structured block");
12576 break;
12577 default:
12578 gcc_unreachable ();
12580 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12581 break;
12583 case RID_RETURN:
12585 tree expr;
12586 bool expr_non_constant_p;
12588 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12590 cp_lexer_set_source_position (parser->lexer);
12591 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12592 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12594 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12595 expr = cp_parser_expression (parser);
12596 else
12597 /* If the next token is a `;', then there is no
12598 expression. */
12599 expr = NULL_TREE;
12600 /* Build the return-statement. */
12601 if (current_function_auto_return_pattern && in_discarded_stmt)
12602 /* Don't deduce from a discarded return statement. */;
12603 else
12604 statement = finish_return_stmt (expr);
12605 /* Look for the final `;'. */
12606 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12608 break;
12610 case RID_GOTO:
12611 if (parser->in_function_body
12612 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12614 error ("%<goto%> in %<constexpr%> function");
12615 cp_function_chain->invalid_constexpr = true;
12618 /* Create the goto-statement. */
12619 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12621 /* Issue a warning about this use of a GNU extension. */
12622 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12623 /* Consume the '*' token. */
12624 cp_lexer_consume_token (parser->lexer);
12625 /* Parse the dependent expression. */
12626 finish_goto_stmt (cp_parser_expression (parser));
12628 else
12629 finish_goto_stmt (cp_parser_identifier (parser));
12630 /* Look for the final `;'. */
12631 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12632 break;
12634 default:
12635 cp_parser_error (parser, "expected jump-statement");
12636 break;
12639 return statement;
12642 /* Parse a declaration-statement.
12644 declaration-statement:
12645 block-declaration */
12647 static void
12648 cp_parser_declaration_statement (cp_parser* parser)
12650 void *p;
12652 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12653 p = obstack_alloc (&declarator_obstack, 0);
12655 /* Parse the block-declaration. */
12656 cp_parser_block_declaration (parser, /*statement_p=*/true);
12658 /* Free any declarators allocated. */
12659 obstack_free (&declarator_obstack, p);
12662 /* Some dependent statements (like `if (cond) statement'), are
12663 implicitly in their own scope. In other words, if the statement is
12664 a single statement (as opposed to a compound-statement), it is
12665 none-the-less treated as if it were enclosed in braces. Any
12666 declarations appearing in the dependent statement are out of scope
12667 after control passes that point. This function parses a statement,
12668 but ensures that is in its own scope, even if it is not a
12669 compound-statement.
12671 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12672 is a (possibly labeled) if statement which is not enclosed in
12673 braces and has an else clause. This is used to implement
12674 -Wparentheses.
12676 CHAIN is a vector of if-else-if conditions. This is used to implement
12677 -Wduplicated-cond.
12679 Returns the new statement. */
12681 static tree
12682 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12683 const token_indent_info &guard_tinfo,
12684 vec<tree> *chain)
12686 tree statement;
12687 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12688 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12689 token_indent_info body_tinfo
12690 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12692 if (if_p != NULL)
12693 *if_p = false;
12695 /* Mark if () ; with a special NOP_EXPR. */
12696 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12698 cp_lexer_consume_token (parser->lexer);
12699 statement = add_stmt (build_empty_stmt (body_loc));
12701 if (guard_tinfo.keyword == RID_IF
12702 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12703 warning_at (body_loc, OPT_Wempty_body,
12704 "suggest braces around empty body in an %<if%> statement");
12705 else if (guard_tinfo.keyword == RID_ELSE)
12706 warning_at (body_loc, OPT_Wempty_body,
12707 "suggest braces around empty body in an %<else%> statement");
12709 /* if a compound is opened, we simply parse the statement directly. */
12710 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12711 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12712 /* If the token is not a `{', then we must take special action. */
12713 else
12715 /* Create a compound-statement. */
12716 statement = begin_compound_stmt (0);
12717 /* Parse the dependent-statement. */
12718 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12719 &body_loc_after_labels);
12720 /* Finish the dummy compound-statement. */
12721 finish_compound_stmt (statement);
12724 token_indent_info next_tinfo
12725 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12726 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12728 if (body_loc_after_labels != UNKNOWN_LOCATION
12729 && next_tinfo.type != CPP_SEMICOLON)
12730 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12731 guard_tinfo.location, guard_tinfo.keyword);
12733 /* Return the statement. */
12734 return statement;
12737 /* For some dependent statements (like `while (cond) statement'), we
12738 have already created a scope. Therefore, even if the dependent
12739 statement is a compound-statement, we do not want to create another
12740 scope. */
12742 static void
12743 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12744 const token_indent_info &guard_tinfo)
12746 /* If the token is a `{', then we must take special action. */
12747 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12749 token_indent_info body_tinfo
12750 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12751 location_t loc_after_labels = UNKNOWN_LOCATION;
12753 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12754 &loc_after_labels);
12755 token_indent_info next_tinfo
12756 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12757 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12759 if (loc_after_labels != UNKNOWN_LOCATION
12760 && next_tinfo.type != CPP_SEMICOLON)
12761 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12762 guard_tinfo.location,
12763 guard_tinfo.keyword);
12765 else
12767 /* Avoid calling cp_parser_compound_statement, so that we
12768 don't create a new scope. Do everything else by hand. */
12769 matching_braces braces;
12770 braces.require_open (parser);
12771 /* If the next keyword is `__label__' we have a label declaration. */
12772 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12773 cp_parser_label_declaration (parser);
12774 /* Parse an (optional) statement-seq. */
12775 cp_parser_statement_seq_opt (parser, NULL_TREE);
12776 braces.require_close (parser);
12780 /* Declarations [gram.dcl.dcl] */
12782 /* Parse an optional declaration-sequence.
12784 declaration-seq:
12785 declaration
12786 declaration-seq declaration */
12788 static void
12789 cp_parser_declaration_seq_opt (cp_parser* parser)
12791 while (true)
12793 cp_token *token = cp_lexer_peek_token (parser->lexer);
12795 if (token->type == CPP_CLOSE_BRACE
12796 || token->type == CPP_EOF)
12797 break;
12798 else
12799 cp_parser_toplevel_declaration (parser);
12803 /* Parse a declaration.
12805 declaration:
12806 block-declaration
12807 function-definition
12808 template-declaration
12809 explicit-instantiation
12810 explicit-specialization
12811 linkage-specification
12812 namespace-definition
12814 C++17:
12815 deduction-guide
12817 GNU extension:
12819 declaration:
12820 __extension__ declaration */
12822 static void
12823 cp_parser_declaration (cp_parser* parser)
12825 cp_token token1;
12826 cp_token token2;
12827 int saved_pedantic;
12828 void *p;
12829 tree attributes = NULL_TREE;
12831 /* Check for the `__extension__' keyword. */
12832 if (cp_parser_extension_opt (parser, &saved_pedantic))
12834 /* Parse the qualified declaration. */
12835 cp_parser_declaration (parser);
12836 /* Restore the PEDANTIC flag. */
12837 pedantic = saved_pedantic;
12839 return;
12842 /* Try to figure out what kind of declaration is present. */
12843 token1 = *cp_lexer_peek_token (parser->lexer);
12845 if (token1.type != CPP_EOF)
12846 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12847 else
12849 token2.type = CPP_EOF;
12850 token2.keyword = RID_MAX;
12853 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12854 p = obstack_alloc (&declarator_obstack, 0);
12856 /* If the next token is `extern' and the following token is a string
12857 literal, then we have a linkage specification. */
12858 if (token1.keyword == RID_EXTERN
12859 && cp_parser_is_pure_string_literal (&token2))
12860 cp_parser_linkage_specification (parser);
12861 /* If the next token is `template', then we have either a template
12862 declaration, an explicit instantiation, or an explicit
12863 specialization. */
12864 else if (token1.keyword == RID_TEMPLATE)
12866 /* `template <>' indicates a template specialization. */
12867 if (token2.type == CPP_LESS
12868 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12869 cp_parser_explicit_specialization (parser);
12870 /* `template <' indicates a template declaration. */
12871 else if (token2.type == CPP_LESS)
12872 cp_parser_template_declaration (parser, /*member_p=*/false);
12873 /* Anything else must be an explicit instantiation. */
12874 else
12875 cp_parser_explicit_instantiation (parser);
12877 /* If the next token is `export', then we have a template
12878 declaration. */
12879 else if (token1.keyword == RID_EXPORT)
12880 cp_parser_template_declaration (parser, /*member_p=*/false);
12881 /* If the next token is `extern', 'static' or 'inline' and the one
12882 after that is `template', we have a GNU extended explicit
12883 instantiation directive. */
12884 else if (cp_parser_allow_gnu_extensions_p (parser)
12885 && (token1.keyword == RID_EXTERN
12886 || token1.keyword == RID_STATIC
12887 || token1.keyword == RID_INLINE)
12888 && token2.keyword == RID_TEMPLATE)
12889 cp_parser_explicit_instantiation (parser);
12890 /* If the next token is `namespace', check for a named or unnamed
12891 namespace definition. */
12892 else if (token1.keyword == RID_NAMESPACE
12893 && (/* A named namespace definition. */
12894 (token2.type == CPP_NAME
12895 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12896 != CPP_EQ))
12897 || (token2.type == CPP_OPEN_SQUARE
12898 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12899 == CPP_OPEN_SQUARE)
12900 /* An unnamed namespace definition. */
12901 || token2.type == CPP_OPEN_BRACE
12902 || token2.keyword == RID_ATTRIBUTE))
12903 cp_parser_namespace_definition (parser);
12904 /* An inline (associated) namespace definition. */
12905 else if (token1.keyword == RID_INLINE
12906 && token2.keyword == RID_NAMESPACE)
12907 cp_parser_namespace_definition (parser);
12908 /* Objective-C++ declaration/definition. */
12909 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12910 cp_parser_objc_declaration (parser, NULL_TREE);
12911 else if (c_dialect_objc ()
12912 && token1.keyword == RID_ATTRIBUTE
12913 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12914 cp_parser_objc_declaration (parser, attributes);
12915 /* At this point we may have a template declared by a concept
12916 introduction. */
12917 else if (flag_concepts
12918 && cp_parser_template_declaration_after_export (parser,
12919 /*member_p=*/false))
12920 /* We did. */;
12921 else
12922 /* Try to parse a block-declaration, or a function-definition. */
12923 cp_parser_block_declaration (parser, /*statement_p=*/false);
12925 /* Free any declarators allocated. */
12926 obstack_free (&declarator_obstack, p);
12929 /* Parse a namespace-scope declaration. */
12931 static void
12932 cp_parser_toplevel_declaration (cp_parser* parser)
12934 cp_token *token = cp_lexer_peek_token (parser->lexer);
12936 if (token->type == CPP_PRAGMA)
12937 /* A top-level declaration can consist solely of a #pragma. A
12938 nested declaration cannot, so this is done here and not in
12939 cp_parser_declaration. (A #pragma at block scope is
12940 handled in cp_parser_statement.) */
12941 cp_parser_pragma (parser, pragma_external, NULL);
12942 else if (token->type == CPP_SEMICOLON)
12944 /* A declaration consisting of a single semicolon is
12945 invalid. Allow it unless we're being pedantic. */
12946 cp_lexer_consume_token (parser->lexer);
12947 if (!in_system_header_at (input_location))
12948 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12950 else
12951 /* Parse the declaration itself. */
12952 cp_parser_declaration (parser);
12955 /* Parse a block-declaration.
12957 block-declaration:
12958 simple-declaration
12959 asm-definition
12960 namespace-alias-definition
12961 using-declaration
12962 using-directive
12964 GNU Extension:
12966 block-declaration:
12967 __extension__ block-declaration
12969 C++0x Extension:
12971 block-declaration:
12972 static_assert-declaration
12974 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12975 part of a declaration-statement. */
12977 static void
12978 cp_parser_block_declaration (cp_parser *parser,
12979 bool statement_p)
12981 cp_token *token1;
12982 int saved_pedantic;
12984 /* Check for the `__extension__' keyword. */
12985 if (cp_parser_extension_opt (parser, &saved_pedantic))
12987 /* Parse the qualified declaration. */
12988 cp_parser_block_declaration (parser, statement_p);
12989 /* Restore the PEDANTIC flag. */
12990 pedantic = saved_pedantic;
12992 return;
12995 /* Peek at the next token to figure out which kind of declaration is
12996 present. */
12997 token1 = cp_lexer_peek_token (parser->lexer);
12999 /* If the next keyword is `asm', we have an asm-definition. */
13000 if (token1->keyword == RID_ASM)
13002 if (statement_p)
13003 cp_parser_commit_to_tentative_parse (parser);
13004 cp_parser_asm_definition (parser);
13006 /* If the next keyword is `namespace', we have a
13007 namespace-alias-definition. */
13008 else if (token1->keyword == RID_NAMESPACE)
13009 cp_parser_namespace_alias_definition (parser);
13010 /* If the next keyword is `using', we have a
13011 using-declaration, a using-directive, or an alias-declaration. */
13012 else if (token1->keyword == RID_USING)
13014 cp_token *token2;
13016 if (statement_p)
13017 cp_parser_commit_to_tentative_parse (parser);
13018 /* If the token after `using' is `namespace', then we have a
13019 using-directive. */
13020 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13021 if (token2->keyword == RID_NAMESPACE)
13022 cp_parser_using_directive (parser);
13023 /* If the second token after 'using' is '=', then we have an
13024 alias-declaration. */
13025 else if (cxx_dialect >= cxx11
13026 && token2->type == CPP_NAME
13027 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13028 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13029 cp_parser_alias_declaration (parser);
13030 /* Otherwise, it's a using-declaration. */
13031 else
13032 cp_parser_using_declaration (parser,
13033 /*access_declaration_p=*/false);
13035 /* If the next keyword is `__label__' we have a misplaced label
13036 declaration. */
13037 else if (token1->keyword == RID_LABEL)
13039 cp_lexer_consume_token (parser->lexer);
13040 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13041 cp_parser_skip_to_end_of_statement (parser);
13042 /* If the next token is now a `;', consume it. */
13043 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13044 cp_lexer_consume_token (parser->lexer);
13046 /* If the next token is `static_assert' we have a static assertion. */
13047 else if (token1->keyword == RID_STATIC_ASSERT)
13048 cp_parser_static_assert (parser, /*member_p=*/false);
13049 /* Anything else must be a simple-declaration. */
13050 else
13051 cp_parser_simple_declaration (parser, !statement_p,
13052 /*maybe_range_for_decl*/NULL);
13055 /* Parse a simple-declaration.
13057 simple-declaration:
13058 decl-specifier-seq [opt] init-declarator-list [opt] ;
13059 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13060 brace-or-equal-initializer ;
13062 init-declarator-list:
13063 init-declarator
13064 init-declarator-list , init-declarator
13066 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13067 function-definition as a simple-declaration.
13069 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13070 parsed declaration if it is an uninitialized single declarator not followed
13071 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13072 if present, will not be consumed. */
13074 static void
13075 cp_parser_simple_declaration (cp_parser* parser,
13076 bool function_definition_allowed_p,
13077 tree *maybe_range_for_decl)
13079 cp_decl_specifier_seq decl_specifiers;
13080 int declares_class_or_enum;
13081 bool saw_declarator;
13082 location_t comma_loc = UNKNOWN_LOCATION;
13083 location_t init_loc = UNKNOWN_LOCATION;
13085 if (maybe_range_for_decl)
13086 *maybe_range_for_decl = NULL_TREE;
13088 /* Defer access checks until we know what is being declared; the
13089 checks for names appearing in the decl-specifier-seq should be
13090 done as if we were in the scope of the thing being declared. */
13091 push_deferring_access_checks (dk_deferred);
13093 /* Parse the decl-specifier-seq. We have to keep track of whether
13094 or not the decl-specifier-seq declares a named class or
13095 enumeration type, since that is the only case in which the
13096 init-declarator-list is allowed to be empty.
13098 [dcl.dcl]
13100 In a simple-declaration, the optional init-declarator-list can be
13101 omitted only when declaring a class or enumeration, that is when
13102 the decl-specifier-seq contains either a class-specifier, an
13103 elaborated-type-specifier, or an enum-specifier. */
13104 cp_parser_decl_specifier_seq (parser,
13105 CP_PARSER_FLAGS_OPTIONAL,
13106 &decl_specifiers,
13107 &declares_class_or_enum);
13108 /* We no longer need to defer access checks. */
13109 stop_deferring_access_checks ();
13111 /* In a block scope, a valid declaration must always have a
13112 decl-specifier-seq. By not trying to parse declarators, we can
13113 resolve the declaration/expression ambiguity more quickly. */
13114 if (!function_definition_allowed_p
13115 && !decl_specifiers.any_specifiers_p)
13117 cp_parser_error (parser, "expected declaration");
13118 goto done;
13121 /* If the next two tokens are both identifiers, the code is
13122 erroneous. The usual cause of this situation is code like:
13124 T t;
13126 where "T" should name a type -- but does not. */
13127 if (!decl_specifiers.any_type_specifiers_p
13128 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13130 /* If parsing tentatively, we should commit; we really are
13131 looking at a declaration. */
13132 cp_parser_commit_to_tentative_parse (parser);
13133 /* Give up. */
13134 goto done;
13137 cp_parser_maybe_commit_to_declaration (parser,
13138 decl_specifiers.any_specifiers_p);
13140 /* Look for C++17 decomposition declaration. */
13141 for (size_t n = 1; ; n++)
13142 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13143 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13144 continue;
13145 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13146 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13147 && decl_specifiers.any_specifiers_p)
13149 tree decl
13150 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13151 maybe_range_for_decl,
13152 &init_loc);
13154 /* The next token should be either a `,' or a `;'. */
13155 cp_token *token = cp_lexer_peek_token (parser->lexer);
13156 /* If it's a `;', we are done. */
13157 if (token->type == CPP_SEMICOLON)
13158 goto finish;
13159 else if (maybe_range_for_decl)
13161 if (*maybe_range_for_decl == NULL_TREE)
13162 *maybe_range_for_decl = error_mark_node;
13163 goto finish;
13165 /* Anything else is an error. */
13166 else
13168 /* If we have already issued an error message we don't need
13169 to issue another one. */
13170 if ((decl != error_mark_node
13171 && DECL_INITIAL (decl) != error_mark_node)
13172 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13173 cp_parser_error (parser, "expected %<,%> or %<;%>");
13174 /* Skip tokens until we reach the end of the statement. */
13175 cp_parser_skip_to_end_of_statement (parser);
13176 /* If the next token is now a `;', consume it. */
13177 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13178 cp_lexer_consume_token (parser->lexer);
13179 goto done;
13182 else
13183 break;
13185 tree last_type;
13186 bool auto_specifier_p;
13187 /* NULL_TREE if both variable and function declaration are allowed,
13188 error_mark_node if function declaration are not allowed and
13189 a FUNCTION_DECL that should be diagnosed if it is followed by
13190 variable declarations. */
13191 tree auto_function_declaration;
13193 last_type = NULL_TREE;
13194 auto_specifier_p
13195 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13196 auto_function_declaration = NULL_TREE;
13198 /* Keep going until we hit the `;' at the end of the simple
13199 declaration. */
13200 saw_declarator = false;
13201 while (cp_lexer_next_token_is_not (parser->lexer,
13202 CPP_SEMICOLON))
13204 cp_token *token;
13205 bool function_definition_p;
13206 tree decl;
13207 tree auto_result = NULL_TREE;
13209 if (saw_declarator)
13211 /* If we are processing next declarator, comma is expected */
13212 token = cp_lexer_peek_token (parser->lexer);
13213 gcc_assert (token->type == CPP_COMMA);
13214 cp_lexer_consume_token (parser->lexer);
13215 if (maybe_range_for_decl)
13217 *maybe_range_for_decl = error_mark_node;
13218 if (comma_loc == UNKNOWN_LOCATION)
13219 comma_loc = token->location;
13222 else
13223 saw_declarator = true;
13225 /* Parse the init-declarator. */
13226 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13227 /*checks=*/NULL,
13228 function_definition_allowed_p,
13229 /*member_p=*/false,
13230 declares_class_or_enum,
13231 &function_definition_p,
13232 maybe_range_for_decl,
13233 &init_loc,
13234 &auto_result);
13235 /* If an error occurred while parsing tentatively, exit quickly.
13236 (That usually happens when in the body of a function; each
13237 statement is treated as a declaration-statement until proven
13238 otherwise.) */
13239 if (cp_parser_error_occurred (parser))
13240 goto done;
13242 if (auto_specifier_p && cxx_dialect >= cxx14)
13244 /* If the init-declarator-list contains more than one
13245 init-declarator, they shall all form declarations of
13246 variables. */
13247 if (auto_function_declaration == NULL_TREE)
13248 auto_function_declaration
13249 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13250 else if (TREE_CODE (decl) == FUNCTION_DECL
13251 || auto_function_declaration != error_mark_node)
13253 error_at (decl_specifiers.locations[ds_type_spec],
13254 "non-variable %qD in declaration with more than one "
13255 "declarator with placeholder type",
13256 TREE_CODE (decl) == FUNCTION_DECL
13257 ? decl : auto_function_declaration);
13258 auto_function_declaration = error_mark_node;
13262 if (auto_result
13263 && (!processing_template_decl || !type_uses_auto (auto_result)))
13265 if (last_type
13266 && last_type != error_mark_node
13267 && !same_type_p (auto_result, last_type))
13269 /* If the list of declarators contains more than one declarator,
13270 the type of each declared variable is determined as described
13271 above. If the type deduced for the template parameter U is not
13272 the same in each deduction, the program is ill-formed. */
13273 error_at (decl_specifiers.locations[ds_type_spec],
13274 "inconsistent deduction for %qT: %qT and then %qT",
13275 decl_specifiers.type, last_type, auto_result);
13276 last_type = error_mark_node;
13278 else
13279 last_type = auto_result;
13282 /* Handle function definitions specially. */
13283 if (function_definition_p)
13285 /* If the next token is a `,', then we are probably
13286 processing something like:
13288 void f() {}, *p;
13290 which is erroneous. */
13291 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13293 cp_token *token = cp_lexer_peek_token (parser->lexer);
13294 error_at (token->location,
13295 "mixing"
13296 " declarations and function-definitions is forbidden");
13298 /* Otherwise, we're done with the list of declarators. */
13299 else
13301 pop_deferring_access_checks ();
13302 return;
13305 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13306 *maybe_range_for_decl = decl;
13307 /* The next token should be either a `,' or a `;'. */
13308 token = cp_lexer_peek_token (parser->lexer);
13309 /* If it's a `,', there are more declarators to come. */
13310 if (token->type == CPP_COMMA)
13311 /* will be consumed next time around */;
13312 /* If it's a `;', we are done. */
13313 else if (token->type == CPP_SEMICOLON)
13314 break;
13315 else if (maybe_range_for_decl)
13317 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13318 permerror (decl_specifiers.locations[ds_type_spec],
13319 "types may not be defined in a for-range-declaration");
13320 break;
13322 /* Anything else is an error. */
13323 else
13325 /* If we have already issued an error message we don't need
13326 to issue another one. */
13327 if ((decl != error_mark_node
13328 && DECL_INITIAL (decl) != error_mark_node)
13329 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13330 cp_parser_error (parser, "expected %<,%> or %<;%>");
13331 /* Skip tokens until we reach the end of the statement. */
13332 cp_parser_skip_to_end_of_statement (parser);
13333 /* If the next token is now a `;', consume it. */
13334 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13335 cp_lexer_consume_token (parser->lexer);
13336 goto done;
13338 /* After the first time around, a function-definition is not
13339 allowed -- even if it was OK at first. For example:
13341 int i, f() {}
13343 is not valid. */
13344 function_definition_allowed_p = false;
13347 /* Issue an error message if no declarators are present, and the
13348 decl-specifier-seq does not itself declare a class or
13349 enumeration: [dcl.dcl]/3. */
13350 if (!saw_declarator)
13352 if (cp_parser_declares_only_class_p (parser))
13354 if (!declares_class_or_enum
13355 && decl_specifiers.type
13356 && OVERLOAD_TYPE_P (decl_specifiers.type))
13357 /* Ensure an error is issued anyway when finish_decltype_type,
13358 called via cp_parser_decl_specifier_seq, returns a class or
13359 an enumeration (c++/51786). */
13360 decl_specifiers.type = NULL_TREE;
13361 shadow_tag (&decl_specifiers);
13363 /* Perform any deferred access checks. */
13364 perform_deferred_access_checks (tf_warning_or_error);
13367 /* Consume the `;'. */
13368 finish:
13369 if (!maybe_range_for_decl)
13370 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13371 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13373 if (init_loc != UNKNOWN_LOCATION)
13374 error_at (init_loc, "initializer in range-based %<for%> loop");
13375 if (comma_loc != UNKNOWN_LOCATION)
13376 error_at (comma_loc,
13377 "multiple declarations in range-based %<for%> loop");
13380 done:
13381 pop_deferring_access_checks ();
13384 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13385 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13386 initializer ; */
13388 static tree
13389 cp_parser_decomposition_declaration (cp_parser *parser,
13390 cp_decl_specifier_seq *decl_specifiers,
13391 tree *maybe_range_for_decl,
13392 location_t *init_loc)
13394 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13395 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13396 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13398 /* Parse the identifier-list. */
13399 auto_vec<cp_expr, 10> v;
13400 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13401 while (true)
13403 cp_expr e = cp_parser_identifier (parser);
13404 if (e.get_value () == error_mark_node)
13405 break;
13406 v.safe_push (e);
13407 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13408 break;
13409 cp_lexer_consume_token (parser->lexer);
13412 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13413 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13415 end_loc = UNKNOWN_LOCATION;
13416 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13417 false);
13418 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13419 cp_lexer_consume_token (parser->lexer);
13420 else
13422 cp_parser_skip_to_end_of_statement (parser);
13423 return error_mark_node;
13427 if (cxx_dialect < cxx17)
13428 pedwarn (loc, 0, "structured bindings only available with "
13429 "-std=c++17 or -std=gnu++17");
13431 tree pushed_scope;
13432 cp_declarator *declarator = make_declarator (cdk_decomp);
13433 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13434 declarator->id_loc = loc;
13435 if (ref_qual != REF_QUAL_NONE)
13436 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13437 ref_qual == REF_QUAL_RVALUE,
13438 NULL_TREE);
13439 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13440 NULL_TREE, decl_specifiers->attributes,
13441 &pushed_scope);
13442 tree orig_decl = decl;
13444 unsigned int i;
13445 cp_expr e;
13446 cp_decl_specifier_seq decl_specs;
13447 clear_decl_specs (&decl_specs);
13448 decl_specs.type = make_auto ();
13449 tree prev = decl;
13450 FOR_EACH_VEC_ELT (v, i, e)
13452 if (i == 0)
13453 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13454 else
13455 declarator->u.id.unqualified_name = e.get_value ();
13456 declarator->id_loc = e.get_location ();
13457 tree elt_pushed_scope;
13458 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13459 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13460 if (decl2 == error_mark_node)
13461 decl = error_mark_node;
13462 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13464 /* Ensure we've diagnosed redeclaration if we aren't creating
13465 a new VAR_DECL. */
13466 gcc_assert (errorcount);
13467 decl = error_mark_node;
13469 else
13470 prev = decl2;
13471 if (elt_pushed_scope)
13472 pop_scope (elt_pushed_scope);
13475 if (v.is_empty ())
13477 error_at (loc, "empty structured binding declaration");
13478 decl = error_mark_node;
13481 if (maybe_range_for_decl == NULL
13482 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13484 bool non_constant_p = false, is_direct_init = false;
13485 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13486 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13487 &non_constant_p);
13488 if (initializer == NULL_TREE
13489 || (TREE_CODE (initializer) == TREE_LIST
13490 && TREE_CHAIN (initializer))
13491 || (is_direct_init
13492 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13493 && CONSTRUCTOR_NELTS (initializer) != 1))
13495 error_at (loc, "invalid initializer for structured binding "
13496 "declaration");
13497 initializer = error_mark_node;
13500 if (decl != error_mark_node)
13502 cp_maybe_mangle_decomp (decl, prev, v.length ());
13503 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13504 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13505 cp_finish_decomp (decl, prev, v.length ());
13508 else if (decl != error_mark_node)
13510 *maybe_range_for_decl = prev;
13511 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13512 the underlying DECL. */
13513 cp_finish_decomp (decl, prev, v.length ());
13516 if (pushed_scope)
13517 pop_scope (pushed_scope);
13519 if (decl == error_mark_node && DECL_P (orig_decl))
13521 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13522 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13525 return decl;
13528 /* Parse a decl-specifier-seq.
13530 decl-specifier-seq:
13531 decl-specifier-seq [opt] decl-specifier
13532 decl-specifier attribute-specifier-seq [opt] (C++11)
13534 decl-specifier:
13535 storage-class-specifier
13536 type-specifier
13537 function-specifier
13538 friend
13539 typedef
13541 GNU Extension:
13543 decl-specifier:
13544 attributes
13546 Concepts Extension:
13548 decl-specifier:
13549 concept
13551 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13553 The parser flags FLAGS is used to control type-specifier parsing.
13555 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13556 flags:
13558 1: one of the decl-specifiers is an elaborated-type-specifier
13559 (i.e., a type declaration)
13560 2: one of the decl-specifiers is an enum-specifier or a
13561 class-specifier (i.e., a type definition)
13565 static void
13566 cp_parser_decl_specifier_seq (cp_parser* parser,
13567 cp_parser_flags flags,
13568 cp_decl_specifier_seq *decl_specs,
13569 int* declares_class_or_enum)
13571 bool constructor_possible_p = !parser->in_declarator_p;
13572 bool found_decl_spec = false;
13573 cp_token *start_token = NULL;
13574 cp_decl_spec ds;
13576 /* Clear DECL_SPECS. */
13577 clear_decl_specs (decl_specs);
13579 /* Assume no class or enumeration type is declared. */
13580 *declares_class_or_enum = 0;
13582 /* Keep reading specifiers until there are no more to read. */
13583 while (true)
13585 bool constructor_p;
13586 cp_token *token;
13587 ds = ds_last;
13589 /* Peek at the next token. */
13590 token = cp_lexer_peek_token (parser->lexer);
13592 /* Save the first token of the decl spec list for error
13593 reporting. */
13594 if (!start_token)
13595 start_token = token;
13596 /* Handle attributes. */
13597 if (cp_next_tokens_can_be_attribute_p (parser))
13599 /* Parse the attributes. */
13600 tree attrs = cp_parser_attributes_opt (parser);
13602 /* In a sequence of declaration specifiers, c++11 attributes
13603 appertain to the type that precede them. In that case
13604 [dcl.spec]/1 says:
13606 The attribute-specifier-seq affects the type only for
13607 the declaration it appears in, not other declarations
13608 involving the same type.
13610 But for now let's force the user to position the
13611 attribute either at the beginning of the declaration or
13612 after the declarator-id, which would clearly mean that it
13613 applies to the declarator. */
13614 if (cxx11_attribute_p (attrs))
13616 if (!found_decl_spec)
13617 /* The c++11 attribute is at the beginning of the
13618 declaration. It appertains to the entity being
13619 declared. */;
13620 else
13622 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13624 /* This is an attribute following a
13625 class-specifier. */
13626 if (decl_specs->type_definition_p)
13627 warn_misplaced_attr_for_class_type (token->location,
13628 decl_specs->type);
13629 attrs = NULL_TREE;
13631 else
13633 decl_specs->std_attributes
13634 = attr_chainon (decl_specs->std_attributes, attrs);
13635 if (decl_specs->locations[ds_std_attribute] == 0)
13636 decl_specs->locations[ds_std_attribute] = token->location;
13638 continue;
13642 decl_specs->attributes
13643 = attr_chainon (decl_specs->attributes, attrs);
13644 if (decl_specs->locations[ds_attribute] == 0)
13645 decl_specs->locations[ds_attribute] = token->location;
13646 continue;
13648 /* Assume we will find a decl-specifier keyword. */
13649 found_decl_spec = true;
13650 /* If the next token is an appropriate keyword, we can simply
13651 add it to the list. */
13652 switch (token->keyword)
13654 /* decl-specifier:
13655 friend
13656 constexpr */
13657 case RID_FRIEND:
13658 if (!at_class_scope_p ())
13660 gcc_rich_location richloc (token->location);
13661 richloc.add_fixit_remove ();
13662 error_at (&richloc, "%<friend%> used outside of class");
13663 cp_lexer_purge_token (parser->lexer);
13665 else
13667 ds = ds_friend;
13668 /* Consume the token. */
13669 cp_lexer_consume_token (parser->lexer);
13671 break;
13673 case RID_CONSTEXPR:
13674 ds = ds_constexpr;
13675 cp_lexer_consume_token (parser->lexer);
13676 break;
13678 case RID_CONCEPT:
13679 ds = ds_concept;
13680 cp_lexer_consume_token (parser->lexer);
13681 break;
13683 /* function-specifier:
13684 inline
13685 virtual
13686 explicit */
13687 case RID_INLINE:
13688 case RID_VIRTUAL:
13689 case RID_EXPLICIT:
13690 cp_parser_function_specifier_opt (parser, decl_specs);
13691 break;
13693 /* decl-specifier:
13694 typedef */
13695 case RID_TYPEDEF:
13696 ds = ds_typedef;
13697 /* Consume the token. */
13698 cp_lexer_consume_token (parser->lexer);
13699 /* A constructor declarator cannot appear in a typedef. */
13700 constructor_possible_p = false;
13701 /* The "typedef" keyword can only occur in a declaration; we
13702 may as well commit at this point. */
13703 cp_parser_commit_to_tentative_parse (parser);
13705 if (decl_specs->storage_class != sc_none)
13706 decl_specs->conflicting_specifiers_p = true;
13707 break;
13709 /* storage-class-specifier:
13710 auto
13711 register
13712 static
13713 extern
13714 mutable
13716 GNU Extension:
13717 thread */
13718 case RID_AUTO:
13719 if (cxx_dialect == cxx98)
13721 /* Consume the token. */
13722 cp_lexer_consume_token (parser->lexer);
13724 /* Complain about `auto' as a storage specifier, if
13725 we're complaining about C++0x compatibility. */
13726 gcc_rich_location richloc (token->location);
13727 richloc.add_fixit_remove ();
13728 warning_at (&richloc, OPT_Wc__11_compat,
13729 "%<auto%> changes meaning in C++11; "
13730 "please remove it");
13732 /* Set the storage class anyway. */
13733 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13734 token);
13736 else
13737 /* C++0x auto type-specifier. */
13738 found_decl_spec = false;
13739 break;
13741 case RID_REGISTER:
13742 case RID_STATIC:
13743 case RID_EXTERN:
13744 case RID_MUTABLE:
13745 /* Consume the token. */
13746 cp_lexer_consume_token (parser->lexer);
13747 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13748 token);
13749 break;
13750 case RID_THREAD:
13751 /* Consume the token. */
13752 ds = ds_thread;
13753 cp_lexer_consume_token (parser->lexer);
13754 break;
13756 default:
13757 /* We did not yet find a decl-specifier yet. */
13758 found_decl_spec = false;
13759 break;
13762 if (found_decl_spec
13763 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13764 && token->keyword != RID_CONSTEXPR)
13765 error ("decl-specifier invalid in condition");
13767 if (found_decl_spec
13768 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13769 && token->keyword != RID_MUTABLE
13770 && token->keyword != RID_CONSTEXPR)
13771 error_at (token->location, "%qD invalid in lambda",
13772 ridpointers[token->keyword]);
13774 if (ds != ds_last)
13775 set_and_check_decl_spec_loc (decl_specs, ds, token);
13777 /* Constructors are a special case. The `S' in `S()' is not a
13778 decl-specifier; it is the beginning of the declarator. */
13779 constructor_p
13780 = (!found_decl_spec
13781 && constructor_possible_p
13782 && (cp_parser_constructor_declarator_p
13783 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13785 /* If we don't have a DECL_SPEC yet, then we must be looking at
13786 a type-specifier. */
13787 if (!found_decl_spec && !constructor_p)
13789 int decl_spec_declares_class_or_enum;
13790 bool is_cv_qualifier;
13791 tree type_spec;
13793 type_spec
13794 = cp_parser_type_specifier (parser, flags,
13795 decl_specs,
13796 /*is_declaration=*/true,
13797 &decl_spec_declares_class_or_enum,
13798 &is_cv_qualifier);
13799 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13801 /* If this type-specifier referenced a user-defined type
13802 (a typedef, class-name, etc.), then we can't allow any
13803 more such type-specifiers henceforth.
13805 [dcl.spec]
13807 The longest sequence of decl-specifiers that could
13808 possibly be a type name is taken as the
13809 decl-specifier-seq of a declaration. The sequence shall
13810 be self-consistent as described below.
13812 [dcl.type]
13814 As a general rule, at most one type-specifier is allowed
13815 in the complete decl-specifier-seq of a declaration. The
13816 only exceptions are the following:
13818 -- const or volatile can be combined with any other
13819 type-specifier.
13821 -- signed or unsigned can be combined with char, long,
13822 short, or int.
13824 -- ..
13826 Example:
13828 typedef char* Pc;
13829 void g (const int Pc);
13831 Here, Pc is *not* part of the decl-specifier seq; it's
13832 the declarator. Therefore, once we see a type-specifier
13833 (other than a cv-qualifier), we forbid any additional
13834 user-defined types. We *do* still allow things like `int
13835 int' to be considered a decl-specifier-seq, and issue the
13836 error message later. */
13837 if (type_spec && !is_cv_qualifier)
13838 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13839 /* A constructor declarator cannot follow a type-specifier. */
13840 if (type_spec)
13842 constructor_possible_p = false;
13843 found_decl_spec = true;
13844 if (!is_cv_qualifier)
13845 decl_specs->any_type_specifiers_p = true;
13847 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
13848 error_at (token->location, "type-specifier invalid in lambda");
13852 /* If we still do not have a DECL_SPEC, then there are no more
13853 decl-specifiers. */
13854 if (!found_decl_spec)
13855 break;
13857 decl_specs->any_specifiers_p = true;
13858 /* After we see one decl-specifier, further decl-specifiers are
13859 always optional. */
13860 flags |= CP_PARSER_FLAGS_OPTIONAL;
13863 /* Don't allow a friend specifier with a class definition. */
13864 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13865 && (*declares_class_or_enum & 2))
13866 error_at (decl_specs->locations[ds_friend],
13867 "class definition may not be declared a friend");
13870 /* Parse an (optional) storage-class-specifier.
13872 storage-class-specifier:
13873 auto
13874 register
13875 static
13876 extern
13877 mutable
13879 GNU Extension:
13881 storage-class-specifier:
13882 thread
13884 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13886 static tree
13887 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13889 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13891 case RID_AUTO:
13892 if (cxx_dialect != cxx98)
13893 return NULL_TREE;
13894 /* Fall through for C++98. */
13895 gcc_fallthrough ();
13897 case RID_REGISTER:
13898 case RID_STATIC:
13899 case RID_EXTERN:
13900 case RID_MUTABLE:
13901 case RID_THREAD:
13902 /* Consume the token. */
13903 return cp_lexer_consume_token (parser->lexer)->u.value;
13905 default:
13906 return NULL_TREE;
13910 /* Parse an (optional) function-specifier.
13912 function-specifier:
13913 inline
13914 virtual
13915 explicit
13917 C++2A Extension:
13918 explicit(constant-expression)
13920 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13921 Updates DECL_SPECS, if it is non-NULL. */
13923 static tree
13924 cp_parser_function_specifier_opt (cp_parser* parser,
13925 cp_decl_specifier_seq *decl_specs)
13927 cp_token *token = cp_lexer_peek_token (parser->lexer);
13928 switch (token->keyword)
13930 case RID_INLINE:
13931 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13932 break;
13934 case RID_VIRTUAL:
13935 /* 14.5.2.3 [temp.mem]
13937 A member function template shall not be virtual. */
13938 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13939 && current_class_type)
13940 error_at (token->location, "templates may not be %<virtual%>");
13941 else
13942 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13943 break;
13945 case RID_EXPLICIT:
13947 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
13948 /* If we see '(', it's C++20 explicit(bool). */
13949 tree expr;
13950 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13952 matching_parens parens;
13953 parens.consume_open (parser);
13955 /* New types are not allowed in an explicit-specifier. */
13956 const char *saved_message
13957 = parser->type_definition_forbidden_message;
13958 parser->type_definition_forbidden_message
13959 = G_("types may not be defined in explicit-specifier");
13961 if (cxx_dialect < cxx2a)
13962 pedwarn (token->location, 0,
13963 "%<explicit(bool)%> only available with -std=c++2a "
13964 "or -std=gnu++2a");
13966 /* Parse the constant-expression. */
13967 expr = cp_parser_constant_expression (parser);
13969 /* Restore the saved message. */
13970 parser->type_definition_forbidden_message = saved_message;
13971 parens.require_close (parser);
13973 else
13974 /* The explicit-specifier explicit without a constant-expression is
13975 equivalent to the explicit-specifier explicit(true). */
13976 expr = boolean_true_node;
13978 /* [dcl.fct.spec]
13979 "the constant-expression, if supplied, shall be a contextually
13980 converted constant expression of type bool." */
13981 expr = build_explicit_specifier (expr, tf_warning_or_error);
13982 /* We could evaluate it -- mark the decl as appropriate. */
13983 if (expr == boolean_true_node)
13984 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13985 else if (expr == boolean_false_node)
13986 /* Don't mark the decl as explicit. */;
13987 else if (decl_specs)
13988 /* The expression was value-dependent. Remember it so that we can
13989 substitute it later. */
13990 decl_specs->explicit_specifier = expr;
13991 return id;
13994 default:
13995 return NULL_TREE;
13998 /* Consume the token. */
13999 return cp_lexer_consume_token (parser->lexer)->u.value;
14002 /* Parse a linkage-specification.
14004 linkage-specification:
14005 extern string-literal { declaration-seq [opt] }
14006 extern string-literal declaration */
14008 static void
14009 cp_parser_linkage_specification (cp_parser* parser)
14011 tree linkage;
14013 /* Look for the `extern' keyword. */
14014 cp_token *extern_token
14015 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14017 /* Look for the string-literal. */
14018 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14019 linkage = cp_parser_string_literal (parser, false, false);
14021 /* Transform the literal into an identifier. If the literal is a
14022 wide-character string, or contains embedded NULs, then we can't
14023 handle it as the user wants. */
14024 if (strlen (TREE_STRING_POINTER (linkage))
14025 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14027 cp_parser_error (parser, "invalid linkage-specification");
14028 /* Assume C++ linkage. */
14029 linkage = lang_name_cplusplus;
14031 else
14032 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14034 /* We're now using the new linkage. */
14035 push_lang_context (linkage);
14037 /* Preserve the location of the the innermost linkage specification,
14038 tracking the locations of nested specifications via a local. */
14039 location_t saved_location
14040 = parser->innermost_linkage_specification_location;
14041 /* Construct a location ranging from the start of the "extern" to
14042 the end of the string-literal, with the caret at the start, e.g.:
14043 extern "C" {
14044 ^~~~~~~~~~
14046 parser->innermost_linkage_specification_location
14047 = make_location (extern_token->location,
14048 extern_token->location,
14049 get_finish (string_token->location));
14051 /* If the next token is a `{', then we're using the first
14052 production. */
14053 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14055 cp_ensure_no_omp_declare_simd (parser);
14056 cp_ensure_no_oacc_routine (parser);
14058 /* Consume the `{' token. */
14059 matching_braces braces;
14060 braces.consume_open (parser)->location;
14061 /* Parse the declarations. */
14062 cp_parser_declaration_seq_opt (parser);
14063 /* Look for the closing `}'. */
14064 braces.require_close (parser);
14066 /* Otherwise, there's just one declaration. */
14067 else
14069 bool saved_in_unbraced_linkage_specification_p;
14071 saved_in_unbraced_linkage_specification_p
14072 = parser->in_unbraced_linkage_specification_p;
14073 parser->in_unbraced_linkage_specification_p = true;
14074 cp_parser_declaration (parser);
14075 parser->in_unbraced_linkage_specification_p
14076 = saved_in_unbraced_linkage_specification_p;
14079 /* We're done with the linkage-specification. */
14080 pop_lang_context ();
14082 /* Restore location of parent linkage specification, if any. */
14083 parser->innermost_linkage_specification_location = saved_location;
14086 /* Parse a static_assert-declaration.
14088 static_assert-declaration:
14089 static_assert ( constant-expression , string-literal ) ;
14090 static_assert ( constant-expression ) ; (C++17)
14092 If MEMBER_P, this static_assert is a class member. */
14094 static void
14095 cp_parser_static_assert(cp_parser *parser, bool member_p)
14097 cp_expr condition;
14098 location_t token_loc;
14099 tree message;
14100 bool dummy;
14102 /* Peek at the `static_assert' token so we can keep track of exactly
14103 where the static assertion started. */
14104 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14106 /* Look for the `static_assert' keyword. */
14107 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14108 RT_STATIC_ASSERT))
14109 return;
14111 /* We know we are in a static assertion; commit to any tentative
14112 parse. */
14113 if (cp_parser_parsing_tentatively (parser))
14114 cp_parser_commit_to_tentative_parse (parser);
14116 /* Parse the `(' starting the static assertion condition. */
14117 matching_parens parens;
14118 parens.require_open (parser);
14120 /* Parse the constant-expression. Allow a non-constant expression
14121 here in order to give better diagnostics in finish_static_assert. */
14122 condition =
14123 cp_parser_constant_expression (parser,
14124 /*allow_non_constant_p=*/true,
14125 /*non_constant_p=*/&dummy);
14127 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14129 if (cxx_dialect < cxx17)
14130 pedwarn (input_location, OPT_Wpedantic,
14131 "static_assert without a message "
14132 "only available with -std=c++17 or -std=gnu++17");
14133 /* Eat the ')' */
14134 cp_lexer_consume_token (parser->lexer);
14135 message = build_string (1, "");
14136 TREE_TYPE (message) = char_array_type_node;
14137 fix_string_type (message);
14139 else
14141 /* Parse the separating `,'. */
14142 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14144 /* Parse the string-literal message. */
14145 message = cp_parser_string_literal (parser,
14146 /*translate=*/false,
14147 /*wide_ok=*/true);
14149 /* A `)' completes the static assertion. */
14150 if (!parens.require_close (parser))
14151 cp_parser_skip_to_closing_parenthesis (parser,
14152 /*recovering=*/true,
14153 /*or_comma=*/false,
14154 /*consume_paren=*/true);
14157 /* A semicolon terminates the declaration. */
14158 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14160 /* Get the location for the static assertion. Use that of the
14161 condition if available, otherwise, use that of the "static_assert"
14162 token. */
14163 location_t assert_loc = condition.get_location ();
14164 if (assert_loc == UNKNOWN_LOCATION)
14165 assert_loc = token_loc;
14167 /* Complete the static assertion, which may mean either processing
14168 the static assert now or saving it for template instantiation. */
14169 finish_static_assert (condition, message, assert_loc, member_p);
14172 /* Parse the expression in decltype ( expression ). */
14174 static tree
14175 cp_parser_decltype_expr (cp_parser *parser,
14176 bool &id_expression_or_member_access_p)
14178 cp_token *id_expr_start_token;
14179 tree expr;
14181 /* Since we're going to preserve any side-effects from this parse, set up a
14182 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14183 in the expression. */
14184 tentative_firewall firewall (parser);
14186 /* First, try parsing an id-expression. */
14187 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14188 cp_parser_parse_tentatively (parser);
14189 expr = cp_parser_id_expression (parser,
14190 /*template_keyword_p=*/false,
14191 /*check_dependency_p=*/true,
14192 /*template_p=*/NULL,
14193 /*declarator_p=*/false,
14194 /*optional_p=*/false);
14196 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14198 bool non_integral_constant_expression_p = false;
14199 tree id_expression = expr;
14200 cp_id_kind idk;
14201 const char *error_msg;
14203 if (identifier_p (expr))
14204 /* Lookup the name we got back from the id-expression. */
14205 expr = cp_parser_lookup_name_simple (parser, expr,
14206 id_expr_start_token->location);
14208 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14209 /* A template without args is not a complete id-expression. */
14210 expr = error_mark_node;
14212 if (expr
14213 && expr != error_mark_node
14214 && TREE_CODE (expr) != TYPE_DECL
14215 && (TREE_CODE (expr) != BIT_NOT_EXPR
14216 || !TYPE_P (TREE_OPERAND (expr, 0)))
14217 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14219 /* Complete lookup of the id-expression. */
14220 expr = (finish_id_expression
14221 (id_expression, expr, parser->scope, &idk,
14222 /*integral_constant_expression_p=*/false,
14223 /*allow_non_integral_constant_expression_p=*/true,
14224 &non_integral_constant_expression_p,
14225 /*template_p=*/false,
14226 /*done=*/true,
14227 /*address_p=*/false,
14228 /*template_arg_p=*/false,
14229 &error_msg,
14230 id_expr_start_token->location));
14232 if (expr == error_mark_node)
14233 /* We found an id-expression, but it was something that we
14234 should not have found. This is an error, not something
14235 we can recover from, so note that we found an
14236 id-expression and we'll recover as gracefully as
14237 possible. */
14238 id_expression_or_member_access_p = true;
14241 if (expr
14242 && expr != error_mark_node
14243 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14244 /* We have an id-expression. */
14245 id_expression_or_member_access_p = true;
14248 if (!id_expression_or_member_access_p)
14250 /* Abort the id-expression parse. */
14251 cp_parser_abort_tentative_parse (parser);
14253 /* Parsing tentatively, again. */
14254 cp_parser_parse_tentatively (parser);
14256 /* Parse a class member access. */
14257 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14258 /*cast_p=*/false, /*decltype*/true,
14259 /*member_access_only_p=*/true, NULL);
14261 if (expr
14262 && expr != error_mark_node
14263 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14264 /* We have an id-expression. */
14265 id_expression_or_member_access_p = true;
14268 if (id_expression_or_member_access_p)
14269 /* We have parsed the complete id-expression or member access. */
14270 cp_parser_parse_definitely (parser);
14271 else
14273 /* Abort our attempt to parse an id-expression or member access
14274 expression. */
14275 cp_parser_abort_tentative_parse (parser);
14277 /* Commit to the tentative_firewall so we get syntax errors. */
14278 cp_parser_commit_to_tentative_parse (parser);
14280 /* Parse a full expression. */
14281 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14282 /*decltype_p=*/true);
14285 return expr;
14288 /* Parse a `decltype' type. Returns the type.
14290 simple-type-specifier:
14291 decltype ( expression )
14292 C++14 proposal:
14293 decltype ( auto ) */
14295 static tree
14296 cp_parser_decltype (cp_parser *parser)
14298 bool id_expression_or_member_access_p = false;
14299 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14301 if (start_token->type == CPP_DECLTYPE)
14303 /* Already parsed. */
14304 cp_lexer_consume_token (parser->lexer);
14305 return saved_checks_value (start_token->u.tree_check_value);
14308 /* Look for the `decltype' token. */
14309 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14310 return error_mark_node;
14312 /* Parse the opening `('. */
14313 matching_parens parens;
14314 if (!parens.require_open (parser))
14315 return error_mark_node;
14317 push_deferring_access_checks (dk_deferred);
14319 tree expr = NULL_TREE;
14321 if (cxx_dialect >= cxx14
14322 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14323 /* decltype (auto) */
14324 cp_lexer_consume_token (parser->lexer);
14325 else
14327 /* decltype (expression) */
14329 /* Types cannot be defined in a `decltype' expression. Save away the
14330 old message and set the new one. */
14331 const char *saved_message = parser->type_definition_forbidden_message;
14332 parser->type_definition_forbidden_message
14333 = G_("types may not be defined in %<decltype%> expressions");
14335 /* The restrictions on constant-expressions do not apply inside
14336 decltype expressions. */
14337 bool saved_integral_constant_expression_p
14338 = parser->integral_constant_expression_p;
14339 bool saved_non_integral_constant_expression_p
14340 = parser->non_integral_constant_expression_p;
14341 parser->integral_constant_expression_p = false;
14343 /* Within a parenthesized expression, a `>' token is always
14344 the greater-than operator. */
14345 bool saved_greater_than_is_operator_p
14346 = parser->greater_than_is_operator_p;
14347 parser->greater_than_is_operator_p = true;
14349 /* Do not actually evaluate the expression. */
14350 ++cp_unevaluated_operand;
14352 /* Do not warn about problems with the expression. */
14353 ++c_inhibit_evaluation_warnings;
14355 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14357 /* Go back to evaluating expressions. */
14358 --cp_unevaluated_operand;
14359 --c_inhibit_evaluation_warnings;
14361 /* The `>' token might be the end of a template-id or
14362 template-parameter-list now. */
14363 parser->greater_than_is_operator_p
14364 = saved_greater_than_is_operator_p;
14366 /* Restore the old message and the integral constant expression
14367 flags. */
14368 parser->type_definition_forbidden_message = saved_message;
14369 parser->integral_constant_expression_p
14370 = saved_integral_constant_expression_p;
14371 parser->non_integral_constant_expression_p
14372 = saved_non_integral_constant_expression_p;
14375 /* Parse to the closing `)'. */
14376 if (!parens.require_close (parser))
14378 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14379 /*consume_paren=*/true);
14380 pop_deferring_access_checks ();
14381 return error_mark_node;
14384 if (!expr)
14386 /* Build auto. */
14387 expr = make_decltype_auto ();
14388 AUTO_IS_DECLTYPE (expr) = true;
14390 else
14391 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14392 tf_warning_or_error);
14394 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14395 it again. */
14396 start_token->type = CPP_DECLTYPE;
14397 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14398 start_token->u.tree_check_value->value = expr;
14399 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14400 start_token->keyword = RID_MAX;
14401 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14403 pop_to_parent_deferring_access_checks ();
14405 return expr;
14408 /* Special member functions [gram.special] */
14410 /* Parse a conversion-function-id.
14412 conversion-function-id:
14413 operator conversion-type-id
14415 Returns an IDENTIFIER_NODE representing the operator. */
14417 static tree
14418 cp_parser_conversion_function_id (cp_parser* parser)
14420 tree type;
14421 tree saved_scope;
14422 tree saved_qualifying_scope;
14423 tree saved_object_scope;
14424 tree pushed_scope = NULL_TREE;
14426 /* Look for the `operator' token. */
14427 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14428 return error_mark_node;
14429 /* When we parse the conversion-type-id, the current scope will be
14430 reset. However, we need that information in able to look up the
14431 conversion function later, so we save it here. */
14432 saved_scope = parser->scope;
14433 saved_qualifying_scope = parser->qualifying_scope;
14434 saved_object_scope = parser->object_scope;
14435 /* We must enter the scope of the class so that the names of
14436 entities declared within the class are available in the
14437 conversion-type-id. For example, consider:
14439 struct S {
14440 typedef int I;
14441 operator I();
14444 S::operator I() { ... }
14446 In order to see that `I' is a type-name in the definition, we
14447 must be in the scope of `S'. */
14448 if (saved_scope)
14449 pushed_scope = push_scope (saved_scope);
14450 /* Parse the conversion-type-id. */
14451 type = cp_parser_conversion_type_id (parser);
14452 /* Leave the scope of the class, if any. */
14453 if (pushed_scope)
14454 pop_scope (pushed_scope);
14455 /* Restore the saved scope. */
14456 parser->scope = saved_scope;
14457 parser->qualifying_scope = saved_qualifying_scope;
14458 parser->object_scope = saved_object_scope;
14459 /* If the TYPE is invalid, indicate failure. */
14460 if (type == error_mark_node)
14461 return error_mark_node;
14462 return make_conv_op_name (type);
14465 /* Parse a conversion-type-id:
14467 conversion-type-id:
14468 type-specifier-seq conversion-declarator [opt]
14470 Returns the TYPE specified. */
14472 static tree
14473 cp_parser_conversion_type_id (cp_parser* parser)
14475 tree attributes;
14476 cp_decl_specifier_seq type_specifiers;
14477 cp_declarator *declarator;
14478 tree type_specified;
14479 const char *saved_message;
14481 /* Parse the attributes. */
14482 attributes = cp_parser_attributes_opt (parser);
14484 saved_message = parser->type_definition_forbidden_message;
14485 parser->type_definition_forbidden_message
14486 = G_("types may not be defined in a conversion-type-id");
14488 /* Parse the type-specifiers. */
14489 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14490 /*is_trailing_return=*/false,
14491 &type_specifiers);
14493 parser->type_definition_forbidden_message = saved_message;
14495 /* If that didn't work, stop. */
14496 if (type_specifiers.type == error_mark_node)
14497 return error_mark_node;
14498 /* Parse the conversion-declarator. */
14499 declarator = cp_parser_conversion_declarator_opt (parser);
14501 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14502 /*initialized=*/0, &attributes);
14503 if (attributes)
14504 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14506 /* Don't give this error when parsing tentatively. This happens to
14507 work because we always parse this definitively once. */
14508 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14509 && type_uses_auto (type_specified))
14511 if (cxx_dialect < cxx14)
14513 error ("invalid use of %<auto%> in conversion operator");
14514 return error_mark_node;
14516 else if (template_parm_scope_p ())
14517 warning (0, "use of %<auto%> in member template "
14518 "conversion operator can never be deduced");
14521 return type_specified;
14524 /* Parse an (optional) conversion-declarator.
14526 conversion-declarator:
14527 ptr-operator conversion-declarator [opt]
14531 static cp_declarator *
14532 cp_parser_conversion_declarator_opt (cp_parser* parser)
14534 enum tree_code code;
14535 tree class_type, std_attributes = NULL_TREE;
14536 cp_cv_quals cv_quals;
14538 /* We don't know if there's a ptr-operator next, or not. */
14539 cp_parser_parse_tentatively (parser);
14540 /* Try the ptr-operator. */
14541 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14542 &std_attributes);
14543 /* If it worked, look for more conversion-declarators. */
14544 if (cp_parser_parse_definitely (parser))
14546 cp_declarator *declarator;
14548 /* Parse another optional declarator. */
14549 declarator = cp_parser_conversion_declarator_opt (parser);
14551 declarator = cp_parser_make_indirect_declarator
14552 (code, class_type, cv_quals, declarator, std_attributes);
14554 return declarator;
14557 return NULL;
14560 /* Parse an (optional) ctor-initializer.
14562 ctor-initializer:
14563 : mem-initializer-list */
14565 static void
14566 cp_parser_ctor_initializer_opt (cp_parser* parser)
14568 /* If the next token is not a `:', then there is no
14569 ctor-initializer. */
14570 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14572 /* Do default initialization of any bases and members. */
14573 if (DECL_CONSTRUCTOR_P (current_function_decl))
14574 finish_mem_initializers (NULL_TREE);
14575 return;
14578 /* Consume the `:' token. */
14579 cp_lexer_consume_token (parser->lexer);
14580 /* And the mem-initializer-list. */
14581 cp_parser_mem_initializer_list (parser);
14584 /* Parse a mem-initializer-list.
14586 mem-initializer-list:
14587 mem-initializer ... [opt]
14588 mem-initializer ... [opt] , mem-initializer-list */
14590 static void
14591 cp_parser_mem_initializer_list (cp_parser* parser)
14593 tree mem_initializer_list = NULL_TREE;
14594 tree target_ctor = error_mark_node;
14595 cp_token *token = cp_lexer_peek_token (parser->lexer);
14597 /* Let the semantic analysis code know that we are starting the
14598 mem-initializer-list. */
14599 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14600 error_at (token->location,
14601 "only constructors take member initializers");
14603 /* Loop through the list. */
14604 while (true)
14606 tree mem_initializer;
14608 token = cp_lexer_peek_token (parser->lexer);
14609 /* Parse the mem-initializer. */
14610 mem_initializer = cp_parser_mem_initializer (parser);
14611 /* If the next token is a `...', we're expanding member initializers. */
14612 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14613 if (ellipsis
14614 || (mem_initializer != error_mark_node
14615 && check_for_bare_parameter_packs (TREE_PURPOSE
14616 (mem_initializer))))
14618 /* Consume the `...'. */
14619 if (ellipsis)
14620 cp_lexer_consume_token (parser->lexer);
14622 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14623 can be expanded but members cannot. */
14624 if (mem_initializer != error_mark_node
14625 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14627 error_at (token->location,
14628 "cannot expand initializer for member %qD",
14629 TREE_PURPOSE (mem_initializer));
14630 mem_initializer = error_mark_node;
14633 /* Construct the pack expansion type. */
14634 if (mem_initializer != error_mark_node)
14635 mem_initializer = make_pack_expansion (mem_initializer);
14637 if (target_ctor != error_mark_node
14638 && mem_initializer != error_mark_node)
14640 error ("mem-initializer for %qD follows constructor delegation",
14641 TREE_PURPOSE (mem_initializer));
14642 mem_initializer = error_mark_node;
14644 /* Look for a target constructor. */
14645 if (mem_initializer != error_mark_node
14646 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14647 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14649 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14650 if (mem_initializer_list)
14652 error ("constructor delegation follows mem-initializer for %qD",
14653 TREE_PURPOSE (mem_initializer_list));
14654 mem_initializer = error_mark_node;
14656 target_ctor = mem_initializer;
14658 /* Add it to the list, unless it was erroneous. */
14659 if (mem_initializer != error_mark_node)
14661 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14662 mem_initializer_list = mem_initializer;
14664 /* If the next token is not a `,', we're done. */
14665 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14666 break;
14667 /* Consume the `,' token. */
14668 cp_lexer_consume_token (parser->lexer);
14671 /* Perform semantic analysis. */
14672 if (DECL_CONSTRUCTOR_P (current_function_decl))
14673 finish_mem_initializers (mem_initializer_list);
14676 /* Parse a mem-initializer.
14678 mem-initializer:
14679 mem-initializer-id ( expression-list [opt] )
14680 mem-initializer-id braced-init-list
14682 GNU extension:
14684 mem-initializer:
14685 ( expression-list [opt] )
14687 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14688 class) or FIELD_DECL (for a non-static data member) to initialize;
14689 the TREE_VALUE is the expression-list. An empty initialization
14690 list is represented by void_list_node. */
14692 static tree
14693 cp_parser_mem_initializer (cp_parser* parser)
14695 tree mem_initializer_id;
14696 tree expression_list;
14697 tree member;
14698 cp_token *token = cp_lexer_peek_token (parser->lexer);
14700 /* Find out what is being initialized. */
14701 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14703 permerror (token->location,
14704 "anachronistic old-style base class initializer");
14705 mem_initializer_id = NULL_TREE;
14707 else
14709 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14710 if (mem_initializer_id == error_mark_node)
14711 return mem_initializer_id;
14713 member = expand_member_init (mem_initializer_id);
14714 if (member && !DECL_P (member))
14715 in_base_initializer = 1;
14717 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14719 bool expr_non_constant_p;
14720 cp_lexer_set_source_position (parser->lexer);
14721 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14722 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14723 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14724 expression_list = build_tree_list (NULL_TREE, expression_list);
14726 else
14728 vec<tree, va_gc> *vec;
14729 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14730 /*cast_p=*/false,
14731 /*allow_expansion_p=*/true,
14732 /*non_constant_p=*/NULL);
14733 if (vec == NULL)
14734 return error_mark_node;
14735 expression_list = build_tree_list_vec (vec);
14736 release_tree_vector (vec);
14739 if (expression_list == error_mark_node)
14740 return error_mark_node;
14741 if (!expression_list)
14742 expression_list = void_type_node;
14744 in_base_initializer = 0;
14746 return member ? build_tree_list (member, expression_list) : error_mark_node;
14749 /* Parse a mem-initializer-id.
14751 mem-initializer-id:
14752 :: [opt] nested-name-specifier [opt] class-name
14753 decltype-specifier (C++11)
14754 identifier
14756 Returns a TYPE indicating the class to be initialized for the first
14757 production (and the second in C++11). Returns an IDENTIFIER_NODE
14758 indicating the data member to be initialized for the last production. */
14760 static tree
14761 cp_parser_mem_initializer_id (cp_parser* parser)
14763 bool global_scope_p;
14764 bool nested_name_specifier_p;
14765 bool template_p = false;
14766 tree id;
14768 cp_token *token = cp_lexer_peek_token (parser->lexer);
14770 /* `typename' is not allowed in this context ([temp.res]). */
14771 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14773 error_at (token->location,
14774 "keyword %<typename%> not allowed in this context (a qualified "
14775 "member initializer is implicitly a type)");
14776 cp_lexer_consume_token (parser->lexer);
14778 /* Look for the optional `::' operator. */
14779 global_scope_p
14780 = (cp_parser_global_scope_opt (parser,
14781 /*current_scope_valid_p=*/false)
14782 != NULL_TREE);
14783 /* Look for the optional nested-name-specifier. The simplest way to
14784 implement:
14786 [temp.res]
14788 The keyword `typename' is not permitted in a base-specifier or
14789 mem-initializer; in these contexts a qualified name that
14790 depends on a template-parameter is implicitly assumed to be a
14791 type name.
14793 is to assume that we have seen the `typename' keyword at this
14794 point. */
14795 nested_name_specifier_p
14796 = (cp_parser_nested_name_specifier_opt (parser,
14797 /*typename_keyword_p=*/true,
14798 /*check_dependency_p=*/true,
14799 /*type_p=*/true,
14800 /*is_declaration=*/true)
14801 != NULL_TREE);
14802 if (nested_name_specifier_p)
14803 template_p = cp_parser_optional_template_keyword (parser);
14804 /* If there is a `::' operator or a nested-name-specifier, then we
14805 are definitely looking for a class-name. */
14806 if (global_scope_p || nested_name_specifier_p)
14807 return cp_parser_class_name (parser,
14808 /*typename_keyword_p=*/true,
14809 /*template_keyword_p=*/template_p,
14810 typename_type,
14811 /*check_dependency_p=*/true,
14812 /*class_head_p=*/false,
14813 /*is_declaration=*/true);
14814 /* Otherwise, we could also be looking for an ordinary identifier. */
14815 cp_parser_parse_tentatively (parser);
14816 if (cp_lexer_next_token_is_decltype (parser->lexer))
14817 /* Try a decltype-specifier. */
14818 id = cp_parser_decltype (parser);
14819 else
14820 /* Otherwise, try a class-name. */
14821 id = cp_parser_class_name (parser,
14822 /*typename_keyword_p=*/true,
14823 /*template_keyword_p=*/false,
14824 none_type,
14825 /*check_dependency_p=*/true,
14826 /*class_head_p=*/false,
14827 /*is_declaration=*/true);
14828 /* If we found one, we're done. */
14829 if (cp_parser_parse_definitely (parser))
14830 return id;
14831 /* Otherwise, look for an ordinary identifier. */
14832 return cp_parser_identifier (parser);
14835 /* Overloading [gram.over] */
14837 /* Parse an operator-function-id.
14839 operator-function-id:
14840 operator operator
14842 Returns an IDENTIFIER_NODE for the operator which is a
14843 human-readable spelling of the identifier, e.g., `operator +'. */
14845 static cp_expr
14846 cp_parser_operator_function_id (cp_parser* parser)
14848 /* Look for the `operator' keyword. */
14849 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14850 return error_mark_node;
14851 /* And then the name of the operator itself. */
14852 return cp_parser_operator (parser);
14855 /* Return an identifier node for a user-defined literal operator.
14856 The suffix identifier is chained to the operator name identifier. */
14858 tree
14859 cp_literal_operator_id (const char* name)
14861 tree identifier;
14862 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14863 + strlen (name) + 10);
14864 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14865 identifier = get_identifier (buffer);
14867 return identifier;
14870 /* Parse an operator.
14872 operator:
14873 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14874 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14875 || ++ -- , ->* -> () []
14877 GNU Extensions:
14879 operator:
14880 <? >? <?= >?=
14882 Returns an IDENTIFIER_NODE for the operator which is a
14883 human-readable spelling of the identifier, e.g., `operator +'. */
14885 static cp_expr
14886 cp_parser_operator (cp_parser* parser)
14888 tree id = NULL_TREE;
14889 cp_token *token;
14890 bool utf8 = false;
14892 /* Peek at the next token. */
14893 token = cp_lexer_peek_token (parser->lexer);
14895 location_t start_loc = token->location;
14897 /* Figure out which operator we have. */
14898 enum tree_code op = ERROR_MARK;
14899 bool assop = false;
14900 bool consumed = false;
14901 switch (token->type)
14903 case CPP_KEYWORD:
14905 /* The keyword should be either `new' or `delete'. */
14906 if (token->keyword == RID_NEW)
14907 op = NEW_EXPR;
14908 else if (token->keyword == RID_DELETE)
14909 op = DELETE_EXPR;
14910 else
14911 break;
14913 /* Consume the `new' or `delete' token. */
14914 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14916 /* Peek at the next token. */
14917 token = cp_lexer_peek_token (parser->lexer);
14918 /* If it's a `[' token then this is the array variant of the
14919 operator. */
14920 if (token->type == CPP_OPEN_SQUARE)
14922 /* Consume the `[' token. */
14923 cp_lexer_consume_token (parser->lexer);
14924 /* Look for the `]' token. */
14925 if (cp_token *close_token
14926 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14927 end_loc = close_token->location;
14928 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14930 start_loc = make_location (start_loc, start_loc, end_loc);
14931 consumed = true;
14932 break;
14935 case CPP_PLUS:
14936 op = PLUS_EXPR;
14937 break;
14939 case CPP_MINUS:
14940 op = MINUS_EXPR;
14941 break;
14943 case CPP_MULT:
14944 op = MULT_EXPR;
14945 break;
14947 case CPP_DIV:
14948 op = TRUNC_DIV_EXPR;
14949 break;
14951 case CPP_MOD:
14952 op = TRUNC_MOD_EXPR;
14953 break;
14955 case CPP_XOR:
14956 op = BIT_XOR_EXPR;
14957 break;
14959 case CPP_AND:
14960 op = BIT_AND_EXPR;
14961 break;
14963 case CPP_OR:
14964 op = BIT_IOR_EXPR;
14965 break;
14967 case CPP_COMPL:
14968 op = BIT_NOT_EXPR;
14969 break;
14971 case CPP_NOT:
14972 op = TRUTH_NOT_EXPR;
14973 break;
14975 case CPP_EQ:
14976 assop = true;
14977 op = NOP_EXPR;
14978 break;
14980 case CPP_LESS:
14981 op = LT_EXPR;
14982 break;
14984 case CPP_GREATER:
14985 op = GT_EXPR;
14986 break;
14988 case CPP_PLUS_EQ:
14989 assop = true;
14990 op = PLUS_EXPR;
14991 break;
14993 case CPP_MINUS_EQ:
14994 assop = true;
14995 op = MINUS_EXPR;
14996 break;
14998 case CPP_MULT_EQ:
14999 assop = true;
15000 op = MULT_EXPR;
15001 break;
15003 case CPP_DIV_EQ:
15004 assop = true;
15005 op = TRUNC_DIV_EXPR;
15006 break;
15008 case CPP_MOD_EQ:
15009 assop = true;
15010 op = TRUNC_MOD_EXPR;
15011 break;
15013 case CPP_XOR_EQ:
15014 assop = true;
15015 op = BIT_XOR_EXPR;
15016 break;
15018 case CPP_AND_EQ:
15019 assop = true;
15020 op = BIT_AND_EXPR;
15021 break;
15023 case CPP_OR_EQ:
15024 assop = true;
15025 op = BIT_IOR_EXPR;
15026 break;
15028 case CPP_LSHIFT:
15029 op = LSHIFT_EXPR;
15030 break;
15032 case CPP_RSHIFT:
15033 op = RSHIFT_EXPR;
15034 break;
15036 case CPP_LSHIFT_EQ:
15037 assop = true;
15038 op = LSHIFT_EXPR;
15039 break;
15041 case CPP_RSHIFT_EQ:
15042 assop = true;
15043 op = RSHIFT_EXPR;
15044 break;
15046 case CPP_EQ_EQ:
15047 op = EQ_EXPR;
15048 break;
15050 case CPP_NOT_EQ:
15051 op = NE_EXPR;
15052 break;
15054 case CPP_LESS_EQ:
15055 op = LE_EXPR;
15056 break;
15058 case CPP_GREATER_EQ:
15059 op = GE_EXPR;
15060 break;
15062 case CPP_AND_AND:
15063 op = TRUTH_ANDIF_EXPR;
15064 break;
15066 case CPP_OR_OR:
15067 op = TRUTH_ORIF_EXPR;
15068 break;
15070 case CPP_PLUS_PLUS:
15071 op = POSTINCREMENT_EXPR;
15072 break;
15074 case CPP_MINUS_MINUS:
15075 op = PREDECREMENT_EXPR;
15076 break;
15078 case CPP_COMMA:
15079 op = COMPOUND_EXPR;
15080 break;
15082 case CPP_DEREF_STAR:
15083 op = MEMBER_REF;
15084 break;
15086 case CPP_DEREF:
15087 op = COMPONENT_REF;
15088 break;
15090 case CPP_OPEN_PAREN:
15092 /* Consume the `('. */
15093 matching_parens parens;
15094 parens.consume_open (parser);
15095 /* Look for the matching `)'. */
15096 parens.require_close (parser);
15097 op = CALL_EXPR;
15098 consumed = true;
15099 break;
15102 case CPP_OPEN_SQUARE:
15103 /* Consume the `['. */
15104 cp_lexer_consume_token (parser->lexer);
15105 /* Look for the matching `]'. */
15106 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15107 op = ARRAY_REF;
15108 consumed = true;
15109 break;
15111 case CPP_UTF8STRING:
15112 case CPP_UTF8STRING_USERDEF:
15113 utf8 = true;
15114 /* FALLTHRU */
15115 case CPP_STRING:
15116 case CPP_WSTRING:
15117 case CPP_STRING16:
15118 case CPP_STRING32:
15119 case CPP_STRING_USERDEF:
15120 case CPP_WSTRING_USERDEF:
15121 case CPP_STRING16_USERDEF:
15122 case CPP_STRING32_USERDEF:
15124 tree str, string_tree;
15125 int sz, len;
15127 if (cxx_dialect == cxx98)
15128 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15130 /* Consume the string. */
15131 str = cp_parser_string_literal (parser, /*translate=*/true,
15132 /*wide_ok=*/true, /*lookup_udlit=*/false);
15133 if (str == error_mark_node)
15134 return error_mark_node;
15135 else if (TREE_CODE (str) == USERDEF_LITERAL)
15137 string_tree = USERDEF_LITERAL_VALUE (str);
15138 id = USERDEF_LITERAL_SUFFIX_ID (str);
15140 else
15142 string_tree = str;
15143 /* Look for the suffix identifier. */
15144 token = cp_lexer_peek_token (parser->lexer);
15145 if (token->type == CPP_NAME)
15146 id = cp_parser_identifier (parser);
15147 else if (token->type == CPP_KEYWORD)
15149 error ("unexpected keyword;"
15150 " remove space between quotes and suffix identifier");
15151 return error_mark_node;
15153 else
15155 error ("expected suffix identifier");
15156 return error_mark_node;
15159 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15160 (TREE_TYPE (TREE_TYPE (string_tree))));
15161 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15162 if (len != 0)
15164 error ("expected empty string after %<operator%> keyword");
15165 return error_mark_node;
15167 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15168 != char_type_node)
15170 error ("invalid encoding prefix in literal operator");
15171 return error_mark_node;
15173 if (id != error_mark_node)
15175 const char *name = IDENTIFIER_POINTER (id);
15176 id = cp_literal_operator_id (name);
15178 return id;
15181 default:
15182 /* Anything else is an error. */
15183 break;
15186 /* If we have selected an identifier, we need to consume the
15187 operator token. */
15188 if (op != ERROR_MARK)
15190 id = ovl_op_identifier (assop, op);
15191 if (!consumed)
15192 cp_lexer_consume_token (parser->lexer);
15194 /* Otherwise, no valid operator name was present. */
15195 else
15197 cp_parser_error (parser, "expected operator");
15198 id = error_mark_node;
15201 return cp_expr (id, start_loc);
15204 /* Parse a template-declaration.
15206 template-declaration:
15207 export [opt] template < template-parameter-list > declaration
15209 If MEMBER_P is TRUE, this template-declaration occurs within a
15210 class-specifier.
15212 The grammar rule given by the standard isn't correct. What
15213 is really meant is:
15215 template-declaration:
15216 export [opt] template-parameter-list-seq
15217 decl-specifier-seq [opt] init-declarator [opt] ;
15218 export [opt] template-parameter-list-seq
15219 function-definition
15221 template-parameter-list-seq:
15222 template-parameter-list-seq [opt]
15223 template < template-parameter-list >
15225 Concept Extensions:
15227 template-parameter-list-seq:
15228 template < template-parameter-list > requires-clause [opt]
15230 requires-clause:
15231 requires logical-or-expression */
15233 static void
15234 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15236 /* Check for `export'. */
15237 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15239 /* Consume the `export' token. */
15240 cp_lexer_consume_token (parser->lexer);
15241 /* Warn that we do not support `export'. */
15242 warning (0, "keyword %<export%> not implemented, and will be ignored");
15245 cp_parser_template_declaration_after_export (parser, member_p);
15248 /* Parse a template-parameter-list.
15250 template-parameter-list:
15251 template-parameter
15252 template-parameter-list , template-parameter
15254 Returns a TREE_LIST. Each node represents a template parameter.
15255 The nodes are connected via their TREE_CHAINs. */
15257 static tree
15258 cp_parser_template_parameter_list (cp_parser* parser)
15260 tree parameter_list = NULL_TREE;
15262 begin_template_parm_list ();
15264 /* The loop below parses the template parms. We first need to know
15265 the total number of template parms to be able to compute proper
15266 canonical types of each dependent type. So after the loop, when
15267 we know the total number of template parms,
15268 end_template_parm_list computes the proper canonical types and
15269 fixes up the dependent types accordingly. */
15270 while (true)
15272 tree parameter;
15273 bool is_non_type;
15274 bool is_parameter_pack;
15275 location_t parm_loc;
15277 /* Parse the template-parameter. */
15278 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15279 parameter = cp_parser_template_parameter (parser,
15280 &is_non_type,
15281 &is_parameter_pack);
15282 /* Add it to the list. */
15283 if (parameter != error_mark_node)
15284 parameter_list = process_template_parm (parameter_list,
15285 parm_loc,
15286 parameter,
15287 is_non_type,
15288 is_parameter_pack);
15289 else
15291 tree err_parm = build_tree_list (parameter, parameter);
15292 parameter_list = chainon (parameter_list, err_parm);
15295 /* If the next token is not a `,', we're done. */
15296 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15297 break;
15298 /* Otherwise, consume the `,' token. */
15299 cp_lexer_consume_token (parser->lexer);
15302 return end_template_parm_list (parameter_list);
15305 /* Parse a introduction-list.
15307 introduction-list:
15308 introduced-parameter
15309 introduction-list , introduced-parameter
15311 introduced-parameter:
15312 ...[opt] identifier
15314 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15315 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15316 WILDCARD_DECL will also have DECL_NAME set and token location in
15317 DECL_SOURCE_LOCATION. */
15319 static tree
15320 cp_parser_introduction_list (cp_parser *parser)
15322 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15324 while (true)
15326 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15327 if (is_pack)
15328 cp_lexer_consume_token (parser->lexer);
15330 tree identifier = cp_parser_identifier (parser);
15331 if (identifier == error_mark_node)
15332 break;
15334 /* Build placeholder. */
15335 tree parm = build_nt (WILDCARD_DECL);
15336 DECL_SOURCE_LOCATION (parm)
15337 = cp_lexer_peek_token (parser->lexer)->location;
15338 DECL_NAME (parm) = identifier;
15339 WILDCARD_PACK_P (parm) = is_pack;
15340 vec_safe_push (introduction_vec, parm);
15342 /* If the next token is not a `,', we're done. */
15343 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15344 break;
15345 /* Otherwise, consume the `,' token. */
15346 cp_lexer_consume_token (parser->lexer);
15349 /* Convert the vec into a TREE_VEC. */
15350 tree introduction_list = make_tree_vec (introduction_vec->length ());
15351 unsigned int n;
15352 tree parm;
15353 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15354 TREE_VEC_ELT (introduction_list, n) = parm;
15356 release_tree_vector (introduction_vec);
15357 return introduction_list;
15360 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15361 is an abstract declarator. */
15363 static inline cp_declarator*
15364 get_id_declarator (cp_declarator *declarator)
15366 cp_declarator *d = declarator;
15367 while (d && d->kind != cdk_id)
15368 d = d->declarator;
15369 return d;
15372 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15373 is an abstract declarator. */
15375 static inline tree
15376 get_unqualified_id (cp_declarator *declarator)
15378 declarator = get_id_declarator (declarator);
15379 if (declarator)
15380 return declarator->u.id.unqualified_name;
15381 else
15382 return NULL_TREE;
15385 /* Returns true if DECL represents a constrained-parameter. */
15387 static inline bool
15388 is_constrained_parameter (tree decl)
15390 return (decl
15391 && TREE_CODE (decl) == TYPE_DECL
15392 && CONSTRAINED_PARM_CONCEPT (decl)
15393 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15396 /* Returns true if PARM declares a constrained-parameter. */
15398 static inline bool
15399 is_constrained_parameter (cp_parameter_declarator *parm)
15401 return is_constrained_parameter (parm->decl_specifiers.type);
15404 /* Check that the type parameter is only a declarator-id, and that its
15405 type is not cv-qualified. */
15407 bool
15408 cp_parser_check_constrained_type_parm (cp_parser *parser,
15409 cp_parameter_declarator *parm)
15411 if (!parm->declarator)
15412 return true;
15414 if (parm->declarator->kind != cdk_id)
15416 cp_parser_error (parser, "invalid constrained type parameter");
15417 return false;
15420 /* Don't allow cv-qualified type parameters. */
15421 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15422 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15424 cp_parser_error (parser, "cv-qualified type parameter");
15425 return false;
15428 return true;
15431 /* Finish parsing/processing a template type parameter and checking
15432 various restrictions. */
15434 static inline tree
15435 cp_parser_constrained_type_template_parm (cp_parser *parser,
15436 tree id,
15437 cp_parameter_declarator* parmdecl)
15439 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15440 return finish_template_type_parm (class_type_node, id);
15441 else
15442 return error_mark_node;
15445 static tree
15446 finish_constrained_template_template_parm (tree proto, tree id)
15448 /* FIXME: This should probably be copied, and we may need to adjust
15449 the template parameter depths. */
15450 tree saved_parms = current_template_parms;
15451 begin_template_parm_list ();
15452 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15453 end_template_parm_list ();
15455 tree parm = finish_template_template_parm (class_type_node, id);
15456 current_template_parms = saved_parms;
15458 return parm;
15461 /* Finish parsing/processing a template template parameter by borrowing
15462 the template parameter list from the prototype parameter. */
15464 static tree
15465 cp_parser_constrained_template_template_parm (cp_parser *parser,
15466 tree proto,
15467 tree id,
15468 cp_parameter_declarator *parmdecl)
15470 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15471 return error_mark_node;
15472 return finish_constrained_template_template_parm (proto, id);
15475 /* Create a new non-type template parameter from the given PARM
15476 declarator. */
15478 static tree
15479 constrained_non_type_template_parm (bool *is_non_type,
15480 cp_parameter_declarator *parm)
15482 *is_non_type = true;
15483 cp_declarator *decl = parm->declarator;
15484 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15485 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15486 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15489 /* Build a constrained template parameter based on the PARMDECL
15490 declarator. The type of PARMDECL is the constrained type, which
15491 refers to the prototype template parameter that ultimately
15492 specifies the type of the declared parameter. */
15494 static tree
15495 finish_constrained_parameter (cp_parser *parser,
15496 cp_parameter_declarator *parmdecl,
15497 bool *is_non_type,
15498 bool *is_parameter_pack)
15500 tree decl = parmdecl->decl_specifiers.type;
15501 tree id = get_unqualified_id (parmdecl->declarator);
15502 tree def = parmdecl->default_argument;
15503 tree proto = DECL_INITIAL (decl);
15505 /* A template parameter constrained by a variadic concept shall also
15506 be declared as a template parameter pack. */
15507 bool is_variadic = template_parameter_pack_p (proto);
15508 if (is_variadic && !*is_parameter_pack)
15509 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15511 /* Build the parameter. Return an error if the declarator was invalid. */
15512 tree parm;
15513 if (TREE_CODE (proto) == TYPE_DECL)
15514 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15515 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15516 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15517 parmdecl);
15518 else
15519 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15520 if (parm == error_mark_node)
15521 return error_mark_node;
15523 /* Finish the parameter decl and create a node attaching the
15524 default argument and constraint. */
15525 parm = build_tree_list (def, parm);
15526 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15528 return parm;
15531 /* Returns true if the parsed type actually represents the declaration
15532 of a type template-parameter. */
15534 static inline bool
15535 declares_constrained_type_template_parameter (tree type)
15537 return (is_constrained_parameter (type)
15538 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15542 /* Returns true if the parsed type actually represents the declaration of
15543 a template template-parameter. */
15545 static bool
15546 declares_constrained_template_template_parameter (tree type)
15548 return (is_constrained_parameter (type)
15549 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15552 /* Parse a default argument for a type template-parameter.
15553 Note that diagnostics are handled in cp_parser_template_parameter. */
15555 static tree
15556 cp_parser_default_type_template_argument (cp_parser *parser)
15558 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15560 /* Consume the `=' token. */
15561 cp_lexer_consume_token (parser->lexer);
15563 cp_token *token = cp_lexer_peek_token (parser->lexer);
15565 /* Parse the default-argument. */
15566 push_deferring_access_checks (dk_no_deferred);
15567 tree default_argument = cp_parser_type_id (parser);
15568 pop_deferring_access_checks ();
15570 if (flag_concepts && type_uses_auto (default_argument))
15572 error_at (token->location,
15573 "invalid use of %<auto%> in default template argument");
15574 return error_mark_node;
15577 return default_argument;
15580 /* Parse a default argument for a template template-parameter. */
15582 static tree
15583 cp_parser_default_template_template_argument (cp_parser *parser)
15585 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15587 bool is_template;
15589 /* Consume the `='. */
15590 cp_lexer_consume_token (parser->lexer);
15591 /* Parse the id-expression. */
15592 push_deferring_access_checks (dk_no_deferred);
15593 /* save token before parsing the id-expression, for error
15594 reporting */
15595 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15596 tree default_argument
15597 = cp_parser_id_expression (parser,
15598 /*template_keyword_p=*/false,
15599 /*check_dependency_p=*/true,
15600 /*template_p=*/&is_template,
15601 /*declarator_p=*/false,
15602 /*optional_p=*/false);
15603 if (TREE_CODE (default_argument) == TYPE_DECL)
15604 /* If the id-expression was a template-id that refers to
15605 a template-class, we already have the declaration here,
15606 so no further lookup is needed. */
15608 else
15609 /* Look up the name. */
15610 default_argument
15611 = cp_parser_lookup_name (parser, default_argument,
15612 none_type,
15613 /*is_template=*/is_template,
15614 /*is_namespace=*/false,
15615 /*check_dependency=*/true,
15616 /*ambiguous_decls=*/NULL,
15617 token->location);
15618 /* See if the default argument is valid. */
15619 default_argument = check_template_template_default_arg (default_argument);
15620 pop_deferring_access_checks ();
15621 return default_argument;
15624 /* Parse a template-parameter.
15626 template-parameter:
15627 type-parameter
15628 parameter-declaration
15630 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15631 the parameter. The TREE_PURPOSE is the default value, if any.
15632 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15633 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15634 set to true iff this parameter is a parameter pack. */
15636 static tree
15637 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15638 bool *is_parameter_pack)
15640 cp_token *token;
15641 cp_parameter_declarator *parameter_declarator;
15642 tree parm;
15644 /* Assume it is a type parameter or a template parameter. */
15645 *is_non_type = false;
15646 /* Assume it not a parameter pack. */
15647 *is_parameter_pack = false;
15648 /* Peek at the next token. */
15649 token = cp_lexer_peek_token (parser->lexer);
15650 /* If it is `template', we have a type-parameter. */
15651 if (token->keyword == RID_TEMPLATE)
15652 return cp_parser_type_parameter (parser, is_parameter_pack);
15653 /* If it is `class' or `typename' we do not know yet whether it is a
15654 type parameter or a non-type parameter. Consider:
15656 template <typename T, typename T::X X> ...
15660 template <class C, class D*> ...
15662 Here, the first parameter is a type parameter, and the second is
15663 a non-type parameter. We can tell by looking at the token after
15664 the identifier -- if it is a `,', `=', or `>' then we have a type
15665 parameter. */
15666 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15668 /* Peek at the token after `class' or `typename'. */
15669 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15670 /* If it's an ellipsis, we have a template type parameter
15671 pack. */
15672 if (token->type == CPP_ELLIPSIS)
15673 return cp_parser_type_parameter (parser, is_parameter_pack);
15674 /* If it's an identifier, skip it. */
15675 if (token->type == CPP_NAME)
15676 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15677 /* Now, see if the token looks like the end of a template
15678 parameter. */
15679 if (token->type == CPP_COMMA
15680 || token->type == CPP_EQ
15681 || token->type == CPP_GREATER)
15682 return cp_parser_type_parameter (parser, is_parameter_pack);
15685 /* Otherwise, it is a non-type parameter or a constrained parameter.
15687 [temp.param]
15689 When parsing a default template-argument for a non-type
15690 template-parameter, the first non-nested `>' is taken as the end
15691 of the template parameter-list rather than a greater-than
15692 operator. */
15693 parameter_declarator
15694 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15695 /*parenthesized_p=*/NULL);
15697 if (!parameter_declarator)
15698 return error_mark_node;
15700 /* If the parameter declaration is marked as a parameter pack, set
15701 *IS_PARAMETER_PACK to notify the caller. */
15702 if (parameter_declarator->template_parameter_pack_p)
15703 *is_parameter_pack = true;
15705 if (parameter_declarator->default_argument)
15707 /* Can happen in some cases of erroneous input (c++/34892). */
15708 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15709 /* Consume the `...' for better error recovery. */
15710 cp_lexer_consume_token (parser->lexer);
15713 // The parameter may have been constrained.
15714 if (is_constrained_parameter (parameter_declarator))
15715 return finish_constrained_parameter (parser,
15716 parameter_declarator,
15717 is_non_type,
15718 is_parameter_pack);
15720 // Now we're sure that the parameter is a non-type parameter.
15721 *is_non_type = true;
15723 parm = grokdeclarator (parameter_declarator->declarator,
15724 &parameter_declarator->decl_specifiers,
15725 TPARM, /*initialized=*/0,
15726 /*attrlist=*/NULL);
15727 if (parm == error_mark_node)
15728 return error_mark_node;
15730 return build_tree_list (parameter_declarator->default_argument, parm);
15733 /* Parse a type-parameter.
15735 type-parameter:
15736 class identifier [opt]
15737 class identifier [opt] = type-id
15738 typename identifier [opt]
15739 typename identifier [opt] = type-id
15740 template < template-parameter-list > class identifier [opt]
15741 template < template-parameter-list > class identifier [opt]
15742 = id-expression
15744 GNU Extension (variadic templates):
15746 type-parameter:
15747 class ... identifier [opt]
15748 typename ... identifier [opt]
15750 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15751 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15752 the declaration of the parameter.
15754 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15756 static tree
15757 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15759 cp_token *token;
15760 tree parameter;
15762 /* Look for a keyword to tell us what kind of parameter this is. */
15763 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15764 if (!token)
15765 return error_mark_node;
15767 switch (token->keyword)
15769 case RID_CLASS:
15770 case RID_TYPENAME:
15772 tree identifier;
15773 tree default_argument;
15775 /* If the next token is an ellipsis, we have a template
15776 argument pack. */
15777 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15779 /* Consume the `...' token. */
15780 cp_lexer_consume_token (parser->lexer);
15781 maybe_warn_variadic_templates ();
15783 *is_parameter_pack = true;
15786 /* If the next token is an identifier, then it names the
15787 parameter. */
15788 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15789 identifier = cp_parser_identifier (parser);
15790 else
15791 identifier = NULL_TREE;
15793 /* Create the parameter. */
15794 parameter = finish_template_type_parm (class_type_node, identifier);
15796 /* If the next token is an `=', we have a default argument. */
15797 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15799 default_argument
15800 = cp_parser_default_type_template_argument (parser);
15802 /* Template parameter packs cannot have default
15803 arguments. */
15804 if (*is_parameter_pack)
15806 if (identifier)
15807 error_at (token->location,
15808 "template parameter pack %qD cannot have a "
15809 "default argument", identifier);
15810 else
15811 error_at (token->location,
15812 "template parameter packs cannot have "
15813 "default arguments");
15814 default_argument = NULL_TREE;
15816 else if (check_for_bare_parameter_packs (default_argument))
15817 default_argument = error_mark_node;
15819 else
15820 default_argument = NULL_TREE;
15822 /* Create the combined representation of the parameter and the
15823 default argument. */
15824 parameter = build_tree_list (default_argument, parameter);
15826 break;
15828 case RID_TEMPLATE:
15830 tree identifier;
15831 tree default_argument;
15833 /* Look for the `<'. */
15834 cp_parser_require (parser, CPP_LESS, RT_LESS);
15835 /* Parse the template-parameter-list. */
15836 cp_parser_template_parameter_list (parser);
15837 /* Look for the `>'. */
15838 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15840 // If template requirements are present, parse them.
15841 if (flag_concepts)
15843 tree reqs = get_shorthand_constraints (current_template_parms);
15844 if (tree r = cp_parser_requires_clause_opt (parser))
15845 reqs = conjoin_constraints (reqs, normalize_expression (r));
15846 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15849 /* Look for the `class' or 'typename' keywords. */
15850 cp_parser_type_parameter_key (parser);
15851 /* If the next token is an ellipsis, we have a template
15852 argument pack. */
15853 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15855 /* Consume the `...' token. */
15856 cp_lexer_consume_token (parser->lexer);
15857 maybe_warn_variadic_templates ();
15859 *is_parameter_pack = true;
15861 /* If the next token is an `=', then there is a
15862 default-argument. If the next token is a `>', we are at
15863 the end of the parameter-list. If the next token is a `,',
15864 then we are at the end of this parameter. */
15865 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15866 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15867 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15869 identifier = cp_parser_identifier (parser);
15870 /* Treat invalid names as if the parameter were nameless. */
15871 if (identifier == error_mark_node)
15872 identifier = NULL_TREE;
15874 else
15875 identifier = NULL_TREE;
15877 /* Create the template parameter. */
15878 parameter = finish_template_template_parm (class_type_node,
15879 identifier);
15881 /* If the next token is an `=', then there is a
15882 default-argument. */
15883 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15885 default_argument
15886 = cp_parser_default_template_template_argument (parser);
15888 /* Template parameter packs cannot have default
15889 arguments. */
15890 if (*is_parameter_pack)
15892 if (identifier)
15893 error_at (token->location,
15894 "template parameter pack %qD cannot "
15895 "have a default argument",
15896 identifier);
15897 else
15898 error_at (token->location, "template parameter packs cannot "
15899 "have default arguments");
15900 default_argument = NULL_TREE;
15903 else
15904 default_argument = NULL_TREE;
15906 /* Create the combined representation of the parameter and the
15907 default argument. */
15908 parameter = build_tree_list (default_argument, parameter);
15910 break;
15912 default:
15913 gcc_unreachable ();
15914 break;
15917 return parameter;
15920 /* Parse a template-id.
15922 template-id:
15923 template-name < template-argument-list [opt] >
15925 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15926 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15927 returned. Otherwise, if the template-name names a function, or set
15928 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15929 names a class, returns a TYPE_DECL for the specialization.
15931 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15932 uninstantiated templates. */
15934 static tree
15935 cp_parser_template_id (cp_parser *parser,
15936 bool template_keyword_p,
15937 bool check_dependency_p,
15938 enum tag_types tag_type,
15939 bool is_declaration)
15941 tree templ;
15942 tree arguments;
15943 tree template_id;
15944 cp_token_position start_of_id = 0;
15945 cp_token *next_token = NULL, *next_token_2 = NULL;
15946 bool is_identifier;
15948 /* If the next token corresponds to a template-id, there is no need
15949 to reparse it. */
15950 cp_token *token = cp_lexer_peek_token (parser->lexer);
15951 if (token->type == CPP_TEMPLATE_ID)
15953 cp_lexer_consume_token (parser->lexer);
15954 return saved_checks_value (token->u.tree_check_value);
15957 /* Avoid performing name lookup if there is no possibility of
15958 finding a template-id. */
15959 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15960 || (token->type == CPP_NAME
15961 && !cp_parser_nth_token_starts_template_argument_list_p
15962 (parser, 2)))
15964 cp_parser_error (parser, "expected template-id");
15965 return error_mark_node;
15968 /* Remember where the template-id starts. */
15969 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15970 start_of_id = cp_lexer_token_position (parser->lexer, false);
15972 push_deferring_access_checks (dk_deferred);
15974 /* Parse the template-name. */
15975 is_identifier = false;
15976 templ = cp_parser_template_name (parser, template_keyword_p,
15977 check_dependency_p,
15978 is_declaration,
15979 tag_type,
15980 &is_identifier);
15981 if (templ == error_mark_node || is_identifier)
15983 pop_deferring_access_checks ();
15984 return templ;
15987 /* Since we're going to preserve any side-effects from this parse, set up a
15988 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15989 in the template arguments. */
15990 tentative_firewall firewall (parser);
15992 /* If we find the sequence `[:' after a template-name, it's probably
15993 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15994 parse correctly the argument list. */
15995 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15996 == CPP_OPEN_SQUARE)
15997 && next_token->flags & DIGRAPH
15998 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15999 == CPP_COLON)
16000 && !(next_token_2->flags & PREV_WHITE))
16002 cp_parser_parse_tentatively (parser);
16003 /* Change `:' into `::'. */
16004 next_token_2->type = CPP_SCOPE;
16005 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16006 CPP_LESS. */
16007 cp_lexer_consume_token (parser->lexer);
16009 /* Parse the arguments. */
16010 arguments = cp_parser_enclosed_template_argument_list (parser);
16011 if (!cp_parser_parse_definitely (parser))
16013 /* If we couldn't parse an argument list, then we revert our changes
16014 and return simply an error. Maybe this is not a template-id
16015 after all. */
16016 next_token_2->type = CPP_COLON;
16017 cp_parser_error (parser, "expected %<<%>");
16018 pop_deferring_access_checks ();
16019 return error_mark_node;
16021 /* Otherwise, emit an error about the invalid digraph, but continue
16022 parsing because we got our argument list. */
16023 if (permerror (next_token->location,
16024 "%<<::%> cannot begin a template-argument list"))
16026 static bool hint = false;
16027 inform (next_token->location,
16028 "%<<:%> is an alternate spelling for %<[%>."
16029 " Insert whitespace between %<<%> and %<::%>");
16030 if (!hint && !flag_permissive)
16032 inform (next_token->location, "(if you use %<-fpermissive%> "
16033 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16034 "accept your code)");
16035 hint = true;
16039 else
16041 /* Look for the `<' that starts the template-argument-list. */
16042 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16044 pop_deferring_access_checks ();
16045 return error_mark_node;
16047 /* Parse the arguments. */
16048 arguments = cp_parser_enclosed_template_argument_list (parser);
16050 if ((cxx_dialect > cxx17)
16051 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16052 && !template_keyword_p
16053 && (cp_parser_error_occurred (parser)
16054 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16056 /* This didn't go well. */
16057 if (TREE_CODE (templ) == FUNCTION_DECL)
16059 /* C++2A says that "function-name < a;" is now ill-formed. */
16060 if (cp_parser_error_occurred (parser))
16062 error_at (token->location, "invalid template-argument-list");
16063 inform (token->location, "function name as the left hand "
16064 "operand of %<<%> is ill-formed in C++2a; wrap the "
16065 "function name in %<()%>");
16067 else
16068 /* We expect "f<targs>" to be followed by "(args)". */
16069 error_at (cp_lexer_peek_token (parser->lexer)->location,
16070 "expected %<(%> after template-argument-list");
16071 if (start_of_id)
16072 /* Purge all subsequent tokens. */
16073 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16075 else
16076 cp_parser_simulate_error (parser);
16077 pop_deferring_access_checks ();
16078 return error_mark_node;
16082 /* Set the location to be of the form:
16083 template-name < template-argument-list [opt] >
16084 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16085 with caret == start at the start of the template-name,
16086 ranging until the closing '>'. */
16087 location_t finish_loc
16088 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16089 location_t combined_loc
16090 = make_location (token->location, token->location, finish_loc);
16092 /* Check for concepts autos where they don't belong. We could
16093 identify types in some cases of idnetifier TEMPL, looking ahead
16094 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16095 types. We reject them in functions, but if what we have is an
16096 identifier, even with none_type we can't conclude it's NOT a
16097 type, we have to wait for template substitution. */
16098 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16099 template_id = error_mark_node;
16100 /* Build a representation of the specialization. */
16101 else if (identifier_p (templ))
16102 template_id = build_min_nt_loc (combined_loc,
16103 TEMPLATE_ID_EXPR,
16104 templ, arguments);
16105 else if (DECL_TYPE_TEMPLATE_P (templ)
16106 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16108 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16109 template (rather than some instantiation thereof) only if
16110 is not nested within some other construct. For example, in
16111 "template <typename T> void f(T) { A<T>::", A<T> is just an
16112 instantiation of A. */
16113 bool entering_scope
16114 = (template_parm_scope_p ()
16115 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16116 template_id
16117 = finish_template_type (templ, arguments, entering_scope);
16119 /* A template-like identifier may be a partial concept id. */
16120 else if (flag_concepts
16121 && (template_id = (cp_parser_maybe_partial_concept_id
16122 (parser, templ, arguments))))
16123 return template_id;
16124 else if (variable_template_p (templ))
16126 template_id = lookup_template_variable (templ, arguments);
16127 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16128 SET_EXPR_LOCATION (template_id, combined_loc);
16130 else
16132 /* If it's not a class-template or a template-template, it should be
16133 a function-template. */
16134 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16135 || TREE_CODE (templ) == OVERLOAD
16136 || TREE_CODE (templ) == FUNCTION_DECL
16137 || BASELINK_P (templ)));
16139 template_id = lookup_template_function (templ, arguments);
16140 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16141 SET_EXPR_LOCATION (template_id, combined_loc);
16144 /* If parsing tentatively, replace the sequence of tokens that makes
16145 up the template-id with a CPP_TEMPLATE_ID token. That way,
16146 should we re-parse the token stream, we will not have to repeat
16147 the effort required to do the parse, nor will we issue duplicate
16148 error messages about problems during instantiation of the
16149 template. */
16150 if (start_of_id
16151 /* Don't do this if we had a parse error in a declarator; re-parsing
16152 might succeed if a name changes meaning (60361). */
16153 && !(cp_parser_error_occurred (parser)
16154 && cp_parser_parsing_tentatively (parser)
16155 && parser->in_declarator_p))
16157 /* Reset the contents of the START_OF_ID token. */
16158 token->type = CPP_TEMPLATE_ID;
16159 token->location = combined_loc;
16161 /* Retrieve any deferred checks. Do not pop this access checks yet
16162 so the memory will not be reclaimed during token replacing below. */
16163 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16164 token->u.tree_check_value->value = template_id;
16165 token->u.tree_check_value->checks = get_deferred_access_checks ();
16166 token->keyword = RID_MAX;
16168 /* Purge all subsequent tokens. */
16169 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16171 /* ??? Can we actually assume that, if template_id ==
16172 error_mark_node, we will have issued a diagnostic to the
16173 user, as opposed to simply marking the tentative parse as
16174 failed? */
16175 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16176 error_at (token->location, "parse error in template argument list");
16179 pop_to_parent_deferring_access_checks ();
16180 return template_id;
16183 /* Parse a template-name.
16185 template-name:
16186 identifier
16188 The standard should actually say:
16190 template-name:
16191 identifier
16192 operator-function-id
16194 A defect report has been filed about this issue.
16196 A conversion-function-id cannot be a template name because they cannot
16197 be part of a template-id. In fact, looking at this code:
16199 a.operator K<int>()
16201 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16202 It is impossible to call a templated conversion-function-id with an
16203 explicit argument list, since the only allowed template parameter is
16204 the type to which it is converting.
16206 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16207 `template' keyword, in a construction like:
16209 T::template f<3>()
16211 In that case `f' is taken to be a template-name, even though there
16212 is no way of knowing for sure.
16214 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16215 name refers to a set of overloaded functions, at least one of which
16216 is a template, or an IDENTIFIER_NODE with the name of the template,
16217 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16218 names are looked up inside uninstantiated templates. */
16220 static tree
16221 cp_parser_template_name (cp_parser* parser,
16222 bool template_keyword_p,
16223 bool check_dependency_p,
16224 bool is_declaration,
16225 enum tag_types tag_type,
16226 bool *is_identifier)
16228 tree identifier;
16229 tree decl;
16230 cp_token *token = cp_lexer_peek_token (parser->lexer);
16232 /* If the next token is `operator', then we have either an
16233 operator-function-id or a conversion-function-id. */
16234 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16236 /* We don't know whether we're looking at an
16237 operator-function-id or a conversion-function-id. */
16238 cp_parser_parse_tentatively (parser);
16239 /* Try an operator-function-id. */
16240 identifier = cp_parser_operator_function_id (parser);
16241 /* If that didn't work, try a conversion-function-id. */
16242 if (!cp_parser_parse_definitely (parser))
16244 cp_parser_error (parser, "expected template-name");
16245 return error_mark_node;
16248 /* Look for the identifier. */
16249 else
16250 identifier = cp_parser_identifier (parser);
16252 /* If we didn't find an identifier, we don't have a template-id. */
16253 if (identifier == error_mark_node)
16254 return error_mark_node;
16256 /* If the name immediately followed the `template' keyword, then it
16257 is a template-name. However, if the next token is not `<', then
16258 we do not treat it as a template-name, since it is not being used
16259 as part of a template-id. This enables us to handle constructs
16260 like:
16262 template <typename T> struct S { S(); };
16263 template <typename T> S<T>::S();
16265 correctly. We would treat `S' as a template -- if it were `S<T>'
16266 -- but we do not if there is no `<'. */
16268 if (processing_template_decl
16269 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16271 /* In a declaration, in a dependent context, we pretend that the
16272 "template" keyword was present in order to improve error
16273 recovery. For example, given:
16275 template <typename T> void f(T::X<int>);
16277 we want to treat "X<int>" as a template-id. */
16278 if (is_declaration
16279 && !template_keyword_p
16280 && parser->scope && TYPE_P (parser->scope)
16281 && check_dependency_p
16282 && dependent_scope_p (parser->scope)
16283 /* Do not do this for dtors (or ctors), since they never
16284 need the template keyword before their name. */
16285 && !constructor_name_p (identifier, parser->scope))
16287 cp_token_position start = 0;
16289 /* Explain what went wrong. */
16290 error_at (token->location, "non-template %qD used as template",
16291 identifier);
16292 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16293 parser->scope, identifier);
16294 /* If parsing tentatively, find the location of the "<" token. */
16295 if (cp_parser_simulate_error (parser))
16296 start = cp_lexer_token_position (parser->lexer, true);
16297 /* Parse the template arguments so that we can issue error
16298 messages about them. */
16299 cp_lexer_consume_token (parser->lexer);
16300 cp_parser_enclosed_template_argument_list (parser);
16301 /* Skip tokens until we find a good place from which to
16302 continue parsing. */
16303 cp_parser_skip_to_closing_parenthesis (parser,
16304 /*recovering=*/true,
16305 /*or_comma=*/true,
16306 /*consume_paren=*/false);
16307 /* If parsing tentatively, permanently remove the
16308 template argument list. That will prevent duplicate
16309 error messages from being issued about the missing
16310 "template" keyword. */
16311 if (start)
16312 cp_lexer_purge_tokens_after (parser->lexer, start);
16313 if (is_identifier)
16314 *is_identifier = true;
16315 parser->context->object_type = NULL_TREE;
16316 return identifier;
16319 /* If the "template" keyword is present, then there is generally
16320 no point in doing name-lookup, so we just return IDENTIFIER.
16321 But, if the qualifying scope is non-dependent then we can
16322 (and must) do name-lookup normally. */
16323 if (template_keyword_p)
16325 tree scope = (parser->scope ? parser->scope
16326 : parser->context->object_type);
16327 if (scope && TYPE_P (scope)
16328 && (!CLASS_TYPE_P (scope)
16329 || (check_dependency_p && dependent_type_p (scope))))
16331 /* We're optimizing away the call to cp_parser_lookup_name, but
16332 we still need to do this. */
16333 parser->context->object_type = NULL_TREE;
16334 return identifier;
16339 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16340 const bool scoped_p = ((parser->scope ? parser->scope
16341 : parser->context->object_type) != NULL_TREE);
16343 /* Look up the name. */
16344 decl = cp_parser_lookup_name (parser, identifier,
16345 tag_type,
16346 /*is_template=*/true,
16347 /*is_namespace=*/false,
16348 check_dependency_p,
16349 /*ambiguous_decls=*/NULL,
16350 token->location);
16352 decl = strip_using_decl (decl);
16354 /* If DECL is a template, then the name was a template-name. */
16355 if (TREE_CODE (decl) == TEMPLATE_DECL)
16357 if (TREE_DEPRECATED (decl)
16358 && deprecated_state != DEPRECATED_SUPPRESS)
16359 warn_deprecated_use (decl, NULL_TREE);
16361 else
16363 /* The standard does not explicitly indicate whether a name that
16364 names a set of overloaded declarations, some of which are
16365 templates, is a template-name. However, such a name should
16366 be a template-name; otherwise, there is no way to form a
16367 template-id for the overloaded templates. */
16368 bool found = false;
16370 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16371 !found && iter; ++iter)
16372 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16373 found = true;
16375 if (!found
16376 && (cxx_dialect > cxx17)
16377 && !scoped_p
16378 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16380 /* [temp.names] says "A name is also considered to refer to a template
16381 if it is an unqualified-id followed by a < and name lookup finds
16382 either one or more functions or finds nothing." */
16384 /* The "more functions" case. Just use the OVERLOAD as normally.
16385 We don't use is_overloaded_fn here to avoid considering
16386 BASELINKs. */
16387 if (TREE_CODE (decl) == OVERLOAD
16388 /* Name lookup found one function. */
16389 || TREE_CODE (decl) == FUNCTION_DECL)
16390 found = true;
16391 /* Name lookup found nothing. */
16392 else if (decl == error_mark_node)
16393 return identifier;
16396 if (!found)
16398 /* The name does not name a template. */
16399 cp_parser_error (parser, "expected template-name");
16400 return error_mark_node;
16404 return decl;
16407 /* Parse a template-argument-list.
16409 template-argument-list:
16410 template-argument ... [opt]
16411 template-argument-list , template-argument ... [opt]
16413 Returns a TREE_VEC containing the arguments. */
16415 static tree
16416 cp_parser_template_argument_list (cp_parser* parser)
16418 tree fixed_args[10];
16419 unsigned n_args = 0;
16420 unsigned alloced = 10;
16421 tree *arg_ary = fixed_args;
16422 tree vec;
16423 bool saved_in_template_argument_list_p;
16424 bool saved_ice_p;
16425 bool saved_non_ice_p;
16427 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16428 parser->in_template_argument_list_p = true;
16429 /* Even if the template-id appears in an integral
16430 constant-expression, the contents of the argument list do
16431 not. */
16432 saved_ice_p = parser->integral_constant_expression_p;
16433 parser->integral_constant_expression_p = false;
16434 saved_non_ice_p = parser->non_integral_constant_expression_p;
16435 parser->non_integral_constant_expression_p = false;
16437 /* Parse the arguments. */
16440 tree argument;
16442 if (n_args)
16443 /* Consume the comma. */
16444 cp_lexer_consume_token (parser->lexer);
16446 /* Parse the template-argument. */
16447 argument = cp_parser_template_argument (parser);
16449 /* If the next token is an ellipsis, we're expanding a template
16450 argument pack. */
16451 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16453 if (argument == error_mark_node)
16455 cp_token *token = cp_lexer_peek_token (parser->lexer);
16456 error_at (token->location,
16457 "expected parameter pack before %<...%>");
16459 /* Consume the `...' token. */
16460 cp_lexer_consume_token (parser->lexer);
16462 /* Make the argument into a TYPE_PACK_EXPANSION or
16463 EXPR_PACK_EXPANSION. */
16464 argument = make_pack_expansion (argument);
16467 if (n_args == alloced)
16469 alloced *= 2;
16471 if (arg_ary == fixed_args)
16473 arg_ary = XNEWVEC (tree, alloced);
16474 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16476 else
16477 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16479 arg_ary[n_args++] = argument;
16481 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16483 vec = make_tree_vec (n_args);
16485 while (n_args--)
16486 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16488 if (arg_ary != fixed_args)
16489 free (arg_ary);
16490 parser->non_integral_constant_expression_p = saved_non_ice_p;
16491 parser->integral_constant_expression_p = saved_ice_p;
16492 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16493 if (CHECKING_P)
16494 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16495 return vec;
16498 /* Parse a template-argument.
16500 template-argument:
16501 assignment-expression
16502 type-id
16503 id-expression
16505 The representation is that of an assignment-expression, type-id, or
16506 id-expression -- except that the qualified id-expression is
16507 evaluated, so that the value returned is either a DECL or an
16508 OVERLOAD.
16510 Although the standard says "assignment-expression", it forbids
16511 throw-expressions or assignments in the template argument.
16512 Therefore, we use "conditional-expression" instead. */
16514 static tree
16515 cp_parser_template_argument (cp_parser* parser)
16517 tree argument;
16518 bool template_p;
16519 bool address_p;
16520 bool maybe_type_id = false;
16521 cp_token *token = NULL, *argument_start_token = NULL;
16522 location_t loc = 0;
16523 cp_id_kind idk;
16525 /* There's really no way to know what we're looking at, so we just
16526 try each alternative in order.
16528 [temp.arg]
16530 In a template-argument, an ambiguity between a type-id and an
16531 expression is resolved to a type-id, regardless of the form of
16532 the corresponding template-parameter.
16534 Therefore, we try a type-id first. */
16535 cp_parser_parse_tentatively (parser);
16536 argument = cp_parser_template_type_arg (parser);
16537 /* If there was no error parsing the type-id but the next token is a
16538 '>>', our behavior depends on which dialect of C++ we're
16539 parsing. In C++98, we probably found a typo for '> >'. But there
16540 are type-id which are also valid expressions. For instance:
16542 struct X { int operator >> (int); };
16543 template <int V> struct Foo {};
16544 Foo<X () >> 5> r;
16546 Here 'X()' is a valid type-id of a function type, but the user just
16547 wanted to write the expression "X() >> 5". Thus, we remember that we
16548 found a valid type-id, but we still try to parse the argument as an
16549 expression to see what happens.
16551 In C++0x, the '>>' will be considered two separate '>'
16552 tokens. */
16553 if (!cp_parser_error_occurred (parser)
16554 && cxx_dialect == cxx98
16555 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16557 maybe_type_id = true;
16558 cp_parser_abort_tentative_parse (parser);
16560 else
16562 /* If the next token isn't a `,' or a `>', then this argument wasn't
16563 really finished. This means that the argument is not a valid
16564 type-id. */
16565 if (!cp_parser_next_token_ends_template_argument_p (parser))
16566 cp_parser_error (parser, "expected template-argument");
16567 /* If that worked, we're done. */
16568 if (cp_parser_parse_definitely (parser))
16569 return argument;
16571 /* We're still not sure what the argument will be. */
16572 cp_parser_parse_tentatively (parser);
16573 /* Try a template. */
16574 argument_start_token = cp_lexer_peek_token (parser->lexer);
16575 argument = cp_parser_id_expression (parser,
16576 /*template_keyword_p=*/false,
16577 /*check_dependency_p=*/true,
16578 &template_p,
16579 /*declarator_p=*/false,
16580 /*optional_p=*/false);
16581 /* If the next token isn't a `,' or a `>', then this argument wasn't
16582 really finished. */
16583 if (!cp_parser_next_token_ends_template_argument_p (parser))
16584 cp_parser_error (parser, "expected template-argument");
16585 if (!cp_parser_error_occurred (parser))
16587 /* Figure out what is being referred to. If the id-expression
16588 was for a class template specialization, then we will have a
16589 TYPE_DECL at this point. There is no need to do name lookup
16590 at this point in that case. */
16591 if (TREE_CODE (argument) != TYPE_DECL)
16592 argument = cp_parser_lookup_name (parser, argument,
16593 none_type,
16594 /*is_template=*/template_p,
16595 /*is_namespace=*/false,
16596 /*check_dependency=*/true,
16597 /*ambiguous_decls=*/NULL,
16598 argument_start_token->location);
16599 /* Handle a constrained-type-specifier for a non-type template
16600 parameter. */
16601 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16602 argument = decl;
16603 else if (TREE_CODE (argument) != TEMPLATE_DECL
16604 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16605 cp_parser_error (parser, "expected template-name");
16607 if (cp_parser_parse_definitely (parser))
16609 if (TREE_DEPRECATED (argument))
16610 warn_deprecated_use (argument, NULL_TREE);
16611 return argument;
16613 /* It must be a non-type argument. In C++17 any constant-expression is
16614 allowed. */
16615 if (cxx_dialect > cxx14)
16616 goto general_expr;
16618 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16620 -- an integral constant-expression of integral or enumeration
16621 type; or
16623 -- the name of a non-type template-parameter; or
16625 -- the name of an object or function with external linkage...
16627 -- the address of an object or function with external linkage...
16629 -- a pointer to member... */
16630 /* Look for a non-type template parameter. */
16631 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16633 cp_parser_parse_tentatively (parser);
16634 argument = cp_parser_primary_expression (parser,
16635 /*address_p=*/false,
16636 /*cast_p=*/false,
16637 /*template_arg_p=*/true,
16638 &idk);
16639 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16640 || !cp_parser_next_token_ends_template_argument_p (parser))
16641 cp_parser_simulate_error (parser);
16642 if (cp_parser_parse_definitely (parser))
16643 return argument;
16646 /* If the next token is "&", the argument must be the address of an
16647 object or function with external linkage. */
16648 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16649 if (address_p)
16651 loc = cp_lexer_peek_token (parser->lexer)->location;
16652 cp_lexer_consume_token (parser->lexer);
16654 /* See if we might have an id-expression. */
16655 token = cp_lexer_peek_token (parser->lexer);
16656 if (token->type == CPP_NAME
16657 || token->keyword == RID_OPERATOR
16658 || token->type == CPP_SCOPE
16659 || token->type == CPP_TEMPLATE_ID
16660 || token->type == CPP_NESTED_NAME_SPECIFIER)
16662 cp_parser_parse_tentatively (parser);
16663 argument = cp_parser_primary_expression (parser,
16664 address_p,
16665 /*cast_p=*/false,
16666 /*template_arg_p=*/true,
16667 &idk);
16668 if (cp_parser_error_occurred (parser)
16669 || !cp_parser_next_token_ends_template_argument_p (parser))
16670 cp_parser_abort_tentative_parse (parser);
16671 else
16673 tree probe;
16675 if (INDIRECT_REF_P (argument))
16677 /* Strip the dereference temporarily. */
16678 gcc_assert (REFERENCE_REF_P (argument));
16679 argument = TREE_OPERAND (argument, 0);
16682 /* If we're in a template, we represent a qualified-id referring
16683 to a static data member as a SCOPE_REF even if the scope isn't
16684 dependent so that we can check access control later. */
16685 probe = argument;
16686 if (TREE_CODE (probe) == SCOPE_REF)
16687 probe = TREE_OPERAND (probe, 1);
16688 if (VAR_P (probe))
16690 /* A variable without external linkage might still be a
16691 valid constant-expression, so no error is issued here
16692 if the external-linkage check fails. */
16693 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16694 cp_parser_simulate_error (parser);
16696 else if (is_overloaded_fn (argument))
16697 /* All overloaded functions are allowed; if the external
16698 linkage test does not pass, an error will be issued
16699 later. */
16701 else if (address_p
16702 && (TREE_CODE (argument) == OFFSET_REF
16703 || TREE_CODE (argument) == SCOPE_REF))
16704 /* A pointer-to-member. */
16706 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16708 else
16709 cp_parser_simulate_error (parser);
16711 if (cp_parser_parse_definitely (parser))
16713 if (address_p)
16714 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16715 tf_warning_or_error);
16716 else
16717 argument = convert_from_reference (argument);
16718 return argument;
16722 /* If the argument started with "&", there are no other valid
16723 alternatives at this point. */
16724 if (address_p)
16726 cp_parser_error (parser, "invalid non-type template argument");
16727 return error_mark_node;
16730 general_expr:
16731 /* If the argument wasn't successfully parsed as a type-id followed
16732 by '>>', the argument can only be a constant expression now.
16733 Otherwise, we try parsing the constant-expression tentatively,
16734 because the argument could really be a type-id. */
16735 if (maybe_type_id)
16736 cp_parser_parse_tentatively (parser);
16738 if (cxx_dialect <= cxx14)
16739 argument = cp_parser_constant_expression (parser);
16740 else
16742 /* With C++17 generalized non-type template arguments we need to handle
16743 lvalue constant expressions, too. */
16744 argument = cp_parser_assignment_expression (parser);
16745 require_potential_constant_expression (argument);
16748 if (!maybe_type_id)
16749 return argument;
16750 if (!cp_parser_next_token_ends_template_argument_p (parser))
16751 cp_parser_error (parser, "expected template-argument");
16752 if (cp_parser_parse_definitely (parser))
16753 return argument;
16754 /* We did our best to parse the argument as a non type-id, but that
16755 was the only alternative that matched (albeit with a '>' after
16756 it). We can assume it's just a typo from the user, and a
16757 diagnostic will then be issued. */
16758 return cp_parser_template_type_arg (parser);
16761 /* Parse an explicit-instantiation.
16763 explicit-instantiation:
16764 template declaration
16766 Although the standard says `declaration', what it really means is:
16768 explicit-instantiation:
16769 template decl-specifier-seq [opt] declarator [opt] ;
16771 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16772 supposed to be allowed. A defect report has been filed about this
16773 issue.
16775 GNU Extension:
16777 explicit-instantiation:
16778 storage-class-specifier template
16779 decl-specifier-seq [opt] declarator [opt] ;
16780 function-specifier template
16781 decl-specifier-seq [opt] declarator [opt] ; */
16783 static void
16784 cp_parser_explicit_instantiation (cp_parser* parser)
16786 int declares_class_or_enum;
16787 cp_decl_specifier_seq decl_specifiers;
16788 tree extension_specifier = NULL_TREE;
16790 timevar_push (TV_TEMPLATE_INST);
16792 /* Look for an (optional) storage-class-specifier or
16793 function-specifier. */
16794 if (cp_parser_allow_gnu_extensions_p (parser))
16796 extension_specifier
16797 = cp_parser_storage_class_specifier_opt (parser);
16798 if (!extension_specifier)
16799 extension_specifier
16800 = cp_parser_function_specifier_opt (parser,
16801 /*decl_specs=*/NULL);
16804 /* Look for the `template' keyword. */
16805 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16806 /* Let the front end know that we are processing an explicit
16807 instantiation. */
16808 begin_explicit_instantiation ();
16809 /* [temp.explicit] says that we are supposed to ignore access
16810 control while processing explicit instantiation directives. */
16811 push_deferring_access_checks (dk_no_check);
16812 /* Parse a decl-specifier-seq. */
16813 cp_parser_decl_specifier_seq (parser,
16814 CP_PARSER_FLAGS_OPTIONAL,
16815 &decl_specifiers,
16816 &declares_class_or_enum);
16817 /* If there was exactly one decl-specifier, and it declared a class,
16818 and there's no declarator, then we have an explicit type
16819 instantiation. */
16820 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16822 tree type;
16824 type = check_tag_decl (&decl_specifiers,
16825 /*explicit_type_instantiation_p=*/true);
16826 /* Turn access control back on for names used during
16827 template instantiation. */
16828 pop_deferring_access_checks ();
16829 if (type)
16830 do_type_instantiation (type, extension_specifier,
16831 /*complain=*/tf_error);
16833 else
16835 cp_declarator *declarator;
16836 tree decl;
16838 /* Parse the declarator. */
16839 declarator
16840 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16841 /*ctor_dtor_or_conv_p=*/NULL,
16842 /*parenthesized_p=*/NULL,
16843 /*member_p=*/false,
16844 /*friend_p=*/false);
16845 if (declares_class_or_enum & 2)
16846 cp_parser_check_for_definition_in_return_type (declarator,
16847 decl_specifiers.type,
16848 decl_specifiers.locations[ds_type_spec]);
16849 if (declarator != cp_error_declarator)
16851 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16852 permerror (decl_specifiers.locations[ds_inline],
16853 "explicit instantiation shall not use"
16854 " %<inline%> specifier");
16855 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16856 permerror (decl_specifiers.locations[ds_constexpr],
16857 "explicit instantiation shall not use"
16858 " %<constexpr%> specifier");
16860 decl = grokdeclarator (declarator, &decl_specifiers,
16861 NORMAL, 0, &decl_specifiers.attributes);
16862 /* Turn access control back on for names used during
16863 template instantiation. */
16864 pop_deferring_access_checks ();
16865 /* Do the explicit instantiation. */
16866 do_decl_instantiation (decl, extension_specifier);
16868 else
16870 pop_deferring_access_checks ();
16871 /* Skip the body of the explicit instantiation. */
16872 cp_parser_skip_to_end_of_statement (parser);
16875 /* We're done with the instantiation. */
16876 end_explicit_instantiation ();
16878 cp_parser_consume_semicolon_at_end_of_statement (parser);
16880 timevar_pop (TV_TEMPLATE_INST);
16883 /* Parse an explicit-specialization.
16885 explicit-specialization:
16886 template < > declaration
16888 Although the standard says `declaration', what it really means is:
16890 explicit-specialization:
16891 template <> decl-specifier [opt] init-declarator [opt] ;
16892 template <> function-definition
16893 template <> explicit-specialization
16894 template <> template-declaration */
16896 static void
16897 cp_parser_explicit_specialization (cp_parser* parser)
16899 bool need_lang_pop;
16900 cp_token *token = cp_lexer_peek_token (parser->lexer);
16902 /* Look for the `template' keyword. */
16903 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16904 /* Look for the `<'. */
16905 cp_parser_require (parser, CPP_LESS, RT_LESS);
16906 /* Look for the `>'. */
16907 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16908 /* We have processed another parameter list. */
16909 ++parser->num_template_parameter_lists;
16910 /* [temp]
16912 A template ... explicit specialization ... shall not have C
16913 linkage. */
16914 if (current_lang_name == lang_name_c)
16916 error_at (token->location, "template specialization with C linkage");
16917 maybe_show_extern_c_location ();
16918 /* Give it C++ linkage to avoid confusing other parts of the
16919 front end. */
16920 push_lang_context (lang_name_cplusplus);
16921 need_lang_pop = true;
16923 else
16924 need_lang_pop = false;
16925 /* Let the front end know that we are beginning a specialization. */
16926 if (!begin_specialization ())
16928 end_specialization ();
16929 return;
16932 /* If the next keyword is `template', we need to figure out whether
16933 or not we're looking a template-declaration. */
16934 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16936 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16937 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16938 cp_parser_template_declaration_after_export (parser,
16939 /*member_p=*/false);
16940 else
16941 cp_parser_explicit_specialization (parser);
16943 else
16944 /* Parse the dependent declaration. */
16945 cp_parser_single_declaration (parser,
16946 /*checks=*/NULL,
16947 /*member_p=*/false,
16948 /*explicit_specialization_p=*/true,
16949 /*friend_p=*/NULL);
16950 /* We're done with the specialization. */
16951 end_specialization ();
16952 /* For the erroneous case of a template with C linkage, we pushed an
16953 implicit C++ linkage scope; exit that scope now. */
16954 if (need_lang_pop)
16955 pop_lang_context ();
16956 /* We're done with this parameter list. */
16957 --parser->num_template_parameter_lists;
16960 /* Parse a type-specifier.
16962 type-specifier:
16963 simple-type-specifier
16964 class-specifier
16965 enum-specifier
16966 elaborated-type-specifier
16967 cv-qualifier
16969 GNU Extension:
16971 type-specifier:
16972 __complex__
16974 Returns a representation of the type-specifier. For a
16975 class-specifier, enum-specifier, or elaborated-type-specifier, a
16976 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16978 The parser flags FLAGS is used to control type-specifier parsing.
16980 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16981 in a decl-specifier-seq.
16983 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16984 class-specifier, enum-specifier, or elaborated-type-specifier, then
16985 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16986 if a type is declared; 2 if it is defined. Otherwise, it is set to
16987 zero.
16989 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16990 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16991 is set to FALSE. */
16993 static tree
16994 cp_parser_type_specifier (cp_parser* parser,
16995 cp_parser_flags flags,
16996 cp_decl_specifier_seq *decl_specs,
16997 bool is_declaration,
16998 int* declares_class_or_enum,
16999 bool* is_cv_qualifier)
17001 tree type_spec = NULL_TREE;
17002 cp_token *token;
17003 enum rid keyword;
17004 cp_decl_spec ds = ds_last;
17006 /* Assume this type-specifier does not declare a new type. */
17007 if (declares_class_or_enum)
17008 *declares_class_or_enum = 0;
17009 /* And that it does not specify a cv-qualifier. */
17010 if (is_cv_qualifier)
17011 *is_cv_qualifier = false;
17012 /* Peek at the next token. */
17013 token = cp_lexer_peek_token (parser->lexer);
17015 /* If we're looking at a keyword, we can use that to guide the
17016 production we choose. */
17017 keyword = token->keyword;
17018 switch (keyword)
17020 case RID_ENUM:
17021 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17022 goto elaborated_type_specifier;
17024 /* Look for the enum-specifier. */
17025 type_spec = cp_parser_enum_specifier (parser);
17026 /* If that worked, we're done. */
17027 if (type_spec)
17029 if (declares_class_or_enum)
17030 *declares_class_or_enum = 2;
17031 if (decl_specs)
17032 cp_parser_set_decl_spec_type (decl_specs,
17033 type_spec,
17034 token,
17035 /*type_definition_p=*/true);
17036 return type_spec;
17038 else
17039 goto elaborated_type_specifier;
17041 /* Any of these indicate either a class-specifier, or an
17042 elaborated-type-specifier. */
17043 case RID_CLASS:
17044 case RID_STRUCT:
17045 case RID_UNION:
17046 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17047 goto elaborated_type_specifier;
17049 /* Parse tentatively so that we can back up if we don't find a
17050 class-specifier. */
17051 cp_parser_parse_tentatively (parser);
17052 /* Look for the class-specifier. */
17053 type_spec = cp_parser_class_specifier (parser);
17054 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17055 /* If that worked, we're done. */
17056 if (cp_parser_parse_definitely (parser))
17058 if (declares_class_or_enum)
17059 *declares_class_or_enum = 2;
17060 if (decl_specs)
17061 cp_parser_set_decl_spec_type (decl_specs,
17062 type_spec,
17063 token,
17064 /*type_definition_p=*/true);
17065 return type_spec;
17068 /* Fall through. */
17069 elaborated_type_specifier:
17070 /* We're declaring (not defining) a class or enum. */
17071 if (declares_class_or_enum)
17072 *declares_class_or_enum = 1;
17074 /* Fall through. */
17075 case RID_TYPENAME:
17076 /* Look for an elaborated-type-specifier. */
17077 type_spec
17078 = (cp_parser_elaborated_type_specifier
17079 (parser,
17080 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17081 is_declaration));
17082 if (decl_specs)
17083 cp_parser_set_decl_spec_type (decl_specs,
17084 type_spec,
17085 token,
17086 /*type_definition_p=*/false);
17087 return type_spec;
17089 case RID_CONST:
17090 ds = ds_const;
17091 if (is_cv_qualifier)
17092 *is_cv_qualifier = true;
17093 break;
17095 case RID_VOLATILE:
17096 ds = ds_volatile;
17097 if (is_cv_qualifier)
17098 *is_cv_qualifier = true;
17099 break;
17101 case RID_RESTRICT:
17102 ds = ds_restrict;
17103 if (is_cv_qualifier)
17104 *is_cv_qualifier = true;
17105 break;
17107 case RID_COMPLEX:
17108 /* The `__complex__' keyword is a GNU extension. */
17109 ds = ds_complex;
17110 break;
17112 default:
17113 break;
17116 /* Handle simple keywords. */
17117 if (ds != ds_last)
17119 if (decl_specs)
17121 set_and_check_decl_spec_loc (decl_specs, ds, token);
17122 decl_specs->any_specifiers_p = true;
17124 return cp_lexer_consume_token (parser->lexer)->u.value;
17127 /* If we do not already have a type-specifier, assume we are looking
17128 at a simple-type-specifier. */
17129 type_spec = cp_parser_simple_type_specifier (parser,
17130 decl_specs,
17131 flags);
17133 /* If we didn't find a type-specifier, and a type-specifier was not
17134 optional in this context, issue an error message. */
17135 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17137 cp_parser_error (parser, "expected type specifier");
17138 return error_mark_node;
17141 return type_spec;
17144 /* Parse a simple-type-specifier.
17146 simple-type-specifier:
17147 :: [opt] nested-name-specifier [opt] type-name
17148 :: [opt] nested-name-specifier template template-id
17149 char
17150 wchar_t
17151 bool
17152 short
17154 long
17155 signed
17156 unsigned
17157 float
17158 double
17159 void
17161 C++11 Extension:
17163 simple-type-specifier:
17164 auto
17165 decltype ( expression )
17166 char16_t
17167 char32_t
17168 __underlying_type ( type-id )
17170 C++17 extension:
17172 nested-name-specifier(opt) template-name
17174 GNU Extension:
17176 simple-type-specifier:
17177 __int128
17178 __typeof__ unary-expression
17179 __typeof__ ( type-id )
17180 __typeof__ ( type-id ) { initializer-list , [opt] }
17182 Concepts Extension:
17184 simple-type-specifier:
17185 constrained-type-specifier
17187 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17188 appropriately updated. */
17190 static tree
17191 cp_parser_simple_type_specifier (cp_parser* parser,
17192 cp_decl_specifier_seq *decl_specs,
17193 cp_parser_flags flags)
17195 tree type = NULL_TREE;
17196 cp_token *token;
17197 int idx;
17199 /* Peek at the next token. */
17200 token = cp_lexer_peek_token (parser->lexer);
17202 /* If we're looking at a keyword, things are easy. */
17203 switch (token->keyword)
17205 case RID_CHAR:
17206 if (decl_specs)
17207 decl_specs->explicit_char_p = true;
17208 type = char_type_node;
17209 break;
17210 case RID_CHAR16:
17211 type = char16_type_node;
17212 break;
17213 case RID_CHAR32:
17214 type = char32_type_node;
17215 break;
17216 case RID_WCHAR:
17217 type = wchar_type_node;
17218 break;
17219 case RID_BOOL:
17220 type = boolean_type_node;
17221 break;
17222 case RID_SHORT:
17223 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17224 type = short_integer_type_node;
17225 break;
17226 case RID_INT:
17227 if (decl_specs)
17228 decl_specs->explicit_int_p = true;
17229 type = integer_type_node;
17230 break;
17231 case RID_INT_N_0:
17232 case RID_INT_N_1:
17233 case RID_INT_N_2:
17234 case RID_INT_N_3:
17235 idx = token->keyword - RID_INT_N_0;
17236 if (! int_n_enabled_p [idx])
17237 break;
17238 if (decl_specs)
17240 decl_specs->explicit_intN_p = true;
17241 decl_specs->int_n_idx = idx;
17243 type = int_n_trees [idx].signed_type;
17244 break;
17245 case RID_LONG:
17246 if (decl_specs)
17247 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17248 type = long_integer_type_node;
17249 break;
17250 case RID_SIGNED:
17251 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17252 type = integer_type_node;
17253 break;
17254 case RID_UNSIGNED:
17255 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17256 type = unsigned_type_node;
17257 break;
17258 case RID_FLOAT:
17259 type = float_type_node;
17260 break;
17261 case RID_DOUBLE:
17262 type = double_type_node;
17263 break;
17264 case RID_VOID:
17265 type = void_type_node;
17266 break;
17268 case RID_AUTO:
17269 maybe_warn_cpp0x (CPP0X_AUTO);
17270 if (parser->auto_is_implicit_function_template_parm_p)
17272 /* The 'auto' might be the placeholder return type for a function decl
17273 with trailing return type. */
17274 bool have_trailing_return_fn_decl = false;
17276 cp_parser_parse_tentatively (parser);
17277 cp_lexer_consume_token (parser->lexer);
17278 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17279 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17280 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17281 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17283 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17285 cp_lexer_consume_token (parser->lexer);
17286 cp_parser_skip_to_closing_parenthesis (parser,
17287 /*recovering*/false,
17288 /*or_comma*/false,
17289 /*consume_paren*/true);
17290 continue;
17293 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17295 have_trailing_return_fn_decl = true;
17296 break;
17299 cp_lexer_consume_token (parser->lexer);
17301 cp_parser_abort_tentative_parse (parser);
17303 if (have_trailing_return_fn_decl)
17305 type = make_auto ();
17306 break;
17309 if (cxx_dialect >= cxx14)
17311 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17312 type = TREE_TYPE (type);
17314 else
17315 type = error_mark_node;
17317 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17319 if (cxx_dialect < cxx14)
17320 error_at (token->location,
17321 "use of %<auto%> in lambda parameter declaration "
17322 "only available with "
17323 "-std=c++14 or -std=gnu++14");
17325 else if (cxx_dialect < cxx14)
17326 error_at (token->location,
17327 "use of %<auto%> in parameter declaration "
17328 "only available with "
17329 "-std=c++14 or -std=gnu++14");
17330 else if (!flag_concepts)
17331 pedwarn (token->location, 0,
17332 "use of %<auto%> in parameter declaration "
17333 "only available with -fconcepts");
17335 else
17336 type = make_auto ();
17337 break;
17339 case RID_DECLTYPE:
17340 /* Since DR 743, decltype can either be a simple-type-specifier by
17341 itself or begin a nested-name-specifier. Parsing it will replace
17342 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17343 handling below decide what to do. */
17344 cp_parser_decltype (parser);
17345 cp_lexer_set_token_position (parser->lexer, token);
17346 break;
17348 case RID_TYPEOF:
17349 /* Consume the `typeof' token. */
17350 cp_lexer_consume_token (parser->lexer);
17351 /* Parse the operand to `typeof'. */
17352 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17353 /* If it is not already a TYPE, take its type. */
17354 if (!TYPE_P (type))
17355 type = finish_typeof (type);
17357 if (decl_specs)
17358 cp_parser_set_decl_spec_type (decl_specs, type,
17359 token,
17360 /*type_definition_p=*/false);
17362 return type;
17364 case RID_UNDERLYING_TYPE:
17365 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17366 if (decl_specs)
17367 cp_parser_set_decl_spec_type (decl_specs, type,
17368 token,
17369 /*type_definition_p=*/false);
17371 return type;
17373 case RID_BASES:
17374 case RID_DIRECT_BASES:
17375 type = cp_parser_trait_expr (parser, token->keyword);
17376 if (decl_specs)
17377 cp_parser_set_decl_spec_type (decl_specs, type,
17378 token,
17379 /*type_definition_p=*/false);
17380 return type;
17381 default:
17382 break;
17385 /* If token is an already-parsed decltype not followed by ::,
17386 it's a simple-type-specifier. */
17387 if (token->type == CPP_DECLTYPE
17388 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17390 type = saved_checks_value (token->u.tree_check_value);
17391 if (decl_specs)
17393 cp_parser_set_decl_spec_type (decl_specs, type,
17394 token,
17395 /*type_definition_p=*/false);
17396 /* Remember that we are handling a decltype in order to
17397 implement the resolution of DR 1510 when the argument
17398 isn't instantiation dependent. */
17399 decl_specs->decltype_p = true;
17401 cp_lexer_consume_token (parser->lexer);
17402 return type;
17405 /* If the type-specifier was for a built-in type, we're done. */
17406 if (type)
17408 /* Record the type. */
17409 if (decl_specs
17410 && (token->keyword != RID_SIGNED
17411 && token->keyword != RID_UNSIGNED
17412 && token->keyword != RID_SHORT
17413 && token->keyword != RID_LONG))
17414 cp_parser_set_decl_spec_type (decl_specs,
17415 type,
17416 token,
17417 /*type_definition_p=*/false);
17418 if (decl_specs)
17419 decl_specs->any_specifiers_p = true;
17421 /* Consume the token. */
17422 cp_lexer_consume_token (parser->lexer);
17424 if (type == error_mark_node)
17425 return error_mark_node;
17427 /* There is no valid C++ program where a non-template type is
17428 followed by a "<". That usually indicates that the user thought
17429 that the type was a template. */
17430 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17431 token->location);
17433 return TYPE_NAME (type);
17436 /* The type-specifier must be a user-defined type. */
17437 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17439 bool qualified_p;
17440 bool global_p;
17442 /* Don't gobble tokens or issue error messages if this is an
17443 optional type-specifier. */
17444 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17445 cp_parser_parse_tentatively (parser);
17447 token = cp_lexer_peek_token (parser->lexer);
17449 /* Look for the optional `::' operator. */
17450 global_p
17451 = (cp_parser_global_scope_opt (parser,
17452 /*current_scope_valid_p=*/false)
17453 != NULL_TREE);
17454 /* Look for the nested-name specifier. */
17455 qualified_p
17456 = (cp_parser_nested_name_specifier_opt (parser,
17457 /*typename_keyword_p=*/false,
17458 /*check_dependency_p=*/true,
17459 /*type_p=*/false,
17460 /*is_declaration=*/false)
17461 != NULL_TREE);
17462 /* If we have seen a nested-name-specifier, and the next token
17463 is `template', then we are using the template-id production. */
17464 if (parser->scope
17465 && cp_parser_optional_template_keyword (parser))
17467 /* Look for the template-id. */
17468 type = cp_parser_template_id (parser,
17469 /*template_keyword_p=*/true,
17470 /*check_dependency_p=*/true,
17471 none_type,
17472 /*is_declaration=*/false);
17473 /* If the template-id did not name a type, we are out of
17474 luck. */
17475 if (TREE_CODE (type) != TYPE_DECL)
17477 cp_parser_error (parser, "expected template-id for type");
17478 type = NULL_TREE;
17481 /* Otherwise, look for a type-name. */
17482 else
17483 type = cp_parser_type_name (parser);
17484 /* Keep track of all name-lookups performed in class scopes. */
17485 if (type
17486 && !global_p
17487 && !qualified_p
17488 && TREE_CODE (type) == TYPE_DECL
17489 && identifier_p (DECL_NAME (type)))
17490 maybe_note_name_used_in_class (DECL_NAME (type), type);
17491 /* If it didn't work out, we don't have a TYPE. */
17492 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17493 && !cp_parser_parse_definitely (parser))
17494 type = NULL_TREE;
17495 if (!type && cxx_dialect >= cxx17)
17497 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17498 cp_parser_parse_tentatively (parser);
17500 cp_parser_global_scope_opt (parser,
17501 /*current_scope_valid_p=*/false);
17502 cp_parser_nested_name_specifier_opt (parser,
17503 /*typename_keyword_p=*/false,
17504 /*check_dependency_p=*/true,
17505 /*type_p=*/false,
17506 /*is_declaration=*/false);
17507 tree name = cp_parser_identifier (parser);
17508 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17509 && parser->scope != error_mark_node)
17511 tree tmpl = cp_parser_lookup_name (parser, name,
17512 none_type,
17513 /*is_template=*/false,
17514 /*is_namespace=*/false,
17515 /*check_dependency=*/true,
17516 /*ambiguous_decls=*/NULL,
17517 token->location);
17518 if (tmpl && tmpl != error_mark_node
17519 && (DECL_CLASS_TEMPLATE_P (tmpl)
17520 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17521 type = make_template_placeholder (tmpl);
17522 else
17524 type = error_mark_node;
17525 if (!cp_parser_simulate_error (parser))
17526 cp_parser_name_lookup_error (parser, name, tmpl,
17527 NLE_TYPE, token->location);
17530 else
17531 type = error_mark_node;
17533 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17534 && !cp_parser_parse_definitely (parser))
17535 type = NULL_TREE;
17537 if (type && decl_specs)
17538 cp_parser_set_decl_spec_type (decl_specs, type,
17539 token,
17540 /*type_definition_p=*/false);
17543 /* If we didn't get a type-name, issue an error message. */
17544 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17546 cp_parser_error (parser, "expected type-name");
17547 return error_mark_node;
17550 if (type && type != error_mark_node)
17552 /* See if TYPE is an Objective-C type, and if so, parse and
17553 accept any protocol references following it. Do this before
17554 the cp_parser_check_for_invalid_template_id() call, because
17555 Objective-C types can be followed by '<...>' which would
17556 enclose protocol names rather than template arguments, and so
17557 everything is fine. */
17558 if (c_dialect_objc () && !parser->scope
17559 && (objc_is_id (type) || objc_is_class_name (type)))
17561 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17562 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17564 /* Clobber the "unqualified" type previously entered into
17565 DECL_SPECS with the new, improved protocol-qualified version. */
17566 if (decl_specs)
17567 decl_specs->type = qual_type;
17569 return qual_type;
17572 /* There is no valid C++ program where a non-template type is
17573 followed by a "<". That usually indicates that the user
17574 thought that the type was a template. */
17575 cp_parser_check_for_invalid_template_id (parser, type,
17576 none_type,
17577 token->location);
17580 return type;
17583 /* Parse a type-name.
17585 type-name:
17586 class-name
17587 enum-name
17588 typedef-name
17589 simple-template-id [in c++0x]
17591 enum-name:
17592 identifier
17594 typedef-name:
17595 identifier
17597 Concepts:
17599 type-name:
17600 concept-name
17601 partial-concept-id
17603 concept-name:
17604 identifier
17606 Returns a TYPE_DECL for the type. */
17608 static tree
17609 cp_parser_type_name (cp_parser* parser)
17611 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17614 /* See above. */
17615 static tree
17616 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17618 tree type_decl;
17620 /* We can't know yet whether it is a class-name or not. */
17621 cp_parser_parse_tentatively (parser);
17622 /* Try a class-name. */
17623 type_decl = cp_parser_class_name (parser,
17624 typename_keyword_p,
17625 /*template_keyword_p=*/false,
17626 none_type,
17627 /*check_dependency_p=*/true,
17628 /*class_head_p=*/false,
17629 /*is_declaration=*/false);
17630 /* If it's not a class-name, keep looking. */
17631 if (!cp_parser_parse_definitely (parser))
17633 if (cxx_dialect < cxx11)
17634 /* It must be a typedef-name or an enum-name. */
17635 return cp_parser_nonclass_name (parser);
17637 cp_parser_parse_tentatively (parser);
17638 /* It is either a simple-template-id representing an
17639 instantiation of an alias template... */
17640 type_decl = cp_parser_template_id (parser,
17641 /*template_keyword_p=*/false,
17642 /*check_dependency_p=*/true,
17643 none_type,
17644 /*is_declaration=*/false);
17645 /* Note that this must be an instantiation of an alias template
17646 because [temp.names]/6 says:
17648 A template-id that names an alias template specialization
17649 is a type-name.
17651 Whereas [temp.names]/7 says:
17653 A simple-template-id that names a class template
17654 specialization is a class-name.
17656 With concepts, this could also be a partial-concept-id that
17657 declares a non-type template parameter. */
17658 if (type_decl != NULL_TREE
17659 && TREE_CODE (type_decl) == TYPE_DECL
17660 && TYPE_DECL_ALIAS_P (type_decl))
17661 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17662 else if (is_constrained_parameter (type_decl))
17663 /* Don't do anything. */ ;
17664 else
17665 cp_parser_simulate_error (parser);
17667 if (!cp_parser_parse_definitely (parser))
17668 /* ... Or a typedef-name or an enum-name. */
17669 return cp_parser_nonclass_name (parser);
17672 return type_decl;
17675 /* Check if DECL and ARGS can form a constrained-type-specifier.
17676 If ARGS is non-null, we try to form a concept check of the
17677 form DECL<?, ARGS> where ? is a wildcard that matches any
17678 kind of template argument. If ARGS is NULL, then we try to
17679 form a concept check of the form DECL<?>. */
17681 static tree
17682 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17683 tree decl, tree args)
17685 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17687 /* If we a constrained-type-specifier cannot be deduced. */
17688 if (parser->prevent_constrained_type_specifiers)
17689 return NULL_TREE;
17691 /* A constrained type specifier can only be found in an
17692 overload set or as a reference to a template declaration.
17694 FIXME: This might be masking a bug. It's possible that
17695 that the deduction below is causing template specializations
17696 to be formed with the wildcard as an argument. */
17697 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17698 return NULL_TREE;
17700 /* Try to build a call expression that evaluates the
17701 concept. This can fail if the overload set refers
17702 only to non-templates. */
17703 tree placeholder = build_nt (WILDCARD_DECL);
17704 tree check = build_concept_check (decl, placeholder, args);
17705 if (check == error_mark_node)
17706 return NULL_TREE;
17708 /* Deduce the checked constraint and the prototype parameter.
17710 FIXME: In certain cases, failure to deduce should be a
17711 diagnosable error. */
17712 tree conc;
17713 tree proto;
17714 if (!deduce_constrained_parameter (check, conc, proto))
17715 return NULL_TREE;
17717 /* In template parameter scope, this results in a constrained
17718 parameter. Return a descriptor of that parm. */
17719 if (processing_template_parmlist)
17720 return build_constrained_parameter (conc, proto, args);
17722 /* In a parameter-declaration-clause, constrained-type
17723 specifiers result in invented template parameters. */
17724 if (parser->auto_is_implicit_function_template_parm_p)
17726 tree x = build_constrained_parameter (conc, proto, args);
17727 return synthesize_implicit_template_parm (parser, x);
17729 else
17731 /* Otherwise, we're in a context where the constrained
17732 type name is deduced and the constraint applies
17733 after deduction. */
17734 return make_constrained_auto (conc, args);
17737 return NULL_TREE;
17740 /* If DECL refers to a concept, return a TYPE_DECL representing
17741 the result of using the constrained type specifier in the
17742 current context. DECL refers to a concept if
17744 - it is an overload set containing a function concept taking a single
17745 type argument, or
17747 - it is a variable concept taking a single type argument. */
17749 static tree
17750 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17752 if (flag_concepts
17753 && (TREE_CODE (decl) == OVERLOAD
17754 || BASELINK_P (decl)
17755 || variable_concept_p (decl)))
17756 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17757 else
17758 return NULL_TREE;
17761 /* Check if DECL and ARGS form a partial-concept-id. If so,
17762 assign ID to the resulting constrained placeholder.
17764 Returns true if the partial-concept-id designates a placeholder
17765 and false otherwise. Note that *id is set to NULL_TREE in
17766 this case. */
17768 static tree
17769 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17771 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17774 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17775 or a concept-name.
17777 enum-name:
17778 identifier
17780 typedef-name:
17781 identifier
17783 concept-name:
17784 identifier
17786 Returns a TYPE_DECL for the type. */
17788 static tree
17789 cp_parser_nonclass_name (cp_parser* parser)
17791 tree type_decl;
17792 tree identifier;
17794 cp_token *token = cp_lexer_peek_token (parser->lexer);
17795 identifier = cp_parser_identifier (parser);
17796 if (identifier == error_mark_node)
17797 return error_mark_node;
17799 /* Look up the type-name. */
17800 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17802 type_decl = strip_using_decl (type_decl);
17804 /* If we found an overload set, then it may refer to a concept-name. */
17805 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17806 type_decl = decl;
17808 if (TREE_CODE (type_decl) != TYPE_DECL
17809 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17811 /* See if this is an Objective-C type. */
17812 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17813 tree type = objc_get_protocol_qualified_type (identifier, protos);
17814 if (type)
17815 type_decl = TYPE_NAME (type);
17818 /* Issue an error if we did not find a type-name. */
17819 if (TREE_CODE (type_decl) != TYPE_DECL
17820 /* In Objective-C, we have the complication that class names are
17821 normally type names and start declarations (eg, the
17822 "NSObject" in "NSObject *object;"), but can be used in an
17823 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17824 is an expression. So, a classname followed by a dot is not a
17825 valid type-name. */
17826 || (objc_is_class_name (TREE_TYPE (type_decl))
17827 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17829 if (!cp_parser_simulate_error (parser))
17830 cp_parser_name_lookup_error (parser, identifier, type_decl,
17831 NLE_TYPE, token->location);
17832 return error_mark_node;
17834 /* Remember that the name was used in the definition of the
17835 current class so that we can check later to see if the
17836 meaning would have been different after the class was
17837 entirely defined. */
17838 else if (type_decl != error_mark_node
17839 && !parser->scope)
17840 maybe_note_name_used_in_class (identifier, type_decl);
17842 return type_decl;
17845 /* Parse an elaborated-type-specifier. Note that the grammar given
17846 here incorporates the resolution to DR68.
17848 elaborated-type-specifier:
17849 class-key :: [opt] nested-name-specifier [opt] identifier
17850 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17851 enum-key :: [opt] nested-name-specifier [opt] identifier
17852 typename :: [opt] nested-name-specifier identifier
17853 typename :: [opt] nested-name-specifier template [opt]
17854 template-id
17856 GNU extension:
17858 elaborated-type-specifier:
17859 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17860 class-key attributes :: [opt] nested-name-specifier [opt]
17861 template [opt] template-id
17862 enum attributes :: [opt] nested-name-specifier [opt] identifier
17864 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17865 declared `friend'. If IS_DECLARATION is TRUE, then this
17866 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17867 something is being declared.
17869 Returns the TYPE specified. */
17871 static tree
17872 cp_parser_elaborated_type_specifier (cp_parser* parser,
17873 bool is_friend,
17874 bool is_declaration)
17876 enum tag_types tag_type;
17877 tree identifier;
17878 tree type = NULL_TREE;
17879 tree attributes = NULL_TREE;
17880 tree globalscope;
17881 cp_token *token = NULL;
17883 /* See if we're looking at the `enum' keyword. */
17884 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17886 /* Consume the `enum' token. */
17887 cp_lexer_consume_token (parser->lexer);
17888 /* Remember that it's an enumeration type. */
17889 tag_type = enum_type;
17890 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17891 enums) is used here. */
17892 cp_token *token = cp_lexer_peek_token (parser->lexer);
17893 if (cp_parser_is_keyword (token, RID_CLASS)
17894 || cp_parser_is_keyword (token, RID_STRUCT))
17896 gcc_rich_location richloc (token->location);
17897 richloc.add_range (input_location);
17898 richloc.add_fixit_remove ();
17899 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17900 "a scoped enum must not use the %qD keyword",
17901 token->u.value);
17902 /* Consume the `struct' or `class' and parse it anyway. */
17903 cp_lexer_consume_token (parser->lexer);
17905 /* Parse the attributes. */
17906 attributes = cp_parser_attributes_opt (parser);
17908 /* Or, it might be `typename'. */
17909 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17910 RID_TYPENAME))
17912 /* Consume the `typename' token. */
17913 cp_lexer_consume_token (parser->lexer);
17914 /* Remember that it's a `typename' type. */
17915 tag_type = typename_type;
17917 /* Otherwise it must be a class-key. */
17918 else
17920 tag_type = cp_parser_class_key (parser);
17921 if (tag_type == none_type)
17922 return error_mark_node;
17923 /* Parse the attributes. */
17924 attributes = cp_parser_attributes_opt (parser);
17927 /* Look for the `::' operator. */
17928 globalscope = cp_parser_global_scope_opt (parser,
17929 /*current_scope_valid_p=*/false);
17930 /* Look for the nested-name-specifier. */
17931 tree nested_name_specifier;
17932 if (tag_type == typename_type && !globalscope)
17934 nested_name_specifier
17935 = cp_parser_nested_name_specifier (parser,
17936 /*typename_keyword_p=*/true,
17937 /*check_dependency_p=*/true,
17938 /*type_p=*/true,
17939 is_declaration);
17940 if (!nested_name_specifier)
17941 return error_mark_node;
17943 else
17944 /* Even though `typename' is not present, the proposed resolution
17945 to Core Issue 180 says that in `class A<T>::B', `B' should be
17946 considered a type-name, even if `A<T>' is dependent. */
17947 nested_name_specifier
17948 = cp_parser_nested_name_specifier_opt (parser,
17949 /*typename_keyword_p=*/true,
17950 /*check_dependency_p=*/true,
17951 /*type_p=*/true,
17952 is_declaration);
17953 /* For everything but enumeration types, consider a template-id.
17954 For an enumeration type, consider only a plain identifier. */
17955 if (tag_type != enum_type)
17957 bool template_p = false;
17958 tree decl;
17960 /* Allow the `template' keyword. */
17961 template_p = cp_parser_optional_template_keyword (parser);
17962 /* If we didn't see `template', we don't know if there's a
17963 template-id or not. */
17964 if (!template_p)
17965 cp_parser_parse_tentatively (parser);
17966 /* Parse the template-id. */
17967 token = cp_lexer_peek_token (parser->lexer);
17968 decl = cp_parser_template_id (parser, template_p,
17969 /*check_dependency_p=*/true,
17970 tag_type,
17971 is_declaration);
17972 /* If we didn't find a template-id, look for an ordinary
17973 identifier. */
17974 if (!template_p && !cp_parser_parse_definitely (parser))
17976 /* We can get here when cp_parser_template_id, called by
17977 cp_parser_class_name with tag_type == none_type, succeeds
17978 and caches a BASELINK. Then, when called again here,
17979 instead of failing and returning an error_mark_node
17980 returns it (see template/typename17.C in C++11).
17981 ??? Could we diagnose this earlier? */
17982 else if (tag_type == typename_type && BASELINK_P (decl))
17984 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17985 type = error_mark_node;
17987 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17988 in effect, then we must assume that, upon instantiation, the
17989 template will correspond to a class. */
17990 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17991 && tag_type == typename_type)
17992 type = make_typename_type (parser->scope, decl,
17993 typename_type,
17994 /*complain=*/tf_error);
17995 /* If the `typename' keyword is in effect and DECL is not a type
17996 decl, then type is non existent. */
17997 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17999 else if (TREE_CODE (decl) == TYPE_DECL)
18001 type = check_elaborated_type_specifier (tag_type, decl,
18002 /*allow_template_p=*/true);
18004 /* If the next token is a semicolon, this must be a specialization,
18005 instantiation, or friend declaration. Check the scope while we
18006 still know whether or not we had a nested-name-specifier. */
18007 if (type != error_mark_node
18008 && !nested_name_specifier && !is_friend
18009 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18010 check_unqualified_spec_or_inst (type, token->location);
18012 else if (decl == error_mark_node)
18013 type = error_mark_node;
18016 if (!type)
18018 token = cp_lexer_peek_token (parser->lexer);
18019 identifier = cp_parser_identifier (parser);
18021 if (identifier == error_mark_node)
18023 parser->scope = NULL_TREE;
18024 return error_mark_node;
18027 /* For a `typename', we needn't call xref_tag. */
18028 if (tag_type == typename_type
18029 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18030 return cp_parser_make_typename_type (parser, identifier,
18031 token->location);
18033 /* Template parameter lists apply only if we are not within a
18034 function parameter list. */
18035 bool template_parm_lists_apply
18036 = parser->num_template_parameter_lists;
18037 if (template_parm_lists_apply)
18038 for (cp_binding_level *s = current_binding_level;
18039 s && s->kind != sk_template_parms;
18040 s = s->level_chain)
18041 if (s->kind == sk_function_parms)
18042 template_parm_lists_apply = false;
18044 /* Look up a qualified name in the usual way. */
18045 if (parser->scope)
18047 tree decl;
18048 tree ambiguous_decls;
18050 decl = cp_parser_lookup_name (parser, identifier,
18051 tag_type,
18052 /*is_template=*/false,
18053 /*is_namespace=*/false,
18054 /*check_dependency=*/true,
18055 &ambiguous_decls,
18056 token->location);
18058 /* If the lookup was ambiguous, an error will already have been
18059 issued. */
18060 if (ambiguous_decls)
18061 return error_mark_node;
18063 /* If we are parsing friend declaration, DECL may be a
18064 TEMPLATE_DECL tree node here. However, we need to check
18065 whether this TEMPLATE_DECL results in valid code. Consider
18066 the following example:
18068 namespace N {
18069 template <class T> class C {};
18071 class X {
18072 template <class T> friend class N::C; // #1, valid code
18074 template <class T> class Y {
18075 friend class N::C; // #2, invalid code
18078 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18079 name lookup of `N::C'. We see that friend declaration must
18080 be template for the code to be valid. Note that
18081 processing_template_decl does not work here since it is
18082 always 1 for the above two cases. */
18084 decl = (cp_parser_maybe_treat_template_as_class
18085 (decl, /*tag_name_p=*/is_friend
18086 && template_parm_lists_apply));
18088 if (TREE_CODE (decl) != TYPE_DECL)
18090 cp_parser_diagnose_invalid_type_name (parser,
18091 identifier,
18092 token->location);
18093 return error_mark_node;
18096 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18098 bool allow_template = (template_parm_lists_apply
18099 || DECL_SELF_REFERENCE_P (decl));
18100 type = check_elaborated_type_specifier (tag_type, decl,
18101 allow_template);
18103 if (type == error_mark_node)
18104 return error_mark_node;
18107 /* Forward declarations of nested types, such as
18109 class C1::C2;
18110 class C1::C2::C3;
18112 are invalid unless all components preceding the final '::'
18113 are complete. If all enclosing types are complete, these
18114 declarations become merely pointless.
18116 Invalid forward declarations of nested types are errors
18117 caught elsewhere in parsing. Those that are pointless arrive
18118 here. */
18120 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18121 && !is_friend && !processing_explicit_instantiation)
18122 warning (0, "declaration %qD does not declare anything", decl);
18124 type = TREE_TYPE (decl);
18126 else
18128 /* An elaborated-type-specifier sometimes introduces a new type and
18129 sometimes names an existing type. Normally, the rule is that it
18130 introduces a new type only if there is not an existing type of
18131 the same name already in scope. For example, given:
18133 struct S {};
18134 void f() { struct S s; }
18136 the `struct S' in the body of `f' is the same `struct S' as in
18137 the global scope; the existing definition is used. However, if
18138 there were no global declaration, this would introduce a new
18139 local class named `S'.
18141 An exception to this rule applies to the following code:
18143 namespace N { struct S; }
18145 Here, the elaborated-type-specifier names a new type
18146 unconditionally; even if there is already an `S' in the
18147 containing scope this declaration names a new type.
18148 This exception only applies if the elaborated-type-specifier
18149 forms the complete declaration:
18151 [class.name]
18153 A declaration consisting solely of `class-key identifier ;' is
18154 either a redeclaration of the name in the current scope or a
18155 forward declaration of the identifier as a class name. It
18156 introduces the name into the current scope.
18158 We are in this situation precisely when the next token is a `;'.
18160 An exception to the exception is that a `friend' declaration does
18161 *not* name a new type; i.e., given:
18163 struct S { friend struct T; };
18165 `T' is not a new type in the scope of `S'.
18167 Also, `new struct S' or `sizeof (struct S)' never results in the
18168 definition of a new type; a new type can only be declared in a
18169 declaration context. */
18171 tag_scope ts;
18172 bool template_p;
18174 if (is_friend)
18175 /* Friends have special name lookup rules. */
18176 ts = ts_within_enclosing_non_class;
18177 else if (is_declaration
18178 && cp_lexer_next_token_is (parser->lexer,
18179 CPP_SEMICOLON))
18180 /* This is a `class-key identifier ;' */
18181 ts = ts_current;
18182 else
18183 ts = ts_global;
18185 template_p =
18186 (template_parm_lists_apply
18187 && (cp_parser_next_token_starts_class_definition_p (parser)
18188 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18189 /* An unqualified name was used to reference this type, so
18190 there were no qualifying templates. */
18191 if (template_parm_lists_apply
18192 && !cp_parser_check_template_parameters (parser,
18193 /*num_templates=*/0,
18194 /*template_id*/false,
18195 token->location,
18196 /*declarator=*/NULL))
18197 return error_mark_node;
18198 type = xref_tag (tag_type, identifier, ts, template_p);
18202 if (type == error_mark_node)
18203 return error_mark_node;
18205 /* Allow attributes on forward declarations of classes. */
18206 if (attributes)
18208 if (TREE_CODE (type) == TYPENAME_TYPE)
18209 warning (OPT_Wattributes,
18210 "attributes ignored on uninstantiated type");
18211 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18212 && ! processing_explicit_instantiation)
18213 warning (OPT_Wattributes,
18214 "attributes ignored on template instantiation");
18215 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18216 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18217 else
18218 warning (OPT_Wattributes,
18219 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18222 if (tag_type != enum_type)
18224 /* Indicate whether this class was declared as a `class' or as a
18225 `struct'. */
18226 if (CLASS_TYPE_P (type))
18227 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18228 cp_parser_check_class_key (tag_type, type);
18231 /* A "<" cannot follow an elaborated type specifier. If that
18232 happens, the user was probably trying to form a template-id. */
18233 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18234 token->location);
18236 return type;
18239 /* Parse an enum-specifier.
18241 enum-specifier:
18242 enum-head { enumerator-list [opt] }
18243 enum-head { enumerator-list , } [C++0x]
18245 enum-head:
18246 enum-key identifier [opt] enum-base [opt]
18247 enum-key nested-name-specifier identifier enum-base [opt]
18249 enum-key:
18250 enum
18251 enum class [C++0x]
18252 enum struct [C++0x]
18254 enum-base: [C++0x]
18255 : type-specifier-seq
18257 opaque-enum-specifier:
18258 enum-key identifier enum-base [opt] ;
18260 GNU Extensions:
18261 enum-key attributes[opt] identifier [opt] enum-base [opt]
18262 { enumerator-list [opt] }attributes[opt]
18263 enum-key attributes[opt] identifier [opt] enum-base [opt]
18264 { enumerator-list, }attributes[opt] [C++0x]
18266 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18267 if the token stream isn't an enum-specifier after all. */
18269 static tree
18270 cp_parser_enum_specifier (cp_parser* parser)
18272 tree identifier;
18273 tree type = NULL_TREE;
18274 tree prev_scope;
18275 tree nested_name_specifier = NULL_TREE;
18276 tree attributes;
18277 bool scoped_enum_p = false;
18278 bool has_underlying_type = false;
18279 bool nested_being_defined = false;
18280 bool new_value_list = false;
18281 bool is_new_type = false;
18282 bool is_unnamed = false;
18283 tree underlying_type = NULL_TREE;
18284 cp_token *type_start_token = NULL;
18285 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18287 parser->colon_corrects_to_scope_p = false;
18289 /* Parse tentatively so that we can back up if we don't find a
18290 enum-specifier. */
18291 cp_parser_parse_tentatively (parser);
18293 /* Caller guarantees that the current token is 'enum', an identifier
18294 possibly follows, and the token after that is an opening brace.
18295 If we don't have an identifier, fabricate an anonymous name for
18296 the enumeration being defined. */
18297 cp_lexer_consume_token (parser->lexer);
18299 /* Parse the "class" or "struct", which indicates a scoped
18300 enumeration type in C++0x. */
18301 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18302 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18304 if (cxx_dialect < cxx11)
18305 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18307 /* Consume the `struct' or `class' token. */
18308 cp_lexer_consume_token (parser->lexer);
18310 scoped_enum_p = true;
18313 attributes = cp_parser_attributes_opt (parser);
18315 /* Clear the qualification. */
18316 parser->scope = NULL_TREE;
18317 parser->qualifying_scope = NULL_TREE;
18318 parser->object_scope = NULL_TREE;
18320 /* Figure out in what scope the declaration is being placed. */
18321 prev_scope = current_scope ();
18323 type_start_token = cp_lexer_peek_token (parser->lexer);
18325 push_deferring_access_checks (dk_no_check);
18326 nested_name_specifier
18327 = cp_parser_nested_name_specifier_opt (parser,
18328 /*typename_keyword_p=*/true,
18329 /*check_dependency_p=*/false,
18330 /*type_p=*/false,
18331 /*is_declaration=*/false);
18333 if (nested_name_specifier)
18335 tree name;
18337 identifier = cp_parser_identifier (parser);
18338 name = cp_parser_lookup_name (parser, identifier,
18339 enum_type,
18340 /*is_template=*/false,
18341 /*is_namespace=*/false,
18342 /*check_dependency=*/true,
18343 /*ambiguous_decls=*/NULL,
18344 input_location);
18345 if (name && name != error_mark_node)
18347 type = TREE_TYPE (name);
18348 if (TREE_CODE (type) == TYPENAME_TYPE)
18350 /* Are template enums allowed in ISO? */
18351 if (template_parm_scope_p ())
18352 pedwarn (type_start_token->location, OPT_Wpedantic,
18353 "%qD is an enumeration template", name);
18354 /* ignore a typename reference, for it will be solved by name
18355 in start_enum. */
18356 type = NULL_TREE;
18359 else if (nested_name_specifier == error_mark_node)
18360 /* We already issued an error. */;
18361 else
18363 error_at (type_start_token->location,
18364 "%qD does not name an enumeration in %qT",
18365 identifier, nested_name_specifier);
18366 nested_name_specifier = error_mark_node;
18369 else
18371 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18372 identifier = cp_parser_identifier (parser);
18373 else
18375 identifier = make_anon_name ();
18376 is_unnamed = true;
18377 if (scoped_enum_p)
18378 error_at (type_start_token->location,
18379 "unnamed scoped enum is not allowed");
18382 pop_deferring_access_checks ();
18384 /* Check for the `:' that denotes a specified underlying type in C++0x.
18385 Note that a ':' could also indicate a bitfield width, however. */
18386 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18388 cp_decl_specifier_seq type_specifiers;
18390 /* Consume the `:'. */
18391 cp_lexer_consume_token (parser->lexer);
18393 /* Parse the type-specifier-seq. */
18394 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18395 /*is_trailing_return=*/false,
18396 &type_specifiers);
18398 /* At this point this is surely not elaborated type specifier. */
18399 if (!cp_parser_parse_definitely (parser))
18400 return NULL_TREE;
18402 if (cxx_dialect < cxx11)
18403 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18405 has_underlying_type = true;
18407 /* If that didn't work, stop. */
18408 if (type_specifiers.type != error_mark_node)
18410 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18411 /*initialized=*/0, NULL);
18412 if (underlying_type == error_mark_node
18413 || check_for_bare_parameter_packs (underlying_type))
18414 underlying_type = NULL_TREE;
18418 /* Look for the `{' but don't consume it yet. */
18419 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18421 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18423 cp_parser_error (parser, "expected %<{%>");
18424 if (has_underlying_type)
18426 type = NULL_TREE;
18427 goto out;
18430 /* An opaque-enum-specifier must have a ';' here. */
18431 if ((scoped_enum_p || underlying_type)
18432 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18434 cp_parser_error (parser, "expected %<;%> or %<{%>");
18435 if (has_underlying_type)
18437 type = NULL_TREE;
18438 goto out;
18443 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18444 return NULL_TREE;
18446 if (nested_name_specifier)
18448 if (CLASS_TYPE_P (nested_name_specifier))
18450 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18451 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18452 push_scope (nested_name_specifier);
18454 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18456 push_nested_namespace (nested_name_specifier);
18460 /* Issue an error message if type-definitions are forbidden here. */
18461 if (!cp_parser_check_type_definition (parser))
18462 type = error_mark_node;
18463 else
18464 /* Create the new type. We do this before consuming the opening
18465 brace so the enum will be recorded as being on the line of its
18466 tag (or the 'enum' keyword, if there is no tag). */
18467 type = start_enum (identifier, type, underlying_type,
18468 attributes, scoped_enum_p, &is_new_type);
18470 /* If the next token is not '{' it is an opaque-enum-specifier or an
18471 elaborated-type-specifier. */
18472 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18474 timevar_push (TV_PARSE_ENUM);
18475 if (nested_name_specifier
18476 && nested_name_specifier != error_mark_node)
18478 /* The following catches invalid code such as:
18479 enum class S<int>::E { A, B, C }; */
18480 if (!processing_specialization
18481 && CLASS_TYPE_P (nested_name_specifier)
18482 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18483 error_at (type_start_token->location, "cannot add an enumerator "
18484 "list to a template instantiation");
18486 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18488 error_at (type_start_token->location,
18489 "%<%T::%E%> has not been declared",
18490 TYPE_CONTEXT (nested_name_specifier),
18491 nested_name_specifier);
18492 type = error_mark_node;
18494 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18495 && !CLASS_TYPE_P (nested_name_specifier))
18497 error_at (type_start_token->location, "nested name specifier "
18498 "%qT for enum declaration does not name a class "
18499 "or namespace", nested_name_specifier);
18500 type = error_mark_node;
18502 /* If that scope does not contain the scope in which the
18503 class was originally declared, the program is invalid. */
18504 else if (prev_scope && !is_ancestor (prev_scope,
18505 nested_name_specifier))
18507 if (at_namespace_scope_p ())
18508 error_at (type_start_token->location,
18509 "declaration of %qD in namespace %qD which does not "
18510 "enclose %qD",
18511 type, prev_scope, nested_name_specifier);
18512 else
18513 error_at (type_start_token->location,
18514 "declaration of %qD in %qD which does not "
18515 "enclose %qD",
18516 type, prev_scope, nested_name_specifier);
18517 type = error_mark_node;
18519 /* If that scope is the scope where the declaration is being placed
18520 the program is invalid. */
18521 else if (CLASS_TYPE_P (nested_name_specifier)
18522 && CLASS_TYPE_P (prev_scope)
18523 && same_type_p (nested_name_specifier, prev_scope))
18525 permerror (type_start_token->location,
18526 "extra qualification not allowed");
18527 nested_name_specifier = NULL_TREE;
18531 if (scoped_enum_p)
18532 begin_scope (sk_scoped_enum, type);
18534 /* Consume the opening brace. */
18535 matching_braces braces;
18536 braces.consume_open (parser);
18538 if (type == error_mark_node)
18539 ; /* Nothing to add */
18540 else if (OPAQUE_ENUM_P (type)
18541 || (cxx_dialect > cxx98 && processing_specialization))
18543 new_value_list = true;
18544 SET_OPAQUE_ENUM_P (type, false);
18545 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18547 else
18549 error_at (type_start_token->location,
18550 "multiple definition of %q#T", type);
18551 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18552 "previous definition here");
18553 type = error_mark_node;
18556 if (type == error_mark_node)
18557 cp_parser_skip_to_end_of_block_or_statement (parser);
18558 /* If the next token is not '}', then there are some enumerators. */
18559 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18561 if (is_unnamed && !scoped_enum_p)
18562 pedwarn (type_start_token->location, OPT_Wpedantic,
18563 "ISO C++ forbids empty unnamed enum");
18565 else
18566 cp_parser_enumerator_list (parser, type);
18568 /* Consume the final '}'. */
18569 braces.require_close (parser);
18571 if (scoped_enum_p)
18572 finish_scope ();
18573 timevar_pop (TV_PARSE_ENUM);
18575 else
18577 /* If a ';' follows, then it is an opaque-enum-specifier
18578 and additional restrictions apply. */
18579 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18581 if (is_unnamed)
18582 error_at (type_start_token->location,
18583 "opaque-enum-specifier without name");
18584 else if (nested_name_specifier)
18585 error_at (type_start_token->location,
18586 "opaque-enum-specifier must use a simple identifier");
18590 /* Look for trailing attributes to apply to this enumeration, and
18591 apply them if appropriate. */
18592 if (cp_parser_allow_gnu_extensions_p (parser))
18594 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18595 cplus_decl_attributes (&type,
18596 trailing_attr,
18597 (int) ATTR_FLAG_TYPE_IN_PLACE);
18600 /* Finish up the enumeration. */
18601 if (type != error_mark_node)
18603 if (new_value_list)
18604 finish_enum_value_list (type);
18605 if (is_new_type)
18606 finish_enum (type);
18609 if (nested_name_specifier)
18611 if (CLASS_TYPE_P (nested_name_specifier))
18613 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18614 pop_scope (nested_name_specifier);
18616 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18618 pop_nested_namespace (nested_name_specifier);
18621 out:
18622 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18623 return type;
18626 /* Parse an enumerator-list. The enumerators all have the indicated
18627 TYPE.
18629 enumerator-list:
18630 enumerator-definition
18631 enumerator-list , enumerator-definition */
18633 static void
18634 cp_parser_enumerator_list (cp_parser* parser, tree type)
18636 while (true)
18638 /* Parse an enumerator-definition. */
18639 cp_parser_enumerator_definition (parser, type);
18641 /* If the next token is not a ',', we've reached the end of
18642 the list. */
18643 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18644 break;
18645 /* Otherwise, consume the `,' and keep going. */
18646 cp_lexer_consume_token (parser->lexer);
18647 /* If the next token is a `}', there is a trailing comma. */
18648 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18650 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18651 pedwarn (input_location, OPT_Wpedantic,
18652 "comma at end of enumerator list");
18653 break;
18658 /* Parse an enumerator-definition. The enumerator has the indicated
18659 TYPE.
18661 enumerator-definition:
18662 enumerator
18663 enumerator = constant-expression
18665 enumerator:
18666 identifier
18668 GNU Extensions:
18670 enumerator-definition:
18671 enumerator attributes [opt]
18672 enumerator attributes [opt] = constant-expression */
18674 static void
18675 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18677 tree identifier;
18678 tree value;
18679 location_t loc;
18681 /* Save the input location because we are interested in the location
18682 of the identifier and not the location of the explicit value. */
18683 loc = cp_lexer_peek_token (parser->lexer)->location;
18685 /* Look for the identifier. */
18686 identifier = cp_parser_identifier (parser);
18687 if (identifier == error_mark_node)
18688 return;
18690 /* Parse any specified attributes. */
18691 tree attrs = cp_parser_attributes_opt (parser);
18693 /* If the next token is an '=', then there is an explicit value. */
18694 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18696 /* Consume the `=' token. */
18697 cp_lexer_consume_token (parser->lexer);
18698 /* Parse the value. */
18699 value = cp_parser_constant_expression (parser);
18701 else
18702 value = NULL_TREE;
18704 /* If we are processing a template, make sure the initializer of the
18705 enumerator doesn't contain any bare template parameter pack. */
18706 if (check_for_bare_parameter_packs (value))
18707 value = error_mark_node;
18709 /* Create the enumerator. */
18710 build_enumerator (identifier, value, type, attrs, loc);
18713 /* Parse a namespace-name.
18715 namespace-name:
18716 original-namespace-name
18717 namespace-alias
18719 Returns the NAMESPACE_DECL for the namespace. */
18721 static tree
18722 cp_parser_namespace_name (cp_parser* parser)
18724 tree identifier;
18725 tree namespace_decl;
18727 cp_token *token = cp_lexer_peek_token (parser->lexer);
18729 /* Get the name of the namespace. */
18730 identifier = cp_parser_identifier (parser);
18731 if (identifier == error_mark_node)
18732 return error_mark_node;
18734 /* Look up the identifier in the currently active scope. Look only
18735 for namespaces, due to:
18737 [basic.lookup.udir]
18739 When looking up a namespace-name in a using-directive or alias
18740 definition, only namespace names are considered.
18742 And:
18744 [basic.lookup.qual]
18746 During the lookup of a name preceding the :: scope resolution
18747 operator, object, function, and enumerator names are ignored.
18749 (Note that cp_parser_qualifying_entity only calls this
18750 function if the token after the name is the scope resolution
18751 operator.) */
18752 namespace_decl = cp_parser_lookup_name (parser, identifier,
18753 none_type,
18754 /*is_template=*/false,
18755 /*is_namespace=*/true,
18756 /*check_dependency=*/true,
18757 /*ambiguous_decls=*/NULL,
18758 token->location);
18759 /* If it's not a namespace, issue an error. */
18760 if (namespace_decl == error_mark_node
18761 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18763 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18765 auto_diagnostic_group d;
18766 name_hint hint;
18767 if (namespace_decl == error_mark_node
18768 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18769 hint = suggest_alternative_in_explicit_scope (token->location,
18770 identifier,
18771 parser->scope);
18772 if (const char *suggestion = hint.suggestion ())
18774 gcc_rich_location richloc (token->location);
18775 richloc.add_fixit_replace (suggestion);
18776 error_at (&richloc,
18777 "%qD is not a namespace-name; did you mean %qs?",
18778 identifier, suggestion);
18780 else
18781 error_at (token->location, "%qD is not a namespace-name",
18782 identifier);
18784 else
18785 cp_parser_error (parser, "expected namespace-name");
18786 namespace_decl = error_mark_node;
18789 return namespace_decl;
18792 /* Parse a namespace-definition.
18794 namespace-definition:
18795 named-namespace-definition
18796 unnamed-namespace-definition
18798 named-namespace-definition:
18799 original-namespace-definition
18800 extension-namespace-definition
18802 original-namespace-definition:
18803 namespace identifier { namespace-body }
18805 extension-namespace-definition:
18806 namespace original-namespace-name { namespace-body }
18808 unnamed-namespace-definition:
18809 namespace { namespace-body } */
18811 static void
18812 cp_parser_namespace_definition (cp_parser* parser)
18814 tree identifier;
18815 int nested_definition_count = 0;
18817 cp_ensure_no_omp_declare_simd (parser);
18818 cp_ensure_no_oacc_routine (parser);
18820 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18822 if (is_inline)
18824 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18825 cp_lexer_consume_token (parser->lexer);
18828 /* Look for the `namespace' keyword. */
18829 cp_token* token
18830 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18832 /* Parse any specified attributes before the identifier. */
18833 tree attribs = cp_parser_attributes_opt (parser);
18835 for (;;)
18837 identifier = NULL_TREE;
18839 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18841 identifier = cp_parser_identifier (parser);
18843 if (cp_next_tokens_can_be_std_attribute_p (parser))
18844 pedwarn (input_location, OPT_Wpedantic,
18845 "standard attributes on namespaces must precede "
18846 "the namespace name");
18848 /* Parse any attributes specified after the identifier. */
18849 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18852 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18853 break;
18855 if (!nested_definition_count && cxx_dialect < cxx17)
18856 pedwarn (input_location, OPT_Wpedantic,
18857 "nested namespace definitions only available with "
18858 "-std=c++17 or -std=gnu++17");
18860 /* Nested namespace names can create new namespaces (unlike
18861 other qualified-ids). */
18862 if (int count = identifier ? push_namespace (identifier) : 0)
18863 nested_definition_count += count;
18864 else
18865 cp_parser_error (parser, "nested namespace name required");
18866 cp_lexer_consume_token (parser->lexer);
18869 if (nested_definition_count && !identifier)
18870 cp_parser_error (parser, "namespace name required");
18872 if (nested_definition_count && attribs)
18873 error_at (token->location,
18874 "a nested namespace definition cannot have attributes");
18875 if (nested_definition_count && is_inline)
18876 error_at (token->location,
18877 "a nested namespace definition cannot be inline");
18879 /* Start the namespace. */
18880 nested_definition_count += push_namespace (identifier, is_inline);
18882 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18884 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18886 /* Look for the `{' to validate starting the namespace. */
18887 matching_braces braces;
18888 if (braces.require_open (parser))
18890 /* Parse the body of the namespace. */
18891 cp_parser_namespace_body (parser);
18893 /* Look for the final `}'. */
18894 braces.require_close (parser);
18897 if (has_visibility)
18898 pop_visibility (1);
18900 /* Pop the nested namespace definitions. */
18901 while (nested_definition_count--)
18902 pop_namespace ();
18905 /* Parse a namespace-body.
18907 namespace-body:
18908 declaration-seq [opt] */
18910 static void
18911 cp_parser_namespace_body (cp_parser* parser)
18913 cp_parser_declaration_seq_opt (parser);
18916 /* Parse a namespace-alias-definition.
18918 namespace-alias-definition:
18919 namespace identifier = qualified-namespace-specifier ; */
18921 static void
18922 cp_parser_namespace_alias_definition (cp_parser* parser)
18924 tree identifier;
18925 tree namespace_specifier;
18927 cp_token *token = cp_lexer_peek_token (parser->lexer);
18929 /* Look for the `namespace' keyword. */
18930 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18931 /* Look for the identifier. */
18932 identifier = cp_parser_identifier (parser);
18933 if (identifier == error_mark_node)
18934 return;
18935 /* Look for the `=' token. */
18936 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18937 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18939 error_at (token->location, "%<namespace%> definition is not allowed here");
18940 /* Skip the definition. */
18941 cp_lexer_consume_token (parser->lexer);
18942 if (cp_parser_skip_to_closing_brace (parser))
18943 cp_lexer_consume_token (parser->lexer);
18944 return;
18946 cp_parser_require (parser, CPP_EQ, RT_EQ);
18947 /* Look for the qualified-namespace-specifier. */
18948 namespace_specifier
18949 = cp_parser_qualified_namespace_specifier (parser);
18950 /* Look for the `;' token. */
18951 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18953 /* Register the alias in the symbol table. */
18954 do_namespace_alias (identifier, namespace_specifier);
18957 /* Parse a qualified-namespace-specifier.
18959 qualified-namespace-specifier:
18960 :: [opt] nested-name-specifier [opt] namespace-name
18962 Returns a NAMESPACE_DECL corresponding to the specified
18963 namespace. */
18965 static tree
18966 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18968 /* Look for the optional `::'. */
18969 cp_parser_global_scope_opt (parser,
18970 /*current_scope_valid_p=*/false);
18972 /* Look for the optional nested-name-specifier. */
18973 cp_parser_nested_name_specifier_opt (parser,
18974 /*typename_keyword_p=*/false,
18975 /*check_dependency_p=*/true,
18976 /*type_p=*/false,
18977 /*is_declaration=*/true);
18979 return cp_parser_namespace_name (parser);
18982 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18983 access declaration.
18985 using-declaration:
18986 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18987 using :: unqualified-id ;
18989 access-declaration:
18990 qualified-id ;
18994 static bool
18995 cp_parser_using_declaration (cp_parser* parser,
18996 bool access_declaration_p)
18998 cp_token *token;
18999 bool typename_p = false;
19000 bool global_scope_p;
19001 tree decl;
19002 tree identifier;
19003 tree qscope;
19004 int oldcount = errorcount;
19005 cp_token *diag_token = NULL;
19007 if (access_declaration_p)
19009 diag_token = cp_lexer_peek_token (parser->lexer);
19010 cp_parser_parse_tentatively (parser);
19012 else
19014 /* Look for the `using' keyword. */
19015 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19017 again:
19018 /* Peek at the next token. */
19019 token = cp_lexer_peek_token (parser->lexer);
19020 /* See if it's `typename'. */
19021 if (token->keyword == RID_TYPENAME)
19023 /* Remember that we've seen it. */
19024 typename_p = true;
19025 /* Consume the `typename' token. */
19026 cp_lexer_consume_token (parser->lexer);
19030 /* Look for the optional global scope qualification. */
19031 global_scope_p
19032 = (cp_parser_global_scope_opt (parser,
19033 /*current_scope_valid_p=*/false)
19034 != NULL_TREE);
19036 /* If we saw `typename', or didn't see `::', then there must be a
19037 nested-name-specifier present. */
19038 if (typename_p || !global_scope_p)
19040 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19041 /*check_dependency_p=*/true,
19042 /*type_p=*/false,
19043 /*is_declaration=*/true);
19044 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19046 cp_parser_skip_to_end_of_block_or_statement (parser);
19047 return false;
19050 /* Otherwise, we could be in either of the two productions. In that
19051 case, treat the nested-name-specifier as optional. */
19052 else
19053 qscope = cp_parser_nested_name_specifier_opt (parser,
19054 /*typename_keyword_p=*/false,
19055 /*check_dependency_p=*/true,
19056 /*type_p=*/false,
19057 /*is_declaration=*/true);
19058 if (!qscope)
19059 qscope = global_namespace;
19060 else if (UNSCOPED_ENUM_P (qscope))
19061 qscope = CP_TYPE_CONTEXT (qscope);
19063 if (access_declaration_p && cp_parser_error_occurred (parser))
19064 /* Something has already gone wrong; there's no need to parse
19065 further. Since an error has occurred, the return value of
19066 cp_parser_parse_definitely will be false, as required. */
19067 return cp_parser_parse_definitely (parser);
19069 token = cp_lexer_peek_token (parser->lexer);
19070 /* Parse the unqualified-id. */
19071 identifier = cp_parser_unqualified_id (parser,
19072 /*template_keyword_p=*/false,
19073 /*check_dependency_p=*/true,
19074 /*declarator_p=*/true,
19075 /*optional_p=*/false);
19077 if (access_declaration_p)
19079 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19080 cp_parser_simulate_error (parser);
19081 if (!cp_parser_parse_definitely (parser))
19082 return false;
19084 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19086 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19087 if (cxx_dialect < cxx17
19088 && !in_system_header_at (ell->location))
19089 pedwarn (ell->location, 0,
19090 "pack expansion in using-declaration only available "
19091 "with -std=c++17 or -std=gnu++17");
19092 qscope = make_pack_expansion (qscope);
19095 /* The function we call to handle a using-declaration is different
19096 depending on what scope we are in. */
19097 if (qscope == error_mark_node || identifier == error_mark_node)
19099 else if (!identifier_p (identifier)
19100 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19101 /* [namespace.udecl]
19103 A using declaration shall not name a template-id. */
19104 error_at (token->location,
19105 "a template-id may not appear in a using-declaration");
19106 else
19108 if (at_class_scope_p ())
19110 /* Create the USING_DECL. */
19111 decl = do_class_using_decl (qscope, identifier);
19113 if (decl && typename_p)
19114 USING_DECL_TYPENAME_P (decl) = 1;
19116 if (check_for_bare_parameter_packs (decl))
19118 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19119 return false;
19121 else
19122 /* Add it to the list of members in this class. */
19123 finish_member_declaration (decl);
19125 else
19127 decl = cp_parser_lookup_name_simple (parser,
19128 identifier,
19129 token->location);
19130 if (decl == error_mark_node)
19131 cp_parser_name_lookup_error (parser, identifier,
19132 decl, NLE_NULL,
19133 token->location);
19134 else if (check_for_bare_parameter_packs (decl))
19136 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19137 return false;
19139 else if (!at_namespace_scope_p ())
19140 finish_local_using_decl (decl, qscope, identifier);
19141 else
19142 finish_namespace_using_decl (decl, qscope, identifier);
19146 if (!access_declaration_p
19147 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19149 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19150 if (cxx_dialect < cxx17)
19151 pedwarn (comma->location, 0,
19152 "comma-separated list in using-declaration only available "
19153 "with -std=c++17 or -std=gnu++17");
19154 goto again;
19157 /* Look for the final `;'. */
19158 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19160 if (access_declaration_p && errorcount == oldcount)
19161 warning_at (diag_token->location, OPT_Wdeprecated,
19162 "access declarations are deprecated "
19163 "in favour of using-declarations; "
19164 "suggestion: add the %<using%> keyword");
19166 return true;
19169 /* Parse an alias-declaration.
19171 alias-declaration:
19172 using identifier attribute-specifier-seq [opt] = type-id */
19174 static tree
19175 cp_parser_alias_declaration (cp_parser* parser)
19177 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19178 location_t id_location, type_location;
19179 cp_declarator *declarator;
19180 cp_decl_specifier_seq decl_specs;
19181 bool member_p;
19182 const char *saved_message = NULL;
19184 /* Look for the `using' keyword. */
19185 cp_token *using_token
19186 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19187 if (using_token == NULL)
19188 return error_mark_node;
19190 id_location = cp_lexer_peek_token (parser->lexer)->location;
19191 id = cp_parser_identifier (parser);
19192 if (id == error_mark_node)
19193 return error_mark_node;
19195 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19196 attributes = cp_parser_attributes_opt (parser);
19197 if (attributes == error_mark_node)
19198 return error_mark_node;
19200 cp_parser_require (parser, CPP_EQ, RT_EQ);
19202 if (cp_parser_error_occurred (parser))
19203 return error_mark_node;
19205 cp_parser_commit_to_tentative_parse (parser);
19207 /* Now we are going to parse the type-id of the declaration. */
19210 [dcl.type]/3 says:
19212 "A type-specifier-seq shall not define a class or enumeration
19213 unless it appears in the type-id of an alias-declaration (7.1.3) that
19214 is not the declaration of a template-declaration."
19216 In other words, if we currently are in an alias template, the
19217 type-id should not define a type.
19219 So let's set parser->type_definition_forbidden_message in that
19220 case; cp_parser_check_type_definition (called by
19221 cp_parser_class_specifier) will then emit an error if a type is
19222 defined in the type-id. */
19223 if (parser->num_template_parameter_lists)
19225 saved_message = parser->type_definition_forbidden_message;
19226 parser->type_definition_forbidden_message =
19227 G_("types may not be defined in alias template declarations");
19230 type = cp_parser_type_id (parser, &type_location);
19232 /* Restore the error message if need be. */
19233 if (parser->num_template_parameter_lists)
19234 parser->type_definition_forbidden_message = saved_message;
19236 if (type == error_mark_node
19237 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19239 cp_parser_skip_to_end_of_block_or_statement (parser);
19240 return error_mark_node;
19243 /* A typedef-name can also be introduced by an alias-declaration. The
19244 identifier following the using keyword becomes a typedef-name. It has
19245 the same semantics as if it were introduced by the typedef
19246 specifier. In particular, it does not define a new type and it shall
19247 not appear in the type-id. */
19249 clear_decl_specs (&decl_specs);
19250 decl_specs.type = type;
19251 if (attributes != NULL_TREE)
19253 decl_specs.attributes = attributes;
19254 set_and_check_decl_spec_loc (&decl_specs,
19255 ds_attribute,
19256 attrs_token);
19258 set_and_check_decl_spec_loc (&decl_specs,
19259 ds_typedef,
19260 using_token);
19261 set_and_check_decl_spec_loc (&decl_specs,
19262 ds_alias,
19263 using_token);
19264 decl_specs.locations[ds_type_spec] = type_location;
19266 if (parser->num_template_parameter_lists
19267 && !cp_parser_check_template_parameters (parser,
19268 /*num_templates=*/0,
19269 /*template_id*/false,
19270 id_location,
19271 /*declarator=*/NULL))
19272 return error_mark_node;
19274 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19275 declarator->id_loc = id_location;
19277 member_p = at_class_scope_p ();
19278 if (member_p)
19279 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19280 NULL_TREE, attributes);
19281 else
19282 decl = start_decl (declarator, &decl_specs, 0,
19283 attributes, NULL_TREE, &pushed_scope);
19284 if (decl == error_mark_node)
19285 return decl;
19287 // Attach constraints to the alias declaration.
19288 if (flag_concepts && current_template_parms)
19290 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19291 tree constr = build_constraints (reqs, NULL_TREE);
19292 set_constraints (decl, constr);
19295 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19297 if (pushed_scope)
19298 pop_scope (pushed_scope);
19300 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19301 added into the symbol table; otherwise, return the TYPE_DECL. */
19302 if (DECL_LANG_SPECIFIC (decl)
19303 && DECL_TEMPLATE_INFO (decl)
19304 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19306 decl = DECL_TI_TEMPLATE (decl);
19307 if (member_p)
19308 check_member_template (decl);
19311 return decl;
19314 /* Parse a using-directive.
19316 using-directive:
19317 using namespace :: [opt] nested-name-specifier [opt]
19318 namespace-name ; */
19320 static void
19321 cp_parser_using_directive (cp_parser* parser)
19323 tree namespace_decl;
19324 tree attribs;
19326 /* Look for the `using' keyword. */
19327 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19328 /* And the `namespace' keyword. */
19329 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19330 /* Look for the optional `::' operator. */
19331 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19332 /* And the optional nested-name-specifier. */
19333 cp_parser_nested_name_specifier_opt (parser,
19334 /*typename_keyword_p=*/false,
19335 /*check_dependency_p=*/true,
19336 /*type_p=*/false,
19337 /*is_declaration=*/true);
19338 /* Get the namespace being used. */
19339 namespace_decl = cp_parser_namespace_name (parser);
19340 /* And any specified attributes. */
19341 attribs = cp_parser_attributes_opt (parser);
19343 /* Update the symbol table. */
19344 if (namespace_bindings_p ())
19345 finish_namespace_using_directive (namespace_decl, attribs);
19346 else
19347 finish_local_using_directive (namespace_decl, attribs);
19349 /* Look for the final `;'. */
19350 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19353 /* Parse an asm-definition.
19355 asm-definition:
19356 asm ( string-literal ) ;
19358 GNU Extension:
19360 asm-definition:
19361 asm volatile [opt] ( string-literal ) ;
19362 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19363 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19364 : asm-operand-list [opt] ) ;
19365 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19366 : asm-operand-list [opt]
19367 : asm-clobber-list [opt] ) ;
19368 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19369 : asm-clobber-list [opt]
19370 : asm-goto-list ) ; */
19372 static void
19373 cp_parser_asm_definition (cp_parser* parser)
19375 tree string;
19376 tree outputs = NULL_TREE;
19377 tree inputs = NULL_TREE;
19378 tree clobbers = NULL_TREE;
19379 tree labels = NULL_TREE;
19380 tree asm_stmt;
19381 bool volatile_p = false;
19382 bool extended_p = false;
19383 bool invalid_inputs_p = false;
19384 bool invalid_outputs_p = false;
19385 bool goto_p = false;
19386 required_token missing = RT_NONE;
19388 /* Look for the `asm' keyword. */
19389 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19391 if (parser->in_function_body
19392 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19394 error ("%<asm%> in %<constexpr%> function");
19395 cp_function_chain->invalid_constexpr = true;
19398 /* See if the next token is `volatile'. */
19399 if (cp_parser_allow_gnu_extensions_p (parser)
19400 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19402 /* Remember that we saw the `volatile' keyword. */
19403 volatile_p = true;
19404 /* Consume the token. */
19405 cp_lexer_consume_token (parser->lexer);
19407 if (cp_parser_allow_gnu_extensions_p (parser)
19408 && parser->in_function_body
19409 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19411 /* Remember that we saw the `goto' keyword. */
19412 goto_p = true;
19413 /* Consume the token. */
19414 cp_lexer_consume_token (parser->lexer);
19416 /* Look for the opening `('. */
19417 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19418 return;
19419 /* Look for the string. */
19420 string = cp_parser_string_literal (parser, false, false);
19421 if (string == error_mark_node)
19423 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19424 /*consume_paren=*/true);
19425 return;
19428 /* If we're allowing GNU extensions, check for the extended assembly
19429 syntax. Unfortunately, the `:' tokens need not be separated by
19430 a space in C, and so, for compatibility, we tolerate that here
19431 too. Doing that means that we have to treat the `::' operator as
19432 two `:' tokens. */
19433 if (cp_parser_allow_gnu_extensions_p (parser)
19434 && parser->in_function_body
19435 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19436 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19438 bool inputs_p = false;
19439 bool clobbers_p = false;
19440 bool labels_p = false;
19442 /* The extended syntax was used. */
19443 extended_p = true;
19445 /* Look for outputs. */
19446 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19448 /* Consume the `:'. */
19449 cp_lexer_consume_token (parser->lexer);
19450 /* Parse the output-operands. */
19451 if (cp_lexer_next_token_is_not (parser->lexer,
19452 CPP_COLON)
19453 && cp_lexer_next_token_is_not (parser->lexer,
19454 CPP_SCOPE)
19455 && cp_lexer_next_token_is_not (parser->lexer,
19456 CPP_CLOSE_PAREN)
19457 && !goto_p)
19459 outputs = cp_parser_asm_operand_list (parser);
19460 if (outputs == error_mark_node)
19461 invalid_outputs_p = true;
19464 /* If the next token is `::', there are no outputs, and the
19465 next token is the beginning of the inputs. */
19466 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19467 /* The inputs are coming next. */
19468 inputs_p = true;
19470 /* Look for inputs. */
19471 if (inputs_p
19472 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19474 /* Consume the `:' or `::'. */
19475 cp_lexer_consume_token (parser->lexer);
19476 /* Parse the output-operands. */
19477 if (cp_lexer_next_token_is_not (parser->lexer,
19478 CPP_COLON)
19479 && cp_lexer_next_token_is_not (parser->lexer,
19480 CPP_SCOPE)
19481 && cp_lexer_next_token_is_not (parser->lexer,
19482 CPP_CLOSE_PAREN))
19484 inputs = cp_parser_asm_operand_list (parser);
19485 if (inputs == error_mark_node)
19486 invalid_inputs_p = true;
19489 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19490 /* The clobbers are coming next. */
19491 clobbers_p = true;
19493 /* Look for clobbers. */
19494 if (clobbers_p
19495 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19497 clobbers_p = true;
19498 /* Consume the `:' or `::'. */
19499 cp_lexer_consume_token (parser->lexer);
19500 /* Parse the clobbers. */
19501 if (cp_lexer_next_token_is_not (parser->lexer,
19502 CPP_COLON)
19503 && cp_lexer_next_token_is_not (parser->lexer,
19504 CPP_CLOSE_PAREN))
19505 clobbers = cp_parser_asm_clobber_list (parser);
19507 else if (goto_p
19508 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19509 /* The labels are coming next. */
19510 labels_p = true;
19512 /* Look for labels. */
19513 if (labels_p
19514 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19516 labels_p = true;
19517 /* Consume the `:' or `::'. */
19518 cp_lexer_consume_token (parser->lexer);
19519 /* Parse the labels. */
19520 labels = cp_parser_asm_label_list (parser);
19523 if (goto_p && !labels_p)
19524 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19526 else if (goto_p)
19527 missing = RT_COLON_SCOPE;
19529 /* Look for the closing `)'. */
19530 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19531 missing ? missing : RT_CLOSE_PAREN))
19532 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19533 /*consume_paren=*/true);
19534 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19536 if (!invalid_inputs_p && !invalid_outputs_p)
19538 /* Create the ASM_EXPR. */
19539 if (parser->in_function_body)
19541 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19542 inputs, clobbers, labels);
19543 /* If the extended syntax was not used, mark the ASM_EXPR. */
19544 if (!extended_p)
19546 tree temp = asm_stmt;
19547 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19548 temp = TREE_OPERAND (temp, 0);
19550 ASM_INPUT_P (temp) = 1;
19553 else
19554 symtab->finalize_toplevel_asm (string);
19558 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19559 type that comes from the decl-specifier-seq. */
19561 static tree
19562 strip_declarator_types (tree type, cp_declarator *declarator)
19564 for (cp_declarator *d = declarator; d;)
19565 switch (d->kind)
19567 case cdk_id:
19568 case cdk_decomp:
19569 case cdk_error:
19570 d = NULL;
19571 break;
19573 default:
19574 if (TYPE_PTRMEMFUNC_P (type))
19575 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19576 type = TREE_TYPE (type);
19577 d = d->declarator;
19578 break;
19581 return type;
19584 /* Declarators [gram.dcl.decl] */
19586 /* Parse an init-declarator.
19588 init-declarator:
19589 declarator initializer [opt]
19591 GNU Extension:
19593 init-declarator:
19594 declarator asm-specification [opt] attributes [opt] initializer [opt]
19596 function-definition:
19597 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19598 function-body
19599 decl-specifier-seq [opt] declarator function-try-block
19601 GNU Extension:
19603 function-definition:
19604 __extension__ function-definition
19606 TM Extension:
19608 function-definition:
19609 decl-specifier-seq [opt] declarator function-transaction-block
19611 The DECL_SPECIFIERS apply to this declarator. Returns a
19612 representation of the entity declared. If MEMBER_P is TRUE, then
19613 this declarator appears in a class scope. The new DECL created by
19614 this declarator is returned.
19616 The CHECKS are access checks that should be performed once we know
19617 what entity is being declared (and, therefore, what classes have
19618 befriended it).
19620 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19621 for a function-definition here as well. If the declarator is a
19622 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19623 be TRUE upon return. By that point, the function-definition will
19624 have been completely parsed.
19626 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19627 is FALSE.
19629 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19630 parsed declaration if it is an uninitialized single declarator not followed
19631 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19632 if present, will not be consumed. If returned, this declarator will be
19633 created with SD_INITIALIZED but will not call cp_finish_decl.
19635 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19636 and there is an initializer, the pointed location_t is set to the
19637 location of the '=' or `(', or '{' in C++11 token introducing the
19638 initializer. */
19640 static tree
19641 cp_parser_init_declarator (cp_parser* parser,
19642 cp_decl_specifier_seq *decl_specifiers,
19643 vec<deferred_access_check, va_gc> *checks,
19644 bool function_definition_allowed_p,
19645 bool member_p,
19646 int declares_class_or_enum,
19647 bool* function_definition_p,
19648 tree* maybe_range_for_decl,
19649 location_t* init_loc,
19650 tree* auto_result)
19652 cp_token *token = NULL, *asm_spec_start_token = NULL,
19653 *attributes_start_token = NULL;
19654 cp_declarator *declarator;
19655 tree prefix_attributes;
19656 tree attributes = NULL;
19657 tree asm_specification;
19658 tree initializer;
19659 tree decl = NULL_TREE;
19660 tree scope;
19661 int is_initialized;
19662 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19663 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19664 "(...)". */
19665 enum cpp_ttype initialization_kind;
19666 bool is_direct_init = false;
19667 bool is_non_constant_init;
19668 int ctor_dtor_or_conv_p;
19669 bool friend_p = cp_parser_friend_p (decl_specifiers);
19670 tree pushed_scope = NULL_TREE;
19671 bool range_for_decl_p = false;
19672 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19673 location_t tmp_init_loc = UNKNOWN_LOCATION;
19675 /* Gather the attributes that were provided with the
19676 decl-specifiers. */
19677 prefix_attributes = decl_specifiers->attributes;
19679 /* Assume that this is not the declarator for a function
19680 definition. */
19681 if (function_definition_p)
19682 *function_definition_p = false;
19684 /* Default arguments are only permitted for function parameters. */
19685 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19686 parser->default_arg_ok_p = false;
19688 /* Defer access checks while parsing the declarator; we cannot know
19689 what names are accessible until we know what is being
19690 declared. */
19691 resume_deferring_access_checks ();
19693 token = cp_lexer_peek_token (parser->lexer);
19695 /* Parse the declarator. */
19696 declarator
19697 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19698 &ctor_dtor_or_conv_p,
19699 /*parenthesized_p=*/NULL,
19700 member_p, friend_p);
19701 /* Gather up the deferred checks. */
19702 stop_deferring_access_checks ();
19704 parser->default_arg_ok_p = saved_default_arg_ok_p;
19706 /* If the DECLARATOR was erroneous, there's no need to go
19707 further. */
19708 if (declarator == cp_error_declarator)
19709 return error_mark_node;
19711 /* Check that the number of template-parameter-lists is OK. */
19712 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19713 token->location))
19714 return error_mark_node;
19716 if (declares_class_or_enum & 2)
19717 cp_parser_check_for_definition_in_return_type (declarator,
19718 decl_specifiers->type,
19719 decl_specifiers->locations[ds_type_spec]);
19721 /* Figure out what scope the entity declared by the DECLARATOR is
19722 located in. `grokdeclarator' sometimes changes the scope, so
19723 we compute it now. */
19724 scope = get_scope_of_declarator (declarator);
19726 /* Perform any lookups in the declared type which were thought to be
19727 dependent, but are not in the scope of the declarator. */
19728 decl_specifiers->type
19729 = maybe_update_decl_type (decl_specifiers->type, scope);
19731 /* If we're allowing GNU extensions, look for an
19732 asm-specification. */
19733 if (cp_parser_allow_gnu_extensions_p (parser))
19735 /* Look for an asm-specification. */
19736 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19737 asm_specification = cp_parser_asm_specification_opt (parser);
19739 else
19740 asm_specification = NULL_TREE;
19742 /* Look for attributes. */
19743 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19744 attributes = cp_parser_attributes_opt (parser);
19746 /* Peek at the next token. */
19747 token = cp_lexer_peek_token (parser->lexer);
19749 bool bogus_implicit_tmpl = false;
19751 if (function_declarator_p (declarator))
19753 /* Handle C++17 deduction guides. */
19754 if (!decl_specifiers->type
19755 && ctor_dtor_or_conv_p <= 0
19756 && cxx_dialect >= cxx17)
19758 cp_declarator *id = get_id_declarator (declarator);
19759 tree name = id->u.id.unqualified_name;
19760 parser->scope = id->u.id.qualifying_scope;
19761 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19762 if (tmpl
19763 && (DECL_CLASS_TEMPLATE_P (tmpl)
19764 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19766 id->u.id.unqualified_name = dguide_name (tmpl);
19767 id->u.id.sfk = sfk_deduction_guide;
19768 ctor_dtor_or_conv_p = 1;
19772 /* Check to see if the token indicates the start of a
19773 function-definition. */
19774 if (cp_parser_token_starts_function_definition_p (token))
19776 if (!function_definition_allowed_p)
19778 /* If a function-definition should not appear here, issue an
19779 error message. */
19780 cp_parser_error (parser,
19781 "a function-definition is not allowed here");
19782 return error_mark_node;
19785 location_t func_brace_location
19786 = cp_lexer_peek_token (parser->lexer)->location;
19788 /* Neither attributes nor an asm-specification are allowed
19789 on a function-definition. */
19790 if (asm_specification)
19791 error_at (asm_spec_start_token->location,
19792 "an asm-specification is not allowed "
19793 "on a function-definition");
19794 if (attributes)
19795 error_at (attributes_start_token->location,
19796 "attributes are not allowed "
19797 "on a function-definition");
19798 /* This is a function-definition. */
19799 *function_definition_p = true;
19801 /* Parse the function definition. */
19802 if (member_p)
19803 decl = cp_parser_save_member_function_body (parser,
19804 decl_specifiers,
19805 declarator,
19806 prefix_attributes);
19807 else
19808 decl =
19809 (cp_parser_function_definition_from_specifiers_and_declarator
19810 (parser, decl_specifiers, prefix_attributes, declarator));
19812 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19814 /* This is where the prologue starts... */
19815 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19816 = func_brace_location;
19819 return decl;
19822 else if (parser->fully_implicit_function_template_p)
19824 /* A non-template declaration involving a function parameter list
19825 containing an implicit template parameter will be made into a
19826 template. If the resulting declaration is not going to be an
19827 actual function then finish the template scope here to prevent it.
19828 An error message will be issued once we have a decl to talk about.
19830 FIXME probably we should do type deduction rather than create an
19831 implicit template, but the standard currently doesn't allow it. */
19832 bogus_implicit_tmpl = true;
19833 finish_fully_implicit_template (parser, NULL_TREE);
19836 /* [dcl.dcl]
19838 Only in function declarations for constructors, destructors, type
19839 conversions, and deduction guides can the decl-specifier-seq be omitted.
19841 We explicitly postpone this check past the point where we handle
19842 function-definitions because we tolerate function-definitions
19843 that are missing their return types in some modes. */
19844 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19846 cp_parser_error (parser,
19847 "expected constructor, destructor, or type conversion");
19848 return error_mark_node;
19851 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19852 if (token->type == CPP_EQ
19853 || token->type == CPP_OPEN_PAREN
19854 || token->type == CPP_OPEN_BRACE)
19856 is_initialized = SD_INITIALIZED;
19857 initialization_kind = token->type;
19858 if (maybe_range_for_decl)
19859 *maybe_range_for_decl = error_mark_node;
19860 tmp_init_loc = token->location;
19861 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19862 *init_loc = tmp_init_loc;
19864 if (token->type == CPP_EQ
19865 && function_declarator_p (declarator))
19867 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19868 if (t2->keyword == RID_DEFAULT)
19869 is_initialized = SD_DEFAULTED;
19870 else if (t2->keyword == RID_DELETE)
19871 is_initialized = SD_DELETED;
19874 else
19876 /* If the init-declarator isn't initialized and isn't followed by a
19877 `,' or `;', it's not a valid init-declarator. */
19878 if (token->type != CPP_COMMA
19879 && token->type != CPP_SEMICOLON)
19881 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19882 range_for_decl_p = true;
19883 else
19885 if (!maybe_range_for_decl)
19886 cp_parser_error (parser, "expected initializer");
19887 return error_mark_node;
19890 is_initialized = SD_UNINITIALIZED;
19891 initialization_kind = CPP_EOF;
19894 /* Because start_decl has side-effects, we should only call it if we
19895 know we're going ahead. By this point, we know that we cannot
19896 possibly be looking at any other construct. */
19897 cp_parser_commit_to_tentative_parse (parser);
19899 /* Enter the newly declared entry in the symbol table. If we're
19900 processing a declaration in a class-specifier, we wait until
19901 after processing the initializer. */
19902 if (!member_p)
19904 if (parser->in_unbraced_linkage_specification_p)
19905 decl_specifiers->storage_class = sc_extern;
19906 decl = start_decl (declarator, decl_specifiers,
19907 range_for_decl_p? SD_INITIALIZED : is_initialized,
19908 attributes, prefix_attributes, &pushed_scope);
19909 cp_finalize_omp_declare_simd (parser, decl);
19910 cp_finalize_oacc_routine (parser, decl, false);
19911 /* Adjust location of decl if declarator->id_loc is more appropriate:
19912 set, and decl wasn't merged with another decl, in which case its
19913 location would be different from input_location, and more accurate. */
19914 if (DECL_P (decl)
19915 && declarator->id_loc != UNKNOWN_LOCATION
19916 && DECL_SOURCE_LOCATION (decl) == input_location)
19917 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19919 else if (scope)
19920 /* Enter the SCOPE. That way unqualified names appearing in the
19921 initializer will be looked up in SCOPE. */
19922 pushed_scope = push_scope (scope);
19924 /* Perform deferred access control checks, now that we know in which
19925 SCOPE the declared entity resides. */
19926 if (!member_p && decl)
19928 tree saved_current_function_decl = NULL_TREE;
19930 /* If the entity being declared is a function, pretend that we
19931 are in its scope. If it is a `friend', it may have access to
19932 things that would not otherwise be accessible. */
19933 if (TREE_CODE (decl) == FUNCTION_DECL)
19935 saved_current_function_decl = current_function_decl;
19936 current_function_decl = decl;
19939 /* Perform access checks for template parameters. */
19940 cp_parser_perform_template_parameter_access_checks (checks);
19942 /* Perform the access control checks for the declarator and the
19943 decl-specifiers. */
19944 perform_deferred_access_checks (tf_warning_or_error);
19946 /* Restore the saved value. */
19947 if (TREE_CODE (decl) == FUNCTION_DECL)
19948 current_function_decl = saved_current_function_decl;
19951 /* Parse the initializer. */
19952 initializer = NULL_TREE;
19953 is_direct_init = false;
19954 is_non_constant_init = true;
19955 if (is_initialized)
19957 if (function_declarator_p (declarator))
19959 if (initialization_kind == CPP_EQ)
19960 initializer = cp_parser_pure_specifier (parser);
19961 else
19963 /* If the declaration was erroneous, we don't really
19964 know what the user intended, so just silently
19965 consume the initializer. */
19966 if (decl != error_mark_node)
19967 error_at (tmp_init_loc, "initializer provided for function");
19968 cp_parser_skip_to_closing_parenthesis (parser,
19969 /*recovering=*/true,
19970 /*or_comma=*/false,
19971 /*consume_paren=*/true);
19974 else
19976 /* We want to record the extra mangling scope for in-class
19977 initializers of class members and initializers of static data
19978 member templates. The former involves deferring
19979 parsing of the initializer until end of class as with default
19980 arguments. So right here we only handle the latter. */
19981 if (!member_p && processing_template_decl && decl != error_mark_node)
19982 start_lambda_scope (decl);
19983 initializer = cp_parser_initializer (parser,
19984 &is_direct_init,
19985 &is_non_constant_init);
19986 if (!member_p && processing_template_decl && decl != error_mark_node)
19987 finish_lambda_scope ();
19988 if (initializer == error_mark_node)
19989 cp_parser_skip_to_end_of_statement (parser);
19993 /* The old parser allows attributes to appear after a parenthesized
19994 initializer. Mark Mitchell proposed removing this functionality
19995 on the GCC mailing lists on 2002-08-13. This parser accepts the
19996 attributes -- but ignores them. Made a permerror in GCC 8. */
19997 if (cp_parser_allow_gnu_extensions_p (parser)
19998 && initialization_kind == CPP_OPEN_PAREN
19999 && cp_parser_attributes_opt (parser)
20000 && permerror (input_location,
20001 "attributes after parenthesized initializer ignored"))
20003 static bool hint;
20004 if (flag_permissive && !hint)
20006 hint = true;
20007 inform (input_location,
20008 "this flexibility is deprecated and will be removed");
20012 /* And now complain about a non-function implicit template. */
20013 if (bogus_implicit_tmpl && decl != error_mark_node)
20014 error_at (DECL_SOURCE_LOCATION (decl),
20015 "non-function %qD declared as implicit template", decl);
20017 /* For an in-class declaration, use `grokfield' to create the
20018 declaration. */
20019 if (member_p)
20021 if (pushed_scope)
20023 pop_scope (pushed_scope);
20024 pushed_scope = NULL_TREE;
20026 decl = grokfield (declarator, decl_specifiers,
20027 initializer, !is_non_constant_init,
20028 /*asmspec=*/NULL_TREE,
20029 attr_chainon (attributes, prefix_attributes));
20030 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20031 cp_parser_save_default_args (parser, decl);
20032 cp_finalize_omp_declare_simd (parser, decl);
20033 cp_finalize_oacc_routine (parser, decl, false);
20036 /* Finish processing the declaration. But, skip member
20037 declarations. */
20038 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20040 cp_finish_decl (decl,
20041 initializer, !is_non_constant_init,
20042 asm_specification,
20043 /* If the initializer is in parentheses, then this is
20044 a direct-initialization, which means that an
20045 `explicit' constructor is OK. Otherwise, an
20046 `explicit' constructor cannot be used. */
20047 ((is_direct_init || !is_initialized)
20048 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20050 else if ((cxx_dialect != cxx98) && friend_p
20051 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20052 /* Core issue #226 (C++0x only): A default template-argument
20053 shall not be specified in a friend class template
20054 declaration. */
20055 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20056 /*is_partial=*/false, /*is_friend_decl=*/1);
20058 if (!friend_p && pushed_scope)
20059 pop_scope (pushed_scope);
20061 if (function_declarator_p (declarator)
20062 && parser->fully_implicit_function_template_p)
20064 if (member_p)
20065 decl = finish_fully_implicit_template (parser, decl);
20066 else
20067 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20070 if (auto_result && is_initialized && decl_specifiers->type
20071 && type_uses_auto (decl_specifiers->type))
20072 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20074 return decl;
20077 /* Parse a declarator.
20079 declarator:
20080 direct-declarator
20081 ptr-operator declarator
20083 abstract-declarator:
20084 ptr-operator abstract-declarator [opt]
20085 direct-abstract-declarator
20087 GNU Extensions:
20089 declarator:
20090 attributes [opt] direct-declarator
20091 attributes [opt] ptr-operator declarator
20093 abstract-declarator:
20094 attributes [opt] ptr-operator abstract-declarator [opt]
20095 attributes [opt] direct-abstract-declarator
20097 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20098 detect constructors, destructors, deduction guides, or conversion operators.
20099 It is set to -1 if the declarator is a name, and +1 if it is a
20100 function. Otherwise it is set to zero. Usually you just want to
20101 test for >0, but internally the negative value is used.
20103 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20104 a decl-specifier-seq unless it declares a constructor, destructor,
20105 or conversion. It might seem that we could check this condition in
20106 semantic analysis, rather than parsing, but that makes it difficult
20107 to handle something like `f()'. We want to notice that there are
20108 no decl-specifiers, and therefore realize that this is an
20109 expression, not a declaration.)
20111 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20112 the declarator is a direct-declarator of the form "(...)".
20114 MEMBER_P is true iff this declarator is a member-declarator.
20116 FRIEND_P is true iff this declarator is a friend. */
20118 static cp_declarator *
20119 cp_parser_declarator (cp_parser* parser,
20120 cp_parser_declarator_kind dcl_kind,
20121 int* ctor_dtor_or_conv_p,
20122 bool* parenthesized_p,
20123 bool member_p, bool friend_p)
20125 cp_declarator *declarator;
20126 enum tree_code code;
20127 cp_cv_quals cv_quals;
20128 tree class_type;
20129 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20131 /* Assume this is not a constructor, destructor, or type-conversion
20132 operator. */
20133 if (ctor_dtor_or_conv_p)
20134 *ctor_dtor_or_conv_p = 0;
20136 if (cp_parser_allow_gnu_extensions_p (parser))
20137 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20139 /* Check for the ptr-operator production. */
20140 cp_parser_parse_tentatively (parser);
20141 /* Parse the ptr-operator. */
20142 code = cp_parser_ptr_operator (parser,
20143 &class_type,
20144 &cv_quals,
20145 &std_attributes);
20147 /* If that worked, then we have a ptr-operator. */
20148 if (cp_parser_parse_definitely (parser))
20150 /* If a ptr-operator was found, then this declarator was not
20151 parenthesized. */
20152 if (parenthesized_p)
20153 *parenthesized_p = true;
20154 /* The dependent declarator is optional if we are parsing an
20155 abstract-declarator. */
20156 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20157 cp_parser_parse_tentatively (parser);
20159 /* Parse the dependent declarator. */
20160 declarator = cp_parser_declarator (parser, dcl_kind,
20161 /*ctor_dtor_or_conv_p=*/NULL,
20162 /*parenthesized_p=*/NULL,
20163 /*member_p=*/false,
20164 friend_p);
20166 /* If we are parsing an abstract-declarator, we must handle the
20167 case where the dependent declarator is absent. */
20168 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20169 && !cp_parser_parse_definitely (parser))
20170 declarator = NULL;
20172 declarator = cp_parser_make_indirect_declarator
20173 (code, class_type, cv_quals, declarator, std_attributes);
20175 /* Everything else is a direct-declarator. */
20176 else
20178 if (parenthesized_p)
20179 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20180 CPP_OPEN_PAREN);
20181 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20182 ctor_dtor_or_conv_p,
20183 member_p, friend_p);
20186 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20187 declarator->attributes = gnu_attributes;
20188 return declarator;
20191 /* Parse a direct-declarator or direct-abstract-declarator.
20193 direct-declarator:
20194 declarator-id
20195 direct-declarator ( parameter-declaration-clause )
20196 cv-qualifier-seq [opt]
20197 ref-qualifier [opt]
20198 exception-specification [opt]
20199 direct-declarator [ constant-expression [opt] ]
20200 ( declarator )
20202 direct-abstract-declarator:
20203 direct-abstract-declarator [opt]
20204 ( parameter-declaration-clause )
20205 cv-qualifier-seq [opt]
20206 ref-qualifier [opt]
20207 exception-specification [opt]
20208 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20209 ( abstract-declarator )
20211 Returns a representation of the declarator. DCL_KIND is
20212 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20213 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20214 we are parsing a direct-declarator. It is
20215 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20216 of ambiguity we prefer an abstract declarator, as per
20217 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20218 as for cp_parser_declarator. */
20220 static cp_declarator *
20221 cp_parser_direct_declarator (cp_parser* parser,
20222 cp_parser_declarator_kind dcl_kind,
20223 int* ctor_dtor_or_conv_p,
20224 bool member_p, bool friend_p)
20226 cp_token *token;
20227 cp_declarator *declarator = NULL;
20228 tree scope = NULL_TREE;
20229 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20230 bool saved_in_declarator_p = parser->in_declarator_p;
20231 bool first = true;
20232 tree pushed_scope = NULL_TREE;
20233 cp_token *open_paren = NULL, *close_paren = NULL;
20235 while (true)
20237 /* Peek at the next token. */
20238 token = cp_lexer_peek_token (parser->lexer);
20239 if (token->type == CPP_OPEN_PAREN)
20241 /* This is either a parameter-declaration-clause, or a
20242 parenthesized declarator. When we know we are parsing a
20243 named declarator, it must be a parenthesized declarator
20244 if FIRST is true. For instance, `(int)' is a
20245 parameter-declaration-clause, with an omitted
20246 direct-abstract-declarator. But `((*))', is a
20247 parenthesized abstract declarator. Finally, when T is a
20248 template parameter `(T)' is a
20249 parameter-declaration-clause, and not a parenthesized
20250 named declarator.
20252 We first try and parse a parameter-declaration-clause,
20253 and then try a nested declarator (if FIRST is true).
20255 It is not an error for it not to be a
20256 parameter-declaration-clause, even when FIRST is
20257 false. Consider,
20259 int i (int);
20260 int i (3);
20262 The first is the declaration of a function while the
20263 second is the definition of a variable, including its
20264 initializer.
20266 Having seen only the parenthesis, we cannot know which of
20267 these two alternatives should be selected. Even more
20268 complex are examples like:
20270 int i (int (a));
20271 int i (int (3));
20273 The former is a function-declaration; the latter is a
20274 variable initialization.
20276 Thus again, we try a parameter-declaration-clause, and if
20277 that fails, we back out and return. */
20279 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20281 tree params;
20282 bool is_declarator = false;
20284 open_paren = NULL;
20286 /* In a member-declarator, the only valid interpretation
20287 of a parenthesis is the start of a
20288 parameter-declaration-clause. (It is invalid to
20289 initialize a static data member with a parenthesized
20290 initializer; only the "=" form of initialization is
20291 permitted.) */
20292 if (!member_p)
20293 cp_parser_parse_tentatively (parser);
20295 /* Consume the `('. */
20296 matching_parens parens;
20297 parens.consume_open (parser);
20298 if (first)
20300 /* If this is going to be an abstract declarator, we're
20301 in a declarator and we can't have default args. */
20302 parser->default_arg_ok_p = false;
20303 parser->in_declarator_p = true;
20306 begin_scope (sk_function_parms, NULL_TREE);
20308 /* Parse the parameter-declaration-clause. */
20309 params = cp_parser_parameter_declaration_clause (parser);
20311 /* Consume the `)'. */
20312 parens.require_close (parser);
20314 /* If all went well, parse the cv-qualifier-seq,
20315 ref-qualifier and the exception-specification. */
20316 if (member_p || cp_parser_parse_definitely (parser))
20318 cp_cv_quals cv_quals;
20319 cp_virt_specifiers virt_specifiers;
20320 cp_ref_qualifier ref_qual;
20321 tree exception_specification;
20322 tree late_return;
20323 tree attrs;
20324 bool memfn = (member_p || (pushed_scope
20325 && CLASS_TYPE_P (pushed_scope)));
20327 is_declarator = true;
20329 if (ctor_dtor_or_conv_p)
20330 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20331 first = false;
20333 /* Parse the cv-qualifier-seq. */
20334 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20335 /* Parse the ref-qualifier. */
20336 ref_qual = cp_parser_ref_qualifier_opt (parser);
20337 /* Parse the tx-qualifier. */
20338 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20339 /* And the exception-specification. */
20340 exception_specification
20341 = cp_parser_exception_specification_opt (parser);
20343 attrs = cp_parser_std_attribute_spec_seq (parser);
20345 /* In here, we handle cases where attribute is used after
20346 the function declaration. For example:
20347 void func (int x) __attribute__((vector(..))); */
20348 tree gnu_attrs = NULL_TREE;
20349 tree requires_clause = NULL_TREE;
20350 late_return = (cp_parser_late_return_type_opt
20351 (parser, declarator, requires_clause,
20352 memfn ? cv_quals : -1));
20354 /* Parse the virt-specifier-seq. */
20355 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20357 /* Create the function-declarator. */
20358 declarator = make_call_declarator (declarator,
20359 params,
20360 cv_quals,
20361 virt_specifiers,
20362 ref_qual,
20363 tx_qual,
20364 exception_specification,
20365 late_return,
20366 requires_clause);
20367 declarator->std_attributes = attrs;
20368 declarator->attributes = gnu_attrs;
20369 /* Any subsequent parameter lists are to do with
20370 return type, so are not those of the declared
20371 function. */
20372 parser->default_arg_ok_p = false;
20375 /* Remove the function parms from scope. */
20376 pop_bindings_and_leave_scope ();
20378 if (is_declarator)
20379 /* Repeat the main loop. */
20380 continue;
20383 /* If this is the first, we can try a parenthesized
20384 declarator. */
20385 if (first)
20387 bool saved_in_type_id_in_expr_p;
20389 parser->default_arg_ok_p = saved_default_arg_ok_p;
20390 parser->in_declarator_p = saved_in_declarator_p;
20392 open_paren = token;
20393 /* Consume the `('. */
20394 matching_parens parens;
20395 parens.consume_open (parser);
20396 /* Parse the nested declarator. */
20397 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20398 parser->in_type_id_in_expr_p = true;
20399 declarator
20400 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20401 /*parenthesized_p=*/NULL,
20402 member_p, friend_p);
20403 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20404 first = false;
20405 /* Expect a `)'. */
20406 close_paren = cp_lexer_peek_token (parser->lexer);
20407 if (!parens.require_close (parser))
20408 declarator = cp_error_declarator;
20409 if (declarator == cp_error_declarator)
20410 break;
20412 goto handle_declarator;
20414 /* Otherwise, we must be done. */
20415 else
20416 break;
20418 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20419 && token->type == CPP_OPEN_SQUARE
20420 && !cp_next_tokens_can_be_attribute_p (parser))
20422 /* Parse an array-declarator. */
20423 tree bounds, attrs;
20425 if (ctor_dtor_or_conv_p)
20426 *ctor_dtor_or_conv_p = 0;
20428 open_paren = NULL;
20429 first = false;
20430 parser->default_arg_ok_p = false;
20431 parser->in_declarator_p = true;
20432 /* Consume the `['. */
20433 cp_lexer_consume_token (parser->lexer);
20434 /* Peek at the next token. */
20435 token = cp_lexer_peek_token (parser->lexer);
20436 /* If the next token is `]', then there is no
20437 constant-expression. */
20438 if (token->type != CPP_CLOSE_SQUARE)
20440 bool non_constant_p;
20441 bounds
20442 = cp_parser_constant_expression (parser,
20443 /*allow_non_constant=*/true,
20444 &non_constant_p);
20445 if (!non_constant_p)
20446 /* OK */;
20447 else if (error_operand_p (bounds))
20448 /* Already gave an error. */;
20449 else if (!parser->in_function_body
20450 || current_binding_level->kind == sk_function_parms)
20452 /* Normally, the array bound must be an integral constant
20453 expression. However, as an extension, we allow VLAs
20454 in function scopes as long as they aren't part of a
20455 parameter declaration. */
20456 cp_parser_error (parser,
20457 "array bound is not an integer constant");
20458 bounds = error_mark_node;
20460 else if (processing_template_decl
20461 && !type_dependent_expression_p (bounds))
20463 /* Remember this wasn't a constant-expression. */
20464 bounds = build_nop (TREE_TYPE (bounds), bounds);
20465 TREE_SIDE_EFFECTS (bounds) = 1;
20468 else
20469 bounds = NULL_TREE;
20470 /* Look for the closing `]'. */
20471 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20473 declarator = cp_error_declarator;
20474 break;
20477 attrs = cp_parser_std_attribute_spec_seq (parser);
20478 declarator = make_array_declarator (declarator, bounds);
20479 declarator->std_attributes = attrs;
20481 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20484 tree qualifying_scope;
20485 tree unqualified_name;
20486 tree attrs;
20487 special_function_kind sfk;
20488 bool abstract_ok;
20489 bool pack_expansion_p = false;
20490 cp_token *declarator_id_start_token;
20492 /* Parse a declarator-id */
20493 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20494 if (abstract_ok)
20496 cp_parser_parse_tentatively (parser);
20498 /* If we see an ellipsis, we should be looking at a
20499 parameter pack. */
20500 if (token->type == CPP_ELLIPSIS)
20502 /* Consume the `...' */
20503 cp_lexer_consume_token (parser->lexer);
20505 pack_expansion_p = true;
20509 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20510 unqualified_name
20511 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20512 qualifying_scope = parser->scope;
20513 if (abstract_ok)
20515 bool okay = false;
20517 if (!unqualified_name && pack_expansion_p)
20519 /* Check whether an error occurred. */
20520 okay = !cp_parser_error_occurred (parser);
20522 /* We already consumed the ellipsis to mark a
20523 parameter pack, but we have no way to report it,
20524 so abort the tentative parse. We will be exiting
20525 immediately anyway. */
20526 cp_parser_abort_tentative_parse (parser);
20528 else
20529 okay = cp_parser_parse_definitely (parser);
20531 if (!okay)
20532 unqualified_name = error_mark_node;
20533 else if (unqualified_name
20534 && (qualifying_scope
20535 || (!identifier_p (unqualified_name))))
20537 cp_parser_error (parser, "expected unqualified-id");
20538 unqualified_name = error_mark_node;
20542 if (!unqualified_name)
20543 return NULL;
20544 if (unqualified_name == error_mark_node)
20546 declarator = cp_error_declarator;
20547 pack_expansion_p = false;
20548 declarator->parameter_pack_p = false;
20549 break;
20552 attrs = cp_parser_std_attribute_spec_seq (parser);
20554 if (qualifying_scope && at_namespace_scope_p ()
20555 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20557 /* In the declaration of a member of a template class
20558 outside of the class itself, the SCOPE will sometimes
20559 be a TYPENAME_TYPE. For example, given:
20561 template <typename T>
20562 int S<T>::R::i = 3;
20564 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20565 this context, we must resolve S<T>::R to an ordinary
20566 type, rather than a typename type.
20568 The reason we normally avoid resolving TYPENAME_TYPEs
20569 is that a specialization of `S' might render
20570 `S<T>::R' not a type. However, if `S' is
20571 specialized, then this `i' will not be used, so there
20572 is no harm in resolving the types here. */
20573 tree type;
20575 /* Resolve the TYPENAME_TYPE. */
20576 type = resolve_typename_type (qualifying_scope,
20577 /*only_current_p=*/false);
20578 /* If that failed, the declarator is invalid. */
20579 if (TREE_CODE (type) == TYPENAME_TYPE)
20581 if (typedef_variant_p (type))
20582 error_at (declarator_id_start_token->location,
20583 "cannot define member of dependent typedef "
20584 "%qT", type);
20585 else
20586 error_at (declarator_id_start_token->location,
20587 "%<%T::%E%> is not a type",
20588 TYPE_CONTEXT (qualifying_scope),
20589 TYPE_IDENTIFIER (qualifying_scope));
20591 qualifying_scope = type;
20594 sfk = sfk_none;
20596 if (unqualified_name)
20598 tree class_type;
20600 if (qualifying_scope
20601 && CLASS_TYPE_P (qualifying_scope))
20602 class_type = qualifying_scope;
20603 else
20604 class_type = current_class_type;
20606 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20608 tree name_type = TREE_TYPE (unqualified_name);
20610 if (!class_type || !same_type_p (name_type, class_type))
20612 /* We do not attempt to print the declarator
20613 here because we do not have enough
20614 information about its original syntactic
20615 form. */
20616 cp_parser_error (parser, "invalid declarator");
20617 declarator = cp_error_declarator;
20618 break;
20620 else if (qualifying_scope
20621 && CLASSTYPE_USE_TEMPLATE (name_type))
20623 error_at (declarator_id_start_token->location,
20624 "invalid use of constructor as a template");
20625 inform (declarator_id_start_token->location,
20626 "use %<%T::%D%> instead of %<%T::%D%> to "
20627 "name the constructor in a qualified name",
20628 class_type,
20629 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20630 class_type, name_type);
20631 declarator = cp_error_declarator;
20632 break;
20634 unqualified_name = constructor_name (class_type);
20637 if (class_type)
20639 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20640 sfk = sfk_destructor;
20641 else if (identifier_p (unqualified_name)
20642 && IDENTIFIER_CONV_OP_P (unqualified_name))
20643 sfk = sfk_conversion;
20644 else if (/* There's no way to declare a constructor
20645 for an unnamed type, even if the type
20646 got a name for linkage purposes. */
20647 !TYPE_WAS_UNNAMED (class_type)
20648 /* Handle correctly (c++/19200):
20650 struct S {
20651 struct T{};
20652 friend void S(T);
20655 and also:
20657 namespace N {
20658 void S();
20661 struct S {
20662 friend void N::S();
20663 }; */
20664 && (!friend_p || class_type == qualifying_scope)
20665 && constructor_name_p (unqualified_name,
20666 class_type))
20667 sfk = sfk_constructor;
20668 else if (is_overloaded_fn (unqualified_name)
20669 && DECL_CONSTRUCTOR_P (get_first_fn
20670 (unqualified_name)))
20671 sfk = sfk_constructor;
20673 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20674 *ctor_dtor_or_conv_p = -1;
20677 declarator = make_id_declarator (qualifying_scope,
20678 unqualified_name,
20679 sfk);
20680 declarator->std_attributes = attrs;
20681 declarator->id_loc = token->location;
20682 declarator->parameter_pack_p = pack_expansion_p;
20684 if (pack_expansion_p)
20685 maybe_warn_variadic_templates ();
20688 handle_declarator:;
20689 scope = get_scope_of_declarator (declarator);
20690 if (scope)
20692 /* Any names that appear after the declarator-id for a
20693 member are looked up in the containing scope. */
20694 if (at_function_scope_p ())
20696 /* But declarations with qualified-ids can't appear in a
20697 function. */
20698 cp_parser_error (parser, "qualified-id in declaration");
20699 declarator = cp_error_declarator;
20700 break;
20702 pushed_scope = push_scope (scope);
20704 parser->in_declarator_p = true;
20705 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20706 || (declarator && declarator->kind == cdk_id))
20707 /* Default args are only allowed on function
20708 declarations. */
20709 parser->default_arg_ok_p = saved_default_arg_ok_p;
20710 else
20711 parser->default_arg_ok_p = false;
20713 first = false;
20715 /* We're done. */
20716 else
20717 break;
20720 /* For an abstract declarator, we might wind up with nothing at this
20721 point. That's an error; the declarator is not optional. */
20722 if (!declarator)
20723 cp_parser_error (parser, "expected declarator");
20724 else if (open_paren)
20726 /* Record overly parenthesized declarator so we can give a
20727 diagnostic about confusing decl/expr disambiguation. */
20728 if (declarator->kind == cdk_array)
20730 /* If the open and close parens are on different lines, this
20731 is probably a formatting thing, so ignore. */
20732 expanded_location open = expand_location (open_paren->location);
20733 expanded_location close = expand_location (close_paren->location);
20734 if (open.line != close.line || open.file != close.file)
20735 open_paren = NULL;
20737 if (open_paren)
20738 declarator->parenthesized = open_paren->location;
20741 /* If we entered a scope, we must exit it now. */
20742 if (pushed_scope)
20743 pop_scope (pushed_scope);
20745 parser->default_arg_ok_p = saved_default_arg_ok_p;
20746 parser->in_declarator_p = saved_in_declarator_p;
20748 return declarator;
20751 /* Parse a ptr-operator.
20753 ptr-operator:
20754 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20755 * cv-qualifier-seq [opt]
20757 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20758 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20760 GNU Extension:
20762 ptr-operator:
20763 & cv-qualifier-seq [opt]
20765 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20766 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20767 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20768 filled in with the TYPE containing the member. *CV_QUALS is
20769 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20770 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20771 Note that the tree codes returned by this function have nothing
20772 to do with the types of trees that will be eventually be created
20773 to represent the pointer or reference type being parsed. They are
20774 just constants with suggestive names. */
20775 static enum tree_code
20776 cp_parser_ptr_operator (cp_parser* parser,
20777 tree* type,
20778 cp_cv_quals *cv_quals,
20779 tree *attributes)
20781 enum tree_code code = ERROR_MARK;
20782 cp_token *token;
20783 tree attrs = NULL_TREE;
20785 /* Assume that it's not a pointer-to-member. */
20786 *type = NULL_TREE;
20787 /* And that there are no cv-qualifiers. */
20788 *cv_quals = TYPE_UNQUALIFIED;
20790 /* Peek at the next token. */
20791 token = cp_lexer_peek_token (parser->lexer);
20793 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20794 if (token->type == CPP_MULT)
20795 code = INDIRECT_REF;
20796 else if (token->type == CPP_AND)
20797 code = ADDR_EXPR;
20798 else if ((cxx_dialect != cxx98) &&
20799 token->type == CPP_AND_AND) /* C++0x only */
20800 code = NON_LVALUE_EXPR;
20802 if (code != ERROR_MARK)
20804 /* Consume the `*', `&' or `&&'. */
20805 cp_lexer_consume_token (parser->lexer);
20807 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20808 `&', if we are allowing GNU extensions. (The only qualifier
20809 that can legally appear after `&' is `restrict', but that is
20810 enforced during semantic analysis. */
20811 if (code == INDIRECT_REF
20812 || cp_parser_allow_gnu_extensions_p (parser))
20813 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20815 attrs = cp_parser_std_attribute_spec_seq (parser);
20816 if (attributes != NULL)
20817 *attributes = attrs;
20819 else
20821 /* Try the pointer-to-member case. */
20822 cp_parser_parse_tentatively (parser);
20823 /* Look for the optional `::' operator. */
20824 cp_parser_global_scope_opt (parser,
20825 /*current_scope_valid_p=*/false);
20826 /* Look for the nested-name specifier. */
20827 token = cp_lexer_peek_token (parser->lexer);
20828 cp_parser_nested_name_specifier (parser,
20829 /*typename_keyword_p=*/false,
20830 /*check_dependency_p=*/true,
20831 /*type_p=*/false,
20832 /*is_declaration=*/false);
20833 /* If we found it, and the next token is a `*', then we are
20834 indeed looking at a pointer-to-member operator. */
20835 if (!cp_parser_error_occurred (parser)
20836 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20838 /* Indicate that the `*' operator was used. */
20839 code = INDIRECT_REF;
20841 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20842 error_at (token->location, "%qD is a namespace", parser->scope);
20843 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20844 error_at (token->location, "cannot form pointer to member of "
20845 "non-class %q#T", parser->scope);
20846 else
20848 /* The type of which the member is a member is given by the
20849 current SCOPE. */
20850 *type = parser->scope;
20851 /* The next name will not be qualified. */
20852 parser->scope = NULL_TREE;
20853 parser->qualifying_scope = NULL_TREE;
20854 parser->object_scope = NULL_TREE;
20855 /* Look for optional c++11 attributes. */
20856 attrs = cp_parser_std_attribute_spec_seq (parser);
20857 if (attributes != NULL)
20858 *attributes = attrs;
20859 /* Look for the optional cv-qualifier-seq. */
20860 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20863 /* If that didn't work we don't have a ptr-operator. */
20864 if (!cp_parser_parse_definitely (parser))
20865 cp_parser_error (parser, "expected ptr-operator");
20868 return code;
20871 /* Parse an (optional) cv-qualifier-seq.
20873 cv-qualifier-seq:
20874 cv-qualifier cv-qualifier-seq [opt]
20876 cv-qualifier:
20877 const
20878 volatile
20880 GNU Extension:
20882 cv-qualifier:
20883 __restrict__
20885 Returns a bitmask representing the cv-qualifiers. */
20887 static cp_cv_quals
20888 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20890 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20892 while (true)
20894 cp_token *token;
20895 cp_cv_quals cv_qualifier;
20897 /* Peek at the next token. */
20898 token = cp_lexer_peek_token (parser->lexer);
20899 /* See if it's a cv-qualifier. */
20900 switch (token->keyword)
20902 case RID_CONST:
20903 cv_qualifier = TYPE_QUAL_CONST;
20904 break;
20906 case RID_VOLATILE:
20907 cv_qualifier = TYPE_QUAL_VOLATILE;
20908 break;
20910 case RID_RESTRICT:
20911 cv_qualifier = TYPE_QUAL_RESTRICT;
20912 break;
20914 default:
20915 cv_qualifier = TYPE_UNQUALIFIED;
20916 break;
20919 if (!cv_qualifier)
20920 break;
20922 if (cv_quals & cv_qualifier)
20924 gcc_rich_location richloc (token->location);
20925 richloc.add_fixit_remove ();
20926 error_at (&richloc, "duplicate cv-qualifier");
20927 cp_lexer_purge_token (parser->lexer);
20929 else
20931 cp_lexer_consume_token (parser->lexer);
20932 cv_quals |= cv_qualifier;
20936 return cv_quals;
20939 /* Parse an (optional) ref-qualifier
20941 ref-qualifier:
20945 Returns cp_ref_qualifier representing ref-qualifier. */
20947 static cp_ref_qualifier
20948 cp_parser_ref_qualifier_opt (cp_parser* parser)
20950 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20952 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20953 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20954 return ref_qual;
20956 while (true)
20958 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20959 cp_token *token = cp_lexer_peek_token (parser->lexer);
20961 switch (token->type)
20963 case CPP_AND:
20964 curr_ref_qual = REF_QUAL_LVALUE;
20965 break;
20967 case CPP_AND_AND:
20968 curr_ref_qual = REF_QUAL_RVALUE;
20969 break;
20971 default:
20972 curr_ref_qual = REF_QUAL_NONE;
20973 break;
20976 if (!curr_ref_qual)
20977 break;
20978 else if (ref_qual)
20980 error_at (token->location, "multiple ref-qualifiers");
20981 cp_lexer_purge_token (parser->lexer);
20983 else
20985 ref_qual = curr_ref_qual;
20986 cp_lexer_consume_token (parser->lexer);
20990 return ref_qual;
20993 /* Parse an optional tx-qualifier.
20995 tx-qualifier:
20996 transaction_safe
20997 transaction_safe_dynamic */
20999 static tree
21000 cp_parser_tx_qualifier_opt (cp_parser *parser)
21002 cp_token *token = cp_lexer_peek_token (parser->lexer);
21003 if (token->type == CPP_NAME)
21005 tree name = token->u.value;
21006 const char *p = IDENTIFIER_POINTER (name);
21007 const int len = strlen ("transaction_safe");
21008 if (!strncmp (p, "transaction_safe", len))
21010 p += len;
21011 if (*p == '\0'
21012 || !strcmp (p, "_dynamic"))
21014 cp_lexer_consume_token (parser->lexer);
21015 if (!flag_tm)
21017 error ("%qE requires %<-fgnu-tm%>", name);
21018 return NULL_TREE;
21020 else
21021 return name;
21025 return NULL_TREE;
21028 /* Parse an (optional) virt-specifier-seq.
21030 virt-specifier-seq:
21031 virt-specifier virt-specifier-seq [opt]
21033 virt-specifier:
21034 override
21035 final
21037 Returns a bitmask representing the virt-specifiers. */
21039 static cp_virt_specifiers
21040 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21042 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21044 while (true)
21046 cp_token *token;
21047 cp_virt_specifiers virt_specifier;
21049 /* Peek at the next token. */
21050 token = cp_lexer_peek_token (parser->lexer);
21051 /* See if it's a virt-specifier-qualifier. */
21052 if (token->type != CPP_NAME)
21053 break;
21054 if (id_equal (token->u.value, "override"))
21056 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21057 virt_specifier = VIRT_SPEC_OVERRIDE;
21059 else if (id_equal (token->u.value, "final"))
21061 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21062 virt_specifier = VIRT_SPEC_FINAL;
21064 else if (id_equal (token->u.value, "__final"))
21066 virt_specifier = VIRT_SPEC_FINAL;
21068 else
21069 break;
21071 if (virt_specifiers & virt_specifier)
21073 gcc_rich_location richloc (token->location);
21074 richloc.add_fixit_remove ();
21075 error_at (&richloc, "duplicate virt-specifier");
21076 cp_lexer_purge_token (parser->lexer);
21078 else
21080 cp_lexer_consume_token (parser->lexer);
21081 virt_specifiers |= virt_specifier;
21084 return virt_specifiers;
21087 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21088 is in scope even though it isn't real. */
21090 void
21091 inject_this_parameter (tree ctype, cp_cv_quals quals)
21093 tree this_parm;
21095 if (current_class_ptr)
21097 /* We don't clear this between NSDMIs. Is it already what we want? */
21098 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21099 if (DECL_P (current_class_ptr)
21100 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21101 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21102 && cp_type_quals (type) == quals)
21103 return;
21106 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21107 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21108 current_class_ptr = NULL_TREE;
21109 current_class_ref
21110 = cp_build_fold_indirect_ref (this_parm);
21111 current_class_ptr = this_parm;
21114 /* Return true iff our current scope is a non-static data member
21115 initializer. */
21117 bool
21118 parsing_nsdmi (void)
21120 /* We recognize NSDMI context by the context-less 'this' pointer set up
21121 by the function above. */
21122 if (current_class_ptr
21123 && TREE_CODE (current_class_ptr) == PARM_DECL
21124 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21125 return true;
21126 return false;
21129 /* Parse a late-specified return type, if any. This is not a separate
21130 non-terminal, but part of a function declarator, which looks like
21132 -> trailing-type-specifier-seq abstract-declarator(opt)
21134 Returns the type indicated by the type-id.
21136 In addition to this, parse any queued up #pragma omp declare simd
21137 clauses, and #pragma acc routine clauses.
21139 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21140 function. */
21142 static tree
21143 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21144 tree& requires_clause, cp_cv_quals quals)
21146 cp_token *token;
21147 tree type = NULL_TREE;
21148 bool declare_simd_p = (parser->omp_declare_simd
21149 && declarator
21150 && declarator->kind == cdk_id);
21152 bool oacc_routine_p = (parser->oacc_routine
21153 && declarator
21154 && declarator->kind == cdk_id);
21156 /* Peek at the next token. */
21157 token = cp_lexer_peek_token (parser->lexer);
21158 /* A late-specified return type is indicated by an initial '->'. */
21159 if (token->type != CPP_DEREF
21160 && token->keyword != RID_REQUIRES
21161 && !(token->type == CPP_NAME
21162 && token->u.value == ridpointers[RID_REQUIRES])
21163 && !(declare_simd_p || oacc_routine_p))
21164 return NULL_TREE;
21166 tree save_ccp = current_class_ptr;
21167 tree save_ccr = current_class_ref;
21168 if (quals >= 0)
21170 /* DR 1207: 'this' is in scope in the trailing return type. */
21171 inject_this_parameter (current_class_type, quals);
21174 if (token->type == CPP_DEREF)
21176 /* Consume the ->. */
21177 cp_lexer_consume_token (parser->lexer);
21179 type = cp_parser_trailing_type_id (parser);
21182 /* Function declarations may be followed by a trailing
21183 requires-clause. */
21184 requires_clause = cp_parser_requires_clause_opt (parser);
21186 if (declare_simd_p)
21187 declarator->attributes
21188 = cp_parser_late_parsing_omp_declare_simd (parser,
21189 declarator->attributes);
21190 if (oacc_routine_p)
21191 declarator->attributes
21192 = cp_parser_late_parsing_oacc_routine (parser,
21193 declarator->attributes);
21195 if (quals >= 0)
21197 current_class_ptr = save_ccp;
21198 current_class_ref = save_ccr;
21201 return type;
21204 /* Parse a declarator-id.
21206 declarator-id:
21207 id-expression
21208 :: [opt] nested-name-specifier [opt] type-name
21210 In the `id-expression' case, the value returned is as for
21211 cp_parser_id_expression if the id-expression was an unqualified-id.
21212 If the id-expression was a qualified-id, then a SCOPE_REF is
21213 returned. The first operand is the scope (either a NAMESPACE_DECL
21214 or TREE_TYPE), but the second is still just a representation of an
21215 unqualified-id. */
21217 static tree
21218 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21220 tree id;
21221 /* The expression must be an id-expression. Assume that qualified
21222 names are the names of types so that:
21224 template <class T>
21225 int S<T>::R::i = 3;
21227 will work; we must treat `S<T>::R' as the name of a type.
21228 Similarly, assume that qualified names are templates, where
21229 required, so that:
21231 template <class T>
21232 int S<T>::R<T>::i = 3;
21234 will work, too. */
21235 id = cp_parser_id_expression (parser,
21236 /*template_keyword_p=*/false,
21237 /*check_dependency_p=*/false,
21238 /*template_p=*/NULL,
21239 /*declarator_p=*/true,
21240 optional_p);
21241 if (id && BASELINK_P (id))
21242 id = BASELINK_FUNCTIONS (id);
21243 return id;
21246 /* Parse a type-id.
21248 type-id:
21249 type-specifier-seq abstract-declarator [opt]
21251 Returns the TYPE specified. */
21253 static tree
21254 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21255 bool is_trailing_return, location_t * type_location)
21257 cp_decl_specifier_seq type_specifier_seq;
21258 cp_declarator *abstract_declarator;
21260 /* Parse the type-specifier-seq. */
21261 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21262 is_trailing_return,
21263 &type_specifier_seq);
21264 if (type_location)
21265 *type_location = type_specifier_seq.locations[ds_type_spec];
21267 if (is_template_arg && type_specifier_seq.type
21268 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21269 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21270 /* A bare template name as a template argument is a template template
21271 argument, not a placeholder, so fail parsing it as a type argument. */
21273 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21274 cp_parser_simulate_error (parser);
21275 return error_mark_node;
21277 if (type_specifier_seq.type == error_mark_node)
21278 return error_mark_node;
21280 /* There might or might not be an abstract declarator. */
21281 cp_parser_parse_tentatively (parser);
21282 /* Look for the declarator. */
21283 abstract_declarator
21284 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21285 /*parenthesized_p=*/NULL,
21286 /*member_p=*/false,
21287 /*friend_p=*/false);
21288 /* Check to see if there really was a declarator. */
21289 if (!cp_parser_parse_definitely (parser))
21290 abstract_declarator = NULL;
21292 if (type_specifier_seq.type
21293 /* The concepts TS allows 'auto' as a type-id. */
21294 && (!flag_concepts || parser->in_type_id_in_expr_p)
21295 /* None of the valid uses of 'auto' in C++14 involve the type-id
21296 nonterminal, but it is valid in a trailing-return-type. */
21297 && !(cxx_dialect >= cxx14 && is_trailing_return))
21298 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21300 /* A type-id with type 'auto' is only ok if the abstract declarator
21301 is a function declarator with a late-specified return type.
21303 A type-id with 'auto' is also valid in a trailing-return-type
21304 in a compound-requirement. */
21305 if (abstract_declarator
21306 && abstract_declarator->kind == cdk_function
21307 && abstract_declarator->u.function.late_return_type)
21308 /* OK */;
21309 else if (parser->in_result_type_constraint_p)
21310 /* OK */;
21311 else
21313 location_t loc = type_specifier_seq.locations[ds_type_spec];
21314 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21316 error_at (loc, "missing template arguments after %qT",
21317 auto_node);
21318 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21319 tmpl);
21321 else
21322 error_at (loc, "invalid use of %qT", auto_node);
21323 return error_mark_node;
21327 return groktypename (&type_specifier_seq, abstract_declarator,
21328 is_template_arg);
21331 static tree
21332 cp_parser_type_id (cp_parser *parser, location_t * type_location)
21334 return cp_parser_type_id_1 (parser, false, false, type_location);
21337 static tree
21338 cp_parser_template_type_arg (cp_parser *parser)
21340 tree r;
21341 const char *saved_message = parser->type_definition_forbidden_message;
21342 parser->type_definition_forbidden_message
21343 = G_("types may not be defined in template arguments");
21344 r = cp_parser_type_id_1 (parser, true, false, NULL);
21345 parser->type_definition_forbidden_message = saved_message;
21346 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21348 error ("invalid use of %<auto%> in template argument");
21349 r = error_mark_node;
21351 return r;
21354 static tree
21355 cp_parser_trailing_type_id (cp_parser *parser)
21357 return cp_parser_type_id_1 (parser, false, true, NULL);
21360 /* Parse a type-specifier-seq.
21362 type-specifier-seq:
21363 type-specifier type-specifier-seq [opt]
21365 GNU extension:
21367 type-specifier-seq:
21368 attributes type-specifier-seq [opt]
21370 If IS_DECLARATION is true, we are at the start of a "condition" or
21371 exception-declaration, so we might be followed by a declarator-id.
21373 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21374 i.e. we've just seen "->".
21376 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21378 static void
21379 cp_parser_type_specifier_seq (cp_parser* parser,
21380 bool is_declaration,
21381 bool is_trailing_return,
21382 cp_decl_specifier_seq *type_specifier_seq)
21384 bool seen_type_specifier = false;
21385 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21386 cp_token *start_token = NULL;
21388 /* Clear the TYPE_SPECIFIER_SEQ. */
21389 clear_decl_specs (type_specifier_seq);
21391 /* In the context of a trailing return type, enum E { } is an
21392 elaborated-type-specifier followed by a function-body, not an
21393 enum-specifier. */
21394 if (is_trailing_return)
21395 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21397 /* Parse the type-specifiers and attributes. */
21398 while (true)
21400 tree type_specifier;
21401 bool is_cv_qualifier;
21403 /* Check for attributes first. */
21404 if (cp_next_tokens_can_be_attribute_p (parser))
21406 type_specifier_seq->attributes
21407 = attr_chainon (type_specifier_seq->attributes,
21408 cp_parser_attributes_opt (parser));
21409 continue;
21412 /* record the token of the beginning of the type specifier seq,
21413 for error reporting purposes*/
21414 if (!start_token)
21415 start_token = cp_lexer_peek_token (parser->lexer);
21417 /* Look for the type-specifier. */
21418 type_specifier = cp_parser_type_specifier (parser,
21419 flags,
21420 type_specifier_seq,
21421 /*is_declaration=*/false,
21422 NULL,
21423 &is_cv_qualifier);
21424 if (!type_specifier)
21426 /* If the first type-specifier could not be found, this is not a
21427 type-specifier-seq at all. */
21428 if (!seen_type_specifier)
21430 /* Set in_declarator_p to avoid skipping to the semicolon. */
21431 int in_decl = parser->in_declarator_p;
21432 parser->in_declarator_p = true;
21434 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21435 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21436 cp_parser_error (parser, "expected type-specifier");
21438 parser->in_declarator_p = in_decl;
21440 type_specifier_seq->type = error_mark_node;
21441 return;
21443 /* If subsequent type-specifiers could not be found, the
21444 type-specifier-seq is complete. */
21445 break;
21448 seen_type_specifier = true;
21449 /* The standard says that a condition can be:
21451 type-specifier-seq declarator = assignment-expression
21453 However, given:
21455 struct S {};
21456 if (int S = ...)
21458 we should treat the "S" as a declarator, not as a
21459 type-specifier. The standard doesn't say that explicitly for
21460 type-specifier-seq, but it does say that for
21461 decl-specifier-seq in an ordinary declaration. Perhaps it
21462 would be clearer just to allow a decl-specifier-seq here, and
21463 then add a semantic restriction that if any decl-specifiers
21464 that are not type-specifiers appear, the program is invalid. */
21465 if (is_declaration && !is_cv_qualifier)
21466 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21470 /* Return whether the function currently being declared has an associated
21471 template parameter list. */
21473 static bool
21474 function_being_declared_is_template_p (cp_parser* parser)
21476 if (!current_template_parms || processing_template_parmlist)
21477 return false;
21479 if (parser->implicit_template_scope)
21480 return true;
21482 if (at_class_scope_p ()
21483 && TYPE_BEING_DEFINED (current_class_type))
21484 return parser->num_template_parameter_lists != 0;
21486 return ((int) parser->num_template_parameter_lists > template_class_depth
21487 (current_class_type));
21490 /* Parse a parameter-declaration-clause.
21492 parameter-declaration-clause:
21493 parameter-declaration-list [opt] ... [opt]
21494 parameter-declaration-list , ...
21496 Returns a representation for the parameter declarations. A return
21497 value of NULL indicates a parameter-declaration-clause consisting
21498 only of an ellipsis. */
21500 static tree
21501 cp_parser_parameter_declaration_clause (cp_parser* parser)
21503 tree parameters;
21504 cp_token *token;
21505 bool ellipsis_p;
21507 temp_override<bool> cleanup
21508 (parser->auto_is_implicit_function_template_parm_p);
21510 if (!processing_specialization
21511 && !processing_template_parmlist
21512 && !processing_explicit_instantiation
21513 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21514 actual function or a random abstract declarator. */
21515 && parser->default_arg_ok_p)
21516 if (!current_function_decl
21517 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21518 parser->auto_is_implicit_function_template_parm_p = true;
21520 /* Peek at the next token. */
21521 token = cp_lexer_peek_token (parser->lexer);
21522 /* Check for trivial parameter-declaration-clauses. */
21523 if (token->type == CPP_ELLIPSIS)
21525 /* Consume the `...' token. */
21526 cp_lexer_consume_token (parser->lexer);
21527 return NULL_TREE;
21529 else if (token->type == CPP_CLOSE_PAREN)
21530 /* There are no parameters. */
21531 return void_list_node;
21532 /* Check for `(void)', too, which is a special case. */
21533 else if (token->keyword == RID_VOID
21534 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21535 == CPP_CLOSE_PAREN))
21537 /* Consume the `void' token. */
21538 cp_lexer_consume_token (parser->lexer);
21539 /* There are no parameters. */
21540 return void_list_node;
21543 /* Parse the parameter-declaration-list. */
21544 parameters = cp_parser_parameter_declaration_list (parser);
21545 /* If a parse error occurred while parsing the
21546 parameter-declaration-list, then the entire
21547 parameter-declaration-clause is erroneous. */
21548 if (parameters == error_mark_node)
21549 return NULL_TREE;
21551 /* Peek at the next token. */
21552 token = cp_lexer_peek_token (parser->lexer);
21553 /* If it's a `,', the clause should terminate with an ellipsis. */
21554 if (token->type == CPP_COMMA)
21556 /* Consume the `,'. */
21557 cp_lexer_consume_token (parser->lexer);
21558 /* Expect an ellipsis. */
21559 ellipsis_p
21560 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21562 /* It might also be `...' if the optional trailing `,' was
21563 omitted. */
21564 else if (token->type == CPP_ELLIPSIS)
21566 /* Consume the `...' token. */
21567 cp_lexer_consume_token (parser->lexer);
21568 /* And remember that we saw it. */
21569 ellipsis_p = true;
21571 else
21572 ellipsis_p = false;
21574 /* Finish the parameter list. */
21575 if (!ellipsis_p)
21576 parameters = chainon (parameters, void_list_node);
21578 return parameters;
21581 /* Parse a parameter-declaration-list.
21583 parameter-declaration-list:
21584 parameter-declaration
21585 parameter-declaration-list , parameter-declaration
21587 Returns a representation of the parameter-declaration-list, as for
21588 cp_parser_parameter_declaration_clause. However, the
21589 `void_list_node' is never appended to the list. */
21591 static tree
21592 cp_parser_parameter_declaration_list (cp_parser* parser)
21594 tree parameters = NULL_TREE;
21595 tree *tail = &parameters;
21596 bool saved_in_unbraced_linkage_specification_p;
21597 int index = 0;
21599 /* The special considerations that apply to a function within an
21600 unbraced linkage specifications do not apply to the parameters
21601 to the function. */
21602 saved_in_unbraced_linkage_specification_p
21603 = parser->in_unbraced_linkage_specification_p;
21604 parser->in_unbraced_linkage_specification_p = false;
21606 /* Look for more parameters. */
21607 while (true)
21609 cp_parameter_declarator *parameter;
21610 tree decl = error_mark_node;
21611 bool parenthesized_p = false;
21613 /* Parse the parameter. */
21614 parameter
21615 = cp_parser_parameter_declaration (parser,
21616 /*template_parm_p=*/false,
21617 &parenthesized_p);
21619 /* We don't know yet if the enclosing context is deprecated, so wait
21620 and warn in grokparms if appropriate. */
21621 deprecated_state = DEPRECATED_SUPPRESS;
21623 if (parameter)
21625 decl = grokdeclarator (parameter->declarator,
21626 &parameter->decl_specifiers,
21627 PARM,
21628 parameter->default_argument != NULL_TREE,
21629 &parameter->decl_specifiers.attributes);
21630 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21631 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21634 deprecated_state = DEPRECATED_NORMAL;
21636 /* If a parse error occurred parsing the parameter declaration,
21637 then the entire parameter-declaration-list is erroneous. */
21638 if (decl == error_mark_node)
21640 parameters = error_mark_node;
21641 break;
21644 if (parameter->decl_specifiers.attributes)
21645 cplus_decl_attributes (&decl,
21646 parameter->decl_specifiers.attributes,
21648 if (DECL_NAME (decl))
21649 decl = pushdecl (decl);
21651 if (decl != error_mark_node)
21653 retrofit_lang_decl (decl);
21654 DECL_PARM_INDEX (decl) = ++index;
21655 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21658 /* Add the new parameter to the list. */
21659 *tail = build_tree_list (parameter->default_argument, decl);
21660 tail = &TREE_CHAIN (*tail);
21662 /* Peek at the next token. */
21663 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21664 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21665 /* These are for Objective-C++ */
21666 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21667 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21668 /* The parameter-declaration-list is complete. */
21669 break;
21670 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21672 cp_token *token;
21674 /* Peek at the next token. */
21675 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21676 /* If it's an ellipsis, then the list is complete. */
21677 if (token->type == CPP_ELLIPSIS)
21678 break;
21679 /* Otherwise, there must be more parameters. Consume the
21680 `,'. */
21681 cp_lexer_consume_token (parser->lexer);
21682 /* When parsing something like:
21684 int i(float f, double d)
21686 we can tell after seeing the declaration for "f" that we
21687 are not looking at an initialization of a variable "i",
21688 but rather at the declaration of a function "i".
21690 Due to the fact that the parsing of template arguments
21691 (as specified to a template-id) requires backtracking we
21692 cannot use this technique when inside a template argument
21693 list. */
21694 if (!parser->in_template_argument_list_p
21695 && !parser->in_type_id_in_expr_p
21696 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21697 /* However, a parameter-declaration of the form
21698 "float(f)" (which is a valid declaration of a
21699 parameter "f") can also be interpreted as an
21700 expression (the conversion of "f" to "float"). */
21701 && !parenthesized_p)
21702 cp_parser_commit_to_tentative_parse (parser);
21704 else
21706 cp_parser_error (parser, "expected %<,%> or %<...%>");
21707 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21708 cp_parser_skip_to_closing_parenthesis (parser,
21709 /*recovering=*/true,
21710 /*or_comma=*/false,
21711 /*consume_paren=*/false);
21712 break;
21716 parser->in_unbraced_linkage_specification_p
21717 = saved_in_unbraced_linkage_specification_p;
21719 /* Reset implicit_template_scope if we are about to leave the function
21720 parameter list that introduced it. Note that for out-of-line member
21721 definitions, there will be one or more class scopes before we get to
21722 the template parameter scope. */
21724 if (cp_binding_level *its = parser->implicit_template_scope)
21725 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21727 while (maybe_its->kind == sk_class)
21728 maybe_its = maybe_its->level_chain;
21729 if (maybe_its == its)
21731 parser->implicit_template_parms = 0;
21732 parser->implicit_template_scope = 0;
21736 return parameters;
21739 /* Parse a parameter declaration.
21741 parameter-declaration:
21742 decl-specifier-seq ... [opt] declarator
21743 decl-specifier-seq declarator = assignment-expression
21744 decl-specifier-seq ... [opt] abstract-declarator [opt]
21745 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21747 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21748 declares a template parameter. (In that case, a non-nested `>'
21749 token encountered during the parsing of the assignment-expression
21750 is not interpreted as a greater-than operator.)
21752 Returns a representation of the parameter, or NULL if an error
21753 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21754 true iff the declarator is of the form "(p)". */
21756 static cp_parameter_declarator *
21757 cp_parser_parameter_declaration (cp_parser *parser,
21758 bool template_parm_p,
21759 bool *parenthesized_p)
21761 int declares_class_or_enum;
21762 cp_decl_specifier_seq decl_specifiers;
21763 cp_declarator *declarator;
21764 tree default_argument;
21765 cp_token *token = NULL, *declarator_token_start = NULL;
21766 const char *saved_message;
21767 bool template_parameter_pack_p = false;
21769 /* In a template parameter, `>' is not an operator.
21771 [temp.param]
21773 When parsing a default template-argument for a non-type
21774 template-parameter, the first non-nested `>' is taken as the end
21775 of the template parameter-list rather than a greater-than
21776 operator. */
21778 /* Type definitions may not appear in parameter types. */
21779 saved_message = parser->type_definition_forbidden_message;
21780 parser->type_definition_forbidden_message
21781 = G_("types may not be defined in parameter types");
21783 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21784 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21785 (current_template_parms)) : 0);
21787 /* Parse the declaration-specifiers. */
21788 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21789 cp_parser_decl_specifier_seq (parser,
21790 CP_PARSER_FLAGS_NONE,
21791 &decl_specifiers,
21792 &declares_class_or_enum);
21794 /* Complain about missing 'typename' or other invalid type names. */
21795 if (!decl_specifiers.any_type_specifiers_p
21796 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21797 decl_specifiers.type = error_mark_node;
21799 /* If an error occurred, there's no reason to attempt to parse the
21800 rest of the declaration. */
21801 if (cp_parser_error_occurred (parser))
21803 parser->type_definition_forbidden_message = saved_message;
21804 return NULL;
21807 /* Peek at the next token. */
21808 token = cp_lexer_peek_token (parser->lexer);
21810 /* If the next token is a `)', `,', `=', `>', or `...', then there
21811 is no declarator. However, when variadic templates are enabled,
21812 there may be a declarator following `...'. */
21813 if (token->type == CPP_CLOSE_PAREN
21814 || token->type == CPP_COMMA
21815 || token->type == CPP_EQ
21816 || token->type == CPP_GREATER)
21818 declarator = NULL;
21819 if (parenthesized_p)
21820 *parenthesized_p = false;
21822 /* Otherwise, there should be a declarator. */
21823 else
21825 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21826 parser->default_arg_ok_p = false;
21828 /* After seeing a decl-specifier-seq, if the next token is not a
21829 "(", there is no possibility that the code is a valid
21830 expression. Therefore, if parsing tentatively, we commit at
21831 this point. */
21832 if (!parser->in_template_argument_list_p
21833 /* In an expression context, having seen:
21835 (int((char ...
21837 we cannot be sure whether we are looking at a
21838 function-type (taking a "char" as a parameter) or a cast
21839 of some object of type "char" to "int". */
21840 && !parser->in_type_id_in_expr_p
21841 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21842 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21843 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21844 cp_parser_commit_to_tentative_parse (parser);
21845 /* Parse the declarator. */
21846 declarator_token_start = token;
21847 declarator = cp_parser_declarator (parser,
21848 CP_PARSER_DECLARATOR_EITHER,
21849 /*ctor_dtor_or_conv_p=*/NULL,
21850 parenthesized_p,
21851 /*member_p=*/false,
21852 /*friend_p=*/false);
21853 parser->default_arg_ok_p = saved_default_arg_ok_p;
21854 /* After the declarator, allow more attributes. */
21855 decl_specifiers.attributes
21856 = attr_chainon (decl_specifiers.attributes,
21857 cp_parser_attributes_opt (parser));
21859 /* If the declarator is a template parameter pack, remember that and
21860 clear the flag in the declarator itself so we don't get errors
21861 from grokdeclarator. */
21862 if (template_parm_p && declarator && declarator->parameter_pack_p)
21864 declarator->parameter_pack_p = false;
21865 template_parameter_pack_p = true;
21869 /* If the next token is an ellipsis, and we have not seen a declarator
21870 name, and if either the type of the declarator contains parameter
21871 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21872 for, eg, abbreviated integral type names), then we actually have a
21873 parameter pack expansion expression. Otherwise, leave the ellipsis
21874 for a C-style variadic function. */
21875 token = cp_lexer_peek_token (parser->lexer);
21877 /* If a function parameter pack was specified and an implicit template
21878 parameter was introduced during cp_parser_parameter_declaration,
21879 change any implicit parameters introduced into packs. */
21880 if (parser->implicit_template_parms
21881 && ((token->type == CPP_ELLIPSIS
21882 && declarator_can_be_parameter_pack (declarator))
21883 || (declarator && declarator->parameter_pack_p)))
21885 int latest_template_parm_idx = TREE_VEC_LENGTH
21886 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21888 if (latest_template_parm_idx != template_parm_idx)
21889 decl_specifiers.type = convert_generic_types_to_packs
21890 (decl_specifiers.type,
21891 template_parm_idx, latest_template_parm_idx);
21894 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21896 tree type = decl_specifiers.type;
21898 if (type && DECL_P (type))
21899 type = TREE_TYPE (type);
21901 if (((type
21902 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21903 && (template_parm_p || uses_parameter_packs (type)))
21904 || (!type && template_parm_p))
21905 && declarator_can_be_parameter_pack (declarator))
21907 /* Consume the `...'. */
21908 cp_lexer_consume_token (parser->lexer);
21909 maybe_warn_variadic_templates ();
21911 /* Build a pack expansion type */
21912 if (template_parm_p)
21913 template_parameter_pack_p = true;
21914 else if (declarator)
21915 declarator->parameter_pack_p = true;
21916 else
21917 decl_specifiers.type = make_pack_expansion (type);
21921 /* The restriction on defining new types applies only to the type
21922 of the parameter, not to the default argument. */
21923 parser->type_definition_forbidden_message = saved_message;
21925 /* If the next token is `=', then process a default argument. */
21926 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21928 tree type = decl_specifiers.type;
21929 token = cp_lexer_peek_token (parser->lexer);
21930 /* If we are defining a class, then the tokens that make up the
21931 default argument must be saved and processed later. */
21932 if (!template_parm_p && at_class_scope_p ()
21933 && TYPE_BEING_DEFINED (current_class_type)
21934 && !LAMBDA_TYPE_P (current_class_type))
21935 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21937 // A constrained-type-specifier may declare a type template-parameter.
21938 else if (declares_constrained_type_template_parameter (type))
21939 default_argument
21940 = cp_parser_default_type_template_argument (parser);
21942 // A constrained-type-specifier may declare a template-template-parameter.
21943 else if (declares_constrained_template_template_parameter (type))
21944 default_argument
21945 = cp_parser_default_template_template_argument (parser);
21947 /* Outside of a class definition, we can just parse the
21948 assignment-expression. */
21949 else
21950 default_argument
21951 = cp_parser_default_argument (parser, template_parm_p);
21953 if (!parser->default_arg_ok_p)
21955 permerror (token->location,
21956 "default arguments are only "
21957 "permitted for function parameters");
21959 else if ((declarator && declarator->parameter_pack_p)
21960 || template_parameter_pack_p
21961 || (decl_specifiers.type
21962 && PACK_EXPANSION_P (decl_specifiers.type)))
21964 /* Find the name of the parameter pack. */
21965 cp_declarator *id_declarator = declarator;
21966 while (id_declarator && id_declarator->kind != cdk_id)
21967 id_declarator = id_declarator->declarator;
21969 if (id_declarator && id_declarator->kind == cdk_id)
21970 error_at (declarator_token_start->location,
21971 template_parm_p
21972 ? G_("template parameter pack %qD "
21973 "cannot have a default argument")
21974 : G_("parameter pack %qD cannot have "
21975 "a default argument"),
21976 id_declarator->u.id.unqualified_name);
21977 else
21978 error_at (declarator_token_start->location,
21979 template_parm_p
21980 ? G_("template parameter pack cannot have "
21981 "a default argument")
21982 : G_("parameter pack cannot have a "
21983 "default argument"));
21985 default_argument = NULL_TREE;
21988 else
21989 default_argument = NULL_TREE;
21991 /* Generate a location for the parameter, ranging from the start of the
21992 initial token to the end of the final token (using input_location for
21993 the latter, set up by cp_lexer_set_source_position_from_token when
21994 consuming tokens).
21996 If we have a identifier, then use it for the caret location, e.g.
21998 extern int callee (int one, int (*two)(int, int), float three);
21999 ~~~~~~^~~~~~~~~~~~~~
22001 otherwise, reuse the start location for the caret location e.g.:
22003 extern int callee (int one, int (*)(int, int), float three);
22004 ^~~~~~~~~~~~~~~~~
22007 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22008 ? declarator->id_loc
22009 : decl_spec_token_start->location);
22010 location_t param_loc = make_location (caret_loc,
22011 decl_spec_token_start->location,
22012 input_location);
22014 return make_parameter_declarator (&decl_specifiers,
22015 declarator,
22016 default_argument,
22017 param_loc,
22018 template_parameter_pack_p);
22021 /* Parse a default argument and return it.
22023 TEMPLATE_PARM_P is true if this is a default argument for a
22024 non-type template parameter. */
22025 static tree
22026 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22028 tree default_argument = NULL_TREE;
22029 bool saved_greater_than_is_operator_p;
22030 bool saved_local_variables_forbidden_p;
22031 bool non_constant_p, is_direct_init;
22033 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22034 set correctly. */
22035 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22036 parser->greater_than_is_operator_p = !template_parm_p;
22037 /* Local variable names (and the `this' keyword) may not
22038 appear in a default argument. */
22039 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22040 parser->local_variables_forbidden_p = true;
22041 /* Parse the assignment-expression. */
22042 if (template_parm_p)
22043 push_deferring_access_checks (dk_no_deferred);
22044 tree saved_class_ptr = NULL_TREE;
22045 tree saved_class_ref = NULL_TREE;
22046 /* The "this" pointer is not valid in a default argument. */
22047 if (cfun)
22049 saved_class_ptr = current_class_ptr;
22050 cp_function_chain->x_current_class_ptr = NULL_TREE;
22051 saved_class_ref = current_class_ref;
22052 cp_function_chain->x_current_class_ref = NULL_TREE;
22054 default_argument
22055 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22056 /* Restore the "this" pointer. */
22057 if (cfun)
22059 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22060 cp_function_chain->x_current_class_ref = saved_class_ref;
22062 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22063 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22064 if (template_parm_p)
22065 pop_deferring_access_checks ();
22066 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22067 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22069 return default_argument;
22072 /* Parse a function-body.
22074 function-body:
22075 compound_statement */
22077 static void
22078 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22080 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22081 ? BCS_TRY_BLOCK : BCS_NORMAL),
22082 true);
22085 /* Parse a ctor-initializer-opt followed by a function-body. Return
22086 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22087 is true we are parsing a function-try-block. */
22089 static void
22090 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22091 bool in_function_try_block)
22093 tree body, list;
22094 const bool check_body_p =
22095 DECL_CONSTRUCTOR_P (current_function_decl)
22096 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
22097 tree last = NULL;
22099 /* Begin the function body. */
22100 body = begin_function_body ();
22101 /* Parse the optional ctor-initializer. */
22102 cp_parser_ctor_initializer_opt (parser);
22104 /* If we're parsing a constexpr constructor definition, we need
22105 to check that the constructor body is indeed empty. However,
22106 before we get to cp_parser_function_body lot of junk has been
22107 generated, so we can't just check that we have an empty block.
22108 Rather we take a snapshot of the outermost block, and check whether
22109 cp_parser_function_body changed its state. */
22110 if (check_body_p)
22112 list = cur_stmt_list;
22113 if (STATEMENT_LIST_TAIL (list))
22114 last = STATEMENT_LIST_TAIL (list)->stmt;
22116 /* Parse the function-body. */
22117 cp_parser_function_body (parser, in_function_try_block);
22118 if (check_body_p)
22119 check_constexpr_ctor_body (last, list, /*complain=*/true);
22120 /* Finish the function body. */
22121 finish_function_body (body);
22124 /* Parse an initializer.
22126 initializer:
22127 = initializer-clause
22128 ( expression-list )
22130 Returns an expression representing the initializer. If no
22131 initializer is present, NULL_TREE is returned.
22133 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22134 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22135 set to TRUE if there is no initializer present. If there is an
22136 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22137 is set to true; otherwise it is set to false. */
22139 static tree
22140 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22141 bool* non_constant_p, bool subexpression_p)
22143 cp_token *token;
22144 tree init;
22146 /* Peek at the next token. */
22147 token = cp_lexer_peek_token (parser->lexer);
22149 /* Let our caller know whether or not this initializer was
22150 parenthesized. */
22151 *is_direct_init = (token->type != CPP_EQ);
22152 /* Assume that the initializer is constant. */
22153 *non_constant_p = false;
22155 if (token->type == CPP_EQ)
22157 /* Consume the `='. */
22158 cp_lexer_consume_token (parser->lexer);
22159 /* Parse the initializer-clause. */
22160 init = cp_parser_initializer_clause (parser, non_constant_p);
22162 else if (token->type == CPP_OPEN_PAREN)
22164 vec<tree, va_gc> *vec;
22165 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22166 /*cast_p=*/false,
22167 /*allow_expansion_p=*/true,
22168 non_constant_p);
22169 if (vec == NULL)
22170 return error_mark_node;
22171 init = build_tree_list_vec (vec);
22172 release_tree_vector (vec);
22174 else if (token->type == CPP_OPEN_BRACE)
22176 cp_lexer_set_source_position (parser->lexer);
22177 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22178 init = cp_parser_braced_list (parser, non_constant_p);
22179 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22181 else
22183 /* Anything else is an error. */
22184 cp_parser_error (parser, "expected initializer");
22185 init = error_mark_node;
22188 if (!subexpression_p && check_for_bare_parameter_packs (init))
22189 init = error_mark_node;
22191 return init;
22194 /* Parse an initializer-clause.
22196 initializer-clause:
22197 assignment-expression
22198 braced-init-list
22200 Returns an expression representing the initializer.
22202 If the `assignment-expression' production is used the value
22203 returned is simply a representation for the expression.
22205 Otherwise, calls cp_parser_braced_list. */
22207 static cp_expr
22208 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22210 cp_expr initializer;
22212 /* Assume the expression is constant. */
22213 *non_constant_p = false;
22215 /* If it is not a `{', then we are looking at an
22216 assignment-expression. */
22217 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22219 initializer
22220 = cp_parser_constant_expression (parser,
22221 /*allow_non_constant_p=*/true,
22222 non_constant_p);
22224 else
22225 initializer = cp_parser_braced_list (parser, non_constant_p);
22227 return initializer;
22230 /* Parse a brace-enclosed initializer list.
22232 braced-init-list:
22233 { initializer-list , [opt] }
22234 { designated-initializer-list , [opt] }
22237 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22238 the elements of the initializer-list (or NULL, if the last
22239 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22240 NULL_TREE. There is no way to detect whether or not the optional
22241 trailing `,' was provided. NON_CONSTANT_P is as for
22242 cp_parser_initializer. */
22244 static cp_expr
22245 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22247 tree initializer;
22248 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22250 /* Consume the `{' token. */
22251 matching_braces braces;
22252 braces.require_open (parser);
22253 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22254 initializer = make_node (CONSTRUCTOR);
22255 /* If it's not a `}', then there is a non-trivial initializer. */
22256 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22258 /* Parse the initializer list. */
22259 CONSTRUCTOR_ELTS (initializer)
22260 = cp_parser_initializer_list (parser, non_constant_p);
22261 /* A trailing `,' token is allowed. */
22262 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22263 cp_lexer_consume_token (parser->lexer);
22265 else
22266 *non_constant_p = false;
22267 /* Now, there should be a trailing `}'. */
22268 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22269 braces.require_close (parser);
22270 TREE_TYPE (initializer) = init_list_type_node;
22272 cp_expr result (initializer);
22273 /* Build a location of the form:
22274 { ... }
22275 ^~~~~~~
22276 with caret==start at the open brace, finish at the close brace. */
22277 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22278 result.set_location (combined_loc);
22279 return result;
22282 /* Consume tokens up to, and including, the next non-nested closing `]'.
22283 Returns true iff we found a closing `]'. */
22285 static bool
22286 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22288 unsigned square_depth = 0;
22290 while (true)
22292 cp_token * token = cp_lexer_peek_token (parser->lexer);
22294 switch (token->type)
22296 case CPP_EOF:
22297 case CPP_PRAGMA_EOL:
22298 /* If we've run out of tokens, then there is no closing `]'. */
22299 return false;
22301 case CPP_OPEN_SQUARE:
22302 ++square_depth;
22303 break;
22305 case CPP_CLOSE_SQUARE:
22306 if (!square_depth--)
22308 cp_lexer_consume_token (parser->lexer);
22309 return true;
22311 break;
22313 default:
22314 break;
22317 /* Consume the token. */
22318 cp_lexer_consume_token (parser->lexer);
22322 /* Return true if we are looking at an array-designator, false otherwise. */
22324 static bool
22325 cp_parser_array_designator_p (cp_parser *parser)
22327 /* Consume the `['. */
22328 cp_lexer_consume_token (parser->lexer);
22330 cp_lexer_save_tokens (parser->lexer);
22332 /* Skip tokens until the next token is a closing square bracket.
22333 If we find the closing `]', and the next token is a `=', then
22334 we are looking at an array designator. */
22335 bool array_designator_p
22336 = (cp_parser_skip_to_closing_square_bracket (parser)
22337 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22339 /* Roll back the tokens we skipped. */
22340 cp_lexer_rollback_tokens (parser->lexer);
22342 return array_designator_p;
22345 /* Parse an initializer-list.
22347 initializer-list:
22348 initializer-clause ... [opt]
22349 initializer-list , initializer-clause ... [opt]
22351 C++2A Extension:
22353 designated-initializer-list:
22354 designated-initializer-clause
22355 designated-initializer-list , designated-initializer-clause
22357 designated-initializer-clause:
22358 designator brace-or-equal-initializer
22360 designator:
22361 . identifier
22363 GNU Extension:
22365 initializer-list:
22366 designation initializer-clause ...[opt]
22367 initializer-list , designation initializer-clause ...[opt]
22369 designation:
22370 . identifier =
22371 identifier :
22372 [ constant-expression ] =
22374 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22375 for the initializer. If the INDEX of the elt is non-NULL, it is the
22376 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22377 as for cp_parser_initializer. */
22379 static vec<constructor_elt, va_gc> *
22380 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22382 vec<constructor_elt, va_gc> *v = NULL;
22383 bool first_p = true;
22384 tree first_designator = NULL_TREE;
22386 /* Assume all of the expressions are constant. */
22387 *non_constant_p = false;
22389 /* Parse the rest of the list. */
22390 while (true)
22392 cp_token *token;
22393 tree designator;
22394 tree initializer;
22395 bool clause_non_constant_p;
22396 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22398 /* Handle the C++2A syntax, '. id ='. */
22399 if ((cxx_dialect >= cxx2a
22400 || cp_parser_allow_gnu_extensions_p (parser))
22401 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22402 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22403 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22404 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22405 == CPP_OPEN_BRACE)))
22407 if (cxx_dialect < cxx2a)
22408 pedwarn (loc, OPT_Wpedantic,
22409 "C++ designated initializers only available with "
22410 "-std=c++2a or -std=gnu++2a");
22411 /* Consume the `.'. */
22412 cp_lexer_consume_token (parser->lexer);
22413 /* Consume the identifier. */
22414 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22415 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22416 /* Consume the `='. */
22417 cp_lexer_consume_token (parser->lexer);
22419 /* Also, if the next token is an identifier and the following one is a
22420 colon, we are looking at the GNU designated-initializer
22421 syntax. */
22422 else if (cp_parser_allow_gnu_extensions_p (parser)
22423 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22424 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22425 == CPP_COLON))
22427 /* Warn the user that they are using an extension. */
22428 pedwarn (loc, OPT_Wpedantic,
22429 "ISO C++ does not allow GNU designated initializers");
22430 /* Consume the identifier. */
22431 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22432 /* Consume the `:'. */
22433 cp_lexer_consume_token (parser->lexer);
22435 /* Also handle C99 array designators, '[ const ] ='. */
22436 else if (cp_parser_allow_gnu_extensions_p (parser)
22437 && !c_dialect_objc ()
22438 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22440 /* In C++11, [ could start a lambda-introducer. */
22441 bool non_const = false;
22443 cp_parser_parse_tentatively (parser);
22445 if (!cp_parser_array_designator_p (parser))
22447 cp_parser_simulate_error (parser);
22448 designator = NULL_TREE;
22450 else
22452 designator = cp_parser_constant_expression (parser, true,
22453 &non_const);
22454 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22455 cp_parser_require (parser, CPP_EQ, RT_EQ);
22458 if (!cp_parser_parse_definitely (parser))
22459 designator = NULL_TREE;
22460 else if (non_const
22461 && (!require_potential_rvalue_constant_expression
22462 (designator)))
22463 designator = NULL_TREE;
22464 if (designator)
22465 /* Warn the user that they are using an extension. */
22466 pedwarn (loc, OPT_Wpedantic,
22467 "ISO C++ does not allow C99 designated initializers");
22469 else
22470 designator = NULL_TREE;
22472 if (first_p)
22474 first_designator = designator;
22475 first_p = false;
22477 else if (cxx_dialect >= cxx2a
22478 && first_designator != error_mark_node
22479 && (!first_designator != !designator))
22481 error_at (loc, "either all initializer clauses should be designated "
22482 "or none of them should be");
22483 first_designator = error_mark_node;
22485 else if (cxx_dialect < cxx2a && !first_designator)
22486 first_designator = designator;
22488 /* Parse the initializer. */
22489 initializer = cp_parser_initializer_clause (parser,
22490 &clause_non_constant_p);
22491 /* If any clause is non-constant, so is the entire initializer. */
22492 if (clause_non_constant_p)
22493 *non_constant_p = true;
22495 /* If we have an ellipsis, this is an initializer pack
22496 expansion. */
22497 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22499 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22501 /* Consume the `...'. */
22502 cp_lexer_consume_token (parser->lexer);
22504 if (designator && cxx_dialect >= cxx2a)
22505 error_at (loc,
22506 "%<...%> not allowed in designated initializer list");
22508 /* Turn the initializer into an initializer expansion. */
22509 initializer = make_pack_expansion (initializer);
22512 /* Add it to the vector. */
22513 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22515 /* If the next token is not a comma, we have reached the end of
22516 the list. */
22517 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22518 break;
22520 /* Peek at the next token. */
22521 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22522 /* If the next token is a `}', then we're still done. An
22523 initializer-clause can have a trailing `,' after the
22524 initializer-list and before the closing `}'. */
22525 if (token->type == CPP_CLOSE_BRACE)
22526 break;
22528 /* Consume the `,' token. */
22529 cp_lexer_consume_token (parser->lexer);
22532 /* The same identifier shall not appear in multiple designators
22533 of a designated-initializer-list. */
22534 if (first_designator)
22536 unsigned int i;
22537 tree designator, val;
22538 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22539 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22541 if (IDENTIFIER_MARKED (designator))
22543 error_at (cp_expr_loc_or_loc (val, input_location),
22544 "%<.%s%> designator used multiple times in "
22545 "the same initializer list",
22546 IDENTIFIER_POINTER (designator));
22547 (*v)[i].index = error_mark_node;
22549 else
22550 IDENTIFIER_MARKED (designator) = 1;
22552 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22553 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22554 IDENTIFIER_MARKED (designator) = 0;
22557 return v;
22560 /* Classes [gram.class] */
22562 /* Parse a class-name.
22564 class-name:
22565 identifier
22566 template-id
22568 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22569 to indicate that names looked up in dependent types should be
22570 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22571 keyword has been used to indicate that the name that appears next
22572 is a template. TAG_TYPE indicates the explicit tag given before
22573 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22574 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22575 is the class being defined in a class-head. If ENUM_OK is TRUE,
22576 enum-names are also accepted.
22578 Returns the TYPE_DECL representing the class. */
22580 static tree
22581 cp_parser_class_name (cp_parser *parser,
22582 bool typename_keyword_p,
22583 bool template_keyword_p,
22584 enum tag_types tag_type,
22585 bool check_dependency_p,
22586 bool class_head_p,
22587 bool is_declaration,
22588 bool enum_ok)
22590 tree decl;
22591 tree scope;
22592 bool typename_p;
22593 cp_token *token;
22594 tree identifier = NULL_TREE;
22596 /* All class-names start with an identifier. */
22597 token = cp_lexer_peek_token (parser->lexer);
22598 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22600 cp_parser_error (parser, "expected class-name");
22601 return error_mark_node;
22604 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22605 to a template-id, so we save it here. */
22606 scope = parser->scope;
22607 if (scope == error_mark_node)
22608 return error_mark_node;
22610 /* Any name names a type if we're following the `typename' keyword
22611 in a qualified name where the enclosing scope is type-dependent. */
22612 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22613 && dependent_type_p (scope));
22614 /* Handle the common case (an identifier, but not a template-id)
22615 efficiently. */
22616 if (token->type == CPP_NAME
22617 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22619 cp_token *identifier_token;
22620 bool ambiguous_p;
22622 /* Look for the identifier. */
22623 identifier_token = cp_lexer_peek_token (parser->lexer);
22624 ambiguous_p = identifier_token->error_reported;
22625 identifier = cp_parser_identifier (parser);
22626 /* If the next token isn't an identifier, we are certainly not
22627 looking at a class-name. */
22628 if (identifier == error_mark_node)
22629 decl = error_mark_node;
22630 /* If we know this is a type-name, there's no need to look it
22631 up. */
22632 else if (typename_p)
22633 decl = identifier;
22634 else
22636 tree ambiguous_decls;
22637 /* If we already know that this lookup is ambiguous, then
22638 we've already issued an error message; there's no reason
22639 to check again. */
22640 if (ambiguous_p)
22642 cp_parser_simulate_error (parser);
22643 return error_mark_node;
22645 /* If the next token is a `::', then the name must be a type
22646 name.
22648 [basic.lookup.qual]
22650 During the lookup for a name preceding the :: scope
22651 resolution operator, object, function, and enumerator
22652 names are ignored. */
22653 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22654 tag_type = scope_type;
22655 /* Look up the name. */
22656 decl = cp_parser_lookup_name (parser, identifier,
22657 tag_type,
22658 /*is_template=*/false,
22659 /*is_namespace=*/false,
22660 check_dependency_p,
22661 &ambiguous_decls,
22662 identifier_token->location);
22663 if (ambiguous_decls)
22665 if (cp_parser_parsing_tentatively (parser))
22666 cp_parser_simulate_error (parser);
22667 return error_mark_node;
22671 else
22673 /* Try a template-id. */
22674 decl = cp_parser_template_id (parser, template_keyword_p,
22675 check_dependency_p,
22676 tag_type,
22677 is_declaration);
22678 if (decl == error_mark_node)
22679 return error_mark_node;
22682 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22684 /* If this is a typename, create a TYPENAME_TYPE. */
22685 if (typename_p && decl != error_mark_node)
22687 decl = make_typename_type (scope, decl, typename_type,
22688 /*complain=*/tf_error);
22689 if (decl != error_mark_node)
22690 decl = TYPE_NAME (decl);
22693 decl = strip_using_decl (decl);
22695 /* Check to see that it is really the name of a class. */
22696 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22697 && identifier_p (TREE_OPERAND (decl, 0))
22698 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22699 /* Situations like this:
22701 template <typename T> struct A {
22702 typename T::template X<int>::I i;
22705 are problematic. Is `T::template X<int>' a class-name? The
22706 standard does not seem to be definitive, but there is no other
22707 valid interpretation of the following `::'. Therefore, those
22708 names are considered class-names. */
22710 decl = make_typename_type (scope, decl, tag_type, tf_error);
22711 if (decl != error_mark_node)
22712 decl = TYPE_NAME (decl);
22714 else if (TREE_CODE (decl) != TYPE_DECL
22715 || TREE_TYPE (decl) == error_mark_node
22716 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22717 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22718 /* In Objective-C 2.0, a classname followed by '.' starts a
22719 dot-syntax expression, and it's not a type-name. */
22720 || (c_dialect_objc ()
22721 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22722 && objc_is_class_name (decl)))
22723 decl = error_mark_node;
22725 if (decl == error_mark_node)
22726 cp_parser_error (parser, "expected class-name");
22727 else if (identifier && !parser->scope)
22728 maybe_note_name_used_in_class (identifier, decl);
22730 return decl;
22733 /* Parse a class-specifier.
22735 class-specifier:
22736 class-head { member-specification [opt] }
22738 Returns the TREE_TYPE representing the class. */
22740 static tree
22741 cp_parser_class_specifier_1 (cp_parser* parser)
22743 tree type;
22744 tree attributes = NULL_TREE;
22745 bool nested_name_specifier_p;
22746 unsigned saved_num_template_parameter_lists;
22747 bool saved_in_function_body;
22748 unsigned char in_statement;
22749 bool in_switch_statement_p;
22750 bool saved_in_unbraced_linkage_specification_p;
22751 tree old_scope = NULL_TREE;
22752 tree scope = NULL_TREE;
22753 cp_token *closing_brace;
22755 push_deferring_access_checks (dk_no_deferred);
22757 /* Parse the class-head. */
22758 type = cp_parser_class_head (parser,
22759 &nested_name_specifier_p);
22760 /* If the class-head was a semantic disaster, skip the entire body
22761 of the class. */
22762 if (!type)
22764 cp_parser_skip_to_end_of_block_or_statement (parser);
22765 pop_deferring_access_checks ();
22766 return error_mark_node;
22769 /* Look for the `{'. */
22770 matching_braces braces;
22771 if (!braces.require_open (parser))
22773 pop_deferring_access_checks ();
22774 return error_mark_node;
22777 cp_ensure_no_omp_declare_simd (parser);
22778 cp_ensure_no_oacc_routine (parser);
22780 /* Issue an error message if type-definitions are forbidden here. */
22781 cp_parser_check_type_definition (parser);
22782 /* Remember that we are defining one more class. */
22783 ++parser->num_classes_being_defined;
22784 /* Inside the class, surrounding template-parameter-lists do not
22785 apply. */
22786 saved_num_template_parameter_lists
22787 = parser->num_template_parameter_lists;
22788 parser->num_template_parameter_lists = 0;
22789 /* We are not in a function body. */
22790 saved_in_function_body = parser->in_function_body;
22791 parser->in_function_body = false;
22792 /* Or in a loop. */
22793 in_statement = parser->in_statement;
22794 parser->in_statement = 0;
22795 /* Or in a switch. */
22796 in_switch_statement_p = parser->in_switch_statement_p;
22797 parser->in_switch_statement_p = false;
22798 /* We are not immediately inside an extern "lang" block. */
22799 saved_in_unbraced_linkage_specification_p
22800 = parser->in_unbraced_linkage_specification_p;
22801 parser->in_unbraced_linkage_specification_p = false;
22803 // Associate constraints with the type.
22804 if (flag_concepts)
22805 type = associate_classtype_constraints (type);
22807 /* Start the class. */
22808 if (nested_name_specifier_p)
22810 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22811 old_scope = push_inner_scope (scope);
22813 type = begin_class_definition (type);
22815 if (type == error_mark_node)
22816 /* If the type is erroneous, skip the entire body of the class. */
22817 cp_parser_skip_to_closing_brace (parser);
22818 else
22819 /* Parse the member-specification. */
22820 cp_parser_member_specification_opt (parser);
22822 /* Look for the trailing `}'. */
22823 closing_brace = braces.require_close (parser);
22824 /* Look for trailing attributes to apply to this class. */
22825 if (cp_parser_allow_gnu_extensions_p (parser))
22826 attributes = cp_parser_gnu_attributes_opt (parser);
22827 if (type != error_mark_node)
22828 type = finish_struct (type, attributes);
22829 if (nested_name_specifier_p)
22830 pop_inner_scope (old_scope, scope);
22832 /* We've finished a type definition. Check for the common syntax
22833 error of forgetting a semicolon after the definition. We need to
22834 be careful, as we can't just check for not-a-semicolon and be done
22835 with it; the user might have typed:
22837 class X { } c = ...;
22838 class X { } *p = ...;
22840 and so forth. Instead, enumerate all the possible tokens that
22841 might follow this production; if we don't see one of them, then
22842 complain and silently insert the semicolon. */
22844 cp_token *token = cp_lexer_peek_token (parser->lexer);
22845 bool want_semicolon = true;
22847 if (cp_next_tokens_can_be_std_attribute_p (parser))
22848 /* Don't try to parse c++11 attributes here. As per the
22849 grammar, that should be a task for
22850 cp_parser_decl_specifier_seq. */
22851 want_semicolon = false;
22853 switch (token->type)
22855 case CPP_NAME:
22856 case CPP_SEMICOLON:
22857 case CPP_MULT:
22858 case CPP_AND:
22859 case CPP_OPEN_PAREN:
22860 case CPP_CLOSE_PAREN:
22861 case CPP_COMMA:
22862 want_semicolon = false;
22863 break;
22865 /* While it's legal for type qualifiers and storage class
22866 specifiers to follow type definitions in the grammar, only
22867 compiler testsuites contain code like that. Assume that if
22868 we see such code, then what we're really seeing is a case
22869 like:
22871 class X { }
22872 const <type> var = ...;
22876 class Y { }
22877 static <type> func (...) ...
22879 i.e. the qualifier or specifier applies to the next
22880 declaration. To do so, however, we need to look ahead one
22881 more token to see if *that* token is a type specifier.
22883 This code could be improved to handle:
22885 class Z { }
22886 static const <type> var = ...; */
22887 case CPP_KEYWORD:
22888 if (keyword_is_decl_specifier (token->keyword))
22890 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22892 /* Handling user-defined types here would be nice, but very
22893 tricky. */
22894 want_semicolon
22895 = (lookahead->type == CPP_KEYWORD
22896 && keyword_begins_type_specifier (lookahead->keyword));
22898 break;
22899 default:
22900 break;
22903 /* If we don't have a type, then something is very wrong and we
22904 shouldn't try to do anything clever. Likewise for not seeing the
22905 closing brace. */
22906 if (closing_brace && TYPE_P (type) && want_semicolon)
22908 /* Locate the closing brace. */
22909 cp_token_position prev
22910 = cp_lexer_previous_token_position (parser->lexer);
22911 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22912 location_t loc = prev_token->location;
22914 /* We want to suggest insertion of a ';' immediately *after* the
22915 closing brace, so, if we can, offset the location by 1 column. */
22916 location_t next_loc = loc;
22917 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22918 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22920 rich_location richloc (line_table, next_loc);
22922 /* If we successfully offset the location, suggest the fix-it. */
22923 if (next_loc != loc)
22924 richloc.add_fixit_insert_before (next_loc, ";");
22926 if (CLASSTYPE_DECLARED_CLASS (type))
22927 error_at (&richloc,
22928 "expected %<;%> after class definition");
22929 else if (TREE_CODE (type) == RECORD_TYPE)
22930 error_at (&richloc,
22931 "expected %<;%> after struct definition");
22932 else if (TREE_CODE (type) == UNION_TYPE)
22933 error_at (&richloc,
22934 "expected %<;%> after union definition");
22935 else
22936 gcc_unreachable ();
22938 /* Unget one token and smash it to look as though we encountered
22939 a semicolon in the input stream. */
22940 cp_lexer_set_token_position (parser->lexer, prev);
22941 token = cp_lexer_peek_token (parser->lexer);
22942 token->type = CPP_SEMICOLON;
22943 token->keyword = RID_MAX;
22947 /* If this class is not itself within the scope of another class,
22948 then we need to parse the bodies of all of the queued function
22949 definitions. Note that the queued functions defined in a class
22950 are not always processed immediately following the
22951 class-specifier for that class. Consider:
22953 struct A {
22954 struct B { void f() { sizeof (A); } };
22957 If `f' were processed before the processing of `A' were
22958 completed, there would be no way to compute the size of `A'.
22959 Note that the nesting we are interested in here is lexical --
22960 not the semantic nesting given by TYPE_CONTEXT. In particular,
22961 for:
22963 struct A { struct B; };
22964 struct A::B { void f() { } };
22966 there is no need to delay the parsing of `A::B::f'. */
22967 if (--parser->num_classes_being_defined == 0)
22969 tree decl;
22970 tree class_type = NULL_TREE;
22971 tree pushed_scope = NULL_TREE;
22972 unsigned ix;
22973 cp_default_arg_entry *e;
22974 tree save_ccp, save_ccr;
22976 if (any_erroneous_template_args_p (type))
22978 /* Skip default arguments, NSDMIs, etc, in order to improve
22979 error recovery (c++/71169, c++/71832). */
22980 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22981 vec_safe_truncate (unparsed_nsdmis, 0);
22982 vec_safe_truncate (unparsed_classes, 0);
22983 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22986 /* In a first pass, parse default arguments to the functions.
22987 Then, in a second pass, parse the bodies of the functions.
22988 This two-phased approach handles cases like:
22990 struct S {
22991 void f() { g(); }
22992 void g(int i = 3);
22996 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22998 decl = e->decl;
22999 /* If there are default arguments that have not yet been processed,
23000 take care of them now. */
23001 if (class_type != e->class_type)
23003 if (pushed_scope)
23004 pop_scope (pushed_scope);
23005 class_type = e->class_type;
23006 pushed_scope = push_scope (class_type);
23008 /* Make sure that any template parameters are in scope. */
23009 maybe_begin_member_template_processing (decl);
23010 /* Parse the default argument expressions. */
23011 cp_parser_late_parsing_default_args (parser, decl);
23012 /* Remove any template parameters from the symbol table. */
23013 maybe_end_member_template_processing ();
23015 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23016 /* Now parse any NSDMIs. */
23017 save_ccp = current_class_ptr;
23018 save_ccr = current_class_ref;
23019 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23021 if (class_type != DECL_CONTEXT (decl))
23023 if (pushed_scope)
23024 pop_scope (pushed_scope);
23025 class_type = DECL_CONTEXT (decl);
23026 pushed_scope = push_scope (class_type);
23028 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23029 cp_parser_late_parsing_nsdmi (parser, decl);
23031 vec_safe_truncate (unparsed_nsdmis, 0);
23032 current_class_ptr = save_ccp;
23033 current_class_ref = save_ccr;
23034 if (pushed_scope)
23035 pop_scope (pushed_scope);
23037 /* Now do some post-NSDMI bookkeeping. */
23038 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23039 after_nsdmi_defaulted_late_checks (class_type);
23040 vec_safe_truncate (unparsed_classes, 0);
23041 after_nsdmi_defaulted_late_checks (type);
23043 /* Now parse the body of the functions. */
23044 if (flag_openmp)
23046 /* OpenMP UDRs need to be parsed before all other functions. */
23047 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23048 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23049 cp_parser_late_parsing_for_member (parser, decl);
23050 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23051 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23052 cp_parser_late_parsing_for_member (parser, decl);
23054 else
23055 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23056 cp_parser_late_parsing_for_member (parser, decl);
23057 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23059 else
23060 vec_safe_push (unparsed_classes, type);
23062 /* Put back any saved access checks. */
23063 pop_deferring_access_checks ();
23065 /* Restore saved state. */
23066 parser->in_switch_statement_p = in_switch_statement_p;
23067 parser->in_statement = in_statement;
23068 parser->in_function_body = saved_in_function_body;
23069 parser->num_template_parameter_lists
23070 = saved_num_template_parameter_lists;
23071 parser->in_unbraced_linkage_specification_p
23072 = saved_in_unbraced_linkage_specification_p;
23074 return type;
23077 static tree
23078 cp_parser_class_specifier (cp_parser* parser)
23080 tree ret;
23081 timevar_push (TV_PARSE_STRUCT);
23082 ret = cp_parser_class_specifier_1 (parser);
23083 timevar_pop (TV_PARSE_STRUCT);
23084 return ret;
23087 /* Parse a class-head.
23089 class-head:
23090 class-key identifier [opt] base-clause [opt]
23091 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23092 class-key nested-name-specifier [opt] template-id
23093 base-clause [opt]
23095 class-virt-specifier:
23096 final
23098 GNU Extensions:
23099 class-key attributes identifier [opt] base-clause [opt]
23100 class-key attributes nested-name-specifier identifier base-clause [opt]
23101 class-key attributes nested-name-specifier [opt] template-id
23102 base-clause [opt]
23104 Upon return BASES is initialized to the list of base classes (or
23105 NULL, if there are none) in the same form returned by
23106 cp_parser_base_clause.
23108 Returns the TYPE of the indicated class. Sets
23109 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23110 involving a nested-name-specifier was used, and FALSE otherwise.
23112 Returns error_mark_node if this is not a class-head.
23114 Returns NULL_TREE if the class-head is syntactically valid, but
23115 semantically invalid in a way that means we should skip the entire
23116 body of the class. */
23118 static tree
23119 cp_parser_class_head (cp_parser* parser,
23120 bool* nested_name_specifier_p)
23122 tree nested_name_specifier;
23123 enum tag_types class_key;
23124 tree id = NULL_TREE;
23125 tree type = NULL_TREE;
23126 tree attributes;
23127 tree bases;
23128 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23129 bool template_id_p = false;
23130 bool qualified_p = false;
23131 bool invalid_nested_name_p = false;
23132 bool invalid_explicit_specialization_p = false;
23133 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23134 tree pushed_scope = NULL_TREE;
23135 unsigned num_templates;
23136 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23137 /* Assume no nested-name-specifier will be present. */
23138 *nested_name_specifier_p = false;
23139 /* Assume no template parameter lists will be used in defining the
23140 type. */
23141 num_templates = 0;
23142 parser->colon_corrects_to_scope_p = false;
23144 /* Look for the class-key. */
23145 class_key = cp_parser_class_key (parser);
23146 if (class_key == none_type)
23147 return error_mark_node;
23149 location_t class_head_start_location = input_location;
23151 /* Parse the attributes. */
23152 attributes = cp_parser_attributes_opt (parser);
23154 /* If the next token is `::', that is invalid -- but sometimes
23155 people do try to write:
23157 struct ::S {};
23159 Handle this gracefully by accepting the extra qualifier, and then
23160 issuing an error about it later if this really is a
23161 class-head. If it turns out just to be an elaborated type
23162 specifier, remain silent. */
23163 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23164 qualified_p = true;
23166 push_deferring_access_checks (dk_no_check);
23168 /* Determine the name of the class. Begin by looking for an
23169 optional nested-name-specifier. */
23170 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23171 nested_name_specifier
23172 = cp_parser_nested_name_specifier_opt (parser,
23173 /*typename_keyword_p=*/false,
23174 /*check_dependency_p=*/false,
23175 /*type_p=*/true,
23176 /*is_declaration=*/false);
23177 /* If there was a nested-name-specifier, then there *must* be an
23178 identifier. */
23180 cp_token *bad_template_keyword = NULL;
23182 if (nested_name_specifier)
23184 type_start_token = cp_lexer_peek_token (parser->lexer);
23185 /* Although the grammar says `identifier', it really means
23186 `class-name' or `template-name'. You are only allowed to
23187 define a class that has already been declared with this
23188 syntax.
23190 The proposed resolution for Core Issue 180 says that wherever
23191 you see `class T::X' you should treat `X' as a type-name.
23193 It is OK to define an inaccessible class; for example:
23195 class A { class B; };
23196 class A::B {};
23198 We do not know if we will see a class-name, or a
23199 template-name. We look for a class-name first, in case the
23200 class-name is a template-id; if we looked for the
23201 template-name first we would stop after the template-name. */
23202 cp_parser_parse_tentatively (parser);
23203 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23204 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23205 type = cp_parser_class_name (parser,
23206 /*typename_keyword_p=*/false,
23207 /*template_keyword_p=*/false,
23208 class_type,
23209 /*check_dependency_p=*/false,
23210 /*class_head_p=*/true,
23211 /*is_declaration=*/false);
23212 /* If that didn't work, ignore the nested-name-specifier. */
23213 if (!cp_parser_parse_definitely (parser))
23215 invalid_nested_name_p = true;
23216 type_start_token = cp_lexer_peek_token (parser->lexer);
23217 id = cp_parser_identifier (parser);
23218 if (id == error_mark_node)
23219 id = NULL_TREE;
23221 /* If we could not find a corresponding TYPE, treat this
23222 declaration like an unqualified declaration. */
23223 if (type == error_mark_node)
23224 nested_name_specifier = NULL_TREE;
23225 /* Otherwise, count the number of templates used in TYPE and its
23226 containing scopes. */
23227 else
23228 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23230 /* Otherwise, the identifier is optional. */
23231 else
23233 /* We don't know whether what comes next is a template-id,
23234 an identifier, or nothing at all. */
23235 cp_parser_parse_tentatively (parser);
23236 /* Check for a template-id. */
23237 type_start_token = cp_lexer_peek_token (parser->lexer);
23238 id = cp_parser_template_id (parser,
23239 /*template_keyword_p=*/false,
23240 /*check_dependency_p=*/true,
23241 class_key,
23242 /*is_declaration=*/true);
23243 /* If that didn't work, it could still be an identifier. */
23244 if (!cp_parser_parse_definitely (parser))
23246 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23248 type_start_token = cp_lexer_peek_token (parser->lexer);
23249 id = cp_parser_identifier (parser);
23251 else
23252 id = NULL_TREE;
23254 else
23256 template_id_p = true;
23257 ++num_templates;
23261 pop_deferring_access_checks ();
23263 if (id)
23265 cp_parser_check_for_invalid_template_id (parser, id,
23266 class_key,
23267 type_start_token->location);
23269 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23271 /* If it's not a `:' or a `{' then we can't really be looking at a
23272 class-head, since a class-head only appears as part of a
23273 class-specifier. We have to detect this situation before calling
23274 xref_tag, since that has irreversible side-effects. */
23275 if (!cp_parser_next_token_starts_class_definition_p (parser))
23277 cp_parser_error (parser, "expected %<{%> or %<:%>");
23278 type = error_mark_node;
23279 goto out;
23282 /* At this point, we're going ahead with the class-specifier, even
23283 if some other problem occurs. */
23284 cp_parser_commit_to_tentative_parse (parser);
23285 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23287 cp_parser_error (parser,
23288 "cannot specify %<override%> for a class");
23289 type = error_mark_node;
23290 goto out;
23292 /* Issue the error about the overly-qualified name now. */
23293 if (qualified_p)
23295 cp_parser_error (parser,
23296 "global qualification of class name is invalid");
23297 type = error_mark_node;
23298 goto out;
23300 else if (invalid_nested_name_p)
23302 cp_parser_error (parser,
23303 "qualified name does not name a class");
23304 type = error_mark_node;
23305 goto out;
23307 else if (nested_name_specifier)
23309 tree scope;
23311 if (bad_template_keyword)
23312 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23313 keyword template shall not appear at the top level. */
23314 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23315 "keyword %<template%> not allowed in class-head-name");
23317 /* Reject typedef-names in class heads. */
23318 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23320 error_at (type_start_token->location,
23321 "invalid class name in declaration of %qD",
23322 type);
23323 type = NULL_TREE;
23324 goto done;
23327 /* Figure out in what scope the declaration is being placed. */
23328 scope = current_scope ();
23329 /* If that scope does not contain the scope in which the
23330 class was originally declared, the program is invalid. */
23331 if (scope && !is_ancestor (scope, nested_name_specifier))
23333 if (at_namespace_scope_p ())
23334 error_at (type_start_token->location,
23335 "declaration of %qD in namespace %qD which does not "
23336 "enclose %qD",
23337 type, scope, nested_name_specifier);
23338 else
23339 error_at (type_start_token->location,
23340 "declaration of %qD in %qD which does not enclose %qD",
23341 type, scope, nested_name_specifier);
23342 type = NULL_TREE;
23343 goto done;
23345 /* [dcl.meaning]
23347 A declarator-id shall not be qualified except for the
23348 definition of a ... nested class outside of its class
23349 ... [or] the definition or explicit instantiation of a
23350 class member of a namespace outside of its namespace. */
23351 if (scope == nested_name_specifier)
23353 permerror (nested_name_specifier_token_start->location,
23354 "extra qualification not allowed");
23355 nested_name_specifier = NULL_TREE;
23356 num_templates = 0;
23359 /* An explicit-specialization must be preceded by "template <>". If
23360 it is not, try to recover gracefully. */
23361 if (at_namespace_scope_p ()
23362 && parser->num_template_parameter_lists == 0
23363 && !processing_template_parmlist
23364 && template_id_p)
23366 /* Build a location of this form:
23367 struct typename <ARGS>
23368 ^~~~~~~~~~~~~~~~~~~~~~
23369 with caret==start at the start token, and
23370 finishing at the end of the type. */
23371 location_t reported_loc
23372 = make_location (class_head_start_location,
23373 class_head_start_location,
23374 get_finish (type_start_token->location));
23375 rich_location richloc (line_table, reported_loc);
23376 richloc.add_fixit_insert_before (class_head_start_location,
23377 "template <> ");
23378 error_at (&richloc,
23379 "an explicit specialization must be preceded by"
23380 " %<template <>%>");
23381 invalid_explicit_specialization_p = true;
23382 /* Take the same action that would have been taken by
23383 cp_parser_explicit_specialization. */
23384 ++parser->num_template_parameter_lists;
23385 begin_specialization ();
23387 /* There must be no "return" statements between this point and the
23388 end of this function; set "type "to the correct return value and
23389 use "goto done;" to return. */
23390 /* Make sure that the right number of template parameters were
23391 present. */
23392 if (!cp_parser_check_template_parameters (parser, num_templates,
23393 template_id_p,
23394 type_start_token->location,
23395 /*declarator=*/NULL))
23397 /* If something went wrong, there is no point in even trying to
23398 process the class-definition. */
23399 type = NULL_TREE;
23400 goto done;
23403 /* Look up the type. */
23404 if (template_id_p)
23406 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23407 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23408 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23410 error_at (type_start_token->location,
23411 "function template %qD redeclared as a class template", id);
23412 type = error_mark_node;
23414 else
23416 type = TREE_TYPE (id);
23417 type = maybe_process_partial_specialization (type);
23419 /* Check the scope while we still know whether or not we had a
23420 nested-name-specifier. */
23421 if (type != error_mark_node)
23422 check_unqualified_spec_or_inst (type, type_start_token->location);
23424 if (nested_name_specifier)
23425 pushed_scope = push_scope (nested_name_specifier);
23427 else if (nested_name_specifier)
23429 tree class_type;
23431 /* Given:
23433 template <typename T> struct S { struct T };
23434 template <typename T> struct S<T>::T { };
23436 we will get a TYPENAME_TYPE when processing the definition of
23437 `S::T'. We need to resolve it to the actual type before we
23438 try to define it. */
23439 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23441 class_type = resolve_typename_type (TREE_TYPE (type),
23442 /*only_current_p=*/false);
23443 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23444 type = TYPE_NAME (class_type);
23445 else
23447 cp_parser_error (parser, "could not resolve typename type");
23448 type = error_mark_node;
23452 if (maybe_process_partial_specialization (TREE_TYPE (type))
23453 == error_mark_node)
23455 type = NULL_TREE;
23456 goto done;
23459 class_type = current_class_type;
23460 /* Enter the scope indicated by the nested-name-specifier. */
23461 pushed_scope = push_scope (nested_name_specifier);
23462 /* Get the canonical version of this type. */
23463 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23464 /* Call push_template_decl if it seems like we should be defining a
23465 template either from the template headers or the type we're
23466 defining, so that we diagnose both extra and missing headers. */
23467 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23468 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23469 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23471 type = push_template_decl (type);
23472 if (type == error_mark_node)
23474 type = NULL_TREE;
23475 goto done;
23479 type = TREE_TYPE (type);
23480 *nested_name_specifier_p = true;
23482 else /* The name is not a nested name. */
23484 /* If the class was unnamed, create a dummy name. */
23485 if (!id)
23486 id = make_anon_name ();
23487 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23488 ? ts_within_enclosing_non_class
23489 : ts_current);
23490 type = xref_tag (class_key, id, tag_scope,
23491 parser->num_template_parameter_lists);
23494 /* Indicate whether this class was declared as a `class' or as a
23495 `struct'. */
23496 if (TREE_CODE (type) == RECORD_TYPE)
23497 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23498 cp_parser_check_class_key (class_key, type);
23500 /* If this type was already complete, and we see another definition,
23501 that's an error. */
23502 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23504 error_at (type_start_token->location, "redefinition of %q#T",
23505 type);
23506 inform (location_of (type), "previous definition of %q#T",
23507 type);
23508 type = NULL_TREE;
23509 goto done;
23511 else if (type == error_mark_node)
23512 type = NULL_TREE;
23514 if (type)
23516 /* Apply attributes now, before any use of the class as a template
23517 argument in its base list. */
23518 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23519 fixup_attribute_variants (type);
23522 /* We will have entered the scope containing the class; the names of
23523 base classes should be looked up in that context. For example:
23525 struct A { struct B {}; struct C; };
23526 struct A::C : B {};
23528 is valid. */
23530 /* Get the list of base-classes, if there is one. */
23531 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23533 /* PR59482: enter the class scope so that base-specifiers are looked
23534 up correctly. */
23535 if (type)
23536 pushclass (type);
23537 bases = cp_parser_base_clause (parser);
23538 /* PR59482: get out of the previously pushed class scope so that the
23539 subsequent pops pop the right thing. */
23540 if (type)
23541 popclass ();
23543 else
23544 bases = NULL_TREE;
23546 /* If we're really defining a class, process the base classes.
23547 If they're invalid, fail. */
23548 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23549 xref_basetypes (type, bases);
23551 done:
23552 /* Leave the scope given by the nested-name-specifier. We will
23553 enter the class scope itself while processing the members. */
23554 if (pushed_scope)
23555 pop_scope (pushed_scope);
23557 if (invalid_explicit_specialization_p)
23559 end_specialization ();
23560 --parser->num_template_parameter_lists;
23563 if (type)
23564 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23565 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23566 CLASSTYPE_FINAL (type) = 1;
23567 out:
23568 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23569 return type;
23572 /* Parse a class-key.
23574 class-key:
23575 class
23576 struct
23577 union
23579 Returns the kind of class-key specified, or none_type to indicate
23580 error. */
23582 static enum tag_types
23583 cp_parser_class_key (cp_parser* parser)
23585 cp_token *token;
23586 enum tag_types tag_type;
23588 /* Look for the class-key. */
23589 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23590 if (!token)
23591 return none_type;
23593 /* Check to see if the TOKEN is a class-key. */
23594 tag_type = cp_parser_token_is_class_key (token);
23595 if (!tag_type)
23596 cp_parser_error (parser, "expected class-key");
23597 return tag_type;
23600 /* Parse a type-parameter-key.
23602 type-parameter-key:
23603 class
23604 typename
23607 static void
23608 cp_parser_type_parameter_key (cp_parser* parser)
23610 /* Look for the type-parameter-key. */
23611 enum tag_types tag_type = none_type;
23612 cp_token *token = cp_lexer_peek_token (parser->lexer);
23613 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23615 cp_lexer_consume_token (parser->lexer);
23616 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23617 /* typename is not allowed in a template template parameter
23618 by the standard until C++17. */
23619 pedwarn (token->location, OPT_Wpedantic,
23620 "ISO C++ forbids typename key in template template parameter;"
23621 " use -std=c++17 or -std=gnu++17");
23623 else
23624 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23626 return;
23629 /* Parse an (optional) member-specification.
23631 member-specification:
23632 member-declaration member-specification [opt]
23633 access-specifier : member-specification [opt] */
23635 static void
23636 cp_parser_member_specification_opt (cp_parser* parser)
23638 while (true)
23640 cp_token *token;
23641 enum rid keyword;
23643 /* Peek at the next token. */
23644 token = cp_lexer_peek_token (parser->lexer);
23645 /* If it's a `}', or EOF then we've seen all the members. */
23646 if (token->type == CPP_CLOSE_BRACE
23647 || token->type == CPP_EOF
23648 || token->type == CPP_PRAGMA_EOL)
23649 break;
23651 /* See if this token is a keyword. */
23652 keyword = token->keyword;
23653 switch (keyword)
23655 case RID_PUBLIC:
23656 case RID_PROTECTED:
23657 case RID_PRIVATE:
23658 /* Consume the access-specifier. */
23659 cp_lexer_consume_token (parser->lexer);
23660 /* Remember which access-specifier is active. */
23661 current_access_specifier = token->u.value;
23662 /* Look for the `:'. */
23663 cp_parser_require (parser, CPP_COLON, RT_COLON);
23664 break;
23666 default:
23667 /* Accept #pragmas at class scope. */
23668 if (token->type == CPP_PRAGMA)
23670 cp_parser_pragma (parser, pragma_member, NULL);
23671 break;
23674 /* Otherwise, the next construction must be a
23675 member-declaration. */
23676 cp_parser_member_declaration (parser);
23681 /* Parse a member-declaration.
23683 member-declaration:
23684 decl-specifier-seq [opt] member-declarator-list [opt] ;
23685 function-definition ; [opt]
23686 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23687 using-declaration
23688 template-declaration
23689 alias-declaration
23691 member-declarator-list:
23692 member-declarator
23693 member-declarator-list , member-declarator
23695 member-declarator:
23696 declarator pure-specifier [opt]
23697 declarator constant-initializer [opt]
23698 identifier [opt] : constant-expression
23700 GNU Extensions:
23702 member-declaration:
23703 __extension__ member-declaration
23705 member-declarator:
23706 declarator attributes [opt] pure-specifier [opt]
23707 declarator attributes [opt] constant-initializer [opt]
23708 identifier [opt] attributes [opt] : constant-expression
23710 C++0x Extensions:
23712 member-declaration:
23713 static_assert-declaration */
23715 static void
23716 cp_parser_member_declaration (cp_parser* parser)
23718 cp_decl_specifier_seq decl_specifiers;
23719 tree prefix_attributes;
23720 tree decl;
23721 int declares_class_or_enum;
23722 bool friend_p;
23723 cp_token *token = NULL;
23724 cp_token *decl_spec_token_start = NULL;
23725 cp_token *initializer_token_start = NULL;
23726 int saved_pedantic;
23727 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23729 /* Check for the `__extension__' keyword. */
23730 if (cp_parser_extension_opt (parser, &saved_pedantic))
23732 /* Recurse. */
23733 cp_parser_member_declaration (parser);
23734 /* Restore the old value of the PEDANTIC flag. */
23735 pedantic = saved_pedantic;
23737 return;
23740 /* Check for a template-declaration. */
23741 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23743 /* An explicit specialization here is an error condition, and we
23744 expect the specialization handler to detect and report this. */
23745 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23746 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23747 cp_parser_explicit_specialization (parser);
23748 else
23749 cp_parser_template_declaration (parser, /*member_p=*/true);
23751 return;
23753 /* Check for a template introduction. */
23754 else if (cp_parser_template_declaration_after_export (parser, true))
23755 return;
23757 /* Check for a using-declaration. */
23758 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23760 if (cxx_dialect < cxx11)
23762 /* Parse the using-declaration. */
23763 cp_parser_using_declaration (parser,
23764 /*access_declaration_p=*/false);
23765 return;
23767 else
23769 tree decl;
23770 bool alias_decl_expected;
23771 cp_parser_parse_tentatively (parser);
23772 decl = cp_parser_alias_declaration (parser);
23773 /* Note that if we actually see the '=' token after the
23774 identifier, cp_parser_alias_declaration commits the
23775 tentative parse. In that case, we really expect an
23776 alias-declaration. Otherwise, we expect a using
23777 declaration. */
23778 alias_decl_expected =
23779 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23780 cp_parser_parse_definitely (parser);
23782 if (alias_decl_expected)
23783 finish_member_declaration (decl);
23784 else
23785 cp_parser_using_declaration (parser,
23786 /*access_declaration_p=*/false);
23787 return;
23791 /* Check for @defs. */
23792 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23794 tree ivar, member;
23795 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23796 ivar = ivar_chains;
23797 while (ivar)
23799 member = ivar;
23800 ivar = TREE_CHAIN (member);
23801 TREE_CHAIN (member) = NULL_TREE;
23802 finish_member_declaration (member);
23804 return;
23807 /* If the next token is `static_assert' we have a static assertion. */
23808 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23810 cp_parser_static_assert (parser, /*member_p=*/true);
23811 return;
23814 parser->colon_corrects_to_scope_p = false;
23816 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23817 goto out;
23819 /* Parse the decl-specifier-seq. */
23820 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23821 cp_parser_decl_specifier_seq (parser,
23822 CP_PARSER_FLAGS_OPTIONAL,
23823 &decl_specifiers,
23824 &declares_class_or_enum);
23825 /* Check for an invalid type-name. */
23826 if (!decl_specifiers.any_type_specifiers_p
23827 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23828 goto out;
23829 /* If there is no declarator, then the decl-specifier-seq should
23830 specify a type. */
23831 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23833 /* If there was no decl-specifier-seq, and the next token is a
23834 `;', then we have something like:
23836 struct S { ; };
23838 [class.mem]
23840 Each member-declaration shall declare at least one member
23841 name of the class. */
23842 if (!decl_specifiers.any_specifiers_p)
23844 cp_token *token = cp_lexer_peek_token (parser->lexer);
23845 if (!in_system_header_at (token->location))
23847 gcc_rich_location richloc (token->location);
23848 richloc.add_fixit_remove ();
23849 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23852 else
23854 tree type;
23856 /* See if this declaration is a friend. */
23857 friend_p = cp_parser_friend_p (&decl_specifiers);
23858 /* If there were decl-specifiers, check to see if there was
23859 a class-declaration. */
23860 type = check_tag_decl (&decl_specifiers,
23861 /*explicit_type_instantiation_p=*/false);
23862 /* Nested classes have already been added to the class, but
23863 a `friend' needs to be explicitly registered. */
23864 if (friend_p)
23866 /* If the `friend' keyword was present, the friend must
23867 be introduced with a class-key. */
23868 if (!declares_class_or_enum && cxx_dialect < cxx11)
23869 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23870 "in C++03 a class-key must be used "
23871 "when declaring a friend");
23872 /* In this case:
23874 template <typename T> struct A {
23875 friend struct A<T>::B;
23878 A<T>::B will be represented by a TYPENAME_TYPE, and
23879 therefore not recognized by check_tag_decl. */
23880 if (!type)
23882 type = decl_specifiers.type;
23883 if (type && TREE_CODE (type) == TYPE_DECL)
23884 type = TREE_TYPE (type);
23886 if (!type || !TYPE_P (type))
23887 error_at (decl_spec_token_start->location,
23888 "friend declaration does not name a class or "
23889 "function");
23890 else
23891 make_friend_class (current_class_type, type,
23892 /*complain=*/true);
23894 /* If there is no TYPE, an error message will already have
23895 been issued. */
23896 else if (!type || type == error_mark_node)
23898 /* An anonymous aggregate has to be handled specially; such
23899 a declaration really declares a data member (with a
23900 particular type), as opposed to a nested class. */
23901 else if (ANON_AGGR_TYPE_P (type))
23903 /* C++11 9.5/6. */
23904 if (decl_specifiers.storage_class != sc_none)
23905 error_at (decl_spec_token_start->location,
23906 "a storage class on an anonymous aggregate "
23907 "in class scope is not allowed");
23909 /* Remove constructors and such from TYPE, now that we
23910 know it is an anonymous aggregate. */
23911 fixup_anonymous_aggr (type);
23912 /* And make the corresponding data member. */
23913 decl = build_decl (decl_spec_token_start->location,
23914 FIELD_DECL, NULL_TREE, type);
23915 /* Add it to the class. */
23916 finish_member_declaration (decl);
23918 else
23919 cp_parser_check_access_in_redeclaration
23920 (TYPE_NAME (type),
23921 decl_spec_token_start->location);
23924 else
23926 bool assume_semicolon = false;
23928 /* Clear attributes from the decl_specifiers but keep them
23929 around as prefix attributes that apply them to the entity
23930 being declared. */
23931 prefix_attributes = decl_specifiers.attributes;
23932 decl_specifiers.attributes = NULL_TREE;
23934 /* See if these declarations will be friends. */
23935 friend_p = cp_parser_friend_p (&decl_specifiers);
23937 /* Keep going until we hit the `;' at the end of the
23938 declaration. */
23939 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23941 tree attributes = NULL_TREE;
23942 tree first_attribute;
23943 tree initializer;
23944 bool named_bitfld = false;
23946 /* Peek at the next token. */
23947 token = cp_lexer_peek_token (parser->lexer);
23949 /* The following code wants to know early if it is a bit-field
23950 or some other declaration. Attributes can appear before
23951 the `:' token. Skip over them without consuming any tokens
23952 to peek if they are followed by `:'. */
23953 if (cp_next_tokens_can_be_attribute_p (parser)
23954 || (token->type == CPP_NAME
23955 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23956 && (named_bitfld = true)))
23958 size_t n
23959 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23960 token = cp_lexer_peek_nth_token (parser->lexer, n);
23963 /* Check for a bitfield declaration. */
23964 if (token->type == CPP_COLON
23965 || (token->type == CPP_NAME
23966 && token == cp_lexer_peek_token (parser->lexer)
23967 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23968 && (named_bitfld = true)))
23970 tree identifier;
23971 tree width;
23972 tree late_attributes = NULL_TREE;
23974 if (named_bitfld)
23975 identifier = cp_parser_identifier (parser);
23976 else
23977 identifier = NULL_TREE;
23979 /* Look for attributes that apply to the bitfield. */
23980 attributes = cp_parser_attributes_opt (parser);
23982 /* Consume the `:' token. */
23983 cp_lexer_consume_token (parser->lexer);
23985 /* Get the width of the bitfield. */
23986 width = cp_parser_constant_expression (parser, false, NULL,
23987 cxx_dialect >= cxx11);
23989 /* In C++2A and as extension for C++11 and above we allow
23990 default member initializers for bit-fields. */
23991 initializer = NULL_TREE;
23992 if (cxx_dialect >= cxx11
23993 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23994 || cp_lexer_next_token_is (parser->lexer,
23995 CPP_OPEN_BRACE)))
23997 location_t loc
23998 = cp_lexer_peek_token (parser->lexer)->location;
23999 if (cxx_dialect < cxx2a
24000 && !in_system_header_at (loc)
24001 && identifier != NULL_TREE)
24002 pedwarn (loc, 0,
24003 "default member initializers for bit-fields "
24004 "only available with -std=c++2a or "
24005 "-std=gnu++2a");
24007 initializer = cp_parser_save_nsdmi (parser);
24008 if (identifier == NULL_TREE)
24010 error_at (loc, "default member initializer for "
24011 "unnamed bit-field");
24012 initializer = NULL_TREE;
24015 else
24017 /* Look for attributes that apply to the bitfield after
24018 the `:' token and width. This is where GCC used to
24019 parse attributes in the past, pedwarn if there is
24020 a std attribute. */
24021 if (cp_next_tokens_can_be_std_attribute_p (parser))
24022 pedwarn (input_location, OPT_Wpedantic,
24023 "ISO C++ allows bit-field attributes only "
24024 "before the %<:%> token");
24026 late_attributes = cp_parser_attributes_opt (parser);
24029 attributes = attr_chainon (attributes, late_attributes);
24031 /* Remember which attributes are prefix attributes and
24032 which are not. */
24033 first_attribute = attributes;
24034 /* Combine the attributes. */
24035 attributes = attr_chainon (prefix_attributes, attributes);
24037 /* Create the bitfield declaration. */
24038 decl = grokbitfield (identifier
24039 ? make_id_declarator (NULL_TREE,
24040 identifier,
24041 sfk_none)
24042 : NULL,
24043 &decl_specifiers,
24044 width, initializer,
24045 attributes);
24047 else
24049 cp_declarator *declarator;
24050 tree asm_specification;
24051 int ctor_dtor_or_conv_p;
24053 /* Parse the declarator. */
24054 declarator
24055 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24056 &ctor_dtor_or_conv_p,
24057 /*parenthesized_p=*/NULL,
24058 /*member_p=*/true,
24059 friend_p);
24061 /* If something went wrong parsing the declarator, make sure
24062 that we at least consume some tokens. */
24063 if (declarator == cp_error_declarator)
24065 /* Skip to the end of the statement. */
24066 cp_parser_skip_to_end_of_statement (parser);
24067 /* If the next token is not a semicolon, that is
24068 probably because we just skipped over the body of
24069 a function. So, we consume a semicolon if
24070 present, but do not issue an error message if it
24071 is not present. */
24072 if (cp_lexer_next_token_is (parser->lexer,
24073 CPP_SEMICOLON))
24074 cp_lexer_consume_token (parser->lexer);
24075 goto out;
24078 if (declares_class_or_enum & 2)
24079 cp_parser_check_for_definition_in_return_type
24080 (declarator, decl_specifiers.type,
24081 decl_specifiers.locations[ds_type_spec]);
24083 /* Look for an asm-specification. */
24084 asm_specification = cp_parser_asm_specification_opt (parser);
24085 /* Look for attributes that apply to the declaration. */
24086 attributes = cp_parser_attributes_opt (parser);
24087 /* Remember which attributes are prefix attributes and
24088 which are not. */
24089 first_attribute = attributes;
24090 /* Combine the attributes. */
24091 attributes = attr_chainon (prefix_attributes, attributes);
24093 /* If it's an `=', then we have a constant-initializer or a
24094 pure-specifier. It is not correct to parse the
24095 initializer before registering the member declaration
24096 since the member declaration should be in scope while
24097 its initializer is processed. However, the rest of the
24098 front end does not yet provide an interface that allows
24099 us to handle this correctly. */
24100 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24102 /* In [class.mem]:
24104 A pure-specifier shall be used only in the declaration of
24105 a virtual function.
24107 A member-declarator can contain a constant-initializer
24108 only if it declares a static member of integral or
24109 enumeration type.
24111 Therefore, if the DECLARATOR is for a function, we look
24112 for a pure-specifier; otherwise, we look for a
24113 constant-initializer. When we call `grokfield', it will
24114 perform more stringent semantics checks. */
24115 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24116 if (function_declarator_p (declarator)
24117 || (decl_specifiers.type
24118 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24119 && declarator->kind == cdk_id
24120 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24121 == FUNCTION_TYPE)))
24122 initializer = cp_parser_pure_specifier (parser);
24123 else if (decl_specifiers.storage_class != sc_static)
24124 initializer = cp_parser_save_nsdmi (parser);
24125 else if (cxx_dialect >= cxx11)
24127 bool nonconst;
24128 /* Don't require a constant rvalue in C++11, since we
24129 might want a reference constant. We'll enforce
24130 constancy later. */
24131 cp_lexer_consume_token (parser->lexer);
24132 /* Parse the initializer. */
24133 initializer = cp_parser_initializer_clause (parser,
24134 &nonconst);
24136 else
24137 /* Parse the initializer. */
24138 initializer = cp_parser_constant_initializer (parser);
24140 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24141 && !function_declarator_p (declarator))
24143 bool x;
24144 if (decl_specifiers.storage_class != sc_static)
24145 initializer = cp_parser_save_nsdmi (parser);
24146 else
24147 initializer = cp_parser_initializer (parser, &x, &x);
24149 /* Otherwise, there is no initializer. */
24150 else
24151 initializer = NULL_TREE;
24153 /* See if we are probably looking at a function
24154 definition. We are certainly not looking at a
24155 member-declarator. Calling `grokfield' has
24156 side-effects, so we must not do it unless we are sure
24157 that we are looking at a member-declarator. */
24158 if (cp_parser_token_starts_function_definition_p
24159 (cp_lexer_peek_token (parser->lexer)))
24161 /* The grammar does not allow a pure-specifier to be
24162 used when a member function is defined. (It is
24163 possible that this fact is an oversight in the
24164 standard, since a pure function may be defined
24165 outside of the class-specifier. */
24166 if (initializer && initializer_token_start)
24167 error_at (initializer_token_start->location,
24168 "pure-specifier on function-definition");
24169 decl = cp_parser_save_member_function_body (parser,
24170 &decl_specifiers,
24171 declarator,
24172 attributes);
24173 if (parser->fully_implicit_function_template_p)
24174 decl = finish_fully_implicit_template (parser, decl);
24175 /* If the member was not a friend, declare it here. */
24176 if (!friend_p)
24177 finish_member_declaration (decl);
24178 /* Peek at the next token. */
24179 token = cp_lexer_peek_token (parser->lexer);
24180 /* If the next token is a semicolon, consume it. */
24181 if (token->type == CPP_SEMICOLON)
24183 location_t semicolon_loc
24184 = cp_lexer_consume_token (parser->lexer)->location;
24185 gcc_rich_location richloc (semicolon_loc);
24186 richloc.add_fixit_remove ();
24187 warning_at (&richloc, OPT_Wextra_semi,
24188 "extra %<;%> after in-class "
24189 "function definition");
24191 goto out;
24193 else
24194 if (declarator->kind == cdk_function)
24195 declarator->id_loc = token->location;
24196 /* Create the declaration. */
24197 decl = grokfield (declarator, &decl_specifiers,
24198 initializer, /*init_const_expr_p=*/true,
24199 asm_specification, attributes);
24200 if (parser->fully_implicit_function_template_p)
24202 if (friend_p)
24203 finish_fully_implicit_template (parser, 0);
24204 else
24205 decl = finish_fully_implicit_template (parser, decl);
24209 cp_finalize_omp_declare_simd (parser, decl);
24210 cp_finalize_oacc_routine (parser, decl, false);
24212 /* Reset PREFIX_ATTRIBUTES. */
24213 if (attributes != error_mark_node)
24215 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24216 attributes = TREE_CHAIN (attributes);
24217 if (attributes)
24218 TREE_CHAIN (attributes) = NULL_TREE;
24221 /* If there is any qualification still in effect, clear it
24222 now; we will be starting fresh with the next declarator. */
24223 parser->scope = NULL_TREE;
24224 parser->qualifying_scope = NULL_TREE;
24225 parser->object_scope = NULL_TREE;
24226 /* If it's a `,', then there are more declarators. */
24227 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24229 cp_lexer_consume_token (parser->lexer);
24230 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24232 cp_token *token = cp_lexer_previous_token (parser->lexer);
24233 gcc_rich_location richloc (token->location);
24234 richloc.add_fixit_remove ();
24235 error_at (&richloc, "stray %<,%> at end of "
24236 "member declaration");
24239 /* If the next token isn't a `;', then we have a parse error. */
24240 else if (cp_lexer_next_token_is_not (parser->lexer,
24241 CPP_SEMICOLON))
24243 /* The next token might be a ways away from where the
24244 actual semicolon is missing. Find the previous token
24245 and use that for our error position. */
24246 cp_token *token = cp_lexer_previous_token (parser->lexer);
24247 gcc_rich_location richloc (token->location);
24248 richloc.add_fixit_insert_after (";");
24249 error_at (&richloc, "expected %<;%> at end of "
24250 "member declaration");
24252 /* Assume that the user meant to provide a semicolon. If
24253 we were to cp_parser_skip_to_end_of_statement, we might
24254 skip to a semicolon inside a member function definition
24255 and issue nonsensical error messages. */
24256 assume_semicolon = true;
24259 if (decl)
24261 /* Add DECL to the list of members. */
24262 if (!friend_p
24263 /* Explicitly include, eg, NSDMIs, for better error
24264 recovery (c++/58650). */
24265 || !DECL_DECLARES_FUNCTION_P (decl))
24266 finish_member_declaration (decl);
24268 if (TREE_CODE (decl) == FUNCTION_DECL)
24269 cp_parser_save_default_args (parser, decl);
24270 else if (TREE_CODE (decl) == FIELD_DECL
24271 && DECL_INITIAL (decl))
24272 /* Add DECL to the queue of NSDMI to be parsed later. */
24273 vec_safe_push (unparsed_nsdmis, decl);
24276 if (assume_semicolon)
24277 goto out;
24281 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24282 out:
24283 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24286 /* Parse a pure-specifier.
24288 pure-specifier:
24291 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24292 Otherwise, ERROR_MARK_NODE is returned. */
24294 static tree
24295 cp_parser_pure_specifier (cp_parser* parser)
24297 cp_token *token;
24299 /* Look for the `=' token. */
24300 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24301 return error_mark_node;
24302 /* Look for the `0' token. */
24303 token = cp_lexer_peek_token (parser->lexer);
24305 if (token->type == CPP_EOF
24306 || token->type == CPP_PRAGMA_EOL)
24307 return error_mark_node;
24309 cp_lexer_consume_token (parser->lexer);
24311 /* Accept = default or = delete in c++0x mode. */
24312 if (token->keyword == RID_DEFAULT
24313 || token->keyword == RID_DELETE)
24315 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24316 return token->u.value;
24319 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24320 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24322 cp_parser_error (parser,
24323 "invalid pure specifier (only %<= 0%> is allowed)");
24324 cp_parser_skip_to_end_of_statement (parser);
24325 return error_mark_node;
24327 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24329 error_at (token->location, "templates may not be %<virtual%>");
24330 return error_mark_node;
24333 return integer_zero_node;
24336 /* Parse a constant-initializer.
24338 constant-initializer:
24339 = constant-expression
24341 Returns a representation of the constant-expression. */
24343 static tree
24344 cp_parser_constant_initializer (cp_parser* parser)
24346 /* Look for the `=' token. */
24347 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24348 return error_mark_node;
24350 /* It is invalid to write:
24352 struct S { static const int i = { 7 }; };
24355 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24357 cp_parser_error (parser,
24358 "a brace-enclosed initializer is not allowed here");
24359 /* Consume the opening brace. */
24360 matching_braces braces;
24361 braces.consume_open (parser);
24362 /* Skip the initializer. */
24363 cp_parser_skip_to_closing_brace (parser);
24364 /* Look for the trailing `}'. */
24365 braces.require_close (parser);
24367 return error_mark_node;
24370 return cp_parser_constant_expression (parser);
24373 /* Derived classes [gram.class.derived] */
24375 /* Parse a base-clause.
24377 base-clause:
24378 : base-specifier-list
24380 base-specifier-list:
24381 base-specifier ... [opt]
24382 base-specifier-list , base-specifier ... [opt]
24384 Returns a TREE_LIST representing the base-classes, in the order in
24385 which they were declared. The representation of each node is as
24386 described by cp_parser_base_specifier.
24388 In the case that no bases are specified, this function will return
24389 NULL_TREE, not ERROR_MARK_NODE. */
24391 static tree
24392 cp_parser_base_clause (cp_parser* parser)
24394 tree bases = NULL_TREE;
24396 /* Look for the `:' that begins the list. */
24397 cp_parser_require (parser, CPP_COLON, RT_COLON);
24399 /* Scan the base-specifier-list. */
24400 while (true)
24402 cp_token *token;
24403 tree base;
24404 bool pack_expansion_p = false;
24406 /* Look for the base-specifier. */
24407 base = cp_parser_base_specifier (parser);
24408 /* Look for the (optional) ellipsis. */
24409 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24411 /* Consume the `...'. */
24412 cp_lexer_consume_token (parser->lexer);
24414 pack_expansion_p = true;
24417 /* Add BASE to the front of the list. */
24418 if (base && base != error_mark_node)
24420 if (pack_expansion_p)
24421 /* Make this a pack expansion type. */
24422 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24424 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24426 TREE_CHAIN (base) = bases;
24427 bases = base;
24430 /* Peek at the next token. */
24431 token = cp_lexer_peek_token (parser->lexer);
24432 /* If it's not a comma, then the list is complete. */
24433 if (token->type != CPP_COMMA)
24434 break;
24435 /* Consume the `,'. */
24436 cp_lexer_consume_token (parser->lexer);
24439 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24440 base class had a qualified name. However, the next name that
24441 appears is certainly not qualified. */
24442 parser->scope = NULL_TREE;
24443 parser->qualifying_scope = NULL_TREE;
24444 parser->object_scope = NULL_TREE;
24446 return nreverse (bases);
24449 /* Parse a base-specifier.
24451 base-specifier:
24452 :: [opt] nested-name-specifier [opt] class-name
24453 virtual access-specifier [opt] :: [opt] nested-name-specifier
24454 [opt] class-name
24455 access-specifier virtual [opt] :: [opt] nested-name-specifier
24456 [opt] class-name
24458 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24459 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24460 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24461 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24463 static tree
24464 cp_parser_base_specifier (cp_parser* parser)
24466 cp_token *token;
24467 bool done = false;
24468 bool virtual_p = false;
24469 bool duplicate_virtual_error_issued_p = false;
24470 bool duplicate_access_error_issued_p = false;
24471 bool class_scope_p, template_p;
24472 tree access = access_default_node;
24473 tree type;
24475 /* Process the optional `virtual' and `access-specifier'. */
24476 while (!done)
24478 /* Peek at the next token. */
24479 token = cp_lexer_peek_token (parser->lexer);
24480 /* Process `virtual'. */
24481 switch (token->keyword)
24483 case RID_VIRTUAL:
24484 /* If `virtual' appears more than once, issue an error. */
24485 if (virtual_p && !duplicate_virtual_error_issued_p)
24487 cp_parser_error (parser,
24488 "%<virtual%> specified more than once in base-specifier");
24489 duplicate_virtual_error_issued_p = true;
24492 virtual_p = true;
24494 /* Consume the `virtual' token. */
24495 cp_lexer_consume_token (parser->lexer);
24497 break;
24499 case RID_PUBLIC:
24500 case RID_PROTECTED:
24501 case RID_PRIVATE:
24502 /* If more than one access specifier appears, issue an
24503 error. */
24504 if (access != access_default_node
24505 && !duplicate_access_error_issued_p)
24507 cp_parser_error (parser,
24508 "more than one access specifier in base-specifier");
24509 duplicate_access_error_issued_p = true;
24512 access = ridpointers[(int) token->keyword];
24514 /* Consume the access-specifier. */
24515 cp_lexer_consume_token (parser->lexer);
24517 break;
24519 default:
24520 done = true;
24521 break;
24524 /* It is not uncommon to see programs mechanically, erroneously, use
24525 the 'typename' keyword to denote (dependent) qualified types
24526 as base classes. */
24527 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24529 token = cp_lexer_peek_token (parser->lexer);
24530 if (!processing_template_decl)
24531 error_at (token->location,
24532 "keyword %<typename%> not allowed outside of templates");
24533 else
24534 error_at (token->location,
24535 "keyword %<typename%> not allowed in this context "
24536 "(the base class is implicitly a type)");
24537 cp_lexer_consume_token (parser->lexer);
24540 /* Look for the optional `::' operator. */
24541 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24542 /* Look for the nested-name-specifier. The simplest way to
24543 implement:
24545 [temp.res]
24547 The keyword `typename' is not permitted in a base-specifier or
24548 mem-initializer; in these contexts a qualified name that
24549 depends on a template-parameter is implicitly assumed to be a
24550 type name.
24552 is to pretend that we have seen the `typename' keyword at this
24553 point. */
24554 cp_parser_nested_name_specifier_opt (parser,
24555 /*typename_keyword_p=*/true,
24556 /*check_dependency_p=*/true,
24557 /*type_p=*/true,
24558 /*is_declaration=*/true);
24559 /* If the base class is given by a qualified name, assume that names
24560 we see are type names or templates, as appropriate. */
24561 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24562 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24564 if (!parser->scope
24565 && cp_lexer_next_token_is_decltype (parser->lexer))
24566 /* DR 950 allows decltype as a base-specifier. */
24567 type = cp_parser_decltype (parser);
24568 else
24570 /* Otherwise, look for the class-name. */
24571 type = cp_parser_class_name (parser,
24572 class_scope_p,
24573 template_p,
24574 typename_type,
24575 /*check_dependency_p=*/true,
24576 /*class_head_p=*/false,
24577 /*is_declaration=*/true);
24578 type = TREE_TYPE (type);
24581 if (type == error_mark_node)
24582 return error_mark_node;
24584 return finish_base_specifier (type, access, virtual_p);
24587 /* Exception handling [gram.exception] */
24589 /* Parse an (optional) noexcept-specification.
24591 noexcept-specification:
24592 noexcept ( constant-expression ) [opt]
24594 If no noexcept-specification is present, returns NULL_TREE.
24595 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24596 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24597 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24598 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24599 in which case a boolean condition is returned instead. */
24601 static tree
24602 cp_parser_noexcept_specification_opt (cp_parser* parser,
24603 bool require_constexpr,
24604 bool* consumed_expr,
24605 bool return_cond)
24607 cp_token *token;
24608 const char *saved_message;
24610 /* Peek at the next token. */
24611 token = cp_lexer_peek_token (parser->lexer);
24613 /* Is it a noexcept-specification? */
24614 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24616 tree expr;
24617 cp_lexer_consume_token (parser->lexer);
24619 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24621 matching_parens parens;
24622 parens.consume_open (parser);
24624 if (require_constexpr)
24626 /* Types may not be defined in an exception-specification. */
24627 saved_message = parser->type_definition_forbidden_message;
24628 parser->type_definition_forbidden_message
24629 = G_("types may not be defined in an exception-specification");
24631 expr = cp_parser_constant_expression (parser);
24633 /* Restore the saved message. */
24634 parser->type_definition_forbidden_message = saved_message;
24636 else
24638 expr = cp_parser_expression (parser);
24639 *consumed_expr = true;
24642 parens.require_close (parser);
24644 else
24646 expr = boolean_true_node;
24647 if (!require_constexpr)
24648 *consumed_expr = false;
24651 /* We cannot build a noexcept-spec right away because this will check
24652 that expr is a constexpr. */
24653 if (!return_cond)
24654 return build_noexcept_spec (expr, tf_warning_or_error);
24655 else
24656 return expr;
24658 else
24659 return NULL_TREE;
24662 /* Parse an (optional) exception-specification.
24664 exception-specification:
24665 throw ( type-id-list [opt] )
24667 Returns a TREE_LIST representing the exception-specification. The
24668 TREE_VALUE of each node is a type. */
24670 static tree
24671 cp_parser_exception_specification_opt (cp_parser* parser)
24673 cp_token *token;
24674 tree type_id_list;
24675 const char *saved_message;
24677 /* Peek at the next token. */
24678 token = cp_lexer_peek_token (parser->lexer);
24680 /* Is it a noexcept-specification? */
24681 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24682 false);
24683 if (type_id_list != NULL_TREE)
24684 return type_id_list;
24686 /* If it's not `throw', then there's no exception-specification. */
24687 if (!cp_parser_is_keyword (token, RID_THROW))
24688 return NULL_TREE;
24690 location_t loc = token->location;
24692 /* Consume the `throw'. */
24693 cp_lexer_consume_token (parser->lexer);
24695 /* Look for the `('. */
24696 matching_parens parens;
24697 parens.require_open (parser);
24699 /* Peek at the next token. */
24700 token = cp_lexer_peek_token (parser->lexer);
24701 /* If it's not a `)', then there is a type-id-list. */
24702 if (token->type != CPP_CLOSE_PAREN)
24704 /* Types may not be defined in an exception-specification. */
24705 saved_message = parser->type_definition_forbidden_message;
24706 parser->type_definition_forbidden_message
24707 = G_("types may not be defined in an exception-specification");
24708 /* Parse the type-id-list. */
24709 type_id_list = cp_parser_type_id_list (parser);
24710 /* Restore the saved message. */
24711 parser->type_definition_forbidden_message = saved_message;
24713 if (cxx_dialect >= cxx17)
24715 error_at (loc, "ISO C++17 does not allow dynamic exception "
24716 "specifications");
24717 type_id_list = NULL_TREE;
24719 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24720 warning_at (loc, OPT_Wdeprecated,
24721 "dynamic exception specifications are deprecated in "
24722 "C++11");
24724 /* In C++17, throw() is equivalent to noexcept (true). throw()
24725 is deprecated in C++11 and above as well, but is still widely used,
24726 so don't warn about it yet. */
24727 else if (cxx_dialect >= cxx17)
24728 type_id_list = noexcept_true_spec;
24729 else
24730 type_id_list = empty_except_spec;
24732 /* Look for the `)'. */
24733 parens.require_close (parser);
24735 return type_id_list;
24738 /* Parse an (optional) type-id-list.
24740 type-id-list:
24741 type-id ... [opt]
24742 type-id-list , type-id ... [opt]
24744 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24745 in the order that the types were presented. */
24747 static tree
24748 cp_parser_type_id_list (cp_parser* parser)
24750 tree types = NULL_TREE;
24752 while (true)
24754 cp_token *token;
24755 tree type;
24757 token = cp_lexer_peek_token (parser->lexer);
24759 /* Get the next type-id. */
24760 type = cp_parser_type_id (parser);
24761 /* Check for invalid 'auto'. */
24762 if (flag_concepts && type_uses_auto (type))
24764 error_at (token->location,
24765 "invalid use of %<auto%> in exception-specification");
24766 type = error_mark_node;
24768 /* Parse the optional ellipsis. */
24769 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24771 /* Consume the `...'. */
24772 cp_lexer_consume_token (parser->lexer);
24774 /* Turn the type into a pack expansion expression. */
24775 type = make_pack_expansion (type);
24777 /* Add it to the list. */
24778 types = add_exception_specifier (types, type, /*complain=*/1);
24779 /* Peek at the next token. */
24780 token = cp_lexer_peek_token (parser->lexer);
24781 /* If it is not a `,', we are done. */
24782 if (token->type != CPP_COMMA)
24783 break;
24784 /* Consume the `,'. */
24785 cp_lexer_consume_token (parser->lexer);
24788 return nreverse (types);
24791 /* Parse a try-block.
24793 try-block:
24794 try compound-statement handler-seq */
24796 static tree
24797 cp_parser_try_block (cp_parser* parser)
24799 tree try_block;
24801 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24802 if (parser->in_function_body
24803 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24804 error ("%<try%> in %<constexpr%> function");
24806 try_block = begin_try_block ();
24807 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24808 finish_try_block (try_block);
24809 cp_parser_handler_seq (parser);
24810 finish_handler_sequence (try_block);
24812 return try_block;
24815 /* Parse a function-try-block.
24817 function-try-block:
24818 try ctor-initializer [opt] function-body handler-seq */
24820 static void
24821 cp_parser_function_try_block (cp_parser* parser)
24823 tree compound_stmt;
24824 tree try_block;
24826 /* Look for the `try' keyword. */
24827 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24828 return;
24829 /* Let the rest of the front end know where we are. */
24830 try_block = begin_function_try_block (&compound_stmt);
24831 /* Parse the function-body. */
24832 cp_parser_ctor_initializer_opt_and_function_body
24833 (parser, /*in_function_try_block=*/true);
24834 /* We're done with the `try' part. */
24835 finish_function_try_block (try_block);
24836 /* Parse the handlers. */
24837 cp_parser_handler_seq (parser);
24838 /* We're done with the handlers. */
24839 finish_function_handler_sequence (try_block, compound_stmt);
24842 /* Parse a handler-seq.
24844 handler-seq:
24845 handler handler-seq [opt] */
24847 static void
24848 cp_parser_handler_seq (cp_parser* parser)
24850 while (true)
24852 cp_token *token;
24854 /* Parse the handler. */
24855 cp_parser_handler (parser);
24856 /* Peek at the next token. */
24857 token = cp_lexer_peek_token (parser->lexer);
24858 /* If it's not `catch' then there are no more handlers. */
24859 if (!cp_parser_is_keyword (token, RID_CATCH))
24860 break;
24864 /* Parse a handler.
24866 handler:
24867 catch ( exception-declaration ) compound-statement */
24869 static void
24870 cp_parser_handler (cp_parser* parser)
24872 tree handler;
24873 tree declaration;
24875 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24876 handler = begin_handler ();
24877 matching_parens parens;
24878 parens.require_open (parser);
24879 declaration = cp_parser_exception_declaration (parser);
24880 finish_handler_parms (declaration, handler);
24881 parens.require_close (parser);
24882 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24883 finish_handler (handler);
24886 /* Parse an exception-declaration.
24888 exception-declaration:
24889 type-specifier-seq declarator
24890 type-specifier-seq abstract-declarator
24891 type-specifier-seq
24894 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24895 ellipsis variant is used. */
24897 static tree
24898 cp_parser_exception_declaration (cp_parser* parser)
24900 cp_decl_specifier_seq type_specifiers;
24901 cp_declarator *declarator;
24902 const char *saved_message;
24904 /* If it's an ellipsis, it's easy to handle. */
24905 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24907 /* Consume the `...' token. */
24908 cp_lexer_consume_token (parser->lexer);
24909 return NULL_TREE;
24912 /* Types may not be defined in exception-declarations. */
24913 saved_message = parser->type_definition_forbidden_message;
24914 parser->type_definition_forbidden_message
24915 = G_("types may not be defined in exception-declarations");
24917 /* Parse the type-specifier-seq. */
24918 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24919 /*is_trailing_return=*/false,
24920 &type_specifiers);
24921 /* If it's a `)', then there is no declarator. */
24922 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24923 declarator = NULL;
24924 else
24925 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24926 /*ctor_dtor_or_conv_p=*/NULL,
24927 /*parenthesized_p=*/NULL,
24928 /*member_p=*/false,
24929 /*friend_p=*/false);
24931 /* Restore the saved message. */
24932 parser->type_definition_forbidden_message = saved_message;
24934 if (!type_specifiers.any_specifiers_p)
24935 return error_mark_node;
24937 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24940 /* Parse a throw-expression.
24942 throw-expression:
24943 throw assignment-expression [opt]
24945 Returns a THROW_EXPR representing the throw-expression. */
24947 static tree
24948 cp_parser_throw_expression (cp_parser* parser)
24950 tree expression;
24951 cp_token* token;
24953 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24954 token = cp_lexer_peek_token (parser->lexer);
24955 /* Figure out whether or not there is an assignment-expression
24956 following the "throw" keyword. */
24957 if (token->type == CPP_COMMA
24958 || token->type == CPP_SEMICOLON
24959 || token->type == CPP_CLOSE_PAREN
24960 || token->type == CPP_CLOSE_SQUARE
24961 || token->type == CPP_CLOSE_BRACE
24962 || token->type == CPP_COLON)
24963 expression = NULL_TREE;
24964 else
24965 expression = cp_parser_assignment_expression (parser);
24967 return build_throw (expression);
24970 /* GNU Extensions */
24972 /* Parse an (optional) asm-specification.
24974 asm-specification:
24975 asm ( string-literal )
24977 If the asm-specification is present, returns a STRING_CST
24978 corresponding to the string-literal. Otherwise, returns
24979 NULL_TREE. */
24981 static tree
24982 cp_parser_asm_specification_opt (cp_parser* parser)
24984 cp_token *token;
24985 tree asm_specification;
24987 /* Peek at the next token. */
24988 token = cp_lexer_peek_token (parser->lexer);
24989 /* If the next token isn't the `asm' keyword, then there's no
24990 asm-specification. */
24991 if (!cp_parser_is_keyword (token, RID_ASM))
24992 return NULL_TREE;
24994 /* Consume the `asm' token. */
24995 cp_lexer_consume_token (parser->lexer);
24996 /* Look for the `('. */
24997 matching_parens parens;
24998 parens.require_open (parser);
25000 /* Look for the string-literal. */
25001 asm_specification = cp_parser_string_literal (parser, false, false);
25003 /* Look for the `)'. */
25004 parens.require_close (parser);
25006 return asm_specification;
25009 /* Parse an asm-operand-list.
25011 asm-operand-list:
25012 asm-operand
25013 asm-operand-list , asm-operand
25015 asm-operand:
25016 string-literal ( expression )
25017 [ string-literal ] string-literal ( expression )
25019 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25020 each node is the expression. The TREE_PURPOSE is itself a
25021 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25022 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25023 is a STRING_CST for the string literal before the parenthesis. Returns
25024 ERROR_MARK_NODE if any of the operands are invalid. */
25026 static tree
25027 cp_parser_asm_operand_list (cp_parser* parser)
25029 tree asm_operands = NULL_TREE;
25030 bool invalid_operands = false;
25032 while (true)
25034 tree string_literal;
25035 tree expression;
25036 tree name;
25038 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25040 /* Consume the `[' token. */
25041 cp_lexer_consume_token (parser->lexer);
25042 /* Read the operand name. */
25043 name = cp_parser_identifier (parser);
25044 if (name != error_mark_node)
25045 name = build_string (IDENTIFIER_LENGTH (name),
25046 IDENTIFIER_POINTER (name));
25047 /* Look for the closing `]'. */
25048 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25050 else
25051 name = NULL_TREE;
25052 /* Look for the string-literal. */
25053 string_literal = cp_parser_string_literal (parser, false, false);
25055 /* Look for the `('. */
25056 matching_parens parens;
25057 parens.require_open (parser);
25058 /* Parse the expression. */
25059 expression = cp_parser_expression (parser);
25060 /* Look for the `)'. */
25061 parens.require_close (parser);
25063 if (name == error_mark_node
25064 || string_literal == error_mark_node
25065 || expression == error_mark_node)
25066 invalid_operands = true;
25068 /* Add this operand to the list. */
25069 asm_operands = tree_cons (build_tree_list (name, string_literal),
25070 expression,
25071 asm_operands);
25072 /* If the next token is not a `,', there are no more
25073 operands. */
25074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25075 break;
25076 /* Consume the `,'. */
25077 cp_lexer_consume_token (parser->lexer);
25080 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25083 /* Parse an asm-clobber-list.
25085 asm-clobber-list:
25086 string-literal
25087 asm-clobber-list , string-literal
25089 Returns a TREE_LIST, indicating the clobbers in the order that they
25090 appeared. The TREE_VALUE of each node is a STRING_CST. */
25092 static tree
25093 cp_parser_asm_clobber_list (cp_parser* parser)
25095 tree clobbers = NULL_TREE;
25097 while (true)
25099 tree string_literal;
25101 /* Look for the string literal. */
25102 string_literal = cp_parser_string_literal (parser, false, false);
25103 /* Add it to the list. */
25104 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25105 /* If the next token is not a `,', then the list is
25106 complete. */
25107 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25108 break;
25109 /* Consume the `,' token. */
25110 cp_lexer_consume_token (parser->lexer);
25113 return clobbers;
25116 /* Parse an asm-label-list.
25118 asm-label-list:
25119 identifier
25120 asm-label-list , identifier
25122 Returns a TREE_LIST, indicating the labels in the order that they
25123 appeared. The TREE_VALUE of each node is a label. */
25125 static tree
25126 cp_parser_asm_label_list (cp_parser* parser)
25128 tree labels = NULL_TREE;
25130 while (true)
25132 tree identifier, label, name;
25134 /* Look for the identifier. */
25135 identifier = cp_parser_identifier (parser);
25136 if (!error_operand_p (identifier))
25138 label = lookup_label (identifier);
25139 if (TREE_CODE (label) == LABEL_DECL)
25141 TREE_USED (label) = 1;
25142 check_goto (label);
25143 name = build_string (IDENTIFIER_LENGTH (identifier),
25144 IDENTIFIER_POINTER (identifier));
25145 labels = tree_cons (name, label, labels);
25148 /* If the next token is not a `,', then the list is
25149 complete. */
25150 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25151 break;
25152 /* Consume the `,' token. */
25153 cp_lexer_consume_token (parser->lexer);
25156 return nreverse (labels);
25159 /* Return TRUE iff the next tokens in the stream are possibly the
25160 beginning of a GNU extension attribute. */
25162 static bool
25163 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25165 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25168 /* Return TRUE iff the next tokens in the stream are possibly the
25169 beginning of a standard C++-11 attribute specifier. */
25171 static bool
25172 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25174 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25177 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25178 beginning of a standard C++-11 attribute specifier. */
25180 static bool
25181 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25183 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25185 return (cxx_dialect >= cxx11
25186 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25187 || (token->type == CPP_OPEN_SQUARE
25188 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25189 && token->type == CPP_OPEN_SQUARE)));
25192 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25193 beginning of a GNU extension attribute. */
25195 static bool
25196 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25198 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25200 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25203 /* Return true iff the next tokens can be the beginning of either a
25204 GNU attribute list, or a standard C++11 attribute sequence. */
25206 static bool
25207 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25209 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25210 || cp_next_tokens_can_be_std_attribute_p (parser));
25213 /* Return true iff the next Nth tokens can be the beginning of either
25214 a GNU attribute list, or a standard C++11 attribute sequence. */
25216 static bool
25217 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25219 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25220 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25223 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25224 of GNU attributes, or return NULL. */
25226 static tree
25227 cp_parser_attributes_opt (cp_parser *parser)
25229 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25230 return cp_parser_gnu_attributes_opt (parser);
25231 return cp_parser_std_attribute_spec_seq (parser);
25234 /* Parse an (optional) series of attributes.
25236 attributes:
25237 attributes attribute
25239 attribute:
25240 __attribute__ (( attribute-list [opt] ))
25242 The return value is as for cp_parser_gnu_attribute_list. */
25244 static tree
25245 cp_parser_gnu_attributes_opt (cp_parser* parser)
25247 tree attributes = NULL_TREE;
25249 temp_override<bool> cleanup
25250 (parser->auto_is_implicit_function_template_parm_p, false);
25252 while (true)
25254 cp_token *token;
25255 tree attribute_list;
25256 bool ok = true;
25258 /* Peek at the next token. */
25259 token = cp_lexer_peek_token (parser->lexer);
25260 /* If it's not `__attribute__', then we're done. */
25261 if (token->keyword != RID_ATTRIBUTE)
25262 break;
25264 /* Consume the `__attribute__' keyword. */
25265 cp_lexer_consume_token (parser->lexer);
25266 /* Look for the two `(' tokens. */
25267 matching_parens outer_parens;
25268 outer_parens.require_open (parser);
25269 matching_parens inner_parens;
25270 inner_parens.require_open (parser);
25272 /* Peek at the next token. */
25273 token = cp_lexer_peek_token (parser->lexer);
25274 if (token->type != CPP_CLOSE_PAREN)
25275 /* Parse the attribute-list. */
25276 attribute_list = cp_parser_gnu_attribute_list (parser);
25277 else
25278 /* If the next token is a `)', then there is no attribute
25279 list. */
25280 attribute_list = NULL;
25282 /* Look for the two `)' tokens. */
25283 if (!inner_parens.require_close (parser))
25284 ok = false;
25285 if (!outer_parens.require_close (parser))
25286 ok = false;
25287 if (!ok)
25288 cp_parser_skip_to_end_of_statement (parser);
25290 /* Add these new attributes to the list. */
25291 attributes = attr_chainon (attributes, attribute_list);
25294 return attributes;
25297 /* Parse a GNU attribute-list.
25299 attribute-list:
25300 attribute
25301 attribute-list , attribute
25303 attribute:
25304 identifier
25305 identifier ( identifier )
25306 identifier ( identifier , expression-list )
25307 identifier ( expression-list )
25309 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25310 to an attribute. The TREE_PURPOSE of each node is the identifier
25311 indicating which attribute is in use. The TREE_VALUE represents
25312 the arguments, if any. */
25314 static tree
25315 cp_parser_gnu_attribute_list (cp_parser* parser)
25317 tree attribute_list = NULL_TREE;
25318 bool save_translate_strings_p = parser->translate_strings_p;
25320 parser->translate_strings_p = false;
25321 while (true)
25323 cp_token *token;
25324 tree identifier;
25325 tree attribute;
25327 /* Look for the identifier. We also allow keywords here; for
25328 example `__attribute__ ((const))' is legal. */
25329 token = cp_lexer_peek_token (parser->lexer);
25330 if (token->type == CPP_NAME
25331 || token->type == CPP_KEYWORD)
25333 tree arguments = NULL_TREE;
25335 /* Consume the token, but save it since we need it for the
25336 SIMD enabled function parsing. */
25337 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25339 /* Save away the identifier that indicates which attribute
25340 this is. */
25341 identifier = (token->type == CPP_KEYWORD)
25342 /* For keywords, use the canonical spelling, not the
25343 parsed identifier. */
25344 ? ridpointers[(int) token->keyword]
25345 : id_token->u.value;
25347 identifier = canonicalize_attr_name (identifier);
25348 attribute = build_tree_list (identifier, NULL_TREE);
25350 /* Peek at the next token. */
25351 token = cp_lexer_peek_token (parser->lexer);
25352 /* If it's an `(', then parse the attribute arguments. */
25353 if (token->type == CPP_OPEN_PAREN)
25355 vec<tree, va_gc> *vec;
25356 int attr_flag = (attribute_takes_identifier_p (identifier)
25357 ? id_attr : normal_attr);
25358 vec = cp_parser_parenthesized_expression_list
25359 (parser, attr_flag, /*cast_p=*/false,
25360 /*allow_expansion_p=*/false,
25361 /*non_constant_p=*/NULL);
25362 if (vec == NULL)
25363 arguments = error_mark_node;
25364 else
25366 arguments = build_tree_list_vec (vec);
25367 release_tree_vector (vec);
25369 /* Save the arguments away. */
25370 TREE_VALUE (attribute) = arguments;
25373 if (arguments != error_mark_node)
25375 /* Add this attribute to the list. */
25376 TREE_CHAIN (attribute) = attribute_list;
25377 attribute_list = attribute;
25380 token = cp_lexer_peek_token (parser->lexer);
25382 /* Now, look for more attributes. If the next token isn't a
25383 `,', we're done. */
25384 if (token->type != CPP_COMMA)
25385 break;
25387 /* Consume the comma and keep going. */
25388 cp_lexer_consume_token (parser->lexer);
25390 parser->translate_strings_p = save_translate_strings_p;
25392 /* We built up the list in reverse order. */
25393 return nreverse (attribute_list);
25396 /* Parse a standard C++11 attribute.
25398 The returned representation is a TREE_LIST which TREE_PURPOSE is
25399 the scoped name of the attribute, and the TREE_VALUE is its
25400 arguments list.
25402 Note that the scoped name of the attribute is itself a TREE_LIST
25403 which TREE_PURPOSE is the namespace of the attribute, and
25404 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25405 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25406 and which TREE_PURPOSE is directly the attribute name.
25408 Clients of the attribute code should use get_attribute_namespace
25409 and get_attribute_name to get the actual namespace and name of
25410 attributes, regardless of their being GNU or C++11 attributes.
25412 attribute:
25413 attribute-token attribute-argument-clause [opt]
25415 attribute-token:
25416 identifier
25417 attribute-scoped-token
25419 attribute-scoped-token:
25420 attribute-namespace :: identifier
25422 attribute-namespace:
25423 identifier
25425 attribute-argument-clause:
25426 ( balanced-token-seq )
25428 balanced-token-seq:
25429 balanced-token [opt]
25430 balanced-token-seq balanced-token
25432 balanced-token:
25433 ( balanced-token-seq )
25434 [ balanced-token-seq ]
25435 { balanced-token-seq }. */
25437 static tree
25438 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25440 tree attribute, attr_id = NULL_TREE, arguments;
25441 cp_token *token;
25443 temp_override<bool> cleanup
25444 (parser->auto_is_implicit_function_template_parm_p, false);
25446 /* First, parse name of the attribute, a.k.a attribute-token. */
25448 token = cp_lexer_peek_token (parser->lexer);
25449 if (token->type == CPP_NAME)
25450 attr_id = token->u.value;
25451 else if (token->type == CPP_KEYWORD)
25452 attr_id = ridpointers[(int) token->keyword];
25453 else if (token->flags & NAMED_OP)
25454 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25456 if (attr_id == NULL_TREE)
25457 return NULL_TREE;
25459 cp_lexer_consume_token (parser->lexer);
25461 token = cp_lexer_peek_token (parser->lexer);
25462 if (token->type == CPP_SCOPE)
25464 /* We are seeing a scoped attribute token. */
25466 cp_lexer_consume_token (parser->lexer);
25467 if (attr_ns)
25468 error_at (token->location, "attribute using prefix used together "
25469 "with scoped attribute token");
25470 attr_ns = attr_id;
25472 token = cp_lexer_consume_token (parser->lexer);
25473 if (token->type == CPP_NAME)
25474 attr_id = token->u.value;
25475 else if (token->type == CPP_KEYWORD)
25476 attr_id = ridpointers[(int) token->keyword];
25477 else if (token->flags & NAMED_OP)
25478 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25479 else
25481 error_at (token->location,
25482 "expected an identifier for the attribute name");
25483 return error_mark_node;
25486 attr_ns = canonicalize_attr_name (attr_ns);
25487 attr_id = canonicalize_attr_name (attr_id);
25488 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25489 NULL_TREE);
25490 token = cp_lexer_peek_token (parser->lexer);
25492 else if (attr_ns)
25494 attr_ns = canonicalize_attr_name (attr_ns);
25495 attr_id = canonicalize_attr_name (attr_id);
25496 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25497 NULL_TREE);
25499 else
25501 attr_id = canonicalize_attr_name (attr_id);
25502 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25503 NULL_TREE);
25504 /* C++11 noreturn attribute is equivalent to GNU's. */
25505 if (is_attribute_p ("noreturn", attr_id))
25506 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25507 /* C++14 deprecated attribute is equivalent to GNU's. */
25508 else if (is_attribute_p ("deprecated", attr_id))
25509 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25510 /* C++17 fallthrough attribute is equivalent to GNU's. */
25511 else if (is_attribute_p ("fallthrough", attr_id))
25512 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25513 /* Transactional Memory TS optimize_for_synchronized attribute is
25514 equivalent to GNU transaction_callable. */
25515 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25516 TREE_PURPOSE (attribute)
25517 = get_identifier ("transaction_callable");
25518 /* Transactional Memory attributes are GNU attributes. */
25519 else if (tm_attr_to_mask (attr_id))
25520 TREE_PURPOSE (attribute) = attr_id;
25523 /* Now parse the optional argument clause of the attribute. */
25525 if (token->type != CPP_OPEN_PAREN)
25526 return attribute;
25529 vec<tree, va_gc> *vec;
25530 int attr_flag = normal_attr;
25532 if (attr_ns == gnu_identifier
25533 && attribute_takes_identifier_p (attr_id))
25534 /* A GNU attribute that takes an identifier in parameter. */
25535 attr_flag = id_attr;
25537 vec = cp_parser_parenthesized_expression_list
25538 (parser, attr_flag, /*cast_p=*/false,
25539 /*allow_expansion_p=*/true,
25540 /*non_constant_p=*/NULL);
25541 if (vec == NULL)
25542 arguments = error_mark_node;
25543 else
25545 arguments = build_tree_list_vec (vec);
25546 release_tree_vector (vec);
25549 if (arguments == error_mark_node)
25550 attribute = error_mark_node;
25551 else
25552 TREE_VALUE (attribute) = arguments;
25555 return attribute;
25558 /* Check that the attribute ATTRIBUTE appears at most once in the
25559 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25560 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25561 isn't implemented yet in GCC. */
25563 static void
25564 cp_parser_check_std_attribute (tree attributes, tree attribute)
25566 if (attributes)
25568 tree name = get_attribute_name (attribute);
25569 if (is_attribute_p ("noreturn", name)
25570 && lookup_attribute ("noreturn", attributes))
25571 error ("attribute %<noreturn%> can appear at most once "
25572 "in an attribute-list");
25573 else if (is_attribute_p ("deprecated", name)
25574 && lookup_attribute ("deprecated", attributes))
25575 error ("attribute %<deprecated%> can appear at most once "
25576 "in an attribute-list");
25580 /* Parse a list of standard C++-11 attributes.
25582 attribute-list:
25583 attribute [opt]
25584 attribute-list , attribute[opt]
25585 attribute ...
25586 attribute-list , attribute ...
25589 static tree
25590 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25592 tree attributes = NULL_TREE, attribute = NULL_TREE;
25593 cp_token *token = NULL;
25595 while (true)
25597 attribute = cp_parser_std_attribute (parser, attr_ns);
25598 if (attribute == error_mark_node)
25599 break;
25600 if (attribute != NULL_TREE)
25602 cp_parser_check_std_attribute (attributes, attribute);
25603 TREE_CHAIN (attribute) = attributes;
25604 attributes = attribute;
25606 token = cp_lexer_peek_token (parser->lexer);
25607 if (token->type == CPP_ELLIPSIS)
25609 cp_lexer_consume_token (parser->lexer);
25610 if (attribute == NULL_TREE)
25611 error_at (token->location,
25612 "expected attribute before %<...%>");
25613 else
25615 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25616 if (pack == error_mark_node)
25617 return error_mark_node;
25618 TREE_VALUE (attribute) = pack;
25620 token = cp_lexer_peek_token (parser->lexer);
25622 if (token->type != CPP_COMMA)
25623 break;
25624 cp_lexer_consume_token (parser->lexer);
25626 attributes = nreverse (attributes);
25627 return attributes;
25630 /* Parse a standard C++-11 attribute specifier.
25632 attribute-specifier:
25633 [ [ attribute-using-prefix [opt] attribute-list ] ]
25634 alignment-specifier
25636 attribute-using-prefix:
25637 using attribute-namespace :
25639 alignment-specifier:
25640 alignas ( type-id ... [opt] )
25641 alignas ( alignment-expression ... [opt] ). */
25643 static tree
25644 cp_parser_std_attribute_spec (cp_parser *parser)
25646 tree attributes = NULL_TREE;
25647 cp_token *token = cp_lexer_peek_token (parser->lexer);
25649 if (token->type == CPP_OPEN_SQUARE
25650 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25652 tree attr_ns = NULL_TREE;
25654 cp_lexer_consume_token (parser->lexer);
25655 cp_lexer_consume_token (parser->lexer);
25657 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25659 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25660 if (token->type == CPP_NAME)
25661 attr_ns = token->u.value;
25662 else if (token->type == CPP_KEYWORD)
25663 attr_ns = ridpointers[(int) token->keyword];
25664 else if (token->flags & NAMED_OP)
25665 attr_ns = get_identifier (cpp_type2name (token->type,
25666 token->flags));
25667 if (attr_ns
25668 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25670 if (cxx_dialect < cxx17
25671 && !in_system_header_at (input_location))
25672 pedwarn (input_location, 0,
25673 "attribute using prefix only available "
25674 "with -std=c++17 or -std=gnu++17");
25676 cp_lexer_consume_token (parser->lexer);
25677 cp_lexer_consume_token (parser->lexer);
25678 cp_lexer_consume_token (parser->lexer);
25680 else
25681 attr_ns = NULL_TREE;
25684 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25686 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25687 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25688 cp_parser_skip_to_end_of_statement (parser);
25689 else
25690 /* Warn about parsing c++11 attribute in non-c++11 mode, only
25691 when we are sure that we have actually parsed them. */
25692 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25694 else
25696 tree alignas_expr;
25698 /* Look for an alignment-specifier. */
25700 token = cp_lexer_peek_token (parser->lexer);
25702 if (token->type != CPP_KEYWORD
25703 || token->keyword != RID_ALIGNAS)
25704 return NULL_TREE;
25706 cp_lexer_consume_token (parser->lexer);
25707 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25709 matching_parens parens;
25710 if (!parens.require_open (parser))
25711 return error_mark_node;
25713 cp_parser_parse_tentatively (parser);
25714 alignas_expr = cp_parser_type_id (parser);
25716 if (!cp_parser_parse_definitely (parser))
25718 alignas_expr = cp_parser_assignment_expression (parser);
25719 if (alignas_expr == error_mark_node)
25720 cp_parser_skip_to_end_of_statement (parser);
25721 if (alignas_expr == NULL_TREE
25722 || alignas_expr == error_mark_node)
25723 return alignas_expr;
25726 alignas_expr = cxx_alignas_expr (alignas_expr);
25727 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25729 /* Handle alignas (pack...). */
25730 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25732 cp_lexer_consume_token (parser->lexer);
25733 alignas_expr = make_pack_expansion (alignas_expr);
25736 /* Something went wrong, so don't build the attribute. */
25737 if (alignas_expr == error_mark_node)
25738 return error_mark_node;
25740 if (!parens.require_close (parser))
25741 return error_mark_node;
25743 /* Build the C++-11 representation of an 'aligned'
25744 attribute. */
25745 attributes
25746 = build_tree_list (build_tree_list (gnu_identifier,
25747 aligned_identifier), alignas_expr);
25750 return attributes;
25753 /* Parse a standard C++-11 attribute-specifier-seq.
25755 attribute-specifier-seq:
25756 attribute-specifier-seq [opt] attribute-specifier
25759 static tree
25760 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25762 tree attr_specs = NULL_TREE;
25763 tree attr_last = NULL_TREE;
25765 while (true)
25767 tree attr_spec = cp_parser_std_attribute_spec (parser);
25768 if (attr_spec == NULL_TREE)
25769 break;
25770 if (attr_spec == error_mark_node)
25771 return error_mark_node;
25773 if (attr_last)
25774 TREE_CHAIN (attr_last) = attr_spec;
25775 else
25776 attr_specs = attr_last = attr_spec;
25777 attr_last = tree_last (attr_last);
25780 return attr_specs;
25783 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25784 return index of the first token after balanced-token, or N on failure. */
25786 static size_t
25787 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25789 size_t orig_n = n;
25790 int nparens = 0, nbraces = 0, nsquares = 0;
25792 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25794 case CPP_EOF:
25795 case CPP_PRAGMA_EOL:
25796 /* Ran out of tokens. */
25797 return orig_n;
25798 case CPP_OPEN_PAREN:
25799 ++nparens;
25800 break;
25801 case CPP_OPEN_BRACE:
25802 ++nbraces;
25803 break;
25804 case CPP_OPEN_SQUARE:
25805 ++nsquares;
25806 break;
25807 case CPP_CLOSE_PAREN:
25808 --nparens;
25809 break;
25810 case CPP_CLOSE_BRACE:
25811 --nbraces;
25812 break;
25813 case CPP_CLOSE_SQUARE:
25814 --nsquares;
25815 break;
25816 default:
25817 break;
25819 while (nparens || nbraces || nsquares);
25820 return n;
25823 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25824 return index of the first token after the GNU attribute tokens, or N on
25825 failure. */
25827 static size_t
25828 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25830 while (true)
25832 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25833 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25834 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25835 break;
25837 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25838 if (n2 == n + 2)
25839 break;
25840 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25841 break;
25842 n = n2 + 1;
25844 return n;
25847 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25848 next token), return index of the first token after the standard C++11
25849 attribute tokens, or N on failure. */
25851 static size_t
25852 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25854 while (true)
25856 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25857 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25859 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25860 if (n2 == n + 1)
25861 break;
25862 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25863 break;
25864 n = n2 + 1;
25866 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25867 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25869 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25870 if (n2 == n + 1)
25871 break;
25872 n = n2;
25874 else
25875 break;
25877 return n;
25880 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25881 as the next token), return index of the first token after the attribute
25882 tokens, or N on failure. */
25884 static size_t
25885 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25887 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25888 return cp_parser_skip_gnu_attributes_opt (parser, n);
25889 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25892 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25893 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25894 current value of the PEDANTIC flag, regardless of whether or not
25895 the `__extension__' keyword is present. The caller is responsible
25896 for restoring the value of the PEDANTIC flag. */
25898 static bool
25899 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25901 /* Save the old value of the PEDANTIC flag. */
25902 *saved_pedantic = pedantic;
25904 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25906 /* Consume the `__extension__' token. */
25907 cp_lexer_consume_token (parser->lexer);
25908 /* We're not being pedantic while the `__extension__' keyword is
25909 in effect. */
25910 pedantic = 0;
25912 return true;
25915 return false;
25918 /* Parse a label declaration.
25920 label-declaration:
25921 __label__ label-declarator-seq ;
25923 label-declarator-seq:
25924 identifier , label-declarator-seq
25925 identifier */
25927 static void
25928 cp_parser_label_declaration (cp_parser* parser)
25930 /* Look for the `__label__' keyword. */
25931 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25933 while (true)
25935 tree identifier;
25937 /* Look for an identifier. */
25938 identifier = cp_parser_identifier (parser);
25939 /* If we failed, stop. */
25940 if (identifier == error_mark_node)
25941 break;
25942 /* Declare it as a label. */
25943 finish_label_decl (identifier);
25944 /* If the next token is a `;', stop. */
25945 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25946 break;
25947 /* Look for the `,' separating the label declarations. */
25948 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25951 /* Look for the final `;'. */
25952 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25955 // -------------------------------------------------------------------------- //
25956 // Requires Clause
25958 // Parse a requires clause.
25960 // requires-clause:
25961 // 'requires' logical-or-expression
25963 // The required logical-or-expression must be a constant expression. Note
25964 // that we don't check that the expression is constepxr here. We defer until
25965 // we analyze constraints and then, we only check atomic constraints.
25966 static tree
25967 cp_parser_requires_clause (cp_parser *parser)
25969 // Parse the requires clause so that it is not automatically folded.
25970 ++processing_template_decl;
25971 tree expr = cp_parser_binary_expression (parser, false, false,
25972 PREC_NOT_OPERATOR, NULL);
25973 if (check_for_bare_parameter_packs (expr))
25974 expr = error_mark_node;
25975 --processing_template_decl;
25976 return expr;
25979 // Optionally parse a requires clause:
25980 static tree
25981 cp_parser_requires_clause_opt (cp_parser *parser)
25983 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25984 if (tok->keyword != RID_REQUIRES)
25986 if (!flag_concepts && tok->type == CPP_NAME
25987 && tok->u.value == ridpointers[RID_REQUIRES])
25989 error_at (cp_lexer_peek_token (parser->lexer)->location,
25990 "%<requires%> only available with -fconcepts");
25991 /* Parse and discard the requires-clause. */
25992 cp_lexer_consume_token (parser->lexer);
25993 cp_parser_requires_clause (parser);
25995 return NULL_TREE;
25997 cp_lexer_consume_token (parser->lexer);
25998 return cp_parser_requires_clause (parser);
26002 /*---------------------------------------------------------------------------
26003 Requires expressions
26004 ---------------------------------------------------------------------------*/
26006 /* Parse a requires expression
26008 requirement-expression:
26009 'requires' requirement-parameter-list [opt] requirement-body */
26010 static tree
26011 cp_parser_requires_expression (cp_parser *parser)
26013 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26014 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26016 /* A requires-expression shall appear only within a concept
26017 definition or a requires-clause.
26019 TODO: Implement this diagnostic correctly. */
26020 if (!processing_template_decl)
26022 error_at (loc, "a requires expression cannot appear outside a template");
26023 cp_parser_skip_to_end_of_statement (parser);
26024 return error_mark_node;
26027 tree parms, reqs;
26029 /* Local parameters are delared as variables within the scope
26030 of the expression. They are not visible past the end of
26031 the expression. Expressions within the requires-expression
26032 are unevaluated. */
26033 struct scope_sentinel
26035 scope_sentinel ()
26037 ++cp_unevaluated_operand;
26038 begin_scope (sk_block, NULL_TREE);
26041 ~scope_sentinel ()
26043 pop_bindings_and_leave_scope ();
26044 --cp_unevaluated_operand;
26046 } s;
26048 /* Parse the optional parameter list. */
26049 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26051 parms = cp_parser_requirement_parameter_list (parser);
26052 if (parms == error_mark_node)
26053 return error_mark_node;
26055 else
26056 parms = NULL_TREE;
26058 /* Parse the requirement body. */
26059 reqs = cp_parser_requirement_body (parser);
26060 if (reqs == error_mark_node)
26061 return error_mark_node;
26064 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26065 the parm chain. */
26066 grokparms (parms, &parms);
26067 return finish_requires_expr (parms, reqs);
26070 /* Parse a parameterized requirement.
26072 requirement-parameter-list:
26073 '(' parameter-declaration-clause ')' */
26074 static tree
26075 cp_parser_requirement_parameter_list (cp_parser *parser)
26077 matching_parens parens;
26078 if (!parens.require_open (parser))
26079 return error_mark_node;
26081 tree parms = cp_parser_parameter_declaration_clause (parser);
26083 if (!parens.require_close (parser))
26084 return error_mark_node;
26086 return parms;
26089 /* Parse the body of a requirement.
26091 requirement-body:
26092 '{' requirement-list '}' */
26093 static tree
26094 cp_parser_requirement_body (cp_parser *parser)
26096 matching_braces braces;
26097 if (!braces.require_open (parser))
26098 return error_mark_node;
26100 tree reqs = cp_parser_requirement_list (parser);
26102 if (!braces.require_close (parser))
26103 return error_mark_node;
26105 return reqs;
26108 /* Parse a list of requirements.
26110 requirement-list:
26111 requirement
26112 requirement-list ';' requirement[opt] */
26113 static tree
26114 cp_parser_requirement_list (cp_parser *parser)
26116 tree result = NULL_TREE;
26117 while (true)
26119 tree req = cp_parser_requirement (parser);
26120 if (req == error_mark_node)
26121 return error_mark_node;
26123 result = tree_cons (NULL_TREE, req, result);
26125 /* If we see a semi-colon, consume it. */
26126 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26127 cp_lexer_consume_token (parser->lexer);
26129 /* Stop processing at the end of the list. */
26130 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26131 break;
26134 /* Reverse the order of requirements so they are analyzed in
26135 declaration order. */
26136 return nreverse (result);
26139 /* Parse a syntactic requirement or type requirement.
26141 requirement:
26142 simple-requirement
26143 compound-requirement
26144 type-requirement
26145 nested-requirement */
26146 static tree
26147 cp_parser_requirement (cp_parser *parser)
26149 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26150 return cp_parser_compound_requirement (parser);
26151 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26152 return cp_parser_type_requirement (parser);
26153 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26154 return cp_parser_nested_requirement (parser);
26155 else
26156 return cp_parser_simple_requirement (parser);
26159 /* Parse a simple requirement.
26161 simple-requirement:
26162 expression ';' */
26163 static tree
26164 cp_parser_simple_requirement (cp_parser *parser)
26166 tree expr = cp_parser_expression (parser, NULL, false, false);
26167 if (!expr || expr == error_mark_node)
26168 return error_mark_node;
26170 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26171 return error_mark_node;
26173 return finish_simple_requirement (expr);
26176 /* Parse a type requirement
26178 type-requirement
26179 nested-name-specifier [opt] required-type-name ';'
26181 required-type-name:
26182 type-name
26183 'template' [opt] simple-template-id */
26184 static tree
26185 cp_parser_type_requirement (cp_parser *parser)
26187 cp_lexer_consume_token (parser->lexer);
26189 // Save the scope before parsing name specifiers.
26190 tree saved_scope = parser->scope;
26191 tree saved_object_scope = parser->object_scope;
26192 tree saved_qualifying_scope = parser->qualifying_scope;
26193 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26194 cp_parser_nested_name_specifier_opt (parser,
26195 /*typename_keyword_p=*/true,
26196 /*check_dependency_p=*/false,
26197 /*type_p=*/true,
26198 /*is_declaration=*/false);
26200 tree type;
26201 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26203 cp_lexer_consume_token (parser->lexer);
26204 type = cp_parser_template_id (parser,
26205 /*template_keyword_p=*/true,
26206 /*check_dependency=*/false,
26207 /*tag_type=*/none_type,
26208 /*is_declaration=*/false);
26209 type = make_typename_type (parser->scope, type, typename_type,
26210 /*complain=*/tf_error);
26212 else
26213 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26215 if (TREE_CODE (type) == TYPE_DECL)
26216 type = TREE_TYPE (type);
26218 parser->scope = saved_scope;
26219 parser->object_scope = saved_object_scope;
26220 parser->qualifying_scope = saved_qualifying_scope;
26222 if (type == error_mark_node)
26223 cp_parser_skip_to_end_of_statement (parser);
26225 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26226 return error_mark_node;
26227 if (type == error_mark_node)
26228 return error_mark_node;
26230 return finish_type_requirement (type);
26233 /* Parse a compound requirement
26235 compound-requirement:
26236 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26237 static tree
26238 cp_parser_compound_requirement (cp_parser *parser)
26240 /* Parse an expression enclosed in '{ }'s. */
26241 matching_braces braces;
26242 if (!braces.require_open (parser))
26243 return error_mark_node;
26245 tree expr = cp_parser_expression (parser, NULL, false, false);
26246 if (!expr || expr == error_mark_node)
26247 return error_mark_node;
26249 if (!braces.require_close (parser))
26250 return error_mark_node;
26252 /* Parse the optional noexcept. */
26253 bool noexcept_p = false;
26254 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26256 cp_lexer_consume_token (parser->lexer);
26257 noexcept_p = true;
26260 /* Parse the optional trailing return type. */
26261 tree type = NULL_TREE;
26262 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26264 cp_lexer_consume_token (parser->lexer);
26265 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26266 parser->in_result_type_constraint_p = true;
26267 type = cp_parser_trailing_type_id (parser);
26268 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26269 if (type == error_mark_node)
26270 return error_mark_node;
26273 return finish_compound_requirement (expr, type, noexcept_p);
26276 /* Parse a nested requirement. This is the same as a requires clause.
26278 nested-requirement:
26279 requires-clause */
26280 static tree
26281 cp_parser_nested_requirement (cp_parser *parser)
26283 cp_lexer_consume_token (parser->lexer);
26284 tree req = cp_parser_requires_clause (parser);
26285 if (req == error_mark_node)
26286 return error_mark_node;
26287 return finish_nested_requirement (req);
26290 /* Support Functions */
26292 /* Return the appropriate prefer_type argument for lookup_name_real based on
26293 tag_type and template_mem_access. */
26295 static inline int
26296 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26298 /* DR 141: When looking in the current enclosing context for a template-name
26299 after -> or ., only consider class templates. */
26300 if (template_mem_access)
26301 return 2;
26302 switch (tag_type)
26304 case none_type: return 0; // No preference.
26305 case scope_type: return 1; // Type or namespace.
26306 default: return 2; // Type only.
26310 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26311 NAME should have one of the representations used for an
26312 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26313 is returned. If PARSER->SCOPE is a dependent type, then a
26314 SCOPE_REF is returned.
26316 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26317 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26318 was formed. Abstractly, such entities should not be passed to this
26319 function, because they do not need to be looked up, but it is
26320 simpler to check for this special case here, rather than at the
26321 call-sites.
26323 In cases not explicitly covered above, this function returns a
26324 DECL, OVERLOAD, or baselink representing the result of the lookup.
26325 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26326 is returned.
26328 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26329 (e.g., "struct") that was used. In that case bindings that do not
26330 refer to types are ignored.
26332 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26333 ignored.
26335 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26336 are ignored.
26338 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26339 types.
26341 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26342 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26343 NULL_TREE otherwise. */
26345 static cp_expr
26346 cp_parser_lookup_name (cp_parser *parser, tree name,
26347 enum tag_types tag_type,
26348 bool is_template,
26349 bool is_namespace,
26350 bool check_dependency,
26351 tree *ambiguous_decls,
26352 location_t name_location)
26354 tree decl;
26355 tree object_type = parser->context->object_type;
26357 /* Assume that the lookup will be unambiguous. */
26358 if (ambiguous_decls)
26359 *ambiguous_decls = NULL_TREE;
26361 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26362 no longer valid. Note that if we are parsing tentatively, and
26363 the parse fails, OBJECT_TYPE will be automatically restored. */
26364 parser->context->object_type = NULL_TREE;
26366 if (name == error_mark_node)
26367 return error_mark_node;
26369 /* A template-id has already been resolved; there is no lookup to
26370 do. */
26371 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26372 return name;
26373 if (BASELINK_P (name))
26375 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26376 == TEMPLATE_ID_EXPR);
26377 return name;
26380 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26381 it should already have been checked to make sure that the name
26382 used matches the type being destroyed. */
26383 if (TREE_CODE (name) == BIT_NOT_EXPR)
26385 tree type;
26387 /* Figure out to which type this destructor applies. */
26388 if (parser->scope)
26389 type = parser->scope;
26390 else if (object_type)
26391 type = object_type;
26392 else
26393 type = current_class_type;
26394 /* If that's not a class type, there is no destructor. */
26395 if (!type || !CLASS_TYPE_P (type))
26396 return error_mark_node;
26398 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26399 lazily_declare_fn (sfk_destructor, type);
26401 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26402 return dtor;
26404 return error_mark_node;
26407 /* By this point, the NAME should be an ordinary identifier. If
26408 the id-expression was a qualified name, the qualifying scope is
26409 stored in PARSER->SCOPE at this point. */
26410 gcc_assert (identifier_p (name));
26412 /* Perform the lookup. */
26413 if (parser->scope)
26415 bool dependent_p;
26417 if (parser->scope == error_mark_node)
26418 return error_mark_node;
26420 /* If the SCOPE is dependent, the lookup must be deferred until
26421 the template is instantiated -- unless we are explicitly
26422 looking up names in uninstantiated templates. Even then, we
26423 cannot look up the name if the scope is not a class type; it
26424 might, for example, be a template type parameter. */
26425 dependent_p = (TYPE_P (parser->scope)
26426 && dependent_scope_p (parser->scope));
26427 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26428 && dependent_p)
26429 /* Defer lookup. */
26430 decl = error_mark_node;
26431 else
26433 tree pushed_scope = NULL_TREE;
26435 /* If PARSER->SCOPE is a dependent type, then it must be a
26436 class type, and we must not be checking dependencies;
26437 otherwise, we would have processed this lookup above. So
26438 that PARSER->SCOPE is not considered a dependent base by
26439 lookup_member, we must enter the scope here. */
26440 if (dependent_p)
26441 pushed_scope = push_scope (parser->scope);
26443 /* If the PARSER->SCOPE is a template specialization, it
26444 may be instantiated during name lookup. In that case,
26445 errors may be issued. Even if we rollback the current
26446 tentative parse, those errors are valid. */
26447 decl = lookup_qualified_name (parser->scope, name,
26448 prefer_type_arg (tag_type),
26449 /*complain=*/true);
26451 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26452 lookup result and the nested-name-specifier nominates a class C:
26453 * if the name specified after the nested-name-specifier, when
26454 looked up in C, is the injected-class-name of C (Clause 9), or
26455 * if the name specified after the nested-name-specifier is the
26456 same as the identifier or the simple-template-id's template-
26457 name in the last component of the nested-name-specifier,
26458 the name is instead considered to name the constructor of
26459 class C. [ Note: for example, the constructor is not an
26460 acceptable lookup result in an elaborated-type-specifier so
26461 the constructor would not be used in place of the
26462 injected-class-name. --end note ] Such a constructor name
26463 shall be used only in the declarator-id of a declaration that
26464 names a constructor or in a using-declaration. */
26465 if (tag_type == none_type
26466 && DECL_SELF_REFERENCE_P (decl)
26467 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26468 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26469 prefer_type_arg (tag_type),
26470 /*complain=*/true);
26472 /* If we have a single function from a using decl, pull it out. */
26473 if (TREE_CODE (decl) == OVERLOAD
26474 && !really_overloaded_fn (decl))
26475 decl = OVL_FUNCTION (decl);
26477 if (pushed_scope)
26478 pop_scope (pushed_scope);
26481 /* If the scope is a dependent type and either we deferred lookup or
26482 we did lookup but didn't find the name, rememeber the name. */
26483 if (decl == error_mark_node && TYPE_P (parser->scope)
26484 && dependent_type_p (parser->scope))
26486 if (tag_type)
26488 tree type;
26490 /* The resolution to Core Issue 180 says that `struct
26491 A::B' should be considered a type-name, even if `A'
26492 is dependent. */
26493 type = make_typename_type (parser->scope, name, tag_type,
26494 /*complain=*/tf_error);
26495 if (type != error_mark_node)
26496 decl = TYPE_NAME (type);
26498 else if (is_template
26499 && (cp_parser_next_token_ends_template_argument_p (parser)
26500 || cp_lexer_next_token_is (parser->lexer,
26501 CPP_CLOSE_PAREN)))
26502 decl = make_unbound_class_template (parser->scope,
26503 name, NULL_TREE,
26504 /*complain=*/tf_error);
26505 else
26506 decl = build_qualified_name (/*type=*/NULL_TREE,
26507 parser->scope, name,
26508 is_template);
26510 parser->qualifying_scope = parser->scope;
26511 parser->object_scope = NULL_TREE;
26513 else if (object_type)
26515 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26516 OBJECT_TYPE is not a class. */
26517 if (CLASS_TYPE_P (object_type))
26518 /* If the OBJECT_TYPE is a template specialization, it may
26519 be instantiated during name lookup. In that case, errors
26520 may be issued. Even if we rollback the current tentative
26521 parse, those errors are valid. */
26522 decl = lookup_member (object_type,
26523 name,
26524 /*protect=*/0,
26525 prefer_type_arg (tag_type),
26526 tf_warning_or_error);
26527 else
26528 decl = NULL_TREE;
26530 if (!decl)
26531 /* Look it up in the enclosing context. DR 141: When looking for a
26532 template-name after -> or ., only consider class templates. */
26533 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26534 /*nonclass=*/0,
26535 /*block_p=*/true, is_namespace, 0);
26536 if (object_type == unknown_type_node)
26537 /* The object is type-dependent, so we can't look anything up; we used
26538 this to get the DR 141 behavior. */
26539 object_type = NULL_TREE;
26540 parser->object_scope = object_type;
26541 parser->qualifying_scope = NULL_TREE;
26543 else
26545 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26546 /*nonclass=*/0,
26547 /*block_p=*/true, is_namespace, 0);
26548 parser->qualifying_scope = NULL_TREE;
26549 parser->object_scope = NULL_TREE;
26552 /* If the lookup failed, let our caller know. */
26553 if (!decl || decl == error_mark_node)
26554 return error_mark_node;
26556 /* Pull out the template from an injected-class-name (or multiple). */
26557 if (is_template)
26558 decl = maybe_get_template_decl_from_type_decl (decl);
26560 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26561 if (TREE_CODE (decl) == TREE_LIST)
26563 if (ambiguous_decls)
26564 *ambiguous_decls = decl;
26565 /* The error message we have to print is too complicated for
26566 cp_parser_error, so we incorporate its actions directly. */
26567 if (!cp_parser_simulate_error (parser))
26569 error_at (name_location, "reference to %qD is ambiguous",
26570 name);
26571 print_candidates (decl);
26573 return error_mark_node;
26576 gcc_assert (DECL_P (decl)
26577 || TREE_CODE (decl) == OVERLOAD
26578 || TREE_CODE (decl) == SCOPE_REF
26579 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26580 || BASELINK_P (decl));
26582 /* If we have resolved the name of a member declaration, check to
26583 see if the declaration is accessible. When the name resolves to
26584 set of overloaded functions, accessibility is checked when
26585 overload resolution is done.
26587 During an explicit instantiation, access is not checked at all,
26588 as per [temp.explicit]. */
26589 if (DECL_P (decl))
26590 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26592 maybe_record_typedef_use (decl);
26594 return cp_expr (decl, name_location);
26597 /* Like cp_parser_lookup_name, but for use in the typical case where
26598 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26599 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26601 static tree
26602 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26604 return cp_parser_lookup_name (parser, name,
26605 none_type,
26606 /*is_template=*/false,
26607 /*is_namespace=*/false,
26608 /*check_dependency=*/true,
26609 /*ambiguous_decls=*/NULL,
26610 location);
26613 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26614 the current context, return the TYPE_DECL. If TAG_NAME_P is
26615 true, the DECL indicates the class being defined in a class-head,
26616 or declared in an elaborated-type-specifier.
26618 Otherwise, return DECL. */
26620 static tree
26621 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26623 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26624 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26626 struct A {
26627 template <typename T> struct B;
26630 template <typename T> struct A::B {};
26632 Similarly, in an elaborated-type-specifier:
26634 namespace N { struct X{}; }
26636 struct A {
26637 template <typename T> friend struct N::X;
26640 However, if the DECL refers to a class type, and we are in
26641 the scope of the class, then the name lookup automatically
26642 finds the TYPE_DECL created by build_self_reference rather
26643 than a TEMPLATE_DECL. For example, in:
26645 template <class T> struct S {
26646 S s;
26649 there is no need to handle such case. */
26651 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26652 return DECL_TEMPLATE_RESULT (decl);
26654 return decl;
26657 /* If too many, or too few, template-parameter lists apply to the
26658 declarator, issue an error message. Returns TRUE if all went well,
26659 and FALSE otherwise. */
26661 static bool
26662 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26663 cp_declarator *declarator,
26664 location_t declarator_location)
26666 switch (declarator->kind)
26668 case cdk_id:
26670 unsigned num_templates = 0;
26671 tree scope = declarator->u.id.qualifying_scope;
26672 bool template_id_p = false;
26674 if (scope)
26675 num_templates = num_template_headers_for_class (scope);
26676 else if (TREE_CODE (declarator->u.id.unqualified_name)
26677 == TEMPLATE_ID_EXPR)
26679 /* If the DECLARATOR has the form `X<y>' then it uses one
26680 additional level of template parameters. */
26681 ++num_templates;
26682 template_id_p = true;
26685 return cp_parser_check_template_parameters
26686 (parser, num_templates, template_id_p, declarator_location,
26687 declarator);
26690 case cdk_function:
26691 case cdk_array:
26692 case cdk_pointer:
26693 case cdk_reference:
26694 case cdk_ptrmem:
26695 return (cp_parser_check_declarator_template_parameters
26696 (parser, declarator->declarator, declarator_location));
26698 case cdk_decomp:
26699 case cdk_error:
26700 return true;
26702 default:
26703 gcc_unreachable ();
26705 return false;
26708 /* NUM_TEMPLATES were used in the current declaration. If that is
26709 invalid, return FALSE and issue an error messages. Otherwise,
26710 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26711 declarator and we can print more accurate diagnostics. */
26713 static bool
26714 cp_parser_check_template_parameters (cp_parser* parser,
26715 unsigned num_templates,
26716 bool template_id_p,
26717 location_t location,
26718 cp_declarator *declarator)
26720 /* If there are the same number of template classes and parameter
26721 lists, that's OK. */
26722 if (parser->num_template_parameter_lists == num_templates)
26723 return true;
26724 /* If there are more, but only one more, and the name ends in an identifier,
26725 then we are declaring a primary template. That's OK too. */
26726 if (!template_id_p
26727 && parser->num_template_parameter_lists == num_templates + 1)
26728 return true;
26729 /* If there are more template classes than parameter lists, we have
26730 something like:
26732 template <class T> void S<T>::R<T>::f (); */
26733 if (parser->num_template_parameter_lists < num_templates)
26735 if (declarator && !current_function_decl)
26736 error_at (location, "specializing member %<%T::%E%> "
26737 "requires %<template<>%> syntax",
26738 declarator->u.id.qualifying_scope,
26739 declarator->u.id.unqualified_name);
26740 else if (declarator)
26741 error_at (location, "invalid declaration of %<%T::%E%>",
26742 declarator->u.id.qualifying_scope,
26743 declarator->u.id.unqualified_name);
26744 else
26745 error_at (location, "too few template-parameter-lists");
26746 return false;
26748 /* Otherwise, there are too many template parameter lists. We have
26749 something like:
26751 template <class T> template <class U> void S::f(); */
26752 error_at (location, "too many template-parameter-lists");
26753 return false;
26756 /* Parse an optional `::' token indicating that the following name is
26757 from the global namespace. If so, PARSER->SCOPE is set to the
26758 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26759 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26760 Returns the new value of PARSER->SCOPE, if the `::' token is
26761 present, and NULL_TREE otherwise. */
26763 static tree
26764 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26766 cp_token *token;
26768 /* Peek at the next token. */
26769 token = cp_lexer_peek_token (parser->lexer);
26770 /* If we're looking at a `::' token then we're starting from the
26771 global namespace, not our current location. */
26772 if (token->type == CPP_SCOPE)
26774 /* Consume the `::' token. */
26775 cp_lexer_consume_token (parser->lexer);
26776 /* Set the SCOPE so that we know where to start the lookup. */
26777 parser->scope = global_namespace;
26778 parser->qualifying_scope = global_namespace;
26779 parser->object_scope = NULL_TREE;
26781 return parser->scope;
26783 else if (!current_scope_valid_p)
26785 parser->scope = NULL_TREE;
26786 parser->qualifying_scope = NULL_TREE;
26787 parser->object_scope = NULL_TREE;
26790 return NULL_TREE;
26793 /* Returns TRUE if the upcoming token sequence is the start of a
26794 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26795 declarator is preceded by the `friend' specifier. */
26797 static bool
26798 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26800 bool constructor_p;
26801 bool outside_class_specifier_p;
26802 tree nested_name_specifier;
26803 cp_token *next_token;
26805 /* The common case is that this is not a constructor declarator, so
26806 try to avoid doing lots of work if at all possible. It's not
26807 valid declare a constructor at function scope. */
26808 if (parser->in_function_body)
26809 return false;
26810 /* And only certain tokens can begin a constructor declarator. */
26811 next_token = cp_lexer_peek_token (parser->lexer);
26812 if (next_token->type != CPP_NAME
26813 && next_token->type != CPP_SCOPE
26814 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26815 && next_token->type != CPP_TEMPLATE_ID)
26816 return false;
26818 /* Parse tentatively; we are going to roll back all of the tokens
26819 consumed here. */
26820 cp_parser_parse_tentatively (parser);
26821 /* Assume that we are looking at a constructor declarator. */
26822 constructor_p = true;
26824 /* Look for the optional `::' operator. */
26825 cp_parser_global_scope_opt (parser,
26826 /*current_scope_valid_p=*/false);
26827 /* Look for the nested-name-specifier. */
26828 nested_name_specifier
26829 = (cp_parser_nested_name_specifier_opt (parser,
26830 /*typename_keyword_p=*/false,
26831 /*check_dependency_p=*/false,
26832 /*type_p=*/false,
26833 /*is_declaration=*/false));
26835 outside_class_specifier_p = (!at_class_scope_p ()
26836 || !TYPE_BEING_DEFINED (current_class_type)
26837 || friend_p);
26839 /* Outside of a class-specifier, there must be a
26840 nested-name-specifier. Except in C++17 mode, where we
26841 might be declaring a guiding declaration. */
26842 if (!nested_name_specifier && outside_class_specifier_p
26843 && cxx_dialect < cxx17)
26844 constructor_p = false;
26845 else if (nested_name_specifier == error_mark_node)
26846 constructor_p = false;
26848 /* If we have a class scope, this is easy; DR 147 says that S::S always
26849 names the constructor, and no other qualified name could. */
26850 if (constructor_p && nested_name_specifier
26851 && CLASS_TYPE_P (nested_name_specifier))
26853 tree id = cp_parser_unqualified_id (parser,
26854 /*template_keyword_p=*/false,
26855 /*check_dependency_p=*/false,
26856 /*declarator_p=*/true,
26857 /*optional_p=*/false);
26858 if (is_overloaded_fn (id))
26859 id = DECL_NAME (get_first_fn (id));
26860 if (!constructor_name_p (id, nested_name_specifier))
26861 constructor_p = false;
26863 /* If we still think that this might be a constructor-declarator,
26864 look for a class-name. */
26865 else if (constructor_p)
26867 /* If we have:
26869 template <typename T> struct S {
26870 S();
26873 we must recognize that the nested `S' names a class. */
26874 if (cxx_dialect >= cxx17)
26875 cp_parser_parse_tentatively (parser);
26877 tree type_decl;
26878 type_decl = cp_parser_class_name (parser,
26879 /*typename_keyword_p=*/false,
26880 /*template_keyword_p=*/false,
26881 none_type,
26882 /*check_dependency_p=*/false,
26883 /*class_head_p=*/false,
26884 /*is_declaration=*/false);
26886 if (cxx_dialect >= cxx17
26887 && !cp_parser_parse_definitely (parser))
26889 type_decl = NULL_TREE;
26890 tree tmpl = cp_parser_template_name (parser,
26891 /*template_keyword*/false,
26892 /*check_dependency_p*/false,
26893 /*is_declaration*/false,
26894 none_type,
26895 /*is_identifier*/NULL);
26896 if (DECL_CLASS_TEMPLATE_P (tmpl)
26897 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26898 /* It's a deduction guide, return true. */;
26899 else
26900 cp_parser_simulate_error (parser);
26903 /* If there was no class-name, then this is not a constructor.
26904 Otherwise, if we are in a class-specifier and we aren't
26905 handling a friend declaration, check that its type matches
26906 current_class_type (c++/38313). Note: error_mark_node
26907 is left alone for error recovery purposes. */
26908 constructor_p = (!cp_parser_error_occurred (parser)
26909 && (outside_class_specifier_p
26910 || type_decl == NULL_TREE
26911 || type_decl == error_mark_node
26912 || same_type_p (current_class_type,
26913 TREE_TYPE (type_decl))));
26915 /* If we're still considering a constructor, we have to see a `(',
26916 to begin the parameter-declaration-clause, followed by either a
26917 `)', an `...', or a decl-specifier. We need to check for a
26918 type-specifier to avoid being fooled into thinking that:
26920 S (f) (int);
26922 is a constructor. (It is actually a function named `f' that
26923 takes one parameter (of type `int') and returns a value of type
26924 `S'. */
26925 if (constructor_p
26926 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26927 constructor_p = false;
26929 if (constructor_p
26930 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26931 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26932 /* A parameter declaration begins with a decl-specifier,
26933 which is either the "attribute" keyword, a storage class
26934 specifier, or (usually) a type-specifier. */
26935 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26937 tree type;
26938 tree pushed_scope = NULL_TREE;
26939 unsigned saved_num_template_parameter_lists;
26941 /* Names appearing in the type-specifier should be looked up
26942 in the scope of the class. */
26943 if (current_class_type)
26944 type = NULL_TREE;
26945 else if (type_decl)
26947 type = TREE_TYPE (type_decl);
26948 if (TREE_CODE (type) == TYPENAME_TYPE)
26950 type = resolve_typename_type (type,
26951 /*only_current_p=*/false);
26952 if (TREE_CODE (type) == TYPENAME_TYPE)
26954 cp_parser_abort_tentative_parse (parser);
26955 return false;
26958 pushed_scope = push_scope (type);
26961 /* Inside the constructor parameter list, surrounding
26962 template-parameter-lists do not apply. */
26963 saved_num_template_parameter_lists
26964 = parser->num_template_parameter_lists;
26965 parser->num_template_parameter_lists = 0;
26967 /* Look for the type-specifier. */
26968 cp_parser_type_specifier (parser,
26969 CP_PARSER_FLAGS_NONE,
26970 /*decl_specs=*/NULL,
26971 /*is_declarator=*/true,
26972 /*declares_class_or_enum=*/NULL,
26973 /*is_cv_qualifier=*/NULL);
26975 parser->num_template_parameter_lists
26976 = saved_num_template_parameter_lists;
26978 /* Leave the scope of the class. */
26979 if (pushed_scope)
26980 pop_scope (pushed_scope);
26982 constructor_p = !cp_parser_error_occurred (parser);
26986 /* We did not really want to consume any tokens. */
26987 cp_parser_abort_tentative_parse (parser);
26989 return constructor_p;
26992 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26993 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26994 they must be performed once we are in the scope of the function.
26996 Returns the function defined. */
26998 static tree
26999 cp_parser_function_definition_from_specifiers_and_declarator
27000 (cp_parser* parser,
27001 cp_decl_specifier_seq *decl_specifiers,
27002 tree attributes,
27003 const cp_declarator *declarator)
27005 tree fn;
27006 bool success_p;
27008 /* Begin the function-definition. */
27009 success_p = start_function (decl_specifiers, declarator, attributes);
27011 /* The things we're about to see are not directly qualified by any
27012 template headers we've seen thus far. */
27013 reset_specialization ();
27015 /* If there were names looked up in the decl-specifier-seq that we
27016 did not check, check them now. We must wait until we are in the
27017 scope of the function to perform the checks, since the function
27018 might be a friend. */
27019 perform_deferred_access_checks (tf_warning_or_error);
27021 if (success_p)
27023 cp_finalize_omp_declare_simd (parser, current_function_decl);
27024 parser->omp_declare_simd = NULL;
27025 cp_finalize_oacc_routine (parser, current_function_decl, true);
27026 parser->oacc_routine = NULL;
27029 if (!success_p)
27031 /* Skip the entire function. */
27032 cp_parser_skip_to_end_of_block_or_statement (parser);
27033 fn = error_mark_node;
27035 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27037 /* Seen already, skip it. An error message has already been output. */
27038 cp_parser_skip_to_end_of_block_or_statement (parser);
27039 fn = current_function_decl;
27040 current_function_decl = NULL_TREE;
27041 /* If this is a function from a class, pop the nested class. */
27042 if (current_class_name)
27043 pop_nested_class ();
27045 else
27047 timevar_id_t tv;
27048 if (DECL_DECLARED_INLINE_P (current_function_decl))
27049 tv = TV_PARSE_INLINE;
27050 else
27051 tv = TV_PARSE_FUNC;
27052 timevar_push (tv);
27053 fn = cp_parser_function_definition_after_declarator (parser,
27054 /*inline_p=*/false);
27055 timevar_pop (tv);
27058 return fn;
27061 /* Parse the part of a function-definition that follows the
27062 declarator. INLINE_P is TRUE iff this function is an inline
27063 function defined within a class-specifier.
27065 Returns the function defined. */
27067 static tree
27068 cp_parser_function_definition_after_declarator (cp_parser* parser,
27069 bool inline_p)
27071 tree fn;
27072 bool saved_in_unbraced_linkage_specification_p;
27073 bool saved_in_function_body;
27074 unsigned saved_num_template_parameter_lists;
27075 cp_token *token;
27076 bool fully_implicit_function_template_p
27077 = parser->fully_implicit_function_template_p;
27078 parser->fully_implicit_function_template_p = false;
27079 tree implicit_template_parms
27080 = parser->implicit_template_parms;
27081 parser->implicit_template_parms = 0;
27082 cp_binding_level* implicit_template_scope
27083 = parser->implicit_template_scope;
27084 parser->implicit_template_scope = 0;
27086 saved_in_function_body = parser->in_function_body;
27087 parser->in_function_body = true;
27088 /* If the next token is `return', then the code may be trying to
27089 make use of the "named return value" extension that G++ used to
27090 support. */
27091 token = cp_lexer_peek_token (parser->lexer);
27092 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27094 /* Consume the `return' keyword. */
27095 cp_lexer_consume_token (parser->lexer);
27096 /* Look for the identifier that indicates what value is to be
27097 returned. */
27098 cp_parser_identifier (parser);
27099 /* Issue an error message. */
27100 error_at (token->location,
27101 "named return values are no longer supported");
27102 /* Skip tokens until we reach the start of the function body. */
27103 while (true)
27105 cp_token *token = cp_lexer_peek_token (parser->lexer);
27106 if (token->type == CPP_OPEN_BRACE
27107 || token->type == CPP_EOF
27108 || token->type == CPP_PRAGMA_EOL)
27109 break;
27110 cp_lexer_consume_token (parser->lexer);
27113 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27114 anything declared inside `f'. */
27115 saved_in_unbraced_linkage_specification_p
27116 = parser->in_unbraced_linkage_specification_p;
27117 parser->in_unbraced_linkage_specification_p = false;
27118 /* Inside the function, surrounding template-parameter-lists do not
27119 apply. */
27120 saved_num_template_parameter_lists
27121 = parser->num_template_parameter_lists;
27122 parser->num_template_parameter_lists = 0;
27124 /* If the next token is `try', `__transaction_atomic', or
27125 `__transaction_relaxed`, then we are looking at either function-try-block
27126 or function-transaction-block. Note that all of these include the
27127 function-body. */
27128 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27129 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27130 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27131 RID_TRANSACTION_RELAXED))
27132 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27133 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27134 cp_parser_function_try_block (parser);
27135 else
27136 cp_parser_ctor_initializer_opt_and_function_body
27137 (parser, /*in_function_try_block=*/false);
27139 /* Finish the function. */
27140 fn = finish_function (inline_p);
27141 /* Generate code for it, if necessary. */
27142 expand_or_defer_fn (fn);
27143 /* Restore the saved values. */
27144 parser->in_unbraced_linkage_specification_p
27145 = saved_in_unbraced_linkage_specification_p;
27146 parser->num_template_parameter_lists
27147 = saved_num_template_parameter_lists;
27148 parser->in_function_body = saved_in_function_body;
27150 parser->fully_implicit_function_template_p
27151 = fully_implicit_function_template_p;
27152 parser->implicit_template_parms
27153 = implicit_template_parms;
27154 parser->implicit_template_scope
27155 = implicit_template_scope;
27157 if (parser->fully_implicit_function_template_p)
27158 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27160 return fn;
27163 /* Parse a template-declaration body (following argument list). */
27165 static void
27166 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27167 tree parameter_list,
27168 bool member_p)
27170 tree decl = NULL_TREE;
27171 bool friend_p = false;
27173 /* We just processed one more parameter list. */
27174 ++parser->num_template_parameter_lists;
27176 /* Get the deferred access checks from the parameter list. These
27177 will be checked once we know what is being declared, as for a
27178 member template the checks must be performed in the scope of the
27179 class containing the member. */
27180 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27182 /* Tentatively parse for a new template parameter list, which can either be
27183 the template keyword or a template introduction. */
27184 if (cp_parser_template_declaration_after_export (parser, member_p))
27185 /* OK */;
27186 else if (cxx_dialect >= cxx11
27187 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27188 decl = cp_parser_alias_declaration (parser);
27189 else
27191 /* There are no access checks when parsing a template, as we do not
27192 know if a specialization will be a friend. */
27193 push_deferring_access_checks (dk_no_check);
27194 cp_token *token = cp_lexer_peek_token (parser->lexer);
27195 decl = cp_parser_single_declaration (parser,
27196 checks,
27197 member_p,
27198 /*explicit_specialization_p=*/false,
27199 &friend_p);
27200 pop_deferring_access_checks ();
27202 /* If this is a member template declaration, let the front
27203 end know. */
27204 if (member_p && !friend_p && decl)
27206 if (TREE_CODE (decl) == TYPE_DECL)
27207 cp_parser_check_access_in_redeclaration (decl, token->location);
27209 decl = finish_member_template_decl (decl);
27211 else if (friend_p && decl
27212 && DECL_DECLARES_TYPE_P (decl))
27213 make_friend_class (current_class_type, TREE_TYPE (decl),
27214 /*complain=*/true);
27216 /* We are done with the current parameter list. */
27217 --parser->num_template_parameter_lists;
27219 pop_deferring_access_checks ();
27221 /* Finish up. */
27222 finish_template_decl (parameter_list);
27224 /* Check the template arguments for a literal operator template. */
27225 if (decl
27226 && DECL_DECLARES_FUNCTION_P (decl)
27227 && UDLIT_OPER_P (DECL_NAME (decl)))
27229 bool ok = true;
27230 if (parameter_list == NULL_TREE)
27231 ok = false;
27232 else
27234 int num_parms = TREE_VEC_LENGTH (parameter_list);
27235 if (num_parms == 1)
27237 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27238 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27239 if (CLASS_TYPE_P (TREE_TYPE (parm)))
27240 /* OK, C++20 string literal operator template. We don't need
27241 to warn in lower dialects here because we will have already
27242 warned about the template parameter. */;
27243 else if (TREE_TYPE (parm) != char_type_node
27244 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27245 ok = false;
27247 else if (num_parms == 2 && cxx_dialect >= cxx14)
27249 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27250 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27251 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27252 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27253 if (parm == error_mark_node
27254 || TREE_TYPE (parm) != TREE_TYPE (type)
27255 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27256 ok = false;
27257 else
27258 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27259 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
27260 "ISO C++ did not adopt string literal operator templa"
27261 "tes taking an argument pack of characters");
27263 else
27264 ok = false;
27266 if (!ok)
27268 if (cxx_dialect > cxx17)
27269 error ("literal operator template %qD has invalid parameter list;"
27270 " Expected non-type template parameter pack <char...> "
27271 " or single non-type parameter of class type",
27272 decl);
27273 else
27274 error ("literal operator template %qD has invalid parameter list."
27275 " Expected non-type template parameter pack <char...>",
27276 decl);
27280 /* Register member declarations. */
27281 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27282 finish_member_declaration (decl);
27283 /* If DECL is a function template, we must return to parse it later.
27284 (Even though there is no definition, there might be default
27285 arguments that need handling.) */
27286 if (member_p && decl
27287 && DECL_DECLARES_FUNCTION_P (decl))
27288 vec_safe_push (unparsed_funs_with_definitions, decl);
27291 /* Parse a template introduction header for a template-declaration. Returns
27292 false if tentative parse fails. */
27294 static bool
27295 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27297 cp_parser_parse_tentatively (parser);
27299 tree saved_scope = parser->scope;
27300 tree saved_object_scope = parser->object_scope;
27301 tree saved_qualifying_scope = parser->qualifying_scope;
27303 /* Look for the optional `::' operator. */
27304 cp_parser_global_scope_opt (parser,
27305 /*current_scope_valid_p=*/false);
27306 /* Look for the nested-name-specifier. */
27307 cp_parser_nested_name_specifier_opt (parser,
27308 /*typename_keyword_p=*/false,
27309 /*check_dependency_p=*/true,
27310 /*type_p=*/false,
27311 /*is_declaration=*/false);
27313 cp_token *token = cp_lexer_peek_token (parser->lexer);
27314 tree concept_name = cp_parser_identifier (parser);
27316 /* Look up the concept for which we will be matching
27317 template parameters. */
27318 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27319 token->location);
27320 parser->scope = saved_scope;
27321 parser->object_scope = saved_object_scope;
27322 parser->qualifying_scope = saved_qualifying_scope;
27324 if (concept_name == error_mark_node)
27325 cp_parser_simulate_error (parser);
27327 /* Look for opening brace for introduction. */
27328 matching_braces braces;
27329 braces.require_open (parser);
27331 if (!cp_parser_parse_definitely (parser))
27332 return false;
27334 push_deferring_access_checks (dk_deferred);
27336 /* Build vector of placeholder parameters and grab
27337 matching identifiers. */
27338 tree introduction_list = cp_parser_introduction_list (parser);
27340 /* Look for closing brace for introduction. */
27341 if (!braces.require_close (parser))
27342 return true;
27344 /* The introduction-list shall not be empty. */
27345 int nargs = TREE_VEC_LENGTH (introduction_list);
27346 if (nargs == 0)
27348 /* In cp_parser_introduction_list we have already issued an error. */
27349 return true;
27352 if (tmpl_decl == error_mark_node)
27354 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27355 token->location);
27356 return true;
27359 /* Build and associate the constraint. */
27360 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27361 if (parms && parms != error_mark_node)
27363 cp_parser_template_declaration_after_parameters (parser, parms,
27364 member_p);
27365 return true;
27368 error_at (token->location, "no matching concept for template-introduction");
27369 return true;
27372 /* Parse a normal template-declaration following the template keyword. */
27374 static void
27375 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27377 tree parameter_list;
27378 bool need_lang_pop;
27379 location_t location = input_location;
27381 /* Look for the `<' token. */
27382 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27383 return;
27384 if (at_class_scope_p () && current_function_decl)
27386 /* 14.5.2.2 [temp.mem]
27388 A local class shall not have member templates. */
27389 error_at (location,
27390 "invalid declaration of member template in local class");
27391 cp_parser_skip_to_end_of_block_or_statement (parser);
27392 return;
27394 /* [temp]
27396 A template ... shall not have C linkage. */
27397 if (current_lang_name == lang_name_c)
27399 error_at (location, "template with C linkage");
27400 maybe_show_extern_c_location ();
27401 /* Give it C++ linkage to avoid confusing other parts of the
27402 front end. */
27403 push_lang_context (lang_name_cplusplus);
27404 need_lang_pop = true;
27406 else
27407 need_lang_pop = false;
27409 /* We cannot perform access checks on the template parameter
27410 declarations until we know what is being declared, just as we
27411 cannot check the decl-specifier list. */
27412 push_deferring_access_checks (dk_deferred);
27414 /* If the next token is `>', then we have an invalid
27415 specialization. Rather than complain about an invalid template
27416 parameter, issue an error message here. */
27417 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27419 cp_parser_error (parser, "invalid explicit specialization");
27420 begin_specialization ();
27421 parameter_list = NULL_TREE;
27423 else
27425 /* Parse the template parameters. */
27426 parameter_list = cp_parser_template_parameter_list (parser);
27429 /* Look for the `>'. */
27430 cp_parser_skip_to_end_of_template_parameter_list (parser);
27432 /* Manage template requirements */
27433 if (flag_concepts)
27435 tree reqs = get_shorthand_constraints (current_template_parms);
27436 if (tree r = cp_parser_requires_clause_opt (parser))
27437 reqs = conjoin_constraints (reqs, normalize_expression (r));
27438 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27441 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27442 member_p);
27444 /* For the erroneous case of a template with C linkage, we pushed an
27445 implicit C++ linkage scope; exit that scope now. */
27446 if (need_lang_pop)
27447 pop_lang_context ();
27450 /* Parse a template-declaration, assuming that the `export' (and
27451 `extern') keywords, if present, has already been scanned. MEMBER_P
27452 is as for cp_parser_template_declaration. */
27454 static bool
27455 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27457 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27459 cp_lexer_consume_token (parser->lexer);
27460 cp_parser_explicit_template_declaration (parser, member_p);
27461 return true;
27463 else if (flag_concepts)
27464 return cp_parser_template_introduction (parser, member_p);
27466 return false;
27469 /* Perform the deferred access checks from a template-parameter-list.
27470 CHECKS is a TREE_LIST of access checks, as returned by
27471 get_deferred_access_checks. */
27473 static void
27474 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27476 ++processing_template_parmlist;
27477 perform_access_checks (checks, tf_warning_or_error);
27478 --processing_template_parmlist;
27481 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27482 `function-definition' sequence that follows a template header.
27483 If MEMBER_P is true, this declaration appears in a class scope.
27485 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27486 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27488 static tree
27489 cp_parser_single_declaration (cp_parser* parser,
27490 vec<deferred_access_check, va_gc> *checks,
27491 bool member_p,
27492 bool explicit_specialization_p,
27493 bool* friend_p)
27495 int declares_class_or_enum;
27496 tree decl = NULL_TREE;
27497 cp_decl_specifier_seq decl_specifiers;
27498 bool function_definition_p = false;
27499 cp_token *decl_spec_token_start;
27501 /* This function is only used when processing a template
27502 declaration. */
27503 gcc_assert (innermost_scope_kind () == sk_template_parms
27504 || innermost_scope_kind () == sk_template_spec);
27506 /* Defer access checks until we know what is being declared. */
27507 push_deferring_access_checks (dk_deferred);
27509 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27510 alternative. */
27511 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27512 cp_parser_decl_specifier_seq (parser,
27513 CP_PARSER_FLAGS_OPTIONAL,
27514 &decl_specifiers,
27515 &declares_class_or_enum);
27516 if (friend_p)
27517 *friend_p = cp_parser_friend_p (&decl_specifiers);
27519 /* There are no template typedefs. */
27520 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27522 error_at (decl_spec_token_start->location,
27523 "template declaration of %<typedef%>");
27524 decl = error_mark_node;
27527 /* Gather up the access checks that occurred the
27528 decl-specifier-seq. */
27529 stop_deferring_access_checks ();
27531 /* Check for the declaration of a template class. */
27532 if (declares_class_or_enum)
27534 if (cp_parser_declares_only_class_p (parser)
27535 || (declares_class_or_enum & 2))
27537 // If this is a declaration, but not a definition, associate
27538 // any constraints with the type declaration. Constraints
27539 // are associated with definitions in cp_parser_class_specifier.
27540 if (declares_class_or_enum == 1)
27541 associate_classtype_constraints (decl_specifiers.type);
27543 decl = shadow_tag (&decl_specifiers);
27545 /* In this case:
27547 struct C {
27548 friend template <typename T> struct A<T>::B;
27551 A<T>::B will be represented by a TYPENAME_TYPE, and
27552 therefore not recognized by shadow_tag. */
27553 if (friend_p && *friend_p
27554 && !decl
27555 && decl_specifiers.type
27556 && TYPE_P (decl_specifiers.type))
27557 decl = decl_specifiers.type;
27559 if (decl && decl != error_mark_node)
27560 decl = TYPE_NAME (decl);
27561 else
27562 decl = error_mark_node;
27564 /* Perform access checks for template parameters. */
27565 cp_parser_perform_template_parameter_access_checks (checks);
27567 /* Give a helpful diagnostic for
27568 template <class T> struct A { } a;
27569 if we aren't already recovering from an error. */
27570 if (!cp_parser_declares_only_class_p (parser)
27571 && !seen_error ())
27573 error_at (cp_lexer_peek_token (parser->lexer)->location,
27574 "a class template declaration must not declare "
27575 "anything else");
27576 cp_parser_skip_to_end_of_block_or_statement (parser);
27577 goto out;
27582 /* Complain about missing 'typename' or other invalid type names. */
27583 if (!decl_specifiers.any_type_specifiers_p
27584 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27586 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27587 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27588 the rest of this declaration. */
27589 decl = error_mark_node;
27590 goto out;
27593 /* If it's not a template class, try for a template function. If
27594 the next token is a `;', then this declaration does not declare
27595 anything. But, if there were errors in the decl-specifiers, then
27596 the error might well have come from an attempted class-specifier.
27597 In that case, there's no need to warn about a missing declarator. */
27598 if (!decl
27599 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27600 || decl_specifiers.type != error_mark_node))
27602 decl = cp_parser_init_declarator (parser,
27603 &decl_specifiers,
27604 checks,
27605 /*function_definition_allowed_p=*/true,
27606 member_p,
27607 declares_class_or_enum,
27608 &function_definition_p,
27609 NULL, NULL, NULL);
27611 /* 7.1.1-1 [dcl.stc]
27613 A storage-class-specifier shall not be specified in an explicit
27614 specialization... */
27615 if (decl
27616 && explicit_specialization_p
27617 && decl_specifiers.storage_class != sc_none)
27619 error_at (decl_spec_token_start->location,
27620 "explicit template specialization cannot have a storage class");
27621 decl = error_mark_node;
27624 if (decl && VAR_P (decl))
27625 check_template_variable (decl);
27628 /* Look for a trailing `;' after the declaration. */
27629 if (!function_definition_p
27630 && (decl == error_mark_node
27631 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27632 cp_parser_skip_to_end_of_block_or_statement (parser);
27634 out:
27635 pop_deferring_access_checks ();
27637 /* Clear any current qualification; whatever comes next is the start
27638 of something new. */
27639 parser->scope = NULL_TREE;
27640 parser->qualifying_scope = NULL_TREE;
27641 parser->object_scope = NULL_TREE;
27643 return decl;
27646 /* Parse a cast-expression that is not the operand of a unary "&". */
27648 static cp_expr
27649 cp_parser_simple_cast_expression (cp_parser *parser)
27651 return cp_parser_cast_expression (parser, /*address_p=*/false,
27652 /*cast_p=*/false, /*decltype*/false, NULL);
27655 /* Parse a functional cast to TYPE. Returns an expression
27656 representing the cast. */
27658 static cp_expr
27659 cp_parser_functional_cast (cp_parser* parser, tree type)
27661 vec<tree, va_gc> *vec;
27662 tree expression_list;
27663 cp_expr cast;
27664 bool nonconst_p;
27666 location_t start_loc = input_location;
27668 if (!type)
27669 type = error_mark_node;
27671 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27673 cp_lexer_set_source_position (parser->lexer);
27674 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27675 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27676 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27677 if (TREE_CODE (type) == TYPE_DECL)
27678 type = TREE_TYPE (type);
27680 cast = finish_compound_literal (type, expression_list,
27681 tf_warning_or_error, fcl_functional);
27682 /* Create a location of the form:
27683 type_name{i, f}
27684 ^~~~~~~~~~~~~~~
27685 with caret == start at the start of the type name,
27686 finishing at the closing brace. */
27687 location_t finish_loc
27688 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27689 location_t combined_loc = make_location (start_loc, start_loc,
27690 finish_loc);
27691 cast.set_location (combined_loc);
27692 return cast;
27696 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27697 /*cast_p=*/true,
27698 /*allow_expansion_p=*/true,
27699 /*non_constant_p=*/NULL);
27700 if (vec == NULL)
27701 expression_list = error_mark_node;
27702 else
27704 expression_list = build_tree_list_vec (vec);
27705 release_tree_vector (vec);
27708 cast = build_functional_cast (type, expression_list,
27709 tf_warning_or_error);
27710 /* [expr.const]/1: In an integral constant expression "only type
27711 conversions to integral or enumeration type can be used". */
27712 if (TREE_CODE (type) == TYPE_DECL)
27713 type = TREE_TYPE (type);
27714 if (cast != error_mark_node
27715 && !cast_valid_in_integral_constant_expression_p (type)
27716 && cp_parser_non_integral_constant_expression (parser,
27717 NIC_CONSTRUCTOR))
27718 return error_mark_node;
27720 /* Create a location of the form:
27721 float(i)
27722 ^~~~~~~~
27723 with caret == start at the start of the type name,
27724 finishing at the closing paren. */
27725 location_t finish_loc
27726 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27727 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27728 cast.set_location (combined_loc);
27729 return cast;
27732 /* Save the tokens that make up the body of a member function defined
27733 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27734 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27735 specifiers applied to the declaration. Returns the FUNCTION_DECL
27736 for the member function. */
27738 static tree
27739 cp_parser_save_member_function_body (cp_parser* parser,
27740 cp_decl_specifier_seq *decl_specifiers,
27741 cp_declarator *declarator,
27742 tree attributes)
27744 cp_token *first;
27745 cp_token *last;
27746 tree fn;
27747 bool function_try_block = false;
27749 /* Create the FUNCTION_DECL. */
27750 fn = grokmethod (decl_specifiers, declarator, attributes);
27751 cp_finalize_omp_declare_simd (parser, fn);
27752 cp_finalize_oacc_routine (parser, fn, true);
27753 /* If something went badly wrong, bail out now. */
27754 if (fn == error_mark_node)
27756 /* If there's a function-body, skip it. */
27757 if (cp_parser_token_starts_function_definition_p
27758 (cp_lexer_peek_token (parser->lexer)))
27759 cp_parser_skip_to_end_of_block_or_statement (parser);
27760 return error_mark_node;
27763 /* Remember it, if there default args to post process. */
27764 cp_parser_save_default_args (parser, fn);
27766 /* Save away the tokens that make up the body of the
27767 function. */
27768 first = parser->lexer->next_token;
27770 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27771 cp_lexer_consume_token (parser->lexer);
27772 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27773 RID_TRANSACTION_ATOMIC))
27775 cp_lexer_consume_token (parser->lexer);
27776 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27777 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27778 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27779 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27780 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27781 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27782 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27784 cp_lexer_consume_token (parser->lexer);
27785 cp_lexer_consume_token (parser->lexer);
27786 cp_lexer_consume_token (parser->lexer);
27787 cp_lexer_consume_token (parser->lexer);
27788 cp_lexer_consume_token (parser->lexer);
27790 else
27791 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27792 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27794 cp_lexer_consume_token (parser->lexer);
27795 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27796 break;
27800 /* Handle function try blocks. */
27801 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27803 cp_lexer_consume_token (parser->lexer);
27804 function_try_block = true;
27806 /* We can have braced-init-list mem-initializers before the fn body. */
27807 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27809 cp_lexer_consume_token (parser->lexer);
27810 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27812 /* cache_group will stop after an un-nested { } pair, too. */
27813 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27814 break;
27816 /* variadic mem-inits have ... after the ')'. */
27817 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27818 cp_lexer_consume_token (parser->lexer);
27821 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27822 /* Handle function try blocks. */
27823 if (function_try_block)
27824 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27825 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27826 last = parser->lexer->next_token;
27828 /* Save away the inline definition; we will process it when the
27829 class is complete. */
27830 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27831 DECL_PENDING_INLINE_P (fn) = 1;
27833 /* We need to know that this was defined in the class, so that
27834 friend templates are handled correctly. */
27835 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27837 /* Add FN to the queue of functions to be parsed later. */
27838 vec_safe_push (unparsed_funs_with_definitions, fn);
27840 return fn;
27843 /* Save the tokens that make up the in-class initializer for a non-static
27844 data member. Returns a DEFAULT_ARG. */
27846 static tree
27847 cp_parser_save_nsdmi (cp_parser* parser)
27849 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27852 /* Parse a template-argument-list, as well as the trailing ">" (but
27853 not the opening "<"). See cp_parser_template_argument_list for the
27854 return value. */
27856 static tree
27857 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27859 tree arguments;
27860 tree saved_scope;
27861 tree saved_qualifying_scope;
27862 tree saved_object_scope;
27863 bool saved_greater_than_is_operator_p;
27865 /* [temp.names]
27867 When parsing a template-id, the first non-nested `>' is taken as
27868 the end of the template-argument-list rather than a greater-than
27869 operator. */
27870 saved_greater_than_is_operator_p
27871 = parser->greater_than_is_operator_p;
27872 parser->greater_than_is_operator_p = false;
27873 /* Parsing the argument list may modify SCOPE, so we save it
27874 here. */
27875 saved_scope = parser->scope;
27876 saved_qualifying_scope = parser->qualifying_scope;
27877 saved_object_scope = parser->object_scope;
27878 /* We need to evaluate the template arguments, even though this
27879 template-id may be nested within a "sizeof". */
27880 cp_evaluated ev;
27881 /* Parse the template-argument-list itself. */
27882 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27883 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27884 arguments = NULL_TREE;
27885 else
27886 arguments = cp_parser_template_argument_list (parser);
27887 /* Look for the `>' that ends the template-argument-list. If we find
27888 a '>>' instead, it's probably just a typo. */
27889 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27891 if (cxx_dialect != cxx98)
27893 /* In C++0x, a `>>' in a template argument list or cast
27894 expression is considered to be two separate `>'
27895 tokens. So, change the current token to a `>', but don't
27896 consume it: it will be consumed later when the outer
27897 template argument list (or cast expression) is parsed.
27898 Note that this replacement of `>' for `>>' is necessary
27899 even if we are parsing tentatively: in the tentative
27900 case, after calling
27901 cp_parser_enclosed_template_argument_list we will always
27902 throw away all of the template arguments and the first
27903 closing `>', either because the template argument list
27904 was erroneous or because we are replacing those tokens
27905 with a CPP_TEMPLATE_ID token. The second `>' (which will
27906 not have been thrown away) is needed either to close an
27907 outer template argument list or to complete a new-style
27908 cast. */
27909 cp_token *token = cp_lexer_peek_token (parser->lexer);
27910 token->type = CPP_GREATER;
27912 else if (!saved_greater_than_is_operator_p)
27914 /* If we're in a nested template argument list, the '>>' has
27915 to be a typo for '> >'. We emit the error message, but we
27916 continue parsing and we push a '>' as next token, so that
27917 the argument list will be parsed correctly. Note that the
27918 global source location is still on the token before the
27919 '>>', so we need to say explicitly where we want it. */
27920 cp_token *token = cp_lexer_peek_token (parser->lexer);
27921 gcc_rich_location richloc (token->location);
27922 richloc.add_fixit_replace ("> >");
27923 error_at (&richloc, "%<>>%> should be %<> >%> "
27924 "within a nested template argument list");
27926 token->type = CPP_GREATER;
27928 else
27930 /* If this is not a nested template argument list, the '>>'
27931 is a typo for '>'. Emit an error message and continue.
27932 Same deal about the token location, but here we can get it
27933 right by consuming the '>>' before issuing the diagnostic. */
27934 cp_token *token = cp_lexer_consume_token (parser->lexer);
27935 error_at (token->location,
27936 "spurious %<>>%>, use %<>%> to terminate "
27937 "a template argument list");
27940 else
27941 cp_parser_skip_to_end_of_template_parameter_list (parser);
27942 /* The `>' token might be a greater-than operator again now. */
27943 parser->greater_than_is_operator_p
27944 = saved_greater_than_is_operator_p;
27945 /* Restore the SAVED_SCOPE. */
27946 parser->scope = saved_scope;
27947 parser->qualifying_scope = saved_qualifying_scope;
27948 parser->object_scope = saved_object_scope;
27950 return arguments;
27953 /* MEMBER_FUNCTION is a member function, or a friend. If default
27954 arguments, or the body of the function have not yet been parsed,
27955 parse them now. */
27957 static void
27958 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27960 timevar_push (TV_PARSE_INMETH);
27961 /* If this member is a template, get the underlying
27962 FUNCTION_DECL. */
27963 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27964 member_function = DECL_TEMPLATE_RESULT (member_function);
27966 /* There should not be any class definitions in progress at this
27967 point; the bodies of members are only parsed outside of all class
27968 definitions. */
27969 gcc_assert (parser->num_classes_being_defined == 0);
27970 /* While we're parsing the member functions we might encounter more
27971 classes. We want to handle them right away, but we don't want
27972 them getting mixed up with functions that are currently in the
27973 queue. */
27974 push_unparsed_function_queues (parser);
27976 /* Make sure that any template parameters are in scope. */
27977 maybe_begin_member_template_processing (member_function);
27979 /* If the body of the function has not yet been parsed, parse it
27980 now. */
27981 if (DECL_PENDING_INLINE_P (member_function))
27983 tree function_scope;
27984 cp_token_cache *tokens;
27986 /* The function is no longer pending; we are processing it. */
27987 tokens = DECL_PENDING_INLINE_INFO (member_function);
27988 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27989 DECL_PENDING_INLINE_P (member_function) = 0;
27991 /* If this is a local class, enter the scope of the containing
27992 function. */
27993 function_scope = current_function_decl;
27994 if (function_scope)
27995 push_function_context ();
27997 /* Push the body of the function onto the lexer stack. */
27998 cp_parser_push_lexer_for_tokens (parser, tokens);
28000 /* Let the front end know that we going to be defining this
28001 function. */
28002 start_preparsed_function (member_function, NULL_TREE,
28003 SF_PRE_PARSED | SF_INCLASS_INLINE);
28005 /* Don't do access checking if it is a templated function. */
28006 if (processing_template_decl)
28007 push_deferring_access_checks (dk_no_check);
28009 /* #pragma omp declare reduction needs special parsing. */
28010 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28012 parser->lexer->in_pragma = true;
28013 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28014 finish_function (/*inline_p=*/true);
28015 cp_check_omp_declare_reduction (member_function);
28017 else
28018 /* Now, parse the body of the function. */
28019 cp_parser_function_definition_after_declarator (parser,
28020 /*inline_p=*/true);
28022 if (processing_template_decl)
28023 pop_deferring_access_checks ();
28025 /* Leave the scope of the containing function. */
28026 if (function_scope)
28027 pop_function_context ();
28028 cp_parser_pop_lexer (parser);
28031 /* Remove any template parameters from the symbol table. */
28032 maybe_end_member_template_processing ();
28034 /* Restore the queue. */
28035 pop_unparsed_function_queues (parser);
28036 timevar_pop (TV_PARSE_INMETH);
28039 /* If DECL contains any default args, remember it on the unparsed
28040 functions queue. */
28042 static void
28043 cp_parser_save_default_args (cp_parser* parser, tree decl)
28045 tree probe;
28047 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28048 probe;
28049 probe = TREE_CHAIN (probe))
28050 if (TREE_PURPOSE (probe))
28052 cp_default_arg_entry entry = {current_class_type, decl};
28053 vec_safe_push (unparsed_funs_with_default_args, entry);
28054 break;
28058 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28059 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28060 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28061 from the parameter-type-list. */
28063 static tree
28064 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28065 tree default_arg, tree parmtype)
28067 cp_token_cache *tokens;
28068 tree parsed_arg;
28069 bool dummy;
28071 if (default_arg == error_mark_node)
28072 return error_mark_node;
28074 /* Push the saved tokens for the default argument onto the parser's
28075 lexer stack. */
28076 tokens = DEFARG_TOKENS (default_arg);
28077 cp_parser_push_lexer_for_tokens (parser, tokens);
28079 start_lambda_scope (decl);
28081 /* Parse the default argument. */
28082 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28083 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28084 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28086 finish_lambda_scope ();
28088 if (parsed_arg == error_mark_node)
28089 cp_parser_skip_to_end_of_statement (parser);
28091 if (!processing_template_decl)
28093 /* In a non-template class, check conversions now. In a template,
28094 we'll wait and instantiate these as needed. */
28095 if (TREE_CODE (decl) == PARM_DECL)
28096 parsed_arg = check_default_argument (parmtype, parsed_arg,
28097 tf_warning_or_error);
28098 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28099 parsed_arg = error_mark_node;
28100 else
28101 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28104 /* If the token stream has not been completely used up, then
28105 there was extra junk after the end of the default
28106 argument. */
28107 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28109 if (TREE_CODE (decl) == PARM_DECL)
28110 cp_parser_error (parser, "expected %<,%>");
28111 else
28112 cp_parser_error (parser, "expected %<;%>");
28115 /* Revert to the main lexer. */
28116 cp_parser_pop_lexer (parser);
28118 return parsed_arg;
28121 /* FIELD is a non-static data member with an initializer which we saved for
28122 later; parse it now. */
28124 static void
28125 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28127 tree def;
28129 maybe_begin_member_template_processing (field);
28131 push_unparsed_function_queues (parser);
28132 def = cp_parser_late_parse_one_default_arg (parser, field,
28133 DECL_INITIAL (field),
28134 NULL_TREE);
28135 pop_unparsed_function_queues (parser);
28137 maybe_end_member_template_processing ();
28139 DECL_INITIAL (field) = def;
28142 /* FN is a FUNCTION_DECL which may contains a parameter with an
28143 unparsed DEFAULT_ARG. Parse the default args now. This function
28144 assumes that the current scope is the scope in which the default
28145 argument should be processed. */
28147 static void
28148 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28150 bool saved_local_variables_forbidden_p;
28151 tree parm, parmdecl;
28153 /* While we're parsing the default args, we might (due to the
28154 statement expression extension) encounter more classes. We want
28155 to handle them right away, but we don't want them getting mixed
28156 up with default args that are currently in the queue. */
28157 push_unparsed_function_queues (parser);
28159 /* Local variable names (and the `this' keyword) may not appear
28160 in a default argument. */
28161 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28162 parser->local_variables_forbidden_p = true;
28164 push_defarg_context (fn);
28166 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28167 parmdecl = DECL_ARGUMENTS (fn);
28168 parm && parm != void_list_node;
28169 parm = TREE_CHAIN (parm),
28170 parmdecl = DECL_CHAIN (parmdecl))
28172 tree default_arg = TREE_PURPOSE (parm);
28173 tree parsed_arg;
28174 vec<tree, va_gc> *insts;
28175 tree copy;
28176 unsigned ix;
28178 if (!default_arg)
28179 continue;
28181 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28182 /* This can happen for a friend declaration for a function
28183 already declared with default arguments. */
28184 continue;
28186 parsed_arg
28187 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28188 default_arg,
28189 TREE_VALUE (parm));
28190 TREE_PURPOSE (parm) = parsed_arg;
28192 /* Update any instantiations we've already created. */
28193 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28194 vec_safe_iterate (insts, ix, &copy); ix++)
28195 TREE_PURPOSE (copy) = parsed_arg;
28198 pop_defarg_context ();
28200 /* Make sure no default arg is missing. */
28201 check_default_args (fn);
28203 /* Restore the state of local_variables_forbidden_p. */
28204 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28206 /* Restore the queue. */
28207 pop_unparsed_function_queues (parser);
28210 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28212 sizeof ... ( identifier )
28214 where the 'sizeof' token has already been consumed. */
28216 static tree
28217 cp_parser_sizeof_pack (cp_parser *parser)
28219 /* Consume the `...'. */
28220 cp_lexer_consume_token (parser->lexer);
28221 maybe_warn_variadic_templates ();
28223 matching_parens parens;
28224 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28225 if (paren)
28226 parens.consume_open (parser);
28227 else
28228 permerror (cp_lexer_peek_token (parser->lexer)->location,
28229 "%<sizeof...%> argument must be surrounded by parentheses");
28231 cp_token *token = cp_lexer_peek_token (parser->lexer);
28232 tree name = cp_parser_identifier (parser);
28233 if (name == error_mark_node)
28234 return error_mark_node;
28235 /* The name is not qualified. */
28236 parser->scope = NULL_TREE;
28237 parser->qualifying_scope = NULL_TREE;
28238 parser->object_scope = NULL_TREE;
28239 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28240 if (expr == error_mark_node)
28241 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28242 token->location);
28243 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28244 expr = TREE_TYPE (expr);
28245 else if (TREE_CODE (expr) == CONST_DECL)
28246 expr = DECL_INITIAL (expr);
28247 expr = make_pack_expansion (expr);
28248 PACK_EXPANSION_SIZEOF_P (expr) = true;
28250 if (paren)
28251 parens.require_close (parser);
28253 return expr;
28256 /* Parse the operand of `sizeof' (or a similar operator). Returns
28257 either a TYPE or an expression, depending on the form of the
28258 input. The KEYWORD indicates which kind of expression we have
28259 encountered. */
28261 static tree
28262 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28264 tree expr = NULL_TREE;
28265 const char *saved_message;
28266 char *tmp;
28267 bool saved_integral_constant_expression_p;
28268 bool saved_non_integral_constant_expression_p;
28270 /* If it's a `...', then we are computing the length of a parameter
28271 pack. */
28272 if (keyword == RID_SIZEOF
28273 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28274 return cp_parser_sizeof_pack (parser);
28276 /* Types cannot be defined in a `sizeof' expression. Save away the
28277 old message. */
28278 saved_message = parser->type_definition_forbidden_message;
28279 /* And create the new one. */
28280 tmp = concat ("types may not be defined in %<",
28281 IDENTIFIER_POINTER (ridpointers[keyword]),
28282 "%> expressions", NULL);
28283 parser->type_definition_forbidden_message = tmp;
28285 /* The restrictions on constant-expressions do not apply inside
28286 sizeof expressions. */
28287 saved_integral_constant_expression_p
28288 = parser->integral_constant_expression_p;
28289 saved_non_integral_constant_expression_p
28290 = parser->non_integral_constant_expression_p;
28291 parser->integral_constant_expression_p = false;
28293 /* Do not actually evaluate the expression. */
28294 ++cp_unevaluated_operand;
28295 ++c_inhibit_evaluation_warnings;
28296 /* If it's a `(', then we might be looking at the type-id
28297 construction. */
28298 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28300 tree type = NULL_TREE;
28302 /* We can't be sure yet whether we're looking at a type-id or an
28303 expression. */
28304 cp_parser_parse_tentatively (parser);
28306 matching_parens parens;
28307 parens.consume_open (parser);
28309 /* Note: as a GNU Extension, compound literals are considered
28310 postfix-expressions as they are in C99, so they are valid
28311 arguments to sizeof. See comment in cp_parser_cast_expression
28312 for details. */
28313 if (cp_parser_compound_literal_p (parser))
28314 cp_parser_simulate_error (parser);
28315 else
28317 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28318 parser->in_type_id_in_expr_p = true;
28319 /* Look for the type-id. */
28320 type = cp_parser_type_id (parser);
28321 /* Look for the closing `)'. */
28322 parens.require_close (parser);
28323 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28326 /* If all went well, then we're done. */
28327 if (cp_parser_parse_definitely (parser))
28328 expr = type;
28331 /* If the type-id production did not work out, then we must be
28332 looking at the unary-expression production. */
28333 if (!expr)
28334 expr = cp_parser_unary_expression (parser);
28336 /* Go back to evaluating expressions. */
28337 --cp_unevaluated_operand;
28338 --c_inhibit_evaluation_warnings;
28340 /* Free the message we created. */
28341 free (tmp);
28342 /* And restore the old one. */
28343 parser->type_definition_forbidden_message = saved_message;
28344 parser->integral_constant_expression_p
28345 = saved_integral_constant_expression_p;
28346 parser->non_integral_constant_expression_p
28347 = saved_non_integral_constant_expression_p;
28349 return expr;
28352 /* If the current declaration has no declarator, return true. */
28354 static bool
28355 cp_parser_declares_only_class_p (cp_parser *parser)
28357 /* If the next token is a `;' or a `,' then there is no
28358 declarator. */
28359 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28360 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28363 /* Update the DECL_SPECS to reflect the storage class indicated by
28364 KEYWORD. */
28366 static void
28367 cp_parser_set_storage_class (cp_parser *parser,
28368 cp_decl_specifier_seq *decl_specs,
28369 enum rid keyword,
28370 cp_token *token)
28372 cp_storage_class storage_class;
28374 if (parser->in_unbraced_linkage_specification_p)
28376 error_at (token->location, "invalid use of %qD in linkage specification",
28377 ridpointers[keyword]);
28378 return;
28380 else if (decl_specs->storage_class != sc_none)
28382 decl_specs->conflicting_specifiers_p = true;
28383 return;
28386 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28387 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28388 && decl_specs->gnu_thread_keyword_p)
28390 pedwarn (decl_specs->locations[ds_thread], 0,
28391 "%<__thread%> before %qD", ridpointers[keyword]);
28394 switch (keyword)
28396 case RID_AUTO:
28397 storage_class = sc_auto;
28398 break;
28399 case RID_REGISTER:
28400 storage_class = sc_register;
28401 break;
28402 case RID_STATIC:
28403 storage_class = sc_static;
28404 break;
28405 case RID_EXTERN:
28406 storage_class = sc_extern;
28407 break;
28408 case RID_MUTABLE:
28409 storage_class = sc_mutable;
28410 break;
28411 default:
28412 gcc_unreachable ();
28414 decl_specs->storage_class = storage_class;
28415 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28417 /* A storage class specifier cannot be applied alongside a typedef
28418 specifier. If there is a typedef specifier present then set
28419 conflicting_specifiers_p which will trigger an error later
28420 on in grokdeclarator. */
28421 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28422 decl_specs->conflicting_specifiers_p = true;
28425 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28426 is true, the type is a class or enum definition. */
28428 static void
28429 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28430 tree type_spec,
28431 cp_token *token,
28432 bool type_definition_p)
28434 decl_specs->any_specifiers_p = true;
28436 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28437 (with, for example, in "typedef int wchar_t;") we remember that
28438 this is what happened. In system headers, we ignore these
28439 declarations so that G++ can work with system headers that are not
28440 C++-safe. */
28441 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28442 && !type_definition_p
28443 && (type_spec == boolean_type_node
28444 || type_spec == char16_type_node
28445 || type_spec == char32_type_node
28446 || type_spec == wchar_type_node)
28447 && (decl_specs->type
28448 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28449 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28450 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28451 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28453 decl_specs->redefined_builtin_type = type_spec;
28454 set_and_check_decl_spec_loc (decl_specs,
28455 ds_redefined_builtin_type_spec,
28456 token);
28457 if (!decl_specs->type)
28459 decl_specs->type = type_spec;
28460 decl_specs->type_definition_p = false;
28461 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28464 else if (decl_specs->type)
28465 decl_specs->multiple_types_p = true;
28466 else
28468 decl_specs->type = type_spec;
28469 decl_specs->type_definition_p = type_definition_p;
28470 decl_specs->redefined_builtin_type = NULL_TREE;
28471 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28475 /* True iff TOKEN is the GNU keyword __thread. */
28477 static bool
28478 token_is__thread (cp_token *token)
28480 gcc_assert (token->keyword == RID_THREAD);
28481 return id_equal (token->u.value, "__thread");
28484 /* Set the location for a declarator specifier and check if it is
28485 duplicated.
28487 DECL_SPECS is the sequence of declarator specifiers onto which to
28488 set the location.
28490 DS is the single declarator specifier to set which location is to
28491 be set onto the existing sequence of declarators.
28493 LOCATION is the location for the declarator specifier to
28494 consider. */
28496 static void
28497 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28498 cp_decl_spec ds, cp_token *token)
28500 gcc_assert (ds < ds_last);
28502 if (decl_specs == NULL)
28503 return;
28505 source_location location = token->location;
28507 if (decl_specs->locations[ds] == 0)
28509 decl_specs->locations[ds] = location;
28510 if (ds == ds_thread)
28511 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28513 else
28515 if (ds == ds_long)
28517 if (decl_specs->locations[ds_long_long] != 0)
28518 error_at (location,
28519 "%<long long long%> is too long for GCC");
28520 else
28522 decl_specs->locations[ds_long_long] = location;
28523 pedwarn_cxx98 (location,
28524 OPT_Wlong_long,
28525 "ISO C++ 1998 does not support %<long long%>");
28528 else if (ds == ds_thread)
28530 bool gnu = token_is__thread (token);
28531 gcc_rich_location richloc (location);
28532 if (gnu != decl_specs->gnu_thread_keyword_p)
28534 richloc.add_range (decl_specs->locations[ds_thread]);
28535 error_at (&richloc,
28536 "both %<__thread%> and %<thread_local%> specified");
28538 else
28540 richloc.add_fixit_remove ();
28541 error_at (&richloc, "duplicate %qD", token->u.value);
28544 else
28546 static const char *const decl_spec_names[] = {
28547 "signed",
28548 "unsigned",
28549 "short",
28550 "long",
28551 "const",
28552 "volatile",
28553 "restrict",
28554 "inline",
28555 "virtual",
28556 "explicit",
28557 "friend",
28558 "typedef",
28559 "using",
28560 "constexpr",
28561 "__complex"
28563 gcc_rich_location richloc (location);
28564 richloc.add_fixit_remove ();
28565 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28570 /* Return true iff the declarator specifier DS is present in the
28571 sequence of declarator specifiers DECL_SPECS. */
28573 bool
28574 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28575 cp_decl_spec ds)
28577 gcc_assert (ds < ds_last);
28579 if (decl_specs == NULL)
28580 return false;
28582 return decl_specs->locations[ds] != 0;
28585 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28586 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28588 static bool
28589 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28591 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28594 /* Issue an error message indicating that TOKEN_DESC was expected.
28595 If KEYWORD is true, it indicated this function is called by
28596 cp_parser_require_keword and the required token can only be
28597 a indicated keyword.
28599 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28600 within any error as the location of an "opening" token matching
28601 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28602 RT_CLOSE_PAREN). */
28604 static void
28605 cp_parser_required_error (cp_parser *parser,
28606 required_token token_desc,
28607 bool keyword,
28608 location_t matching_location)
28610 if (cp_parser_simulate_error (parser))
28611 return;
28613 const char *gmsgid = NULL;
28614 switch (token_desc)
28616 case RT_NEW:
28617 gmsgid = G_("expected %<new%>");
28618 break;
28619 case RT_DELETE:
28620 gmsgid = G_("expected %<delete%>");
28621 break;
28622 case RT_RETURN:
28623 gmsgid = G_("expected %<return%>");
28624 break;
28625 case RT_WHILE:
28626 gmsgid = G_("expected %<while%>");
28627 break;
28628 case RT_EXTERN:
28629 gmsgid = G_("expected %<extern%>");
28630 break;
28631 case RT_STATIC_ASSERT:
28632 gmsgid = G_("expected %<static_assert%>");
28633 break;
28634 case RT_DECLTYPE:
28635 gmsgid = G_("expected %<decltype%>");
28636 break;
28637 case RT_OPERATOR:
28638 gmsgid = G_("expected %<operator%>");
28639 break;
28640 case RT_CLASS:
28641 gmsgid = G_("expected %<class%>");
28642 break;
28643 case RT_TEMPLATE:
28644 gmsgid = G_("expected %<template%>");
28645 break;
28646 case RT_NAMESPACE:
28647 gmsgid = G_("expected %<namespace%>");
28648 break;
28649 case RT_USING:
28650 gmsgid = G_("expected %<using%>");
28651 break;
28652 case RT_ASM:
28653 gmsgid = G_("expected %<asm%>");
28654 break;
28655 case RT_TRY:
28656 gmsgid = G_("expected %<try%>");
28657 break;
28658 case RT_CATCH:
28659 gmsgid = G_("expected %<catch%>");
28660 break;
28661 case RT_THROW:
28662 gmsgid = G_("expected %<throw%>");
28663 break;
28664 case RT_LABEL:
28665 gmsgid = G_("expected %<__label__%>");
28666 break;
28667 case RT_AT_TRY:
28668 gmsgid = G_("expected %<@try%>");
28669 break;
28670 case RT_AT_SYNCHRONIZED:
28671 gmsgid = G_("expected %<@synchronized%>");
28672 break;
28673 case RT_AT_THROW:
28674 gmsgid = G_("expected %<@throw%>");
28675 break;
28676 case RT_TRANSACTION_ATOMIC:
28677 gmsgid = G_("expected %<__transaction_atomic%>");
28678 break;
28679 case RT_TRANSACTION_RELAXED:
28680 gmsgid = G_("expected %<__transaction_relaxed%>");
28681 break;
28682 default:
28683 break;
28686 if (!gmsgid && !keyword)
28688 switch (token_desc)
28690 case RT_SEMICOLON:
28691 gmsgid = G_("expected %<;%>");
28692 break;
28693 case RT_OPEN_PAREN:
28694 gmsgid = G_("expected %<(%>");
28695 break;
28696 case RT_CLOSE_BRACE:
28697 gmsgid = G_("expected %<}%>");
28698 break;
28699 case RT_OPEN_BRACE:
28700 gmsgid = G_("expected %<{%>");
28701 break;
28702 case RT_CLOSE_SQUARE:
28703 gmsgid = G_("expected %<]%>");
28704 break;
28705 case RT_OPEN_SQUARE:
28706 gmsgid = G_("expected %<[%>");
28707 break;
28708 case RT_COMMA:
28709 gmsgid = G_("expected %<,%>");
28710 break;
28711 case RT_SCOPE:
28712 gmsgid = G_("expected %<::%>");
28713 break;
28714 case RT_LESS:
28715 gmsgid = G_("expected %<<%>");
28716 break;
28717 case RT_GREATER:
28718 gmsgid = G_("expected %<>%>");
28719 break;
28720 case RT_EQ:
28721 gmsgid = G_("expected %<=%>");
28722 break;
28723 case RT_ELLIPSIS:
28724 gmsgid = G_("expected %<...%>");
28725 break;
28726 case RT_MULT:
28727 gmsgid = G_("expected %<*%>");
28728 break;
28729 case RT_COMPL:
28730 gmsgid = G_("expected %<~%>");
28731 break;
28732 case RT_COLON:
28733 gmsgid = G_("expected %<:%>");
28734 break;
28735 case RT_COLON_SCOPE:
28736 gmsgid = G_("expected %<:%> or %<::%>");
28737 break;
28738 case RT_CLOSE_PAREN:
28739 gmsgid = G_("expected %<)%>");
28740 break;
28741 case RT_COMMA_CLOSE_PAREN:
28742 gmsgid = G_("expected %<,%> or %<)%>");
28743 break;
28744 case RT_PRAGMA_EOL:
28745 gmsgid = G_("expected end of line");
28746 break;
28747 case RT_NAME:
28748 gmsgid = G_("expected identifier");
28749 break;
28750 case RT_SELECT:
28751 gmsgid = G_("expected selection-statement");
28752 break;
28753 case RT_ITERATION:
28754 gmsgid = G_("expected iteration-statement");
28755 break;
28756 case RT_JUMP:
28757 gmsgid = G_("expected jump-statement");
28758 break;
28759 case RT_CLASS_KEY:
28760 gmsgid = G_("expected class-key");
28761 break;
28762 case RT_CLASS_TYPENAME_TEMPLATE:
28763 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28764 break;
28765 default:
28766 gcc_unreachable ();
28770 if (gmsgid)
28771 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28775 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28776 issue an error message indicating that TOKEN_DESC was expected.
28778 Returns the token consumed, if the token had the appropriate type.
28779 Otherwise, returns NULL.
28781 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28782 within any error as the location of an "opening" token matching
28783 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28784 RT_CLOSE_PAREN). */
28786 static cp_token *
28787 cp_parser_require (cp_parser* parser,
28788 enum cpp_ttype type,
28789 required_token token_desc,
28790 location_t matching_location)
28792 if (cp_lexer_next_token_is (parser->lexer, type))
28793 return cp_lexer_consume_token (parser->lexer);
28794 else
28796 /* Output the MESSAGE -- unless we're parsing tentatively. */
28797 if (!cp_parser_simulate_error (parser))
28798 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28799 matching_location);
28800 return NULL;
28804 /* An error message is produced if the next token is not '>'.
28805 All further tokens are skipped until the desired token is
28806 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28808 static void
28809 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28811 /* Current level of '< ... >'. */
28812 unsigned level = 0;
28813 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28814 unsigned nesting_depth = 0;
28816 /* Are we ready, yet? If not, issue error message. */
28817 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28818 return;
28820 /* Skip tokens until the desired token is found. */
28821 while (true)
28823 /* Peek at the next token. */
28824 switch (cp_lexer_peek_token (parser->lexer)->type)
28826 case CPP_LESS:
28827 if (!nesting_depth)
28828 ++level;
28829 break;
28831 case CPP_RSHIFT:
28832 if (cxx_dialect == cxx98)
28833 /* C++0x views the `>>' operator as two `>' tokens, but
28834 C++98 does not. */
28835 break;
28836 else if (!nesting_depth && level-- == 0)
28838 /* We've hit a `>>' where the first `>' closes the
28839 template argument list, and the second `>' is
28840 spurious. Just consume the `>>' and stop; we've
28841 already produced at least one error. */
28842 cp_lexer_consume_token (parser->lexer);
28843 return;
28845 /* Fall through for C++0x, so we handle the second `>' in
28846 the `>>'. */
28847 gcc_fallthrough ();
28849 case CPP_GREATER:
28850 if (!nesting_depth && level-- == 0)
28852 /* We've reached the token we want, consume it and stop. */
28853 cp_lexer_consume_token (parser->lexer);
28854 return;
28856 break;
28858 case CPP_OPEN_PAREN:
28859 case CPP_OPEN_SQUARE:
28860 ++nesting_depth;
28861 break;
28863 case CPP_CLOSE_PAREN:
28864 case CPP_CLOSE_SQUARE:
28865 if (nesting_depth-- == 0)
28866 return;
28867 break;
28869 case CPP_EOF:
28870 case CPP_PRAGMA_EOL:
28871 case CPP_SEMICOLON:
28872 case CPP_OPEN_BRACE:
28873 case CPP_CLOSE_BRACE:
28874 /* The '>' was probably forgotten, don't look further. */
28875 return;
28877 default:
28878 break;
28881 /* Consume this token. */
28882 cp_lexer_consume_token (parser->lexer);
28886 /* If the next token is the indicated keyword, consume it. Otherwise,
28887 issue an error message indicating that TOKEN_DESC was expected.
28889 Returns the token consumed, if the token had the appropriate type.
28890 Otherwise, returns NULL. */
28892 static cp_token *
28893 cp_parser_require_keyword (cp_parser* parser,
28894 enum rid keyword,
28895 required_token token_desc)
28897 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28899 if (token && token->keyword != keyword)
28901 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28902 UNKNOWN_LOCATION);
28903 return NULL;
28906 return token;
28909 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28910 function-definition. */
28912 static bool
28913 cp_parser_token_starts_function_definition_p (cp_token* token)
28915 return (/* An ordinary function-body begins with an `{'. */
28916 token->type == CPP_OPEN_BRACE
28917 /* A ctor-initializer begins with a `:'. */
28918 || token->type == CPP_COLON
28919 /* A function-try-block begins with `try'. */
28920 || token->keyword == RID_TRY
28921 /* A function-transaction-block begins with `__transaction_atomic'
28922 or `__transaction_relaxed'. */
28923 || token->keyword == RID_TRANSACTION_ATOMIC
28924 || token->keyword == RID_TRANSACTION_RELAXED
28925 /* The named return value extension begins with `return'. */
28926 || token->keyword == RID_RETURN);
28929 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28930 definition. */
28932 static bool
28933 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28935 cp_token *token;
28937 token = cp_lexer_peek_token (parser->lexer);
28938 return (token->type == CPP_OPEN_BRACE
28939 || (token->type == CPP_COLON
28940 && !parser->colon_doesnt_start_class_def_p));
28943 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28944 C++0x) ending a template-argument. */
28946 static bool
28947 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28949 cp_token *token;
28951 token = cp_lexer_peek_token (parser->lexer);
28952 return (token->type == CPP_COMMA
28953 || token->type == CPP_GREATER
28954 || token->type == CPP_ELLIPSIS
28955 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28958 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28959 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28961 static bool
28962 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28963 size_t n)
28965 cp_token *token;
28967 token = cp_lexer_peek_nth_token (parser->lexer, n);
28968 if (token->type == CPP_LESS)
28969 return true;
28970 /* Check for the sequence `<::' in the original code. It would be lexed as
28971 `[:', where `[' is a digraph, and there is no whitespace before
28972 `:'. */
28973 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28975 cp_token *token2;
28976 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28977 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28978 return true;
28980 return false;
28983 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28984 or none_type otherwise. */
28986 static enum tag_types
28987 cp_parser_token_is_class_key (cp_token* token)
28989 switch (token->keyword)
28991 case RID_CLASS:
28992 return class_type;
28993 case RID_STRUCT:
28994 return record_type;
28995 case RID_UNION:
28996 return union_type;
28998 default:
28999 return none_type;
29003 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29004 or none_type otherwise or if the token is null. */
29006 static enum tag_types
29007 cp_parser_token_is_type_parameter_key (cp_token* token)
29009 if (!token)
29010 return none_type;
29012 switch (token->keyword)
29014 case RID_CLASS:
29015 return class_type;
29016 case RID_TYPENAME:
29017 return typename_type;
29019 default:
29020 return none_type;
29024 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29026 static void
29027 cp_parser_check_class_key (enum tag_types class_key, tree type)
29029 if (type == error_mark_node)
29030 return;
29031 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29033 if (permerror (input_location, "%qs tag used in naming %q#T",
29034 class_key == union_type ? "union"
29035 : class_key == record_type ? "struct" : "class",
29036 type))
29037 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29038 "%q#T was previously declared here", type);
29042 /* Issue an error message if DECL is redeclared with different
29043 access than its original declaration [class.access.spec/3].
29044 This applies to nested classes, nested class templates and
29045 enumerations [class.mem/1]. */
29047 static void
29048 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29050 if (!decl
29051 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29052 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29053 return;
29055 if ((TREE_PRIVATE (decl)
29056 != (current_access_specifier == access_private_node))
29057 || (TREE_PROTECTED (decl)
29058 != (current_access_specifier == access_protected_node)))
29059 error_at (location, "%qD redeclared with different access", decl);
29062 /* Look for the `template' keyword, as a syntactic disambiguator.
29063 Return TRUE iff it is present, in which case it will be
29064 consumed. */
29066 static bool
29067 cp_parser_optional_template_keyword (cp_parser *parser)
29069 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29071 /* In C++98 the `template' keyword can only be used within templates;
29072 outside templates the parser can always figure out what is a
29073 template and what is not. In C++11, per the resolution of DR 468,
29074 `template' is allowed in cases where it is not strictly necessary. */
29075 if (!processing_template_decl
29076 && pedantic && cxx_dialect == cxx98)
29078 cp_token *token = cp_lexer_peek_token (parser->lexer);
29079 pedwarn (token->location, OPT_Wpedantic,
29080 "in C++98 %<template%> (as a disambiguator) is only "
29081 "allowed within templates");
29082 /* If this part of the token stream is rescanned, the same
29083 error message would be generated. So, we purge the token
29084 from the stream. */
29085 cp_lexer_purge_token (parser->lexer);
29086 return false;
29088 else
29090 /* Consume the `template' keyword. */
29091 cp_lexer_consume_token (parser->lexer);
29092 return true;
29095 return false;
29098 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29099 set PARSER->SCOPE, and perform other related actions. */
29101 static void
29102 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29104 struct tree_check *check_value;
29106 /* Get the stored value. */
29107 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29108 /* Set the scope from the stored value. */
29109 parser->scope = saved_checks_value (check_value);
29110 parser->qualifying_scope = check_value->qualifying_scope;
29111 parser->object_scope = NULL_TREE;
29114 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29115 encounter the end of a block before what we were looking for. */
29117 static bool
29118 cp_parser_cache_group (cp_parser *parser,
29119 enum cpp_ttype end,
29120 unsigned depth)
29122 while (true)
29124 cp_token *token = cp_lexer_peek_token (parser->lexer);
29126 /* Abort a parenthesized expression if we encounter a semicolon. */
29127 if ((end == CPP_CLOSE_PAREN || depth == 0)
29128 && token->type == CPP_SEMICOLON)
29129 return true;
29130 /* If we've reached the end of the file, stop. */
29131 if (token->type == CPP_EOF
29132 || (end != CPP_PRAGMA_EOL
29133 && token->type == CPP_PRAGMA_EOL))
29134 return true;
29135 if (token->type == CPP_CLOSE_BRACE && depth == 0)
29136 /* We've hit the end of an enclosing block, so there's been some
29137 kind of syntax error. */
29138 return true;
29140 /* Consume the token. */
29141 cp_lexer_consume_token (parser->lexer);
29142 /* See if it starts a new group. */
29143 if (token->type == CPP_OPEN_BRACE)
29145 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29146 /* In theory this should probably check end == '}', but
29147 cp_parser_save_member_function_body needs it to exit
29148 after either '}' or ')' when called with ')'. */
29149 if (depth == 0)
29150 return false;
29152 else if (token->type == CPP_OPEN_PAREN)
29154 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29155 if (depth == 0 && end == CPP_CLOSE_PAREN)
29156 return false;
29158 else if (token->type == CPP_PRAGMA)
29159 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29160 else if (token->type == end)
29161 return false;
29165 /* Like above, for caching a default argument or NSDMI. Both of these are
29166 terminated by a non-nested comma, but it can be unclear whether or not a
29167 comma is nested in a template argument list unless we do more parsing.
29168 In order to handle this ambiguity, when we encounter a ',' after a '<'
29169 we try to parse what follows as a parameter-declaration-list (in the
29170 case of a default argument) or a member-declarator (in the case of an
29171 NSDMI). If that succeeds, then we stop caching. */
29173 static tree
29174 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29176 unsigned depth = 0;
29177 int maybe_template_id = 0;
29178 cp_token *first_token;
29179 cp_token *token;
29180 tree default_argument;
29182 /* Add tokens until we have processed the entire default
29183 argument. We add the range [first_token, token). */
29184 first_token = cp_lexer_peek_token (parser->lexer);
29185 if (first_token->type == CPP_OPEN_BRACE)
29187 /* For list-initialization, this is straightforward. */
29188 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29189 token = cp_lexer_peek_token (parser->lexer);
29191 else while (true)
29193 bool done = false;
29195 /* Peek at the next token. */
29196 token = cp_lexer_peek_token (parser->lexer);
29197 /* What we do depends on what token we have. */
29198 switch (token->type)
29200 /* In valid code, a default argument must be
29201 immediately followed by a `,' `)', or `...'. */
29202 case CPP_COMMA:
29203 if (depth == 0 && maybe_template_id)
29205 /* If we've seen a '<', we might be in a
29206 template-argument-list. Until Core issue 325 is
29207 resolved, we don't know how this situation ought
29208 to be handled, so try to DTRT. We check whether
29209 what comes after the comma is a valid parameter
29210 declaration list. If it is, then the comma ends
29211 the default argument; otherwise the default
29212 argument continues. */
29213 bool error = false;
29214 cp_token *peek;
29216 /* Set ITALP so cp_parser_parameter_declaration_list
29217 doesn't decide to commit to this parse. */
29218 bool saved_italp = parser->in_template_argument_list_p;
29219 parser->in_template_argument_list_p = true;
29221 cp_parser_parse_tentatively (parser);
29223 if (nsdmi)
29225 /* Parse declarators until we reach a non-comma or
29226 somthing that cannot be an initializer.
29227 Just checking whether we're looking at a single
29228 declarator is insufficient. Consider:
29229 int var = tuple<T,U>::x;
29230 The template parameter 'U' looks exactly like a
29231 declarator. */
29234 int ctor_dtor_or_conv_p;
29235 cp_lexer_consume_token (parser->lexer);
29236 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29237 &ctor_dtor_or_conv_p,
29238 /*parenthesized_p=*/NULL,
29239 /*member_p=*/true,
29240 /*friend_p=*/false);
29241 peek = cp_lexer_peek_token (parser->lexer);
29242 if (cp_parser_error_occurred (parser))
29243 break;
29245 while (peek->type == CPP_COMMA);
29246 /* If we met an '=' or ';' then the original comma
29247 was the end of the NSDMI. Otherwise assume
29248 we're still in the NSDMI. */
29249 error = (peek->type != CPP_EQ
29250 && peek->type != CPP_SEMICOLON);
29252 else
29254 cp_lexer_consume_token (parser->lexer);
29255 begin_scope (sk_function_parms, NULL_TREE);
29256 if (cp_parser_parameter_declaration_list (parser)
29257 == error_mark_node)
29258 error = true;
29259 pop_bindings_and_leave_scope ();
29261 if (!cp_parser_error_occurred (parser) && !error)
29262 done = true;
29263 cp_parser_abort_tentative_parse (parser);
29265 parser->in_template_argument_list_p = saved_italp;
29266 break;
29268 /* FALLTHRU */
29269 case CPP_CLOSE_PAREN:
29270 case CPP_ELLIPSIS:
29271 /* If we run into a non-nested `;', `}', or `]',
29272 then the code is invalid -- but the default
29273 argument is certainly over. */
29274 case CPP_SEMICOLON:
29275 case CPP_CLOSE_BRACE:
29276 case CPP_CLOSE_SQUARE:
29277 if (depth == 0
29278 /* Handle correctly int n = sizeof ... ( p ); */
29279 && token->type != CPP_ELLIPSIS)
29280 done = true;
29281 /* Update DEPTH, if necessary. */
29282 else if (token->type == CPP_CLOSE_PAREN
29283 || token->type == CPP_CLOSE_BRACE
29284 || token->type == CPP_CLOSE_SQUARE)
29285 --depth;
29286 break;
29288 case CPP_OPEN_PAREN:
29289 case CPP_OPEN_SQUARE:
29290 case CPP_OPEN_BRACE:
29291 ++depth;
29292 break;
29294 case CPP_LESS:
29295 if (depth == 0)
29296 /* This might be the comparison operator, or it might
29297 start a template argument list. */
29298 ++maybe_template_id;
29299 break;
29301 case CPP_RSHIFT:
29302 if (cxx_dialect == cxx98)
29303 break;
29304 /* Fall through for C++0x, which treats the `>>'
29305 operator like two `>' tokens in certain
29306 cases. */
29307 gcc_fallthrough ();
29309 case CPP_GREATER:
29310 if (depth == 0)
29312 /* This might be an operator, or it might close a
29313 template argument list. But if a previous '<'
29314 started a template argument list, this will have
29315 closed it, so we can't be in one anymore. */
29316 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29317 if (maybe_template_id < 0)
29318 maybe_template_id = 0;
29320 break;
29322 /* If we run out of tokens, issue an error message. */
29323 case CPP_EOF:
29324 case CPP_PRAGMA_EOL:
29325 error_at (token->location, "file ends in default argument");
29326 return error_mark_node;
29328 case CPP_NAME:
29329 case CPP_SCOPE:
29330 /* In these cases, we should look for template-ids.
29331 For example, if the default argument is
29332 `X<int, double>()', we need to do name lookup to
29333 figure out whether or not `X' is a template; if
29334 so, the `,' does not end the default argument.
29336 That is not yet done. */
29337 break;
29339 default:
29340 break;
29343 /* If we've reached the end, stop. */
29344 if (done)
29345 break;
29347 /* Add the token to the token block. */
29348 token = cp_lexer_consume_token (parser->lexer);
29351 /* Create a DEFAULT_ARG to represent the unparsed default
29352 argument. */
29353 default_argument = make_node (DEFAULT_ARG);
29354 DEFARG_TOKENS (default_argument)
29355 = cp_token_cache_new (first_token, token);
29356 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29358 return default_argument;
29361 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29363 location_t
29364 defarg_location (tree default_argument)
29366 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29367 location_t start = tokens->first->location;
29368 location_t end = tokens->last->location;
29369 return make_location (start, start, end);
29372 /* Begin parsing tentatively. We always save tokens while parsing
29373 tentatively so that if the tentative parsing fails we can restore the
29374 tokens. */
29376 static void
29377 cp_parser_parse_tentatively (cp_parser* parser)
29379 /* Enter a new parsing context. */
29380 parser->context = cp_parser_context_new (parser->context);
29381 /* Begin saving tokens. */
29382 cp_lexer_save_tokens (parser->lexer);
29383 /* In order to avoid repetitive access control error messages,
29384 access checks are queued up until we are no longer parsing
29385 tentatively. */
29386 push_deferring_access_checks (dk_deferred);
29389 /* Commit to the currently active tentative parse. */
29391 static void
29392 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29394 cp_parser_context *context;
29395 cp_lexer *lexer;
29397 /* Mark all of the levels as committed. */
29398 lexer = parser->lexer;
29399 for (context = parser->context; context->next; context = context->next)
29401 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29402 break;
29403 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29404 while (!cp_lexer_saving_tokens (lexer))
29405 lexer = lexer->next;
29406 cp_lexer_commit_tokens (lexer);
29410 /* Commit to the topmost currently active tentative parse.
29412 Note that this function shouldn't be called when there are
29413 irreversible side-effects while in a tentative state. For
29414 example, we shouldn't create a permanent entry in the symbol
29415 table, or issue an error message that might not apply if the
29416 tentative parse is aborted. */
29418 static void
29419 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29421 cp_parser_context *context = parser->context;
29422 cp_lexer *lexer = parser->lexer;
29424 if (context)
29426 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29427 return;
29428 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29430 while (!cp_lexer_saving_tokens (lexer))
29431 lexer = lexer->next;
29432 cp_lexer_commit_tokens (lexer);
29436 /* Abort the currently active tentative parse. All consumed tokens
29437 will be rolled back, and no diagnostics will be issued. */
29439 static void
29440 cp_parser_abort_tentative_parse (cp_parser* parser)
29442 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29443 || errorcount > 0);
29444 cp_parser_simulate_error (parser);
29445 /* Now, pretend that we want to see if the construct was
29446 successfully parsed. */
29447 cp_parser_parse_definitely (parser);
29450 /* Stop parsing tentatively. If a parse error has occurred, restore the
29451 token stream. Otherwise, commit to the tokens we have consumed.
29452 Returns true if no error occurred; false otherwise. */
29454 static bool
29455 cp_parser_parse_definitely (cp_parser* parser)
29457 bool error_occurred;
29458 cp_parser_context *context;
29460 /* Remember whether or not an error occurred, since we are about to
29461 destroy that information. */
29462 error_occurred = cp_parser_error_occurred (parser);
29463 /* Remove the topmost context from the stack. */
29464 context = parser->context;
29465 parser->context = context->next;
29466 /* If no parse errors occurred, commit to the tentative parse. */
29467 if (!error_occurred)
29469 /* Commit to the tokens read tentatively, unless that was
29470 already done. */
29471 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29472 cp_lexer_commit_tokens (parser->lexer);
29474 pop_to_parent_deferring_access_checks ();
29476 /* Otherwise, if errors occurred, roll back our state so that things
29477 are just as they were before we began the tentative parse. */
29478 else
29480 cp_lexer_rollback_tokens (parser->lexer);
29481 pop_deferring_access_checks ();
29483 /* Add the context to the front of the free list. */
29484 context->next = cp_parser_context_free_list;
29485 cp_parser_context_free_list = context;
29487 return !error_occurred;
29490 /* Returns true if we are parsing tentatively and are not committed to
29491 this tentative parse. */
29493 static bool
29494 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29496 return (cp_parser_parsing_tentatively (parser)
29497 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29500 /* Returns nonzero iff an error has occurred during the most recent
29501 tentative parse. */
29503 static bool
29504 cp_parser_error_occurred (cp_parser* parser)
29506 return (cp_parser_parsing_tentatively (parser)
29507 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29510 /* Returns nonzero if GNU extensions are allowed. */
29512 static bool
29513 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29515 return parser->allow_gnu_extensions_p;
29518 /* Objective-C++ Productions */
29521 /* Parse an Objective-C expression, which feeds into a primary-expression
29522 above.
29524 objc-expression:
29525 objc-message-expression
29526 objc-string-literal
29527 objc-encode-expression
29528 objc-protocol-expression
29529 objc-selector-expression
29531 Returns a tree representation of the expression. */
29533 static cp_expr
29534 cp_parser_objc_expression (cp_parser* parser)
29536 /* Try to figure out what kind of declaration is present. */
29537 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29539 switch (kwd->type)
29541 case CPP_OPEN_SQUARE:
29542 return cp_parser_objc_message_expression (parser);
29544 case CPP_OBJC_STRING:
29545 kwd = cp_lexer_consume_token (parser->lexer);
29546 return objc_build_string_object (kwd->u.value);
29548 case CPP_KEYWORD:
29549 switch (kwd->keyword)
29551 case RID_AT_ENCODE:
29552 return cp_parser_objc_encode_expression (parser);
29554 case RID_AT_PROTOCOL:
29555 return cp_parser_objc_protocol_expression (parser);
29557 case RID_AT_SELECTOR:
29558 return cp_parser_objc_selector_expression (parser);
29560 default:
29561 break;
29563 /* FALLTHRU */
29564 default:
29565 error_at (kwd->location,
29566 "misplaced %<@%D%> Objective-C++ construct",
29567 kwd->u.value);
29568 cp_parser_skip_to_end_of_block_or_statement (parser);
29571 return error_mark_node;
29574 /* Parse an Objective-C message expression.
29576 objc-message-expression:
29577 [ objc-message-receiver objc-message-args ]
29579 Returns a representation of an Objective-C message. */
29581 static tree
29582 cp_parser_objc_message_expression (cp_parser* parser)
29584 tree receiver, messageargs;
29586 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29587 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29588 receiver = cp_parser_objc_message_receiver (parser);
29589 messageargs = cp_parser_objc_message_args (parser);
29590 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29591 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29593 tree result = objc_build_message_expr (receiver, messageargs);
29595 /* Construct a location e.g.
29596 [self func1:5]
29597 ^~~~~~~~~~~~~~
29598 ranging from the '[' to the ']', with the caret at the start. */
29599 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29600 protected_set_expr_location (result, combined_loc);
29602 return result;
29605 /* Parse an objc-message-receiver.
29607 objc-message-receiver:
29608 expression
29609 simple-type-specifier
29611 Returns a representation of the type or expression. */
29613 static tree
29614 cp_parser_objc_message_receiver (cp_parser* parser)
29616 tree rcv;
29618 /* An Objective-C message receiver may be either (1) a type
29619 or (2) an expression. */
29620 cp_parser_parse_tentatively (parser);
29621 rcv = cp_parser_expression (parser);
29623 /* If that worked out, fine. */
29624 if (cp_parser_parse_definitely (parser))
29625 return rcv;
29627 cp_parser_parse_tentatively (parser);
29628 rcv = cp_parser_simple_type_specifier (parser,
29629 /*decl_specs=*/NULL,
29630 CP_PARSER_FLAGS_NONE);
29632 if (cp_parser_parse_definitely (parser))
29633 return objc_get_class_reference (rcv);
29635 cp_parser_error (parser, "objective-c++ message receiver expected");
29636 return error_mark_node;
29639 /* Parse the arguments and selectors comprising an Objective-C message.
29641 objc-message-args:
29642 objc-selector
29643 objc-selector-args
29644 objc-selector-args , objc-comma-args
29646 objc-selector-args:
29647 objc-selector [opt] : assignment-expression
29648 objc-selector-args objc-selector [opt] : assignment-expression
29650 objc-comma-args:
29651 assignment-expression
29652 objc-comma-args , assignment-expression
29654 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29655 selector arguments and TREE_VALUE containing a list of comma
29656 arguments. */
29658 static tree
29659 cp_parser_objc_message_args (cp_parser* parser)
29661 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29662 bool maybe_unary_selector_p = true;
29663 cp_token *token = cp_lexer_peek_token (parser->lexer);
29665 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29667 tree selector = NULL_TREE, arg;
29669 if (token->type != CPP_COLON)
29670 selector = cp_parser_objc_selector (parser);
29672 /* Detect if we have a unary selector. */
29673 if (maybe_unary_selector_p
29674 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29675 return build_tree_list (selector, NULL_TREE);
29677 maybe_unary_selector_p = false;
29678 cp_parser_require (parser, CPP_COLON, RT_COLON);
29679 arg = cp_parser_assignment_expression (parser);
29681 sel_args
29682 = chainon (sel_args,
29683 build_tree_list (selector, arg));
29685 token = cp_lexer_peek_token (parser->lexer);
29688 /* Handle non-selector arguments, if any. */
29689 while (token->type == CPP_COMMA)
29691 tree arg;
29693 cp_lexer_consume_token (parser->lexer);
29694 arg = cp_parser_assignment_expression (parser);
29696 addl_args
29697 = chainon (addl_args,
29698 build_tree_list (NULL_TREE, arg));
29700 token = cp_lexer_peek_token (parser->lexer);
29703 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29705 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29706 return build_tree_list (error_mark_node, error_mark_node);
29709 return build_tree_list (sel_args, addl_args);
29712 /* Parse an Objective-C encode expression.
29714 objc-encode-expression:
29715 @encode objc-typename
29717 Returns an encoded representation of the type argument. */
29719 static cp_expr
29720 cp_parser_objc_encode_expression (cp_parser* parser)
29722 tree type;
29723 cp_token *token;
29724 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29726 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29727 matching_parens parens;
29728 parens.require_open (parser);
29729 token = cp_lexer_peek_token (parser->lexer);
29730 type = complete_type (cp_parser_type_id (parser));
29731 parens.require_close (parser);
29733 if (!type)
29735 error_at (token->location,
29736 "%<@encode%> must specify a type as an argument");
29737 return error_mark_node;
29740 /* This happens if we find @encode(T) (where T is a template
29741 typename or something dependent on a template typename) when
29742 parsing a template. In that case, we can't compile it
29743 immediately, but we rather create an AT_ENCODE_EXPR which will
29744 need to be instantiated when the template is used.
29746 if (dependent_type_p (type))
29748 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29749 TREE_READONLY (value) = 1;
29750 return value;
29754 /* Build a location of the form:
29755 @encode(int)
29756 ^~~~~~~~~~~~
29757 with caret==start at the @ token, finishing at the close paren. */
29758 location_t combined_loc
29759 = make_location (start_loc, start_loc,
29760 cp_lexer_previous_token (parser->lexer)->location);
29762 return cp_expr (objc_build_encode_expr (type), combined_loc);
29765 /* Parse an Objective-C @defs expression. */
29767 static tree
29768 cp_parser_objc_defs_expression (cp_parser *parser)
29770 tree name;
29772 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29773 matching_parens parens;
29774 parens.require_open (parser);
29775 name = cp_parser_identifier (parser);
29776 parens.require_close (parser);
29778 return objc_get_class_ivars (name);
29781 /* Parse an Objective-C protocol expression.
29783 objc-protocol-expression:
29784 @protocol ( identifier )
29786 Returns a representation of the protocol expression. */
29788 static tree
29789 cp_parser_objc_protocol_expression (cp_parser* parser)
29791 tree proto;
29792 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29794 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29795 matching_parens parens;
29796 parens.require_open (parser);
29797 proto = cp_parser_identifier (parser);
29798 parens.require_close (parser);
29800 /* Build a location of the form:
29801 @protocol(prot)
29802 ^~~~~~~~~~~~~~~
29803 with caret==start at the @ token, finishing at the close paren. */
29804 location_t combined_loc
29805 = make_location (start_loc, start_loc,
29806 cp_lexer_previous_token (parser->lexer)->location);
29807 tree result = objc_build_protocol_expr (proto);
29808 protected_set_expr_location (result, combined_loc);
29809 return result;
29812 /* Parse an Objective-C selector expression.
29814 objc-selector-expression:
29815 @selector ( objc-method-signature )
29817 objc-method-signature:
29818 objc-selector
29819 objc-selector-seq
29821 objc-selector-seq:
29822 objc-selector :
29823 objc-selector-seq objc-selector :
29825 Returns a representation of the method selector. */
29827 static tree
29828 cp_parser_objc_selector_expression (cp_parser* parser)
29830 tree sel_seq = NULL_TREE;
29831 bool maybe_unary_selector_p = true;
29832 cp_token *token;
29833 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29835 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29836 matching_parens parens;
29837 parens.require_open (parser);
29838 token = cp_lexer_peek_token (parser->lexer);
29840 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29841 || token->type == CPP_SCOPE)
29843 tree selector = NULL_TREE;
29845 if (token->type != CPP_COLON
29846 || token->type == CPP_SCOPE)
29847 selector = cp_parser_objc_selector (parser);
29849 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29850 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29852 /* Detect if we have a unary selector. */
29853 if (maybe_unary_selector_p)
29855 sel_seq = selector;
29856 goto finish_selector;
29858 else
29860 cp_parser_error (parser, "expected %<:%>");
29863 maybe_unary_selector_p = false;
29864 token = cp_lexer_consume_token (parser->lexer);
29866 if (token->type == CPP_SCOPE)
29868 sel_seq
29869 = chainon (sel_seq,
29870 build_tree_list (selector, NULL_TREE));
29871 sel_seq
29872 = chainon (sel_seq,
29873 build_tree_list (NULL_TREE, NULL_TREE));
29875 else
29876 sel_seq
29877 = chainon (sel_seq,
29878 build_tree_list (selector, NULL_TREE));
29880 token = cp_lexer_peek_token (parser->lexer);
29883 finish_selector:
29884 parens.require_close (parser);
29887 /* Build a location of the form:
29888 @selector(func)
29889 ^~~~~~~~~~~~~~~
29890 with caret==start at the @ token, finishing at the close paren. */
29891 location_t combined_loc
29892 = make_location (loc, loc,
29893 cp_lexer_previous_token (parser->lexer)->location);
29894 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29895 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29896 protected_set_expr_location (result, combined_loc);
29897 return result;
29900 /* Parse a list of identifiers.
29902 objc-identifier-list:
29903 identifier
29904 objc-identifier-list , identifier
29906 Returns a TREE_LIST of identifier nodes. */
29908 static tree
29909 cp_parser_objc_identifier_list (cp_parser* parser)
29911 tree identifier;
29912 tree list;
29913 cp_token *sep;
29915 identifier = cp_parser_identifier (parser);
29916 if (identifier == error_mark_node)
29917 return error_mark_node;
29919 list = build_tree_list (NULL_TREE, identifier);
29920 sep = cp_lexer_peek_token (parser->lexer);
29922 while (sep->type == CPP_COMMA)
29924 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29925 identifier = cp_parser_identifier (parser);
29926 if (identifier == error_mark_node)
29927 return list;
29929 list = chainon (list, build_tree_list (NULL_TREE,
29930 identifier));
29931 sep = cp_lexer_peek_token (parser->lexer);
29934 return list;
29937 /* Parse an Objective-C alias declaration.
29939 objc-alias-declaration:
29940 @compatibility_alias identifier identifier ;
29942 This function registers the alias mapping with the Objective-C front end.
29943 It returns nothing. */
29945 static void
29946 cp_parser_objc_alias_declaration (cp_parser* parser)
29948 tree alias, orig;
29950 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29951 alias = cp_parser_identifier (parser);
29952 orig = cp_parser_identifier (parser);
29953 objc_declare_alias (alias, orig);
29954 cp_parser_consume_semicolon_at_end_of_statement (parser);
29957 /* Parse an Objective-C class forward-declaration.
29959 objc-class-declaration:
29960 @class objc-identifier-list ;
29962 The function registers the forward declarations with the Objective-C
29963 front end. It returns nothing. */
29965 static void
29966 cp_parser_objc_class_declaration (cp_parser* parser)
29968 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29969 while (true)
29971 tree id;
29973 id = cp_parser_identifier (parser);
29974 if (id == error_mark_node)
29975 break;
29977 objc_declare_class (id);
29979 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29980 cp_lexer_consume_token (parser->lexer);
29981 else
29982 break;
29984 cp_parser_consume_semicolon_at_end_of_statement (parser);
29987 /* Parse a list of Objective-C protocol references.
29989 objc-protocol-refs-opt:
29990 objc-protocol-refs [opt]
29992 objc-protocol-refs:
29993 < objc-identifier-list >
29995 Returns a TREE_LIST of identifiers, if any. */
29997 static tree
29998 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30000 tree protorefs = NULL_TREE;
30002 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30004 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30005 protorefs = cp_parser_objc_identifier_list (parser);
30006 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30009 return protorefs;
30012 /* Parse a Objective-C visibility specification. */
30014 static void
30015 cp_parser_objc_visibility_spec (cp_parser* parser)
30017 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30019 switch (vis->keyword)
30021 case RID_AT_PRIVATE:
30022 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30023 break;
30024 case RID_AT_PROTECTED:
30025 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30026 break;
30027 case RID_AT_PUBLIC:
30028 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30029 break;
30030 case RID_AT_PACKAGE:
30031 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30032 break;
30033 default:
30034 return;
30037 /* Eat '@private'/'@protected'/'@public'. */
30038 cp_lexer_consume_token (parser->lexer);
30041 /* Parse an Objective-C method type. Return 'true' if it is a class
30042 (+) method, and 'false' if it is an instance (-) method. */
30044 static inline bool
30045 cp_parser_objc_method_type (cp_parser* parser)
30047 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30048 return true;
30049 else
30050 return false;
30053 /* Parse an Objective-C protocol qualifier. */
30055 static tree
30056 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30058 tree quals = NULL_TREE, node;
30059 cp_token *token = cp_lexer_peek_token (parser->lexer);
30061 node = token->u.value;
30063 while (node && identifier_p (node)
30064 && (node == ridpointers [(int) RID_IN]
30065 || node == ridpointers [(int) RID_OUT]
30066 || node == ridpointers [(int) RID_INOUT]
30067 || node == ridpointers [(int) RID_BYCOPY]
30068 || node == ridpointers [(int) RID_BYREF]
30069 || node == ridpointers [(int) RID_ONEWAY]))
30071 quals = tree_cons (NULL_TREE, node, quals);
30072 cp_lexer_consume_token (parser->lexer);
30073 token = cp_lexer_peek_token (parser->lexer);
30074 node = token->u.value;
30077 return quals;
30080 /* Parse an Objective-C typename. */
30082 static tree
30083 cp_parser_objc_typename (cp_parser* parser)
30085 tree type_name = NULL_TREE;
30087 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30089 tree proto_quals, cp_type = NULL_TREE;
30091 matching_parens parens;
30092 parens.consume_open (parser); /* Eat '('. */
30093 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30095 /* An ObjC type name may consist of just protocol qualifiers, in which
30096 case the type shall default to 'id'. */
30097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30099 cp_type = cp_parser_type_id (parser);
30101 /* If the type could not be parsed, an error has already
30102 been produced. For error recovery, behave as if it had
30103 not been specified, which will use the default type
30104 'id'. */
30105 if (cp_type == error_mark_node)
30107 cp_type = NULL_TREE;
30108 /* We need to skip to the closing parenthesis as
30109 cp_parser_type_id() does not seem to do it for
30110 us. */
30111 cp_parser_skip_to_closing_parenthesis (parser,
30112 /*recovering=*/true,
30113 /*or_comma=*/false,
30114 /*consume_paren=*/false);
30118 parens.require_close (parser);
30119 type_name = build_tree_list (proto_quals, cp_type);
30122 return type_name;
30125 /* Check to see if TYPE refers to an Objective-C selector name. */
30127 static bool
30128 cp_parser_objc_selector_p (enum cpp_ttype type)
30130 return (type == CPP_NAME || type == CPP_KEYWORD
30131 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30132 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30133 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30134 || type == CPP_XOR || type == CPP_XOR_EQ);
30137 /* Parse an Objective-C selector. */
30139 static tree
30140 cp_parser_objc_selector (cp_parser* parser)
30142 cp_token *token = cp_lexer_consume_token (parser->lexer);
30144 if (!cp_parser_objc_selector_p (token->type))
30146 error_at (token->location, "invalid Objective-C++ selector name");
30147 return error_mark_node;
30150 /* C++ operator names are allowed to appear in ObjC selectors. */
30151 switch (token->type)
30153 case CPP_AND_AND: return get_identifier ("and");
30154 case CPP_AND_EQ: return get_identifier ("and_eq");
30155 case CPP_AND: return get_identifier ("bitand");
30156 case CPP_OR: return get_identifier ("bitor");
30157 case CPP_COMPL: return get_identifier ("compl");
30158 case CPP_NOT: return get_identifier ("not");
30159 case CPP_NOT_EQ: return get_identifier ("not_eq");
30160 case CPP_OR_OR: return get_identifier ("or");
30161 case CPP_OR_EQ: return get_identifier ("or_eq");
30162 case CPP_XOR: return get_identifier ("xor");
30163 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30164 default: return token->u.value;
30168 /* Parse an Objective-C params list. */
30170 static tree
30171 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30173 tree params = NULL_TREE;
30174 bool maybe_unary_selector_p = true;
30175 cp_token *token = cp_lexer_peek_token (parser->lexer);
30177 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30179 tree selector = NULL_TREE, type_name, identifier;
30180 tree parm_attr = NULL_TREE;
30182 if (token->keyword == RID_ATTRIBUTE)
30183 break;
30185 if (token->type != CPP_COLON)
30186 selector = cp_parser_objc_selector (parser);
30188 /* Detect if we have a unary selector. */
30189 if (maybe_unary_selector_p
30190 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30192 params = selector; /* Might be followed by attributes. */
30193 break;
30196 maybe_unary_selector_p = false;
30197 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30199 /* Something went quite wrong. There should be a colon
30200 here, but there is not. Stop parsing parameters. */
30201 break;
30203 type_name = cp_parser_objc_typename (parser);
30204 /* New ObjC allows attributes on parameters too. */
30205 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30206 parm_attr = cp_parser_attributes_opt (parser);
30207 identifier = cp_parser_identifier (parser);
30209 params
30210 = chainon (params,
30211 objc_build_keyword_decl (selector,
30212 type_name,
30213 identifier,
30214 parm_attr));
30216 token = cp_lexer_peek_token (parser->lexer);
30219 if (params == NULL_TREE)
30221 cp_parser_error (parser, "objective-c++ method declaration is expected");
30222 return error_mark_node;
30225 /* We allow tail attributes for the method. */
30226 if (token->keyword == RID_ATTRIBUTE)
30228 *attributes = cp_parser_attributes_opt (parser);
30229 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30230 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30231 return params;
30232 cp_parser_error (parser,
30233 "method attributes must be specified at the end");
30234 return error_mark_node;
30237 if (params == NULL_TREE)
30239 cp_parser_error (parser, "objective-c++ method declaration is expected");
30240 return error_mark_node;
30242 return params;
30245 /* Parse the non-keyword Objective-C params. */
30247 static tree
30248 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30249 tree* attributes)
30251 tree params = make_node (TREE_LIST);
30252 cp_token *token = cp_lexer_peek_token (parser->lexer);
30253 *ellipsisp = false; /* Initially, assume no ellipsis. */
30255 while (token->type == CPP_COMMA)
30257 cp_parameter_declarator *parmdecl;
30258 tree parm;
30260 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30261 token = cp_lexer_peek_token (parser->lexer);
30263 if (token->type == CPP_ELLIPSIS)
30265 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30266 *ellipsisp = true;
30267 token = cp_lexer_peek_token (parser->lexer);
30268 break;
30271 /* TODO: parse attributes for tail parameters. */
30272 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30273 parm = grokdeclarator (parmdecl->declarator,
30274 &parmdecl->decl_specifiers,
30275 PARM, /*initialized=*/0,
30276 /*attrlist=*/NULL);
30278 chainon (params, build_tree_list (NULL_TREE, parm));
30279 token = cp_lexer_peek_token (parser->lexer);
30282 /* We allow tail attributes for the method. */
30283 if (token->keyword == RID_ATTRIBUTE)
30285 if (*attributes == NULL_TREE)
30287 *attributes = cp_parser_attributes_opt (parser);
30288 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30289 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30290 return params;
30292 else
30293 /* We have an error, but parse the attributes, so that we can
30294 carry on. */
30295 *attributes = cp_parser_attributes_opt (parser);
30297 cp_parser_error (parser,
30298 "method attributes must be specified at the end");
30299 return error_mark_node;
30302 return params;
30305 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30307 static void
30308 cp_parser_objc_interstitial_code (cp_parser* parser)
30310 cp_token *token = cp_lexer_peek_token (parser->lexer);
30312 /* If the next token is `extern' and the following token is a string
30313 literal, then we have a linkage specification. */
30314 if (token->keyword == RID_EXTERN
30315 && cp_parser_is_pure_string_literal
30316 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30317 cp_parser_linkage_specification (parser);
30318 /* Handle #pragma, if any. */
30319 else if (token->type == CPP_PRAGMA)
30320 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30321 /* Allow stray semicolons. */
30322 else if (token->type == CPP_SEMICOLON)
30323 cp_lexer_consume_token (parser->lexer);
30324 /* Mark methods as optional or required, when building protocols. */
30325 else if (token->keyword == RID_AT_OPTIONAL)
30327 cp_lexer_consume_token (parser->lexer);
30328 objc_set_method_opt (true);
30330 else if (token->keyword == RID_AT_REQUIRED)
30332 cp_lexer_consume_token (parser->lexer);
30333 objc_set_method_opt (false);
30335 else if (token->keyword == RID_NAMESPACE)
30336 cp_parser_namespace_definition (parser);
30337 /* Other stray characters must generate errors. */
30338 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30340 cp_lexer_consume_token (parser->lexer);
30341 error ("stray %qs between Objective-C++ methods",
30342 token->type == CPP_OPEN_BRACE ? "{" : "}");
30344 /* Finally, try to parse a block-declaration, or a function-definition. */
30345 else
30346 cp_parser_block_declaration (parser, /*statement_p=*/false);
30349 /* Parse a method signature. */
30351 static tree
30352 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30354 tree rettype, kwdparms, optparms;
30355 bool ellipsis = false;
30356 bool is_class_method;
30358 is_class_method = cp_parser_objc_method_type (parser);
30359 rettype = cp_parser_objc_typename (parser);
30360 *attributes = NULL_TREE;
30361 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30362 if (kwdparms == error_mark_node)
30363 return error_mark_node;
30364 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30365 if (optparms == error_mark_node)
30366 return error_mark_node;
30368 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30371 static bool
30372 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30374 tree tattr;
30375 cp_lexer_save_tokens (parser->lexer);
30376 tattr = cp_parser_attributes_opt (parser);
30377 gcc_assert (tattr) ;
30379 /* If the attributes are followed by a method introducer, this is not allowed.
30380 Dump the attributes and flag the situation. */
30381 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30382 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30383 return true;
30385 /* Otherwise, the attributes introduce some interstitial code, possibly so
30386 rewind to allow that check. */
30387 cp_lexer_rollback_tokens (parser->lexer);
30388 return false;
30391 /* Parse an Objective-C method prototype list. */
30393 static void
30394 cp_parser_objc_method_prototype_list (cp_parser* parser)
30396 cp_token *token = cp_lexer_peek_token (parser->lexer);
30398 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30400 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30402 tree attributes, sig;
30403 bool is_class_method;
30404 if (token->type == CPP_PLUS)
30405 is_class_method = true;
30406 else
30407 is_class_method = false;
30408 sig = cp_parser_objc_method_signature (parser, &attributes);
30409 if (sig == error_mark_node)
30411 cp_parser_skip_to_end_of_block_or_statement (parser);
30412 token = cp_lexer_peek_token (parser->lexer);
30413 continue;
30415 objc_add_method_declaration (is_class_method, sig, attributes);
30416 cp_parser_consume_semicolon_at_end_of_statement (parser);
30418 else if (token->keyword == RID_AT_PROPERTY)
30419 cp_parser_objc_at_property_declaration (parser);
30420 else if (token->keyword == RID_ATTRIBUTE
30421 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30422 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30423 OPT_Wattributes,
30424 "prefix attributes are ignored for methods");
30425 else
30426 /* Allow for interspersed non-ObjC++ code. */
30427 cp_parser_objc_interstitial_code (parser);
30429 token = cp_lexer_peek_token (parser->lexer);
30432 if (token->type != CPP_EOF)
30433 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30434 else
30435 cp_parser_error (parser, "expected %<@end%>");
30437 objc_finish_interface ();
30440 /* Parse an Objective-C method definition list. */
30442 static void
30443 cp_parser_objc_method_definition_list (cp_parser* parser)
30445 cp_token *token = cp_lexer_peek_token (parser->lexer);
30447 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30449 tree meth;
30451 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30453 cp_token *ptk;
30454 tree sig, attribute;
30455 bool is_class_method;
30456 if (token->type == CPP_PLUS)
30457 is_class_method = true;
30458 else
30459 is_class_method = false;
30460 push_deferring_access_checks (dk_deferred);
30461 sig = cp_parser_objc_method_signature (parser, &attribute);
30462 if (sig == error_mark_node)
30464 cp_parser_skip_to_end_of_block_or_statement (parser);
30465 token = cp_lexer_peek_token (parser->lexer);
30466 continue;
30468 objc_start_method_definition (is_class_method, sig, attribute,
30469 NULL_TREE);
30471 /* For historical reasons, we accept an optional semicolon. */
30472 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30473 cp_lexer_consume_token (parser->lexer);
30475 ptk = cp_lexer_peek_token (parser->lexer);
30476 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30477 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30479 perform_deferred_access_checks (tf_warning_or_error);
30480 stop_deferring_access_checks ();
30481 meth = cp_parser_function_definition_after_declarator (parser,
30482 false);
30483 pop_deferring_access_checks ();
30484 objc_finish_method_definition (meth);
30487 /* The following case will be removed once @synthesize is
30488 completely implemented. */
30489 else if (token->keyword == RID_AT_PROPERTY)
30490 cp_parser_objc_at_property_declaration (parser);
30491 else if (token->keyword == RID_AT_SYNTHESIZE)
30492 cp_parser_objc_at_synthesize_declaration (parser);
30493 else if (token->keyword == RID_AT_DYNAMIC)
30494 cp_parser_objc_at_dynamic_declaration (parser);
30495 else if (token->keyword == RID_ATTRIBUTE
30496 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30497 warning_at (token->location, OPT_Wattributes,
30498 "prefix attributes are ignored for methods");
30499 else
30500 /* Allow for interspersed non-ObjC++ code. */
30501 cp_parser_objc_interstitial_code (parser);
30503 token = cp_lexer_peek_token (parser->lexer);
30506 if (token->type != CPP_EOF)
30507 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30508 else
30509 cp_parser_error (parser, "expected %<@end%>");
30511 objc_finish_implementation ();
30514 /* Parse Objective-C ivars. */
30516 static void
30517 cp_parser_objc_class_ivars (cp_parser* parser)
30519 cp_token *token = cp_lexer_peek_token (parser->lexer);
30521 if (token->type != CPP_OPEN_BRACE)
30522 return; /* No ivars specified. */
30524 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30525 token = cp_lexer_peek_token (parser->lexer);
30527 while (token->type != CPP_CLOSE_BRACE
30528 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30530 cp_decl_specifier_seq declspecs;
30531 int decl_class_or_enum_p;
30532 tree prefix_attributes;
30534 cp_parser_objc_visibility_spec (parser);
30536 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30537 break;
30539 cp_parser_decl_specifier_seq (parser,
30540 CP_PARSER_FLAGS_OPTIONAL,
30541 &declspecs,
30542 &decl_class_or_enum_p);
30544 /* auto, register, static, extern, mutable. */
30545 if (declspecs.storage_class != sc_none)
30547 cp_parser_error (parser, "invalid type for instance variable");
30548 declspecs.storage_class = sc_none;
30551 /* thread_local. */
30552 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30554 cp_parser_error (parser, "invalid type for instance variable");
30555 declspecs.locations[ds_thread] = 0;
30558 /* typedef. */
30559 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30561 cp_parser_error (parser, "invalid type for instance variable");
30562 declspecs.locations[ds_typedef] = 0;
30565 prefix_attributes = declspecs.attributes;
30566 declspecs.attributes = NULL_TREE;
30568 /* Keep going until we hit the `;' at the end of the
30569 declaration. */
30570 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30572 tree width = NULL_TREE, attributes, first_attribute, decl;
30573 cp_declarator *declarator = NULL;
30574 int ctor_dtor_or_conv_p;
30576 /* Check for a (possibly unnamed) bitfield declaration. */
30577 token = cp_lexer_peek_token (parser->lexer);
30578 if (token->type == CPP_COLON)
30579 goto eat_colon;
30581 if (token->type == CPP_NAME
30582 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30583 == CPP_COLON))
30585 /* Get the name of the bitfield. */
30586 declarator = make_id_declarator (NULL_TREE,
30587 cp_parser_identifier (parser),
30588 sfk_none);
30590 eat_colon:
30591 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30592 /* Get the width of the bitfield. */
30593 width
30594 = cp_parser_constant_expression (parser);
30596 else
30598 /* Parse the declarator. */
30599 declarator
30600 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30601 &ctor_dtor_or_conv_p,
30602 /*parenthesized_p=*/NULL,
30603 /*member_p=*/false,
30604 /*friend_p=*/false);
30607 /* Look for attributes that apply to the ivar. */
30608 attributes = cp_parser_attributes_opt (parser);
30609 /* Remember which attributes are prefix attributes and
30610 which are not. */
30611 first_attribute = attributes;
30612 /* Combine the attributes. */
30613 attributes = attr_chainon (prefix_attributes, attributes);
30615 if (width)
30616 /* Create the bitfield declaration. */
30617 decl = grokbitfield (declarator, &declspecs,
30618 width, NULL_TREE, attributes);
30619 else
30620 decl = grokfield (declarator, &declspecs,
30621 NULL_TREE, /*init_const_expr_p=*/false,
30622 NULL_TREE, attributes);
30624 /* Add the instance variable. */
30625 if (decl != error_mark_node && decl != NULL_TREE)
30626 objc_add_instance_variable (decl);
30628 /* Reset PREFIX_ATTRIBUTES. */
30629 if (attributes != error_mark_node)
30631 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30632 attributes = TREE_CHAIN (attributes);
30633 if (attributes)
30634 TREE_CHAIN (attributes) = NULL_TREE;
30637 token = cp_lexer_peek_token (parser->lexer);
30639 if (token->type == CPP_COMMA)
30641 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30642 continue;
30644 break;
30647 cp_parser_consume_semicolon_at_end_of_statement (parser);
30648 token = cp_lexer_peek_token (parser->lexer);
30651 if (token->keyword == RID_AT_END)
30652 cp_parser_error (parser, "expected %<}%>");
30654 /* Do not consume the RID_AT_END, so it will be read again as terminating
30655 the @interface of @implementation. */
30656 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30657 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30659 /* For historical reasons, we accept an optional semicolon. */
30660 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30661 cp_lexer_consume_token (parser->lexer);
30664 /* Parse an Objective-C protocol declaration. */
30666 static void
30667 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30669 tree proto, protorefs;
30670 cp_token *tok;
30672 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30673 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30675 tok = cp_lexer_peek_token (parser->lexer);
30676 error_at (tok->location, "identifier expected after %<@protocol%>");
30677 cp_parser_consume_semicolon_at_end_of_statement (parser);
30678 return;
30681 /* See if we have a forward declaration or a definition. */
30682 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30684 /* Try a forward declaration first. */
30685 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30687 while (true)
30689 tree id;
30691 id = cp_parser_identifier (parser);
30692 if (id == error_mark_node)
30693 break;
30695 objc_declare_protocol (id, attributes);
30697 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30698 cp_lexer_consume_token (parser->lexer);
30699 else
30700 break;
30702 cp_parser_consume_semicolon_at_end_of_statement (parser);
30705 /* Ok, we got a full-fledged definition (or at least should). */
30706 else
30708 proto = cp_parser_identifier (parser);
30709 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30710 objc_start_protocol (proto, protorefs, attributes);
30711 cp_parser_objc_method_prototype_list (parser);
30715 /* Parse an Objective-C superclass or category. */
30717 static void
30718 cp_parser_objc_superclass_or_category (cp_parser *parser,
30719 bool iface_p,
30720 tree *super,
30721 tree *categ, bool *is_class_extension)
30723 cp_token *next = cp_lexer_peek_token (parser->lexer);
30725 *super = *categ = NULL_TREE;
30726 *is_class_extension = false;
30727 if (next->type == CPP_COLON)
30729 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30730 *super = cp_parser_identifier (parser);
30732 else if (next->type == CPP_OPEN_PAREN)
30734 matching_parens parens;
30735 parens.consume_open (parser); /* Eat '('. */
30737 /* If there is no category name, and this is an @interface, we
30738 have a class extension. */
30739 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30741 *categ = NULL_TREE;
30742 *is_class_extension = true;
30744 else
30745 *categ = cp_parser_identifier (parser);
30747 parens.require_close (parser);
30751 /* Parse an Objective-C class interface. */
30753 static void
30754 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30756 tree name, super, categ, protos;
30757 bool is_class_extension;
30759 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30760 name = cp_parser_identifier (parser);
30761 if (name == error_mark_node)
30763 /* It's hard to recover because even if valid @interface stuff
30764 is to follow, we can't compile it (or validate it) if we
30765 don't even know which class it refers to. Let's assume this
30766 was a stray '@interface' token in the stream and skip it.
30768 return;
30770 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30771 &is_class_extension);
30772 protos = cp_parser_objc_protocol_refs_opt (parser);
30774 /* We have either a class or a category on our hands. */
30775 if (categ || is_class_extension)
30776 objc_start_category_interface (name, categ, protos, attributes);
30777 else
30779 objc_start_class_interface (name, super, protos, attributes);
30780 /* Handle instance variable declarations, if any. */
30781 cp_parser_objc_class_ivars (parser);
30782 objc_continue_interface ();
30785 cp_parser_objc_method_prototype_list (parser);
30788 /* Parse an Objective-C class implementation. */
30790 static void
30791 cp_parser_objc_class_implementation (cp_parser* parser)
30793 tree name, super, categ;
30794 bool is_class_extension;
30796 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30797 name = cp_parser_identifier (parser);
30798 if (name == error_mark_node)
30800 /* It's hard to recover because even if valid @implementation
30801 stuff is to follow, we can't compile it (or validate it) if
30802 we don't even know which class it refers to. Let's assume
30803 this was a stray '@implementation' token in the stream and
30804 skip it.
30806 return;
30808 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30809 &is_class_extension);
30811 /* We have either a class or a category on our hands. */
30812 if (categ)
30813 objc_start_category_implementation (name, categ);
30814 else
30816 objc_start_class_implementation (name, super);
30817 /* Handle instance variable declarations, if any. */
30818 cp_parser_objc_class_ivars (parser);
30819 objc_continue_implementation ();
30822 cp_parser_objc_method_definition_list (parser);
30825 /* Consume the @end token and finish off the implementation. */
30827 static void
30828 cp_parser_objc_end_implementation (cp_parser* parser)
30830 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30831 objc_finish_implementation ();
30834 /* Parse an Objective-C declaration. */
30836 static void
30837 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30839 /* Try to figure out what kind of declaration is present. */
30840 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30842 if (attributes)
30843 switch (kwd->keyword)
30845 case RID_AT_ALIAS:
30846 case RID_AT_CLASS:
30847 case RID_AT_END:
30848 error_at (kwd->location, "attributes may not be specified before"
30849 " the %<@%D%> Objective-C++ keyword",
30850 kwd->u.value);
30851 attributes = NULL;
30852 break;
30853 case RID_AT_IMPLEMENTATION:
30854 warning_at (kwd->location, OPT_Wattributes,
30855 "prefix attributes are ignored before %<@%D%>",
30856 kwd->u.value);
30857 attributes = NULL;
30858 default:
30859 break;
30862 switch (kwd->keyword)
30864 case RID_AT_ALIAS:
30865 cp_parser_objc_alias_declaration (parser);
30866 break;
30867 case RID_AT_CLASS:
30868 cp_parser_objc_class_declaration (parser);
30869 break;
30870 case RID_AT_PROTOCOL:
30871 cp_parser_objc_protocol_declaration (parser, attributes);
30872 break;
30873 case RID_AT_INTERFACE:
30874 cp_parser_objc_class_interface (parser, attributes);
30875 break;
30876 case RID_AT_IMPLEMENTATION:
30877 cp_parser_objc_class_implementation (parser);
30878 break;
30879 case RID_AT_END:
30880 cp_parser_objc_end_implementation (parser);
30881 break;
30882 default:
30883 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30884 kwd->u.value);
30885 cp_parser_skip_to_end_of_block_or_statement (parser);
30889 /* Parse an Objective-C try-catch-finally statement.
30891 objc-try-catch-finally-stmt:
30892 @try compound-statement objc-catch-clause-seq [opt]
30893 objc-finally-clause [opt]
30895 objc-catch-clause-seq:
30896 objc-catch-clause objc-catch-clause-seq [opt]
30898 objc-catch-clause:
30899 @catch ( objc-exception-declaration ) compound-statement
30901 objc-finally-clause:
30902 @finally compound-statement
30904 objc-exception-declaration:
30905 parameter-declaration
30906 '...'
30908 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30910 Returns NULL_TREE.
30912 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30913 for C. Keep them in sync. */
30915 static tree
30916 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30918 location_t location;
30919 tree stmt;
30921 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30922 location = cp_lexer_peek_token (parser->lexer)->location;
30923 objc_maybe_warn_exceptions (location);
30924 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30925 node, lest it get absorbed into the surrounding block. */
30926 stmt = push_stmt_list ();
30927 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30928 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30930 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30932 cp_parameter_declarator *parm;
30933 tree parameter_declaration = error_mark_node;
30934 bool seen_open_paren = false;
30935 matching_parens parens;
30937 cp_lexer_consume_token (parser->lexer);
30938 if (parens.require_open (parser))
30939 seen_open_paren = true;
30940 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30942 /* We have "@catch (...)" (where the '...' are literally
30943 what is in the code). Skip the '...'.
30944 parameter_declaration is set to NULL_TREE, and
30945 objc_being_catch_clauses() knows that that means
30946 '...'. */
30947 cp_lexer_consume_token (parser->lexer);
30948 parameter_declaration = NULL_TREE;
30950 else
30952 /* We have "@catch (NSException *exception)" or something
30953 like that. Parse the parameter declaration. */
30954 parm = cp_parser_parameter_declaration (parser, false, NULL);
30955 if (parm == NULL)
30956 parameter_declaration = error_mark_node;
30957 else
30958 parameter_declaration = grokdeclarator (parm->declarator,
30959 &parm->decl_specifiers,
30960 PARM, /*initialized=*/0,
30961 /*attrlist=*/NULL);
30963 if (seen_open_paren)
30964 parens.require_close (parser);
30965 else
30967 /* If there was no open parenthesis, we are recovering from
30968 an error, and we are trying to figure out what mistake
30969 the user has made. */
30971 /* If there is an immediate closing parenthesis, the user
30972 probably forgot the opening one (ie, they typed "@catch
30973 NSException *e)". Parse the closing parenthesis and keep
30974 going. */
30975 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30976 cp_lexer_consume_token (parser->lexer);
30978 /* If these is no immediate closing parenthesis, the user
30979 probably doesn't know that parenthesis are required at
30980 all (ie, they typed "@catch NSException *e"). So, just
30981 forget about the closing parenthesis and keep going. */
30983 objc_begin_catch_clause (parameter_declaration);
30984 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30985 objc_finish_catch_clause ();
30987 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30989 cp_lexer_consume_token (parser->lexer);
30990 location = cp_lexer_peek_token (parser->lexer)->location;
30991 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30992 node, lest it get absorbed into the surrounding block. */
30993 stmt = push_stmt_list ();
30994 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30995 objc_build_finally_clause (location, pop_stmt_list (stmt));
30998 return objc_finish_try_stmt ();
31001 /* Parse an Objective-C synchronized statement.
31003 objc-synchronized-stmt:
31004 @synchronized ( expression ) compound-statement
31006 Returns NULL_TREE. */
31008 static tree
31009 cp_parser_objc_synchronized_statement (cp_parser *parser)
31011 location_t location;
31012 tree lock, stmt;
31014 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31016 location = cp_lexer_peek_token (parser->lexer)->location;
31017 objc_maybe_warn_exceptions (location);
31018 matching_parens parens;
31019 parens.require_open (parser);
31020 lock = cp_parser_expression (parser);
31021 parens.require_close (parser);
31023 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31024 node, lest it get absorbed into the surrounding block. */
31025 stmt = push_stmt_list ();
31026 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31028 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31031 /* Parse an Objective-C throw statement.
31033 objc-throw-stmt:
31034 @throw assignment-expression [opt] ;
31036 Returns a constructed '@throw' statement. */
31038 static tree
31039 cp_parser_objc_throw_statement (cp_parser *parser)
31041 tree expr = NULL_TREE;
31042 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31044 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31046 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31047 expr = cp_parser_expression (parser);
31049 cp_parser_consume_semicolon_at_end_of_statement (parser);
31051 return objc_build_throw_stmt (loc, expr);
31054 /* Parse an Objective-C statement. */
31056 static tree
31057 cp_parser_objc_statement (cp_parser * parser)
31059 /* Try to figure out what kind of declaration is present. */
31060 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31062 switch (kwd->keyword)
31064 case RID_AT_TRY:
31065 return cp_parser_objc_try_catch_finally_statement (parser);
31066 case RID_AT_SYNCHRONIZED:
31067 return cp_parser_objc_synchronized_statement (parser);
31068 case RID_AT_THROW:
31069 return cp_parser_objc_throw_statement (parser);
31070 default:
31071 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31072 kwd->u.value);
31073 cp_parser_skip_to_end_of_block_or_statement (parser);
31076 return error_mark_node;
31079 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31080 look ahead to see if an objc keyword follows the attributes. This
31081 is to detect the use of prefix attributes on ObjC @interface and
31082 @protocol. */
31084 static bool
31085 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31087 cp_lexer_save_tokens (parser->lexer);
31088 *attrib = cp_parser_attributes_opt (parser);
31089 gcc_assert (*attrib);
31090 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31092 cp_lexer_commit_tokens (parser->lexer);
31093 return true;
31095 cp_lexer_rollback_tokens (parser->lexer);
31096 return false;
31099 /* This routine is a minimal replacement for
31100 c_parser_struct_declaration () used when parsing the list of
31101 types/names or ObjC++ properties. For example, when parsing the
31102 code
31104 @property (readonly) int a, b, c;
31106 this function is responsible for parsing "int a, int b, int c" and
31107 returning the declarations as CHAIN of DECLs.
31109 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31110 similar parsing. */
31111 static tree
31112 cp_parser_objc_struct_declaration (cp_parser *parser)
31114 tree decls = NULL_TREE;
31115 cp_decl_specifier_seq declspecs;
31116 int decl_class_or_enum_p;
31117 tree prefix_attributes;
31119 cp_parser_decl_specifier_seq (parser,
31120 CP_PARSER_FLAGS_NONE,
31121 &declspecs,
31122 &decl_class_or_enum_p);
31124 if (declspecs.type == error_mark_node)
31125 return error_mark_node;
31127 /* auto, register, static, extern, mutable. */
31128 if (declspecs.storage_class != sc_none)
31130 cp_parser_error (parser, "invalid type for property");
31131 declspecs.storage_class = sc_none;
31134 /* thread_local. */
31135 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31137 cp_parser_error (parser, "invalid type for property");
31138 declspecs.locations[ds_thread] = 0;
31141 /* typedef. */
31142 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31144 cp_parser_error (parser, "invalid type for property");
31145 declspecs.locations[ds_typedef] = 0;
31148 prefix_attributes = declspecs.attributes;
31149 declspecs.attributes = NULL_TREE;
31151 /* Keep going until we hit the `;' at the end of the declaration. */
31152 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31154 tree attributes, first_attribute, decl;
31155 cp_declarator *declarator;
31156 cp_token *token;
31158 /* Parse the declarator. */
31159 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31160 NULL, NULL, false, false);
31162 /* Look for attributes that apply to the ivar. */
31163 attributes = cp_parser_attributes_opt (parser);
31164 /* Remember which attributes are prefix attributes and
31165 which are not. */
31166 first_attribute = attributes;
31167 /* Combine the attributes. */
31168 attributes = attr_chainon (prefix_attributes, attributes);
31170 decl = grokfield (declarator, &declspecs,
31171 NULL_TREE, /*init_const_expr_p=*/false,
31172 NULL_TREE, attributes);
31174 if (decl == error_mark_node || decl == NULL_TREE)
31175 return error_mark_node;
31177 /* Reset PREFIX_ATTRIBUTES. */
31178 if (attributes != error_mark_node)
31180 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31181 attributes = TREE_CHAIN (attributes);
31182 if (attributes)
31183 TREE_CHAIN (attributes) = NULL_TREE;
31186 DECL_CHAIN (decl) = decls;
31187 decls = decl;
31189 token = cp_lexer_peek_token (parser->lexer);
31190 if (token->type == CPP_COMMA)
31192 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31193 continue;
31195 else
31196 break;
31198 return decls;
31201 /* Parse an Objective-C @property declaration. The syntax is:
31203 objc-property-declaration:
31204 '@property' objc-property-attributes[opt] struct-declaration ;
31206 objc-property-attributes:
31207 '(' objc-property-attribute-list ')'
31209 objc-property-attribute-list:
31210 objc-property-attribute
31211 objc-property-attribute-list, objc-property-attribute
31213 objc-property-attribute
31214 'getter' = identifier
31215 'setter' = identifier
31216 'readonly'
31217 'readwrite'
31218 'assign'
31219 'retain'
31220 'copy'
31221 'nonatomic'
31223 For example:
31224 @property NSString *name;
31225 @property (readonly) id object;
31226 @property (retain, nonatomic, getter=getTheName) id name;
31227 @property int a, b, c;
31229 PS: This function is identical to
31230 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31231 static void
31232 cp_parser_objc_at_property_declaration (cp_parser *parser)
31234 /* The following variables hold the attributes of the properties as
31235 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31236 seen. When we see an attribute, we set them to 'true' (if they
31237 are boolean properties) or to the identifier (if they have an
31238 argument, ie, for getter and setter). Note that here we only
31239 parse the list of attributes, check the syntax and accumulate the
31240 attributes that we find. objc_add_property_declaration() will
31241 then process the information. */
31242 bool property_assign = false;
31243 bool property_copy = false;
31244 tree property_getter_ident = NULL_TREE;
31245 bool property_nonatomic = false;
31246 bool property_readonly = false;
31247 bool property_readwrite = false;
31248 bool property_retain = false;
31249 tree property_setter_ident = NULL_TREE;
31251 /* 'properties' is the list of properties that we read. Usually a
31252 single one, but maybe more (eg, in "@property int a, b, c;" there
31253 are three). */
31254 tree properties;
31255 location_t loc;
31257 loc = cp_lexer_peek_token (parser->lexer)->location;
31259 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31261 /* Parse the optional attribute list... */
31262 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31264 /* Eat the '('. */
31265 matching_parens parens;
31266 parens.consume_open (parser);
31268 while (true)
31270 bool syntax_error = false;
31271 cp_token *token = cp_lexer_peek_token (parser->lexer);
31272 enum rid keyword;
31274 if (token->type != CPP_NAME)
31276 cp_parser_error (parser, "expected identifier");
31277 break;
31279 keyword = C_RID_CODE (token->u.value);
31280 cp_lexer_consume_token (parser->lexer);
31281 switch (keyword)
31283 case RID_ASSIGN: property_assign = true; break;
31284 case RID_COPY: property_copy = true; break;
31285 case RID_NONATOMIC: property_nonatomic = true; break;
31286 case RID_READONLY: property_readonly = true; break;
31287 case RID_READWRITE: property_readwrite = true; break;
31288 case RID_RETAIN: property_retain = true; break;
31290 case RID_GETTER:
31291 case RID_SETTER:
31292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31294 if (keyword == RID_GETTER)
31295 cp_parser_error (parser,
31296 "missing %<=%> (after %<getter%> attribute)");
31297 else
31298 cp_parser_error (parser,
31299 "missing %<=%> (after %<setter%> attribute)");
31300 syntax_error = true;
31301 break;
31303 cp_lexer_consume_token (parser->lexer); /* eat the = */
31304 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31306 cp_parser_error (parser, "expected identifier");
31307 syntax_error = true;
31308 break;
31310 if (keyword == RID_SETTER)
31312 if (property_setter_ident != NULL_TREE)
31314 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31315 cp_lexer_consume_token (parser->lexer);
31317 else
31318 property_setter_ident = cp_parser_objc_selector (parser);
31319 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31320 cp_parser_error (parser, "setter name must terminate with %<:%>");
31321 else
31322 cp_lexer_consume_token (parser->lexer);
31324 else
31326 if (property_getter_ident != NULL_TREE)
31328 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31329 cp_lexer_consume_token (parser->lexer);
31331 else
31332 property_getter_ident = cp_parser_objc_selector (parser);
31334 break;
31335 default:
31336 cp_parser_error (parser, "unknown property attribute");
31337 syntax_error = true;
31338 break;
31341 if (syntax_error)
31342 break;
31344 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31345 cp_lexer_consume_token (parser->lexer);
31346 else
31347 break;
31350 /* FIXME: "@property (setter, assign);" will generate a spurious
31351 "error: expected ‘)’ before ‘,’ token". This is because
31352 cp_parser_require, unlike the C counterpart, will produce an
31353 error even if we are in error recovery. */
31354 if (!parens.require_close (parser))
31356 cp_parser_skip_to_closing_parenthesis (parser,
31357 /*recovering=*/true,
31358 /*or_comma=*/false,
31359 /*consume_paren=*/true);
31363 /* ... and the property declaration(s). */
31364 properties = cp_parser_objc_struct_declaration (parser);
31366 if (properties == error_mark_node)
31368 cp_parser_skip_to_end_of_statement (parser);
31369 /* If the next token is now a `;', consume it. */
31370 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31371 cp_lexer_consume_token (parser->lexer);
31372 return;
31375 if (properties == NULL_TREE)
31376 cp_parser_error (parser, "expected identifier");
31377 else
31379 /* Comma-separated properties are chained together in
31380 reverse order; add them one by one. */
31381 properties = nreverse (properties);
31383 for (; properties; properties = TREE_CHAIN (properties))
31384 objc_add_property_declaration (loc, copy_node (properties),
31385 property_readonly, property_readwrite,
31386 property_assign, property_retain,
31387 property_copy, property_nonatomic,
31388 property_getter_ident, property_setter_ident);
31391 cp_parser_consume_semicolon_at_end_of_statement (parser);
31394 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31396 objc-synthesize-declaration:
31397 @synthesize objc-synthesize-identifier-list ;
31399 objc-synthesize-identifier-list:
31400 objc-synthesize-identifier
31401 objc-synthesize-identifier-list, objc-synthesize-identifier
31403 objc-synthesize-identifier
31404 identifier
31405 identifier = identifier
31407 For example:
31408 @synthesize MyProperty;
31409 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31411 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31412 for C. Keep them in sync.
31414 static void
31415 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31417 tree list = NULL_TREE;
31418 location_t loc;
31419 loc = cp_lexer_peek_token (parser->lexer)->location;
31421 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31422 while (true)
31424 tree property, ivar;
31425 property = cp_parser_identifier (parser);
31426 if (property == error_mark_node)
31428 cp_parser_consume_semicolon_at_end_of_statement (parser);
31429 return;
31431 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31433 cp_lexer_consume_token (parser->lexer);
31434 ivar = cp_parser_identifier (parser);
31435 if (ivar == error_mark_node)
31437 cp_parser_consume_semicolon_at_end_of_statement (parser);
31438 return;
31441 else
31442 ivar = NULL_TREE;
31443 list = chainon (list, build_tree_list (ivar, property));
31444 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31445 cp_lexer_consume_token (parser->lexer);
31446 else
31447 break;
31449 cp_parser_consume_semicolon_at_end_of_statement (parser);
31450 objc_add_synthesize_declaration (loc, list);
31453 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31455 objc-dynamic-declaration:
31456 @dynamic identifier-list ;
31458 For example:
31459 @dynamic MyProperty;
31460 @dynamic MyProperty, AnotherProperty;
31462 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31463 for C. Keep them in sync.
31465 static void
31466 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31468 tree list = NULL_TREE;
31469 location_t loc;
31470 loc = cp_lexer_peek_token (parser->lexer)->location;
31472 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31473 while (true)
31475 tree property;
31476 property = cp_parser_identifier (parser);
31477 if (property == error_mark_node)
31479 cp_parser_consume_semicolon_at_end_of_statement (parser);
31480 return;
31482 list = chainon (list, build_tree_list (NULL, property));
31483 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31484 cp_lexer_consume_token (parser->lexer);
31485 else
31486 break;
31488 cp_parser_consume_semicolon_at_end_of_statement (parser);
31489 objc_add_dynamic_declaration (loc, list);
31493 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
31495 /* Returns name of the next clause.
31496 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31497 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31498 returned and the token is consumed. */
31500 static pragma_omp_clause
31501 cp_parser_omp_clause_name (cp_parser *parser)
31503 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31505 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31506 result = PRAGMA_OACC_CLAUSE_AUTO;
31507 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31508 result = PRAGMA_OMP_CLAUSE_IF;
31509 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31510 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31511 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31512 result = PRAGMA_OACC_CLAUSE_DELETE;
31513 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31514 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31515 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31516 result = PRAGMA_OMP_CLAUSE_FOR;
31517 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31519 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31520 const char *p = IDENTIFIER_POINTER (id);
31522 switch (p[0])
31524 case 'a':
31525 if (!strcmp ("aligned", p))
31526 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31527 else if (!strcmp ("async", p))
31528 result = PRAGMA_OACC_CLAUSE_ASYNC;
31529 break;
31530 case 'c':
31531 if (!strcmp ("collapse", p))
31532 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31533 else if (!strcmp ("copy", p))
31534 result = PRAGMA_OACC_CLAUSE_COPY;
31535 else if (!strcmp ("copyin", p))
31536 result = PRAGMA_OMP_CLAUSE_COPYIN;
31537 else if (!strcmp ("copyout", p))
31538 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31539 else if (!strcmp ("copyprivate", p))
31540 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31541 else if (!strcmp ("create", p))
31542 result = PRAGMA_OACC_CLAUSE_CREATE;
31543 break;
31544 case 'd':
31545 if (!strcmp ("defaultmap", p))
31546 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31547 else if (!strcmp ("depend", p))
31548 result = PRAGMA_OMP_CLAUSE_DEPEND;
31549 else if (!strcmp ("device", p))
31550 result = PRAGMA_OMP_CLAUSE_DEVICE;
31551 else if (!strcmp ("deviceptr", p))
31552 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31553 else if (!strcmp ("device_resident", p))
31554 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31555 else if (!strcmp ("dist_schedule", p))
31556 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31557 break;
31558 case 'f':
31559 if (!strcmp ("final", p))
31560 result = PRAGMA_OMP_CLAUSE_FINAL;
31561 else if (!strcmp ("finalize", p))
31562 result = PRAGMA_OACC_CLAUSE_FINALIZE;
31563 else if (!strcmp ("firstprivate", p))
31564 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31565 else if (!strcmp ("from", p))
31566 result = PRAGMA_OMP_CLAUSE_FROM;
31567 break;
31568 case 'g':
31569 if (!strcmp ("gang", p))
31570 result = PRAGMA_OACC_CLAUSE_GANG;
31571 else if (!strcmp ("grainsize", p))
31572 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31573 break;
31574 case 'h':
31575 if (!strcmp ("hint", p))
31576 result = PRAGMA_OMP_CLAUSE_HINT;
31577 else if (!strcmp ("host", p))
31578 result = PRAGMA_OACC_CLAUSE_HOST;
31579 break;
31580 case 'i':
31581 if (!strcmp ("if_present", p))
31582 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
31583 else if (!strcmp ("in_reduction", p))
31584 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
31585 else if (!strcmp ("inbranch", p))
31586 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31587 else if (!strcmp ("independent", p))
31588 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31589 else if (!strcmp ("is_device_ptr", p))
31590 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31591 break;
31592 case 'l':
31593 if (!strcmp ("lastprivate", p))
31594 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31595 else if (!strcmp ("linear", p))
31596 result = PRAGMA_OMP_CLAUSE_LINEAR;
31597 else if (!strcmp ("link", p))
31598 result = PRAGMA_OMP_CLAUSE_LINK;
31599 break;
31600 case 'm':
31601 if (!strcmp ("map", p))
31602 result = PRAGMA_OMP_CLAUSE_MAP;
31603 else if (!strcmp ("mergeable", p))
31604 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31605 break;
31606 case 'n':
31607 if (!strcmp ("nogroup", p))
31608 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31609 else if (!strcmp ("nontemporal", p))
31610 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
31611 else if (!strcmp ("notinbranch", p))
31612 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31613 else if (!strcmp ("nowait", p))
31614 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31615 else if (!strcmp ("num_gangs", p))
31616 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31617 else if (!strcmp ("num_tasks", p))
31618 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31619 else if (!strcmp ("num_teams", p))
31620 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31621 else if (!strcmp ("num_threads", p))
31622 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31623 else if (!strcmp ("num_workers", p))
31624 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31625 break;
31626 case 'o':
31627 if (!strcmp ("ordered", p))
31628 result = PRAGMA_OMP_CLAUSE_ORDERED;
31629 break;
31630 case 'p':
31631 if (!strcmp ("parallel", p))
31632 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31633 else if (!strcmp ("present", p))
31634 result = PRAGMA_OACC_CLAUSE_PRESENT;
31635 else if (!strcmp ("present_or_copy", p)
31636 || !strcmp ("pcopy", p))
31637 result = PRAGMA_OACC_CLAUSE_COPY;
31638 else if (!strcmp ("present_or_copyin", p)
31639 || !strcmp ("pcopyin", p))
31640 result = PRAGMA_OACC_CLAUSE_COPYIN;
31641 else if (!strcmp ("present_or_copyout", p)
31642 || !strcmp ("pcopyout", p))
31643 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31644 else if (!strcmp ("present_or_create", p)
31645 || !strcmp ("pcreate", p))
31646 result = PRAGMA_OACC_CLAUSE_CREATE;
31647 else if (!strcmp ("priority", p))
31648 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31649 else if (!strcmp ("proc_bind", p))
31650 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31651 break;
31652 case 'r':
31653 if (!strcmp ("reduction", p))
31654 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31655 break;
31656 case 's':
31657 if (!strcmp ("safelen", p))
31658 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31659 else if (!strcmp ("schedule", p))
31660 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31661 else if (!strcmp ("sections", p))
31662 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31663 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
31664 result = PRAGMA_OACC_CLAUSE_HOST;
31665 else if (!strcmp ("seq", p))
31666 result = PRAGMA_OACC_CLAUSE_SEQ;
31667 else if (!strcmp ("shared", p))
31668 result = PRAGMA_OMP_CLAUSE_SHARED;
31669 else if (!strcmp ("simd", p))
31670 result = PRAGMA_OMP_CLAUSE_SIMD;
31671 else if (!strcmp ("simdlen", p))
31672 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31673 break;
31674 case 't':
31675 if (!strcmp ("task_reduction", p))
31676 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
31677 else if (!strcmp ("taskgroup", p))
31678 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31679 else if (!strcmp ("thread_limit", p))
31680 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31681 else if (!strcmp ("threads", p))
31682 result = PRAGMA_OMP_CLAUSE_THREADS;
31683 else if (!strcmp ("tile", p))
31684 result = PRAGMA_OACC_CLAUSE_TILE;
31685 else if (!strcmp ("to", p))
31686 result = PRAGMA_OMP_CLAUSE_TO;
31687 break;
31688 case 'u':
31689 if (!strcmp ("uniform", p))
31690 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31691 else if (!strcmp ("untied", p))
31692 result = PRAGMA_OMP_CLAUSE_UNTIED;
31693 else if (!strcmp ("use_device", p))
31694 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31695 else if (!strcmp ("use_device_ptr", p))
31696 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31697 break;
31698 case 'v':
31699 if (!strcmp ("vector", p))
31700 result = PRAGMA_OACC_CLAUSE_VECTOR;
31701 else if (!strcmp ("vector_length", p))
31702 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31703 break;
31704 case 'w':
31705 if (!strcmp ("wait", p))
31706 result = PRAGMA_OACC_CLAUSE_WAIT;
31707 else if (!strcmp ("worker", p))
31708 result = PRAGMA_OACC_CLAUSE_WORKER;
31709 break;
31713 if (result != PRAGMA_OMP_CLAUSE_NONE)
31714 cp_lexer_consume_token (parser->lexer);
31716 return result;
31719 /* Validate that a clause of the given type does not already exist. */
31721 static void
31722 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31723 const char *name, location_t location)
31725 tree c;
31727 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31728 if (OMP_CLAUSE_CODE (c) == code)
31730 error_at (location, "too many %qs clauses", name);
31731 break;
31735 /* OpenMP 2.5:
31736 variable-list:
31737 identifier
31738 variable-list , identifier
31740 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31741 colon). An opening parenthesis will have been consumed by the caller.
31743 If KIND is nonzero, create the appropriate node and install the decl
31744 in OMP_CLAUSE_DECL and add the node to the head of the list.
31746 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31747 return the list created.
31749 COLON can be NULL if only closing parenthesis should end the list,
31750 or pointer to bool which will receive false if the list is terminated
31751 by closing parenthesis or true if the list is terminated by colon. */
31753 static tree
31754 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31755 tree list, bool *colon)
31757 cp_token *token;
31758 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31759 if (colon)
31761 parser->colon_corrects_to_scope_p = false;
31762 *colon = false;
31764 while (1)
31766 tree name, decl;
31768 if (kind == OMP_CLAUSE_DEPEND)
31769 cp_parser_parse_tentatively (parser);
31770 token = cp_lexer_peek_token (parser->lexer);
31771 if (kind != 0
31772 && current_class_ptr
31773 && cp_parser_is_keyword (token, RID_THIS))
31775 decl = finish_this_expr ();
31776 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31777 || CONVERT_EXPR_P (decl))
31778 decl = TREE_OPERAND (decl, 0);
31779 cp_lexer_consume_token (parser->lexer);
31781 else
31783 name = cp_parser_id_expression (parser, /*template_p=*/false,
31784 /*check_dependency_p=*/true,
31785 /*template_p=*/NULL,
31786 /*declarator_p=*/false,
31787 /*optional_p=*/false);
31788 if (name == error_mark_node)
31790 if (kind == OMP_CLAUSE_DEPEND
31791 && cp_parser_simulate_error (parser))
31792 goto depend_lvalue;
31793 goto skip_comma;
31796 if (identifier_p (name))
31797 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31798 else
31799 decl = name;
31800 if (decl == error_mark_node)
31802 if (kind == OMP_CLAUSE_DEPEND
31803 && cp_parser_simulate_error (parser))
31804 goto depend_lvalue;
31805 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31806 token->location);
31809 if (decl == error_mark_node)
31811 else if (kind != 0)
31813 switch (kind)
31815 case OMP_CLAUSE__CACHE_:
31816 /* The OpenACC cache directive explicitly only allows "array
31817 elements or subarrays". */
31818 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31820 error_at (token->location, "expected %<[%>");
31821 decl = error_mark_node;
31822 break;
31824 /* FALLTHROUGH. */
31825 case OMP_CLAUSE_MAP:
31826 case OMP_CLAUSE_FROM:
31827 case OMP_CLAUSE_TO:
31828 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31830 location_t loc
31831 = cp_lexer_peek_token (parser->lexer)->location;
31832 cp_id_kind idk = CP_ID_KIND_NONE;
31833 cp_lexer_consume_token (parser->lexer);
31834 decl = convert_from_reference (decl);
31835 decl
31836 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31837 decl, false,
31838 &idk, loc);
31840 /* FALLTHROUGH. */
31841 case OMP_CLAUSE_DEPEND:
31842 case OMP_CLAUSE_REDUCTION:
31843 case OMP_CLAUSE_IN_REDUCTION:
31844 case OMP_CLAUSE_TASK_REDUCTION:
31845 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31847 tree low_bound = NULL_TREE, length = NULL_TREE;
31849 parser->colon_corrects_to_scope_p = false;
31850 cp_lexer_consume_token (parser->lexer);
31851 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31852 low_bound = cp_parser_expression (parser);
31853 if (!colon)
31854 parser->colon_corrects_to_scope_p
31855 = saved_colon_corrects_to_scope_p;
31856 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31857 length = integer_one_node;
31858 else
31860 /* Look for `:'. */
31861 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31863 if (kind == OMP_CLAUSE_DEPEND
31864 && cp_parser_simulate_error (parser))
31865 goto depend_lvalue;
31866 goto skip_comma;
31868 if (kind == OMP_CLAUSE_DEPEND)
31869 cp_parser_commit_to_tentative_parse (parser);
31870 if (!cp_lexer_next_token_is (parser->lexer,
31871 CPP_CLOSE_SQUARE))
31872 length = cp_parser_expression (parser);
31874 /* Look for the closing `]'. */
31875 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31876 RT_CLOSE_SQUARE))
31878 if (kind == OMP_CLAUSE_DEPEND
31879 && cp_parser_simulate_error (parser))
31880 goto depend_lvalue;
31881 goto skip_comma;
31884 decl = tree_cons (low_bound, length, decl);
31886 break;
31887 default:
31888 break;
31891 if (kind == OMP_CLAUSE_DEPEND)
31893 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
31894 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
31895 && cp_parser_simulate_error (parser))
31897 depend_lvalue:
31898 cp_parser_abort_tentative_parse (parser);
31899 decl = cp_parser_assignment_expression (parser, NULL,
31900 false, false);
31902 else
31903 cp_parser_parse_definitely (parser);
31906 tree u = build_omp_clause (token->location, kind);
31907 OMP_CLAUSE_DECL (u) = decl;
31908 OMP_CLAUSE_CHAIN (u) = list;
31909 list = u;
31911 else
31912 list = tree_cons (decl, NULL_TREE, list);
31914 get_comma:
31915 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31916 break;
31917 cp_lexer_consume_token (parser->lexer);
31920 if (colon)
31921 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31923 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31925 *colon = true;
31926 cp_parser_require (parser, CPP_COLON, RT_COLON);
31927 return list;
31930 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31932 int ending;
31934 /* Try to resync to an unnested comma. Copied from
31935 cp_parser_parenthesized_expression_list. */
31936 skip_comma:
31937 if (colon)
31938 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31939 ending = cp_parser_skip_to_closing_parenthesis (parser,
31940 /*recovering=*/true,
31941 /*or_comma=*/true,
31942 /*consume_paren=*/true);
31943 if (ending < 0)
31944 goto get_comma;
31947 return list;
31950 /* Similarly, but expect leading and trailing parenthesis. This is a very
31951 common case for omp clauses. */
31953 static tree
31954 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31956 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31957 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31958 return list;
31961 /* OpenACC 2.0:
31962 copy ( variable-list )
31963 copyin ( variable-list )
31964 copyout ( variable-list )
31965 create ( variable-list )
31966 delete ( variable-list )
31967 present ( variable-list ) */
31969 static tree
31970 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31971 tree list)
31973 enum gomp_map_kind kind;
31974 switch (c_kind)
31976 case PRAGMA_OACC_CLAUSE_COPY:
31977 kind = GOMP_MAP_TOFROM;
31978 break;
31979 case PRAGMA_OACC_CLAUSE_COPYIN:
31980 kind = GOMP_MAP_TO;
31981 break;
31982 case PRAGMA_OACC_CLAUSE_COPYOUT:
31983 kind = GOMP_MAP_FROM;
31984 break;
31985 case PRAGMA_OACC_CLAUSE_CREATE:
31986 kind = GOMP_MAP_ALLOC;
31987 break;
31988 case PRAGMA_OACC_CLAUSE_DELETE:
31989 kind = GOMP_MAP_RELEASE;
31990 break;
31991 case PRAGMA_OACC_CLAUSE_DEVICE:
31992 kind = GOMP_MAP_FORCE_TO;
31993 break;
31994 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31995 kind = GOMP_MAP_DEVICE_RESIDENT;
31996 break;
31997 case PRAGMA_OACC_CLAUSE_HOST:
31998 kind = GOMP_MAP_FORCE_FROM;
31999 break;
32000 case PRAGMA_OACC_CLAUSE_LINK:
32001 kind = GOMP_MAP_LINK;
32002 break;
32003 case PRAGMA_OACC_CLAUSE_PRESENT:
32004 kind = GOMP_MAP_FORCE_PRESENT;
32005 break;
32006 default:
32007 gcc_unreachable ();
32009 tree nl, c;
32010 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32012 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32013 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32015 return nl;
32018 /* OpenACC 2.0:
32019 deviceptr ( variable-list ) */
32021 static tree
32022 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32024 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32025 tree vars, t;
32027 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32028 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32029 variable-list must only allow for pointer variables. */
32030 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32031 for (t = vars; t; t = TREE_CHAIN (t))
32033 tree v = TREE_PURPOSE (t);
32034 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32035 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32036 OMP_CLAUSE_DECL (u) = v;
32037 OMP_CLAUSE_CHAIN (u) = list;
32038 list = u;
32041 return list;
32044 /* OpenACC 2.5:
32045 auto
32046 finalize
32047 independent
32048 nohost
32049 seq */
32051 static tree
32052 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
32053 enum omp_clause_code code,
32054 tree list, location_t location)
32056 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32057 tree c = build_omp_clause (location, code);
32058 OMP_CLAUSE_CHAIN (c) = list;
32059 return c;
32062 /* OpenACC:
32063 num_gangs ( expression )
32064 num_workers ( expression )
32065 vector_length ( expression ) */
32067 static tree
32068 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32069 const char *str, tree list)
32071 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32073 matching_parens parens;
32074 if (!parens.require_open (parser))
32075 return list;
32077 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32079 if (t == error_mark_node
32080 || !parens.require_close (parser))
32082 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32083 /*or_comma=*/false,
32084 /*consume_paren=*/true);
32085 return list;
32088 check_no_duplicate_clause (list, code, str, loc);
32090 tree c = build_omp_clause (loc, code);
32091 OMP_CLAUSE_OPERAND (c, 0) = t;
32092 OMP_CLAUSE_CHAIN (c) = list;
32093 return c;
32096 /* OpenACC:
32098 gang [( gang-arg-list )]
32099 worker [( [num:] int-expr )]
32100 vector [( [length:] int-expr )]
32102 where gang-arg is one of:
32104 [num:] int-expr
32105 static: size-expr
32107 and size-expr may be:
32110 int-expr
32113 static tree
32114 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
32115 const char *str, tree list)
32117 const char *id = "num";
32118 cp_lexer *lexer = parser->lexer;
32119 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32120 location_t loc = cp_lexer_peek_token (lexer)->location;
32122 if (kind == OMP_CLAUSE_VECTOR)
32123 id = "length";
32125 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32127 matching_parens parens;
32128 parens.consume_open (parser);
32132 cp_token *next = cp_lexer_peek_token (lexer);
32133 int idx = 0;
32135 /* Gang static argument. */
32136 if (kind == OMP_CLAUSE_GANG
32137 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32139 cp_lexer_consume_token (lexer);
32141 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32142 goto cleanup_error;
32144 idx = 1;
32145 if (ops[idx] != NULL)
32147 cp_parser_error (parser, "too many %<static%> arguments");
32148 goto cleanup_error;
32151 /* Check for the '*' argument. */
32152 if (cp_lexer_next_token_is (lexer, CPP_MULT)
32153 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32154 || cp_lexer_nth_token_is (parser->lexer, 2,
32155 CPP_CLOSE_PAREN)))
32157 cp_lexer_consume_token (lexer);
32158 ops[idx] = integer_minus_one_node;
32160 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32162 cp_lexer_consume_token (lexer);
32163 continue;
32165 else break;
32168 /* Worker num: argument and vector length: arguments. */
32169 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32170 && id_equal (next->u.value, id)
32171 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32173 cp_lexer_consume_token (lexer); /* id */
32174 cp_lexer_consume_token (lexer); /* ':' */
32177 /* Now collect the actual argument. */
32178 if (ops[idx] != NULL_TREE)
32180 cp_parser_error (parser, "unexpected argument");
32181 goto cleanup_error;
32184 tree expr = cp_parser_assignment_expression (parser, NULL, false,
32185 false);
32186 if (expr == error_mark_node)
32187 goto cleanup_error;
32189 mark_exp_read (expr);
32190 ops[idx] = expr;
32192 if (kind == OMP_CLAUSE_GANG
32193 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32195 cp_lexer_consume_token (lexer);
32196 continue;
32198 break;
32200 while (1);
32202 if (!parens.require_close (parser))
32203 goto cleanup_error;
32206 check_no_duplicate_clause (list, kind, str, loc);
32208 c = build_omp_clause (loc, kind);
32210 if (ops[1])
32211 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32213 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32214 OMP_CLAUSE_CHAIN (c) = list;
32216 return c;
32218 cleanup_error:
32219 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32220 return list;
32223 /* OpenACC 2.0:
32224 tile ( size-expr-list ) */
32226 static tree
32227 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32229 tree c, expr = error_mark_node;
32230 tree tile = NULL_TREE;
32232 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32233 so, but the spec authors never considered such a case and have
32234 differing opinions on what it might mean, including 'not
32235 allowed'.) */
32236 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32237 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32238 clause_loc);
32240 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32241 return list;
32245 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32246 return list;
32248 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32249 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32250 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32252 cp_lexer_consume_token (parser->lexer);
32253 expr = integer_zero_node;
32255 else
32256 expr = cp_parser_constant_expression (parser);
32258 tile = tree_cons (NULL_TREE, expr, tile);
32260 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32262 /* Consume the trailing ')'. */
32263 cp_lexer_consume_token (parser->lexer);
32265 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32266 tile = nreverse (tile);
32267 OMP_CLAUSE_TILE_LIST (c) = tile;
32268 OMP_CLAUSE_CHAIN (c) = list;
32269 return c;
32272 /* OpenACC 2.0
32273 Parse wait clause or directive parameters. */
32275 static tree
32276 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32278 vec<tree, va_gc> *args;
32279 tree t, args_tree;
32281 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32282 /*cast_p=*/false,
32283 /*allow_expansion_p=*/true,
32284 /*non_constant_p=*/NULL);
32286 if (args == NULL || args->length () == 0)
32288 cp_parser_error (parser, "expected integer expression before ')'");
32289 if (args != NULL)
32290 release_tree_vector (args);
32291 return list;
32294 args_tree = build_tree_list_vec (args);
32296 release_tree_vector (args);
32298 for (t = args_tree; t; t = TREE_CHAIN (t))
32300 tree targ = TREE_VALUE (t);
32302 if (targ != error_mark_node)
32304 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32305 error ("%<wait%> expression must be integral");
32306 else
32308 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32310 targ = mark_rvalue_use (targ);
32311 OMP_CLAUSE_DECL (c) = targ;
32312 OMP_CLAUSE_CHAIN (c) = list;
32313 list = c;
32318 return list;
32321 /* OpenACC:
32322 wait ( int-expr-list ) */
32324 static tree
32325 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32327 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32329 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32330 return list;
32332 list = cp_parser_oacc_wait_list (parser, location, list);
32334 return list;
32337 /* OpenMP 3.0:
32338 collapse ( constant-expression ) */
32340 static tree
32341 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32343 tree c, num;
32344 location_t loc;
32345 HOST_WIDE_INT n;
32347 loc = cp_lexer_peek_token (parser->lexer)->location;
32348 matching_parens parens;
32349 if (!parens.require_open (parser))
32350 return list;
32352 num = cp_parser_constant_expression (parser);
32354 if (!parens.require_close (parser))
32355 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32356 /*or_comma=*/false,
32357 /*consume_paren=*/true);
32359 if (num == error_mark_node)
32360 return list;
32361 num = fold_non_dependent_expr (num);
32362 if (!tree_fits_shwi_p (num)
32363 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32364 || (n = tree_to_shwi (num)) <= 0
32365 || (int) n != n)
32367 error_at (loc, "collapse argument needs positive constant integer expression");
32368 return list;
32371 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32372 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32373 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32374 OMP_CLAUSE_CHAIN (c) = list;
32375 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32377 return c;
32380 /* OpenMP 2.5:
32381 default ( none | shared )
32383 OpenACC:
32384 default ( none | present ) */
32386 static tree
32387 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32388 location_t location, bool is_oacc)
32390 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32391 tree c;
32393 matching_parens parens;
32394 if (!parens.require_open (parser))
32395 return list;
32396 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32398 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32399 const char *p = IDENTIFIER_POINTER (id);
32401 switch (p[0])
32403 case 'n':
32404 if (strcmp ("none", p) != 0)
32405 goto invalid_kind;
32406 kind = OMP_CLAUSE_DEFAULT_NONE;
32407 break;
32409 case 'p':
32410 if (strcmp ("present", p) != 0 || !is_oacc)
32411 goto invalid_kind;
32412 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32413 break;
32415 case 's':
32416 if (strcmp ("shared", p) != 0 || is_oacc)
32417 goto invalid_kind;
32418 kind = OMP_CLAUSE_DEFAULT_SHARED;
32419 break;
32421 default:
32422 goto invalid_kind;
32425 cp_lexer_consume_token (parser->lexer);
32427 else
32429 invalid_kind:
32430 if (is_oacc)
32431 cp_parser_error (parser, "expected %<none%> or %<present%>");
32432 else
32433 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32436 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32437 || !parens.require_close (parser))
32438 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32439 /*or_comma=*/false,
32440 /*consume_paren=*/true);
32442 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32443 return list;
32445 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32446 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32447 OMP_CLAUSE_CHAIN (c) = list;
32448 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32450 return c;
32453 /* OpenMP 3.1:
32454 final ( expression ) */
32456 static tree
32457 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32459 tree t, c;
32461 matching_parens parens;
32462 if (!parens.require_open (parser))
32463 return list;
32465 t = cp_parser_assignment_expression (parser);
32467 if (t == error_mark_node
32468 || !parens.require_close (parser))
32469 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32470 /*or_comma=*/false,
32471 /*consume_paren=*/true);
32473 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32475 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32476 OMP_CLAUSE_FINAL_EXPR (c) = t;
32477 OMP_CLAUSE_CHAIN (c) = list;
32479 return c;
32482 /* OpenMP 2.5:
32483 if ( expression )
32485 OpenMP 4.5:
32486 if ( directive-name-modifier : expression )
32488 directive-name-modifier:
32489 parallel | task | taskloop | target data | target | target update
32490 | target enter data | target exit data
32492 OpenMP 5.0:
32493 directive-name-modifier:
32494 ... | simd | cancel */
32496 static tree
32497 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32498 bool is_omp)
32500 tree t, c;
32501 enum tree_code if_modifier = ERROR_MARK;
32503 matching_parens parens;
32504 if (!parens.require_open (parser))
32505 return list;
32507 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32509 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32510 const char *p = IDENTIFIER_POINTER (id);
32511 int n = 2;
32513 if (strcmp ("cancel", p) == 0)
32514 if_modifier = VOID_CST;
32515 else if (strcmp ("parallel", p) == 0)
32516 if_modifier = OMP_PARALLEL;
32517 else if (strcmp ("simd", p) == 0)
32518 if_modifier = OMP_SIMD;
32519 else if (strcmp ("task", p) == 0)
32520 if_modifier = OMP_TASK;
32521 else if (strcmp ("taskloop", p) == 0)
32522 if_modifier = OMP_TASKLOOP;
32523 else if (strcmp ("target", p) == 0)
32525 if_modifier = OMP_TARGET;
32526 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32528 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32529 p = IDENTIFIER_POINTER (id);
32530 if (strcmp ("data", p) == 0)
32531 if_modifier = OMP_TARGET_DATA;
32532 else if (strcmp ("update", p) == 0)
32533 if_modifier = OMP_TARGET_UPDATE;
32534 else if (strcmp ("enter", p) == 0)
32535 if_modifier = OMP_TARGET_ENTER_DATA;
32536 else if (strcmp ("exit", p) == 0)
32537 if_modifier = OMP_TARGET_EXIT_DATA;
32538 if (if_modifier != OMP_TARGET)
32539 n = 3;
32540 else
32542 location_t loc
32543 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32544 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32545 "or %<exit%>");
32546 if_modifier = ERROR_MARK;
32548 if (if_modifier == OMP_TARGET_ENTER_DATA
32549 || if_modifier == OMP_TARGET_EXIT_DATA)
32551 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32553 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32554 p = IDENTIFIER_POINTER (id);
32555 if (strcmp ("data", p) == 0)
32556 n = 4;
32558 if (n != 4)
32560 location_t loc
32561 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32562 error_at (loc, "expected %<data%>");
32563 if_modifier = ERROR_MARK;
32568 if (if_modifier != ERROR_MARK)
32570 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32572 while (n-- > 0)
32573 cp_lexer_consume_token (parser->lexer);
32575 else
32577 if (n > 2)
32579 location_t loc
32580 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32581 error_at (loc, "expected %<:%>");
32583 if_modifier = ERROR_MARK;
32588 t = cp_parser_assignment_expression (parser);
32590 if (t == error_mark_node
32591 || !parens.require_close (parser))
32592 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32593 /*or_comma=*/false,
32594 /*consume_paren=*/true);
32596 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32597 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32599 if (if_modifier != ERROR_MARK
32600 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32602 const char *p = NULL;
32603 switch (if_modifier)
32605 case VOID_CST: p = "cancel"; break;
32606 case OMP_PARALLEL: p = "parallel"; break;
32607 case OMP_SIMD: p = "simd"; break;
32608 case OMP_TASK: p = "task"; break;
32609 case OMP_TASKLOOP: p = "taskloop"; break;
32610 case OMP_TARGET_DATA: p = "target data"; break;
32611 case OMP_TARGET: p = "target"; break;
32612 case OMP_TARGET_UPDATE: p = "target update"; break;
32613 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32614 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32615 default: gcc_unreachable ();
32617 error_at (location, "too many %<if%> clauses with %qs modifier",
32619 return list;
32621 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32623 if (!is_omp)
32624 error_at (location, "too many %<if%> clauses");
32625 else
32626 error_at (location, "too many %<if%> clauses without modifier");
32627 return list;
32629 else if (if_modifier == ERROR_MARK
32630 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32632 error_at (location, "if any %<if%> clause has modifier, then all "
32633 "%<if%> clauses have to use modifier");
32634 return list;
32638 c = build_omp_clause (location, OMP_CLAUSE_IF);
32639 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32640 OMP_CLAUSE_IF_EXPR (c) = t;
32641 OMP_CLAUSE_CHAIN (c) = list;
32643 return c;
32646 /* OpenMP 3.1:
32647 mergeable */
32649 static tree
32650 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32651 tree list, location_t location)
32653 tree c;
32655 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32656 location);
32658 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32659 OMP_CLAUSE_CHAIN (c) = list;
32660 return c;
32663 /* OpenMP 2.5:
32664 nowait */
32666 static tree
32667 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32668 tree list, location_t location)
32670 tree c;
32672 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32674 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32675 OMP_CLAUSE_CHAIN (c) = list;
32676 return c;
32679 /* OpenMP 2.5:
32680 num_threads ( expression ) */
32682 static tree
32683 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32684 location_t location)
32686 tree t, c;
32688 matching_parens parens;
32689 if (!parens.require_open (parser))
32690 return list;
32692 t = cp_parser_assignment_expression (parser);
32694 if (t == error_mark_node
32695 || !parens.require_close (parser))
32696 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32697 /*or_comma=*/false,
32698 /*consume_paren=*/true);
32700 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32701 "num_threads", location);
32703 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32704 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32705 OMP_CLAUSE_CHAIN (c) = list;
32707 return c;
32710 /* OpenMP 4.5:
32711 num_tasks ( expression ) */
32713 static tree
32714 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32715 location_t location)
32717 tree t, c;
32719 matching_parens parens;
32720 if (!parens.require_open (parser))
32721 return list;
32723 t = cp_parser_assignment_expression (parser);
32725 if (t == error_mark_node
32726 || !parens.require_close (parser))
32727 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32728 /*or_comma=*/false,
32729 /*consume_paren=*/true);
32731 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32732 "num_tasks", location);
32734 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32735 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32736 OMP_CLAUSE_CHAIN (c) = list;
32738 return c;
32741 /* OpenMP 4.5:
32742 grainsize ( expression ) */
32744 static tree
32745 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32746 location_t location)
32748 tree t, c;
32750 matching_parens parens;
32751 if (!parens.require_open (parser))
32752 return list;
32754 t = cp_parser_assignment_expression (parser);
32756 if (t == error_mark_node
32757 || !parens.require_close (parser))
32758 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32759 /*or_comma=*/false,
32760 /*consume_paren=*/true);
32762 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32763 "grainsize", location);
32765 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32766 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32767 OMP_CLAUSE_CHAIN (c) = list;
32769 return c;
32772 /* OpenMP 4.5:
32773 priority ( expression ) */
32775 static tree
32776 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32777 location_t location)
32779 tree t, c;
32781 matching_parens parens;
32782 if (!parens.require_open (parser))
32783 return list;
32785 t = cp_parser_assignment_expression (parser);
32787 if (t == error_mark_node
32788 || !parens.require_close (parser))
32789 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32790 /*or_comma=*/false,
32791 /*consume_paren=*/true);
32793 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32794 "priority", location);
32796 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32797 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32798 OMP_CLAUSE_CHAIN (c) = list;
32800 return c;
32803 /* OpenMP 4.5:
32804 hint ( expression ) */
32806 static tree
32807 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
32809 tree t, c;
32811 matching_parens parens;
32812 if (!parens.require_open (parser))
32813 return list;
32815 t = cp_parser_assignment_expression (parser);
32817 if (t == error_mark_node
32818 || !parens.require_close (parser))
32819 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32820 /*or_comma=*/false,
32821 /*consume_paren=*/true);
32823 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32825 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32826 OMP_CLAUSE_HINT_EXPR (c) = t;
32827 OMP_CLAUSE_CHAIN (c) = list;
32829 return c;
32832 /* OpenMP 4.5:
32833 defaultmap ( tofrom : scalar )
32835 OpenMP 5.0:
32836 defaultmap ( implicit-behavior [ : variable-category ] ) */
32838 static tree
32839 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32840 location_t location)
32842 tree c, id;
32843 const char *p;
32844 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
32845 enum omp_clause_defaultmap_kind category
32846 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
32848 matching_parens parens;
32849 if (!parens.require_open (parser))
32850 return list;
32852 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
32853 p = "default";
32854 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32856 invalid_behavior:
32857 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
32858 "%<tofrom%>, %<firstprivate%>, %<none%> "
32859 "or %<default%>");
32860 goto out_err;
32862 else
32864 id = cp_lexer_peek_token (parser->lexer)->u.value;
32865 p = IDENTIFIER_POINTER (id);
32868 switch (p[0])
32870 case 'a':
32871 if (strcmp ("alloc", p) == 0)
32872 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
32873 else
32874 goto invalid_behavior;
32875 break;
32877 case 'd':
32878 if (strcmp ("default", p) == 0)
32879 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
32880 else
32881 goto invalid_behavior;
32882 break;
32884 case 'f':
32885 if (strcmp ("firstprivate", p) == 0)
32886 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
32887 else if (strcmp ("from", p) == 0)
32888 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
32889 else
32890 goto invalid_behavior;
32891 break;
32893 case 'n':
32894 if (strcmp ("none", p) == 0)
32895 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
32896 else
32897 goto invalid_behavior;
32898 break;
32900 case 't':
32901 if (strcmp ("tofrom", p) == 0)
32902 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
32903 else if (strcmp ("to", p) == 0)
32904 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
32905 else
32906 goto invalid_behavior;
32907 break;
32909 default:
32910 goto invalid_behavior;
32912 cp_lexer_consume_token (parser->lexer);
32914 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32916 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32917 goto out_err;
32919 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32921 invalid_category:
32922 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
32923 "%<pointer%>");
32924 goto out_err;
32926 id = cp_lexer_peek_token (parser->lexer)->u.value;
32927 p = IDENTIFIER_POINTER (id);
32929 switch (p[0])
32931 case 'a':
32932 if (strcmp ("aggregate", p) == 0)
32933 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
32934 else
32935 goto invalid_category;
32936 break;
32938 case 'p':
32939 if (strcmp ("pointer", p) == 0)
32940 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
32941 else
32942 goto invalid_category;
32943 break;
32945 case 's':
32946 if (strcmp ("scalar", p) == 0)
32947 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
32948 else
32949 goto invalid_category;
32950 break;
32952 default:
32953 goto invalid_category;
32956 cp_lexer_consume_token (parser->lexer);
32958 if (!parens.require_close (parser))
32959 goto out_err;
32961 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32962 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
32963 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
32964 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
32965 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
32966 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
32968 enum omp_clause_defaultmap_kind cat = category;
32969 location_t loc = OMP_CLAUSE_LOCATION (c);
32970 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
32971 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
32972 p = NULL;
32973 switch (cat)
32975 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
32976 p = NULL;
32977 break;
32978 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
32979 p = "aggregate";
32980 break;
32981 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
32982 p = "pointer";
32983 break;
32984 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
32985 p = "scalar";
32986 break;
32987 default:
32988 gcc_unreachable ();
32990 if (p)
32991 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
32993 else
32994 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
32995 "category");
32996 break;
32999 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33000 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33001 OMP_CLAUSE_CHAIN (c) = list;
33002 return c;
33004 out_err:
33005 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33006 /*or_comma=*/false,
33007 /*consume_paren=*/true);
33008 return list;
33011 /* OpenMP 2.5:
33012 ordered
33014 OpenMP 4.5:
33015 ordered ( constant-expression ) */
33017 static tree
33018 cp_parser_omp_clause_ordered (cp_parser *parser,
33019 tree list, location_t location)
33021 tree c, num = NULL_TREE;
33022 HOST_WIDE_INT n;
33024 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33025 "ordered", location);
33027 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33029 matching_parens parens;
33030 parens.consume_open (parser);
33032 num = cp_parser_constant_expression (parser);
33034 if (!parens.require_close (parser))
33035 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33036 /*or_comma=*/false,
33037 /*consume_paren=*/true);
33039 if (num == error_mark_node)
33040 return list;
33041 num = fold_non_dependent_expr (num);
33042 if (!tree_fits_shwi_p (num)
33043 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33044 || (n = tree_to_shwi (num)) <= 0
33045 || (int) n != n)
33047 error_at (location,
33048 "ordered argument needs positive constant integer "
33049 "expression");
33050 return list;
33054 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
33055 OMP_CLAUSE_ORDERED_EXPR (c) = num;
33056 OMP_CLAUSE_CHAIN (c) = list;
33057 return c;
33060 /* OpenMP 2.5:
33061 reduction ( reduction-operator : variable-list )
33063 reduction-operator:
33064 One of: + * - & ^ | && ||
33066 OpenMP 3.1:
33068 reduction-operator:
33069 One of: + * - & ^ | && || min max
33071 OpenMP 4.0:
33073 reduction-operator:
33074 One of: + * - & ^ | && ||
33075 id-expression
33077 OpenMP 5.0:
33078 reduction ( reduction-modifier, reduction-operator : variable-list )
33079 in_reduction ( reduction-operator : variable-list )
33080 task_reduction ( reduction-operator : variable-list ) */
33082 static tree
33083 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
33084 bool is_omp, tree list)
33086 enum tree_code code = ERROR_MARK;
33087 tree nlist, c, id = NULL_TREE;
33088 bool task = false;
33089 bool inscan = false;
33091 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33092 return list;
33094 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
33096 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
33097 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33099 cp_lexer_consume_token (parser->lexer);
33100 cp_lexer_consume_token (parser->lexer);
33102 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33103 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33105 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33106 const char *p = IDENTIFIER_POINTER (id);
33107 if (strcmp (p, "task") == 0)
33108 task = true;
33109 else if (strcmp (p, "inscan") == 0)
33111 inscan = true;
33112 sorry ("%<inscan%> modifier on %<reduction%> clause "
33113 "not supported yet");
33115 if (task || inscan)
33117 cp_lexer_consume_token (parser->lexer);
33118 cp_lexer_consume_token (parser->lexer);
33123 switch (cp_lexer_peek_token (parser->lexer)->type)
33125 case CPP_PLUS: code = PLUS_EXPR; break;
33126 case CPP_MULT: code = MULT_EXPR; break;
33127 case CPP_MINUS: code = MINUS_EXPR; break;
33128 case CPP_AND: code = BIT_AND_EXPR; break;
33129 case CPP_XOR: code = BIT_XOR_EXPR; break;
33130 case CPP_OR: code = BIT_IOR_EXPR; break;
33131 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
33132 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
33133 default: break;
33136 if (code != ERROR_MARK)
33137 cp_lexer_consume_token (parser->lexer);
33138 else
33140 bool saved_colon_corrects_to_scope_p;
33141 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33142 parser->colon_corrects_to_scope_p = false;
33143 id = cp_parser_id_expression (parser, /*template_p=*/false,
33144 /*check_dependency_p=*/true,
33145 /*template_p=*/NULL,
33146 /*declarator_p=*/false,
33147 /*optional_p=*/false);
33148 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33149 if (identifier_p (id))
33151 const char *p = IDENTIFIER_POINTER (id);
33153 if (strcmp (p, "min") == 0)
33154 code = MIN_EXPR;
33155 else if (strcmp (p, "max") == 0)
33156 code = MAX_EXPR;
33157 else if (id == ovl_op_identifier (false, PLUS_EXPR))
33158 code = PLUS_EXPR;
33159 else if (id == ovl_op_identifier (false, MULT_EXPR))
33160 code = MULT_EXPR;
33161 else if (id == ovl_op_identifier (false, MINUS_EXPR))
33162 code = MINUS_EXPR;
33163 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
33164 code = BIT_AND_EXPR;
33165 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
33166 code = BIT_IOR_EXPR;
33167 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
33168 code = BIT_XOR_EXPR;
33169 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
33170 code = TRUTH_ANDIF_EXPR;
33171 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
33172 code = TRUTH_ORIF_EXPR;
33173 id = omp_reduction_id (code, id, NULL_TREE);
33174 tree scope = parser->scope;
33175 if (scope)
33176 id = build_qualified_name (NULL_TREE, scope, id, false);
33177 parser->scope = NULL_TREE;
33178 parser->qualifying_scope = NULL_TREE;
33179 parser->object_scope = NULL_TREE;
33181 else
33183 error ("invalid reduction-identifier");
33184 resync_fail:
33185 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33186 /*or_comma=*/false,
33187 /*consume_paren=*/true);
33188 return list;
33192 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33193 goto resync_fail;
33195 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
33196 NULL);
33197 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33199 OMP_CLAUSE_REDUCTION_CODE (c) = code;
33200 if (task)
33201 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
33202 else if (inscan)
33203 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
33204 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
33207 return nlist;
33210 /* OpenMP 2.5:
33211 schedule ( schedule-kind )
33212 schedule ( schedule-kind , expression )
33214 schedule-kind:
33215 static | dynamic | guided | runtime | auto
33217 OpenMP 4.5:
33218 schedule ( schedule-modifier : schedule-kind )
33219 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33221 schedule-modifier:
33222 simd
33223 monotonic
33224 nonmonotonic */
33226 static tree
33227 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
33229 tree c, t;
33230 int modifiers = 0, nmodifiers = 0;
33232 matching_parens parens;
33233 if (!parens.require_open (parser))
33234 return list;
33236 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
33238 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33240 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33241 const char *p = IDENTIFIER_POINTER (id);
33242 if (strcmp ("simd", p) == 0)
33243 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
33244 else if (strcmp ("monotonic", p) == 0)
33245 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
33246 else if (strcmp ("nonmonotonic", p) == 0)
33247 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
33248 else
33249 break;
33250 cp_lexer_consume_token (parser->lexer);
33251 if (nmodifiers++ == 0
33252 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33253 cp_lexer_consume_token (parser->lexer);
33254 else
33256 cp_parser_require (parser, CPP_COLON, RT_COLON);
33257 break;
33261 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33263 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33264 const char *p = IDENTIFIER_POINTER (id);
33266 switch (p[0])
33268 case 'd':
33269 if (strcmp ("dynamic", p) != 0)
33270 goto invalid_kind;
33271 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
33272 break;
33274 case 'g':
33275 if (strcmp ("guided", p) != 0)
33276 goto invalid_kind;
33277 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
33278 break;
33280 case 'r':
33281 if (strcmp ("runtime", p) != 0)
33282 goto invalid_kind;
33283 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
33284 break;
33286 default:
33287 goto invalid_kind;
33290 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33291 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33292 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33293 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33294 else
33295 goto invalid_kind;
33296 cp_lexer_consume_token (parser->lexer);
33298 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33299 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33300 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33301 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33303 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33304 "specified");
33305 modifiers = 0;
33308 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33310 cp_token *token;
33311 cp_lexer_consume_token (parser->lexer);
33313 token = cp_lexer_peek_token (parser->lexer);
33314 t = cp_parser_assignment_expression (parser);
33316 if (t == error_mark_node)
33317 goto resync_fail;
33318 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
33319 error_at (token->location, "schedule %<runtime%> does not take "
33320 "a %<chunk_size%> parameter");
33321 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
33322 error_at (token->location, "schedule %<auto%> does not take "
33323 "a %<chunk_size%> parameter");
33324 else
33325 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
33327 if (!parens.require_close (parser))
33328 goto resync_fail;
33330 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33331 goto resync_fail;
33333 OMP_CLAUSE_SCHEDULE_KIND (c)
33334 = (enum omp_clause_schedule_kind)
33335 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
33337 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
33338 OMP_CLAUSE_CHAIN (c) = list;
33339 return c;
33341 invalid_kind:
33342 cp_parser_error (parser, "invalid schedule kind");
33343 resync_fail:
33344 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33345 /*or_comma=*/false,
33346 /*consume_paren=*/true);
33347 return list;
33350 /* OpenMP 3.0:
33351 untied */
33353 static tree
33354 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
33355 tree list, location_t location)
33357 tree c;
33359 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
33361 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
33362 OMP_CLAUSE_CHAIN (c) = list;
33363 return c;
33366 /* OpenMP 4.0:
33367 inbranch
33368 notinbranch */
33370 static tree
33371 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33372 tree list, location_t location)
33374 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33375 tree c = build_omp_clause (location, code);
33376 OMP_CLAUSE_CHAIN (c) = list;
33377 return c;
33380 /* OpenMP 4.0:
33381 parallel
33383 sections
33384 taskgroup */
33386 static tree
33387 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33388 enum omp_clause_code code,
33389 tree list, location_t location)
33391 tree c = build_omp_clause (location, code);
33392 OMP_CLAUSE_CHAIN (c) = list;
33393 return c;
33396 /* OpenMP 4.5:
33397 nogroup */
33399 static tree
33400 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33401 tree list, location_t location)
33403 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33404 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33405 OMP_CLAUSE_CHAIN (c) = list;
33406 return c;
33409 /* OpenMP 4.5:
33410 simd
33411 threads */
33413 static tree
33414 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33415 enum omp_clause_code code,
33416 tree list, location_t location)
33418 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33419 tree c = build_omp_clause (location, code);
33420 OMP_CLAUSE_CHAIN (c) = list;
33421 return c;
33424 /* OpenMP 4.0:
33425 num_teams ( expression ) */
33427 static tree
33428 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33429 location_t location)
33431 tree t, c;
33433 matching_parens parens;
33434 if (!parens.require_open (parser))
33435 return list;
33437 t = cp_parser_assignment_expression (parser);
33439 if (t == error_mark_node
33440 || !parens.require_close (parser))
33441 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33442 /*or_comma=*/false,
33443 /*consume_paren=*/true);
33445 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33446 "num_teams", location);
33448 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33449 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33450 OMP_CLAUSE_CHAIN (c) = list;
33452 return c;
33455 /* OpenMP 4.0:
33456 thread_limit ( expression ) */
33458 static tree
33459 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33460 location_t location)
33462 tree t, c;
33464 matching_parens parens;
33465 if (!parens.require_open (parser))
33466 return list;
33468 t = cp_parser_assignment_expression (parser);
33470 if (t == error_mark_node
33471 || !parens.require_close (parser))
33472 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33473 /*or_comma=*/false,
33474 /*consume_paren=*/true);
33476 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33477 "thread_limit", location);
33479 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33480 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33481 OMP_CLAUSE_CHAIN (c) = list;
33483 return c;
33486 /* OpenMP 4.0:
33487 aligned ( variable-list )
33488 aligned ( variable-list : constant-expression ) */
33490 static tree
33491 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33493 tree nlist, c, alignment = NULL_TREE;
33494 bool colon;
33496 matching_parens parens;
33497 if (!parens.require_open (parser))
33498 return list;
33500 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33501 &colon);
33503 if (colon)
33505 alignment = cp_parser_constant_expression (parser);
33507 if (!parens.require_close (parser))
33508 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33509 /*or_comma=*/false,
33510 /*consume_paren=*/true);
33512 if (alignment == error_mark_node)
33513 alignment = NULL_TREE;
33516 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33517 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33519 return nlist;
33522 /* OpenMP 2.5:
33523 lastprivate ( variable-list )
33525 OpenMP 5.0:
33526 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
33528 static tree
33529 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
33531 bool conditional = false;
33533 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33534 return list;
33536 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33537 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
33539 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33540 const char *p = IDENTIFIER_POINTER (id);
33542 if (strcmp ("conditional", p) == 0)
33544 conditional = true;
33545 cp_lexer_consume_token (parser->lexer);
33546 cp_lexer_consume_token (parser->lexer);
33550 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
33551 list, NULL);
33553 if (conditional)
33554 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33555 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
33556 return nlist;
33559 /* OpenMP 4.0:
33560 linear ( variable-list )
33561 linear ( variable-list : expression )
33563 OpenMP 4.5:
33564 linear ( modifier ( variable-list ) )
33565 linear ( modifier ( variable-list ) : expression ) */
33567 static tree
33568 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33569 bool declare_simd)
33571 tree nlist, c, step = integer_one_node;
33572 bool colon;
33573 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33575 matching_parens parens;
33576 if (!parens.require_open (parser))
33577 return list;
33579 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33581 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33582 const char *p = IDENTIFIER_POINTER (id);
33584 if (strcmp ("ref", p) == 0)
33585 kind = OMP_CLAUSE_LINEAR_REF;
33586 else if (strcmp ("val", p) == 0)
33587 kind = OMP_CLAUSE_LINEAR_VAL;
33588 else if (strcmp ("uval", p) == 0)
33589 kind = OMP_CLAUSE_LINEAR_UVAL;
33590 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33591 cp_lexer_consume_token (parser->lexer);
33592 else
33593 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33596 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33597 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33598 &colon);
33599 else
33601 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33602 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33603 if (colon)
33604 cp_parser_require (parser, CPP_COLON, RT_COLON);
33605 else if (!parens.require_close (parser))
33606 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33607 /*or_comma=*/false,
33608 /*consume_paren=*/true);
33611 if (colon)
33613 step = NULL_TREE;
33614 if (declare_simd
33615 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33616 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33618 cp_token *token = cp_lexer_peek_token (parser->lexer);
33619 cp_parser_parse_tentatively (parser);
33620 step = cp_parser_id_expression (parser, /*template_p=*/false,
33621 /*check_dependency_p=*/true,
33622 /*template_p=*/NULL,
33623 /*declarator_p=*/false,
33624 /*optional_p=*/false);
33625 if (step != error_mark_node)
33626 step = cp_parser_lookup_name_simple (parser, step, token->location);
33627 if (step == error_mark_node)
33629 step = NULL_TREE;
33630 cp_parser_abort_tentative_parse (parser);
33632 else if (!cp_parser_parse_definitely (parser))
33633 step = NULL_TREE;
33635 if (!step)
33636 step = cp_parser_assignment_expression (parser);
33638 if (!parens.require_close (parser))
33639 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33640 /*or_comma=*/false,
33641 /*consume_paren=*/true);
33643 if (step == error_mark_node)
33644 return list;
33647 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33649 OMP_CLAUSE_LINEAR_STEP (c) = step;
33650 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33653 return nlist;
33656 /* OpenMP 4.0:
33657 safelen ( constant-expression ) */
33659 static tree
33660 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33661 location_t location)
33663 tree t, c;
33665 matching_parens parens;
33666 if (!parens.require_open (parser))
33667 return list;
33669 t = cp_parser_constant_expression (parser);
33671 if (t == error_mark_node
33672 || !parens.require_close (parser))
33673 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33674 /*or_comma=*/false,
33675 /*consume_paren=*/true);
33677 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33679 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33680 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33681 OMP_CLAUSE_CHAIN (c) = list;
33683 return c;
33686 /* OpenMP 4.0:
33687 simdlen ( constant-expression ) */
33689 static tree
33690 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33691 location_t location)
33693 tree t, c;
33695 matching_parens parens;
33696 if (!parens.require_open (parser))
33697 return list;
33699 t = cp_parser_constant_expression (parser);
33701 if (t == error_mark_node
33702 || !parens.require_close (parser))
33703 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33704 /*or_comma=*/false,
33705 /*consume_paren=*/true);
33707 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33709 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33710 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33711 OMP_CLAUSE_CHAIN (c) = list;
33713 return c;
33716 /* OpenMP 4.5:
33717 vec:
33718 identifier [+/- integer]
33719 vec , identifier [+/- integer]
33722 static tree
33723 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33724 tree list)
33726 tree vec = NULL;
33728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33730 cp_parser_error (parser, "expected identifier");
33731 return list;
33734 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33736 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33737 tree t, identifier = cp_parser_identifier (parser);
33738 tree addend = NULL;
33740 if (identifier == error_mark_node)
33741 t = error_mark_node;
33742 else
33744 t = cp_parser_lookup_name_simple
33745 (parser, identifier,
33746 cp_lexer_peek_token (parser->lexer)->location);
33747 if (t == error_mark_node)
33748 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33749 id_loc);
33752 bool neg = false;
33753 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33754 neg = true;
33755 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33757 addend = integer_zero_node;
33758 goto add_to_vector;
33760 cp_lexer_consume_token (parser->lexer);
33762 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33764 cp_parser_error (parser, "expected integer");
33765 return list;
33768 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33769 if (TREE_CODE (addend) != INTEGER_CST)
33771 cp_parser_error (parser, "expected integer");
33772 return list;
33774 cp_lexer_consume_token (parser->lexer);
33776 add_to_vector:
33777 if (t != error_mark_node)
33779 vec = tree_cons (addend, t, vec);
33780 if (neg)
33781 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33784 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33785 break;
33787 cp_lexer_consume_token (parser->lexer);
33790 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33792 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33793 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33794 OMP_CLAUSE_DECL (u) = nreverse (vec);
33795 OMP_CLAUSE_CHAIN (u) = list;
33796 return u;
33798 return list;
33801 /* OpenMP 5.0:
33802 iterators ( iterators-definition )
33804 iterators-definition:
33805 iterator-specifier
33806 iterator-specifier , iterators-definition
33808 iterator-specifier:
33809 identifier = range-specification
33810 iterator-type identifier = range-specification
33812 range-specification:
33813 begin : end
33814 begin : end : step */
33816 static tree
33817 cp_parser_omp_iterators (cp_parser *parser)
33819 tree ret = NULL_TREE, *last = &ret;
33820 cp_lexer_consume_token (parser->lexer);
33822 matching_parens parens;
33823 if (!parens.require_open (parser))
33824 return error_mark_node;
33826 bool saved_colon_corrects_to_scope_p
33827 = parser->colon_corrects_to_scope_p;
33828 bool saved_colon_doesnt_start_class_def_p
33829 = parser->colon_doesnt_start_class_def_p;
33833 tree iter_type;
33834 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33835 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
33836 iter_type = integer_type_node;
33837 else
33839 const char *saved_message
33840 = parser->type_definition_forbidden_message;
33841 parser->type_definition_forbidden_message
33842 = G_("types may not be defined in iterator type");
33844 iter_type = cp_parser_type_id (parser);
33846 parser->type_definition_forbidden_message = saved_message;
33849 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33850 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33852 cp_parser_error (parser, "expected identifier");
33853 break;
33856 tree id = cp_parser_identifier (parser);
33857 if (id == error_mark_node)
33858 break;
33860 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33861 break;
33863 parser->colon_corrects_to_scope_p = false;
33864 parser->colon_doesnt_start_class_def_p = true;
33865 tree begin = cp_parser_assignment_expression (parser);
33867 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33868 break;
33870 tree end = cp_parser_assignment_expression (parser);
33872 tree step = integer_one_node;
33873 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
33875 cp_lexer_consume_token (parser->lexer);
33876 step = cp_parser_assignment_expression (parser);
33879 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
33880 DECL_ARTIFICIAL (iter_var) = 1;
33881 DECL_CONTEXT (iter_var) = current_function_decl;
33882 pushdecl (iter_var);
33884 *last = make_tree_vec (6);
33885 TREE_VEC_ELT (*last, 0) = iter_var;
33886 TREE_VEC_ELT (*last, 1) = begin;
33887 TREE_VEC_ELT (*last, 2) = end;
33888 TREE_VEC_ELT (*last, 3) = step;
33889 last = &TREE_CHAIN (*last);
33891 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33893 cp_lexer_consume_token (parser->lexer);
33894 continue;
33896 break;
33898 while (1);
33900 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33901 parser->colon_doesnt_start_class_def_p
33902 = saved_colon_doesnt_start_class_def_p;
33904 if (!parens.require_close (parser))
33905 cp_parser_skip_to_closing_parenthesis (parser,
33906 /*recovering=*/true,
33907 /*or_comma=*/false,
33908 /*consume_paren=*/true);
33910 return ret ? ret : error_mark_node;
33913 /* OpenMP 4.0:
33914 depend ( depend-kind : variable-list )
33916 depend-kind:
33917 in | out | inout
33919 OpenMP 4.5:
33920 depend ( source )
33922 depend ( sink : vec )
33924 OpenMP 5.0:
33925 depend ( depend-modifier , depend-kind: variable-list )
33927 depend-kind:
33928 in | out | inout | mutexinoutset | depobj
33930 depend-modifier:
33931 iterator ( iterators-definition ) */
33933 static tree
33934 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33936 tree nlist, c, iterators = NULL_TREE;
33937 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
33939 matching_parens parens;
33940 if (!parens.require_open (parser))
33941 return list;
33945 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33946 goto invalid_kind;
33948 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33949 const char *p = IDENTIFIER_POINTER (id);
33951 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
33953 begin_scope (sk_omp, NULL);
33954 iterators = cp_parser_omp_iterators (parser);
33955 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
33956 continue;
33958 if (strcmp ("in", p) == 0)
33959 kind = OMP_CLAUSE_DEPEND_IN;
33960 else if (strcmp ("inout", p) == 0)
33961 kind = OMP_CLAUSE_DEPEND_INOUT;
33962 else if (strcmp ("mutexinoutset", p) == 0)
33963 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
33964 else if (strcmp ("out", p) == 0)
33965 kind = OMP_CLAUSE_DEPEND_OUT;
33966 else if (strcmp ("depobj", p) == 0)
33967 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
33968 else if (strcmp ("sink", p) == 0)
33969 kind = OMP_CLAUSE_DEPEND_SINK;
33970 else if (strcmp ("source", p) == 0)
33971 kind = OMP_CLAUSE_DEPEND_SOURCE;
33972 else
33973 goto invalid_kind;
33974 break;
33976 while (1);
33978 cp_lexer_consume_token (parser->lexer);
33980 if (iterators
33981 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
33983 poplevel (0, 1, 0);
33984 error_at (loc, "%<iterator%> modifier incompatible with %qs",
33985 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
33986 iterators = NULL_TREE;
33989 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33991 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33992 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33993 OMP_CLAUSE_DECL (c) = NULL_TREE;
33994 OMP_CLAUSE_CHAIN (c) = list;
33995 if (!parens.require_close (parser))
33996 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33997 /*or_comma=*/false,
33998 /*consume_paren=*/true);
33999 return c;
34002 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34003 goto resync_fail;
34005 if (kind == OMP_CLAUSE_DEPEND_SINK)
34006 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34007 else
34009 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34010 list, NULL);
34012 if (iterators)
34014 tree block = poplevel (1, 1, 0);
34015 if (iterators == error_mark_node)
34016 iterators = NULL_TREE;
34017 else
34018 TREE_VEC_ELT (iterators, 5) = block;
34021 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34023 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34024 if (iterators)
34025 OMP_CLAUSE_DECL (c)
34026 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34029 return nlist;
34031 invalid_kind:
34032 cp_parser_error (parser, "invalid depend kind");
34033 resync_fail:
34034 if (iterators)
34035 poplevel (0, 1, 0);
34036 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34037 /*or_comma=*/false,
34038 /*consume_paren=*/true);
34039 return list;
34042 /* OpenMP 4.0:
34043 map ( map-kind : variable-list )
34044 map ( variable-list )
34046 map-kind:
34047 alloc | to | from | tofrom
34049 OpenMP 4.5:
34050 map-kind:
34051 alloc | to | from | tofrom | release | delete
34053 map ( always [,] map-kind: variable-list ) */
34055 static tree
34056 cp_parser_omp_clause_map (cp_parser *parser, tree list)
34058 tree nlist, c;
34059 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
34060 bool always = false;
34062 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34063 return list;
34065 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34067 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34068 const char *p = IDENTIFIER_POINTER (id);
34070 if (strcmp ("always", p) == 0)
34072 int nth = 2;
34073 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
34074 nth++;
34075 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
34076 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
34077 == RID_DELETE))
34078 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
34079 == CPP_COLON))
34081 always = true;
34082 cp_lexer_consume_token (parser->lexer);
34083 if (nth == 3)
34084 cp_lexer_consume_token (parser->lexer);
34089 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34090 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34092 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34093 const char *p = IDENTIFIER_POINTER (id);
34095 if (strcmp ("alloc", p) == 0)
34096 kind = GOMP_MAP_ALLOC;
34097 else if (strcmp ("to", p) == 0)
34098 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
34099 else if (strcmp ("from", p) == 0)
34100 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
34101 else if (strcmp ("tofrom", p) == 0)
34102 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
34103 else if (strcmp ("release", p) == 0)
34104 kind = GOMP_MAP_RELEASE;
34105 else
34107 cp_parser_error (parser, "invalid map kind");
34108 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34109 /*or_comma=*/false,
34110 /*consume_paren=*/true);
34111 return list;
34113 cp_lexer_consume_token (parser->lexer);
34114 cp_lexer_consume_token (parser->lexer);
34116 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
34117 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34119 kind = GOMP_MAP_DELETE;
34120 cp_lexer_consume_token (parser->lexer);
34121 cp_lexer_consume_token (parser->lexer);
34124 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
34125 NULL);
34127 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34128 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34130 return nlist;
34133 /* OpenMP 4.0:
34134 device ( expression ) */
34136 static tree
34137 cp_parser_omp_clause_device (cp_parser *parser, tree list,
34138 location_t location)
34140 tree t, c;
34142 matching_parens parens;
34143 if (!parens.require_open (parser))
34144 return list;
34146 t = cp_parser_assignment_expression (parser);
34148 if (t == error_mark_node
34149 || !parens.require_close (parser))
34150 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34151 /*or_comma=*/false,
34152 /*consume_paren=*/true);
34154 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
34155 "device", location);
34157 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
34158 OMP_CLAUSE_DEVICE_ID (c) = t;
34159 OMP_CLAUSE_CHAIN (c) = list;
34161 return c;
34164 /* OpenMP 4.0:
34165 dist_schedule ( static )
34166 dist_schedule ( static , expression ) */
34168 static tree
34169 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
34170 location_t location)
34172 tree c, t;
34174 matching_parens parens;
34175 if (!parens.require_open (parser))
34176 return list;
34178 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
34180 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34181 goto invalid_kind;
34182 cp_lexer_consume_token (parser->lexer);
34184 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34186 cp_lexer_consume_token (parser->lexer);
34188 t = cp_parser_assignment_expression (parser);
34190 if (t == error_mark_node)
34191 goto resync_fail;
34192 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
34194 if (!parens.require_close (parser))
34195 goto resync_fail;
34197 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34198 goto resync_fail;
34200 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
34201 location);
34202 OMP_CLAUSE_CHAIN (c) = list;
34203 return c;
34205 invalid_kind:
34206 cp_parser_error (parser, "invalid dist_schedule kind");
34207 resync_fail:
34208 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34209 /*or_comma=*/false,
34210 /*consume_paren=*/true);
34211 return list;
34214 /* OpenMP 4.0:
34215 proc_bind ( proc-bind-kind )
34217 proc-bind-kind:
34218 master | close | spread */
34220 static tree
34221 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
34222 location_t location)
34224 tree c;
34225 enum omp_clause_proc_bind_kind kind;
34227 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34228 return list;
34230 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34232 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34233 const char *p = IDENTIFIER_POINTER (id);
34235 if (strcmp ("master", p) == 0)
34236 kind = OMP_CLAUSE_PROC_BIND_MASTER;
34237 else if (strcmp ("close", p) == 0)
34238 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
34239 else if (strcmp ("spread", p) == 0)
34240 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
34241 else
34242 goto invalid_kind;
34244 else
34245 goto invalid_kind;
34247 cp_lexer_consume_token (parser->lexer);
34248 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34249 goto resync_fail;
34251 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
34252 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
34253 location);
34254 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
34255 OMP_CLAUSE_CHAIN (c) = list;
34256 return c;
34258 invalid_kind:
34259 cp_parser_error (parser, "invalid depend kind");
34260 resync_fail:
34261 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34262 /*or_comma=*/false,
34263 /*consume_paren=*/true);
34264 return list;
34267 /* OpenACC:
34268 async [( int-expr )] */
34270 static tree
34271 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
34273 tree c, t;
34274 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34276 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34278 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34280 matching_parens parens;
34281 parens.consume_open (parser);
34283 t = cp_parser_expression (parser);
34284 if (t == error_mark_node
34285 || !parens.require_close (parser))
34286 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34287 /*or_comma=*/false,
34288 /*consume_paren=*/true);
34291 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
34293 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
34294 OMP_CLAUSE_ASYNC_EXPR (c) = t;
34295 OMP_CLAUSE_CHAIN (c) = list;
34296 list = c;
34298 return list;
34301 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34302 is a bitmask in MASK. Return the list of clauses found. */
34304 static tree
34305 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
34306 const char *where, cp_token *pragma_tok,
34307 bool finish_p = true)
34309 tree clauses = NULL;
34310 bool first = true;
34312 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34314 location_t here;
34315 pragma_omp_clause c_kind;
34316 omp_clause_code code;
34317 const char *c_name;
34318 tree prev = clauses;
34320 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34321 cp_lexer_consume_token (parser->lexer);
34323 here = cp_lexer_peek_token (parser->lexer)->location;
34324 c_kind = cp_parser_omp_clause_name (parser);
34326 switch (c_kind)
34328 case PRAGMA_OACC_CLAUSE_ASYNC:
34329 clauses = cp_parser_oacc_clause_async (parser, clauses);
34330 c_name = "async";
34331 break;
34332 case PRAGMA_OACC_CLAUSE_AUTO:
34333 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
34334 clauses, here);
34335 c_name = "auto";
34336 break;
34337 case PRAGMA_OACC_CLAUSE_COLLAPSE:
34338 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
34339 c_name = "collapse";
34340 break;
34341 case PRAGMA_OACC_CLAUSE_COPY:
34342 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34343 c_name = "copy";
34344 break;
34345 case PRAGMA_OACC_CLAUSE_COPYIN:
34346 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34347 c_name = "copyin";
34348 break;
34349 case PRAGMA_OACC_CLAUSE_COPYOUT:
34350 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34351 c_name = "copyout";
34352 break;
34353 case PRAGMA_OACC_CLAUSE_CREATE:
34354 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34355 c_name = "create";
34356 break;
34357 case PRAGMA_OACC_CLAUSE_DELETE:
34358 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34359 c_name = "delete";
34360 break;
34361 case PRAGMA_OMP_CLAUSE_DEFAULT:
34362 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
34363 c_name = "default";
34364 break;
34365 case PRAGMA_OACC_CLAUSE_DEVICE:
34366 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34367 c_name = "device";
34368 break;
34369 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
34370 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
34371 c_name = "deviceptr";
34372 break;
34373 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
34374 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34375 c_name = "device_resident";
34376 break;
34377 case PRAGMA_OACC_CLAUSE_FINALIZE:
34378 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
34379 clauses, here);
34380 c_name = "finalize";
34381 break;
34382 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
34383 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34384 clauses);
34385 c_name = "firstprivate";
34386 break;
34387 case PRAGMA_OACC_CLAUSE_GANG:
34388 c_name = "gang";
34389 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
34390 c_name, clauses);
34391 break;
34392 case PRAGMA_OACC_CLAUSE_HOST:
34393 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34394 c_name = "host";
34395 break;
34396 case PRAGMA_OACC_CLAUSE_IF:
34397 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
34398 c_name = "if";
34399 break;
34400 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
34401 clauses = cp_parser_oacc_simple_clause (parser,
34402 OMP_CLAUSE_IF_PRESENT,
34403 clauses, here);
34404 c_name = "if_present";
34405 break;
34406 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
34407 clauses = cp_parser_oacc_simple_clause (parser,
34408 OMP_CLAUSE_INDEPENDENT,
34409 clauses, here);
34410 c_name = "independent";
34411 break;
34412 case PRAGMA_OACC_CLAUSE_LINK:
34413 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34414 c_name = "link";
34415 break;
34416 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
34417 code = OMP_CLAUSE_NUM_GANGS;
34418 c_name = "num_gangs";
34419 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34420 clauses);
34421 break;
34422 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
34423 c_name = "num_workers";
34424 code = OMP_CLAUSE_NUM_WORKERS;
34425 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34426 clauses);
34427 break;
34428 case PRAGMA_OACC_CLAUSE_PRESENT:
34429 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34430 c_name = "present";
34431 break;
34432 case PRAGMA_OACC_CLAUSE_PRIVATE:
34433 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34434 clauses);
34435 c_name = "private";
34436 break;
34437 case PRAGMA_OACC_CLAUSE_REDUCTION:
34438 clauses
34439 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34440 false, clauses);
34441 c_name = "reduction";
34442 break;
34443 case PRAGMA_OACC_CLAUSE_SEQ:
34444 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
34445 clauses, here);
34446 c_name = "seq";
34447 break;
34448 case PRAGMA_OACC_CLAUSE_TILE:
34449 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
34450 c_name = "tile";
34451 break;
34452 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
34453 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34454 clauses);
34455 c_name = "use_device";
34456 break;
34457 case PRAGMA_OACC_CLAUSE_VECTOR:
34458 c_name = "vector";
34459 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
34460 c_name, clauses);
34461 break;
34462 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
34463 c_name = "vector_length";
34464 code = OMP_CLAUSE_VECTOR_LENGTH;
34465 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34466 clauses);
34467 break;
34468 case PRAGMA_OACC_CLAUSE_WAIT:
34469 clauses = cp_parser_oacc_clause_wait (parser, clauses);
34470 c_name = "wait";
34471 break;
34472 case PRAGMA_OACC_CLAUSE_WORKER:
34473 c_name = "worker";
34474 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
34475 c_name, clauses);
34476 break;
34477 default:
34478 cp_parser_error (parser, "expected %<#pragma acc%> clause");
34479 goto saw_error;
34482 first = false;
34484 if (((mask >> c_kind) & 1) == 0)
34486 /* Remove the invalid clause(s) from the list to avoid
34487 confusing the rest of the compiler. */
34488 clauses = prev;
34489 error_at (here, "%qs is not valid for %qs", c_name, where);
34493 saw_error:
34494 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34496 if (finish_p)
34497 return finish_omp_clauses (clauses, C_ORT_ACC);
34499 return clauses;
34502 /* Parse all OpenMP clauses. The set clauses allowed by the directive
34503 is a bitmask in MASK. Return the list of clauses found; the result
34504 of clause default goes in *pdefault. */
34506 static tree
34507 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
34508 const char *where, cp_token *pragma_tok,
34509 bool finish_p = true)
34511 tree clauses = NULL;
34512 bool first = true;
34513 cp_token *token = NULL;
34515 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34517 pragma_omp_clause c_kind;
34518 const char *c_name;
34519 tree prev = clauses;
34521 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34522 cp_lexer_consume_token (parser->lexer);
34524 token = cp_lexer_peek_token (parser->lexer);
34525 c_kind = cp_parser_omp_clause_name (parser);
34527 switch (c_kind)
34529 case PRAGMA_OMP_CLAUSE_COLLAPSE:
34530 clauses = cp_parser_omp_clause_collapse (parser, clauses,
34531 token->location);
34532 c_name = "collapse";
34533 break;
34534 case PRAGMA_OMP_CLAUSE_COPYIN:
34535 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
34536 c_name = "copyin";
34537 break;
34538 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
34539 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
34540 clauses);
34541 c_name = "copyprivate";
34542 break;
34543 case PRAGMA_OMP_CLAUSE_DEFAULT:
34544 clauses = cp_parser_omp_clause_default (parser, clauses,
34545 token->location, false);
34546 c_name = "default";
34547 break;
34548 case PRAGMA_OMP_CLAUSE_FINAL:
34549 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
34550 c_name = "final";
34551 break;
34552 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
34553 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34554 clauses);
34555 c_name = "firstprivate";
34556 break;
34557 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
34558 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
34559 token->location);
34560 c_name = "grainsize";
34561 break;
34562 case PRAGMA_OMP_CLAUSE_HINT:
34563 clauses = cp_parser_omp_clause_hint (parser, clauses,
34564 token->location);
34565 c_name = "hint";
34566 break;
34567 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
34568 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34569 token->location);
34570 c_name = "defaultmap";
34571 break;
34572 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34573 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34574 clauses);
34575 c_name = "use_device_ptr";
34576 break;
34577 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34578 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34579 clauses);
34580 c_name = "is_device_ptr";
34581 break;
34582 case PRAGMA_OMP_CLAUSE_IF:
34583 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34584 true);
34585 c_name = "if";
34586 break;
34587 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
34588 clauses
34589 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
34590 true, clauses);
34591 c_name = "in_reduction";
34592 break;
34593 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34594 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
34595 c_name = "lastprivate";
34596 break;
34597 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34598 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34599 token->location);
34600 c_name = "mergeable";
34601 break;
34602 case PRAGMA_OMP_CLAUSE_NOWAIT:
34603 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34604 c_name = "nowait";
34605 break;
34606 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34607 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34608 token->location);
34609 c_name = "num_tasks";
34610 break;
34611 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34612 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34613 token->location);
34614 c_name = "num_threads";
34615 break;
34616 case PRAGMA_OMP_CLAUSE_ORDERED:
34617 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34618 token->location);
34619 c_name = "ordered";
34620 break;
34621 case PRAGMA_OMP_CLAUSE_PRIORITY:
34622 clauses = cp_parser_omp_clause_priority (parser, clauses,
34623 token->location);
34624 c_name = "priority";
34625 break;
34626 case PRAGMA_OMP_CLAUSE_PRIVATE:
34627 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34628 clauses);
34629 c_name = "private";
34630 break;
34631 case PRAGMA_OMP_CLAUSE_REDUCTION:
34632 clauses
34633 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34634 true, clauses);
34635 c_name = "reduction";
34636 break;
34637 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34638 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34639 token->location);
34640 c_name = "schedule";
34641 break;
34642 case PRAGMA_OMP_CLAUSE_SHARED:
34643 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34644 clauses);
34645 c_name = "shared";
34646 break;
34647 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
34648 clauses
34649 = cp_parser_omp_clause_reduction (parser,
34650 OMP_CLAUSE_TASK_REDUCTION,
34651 true, clauses);
34652 c_name = "task_reduction";
34653 break;
34654 case PRAGMA_OMP_CLAUSE_UNTIED:
34655 clauses = cp_parser_omp_clause_untied (parser, clauses,
34656 token->location);
34657 c_name = "untied";
34658 break;
34659 case PRAGMA_OMP_CLAUSE_INBRANCH:
34660 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34661 clauses, token->location);
34662 c_name = "inbranch";
34663 break;
34664 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
34665 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
34666 clauses);
34667 c_name = "nontemporal";
34668 break;
34669 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34670 clauses = cp_parser_omp_clause_branch (parser,
34671 OMP_CLAUSE_NOTINBRANCH,
34672 clauses, token->location);
34673 c_name = "notinbranch";
34674 break;
34675 case PRAGMA_OMP_CLAUSE_PARALLEL:
34676 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34677 clauses, token->location);
34678 c_name = "parallel";
34679 if (!first)
34681 clause_not_first:
34682 error_at (token->location, "%qs must be the first clause of %qs",
34683 c_name, where);
34684 clauses = prev;
34686 break;
34687 case PRAGMA_OMP_CLAUSE_FOR:
34688 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34689 clauses, token->location);
34690 c_name = "for";
34691 if (!first)
34692 goto clause_not_first;
34693 break;
34694 case PRAGMA_OMP_CLAUSE_SECTIONS:
34695 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34696 clauses, token->location);
34697 c_name = "sections";
34698 if (!first)
34699 goto clause_not_first;
34700 break;
34701 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34702 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34703 clauses, token->location);
34704 c_name = "taskgroup";
34705 if (!first)
34706 goto clause_not_first;
34707 break;
34708 case PRAGMA_OMP_CLAUSE_LINK:
34709 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34710 c_name = "to";
34711 break;
34712 case PRAGMA_OMP_CLAUSE_TO:
34713 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34714 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34715 clauses);
34716 else
34717 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34718 c_name = "to";
34719 break;
34720 case PRAGMA_OMP_CLAUSE_FROM:
34721 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34722 c_name = "from";
34723 break;
34724 case PRAGMA_OMP_CLAUSE_UNIFORM:
34725 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34726 clauses);
34727 c_name = "uniform";
34728 break;
34729 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34730 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34731 token->location);
34732 c_name = "num_teams";
34733 break;
34734 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34735 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34736 token->location);
34737 c_name = "thread_limit";
34738 break;
34739 case PRAGMA_OMP_CLAUSE_ALIGNED:
34740 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34741 c_name = "aligned";
34742 break;
34743 case PRAGMA_OMP_CLAUSE_LINEAR:
34745 bool declare_simd = false;
34746 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34747 declare_simd = true;
34748 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34750 c_name = "linear";
34751 break;
34752 case PRAGMA_OMP_CLAUSE_DEPEND:
34753 clauses = cp_parser_omp_clause_depend (parser, clauses,
34754 token->location);
34755 c_name = "depend";
34756 break;
34757 case PRAGMA_OMP_CLAUSE_MAP:
34758 clauses = cp_parser_omp_clause_map (parser, clauses);
34759 c_name = "map";
34760 break;
34761 case PRAGMA_OMP_CLAUSE_DEVICE:
34762 clauses = cp_parser_omp_clause_device (parser, clauses,
34763 token->location);
34764 c_name = "device";
34765 break;
34766 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34767 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34768 token->location);
34769 c_name = "dist_schedule";
34770 break;
34771 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34772 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34773 token->location);
34774 c_name = "proc_bind";
34775 break;
34776 case PRAGMA_OMP_CLAUSE_SAFELEN:
34777 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34778 token->location);
34779 c_name = "safelen";
34780 break;
34781 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34782 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34783 token->location);
34784 c_name = "simdlen";
34785 break;
34786 case PRAGMA_OMP_CLAUSE_NOGROUP:
34787 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34788 token->location);
34789 c_name = "nogroup";
34790 break;
34791 case PRAGMA_OMP_CLAUSE_THREADS:
34792 clauses
34793 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34794 clauses, token->location);
34795 c_name = "threads";
34796 break;
34797 case PRAGMA_OMP_CLAUSE_SIMD:
34798 clauses
34799 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34800 clauses, token->location);
34801 c_name = "simd";
34802 break;
34803 default:
34804 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34805 goto saw_error;
34808 first = false;
34810 if (((mask >> c_kind) & 1) == 0)
34812 /* Remove the invalid clause(s) from the list to avoid
34813 confusing the rest of the compiler. */
34814 clauses = prev;
34815 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34818 saw_error:
34819 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34820 if (finish_p)
34822 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34823 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34824 else
34825 return finish_omp_clauses (clauses, C_ORT_OMP);
34827 return clauses;
34830 /* OpenMP 2.5:
34831 structured-block:
34832 statement
34834 In practice, we're also interested in adding the statement to an
34835 outer node. So it is convenient if we work around the fact that
34836 cp_parser_statement calls add_stmt. */
34838 static unsigned
34839 cp_parser_begin_omp_structured_block (cp_parser *parser)
34841 unsigned save = parser->in_statement;
34843 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34844 This preserves the "not within loop or switch" style error messages
34845 for nonsense cases like
34846 void foo() {
34847 #pragma omp single
34848 break;
34851 if (parser->in_statement)
34852 parser->in_statement = IN_OMP_BLOCK;
34854 return save;
34857 static void
34858 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34860 parser->in_statement = save;
34863 static tree
34864 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34866 tree stmt = begin_omp_structured_block ();
34867 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34869 cp_parser_statement (parser, NULL_TREE, false, if_p);
34871 cp_parser_end_omp_structured_block (parser, save);
34872 return finish_omp_structured_block (stmt);
34875 /* OpenMP 2.5:
34876 # pragma omp atomic new-line
34877 expression-stmt
34879 expression-stmt:
34880 x binop= expr | x++ | ++x | x-- | --x
34881 binop:
34882 +, *, -, /, &, ^, |, <<, >>
34884 where x is an lvalue expression with scalar type.
34886 OpenMP 3.1:
34887 # pragma omp atomic new-line
34888 update-stmt
34890 # pragma omp atomic read new-line
34891 read-stmt
34893 # pragma omp atomic write new-line
34894 write-stmt
34896 # pragma omp atomic update new-line
34897 update-stmt
34899 # pragma omp atomic capture new-line
34900 capture-stmt
34902 # pragma omp atomic capture new-line
34903 capture-block
34905 read-stmt:
34906 v = x
34907 write-stmt:
34908 x = expr
34909 update-stmt:
34910 expression-stmt | x = x binop expr
34911 capture-stmt:
34912 v = expression-stmt
34913 capture-block:
34914 { v = x; update-stmt; } | { update-stmt; v = x; }
34916 OpenMP 4.0:
34917 update-stmt:
34918 expression-stmt | x = x binop expr | x = expr binop x
34919 capture-stmt:
34920 v = update-stmt
34921 capture-block:
34922 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34924 where x and v are lvalue expressions with scalar type. */
34926 static void
34927 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34929 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34930 tree rhs1 = NULL_TREE, orig_lhs;
34931 location_t loc = pragma_tok->location;
34932 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
34933 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
34934 bool structured_block = false;
34935 bool first = true;
34936 tree clauses = NULL_TREE;
34938 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34940 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34941 cp_lexer_consume_token (parser->lexer);
34943 first = false;
34945 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34947 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34948 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
34949 const char *p = IDENTIFIER_POINTER (id);
34950 enum tree_code new_code = ERROR_MARK;
34951 enum omp_memory_order new_memory_order
34952 = OMP_MEMORY_ORDER_UNSPECIFIED;
34954 if (!strcmp (p, "read"))
34955 new_code = OMP_ATOMIC_READ;
34956 else if (!strcmp (p, "write"))
34957 new_code = NOP_EXPR;
34958 else if (!strcmp (p, "update"))
34959 new_code = OMP_ATOMIC;
34960 else if (!strcmp (p, "capture"))
34961 new_code = OMP_ATOMIC_CAPTURE_NEW;
34962 else if (!strcmp (p, "seq_cst"))
34963 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
34964 else if (!strcmp (p, "acq_rel"))
34965 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
34966 else if (!strcmp (p, "release"))
34967 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
34968 else if (!strcmp (p, "acquire"))
34969 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
34970 else if (!strcmp (p, "relaxed"))
34971 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
34972 else if (!strcmp (p, "hint"))
34974 cp_lexer_consume_token (parser->lexer);
34975 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
34976 continue;
34978 else
34980 p = NULL;
34981 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
34982 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
34983 "%<release%>, %<relaxed%> or %<hint%> clause");
34985 if (p)
34987 if (new_code != ERROR_MARK)
34989 if (code != ERROR_MARK)
34990 error_at (cloc, "too many atomic clauses");
34991 else
34992 code = new_code;
34994 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
34996 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
34997 error_at (cloc, "too many memory order clauses");
34998 else
34999 memory_order = new_memory_order;
35001 cp_lexer_consume_token (parser->lexer);
35002 continue;
35005 break;
35007 cp_parser_require_pragma_eol (parser, pragma_tok);
35009 if (code == ERROR_MARK)
35010 code = OMP_ATOMIC;
35011 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35013 omp_requires_mask
35014 = (enum omp_requires) (omp_requires_mask
35015 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35016 switch ((enum omp_memory_order)
35017 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35019 case OMP_MEMORY_ORDER_UNSPECIFIED:
35020 case OMP_MEMORY_ORDER_RELAXED:
35021 memory_order = OMP_MEMORY_ORDER_RELAXED;
35022 break;
35023 case OMP_MEMORY_ORDER_SEQ_CST:
35024 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35025 break;
35026 case OMP_MEMORY_ORDER_ACQ_REL:
35027 switch (code)
35029 case OMP_ATOMIC_READ:
35030 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35031 break;
35032 case NOP_EXPR: /* atomic write */
35033 case OMP_ATOMIC:
35034 memory_order = OMP_MEMORY_ORDER_RELEASE;
35035 break;
35036 default:
35037 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35038 break;
35040 break;
35041 default:
35042 gcc_unreachable ();
35045 else
35046 switch (code)
35048 case OMP_ATOMIC_READ:
35049 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35050 || memory_order == OMP_MEMORY_ORDER_RELEASE)
35052 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
35053 "%<acq_rel%> or %<release%> clauses");
35054 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35056 break;
35057 case NOP_EXPR: /* atomic write */
35058 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35059 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35061 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
35062 "%<acq_rel%> or %<acquire%> clauses");
35063 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35065 break;
35066 case OMP_ATOMIC:
35067 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35068 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35070 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
35071 "%<acq_rel%> or %<acquire%> clauses");
35072 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35074 break;
35075 default:
35076 break;
35079 switch (code)
35081 case OMP_ATOMIC_READ:
35082 case NOP_EXPR: /* atomic write */
35083 v = cp_parser_unary_expression (parser);
35084 if (v == error_mark_node)
35085 goto saw_error;
35086 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35087 goto saw_error;
35088 if (code == NOP_EXPR)
35089 lhs = cp_parser_expression (parser);
35090 else
35091 lhs = cp_parser_unary_expression (parser);
35092 if (lhs == error_mark_node)
35093 goto saw_error;
35094 if (code == NOP_EXPR)
35096 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35097 opcode. */
35098 code = OMP_ATOMIC;
35099 rhs = lhs;
35100 lhs = v;
35101 v = NULL_TREE;
35103 goto done;
35104 case OMP_ATOMIC_CAPTURE_NEW:
35105 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35107 cp_lexer_consume_token (parser->lexer);
35108 structured_block = true;
35110 else
35112 v = cp_parser_unary_expression (parser);
35113 if (v == error_mark_node)
35114 goto saw_error;
35115 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35116 goto saw_error;
35118 default:
35119 break;
35122 restart:
35123 lhs = cp_parser_unary_expression (parser);
35124 orig_lhs = lhs;
35125 switch (TREE_CODE (lhs))
35127 case ERROR_MARK:
35128 goto saw_error;
35130 case POSTINCREMENT_EXPR:
35131 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35132 code = OMP_ATOMIC_CAPTURE_OLD;
35133 /* FALLTHROUGH */
35134 case PREINCREMENT_EXPR:
35135 lhs = TREE_OPERAND (lhs, 0);
35136 opcode = PLUS_EXPR;
35137 rhs = integer_one_node;
35138 break;
35140 case POSTDECREMENT_EXPR:
35141 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35142 code = OMP_ATOMIC_CAPTURE_OLD;
35143 /* FALLTHROUGH */
35144 case PREDECREMENT_EXPR:
35145 lhs = TREE_OPERAND (lhs, 0);
35146 opcode = MINUS_EXPR;
35147 rhs = integer_one_node;
35148 break;
35150 case COMPOUND_EXPR:
35151 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
35152 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
35153 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
35154 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
35155 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35156 (TREE_OPERAND (lhs, 1), 0), 0)))
35157 == BOOLEAN_TYPE)
35158 /* Undo effects of boolean_increment for post {in,de}crement. */
35159 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
35160 /* FALLTHRU */
35161 case MODIFY_EXPR:
35162 if (TREE_CODE (lhs) == MODIFY_EXPR
35163 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
35165 /* Undo effects of boolean_increment. */
35166 if (integer_onep (TREE_OPERAND (lhs, 1)))
35168 /* This is pre or post increment. */
35169 rhs = TREE_OPERAND (lhs, 1);
35170 lhs = TREE_OPERAND (lhs, 0);
35171 opcode = NOP_EXPR;
35172 if (code == OMP_ATOMIC_CAPTURE_NEW
35173 && !structured_block
35174 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
35175 code = OMP_ATOMIC_CAPTURE_OLD;
35176 break;
35179 /* FALLTHRU */
35180 default:
35181 switch (cp_lexer_peek_token (parser->lexer)->type)
35183 case CPP_MULT_EQ:
35184 opcode = MULT_EXPR;
35185 break;
35186 case CPP_DIV_EQ:
35187 opcode = TRUNC_DIV_EXPR;
35188 break;
35189 case CPP_PLUS_EQ:
35190 opcode = PLUS_EXPR;
35191 break;
35192 case CPP_MINUS_EQ:
35193 opcode = MINUS_EXPR;
35194 break;
35195 case CPP_LSHIFT_EQ:
35196 opcode = LSHIFT_EXPR;
35197 break;
35198 case CPP_RSHIFT_EQ:
35199 opcode = RSHIFT_EXPR;
35200 break;
35201 case CPP_AND_EQ:
35202 opcode = BIT_AND_EXPR;
35203 break;
35204 case CPP_OR_EQ:
35205 opcode = BIT_IOR_EXPR;
35206 break;
35207 case CPP_XOR_EQ:
35208 opcode = BIT_XOR_EXPR;
35209 break;
35210 case CPP_EQ:
35211 enum cp_parser_prec oprec;
35212 cp_token *token;
35213 cp_lexer_consume_token (parser->lexer);
35214 cp_parser_parse_tentatively (parser);
35215 rhs1 = cp_parser_simple_cast_expression (parser);
35216 if (rhs1 == error_mark_node)
35218 cp_parser_abort_tentative_parse (parser);
35219 cp_parser_simple_cast_expression (parser);
35220 goto saw_error;
35222 token = cp_lexer_peek_token (parser->lexer);
35223 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
35225 cp_parser_abort_tentative_parse (parser);
35226 cp_parser_parse_tentatively (parser);
35227 rhs = cp_parser_binary_expression (parser, false, true,
35228 PREC_NOT_OPERATOR, NULL);
35229 if (rhs == error_mark_node)
35231 cp_parser_abort_tentative_parse (parser);
35232 cp_parser_binary_expression (parser, false, true,
35233 PREC_NOT_OPERATOR, NULL);
35234 goto saw_error;
35236 switch (TREE_CODE (rhs))
35238 case MULT_EXPR:
35239 case TRUNC_DIV_EXPR:
35240 case RDIV_EXPR:
35241 case PLUS_EXPR:
35242 case MINUS_EXPR:
35243 case LSHIFT_EXPR:
35244 case RSHIFT_EXPR:
35245 case BIT_AND_EXPR:
35246 case BIT_IOR_EXPR:
35247 case BIT_XOR_EXPR:
35248 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
35250 if (cp_parser_parse_definitely (parser))
35252 opcode = TREE_CODE (rhs);
35253 rhs1 = TREE_OPERAND (rhs, 0);
35254 rhs = TREE_OPERAND (rhs, 1);
35255 goto stmt_done;
35257 else
35258 goto saw_error;
35260 break;
35261 default:
35262 break;
35264 cp_parser_abort_tentative_parse (parser);
35265 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
35267 rhs = cp_parser_expression (parser);
35268 if (rhs == error_mark_node)
35269 goto saw_error;
35270 opcode = NOP_EXPR;
35271 rhs1 = NULL_TREE;
35272 goto stmt_done;
35274 cp_parser_error (parser,
35275 "invalid form of %<#pragma omp atomic%>");
35276 goto saw_error;
35278 if (!cp_parser_parse_definitely (parser))
35279 goto saw_error;
35280 switch (token->type)
35282 case CPP_SEMICOLON:
35283 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35285 code = OMP_ATOMIC_CAPTURE_OLD;
35286 v = lhs;
35287 lhs = NULL_TREE;
35288 lhs1 = rhs1;
35289 rhs1 = NULL_TREE;
35290 cp_lexer_consume_token (parser->lexer);
35291 goto restart;
35293 else if (structured_block)
35295 opcode = NOP_EXPR;
35296 rhs = rhs1;
35297 rhs1 = NULL_TREE;
35298 goto stmt_done;
35300 cp_parser_error (parser,
35301 "invalid form of %<#pragma omp atomic%>");
35302 goto saw_error;
35303 case CPP_MULT:
35304 opcode = MULT_EXPR;
35305 break;
35306 case CPP_DIV:
35307 opcode = TRUNC_DIV_EXPR;
35308 break;
35309 case CPP_PLUS:
35310 opcode = PLUS_EXPR;
35311 break;
35312 case CPP_MINUS:
35313 opcode = MINUS_EXPR;
35314 break;
35315 case CPP_LSHIFT:
35316 opcode = LSHIFT_EXPR;
35317 break;
35318 case CPP_RSHIFT:
35319 opcode = RSHIFT_EXPR;
35320 break;
35321 case CPP_AND:
35322 opcode = BIT_AND_EXPR;
35323 break;
35324 case CPP_OR:
35325 opcode = BIT_IOR_EXPR;
35326 break;
35327 case CPP_XOR:
35328 opcode = BIT_XOR_EXPR;
35329 break;
35330 default:
35331 cp_parser_error (parser,
35332 "invalid operator for %<#pragma omp atomic%>");
35333 goto saw_error;
35335 oprec = TOKEN_PRECEDENCE (token);
35336 gcc_assert (oprec != PREC_NOT_OPERATOR);
35337 if (commutative_tree_code (opcode))
35338 oprec = (enum cp_parser_prec) (oprec - 1);
35339 cp_lexer_consume_token (parser->lexer);
35340 rhs = cp_parser_binary_expression (parser, false, false,
35341 oprec, NULL);
35342 if (rhs == error_mark_node)
35343 goto saw_error;
35344 goto stmt_done;
35345 /* FALLTHROUGH */
35346 default:
35347 cp_parser_error (parser,
35348 "invalid operator for %<#pragma omp atomic%>");
35349 goto saw_error;
35351 cp_lexer_consume_token (parser->lexer);
35353 rhs = cp_parser_expression (parser);
35354 if (rhs == error_mark_node)
35355 goto saw_error;
35356 break;
35358 stmt_done:
35359 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35361 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
35362 goto saw_error;
35363 v = cp_parser_unary_expression (parser);
35364 if (v == error_mark_node)
35365 goto saw_error;
35366 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35367 goto saw_error;
35368 lhs1 = cp_parser_unary_expression (parser);
35369 if (lhs1 == error_mark_node)
35370 goto saw_error;
35372 if (structured_block)
35374 cp_parser_consume_semicolon_at_end_of_statement (parser);
35375 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35377 done:
35378 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
35379 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
35380 rhs1, clauses, memory_order);
35381 if (!structured_block)
35382 cp_parser_consume_semicolon_at_end_of_statement (parser);
35383 return;
35385 saw_error:
35386 cp_parser_skip_to_end_of_block_or_statement (parser);
35387 if (structured_block)
35389 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35390 cp_lexer_consume_token (parser->lexer);
35391 else if (code == OMP_ATOMIC_CAPTURE_NEW)
35393 cp_parser_skip_to_end_of_block_or_statement (parser);
35394 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35395 cp_lexer_consume_token (parser->lexer);
35401 /* OpenMP 2.5:
35402 # pragma omp barrier new-line */
35404 static void
35405 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
35407 cp_parser_require_pragma_eol (parser, pragma_tok);
35408 finish_omp_barrier ();
35411 /* OpenMP 2.5:
35412 # pragma omp critical [(name)] new-line
35413 structured-block
35415 OpenMP 4.5:
35416 # pragma omp critical [(name) [hint(expression)]] new-line
35417 structured-block */
35419 #define OMP_CRITICAL_CLAUSE_MASK \
35420 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35422 static tree
35423 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35425 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
35427 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35429 matching_parens parens;
35430 parens.consume_open (parser);
35432 name = cp_parser_identifier (parser);
35434 if (name == error_mark_node
35435 || !parens.require_close (parser))
35436 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35437 /*or_comma=*/false,
35438 /*consume_paren=*/true);
35439 if (name == error_mark_node)
35440 name = NULL;
35442 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
35443 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
35444 cp_lexer_consume_token (parser->lexer);
35446 clauses = cp_parser_omp_all_clauses (parser,
35447 OMP_CRITICAL_CLAUSE_MASK,
35448 "#pragma omp critical", pragma_tok);
35450 else
35451 cp_parser_require_pragma_eol (parser, pragma_tok);
35453 stmt = cp_parser_omp_structured_block (parser, if_p);
35454 return c_finish_omp_critical (input_location, stmt, name, clauses);
35457 /* OpenMP 5.0:
35458 # pragma omp depobj ( depobj ) depobj-clause new-line
35460 depobj-clause:
35461 depend (dependence-type : locator)
35462 destroy
35463 update (dependence-type)
35465 dependence-type:
35468 inout
35469 mutexinout */
35471 static void
35472 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
35474 location_t loc = pragma_tok->location;
35475 matching_parens parens;
35476 if (!parens.require_open (parser))
35478 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35479 return;
35482 tree depobj = cp_parser_assignment_expression (parser);
35484 if (!parens.require_close (parser))
35485 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35486 /*or_comma=*/false,
35487 /*consume_paren=*/true);
35489 tree clause = NULL_TREE;
35490 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
35491 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
35492 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35494 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35495 const char *p = IDENTIFIER_POINTER (id);
35497 cp_lexer_consume_token (parser->lexer);
35498 if (!strcmp ("depend", p))
35500 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
35501 if (clause)
35502 clause = finish_omp_clauses (clause, C_ORT_OMP);
35503 if (!clause)
35504 clause = error_mark_node;
35506 else if (!strcmp ("destroy", p))
35507 kind = OMP_CLAUSE_DEPEND_LAST;
35508 else if (!strcmp ("update", p))
35510 matching_parens c_parens;
35511 if (c_parens.require_open (parser))
35513 location_t c2_loc
35514 = cp_lexer_peek_token (parser->lexer)->location;
35515 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35517 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
35518 const char *p2 = IDENTIFIER_POINTER (id2);
35520 cp_lexer_consume_token (parser->lexer);
35521 if (!strcmp ("in", p2))
35522 kind = OMP_CLAUSE_DEPEND_IN;
35523 else if (!strcmp ("out", p2))
35524 kind = OMP_CLAUSE_DEPEND_OUT;
35525 else if (!strcmp ("inout", p2))
35526 kind = OMP_CLAUSE_DEPEND_INOUT;
35527 else if (!strcmp ("mutexinoutset", p2))
35528 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
35530 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
35532 clause = error_mark_node;
35533 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
35534 "%<mutexinoutset%>");
35536 if (!c_parens.require_close (parser))
35537 cp_parser_skip_to_closing_parenthesis (parser,
35538 /*recovering=*/true,
35539 /*or_comma=*/false,
35540 /*consume_paren=*/true);
35542 else
35543 clause = error_mark_node;
35546 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
35548 clause = error_mark_node;
35549 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
35551 cp_parser_require_pragma_eol (parser, pragma_tok);
35553 finish_omp_depobj (loc, depobj, kind, clause);
35557 /* OpenMP 2.5:
35558 # pragma omp flush flush-vars[opt] new-line
35560 flush-vars:
35561 ( variable-list )
35563 OpenMP 5.0:
35564 # pragma omp flush memory-order-clause new-line */
35566 static void
35567 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
35569 enum memmodel mo = MEMMODEL_LAST;
35570 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35572 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35573 const char *p = IDENTIFIER_POINTER (id);
35574 if (!strcmp (p, "acq_rel"))
35575 mo = MEMMODEL_ACQ_REL;
35576 else if (!strcmp (p, "release"))
35577 mo = MEMMODEL_RELEASE;
35578 else if (!strcmp (p, "acquire"))
35579 mo = MEMMODEL_ACQUIRE;
35580 else
35581 error_at (cp_lexer_peek_token (parser->lexer)->location,
35582 "expected %<acq_rel%>, %<release%> or %<acquire%>");
35583 cp_lexer_consume_token (parser->lexer);
35585 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35587 if (mo != MEMMODEL_LAST)
35588 error_at (cp_lexer_peek_token (parser->lexer)->location,
35589 "%<flush%> list specified together with memory order "
35590 "clause");
35591 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35593 cp_parser_require_pragma_eol (parser, pragma_tok);
35595 finish_omp_flush (mo);
35598 /* Helper function, to parse omp for increment expression. */
35600 static tree
35601 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
35603 tree cond = cp_parser_binary_expression (parser, false, true,
35604 PREC_NOT_OPERATOR, NULL);
35605 if (cond == error_mark_node
35606 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35608 cp_parser_skip_to_end_of_statement (parser);
35609 return error_mark_node;
35612 switch (TREE_CODE (cond))
35614 case GT_EXPR:
35615 case GE_EXPR:
35616 case LT_EXPR:
35617 case LE_EXPR:
35618 break;
35619 case NE_EXPR:
35620 if (code != OACC_LOOP)
35621 break;
35622 gcc_fallthrough ();
35623 default:
35624 return error_mark_node;
35627 /* If decl is an iterator, preserve LHS and RHS of the relational
35628 expr until finish_omp_for. */
35629 if (decl
35630 && (type_dependent_expression_p (decl)
35631 || CLASS_TYPE_P (TREE_TYPE (decl))))
35632 return cond;
35634 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
35635 TREE_CODE (cond),
35636 TREE_OPERAND (cond, 0), ERROR_MARK,
35637 TREE_OPERAND (cond, 1), ERROR_MARK,
35638 /*overload=*/NULL, tf_warning_or_error);
35641 /* Helper function, to parse omp for increment expression. */
35643 static tree
35644 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
35646 cp_token *token = cp_lexer_peek_token (parser->lexer);
35647 enum tree_code op;
35648 tree lhs, rhs;
35649 cp_id_kind idk;
35650 bool decl_first;
35652 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
35654 op = (token->type == CPP_PLUS_PLUS
35655 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
35656 cp_lexer_consume_token (parser->lexer);
35657 lhs = cp_parser_simple_cast_expression (parser);
35658 if (lhs != decl
35659 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
35660 return error_mark_node;
35661 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
35664 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
35665 if (lhs != decl
35666 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
35667 return error_mark_node;
35669 token = cp_lexer_peek_token (parser->lexer);
35670 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
35672 op = (token->type == CPP_PLUS_PLUS
35673 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
35674 cp_lexer_consume_token (parser->lexer);
35675 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
35678 op = cp_parser_assignment_operator_opt (parser);
35679 if (op == ERROR_MARK)
35680 return error_mark_node;
35682 if (op != NOP_EXPR)
35684 rhs = cp_parser_assignment_expression (parser);
35685 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
35686 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35689 lhs = cp_parser_binary_expression (parser, false, false,
35690 PREC_ADDITIVE_EXPRESSION, NULL);
35691 token = cp_lexer_peek_token (parser->lexer);
35692 decl_first = (lhs == decl
35693 || (processing_template_decl && cp_tree_equal (lhs, decl)));
35694 if (decl_first)
35695 lhs = NULL_TREE;
35696 if (token->type != CPP_PLUS
35697 && token->type != CPP_MINUS)
35698 return error_mark_node;
35702 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
35703 cp_lexer_consume_token (parser->lexer);
35704 rhs = cp_parser_binary_expression (parser, false, false,
35705 PREC_ADDITIVE_EXPRESSION, NULL);
35706 token = cp_lexer_peek_token (parser->lexer);
35707 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
35709 if (lhs == NULL_TREE)
35711 if (op == PLUS_EXPR)
35712 lhs = rhs;
35713 else
35714 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
35715 tf_warning_or_error);
35717 else
35718 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
35719 ERROR_MARK, NULL, tf_warning_or_error);
35722 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
35724 if (!decl_first)
35726 if ((rhs != decl
35727 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
35728 || op == MINUS_EXPR)
35729 return error_mark_node;
35730 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
35732 else
35733 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
35735 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35738 /* Parse the initialization statement of an OpenMP for loop.
35740 Return true if the resulting construct should have an
35741 OMP_CLAUSE_PRIVATE added to it. */
35743 static tree
35744 cp_parser_omp_for_loop_init (cp_parser *parser,
35745 tree &this_pre_body,
35746 vec<tree, va_gc> *&for_block,
35747 tree &init,
35748 tree &orig_init,
35749 tree &decl,
35750 tree &real_decl)
35752 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35753 return NULL_TREE;
35755 tree add_private_clause = NULL_TREE;
35757 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
35759 init-expr:
35760 var = lb
35761 integer-type var = lb
35762 random-access-iterator-type var = lb
35763 pointer-type var = lb
35765 cp_decl_specifier_seq type_specifiers;
35767 /* First, try to parse as an initialized declaration. See
35768 cp_parser_condition, from whence the bulk of this is copied. */
35770 cp_parser_parse_tentatively (parser);
35771 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
35772 /*is_trailing_return=*/false,
35773 &type_specifiers);
35774 if (cp_parser_parse_definitely (parser))
35776 /* If parsing a type specifier seq succeeded, then this
35777 MUST be a initialized declaration. */
35778 tree asm_specification, attributes;
35779 cp_declarator *declarator;
35781 declarator = cp_parser_declarator (parser,
35782 CP_PARSER_DECLARATOR_NAMED,
35783 /*ctor_dtor_or_conv_p=*/NULL,
35784 /*parenthesized_p=*/NULL,
35785 /*member_p=*/false,
35786 /*friend_p=*/false);
35787 attributes = cp_parser_attributes_opt (parser);
35788 asm_specification = cp_parser_asm_specification_opt (parser);
35790 if (declarator == cp_error_declarator)
35791 cp_parser_skip_to_end_of_statement (parser);
35793 else
35795 tree pushed_scope, auto_node;
35797 decl = start_decl (declarator, &type_specifiers,
35798 SD_INITIALIZED, attributes,
35799 /*prefix_attributes=*/NULL_TREE,
35800 &pushed_scope);
35802 auto_node = type_uses_auto (TREE_TYPE (decl));
35803 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
35805 if (cp_lexer_next_token_is (parser->lexer,
35806 CPP_OPEN_PAREN))
35807 error ("parenthesized initialization is not allowed in "
35808 "OpenMP %<for%> loop");
35809 else
35810 /* Trigger an error. */
35811 cp_parser_require (parser, CPP_EQ, RT_EQ);
35813 init = error_mark_node;
35814 cp_parser_skip_to_end_of_statement (parser);
35816 else if (CLASS_TYPE_P (TREE_TYPE (decl))
35817 || type_dependent_expression_p (decl)
35818 || auto_node)
35820 bool is_direct_init, is_non_constant_init;
35822 init = cp_parser_initializer (parser,
35823 &is_direct_init,
35824 &is_non_constant_init);
35826 if (auto_node)
35828 TREE_TYPE (decl)
35829 = do_auto_deduction (TREE_TYPE (decl), init,
35830 auto_node);
35832 if (!CLASS_TYPE_P (TREE_TYPE (decl))
35833 && !type_dependent_expression_p (decl))
35834 goto non_class;
35837 cp_finish_decl (decl, init, !is_non_constant_init,
35838 asm_specification,
35839 LOOKUP_ONLYCONVERTING);
35840 orig_init = init;
35841 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35843 vec_safe_push (for_block, this_pre_body);
35844 init = NULL_TREE;
35846 else
35848 init = pop_stmt_list (this_pre_body);
35849 if (init && TREE_CODE (init) == STATEMENT_LIST)
35851 tree_stmt_iterator i = tsi_start (init);
35852 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35853 while (!tsi_end_p (i))
35855 tree t = tsi_stmt (i);
35856 if (TREE_CODE (t) == DECL_EXPR
35857 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35859 tsi_delink (&i);
35860 vec_safe_push (for_block, t);
35861 continue;
35863 break;
35865 if (tsi_one_before_end_p (i))
35867 tree t = tsi_stmt (i);
35868 tsi_delink (&i);
35869 free_stmt_list (init);
35870 init = t;
35874 this_pre_body = NULL_TREE;
35876 else
35878 /* Consume '='. */
35879 cp_lexer_consume_token (parser->lexer);
35880 init = cp_parser_assignment_expression (parser);
35882 non_class:
35883 if (TYPE_REF_P (TREE_TYPE (decl)))
35884 init = error_mark_node;
35885 else
35886 cp_finish_decl (decl, NULL_TREE,
35887 /*init_const_expr_p=*/false,
35888 asm_specification,
35889 LOOKUP_ONLYCONVERTING);
35892 if (pushed_scope)
35893 pop_scope (pushed_scope);
35896 else
35898 cp_id_kind idk;
35899 /* If parsing a type specifier sequence failed, then
35900 this MUST be a simple expression. */
35901 cp_parser_parse_tentatively (parser);
35902 decl = cp_parser_primary_expression (parser, false, false,
35903 false, &idk);
35904 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35905 if (!cp_parser_error_occurred (parser)
35906 && decl
35907 && (TREE_CODE (decl) == COMPONENT_REF
35908 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35910 cp_parser_abort_tentative_parse (parser);
35911 cp_parser_parse_tentatively (parser);
35912 cp_token *token = cp_lexer_peek_token (parser->lexer);
35913 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35914 /*check_dependency_p=*/true,
35915 /*template_p=*/NULL,
35916 /*declarator_p=*/false,
35917 /*optional_p=*/false);
35918 if (name != error_mark_node
35919 && last_tok == cp_lexer_peek_token (parser->lexer))
35921 decl = cp_parser_lookup_name_simple (parser, name,
35922 token->location);
35923 if (TREE_CODE (decl) == FIELD_DECL)
35924 add_private_clause = omp_privatize_field (decl, false);
35926 cp_parser_abort_tentative_parse (parser);
35927 cp_parser_parse_tentatively (parser);
35928 decl = cp_parser_primary_expression (parser, false, false,
35929 false, &idk);
35931 if (!cp_parser_error_occurred (parser)
35932 && decl
35933 && DECL_P (decl)
35934 && CLASS_TYPE_P (TREE_TYPE (decl)))
35936 tree rhs;
35938 cp_parser_parse_definitely (parser);
35939 cp_parser_require (parser, CPP_EQ, RT_EQ);
35940 rhs = cp_parser_assignment_expression (parser);
35941 orig_init = rhs;
35942 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35943 decl, NOP_EXPR,
35944 rhs,
35945 tf_warning_or_error));
35946 if (!add_private_clause)
35947 add_private_clause = decl;
35949 else
35951 decl = NULL;
35952 cp_parser_abort_tentative_parse (parser);
35953 init = cp_parser_expression (parser);
35954 if (init)
35956 if (TREE_CODE (init) == MODIFY_EXPR
35957 || TREE_CODE (init) == MODOP_EXPR)
35958 real_decl = TREE_OPERAND (init, 0);
35962 return add_private_clause;
35965 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
35967 void
35968 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
35969 tree &decl, tree &orig_decl, tree &init,
35970 tree &orig_init, tree &cond, tree &incr)
35972 tree begin, end, range_temp_decl = NULL_TREE;
35973 tree iter_type, begin_expr, end_expr;
35975 if (processing_template_decl)
35977 if (check_for_bare_parameter_packs (init))
35978 init = error_mark_node;
35979 if (!type_dependent_expression_p (init)
35980 /* do_auto_deduction doesn't mess with template init-lists. */
35981 && !BRACE_ENCLOSED_INITIALIZER_P (init))
35983 tree d = decl;
35984 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
35986 tree v = DECL_VALUE_EXPR (decl);
35987 if (TREE_CODE (v) == ARRAY_REF
35988 && VAR_P (TREE_OPERAND (v, 0))
35989 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
35990 d = TREE_OPERAND (v, 0);
35992 do_range_for_auto_deduction (d, init);
35994 cond = global_namespace;
35995 incr = NULL_TREE;
35996 orig_init = init;
35997 if (this_pre_body)
35998 this_pre_body = pop_stmt_list (this_pre_body);
35999 return;
36002 init = mark_lvalue_use (init);
36004 if (decl == error_mark_node || init == error_mark_node)
36005 /* If an error happened previously do nothing or else a lot of
36006 unhelpful errors would be issued. */
36007 begin_expr = end_expr = iter_type = error_mark_node;
36008 else
36010 tree range_temp;
36012 if (VAR_P (init)
36013 && array_of_runtime_bound_p (TREE_TYPE (init)))
36014 /* Can't bind a reference to an array of runtime bound. */
36015 range_temp = init;
36016 else
36018 range_temp = build_range_temp (init);
36019 DECL_NAME (range_temp) = NULL_TREE;
36020 pushdecl (range_temp);
36021 cp_finish_decl (range_temp, init,
36022 /*is_constant_init*/false, NULL_TREE,
36023 LOOKUP_ONLYCONVERTING);
36024 range_temp_decl = range_temp;
36025 range_temp = convert_from_reference (range_temp);
36027 iter_type = cp_parser_perform_range_for_lookup (range_temp,
36028 &begin_expr, &end_expr);
36031 tree end_iter_type = iter_type;
36032 if (cxx_dialect >= cxx17)
36033 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36034 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
36035 TREE_USED (end) = 1;
36036 DECL_ARTIFICIAL (end) = 1;
36037 pushdecl (end);
36038 cp_finish_decl (end, end_expr,
36039 /*is_constant_init*/false, NULL_TREE,
36040 LOOKUP_ONLYCONVERTING);
36042 /* The new for initialization statement. */
36043 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
36044 TREE_USED (begin) = 1;
36045 DECL_ARTIFICIAL (begin) = 1;
36046 pushdecl (begin);
36047 orig_init = init;
36048 if (CLASS_TYPE_P (iter_type))
36049 init = NULL_TREE;
36050 else
36052 init = begin_expr;
36053 begin_expr = NULL_TREE;
36055 cp_finish_decl (begin, begin_expr,
36056 /*is_constant_init*/false, NULL_TREE,
36057 LOOKUP_ONLYCONVERTING);
36059 /* The new for condition. */
36060 if (CLASS_TYPE_P (iter_type))
36061 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
36062 else
36063 cond = build_x_binary_op (input_location, NE_EXPR,
36064 begin, ERROR_MARK,
36065 end, ERROR_MARK,
36066 NULL, tf_warning_or_error);
36068 /* The new increment expression. */
36069 if (CLASS_TYPE_P (iter_type))
36070 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
36071 else
36072 incr = finish_unary_op_expr (input_location,
36073 PREINCREMENT_EXPR, begin,
36074 tf_warning_or_error);
36076 orig_decl = decl;
36077 decl = begin;
36078 if (for_block)
36080 vec_safe_push (for_block, this_pre_body);
36081 this_pre_body = NULL_TREE;
36084 tree decomp_first_name = NULL_TREE;
36085 unsigned decomp_cnt = 0;
36086 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
36088 tree v = DECL_VALUE_EXPR (orig_decl);
36089 if (TREE_CODE (v) == ARRAY_REF
36090 && VAR_P (TREE_OPERAND (v, 0))
36091 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36093 tree d = orig_decl;
36094 orig_decl = TREE_OPERAND (v, 0);
36095 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
36096 decomp_first_name = d;
36100 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
36101 if (auto_node)
36103 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36104 tf_none);
36105 if (!error_operand_p (t))
36106 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
36107 t, auto_node);
36110 tree v = make_tree_vec (decomp_cnt + 3);
36111 TREE_VEC_ELT (v, 0) = range_temp_decl;
36112 TREE_VEC_ELT (v, 1) = end;
36113 TREE_VEC_ELT (v, 2) = orig_decl;
36114 for (unsigned i = 0; i < decomp_cnt; i++)
36116 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
36117 decomp_first_name = DECL_CHAIN (decomp_first_name);
36119 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
36122 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36123 inside of the collapsed body. */
36125 void
36126 cp_finish_omp_range_for (tree orig, tree begin)
36128 gcc_assert (TREE_CODE (orig) == TREE_LIST
36129 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
36130 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
36131 tree decomp_first_name = NULL_TREE;
36132 unsigned int decomp_cnt = 0;
36134 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36136 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
36137 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
36138 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
36141 /* The declaration is initialized with *__begin inside the loop body. */
36142 cp_finish_decl (decl,
36143 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36144 tf_warning_or_error),
36145 /*is_constant_init*/false, NULL_TREE,
36146 LOOKUP_ONLYCONVERTING);
36147 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36148 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
36151 /* Parse the restricted form of the for statement allowed by OpenMP. */
36153 static tree
36154 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
36155 tree *cclauses, bool *if_p)
36157 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
36158 tree orig_decl;
36159 tree real_decl, initv, condv, incrv, declv, orig_declv;
36160 tree this_pre_body, cl, ordered_cl = NULL_TREE;
36161 location_t loc_first;
36162 bool collapse_err = false;
36163 int i, collapse = 1, ordered = 0, count, nbraces = 0;
36164 vec<tree, va_gc> *for_block = make_tree_vector ();
36165 auto_vec<tree, 4> orig_inits;
36166 bool tiling = false;
36168 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
36169 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
36170 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
36171 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
36173 tiling = true;
36174 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
36176 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
36177 && OMP_CLAUSE_ORDERED_EXPR (cl))
36179 ordered_cl = cl;
36180 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
36183 if (ordered && ordered < collapse)
36185 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36186 "%<ordered%> clause parameter is less than %<collapse%>");
36187 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
36188 = build_int_cst (NULL_TREE, collapse);
36189 ordered = collapse;
36191 if (ordered)
36193 for (tree *pc = &clauses; *pc; )
36194 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
36196 error_at (OMP_CLAUSE_LOCATION (*pc),
36197 "%<linear%> clause may not be specified together "
36198 "with %<ordered%> clause with a parameter");
36199 *pc = OMP_CLAUSE_CHAIN (*pc);
36201 else
36202 pc = &OMP_CLAUSE_CHAIN (*pc);
36205 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
36206 count = ordered ? ordered : collapse;
36208 declv = make_tree_vec (count);
36209 initv = make_tree_vec (count);
36210 condv = make_tree_vec (count);
36211 incrv = make_tree_vec (count);
36212 orig_declv = NULL_TREE;
36214 loc_first = cp_lexer_peek_token (parser->lexer)->location;
36216 for (i = 0; i < count; i++)
36218 int bracecount = 0;
36219 tree add_private_clause = NULL_TREE;
36220 location_t loc;
36222 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36224 if (!collapse_err)
36225 cp_parser_error (parser, "for statement expected");
36226 return NULL;
36228 loc = cp_lexer_consume_token (parser->lexer)->location;
36230 matching_parens parens;
36231 if (!parens.require_open (parser))
36232 return NULL;
36234 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
36235 this_pre_body = push_stmt_list ();
36237 if (code != OACC_LOOP && cxx_dialect >= cxx11)
36239 /* Save tokens so that we can put them back. */
36240 cp_lexer_save_tokens (parser->lexer);
36242 /* Look for ':' that is not nested in () or {}. */
36243 bool is_range_for
36244 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
36245 /*recovering=*/false,
36246 CPP_COLON,
36247 /*consume_paren=*/
36248 false) == -1);
36250 /* Roll back the tokens we skipped. */
36251 cp_lexer_rollback_tokens (parser->lexer);
36253 if (is_range_for)
36255 bool saved_colon_corrects_to_scope_p
36256 = parser->colon_corrects_to_scope_p;
36258 /* A colon is used in range-based for. */
36259 parser->colon_corrects_to_scope_p = false;
36261 /* Parse the declaration. */
36262 cp_parser_simple_declaration (parser,
36263 /*function_definition_allowed_p=*/
36264 false, &decl);
36265 parser->colon_corrects_to_scope_p
36266 = saved_colon_corrects_to_scope_p;
36268 cp_parser_require (parser, CPP_COLON, RT_COLON);
36270 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
36271 false, 0, true);
36273 cp_convert_omp_range_for (this_pre_body, for_block, decl,
36274 orig_decl, init, orig_init,
36275 cond, incr);
36276 if (this_pre_body)
36278 if (pre_body)
36280 tree t = pre_body;
36281 pre_body = push_stmt_list ();
36282 add_stmt (t);
36283 add_stmt (this_pre_body);
36284 pre_body = pop_stmt_list (pre_body);
36286 else
36287 pre_body = this_pre_body;
36290 if (ordered_cl)
36291 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36292 "%<ordered%> clause with parameter on "
36293 "range-based %<for%> loop");
36295 goto parse_close_paren;
36299 add_private_clause
36300 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
36301 init, orig_init, decl, real_decl);
36303 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36304 if (this_pre_body)
36306 this_pre_body = pop_stmt_list (this_pre_body);
36307 if (pre_body)
36309 tree t = pre_body;
36310 pre_body = push_stmt_list ();
36311 add_stmt (t);
36312 add_stmt (this_pre_body);
36313 pre_body = pop_stmt_list (pre_body);
36315 else
36316 pre_body = this_pre_body;
36319 if (decl)
36320 real_decl = decl;
36321 if (cclauses != NULL
36322 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
36323 && real_decl != NULL_TREE)
36325 tree *c;
36326 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
36327 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
36328 && OMP_CLAUSE_DECL (*c) == real_decl)
36330 error_at (loc, "iteration variable %qD"
36331 " should not be firstprivate", real_decl);
36332 *c = OMP_CLAUSE_CHAIN (*c);
36334 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
36335 && OMP_CLAUSE_DECL (*c) == real_decl)
36337 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36338 tree l = *c;
36339 *c = OMP_CLAUSE_CHAIN (*c);
36340 if (code == OMP_SIMD)
36342 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36343 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
36345 else
36347 OMP_CLAUSE_CHAIN (l) = clauses;
36348 clauses = l;
36350 add_private_clause = NULL_TREE;
36352 else
36354 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
36355 && OMP_CLAUSE_DECL (*c) == real_decl)
36356 add_private_clause = NULL_TREE;
36357 c = &OMP_CLAUSE_CHAIN (*c);
36361 if (add_private_clause)
36363 tree c;
36364 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
36366 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
36367 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
36368 && OMP_CLAUSE_DECL (c) == decl)
36369 break;
36370 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
36371 && OMP_CLAUSE_DECL (c) == decl)
36372 error_at (loc, "iteration variable %qD "
36373 "should not be firstprivate",
36374 decl);
36375 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
36376 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
36377 && OMP_CLAUSE_DECL (c) == decl)
36378 error_at (loc, "iteration variable %qD should not be reduction",
36379 decl);
36381 if (c == NULL)
36383 if (code != OMP_SIMD)
36384 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
36385 else if (collapse == 1)
36386 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
36387 else
36388 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
36389 OMP_CLAUSE_DECL (c) = add_private_clause;
36390 c = finish_omp_clauses (c, C_ORT_OMP);
36391 if (c)
36393 OMP_CLAUSE_CHAIN (c) = clauses;
36394 clauses = c;
36395 /* For linear, signal that we need to fill up
36396 the so far unknown linear step. */
36397 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
36398 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
36403 cond = NULL;
36404 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36405 cond = cp_parser_omp_for_cond (parser, decl, code);
36406 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36408 incr = NULL;
36409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
36411 /* If decl is an iterator, preserve the operator on decl
36412 until finish_omp_for. */
36413 if (real_decl
36414 && ((processing_template_decl
36415 && (TREE_TYPE (real_decl) == NULL_TREE
36416 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
36417 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
36418 incr = cp_parser_omp_for_incr (parser, real_decl);
36419 else
36420 incr = cp_parser_expression (parser);
36421 if (!EXPR_HAS_LOCATION (incr))
36422 protected_set_expr_location (incr, input_location);
36425 parse_close_paren:
36426 if (!parens.require_close (parser))
36427 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36428 /*or_comma=*/false,
36429 /*consume_paren=*/true);
36431 TREE_VEC_ELT (declv, i) = decl;
36432 TREE_VEC_ELT (initv, i) = init;
36433 TREE_VEC_ELT (condv, i) = cond;
36434 TREE_VEC_ELT (incrv, i) = incr;
36435 if (orig_init)
36437 orig_inits.safe_grow_cleared (i + 1);
36438 orig_inits[i] = orig_init;
36440 if (orig_decl)
36442 if (!orig_declv)
36443 orig_declv = copy_node (declv);
36444 TREE_VEC_ELT (orig_declv, i) = orig_decl;
36446 else if (orig_declv)
36447 TREE_VEC_ELT (orig_declv, i) = decl;
36449 if (i == count - 1)
36450 break;
36452 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
36453 in between the collapsed for loops to be still considered perfectly
36454 nested. Hopefully the final version clarifies this.
36455 For now handle (multiple) {'s and empty statements. */
36456 cp_parser_parse_tentatively (parser);
36457 for (;;)
36459 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36460 break;
36461 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36463 cp_lexer_consume_token (parser->lexer);
36464 bracecount++;
36466 else if (bracecount
36467 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36468 cp_lexer_consume_token (parser->lexer);
36469 else
36471 loc = cp_lexer_peek_token (parser->lexer)->location;
36472 error_at (loc, "not enough for loops to collapse");
36473 collapse_err = true;
36474 cp_parser_abort_tentative_parse (parser);
36475 declv = NULL_TREE;
36476 break;
36480 if (declv)
36482 cp_parser_parse_definitely (parser);
36483 nbraces += bracecount;
36487 if (nbraces)
36488 if_p = NULL;
36490 /* Note that we saved the original contents of this flag when we entered
36491 the structured block, and so we don't need to re-save it here. */
36492 parser->in_statement = IN_OMP_FOR;
36494 /* Note that the grammar doesn't call for a structured block here,
36495 though the loop as a whole is a structured block. */
36496 if (orig_declv)
36498 body = begin_omp_structured_block ();
36499 for (i = 0; i < count; i++)
36500 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
36501 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
36502 TREE_VEC_ELT (declv, i));
36504 else
36505 body = push_stmt_list ();
36506 cp_parser_statement (parser, NULL_TREE, false, if_p);
36507 if (orig_declv)
36508 body = finish_omp_structured_block (body);
36509 else
36510 body = pop_stmt_list (body);
36512 if (declv == NULL_TREE)
36513 ret = NULL_TREE;
36514 else
36515 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
36516 incrv, body, pre_body, &orig_inits, clauses);
36518 while (nbraces)
36520 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36522 cp_lexer_consume_token (parser->lexer);
36523 nbraces--;
36525 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36526 cp_lexer_consume_token (parser->lexer);
36527 else
36529 if (!collapse_err)
36531 error_at (cp_lexer_peek_token (parser->lexer)->location,
36532 "collapsed loops not perfectly nested");
36534 collapse_err = true;
36535 cp_parser_statement_seq_opt (parser, NULL);
36536 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
36537 break;
36541 while (!for_block->is_empty ())
36543 tree t = for_block->pop ();
36544 if (TREE_CODE (t) == STATEMENT_LIST)
36545 add_stmt (pop_stmt_list (t));
36546 else
36547 add_stmt (t);
36549 release_tree_vector (for_block);
36551 return ret;
36554 /* Helper function for OpenMP parsing, split clauses and call
36555 finish_omp_clauses on each of the set of clauses afterwards. */
36557 static void
36558 cp_omp_split_clauses (location_t loc, enum tree_code code,
36559 omp_clause_mask mask, tree clauses, tree *cclauses)
36561 int i;
36562 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
36563 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
36564 if (cclauses[i])
36565 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
36568 /* OpenMP 4.0:
36569 #pragma omp simd simd-clause[optseq] new-line
36570 for-loop */
36572 #define OMP_SIMD_CLAUSE_MASK \
36573 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
36574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
36584 static tree
36585 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
36586 char *p_name, omp_clause_mask mask, tree *cclauses,
36587 bool *if_p)
36589 tree clauses, sb, ret;
36590 unsigned int save;
36591 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36593 strcat (p_name, " simd");
36594 mask |= OMP_SIMD_CLAUSE_MASK;
36596 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36597 cclauses == NULL);
36598 if (cclauses)
36600 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
36601 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
36602 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
36603 OMP_CLAUSE_ORDERED);
36604 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
36606 error_at (OMP_CLAUSE_LOCATION (c),
36607 "%<ordered%> clause with parameter may not be specified "
36608 "on %qs construct", p_name);
36609 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
36613 keep_next_level (true);
36614 sb = begin_omp_structured_block ();
36615 save = cp_parser_begin_omp_structured_block (parser);
36617 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
36619 cp_parser_end_omp_structured_block (parser, save);
36620 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
36622 return ret;
36625 /* OpenMP 2.5:
36626 #pragma omp for for-clause[optseq] new-line
36627 for-loop
36629 OpenMP 4.0:
36630 #pragma omp for simd for-simd-clause[optseq] new-line
36631 for-loop */
36633 #define OMP_FOR_CLAUSE_MASK \
36634 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
36640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
36641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36644 static tree
36645 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
36646 char *p_name, omp_clause_mask mask, tree *cclauses,
36647 bool *if_p)
36649 tree clauses, sb, ret;
36650 unsigned int save;
36651 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36653 strcat (p_name, " for");
36654 mask |= OMP_FOR_CLAUSE_MASK;
36655 /* parallel for{, simd} disallows nowait clause, but for
36656 target {teams distribute ,}parallel for{, simd} it should be accepted. */
36657 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
36658 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
36659 /* Composite distribute parallel for{, simd} disallows ordered clause. */
36660 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
36661 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
36663 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36665 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36666 const char *p = IDENTIFIER_POINTER (id);
36668 if (strcmp (p, "simd") == 0)
36670 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36671 if (cclauses == NULL)
36672 cclauses = cclauses_buf;
36674 cp_lexer_consume_token (parser->lexer);
36675 if (!flag_openmp) /* flag_openmp_simd */
36676 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36677 cclauses, if_p);
36678 sb = begin_omp_structured_block ();
36679 save = cp_parser_begin_omp_structured_block (parser);
36680 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36681 cclauses, if_p);
36682 cp_parser_end_omp_structured_block (parser, save);
36683 tree body = finish_omp_structured_block (sb);
36684 if (ret == NULL)
36685 return ret;
36686 ret = make_node (OMP_FOR);
36687 TREE_TYPE (ret) = void_type_node;
36688 OMP_FOR_BODY (ret) = body;
36689 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36690 SET_EXPR_LOCATION (ret, loc);
36691 add_stmt (ret);
36692 return ret;
36695 if (!flag_openmp) /* flag_openmp_simd */
36697 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36698 return NULL_TREE;
36701 /* Composite distribute parallel for disallows linear clause. */
36702 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
36703 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
36705 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36706 cclauses == NULL);
36707 if (cclauses)
36709 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
36710 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36713 keep_next_level (true);
36714 sb = begin_omp_structured_block ();
36715 save = cp_parser_begin_omp_structured_block (parser);
36717 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
36719 cp_parser_end_omp_structured_block (parser, save);
36720 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
36722 return ret;
36725 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
36726 omp_clause_mask, tree *, bool *);
36728 /* OpenMP 2.5:
36729 # pragma omp master new-line
36730 structured-block */
36732 static tree
36733 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
36734 char *p_name, omp_clause_mask mask, tree *cclauses,
36735 bool *if_p)
36737 tree clauses, sb, ret;
36738 unsigned int save;
36739 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36741 strcat (p_name, " master");
36743 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36745 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36746 const char *p = IDENTIFIER_POINTER (id);
36748 if (strcmp (p, "taskloop") == 0)
36750 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36751 if (cclauses == NULL)
36752 cclauses = cclauses_buf;
36754 cp_lexer_consume_token (parser->lexer);
36755 if (!flag_openmp) /* flag_openmp_simd */
36756 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
36757 cclauses, if_p);
36758 sb = begin_omp_structured_block ();
36759 save = cp_parser_begin_omp_structured_block (parser);
36760 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
36761 cclauses, if_p);
36762 cp_parser_end_omp_structured_block (parser, save);
36763 tree body = finish_omp_structured_block (sb);
36764 if (ret == NULL)
36765 return ret;
36766 return c_finish_omp_master (loc, body);
36769 if (!flag_openmp) /* flag_openmp_simd */
36771 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36772 return NULL_TREE;
36775 if (cclauses)
36777 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36778 false);
36779 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
36781 else
36782 cp_parser_require_pragma_eol (parser, pragma_tok);
36784 return c_finish_omp_master (loc,
36785 cp_parser_omp_structured_block (parser, if_p));
36788 /* OpenMP 2.5:
36789 # pragma omp ordered new-line
36790 structured-block
36792 OpenMP 4.5:
36793 # pragma omp ordered ordered-clauses new-line
36794 structured-block */
36796 #define OMP_ORDERED_CLAUSE_MASK \
36797 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
36798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
36800 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
36801 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
36803 static bool
36804 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
36805 enum pragma_context context, bool *if_p)
36807 location_t loc = pragma_tok->location;
36809 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36811 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36812 const char *p = IDENTIFIER_POINTER (id);
36814 if (strcmp (p, "depend") == 0)
36816 if (!flag_openmp) /* flag_openmp_simd */
36818 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36819 return false;
36821 if (context == pragma_stmt)
36823 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
36824 "%<depend%> clause may only be used in compound "
36825 "statements");
36826 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36827 return false;
36829 tree clauses
36830 = cp_parser_omp_all_clauses (parser,
36831 OMP_ORDERED_DEPEND_CLAUSE_MASK,
36832 "#pragma omp ordered", pragma_tok);
36833 c_finish_omp_ordered (loc, clauses, NULL_TREE);
36834 return false;
36838 tree clauses
36839 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
36840 "#pragma omp ordered", pragma_tok);
36842 if (!flag_openmp /* flag_openmp_simd */
36843 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
36844 return false;
36846 c_finish_omp_ordered (loc, clauses,
36847 cp_parser_omp_structured_block (parser, if_p));
36848 return true;
36851 /* OpenMP 2.5:
36853 section-scope:
36854 { section-sequence }
36856 section-sequence:
36857 section-directive[opt] structured-block
36858 section-sequence section-directive structured-block */
36860 static tree
36861 cp_parser_omp_sections_scope (cp_parser *parser)
36863 tree stmt, substmt;
36864 bool error_suppress = false;
36865 cp_token *tok;
36867 matching_braces braces;
36868 if (!braces.require_open (parser))
36869 return NULL_TREE;
36871 stmt = push_stmt_list ();
36873 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
36874 != PRAGMA_OMP_SECTION)
36876 substmt = cp_parser_omp_structured_block (parser, NULL);
36877 substmt = build1 (OMP_SECTION, void_type_node, substmt);
36878 add_stmt (substmt);
36881 while (1)
36883 tok = cp_lexer_peek_token (parser->lexer);
36884 if (tok->type == CPP_CLOSE_BRACE)
36885 break;
36886 if (tok->type == CPP_EOF)
36887 break;
36889 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
36891 cp_lexer_consume_token (parser->lexer);
36892 cp_parser_require_pragma_eol (parser, tok);
36893 error_suppress = false;
36895 else if (!error_suppress)
36897 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
36898 error_suppress = true;
36901 substmt = cp_parser_omp_structured_block (parser, NULL);
36902 substmt = build1 (OMP_SECTION, void_type_node, substmt);
36903 add_stmt (substmt);
36905 braces.require_close (parser);
36907 substmt = pop_stmt_list (stmt);
36909 stmt = make_node (OMP_SECTIONS);
36910 TREE_TYPE (stmt) = void_type_node;
36911 OMP_SECTIONS_BODY (stmt) = substmt;
36913 add_stmt (stmt);
36914 return stmt;
36917 /* OpenMP 2.5:
36918 # pragma omp sections sections-clause[optseq] newline
36919 sections-scope */
36921 #define OMP_SECTIONS_CLAUSE_MASK \
36922 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36928 static tree
36929 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
36930 char *p_name, omp_clause_mask mask, tree *cclauses)
36932 tree clauses, ret;
36933 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36935 strcat (p_name, " sections");
36936 mask |= OMP_SECTIONS_CLAUSE_MASK;
36937 if (cclauses)
36938 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
36940 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36941 cclauses == NULL);
36942 if (cclauses)
36944 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
36945 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
36948 ret = cp_parser_omp_sections_scope (parser);
36949 if (ret)
36950 OMP_SECTIONS_CLAUSES (ret) = clauses;
36952 return ret;
36955 /* OpenMP 2.5:
36956 # pragma omp parallel parallel-clause[optseq] new-line
36957 structured-block
36958 # pragma omp parallel for parallel-for-clause[optseq] new-line
36959 structured-block
36960 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
36961 structured-block
36963 OpenMP 4.0:
36964 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
36965 structured-block */
36967 #define OMP_PARALLEL_CLAUSE_MASK \
36968 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
36974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
36976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
36978 static tree
36979 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
36980 char *p_name, omp_clause_mask mask, tree *cclauses,
36981 bool *if_p)
36983 tree stmt, clauses, block;
36984 unsigned int save;
36985 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36987 strcat (p_name, " parallel");
36988 mask |= OMP_PARALLEL_CLAUSE_MASK;
36989 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
36990 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
36991 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
36992 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
36994 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36996 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36997 if (cclauses == NULL)
36998 cclauses = cclauses_buf;
37000 cp_lexer_consume_token (parser->lexer);
37001 if (!flag_openmp) /* flag_openmp_simd */
37002 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37003 if_p);
37004 block = begin_omp_parallel ();
37005 save = cp_parser_begin_omp_structured_block (parser);
37006 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37007 if_p);
37008 cp_parser_end_omp_structured_block (parser, save);
37009 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37010 block);
37011 if (ret == NULL_TREE)
37012 return ret;
37013 OMP_PARALLEL_COMBINED (stmt) = 1;
37014 return stmt;
37016 /* When combined with distribute, parallel has to be followed by for.
37017 #pragma omp target parallel is allowed though. */
37018 else if (cclauses
37019 && (mask & (OMP_CLAUSE_MASK_1
37020 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37022 error_at (loc, "expected %<for%> after %qs", p_name);
37023 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37024 return NULL_TREE;
37026 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37028 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37029 const char *p = IDENTIFIER_POINTER (id);
37030 if (strcmp (p, "master") == 0)
37032 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37033 cclauses = cclauses_buf;
37035 cp_lexer_consume_token (parser->lexer);
37036 block = begin_omp_parallel ();
37037 save = cp_parser_begin_omp_structured_block (parser);
37038 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37039 cclauses, if_p);
37040 cp_parser_end_omp_structured_block (parser, save);
37041 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37042 block);
37043 OMP_PARALLEL_COMBINED (stmt) = 1;
37044 if (ret == NULL_TREE)
37045 return ret;
37046 return stmt;
37048 else if (!flag_openmp) /* flag_openmp_simd */
37050 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37051 return NULL_TREE;
37053 else if (strcmp (p, "sections") == 0)
37055 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37056 cclauses = cclauses_buf;
37058 cp_lexer_consume_token (parser->lexer);
37059 block = begin_omp_parallel ();
37060 save = cp_parser_begin_omp_structured_block (parser);
37061 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
37062 cp_parser_end_omp_structured_block (parser, save);
37063 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37064 block);
37065 OMP_PARALLEL_COMBINED (stmt) = 1;
37066 return stmt;
37069 else if (!flag_openmp) /* flag_openmp_simd */
37071 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37072 return NULL_TREE;
37075 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37076 cclauses == NULL);
37077 if (cclauses)
37079 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
37080 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
37083 block = begin_omp_parallel ();
37084 save = cp_parser_begin_omp_structured_block (parser);
37085 cp_parser_statement (parser, NULL_TREE, false, if_p);
37086 cp_parser_end_omp_structured_block (parser, save);
37087 stmt = finish_omp_parallel (clauses, block);
37088 return stmt;
37091 /* OpenMP 2.5:
37092 # pragma omp single single-clause[optseq] new-line
37093 structured-block */
37095 #define OMP_SINGLE_CLAUSE_MASK \
37096 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37101 static tree
37102 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37104 tree stmt = make_node (OMP_SINGLE);
37105 TREE_TYPE (stmt) = void_type_node;
37106 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37108 OMP_SINGLE_CLAUSES (stmt)
37109 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
37110 "#pragma omp single", pragma_tok);
37111 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37113 return add_stmt (stmt);
37116 /* OpenMP 3.0:
37117 # pragma omp task task-clause[optseq] new-line
37118 structured-block */
37120 #define OMP_TASK_CLAUSE_MASK \
37121 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37133 static tree
37134 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37136 tree clauses, block;
37137 unsigned int save;
37139 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
37140 "#pragma omp task", pragma_tok);
37141 block = begin_omp_task ();
37142 save = cp_parser_begin_omp_structured_block (parser);
37143 cp_parser_statement (parser, NULL_TREE, false, if_p);
37144 cp_parser_end_omp_structured_block (parser, save);
37145 return finish_omp_task (clauses, block);
37148 /* OpenMP 3.0:
37149 # pragma omp taskwait new-line
37151 OpenMP 5.0:
37152 # pragma omp taskwait taskwait-clause[opt] new-line */
37154 #define OMP_TASKWAIT_CLAUSE_MASK \
37155 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37157 static void
37158 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
37160 tree clauses
37161 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
37162 "#pragma omp taskwait", pragma_tok);
37164 if (clauses)
37166 tree stmt = make_node (OMP_TASK);
37167 TREE_TYPE (stmt) = void_node;
37168 OMP_TASK_CLAUSES (stmt) = clauses;
37169 OMP_TASK_BODY (stmt) = NULL_TREE;
37170 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37171 add_stmt (stmt);
37173 else
37174 finish_omp_taskwait ();
37177 /* OpenMP 3.1:
37178 # pragma omp taskyield new-line */
37180 static void
37181 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
37183 cp_parser_require_pragma_eol (parser, pragma_tok);
37184 finish_omp_taskyield ();
37187 /* OpenMP 4.0:
37188 # pragma omp taskgroup new-line
37189 structured-block
37191 OpenMP 5.0:
37192 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37194 #define OMP_TASKGROUP_CLAUSE_MASK \
37195 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37197 static tree
37198 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37200 tree clauses
37201 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
37202 "#pragma omp taskgroup", pragma_tok);
37203 return c_finish_omp_taskgroup (input_location,
37204 cp_parser_omp_structured_block (parser,
37205 if_p),
37206 clauses);
37210 /* OpenMP 2.5:
37211 # pragma omp threadprivate (variable-list) */
37213 static void
37214 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
37216 tree vars;
37218 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37219 cp_parser_require_pragma_eol (parser, pragma_tok);
37221 finish_omp_threadprivate (vars);
37224 /* OpenMP 4.0:
37225 # pragma omp cancel cancel-clause[optseq] new-line */
37227 #define OMP_CANCEL_CLAUSE_MASK \
37228 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37234 static void
37235 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
37237 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
37238 "#pragma omp cancel", pragma_tok);
37239 finish_omp_cancel (clauses);
37242 /* OpenMP 4.0:
37243 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37245 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37246 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37251 static void
37252 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
37253 enum pragma_context context)
37255 tree clauses;
37256 bool point_seen = false;
37258 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37260 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37261 const char *p = IDENTIFIER_POINTER (id);
37263 if (strcmp (p, "point") == 0)
37265 cp_lexer_consume_token (parser->lexer);
37266 point_seen = true;
37269 if (!point_seen)
37271 cp_parser_error (parser, "expected %<point%>");
37272 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37273 return;
37276 if (context != pragma_compound)
37278 if (context == pragma_stmt)
37279 error_at (pragma_tok->location,
37280 "%<#pragma %s%> may only be used in compound statements",
37281 "omp cancellation point");
37282 else
37283 cp_parser_error (parser, "expected declaration specifiers");
37284 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37285 return;
37288 clauses = cp_parser_omp_all_clauses (parser,
37289 OMP_CANCELLATION_POINT_CLAUSE_MASK,
37290 "#pragma omp cancellation point",
37291 pragma_tok);
37292 finish_omp_cancellation_point (clauses);
37295 /* OpenMP 4.0:
37296 #pragma omp distribute distribute-clause[optseq] new-line
37297 for-loop */
37299 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37300 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37306 static tree
37307 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
37308 char *p_name, omp_clause_mask mask, tree *cclauses,
37309 bool *if_p)
37311 tree clauses, sb, ret;
37312 unsigned int save;
37313 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37315 strcat (p_name, " distribute");
37316 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
37318 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37320 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37321 const char *p = IDENTIFIER_POINTER (id);
37322 bool simd = false;
37323 bool parallel = false;
37325 if (strcmp (p, "simd") == 0)
37326 simd = true;
37327 else
37328 parallel = strcmp (p, "parallel") == 0;
37329 if (parallel || simd)
37331 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37332 if (cclauses == NULL)
37333 cclauses = cclauses_buf;
37334 cp_lexer_consume_token (parser->lexer);
37335 if (!flag_openmp) /* flag_openmp_simd */
37337 if (simd)
37338 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37339 cclauses, if_p);
37340 else
37341 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37342 cclauses, if_p);
37344 sb = begin_omp_structured_block ();
37345 save = cp_parser_begin_omp_structured_block (parser);
37346 if (simd)
37347 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37348 cclauses, if_p);
37349 else
37350 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37351 cclauses, if_p);
37352 cp_parser_end_omp_structured_block (parser, save);
37353 tree body = finish_omp_structured_block (sb);
37354 if (ret == NULL)
37355 return ret;
37356 ret = make_node (OMP_DISTRIBUTE);
37357 TREE_TYPE (ret) = void_type_node;
37358 OMP_FOR_BODY (ret) = body;
37359 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37360 SET_EXPR_LOCATION (ret, loc);
37361 add_stmt (ret);
37362 return ret;
37365 if (!flag_openmp) /* flag_openmp_simd */
37367 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37368 return NULL_TREE;
37371 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37372 cclauses == NULL);
37373 if (cclauses)
37375 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
37376 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37379 keep_next_level (true);
37380 sb = begin_omp_structured_block ();
37381 save = cp_parser_begin_omp_structured_block (parser);
37383 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
37385 cp_parser_end_omp_structured_block (parser, save);
37386 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37388 return ret;
37391 /* OpenMP 4.0:
37392 # pragma omp teams teams-clause[optseq] new-line
37393 structured-block */
37395 #define OMP_TEAMS_CLAUSE_MASK \
37396 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37404 static tree
37405 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
37406 char *p_name, omp_clause_mask mask, tree *cclauses,
37407 bool *if_p)
37409 tree clauses, sb, ret;
37410 unsigned int save;
37411 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37413 strcat (p_name, " teams");
37414 mask |= OMP_TEAMS_CLAUSE_MASK;
37416 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37418 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37419 const char *p = IDENTIFIER_POINTER (id);
37420 if (strcmp (p, "distribute") == 0)
37422 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37423 if (cclauses == NULL)
37424 cclauses = cclauses_buf;
37426 cp_lexer_consume_token (parser->lexer);
37427 if (!flag_openmp) /* flag_openmp_simd */
37428 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37429 cclauses, if_p);
37430 keep_next_level (true);
37431 sb = begin_omp_structured_block ();
37432 save = cp_parser_begin_omp_structured_block (parser);
37433 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37434 cclauses, if_p);
37435 cp_parser_end_omp_structured_block (parser, save);
37436 tree body = finish_omp_structured_block (sb);
37437 if (ret == NULL)
37438 return ret;
37439 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37440 ret = make_node (OMP_TEAMS);
37441 TREE_TYPE (ret) = void_type_node;
37442 OMP_TEAMS_CLAUSES (ret) = clauses;
37443 OMP_TEAMS_BODY (ret) = body;
37444 OMP_TEAMS_COMBINED (ret) = 1;
37445 SET_EXPR_LOCATION (ret, loc);
37446 return add_stmt (ret);
37449 if (!flag_openmp) /* flag_openmp_simd */
37451 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37452 return NULL_TREE;
37455 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37456 cclauses == NULL);
37457 if (cclauses)
37459 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
37460 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37463 tree stmt = make_node (OMP_TEAMS);
37464 TREE_TYPE (stmt) = void_type_node;
37465 OMP_TEAMS_CLAUSES (stmt) = clauses;
37466 keep_next_level (true);
37467 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37468 SET_EXPR_LOCATION (stmt, loc);
37470 return add_stmt (stmt);
37473 /* OpenMP 4.0:
37474 # pragma omp target data target-data-clause[optseq] new-line
37475 structured-block */
37477 #define OMP_TARGET_DATA_CLAUSE_MASK \
37478 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
37483 static tree
37484 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37486 tree clauses
37487 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
37488 "#pragma omp target data", pragma_tok);
37489 int map_seen = 0;
37490 for (tree *pc = &clauses; *pc;)
37492 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37493 switch (OMP_CLAUSE_MAP_KIND (*pc))
37495 case GOMP_MAP_TO:
37496 case GOMP_MAP_ALWAYS_TO:
37497 case GOMP_MAP_FROM:
37498 case GOMP_MAP_ALWAYS_FROM:
37499 case GOMP_MAP_TOFROM:
37500 case GOMP_MAP_ALWAYS_TOFROM:
37501 case GOMP_MAP_ALLOC:
37502 map_seen = 3;
37503 break;
37504 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37505 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37506 case GOMP_MAP_ALWAYS_POINTER:
37507 break;
37508 default:
37509 map_seen |= 1;
37510 error_at (OMP_CLAUSE_LOCATION (*pc),
37511 "%<#pragma omp target data%> with map-type other "
37512 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37513 "on %<map%> clause");
37514 *pc = OMP_CLAUSE_CHAIN (*pc);
37515 continue;
37517 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
37518 map_seen = 3;
37519 pc = &OMP_CLAUSE_CHAIN (*pc);
37522 if (map_seen != 3)
37524 if (map_seen == 0)
37525 error_at (pragma_tok->location,
37526 "%<#pragma omp target data%> must contain at least "
37527 "one %<map%> or %<use_device_ptr%> clause");
37528 return NULL_TREE;
37531 tree stmt = make_node (OMP_TARGET_DATA);
37532 TREE_TYPE (stmt) = void_type_node;
37533 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
37535 keep_next_level (true);
37536 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37538 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37539 return add_stmt (stmt);
37542 /* OpenMP 4.5:
37543 # pragma omp target enter data target-enter-data-clause[optseq] new-line
37544 structured-block */
37546 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
37547 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37553 static tree
37554 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
37555 enum pragma_context context)
37557 bool data_seen = false;
37558 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37560 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37561 const char *p = IDENTIFIER_POINTER (id);
37563 if (strcmp (p, "data") == 0)
37565 cp_lexer_consume_token (parser->lexer);
37566 data_seen = true;
37569 if (!data_seen)
37571 cp_parser_error (parser, "expected %<data%>");
37572 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37573 return NULL_TREE;
37576 if (context == pragma_stmt)
37578 error_at (pragma_tok->location,
37579 "%<#pragma %s%> may only be used in compound statements",
37580 "omp target enter data");
37581 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37582 return NULL_TREE;
37585 tree clauses
37586 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
37587 "#pragma omp target enter data", pragma_tok);
37588 int map_seen = 0;
37589 for (tree *pc = &clauses; *pc;)
37591 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37592 switch (OMP_CLAUSE_MAP_KIND (*pc))
37594 case GOMP_MAP_TO:
37595 case GOMP_MAP_ALWAYS_TO:
37596 case GOMP_MAP_ALLOC:
37597 map_seen = 3;
37598 break;
37599 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37600 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37601 case GOMP_MAP_ALWAYS_POINTER:
37602 break;
37603 default:
37604 map_seen |= 1;
37605 error_at (OMP_CLAUSE_LOCATION (*pc),
37606 "%<#pragma omp target enter data%> with map-type other "
37607 "than %<to%> or %<alloc%> on %<map%> clause");
37608 *pc = OMP_CLAUSE_CHAIN (*pc);
37609 continue;
37611 pc = &OMP_CLAUSE_CHAIN (*pc);
37614 if (map_seen != 3)
37616 if (map_seen == 0)
37617 error_at (pragma_tok->location,
37618 "%<#pragma omp target enter data%> must contain at least "
37619 "one %<map%> clause");
37620 return NULL_TREE;
37623 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
37624 TREE_TYPE (stmt) = void_type_node;
37625 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
37626 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37627 return add_stmt (stmt);
37630 /* OpenMP 4.5:
37631 # pragma omp target exit data target-enter-data-clause[optseq] new-line
37632 structured-block */
37634 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
37635 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37641 static tree
37642 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
37643 enum pragma_context context)
37645 bool data_seen = false;
37646 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37648 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37649 const char *p = IDENTIFIER_POINTER (id);
37651 if (strcmp (p, "data") == 0)
37653 cp_lexer_consume_token (parser->lexer);
37654 data_seen = true;
37657 if (!data_seen)
37659 cp_parser_error (parser, "expected %<data%>");
37660 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37661 return NULL_TREE;
37664 if (context == pragma_stmt)
37666 error_at (pragma_tok->location,
37667 "%<#pragma %s%> may only be used in compound statements",
37668 "omp target exit data");
37669 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37670 return NULL_TREE;
37673 tree clauses
37674 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
37675 "#pragma omp target exit data", pragma_tok);
37676 int map_seen = 0;
37677 for (tree *pc = &clauses; *pc;)
37679 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37680 switch (OMP_CLAUSE_MAP_KIND (*pc))
37682 case GOMP_MAP_FROM:
37683 case GOMP_MAP_ALWAYS_FROM:
37684 case GOMP_MAP_RELEASE:
37685 case GOMP_MAP_DELETE:
37686 map_seen = 3;
37687 break;
37688 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37689 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37690 case GOMP_MAP_ALWAYS_POINTER:
37691 break;
37692 default:
37693 map_seen |= 1;
37694 error_at (OMP_CLAUSE_LOCATION (*pc),
37695 "%<#pragma omp target exit data%> with map-type other "
37696 "than %<from%>, %<release%> or %<delete%> on %<map%>"
37697 " clause");
37698 *pc = OMP_CLAUSE_CHAIN (*pc);
37699 continue;
37701 pc = &OMP_CLAUSE_CHAIN (*pc);
37704 if (map_seen != 3)
37706 if (map_seen == 0)
37707 error_at (pragma_tok->location,
37708 "%<#pragma omp target exit data%> must contain at least "
37709 "one %<map%> clause");
37710 return NULL_TREE;
37713 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
37714 TREE_TYPE (stmt) = void_type_node;
37715 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
37716 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37717 return add_stmt (stmt);
37720 /* OpenMP 4.0:
37721 # pragma omp target update target-update-clause[optseq] new-line */
37723 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
37724 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
37725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37731 static bool
37732 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
37733 enum pragma_context context)
37735 if (context == pragma_stmt)
37737 error_at (pragma_tok->location,
37738 "%<#pragma %s%> may only be used in compound statements",
37739 "omp target update");
37740 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37741 return false;
37744 tree clauses
37745 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
37746 "#pragma omp target update", pragma_tok);
37747 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
37748 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
37750 error_at (pragma_tok->location,
37751 "%<#pragma omp target update%> must contain at least one "
37752 "%<from%> or %<to%> clauses");
37753 return false;
37756 tree stmt = make_node (OMP_TARGET_UPDATE);
37757 TREE_TYPE (stmt) = void_type_node;
37758 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
37759 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37760 add_stmt (stmt);
37761 return false;
37764 /* OpenMP 4.0:
37765 # pragma omp target target-clause[optseq] new-line
37766 structured-block */
37768 #define OMP_TARGET_CLAUSE_MASK \
37769 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
37777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
37779 static bool
37780 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
37781 enum pragma_context context, bool *if_p)
37783 tree *pc = NULL, stmt;
37785 if (flag_openmp)
37786 omp_requires_mask
37787 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
37789 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37791 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37792 const char *p = IDENTIFIER_POINTER (id);
37793 enum tree_code ccode = ERROR_MARK;
37795 if (strcmp (p, "teams") == 0)
37796 ccode = OMP_TEAMS;
37797 else if (strcmp (p, "parallel") == 0)
37798 ccode = OMP_PARALLEL;
37799 else if (strcmp (p, "simd") == 0)
37800 ccode = OMP_SIMD;
37801 if (ccode != ERROR_MARK)
37803 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
37804 char p_name[sizeof ("#pragma omp target teams distribute "
37805 "parallel for simd")];
37807 cp_lexer_consume_token (parser->lexer);
37808 strcpy (p_name, "#pragma omp target");
37809 if (!flag_openmp) /* flag_openmp_simd */
37811 tree stmt;
37812 switch (ccode)
37814 case OMP_TEAMS:
37815 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
37816 OMP_TARGET_CLAUSE_MASK,
37817 cclauses, if_p);
37818 break;
37819 case OMP_PARALLEL:
37820 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
37821 OMP_TARGET_CLAUSE_MASK,
37822 cclauses, if_p);
37823 break;
37824 case OMP_SIMD:
37825 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
37826 OMP_TARGET_CLAUSE_MASK,
37827 cclauses, if_p);
37828 break;
37829 default:
37830 gcc_unreachable ();
37832 return stmt != NULL_TREE;
37834 keep_next_level (true);
37835 tree sb = begin_omp_structured_block (), ret;
37836 unsigned save = cp_parser_begin_omp_structured_block (parser);
37837 switch (ccode)
37839 case OMP_TEAMS:
37840 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
37841 OMP_TARGET_CLAUSE_MASK, cclauses,
37842 if_p);
37843 break;
37844 case OMP_PARALLEL:
37845 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
37846 OMP_TARGET_CLAUSE_MASK, cclauses,
37847 if_p);
37848 break;
37849 case OMP_SIMD:
37850 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
37851 OMP_TARGET_CLAUSE_MASK, cclauses,
37852 if_p);
37853 break;
37854 default:
37855 gcc_unreachable ();
37857 cp_parser_end_omp_structured_block (parser, save);
37858 tree body = finish_omp_structured_block (sb);
37859 if (ret == NULL_TREE)
37860 return false;
37861 if (ccode == OMP_TEAMS && !processing_template_decl)
37863 /* For combined target teams, ensure the num_teams and
37864 thread_limit clause expressions are evaluated on the host,
37865 before entering the target construct. */
37866 tree c;
37867 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37868 c; c = OMP_CLAUSE_CHAIN (c))
37869 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
37870 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
37871 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
37873 tree expr = OMP_CLAUSE_OPERAND (c, 0);
37874 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
37875 if (expr == error_mark_node)
37876 continue;
37877 tree tmp = TARGET_EXPR_SLOT (expr);
37878 add_stmt (expr);
37879 OMP_CLAUSE_OPERAND (c, 0) = expr;
37880 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
37881 OMP_CLAUSE_FIRSTPRIVATE);
37882 OMP_CLAUSE_DECL (tc) = tmp;
37883 OMP_CLAUSE_CHAIN (tc)
37884 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
37885 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
37888 tree stmt = make_node (OMP_TARGET);
37889 TREE_TYPE (stmt) = void_type_node;
37890 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
37891 OMP_TARGET_BODY (stmt) = body;
37892 OMP_TARGET_COMBINED (stmt) = 1;
37893 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37894 add_stmt (stmt);
37895 pc = &OMP_TARGET_CLAUSES (stmt);
37896 goto check_clauses;
37898 else if (!flag_openmp) /* flag_openmp_simd */
37900 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37901 return false;
37903 else if (strcmp (p, "data") == 0)
37905 cp_lexer_consume_token (parser->lexer);
37906 cp_parser_omp_target_data (parser, pragma_tok, if_p);
37907 return true;
37909 else if (strcmp (p, "enter") == 0)
37911 cp_lexer_consume_token (parser->lexer);
37912 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
37913 return false;
37915 else if (strcmp (p, "exit") == 0)
37917 cp_lexer_consume_token (parser->lexer);
37918 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
37919 return false;
37921 else if (strcmp (p, "update") == 0)
37923 cp_lexer_consume_token (parser->lexer);
37924 return cp_parser_omp_target_update (parser, pragma_tok, context);
37927 if (!flag_openmp) /* flag_openmp_simd */
37929 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37930 return false;
37933 stmt = make_node (OMP_TARGET);
37934 TREE_TYPE (stmt) = void_type_node;
37936 OMP_TARGET_CLAUSES (stmt)
37937 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
37938 "#pragma omp target", pragma_tok);
37939 pc = &OMP_TARGET_CLAUSES (stmt);
37940 keep_next_level (true);
37941 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37943 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37944 add_stmt (stmt);
37946 check_clauses:
37947 while (*pc)
37949 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37950 switch (OMP_CLAUSE_MAP_KIND (*pc))
37952 case GOMP_MAP_TO:
37953 case GOMP_MAP_ALWAYS_TO:
37954 case GOMP_MAP_FROM:
37955 case GOMP_MAP_ALWAYS_FROM:
37956 case GOMP_MAP_TOFROM:
37957 case GOMP_MAP_ALWAYS_TOFROM:
37958 case GOMP_MAP_ALLOC:
37959 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37960 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37961 case GOMP_MAP_ALWAYS_POINTER:
37962 break;
37963 default:
37964 error_at (OMP_CLAUSE_LOCATION (*pc),
37965 "%<#pragma omp target%> with map-type other "
37966 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37967 "on %<map%> clause");
37968 *pc = OMP_CLAUSE_CHAIN (*pc);
37969 continue;
37971 pc = &OMP_CLAUSE_CHAIN (*pc);
37973 return true;
37976 /* OpenACC 2.0:
37977 # pragma acc cache (variable-list) new-line
37980 static tree
37981 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
37983 tree stmt, clauses;
37985 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
37986 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37988 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
37990 stmt = make_node (OACC_CACHE);
37991 TREE_TYPE (stmt) = void_type_node;
37992 OACC_CACHE_CLAUSES (stmt) = clauses;
37993 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37994 add_stmt (stmt);
37996 return stmt;
37999 /* OpenACC 2.0:
38000 # pragma acc data oacc-data-clause[optseq] new-line
38001 structured-block */
38003 #define OACC_DATA_CLAUSE_MASK \
38004 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38012 static tree
38013 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38015 tree stmt, clauses, block;
38016 unsigned int save;
38018 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
38019 "#pragma acc data", pragma_tok);
38021 block = begin_omp_parallel ();
38022 save = cp_parser_begin_omp_structured_block (parser);
38023 cp_parser_statement (parser, NULL_TREE, false, if_p);
38024 cp_parser_end_omp_structured_block (parser, save);
38025 stmt = finish_oacc_data (clauses, block);
38026 return stmt;
38029 /* OpenACC 2.0:
38030 # pragma acc host_data <clauses> new-line
38031 structured-block */
38033 #define OACC_HOST_DATA_CLAUSE_MASK \
38034 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38036 static tree
38037 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38039 tree stmt, clauses, block;
38040 unsigned int save;
38042 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
38043 "#pragma acc host_data", pragma_tok);
38045 block = begin_omp_parallel ();
38046 save = cp_parser_begin_omp_structured_block (parser);
38047 cp_parser_statement (parser, NULL_TREE, false, if_p);
38048 cp_parser_end_omp_structured_block (parser, save);
38049 stmt = finish_oacc_host_data (clauses, block);
38050 return stmt;
38053 /* OpenACC 2.0:
38054 # pragma acc declare oacc-data-clause[optseq] new-line
38057 #define OACC_DECLARE_CLAUSE_MASK \
38058 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38067 static tree
38068 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
38070 tree clauses, stmt;
38071 bool error = false;
38073 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
38074 "#pragma acc declare", pragma_tok, true);
38077 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38079 error_at (pragma_tok->location,
38080 "no valid clauses specified in %<#pragma acc declare%>");
38081 return NULL_TREE;
38084 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
38086 location_t loc = OMP_CLAUSE_LOCATION (t);
38087 tree decl = OMP_CLAUSE_DECL (t);
38088 if (!DECL_P (decl))
38090 error_at (loc, "array section in %<#pragma acc declare%>");
38091 error = true;
38092 continue;
38094 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
38095 switch (OMP_CLAUSE_MAP_KIND (t))
38097 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38098 case GOMP_MAP_ALLOC:
38099 case GOMP_MAP_TO:
38100 case GOMP_MAP_FORCE_DEVICEPTR:
38101 case GOMP_MAP_DEVICE_RESIDENT:
38102 break;
38104 case GOMP_MAP_LINK:
38105 if (!global_bindings_p ()
38106 && (TREE_STATIC (decl)
38107 || !DECL_EXTERNAL (decl)))
38109 error_at (loc,
38110 "%qD must be a global variable in "
38111 "%<#pragma acc declare link%>",
38112 decl);
38113 error = true;
38114 continue;
38116 break;
38118 default:
38119 if (global_bindings_p ())
38121 error_at (loc, "invalid OpenACC clause at file scope");
38122 error = true;
38123 continue;
38125 if (DECL_EXTERNAL (decl))
38127 error_at (loc,
38128 "invalid use of %<extern%> variable %qD "
38129 "in %<#pragma acc declare%>", decl);
38130 error = true;
38131 continue;
38133 else if (TREE_PUBLIC (decl))
38135 error_at (loc,
38136 "invalid use of %<global%> variable %qD "
38137 "in %<#pragma acc declare%>", decl);
38138 error = true;
38139 continue;
38141 break;
38144 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
38145 || lookup_attribute ("omp declare target link",
38146 DECL_ATTRIBUTES (decl)))
38148 error_at (loc, "variable %qD used more than once with "
38149 "%<#pragma acc declare%>", decl);
38150 error = true;
38151 continue;
38154 if (!error)
38156 tree id;
38158 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
38159 id = get_identifier ("omp declare target link");
38160 else
38161 id = get_identifier ("omp declare target");
38163 DECL_ATTRIBUTES (decl)
38164 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
38165 if (global_bindings_p ())
38167 symtab_node *node = symtab_node::get (decl);
38168 if (node != NULL)
38170 node->offloadable = 1;
38171 if (ENABLE_OFFLOADING)
38173 g->have_offload = true;
38174 if (is_a <varpool_node *> (node))
38175 vec_safe_push (offload_vars, decl);
38182 if (error || global_bindings_p ())
38183 return NULL_TREE;
38185 stmt = make_node (OACC_DECLARE);
38186 TREE_TYPE (stmt) = void_type_node;
38187 OACC_DECLARE_CLAUSES (stmt) = clauses;
38188 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38190 add_stmt (stmt);
38192 return NULL_TREE;
38195 /* OpenACC 2.0:
38196 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38200 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38202 LOC is the location of the #pragma token.
38205 #define OACC_ENTER_DATA_CLAUSE_MASK \
38206 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38212 #define OACC_EXIT_DATA_CLAUSE_MASK \
38213 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38220 static tree
38221 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
38222 bool enter)
38224 location_t loc = pragma_tok->location;
38225 tree stmt, clauses;
38226 const char *p = "";
38228 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38229 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38231 if (strcmp (p, "data") != 0)
38233 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
38234 enter ? "enter" : "exit");
38235 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38236 return NULL_TREE;
38239 cp_lexer_consume_token (parser->lexer);
38241 if (enter)
38242 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
38243 "#pragma acc enter data", pragma_tok);
38244 else
38245 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
38246 "#pragma acc exit data", pragma_tok);
38248 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38250 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
38251 enter ? "enter" : "exit");
38252 return NULL_TREE;
38255 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
38256 TREE_TYPE (stmt) = void_type_node;
38257 OMP_STANDALONE_CLAUSES (stmt) = clauses;
38258 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38259 add_stmt (stmt);
38260 return stmt;
38263 /* OpenACC 2.0:
38264 # pragma acc loop oacc-loop-clause[optseq] new-line
38265 structured-block */
38267 #define OACC_LOOP_CLAUSE_MASK \
38268 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38279 static tree
38280 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
38281 omp_clause_mask mask, tree *cclauses, bool *if_p)
38283 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
38285 strcat (p_name, " loop");
38286 mask |= OACC_LOOP_CLAUSE_MASK;
38288 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
38289 cclauses == NULL);
38290 if (cclauses)
38292 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
38293 if (*cclauses)
38294 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
38295 if (clauses)
38296 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38299 tree block = begin_omp_structured_block ();
38300 int save = cp_parser_begin_omp_structured_block (parser);
38301 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
38302 cp_parser_end_omp_structured_block (parser, save);
38303 add_stmt (finish_omp_structured_block (block));
38305 return stmt;
38308 /* OpenACC 2.0:
38309 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38310 structured-block
38314 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38315 structured-block
38318 #define OACC_KERNELS_CLAUSE_MASK \
38319 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38333 #define OACC_PARALLEL_CLAUSE_MASK \
38334 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38351 static tree
38352 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
38353 char *p_name, bool *if_p)
38355 omp_clause_mask mask;
38356 enum tree_code code;
38357 switch (cp_parser_pragma_kind (pragma_tok))
38359 case PRAGMA_OACC_KERNELS:
38360 strcat (p_name, " kernels");
38361 mask = OACC_KERNELS_CLAUSE_MASK;
38362 code = OACC_KERNELS;
38363 break;
38364 case PRAGMA_OACC_PARALLEL:
38365 strcat (p_name, " parallel");
38366 mask = OACC_PARALLEL_CLAUSE_MASK;
38367 code = OACC_PARALLEL;
38368 break;
38369 default:
38370 gcc_unreachable ();
38373 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38375 const char *p
38376 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38377 if (strcmp (p, "loop") == 0)
38379 cp_lexer_consume_token (parser->lexer);
38380 tree block = begin_omp_parallel ();
38381 tree clauses;
38382 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
38383 if_p);
38384 return finish_omp_construct (code, block, clauses);
38388 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
38390 tree block = begin_omp_parallel ();
38391 unsigned int save = cp_parser_begin_omp_structured_block (parser);
38392 cp_parser_statement (parser, NULL_TREE, false, if_p);
38393 cp_parser_end_omp_structured_block (parser, save);
38394 return finish_omp_construct (code, block, clauses);
38397 /* OpenACC 2.0:
38398 # pragma acc update oacc-update-clause[optseq] new-line
38401 #define OACC_UPDATE_CLAUSE_MASK \
38402 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38409 static tree
38410 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
38412 tree stmt, clauses;
38414 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
38415 "#pragma acc update", pragma_tok);
38417 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38419 error_at (pragma_tok->location,
38420 "%<#pragma acc update%> must contain at least one "
38421 "%<device%> or %<host%> or %<self%> clause");
38422 return NULL_TREE;
38425 stmt = make_node (OACC_UPDATE);
38426 TREE_TYPE (stmt) = void_type_node;
38427 OACC_UPDATE_CLAUSES (stmt) = clauses;
38428 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38429 add_stmt (stmt);
38430 return stmt;
38433 /* OpenACC 2.0:
38434 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38436 LOC is the location of the #pragma token.
38439 #define OACC_WAIT_CLAUSE_MASK \
38440 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
38442 static tree
38443 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
38445 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
38446 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38448 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38449 list = cp_parser_oacc_wait_list (parser, loc, list);
38451 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
38452 "#pragma acc wait", pragma_tok);
38454 stmt = c_finish_oacc_wait (loc, list, clauses);
38455 stmt = finish_expr_stmt (stmt);
38457 return stmt;
38460 /* OpenMP 4.0:
38461 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
38463 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
38464 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
38468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
38469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
38471 static void
38472 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
38473 enum pragma_context context)
38475 bool first_p = parser->omp_declare_simd == NULL;
38476 cp_omp_declare_simd_data data;
38477 if (first_p)
38479 data.error_seen = false;
38480 data.fndecl_seen = false;
38481 data.tokens = vNULL;
38482 data.clauses = NULL_TREE;
38483 /* It is safe to take the address of a local variable; it will only be
38484 used while this scope is live. */
38485 parser->omp_declare_simd = &data;
38488 /* Store away all pragma tokens. */
38489 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38490 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38491 cp_lexer_consume_token (parser->lexer);
38492 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38493 parser->omp_declare_simd->error_seen = true;
38494 cp_parser_require_pragma_eol (parser, pragma_tok);
38495 struct cp_token_cache *cp
38496 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38497 parser->omp_declare_simd->tokens.safe_push (cp);
38499 if (first_p)
38501 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38502 cp_parser_pragma (parser, context, NULL);
38503 switch (context)
38505 case pragma_external:
38506 cp_parser_declaration (parser);
38507 break;
38508 case pragma_member:
38509 cp_parser_member_declaration (parser);
38510 break;
38511 case pragma_objc_icode:
38512 cp_parser_block_declaration (parser, /*statement_p=*/false);
38513 break;
38514 default:
38515 cp_parser_declaration_statement (parser);
38516 break;
38518 if (parser->omp_declare_simd
38519 && !parser->omp_declare_simd->error_seen
38520 && !parser->omp_declare_simd->fndecl_seen)
38521 error_at (pragma_tok->location,
38522 "%<#pragma omp declare simd%> not immediately followed by "
38523 "function declaration or definition");
38524 data.tokens.release ();
38525 parser->omp_declare_simd = NULL;
38529 /* Finalize #pragma omp declare simd clauses after direct declarator has
38530 been parsed, and put that into "omp declare simd" attribute. */
38532 static tree
38533 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
38535 struct cp_token_cache *ce;
38536 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
38537 int i;
38539 if (!data->error_seen && data->fndecl_seen)
38541 error ("%<#pragma omp declare simd%> not immediately followed by "
38542 "a single function declaration or definition");
38543 data->error_seen = true;
38545 if (data->error_seen)
38546 return attrs;
38548 FOR_EACH_VEC_ELT (data->tokens, i, ce)
38550 tree c, cl;
38552 cp_parser_push_lexer_for_tokens (parser, ce);
38553 parser->lexer->in_pragma = true;
38554 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38555 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38556 cp_lexer_consume_token (parser->lexer);
38557 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
38558 "#pragma omp declare simd", pragma_tok);
38559 cp_parser_pop_lexer (parser);
38560 if (cl)
38561 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
38562 c = build_tree_list (get_identifier ("omp declare simd"), cl);
38563 TREE_CHAIN (c) = attrs;
38564 if (processing_template_decl)
38565 ATTR_IS_DEPENDENT (c) = 1;
38566 attrs = c;
38569 data->fndecl_seen = true;
38570 return attrs;
38574 /* OpenMP 4.0:
38575 # pragma omp declare target new-line
38576 declarations and definitions
38577 # pragma omp end declare target new-line
38579 OpenMP 4.5:
38580 # pragma omp declare target ( extended-list ) new-line
38582 # pragma omp declare target declare-target-clauses[seq] new-line */
38584 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
38585 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
38588 static void
38589 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
38591 tree clauses = NULL_TREE;
38592 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38593 clauses
38594 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
38595 "#pragma omp declare target", pragma_tok);
38596 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38598 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
38599 clauses);
38600 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
38601 cp_parser_require_pragma_eol (parser, pragma_tok);
38603 else
38605 cp_parser_require_pragma_eol (parser, pragma_tok);
38606 scope_chain->omp_declare_target_attribute++;
38607 return;
38609 if (scope_chain->omp_declare_target_attribute)
38610 error_at (pragma_tok->location,
38611 "%<#pragma omp declare target%> with clauses in between "
38612 "%<#pragma omp declare target%> without clauses and "
38613 "%<#pragma omp end declare target%>");
38614 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
38616 tree t = OMP_CLAUSE_DECL (c), id;
38617 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
38618 tree at2 = lookup_attribute ("omp declare target link",
38619 DECL_ATTRIBUTES (t));
38620 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
38622 id = get_identifier ("omp declare target link");
38623 std::swap (at1, at2);
38625 else
38626 id = get_identifier ("omp declare target");
38627 if (at2)
38629 error_at (OMP_CLAUSE_LOCATION (c),
38630 "%qD specified both in declare target %<link%> and %<to%>"
38631 " clauses", t);
38632 continue;
38634 if (!at1)
38636 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
38637 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
38638 continue;
38640 symtab_node *node = symtab_node::get (t);
38641 if (node != NULL)
38643 node->offloadable = 1;
38644 if (ENABLE_OFFLOADING)
38646 g->have_offload = true;
38647 if (is_a <varpool_node *> (node))
38648 vec_safe_push (offload_vars, t);
38655 static void
38656 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
38658 const char *p = "";
38659 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38661 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38662 p = IDENTIFIER_POINTER (id);
38664 if (strcmp (p, "declare") == 0)
38666 cp_lexer_consume_token (parser->lexer);
38667 p = "";
38668 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38670 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38671 p = IDENTIFIER_POINTER (id);
38673 if (strcmp (p, "target") == 0)
38674 cp_lexer_consume_token (parser->lexer);
38675 else
38677 cp_parser_error (parser, "expected %<target%>");
38678 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38679 return;
38682 else
38684 cp_parser_error (parser, "expected %<declare%>");
38685 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38686 return;
38688 cp_parser_require_pragma_eol (parser, pragma_tok);
38689 if (!scope_chain->omp_declare_target_attribute)
38690 error_at (pragma_tok->location,
38691 "%<#pragma omp end declare target%> without corresponding "
38692 "%<#pragma omp declare target%>");
38693 else
38694 scope_chain->omp_declare_target_attribute--;
38697 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
38698 expression and optional initializer clause of
38699 #pragma omp declare reduction. We store the expression(s) as
38700 either 3, 6 or 7 special statements inside of the artificial function's
38701 body. The first two statements are DECL_EXPRs for the artificial
38702 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
38703 expression that uses those variables.
38704 If there was any INITIALIZER clause, this is followed by further statements,
38705 the fourth and fifth statements are DECL_EXPRs for the artificial
38706 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
38707 constructor variant (first token after open paren is not omp_priv),
38708 then the sixth statement is a statement with the function call expression
38709 that uses the OMP_PRIV and optionally OMP_ORIG variable.
38710 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
38711 to initialize the OMP_PRIV artificial variable and there is seventh
38712 statement, a DECL_EXPR of the OMP_PRIV statement again. */
38714 static bool
38715 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
38717 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
38718 gcc_assert (TYPE_REF_P (type));
38719 type = TREE_TYPE (type);
38720 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
38721 DECL_ARTIFICIAL (omp_out) = 1;
38722 pushdecl (omp_out);
38723 add_decl_expr (omp_out);
38724 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
38725 DECL_ARTIFICIAL (omp_in) = 1;
38726 pushdecl (omp_in);
38727 add_decl_expr (omp_in);
38728 tree combiner;
38729 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
38731 keep_next_level (true);
38732 tree block = begin_omp_structured_block ();
38733 combiner = cp_parser_expression (parser);
38734 finish_expr_stmt (combiner);
38735 block = finish_omp_structured_block (block);
38736 add_stmt (block);
38738 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38739 return false;
38741 const char *p = "";
38742 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38744 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38745 p = IDENTIFIER_POINTER (id);
38748 if (strcmp (p, "initializer") == 0)
38750 cp_lexer_consume_token (parser->lexer);
38751 matching_parens parens;
38752 if (!parens.require_open (parser))
38753 return false;
38755 p = "";
38756 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38758 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38759 p = IDENTIFIER_POINTER (id);
38762 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
38763 DECL_ARTIFICIAL (omp_priv) = 1;
38764 pushdecl (omp_priv);
38765 add_decl_expr (omp_priv);
38766 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
38767 DECL_ARTIFICIAL (omp_orig) = 1;
38768 pushdecl (omp_orig);
38769 add_decl_expr (omp_orig);
38771 keep_next_level (true);
38772 block = begin_omp_structured_block ();
38774 bool ctor = false;
38775 if (strcmp (p, "omp_priv") == 0)
38777 bool is_direct_init, is_non_constant_init;
38778 ctor = true;
38779 cp_lexer_consume_token (parser->lexer);
38780 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
38781 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
38782 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
38783 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
38784 == CPP_CLOSE_PAREN
38785 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
38786 == CPP_CLOSE_PAREN))
38788 finish_omp_structured_block (block);
38789 error ("invalid initializer clause");
38790 return false;
38792 initializer = cp_parser_initializer (parser, &is_direct_init,
38793 &is_non_constant_init);
38794 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
38795 NULL_TREE, LOOKUP_ONLYCONVERTING);
38797 else
38799 cp_parser_parse_tentatively (parser);
38800 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
38801 /*check_dependency_p=*/true,
38802 /*template_p=*/NULL,
38803 /*declarator_p=*/false,
38804 /*optional_p=*/false);
38805 vec<tree, va_gc> *args;
38806 if (fn_name == error_mark_node
38807 || cp_parser_error_occurred (parser)
38808 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
38809 || ((args = cp_parser_parenthesized_expression_list
38810 (parser, non_attr, /*cast_p=*/false,
38811 /*allow_expansion_p=*/true,
38812 /*non_constant_p=*/NULL)),
38813 cp_parser_error_occurred (parser)))
38815 finish_omp_structured_block (block);
38816 cp_parser_abort_tentative_parse (parser);
38817 cp_parser_error (parser, "expected id-expression (arguments)");
38818 return false;
38820 unsigned int i;
38821 tree arg;
38822 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
38823 if (arg == omp_priv
38824 || (TREE_CODE (arg) == ADDR_EXPR
38825 && TREE_OPERAND (arg, 0) == omp_priv))
38826 break;
38827 cp_parser_abort_tentative_parse (parser);
38828 if (arg == NULL_TREE)
38829 error ("one of the initializer call arguments should be %<omp_priv%>"
38830 " or %<&omp_priv%>");
38831 initializer = cp_parser_postfix_expression (parser, false, false, false,
38832 false, NULL);
38833 finish_expr_stmt (initializer);
38836 block = finish_omp_structured_block (block);
38837 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
38838 add_stmt (block);
38840 if (ctor)
38841 add_decl_expr (omp_orig);
38843 if (!parens.require_close (parser))
38844 return false;
38847 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
38848 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
38849 UNKNOWN_LOCATION);
38851 return true;
38854 /* OpenMP 4.0
38855 #pragma omp declare reduction (reduction-id : typename-list : expression) \
38856 initializer-clause[opt] new-line
38858 initializer-clause:
38859 initializer (omp_priv initializer)
38860 initializer (function-name (argument-list)) */
38862 static void
38863 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
38864 enum pragma_context)
38866 auto_vec<tree> types;
38867 enum tree_code reduc_code = ERROR_MARK;
38868 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
38869 unsigned int i;
38870 cp_token *first_token;
38871 cp_token_cache *cp;
38872 int errs;
38873 void *p;
38875 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
38876 p = obstack_alloc (&declarator_obstack, 0);
38878 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38879 goto fail;
38881 switch (cp_lexer_peek_token (parser->lexer)->type)
38883 case CPP_PLUS:
38884 reduc_code = PLUS_EXPR;
38885 break;
38886 case CPP_MULT:
38887 reduc_code = MULT_EXPR;
38888 break;
38889 case CPP_MINUS:
38890 reduc_code = MINUS_EXPR;
38891 break;
38892 case CPP_AND:
38893 reduc_code = BIT_AND_EXPR;
38894 break;
38895 case CPP_XOR:
38896 reduc_code = BIT_XOR_EXPR;
38897 break;
38898 case CPP_OR:
38899 reduc_code = BIT_IOR_EXPR;
38900 break;
38901 case CPP_AND_AND:
38902 reduc_code = TRUTH_ANDIF_EXPR;
38903 break;
38904 case CPP_OR_OR:
38905 reduc_code = TRUTH_ORIF_EXPR;
38906 break;
38907 case CPP_NAME:
38908 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
38909 break;
38910 default:
38911 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
38912 "%<|%>, %<&&%>, %<||%> or identifier");
38913 goto fail;
38916 if (reduc_code != ERROR_MARK)
38917 cp_lexer_consume_token (parser->lexer);
38919 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
38920 if (reduc_id == error_mark_node)
38921 goto fail;
38923 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38924 goto fail;
38926 /* Types may not be defined in declare reduction type list. */
38927 const char *saved_message;
38928 saved_message = parser->type_definition_forbidden_message;
38929 parser->type_definition_forbidden_message
38930 = G_("types may not be defined in declare reduction type list");
38931 bool saved_colon_corrects_to_scope_p;
38932 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38933 parser->colon_corrects_to_scope_p = false;
38934 bool saved_colon_doesnt_start_class_def_p;
38935 saved_colon_doesnt_start_class_def_p
38936 = parser->colon_doesnt_start_class_def_p;
38937 parser->colon_doesnt_start_class_def_p = true;
38939 while (true)
38941 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38942 type = cp_parser_type_id (parser);
38943 if (type == error_mark_node)
38945 else if (ARITHMETIC_TYPE_P (type)
38946 && (orig_reduc_id == NULL_TREE
38947 || (TREE_CODE (type) != COMPLEX_TYPE
38948 && (id_equal (orig_reduc_id, "min")
38949 || id_equal (orig_reduc_id, "max")))))
38950 error_at (loc, "predeclared arithmetic type %qT in "
38951 "%<#pragma omp declare reduction%>", type);
38952 else if (TREE_CODE (type) == FUNCTION_TYPE
38953 || TREE_CODE (type) == METHOD_TYPE
38954 || TREE_CODE (type) == ARRAY_TYPE)
38955 error_at (loc, "function or array type %qT in "
38956 "%<#pragma omp declare reduction%>", type);
38957 else if (TYPE_REF_P (type))
38958 error_at (loc, "reference type %qT in "
38959 "%<#pragma omp declare reduction%>", type);
38960 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
38961 error_at (loc, "const, volatile or __restrict qualified type %qT in "
38962 "%<#pragma omp declare reduction%>", type);
38963 else
38964 types.safe_push (type);
38966 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38967 cp_lexer_consume_token (parser->lexer);
38968 else
38969 break;
38972 /* Restore the saved message. */
38973 parser->type_definition_forbidden_message = saved_message;
38974 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38975 parser->colon_doesnt_start_class_def_p
38976 = saved_colon_doesnt_start_class_def_p;
38978 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
38979 || types.is_empty ())
38981 fail:
38982 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38983 goto done;
38986 first_token = cp_lexer_peek_token (parser->lexer);
38987 cp = NULL;
38988 errs = errorcount;
38989 FOR_EACH_VEC_ELT (types, i, type)
38991 tree fntype
38992 = build_function_type_list (void_type_node,
38993 cp_build_reference_type (type, false),
38994 NULL_TREE);
38995 tree this_reduc_id = reduc_id;
38996 if (!dependent_type_p (type))
38997 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
38998 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
38999 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
39000 DECL_ARTIFICIAL (fndecl) = 1;
39001 DECL_EXTERNAL (fndecl) = 1;
39002 DECL_DECLARED_INLINE_P (fndecl) = 1;
39003 DECL_IGNORED_P (fndecl) = 1;
39004 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
39005 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
39006 DECL_ATTRIBUTES (fndecl)
39007 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
39008 DECL_ATTRIBUTES (fndecl));
39009 if (processing_template_decl)
39010 fndecl = push_template_decl (fndecl);
39011 bool block_scope = false;
39012 tree block = NULL_TREE;
39013 if (current_function_decl)
39015 block_scope = true;
39016 DECL_CONTEXT (fndecl) = global_namespace;
39017 if (!processing_template_decl)
39018 pushdecl (fndecl);
39020 else if (current_class_type)
39022 if (cp == NULL)
39024 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39025 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39026 cp_lexer_consume_token (parser->lexer);
39027 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39028 goto fail;
39029 cp = cp_token_cache_new (first_token,
39030 cp_lexer_peek_nth_token (parser->lexer,
39031 2));
39033 DECL_STATIC_FUNCTION_P (fndecl) = 1;
39034 finish_member_declaration (fndecl);
39035 DECL_PENDING_INLINE_INFO (fndecl) = cp;
39036 DECL_PENDING_INLINE_P (fndecl) = 1;
39037 vec_safe_push (unparsed_funs_with_definitions, fndecl);
39038 continue;
39040 else
39042 DECL_CONTEXT (fndecl) = current_namespace;
39043 pushdecl (fndecl);
39045 if (!block_scope)
39046 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
39047 else
39048 block = begin_omp_structured_block ();
39049 if (cp)
39051 cp_parser_push_lexer_for_tokens (parser, cp);
39052 parser->lexer->in_pragma = true;
39054 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
39056 if (!block_scope)
39057 finish_function (/*inline_p=*/false);
39058 else
39059 DECL_CONTEXT (fndecl) = current_function_decl;
39060 if (cp)
39061 cp_parser_pop_lexer (parser);
39062 goto fail;
39064 if (cp)
39065 cp_parser_pop_lexer (parser);
39066 if (!block_scope)
39067 finish_function (/*inline_p=*/false);
39068 else
39070 DECL_CONTEXT (fndecl) = current_function_decl;
39071 block = finish_omp_structured_block (block);
39072 if (TREE_CODE (block) == BIND_EXPR)
39073 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
39074 else if (TREE_CODE (block) == STATEMENT_LIST)
39075 DECL_SAVED_TREE (fndecl) = block;
39076 if (processing_template_decl)
39077 add_decl_expr (fndecl);
39079 cp_check_omp_declare_reduction (fndecl);
39080 if (cp == NULL && types.length () > 1)
39081 cp = cp_token_cache_new (first_token,
39082 cp_lexer_peek_nth_token (parser->lexer, 2));
39083 if (errs != errorcount)
39084 break;
39087 cp_parser_require_pragma_eol (parser, pragma_tok);
39089 done:
39090 /* Free any declarators allocated. */
39091 obstack_free (&declarator_obstack, p);
39094 /* OpenMP 4.0
39095 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39096 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39097 initializer-clause[opt] new-line
39098 #pragma omp declare target new-line */
39100 static bool
39101 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
39102 enum pragma_context context)
39104 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39106 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39107 const char *p = IDENTIFIER_POINTER (id);
39109 if (strcmp (p, "simd") == 0)
39111 cp_lexer_consume_token (parser->lexer);
39112 cp_parser_omp_declare_simd (parser, pragma_tok,
39113 context);
39114 return true;
39116 cp_ensure_no_omp_declare_simd (parser);
39117 if (strcmp (p, "reduction") == 0)
39119 cp_lexer_consume_token (parser->lexer);
39120 cp_parser_omp_declare_reduction (parser, pragma_tok,
39121 context);
39122 return false;
39124 if (!flag_openmp) /* flag_openmp_simd */
39126 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39127 return false;
39129 if (strcmp (p, "target") == 0)
39131 cp_lexer_consume_token (parser->lexer);
39132 cp_parser_omp_declare_target (parser, pragma_tok);
39133 return false;
39136 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
39137 "or %<target%>");
39138 cp_parser_require_pragma_eol (parser, pragma_tok);
39139 return false;
39142 /* OpenMP 5.0
39143 #pragma omp requires clauses[optseq] new-line */
39145 static bool
39146 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
39148 bool first = true;
39149 enum omp_requires new_req = (enum omp_requires) 0;
39151 location_t loc = pragma_tok->location;
39152 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39154 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39155 cp_lexer_consume_token (parser->lexer);
39157 first = false;
39159 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39161 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39162 const char *p = IDENTIFIER_POINTER (id);
39163 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
39164 enum omp_requires this_req = (enum omp_requires) 0;
39166 if (!strcmp (p, "unified_address"))
39167 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
39168 else if (!strcmp (p, "unified_shared_memory"))
39169 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
39170 else if (!strcmp (p, "dynamic_allocators"))
39171 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
39172 else if (!strcmp (p, "reverse_offload"))
39173 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
39174 else if (!strcmp (p, "atomic_default_mem_order"))
39176 cp_lexer_consume_token (parser->lexer);
39178 matching_parens parens;
39179 if (parens.require_open (parser))
39181 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39183 id = cp_lexer_peek_token (parser->lexer)->u.value;
39184 p = IDENTIFIER_POINTER (id);
39186 if (!strcmp (p, "seq_cst"))
39187 this_req
39188 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
39189 else if (!strcmp (p, "relaxed"))
39190 this_req
39191 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
39192 else if (!strcmp (p, "acq_rel"))
39193 this_req
39194 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
39196 if (this_req == 0)
39198 error_at (cp_lexer_peek_token (parser->lexer)->location,
39199 "expected %<seq_cst%>, %<relaxed%> or "
39200 "%<acq_rel%>");
39201 if (cp_lexer_nth_token_is (parser->lexer, 2,
39202 CPP_CLOSE_PAREN))
39203 cp_lexer_consume_token (parser->lexer);
39205 else
39206 cp_lexer_consume_token (parser->lexer);
39208 if (!parens.require_close (parser))
39209 cp_parser_skip_to_closing_parenthesis (parser,
39210 /*recovering=*/true,
39211 /*or_comma=*/false,
39212 /*consume_paren=*/
39213 true);
39215 if (this_req == 0)
39217 cp_parser_require_pragma_eol (parser, pragma_tok);
39218 return false;
39221 p = NULL;
39223 else
39225 error_at (cloc, "expected %<unified_address%>, "
39226 "%<unified_shared_memory%>, "
39227 "%<dynamic_allocators%>, "
39228 "%<reverse_offload%> "
39229 "or %<atomic_default_mem_order%> clause");
39230 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39231 return false;
39233 if (p)
39234 sorry_at (cloc, "%qs clause on %<requires%> directive not "
39235 "supported yet", p);
39236 if (p)
39237 cp_lexer_consume_token (parser->lexer);
39238 if (this_req)
39240 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39242 if ((this_req & new_req) != 0)
39243 error_at (cloc, "too many %qs clauses", p);
39244 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
39245 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
39246 error_at (cloc, "%qs clause used lexically after first "
39247 "target construct or offloading API", p);
39249 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39251 error_at (cloc, "too many %qs clauses",
39252 "atomic_default_mem_order");
39253 this_req = (enum omp_requires) 0;
39255 else if ((omp_requires_mask
39256 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39258 error_at (cloc, "more than one %<atomic_default_mem_order%>"
39259 " clause in a single compilation unit");
39260 this_req
39261 = (enum omp_requires)
39262 (omp_requires_mask
39263 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
39265 else if ((omp_requires_mask
39266 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
39267 error_at (cloc, "%<atomic_default_mem_order%> clause used "
39268 "lexically after first %<atomic%> construct "
39269 "without memory order clause");
39270 new_req = (enum omp_requires) (new_req | this_req);
39271 omp_requires_mask
39272 = (enum omp_requires) (omp_requires_mask | this_req);
39273 continue;
39276 break;
39278 cp_parser_require_pragma_eol (parser, pragma_tok);
39280 if (new_req == 0)
39281 error_at (loc, "%<pragma omp requires%> requires at least one clause");
39282 return false;
39286 /* OpenMP 4.5:
39287 #pragma omp taskloop taskloop-clause[optseq] new-line
39288 for-loop
39290 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39291 for-loop */
39293 #define OMP_TASKLOOP_CLAUSE_MASK \
39294 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39307 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39311 static tree
39312 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
39313 char *p_name, omp_clause_mask mask, tree *cclauses,
39314 bool *if_p)
39316 tree clauses, sb, ret;
39317 unsigned int save;
39318 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39320 strcat (p_name, " taskloop");
39321 mask |= OMP_TASKLOOP_CLAUSE_MASK;
39322 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39323 clause. */
39324 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
39325 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
39327 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39329 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39330 const char *p = IDENTIFIER_POINTER (id);
39332 if (strcmp (p, "simd") == 0)
39334 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39335 if (cclauses == NULL)
39336 cclauses = cclauses_buf;
39338 cp_lexer_consume_token (parser->lexer);
39339 if (!flag_openmp) /* flag_openmp_simd */
39340 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39341 cclauses, if_p);
39342 sb = begin_omp_structured_block ();
39343 save = cp_parser_begin_omp_structured_block (parser);
39344 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39345 cclauses, if_p);
39346 cp_parser_end_omp_structured_block (parser, save);
39347 tree body = finish_omp_structured_block (sb);
39348 if (ret == NULL)
39349 return ret;
39350 ret = make_node (OMP_TASKLOOP);
39351 TREE_TYPE (ret) = void_type_node;
39352 OMP_FOR_BODY (ret) = body;
39353 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39354 SET_EXPR_LOCATION (ret, loc);
39355 add_stmt (ret);
39356 return ret;
39359 if (!flag_openmp) /* flag_openmp_simd */
39361 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39362 return NULL_TREE;
39365 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39366 cclauses == NULL);
39367 if (cclauses)
39369 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
39370 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39373 keep_next_level (true);
39374 sb = begin_omp_structured_block ();
39375 save = cp_parser_begin_omp_structured_block (parser);
39377 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
39378 if_p);
39380 cp_parser_end_omp_structured_block (parser, save);
39381 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39383 return ret;
39387 /* OpenACC 2.0:
39388 # pragma acc routine oacc-routine-clause[optseq] new-line
39389 function-definition
39391 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39394 #define OACC_ROUTINE_CLAUSE_MASK \
39395 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39401 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39402 component, which must resolve to a declared namespace-scope
39403 function. The clauses are either processed directly (for a named
39404 function), or defered until the immediatley following declaration
39405 is parsed. */
39407 static void
39408 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
39409 enum pragma_context context)
39411 gcc_checking_assert (context == pragma_external);
39412 /* The checking for "another pragma following this one" in the "no optional
39413 '( name )'" case makes sure that we dont re-enter. */
39414 gcc_checking_assert (parser->oacc_routine == NULL);
39416 cp_oacc_routine_data data;
39417 data.error_seen = false;
39418 data.fndecl_seen = false;
39419 data.tokens = vNULL;
39420 data.clauses = NULL_TREE;
39421 data.loc = pragma_tok->location;
39422 /* It is safe to take the address of a local variable; it will only be
39423 used while this scope is live. */
39424 parser->oacc_routine = &data;
39426 /* Look for optional '( name )'. */
39427 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39429 matching_parens parens;
39430 parens.consume_open (parser); /* '(' */
39432 /* We parse the name as an id-expression. If it resolves to
39433 anything other than a non-overloaded function at namespace
39434 scope, it's an error. */
39435 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
39436 tree name = cp_parser_id_expression (parser,
39437 /*template_keyword_p=*/false,
39438 /*check_dependency_p=*/false,
39439 /*template_p=*/NULL,
39440 /*declarator_p=*/false,
39441 /*optional_p=*/false);
39442 tree decl = (identifier_p (name)
39443 ? cp_parser_lookup_name_simple (parser, name, name_loc)
39444 : name);
39445 if (name != error_mark_node && decl == error_mark_node)
39446 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
39448 if (decl == error_mark_node
39449 || !parens.require_close (parser))
39451 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39452 parser->oacc_routine = NULL;
39453 return;
39456 data.clauses
39457 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39458 "#pragma acc routine",
39459 cp_lexer_peek_token (parser->lexer));
39461 if (decl && is_overloaded_fn (decl)
39462 && (TREE_CODE (decl) != FUNCTION_DECL
39463 || DECL_FUNCTION_TEMPLATE_P (decl)))
39465 error_at (name_loc,
39466 "%<#pragma acc routine%> names a set of overloads");
39467 parser->oacc_routine = NULL;
39468 return;
39471 /* Perhaps we should use the same rule as declarations in different
39472 namespaces? */
39473 if (!DECL_NAMESPACE_SCOPE_P (decl))
39475 error_at (name_loc,
39476 "%qD does not refer to a namespace scope function", decl);
39477 parser->oacc_routine = NULL;
39478 return;
39481 if (TREE_CODE (decl) != FUNCTION_DECL)
39483 error_at (name_loc, "%qD does not refer to a function", decl);
39484 parser->oacc_routine = NULL;
39485 return;
39488 cp_finalize_oacc_routine (parser, decl, false);
39489 parser->oacc_routine = NULL;
39491 else /* No optional '( name )'. */
39493 /* Store away all pragma tokens. */
39494 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39495 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39496 cp_lexer_consume_token (parser->lexer);
39497 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39498 parser->oacc_routine->error_seen = true;
39499 cp_parser_require_pragma_eol (parser, pragma_tok);
39500 struct cp_token_cache *cp
39501 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39502 parser->oacc_routine->tokens.safe_push (cp);
39504 /* Emit a helpful diagnostic if there's another pragma following this
39505 one. */
39506 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39508 cp_ensure_no_oacc_routine (parser);
39509 data.tokens.release ();
39510 /* ..., and then just keep going. */
39511 return;
39514 /* We only have to consider the pragma_external case here. */
39515 cp_parser_declaration (parser);
39516 if (parser->oacc_routine
39517 && !parser->oacc_routine->fndecl_seen)
39518 cp_ensure_no_oacc_routine (parser);
39519 else
39520 parser->oacc_routine = NULL;
39521 data.tokens.release ();
39525 /* Finalize #pragma acc routine clauses after direct declarator has
39526 been parsed. */
39528 static tree
39529 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
39531 struct cp_token_cache *ce;
39532 cp_oacc_routine_data *data = parser->oacc_routine;
39534 if (!data->error_seen && data->fndecl_seen)
39536 error_at (data->loc,
39537 "%<#pragma acc routine%> not immediately followed by "
39538 "a single function declaration or definition");
39539 data->error_seen = true;
39541 if (data->error_seen)
39542 return attrs;
39544 gcc_checking_assert (data->tokens.length () == 1);
39545 ce = data->tokens[0];
39547 cp_parser_push_lexer_for_tokens (parser, ce);
39548 parser->lexer->in_pragma = true;
39549 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39551 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39552 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
39553 parser->oacc_routine->clauses
39554 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39555 "#pragma acc routine", pragma_tok);
39556 cp_parser_pop_lexer (parser);
39557 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
39558 fndecl_seen. */
39560 return attrs;
39563 /* Apply any saved OpenACC routine clauses to a just-parsed
39564 declaration. */
39566 static void
39567 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
39569 if (__builtin_expect (parser->oacc_routine != NULL, 0))
39571 /* Keep going if we're in error reporting mode. */
39572 if (parser->oacc_routine->error_seen
39573 || fndecl == error_mark_node)
39574 return;
39576 if (parser->oacc_routine->fndecl_seen)
39578 error_at (parser->oacc_routine->loc,
39579 "%<#pragma acc routine%> not immediately followed by"
39580 " a single function declaration or definition");
39581 parser->oacc_routine = NULL;
39582 return;
39584 if (TREE_CODE (fndecl) != FUNCTION_DECL)
39586 cp_ensure_no_oacc_routine (parser);
39587 return;
39590 if (oacc_get_fn_attrib (fndecl))
39592 error_at (parser->oacc_routine->loc,
39593 "%<#pragma acc routine%> already applied to %qD", fndecl);
39594 parser->oacc_routine = NULL;
39595 return;
39598 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
39600 error_at (parser->oacc_routine->loc,
39601 TREE_USED (fndecl)
39602 ? G_("%<#pragma acc routine%> must be applied before use")
39603 : G_("%<#pragma acc routine%> must be applied before "
39604 "definition"));
39605 parser->oacc_routine = NULL;
39606 return;
39609 /* Process the routine's dimension clauses. */
39610 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
39611 oacc_replace_fn_attrib (fndecl, dims);
39613 /* Add an "omp declare target" attribute. */
39614 DECL_ATTRIBUTES (fndecl)
39615 = tree_cons (get_identifier ("omp declare target"),
39616 NULL_TREE, DECL_ATTRIBUTES (fndecl));
39618 /* Don't unset parser->oacc_routine here: we may still need it to
39619 diagnose wrong usage. But, remember that we've used this "#pragma acc
39620 routine". */
39621 parser->oacc_routine->fndecl_seen = true;
39625 /* Main entry point to OpenMP statement pragmas. */
39627 static void
39628 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39630 tree stmt;
39631 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
39632 omp_clause_mask mask (0);
39634 switch (cp_parser_pragma_kind (pragma_tok))
39636 case PRAGMA_OACC_ATOMIC:
39637 cp_parser_omp_atomic (parser, pragma_tok);
39638 return;
39639 case PRAGMA_OACC_CACHE:
39640 stmt = cp_parser_oacc_cache (parser, pragma_tok);
39641 break;
39642 case PRAGMA_OACC_DATA:
39643 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
39644 break;
39645 case PRAGMA_OACC_ENTER_DATA:
39646 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
39647 break;
39648 case PRAGMA_OACC_EXIT_DATA:
39649 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
39650 break;
39651 case PRAGMA_OACC_HOST_DATA:
39652 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
39653 break;
39654 case PRAGMA_OACC_KERNELS:
39655 case PRAGMA_OACC_PARALLEL:
39656 strcpy (p_name, "#pragma acc");
39657 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
39658 if_p);
39659 break;
39660 case PRAGMA_OACC_LOOP:
39661 strcpy (p_name, "#pragma acc");
39662 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
39663 if_p);
39664 break;
39665 case PRAGMA_OACC_UPDATE:
39666 stmt = cp_parser_oacc_update (parser, pragma_tok);
39667 break;
39668 case PRAGMA_OACC_WAIT:
39669 stmt = cp_parser_oacc_wait (parser, pragma_tok);
39670 break;
39671 case PRAGMA_OMP_ATOMIC:
39672 cp_parser_omp_atomic (parser, pragma_tok);
39673 return;
39674 case PRAGMA_OMP_CRITICAL:
39675 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
39676 break;
39677 case PRAGMA_OMP_DISTRIBUTE:
39678 strcpy (p_name, "#pragma omp");
39679 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
39680 if_p);
39681 break;
39682 case PRAGMA_OMP_FOR:
39683 strcpy (p_name, "#pragma omp");
39684 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
39685 if_p);
39686 break;
39687 case PRAGMA_OMP_MASTER:
39688 strcpy (p_name, "#pragma omp");
39689 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
39690 if_p);
39691 break;
39692 case PRAGMA_OMP_PARALLEL:
39693 strcpy (p_name, "#pragma omp");
39694 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
39695 if_p);
39696 break;
39697 case PRAGMA_OMP_SECTIONS:
39698 strcpy (p_name, "#pragma omp");
39699 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
39700 break;
39701 case PRAGMA_OMP_SIMD:
39702 strcpy (p_name, "#pragma omp");
39703 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
39704 if_p);
39705 break;
39706 case PRAGMA_OMP_SINGLE:
39707 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
39708 break;
39709 case PRAGMA_OMP_TASK:
39710 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
39711 break;
39712 case PRAGMA_OMP_TASKGROUP:
39713 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
39714 break;
39715 case PRAGMA_OMP_TASKLOOP:
39716 strcpy (p_name, "#pragma omp");
39717 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
39718 if_p);
39719 break;
39720 case PRAGMA_OMP_TEAMS:
39721 strcpy (p_name, "#pragma omp");
39722 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
39723 if_p);
39724 break;
39725 default:
39726 gcc_unreachable ();
39729 protected_set_expr_location (stmt, pragma_tok->location);
39732 /* Transactional Memory parsing routines. */
39734 /* Parse a transaction attribute.
39736 txn-attribute:
39737 attribute
39738 [ [ identifier ] ]
39740 We use this instead of cp_parser_attributes_opt for transactions to avoid
39741 the pedwarn in C++98 mode. */
39743 static tree
39744 cp_parser_txn_attribute_opt (cp_parser *parser)
39746 cp_token *token;
39747 tree attr_name, attr = NULL;
39749 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
39750 return cp_parser_attributes_opt (parser);
39752 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
39753 return NULL_TREE;
39754 cp_lexer_consume_token (parser->lexer);
39755 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
39756 goto error1;
39758 token = cp_lexer_peek_token (parser->lexer);
39759 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
39761 token = cp_lexer_consume_token (parser->lexer);
39763 attr_name = (token->type == CPP_KEYWORD
39764 /* For keywords, use the canonical spelling,
39765 not the parsed identifier. */
39766 ? ridpointers[(int) token->keyword]
39767 : token->u.value);
39768 attr = build_tree_list (attr_name, NULL_TREE);
39770 else
39771 cp_parser_error (parser, "expected identifier");
39773 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
39774 error1:
39775 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
39776 return attr;
39779 /* Parse a __transaction_atomic or __transaction_relaxed statement.
39781 transaction-statement:
39782 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
39783 compound-statement
39784 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
39787 static tree
39788 cp_parser_transaction (cp_parser *parser, cp_token *token)
39790 unsigned char old_in = parser->in_transaction;
39791 unsigned char this_in = 1, new_in;
39792 enum rid keyword = token->keyword;
39793 tree stmt, attrs, noex;
39795 cp_lexer_consume_token (parser->lexer);
39797 if (keyword == RID_TRANSACTION_RELAXED
39798 || keyword == RID_SYNCHRONIZED)
39799 this_in |= TM_STMT_ATTR_RELAXED;
39800 else
39802 attrs = cp_parser_txn_attribute_opt (parser);
39803 if (attrs)
39804 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
39807 /* Parse a noexcept specification. */
39808 if (keyword == RID_ATOMIC_NOEXCEPT)
39809 noex = boolean_true_node;
39810 else if (keyword == RID_ATOMIC_CANCEL)
39812 /* cancel-and-throw is unimplemented. */
39813 sorry ("atomic_cancel");
39814 noex = NULL_TREE;
39816 else
39817 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
39819 /* Keep track if we're in the lexical scope of an outer transaction. */
39820 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
39822 stmt = begin_transaction_stmt (token->location, NULL, this_in);
39824 parser->in_transaction = new_in;
39825 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
39826 parser->in_transaction = old_in;
39828 finish_transaction_stmt (stmt, NULL, this_in, noex);
39830 return stmt;
39833 /* Parse a __transaction_atomic or __transaction_relaxed expression.
39835 transaction-expression:
39836 __transaction_atomic txn-noexcept-spec[opt] ( expression )
39837 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
39840 static tree
39841 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
39843 unsigned char old_in = parser->in_transaction;
39844 unsigned char this_in = 1;
39845 cp_token *token;
39846 tree expr, noex;
39847 bool noex_expr;
39848 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39850 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
39851 || keyword == RID_TRANSACTION_RELAXED);
39853 if (!flag_tm)
39854 error_at (loc,
39855 keyword == RID_TRANSACTION_RELAXED
39856 ? G_("%<__transaction_relaxed%> without transactional memory "
39857 "support enabled")
39858 : G_("%<__transaction_atomic%> without transactional memory "
39859 "support enabled"));
39861 token = cp_parser_require_keyword (parser, keyword,
39862 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
39863 : RT_TRANSACTION_RELAXED));
39864 gcc_assert (token != NULL);
39866 if (keyword == RID_TRANSACTION_RELAXED)
39867 this_in |= TM_STMT_ATTR_RELAXED;
39869 /* Set this early. This might mean that we allow transaction_cancel in
39870 an expression that we find out later actually has to be a constexpr.
39871 However, we expect that cxx_constant_value will be able to deal with
39872 this; also, if the noexcept has no constexpr, then what we parse next
39873 really is a transaction's body. */
39874 parser->in_transaction = this_in;
39876 /* Parse a noexcept specification. */
39877 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
39878 true);
39880 if (!noex || !noex_expr
39881 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39883 matching_parens parens;
39884 parens.require_open (parser);
39886 expr = cp_parser_expression (parser);
39887 expr = finish_parenthesized_expr (expr);
39889 parens.require_close (parser);
39891 else
39893 /* The only expression that is available got parsed for the noexcept
39894 already. noexcept is true then. */
39895 expr = noex;
39896 noex = boolean_true_node;
39899 expr = build_transaction_expr (token->location, expr, this_in, noex);
39900 parser->in_transaction = old_in;
39902 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
39903 return error_mark_node;
39905 return (flag_tm ? expr : error_mark_node);
39908 /* Parse a function-transaction-block.
39910 function-transaction-block:
39911 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
39912 function-body
39913 __transaction_atomic txn-attribute[opt] function-try-block
39914 __transaction_relaxed ctor-initializer[opt] function-body
39915 __transaction_relaxed function-try-block
39918 static void
39919 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
39921 unsigned char old_in = parser->in_transaction;
39922 unsigned char new_in = 1;
39923 tree compound_stmt, stmt, attrs;
39924 cp_token *token;
39926 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
39927 || keyword == RID_TRANSACTION_RELAXED);
39928 token = cp_parser_require_keyword (parser, keyword,
39929 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
39930 : RT_TRANSACTION_RELAXED));
39931 gcc_assert (token != NULL);
39933 if (keyword == RID_TRANSACTION_RELAXED)
39934 new_in |= TM_STMT_ATTR_RELAXED;
39935 else
39937 attrs = cp_parser_txn_attribute_opt (parser);
39938 if (attrs)
39939 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
39942 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
39944 parser->in_transaction = new_in;
39946 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
39947 cp_parser_function_try_block (parser);
39948 else
39949 cp_parser_ctor_initializer_opt_and_function_body
39950 (parser, /*in_function_try_block=*/false);
39952 parser->in_transaction = old_in;
39954 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
39957 /* Parse a __transaction_cancel statement.
39959 cancel-statement:
39960 __transaction_cancel txn-attribute[opt] ;
39961 __transaction_cancel txn-attribute[opt] throw-expression ;
39963 ??? Cancel and throw is not yet implemented. */
39965 static tree
39966 cp_parser_transaction_cancel (cp_parser *parser)
39968 cp_token *token;
39969 bool is_outer = false;
39970 tree stmt, attrs;
39972 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
39973 RT_TRANSACTION_CANCEL);
39974 gcc_assert (token != NULL);
39976 attrs = cp_parser_txn_attribute_opt (parser);
39977 if (attrs)
39978 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
39980 /* ??? Parse cancel-and-throw here. */
39982 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
39984 if (!flag_tm)
39986 error_at (token->location, "%<__transaction_cancel%> without "
39987 "transactional memory support enabled");
39988 return error_mark_node;
39990 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
39992 error_at (token->location, "%<__transaction_cancel%> within a "
39993 "%<__transaction_relaxed%>");
39994 return error_mark_node;
39996 else if (is_outer)
39998 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
39999 && !is_tm_may_cancel_outer (current_function_decl))
40001 error_at (token->location, "outer %<__transaction_cancel%> not "
40002 "within outer %<__transaction_atomic%>");
40003 error_at (token->location,
40004 " or a %<transaction_may_cancel_outer%> function");
40005 return error_mark_node;
40008 else if (parser->in_transaction == 0)
40010 error_at (token->location, "%<__transaction_cancel%> not within "
40011 "%<__transaction_atomic%>");
40012 return error_mark_node;
40015 stmt = build_tm_abort_call (token->location, is_outer);
40016 add_stmt (stmt);
40018 return stmt;
40021 /* The parser. */
40023 static GTY (()) cp_parser *the_parser;
40026 /* Special handling for the first token or line in the file. The first
40027 thing in the file might be #pragma GCC pch_preprocess, which loads a
40028 PCH file, which is a GC collection point. So we need to handle this
40029 first pragma without benefit of an existing lexer structure.
40031 Always returns one token to the caller in *FIRST_TOKEN. This is
40032 either the true first token of the file, or the first token after
40033 the initial pragma. */
40035 static void
40036 cp_parser_initial_pragma (cp_token *first_token)
40038 tree name = NULL;
40040 cp_lexer_get_preprocessor_token (NULL, first_token);
40041 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
40042 return;
40044 cp_lexer_get_preprocessor_token (NULL, first_token);
40045 if (first_token->type == CPP_STRING)
40047 name = first_token->u.value;
40049 cp_lexer_get_preprocessor_token (NULL, first_token);
40050 if (first_token->type != CPP_PRAGMA_EOL)
40051 error_at (first_token->location,
40052 "junk at end of %<#pragma GCC pch_preprocess%>");
40054 else
40055 error_at (first_token->location, "expected string literal");
40057 /* Skip to the end of the pragma. */
40058 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
40059 cp_lexer_get_preprocessor_token (NULL, first_token);
40061 /* Now actually load the PCH file. */
40062 if (name)
40063 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
40065 /* Read one more token to return to our caller. We have to do this
40066 after reading the PCH file in, since its pointers have to be
40067 live. */
40068 cp_lexer_get_preprocessor_token (NULL, first_token);
40071 /* Parse a pragma GCC ivdep. */
40073 static bool
40074 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
40076 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40077 return true;
40080 /* Parse a pragma GCC unroll. */
40082 static unsigned short
40083 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
40085 location_t location = cp_lexer_peek_token (parser->lexer)->location;
40086 tree expr = cp_parser_constant_expression (parser);
40087 unsigned short unroll;
40088 expr = maybe_constant_value (expr);
40089 HOST_WIDE_INT lunroll = 0;
40090 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
40091 || TREE_CODE (expr) != INTEGER_CST
40092 || (lunroll = tree_to_shwi (expr)) < 0
40093 || lunroll >= USHRT_MAX)
40095 error_at (location, "%<#pragma GCC unroll%> requires an"
40096 " assignment-expression that evaluates to a non-negative"
40097 " integral constant less than %u", USHRT_MAX);
40098 unroll = 0;
40100 else
40102 unroll = (unsigned short)lunroll;
40103 if (unroll == 0)
40104 unroll = 1;
40106 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40107 return unroll;
40110 /* Normal parsing of a pragma token. Here we can (and must) use the
40111 regular lexer. */
40113 static bool
40114 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
40116 cp_token *pragma_tok;
40117 unsigned int id;
40118 tree stmt;
40119 bool ret;
40121 pragma_tok = cp_lexer_consume_token (parser->lexer);
40122 gcc_assert (pragma_tok->type == CPP_PRAGMA);
40123 parser->lexer->in_pragma = true;
40125 id = cp_parser_pragma_kind (pragma_tok);
40126 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
40127 cp_ensure_no_omp_declare_simd (parser);
40128 switch (id)
40130 case PRAGMA_GCC_PCH_PREPROCESS:
40131 error_at (pragma_tok->location,
40132 "%<#pragma GCC pch_preprocess%> must be first");
40133 break;
40135 case PRAGMA_OMP_BARRIER:
40136 switch (context)
40138 case pragma_compound:
40139 cp_parser_omp_barrier (parser, pragma_tok);
40140 return false;
40141 case pragma_stmt:
40142 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40143 "used in compound statements", "omp barrier");
40144 break;
40145 default:
40146 goto bad_stmt;
40148 break;
40150 case PRAGMA_OMP_DEPOBJ:
40151 switch (context)
40153 case pragma_compound:
40154 cp_parser_omp_depobj (parser, pragma_tok);
40155 return false;
40156 case pragma_stmt:
40157 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40158 "used in compound statements", "omp depobj");
40159 break;
40160 default:
40161 goto bad_stmt;
40163 break;
40165 case PRAGMA_OMP_FLUSH:
40166 switch (context)
40168 case pragma_compound:
40169 cp_parser_omp_flush (parser, pragma_tok);
40170 return false;
40171 case pragma_stmt:
40172 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40173 "used in compound statements", "omp flush");
40174 break;
40175 default:
40176 goto bad_stmt;
40178 break;
40180 case PRAGMA_OMP_TASKWAIT:
40181 switch (context)
40183 case pragma_compound:
40184 cp_parser_omp_taskwait (parser, pragma_tok);
40185 return false;
40186 case pragma_stmt:
40187 error_at (pragma_tok->location,
40188 "%<#pragma %s%> may only be used in compound statements",
40189 "omp taskwait");
40190 break;
40191 default:
40192 goto bad_stmt;
40194 break;
40196 case PRAGMA_OMP_TASKYIELD:
40197 switch (context)
40199 case pragma_compound:
40200 cp_parser_omp_taskyield (parser, pragma_tok);
40201 return false;
40202 case pragma_stmt:
40203 error_at (pragma_tok->location,
40204 "%<#pragma %s%> may only be used in compound statements",
40205 "omp taskyield");
40206 break;
40207 default:
40208 goto bad_stmt;
40210 break;
40212 case PRAGMA_OMP_CANCEL:
40213 switch (context)
40215 case pragma_compound:
40216 cp_parser_omp_cancel (parser, pragma_tok);
40217 return false;
40218 case pragma_stmt:
40219 error_at (pragma_tok->location,
40220 "%<#pragma %s%> may only be used in compound statements",
40221 "omp cancel");
40222 break;
40223 default:
40224 goto bad_stmt;
40226 break;
40228 case PRAGMA_OMP_CANCELLATION_POINT:
40229 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
40230 return false;
40232 case PRAGMA_OMP_THREADPRIVATE:
40233 cp_parser_omp_threadprivate (parser, pragma_tok);
40234 return false;
40236 case PRAGMA_OMP_DECLARE:
40237 return cp_parser_omp_declare (parser, pragma_tok, context);
40239 case PRAGMA_OACC_DECLARE:
40240 cp_parser_oacc_declare (parser, pragma_tok);
40241 return false;
40243 case PRAGMA_OACC_ENTER_DATA:
40244 if (context == pragma_stmt)
40246 error_at (pragma_tok->location,
40247 "%<#pragma %s%> may only be used in compound statements",
40248 "acc enter data");
40249 break;
40251 else if (context != pragma_compound)
40252 goto bad_stmt;
40253 cp_parser_omp_construct (parser, pragma_tok, if_p);
40254 return true;
40256 case PRAGMA_OACC_EXIT_DATA:
40257 if (context == pragma_stmt)
40259 error_at (pragma_tok->location,
40260 "%<#pragma %s%> may only be used in compound statements",
40261 "acc exit data");
40262 break;
40264 else if (context != pragma_compound)
40265 goto bad_stmt;
40266 cp_parser_omp_construct (parser, pragma_tok, if_p);
40267 return true;
40269 case PRAGMA_OACC_ROUTINE:
40270 if (context != pragma_external)
40272 error_at (pragma_tok->location,
40273 "%<#pragma acc routine%> must be at file scope");
40274 break;
40276 cp_parser_oacc_routine (parser, pragma_tok, context);
40277 return false;
40279 case PRAGMA_OACC_UPDATE:
40280 if (context == pragma_stmt)
40282 error_at (pragma_tok->location,
40283 "%<#pragma %s%> may only be used in compound statements",
40284 "acc update");
40285 break;
40287 else if (context != pragma_compound)
40288 goto bad_stmt;
40289 cp_parser_omp_construct (parser, pragma_tok, if_p);
40290 return true;
40292 case PRAGMA_OACC_WAIT:
40293 if (context == pragma_stmt)
40295 error_at (pragma_tok->location,
40296 "%<#pragma %s%> may only be used in compound statements",
40297 "acc wait");
40298 break;
40300 else if (context != pragma_compound)
40301 goto bad_stmt;
40302 cp_parser_omp_construct (parser, pragma_tok, if_p);
40303 return true;
40305 case PRAGMA_OACC_ATOMIC:
40306 case PRAGMA_OACC_CACHE:
40307 case PRAGMA_OACC_DATA:
40308 case PRAGMA_OACC_HOST_DATA:
40309 case PRAGMA_OACC_KERNELS:
40310 case PRAGMA_OACC_PARALLEL:
40311 case PRAGMA_OACC_LOOP:
40312 case PRAGMA_OMP_ATOMIC:
40313 case PRAGMA_OMP_CRITICAL:
40314 case PRAGMA_OMP_DISTRIBUTE:
40315 case PRAGMA_OMP_FOR:
40316 case PRAGMA_OMP_MASTER:
40317 case PRAGMA_OMP_PARALLEL:
40318 case PRAGMA_OMP_SECTIONS:
40319 case PRAGMA_OMP_SIMD:
40320 case PRAGMA_OMP_SINGLE:
40321 case PRAGMA_OMP_TASK:
40322 case PRAGMA_OMP_TASKGROUP:
40323 case PRAGMA_OMP_TASKLOOP:
40324 case PRAGMA_OMP_TEAMS:
40325 if (context != pragma_stmt && context != pragma_compound)
40326 goto bad_stmt;
40327 stmt = push_omp_privatization_clauses (false);
40328 cp_parser_omp_construct (parser, pragma_tok, if_p);
40329 pop_omp_privatization_clauses (stmt);
40330 return true;
40332 case PRAGMA_OMP_REQUIRES:
40333 return cp_parser_omp_requires (parser, pragma_tok);
40335 case PRAGMA_OMP_ORDERED:
40336 if (context != pragma_stmt && context != pragma_compound)
40337 goto bad_stmt;
40338 stmt = push_omp_privatization_clauses (false);
40339 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
40340 pop_omp_privatization_clauses (stmt);
40341 return ret;
40343 case PRAGMA_OMP_TARGET:
40344 if (context != pragma_stmt && context != pragma_compound)
40345 goto bad_stmt;
40346 stmt = push_omp_privatization_clauses (false);
40347 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
40348 pop_omp_privatization_clauses (stmt);
40349 return ret;
40351 case PRAGMA_OMP_END_DECLARE_TARGET:
40352 cp_parser_omp_end_declare_target (parser, pragma_tok);
40353 return false;
40355 case PRAGMA_OMP_SECTION:
40356 error_at (pragma_tok->location,
40357 "%<#pragma omp section%> may only be used in "
40358 "%<#pragma omp sections%> construct");
40359 break;
40361 case PRAGMA_IVDEP:
40363 if (context == pragma_external)
40365 error_at (pragma_tok->location,
40366 "%<#pragma GCC ivdep%> must be inside a function");
40367 break;
40369 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
40370 unsigned short unroll;
40371 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40372 if (tok->type == CPP_PRAGMA
40373 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
40375 tok = cp_lexer_consume_token (parser->lexer);
40376 unroll = cp_parser_pragma_unroll (parser, tok);
40377 tok = cp_lexer_peek_token (the_parser->lexer);
40379 else
40380 unroll = 0;
40381 if (tok->type != CPP_KEYWORD
40382 || (tok->keyword != RID_FOR
40383 && tok->keyword != RID_WHILE
40384 && tok->keyword != RID_DO))
40386 cp_parser_error (parser, "for, while or do statement expected");
40387 return false;
40389 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40390 return true;
40393 case PRAGMA_UNROLL:
40395 if (context == pragma_external)
40397 error_at (pragma_tok->location,
40398 "%<#pragma GCC unroll%> must be inside a function");
40399 break;
40401 const unsigned short unroll
40402 = cp_parser_pragma_unroll (parser, pragma_tok);
40403 bool ivdep;
40404 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40405 if (tok->type == CPP_PRAGMA
40406 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
40408 tok = cp_lexer_consume_token (parser->lexer);
40409 ivdep = cp_parser_pragma_ivdep (parser, tok);
40410 tok = cp_lexer_peek_token (the_parser->lexer);
40412 else
40413 ivdep = false;
40414 if (tok->type != CPP_KEYWORD
40415 || (tok->keyword != RID_FOR
40416 && tok->keyword != RID_WHILE
40417 && tok->keyword != RID_DO))
40419 cp_parser_error (parser, "for, while or do statement expected");
40420 return false;
40422 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40423 return true;
40426 default:
40427 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
40428 c_invoke_pragma_handler (id);
40429 break;
40431 bad_stmt:
40432 cp_parser_error (parser, "expected declaration specifiers");
40433 break;
40436 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40437 return false;
40440 /* The interface the pragma parsers have to the lexer. */
40442 enum cpp_ttype
40443 pragma_lex (tree *value, location_t *loc)
40445 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40446 enum cpp_ttype ret = tok->type;
40448 *value = tok->u.value;
40449 if (loc)
40450 *loc = tok->location;
40452 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
40453 ret = CPP_EOF;
40454 else if (ret == CPP_STRING)
40455 *value = cp_parser_string_literal (the_parser, false, false);
40456 else
40458 if (ret == CPP_KEYWORD)
40459 ret = CPP_NAME;
40460 cp_lexer_consume_token (the_parser->lexer);
40463 return ret;
40467 /* External interface. */
40469 /* Parse one entire translation unit. */
40471 void
40472 c_parse_file (void)
40474 static bool already_called = false;
40476 if (already_called)
40477 fatal_error (input_location,
40478 "inter-module optimizations not implemented for C++");
40479 already_called = true;
40481 the_parser = cp_parser_new ();
40482 push_deferring_access_checks (flag_access_control
40483 ? dk_no_deferred : dk_no_check);
40484 cp_parser_translation_unit (the_parser);
40485 the_parser = NULL;
40487 finish_translation_unit ();
40490 /* Create an identifier for a generic parameter type (a synthesized
40491 template parameter implied by `auto' or a concept identifier). */
40493 static GTY(()) int generic_parm_count;
40494 static tree
40495 make_generic_type_name ()
40497 char buf[32];
40498 sprintf (buf, "auto:%d", ++generic_parm_count);
40499 return get_identifier (buf);
40502 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
40503 (creating a new template parameter list if necessary). Returns the newly
40504 created template type parm. */
40506 static tree
40507 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
40509 gcc_assert (current_binding_level->kind == sk_function_parms);
40511 /* Before committing to modifying any scope, if we're in an
40512 implicit template scope, and we're trying to synthesize a
40513 constrained parameter, try to find a previous parameter with
40514 the same name. This is the same-type rule for abbreviated
40515 function templates.
40517 NOTE: We can generate implicit parameters when tentatively
40518 parsing a nested name specifier, only to reject that parse
40519 later. However, matching the same template-id as part of a
40520 direct-declarator should generate an identical template
40521 parameter, so this rule will merge them. */
40522 if (parser->implicit_template_scope && constr)
40524 tree t = parser->implicit_template_parms;
40525 while (t)
40527 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
40529 tree d = TREE_VALUE (t);
40530 if (TREE_CODE (d) == PARM_DECL)
40531 /* Return the TEMPLATE_PARM_INDEX. */
40532 d = DECL_INITIAL (d);
40533 return d;
40535 t = TREE_CHAIN (t);
40539 /* We are either continuing a function template that already contains implicit
40540 template parameters, creating a new fully-implicit function template, or
40541 extending an existing explicit function template with implicit template
40542 parameters. */
40544 cp_binding_level *const entry_scope = current_binding_level;
40546 bool become_template = false;
40547 cp_binding_level *parent_scope = 0;
40549 if (parser->implicit_template_scope)
40551 gcc_assert (parser->implicit_template_parms);
40553 current_binding_level = parser->implicit_template_scope;
40555 else
40557 /* Roll back to the existing template parameter scope (in the case of
40558 extending an explicit function template) or introduce a new template
40559 parameter scope ahead of the function parameter scope (or class scope
40560 in the case of out-of-line member definitions). The function scope is
40561 added back after template parameter synthesis below. */
40563 cp_binding_level *scope = entry_scope;
40565 while (scope->kind == sk_function_parms)
40567 parent_scope = scope;
40568 scope = scope->level_chain;
40570 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
40572 /* If not defining a class, then any class scope is a scope level in
40573 an out-of-line member definition. In this case simply wind back
40574 beyond the first such scope to inject the template parameter list.
40575 Otherwise wind back to the class being defined. The latter can
40576 occur in class member friend declarations such as:
40578 class A {
40579 void foo (auto);
40581 class B {
40582 friend void A::foo (auto);
40585 The template parameter list synthesized for the friend declaration
40586 must be injected in the scope of 'B'. This can also occur in
40587 erroneous cases such as:
40589 struct A {
40590 struct B {
40591 void foo (auto);
40593 void B::foo (auto) {}
40596 Here the attempted definition of 'B::foo' within 'A' is ill-formed
40597 but, nevertheless, the template parameter list synthesized for the
40598 declarator should be injected into the scope of 'A' as if the
40599 ill-formed template was specified explicitly. */
40601 while (scope->kind == sk_class && !scope->defining_class_p)
40603 parent_scope = scope;
40604 scope = scope->level_chain;
40608 current_binding_level = scope;
40610 if (scope->kind != sk_template_parms
40611 || !function_being_declared_is_template_p (parser))
40613 /* Introduce a new template parameter list for implicit template
40614 parameters. */
40616 become_template = true;
40618 parser->implicit_template_scope
40619 = begin_scope (sk_template_parms, NULL);
40621 ++processing_template_decl;
40623 parser->fully_implicit_function_template_p = true;
40624 ++parser->num_template_parameter_lists;
40626 else
40628 /* Synthesize implicit template parameters at the end of the explicit
40629 template parameter list. */
40631 gcc_assert (current_template_parms);
40633 parser->implicit_template_scope = scope;
40635 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
40636 parser->implicit_template_parms
40637 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
40641 /* Synthesize a new template parameter and track the current template
40642 parameter chain with implicit_template_parms. */
40644 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
40645 tree synth_id = make_generic_type_name ();
40646 tree synth_tmpl_parm;
40647 bool non_type = false;
40649 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
40650 synth_tmpl_parm
40651 = finish_template_type_parm (class_type_node, synth_id);
40652 else if (TREE_CODE (proto) == TEMPLATE_DECL)
40653 synth_tmpl_parm
40654 = finish_constrained_template_template_parm (proto, synth_id);
40655 else
40657 synth_tmpl_parm = copy_decl (proto);
40658 DECL_NAME (synth_tmpl_parm) = synth_id;
40659 non_type = true;
40662 // Attach the constraint to the parm before processing.
40663 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
40664 TREE_TYPE (node) = constr;
40665 tree new_parm
40666 = process_template_parm (parser->implicit_template_parms,
40667 input_location,
40668 node,
40669 /*non_type=*/non_type,
40670 /*param_pack=*/false);
40672 // Chain the new parameter to the list of implicit parameters.
40673 if (parser->implicit_template_parms)
40674 parser->implicit_template_parms
40675 = TREE_CHAIN (parser->implicit_template_parms);
40676 else
40677 parser->implicit_template_parms = new_parm;
40679 tree new_decl = get_local_decls ();
40680 if (non_type)
40681 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
40682 new_decl = DECL_INITIAL (new_decl);
40684 /* If creating a fully implicit function template, start the new implicit
40685 template parameter list with this synthesized type, otherwise grow the
40686 current template parameter list. */
40688 if (become_template)
40690 parent_scope->level_chain = current_binding_level;
40692 tree new_parms = make_tree_vec (1);
40693 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
40694 current_template_parms = tree_cons (size_int (processing_template_decl),
40695 new_parms, current_template_parms);
40697 else
40699 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
40700 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
40701 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
40702 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
40705 // If the new parameter was constrained, we need to add that to the
40706 // constraints in the template parameter list.
40707 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
40709 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
40710 reqs = conjoin_constraints (reqs, req);
40711 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
40714 current_binding_level = entry_scope;
40716 return new_decl;
40719 /* Finish the declaration of a fully implicit function template. Such a
40720 template has no explicit template parameter list so has not been through the
40721 normal template head and tail processing. synthesize_implicit_template_parm
40722 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
40723 provided if the declaration is a class member such that its template
40724 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
40725 form is returned. Otherwise NULL_TREE is returned. */
40727 static tree
40728 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
40730 gcc_assert (parser->fully_implicit_function_template_p);
40732 if (member_decl_opt && member_decl_opt != error_mark_node
40733 && DECL_VIRTUAL_P (member_decl_opt))
40735 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
40736 "implicit templates may not be %<virtual%>");
40737 DECL_VIRTUAL_P (member_decl_opt) = false;
40740 if (member_decl_opt)
40741 member_decl_opt = finish_member_template_decl (member_decl_opt);
40742 end_template_decl ();
40744 parser->fully_implicit_function_template_p = false;
40745 parser->implicit_template_parms = 0;
40746 parser->implicit_template_scope = 0;
40747 --parser->num_template_parameter_lists;
40749 return member_decl_opt;
40752 /* Like finish_fully_implicit_template, but to be used in error
40753 recovery, rearranging scopes so that we restore the state we had
40754 before synthesize_implicit_template_parm inserted the implement
40755 template parms scope. */
40757 static void
40758 abort_fully_implicit_template (cp_parser *parser)
40760 cp_binding_level *return_to_scope = current_binding_level;
40762 if (parser->implicit_template_scope
40763 && return_to_scope != parser->implicit_template_scope)
40765 cp_binding_level *child = return_to_scope;
40766 for (cp_binding_level *scope = child->level_chain;
40767 scope != parser->implicit_template_scope;
40768 scope = child->level_chain)
40769 child = scope;
40770 child->level_chain = parser->implicit_template_scope->level_chain;
40771 parser->implicit_template_scope->level_chain = return_to_scope;
40772 current_binding_level = parser->implicit_template_scope;
40774 else
40775 return_to_scope = return_to_scope->level_chain;
40777 finish_fully_implicit_template (parser, NULL);
40779 gcc_assert (current_binding_level == return_to_scope);
40782 /* Helper function for diagnostics that have complained about things
40783 being used with 'extern "C"' linkage.
40785 Attempt to issue a note showing where the 'extern "C"' linkage began. */
40787 void
40788 maybe_show_extern_c_location (void)
40790 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
40791 inform (the_parser->innermost_linkage_specification_location,
40792 "%<extern \"C\"%> linkage started here");
40795 #include "gt-cp-parser.h"