Implement P0315R4, Lambdas in unevaluated contexts.
[official-gcc.git] / gcc / cp / parser.c
blobdb0f0338179ea1d20a574be11bead07f46650f13
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 (cxx_dialect >= cxx2a)
10179 /* C++20 allows lambdas in unevaluated context. */;
10180 else if (cp_unevaluated_operand)
10182 if (!token->error_reported)
10184 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10185 "lambda-expression in unevaluated context"
10186 " only available with -std=c++2a or -std=gnu++2a");
10187 token->error_reported = true;
10189 ok = false;
10191 else if (parser->in_template_argument_list_p)
10193 if (!token->error_reported)
10195 error_at (token->location, "lambda-expression in template-argument"
10196 " only available with -std=c++2a or -std=gnu++2a");
10197 token->error_reported = true;
10199 ok = false;
10202 /* We may be in the middle of deferred access check. Disable
10203 it now. */
10204 push_deferring_access_checks (dk_no_deferred);
10206 cp_parser_lambda_introducer (parser, lambda_expr);
10207 if (cp_parser_error_occurred (parser))
10208 return error_mark_node;
10210 type = begin_lambda_type (lambda_expr);
10211 if (type == error_mark_node)
10212 return error_mark_node;
10214 record_lambda_scope (lambda_expr);
10216 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10217 determine_visibility (TYPE_NAME (type));
10219 /* Now that we've started the type, add the capture fields for any
10220 explicit captures. */
10221 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10224 /* Inside the class, surrounding template-parameter-lists do not apply. */
10225 unsigned int saved_num_template_parameter_lists
10226 = parser->num_template_parameter_lists;
10227 unsigned char in_statement = parser->in_statement;
10228 bool in_switch_statement_p = parser->in_switch_statement_p;
10229 bool fully_implicit_function_template_p
10230 = parser->fully_implicit_function_template_p;
10231 tree implicit_template_parms = parser->implicit_template_parms;
10232 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10233 bool auto_is_implicit_function_template_parm_p
10234 = parser->auto_is_implicit_function_template_parm_p;
10236 parser->num_template_parameter_lists = 0;
10237 parser->in_statement = 0;
10238 parser->in_switch_statement_p = false;
10239 parser->fully_implicit_function_template_p = false;
10240 parser->implicit_template_parms = 0;
10241 parser->implicit_template_scope = 0;
10242 parser->auto_is_implicit_function_template_parm_p = false;
10244 /* By virtue of defining a local class, a lambda expression has access to
10245 the private variables of enclosing classes. */
10247 if (cp_parser_start_tentative_firewall (parser))
10248 start = token;
10250 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10252 if (ok && cp_parser_error_occurred (parser))
10253 ok = false;
10255 if (ok)
10257 cp_parser_lambda_body (parser, lambda_expr);
10259 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10261 if (cp_parser_skip_to_closing_brace (parser))
10262 cp_lexer_consume_token (parser->lexer);
10265 /* The capture list was built up in reverse order; fix that now. */
10266 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10267 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10269 if (ok)
10270 maybe_add_lambda_conv_op (type);
10272 type = finish_struct (type, /*attributes=*/NULL_TREE);
10274 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10275 parser->in_statement = in_statement;
10276 parser->in_switch_statement_p = in_switch_statement_p;
10277 parser->fully_implicit_function_template_p
10278 = fully_implicit_function_template_p;
10279 parser->implicit_template_parms = implicit_template_parms;
10280 parser->implicit_template_scope = implicit_template_scope;
10281 parser->auto_is_implicit_function_template_parm_p
10282 = auto_is_implicit_function_template_parm_p;
10285 /* This field is only used during parsing of the lambda. */
10286 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10288 /* This lambda shouldn't have any proxies left at this point. */
10289 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10290 /* And now that we're done, push proxies for an enclosing lambda. */
10291 insert_pending_capture_proxies ();
10293 /* Update the lambda expression to a range. */
10294 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10295 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10296 token->location,
10297 end_tok->location);
10299 if (ok)
10300 lambda_expr = build_lambda_object (lambda_expr);
10301 else
10302 lambda_expr = error_mark_node;
10304 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10306 pop_deferring_access_checks ();
10308 return lambda_expr;
10311 /* Parse the beginning of a lambda expression.
10313 lambda-introducer:
10314 [ lambda-capture [opt] ]
10316 LAMBDA_EXPR is the current representation of the lambda expression. */
10318 static void
10319 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10321 /* Need commas after the first capture. */
10322 bool first = true;
10324 /* Eat the leading `['. */
10325 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10327 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10328 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10329 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10330 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10331 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10332 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10334 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10336 cp_lexer_consume_token (parser->lexer);
10337 first = false;
10339 if (!(at_function_scope_p () || parsing_nsdmi ()))
10340 error ("non-local lambda expression cannot have a capture-default");
10343 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10345 cp_token* capture_token;
10346 tree capture_id;
10347 tree capture_init_expr;
10348 cp_id_kind idk = CP_ID_KIND_NONE;
10349 bool explicit_init_p = false;
10351 enum capture_kind_type
10353 BY_COPY,
10354 BY_REFERENCE
10356 enum capture_kind_type capture_kind = BY_COPY;
10358 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10360 error ("expected end of capture-list");
10361 return;
10364 if (first)
10365 first = false;
10366 else
10367 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10369 /* Possibly capture `this'. */
10370 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10372 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10373 if (cxx_dialect < cxx2a
10374 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10375 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10376 "with by-copy capture default");
10377 cp_lexer_consume_token (parser->lexer);
10378 add_capture (lambda_expr,
10379 /*id=*/this_identifier,
10380 /*initializer=*/finish_this_expr (),
10381 /*by_reference_p=*/true,
10382 explicit_init_p);
10383 continue;
10386 /* Possibly capture `*this'. */
10387 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10388 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10390 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10391 if (cxx_dialect < cxx17)
10392 pedwarn (loc, 0, "%<*this%> capture only available with "
10393 "-std=c++17 or -std=gnu++17");
10394 cp_lexer_consume_token (parser->lexer);
10395 cp_lexer_consume_token (parser->lexer);
10396 add_capture (lambda_expr,
10397 /*id=*/this_identifier,
10398 /*initializer=*/finish_this_expr (),
10399 /*by_reference_p=*/false,
10400 explicit_init_p);
10401 continue;
10404 bool init_pack_expansion = false;
10405 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10407 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10408 if (cxx_dialect < cxx2a)
10409 pedwarn (loc, 0, "pack init-capture only available with "
10410 "-std=c++2a or -std=gnu++2a");
10411 cp_lexer_consume_token (parser->lexer);
10412 init_pack_expansion = true;
10415 /* Remember whether we want to capture as a reference or not. */
10416 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10418 capture_kind = BY_REFERENCE;
10419 cp_lexer_consume_token (parser->lexer);
10422 /* Get the identifier. */
10423 capture_token = cp_lexer_peek_token (parser->lexer);
10424 capture_id = cp_parser_identifier (parser);
10426 if (capture_id == error_mark_node)
10427 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10428 delimiters, but I modified this to stop on unnested ']' as well. It
10429 was already changed to stop on unnested '}', so the
10430 "closing_parenthesis" name is no more misleading with my change. */
10432 cp_parser_skip_to_closing_parenthesis (parser,
10433 /*recovering=*/true,
10434 /*or_comma=*/true,
10435 /*consume_paren=*/true);
10436 break;
10439 /* Find the initializer for this capture. */
10440 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10441 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10442 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10444 bool direct, non_constant;
10445 /* An explicit initializer exists. */
10446 if (cxx_dialect < cxx14)
10447 pedwarn (input_location, 0,
10448 "lambda capture initializers "
10449 "only available with -std=c++14 or -std=gnu++14");
10450 capture_init_expr = cp_parser_initializer (parser, &direct,
10451 &non_constant, true);
10452 explicit_init_p = true;
10453 if (capture_init_expr == NULL_TREE)
10455 error ("empty initializer for lambda init-capture");
10456 capture_init_expr = error_mark_node;
10458 if (init_pack_expansion)
10459 capture_init_expr = make_pack_expansion (capture_init_expr);
10461 else
10463 const char* error_msg;
10465 /* Turn the identifier into an id-expression. */
10466 capture_init_expr
10467 = cp_parser_lookup_name_simple (parser, capture_id,
10468 capture_token->location);
10470 if (capture_init_expr == error_mark_node)
10472 unqualified_name_lookup_error (capture_id);
10473 continue;
10475 else if (!VAR_P (capture_init_expr)
10476 && TREE_CODE (capture_init_expr) != PARM_DECL)
10478 error_at (capture_token->location,
10479 "capture of non-variable %qE",
10480 capture_init_expr);
10481 if (DECL_P (capture_init_expr))
10482 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10483 "%q#D declared here", capture_init_expr);
10484 continue;
10486 if (VAR_P (capture_init_expr)
10487 && decl_storage_duration (capture_init_expr) != dk_auto)
10489 if (pedwarn (capture_token->location, 0, "capture of variable "
10490 "%qD with non-automatic storage duration",
10491 capture_init_expr))
10492 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10493 "%q#D declared here", capture_init_expr);
10494 continue;
10497 capture_init_expr
10498 = finish_id_expression
10499 (capture_id,
10500 capture_init_expr,
10501 parser->scope,
10502 &idk,
10503 /*integral_constant_expression_p=*/false,
10504 /*allow_non_integral_constant_expression_p=*/false,
10505 /*non_integral_constant_expression_p=*/NULL,
10506 /*template_p=*/false,
10507 /*done=*/true,
10508 /*address_p=*/false,
10509 /*template_arg_p=*/false,
10510 &error_msg,
10511 capture_token->location);
10513 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10515 cp_lexer_consume_token (parser->lexer);
10516 capture_init_expr = make_pack_expansion (capture_init_expr);
10520 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10521 && !explicit_init_p)
10523 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10524 && capture_kind == BY_COPY)
10525 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10526 "of %qD redundant with by-copy capture default",
10527 capture_id);
10528 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10529 && capture_kind == BY_REFERENCE)
10530 pedwarn (capture_token->location, 0, "explicit by-reference "
10531 "capture of %qD redundant with by-reference capture "
10532 "default", capture_id);
10535 add_capture (lambda_expr,
10536 capture_id,
10537 capture_init_expr,
10538 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10539 explicit_init_p);
10541 /* If there is any qualification still in effect, clear it
10542 now; we will be starting fresh with the next capture. */
10543 parser->scope = NULL_TREE;
10544 parser->qualifying_scope = NULL_TREE;
10545 parser->object_scope = NULL_TREE;
10548 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10551 /* Parse the (optional) middle of a lambda expression.
10553 lambda-declarator:
10554 < template-parameter-list [opt] >
10555 ( parameter-declaration-clause [opt] )
10556 attribute-specifier [opt]
10557 decl-specifier-seq [opt]
10558 exception-specification [opt]
10559 lambda-return-type-clause [opt]
10561 LAMBDA_EXPR is the current representation of the lambda expression. */
10563 static bool
10564 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10566 /* 5.1.1.4 of the standard says:
10567 If a lambda-expression does not include a lambda-declarator, it is as if
10568 the lambda-declarator were ().
10569 This means an empty parameter list, no attributes, and no exception
10570 specification. */
10571 tree param_list = void_list_node;
10572 tree attributes = NULL_TREE;
10573 tree exception_spec = NULL_TREE;
10574 tree template_param_list = NULL_TREE;
10575 tree tx_qual = NULL_TREE;
10576 tree return_type = NULL_TREE;
10577 cp_decl_specifier_seq lambda_specs;
10578 clear_decl_specs (&lambda_specs);
10580 /* The template-parameter-list is optional, but must begin with
10581 an opening angle if present. */
10582 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10584 if (cxx_dialect < cxx14)
10585 pedwarn (parser->lexer->next_token->location, 0,
10586 "lambda templates are only available with "
10587 "-std=c++14 or -std=gnu++14");
10588 else if (cxx_dialect < cxx2a)
10589 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10590 "lambda templates are only available with "
10591 "-std=c++2a or -std=gnu++2a");
10593 cp_lexer_consume_token (parser->lexer);
10595 template_param_list = cp_parser_template_parameter_list (parser);
10597 cp_parser_skip_to_end_of_template_parameter_list (parser);
10599 /* We just processed one more parameter list. */
10600 ++parser->num_template_parameter_lists;
10603 /* The parameter-declaration-clause is optional (unless
10604 template-parameter-list was given), but must begin with an
10605 opening parenthesis if present. */
10606 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10608 matching_parens parens;
10609 parens.consume_open (parser);
10611 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10613 /* Parse parameters. */
10614 param_list = cp_parser_parameter_declaration_clause (parser);
10616 /* Default arguments shall not be specified in the
10617 parameter-declaration-clause of a lambda-declarator. */
10618 if (cxx_dialect < cxx14)
10619 for (tree t = param_list; t; t = TREE_CHAIN (t))
10620 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10621 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10622 "default argument specified for lambda parameter");
10624 parens.require_close (parser);
10626 /* In the decl-specifier-seq of the lambda-declarator, each
10627 decl-specifier shall either be mutable or constexpr. */
10628 int declares_class_or_enum;
10629 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10630 cp_parser_decl_specifier_seq (parser,
10631 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10632 &lambda_specs, &declares_class_or_enum);
10633 if (lambda_specs.storage_class == sc_mutable)
10635 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10636 if (lambda_specs.conflicting_specifiers_p)
10637 error_at (lambda_specs.locations[ds_storage_class],
10638 "duplicate %<mutable%>");
10641 tx_qual = cp_parser_tx_qualifier_opt (parser);
10643 /* Parse optional exception specification. */
10644 exception_spec = cp_parser_exception_specification_opt (parser);
10646 attributes = cp_parser_std_attribute_spec_seq (parser);
10648 /* Parse optional trailing return type. */
10649 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10651 cp_lexer_consume_token (parser->lexer);
10652 return_type = cp_parser_trailing_type_id (parser);
10655 /* The function parameters must be in scope all the way until after the
10656 trailing-return-type in case of decltype. */
10657 pop_bindings_and_leave_scope ();
10659 else if (template_param_list != NULL_TREE) // generate diagnostic
10660 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10662 /* Create the function call operator.
10664 Messing with declarators like this is no uglier than building up the
10665 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10666 other code. */
10668 cp_decl_specifier_seq return_type_specs;
10669 cp_declarator* declarator;
10670 tree fco;
10671 int quals;
10672 void *p;
10674 clear_decl_specs (&return_type_specs);
10675 return_type_specs.type = make_auto ();
10677 if (lambda_specs.locations[ds_constexpr])
10679 if (cxx_dialect >= cxx17)
10680 return_type_specs.locations[ds_constexpr]
10681 = lambda_specs.locations[ds_constexpr];
10682 else
10683 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10684 "lambda only available with -std=c++17 or -std=gnu++17");
10687 p = obstack_alloc (&declarator_obstack, 0);
10689 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10691 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10692 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10693 declarator = make_call_declarator (declarator, param_list, quals,
10694 VIRT_SPEC_UNSPECIFIED,
10695 REF_QUAL_NONE,
10696 tx_qual,
10697 exception_spec,
10698 return_type,
10699 /*requires_clause*/NULL_TREE);
10700 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10701 declarator->std_attributes = attributes;
10703 fco = grokmethod (&return_type_specs,
10704 declarator,
10705 NULL_TREE);
10706 if (fco != error_mark_node)
10708 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10709 DECL_ARTIFICIAL (fco) = 1;
10710 /* Give the object parameter a different name. */
10711 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10712 DECL_LAMBDA_FUNCTION (fco) = 1;
10714 if (template_param_list)
10716 fco = finish_member_template_decl (fco);
10717 finish_template_decl (template_param_list);
10718 --parser->num_template_parameter_lists;
10720 else if (parser->fully_implicit_function_template_p)
10721 fco = finish_fully_implicit_template (parser, fco);
10723 finish_member_declaration (fco);
10725 obstack_free (&declarator_obstack, p);
10727 return (fco != error_mark_node);
10731 /* Parse the body of a lambda expression, which is simply
10733 compound-statement
10735 but which requires special handling.
10736 LAMBDA_EXPR is the current representation of the lambda expression. */
10738 static void
10739 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10741 bool nested = (current_function_decl != NULL_TREE);
10742 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10743 bool in_function_body = parser->in_function_body;
10745 /* The body of a lambda-expression is not a subexpression of the enclosing
10746 expression. */
10747 cp_evaluated ev;
10749 if (nested)
10750 push_function_context ();
10751 else
10752 /* Still increment function_depth so that we don't GC in the
10753 middle of an expression. */
10754 ++function_depth;
10756 vec<tree> omp_privatization_save;
10757 save_omp_privatization_clauses (omp_privatization_save);
10758 /* Clear this in case we're in the middle of a default argument. */
10759 parser->local_variables_forbidden_p = false;
10760 parser->in_function_body = true;
10763 local_specialization_stack s (lss_copy);
10764 tree fco = lambda_function (lambda_expr);
10765 tree body = start_lambda_function (fco, lambda_expr);
10766 matching_braces braces;
10768 if (braces.require_open (parser))
10770 tree compound_stmt = begin_compound_stmt (0);
10772 /* Originally C++11 required us to peek for 'return expr'; and
10773 process it specially here to deduce the return type. N3638
10774 removed the need for that. */
10776 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10777 cp_parser_label_declaration (parser);
10778 cp_parser_statement_seq_opt (parser, NULL_TREE);
10779 braces.require_close (parser);
10781 finish_compound_stmt (compound_stmt);
10784 finish_lambda_function (body);
10787 restore_omp_privatization_clauses (omp_privatization_save);
10788 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10789 parser->in_function_body = in_function_body;
10790 if (nested)
10791 pop_function_context();
10792 else
10793 --function_depth;
10796 /* Statements [gram.stmt.stmt] */
10798 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10800 static void
10801 add_debug_begin_stmt (location_t loc)
10803 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10804 return;
10805 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10806 /* A concept is never expanded normally. */
10807 return;
10809 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10810 SET_EXPR_LOCATION (stmt, loc);
10811 add_stmt (stmt);
10814 /* Parse a statement.
10816 statement:
10817 labeled-statement
10818 expression-statement
10819 compound-statement
10820 selection-statement
10821 iteration-statement
10822 jump-statement
10823 declaration-statement
10824 try-block
10826 C++11:
10828 statement:
10829 labeled-statement
10830 attribute-specifier-seq (opt) expression-statement
10831 attribute-specifier-seq (opt) compound-statement
10832 attribute-specifier-seq (opt) selection-statement
10833 attribute-specifier-seq (opt) iteration-statement
10834 attribute-specifier-seq (opt) jump-statement
10835 declaration-statement
10836 attribute-specifier-seq (opt) try-block
10838 init-statement:
10839 expression-statement
10840 simple-declaration
10842 TM Extension:
10844 statement:
10845 atomic-statement
10847 IN_COMPOUND is true when the statement is nested inside a
10848 cp_parser_compound_statement; this matters for certain pragmas.
10850 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10851 is a (possibly labeled) if statement which is not enclosed in braces
10852 and has an else clause. This is used to implement -Wparentheses.
10854 CHAIN is a vector of if-else-if conditions. */
10856 static void
10857 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10858 bool in_compound, bool *if_p, vec<tree> *chain,
10859 location_t *loc_after_labels)
10861 tree statement, std_attrs = NULL_TREE;
10862 cp_token *token;
10863 location_t statement_location, attrs_location;
10865 restart:
10866 if (if_p != NULL)
10867 *if_p = false;
10868 /* There is no statement yet. */
10869 statement = NULL_TREE;
10871 saved_token_sentinel saved_tokens (parser->lexer);
10872 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10873 if (c_dialect_objc ())
10874 /* In obj-c++, seeing '[[' might be the either the beginning of
10875 c++11 attributes, or a nested objc-message-expression. So
10876 let's parse the c++11 attributes tentatively. */
10877 cp_parser_parse_tentatively (parser);
10878 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10879 if (c_dialect_objc ())
10881 if (!cp_parser_parse_definitely (parser))
10882 std_attrs = NULL_TREE;
10885 /* Peek at the next token. */
10886 token = cp_lexer_peek_token (parser->lexer);
10887 /* Remember the location of the first token in the statement. */
10888 statement_location = token->location;
10889 add_debug_begin_stmt (statement_location);
10890 /* If this is a keyword, then that will often determine what kind of
10891 statement we have. */
10892 if (token->type == CPP_KEYWORD)
10894 enum rid keyword = token->keyword;
10896 switch (keyword)
10898 case RID_CASE:
10899 case RID_DEFAULT:
10900 /* Looks like a labeled-statement with a case label.
10901 Parse the label, and then use tail recursion to parse
10902 the statement. */
10903 cp_parser_label_for_labeled_statement (parser, std_attrs);
10904 in_compound = false;
10905 goto restart;
10907 case RID_IF:
10908 case RID_SWITCH:
10909 statement = cp_parser_selection_statement (parser, if_p, chain);
10910 break;
10912 case RID_WHILE:
10913 case RID_DO:
10914 case RID_FOR:
10915 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10916 break;
10918 case RID_BREAK:
10919 case RID_CONTINUE:
10920 case RID_RETURN:
10921 case RID_GOTO:
10922 statement = cp_parser_jump_statement (parser);
10923 break;
10925 /* Objective-C++ exception-handling constructs. */
10926 case RID_AT_TRY:
10927 case RID_AT_CATCH:
10928 case RID_AT_FINALLY:
10929 case RID_AT_SYNCHRONIZED:
10930 case RID_AT_THROW:
10931 statement = cp_parser_objc_statement (parser);
10932 break;
10934 case RID_TRY:
10935 statement = cp_parser_try_block (parser);
10936 break;
10938 case RID_NAMESPACE:
10939 /* This must be a namespace alias definition. */
10940 cp_parser_declaration_statement (parser);
10941 return;
10943 case RID_TRANSACTION_ATOMIC:
10944 case RID_TRANSACTION_RELAXED:
10945 case RID_SYNCHRONIZED:
10946 case RID_ATOMIC_NOEXCEPT:
10947 case RID_ATOMIC_CANCEL:
10948 statement = cp_parser_transaction (parser, token);
10949 break;
10950 case RID_TRANSACTION_CANCEL:
10951 statement = cp_parser_transaction_cancel (parser);
10952 break;
10954 default:
10955 /* It might be a keyword like `int' that can start a
10956 declaration-statement. */
10957 break;
10960 else if (token->type == CPP_NAME)
10962 /* If the next token is a `:', then we are looking at a
10963 labeled-statement. */
10964 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10965 if (token->type == CPP_COLON)
10967 /* Looks like a labeled-statement with an ordinary label.
10968 Parse the label, and then use tail recursion to parse
10969 the statement. */
10971 cp_parser_label_for_labeled_statement (parser, std_attrs);
10972 in_compound = false;
10973 goto restart;
10976 /* Anything that starts with a `{' must be a compound-statement. */
10977 else if (token->type == CPP_OPEN_BRACE)
10978 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10979 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10980 a statement all its own. */
10981 else if (token->type == CPP_PRAGMA)
10983 /* Only certain OpenMP pragmas are attached to statements, and thus
10984 are considered statements themselves. All others are not. In
10985 the context of a compound, accept the pragma as a "statement" and
10986 return so that we can check for a close brace. Otherwise we
10987 require a real statement and must go back and read one. */
10988 if (in_compound)
10989 cp_parser_pragma (parser, pragma_compound, if_p);
10990 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10991 goto restart;
10992 return;
10994 else if (token->type == CPP_EOF)
10996 cp_parser_error (parser, "expected statement");
10997 return;
11000 /* Everything else must be a declaration-statement or an
11001 expression-statement. Try for the declaration-statement
11002 first, unless we are looking at a `;', in which case we know that
11003 we have an expression-statement. */
11004 if (!statement)
11006 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11008 if (std_attrs != NULL_TREE)
11010 /* Attributes should be parsed as part of the the
11011 declaration, so let's un-parse them. */
11012 saved_tokens.rollback();
11013 std_attrs = NULL_TREE;
11016 cp_parser_parse_tentatively (parser);
11017 /* Try to parse the declaration-statement. */
11018 cp_parser_declaration_statement (parser);
11019 /* If that worked, we're done. */
11020 if (cp_parser_parse_definitely (parser))
11021 return;
11023 /* All preceding labels have been parsed at this point. */
11024 if (loc_after_labels != NULL)
11025 *loc_after_labels = statement_location;
11027 /* Look for an expression-statement instead. */
11028 statement = cp_parser_expression_statement (parser, in_statement_expr);
11030 /* Handle [[fallthrough]];. */
11031 if (attribute_fallthrough_p (std_attrs))
11033 /* The next token after the fallthrough attribute is ';'. */
11034 if (statement == NULL_TREE)
11036 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11037 statement = build_call_expr_internal_loc (statement_location,
11038 IFN_FALLTHROUGH,
11039 void_type_node, 0);
11040 finish_expr_stmt (statement);
11042 else
11043 warning_at (statement_location, OPT_Wattributes,
11044 "%<fallthrough%> attribute not followed by %<;%>");
11045 std_attrs = NULL_TREE;
11049 /* Set the line number for the statement. */
11050 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11051 SET_EXPR_LOCATION (statement, statement_location);
11053 /* Allow "[[fallthrough]];", but warn otherwise. */
11054 if (std_attrs != NULL_TREE)
11055 warning_at (attrs_location,
11056 OPT_Wattributes,
11057 "attributes at the beginning of statement are ignored");
11060 /* Append ATTR to attribute list ATTRS. */
11062 static tree
11063 attr_chainon (tree attrs, tree attr)
11065 if (attrs == error_mark_node)
11066 return error_mark_node;
11067 if (attr == error_mark_node)
11068 return error_mark_node;
11069 return chainon (attrs, attr);
11072 /* Parse the label for a labeled-statement, i.e.
11074 identifier :
11075 case constant-expression :
11076 default :
11078 GNU Extension:
11079 case constant-expression ... constant-expression : statement
11081 When a label is parsed without errors, the label is added to the
11082 parse tree by the finish_* functions, so this function doesn't
11083 have to return the label. */
11085 static void
11086 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11088 cp_token *token;
11089 tree label = NULL_TREE;
11090 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11092 /* The next token should be an identifier. */
11093 token = cp_lexer_peek_token (parser->lexer);
11094 if (token->type != CPP_NAME
11095 && token->type != CPP_KEYWORD)
11097 cp_parser_error (parser, "expected labeled-statement");
11098 return;
11101 /* Remember whether this case or a user-defined label is allowed to fall
11102 through to. */
11103 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11105 parser->colon_corrects_to_scope_p = false;
11106 switch (token->keyword)
11108 case RID_CASE:
11110 tree expr, expr_hi;
11111 cp_token *ellipsis;
11113 /* Consume the `case' token. */
11114 cp_lexer_consume_token (parser->lexer);
11115 /* Parse the constant-expression. */
11116 expr = cp_parser_constant_expression (parser);
11117 if (check_for_bare_parameter_packs (expr))
11118 expr = error_mark_node;
11120 ellipsis = cp_lexer_peek_token (parser->lexer);
11121 if (ellipsis->type == CPP_ELLIPSIS)
11123 /* Consume the `...' token. */
11124 cp_lexer_consume_token (parser->lexer);
11125 expr_hi = cp_parser_constant_expression (parser);
11126 if (check_for_bare_parameter_packs (expr_hi))
11127 expr_hi = error_mark_node;
11129 /* We don't need to emit warnings here, as the common code
11130 will do this for us. */
11132 else
11133 expr_hi = NULL_TREE;
11135 if (parser->in_switch_statement_p)
11137 tree l = finish_case_label (token->location, expr, expr_hi);
11138 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11139 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11141 else
11142 error_at (token->location,
11143 "case label %qE not within a switch statement",
11144 expr);
11146 break;
11148 case RID_DEFAULT:
11149 /* Consume the `default' token. */
11150 cp_lexer_consume_token (parser->lexer);
11152 if (parser->in_switch_statement_p)
11154 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11155 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11156 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11158 else
11159 error_at (token->location, "case label not within a switch statement");
11160 break;
11162 default:
11163 /* Anything else must be an ordinary label. */
11164 label = finish_label_stmt (cp_parser_identifier (parser));
11165 if (label && TREE_CODE (label) == LABEL_DECL)
11166 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11167 break;
11170 /* Require the `:' token. */
11171 cp_parser_require (parser, CPP_COLON, RT_COLON);
11173 /* An ordinary label may optionally be followed by attributes.
11174 However, this is only permitted if the attributes are then
11175 followed by a semicolon. This is because, for backward
11176 compatibility, when parsing
11177 lab: __attribute__ ((unused)) int i;
11178 we want the attribute to attach to "i", not "lab". */
11179 if (label != NULL_TREE
11180 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11182 tree attrs;
11183 cp_parser_parse_tentatively (parser);
11184 attrs = cp_parser_gnu_attributes_opt (parser);
11185 if (attrs == NULL_TREE
11186 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11187 cp_parser_abort_tentative_parse (parser);
11188 else if (!cp_parser_parse_definitely (parser))
11190 else
11191 attributes = attr_chainon (attributes, attrs);
11194 if (attributes != NULL_TREE)
11195 cplus_decl_attributes (&label, attributes, 0);
11197 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11200 /* Parse an expression-statement.
11202 expression-statement:
11203 expression [opt] ;
11205 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11206 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11207 indicates whether this expression-statement is part of an
11208 expression statement. */
11210 static tree
11211 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11213 tree statement = NULL_TREE;
11214 cp_token *token = cp_lexer_peek_token (parser->lexer);
11215 location_t loc = token->location;
11217 /* There might be attribute fallthrough. */
11218 tree attr = cp_parser_gnu_attributes_opt (parser);
11220 /* If the next token is a ';', then there is no expression
11221 statement. */
11222 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11224 statement = cp_parser_expression (parser);
11225 if (statement == error_mark_node
11226 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11228 cp_parser_skip_to_end_of_block_or_statement (parser);
11229 return error_mark_node;
11233 /* Handle [[fallthrough]];. */
11234 if (attribute_fallthrough_p (attr))
11236 /* The next token after the fallthrough attribute is ';'. */
11237 if (statement == NULL_TREE)
11238 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11239 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11240 void_type_node, 0);
11241 else
11242 warning_at (loc, OPT_Wattributes,
11243 "%<fallthrough%> attribute not followed by %<;%>");
11244 attr = NULL_TREE;
11247 /* Allow "[[fallthrough]];", but warn otherwise. */
11248 if (attr != NULL_TREE)
11249 warning_at (loc, OPT_Wattributes,
11250 "attributes at the beginning of statement are ignored");
11252 /* Give a helpful message for "A<T>::type t;" and the like. */
11253 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11254 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11256 if (TREE_CODE (statement) == SCOPE_REF)
11257 error_at (token->location, "need %<typename%> before %qE because "
11258 "%qT is a dependent scope",
11259 statement, TREE_OPERAND (statement, 0));
11260 else if (is_overloaded_fn (statement)
11261 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11263 /* A::A a; */
11264 tree fn = get_first_fn (statement);
11265 error_at (token->location,
11266 "%<%T::%D%> names the constructor, not the type",
11267 DECL_CONTEXT (fn), DECL_NAME (fn));
11271 /* Consume the final `;'. */
11272 cp_parser_consume_semicolon_at_end_of_statement (parser);
11274 if (in_statement_expr
11275 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11276 /* This is the final expression statement of a statement
11277 expression. */
11278 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11279 else if (statement)
11280 statement = finish_expr_stmt (statement);
11282 return statement;
11285 /* Parse a compound-statement.
11287 compound-statement:
11288 { statement-seq [opt] }
11290 GNU extension:
11292 compound-statement:
11293 { label-declaration-seq [opt] statement-seq [opt] }
11295 label-declaration-seq:
11296 label-declaration
11297 label-declaration-seq label-declaration
11299 Returns a tree representing the statement. */
11301 static tree
11302 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11303 int bcs_flags, bool function_body)
11305 tree compound_stmt;
11306 matching_braces braces;
11308 /* Consume the `{'. */
11309 if (!braces.require_open (parser))
11310 return error_mark_node;
11311 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11312 && !function_body && cxx_dialect < cxx14)
11313 pedwarn (input_location, OPT_Wpedantic,
11314 "compound-statement in %<constexpr%> function");
11315 /* Begin the compound-statement. */
11316 compound_stmt = begin_compound_stmt (bcs_flags);
11317 /* If the next keyword is `__label__' we have a label declaration. */
11318 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11319 cp_parser_label_declaration (parser);
11320 /* Parse an (optional) statement-seq. */
11321 cp_parser_statement_seq_opt (parser, in_statement_expr);
11322 /* Finish the compound-statement. */
11323 finish_compound_stmt (compound_stmt);
11324 /* Consume the `}'. */
11325 braces.require_close (parser);
11327 return compound_stmt;
11330 /* Parse an (optional) statement-seq.
11332 statement-seq:
11333 statement
11334 statement-seq [opt] statement */
11336 static void
11337 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11339 /* Scan statements until there aren't any more. */
11340 while (true)
11342 cp_token *token = cp_lexer_peek_token (parser->lexer);
11344 /* If we are looking at a `}', then we have run out of
11345 statements; the same is true if we have reached the end
11346 of file, or have stumbled upon a stray '@end'. */
11347 if (token->type == CPP_CLOSE_BRACE
11348 || token->type == CPP_EOF
11349 || token->type == CPP_PRAGMA_EOL
11350 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11351 break;
11353 /* If we are in a compound statement and find 'else' then
11354 something went wrong. */
11355 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11357 if (parser->in_statement & IN_IF_STMT)
11358 break;
11359 else
11361 token = cp_lexer_consume_token (parser->lexer);
11362 error_at (token->location, "%<else%> without a previous %<if%>");
11366 /* Parse the statement. */
11367 cp_parser_statement (parser, in_statement_expr, true, NULL);
11371 /* Return true if this is the C++20 version of range-based-for with
11372 init-statement. */
11374 static bool
11375 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11377 bool r = false;
11379 /* Save tokens so that we can put them back. */
11380 cp_lexer_save_tokens (parser->lexer);
11382 /* There has to be an unnested ; followed by an unnested :. */
11383 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11384 /*recovering=*/false,
11385 CPP_SEMICOLON,
11386 /*consume_paren=*/false) != -1)
11387 goto out;
11389 /* We found the semicolon, eat it now. */
11390 cp_lexer_consume_token (parser->lexer);
11392 /* Now look for ':' that is not nested in () or {}. */
11393 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11394 /*recovering=*/false,
11395 CPP_COLON,
11396 /*consume_paren=*/false) == -1);
11398 out:
11399 /* Roll back the tokens we skipped. */
11400 cp_lexer_rollback_tokens (parser->lexer);
11402 return r;
11405 /* Return true if we're looking at (init; cond), false otherwise. */
11407 static bool
11408 cp_parser_init_statement_p (cp_parser *parser)
11410 /* Save tokens so that we can put them back. */
11411 cp_lexer_save_tokens (parser->lexer);
11413 /* Look for ';' that is not nested in () or {}. */
11414 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11415 /*recovering=*/false,
11416 CPP_SEMICOLON,
11417 /*consume_paren=*/false);
11419 /* Roll back the tokens we skipped. */
11420 cp_lexer_rollback_tokens (parser->lexer);
11422 return ret == -1;
11425 /* Parse a selection-statement.
11427 selection-statement:
11428 if ( init-statement [opt] condition ) statement
11429 if ( init-statement [opt] condition ) statement else statement
11430 switch ( init-statement [opt] condition ) statement
11432 Returns the new IF_STMT or SWITCH_STMT.
11434 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11435 is a (possibly labeled) if statement which is not enclosed in
11436 braces and has an else clause. This is used to implement
11437 -Wparentheses.
11439 CHAIN is a vector of if-else-if conditions. This is used to implement
11440 -Wduplicated-cond. */
11442 static tree
11443 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11444 vec<tree> *chain)
11446 cp_token *token;
11447 enum rid keyword;
11448 token_indent_info guard_tinfo;
11450 if (if_p != NULL)
11451 *if_p = false;
11453 /* Peek at the next token. */
11454 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11455 guard_tinfo = get_token_indent_info (token);
11457 /* See what kind of keyword it is. */
11458 keyword = token->keyword;
11459 switch (keyword)
11461 case RID_IF:
11462 case RID_SWITCH:
11464 tree statement;
11465 tree condition;
11467 bool cx = false;
11468 if (keyword == RID_IF
11469 && cp_lexer_next_token_is_keyword (parser->lexer,
11470 RID_CONSTEXPR))
11472 cx = true;
11473 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11474 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11475 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11476 "with -std=c++17 or -std=gnu++17");
11479 /* Look for the `('. */
11480 matching_parens parens;
11481 if (!parens.require_open (parser))
11483 cp_parser_skip_to_end_of_statement (parser);
11484 return error_mark_node;
11487 /* Begin the selection-statement. */
11488 if (keyword == RID_IF)
11490 statement = begin_if_stmt ();
11491 IF_STMT_CONSTEXPR_P (statement) = cx;
11493 else
11494 statement = begin_switch_stmt ();
11496 /* Parse the optional init-statement. */
11497 if (cp_parser_init_statement_p (parser))
11499 tree decl;
11500 if (cxx_dialect < cxx17)
11501 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11502 "init-statement in selection statements only available "
11503 "with -std=c++17 or -std=gnu++17");
11504 cp_parser_init_statement (parser, &decl);
11507 /* Parse the condition. */
11508 condition = cp_parser_condition (parser);
11509 /* Look for the `)'. */
11510 if (!parens.require_close (parser))
11511 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11512 /*consume_paren=*/true);
11514 if (keyword == RID_IF)
11516 bool nested_if;
11517 unsigned char in_statement;
11519 /* Add the condition. */
11520 condition = finish_if_stmt_cond (condition, statement);
11522 if (warn_duplicated_cond)
11523 warn_duplicated_cond_add_or_warn (token->location, condition,
11524 &chain);
11526 /* Parse the then-clause. */
11527 in_statement = parser->in_statement;
11528 parser->in_statement |= IN_IF_STMT;
11530 /* Outside a template, the non-selected branch of a constexpr
11531 if is a 'discarded statement', i.e. unevaluated. */
11532 bool was_discarded = in_discarded_stmt;
11533 bool discard_then = (cx && !processing_template_decl
11534 && integer_zerop (condition));
11535 if (discard_then)
11537 in_discarded_stmt = true;
11538 ++c_inhibit_evaluation_warnings;
11541 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11542 guard_tinfo);
11544 parser->in_statement = in_statement;
11546 finish_then_clause (statement);
11548 if (discard_then)
11550 THEN_CLAUSE (statement) = NULL_TREE;
11551 in_discarded_stmt = was_discarded;
11552 --c_inhibit_evaluation_warnings;
11555 /* If the next token is `else', parse the else-clause. */
11556 if (cp_lexer_next_token_is_keyword (parser->lexer,
11557 RID_ELSE))
11559 bool discard_else = (cx && !processing_template_decl
11560 && integer_nonzerop (condition));
11561 if (discard_else)
11563 in_discarded_stmt = true;
11564 ++c_inhibit_evaluation_warnings;
11567 guard_tinfo
11568 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11569 /* Consume the `else' keyword. */
11570 cp_lexer_consume_token (parser->lexer);
11571 if (warn_duplicated_cond)
11573 if (cp_lexer_next_token_is_keyword (parser->lexer,
11574 RID_IF)
11575 && chain == NULL)
11577 /* We've got "if (COND) else if (COND2)". Start
11578 the condition chain and add COND as the first
11579 element. */
11580 chain = new vec<tree> ();
11581 if (!CONSTANT_CLASS_P (condition)
11582 && !TREE_SIDE_EFFECTS (condition))
11584 /* Wrap it in a NOP_EXPR so that we can set the
11585 location of the condition. */
11586 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11587 condition);
11588 SET_EXPR_LOCATION (e, token->location);
11589 chain->safe_push (e);
11592 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11593 RID_IF))
11595 /* This is if-else without subsequent if. Zap the
11596 condition chain; we would have already warned at
11597 this point. */
11598 delete chain;
11599 chain = NULL;
11602 begin_else_clause (statement);
11603 /* Parse the else-clause. */
11604 cp_parser_implicitly_scoped_statement (parser, NULL,
11605 guard_tinfo, chain);
11607 finish_else_clause (statement);
11609 /* If we are currently parsing a then-clause, then
11610 IF_P will not be NULL. We set it to true to
11611 indicate that this if statement has an else clause.
11612 This may trigger the Wparentheses warning below
11613 when we get back up to the parent if statement. */
11614 if (if_p != NULL)
11615 *if_p = true;
11617 if (discard_else)
11619 ELSE_CLAUSE (statement) = NULL_TREE;
11620 in_discarded_stmt = was_discarded;
11621 --c_inhibit_evaluation_warnings;
11624 else
11626 /* This if statement does not have an else clause. If
11627 NESTED_IF is true, then the then-clause has an if
11628 statement which does have an else clause. We warn
11629 about the potential ambiguity. */
11630 if (nested_if)
11631 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11632 "suggest explicit braces to avoid ambiguous"
11633 " %<else%>");
11634 if (warn_duplicated_cond)
11636 /* We don't need the condition chain anymore. */
11637 delete chain;
11638 chain = NULL;
11642 /* Now we're all done with the if-statement. */
11643 finish_if_stmt (statement);
11645 else
11647 bool in_switch_statement_p;
11648 unsigned char in_statement;
11650 /* Add the condition. */
11651 finish_switch_cond (condition, statement);
11653 /* Parse the body of the switch-statement. */
11654 in_switch_statement_p = parser->in_switch_statement_p;
11655 in_statement = parser->in_statement;
11656 parser->in_switch_statement_p = true;
11657 parser->in_statement |= IN_SWITCH_STMT;
11658 cp_parser_implicitly_scoped_statement (parser, if_p,
11659 guard_tinfo);
11660 parser->in_switch_statement_p = in_switch_statement_p;
11661 parser->in_statement = in_statement;
11663 /* Now we're all done with the switch-statement. */
11664 finish_switch_stmt (statement);
11667 return statement;
11669 break;
11671 default:
11672 cp_parser_error (parser, "expected selection-statement");
11673 return error_mark_node;
11677 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11678 If we have seen at least one decl-specifier, and the next token
11679 is not a parenthesis, then we must be looking at a declaration.
11680 (After "int (" we might be looking at a functional cast.) */
11682 static void
11683 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11684 bool any_specifiers_p)
11686 if (any_specifiers_p
11687 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11688 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11689 && !cp_parser_error_occurred (parser))
11690 cp_parser_commit_to_tentative_parse (parser);
11693 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11694 The declarator shall not specify a function or an array. Returns
11695 TRUE if the declarator is valid, FALSE otherwise. */
11697 static bool
11698 cp_parser_check_condition_declarator (cp_parser* parser,
11699 cp_declarator *declarator,
11700 location_t loc)
11702 if (declarator == cp_error_declarator
11703 || function_declarator_p (declarator)
11704 || declarator->kind == cdk_array)
11706 if (declarator == cp_error_declarator)
11707 /* Already complained. */;
11708 else if (declarator->kind == cdk_array)
11709 error_at (loc, "condition declares an array");
11710 else
11711 error_at (loc, "condition declares a function");
11712 if (parser->fully_implicit_function_template_p)
11713 abort_fully_implicit_template (parser);
11714 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11715 /*or_comma=*/false,
11716 /*consume_paren=*/false);
11717 return false;
11719 else
11720 return true;
11723 /* Parse a condition.
11725 condition:
11726 expression
11727 type-specifier-seq declarator = initializer-clause
11728 type-specifier-seq declarator braced-init-list
11730 GNU Extension:
11732 condition:
11733 type-specifier-seq declarator asm-specification [opt]
11734 attributes [opt] = assignment-expression
11736 Returns the expression that should be tested. */
11738 static tree
11739 cp_parser_condition (cp_parser* parser)
11741 cp_decl_specifier_seq type_specifiers;
11742 const char *saved_message;
11743 int declares_class_or_enum;
11745 /* Try the declaration first. */
11746 cp_parser_parse_tentatively (parser);
11747 /* New types are not allowed in the type-specifier-seq for a
11748 condition. */
11749 saved_message = parser->type_definition_forbidden_message;
11750 parser->type_definition_forbidden_message
11751 = G_("types may not be defined in conditions");
11752 /* Parse the type-specifier-seq. */
11753 cp_parser_decl_specifier_seq (parser,
11754 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11755 &type_specifiers,
11756 &declares_class_or_enum);
11757 /* Restore the saved message. */
11758 parser->type_definition_forbidden_message = saved_message;
11760 cp_parser_maybe_commit_to_declaration (parser,
11761 type_specifiers.any_specifiers_p);
11763 /* If all is well, we might be looking at a declaration. */
11764 if (!cp_parser_error_occurred (parser))
11766 tree decl;
11767 tree asm_specification;
11768 tree attributes;
11769 cp_declarator *declarator;
11770 tree initializer = NULL_TREE;
11771 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11773 /* Parse the declarator. */
11774 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11775 /*ctor_dtor_or_conv_p=*/NULL,
11776 /*parenthesized_p=*/NULL,
11777 /*member_p=*/false,
11778 /*friend_p=*/false);
11779 /* Parse the attributes. */
11780 attributes = cp_parser_attributes_opt (parser);
11781 /* Parse the asm-specification. */
11782 asm_specification = cp_parser_asm_specification_opt (parser);
11783 /* If the next token is not an `=' or '{', then we might still be
11784 looking at an expression. For example:
11786 if (A(a).x)
11788 looks like a decl-specifier-seq and a declarator -- but then
11789 there is no `=', so this is an expression. */
11790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11791 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11792 cp_parser_simulate_error (parser);
11794 /* If we did see an `=' or '{', then we are looking at a declaration
11795 for sure. */
11796 if (cp_parser_parse_definitely (parser))
11798 tree pushed_scope;
11799 bool non_constant_p = false;
11800 int flags = LOOKUP_ONLYCONVERTING;
11802 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11803 return error_mark_node;
11805 /* Create the declaration. */
11806 decl = start_decl (declarator, &type_specifiers,
11807 /*initialized_p=*/true,
11808 attributes, /*prefix_attributes=*/NULL_TREE,
11809 &pushed_scope);
11811 /* Parse the initializer. */
11812 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11814 initializer = cp_parser_braced_list (parser, &non_constant_p);
11815 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11816 flags = 0;
11818 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11820 /* Consume the `='. */
11821 cp_lexer_consume_token (parser->lexer);
11822 initializer = cp_parser_initializer_clause (parser,
11823 &non_constant_p);
11825 else
11827 cp_parser_error (parser, "expected initializer");
11828 initializer = error_mark_node;
11830 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11831 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11833 /* Process the initializer. */
11834 cp_finish_decl (decl,
11835 initializer, !non_constant_p,
11836 asm_specification,
11837 flags);
11839 if (pushed_scope)
11840 pop_scope (pushed_scope);
11842 return convert_from_reference (decl);
11845 /* If we didn't even get past the declarator successfully, we are
11846 definitely not looking at a declaration. */
11847 else
11848 cp_parser_abort_tentative_parse (parser);
11850 /* Otherwise, we are looking at an expression. */
11851 return cp_parser_expression (parser);
11854 /* Parses a for-statement or range-for-statement until the closing ')',
11855 not included. */
11857 static tree
11858 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11860 tree init, scope, decl;
11861 bool is_range_for;
11863 /* Begin the for-statement. */
11864 scope = begin_for_scope (&init);
11866 /* Parse the initialization. */
11867 is_range_for = cp_parser_init_statement (parser, &decl);
11869 if (is_range_for)
11870 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
11871 false);
11872 else
11873 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11876 static tree
11877 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11878 unsigned short unroll)
11880 /* Normal for loop */
11881 tree condition = NULL_TREE;
11882 tree expression = NULL_TREE;
11883 tree stmt;
11885 stmt = begin_for_stmt (scope, init);
11886 /* The init-statement has already been parsed in
11887 cp_parser_init_statement, so no work is needed here. */
11888 finish_init_stmt (stmt);
11890 /* If there's a condition, process it. */
11891 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11892 condition = cp_parser_condition (parser);
11893 else if (ivdep)
11895 cp_parser_error (parser, "missing loop condition in loop with "
11896 "%<GCC ivdep%> pragma");
11897 condition = error_mark_node;
11899 else if (unroll)
11901 cp_parser_error (parser, "missing loop condition in loop with "
11902 "%<GCC unroll%> pragma");
11903 condition = error_mark_node;
11905 finish_for_cond (condition, stmt, ivdep, unroll);
11906 /* Look for the `;'. */
11907 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11909 /* If there's an expression, process it. */
11910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11911 expression = cp_parser_expression (parser);
11912 finish_for_expr (expression, stmt);
11914 return stmt;
11917 /* Tries to parse a range-based for-statement:
11919 range-based-for:
11920 decl-specifier-seq declarator : expression
11922 The decl-specifier-seq declarator and the `:' are already parsed by
11923 cp_parser_init_statement. If processing_template_decl it returns a
11924 newly created RANGE_FOR_STMT; if not, it is converted to a
11925 regular FOR_STMT. */
11927 static tree
11928 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11929 bool ivdep, unsigned short unroll, bool is_omp)
11931 tree stmt, range_expr;
11932 auto_vec <cxx_binding *, 16> bindings;
11933 auto_vec <tree, 16> names;
11934 tree decomp_first_name = NULL_TREE;
11935 unsigned int decomp_cnt = 0;
11937 /* Get the range declaration momentarily out of the way so that
11938 the range expression doesn't clash with it. */
11939 if (range_decl != error_mark_node)
11941 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11943 tree v = DECL_VALUE_EXPR (range_decl);
11944 /* For decomposition declaration get all of the corresponding
11945 declarations out of the way. */
11946 if (TREE_CODE (v) == ARRAY_REF
11947 && VAR_P (TREE_OPERAND (v, 0))
11948 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11950 tree d = range_decl;
11951 range_decl = TREE_OPERAND (v, 0);
11952 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11953 decomp_first_name = d;
11954 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11956 tree name = DECL_NAME (d);
11957 names.safe_push (name);
11958 bindings.safe_push (IDENTIFIER_BINDING (name));
11959 IDENTIFIER_BINDING (name)
11960 = IDENTIFIER_BINDING (name)->previous;
11964 if (names.is_empty ())
11966 tree name = DECL_NAME (range_decl);
11967 names.safe_push (name);
11968 bindings.safe_push (IDENTIFIER_BINDING (name));
11969 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11973 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11975 bool expr_non_constant_p;
11976 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11978 else
11979 range_expr = cp_parser_expression (parser);
11981 /* Put the range declaration(s) back into scope. */
11982 for (unsigned int i = 0; i < names.length (); i++)
11984 cxx_binding *binding = bindings[i];
11985 binding->previous = IDENTIFIER_BINDING (names[i]);
11986 IDENTIFIER_BINDING (names[i]) = binding;
11989 /* finish_omp_for has its own code for the following, so just
11990 return the range_expr instead. */
11991 if (is_omp)
11992 return range_expr;
11994 /* If in template, STMT is converted to a normal for-statement
11995 at instantiation. If not, it is done just ahead. */
11996 if (processing_template_decl)
11998 if (check_for_bare_parameter_packs (range_expr))
11999 range_expr = error_mark_node;
12000 stmt = begin_range_for_stmt (scope, init);
12001 if (ivdep)
12002 RANGE_FOR_IVDEP (stmt) = 1;
12003 if (unroll)
12004 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12005 finish_range_for_decl (stmt, range_decl, range_expr);
12006 if (!type_dependent_expression_p (range_expr)
12007 /* do_auto_deduction doesn't mess with template init-lists. */
12008 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12009 do_range_for_auto_deduction (range_decl, range_expr);
12011 else
12013 stmt = begin_for_stmt (scope, init);
12014 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12015 decomp_first_name, decomp_cnt, ivdep,
12016 unroll);
12018 return stmt;
12021 /* Subroutine of cp_convert_range_for: given the initializer expression,
12022 builds up the range temporary. */
12024 static tree
12025 build_range_temp (tree range_expr)
12027 tree range_type, range_temp;
12029 /* Find out the type deduced by the declaration
12030 `auto &&__range = range_expr'. */
12031 range_type = cp_build_reference_type (make_auto (), true);
12032 range_type = do_auto_deduction (range_type, range_expr,
12033 type_uses_auto (range_type));
12035 /* Create the __range variable. */
12036 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12037 range_type);
12038 TREE_USED (range_temp) = 1;
12039 DECL_ARTIFICIAL (range_temp) = 1;
12041 return range_temp;
12044 /* Used by cp_parser_range_for in template context: we aren't going to
12045 do a full conversion yet, but we still need to resolve auto in the
12046 type of the for-range-declaration if present. This is basically
12047 a shortcut version of cp_convert_range_for. */
12049 static void
12050 do_range_for_auto_deduction (tree decl, tree range_expr)
12052 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12053 if (auto_node)
12055 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12056 range_temp = convert_from_reference (build_range_temp (range_expr));
12057 iter_type = (cp_parser_perform_range_for_lookup
12058 (range_temp, &begin_dummy, &end_dummy));
12059 if (iter_type)
12061 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12062 iter_type);
12063 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12064 RO_UNARY_STAR,
12065 tf_warning_or_error);
12066 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12067 iter_decl, auto_node);
12072 /* Converts a range-based for-statement into a normal
12073 for-statement, as per the definition.
12075 for (RANGE_DECL : RANGE_EXPR)
12076 BLOCK
12078 should be equivalent to:
12081 auto &&__range = RANGE_EXPR;
12082 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12083 __begin != __end;
12084 ++__begin)
12086 RANGE_DECL = *__begin;
12087 BLOCK
12091 If RANGE_EXPR is an array:
12092 BEGIN_EXPR = __range
12093 END_EXPR = __range + ARRAY_SIZE(__range)
12094 Else if RANGE_EXPR has a member 'begin' or 'end':
12095 BEGIN_EXPR = __range.begin()
12096 END_EXPR = __range.end()
12097 Else:
12098 BEGIN_EXPR = begin(__range)
12099 END_EXPR = end(__range);
12101 If __range has a member 'begin' but not 'end', or vice versa, we must
12102 still use the second alternative (it will surely fail, however).
12103 When calling begin()/end() in the third alternative we must use
12104 argument dependent lookup, but always considering 'std' as an associated
12105 namespace. */
12107 tree
12108 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12109 tree decomp_first_name, unsigned int decomp_cnt,
12110 bool ivdep, unsigned short unroll)
12112 tree begin, end;
12113 tree iter_type, begin_expr, end_expr;
12114 tree condition, expression;
12116 range_expr = mark_lvalue_use (range_expr);
12118 if (range_decl == error_mark_node || range_expr == error_mark_node)
12119 /* If an error happened previously do nothing or else a lot of
12120 unhelpful errors would be issued. */
12121 begin_expr = end_expr = iter_type = error_mark_node;
12122 else
12124 tree range_temp;
12126 if (VAR_P (range_expr)
12127 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12128 /* Can't bind a reference to an array of runtime bound. */
12129 range_temp = range_expr;
12130 else
12132 range_temp = build_range_temp (range_expr);
12133 pushdecl (range_temp);
12134 cp_finish_decl (range_temp, range_expr,
12135 /*is_constant_init*/false, NULL_TREE,
12136 LOOKUP_ONLYCONVERTING);
12137 range_temp = convert_from_reference (range_temp);
12139 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12140 &begin_expr, &end_expr);
12143 /* The new for initialization statement. */
12144 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12145 iter_type);
12146 TREE_USED (begin) = 1;
12147 DECL_ARTIFICIAL (begin) = 1;
12148 pushdecl (begin);
12149 cp_finish_decl (begin, begin_expr,
12150 /*is_constant_init*/false, NULL_TREE,
12151 LOOKUP_ONLYCONVERTING);
12153 if (cxx_dialect >= cxx17)
12154 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12155 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12156 TREE_USED (end) = 1;
12157 DECL_ARTIFICIAL (end) = 1;
12158 pushdecl (end);
12159 cp_finish_decl (end, end_expr,
12160 /*is_constant_init*/false, NULL_TREE,
12161 LOOKUP_ONLYCONVERTING);
12163 finish_init_stmt (statement);
12165 /* The new for condition. */
12166 condition = build_x_binary_op (input_location, NE_EXPR,
12167 begin, ERROR_MARK,
12168 end, ERROR_MARK,
12169 NULL, tf_warning_or_error);
12170 finish_for_cond (condition, statement, ivdep, unroll);
12172 /* The new increment expression. */
12173 expression = finish_unary_op_expr (input_location,
12174 PREINCREMENT_EXPR, begin,
12175 tf_warning_or_error);
12176 finish_for_expr (expression, statement);
12178 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12179 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12181 /* The declaration is initialized with *__begin inside the loop body. */
12182 cp_finish_decl (range_decl,
12183 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12184 tf_warning_or_error),
12185 /*is_constant_init*/false, NULL_TREE,
12186 LOOKUP_ONLYCONVERTING);
12187 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12188 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12190 return statement;
12193 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12194 We need to solve both at the same time because the method used
12195 depends on the existence of members begin or end.
12196 Returns the type deduced for the iterator expression. */
12198 static tree
12199 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12201 if (error_operand_p (range))
12203 *begin = *end = error_mark_node;
12204 return error_mark_node;
12207 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12209 error ("range-based %<for%> expression of type %qT "
12210 "has incomplete type", TREE_TYPE (range));
12211 *begin = *end = error_mark_node;
12212 return error_mark_node;
12214 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12216 /* If RANGE is an array, we will use pointer arithmetic. */
12217 *begin = decay_conversion (range, tf_warning_or_error);
12218 *end = build_binary_op (input_location, PLUS_EXPR,
12219 range,
12220 array_type_nelts_top (TREE_TYPE (range)),
12221 false);
12222 return TREE_TYPE (*begin);
12224 else
12226 /* If it is not an array, we must do a bit of magic. */
12227 tree id_begin, id_end;
12228 tree member_begin, member_end;
12230 *begin = *end = error_mark_node;
12232 id_begin = get_identifier ("begin");
12233 id_end = get_identifier ("end");
12234 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12235 /*protect=*/2, /*want_type=*/false,
12236 tf_warning_or_error);
12237 member_end = lookup_member (TREE_TYPE (range), id_end,
12238 /*protect=*/2, /*want_type=*/false,
12239 tf_warning_or_error);
12241 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12243 /* Use the member functions. */
12244 *begin = cp_parser_range_for_member_function (range, id_begin);
12245 *end = cp_parser_range_for_member_function (range, id_end);
12247 else
12249 /* Use global functions with ADL. */
12250 vec<tree, va_gc> *vec;
12251 vec = make_tree_vector ();
12253 vec_safe_push (vec, range);
12255 member_begin = perform_koenig_lookup (id_begin, vec,
12256 tf_warning_or_error);
12257 *begin = finish_call_expr (member_begin, &vec, false, true,
12258 tf_warning_or_error);
12259 member_end = perform_koenig_lookup (id_end, vec,
12260 tf_warning_or_error);
12261 *end = finish_call_expr (member_end, &vec, false, true,
12262 tf_warning_or_error);
12264 release_tree_vector (vec);
12267 /* Last common checks. */
12268 if (*begin == error_mark_node || *end == error_mark_node)
12270 /* If one of the expressions is an error do no more checks. */
12271 *begin = *end = error_mark_node;
12272 return error_mark_node;
12274 else if (type_dependent_expression_p (*begin)
12275 || type_dependent_expression_p (*end))
12276 /* Can happen, when, eg, in a template context, Koenig lookup
12277 can't resolve begin/end (c++/58503). */
12278 return NULL_TREE;
12279 else
12281 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12282 /* The unqualified type of the __begin and __end temporaries should
12283 be the same, as required by the multiple auto declaration. */
12284 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12286 if (cxx_dialect >= cxx17
12287 && (build_x_binary_op (input_location, NE_EXPR,
12288 *begin, ERROR_MARK,
12289 *end, ERROR_MARK,
12290 NULL, tf_none)
12291 != error_mark_node))
12292 /* P0184R0 allows __begin and __end to have different types,
12293 but make sure they are comparable so we can give a better
12294 diagnostic. */;
12295 else
12296 error ("inconsistent begin/end types in range-based %<for%> "
12297 "statement: %qT and %qT",
12298 TREE_TYPE (*begin), TREE_TYPE (*end));
12300 return iter_type;
12305 /* Helper function for cp_parser_perform_range_for_lookup.
12306 Builds a tree for RANGE.IDENTIFIER(). */
12308 static tree
12309 cp_parser_range_for_member_function (tree range, tree identifier)
12311 tree member, res;
12312 vec<tree, va_gc> *vec;
12314 member = finish_class_member_access_expr (range, identifier,
12315 false, tf_warning_or_error);
12316 if (member == error_mark_node)
12317 return error_mark_node;
12319 vec = make_tree_vector ();
12320 res = finish_call_expr (member, &vec,
12321 /*disallow_virtual=*/false,
12322 /*koenig_p=*/false,
12323 tf_warning_or_error);
12324 release_tree_vector (vec);
12325 return res;
12328 /* Parse an iteration-statement.
12330 iteration-statement:
12331 while ( condition ) statement
12332 do statement while ( expression ) ;
12333 for ( init-statement condition [opt] ; expression [opt] )
12334 statement
12336 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12338 static tree
12339 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12340 unsigned short unroll)
12342 cp_token *token;
12343 enum rid keyword;
12344 tree statement;
12345 unsigned char in_statement;
12346 token_indent_info guard_tinfo;
12348 /* Peek at the next token. */
12349 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12350 if (!token)
12351 return error_mark_node;
12353 guard_tinfo = get_token_indent_info (token);
12355 /* Remember whether or not we are already within an iteration
12356 statement. */
12357 in_statement = parser->in_statement;
12359 /* See what kind of keyword it is. */
12360 keyword = token->keyword;
12361 switch (keyword)
12363 case RID_WHILE:
12365 tree condition;
12367 /* Begin the while-statement. */
12368 statement = begin_while_stmt ();
12369 /* Look for the `('. */
12370 matching_parens parens;
12371 parens.require_open (parser);
12372 /* Parse the condition. */
12373 condition = cp_parser_condition (parser);
12374 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12375 /* Look for the `)'. */
12376 parens.require_close (parser);
12377 /* Parse the dependent statement. */
12378 parser->in_statement = IN_ITERATION_STMT;
12379 bool prev = note_iteration_stmt_body_start ();
12380 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12381 note_iteration_stmt_body_end (prev);
12382 parser->in_statement = in_statement;
12383 /* We're done with the while-statement. */
12384 finish_while_stmt (statement);
12386 break;
12388 case RID_DO:
12390 tree expression;
12392 /* Begin the do-statement. */
12393 statement = begin_do_stmt ();
12394 /* Parse the body of the do-statement. */
12395 parser->in_statement = IN_ITERATION_STMT;
12396 bool prev = note_iteration_stmt_body_start ();
12397 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12398 note_iteration_stmt_body_end (prev);
12399 parser->in_statement = in_statement;
12400 finish_do_body (statement);
12401 /* Look for the `while' keyword. */
12402 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12403 /* Look for the `('. */
12404 matching_parens parens;
12405 parens.require_open (parser);
12406 /* Parse the expression. */
12407 expression = cp_parser_expression (parser);
12408 /* We're done with the do-statement. */
12409 finish_do_stmt (expression, statement, ivdep, unroll);
12410 /* Look for the `)'. */
12411 parens.require_close (parser);
12412 /* Look for the `;'. */
12413 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12415 break;
12417 case RID_FOR:
12419 /* Look for the `('. */
12420 matching_parens parens;
12421 parens.require_open (parser);
12423 statement = cp_parser_for (parser, ivdep, unroll);
12425 /* Look for the `)'. */
12426 parens.require_close (parser);
12428 /* Parse the body of the for-statement. */
12429 parser->in_statement = IN_ITERATION_STMT;
12430 bool prev = note_iteration_stmt_body_start ();
12431 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12432 note_iteration_stmt_body_end (prev);
12433 parser->in_statement = in_statement;
12435 /* We're done with the for-statement. */
12436 finish_for_stmt (statement);
12438 break;
12440 default:
12441 cp_parser_error (parser, "expected iteration-statement");
12442 statement = error_mark_node;
12443 break;
12446 return statement;
12449 /* Parse a init-statement or the declarator of a range-based-for.
12450 Returns true if a range-based-for declaration is seen.
12452 init-statement:
12453 expression-statement
12454 simple-declaration */
12456 static bool
12457 cp_parser_init_statement (cp_parser *parser, tree *decl)
12459 /* If the next token is a `;', then we have an empty
12460 expression-statement. Grammatically, this is also a
12461 simple-declaration, but an invalid one, because it does not
12462 declare anything. Therefore, if we did not handle this case
12463 specially, we would issue an error message about an invalid
12464 declaration. */
12465 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12467 bool is_range_for = false;
12468 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12470 /* Try to parse the init-statement. */
12471 if (cp_parser_range_based_for_with_init_p (parser))
12473 tree dummy;
12474 cp_parser_parse_tentatively (parser);
12475 /* Parse the declaration. */
12476 cp_parser_simple_declaration (parser,
12477 /*function_definition_allowed_p=*/false,
12478 &dummy);
12479 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12480 if (!cp_parser_parse_definitely (parser))
12481 /* That didn't work, try to parse it as an expression-statement. */
12482 cp_parser_expression_statement (parser, NULL_TREE);
12484 if (cxx_dialect < cxx2a)
12486 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12487 "range-based %<for%> loops with initializer only "
12488 "available with -std=c++2a or -std=gnu++2a");
12489 *decl = error_mark_node;
12493 /* A colon is used in range-based for. */
12494 parser->colon_corrects_to_scope_p = false;
12496 /* We're going to speculatively look for a declaration, falling back
12497 to an expression, if necessary. */
12498 cp_parser_parse_tentatively (parser);
12499 /* Parse the declaration. */
12500 cp_parser_simple_declaration (parser,
12501 /*function_definition_allowed_p=*/false,
12502 decl);
12503 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12504 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12506 /* It is a range-for, consume the ':'. */
12507 cp_lexer_consume_token (parser->lexer);
12508 is_range_for = true;
12509 if (cxx_dialect < cxx11)
12510 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12511 "range-based %<for%> loops only available with "
12512 "-std=c++11 or -std=gnu++11");
12514 else
12515 /* The ';' is not consumed yet because we told
12516 cp_parser_simple_declaration not to. */
12517 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12519 if (cp_parser_parse_definitely (parser))
12520 return is_range_for;
12521 /* If the tentative parse failed, then we shall need to look for an
12522 expression-statement. */
12524 /* If we are here, it is an expression-statement. */
12525 cp_parser_expression_statement (parser, NULL_TREE);
12526 return false;
12529 /* Parse a jump-statement.
12531 jump-statement:
12532 break ;
12533 continue ;
12534 return expression [opt] ;
12535 return braced-init-list ;
12536 goto identifier ;
12538 GNU extension:
12540 jump-statement:
12541 goto * expression ;
12543 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12545 static tree
12546 cp_parser_jump_statement (cp_parser* parser)
12548 tree statement = error_mark_node;
12549 cp_token *token;
12550 enum rid keyword;
12551 unsigned char in_statement;
12553 /* Peek at the next token. */
12554 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12555 if (!token)
12556 return error_mark_node;
12558 /* See what kind of keyword it is. */
12559 keyword = token->keyword;
12560 switch (keyword)
12562 case RID_BREAK:
12563 in_statement = parser->in_statement & ~IN_IF_STMT;
12564 switch (in_statement)
12566 case 0:
12567 error_at (token->location, "break statement not within loop or switch");
12568 break;
12569 default:
12570 gcc_assert ((in_statement & IN_SWITCH_STMT)
12571 || in_statement == IN_ITERATION_STMT);
12572 statement = finish_break_stmt ();
12573 if (in_statement == IN_ITERATION_STMT)
12574 break_maybe_infinite_loop ();
12575 break;
12576 case IN_OMP_BLOCK:
12577 error_at (token->location, "invalid exit from OpenMP structured block");
12578 break;
12579 case IN_OMP_FOR:
12580 error_at (token->location, "break statement used with OpenMP for loop");
12581 break;
12583 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12584 break;
12586 case RID_CONTINUE:
12587 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12589 case 0:
12590 error_at (token->location, "continue statement not within a loop");
12591 break;
12592 /* Fall through. */
12593 case IN_ITERATION_STMT:
12594 case IN_OMP_FOR:
12595 statement = finish_continue_stmt ();
12596 break;
12597 case IN_OMP_BLOCK:
12598 error_at (token->location, "invalid exit from OpenMP structured block");
12599 break;
12600 default:
12601 gcc_unreachable ();
12603 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12604 break;
12606 case RID_RETURN:
12608 tree expr;
12609 bool expr_non_constant_p;
12611 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12613 cp_lexer_set_source_position (parser->lexer);
12614 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12615 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12617 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12618 expr = cp_parser_expression (parser);
12619 else
12620 /* If the next token is a `;', then there is no
12621 expression. */
12622 expr = NULL_TREE;
12623 /* Build the return-statement. */
12624 if (current_function_auto_return_pattern && in_discarded_stmt)
12625 /* Don't deduce from a discarded return statement. */;
12626 else
12627 statement = finish_return_stmt (expr);
12628 /* Look for the final `;'. */
12629 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12631 break;
12633 case RID_GOTO:
12634 if (parser->in_function_body
12635 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12637 error ("%<goto%> in %<constexpr%> function");
12638 cp_function_chain->invalid_constexpr = true;
12641 /* Create the goto-statement. */
12642 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12644 /* Issue a warning about this use of a GNU extension. */
12645 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12646 /* Consume the '*' token. */
12647 cp_lexer_consume_token (parser->lexer);
12648 /* Parse the dependent expression. */
12649 finish_goto_stmt (cp_parser_expression (parser));
12651 else
12652 finish_goto_stmt (cp_parser_identifier (parser));
12653 /* Look for the final `;'. */
12654 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12655 break;
12657 default:
12658 cp_parser_error (parser, "expected jump-statement");
12659 break;
12662 return statement;
12665 /* Parse a declaration-statement.
12667 declaration-statement:
12668 block-declaration */
12670 static void
12671 cp_parser_declaration_statement (cp_parser* parser)
12673 void *p;
12675 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12676 p = obstack_alloc (&declarator_obstack, 0);
12678 /* Parse the block-declaration. */
12679 cp_parser_block_declaration (parser, /*statement_p=*/true);
12681 /* Free any declarators allocated. */
12682 obstack_free (&declarator_obstack, p);
12685 /* Some dependent statements (like `if (cond) statement'), are
12686 implicitly in their own scope. In other words, if the statement is
12687 a single statement (as opposed to a compound-statement), it is
12688 none-the-less treated as if it were enclosed in braces. Any
12689 declarations appearing in the dependent statement are out of scope
12690 after control passes that point. This function parses a statement,
12691 but ensures that is in its own scope, even if it is not a
12692 compound-statement.
12694 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12695 is a (possibly labeled) if statement which is not enclosed in
12696 braces and has an else clause. This is used to implement
12697 -Wparentheses.
12699 CHAIN is a vector of if-else-if conditions. This is used to implement
12700 -Wduplicated-cond.
12702 Returns the new statement. */
12704 static tree
12705 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12706 const token_indent_info &guard_tinfo,
12707 vec<tree> *chain)
12709 tree statement;
12710 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12711 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12712 token_indent_info body_tinfo
12713 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12715 if (if_p != NULL)
12716 *if_p = false;
12718 /* Mark if () ; with a special NOP_EXPR. */
12719 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12721 cp_lexer_consume_token (parser->lexer);
12722 statement = add_stmt (build_empty_stmt (body_loc));
12724 if (guard_tinfo.keyword == RID_IF
12725 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12726 warning_at (body_loc, OPT_Wempty_body,
12727 "suggest braces around empty body in an %<if%> statement");
12728 else if (guard_tinfo.keyword == RID_ELSE)
12729 warning_at (body_loc, OPT_Wempty_body,
12730 "suggest braces around empty body in an %<else%> statement");
12732 /* if a compound is opened, we simply parse the statement directly. */
12733 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12734 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12735 /* If the token is not a `{', then we must take special action. */
12736 else
12738 /* Create a compound-statement. */
12739 statement = begin_compound_stmt (0);
12740 /* Parse the dependent-statement. */
12741 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12742 &body_loc_after_labels);
12743 /* Finish the dummy compound-statement. */
12744 finish_compound_stmt (statement);
12747 token_indent_info next_tinfo
12748 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12749 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12751 if (body_loc_after_labels != UNKNOWN_LOCATION
12752 && next_tinfo.type != CPP_SEMICOLON)
12753 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12754 guard_tinfo.location, guard_tinfo.keyword);
12756 /* Return the statement. */
12757 return statement;
12760 /* For some dependent statements (like `while (cond) statement'), we
12761 have already created a scope. Therefore, even if the dependent
12762 statement is a compound-statement, we do not want to create another
12763 scope. */
12765 static void
12766 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12767 const token_indent_info &guard_tinfo)
12769 /* If the token is a `{', then we must take special action. */
12770 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12772 token_indent_info body_tinfo
12773 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12774 location_t loc_after_labels = UNKNOWN_LOCATION;
12776 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12777 &loc_after_labels);
12778 token_indent_info next_tinfo
12779 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12780 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12782 if (loc_after_labels != UNKNOWN_LOCATION
12783 && next_tinfo.type != CPP_SEMICOLON)
12784 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12785 guard_tinfo.location,
12786 guard_tinfo.keyword);
12788 else
12790 /* Avoid calling cp_parser_compound_statement, so that we
12791 don't create a new scope. Do everything else by hand. */
12792 matching_braces braces;
12793 braces.require_open (parser);
12794 /* If the next keyword is `__label__' we have a label declaration. */
12795 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12796 cp_parser_label_declaration (parser);
12797 /* Parse an (optional) statement-seq. */
12798 cp_parser_statement_seq_opt (parser, NULL_TREE);
12799 braces.require_close (parser);
12803 /* Declarations [gram.dcl.dcl] */
12805 /* Parse an optional declaration-sequence.
12807 declaration-seq:
12808 declaration
12809 declaration-seq declaration */
12811 static void
12812 cp_parser_declaration_seq_opt (cp_parser* parser)
12814 while (true)
12816 cp_token *token = cp_lexer_peek_token (parser->lexer);
12818 if (token->type == CPP_CLOSE_BRACE
12819 || token->type == CPP_EOF)
12820 break;
12821 else
12822 cp_parser_toplevel_declaration (parser);
12826 /* Parse a declaration.
12828 declaration:
12829 block-declaration
12830 function-definition
12831 template-declaration
12832 explicit-instantiation
12833 explicit-specialization
12834 linkage-specification
12835 namespace-definition
12837 C++17:
12838 deduction-guide
12840 GNU extension:
12842 declaration:
12843 __extension__ declaration */
12845 static void
12846 cp_parser_declaration (cp_parser* parser)
12848 cp_token token1;
12849 cp_token token2;
12850 int saved_pedantic;
12851 void *p;
12852 tree attributes = NULL_TREE;
12854 /* Check for the `__extension__' keyword. */
12855 if (cp_parser_extension_opt (parser, &saved_pedantic))
12857 /* Parse the qualified declaration. */
12858 cp_parser_declaration (parser);
12859 /* Restore the PEDANTIC flag. */
12860 pedantic = saved_pedantic;
12862 return;
12865 /* Try to figure out what kind of declaration is present. */
12866 token1 = *cp_lexer_peek_token (parser->lexer);
12868 if (token1.type != CPP_EOF)
12869 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12870 else
12872 token2.type = CPP_EOF;
12873 token2.keyword = RID_MAX;
12876 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12877 p = obstack_alloc (&declarator_obstack, 0);
12879 /* If the next token is `extern' and the following token is a string
12880 literal, then we have a linkage specification. */
12881 if (token1.keyword == RID_EXTERN
12882 && cp_parser_is_pure_string_literal (&token2))
12883 cp_parser_linkage_specification (parser);
12884 /* If the next token is `template', then we have either a template
12885 declaration, an explicit instantiation, or an explicit
12886 specialization. */
12887 else if (token1.keyword == RID_TEMPLATE)
12889 /* `template <>' indicates a template specialization. */
12890 if (token2.type == CPP_LESS
12891 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12892 cp_parser_explicit_specialization (parser);
12893 /* `template <' indicates a template declaration. */
12894 else if (token2.type == CPP_LESS)
12895 cp_parser_template_declaration (parser, /*member_p=*/false);
12896 /* Anything else must be an explicit instantiation. */
12897 else
12898 cp_parser_explicit_instantiation (parser);
12900 /* If the next token is `export', then we have a template
12901 declaration. */
12902 else if (token1.keyword == RID_EXPORT)
12903 cp_parser_template_declaration (parser, /*member_p=*/false);
12904 /* If the next token is `extern', 'static' or 'inline' and the one
12905 after that is `template', we have a GNU extended explicit
12906 instantiation directive. */
12907 else if (cp_parser_allow_gnu_extensions_p (parser)
12908 && (token1.keyword == RID_EXTERN
12909 || token1.keyword == RID_STATIC
12910 || token1.keyword == RID_INLINE)
12911 && token2.keyword == RID_TEMPLATE)
12912 cp_parser_explicit_instantiation (parser);
12913 /* If the next token is `namespace', check for a named or unnamed
12914 namespace definition. */
12915 else if (token1.keyword == RID_NAMESPACE
12916 && (/* A named namespace definition. */
12917 (token2.type == CPP_NAME
12918 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12919 != CPP_EQ))
12920 || (token2.type == CPP_OPEN_SQUARE
12921 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12922 == CPP_OPEN_SQUARE)
12923 /* An unnamed namespace definition. */
12924 || token2.type == CPP_OPEN_BRACE
12925 || token2.keyword == RID_ATTRIBUTE))
12926 cp_parser_namespace_definition (parser);
12927 /* An inline (associated) namespace definition. */
12928 else if (token1.keyword == RID_INLINE
12929 && token2.keyword == RID_NAMESPACE)
12930 cp_parser_namespace_definition (parser);
12931 /* Objective-C++ declaration/definition. */
12932 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12933 cp_parser_objc_declaration (parser, NULL_TREE);
12934 else if (c_dialect_objc ()
12935 && token1.keyword == RID_ATTRIBUTE
12936 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12937 cp_parser_objc_declaration (parser, attributes);
12938 /* At this point we may have a template declared by a concept
12939 introduction. */
12940 else if (flag_concepts
12941 && cp_parser_template_declaration_after_export (parser,
12942 /*member_p=*/false))
12943 /* We did. */;
12944 else
12945 /* Try to parse a block-declaration, or a function-definition. */
12946 cp_parser_block_declaration (parser, /*statement_p=*/false);
12948 /* Free any declarators allocated. */
12949 obstack_free (&declarator_obstack, p);
12952 /* Parse a namespace-scope declaration. */
12954 static void
12955 cp_parser_toplevel_declaration (cp_parser* parser)
12957 cp_token *token = cp_lexer_peek_token (parser->lexer);
12959 if (token->type == CPP_PRAGMA)
12960 /* A top-level declaration can consist solely of a #pragma. A
12961 nested declaration cannot, so this is done here and not in
12962 cp_parser_declaration. (A #pragma at block scope is
12963 handled in cp_parser_statement.) */
12964 cp_parser_pragma (parser, pragma_external, NULL);
12965 else if (token->type == CPP_SEMICOLON)
12967 /* A declaration consisting of a single semicolon is
12968 invalid. Allow it unless we're being pedantic. */
12969 cp_lexer_consume_token (parser->lexer);
12970 if (!in_system_header_at (input_location))
12971 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12973 else
12974 /* Parse the declaration itself. */
12975 cp_parser_declaration (parser);
12978 /* Parse a block-declaration.
12980 block-declaration:
12981 simple-declaration
12982 asm-definition
12983 namespace-alias-definition
12984 using-declaration
12985 using-directive
12987 GNU Extension:
12989 block-declaration:
12990 __extension__ block-declaration
12992 C++0x Extension:
12994 block-declaration:
12995 static_assert-declaration
12997 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12998 part of a declaration-statement. */
13000 static void
13001 cp_parser_block_declaration (cp_parser *parser,
13002 bool statement_p)
13004 cp_token *token1;
13005 int saved_pedantic;
13007 /* Check for the `__extension__' keyword. */
13008 if (cp_parser_extension_opt (parser, &saved_pedantic))
13010 /* Parse the qualified declaration. */
13011 cp_parser_block_declaration (parser, statement_p);
13012 /* Restore the PEDANTIC flag. */
13013 pedantic = saved_pedantic;
13015 return;
13018 /* Peek at the next token to figure out which kind of declaration is
13019 present. */
13020 token1 = cp_lexer_peek_token (parser->lexer);
13022 /* If the next keyword is `asm', we have an asm-definition. */
13023 if (token1->keyword == RID_ASM)
13025 if (statement_p)
13026 cp_parser_commit_to_tentative_parse (parser);
13027 cp_parser_asm_definition (parser);
13029 /* If the next keyword is `namespace', we have a
13030 namespace-alias-definition. */
13031 else if (token1->keyword == RID_NAMESPACE)
13032 cp_parser_namespace_alias_definition (parser);
13033 /* If the next keyword is `using', we have a
13034 using-declaration, a using-directive, or an alias-declaration. */
13035 else if (token1->keyword == RID_USING)
13037 cp_token *token2;
13039 if (statement_p)
13040 cp_parser_commit_to_tentative_parse (parser);
13041 /* If the token after `using' is `namespace', then we have a
13042 using-directive. */
13043 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13044 if (token2->keyword == RID_NAMESPACE)
13045 cp_parser_using_directive (parser);
13046 /* If the second token after 'using' is '=', then we have an
13047 alias-declaration. */
13048 else if (cxx_dialect >= cxx11
13049 && token2->type == CPP_NAME
13050 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13051 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13052 cp_parser_alias_declaration (parser);
13053 /* Otherwise, it's a using-declaration. */
13054 else
13055 cp_parser_using_declaration (parser,
13056 /*access_declaration_p=*/false);
13058 /* If the next keyword is `__label__' we have a misplaced label
13059 declaration. */
13060 else if (token1->keyword == RID_LABEL)
13062 cp_lexer_consume_token (parser->lexer);
13063 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13064 cp_parser_skip_to_end_of_statement (parser);
13065 /* If the next token is now a `;', consume it. */
13066 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13067 cp_lexer_consume_token (parser->lexer);
13069 /* If the next token is `static_assert' we have a static assertion. */
13070 else if (token1->keyword == RID_STATIC_ASSERT)
13071 cp_parser_static_assert (parser, /*member_p=*/false);
13072 /* Anything else must be a simple-declaration. */
13073 else
13074 cp_parser_simple_declaration (parser, !statement_p,
13075 /*maybe_range_for_decl*/NULL);
13078 /* Parse a simple-declaration.
13080 simple-declaration:
13081 decl-specifier-seq [opt] init-declarator-list [opt] ;
13082 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13083 brace-or-equal-initializer ;
13085 init-declarator-list:
13086 init-declarator
13087 init-declarator-list , init-declarator
13089 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13090 function-definition as a simple-declaration.
13092 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13093 parsed declaration if it is an uninitialized single declarator not followed
13094 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13095 if present, will not be consumed. */
13097 static void
13098 cp_parser_simple_declaration (cp_parser* parser,
13099 bool function_definition_allowed_p,
13100 tree *maybe_range_for_decl)
13102 cp_decl_specifier_seq decl_specifiers;
13103 int declares_class_or_enum;
13104 bool saw_declarator;
13105 location_t comma_loc = UNKNOWN_LOCATION;
13106 location_t init_loc = UNKNOWN_LOCATION;
13108 if (maybe_range_for_decl)
13109 *maybe_range_for_decl = NULL_TREE;
13111 /* Defer access checks until we know what is being declared; the
13112 checks for names appearing in the decl-specifier-seq should be
13113 done as if we were in the scope of the thing being declared. */
13114 push_deferring_access_checks (dk_deferred);
13116 /* Parse the decl-specifier-seq. We have to keep track of whether
13117 or not the decl-specifier-seq declares a named class or
13118 enumeration type, since that is the only case in which the
13119 init-declarator-list is allowed to be empty.
13121 [dcl.dcl]
13123 In a simple-declaration, the optional init-declarator-list can be
13124 omitted only when declaring a class or enumeration, that is when
13125 the decl-specifier-seq contains either a class-specifier, an
13126 elaborated-type-specifier, or an enum-specifier. */
13127 cp_parser_decl_specifier_seq (parser,
13128 CP_PARSER_FLAGS_OPTIONAL,
13129 &decl_specifiers,
13130 &declares_class_or_enum);
13131 /* We no longer need to defer access checks. */
13132 stop_deferring_access_checks ();
13134 /* In a block scope, a valid declaration must always have a
13135 decl-specifier-seq. By not trying to parse declarators, we can
13136 resolve the declaration/expression ambiguity more quickly. */
13137 if (!function_definition_allowed_p
13138 && !decl_specifiers.any_specifiers_p)
13140 cp_parser_error (parser, "expected declaration");
13141 goto done;
13144 /* If the next two tokens are both identifiers, the code is
13145 erroneous. The usual cause of this situation is code like:
13147 T t;
13149 where "T" should name a type -- but does not. */
13150 if (!decl_specifiers.any_type_specifiers_p
13151 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13153 /* If parsing tentatively, we should commit; we really are
13154 looking at a declaration. */
13155 cp_parser_commit_to_tentative_parse (parser);
13156 /* Give up. */
13157 goto done;
13160 cp_parser_maybe_commit_to_declaration (parser,
13161 decl_specifiers.any_specifiers_p);
13163 /* Look for C++17 decomposition declaration. */
13164 for (size_t n = 1; ; n++)
13165 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13166 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13167 continue;
13168 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13169 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13170 && decl_specifiers.any_specifiers_p)
13172 tree decl
13173 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13174 maybe_range_for_decl,
13175 &init_loc);
13177 /* The next token should be either a `,' or a `;'. */
13178 cp_token *token = cp_lexer_peek_token (parser->lexer);
13179 /* If it's a `;', we are done. */
13180 if (token->type == CPP_SEMICOLON)
13181 goto finish;
13182 else if (maybe_range_for_decl)
13184 if (*maybe_range_for_decl == NULL_TREE)
13185 *maybe_range_for_decl = error_mark_node;
13186 goto finish;
13188 /* Anything else is an error. */
13189 else
13191 /* If we have already issued an error message we don't need
13192 to issue another one. */
13193 if ((decl != error_mark_node
13194 && DECL_INITIAL (decl) != error_mark_node)
13195 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13196 cp_parser_error (parser, "expected %<,%> or %<;%>");
13197 /* Skip tokens until we reach the end of the statement. */
13198 cp_parser_skip_to_end_of_statement (parser);
13199 /* If the next token is now a `;', consume it. */
13200 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13201 cp_lexer_consume_token (parser->lexer);
13202 goto done;
13205 else
13206 break;
13208 tree last_type;
13209 bool auto_specifier_p;
13210 /* NULL_TREE if both variable and function declaration are allowed,
13211 error_mark_node if function declaration are not allowed and
13212 a FUNCTION_DECL that should be diagnosed if it is followed by
13213 variable declarations. */
13214 tree auto_function_declaration;
13216 last_type = NULL_TREE;
13217 auto_specifier_p
13218 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13219 auto_function_declaration = NULL_TREE;
13221 /* Keep going until we hit the `;' at the end of the simple
13222 declaration. */
13223 saw_declarator = false;
13224 while (cp_lexer_next_token_is_not (parser->lexer,
13225 CPP_SEMICOLON))
13227 cp_token *token;
13228 bool function_definition_p;
13229 tree decl;
13230 tree auto_result = NULL_TREE;
13232 if (saw_declarator)
13234 /* If we are processing next declarator, comma is expected */
13235 token = cp_lexer_peek_token (parser->lexer);
13236 gcc_assert (token->type == CPP_COMMA);
13237 cp_lexer_consume_token (parser->lexer);
13238 if (maybe_range_for_decl)
13240 *maybe_range_for_decl = error_mark_node;
13241 if (comma_loc == UNKNOWN_LOCATION)
13242 comma_loc = token->location;
13245 else
13246 saw_declarator = true;
13248 /* Parse the init-declarator. */
13249 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13250 /*checks=*/NULL,
13251 function_definition_allowed_p,
13252 /*member_p=*/false,
13253 declares_class_or_enum,
13254 &function_definition_p,
13255 maybe_range_for_decl,
13256 &init_loc,
13257 &auto_result);
13258 /* If an error occurred while parsing tentatively, exit quickly.
13259 (That usually happens when in the body of a function; each
13260 statement is treated as a declaration-statement until proven
13261 otherwise.) */
13262 if (cp_parser_error_occurred (parser))
13263 goto done;
13265 if (auto_specifier_p && cxx_dialect >= cxx14)
13267 /* If the init-declarator-list contains more than one
13268 init-declarator, they shall all form declarations of
13269 variables. */
13270 if (auto_function_declaration == NULL_TREE)
13271 auto_function_declaration
13272 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13273 else if (TREE_CODE (decl) == FUNCTION_DECL
13274 || auto_function_declaration != error_mark_node)
13276 error_at (decl_specifiers.locations[ds_type_spec],
13277 "non-variable %qD in declaration with more than one "
13278 "declarator with placeholder type",
13279 TREE_CODE (decl) == FUNCTION_DECL
13280 ? decl : auto_function_declaration);
13281 auto_function_declaration = error_mark_node;
13285 if (auto_result
13286 && (!processing_template_decl || !type_uses_auto (auto_result)))
13288 if (last_type
13289 && last_type != error_mark_node
13290 && !same_type_p (auto_result, last_type))
13292 /* If the list of declarators contains more than one declarator,
13293 the type of each declared variable is determined as described
13294 above. If the type deduced for the template parameter U is not
13295 the same in each deduction, the program is ill-formed. */
13296 error_at (decl_specifiers.locations[ds_type_spec],
13297 "inconsistent deduction for %qT: %qT and then %qT",
13298 decl_specifiers.type, last_type, auto_result);
13299 last_type = error_mark_node;
13301 else
13302 last_type = auto_result;
13305 /* Handle function definitions specially. */
13306 if (function_definition_p)
13308 /* If the next token is a `,', then we are probably
13309 processing something like:
13311 void f() {}, *p;
13313 which is erroneous. */
13314 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13316 cp_token *token = cp_lexer_peek_token (parser->lexer);
13317 error_at (token->location,
13318 "mixing"
13319 " declarations and function-definitions is forbidden");
13321 /* Otherwise, we're done with the list of declarators. */
13322 else
13324 pop_deferring_access_checks ();
13325 return;
13328 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13329 *maybe_range_for_decl = decl;
13330 /* The next token should be either a `,' or a `;'. */
13331 token = cp_lexer_peek_token (parser->lexer);
13332 /* If it's a `,', there are more declarators to come. */
13333 if (token->type == CPP_COMMA)
13334 /* will be consumed next time around */;
13335 /* If it's a `;', we are done. */
13336 else if (token->type == CPP_SEMICOLON)
13337 break;
13338 else if (maybe_range_for_decl)
13340 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13341 permerror (decl_specifiers.locations[ds_type_spec],
13342 "types may not be defined in a for-range-declaration");
13343 break;
13345 /* Anything else is an error. */
13346 else
13348 /* If we have already issued an error message we don't need
13349 to issue another one. */
13350 if ((decl != error_mark_node
13351 && DECL_INITIAL (decl) != error_mark_node)
13352 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13353 cp_parser_error (parser, "expected %<,%> or %<;%>");
13354 /* Skip tokens until we reach the end of the statement. */
13355 cp_parser_skip_to_end_of_statement (parser);
13356 /* If the next token is now a `;', consume it. */
13357 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13358 cp_lexer_consume_token (parser->lexer);
13359 goto done;
13361 /* After the first time around, a function-definition is not
13362 allowed -- even if it was OK at first. For example:
13364 int i, f() {}
13366 is not valid. */
13367 function_definition_allowed_p = false;
13370 /* Issue an error message if no declarators are present, and the
13371 decl-specifier-seq does not itself declare a class or
13372 enumeration: [dcl.dcl]/3. */
13373 if (!saw_declarator)
13375 if (cp_parser_declares_only_class_p (parser))
13377 if (!declares_class_or_enum
13378 && decl_specifiers.type
13379 && OVERLOAD_TYPE_P (decl_specifiers.type))
13380 /* Ensure an error is issued anyway when finish_decltype_type,
13381 called via cp_parser_decl_specifier_seq, returns a class or
13382 an enumeration (c++/51786). */
13383 decl_specifiers.type = NULL_TREE;
13384 shadow_tag (&decl_specifiers);
13386 /* Perform any deferred access checks. */
13387 perform_deferred_access_checks (tf_warning_or_error);
13390 /* Consume the `;'. */
13391 finish:
13392 if (!maybe_range_for_decl)
13393 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13394 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13396 if (init_loc != UNKNOWN_LOCATION)
13397 error_at (init_loc, "initializer in range-based %<for%> loop");
13398 if (comma_loc != UNKNOWN_LOCATION)
13399 error_at (comma_loc,
13400 "multiple declarations in range-based %<for%> loop");
13403 done:
13404 pop_deferring_access_checks ();
13407 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13408 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13409 initializer ; */
13411 static tree
13412 cp_parser_decomposition_declaration (cp_parser *parser,
13413 cp_decl_specifier_seq *decl_specifiers,
13414 tree *maybe_range_for_decl,
13415 location_t *init_loc)
13417 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13418 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13419 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13421 /* Parse the identifier-list. */
13422 auto_vec<cp_expr, 10> v;
13423 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13424 while (true)
13426 cp_expr e = cp_parser_identifier (parser);
13427 if (e.get_value () == error_mark_node)
13428 break;
13429 v.safe_push (e);
13430 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13431 break;
13432 cp_lexer_consume_token (parser->lexer);
13435 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13436 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13438 end_loc = UNKNOWN_LOCATION;
13439 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13440 false);
13441 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13442 cp_lexer_consume_token (parser->lexer);
13443 else
13445 cp_parser_skip_to_end_of_statement (parser);
13446 return error_mark_node;
13450 if (cxx_dialect < cxx17)
13451 pedwarn (loc, 0, "structured bindings only available with "
13452 "-std=c++17 or -std=gnu++17");
13454 tree pushed_scope;
13455 cp_declarator *declarator = make_declarator (cdk_decomp);
13456 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13457 declarator->id_loc = loc;
13458 if (ref_qual != REF_QUAL_NONE)
13459 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13460 ref_qual == REF_QUAL_RVALUE,
13461 NULL_TREE);
13462 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13463 NULL_TREE, decl_specifiers->attributes,
13464 &pushed_scope);
13465 tree orig_decl = decl;
13467 unsigned int i;
13468 cp_expr e;
13469 cp_decl_specifier_seq decl_specs;
13470 clear_decl_specs (&decl_specs);
13471 decl_specs.type = make_auto ();
13472 tree prev = decl;
13473 FOR_EACH_VEC_ELT (v, i, e)
13475 if (i == 0)
13476 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13477 else
13478 declarator->u.id.unqualified_name = e.get_value ();
13479 declarator->id_loc = e.get_location ();
13480 tree elt_pushed_scope;
13481 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13482 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13483 if (decl2 == error_mark_node)
13484 decl = error_mark_node;
13485 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13487 /* Ensure we've diagnosed redeclaration if we aren't creating
13488 a new VAR_DECL. */
13489 gcc_assert (errorcount);
13490 decl = error_mark_node;
13492 else
13493 prev = decl2;
13494 if (elt_pushed_scope)
13495 pop_scope (elt_pushed_scope);
13498 if (v.is_empty ())
13500 error_at (loc, "empty structured binding declaration");
13501 decl = error_mark_node;
13504 if (maybe_range_for_decl == NULL
13505 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13507 bool non_constant_p = false, is_direct_init = false;
13508 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13509 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13510 &non_constant_p);
13511 if (initializer == NULL_TREE
13512 || (TREE_CODE (initializer) == TREE_LIST
13513 && TREE_CHAIN (initializer))
13514 || (is_direct_init
13515 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13516 && CONSTRUCTOR_NELTS (initializer) != 1))
13518 error_at (loc, "invalid initializer for structured binding "
13519 "declaration");
13520 initializer = error_mark_node;
13523 if (decl != error_mark_node)
13525 cp_maybe_mangle_decomp (decl, prev, v.length ());
13526 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13527 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13528 cp_finish_decomp (decl, prev, v.length ());
13531 else if (decl != error_mark_node)
13533 *maybe_range_for_decl = prev;
13534 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13535 the underlying DECL. */
13536 cp_finish_decomp (decl, prev, v.length ());
13539 if (pushed_scope)
13540 pop_scope (pushed_scope);
13542 if (decl == error_mark_node && DECL_P (orig_decl))
13544 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13545 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13548 return decl;
13551 /* Parse a decl-specifier-seq.
13553 decl-specifier-seq:
13554 decl-specifier-seq [opt] decl-specifier
13555 decl-specifier attribute-specifier-seq [opt] (C++11)
13557 decl-specifier:
13558 storage-class-specifier
13559 type-specifier
13560 function-specifier
13561 friend
13562 typedef
13564 GNU Extension:
13566 decl-specifier:
13567 attributes
13569 Concepts Extension:
13571 decl-specifier:
13572 concept
13574 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13576 The parser flags FLAGS is used to control type-specifier parsing.
13578 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13579 flags:
13581 1: one of the decl-specifiers is an elaborated-type-specifier
13582 (i.e., a type declaration)
13583 2: one of the decl-specifiers is an enum-specifier or a
13584 class-specifier (i.e., a type definition)
13588 static void
13589 cp_parser_decl_specifier_seq (cp_parser* parser,
13590 cp_parser_flags flags,
13591 cp_decl_specifier_seq *decl_specs,
13592 int* declares_class_or_enum)
13594 bool constructor_possible_p = !parser->in_declarator_p;
13595 bool found_decl_spec = false;
13596 cp_token *start_token = NULL;
13597 cp_decl_spec ds;
13599 /* Clear DECL_SPECS. */
13600 clear_decl_specs (decl_specs);
13602 /* Assume no class or enumeration type is declared. */
13603 *declares_class_or_enum = 0;
13605 /* Keep reading specifiers until there are no more to read. */
13606 while (true)
13608 bool constructor_p;
13609 cp_token *token;
13610 ds = ds_last;
13612 /* Peek at the next token. */
13613 token = cp_lexer_peek_token (parser->lexer);
13615 /* Save the first token of the decl spec list for error
13616 reporting. */
13617 if (!start_token)
13618 start_token = token;
13619 /* Handle attributes. */
13620 if (cp_next_tokens_can_be_attribute_p (parser))
13622 /* Parse the attributes. */
13623 tree attrs = cp_parser_attributes_opt (parser);
13625 /* In a sequence of declaration specifiers, c++11 attributes
13626 appertain to the type that precede them. In that case
13627 [dcl.spec]/1 says:
13629 The attribute-specifier-seq affects the type only for
13630 the declaration it appears in, not other declarations
13631 involving the same type.
13633 But for now let's force the user to position the
13634 attribute either at the beginning of the declaration or
13635 after the declarator-id, which would clearly mean that it
13636 applies to the declarator. */
13637 if (cxx11_attribute_p (attrs))
13639 if (!found_decl_spec)
13640 /* The c++11 attribute is at the beginning of the
13641 declaration. It appertains to the entity being
13642 declared. */;
13643 else
13645 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13647 /* This is an attribute following a
13648 class-specifier. */
13649 if (decl_specs->type_definition_p)
13650 warn_misplaced_attr_for_class_type (token->location,
13651 decl_specs->type);
13652 attrs = NULL_TREE;
13654 else
13656 decl_specs->std_attributes
13657 = attr_chainon (decl_specs->std_attributes, attrs);
13658 if (decl_specs->locations[ds_std_attribute] == 0)
13659 decl_specs->locations[ds_std_attribute] = token->location;
13661 continue;
13665 decl_specs->attributes
13666 = attr_chainon (decl_specs->attributes, attrs);
13667 if (decl_specs->locations[ds_attribute] == 0)
13668 decl_specs->locations[ds_attribute] = token->location;
13669 continue;
13671 /* Assume we will find a decl-specifier keyword. */
13672 found_decl_spec = true;
13673 /* If the next token is an appropriate keyword, we can simply
13674 add it to the list. */
13675 switch (token->keyword)
13677 /* decl-specifier:
13678 friend
13679 constexpr */
13680 case RID_FRIEND:
13681 if (!at_class_scope_p ())
13683 gcc_rich_location richloc (token->location);
13684 richloc.add_fixit_remove ();
13685 error_at (&richloc, "%<friend%> used outside of class");
13686 cp_lexer_purge_token (parser->lexer);
13688 else
13690 ds = ds_friend;
13691 /* Consume the token. */
13692 cp_lexer_consume_token (parser->lexer);
13694 break;
13696 case RID_CONSTEXPR:
13697 ds = ds_constexpr;
13698 cp_lexer_consume_token (parser->lexer);
13699 break;
13701 case RID_CONCEPT:
13702 ds = ds_concept;
13703 cp_lexer_consume_token (parser->lexer);
13704 break;
13706 /* function-specifier:
13707 inline
13708 virtual
13709 explicit */
13710 case RID_INLINE:
13711 case RID_VIRTUAL:
13712 case RID_EXPLICIT:
13713 cp_parser_function_specifier_opt (parser, decl_specs);
13714 break;
13716 /* decl-specifier:
13717 typedef */
13718 case RID_TYPEDEF:
13719 ds = ds_typedef;
13720 /* Consume the token. */
13721 cp_lexer_consume_token (parser->lexer);
13722 /* A constructor declarator cannot appear in a typedef. */
13723 constructor_possible_p = false;
13724 /* The "typedef" keyword can only occur in a declaration; we
13725 may as well commit at this point. */
13726 cp_parser_commit_to_tentative_parse (parser);
13728 if (decl_specs->storage_class != sc_none)
13729 decl_specs->conflicting_specifiers_p = true;
13730 break;
13732 /* storage-class-specifier:
13733 auto
13734 register
13735 static
13736 extern
13737 mutable
13739 GNU Extension:
13740 thread */
13741 case RID_AUTO:
13742 if (cxx_dialect == cxx98)
13744 /* Consume the token. */
13745 cp_lexer_consume_token (parser->lexer);
13747 /* Complain about `auto' as a storage specifier, if
13748 we're complaining about C++0x compatibility. */
13749 gcc_rich_location richloc (token->location);
13750 richloc.add_fixit_remove ();
13751 warning_at (&richloc, OPT_Wc__11_compat,
13752 "%<auto%> changes meaning in C++11; "
13753 "please remove it");
13755 /* Set the storage class anyway. */
13756 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13757 token);
13759 else
13760 /* C++0x auto type-specifier. */
13761 found_decl_spec = false;
13762 break;
13764 case RID_REGISTER:
13765 case RID_STATIC:
13766 case RID_EXTERN:
13767 case RID_MUTABLE:
13768 /* Consume the token. */
13769 cp_lexer_consume_token (parser->lexer);
13770 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13771 token);
13772 break;
13773 case RID_THREAD:
13774 /* Consume the token. */
13775 ds = ds_thread;
13776 cp_lexer_consume_token (parser->lexer);
13777 break;
13779 default:
13780 /* We did not yet find a decl-specifier yet. */
13781 found_decl_spec = false;
13782 break;
13785 if (found_decl_spec
13786 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13787 && token->keyword != RID_CONSTEXPR)
13788 error ("decl-specifier invalid in condition");
13790 if (found_decl_spec
13791 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13792 && token->keyword != RID_MUTABLE
13793 && token->keyword != RID_CONSTEXPR)
13794 error_at (token->location, "%qD invalid in lambda",
13795 ridpointers[token->keyword]);
13797 if (ds != ds_last)
13798 set_and_check_decl_spec_loc (decl_specs, ds, token);
13800 /* Constructors are a special case. The `S' in `S()' is not a
13801 decl-specifier; it is the beginning of the declarator. */
13802 constructor_p
13803 = (!found_decl_spec
13804 && constructor_possible_p
13805 && (cp_parser_constructor_declarator_p
13806 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13808 /* If we don't have a DECL_SPEC yet, then we must be looking at
13809 a type-specifier. */
13810 if (!found_decl_spec && !constructor_p)
13812 int decl_spec_declares_class_or_enum;
13813 bool is_cv_qualifier;
13814 tree type_spec;
13816 type_spec
13817 = cp_parser_type_specifier (parser, flags,
13818 decl_specs,
13819 /*is_declaration=*/true,
13820 &decl_spec_declares_class_or_enum,
13821 &is_cv_qualifier);
13822 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13824 /* If this type-specifier referenced a user-defined type
13825 (a typedef, class-name, etc.), then we can't allow any
13826 more such type-specifiers henceforth.
13828 [dcl.spec]
13830 The longest sequence of decl-specifiers that could
13831 possibly be a type name is taken as the
13832 decl-specifier-seq of a declaration. The sequence shall
13833 be self-consistent as described below.
13835 [dcl.type]
13837 As a general rule, at most one type-specifier is allowed
13838 in the complete decl-specifier-seq of a declaration. The
13839 only exceptions are the following:
13841 -- const or volatile can be combined with any other
13842 type-specifier.
13844 -- signed or unsigned can be combined with char, long,
13845 short, or int.
13847 -- ..
13849 Example:
13851 typedef char* Pc;
13852 void g (const int Pc);
13854 Here, Pc is *not* part of the decl-specifier seq; it's
13855 the declarator. Therefore, once we see a type-specifier
13856 (other than a cv-qualifier), we forbid any additional
13857 user-defined types. We *do* still allow things like `int
13858 int' to be considered a decl-specifier-seq, and issue the
13859 error message later. */
13860 if (type_spec && !is_cv_qualifier)
13861 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13862 /* A constructor declarator cannot follow a type-specifier. */
13863 if (type_spec)
13865 constructor_possible_p = false;
13866 found_decl_spec = true;
13867 if (!is_cv_qualifier)
13868 decl_specs->any_type_specifiers_p = true;
13870 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
13871 error_at (token->location, "type-specifier invalid in lambda");
13875 /* If we still do not have a DECL_SPEC, then there are no more
13876 decl-specifiers. */
13877 if (!found_decl_spec)
13878 break;
13880 decl_specs->any_specifiers_p = true;
13881 /* After we see one decl-specifier, further decl-specifiers are
13882 always optional. */
13883 flags |= CP_PARSER_FLAGS_OPTIONAL;
13886 /* Don't allow a friend specifier with a class definition. */
13887 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13888 && (*declares_class_or_enum & 2))
13889 error_at (decl_specs->locations[ds_friend],
13890 "class definition may not be declared a friend");
13893 /* Parse an (optional) storage-class-specifier.
13895 storage-class-specifier:
13896 auto
13897 register
13898 static
13899 extern
13900 mutable
13902 GNU Extension:
13904 storage-class-specifier:
13905 thread
13907 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13909 static tree
13910 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13912 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13914 case RID_AUTO:
13915 if (cxx_dialect != cxx98)
13916 return NULL_TREE;
13917 /* Fall through for C++98. */
13918 gcc_fallthrough ();
13920 case RID_REGISTER:
13921 case RID_STATIC:
13922 case RID_EXTERN:
13923 case RID_MUTABLE:
13924 case RID_THREAD:
13925 /* Consume the token. */
13926 return cp_lexer_consume_token (parser->lexer)->u.value;
13928 default:
13929 return NULL_TREE;
13933 /* Parse an (optional) function-specifier.
13935 function-specifier:
13936 inline
13937 virtual
13938 explicit
13940 C++2A Extension:
13941 explicit(constant-expression)
13943 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13944 Updates DECL_SPECS, if it is non-NULL. */
13946 static tree
13947 cp_parser_function_specifier_opt (cp_parser* parser,
13948 cp_decl_specifier_seq *decl_specs)
13950 cp_token *token = cp_lexer_peek_token (parser->lexer);
13951 switch (token->keyword)
13953 case RID_INLINE:
13954 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13955 break;
13957 case RID_VIRTUAL:
13958 /* 14.5.2.3 [temp.mem]
13960 A member function template shall not be virtual. */
13961 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13962 && current_class_type)
13963 error_at (token->location, "templates may not be %<virtual%>");
13964 else
13965 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13966 break;
13968 case RID_EXPLICIT:
13970 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
13971 /* If we see '(', it's C++20 explicit(bool). */
13972 tree expr;
13973 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13975 matching_parens parens;
13976 parens.consume_open (parser);
13978 /* New types are not allowed in an explicit-specifier. */
13979 const char *saved_message
13980 = parser->type_definition_forbidden_message;
13981 parser->type_definition_forbidden_message
13982 = G_("types may not be defined in explicit-specifier");
13984 if (cxx_dialect < cxx2a)
13985 pedwarn (token->location, 0,
13986 "%<explicit(bool)%> only available with -std=c++2a "
13987 "or -std=gnu++2a");
13989 /* Parse the constant-expression. */
13990 expr = cp_parser_constant_expression (parser);
13992 /* Restore the saved message. */
13993 parser->type_definition_forbidden_message = saved_message;
13994 parens.require_close (parser);
13996 else
13997 /* The explicit-specifier explicit without a constant-expression is
13998 equivalent to the explicit-specifier explicit(true). */
13999 expr = boolean_true_node;
14001 /* [dcl.fct.spec]
14002 "the constant-expression, if supplied, shall be a contextually
14003 converted constant expression of type bool." */
14004 expr = build_explicit_specifier (expr, tf_warning_or_error);
14005 /* We could evaluate it -- mark the decl as appropriate. */
14006 if (expr == boolean_true_node)
14007 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14008 else if (expr == boolean_false_node)
14009 /* Don't mark the decl as explicit. */;
14010 else if (decl_specs)
14011 /* The expression was value-dependent. Remember it so that we can
14012 substitute it later. */
14013 decl_specs->explicit_specifier = expr;
14014 return id;
14017 default:
14018 return NULL_TREE;
14021 /* Consume the token. */
14022 return cp_lexer_consume_token (parser->lexer)->u.value;
14025 /* Parse a linkage-specification.
14027 linkage-specification:
14028 extern string-literal { declaration-seq [opt] }
14029 extern string-literal declaration */
14031 static void
14032 cp_parser_linkage_specification (cp_parser* parser)
14034 tree linkage;
14036 /* Look for the `extern' keyword. */
14037 cp_token *extern_token
14038 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14040 /* Look for the string-literal. */
14041 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14042 linkage = cp_parser_string_literal (parser, false, false);
14044 /* Transform the literal into an identifier. If the literal is a
14045 wide-character string, or contains embedded NULs, then we can't
14046 handle it as the user wants. */
14047 if (strlen (TREE_STRING_POINTER (linkage))
14048 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14050 cp_parser_error (parser, "invalid linkage-specification");
14051 /* Assume C++ linkage. */
14052 linkage = lang_name_cplusplus;
14054 else
14055 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14057 /* We're now using the new linkage. */
14058 push_lang_context (linkage);
14060 /* Preserve the location of the the innermost linkage specification,
14061 tracking the locations of nested specifications via a local. */
14062 location_t saved_location
14063 = parser->innermost_linkage_specification_location;
14064 /* Construct a location ranging from the start of the "extern" to
14065 the end of the string-literal, with the caret at the start, e.g.:
14066 extern "C" {
14067 ^~~~~~~~~~
14069 parser->innermost_linkage_specification_location
14070 = make_location (extern_token->location,
14071 extern_token->location,
14072 get_finish (string_token->location));
14074 /* If the next token is a `{', then we're using the first
14075 production. */
14076 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14078 cp_ensure_no_omp_declare_simd (parser);
14079 cp_ensure_no_oacc_routine (parser);
14081 /* Consume the `{' token. */
14082 matching_braces braces;
14083 braces.consume_open (parser)->location;
14084 /* Parse the declarations. */
14085 cp_parser_declaration_seq_opt (parser);
14086 /* Look for the closing `}'. */
14087 braces.require_close (parser);
14089 /* Otherwise, there's just one declaration. */
14090 else
14092 bool saved_in_unbraced_linkage_specification_p;
14094 saved_in_unbraced_linkage_specification_p
14095 = parser->in_unbraced_linkage_specification_p;
14096 parser->in_unbraced_linkage_specification_p = true;
14097 cp_parser_declaration (parser);
14098 parser->in_unbraced_linkage_specification_p
14099 = saved_in_unbraced_linkage_specification_p;
14102 /* We're done with the linkage-specification. */
14103 pop_lang_context ();
14105 /* Restore location of parent linkage specification, if any. */
14106 parser->innermost_linkage_specification_location = saved_location;
14109 /* Parse a static_assert-declaration.
14111 static_assert-declaration:
14112 static_assert ( constant-expression , string-literal ) ;
14113 static_assert ( constant-expression ) ; (C++17)
14115 If MEMBER_P, this static_assert is a class member. */
14117 static void
14118 cp_parser_static_assert(cp_parser *parser, bool member_p)
14120 cp_expr condition;
14121 location_t token_loc;
14122 tree message;
14123 bool dummy;
14125 /* Peek at the `static_assert' token so we can keep track of exactly
14126 where the static assertion started. */
14127 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14129 /* Look for the `static_assert' keyword. */
14130 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14131 RT_STATIC_ASSERT))
14132 return;
14134 /* We know we are in a static assertion; commit to any tentative
14135 parse. */
14136 if (cp_parser_parsing_tentatively (parser))
14137 cp_parser_commit_to_tentative_parse (parser);
14139 /* Parse the `(' starting the static assertion condition. */
14140 matching_parens parens;
14141 parens.require_open (parser);
14143 /* Parse the constant-expression. Allow a non-constant expression
14144 here in order to give better diagnostics in finish_static_assert. */
14145 condition =
14146 cp_parser_constant_expression (parser,
14147 /*allow_non_constant_p=*/true,
14148 /*non_constant_p=*/&dummy);
14150 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14152 if (cxx_dialect < cxx17)
14153 pedwarn (input_location, OPT_Wpedantic,
14154 "static_assert without a message "
14155 "only available with -std=c++17 or -std=gnu++17");
14156 /* Eat the ')' */
14157 cp_lexer_consume_token (parser->lexer);
14158 message = build_string (1, "");
14159 TREE_TYPE (message) = char_array_type_node;
14160 fix_string_type (message);
14162 else
14164 /* Parse the separating `,'. */
14165 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14167 /* Parse the string-literal message. */
14168 message = cp_parser_string_literal (parser,
14169 /*translate=*/false,
14170 /*wide_ok=*/true);
14172 /* A `)' completes the static assertion. */
14173 if (!parens.require_close (parser))
14174 cp_parser_skip_to_closing_parenthesis (parser,
14175 /*recovering=*/true,
14176 /*or_comma=*/false,
14177 /*consume_paren=*/true);
14180 /* A semicolon terminates the declaration. */
14181 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14183 /* Get the location for the static assertion. Use that of the
14184 condition if available, otherwise, use that of the "static_assert"
14185 token. */
14186 location_t assert_loc = condition.get_location ();
14187 if (assert_loc == UNKNOWN_LOCATION)
14188 assert_loc = token_loc;
14190 /* Complete the static assertion, which may mean either processing
14191 the static assert now or saving it for template instantiation. */
14192 finish_static_assert (condition, message, assert_loc, member_p);
14195 /* Parse the expression in decltype ( expression ). */
14197 static tree
14198 cp_parser_decltype_expr (cp_parser *parser,
14199 bool &id_expression_or_member_access_p)
14201 cp_token *id_expr_start_token;
14202 tree expr;
14204 /* Since we're going to preserve any side-effects from this parse, set up a
14205 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14206 in the expression. */
14207 tentative_firewall firewall (parser);
14209 /* First, try parsing an id-expression. */
14210 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14211 cp_parser_parse_tentatively (parser);
14212 expr = cp_parser_id_expression (parser,
14213 /*template_keyword_p=*/false,
14214 /*check_dependency_p=*/true,
14215 /*template_p=*/NULL,
14216 /*declarator_p=*/false,
14217 /*optional_p=*/false);
14219 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14221 bool non_integral_constant_expression_p = false;
14222 tree id_expression = expr;
14223 cp_id_kind idk;
14224 const char *error_msg;
14226 if (identifier_p (expr))
14227 /* Lookup the name we got back from the id-expression. */
14228 expr = cp_parser_lookup_name_simple (parser, expr,
14229 id_expr_start_token->location);
14231 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14232 /* A template without args is not a complete id-expression. */
14233 expr = error_mark_node;
14235 if (expr
14236 && expr != error_mark_node
14237 && TREE_CODE (expr) != TYPE_DECL
14238 && (TREE_CODE (expr) != BIT_NOT_EXPR
14239 || !TYPE_P (TREE_OPERAND (expr, 0)))
14240 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14242 /* Complete lookup of the id-expression. */
14243 expr = (finish_id_expression
14244 (id_expression, expr, parser->scope, &idk,
14245 /*integral_constant_expression_p=*/false,
14246 /*allow_non_integral_constant_expression_p=*/true,
14247 &non_integral_constant_expression_p,
14248 /*template_p=*/false,
14249 /*done=*/true,
14250 /*address_p=*/false,
14251 /*template_arg_p=*/false,
14252 &error_msg,
14253 id_expr_start_token->location));
14255 if (expr == error_mark_node)
14256 /* We found an id-expression, but it was something that we
14257 should not have found. This is an error, not something
14258 we can recover from, so note that we found an
14259 id-expression and we'll recover as gracefully as
14260 possible. */
14261 id_expression_or_member_access_p = true;
14264 if (expr
14265 && expr != error_mark_node
14266 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14267 /* We have an id-expression. */
14268 id_expression_or_member_access_p = true;
14271 if (!id_expression_or_member_access_p)
14273 /* Abort the id-expression parse. */
14274 cp_parser_abort_tentative_parse (parser);
14276 /* Parsing tentatively, again. */
14277 cp_parser_parse_tentatively (parser);
14279 /* Parse a class member access. */
14280 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14281 /*cast_p=*/false, /*decltype*/true,
14282 /*member_access_only_p=*/true, NULL);
14284 if (expr
14285 && expr != error_mark_node
14286 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14287 /* We have an id-expression. */
14288 id_expression_or_member_access_p = true;
14291 if (id_expression_or_member_access_p)
14292 /* We have parsed the complete id-expression or member access. */
14293 cp_parser_parse_definitely (parser);
14294 else
14296 /* Abort our attempt to parse an id-expression or member access
14297 expression. */
14298 cp_parser_abort_tentative_parse (parser);
14300 /* Commit to the tentative_firewall so we get syntax errors. */
14301 cp_parser_commit_to_tentative_parse (parser);
14303 /* Parse a full expression. */
14304 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14305 /*decltype_p=*/true);
14308 return expr;
14311 /* Parse a `decltype' type. Returns the type.
14313 simple-type-specifier:
14314 decltype ( expression )
14315 C++14 proposal:
14316 decltype ( auto ) */
14318 static tree
14319 cp_parser_decltype (cp_parser *parser)
14321 bool id_expression_or_member_access_p = false;
14322 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14324 if (start_token->type == CPP_DECLTYPE)
14326 /* Already parsed. */
14327 cp_lexer_consume_token (parser->lexer);
14328 return saved_checks_value (start_token->u.tree_check_value);
14331 /* Look for the `decltype' token. */
14332 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14333 return error_mark_node;
14335 /* Parse the opening `('. */
14336 matching_parens parens;
14337 if (!parens.require_open (parser))
14338 return error_mark_node;
14340 push_deferring_access_checks (dk_deferred);
14342 tree expr = NULL_TREE;
14344 if (cxx_dialect >= cxx14
14345 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14346 /* decltype (auto) */
14347 cp_lexer_consume_token (parser->lexer);
14348 else
14350 /* decltype (expression) */
14352 /* Types cannot be defined in a `decltype' expression. Save away the
14353 old message and set the new one. */
14354 const char *saved_message = parser->type_definition_forbidden_message;
14355 parser->type_definition_forbidden_message
14356 = G_("types may not be defined in %<decltype%> expressions");
14358 /* The restrictions on constant-expressions do not apply inside
14359 decltype expressions. */
14360 bool saved_integral_constant_expression_p
14361 = parser->integral_constant_expression_p;
14362 bool saved_non_integral_constant_expression_p
14363 = parser->non_integral_constant_expression_p;
14364 parser->integral_constant_expression_p = false;
14366 /* Within a parenthesized expression, a `>' token is always
14367 the greater-than operator. */
14368 bool saved_greater_than_is_operator_p
14369 = parser->greater_than_is_operator_p;
14370 parser->greater_than_is_operator_p = true;
14372 /* Do not actually evaluate the expression. */
14373 ++cp_unevaluated_operand;
14375 /* Do not warn about problems with the expression. */
14376 ++c_inhibit_evaluation_warnings;
14378 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14380 /* Go back to evaluating expressions. */
14381 --cp_unevaluated_operand;
14382 --c_inhibit_evaluation_warnings;
14384 /* The `>' token might be the end of a template-id or
14385 template-parameter-list now. */
14386 parser->greater_than_is_operator_p
14387 = saved_greater_than_is_operator_p;
14389 /* Restore the old message and the integral constant expression
14390 flags. */
14391 parser->type_definition_forbidden_message = saved_message;
14392 parser->integral_constant_expression_p
14393 = saved_integral_constant_expression_p;
14394 parser->non_integral_constant_expression_p
14395 = saved_non_integral_constant_expression_p;
14398 /* Parse to the closing `)'. */
14399 if (!parens.require_close (parser))
14401 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14402 /*consume_paren=*/true);
14403 pop_deferring_access_checks ();
14404 return error_mark_node;
14407 if (!expr)
14409 /* Build auto. */
14410 expr = make_decltype_auto ();
14411 AUTO_IS_DECLTYPE (expr) = true;
14413 else
14414 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14415 tf_warning_or_error);
14417 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14418 it again. */
14419 start_token->type = CPP_DECLTYPE;
14420 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14421 start_token->u.tree_check_value->value = expr;
14422 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14423 start_token->keyword = RID_MAX;
14424 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14426 pop_to_parent_deferring_access_checks ();
14428 return expr;
14431 /* Special member functions [gram.special] */
14433 /* Parse a conversion-function-id.
14435 conversion-function-id:
14436 operator conversion-type-id
14438 Returns an IDENTIFIER_NODE representing the operator. */
14440 static tree
14441 cp_parser_conversion_function_id (cp_parser* parser)
14443 tree type;
14444 tree saved_scope;
14445 tree saved_qualifying_scope;
14446 tree saved_object_scope;
14447 tree pushed_scope = NULL_TREE;
14449 /* Look for the `operator' token. */
14450 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14451 return error_mark_node;
14452 /* When we parse the conversion-type-id, the current scope will be
14453 reset. However, we need that information in able to look up the
14454 conversion function later, so we save it here. */
14455 saved_scope = parser->scope;
14456 saved_qualifying_scope = parser->qualifying_scope;
14457 saved_object_scope = parser->object_scope;
14458 /* We must enter the scope of the class so that the names of
14459 entities declared within the class are available in the
14460 conversion-type-id. For example, consider:
14462 struct S {
14463 typedef int I;
14464 operator I();
14467 S::operator I() { ... }
14469 In order to see that `I' is a type-name in the definition, we
14470 must be in the scope of `S'. */
14471 if (saved_scope)
14472 pushed_scope = push_scope (saved_scope);
14473 /* Parse the conversion-type-id. */
14474 type = cp_parser_conversion_type_id (parser);
14475 /* Leave the scope of the class, if any. */
14476 if (pushed_scope)
14477 pop_scope (pushed_scope);
14478 /* Restore the saved scope. */
14479 parser->scope = saved_scope;
14480 parser->qualifying_scope = saved_qualifying_scope;
14481 parser->object_scope = saved_object_scope;
14482 /* If the TYPE is invalid, indicate failure. */
14483 if (type == error_mark_node)
14484 return error_mark_node;
14485 return make_conv_op_name (type);
14488 /* Parse a conversion-type-id:
14490 conversion-type-id:
14491 type-specifier-seq conversion-declarator [opt]
14493 Returns the TYPE specified. */
14495 static tree
14496 cp_parser_conversion_type_id (cp_parser* parser)
14498 tree attributes;
14499 cp_decl_specifier_seq type_specifiers;
14500 cp_declarator *declarator;
14501 tree type_specified;
14502 const char *saved_message;
14504 /* Parse the attributes. */
14505 attributes = cp_parser_attributes_opt (parser);
14507 saved_message = parser->type_definition_forbidden_message;
14508 parser->type_definition_forbidden_message
14509 = G_("types may not be defined in a conversion-type-id");
14511 /* Parse the type-specifiers. */
14512 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14513 /*is_trailing_return=*/false,
14514 &type_specifiers);
14516 parser->type_definition_forbidden_message = saved_message;
14518 /* If that didn't work, stop. */
14519 if (type_specifiers.type == error_mark_node)
14520 return error_mark_node;
14521 /* Parse the conversion-declarator. */
14522 declarator = cp_parser_conversion_declarator_opt (parser);
14524 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14525 /*initialized=*/0, &attributes);
14526 if (attributes)
14527 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14529 /* Don't give this error when parsing tentatively. This happens to
14530 work because we always parse this definitively once. */
14531 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14532 && type_uses_auto (type_specified))
14534 if (cxx_dialect < cxx14)
14536 error ("invalid use of %<auto%> in conversion operator");
14537 return error_mark_node;
14539 else if (template_parm_scope_p ())
14540 warning (0, "use of %<auto%> in member template "
14541 "conversion operator can never be deduced");
14544 return type_specified;
14547 /* Parse an (optional) conversion-declarator.
14549 conversion-declarator:
14550 ptr-operator conversion-declarator [opt]
14554 static cp_declarator *
14555 cp_parser_conversion_declarator_opt (cp_parser* parser)
14557 enum tree_code code;
14558 tree class_type, std_attributes = NULL_TREE;
14559 cp_cv_quals cv_quals;
14561 /* We don't know if there's a ptr-operator next, or not. */
14562 cp_parser_parse_tentatively (parser);
14563 /* Try the ptr-operator. */
14564 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14565 &std_attributes);
14566 /* If it worked, look for more conversion-declarators. */
14567 if (cp_parser_parse_definitely (parser))
14569 cp_declarator *declarator;
14571 /* Parse another optional declarator. */
14572 declarator = cp_parser_conversion_declarator_opt (parser);
14574 declarator = cp_parser_make_indirect_declarator
14575 (code, class_type, cv_quals, declarator, std_attributes);
14577 return declarator;
14580 return NULL;
14583 /* Parse an (optional) ctor-initializer.
14585 ctor-initializer:
14586 : mem-initializer-list */
14588 static void
14589 cp_parser_ctor_initializer_opt (cp_parser* parser)
14591 /* If the next token is not a `:', then there is no
14592 ctor-initializer. */
14593 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14595 /* Do default initialization of any bases and members. */
14596 if (DECL_CONSTRUCTOR_P (current_function_decl))
14597 finish_mem_initializers (NULL_TREE);
14598 return;
14601 /* Consume the `:' token. */
14602 cp_lexer_consume_token (parser->lexer);
14603 /* And the mem-initializer-list. */
14604 cp_parser_mem_initializer_list (parser);
14607 /* Parse a mem-initializer-list.
14609 mem-initializer-list:
14610 mem-initializer ... [opt]
14611 mem-initializer ... [opt] , mem-initializer-list */
14613 static void
14614 cp_parser_mem_initializer_list (cp_parser* parser)
14616 tree mem_initializer_list = NULL_TREE;
14617 tree target_ctor = error_mark_node;
14618 cp_token *token = cp_lexer_peek_token (parser->lexer);
14620 /* Let the semantic analysis code know that we are starting the
14621 mem-initializer-list. */
14622 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14623 error_at (token->location,
14624 "only constructors take member initializers");
14626 /* Loop through the list. */
14627 while (true)
14629 tree mem_initializer;
14631 token = cp_lexer_peek_token (parser->lexer);
14632 /* Parse the mem-initializer. */
14633 mem_initializer = cp_parser_mem_initializer (parser);
14634 /* If the next token is a `...', we're expanding member initializers. */
14635 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14636 if (ellipsis
14637 || (mem_initializer != error_mark_node
14638 && check_for_bare_parameter_packs (TREE_PURPOSE
14639 (mem_initializer))))
14641 /* Consume the `...'. */
14642 if (ellipsis)
14643 cp_lexer_consume_token (parser->lexer);
14645 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14646 can be expanded but members cannot. */
14647 if (mem_initializer != error_mark_node
14648 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14650 error_at (token->location,
14651 "cannot expand initializer for member %qD",
14652 TREE_PURPOSE (mem_initializer));
14653 mem_initializer = error_mark_node;
14656 /* Construct the pack expansion type. */
14657 if (mem_initializer != error_mark_node)
14658 mem_initializer = make_pack_expansion (mem_initializer);
14660 if (target_ctor != error_mark_node
14661 && mem_initializer != error_mark_node)
14663 error ("mem-initializer for %qD follows constructor delegation",
14664 TREE_PURPOSE (mem_initializer));
14665 mem_initializer = error_mark_node;
14667 /* Look for a target constructor. */
14668 if (mem_initializer != error_mark_node
14669 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14670 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14672 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14673 if (mem_initializer_list)
14675 error ("constructor delegation follows mem-initializer for %qD",
14676 TREE_PURPOSE (mem_initializer_list));
14677 mem_initializer = error_mark_node;
14679 target_ctor = mem_initializer;
14681 /* Add it to the list, unless it was erroneous. */
14682 if (mem_initializer != error_mark_node)
14684 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14685 mem_initializer_list = mem_initializer;
14687 /* If the next token is not a `,', we're done. */
14688 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14689 break;
14690 /* Consume the `,' token. */
14691 cp_lexer_consume_token (parser->lexer);
14694 /* Perform semantic analysis. */
14695 if (DECL_CONSTRUCTOR_P (current_function_decl))
14696 finish_mem_initializers (mem_initializer_list);
14699 /* Parse a mem-initializer.
14701 mem-initializer:
14702 mem-initializer-id ( expression-list [opt] )
14703 mem-initializer-id braced-init-list
14705 GNU extension:
14707 mem-initializer:
14708 ( expression-list [opt] )
14710 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14711 class) or FIELD_DECL (for a non-static data member) to initialize;
14712 the TREE_VALUE is the expression-list. An empty initialization
14713 list is represented by void_list_node. */
14715 static tree
14716 cp_parser_mem_initializer (cp_parser* parser)
14718 tree mem_initializer_id;
14719 tree expression_list;
14720 tree member;
14721 cp_token *token = cp_lexer_peek_token (parser->lexer);
14723 /* Find out what is being initialized. */
14724 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14726 permerror (token->location,
14727 "anachronistic old-style base class initializer");
14728 mem_initializer_id = NULL_TREE;
14730 else
14732 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14733 if (mem_initializer_id == error_mark_node)
14734 return mem_initializer_id;
14736 member = expand_member_init (mem_initializer_id);
14737 if (member && !DECL_P (member))
14738 in_base_initializer = 1;
14740 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14742 bool expr_non_constant_p;
14743 cp_lexer_set_source_position (parser->lexer);
14744 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14745 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14746 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14747 expression_list = build_tree_list (NULL_TREE, expression_list);
14749 else
14751 vec<tree, va_gc> *vec;
14752 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14753 /*cast_p=*/false,
14754 /*allow_expansion_p=*/true,
14755 /*non_constant_p=*/NULL);
14756 if (vec == NULL)
14757 return error_mark_node;
14758 expression_list = build_tree_list_vec (vec);
14759 release_tree_vector (vec);
14762 if (expression_list == error_mark_node)
14763 return error_mark_node;
14764 if (!expression_list)
14765 expression_list = void_type_node;
14767 in_base_initializer = 0;
14769 return member ? build_tree_list (member, expression_list) : error_mark_node;
14772 /* Parse a mem-initializer-id.
14774 mem-initializer-id:
14775 :: [opt] nested-name-specifier [opt] class-name
14776 decltype-specifier (C++11)
14777 identifier
14779 Returns a TYPE indicating the class to be initialized for the first
14780 production (and the second in C++11). Returns an IDENTIFIER_NODE
14781 indicating the data member to be initialized for the last production. */
14783 static tree
14784 cp_parser_mem_initializer_id (cp_parser* parser)
14786 bool global_scope_p;
14787 bool nested_name_specifier_p;
14788 bool template_p = false;
14789 tree id;
14791 cp_token *token = cp_lexer_peek_token (parser->lexer);
14793 /* `typename' is not allowed in this context ([temp.res]). */
14794 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14796 error_at (token->location,
14797 "keyword %<typename%> not allowed in this context (a qualified "
14798 "member initializer is implicitly a type)");
14799 cp_lexer_consume_token (parser->lexer);
14801 /* Look for the optional `::' operator. */
14802 global_scope_p
14803 = (cp_parser_global_scope_opt (parser,
14804 /*current_scope_valid_p=*/false)
14805 != NULL_TREE);
14806 /* Look for the optional nested-name-specifier. The simplest way to
14807 implement:
14809 [temp.res]
14811 The keyword `typename' is not permitted in a base-specifier or
14812 mem-initializer; in these contexts a qualified name that
14813 depends on a template-parameter is implicitly assumed to be a
14814 type name.
14816 is to assume that we have seen the `typename' keyword at this
14817 point. */
14818 nested_name_specifier_p
14819 = (cp_parser_nested_name_specifier_opt (parser,
14820 /*typename_keyword_p=*/true,
14821 /*check_dependency_p=*/true,
14822 /*type_p=*/true,
14823 /*is_declaration=*/true)
14824 != NULL_TREE);
14825 if (nested_name_specifier_p)
14826 template_p = cp_parser_optional_template_keyword (parser);
14827 /* If there is a `::' operator or a nested-name-specifier, then we
14828 are definitely looking for a class-name. */
14829 if (global_scope_p || nested_name_specifier_p)
14830 return cp_parser_class_name (parser,
14831 /*typename_keyword_p=*/true,
14832 /*template_keyword_p=*/template_p,
14833 typename_type,
14834 /*check_dependency_p=*/true,
14835 /*class_head_p=*/false,
14836 /*is_declaration=*/true);
14837 /* Otherwise, we could also be looking for an ordinary identifier. */
14838 cp_parser_parse_tentatively (parser);
14839 if (cp_lexer_next_token_is_decltype (parser->lexer))
14840 /* Try a decltype-specifier. */
14841 id = cp_parser_decltype (parser);
14842 else
14843 /* Otherwise, try a class-name. */
14844 id = cp_parser_class_name (parser,
14845 /*typename_keyword_p=*/true,
14846 /*template_keyword_p=*/false,
14847 none_type,
14848 /*check_dependency_p=*/true,
14849 /*class_head_p=*/false,
14850 /*is_declaration=*/true);
14851 /* If we found one, we're done. */
14852 if (cp_parser_parse_definitely (parser))
14853 return id;
14854 /* Otherwise, look for an ordinary identifier. */
14855 return cp_parser_identifier (parser);
14858 /* Overloading [gram.over] */
14860 /* Parse an operator-function-id.
14862 operator-function-id:
14863 operator operator
14865 Returns an IDENTIFIER_NODE for the operator which is a
14866 human-readable spelling of the identifier, e.g., `operator +'. */
14868 static cp_expr
14869 cp_parser_operator_function_id (cp_parser* parser)
14871 /* Look for the `operator' keyword. */
14872 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14873 return error_mark_node;
14874 /* And then the name of the operator itself. */
14875 return cp_parser_operator (parser);
14878 /* Return an identifier node for a user-defined literal operator.
14879 The suffix identifier is chained to the operator name identifier. */
14881 tree
14882 cp_literal_operator_id (const char* name)
14884 tree identifier;
14885 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14886 + strlen (name) + 10);
14887 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14888 identifier = get_identifier (buffer);
14890 return identifier;
14893 /* Parse an operator.
14895 operator:
14896 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14897 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14898 || ++ -- , ->* -> () []
14900 GNU Extensions:
14902 operator:
14903 <? >? <?= >?=
14905 Returns an IDENTIFIER_NODE for the operator which is a
14906 human-readable spelling of the identifier, e.g., `operator +'. */
14908 static cp_expr
14909 cp_parser_operator (cp_parser* parser)
14911 tree id = NULL_TREE;
14912 cp_token *token;
14913 bool utf8 = false;
14915 /* Peek at the next token. */
14916 token = cp_lexer_peek_token (parser->lexer);
14918 location_t start_loc = token->location;
14920 /* Figure out which operator we have. */
14921 enum tree_code op = ERROR_MARK;
14922 bool assop = false;
14923 bool consumed = false;
14924 switch (token->type)
14926 case CPP_KEYWORD:
14928 /* The keyword should be either `new' or `delete'. */
14929 if (token->keyword == RID_NEW)
14930 op = NEW_EXPR;
14931 else if (token->keyword == RID_DELETE)
14932 op = DELETE_EXPR;
14933 else
14934 break;
14936 /* Consume the `new' or `delete' token. */
14937 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14939 /* Peek at the next token. */
14940 token = cp_lexer_peek_token (parser->lexer);
14941 /* If it's a `[' token then this is the array variant of the
14942 operator. */
14943 if (token->type == CPP_OPEN_SQUARE)
14945 /* Consume the `[' token. */
14946 cp_lexer_consume_token (parser->lexer);
14947 /* Look for the `]' token. */
14948 if (cp_token *close_token
14949 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14950 end_loc = close_token->location;
14951 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14953 start_loc = make_location (start_loc, start_loc, end_loc);
14954 consumed = true;
14955 break;
14958 case CPP_PLUS:
14959 op = PLUS_EXPR;
14960 break;
14962 case CPP_MINUS:
14963 op = MINUS_EXPR;
14964 break;
14966 case CPP_MULT:
14967 op = MULT_EXPR;
14968 break;
14970 case CPP_DIV:
14971 op = TRUNC_DIV_EXPR;
14972 break;
14974 case CPP_MOD:
14975 op = TRUNC_MOD_EXPR;
14976 break;
14978 case CPP_XOR:
14979 op = BIT_XOR_EXPR;
14980 break;
14982 case CPP_AND:
14983 op = BIT_AND_EXPR;
14984 break;
14986 case CPP_OR:
14987 op = BIT_IOR_EXPR;
14988 break;
14990 case CPP_COMPL:
14991 op = BIT_NOT_EXPR;
14992 break;
14994 case CPP_NOT:
14995 op = TRUTH_NOT_EXPR;
14996 break;
14998 case CPP_EQ:
14999 assop = true;
15000 op = NOP_EXPR;
15001 break;
15003 case CPP_LESS:
15004 op = LT_EXPR;
15005 break;
15007 case CPP_GREATER:
15008 op = GT_EXPR;
15009 break;
15011 case CPP_PLUS_EQ:
15012 assop = true;
15013 op = PLUS_EXPR;
15014 break;
15016 case CPP_MINUS_EQ:
15017 assop = true;
15018 op = MINUS_EXPR;
15019 break;
15021 case CPP_MULT_EQ:
15022 assop = true;
15023 op = MULT_EXPR;
15024 break;
15026 case CPP_DIV_EQ:
15027 assop = true;
15028 op = TRUNC_DIV_EXPR;
15029 break;
15031 case CPP_MOD_EQ:
15032 assop = true;
15033 op = TRUNC_MOD_EXPR;
15034 break;
15036 case CPP_XOR_EQ:
15037 assop = true;
15038 op = BIT_XOR_EXPR;
15039 break;
15041 case CPP_AND_EQ:
15042 assop = true;
15043 op = BIT_AND_EXPR;
15044 break;
15046 case CPP_OR_EQ:
15047 assop = true;
15048 op = BIT_IOR_EXPR;
15049 break;
15051 case CPP_LSHIFT:
15052 op = LSHIFT_EXPR;
15053 break;
15055 case CPP_RSHIFT:
15056 op = RSHIFT_EXPR;
15057 break;
15059 case CPP_LSHIFT_EQ:
15060 assop = true;
15061 op = LSHIFT_EXPR;
15062 break;
15064 case CPP_RSHIFT_EQ:
15065 assop = true;
15066 op = RSHIFT_EXPR;
15067 break;
15069 case CPP_EQ_EQ:
15070 op = EQ_EXPR;
15071 break;
15073 case CPP_NOT_EQ:
15074 op = NE_EXPR;
15075 break;
15077 case CPP_LESS_EQ:
15078 op = LE_EXPR;
15079 break;
15081 case CPP_GREATER_EQ:
15082 op = GE_EXPR;
15083 break;
15085 case CPP_AND_AND:
15086 op = TRUTH_ANDIF_EXPR;
15087 break;
15089 case CPP_OR_OR:
15090 op = TRUTH_ORIF_EXPR;
15091 break;
15093 case CPP_PLUS_PLUS:
15094 op = POSTINCREMENT_EXPR;
15095 break;
15097 case CPP_MINUS_MINUS:
15098 op = PREDECREMENT_EXPR;
15099 break;
15101 case CPP_COMMA:
15102 op = COMPOUND_EXPR;
15103 break;
15105 case CPP_DEREF_STAR:
15106 op = MEMBER_REF;
15107 break;
15109 case CPP_DEREF:
15110 op = COMPONENT_REF;
15111 break;
15113 case CPP_OPEN_PAREN:
15115 /* Consume the `('. */
15116 matching_parens parens;
15117 parens.consume_open (parser);
15118 /* Look for the matching `)'. */
15119 parens.require_close (parser);
15120 op = CALL_EXPR;
15121 consumed = true;
15122 break;
15125 case CPP_OPEN_SQUARE:
15126 /* Consume the `['. */
15127 cp_lexer_consume_token (parser->lexer);
15128 /* Look for the matching `]'. */
15129 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15130 op = ARRAY_REF;
15131 consumed = true;
15132 break;
15134 case CPP_UTF8STRING:
15135 case CPP_UTF8STRING_USERDEF:
15136 utf8 = true;
15137 /* FALLTHRU */
15138 case CPP_STRING:
15139 case CPP_WSTRING:
15140 case CPP_STRING16:
15141 case CPP_STRING32:
15142 case CPP_STRING_USERDEF:
15143 case CPP_WSTRING_USERDEF:
15144 case CPP_STRING16_USERDEF:
15145 case CPP_STRING32_USERDEF:
15147 tree str, string_tree;
15148 int sz, len;
15150 if (cxx_dialect == cxx98)
15151 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15153 /* Consume the string. */
15154 str = cp_parser_string_literal (parser, /*translate=*/true,
15155 /*wide_ok=*/true, /*lookup_udlit=*/false);
15156 if (str == error_mark_node)
15157 return error_mark_node;
15158 else if (TREE_CODE (str) == USERDEF_LITERAL)
15160 string_tree = USERDEF_LITERAL_VALUE (str);
15161 id = USERDEF_LITERAL_SUFFIX_ID (str);
15163 else
15165 string_tree = str;
15166 /* Look for the suffix identifier. */
15167 token = cp_lexer_peek_token (parser->lexer);
15168 if (token->type == CPP_NAME)
15169 id = cp_parser_identifier (parser);
15170 else if (token->type == CPP_KEYWORD)
15172 error ("unexpected keyword;"
15173 " remove space between quotes and suffix identifier");
15174 return error_mark_node;
15176 else
15178 error ("expected suffix identifier");
15179 return error_mark_node;
15182 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15183 (TREE_TYPE (TREE_TYPE (string_tree))));
15184 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15185 if (len != 0)
15187 error ("expected empty string after %<operator%> keyword");
15188 return error_mark_node;
15190 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15191 != char_type_node)
15193 error ("invalid encoding prefix in literal operator");
15194 return error_mark_node;
15196 if (id != error_mark_node)
15198 const char *name = IDENTIFIER_POINTER (id);
15199 id = cp_literal_operator_id (name);
15201 return id;
15204 default:
15205 /* Anything else is an error. */
15206 break;
15209 /* If we have selected an identifier, we need to consume the
15210 operator token. */
15211 if (op != ERROR_MARK)
15213 id = ovl_op_identifier (assop, op);
15214 if (!consumed)
15215 cp_lexer_consume_token (parser->lexer);
15217 /* Otherwise, no valid operator name was present. */
15218 else
15220 cp_parser_error (parser, "expected operator");
15221 id = error_mark_node;
15224 return cp_expr (id, start_loc);
15227 /* Parse a template-declaration.
15229 template-declaration:
15230 export [opt] template < template-parameter-list > declaration
15232 If MEMBER_P is TRUE, this template-declaration occurs within a
15233 class-specifier.
15235 The grammar rule given by the standard isn't correct. What
15236 is really meant is:
15238 template-declaration:
15239 export [opt] template-parameter-list-seq
15240 decl-specifier-seq [opt] init-declarator [opt] ;
15241 export [opt] template-parameter-list-seq
15242 function-definition
15244 template-parameter-list-seq:
15245 template-parameter-list-seq [opt]
15246 template < template-parameter-list >
15248 Concept Extensions:
15250 template-parameter-list-seq:
15251 template < template-parameter-list > requires-clause [opt]
15253 requires-clause:
15254 requires logical-or-expression */
15256 static void
15257 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15259 /* Check for `export'. */
15260 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15262 /* Consume the `export' token. */
15263 cp_lexer_consume_token (parser->lexer);
15264 /* Warn that we do not support `export'. */
15265 warning (0, "keyword %<export%> not implemented, and will be ignored");
15268 cp_parser_template_declaration_after_export (parser, member_p);
15271 /* Parse a template-parameter-list.
15273 template-parameter-list:
15274 template-parameter
15275 template-parameter-list , template-parameter
15277 Returns a TREE_LIST. Each node represents a template parameter.
15278 The nodes are connected via their TREE_CHAINs. */
15280 static tree
15281 cp_parser_template_parameter_list (cp_parser* parser)
15283 tree parameter_list = NULL_TREE;
15285 begin_template_parm_list ();
15287 /* The loop below parses the template parms. We first need to know
15288 the total number of template parms to be able to compute proper
15289 canonical types of each dependent type. So after the loop, when
15290 we know the total number of template parms,
15291 end_template_parm_list computes the proper canonical types and
15292 fixes up the dependent types accordingly. */
15293 while (true)
15295 tree parameter;
15296 bool is_non_type;
15297 bool is_parameter_pack;
15298 location_t parm_loc;
15300 /* Parse the template-parameter. */
15301 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15302 parameter = cp_parser_template_parameter (parser,
15303 &is_non_type,
15304 &is_parameter_pack);
15305 /* Add it to the list. */
15306 if (parameter != error_mark_node)
15307 parameter_list = process_template_parm (parameter_list,
15308 parm_loc,
15309 parameter,
15310 is_non_type,
15311 is_parameter_pack);
15312 else
15314 tree err_parm = build_tree_list (parameter, parameter);
15315 parameter_list = chainon (parameter_list, err_parm);
15318 /* If the next token is not a `,', we're done. */
15319 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15320 break;
15321 /* Otherwise, consume the `,' token. */
15322 cp_lexer_consume_token (parser->lexer);
15325 return end_template_parm_list (parameter_list);
15328 /* Parse a introduction-list.
15330 introduction-list:
15331 introduced-parameter
15332 introduction-list , introduced-parameter
15334 introduced-parameter:
15335 ...[opt] identifier
15337 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15338 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15339 WILDCARD_DECL will also have DECL_NAME set and token location in
15340 DECL_SOURCE_LOCATION. */
15342 static tree
15343 cp_parser_introduction_list (cp_parser *parser)
15345 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15347 while (true)
15349 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15350 if (is_pack)
15351 cp_lexer_consume_token (parser->lexer);
15353 tree identifier = cp_parser_identifier (parser);
15354 if (identifier == error_mark_node)
15355 break;
15357 /* Build placeholder. */
15358 tree parm = build_nt (WILDCARD_DECL);
15359 DECL_SOURCE_LOCATION (parm)
15360 = cp_lexer_peek_token (parser->lexer)->location;
15361 DECL_NAME (parm) = identifier;
15362 WILDCARD_PACK_P (parm) = is_pack;
15363 vec_safe_push (introduction_vec, parm);
15365 /* If the next token is not a `,', we're done. */
15366 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15367 break;
15368 /* Otherwise, consume the `,' token. */
15369 cp_lexer_consume_token (parser->lexer);
15372 /* Convert the vec into a TREE_VEC. */
15373 tree introduction_list = make_tree_vec (introduction_vec->length ());
15374 unsigned int n;
15375 tree parm;
15376 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15377 TREE_VEC_ELT (introduction_list, n) = parm;
15379 release_tree_vector (introduction_vec);
15380 return introduction_list;
15383 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15384 is an abstract declarator. */
15386 static inline cp_declarator*
15387 get_id_declarator (cp_declarator *declarator)
15389 cp_declarator *d = declarator;
15390 while (d && d->kind != cdk_id)
15391 d = d->declarator;
15392 return d;
15395 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15396 is an abstract declarator. */
15398 static inline tree
15399 get_unqualified_id (cp_declarator *declarator)
15401 declarator = get_id_declarator (declarator);
15402 if (declarator)
15403 return declarator->u.id.unqualified_name;
15404 else
15405 return NULL_TREE;
15408 /* Returns true if DECL represents a constrained-parameter. */
15410 static inline bool
15411 is_constrained_parameter (tree decl)
15413 return (decl
15414 && TREE_CODE (decl) == TYPE_DECL
15415 && CONSTRAINED_PARM_CONCEPT (decl)
15416 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15419 /* Returns true if PARM declares a constrained-parameter. */
15421 static inline bool
15422 is_constrained_parameter (cp_parameter_declarator *parm)
15424 return is_constrained_parameter (parm->decl_specifiers.type);
15427 /* Check that the type parameter is only a declarator-id, and that its
15428 type is not cv-qualified. */
15430 bool
15431 cp_parser_check_constrained_type_parm (cp_parser *parser,
15432 cp_parameter_declarator *parm)
15434 if (!parm->declarator)
15435 return true;
15437 if (parm->declarator->kind != cdk_id)
15439 cp_parser_error (parser, "invalid constrained type parameter");
15440 return false;
15443 /* Don't allow cv-qualified type parameters. */
15444 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15445 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15447 cp_parser_error (parser, "cv-qualified type parameter");
15448 return false;
15451 return true;
15454 /* Finish parsing/processing a template type parameter and checking
15455 various restrictions. */
15457 static inline tree
15458 cp_parser_constrained_type_template_parm (cp_parser *parser,
15459 tree id,
15460 cp_parameter_declarator* parmdecl)
15462 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15463 return finish_template_type_parm (class_type_node, id);
15464 else
15465 return error_mark_node;
15468 static tree
15469 finish_constrained_template_template_parm (tree proto, tree id)
15471 /* FIXME: This should probably be copied, and we may need to adjust
15472 the template parameter depths. */
15473 tree saved_parms = current_template_parms;
15474 begin_template_parm_list ();
15475 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15476 end_template_parm_list ();
15478 tree parm = finish_template_template_parm (class_type_node, id);
15479 current_template_parms = saved_parms;
15481 return parm;
15484 /* Finish parsing/processing a template template parameter by borrowing
15485 the template parameter list from the prototype parameter. */
15487 static tree
15488 cp_parser_constrained_template_template_parm (cp_parser *parser,
15489 tree proto,
15490 tree id,
15491 cp_parameter_declarator *parmdecl)
15493 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15494 return error_mark_node;
15495 return finish_constrained_template_template_parm (proto, id);
15498 /* Create a new non-type template parameter from the given PARM
15499 declarator. */
15501 static tree
15502 constrained_non_type_template_parm (bool *is_non_type,
15503 cp_parameter_declarator *parm)
15505 *is_non_type = true;
15506 cp_declarator *decl = parm->declarator;
15507 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15508 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15509 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15512 /* Build a constrained template parameter based on the PARMDECL
15513 declarator. The type of PARMDECL is the constrained type, which
15514 refers to the prototype template parameter that ultimately
15515 specifies the type of the declared parameter. */
15517 static tree
15518 finish_constrained_parameter (cp_parser *parser,
15519 cp_parameter_declarator *parmdecl,
15520 bool *is_non_type,
15521 bool *is_parameter_pack)
15523 tree decl = parmdecl->decl_specifiers.type;
15524 tree id = get_unqualified_id (parmdecl->declarator);
15525 tree def = parmdecl->default_argument;
15526 tree proto = DECL_INITIAL (decl);
15528 /* A template parameter constrained by a variadic concept shall also
15529 be declared as a template parameter pack. */
15530 bool is_variadic = template_parameter_pack_p (proto);
15531 if (is_variadic && !*is_parameter_pack)
15532 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15534 /* Build the parameter. Return an error if the declarator was invalid. */
15535 tree parm;
15536 if (TREE_CODE (proto) == TYPE_DECL)
15537 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15538 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15539 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15540 parmdecl);
15541 else
15542 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15543 if (parm == error_mark_node)
15544 return error_mark_node;
15546 /* Finish the parameter decl and create a node attaching the
15547 default argument and constraint. */
15548 parm = build_tree_list (def, parm);
15549 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15551 return parm;
15554 /* Returns true if the parsed type actually represents the declaration
15555 of a type template-parameter. */
15557 static inline bool
15558 declares_constrained_type_template_parameter (tree type)
15560 return (is_constrained_parameter (type)
15561 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15565 /* Returns true if the parsed type actually represents the declaration of
15566 a template template-parameter. */
15568 static bool
15569 declares_constrained_template_template_parameter (tree type)
15571 return (is_constrained_parameter (type)
15572 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15575 /* Parse a default argument for a type template-parameter.
15576 Note that diagnostics are handled in cp_parser_template_parameter. */
15578 static tree
15579 cp_parser_default_type_template_argument (cp_parser *parser)
15581 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15583 /* Consume the `=' token. */
15584 cp_lexer_consume_token (parser->lexer);
15586 cp_token *token = cp_lexer_peek_token (parser->lexer);
15588 /* Parse the default-argument. */
15589 push_deferring_access_checks (dk_no_deferred);
15590 tree default_argument = cp_parser_type_id (parser);
15591 pop_deferring_access_checks ();
15593 if (flag_concepts && type_uses_auto (default_argument))
15595 error_at (token->location,
15596 "invalid use of %<auto%> in default template argument");
15597 return error_mark_node;
15600 return default_argument;
15603 /* Parse a default argument for a template template-parameter. */
15605 static tree
15606 cp_parser_default_template_template_argument (cp_parser *parser)
15608 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15610 bool is_template;
15612 /* Consume the `='. */
15613 cp_lexer_consume_token (parser->lexer);
15614 /* Parse the id-expression. */
15615 push_deferring_access_checks (dk_no_deferred);
15616 /* save token before parsing the id-expression, for error
15617 reporting */
15618 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15619 tree default_argument
15620 = cp_parser_id_expression (parser,
15621 /*template_keyword_p=*/false,
15622 /*check_dependency_p=*/true,
15623 /*template_p=*/&is_template,
15624 /*declarator_p=*/false,
15625 /*optional_p=*/false);
15626 if (TREE_CODE (default_argument) == TYPE_DECL)
15627 /* If the id-expression was a template-id that refers to
15628 a template-class, we already have the declaration here,
15629 so no further lookup is needed. */
15631 else
15632 /* Look up the name. */
15633 default_argument
15634 = cp_parser_lookup_name (parser, default_argument,
15635 none_type,
15636 /*is_template=*/is_template,
15637 /*is_namespace=*/false,
15638 /*check_dependency=*/true,
15639 /*ambiguous_decls=*/NULL,
15640 token->location);
15641 /* See if the default argument is valid. */
15642 default_argument = check_template_template_default_arg (default_argument);
15643 pop_deferring_access_checks ();
15644 return default_argument;
15647 /* Parse a template-parameter.
15649 template-parameter:
15650 type-parameter
15651 parameter-declaration
15653 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15654 the parameter. The TREE_PURPOSE is the default value, if any.
15655 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15656 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15657 set to true iff this parameter is a parameter pack. */
15659 static tree
15660 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15661 bool *is_parameter_pack)
15663 cp_token *token;
15664 cp_parameter_declarator *parameter_declarator;
15665 tree parm;
15667 /* Assume it is a type parameter or a template parameter. */
15668 *is_non_type = false;
15669 /* Assume it not a parameter pack. */
15670 *is_parameter_pack = false;
15671 /* Peek at the next token. */
15672 token = cp_lexer_peek_token (parser->lexer);
15673 /* If it is `template', we have a type-parameter. */
15674 if (token->keyword == RID_TEMPLATE)
15675 return cp_parser_type_parameter (parser, is_parameter_pack);
15676 /* If it is `class' or `typename' we do not know yet whether it is a
15677 type parameter or a non-type parameter. Consider:
15679 template <typename T, typename T::X X> ...
15683 template <class C, class D*> ...
15685 Here, the first parameter is a type parameter, and the second is
15686 a non-type parameter. We can tell by looking at the token after
15687 the identifier -- if it is a `,', `=', or `>' then we have a type
15688 parameter. */
15689 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15691 /* Peek at the token after `class' or `typename'. */
15692 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15693 /* If it's an ellipsis, we have a template type parameter
15694 pack. */
15695 if (token->type == CPP_ELLIPSIS)
15696 return cp_parser_type_parameter (parser, is_parameter_pack);
15697 /* If it's an identifier, skip it. */
15698 if (token->type == CPP_NAME)
15699 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15700 /* Now, see if the token looks like the end of a template
15701 parameter. */
15702 if (token->type == CPP_COMMA
15703 || token->type == CPP_EQ
15704 || token->type == CPP_GREATER)
15705 return cp_parser_type_parameter (parser, is_parameter_pack);
15708 /* Otherwise, it is a non-type parameter or a constrained parameter.
15710 [temp.param]
15712 When parsing a default template-argument for a non-type
15713 template-parameter, the first non-nested `>' is taken as the end
15714 of the template parameter-list rather than a greater-than
15715 operator. */
15716 parameter_declarator
15717 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15718 /*parenthesized_p=*/NULL);
15720 if (!parameter_declarator)
15721 return error_mark_node;
15723 /* If the parameter declaration is marked as a parameter pack, set
15724 *IS_PARAMETER_PACK to notify the caller. */
15725 if (parameter_declarator->template_parameter_pack_p)
15726 *is_parameter_pack = true;
15728 if (parameter_declarator->default_argument)
15730 /* Can happen in some cases of erroneous input (c++/34892). */
15731 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15732 /* Consume the `...' for better error recovery. */
15733 cp_lexer_consume_token (parser->lexer);
15736 // The parameter may have been constrained.
15737 if (is_constrained_parameter (parameter_declarator))
15738 return finish_constrained_parameter (parser,
15739 parameter_declarator,
15740 is_non_type,
15741 is_parameter_pack);
15743 // Now we're sure that the parameter is a non-type parameter.
15744 *is_non_type = true;
15746 parm = grokdeclarator (parameter_declarator->declarator,
15747 &parameter_declarator->decl_specifiers,
15748 TPARM, /*initialized=*/0,
15749 /*attrlist=*/NULL);
15750 if (parm == error_mark_node)
15751 return error_mark_node;
15753 return build_tree_list (parameter_declarator->default_argument, parm);
15756 /* Parse a type-parameter.
15758 type-parameter:
15759 class identifier [opt]
15760 class identifier [opt] = type-id
15761 typename identifier [opt]
15762 typename identifier [opt] = type-id
15763 template < template-parameter-list > class identifier [opt]
15764 template < template-parameter-list > class identifier [opt]
15765 = id-expression
15767 GNU Extension (variadic templates):
15769 type-parameter:
15770 class ... identifier [opt]
15771 typename ... identifier [opt]
15773 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15774 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15775 the declaration of the parameter.
15777 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15779 static tree
15780 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15782 cp_token *token;
15783 tree parameter;
15785 /* Look for a keyword to tell us what kind of parameter this is. */
15786 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15787 if (!token)
15788 return error_mark_node;
15790 switch (token->keyword)
15792 case RID_CLASS:
15793 case RID_TYPENAME:
15795 tree identifier;
15796 tree default_argument;
15798 /* If the next token is an ellipsis, we have a template
15799 argument pack. */
15800 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15802 /* Consume the `...' token. */
15803 cp_lexer_consume_token (parser->lexer);
15804 maybe_warn_variadic_templates ();
15806 *is_parameter_pack = true;
15809 /* If the next token is an identifier, then it names the
15810 parameter. */
15811 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15812 identifier = cp_parser_identifier (parser);
15813 else
15814 identifier = NULL_TREE;
15816 /* Create the parameter. */
15817 parameter = finish_template_type_parm (class_type_node, identifier);
15819 /* If the next token is an `=', we have a default argument. */
15820 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15822 default_argument
15823 = cp_parser_default_type_template_argument (parser);
15825 /* Template parameter packs cannot have default
15826 arguments. */
15827 if (*is_parameter_pack)
15829 if (identifier)
15830 error_at (token->location,
15831 "template parameter pack %qD cannot have a "
15832 "default argument", identifier);
15833 else
15834 error_at (token->location,
15835 "template parameter packs cannot have "
15836 "default arguments");
15837 default_argument = NULL_TREE;
15839 else if (check_for_bare_parameter_packs (default_argument))
15840 default_argument = error_mark_node;
15842 else
15843 default_argument = NULL_TREE;
15845 /* Create the combined representation of the parameter and the
15846 default argument. */
15847 parameter = build_tree_list (default_argument, parameter);
15849 break;
15851 case RID_TEMPLATE:
15853 tree identifier;
15854 tree default_argument;
15856 /* Look for the `<'. */
15857 cp_parser_require (parser, CPP_LESS, RT_LESS);
15858 /* Parse the template-parameter-list. */
15859 cp_parser_template_parameter_list (parser);
15860 /* Look for the `>'. */
15861 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15863 // If template requirements are present, parse them.
15864 if (flag_concepts)
15866 tree reqs = get_shorthand_constraints (current_template_parms);
15867 if (tree r = cp_parser_requires_clause_opt (parser))
15868 reqs = conjoin_constraints (reqs, normalize_expression (r));
15869 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15872 /* Look for the `class' or 'typename' keywords. */
15873 cp_parser_type_parameter_key (parser);
15874 /* If the next token is an ellipsis, we have a template
15875 argument pack. */
15876 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15878 /* Consume the `...' token. */
15879 cp_lexer_consume_token (parser->lexer);
15880 maybe_warn_variadic_templates ();
15882 *is_parameter_pack = true;
15884 /* If the next token is an `=', then there is a
15885 default-argument. If the next token is a `>', we are at
15886 the end of the parameter-list. If the next token is a `,',
15887 then we are at the end of this parameter. */
15888 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15889 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15890 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15892 identifier = cp_parser_identifier (parser);
15893 /* Treat invalid names as if the parameter were nameless. */
15894 if (identifier == error_mark_node)
15895 identifier = NULL_TREE;
15897 else
15898 identifier = NULL_TREE;
15900 /* Create the template parameter. */
15901 parameter = finish_template_template_parm (class_type_node,
15902 identifier);
15904 /* If the next token is an `=', then there is a
15905 default-argument. */
15906 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15908 default_argument
15909 = cp_parser_default_template_template_argument (parser);
15911 /* Template parameter packs cannot have default
15912 arguments. */
15913 if (*is_parameter_pack)
15915 if (identifier)
15916 error_at (token->location,
15917 "template parameter pack %qD cannot "
15918 "have a default argument",
15919 identifier);
15920 else
15921 error_at (token->location, "template parameter packs cannot "
15922 "have default arguments");
15923 default_argument = NULL_TREE;
15926 else
15927 default_argument = NULL_TREE;
15929 /* Create the combined representation of the parameter and the
15930 default argument. */
15931 parameter = build_tree_list (default_argument, parameter);
15933 break;
15935 default:
15936 gcc_unreachable ();
15937 break;
15940 return parameter;
15943 /* Parse a template-id.
15945 template-id:
15946 template-name < template-argument-list [opt] >
15948 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15949 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15950 returned. Otherwise, if the template-name names a function, or set
15951 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15952 names a class, returns a TYPE_DECL for the specialization.
15954 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15955 uninstantiated templates. */
15957 static tree
15958 cp_parser_template_id (cp_parser *parser,
15959 bool template_keyword_p,
15960 bool check_dependency_p,
15961 enum tag_types tag_type,
15962 bool is_declaration)
15964 tree templ;
15965 tree arguments;
15966 tree template_id;
15967 cp_token_position start_of_id = 0;
15968 cp_token *next_token = NULL, *next_token_2 = NULL;
15969 bool is_identifier;
15971 /* If the next token corresponds to a template-id, there is no need
15972 to reparse it. */
15973 cp_token *token = cp_lexer_peek_token (parser->lexer);
15974 if (token->type == CPP_TEMPLATE_ID)
15976 cp_lexer_consume_token (parser->lexer);
15977 return saved_checks_value (token->u.tree_check_value);
15980 /* Avoid performing name lookup if there is no possibility of
15981 finding a template-id. */
15982 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15983 || (token->type == CPP_NAME
15984 && !cp_parser_nth_token_starts_template_argument_list_p
15985 (parser, 2)))
15987 cp_parser_error (parser, "expected template-id");
15988 return error_mark_node;
15991 /* Remember where the template-id starts. */
15992 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15993 start_of_id = cp_lexer_token_position (parser->lexer, false);
15995 push_deferring_access_checks (dk_deferred);
15997 /* Parse the template-name. */
15998 is_identifier = false;
15999 templ = cp_parser_template_name (parser, template_keyword_p,
16000 check_dependency_p,
16001 is_declaration,
16002 tag_type,
16003 &is_identifier);
16004 if (templ == error_mark_node || is_identifier)
16006 pop_deferring_access_checks ();
16007 return templ;
16010 /* Since we're going to preserve any side-effects from this parse, set up a
16011 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16012 in the template arguments. */
16013 tentative_firewall firewall (parser);
16015 /* If we find the sequence `[:' after a template-name, it's probably
16016 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16017 parse correctly the argument list. */
16018 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16019 == CPP_OPEN_SQUARE)
16020 && next_token->flags & DIGRAPH
16021 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16022 == CPP_COLON)
16023 && !(next_token_2->flags & PREV_WHITE))
16025 cp_parser_parse_tentatively (parser);
16026 /* Change `:' into `::'. */
16027 next_token_2->type = CPP_SCOPE;
16028 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16029 CPP_LESS. */
16030 cp_lexer_consume_token (parser->lexer);
16032 /* Parse the arguments. */
16033 arguments = cp_parser_enclosed_template_argument_list (parser);
16034 if (!cp_parser_parse_definitely (parser))
16036 /* If we couldn't parse an argument list, then we revert our changes
16037 and return simply an error. Maybe this is not a template-id
16038 after all. */
16039 next_token_2->type = CPP_COLON;
16040 cp_parser_error (parser, "expected %<<%>");
16041 pop_deferring_access_checks ();
16042 return error_mark_node;
16044 /* Otherwise, emit an error about the invalid digraph, but continue
16045 parsing because we got our argument list. */
16046 if (permerror (next_token->location,
16047 "%<<::%> cannot begin a template-argument list"))
16049 static bool hint = false;
16050 inform (next_token->location,
16051 "%<<:%> is an alternate spelling for %<[%>."
16052 " Insert whitespace between %<<%> and %<::%>");
16053 if (!hint && !flag_permissive)
16055 inform (next_token->location, "(if you use %<-fpermissive%> "
16056 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16057 "accept your code)");
16058 hint = true;
16062 else
16064 /* Look for the `<' that starts the template-argument-list. */
16065 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16067 pop_deferring_access_checks ();
16068 return error_mark_node;
16070 /* Parse the arguments. */
16071 arguments = cp_parser_enclosed_template_argument_list (parser);
16073 if ((cxx_dialect > cxx17)
16074 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16075 && !template_keyword_p
16076 && (cp_parser_error_occurred (parser)
16077 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16079 /* This didn't go well. */
16080 if (TREE_CODE (templ) == FUNCTION_DECL)
16082 /* C++2A says that "function-name < a;" is now ill-formed. */
16083 if (cp_parser_error_occurred (parser))
16085 error_at (token->location, "invalid template-argument-list");
16086 inform (token->location, "function name as the left hand "
16087 "operand of %<<%> is ill-formed in C++2a; wrap the "
16088 "function name in %<()%>");
16090 else
16091 /* We expect "f<targs>" to be followed by "(args)". */
16092 error_at (cp_lexer_peek_token (parser->lexer)->location,
16093 "expected %<(%> after template-argument-list");
16094 if (start_of_id)
16095 /* Purge all subsequent tokens. */
16096 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16098 else
16099 cp_parser_simulate_error (parser);
16100 pop_deferring_access_checks ();
16101 return error_mark_node;
16105 /* Set the location to be of the form:
16106 template-name < template-argument-list [opt] >
16107 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16108 with caret == start at the start of the template-name,
16109 ranging until the closing '>'. */
16110 location_t finish_loc
16111 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16112 location_t combined_loc
16113 = make_location (token->location, token->location, finish_loc);
16115 /* Check for concepts autos where they don't belong. We could
16116 identify types in some cases of idnetifier TEMPL, looking ahead
16117 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16118 types. We reject them in functions, but if what we have is an
16119 identifier, even with none_type we can't conclude it's NOT a
16120 type, we have to wait for template substitution. */
16121 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16122 template_id = error_mark_node;
16123 /* Build a representation of the specialization. */
16124 else if (identifier_p (templ))
16125 template_id = build_min_nt_loc (combined_loc,
16126 TEMPLATE_ID_EXPR,
16127 templ, arguments);
16128 else if (DECL_TYPE_TEMPLATE_P (templ)
16129 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16131 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16132 template (rather than some instantiation thereof) only if
16133 is not nested within some other construct. For example, in
16134 "template <typename T> void f(T) { A<T>::", A<T> is just an
16135 instantiation of A. */
16136 bool entering_scope
16137 = (template_parm_scope_p ()
16138 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16139 template_id
16140 = finish_template_type (templ, arguments, entering_scope);
16142 /* A template-like identifier may be a partial concept id. */
16143 else if (flag_concepts
16144 && (template_id = (cp_parser_maybe_partial_concept_id
16145 (parser, templ, arguments))))
16146 return template_id;
16147 else if (variable_template_p (templ))
16149 template_id = lookup_template_variable (templ, arguments);
16150 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16151 SET_EXPR_LOCATION (template_id, combined_loc);
16153 else
16155 /* If it's not a class-template or a template-template, it should be
16156 a function-template. */
16157 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16158 || TREE_CODE (templ) == OVERLOAD
16159 || TREE_CODE (templ) == FUNCTION_DECL
16160 || BASELINK_P (templ)));
16162 template_id = lookup_template_function (templ, arguments);
16163 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16164 SET_EXPR_LOCATION (template_id, combined_loc);
16167 /* If parsing tentatively, replace the sequence of tokens that makes
16168 up the template-id with a CPP_TEMPLATE_ID token. That way,
16169 should we re-parse the token stream, we will not have to repeat
16170 the effort required to do the parse, nor will we issue duplicate
16171 error messages about problems during instantiation of the
16172 template. */
16173 if (start_of_id
16174 /* Don't do this if we had a parse error in a declarator; re-parsing
16175 might succeed if a name changes meaning (60361). */
16176 && !(cp_parser_error_occurred (parser)
16177 && cp_parser_parsing_tentatively (parser)
16178 && parser->in_declarator_p))
16180 /* Reset the contents of the START_OF_ID token. */
16181 token->type = CPP_TEMPLATE_ID;
16182 token->location = combined_loc;
16184 /* Retrieve any deferred checks. Do not pop this access checks yet
16185 so the memory will not be reclaimed during token replacing below. */
16186 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16187 token->u.tree_check_value->value = template_id;
16188 token->u.tree_check_value->checks = get_deferred_access_checks ();
16189 token->keyword = RID_MAX;
16191 /* Purge all subsequent tokens. */
16192 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16194 /* ??? Can we actually assume that, if template_id ==
16195 error_mark_node, we will have issued a diagnostic to the
16196 user, as opposed to simply marking the tentative parse as
16197 failed? */
16198 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16199 error_at (token->location, "parse error in template argument list");
16202 pop_to_parent_deferring_access_checks ();
16203 return template_id;
16206 /* Parse a template-name.
16208 template-name:
16209 identifier
16211 The standard should actually say:
16213 template-name:
16214 identifier
16215 operator-function-id
16217 A defect report has been filed about this issue.
16219 A conversion-function-id cannot be a template name because they cannot
16220 be part of a template-id. In fact, looking at this code:
16222 a.operator K<int>()
16224 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16225 It is impossible to call a templated conversion-function-id with an
16226 explicit argument list, since the only allowed template parameter is
16227 the type to which it is converting.
16229 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16230 `template' keyword, in a construction like:
16232 T::template f<3>()
16234 In that case `f' is taken to be a template-name, even though there
16235 is no way of knowing for sure.
16237 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16238 name refers to a set of overloaded functions, at least one of which
16239 is a template, or an IDENTIFIER_NODE with the name of the template,
16240 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16241 names are looked up inside uninstantiated templates. */
16243 static tree
16244 cp_parser_template_name (cp_parser* parser,
16245 bool template_keyword_p,
16246 bool check_dependency_p,
16247 bool is_declaration,
16248 enum tag_types tag_type,
16249 bool *is_identifier)
16251 tree identifier;
16252 tree decl;
16253 cp_token *token = cp_lexer_peek_token (parser->lexer);
16255 /* If the next token is `operator', then we have either an
16256 operator-function-id or a conversion-function-id. */
16257 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16259 /* We don't know whether we're looking at an
16260 operator-function-id or a conversion-function-id. */
16261 cp_parser_parse_tentatively (parser);
16262 /* Try an operator-function-id. */
16263 identifier = cp_parser_operator_function_id (parser);
16264 /* If that didn't work, try a conversion-function-id. */
16265 if (!cp_parser_parse_definitely (parser))
16267 cp_parser_error (parser, "expected template-name");
16268 return error_mark_node;
16271 /* Look for the identifier. */
16272 else
16273 identifier = cp_parser_identifier (parser);
16275 /* If we didn't find an identifier, we don't have a template-id. */
16276 if (identifier == error_mark_node)
16277 return error_mark_node;
16279 /* If the name immediately followed the `template' keyword, then it
16280 is a template-name. However, if the next token is not `<', then
16281 we do not treat it as a template-name, since it is not being used
16282 as part of a template-id. This enables us to handle constructs
16283 like:
16285 template <typename T> struct S { S(); };
16286 template <typename T> S<T>::S();
16288 correctly. We would treat `S' as a template -- if it were `S<T>'
16289 -- but we do not if there is no `<'. */
16291 if (processing_template_decl
16292 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16294 /* In a declaration, in a dependent context, we pretend that the
16295 "template" keyword was present in order to improve error
16296 recovery. For example, given:
16298 template <typename T> void f(T::X<int>);
16300 we want to treat "X<int>" as a template-id. */
16301 if (is_declaration
16302 && !template_keyword_p
16303 && parser->scope && TYPE_P (parser->scope)
16304 && check_dependency_p
16305 && dependent_scope_p (parser->scope)
16306 /* Do not do this for dtors (or ctors), since they never
16307 need the template keyword before their name. */
16308 && !constructor_name_p (identifier, parser->scope))
16310 cp_token_position start = 0;
16312 /* Explain what went wrong. */
16313 error_at (token->location, "non-template %qD used as template",
16314 identifier);
16315 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16316 parser->scope, identifier);
16317 /* If parsing tentatively, find the location of the "<" token. */
16318 if (cp_parser_simulate_error (parser))
16319 start = cp_lexer_token_position (parser->lexer, true);
16320 /* Parse the template arguments so that we can issue error
16321 messages about them. */
16322 cp_lexer_consume_token (parser->lexer);
16323 cp_parser_enclosed_template_argument_list (parser);
16324 /* Skip tokens until we find a good place from which to
16325 continue parsing. */
16326 cp_parser_skip_to_closing_parenthesis (parser,
16327 /*recovering=*/true,
16328 /*or_comma=*/true,
16329 /*consume_paren=*/false);
16330 /* If parsing tentatively, permanently remove the
16331 template argument list. That will prevent duplicate
16332 error messages from being issued about the missing
16333 "template" keyword. */
16334 if (start)
16335 cp_lexer_purge_tokens_after (parser->lexer, start);
16336 if (is_identifier)
16337 *is_identifier = true;
16338 parser->context->object_type = NULL_TREE;
16339 return identifier;
16342 /* If the "template" keyword is present, then there is generally
16343 no point in doing name-lookup, so we just return IDENTIFIER.
16344 But, if the qualifying scope is non-dependent then we can
16345 (and must) do name-lookup normally. */
16346 if (template_keyword_p)
16348 tree scope = (parser->scope ? parser->scope
16349 : parser->context->object_type);
16350 if (scope && TYPE_P (scope)
16351 && (!CLASS_TYPE_P (scope)
16352 || (check_dependency_p && dependent_type_p (scope))))
16354 /* We're optimizing away the call to cp_parser_lookup_name, but
16355 we still need to do this. */
16356 parser->context->object_type = NULL_TREE;
16357 return identifier;
16362 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16363 const bool scoped_p = ((parser->scope ? parser->scope
16364 : parser->context->object_type) != NULL_TREE);
16366 /* Look up the name. */
16367 decl = cp_parser_lookup_name (parser, identifier,
16368 tag_type,
16369 /*is_template=*/true,
16370 /*is_namespace=*/false,
16371 check_dependency_p,
16372 /*ambiguous_decls=*/NULL,
16373 token->location);
16375 decl = strip_using_decl (decl);
16377 /* If DECL is a template, then the name was a template-name. */
16378 if (TREE_CODE (decl) == TEMPLATE_DECL)
16380 if (TREE_DEPRECATED (decl)
16381 && deprecated_state != DEPRECATED_SUPPRESS)
16382 warn_deprecated_use (decl, NULL_TREE);
16384 else
16386 /* The standard does not explicitly indicate whether a name that
16387 names a set of overloaded declarations, some of which are
16388 templates, is a template-name. However, such a name should
16389 be a template-name; otherwise, there is no way to form a
16390 template-id for the overloaded templates. */
16391 bool found = false;
16393 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16394 !found && iter; ++iter)
16395 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16396 found = true;
16398 if (!found
16399 && (cxx_dialect > cxx17)
16400 && !scoped_p
16401 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16403 /* [temp.names] says "A name is also considered to refer to a template
16404 if it is an unqualified-id followed by a < and name lookup finds
16405 either one or more functions or finds nothing." */
16407 /* The "more functions" case. Just use the OVERLOAD as normally.
16408 We don't use is_overloaded_fn here to avoid considering
16409 BASELINKs. */
16410 if (TREE_CODE (decl) == OVERLOAD
16411 /* Name lookup found one function. */
16412 || TREE_CODE (decl) == FUNCTION_DECL)
16413 found = true;
16414 /* Name lookup found nothing. */
16415 else if (decl == error_mark_node)
16416 return identifier;
16419 if (!found)
16421 /* The name does not name a template. */
16422 cp_parser_error (parser, "expected template-name");
16423 return error_mark_node;
16427 return decl;
16430 /* Parse a template-argument-list.
16432 template-argument-list:
16433 template-argument ... [opt]
16434 template-argument-list , template-argument ... [opt]
16436 Returns a TREE_VEC containing the arguments. */
16438 static tree
16439 cp_parser_template_argument_list (cp_parser* parser)
16441 tree fixed_args[10];
16442 unsigned n_args = 0;
16443 unsigned alloced = 10;
16444 tree *arg_ary = fixed_args;
16445 tree vec;
16446 bool saved_in_template_argument_list_p;
16447 bool saved_ice_p;
16448 bool saved_non_ice_p;
16450 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16451 parser->in_template_argument_list_p = true;
16452 /* Even if the template-id appears in an integral
16453 constant-expression, the contents of the argument list do
16454 not. */
16455 saved_ice_p = parser->integral_constant_expression_p;
16456 parser->integral_constant_expression_p = false;
16457 saved_non_ice_p = parser->non_integral_constant_expression_p;
16458 parser->non_integral_constant_expression_p = false;
16460 /* Parse the arguments. */
16463 tree argument;
16465 if (n_args)
16466 /* Consume the comma. */
16467 cp_lexer_consume_token (parser->lexer);
16469 /* Parse the template-argument. */
16470 argument = cp_parser_template_argument (parser);
16472 /* If the next token is an ellipsis, we're expanding a template
16473 argument pack. */
16474 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16476 if (argument == error_mark_node)
16478 cp_token *token = cp_lexer_peek_token (parser->lexer);
16479 error_at (token->location,
16480 "expected parameter pack before %<...%>");
16482 /* Consume the `...' token. */
16483 cp_lexer_consume_token (parser->lexer);
16485 /* Make the argument into a TYPE_PACK_EXPANSION or
16486 EXPR_PACK_EXPANSION. */
16487 argument = make_pack_expansion (argument);
16490 if (n_args == alloced)
16492 alloced *= 2;
16494 if (arg_ary == fixed_args)
16496 arg_ary = XNEWVEC (tree, alloced);
16497 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16499 else
16500 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16502 arg_ary[n_args++] = argument;
16504 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16506 vec = make_tree_vec (n_args);
16508 while (n_args--)
16509 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16511 if (arg_ary != fixed_args)
16512 free (arg_ary);
16513 parser->non_integral_constant_expression_p = saved_non_ice_p;
16514 parser->integral_constant_expression_p = saved_ice_p;
16515 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16516 if (CHECKING_P)
16517 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16518 return vec;
16521 /* Parse a template-argument.
16523 template-argument:
16524 assignment-expression
16525 type-id
16526 id-expression
16528 The representation is that of an assignment-expression, type-id, or
16529 id-expression -- except that the qualified id-expression is
16530 evaluated, so that the value returned is either a DECL or an
16531 OVERLOAD.
16533 Although the standard says "assignment-expression", it forbids
16534 throw-expressions or assignments in the template argument.
16535 Therefore, we use "conditional-expression" instead. */
16537 static tree
16538 cp_parser_template_argument (cp_parser* parser)
16540 tree argument;
16541 bool template_p;
16542 bool address_p;
16543 bool maybe_type_id = false;
16544 cp_token *token = NULL, *argument_start_token = NULL;
16545 location_t loc = 0;
16546 cp_id_kind idk;
16548 /* There's really no way to know what we're looking at, so we just
16549 try each alternative in order.
16551 [temp.arg]
16553 In a template-argument, an ambiguity between a type-id and an
16554 expression is resolved to a type-id, regardless of the form of
16555 the corresponding template-parameter.
16557 Therefore, we try a type-id first. */
16558 cp_parser_parse_tentatively (parser);
16559 argument = cp_parser_template_type_arg (parser);
16560 /* If there was no error parsing the type-id but the next token is a
16561 '>>', our behavior depends on which dialect of C++ we're
16562 parsing. In C++98, we probably found a typo for '> >'. But there
16563 are type-id which are also valid expressions. For instance:
16565 struct X { int operator >> (int); };
16566 template <int V> struct Foo {};
16567 Foo<X () >> 5> r;
16569 Here 'X()' is a valid type-id of a function type, but the user just
16570 wanted to write the expression "X() >> 5". Thus, we remember that we
16571 found a valid type-id, but we still try to parse the argument as an
16572 expression to see what happens.
16574 In C++0x, the '>>' will be considered two separate '>'
16575 tokens. */
16576 if (!cp_parser_error_occurred (parser)
16577 && cxx_dialect == cxx98
16578 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16580 maybe_type_id = true;
16581 cp_parser_abort_tentative_parse (parser);
16583 else
16585 /* If the next token isn't a `,' or a `>', then this argument wasn't
16586 really finished. This means that the argument is not a valid
16587 type-id. */
16588 if (!cp_parser_next_token_ends_template_argument_p (parser))
16589 cp_parser_error (parser, "expected template-argument");
16590 /* If that worked, we're done. */
16591 if (cp_parser_parse_definitely (parser))
16592 return argument;
16594 /* We're still not sure what the argument will be. */
16595 cp_parser_parse_tentatively (parser);
16596 /* Try a template. */
16597 argument_start_token = cp_lexer_peek_token (parser->lexer);
16598 argument = cp_parser_id_expression (parser,
16599 /*template_keyword_p=*/false,
16600 /*check_dependency_p=*/true,
16601 &template_p,
16602 /*declarator_p=*/false,
16603 /*optional_p=*/false);
16604 /* If the next token isn't a `,' or a `>', then this argument wasn't
16605 really finished. */
16606 if (!cp_parser_next_token_ends_template_argument_p (parser))
16607 cp_parser_error (parser, "expected template-argument");
16608 if (!cp_parser_error_occurred (parser))
16610 /* Figure out what is being referred to. If the id-expression
16611 was for a class template specialization, then we will have a
16612 TYPE_DECL at this point. There is no need to do name lookup
16613 at this point in that case. */
16614 if (TREE_CODE (argument) != TYPE_DECL)
16615 argument = cp_parser_lookup_name (parser, argument,
16616 none_type,
16617 /*is_template=*/template_p,
16618 /*is_namespace=*/false,
16619 /*check_dependency=*/true,
16620 /*ambiguous_decls=*/NULL,
16621 argument_start_token->location);
16622 /* Handle a constrained-type-specifier for a non-type template
16623 parameter. */
16624 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16625 argument = decl;
16626 else if (TREE_CODE (argument) != TEMPLATE_DECL
16627 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16628 cp_parser_error (parser, "expected template-name");
16630 if (cp_parser_parse_definitely (parser))
16632 if (TREE_DEPRECATED (argument))
16633 warn_deprecated_use (argument, NULL_TREE);
16634 return argument;
16636 /* It must be a non-type argument. In C++17 any constant-expression is
16637 allowed. */
16638 if (cxx_dialect > cxx14)
16639 goto general_expr;
16641 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16643 -- an integral constant-expression of integral or enumeration
16644 type; or
16646 -- the name of a non-type template-parameter; or
16648 -- the name of an object or function with external linkage...
16650 -- the address of an object or function with external linkage...
16652 -- a pointer to member... */
16653 /* Look for a non-type template parameter. */
16654 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16656 cp_parser_parse_tentatively (parser);
16657 argument = cp_parser_primary_expression (parser,
16658 /*address_p=*/false,
16659 /*cast_p=*/false,
16660 /*template_arg_p=*/true,
16661 &idk);
16662 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16663 || !cp_parser_next_token_ends_template_argument_p (parser))
16664 cp_parser_simulate_error (parser);
16665 if (cp_parser_parse_definitely (parser))
16666 return argument;
16669 /* If the next token is "&", the argument must be the address of an
16670 object or function with external linkage. */
16671 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16672 if (address_p)
16674 loc = cp_lexer_peek_token (parser->lexer)->location;
16675 cp_lexer_consume_token (parser->lexer);
16677 /* See if we might have an id-expression. */
16678 token = cp_lexer_peek_token (parser->lexer);
16679 if (token->type == CPP_NAME
16680 || token->keyword == RID_OPERATOR
16681 || token->type == CPP_SCOPE
16682 || token->type == CPP_TEMPLATE_ID
16683 || token->type == CPP_NESTED_NAME_SPECIFIER)
16685 cp_parser_parse_tentatively (parser);
16686 argument = cp_parser_primary_expression (parser,
16687 address_p,
16688 /*cast_p=*/false,
16689 /*template_arg_p=*/true,
16690 &idk);
16691 if (cp_parser_error_occurred (parser)
16692 || !cp_parser_next_token_ends_template_argument_p (parser))
16693 cp_parser_abort_tentative_parse (parser);
16694 else
16696 tree probe;
16698 if (INDIRECT_REF_P (argument))
16700 /* Strip the dereference temporarily. */
16701 gcc_assert (REFERENCE_REF_P (argument));
16702 argument = TREE_OPERAND (argument, 0);
16705 /* If we're in a template, we represent a qualified-id referring
16706 to a static data member as a SCOPE_REF even if the scope isn't
16707 dependent so that we can check access control later. */
16708 probe = argument;
16709 if (TREE_CODE (probe) == SCOPE_REF)
16710 probe = TREE_OPERAND (probe, 1);
16711 if (VAR_P (probe))
16713 /* A variable without external linkage might still be a
16714 valid constant-expression, so no error is issued here
16715 if the external-linkage check fails. */
16716 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16717 cp_parser_simulate_error (parser);
16719 else if (is_overloaded_fn (argument))
16720 /* All overloaded functions are allowed; if the external
16721 linkage test does not pass, an error will be issued
16722 later. */
16724 else if (address_p
16725 && (TREE_CODE (argument) == OFFSET_REF
16726 || TREE_CODE (argument) == SCOPE_REF))
16727 /* A pointer-to-member. */
16729 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16731 else
16732 cp_parser_simulate_error (parser);
16734 if (cp_parser_parse_definitely (parser))
16736 if (address_p)
16737 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16738 tf_warning_or_error);
16739 else
16740 argument = convert_from_reference (argument);
16741 return argument;
16745 /* If the argument started with "&", there are no other valid
16746 alternatives at this point. */
16747 if (address_p)
16749 cp_parser_error (parser, "invalid non-type template argument");
16750 return error_mark_node;
16753 general_expr:
16754 /* If the argument wasn't successfully parsed as a type-id followed
16755 by '>>', the argument can only be a constant expression now.
16756 Otherwise, we try parsing the constant-expression tentatively,
16757 because the argument could really be a type-id. */
16758 if (maybe_type_id)
16759 cp_parser_parse_tentatively (parser);
16761 if (cxx_dialect <= cxx14)
16762 argument = cp_parser_constant_expression (parser);
16763 else
16765 /* With C++17 generalized non-type template arguments we need to handle
16766 lvalue constant expressions, too. */
16767 argument = cp_parser_assignment_expression (parser);
16768 require_potential_constant_expression (argument);
16771 if (!maybe_type_id)
16772 return argument;
16773 if (!cp_parser_next_token_ends_template_argument_p (parser))
16774 cp_parser_error (parser, "expected template-argument");
16775 if (cp_parser_parse_definitely (parser))
16776 return argument;
16777 /* We did our best to parse the argument as a non type-id, but that
16778 was the only alternative that matched (albeit with a '>' after
16779 it). We can assume it's just a typo from the user, and a
16780 diagnostic will then be issued. */
16781 return cp_parser_template_type_arg (parser);
16784 /* Parse an explicit-instantiation.
16786 explicit-instantiation:
16787 template declaration
16789 Although the standard says `declaration', what it really means is:
16791 explicit-instantiation:
16792 template decl-specifier-seq [opt] declarator [opt] ;
16794 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16795 supposed to be allowed. A defect report has been filed about this
16796 issue.
16798 GNU Extension:
16800 explicit-instantiation:
16801 storage-class-specifier template
16802 decl-specifier-seq [opt] declarator [opt] ;
16803 function-specifier template
16804 decl-specifier-seq [opt] declarator [opt] ; */
16806 static void
16807 cp_parser_explicit_instantiation (cp_parser* parser)
16809 int declares_class_or_enum;
16810 cp_decl_specifier_seq decl_specifiers;
16811 tree extension_specifier = NULL_TREE;
16813 timevar_push (TV_TEMPLATE_INST);
16815 /* Look for an (optional) storage-class-specifier or
16816 function-specifier. */
16817 if (cp_parser_allow_gnu_extensions_p (parser))
16819 extension_specifier
16820 = cp_parser_storage_class_specifier_opt (parser);
16821 if (!extension_specifier)
16822 extension_specifier
16823 = cp_parser_function_specifier_opt (parser,
16824 /*decl_specs=*/NULL);
16827 /* Look for the `template' keyword. */
16828 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16829 /* Let the front end know that we are processing an explicit
16830 instantiation. */
16831 begin_explicit_instantiation ();
16832 /* [temp.explicit] says that we are supposed to ignore access
16833 control while processing explicit instantiation directives. */
16834 push_deferring_access_checks (dk_no_check);
16835 /* Parse a decl-specifier-seq. */
16836 cp_parser_decl_specifier_seq (parser,
16837 CP_PARSER_FLAGS_OPTIONAL,
16838 &decl_specifiers,
16839 &declares_class_or_enum);
16840 /* If there was exactly one decl-specifier, and it declared a class,
16841 and there's no declarator, then we have an explicit type
16842 instantiation. */
16843 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16845 tree type;
16847 type = check_tag_decl (&decl_specifiers,
16848 /*explicit_type_instantiation_p=*/true);
16849 /* Turn access control back on for names used during
16850 template instantiation. */
16851 pop_deferring_access_checks ();
16852 if (type)
16853 do_type_instantiation (type, extension_specifier,
16854 /*complain=*/tf_error);
16856 else
16858 cp_declarator *declarator;
16859 tree decl;
16861 /* Parse the declarator. */
16862 declarator
16863 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16864 /*ctor_dtor_or_conv_p=*/NULL,
16865 /*parenthesized_p=*/NULL,
16866 /*member_p=*/false,
16867 /*friend_p=*/false);
16868 if (declares_class_or_enum & 2)
16869 cp_parser_check_for_definition_in_return_type (declarator,
16870 decl_specifiers.type,
16871 decl_specifiers.locations[ds_type_spec]);
16872 if (declarator != cp_error_declarator)
16874 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16875 permerror (decl_specifiers.locations[ds_inline],
16876 "explicit instantiation shall not use"
16877 " %<inline%> specifier");
16878 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16879 permerror (decl_specifiers.locations[ds_constexpr],
16880 "explicit instantiation shall not use"
16881 " %<constexpr%> specifier");
16883 decl = grokdeclarator (declarator, &decl_specifiers,
16884 NORMAL, 0, &decl_specifiers.attributes);
16885 /* Turn access control back on for names used during
16886 template instantiation. */
16887 pop_deferring_access_checks ();
16888 /* Do the explicit instantiation. */
16889 do_decl_instantiation (decl, extension_specifier);
16891 else
16893 pop_deferring_access_checks ();
16894 /* Skip the body of the explicit instantiation. */
16895 cp_parser_skip_to_end_of_statement (parser);
16898 /* We're done with the instantiation. */
16899 end_explicit_instantiation ();
16901 cp_parser_consume_semicolon_at_end_of_statement (parser);
16903 timevar_pop (TV_TEMPLATE_INST);
16906 /* Parse an explicit-specialization.
16908 explicit-specialization:
16909 template < > declaration
16911 Although the standard says `declaration', what it really means is:
16913 explicit-specialization:
16914 template <> decl-specifier [opt] init-declarator [opt] ;
16915 template <> function-definition
16916 template <> explicit-specialization
16917 template <> template-declaration */
16919 static void
16920 cp_parser_explicit_specialization (cp_parser* parser)
16922 bool need_lang_pop;
16923 cp_token *token = cp_lexer_peek_token (parser->lexer);
16925 /* Look for the `template' keyword. */
16926 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16927 /* Look for the `<'. */
16928 cp_parser_require (parser, CPP_LESS, RT_LESS);
16929 /* Look for the `>'. */
16930 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16931 /* We have processed another parameter list. */
16932 ++parser->num_template_parameter_lists;
16933 /* [temp]
16935 A template ... explicit specialization ... shall not have C
16936 linkage. */
16937 if (current_lang_name == lang_name_c)
16939 error_at (token->location, "template specialization with C linkage");
16940 maybe_show_extern_c_location ();
16941 /* Give it C++ linkage to avoid confusing other parts of the
16942 front end. */
16943 push_lang_context (lang_name_cplusplus);
16944 need_lang_pop = true;
16946 else
16947 need_lang_pop = false;
16948 /* Let the front end know that we are beginning a specialization. */
16949 if (!begin_specialization ())
16951 end_specialization ();
16952 return;
16955 /* If the next keyword is `template', we need to figure out whether
16956 or not we're looking a template-declaration. */
16957 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16959 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16960 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16961 cp_parser_template_declaration_after_export (parser,
16962 /*member_p=*/false);
16963 else
16964 cp_parser_explicit_specialization (parser);
16966 else
16967 /* Parse the dependent declaration. */
16968 cp_parser_single_declaration (parser,
16969 /*checks=*/NULL,
16970 /*member_p=*/false,
16971 /*explicit_specialization_p=*/true,
16972 /*friend_p=*/NULL);
16973 /* We're done with the specialization. */
16974 end_specialization ();
16975 /* For the erroneous case of a template with C linkage, we pushed an
16976 implicit C++ linkage scope; exit that scope now. */
16977 if (need_lang_pop)
16978 pop_lang_context ();
16979 /* We're done with this parameter list. */
16980 --parser->num_template_parameter_lists;
16983 /* Parse a type-specifier.
16985 type-specifier:
16986 simple-type-specifier
16987 class-specifier
16988 enum-specifier
16989 elaborated-type-specifier
16990 cv-qualifier
16992 GNU Extension:
16994 type-specifier:
16995 __complex__
16997 Returns a representation of the type-specifier. For a
16998 class-specifier, enum-specifier, or elaborated-type-specifier, a
16999 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17001 The parser flags FLAGS is used to control type-specifier parsing.
17003 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17004 in a decl-specifier-seq.
17006 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17007 class-specifier, enum-specifier, or elaborated-type-specifier, then
17008 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17009 if a type is declared; 2 if it is defined. Otherwise, it is set to
17010 zero.
17012 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17013 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17014 is set to FALSE. */
17016 static tree
17017 cp_parser_type_specifier (cp_parser* parser,
17018 cp_parser_flags flags,
17019 cp_decl_specifier_seq *decl_specs,
17020 bool is_declaration,
17021 int* declares_class_or_enum,
17022 bool* is_cv_qualifier)
17024 tree type_spec = NULL_TREE;
17025 cp_token *token;
17026 enum rid keyword;
17027 cp_decl_spec ds = ds_last;
17029 /* Assume this type-specifier does not declare a new type. */
17030 if (declares_class_or_enum)
17031 *declares_class_or_enum = 0;
17032 /* And that it does not specify a cv-qualifier. */
17033 if (is_cv_qualifier)
17034 *is_cv_qualifier = false;
17035 /* Peek at the next token. */
17036 token = cp_lexer_peek_token (parser->lexer);
17038 /* If we're looking at a keyword, we can use that to guide the
17039 production we choose. */
17040 keyword = token->keyword;
17041 switch (keyword)
17043 case RID_ENUM:
17044 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17045 goto elaborated_type_specifier;
17047 /* Look for the enum-specifier. */
17048 type_spec = cp_parser_enum_specifier (parser);
17049 /* If that worked, we're done. */
17050 if (type_spec)
17052 if (declares_class_or_enum)
17053 *declares_class_or_enum = 2;
17054 if (decl_specs)
17055 cp_parser_set_decl_spec_type (decl_specs,
17056 type_spec,
17057 token,
17058 /*type_definition_p=*/true);
17059 return type_spec;
17061 else
17062 goto elaborated_type_specifier;
17064 /* Any of these indicate either a class-specifier, or an
17065 elaborated-type-specifier. */
17066 case RID_CLASS:
17067 case RID_STRUCT:
17068 case RID_UNION:
17069 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17070 goto elaborated_type_specifier;
17072 /* Parse tentatively so that we can back up if we don't find a
17073 class-specifier. */
17074 cp_parser_parse_tentatively (parser);
17075 /* Look for the class-specifier. */
17076 type_spec = cp_parser_class_specifier (parser);
17077 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17078 /* If that worked, we're done. */
17079 if (cp_parser_parse_definitely (parser))
17081 if (declares_class_or_enum)
17082 *declares_class_or_enum = 2;
17083 if (decl_specs)
17084 cp_parser_set_decl_spec_type (decl_specs,
17085 type_spec,
17086 token,
17087 /*type_definition_p=*/true);
17088 return type_spec;
17091 /* Fall through. */
17092 elaborated_type_specifier:
17093 /* We're declaring (not defining) a class or enum. */
17094 if (declares_class_or_enum)
17095 *declares_class_or_enum = 1;
17097 /* Fall through. */
17098 case RID_TYPENAME:
17099 /* Look for an elaborated-type-specifier. */
17100 type_spec
17101 = (cp_parser_elaborated_type_specifier
17102 (parser,
17103 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17104 is_declaration));
17105 if (decl_specs)
17106 cp_parser_set_decl_spec_type (decl_specs,
17107 type_spec,
17108 token,
17109 /*type_definition_p=*/false);
17110 return type_spec;
17112 case RID_CONST:
17113 ds = ds_const;
17114 if (is_cv_qualifier)
17115 *is_cv_qualifier = true;
17116 break;
17118 case RID_VOLATILE:
17119 ds = ds_volatile;
17120 if (is_cv_qualifier)
17121 *is_cv_qualifier = true;
17122 break;
17124 case RID_RESTRICT:
17125 ds = ds_restrict;
17126 if (is_cv_qualifier)
17127 *is_cv_qualifier = true;
17128 break;
17130 case RID_COMPLEX:
17131 /* The `__complex__' keyword is a GNU extension. */
17132 ds = ds_complex;
17133 break;
17135 default:
17136 break;
17139 /* Handle simple keywords. */
17140 if (ds != ds_last)
17142 if (decl_specs)
17144 set_and_check_decl_spec_loc (decl_specs, ds, token);
17145 decl_specs->any_specifiers_p = true;
17147 return cp_lexer_consume_token (parser->lexer)->u.value;
17150 /* If we do not already have a type-specifier, assume we are looking
17151 at a simple-type-specifier. */
17152 type_spec = cp_parser_simple_type_specifier (parser,
17153 decl_specs,
17154 flags);
17156 /* If we didn't find a type-specifier, and a type-specifier was not
17157 optional in this context, issue an error message. */
17158 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17160 cp_parser_error (parser, "expected type specifier");
17161 return error_mark_node;
17164 return type_spec;
17167 /* Parse a simple-type-specifier.
17169 simple-type-specifier:
17170 :: [opt] nested-name-specifier [opt] type-name
17171 :: [opt] nested-name-specifier template template-id
17172 char
17173 wchar_t
17174 bool
17175 short
17177 long
17178 signed
17179 unsigned
17180 float
17181 double
17182 void
17184 C++11 Extension:
17186 simple-type-specifier:
17187 auto
17188 decltype ( expression )
17189 char16_t
17190 char32_t
17191 __underlying_type ( type-id )
17193 C++17 extension:
17195 nested-name-specifier(opt) template-name
17197 GNU Extension:
17199 simple-type-specifier:
17200 __int128
17201 __typeof__ unary-expression
17202 __typeof__ ( type-id )
17203 __typeof__ ( type-id ) { initializer-list , [opt] }
17205 Concepts Extension:
17207 simple-type-specifier:
17208 constrained-type-specifier
17210 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17211 appropriately updated. */
17213 static tree
17214 cp_parser_simple_type_specifier (cp_parser* parser,
17215 cp_decl_specifier_seq *decl_specs,
17216 cp_parser_flags flags)
17218 tree type = NULL_TREE;
17219 cp_token *token;
17220 int idx;
17222 /* Peek at the next token. */
17223 token = cp_lexer_peek_token (parser->lexer);
17225 /* If we're looking at a keyword, things are easy. */
17226 switch (token->keyword)
17228 case RID_CHAR:
17229 if (decl_specs)
17230 decl_specs->explicit_char_p = true;
17231 type = char_type_node;
17232 break;
17233 case RID_CHAR16:
17234 type = char16_type_node;
17235 break;
17236 case RID_CHAR32:
17237 type = char32_type_node;
17238 break;
17239 case RID_WCHAR:
17240 type = wchar_type_node;
17241 break;
17242 case RID_BOOL:
17243 type = boolean_type_node;
17244 break;
17245 case RID_SHORT:
17246 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17247 type = short_integer_type_node;
17248 break;
17249 case RID_INT:
17250 if (decl_specs)
17251 decl_specs->explicit_int_p = true;
17252 type = integer_type_node;
17253 break;
17254 case RID_INT_N_0:
17255 case RID_INT_N_1:
17256 case RID_INT_N_2:
17257 case RID_INT_N_3:
17258 idx = token->keyword - RID_INT_N_0;
17259 if (! int_n_enabled_p [idx])
17260 break;
17261 if (decl_specs)
17263 decl_specs->explicit_intN_p = true;
17264 decl_specs->int_n_idx = idx;
17266 type = int_n_trees [idx].signed_type;
17267 break;
17268 case RID_LONG:
17269 if (decl_specs)
17270 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17271 type = long_integer_type_node;
17272 break;
17273 case RID_SIGNED:
17274 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17275 type = integer_type_node;
17276 break;
17277 case RID_UNSIGNED:
17278 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17279 type = unsigned_type_node;
17280 break;
17281 case RID_FLOAT:
17282 type = float_type_node;
17283 break;
17284 case RID_DOUBLE:
17285 type = double_type_node;
17286 break;
17287 case RID_VOID:
17288 type = void_type_node;
17289 break;
17291 case RID_AUTO:
17292 maybe_warn_cpp0x (CPP0X_AUTO);
17293 if (parser->auto_is_implicit_function_template_parm_p)
17295 /* The 'auto' might be the placeholder return type for a function decl
17296 with trailing return type. */
17297 bool have_trailing_return_fn_decl = false;
17299 cp_parser_parse_tentatively (parser);
17300 cp_lexer_consume_token (parser->lexer);
17301 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17302 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17303 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17304 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17306 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17308 cp_lexer_consume_token (parser->lexer);
17309 cp_parser_skip_to_closing_parenthesis (parser,
17310 /*recovering*/false,
17311 /*or_comma*/false,
17312 /*consume_paren*/true);
17313 continue;
17316 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17318 have_trailing_return_fn_decl = true;
17319 break;
17322 cp_lexer_consume_token (parser->lexer);
17324 cp_parser_abort_tentative_parse (parser);
17326 if (have_trailing_return_fn_decl)
17328 type = make_auto ();
17329 break;
17332 if (cxx_dialect >= cxx14)
17334 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17335 type = TREE_TYPE (type);
17337 else
17338 type = error_mark_node;
17340 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17342 if (cxx_dialect < cxx14)
17343 error_at (token->location,
17344 "use of %<auto%> in lambda parameter declaration "
17345 "only available with "
17346 "-std=c++14 or -std=gnu++14");
17348 else if (cxx_dialect < cxx14)
17349 error_at (token->location,
17350 "use of %<auto%> in parameter declaration "
17351 "only available with "
17352 "-std=c++14 or -std=gnu++14");
17353 else if (!flag_concepts)
17354 pedwarn (token->location, 0,
17355 "use of %<auto%> in parameter declaration "
17356 "only available with -fconcepts");
17358 else
17359 type = make_auto ();
17360 break;
17362 case RID_DECLTYPE:
17363 /* Since DR 743, decltype can either be a simple-type-specifier by
17364 itself or begin a nested-name-specifier. Parsing it will replace
17365 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17366 handling below decide what to do. */
17367 cp_parser_decltype (parser);
17368 cp_lexer_set_token_position (parser->lexer, token);
17369 break;
17371 case RID_TYPEOF:
17372 /* Consume the `typeof' token. */
17373 cp_lexer_consume_token (parser->lexer);
17374 /* Parse the operand to `typeof'. */
17375 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17376 /* If it is not already a TYPE, take its type. */
17377 if (!TYPE_P (type))
17378 type = finish_typeof (type);
17380 if (decl_specs)
17381 cp_parser_set_decl_spec_type (decl_specs, type,
17382 token,
17383 /*type_definition_p=*/false);
17385 return type;
17387 case RID_UNDERLYING_TYPE:
17388 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17389 if (decl_specs)
17390 cp_parser_set_decl_spec_type (decl_specs, type,
17391 token,
17392 /*type_definition_p=*/false);
17394 return type;
17396 case RID_BASES:
17397 case RID_DIRECT_BASES:
17398 type = cp_parser_trait_expr (parser, token->keyword);
17399 if (decl_specs)
17400 cp_parser_set_decl_spec_type (decl_specs, type,
17401 token,
17402 /*type_definition_p=*/false);
17403 return type;
17404 default:
17405 break;
17408 /* If token is an already-parsed decltype not followed by ::,
17409 it's a simple-type-specifier. */
17410 if (token->type == CPP_DECLTYPE
17411 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17413 type = saved_checks_value (token->u.tree_check_value);
17414 if (decl_specs)
17416 cp_parser_set_decl_spec_type (decl_specs, type,
17417 token,
17418 /*type_definition_p=*/false);
17419 /* Remember that we are handling a decltype in order to
17420 implement the resolution of DR 1510 when the argument
17421 isn't instantiation dependent. */
17422 decl_specs->decltype_p = true;
17424 cp_lexer_consume_token (parser->lexer);
17425 return type;
17428 /* If the type-specifier was for a built-in type, we're done. */
17429 if (type)
17431 /* Record the type. */
17432 if (decl_specs
17433 && (token->keyword != RID_SIGNED
17434 && token->keyword != RID_UNSIGNED
17435 && token->keyword != RID_SHORT
17436 && token->keyword != RID_LONG))
17437 cp_parser_set_decl_spec_type (decl_specs,
17438 type,
17439 token,
17440 /*type_definition_p=*/false);
17441 if (decl_specs)
17442 decl_specs->any_specifiers_p = true;
17444 /* Consume the token. */
17445 cp_lexer_consume_token (parser->lexer);
17447 if (type == error_mark_node)
17448 return error_mark_node;
17450 /* There is no valid C++ program where a non-template type is
17451 followed by a "<". That usually indicates that the user thought
17452 that the type was a template. */
17453 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17454 token->location);
17456 return TYPE_NAME (type);
17459 /* The type-specifier must be a user-defined type. */
17460 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17462 bool qualified_p;
17463 bool global_p;
17465 /* Don't gobble tokens or issue error messages if this is an
17466 optional type-specifier. */
17467 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17468 cp_parser_parse_tentatively (parser);
17470 token = cp_lexer_peek_token (parser->lexer);
17472 /* Look for the optional `::' operator. */
17473 global_p
17474 = (cp_parser_global_scope_opt (parser,
17475 /*current_scope_valid_p=*/false)
17476 != NULL_TREE);
17477 /* Look for the nested-name specifier. */
17478 qualified_p
17479 = (cp_parser_nested_name_specifier_opt (parser,
17480 /*typename_keyword_p=*/false,
17481 /*check_dependency_p=*/true,
17482 /*type_p=*/false,
17483 /*is_declaration=*/false)
17484 != NULL_TREE);
17485 /* If we have seen a nested-name-specifier, and the next token
17486 is `template', then we are using the template-id production. */
17487 if (parser->scope
17488 && cp_parser_optional_template_keyword (parser))
17490 /* Look for the template-id. */
17491 type = cp_parser_template_id (parser,
17492 /*template_keyword_p=*/true,
17493 /*check_dependency_p=*/true,
17494 none_type,
17495 /*is_declaration=*/false);
17496 /* If the template-id did not name a type, we are out of
17497 luck. */
17498 if (TREE_CODE (type) != TYPE_DECL)
17500 cp_parser_error (parser, "expected template-id for type");
17501 type = NULL_TREE;
17504 /* Otherwise, look for a type-name. */
17505 else
17506 type = cp_parser_type_name (parser);
17507 /* Keep track of all name-lookups performed in class scopes. */
17508 if (type
17509 && !global_p
17510 && !qualified_p
17511 && TREE_CODE (type) == TYPE_DECL
17512 && identifier_p (DECL_NAME (type)))
17513 maybe_note_name_used_in_class (DECL_NAME (type), type);
17514 /* If it didn't work out, we don't have a TYPE. */
17515 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17516 && !cp_parser_parse_definitely (parser))
17517 type = NULL_TREE;
17518 if (!type && cxx_dialect >= cxx17)
17520 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17521 cp_parser_parse_tentatively (parser);
17523 cp_parser_global_scope_opt (parser,
17524 /*current_scope_valid_p=*/false);
17525 cp_parser_nested_name_specifier_opt (parser,
17526 /*typename_keyword_p=*/false,
17527 /*check_dependency_p=*/true,
17528 /*type_p=*/false,
17529 /*is_declaration=*/false);
17530 tree name = cp_parser_identifier (parser);
17531 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17532 && parser->scope != error_mark_node)
17534 tree tmpl = cp_parser_lookup_name (parser, name,
17535 none_type,
17536 /*is_template=*/false,
17537 /*is_namespace=*/false,
17538 /*check_dependency=*/true,
17539 /*ambiguous_decls=*/NULL,
17540 token->location);
17541 if (tmpl && tmpl != error_mark_node
17542 && (DECL_CLASS_TEMPLATE_P (tmpl)
17543 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17544 type = make_template_placeholder (tmpl);
17545 else
17547 type = error_mark_node;
17548 if (!cp_parser_simulate_error (parser))
17549 cp_parser_name_lookup_error (parser, name, tmpl,
17550 NLE_TYPE, token->location);
17553 else
17554 type = error_mark_node;
17556 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17557 && !cp_parser_parse_definitely (parser))
17558 type = NULL_TREE;
17560 if (type && decl_specs)
17561 cp_parser_set_decl_spec_type (decl_specs, type,
17562 token,
17563 /*type_definition_p=*/false);
17566 /* If we didn't get a type-name, issue an error message. */
17567 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17569 cp_parser_error (parser, "expected type-name");
17570 return error_mark_node;
17573 if (type && type != error_mark_node)
17575 /* See if TYPE is an Objective-C type, and if so, parse and
17576 accept any protocol references following it. Do this before
17577 the cp_parser_check_for_invalid_template_id() call, because
17578 Objective-C types can be followed by '<...>' which would
17579 enclose protocol names rather than template arguments, and so
17580 everything is fine. */
17581 if (c_dialect_objc () && !parser->scope
17582 && (objc_is_id (type) || objc_is_class_name (type)))
17584 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17585 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17587 /* Clobber the "unqualified" type previously entered into
17588 DECL_SPECS with the new, improved protocol-qualified version. */
17589 if (decl_specs)
17590 decl_specs->type = qual_type;
17592 return qual_type;
17595 /* There is no valid C++ program where a non-template type is
17596 followed by a "<". That usually indicates that the user
17597 thought that the type was a template. */
17598 cp_parser_check_for_invalid_template_id (parser, type,
17599 none_type,
17600 token->location);
17603 return type;
17606 /* Parse a type-name.
17608 type-name:
17609 class-name
17610 enum-name
17611 typedef-name
17612 simple-template-id [in c++0x]
17614 enum-name:
17615 identifier
17617 typedef-name:
17618 identifier
17620 Concepts:
17622 type-name:
17623 concept-name
17624 partial-concept-id
17626 concept-name:
17627 identifier
17629 Returns a TYPE_DECL for the type. */
17631 static tree
17632 cp_parser_type_name (cp_parser* parser)
17634 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17637 /* See above. */
17638 static tree
17639 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17641 tree type_decl;
17643 /* We can't know yet whether it is a class-name or not. */
17644 cp_parser_parse_tentatively (parser);
17645 /* Try a class-name. */
17646 type_decl = cp_parser_class_name (parser,
17647 typename_keyword_p,
17648 /*template_keyword_p=*/false,
17649 none_type,
17650 /*check_dependency_p=*/true,
17651 /*class_head_p=*/false,
17652 /*is_declaration=*/false);
17653 /* If it's not a class-name, keep looking. */
17654 if (!cp_parser_parse_definitely (parser))
17656 if (cxx_dialect < cxx11)
17657 /* It must be a typedef-name or an enum-name. */
17658 return cp_parser_nonclass_name (parser);
17660 cp_parser_parse_tentatively (parser);
17661 /* It is either a simple-template-id representing an
17662 instantiation of an alias template... */
17663 type_decl = cp_parser_template_id (parser,
17664 /*template_keyword_p=*/false,
17665 /*check_dependency_p=*/true,
17666 none_type,
17667 /*is_declaration=*/false);
17668 /* Note that this must be an instantiation of an alias template
17669 because [temp.names]/6 says:
17671 A template-id that names an alias template specialization
17672 is a type-name.
17674 Whereas [temp.names]/7 says:
17676 A simple-template-id that names a class template
17677 specialization is a class-name.
17679 With concepts, this could also be a partial-concept-id that
17680 declares a non-type template parameter. */
17681 if (type_decl != NULL_TREE
17682 && TREE_CODE (type_decl) == TYPE_DECL
17683 && TYPE_DECL_ALIAS_P (type_decl))
17684 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17685 else if (is_constrained_parameter (type_decl))
17686 /* Don't do anything. */ ;
17687 else
17688 cp_parser_simulate_error (parser);
17690 if (!cp_parser_parse_definitely (parser))
17691 /* ... Or a typedef-name or an enum-name. */
17692 return cp_parser_nonclass_name (parser);
17695 return type_decl;
17698 /* Check if DECL and ARGS can form a constrained-type-specifier.
17699 If ARGS is non-null, we try to form a concept check of the
17700 form DECL<?, ARGS> where ? is a wildcard that matches any
17701 kind of template argument. If ARGS is NULL, then we try to
17702 form a concept check of the form DECL<?>. */
17704 static tree
17705 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17706 tree decl, tree args)
17708 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17710 /* If we a constrained-type-specifier cannot be deduced. */
17711 if (parser->prevent_constrained_type_specifiers)
17712 return NULL_TREE;
17714 /* A constrained type specifier can only be found in an
17715 overload set or as a reference to a template declaration.
17717 FIXME: This might be masking a bug. It's possible that
17718 that the deduction below is causing template specializations
17719 to be formed with the wildcard as an argument. */
17720 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17721 return NULL_TREE;
17723 /* Try to build a call expression that evaluates the
17724 concept. This can fail if the overload set refers
17725 only to non-templates. */
17726 tree placeholder = build_nt (WILDCARD_DECL);
17727 tree check = build_concept_check (decl, placeholder, args);
17728 if (check == error_mark_node)
17729 return NULL_TREE;
17731 /* Deduce the checked constraint and the prototype parameter.
17733 FIXME: In certain cases, failure to deduce should be a
17734 diagnosable error. */
17735 tree conc;
17736 tree proto;
17737 if (!deduce_constrained_parameter (check, conc, proto))
17738 return NULL_TREE;
17740 /* In template parameter scope, this results in a constrained
17741 parameter. Return a descriptor of that parm. */
17742 if (processing_template_parmlist)
17743 return build_constrained_parameter (conc, proto, args);
17745 /* In a parameter-declaration-clause, constrained-type
17746 specifiers result in invented template parameters. */
17747 if (parser->auto_is_implicit_function_template_parm_p)
17749 tree x = build_constrained_parameter (conc, proto, args);
17750 return synthesize_implicit_template_parm (parser, x);
17752 else
17754 /* Otherwise, we're in a context where the constrained
17755 type name is deduced and the constraint applies
17756 after deduction. */
17757 return make_constrained_auto (conc, args);
17760 return NULL_TREE;
17763 /* If DECL refers to a concept, return a TYPE_DECL representing
17764 the result of using the constrained type specifier in the
17765 current context. DECL refers to a concept if
17767 - it is an overload set containing a function concept taking a single
17768 type argument, or
17770 - it is a variable concept taking a single type argument. */
17772 static tree
17773 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17775 if (flag_concepts
17776 && (TREE_CODE (decl) == OVERLOAD
17777 || BASELINK_P (decl)
17778 || variable_concept_p (decl)))
17779 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17780 else
17781 return NULL_TREE;
17784 /* Check if DECL and ARGS form a partial-concept-id. If so,
17785 assign ID to the resulting constrained placeholder.
17787 Returns true if the partial-concept-id designates a placeholder
17788 and false otherwise. Note that *id is set to NULL_TREE in
17789 this case. */
17791 static tree
17792 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17794 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17797 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17798 or a concept-name.
17800 enum-name:
17801 identifier
17803 typedef-name:
17804 identifier
17806 concept-name:
17807 identifier
17809 Returns a TYPE_DECL for the type. */
17811 static tree
17812 cp_parser_nonclass_name (cp_parser* parser)
17814 tree type_decl;
17815 tree identifier;
17817 cp_token *token = cp_lexer_peek_token (parser->lexer);
17818 identifier = cp_parser_identifier (parser);
17819 if (identifier == error_mark_node)
17820 return error_mark_node;
17822 /* Look up the type-name. */
17823 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17825 type_decl = strip_using_decl (type_decl);
17827 /* If we found an overload set, then it may refer to a concept-name. */
17828 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17829 type_decl = decl;
17831 if (TREE_CODE (type_decl) != TYPE_DECL
17832 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17834 /* See if this is an Objective-C type. */
17835 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17836 tree type = objc_get_protocol_qualified_type (identifier, protos);
17837 if (type)
17838 type_decl = TYPE_NAME (type);
17841 /* Issue an error if we did not find a type-name. */
17842 if (TREE_CODE (type_decl) != TYPE_DECL
17843 /* In Objective-C, we have the complication that class names are
17844 normally type names and start declarations (eg, the
17845 "NSObject" in "NSObject *object;"), but can be used in an
17846 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17847 is an expression. So, a classname followed by a dot is not a
17848 valid type-name. */
17849 || (objc_is_class_name (TREE_TYPE (type_decl))
17850 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17852 if (!cp_parser_simulate_error (parser))
17853 cp_parser_name_lookup_error (parser, identifier, type_decl,
17854 NLE_TYPE, token->location);
17855 return error_mark_node;
17857 /* Remember that the name was used in the definition of the
17858 current class so that we can check later to see if the
17859 meaning would have been different after the class was
17860 entirely defined. */
17861 else if (type_decl != error_mark_node
17862 && !parser->scope)
17863 maybe_note_name_used_in_class (identifier, type_decl);
17865 return type_decl;
17868 /* Parse an elaborated-type-specifier. Note that the grammar given
17869 here incorporates the resolution to DR68.
17871 elaborated-type-specifier:
17872 class-key :: [opt] nested-name-specifier [opt] identifier
17873 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17874 enum-key :: [opt] nested-name-specifier [opt] identifier
17875 typename :: [opt] nested-name-specifier identifier
17876 typename :: [opt] nested-name-specifier template [opt]
17877 template-id
17879 GNU extension:
17881 elaborated-type-specifier:
17882 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17883 class-key attributes :: [opt] nested-name-specifier [opt]
17884 template [opt] template-id
17885 enum attributes :: [opt] nested-name-specifier [opt] identifier
17887 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17888 declared `friend'. If IS_DECLARATION is TRUE, then this
17889 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17890 something is being declared.
17892 Returns the TYPE specified. */
17894 static tree
17895 cp_parser_elaborated_type_specifier (cp_parser* parser,
17896 bool is_friend,
17897 bool is_declaration)
17899 enum tag_types tag_type;
17900 tree identifier;
17901 tree type = NULL_TREE;
17902 tree attributes = NULL_TREE;
17903 tree globalscope;
17904 cp_token *token = NULL;
17906 /* See if we're looking at the `enum' keyword. */
17907 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17909 /* Consume the `enum' token. */
17910 cp_lexer_consume_token (parser->lexer);
17911 /* Remember that it's an enumeration type. */
17912 tag_type = enum_type;
17913 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17914 enums) is used here. */
17915 cp_token *token = cp_lexer_peek_token (parser->lexer);
17916 if (cp_parser_is_keyword (token, RID_CLASS)
17917 || cp_parser_is_keyword (token, RID_STRUCT))
17919 gcc_rich_location richloc (token->location);
17920 richloc.add_range (input_location);
17921 richloc.add_fixit_remove ();
17922 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17923 "a scoped enum must not use the %qD keyword",
17924 token->u.value);
17925 /* Consume the `struct' or `class' and parse it anyway. */
17926 cp_lexer_consume_token (parser->lexer);
17928 /* Parse the attributes. */
17929 attributes = cp_parser_attributes_opt (parser);
17931 /* Or, it might be `typename'. */
17932 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17933 RID_TYPENAME))
17935 /* Consume the `typename' token. */
17936 cp_lexer_consume_token (parser->lexer);
17937 /* Remember that it's a `typename' type. */
17938 tag_type = typename_type;
17940 /* Otherwise it must be a class-key. */
17941 else
17943 tag_type = cp_parser_class_key (parser);
17944 if (tag_type == none_type)
17945 return error_mark_node;
17946 /* Parse the attributes. */
17947 attributes = cp_parser_attributes_opt (parser);
17950 /* Look for the `::' operator. */
17951 globalscope = cp_parser_global_scope_opt (parser,
17952 /*current_scope_valid_p=*/false);
17953 /* Look for the nested-name-specifier. */
17954 tree nested_name_specifier;
17955 if (tag_type == typename_type && !globalscope)
17957 nested_name_specifier
17958 = cp_parser_nested_name_specifier (parser,
17959 /*typename_keyword_p=*/true,
17960 /*check_dependency_p=*/true,
17961 /*type_p=*/true,
17962 is_declaration);
17963 if (!nested_name_specifier)
17964 return error_mark_node;
17966 else
17967 /* Even though `typename' is not present, the proposed resolution
17968 to Core Issue 180 says that in `class A<T>::B', `B' should be
17969 considered a type-name, even if `A<T>' is dependent. */
17970 nested_name_specifier
17971 = cp_parser_nested_name_specifier_opt (parser,
17972 /*typename_keyword_p=*/true,
17973 /*check_dependency_p=*/true,
17974 /*type_p=*/true,
17975 is_declaration);
17976 /* For everything but enumeration types, consider a template-id.
17977 For an enumeration type, consider only a plain identifier. */
17978 if (tag_type != enum_type)
17980 bool template_p = false;
17981 tree decl;
17983 /* Allow the `template' keyword. */
17984 template_p = cp_parser_optional_template_keyword (parser);
17985 /* If we didn't see `template', we don't know if there's a
17986 template-id or not. */
17987 if (!template_p)
17988 cp_parser_parse_tentatively (parser);
17989 /* Parse the template-id. */
17990 token = cp_lexer_peek_token (parser->lexer);
17991 decl = cp_parser_template_id (parser, template_p,
17992 /*check_dependency_p=*/true,
17993 tag_type,
17994 is_declaration);
17995 /* If we didn't find a template-id, look for an ordinary
17996 identifier. */
17997 if (!template_p && !cp_parser_parse_definitely (parser))
17999 /* We can get here when cp_parser_template_id, called by
18000 cp_parser_class_name with tag_type == none_type, succeeds
18001 and caches a BASELINK. Then, when called again here,
18002 instead of failing and returning an error_mark_node
18003 returns it (see template/typename17.C in C++11).
18004 ??? Could we diagnose this earlier? */
18005 else if (tag_type == typename_type && BASELINK_P (decl))
18007 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18008 type = error_mark_node;
18010 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18011 in effect, then we must assume that, upon instantiation, the
18012 template will correspond to a class. */
18013 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18014 && tag_type == typename_type)
18015 type = make_typename_type (parser->scope, decl,
18016 typename_type,
18017 /*complain=*/tf_error);
18018 /* If the `typename' keyword is in effect and DECL is not a type
18019 decl, then type is non existent. */
18020 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18022 else if (TREE_CODE (decl) == TYPE_DECL)
18024 type = check_elaborated_type_specifier (tag_type, decl,
18025 /*allow_template_p=*/true);
18027 /* If the next token is a semicolon, this must be a specialization,
18028 instantiation, or friend declaration. Check the scope while we
18029 still know whether or not we had a nested-name-specifier. */
18030 if (type != error_mark_node
18031 && !nested_name_specifier && !is_friend
18032 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18033 check_unqualified_spec_or_inst (type, token->location);
18035 else if (decl == error_mark_node)
18036 type = error_mark_node;
18039 if (!type)
18041 token = cp_lexer_peek_token (parser->lexer);
18042 identifier = cp_parser_identifier (parser);
18044 if (identifier == error_mark_node)
18046 parser->scope = NULL_TREE;
18047 return error_mark_node;
18050 /* For a `typename', we needn't call xref_tag. */
18051 if (tag_type == typename_type
18052 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18053 return cp_parser_make_typename_type (parser, identifier,
18054 token->location);
18056 /* Template parameter lists apply only if we are not within a
18057 function parameter list. */
18058 bool template_parm_lists_apply
18059 = parser->num_template_parameter_lists;
18060 if (template_parm_lists_apply)
18061 for (cp_binding_level *s = current_binding_level;
18062 s && s->kind != sk_template_parms;
18063 s = s->level_chain)
18064 if (s->kind == sk_function_parms)
18065 template_parm_lists_apply = false;
18067 /* Look up a qualified name in the usual way. */
18068 if (parser->scope)
18070 tree decl;
18071 tree ambiguous_decls;
18073 decl = cp_parser_lookup_name (parser, identifier,
18074 tag_type,
18075 /*is_template=*/false,
18076 /*is_namespace=*/false,
18077 /*check_dependency=*/true,
18078 &ambiguous_decls,
18079 token->location);
18081 /* If the lookup was ambiguous, an error will already have been
18082 issued. */
18083 if (ambiguous_decls)
18084 return error_mark_node;
18086 /* If we are parsing friend declaration, DECL may be a
18087 TEMPLATE_DECL tree node here. However, we need to check
18088 whether this TEMPLATE_DECL results in valid code. Consider
18089 the following example:
18091 namespace N {
18092 template <class T> class C {};
18094 class X {
18095 template <class T> friend class N::C; // #1, valid code
18097 template <class T> class Y {
18098 friend class N::C; // #2, invalid code
18101 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18102 name lookup of `N::C'. We see that friend declaration must
18103 be template for the code to be valid. Note that
18104 processing_template_decl does not work here since it is
18105 always 1 for the above two cases. */
18107 decl = (cp_parser_maybe_treat_template_as_class
18108 (decl, /*tag_name_p=*/is_friend
18109 && template_parm_lists_apply));
18111 if (TREE_CODE (decl) != TYPE_DECL)
18113 cp_parser_diagnose_invalid_type_name (parser,
18114 identifier,
18115 token->location);
18116 return error_mark_node;
18119 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18121 bool allow_template = (template_parm_lists_apply
18122 || DECL_SELF_REFERENCE_P (decl));
18123 type = check_elaborated_type_specifier (tag_type, decl,
18124 allow_template);
18126 if (type == error_mark_node)
18127 return error_mark_node;
18130 /* Forward declarations of nested types, such as
18132 class C1::C2;
18133 class C1::C2::C3;
18135 are invalid unless all components preceding the final '::'
18136 are complete. If all enclosing types are complete, these
18137 declarations become merely pointless.
18139 Invalid forward declarations of nested types are errors
18140 caught elsewhere in parsing. Those that are pointless arrive
18141 here. */
18143 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18144 && !is_friend && !processing_explicit_instantiation)
18145 warning (0, "declaration %qD does not declare anything", decl);
18147 type = TREE_TYPE (decl);
18149 else
18151 /* An elaborated-type-specifier sometimes introduces a new type and
18152 sometimes names an existing type. Normally, the rule is that it
18153 introduces a new type only if there is not an existing type of
18154 the same name already in scope. For example, given:
18156 struct S {};
18157 void f() { struct S s; }
18159 the `struct S' in the body of `f' is the same `struct S' as in
18160 the global scope; the existing definition is used. However, if
18161 there were no global declaration, this would introduce a new
18162 local class named `S'.
18164 An exception to this rule applies to the following code:
18166 namespace N { struct S; }
18168 Here, the elaborated-type-specifier names a new type
18169 unconditionally; even if there is already an `S' in the
18170 containing scope this declaration names a new type.
18171 This exception only applies if the elaborated-type-specifier
18172 forms the complete declaration:
18174 [class.name]
18176 A declaration consisting solely of `class-key identifier ;' is
18177 either a redeclaration of the name in the current scope or a
18178 forward declaration of the identifier as a class name. It
18179 introduces the name into the current scope.
18181 We are in this situation precisely when the next token is a `;'.
18183 An exception to the exception is that a `friend' declaration does
18184 *not* name a new type; i.e., given:
18186 struct S { friend struct T; };
18188 `T' is not a new type in the scope of `S'.
18190 Also, `new struct S' or `sizeof (struct S)' never results in the
18191 definition of a new type; a new type can only be declared in a
18192 declaration context. */
18194 tag_scope ts;
18195 bool template_p;
18197 if (is_friend)
18198 /* Friends have special name lookup rules. */
18199 ts = ts_within_enclosing_non_class;
18200 else if (is_declaration
18201 && cp_lexer_next_token_is (parser->lexer,
18202 CPP_SEMICOLON))
18203 /* This is a `class-key identifier ;' */
18204 ts = ts_current;
18205 else
18206 ts = ts_global;
18208 template_p =
18209 (template_parm_lists_apply
18210 && (cp_parser_next_token_starts_class_definition_p (parser)
18211 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18212 /* An unqualified name was used to reference this type, so
18213 there were no qualifying templates. */
18214 if (template_parm_lists_apply
18215 && !cp_parser_check_template_parameters (parser,
18216 /*num_templates=*/0,
18217 /*template_id*/false,
18218 token->location,
18219 /*declarator=*/NULL))
18220 return error_mark_node;
18221 type = xref_tag (tag_type, identifier, ts, template_p);
18225 if (type == error_mark_node)
18226 return error_mark_node;
18228 /* Allow attributes on forward declarations of classes. */
18229 if (attributes)
18231 if (TREE_CODE (type) == TYPENAME_TYPE)
18232 warning (OPT_Wattributes,
18233 "attributes ignored on uninstantiated type");
18234 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18235 && ! processing_explicit_instantiation)
18236 warning (OPT_Wattributes,
18237 "attributes ignored on template instantiation");
18238 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18239 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18240 else
18241 warning (OPT_Wattributes,
18242 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18245 if (tag_type != enum_type)
18247 /* Indicate whether this class was declared as a `class' or as a
18248 `struct'. */
18249 if (CLASS_TYPE_P (type))
18250 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18251 cp_parser_check_class_key (tag_type, type);
18254 /* A "<" cannot follow an elaborated type specifier. If that
18255 happens, the user was probably trying to form a template-id. */
18256 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18257 token->location);
18259 return type;
18262 /* Parse an enum-specifier.
18264 enum-specifier:
18265 enum-head { enumerator-list [opt] }
18266 enum-head { enumerator-list , } [C++0x]
18268 enum-head:
18269 enum-key identifier [opt] enum-base [opt]
18270 enum-key nested-name-specifier identifier enum-base [opt]
18272 enum-key:
18273 enum
18274 enum class [C++0x]
18275 enum struct [C++0x]
18277 enum-base: [C++0x]
18278 : type-specifier-seq
18280 opaque-enum-specifier:
18281 enum-key identifier enum-base [opt] ;
18283 GNU Extensions:
18284 enum-key attributes[opt] identifier [opt] enum-base [opt]
18285 { enumerator-list [opt] }attributes[opt]
18286 enum-key attributes[opt] identifier [opt] enum-base [opt]
18287 { enumerator-list, }attributes[opt] [C++0x]
18289 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18290 if the token stream isn't an enum-specifier after all. */
18292 static tree
18293 cp_parser_enum_specifier (cp_parser* parser)
18295 tree identifier;
18296 tree type = NULL_TREE;
18297 tree prev_scope;
18298 tree nested_name_specifier = NULL_TREE;
18299 tree attributes;
18300 bool scoped_enum_p = false;
18301 bool has_underlying_type = false;
18302 bool nested_being_defined = false;
18303 bool new_value_list = false;
18304 bool is_new_type = false;
18305 bool is_unnamed = false;
18306 tree underlying_type = NULL_TREE;
18307 cp_token *type_start_token = NULL;
18308 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18310 parser->colon_corrects_to_scope_p = false;
18312 /* Parse tentatively so that we can back up if we don't find a
18313 enum-specifier. */
18314 cp_parser_parse_tentatively (parser);
18316 /* Caller guarantees that the current token is 'enum', an identifier
18317 possibly follows, and the token after that is an opening brace.
18318 If we don't have an identifier, fabricate an anonymous name for
18319 the enumeration being defined. */
18320 cp_lexer_consume_token (parser->lexer);
18322 /* Parse the "class" or "struct", which indicates a scoped
18323 enumeration type in C++0x. */
18324 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18325 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18327 if (cxx_dialect < cxx11)
18328 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18330 /* Consume the `struct' or `class' token. */
18331 cp_lexer_consume_token (parser->lexer);
18333 scoped_enum_p = true;
18336 attributes = cp_parser_attributes_opt (parser);
18338 /* Clear the qualification. */
18339 parser->scope = NULL_TREE;
18340 parser->qualifying_scope = NULL_TREE;
18341 parser->object_scope = NULL_TREE;
18343 /* Figure out in what scope the declaration is being placed. */
18344 prev_scope = current_scope ();
18346 type_start_token = cp_lexer_peek_token (parser->lexer);
18348 push_deferring_access_checks (dk_no_check);
18349 nested_name_specifier
18350 = cp_parser_nested_name_specifier_opt (parser,
18351 /*typename_keyword_p=*/true,
18352 /*check_dependency_p=*/false,
18353 /*type_p=*/false,
18354 /*is_declaration=*/false);
18356 if (nested_name_specifier)
18358 tree name;
18360 identifier = cp_parser_identifier (parser);
18361 name = cp_parser_lookup_name (parser, identifier,
18362 enum_type,
18363 /*is_template=*/false,
18364 /*is_namespace=*/false,
18365 /*check_dependency=*/true,
18366 /*ambiguous_decls=*/NULL,
18367 input_location);
18368 if (name && name != error_mark_node)
18370 type = TREE_TYPE (name);
18371 if (TREE_CODE (type) == TYPENAME_TYPE)
18373 /* Are template enums allowed in ISO? */
18374 if (template_parm_scope_p ())
18375 pedwarn (type_start_token->location, OPT_Wpedantic,
18376 "%qD is an enumeration template", name);
18377 /* ignore a typename reference, for it will be solved by name
18378 in start_enum. */
18379 type = NULL_TREE;
18382 else if (nested_name_specifier == error_mark_node)
18383 /* We already issued an error. */;
18384 else
18386 error_at (type_start_token->location,
18387 "%qD does not name an enumeration in %qT",
18388 identifier, nested_name_specifier);
18389 nested_name_specifier = error_mark_node;
18392 else
18394 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18395 identifier = cp_parser_identifier (parser);
18396 else
18398 identifier = make_anon_name ();
18399 is_unnamed = true;
18400 if (scoped_enum_p)
18401 error_at (type_start_token->location,
18402 "unnamed scoped enum is not allowed");
18405 pop_deferring_access_checks ();
18407 /* Check for the `:' that denotes a specified underlying type in C++0x.
18408 Note that a ':' could also indicate a bitfield width, however. */
18409 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18411 cp_decl_specifier_seq type_specifiers;
18413 /* Consume the `:'. */
18414 cp_lexer_consume_token (parser->lexer);
18416 /* Parse the type-specifier-seq. */
18417 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18418 /*is_trailing_return=*/false,
18419 &type_specifiers);
18421 /* At this point this is surely not elaborated type specifier. */
18422 if (!cp_parser_parse_definitely (parser))
18423 return NULL_TREE;
18425 if (cxx_dialect < cxx11)
18426 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18428 has_underlying_type = true;
18430 /* If that didn't work, stop. */
18431 if (type_specifiers.type != error_mark_node)
18433 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18434 /*initialized=*/0, NULL);
18435 if (underlying_type == error_mark_node
18436 || check_for_bare_parameter_packs (underlying_type))
18437 underlying_type = NULL_TREE;
18441 /* Look for the `{' but don't consume it yet. */
18442 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18444 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18446 cp_parser_error (parser, "expected %<{%>");
18447 if (has_underlying_type)
18449 type = NULL_TREE;
18450 goto out;
18453 /* An opaque-enum-specifier must have a ';' here. */
18454 if ((scoped_enum_p || underlying_type)
18455 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18457 cp_parser_error (parser, "expected %<;%> or %<{%>");
18458 if (has_underlying_type)
18460 type = NULL_TREE;
18461 goto out;
18466 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18467 return NULL_TREE;
18469 if (nested_name_specifier)
18471 if (CLASS_TYPE_P (nested_name_specifier))
18473 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18474 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18475 push_scope (nested_name_specifier);
18477 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18479 push_nested_namespace (nested_name_specifier);
18483 /* Issue an error message if type-definitions are forbidden here. */
18484 if (!cp_parser_check_type_definition (parser))
18485 type = error_mark_node;
18486 else
18487 /* Create the new type. We do this before consuming the opening
18488 brace so the enum will be recorded as being on the line of its
18489 tag (or the 'enum' keyword, if there is no tag). */
18490 type = start_enum (identifier, type, underlying_type,
18491 attributes, scoped_enum_p, &is_new_type);
18493 /* If the next token is not '{' it is an opaque-enum-specifier or an
18494 elaborated-type-specifier. */
18495 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18497 timevar_push (TV_PARSE_ENUM);
18498 if (nested_name_specifier
18499 && nested_name_specifier != error_mark_node)
18501 /* The following catches invalid code such as:
18502 enum class S<int>::E { A, B, C }; */
18503 if (!processing_specialization
18504 && CLASS_TYPE_P (nested_name_specifier)
18505 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18506 error_at (type_start_token->location, "cannot add an enumerator "
18507 "list to a template instantiation");
18509 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18511 error_at (type_start_token->location,
18512 "%<%T::%E%> has not been declared",
18513 TYPE_CONTEXT (nested_name_specifier),
18514 nested_name_specifier);
18515 type = error_mark_node;
18517 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18518 && !CLASS_TYPE_P (nested_name_specifier))
18520 error_at (type_start_token->location, "nested name specifier "
18521 "%qT for enum declaration does not name a class "
18522 "or namespace", nested_name_specifier);
18523 type = error_mark_node;
18525 /* If that scope does not contain the scope in which the
18526 class was originally declared, the program is invalid. */
18527 else if (prev_scope && !is_ancestor (prev_scope,
18528 nested_name_specifier))
18530 if (at_namespace_scope_p ())
18531 error_at (type_start_token->location,
18532 "declaration of %qD in namespace %qD which does not "
18533 "enclose %qD",
18534 type, prev_scope, nested_name_specifier);
18535 else
18536 error_at (type_start_token->location,
18537 "declaration of %qD in %qD which does not "
18538 "enclose %qD",
18539 type, prev_scope, nested_name_specifier);
18540 type = error_mark_node;
18542 /* If that scope is the scope where the declaration is being placed
18543 the program is invalid. */
18544 else if (CLASS_TYPE_P (nested_name_specifier)
18545 && CLASS_TYPE_P (prev_scope)
18546 && same_type_p (nested_name_specifier, prev_scope))
18548 permerror (type_start_token->location,
18549 "extra qualification not allowed");
18550 nested_name_specifier = NULL_TREE;
18554 if (scoped_enum_p)
18555 begin_scope (sk_scoped_enum, type);
18557 /* Consume the opening brace. */
18558 matching_braces braces;
18559 braces.consume_open (parser);
18561 if (type == error_mark_node)
18562 ; /* Nothing to add */
18563 else if (OPAQUE_ENUM_P (type)
18564 || (cxx_dialect > cxx98 && processing_specialization))
18566 new_value_list = true;
18567 SET_OPAQUE_ENUM_P (type, false);
18568 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18570 else
18572 error_at (type_start_token->location,
18573 "multiple definition of %q#T", type);
18574 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18575 "previous definition here");
18576 type = error_mark_node;
18579 if (type == error_mark_node)
18580 cp_parser_skip_to_end_of_block_or_statement (parser);
18581 /* If the next token is not '}', then there are some enumerators. */
18582 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18584 if (is_unnamed && !scoped_enum_p)
18585 pedwarn (type_start_token->location, OPT_Wpedantic,
18586 "ISO C++ forbids empty unnamed enum");
18588 else
18589 cp_parser_enumerator_list (parser, type);
18591 /* Consume the final '}'. */
18592 braces.require_close (parser);
18594 if (scoped_enum_p)
18595 finish_scope ();
18596 timevar_pop (TV_PARSE_ENUM);
18598 else
18600 /* If a ';' follows, then it is an opaque-enum-specifier
18601 and additional restrictions apply. */
18602 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18604 if (is_unnamed)
18605 error_at (type_start_token->location,
18606 "opaque-enum-specifier without name");
18607 else if (nested_name_specifier)
18608 error_at (type_start_token->location,
18609 "opaque-enum-specifier must use a simple identifier");
18613 /* Look for trailing attributes to apply to this enumeration, and
18614 apply them if appropriate. */
18615 if (cp_parser_allow_gnu_extensions_p (parser))
18617 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18618 cplus_decl_attributes (&type,
18619 trailing_attr,
18620 (int) ATTR_FLAG_TYPE_IN_PLACE);
18623 /* Finish up the enumeration. */
18624 if (type != error_mark_node)
18626 if (new_value_list)
18627 finish_enum_value_list (type);
18628 if (is_new_type)
18629 finish_enum (type);
18632 if (nested_name_specifier)
18634 if (CLASS_TYPE_P (nested_name_specifier))
18636 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18637 pop_scope (nested_name_specifier);
18639 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18641 pop_nested_namespace (nested_name_specifier);
18644 out:
18645 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18646 return type;
18649 /* Parse an enumerator-list. The enumerators all have the indicated
18650 TYPE.
18652 enumerator-list:
18653 enumerator-definition
18654 enumerator-list , enumerator-definition */
18656 static void
18657 cp_parser_enumerator_list (cp_parser* parser, tree type)
18659 while (true)
18661 /* Parse an enumerator-definition. */
18662 cp_parser_enumerator_definition (parser, type);
18664 /* If the next token is not a ',', we've reached the end of
18665 the list. */
18666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18667 break;
18668 /* Otherwise, consume the `,' and keep going. */
18669 cp_lexer_consume_token (parser->lexer);
18670 /* If the next token is a `}', there is a trailing comma. */
18671 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18673 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18674 pedwarn (input_location, OPT_Wpedantic,
18675 "comma at end of enumerator list");
18676 break;
18681 /* Parse an enumerator-definition. The enumerator has the indicated
18682 TYPE.
18684 enumerator-definition:
18685 enumerator
18686 enumerator = constant-expression
18688 enumerator:
18689 identifier
18691 GNU Extensions:
18693 enumerator-definition:
18694 enumerator attributes [opt]
18695 enumerator attributes [opt] = constant-expression */
18697 static void
18698 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18700 tree identifier;
18701 tree value;
18702 location_t loc;
18704 /* Save the input location because we are interested in the location
18705 of the identifier and not the location of the explicit value. */
18706 loc = cp_lexer_peek_token (parser->lexer)->location;
18708 /* Look for the identifier. */
18709 identifier = cp_parser_identifier (parser);
18710 if (identifier == error_mark_node)
18711 return;
18713 /* Parse any specified attributes. */
18714 tree attrs = cp_parser_attributes_opt (parser);
18716 /* If the next token is an '=', then there is an explicit value. */
18717 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18719 /* Consume the `=' token. */
18720 cp_lexer_consume_token (parser->lexer);
18721 /* Parse the value. */
18722 value = cp_parser_constant_expression (parser);
18724 else
18725 value = NULL_TREE;
18727 /* If we are processing a template, make sure the initializer of the
18728 enumerator doesn't contain any bare template parameter pack. */
18729 if (check_for_bare_parameter_packs (value))
18730 value = error_mark_node;
18732 /* Create the enumerator. */
18733 build_enumerator (identifier, value, type, attrs, loc);
18736 /* Parse a namespace-name.
18738 namespace-name:
18739 original-namespace-name
18740 namespace-alias
18742 Returns the NAMESPACE_DECL for the namespace. */
18744 static tree
18745 cp_parser_namespace_name (cp_parser* parser)
18747 tree identifier;
18748 tree namespace_decl;
18750 cp_token *token = cp_lexer_peek_token (parser->lexer);
18752 /* Get the name of the namespace. */
18753 identifier = cp_parser_identifier (parser);
18754 if (identifier == error_mark_node)
18755 return error_mark_node;
18757 /* Look up the identifier in the currently active scope. Look only
18758 for namespaces, due to:
18760 [basic.lookup.udir]
18762 When looking up a namespace-name in a using-directive or alias
18763 definition, only namespace names are considered.
18765 And:
18767 [basic.lookup.qual]
18769 During the lookup of a name preceding the :: scope resolution
18770 operator, object, function, and enumerator names are ignored.
18772 (Note that cp_parser_qualifying_entity only calls this
18773 function if the token after the name is the scope resolution
18774 operator.) */
18775 namespace_decl = cp_parser_lookup_name (parser, identifier,
18776 none_type,
18777 /*is_template=*/false,
18778 /*is_namespace=*/true,
18779 /*check_dependency=*/true,
18780 /*ambiguous_decls=*/NULL,
18781 token->location);
18782 /* If it's not a namespace, issue an error. */
18783 if (namespace_decl == error_mark_node
18784 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18786 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18788 auto_diagnostic_group d;
18789 name_hint hint;
18790 if (namespace_decl == error_mark_node
18791 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18792 hint = suggest_alternative_in_explicit_scope (token->location,
18793 identifier,
18794 parser->scope);
18795 if (const char *suggestion = hint.suggestion ())
18797 gcc_rich_location richloc (token->location);
18798 richloc.add_fixit_replace (suggestion);
18799 error_at (&richloc,
18800 "%qD is not a namespace-name; did you mean %qs?",
18801 identifier, suggestion);
18803 else
18804 error_at (token->location, "%qD is not a namespace-name",
18805 identifier);
18807 else
18808 cp_parser_error (parser, "expected namespace-name");
18809 namespace_decl = error_mark_node;
18812 return namespace_decl;
18815 /* Parse a namespace-definition.
18817 namespace-definition:
18818 named-namespace-definition
18819 unnamed-namespace-definition
18821 named-namespace-definition:
18822 original-namespace-definition
18823 extension-namespace-definition
18825 original-namespace-definition:
18826 namespace identifier { namespace-body }
18828 extension-namespace-definition:
18829 namespace original-namespace-name { namespace-body }
18831 unnamed-namespace-definition:
18832 namespace { namespace-body } */
18834 static void
18835 cp_parser_namespace_definition (cp_parser* parser)
18837 tree identifier;
18838 int nested_definition_count = 0;
18840 cp_ensure_no_omp_declare_simd (parser);
18841 cp_ensure_no_oacc_routine (parser);
18843 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18845 if (is_inline)
18847 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18848 cp_lexer_consume_token (parser->lexer);
18851 /* Look for the `namespace' keyword. */
18852 cp_token* token
18853 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18855 /* Parse any specified attributes before the identifier. */
18856 tree attribs = cp_parser_attributes_opt (parser);
18858 for (;;)
18860 identifier = NULL_TREE;
18862 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18864 identifier = cp_parser_identifier (parser);
18866 if (cp_next_tokens_can_be_std_attribute_p (parser))
18867 pedwarn (input_location, OPT_Wpedantic,
18868 "standard attributes on namespaces must precede "
18869 "the namespace name");
18871 /* Parse any attributes specified after the identifier. */
18872 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18875 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18876 break;
18878 if (!nested_definition_count && cxx_dialect < cxx17)
18879 pedwarn (input_location, OPT_Wpedantic,
18880 "nested namespace definitions only available with "
18881 "-std=c++17 or -std=gnu++17");
18883 /* Nested namespace names can create new namespaces (unlike
18884 other qualified-ids). */
18885 if (int count = identifier ? push_namespace (identifier) : 0)
18886 nested_definition_count += count;
18887 else
18888 cp_parser_error (parser, "nested namespace name required");
18889 cp_lexer_consume_token (parser->lexer);
18892 if (nested_definition_count && !identifier)
18893 cp_parser_error (parser, "namespace name required");
18895 if (nested_definition_count && attribs)
18896 error_at (token->location,
18897 "a nested namespace definition cannot have attributes");
18898 if (nested_definition_count && is_inline)
18899 error_at (token->location,
18900 "a nested namespace definition cannot be inline");
18902 /* Start the namespace. */
18903 nested_definition_count += push_namespace (identifier, is_inline);
18905 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18907 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18909 /* Look for the `{' to validate starting the namespace. */
18910 matching_braces braces;
18911 if (braces.require_open (parser))
18913 /* Parse the body of the namespace. */
18914 cp_parser_namespace_body (parser);
18916 /* Look for the final `}'. */
18917 braces.require_close (parser);
18920 if (has_visibility)
18921 pop_visibility (1);
18923 /* Pop the nested namespace definitions. */
18924 while (nested_definition_count--)
18925 pop_namespace ();
18928 /* Parse a namespace-body.
18930 namespace-body:
18931 declaration-seq [opt] */
18933 static void
18934 cp_parser_namespace_body (cp_parser* parser)
18936 cp_parser_declaration_seq_opt (parser);
18939 /* Parse a namespace-alias-definition.
18941 namespace-alias-definition:
18942 namespace identifier = qualified-namespace-specifier ; */
18944 static void
18945 cp_parser_namespace_alias_definition (cp_parser* parser)
18947 tree identifier;
18948 tree namespace_specifier;
18950 cp_token *token = cp_lexer_peek_token (parser->lexer);
18952 /* Look for the `namespace' keyword. */
18953 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18954 /* Look for the identifier. */
18955 identifier = cp_parser_identifier (parser);
18956 if (identifier == error_mark_node)
18957 return;
18958 /* Look for the `=' token. */
18959 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18960 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18962 error_at (token->location, "%<namespace%> definition is not allowed here");
18963 /* Skip the definition. */
18964 cp_lexer_consume_token (parser->lexer);
18965 if (cp_parser_skip_to_closing_brace (parser))
18966 cp_lexer_consume_token (parser->lexer);
18967 return;
18969 cp_parser_require (parser, CPP_EQ, RT_EQ);
18970 /* Look for the qualified-namespace-specifier. */
18971 namespace_specifier
18972 = cp_parser_qualified_namespace_specifier (parser);
18973 /* Look for the `;' token. */
18974 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18976 /* Register the alias in the symbol table. */
18977 do_namespace_alias (identifier, namespace_specifier);
18980 /* Parse a qualified-namespace-specifier.
18982 qualified-namespace-specifier:
18983 :: [opt] nested-name-specifier [opt] namespace-name
18985 Returns a NAMESPACE_DECL corresponding to the specified
18986 namespace. */
18988 static tree
18989 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18991 /* Look for the optional `::'. */
18992 cp_parser_global_scope_opt (parser,
18993 /*current_scope_valid_p=*/false);
18995 /* Look for the optional nested-name-specifier. */
18996 cp_parser_nested_name_specifier_opt (parser,
18997 /*typename_keyword_p=*/false,
18998 /*check_dependency_p=*/true,
18999 /*type_p=*/false,
19000 /*is_declaration=*/true);
19002 return cp_parser_namespace_name (parser);
19005 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19006 access declaration.
19008 using-declaration:
19009 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19010 using :: unqualified-id ;
19012 access-declaration:
19013 qualified-id ;
19017 static bool
19018 cp_parser_using_declaration (cp_parser* parser,
19019 bool access_declaration_p)
19021 cp_token *token;
19022 bool typename_p = false;
19023 bool global_scope_p;
19024 tree decl;
19025 tree identifier;
19026 tree qscope;
19027 int oldcount = errorcount;
19028 cp_token *diag_token = NULL;
19030 if (access_declaration_p)
19032 diag_token = cp_lexer_peek_token (parser->lexer);
19033 cp_parser_parse_tentatively (parser);
19035 else
19037 /* Look for the `using' keyword. */
19038 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19040 again:
19041 /* Peek at the next token. */
19042 token = cp_lexer_peek_token (parser->lexer);
19043 /* See if it's `typename'. */
19044 if (token->keyword == RID_TYPENAME)
19046 /* Remember that we've seen it. */
19047 typename_p = true;
19048 /* Consume the `typename' token. */
19049 cp_lexer_consume_token (parser->lexer);
19053 /* Look for the optional global scope qualification. */
19054 global_scope_p
19055 = (cp_parser_global_scope_opt (parser,
19056 /*current_scope_valid_p=*/false)
19057 != NULL_TREE);
19059 /* If we saw `typename', or didn't see `::', then there must be a
19060 nested-name-specifier present. */
19061 if (typename_p || !global_scope_p)
19063 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19064 /*check_dependency_p=*/true,
19065 /*type_p=*/false,
19066 /*is_declaration=*/true);
19067 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19069 cp_parser_skip_to_end_of_block_or_statement (parser);
19070 return false;
19073 /* Otherwise, we could be in either of the two productions. In that
19074 case, treat the nested-name-specifier as optional. */
19075 else
19076 qscope = cp_parser_nested_name_specifier_opt (parser,
19077 /*typename_keyword_p=*/false,
19078 /*check_dependency_p=*/true,
19079 /*type_p=*/false,
19080 /*is_declaration=*/true);
19081 if (!qscope)
19082 qscope = global_namespace;
19083 else if (UNSCOPED_ENUM_P (qscope))
19084 qscope = CP_TYPE_CONTEXT (qscope);
19086 if (access_declaration_p && cp_parser_error_occurred (parser))
19087 /* Something has already gone wrong; there's no need to parse
19088 further. Since an error has occurred, the return value of
19089 cp_parser_parse_definitely will be false, as required. */
19090 return cp_parser_parse_definitely (parser);
19092 token = cp_lexer_peek_token (parser->lexer);
19093 /* Parse the unqualified-id. */
19094 identifier = cp_parser_unqualified_id (parser,
19095 /*template_keyword_p=*/false,
19096 /*check_dependency_p=*/true,
19097 /*declarator_p=*/true,
19098 /*optional_p=*/false);
19100 if (access_declaration_p)
19102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19103 cp_parser_simulate_error (parser);
19104 if (!cp_parser_parse_definitely (parser))
19105 return false;
19107 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19109 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19110 if (cxx_dialect < cxx17
19111 && !in_system_header_at (ell->location))
19112 pedwarn (ell->location, 0,
19113 "pack expansion in using-declaration only available "
19114 "with -std=c++17 or -std=gnu++17");
19115 qscope = make_pack_expansion (qscope);
19118 /* The function we call to handle a using-declaration is different
19119 depending on what scope we are in. */
19120 if (qscope == error_mark_node || identifier == error_mark_node)
19122 else if (!identifier_p (identifier)
19123 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19124 /* [namespace.udecl]
19126 A using declaration shall not name a template-id. */
19127 error_at (token->location,
19128 "a template-id may not appear in a using-declaration");
19129 else
19131 if (at_class_scope_p ())
19133 /* Create the USING_DECL. */
19134 decl = do_class_using_decl (qscope, identifier);
19136 if (decl && typename_p)
19137 USING_DECL_TYPENAME_P (decl) = 1;
19139 if (check_for_bare_parameter_packs (decl))
19141 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19142 return false;
19144 else
19145 /* Add it to the list of members in this class. */
19146 finish_member_declaration (decl);
19148 else
19150 decl = cp_parser_lookup_name_simple (parser,
19151 identifier,
19152 token->location);
19153 if (decl == error_mark_node)
19154 cp_parser_name_lookup_error (parser, identifier,
19155 decl, NLE_NULL,
19156 token->location);
19157 else if (check_for_bare_parameter_packs (decl))
19159 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19160 return false;
19162 else if (!at_namespace_scope_p ())
19163 finish_local_using_decl (decl, qscope, identifier);
19164 else
19165 finish_namespace_using_decl (decl, qscope, identifier);
19169 if (!access_declaration_p
19170 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19172 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19173 if (cxx_dialect < cxx17)
19174 pedwarn (comma->location, 0,
19175 "comma-separated list in using-declaration only available "
19176 "with -std=c++17 or -std=gnu++17");
19177 goto again;
19180 /* Look for the final `;'. */
19181 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19183 if (access_declaration_p && errorcount == oldcount)
19184 warning_at (diag_token->location, OPT_Wdeprecated,
19185 "access declarations are deprecated "
19186 "in favour of using-declarations; "
19187 "suggestion: add the %<using%> keyword");
19189 return true;
19192 /* Parse an alias-declaration.
19194 alias-declaration:
19195 using identifier attribute-specifier-seq [opt] = type-id */
19197 static tree
19198 cp_parser_alias_declaration (cp_parser* parser)
19200 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19201 location_t id_location, type_location;
19202 cp_declarator *declarator;
19203 cp_decl_specifier_seq decl_specs;
19204 bool member_p;
19205 const char *saved_message = NULL;
19207 /* Look for the `using' keyword. */
19208 cp_token *using_token
19209 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19210 if (using_token == NULL)
19211 return error_mark_node;
19213 id_location = cp_lexer_peek_token (parser->lexer)->location;
19214 id = cp_parser_identifier (parser);
19215 if (id == error_mark_node)
19216 return error_mark_node;
19218 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19219 attributes = cp_parser_attributes_opt (parser);
19220 if (attributes == error_mark_node)
19221 return error_mark_node;
19223 cp_parser_require (parser, CPP_EQ, RT_EQ);
19225 if (cp_parser_error_occurred (parser))
19226 return error_mark_node;
19228 cp_parser_commit_to_tentative_parse (parser);
19230 /* Now we are going to parse the type-id of the declaration. */
19233 [dcl.type]/3 says:
19235 "A type-specifier-seq shall not define a class or enumeration
19236 unless it appears in the type-id of an alias-declaration (7.1.3) that
19237 is not the declaration of a template-declaration."
19239 In other words, if we currently are in an alias template, the
19240 type-id should not define a type.
19242 So let's set parser->type_definition_forbidden_message in that
19243 case; cp_parser_check_type_definition (called by
19244 cp_parser_class_specifier) will then emit an error if a type is
19245 defined in the type-id. */
19246 if (parser->num_template_parameter_lists)
19248 saved_message = parser->type_definition_forbidden_message;
19249 parser->type_definition_forbidden_message =
19250 G_("types may not be defined in alias template declarations");
19253 type = cp_parser_type_id (parser, &type_location);
19255 /* Restore the error message if need be. */
19256 if (parser->num_template_parameter_lists)
19257 parser->type_definition_forbidden_message = saved_message;
19259 if (type == error_mark_node
19260 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19262 cp_parser_skip_to_end_of_block_or_statement (parser);
19263 return error_mark_node;
19266 /* A typedef-name can also be introduced by an alias-declaration. The
19267 identifier following the using keyword becomes a typedef-name. It has
19268 the same semantics as if it were introduced by the typedef
19269 specifier. In particular, it does not define a new type and it shall
19270 not appear in the type-id. */
19272 clear_decl_specs (&decl_specs);
19273 decl_specs.type = type;
19274 if (attributes != NULL_TREE)
19276 decl_specs.attributes = attributes;
19277 set_and_check_decl_spec_loc (&decl_specs,
19278 ds_attribute,
19279 attrs_token);
19281 set_and_check_decl_spec_loc (&decl_specs,
19282 ds_typedef,
19283 using_token);
19284 set_and_check_decl_spec_loc (&decl_specs,
19285 ds_alias,
19286 using_token);
19287 decl_specs.locations[ds_type_spec] = type_location;
19289 if (parser->num_template_parameter_lists
19290 && !cp_parser_check_template_parameters (parser,
19291 /*num_templates=*/0,
19292 /*template_id*/false,
19293 id_location,
19294 /*declarator=*/NULL))
19295 return error_mark_node;
19297 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19298 declarator->id_loc = id_location;
19300 member_p = at_class_scope_p ();
19301 if (member_p)
19302 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19303 NULL_TREE, attributes);
19304 else
19305 decl = start_decl (declarator, &decl_specs, 0,
19306 attributes, NULL_TREE, &pushed_scope);
19307 if (decl == error_mark_node)
19308 return decl;
19310 // Attach constraints to the alias declaration.
19311 if (flag_concepts && current_template_parms)
19313 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19314 tree constr = build_constraints (reqs, NULL_TREE);
19315 set_constraints (decl, constr);
19318 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19320 if (pushed_scope)
19321 pop_scope (pushed_scope);
19323 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19324 added into the symbol table; otherwise, return the TYPE_DECL. */
19325 if (DECL_LANG_SPECIFIC (decl)
19326 && DECL_TEMPLATE_INFO (decl)
19327 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19329 decl = DECL_TI_TEMPLATE (decl);
19330 if (member_p)
19331 check_member_template (decl);
19334 return decl;
19337 /* Parse a using-directive.
19339 using-directive:
19340 using namespace :: [opt] nested-name-specifier [opt]
19341 namespace-name ; */
19343 static void
19344 cp_parser_using_directive (cp_parser* parser)
19346 tree namespace_decl;
19347 tree attribs;
19349 /* Look for the `using' keyword. */
19350 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19351 /* And the `namespace' keyword. */
19352 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19353 /* Look for the optional `::' operator. */
19354 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19355 /* And the optional nested-name-specifier. */
19356 cp_parser_nested_name_specifier_opt (parser,
19357 /*typename_keyword_p=*/false,
19358 /*check_dependency_p=*/true,
19359 /*type_p=*/false,
19360 /*is_declaration=*/true);
19361 /* Get the namespace being used. */
19362 namespace_decl = cp_parser_namespace_name (parser);
19363 /* And any specified attributes. */
19364 attribs = cp_parser_attributes_opt (parser);
19366 /* Update the symbol table. */
19367 if (namespace_bindings_p ())
19368 finish_namespace_using_directive (namespace_decl, attribs);
19369 else
19370 finish_local_using_directive (namespace_decl, attribs);
19372 /* Look for the final `;'. */
19373 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19376 /* Parse an asm-definition.
19378 asm-definition:
19379 asm ( string-literal ) ;
19381 GNU Extension:
19383 asm-definition:
19384 asm volatile [opt] ( string-literal ) ;
19385 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19386 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19387 : asm-operand-list [opt] ) ;
19388 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19389 : asm-operand-list [opt]
19390 : asm-clobber-list [opt] ) ;
19391 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19392 : asm-clobber-list [opt]
19393 : asm-goto-list ) ; */
19395 static void
19396 cp_parser_asm_definition (cp_parser* parser)
19398 tree string;
19399 tree outputs = NULL_TREE;
19400 tree inputs = NULL_TREE;
19401 tree clobbers = NULL_TREE;
19402 tree labels = NULL_TREE;
19403 tree asm_stmt;
19404 bool volatile_p = false;
19405 bool extended_p = false;
19406 bool invalid_inputs_p = false;
19407 bool invalid_outputs_p = false;
19408 bool goto_p = false;
19409 required_token missing = RT_NONE;
19411 /* Look for the `asm' keyword. */
19412 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19414 if (parser->in_function_body
19415 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19417 error ("%<asm%> in %<constexpr%> function");
19418 cp_function_chain->invalid_constexpr = true;
19421 /* See if the next token is `volatile'. */
19422 if (cp_parser_allow_gnu_extensions_p (parser)
19423 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19425 /* Remember that we saw the `volatile' keyword. */
19426 volatile_p = true;
19427 /* Consume the token. */
19428 cp_lexer_consume_token (parser->lexer);
19430 if (cp_parser_allow_gnu_extensions_p (parser)
19431 && parser->in_function_body
19432 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19434 /* Remember that we saw the `goto' keyword. */
19435 goto_p = true;
19436 /* Consume the token. */
19437 cp_lexer_consume_token (parser->lexer);
19439 /* Look for the opening `('. */
19440 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19441 return;
19442 /* Look for the string. */
19443 string = cp_parser_string_literal (parser, false, false);
19444 if (string == error_mark_node)
19446 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19447 /*consume_paren=*/true);
19448 return;
19451 /* If we're allowing GNU extensions, check for the extended assembly
19452 syntax. Unfortunately, the `:' tokens need not be separated by
19453 a space in C, and so, for compatibility, we tolerate that here
19454 too. Doing that means that we have to treat the `::' operator as
19455 two `:' tokens. */
19456 if (cp_parser_allow_gnu_extensions_p (parser)
19457 && parser->in_function_body
19458 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19459 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19461 bool inputs_p = false;
19462 bool clobbers_p = false;
19463 bool labels_p = false;
19465 /* The extended syntax was used. */
19466 extended_p = true;
19468 /* Look for outputs. */
19469 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19471 /* Consume the `:'. */
19472 cp_lexer_consume_token (parser->lexer);
19473 /* Parse the output-operands. */
19474 if (cp_lexer_next_token_is_not (parser->lexer,
19475 CPP_COLON)
19476 && cp_lexer_next_token_is_not (parser->lexer,
19477 CPP_SCOPE)
19478 && cp_lexer_next_token_is_not (parser->lexer,
19479 CPP_CLOSE_PAREN)
19480 && !goto_p)
19482 outputs = cp_parser_asm_operand_list (parser);
19483 if (outputs == error_mark_node)
19484 invalid_outputs_p = true;
19487 /* If the next token is `::', there are no outputs, and the
19488 next token is the beginning of the inputs. */
19489 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19490 /* The inputs are coming next. */
19491 inputs_p = true;
19493 /* Look for inputs. */
19494 if (inputs_p
19495 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19497 /* Consume the `:' or `::'. */
19498 cp_lexer_consume_token (parser->lexer);
19499 /* Parse the output-operands. */
19500 if (cp_lexer_next_token_is_not (parser->lexer,
19501 CPP_COLON)
19502 && cp_lexer_next_token_is_not (parser->lexer,
19503 CPP_SCOPE)
19504 && cp_lexer_next_token_is_not (parser->lexer,
19505 CPP_CLOSE_PAREN))
19507 inputs = cp_parser_asm_operand_list (parser);
19508 if (inputs == error_mark_node)
19509 invalid_inputs_p = true;
19512 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19513 /* The clobbers are coming next. */
19514 clobbers_p = true;
19516 /* Look for clobbers. */
19517 if (clobbers_p
19518 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19520 clobbers_p = true;
19521 /* Consume the `:' or `::'. */
19522 cp_lexer_consume_token (parser->lexer);
19523 /* Parse the clobbers. */
19524 if (cp_lexer_next_token_is_not (parser->lexer,
19525 CPP_COLON)
19526 && cp_lexer_next_token_is_not (parser->lexer,
19527 CPP_CLOSE_PAREN))
19528 clobbers = cp_parser_asm_clobber_list (parser);
19530 else if (goto_p
19531 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19532 /* The labels are coming next. */
19533 labels_p = true;
19535 /* Look for labels. */
19536 if (labels_p
19537 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19539 labels_p = true;
19540 /* Consume the `:' or `::'. */
19541 cp_lexer_consume_token (parser->lexer);
19542 /* Parse the labels. */
19543 labels = cp_parser_asm_label_list (parser);
19546 if (goto_p && !labels_p)
19547 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19549 else if (goto_p)
19550 missing = RT_COLON_SCOPE;
19552 /* Look for the closing `)'. */
19553 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19554 missing ? missing : RT_CLOSE_PAREN))
19555 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19556 /*consume_paren=*/true);
19557 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19559 if (!invalid_inputs_p && !invalid_outputs_p)
19561 /* Create the ASM_EXPR. */
19562 if (parser->in_function_body)
19564 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19565 inputs, clobbers, labels);
19566 /* If the extended syntax was not used, mark the ASM_EXPR. */
19567 if (!extended_p)
19569 tree temp = asm_stmt;
19570 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19571 temp = TREE_OPERAND (temp, 0);
19573 ASM_INPUT_P (temp) = 1;
19576 else
19577 symtab->finalize_toplevel_asm (string);
19581 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19582 type that comes from the decl-specifier-seq. */
19584 static tree
19585 strip_declarator_types (tree type, cp_declarator *declarator)
19587 for (cp_declarator *d = declarator; d;)
19588 switch (d->kind)
19590 case cdk_id:
19591 case cdk_decomp:
19592 case cdk_error:
19593 d = NULL;
19594 break;
19596 default:
19597 if (TYPE_PTRMEMFUNC_P (type))
19598 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19599 type = TREE_TYPE (type);
19600 d = d->declarator;
19601 break;
19604 return type;
19607 /* Declarators [gram.dcl.decl] */
19609 /* Parse an init-declarator.
19611 init-declarator:
19612 declarator initializer [opt]
19614 GNU Extension:
19616 init-declarator:
19617 declarator asm-specification [opt] attributes [opt] initializer [opt]
19619 function-definition:
19620 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19621 function-body
19622 decl-specifier-seq [opt] declarator function-try-block
19624 GNU Extension:
19626 function-definition:
19627 __extension__ function-definition
19629 TM Extension:
19631 function-definition:
19632 decl-specifier-seq [opt] declarator function-transaction-block
19634 The DECL_SPECIFIERS apply to this declarator. Returns a
19635 representation of the entity declared. If MEMBER_P is TRUE, then
19636 this declarator appears in a class scope. The new DECL created by
19637 this declarator is returned.
19639 The CHECKS are access checks that should be performed once we know
19640 what entity is being declared (and, therefore, what classes have
19641 befriended it).
19643 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19644 for a function-definition here as well. If the declarator is a
19645 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19646 be TRUE upon return. By that point, the function-definition will
19647 have been completely parsed.
19649 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19650 is FALSE.
19652 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19653 parsed declaration if it is an uninitialized single declarator not followed
19654 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19655 if present, will not be consumed. If returned, this declarator will be
19656 created with SD_INITIALIZED but will not call cp_finish_decl.
19658 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19659 and there is an initializer, the pointed location_t is set to the
19660 location of the '=' or `(', or '{' in C++11 token introducing the
19661 initializer. */
19663 static tree
19664 cp_parser_init_declarator (cp_parser* parser,
19665 cp_decl_specifier_seq *decl_specifiers,
19666 vec<deferred_access_check, va_gc> *checks,
19667 bool function_definition_allowed_p,
19668 bool member_p,
19669 int declares_class_or_enum,
19670 bool* function_definition_p,
19671 tree* maybe_range_for_decl,
19672 location_t* init_loc,
19673 tree* auto_result)
19675 cp_token *token = NULL, *asm_spec_start_token = NULL,
19676 *attributes_start_token = NULL;
19677 cp_declarator *declarator;
19678 tree prefix_attributes;
19679 tree attributes = NULL;
19680 tree asm_specification;
19681 tree initializer;
19682 tree decl = NULL_TREE;
19683 tree scope;
19684 int is_initialized;
19685 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19686 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19687 "(...)". */
19688 enum cpp_ttype initialization_kind;
19689 bool is_direct_init = false;
19690 bool is_non_constant_init;
19691 int ctor_dtor_or_conv_p;
19692 bool friend_p = cp_parser_friend_p (decl_specifiers);
19693 tree pushed_scope = NULL_TREE;
19694 bool range_for_decl_p = false;
19695 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19696 location_t tmp_init_loc = UNKNOWN_LOCATION;
19698 /* Gather the attributes that were provided with the
19699 decl-specifiers. */
19700 prefix_attributes = decl_specifiers->attributes;
19702 /* Assume that this is not the declarator for a function
19703 definition. */
19704 if (function_definition_p)
19705 *function_definition_p = false;
19707 /* Default arguments are only permitted for function parameters. */
19708 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19709 parser->default_arg_ok_p = false;
19711 /* Defer access checks while parsing the declarator; we cannot know
19712 what names are accessible until we know what is being
19713 declared. */
19714 resume_deferring_access_checks ();
19716 token = cp_lexer_peek_token (parser->lexer);
19718 /* Parse the declarator. */
19719 declarator
19720 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19721 &ctor_dtor_or_conv_p,
19722 /*parenthesized_p=*/NULL,
19723 member_p, friend_p);
19724 /* Gather up the deferred checks. */
19725 stop_deferring_access_checks ();
19727 parser->default_arg_ok_p = saved_default_arg_ok_p;
19729 /* If the DECLARATOR was erroneous, there's no need to go
19730 further. */
19731 if (declarator == cp_error_declarator)
19732 return error_mark_node;
19734 /* Check that the number of template-parameter-lists is OK. */
19735 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19736 token->location))
19737 return error_mark_node;
19739 if (declares_class_or_enum & 2)
19740 cp_parser_check_for_definition_in_return_type (declarator,
19741 decl_specifiers->type,
19742 decl_specifiers->locations[ds_type_spec]);
19744 /* Figure out what scope the entity declared by the DECLARATOR is
19745 located in. `grokdeclarator' sometimes changes the scope, so
19746 we compute it now. */
19747 scope = get_scope_of_declarator (declarator);
19749 /* Perform any lookups in the declared type which were thought to be
19750 dependent, but are not in the scope of the declarator. */
19751 decl_specifiers->type
19752 = maybe_update_decl_type (decl_specifiers->type, scope);
19754 /* If we're allowing GNU extensions, look for an
19755 asm-specification. */
19756 if (cp_parser_allow_gnu_extensions_p (parser))
19758 /* Look for an asm-specification. */
19759 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19760 asm_specification = cp_parser_asm_specification_opt (parser);
19762 else
19763 asm_specification = NULL_TREE;
19765 /* Look for attributes. */
19766 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19767 attributes = cp_parser_attributes_opt (parser);
19769 /* Peek at the next token. */
19770 token = cp_lexer_peek_token (parser->lexer);
19772 bool bogus_implicit_tmpl = false;
19774 if (function_declarator_p (declarator))
19776 /* Handle C++17 deduction guides. */
19777 if (!decl_specifiers->type
19778 && ctor_dtor_or_conv_p <= 0
19779 && cxx_dialect >= cxx17)
19781 cp_declarator *id = get_id_declarator (declarator);
19782 tree name = id->u.id.unqualified_name;
19783 parser->scope = id->u.id.qualifying_scope;
19784 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19785 if (tmpl
19786 && (DECL_CLASS_TEMPLATE_P (tmpl)
19787 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19789 id->u.id.unqualified_name = dguide_name (tmpl);
19790 id->u.id.sfk = sfk_deduction_guide;
19791 ctor_dtor_or_conv_p = 1;
19795 /* Check to see if the token indicates the start of a
19796 function-definition. */
19797 if (cp_parser_token_starts_function_definition_p (token))
19799 if (!function_definition_allowed_p)
19801 /* If a function-definition should not appear here, issue an
19802 error message. */
19803 cp_parser_error (parser,
19804 "a function-definition is not allowed here");
19805 return error_mark_node;
19808 location_t func_brace_location
19809 = cp_lexer_peek_token (parser->lexer)->location;
19811 /* Neither attributes nor an asm-specification are allowed
19812 on a function-definition. */
19813 if (asm_specification)
19814 error_at (asm_spec_start_token->location,
19815 "an asm-specification is not allowed "
19816 "on a function-definition");
19817 if (attributes)
19818 error_at (attributes_start_token->location,
19819 "attributes are not allowed "
19820 "on a function-definition");
19821 /* This is a function-definition. */
19822 *function_definition_p = true;
19824 /* Parse the function definition. */
19825 if (member_p)
19826 decl = cp_parser_save_member_function_body (parser,
19827 decl_specifiers,
19828 declarator,
19829 prefix_attributes);
19830 else
19831 decl =
19832 (cp_parser_function_definition_from_specifiers_and_declarator
19833 (parser, decl_specifiers, prefix_attributes, declarator));
19835 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19837 /* This is where the prologue starts... */
19838 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19839 = func_brace_location;
19842 return decl;
19845 else if (parser->fully_implicit_function_template_p)
19847 /* A non-template declaration involving a function parameter list
19848 containing an implicit template parameter will be made into a
19849 template. If the resulting declaration is not going to be an
19850 actual function then finish the template scope here to prevent it.
19851 An error message will be issued once we have a decl to talk about.
19853 FIXME probably we should do type deduction rather than create an
19854 implicit template, but the standard currently doesn't allow it. */
19855 bogus_implicit_tmpl = true;
19856 finish_fully_implicit_template (parser, NULL_TREE);
19859 /* [dcl.dcl]
19861 Only in function declarations for constructors, destructors, type
19862 conversions, and deduction guides can the decl-specifier-seq be omitted.
19864 We explicitly postpone this check past the point where we handle
19865 function-definitions because we tolerate function-definitions
19866 that are missing their return types in some modes. */
19867 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19869 cp_parser_error (parser,
19870 "expected constructor, destructor, or type conversion");
19871 return error_mark_node;
19874 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19875 if (token->type == CPP_EQ
19876 || token->type == CPP_OPEN_PAREN
19877 || token->type == CPP_OPEN_BRACE)
19879 is_initialized = SD_INITIALIZED;
19880 initialization_kind = token->type;
19881 if (maybe_range_for_decl)
19882 *maybe_range_for_decl = error_mark_node;
19883 tmp_init_loc = token->location;
19884 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19885 *init_loc = tmp_init_loc;
19887 if (token->type == CPP_EQ
19888 && function_declarator_p (declarator))
19890 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19891 if (t2->keyword == RID_DEFAULT)
19892 is_initialized = SD_DEFAULTED;
19893 else if (t2->keyword == RID_DELETE)
19894 is_initialized = SD_DELETED;
19897 else
19899 /* If the init-declarator isn't initialized and isn't followed by a
19900 `,' or `;', it's not a valid init-declarator. */
19901 if (token->type != CPP_COMMA
19902 && token->type != CPP_SEMICOLON)
19904 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19905 range_for_decl_p = true;
19906 else
19908 if (!maybe_range_for_decl)
19909 cp_parser_error (parser, "expected initializer");
19910 return error_mark_node;
19913 is_initialized = SD_UNINITIALIZED;
19914 initialization_kind = CPP_EOF;
19917 /* Because start_decl has side-effects, we should only call it if we
19918 know we're going ahead. By this point, we know that we cannot
19919 possibly be looking at any other construct. */
19920 cp_parser_commit_to_tentative_parse (parser);
19922 /* Enter the newly declared entry in the symbol table. If we're
19923 processing a declaration in a class-specifier, we wait until
19924 after processing the initializer. */
19925 if (!member_p)
19927 if (parser->in_unbraced_linkage_specification_p)
19928 decl_specifiers->storage_class = sc_extern;
19929 decl = start_decl (declarator, decl_specifiers,
19930 range_for_decl_p? SD_INITIALIZED : is_initialized,
19931 attributes, prefix_attributes, &pushed_scope);
19932 cp_finalize_omp_declare_simd (parser, decl);
19933 cp_finalize_oacc_routine (parser, decl, false);
19934 /* Adjust location of decl if declarator->id_loc is more appropriate:
19935 set, and decl wasn't merged with another decl, in which case its
19936 location would be different from input_location, and more accurate. */
19937 if (DECL_P (decl)
19938 && declarator->id_loc != UNKNOWN_LOCATION
19939 && DECL_SOURCE_LOCATION (decl) == input_location)
19940 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19942 else if (scope)
19943 /* Enter the SCOPE. That way unqualified names appearing in the
19944 initializer will be looked up in SCOPE. */
19945 pushed_scope = push_scope (scope);
19947 /* Perform deferred access control checks, now that we know in which
19948 SCOPE the declared entity resides. */
19949 if (!member_p && decl)
19951 tree saved_current_function_decl = NULL_TREE;
19953 /* If the entity being declared is a function, pretend that we
19954 are in its scope. If it is a `friend', it may have access to
19955 things that would not otherwise be accessible. */
19956 if (TREE_CODE (decl) == FUNCTION_DECL)
19958 saved_current_function_decl = current_function_decl;
19959 current_function_decl = decl;
19962 /* Perform access checks for template parameters. */
19963 cp_parser_perform_template_parameter_access_checks (checks);
19965 /* Perform the access control checks for the declarator and the
19966 decl-specifiers. */
19967 perform_deferred_access_checks (tf_warning_or_error);
19969 /* Restore the saved value. */
19970 if (TREE_CODE (decl) == FUNCTION_DECL)
19971 current_function_decl = saved_current_function_decl;
19974 /* Parse the initializer. */
19975 initializer = NULL_TREE;
19976 is_direct_init = false;
19977 is_non_constant_init = true;
19978 if (is_initialized)
19980 if (function_declarator_p (declarator))
19982 if (initialization_kind == CPP_EQ)
19983 initializer = cp_parser_pure_specifier (parser);
19984 else
19986 /* If the declaration was erroneous, we don't really
19987 know what the user intended, so just silently
19988 consume the initializer. */
19989 if (decl != error_mark_node)
19990 error_at (tmp_init_loc, "initializer provided for function");
19991 cp_parser_skip_to_closing_parenthesis (parser,
19992 /*recovering=*/true,
19993 /*or_comma=*/false,
19994 /*consume_paren=*/true);
19997 else
19999 /* We want to record the extra mangling scope for in-class
20000 initializers of class members and initializers of static data
20001 member templates. The former involves deferring
20002 parsing of the initializer until end of class as with default
20003 arguments. So right here we only handle the latter. */
20004 if (!member_p && processing_template_decl && decl != error_mark_node)
20005 start_lambda_scope (decl);
20006 initializer = cp_parser_initializer (parser,
20007 &is_direct_init,
20008 &is_non_constant_init);
20009 if (!member_p && processing_template_decl && decl != error_mark_node)
20010 finish_lambda_scope ();
20011 if (initializer == error_mark_node)
20012 cp_parser_skip_to_end_of_statement (parser);
20016 /* The old parser allows attributes to appear after a parenthesized
20017 initializer. Mark Mitchell proposed removing this functionality
20018 on the GCC mailing lists on 2002-08-13. This parser accepts the
20019 attributes -- but ignores them. Made a permerror in GCC 8. */
20020 if (cp_parser_allow_gnu_extensions_p (parser)
20021 && initialization_kind == CPP_OPEN_PAREN
20022 && cp_parser_attributes_opt (parser)
20023 && permerror (input_location,
20024 "attributes after parenthesized initializer ignored"))
20026 static bool hint;
20027 if (flag_permissive && !hint)
20029 hint = true;
20030 inform (input_location,
20031 "this flexibility is deprecated and will be removed");
20035 /* And now complain about a non-function implicit template. */
20036 if (bogus_implicit_tmpl && decl != error_mark_node)
20037 error_at (DECL_SOURCE_LOCATION (decl),
20038 "non-function %qD declared as implicit template", decl);
20040 /* For an in-class declaration, use `grokfield' to create the
20041 declaration. */
20042 if (member_p)
20044 if (pushed_scope)
20046 pop_scope (pushed_scope);
20047 pushed_scope = NULL_TREE;
20049 decl = grokfield (declarator, decl_specifiers,
20050 initializer, !is_non_constant_init,
20051 /*asmspec=*/NULL_TREE,
20052 attr_chainon (attributes, prefix_attributes));
20053 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20054 cp_parser_save_default_args (parser, decl);
20055 cp_finalize_omp_declare_simd (parser, decl);
20056 cp_finalize_oacc_routine (parser, decl, false);
20059 /* Finish processing the declaration. But, skip member
20060 declarations. */
20061 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20063 cp_finish_decl (decl,
20064 initializer, !is_non_constant_init,
20065 asm_specification,
20066 /* If the initializer is in parentheses, then this is
20067 a direct-initialization, which means that an
20068 `explicit' constructor is OK. Otherwise, an
20069 `explicit' constructor cannot be used. */
20070 ((is_direct_init || !is_initialized)
20071 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20073 else if ((cxx_dialect != cxx98) && friend_p
20074 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20075 /* Core issue #226 (C++0x only): A default template-argument
20076 shall not be specified in a friend class template
20077 declaration. */
20078 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20079 /*is_partial=*/false, /*is_friend_decl=*/1);
20081 if (!friend_p && pushed_scope)
20082 pop_scope (pushed_scope);
20084 if (function_declarator_p (declarator)
20085 && parser->fully_implicit_function_template_p)
20087 if (member_p)
20088 decl = finish_fully_implicit_template (parser, decl);
20089 else
20090 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20093 if (auto_result && is_initialized && decl_specifiers->type
20094 && type_uses_auto (decl_specifiers->type))
20095 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20097 return decl;
20100 /* Parse a declarator.
20102 declarator:
20103 direct-declarator
20104 ptr-operator declarator
20106 abstract-declarator:
20107 ptr-operator abstract-declarator [opt]
20108 direct-abstract-declarator
20110 GNU Extensions:
20112 declarator:
20113 attributes [opt] direct-declarator
20114 attributes [opt] ptr-operator declarator
20116 abstract-declarator:
20117 attributes [opt] ptr-operator abstract-declarator [opt]
20118 attributes [opt] direct-abstract-declarator
20120 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20121 detect constructors, destructors, deduction guides, or conversion operators.
20122 It is set to -1 if the declarator is a name, and +1 if it is a
20123 function. Otherwise it is set to zero. Usually you just want to
20124 test for >0, but internally the negative value is used.
20126 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20127 a decl-specifier-seq unless it declares a constructor, destructor,
20128 or conversion. It might seem that we could check this condition in
20129 semantic analysis, rather than parsing, but that makes it difficult
20130 to handle something like `f()'. We want to notice that there are
20131 no decl-specifiers, and therefore realize that this is an
20132 expression, not a declaration.)
20134 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20135 the declarator is a direct-declarator of the form "(...)".
20137 MEMBER_P is true iff this declarator is a member-declarator.
20139 FRIEND_P is true iff this declarator is a friend. */
20141 static cp_declarator *
20142 cp_parser_declarator (cp_parser* parser,
20143 cp_parser_declarator_kind dcl_kind,
20144 int* ctor_dtor_or_conv_p,
20145 bool* parenthesized_p,
20146 bool member_p, bool friend_p)
20148 cp_declarator *declarator;
20149 enum tree_code code;
20150 cp_cv_quals cv_quals;
20151 tree class_type;
20152 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20154 /* Assume this is not a constructor, destructor, or type-conversion
20155 operator. */
20156 if (ctor_dtor_or_conv_p)
20157 *ctor_dtor_or_conv_p = 0;
20159 if (cp_parser_allow_gnu_extensions_p (parser))
20160 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20162 /* Check for the ptr-operator production. */
20163 cp_parser_parse_tentatively (parser);
20164 /* Parse the ptr-operator. */
20165 code = cp_parser_ptr_operator (parser,
20166 &class_type,
20167 &cv_quals,
20168 &std_attributes);
20170 /* If that worked, then we have a ptr-operator. */
20171 if (cp_parser_parse_definitely (parser))
20173 /* If a ptr-operator was found, then this declarator was not
20174 parenthesized. */
20175 if (parenthesized_p)
20176 *parenthesized_p = true;
20177 /* The dependent declarator is optional if we are parsing an
20178 abstract-declarator. */
20179 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20180 cp_parser_parse_tentatively (parser);
20182 /* Parse the dependent declarator. */
20183 declarator = cp_parser_declarator (parser, dcl_kind,
20184 /*ctor_dtor_or_conv_p=*/NULL,
20185 /*parenthesized_p=*/NULL,
20186 /*member_p=*/false,
20187 friend_p);
20189 /* If we are parsing an abstract-declarator, we must handle the
20190 case where the dependent declarator is absent. */
20191 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20192 && !cp_parser_parse_definitely (parser))
20193 declarator = NULL;
20195 declarator = cp_parser_make_indirect_declarator
20196 (code, class_type, cv_quals, declarator, std_attributes);
20198 /* Everything else is a direct-declarator. */
20199 else
20201 if (parenthesized_p)
20202 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20203 CPP_OPEN_PAREN);
20204 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20205 ctor_dtor_or_conv_p,
20206 member_p, friend_p);
20209 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20210 declarator->attributes = gnu_attributes;
20211 return declarator;
20214 /* Parse a direct-declarator or direct-abstract-declarator.
20216 direct-declarator:
20217 declarator-id
20218 direct-declarator ( parameter-declaration-clause )
20219 cv-qualifier-seq [opt]
20220 ref-qualifier [opt]
20221 exception-specification [opt]
20222 direct-declarator [ constant-expression [opt] ]
20223 ( declarator )
20225 direct-abstract-declarator:
20226 direct-abstract-declarator [opt]
20227 ( parameter-declaration-clause )
20228 cv-qualifier-seq [opt]
20229 ref-qualifier [opt]
20230 exception-specification [opt]
20231 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20232 ( abstract-declarator )
20234 Returns a representation of the declarator. DCL_KIND is
20235 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20236 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20237 we are parsing a direct-declarator. It is
20238 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20239 of ambiguity we prefer an abstract declarator, as per
20240 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20241 as for cp_parser_declarator. */
20243 static cp_declarator *
20244 cp_parser_direct_declarator (cp_parser* parser,
20245 cp_parser_declarator_kind dcl_kind,
20246 int* ctor_dtor_or_conv_p,
20247 bool member_p, bool friend_p)
20249 cp_token *token;
20250 cp_declarator *declarator = NULL;
20251 tree scope = NULL_TREE;
20252 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20253 bool saved_in_declarator_p = parser->in_declarator_p;
20254 bool first = true;
20255 tree pushed_scope = NULL_TREE;
20256 cp_token *open_paren = NULL, *close_paren = NULL;
20258 while (true)
20260 /* Peek at the next token. */
20261 token = cp_lexer_peek_token (parser->lexer);
20262 if (token->type == CPP_OPEN_PAREN)
20264 /* This is either a parameter-declaration-clause, or a
20265 parenthesized declarator. When we know we are parsing a
20266 named declarator, it must be a parenthesized declarator
20267 if FIRST is true. For instance, `(int)' is a
20268 parameter-declaration-clause, with an omitted
20269 direct-abstract-declarator. But `((*))', is a
20270 parenthesized abstract declarator. Finally, when T is a
20271 template parameter `(T)' is a
20272 parameter-declaration-clause, and not a parenthesized
20273 named declarator.
20275 We first try and parse a parameter-declaration-clause,
20276 and then try a nested declarator (if FIRST is true).
20278 It is not an error for it not to be a
20279 parameter-declaration-clause, even when FIRST is
20280 false. Consider,
20282 int i (int);
20283 int i (3);
20285 The first is the declaration of a function while the
20286 second is the definition of a variable, including its
20287 initializer.
20289 Having seen only the parenthesis, we cannot know which of
20290 these two alternatives should be selected. Even more
20291 complex are examples like:
20293 int i (int (a));
20294 int i (int (3));
20296 The former is a function-declaration; the latter is a
20297 variable initialization.
20299 Thus again, we try a parameter-declaration-clause, and if
20300 that fails, we back out and return. */
20302 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20304 tree params;
20305 bool is_declarator = false;
20307 open_paren = NULL;
20309 /* In a member-declarator, the only valid interpretation
20310 of a parenthesis is the start of a
20311 parameter-declaration-clause. (It is invalid to
20312 initialize a static data member with a parenthesized
20313 initializer; only the "=" form of initialization is
20314 permitted.) */
20315 if (!member_p)
20316 cp_parser_parse_tentatively (parser);
20318 /* Consume the `('. */
20319 matching_parens parens;
20320 parens.consume_open (parser);
20321 if (first)
20323 /* If this is going to be an abstract declarator, we're
20324 in a declarator and we can't have default args. */
20325 parser->default_arg_ok_p = false;
20326 parser->in_declarator_p = true;
20329 begin_scope (sk_function_parms, NULL_TREE);
20331 /* Parse the parameter-declaration-clause. */
20332 params = cp_parser_parameter_declaration_clause (parser);
20334 /* Consume the `)'. */
20335 parens.require_close (parser);
20337 /* If all went well, parse the cv-qualifier-seq,
20338 ref-qualifier and the exception-specification. */
20339 if (member_p || cp_parser_parse_definitely (parser))
20341 cp_cv_quals cv_quals;
20342 cp_virt_specifiers virt_specifiers;
20343 cp_ref_qualifier ref_qual;
20344 tree exception_specification;
20345 tree late_return;
20346 tree attrs;
20347 bool memfn = (member_p || (pushed_scope
20348 && CLASS_TYPE_P (pushed_scope)));
20350 is_declarator = true;
20352 if (ctor_dtor_or_conv_p)
20353 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20354 first = false;
20356 /* Parse the cv-qualifier-seq. */
20357 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20358 /* Parse the ref-qualifier. */
20359 ref_qual = cp_parser_ref_qualifier_opt (parser);
20360 /* Parse the tx-qualifier. */
20361 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20362 /* And the exception-specification. */
20363 exception_specification
20364 = cp_parser_exception_specification_opt (parser);
20366 attrs = cp_parser_std_attribute_spec_seq (parser);
20368 /* In here, we handle cases where attribute is used after
20369 the function declaration. For example:
20370 void func (int x) __attribute__((vector(..))); */
20371 tree gnu_attrs = NULL_TREE;
20372 tree requires_clause = NULL_TREE;
20373 late_return = (cp_parser_late_return_type_opt
20374 (parser, declarator, requires_clause,
20375 memfn ? cv_quals : -1));
20377 /* Parse the virt-specifier-seq. */
20378 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20380 /* Create the function-declarator. */
20381 declarator = make_call_declarator (declarator,
20382 params,
20383 cv_quals,
20384 virt_specifiers,
20385 ref_qual,
20386 tx_qual,
20387 exception_specification,
20388 late_return,
20389 requires_clause);
20390 declarator->std_attributes = attrs;
20391 declarator->attributes = gnu_attrs;
20392 /* Any subsequent parameter lists are to do with
20393 return type, so are not those of the declared
20394 function. */
20395 parser->default_arg_ok_p = false;
20398 /* Remove the function parms from scope. */
20399 pop_bindings_and_leave_scope ();
20401 if (is_declarator)
20402 /* Repeat the main loop. */
20403 continue;
20406 /* If this is the first, we can try a parenthesized
20407 declarator. */
20408 if (first)
20410 bool saved_in_type_id_in_expr_p;
20412 parser->default_arg_ok_p = saved_default_arg_ok_p;
20413 parser->in_declarator_p = saved_in_declarator_p;
20415 open_paren = token;
20416 /* Consume the `('. */
20417 matching_parens parens;
20418 parens.consume_open (parser);
20419 /* Parse the nested declarator. */
20420 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20421 parser->in_type_id_in_expr_p = true;
20422 declarator
20423 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20424 /*parenthesized_p=*/NULL,
20425 member_p, friend_p);
20426 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20427 first = false;
20428 /* Expect a `)'. */
20429 close_paren = cp_lexer_peek_token (parser->lexer);
20430 if (!parens.require_close (parser))
20431 declarator = cp_error_declarator;
20432 if (declarator == cp_error_declarator)
20433 break;
20435 goto handle_declarator;
20437 /* Otherwise, we must be done. */
20438 else
20439 break;
20441 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20442 && token->type == CPP_OPEN_SQUARE
20443 && !cp_next_tokens_can_be_attribute_p (parser))
20445 /* Parse an array-declarator. */
20446 tree bounds, attrs;
20448 if (ctor_dtor_or_conv_p)
20449 *ctor_dtor_or_conv_p = 0;
20451 open_paren = NULL;
20452 first = false;
20453 parser->default_arg_ok_p = false;
20454 parser->in_declarator_p = true;
20455 /* Consume the `['. */
20456 cp_lexer_consume_token (parser->lexer);
20457 /* Peek at the next token. */
20458 token = cp_lexer_peek_token (parser->lexer);
20459 /* If the next token is `]', then there is no
20460 constant-expression. */
20461 if (token->type != CPP_CLOSE_SQUARE)
20463 bool non_constant_p;
20464 bounds
20465 = cp_parser_constant_expression (parser,
20466 /*allow_non_constant=*/true,
20467 &non_constant_p);
20468 if (!non_constant_p)
20469 /* OK */;
20470 else if (error_operand_p (bounds))
20471 /* Already gave an error. */;
20472 else if (!parser->in_function_body
20473 || current_binding_level->kind == sk_function_parms)
20475 /* Normally, the array bound must be an integral constant
20476 expression. However, as an extension, we allow VLAs
20477 in function scopes as long as they aren't part of a
20478 parameter declaration. */
20479 cp_parser_error (parser,
20480 "array bound is not an integer constant");
20481 bounds = error_mark_node;
20483 else if (processing_template_decl
20484 && !type_dependent_expression_p (bounds))
20486 /* Remember this wasn't a constant-expression. */
20487 bounds = build_nop (TREE_TYPE (bounds), bounds);
20488 TREE_SIDE_EFFECTS (bounds) = 1;
20491 else
20492 bounds = NULL_TREE;
20493 /* Look for the closing `]'. */
20494 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20496 declarator = cp_error_declarator;
20497 break;
20500 attrs = cp_parser_std_attribute_spec_seq (parser);
20501 declarator = make_array_declarator (declarator, bounds);
20502 declarator->std_attributes = attrs;
20504 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20507 tree qualifying_scope;
20508 tree unqualified_name;
20509 tree attrs;
20510 special_function_kind sfk;
20511 bool abstract_ok;
20512 bool pack_expansion_p = false;
20513 cp_token *declarator_id_start_token;
20515 /* Parse a declarator-id */
20516 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20517 if (abstract_ok)
20519 cp_parser_parse_tentatively (parser);
20521 /* If we see an ellipsis, we should be looking at a
20522 parameter pack. */
20523 if (token->type == CPP_ELLIPSIS)
20525 /* Consume the `...' */
20526 cp_lexer_consume_token (parser->lexer);
20528 pack_expansion_p = true;
20532 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20533 unqualified_name
20534 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20535 qualifying_scope = parser->scope;
20536 if (abstract_ok)
20538 bool okay = false;
20540 if (!unqualified_name && pack_expansion_p)
20542 /* Check whether an error occurred. */
20543 okay = !cp_parser_error_occurred (parser);
20545 /* We already consumed the ellipsis to mark a
20546 parameter pack, but we have no way to report it,
20547 so abort the tentative parse. We will be exiting
20548 immediately anyway. */
20549 cp_parser_abort_tentative_parse (parser);
20551 else
20552 okay = cp_parser_parse_definitely (parser);
20554 if (!okay)
20555 unqualified_name = error_mark_node;
20556 else if (unqualified_name
20557 && (qualifying_scope
20558 || (!identifier_p (unqualified_name))))
20560 cp_parser_error (parser, "expected unqualified-id");
20561 unqualified_name = error_mark_node;
20565 if (!unqualified_name)
20566 return NULL;
20567 if (unqualified_name == error_mark_node)
20569 declarator = cp_error_declarator;
20570 pack_expansion_p = false;
20571 declarator->parameter_pack_p = false;
20572 break;
20575 attrs = cp_parser_std_attribute_spec_seq (parser);
20577 if (qualifying_scope && at_namespace_scope_p ()
20578 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20580 /* In the declaration of a member of a template class
20581 outside of the class itself, the SCOPE will sometimes
20582 be a TYPENAME_TYPE. For example, given:
20584 template <typename T>
20585 int S<T>::R::i = 3;
20587 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20588 this context, we must resolve S<T>::R to an ordinary
20589 type, rather than a typename type.
20591 The reason we normally avoid resolving TYPENAME_TYPEs
20592 is that a specialization of `S' might render
20593 `S<T>::R' not a type. However, if `S' is
20594 specialized, then this `i' will not be used, so there
20595 is no harm in resolving the types here. */
20596 tree type;
20598 /* Resolve the TYPENAME_TYPE. */
20599 type = resolve_typename_type (qualifying_scope,
20600 /*only_current_p=*/false);
20601 /* If that failed, the declarator is invalid. */
20602 if (TREE_CODE (type) == TYPENAME_TYPE)
20604 if (typedef_variant_p (type))
20605 error_at (declarator_id_start_token->location,
20606 "cannot define member of dependent typedef "
20607 "%qT", type);
20608 else
20609 error_at (declarator_id_start_token->location,
20610 "%<%T::%E%> is not a type",
20611 TYPE_CONTEXT (qualifying_scope),
20612 TYPE_IDENTIFIER (qualifying_scope));
20614 qualifying_scope = type;
20617 sfk = sfk_none;
20619 if (unqualified_name)
20621 tree class_type;
20623 if (qualifying_scope
20624 && CLASS_TYPE_P (qualifying_scope))
20625 class_type = qualifying_scope;
20626 else
20627 class_type = current_class_type;
20629 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20631 tree name_type = TREE_TYPE (unqualified_name);
20633 if (!class_type || !same_type_p (name_type, class_type))
20635 /* We do not attempt to print the declarator
20636 here because we do not have enough
20637 information about its original syntactic
20638 form. */
20639 cp_parser_error (parser, "invalid declarator");
20640 declarator = cp_error_declarator;
20641 break;
20643 else if (qualifying_scope
20644 && CLASSTYPE_USE_TEMPLATE (name_type))
20646 error_at (declarator_id_start_token->location,
20647 "invalid use of constructor as a template");
20648 inform (declarator_id_start_token->location,
20649 "use %<%T::%D%> instead of %<%T::%D%> to "
20650 "name the constructor in a qualified name",
20651 class_type,
20652 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20653 class_type, name_type);
20654 declarator = cp_error_declarator;
20655 break;
20657 unqualified_name = constructor_name (class_type);
20660 if (class_type)
20662 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20663 sfk = sfk_destructor;
20664 else if (identifier_p (unqualified_name)
20665 && IDENTIFIER_CONV_OP_P (unqualified_name))
20666 sfk = sfk_conversion;
20667 else if (/* There's no way to declare a constructor
20668 for an unnamed type, even if the type
20669 got a name for linkage purposes. */
20670 !TYPE_WAS_UNNAMED (class_type)
20671 /* Handle correctly (c++/19200):
20673 struct S {
20674 struct T{};
20675 friend void S(T);
20678 and also:
20680 namespace N {
20681 void S();
20684 struct S {
20685 friend void N::S();
20686 }; */
20687 && (!friend_p || class_type == qualifying_scope)
20688 && constructor_name_p (unqualified_name,
20689 class_type))
20690 sfk = sfk_constructor;
20691 else if (is_overloaded_fn (unqualified_name)
20692 && DECL_CONSTRUCTOR_P (get_first_fn
20693 (unqualified_name)))
20694 sfk = sfk_constructor;
20696 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20697 *ctor_dtor_or_conv_p = -1;
20700 declarator = make_id_declarator (qualifying_scope,
20701 unqualified_name,
20702 sfk);
20703 declarator->std_attributes = attrs;
20704 declarator->id_loc = token->location;
20705 declarator->parameter_pack_p = pack_expansion_p;
20707 if (pack_expansion_p)
20708 maybe_warn_variadic_templates ();
20711 handle_declarator:;
20712 scope = get_scope_of_declarator (declarator);
20713 if (scope)
20715 /* Any names that appear after the declarator-id for a
20716 member are looked up in the containing scope. */
20717 if (at_function_scope_p ())
20719 /* But declarations with qualified-ids can't appear in a
20720 function. */
20721 cp_parser_error (parser, "qualified-id in declaration");
20722 declarator = cp_error_declarator;
20723 break;
20725 pushed_scope = push_scope (scope);
20727 parser->in_declarator_p = true;
20728 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20729 || (declarator && declarator->kind == cdk_id))
20730 /* Default args are only allowed on function
20731 declarations. */
20732 parser->default_arg_ok_p = saved_default_arg_ok_p;
20733 else
20734 parser->default_arg_ok_p = false;
20736 first = false;
20738 /* We're done. */
20739 else
20740 break;
20743 /* For an abstract declarator, we might wind up with nothing at this
20744 point. That's an error; the declarator is not optional. */
20745 if (!declarator)
20746 cp_parser_error (parser, "expected declarator");
20747 else if (open_paren)
20749 /* Record overly parenthesized declarator so we can give a
20750 diagnostic about confusing decl/expr disambiguation. */
20751 if (declarator->kind == cdk_array)
20753 /* If the open and close parens are on different lines, this
20754 is probably a formatting thing, so ignore. */
20755 expanded_location open = expand_location (open_paren->location);
20756 expanded_location close = expand_location (close_paren->location);
20757 if (open.line != close.line || open.file != close.file)
20758 open_paren = NULL;
20760 if (open_paren)
20761 declarator->parenthesized = open_paren->location;
20764 /* If we entered a scope, we must exit it now. */
20765 if (pushed_scope)
20766 pop_scope (pushed_scope);
20768 parser->default_arg_ok_p = saved_default_arg_ok_p;
20769 parser->in_declarator_p = saved_in_declarator_p;
20771 return declarator;
20774 /* Parse a ptr-operator.
20776 ptr-operator:
20777 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20778 * cv-qualifier-seq [opt]
20780 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20781 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20783 GNU Extension:
20785 ptr-operator:
20786 & cv-qualifier-seq [opt]
20788 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20789 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20790 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20791 filled in with the TYPE containing the member. *CV_QUALS is
20792 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20793 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20794 Note that the tree codes returned by this function have nothing
20795 to do with the types of trees that will be eventually be created
20796 to represent the pointer or reference type being parsed. They are
20797 just constants with suggestive names. */
20798 static enum tree_code
20799 cp_parser_ptr_operator (cp_parser* parser,
20800 tree* type,
20801 cp_cv_quals *cv_quals,
20802 tree *attributes)
20804 enum tree_code code = ERROR_MARK;
20805 cp_token *token;
20806 tree attrs = NULL_TREE;
20808 /* Assume that it's not a pointer-to-member. */
20809 *type = NULL_TREE;
20810 /* And that there are no cv-qualifiers. */
20811 *cv_quals = TYPE_UNQUALIFIED;
20813 /* Peek at the next token. */
20814 token = cp_lexer_peek_token (parser->lexer);
20816 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20817 if (token->type == CPP_MULT)
20818 code = INDIRECT_REF;
20819 else if (token->type == CPP_AND)
20820 code = ADDR_EXPR;
20821 else if ((cxx_dialect != cxx98) &&
20822 token->type == CPP_AND_AND) /* C++0x only */
20823 code = NON_LVALUE_EXPR;
20825 if (code != ERROR_MARK)
20827 /* Consume the `*', `&' or `&&'. */
20828 cp_lexer_consume_token (parser->lexer);
20830 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20831 `&', if we are allowing GNU extensions. (The only qualifier
20832 that can legally appear after `&' is `restrict', but that is
20833 enforced during semantic analysis. */
20834 if (code == INDIRECT_REF
20835 || cp_parser_allow_gnu_extensions_p (parser))
20836 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20838 attrs = cp_parser_std_attribute_spec_seq (parser);
20839 if (attributes != NULL)
20840 *attributes = attrs;
20842 else
20844 /* Try the pointer-to-member case. */
20845 cp_parser_parse_tentatively (parser);
20846 /* Look for the optional `::' operator. */
20847 cp_parser_global_scope_opt (parser,
20848 /*current_scope_valid_p=*/false);
20849 /* Look for the nested-name specifier. */
20850 token = cp_lexer_peek_token (parser->lexer);
20851 cp_parser_nested_name_specifier (parser,
20852 /*typename_keyword_p=*/false,
20853 /*check_dependency_p=*/true,
20854 /*type_p=*/false,
20855 /*is_declaration=*/false);
20856 /* If we found it, and the next token is a `*', then we are
20857 indeed looking at a pointer-to-member operator. */
20858 if (!cp_parser_error_occurred (parser)
20859 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20861 /* Indicate that the `*' operator was used. */
20862 code = INDIRECT_REF;
20864 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20865 error_at (token->location, "%qD is a namespace", parser->scope);
20866 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20867 error_at (token->location, "cannot form pointer to member of "
20868 "non-class %q#T", parser->scope);
20869 else
20871 /* The type of which the member is a member is given by the
20872 current SCOPE. */
20873 *type = parser->scope;
20874 /* The next name will not be qualified. */
20875 parser->scope = NULL_TREE;
20876 parser->qualifying_scope = NULL_TREE;
20877 parser->object_scope = NULL_TREE;
20878 /* Look for optional c++11 attributes. */
20879 attrs = cp_parser_std_attribute_spec_seq (parser);
20880 if (attributes != NULL)
20881 *attributes = attrs;
20882 /* Look for the optional cv-qualifier-seq. */
20883 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20886 /* If that didn't work we don't have a ptr-operator. */
20887 if (!cp_parser_parse_definitely (parser))
20888 cp_parser_error (parser, "expected ptr-operator");
20891 return code;
20894 /* Parse an (optional) cv-qualifier-seq.
20896 cv-qualifier-seq:
20897 cv-qualifier cv-qualifier-seq [opt]
20899 cv-qualifier:
20900 const
20901 volatile
20903 GNU Extension:
20905 cv-qualifier:
20906 __restrict__
20908 Returns a bitmask representing the cv-qualifiers. */
20910 static cp_cv_quals
20911 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20913 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20915 while (true)
20917 cp_token *token;
20918 cp_cv_quals cv_qualifier;
20920 /* Peek at the next token. */
20921 token = cp_lexer_peek_token (parser->lexer);
20922 /* See if it's a cv-qualifier. */
20923 switch (token->keyword)
20925 case RID_CONST:
20926 cv_qualifier = TYPE_QUAL_CONST;
20927 break;
20929 case RID_VOLATILE:
20930 cv_qualifier = TYPE_QUAL_VOLATILE;
20931 break;
20933 case RID_RESTRICT:
20934 cv_qualifier = TYPE_QUAL_RESTRICT;
20935 break;
20937 default:
20938 cv_qualifier = TYPE_UNQUALIFIED;
20939 break;
20942 if (!cv_qualifier)
20943 break;
20945 if (cv_quals & cv_qualifier)
20947 gcc_rich_location richloc (token->location);
20948 richloc.add_fixit_remove ();
20949 error_at (&richloc, "duplicate cv-qualifier");
20950 cp_lexer_purge_token (parser->lexer);
20952 else
20954 cp_lexer_consume_token (parser->lexer);
20955 cv_quals |= cv_qualifier;
20959 return cv_quals;
20962 /* Parse an (optional) ref-qualifier
20964 ref-qualifier:
20968 Returns cp_ref_qualifier representing ref-qualifier. */
20970 static cp_ref_qualifier
20971 cp_parser_ref_qualifier_opt (cp_parser* parser)
20973 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20975 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20976 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20977 return ref_qual;
20979 while (true)
20981 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20982 cp_token *token = cp_lexer_peek_token (parser->lexer);
20984 switch (token->type)
20986 case CPP_AND:
20987 curr_ref_qual = REF_QUAL_LVALUE;
20988 break;
20990 case CPP_AND_AND:
20991 curr_ref_qual = REF_QUAL_RVALUE;
20992 break;
20994 default:
20995 curr_ref_qual = REF_QUAL_NONE;
20996 break;
20999 if (!curr_ref_qual)
21000 break;
21001 else if (ref_qual)
21003 error_at (token->location, "multiple ref-qualifiers");
21004 cp_lexer_purge_token (parser->lexer);
21006 else
21008 ref_qual = curr_ref_qual;
21009 cp_lexer_consume_token (parser->lexer);
21013 return ref_qual;
21016 /* Parse an optional tx-qualifier.
21018 tx-qualifier:
21019 transaction_safe
21020 transaction_safe_dynamic */
21022 static tree
21023 cp_parser_tx_qualifier_opt (cp_parser *parser)
21025 cp_token *token = cp_lexer_peek_token (parser->lexer);
21026 if (token->type == CPP_NAME)
21028 tree name = token->u.value;
21029 const char *p = IDENTIFIER_POINTER (name);
21030 const int len = strlen ("transaction_safe");
21031 if (!strncmp (p, "transaction_safe", len))
21033 p += len;
21034 if (*p == '\0'
21035 || !strcmp (p, "_dynamic"))
21037 cp_lexer_consume_token (parser->lexer);
21038 if (!flag_tm)
21040 error ("%qE requires %<-fgnu-tm%>", name);
21041 return NULL_TREE;
21043 else
21044 return name;
21048 return NULL_TREE;
21051 /* Parse an (optional) virt-specifier-seq.
21053 virt-specifier-seq:
21054 virt-specifier virt-specifier-seq [opt]
21056 virt-specifier:
21057 override
21058 final
21060 Returns a bitmask representing the virt-specifiers. */
21062 static cp_virt_specifiers
21063 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21065 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21067 while (true)
21069 cp_token *token;
21070 cp_virt_specifiers virt_specifier;
21072 /* Peek at the next token. */
21073 token = cp_lexer_peek_token (parser->lexer);
21074 /* See if it's a virt-specifier-qualifier. */
21075 if (token->type != CPP_NAME)
21076 break;
21077 if (id_equal (token->u.value, "override"))
21079 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21080 virt_specifier = VIRT_SPEC_OVERRIDE;
21082 else if (id_equal (token->u.value, "final"))
21084 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21085 virt_specifier = VIRT_SPEC_FINAL;
21087 else if (id_equal (token->u.value, "__final"))
21089 virt_specifier = VIRT_SPEC_FINAL;
21091 else
21092 break;
21094 if (virt_specifiers & virt_specifier)
21096 gcc_rich_location richloc (token->location);
21097 richloc.add_fixit_remove ();
21098 error_at (&richloc, "duplicate virt-specifier");
21099 cp_lexer_purge_token (parser->lexer);
21101 else
21103 cp_lexer_consume_token (parser->lexer);
21104 virt_specifiers |= virt_specifier;
21107 return virt_specifiers;
21110 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21111 is in scope even though it isn't real. */
21113 void
21114 inject_this_parameter (tree ctype, cp_cv_quals quals)
21116 tree this_parm;
21118 if (current_class_ptr)
21120 /* We don't clear this between NSDMIs. Is it already what we want? */
21121 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21122 if (DECL_P (current_class_ptr)
21123 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21124 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21125 && cp_type_quals (type) == quals)
21126 return;
21129 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21130 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21131 current_class_ptr = NULL_TREE;
21132 current_class_ref
21133 = cp_build_fold_indirect_ref (this_parm);
21134 current_class_ptr = this_parm;
21137 /* Return true iff our current scope is a non-static data member
21138 initializer. */
21140 bool
21141 parsing_nsdmi (void)
21143 /* We recognize NSDMI context by the context-less 'this' pointer set up
21144 by the function above. */
21145 if (current_class_ptr
21146 && TREE_CODE (current_class_ptr) == PARM_DECL
21147 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21148 return true;
21149 return false;
21152 /* Parse a late-specified return type, if any. This is not a separate
21153 non-terminal, but part of a function declarator, which looks like
21155 -> trailing-type-specifier-seq abstract-declarator(opt)
21157 Returns the type indicated by the type-id.
21159 In addition to this, parse any queued up #pragma omp declare simd
21160 clauses, and #pragma acc routine clauses.
21162 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21163 function. */
21165 static tree
21166 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21167 tree& requires_clause, cp_cv_quals quals)
21169 cp_token *token;
21170 tree type = NULL_TREE;
21171 bool declare_simd_p = (parser->omp_declare_simd
21172 && declarator
21173 && declarator->kind == cdk_id);
21175 bool oacc_routine_p = (parser->oacc_routine
21176 && declarator
21177 && declarator->kind == cdk_id);
21179 /* Peek at the next token. */
21180 token = cp_lexer_peek_token (parser->lexer);
21181 /* A late-specified return type is indicated by an initial '->'. */
21182 if (token->type != CPP_DEREF
21183 && token->keyword != RID_REQUIRES
21184 && !(token->type == CPP_NAME
21185 && token->u.value == ridpointers[RID_REQUIRES])
21186 && !(declare_simd_p || oacc_routine_p))
21187 return NULL_TREE;
21189 tree save_ccp = current_class_ptr;
21190 tree save_ccr = current_class_ref;
21191 if (quals >= 0)
21193 /* DR 1207: 'this' is in scope in the trailing return type. */
21194 inject_this_parameter (current_class_type, quals);
21197 if (token->type == CPP_DEREF)
21199 /* Consume the ->. */
21200 cp_lexer_consume_token (parser->lexer);
21202 type = cp_parser_trailing_type_id (parser);
21205 /* Function declarations may be followed by a trailing
21206 requires-clause. */
21207 requires_clause = cp_parser_requires_clause_opt (parser);
21209 if (declare_simd_p)
21210 declarator->attributes
21211 = cp_parser_late_parsing_omp_declare_simd (parser,
21212 declarator->attributes);
21213 if (oacc_routine_p)
21214 declarator->attributes
21215 = cp_parser_late_parsing_oacc_routine (parser,
21216 declarator->attributes);
21218 if (quals >= 0)
21220 current_class_ptr = save_ccp;
21221 current_class_ref = save_ccr;
21224 return type;
21227 /* Parse a declarator-id.
21229 declarator-id:
21230 id-expression
21231 :: [opt] nested-name-specifier [opt] type-name
21233 In the `id-expression' case, the value returned is as for
21234 cp_parser_id_expression if the id-expression was an unqualified-id.
21235 If the id-expression was a qualified-id, then a SCOPE_REF is
21236 returned. The first operand is the scope (either a NAMESPACE_DECL
21237 or TREE_TYPE), but the second is still just a representation of an
21238 unqualified-id. */
21240 static tree
21241 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21243 tree id;
21244 /* The expression must be an id-expression. Assume that qualified
21245 names are the names of types so that:
21247 template <class T>
21248 int S<T>::R::i = 3;
21250 will work; we must treat `S<T>::R' as the name of a type.
21251 Similarly, assume that qualified names are templates, where
21252 required, so that:
21254 template <class T>
21255 int S<T>::R<T>::i = 3;
21257 will work, too. */
21258 id = cp_parser_id_expression (parser,
21259 /*template_keyword_p=*/false,
21260 /*check_dependency_p=*/false,
21261 /*template_p=*/NULL,
21262 /*declarator_p=*/true,
21263 optional_p);
21264 if (id && BASELINK_P (id))
21265 id = BASELINK_FUNCTIONS (id);
21266 return id;
21269 /* Parse a type-id.
21271 type-id:
21272 type-specifier-seq abstract-declarator [opt]
21274 Returns the TYPE specified. */
21276 static tree
21277 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21278 bool is_trailing_return, location_t * type_location)
21280 cp_decl_specifier_seq type_specifier_seq;
21281 cp_declarator *abstract_declarator;
21283 /* Parse the type-specifier-seq. */
21284 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21285 is_trailing_return,
21286 &type_specifier_seq);
21287 if (type_location)
21288 *type_location = type_specifier_seq.locations[ds_type_spec];
21290 if (is_template_arg && type_specifier_seq.type
21291 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21292 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21293 /* A bare template name as a template argument is a template template
21294 argument, not a placeholder, so fail parsing it as a type argument. */
21296 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21297 cp_parser_simulate_error (parser);
21298 return error_mark_node;
21300 if (type_specifier_seq.type == error_mark_node)
21301 return error_mark_node;
21303 /* There might or might not be an abstract declarator. */
21304 cp_parser_parse_tentatively (parser);
21305 /* Look for the declarator. */
21306 abstract_declarator
21307 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21308 /*parenthesized_p=*/NULL,
21309 /*member_p=*/false,
21310 /*friend_p=*/false);
21311 /* Check to see if there really was a declarator. */
21312 if (!cp_parser_parse_definitely (parser))
21313 abstract_declarator = NULL;
21315 if (type_specifier_seq.type
21316 /* The concepts TS allows 'auto' as a type-id. */
21317 && (!flag_concepts || parser->in_type_id_in_expr_p)
21318 /* None of the valid uses of 'auto' in C++14 involve the type-id
21319 nonterminal, but it is valid in a trailing-return-type. */
21320 && !(cxx_dialect >= cxx14 && is_trailing_return))
21321 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21323 /* A type-id with type 'auto' is only ok if the abstract declarator
21324 is a function declarator with a late-specified return type.
21326 A type-id with 'auto' is also valid in a trailing-return-type
21327 in a compound-requirement. */
21328 if (abstract_declarator
21329 && abstract_declarator->kind == cdk_function
21330 && abstract_declarator->u.function.late_return_type)
21331 /* OK */;
21332 else if (parser->in_result_type_constraint_p)
21333 /* OK */;
21334 else
21336 location_t loc = type_specifier_seq.locations[ds_type_spec];
21337 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21339 error_at (loc, "missing template arguments after %qT",
21340 auto_node);
21341 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21342 tmpl);
21344 else
21345 error_at (loc, "invalid use of %qT", auto_node);
21346 return error_mark_node;
21350 return groktypename (&type_specifier_seq, abstract_declarator,
21351 is_template_arg);
21354 static tree
21355 cp_parser_type_id (cp_parser *parser, location_t * type_location)
21357 return cp_parser_type_id_1 (parser, false, false, type_location);
21360 static tree
21361 cp_parser_template_type_arg (cp_parser *parser)
21363 tree r;
21364 const char *saved_message = parser->type_definition_forbidden_message;
21365 parser->type_definition_forbidden_message
21366 = G_("types may not be defined in template arguments");
21367 r = cp_parser_type_id_1 (parser, true, false, NULL);
21368 parser->type_definition_forbidden_message = saved_message;
21369 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21371 error ("invalid use of %<auto%> in template argument");
21372 r = error_mark_node;
21374 return r;
21377 static tree
21378 cp_parser_trailing_type_id (cp_parser *parser)
21380 return cp_parser_type_id_1 (parser, false, true, NULL);
21383 /* Parse a type-specifier-seq.
21385 type-specifier-seq:
21386 type-specifier type-specifier-seq [opt]
21388 GNU extension:
21390 type-specifier-seq:
21391 attributes type-specifier-seq [opt]
21393 If IS_DECLARATION is true, we are at the start of a "condition" or
21394 exception-declaration, so we might be followed by a declarator-id.
21396 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21397 i.e. we've just seen "->".
21399 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21401 static void
21402 cp_parser_type_specifier_seq (cp_parser* parser,
21403 bool is_declaration,
21404 bool is_trailing_return,
21405 cp_decl_specifier_seq *type_specifier_seq)
21407 bool seen_type_specifier = false;
21408 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21409 cp_token *start_token = NULL;
21411 /* Clear the TYPE_SPECIFIER_SEQ. */
21412 clear_decl_specs (type_specifier_seq);
21414 /* In the context of a trailing return type, enum E { } is an
21415 elaborated-type-specifier followed by a function-body, not an
21416 enum-specifier. */
21417 if (is_trailing_return)
21418 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21420 /* Parse the type-specifiers and attributes. */
21421 while (true)
21423 tree type_specifier;
21424 bool is_cv_qualifier;
21426 /* Check for attributes first. */
21427 if (cp_next_tokens_can_be_attribute_p (parser))
21429 type_specifier_seq->attributes
21430 = attr_chainon (type_specifier_seq->attributes,
21431 cp_parser_attributes_opt (parser));
21432 continue;
21435 /* record the token of the beginning of the type specifier seq,
21436 for error reporting purposes*/
21437 if (!start_token)
21438 start_token = cp_lexer_peek_token (parser->lexer);
21440 /* Look for the type-specifier. */
21441 type_specifier = cp_parser_type_specifier (parser,
21442 flags,
21443 type_specifier_seq,
21444 /*is_declaration=*/false,
21445 NULL,
21446 &is_cv_qualifier);
21447 if (!type_specifier)
21449 /* If the first type-specifier could not be found, this is not a
21450 type-specifier-seq at all. */
21451 if (!seen_type_specifier)
21453 /* Set in_declarator_p to avoid skipping to the semicolon. */
21454 int in_decl = parser->in_declarator_p;
21455 parser->in_declarator_p = true;
21457 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21458 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21459 cp_parser_error (parser, "expected type-specifier");
21461 parser->in_declarator_p = in_decl;
21463 type_specifier_seq->type = error_mark_node;
21464 return;
21466 /* If subsequent type-specifiers could not be found, the
21467 type-specifier-seq is complete. */
21468 break;
21471 seen_type_specifier = true;
21472 /* The standard says that a condition can be:
21474 type-specifier-seq declarator = assignment-expression
21476 However, given:
21478 struct S {};
21479 if (int S = ...)
21481 we should treat the "S" as a declarator, not as a
21482 type-specifier. The standard doesn't say that explicitly for
21483 type-specifier-seq, but it does say that for
21484 decl-specifier-seq in an ordinary declaration. Perhaps it
21485 would be clearer just to allow a decl-specifier-seq here, and
21486 then add a semantic restriction that if any decl-specifiers
21487 that are not type-specifiers appear, the program is invalid. */
21488 if (is_declaration && !is_cv_qualifier)
21489 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21493 /* Return whether the function currently being declared has an associated
21494 template parameter list. */
21496 static bool
21497 function_being_declared_is_template_p (cp_parser* parser)
21499 if (!current_template_parms || processing_template_parmlist)
21500 return false;
21502 if (parser->implicit_template_scope)
21503 return true;
21505 if (at_class_scope_p ()
21506 && TYPE_BEING_DEFINED (current_class_type))
21507 return parser->num_template_parameter_lists != 0;
21509 return ((int) parser->num_template_parameter_lists > template_class_depth
21510 (current_class_type));
21513 /* Parse a parameter-declaration-clause.
21515 parameter-declaration-clause:
21516 parameter-declaration-list [opt] ... [opt]
21517 parameter-declaration-list , ...
21519 Returns a representation for the parameter declarations. A return
21520 value of NULL indicates a parameter-declaration-clause consisting
21521 only of an ellipsis. */
21523 static tree
21524 cp_parser_parameter_declaration_clause (cp_parser* parser)
21526 tree parameters;
21527 cp_token *token;
21528 bool ellipsis_p;
21530 temp_override<bool> cleanup
21531 (parser->auto_is_implicit_function_template_parm_p);
21533 if (!processing_specialization
21534 && !processing_template_parmlist
21535 && !processing_explicit_instantiation
21536 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21537 actual function or a random abstract declarator. */
21538 && parser->default_arg_ok_p)
21539 if (!current_function_decl
21540 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21541 parser->auto_is_implicit_function_template_parm_p = true;
21543 /* Peek at the next token. */
21544 token = cp_lexer_peek_token (parser->lexer);
21545 /* Check for trivial parameter-declaration-clauses. */
21546 if (token->type == CPP_ELLIPSIS)
21548 /* Consume the `...' token. */
21549 cp_lexer_consume_token (parser->lexer);
21550 return NULL_TREE;
21552 else if (token->type == CPP_CLOSE_PAREN)
21553 /* There are no parameters. */
21554 return void_list_node;
21555 /* Check for `(void)', too, which is a special case. */
21556 else if (token->keyword == RID_VOID
21557 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21558 == CPP_CLOSE_PAREN))
21560 /* Consume the `void' token. */
21561 cp_lexer_consume_token (parser->lexer);
21562 /* There are no parameters. */
21563 return void_list_node;
21566 /* Parse the parameter-declaration-list. */
21567 parameters = cp_parser_parameter_declaration_list (parser);
21568 /* If a parse error occurred while parsing the
21569 parameter-declaration-list, then the entire
21570 parameter-declaration-clause is erroneous. */
21571 if (parameters == error_mark_node)
21572 return NULL_TREE;
21574 /* Peek at the next token. */
21575 token = cp_lexer_peek_token (parser->lexer);
21576 /* If it's a `,', the clause should terminate with an ellipsis. */
21577 if (token->type == CPP_COMMA)
21579 /* Consume the `,'. */
21580 cp_lexer_consume_token (parser->lexer);
21581 /* Expect an ellipsis. */
21582 ellipsis_p
21583 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21585 /* It might also be `...' if the optional trailing `,' was
21586 omitted. */
21587 else if (token->type == CPP_ELLIPSIS)
21589 /* Consume the `...' token. */
21590 cp_lexer_consume_token (parser->lexer);
21591 /* And remember that we saw it. */
21592 ellipsis_p = true;
21594 else
21595 ellipsis_p = false;
21597 /* Finish the parameter list. */
21598 if (!ellipsis_p)
21599 parameters = chainon (parameters, void_list_node);
21601 return parameters;
21604 /* Parse a parameter-declaration-list.
21606 parameter-declaration-list:
21607 parameter-declaration
21608 parameter-declaration-list , parameter-declaration
21610 Returns a representation of the parameter-declaration-list, as for
21611 cp_parser_parameter_declaration_clause. However, the
21612 `void_list_node' is never appended to the list. */
21614 static tree
21615 cp_parser_parameter_declaration_list (cp_parser* parser)
21617 tree parameters = NULL_TREE;
21618 tree *tail = &parameters;
21619 bool saved_in_unbraced_linkage_specification_p;
21620 int index = 0;
21622 /* The special considerations that apply to a function within an
21623 unbraced linkage specifications do not apply to the parameters
21624 to the function. */
21625 saved_in_unbraced_linkage_specification_p
21626 = parser->in_unbraced_linkage_specification_p;
21627 parser->in_unbraced_linkage_specification_p = false;
21629 /* Look for more parameters. */
21630 while (true)
21632 cp_parameter_declarator *parameter;
21633 tree decl = error_mark_node;
21634 bool parenthesized_p = false;
21636 /* Parse the parameter. */
21637 parameter
21638 = cp_parser_parameter_declaration (parser,
21639 /*template_parm_p=*/false,
21640 &parenthesized_p);
21642 /* We don't know yet if the enclosing context is deprecated, so wait
21643 and warn in grokparms if appropriate. */
21644 deprecated_state = DEPRECATED_SUPPRESS;
21646 if (parameter)
21648 decl = grokdeclarator (parameter->declarator,
21649 &parameter->decl_specifiers,
21650 PARM,
21651 parameter->default_argument != NULL_TREE,
21652 &parameter->decl_specifiers.attributes);
21653 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21654 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21657 deprecated_state = DEPRECATED_NORMAL;
21659 /* If a parse error occurred parsing the parameter declaration,
21660 then the entire parameter-declaration-list is erroneous. */
21661 if (decl == error_mark_node)
21663 parameters = error_mark_node;
21664 break;
21667 if (parameter->decl_specifiers.attributes)
21668 cplus_decl_attributes (&decl,
21669 parameter->decl_specifiers.attributes,
21671 if (DECL_NAME (decl))
21672 decl = pushdecl (decl);
21674 if (decl != error_mark_node)
21676 retrofit_lang_decl (decl);
21677 DECL_PARM_INDEX (decl) = ++index;
21678 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21681 /* Add the new parameter to the list. */
21682 *tail = build_tree_list (parameter->default_argument, decl);
21683 tail = &TREE_CHAIN (*tail);
21685 /* Peek at the next token. */
21686 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21687 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21688 /* These are for Objective-C++ */
21689 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21690 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21691 /* The parameter-declaration-list is complete. */
21692 break;
21693 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21695 cp_token *token;
21697 /* Peek at the next token. */
21698 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21699 /* If it's an ellipsis, then the list is complete. */
21700 if (token->type == CPP_ELLIPSIS)
21701 break;
21702 /* Otherwise, there must be more parameters. Consume the
21703 `,'. */
21704 cp_lexer_consume_token (parser->lexer);
21705 /* When parsing something like:
21707 int i(float f, double d)
21709 we can tell after seeing the declaration for "f" that we
21710 are not looking at an initialization of a variable "i",
21711 but rather at the declaration of a function "i".
21713 Due to the fact that the parsing of template arguments
21714 (as specified to a template-id) requires backtracking we
21715 cannot use this technique when inside a template argument
21716 list. */
21717 if (!parser->in_template_argument_list_p
21718 && !parser->in_type_id_in_expr_p
21719 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21720 /* However, a parameter-declaration of the form
21721 "float(f)" (which is a valid declaration of a
21722 parameter "f") can also be interpreted as an
21723 expression (the conversion of "f" to "float"). */
21724 && !parenthesized_p)
21725 cp_parser_commit_to_tentative_parse (parser);
21727 else
21729 cp_parser_error (parser, "expected %<,%> or %<...%>");
21730 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21731 cp_parser_skip_to_closing_parenthesis (parser,
21732 /*recovering=*/true,
21733 /*or_comma=*/false,
21734 /*consume_paren=*/false);
21735 break;
21739 parser->in_unbraced_linkage_specification_p
21740 = saved_in_unbraced_linkage_specification_p;
21742 /* Reset implicit_template_scope if we are about to leave the function
21743 parameter list that introduced it. Note that for out-of-line member
21744 definitions, there will be one or more class scopes before we get to
21745 the template parameter scope. */
21747 if (cp_binding_level *its = parser->implicit_template_scope)
21748 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21750 while (maybe_its->kind == sk_class)
21751 maybe_its = maybe_its->level_chain;
21752 if (maybe_its == its)
21754 parser->implicit_template_parms = 0;
21755 parser->implicit_template_scope = 0;
21759 return parameters;
21762 /* Parse a parameter declaration.
21764 parameter-declaration:
21765 decl-specifier-seq ... [opt] declarator
21766 decl-specifier-seq declarator = assignment-expression
21767 decl-specifier-seq ... [opt] abstract-declarator [opt]
21768 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21770 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21771 declares a template parameter. (In that case, a non-nested `>'
21772 token encountered during the parsing of the assignment-expression
21773 is not interpreted as a greater-than operator.)
21775 Returns a representation of the parameter, or NULL if an error
21776 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21777 true iff the declarator is of the form "(p)". */
21779 static cp_parameter_declarator *
21780 cp_parser_parameter_declaration (cp_parser *parser,
21781 bool template_parm_p,
21782 bool *parenthesized_p)
21784 int declares_class_or_enum;
21785 cp_decl_specifier_seq decl_specifiers;
21786 cp_declarator *declarator;
21787 tree default_argument;
21788 cp_token *token = NULL, *declarator_token_start = NULL;
21789 const char *saved_message;
21790 bool template_parameter_pack_p = false;
21792 /* In a template parameter, `>' is not an operator.
21794 [temp.param]
21796 When parsing a default template-argument for a non-type
21797 template-parameter, the first non-nested `>' is taken as the end
21798 of the template parameter-list rather than a greater-than
21799 operator. */
21801 /* Type definitions may not appear in parameter types. */
21802 saved_message = parser->type_definition_forbidden_message;
21803 parser->type_definition_forbidden_message
21804 = G_("types may not be defined in parameter types");
21806 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21807 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21808 (current_template_parms)) : 0);
21810 /* Parse the declaration-specifiers. */
21811 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21812 cp_parser_decl_specifier_seq (parser,
21813 CP_PARSER_FLAGS_NONE,
21814 &decl_specifiers,
21815 &declares_class_or_enum);
21817 /* Complain about missing 'typename' or other invalid type names. */
21818 if (!decl_specifiers.any_type_specifiers_p
21819 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21820 decl_specifiers.type = error_mark_node;
21822 /* If an error occurred, there's no reason to attempt to parse the
21823 rest of the declaration. */
21824 if (cp_parser_error_occurred (parser))
21826 parser->type_definition_forbidden_message = saved_message;
21827 return NULL;
21830 /* Peek at the next token. */
21831 token = cp_lexer_peek_token (parser->lexer);
21833 /* If the next token is a `)', `,', `=', `>', or `...', then there
21834 is no declarator. However, when variadic templates are enabled,
21835 there may be a declarator following `...'. */
21836 if (token->type == CPP_CLOSE_PAREN
21837 || token->type == CPP_COMMA
21838 || token->type == CPP_EQ
21839 || token->type == CPP_GREATER)
21841 declarator = NULL;
21842 if (parenthesized_p)
21843 *parenthesized_p = false;
21845 /* Otherwise, there should be a declarator. */
21846 else
21848 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21849 parser->default_arg_ok_p = false;
21851 /* After seeing a decl-specifier-seq, if the next token is not a
21852 "(", there is no possibility that the code is a valid
21853 expression. Therefore, if parsing tentatively, we commit at
21854 this point. */
21855 if (!parser->in_template_argument_list_p
21856 /* In an expression context, having seen:
21858 (int((char ...
21860 we cannot be sure whether we are looking at a
21861 function-type (taking a "char" as a parameter) or a cast
21862 of some object of type "char" to "int". */
21863 && !parser->in_type_id_in_expr_p
21864 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21865 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21866 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21867 cp_parser_commit_to_tentative_parse (parser);
21868 /* Parse the declarator. */
21869 declarator_token_start = token;
21870 declarator = cp_parser_declarator (parser,
21871 CP_PARSER_DECLARATOR_EITHER,
21872 /*ctor_dtor_or_conv_p=*/NULL,
21873 parenthesized_p,
21874 /*member_p=*/false,
21875 /*friend_p=*/false);
21876 parser->default_arg_ok_p = saved_default_arg_ok_p;
21877 /* After the declarator, allow more attributes. */
21878 decl_specifiers.attributes
21879 = attr_chainon (decl_specifiers.attributes,
21880 cp_parser_attributes_opt (parser));
21882 /* If the declarator is a template parameter pack, remember that and
21883 clear the flag in the declarator itself so we don't get errors
21884 from grokdeclarator. */
21885 if (template_parm_p && declarator && declarator->parameter_pack_p)
21887 declarator->parameter_pack_p = false;
21888 template_parameter_pack_p = true;
21892 /* If the next token is an ellipsis, and we have not seen a declarator
21893 name, and if either the type of the declarator contains parameter
21894 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21895 for, eg, abbreviated integral type names), then we actually have a
21896 parameter pack expansion expression. Otherwise, leave the ellipsis
21897 for a C-style variadic function. */
21898 token = cp_lexer_peek_token (parser->lexer);
21900 /* If a function parameter pack was specified and an implicit template
21901 parameter was introduced during cp_parser_parameter_declaration,
21902 change any implicit parameters introduced into packs. */
21903 if (parser->implicit_template_parms
21904 && ((token->type == CPP_ELLIPSIS
21905 && declarator_can_be_parameter_pack (declarator))
21906 || (declarator && declarator->parameter_pack_p)))
21908 int latest_template_parm_idx = TREE_VEC_LENGTH
21909 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21911 if (latest_template_parm_idx != template_parm_idx)
21912 decl_specifiers.type = convert_generic_types_to_packs
21913 (decl_specifiers.type,
21914 template_parm_idx, latest_template_parm_idx);
21917 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21919 tree type = decl_specifiers.type;
21921 if (type && DECL_P (type))
21922 type = TREE_TYPE (type);
21924 if (((type
21925 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21926 && (template_parm_p || uses_parameter_packs (type)))
21927 || (!type && template_parm_p))
21928 && declarator_can_be_parameter_pack (declarator))
21930 /* Consume the `...'. */
21931 cp_lexer_consume_token (parser->lexer);
21932 maybe_warn_variadic_templates ();
21934 /* Build a pack expansion type */
21935 if (template_parm_p)
21936 template_parameter_pack_p = true;
21937 else if (declarator)
21938 declarator->parameter_pack_p = true;
21939 else
21940 decl_specifiers.type = make_pack_expansion (type);
21944 /* The restriction on defining new types applies only to the type
21945 of the parameter, not to the default argument. */
21946 parser->type_definition_forbidden_message = saved_message;
21948 /* If the next token is `=', then process a default argument. */
21949 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21951 tree type = decl_specifiers.type;
21952 token = cp_lexer_peek_token (parser->lexer);
21953 /* If we are defining a class, then the tokens that make up the
21954 default argument must be saved and processed later. */
21955 if (!template_parm_p && at_class_scope_p ()
21956 && TYPE_BEING_DEFINED (current_class_type)
21957 && !LAMBDA_TYPE_P (current_class_type))
21958 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21960 // A constrained-type-specifier may declare a type template-parameter.
21961 else if (declares_constrained_type_template_parameter (type))
21962 default_argument
21963 = cp_parser_default_type_template_argument (parser);
21965 // A constrained-type-specifier may declare a template-template-parameter.
21966 else if (declares_constrained_template_template_parameter (type))
21967 default_argument
21968 = cp_parser_default_template_template_argument (parser);
21970 /* Outside of a class definition, we can just parse the
21971 assignment-expression. */
21972 else
21973 default_argument
21974 = cp_parser_default_argument (parser, template_parm_p);
21976 if (!parser->default_arg_ok_p)
21978 permerror (token->location,
21979 "default arguments are only "
21980 "permitted for function parameters");
21982 else if ((declarator && declarator->parameter_pack_p)
21983 || template_parameter_pack_p
21984 || (decl_specifiers.type
21985 && PACK_EXPANSION_P (decl_specifiers.type)))
21987 /* Find the name of the parameter pack. */
21988 cp_declarator *id_declarator = declarator;
21989 while (id_declarator && id_declarator->kind != cdk_id)
21990 id_declarator = id_declarator->declarator;
21992 if (id_declarator && id_declarator->kind == cdk_id)
21993 error_at (declarator_token_start->location,
21994 template_parm_p
21995 ? G_("template parameter pack %qD "
21996 "cannot have a default argument")
21997 : G_("parameter pack %qD cannot have "
21998 "a default argument"),
21999 id_declarator->u.id.unqualified_name);
22000 else
22001 error_at (declarator_token_start->location,
22002 template_parm_p
22003 ? G_("template parameter pack cannot have "
22004 "a default argument")
22005 : G_("parameter pack cannot have a "
22006 "default argument"));
22008 default_argument = NULL_TREE;
22011 else
22012 default_argument = NULL_TREE;
22014 /* Generate a location for the parameter, ranging from the start of the
22015 initial token to the end of the final token (using input_location for
22016 the latter, set up by cp_lexer_set_source_position_from_token when
22017 consuming tokens).
22019 If we have a identifier, then use it for the caret location, e.g.
22021 extern int callee (int one, int (*two)(int, int), float three);
22022 ~~~~~~^~~~~~~~~~~~~~
22024 otherwise, reuse the start location for the caret location e.g.:
22026 extern int callee (int one, int (*)(int, int), float three);
22027 ^~~~~~~~~~~~~~~~~
22030 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22031 ? declarator->id_loc
22032 : decl_spec_token_start->location);
22033 location_t param_loc = make_location (caret_loc,
22034 decl_spec_token_start->location,
22035 input_location);
22037 return make_parameter_declarator (&decl_specifiers,
22038 declarator,
22039 default_argument,
22040 param_loc,
22041 template_parameter_pack_p);
22044 /* Parse a default argument and return it.
22046 TEMPLATE_PARM_P is true if this is a default argument for a
22047 non-type template parameter. */
22048 static tree
22049 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22051 tree default_argument = NULL_TREE;
22052 bool saved_greater_than_is_operator_p;
22053 bool saved_local_variables_forbidden_p;
22054 bool non_constant_p, is_direct_init;
22056 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22057 set correctly. */
22058 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22059 parser->greater_than_is_operator_p = !template_parm_p;
22060 /* Local variable names (and the `this' keyword) may not
22061 appear in a default argument. */
22062 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22063 parser->local_variables_forbidden_p = true;
22064 /* Parse the assignment-expression. */
22065 if (template_parm_p)
22066 push_deferring_access_checks (dk_no_deferred);
22067 tree saved_class_ptr = NULL_TREE;
22068 tree saved_class_ref = NULL_TREE;
22069 /* The "this" pointer is not valid in a default argument. */
22070 if (cfun)
22072 saved_class_ptr = current_class_ptr;
22073 cp_function_chain->x_current_class_ptr = NULL_TREE;
22074 saved_class_ref = current_class_ref;
22075 cp_function_chain->x_current_class_ref = NULL_TREE;
22077 default_argument
22078 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22079 /* Restore the "this" pointer. */
22080 if (cfun)
22082 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22083 cp_function_chain->x_current_class_ref = saved_class_ref;
22085 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22086 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22087 if (template_parm_p)
22088 pop_deferring_access_checks ();
22089 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22090 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22092 return default_argument;
22095 /* Parse a function-body.
22097 function-body:
22098 compound_statement */
22100 static void
22101 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22103 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22104 ? BCS_TRY_BLOCK : BCS_NORMAL),
22105 true);
22108 /* Parse a ctor-initializer-opt followed by a function-body. Return
22109 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22110 is true we are parsing a function-try-block. */
22112 static void
22113 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22114 bool in_function_try_block)
22116 tree body, list;
22117 const bool check_body_p =
22118 DECL_CONSTRUCTOR_P (current_function_decl)
22119 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
22120 tree last = NULL;
22122 /* Begin the function body. */
22123 body = begin_function_body ();
22124 /* Parse the optional ctor-initializer. */
22125 cp_parser_ctor_initializer_opt (parser);
22127 /* If we're parsing a constexpr constructor definition, we need
22128 to check that the constructor body is indeed empty. However,
22129 before we get to cp_parser_function_body lot of junk has been
22130 generated, so we can't just check that we have an empty block.
22131 Rather we take a snapshot of the outermost block, and check whether
22132 cp_parser_function_body changed its state. */
22133 if (check_body_p)
22135 list = cur_stmt_list;
22136 if (STATEMENT_LIST_TAIL (list))
22137 last = STATEMENT_LIST_TAIL (list)->stmt;
22139 /* Parse the function-body. */
22140 cp_parser_function_body (parser, in_function_try_block);
22141 if (check_body_p)
22142 check_constexpr_ctor_body (last, list, /*complain=*/true);
22143 /* Finish the function body. */
22144 finish_function_body (body);
22147 /* Parse an initializer.
22149 initializer:
22150 = initializer-clause
22151 ( expression-list )
22153 Returns an expression representing the initializer. If no
22154 initializer is present, NULL_TREE is returned.
22156 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22157 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22158 set to TRUE if there is no initializer present. If there is an
22159 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22160 is set to true; otherwise it is set to false. */
22162 static tree
22163 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22164 bool* non_constant_p, bool subexpression_p)
22166 cp_token *token;
22167 tree init;
22169 /* Peek at the next token. */
22170 token = cp_lexer_peek_token (parser->lexer);
22172 /* Let our caller know whether or not this initializer was
22173 parenthesized. */
22174 *is_direct_init = (token->type != CPP_EQ);
22175 /* Assume that the initializer is constant. */
22176 *non_constant_p = false;
22178 if (token->type == CPP_EQ)
22180 /* Consume the `='. */
22181 cp_lexer_consume_token (parser->lexer);
22182 /* Parse the initializer-clause. */
22183 init = cp_parser_initializer_clause (parser, non_constant_p);
22185 else if (token->type == CPP_OPEN_PAREN)
22187 vec<tree, va_gc> *vec;
22188 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22189 /*cast_p=*/false,
22190 /*allow_expansion_p=*/true,
22191 non_constant_p);
22192 if (vec == NULL)
22193 return error_mark_node;
22194 init = build_tree_list_vec (vec);
22195 release_tree_vector (vec);
22197 else if (token->type == CPP_OPEN_BRACE)
22199 cp_lexer_set_source_position (parser->lexer);
22200 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22201 init = cp_parser_braced_list (parser, non_constant_p);
22202 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22204 else
22206 /* Anything else is an error. */
22207 cp_parser_error (parser, "expected initializer");
22208 init = error_mark_node;
22211 if (!subexpression_p && check_for_bare_parameter_packs (init))
22212 init = error_mark_node;
22214 return init;
22217 /* Parse an initializer-clause.
22219 initializer-clause:
22220 assignment-expression
22221 braced-init-list
22223 Returns an expression representing the initializer.
22225 If the `assignment-expression' production is used the value
22226 returned is simply a representation for the expression.
22228 Otherwise, calls cp_parser_braced_list. */
22230 static cp_expr
22231 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22233 cp_expr initializer;
22235 /* Assume the expression is constant. */
22236 *non_constant_p = false;
22238 /* If it is not a `{', then we are looking at an
22239 assignment-expression. */
22240 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22242 initializer
22243 = cp_parser_constant_expression (parser,
22244 /*allow_non_constant_p=*/true,
22245 non_constant_p);
22247 else
22248 initializer = cp_parser_braced_list (parser, non_constant_p);
22250 return initializer;
22253 /* Parse a brace-enclosed initializer list.
22255 braced-init-list:
22256 { initializer-list , [opt] }
22257 { designated-initializer-list , [opt] }
22260 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22261 the elements of the initializer-list (or NULL, if the last
22262 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22263 NULL_TREE. There is no way to detect whether or not the optional
22264 trailing `,' was provided. NON_CONSTANT_P is as for
22265 cp_parser_initializer. */
22267 static cp_expr
22268 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22270 tree initializer;
22271 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22273 /* Consume the `{' token. */
22274 matching_braces braces;
22275 braces.require_open (parser);
22276 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22277 initializer = make_node (CONSTRUCTOR);
22278 /* If it's not a `}', then there is a non-trivial initializer. */
22279 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22281 /* Parse the initializer list. */
22282 CONSTRUCTOR_ELTS (initializer)
22283 = cp_parser_initializer_list (parser, non_constant_p);
22284 /* A trailing `,' token is allowed. */
22285 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22286 cp_lexer_consume_token (parser->lexer);
22288 else
22289 *non_constant_p = false;
22290 /* Now, there should be a trailing `}'. */
22291 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22292 braces.require_close (parser);
22293 TREE_TYPE (initializer) = init_list_type_node;
22295 cp_expr result (initializer);
22296 /* Build a location of the form:
22297 { ... }
22298 ^~~~~~~
22299 with caret==start at the open brace, finish at the close brace. */
22300 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22301 result.set_location (combined_loc);
22302 return result;
22305 /* Consume tokens up to, and including, the next non-nested closing `]'.
22306 Returns true iff we found a closing `]'. */
22308 static bool
22309 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22311 unsigned square_depth = 0;
22313 while (true)
22315 cp_token * token = cp_lexer_peek_token (parser->lexer);
22317 switch (token->type)
22319 case CPP_EOF:
22320 case CPP_PRAGMA_EOL:
22321 /* If we've run out of tokens, then there is no closing `]'. */
22322 return false;
22324 case CPP_OPEN_SQUARE:
22325 ++square_depth;
22326 break;
22328 case CPP_CLOSE_SQUARE:
22329 if (!square_depth--)
22331 cp_lexer_consume_token (parser->lexer);
22332 return true;
22334 break;
22336 default:
22337 break;
22340 /* Consume the token. */
22341 cp_lexer_consume_token (parser->lexer);
22345 /* Return true if we are looking at an array-designator, false otherwise. */
22347 static bool
22348 cp_parser_array_designator_p (cp_parser *parser)
22350 /* Consume the `['. */
22351 cp_lexer_consume_token (parser->lexer);
22353 cp_lexer_save_tokens (parser->lexer);
22355 /* Skip tokens until the next token is a closing square bracket.
22356 If we find the closing `]', and the next token is a `=', then
22357 we are looking at an array designator. */
22358 bool array_designator_p
22359 = (cp_parser_skip_to_closing_square_bracket (parser)
22360 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22362 /* Roll back the tokens we skipped. */
22363 cp_lexer_rollback_tokens (parser->lexer);
22365 return array_designator_p;
22368 /* Parse an initializer-list.
22370 initializer-list:
22371 initializer-clause ... [opt]
22372 initializer-list , initializer-clause ... [opt]
22374 C++2A Extension:
22376 designated-initializer-list:
22377 designated-initializer-clause
22378 designated-initializer-list , designated-initializer-clause
22380 designated-initializer-clause:
22381 designator brace-or-equal-initializer
22383 designator:
22384 . identifier
22386 GNU Extension:
22388 initializer-list:
22389 designation initializer-clause ...[opt]
22390 initializer-list , designation initializer-clause ...[opt]
22392 designation:
22393 . identifier =
22394 identifier :
22395 [ constant-expression ] =
22397 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22398 for the initializer. If the INDEX of the elt is non-NULL, it is the
22399 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22400 as for cp_parser_initializer. */
22402 static vec<constructor_elt, va_gc> *
22403 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22405 vec<constructor_elt, va_gc> *v = NULL;
22406 bool first_p = true;
22407 tree first_designator = NULL_TREE;
22409 /* Assume all of the expressions are constant. */
22410 *non_constant_p = false;
22412 /* Parse the rest of the list. */
22413 while (true)
22415 cp_token *token;
22416 tree designator;
22417 tree initializer;
22418 bool clause_non_constant_p;
22419 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22421 /* Handle the C++2A syntax, '. id ='. */
22422 if ((cxx_dialect >= cxx2a
22423 || cp_parser_allow_gnu_extensions_p (parser))
22424 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22425 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22426 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22427 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22428 == CPP_OPEN_BRACE)))
22430 if (cxx_dialect < cxx2a)
22431 pedwarn (loc, OPT_Wpedantic,
22432 "C++ designated initializers only available with "
22433 "-std=c++2a or -std=gnu++2a");
22434 /* Consume the `.'. */
22435 cp_lexer_consume_token (parser->lexer);
22436 /* Consume the identifier. */
22437 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22438 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22439 /* Consume the `='. */
22440 cp_lexer_consume_token (parser->lexer);
22442 /* Also, if the next token is an identifier and the following one is a
22443 colon, we are looking at the GNU designated-initializer
22444 syntax. */
22445 else if (cp_parser_allow_gnu_extensions_p (parser)
22446 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22447 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22448 == CPP_COLON))
22450 /* Warn the user that they are using an extension. */
22451 pedwarn (loc, OPT_Wpedantic,
22452 "ISO C++ does not allow GNU designated initializers");
22453 /* Consume the identifier. */
22454 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22455 /* Consume the `:'. */
22456 cp_lexer_consume_token (parser->lexer);
22458 /* Also handle C99 array designators, '[ const ] ='. */
22459 else if (cp_parser_allow_gnu_extensions_p (parser)
22460 && !c_dialect_objc ()
22461 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22463 /* In C++11, [ could start a lambda-introducer. */
22464 bool non_const = false;
22466 cp_parser_parse_tentatively (parser);
22468 if (!cp_parser_array_designator_p (parser))
22470 cp_parser_simulate_error (parser);
22471 designator = NULL_TREE;
22473 else
22475 designator = cp_parser_constant_expression (parser, true,
22476 &non_const);
22477 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22478 cp_parser_require (parser, CPP_EQ, RT_EQ);
22481 if (!cp_parser_parse_definitely (parser))
22482 designator = NULL_TREE;
22483 else if (non_const
22484 && (!require_potential_rvalue_constant_expression
22485 (designator)))
22486 designator = NULL_TREE;
22487 if (designator)
22488 /* Warn the user that they are using an extension. */
22489 pedwarn (loc, OPT_Wpedantic,
22490 "ISO C++ does not allow C99 designated initializers");
22492 else
22493 designator = NULL_TREE;
22495 if (first_p)
22497 first_designator = designator;
22498 first_p = false;
22500 else if (cxx_dialect >= cxx2a
22501 && first_designator != error_mark_node
22502 && (!first_designator != !designator))
22504 error_at (loc, "either all initializer clauses should be designated "
22505 "or none of them should be");
22506 first_designator = error_mark_node;
22508 else if (cxx_dialect < cxx2a && !first_designator)
22509 first_designator = designator;
22511 /* Parse the initializer. */
22512 initializer = cp_parser_initializer_clause (parser,
22513 &clause_non_constant_p);
22514 /* If any clause is non-constant, so is the entire initializer. */
22515 if (clause_non_constant_p)
22516 *non_constant_p = true;
22518 /* If we have an ellipsis, this is an initializer pack
22519 expansion. */
22520 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22522 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22524 /* Consume the `...'. */
22525 cp_lexer_consume_token (parser->lexer);
22527 if (designator && cxx_dialect >= cxx2a)
22528 error_at (loc,
22529 "%<...%> not allowed in designated initializer list");
22531 /* Turn the initializer into an initializer expansion. */
22532 initializer = make_pack_expansion (initializer);
22535 /* Add it to the vector. */
22536 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22538 /* If the next token is not a comma, we have reached the end of
22539 the list. */
22540 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22541 break;
22543 /* Peek at the next token. */
22544 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22545 /* If the next token is a `}', then we're still done. An
22546 initializer-clause can have a trailing `,' after the
22547 initializer-list and before the closing `}'. */
22548 if (token->type == CPP_CLOSE_BRACE)
22549 break;
22551 /* Consume the `,' token. */
22552 cp_lexer_consume_token (parser->lexer);
22555 /* The same identifier shall not appear in multiple designators
22556 of a designated-initializer-list. */
22557 if (first_designator)
22559 unsigned int i;
22560 tree designator, val;
22561 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22562 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22564 if (IDENTIFIER_MARKED (designator))
22566 error_at (cp_expr_loc_or_loc (val, input_location),
22567 "%<.%s%> designator used multiple times in "
22568 "the same initializer list",
22569 IDENTIFIER_POINTER (designator));
22570 (*v)[i].index = error_mark_node;
22572 else
22573 IDENTIFIER_MARKED (designator) = 1;
22575 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22576 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22577 IDENTIFIER_MARKED (designator) = 0;
22580 return v;
22583 /* Classes [gram.class] */
22585 /* Parse a class-name.
22587 class-name:
22588 identifier
22589 template-id
22591 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22592 to indicate that names looked up in dependent types should be
22593 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22594 keyword has been used to indicate that the name that appears next
22595 is a template. TAG_TYPE indicates the explicit tag given before
22596 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22597 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22598 is the class being defined in a class-head. If ENUM_OK is TRUE,
22599 enum-names are also accepted.
22601 Returns the TYPE_DECL representing the class. */
22603 static tree
22604 cp_parser_class_name (cp_parser *parser,
22605 bool typename_keyword_p,
22606 bool template_keyword_p,
22607 enum tag_types tag_type,
22608 bool check_dependency_p,
22609 bool class_head_p,
22610 bool is_declaration,
22611 bool enum_ok)
22613 tree decl;
22614 tree scope;
22615 bool typename_p;
22616 cp_token *token;
22617 tree identifier = NULL_TREE;
22619 /* All class-names start with an identifier. */
22620 token = cp_lexer_peek_token (parser->lexer);
22621 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22623 cp_parser_error (parser, "expected class-name");
22624 return error_mark_node;
22627 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22628 to a template-id, so we save it here. */
22629 scope = parser->scope;
22630 if (scope == error_mark_node)
22631 return error_mark_node;
22633 /* Any name names a type if we're following the `typename' keyword
22634 in a qualified name where the enclosing scope is type-dependent. */
22635 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22636 && dependent_type_p (scope));
22637 /* Handle the common case (an identifier, but not a template-id)
22638 efficiently. */
22639 if (token->type == CPP_NAME
22640 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22642 cp_token *identifier_token;
22643 bool ambiguous_p;
22645 /* Look for the identifier. */
22646 identifier_token = cp_lexer_peek_token (parser->lexer);
22647 ambiguous_p = identifier_token->error_reported;
22648 identifier = cp_parser_identifier (parser);
22649 /* If the next token isn't an identifier, we are certainly not
22650 looking at a class-name. */
22651 if (identifier == error_mark_node)
22652 decl = error_mark_node;
22653 /* If we know this is a type-name, there's no need to look it
22654 up. */
22655 else if (typename_p)
22656 decl = identifier;
22657 else
22659 tree ambiguous_decls;
22660 /* If we already know that this lookup is ambiguous, then
22661 we've already issued an error message; there's no reason
22662 to check again. */
22663 if (ambiguous_p)
22665 cp_parser_simulate_error (parser);
22666 return error_mark_node;
22668 /* If the next token is a `::', then the name must be a type
22669 name.
22671 [basic.lookup.qual]
22673 During the lookup for a name preceding the :: scope
22674 resolution operator, object, function, and enumerator
22675 names are ignored. */
22676 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22677 tag_type = scope_type;
22678 /* Look up the name. */
22679 decl = cp_parser_lookup_name (parser, identifier,
22680 tag_type,
22681 /*is_template=*/false,
22682 /*is_namespace=*/false,
22683 check_dependency_p,
22684 &ambiguous_decls,
22685 identifier_token->location);
22686 if (ambiguous_decls)
22688 if (cp_parser_parsing_tentatively (parser))
22689 cp_parser_simulate_error (parser);
22690 return error_mark_node;
22694 else
22696 /* Try a template-id. */
22697 decl = cp_parser_template_id (parser, template_keyword_p,
22698 check_dependency_p,
22699 tag_type,
22700 is_declaration);
22701 if (decl == error_mark_node)
22702 return error_mark_node;
22705 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22707 /* If this is a typename, create a TYPENAME_TYPE. */
22708 if (typename_p && decl != error_mark_node)
22710 decl = make_typename_type (scope, decl, typename_type,
22711 /*complain=*/tf_error);
22712 if (decl != error_mark_node)
22713 decl = TYPE_NAME (decl);
22716 decl = strip_using_decl (decl);
22718 /* Check to see that it is really the name of a class. */
22719 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22720 && identifier_p (TREE_OPERAND (decl, 0))
22721 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22722 /* Situations like this:
22724 template <typename T> struct A {
22725 typename T::template X<int>::I i;
22728 are problematic. Is `T::template X<int>' a class-name? The
22729 standard does not seem to be definitive, but there is no other
22730 valid interpretation of the following `::'. Therefore, those
22731 names are considered class-names. */
22733 decl = make_typename_type (scope, decl, tag_type, tf_error);
22734 if (decl != error_mark_node)
22735 decl = TYPE_NAME (decl);
22737 else if (TREE_CODE (decl) != TYPE_DECL
22738 || TREE_TYPE (decl) == error_mark_node
22739 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22740 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22741 /* In Objective-C 2.0, a classname followed by '.' starts a
22742 dot-syntax expression, and it's not a type-name. */
22743 || (c_dialect_objc ()
22744 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22745 && objc_is_class_name (decl)))
22746 decl = error_mark_node;
22748 if (decl == error_mark_node)
22749 cp_parser_error (parser, "expected class-name");
22750 else if (identifier && !parser->scope)
22751 maybe_note_name_used_in_class (identifier, decl);
22753 return decl;
22756 /* Parse a class-specifier.
22758 class-specifier:
22759 class-head { member-specification [opt] }
22761 Returns the TREE_TYPE representing the class. */
22763 static tree
22764 cp_parser_class_specifier_1 (cp_parser* parser)
22766 tree type;
22767 tree attributes = NULL_TREE;
22768 bool nested_name_specifier_p;
22769 unsigned saved_num_template_parameter_lists;
22770 bool saved_in_function_body;
22771 unsigned char in_statement;
22772 bool in_switch_statement_p;
22773 bool saved_in_unbraced_linkage_specification_p;
22774 tree old_scope = NULL_TREE;
22775 tree scope = NULL_TREE;
22776 cp_token *closing_brace;
22778 push_deferring_access_checks (dk_no_deferred);
22780 /* Parse the class-head. */
22781 type = cp_parser_class_head (parser,
22782 &nested_name_specifier_p);
22783 /* If the class-head was a semantic disaster, skip the entire body
22784 of the class. */
22785 if (!type)
22787 cp_parser_skip_to_end_of_block_or_statement (parser);
22788 pop_deferring_access_checks ();
22789 return error_mark_node;
22792 /* Look for the `{'. */
22793 matching_braces braces;
22794 if (!braces.require_open (parser))
22796 pop_deferring_access_checks ();
22797 return error_mark_node;
22800 cp_ensure_no_omp_declare_simd (parser);
22801 cp_ensure_no_oacc_routine (parser);
22803 /* Issue an error message if type-definitions are forbidden here. */
22804 cp_parser_check_type_definition (parser);
22805 /* Remember that we are defining one more class. */
22806 ++parser->num_classes_being_defined;
22807 /* Inside the class, surrounding template-parameter-lists do not
22808 apply. */
22809 saved_num_template_parameter_lists
22810 = parser->num_template_parameter_lists;
22811 parser->num_template_parameter_lists = 0;
22812 /* We are not in a function body. */
22813 saved_in_function_body = parser->in_function_body;
22814 parser->in_function_body = false;
22815 /* Or in a loop. */
22816 in_statement = parser->in_statement;
22817 parser->in_statement = 0;
22818 /* Or in a switch. */
22819 in_switch_statement_p = parser->in_switch_statement_p;
22820 parser->in_switch_statement_p = false;
22821 /* We are not immediately inside an extern "lang" block. */
22822 saved_in_unbraced_linkage_specification_p
22823 = parser->in_unbraced_linkage_specification_p;
22824 parser->in_unbraced_linkage_specification_p = false;
22826 // Associate constraints with the type.
22827 if (flag_concepts)
22828 type = associate_classtype_constraints (type);
22830 /* Start the class. */
22831 if (nested_name_specifier_p)
22833 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22834 old_scope = push_inner_scope (scope);
22836 type = begin_class_definition (type);
22838 if (type == error_mark_node)
22839 /* If the type is erroneous, skip the entire body of the class. */
22840 cp_parser_skip_to_closing_brace (parser);
22841 else
22842 /* Parse the member-specification. */
22843 cp_parser_member_specification_opt (parser);
22845 /* Look for the trailing `}'. */
22846 closing_brace = braces.require_close (parser);
22847 /* Look for trailing attributes to apply to this class. */
22848 if (cp_parser_allow_gnu_extensions_p (parser))
22849 attributes = cp_parser_gnu_attributes_opt (parser);
22850 if (type != error_mark_node)
22851 type = finish_struct (type, attributes);
22852 if (nested_name_specifier_p)
22853 pop_inner_scope (old_scope, scope);
22855 /* We've finished a type definition. Check for the common syntax
22856 error of forgetting a semicolon after the definition. We need to
22857 be careful, as we can't just check for not-a-semicolon and be done
22858 with it; the user might have typed:
22860 class X { } c = ...;
22861 class X { } *p = ...;
22863 and so forth. Instead, enumerate all the possible tokens that
22864 might follow this production; if we don't see one of them, then
22865 complain and silently insert the semicolon. */
22867 cp_token *token = cp_lexer_peek_token (parser->lexer);
22868 bool want_semicolon = true;
22870 if (cp_next_tokens_can_be_std_attribute_p (parser))
22871 /* Don't try to parse c++11 attributes here. As per the
22872 grammar, that should be a task for
22873 cp_parser_decl_specifier_seq. */
22874 want_semicolon = false;
22876 switch (token->type)
22878 case CPP_NAME:
22879 case CPP_SEMICOLON:
22880 case CPP_MULT:
22881 case CPP_AND:
22882 case CPP_OPEN_PAREN:
22883 case CPP_CLOSE_PAREN:
22884 case CPP_COMMA:
22885 want_semicolon = false;
22886 break;
22888 /* While it's legal for type qualifiers and storage class
22889 specifiers to follow type definitions in the grammar, only
22890 compiler testsuites contain code like that. Assume that if
22891 we see such code, then what we're really seeing is a case
22892 like:
22894 class X { }
22895 const <type> var = ...;
22899 class Y { }
22900 static <type> func (...) ...
22902 i.e. the qualifier or specifier applies to the next
22903 declaration. To do so, however, we need to look ahead one
22904 more token to see if *that* token is a type specifier.
22906 This code could be improved to handle:
22908 class Z { }
22909 static const <type> var = ...; */
22910 case CPP_KEYWORD:
22911 if (keyword_is_decl_specifier (token->keyword))
22913 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22915 /* Handling user-defined types here would be nice, but very
22916 tricky. */
22917 want_semicolon
22918 = (lookahead->type == CPP_KEYWORD
22919 && keyword_begins_type_specifier (lookahead->keyword));
22921 break;
22922 default:
22923 break;
22926 /* If we don't have a type, then something is very wrong and we
22927 shouldn't try to do anything clever. Likewise for not seeing the
22928 closing brace. */
22929 if (closing_brace && TYPE_P (type) && want_semicolon)
22931 /* Locate the closing brace. */
22932 cp_token_position prev
22933 = cp_lexer_previous_token_position (parser->lexer);
22934 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22935 location_t loc = prev_token->location;
22937 /* We want to suggest insertion of a ';' immediately *after* the
22938 closing brace, so, if we can, offset the location by 1 column. */
22939 location_t next_loc = loc;
22940 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22941 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22943 rich_location richloc (line_table, next_loc);
22945 /* If we successfully offset the location, suggest the fix-it. */
22946 if (next_loc != loc)
22947 richloc.add_fixit_insert_before (next_loc, ";");
22949 if (CLASSTYPE_DECLARED_CLASS (type))
22950 error_at (&richloc,
22951 "expected %<;%> after class definition");
22952 else if (TREE_CODE (type) == RECORD_TYPE)
22953 error_at (&richloc,
22954 "expected %<;%> after struct definition");
22955 else if (TREE_CODE (type) == UNION_TYPE)
22956 error_at (&richloc,
22957 "expected %<;%> after union definition");
22958 else
22959 gcc_unreachable ();
22961 /* Unget one token and smash it to look as though we encountered
22962 a semicolon in the input stream. */
22963 cp_lexer_set_token_position (parser->lexer, prev);
22964 token = cp_lexer_peek_token (parser->lexer);
22965 token->type = CPP_SEMICOLON;
22966 token->keyword = RID_MAX;
22970 /* If this class is not itself within the scope of another class,
22971 then we need to parse the bodies of all of the queued function
22972 definitions. Note that the queued functions defined in a class
22973 are not always processed immediately following the
22974 class-specifier for that class. Consider:
22976 struct A {
22977 struct B { void f() { sizeof (A); } };
22980 If `f' were processed before the processing of `A' were
22981 completed, there would be no way to compute the size of `A'.
22982 Note that the nesting we are interested in here is lexical --
22983 not the semantic nesting given by TYPE_CONTEXT. In particular,
22984 for:
22986 struct A { struct B; };
22987 struct A::B { void f() { } };
22989 there is no need to delay the parsing of `A::B::f'. */
22990 if (--parser->num_classes_being_defined == 0)
22992 tree decl;
22993 tree class_type = NULL_TREE;
22994 tree pushed_scope = NULL_TREE;
22995 unsigned ix;
22996 cp_default_arg_entry *e;
22997 tree save_ccp, save_ccr;
22999 if (any_erroneous_template_args_p (type))
23001 /* Skip default arguments, NSDMIs, etc, in order to improve
23002 error recovery (c++/71169, c++/71832). */
23003 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23004 vec_safe_truncate (unparsed_nsdmis, 0);
23005 vec_safe_truncate (unparsed_classes, 0);
23006 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23009 /* In a first pass, parse default arguments to the functions.
23010 Then, in a second pass, parse the bodies of the functions.
23011 This two-phased approach handles cases like:
23013 struct S {
23014 void f() { g(); }
23015 void g(int i = 3);
23019 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23021 decl = e->decl;
23022 /* If there are default arguments that have not yet been processed,
23023 take care of them now. */
23024 if (class_type != e->class_type)
23026 if (pushed_scope)
23027 pop_scope (pushed_scope);
23028 class_type = e->class_type;
23029 pushed_scope = push_scope (class_type);
23031 /* Make sure that any template parameters are in scope. */
23032 maybe_begin_member_template_processing (decl);
23033 /* Parse the default argument expressions. */
23034 cp_parser_late_parsing_default_args (parser, decl);
23035 /* Remove any template parameters from the symbol table. */
23036 maybe_end_member_template_processing ();
23038 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23039 /* Now parse any NSDMIs. */
23040 save_ccp = current_class_ptr;
23041 save_ccr = current_class_ref;
23042 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23044 if (class_type != DECL_CONTEXT (decl))
23046 if (pushed_scope)
23047 pop_scope (pushed_scope);
23048 class_type = DECL_CONTEXT (decl);
23049 pushed_scope = push_scope (class_type);
23051 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23052 cp_parser_late_parsing_nsdmi (parser, decl);
23054 vec_safe_truncate (unparsed_nsdmis, 0);
23055 current_class_ptr = save_ccp;
23056 current_class_ref = save_ccr;
23057 if (pushed_scope)
23058 pop_scope (pushed_scope);
23060 /* Now do some post-NSDMI bookkeeping. */
23061 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23062 after_nsdmi_defaulted_late_checks (class_type);
23063 vec_safe_truncate (unparsed_classes, 0);
23064 after_nsdmi_defaulted_late_checks (type);
23066 /* Now parse the body of the functions. */
23067 if (flag_openmp)
23069 /* OpenMP UDRs need to be parsed before all other functions. */
23070 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23071 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23072 cp_parser_late_parsing_for_member (parser, decl);
23073 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23074 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23075 cp_parser_late_parsing_for_member (parser, decl);
23077 else
23078 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23079 cp_parser_late_parsing_for_member (parser, decl);
23080 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23082 else
23083 vec_safe_push (unparsed_classes, type);
23085 /* Put back any saved access checks. */
23086 pop_deferring_access_checks ();
23088 /* Restore saved state. */
23089 parser->in_switch_statement_p = in_switch_statement_p;
23090 parser->in_statement = in_statement;
23091 parser->in_function_body = saved_in_function_body;
23092 parser->num_template_parameter_lists
23093 = saved_num_template_parameter_lists;
23094 parser->in_unbraced_linkage_specification_p
23095 = saved_in_unbraced_linkage_specification_p;
23097 return type;
23100 static tree
23101 cp_parser_class_specifier (cp_parser* parser)
23103 tree ret;
23104 timevar_push (TV_PARSE_STRUCT);
23105 ret = cp_parser_class_specifier_1 (parser);
23106 timevar_pop (TV_PARSE_STRUCT);
23107 return ret;
23110 /* Parse a class-head.
23112 class-head:
23113 class-key identifier [opt] base-clause [opt]
23114 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23115 class-key nested-name-specifier [opt] template-id
23116 base-clause [opt]
23118 class-virt-specifier:
23119 final
23121 GNU Extensions:
23122 class-key attributes identifier [opt] base-clause [opt]
23123 class-key attributes nested-name-specifier identifier base-clause [opt]
23124 class-key attributes nested-name-specifier [opt] template-id
23125 base-clause [opt]
23127 Upon return BASES is initialized to the list of base classes (or
23128 NULL, if there are none) in the same form returned by
23129 cp_parser_base_clause.
23131 Returns the TYPE of the indicated class. Sets
23132 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23133 involving a nested-name-specifier was used, and FALSE otherwise.
23135 Returns error_mark_node if this is not a class-head.
23137 Returns NULL_TREE if the class-head is syntactically valid, but
23138 semantically invalid in a way that means we should skip the entire
23139 body of the class. */
23141 static tree
23142 cp_parser_class_head (cp_parser* parser,
23143 bool* nested_name_specifier_p)
23145 tree nested_name_specifier;
23146 enum tag_types class_key;
23147 tree id = NULL_TREE;
23148 tree type = NULL_TREE;
23149 tree attributes;
23150 tree bases;
23151 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23152 bool template_id_p = false;
23153 bool qualified_p = false;
23154 bool invalid_nested_name_p = false;
23155 bool invalid_explicit_specialization_p = false;
23156 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23157 tree pushed_scope = NULL_TREE;
23158 unsigned num_templates;
23159 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23160 /* Assume no nested-name-specifier will be present. */
23161 *nested_name_specifier_p = false;
23162 /* Assume no template parameter lists will be used in defining the
23163 type. */
23164 num_templates = 0;
23165 parser->colon_corrects_to_scope_p = false;
23167 /* Look for the class-key. */
23168 class_key = cp_parser_class_key (parser);
23169 if (class_key == none_type)
23170 return error_mark_node;
23172 location_t class_head_start_location = input_location;
23174 /* Parse the attributes. */
23175 attributes = cp_parser_attributes_opt (parser);
23177 /* If the next token is `::', that is invalid -- but sometimes
23178 people do try to write:
23180 struct ::S {};
23182 Handle this gracefully by accepting the extra qualifier, and then
23183 issuing an error about it later if this really is a
23184 class-head. If it turns out just to be an elaborated type
23185 specifier, remain silent. */
23186 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23187 qualified_p = true;
23189 push_deferring_access_checks (dk_no_check);
23191 /* Determine the name of the class. Begin by looking for an
23192 optional nested-name-specifier. */
23193 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23194 nested_name_specifier
23195 = cp_parser_nested_name_specifier_opt (parser,
23196 /*typename_keyword_p=*/false,
23197 /*check_dependency_p=*/false,
23198 /*type_p=*/true,
23199 /*is_declaration=*/false);
23200 /* If there was a nested-name-specifier, then there *must* be an
23201 identifier. */
23203 cp_token *bad_template_keyword = NULL;
23205 if (nested_name_specifier)
23207 type_start_token = cp_lexer_peek_token (parser->lexer);
23208 /* Although the grammar says `identifier', it really means
23209 `class-name' or `template-name'. You are only allowed to
23210 define a class that has already been declared with this
23211 syntax.
23213 The proposed resolution for Core Issue 180 says that wherever
23214 you see `class T::X' you should treat `X' as a type-name.
23216 It is OK to define an inaccessible class; for example:
23218 class A { class B; };
23219 class A::B {};
23221 We do not know if we will see a class-name, or a
23222 template-name. We look for a class-name first, in case the
23223 class-name is a template-id; if we looked for the
23224 template-name first we would stop after the template-name. */
23225 cp_parser_parse_tentatively (parser);
23226 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23227 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23228 type = cp_parser_class_name (parser,
23229 /*typename_keyword_p=*/false,
23230 /*template_keyword_p=*/false,
23231 class_type,
23232 /*check_dependency_p=*/false,
23233 /*class_head_p=*/true,
23234 /*is_declaration=*/false);
23235 /* If that didn't work, ignore the nested-name-specifier. */
23236 if (!cp_parser_parse_definitely (parser))
23238 invalid_nested_name_p = true;
23239 type_start_token = cp_lexer_peek_token (parser->lexer);
23240 id = cp_parser_identifier (parser);
23241 if (id == error_mark_node)
23242 id = NULL_TREE;
23244 /* If we could not find a corresponding TYPE, treat this
23245 declaration like an unqualified declaration. */
23246 if (type == error_mark_node)
23247 nested_name_specifier = NULL_TREE;
23248 /* Otherwise, count the number of templates used in TYPE and its
23249 containing scopes. */
23250 else
23251 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23253 /* Otherwise, the identifier is optional. */
23254 else
23256 /* We don't know whether what comes next is a template-id,
23257 an identifier, or nothing at all. */
23258 cp_parser_parse_tentatively (parser);
23259 /* Check for a template-id. */
23260 type_start_token = cp_lexer_peek_token (parser->lexer);
23261 id = cp_parser_template_id (parser,
23262 /*template_keyword_p=*/false,
23263 /*check_dependency_p=*/true,
23264 class_key,
23265 /*is_declaration=*/true);
23266 /* If that didn't work, it could still be an identifier. */
23267 if (!cp_parser_parse_definitely (parser))
23269 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23271 type_start_token = cp_lexer_peek_token (parser->lexer);
23272 id = cp_parser_identifier (parser);
23274 else
23275 id = NULL_TREE;
23277 else
23279 template_id_p = true;
23280 ++num_templates;
23284 pop_deferring_access_checks ();
23286 if (id)
23288 cp_parser_check_for_invalid_template_id (parser, id,
23289 class_key,
23290 type_start_token->location);
23292 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23294 /* If it's not a `:' or a `{' then we can't really be looking at a
23295 class-head, since a class-head only appears as part of a
23296 class-specifier. We have to detect this situation before calling
23297 xref_tag, since that has irreversible side-effects. */
23298 if (!cp_parser_next_token_starts_class_definition_p (parser))
23300 cp_parser_error (parser, "expected %<{%> or %<:%>");
23301 type = error_mark_node;
23302 goto out;
23305 /* At this point, we're going ahead with the class-specifier, even
23306 if some other problem occurs. */
23307 cp_parser_commit_to_tentative_parse (parser);
23308 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23310 cp_parser_error (parser,
23311 "cannot specify %<override%> for a class");
23312 type = error_mark_node;
23313 goto out;
23315 /* Issue the error about the overly-qualified name now. */
23316 if (qualified_p)
23318 cp_parser_error (parser,
23319 "global qualification of class name is invalid");
23320 type = error_mark_node;
23321 goto out;
23323 else if (invalid_nested_name_p)
23325 cp_parser_error (parser,
23326 "qualified name does not name a class");
23327 type = error_mark_node;
23328 goto out;
23330 else if (nested_name_specifier)
23332 tree scope;
23334 if (bad_template_keyword)
23335 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23336 keyword template shall not appear at the top level. */
23337 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23338 "keyword %<template%> not allowed in class-head-name");
23340 /* Reject typedef-names in class heads. */
23341 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23343 error_at (type_start_token->location,
23344 "invalid class name in declaration of %qD",
23345 type);
23346 type = NULL_TREE;
23347 goto done;
23350 /* Figure out in what scope the declaration is being placed. */
23351 scope = current_scope ();
23352 /* If that scope does not contain the scope in which the
23353 class was originally declared, the program is invalid. */
23354 if (scope && !is_ancestor (scope, nested_name_specifier))
23356 if (at_namespace_scope_p ())
23357 error_at (type_start_token->location,
23358 "declaration of %qD in namespace %qD which does not "
23359 "enclose %qD",
23360 type, scope, nested_name_specifier);
23361 else
23362 error_at (type_start_token->location,
23363 "declaration of %qD in %qD which does not enclose %qD",
23364 type, scope, nested_name_specifier);
23365 type = NULL_TREE;
23366 goto done;
23368 /* [dcl.meaning]
23370 A declarator-id shall not be qualified except for the
23371 definition of a ... nested class outside of its class
23372 ... [or] the definition or explicit instantiation of a
23373 class member of a namespace outside of its namespace. */
23374 if (scope == nested_name_specifier)
23376 permerror (nested_name_specifier_token_start->location,
23377 "extra qualification not allowed");
23378 nested_name_specifier = NULL_TREE;
23379 num_templates = 0;
23382 /* An explicit-specialization must be preceded by "template <>". If
23383 it is not, try to recover gracefully. */
23384 if (at_namespace_scope_p ()
23385 && parser->num_template_parameter_lists == 0
23386 && !processing_template_parmlist
23387 && template_id_p)
23389 /* Build a location of this form:
23390 struct typename <ARGS>
23391 ^~~~~~~~~~~~~~~~~~~~~~
23392 with caret==start at the start token, and
23393 finishing at the end of the type. */
23394 location_t reported_loc
23395 = make_location (class_head_start_location,
23396 class_head_start_location,
23397 get_finish (type_start_token->location));
23398 rich_location richloc (line_table, reported_loc);
23399 richloc.add_fixit_insert_before (class_head_start_location,
23400 "template <> ");
23401 error_at (&richloc,
23402 "an explicit specialization must be preceded by"
23403 " %<template <>%>");
23404 invalid_explicit_specialization_p = true;
23405 /* Take the same action that would have been taken by
23406 cp_parser_explicit_specialization. */
23407 ++parser->num_template_parameter_lists;
23408 begin_specialization ();
23410 /* There must be no "return" statements between this point and the
23411 end of this function; set "type "to the correct return value and
23412 use "goto done;" to return. */
23413 /* Make sure that the right number of template parameters were
23414 present. */
23415 if (!cp_parser_check_template_parameters (parser, num_templates,
23416 template_id_p,
23417 type_start_token->location,
23418 /*declarator=*/NULL))
23420 /* If something went wrong, there is no point in even trying to
23421 process the class-definition. */
23422 type = NULL_TREE;
23423 goto done;
23426 /* Look up the type. */
23427 if (template_id_p)
23429 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23430 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23431 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23433 error_at (type_start_token->location,
23434 "function template %qD redeclared as a class template", id);
23435 type = error_mark_node;
23437 else
23439 type = TREE_TYPE (id);
23440 type = maybe_process_partial_specialization (type);
23442 /* Check the scope while we still know whether or not we had a
23443 nested-name-specifier. */
23444 if (type != error_mark_node)
23445 check_unqualified_spec_or_inst (type, type_start_token->location);
23447 if (nested_name_specifier)
23448 pushed_scope = push_scope (nested_name_specifier);
23450 else if (nested_name_specifier)
23452 tree class_type;
23454 /* Given:
23456 template <typename T> struct S { struct T };
23457 template <typename T> struct S<T>::T { };
23459 we will get a TYPENAME_TYPE when processing the definition of
23460 `S::T'. We need to resolve it to the actual type before we
23461 try to define it. */
23462 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23464 class_type = resolve_typename_type (TREE_TYPE (type),
23465 /*only_current_p=*/false);
23466 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23467 type = TYPE_NAME (class_type);
23468 else
23470 cp_parser_error (parser, "could not resolve typename type");
23471 type = error_mark_node;
23475 if (maybe_process_partial_specialization (TREE_TYPE (type))
23476 == error_mark_node)
23478 type = NULL_TREE;
23479 goto done;
23482 class_type = current_class_type;
23483 /* Enter the scope indicated by the nested-name-specifier. */
23484 pushed_scope = push_scope (nested_name_specifier);
23485 /* Get the canonical version of this type. */
23486 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23487 /* Call push_template_decl if it seems like we should be defining a
23488 template either from the template headers or the type we're
23489 defining, so that we diagnose both extra and missing headers. */
23490 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23491 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23492 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23494 type = push_template_decl (type);
23495 if (type == error_mark_node)
23497 type = NULL_TREE;
23498 goto done;
23502 type = TREE_TYPE (type);
23503 *nested_name_specifier_p = true;
23505 else /* The name is not a nested name. */
23507 /* If the class was unnamed, create a dummy name. */
23508 if (!id)
23509 id = make_anon_name ();
23510 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23511 ? ts_within_enclosing_non_class
23512 : ts_current);
23513 type = xref_tag (class_key, id, tag_scope,
23514 parser->num_template_parameter_lists);
23517 /* Indicate whether this class was declared as a `class' or as a
23518 `struct'. */
23519 if (TREE_CODE (type) == RECORD_TYPE)
23520 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23521 cp_parser_check_class_key (class_key, type);
23523 /* If this type was already complete, and we see another definition,
23524 that's an error. */
23525 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23527 error_at (type_start_token->location, "redefinition of %q#T",
23528 type);
23529 inform (location_of (type), "previous definition of %q#T",
23530 type);
23531 type = NULL_TREE;
23532 goto done;
23534 else if (type == error_mark_node)
23535 type = NULL_TREE;
23537 if (type)
23539 /* Apply attributes now, before any use of the class as a template
23540 argument in its base list. */
23541 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23542 fixup_attribute_variants (type);
23545 /* We will have entered the scope containing the class; the names of
23546 base classes should be looked up in that context. For example:
23548 struct A { struct B {}; struct C; };
23549 struct A::C : B {};
23551 is valid. */
23553 /* Get the list of base-classes, if there is one. */
23554 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23556 /* PR59482: enter the class scope so that base-specifiers are looked
23557 up correctly. */
23558 if (type)
23559 pushclass (type);
23560 bases = cp_parser_base_clause (parser);
23561 /* PR59482: get out of the previously pushed class scope so that the
23562 subsequent pops pop the right thing. */
23563 if (type)
23564 popclass ();
23566 else
23567 bases = NULL_TREE;
23569 /* If we're really defining a class, process the base classes.
23570 If they're invalid, fail. */
23571 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23572 xref_basetypes (type, bases);
23574 done:
23575 /* Leave the scope given by the nested-name-specifier. We will
23576 enter the class scope itself while processing the members. */
23577 if (pushed_scope)
23578 pop_scope (pushed_scope);
23580 if (invalid_explicit_specialization_p)
23582 end_specialization ();
23583 --parser->num_template_parameter_lists;
23586 if (type)
23587 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23588 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23589 CLASSTYPE_FINAL (type) = 1;
23590 out:
23591 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23592 return type;
23595 /* Parse a class-key.
23597 class-key:
23598 class
23599 struct
23600 union
23602 Returns the kind of class-key specified, or none_type to indicate
23603 error. */
23605 static enum tag_types
23606 cp_parser_class_key (cp_parser* parser)
23608 cp_token *token;
23609 enum tag_types tag_type;
23611 /* Look for the class-key. */
23612 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23613 if (!token)
23614 return none_type;
23616 /* Check to see if the TOKEN is a class-key. */
23617 tag_type = cp_parser_token_is_class_key (token);
23618 if (!tag_type)
23619 cp_parser_error (parser, "expected class-key");
23620 return tag_type;
23623 /* Parse a type-parameter-key.
23625 type-parameter-key:
23626 class
23627 typename
23630 static void
23631 cp_parser_type_parameter_key (cp_parser* parser)
23633 /* Look for the type-parameter-key. */
23634 enum tag_types tag_type = none_type;
23635 cp_token *token = cp_lexer_peek_token (parser->lexer);
23636 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23638 cp_lexer_consume_token (parser->lexer);
23639 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23640 /* typename is not allowed in a template template parameter
23641 by the standard until C++17. */
23642 pedwarn (token->location, OPT_Wpedantic,
23643 "ISO C++ forbids typename key in template template parameter;"
23644 " use -std=c++17 or -std=gnu++17");
23646 else
23647 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23649 return;
23652 /* Parse an (optional) member-specification.
23654 member-specification:
23655 member-declaration member-specification [opt]
23656 access-specifier : member-specification [opt] */
23658 static void
23659 cp_parser_member_specification_opt (cp_parser* parser)
23661 while (true)
23663 cp_token *token;
23664 enum rid keyword;
23666 /* Peek at the next token. */
23667 token = cp_lexer_peek_token (parser->lexer);
23668 /* If it's a `}', or EOF then we've seen all the members. */
23669 if (token->type == CPP_CLOSE_BRACE
23670 || token->type == CPP_EOF
23671 || token->type == CPP_PRAGMA_EOL)
23672 break;
23674 /* See if this token is a keyword. */
23675 keyword = token->keyword;
23676 switch (keyword)
23678 case RID_PUBLIC:
23679 case RID_PROTECTED:
23680 case RID_PRIVATE:
23681 /* Consume the access-specifier. */
23682 cp_lexer_consume_token (parser->lexer);
23683 /* Remember which access-specifier is active. */
23684 current_access_specifier = token->u.value;
23685 /* Look for the `:'. */
23686 cp_parser_require (parser, CPP_COLON, RT_COLON);
23687 break;
23689 default:
23690 /* Accept #pragmas at class scope. */
23691 if (token->type == CPP_PRAGMA)
23693 cp_parser_pragma (parser, pragma_member, NULL);
23694 break;
23697 /* Otherwise, the next construction must be a
23698 member-declaration. */
23699 cp_parser_member_declaration (parser);
23704 /* Parse a member-declaration.
23706 member-declaration:
23707 decl-specifier-seq [opt] member-declarator-list [opt] ;
23708 function-definition ; [opt]
23709 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23710 using-declaration
23711 template-declaration
23712 alias-declaration
23714 member-declarator-list:
23715 member-declarator
23716 member-declarator-list , member-declarator
23718 member-declarator:
23719 declarator pure-specifier [opt]
23720 declarator constant-initializer [opt]
23721 identifier [opt] : constant-expression
23723 GNU Extensions:
23725 member-declaration:
23726 __extension__ member-declaration
23728 member-declarator:
23729 declarator attributes [opt] pure-specifier [opt]
23730 declarator attributes [opt] constant-initializer [opt]
23731 identifier [opt] attributes [opt] : constant-expression
23733 C++0x Extensions:
23735 member-declaration:
23736 static_assert-declaration */
23738 static void
23739 cp_parser_member_declaration (cp_parser* parser)
23741 cp_decl_specifier_seq decl_specifiers;
23742 tree prefix_attributes;
23743 tree decl;
23744 int declares_class_or_enum;
23745 bool friend_p;
23746 cp_token *token = NULL;
23747 cp_token *decl_spec_token_start = NULL;
23748 cp_token *initializer_token_start = NULL;
23749 int saved_pedantic;
23750 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23752 /* Check for the `__extension__' keyword. */
23753 if (cp_parser_extension_opt (parser, &saved_pedantic))
23755 /* Recurse. */
23756 cp_parser_member_declaration (parser);
23757 /* Restore the old value of the PEDANTIC flag. */
23758 pedantic = saved_pedantic;
23760 return;
23763 /* Check for a template-declaration. */
23764 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23766 /* An explicit specialization here is an error condition, and we
23767 expect the specialization handler to detect and report this. */
23768 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23769 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23770 cp_parser_explicit_specialization (parser);
23771 else
23772 cp_parser_template_declaration (parser, /*member_p=*/true);
23774 return;
23776 /* Check for a template introduction. */
23777 else if (cp_parser_template_declaration_after_export (parser, true))
23778 return;
23780 /* Check for a using-declaration. */
23781 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23783 if (cxx_dialect < cxx11)
23785 /* Parse the using-declaration. */
23786 cp_parser_using_declaration (parser,
23787 /*access_declaration_p=*/false);
23788 return;
23790 else
23792 tree decl;
23793 bool alias_decl_expected;
23794 cp_parser_parse_tentatively (parser);
23795 decl = cp_parser_alias_declaration (parser);
23796 /* Note that if we actually see the '=' token after the
23797 identifier, cp_parser_alias_declaration commits the
23798 tentative parse. In that case, we really expect an
23799 alias-declaration. Otherwise, we expect a using
23800 declaration. */
23801 alias_decl_expected =
23802 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23803 cp_parser_parse_definitely (parser);
23805 if (alias_decl_expected)
23806 finish_member_declaration (decl);
23807 else
23808 cp_parser_using_declaration (parser,
23809 /*access_declaration_p=*/false);
23810 return;
23814 /* Check for @defs. */
23815 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23817 tree ivar, member;
23818 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23819 ivar = ivar_chains;
23820 while (ivar)
23822 member = ivar;
23823 ivar = TREE_CHAIN (member);
23824 TREE_CHAIN (member) = NULL_TREE;
23825 finish_member_declaration (member);
23827 return;
23830 /* If the next token is `static_assert' we have a static assertion. */
23831 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23833 cp_parser_static_assert (parser, /*member_p=*/true);
23834 return;
23837 parser->colon_corrects_to_scope_p = false;
23839 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23840 goto out;
23842 /* Parse the decl-specifier-seq. */
23843 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23844 cp_parser_decl_specifier_seq (parser,
23845 CP_PARSER_FLAGS_OPTIONAL,
23846 &decl_specifiers,
23847 &declares_class_or_enum);
23848 /* Check for an invalid type-name. */
23849 if (!decl_specifiers.any_type_specifiers_p
23850 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23851 goto out;
23852 /* If there is no declarator, then the decl-specifier-seq should
23853 specify a type. */
23854 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23856 /* If there was no decl-specifier-seq, and the next token is a
23857 `;', then we have something like:
23859 struct S { ; };
23861 [class.mem]
23863 Each member-declaration shall declare at least one member
23864 name of the class. */
23865 if (!decl_specifiers.any_specifiers_p)
23867 cp_token *token = cp_lexer_peek_token (parser->lexer);
23868 if (!in_system_header_at (token->location))
23870 gcc_rich_location richloc (token->location);
23871 richloc.add_fixit_remove ();
23872 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23875 else
23877 tree type;
23879 /* See if this declaration is a friend. */
23880 friend_p = cp_parser_friend_p (&decl_specifiers);
23881 /* If there were decl-specifiers, check to see if there was
23882 a class-declaration. */
23883 type = check_tag_decl (&decl_specifiers,
23884 /*explicit_type_instantiation_p=*/false);
23885 /* Nested classes have already been added to the class, but
23886 a `friend' needs to be explicitly registered. */
23887 if (friend_p)
23889 /* If the `friend' keyword was present, the friend must
23890 be introduced with a class-key. */
23891 if (!declares_class_or_enum && cxx_dialect < cxx11)
23892 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23893 "in C++03 a class-key must be used "
23894 "when declaring a friend");
23895 /* In this case:
23897 template <typename T> struct A {
23898 friend struct A<T>::B;
23901 A<T>::B will be represented by a TYPENAME_TYPE, and
23902 therefore not recognized by check_tag_decl. */
23903 if (!type)
23905 type = decl_specifiers.type;
23906 if (type && TREE_CODE (type) == TYPE_DECL)
23907 type = TREE_TYPE (type);
23909 if (!type || !TYPE_P (type))
23910 error_at (decl_spec_token_start->location,
23911 "friend declaration does not name a class or "
23912 "function");
23913 else
23914 make_friend_class (current_class_type, type,
23915 /*complain=*/true);
23917 /* If there is no TYPE, an error message will already have
23918 been issued. */
23919 else if (!type || type == error_mark_node)
23921 /* An anonymous aggregate has to be handled specially; such
23922 a declaration really declares a data member (with a
23923 particular type), as opposed to a nested class. */
23924 else if (ANON_AGGR_TYPE_P (type))
23926 /* C++11 9.5/6. */
23927 if (decl_specifiers.storage_class != sc_none)
23928 error_at (decl_spec_token_start->location,
23929 "a storage class on an anonymous aggregate "
23930 "in class scope is not allowed");
23932 /* Remove constructors and such from TYPE, now that we
23933 know it is an anonymous aggregate. */
23934 fixup_anonymous_aggr (type);
23935 /* And make the corresponding data member. */
23936 decl = build_decl (decl_spec_token_start->location,
23937 FIELD_DECL, NULL_TREE, type);
23938 /* Add it to the class. */
23939 finish_member_declaration (decl);
23941 else
23942 cp_parser_check_access_in_redeclaration
23943 (TYPE_NAME (type),
23944 decl_spec_token_start->location);
23947 else
23949 bool assume_semicolon = false;
23951 /* Clear attributes from the decl_specifiers but keep them
23952 around as prefix attributes that apply them to the entity
23953 being declared. */
23954 prefix_attributes = decl_specifiers.attributes;
23955 decl_specifiers.attributes = NULL_TREE;
23957 /* See if these declarations will be friends. */
23958 friend_p = cp_parser_friend_p (&decl_specifiers);
23960 /* Keep going until we hit the `;' at the end of the
23961 declaration. */
23962 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23964 tree attributes = NULL_TREE;
23965 tree first_attribute;
23966 tree initializer;
23967 bool named_bitfld = false;
23969 /* Peek at the next token. */
23970 token = cp_lexer_peek_token (parser->lexer);
23972 /* The following code wants to know early if it is a bit-field
23973 or some other declaration. Attributes can appear before
23974 the `:' token. Skip over them without consuming any tokens
23975 to peek if they are followed by `:'. */
23976 if (cp_next_tokens_can_be_attribute_p (parser)
23977 || (token->type == CPP_NAME
23978 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23979 && (named_bitfld = true)))
23981 size_t n
23982 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23983 token = cp_lexer_peek_nth_token (parser->lexer, n);
23986 /* Check for a bitfield declaration. */
23987 if (token->type == CPP_COLON
23988 || (token->type == CPP_NAME
23989 && token == cp_lexer_peek_token (parser->lexer)
23990 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23991 && (named_bitfld = true)))
23993 tree identifier;
23994 tree width;
23995 tree late_attributes = NULL_TREE;
23997 if (named_bitfld)
23998 identifier = cp_parser_identifier (parser);
23999 else
24000 identifier = NULL_TREE;
24002 /* Look for attributes that apply to the bitfield. */
24003 attributes = cp_parser_attributes_opt (parser);
24005 /* Consume the `:' token. */
24006 cp_lexer_consume_token (parser->lexer);
24008 /* Get the width of the bitfield. */
24009 width = cp_parser_constant_expression (parser, false, NULL,
24010 cxx_dialect >= cxx11);
24012 /* In C++2A and as extension for C++11 and above we allow
24013 default member initializers for bit-fields. */
24014 initializer = NULL_TREE;
24015 if (cxx_dialect >= cxx11
24016 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24017 || cp_lexer_next_token_is (parser->lexer,
24018 CPP_OPEN_BRACE)))
24020 location_t loc
24021 = cp_lexer_peek_token (parser->lexer)->location;
24022 if (cxx_dialect < cxx2a
24023 && !in_system_header_at (loc)
24024 && identifier != NULL_TREE)
24025 pedwarn (loc, 0,
24026 "default member initializers for bit-fields "
24027 "only available with -std=c++2a or "
24028 "-std=gnu++2a");
24030 initializer = cp_parser_save_nsdmi (parser);
24031 if (identifier == NULL_TREE)
24033 error_at (loc, "default member initializer for "
24034 "unnamed bit-field");
24035 initializer = NULL_TREE;
24038 else
24040 /* Look for attributes that apply to the bitfield after
24041 the `:' token and width. This is where GCC used to
24042 parse attributes in the past, pedwarn if there is
24043 a std attribute. */
24044 if (cp_next_tokens_can_be_std_attribute_p (parser))
24045 pedwarn (input_location, OPT_Wpedantic,
24046 "ISO C++ allows bit-field attributes only "
24047 "before the %<:%> token");
24049 late_attributes = cp_parser_attributes_opt (parser);
24052 attributes = attr_chainon (attributes, late_attributes);
24054 /* Remember which attributes are prefix attributes and
24055 which are not. */
24056 first_attribute = attributes;
24057 /* Combine the attributes. */
24058 attributes = attr_chainon (prefix_attributes, attributes);
24060 /* Create the bitfield declaration. */
24061 decl = grokbitfield (identifier
24062 ? make_id_declarator (NULL_TREE,
24063 identifier,
24064 sfk_none)
24065 : NULL,
24066 &decl_specifiers,
24067 width, initializer,
24068 attributes);
24070 else
24072 cp_declarator *declarator;
24073 tree asm_specification;
24074 int ctor_dtor_or_conv_p;
24076 /* Parse the declarator. */
24077 declarator
24078 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24079 &ctor_dtor_or_conv_p,
24080 /*parenthesized_p=*/NULL,
24081 /*member_p=*/true,
24082 friend_p);
24084 /* If something went wrong parsing the declarator, make sure
24085 that we at least consume some tokens. */
24086 if (declarator == cp_error_declarator)
24088 /* Skip to the end of the statement. */
24089 cp_parser_skip_to_end_of_statement (parser);
24090 /* If the next token is not a semicolon, that is
24091 probably because we just skipped over the body of
24092 a function. So, we consume a semicolon if
24093 present, but do not issue an error message if it
24094 is not present. */
24095 if (cp_lexer_next_token_is (parser->lexer,
24096 CPP_SEMICOLON))
24097 cp_lexer_consume_token (parser->lexer);
24098 goto out;
24101 if (declares_class_or_enum & 2)
24102 cp_parser_check_for_definition_in_return_type
24103 (declarator, decl_specifiers.type,
24104 decl_specifiers.locations[ds_type_spec]);
24106 /* Look for an asm-specification. */
24107 asm_specification = cp_parser_asm_specification_opt (parser);
24108 /* Look for attributes that apply to the declaration. */
24109 attributes = cp_parser_attributes_opt (parser);
24110 /* Remember which attributes are prefix attributes and
24111 which are not. */
24112 first_attribute = attributes;
24113 /* Combine the attributes. */
24114 attributes = attr_chainon (prefix_attributes, attributes);
24116 /* If it's an `=', then we have a constant-initializer or a
24117 pure-specifier. It is not correct to parse the
24118 initializer before registering the member declaration
24119 since the member declaration should be in scope while
24120 its initializer is processed. However, the rest of the
24121 front end does not yet provide an interface that allows
24122 us to handle this correctly. */
24123 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24125 /* In [class.mem]:
24127 A pure-specifier shall be used only in the declaration of
24128 a virtual function.
24130 A member-declarator can contain a constant-initializer
24131 only if it declares a static member of integral or
24132 enumeration type.
24134 Therefore, if the DECLARATOR is for a function, we look
24135 for a pure-specifier; otherwise, we look for a
24136 constant-initializer. When we call `grokfield', it will
24137 perform more stringent semantics checks. */
24138 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24139 if (function_declarator_p (declarator)
24140 || (decl_specifiers.type
24141 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24142 && declarator->kind == cdk_id
24143 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24144 == FUNCTION_TYPE)))
24145 initializer = cp_parser_pure_specifier (parser);
24146 else if (decl_specifiers.storage_class != sc_static)
24147 initializer = cp_parser_save_nsdmi (parser);
24148 else if (cxx_dialect >= cxx11)
24150 bool nonconst;
24151 /* Don't require a constant rvalue in C++11, since we
24152 might want a reference constant. We'll enforce
24153 constancy later. */
24154 cp_lexer_consume_token (parser->lexer);
24155 /* Parse the initializer. */
24156 initializer = cp_parser_initializer_clause (parser,
24157 &nonconst);
24159 else
24160 /* Parse the initializer. */
24161 initializer = cp_parser_constant_initializer (parser);
24163 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24164 && !function_declarator_p (declarator))
24166 bool x;
24167 if (decl_specifiers.storage_class != sc_static)
24168 initializer = cp_parser_save_nsdmi (parser);
24169 else
24170 initializer = cp_parser_initializer (parser, &x, &x);
24172 /* Otherwise, there is no initializer. */
24173 else
24174 initializer = NULL_TREE;
24176 /* See if we are probably looking at a function
24177 definition. We are certainly not looking at a
24178 member-declarator. Calling `grokfield' has
24179 side-effects, so we must not do it unless we are sure
24180 that we are looking at a member-declarator. */
24181 if (cp_parser_token_starts_function_definition_p
24182 (cp_lexer_peek_token (parser->lexer)))
24184 /* The grammar does not allow a pure-specifier to be
24185 used when a member function is defined. (It is
24186 possible that this fact is an oversight in the
24187 standard, since a pure function may be defined
24188 outside of the class-specifier. */
24189 if (initializer && initializer_token_start)
24190 error_at (initializer_token_start->location,
24191 "pure-specifier on function-definition");
24192 decl = cp_parser_save_member_function_body (parser,
24193 &decl_specifiers,
24194 declarator,
24195 attributes);
24196 if (parser->fully_implicit_function_template_p)
24197 decl = finish_fully_implicit_template (parser, decl);
24198 /* If the member was not a friend, declare it here. */
24199 if (!friend_p)
24200 finish_member_declaration (decl);
24201 /* Peek at the next token. */
24202 token = cp_lexer_peek_token (parser->lexer);
24203 /* If the next token is a semicolon, consume it. */
24204 if (token->type == CPP_SEMICOLON)
24206 location_t semicolon_loc
24207 = cp_lexer_consume_token (parser->lexer)->location;
24208 gcc_rich_location richloc (semicolon_loc);
24209 richloc.add_fixit_remove ();
24210 warning_at (&richloc, OPT_Wextra_semi,
24211 "extra %<;%> after in-class "
24212 "function definition");
24214 goto out;
24216 else
24217 if (declarator->kind == cdk_function)
24218 declarator->id_loc = token->location;
24219 /* Create the declaration. */
24220 decl = grokfield (declarator, &decl_specifiers,
24221 initializer, /*init_const_expr_p=*/true,
24222 asm_specification, attributes);
24223 if (parser->fully_implicit_function_template_p)
24225 if (friend_p)
24226 finish_fully_implicit_template (parser, 0);
24227 else
24228 decl = finish_fully_implicit_template (parser, decl);
24232 cp_finalize_omp_declare_simd (parser, decl);
24233 cp_finalize_oacc_routine (parser, decl, false);
24235 /* Reset PREFIX_ATTRIBUTES. */
24236 if (attributes != error_mark_node)
24238 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24239 attributes = TREE_CHAIN (attributes);
24240 if (attributes)
24241 TREE_CHAIN (attributes) = NULL_TREE;
24244 /* If there is any qualification still in effect, clear it
24245 now; we will be starting fresh with the next declarator. */
24246 parser->scope = NULL_TREE;
24247 parser->qualifying_scope = NULL_TREE;
24248 parser->object_scope = NULL_TREE;
24249 /* If it's a `,', then there are more declarators. */
24250 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24252 cp_lexer_consume_token (parser->lexer);
24253 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24255 cp_token *token = cp_lexer_previous_token (parser->lexer);
24256 gcc_rich_location richloc (token->location);
24257 richloc.add_fixit_remove ();
24258 error_at (&richloc, "stray %<,%> at end of "
24259 "member declaration");
24262 /* If the next token isn't a `;', then we have a parse error. */
24263 else if (cp_lexer_next_token_is_not (parser->lexer,
24264 CPP_SEMICOLON))
24266 /* The next token might be a ways away from where the
24267 actual semicolon is missing. Find the previous token
24268 and use that for our error position. */
24269 cp_token *token = cp_lexer_previous_token (parser->lexer);
24270 gcc_rich_location richloc (token->location);
24271 richloc.add_fixit_insert_after (";");
24272 error_at (&richloc, "expected %<;%> at end of "
24273 "member declaration");
24275 /* Assume that the user meant to provide a semicolon. If
24276 we were to cp_parser_skip_to_end_of_statement, we might
24277 skip to a semicolon inside a member function definition
24278 and issue nonsensical error messages. */
24279 assume_semicolon = true;
24282 if (decl)
24284 /* Add DECL to the list of members. */
24285 if (!friend_p
24286 /* Explicitly include, eg, NSDMIs, for better error
24287 recovery (c++/58650). */
24288 || !DECL_DECLARES_FUNCTION_P (decl))
24289 finish_member_declaration (decl);
24291 if (TREE_CODE (decl) == FUNCTION_DECL)
24292 cp_parser_save_default_args (parser, decl);
24293 else if (TREE_CODE (decl) == FIELD_DECL
24294 && DECL_INITIAL (decl))
24295 /* Add DECL to the queue of NSDMI to be parsed later. */
24296 vec_safe_push (unparsed_nsdmis, decl);
24299 if (assume_semicolon)
24300 goto out;
24304 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24305 out:
24306 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24309 /* Parse a pure-specifier.
24311 pure-specifier:
24314 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24315 Otherwise, ERROR_MARK_NODE is returned. */
24317 static tree
24318 cp_parser_pure_specifier (cp_parser* parser)
24320 cp_token *token;
24322 /* Look for the `=' token. */
24323 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24324 return error_mark_node;
24325 /* Look for the `0' token. */
24326 token = cp_lexer_peek_token (parser->lexer);
24328 if (token->type == CPP_EOF
24329 || token->type == CPP_PRAGMA_EOL)
24330 return error_mark_node;
24332 cp_lexer_consume_token (parser->lexer);
24334 /* Accept = default or = delete in c++0x mode. */
24335 if (token->keyword == RID_DEFAULT
24336 || token->keyword == RID_DELETE)
24338 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24339 return token->u.value;
24342 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24343 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24345 cp_parser_error (parser,
24346 "invalid pure specifier (only %<= 0%> is allowed)");
24347 cp_parser_skip_to_end_of_statement (parser);
24348 return error_mark_node;
24350 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24352 error_at (token->location, "templates may not be %<virtual%>");
24353 return error_mark_node;
24356 return integer_zero_node;
24359 /* Parse a constant-initializer.
24361 constant-initializer:
24362 = constant-expression
24364 Returns a representation of the constant-expression. */
24366 static tree
24367 cp_parser_constant_initializer (cp_parser* parser)
24369 /* Look for the `=' token. */
24370 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24371 return error_mark_node;
24373 /* It is invalid to write:
24375 struct S { static const int i = { 7 }; };
24378 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24380 cp_parser_error (parser,
24381 "a brace-enclosed initializer is not allowed here");
24382 /* Consume the opening brace. */
24383 matching_braces braces;
24384 braces.consume_open (parser);
24385 /* Skip the initializer. */
24386 cp_parser_skip_to_closing_brace (parser);
24387 /* Look for the trailing `}'. */
24388 braces.require_close (parser);
24390 return error_mark_node;
24393 return cp_parser_constant_expression (parser);
24396 /* Derived classes [gram.class.derived] */
24398 /* Parse a base-clause.
24400 base-clause:
24401 : base-specifier-list
24403 base-specifier-list:
24404 base-specifier ... [opt]
24405 base-specifier-list , base-specifier ... [opt]
24407 Returns a TREE_LIST representing the base-classes, in the order in
24408 which they were declared. The representation of each node is as
24409 described by cp_parser_base_specifier.
24411 In the case that no bases are specified, this function will return
24412 NULL_TREE, not ERROR_MARK_NODE. */
24414 static tree
24415 cp_parser_base_clause (cp_parser* parser)
24417 tree bases = NULL_TREE;
24419 /* Look for the `:' that begins the list. */
24420 cp_parser_require (parser, CPP_COLON, RT_COLON);
24422 /* Scan the base-specifier-list. */
24423 while (true)
24425 cp_token *token;
24426 tree base;
24427 bool pack_expansion_p = false;
24429 /* Look for the base-specifier. */
24430 base = cp_parser_base_specifier (parser);
24431 /* Look for the (optional) ellipsis. */
24432 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24434 /* Consume the `...'. */
24435 cp_lexer_consume_token (parser->lexer);
24437 pack_expansion_p = true;
24440 /* Add BASE to the front of the list. */
24441 if (base && base != error_mark_node)
24443 if (pack_expansion_p)
24444 /* Make this a pack expansion type. */
24445 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24447 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24449 TREE_CHAIN (base) = bases;
24450 bases = base;
24453 /* Peek at the next token. */
24454 token = cp_lexer_peek_token (parser->lexer);
24455 /* If it's not a comma, then the list is complete. */
24456 if (token->type != CPP_COMMA)
24457 break;
24458 /* Consume the `,'. */
24459 cp_lexer_consume_token (parser->lexer);
24462 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24463 base class had a qualified name. However, the next name that
24464 appears is certainly not qualified. */
24465 parser->scope = NULL_TREE;
24466 parser->qualifying_scope = NULL_TREE;
24467 parser->object_scope = NULL_TREE;
24469 return nreverse (bases);
24472 /* Parse a base-specifier.
24474 base-specifier:
24475 :: [opt] nested-name-specifier [opt] class-name
24476 virtual access-specifier [opt] :: [opt] nested-name-specifier
24477 [opt] class-name
24478 access-specifier virtual [opt] :: [opt] nested-name-specifier
24479 [opt] class-name
24481 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24482 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24483 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24484 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24486 static tree
24487 cp_parser_base_specifier (cp_parser* parser)
24489 cp_token *token;
24490 bool done = false;
24491 bool virtual_p = false;
24492 bool duplicate_virtual_error_issued_p = false;
24493 bool duplicate_access_error_issued_p = false;
24494 bool class_scope_p, template_p;
24495 tree access = access_default_node;
24496 tree type;
24498 /* Process the optional `virtual' and `access-specifier'. */
24499 while (!done)
24501 /* Peek at the next token. */
24502 token = cp_lexer_peek_token (parser->lexer);
24503 /* Process `virtual'. */
24504 switch (token->keyword)
24506 case RID_VIRTUAL:
24507 /* If `virtual' appears more than once, issue an error. */
24508 if (virtual_p && !duplicate_virtual_error_issued_p)
24510 cp_parser_error (parser,
24511 "%<virtual%> specified more than once in base-specifier");
24512 duplicate_virtual_error_issued_p = true;
24515 virtual_p = true;
24517 /* Consume the `virtual' token. */
24518 cp_lexer_consume_token (parser->lexer);
24520 break;
24522 case RID_PUBLIC:
24523 case RID_PROTECTED:
24524 case RID_PRIVATE:
24525 /* If more than one access specifier appears, issue an
24526 error. */
24527 if (access != access_default_node
24528 && !duplicate_access_error_issued_p)
24530 cp_parser_error (parser,
24531 "more than one access specifier in base-specifier");
24532 duplicate_access_error_issued_p = true;
24535 access = ridpointers[(int) token->keyword];
24537 /* Consume the access-specifier. */
24538 cp_lexer_consume_token (parser->lexer);
24540 break;
24542 default:
24543 done = true;
24544 break;
24547 /* It is not uncommon to see programs mechanically, erroneously, use
24548 the 'typename' keyword to denote (dependent) qualified types
24549 as base classes. */
24550 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24552 token = cp_lexer_peek_token (parser->lexer);
24553 if (!processing_template_decl)
24554 error_at (token->location,
24555 "keyword %<typename%> not allowed outside of templates");
24556 else
24557 error_at (token->location,
24558 "keyword %<typename%> not allowed in this context "
24559 "(the base class is implicitly a type)");
24560 cp_lexer_consume_token (parser->lexer);
24563 /* Look for the optional `::' operator. */
24564 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24565 /* Look for the nested-name-specifier. The simplest way to
24566 implement:
24568 [temp.res]
24570 The keyword `typename' is not permitted in a base-specifier or
24571 mem-initializer; in these contexts a qualified name that
24572 depends on a template-parameter is implicitly assumed to be a
24573 type name.
24575 is to pretend that we have seen the `typename' keyword at this
24576 point. */
24577 cp_parser_nested_name_specifier_opt (parser,
24578 /*typename_keyword_p=*/true,
24579 /*check_dependency_p=*/true,
24580 /*type_p=*/true,
24581 /*is_declaration=*/true);
24582 /* If the base class is given by a qualified name, assume that names
24583 we see are type names or templates, as appropriate. */
24584 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24585 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24587 if (!parser->scope
24588 && cp_lexer_next_token_is_decltype (parser->lexer))
24589 /* DR 950 allows decltype as a base-specifier. */
24590 type = cp_parser_decltype (parser);
24591 else
24593 /* Otherwise, look for the class-name. */
24594 type = cp_parser_class_name (parser,
24595 class_scope_p,
24596 template_p,
24597 typename_type,
24598 /*check_dependency_p=*/true,
24599 /*class_head_p=*/false,
24600 /*is_declaration=*/true);
24601 type = TREE_TYPE (type);
24604 if (type == error_mark_node)
24605 return error_mark_node;
24607 return finish_base_specifier (type, access, virtual_p);
24610 /* Exception handling [gram.exception] */
24612 /* Parse an (optional) noexcept-specification.
24614 noexcept-specification:
24615 noexcept ( constant-expression ) [opt]
24617 If no noexcept-specification is present, returns NULL_TREE.
24618 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24619 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24620 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24621 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24622 in which case a boolean condition is returned instead. */
24624 static tree
24625 cp_parser_noexcept_specification_opt (cp_parser* parser,
24626 bool require_constexpr,
24627 bool* consumed_expr,
24628 bool return_cond)
24630 cp_token *token;
24631 const char *saved_message;
24633 /* Peek at the next token. */
24634 token = cp_lexer_peek_token (parser->lexer);
24636 /* Is it a noexcept-specification? */
24637 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24639 tree expr;
24640 cp_lexer_consume_token (parser->lexer);
24642 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24644 matching_parens parens;
24645 parens.consume_open (parser);
24647 if (require_constexpr)
24649 /* Types may not be defined in an exception-specification. */
24650 saved_message = parser->type_definition_forbidden_message;
24651 parser->type_definition_forbidden_message
24652 = G_("types may not be defined in an exception-specification");
24654 expr = cp_parser_constant_expression (parser);
24656 /* Restore the saved message. */
24657 parser->type_definition_forbidden_message = saved_message;
24659 else
24661 expr = cp_parser_expression (parser);
24662 *consumed_expr = true;
24665 parens.require_close (parser);
24667 else
24669 expr = boolean_true_node;
24670 if (!require_constexpr)
24671 *consumed_expr = false;
24674 /* We cannot build a noexcept-spec right away because this will check
24675 that expr is a constexpr. */
24676 if (!return_cond)
24677 return build_noexcept_spec (expr, tf_warning_or_error);
24678 else
24679 return expr;
24681 else
24682 return NULL_TREE;
24685 /* Parse an (optional) exception-specification.
24687 exception-specification:
24688 throw ( type-id-list [opt] )
24690 Returns a TREE_LIST representing the exception-specification. The
24691 TREE_VALUE of each node is a type. */
24693 static tree
24694 cp_parser_exception_specification_opt (cp_parser* parser)
24696 cp_token *token;
24697 tree type_id_list;
24698 const char *saved_message;
24700 /* Peek at the next token. */
24701 token = cp_lexer_peek_token (parser->lexer);
24703 /* Is it a noexcept-specification? */
24704 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24705 false);
24706 if (type_id_list != NULL_TREE)
24707 return type_id_list;
24709 /* If it's not `throw', then there's no exception-specification. */
24710 if (!cp_parser_is_keyword (token, RID_THROW))
24711 return NULL_TREE;
24713 location_t loc = token->location;
24715 /* Consume the `throw'. */
24716 cp_lexer_consume_token (parser->lexer);
24718 /* Look for the `('. */
24719 matching_parens parens;
24720 parens.require_open (parser);
24722 /* Peek at the next token. */
24723 token = cp_lexer_peek_token (parser->lexer);
24724 /* If it's not a `)', then there is a type-id-list. */
24725 if (token->type != CPP_CLOSE_PAREN)
24727 /* Types may not be defined in an exception-specification. */
24728 saved_message = parser->type_definition_forbidden_message;
24729 parser->type_definition_forbidden_message
24730 = G_("types may not be defined in an exception-specification");
24731 /* Parse the type-id-list. */
24732 type_id_list = cp_parser_type_id_list (parser);
24733 /* Restore the saved message. */
24734 parser->type_definition_forbidden_message = saved_message;
24736 if (cxx_dialect >= cxx17)
24738 error_at (loc, "ISO C++17 does not allow dynamic exception "
24739 "specifications");
24740 type_id_list = NULL_TREE;
24742 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24743 warning_at (loc, OPT_Wdeprecated,
24744 "dynamic exception specifications are deprecated in "
24745 "C++11");
24747 /* In C++17, throw() is equivalent to noexcept (true). throw()
24748 is deprecated in C++11 and above as well, but is still widely used,
24749 so don't warn about it yet. */
24750 else if (cxx_dialect >= cxx17)
24751 type_id_list = noexcept_true_spec;
24752 else
24753 type_id_list = empty_except_spec;
24755 /* Look for the `)'. */
24756 parens.require_close (parser);
24758 return type_id_list;
24761 /* Parse an (optional) type-id-list.
24763 type-id-list:
24764 type-id ... [opt]
24765 type-id-list , type-id ... [opt]
24767 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24768 in the order that the types were presented. */
24770 static tree
24771 cp_parser_type_id_list (cp_parser* parser)
24773 tree types = NULL_TREE;
24775 while (true)
24777 cp_token *token;
24778 tree type;
24780 token = cp_lexer_peek_token (parser->lexer);
24782 /* Get the next type-id. */
24783 type = cp_parser_type_id (parser);
24784 /* Check for invalid 'auto'. */
24785 if (flag_concepts && type_uses_auto (type))
24787 error_at (token->location,
24788 "invalid use of %<auto%> in exception-specification");
24789 type = error_mark_node;
24791 /* Parse the optional ellipsis. */
24792 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24794 /* Consume the `...'. */
24795 cp_lexer_consume_token (parser->lexer);
24797 /* Turn the type into a pack expansion expression. */
24798 type = make_pack_expansion (type);
24800 /* Add it to the list. */
24801 types = add_exception_specifier (types, type, /*complain=*/1);
24802 /* Peek at the next token. */
24803 token = cp_lexer_peek_token (parser->lexer);
24804 /* If it is not a `,', we are done. */
24805 if (token->type != CPP_COMMA)
24806 break;
24807 /* Consume the `,'. */
24808 cp_lexer_consume_token (parser->lexer);
24811 return nreverse (types);
24814 /* Parse a try-block.
24816 try-block:
24817 try compound-statement handler-seq */
24819 static tree
24820 cp_parser_try_block (cp_parser* parser)
24822 tree try_block;
24824 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24825 if (parser->in_function_body
24826 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24827 error ("%<try%> in %<constexpr%> function");
24829 try_block = begin_try_block ();
24830 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24831 finish_try_block (try_block);
24832 cp_parser_handler_seq (parser);
24833 finish_handler_sequence (try_block);
24835 return try_block;
24838 /* Parse a function-try-block.
24840 function-try-block:
24841 try ctor-initializer [opt] function-body handler-seq */
24843 static void
24844 cp_parser_function_try_block (cp_parser* parser)
24846 tree compound_stmt;
24847 tree try_block;
24849 /* Look for the `try' keyword. */
24850 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24851 return;
24852 /* Let the rest of the front end know where we are. */
24853 try_block = begin_function_try_block (&compound_stmt);
24854 /* Parse the function-body. */
24855 cp_parser_ctor_initializer_opt_and_function_body
24856 (parser, /*in_function_try_block=*/true);
24857 /* We're done with the `try' part. */
24858 finish_function_try_block (try_block);
24859 /* Parse the handlers. */
24860 cp_parser_handler_seq (parser);
24861 /* We're done with the handlers. */
24862 finish_function_handler_sequence (try_block, compound_stmt);
24865 /* Parse a handler-seq.
24867 handler-seq:
24868 handler handler-seq [opt] */
24870 static void
24871 cp_parser_handler_seq (cp_parser* parser)
24873 while (true)
24875 cp_token *token;
24877 /* Parse the handler. */
24878 cp_parser_handler (parser);
24879 /* Peek at the next token. */
24880 token = cp_lexer_peek_token (parser->lexer);
24881 /* If it's not `catch' then there are no more handlers. */
24882 if (!cp_parser_is_keyword (token, RID_CATCH))
24883 break;
24887 /* Parse a handler.
24889 handler:
24890 catch ( exception-declaration ) compound-statement */
24892 static void
24893 cp_parser_handler (cp_parser* parser)
24895 tree handler;
24896 tree declaration;
24898 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24899 handler = begin_handler ();
24900 matching_parens parens;
24901 parens.require_open (parser);
24902 declaration = cp_parser_exception_declaration (parser);
24903 finish_handler_parms (declaration, handler);
24904 parens.require_close (parser);
24905 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24906 finish_handler (handler);
24909 /* Parse an exception-declaration.
24911 exception-declaration:
24912 type-specifier-seq declarator
24913 type-specifier-seq abstract-declarator
24914 type-specifier-seq
24917 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24918 ellipsis variant is used. */
24920 static tree
24921 cp_parser_exception_declaration (cp_parser* parser)
24923 cp_decl_specifier_seq type_specifiers;
24924 cp_declarator *declarator;
24925 const char *saved_message;
24927 /* If it's an ellipsis, it's easy to handle. */
24928 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24930 /* Consume the `...' token. */
24931 cp_lexer_consume_token (parser->lexer);
24932 return NULL_TREE;
24935 /* Types may not be defined in exception-declarations. */
24936 saved_message = parser->type_definition_forbidden_message;
24937 parser->type_definition_forbidden_message
24938 = G_("types may not be defined in exception-declarations");
24940 /* Parse the type-specifier-seq. */
24941 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24942 /*is_trailing_return=*/false,
24943 &type_specifiers);
24944 /* If it's a `)', then there is no declarator. */
24945 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24946 declarator = NULL;
24947 else
24948 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24949 /*ctor_dtor_or_conv_p=*/NULL,
24950 /*parenthesized_p=*/NULL,
24951 /*member_p=*/false,
24952 /*friend_p=*/false);
24954 /* Restore the saved message. */
24955 parser->type_definition_forbidden_message = saved_message;
24957 if (!type_specifiers.any_specifiers_p)
24958 return error_mark_node;
24960 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24963 /* Parse a throw-expression.
24965 throw-expression:
24966 throw assignment-expression [opt]
24968 Returns a THROW_EXPR representing the throw-expression. */
24970 static tree
24971 cp_parser_throw_expression (cp_parser* parser)
24973 tree expression;
24974 cp_token* token;
24976 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24977 token = cp_lexer_peek_token (parser->lexer);
24978 /* Figure out whether or not there is an assignment-expression
24979 following the "throw" keyword. */
24980 if (token->type == CPP_COMMA
24981 || token->type == CPP_SEMICOLON
24982 || token->type == CPP_CLOSE_PAREN
24983 || token->type == CPP_CLOSE_SQUARE
24984 || token->type == CPP_CLOSE_BRACE
24985 || token->type == CPP_COLON)
24986 expression = NULL_TREE;
24987 else
24988 expression = cp_parser_assignment_expression (parser);
24990 return build_throw (expression);
24993 /* GNU Extensions */
24995 /* Parse an (optional) asm-specification.
24997 asm-specification:
24998 asm ( string-literal )
25000 If the asm-specification is present, returns a STRING_CST
25001 corresponding to the string-literal. Otherwise, returns
25002 NULL_TREE. */
25004 static tree
25005 cp_parser_asm_specification_opt (cp_parser* parser)
25007 cp_token *token;
25008 tree asm_specification;
25010 /* Peek at the next token. */
25011 token = cp_lexer_peek_token (parser->lexer);
25012 /* If the next token isn't the `asm' keyword, then there's no
25013 asm-specification. */
25014 if (!cp_parser_is_keyword (token, RID_ASM))
25015 return NULL_TREE;
25017 /* Consume the `asm' token. */
25018 cp_lexer_consume_token (parser->lexer);
25019 /* Look for the `('. */
25020 matching_parens parens;
25021 parens.require_open (parser);
25023 /* Look for the string-literal. */
25024 asm_specification = cp_parser_string_literal (parser, false, false);
25026 /* Look for the `)'. */
25027 parens.require_close (parser);
25029 return asm_specification;
25032 /* Parse an asm-operand-list.
25034 asm-operand-list:
25035 asm-operand
25036 asm-operand-list , asm-operand
25038 asm-operand:
25039 string-literal ( expression )
25040 [ string-literal ] string-literal ( expression )
25042 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25043 each node is the expression. The TREE_PURPOSE is itself a
25044 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25045 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25046 is a STRING_CST for the string literal before the parenthesis. Returns
25047 ERROR_MARK_NODE if any of the operands are invalid. */
25049 static tree
25050 cp_parser_asm_operand_list (cp_parser* parser)
25052 tree asm_operands = NULL_TREE;
25053 bool invalid_operands = false;
25055 while (true)
25057 tree string_literal;
25058 tree expression;
25059 tree name;
25061 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25063 /* Consume the `[' token. */
25064 cp_lexer_consume_token (parser->lexer);
25065 /* Read the operand name. */
25066 name = cp_parser_identifier (parser);
25067 if (name != error_mark_node)
25068 name = build_string (IDENTIFIER_LENGTH (name),
25069 IDENTIFIER_POINTER (name));
25070 /* Look for the closing `]'. */
25071 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25073 else
25074 name = NULL_TREE;
25075 /* Look for the string-literal. */
25076 string_literal = cp_parser_string_literal (parser, false, false);
25078 /* Look for the `('. */
25079 matching_parens parens;
25080 parens.require_open (parser);
25081 /* Parse the expression. */
25082 expression = cp_parser_expression (parser);
25083 /* Look for the `)'. */
25084 parens.require_close (parser);
25086 if (name == error_mark_node
25087 || string_literal == error_mark_node
25088 || expression == error_mark_node)
25089 invalid_operands = true;
25091 /* Add this operand to the list. */
25092 asm_operands = tree_cons (build_tree_list (name, string_literal),
25093 expression,
25094 asm_operands);
25095 /* If the next token is not a `,', there are no more
25096 operands. */
25097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25098 break;
25099 /* Consume the `,'. */
25100 cp_lexer_consume_token (parser->lexer);
25103 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25106 /* Parse an asm-clobber-list.
25108 asm-clobber-list:
25109 string-literal
25110 asm-clobber-list , string-literal
25112 Returns a TREE_LIST, indicating the clobbers in the order that they
25113 appeared. The TREE_VALUE of each node is a STRING_CST. */
25115 static tree
25116 cp_parser_asm_clobber_list (cp_parser* parser)
25118 tree clobbers = NULL_TREE;
25120 while (true)
25122 tree string_literal;
25124 /* Look for the string literal. */
25125 string_literal = cp_parser_string_literal (parser, false, false);
25126 /* Add it to the list. */
25127 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25128 /* If the next token is not a `,', then the list is
25129 complete. */
25130 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25131 break;
25132 /* Consume the `,' token. */
25133 cp_lexer_consume_token (parser->lexer);
25136 return clobbers;
25139 /* Parse an asm-label-list.
25141 asm-label-list:
25142 identifier
25143 asm-label-list , identifier
25145 Returns a TREE_LIST, indicating the labels in the order that they
25146 appeared. The TREE_VALUE of each node is a label. */
25148 static tree
25149 cp_parser_asm_label_list (cp_parser* parser)
25151 tree labels = NULL_TREE;
25153 while (true)
25155 tree identifier, label, name;
25157 /* Look for the identifier. */
25158 identifier = cp_parser_identifier (parser);
25159 if (!error_operand_p (identifier))
25161 label = lookup_label (identifier);
25162 if (TREE_CODE (label) == LABEL_DECL)
25164 TREE_USED (label) = 1;
25165 check_goto (label);
25166 name = build_string (IDENTIFIER_LENGTH (identifier),
25167 IDENTIFIER_POINTER (identifier));
25168 labels = tree_cons (name, label, labels);
25171 /* If the next token is not a `,', then the list is
25172 complete. */
25173 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25174 break;
25175 /* Consume the `,' token. */
25176 cp_lexer_consume_token (parser->lexer);
25179 return nreverse (labels);
25182 /* Return TRUE iff the next tokens in the stream are possibly the
25183 beginning of a GNU extension attribute. */
25185 static bool
25186 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25188 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25191 /* Return TRUE iff the next tokens in the stream are possibly the
25192 beginning of a standard C++-11 attribute specifier. */
25194 static bool
25195 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25197 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25200 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25201 beginning of a standard C++-11 attribute specifier. */
25203 static bool
25204 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25206 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25208 return (cxx_dialect >= cxx11
25209 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25210 || (token->type == CPP_OPEN_SQUARE
25211 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25212 && token->type == CPP_OPEN_SQUARE)));
25215 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25216 beginning of a GNU extension attribute. */
25218 static bool
25219 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25221 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25223 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25226 /* Return true iff the next tokens can be the beginning of either a
25227 GNU attribute list, or a standard C++11 attribute sequence. */
25229 static bool
25230 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25232 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25233 || cp_next_tokens_can_be_std_attribute_p (parser));
25236 /* Return true iff the next Nth tokens can be the beginning of either
25237 a GNU attribute list, or a standard C++11 attribute sequence. */
25239 static bool
25240 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25242 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25243 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25246 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25247 of GNU attributes, or return NULL. */
25249 static tree
25250 cp_parser_attributes_opt (cp_parser *parser)
25252 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25253 return cp_parser_gnu_attributes_opt (parser);
25254 return cp_parser_std_attribute_spec_seq (parser);
25257 /* Parse an (optional) series of attributes.
25259 attributes:
25260 attributes attribute
25262 attribute:
25263 __attribute__ (( attribute-list [opt] ))
25265 The return value is as for cp_parser_gnu_attribute_list. */
25267 static tree
25268 cp_parser_gnu_attributes_opt (cp_parser* parser)
25270 tree attributes = NULL_TREE;
25272 temp_override<bool> cleanup
25273 (parser->auto_is_implicit_function_template_parm_p, false);
25275 while (true)
25277 cp_token *token;
25278 tree attribute_list;
25279 bool ok = true;
25281 /* Peek at the next token. */
25282 token = cp_lexer_peek_token (parser->lexer);
25283 /* If it's not `__attribute__', then we're done. */
25284 if (token->keyword != RID_ATTRIBUTE)
25285 break;
25287 /* Consume the `__attribute__' keyword. */
25288 cp_lexer_consume_token (parser->lexer);
25289 /* Look for the two `(' tokens. */
25290 matching_parens outer_parens;
25291 outer_parens.require_open (parser);
25292 matching_parens inner_parens;
25293 inner_parens.require_open (parser);
25295 /* Peek at the next token. */
25296 token = cp_lexer_peek_token (parser->lexer);
25297 if (token->type != CPP_CLOSE_PAREN)
25298 /* Parse the attribute-list. */
25299 attribute_list = cp_parser_gnu_attribute_list (parser);
25300 else
25301 /* If the next token is a `)', then there is no attribute
25302 list. */
25303 attribute_list = NULL;
25305 /* Look for the two `)' tokens. */
25306 if (!inner_parens.require_close (parser))
25307 ok = false;
25308 if (!outer_parens.require_close (parser))
25309 ok = false;
25310 if (!ok)
25311 cp_parser_skip_to_end_of_statement (parser);
25313 /* Add these new attributes to the list. */
25314 attributes = attr_chainon (attributes, attribute_list);
25317 return attributes;
25320 /* Parse a GNU attribute-list.
25322 attribute-list:
25323 attribute
25324 attribute-list , attribute
25326 attribute:
25327 identifier
25328 identifier ( identifier )
25329 identifier ( identifier , expression-list )
25330 identifier ( expression-list )
25332 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25333 to an attribute. The TREE_PURPOSE of each node is the identifier
25334 indicating which attribute is in use. The TREE_VALUE represents
25335 the arguments, if any. */
25337 static tree
25338 cp_parser_gnu_attribute_list (cp_parser* parser)
25340 tree attribute_list = NULL_TREE;
25341 bool save_translate_strings_p = parser->translate_strings_p;
25343 parser->translate_strings_p = false;
25344 while (true)
25346 cp_token *token;
25347 tree identifier;
25348 tree attribute;
25350 /* Look for the identifier. We also allow keywords here; for
25351 example `__attribute__ ((const))' is legal. */
25352 token = cp_lexer_peek_token (parser->lexer);
25353 if (token->type == CPP_NAME
25354 || token->type == CPP_KEYWORD)
25356 tree arguments = NULL_TREE;
25358 /* Consume the token, but save it since we need it for the
25359 SIMD enabled function parsing. */
25360 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25362 /* Save away the identifier that indicates which attribute
25363 this is. */
25364 identifier = (token->type == CPP_KEYWORD)
25365 /* For keywords, use the canonical spelling, not the
25366 parsed identifier. */
25367 ? ridpointers[(int) token->keyword]
25368 : id_token->u.value;
25370 identifier = canonicalize_attr_name (identifier);
25371 attribute = build_tree_list (identifier, NULL_TREE);
25373 /* Peek at the next token. */
25374 token = cp_lexer_peek_token (parser->lexer);
25375 /* If it's an `(', then parse the attribute arguments. */
25376 if (token->type == CPP_OPEN_PAREN)
25378 vec<tree, va_gc> *vec;
25379 int attr_flag = (attribute_takes_identifier_p (identifier)
25380 ? id_attr : normal_attr);
25381 vec = cp_parser_parenthesized_expression_list
25382 (parser, attr_flag, /*cast_p=*/false,
25383 /*allow_expansion_p=*/false,
25384 /*non_constant_p=*/NULL);
25385 if (vec == NULL)
25386 arguments = error_mark_node;
25387 else
25389 arguments = build_tree_list_vec (vec);
25390 release_tree_vector (vec);
25392 /* Save the arguments away. */
25393 TREE_VALUE (attribute) = arguments;
25396 if (arguments != error_mark_node)
25398 /* Add this attribute to the list. */
25399 TREE_CHAIN (attribute) = attribute_list;
25400 attribute_list = attribute;
25403 token = cp_lexer_peek_token (parser->lexer);
25405 /* Now, look for more attributes. If the next token isn't a
25406 `,', we're done. */
25407 if (token->type != CPP_COMMA)
25408 break;
25410 /* Consume the comma and keep going. */
25411 cp_lexer_consume_token (parser->lexer);
25413 parser->translate_strings_p = save_translate_strings_p;
25415 /* We built up the list in reverse order. */
25416 return nreverse (attribute_list);
25419 /* Parse a standard C++11 attribute.
25421 The returned representation is a TREE_LIST which TREE_PURPOSE is
25422 the scoped name of the attribute, and the TREE_VALUE is its
25423 arguments list.
25425 Note that the scoped name of the attribute is itself a TREE_LIST
25426 which TREE_PURPOSE is the namespace of the attribute, and
25427 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25428 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25429 and which TREE_PURPOSE is directly the attribute name.
25431 Clients of the attribute code should use get_attribute_namespace
25432 and get_attribute_name to get the actual namespace and name of
25433 attributes, regardless of their being GNU or C++11 attributes.
25435 attribute:
25436 attribute-token attribute-argument-clause [opt]
25438 attribute-token:
25439 identifier
25440 attribute-scoped-token
25442 attribute-scoped-token:
25443 attribute-namespace :: identifier
25445 attribute-namespace:
25446 identifier
25448 attribute-argument-clause:
25449 ( balanced-token-seq )
25451 balanced-token-seq:
25452 balanced-token [opt]
25453 balanced-token-seq balanced-token
25455 balanced-token:
25456 ( balanced-token-seq )
25457 [ balanced-token-seq ]
25458 { balanced-token-seq }. */
25460 static tree
25461 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25463 tree attribute, attr_id = NULL_TREE, arguments;
25464 cp_token *token;
25466 temp_override<bool> cleanup
25467 (parser->auto_is_implicit_function_template_parm_p, false);
25469 /* First, parse name of the attribute, a.k.a attribute-token. */
25471 token = cp_lexer_peek_token (parser->lexer);
25472 if (token->type == CPP_NAME)
25473 attr_id = token->u.value;
25474 else if (token->type == CPP_KEYWORD)
25475 attr_id = ridpointers[(int) token->keyword];
25476 else if (token->flags & NAMED_OP)
25477 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25479 if (attr_id == NULL_TREE)
25480 return NULL_TREE;
25482 cp_lexer_consume_token (parser->lexer);
25484 token = cp_lexer_peek_token (parser->lexer);
25485 if (token->type == CPP_SCOPE)
25487 /* We are seeing a scoped attribute token. */
25489 cp_lexer_consume_token (parser->lexer);
25490 if (attr_ns)
25491 error_at (token->location, "attribute using prefix used together "
25492 "with scoped attribute token");
25493 attr_ns = attr_id;
25495 token = cp_lexer_consume_token (parser->lexer);
25496 if (token->type == CPP_NAME)
25497 attr_id = token->u.value;
25498 else if (token->type == CPP_KEYWORD)
25499 attr_id = ridpointers[(int) token->keyword];
25500 else if (token->flags & NAMED_OP)
25501 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25502 else
25504 error_at (token->location,
25505 "expected an identifier for the attribute name");
25506 return error_mark_node;
25509 attr_ns = canonicalize_attr_name (attr_ns);
25510 attr_id = canonicalize_attr_name (attr_id);
25511 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25512 NULL_TREE);
25513 token = cp_lexer_peek_token (parser->lexer);
25515 else if (attr_ns)
25517 attr_ns = canonicalize_attr_name (attr_ns);
25518 attr_id = canonicalize_attr_name (attr_id);
25519 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25520 NULL_TREE);
25522 else
25524 attr_id = canonicalize_attr_name (attr_id);
25525 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25526 NULL_TREE);
25527 /* C++11 noreturn attribute is equivalent to GNU's. */
25528 if (is_attribute_p ("noreturn", attr_id))
25529 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25530 /* C++14 deprecated attribute is equivalent to GNU's. */
25531 else if (is_attribute_p ("deprecated", attr_id))
25532 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25533 /* C++17 fallthrough attribute is equivalent to GNU's. */
25534 else if (is_attribute_p ("fallthrough", attr_id))
25535 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25536 /* Transactional Memory TS optimize_for_synchronized attribute is
25537 equivalent to GNU transaction_callable. */
25538 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25539 TREE_PURPOSE (attribute)
25540 = get_identifier ("transaction_callable");
25541 /* Transactional Memory attributes are GNU attributes. */
25542 else if (tm_attr_to_mask (attr_id))
25543 TREE_PURPOSE (attribute) = attr_id;
25546 /* Now parse the optional argument clause of the attribute. */
25548 if (token->type != CPP_OPEN_PAREN)
25549 return attribute;
25552 vec<tree, va_gc> *vec;
25553 int attr_flag = normal_attr;
25555 if (attr_ns == gnu_identifier
25556 && attribute_takes_identifier_p (attr_id))
25557 /* A GNU attribute that takes an identifier in parameter. */
25558 attr_flag = id_attr;
25560 vec = cp_parser_parenthesized_expression_list
25561 (parser, attr_flag, /*cast_p=*/false,
25562 /*allow_expansion_p=*/true,
25563 /*non_constant_p=*/NULL);
25564 if (vec == NULL)
25565 arguments = error_mark_node;
25566 else
25568 arguments = build_tree_list_vec (vec);
25569 release_tree_vector (vec);
25572 if (arguments == error_mark_node)
25573 attribute = error_mark_node;
25574 else
25575 TREE_VALUE (attribute) = arguments;
25578 return attribute;
25581 /* Check that the attribute ATTRIBUTE appears at most once in the
25582 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25583 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25584 isn't implemented yet in GCC. */
25586 static void
25587 cp_parser_check_std_attribute (tree attributes, tree attribute)
25589 if (attributes)
25591 tree name = get_attribute_name (attribute);
25592 if (is_attribute_p ("noreturn", name)
25593 && lookup_attribute ("noreturn", attributes))
25594 error ("attribute %<noreturn%> can appear at most once "
25595 "in an attribute-list");
25596 else if (is_attribute_p ("deprecated", name)
25597 && lookup_attribute ("deprecated", attributes))
25598 error ("attribute %<deprecated%> can appear at most once "
25599 "in an attribute-list");
25603 /* Parse a list of standard C++-11 attributes.
25605 attribute-list:
25606 attribute [opt]
25607 attribute-list , attribute[opt]
25608 attribute ...
25609 attribute-list , attribute ...
25612 static tree
25613 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25615 tree attributes = NULL_TREE, attribute = NULL_TREE;
25616 cp_token *token = NULL;
25618 while (true)
25620 attribute = cp_parser_std_attribute (parser, attr_ns);
25621 if (attribute == error_mark_node)
25622 break;
25623 if (attribute != NULL_TREE)
25625 cp_parser_check_std_attribute (attributes, attribute);
25626 TREE_CHAIN (attribute) = attributes;
25627 attributes = attribute;
25629 token = cp_lexer_peek_token (parser->lexer);
25630 if (token->type == CPP_ELLIPSIS)
25632 cp_lexer_consume_token (parser->lexer);
25633 if (attribute == NULL_TREE)
25634 error_at (token->location,
25635 "expected attribute before %<...%>");
25636 else
25638 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25639 if (pack == error_mark_node)
25640 return error_mark_node;
25641 TREE_VALUE (attribute) = pack;
25643 token = cp_lexer_peek_token (parser->lexer);
25645 if (token->type != CPP_COMMA)
25646 break;
25647 cp_lexer_consume_token (parser->lexer);
25649 attributes = nreverse (attributes);
25650 return attributes;
25653 /* Parse a standard C++-11 attribute specifier.
25655 attribute-specifier:
25656 [ [ attribute-using-prefix [opt] attribute-list ] ]
25657 alignment-specifier
25659 attribute-using-prefix:
25660 using attribute-namespace :
25662 alignment-specifier:
25663 alignas ( type-id ... [opt] )
25664 alignas ( alignment-expression ... [opt] ). */
25666 static tree
25667 cp_parser_std_attribute_spec (cp_parser *parser)
25669 tree attributes = NULL_TREE;
25670 cp_token *token = cp_lexer_peek_token (parser->lexer);
25672 if (token->type == CPP_OPEN_SQUARE
25673 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25675 tree attr_ns = NULL_TREE;
25677 cp_lexer_consume_token (parser->lexer);
25678 cp_lexer_consume_token (parser->lexer);
25680 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25682 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25683 if (token->type == CPP_NAME)
25684 attr_ns = token->u.value;
25685 else if (token->type == CPP_KEYWORD)
25686 attr_ns = ridpointers[(int) token->keyword];
25687 else if (token->flags & NAMED_OP)
25688 attr_ns = get_identifier (cpp_type2name (token->type,
25689 token->flags));
25690 if (attr_ns
25691 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25693 if (cxx_dialect < cxx17
25694 && !in_system_header_at (input_location))
25695 pedwarn (input_location, 0,
25696 "attribute using prefix only available "
25697 "with -std=c++17 or -std=gnu++17");
25699 cp_lexer_consume_token (parser->lexer);
25700 cp_lexer_consume_token (parser->lexer);
25701 cp_lexer_consume_token (parser->lexer);
25703 else
25704 attr_ns = NULL_TREE;
25707 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25709 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25710 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25711 cp_parser_skip_to_end_of_statement (parser);
25712 else
25713 /* Warn about parsing c++11 attribute in non-c++11 mode, only
25714 when we are sure that we have actually parsed them. */
25715 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25717 else
25719 tree alignas_expr;
25721 /* Look for an alignment-specifier. */
25723 token = cp_lexer_peek_token (parser->lexer);
25725 if (token->type != CPP_KEYWORD
25726 || token->keyword != RID_ALIGNAS)
25727 return NULL_TREE;
25729 cp_lexer_consume_token (parser->lexer);
25730 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25732 matching_parens parens;
25733 if (!parens.require_open (parser))
25734 return error_mark_node;
25736 cp_parser_parse_tentatively (parser);
25737 alignas_expr = cp_parser_type_id (parser);
25739 if (!cp_parser_parse_definitely (parser))
25741 alignas_expr = cp_parser_assignment_expression (parser);
25742 if (alignas_expr == error_mark_node)
25743 cp_parser_skip_to_end_of_statement (parser);
25744 if (alignas_expr == NULL_TREE
25745 || alignas_expr == error_mark_node)
25746 return alignas_expr;
25749 alignas_expr = cxx_alignas_expr (alignas_expr);
25750 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25752 /* Handle alignas (pack...). */
25753 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25755 cp_lexer_consume_token (parser->lexer);
25756 alignas_expr = make_pack_expansion (alignas_expr);
25759 /* Something went wrong, so don't build the attribute. */
25760 if (alignas_expr == error_mark_node)
25761 return error_mark_node;
25763 if (!parens.require_close (parser))
25764 return error_mark_node;
25766 /* Build the C++-11 representation of an 'aligned'
25767 attribute. */
25768 attributes
25769 = build_tree_list (build_tree_list (gnu_identifier,
25770 aligned_identifier), alignas_expr);
25773 return attributes;
25776 /* Parse a standard C++-11 attribute-specifier-seq.
25778 attribute-specifier-seq:
25779 attribute-specifier-seq [opt] attribute-specifier
25782 static tree
25783 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25785 tree attr_specs = NULL_TREE;
25786 tree attr_last = NULL_TREE;
25788 while (true)
25790 tree attr_spec = cp_parser_std_attribute_spec (parser);
25791 if (attr_spec == NULL_TREE)
25792 break;
25793 if (attr_spec == error_mark_node)
25794 return error_mark_node;
25796 if (attr_last)
25797 TREE_CHAIN (attr_last) = attr_spec;
25798 else
25799 attr_specs = attr_last = attr_spec;
25800 attr_last = tree_last (attr_last);
25803 return attr_specs;
25806 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25807 return index of the first token after balanced-token, or N on failure. */
25809 static size_t
25810 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25812 size_t orig_n = n;
25813 int nparens = 0, nbraces = 0, nsquares = 0;
25815 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25817 case CPP_EOF:
25818 case CPP_PRAGMA_EOL:
25819 /* Ran out of tokens. */
25820 return orig_n;
25821 case CPP_OPEN_PAREN:
25822 ++nparens;
25823 break;
25824 case CPP_OPEN_BRACE:
25825 ++nbraces;
25826 break;
25827 case CPP_OPEN_SQUARE:
25828 ++nsquares;
25829 break;
25830 case CPP_CLOSE_PAREN:
25831 --nparens;
25832 break;
25833 case CPP_CLOSE_BRACE:
25834 --nbraces;
25835 break;
25836 case CPP_CLOSE_SQUARE:
25837 --nsquares;
25838 break;
25839 default:
25840 break;
25842 while (nparens || nbraces || nsquares);
25843 return n;
25846 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25847 return index of the first token after the GNU attribute tokens, or N on
25848 failure. */
25850 static size_t
25851 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25853 while (true)
25855 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25856 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25857 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25858 break;
25860 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25861 if (n2 == n + 2)
25862 break;
25863 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25864 break;
25865 n = n2 + 1;
25867 return n;
25870 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25871 next token), return index of the first token after the standard C++11
25872 attribute tokens, or N on failure. */
25874 static size_t
25875 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25877 while (true)
25879 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25880 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25882 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25883 if (n2 == n + 1)
25884 break;
25885 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25886 break;
25887 n = n2 + 1;
25889 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25890 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25892 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25893 if (n2 == n + 1)
25894 break;
25895 n = n2;
25897 else
25898 break;
25900 return n;
25903 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25904 as the next token), return index of the first token after the attribute
25905 tokens, or N on failure. */
25907 static size_t
25908 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25910 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25911 return cp_parser_skip_gnu_attributes_opt (parser, n);
25912 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25915 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25916 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25917 current value of the PEDANTIC flag, regardless of whether or not
25918 the `__extension__' keyword is present. The caller is responsible
25919 for restoring the value of the PEDANTIC flag. */
25921 static bool
25922 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25924 /* Save the old value of the PEDANTIC flag. */
25925 *saved_pedantic = pedantic;
25927 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25929 /* Consume the `__extension__' token. */
25930 cp_lexer_consume_token (parser->lexer);
25931 /* We're not being pedantic while the `__extension__' keyword is
25932 in effect. */
25933 pedantic = 0;
25935 return true;
25938 return false;
25941 /* Parse a label declaration.
25943 label-declaration:
25944 __label__ label-declarator-seq ;
25946 label-declarator-seq:
25947 identifier , label-declarator-seq
25948 identifier */
25950 static void
25951 cp_parser_label_declaration (cp_parser* parser)
25953 /* Look for the `__label__' keyword. */
25954 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25956 while (true)
25958 tree identifier;
25960 /* Look for an identifier. */
25961 identifier = cp_parser_identifier (parser);
25962 /* If we failed, stop. */
25963 if (identifier == error_mark_node)
25964 break;
25965 /* Declare it as a label. */
25966 finish_label_decl (identifier);
25967 /* If the next token is a `;', stop. */
25968 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25969 break;
25970 /* Look for the `,' separating the label declarations. */
25971 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25974 /* Look for the final `;'. */
25975 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25978 // -------------------------------------------------------------------------- //
25979 // Requires Clause
25981 // Parse a requires clause.
25983 // requires-clause:
25984 // 'requires' logical-or-expression
25986 // The required logical-or-expression must be a constant expression. Note
25987 // that we don't check that the expression is constepxr here. We defer until
25988 // we analyze constraints and then, we only check atomic constraints.
25989 static tree
25990 cp_parser_requires_clause (cp_parser *parser)
25992 // Parse the requires clause so that it is not automatically folded.
25993 ++processing_template_decl;
25994 tree expr = cp_parser_binary_expression (parser, false, false,
25995 PREC_NOT_OPERATOR, NULL);
25996 if (check_for_bare_parameter_packs (expr))
25997 expr = error_mark_node;
25998 --processing_template_decl;
25999 return expr;
26002 // Optionally parse a requires clause:
26003 static tree
26004 cp_parser_requires_clause_opt (cp_parser *parser)
26006 cp_token *tok = cp_lexer_peek_token (parser->lexer);
26007 if (tok->keyword != RID_REQUIRES)
26009 if (!flag_concepts && tok->type == CPP_NAME
26010 && tok->u.value == ridpointers[RID_REQUIRES])
26012 error_at (cp_lexer_peek_token (parser->lexer)->location,
26013 "%<requires%> only available with -fconcepts");
26014 /* Parse and discard the requires-clause. */
26015 cp_lexer_consume_token (parser->lexer);
26016 cp_parser_requires_clause (parser);
26018 return NULL_TREE;
26020 cp_lexer_consume_token (parser->lexer);
26021 return cp_parser_requires_clause (parser);
26025 /*---------------------------------------------------------------------------
26026 Requires expressions
26027 ---------------------------------------------------------------------------*/
26029 /* Parse a requires expression
26031 requirement-expression:
26032 'requires' requirement-parameter-list [opt] requirement-body */
26033 static tree
26034 cp_parser_requires_expression (cp_parser *parser)
26036 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26037 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26039 /* A requires-expression shall appear only within a concept
26040 definition or a requires-clause.
26042 TODO: Implement this diagnostic correctly. */
26043 if (!processing_template_decl)
26045 error_at (loc, "a requires expression cannot appear outside a template");
26046 cp_parser_skip_to_end_of_statement (parser);
26047 return error_mark_node;
26050 tree parms, reqs;
26052 /* Local parameters are delared as variables within the scope
26053 of the expression. They are not visible past the end of
26054 the expression. Expressions within the requires-expression
26055 are unevaluated. */
26056 struct scope_sentinel
26058 scope_sentinel ()
26060 ++cp_unevaluated_operand;
26061 begin_scope (sk_block, NULL_TREE);
26064 ~scope_sentinel ()
26066 pop_bindings_and_leave_scope ();
26067 --cp_unevaluated_operand;
26069 } s;
26071 /* Parse the optional parameter list. */
26072 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26074 parms = cp_parser_requirement_parameter_list (parser);
26075 if (parms == error_mark_node)
26076 return error_mark_node;
26078 else
26079 parms = NULL_TREE;
26081 /* Parse the requirement body. */
26082 reqs = cp_parser_requirement_body (parser);
26083 if (reqs == error_mark_node)
26084 return error_mark_node;
26087 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26088 the parm chain. */
26089 grokparms (parms, &parms);
26090 return finish_requires_expr (parms, reqs);
26093 /* Parse a parameterized requirement.
26095 requirement-parameter-list:
26096 '(' parameter-declaration-clause ')' */
26097 static tree
26098 cp_parser_requirement_parameter_list (cp_parser *parser)
26100 matching_parens parens;
26101 if (!parens.require_open (parser))
26102 return error_mark_node;
26104 tree parms = cp_parser_parameter_declaration_clause (parser);
26106 if (!parens.require_close (parser))
26107 return error_mark_node;
26109 return parms;
26112 /* Parse the body of a requirement.
26114 requirement-body:
26115 '{' requirement-list '}' */
26116 static tree
26117 cp_parser_requirement_body (cp_parser *parser)
26119 matching_braces braces;
26120 if (!braces.require_open (parser))
26121 return error_mark_node;
26123 tree reqs = cp_parser_requirement_list (parser);
26125 if (!braces.require_close (parser))
26126 return error_mark_node;
26128 return reqs;
26131 /* Parse a list of requirements.
26133 requirement-list:
26134 requirement
26135 requirement-list ';' requirement[opt] */
26136 static tree
26137 cp_parser_requirement_list (cp_parser *parser)
26139 tree result = NULL_TREE;
26140 while (true)
26142 tree req = cp_parser_requirement (parser);
26143 if (req == error_mark_node)
26144 return error_mark_node;
26146 result = tree_cons (NULL_TREE, req, result);
26148 /* If we see a semi-colon, consume it. */
26149 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26150 cp_lexer_consume_token (parser->lexer);
26152 /* Stop processing at the end of the list. */
26153 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26154 break;
26157 /* Reverse the order of requirements so they are analyzed in
26158 declaration order. */
26159 return nreverse (result);
26162 /* Parse a syntactic requirement or type requirement.
26164 requirement:
26165 simple-requirement
26166 compound-requirement
26167 type-requirement
26168 nested-requirement */
26169 static tree
26170 cp_parser_requirement (cp_parser *parser)
26172 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26173 return cp_parser_compound_requirement (parser);
26174 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26175 return cp_parser_type_requirement (parser);
26176 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26177 return cp_parser_nested_requirement (parser);
26178 else
26179 return cp_parser_simple_requirement (parser);
26182 /* Parse a simple requirement.
26184 simple-requirement:
26185 expression ';' */
26186 static tree
26187 cp_parser_simple_requirement (cp_parser *parser)
26189 tree expr = cp_parser_expression (parser, NULL, false, false);
26190 if (!expr || expr == error_mark_node)
26191 return error_mark_node;
26193 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26194 return error_mark_node;
26196 return finish_simple_requirement (expr);
26199 /* Parse a type requirement
26201 type-requirement
26202 nested-name-specifier [opt] required-type-name ';'
26204 required-type-name:
26205 type-name
26206 'template' [opt] simple-template-id */
26207 static tree
26208 cp_parser_type_requirement (cp_parser *parser)
26210 cp_lexer_consume_token (parser->lexer);
26212 // Save the scope before parsing name specifiers.
26213 tree saved_scope = parser->scope;
26214 tree saved_object_scope = parser->object_scope;
26215 tree saved_qualifying_scope = parser->qualifying_scope;
26216 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26217 cp_parser_nested_name_specifier_opt (parser,
26218 /*typename_keyword_p=*/true,
26219 /*check_dependency_p=*/false,
26220 /*type_p=*/true,
26221 /*is_declaration=*/false);
26223 tree type;
26224 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26226 cp_lexer_consume_token (parser->lexer);
26227 type = cp_parser_template_id (parser,
26228 /*template_keyword_p=*/true,
26229 /*check_dependency=*/false,
26230 /*tag_type=*/none_type,
26231 /*is_declaration=*/false);
26232 type = make_typename_type (parser->scope, type, typename_type,
26233 /*complain=*/tf_error);
26235 else
26236 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26238 if (TREE_CODE (type) == TYPE_DECL)
26239 type = TREE_TYPE (type);
26241 parser->scope = saved_scope;
26242 parser->object_scope = saved_object_scope;
26243 parser->qualifying_scope = saved_qualifying_scope;
26245 if (type == error_mark_node)
26246 cp_parser_skip_to_end_of_statement (parser);
26248 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26249 return error_mark_node;
26250 if (type == error_mark_node)
26251 return error_mark_node;
26253 return finish_type_requirement (type);
26256 /* Parse a compound requirement
26258 compound-requirement:
26259 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26260 static tree
26261 cp_parser_compound_requirement (cp_parser *parser)
26263 /* Parse an expression enclosed in '{ }'s. */
26264 matching_braces braces;
26265 if (!braces.require_open (parser))
26266 return error_mark_node;
26268 tree expr = cp_parser_expression (parser, NULL, false, false);
26269 if (!expr || expr == error_mark_node)
26270 return error_mark_node;
26272 if (!braces.require_close (parser))
26273 return error_mark_node;
26275 /* Parse the optional noexcept. */
26276 bool noexcept_p = false;
26277 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26279 cp_lexer_consume_token (parser->lexer);
26280 noexcept_p = true;
26283 /* Parse the optional trailing return type. */
26284 tree type = NULL_TREE;
26285 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26287 cp_lexer_consume_token (parser->lexer);
26288 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26289 parser->in_result_type_constraint_p = true;
26290 type = cp_parser_trailing_type_id (parser);
26291 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26292 if (type == error_mark_node)
26293 return error_mark_node;
26296 return finish_compound_requirement (expr, type, noexcept_p);
26299 /* Parse a nested requirement. This is the same as a requires clause.
26301 nested-requirement:
26302 requires-clause */
26303 static tree
26304 cp_parser_nested_requirement (cp_parser *parser)
26306 cp_lexer_consume_token (parser->lexer);
26307 tree req = cp_parser_requires_clause (parser);
26308 if (req == error_mark_node)
26309 return error_mark_node;
26310 return finish_nested_requirement (req);
26313 /* Support Functions */
26315 /* Return the appropriate prefer_type argument for lookup_name_real based on
26316 tag_type and template_mem_access. */
26318 static inline int
26319 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26321 /* DR 141: When looking in the current enclosing context for a template-name
26322 after -> or ., only consider class templates. */
26323 if (template_mem_access)
26324 return 2;
26325 switch (tag_type)
26327 case none_type: return 0; // No preference.
26328 case scope_type: return 1; // Type or namespace.
26329 default: return 2; // Type only.
26333 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26334 NAME should have one of the representations used for an
26335 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26336 is returned. If PARSER->SCOPE is a dependent type, then a
26337 SCOPE_REF is returned.
26339 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26340 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26341 was formed. Abstractly, such entities should not be passed to this
26342 function, because they do not need to be looked up, but it is
26343 simpler to check for this special case here, rather than at the
26344 call-sites.
26346 In cases not explicitly covered above, this function returns a
26347 DECL, OVERLOAD, or baselink representing the result of the lookup.
26348 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26349 is returned.
26351 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26352 (e.g., "struct") that was used. In that case bindings that do not
26353 refer to types are ignored.
26355 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26356 ignored.
26358 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26359 are ignored.
26361 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26362 types.
26364 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26365 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26366 NULL_TREE otherwise. */
26368 static cp_expr
26369 cp_parser_lookup_name (cp_parser *parser, tree name,
26370 enum tag_types tag_type,
26371 bool is_template,
26372 bool is_namespace,
26373 bool check_dependency,
26374 tree *ambiguous_decls,
26375 location_t name_location)
26377 tree decl;
26378 tree object_type = parser->context->object_type;
26380 /* Assume that the lookup will be unambiguous. */
26381 if (ambiguous_decls)
26382 *ambiguous_decls = NULL_TREE;
26384 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26385 no longer valid. Note that if we are parsing tentatively, and
26386 the parse fails, OBJECT_TYPE will be automatically restored. */
26387 parser->context->object_type = NULL_TREE;
26389 if (name == error_mark_node)
26390 return error_mark_node;
26392 /* A template-id has already been resolved; there is no lookup to
26393 do. */
26394 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26395 return name;
26396 if (BASELINK_P (name))
26398 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26399 == TEMPLATE_ID_EXPR);
26400 return name;
26403 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26404 it should already have been checked to make sure that the name
26405 used matches the type being destroyed. */
26406 if (TREE_CODE (name) == BIT_NOT_EXPR)
26408 tree type;
26410 /* Figure out to which type this destructor applies. */
26411 if (parser->scope)
26412 type = parser->scope;
26413 else if (object_type)
26414 type = object_type;
26415 else
26416 type = current_class_type;
26417 /* If that's not a class type, there is no destructor. */
26418 if (!type || !CLASS_TYPE_P (type))
26419 return error_mark_node;
26421 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26422 lazily_declare_fn (sfk_destructor, type);
26424 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26425 return dtor;
26427 return error_mark_node;
26430 /* By this point, the NAME should be an ordinary identifier. If
26431 the id-expression was a qualified name, the qualifying scope is
26432 stored in PARSER->SCOPE at this point. */
26433 gcc_assert (identifier_p (name));
26435 /* Perform the lookup. */
26436 if (parser->scope)
26438 bool dependent_p;
26440 if (parser->scope == error_mark_node)
26441 return error_mark_node;
26443 /* If the SCOPE is dependent, the lookup must be deferred until
26444 the template is instantiated -- unless we are explicitly
26445 looking up names in uninstantiated templates. Even then, we
26446 cannot look up the name if the scope is not a class type; it
26447 might, for example, be a template type parameter. */
26448 dependent_p = (TYPE_P (parser->scope)
26449 && dependent_scope_p (parser->scope));
26450 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26451 && dependent_p)
26452 /* Defer lookup. */
26453 decl = error_mark_node;
26454 else
26456 tree pushed_scope = NULL_TREE;
26458 /* If PARSER->SCOPE is a dependent type, then it must be a
26459 class type, and we must not be checking dependencies;
26460 otherwise, we would have processed this lookup above. So
26461 that PARSER->SCOPE is not considered a dependent base by
26462 lookup_member, we must enter the scope here. */
26463 if (dependent_p)
26464 pushed_scope = push_scope (parser->scope);
26466 /* If the PARSER->SCOPE is a template specialization, it
26467 may be instantiated during name lookup. In that case,
26468 errors may be issued. Even if we rollback the current
26469 tentative parse, those errors are valid. */
26470 decl = lookup_qualified_name (parser->scope, name,
26471 prefer_type_arg (tag_type),
26472 /*complain=*/true);
26474 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26475 lookup result and the nested-name-specifier nominates a class C:
26476 * if the name specified after the nested-name-specifier, when
26477 looked up in C, is the injected-class-name of C (Clause 9), or
26478 * if the name specified after the nested-name-specifier is the
26479 same as the identifier or the simple-template-id's template-
26480 name in the last component of the nested-name-specifier,
26481 the name is instead considered to name the constructor of
26482 class C. [ Note: for example, the constructor is not an
26483 acceptable lookup result in an elaborated-type-specifier so
26484 the constructor would not be used in place of the
26485 injected-class-name. --end note ] Such a constructor name
26486 shall be used only in the declarator-id of a declaration that
26487 names a constructor or in a using-declaration. */
26488 if (tag_type == none_type
26489 && DECL_SELF_REFERENCE_P (decl)
26490 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26491 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26492 prefer_type_arg (tag_type),
26493 /*complain=*/true);
26495 /* If we have a single function from a using decl, pull it out. */
26496 if (TREE_CODE (decl) == OVERLOAD
26497 && !really_overloaded_fn (decl))
26498 decl = OVL_FUNCTION (decl);
26500 if (pushed_scope)
26501 pop_scope (pushed_scope);
26504 /* If the scope is a dependent type and either we deferred lookup or
26505 we did lookup but didn't find the name, rememeber the name. */
26506 if (decl == error_mark_node && TYPE_P (parser->scope)
26507 && dependent_type_p (parser->scope))
26509 if (tag_type)
26511 tree type;
26513 /* The resolution to Core Issue 180 says that `struct
26514 A::B' should be considered a type-name, even if `A'
26515 is dependent. */
26516 type = make_typename_type (parser->scope, name, tag_type,
26517 /*complain=*/tf_error);
26518 if (type != error_mark_node)
26519 decl = TYPE_NAME (type);
26521 else if (is_template
26522 && (cp_parser_next_token_ends_template_argument_p (parser)
26523 || cp_lexer_next_token_is (parser->lexer,
26524 CPP_CLOSE_PAREN)))
26525 decl = make_unbound_class_template (parser->scope,
26526 name, NULL_TREE,
26527 /*complain=*/tf_error);
26528 else
26529 decl = build_qualified_name (/*type=*/NULL_TREE,
26530 parser->scope, name,
26531 is_template);
26533 parser->qualifying_scope = parser->scope;
26534 parser->object_scope = NULL_TREE;
26536 else if (object_type)
26538 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26539 OBJECT_TYPE is not a class. */
26540 if (CLASS_TYPE_P (object_type))
26541 /* If the OBJECT_TYPE is a template specialization, it may
26542 be instantiated during name lookup. In that case, errors
26543 may be issued. Even if we rollback the current tentative
26544 parse, those errors are valid. */
26545 decl = lookup_member (object_type,
26546 name,
26547 /*protect=*/0,
26548 prefer_type_arg (tag_type),
26549 tf_warning_or_error);
26550 else
26551 decl = NULL_TREE;
26553 if (!decl)
26554 /* Look it up in the enclosing context. DR 141: When looking for a
26555 template-name after -> or ., only consider class templates. */
26556 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26557 /*nonclass=*/0,
26558 /*block_p=*/true, is_namespace, 0);
26559 if (object_type == unknown_type_node)
26560 /* The object is type-dependent, so we can't look anything up; we used
26561 this to get the DR 141 behavior. */
26562 object_type = NULL_TREE;
26563 parser->object_scope = object_type;
26564 parser->qualifying_scope = NULL_TREE;
26566 else
26568 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26569 /*nonclass=*/0,
26570 /*block_p=*/true, is_namespace, 0);
26571 parser->qualifying_scope = NULL_TREE;
26572 parser->object_scope = NULL_TREE;
26575 /* If the lookup failed, let our caller know. */
26576 if (!decl || decl == error_mark_node)
26577 return error_mark_node;
26579 /* Pull out the template from an injected-class-name (or multiple). */
26580 if (is_template)
26581 decl = maybe_get_template_decl_from_type_decl (decl);
26583 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26584 if (TREE_CODE (decl) == TREE_LIST)
26586 if (ambiguous_decls)
26587 *ambiguous_decls = decl;
26588 /* The error message we have to print is too complicated for
26589 cp_parser_error, so we incorporate its actions directly. */
26590 if (!cp_parser_simulate_error (parser))
26592 error_at (name_location, "reference to %qD is ambiguous",
26593 name);
26594 print_candidates (decl);
26596 return error_mark_node;
26599 gcc_assert (DECL_P (decl)
26600 || TREE_CODE (decl) == OVERLOAD
26601 || TREE_CODE (decl) == SCOPE_REF
26602 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26603 || BASELINK_P (decl));
26605 /* If we have resolved the name of a member declaration, check to
26606 see if the declaration is accessible. When the name resolves to
26607 set of overloaded functions, accessibility is checked when
26608 overload resolution is done.
26610 During an explicit instantiation, access is not checked at all,
26611 as per [temp.explicit]. */
26612 if (DECL_P (decl))
26613 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26615 maybe_record_typedef_use (decl);
26617 return cp_expr (decl, name_location);
26620 /* Like cp_parser_lookup_name, but for use in the typical case where
26621 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26622 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26624 static tree
26625 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26627 return cp_parser_lookup_name (parser, name,
26628 none_type,
26629 /*is_template=*/false,
26630 /*is_namespace=*/false,
26631 /*check_dependency=*/true,
26632 /*ambiguous_decls=*/NULL,
26633 location);
26636 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26637 the current context, return the TYPE_DECL. If TAG_NAME_P is
26638 true, the DECL indicates the class being defined in a class-head,
26639 or declared in an elaborated-type-specifier.
26641 Otherwise, return DECL. */
26643 static tree
26644 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26646 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26647 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26649 struct A {
26650 template <typename T> struct B;
26653 template <typename T> struct A::B {};
26655 Similarly, in an elaborated-type-specifier:
26657 namespace N { struct X{}; }
26659 struct A {
26660 template <typename T> friend struct N::X;
26663 However, if the DECL refers to a class type, and we are in
26664 the scope of the class, then the name lookup automatically
26665 finds the TYPE_DECL created by build_self_reference rather
26666 than a TEMPLATE_DECL. For example, in:
26668 template <class T> struct S {
26669 S s;
26672 there is no need to handle such case. */
26674 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26675 return DECL_TEMPLATE_RESULT (decl);
26677 return decl;
26680 /* If too many, or too few, template-parameter lists apply to the
26681 declarator, issue an error message. Returns TRUE if all went well,
26682 and FALSE otherwise. */
26684 static bool
26685 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26686 cp_declarator *declarator,
26687 location_t declarator_location)
26689 switch (declarator->kind)
26691 case cdk_id:
26693 unsigned num_templates = 0;
26694 tree scope = declarator->u.id.qualifying_scope;
26695 bool template_id_p = false;
26697 if (scope)
26698 num_templates = num_template_headers_for_class (scope);
26699 else if (TREE_CODE (declarator->u.id.unqualified_name)
26700 == TEMPLATE_ID_EXPR)
26702 /* If the DECLARATOR has the form `X<y>' then it uses one
26703 additional level of template parameters. */
26704 ++num_templates;
26705 template_id_p = true;
26708 return cp_parser_check_template_parameters
26709 (parser, num_templates, template_id_p, declarator_location,
26710 declarator);
26713 case cdk_function:
26714 case cdk_array:
26715 case cdk_pointer:
26716 case cdk_reference:
26717 case cdk_ptrmem:
26718 return (cp_parser_check_declarator_template_parameters
26719 (parser, declarator->declarator, declarator_location));
26721 case cdk_decomp:
26722 case cdk_error:
26723 return true;
26725 default:
26726 gcc_unreachable ();
26728 return false;
26731 /* NUM_TEMPLATES were used in the current declaration. If that is
26732 invalid, return FALSE and issue an error messages. Otherwise,
26733 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26734 declarator and we can print more accurate diagnostics. */
26736 static bool
26737 cp_parser_check_template_parameters (cp_parser* parser,
26738 unsigned num_templates,
26739 bool template_id_p,
26740 location_t location,
26741 cp_declarator *declarator)
26743 /* If there are the same number of template classes and parameter
26744 lists, that's OK. */
26745 if (parser->num_template_parameter_lists == num_templates)
26746 return true;
26747 /* If there are more, but only one more, and the name ends in an identifier,
26748 then we are declaring a primary template. That's OK too. */
26749 if (!template_id_p
26750 && parser->num_template_parameter_lists == num_templates + 1)
26751 return true;
26752 /* If there are more template classes than parameter lists, we have
26753 something like:
26755 template <class T> void S<T>::R<T>::f (); */
26756 if (parser->num_template_parameter_lists < num_templates)
26758 if (declarator && !current_function_decl)
26759 error_at (location, "specializing member %<%T::%E%> "
26760 "requires %<template<>%> syntax",
26761 declarator->u.id.qualifying_scope,
26762 declarator->u.id.unqualified_name);
26763 else if (declarator)
26764 error_at (location, "invalid declaration of %<%T::%E%>",
26765 declarator->u.id.qualifying_scope,
26766 declarator->u.id.unqualified_name);
26767 else
26768 error_at (location, "too few template-parameter-lists");
26769 return false;
26771 /* Otherwise, there are too many template parameter lists. We have
26772 something like:
26774 template <class T> template <class U> void S::f(); */
26775 error_at (location, "too many template-parameter-lists");
26776 return false;
26779 /* Parse an optional `::' token indicating that the following name is
26780 from the global namespace. If so, PARSER->SCOPE is set to the
26781 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26782 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26783 Returns the new value of PARSER->SCOPE, if the `::' token is
26784 present, and NULL_TREE otherwise. */
26786 static tree
26787 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26789 cp_token *token;
26791 /* Peek at the next token. */
26792 token = cp_lexer_peek_token (parser->lexer);
26793 /* If we're looking at a `::' token then we're starting from the
26794 global namespace, not our current location. */
26795 if (token->type == CPP_SCOPE)
26797 /* Consume the `::' token. */
26798 cp_lexer_consume_token (parser->lexer);
26799 /* Set the SCOPE so that we know where to start the lookup. */
26800 parser->scope = global_namespace;
26801 parser->qualifying_scope = global_namespace;
26802 parser->object_scope = NULL_TREE;
26804 return parser->scope;
26806 else if (!current_scope_valid_p)
26808 parser->scope = NULL_TREE;
26809 parser->qualifying_scope = NULL_TREE;
26810 parser->object_scope = NULL_TREE;
26813 return NULL_TREE;
26816 /* Returns TRUE if the upcoming token sequence is the start of a
26817 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26818 declarator is preceded by the `friend' specifier. */
26820 static bool
26821 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26823 bool constructor_p;
26824 bool outside_class_specifier_p;
26825 tree nested_name_specifier;
26826 cp_token *next_token;
26828 /* The common case is that this is not a constructor declarator, so
26829 try to avoid doing lots of work if at all possible. It's not
26830 valid declare a constructor at function scope. */
26831 if (parser->in_function_body)
26832 return false;
26833 /* And only certain tokens can begin a constructor declarator. */
26834 next_token = cp_lexer_peek_token (parser->lexer);
26835 if (next_token->type != CPP_NAME
26836 && next_token->type != CPP_SCOPE
26837 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26838 && next_token->type != CPP_TEMPLATE_ID)
26839 return false;
26841 /* Parse tentatively; we are going to roll back all of the tokens
26842 consumed here. */
26843 cp_parser_parse_tentatively (parser);
26844 /* Assume that we are looking at a constructor declarator. */
26845 constructor_p = true;
26847 /* Look for the optional `::' operator. */
26848 cp_parser_global_scope_opt (parser,
26849 /*current_scope_valid_p=*/false);
26850 /* Look for the nested-name-specifier. */
26851 nested_name_specifier
26852 = (cp_parser_nested_name_specifier_opt (parser,
26853 /*typename_keyword_p=*/false,
26854 /*check_dependency_p=*/false,
26855 /*type_p=*/false,
26856 /*is_declaration=*/false));
26858 outside_class_specifier_p = (!at_class_scope_p ()
26859 || !TYPE_BEING_DEFINED (current_class_type)
26860 || friend_p);
26862 /* Outside of a class-specifier, there must be a
26863 nested-name-specifier. Except in C++17 mode, where we
26864 might be declaring a guiding declaration. */
26865 if (!nested_name_specifier && outside_class_specifier_p
26866 && cxx_dialect < cxx17)
26867 constructor_p = false;
26868 else if (nested_name_specifier == error_mark_node)
26869 constructor_p = false;
26871 /* If we have a class scope, this is easy; DR 147 says that S::S always
26872 names the constructor, and no other qualified name could. */
26873 if (constructor_p && nested_name_specifier
26874 && CLASS_TYPE_P (nested_name_specifier))
26876 tree id = cp_parser_unqualified_id (parser,
26877 /*template_keyword_p=*/false,
26878 /*check_dependency_p=*/false,
26879 /*declarator_p=*/true,
26880 /*optional_p=*/false);
26881 if (is_overloaded_fn (id))
26882 id = DECL_NAME (get_first_fn (id));
26883 if (!constructor_name_p (id, nested_name_specifier))
26884 constructor_p = false;
26886 /* If we still think that this might be a constructor-declarator,
26887 look for a class-name. */
26888 else if (constructor_p)
26890 /* If we have:
26892 template <typename T> struct S {
26893 S();
26896 we must recognize that the nested `S' names a class. */
26897 if (cxx_dialect >= cxx17)
26898 cp_parser_parse_tentatively (parser);
26900 tree type_decl;
26901 type_decl = cp_parser_class_name (parser,
26902 /*typename_keyword_p=*/false,
26903 /*template_keyword_p=*/false,
26904 none_type,
26905 /*check_dependency_p=*/false,
26906 /*class_head_p=*/false,
26907 /*is_declaration=*/false);
26909 if (cxx_dialect >= cxx17
26910 && !cp_parser_parse_definitely (parser))
26912 type_decl = NULL_TREE;
26913 tree tmpl = cp_parser_template_name (parser,
26914 /*template_keyword*/false,
26915 /*check_dependency_p*/false,
26916 /*is_declaration*/false,
26917 none_type,
26918 /*is_identifier*/NULL);
26919 if (DECL_CLASS_TEMPLATE_P (tmpl)
26920 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26921 /* It's a deduction guide, return true. */;
26922 else
26923 cp_parser_simulate_error (parser);
26926 /* If there was no class-name, then this is not a constructor.
26927 Otherwise, if we are in a class-specifier and we aren't
26928 handling a friend declaration, check that its type matches
26929 current_class_type (c++/38313). Note: error_mark_node
26930 is left alone for error recovery purposes. */
26931 constructor_p = (!cp_parser_error_occurred (parser)
26932 && (outside_class_specifier_p
26933 || type_decl == NULL_TREE
26934 || type_decl == error_mark_node
26935 || same_type_p (current_class_type,
26936 TREE_TYPE (type_decl))));
26938 /* If we're still considering a constructor, we have to see a `(',
26939 to begin the parameter-declaration-clause, followed by either a
26940 `)', an `...', or a decl-specifier. We need to check for a
26941 type-specifier to avoid being fooled into thinking that:
26943 S (f) (int);
26945 is a constructor. (It is actually a function named `f' that
26946 takes one parameter (of type `int') and returns a value of type
26947 `S'. */
26948 if (constructor_p
26949 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26950 constructor_p = false;
26952 if (constructor_p
26953 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26954 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26955 /* A parameter declaration begins with a decl-specifier,
26956 which is either the "attribute" keyword, a storage class
26957 specifier, or (usually) a type-specifier. */
26958 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26960 tree type;
26961 tree pushed_scope = NULL_TREE;
26962 unsigned saved_num_template_parameter_lists;
26964 /* Names appearing in the type-specifier should be looked up
26965 in the scope of the class. */
26966 if (current_class_type)
26967 type = NULL_TREE;
26968 else if (type_decl)
26970 type = TREE_TYPE (type_decl);
26971 if (TREE_CODE (type) == TYPENAME_TYPE)
26973 type = resolve_typename_type (type,
26974 /*only_current_p=*/false);
26975 if (TREE_CODE (type) == TYPENAME_TYPE)
26977 cp_parser_abort_tentative_parse (parser);
26978 return false;
26981 pushed_scope = push_scope (type);
26984 /* Inside the constructor parameter list, surrounding
26985 template-parameter-lists do not apply. */
26986 saved_num_template_parameter_lists
26987 = parser->num_template_parameter_lists;
26988 parser->num_template_parameter_lists = 0;
26990 /* Look for the type-specifier. */
26991 cp_parser_type_specifier (parser,
26992 CP_PARSER_FLAGS_NONE,
26993 /*decl_specs=*/NULL,
26994 /*is_declarator=*/true,
26995 /*declares_class_or_enum=*/NULL,
26996 /*is_cv_qualifier=*/NULL);
26998 parser->num_template_parameter_lists
26999 = saved_num_template_parameter_lists;
27001 /* Leave the scope of the class. */
27002 if (pushed_scope)
27003 pop_scope (pushed_scope);
27005 constructor_p = !cp_parser_error_occurred (parser);
27009 /* We did not really want to consume any tokens. */
27010 cp_parser_abort_tentative_parse (parser);
27012 return constructor_p;
27015 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27016 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27017 they must be performed once we are in the scope of the function.
27019 Returns the function defined. */
27021 static tree
27022 cp_parser_function_definition_from_specifiers_and_declarator
27023 (cp_parser* parser,
27024 cp_decl_specifier_seq *decl_specifiers,
27025 tree attributes,
27026 const cp_declarator *declarator)
27028 tree fn;
27029 bool success_p;
27031 /* Begin the function-definition. */
27032 success_p = start_function (decl_specifiers, declarator, attributes);
27034 /* The things we're about to see are not directly qualified by any
27035 template headers we've seen thus far. */
27036 reset_specialization ();
27038 /* If there were names looked up in the decl-specifier-seq that we
27039 did not check, check them now. We must wait until we are in the
27040 scope of the function to perform the checks, since the function
27041 might be a friend. */
27042 perform_deferred_access_checks (tf_warning_or_error);
27044 if (success_p)
27046 cp_finalize_omp_declare_simd (parser, current_function_decl);
27047 parser->omp_declare_simd = NULL;
27048 cp_finalize_oacc_routine (parser, current_function_decl, true);
27049 parser->oacc_routine = NULL;
27052 if (!success_p)
27054 /* Skip the entire function. */
27055 cp_parser_skip_to_end_of_block_or_statement (parser);
27056 fn = error_mark_node;
27058 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27060 /* Seen already, skip it. An error message has already been output. */
27061 cp_parser_skip_to_end_of_block_or_statement (parser);
27062 fn = current_function_decl;
27063 current_function_decl = NULL_TREE;
27064 /* If this is a function from a class, pop the nested class. */
27065 if (current_class_name)
27066 pop_nested_class ();
27068 else
27070 timevar_id_t tv;
27071 if (DECL_DECLARED_INLINE_P (current_function_decl))
27072 tv = TV_PARSE_INLINE;
27073 else
27074 tv = TV_PARSE_FUNC;
27075 timevar_push (tv);
27076 fn = cp_parser_function_definition_after_declarator (parser,
27077 /*inline_p=*/false);
27078 timevar_pop (tv);
27081 return fn;
27084 /* Parse the part of a function-definition that follows the
27085 declarator. INLINE_P is TRUE iff this function is an inline
27086 function defined within a class-specifier.
27088 Returns the function defined. */
27090 static tree
27091 cp_parser_function_definition_after_declarator (cp_parser* parser,
27092 bool inline_p)
27094 tree fn;
27095 bool saved_in_unbraced_linkage_specification_p;
27096 bool saved_in_function_body;
27097 unsigned saved_num_template_parameter_lists;
27098 cp_token *token;
27099 bool fully_implicit_function_template_p
27100 = parser->fully_implicit_function_template_p;
27101 parser->fully_implicit_function_template_p = false;
27102 tree implicit_template_parms
27103 = parser->implicit_template_parms;
27104 parser->implicit_template_parms = 0;
27105 cp_binding_level* implicit_template_scope
27106 = parser->implicit_template_scope;
27107 parser->implicit_template_scope = 0;
27109 saved_in_function_body = parser->in_function_body;
27110 parser->in_function_body = true;
27111 /* If the next token is `return', then the code may be trying to
27112 make use of the "named return value" extension that G++ used to
27113 support. */
27114 token = cp_lexer_peek_token (parser->lexer);
27115 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27117 /* Consume the `return' keyword. */
27118 cp_lexer_consume_token (parser->lexer);
27119 /* Look for the identifier that indicates what value is to be
27120 returned. */
27121 cp_parser_identifier (parser);
27122 /* Issue an error message. */
27123 error_at (token->location,
27124 "named return values are no longer supported");
27125 /* Skip tokens until we reach the start of the function body. */
27126 while (true)
27128 cp_token *token = cp_lexer_peek_token (parser->lexer);
27129 if (token->type == CPP_OPEN_BRACE
27130 || token->type == CPP_EOF
27131 || token->type == CPP_PRAGMA_EOL)
27132 break;
27133 cp_lexer_consume_token (parser->lexer);
27136 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27137 anything declared inside `f'. */
27138 saved_in_unbraced_linkage_specification_p
27139 = parser->in_unbraced_linkage_specification_p;
27140 parser->in_unbraced_linkage_specification_p = false;
27141 /* Inside the function, surrounding template-parameter-lists do not
27142 apply. */
27143 saved_num_template_parameter_lists
27144 = parser->num_template_parameter_lists;
27145 parser->num_template_parameter_lists = 0;
27147 /* If the next token is `try', `__transaction_atomic', or
27148 `__transaction_relaxed`, then we are looking at either function-try-block
27149 or function-transaction-block. Note that all of these include the
27150 function-body. */
27151 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27152 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27153 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27154 RID_TRANSACTION_RELAXED))
27155 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27156 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27157 cp_parser_function_try_block (parser);
27158 else
27159 cp_parser_ctor_initializer_opt_and_function_body
27160 (parser, /*in_function_try_block=*/false);
27162 /* Finish the function. */
27163 fn = finish_function (inline_p);
27164 /* Generate code for it, if necessary. */
27165 expand_or_defer_fn (fn);
27166 /* Restore the saved values. */
27167 parser->in_unbraced_linkage_specification_p
27168 = saved_in_unbraced_linkage_specification_p;
27169 parser->num_template_parameter_lists
27170 = saved_num_template_parameter_lists;
27171 parser->in_function_body = saved_in_function_body;
27173 parser->fully_implicit_function_template_p
27174 = fully_implicit_function_template_p;
27175 parser->implicit_template_parms
27176 = implicit_template_parms;
27177 parser->implicit_template_scope
27178 = implicit_template_scope;
27180 if (parser->fully_implicit_function_template_p)
27181 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27183 return fn;
27186 /* Parse a template-declaration body (following argument list). */
27188 static void
27189 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27190 tree parameter_list,
27191 bool member_p)
27193 tree decl = NULL_TREE;
27194 bool friend_p = false;
27196 /* We just processed one more parameter list. */
27197 ++parser->num_template_parameter_lists;
27199 /* Get the deferred access checks from the parameter list. These
27200 will be checked once we know what is being declared, as for a
27201 member template the checks must be performed in the scope of the
27202 class containing the member. */
27203 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27205 /* Tentatively parse for a new template parameter list, which can either be
27206 the template keyword or a template introduction. */
27207 if (cp_parser_template_declaration_after_export (parser, member_p))
27208 /* OK */;
27209 else if (cxx_dialect >= cxx11
27210 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27211 decl = cp_parser_alias_declaration (parser);
27212 else
27214 /* There are no access checks when parsing a template, as we do not
27215 know if a specialization will be a friend. */
27216 push_deferring_access_checks (dk_no_check);
27217 cp_token *token = cp_lexer_peek_token (parser->lexer);
27218 decl = cp_parser_single_declaration (parser,
27219 checks,
27220 member_p,
27221 /*explicit_specialization_p=*/false,
27222 &friend_p);
27223 pop_deferring_access_checks ();
27225 /* If this is a member template declaration, let the front
27226 end know. */
27227 if (member_p && !friend_p && decl)
27229 if (TREE_CODE (decl) == TYPE_DECL)
27230 cp_parser_check_access_in_redeclaration (decl, token->location);
27232 decl = finish_member_template_decl (decl);
27234 else if (friend_p && decl
27235 && DECL_DECLARES_TYPE_P (decl))
27236 make_friend_class (current_class_type, TREE_TYPE (decl),
27237 /*complain=*/true);
27239 /* We are done with the current parameter list. */
27240 --parser->num_template_parameter_lists;
27242 pop_deferring_access_checks ();
27244 /* Finish up. */
27245 finish_template_decl (parameter_list);
27247 /* Check the template arguments for a literal operator template. */
27248 if (decl
27249 && DECL_DECLARES_FUNCTION_P (decl)
27250 && UDLIT_OPER_P (DECL_NAME (decl)))
27252 bool ok = true;
27253 if (parameter_list == NULL_TREE)
27254 ok = false;
27255 else
27257 int num_parms = TREE_VEC_LENGTH (parameter_list);
27258 if (num_parms == 1)
27260 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27261 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27262 if (CLASS_TYPE_P (TREE_TYPE (parm)))
27263 /* OK, C++20 string literal operator template. We don't need
27264 to warn in lower dialects here because we will have already
27265 warned about the template parameter. */;
27266 else if (TREE_TYPE (parm) != char_type_node
27267 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27268 ok = false;
27270 else if (num_parms == 2 && cxx_dialect >= cxx14)
27272 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27273 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27274 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27275 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27276 if (parm == error_mark_node
27277 || TREE_TYPE (parm) != TREE_TYPE (type)
27278 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27279 ok = false;
27280 else
27281 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27282 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
27283 "ISO C++ did not adopt string literal operator templa"
27284 "tes taking an argument pack of characters");
27286 else
27287 ok = false;
27289 if (!ok)
27291 if (cxx_dialect > cxx17)
27292 error ("literal operator template %qD has invalid parameter list;"
27293 " Expected non-type template parameter pack <char...> "
27294 " or single non-type parameter of class type",
27295 decl);
27296 else
27297 error ("literal operator template %qD has invalid parameter list."
27298 " Expected non-type template parameter pack <char...>",
27299 decl);
27303 /* Register member declarations. */
27304 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27305 finish_member_declaration (decl);
27306 /* If DECL is a function template, we must return to parse it later.
27307 (Even though there is no definition, there might be default
27308 arguments that need handling.) */
27309 if (member_p && decl
27310 && DECL_DECLARES_FUNCTION_P (decl))
27311 vec_safe_push (unparsed_funs_with_definitions, decl);
27314 /* Parse a template introduction header for a template-declaration. Returns
27315 false if tentative parse fails. */
27317 static bool
27318 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27320 cp_parser_parse_tentatively (parser);
27322 tree saved_scope = parser->scope;
27323 tree saved_object_scope = parser->object_scope;
27324 tree saved_qualifying_scope = parser->qualifying_scope;
27326 /* Look for the optional `::' operator. */
27327 cp_parser_global_scope_opt (parser,
27328 /*current_scope_valid_p=*/false);
27329 /* Look for the nested-name-specifier. */
27330 cp_parser_nested_name_specifier_opt (parser,
27331 /*typename_keyword_p=*/false,
27332 /*check_dependency_p=*/true,
27333 /*type_p=*/false,
27334 /*is_declaration=*/false);
27336 cp_token *token = cp_lexer_peek_token (parser->lexer);
27337 tree concept_name = cp_parser_identifier (parser);
27339 /* Look up the concept for which we will be matching
27340 template parameters. */
27341 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27342 token->location);
27343 parser->scope = saved_scope;
27344 parser->object_scope = saved_object_scope;
27345 parser->qualifying_scope = saved_qualifying_scope;
27347 if (concept_name == error_mark_node)
27348 cp_parser_simulate_error (parser);
27350 /* Look for opening brace for introduction. */
27351 matching_braces braces;
27352 braces.require_open (parser);
27354 if (!cp_parser_parse_definitely (parser))
27355 return false;
27357 push_deferring_access_checks (dk_deferred);
27359 /* Build vector of placeholder parameters and grab
27360 matching identifiers. */
27361 tree introduction_list = cp_parser_introduction_list (parser);
27363 /* Look for closing brace for introduction. */
27364 if (!braces.require_close (parser))
27365 return true;
27367 /* The introduction-list shall not be empty. */
27368 int nargs = TREE_VEC_LENGTH (introduction_list);
27369 if (nargs == 0)
27371 /* In cp_parser_introduction_list we have already issued an error. */
27372 return true;
27375 if (tmpl_decl == error_mark_node)
27377 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27378 token->location);
27379 return true;
27382 /* Build and associate the constraint. */
27383 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27384 if (parms && parms != error_mark_node)
27386 cp_parser_template_declaration_after_parameters (parser, parms,
27387 member_p);
27388 return true;
27391 error_at (token->location, "no matching concept for template-introduction");
27392 return true;
27395 /* Parse a normal template-declaration following the template keyword. */
27397 static void
27398 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27400 tree parameter_list;
27401 bool need_lang_pop;
27402 location_t location = input_location;
27404 /* Look for the `<' token. */
27405 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27406 return;
27407 if (at_class_scope_p () && current_function_decl)
27409 /* 14.5.2.2 [temp.mem]
27411 A local class shall not have member templates. */
27412 error_at (location,
27413 "invalid declaration of member template in local class");
27414 cp_parser_skip_to_end_of_block_or_statement (parser);
27415 return;
27417 /* [temp]
27419 A template ... shall not have C linkage. */
27420 if (current_lang_name == lang_name_c)
27422 error_at (location, "template with C linkage");
27423 maybe_show_extern_c_location ();
27424 /* Give it C++ linkage to avoid confusing other parts of the
27425 front end. */
27426 push_lang_context (lang_name_cplusplus);
27427 need_lang_pop = true;
27429 else
27430 need_lang_pop = false;
27432 /* We cannot perform access checks on the template parameter
27433 declarations until we know what is being declared, just as we
27434 cannot check the decl-specifier list. */
27435 push_deferring_access_checks (dk_deferred);
27437 /* If the next token is `>', then we have an invalid
27438 specialization. Rather than complain about an invalid template
27439 parameter, issue an error message here. */
27440 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27442 cp_parser_error (parser, "invalid explicit specialization");
27443 begin_specialization ();
27444 parameter_list = NULL_TREE;
27446 else
27448 /* Parse the template parameters. */
27449 parameter_list = cp_parser_template_parameter_list (parser);
27452 /* Look for the `>'. */
27453 cp_parser_skip_to_end_of_template_parameter_list (parser);
27455 /* Manage template requirements */
27456 if (flag_concepts)
27458 tree reqs = get_shorthand_constraints (current_template_parms);
27459 if (tree r = cp_parser_requires_clause_opt (parser))
27460 reqs = conjoin_constraints (reqs, normalize_expression (r));
27461 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27464 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27465 member_p);
27467 /* For the erroneous case of a template with C linkage, we pushed an
27468 implicit C++ linkage scope; exit that scope now. */
27469 if (need_lang_pop)
27470 pop_lang_context ();
27473 /* Parse a template-declaration, assuming that the `export' (and
27474 `extern') keywords, if present, has already been scanned. MEMBER_P
27475 is as for cp_parser_template_declaration. */
27477 static bool
27478 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27480 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27482 cp_lexer_consume_token (parser->lexer);
27483 cp_parser_explicit_template_declaration (parser, member_p);
27484 return true;
27486 else if (flag_concepts)
27487 return cp_parser_template_introduction (parser, member_p);
27489 return false;
27492 /* Perform the deferred access checks from a template-parameter-list.
27493 CHECKS is a TREE_LIST of access checks, as returned by
27494 get_deferred_access_checks. */
27496 static void
27497 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27499 ++processing_template_parmlist;
27500 perform_access_checks (checks, tf_warning_or_error);
27501 --processing_template_parmlist;
27504 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27505 `function-definition' sequence that follows a template header.
27506 If MEMBER_P is true, this declaration appears in a class scope.
27508 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27509 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27511 static tree
27512 cp_parser_single_declaration (cp_parser* parser,
27513 vec<deferred_access_check, va_gc> *checks,
27514 bool member_p,
27515 bool explicit_specialization_p,
27516 bool* friend_p)
27518 int declares_class_or_enum;
27519 tree decl = NULL_TREE;
27520 cp_decl_specifier_seq decl_specifiers;
27521 bool function_definition_p = false;
27522 cp_token *decl_spec_token_start;
27524 /* This function is only used when processing a template
27525 declaration. */
27526 gcc_assert (innermost_scope_kind () == sk_template_parms
27527 || innermost_scope_kind () == sk_template_spec);
27529 /* Defer access checks until we know what is being declared. */
27530 push_deferring_access_checks (dk_deferred);
27532 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27533 alternative. */
27534 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27535 cp_parser_decl_specifier_seq (parser,
27536 CP_PARSER_FLAGS_OPTIONAL,
27537 &decl_specifiers,
27538 &declares_class_or_enum);
27539 if (friend_p)
27540 *friend_p = cp_parser_friend_p (&decl_specifiers);
27542 /* There are no template typedefs. */
27543 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27545 error_at (decl_spec_token_start->location,
27546 "template declaration of %<typedef%>");
27547 decl = error_mark_node;
27550 /* Gather up the access checks that occurred the
27551 decl-specifier-seq. */
27552 stop_deferring_access_checks ();
27554 /* Check for the declaration of a template class. */
27555 if (declares_class_or_enum)
27557 if (cp_parser_declares_only_class_p (parser)
27558 || (declares_class_or_enum & 2))
27560 // If this is a declaration, but not a definition, associate
27561 // any constraints with the type declaration. Constraints
27562 // are associated with definitions in cp_parser_class_specifier.
27563 if (declares_class_or_enum == 1)
27564 associate_classtype_constraints (decl_specifiers.type);
27566 decl = shadow_tag (&decl_specifiers);
27568 /* In this case:
27570 struct C {
27571 friend template <typename T> struct A<T>::B;
27574 A<T>::B will be represented by a TYPENAME_TYPE, and
27575 therefore not recognized by shadow_tag. */
27576 if (friend_p && *friend_p
27577 && !decl
27578 && decl_specifiers.type
27579 && TYPE_P (decl_specifiers.type))
27580 decl = decl_specifiers.type;
27582 if (decl && decl != error_mark_node)
27583 decl = TYPE_NAME (decl);
27584 else
27585 decl = error_mark_node;
27587 /* Perform access checks for template parameters. */
27588 cp_parser_perform_template_parameter_access_checks (checks);
27590 /* Give a helpful diagnostic for
27591 template <class T> struct A { } a;
27592 if we aren't already recovering from an error. */
27593 if (!cp_parser_declares_only_class_p (parser)
27594 && !seen_error ())
27596 error_at (cp_lexer_peek_token (parser->lexer)->location,
27597 "a class template declaration must not declare "
27598 "anything else");
27599 cp_parser_skip_to_end_of_block_or_statement (parser);
27600 goto out;
27605 /* Complain about missing 'typename' or other invalid type names. */
27606 if (!decl_specifiers.any_type_specifiers_p
27607 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27609 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27610 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27611 the rest of this declaration. */
27612 decl = error_mark_node;
27613 goto out;
27616 /* If it's not a template class, try for a template function. If
27617 the next token is a `;', then this declaration does not declare
27618 anything. But, if there were errors in the decl-specifiers, then
27619 the error might well have come from an attempted class-specifier.
27620 In that case, there's no need to warn about a missing declarator. */
27621 if (!decl
27622 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27623 || decl_specifiers.type != error_mark_node))
27625 decl = cp_parser_init_declarator (parser,
27626 &decl_specifiers,
27627 checks,
27628 /*function_definition_allowed_p=*/true,
27629 member_p,
27630 declares_class_or_enum,
27631 &function_definition_p,
27632 NULL, NULL, NULL);
27634 /* 7.1.1-1 [dcl.stc]
27636 A storage-class-specifier shall not be specified in an explicit
27637 specialization... */
27638 if (decl
27639 && explicit_specialization_p
27640 && decl_specifiers.storage_class != sc_none)
27642 error_at (decl_spec_token_start->location,
27643 "explicit template specialization cannot have a storage class");
27644 decl = error_mark_node;
27647 if (decl && VAR_P (decl))
27648 check_template_variable (decl);
27651 /* Look for a trailing `;' after the declaration. */
27652 if (!function_definition_p
27653 && (decl == error_mark_node
27654 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27655 cp_parser_skip_to_end_of_block_or_statement (parser);
27657 out:
27658 pop_deferring_access_checks ();
27660 /* Clear any current qualification; whatever comes next is the start
27661 of something new. */
27662 parser->scope = NULL_TREE;
27663 parser->qualifying_scope = NULL_TREE;
27664 parser->object_scope = NULL_TREE;
27666 return decl;
27669 /* Parse a cast-expression that is not the operand of a unary "&". */
27671 static cp_expr
27672 cp_parser_simple_cast_expression (cp_parser *parser)
27674 return cp_parser_cast_expression (parser, /*address_p=*/false,
27675 /*cast_p=*/false, /*decltype*/false, NULL);
27678 /* Parse a functional cast to TYPE. Returns an expression
27679 representing the cast. */
27681 static cp_expr
27682 cp_parser_functional_cast (cp_parser* parser, tree type)
27684 vec<tree, va_gc> *vec;
27685 tree expression_list;
27686 cp_expr cast;
27687 bool nonconst_p;
27689 location_t start_loc = input_location;
27691 if (!type)
27692 type = error_mark_node;
27694 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27696 cp_lexer_set_source_position (parser->lexer);
27697 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27698 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27699 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27700 if (TREE_CODE (type) == TYPE_DECL)
27701 type = TREE_TYPE (type);
27703 cast = finish_compound_literal (type, expression_list,
27704 tf_warning_or_error, fcl_functional);
27705 /* Create a location of the form:
27706 type_name{i, f}
27707 ^~~~~~~~~~~~~~~
27708 with caret == start at the start of the type name,
27709 finishing at the closing brace. */
27710 location_t finish_loc
27711 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27712 location_t combined_loc = make_location (start_loc, start_loc,
27713 finish_loc);
27714 cast.set_location (combined_loc);
27715 return cast;
27719 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27720 /*cast_p=*/true,
27721 /*allow_expansion_p=*/true,
27722 /*non_constant_p=*/NULL);
27723 if (vec == NULL)
27724 expression_list = error_mark_node;
27725 else
27727 expression_list = build_tree_list_vec (vec);
27728 release_tree_vector (vec);
27731 cast = build_functional_cast (type, expression_list,
27732 tf_warning_or_error);
27733 /* [expr.const]/1: In an integral constant expression "only type
27734 conversions to integral or enumeration type can be used". */
27735 if (TREE_CODE (type) == TYPE_DECL)
27736 type = TREE_TYPE (type);
27737 if (cast != error_mark_node
27738 && !cast_valid_in_integral_constant_expression_p (type)
27739 && cp_parser_non_integral_constant_expression (parser,
27740 NIC_CONSTRUCTOR))
27741 return error_mark_node;
27743 /* Create a location of the form:
27744 float(i)
27745 ^~~~~~~~
27746 with caret == start at the start of the type name,
27747 finishing at the closing paren. */
27748 location_t finish_loc
27749 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27750 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27751 cast.set_location (combined_loc);
27752 return cast;
27755 /* Save the tokens that make up the body of a member function defined
27756 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27757 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27758 specifiers applied to the declaration. Returns the FUNCTION_DECL
27759 for the member function. */
27761 static tree
27762 cp_parser_save_member_function_body (cp_parser* parser,
27763 cp_decl_specifier_seq *decl_specifiers,
27764 cp_declarator *declarator,
27765 tree attributes)
27767 cp_token *first;
27768 cp_token *last;
27769 tree fn;
27770 bool function_try_block = false;
27772 /* Create the FUNCTION_DECL. */
27773 fn = grokmethod (decl_specifiers, declarator, attributes);
27774 cp_finalize_omp_declare_simd (parser, fn);
27775 cp_finalize_oacc_routine (parser, fn, true);
27776 /* If something went badly wrong, bail out now. */
27777 if (fn == error_mark_node)
27779 /* If there's a function-body, skip it. */
27780 if (cp_parser_token_starts_function_definition_p
27781 (cp_lexer_peek_token (parser->lexer)))
27782 cp_parser_skip_to_end_of_block_or_statement (parser);
27783 return error_mark_node;
27786 /* Remember it, if there default args to post process. */
27787 cp_parser_save_default_args (parser, fn);
27789 /* Save away the tokens that make up the body of the
27790 function. */
27791 first = parser->lexer->next_token;
27793 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27794 cp_lexer_consume_token (parser->lexer);
27795 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27796 RID_TRANSACTION_ATOMIC))
27798 cp_lexer_consume_token (parser->lexer);
27799 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27800 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27801 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27802 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27803 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27804 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27805 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27807 cp_lexer_consume_token (parser->lexer);
27808 cp_lexer_consume_token (parser->lexer);
27809 cp_lexer_consume_token (parser->lexer);
27810 cp_lexer_consume_token (parser->lexer);
27811 cp_lexer_consume_token (parser->lexer);
27813 else
27814 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27815 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27817 cp_lexer_consume_token (parser->lexer);
27818 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27819 break;
27823 /* Handle function try blocks. */
27824 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27826 cp_lexer_consume_token (parser->lexer);
27827 function_try_block = true;
27829 /* We can have braced-init-list mem-initializers before the fn body. */
27830 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27832 cp_lexer_consume_token (parser->lexer);
27833 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27835 /* cache_group will stop after an un-nested { } pair, too. */
27836 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27837 break;
27839 /* variadic mem-inits have ... after the ')'. */
27840 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27841 cp_lexer_consume_token (parser->lexer);
27844 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27845 /* Handle function try blocks. */
27846 if (function_try_block)
27847 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27848 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27849 last = parser->lexer->next_token;
27851 /* Save away the inline definition; we will process it when the
27852 class is complete. */
27853 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27854 DECL_PENDING_INLINE_P (fn) = 1;
27856 /* We need to know that this was defined in the class, so that
27857 friend templates are handled correctly. */
27858 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27860 /* Add FN to the queue of functions to be parsed later. */
27861 vec_safe_push (unparsed_funs_with_definitions, fn);
27863 return fn;
27866 /* Save the tokens that make up the in-class initializer for a non-static
27867 data member. Returns a DEFAULT_ARG. */
27869 static tree
27870 cp_parser_save_nsdmi (cp_parser* parser)
27872 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27875 /* Parse a template-argument-list, as well as the trailing ">" (but
27876 not the opening "<"). See cp_parser_template_argument_list for the
27877 return value. */
27879 static tree
27880 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27882 tree arguments;
27883 tree saved_scope;
27884 tree saved_qualifying_scope;
27885 tree saved_object_scope;
27886 bool saved_greater_than_is_operator_p;
27888 /* [temp.names]
27890 When parsing a template-id, the first non-nested `>' is taken as
27891 the end of the template-argument-list rather than a greater-than
27892 operator. */
27893 saved_greater_than_is_operator_p
27894 = parser->greater_than_is_operator_p;
27895 parser->greater_than_is_operator_p = false;
27896 /* Parsing the argument list may modify SCOPE, so we save it
27897 here. */
27898 saved_scope = parser->scope;
27899 saved_qualifying_scope = parser->qualifying_scope;
27900 saved_object_scope = parser->object_scope;
27901 /* We need to evaluate the template arguments, even though this
27902 template-id may be nested within a "sizeof". */
27903 cp_evaluated ev;
27904 /* Parse the template-argument-list itself. */
27905 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27906 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27907 arguments = NULL_TREE;
27908 else
27909 arguments = cp_parser_template_argument_list (parser);
27910 /* Look for the `>' that ends the template-argument-list. If we find
27911 a '>>' instead, it's probably just a typo. */
27912 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27914 if (cxx_dialect != cxx98)
27916 /* In C++0x, a `>>' in a template argument list or cast
27917 expression is considered to be two separate `>'
27918 tokens. So, change the current token to a `>', but don't
27919 consume it: it will be consumed later when the outer
27920 template argument list (or cast expression) is parsed.
27921 Note that this replacement of `>' for `>>' is necessary
27922 even if we are parsing tentatively: in the tentative
27923 case, after calling
27924 cp_parser_enclosed_template_argument_list we will always
27925 throw away all of the template arguments and the first
27926 closing `>', either because the template argument list
27927 was erroneous or because we are replacing those tokens
27928 with a CPP_TEMPLATE_ID token. The second `>' (which will
27929 not have been thrown away) is needed either to close an
27930 outer template argument list or to complete a new-style
27931 cast. */
27932 cp_token *token = cp_lexer_peek_token (parser->lexer);
27933 token->type = CPP_GREATER;
27935 else if (!saved_greater_than_is_operator_p)
27937 /* If we're in a nested template argument list, the '>>' has
27938 to be a typo for '> >'. We emit the error message, but we
27939 continue parsing and we push a '>' as next token, so that
27940 the argument list will be parsed correctly. Note that the
27941 global source location is still on the token before the
27942 '>>', so we need to say explicitly where we want it. */
27943 cp_token *token = cp_lexer_peek_token (parser->lexer);
27944 gcc_rich_location richloc (token->location);
27945 richloc.add_fixit_replace ("> >");
27946 error_at (&richloc, "%<>>%> should be %<> >%> "
27947 "within a nested template argument list");
27949 token->type = CPP_GREATER;
27951 else
27953 /* If this is not a nested template argument list, the '>>'
27954 is a typo for '>'. Emit an error message and continue.
27955 Same deal about the token location, but here we can get it
27956 right by consuming the '>>' before issuing the diagnostic. */
27957 cp_token *token = cp_lexer_consume_token (parser->lexer);
27958 error_at (token->location,
27959 "spurious %<>>%>, use %<>%> to terminate "
27960 "a template argument list");
27963 else
27964 cp_parser_skip_to_end_of_template_parameter_list (parser);
27965 /* The `>' token might be a greater-than operator again now. */
27966 parser->greater_than_is_operator_p
27967 = saved_greater_than_is_operator_p;
27968 /* Restore the SAVED_SCOPE. */
27969 parser->scope = saved_scope;
27970 parser->qualifying_scope = saved_qualifying_scope;
27971 parser->object_scope = saved_object_scope;
27973 return arguments;
27976 /* MEMBER_FUNCTION is a member function, or a friend. If default
27977 arguments, or the body of the function have not yet been parsed,
27978 parse them now. */
27980 static void
27981 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27983 timevar_push (TV_PARSE_INMETH);
27984 /* If this member is a template, get the underlying
27985 FUNCTION_DECL. */
27986 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27987 member_function = DECL_TEMPLATE_RESULT (member_function);
27989 /* There should not be any class definitions in progress at this
27990 point; the bodies of members are only parsed outside of all class
27991 definitions. */
27992 gcc_assert (parser->num_classes_being_defined == 0);
27993 /* While we're parsing the member functions we might encounter more
27994 classes. We want to handle them right away, but we don't want
27995 them getting mixed up with functions that are currently in the
27996 queue. */
27997 push_unparsed_function_queues (parser);
27999 /* Make sure that any template parameters are in scope. */
28000 maybe_begin_member_template_processing (member_function);
28002 /* If the body of the function has not yet been parsed, parse it
28003 now. */
28004 if (DECL_PENDING_INLINE_P (member_function))
28006 tree function_scope;
28007 cp_token_cache *tokens;
28009 /* The function is no longer pending; we are processing it. */
28010 tokens = DECL_PENDING_INLINE_INFO (member_function);
28011 DECL_PENDING_INLINE_INFO (member_function) = NULL;
28012 DECL_PENDING_INLINE_P (member_function) = 0;
28014 /* If this is a local class, enter the scope of the containing
28015 function. */
28016 function_scope = current_function_decl;
28017 if (function_scope)
28018 push_function_context ();
28020 /* Push the body of the function onto the lexer stack. */
28021 cp_parser_push_lexer_for_tokens (parser, tokens);
28023 /* Let the front end know that we going to be defining this
28024 function. */
28025 start_preparsed_function (member_function, NULL_TREE,
28026 SF_PRE_PARSED | SF_INCLASS_INLINE);
28028 /* Don't do access checking if it is a templated function. */
28029 if (processing_template_decl)
28030 push_deferring_access_checks (dk_no_check);
28032 /* #pragma omp declare reduction needs special parsing. */
28033 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28035 parser->lexer->in_pragma = true;
28036 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28037 finish_function (/*inline_p=*/true);
28038 cp_check_omp_declare_reduction (member_function);
28040 else
28041 /* Now, parse the body of the function. */
28042 cp_parser_function_definition_after_declarator (parser,
28043 /*inline_p=*/true);
28045 if (processing_template_decl)
28046 pop_deferring_access_checks ();
28048 /* Leave the scope of the containing function. */
28049 if (function_scope)
28050 pop_function_context ();
28051 cp_parser_pop_lexer (parser);
28054 /* Remove any template parameters from the symbol table. */
28055 maybe_end_member_template_processing ();
28057 /* Restore the queue. */
28058 pop_unparsed_function_queues (parser);
28059 timevar_pop (TV_PARSE_INMETH);
28062 /* If DECL contains any default args, remember it on the unparsed
28063 functions queue. */
28065 static void
28066 cp_parser_save_default_args (cp_parser* parser, tree decl)
28068 tree probe;
28070 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28071 probe;
28072 probe = TREE_CHAIN (probe))
28073 if (TREE_PURPOSE (probe))
28075 cp_default_arg_entry entry = {current_class_type, decl};
28076 vec_safe_push (unparsed_funs_with_default_args, entry);
28077 break;
28081 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28082 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28083 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28084 from the parameter-type-list. */
28086 static tree
28087 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28088 tree default_arg, tree parmtype)
28090 cp_token_cache *tokens;
28091 tree parsed_arg;
28092 bool dummy;
28094 if (default_arg == error_mark_node)
28095 return error_mark_node;
28097 /* Push the saved tokens for the default argument onto the parser's
28098 lexer stack. */
28099 tokens = DEFARG_TOKENS (default_arg);
28100 cp_parser_push_lexer_for_tokens (parser, tokens);
28102 start_lambda_scope (decl);
28104 /* Parse the default argument. */
28105 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28106 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28107 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28109 finish_lambda_scope ();
28111 if (parsed_arg == error_mark_node)
28112 cp_parser_skip_to_end_of_statement (parser);
28114 if (!processing_template_decl)
28116 /* In a non-template class, check conversions now. In a template,
28117 we'll wait and instantiate these as needed. */
28118 if (TREE_CODE (decl) == PARM_DECL)
28119 parsed_arg = check_default_argument (parmtype, parsed_arg,
28120 tf_warning_or_error);
28121 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28122 parsed_arg = error_mark_node;
28123 else
28124 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28127 /* If the token stream has not been completely used up, then
28128 there was extra junk after the end of the default
28129 argument. */
28130 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28132 if (TREE_CODE (decl) == PARM_DECL)
28133 cp_parser_error (parser, "expected %<,%>");
28134 else
28135 cp_parser_error (parser, "expected %<;%>");
28138 /* Revert to the main lexer. */
28139 cp_parser_pop_lexer (parser);
28141 return parsed_arg;
28144 /* FIELD is a non-static data member with an initializer which we saved for
28145 later; parse it now. */
28147 static void
28148 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28150 tree def;
28152 maybe_begin_member_template_processing (field);
28154 push_unparsed_function_queues (parser);
28155 def = cp_parser_late_parse_one_default_arg (parser, field,
28156 DECL_INITIAL (field),
28157 NULL_TREE);
28158 pop_unparsed_function_queues (parser);
28160 maybe_end_member_template_processing ();
28162 DECL_INITIAL (field) = def;
28165 /* FN is a FUNCTION_DECL which may contains a parameter with an
28166 unparsed DEFAULT_ARG. Parse the default args now. This function
28167 assumes that the current scope is the scope in which the default
28168 argument should be processed. */
28170 static void
28171 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28173 bool saved_local_variables_forbidden_p;
28174 tree parm, parmdecl;
28176 /* While we're parsing the default args, we might (due to the
28177 statement expression extension) encounter more classes. We want
28178 to handle them right away, but we don't want them getting mixed
28179 up with default args that are currently in the queue. */
28180 push_unparsed_function_queues (parser);
28182 /* Local variable names (and the `this' keyword) may not appear
28183 in a default argument. */
28184 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28185 parser->local_variables_forbidden_p = true;
28187 push_defarg_context (fn);
28189 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28190 parmdecl = DECL_ARGUMENTS (fn);
28191 parm && parm != void_list_node;
28192 parm = TREE_CHAIN (parm),
28193 parmdecl = DECL_CHAIN (parmdecl))
28195 tree default_arg = TREE_PURPOSE (parm);
28196 tree parsed_arg;
28197 vec<tree, va_gc> *insts;
28198 tree copy;
28199 unsigned ix;
28201 if (!default_arg)
28202 continue;
28204 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28205 /* This can happen for a friend declaration for a function
28206 already declared with default arguments. */
28207 continue;
28209 parsed_arg
28210 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28211 default_arg,
28212 TREE_VALUE (parm));
28213 TREE_PURPOSE (parm) = parsed_arg;
28215 /* Update any instantiations we've already created. */
28216 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28217 vec_safe_iterate (insts, ix, &copy); ix++)
28218 TREE_PURPOSE (copy) = parsed_arg;
28221 pop_defarg_context ();
28223 /* Make sure no default arg is missing. */
28224 check_default_args (fn);
28226 /* Restore the state of local_variables_forbidden_p. */
28227 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28229 /* Restore the queue. */
28230 pop_unparsed_function_queues (parser);
28233 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28235 sizeof ... ( identifier )
28237 where the 'sizeof' token has already been consumed. */
28239 static tree
28240 cp_parser_sizeof_pack (cp_parser *parser)
28242 /* Consume the `...'. */
28243 cp_lexer_consume_token (parser->lexer);
28244 maybe_warn_variadic_templates ();
28246 matching_parens parens;
28247 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28248 if (paren)
28249 parens.consume_open (parser);
28250 else
28251 permerror (cp_lexer_peek_token (parser->lexer)->location,
28252 "%<sizeof...%> argument must be surrounded by parentheses");
28254 cp_token *token = cp_lexer_peek_token (parser->lexer);
28255 tree name = cp_parser_identifier (parser);
28256 if (name == error_mark_node)
28257 return error_mark_node;
28258 /* The name is not qualified. */
28259 parser->scope = NULL_TREE;
28260 parser->qualifying_scope = NULL_TREE;
28261 parser->object_scope = NULL_TREE;
28262 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28263 if (expr == error_mark_node)
28264 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28265 token->location);
28266 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28267 expr = TREE_TYPE (expr);
28268 else if (TREE_CODE (expr) == CONST_DECL)
28269 expr = DECL_INITIAL (expr);
28270 expr = make_pack_expansion (expr);
28271 PACK_EXPANSION_SIZEOF_P (expr) = true;
28273 if (paren)
28274 parens.require_close (parser);
28276 return expr;
28279 /* Parse the operand of `sizeof' (or a similar operator). Returns
28280 either a TYPE or an expression, depending on the form of the
28281 input. The KEYWORD indicates which kind of expression we have
28282 encountered. */
28284 static tree
28285 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28287 tree expr = NULL_TREE;
28288 const char *saved_message;
28289 char *tmp;
28290 bool saved_integral_constant_expression_p;
28291 bool saved_non_integral_constant_expression_p;
28293 /* If it's a `...', then we are computing the length of a parameter
28294 pack. */
28295 if (keyword == RID_SIZEOF
28296 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28297 return cp_parser_sizeof_pack (parser);
28299 /* Types cannot be defined in a `sizeof' expression. Save away the
28300 old message. */
28301 saved_message = parser->type_definition_forbidden_message;
28302 /* And create the new one. */
28303 tmp = concat ("types may not be defined in %<",
28304 IDENTIFIER_POINTER (ridpointers[keyword]),
28305 "%> expressions", NULL);
28306 parser->type_definition_forbidden_message = tmp;
28308 /* The restrictions on constant-expressions do not apply inside
28309 sizeof expressions. */
28310 saved_integral_constant_expression_p
28311 = parser->integral_constant_expression_p;
28312 saved_non_integral_constant_expression_p
28313 = parser->non_integral_constant_expression_p;
28314 parser->integral_constant_expression_p = false;
28316 /* Do not actually evaluate the expression. */
28317 ++cp_unevaluated_operand;
28318 ++c_inhibit_evaluation_warnings;
28319 /* If it's a `(', then we might be looking at the type-id
28320 construction. */
28321 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28323 tree type = NULL_TREE;
28325 /* We can't be sure yet whether we're looking at a type-id or an
28326 expression. */
28327 cp_parser_parse_tentatively (parser);
28329 matching_parens parens;
28330 parens.consume_open (parser);
28332 /* Note: as a GNU Extension, compound literals are considered
28333 postfix-expressions as they are in C99, so they are valid
28334 arguments to sizeof. See comment in cp_parser_cast_expression
28335 for details. */
28336 if (cp_parser_compound_literal_p (parser))
28337 cp_parser_simulate_error (parser);
28338 else
28340 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28341 parser->in_type_id_in_expr_p = true;
28342 /* Look for the type-id. */
28343 type = cp_parser_type_id (parser);
28344 /* Look for the closing `)'. */
28345 parens.require_close (parser);
28346 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28349 /* If all went well, then we're done. */
28350 if (cp_parser_parse_definitely (parser))
28351 expr = type;
28354 /* If the type-id production did not work out, then we must be
28355 looking at the unary-expression production. */
28356 if (!expr)
28357 expr = cp_parser_unary_expression (parser);
28359 /* Go back to evaluating expressions. */
28360 --cp_unevaluated_operand;
28361 --c_inhibit_evaluation_warnings;
28363 /* Free the message we created. */
28364 free (tmp);
28365 /* And restore the old one. */
28366 parser->type_definition_forbidden_message = saved_message;
28367 parser->integral_constant_expression_p
28368 = saved_integral_constant_expression_p;
28369 parser->non_integral_constant_expression_p
28370 = saved_non_integral_constant_expression_p;
28372 return expr;
28375 /* If the current declaration has no declarator, return true. */
28377 static bool
28378 cp_parser_declares_only_class_p (cp_parser *parser)
28380 /* If the next token is a `;' or a `,' then there is no
28381 declarator. */
28382 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28383 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28386 /* Update the DECL_SPECS to reflect the storage class indicated by
28387 KEYWORD. */
28389 static void
28390 cp_parser_set_storage_class (cp_parser *parser,
28391 cp_decl_specifier_seq *decl_specs,
28392 enum rid keyword,
28393 cp_token *token)
28395 cp_storage_class storage_class;
28397 if (parser->in_unbraced_linkage_specification_p)
28399 error_at (token->location, "invalid use of %qD in linkage specification",
28400 ridpointers[keyword]);
28401 return;
28403 else if (decl_specs->storage_class != sc_none)
28405 decl_specs->conflicting_specifiers_p = true;
28406 return;
28409 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28410 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28411 && decl_specs->gnu_thread_keyword_p)
28413 pedwarn (decl_specs->locations[ds_thread], 0,
28414 "%<__thread%> before %qD", ridpointers[keyword]);
28417 switch (keyword)
28419 case RID_AUTO:
28420 storage_class = sc_auto;
28421 break;
28422 case RID_REGISTER:
28423 storage_class = sc_register;
28424 break;
28425 case RID_STATIC:
28426 storage_class = sc_static;
28427 break;
28428 case RID_EXTERN:
28429 storage_class = sc_extern;
28430 break;
28431 case RID_MUTABLE:
28432 storage_class = sc_mutable;
28433 break;
28434 default:
28435 gcc_unreachable ();
28437 decl_specs->storage_class = storage_class;
28438 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28440 /* A storage class specifier cannot be applied alongside a typedef
28441 specifier. If there is a typedef specifier present then set
28442 conflicting_specifiers_p which will trigger an error later
28443 on in grokdeclarator. */
28444 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28445 decl_specs->conflicting_specifiers_p = true;
28448 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28449 is true, the type is a class or enum definition. */
28451 static void
28452 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28453 tree type_spec,
28454 cp_token *token,
28455 bool type_definition_p)
28457 decl_specs->any_specifiers_p = true;
28459 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28460 (with, for example, in "typedef int wchar_t;") we remember that
28461 this is what happened. In system headers, we ignore these
28462 declarations so that G++ can work with system headers that are not
28463 C++-safe. */
28464 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28465 && !type_definition_p
28466 && (type_spec == boolean_type_node
28467 || type_spec == char16_type_node
28468 || type_spec == char32_type_node
28469 || type_spec == wchar_type_node)
28470 && (decl_specs->type
28471 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28472 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28473 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28474 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28476 decl_specs->redefined_builtin_type = type_spec;
28477 set_and_check_decl_spec_loc (decl_specs,
28478 ds_redefined_builtin_type_spec,
28479 token);
28480 if (!decl_specs->type)
28482 decl_specs->type = type_spec;
28483 decl_specs->type_definition_p = false;
28484 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28487 else if (decl_specs->type)
28488 decl_specs->multiple_types_p = true;
28489 else
28491 decl_specs->type = type_spec;
28492 decl_specs->type_definition_p = type_definition_p;
28493 decl_specs->redefined_builtin_type = NULL_TREE;
28494 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28498 /* True iff TOKEN is the GNU keyword __thread. */
28500 static bool
28501 token_is__thread (cp_token *token)
28503 gcc_assert (token->keyword == RID_THREAD);
28504 return id_equal (token->u.value, "__thread");
28507 /* Set the location for a declarator specifier and check if it is
28508 duplicated.
28510 DECL_SPECS is the sequence of declarator specifiers onto which to
28511 set the location.
28513 DS is the single declarator specifier to set which location is to
28514 be set onto the existing sequence of declarators.
28516 LOCATION is the location for the declarator specifier to
28517 consider. */
28519 static void
28520 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28521 cp_decl_spec ds, cp_token *token)
28523 gcc_assert (ds < ds_last);
28525 if (decl_specs == NULL)
28526 return;
28528 source_location location = token->location;
28530 if (decl_specs->locations[ds] == 0)
28532 decl_specs->locations[ds] = location;
28533 if (ds == ds_thread)
28534 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28536 else
28538 if (ds == ds_long)
28540 if (decl_specs->locations[ds_long_long] != 0)
28541 error_at (location,
28542 "%<long long long%> is too long for GCC");
28543 else
28545 decl_specs->locations[ds_long_long] = location;
28546 pedwarn_cxx98 (location,
28547 OPT_Wlong_long,
28548 "ISO C++ 1998 does not support %<long long%>");
28551 else if (ds == ds_thread)
28553 bool gnu = token_is__thread (token);
28554 gcc_rich_location richloc (location);
28555 if (gnu != decl_specs->gnu_thread_keyword_p)
28557 richloc.add_range (decl_specs->locations[ds_thread]);
28558 error_at (&richloc,
28559 "both %<__thread%> and %<thread_local%> specified");
28561 else
28563 richloc.add_fixit_remove ();
28564 error_at (&richloc, "duplicate %qD", token->u.value);
28567 else
28569 static const char *const decl_spec_names[] = {
28570 "signed",
28571 "unsigned",
28572 "short",
28573 "long",
28574 "const",
28575 "volatile",
28576 "restrict",
28577 "inline",
28578 "virtual",
28579 "explicit",
28580 "friend",
28581 "typedef",
28582 "using",
28583 "constexpr",
28584 "__complex"
28586 gcc_rich_location richloc (location);
28587 richloc.add_fixit_remove ();
28588 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28593 /* Return true iff the declarator specifier DS is present in the
28594 sequence of declarator specifiers DECL_SPECS. */
28596 bool
28597 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28598 cp_decl_spec ds)
28600 gcc_assert (ds < ds_last);
28602 if (decl_specs == NULL)
28603 return false;
28605 return decl_specs->locations[ds] != 0;
28608 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28609 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28611 static bool
28612 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28614 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28617 /* Issue an error message indicating that TOKEN_DESC was expected.
28618 If KEYWORD is true, it indicated this function is called by
28619 cp_parser_require_keword and the required token can only be
28620 a indicated keyword.
28622 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28623 within any error as the location of an "opening" token matching
28624 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28625 RT_CLOSE_PAREN). */
28627 static void
28628 cp_parser_required_error (cp_parser *parser,
28629 required_token token_desc,
28630 bool keyword,
28631 location_t matching_location)
28633 if (cp_parser_simulate_error (parser))
28634 return;
28636 const char *gmsgid = NULL;
28637 switch (token_desc)
28639 case RT_NEW:
28640 gmsgid = G_("expected %<new%>");
28641 break;
28642 case RT_DELETE:
28643 gmsgid = G_("expected %<delete%>");
28644 break;
28645 case RT_RETURN:
28646 gmsgid = G_("expected %<return%>");
28647 break;
28648 case RT_WHILE:
28649 gmsgid = G_("expected %<while%>");
28650 break;
28651 case RT_EXTERN:
28652 gmsgid = G_("expected %<extern%>");
28653 break;
28654 case RT_STATIC_ASSERT:
28655 gmsgid = G_("expected %<static_assert%>");
28656 break;
28657 case RT_DECLTYPE:
28658 gmsgid = G_("expected %<decltype%>");
28659 break;
28660 case RT_OPERATOR:
28661 gmsgid = G_("expected %<operator%>");
28662 break;
28663 case RT_CLASS:
28664 gmsgid = G_("expected %<class%>");
28665 break;
28666 case RT_TEMPLATE:
28667 gmsgid = G_("expected %<template%>");
28668 break;
28669 case RT_NAMESPACE:
28670 gmsgid = G_("expected %<namespace%>");
28671 break;
28672 case RT_USING:
28673 gmsgid = G_("expected %<using%>");
28674 break;
28675 case RT_ASM:
28676 gmsgid = G_("expected %<asm%>");
28677 break;
28678 case RT_TRY:
28679 gmsgid = G_("expected %<try%>");
28680 break;
28681 case RT_CATCH:
28682 gmsgid = G_("expected %<catch%>");
28683 break;
28684 case RT_THROW:
28685 gmsgid = G_("expected %<throw%>");
28686 break;
28687 case RT_LABEL:
28688 gmsgid = G_("expected %<__label__%>");
28689 break;
28690 case RT_AT_TRY:
28691 gmsgid = G_("expected %<@try%>");
28692 break;
28693 case RT_AT_SYNCHRONIZED:
28694 gmsgid = G_("expected %<@synchronized%>");
28695 break;
28696 case RT_AT_THROW:
28697 gmsgid = G_("expected %<@throw%>");
28698 break;
28699 case RT_TRANSACTION_ATOMIC:
28700 gmsgid = G_("expected %<__transaction_atomic%>");
28701 break;
28702 case RT_TRANSACTION_RELAXED:
28703 gmsgid = G_("expected %<__transaction_relaxed%>");
28704 break;
28705 default:
28706 break;
28709 if (!gmsgid && !keyword)
28711 switch (token_desc)
28713 case RT_SEMICOLON:
28714 gmsgid = G_("expected %<;%>");
28715 break;
28716 case RT_OPEN_PAREN:
28717 gmsgid = G_("expected %<(%>");
28718 break;
28719 case RT_CLOSE_BRACE:
28720 gmsgid = G_("expected %<}%>");
28721 break;
28722 case RT_OPEN_BRACE:
28723 gmsgid = G_("expected %<{%>");
28724 break;
28725 case RT_CLOSE_SQUARE:
28726 gmsgid = G_("expected %<]%>");
28727 break;
28728 case RT_OPEN_SQUARE:
28729 gmsgid = G_("expected %<[%>");
28730 break;
28731 case RT_COMMA:
28732 gmsgid = G_("expected %<,%>");
28733 break;
28734 case RT_SCOPE:
28735 gmsgid = G_("expected %<::%>");
28736 break;
28737 case RT_LESS:
28738 gmsgid = G_("expected %<<%>");
28739 break;
28740 case RT_GREATER:
28741 gmsgid = G_("expected %<>%>");
28742 break;
28743 case RT_EQ:
28744 gmsgid = G_("expected %<=%>");
28745 break;
28746 case RT_ELLIPSIS:
28747 gmsgid = G_("expected %<...%>");
28748 break;
28749 case RT_MULT:
28750 gmsgid = G_("expected %<*%>");
28751 break;
28752 case RT_COMPL:
28753 gmsgid = G_("expected %<~%>");
28754 break;
28755 case RT_COLON:
28756 gmsgid = G_("expected %<:%>");
28757 break;
28758 case RT_COLON_SCOPE:
28759 gmsgid = G_("expected %<:%> or %<::%>");
28760 break;
28761 case RT_CLOSE_PAREN:
28762 gmsgid = G_("expected %<)%>");
28763 break;
28764 case RT_COMMA_CLOSE_PAREN:
28765 gmsgid = G_("expected %<,%> or %<)%>");
28766 break;
28767 case RT_PRAGMA_EOL:
28768 gmsgid = G_("expected end of line");
28769 break;
28770 case RT_NAME:
28771 gmsgid = G_("expected identifier");
28772 break;
28773 case RT_SELECT:
28774 gmsgid = G_("expected selection-statement");
28775 break;
28776 case RT_ITERATION:
28777 gmsgid = G_("expected iteration-statement");
28778 break;
28779 case RT_JUMP:
28780 gmsgid = G_("expected jump-statement");
28781 break;
28782 case RT_CLASS_KEY:
28783 gmsgid = G_("expected class-key");
28784 break;
28785 case RT_CLASS_TYPENAME_TEMPLATE:
28786 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28787 break;
28788 default:
28789 gcc_unreachable ();
28793 if (gmsgid)
28794 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28798 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28799 issue an error message indicating that TOKEN_DESC was expected.
28801 Returns the token consumed, if the token had the appropriate type.
28802 Otherwise, returns NULL.
28804 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28805 within any error as the location of an "opening" token matching
28806 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28807 RT_CLOSE_PAREN). */
28809 static cp_token *
28810 cp_parser_require (cp_parser* parser,
28811 enum cpp_ttype type,
28812 required_token token_desc,
28813 location_t matching_location)
28815 if (cp_lexer_next_token_is (parser->lexer, type))
28816 return cp_lexer_consume_token (parser->lexer);
28817 else
28819 /* Output the MESSAGE -- unless we're parsing tentatively. */
28820 if (!cp_parser_simulate_error (parser))
28821 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28822 matching_location);
28823 return NULL;
28827 /* An error message is produced if the next token is not '>'.
28828 All further tokens are skipped until the desired token is
28829 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28831 static void
28832 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28834 /* Current level of '< ... >'. */
28835 unsigned level = 0;
28836 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28837 unsigned nesting_depth = 0;
28839 /* Are we ready, yet? If not, issue error message. */
28840 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28841 return;
28843 /* Skip tokens until the desired token is found. */
28844 while (true)
28846 /* Peek at the next token. */
28847 switch (cp_lexer_peek_token (parser->lexer)->type)
28849 case CPP_LESS:
28850 if (!nesting_depth)
28851 ++level;
28852 break;
28854 case CPP_RSHIFT:
28855 if (cxx_dialect == cxx98)
28856 /* C++0x views the `>>' operator as two `>' tokens, but
28857 C++98 does not. */
28858 break;
28859 else if (!nesting_depth && level-- == 0)
28861 /* We've hit a `>>' where the first `>' closes the
28862 template argument list, and the second `>' is
28863 spurious. Just consume the `>>' and stop; we've
28864 already produced at least one error. */
28865 cp_lexer_consume_token (parser->lexer);
28866 return;
28868 /* Fall through for C++0x, so we handle the second `>' in
28869 the `>>'. */
28870 gcc_fallthrough ();
28872 case CPP_GREATER:
28873 if (!nesting_depth && level-- == 0)
28875 /* We've reached the token we want, consume it and stop. */
28876 cp_lexer_consume_token (parser->lexer);
28877 return;
28879 break;
28881 case CPP_OPEN_PAREN:
28882 case CPP_OPEN_SQUARE:
28883 ++nesting_depth;
28884 break;
28886 case CPP_CLOSE_PAREN:
28887 case CPP_CLOSE_SQUARE:
28888 if (nesting_depth-- == 0)
28889 return;
28890 break;
28892 case CPP_EOF:
28893 case CPP_PRAGMA_EOL:
28894 case CPP_SEMICOLON:
28895 case CPP_OPEN_BRACE:
28896 case CPP_CLOSE_BRACE:
28897 /* The '>' was probably forgotten, don't look further. */
28898 return;
28900 default:
28901 break;
28904 /* Consume this token. */
28905 cp_lexer_consume_token (parser->lexer);
28909 /* If the next token is the indicated keyword, consume it. Otherwise,
28910 issue an error message indicating that TOKEN_DESC was expected.
28912 Returns the token consumed, if the token had the appropriate type.
28913 Otherwise, returns NULL. */
28915 static cp_token *
28916 cp_parser_require_keyword (cp_parser* parser,
28917 enum rid keyword,
28918 required_token token_desc)
28920 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28922 if (token && token->keyword != keyword)
28924 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28925 UNKNOWN_LOCATION);
28926 return NULL;
28929 return token;
28932 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28933 function-definition. */
28935 static bool
28936 cp_parser_token_starts_function_definition_p (cp_token* token)
28938 return (/* An ordinary function-body begins with an `{'. */
28939 token->type == CPP_OPEN_BRACE
28940 /* A ctor-initializer begins with a `:'. */
28941 || token->type == CPP_COLON
28942 /* A function-try-block begins with `try'. */
28943 || token->keyword == RID_TRY
28944 /* A function-transaction-block begins with `__transaction_atomic'
28945 or `__transaction_relaxed'. */
28946 || token->keyword == RID_TRANSACTION_ATOMIC
28947 || token->keyword == RID_TRANSACTION_RELAXED
28948 /* The named return value extension begins with `return'. */
28949 || token->keyword == RID_RETURN);
28952 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28953 definition. */
28955 static bool
28956 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28958 cp_token *token;
28960 token = cp_lexer_peek_token (parser->lexer);
28961 return (token->type == CPP_OPEN_BRACE
28962 || (token->type == CPP_COLON
28963 && !parser->colon_doesnt_start_class_def_p));
28966 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28967 C++0x) ending a template-argument. */
28969 static bool
28970 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28972 cp_token *token;
28974 token = cp_lexer_peek_token (parser->lexer);
28975 return (token->type == CPP_COMMA
28976 || token->type == CPP_GREATER
28977 || token->type == CPP_ELLIPSIS
28978 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28981 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28982 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28984 static bool
28985 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28986 size_t n)
28988 cp_token *token;
28990 token = cp_lexer_peek_nth_token (parser->lexer, n);
28991 if (token->type == CPP_LESS)
28992 return true;
28993 /* Check for the sequence `<::' in the original code. It would be lexed as
28994 `[:', where `[' is a digraph, and there is no whitespace before
28995 `:'. */
28996 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28998 cp_token *token2;
28999 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
29000 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
29001 return true;
29003 return false;
29006 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29007 or none_type otherwise. */
29009 static enum tag_types
29010 cp_parser_token_is_class_key (cp_token* token)
29012 switch (token->keyword)
29014 case RID_CLASS:
29015 return class_type;
29016 case RID_STRUCT:
29017 return record_type;
29018 case RID_UNION:
29019 return union_type;
29021 default:
29022 return none_type;
29026 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29027 or none_type otherwise or if the token is null. */
29029 static enum tag_types
29030 cp_parser_token_is_type_parameter_key (cp_token* token)
29032 if (!token)
29033 return none_type;
29035 switch (token->keyword)
29037 case RID_CLASS:
29038 return class_type;
29039 case RID_TYPENAME:
29040 return typename_type;
29042 default:
29043 return none_type;
29047 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29049 static void
29050 cp_parser_check_class_key (enum tag_types class_key, tree type)
29052 if (type == error_mark_node)
29053 return;
29054 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29056 if (permerror (input_location, "%qs tag used in naming %q#T",
29057 class_key == union_type ? "union"
29058 : class_key == record_type ? "struct" : "class",
29059 type))
29060 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29061 "%q#T was previously declared here", type);
29065 /* Issue an error message if DECL is redeclared with different
29066 access than its original declaration [class.access.spec/3].
29067 This applies to nested classes, nested class templates and
29068 enumerations [class.mem/1]. */
29070 static void
29071 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29073 if (!decl
29074 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29075 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29076 return;
29078 if ((TREE_PRIVATE (decl)
29079 != (current_access_specifier == access_private_node))
29080 || (TREE_PROTECTED (decl)
29081 != (current_access_specifier == access_protected_node)))
29082 error_at (location, "%qD redeclared with different access", decl);
29085 /* Look for the `template' keyword, as a syntactic disambiguator.
29086 Return TRUE iff it is present, in which case it will be
29087 consumed. */
29089 static bool
29090 cp_parser_optional_template_keyword (cp_parser *parser)
29092 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29094 /* In C++98 the `template' keyword can only be used within templates;
29095 outside templates the parser can always figure out what is a
29096 template and what is not. In C++11, per the resolution of DR 468,
29097 `template' is allowed in cases where it is not strictly necessary. */
29098 if (!processing_template_decl
29099 && pedantic && cxx_dialect == cxx98)
29101 cp_token *token = cp_lexer_peek_token (parser->lexer);
29102 pedwarn (token->location, OPT_Wpedantic,
29103 "in C++98 %<template%> (as a disambiguator) is only "
29104 "allowed within templates");
29105 /* If this part of the token stream is rescanned, the same
29106 error message would be generated. So, we purge the token
29107 from the stream. */
29108 cp_lexer_purge_token (parser->lexer);
29109 return false;
29111 else
29113 /* Consume the `template' keyword. */
29114 cp_lexer_consume_token (parser->lexer);
29115 return true;
29118 return false;
29121 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29122 set PARSER->SCOPE, and perform other related actions. */
29124 static void
29125 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29127 struct tree_check *check_value;
29129 /* Get the stored value. */
29130 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29131 /* Set the scope from the stored value. */
29132 parser->scope = saved_checks_value (check_value);
29133 parser->qualifying_scope = check_value->qualifying_scope;
29134 parser->object_scope = NULL_TREE;
29137 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29138 encounter the end of a block before what we were looking for. */
29140 static bool
29141 cp_parser_cache_group (cp_parser *parser,
29142 enum cpp_ttype end,
29143 unsigned depth)
29145 while (true)
29147 cp_token *token = cp_lexer_peek_token (parser->lexer);
29149 /* Abort a parenthesized expression if we encounter a semicolon. */
29150 if ((end == CPP_CLOSE_PAREN || depth == 0)
29151 && token->type == CPP_SEMICOLON)
29152 return true;
29153 /* If we've reached the end of the file, stop. */
29154 if (token->type == CPP_EOF
29155 || (end != CPP_PRAGMA_EOL
29156 && token->type == CPP_PRAGMA_EOL))
29157 return true;
29158 if (token->type == CPP_CLOSE_BRACE && depth == 0)
29159 /* We've hit the end of an enclosing block, so there's been some
29160 kind of syntax error. */
29161 return true;
29163 /* Consume the token. */
29164 cp_lexer_consume_token (parser->lexer);
29165 /* See if it starts a new group. */
29166 if (token->type == CPP_OPEN_BRACE)
29168 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29169 /* In theory this should probably check end == '}', but
29170 cp_parser_save_member_function_body needs it to exit
29171 after either '}' or ')' when called with ')'. */
29172 if (depth == 0)
29173 return false;
29175 else if (token->type == CPP_OPEN_PAREN)
29177 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29178 if (depth == 0 && end == CPP_CLOSE_PAREN)
29179 return false;
29181 else if (token->type == CPP_PRAGMA)
29182 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29183 else if (token->type == end)
29184 return false;
29188 /* Like above, for caching a default argument or NSDMI. Both of these are
29189 terminated by a non-nested comma, but it can be unclear whether or not a
29190 comma is nested in a template argument list unless we do more parsing.
29191 In order to handle this ambiguity, when we encounter a ',' after a '<'
29192 we try to parse what follows as a parameter-declaration-list (in the
29193 case of a default argument) or a member-declarator (in the case of an
29194 NSDMI). If that succeeds, then we stop caching. */
29196 static tree
29197 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29199 unsigned depth = 0;
29200 int maybe_template_id = 0;
29201 cp_token *first_token;
29202 cp_token *token;
29203 tree default_argument;
29205 /* Add tokens until we have processed the entire default
29206 argument. We add the range [first_token, token). */
29207 first_token = cp_lexer_peek_token (parser->lexer);
29208 if (first_token->type == CPP_OPEN_BRACE)
29210 /* For list-initialization, this is straightforward. */
29211 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29212 token = cp_lexer_peek_token (parser->lexer);
29214 else while (true)
29216 bool done = false;
29218 /* Peek at the next token. */
29219 token = cp_lexer_peek_token (parser->lexer);
29220 /* What we do depends on what token we have. */
29221 switch (token->type)
29223 /* In valid code, a default argument must be
29224 immediately followed by a `,' `)', or `...'. */
29225 case CPP_COMMA:
29226 if (depth == 0 && maybe_template_id)
29228 /* If we've seen a '<', we might be in a
29229 template-argument-list. Until Core issue 325 is
29230 resolved, we don't know how this situation ought
29231 to be handled, so try to DTRT. We check whether
29232 what comes after the comma is a valid parameter
29233 declaration list. If it is, then the comma ends
29234 the default argument; otherwise the default
29235 argument continues. */
29236 bool error = false;
29237 cp_token *peek;
29239 /* Set ITALP so cp_parser_parameter_declaration_list
29240 doesn't decide to commit to this parse. */
29241 bool saved_italp = parser->in_template_argument_list_p;
29242 parser->in_template_argument_list_p = true;
29244 cp_parser_parse_tentatively (parser);
29246 if (nsdmi)
29248 /* Parse declarators until we reach a non-comma or
29249 somthing that cannot be an initializer.
29250 Just checking whether we're looking at a single
29251 declarator is insufficient. Consider:
29252 int var = tuple<T,U>::x;
29253 The template parameter 'U' looks exactly like a
29254 declarator. */
29257 int ctor_dtor_or_conv_p;
29258 cp_lexer_consume_token (parser->lexer);
29259 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29260 &ctor_dtor_or_conv_p,
29261 /*parenthesized_p=*/NULL,
29262 /*member_p=*/true,
29263 /*friend_p=*/false);
29264 peek = cp_lexer_peek_token (parser->lexer);
29265 if (cp_parser_error_occurred (parser))
29266 break;
29268 while (peek->type == CPP_COMMA);
29269 /* If we met an '=' or ';' then the original comma
29270 was the end of the NSDMI. Otherwise assume
29271 we're still in the NSDMI. */
29272 error = (peek->type != CPP_EQ
29273 && peek->type != CPP_SEMICOLON);
29275 else
29277 cp_lexer_consume_token (parser->lexer);
29278 begin_scope (sk_function_parms, NULL_TREE);
29279 if (cp_parser_parameter_declaration_list (parser)
29280 == error_mark_node)
29281 error = true;
29282 pop_bindings_and_leave_scope ();
29284 if (!cp_parser_error_occurred (parser) && !error)
29285 done = true;
29286 cp_parser_abort_tentative_parse (parser);
29288 parser->in_template_argument_list_p = saved_italp;
29289 break;
29291 /* FALLTHRU */
29292 case CPP_CLOSE_PAREN:
29293 case CPP_ELLIPSIS:
29294 /* If we run into a non-nested `;', `}', or `]',
29295 then the code is invalid -- but the default
29296 argument is certainly over. */
29297 case CPP_SEMICOLON:
29298 case CPP_CLOSE_BRACE:
29299 case CPP_CLOSE_SQUARE:
29300 if (depth == 0
29301 /* Handle correctly int n = sizeof ... ( p ); */
29302 && token->type != CPP_ELLIPSIS)
29303 done = true;
29304 /* Update DEPTH, if necessary. */
29305 else if (token->type == CPP_CLOSE_PAREN
29306 || token->type == CPP_CLOSE_BRACE
29307 || token->type == CPP_CLOSE_SQUARE)
29308 --depth;
29309 break;
29311 case CPP_OPEN_PAREN:
29312 case CPP_OPEN_SQUARE:
29313 case CPP_OPEN_BRACE:
29314 ++depth;
29315 break;
29317 case CPP_LESS:
29318 if (depth == 0)
29319 /* This might be the comparison operator, or it might
29320 start a template argument list. */
29321 ++maybe_template_id;
29322 break;
29324 case CPP_RSHIFT:
29325 if (cxx_dialect == cxx98)
29326 break;
29327 /* Fall through for C++0x, which treats the `>>'
29328 operator like two `>' tokens in certain
29329 cases. */
29330 gcc_fallthrough ();
29332 case CPP_GREATER:
29333 if (depth == 0)
29335 /* This might be an operator, or it might close a
29336 template argument list. But if a previous '<'
29337 started a template argument list, this will have
29338 closed it, so we can't be in one anymore. */
29339 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29340 if (maybe_template_id < 0)
29341 maybe_template_id = 0;
29343 break;
29345 /* If we run out of tokens, issue an error message. */
29346 case CPP_EOF:
29347 case CPP_PRAGMA_EOL:
29348 error_at (token->location, "file ends in default argument");
29349 return error_mark_node;
29351 case CPP_NAME:
29352 case CPP_SCOPE:
29353 /* In these cases, we should look for template-ids.
29354 For example, if the default argument is
29355 `X<int, double>()', we need to do name lookup to
29356 figure out whether or not `X' is a template; if
29357 so, the `,' does not end the default argument.
29359 That is not yet done. */
29360 break;
29362 default:
29363 break;
29366 /* If we've reached the end, stop. */
29367 if (done)
29368 break;
29370 /* Add the token to the token block. */
29371 token = cp_lexer_consume_token (parser->lexer);
29374 /* Create a DEFAULT_ARG to represent the unparsed default
29375 argument. */
29376 default_argument = make_node (DEFAULT_ARG);
29377 DEFARG_TOKENS (default_argument)
29378 = cp_token_cache_new (first_token, token);
29379 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29381 return default_argument;
29384 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29386 location_t
29387 defarg_location (tree default_argument)
29389 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29390 location_t start = tokens->first->location;
29391 location_t end = tokens->last->location;
29392 return make_location (start, start, end);
29395 /* Begin parsing tentatively. We always save tokens while parsing
29396 tentatively so that if the tentative parsing fails we can restore the
29397 tokens. */
29399 static void
29400 cp_parser_parse_tentatively (cp_parser* parser)
29402 /* Enter a new parsing context. */
29403 parser->context = cp_parser_context_new (parser->context);
29404 /* Begin saving tokens. */
29405 cp_lexer_save_tokens (parser->lexer);
29406 /* In order to avoid repetitive access control error messages,
29407 access checks are queued up until we are no longer parsing
29408 tentatively. */
29409 push_deferring_access_checks (dk_deferred);
29412 /* Commit to the currently active tentative parse. */
29414 static void
29415 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29417 cp_parser_context *context;
29418 cp_lexer *lexer;
29420 /* Mark all of the levels as committed. */
29421 lexer = parser->lexer;
29422 for (context = parser->context; context->next; context = context->next)
29424 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29425 break;
29426 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29427 while (!cp_lexer_saving_tokens (lexer))
29428 lexer = lexer->next;
29429 cp_lexer_commit_tokens (lexer);
29433 /* Commit to the topmost currently active tentative parse.
29435 Note that this function shouldn't be called when there are
29436 irreversible side-effects while in a tentative state. For
29437 example, we shouldn't create a permanent entry in the symbol
29438 table, or issue an error message that might not apply if the
29439 tentative parse is aborted. */
29441 static void
29442 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29444 cp_parser_context *context = parser->context;
29445 cp_lexer *lexer = parser->lexer;
29447 if (context)
29449 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29450 return;
29451 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29453 while (!cp_lexer_saving_tokens (lexer))
29454 lexer = lexer->next;
29455 cp_lexer_commit_tokens (lexer);
29459 /* Abort the currently active tentative parse. All consumed tokens
29460 will be rolled back, and no diagnostics will be issued. */
29462 static void
29463 cp_parser_abort_tentative_parse (cp_parser* parser)
29465 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29466 || errorcount > 0);
29467 cp_parser_simulate_error (parser);
29468 /* Now, pretend that we want to see if the construct was
29469 successfully parsed. */
29470 cp_parser_parse_definitely (parser);
29473 /* Stop parsing tentatively. If a parse error has occurred, restore the
29474 token stream. Otherwise, commit to the tokens we have consumed.
29475 Returns true if no error occurred; false otherwise. */
29477 static bool
29478 cp_parser_parse_definitely (cp_parser* parser)
29480 bool error_occurred;
29481 cp_parser_context *context;
29483 /* Remember whether or not an error occurred, since we are about to
29484 destroy that information. */
29485 error_occurred = cp_parser_error_occurred (parser);
29486 /* Remove the topmost context from the stack. */
29487 context = parser->context;
29488 parser->context = context->next;
29489 /* If no parse errors occurred, commit to the tentative parse. */
29490 if (!error_occurred)
29492 /* Commit to the tokens read tentatively, unless that was
29493 already done. */
29494 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29495 cp_lexer_commit_tokens (parser->lexer);
29497 pop_to_parent_deferring_access_checks ();
29499 /* Otherwise, if errors occurred, roll back our state so that things
29500 are just as they were before we began the tentative parse. */
29501 else
29503 cp_lexer_rollback_tokens (parser->lexer);
29504 pop_deferring_access_checks ();
29506 /* Add the context to the front of the free list. */
29507 context->next = cp_parser_context_free_list;
29508 cp_parser_context_free_list = context;
29510 return !error_occurred;
29513 /* Returns true if we are parsing tentatively and are not committed to
29514 this tentative parse. */
29516 static bool
29517 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29519 return (cp_parser_parsing_tentatively (parser)
29520 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29523 /* Returns nonzero iff an error has occurred during the most recent
29524 tentative parse. */
29526 static bool
29527 cp_parser_error_occurred (cp_parser* parser)
29529 return (cp_parser_parsing_tentatively (parser)
29530 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29533 /* Returns nonzero if GNU extensions are allowed. */
29535 static bool
29536 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29538 return parser->allow_gnu_extensions_p;
29541 /* Objective-C++ Productions */
29544 /* Parse an Objective-C expression, which feeds into a primary-expression
29545 above.
29547 objc-expression:
29548 objc-message-expression
29549 objc-string-literal
29550 objc-encode-expression
29551 objc-protocol-expression
29552 objc-selector-expression
29554 Returns a tree representation of the expression. */
29556 static cp_expr
29557 cp_parser_objc_expression (cp_parser* parser)
29559 /* Try to figure out what kind of declaration is present. */
29560 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29562 switch (kwd->type)
29564 case CPP_OPEN_SQUARE:
29565 return cp_parser_objc_message_expression (parser);
29567 case CPP_OBJC_STRING:
29568 kwd = cp_lexer_consume_token (parser->lexer);
29569 return objc_build_string_object (kwd->u.value);
29571 case CPP_KEYWORD:
29572 switch (kwd->keyword)
29574 case RID_AT_ENCODE:
29575 return cp_parser_objc_encode_expression (parser);
29577 case RID_AT_PROTOCOL:
29578 return cp_parser_objc_protocol_expression (parser);
29580 case RID_AT_SELECTOR:
29581 return cp_parser_objc_selector_expression (parser);
29583 default:
29584 break;
29586 /* FALLTHRU */
29587 default:
29588 error_at (kwd->location,
29589 "misplaced %<@%D%> Objective-C++ construct",
29590 kwd->u.value);
29591 cp_parser_skip_to_end_of_block_or_statement (parser);
29594 return error_mark_node;
29597 /* Parse an Objective-C message expression.
29599 objc-message-expression:
29600 [ objc-message-receiver objc-message-args ]
29602 Returns a representation of an Objective-C message. */
29604 static tree
29605 cp_parser_objc_message_expression (cp_parser* parser)
29607 tree receiver, messageargs;
29609 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29610 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29611 receiver = cp_parser_objc_message_receiver (parser);
29612 messageargs = cp_parser_objc_message_args (parser);
29613 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29614 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29616 tree result = objc_build_message_expr (receiver, messageargs);
29618 /* Construct a location e.g.
29619 [self func1:5]
29620 ^~~~~~~~~~~~~~
29621 ranging from the '[' to the ']', with the caret at the start. */
29622 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29623 protected_set_expr_location (result, combined_loc);
29625 return result;
29628 /* Parse an objc-message-receiver.
29630 objc-message-receiver:
29631 expression
29632 simple-type-specifier
29634 Returns a representation of the type or expression. */
29636 static tree
29637 cp_parser_objc_message_receiver (cp_parser* parser)
29639 tree rcv;
29641 /* An Objective-C message receiver may be either (1) a type
29642 or (2) an expression. */
29643 cp_parser_parse_tentatively (parser);
29644 rcv = cp_parser_expression (parser);
29646 /* If that worked out, fine. */
29647 if (cp_parser_parse_definitely (parser))
29648 return rcv;
29650 cp_parser_parse_tentatively (parser);
29651 rcv = cp_parser_simple_type_specifier (parser,
29652 /*decl_specs=*/NULL,
29653 CP_PARSER_FLAGS_NONE);
29655 if (cp_parser_parse_definitely (parser))
29656 return objc_get_class_reference (rcv);
29658 cp_parser_error (parser, "objective-c++ message receiver expected");
29659 return error_mark_node;
29662 /* Parse the arguments and selectors comprising an Objective-C message.
29664 objc-message-args:
29665 objc-selector
29666 objc-selector-args
29667 objc-selector-args , objc-comma-args
29669 objc-selector-args:
29670 objc-selector [opt] : assignment-expression
29671 objc-selector-args objc-selector [opt] : assignment-expression
29673 objc-comma-args:
29674 assignment-expression
29675 objc-comma-args , assignment-expression
29677 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29678 selector arguments and TREE_VALUE containing a list of comma
29679 arguments. */
29681 static tree
29682 cp_parser_objc_message_args (cp_parser* parser)
29684 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29685 bool maybe_unary_selector_p = true;
29686 cp_token *token = cp_lexer_peek_token (parser->lexer);
29688 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29690 tree selector = NULL_TREE, arg;
29692 if (token->type != CPP_COLON)
29693 selector = cp_parser_objc_selector (parser);
29695 /* Detect if we have a unary selector. */
29696 if (maybe_unary_selector_p
29697 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29698 return build_tree_list (selector, NULL_TREE);
29700 maybe_unary_selector_p = false;
29701 cp_parser_require (parser, CPP_COLON, RT_COLON);
29702 arg = cp_parser_assignment_expression (parser);
29704 sel_args
29705 = chainon (sel_args,
29706 build_tree_list (selector, arg));
29708 token = cp_lexer_peek_token (parser->lexer);
29711 /* Handle non-selector arguments, if any. */
29712 while (token->type == CPP_COMMA)
29714 tree arg;
29716 cp_lexer_consume_token (parser->lexer);
29717 arg = cp_parser_assignment_expression (parser);
29719 addl_args
29720 = chainon (addl_args,
29721 build_tree_list (NULL_TREE, arg));
29723 token = cp_lexer_peek_token (parser->lexer);
29726 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29728 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29729 return build_tree_list (error_mark_node, error_mark_node);
29732 return build_tree_list (sel_args, addl_args);
29735 /* Parse an Objective-C encode expression.
29737 objc-encode-expression:
29738 @encode objc-typename
29740 Returns an encoded representation of the type argument. */
29742 static cp_expr
29743 cp_parser_objc_encode_expression (cp_parser* parser)
29745 tree type;
29746 cp_token *token;
29747 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29749 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29750 matching_parens parens;
29751 parens.require_open (parser);
29752 token = cp_lexer_peek_token (parser->lexer);
29753 type = complete_type (cp_parser_type_id (parser));
29754 parens.require_close (parser);
29756 if (!type)
29758 error_at (token->location,
29759 "%<@encode%> must specify a type as an argument");
29760 return error_mark_node;
29763 /* This happens if we find @encode(T) (where T is a template
29764 typename or something dependent on a template typename) when
29765 parsing a template. In that case, we can't compile it
29766 immediately, but we rather create an AT_ENCODE_EXPR which will
29767 need to be instantiated when the template is used.
29769 if (dependent_type_p (type))
29771 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29772 TREE_READONLY (value) = 1;
29773 return value;
29777 /* Build a location of the form:
29778 @encode(int)
29779 ^~~~~~~~~~~~
29780 with caret==start at the @ token, finishing at the close paren. */
29781 location_t combined_loc
29782 = make_location (start_loc, start_loc,
29783 cp_lexer_previous_token (parser->lexer)->location);
29785 return cp_expr (objc_build_encode_expr (type), combined_loc);
29788 /* Parse an Objective-C @defs expression. */
29790 static tree
29791 cp_parser_objc_defs_expression (cp_parser *parser)
29793 tree name;
29795 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29796 matching_parens parens;
29797 parens.require_open (parser);
29798 name = cp_parser_identifier (parser);
29799 parens.require_close (parser);
29801 return objc_get_class_ivars (name);
29804 /* Parse an Objective-C protocol expression.
29806 objc-protocol-expression:
29807 @protocol ( identifier )
29809 Returns a representation of the protocol expression. */
29811 static tree
29812 cp_parser_objc_protocol_expression (cp_parser* parser)
29814 tree proto;
29815 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29817 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29818 matching_parens parens;
29819 parens.require_open (parser);
29820 proto = cp_parser_identifier (parser);
29821 parens.require_close (parser);
29823 /* Build a location of the form:
29824 @protocol(prot)
29825 ^~~~~~~~~~~~~~~
29826 with caret==start at the @ token, finishing at the close paren. */
29827 location_t combined_loc
29828 = make_location (start_loc, start_loc,
29829 cp_lexer_previous_token (parser->lexer)->location);
29830 tree result = objc_build_protocol_expr (proto);
29831 protected_set_expr_location (result, combined_loc);
29832 return result;
29835 /* Parse an Objective-C selector expression.
29837 objc-selector-expression:
29838 @selector ( objc-method-signature )
29840 objc-method-signature:
29841 objc-selector
29842 objc-selector-seq
29844 objc-selector-seq:
29845 objc-selector :
29846 objc-selector-seq objc-selector :
29848 Returns a representation of the method selector. */
29850 static tree
29851 cp_parser_objc_selector_expression (cp_parser* parser)
29853 tree sel_seq = NULL_TREE;
29854 bool maybe_unary_selector_p = true;
29855 cp_token *token;
29856 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29858 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29859 matching_parens parens;
29860 parens.require_open (parser);
29861 token = cp_lexer_peek_token (parser->lexer);
29863 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29864 || token->type == CPP_SCOPE)
29866 tree selector = NULL_TREE;
29868 if (token->type != CPP_COLON
29869 || token->type == CPP_SCOPE)
29870 selector = cp_parser_objc_selector (parser);
29872 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29873 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29875 /* Detect if we have a unary selector. */
29876 if (maybe_unary_selector_p)
29878 sel_seq = selector;
29879 goto finish_selector;
29881 else
29883 cp_parser_error (parser, "expected %<:%>");
29886 maybe_unary_selector_p = false;
29887 token = cp_lexer_consume_token (parser->lexer);
29889 if (token->type == CPP_SCOPE)
29891 sel_seq
29892 = chainon (sel_seq,
29893 build_tree_list (selector, NULL_TREE));
29894 sel_seq
29895 = chainon (sel_seq,
29896 build_tree_list (NULL_TREE, NULL_TREE));
29898 else
29899 sel_seq
29900 = chainon (sel_seq,
29901 build_tree_list (selector, NULL_TREE));
29903 token = cp_lexer_peek_token (parser->lexer);
29906 finish_selector:
29907 parens.require_close (parser);
29910 /* Build a location of the form:
29911 @selector(func)
29912 ^~~~~~~~~~~~~~~
29913 with caret==start at the @ token, finishing at the close paren. */
29914 location_t combined_loc
29915 = make_location (loc, loc,
29916 cp_lexer_previous_token (parser->lexer)->location);
29917 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29918 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29919 protected_set_expr_location (result, combined_loc);
29920 return result;
29923 /* Parse a list of identifiers.
29925 objc-identifier-list:
29926 identifier
29927 objc-identifier-list , identifier
29929 Returns a TREE_LIST of identifier nodes. */
29931 static tree
29932 cp_parser_objc_identifier_list (cp_parser* parser)
29934 tree identifier;
29935 tree list;
29936 cp_token *sep;
29938 identifier = cp_parser_identifier (parser);
29939 if (identifier == error_mark_node)
29940 return error_mark_node;
29942 list = build_tree_list (NULL_TREE, identifier);
29943 sep = cp_lexer_peek_token (parser->lexer);
29945 while (sep->type == CPP_COMMA)
29947 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29948 identifier = cp_parser_identifier (parser);
29949 if (identifier == error_mark_node)
29950 return list;
29952 list = chainon (list, build_tree_list (NULL_TREE,
29953 identifier));
29954 sep = cp_lexer_peek_token (parser->lexer);
29957 return list;
29960 /* Parse an Objective-C alias declaration.
29962 objc-alias-declaration:
29963 @compatibility_alias identifier identifier ;
29965 This function registers the alias mapping with the Objective-C front end.
29966 It returns nothing. */
29968 static void
29969 cp_parser_objc_alias_declaration (cp_parser* parser)
29971 tree alias, orig;
29973 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29974 alias = cp_parser_identifier (parser);
29975 orig = cp_parser_identifier (parser);
29976 objc_declare_alias (alias, orig);
29977 cp_parser_consume_semicolon_at_end_of_statement (parser);
29980 /* Parse an Objective-C class forward-declaration.
29982 objc-class-declaration:
29983 @class objc-identifier-list ;
29985 The function registers the forward declarations with the Objective-C
29986 front end. It returns nothing. */
29988 static void
29989 cp_parser_objc_class_declaration (cp_parser* parser)
29991 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29992 while (true)
29994 tree id;
29996 id = cp_parser_identifier (parser);
29997 if (id == error_mark_node)
29998 break;
30000 objc_declare_class (id);
30002 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30003 cp_lexer_consume_token (parser->lexer);
30004 else
30005 break;
30007 cp_parser_consume_semicolon_at_end_of_statement (parser);
30010 /* Parse a list of Objective-C protocol references.
30012 objc-protocol-refs-opt:
30013 objc-protocol-refs [opt]
30015 objc-protocol-refs:
30016 < objc-identifier-list >
30018 Returns a TREE_LIST of identifiers, if any. */
30020 static tree
30021 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30023 tree protorefs = NULL_TREE;
30025 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30027 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30028 protorefs = cp_parser_objc_identifier_list (parser);
30029 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30032 return protorefs;
30035 /* Parse a Objective-C visibility specification. */
30037 static void
30038 cp_parser_objc_visibility_spec (cp_parser* parser)
30040 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30042 switch (vis->keyword)
30044 case RID_AT_PRIVATE:
30045 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30046 break;
30047 case RID_AT_PROTECTED:
30048 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30049 break;
30050 case RID_AT_PUBLIC:
30051 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30052 break;
30053 case RID_AT_PACKAGE:
30054 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30055 break;
30056 default:
30057 return;
30060 /* Eat '@private'/'@protected'/'@public'. */
30061 cp_lexer_consume_token (parser->lexer);
30064 /* Parse an Objective-C method type. Return 'true' if it is a class
30065 (+) method, and 'false' if it is an instance (-) method. */
30067 static inline bool
30068 cp_parser_objc_method_type (cp_parser* parser)
30070 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30071 return true;
30072 else
30073 return false;
30076 /* Parse an Objective-C protocol qualifier. */
30078 static tree
30079 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30081 tree quals = NULL_TREE, node;
30082 cp_token *token = cp_lexer_peek_token (parser->lexer);
30084 node = token->u.value;
30086 while (node && identifier_p (node)
30087 && (node == ridpointers [(int) RID_IN]
30088 || node == ridpointers [(int) RID_OUT]
30089 || node == ridpointers [(int) RID_INOUT]
30090 || node == ridpointers [(int) RID_BYCOPY]
30091 || node == ridpointers [(int) RID_BYREF]
30092 || node == ridpointers [(int) RID_ONEWAY]))
30094 quals = tree_cons (NULL_TREE, node, quals);
30095 cp_lexer_consume_token (parser->lexer);
30096 token = cp_lexer_peek_token (parser->lexer);
30097 node = token->u.value;
30100 return quals;
30103 /* Parse an Objective-C typename. */
30105 static tree
30106 cp_parser_objc_typename (cp_parser* parser)
30108 tree type_name = NULL_TREE;
30110 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30112 tree proto_quals, cp_type = NULL_TREE;
30114 matching_parens parens;
30115 parens.consume_open (parser); /* Eat '('. */
30116 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30118 /* An ObjC type name may consist of just protocol qualifiers, in which
30119 case the type shall default to 'id'. */
30120 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30122 cp_type = cp_parser_type_id (parser);
30124 /* If the type could not be parsed, an error has already
30125 been produced. For error recovery, behave as if it had
30126 not been specified, which will use the default type
30127 'id'. */
30128 if (cp_type == error_mark_node)
30130 cp_type = NULL_TREE;
30131 /* We need to skip to the closing parenthesis as
30132 cp_parser_type_id() does not seem to do it for
30133 us. */
30134 cp_parser_skip_to_closing_parenthesis (parser,
30135 /*recovering=*/true,
30136 /*or_comma=*/false,
30137 /*consume_paren=*/false);
30141 parens.require_close (parser);
30142 type_name = build_tree_list (proto_quals, cp_type);
30145 return type_name;
30148 /* Check to see if TYPE refers to an Objective-C selector name. */
30150 static bool
30151 cp_parser_objc_selector_p (enum cpp_ttype type)
30153 return (type == CPP_NAME || type == CPP_KEYWORD
30154 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30155 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30156 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30157 || type == CPP_XOR || type == CPP_XOR_EQ);
30160 /* Parse an Objective-C selector. */
30162 static tree
30163 cp_parser_objc_selector (cp_parser* parser)
30165 cp_token *token = cp_lexer_consume_token (parser->lexer);
30167 if (!cp_parser_objc_selector_p (token->type))
30169 error_at (token->location, "invalid Objective-C++ selector name");
30170 return error_mark_node;
30173 /* C++ operator names are allowed to appear in ObjC selectors. */
30174 switch (token->type)
30176 case CPP_AND_AND: return get_identifier ("and");
30177 case CPP_AND_EQ: return get_identifier ("and_eq");
30178 case CPP_AND: return get_identifier ("bitand");
30179 case CPP_OR: return get_identifier ("bitor");
30180 case CPP_COMPL: return get_identifier ("compl");
30181 case CPP_NOT: return get_identifier ("not");
30182 case CPP_NOT_EQ: return get_identifier ("not_eq");
30183 case CPP_OR_OR: return get_identifier ("or");
30184 case CPP_OR_EQ: return get_identifier ("or_eq");
30185 case CPP_XOR: return get_identifier ("xor");
30186 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30187 default: return token->u.value;
30191 /* Parse an Objective-C params list. */
30193 static tree
30194 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30196 tree params = NULL_TREE;
30197 bool maybe_unary_selector_p = true;
30198 cp_token *token = cp_lexer_peek_token (parser->lexer);
30200 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30202 tree selector = NULL_TREE, type_name, identifier;
30203 tree parm_attr = NULL_TREE;
30205 if (token->keyword == RID_ATTRIBUTE)
30206 break;
30208 if (token->type != CPP_COLON)
30209 selector = cp_parser_objc_selector (parser);
30211 /* Detect if we have a unary selector. */
30212 if (maybe_unary_selector_p
30213 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30215 params = selector; /* Might be followed by attributes. */
30216 break;
30219 maybe_unary_selector_p = false;
30220 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30222 /* Something went quite wrong. There should be a colon
30223 here, but there is not. Stop parsing parameters. */
30224 break;
30226 type_name = cp_parser_objc_typename (parser);
30227 /* New ObjC allows attributes on parameters too. */
30228 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30229 parm_attr = cp_parser_attributes_opt (parser);
30230 identifier = cp_parser_identifier (parser);
30232 params
30233 = chainon (params,
30234 objc_build_keyword_decl (selector,
30235 type_name,
30236 identifier,
30237 parm_attr));
30239 token = cp_lexer_peek_token (parser->lexer);
30242 if (params == NULL_TREE)
30244 cp_parser_error (parser, "objective-c++ method declaration is expected");
30245 return error_mark_node;
30248 /* We allow tail attributes for the method. */
30249 if (token->keyword == RID_ATTRIBUTE)
30251 *attributes = cp_parser_attributes_opt (parser);
30252 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30253 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30254 return params;
30255 cp_parser_error (parser,
30256 "method attributes must be specified at the end");
30257 return error_mark_node;
30260 if (params == NULL_TREE)
30262 cp_parser_error (parser, "objective-c++ method declaration is expected");
30263 return error_mark_node;
30265 return params;
30268 /* Parse the non-keyword Objective-C params. */
30270 static tree
30271 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30272 tree* attributes)
30274 tree params = make_node (TREE_LIST);
30275 cp_token *token = cp_lexer_peek_token (parser->lexer);
30276 *ellipsisp = false; /* Initially, assume no ellipsis. */
30278 while (token->type == CPP_COMMA)
30280 cp_parameter_declarator *parmdecl;
30281 tree parm;
30283 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30284 token = cp_lexer_peek_token (parser->lexer);
30286 if (token->type == CPP_ELLIPSIS)
30288 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30289 *ellipsisp = true;
30290 token = cp_lexer_peek_token (parser->lexer);
30291 break;
30294 /* TODO: parse attributes for tail parameters. */
30295 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30296 parm = grokdeclarator (parmdecl->declarator,
30297 &parmdecl->decl_specifiers,
30298 PARM, /*initialized=*/0,
30299 /*attrlist=*/NULL);
30301 chainon (params, build_tree_list (NULL_TREE, parm));
30302 token = cp_lexer_peek_token (parser->lexer);
30305 /* We allow tail attributes for the method. */
30306 if (token->keyword == RID_ATTRIBUTE)
30308 if (*attributes == NULL_TREE)
30310 *attributes = cp_parser_attributes_opt (parser);
30311 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30312 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30313 return params;
30315 else
30316 /* We have an error, but parse the attributes, so that we can
30317 carry on. */
30318 *attributes = cp_parser_attributes_opt (parser);
30320 cp_parser_error (parser,
30321 "method attributes must be specified at the end");
30322 return error_mark_node;
30325 return params;
30328 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30330 static void
30331 cp_parser_objc_interstitial_code (cp_parser* parser)
30333 cp_token *token = cp_lexer_peek_token (parser->lexer);
30335 /* If the next token is `extern' and the following token is a string
30336 literal, then we have a linkage specification. */
30337 if (token->keyword == RID_EXTERN
30338 && cp_parser_is_pure_string_literal
30339 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30340 cp_parser_linkage_specification (parser);
30341 /* Handle #pragma, if any. */
30342 else if (token->type == CPP_PRAGMA)
30343 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30344 /* Allow stray semicolons. */
30345 else if (token->type == CPP_SEMICOLON)
30346 cp_lexer_consume_token (parser->lexer);
30347 /* Mark methods as optional or required, when building protocols. */
30348 else if (token->keyword == RID_AT_OPTIONAL)
30350 cp_lexer_consume_token (parser->lexer);
30351 objc_set_method_opt (true);
30353 else if (token->keyword == RID_AT_REQUIRED)
30355 cp_lexer_consume_token (parser->lexer);
30356 objc_set_method_opt (false);
30358 else if (token->keyword == RID_NAMESPACE)
30359 cp_parser_namespace_definition (parser);
30360 /* Other stray characters must generate errors. */
30361 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30363 cp_lexer_consume_token (parser->lexer);
30364 error ("stray %qs between Objective-C++ methods",
30365 token->type == CPP_OPEN_BRACE ? "{" : "}");
30367 /* Finally, try to parse a block-declaration, or a function-definition. */
30368 else
30369 cp_parser_block_declaration (parser, /*statement_p=*/false);
30372 /* Parse a method signature. */
30374 static tree
30375 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30377 tree rettype, kwdparms, optparms;
30378 bool ellipsis = false;
30379 bool is_class_method;
30381 is_class_method = cp_parser_objc_method_type (parser);
30382 rettype = cp_parser_objc_typename (parser);
30383 *attributes = NULL_TREE;
30384 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30385 if (kwdparms == error_mark_node)
30386 return error_mark_node;
30387 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30388 if (optparms == error_mark_node)
30389 return error_mark_node;
30391 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30394 static bool
30395 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30397 tree tattr;
30398 cp_lexer_save_tokens (parser->lexer);
30399 tattr = cp_parser_attributes_opt (parser);
30400 gcc_assert (tattr) ;
30402 /* If the attributes are followed by a method introducer, this is not allowed.
30403 Dump the attributes and flag the situation. */
30404 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30405 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30406 return true;
30408 /* Otherwise, the attributes introduce some interstitial code, possibly so
30409 rewind to allow that check. */
30410 cp_lexer_rollback_tokens (parser->lexer);
30411 return false;
30414 /* Parse an Objective-C method prototype list. */
30416 static void
30417 cp_parser_objc_method_prototype_list (cp_parser* parser)
30419 cp_token *token = cp_lexer_peek_token (parser->lexer);
30421 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30423 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30425 tree attributes, sig;
30426 bool is_class_method;
30427 if (token->type == CPP_PLUS)
30428 is_class_method = true;
30429 else
30430 is_class_method = false;
30431 sig = cp_parser_objc_method_signature (parser, &attributes);
30432 if (sig == error_mark_node)
30434 cp_parser_skip_to_end_of_block_or_statement (parser);
30435 token = cp_lexer_peek_token (parser->lexer);
30436 continue;
30438 objc_add_method_declaration (is_class_method, sig, attributes);
30439 cp_parser_consume_semicolon_at_end_of_statement (parser);
30441 else if (token->keyword == RID_AT_PROPERTY)
30442 cp_parser_objc_at_property_declaration (parser);
30443 else if (token->keyword == RID_ATTRIBUTE
30444 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30445 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30446 OPT_Wattributes,
30447 "prefix attributes are ignored for methods");
30448 else
30449 /* Allow for interspersed non-ObjC++ code. */
30450 cp_parser_objc_interstitial_code (parser);
30452 token = cp_lexer_peek_token (parser->lexer);
30455 if (token->type != CPP_EOF)
30456 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30457 else
30458 cp_parser_error (parser, "expected %<@end%>");
30460 objc_finish_interface ();
30463 /* Parse an Objective-C method definition list. */
30465 static void
30466 cp_parser_objc_method_definition_list (cp_parser* parser)
30468 cp_token *token = cp_lexer_peek_token (parser->lexer);
30470 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30472 tree meth;
30474 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30476 cp_token *ptk;
30477 tree sig, attribute;
30478 bool is_class_method;
30479 if (token->type == CPP_PLUS)
30480 is_class_method = true;
30481 else
30482 is_class_method = false;
30483 push_deferring_access_checks (dk_deferred);
30484 sig = cp_parser_objc_method_signature (parser, &attribute);
30485 if (sig == error_mark_node)
30487 cp_parser_skip_to_end_of_block_or_statement (parser);
30488 token = cp_lexer_peek_token (parser->lexer);
30489 continue;
30491 objc_start_method_definition (is_class_method, sig, attribute,
30492 NULL_TREE);
30494 /* For historical reasons, we accept an optional semicolon. */
30495 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30496 cp_lexer_consume_token (parser->lexer);
30498 ptk = cp_lexer_peek_token (parser->lexer);
30499 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30500 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30502 perform_deferred_access_checks (tf_warning_or_error);
30503 stop_deferring_access_checks ();
30504 meth = cp_parser_function_definition_after_declarator (parser,
30505 false);
30506 pop_deferring_access_checks ();
30507 objc_finish_method_definition (meth);
30510 /* The following case will be removed once @synthesize is
30511 completely implemented. */
30512 else if (token->keyword == RID_AT_PROPERTY)
30513 cp_parser_objc_at_property_declaration (parser);
30514 else if (token->keyword == RID_AT_SYNTHESIZE)
30515 cp_parser_objc_at_synthesize_declaration (parser);
30516 else if (token->keyword == RID_AT_DYNAMIC)
30517 cp_parser_objc_at_dynamic_declaration (parser);
30518 else if (token->keyword == RID_ATTRIBUTE
30519 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30520 warning_at (token->location, OPT_Wattributes,
30521 "prefix attributes are ignored for methods");
30522 else
30523 /* Allow for interspersed non-ObjC++ code. */
30524 cp_parser_objc_interstitial_code (parser);
30526 token = cp_lexer_peek_token (parser->lexer);
30529 if (token->type != CPP_EOF)
30530 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30531 else
30532 cp_parser_error (parser, "expected %<@end%>");
30534 objc_finish_implementation ();
30537 /* Parse Objective-C ivars. */
30539 static void
30540 cp_parser_objc_class_ivars (cp_parser* parser)
30542 cp_token *token = cp_lexer_peek_token (parser->lexer);
30544 if (token->type != CPP_OPEN_BRACE)
30545 return; /* No ivars specified. */
30547 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30548 token = cp_lexer_peek_token (parser->lexer);
30550 while (token->type != CPP_CLOSE_BRACE
30551 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30553 cp_decl_specifier_seq declspecs;
30554 int decl_class_or_enum_p;
30555 tree prefix_attributes;
30557 cp_parser_objc_visibility_spec (parser);
30559 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30560 break;
30562 cp_parser_decl_specifier_seq (parser,
30563 CP_PARSER_FLAGS_OPTIONAL,
30564 &declspecs,
30565 &decl_class_or_enum_p);
30567 /* auto, register, static, extern, mutable. */
30568 if (declspecs.storage_class != sc_none)
30570 cp_parser_error (parser, "invalid type for instance variable");
30571 declspecs.storage_class = sc_none;
30574 /* thread_local. */
30575 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30577 cp_parser_error (parser, "invalid type for instance variable");
30578 declspecs.locations[ds_thread] = 0;
30581 /* typedef. */
30582 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30584 cp_parser_error (parser, "invalid type for instance variable");
30585 declspecs.locations[ds_typedef] = 0;
30588 prefix_attributes = declspecs.attributes;
30589 declspecs.attributes = NULL_TREE;
30591 /* Keep going until we hit the `;' at the end of the
30592 declaration. */
30593 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30595 tree width = NULL_TREE, attributes, first_attribute, decl;
30596 cp_declarator *declarator = NULL;
30597 int ctor_dtor_or_conv_p;
30599 /* Check for a (possibly unnamed) bitfield declaration. */
30600 token = cp_lexer_peek_token (parser->lexer);
30601 if (token->type == CPP_COLON)
30602 goto eat_colon;
30604 if (token->type == CPP_NAME
30605 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30606 == CPP_COLON))
30608 /* Get the name of the bitfield. */
30609 declarator = make_id_declarator (NULL_TREE,
30610 cp_parser_identifier (parser),
30611 sfk_none);
30613 eat_colon:
30614 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30615 /* Get the width of the bitfield. */
30616 width
30617 = cp_parser_constant_expression (parser);
30619 else
30621 /* Parse the declarator. */
30622 declarator
30623 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30624 &ctor_dtor_or_conv_p,
30625 /*parenthesized_p=*/NULL,
30626 /*member_p=*/false,
30627 /*friend_p=*/false);
30630 /* Look for attributes that apply to the ivar. */
30631 attributes = cp_parser_attributes_opt (parser);
30632 /* Remember which attributes are prefix attributes and
30633 which are not. */
30634 first_attribute = attributes;
30635 /* Combine the attributes. */
30636 attributes = attr_chainon (prefix_attributes, attributes);
30638 if (width)
30639 /* Create the bitfield declaration. */
30640 decl = grokbitfield (declarator, &declspecs,
30641 width, NULL_TREE, attributes);
30642 else
30643 decl = grokfield (declarator, &declspecs,
30644 NULL_TREE, /*init_const_expr_p=*/false,
30645 NULL_TREE, attributes);
30647 /* Add the instance variable. */
30648 if (decl != error_mark_node && decl != NULL_TREE)
30649 objc_add_instance_variable (decl);
30651 /* Reset PREFIX_ATTRIBUTES. */
30652 if (attributes != error_mark_node)
30654 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30655 attributes = TREE_CHAIN (attributes);
30656 if (attributes)
30657 TREE_CHAIN (attributes) = NULL_TREE;
30660 token = cp_lexer_peek_token (parser->lexer);
30662 if (token->type == CPP_COMMA)
30664 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30665 continue;
30667 break;
30670 cp_parser_consume_semicolon_at_end_of_statement (parser);
30671 token = cp_lexer_peek_token (parser->lexer);
30674 if (token->keyword == RID_AT_END)
30675 cp_parser_error (parser, "expected %<}%>");
30677 /* Do not consume the RID_AT_END, so it will be read again as terminating
30678 the @interface of @implementation. */
30679 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30680 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30682 /* For historical reasons, we accept an optional semicolon. */
30683 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30684 cp_lexer_consume_token (parser->lexer);
30687 /* Parse an Objective-C protocol declaration. */
30689 static void
30690 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30692 tree proto, protorefs;
30693 cp_token *tok;
30695 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30698 tok = cp_lexer_peek_token (parser->lexer);
30699 error_at (tok->location, "identifier expected after %<@protocol%>");
30700 cp_parser_consume_semicolon_at_end_of_statement (parser);
30701 return;
30704 /* See if we have a forward declaration or a definition. */
30705 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30707 /* Try a forward declaration first. */
30708 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30710 while (true)
30712 tree id;
30714 id = cp_parser_identifier (parser);
30715 if (id == error_mark_node)
30716 break;
30718 objc_declare_protocol (id, attributes);
30720 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30721 cp_lexer_consume_token (parser->lexer);
30722 else
30723 break;
30725 cp_parser_consume_semicolon_at_end_of_statement (parser);
30728 /* Ok, we got a full-fledged definition (or at least should). */
30729 else
30731 proto = cp_parser_identifier (parser);
30732 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30733 objc_start_protocol (proto, protorefs, attributes);
30734 cp_parser_objc_method_prototype_list (parser);
30738 /* Parse an Objective-C superclass or category. */
30740 static void
30741 cp_parser_objc_superclass_or_category (cp_parser *parser,
30742 bool iface_p,
30743 tree *super,
30744 tree *categ, bool *is_class_extension)
30746 cp_token *next = cp_lexer_peek_token (parser->lexer);
30748 *super = *categ = NULL_TREE;
30749 *is_class_extension = false;
30750 if (next->type == CPP_COLON)
30752 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30753 *super = cp_parser_identifier (parser);
30755 else if (next->type == CPP_OPEN_PAREN)
30757 matching_parens parens;
30758 parens.consume_open (parser); /* Eat '('. */
30760 /* If there is no category name, and this is an @interface, we
30761 have a class extension. */
30762 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30764 *categ = NULL_TREE;
30765 *is_class_extension = true;
30767 else
30768 *categ = cp_parser_identifier (parser);
30770 parens.require_close (parser);
30774 /* Parse an Objective-C class interface. */
30776 static void
30777 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30779 tree name, super, categ, protos;
30780 bool is_class_extension;
30782 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30783 name = cp_parser_identifier (parser);
30784 if (name == error_mark_node)
30786 /* It's hard to recover because even if valid @interface stuff
30787 is to follow, we can't compile it (or validate it) if we
30788 don't even know which class it refers to. Let's assume this
30789 was a stray '@interface' token in the stream and skip it.
30791 return;
30793 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30794 &is_class_extension);
30795 protos = cp_parser_objc_protocol_refs_opt (parser);
30797 /* We have either a class or a category on our hands. */
30798 if (categ || is_class_extension)
30799 objc_start_category_interface (name, categ, protos, attributes);
30800 else
30802 objc_start_class_interface (name, super, protos, attributes);
30803 /* Handle instance variable declarations, if any. */
30804 cp_parser_objc_class_ivars (parser);
30805 objc_continue_interface ();
30808 cp_parser_objc_method_prototype_list (parser);
30811 /* Parse an Objective-C class implementation. */
30813 static void
30814 cp_parser_objc_class_implementation (cp_parser* parser)
30816 tree name, super, categ;
30817 bool is_class_extension;
30819 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30820 name = cp_parser_identifier (parser);
30821 if (name == error_mark_node)
30823 /* It's hard to recover because even if valid @implementation
30824 stuff is to follow, we can't compile it (or validate it) if
30825 we don't even know which class it refers to. Let's assume
30826 this was a stray '@implementation' token in the stream and
30827 skip it.
30829 return;
30831 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30832 &is_class_extension);
30834 /* We have either a class or a category on our hands. */
30835 if (categ)
30836 objc_start_category_implementation (name, categ);
30837 else
30839 objc_start_class_implementation (name, super);
30840 /* Handle instance variable declarations, if any. */
30841 cp_parser_objc_class_ivars (parser);
30842 objc_continue_implementation ();
30845 cp_parser_objc_method_definition_list (parser);
30848 /* Consume the @end token and finish off the implementation. */
30850 static void
30851 cp_parser_objc_end_implementation (cp_parser* parser)
30853 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30854 objc_finish_implementation ();
30857 /* Parse an Objective-C declaration. */
30859 static void
30860 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30862 /* Try to figure out what kind of declaration is present. */
30863 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30865 if (attributes)
30866 switch (kwd->keyword)
30868 case RID_AT_ALIAS:
30869 case RID_AT_CLASS:
30870 case RID_AT_END:
30871 error_at (kwd->location, "attributes may not be specified before"
30872 " the %<@%D%> Objective-C++ keyword",
30873 kwd->u.value);
30874 attributes = NULL;
30875 break;
30876 case RID_AT_IMPLEMENTATION:
30877 warning_at (kwd->location, OPT_Wattributes,
30878 "prefix attributes are ignored before %<@%D%>",
30879 kwd->u.value);
30880 attributes = NULL;
30881 default:
30882 break;
30885 switch (kwd->keyword)
30887 case RID_AT_ALIAS:
30888 cp_parser_objc_alias_declaration (parser);
30889 break;
30890 case RID_AT_CLASS:
30891 cp_parser_objc_class_declaration (parser);
30892 break;
30893 case RID_AT_PROTOCOL:
30894 cp_parser_objc_protocol_declaration (parser, attributes);
30895 break;
30896 case RID_AT_INTERFACE:
30897 cp_parser_objc_class_interface (parser, attributes);
30898 break;
30899 case RID_AT_IMPLEMENTATION:
30900 cp_parser_objc_class_implementation (parser);
30901 break;
30902 case RID_AT_END:
30903 cp_parser_objc_end_implementation (parser);
30904 break;
30905 default:
30906 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30907 kwd->u.value);
30908 cp_parser_skip_to_end_of_block_or_statement (parser);
30912 /* Parse an Objective-C try-catch-finally statement.
30914 objc-try-catch-finally-stmt:
30915 @try compound-statement objc-catch-clause-seq [opt]
30916 objc-finally-clause [opt]
30918 objc-catch-clause-seq:
30919 objc-catch-clause objc-catch-clause-seq [opt]
30921 objc-catch-clause:
30922 @catch ( objc-exception-declaration ) compound-statement
30924 objc-finally-clause:
30925 @finally compound-statement
30927 objc-exception-declaration:
30928 parameter-declaration
30929 '...'
30931 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30933 Returns NULL_TREE.
30935 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30936 for C. Keep them in sync. */
30938 static tree
30939 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30941 location_t location;
30942 tree stmt;
30944 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30945 location = cp_lexer_peek_token (parser->lexer)->location;
30946 objc_maybe_warn_exceptions (location);
30947 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30948 node, lest it get absorbed into the surrounding block. */
30949 stmt = push_stmt_list ();
30950 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30951 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30953 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30955 cp_parameter_declarator *parm;
30956 tree parameter_declaration = error_mark_node;
30957 bool seen_open_paren = false;
30958 matching_parens parens;
30960 cp_lexer_consume_token (parser->lexer);
30961 if (parens.require_open (parser))
30962 seen_open_paren = true;
30963 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30965 /* We have "@catch (...)" (where the '...' are literally
30966 what is in the code). Skip the '...'.
30967 parameter_declaration is set to NULL_TREE, and
30968 objc_being_catch_clauses() knows that that means
30969 '...'. */
30970 cp_lexer_consume_token (parser->lexer);
30971 parameter_declaration = NULL_TREE;
30973 else
30975 /* We have "@catch (NSException *exception)" or something
30976 like that. Parse the parameter declaration. */
30977 parm = cp_parser_parameter_declaration (parser, false, NULL);
30978 if (parm == NULL)
30979 parameter_declaration = error_mark_node;
30980 else
30981 parameter_declaration = grokdeclarator (parm->declarator,
30982 &parm->decl_specifiers,
30983 PARM, /*initialized=*/0,
30984 /*attrlist=*/NULL);
30986 if (seen_open_paren)
30987 parens.require_close (parser);
30988 else
30990 /* If there was no open parenthesis, we are recovering from
30991 an error, and we are trying to figure out what mistake
30992 the user has made. */
30994 /* If there is an immediate closing parenthesis, the user
30995 probably forgot the opening one (ie, they typed "@catch
30996 NSException *e)". Parse the closing parenthesis and keep
30997 going. */
30998 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30999 cp_lexer_consume_token (parser->lexer);
31001 /* If these is no immediate closing parenthesis, the user
31002 probably doesn't know that parenthesis are required at
31003 all (ie, they typed "@catch NSException *e"). So, just
31004 forget about the closing parenthesis and keep going. */
31006 objc_begin_catch_clause (parameter_declaration);
31007 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31008 objc_finish_catch_clause ();
31010 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
31012 cp_lexer_consume_token (parser->lexer);
31013 location = cp_lexer_peek_token (parser->lexer)->location;
31014 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31015 node, lest it get absorbed into the surrounding block. */
31016 stmt = push_stmt_list ();
31017 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31018 objc_build_finally_clause (location, pop_stmt_list (stmt));
31021 return objc_finish_try_stmt ();
31024 /* Parse an Objective-C synchronized statement.
31026 objc-synchronized-stmt:
31027 @synchronized ( expression ) compound-statement
31029 Returns NULL_TREE. */
31031 static tree
31032 cp_parser_objc_synchronized_statement (cp_parser *parser)
31034 location_t location;
31035 tree lock, stmt;
31037 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31039 location = cp_lexer_peek_token (parser->lexer)->location;
31040 objc_maybe_warn_exceptions (location);
31041 matching_parens parens;
31042 parens.require_open (parser);
31043 lock = cp_parser_expression (parser);
31044 parens.require_close (parser);
31046 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31047 node, lest it get absorbed into the surrounding block. */
31048 stmt = push_stmt_list ();
31049 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31051 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31054 /* Parse an Objective-C throw statement.
31056 objc-throw-stmt:
31057 @throw assignment-expression [opt] ;
31059 Returns a constructed '@throw' statement. */
31061 static tree
31062 cp_parser_objc_throw_statement (cp_parser *parser)
31064 tree expr = NULL_TREE;
31065 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31067 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31069 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31070 expr = cp_parser_expression (parser);
31072 cp_parser_consume_semicolon_at_end_of_statement (parser);
31074 return objc_build_throw_stmt (loc, expr);
31077 /* Parse an Objective-C statement. */
31079 static tree
31080 cp_parser_objc_statement (cp_parser * parser)
31082 /* Try to figure out what kind of declaration is present. */
31083 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31085 switch (kwd->keyword)
31087 case RID_AT_TRY:
31088 return cp_parser_objc_try_catch_finally_statement (parser);
31089 case RID_AT_SYNCHRONIZED:
31090 return cp_parser_objc_synchronized_statement (parser);
31091 case RID_AT_THROW:
31092 return cp_parser_objc_throw_statement (parser);
31093 default:
31094 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31095 kwd->u.value);
31096 cp_parser_skip_to_end_of_block_or_statement (parser);
31099 return error_mark_node;
31102 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31103 look ahead to see if an objc keyword follows the attributes. This
31104 is to detect the use of prefix attributes on ObjC @interface and
31105 @protocol. */
31107 static bool
31108 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31110 cp_lexer_save_tokens (parser->lexer);
31111 *attrib = cp_parser_attributes_opt (parser);
31112 gcc_assert (*attrib);
31113 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31115 cp_lexer_commit_tokens (parser->lexer);
31116 return true;
31118 cp_lexer_rollback_tokens (parser->lexer);
31119 return false;
31122 /* This routine is a minimal replacement for
31123 c_parser_struct_declaration () used when parsing the list of
31124 types/names or ObjC++ properties. For example, when parsing the
31125 code
31127 @property (readonly) int a, b, c;
31129 this function is responsible for parsing "int a, int b, int c" and
31130 returning the declarations as CHAIN of DECLs.
31132 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31133 similar parsing. */
31134 static tree
31135 cp_parser_objc_struct_declaration (cp_parser *parser)
31137 tree decls = NULL_TREE;
31138 cp_decl_specifier_seq declspecs;
31139 int decl_class_or_enum_p;
31140 tree prefix_attributes;
31142 cp_parser_decl_specifier_seq (parser,
31143 CP_PARSER_FLAGS_NONE,
31144 &declspecs,
31145 &decl_class_or_enum_p);
31147 if (declspecs.type == error_mark_node)
31148 return error_mark_node;
31150 /* auto, register, static, extern, mutable. */
31151 if (declspecs.storage_class != sc_none)
31153 cp_parser_error (parser, "invalid type for property");
31154 declspecs.storage_class = sc_none;
31157 /* thread_local. */
31158 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31160 cp_parser_error (parser, "invalid type for property");
31161 declspecs.locations[ds_thread] = 0;
31164 /* typedef. */
31165 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31167 cp_parser_error (parser, "invalid type for property");
31168 declspecs.locations[ds_typedef] = 0;
31171 prefix_attributes = declspecs.attributes;
31172 declspecs.attributes = NULL_TREE;
31174 /* Keep going until we hit the `;' at the end of the declaration. */
31175 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31177 tree attributes, first_attribute, decl;
31178 cp_declarator *declarator;
31179 cp_token *token;
31181 /* Parse the declarator. */
31182 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31183 NULL, NULL, false, false);
31185 /* Look for attributes that apply to the ivar. */
31186 attributes = cp_parser_attributes_opt (parser);
31187 /* Remember which attributes are prefix attributes and
31188 which are not. */
31189 first_attribute = attributes;
31190 /* Combine the attributes. */
31191 attributes = attr_chainon (prefix_attributes, attributes);
31193 decl = grokfield (declarator, &declspecs,
31194 NULL_TREE, /*init_const_expr_p=*/false,
31195 NULL_TREE, attributes);
31197 if (decl == error_mark_node || decl == NULL_TREE)
31198 return error_mark_node;
31200 /* Reset PREFIX_ATTRIBUTES. */
31201 if (attributes != error_mark_node)
31203 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31204 attributes = TREE_CHAIN (attributes);
31205 if (attributes)
31206 TREE_CHAIN (attributes) = NULL_TREE;
31209 DECL_CHAIN (decl) = decls;
31210 decls = decl;
31212 token = cp_lexer_peek_token (parser->lexer);
31213 if (token->type == CPP_COMMA)
31215 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31216 continue;
31218 else
31219 break;
31221 return decls;
31224 /* Parse an Objective-C @property declaration. The syntax is:
31226 objc-property-declaration:
31227 '@property' objc-property-attributes[opt] struct-declaration ;
31229 objc-property-attributes:
31230 '(' objc-property-attribute-list ')'
31232 objc-property-attribute-list:
31233 objc-property-attribute
31234 objc-property-attribute-list, objc-property-attribute
31236 objc-property-attribute
31237 'getter' = identifier
31238 'setter' = identifier
31239 'readonly'
31240 'readwrite'
31241 'assign'
31242 'retain'
31243 'copy'
31244 'nonatomic'
31246 For example:
31247 @property NSString *name;
31248 @property (readonly) id object;
31249 @property (retain, nonatomic, getter=getTheName) id name;
31250 @property int a, b, c;
31252 PS: This function is identical to
31253 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31254 static void
31255 cp_parser_objc_at_property_declaration (cp_parser *parser)
31257 /* The following variables hold the attributes of the properties as
31258 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31259 seen. When we see an attribute, we set them to 'true' (if they
31260 are boolean properties) or to the identifier (if they have an
31261 argument, ie, for getter and setter). Note that here we only
31262 parse the list of attributes, check the syntax and accumulate the
31263 attributes that we find. objc_add_property_declaration() will
31264 then process the information. */
31265 bool property_assign = false;
31266 bool property_copy = false;
31267 tree property_getter_ident = NULL_TREE;
31268 bool property_nonatomic = false;
31269 bool property_readonly = false;
31270 bool property_readwrite = false;
31271 bool property_retain = false;
31272 tree property_setter_ident = NULL_TREE;
31274 /* 'properties' is the list of properties that we read. Usually a
31275 single one, but maybe more (eg, in "@property int a, b, c;" there
31276 are three). */
31277 tree properties;
31278 location_t loc;
31280 loc = cp_lexer_peek_token (parser->lexer)->location;
31282 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31284 /* Parse the optional attribute list... */
31285 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31287 /* Eat the '('. */
31288 matching_parens parens;
31289 parens.consume_open (parser);
31291 while (true)
31293 bool syntax_error = false;
31294 cp_token *token = cp_lexer_peek_token (parser->lexer);
31295 enum rid keyword;
31297 if (token->type != CPP_NAME)
31299 cp_parser_error (parser, "expected identifier");
31300 break;
31302 keyword = C_RID_CODE (token->u.value);
31303 cp_lexer_consume_token (parser->lexer);
31304 switch (keyword)
31306 case RID_ASSIGN: property_assign = true; break;
31307 case RID_COPY: property_copy = true; break;
31308 case RID_NONATOMIC: property_nonatomic = true; break;
31309 case RID_READONLY: property_readonly = true; break;
31310 case RID_READWRITE: property_readwrite = true; break;
31311 case RID_RETAIN: property_retain = true; break;
31313 case RID_GETTER:
31314 case RID_SETTER:
31315 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31317 if (keyword == RID_GETTER)
31318 cp_parser_error (parser,
31319 "missing %<=%> (after %<getter%> attribute)");
31320 else
31321 cp_parser_error (parser,
31322 "missing %<=%> (after %<setter%> attribute)");
31323 syntax_error = true;
31324 break;
31326 cp_lexer_consume_token (parser->lexer); /* eat the = */
31327 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31329 cp_parser_error (parser, "expected identifier");
31330 syntax_error = true;
31331 break;
31333 if (keyword == RID_SETTER)
31335 if (property_setter_ident != NULL_TREE)
31337 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31338 cp_lexer_consume_token (parser->lexer);
31340 else
31341 property_setter_ident = cp_parser_objc_selector (parser);
31342 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31343 cp_parser_error (parser, "setter name must terminate with %<:%>");
31344 else
31345 cp_lexer_consume_token (parser->lexer);
31347 else
31349 if (property_getter_ident != NULL_TREE)
31351 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31352 cp_lexer_consume_token (parser->lexer);
31354 else
31355 property_getter_ident = cp_parser_objc_selector (parser);
31357 break;
31358 default:
31359 cp_parser_error (parser, "unknown property attribute");
31360 syntax_error = true;
31361 break;
31364 if (syntax_error)
31365 break;
31367 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31368 cp_lexer_consume_token (parser->lexer);
31369 else
31370 break;
31373 /* FIXME: "@property (setter, assign);" will generate a spurious
31374 "error: expected ‘)’ before ‘,’ token". This is because
31375 cp_parser_require, unlike the C counterpart, will produce an
31376 error even if we are in error recovery. */
31377 if (!parens.require_close (parser))
31379 cp_parser_skip_to_closing_parenthesis (parser,
31380 /*recovering=*/true,
31381 /*or_comma=*/false,
31382 /*consume_paren=*/true);
31386 /* ... and the property declaration(s). */
31387 properties = cp_parser_objc_struct_declaration (parser);
31389 if (properties == error_mark_node)
31391 cp_parser_skip_to_end_of_statement (parser);
31392 /* If the next token is now a `;', consume it. */
31393 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31394 cp_lexer_consume_token (parser->lexer);
31395 return;
31398 if (properties == NULL_TREE)
31399 cp_parser_error (parser, "expected identifier");
31400 else
31402 /* Comma-separated properties are chained together in
31403 reverse order; add them one by one. */
31404 properties = nreverse (properties);
31406 for (; properties; properties = TREE_CHAIN (properties))
31407 objc_add_property_declaration (loc, copy_node (properties),
31408 property_readonly, property_readwrite,
31409 property_assign, property_retain,
31410 property_copy, property_nonatomic,
31411 property_getter_ident, property_setter_ident);
31414 cp_parser_consume_semicolon_at_end_of_statement (parser);
31417 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31419 objc-synthesize-declaration:
31420 @synthesize objc-synthesize-identifier-list ;
31422 objc-synthesize-identifier-list:
31423 objc-synthesize-identifier
31424 objc-synthesize-identifier-list, objc-synthesize-identifier
31426 objc-synthesize-identifier
31427 identifier
31428 identifier = identifier
31430 For example:
31431 @synthesize MyProperty;
31432 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31434 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31435 for C. Keep them in sync.
31437 static void
31438 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31440 tree list = NULL_TREE;
31441 location_t loc;
31442 loc = cp_lexer_peek_token (parser->lexer)->location;
31444 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31445 while (true)
31447 tree property, ivar;
31448 property = cp_parser_identifier (parser);
31449 if (property == error_mark_node)
31451 cp_parser_consume_semicolon_at_end_of_statement (parser);
31452 return;
31454 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31456 cp_lexer_consume_token (parser->lexer);
31457 ivar = cp_parser_identifier (parser);
31458 if (ivar == error_mark_node)
31460 cp_parser_consume_semicolon_at_end_of_statement (parser);
31461 return;
31464 else
31465 ivar = NULL_TREE;
31466 list = chainon (list, build_tree_list (ivar, property));
31467 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31468 cp_lexer_consume_token (parser->lexer);
31469 else
31470 break;
31472 cp_parser_consume_semicolon_at_end_of_statement (parser);
31473 objc_add_synthesize_declaration (loc, list);
31476 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31478 objc-dynamic-declaration:
31479 @dynamic identifier-list ;
31481 For example:
31482 @dynamic MyProperty;
31483 @dynamic MyProperty, AnotherProperty;
31485 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31486 for C. Keep them in sync.
31488 static void
31489 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31491 tree list = NULL_TREE;
31492 location_t loc;
31493 loc = cp_lexer_peek_token (parser->lexer)->location;
31495 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31496 while (true)
31498 tree property;
31499 property = cp_parser_identifier (parser);
31500 if (property == error_mark_node)
31502 cp_parser_consume_semicolon_at_end_of_statement (parser);
31503 return;
31505 list = chainon (list, build_tree_list (NULL, property));
31506 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31507 cp_lexer_consume_token (parser->lexer);
31508 else
31509 break;
31511 cp_parser_consume_semicolon_at_end_of_statement (parser);
31512 objc_add_dynamic_declaration (loc, list);
31516 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
31518 /* Returns name of the next clause.
31519 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31520 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31521 returned and the token is consumed. */
31523 static pragma_omp_clause
31524 cp_parser_omp_clause_name (cp_parser *parser)
31526 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31528 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31529 result = PRAGMA_OACC_CLAUSE_AUTO;
31530 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31531 result = PRAGMA_OMP_CLAUSE_IF;
31532 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31533 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31534 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31535 result = PRAGMA_OACC_CLAUSE_DELETE;
31536 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31537 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31538 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31539 result = PRAGMA_OMP_CLAUSE_FOR;
31540 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31542 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31543 const char *p = IDENTIFIER_POINTER (id);
31545 switch (p[0])
31547 case 'a':
31548 if (!strcmp ("aligned", p))
31549 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31550 else if (!strcmp ("async", p))
31551 result = PRAGMA_OACC_CLAUSE_ASYNC;
31552 break;
31553 case 'c':
31554 if (!strcmp ("collapse", p))
31555 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31556 else if (!strcmp ("copy", p))
31557 result = PRAGMA_OACC_CLAUSE_COPY;
31558 else if (!strcmp ("copyin", p))
31559 result = PRAGMA_OMP_CLAUSE_COPYIN;
31560 else if (!strcmp ("copyout", p))
31561 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31562 else if (!strcmp ("copyprivate", p))
31563 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31564 else if (!strcmp ("create", p))
31565 result = PRAGMA_OACC_CLAUSE_CREATE;
31566 break;
31567 case 'd':
31568 if (!strcmp ("defaultmap", p))
31569 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31570 else if (!strcmp ("depend", p))
31571 result = PRAGMA_OMP_CLAUSE_DEPEND;
31572 else if (!strcmp ("device", p))
31573 result = PRAGMA_OMP_CLAUSE_DEVICE;
31574 else if (!strcmp ("deviceptr", p))
31575 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31576 else if (!strcmp ("device_resident", p))
31577 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31578 else if (!strcmp ("dist_schedule", p))
31579 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31580 break;
31581 case 'f':
31582 if (!strcmp ("final", p))
31583 result = PRAGMA_OMP_CLAUSE_FINAL;
31584 else if (!strcmp ("finalize", p))
31585 result = PRAGMA_OACC_CLAUSE_FINALIZE;
31586 else if (!strcmp ("firstprivate", p))
31587 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31588 else if (!strcmp ("from", p))
31589 result = PRAGMA_OMP_CLAUSE_FROM;
31590 break;
31591 case 'g':
31592 if (!strcmp ("gang", p))
31593 result = PRAGMA_OACC_CLAUSE_GANG;
31594 else if (!strcmp ("grainsize", p))
31595 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31596 break;
31597 case 'h':
31598 if (!strcmp ("hint", p))
31599 result = PRAGMA_OMP_CLAUSE_HINT;
31600 else if (!strcmp ("host", p))
31601 result = PRAGMA_OACC_CLAUSE_HOST;
31602 break;
31603 case 'i':
31604 if (!strcmp ("if_present", p))
31605 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
31606 else if (!strcmp ("in_reduction", p))
31607 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
31608 else if (!strcmp ("inbranch", p))
31609 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31610 else if (!strcmp ("independent", p))
31611 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31612 else if (!strcmp ("is_device_ptr", p))
31613 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31614 break;
31615 case 'l':
31616 if (!strcmp ("lastprivate", p))
31617 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31618 else if (!strcmp ("linear", p))
31619 result = PRAGMA_OMP_CLAUSE_LINEAR;
31620 else if (!strcmp ("link", p))
31621 result = PRAGMA_OMP_CLAUSE_LINK;
31622 break;
31623 case 'm':
31624 if (!strcmp ("map", p))
31625 result = PRAGMA_OMP_CLAUSE_MAP;
31626 else if (!strcmp ("mergeable", p))
31627 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31628 break;
31629 case 'n':
31630 if (!strcmp ("nogroup", p))
31631 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31632 else if (!strcmp ("nontemporal", p))
31633 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
31634 else if (!strcmp ("notinbranch", p))
31635 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31636 else if (!strcmp ("nowait", p))
31637 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31638 else if (!strcmp ("num_gangs", p))
31639 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31640 else if (!strcmp ("num_tasks", p))
31641 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31642 else if (!strcmp ("num_teams", p))
31643 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31644 else if (!strcmp ("num_threads", p))
31645 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31646 else if (!strcmp ("num_workers", p))
31647 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31648 break;
31649 case 'o':
31650 if (!strcmp ("ordered", p))
31651 result = PRAGMA_OMP_CLAUSE_ORDERED;
31652 break;
31653 case 'p':
31654 if (!strcmp ("parallel", p))
31655 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31656 else if (!strcmp ("present", p))
31657 result = PRAGMA_OACC_CLAUSE_PRESENT;
31658 else if (!strcmp ("present_or_copy", p)
31659 || !strcmp ("pcopy", p))
31660 result = PRAGMA_OACC_CLAUSE_COPY;
31661 else if (!strcmp ("present_or_copyin", p)
31662 || !strcmp ("pcopyin", p))
31663 result = PRAGMA_OACC_CLAUSE_COPYIN;
31664 else if (!strcmp ("present_or_copyout", p)
31665 || !strcmp ("pcopyout", p))
31666 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31667 else if (!strcmp ("present_or_create", p)
31668 || !strcmp ("pcreate", p))
31669 result = PRAGMA_OACC_CLAUSE_CREATE;
31670 else if (!strcmp ("priority", p))
31671 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31672 else if (!strcmp ("proc_bind", p))
31673 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31674 break;
31675 case 'r':
31676 if (!strcmp ("reduction", p))
31677 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31678 break;
31679 case 's':
31680 if (!strcmp ("safelen", p))
31681 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31682 else if (!strcmp ("schedule", p))
31683 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31684 else if (!strcmp ("sections", p))
31685 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31686 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
31687 result = PRAGMA_OACC_CLAUSE_HOST;
31688 else if (!strcmp ("seq", p))
31689 result = PRAGMA_OACC_CLAUSE_SEQ;
31690 else if (!strcmp ("shared", p))
31691 result = PRAGMA_OMP_CLAUSE_SHARED;
31692 else if (!strcmp ("simd", p))
31693 result = PRAGMA_OMP_CLAUSE_SIMD;
31694 else if (!strcmp ("simdlen", p))
31695 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31696 break;
31697 case 't':
31698 if (!strcmp ("task_reduction", p))
31699 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
31700 else if (!strcmp ("taskgroup", p))
31701 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31702 else if (!strcmp ("thread_limit", p))
31703 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31704 else if (!strcmp ("threads", p))
31705 result = PRAGMA_OMP_CLAUSE_THREADS;
31706 else if (!strcmp ("tile", p))
31707 result = PRAGMA_OACC_CLAUSE_TILE;
31708 else if (!strcmp ("to", p))
31709 result = PRAGMA_OMP_CLAUSE_TO;
31710 break;
31711 case 'u':
31712 if (!strcmp ("uniform", p))
31713 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31714 else if (!strcmp ("untied", p))
31715 result = PRAGMA_OMP_CLAUSE_UNTIED;
31716 else if (!strcmp ("use_device", p))
31717 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31718 else if (!strcmp ("use_device_ptr", p))
31719 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31720 break;
31721 case 'v':
31722 if (!strcmp ("vector", p))
31723 result = PRAGMA_OACC_CLAUSE_VECTOR;
31724 else if (!strcmp ("vector_length", p))
31725 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31726 break;
31727 case 'w':
31728 if (!strcmp ("wait", p))
31729 result = PRAGMA_OACC_CLAUSE_WAIT;
31730 else if (!strcmp ("worker", p))
31731 result = PRAGMA_OACC_CLAUSE_WORKER;
31732 break;
31736 if (result != PRAGMA_OMP_CLAUSE_NONE)
31737 cp_lexer_consume_token (parser->lexer);
31739 return result;
31742 /* Validate that a clause of the given type does not already exist. */
31744 static void
31745 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31746 const char *name, location_t location)
31748 tree c;
31750 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31751 if (OMP_CLAUSE_CODE (c) == code)
31753 error_at (location, "too many %qs clauses", name);
31754 break;
31758 /* OpenMP 2.5:
31759 variable-list:
31760 identifier
31761 variable-list , identifier
31763 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31764 colon). An opening parenthesis will have been consumed by the caller.
31766 If KIND is nonzero, create the appropriate node and install the decl
31767 in OMP_CLAUSE_DECL and add the node to the head of the list.
31769 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31770 return the list created.
31772 COLON can be NULL if only closing parenthesis should end the list,
31773 or pointer to bool which will receive false if the list is terminated
31774 by closing parenthesis or true if the list is terminated by colon. */
31776 static tree
31777 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31778 tree list, bool *colon)
31780 cp_token *token;
31781 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31782 if (colon)
31784 parser->colon_corrects_to_scope_p = false;
31785 *colon = false;
31787 while (1)
31789 tree name, decl;
31791 if (kind == OMP_CLAUSE_DEPEND)
31792 cp_parser_parse_tentatively (parser);
31793 token = cp_lexer_peek_token (parser->lexer);
31794 if (kind != 0
31795 && current_class_ptr
31796 && cp_parser_is_keyword (token, RID_THIS))
31798 decl = finish_this_expr ();
31799 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31800 || CONVERT_EXPR_P (decl))
31801 decl = TREE_OPERAND (decl, 0);
31802 cp_lexer_consume_token (parser->lexer);
31804 else
31806 name = cp_parser_id_expression (parser, /*template_p=*/false,
31807 /*check_dependency_p=*/true,
31808 /*template_p=*/NULL,
31809 /*declarator_p=*/false,
31810 /*optional_p=*/false);
31811 if (name == error_mark_node)
31813 if (kind == OMP_CLAUSE_DEPEND
31814 && cp_parser_simulate_error (parser))
31815 goto depend_lvalue;
31816 goto skip_comma;
31819 if (identifier_p (name))
31820 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31821 else
31822 decl = name;
31823 if (decl == error_mark_node)
31825 if (kind == OMP_CLAUSE_DEPEND
31826 && cp_parser_simulate_error (parser))
31827 goto depend_lvalue;
31828 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31829 token->location);
31832 if (decl == error_mark_node)
31834 else if (kind != 0)
31836 switch (kind)
31838 case OMP_CLAUSE__CACHE_:
31839 /* The OpenACC cache directive explicitly only allows "array
31840 elements or subarrays". */
31841 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31843 error_at (token->location, "expected %<[%>");
31844 decl = error_mark_node;
31845 break;
31847 /* FALLTHROUGH. */
31848 case OMP_CLAUSE_MAP:
31849 case OMP_CLAUSE_FROM:
31850 case OMP_CLAUSE_TO:
31851 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31853 location_t loc
31854 = cp_lexer_peek_token (parser->lexer)->location;
31855 cp_id_kind idk = CP_ID_KIND_NONE;
31856 cp_lexer_consume_token (parser->lexer);
31857 decl = convert_from_reference (decl);
31858 decl
31859 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31860 decl, false,
31861 &idk, loc);
31863 /* FALLTHROUGH. */
31864 case OMP_CLAUSE_DEPEND:
31865 case OMP_CLAUSE_REDUCTION:
31866 case OMP_CLAUSE_IN_REDUCTION:
31867 case OMP_CLAUSE_TASK_REDUCTION:
31868 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31870 tree low_bound = NULL_TREE, length = NULL_TREE;
31872 parser->colon_corrects_to_scope_p = false;
31873 cp_lexer_consume_token (parser->lexer);
31874 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31875 low_bound = cp_parser_expression (parser);
31876 if (!colon)
31877 parser->colon_corrects_to_scope_p
31878 = saved_colon_corrects_to_scope_p;
31879 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31880 length = integer_one_node;
31881 else
31883 /* Look for `:'. */
31884 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31886 if (kind == OMP_CLAUSE_DEPEND
31887 && cp_parser_simulate_error (parser))
31888 goto depend_lvalue;
31889 goto skip_comma;
31891 if (kind == OMP_CLAUSE_DEPEND)
31892 cp_parser_commit_to_tentative_parse (parser);
31893 if (!cp_lexer_next_token_is (parser->lexer,
31894 CPP_CLOSE_SQUARE))
31895 length = cp_parser_expression (parser);
31897 /* Look for the closing `]'. */
31898 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31899 RT_CLOSE_SQUARE))
31901 if (kind == OMP_CLAUSE_DEPEND
31902 && cp_parser_simulate_error (parser))
31903 goto depend_lvalue;
31904 goto skip_comma;
31907 decl = tree_cons (low_bound, length, decl);
31909 break;
31910 default:
31911 break;
31914 if (kind == OMP_CLAUSE_DEPEND)
31916 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
31917 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
31918 && cp_parser_simulate_error (parser))
31920 depend_lvalue:
31921 cp_parser_abort_tentative_parse (parser);
31922 decl = cp_parser_assignment_expression (parser, NULL,
31923 false, false);
31925 else
31926 cp_parser_parse_definitely (parser);
31929 tree u = build_omp_clause (token->location, kind);
31930 OMP_CLAUSE_DECL (u) = decl;
31931 OMP_CLAUSE_CHAIN (u) = list;
31932 list = u;
31934 else
31935 list = tree_cons (decl, NULL_TREE, list);
31937 get_comma:
31938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31939 break;
31940 cp_lexer_consume_token (parser->lexer);
31943 if (colon)
31944 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31946 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31948 *colon = true;
31949 cp_parser_require (parser, CPP_COLON, RT_COLON);
31950 return list;
31953 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31955 int ending;
31957 /* Try to resync to an unnested comma. Copied from
31958 cp_parser_parenthesized_expression_list. */
31959 skip_comma:
31960 if (colon)
31961 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31962 ending = cp_parser_skip_to_closing_parenthesis (parser,
31963 /*recovering=*/true,
31964 /*or_comma=*/true,
31965 /*consume_paren=*/true);
31966 if (ending < 0)
31967 goto get_comma;
31970 return list;
31973 /* Similarly, but expect leading and trailing parenthesis. This is a very
31974 common case for omp clauses. */
31976 static tree
31977 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31979 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31980 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31981 return list;
31984 /* OpenACC 2.0:
31985 copy ( variable-list )
31986 copyin ( variable-list )
31987 copyout ( variable-list )
31988 create ( variable-list )
31989 delete ( variable-list )
31990 present ( variable-list ) */
31992 static tree
31993 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31994 tree list)
31996 enum gomp_map_kind kind;
31997 switch (c_kind)
31999 case PRAGMA_OACC_CLAUSE_COPY:
32000 kind = GOMP_MAP_TOFROM;
32001 break;
32002 case PRAGMA_OACC_CLAUSE_COPYIN:
32003 kind = GOMP_MAP_TO;
32004 break;
32005 case PRAGMA_OACC_CLAUSE_COPYOUT:
32006 kind = GOMP_MAP_FROM;
32007 break;
32008 case PRAGMA_OACC_CLAUSE_CREATE:
32009 kind = GOMP_MAP_ALLOC;
32010 break;
32011 case PRAGMA_OACC_CLAUSE_DELETE:
32012 kind = GOMP_MAP_RELEASE;
32013 break;
32014 case PRAGMA_OACC_CLAUSE_DEVICE:
32015 kind = GOMP_MAP_FORCE_TO;
32016 break;
32017 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32018 kind = GOMP_MAP_DEVICE_RESIDENT;
32019 break;
32020 case PRAGMA_OACC_CLAUSE_HOST:
32021 kind = GOMP_MAP_FORCE_FROM;
32022 break;
32023 case PRAGMA_OACC_CLAUSE_LINK:
32024 kind = GOMP_MAP_LINK;
32025 break;
32026 case PRAGMA_OACC_CLAUSE_PRESENT:
32027 kind = GOMP_MAP_FORCE_PRESENT;
32028 break;
32029 default:
32030 gcc_unreachable ();
32032 tree nl, c;
32033 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32035 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32036 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32038 return nl;
32041 /* OpenACC 2.0:
32042 deviceptr ( variable-list ) */
32044 static tree
32045 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32047 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32048 tree vars, t;
32050 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32051 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32052 variable-list must only allow for pointer variables. */
32053 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32054 for (t = vars; t; t = TREE_CHAIN (t))
32056 tree v = TREE_PURPOSE (t);
32057 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32058 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32059 OMP_CLAUSE_DECL (u) = v;
32060 OMP_CLAUSE_CHAIN (u) = list;
32061 list = u;
32064 return list;
32067 /* OpenACC 2.5:
32068 auto
32069 finalize
32070 independent
32071 nohost
32072 seq */
32074 static tree
32075 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
32076 enum omp_clause_code code,
32077 tree list, location_t location)
32079 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32080 tree c = build_omp_clause (location, code);
32081 OMP_CLAUSE_CHAIN (c) = list;
32082 return c;
32085 /* OpenACC:
32086 num_gangs ( expression )
32087 num_workers ( expression )
32088 vector_length ( expression ) */
32090 static tree
32091 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32092 const char *str, tree list)
32094 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32096 matching_parens parens;
32097 if (!parens.require_open (parser))
32098 return list;
32100 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32102 if (t == error_mark_node
32103 || !parens.require_close (parser))
32105 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32106 /*or_comma=*/false,
32107 /*consume_paren=*/true);
32108 return list;
32111 check_no_duplicate_clause (list, code, str, loc);
32113 tree c = build_omp_clause (loc, code);
32114 OMP_CLAUSE_OPERAND (c, 0) = t;
32115 OMP_CLAUSE_CHAIN (c) = list;
32116 return c;
32119 /* OpenACC:
32121 gang [( gang-arg-list )]
32122 worker [( [num:] int-expr )]
32123 vector [( [length:] int-expr )]
32125 where gang-arg is one of:
32127 [num:] int-expr
32128 static: size-expr
32130 and size-expr may be:
32133 int-expr
32136 static tree
32137 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
32138 const char *str, tree list)
32140 const char *id = "num";
32141 cp_lexer *lexer = parser->lexer;
32142 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32143 location_t loc = cp_lexer_peek_token (lexer)->location;
32145 if (kind == OMP_CLAUSE_VECTOR)
32146 id = "length";
32148 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32150 matching_parens parens;
32151 parens.consume_open (parser);
32155 cp_token *next = cp_lexer_peek_token (lexer);
32156 int idx = 0;
32158 /* Gang static argument. */
32159 if (kind == OMP_CLAUSE_GANG
32160 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32162 cp_lexer_consume_token (lexer);
32164 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32165 goto cleanup_error;
32167 idx = 1;
32168 if (ops[idx] != NULL)
32170 cp_parser_error (parser, "too many %<static%> arguments");
32171 goto cleanup_error;
32174 /* Check for the '*' argument. */
32175 if (cp_lexer_next_token_is (lexer, CPP_MULT)
32176 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32177 || cp_lexer_nth_token_is (parser->lexer, 2,
32178 CPP_CLOSE_PAREN)))
32180 cp_lexer_consume_token (lexer);
32181 ops[idx] = integer_minus_one_node;
32183 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32185 cp_lexer_consume_token (lexer);
32186 continue;
32188 else break;
32191 /* Worker num: argument and vector length: arguments. */
32192 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32193 && id_equal (next->u.value, id)
32194 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32196 cp_lexer_consume_token (lexer); /* id */
32197 cp_lexer_consume_token (lexer); /* ':' */
32200 /* Now collect the actual argument. */
32201 if (ops[idx] != NULL_TREE)
32203 cp_parser_error (parser, "unexpected argument");
32204 goto cleanup_error;
32207 tree expr = cp_parser_assignment_expression (parser, NULL, false,
32208 false);
32209 if (expr == error_mark_node)
32210 goto cleanup_error;
32212 mark_exp_read (expr);
32213 ops[idx] = expr;
32215 if (kind == OMP_CLAUSE_GANG
32216 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32218 cp_lexer_consume_token (lexer);
32219 continue;
32221 break;
32223 while (1);
32225 if (!parens.require_close (parser))
32226 goto cleanup_error;
32229 check_no_duplicate_clause (list, kind, str, loc);
32231 c = build_omp_clause (loc, kind);
32233 if (ops[1])
32234 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32236 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32237 OMP_CLAUSE_CHAIN (c) = list;
32239 return c;
32241 cleanup_error:
32242 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32243 return list;
32246 /* OpenACC 2.0:
32247 tile ( size-expr-list ) */
32249 static tree
32250 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32252 tree c, expr = error_mark_node;
32253 tree tile = NULL_TREE;
32255 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32256 so, but the spec authors never considered such a case and have
32257 differing opinions on what it might mean, including 'not
32258 allowed'.) */
32259 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32260 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32261 clause_loc);
32263 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32264 return list;
32268 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32269 return list;
32271 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32272 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32273 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32275 cp_lexer_consume_token (parser->lexer);
32276 expr = integer_zero_node;
32278 else
32279 expr = cp_parser_constant_expression (parser);
32281 tile = tree_cons (NULL_TREE, expr, tile);
32283 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32285 /* Consume the trailing ')'. */
32286 cp_lexer_consume_token (parser->lexer);
32288 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32289 tile = nreverse (tile);
32290 OMP_CLAUSE_TILE_LIST (c) = tile;
32291 OMP_CLAUSE_CHAIN (c) = list;
32292 return c;
32295 /* OpenACC 2.0
32296 Parse wait clause or directive parameters. */
32298 static tree
32299 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32301 vec<tree, va_gc> *args;
32302 tree t, args_tree;
32304 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32305 /*cast_p=*/false,
32306 /*allow_expansion_p=*/true,
32307 /*non_constant_p=*/NULL);
32309 if (args == NULL || args->length () == 0)
32311 cp_parser_error (parser, "expected integer expression before ')'");
32312 if (args != NULL)
32313 release_tree_vector (args);
32314 return list;
32317 args_tree = build_tree_list_vec (args);
32319 release_tree_vector (args);
32321 for (t = args_tree; t; t = TREE_CHAIN (t))
32323 tree targ = TREE_VALUE (t);
32325 if (targ != error_mark_node)
32327 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32328 error ("%<wait%> expression must be integral");
32329 else
32331 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32333 targ = mark_rvalue_use (targ);
32334 OMP_CLAUSE_DECL (c) = targ;
32335 OMP_CLAUSE_CHAIN (c) = list;
32336 list = c;
32341 return list;
32344 /* OpenACC:
32345 wait ( int-expr-list ) */
32347 static tree
32348 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32350 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32352 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32353 return list;
32355 list = cp_parser_oacc_wait_list (parser, location, list);
32357 return list;
32360 /* OpenMP 3.0:
32361 collapse ( constant-expression ) */
32363 static tree
32364 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32366 tree c, num;
32367 location_t loc;
32368 HOST_WIDE_INT n;
32370 loc = cp_lexer_peek_token (parser->lexer)->location;
32371 matching_parens parens;
32372 if (!parens.require_open (parser))
32373 return list;
32375 num = cp_parser_constant_expression (parser);
32377 if (!parens.require_close (parser))
32378 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32379 /*or_comma=*/false,
32380 /*consume_paren=*/true);
32382 if (num == error_mark_node)
32383 return list;
32384 num = fold_non_dependent_expr (num);
32385 if (!tree_fits_shwi_p (num)
32386 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32387 || (n = tree_to_shwi (num)) <= 0
32388 || (int) n != n)
32390 error_at (loc, "collapse argument needs positive constant integer expression");
32391 return list;
32394 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32395 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32396 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32397 OMP_CLAUSE_CHAIN (c) = list;
32398 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32400 return c;
32403 /* OpenMP 2.5:
32404 default ( none | shared )
32406 OpenACC:
32407 default ( none | present ) */
32409 static tree
32410 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32411 location_t location, bool is_oacc)
32413 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32414 tree c;
32416 matching_parens parens;
32417 if (!parens.require_open (parser))
32418 return list;
32419 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32421 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32422 const char *p = IDENTIFIER_POINTER (id);
32424 switch (p[0])
32426 case 'n':
32427 if (strcmp ("none", p) != 0)
32428 goto invalid_kind;
32429 kind = OMP_CLAUSE_DEFAULT_NONE;
32430 break;
32432 case 'p':
32433 if (strcmp ("present", p) != 0 || !is_oacc)
32434 goto invalid_kind;
32435 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32436 break;
32438 case 's':
32439 if (strcmp ("shared", p) != 0 || is_oacc)
32440 goto invalid_kind;
32441 kind = OMP_CLAUSE_DEFAULT_SHARED;
32442 break;
32444 default:
32445 goto invalid_kind;
32448 cp_lexer_consume_token (parser->lexer);
32450 else
32452 invalid_kind:
32453 if (is_oacc)
32454 cp_parser_error (parser, "expected %<none%> or %<present%>");
32455 else
32456 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32459 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32460 || !parens.require_close (parser))
32461 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32462 /*or_comma=*/false,
32463 /*consume_paren=*/true);
32465 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32466 return list;
32468 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32469 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32470 OMP_CLAUSE_CHAIN (c) = list;
32471 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32473 return c;
32476 /* OpenMP 3.1:
32477 final ( expression ) */
32479 static tree
32480 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32482 tree t, c;
32484 matching_parens parens;
32485 if (!parens.require_open (parser))
32486 return list;
32488 t = cp_parser_assignment_expression (parser);
32490 if (t == error_mark_node
32491 || !parens.require_close (parser))
32492 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32493 /*or_comma=*/false,
32494 /*consume_paren=*/true);
32496 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32498 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32499 OMP_CLAUSE_FINAL_EXPR (c) = t;
32500 OMP_CLAUSE_CHAIN (c) = list;
32502 return c;
32505 /* OpenMP 2.5:
32506 if ( expression )
32508 OpenMP 4.5:
32509 if ( directive-name-modifier : expression )
32511 directive-name-modifier:
32512 parallel | task | taskloop | target data | target | target update
32513 | target enter data | target exit data
32515 OpenMP 5.0:
32516 directive-name-modifier:
32517 ... | simd | cancel */
32519 static tree
32520 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32521 bool is_omp)
32523 tree t, c;
32524 enum tree_code if_modifier = ERROR_MARK;
32526 matching_parens parens;
32527 if (!parens.require_open (parser))
32528 return list;
32530 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32532 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32533 const char *p = IDENTIFIER_POINTER (id);
32534 int n = 2;
32536 if (strcmp ("cancel", p) == 0)
32537 if_modifier = VOID_CST;
32538 else if (strcmp ("parallel", p) == 0)
32539 if_modifier = OMP_PARALLEL;
32540 else if (strcmp ("simd", p) == 0)
32541 if_modifier = OMP_SIMD;
32542 else if (strcmp ("task", p) == 0)
32543 if_modifier = OMP_TASK;
32544 else if (strcmp ("taskloop", p) == 0)
32545 if_modifier = OMP_TASKLOOP;
32546 else if (strcmp ("target", p) == 0)
32548 if_modifier = OMP_TARGET;
32549 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32551 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32552 p = IDENTIFIER_POINTER (id);
32553 if (strcmp ("data", p) == 0)
32554 if_modifier = OMP_TARGET_DATA;
32555 else if (strcmp ("update", p) == 0)
32556 if_modifier = OMP_TARGET_UPDATE;
32557 else if (strcmp ("enter", p) == 0)
32558 if_modifier = OMP_TARGET_ENTER_DATA;
32559 else if (strcmp ("exit", p) == 0)
32560 if_modifier = OMP_TARGET_EXIT_DATA;
32561 if (if_modifier != OMP_TARGET)
32562 n = 3;
32563 else
32565 location_t loc
32566 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32567 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32568 "or %<exit%>");
32569 if_modifier = ERROR_MARK;
32571 if (if_modifier == OMP_TARGET_ENTER_DATA
32572 || if_modifier == OMP_TARGET_EXIT_DATA)
32574 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32576 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32577 p = IDENTIFIER_POINTER (id);
32578 if (strcmp ("data", p) == 0)
32579 n = 4;
32581 if (n != 4)
32583 location_t loc
32584 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32585 error_at (loc, "expected %<data%>");
32586 if_modifier = ERROR_MARK;
32591 if (if_modifier != ERROR_MARK)
32593 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32595 while (n-- > 0)
32596 cp_lexer_consume_token (parser->lexer);
32598 else
32600 if (n > 2)
32602 location_t loc
32603 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32604 error_at (loc, "expected %<:%>");
32606 if_modifier = ERROR_MARK;
32611 t = cp_parser_assignment_expression (parser);
32613 if (t == error_mark_node
32614 || !parens.require_close (parser))
32615 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32616 /*or_comma=*/false,
32617 /*consume_paren=*/true);
32619 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32620 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32622 if (if_modifier != ERROR_MARK
32623 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32625 const char *p = NULL;
32626 switch (if_modifier)
32628 case VOID_CST: p = "cancel"; break;
32629 case OMP_PARALLEL: p = "parallel"; break;
32630 case OMP_SIMD: p = "simd"; break;
32631 case OMP_TASK: p = "task"; break;
32632 case OMP_TASKLOOP: p = "taskloop"; break;
32633 case OMP_TARGET_DATA: p = "target data"; break;
32634 case OMP_TARGET: p = "target"; break;
32635 case OMP_TARGET_UPDATE: p = "target update"; break;
32636 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32637 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32638 default: gcc_unreachable ();
32640 error_at (location, "too many %<if%> clauses with %qs modifier",
32642 return list;
32644 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32646 if (!is_omp)
32647 error_at (location, "too many %<if%> clauses");
32648 else
32649 error_at (location, "too many %<if%> clauses without modifier");
32650 return list;
32652 else if (if_modifier == ERROR_MARK
32653 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32655 error_at (location, "if any %<if%> clause has modifier, then all "
32656 "%<if%> clauses have to use modifier");
32657 return list;
32661 c = build_omp_clause (location, OMP_CLAUSE_IF);
32662 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32663 OMP_CLAUSE_IF_EXPR (c) = t;
32664 OMP_CLAUSE_CHAIN (c) = list;
32666 return c;
32669 /* OpenMP 3.1:
32670 mergeable */
32672 static tree
32673 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32674 tree list, location_t location)
32676 tree c;
32678 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32679 location);
32681 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32682 OMP_CLAUSE_CHAIN (c) = list;
32683 return c;
32686 /* OpenMP 2.5:
32687 nowait */
32689 static tree
32690 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32691 tree list, location_t location)
32693 tree c;
32695 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32697 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32698 OMP_CLAUSE_CHAIN (c) = list;
32699 return c;
32702 /* OpenMP 2.5:
32703 num_threads ( expression ) */
32705 static tree
32706 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32707 location_t location)
32709 tree t, c;
32711 matching_parens parens;
32712 if (!parens.require_open (parser))
32713 return list;
32715 t = cp_parser_assignment_expression (parser);
32717 if (t == error_mark_node
32718 || !parens.require_close (parser))
32719 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32720 /*or_comma=*/false,
32721 /*consume_paren=*/true);
32723 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32724 "num_threads", location);
32726 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32727 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32728 OMP_CLAUSE_CHAIN (c) = list;
32730 return c;
32733 /* OpenMP 4.5:
32734 num_tasks ( expression ) */
32736 static tree
32737 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32738 location_t location)
32740 tree t, c;
32742 matching_parens parens;
32743 if (!parens.require_open (parser))
32744 return list;
32746 t = cp_parser_assignment_expression (parser);
32748 if (t == error_mark_node
32749 || !parens.require_close (parser))
32750 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32751 /*or_comma=*/false,
32752 /*consume_paren=*/true);
32754 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32755 "num_tasks", location);
32757 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32758 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32759 OMP_CLAUSE_CHAIN (c) = list;
32761 return c;
32764 /* OpenMP 4.5:
32765 grainsize ( expression ) */
32767 static tree
32768 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32769 location_t location)
32771 tree t, c;
32773 matching_parens parens;
32774 if (!parens.require_open (parser))
32775 return list;
32777 t = cp_parser_assignment_expression (parser);
32779 if (t == error_mark_node
32780 || !parens.require_close (parser))
32781 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32782 /*or_comma=*/false,
32783 /*consume_paren=*/true);
32785 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32786 "grainsize", location);
32788 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32789 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32790 OMP_CLAUSE_CHAIN (c) = list;
32792 return c;
32795 /* OpenMP 4.5:
32796 priority ( expression ) */
32798 static tree
32799 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32800 location_t location)
32802 tree t, c;
32804 matching_parens parens;
32805 if (!parens.require_open (parser))
32806 return list;
32808 t = cp_parser_assignment_expression (parser);
32810 if (t == error_mark_node
32811 || !parens.require_close (parser))
32812 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32813 /*or_comma=*/false,
32814 /*consume_paren=*/true);
32816 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32817 "priority", location);
32819 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32820 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32821 OMP_CLAUSE_CHAIN (c) = list;
32823 return c;
32826 /* OpenMP 4.5:
32827 hint ( expression ) */
32829 static tree
32830 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
32832 tree t, c;
32834 matching_parens parens;
32835 if (!parens.require_open (parser))
32836 return list;
32838 t = cp_parser_assignment_expression (parser);
32840 if (t == error_mark_node
32841 || !parens.require_close (parser))
32842 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32843 /*or_comma=*/false,
32844 /*consume_paren=*/true);
32846 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32848 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32849 OMP_CLAUSE_HINT_EXPR (c) = t;
32850 OMP_CLAUSE_CHAIN (c) = list;
32852 return c;
32855 /* OpenMP 4.5:
32856 defaultmap ( tofrom : scalar )
32858 OpenMP 5.0:
32859 defaultmap ( implicit-behavior [ : variable-category ] ) */
32861 static tree
32862 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32863 location_t location)
32865 tree c, id;
32866 const char *p;
32867 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
32868 enum omp_clause_defaultmap_kind category
32869 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
32871 matching_parens parens;
32872 if (!parens.require_open (parser))
32873 return list;
32875 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
32876 p = "default";
32877 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32879 invalid_behavior:
32880 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
32881 "%<tofrom%>, %<firstprivate%>, %<none%> "
32882 "or %<default%>");
32883 goto out_err;
32885 else
32887 id = cp_lexer_peek_token (parser->lexer)->u.value;
32888 p = IDENTIFIER_POINTER (id);
32891 switch (p[0])
32893 case 'a':
32894 if (strcmp ("alloc", p) == 0)
32895 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
32896 else
32897 goto invalid_behavior;
32898 break;
32900 case 'd':
32901 if (strcmp ("default", p) == 0)
32902 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
32903 else
32904 goto invalid_behavior;
32905 break;
32907 case 'f':
32908 if (strcmp ("firstprivate", p) == 0)
32909 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
32910 else if (strcmp ("from", p) == 0)
32911 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
32912 else
32913 goto invalid_behavior;
32914 break;
32916 case 'n':
32917 if (strcmp ("none", p) == 0)
32918 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
32919 else
32920 goto invalid_behavior;
32921 break;
32923 case 't':
32924 if (strcmp ("tofrom", p) == 0)
32925 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
32926 else if (strcmp ("to", p) == 0)
32927 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
32928 else
32929 goto invalid_behavior;
32930 break;
32932 default:
32933 goto invalid_behavior;
32935 cp_lexer_consume_token (parser->lexer);
32937 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32939 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32940 goto out_err;
32942 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32944 invalid_category:
32945 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
32946 "%<pointer%>");
32947 goto out_err;
32949 id = cp_lexer_peek_token (parser->lexer)->u.value;
32950 p = IDENTIFIER_POINTER (id);
32952 switch (p[0])
32954 case 'a':
32955 if (strcmp ("aggregate", p) == 0)
32956 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
32957 else
32958 goto invalid_category;
32959 break;
32961 case 'p':
32962 if (strcmp ("pointer", p) == 0)
32963 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
32964 else
32965 goto invalid_category;
32966 break;
32968 case 's':
32969 if (strcmp ("scalar", p) == 0)
32970 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
32971 else
32972 goto invalid_category;
32973 break;
32975 default:
32976 goto invalid_category;
32979 cp_lexer_consume_token (parser->lexer);
32981 if (!parens.require_close (parser))
32982 goto out_err;
32984 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32985 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
32986 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
32987 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
32988 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
32989 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
32991 enum omp_clause_defaultmap_kind cat = category;
32992 location_t loc = OMP_CLAUSE_LOCATION (c);
32993 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
32994 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
32995 p = NULL;
32996 switch (cat)
32998 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
32999 p = NULL;
33000 break;
33001 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
33002 p = "aggregate";
33003 break;
33004 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
33005 p = "pointer";
33006 break;
33007 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
33008 p = "scalar";
33009 break;
33010 default:
33011 gcc_unreachable ();
33013 if (p)
33014 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33016 else
33017 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33018 "category");
33019 break;
33022 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33023 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33024 OMP_CLAUSE_CHAIN (c) = list;
33025 return c;
33027 out_err:
33028 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33029 /*or_comma=*/false,
33030 /*consume_paren=*/true);
33031 return list;
33034 /* OpenMP 2.5:
33035 ordered
33037 OpenMP 4.5:
33038 ordered ( constant-expression ) */
33040 static tree
33041 cp_parser_omp_clause_ordered (cp_parser *parser,
33042 tree list, location_t location)
33044 tree c, num = NULL_TREE;
33045 HOST_WIDE_INT n;
33047 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33048 "ordered", location);
33050 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33052 matching_parens parens;
33053 parens.consume_open (parser);
33055 num = cp_parser_constant_expression (parser);
33057 if (!parens.require_close (parser))
33058 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33059 /*or_comma=*/false,
33060 /*consume_paren=*/true);
33062 if (num == error_mark_node)
33063 return list;
33064 num = fold_non_dependent_expr (num);
33065 if (!tree_fits_shwi_p (num)
33066 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33067 || (n = tree_to_shwi (num)) <= 0
33068 || (int) n != n)
33070 error_at (location,
33071 "ordered argument needs positive constant integer "
33072 "expression");
33073 return list;
33077 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
33078 OMP_CLAUSE_ORDERED_EXPR (c) = num;
33079 OMP_CLAUSE_CHAIN (c) = list;
33080 return c;
33083 /* OpenMP 2.5:
33084 reduction ( reduction-operator : variable-list )
33086 reduction-operator:
33087 One of: + * - & ^ | && ||
33089 OpenMP 3.1:
33091 reduction-operator:
33092 One of: + * - & ^ | && || min max
33094 OpenMP 4.0:
33096 reduction-operator:
33097 One of: + * - & ^ | && ||
33098 id-expression
33100 OpenMP 5.0:
33101 reduction ( reduction-modifier, reduction-operator : variable-list )
33102 in_reduction ( reduction-operator : variable-list )
33103 task_reduction ( reduction-operator : variable-list ) */
33105 static tree
33106 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
33107 bool is_omp, tree list)
33109 enum tree_code code = ERROR_MARK;
33110 tree nlist, c, id = NULL_TREE;
33111 bool task = false;
33112 bool inscan = false;
33114 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33115 return list;
33117 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
33119 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
33120 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33122 cp_lexer_consume_token (parser->lexer);
33123 cp_lexer_consume_token (parser->lexer);
33125 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33126 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33128 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33129 const char *p = IDENTIFIER_POINTER (id);
33130 if (strcmp (p, "task") == 0)
33131 task = true;
33132 else if (strcmp (p, "inscan") == 0)
33134 inscan = true;
33135 sorry ("%<inscan%> modifier on %<reduction%> clause "
33136 "not supported yet");
33138 if (task || inscan)
33140 cp_lexer_consume_token (parser->lexer);
33141 cp_lexer_consume_token (parser->lexer);
33146 switch (cp_lexer_peek_token (parser->lexer)->type)
33148 case CPP_PLUS: code = PLUS_EXPR; break;
33149 case CPP_MULT: code = MULT_EXPR; break;
33150 case CPP_MINUS: code = MINUS_EXPR; break;
33151 case CPP_AND: code = BIT_AND_EXPR; break;
33152 case CPP_XOR: code = BIT_XOR_EXPR; break;
33153 case CPP_OR: code = BIT_IOR_EXPR; break;
33154 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
33155 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
33156 default: break;
33159 if (code != ERROR_MARK)
33160 cp_lexer_consume_token (parser->lexer);
33161 else
33163 bool saved_colon_corrects_to_scope_p;
33164 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33165 parser->colon_corrects_to_scope_p = false;
33166 id = cp_parser_id_expression (parser, /*template_p=*/false,
33167 /*check_dependency_p=*/true,
33168 /*template_p=*/NULL,
33169 /*declarator_p=*/false,
33170 /*optional_p=*/false);
33171 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33172 if (identifier_p (id))
33174 const char *p = IDENTIFIER_POINTER (id);
33176 if (strcmp (p, "min") == 0)
33177 code = MIN_EXPR;
33178 else if (strcmp (p, "max") == 0)
33179 code = MAX_EXPR;
33180 else if (id == ovl_op_identifier (false, PLUS_EXPR))
33181 code = PLUS_EXPR;
33182 else if (id == ovl_op_identifier (false, MULT_EXPR))
33183 code = MULT_EXPR;
33184 else if (id == ovl_op_identifier (false, MINUS_EXPR))
33185 code = MINUS_EXPR;
33186 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
33187 code = BIT_AND_EXPR;
33188 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
33189 code = BIT_IOR_EXPR;
33190 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
33191 code = BIT_XOR_EXPR;
33192 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
33193 code = TRUTH_ANDIF_EXPR;
33194 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
33195 code = TRUTH_ORIF_EXPR;
33196 id = omp_reduction_id (code, id, NULL_TREE);
33197 tree scope = parser->scope;
33198 if (scope)
33199 id = build_qualified_name (NULL_TREE, scope, id, false);
33200 parser->scope = NULL_TREE;
33201 parser->qualifying_scope = NULL_TREE;
33202 parser->object_scope = NULL_TREE;
33204 else
33206 error ("invalid reduction-identifier");
33207 resync_fail:
33208 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33209 /*or_comma=*/false,
33210 /*consume_paren=*/true);
33211 return list;
33215 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33216 goto resync_fail;
33218 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
33219 NULL);
33220 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33222 OMP_CLAUSE_REDUCTION_CODE (c) = code;
33223 if (task)
33224 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
33225 else if (inscan)
33226 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
33227 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
33230 return nlist;
33233 /* OpenMP 2.5:
33234 schedule ( schedule-kind )
33235 schedule ( schedule-kind , expression )
33237 schedule-kind:
33238 static | dynamic | guided | runtime | auto
33240 OpenMP 4.5:
33241 schedule ( schedule-modifier : schedule-kind )
33242 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33244 schedule-modifier:
33245 simd
33246 monotonic
33247 nonmonotonic */
33249 static tree
33250 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
33252 tree c, t;
33253 int modifiers = 0, nmodifiers = 0;
33255 matching_parens parens;
33256 if (!parens.require_open (parser))
33257 return list;
33259 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
33261 while (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);
33265 if (strcmp ("simd", p) == 0)
33266 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
33267 else if (strcmp ("monotonic", p) == 0)
33268 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
33269 else if (strcmp ("nonmonotonic", p) == 0)
33270 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
33271 else
33272 break;
33273 cp_lexer_consume_token (parser->lexer);
33274 if (nmodifiers++ == 0
33275 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33276 cp_lexer_consume_token (parser->lexer);
33277 else
33279 cp_parser_require (parser, CPP_COLON, RT_COLON);
33280 break;
33284 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33286 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33287 const char *p = IDENTIFIER_POINTER (id);
33289 switch (p[0])
33291 case 'd':
33292 if (strcmp ("dynamic", p) != 0)
33293 goto invalid_kind;
33294 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
33295 break;
33297 case 'g':
33298 if (strcmp ("guided", p) != 0)
33299 goto invalid_kind;
33300 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
33301 break;
33303 case 'r':
33304 if (strcmp ("runtime", p) != 0)
33305 goto invalid_kind;
33306 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
33307 break;
33309 default:
33310 goto invalid_kind;
33313 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33314 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33315 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33316 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33317 else
33318 goto invalid_kind;
33319 cp_lexer_consume_token (parser->lexer);
33321 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33322 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33323 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33324 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33326 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33327 "specified");
33328 modifiers = 0;
33331 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33333 cp_token *token;
33334 cp_lexer_consume_token (parser->lexer);
33336 token = cp_lexer_peek_token (parser->lexer);
33337 t = cp_parser_assignment_expression (parser);
33339 if (t == error_mark_node)
33340 goto resync_fail;
33341 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
33342 error_at (token->location, "schedule %<runtime%> does not take "
33343 "a %<chunk_size%> parameter");
33344 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
33345 error_at (token->location, "schedule %<auto%> does not take "
33346 "a %<chunk_size%> parameter");
33347 else
33348 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
33350 if (!parens.require_close (parser))
33351 goto resync_fail;
33353 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33354 goto resync_fail;
33356 OMP_CLAUSE_SCHEDULE_KIND (c)
33357 = (enum omp_clause_schedule_kind)
33358 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
33360 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
33361 OMP_CLAUSE_CHAIN (c) = list;
33362 return c;
33364 invalid_kind:
33365 cp_parser_error (parser, "invalid schedule kind");
33366 resync_fail:
33367 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33368 /*or_comma=*/false,
33369 /*consume_paren=*/true);
33370 return list;
33373 /* OpenMP 3.0:
33374 untied */
33376 static tree
33377 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
33378 tree list, location_t location)
33380 tree c;
33382 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
33384 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
33385 OMP_CLAUSE_CHAIN (c) = list;
33386 return c;
33389 /* OpenMP 4.0:
33390 inbranch
33391 notinbranch */
33393 static tree
33394 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33395 tree list, location_t location)
33397 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33398 tree c = build_omp_clause (location, code);
33399 OMP_CLAUSE_CHAIN (c) = list;
33400 return c;
33403 /* OpenMP 4.0:
33404 parallel
33406 sections
33407 taskgroup */
33409 static tree
33410 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33411 enum omp_clause_code code,
33412 tree list, location_t location)
33414 tree c = build_omp_clause (location, code);
33415 OMP_CLAUSE_CHAIN (c) = list;
33416 return c;
33419 /* OpenMP 4.5:
33420 nogroup */
33422 static tree
33423 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33424 tree list, location_t location)
33426 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33427 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33428 OMP_CLAUSE_CHAIN (c) = list;
33429 return c;
33432 /* OpenMP 4.5:
33433 simd
33434 threads */
33436 static tree
33437 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33438 enum omp_clause_code code,
33439 tree list, location_t location)
33441 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33442 tree c = build_omp_clause (location, code);
33443 OMP_CLAUSE_CHAIN (c) = list;
33444 return c;
33447 /* OpenMP 4.0:
33448 num_teams ( expression ) */
33450 static tree
33451 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33452 location_t location)
33454 tree t, c;
33456 matching_parens parens;
33457 if (!parens.require_open (parser))
33458 return list;
33460 t = cp_parser_assignment_expression (parser);
33462 if (t == error_mark_node
33463 || !parens.require_close (parser))
33464 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33465 /*or_comma=*/false,
33466 /*consume_paren=*/true);
33468 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33469 "num_teams", location);
33471 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33472 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33473 OMP_CLAUSE_CHAIN (c) = list;
33475 return c;
33478 /* OpenMP 4.0:
33479 thread_limit ( expression ) */
33481 static tree
33482 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33483 location_t location)
33485 tree t, c;
33487 matching_parens parens;
33488 if (!parens.require_open (parser))
33489 return list;
33491 t = cp_parser_assignment_expression (parser);
33493 if (t == error_mark_node
33494 || !parens.require_close (parser))
33495 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33496 /*or_comma=*/false,
33497 /*consume_paren=*/true);
33499 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33500 "thread_limit", location);
33502 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33503 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33504 OMP_CLAUSE_CHAIN (c) = list;
33506 return c;
33509 /* OpenMP 4.0:
33510 aligned ( variable-list )
33511 aligned ( variable-list : constant-expression ) */
33513 static tree
33514 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33516 tree nlist, c, alignment = NULL_TREE;
33517 bool colon;
33519 matching_parens parens;
33520 if (!parens.require_open (parser))
33521 return list;
33523 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33524 &colon);
33526 if (colon)
33528 alignment = cp_parser_constant_expression (parser);
33530 if (!parens.require_close (parser))
33531 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33532 /*or_comma=*/false,
33533 /*consume_paren=*/true);
33535 if (alignment == error_mark_node)
33536 alignment = NULL_TREE;
33539 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33540 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33542 return nlist;
33545 /* OpenMP 2.5:
33546 lastprivate ( variable-list )
33548 OpenMP 5.0:
33549 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
33551 static tree
33552 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
33554 bool conditional = false;
33556 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33557 return list;
33559 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33560 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
33562 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33563 const char *p = IDENTIFIER_POINTER (id);
33565 if (strcmp ("conditional", p) == 0)
33567 conditional = true;
33568 cp_lexer_consume_token (parser->lexer);
33569 cp_lexer_consume_token (parser->lexer);
33573 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
33574 list, NULL);
33576 if (conditional)
33577 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33578 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
33579 return nlist;
33582 /* OpenMP 4.0:
33583 linear ( variable-list )
33584 linear ( variable-list : expression )
33586 OpenMP 4.5:
33587 linear ( modifier ( variable-list ) )
33588 linear ( modifier ( variable-list ) : expression ) */
33590 static tree
33591 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33592 bool declare_simd)
33594 tree nlist, c, step = integer_one_node;
33595 bool colon;
33596 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33598 matching_parens parens;
33599 if (!parens.require_open (parser))
33600 return list;
33602 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33604 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33605 const char *p = IDENTIFIER_POINTER (id);
33607 if (strcmp ("ref", p) == 0)
33608 kind = OMP_CLAUSE_LINEAR_REF;
33609 else if (strcmp ("val", p) == 0)
33610 kind = OMP_CLAUSE_LINEAR_VAL;
33611 else if (strcmp ("uval", p) == 0)
33612 kind = OMP_CLAUSE_LINEAR_UVAL;
33613 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33614 cp_lexer_consume_token (parser->lexer);
33615 else
33616 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33619 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33620 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33621 &colon);
33622 else
33624 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33625 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33626 if (colon)
33627 cp_parser_require (parser, CPP_COLON, RT_COLON);
33628 else if (!parens.require_close (parser))
33629 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33630 /*or_comma=*/false,
33631 /*consume_paren=*/true);
33634 if (colon)
33636 step = NULL_TREE;
33637 if (declare_simd
33638 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33639 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33641 cp_token *token = cp_lexer_peek_token (parser->lexer);
33642 cp_parser_parse_tentatively (parser);
33643 step = cp_parser_id_expression (parser, /*template_p=*/false,
33644 /*check_dependency_p=*/true,
33645 /*template_p=*/NULL,
33646 /*declarator_p=*/false,
33647 /*optional_p=*/false);
33648 if (step != error_mark_node)
33649 step = cp_parser_lookup_name_simple (parser, step, token->location);
33650 if (step == error_mark_node)
33652 step = NULL_TREE;
33653 cp_parser_abort_tentative_parse (parser);
33655 else if (!cp_parser_parse_definitely (parser))
33656 step = NULL_TREE;
33658 if (!step)
33659 step = cp_parser_assignment_expression (parser);
33661 if (!parens.require_close (parser))
33662 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33663 /*or_comma=*/false,
33664 /*consume_paren=*/true);
33666 if (step == error_mark_node)
33667 return list;
33670 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33672 OMP_CLAUSE_LINEAR_STEP (c) = step;
33673 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33676 return nlist;
33679 /* OpenMP 4.0:
33680 safelen ( constant-expression ) */
33682 static tree
33683 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33684 location_t location)
33686 tree t, c;
33688 matching_parens parens;
33689 if (!parens.require_open (parser))
33690 return list;
33692 t = cp_parser_constant_expression (parser);
33694 if (t == error_mark_node
33695 || !parens.require_close (parser))
33696 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33697 /*or_comma=*/false,
33698 /*consume_paren=*/true);
33700 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33702 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33703 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33704 OMP_CLAUSE_CHAIN (c) = list;
33706 return c;
33709 /* OpenMP 4.0:
33710 simdlen ( constant-expression ) */
33712 static tree
33713 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33714 location_t location)
33716 tree t, c;
33718 matching_parens parens;
33719 if (!parens.require_open (parser))
33720 return list;
33722 t = cp_parser_constant_expression (parser);
33724 if (t == error_mark_node
33725 || !parens.require_close (parser))
33726 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33727 /*or_comma=*/false,
33728 /*consume_paren=*/true);
33730 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33732 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33733 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33734 OMP_CLAUSE_CHAIN (c) = list;
33736 return c;
33739 /* OpenMP 4.5:
33740 vec:
33741 identifier [+/- integer]
33742 vec , identifier [+/- integer]
33745 static tree
33746 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33747 tree list)
33749 tree vec = NULL;
33751 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33753 cp_parser_error (parser, "expected identifier");
33754 return list;
33757 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33759 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33760 tree t, identifier = cp_parser_identifier (parser);
33761 tree addend = NULL;
33763 if (identifier == error_mark_node)
33764 t = error_mark_node;
33765 else
33767 t = cp_parser_lookup_name_simple
33768 (parser, identifier,
33769 cp_lexer_peek_token (parser->lexer)->location);
33770 if (t == error_mark_node)
33771 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33772 id_loc);
33775 bool neg = false;
33776 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33777 neg = true;
33778 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33780 addend = integer_zero_node;
33781 goto add_to_vector;
33783 cp_lexer_consume_token (parser->lexer);
33785 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33787 cp_parser_error (parser, "expected integer");
33788 return list;
33791 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33792 if (TREE_CODE (addend) != INTEGER_CST)
33794 cp_parser_error (parser, "expected integer");
33795 return list;
33797 cp_lexer_consume_token (parser->lexer);
33799 add_to_vector:
33800 if (t != error_mark_node)
33802 vec = tree_cons (addend, t, vec);
33803 if (neg)
33804 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33807 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33808 break;
33810 cp_lexer_consume_token (parser->lexer);
33813 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33815 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33816 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33817 OMP_CLAUSE_DECL (u) = nreverse (vec);
33818 OMP_CLAUSE_CHAIN (u) = list;
33819 return u;
33821 return list;
33824 /* OpenMP 5.0:
33825 iterators ( iterators-definition )
33827 iterators-definition:
33828 iterator-specifier
33829 iterator-specifier , iterators-definition
33831 iterator-specifier:
33832 identifier = range-specification
33833 iterator-type identifier = range-specification
33835 range-specification:
33836 begin : end
33837 begin : end : step */
33839 static tree
33840 cp_parser_omp_iterators (cp_parser *parser)
33842 tree ret = NULL_TREE, *last = &ret;
33843 cp_lexer_consume_token (parser->lexer);
33845 matching_parens parens;
33846 if (!parens.require_open (parser))
33847 return error_mark_node;
33849 bool saved_colon_corrects_to_scope_p
33850 = parser->colon_corrects_to_scope_p;
33851 bool saved_colon_doesnt_start_class_def_p
33852 = parser->colon_doesnt_start_class_def_p;
33856 tree iter_type;
33857 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33858 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
33859 iter_type = integer_type_node;
33860 else
33862 const char *saved_message
33863 = parser->type_definition_forbidden_message;
33864 parser->type_definition_forbidden_message
33865 = G_("types may not be defined in iterator type");
33867 iter_type = cp_parser_type_id (parser);
33869 parser->type_definition_forbidden_message = saved_message;
33872 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33873 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33875 cp_parser_error (parser, "expected identifier");
33876 break;
33879 tree id = cp_parser_identifier (parser);
33880 if (id == error_mark_node)
33881 break;
33883 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33884 break;
33886 parser->colon_corrects_to_scope_p = false;
33887 parser->colon_doesnt_start_class_def_p = true;
33888 tree begin = cp_parser_assignment_expression (parser);
33890 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33891 break;
33893 tree end = cp_parser_assignment_expression (parser);
33895 tree step = integer_one_node;
33896 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
33898 cp_lexer_consume_token (parser->lexer);
33899 step = cp_parser_assignment_expression (parser);
33902 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
33903 DECL_ARTIFICIAL (iter_var) = 1;
33904 DECL_CONTEXT (iter_var) = current_function_decl;
33905 pushdecl (iter_var);
33907 *last = make_tree_vec (6);
33908 TREE_VEC_ELT (*last, 0) = iter_var;
33909 TREE_VEC_ELT (*last, 1) = begin;
33910 TREE_VEC_ELT (*last, 2) = end;
33911 TREE_VEC_ELT (*last, 3) = step;
33912 last = &TREE_CHAIN (*last);
33914 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33916 cp_lexer_consume_token (parser->lexer);
33917 continue;
33919 break;
33921 while (1);
33923 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33924 parser->colon_doesnt_start_class_def_p
33925 = saved_colon_doesnt_start_class_def_p;
33927 if (!parens.require_close (parser))
33928 cp_parser_skip_to_closing_parenthesis (parser,
33929 /*recovering=*/true,
33930 /*or_comma=*/false,
33931 /*consume_paren=*/true);
33933 return ret ? ret : error_mark_node;
33936 /* OpenMP 4.0:
33937 depend ( depend-kind : variable-list )
33939 depend-kind:
33940 in | out | inout
33942 OpenMP 4.5:
33943 depend ( source )
33945 depend ( sink : vec )
33947 OpenMP 5.0:
33948 depend ( depend-modifier , depend-kind: variable-list )
33950 depend-kind:
33951 in | out | inout | mutexinoutset | depobj
33953 depend-modifier:
33954 iterator ( iterators-definition ) */
33956 static tree
33957 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33959 tree nlist, c, iterators = NULL_TREE;
33960 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
33962 matching_parens parens;
33963 if (!parens.require_open (parser))
33964 return list;
33968 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33969 goto invalid_kind;
33971 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33972 const char *p = IDENTIFIER_POINTER (id);
33974 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
33976 begin_scope (sk_omp, NULL);
33977 iterators = cp_parser_omp_iterators (parser);
33978 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
33979 continue;
33981 if (strcmp ("in", p) == 0)
33982 kind = OMP_CLAUSE_DEPEND_IN;
33983 else if (strcmp ("inout", p) == 0)
33984 kind = OMP_CLAUSE_DEPEND_INOUT;
33985 else if (strcmp ("mutexinoutset", p) == 0)
33986 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
33987 else if (strcmp ("out", p) == 0)
33988 kind = OMP_CLAUSE_DEPEND_OUT;
33989 else if (strcmp ("depobj", p) == 0)
33990 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
33991 else if (strcmp ("sink", p) == 0)
33992 kind = OMP_CLAUSE_DEPEND_SINK;
33993 else if (strcmp ("source", p) == 0)
33994 kind = OMP_CLAUSE_DEPEND_SOURCE;
33995 else
33996 goto invalid_kind;
33997 break;
33999 while (1);
34001 cp_lexer_consume_token (parser->lexer);
34003 if (iterators
34004 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
34006 poplevel (0, 1, 0);
34007 error_at (loc, "%<iterator%> modifier incompatible with %qs",
34008 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
34009 iterators = NULL_TREE;
34012 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
34014 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
34015 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34016 OMP_CLAUSE_DECL (c) = NULL_TREE;
34017 OMP_CLAUSE_CHAIN (c) = list;
34018 if (!parens.require_close (parser))
34019 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34020 /*or_comma=*/false,
34021 /*consume_paren=*/true);
34022 return c;
34025 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34026 goto resync_fail;
34028 if (kind == OMP_CLAUSE_DEPEND_SINK)
34029 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34030 else
34032 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34033 list, NULL);
34035 if (iterators)
34037 tree block = poplevel (1, 1, 0);
34038 if (iterators == error_mark_node)
34039 iterators = NULL_TREE;
34040 else
34041 TREE_VEC_ELT (iterators, 5) = block;
34044 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34046 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34047 if (iterators)
34048 OMP_CLAUSE_DECL (c)
34049 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34052 return nlist;
34054 invalid_kind:
34055 cp_parser_error (parser, "invalid depend kind");
34056 resync_fail:
34057 if (iterators)
34058 poplevel (0, 1, 0);
34059 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34060 /*or_comma=*/false,
34061 /*consume_paren=*/true);
34062 return list;
34065 /* OpenMP 4.0:
34066 map ( map-kind : variable-list )
34067 map ( variable-list )
34069 map-kind:
34070 alloc | to | from | tofrom
34072 OpenMP 4.5:
34073 map-kind:
34074 alloc | to | from | tofrom | release | delete
34076 map ( always [,] map-kind: variable-list ) */
34078 static tree
34079 cp_parser_omp_clause_map (cp_parser *parser, tree list)
34081 tree nlist, c;
34082 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
34083 bool always = false;
34085 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34086 return list;
34088 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34090 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34091 const char *p = IDENTIFIER_POINTER (id);
34093 if (strcmp ("always", p) == 0)
34095 int nth = 2;
34096 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
34097 nth++;
34098 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
34099 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
34100 == RID_DELETE))
34101 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
34102 == CPP_COLON))
34104 always = true;
34105 cp_lexer_consume_token (parser->lexer);
34106 if (nth == 3)
34107 cp_lexer_consume_token (parser->lexer);
34112 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34113 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34115 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34116 const char *p = IDENTIFIER_POINTER (id);
34118 if (strcmp ("alloc", p) == 0)
34119 kind = GOMP_MAP_ALLOC;
34120 else if (strcmp ("to", p) == 0)
34121 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
34122 else if (strcmp ("from", p) == 0)
34123 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
34124 else if (strcmp ("tofrom", p) == 0)
34125 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
34126 else if (strcmp ("release", p) == 0)
34127 kind = GOMP_MAP_RELEASE;
34128 else
34130 cp_parser_error (parser, "invalid map kind");
34131 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34132 /*or_comma=*/false,
34133 /*consume_paren=*/true);
34134 return list;
34136 cp_lexer_consume_token (parser->lexer);
34137 cp_lexer_consume_token (parser->lexer);
34139 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
34140 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34142 kind = GOMP_MAP_DELETE;
34143 cp_lexer_consume_token (parser->lexer);
34144 cp_lexer_consume_token (parser->lexer);
34147 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
34148 NULL);
34150 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34151 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34153 return nlist;
34156 /* OpenMP 4.0:
34157 device ( expression ) */
34159 static tree
34160 cp_parser_omp_clause_device (cp_parser *parser, tree list,
34161 location_t location)
34163 tree t, c;
34165 matching_parens parens;
34166 if (!parens.require_open (parser))
34167 return list;
34169 t = cp_parser_assignment_expression (parser);
34171 if (t == error_mark_node
34172 || !parens.require_close (parser))
34173 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34174 /*or_comma=*/false,
34175 /*consume_paren=*/true);
34177 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
34178 "device", location);
34180 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
34181 OMP_CLAUSE_DEVICE_ID (c) = t;
34182 OMP_CLAUSE_CHAIN (c) = list;
34184 return c;
34187 /* OpenMP 4.0:
34188 dist_schedule ( static )
34189 dist_schedule ( static , expression ) */
34191 static tree
34192 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
34193 location_t location)
34195 tree c, t;
34197 matching_parens parens;
34198 if (!parens.require_open (parser))
34199 return list;
34201 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
34203 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34204 goto invalid_kind;
34205 cp_lexer_consume_token (parser->lexer);
34207 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34209 cp_lexer_consume_token (parser->lexer);
34211 t = cp_parser_assignment_expression (parser);
34213 if (t == error_mark_node)
34214 goto resync_fail;
34215 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
34217 if (!parens.require_close (parser))
34218 goto resync_fail;
34220 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34221 goto resync_fail;
34223 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
34224 location);
34225 OMP_CLAUSE_CHAIN (c) = list;
34226 return c;
34228 invalid_kind:
34229 cp_parser_error (parser, "invalid dist_schedule kind");
34230 resync_fail:
34231 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34232 /*or_comma=*/false,
34233 /*consume_paren=*/true);
34234 return list;
34237 /* OpenMP 4.0:
34238 proc_bind ( proc-bind-kind )
34240 proc-bind-kind:
34241 master | close | spread */
34243 static tree
34244 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
34245 location_t location)
34247 tree c;
34248 enum omp_clause_proc_bind_kind kind;
34250 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34251 return list;
34253 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34255 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34256 const char *p = IDENTIFIER_POINTER (id);
34258 if (strcmp ("master", p) == 0)
34259 kind = OMP_CLAUSE_PROC_BIND_MASTER;
34260 else if (strcmp ("close", p) == 0)
34261 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
34262 else if (strcmp ("spread", p) == 0)
34263 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
34264 else
34265 goto invalid_kind;
34267 else
34268 goto invalid_kind;
34270 cp_lexer_consume_token (parser->lexer);
34271 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34272 goto resync_fail;
34274 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
34275 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
34276 location);
34277 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
34278 OMP_CLAUSE_CHAIN (c) = list;
34279 return c;
34281 invalid_kind:
34282 cp_parser_error (parser, "invalid depend kind");
34283 resync_fail:
34284 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34285 /*or_comma=*/false,
34286 /*consume_paren=*/true);
34287 return list;
34290 /* OpenACC:
34291 async [( int-expr )] */
34293 static tree
34294 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
34296 tree c, t;
34297 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34299 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34301 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34303 matching_parens parens;
34304 parens.consume_open (parser);
34306 t = cp_parser_expression (parser);
34307 if (t == error_mark_node
34308 || !parens.require_close (parser))
34309 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34310 /*or_comma=*/false,
34311 /*consume_paren=*/true);
34314 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
34316 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
34317 OMP_CLAUSE_ASYNC_EXPR (c) = t;
34318 OMP_CLAUSE_CHAIN (c) = list;
34319 list = c;
34321 return list;
34324 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34325 is a bitmask in MASK. Return the list of clauses found. */
34327 static tree
34328 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
34329 const char *where, cp_token *pragma_tok,
34330 bool finish_p = true)
34332 tree clauses = NULL;
34333 bool first = true;
34335 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34337 location_t here;
34338 pragma_omp_clause c_kind;
34339 omp_clause_code code;
34340 const char *c_name;
34341 tree prev = clauses;
34343 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34344 cp_lexer_consume_token (parser->lexer);
34346 here = cp_lexer_peek_token (parser->lexer)->location;
34347 c_kind = cp_parser_omp_clause_name (parser);
34349 switch (c_kind)
34351 case PRAGMA_OACC_CLAUSE_ASYNC:
34352 clauses = cp_parser_oacc_clause_async (parser, clauses);
34353 c_name = "async";
34354 break;
34355 case PRAGMA_OACC_CLAUSE_AUTO:
34356 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
34357 clauses, here);
34358 c_name = "auto";
34359 break;
34360 case PRAGMA_OACC_CLAUSE_COLLAPSE:
34361 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
34362 c_name = "collapse";
34363 break;
34364 case PRAGMA_OACC_CLAUSE_COPY:
34365 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34366 c_name = "copy";
34367 break;
34368 case PRAGMA_OACC_CLAUSE_COPYIN:
34369 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34370 c_name = "copyin";
34371 break;
34372 case PRAGMA_OACC_CLAUSE_COPYOUT:
34373 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34374 c_name = "copyout";
34375 break;
34376 case PRAGMA_OACC_CLAUSE_CREATE:
34377 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34378 c_name = "create";
34379 break;
34380 case PRAGMA_OACC_CLAUSE_DELETE:
34381 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34382 c_name = "delete";
34383 break;
34384 case PRAGMA_OMP_CLAUSE_DEFAULT:
34385 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
34386 c_name = "default";
34387 break;
34388 case PRAGMA_OACC_CLAUSE_DEVICE:
34389 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34390 c_name = "device";
34391 break;
34392 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
34393 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
34394 c_name = "deviceptr";
34395 break;
34396 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
34397 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34398 c_name = "device_resident";
34399 break;
34400 case PRAGMA_OACC_CLAUSE_FINALIZE:
34401 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
34402 clauses, here);
34403 c_name = "finalize";
34404 break;
34405 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
34406 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34407 clauses);
34408 c_name = "firstprivate";
34409 break;
34410 case PRAGMA_OACC_CLAUSE_GANG:
34411 c_name = "gang";
34412 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
34413 c_name, clauses);
34414 break;
34415 case PRAGMA_OACC_CLAUSE_HOST:
34416 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34417 c_name = "host";
34418 break;
34419 case PRAGMA_OACC_CLAUSE_IF:
34420 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
34421 c_name = "if";
34422 break;
34423 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
34424 clauses = cp_parser_oacc_simple_clause (parser,
34425 OMP_CLAUSE_IF_PRESENT,
34426 clauses, here);
34427 c_name = "if_present";
34428 break;
34429 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
34430 clauses = cp_parser_oacc_simple_clause (parser,
34431 OMP_CLAUSE_INDEPENDENT,
34432 clauses, here);
34433 c_name = "independent";
34434 break;
34435 case PRAGMA_OACC_CLAUSE_LINK:
34436 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34437 c_name = "link";
34438 break;
34439 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
34440 code = OMP_CLAUSE_NUM_GANGS;
34441 c_name = "num_gangs";
34442 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34443 clauses);
34444 break;
34445 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
34446 c_name = "num_workers";
34447 code = OMP_CLAUSE_NUM_WORKERS;
34448 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34449 clauses);
34450 break;
34451 case PRAGMA_OACC_CLAUSE_PRESENT:
34452 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34453 c_name = "present";
34454 break;
34455 case PRAGMA_OACC_CLAUSE_PRIVATE:
34456 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34457 clauses);
34458 c_name = "private";
34459 break;
34460 case PRAGMA_OACC_CLAUSE_REDUCTION:
34461 clauses
34462 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34463 false, clauses);
34464 c_name = "reduction";
34465 break;
34466 case PRAGMA_OACC_CLAUSE_SEQ:
34467 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
34468 clauses, here);
34469 c_name = "seq";
34470 break;
34471 case PRAGMA_OACC_CLAUSE_TILE:
34472 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
34473 c_name = "tile";
34474 break;
34475 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
34476 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34477 clauses);
34478 c_name = "use_device";
34479 break;
34480 case PRAGMA_OACC_CLAUSE_VECTOR:
34481 c_name = "vector";
34482 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
34483 c_name, clauses);
34484 break;
34485 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
34486 c_name = "vector_length";
34487 code = OMP_CLAUSE_VECTOR_LENGTH;
34488 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34489 clauses);
34490 break;
34491 case PRAGMA_OACC_CLAUSE_WAIT:
34492 clauses = cp_parser_oacc_clause_wait (parser, clauses);
34493 c_name = "wait";
34494 break;
34495 case PRAGMA_OACC_CLAUSE_WORKER:
34496 c_name = "worker";
34497 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
34498 c_name, clauses);
34499 break;
34500 default:
34501 cp_parser_error (parser, "expected %<#pragma acc%> clause");
34502 goto saw_error;
34505 first = false;
34507 if (((mask >> c_kind) & 1) == 0)
34509 /* Remove the invalid clause(s) from the list to avoid
34510 confusing the rest of the compiler. */
34511 clauses = prev;
34512 error_at (here, "%qs is not valid for %qs", c_name, where);
34516 saw_error:
34517 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34519 if (finish_p)
34520 return finish_omp_clauses (clauses, C_ORT_ACC);
34522 return clauses;
34525 /* Parse all OpenMP clauses. The set clauses allowed by the directive
34526 is a bitmask in MASK. Return the list of clauses found; the result
34527 of clause default goes in *pdefault. */
34529 static tree
34530 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
34531 const char *where, cp_token *pragma_tok,
34532 bool finish_p = true)
34534 tree clauses = NULL;
34535 bool first = true;
34536 cp_token *token = NULL;
34538 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34540 pragma_omp_clause c_kind;
34541 const char *c_name;
34542 tree prev = clauses;
34544 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34545 cp_lexer_consume_token (parser->lexer);
34547 token = cp_lexer_peek_token (parser->lexer);
34548 c_kind = cp_parser_omp_clause_name (parser);
34550 switch (c_kind)
34552 case PRAGMA_OMP_CLAUSE_COLLAPSE:
34553 clauses = cp_parser_omp_clause_collapse (parser, clauses,
34554 token->location);
34555 c_name = "collapse";
34556 break;
34557 case PRAGMA_OMP_CLAUSE_COPYIN:
34558 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
34559 c_name = "copyin";
34560 break;
34561 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
34562 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
34563 clauses);
34564 c_name = "copyprivate";
34565 break;
34566 case PRAGMA_OMP_CLAUSE_DEFAULT:
34567 clauses = cp_parser_omp_clause_default (parser, clauses,
34568 token->location, false);
34569 c_name = "default";
34570 break;
34571 case PRAGMA_OMP_CLAUSE_FINAL:
34572 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
34573 c_name = "final";
34574 break;
34575 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
34576 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34577 clauses);
34578 c_name = "firstprivate";
34579 break;
34580 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
34581 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
34582 token->location);
34583 c_name = "grainsize";
34584 break;
34585 case PRAGMA_OMP_CLAUSE_HINT:
34586 clauses = cp_parser_omp_clause_hint (parser, clauses,
34587 token->location);
34588 c_name = "hint";
34589 break;
34590 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
34591 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34592 token->location);
34593 c_name = "defaultmap";
34594 break;
34595 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34596 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34597 clauses);
34598 c_name = "use_device_ptr";
34599 break;
34600 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34601 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34602 clauses);
34603 c_name = "is_device_ptr";
34604 break;
34605 case PRAGMA_OMP_CLAUSE_IF:
34606 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34607 true);
34608 c_name = "if";
34609 break;
34610 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
34611 clauses
34612 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
34613 true, clauses);
34614 c_name = "in_reduction";
34615 break;
34616 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34617 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
34618 c_name = "lastprivate";
34619 break;
34620 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34621 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34622 token->location);
34623 c_name = "mergeable";
34624 break;
34625 case PRAGMA_OMP_CLAUSE_NOWAIT:
34626 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34627 c_name = "nowait";
34628 break;
34629 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34630 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34631 token->location);
34632 c_name = "num_tasks";
34633 break;
34634 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34635 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34636 token->location);
34637 c_name = "num_threads";
34638 break;
34639 case PRAGMA_OMP_CLAUSE_ORDERED:
34640 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34641 token->location);
34642 c_name = "ordered";
34643 break;
34644 case PRAGMA_OMP_CLAUSE_PRIORITY:
34645 clauses = cp_parser_omp_clause_priority (parser, clauses,
34646 token->location);
34647 c_name = "priority";
34648 break;
34649 case PRAGMA_OMP_CLAUSE_PRIVATE:
34650 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34651 clauses);
34652 c_name = "private";
34653 break;
34654 case PRAGMA_OMP_CLAUSE_REDUCTION:
34655 clauses
34656 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34657 true, clauses);
34658 c_name = "reduction";
34659 break;
34660 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34661 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34662 token->location);
34663 c_name = "schedule";
34664 break;
34665 case PRAGMA_OMP_CLAUSE_SHARED:
34666 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34667 clauses);
34668 c_name = "shared";
34669 break;
34670 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
34671 clauses
34672 = cp_parser_omp_clause_reduction (parser,
34673 OMP_CLAUSE_TASK_REDUCTION,
34674 true, clauses);
34675 c_name = "task_reduction";
34676 break;
34677 case PRAGMA_OMP_CLAUSE_UNTIED:
34678 clauses = cp_parser_omp_clause_untied (parser, clauses,
34679 token->location);
34680 c_name = "untied";
34681 break;
34682 case PRAGMA_OMP_CLAUSE_INBRANCH:
34683 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34684 clauses, token->location);
34685 c_name = "inbranch";
34686 break;
34687 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
34688 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
34689 clauses);
34690 c_name = "nontemporal";
34691 break;
34692 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34693 clauses = cp_parser_omp_clause_branch (parser,
34694 OMP_CLAUSE_NOTINBRANCH,
34695 clauses, token->location);
34696 c_name = "notinbranch";
34697 break;
34698 case PRAGMA_OMP_CLAUSE_PARALLEL:
34699 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34700 clauses, token->location);
34701 c_name = "parallel";
34702 if (!first)
34704 clause_not_first:
34705 error_at (token->location, "%qs must be the first clause of %qs",
34706 c_name, where);
34707 clauses = prev;
34709 break;
34710 case PRAGMA_OMP_CLAUSE_FOR:
34711 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34712 clauses, token->location);
34713 c_name = "for";
34714 if (!first)
34715 goto clause_not_first;
34716 break;
34717 case PRAGMA_OMP_CLAUSE_SECTIONS:
34718 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34719 clauses, token->location);
34720 c_name = "sections";
34721 if (!first)
34722 goto clause_not_first;
34723 break;
34724 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34725 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34726 clauses, token->location);
34727 c_name = "taskgroup";
34728 if (!first)
34729 goto clause_not_first;
34730 break;
34731 case PRAGMA_OMP_CLAUSE_LINK:
34732 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34733 c_name = "to";
34734 break;
34735 case PRAGMA_OMP_CLAUSE_TO:
34736 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34737 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34738 clauses);
34739 else
34740 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34741 c_name = "to";
34742 break;
34743 case PRAGMA_OMP_CLAUSE_FROM:
34744 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34745 c_name = "from";
34746 break;
34747 case PRAGMA_OMP_CLAUSE_UNIFORM:
34748 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34749 clauses);
34750 c_name = "uniform";
34751 break;
34752 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34753 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34754 token->location);
34755 c_name = "num_teams";
34756 break;
34757 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34758 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34759 token->location);
34760 c_name = "thread_limit";
34761 break;
34762 case PRAGMA_OMP_CLAUSE_ALIGNED:
34763 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34764 c_name = "aligned";
34765 break;
34766 case PRAGMA_OMP_CLAUSE_LINEAR:
34768 bool declare_simd = false;
34769 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34770 declare_simd = true;
34771 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34773 c_name = "linear";
34774 break;
34775 case PRAGMA_OMP_CLAUSE_DEPEND:
34776 clauses = cp_parser_omp_clause_depend (parser, clauses,
34777 token->location);
34778 c_name = "depend";
34779 break;
34780 case PRAGMA_OMP_CLAUSE_MAP:
34781 clauses = cp_parser_omp_clause_map (parser, clauses);
34782 c_name = "map";
34783 break;
34784 case PRAGMA_OMP_CLAUSE_DEVICE:
34785 clauses = cp_parser_omp_clause_device (parser, clauses,
34786 token->location);
34787 c_name = "device";
34788 break;
34789 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34790 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34791 token->location);
34792 c_name = "dist_schedule";
34793 break;
34794 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34795 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34796 token->location);
34797 c_name = "proc_bind";
34798 break;
34799 case PRAGMA_OMP_CLAUSE_SAFELEN:
34800 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34801 token->location);
34802 c_name = "safelen";
34803 break;
34804 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34805 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34806 token->location);
34807 c_name = "simdlen";
34808 break;
34809 case PRAGMA_OMP_CLAUSE_NOGROUP:
34810 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34811 token->location);
34812 c_name = "nogroup";
34813 break;
34814 case PRAGMA_OMP_CLAUSE_THREADS:
34815 clauses
34816 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34817 clauses, token->location);
34818 c_name = "threads";
34819 break;
34820 case PRAGMA_OMP_CLAUSE_SIMD:
34821 clauses
34822 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34823 clauses, token->location);
34824 c_name = "simd";
34825 break;
34826 default:
34827 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34828 goto saw_error;
34831 first = false;
34833 if (((mask >> c_kind) & 1) == 0)
34835 /* Remove the invalid clause(s) from the list to avoid
34836 confusing the rest of the compiler. */
34837 clauses = prev;
34838 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34841 saw_error:
34842 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34843 if (finish_p)
34845 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34846 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34847 else
34848 return finish_omp_clauses (clauses, C_ORT_OMP);
34850 return clauses;
34853 /* OpenMP 2.5:
34854 structured-block:
34855 statement
34857 In practice, we're also interested in adding the statement to an
34858 outer node. So it is convenient if we work around the fact that
34859 cp_parser_statement calls add_stmt. */
34861 static unsigned
34862 cp_parser_begin_omp_structured_block (cp_parser *parser)
34864 unsigned save = parser->in_statement;
34866 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34867 This preserves the "not within loop or switch" style error messages
34868 for nonsense cases like
34869 void foo() {
34870 #pragma omp single
34871 break;
34874 if (parser->in_statement)
34875 parser->in_statement = IN_OMP_BLOCK;
34877 return save;
34880 static void
34881 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34883 parser->in_statement = save;
34886 static tree
34887 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34889 tree stmt = begin_omp_structured_block ();
34890 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34892 cp_parser_statement (parser, NULL_TREE, false, if_p);
34894 cp_parser_end_omp_structured_block (parser, save);
34895 return finish_omp_structured_block (stmt);
34898 /* OpenMP 2.5:
34899 # pragma omp atomic new-line
34900 expression-stmt
34902 expression-stmt:
34903 x binop= expr | x++ | ++x | x-- | --x
34904 binop:
34905 +, *, -, /, &, ^, |, <<, >>
34907 where x is an lvalue expression with scalar type.
34909 OpenMP 3.1:
34910 # pragma omp atomic new-line
34911 update-stmt
34913 # pragma omp atomic read new-line
34914 read-stmt
34916 # pragma omp atomic write new-line
34917 write-stmt
34919 # pragma omp atomic update new-line
34920 update-stmt
34922 # pragma omp atomic capture new-line
34923 capture-stmt
34925 # pragma omp atomic capture new-line
34926 capture-block
34928 read-stmt:
34929 v = x
34930 write-stmt:
34931 x = expr
34932 update-stmt:
34933 expression-stmt | x = x binop expr
34934 capture-stmt:
34935 v = expression-stmt
34936 capture-block:
34937 { v = x; update-stmt; } | { update-stmt; v = x; }
34939 OpenMP 4.0:
34940 update-stmt:
34941 expression-stmt | x = x binop expr | x = expr binop x
34942 capture-stmt:
34943 v = update-stmt
34944 capture-block:
34945 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34947 where x and v are lvalue expressions with scalar type. */
34949 static void
34950 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34952 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34953 tree rhs1 = NULL_TREE, orig_lhs;
34954 location_t loc = pragma_tok->location;
34955 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
34956 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
34957 bool structured_block = false;
34958 bool first = true;
34959 tree clauses = NULL_TREE;
34961 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34963 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34964 cp_lexer_consume_token (parser->lexer);
34966 first = false;
34968 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34970 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34971 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
34972 const char *p = IDENTIFIER_POINTER (id);
34973 enum tree_code new_code = ERROR_MARK;
34974 enum omp_memory_order new_memory_order
34975 = OMP_MEMORY_ORDER_UNSPECIFIED;
34977 if (!strcmp (p, "read"))
34978 new_code = OMP_ATOMIC_READ;
34979 else if (!strcmp (p, "write"))
34980 new_code = NOP_EXPR;
34981 else if (!strcmp (p, "update"))
34982 new_code = OMP_ATOMIC;
34983 else if (!strcmp (p, "capture"))
34984 new_code = OMP_ATOMIC_CAPTURE_NEW;
34985 else if (!strcmp (p, "seq_cst"))
34986 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
34987 else if (!strcmp (p, "acq_rel"))
34988 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
34989 else if (!strcmp (p, "release"))
34990 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
34991 else if (!strcmp (p, "acquire"))
34992 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
34993 else if (!strcmp (p, "relaxed"))
34994 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
34995 else if (!strcmp (p, "hint"))
34997 cp_lexer_consume_token (parser->lexer);
34998 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
34999 continue;
35001 else
35003 p = NULL;
35004 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
35005 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35006 "%<release%>, %<relaxed%> or %<hint%> clause");
35008 if (p)
35010 if (new_code != ERROR_MARK)
35012 if (code != ERROR_MARK)
35013 error_at (cloc, "too many atomic clauses");
35014 else
35015 code = new_code;
35017 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35019 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35020 error_at (cloc, "too many memory order clauses");
35021 else
35022 memory_order = new_memory_order;
35024 cp_lexer_consume_token (parser->lexer);
35025 continue;
35028 break;
35030 cp_parser_require_pragma_eol (parser, pragma_tok);
35032 if (code == ERROR_MARK)
35033 code = OMP_ATOMIC;
35034 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35036 omp_requires_mask
35037 = (enum omp_requires) (omp_requires_mask
35038 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35039 switch ((enum omp_memory_order)
35040 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35042 case OMP_MEMORY_ORDER_UNSPECIFIED:
35043 case OMP_MEMORY_ORDER_RELAXED:
35044 memory_order = OMP_MEMORY_ORDER_RELAXED;
35045 break;
35046 case OMP_MEMORY_ORDER_SEQ_CST:
35047 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35048 break;
35049 case OMP_MEMORY_ORDER_ACQ_REL:
35050 switch (code)
35052 case OMP_ATOMIC_READ:
35053 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35054 break;
35055 case NOP_EXPR: /* atomic write */
35056 case OMP_ATOMIC:
35057 memory_order = OMP_MEMORY_ORDER_RELEASE;
35058 break;
35059 default:
35060 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35061 break;
35063 break;
35064 default:
35065 gcc_unreachable ();
35068 else
35069 switch (code)
35071 case OMP_ATOMIC_READ:
35072 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35073 || memory_order == OMP_MEMORY_ORDER_RELEASE)
35075 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
35076 "%<acq_rel%> or %<release%> clauses");
35077 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35079 break;
35080 case NOP_EXPR: /* atomic write */
35081 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35082 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35084 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
35085 "%<acq_rel%> or %<acquire%> clauses");
35086 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35088 break;
35089 case OMP_ATOMIC:
35090 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35091 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35093 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
35094 "%<acq_rel%> or %<acquire%> clauses");
35095 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35097 break;
35098 default:
35099 break;
35102 switch (code)
35104 case OMP_ATOMIC_READ:
35105 case NOP_EXPR: /* atomic write */
35106 v = cp_parser_unary_expression (parser);
35107 if (v == error_mark_node)
35108 goto saw_error;
35109 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35110 goto saw_error;
35111 if (code == NOP_EXPR)
35112 lhs = cp_parser_expression (parser);
35113 else
35114 lhs = cp_parser_unary_expression (parser);
35115 if (lhs == error_mark_node)
35116 goto saw_error;
35117 if (code == NOP_EXPR)
35119 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35120 opcode. */
35121 code = OMP_ATOMIC;
35122 rhs = lhs;
35123 lhs = v;
35124 v = NULL_TREE;
35126 goto done;
35127 case OMP_ATOMIC_CAPTURE_NEW:
35128 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35130 cp_lexer_consume_token (parser->lexer);
35131 structured_block = true;
35133 else
35135 v = cp_parser_unary_expression (parser);
35136 if (v == error_mark_node)
35137 goto saw_error;
35138 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35139 goto saw_error;
35141 default:
35142 break;
35145 restart:
35146 lhs = cp_parser_unary_expression (parser);
35147 orig_lhs = lhs;
35148 switch (TREE_CODE (lhs))
35150 case ERROR_MARK:
35151 goto saw_error;
35153 case POSTINCREMENT_EXPR:
35154 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35155 code = OMP_ATOMIC_CAPTURE_OLD;
35156 /* FALLTHROUGH */
35157 case PREINCREMENT_EXPR:
35158 lhs = TREE_OPERAND (lhs, 0);
35159 opcode = PLUS_EXPR;
35160 rhs = integer_one_node;
35161 break;
35163 case POSTDECREMENT_EXPR:
35164 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35165 code = OMP_ATOMIC_CAPTURE_OLD;
35166 /* FALLTHROUGH */
35167 case PREDECREMENT_EXPR:
35168 lhs = TREE_OPERAND (lhs, 0);
35169 opcode = MINUS_EXPR;
35170 rhs = integer_one_node;
35171 break;
35173 case COMPOUND_EXPR:
35174 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
35175 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
35176 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
35177 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
35178 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35179 (TREE_OPERAND (lhs, 1), 0), 0)))
35180 == BOOLEAN_TYPE)
35181 /* Undo effects of boolean_increment for post {in,de}crement. */
35182 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
35183 /* FALLTHRU */
35184 case MODIFY_EXPR:
35185 if (TREE_CODE (lhs) == MODIFY_EXPR
35186 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
35188 /* Undo effects of boolean_increment. */
35189 if (integer_onep (TREE_OPERAND (lhs, 1)))
35191 /* This is pre or post increment. */
35192 rhs = TREE_OPERAND (lhs, 1);
35193 lhs = TREE_OPERAND (lhs, 0);
35194 opcode = NOP_EXPR;
35195 if (code == OMP_ATOMIC_CAPTURE_NEW
35196 && !structured_block
35197 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
35198 code = OMP_ATOMIC_CAPTURE_OLD;
35199 break;
35202 /* FALLTHRU */
35203 default:
35204 switch (cp_lexer_peek_token (parser->lexer)->type)
35206 case CPP_MULT_EQ:
35207 opcode = MULT_EXPR;
35208 break;
35209 case CPP_DIV_EQ:
35210 opcode = TRUNC_DIV_EXPR;
35211 break;
35212 case CPP_PLUS_EQ:
35213 opcode = PLUS_EXPR;
35214 break;
35215 case CPP_MINUS_EQ:
35216 opcode = MINUS_EXPR;
35217 break;
35218 case CPP_LSHIFT_EQ:
35219 opcode = LSHIFT_EXPR;
35220 break;
35221 case CPP_RSHIFT_EQ:
35222 opcode = RSHIFT_EXPR;
35223 break;
35224 case CPP_AND_EQ:
35225 opcode = BIT_AND_EXPR;
35226 break;
35227 case CPP_OR_EQ:
35228 opcode = BIT_IOR_EXPR;
35229 break;
35230 case CPP_XOR_EQ:
35231 opcode = BIT_XOR_EXPR;
35232 break;
35233 case CPP_EQ:
35234 enum cp_parser_prec oprec;
35235 cp_token *token;
35236 cp_lexer_consume_token (parser->lexer);
35237 cp_parser_parse_tentatively (parser);
35238 rhs1 = cp_parser_simple_cast_expression (parser);
35239 if (rhs1 == error_mark_node)
35241 cp_parser_abort_tentative_parse (parser);
35242 cp_parser_simple_cast_expression (parser);
35243 goto saw_error;
35245 token = cp_lexer_peek_token (parser->lexer);
35246 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
35248 cp_parser_abort_tentative_parse (parser);
35249 cp_parser_parse_tentatively (parser);
35250 rhs = cp_parser_binary_expression (parser, false, true,
35251 PREC_NOT_OPERATOR, NULL);
35252 if (rhs == error_mark_node)
35254 cp_parser_abort_tentative_parse (parser);
35255 cp_parser_binary_expression (parser, false, true,
35256 PREC_NOT_OPERATOR, NULL);
35257 goto saw_error;
35259 switch (TREE_CODE (rhs))
35261 case MULT_EXPR:
35262 case TRUNC_DIV_EXPR:
35263 case RDIV_EXPR:
35264 case PLUS_EXPR:
35265 case MINUS_EXPR:
35266 case LSHIFT_EXPR:
35267 case RSHIFT_EXPR:
35268 case BIT_AND_EXPR:
35269 case BIT_IOR_EXPR:
35270 case BIT_XOR_EXPR:
35271 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
35273 if (cp_parser_parse_definitely (parser))
35275 opcode = TREE_CODE (rhs);
35276 rhs1 = TREE_OPERAND (rhs, 0);
35277 rhs = TREE_OPERAND (rhs, 1);
35278 goto stmt_done;
35280 else
35281 goto saw_error;
35283 break;
35284 default:
35285 break;
35287 cp_parser_abort_tentative_parse (parser);
35288 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
35290 rhs = cp_parser_expression (parser);
35291 if (rhs == error_mark_node)
35292 goto saw_error;
35293 opcode = NOP_EXPR;
35294 rhs1 = NULL_TREE;
35295 goto stmt_done;
35297 cp_parser_error (parser,
35298 "invalid form of %<#pragma omp atomic%>");
35299 goto saw_error;
35301 if (!cp_parser_parse_definitely (parser))
35302 goto saw_error;
35303 switch (token->type)
35305 case CPP_SEMICOLON:
35306 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35308 code = OMP_ATOMIC_CAPTURE_OLD;
35309 v = lhs;
35310 lhs = NULL_TREE;
35311 lhs1 = rhs1;
35312 rhs1 = NULL_TREE;
35313 cp_lexer_consume_token (parser->lexer);
35314 goto restart;
35316 else if (structured_block)
35318 opcode = NOP_EXPR;
35319 rhs = rhs1;
35320 rhs1 = NULL_TREE;
35321 goto stmt_done;
35323 cp_parser_error (parser,
35324 "invalid form of %<#pragma omp atomic%>");
35325 goto saw_error;
35326 case CPP_MULT:
35327 opcode = MULT_EXPR;
35328 break;
35329 case CPP_DIV:
35330 opcode = TRUNC_DIV_EXPR;
35331 break;
35332 case CPP_PLUS:
35333 opcode = PLUS_EXPR;
35334 break;
35335 case CPP_MINUS:
35336 opcode = MINUS_EXPR;
35337 break;
35338 case CPP_LSHIFT:
35339 opcode = LSHIFT_EXPR;
35340 break;
35341 case CPP_RSHIFT:
35342 opcode = RSHIFT_EXPR;
35343 break;
35344 case CPP_AND:
35345 opcode = BIT_AND_EXPR;
35346 break;
35347 case CPP_OR:
35348 opcode = BIT_IOR_EXPR;
35349 break;
35350 case CPP_XOR:
35351 opcode = BIT_XOR_EXPR;
35352 break;
35353 default:
35354 cp_parser_error (parser,
35355 "invalid operator for %<#pragma omp atomic%>");
35356 goto saw_error;
35358 oprec = TOKEN_PRECEDENCE (token);
35359 gcc_assert (oprec != PREC_NOT_OPERATOR);
35360 if (commutative_tree_code (opcode))
35361 oprec = (enum cp_parser_prec) (oprec - 1);
35362 cp_lexer_consume_token (parser->lexer);
35363 rhs = cp_parser_binary_expression (parser, false, false,
35364 oprec, NULL);
35365 if (rhs == error_mark_node)
35366 goto saw_error;
35367 goto stmt_done;
35368 /* FALLTHROUGH */
35369 default:
35370 cp_parser_error (parser,
35371 "invalid operator for %<#pragma omp atomic%>");
35372 goto saw_error;
35374 cp_lexer_consume_token (parser->lexer);
35376 rhs = cp_parser_expression (parser);
35377 if (rhs == error_mark_node)
35378 goto saw_error;
35379 break;
35381 stmt_done:
35382 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35384 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
35385 goto saw_error;
35386 v = cp_parser_unary_expression (parser);
35387 if (v == error_mark_node)
35388 goto saw_error;
35389 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35390 goto saw_error;
35391 lhs1 = cp_parser_unary_expression (parser);
35392 if (lhs1 == error_mark_node)
35393 goto saw_error;
35395 if (structured_block)
35397 cp_parser_consume_semicolon_at_end_of_statement (parser);
35398 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35400 done:
35401 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
35402 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
35403 rhs1, clauses, memory_order);
35404 if (!structured_block)
35405 cp_parser_consume_semicolon_at_end_of_statement (parser);
35406 return;
35408 saw_error:
35409 cp_parser_skip_to_end_of_block_or_statement (parser);
35410 if (structured_block)
35412 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35413 cp_lexer_consume_token (parser->lexer);
35414 else if (code == OMP_ATOMIC_CAPTURE_NEW)
35416 cp_parser_skip_to_end_of_block_or_statement (parser);
35417 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35418 cp_lexer_consume_token (parser->lexer);
35424 /* OpenMP 2.5:
35425 # pragma omp barrier new-line */
35427 static void
35428 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
35430 cp_parser_require_pragma_eol (parser, pragma_tok);
35431 finish_omp_barrier ();
35434 /* OpenMP 2.5:
35435 # pragma omp critical [(name)] new-line
35436 structured-block
35438 OpenMP 4.5:
35439 # pragma omp critical [(name) [hint(expression)]] new-line
35440 structured-block */
35442 #define OMP_CRITICAL_CLAUSE_MASK \
35443 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35445 static tree
35446 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35448 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
35450 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35452 matching_parens parens;
35453 parens.consume_open (parser);
35455 name = cp_parser_identifier (parser);
35457 if (name == error_mark_node
35458 || !parens.require_close (parser))
35459 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35460 /*or_comma=*/false,
35461 /*consume_paren=*/true);
35462 if (name == error_mark_node)
35463 name = NULL;
35465 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
35466 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
35467 cp_lexer_consume_token (parser->lexer);
35469 clauses = cp_parser_omp_all_clauses (parser,
35470 OMP_CRITICAL_CLAUSE_MASK,
35471 "#pragma omp critical", pragma_tok);
35473 else
35474 cp_parser_require_pragma_eol (parser, pragma_tok);
35476 stmt = cp_parser_omp_structured_block (parser, if_p);
35477 return c_finish_omp_critical (input_location, stmt, name, clauses);
35480 /* OpenMP 5.0:
35481 # pragma omp depobj ( depobj ) depobj-clause new-line
35483 depobj-clause:
35484 depend (dependence-type : locator)
35485 destroy
35486 update (dependence-type)
35488 dependence-type:
35491 inout
35492 mutexinout */
35494 static void
35495 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
35497 location_t loc = pragma_tok->location;
35498 matching_parens parens;
35499 if (!parens.require_open (parser))
35501 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35502 return;
35505 tree depobj = cp_parser_assignment_expression (parser);
35507 if (!parens.require_close (parser))
35508 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35509 /*or_comma=*/false,
35510 /*consume_paren=*/true);
35512 tree clause = NULL_TREE;
35513 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
35514 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
35515 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35517 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35518 const char *p = IDENTIFIER_POINTER (id);
35520 cp_lexer_consume_token (parser->lexer);
35521 if (!strcmp ("depend", p))
35523 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
35524 if (clause)
35525 clause = finish_omp_clauses (clause, C_ORT_OMP);
35526 if (!clause)
35527 clause = error_mark_node;
35529 else if (!strcmp ("destroy", p))
35530 kind = OMP_CLAUSE_DEPEND_LAST;
35531 else if (!strcmp ("update", p))
35533 matching_parens c_parens;
35534 if (c_parens.require_open (parser))
35536 location_t c2_loc
35537 = cp_lexer_peek_token (parser->lexer)->location;
35538 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35540 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
35541 const char *p2 = IDENTIFIER_POINTER (id2);
35543 cp_lexer_consume_token (parser->lexer);
35544 if (!strcmp ("in", p2))
35545 kind = OMP_CLAUSE_DEPEND_IN;
35546 else if (!strcmp ("out", p2))
35547 kind = OMP_CLAUSE_DEPEND_OUT;
35548 else if (!strcmp ("inout", p2))
35549 kind = OMP_CLAUSE_DEPEND_INOUT;
35550 else if (!strcmp ("mutexinoutset", p2))
35551 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
35553 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
35555 clause = error_mark_node;
35556 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
35557 "%<mutexinoutset%>");
35559 if (!c_parens.require_close (parser))
35560 cp_parser_skip_to_closing_parenthesis (parser,
35561 /*recovering=*/true,
35562 /*or_comma=*/false,
35563 /*consume_paren=*/true);
35565 else
35566 clause = error_mark_node;
35569 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
35571 clause = error_mark_node;
35572 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
35574 cp_parser_require_pragma_eol (parser, pragma_tok);
35576 finish_omp_depobj (loc, depobj, kind, clause);
35580 /* OpenMP 2.5:
35581 # pragma omp flush flush-vars[opt] new-line
35583 flush-vars:
35584 ( variable-list )
35586 OpenMP 5.0:
35587 # pragma omp flush memory-order-clause new-line */
35589 static void
35590 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
35592 enum memmodel mo = MEMMODEL_LAST;
35593 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35595 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35596 const char *p = IDENTIFIER_POINTER (id);
35597 if (!strcmp (p, "acq_rel"))
35598 mo = MEMMODEL_ACQ_REL;
35599 else if (!strcmp (p, "release"))
35600 mo = MEMMODEL_RELEASE;
35601 else if (!strcmp (p, "acquire"))
35602 mo = MEMMODEL_ACQUIRE;
35603 else
35604 error_at (cp_lexer_peek_token (parser->lexer)->location,
35605 "expected %<acq_rel%>, %<release%> or %<acquire%>");
35606 cp_lexer_consume_token (parser->lexer);
35608 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35610 if (mo != MEMMODEL_LAST)
35611 error_at (cp_lexer_peek_token (parser->lexer)->location,
35612 "%<flush%> list specified together with memory order "
35613 "clause");
35614 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35616 cp_parser_require_pragma_eol (parser, pragma_tok);
35618 finish_omp_flush (mo);
35621 /* Helper function, to parse omp for increment expression. */
35623 static tree
35624 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
35626 tree cond = cp_parser_binary_expression (parser, false, true,
35627 PREC_NOT_OPERATOR, NULL);
35628 if (cond == error_mark_node
35629 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35631 cp_parser_skip_to_end_of_statement (parser);
35632 return error_mark_node;
35635 switch (TREE_CODE (cond))
35637 case GT_EXPR:
35638 case GE_EXPR:
35639 case LT_EXPR:
35640 case LE_EXPR:
35641 break;
35642 case NE_EXPR:
35643 if (code != OACC_LOOP)
35644 break;
35645 gcc_fallthrough ();
35646 default:
35647 return error_mark_node;
35650 /* If decl is an iterator, preserve LHS and RHS of the relational
35651 expr until finish_omp_for. */
35652 if (decl
35653 && (type_dependent_expression_p (decl)
35654 || CLASS_TYPE_P (TREE_TYPE (decl))))
35655 return cond;
35657 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
35658 TREE_CODE (cond),
35659 TREE_OPERAND (cond, 0), ERROR_MARK,
35660 TREE_OPERAND (cond, 1), ERROR_MARK,
35661 /*overload=*/NULL, tf_warning_or_error);
35664 /* Helper function, to parse omp for increment expression. */
35666 static tree
35667 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
35669 cp_token *token = cp_lexer_peek_token (parser->lexer);
35670 enum tree_code op;
35671 tree lhs, rhs;
35672 cp_id_kind idk;
35673 bool decl_first;
35675 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
35677 op = (token->type == CPP_PLUS_PLUS
35678 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
35679 cp_lexer_consume_token (parser->lexer);
35680 lhs = cp_parser_simple_cast_expression (parser);
35681 if (lhs != decl
35682 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
35683 return error_mark_node;
35684 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
35687 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
35688 if (lhs != decl
35689 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
35690 return error_mark_node;
35692 token = cp_lexer_peek_token (parser->lexer);
35693 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
35695 op = (token->type == CPP_PLUS_PLUS
35696 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
35697 cp_lexer_consume_token (parser->lexer);
35698 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
35701 op = cp_parser_assignment_operator_opt (parser);
35702 if (op == ERROR_MARK)
35703 return error_mark_node;
35705 if (op != NOP_EXPR)
35707 rhs = cp_parser_assignment_expression (parser);
35708 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
35709 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35712 lhs = cp_parser_binary_expression (parser, false, false,
35713 PREC_ADDITIVE_EXPRESSION, NULL);
35714 token = cp_lexer_peek_token (parser->lexer);
35715 decl_first = (lhs == decl
35716 || (processing_template_decl && cp_tree_equal (lhs, decl)));
35717 if (decl_first)
35718 lhs = NULL_TREE;
35719 if (token->type != CPP_PLUS
35720 && token->type != CPP_MINUS)
35721 return error_mark_node;
35725 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
35726 cp_lexer_consume_token (parser->lexer);
35727 rhs = cp_parser_binary_expression (parser, false, false,
35728 PREC_ADDITIVE_EXPRESSION, NULL);
35729 token = cp_lexer_peek_token (parser->lexer);
35730 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
35732 if (lhs == NULL_TREE)
35734 if (op == PLUS_EXPR)
35735 lhs = rhs;
35736 else
35737 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
35738 tf_warning_or_error);
35740 else
35741 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
35742 ERROR_MARK, NULL, tf_warning_or_error);
35745 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
35747 if (!decl_first)
35749 if ((rhs != decl
35750 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
35751 || op == MINUS_EXPR)
35752 return error_mark_node;
35753 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
35755 else
35756 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
35758 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35761 /* Parse the initialization statement of an OpenMP for loop.
35763 Return true if the resulting construct should have an
35764 OMP_CLAUSE_PRIVATE added to it. */
35766 static tree
35767 cp_parser_omp_for_loop_init (cp_parser *parser,
35768 tree &this_pre_body,
35769 vec<tree, va_gc> *&for_block,
35770 tree &init,
35771 tree &orig_init,
35772 tree &decl,
35773 tree &real_decl)
35775 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35776 return NULL_TREE;
35778 tree add_private_clause = NULL_TREE;
35780 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
35782 init-expr:
35783 var = lb
35784 integer-type var = lb
35785 random-access-iterator-type var = lb
35786 pointer-type var = lb
35788 cp_decl_specifier_seq type_specifiers;
35790 /* First, try to parse as an initialized declaration. See
35791 cp_parser_condition, from whence the bulk of this is copied. */
35793 cp_parser_parse_tentatively (parser);
35794 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
35795 /*is_trailing_return=*/false,
35796 &type_specifiers);
35797 if (cp_parser_parse_definitely (parser))
35799 /* If parsing a type specifier seq succeeded, then this
35800 MUST be a initialized declaration. */
35801 tree asm_specification, attributes;
35802 cp_declarator *declarator;
35804 declarator = cp_parser_declarator (parser,
35805 CP_PARSER_DECLARATOR_NAMED,
35806 /*ctor_dtor_or_conv_p=*/NULL,
35807 /*parenthesized_p=*/NULL,
35808 /*member_p=*/false,
35809 /*friend_p=*/false);
35810 attributes = cp_parser_attributes_opt (parser);
35811 asm_specification = cp_parser_asm_specification_opt (parser);
35813 if (declarator == cp_error_declarator)
35814 cp_parser_skip_to_end_of_statement (parser);
35816 else
35818 tree pushed_scope, auto_node;
35820 decl = start_decl (declarator, &type_specifiers,
35821 SD_INITIALIZED, attributes,
35822 /*prefix_attributes=*/NULL_TREE,
35823 &pushed_scope);
35825 auto_node = type_uses_auto (TREE_TYPE (decl));
35826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
35828 if (cp_lexer_next_token_is (parser->lexer,
35829 CPP_OPEN_PAREN))
35830 error ("parenthesized initialization is not allowed in "
35831 "OpenMP %<for%> loop");
35832 else
35833 /* Trigger an error. */
35834 cp_parser_require (parser, CPP_EQ, RT_EQ);
35836 init = error_mark_node;
35837 cp_parser_skip_to_end_of_statement (parser);
35839 else if (CLASS_TYPE_P (TREE_TYPE (decl))
35840 || type_dependent_expression_p (decl)
35841 || auto_node)
35843 bool is_direct_init, is_non_constant_init;
35845 init = cp_parser_initializer (parser,
35846 &is_direct_init,
35847 &is_non_constant_init);
35849 if (auto_node)
35851 TREE_TYPE (decl)
35852 = do_auto_deduction (TREE_TYPE (decl), init,
35853 auto_node);
35855 if (!CLASS_TYPE_P (TREE_TYPE (decl))
35856 && !type_dependent_expression_p (decl))
35857 goto non_class;
35860 cp_finish_decl (decl, init, !is_non_constant_init,
35861 asm_specification,
35862 LOOKUP_ONLYCONVERTING);
35863 orig_init = init;
35864 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35866 vec_safe_push (for_block, this_pre_body);
35867 init = NULL_TREE;
35869 else
35871 init = pop_stmt_list (this_pre_body);
35872 if (init && TREE_CODE (init) == STATEMENT_LIST)
35874 tree_stmt_iterator i = tsi_start (init);
35875 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35876 while (!tsi_end_p (i))
35878 tree t = tsi_stmt (i);
35879 if (TREE_CODE (t) == DECL_EXPR
35880 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35882 tsi_delink (&i);
35883 vec_safe_push (for_block, t);
35884 continue;
35886 break;
35888 if (tsi_one_before_end_p (i))
35890 tree t = tsi_stmt (i);
35891 tsi_delink (&i);
35892 free_stmt_list (init);
35893 init = t;
35897 this_pre_body = NULL_TREE;
35899 else
35901 /* Consume '='. */
35902 cp_lexer_consume_token (parser->lexer);
35903 init = cp_parser_assignment_expression (parser);
35905 non_class:
35906 if (TYPE_REF_P (TREE_TYPE (decl)))
35907 init = error_mark_node;
35908 else
35909 cp_finish_decl (decl, NULL_TREE,
35910 /*init_const_expr_p=*/false,
35911 asm_specification,
35912 LOOKUP_ONLYCONVERTING);
35915 if (pushed_scope)
35916 pop_scope (pushed_scope);
35919 else
35921 cp_id_kind idk;
35922 /* If parsing a type specifier sequence failed, then
35923 this MUST be a simple expression. */
35924 cp_parser_parse_tentatively (parser);
35925 decl = cp_parser_primary_expression (parser, false, false,
35926 false, &idk);
35927 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35928 if (!cp_parser_error_occurred (parser)
35929 && decl
35930 && (TREE_CODE (decl) == COMPONENT_REF
35931 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35933 cp_parser_abort_tentative_parse (parser);
35934 cp_parser_parse_tentatively (parser);
35935 cp_token *token = cp_lexer_peek_token (parser->lexer);
35936 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35937 /*check_dependency_p=*/true,
35938 /*template_p=*/NULL,
35939 /*declarator_p=*/false,
35940 /*optional_p=*/false);
35941 if (name != error_mark_node
35942 && last_tok == cp_lexer_peek_token (parser->lexer))
35944 decl = cp_parser_lookup_name_simple (parser, name,
35945 token->location);
35946 if (TREE_CODE (decl) == FIELD_DECL)
35947 add_private_clause = omp_privatize_field (decl, false);
35949 cp_parser_abort_tentative_parse (parser);
35950 cp_parser_parse_tentatively (parser);
35951 decl = cp_parser_primary_expression (parser, false, false,
35952 false, &idk);
35954 if (!cp_parser_error_occurred (parser)
35955 && decl
35956 && DECL_P (decl)
35957 && CLASS_TYPE_P (TREE_TYPE (decl)))
35959 tree rhs;
35961 cp_parser_parse_definitely (parser);
35962 cp_parser_require (parser, CPP_EQ, RT_EQ);
35963 rhs = cp_parser_assignment_expression (parser);
35964 orig_init = rhs;
35965 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35966 decl, NOP_EXPR,
35967 rhs,
35968 tf_warning_or_error));
35969 if (!add_private_clause)
35970 add_private_clause = decl;
35972 else
35974 decl = NULL;
35975 cp_parser_abort_tentative_parse (parser);
35976 init = cp_parser_expression (parser);
35977 if (init)
35979 if (TREE_CODE (init) == MODIFY_EXPR
35980 || TREE_CODE (init) == MODOP_EXPR)
35981 real_decl = TREE_OPERAND (init, 0);
35985 return add_private_clause;
35988 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
35990 void
35991 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
35992 tree &decl, tree &orig_decl, tree &init,
35993 tree &orig_init, tree &cond, tree &incr)
35995 tree begin, end, range_temp_decl = NULL_TREE;
35996 tree iter_type, begin_expr, end_expr;
35998 if (processing_template_decl)
36000 if (check_for_bare_parameter_packs (init))
36001 init = error_mark_node;
36002 if (!type_dependent_expression_p (init)
36003 /* do_auto_deduction doesn't mess with template init-lists. */
36004 && !BRACE_ENCLOSED_INITIALIZER_P (init))
36006 tree d = decl;
36007 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
36009 tree v = DECL_VALUE_EXPR (decl);
36010 if (TREE_CODE (v) == ARRAY_REF
36011 && VAR_P (TREE_OPERAND (v, 0))
36012 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36013 d = TREE_OPERAND (v, 0);
36015 do_range_for_auto_deduction (d, init);
36017 cond = global_namespace;
36018 incr = NULL_TREE;
36019 orig_init = init;
36020 if (this_pre_body)
36021 this_pre_body = pop_stmt_list (this_pre_body);
36022 return;
36025 init = mark_lvalue_use (init);
36027 if (decl == error_mark_node || init == error_mark_node)
36028 /* If an error happened previously do nothing or else a lot of
36029 unhelpful errors would be issued. */
36030 begin_expr = end_expr = iter_type = error_mark_node;
36031 else
36033 tree range_temp;
36035 if (VAR_P (init)
36036 && array_of_runtime_bound_p (TREE_TYPE (init)))
36037 /* Can't bind a reference to an array of runtime bound. */
36038 range_temp = init;
36039 else
36041 range_temp = build_range_temp (init);
36042 DECL_NAME (range_temp) = NULL_TREE;
36043 pushdecl (range_temp);
36044 cp_finish_decl (range_temp, init,
36045 /*is_constant_init*/false, NULL_TREE,
36046 LOOKUP_ONLYCONVERTING);
36047 range_temp_decl = range_temp;
36048 range_temp = convert_from_reference (range_temp);
36050 iter_type = cp_parser_perform_range_for_lookup (range_temp,
36051 &begin_expr, &end_expr);
36054 tree end_iter_type = iter_type;
36055 if (cxx_dialect >= cxx17)
36056 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36057 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
36058 TREE_USED (end) = 1;
36059 DECL_ARTIFICIAL (end) = 1;
36060 pushdecl (end);
36061 cp_finish_decl (end, end_expr,
36062 /*is_constant_init*/false, NULL_TREE,
36063 LOOKUP_ONLYCONVERTING);
36065 /* The new for initialization statement. */
36066 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
36067 TREE_USED (begin) = 1;
36068 DECL_ARTIFICIAL (begin) = 1;
36069 pushdecl (begin);
36070 orig_init = init;
36071 if (CLASS_TYPE_P (iter_type))
36072 init = NULL_TREE;
36073 else
36075 init = begin_expr;
36076 begin_expr = NULL_TREE;
36078 cp_finish_decl (begin, begin_expr,
36079 /*is_constant_init*/false, NULL_TREE,
36080 LOOKUP_ONLYCONVERTING);
36082 /* The new for condition. */
36083 if (CLASS_TYPE_P (iter_type))
36084 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
36085 else
36086 cond = build_x_binary_op (input_location, NE_EXPR,
36087 begin, ERROR_MARK,
36088 end, ERROR_MARK,
36089 NULL, tf_warning_or_error);
36091 /* The new increment expression. */
36092 if (CLASS_TYPE_P (iter_type))
36093 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
36094 else
36095 incr = finish_unary_op_expr (input_location,
36096 PREINCREMENT_EXPR, begin,
36097 tf_warning_or_error);
36099 orig_decl = decl;
36100 decl = begin;
36101 if (for_block)
36103 vec_safe_push (for_block, this_pre_body);
36104 this_pre_body = NULL_TREE;
36107 tree decomp_first_name = NULL_TREE;
36108 unsigned decomp_cnt = 0;
36109 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
36111 tree v = DECL_VALUE_EXPR (orig_decl);
36112 if (TREE_CODE (v) == ARRAY_REF
36113 && VAR_P (TREE_OPERAND (v, 0))
36114 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36116 tree d = orig_decl;
36117 orig_decl = TREE_OPERAND (v, 0);
36118 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
36119 decomp_first_name = d;
36123 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
36124 if (auto_node)
36126 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36127 tf_none);
36128 if (!error_operand_p (t))
36129 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
36130 t, auto_node);
36133 tree v = make_tree_vec (decomp_cnt + 3);
36134 TREE_VEC_ELT (v, 0) = range_temp_decl;
36135 TREE_VEC_ELT (v, 1) = end;
36136 TREE_VEC_ELT (v, 2) = orig_decl;
36137 for (unsigned i = 0; i < decomp_cnt; i++)
36139 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
36140 decomp_first_name = DECL_CHAIN (decomp_first_name);
36142 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
36145 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36146 inside of the collapsed body. */
36148 void
36149 cp_finish_omp_range_for (tree orig, tree begin)
36151 gcc_assert (TREE_CODE (orig) == TREE_LIST
36152 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
36153 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
36154 tree decomp_first_name = NULL_TREE;
36155 unsigned int decomp_cnt = 0;
36157 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36159 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
36160 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
36161 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
36164 /* The declaration is initialized with *__begin inside the loop body. */
36165 cp_finish_decl (decl,
36166 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36167 tf_warning_or_error),
36168 /*is_constant_init*/false, NULL_TREE,
36169 LOOKUP_ONLYCONVERTING);
36170 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36171 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
36174 /* Parse the restricted form of the for statement allowed by OpenMP. */
36176 static tree
36177 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
36178 tree *cclauses, bool *if_p)
36180 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
36181 tree orig_decl;
36182 tree real_decl, initv, condv, incrv, declv, orig_declv;
36183 tree this_pre_body, cl, ordered_cl = NULL_TREE;
36184 location_t loc_first;
36185 bool collapse_err = false;
36186 int i, collapse = 1, ordered = 0, count, nbraces = 0;
36187 vec<tree, va_gc> *for_block = make_tree_vector ();
36188 auto_vec<tree, 4> orig_inits;
36189 bool tiling = false;
36191 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
36192 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
36193 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
36194 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
36196 tiling = true;
36197 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
36199 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
36200 && OMP_CLAUSE_ORDERED_EXPR (cl))
36202 ordered_cl = cl;
36203 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
36206 if (ordered && ordered < collapse)
36208 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36209 "%<ordered%> clause parameter is less than %<collapse%>");
36210 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
36211 = build_int_cst (NULL_TREE, collapse);
36212 ordered = collapse;
36214 if (ordered)
36216 for (tree *pc = &clauses; *pc; )
36217 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
36219 error_at (OMP_CLAUSE_LOCATION (*pc),
36220 "%<linear%> clause may not be specified together "
36221 "with %<ordered%> clause with a parameter");
36222 *pc = OMP_CLAUSE_CHAIN (*pc);
36224 else
36225 pc = &OMP_CLAUSE_CHAIN (*pc);
36228 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
36229 count = ordered ? ordered : collapse;
36231 declv = make_tree_vec (count);
36232 initv = make_tree_vec (count);
36233 condv = make_tree_vec (count);
36234 incrv = make_tree_vec (count);
36235 orig_declv = NULL_TREE;
36237 loc_first = cp_lexer_peek_token (parser->lexer)->location;
36239 for (i = 0; i < count; i++)
36241 int bracecount = 0;
36242 tree add_private_clause = NULL_TREE;
36243 location_t loc;
36245 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36247 if (!collapse_err)
36248 cp_parser_error (parser, "for statement expected");
36249 return NULL;
36251 loc = cp_lexer_consume_token (parser->lexer)->location;
36253 matching_parens parens;
36254 if (!parens.require_open (parser))
36255 return NULL;
36257 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
36258 this_pre_body = push_stmt_list ();
36260 if (code != OACC_LOOP && cxx_dialect >= cxx11)
36262 /* Save tokens so that we can put them back. */
36263 cp_lexer_save_tokens (parser->lexer);
36265 /* Look for ':' that is not nested in () or {}. */
36266 bool is_range_for
36267 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
36268 /*recovering=*/false,
36269 CPP_COLON,
36270 /*consume_paren=*/
36271 false) == -1);
36273 /* Roll back the tokens we skipped. */
36274 cp_lexer_rollback_tokens (parser->lexer);
36276 if (is_range_for)
36278 bool saved_colon_corrects_to_scope_p
36279 = parser->colon_corrects_to_scope_p;
36281 /* A colon is used in range-based for. */
36282 parser->colon_corrects_to_scope_p = false;
36284 /* Parse the declaration. */
36285 cp_parser_simple_declaration (parser,
36286 /*function_definition_allowed_p=*/
36287 false, &decl);
36288 parser->colon_corrects_to_scope_p
36289 = saved_colon_corrects_to_scope_p;
36291 cp_parser_require (parser, CPP_COLON, RT_COLON);
36293 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
36294 false, 0, true);
36296 cp_convert_omp_range_for (this_pre_body, for_block, decl,
36297 orig_decl, init, orig_init,
36298 cond, incr);
36299 if (this_pre_body)
36301 if (pre_body)
36303 tree t = pre_body;
36304 pre_body = push_stmt_list ();
36305 add_stmt (t);
36306 add_stmt (this_pre_body);
36307 pre_body = pop_stmt_list (pre_body);
36309 else
36310 pre_body = this_pre_body;
36313 if (ordered_cl)
36314 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36315 "%<ordered%> clause with parameter on "
36316 "range-based %<for%> loop");
36318 goto parse_close_paren;
36322 add_private_clause
36323 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
36324 init, orig_init, decl, real_decl);
36326 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36327 if (this_pre_body)
36329 this_pre_body = pop_stmt_list (this_pre_body);
36330 if (pre_body)
36332 tree t = pre_body;
36333 pre_body = push_stmt_list ();
36334 add_stmt (t);
36335 add_stmt (this_pre_body);
36336 pre_body = pop_stmt_list (pre_body);
36338 else
36339 pre_body = this_pre_body;
36342 if (decl)
36343 real_decl = decl;
36344 if (cclauses != NULL
36345 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
36346 && real_decl != NULL_TREE)
36348 tree *c;
36349 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
36350 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
36351 && OMP_CLAUSE_DECL (*c) == real_decl)
36353 error_at (loc, "iteration variable %qD"
36354 " should not be firstprivate", real_decl);
36355 *c = OMP_CLAUSE_CHAIN (*c);
36357 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
36358 && OMP_CLAUSE_DECL (*c) == real_decl)
36360 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36361 tree l = *c;
36362 *c = OMP_CLAUSE_CHAIN (*c);
36363 if (code == OMP_SIMD)
36365 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36366 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
36368 else
36370 OMP_CLAUSE_CHAIN (l) = clauses;
36371 clauses = l;
36373 add_private_clause = NULL_TREE;
36375 else
36377 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
36378 && OMP_CLAUSE_DECL (*c) == real_decl)
36379 add_private_clause = NULL_TREE;
36380 c = &OMP_CLAUSE_CHAIN (*c);
36384 if (add_private_clause)
36386 tree c;
36387 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
36389 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
36390 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
36391 && OMP_CLAUSE_DECL (c) == decl)
36392 break;
36393 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
36394 && OMP_CLAUSE_DECL (c) == decl)
36395 error_at (loc, "iteration variable %qD "
36396 "should not be firstprivate",
36397 decl);
36398 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
36399 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
36400 && OMP_CLAUSE_DECL (c) == decl)
36401 error_at (loc, "iteration variable %qD should not be reduction",
36402 decl);
36404 if (c == NULL)
36406 if (code != OMP_SIMD)
36407 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
36408 else if (collapse == 1)
36409 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
36410 else
36411 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
36412 OMP_CLAUSE_DECL (c) = add_private_clause;
36413 c = finish_omp_clauses (c, C_ORT_OMP);
36414 if (c)
36416 OMP_CLAUSE_CHAIN (c) = clauses;
36417 clauses = c;
36418 /* For linear, signal that we need to fill up
36419 the so far unknown linear step. */
36420 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
36421 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
36426 cond = NULL;
36427 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36428 cond = cp_parser_omp_for_cond (parser, decl, code);
36429 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36431 incr = NULL;
36432 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
36434 /* If decl is an iterator, preserve the operator on decl
36435 until finish_omp_for. */
36436 if (real_decl
36437 && ((processing_template_decl
36438 && (TREE_TYPE (real_decl) == NULL_TREE
36439 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
36440 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
36441 incr = cp_parser_omp_for_incr (parser, real_decl);
36442 else
36443 incr = cp_parser_expression (parser);
36444 if (!EXPR_HAS_LOCATION (incr))
36445 protected_set_expr_location (incr, input_location);
36448 parse_close_paren:
36449 if (!parens.require_close (parser))
36450 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36451 /*or_comma=*/false,
36452 /*consume_paren=*/true);
36454 TREE_VEC_ELT (declv, i) = decl;
36455 TREE_VEC_ELT (initv, i) = init;
36456 TREE_VEC_ELT (condv, i) = cond;
36457 TREE_VEC_ELT (incrv, i) = incr;
36458 if (orig_init)
36460 orig_inits.safe_grow_cleared (i + 1);
36461 orig_inits[i] = orig_init;
36463 if (orig_decl)
36465 if (!orig_declv)
36466 orig_declv = copy_node (declv);
36467 TREE_VEC_ELT (orig_declv, i) = orig_decl;
36469 else if (orig_declv)
36470 TREE_VEC_ELT (orig_declv, i) = decl;
36472 if (i == count - 1)
36473 break;
36475 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
36476 in between the collapsed for loops to be still considered perfectly
36477 nested. Hopefully the final version clarifies this.
36478 For now handle (multiple) {'s and empty statements. */
36479 cp_parser_parse_tentatively (parser);
36480 for (;;)
36482 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36483 break;
36484 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36486 cp_lexer_consume_token (parser->lexer);
36487 bracecount++;
36489 else if (bracecount
36490 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36491 cp_lexer_consume_token (parser->lexer);
36492 else
36494 loc = cp_lexer_peek_token (parser->lexer)->location;
36495 error_at (loc, "not enough for loops to collapse");
36496 collapse_err = true;
36497 cp_parser_abort_tentative_parse (parser);
36498 declv = NULL_TREE;
36499 break;
36503 if (declv)
36505 cp_parser_parse_definitely (parser);
36506 nbraces += bracecount;
36510 if (nbraces)
36511 if_p = NULL;
36513 /* Note that we saved the original contents of this flag when we entered
36514 the structured block, and so we don't need to re-save it here. */
36515 parser->in_statement = IN_OMP_FOR;
36517 /* Note that the grammar doesn't call for a structured block here,
36518 though the loop as a whole is a structured block. */
36519 if (orig_declv)
36521 body = begin_omp_structured_block ();
36522 for (i = 0; i < count; i++)
36523 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
36524 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
36525 TREE_VEC_ELT (declv, i));
36527 else
36528 body = push_stmt_list ();
36529 cp_parser_statement (parser, NULL_TREE, false, if_p);
36530 if (orig_declv)
36531 body = finish_omp_structured_block (body);
36532 else
36533 body = pop_stmt_list (body);
36535 if (declv == NULL_TREE)
36536 ret = NULL_TREE;
36537 else
36538 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
36539 incrv, body, pre_body, &orig_inits, clauses);
36541 while (nbraces)
36543 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36545 cp_lexer_consume_token (parser->lexer);
36546 nbraces--;
36548 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36549 cp_lexer_consume_token (parser->lexer);
36550 else
36552 if (!collapse_err)
36554 error_at (cp_lexer_peek_token (parser->lexer)->location,
36555 "collapsed loops not perfectly nested");
36557 collapse_err = true;
36558 cp_parser_statement_seq_opt (parser, NULL);
36559 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
36560 break;
36564 while (!for_block->is_empty ())
36566 tree t = for_block->pop ();
36567 if (TREE_CODE (t) == STATEMENT_LIST)
36568 add_stmt (pop_stmt_list (t));
36569 else
36570 add_stmt (t);
36572 release_tree_vector (for_block);
36574 return ret;
36577 /* Helper function for OpenMP parsing, split clauses and call
36578 finish_omp_clauses on each of the set of clauses afterwards. */
36580 static void
36581 cp_omp_split_clauses (location_t loc, enum tree_code code,
36582 omp_clause_mask mask, tree clauses, tree *cclauses)
36584 int i;
36585 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
36586 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
36587 if (cclauses[i])
36588 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
36591 /* OpenMP 4.0:
36592 #pragma omp simd simd-clause[optseq] new-line
36593 for-loop */
36595 #define OMP_SIMD_CLAUSE_MASK \
36596 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
36597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
36607 static tree
36608 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
36609 char *p_name, omp_clause_mask mask, tree *cclauses,
36610 bool *if_p)
36612 tree clauses, sb, ret;
36613 unsigned int save;
36614 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36616 strcat (p_name, " simd");
36617 mask |= OMP_SIMD_CLAUSE_MASK;
36619 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36620 cclauses == NULL);
36621 if (cclauses)
36623 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
36624 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
36625 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
36626 OMP_CLAUSE_ORDERED);
36627 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
36629 error_at (OMP_CLAUSE_LOCATION (c),
36630 "%<ordered%> clause with parameter may not be specified "
36631 "on %qs construct", p_name);
36632 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
36636 keep_next_level (true);
36637 sb = begin_omp_structured_block ();
36638 save = cp_parser_begin_omp_structured_block (parser);
36640 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
36642 cp_parser_end_omp_structured_block (parser, save);
36643 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
36645 return ret;
36648 /* OpenMP 2.5:
36649 #pragma omp for for-clause[optseq] new-line
36650 for-loop
36652 OpenMP 4.0:
36653 #pragma omp for simd for-simd-clause[optseq] new-line
36654 for-loop */
36656 #define OMP_FOR_CLAUSE_MASK \
36657 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
36663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
36664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36667 static tree
36668 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
36669 char *p_name, omp_clause_mask mask, tree *cclauses,
36670 bool *if_p)
36672 tree clauses, sb, ret;
36673 unsigned int save;
36674 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36676 strcat (p_name, " for");
36677 mask |= OMP_FOR_CLAUSE_MASK;
36678 /* parallel for{, simd} disallows nowait clause, but for
36679 target {teams distribute ,}parallel for{, simd} it should be accepted. */
36680 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
36681 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
36682 /* Composite distribute parallel for{, simd} disallows ordered clause. */
36683 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
36684 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
36686 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36688 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36689 const char *p = IDENTIFIER_POINTER (id);
36691 if (strcmp (p, "simd") == 0)
36693 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36694 if (cclauses == NULL)
36695 cclauses = cclauses_buf;
36697 cp_lexer_consume_token (parser->lexer);
36698 if (!flag_openmp) /* flag_openmp_simd */
36699 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36700 cclauses, if_p);
36701 sb = begin_omp_structured_block ();
36702 save = cp_parser_begin_omp_structured_block (parser);
36703 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36704 cclauses, if_p);
36705 cp_parser_end_omp_structured_block (parser, save);
36706 tree body = finish_omp_structured_block (sb);
36707 if (ret == NULL)
36708 return ret;
36709 ret = make_node (OMP_FOR);
36710 TREE_TYPE (ret) = void_type_node;
36711 OMP_FOR_BODY (ret) = body;
36712 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36713 SET_EXPR_LOCATION (ret, loc);
36714 add_stmt (ret);
36715 return ret;
36718 if (!flag_openmp) /* flag_openmp_simd */
36720 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36721 return NULL_TREE;
36724 /* Composite distribute parallel for disallows linear clause. */
36725 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
36726 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
36728 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36729 cclauses == NULL);
36730 if (cclauses)
36732 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
36733 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36736 keep_next_level (true);
36737 sb = begin_omp_structured_block ();
36738 save = cp_parser_begin_omp_structured_block (parser);
36740 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
36742 cp_parser_end_omp_structured_block (parser, save);
36743 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
36745 return ret;
36748 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
36749 omp_clause_mask, tree *, bool *);
36751 /* OpenMP 2.5:
36752 # pragma omp master new-line
36753 structured-block */
36755 static tree
36756 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
36757 char *p_name, omp_clause_mask mask, tree *cclauses,
36758 bool *if_p)
36760 tree clauses, sb, ret;
36761 unsigned int save;
36762 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36764 strcat (p_name, " master");
36766 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36768 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36769 const char *p = IDENTIFIER_POINTER (id);
36771 if (strcmp (p, "taskloop") == 0)
36773 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36774 if (cclauses == NULL)
36775 cclauses = cclauses_buf;
36777 cp_lexer_consume_token (parser->lexer);
36778 if (!flag_openmp) /* flag_openmp_simd */
36779 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
36780 cclauses, if_p);
36781 sb = begin_omp_structured_block ();
36782 save = cp_parser_begin_omp_structured_block (parser);
36783 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
36784 cclauses, if_p);
36785 cp_parser_end_omp_structured_block (parser, save);
36786 tree body = finish_omp_structured_block (sb);
36787 if (ret == NULL)
36788 return ret;
36789 return c_finish_omp_master (loc, body);
36792 if (!flag_openmp) /* flag_openmp_simd */
36794 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36795 return NULL_TREE;
36798 if (cclauses)
36800 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36801 false);
36802 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
36804 else
36805 cp_parser_require_pragma_eol (parser, pragma_tok);
36807 return c_finish_omp_master (loc,
36808 cp_parser_omp_structured_block (parser, if_p));
36811 /* OpenMP 2.5:
36812 # pragma omp ordered new-line
36813 structured-block
36815 OpenMP 4.5:
36816 # pragma omp ordered ordered-clauses new-line
36817 structured-block */
36819 #define OMP_ORDERED_CLAUSE_MASK \
36820 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
36821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
36823 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
36824 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
36826 static bool
36827 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
36828 enum pragma_context context, bool *if_p)
36830 location_t loc = pragma_tok->location;
36832 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36834 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36835 const char *p = IDENTIFIER_POINTER (id);
36837 if (strcmp (p, "depend") == 0)
36839 if (!flag_openmp) /* flag_openmp_simd */
36841 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36842 return false;
36844 if (context == pragma_stmt)
36846 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
36847 "%<depend%> clause may only be used in compound "
36848 "statements");
36849 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36850 return false;
36852 tree clauses
36853 = cp_parser_omp_all_clauses (parser,
36854 OMP_ORDERED_DEPEND_CLAUSE_MASK,
36855 "#pragma omp ordered", pragma_tok);
36856 c_finish_omp_ordered (loc, clauses, NULL_TREE);
36857 return false;
36861 tree clauses
36862 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
36863 "#pragma omp ordered", pragma_tok);
36865 if (!flag_openmp /* flag_openmp_simd */
36866 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
36867 return false;
36869 c_finish_omp_ordered (loc, clauses,
36870 cp_parser_omp_structured_block (parser, if_p));
36871 return true;
36874 /* OpenMP 2.5:
36876 section-scope:
36877 { section-sequence }
36879 section-sequence:
36880 section-directive[opt] structured-block
36881 section-sequence section-directive structured-block */
36883 static tree
36884 cp_parser_omp_sections_scope (cp_parser *parser)
36886 tree stmt, substmt;
36887 bool error_suppress = false;
36888 cp_token *tok;
36890 matching_braces braces;
36891 if (!braces.require_open (parser))
36892 return NULL_TREE;
36894 stmt = push_stmt_list ();
36896 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
36897 != PRAGMA_OMP_SECTION)
36899 substmt = cp_parser_omp_structured_block (parser, NULL);
36900 substmt = build1 (OMP_SECTION, void_type_node, substmt);
36901 add_stmt (substmt);
36904 while (1)
36906 tok = cp_lexer_peek_token (parser->lexer);
36907 if (tok->type == CPP_CLOSE_BRACE)
36908 break;
36909 if (tok->type == CPP_EOF)
36910 break;
36912 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
36914 cp_lexer_consume_token (parser->lexer);
36915 cp_parser_require_pragma_eol (parser, tok);
36916 error_suppress = false;
36918 else if (!error_suppress)
36920 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
36921 error_suppress = true;
36924 substmt = cp_parser_omp_structured_block (parser, NULL);
36925 substmt = build1 (OMP_SECTION, void_type_node, substmt);
36926 add_stmt (substmt);
36928 braces.require_close (parser);
36930 substmt = pop_stmt_list (stmt);
36932 stmt = make_node (OMP_SECTIONS);
36933 TREE_TYPE (stmt) = void_type_node;
36934 OMP_SECTIONS_BODY (stmt) = substmt;
36936 add_stmt (stmt);
36937 return stmt;
36940 /* OpenMP 2.5:
36941 # pragma omp sections sections-clause[optseq] newline
36942 sections-scope */
36944 #define OMP_SECTIONS_CLAUSE_MASK \
36945 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36951 static tree
36952 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
36953 char *p_name, omp_clause_mask mask, tree *cclauses)
36955 tree clauses, ret;
36956 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36958 strcat (p_name, " sections");
36959 mask |= OMP_SECTIONS_CLAUSE_MASK;
36960 if (cclauses)
36961 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
36963 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36964 cclauses == NULL);
36965 if (cclauses)
36967 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
36968 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
36971 ret = cp_parser_omp_sections_scope (parser);
36972 if (ret)
36973 OMP_SECTIONS_CLAUSES (ret) = clauses;
36975 return ret;
36978 /* OpenMP 2.5:
36979 # pragma omp parallel parallel-clause[optseq] new-line
36980 structured-block
36981 # pragma omp parallel for parallel-for-clause[optseq] new-line
36982 structured-block
36983 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
36984 structured-block
36986 OpenMP 4.0:
36987 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
36988 structured-block */
36990 #define OMP_PARALLEL_CLAUSE_MASK \
36991 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
36997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
36999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37001 static tree
37002 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
37003 char *p_name, omp_clause_mask mask, tree *cclauses,
37004 bool *if_p)
37006 tree stmt, clauses, block;
37007 unsigned int save;
37008 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37010 strcat (p_name, " parallel");
37011 mask |= OMP_PARALLEL_CLAUSE_MASK;
37012 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37013 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
37014 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
37015 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
37017 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37019 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37020 if (cclauses == NULL)
37021 cclauses = cclauses_buf;
37023 cp_lexer_consume_token (parser->lexer);
37024 if (!flag_openmp) /* flag_openmp_simd */
37025 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37026 if_p);
37027 block = begin_omp_parallel ();
37028 save = cp_parser_begin_omp_structured_block (parser);
37029 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37030 if_p);
37031 cp_parser_end_omp_structured_block (parser, save);
37032 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37033 block);
37034 if (ret == NULL_TREE)
37035 return ret;
37036 OMP_PARALLEL_COMBINED (stmt) = 1;
37037 return stmt;
37039 /* When combined with distribute, parallel has to be followed by for.
37040 #pragma omp target parallel is allowed though. */
37041 else if (cclauses
37042 && (mask & (OMP_CLAUSE_MASK_1
37043 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37045 error_at (loc, "expected %<for%> after %qs", p_name);
37046 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37047 return NULL_TREE;
37049 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37051 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37052 const char *p = IDENTIFIER_POINTER (id);
37053 if (strcmp (p, "master") == 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 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37062 cclauses, if_p);
37063 cp_parser_end_omp_structured_block (parser, save);
37064 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37065 block);
37066 OMP_PARALLEL_COMBINED (stmt) = 1;
37067 if (ret == NULL_TREE)
37068 return ret;
37069 return stmt;
37071 else if (!flag_openmp) /* flag_openmp_simd */
37073 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37074 return NULL_TREE;
37076 else if (strcmp (p, "sections") == 0)
37078 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37079 cclauses = cclauses_buf;
37081 cp_lexer_consume_token (parser->lexer);
37082 block = begin_omp_parallel ();
37083 save = cp_parser_begin_omp_structured_block (parser);
37084 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
37085 cp_parser_end_omp_structured_block (parser, save);
37086 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37087 block);
37088 OMP_PARALLEL_COMBINED (stmt) = 1;
37089 return stmt;
37092 else if (!flag_openmp) /* flag_openmp_simd */
37094 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37095 return NULL_TREE;
37098 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37099 cclauses == NULL);
37100 if (cclauses)
37102 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
37103 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
37106 block = begin_omp_parallel ();
37107 save = cp_parser_begin_omp_structured_block (parser);
37108 cp_parser_statement (parser, NULL_TREE, false, if_p);
37109 cp_parser_end_omp_structured_block (parser, save);
37110 stmt = finish_omp_parallel (clauses, block);
37111 return stmt;
37114 /* OpenMP 2.5:
37115 # pragma omp single single-clause[optseq] new-line
37116 structured-block */
37118 #define OMP_SINGLE_CLAUSE_MASK \
37119 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37124 static tree
37125 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37127 tree stmt = make_node (OMP_SINGLE);
37128 TREE_TYPE (stmt) = void_type_node;
37129 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37131 OMP_SINGLE_CLAUSES (stmt)
37132 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
37133 "#pragma omp single", pragma_tok);
37134 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37136 return add_stmt (stmt);
37139 /* OpenMP 3.0:
37140 # pragma omp task task-clause[optseq] new-line
37141 structured-block */
37143 #define OMP_TASK_CLAUSE_MASK \
37144 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37156 static tree
37157 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37159 tree clauses, block;
37160 unsigned int save;
37162 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
37163 "#pragma omp task", pragma_tok);
37164 block = begin_omp_task ();
37165 save = cp_parser_begin_omp_structured_block (parser);
37166 cp_parser_statement (parser, NULL_TREE, false, if_p);
37167 cp_parser_end_omp_structured_block (parser, save);
37168 return finish_omp_task (clauses, block);
37171 /* OpenMP 3.0:
37172 # pragma omp taskwait new-line
37174 OpenMP 5.0:
37175 # pragma omp taskwait taskwait-clause[opt] new-line */
37177 #define OMP_TASKWAIT_CLAUSE_MASK \
37178 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37180 static void
37181 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
37183 tree clauses
37184 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
37185 "#pragma omp taskwait", pragma_tok);
37187 if (clauses)
37189 tree stmt = make_node (OMP_TASK);
37190 TREE_TYPE (stmt) = void_node;
37191 OMP_TASK_CLAUSES (stmt) = clauses;
37192 OMP_TASK_BODY (stmt) = NULL_TREE;
37193 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37194 add_stmt (stmt);
37196 else
37197 finish_omp_taskwait ();
37200 /* OpenMP 3.1:
37201 # pragma omp taskyield new-line */
37203 static void
37204 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
37206 cp_parser_require_pragma_eol (parser, pragma_tok);
37207 finish_omp_taskyield ();
37210 /* OpenMP 4.0:
37211 # pragma omp taskgroup new-line
37212 structured-block
37214 OpenMP 5.0:
37215 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37217 #define OMP_TASKGROUP_CLAUSE_MASK \
37218 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37220 static tree
37221 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37223 tree clauses
37224 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
37225 "#pragma omp taskgroup", pragma_tok);
37226 return c_finish_omp_taskgroup (input_location,
37227 cp_parser_omp_structured_block (parser,
37228 if_p),
37229 clauses);
37233 /* OpenMP 2.5:
37234 # pragma omp threadprivate (variable-list) */
37236 static void
37237 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
37239 tree vars;
37241 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37242 cp_parser_require_pragma_eol (parser, pragma_tok);
37244 finish_omp_threadprivate (vars);
37247 /* OpenMP 4.0:
37248 # pragma omp cancel cancel-clause[optseq] new-line */
37250 #define OMP_CANCEL_CLAUSE_MASK \
37251 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37257 static void
37258 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
37260 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
37261 "#pragma omp cancel", pragma_tok);
37262 finish_omp_cancel (clauses);
37265 /* OpenMP 4.0:
37266 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37268 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37269 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37274 static void
37275 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
37276 enum pragma_context context)
37278 tree clauses;
37279 bool point_seen = false;
37281 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37283 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37284 const char *p = IDENTIFIER_POINTER (id);
37286 if (strcmp (p, "point") == 0)
37288 cp_lexer_consume_token (parser->lexer);
37289 point_seen = true;
37292 if (!point_seen)
37294 cp_parser_error (parser, "expected %<point%>");
37295 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37296 return;
37299 if (context != pragma_compound)
37301 if (context == pragma_stmt)
37302 error_at (pragma_tok->location,
37303 "%<#pragma %s%> may only be used in compound statements",
37304 "omp cancellation point");
37305 else
37306 cp_parser_error (parser, "expected declaration specifiers");
37307 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37308 return;
37311 clauses = cp_parser_omp_all_clauses (parser,
37312 OMP_CANCELLATION_POINT_CLAUSE_MASK,
37313 "#pragma omp cancellation point",
37314 pragma_tok);
37315 finish_omp_cancellation_point (clauses);
37318 /* OpenMP 4.0:
37319 #pragma omp distribute distribute-clause[optseq] new-line
37320 for-loop */
37322 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37323 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37329 static tree
37330 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
37331 char *p_name, omp_clause_mask mask, tree *cclauses,
37332 bool *if_p)
37334 tree clauses, sb, ret;
37335 unsigned int save;
37336 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37338 strcat (p_name, " distribute");
37339 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
37341 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37343 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37344 const char *p = IDENTIFIER_POINTER (id);
37345 bool simd = false;
37346 bool parallel = false;
37348 if (strcmp (p, "simd") == 0)
37349 simd = true;
37350 else
37351 parallel = strcmp (p, "parallel") == 0;
37352 if (parallel || simd)
37354 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37355 if (cclauses == NULL)
37356 cclauses = cclauses_buf;
37357 cp_lexer_consume_token (parser->lexer);
37358 if (!flag_openmp) /* flag_openmp_simd */
37360 if (simd)
37361 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37362 cclauses, if_p);
37363 else
37364 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37365 cclauses, if_p);
37367 sb = begin_omp_structured_block ();
37368 save = cp_parser_begin_omp_structured_block (parser);
37369 if (simd)
37370 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37371 cclauses, if_p);
37372 else
37373 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37374 cclauses, if_p);
37375 cp_parser_end_omp_structured_block (parser, save);
37376 tree body = finish_omp_structured_block (sb);
37377 if (ret == NULL)
37378 return ret;
37379 ret = make_node (OMP_DISTRIBUTE);
37380 TREE_TYPE (ret) = void_type_node;
37381 OMP_FOR_BODY (ret) = body;
37382 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37383 SET_EXPR_LOCATION (ret, loc);
37384 add_stmt (ret);
37385 return ret;
37388 if (!flag_openmp) /* flag_openmp_simd */
37390 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37391 return NULL_TREE;
37394 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37395 cclauses == NULL);
37396 if (cclauses)
37398 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
37399 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37402 keep_next_level (true);
37403 sb = begin_omp_structured_block ();
37404 save = cp_parser_begin_omp_structured_block (parser);
37406 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
37408 cp_parser_end_omp_structured_block (parser, save);
37409 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37411 return ret;
37414 /* OpenMP 4.0:
37415 # pragma omp teams teams-clause[optseq] new-line
37416 structured-block */
37418 #define OMP_TEAMS_CLAUSE_MASK \
37419 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37427 static tree
37428 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
37429 char *p_name, omp_clause_mask mask, tree *cclauses,
37430 bool *if_p)
37432 tree clauses, sb, ret;
37433 unsigned int save;
37434 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37436 strcat (p_name, " teams");
37437 mask |= OMP_TEAMS_CLAUSE_MASK;
37439 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37441 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37442 const char *p = IDENTIFIER_POINTER (id);
37443 if (strcmp (p, "distribute") == 0)
37445 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37446 if (cclauses == NULL)
37447 cclauses = cclauses_buf;
37449 cp_lexer_consume_token (parser->lexer);
37450 if (!flag_openmp) /* flag_openmp_simd */
37451 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37452 cclauses, if_p);
37453 keep_next_level (true);
37454 sb = begin_omp_structured_block ();
37455 save = cp_parser_begin_omp_structured_block (parser);
37456 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37457 cclauses, if_p);
37458 cp_parser_end_omp_structured_block (parser, save);
37459 tree body = finish_omp_structured_block (sb);
37460 if (ret == NULL)
37461 return ret;
37462 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37463 ret = make_node (OMP_TEAMS);
37464 TREE_TYPE (ret) = void_type_node;
37465 OMP_TEAMS_CLAUSES (ret) = clauses;
37466 OMP_TEAMS_BODY (ret) = body;
37467 OMP_TEAMS_COMBINED (ret) = 1;
37468 SET_EXPR_LOCATION (ret, loc);
37469 return add_stmt (ret);
37472 if (!flag_openmp) /* flag_openmp_simd */
37474 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37475 return NULL_TREE;
37478 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37479 cclauses == NULL);
37480 if (cclauses)
37482 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
37483 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37486 tree stmt = make_node (OMP_TEAMS);
37487 TREE_TYPE (stmt) = void_type_node;
37488 OMP_TEAMS_CLAUSES (stmt) = clauses;
37489 keep_next_level (true);
37490 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37491 SET_EXPR_LOCATION (stmt, loc);
37493 return add_stmt (stmt);
37496 /* OpenMP 4.0:
37497 # pragma omp target data target-data-clause[optseq] new-line
37498 structured-block */
37500 #define OMP_TARGET_DATA_CLAUSE_MASK \
37501 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
37506 static tree
37507 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37509 tree clauses
37510 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
37511 "#pragma omp target data", pragma_tok);
37512 int map_seen = 0;
37513 for (tree *pc = &clauses; *pc;)
37515 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37516 switch (OMP_CLAUSE_MAP_KIND (*pc))
37518 case GOMP_MAP_TO:
37519 case GOMP_MAP_ALWAYS_TO:
37520 case GOMP_MAP_FROM:
37521 case GOMP_MAP_ALWAYS_FROM:
37522 case GOMP_MAP_TOFROM:
37523 case GOMP_MAP_ALWAYS_TOFROM:
37524 case GOMP_MAP_ALLOC:
37525 map_seen = 3;
37526 break;
37527 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37528 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37529 case GOMP_MAP_ALWAYS_POINTER:
37530 break;
37531 default:
37532 map_seen |= 1;
37533 error_at (OMP_CLAUSE_LOCATION (*pc),
37534 "%<#pragma omp target data%> with map-type other "
37535 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37536 "on %<map%> clause");
37537 *pc = OMP_CLAUSE_CHAIN (*pc);
37538 continue;
37540 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
37541 map_seen = 3;
37542 pc = &OMP_CLAUSE_CHAIN (*pc);
37545 if (map_seen != 3)
37547 if (map_seen == 0)
37548 error_at (pragma_tok->location,
37549 "%<#pragma omp target data%> must contain at least "
37550 "one %<map%> or %<use_device_ptr%> clause");
37551 return NULL_TREE;
37554 tree stmt = make_node (OMP_TARGET_DATA);
37555 TREE_TYPE (stmt) = void_type_node;
37556 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
37558 keep_next_level (true);
37559 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37561 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37562 return add_stmt (stmt);
37565 /* OpenMP 4.5:
37566 # pragma omp target enter data target-enter-data-clause[optseq] new-line
37567 structured-block */
37569 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
37570 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37576 static tree
37577 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
37578 enum pragma_context context)
37580 bool data_seen = false;
37581 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37583 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37584 const char *p = IDENTIFIER_POINTER (id);
37586 if (strcmp (p, "data") == 0)
37588 cp_lexer_consume_token (parser->lexer);
37589 data_seen = true;
37592 if (!data_seen)
37594 cp_parser_error (parser, "expected %<data%>");
37595 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37596 return NULL_TREE;
37599 if (context == pragma_stmt)
37601 error_at (pragma_tok->location,
37602 "%<#pragma %s%> may only be used in compound statements",
37603 "omp target enter data");
37604 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37605 return NULL_TREE;
37608 tree clauses
37609 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
37610 "#pragma omp target enter data", pragma_tok);
37611 int map_seen = 0;
37612 for (tree *pc = &clauses; *pc;)
37614 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37615 switch (OMP_CLAUSE_MAP_KIND (*pc))
37617 case GOMP_MAP_TO:
37618 case GOMP_MAP_ALWAYS_TO:
37619 case GOMP_MAP_ALLOC:
37620 map_seen = 3;
37621 break;
37622 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37623 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37624 case GOMP_MAP_ALWAYS_POINTER:
37625 break;
37626 default:
37627 map_seen |= 1;
37628 error_at (OMP_CLAUSE_LOCATION (*pc),
37629 "%<#pragma omp target enter data%> with map-type other "
37630 "than %<to%> or %<alloc%> on %<map%> clause");
37631 *pc = OMP_CLAUSE_CHAIN (*pc);
37632 continue;
37634 pc = &OMP_CLAUSE_CHAIN (*pc);
37637 if (map_seen != 3)
37639 if (map_seen == 0)
37640 error_at (pragma_tok->location,
37641 "%<#pragma omp target enter data%> must contain at least "
37642 "one %<map%> clause");
37643 return NULL_TREE;
37646 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
37647 TREE_TYPE (stmt) = void_type_node;
37648 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
37649 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37650 return add_stmt (stmt);
37653 /* OpenMP 4.5:
37654 # pragma omp target exit data target-enter-data-clause[optseq] new-line
37655 structured-block */
37657 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
37658 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37664 static tree
37665 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
37666 enum pragma_context context)
37668 bool data_seen = false;
37669 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37671 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37672 const char *p = IDENTIFIER_POINTER (id);
37674 if (strcmp (p, "data") == 0)
37676 cp_lexer_consume_token (parser->lexer);
37677 data_seen = true;
37680 if (!data_seen)
37682 cp_parser_error (parser, "expected %<data%>");
37683 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37684 return NULL_TREE;
37687 if (context == pragma_stmt)
37689 error_at (pragma_tok->location,
37690 "%<#pragma %s%> may only be used in compound statements",
37691 "omp target exit data");
37692 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37693 return NULL_TREE;
37696 tree clauses
37697 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
37698 "#pragma omp target exit data", pragma_tok);
37699 int map_seen = 0;
37700 for (tree *pc = &clauses; *pc;)
37702 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37703 switch (OMP_CLAUSE_MAP_KIND (*pc))
37705 case GOMP_MAP_FROM:
37706 case GOMP_MAP_ALWAYS_FROM:
37707 case GOMP_MAP_RELEASE:
37708 case GOMP_MAP_DELETE:
37709 map_seen = 3;
37710 break;
37711 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37712 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37713 case GOMP_MAP_ALWAYS_POINTER:
37714 break;
37715 default:
37716 map_seen |= 1;
37717 error_at (OMP_CLAUSE_LOCATION (*pc),
37718 "%<#pragma omp target exit data%> with map-type other "
37719 "than %<from%>, %<release%> or %<delete%> on %<map%>"
37720 " clause");
37721 *pc = OMP_CLAUSE_CHAIN (*pc);
37722 continue;
37724 pc = &OMP_CLAUSE_CHAIN (*pc);
37727 if (map_seen != 3)
37729 if (map_seen == 0)
37730 error_at (pragma_tok->location,
37731 "%<#pragma omp target exit data%> must contain at least "
37732 "one %<map%> clause");
37733 return NULL_TREE;
37736 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
37737 TREE_TYPE (stmt) = void_type_node;
37738 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
37739 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37740 return add_stmt (stmt);
37743 /* OpenMP 4.0:
37744 # pragma omp target update target-update-clause[optseq] new-line */
37746 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
37747 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
37748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37754 static bool
37755 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
37756 enum pragma_context context)
37758 if (context == pragma_stmt)
37760 error_at (pragma_tok->location,
37761 "%<#pragma %s%> may only be used in compound statements",
37762 "omp target update");
37763 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37764 return false;
37767 tree clauses
37768 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
37769 "#pragma omp target update", pragma_tok);
37770 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
37771 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
37773 error_at (pragma_tok->location,
37774 "%<#pragma omp target update%> must contain at least one "
37775 "%<from%> or %<to%> clauses");
37776 return false;
37779 tree stmt = make_node (OMP_TARGET_UPDATE);
37780 TREE_TYPE (stmt) = void_type_node;
37781 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
37782 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37783 add_stmt (stmt);
37784 return false;
37787 /* OpenMP 4.0:
37788 # pragma omp target target-clause[optseq] new-line
37789 structured-block */
37791 #define OMP_TARGET_CLAUSE_MASK \
37792 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
37800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
37802 static bool
37803 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
37804 enum pragma_context context, bool *if_p)
37806 tree *pc = NULL, stmt;
37808 if (flag_openmp)
37809 omp_requires_mask
37810 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
37812 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37814 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37815 const char *p = IDENTIFIER_POINTER (id);
37816 enum tree_code ccode = ERROR_MARK;
37818 if (strcmp (p, "teams") == 0)
37819 ccode = OMP_TEAMS;
37820 else if (strcmp (p, "parallel") == 0)
37821 ccode = OMP_PARALLEL;
37822 else if (strcmp (p, "simd") == 0)
37823 ccode = OMP_SIMD;
37824 if (ccode != ERROR_MARK)
37826 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
37827 char p_name[sizeof ("#pragma omp target teams distribute "
37828 "parallel for simd")];
37830 cp_lexer_consume_token (parser->lexer);
37831 strcpy (p_name, "#pragma omp target");
37832 if (!flag_openmp) /* flag_openmp_simd */
37834 tree stmt;
37835 switch (ccode)
37837 case OMP_TEAMS:
37838 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
37839 OMP_TARGET_CLAUSE_MASK,
37840 cclauses, if_p);
37841 break;
37842 case OMP_PARALLEL:
37843 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
37844 OMP_TARGET_CLAUSE_MASK,
37845 cclauses, if_p);
37846 break;
37847 case OMP_SIMD:
37848 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
37849 OMP_TARGET_CLAUSE_MASK,
37850 cclauses, if_p);
37851 break;
37852 default:
37853 gcc_unreachable ();
37855 return stmt != NULL_TREE;
37857 keep_next_level (true);
37858 tree sb = begin_omp_structured_block (), ret;
37859 unsigned save = cp_parser_begin_omp_structured_block (parser);
37860 switch (ccode)
37862 case OMP_TEAMS:
37863 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
37864 OMP_TARGET_CLAUSE_MASK, cclauses,
37865 if_p);
37866 break;
37867 case OMP_PARALLEL:
37868 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
37869 OMP_TARGET_CLAUSE_MASK, cclauses,
37870 if_p);
37871 break;
37872 case OMP_SIMD:
37873 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
37874 OMP_TARGET_CLAUSE_MASK, cclauses,
37875 if_p);
37876 break;
37877 default:
37878 gcc_unreachable ();
37880 cp_parser_end_omp_structured_block (parser, save);
37881 tree body = finish_omp_structured_block (sb);
37882 if (ret == NULL_TREE)
37883 return false;
37884 if (ccode == OMP_TEAMS && !processing_template_decl)
37886 /* For combined target teams, ensure the num_teams and
37887 thread_limit clause expressions are evaluated on the host,
37888 before entering the target construct. */
37889 tree c;
37890 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37891 c; c = OMP_CLAUSE_CHAIN (c))
37892 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
37893 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
37894 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
37896 tree expr = OMP_CLAUSE_OPERAND (c, 0);
37897 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
37898 if (expr == error_mark_node)
37899 continue;
37900 tree tmp = TARGET_EXPR_SLOT (expr);
37901 add_stmt (expr);
37902 OMP_CLAUSE_OPERAND (c, 0) = expr;
37903 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
37904 OMP_CLAUSE_FIRSTPRIVATE);
37905 OMP_CLAUSE_DECL (tc) = tmp;
37906 OMP_CLAUSE_CHAIN (tc)
37907 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
37908 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
37911 tree stmt = make_node (OMP_TARGET);
37912 TREE_TYPE (stmt) = void_type_node;
37913 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
37914 OMP_TARGET_BODY (stmt) = body;
37915 OMP_TARGET_COMBINED (stmt) = 1;
37916 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37917 add_stmt (stmt);
37918 pc = &OMP_TARGET_CLAUSES (stmt);
37919 goto check_clauses;
37921 else if (!flag_openmp) /* flag_openmp_simd */
37923 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37924 return false;
37926 else if (strcmp (p, "data") == 0)
37928 cp_lexer_consume_token (parser->lexer);
37929 cp_parser_omp_target_data (parser, pragma_tok, if_p);
37930 return true;
37932 else if (strcmp (p, "enter") == 0)
37934 cp_lexer_consume_token (parser->lexer);
37935 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
37936 return false;
37938 else if (strcmp (p, "exit") == 0)
37940 cp_lexer_consume_token (parser->lexer);
37941 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
37942 return false;
37944 else if (strcmp (p, "update") == 0)
37946 cp_lexer_consume_token (parser->lexer);
37947 return cp_parser_omp_target_update (parser, pragma_tok, context);
37950 if (!flag_openmp) /* flag_openmp_simd */
37952 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37953 return false;
37956 stmt = make_node (OMP_TARGET);
37957 TREE_TYPE (stmt) = void_type_node;
37959 OMP_TARGET_CLAUSES (stmt)
37960 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
37961 "#pragma omp target", pragma_tok);
37962 pc = &OMP_TARGET_CLAUSES (stmt);
37963 keep_next_level (true);
37964 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37966 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37967 add_stmt (stmt);
37969 check_clauses:
37970 while (*pc)
37972 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37973 switch (OMP_CLAUSE_MAP_KIND (*pc))
37975 case GOMP_MAP_TO:
37976 case GOMP_MAP_ALWAYS_TO:
37977 case GOMP_MAP_FROM:
37978 case GOMP_MAP_ALWAYS_FROM:
37979 case GOMP_MAP_TOFROM:
37980 case GOMP_MAP_ALWAYS_TOFROM:
37981 case GOMP_MAP_ALLOC:
37982 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37983 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37984 case GOMP_MAP_ALWAYS_POINTER:
37985 break;
37986 default:
37987 error_at (OMP_CLAUSE_LOCATION (*pc),
37988 "%<#pragma omp target%> with map-type other "
37989 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37990 "on %<map%> clause");
37991 *pc = OMP_CLAUSE_CHAIN (*pc);
37992 continue;
37994 pc = &OMP_CLAUSE_CHAIN (*pc);
37996 return true;
37999 /* OpenACC 2.0:
38000 # pragma acc cache (variable-list) new-line
38003 static tree
38004 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
38006 tree stmt, clauses;
38008 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
38009 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38011 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
38013 stmt = make_node (OACC_CACHE);
38014 TREE_TYPE (stmt) = void_type_node;
38015 OACC_CACHE_CLAUSES (stmt) = clauses;
38016 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38017 add_stmt (stmt);
38019 return stmt;
38022 /* OpenACC 2.0:
38023 # pragma acc data oacc-data-clause[optseq] new-line
38024 structured-block */
38026 #define OACC_DATA_CLAUSE_MASK \
38027 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38035 static tree
38036 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38038 tree stmt, clauses, block;
38039 unsigned int save;
38041 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
38042 "#pragma acc data", pragma_tok);
38044 block = begin_omp_parallel ();
38045 save = cp_parser_begin_omp_structured_block (parser);
38046 cp_parser_statement (parser, NULL_TREE, false, if_p);
38047 cp_parser_end_omp_structured_block (parser, save);
38048 stmt = finish_oacc_data (clauses, block);
38049 return stmt;
38052 /* OpenACC 2.0:
38053 # pragma acc host_data <clauses> new-line
38054 structured-block */
38056 #define OACC_HOST_DATA_CLAUSE_MASK \
38057 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38059 static tree
38060 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38062 tree stmt, clauses, block;
38063 unsigned int save;
38065 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
38066 "#pragma acc host_data", pragma_tok);
38068 block = begin_omp_parallel ();
38069 save = cp_parser_begin_omp_structured_block (parser);
38070 cp_parser_statement (parser, NULL_TREE, false, if_p);
38071 cp_parser_end_omp_structured_block (parser, save);
38072 stmt = finish_oacc_host_data (clauses, block);
38073 return stmt;
38076 /* OpenACC 2.0:
38077 # pragma acc declare oacc-data-clause[optseq] new-line
38080 #define OACC_DECLARE_CLAUSE_MASK \
38081 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38090 static tree
38091 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
38093 tree clauses, stmt;
38094 bool error = false;
38096 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
38097 "#pragma acc declare", pragma_tok, true);
38100 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38102 error_at (pragma_tok->location,
38103 "no valid clauses specified in %<#pragma acc declare%>");
38104 return NULL_TREE;
38107 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
38109 location_t loc = OMP_CLAUSE_LOCATION (t);
38110 tree decl = OMP_CLAUSE_DECL (t);
38111 if (!DECL_P (decl))
38113 error_at (loc, "array section in %<#pragma acc declare%>");
38114 error = true;
38115 continue;
38117 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
38118 switch (OMP_CLAUSE_MAP_KIND (t))
38120 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38121 case GOMP_MAP_ALLOC:
38122 case GOMP_MAP_TO:
38123 case GOMP_MAP_FORCE_DEVICEPTR:
38124 case GOMP_MAP_DEVICE_RESIDENT:
38125 break;
38127 case GOMP_MAP_LINK:
38128 if (!global_bindings_p ()
38129 && (TREE_STATIC (decl)
38130 || !DECL_EXTERNAL (decl)))
38132 error_at (loc,
38133 "%qD must be a global variable in "
38134 "%<#pragma acc declare link%>",
38135 decl);
38136 error = true;
38137 continue;
38139 break;
38141 default:
38142 if (global_bindings_p ())
38144 error_at (loc, "invalid OpenACC clause at file scope");
38145 error = true;
38146 continue;
38148 if (DECL_EXTERNAL (decl))
38150 error_at (loc,
38151 "invalid use of %<extern%> variable %qD "
38152 "in %<#pragma acc declare%>", decl);
38153 error = true;
38154 continue;
38156 else if (TREE_PUBLIC (decl))
38158 error_at (loc,
38159 "invalid use of %<global%> variable %qD "
38160 "in %<#pragma acc declare%>", decl);
38161 error = true;
38162 continue;
38164 break;
38167 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
38168 || lookup_attribute ("omp declare target link",
38169 DECL_ATTRIBUTES (decl)))
38171 error_at (loc, "variable %qD used more than once with "
38172 "%<#pragma acc declare%>", decl);
38173 error = true;
38174 continue;
38177 if (!error)
38179 tree id;
38181 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
38182 id = get_identifier ("omp declare target link");
38183 else
38184 id = get_identifier ("omp declare target");
38186 DECL_ATTRIBUTES (decl)
38187 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
38188 if (global_bindings_p ())
38190 symtab_node *node = symtab_node::get (decl);
38191 if (node != NULL)
38193 node->offloadable = 1;
38194 if (ENABLE_OFFLOADING)
38196 g->have_offload = true;
38197 if (is_a <varpool_node *> (node))
38198 vec_safe_push (offload_vars, decl);
38205 if (error || global_bindings_p ())
38206 return NULL_TREE;
38208 stmt = make_node (OACC_DECLARE);
38209 TREE_TYPE (stmt) = void_type_node;
38210 OACC_DECLARE_CLAUSES (stmt) = clauses;
38211 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38213 add_stmt (stmt);
38215 return NULL_TREE;
38218 /* OpenACC 2.0:
38219 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38223 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38225 LOC is the location of the #pragma token.
38228 #define OACC_ENTER_DATA_CLAUSE_MASK \
38229 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38235 #define OACC_EXIT_DATA_CLAUSE_MASK \
38236 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38243 static tree
38244 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
38245 bool enter)
38247 location_t loc = pragma_tok->location;
38248 tree stmt, clauses;
38249 const char *p = "";
38251 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38252 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38254 if (strcmp (p, "data") != 0)
38256 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
38257 enter ? "enter" : "exit");
38258 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38259 return NULL_TREE;
38262 cp_lexer_consume_token (parser->lexer);
38264 if (enter)
38265 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
38266 "#pragma acc enter data", pragma_tok);
38267 else
38268 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
38269 "#pragma acc exit data", pragma_tok);
38271 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38273 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
38274 enter ? "enter" : "exit");
38275 return NULL_TREE;
38278 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
38279 TREE_TYPE (stmt) = void_type_node;
38280 OMP_STANDALONE_CLAUSES (stmt) = clauses;
38281 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38282 add_stmt (stmt);
38283 return stmt;
38286 /* OpenACC 2.0:
38287 # pragma acc loop oacc-loop-clause[optseq] new-line
38288 structured-block */
38290 #define OACC_LOOP_CLAUSE_MASK \
38291 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38302 static tree
38303 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
38304 omp_clause_mask mask, tree *cclauses, bool *if_p)
38306 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
38308 strcat (p_name, " loop");
38309 mask |= OACC_LOOP_CLAUSE_MASK;
38311 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
38312 cclauses == NULL);
38313 if (cclauses)
38315 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
38316 if (*cclauses)
38317 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
38318 if (clauses)
38319 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38322 tree block = begin_omp_structured_block ();
38323 int save = cp_parser_begin_omp_structured_block (parser);
38324 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
38325 cp_parser_end_omp_structured_block (parser, save);
38326 add_stmt (finish_omp_structured_block (block));
38328 return stmt;
38331 /* OpenACC 2.0:
38332 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38333 structured-block
38337 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38338 structured-block
38341 #define OACC_KERNELS_CLAUSE_MASK \
38342 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38356 #define OACC_PARALLEL_CLAUSE_MASK \
38357 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38374 static tree
38375 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
38376 char *p_name, bool *if_p)
38378 omp_clause_mask mask;
38379 enum tree_code code;
38380 switch (cp_parser_pragma_kind (pragma_tok))
38382 case PRAGMA_OACC_KERNELS:
38383 strcat (p_name, " kernels");
38384 mask = OACC_KERNELS_CLAUSE_MASK;
38385 code = OACC_KERNELS;
38386 break;
38387 case PRAGMA_OACC_PARALLEL:
38388 strcat (p_name, " parallel");
38389 mask = OACC_PARALLEL_CLAUSE_MASK;
38390 code = OACC_PARALLEL;
38391 break;
38392 default:
38393 gcc_unreachable ();
38396 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38398 const char *p
38399 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38400 if (strcmp (p, "loop") == 0)
38402 cp_lexer_consume_token (parser->lexer);
38403 tree block = begin_omp_parallel ();
38404 tree clauses;
38405 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
38406 if_p);
38407 return finish_omp_construct (code, block, clauses);
38411 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
38413 tree block = begin_omp_parallel ();
38414 unsigned int save = cp_parser_begin_omp_structured_block (parser);
38415 cp_parser_statement (parser, NULL_TREE, false, if_p);
38416 cp_parser_end_omp_structured_block (parser, save);
38417 return finish_omp_construct (code, block, clauses);
38420 /* OpenACC 2.0:
38421 # pragma acc update oacc-update-clause[optseq] new-line
38424 #define OACC_UPDATE_CLAUSE_MASK \
38425 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38432 static tree
38433 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
38435 tree stmt, clauses;
38437 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
38438 "#pragma acc update", pragma_tok);
38440 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38442 error_at (pragma_tok->location,
38443 "%<#pragma acc update%> must contain at least one "
38444 "%<device%> or %<host%> or %<self%> clause");
38445 return NULL_TREE;
38448 stmt = make_node (OACC_UPDATE);
38449 TREE_TYPE (stmt) = void_type_node;
38450 OACC_UPDATE_CLAUSES (stmt) = clauses;
38451 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38452 add_stmt (stmt);
38453 return stmt;
38456 /* OpenACC 2.0:
38457 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38459 LOC is the location of the #pragma token.
38462 #define OACC_WAIT_CLAUSE_MASK \
38463 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
38465 static tree
38466 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
38468 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
38469 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38471 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38472 list = cp_parser_oacc_wait_list (parser, loc, list);
38474 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
38475 "#pragma acc wait", pragma_tok);
38477 stmt = c_finish_oacc_wait (loc, list, clauses);
38478 stmt = finish_expr_stmt (stmt);
38480 return stmt;
38483 /* OpenMP 4.0:
38484 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
38486 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
38487 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
38491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
38492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
38494 static void
38495 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
38496 enum pragma_context context)
38498 bool first_p = parser->omp_declare_simd == NULL;
38499 cp_omp_declare_simd_data data;
38500 if (first_p)
38502 data.error_seen = false;
38503 data.fndecl_seen = false;
38504 data.tokens = vNULL;
38505 data.clauses = NULL_TREE;
38506 /* It is safe to take the address of a local variable; it will only be
38507 used while this scope is live. */
38508 parser->omp_declare_simd = &data;
38511 /* Store away all pragma tokens. */
38512 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38513 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38514 cp_lexer_consume_token (parser->lexer);
38515 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38516 parser->omp_declare_simd->error_seen = true;
38517 cp_parser_require_pragma_eol (parser, pragma_tok);
38518 struct cp_token_cache *cp
38519 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38520 parser->omp_declare_simd->tokens.safe_push (cp);
38522 if (first_p)
38524 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38525 cp_parser_pragma (parser, context, NULL);
38526 switch (context)
38528 case pragma_external:
38529 cp_parser_declaration (parser);
38530 break;
38531 case pragma_member:
38532 cp_parser_member_declaration (parser);
38533 break;
38534 case pragma_objc_icode:
38535 cp_parser_block_declaration (parser, /*statement_p=*/false);
38536 break;
38537 default:
38538 cp_parser_declaration_statement (parser);
38539 break;
38541 if (parser->omp_declare_simd
38542 && !parser->omp_declare_simd->error_seen
38543 && !parser->omp_declare_simd->fndecl_seen)
38544 error_at (pragma_tok->location,
38545 "%<#pragma omp declare simd%> not immediately followed by "
38546 "function declaration or definition");
38547 data.tokens.release ();
38548 parser->omp_declare_simd = NULL;
38552 /* Finalize #pragma omp declare simd clauses after direct declarator has
38553 been parsed, and put that into "omp declare simd" attribute. */
38555 static tree
38556 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
38558 struct cp_token_cache *ce;
38559 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
38560 int i;
38562 if (!data->error_seen && data->fndecl_seen)
38564 error ("%<#pragma omp declare simd%> not immediately followed by "
38565 "a single function declaration or definition");
38566 data->error_seen = true;
38568 if (data->error_seen)
38569 return attrs;
38571 FOR_EACH_VEC_ELT (data->tokens, i, ce)
38573 tree c, cl;
38575 cp_parser_push_lexer_for_tokens (parser, ce);
38576 parser->lexer->in_pragma = true;
38577 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38578 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38579 cp_lexer_consume_token (parser->lexer);
38580 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
38581 "#pragma omp declare simd", pragma_tok);
38582 cp_parser_pop_lexer (parser);
38583 if (cl)
38584 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
38585 c = build_tree_list (get_identifier ("omp declare simd"), cl);
38586 TREE_CHAIN (c) = attrs;
38587 if (processing_template_decl)
38588 ATTR_IS_DEPENDENT (c) = 1;
38589 attrs = c;
38592 data->fndecl_seen = true;
38593 return attrs;
38597 /* OpenMP 4.0:
38598 # pragma omp declare target new-line
38599 declarations and definitions
38600 # pragma omp end declare target new-line
38602 OpenMP 4.5:
38603 # pragma omp declare target ( extended-list ) new-line
38605 # pragma omp declare target declare-target-clauses[seq] new-line */
38607 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
38608 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
38611 static void
38612 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
38614 tree clauses = NULL_TREE;
38615 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38616 clauses
38617 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
38618 "#pragma omp declare target", pragma_tok);
38619 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38621 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
38622 clauses);
38623 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
38624 cp_parser_require_pragma_eol (parser, pragma_tok);
38626 else
38628 cp_parser_require_pragma_eol (parser, pragma_tok);
38629 scope_chain->omp_declare_target_attribute++;
38630 return;
38632 if (scope_chain->omp_declare_target_attribute)
38633 error_at (pragma_tok->location,
38634 "%<#pragma omp declare target%> with clauses in between "
38635 "%<#pragma omp declare target%> without clauses and "
38636 "%<#pragma omp end declare target%>");
38637 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
38639 tree t = OMP_CLAUSE_DECL (c), id;
38640 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
38641 tree at2 = lookup_attribute ("omp declare target link",
38642 DECL_ATTRIBUTES (t));
38643 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
38645 id = get_identifier ("omp declare target link");
38646 std::swap (at1, at2);
38648 else
38649 id = get_identifier ("omp declare target");
38650 if (at2)
38652 error_at (OMP_CLAUSE_LOCATION (c),
38653 "%qD specified both in declare target %<link%> and %<to%>"
38654 " clauses", t);
38655 continue;
38657 if (!at1)
38659 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
38660 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
38661 continue;
38663 symtab_node *node = symtab_node::get (t);
38664 if (node != NULL)
38666 node->offloadable = 1;
38667 if (ENABLE_OFFLOADING)
38669 g->have_offload = true;
38670 if (is_a <varpool_node *> (node))
38671 vec_safe_push (offload_vars, t);
38678 static void
38679 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
38681 const char *p = "";
38682 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38684 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38685 p = IDENTIFIER_POINTER (id);
38687 if (strcmp (p, "declare") == 0)
38689 cp_lexer_consume_token (parser->lexer);
38690 p = "";
38691 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38693 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38694 p = IDENTIFIER_POINTER (id);
38696 if (strcmp (p, "target") == 0)
38697 cp_lexer_consume_token (parser->lexer);
38698 else
38700 cp_parser_error (parser, "expected %<target%>");
38701 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38702 return;
38705 else
38707 cp_parser_error (parser, "expected %<declare%>");
38708 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38709 return;
38711 cp_parser_require_pragma_eol (parser, pragma_tok);
38712 if (!scope_chain->omp_declare_target_attribute)
38713 error_at (pragma_tok->location,
38714 "%<#pragma omp end declare target%> without corresponding "
38715 "%<#pragma omp declare target%>");
38716 else
38717 scope_chain->omp_declare_target_attribute--;
38720 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
38721 expression and optional initializer clause of
38722 #pragma omp declare reduction. We store the expression(s) as
38723 either 3, 6 or 7 special statements inside of the artificial function's
38724 body. The first two statements are DECL_EXPRs for the artificial
38725 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
38726 expression that uses those variables.
38727 If there was any INITIALIZER clause, this is followed by further statements,
38728 the fourth and fifth statements are DECL_EXPRs for the artificial
38729 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
38730 constructor variant (first token after open paren is not omp_priv),
38731 then the sixth statement is a statement with the function call expression
38732 that uses the OMP_PRIV and optionally OMP_ORIG variable.
38733 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
38734 to initialize the OMP_PRIV artificial variable and there is seventh
38735 statement, a DECL_EXPR of the OMP_PRIV statement again. */
38737 static bool
38738 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
38740 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
38741 gcc_assert (TYPE_REF_P (type));
38742 type = TREE_TYPE (type);
38743 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
38744 DECL_ARTIFICIAL (omp_out) = 1;
38745 pushdecl (omp_out);
38746 add_decl_expr (omp_out);
38747 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
38748 DECL_ARTIFICIAL (omp_in) = 1;
38749 pushdecl (omp_in);
38750 add_decl_expr (omp_in);
38751 tree combiner;
38752 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
38754 keep_next_level (true);
38755 tree block = begin_omp_structured_block ();
38756 combiner = cp_parser_expression (parser);
38757 finish_expr_stmt (combiner);
38758 block = finish_omp_structured_block (block);
38759 add_stmt (block);
38761 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38762 return false;
38764 const char *p = "";
38765 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38767 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38768 p = IDENTIFIER_POINTER (id);
38771 if (strcmp (p, "initializer") == 0)
38773 cp_lexer_consume_token (parser->lexer);
38774 matching_parens parens;
38775 if (!parens.require_open (parser))
38776 return false;
38778 p = "";
38779 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38781 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38782 p = IDENTIFIER_POINTER (id);
38785 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
38786 DECL_ARTIFICIAL (omp_priv) = 1;
38787 pushdecl (omp_priv);
38788 add_decl_expr (omp_priv);
38789 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
38790 DECL_ARTIFICIAL (omp_orig) = 1;
38791 pushdecl (omp_orig);
38792 add_decl_expr (omp_orig);
38794 keep_next_level (true);
38795 block = begin_omp_structured_block ();
38797 bool ctor = false;
38798 if (strcmp (p, "omp_priv") == 0)
38800 bool is_direct_init, is_non_constant_init;
38801 ctor = true;
38802 cp_lexer_consume_token (parser->lexer);
38803 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
38804 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
38805 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
38806 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
38807 == CPP_CLOSE_PAREN
38808 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
38809 == CPP_CLOSE_PAREN))
38811 finish_omp_structured_block (block);
38812 error ("invalid initializer clause");
38813 return false;
38815 initializer = cp_parser_initializer (parser, &is_direct_init,
38816 &is_non_constant_init);
38817 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
38818 NULL_TREE, LOOKUP_ONLYCONVERTING);
38820 else
38822 cp_parser_parse_tentatively (parser);
38823 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
38824 /*check_dependency_p=*/true,
38825 /*template_p=*/NULL,
38826 /*declarator_p=*/false,
38827 /*optional_p=*/false);
38828 vec<tree, va_gc> *args;
38829 if (fn_name == error_mark_node
38830 || cp_parser_error_occurred (parser)
38831 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
38832 || ((args = cp_parser_parenthesized_expression_list
38833 (parser, non_attr, /*cast_p=*/false,
38834 /*allow_expansion_p=*/true,
38835 /*non_constant_p=*/NULL)),
38836 cp_parser_error_occurred (parser)))
38838 finish_omp_structured_block (block);
38839 cp_parser_abort_tentative_parse (parser);
38840 cp_parser_error (parser, "expected id-expression (arguments)");
38841 return false;
38843 unsigned int i;
38844 tree arg;
38845 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
38846 if (arg == omp_priv
38847 || (TREE_CODE (arg) == ADDR_EXPR
38848 && TREE_OPERAND (arg, 0) == omp_priv))
38849 break;
38850 cp_parser_abort_tentative_parse (parser);
38851 if (arg == NULL_TREE)
38852 error ("one of the initializer call arguments should be %<omp_priv%>"
38853 " or %<&omp_priv%>");
38854 initializer = cp_parser_postfix_expression (parser, false, false, false,
38855 false, NULL);
38856 finish_expr_stmt (initializer);
38859 block = finish_omp_structured_block (block);
38860 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
38861 add_stmt (block);
38863 if (ctor)
38864 add_decl_expr (omp_orig);
38866 if (!parens.require_close (parser))
38867 return false;
38870 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
38871 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
38872 UNKNOWN_LOCATION);
38874 return true;
38877 /* OpenMP 4.0
38878 #pragma omp declare reduction (reduction-id : typename-list : expression) \
38879 initializer-clause[opt] new-line
38881 initializer-clause:
38882 initializer (omp_priv initializer)
38883 initializer (function-name (argument-list)) */
38885 static void
38886 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
38887 enum pragma_context)
38889 auto_vec<tree> types;
38890 enum tree_code reduc_code = ERROR_MARK;
38891 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
38892 unsigned int i;
38893 cp_token *first_token;
38894 cp_token_cache *cp;
38895 int errs;
38896 void *p;
38898 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
38899 p = obstack_alloc (&declarator_obstack, 0);
38901 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38902 goto fail;
38904 switch (cp_lexer_peek_token (parser->lexer)->type)
38906 case CPP_PLUS:
38907 reduc_code = PLUS_EXPR;
38908 break;
38909 case CPP_MULT:
38910 reduc_code = MULT_EXPR;
38911 break;
38912 case CPP_MINUS:
38913 reduc_code = MINUS_EXPR;
38914 break;
38915 case CPP_AND:
38916 reduc_code = BIT_AND_EXPR;
38917 break;
38918 case CPP_XOR:
38919 reduc_code = BIT_XOR_EXPR;
38920 break;
38921 case CPP_OR:
38922 reduc_code = BIT_IOR_EXPR;
38923 break;
38924 case CPP_AND_AND:
38925 reduc_code = TRUTH_ANDIF_EXPR;
38926 break;
38927 case CPP_OR_OR:
38928 reduc_code = TRUTH_ORIF_EXPR;
38929 break;
38930 case CPP_NAME:
38931 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
38932 break;
38933 default:
38934 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
38935 "%<|%>, %<&&%>, %<||%> or identifier");
38936 goto fail;
38939 if (reduc_code != ERROR_MARK)
38940 cp_lexer_consume_token (parser->lexer);
38942 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
38943 if (reduc_id == error_mark_node)
38944 goto fail;
38946 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38947 goto fail;
38949 /* Types may not be defined in declare reduction type list. */
38950 const char *saved_message;
38951 saved_message = parser->type_definition_forbidden_message;
38952 parser->type_definition_forbidden_message
38953 = G_("types may not be defined in declare reduction type list");
38954 bool saved_colon_corrects_to_scope_p;
38955 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38956 parser->colon_corrects_to_scope_p = false;
38957 bool saved_colon_doesnt_start_class_def_p;
38958 saved_colon_doesnt_start_class_def_p
38959 = parser->colon_doesnt_start_class_def_p;
38960 parser->colon_doesnt_start_class_def_p = true;
38962 while (true)
38964 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38965 type = cp_parser_type_id (parser);
38966 if (type == error_mark_node)
38968 else if (ARITHMETIC_TYPE_P (type)
38969 && (orig_reduc_id == NULL_TREE
38970 || (TREE_CODE (type) != COMPLEX_TYPE
38971 && (id_equal (orig_reduc_id, "min")
38972 || id_equal (orig_reduc_id, "max")))))
38973 error_at (loc, "predeclared arithmetic type %qT in "
38974 "%<#pragma omp declare reduction%>", type);
38975 else if (TREE_CODE (type) == FUNCTION_TYPE
38976 || TREE_CODE (type) == METHOD_TYPE
38977 || TREE_CODE (type) == ARRAY_TYPE)
38978 error_at (loc, "function or array type %qT in "
38979 "%<#pragma omp declare reduction%>", type);
38980 else if (TYPE_REF_P (type))
38981 error_at (loc, "reference type %qT in "
38982 "%<#pragma omp declare reduction%>", type);
38983 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
38984 error_at (loc, "const, volatile or __restrict qualified type %qT in "
38985 "%<#pragma omp declare reduction%>", type);
38986 else
38987 types.safe_push (type);
38989 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38990 cp_lexer_consume_token (parser->lexer);
38991 else
38992 break;
38995 /* Restore the saved message. */
38996 parser->type_definition_forbidden_message = saved_message;
38997 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38998 parser->colon_doesnt_start_class_def_p
38999 = saved_colon_doesnt_start_class_def_p;
39001 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
39002 || types.is_empty ())
39004 fail:
39005 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39006 goto done;
39009 first_token = cp_lexer_peek_token (parser->lexer);
39010 cp = NULL;
39011 errs = errorcount;
39012 FOR_EACH_VEC_ELT (types, i, type)
39014 tree fntype
39015 = build_function_type_list (void_type_node,
39016 cp_build_reference_type (type, false),
39017 NULL_TREE);
39018 tree this_reduc_id = reduc_id;
39019 if (!dependent_type_p (type))
39020 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
39021 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
39022 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
39023 DECL_ARTIFICIAL (fndecl) = 1;
39024 DECL_EXTERNAL (fndecl) = 1;
39025 DECL_DECLARED_INLINE_P (fndecl) = 1;
39026 DECL_IGNORED_P (fndecl) = 1;
39027 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
39028 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
39029 DECL_ATTRIBUTES (fndecl)
39030 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
39031 DECL_ATTRIBUTES (fndecl));
39032 if (processing_template_decl)
39033 fndecl = push_template_decl (fndecl);
39034 bool block_scope = false;
39035 tree block = NULL_TREE;
39036 if (current_function_decl)
39038 block_scope = true;
39039 DECL_CONTEXT (fndecl) = global_namespace;
39040 if (!processing_template_decl)
39041 pushdecl (fndecl);
39043 else if (current_class_type)
39045 if (cp == NULL)
39047 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39048 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39049 cp_lexer_consume_token (parser->lexer);
39050 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39051 goto fail;
39052 cp = cp_token_cache_new (first_token,
39053 cp_lexer_peek_nth_token (parser->lexer,
39054 2));
39056 DECL_STATIC_FUNCTION_P (fndecl) = 1;
39057 finish_member_declaration (fndecl);
39058 DECL_PENDING_INLINE_INFO (fndecl) = cp;
39059 DECL_PENDING_INLINE_P (fndecl) = 1;
39060 vec_safe_push (unparsed_funs_with_definitions, fndecl);
39061 continue;
39063 else
39065 DECL_CONTEXT (fndecl) = current_namespace;
39066 pushdecl (fndecl);
39068 if (!block_scope)
39069 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
39070 else
39071 block = begin_omp_structured_block ();
39072 if (cp)
39074 cp_parser_push_lexer_for_tokens (parser, cp);
39075 parser->lexer->in_pragma = true;
39077 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
39079 if (!block_scope)
39080 finish_function (/*inline_p=*/false);
39081 else
39082 DECL_CONTEXT (fndecl) = current_function_decl;
39083 if (cp)
39084 cp_parser_pop_lexer (parser);
39085 goto fail;
39087 if (cp)
39088 cp_parser_pop_lexer (parser);
39089 if (!block_scope)
39090 finish_function (/*inline_p=*/false);
39091 else
39093 DECL_CONTEXT (fndecl) = current_function_decl;
39094 block = finish_omp_structured_block (block);
39095 if (TREE_CODE (block) == BIND_EXPR)
39096 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
39097 else if (TREE_CODE (block) == STATEMENT_LIST)
39098 DECL_SAVED_TREE (fndecl) = block;
39099 if (processing_template_decl)
39100 add_decl_expr (fndecl);
39102 cp_check_omp_declare_reduction (fndecl);
39103 if (cp == NULL && types.length () > 1)
39104 cp = cp_token_cache_new (first_token,
39105 cp_lexer_peek_nth_token (parser->lexer, 2));
39106 if (errs != errorcount)
39107 break;
39110 cp_parser_require_pragma_eol (parser, pragma_tok);
39112 done:
39113 /* Free any declarators allocated. */
39114 obstack_free (&declarator_obstack, p);
39117 /* OpenMP 4.0
39118 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39119 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39120 initializer-clause[opt] new-line
39121 #pragma omp declare target new-line */
39123 static bool
39124 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
39125 enum pragma_context context)
39127 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39129 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39130 const char *p = IDENTIFIER_POINTER (id);
39132 if (strcmp (p, "simd") == 0)
39134 cp_lexer_consume_token (parser->lexer);
39135 cp_parser_omp_declare_simd (parser, pragma_tok,
39136 context);
39137 return true;
39139 cp_ensure_no_omp_declare_simd (parser);
39140 if (strcmp (p, "reduction") == 0)
39142 cp_lexer_consume_token (parser->lexer);
39143 cp_parser_omp_declare_reduction (parser, pragma_tok,
39144 context);
39145 return false;
39147 if (!flag_openmp) /* flag_openmp_simd */
39149 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39150 return false;
39152 if (strcmp (p, "target") == 0)
39154 cp_lexer_consume_token (parser->lexer);
39155 cp_parser_omp_declare_target (parser, pragma_tok);
39156 return false;
39159 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
39160 "or %<target%>");
39161 cp_parser_require_pragma_eol (parser, pragma_tok);
39162 return false;
39165 /* OpenMP 5.0
39166 #pragma omp requires clauses[optseq] new-line */
39168 static bool
39169 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
39171 bool first = true;
39172 enum omp_requires new_req = (enum omp_requires) 0;
39174 location_t loc = pragma_tok->location;
39175 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39177 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39178 cp_lexer_consume_token (parser->lexer);
39180 first = false;
39182 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39184 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39185 const char *p = IDENTIFIER_POINTER (id);
39186 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
39187 enum omp_requires this_req = (enum omp_requires) 0;
39189 if (!strcmp (p, "unified_address"))
39190 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
39191 else if (!strcmp (p, "unified_shared_memory"))
39192 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
39193 else if (!strcmp (p, "dynamic_allocators"))
39194 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
39195 else if (!strcmp (p, "reverse_offload"))
39196 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
39197 else if (!strcmp (p, "atomic_default_mem_order"))
39199 cp_lexer_consume_token (parser->lexer);
39201 matching_parens parens;
39202 if (parens.require_open (parser))
39204 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39206 id = cp_lexer_peek_token (parser->lexer)->u.value;
39207 p = IDENTIFIER_POINTER (id);
39209 if (!strcmp (p, "seq_cst"))
39210 this_req
39211 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
39212 else if (!strcmp (p, "relaxed"))
39213 this_req
39214 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
39215 else if (!strcmp (p, "acq_rel"))
39216 this_req
39217 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
39219 if (this_req == 0)
39221 error_at (cp_lexer_peek_token (parser->lexer)->location,
39222 "expected %<seq_cst%>, %<relaxed%> or "
39223 "%<acq_rel%>");
39224 if (cp_lexer_nth_token_is (parser->lexer, 2,
39225 CPP_CLOSE_PAREN))
39226 cp_lexer_consume_token (parser->lexer);
39228 else
39229 cp_lexer_consume_token (parser->lexer);
39231 if (!parens.require_close (parser))
39232 cp_parser_skip_to_closing_parenthesis (parser,
39233 /*recovering=*/true,
39234 /*or_comma=*/false,
39235 /*consume_paren=*/
39236 true);
39238 if (this_req == 0)
39240 cp_parser_require_pragma_eol (parser, pragma_tok);
39241 return false;
39244 p = NULL;
39246 else
39248 error_at (cloc, "expected %<unified_address%>, "
39249 "%<unified_shared_memory%>, "
39250 "%<dynamic_allocators%>, "
39251 "%<reverse_offload%> "
39252 "or %<atomic_default_mem_order%> clause");
39253 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39254 return false;
39256 if (p)
39257 sorry_at (cloc, "%qs clause on %<requires%> directive not "
39258 "supported yet", p);
39259 if (p)
39260 cp_lexer_consume_token (parser->lexer);
39261 if (this_req)
39263 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39265 if ((this_req & new_req) != 0)
39266 error_at (cloc, "too many %qs clauses", p);
39267 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
39268 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
39269 error_at (cloc, "%qs clause used lexically after first "
39270 "target construct or offloading API", p);
39272 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39274 error_at (cloc, "too many %qs clauses",
39275 "atomic_default_mem_order");
39276 this_req = (enum omp_requires) 0;
39278 else if ((omp_requires_mask
39279 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39281 error_at (cloc, "more than one %<atomic_default_mem_order%>"
39282 " clause in a single compilation unit");
39283 this_req
39284 = (enum omp_requires)
39285 (omp_requires_mask
39286 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
39288 else if ((omp_requires_mask
39289 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
39290 error_at (cloc, "%<atomic_default_mem_order%> clause used "
39291 "lexically after first %<atomic%> construct "
39292 "without memory order clause");
39293 new_req = (enum omp_requires) (new_req | this_req);
39294 omp_requires_mask
39295 = (enum omp_requires) (omp_requires_mask | this_req);
39296 continue;
39299 break;
39301 cp_parser_require_pragma_eol (parser, pragma_tok);
39303 if (new_req == 0)
39304 error_at (loc, "%<pragma omp requires%> requires at least one clause");
39305 return false;
39309 /* OpenMP 4.5:
39310 #pragma omp taskloop taskloop-clause[optseq] new-line
39311 for-loop
39313 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39314 for-loop */
39316 #define OMP_TASKLOOP_CLAUSE_MASK \
39317 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39334 static tree
39335 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
39336 char *p_name, omp_clause_mask mask, tree *cclauses,
39337 bool *if_p)
39339 tree clauses, sb, ret;
39340 unsigned int save;
39341 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39343 strcat (p_name, " taskloop");
39344 mask |= OMP_TASKLOOP_CLAUSE_MASK;
39345 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39346 clause. */
39347 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
39348 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
39350 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39352 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39353 const char *p = IDENTIFIER_POINTER (id);
39355 if (strcmp (p, "simd") == 0)
39357 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39358 if (cclauses == NULL)
39359 cclauses = cclauses_buf;
39361 cp_lexer_consume_token (parser->lexer);
39362 if (!flag_openmp) /* flag_openmp_simd */
39363 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39364 cclauses, if_p);
39365 sb = begin_omp_structured_block ();
39366 save = cp_parser_begin_omp_structured_block (parser);
39367 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39368 cclauses, if_p);
39369 cp_parser_end_omp_structured_block (parser, save);
39370 tree body = finish_omp_structured_block (sb);
39371 if (ret == NULL)
39372 return ret;
39373 ret = make_node (OMP_TASKLOOP);
39374 TREE_TYPE (ret) = void_type_node;
39375 OMP_FOR_BODY (ret) = body;
39376 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39377 SET_EXPR_LOCATION (ret, loc);
39378 add_stmt (ret);
39379 return ret;
39382 if (!flag_openmp) /* flag_openmp_simd */
39384 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39385 return NULL_TREE;
39388 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39389 cclauses == NULL);
39390 if (cclauses)
39392 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
39393 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39396 keep_next_level (true);
39397 sb = begin_omp_structured_block ();
39398 save = cp_parser_begin_omp_structured_block (parser);
39400 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
39401 if_p);
39403 cp_parser_end_omp_structured_block (parser, save);
39404 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39406 return ret;
39410 /* OpenACC 2.0:
39411 # pragma acc routine oacc-routine-clause[optseq] new-line
39412 function-definition
39414 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39417 #define OACC_ROUTINE_CLAUSE_MASK \
39418 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39424 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39425 component, which must resolve to a declared namespace-scope
39426 function. The clauses are either processed directly (for a named
39427 function), or defered until the immediatley following declaration
39428 is parsed. */
39430 static void
39431 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
39432 enum pragma_context context)
39434 gcc_checking_assert (context == pragma_external);
39435 /* The checking for "another pragma following this one" in the "no optional
39436 '( name )'" case makes sure that we dont re-enter. */
39437 gcc_checking_assert (parser->oacc_routine == NULL);
39439 cp_oacc_routine_data data;
39440 data.error_seen = false;
39441 data.fndecl_seen = false;
39442 data.tokens = vNULL;
39443 data.clauses = NULL_TREE;
39444 data.loc = pragma_tok->location;
39445 /* It is safe to take the address of a local variable; it will only be
39446 used while this scope is live. */
39447 parser->oacc_routine = &data;
39449 /* Look for optional '( name )'. */
39450 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39452 matching_parens parens;
39453 parens.consume_open (parser); /* '(' */
39455 /* We parse the name as an id-expression. If it resolves to
39456 anything other than a non-overloaded function at namespace
39457 scope, it's an error. */
39458 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
39459 tree name = cp_parser_id_expression (parser,
39460 /*template_keyword_p=*/false,
39461 /*check_dependency_p=*/false,
39462 /*template_p=*/NULL,
39463 /*declarator_p=*/false,
39464 /*optional_p=*/false);
39465 tree decl = (identifier_p (name)
39466 ? cp_parser_lookup_name_simple (parser, name, name_loc)
39467 : name);
39468 if (name != error_mark_node && decl == error_mark_node)
39469 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
39471 if (decl == error_mark_node
39472 || !parens.require_close (parser))
39474 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39475 parser->oacc_routine = NULL;
39476 return;
39479 data.clauses
39480 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39481 "#pragma acc routine",
39482 cp_lexer_peek_token (parser->lexer));
39484 if (decl && is_overloaded_fn (decl)
39485 && (TREE_CODE (decl) != FUNCTION_DECL
39486 || DECL_FUNCTION_TEMPLATE_P (decl)))
39488 error_at (name_loc,
39489 "%<#pragma acc routine%> names a set of overloads");
39490 parser->oacc_routine = NULL;
39491 return;
39494 /* Perhaps we should use the same rule as declarations in different
39495 namespaces? */
39496 if (!DECL_NAMESPACE_SCOPE_P (decl))
39498 error_at (name_loc,
39499 "%qD does not refer to a namespace scope function", decl);
39500 parser->oacc_routine = NULL;
39501 return;
39504 if (TREE_CODE (decl) != FUNCTION_DECL)
39506 error_at (name_loc, "%qD does not refer to a function", decl);
39507 parser->oacc_routine = NULL;
39508 return;
39511 cp_finalize_oacc_routine (parser, decl, false);
39512 parser->oacc_routine = NULL;
39514 else /* No optional '( name )'. */
39516 /* Store away all pragma tokens. */
39517 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39518 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39519 cp_lexer_consume_token (parser->lexer);
39520 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39521 parser->oacc_routine->error_seen = true;
39522 cp_parser_require_pragma_eol (parser, pragma_tok);
39523 struct cp_token_cache *cp
39524 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39525 parser->oacc_routine->tokens.safe_push (cp);
39527 /* Emit a helpful diagnostic if there's another pragma following this
39528 one. */
39529 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39531 cp_ensure_no_oacc_routine (parser);
39532 data.tokens.release ();
39533 /* ..., and then just keep going. */
39534 return;
39537 /* We only have to consider the pragma_external case here. */
39538 cp_parser_declaration (parser);
39539 if (parser->oacc_routine
39540 && !parser->oacc_routine->fndecl_seen)
39541 cp_ensure_no_oacc_routine (parser);
39542 else
39543 parser->oacc_routine = NULL;
39544 data.tokens.release ();
39548 /* Finalize #pragma acc routine clauses after direct declarator has
39549 been parsed. */
39551 static tree
39552 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
39554 struct cp_token_cache *ce;
39555 cp_oacc_routine_data *data = parser->oacc_routine;
39557 if (!data->error_seen && data->fndecl_seen)
39559 error_at (data->loc,
39560 "%<#pragma acc routine%> not immediately followed by "
39561 "a single function declaration or definition");
39562 data->error_seen = true;
39564 if (data->error_seen)
39565 return attrs;
39567 gcc_checking_assert (data->tokens.length () == 1);
39568 ce = data->tokens[0];
39570 cp_parser_push_lexer_for_tokens (parser, ce);
39571 parser->lexer->in_pragma = true;
39572 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39574 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39575 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
39576 parser->oacc_routine->clauses
39577 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39578 "#pragma acc routine", pragma_tok);
39579 cp_parser_pop_lexer (parser);
39580 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
39581 fndecl_seen. */
39583 return attrs;
39586 /* Apply any saved OpenACC routine clauses to a just-parsed
39587 declaration. */
39589 static void
39590 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
39592 if (__builtin_expect (parser->oacc_routine != NULL, 0))
39594 /* Keep going if we're in error reporting mode. */
39595 if (parser->oacc_routine->error_seen
39596 || fndecl == error_mark_node)
39597 return;
39599 if (parser->oacc_routine->fndecl_seen)
39601 error_at (parser->oacc_routine->loc,
39602 "%<#pragma acc routine%> not immediately followed by"
39603 " a single function declaration or definition");
39604 parser->oacc_routine = NULL;
39605 return;
39607 if (TREE_CODE (fndecl) != FUNCTION_DECL)
39609 cp_ensure_no_oacc_routine (parser);
39610 return;
39613 if (oacc_get_fn_attrib (fndecl))
39615 error_at (parser->oacc_routine->loc,
39616 "%<#pragma acc routine%> already applied to %qD", fndecl);
39617 parser->oacc_routine = NULL;
39618 return;
39621 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
39623 error_at (parser->oacc_routine->loc,
39624 TREE_USED (fndecl)
39625 ? G_("%<#pragma acc routine%> must be applied before use")
39626 : G_("%<#pragma acc routine%> must be applied before "
39627 "definition"));
39628 parser->oacc_routine = NULL;
39629 return;
39632 /* Process the routine's dimension clauses. */
39633 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
39634 oacc_replace_fn_attrib (fndecl, dims);
39636 /* Add an "omp declare target" attribute. */
39637 DECL_ATTRIBUTES (fndecl)
39638 = tree_cons (get_identifier ("omp declare target"),
39639 NULL_TREE, DECL_ATTRIBUTES (fndecl));
39641 /* Don't unset parser->oacc_routine here: we may still need it to
39642 diagnose wrong usage. But, remember that we've used this "#pragma acc
39643 routine". */
39644 parser->oacc_routine->fndecl_seen = true;
39648 /* Main entry point to OpenMP statement pragmas. */
39650 static void
39651 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39653 tree stmt;
39654 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
39655 omp_clause_mask mask (0);
39657 switch (cp_parser_pragma_kind (pragma_tok))
39659 case PRAGMA_OACC_ATOMIC:
39660 cp_parser_omp_atomic (parser, pragma_tok);
39661 return;
39662 case PRAGMA_OACC_CACHE:
39663 stmt = cp_parser_oacc_cache (parser, pragma_tok);
39664 break;
39665 case PRAGMA_OACC_DATA:
39666 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
39667 break;
39668 case PRAGMA_OACC_ENTER_DATA:
39669 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
39670 break;
39671 case PRAGMA_OACC_EXIT_DATA:
39672 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
39673 break;
39674 case PRAGMA_OACC_HOST_DATA:
39675 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
39676 break;
39677 case PRAGMA_OACC_KERNELS:
39678 case PRAGMA_OACC_PARALLEL:
39679 strcpy (p_name, "#pragma acc");
39680 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
39681 if_p);
39682 break;
39683 case PRAGMA_OACC_LOOP:
39684 strcpy (p_name, "#pragma acc");
39685 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
39686 if_p);
39687 break;
39688 case PRAGMA_OACC_UPDATE:
39689 stmt = cp_parser_oacc_update (parser, pragma_tok);
39690 break;
39691 case PRAGMA_OACC_WAIT:
39692 stmt = cp_parser_oacc_wait (parser, pragma_tok);
39693 break;
39694 case PRAGMA_OMP_ATOMIC:
39695 cp_parser_omp_atomic (parser, pragma_tok);
39696 return;
39697 case PRAGMA_OMP_CRITICAL:
39698 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
39699 break;
39700 case PRAGMA_OMP_DISTRIBUTE:
39701 strcpy (p_name, "#pragma omp");
39702 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
39703 if_p);
39704 break;
39705 case PRAGMA_OMP_FOR:
39706 strcpy (p_name, "#pragma omp");
39707 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
39708 if_p);
39709 break;
39710 case PRAGMA_OMP_MASTER:
39711 strcpy (p_name, "#pragma omp");
39712 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
39713 if_p);
39714 break;
39715 case PRAGMA_OMP_PARALLEL:
39716 strcpy (p_name, "#pragma omp");
39717 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
39718 if_p);
39719 break;
39720 case PRAGMA_OMP_SECTIONS:
39721 strcpy (p_name, "#pragma omp");
39722 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
39723 break;
39724 case PRAGMA_OMP_SIMD:
39725 strcpy (p_name, "#pragma omp");
39726 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
39727 if_p);
39728 break;
39729 case PRAGMA_OMP_SINGLE:
39730 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
39731 break;
39732 case PRAGMA_OMP_TASK:
39733 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
39734 break;
39735 case PRAGMA_OMP_TASKGROUP:
39736 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
39737 break;
39738 case PRAGMA_OMP_TASKLOOP:
39739 strcpy (p_name, "#pragma omp");
39740 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
39741 if_p);
39742 break;
39743 case PRAGMA_OMP_TEAMS:
39744 strcpy (p_name, "#pragma omp");
39745 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
39746 if_p);
39747 break;
39748 default:
39749 gcc_unreachable ();
39752 protected_set_expr_location (stmt, pragma_tok->location);
39755 /* Transactional Memory parsing routines. */
39757 /* Parse a transaction attribute.
39759 txn-attribute:
39760 attribute
39761 [ [ identifier ] ]
39763 We use this instead of cp_parser_attributes_opt for transactions to avoid
39764 the pedwarn in C++98 mode. */
39766 static tree
39767 cp_parser_txn_attribute_opt (cp_parser *parser)
39769 cp_token *token;
39770 tree attr_name, attr = NULL;
39772 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
39773 return cp_parser_attributes_opt (parser);
39775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
39776 return NULL_TREE;
39777 cp_lexer_consume_token (parser->lexer);
39778 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
39779 goto error1;
39781 token = cp_lexer_peek_token (parser->lexer);
39782 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
39784 token = cp_lexer_consume_token (parser->lexer);
39786 attr_name = (token->type == CPP_KEYWORD
39787 /* For keywords, use the canonical spelling,
39788 not the parsed identifier. */
39789 ? ridpointers[(int) token->keyword]
39790 : token->u.value);
39791 attr = build_tree_list (attr_name, NULL_TREE);
39793 else
39794 cp_parser_error (parser, "expected identifier");
39796 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
39797 error1:
39798 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
39799 return attr;
39802 /* Parse a __transaction_atomic or __transaction_relaxed statement.
39804 transaction-statement:
39805 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
39806 compound-statement
39807 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
39810 static tree
39811 cp_parser_transaction (cp_parser *parser, cp_token *token)
39813 unsigned char old_in = parser->in_transaction;
39814 unsigned char this_in = 1, new_in;
39815 enum rid keyword = token->keyword;
39816 tree stmt, attrs, noex;
39818 cp_lexer_consume_token (parser->lexer);
39820 if (keyword == RID_TRANSACTION_RELAXED
39821 || keyword == RID_SYNCHRONIZED)
39822 this_in |= TM_STMT_ATTR_RELAXED;
39823 else
39825 attrs = cp_parser_txn_attribute_opt (parser);
39826 if (attrs)
39827 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
39830 /* Parse a noexcept specification. */
39831 if (keyword == RID_ATOMIC_NOEXCEPT)
39832 noex = boolean_true_node;
39833 else if (keyword == RID_ATOMIC_CANCEL)
39835 /* cancel-and-throw is unimplemented. */
39836 sorry ("atomic_cancel");
39837 noex = NULL_TREE;
39839 else
39840 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
39842 /* Keep track if we're in the lexical scope of an outer transaction. */
39843 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
39845 stmt = begin_transaction_stmt (token->location, NULL, this_in);
39847 parser->in_transaction = new_in;
39848 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
39849 parser->in_transaction = old_in;
39851 finish_transaction_stmt (stmt, NULL, this_in, noex);
39853 return stmt;
39856 /* Parse a __transaction_atomic or __transaction_relaxed expression.
39858 transaction-expression:
39859 __transaction_atomic txn-noexcept-spec[opt] ( expression )
39860 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
39863 static tree
39864 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
39866 unsigned char old_in = parser->in_transaction;
39867 unsigned char this_in = 1;
39868 cp_token *token;
39869 tree expr, noex;
39870 bool noex_expr;
39871 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39873 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
39874 || keyword == RID_TRANSACTION_RELAXED);
39876 if (!flag_tm)
39877 error_at (loc,
39878 keyword == RID_TRANSACTION_RELAXED
39879 ? G_("%<__transaction_relaxed%> without transactional memory "
39880 "support enabled")
39881 : G_("%<__transaction_atomic%> without transactional memory "
39882 "support enabled"));
39884 token = cp_parser_require_keyword (parser, keyword,
39885 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
39886 : RT_TRANSACTION_RELAXED));
39887 gcc_assert (token != NULL);
39889 if (keyword == RID_TRANSACTION_RELAXED)
39890 this_in |= TM_STMT_ATTR_RELAXED;
39892 /* Set this early. This might mean that we allow transaction_cancel in
39893 an expression that we find out later actually has to be a constexpr.
39894 However, we expect that cxx_constant_value will be able to deal with
39895 this; also, if the noexcept has no constexpr, then what we parse next
39896 really is a transaction's body. */
39897 parser->in_transaction = this_in;
39899 /* Parse a noexcept specification. */
39900 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
39901 true);
39903 if (!noex || !noex_expr
39904 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39906 matching_parens parens;
39907 parens.require_open (parser);
39909 expr = cp_parser_expression (parser);
39910 expr = finish_parenthesized_expr (expr);
39912 parens.require_close (parser);
39914 else
39916 /* The only expression that is available got parsed for the noexcept
39917 already. noexcept is true then. */
39918 expr = noex;
39919 noex = boolean_true_node;
39922 expr = build_transaction_expr (token->location, expr, this_in, noex);
39923 parser->in_transaction = old_in;
39925 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
39926 return error_mark_node;
39928 return (flag_tm ? expr : error_mark_node);
39931 /* Parse a function-transaction-block.
39933 function-transaction-block:
39934 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
39935 function-body
39936 __transaction_atomic txn-attribute[opt] function-try-block
39937 __transaction_relaxed ctor-initializer[opt] function-body
39938 __transaction_relaxed function-try-block
39941 static void
39942 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
39944 unsigned char old_in = parser->in_transaction;
39945 unsigned char new_in = 1;
39946 tree compound_stmt, stmt, attrs;
39947 cp_token *token;
39949 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
39950 || keyword == RID_TRANSACTION_RELAXED);
39951 token = cp_parser_require_keyword (parser, keyword,
39952 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
39953 : RT_TRANSACTION_RELAXED));
39954 gcc_assert (token != NULL);
39956 if (keyword == RID_TRANSACTION_RELAXED)
39957 new_in |= TM_STMT_ATTR_RELAXED;
39958 else
39960 attrs = cp_parser_txn_attribute_opt (parser);
39961 if (attrs)
39962 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
39965 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
39967 parser->in_transaction = new_in;
39969 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
39970 cp_parser_function_try_block (parser);
39971 else
39972 cp_parser_ctor_initializer_opt_and_function_body
39973 (parser, /*in_function_try_block=*/false);
39975 parser->in_transaction = old_in;
39977 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
39980 /* Parse a __transaction_cancel statement.
39982 cancel-statement:
39983 __transaction_cancel txn-attribute[opt] ;
39984 __transaction_cancel txn-attribute[opt] throw-expression ;
39986 ??? Cancel and throw is not yet implemented. */
39988 static tree
39989 cp_parser_transaction_cancel (cp_parser *parser)
39991 cp_token *token;
39992 bool is_outer = false;
39993 tree stmt, attrs;
39995 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
39996 RT_TRANSACTION_CANCEL);
39997 gcc_assert (token != NULL);
39999 attrs = cp_parser_txn_attribute_opt (parser);
40000 if (attrs)
40001 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
40003 /* ??? Parse cancel-and-throw here. */
40005 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40007 if (!flag_tm)
40009 error_at (token->location, "%<__transaction_cancel%> without "
40010 "transactional memory support enabled");
40011 return error_mark_node;
40013 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
40015 error_at (token->location, "%<__transaction_cancel%> within a "
40016 "%<__transaction_relaxed%>");
40017 return error_mark_node;
40019 else if (is_outer)
40021 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
40022 && !is_tm_may_cancel_outer (current_function_decl))
40024 error_at (token->location, "outer %<__transaction_cancel%> not "
40025 "within outer %<__transaction_atomic%>");
40026 error_at (token->location,
40027 " or a %<transaction_may_cancel_outer%> function");
40028 return error_mark_node;
40031 else if (parser->in_transaction == 0)
40033 error_at (token->location, "%<__transaction_cancel%> not within "
40034 "%<__transaction_atomic%>");
40035 return error_mark_node;
40038 stmt = build_tm_abort_call (token->location, is_outer);
40039 add_stmt (stmt);
40041 return stmt;
40044 /* The parser. */
40046 static GTY (()) cp_parser *the_parser;
40049 /* Special handling for the first token or line in the file. The first
40050 thing in the file might be #pragma GCC pch_preprocess, which loads a
40051 PCH file, which is a GC collection point. So we need to handle this
40052 first pragma without benefit of an existing lexer structure.
40054 Always returns one token to the caller in *FIRST_TOKEN. This is
40055 either the true first token of the file, or the first token after
40056 the initial pragma. */
40058 static void
40059 cp_parser_initial_pragma (cp_token *first_token)
40061 tree name = NULL;
40063 cp_lexer_get_preprocessor_token (NULL, first_token);
40064 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
40065 return;
40067 cp_lexer_get_preprocessor_token (NULL, first_token);
40068 if (first_token->type == CPP_STRING)
40070 name = first_token->u.value;
40072 cp_lexer_get_preprocessor_token (NULL, first_token);
40073 if (first_token->type != CPP_PRAGMA_EOL)
40074 error_at (first_token->location,
40075 "junk at end of %<#pragma GCC pch_preprocess%>");
40077 else
40078 error_at (first_token->location, "expected string literal");
40080 /* Skip to the end of the pragma. */
40081 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
40082 cp_lexer_get_preprocessor_token (NULL, first_token);
40084 /* Now actually load the PCH file. */
40085 if (name)
40086 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
40088 /* Read one more token to return to our caller. We have to do this
40089 after reading the PCH file in, since its pointers have to be
40090 live. */
40091 cp_lexer_get_preprocessor_token (NULL, first_token);
40094 /* Parse a pragma GCC ivdep. */
40096 static bool
40097 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
40099 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40100 return true;
40103 /* Parse a pragma GCC unroll. */
40105 static unsigned short
40106 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
40108 location_t location = cp_lexer_peek_token (parser->lexer)->location;
40109 tree expr = cp_parser_constant_expression (parser);
40110 unsigned short unroll;
40111 expr = maybe_constant_value (expr);
40112 HOST_WIDE_INT lunroll = 0;
40113 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
40114 || TREE_CODE (expr) != INTEGER_CST
40115 || (lunroll = tree_to_shwi (expr)) < 0
40116 || lunroll >= USHRT_MAX)
40118 error_at (location, "%<#pragma GCC unroll%> requires an"
40119 " assignment-expression that evaluates to a non-negative"
40120 " integral constant less than %u", USHRT_MAX);
40121 unroll = 0;
40123 else
40125 unroll = (unsigned short)lunroll;
40126 if (unroll == 0)
40127 unroll = 1;
40129 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40130 return unroll;
40133 /* Normal parsing of a pragma token. Here we can (and must) use the
40134 regular lexer. */
40136 static bool
40137 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
40139 cp_token *pragma_tok;
40140 unsigned int id;
40141 tree stmt;
40142 bool ret;
40144 pragma_tok = cp_lexer_consume_token (parser->lexer);
40145 gcc_assert (pragma_tok->type == CPP_PRAGMA);
40146 parser->lexer->in_pragma = true;
40148 id = cp_parser_pragma_kind (pragma_tok);
40149 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
40150 cp_ensure_no_omp_declare_simd (parser);
40151 switch (id)
40153 case PRAGMA_GCC_PCH_PREPROCESS:
40154 error_at (pragma_tok->location,
40155 "%<#pragma GCC pch_preprocess%> must be first");
40156 break;
40158 case PRAGMA_OMP_BARRIER:
40159 switch (context)
40161 case pragma_compound:
40162 cp_parser_omp_barrier (parser, pragma_tok);
40163 return false;
40164 case pragma_stmt:
40165 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40166 "used in compound statements", "omp barrier");
40167 break;
40168 default:
40169 goto bad_stmt;
40171 break;
40173 case PRAGMA_OMP_DEPOBJ:
40174 switch (context)
40176 case pragma_compound:
40177 cp_parser_omp_depobj (parser, pragma_tok);
40178 return false;
40179 case pragma_stmt:
40180 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40181 "used in compound statements", "omp depobj");
40182 break;
40183 default:
40184 goto bad_stmt;
40186 break;
40188 case PRAGMA_OMP_FLUSH:
40189 switch (context)
40191 case pragma_compound:
40192 cp_parser_omp_flush (parser, pragma_tok);
40193 return false;
40194 case pragma_stmt:
40195 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40196 "used in compound statements", "omp flush");
40197 break;
40198 default:
40199 goto bad_stmt;
40201 break;
40203 case PRAGMA_OMP_TASKWAIT:
40204 switch (context)
40206 case pragma_compound:
40207 cp_parser_omp_taskwait (parser, pragma_tok);
40208 return false;
40209 case pragma_stmt:
40210 error_at (pragma_tok->location,
40211 "%<#pragma %s%> may only be used in compound statements",
40212 "omp taskwait");
40213 break;
40214 default:
40215 goto bad_stmt;
40217 break;
40219 case PRAGMA_OMP_TASKYIELD:
40220 switch (context)
40222 case pragma_compound:
40223 cp_parser_omp_taskyield (parser, pragma_tok);
40224 return false;
40225 case pragma_stmt:
40226 error_at (pragma_tok->location,
40227 "%<#pragma %s%> may only be used in compound statements",
40228 "omp taskyield");
40229 break;
40230 default:
40231 goto bad_stmt;
40233 break;
40235 case PRAGMA_OMP_CANCEL:
40236 switch (context)
40238 case pragma_compound:
40239 cp_parser_omp_cancel (parser, pragma_tok);
40240 return false;
40241 case pragma_stmt:
40242 error_at (pragma_tok->location,
40243 "%<#pragma %s%> may only be used in compound statements",
40244 "omp cancel");
40245 break;
40246 default:
40247 goto bad_stmt;
40249 break;
40251 case PRAGMA_OMP_CANCELLATION_POINT:
40252 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
40253 return false;
40255 case PRAGMA_OMP_THREADPRIVATE:
40256 cp_parser_omp_threadprivate (parser, pragma_tok);
40257 return false;
40259 case PRAGMA_OMP_DECLARE:
40260 return cp_parser_omp_declare (parser, pragma_tok, context);
40262 case PRAGMA_OACC_DECLARE:
40263 cp_parser_oacc_declare (parser, pragma_tok);
40264 return false;
40266 case PRAGMA_OACC_ENTER_DATA:
40267 if (context == pragma_stmt)
40269 error_at (pragma_tok->location,
40270 "%<#pragma %s%> may only be used in compound statements",
40271 "acc enter data");
40272 break;
40274 else if (context != pragma_compound)
40275 goto bad_stmt;
40276 cp_parser_omp_construct (parser, pragma_tok, if_p);
40277 return true;
40279 case PRAGMA_OACC_EXIT_DATA:
40280 if (context == pragma_stmt)
40282 error_at (pragma_tok->location,
40283 "%<#pragma %s%> may only be used in compound statements",
40284 "acc exit data");
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_ROUTINE:
40293 if (context != pragma_external)
40295 error_at (pragma_tok->location,
40296 "%<#pragma acc routine%> must be at file scope");
40297 break;
40299 cp_parser_oacc_routine (parser, pragma_tok, context);
40300 return false;
40302 case PRAGMA_OACC_UPDATE:
40303 if (context == pragma_stmt)
40305 error_at (pragma_tok->location,
40306 "%<#pragma %s%> may only be used in compound statements",
40307 "acc update");
40308 break;
40310 else if (context != pragma_compound)
40311 goto bad_stmt;
40312 cp_parser_omp_construct (parser, pragma_tok, if_p);
40313 return true;
40315 case PRAGMA_OACC_WAIT:
40316 if (context == pragma_stmt)
40318 error_at (pragma_tok->location,
40319 "%<#pragma %s%> may only be used in compound statements",
40320 "acc wait");
40321 break;
40323 else if (context != pragma_compound)
40324 goto bad_stmt;
40325 cp_parser_omp_construct (parser, pragma_tok, if_p);
40326 return true;
40328 case PRAGMA_OACC_ATOMIC:
40329 case PRAGMA_OACC_CACHE:
40330 case PRAGMA_OACC_DATA:
40331 case PRAGMA_OACC_HOST_DATA:
40332 case PRAGMA_OACC_KERNELS:
40333 case PRAGMA_OACC_PARALLEL:
40334 case PRAGMA_OACC_LOOP:
40335 case PRAGMA_OMP_ATOMIC:
40336 case PRAGMA_OMP_CRITICAL:
40337 case PRAGMA_OMP_DISTRIBUTE:
40338 case PRAGMA_OMP_FOR:
40339 case PRAGMA_OMP_MASTER:
40340 case PRAGMA_OMP_PARALLEL:
40341 case PRAGMA_OMP_SECTIONS:
40342 case PRAGMA_OMP_SIMD:
40343 case PRAGMA_OMP_SINGLE:
40344 case PRAGMA_OMP_TASK:
40345 case PRAGMA_OMP_TASKGROUP:
40346 case PRAGMA_OMP_TASKLOOP:
40347 case PRAGMA_OMP_TEAMS:
40348 if (context != pragma_stmt && context != pragma_compound)
40349 goto bad_stmt;
40350 stmt = push_omp_privatization_clauses (false);
40351 cp_parser_omp_construct (parser, pragma_tok, if_p);
40352 pop_omp_privatization_clauses (stmt);
40353 return true;
40355 case PRAGMA_OMP_REQUIRES:
40356 return cp_parser_omp_requires (parser, pragma_tok);
40358 case PRAGMA_OMP_ORDERED:
40359 if (context != pragma_stmt && context != pragma_compound)
40360 goto bad_stmt;
40361 stmt = push_omp_privatization_clauses (false);
40362 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
40363 pop_omp_privatization_clauses (stmt);
40364 return ret;
40366 case PRAGMA_OMP_TARGET:
40367 if (context != pragma_stmt && context != pragma_compound)
40368 goto bad_stmt;
40369 stmt = push_omp_privatization_clauses (false);
40370 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
40371 pop_omp_privatization_clauses (stmt);
40372 return ret;
40374 case PRAGMA_OMP_END_DECLARE_TARGET:
40375 cp_parser_omp_end_declare_target (parser, pragma_tok);
40376 return false;
40378 case PRAGMA_OMP_SECTION:
40379 error_at (pragma_tok->location,
40380 "%<#pragma omp section%> may only be used in "
40381 "%<#pragma omp sections%> construct");
40382 break;
40384 case PRAGMA_IVDEP:
40386 if (context == pragma_external)
40388 error_at (pragma_tok->location,
40389 "%<#pragma GCC ivdep%> must be inside a function");
40390 break;
40392 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
40393 unsigned short unroll;
40394 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40395 if (tok->type == CPP_PRAGMA
40396 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
40398 tok = cp_lexer_consume_token (parser->lexer);
40399 unroll = cp_parser_pragma_unroll (parser, tok);
40400 tok = cp_lexer_peek_token (the_parser->lexer);
40402 else
40403 unroll = 0;
40404 if (tok->type != CPP_KEYWORD
40405 || (tok->keyword != RID_FOR
40406 && tok->keyword != RID_WHILE
40407 && tok->keyword != RID_DO))
40409 cp_parser_error (parser, "for, while or do statement expected");
40410 return false;
40412 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40413 return true;
40416 case PRAGMA_UNROLL:
40418 if (context == pragma_external)
40420 error_at (pragma_tok->location,
40421 "%<#pragma GCC unroll%> must be inside a function");
40422 break;
40424 const unsigned short unroll
40425 = cp_parser_pragma_unroll (parser, pragma_tok);
40426 bool ivdep;
40427 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40428 if (tok->type == CPP_PRAGMA
40429 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
40431 tok = cp_lexer_consume_token (parser->lexer);
40432 ivdep = cp_parser_pragma_ivdep (parser, tok);
40433 tok = cp_lexer_peek_token (the_parser->lexer);
40435 else
40436 ivdep = false;
40437 if (tok->type != CPP_KEYWORD
40438 || (tok->keyword != RID_FOR
40439 && tok->keyword != RID_WHILE
40440 && tok->keyword != RID_DO))
40442 cp_parser_error (parser, "for, while or do statement expected");
40443 return false;
40445 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40446 return true;
40449 default:
40450 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
40451 c_invoke_pragma_handler (id);
40452 break;
40454 bad_stmt:
40455 cp_parser_error (parser, "expected declaration specifiers");
40456 break;
40459 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40460 return false;
40463 /* The interface the pragma parsers have to the lexer. */
40465 enum cpp_ttype
40466 pragma_lex (tree *value, location_t *loc)
40468 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40469 enum cpp_ttype ret = tok->type;
40471 *value = tok->u.value;
40472 if (loc)
40473 *loc = tok->location;
40475 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
40476 ret = CPP_EOF;
40477 else if (ret == CPP_STRING)
40478 *value = cp_parser_string_literal (the_parser, false, false);
40479 else
40481 if (ret == CPP_KEYWORD)
40482 ret = CPP_NAME;
40483 cp_lexer_consume_token (the_parser->lexer);
40486 return ret;
40490 /* External interface. */
40492 /* Parse one entire translation unit. */
40494 void
40495 c_parse_file (void)
40497 static bool already_called = false;
40499 if (already_called)
40500 fatal_error (input_location,
40501 "inter-module optimizations not implemented for C++");
40502 already_called = true;
40504 the_parser = cp_parser_new ();
40505 push_deferring_access_checks (flag_access_control
40506 ? dk_no_deferred : dk_no_check);
40507 cp_parser_translation_unit (the_parser);
40508 the_parser = NULL;
40510 finish_translation_unit ();
40513 /* Create an identifier for a generic parameter type (a synthesized
40514 template parameter implied by `auto' or a concept identifier). */
40516 static GTY(()) int generic_parm_count;
40517 static tree
40518 make_generic_type_name ()
40520 char buf[32];
40521 sprintf (buf, "auto:%d", ++generic_parm_count);
40522 return get_identifier (buf);
40525 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
40526 (creating a new template parameter list if necessary). Returns the newly
40527 created template type parm. */
40529 static tree
40530 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
40532 gcc_assert (current_binding_level->kind == sk_function_parms);
40534 /* Before committing to modifying any scope, if we're in an
40535 implicit template scope, and we're trying to synthesize a
40536 constrained parameter, try to find a previous parameter with
40537 the same name. This is the same-type rule for abbreviated
40538 function templates.
40540 NOTE: We can generate implicit parameters when tentatively
40541 parsing a nested name specifier, only to reject that parse
40542 later. However, matching the same template-id as part of a
40543 direct-declarator should generate an identical template
40544 parameter, so this rule will merge them. */
40545 if (parser->implicit_template_scope && constr)
40547 tree t = parser->implicit_template_parms;
40548 while (t)
40550 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
40552 tree d = TREE_VALUE (t);
40553 if (TREE_CODE (d) == PARM_DECL)
40554 /* Return the TEMPLATE_PARM_INDEX. */
40555 d = DECL_INITIAL (d);
40556 return d;
40558 t = TREE_CHAIN (t);
40562 /* We are either continuing a function template that already contains implicit
40563 template parameters, creating a new fully-implicit function template, or
40564 extending an existing explicit function template with implicit template
40565 parameters. */
40567 cp_binding_level *const entry_scope = current_binding_level;
40569 bool become_template = false;
40570 cp_binding_level *parent_scope = 0;
40572 if (parser->implicit_template_scope)
40574 gcc_assert (parser->implicit_template_parms);
40576 current_binding_level = parser->implicit_template_scope;
40578 else
40580 /* Roll back to the existing template parameter scope (in the case of
40581 extending an explicit function template) or introduce a new template
40582 parameter scope ahead of the function parameter scope (or class scope
40583 in the case of out-of-line member definitions). The function scope is
40584 added back after template parameter synthesis below. */
40586 cp_binding_level *scope = entry_scope;
40588 while (scope->kind == sk_function_parms)
40590 parent_scope = scope;
40591 scope = scope->level_chain;
40593 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
40595 /* If not defining a class, then any class scope is a scope level in
40596 an out-of-line member definition. In this case simply wind back
40597 beyond the first such scope to inject the template parameter list.
40598 Otherwise wind back to the class being defined. The latter can
40599 occur in class member friend declarations such as:
40601 class A {
40602 void foo (auto);
40604 class B {
40605 friend void A::foo (auto);
40608 The template parameter list synthesized for the friend declaration
40609 must be injected in the scope of 'B'. This can also occur in
40610 erroneous cases such as:
40612 struct A {
40613 struct B {
40614 void foo (auto);
40616 void B::foo (auto) {}
40619 Here the attempted definition of 'B::foo' within 'A' is ill-formed
40620 but, nevertheless, the template parameter list synthesized for the
40621 declarator should be injected into the scope of 'A' as if the
40622 ill-formed template was specified explicitly. */
40624 while (scope->kind == sk_class && !scope->defining_class_p)
40626 parent_scope = scope;
40627 scope = scope->level_chain;
40631 current_binding_level = scope;
40633 if (scope->kind != sk_template_parms
40634 || !function_being_declared_is_template_p (parser))
40636 /* Introduce a new template parameter list for implicit template
40637 parameters. */
40639 become_template = true;
40641 parser->implicit_template_scope
40642 = begin_scope (sk_template_parms, NULL);
40644 ++processing_template_decl;
40646 parser->fully_implicit_function_template_p = true;
40647 ++parser->num_template_parameter_lists;
40649 else
40651 /* Synthesize implicit template parameters at the end of the explicit
40652 template parameter list. */
40654 gcc_assert (current_template_parms);
40656 parser->implicit_template_scope = scope;
40658 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
40659 parser->implicit_template_parms
40660 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
40664 /* Synthesize a new template parameter and track the current template
40665 parameter chain with implicit_template_parms. */
40667 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
40668 tree synth_id = make_generic_type_name ();
40669 tree synth_tmpl_parm;
40670 bool non_type = false;
40672 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
40673 synth_tmpl_parm
40674 = finish_template_type_parm (class_type_node, synth_id);
40675 else if (TREE_CODE (proto) == TEMPLATE_DECL)
40676 synth_tmpl_parm
40677 = finish_constrained_template_template_parm (proto, synth_id);
40678 else
40680 synth_tmpl_parm = copy_decl (proto);
40681 DECL_NAME (synth_tmpl_parm) = synth_id;
40682 non_type = true;
40685 // Attach the constraint to the parm before processing.
40686 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
40687 TREE_TYPE (node) = constr;
40688 tree new_parm
40689 = process_template_parm (parser->implicit_template_parms,
40690 input_location,
40691 node,
40692 /*non_type=*/non_type,
40693 /*param_pack=*/false);
40695 // Chain the new parameter to the list of implicit parameters.
40696 if (parser->implicit_template_parms)
40697 parser->implicit_template_parms
40698 = TREE_CHAIN (parser->implicit_template_parms);
40699 else
40700 parser->implicit_template_parms = new_parm;
40702 tree new_decl = get_local_decls ();
40703 if (non_type)
40704 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
40705 new_decl = DECL_INITIAL (new_decl);
40707 /* If creating a fully implicit function template, start the new implicit
40708 template parameter list with this synthesized type, otherwise grow the
40709 current template parameter list. */
40711 if (become_template)
40713 parent_scope->level_chain = current_binding_level;
40715 tree new_parms = make_tree_vec (1);
40716 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
40717 current_template_parms = tree_cons (size_int (processing_template_decl),
40718 new_parms, current_template_parms);
40720 else
40722 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
40723 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
40724 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
40725 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
40728 // If the new parameter was constrained, we need to add that to the
40729 // constraints in the template parameter list.
40730 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
40732 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
40733 reqs = conjoin_constraints (reqs, req);
40734 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
40737 current_binding_level = entry_scope;
40739 return new_decl;
40742 /* Finish the declaration of a fully implicit function template. Such a
40743 template has no explicit template parameter list so has not been through the
40744 normal template head and tail processing. synthesize_implicit_template_parm
40745 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
40746 provided if the declaration is a class member such that its template
40747 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
40748 form is returned. Otherwise NULL_TREE is returned. */
40750 static tree
40751 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
40753 gcc_assert (parser->fully_implicit_function_template_p);
40755 if (member_decl_opt && member_decl_opt != error_mark_node
40756 && DECL_VIRTUAL_P (member_decl_opt))
40758 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
40759 "implicit templates may not be %<virtual%>");
40760 DECL_VIRTUAL_P (member_decl_opt) = false;
40763 if (member_decl_opt)
40764 member_decl_opt = finish_member_template_decl (member_decl_opt);
40765 end_template_decl ();
40767 parser->fully_implicit_function_template_p = false;
40768 parser->implicit_template_parms = 0;
40769 parser->implicit_template_scope = 0;
40770 --parser->num_template_parameter_lists;
40772 return member_decl_opt;
40775 /* Like finish_fully_implicit_template, but to be used in error
40776 recovery, rearranging scopes so that we restore the state we had
40777 before synthesize_implicit_template_parm inserted the implement
40778 template parms scope. */
40780 static void
40781 abort_fully_implicit_template (cp_parser *parser)
40783 cp_binding_level *return_to_scope = current_binding_level;
40785 if (parser->implicit_template_scope
40786 && return_to_scope != parser->implicit_template_scope)
40788 cp_binding_level *child = return_to_scope;
40789 for (cp_binding_level *scope = child->level_chain;
40790 scope != parser->implicit_template_scope;
40791 scope = child->level_chain)
40792 child = scope;
40793 child->level_chain = parser->implicit_template_scope->level_chain;
40794 parser->implicit_template_scope->level_chain = return_to_scope;
40795 current_binding_level = parser->implicit_template_scope;
40797 else
40798 return_to_scope = return_to_scope->level_chain;
40800 finish_fully_implicit_template (parser, NULL);
40802 gcc_assert (current_binding_level == return_to_scope);
40805 /* Helper function for diagnostics that have complained about things
40806 being used with 'extern "C"' linkage.
40808 Attempt to issue a note showing where the 'extern "C"' linkage began. */
40810 void
40811 maybe_show_extern_c_location (void)
40813 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
40814 inform (the_parser->innermost_linkage_specification_location,
40815 "%<extern \"C\"%> linkage started here");
40818 #include "gt-cp-parser.h"