PR c++/52869
[official-gcc.git] / gcc / cp / parser.c
blob88fc426102b04168273b69ac14e5fc5576821caa
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, location_t id_location)
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;
1481 declarator->id_loc = id_location;
1483 return declarator;
1486 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1487 of modifiers such as const or volatile to apply to the pointer
1488 type, represented as identifiers. ATTRIBUTES represent the attributes that
1489 appertain to the pointer or reference. */
1491 cp_declarator *
1492 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1493 tree attributes)
1495 cp_declarator *declarator;
1497 declarator = make_declarator (cdk_pointer);
1498 declarator->declarator = target;
1499 declarator->u.pointer.qualifiers = cv_qualifiers;
1500 declarator->u.pointer.class_type = NULL_TREE;
1501 if (target)
1503 declarator->id_loc = target->id_loc;
1504 declarator->parameter_pack_p = target->parameter_pack_p;
1505 target->parameter_pack_p = false;
1507 else
1508 declarator->parameter_pack_p = false;
1510 declarator->std_attributes = attributes;
1512 return declarator;
1515 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1516 represent the attributes that appertain to the pointer or
1517 reference. */
1519 cp_declarator *
1520 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1521 bool rvalue_ref, tree attributes)
1523 cp_declarator *declarator;
1525 declarator = make_declarator (cdk_reference);
1526 declarator->declarator = target;
1527 declarator->u.reference.qualifiers = cv_qualifiers;
1528 declarator->u.reference.rvalue_ref = rvalue_ref;
1529 if (target)
1531 declarator->id_loc = target->id_loc;
1532 declarator->parameter_pack_p = target->parameter_pack_p;
1533 target->parameter_pack_p = false;
1535 else
1536 declarator->parameter_pack_p = false;
1538 declarator->std_attributes = attributes;
1540 return declarator;
1543 /* Like make_pointer_declarator -- but for a pointer to a non-static
1544 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1545 appertain to the pointer or reference. */
1547 cp_declarator *
1548 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1549 cp_declarator *pointee,
1550 tree attributes)
1552 cp_declarator *declarator;
1554 declarator = make_declarator (cdk_ptrmem);
1555 declarator->declarator = pointee;
1556 declarator->u.pointer.qualifiers = cv_qualifiers;
1557 declarator->u.pointer.class_type = class_type;
1559 if (pointee)
1561 declarator->parameter_pack_p = pointee->parameter_pack_p;
1562 pointee->parameter_pack_p = false;
1564 else
1565 declarator->parameter_pack_p = false;
1567 declarator->std_attributes = attributes;
1569 return declarator;
1572 /* Make a declarator for the function given by TARGET, with the
1573 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1574 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1575 indicates what exceptions can be thrown. */
1577 cp_declarator *
1578 make_call_declarator (cp_declarator *target,
1579 tree parms,
1580 cp_cv_quals cv_qualifiers,
1581 cp_virt_specifiers virt_specifiers,
1582 cp_ref_qualifier ref_qualifier,
1583 tree tx_qualifier,
1584 tree exception_specification,
1585 tree late_return_type,
1586 tree requires_clause)
1588 cp_declarator *declarator;
1590 declarator = make_declarator (cdk_function);
1591 declarator->declarator = target;
1592 declarator->u.function.parameters = parms;
1593 declarator->u.function.qualifiers = cv_qualifiers;
1594 declarator->u.function.virt_specifiers = virt_specifiers;
1595 declarator->u.function.ref_qualifier = ref_qualifier;
1596 declarator->u.function.tx_qualifier = tx_qualifier;
1597 declarator->u.function.exception_specification = exception_specification;
1598 declarator->u.function.late_return_type = late_return_type;
1599 declarator->u.function.requires_clause = requires_clause;
1600 if (target)
1602 declarator->id_loc = target->id_loc;
1603 declarator->parameter_pack_p = target->parameter_pack_p;
1604 target->parameter_pack_p = false;
1606 else
1607 declarator->parameter_pack_p = false;
1609 return declarator;
1612 /* Make a declarator for an array of BOUNDS elements, each of which is
1613 defined by ELEMENT. */
1615 cp_declarator *
1616 make_array_declarator (cp_declarator *element, tree bounds)
1618 cp_declarator *declarator;
1620 declarator = make_declarator (cdk_array);
1621 declarator->declarator = element;
1622 declarator->u.array.bounds = bounds;
1623 if (element)
1625 declarator->id_loc = element->id_loc;
1626 declarator->parameter_pack_p = element->parameter_pack_p;
1627 element->parameter_pack_p = false;
1629 else
1630 declarator->parameter_pack_p = false;
1632 return declarator;
1635 /* Determine whether the declarator we've seen so far can be a
1636 parameter pack, when followed by an ellipsis. */
1637 static bool
1638 declarator_can_be_parameter_pack (cp_declarator *declarator)
1640 if (declarator && declarator->parameter_pack_p)
1641 /* We already saw an ellipsis. */
1642 return false;
1644 /* Search for a declarator name, or any other declarator that goes
1645 after the point where the ellipsis could appear in a parameter
1646 pack. If we find any of these, then this declarator can not be
1647 made into a parameter pack. */
1648 bool found = false;
1649 while (declarator && !found)
1651 switch ((int)declarator->kind)
1653 case cdk_id:
1654 case cdk_array:
1655 case cdk_decomp:
1656 found = true;
1657 break;
1659 case cdk_error:
1660 return true;
1662 default:
1663 declarator = declarator->declarator;
1664 break;
1668 return !found;
1671 cp_parameter_declarator *no_parameters;
1673 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1674 DECLARATOR and DEFAULT_ARGUMENT. */
1676 cp_parameter_declarator *
1677 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1678 cp_declarator *declarator,
1679 tree default_argument,
1680 location_t loc,
1681 bool template_parameter_pack_p = false)
1683 cp_parameter_declarator *parameter;
1685 parameter = ((cp_parameter_declarator *)
1686 alloc_declarator (sizeof (cp_parameter_declarator)));
1687 parameter->next = NULL;
1688 if (decl_specifiers)
1689 parameter->decl_specifiers = *decl_specifiers;
1690 else
1691 clear_decl_specs (&parameter->decl_specifiers);
1692 parameter->declarator = declarator;
1693 parameter->default_argument = default_argument;
1694 parameter->template_parameter_pack_p = template_parameter_pack_p;
1695 parameter->loc = loc;
1697 return parameter;
1700 /* Returns true iff DECLARATOR is a declaration for a function. */
1702 static bool
1703 function_declarator_p (const cp_declarator *declarator)
1705 while (declarator)
1707 if (declarator->kind == cdk_function
1708 && declarator->declarator->kind == cdk_id)
1709 return true;
1710 if (declarator->kind == cdk_id
1711 || declarator->kind == cdk_decomp
1712 || declarator->kind == cdk_error)
1713 return false;
1714 declarator = declarator->declarator;
1716 return false;
1719 /* The parser. */
1721 /* Overview
1722 --------
1724 A cp_parser parses the token stream as specified by the C++
1725 grammar. Its job is purely parsing, not semantic analysis. For
1726 example, the parser breaks the token stream into declarators,
1727 expressions, statements, and other similar syntactic constructs.
1728 It does not check that the types of the expressions on either side
1729 of an assignment-statement are compatible, or that a function is
1730 not declared with a parameter of type `void'.
1732 The parser invokes routines elsewhere in the compiler to perform
1733 semantic analysis and to build up the abstract syntax tree for the
1734 code processed.
1736 The parser (and the template instantiation code, which is, in a
1737 way, a close relative of parsing) are the only parts of the
1738 compiler that should be calling push_scope and pop_scope, or
1739 related functions. The parser (and template instantiation code)
1740 keeps track of what scope is presently active; everything else
1741 should simply honor that. (The code that generates static
1742 initializers may also need to set the scope, in order to check
1743 access control correctly when emitting the initializers.)
1745 Methodology
1746 -----------
1748 The parser is of the standard recursive-descent variety. Upcoming
1749 tokens in the token stream are examined in order to determine which
1750 production to use when parsing a non-terminal. Some C++ constructs
1751 require arbitrary look ahead to disambiguate. For example, it is
1752 impossible, in the general case, to tell whether a statement is an
1753 expression or declaration without scanning the entire statement.
1754 Therefore, the parser is capable of "parsing tentatively." When the
1755 parser is not sure what construct comes next, it enters this mode.
1756 Then, while we attempt to parse the construct, the parser queues up
1757 error messages, rather than issuing them immediately, and saves the
1758 tokens it consumes. If the construct is parsed successfully, the
1759 parser "commits", i.e., it issues any queued error messages and
1760 the tokens that were being preserved are permanently discarded.
1761 If, however, the construct is not parsed successfully, the parser
1762 rolls back its state completely so that it can resume parsing using
1763 a different alternative.
1765 Future Improvements
1766 -------------------
1768 The performance of the parser could probably be improved substantially.
1769 We could often eliminate the need to parse tentatively by looking ahead
1770 a little bit. In some places, this approach might not entirely eliminate
1771 the need to parse tentatively, but it might still speed up the average
1772 case. */
1774 /* Flags that are passed to some parsing functions. These values can
1775 be bitwise-ored together. */
1777 enum
1779 /* No flags. */
1780 CP_PARSER_FLAGS_NONE = 0x0,
1781 /* The construct is optional. If it is not present, then no error
1782 should be issued. */
1783 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1784 /* When parsing a type-specifier, treat user-defined type-names
1785 as non-type identifiers. */
1786 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1787 /* When parsing a type-specifier, do not try to parse a class-specifier
1788 or enum-specifier. */
1789 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1790 /* When parsing a decl-specifier-seq, only allow type-specifier or
1791 constexpr. */
1792 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1793 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1794 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1797 /* This type is used for parameters and variables which hold
1798 combinations of the above flags. */
1799 typedef int cp_parser_flags;
1801 /* The different kinds of declarators we want to parse. */
1803 enum cp_parser_declarator_kind
1805 /* We want an abstract declarator. */
1806 CP_PARSER_DECLARATOR_ABSTRACT,
1807 /* We want a named declarator. */
1808 CP_PARSER_DECLARATOR_NAMED,
1809 /* We don't mind, but the name must be an unqualified-id. */
1810 CP_PARSER_DECLARATOR_EITHER
1813 /* The precedence values used to parse binary expressions. The minimum value
1814 of PREC must be 1, because zero is reserved to quickly discriminate
1815 binary operators from other tokens. */
1817 enum cp_parser_prec
1819 PREC_NOT_OPERATOR,
1820 PREC_LOGICAL_OR_EXPRESSION,
1821 PREC_LOGICAL_AND_EXPRESSION,
1822 PREC_INCLUSIVE_OR_EXPRESSION,
1823 PREC_EXCLUSIVE_OR_EXPRESSION,
1824 PREC_AND_EXPRESSION,
1825 PREC_EQUALITY_EXPRESSION,
1826 PREC_RELATIONAL_EXPRESSION,
1827 PREC_SHIFT_EXPRESSION,
1828 PREC_ADDITIVE_EXPRESSION,
1829 PREC_MULTIPLICATIVE_EXPRESSION,
1830 PREC_PM_EXPRESSION,
1831 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1834 /* A mapping from a token type to a corresponding tree node type, with a
1835 precedence value. */
1837 struct cp_parser_binary_operations_map_node
1839 /* The token type. */
1840 enum cpp_ttype token_type;
1841 /* The corresponding tree code. */
1842 enum tree_code tree_type;
1843 /* The precedence of this operator. */
1844 enum cp_parser_prec prec;
1847 struct cp_parser_expression_stack_entry
1849 /* Left hand side of the binary operation we are currently
1850 parsing. */
1851 cp_expr lhs;
1852 /* Original tree code for left hand side, if it was a binary
1853 expression itself (used for -Wparentheses). */
1854 enum tree_code lhs_type;
1855 /* Tree code for the binary operation we are parsing. */
1856 enum tree_code tree_type;
1857 /* Precedence of the binary operation we are parsing. */
1858 enum cp_parser_prec prec;
1859 /* Location of the binary operation we are parsing. */
1860 location_t loc;
1863 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1864 entries because precedence levels on the stack are monotonically
1865 increasing. */
1866 typedef struct cp_parser_expression_stack_entry
1867 cp_parser_expression_stack[NUM_PREC_VALUES];
1869 /* Prototypes. */
1871 /* Constructors and destructors. */
1873 static cp_parser_context *cp_parser_context_new
1874 (cp_parser_context *);
1876 /* Class variables. */
1878 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1880 /* The operator-precedence table used by cp_parser_binary_expression.
1881 Transformed into an associative array (binops_by_token) by
1882 cp_parser_new. */
1884 static const cp_parser_binary_operations_map_node binops[] = {
1885 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1886 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1888 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1889 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1890 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1892 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1893 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1895 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1896 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1898 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1899 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1900 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1901 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1903 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1904 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1906 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1908 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1910 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1912 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1914 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1917 /* The same as binops, but initialized by cp_parser_new so that
1918 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1919 for speed. */
1920 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1922 /* Constructors and destructors. */
1924 /* Construct a new context. The context below this one on the stack
1925 is given by NEXT. */
1927 static cp_parser_context *
1928 cp_parser_context_new (cp_parser_context* next)
1930 cp_parser_context *context;
1932 /* Allocate the storage. */
1933 if (cp_parser_context_free_list != NULL)
1935 /* Pull the first entry from the free list. */
1936 context = cp_parser_context_free_list;
1937 cp_parser_context_free_list = context->next;
1938 memset (context, 0, sizeof (*context));
1940 else
1941 context = ggc_cleared_alloc<cp_parser_context> ();
1943 /* No errors have occurred yet in this context. */
1944 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1945 /* If this is not the bottommost context, copy information that we
1946 need from the previous context. */
1947 if (next)
1949 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1950 expression, then we are parsing one in this context, too. */
1951 context->object_type = next->object_type;
1952 /* Thread the stack. */
1953 context->next = next;
1956 return context;
1959 /* Managing the unparsed function queues. */
1961 #define unparsed_funs_with_default_args \
1962 parser->unparsed_queues->last ().funs_with_default_args
1963 #define unparsed_funs_with_definitions \
1964 parser->unparsed_queues->last ().funs_with_definitions
1965 #define unparsed_nsdmis \
1966 parser->unparsed_queues->last ().nsdmis
1967 #define unparsed_classes \
1968 parser->unparsed_queues->last ().classes
1970 static void
1971 push_unparsed_function_queues (cp_parser *parser)
1973 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1974 vec_safe_push (parser->unparsed_queues, e);
1977 static void
1978 pop_unparsed_function_queues (cp_parser *parser)
1980 release_tree_vector (unparsed_funs_with_definitions);
1981 parser->unparsed_queues->pop ();
1984 /* Prototypes. */
1986 /* Constructors and destructors. */
1988 static cp_parser *cp_parser_new
1989 (void);
1991 /* Routines to parse various constructs.
1993 Those that return `tree' will return the error_mark_node (rather
1994 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1995 Sometimes, they will return an ordinary node if error-recovery was
1996 attempted, even though a parse error occurred. So, to check
1997 whether or not a parse error occurred, you should always use
1998 cp_parser_error_occurred. If the construct is optional (indicated
1999 either by an `_opt' in the name of the function that does the
2000 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2001 the construct is not present. */
2003 /* Lexical conventions [gram.lex] */
2005 static cp_expr cp_parser_identifier
2006 (cp_parser *);
2007 static cp_expr cp_parser_string_literal
2008 (cp_parser *, bool, bool, bool);
2009 static cp_expr cp_parser_userdef_char_literal
2010 (cp_parser *);
2011 static tree cp_parser_userdef_string_literal
2012 (tree);
2013 static cp_expr cp_parser_userdef_numeric_literal
2014 (cp_parser *);
2016 /* Basic concepts [gram.basic] */
2018 static void cp_parser_translation_unit (cp_parser *);
2020 /* Expressions [gram.expr] */
2022 static cp_expr cp_parser_primary_expression
2023 (cp_parser *, bool, bool, bool, cp_id_kind *);
2024 static cp_expr cp_parser_id_expression
2025 (cp_parser *, bool, bool, bool *, bool, bool);
2026 static cp_expr cp_parser_unqualified_id
2027 (cp_parser *, bool, bool, bool, bool);
2028 static tree cp_parser_nested_name_specifier_opt
2029 (cp_parser *, bool, bool, bool, bool, bool = false);
2030 static tree cp_parser_nested_name_specifier
2031 (cp_parser *, bool, bool, bool, bool);
2032 static tree cp_parser_qualifying_entity
2033 (cp_parser *, bool, bool, bool, bool, bool);
2034 static cp_expr cp_parser_postfix_expression
2035 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2036 static tree cp_parser_postfix_open_square_expression
2037 (cp_parser *, tree, bool, bool);
2038 static tree cp_parser_postfix_dot_deref_expression
2039 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2040 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2041 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2042 bool = false);
2043 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2044 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2045 static void cp_parser_pseudo_destructor_name
2046 (cp_parser *, tree, tree *, tree *);
2047 static cp_expr cp_parser_unary_expression
2048 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2049 static enum tree_code cp_parser_unary_operator
2050 (cp_token *);
2051 static tree cp_parser_new_expression
2052 (cp_parser *);
2053 static vec<tree, va_gc> *cp_parser_new_placement
2054 (cp_parser *);
2055 static tree cp_parser_new_type_id
2056 (cp_parser *, tree *);
2057 static cp_declarator *cp_parser_new_declarator_opt
2058 (cp_parser *);
2059 static cp_declarator *cp_parser_direct_new_declarator
2060 (cp_parser *);
2061 static vec<tree, va_gc> *cp_parser_new_initializer
2062 (cp_parser *);
2063 static tree cp_parser_delete_expression
2064 (cp_parser *);
2065 static cp_expr cp_parser_cast_expression
2066 (cp_parser *, bool, bool, bool, cp_id_kind *);
2067 static cp_expr cp_parser_binary_expression
2068 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2069 static tree cp_parser_question_colon_clause
2070 (cp_parser *, cp_expr);
2071 static cp_expr cp_parser_assignment_expression
2072 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2073 static enum tree_code cp_parser_assignment_operator_opt
2074 (cp_parser *);
2075 static cp_expr cp_parser_expression
2076 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2077 static cp_expr cp_parser_constant_expression
2078 (cp_parser *, bool = false, bool * = NULL, bool = false);
2079 static cp_expr cp_parser_builtin_offsetof
2080 (cp_parser *);
2081 static cp_expr cp_parser_lambda_expression
2082 (cp_parser *);
2083 static void cp_parser_lambda_introducer
2084 (cp_parser *, tree);
2085 static bool cp_parser_lambda_declarator_opt
2086 (cp_parser *, tree);
2087 static void cp_parser_lambda_body
2088 (cp_parser *, tree);
2090 /* Statements [gram.stmt.stmt] */
2092 static void cp_parser_statement
2093 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2094 static void cp_parser_label_for_labeled_statement
2095 (cp_parser *, tree);
2096 static tree cp_parser_expression_statement
2097 (cp_parser *, tree);
2098 static tree cp_parser_compound_statement
2099 (cp_parser *, tree, int, bool);
2100 static void cp_parser_statement_seq_opt
2101 (cp_parser *, tree);
2102 static tree cp_parser_selection_statement
2103 (cp_parser *, bool *, vec<tree> *);
2104 static tree cp_parser_condition
2105 (cp_parser *);
2106 static tree cp_parser_iteration_statement
2107 (cp_parser *, bool *, bool, unsigned short);
2108 static bool cp_parser_init_statement
2109 (cp_parser *, tree *decl);
2110 static tree cp_parser_for
2111 (cp_parser *, bool, unsigned short);
2112 static tree cp_parser_c_for
2113 (cp_parser *, tree, tree, bool, unsigned short);
2114 static tree cp_parser_range_for
2115 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2116 static void do_range_for_auto_deduction
2117 (tree, tree);
2118 static tree cp_parser_perform_range_for_lookup
2119 (tree, tree *, tree *);
2120 static tree cp_parser_range_for_member_function
2121 (tree, tree);
2122 static tree cp_parser_jump_statement
2123 (cp_parser *);
2124 static void cp_parser_declaration_statement
2125 (cp_parser *);
2127 static tree cp_parser_implicitly_scoped_statement
2128 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2129 static void cp_parser_already_scoped_statement
2130 (cp_parser *, bool *, const token_indent_info &);
2132 /* Declarations [gram.dcl.dcl] */
2134 static void cp_parser_declaration_seq_opt
2135 (cp_parser *);
2136 static void cp_parser_declaration
2137 (cp_parser *);
2138 static void cp_parser_toplevel_declaration
2139 (cp_parser *);
2140 static void cp_parser_block_declaration
2141 (cp_parser *, bool);
2142 static void cp_parser_simple_declaration
2143 (cp_parser *, bool, tree *);
2144 static void cp_parser_decl_specifier_seq
2145 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2146 static tree cp_parser_storage_class_specifier_opt
2147 (cp_parser *);
2148 static tree cp_parser_function_specifier_opt
2149 (cp_parser *, cp_decl_specifier_seq *);
2150 static tree cp_parser_type_specifier
2151 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2152 int *, bool *);
2153 static tree cp_parser_simple_type_specifier
2154 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2155 static tree cp_parser_type_name
2156 (cp_parser *, bool);
2157 static tree cp_parser_type_name
2158 (cp_parser *);
2159 static tree cp_parser_nonclass_name
2160 (cp_parser* parser);
2161 static tree cp_parser_elaborated_type_specifier
2162 (cp_parser *, bool, bool);
2163 static tree cp_parser_enum_specifier
2164 (cp_parser *);
2165 static void cp_parser_enumerator_list
2166 (cp_parser *, tree);
2167 static void cp_parser_enumerator_definition
2168 (cp_parser *, tree);
2169 static tree cp_parser_namespace_name
2170 (cp_parser *);
2171 static void cp_parser_namespace_definition
2172 (cp_parser *);
2173 static void cp_parser_namespace_body
2174 (cp_parser *);
2175 static tree cp_parser_qualified_namespace_specifier
2176 (cp_parser *);
2177 static void cp_parser_namespace_alias_definition
2178 (cp_parser *);
2179 static bool cp_parser_using_declaration
2180 (cp_parser *, bool);
2181 static void cp_parser_using_directive
2182 (cp_parser *);
2183 static tree cp_parser_alias_declaration
2184 (cp_parser *);
2185 static void cp_parser_asm_definition
2186 (cp_parser *);
2187 static void cp_parser_linkage_specification
2188 (cp_parser *);
2189 static void cp_parser_static_assert
2190 (cp_parser *, bool);
2191 static tree cp_parser_decltype
2192 (cp_parser *);
2193 static tree cp_parser_decomposition_declaration
2194 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2196 /* Declarators [gram.dcl.decl] */
2198 static tree cp_parser_init_declarator
2199 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2200 bool, bool, int, bool *, tree *, location_t *, tree *);
2201 static cp_declarator *cp_parser_declarator
2202 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2203 static cp_declarator *cp_parser_direct_declarator
2204 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2205 static enum tree_code cp_parser_ptr_operator
2206 (cp_parser *, tree *, cp_cv_quals *, tree *);
2207 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2208 (cp_parser *);
2209 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2210 (cp_parser *);
2211 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2212 (cp_parser *);
2213 static tree cp_parser_tx_qualifier_opt
2214 (cp_parser *);
2215 static tree cp_parser_late_return_type_opt
2216 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2217 static tree cp_parser_declarator_id
2218 (cp_parser *, bool);
2219 static tree cp_parser_type_id
2220 (cp_parser *, location_t * = NULL);
2221 static tree cp_parser_template_type_arg
2222 (cp_parser *);
2223 static tree cp_parser_trailing_type_id (cp_parser *);
2224 static tree cp_parser_type_id_1
2225 (cp_parser *, bool, bool, location_t *);
2226 static void cp_parser_type_specifier_seq
2227 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2228 static tree cp_parser_parameter_declaration_clause
2229 (cp_parser *);
2230 static tree cp_parser_parameter_declaration_list
2231 (cp_parser *);
2232 static cp_parameter_declarator *cp_parser_parameter_declaration
2233 (cp_parser *, bool, bool *);
2234 static tree cp_parser_default_argument
2235 (cp_parser *, bool);
2236 static void cp_parser_function_body
2237 (cp_parser *, bool);
2238 static tree cp_parser_initializer
2239 (cp_parser *, bool *, bool *, bool = false);
2240 static cp_expr cp_parser_initializer_clause
2241 (cp_parser *, bool *);
2242 static cp_expr cp_parser_braced_list
2243 (cp_parser*, bool*);
2244 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2245 (cp_parser *, bool *);
2247 static void cp_parser_ctor_initializer_opt_and_function_body
2248 (cp_parser *, bool);
2250 static tree cp_parser_late_parsing_omp_declare_simd
2251 (cp_parser *, tree);
2253 static tree cp_parser_late_parsing_oacc_routine
2254 (cp_parser *, tree);
2256 static tree synthesize_implicit_template_parm
2257 (cp_parser *, tree);
2258 static tree finish_fully_implicit_template
2259 (cp_parser *, tree);
2260 static void abort_fully_implicit_template
2261 (cp_parser *);
2263 /* Classes [gram.class] */
2265 static tree cp_parser_class_name
2266 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2267 static tree cp_parser_class_specifier
2268 (cp_parser *);
2269 static tree cp_parser_class_head
2270 (cp_parser *, bool *);
2271 static enum tag_types cp_parser_class_key
2272 (cp_parser *);
2273 static void cp_parser_type_parameter_key
2274 (cp_parser* parser);
2275 static void cp_parser_member_specification_opt
2276 (cp_parser *);
2277 static void cp_parser_member_declaration
2278 (cp_parser *);
2279 static tree cp_parser_pure_specifier
2280 (cp_parser *);
2281 static tree cp_parser_constant_initializer
2282 (cp_parser *);
2284 /* Derived classes [gram.class.derived] */
2286 static tree cp_parser_base_clause
2287 (cp_parser *);
2288 static tree cp_parser_base_specifier
2289 (cp_parser *);
2291 /* Special member functions [gram.special] */
2293 static tree cp_parser_conversion_function_id
2294 (cp_parser *);
2295 static tree cp_parser_conversion_type_id
2296 (cp_parser *);
2297 static cp_declarator *cp_parser_conversion_declarator_opt
2298 (cp_parser *);
2299 static void cp_parser_ctor_initializer_opt
2300 (cp_parser *);
2301 static void cp_parser_mem_initializer_list
2302 (cp_parser *);
2303 static tree cp_parser_mem_initializer
2304 (cp_parser *);
2305 static tree cp_parser_mem_initializer_id
2306 (cp_parser *);
2308 /* Overloading [gram.over] */
2310 static cp_expr cp_parser_operator_function_id
2311 (cp_parser *);
2312 static cp_expr cp_parser_operator
2313 (cp_parser *);
2315 /* Templates [gram.temp] */
2317 static void cp_parser_template_declaration
2318 (cp_parser *, bool);
2319 static tree cp_parser_template_parameter_list
2320 (cp_parser *);
2321 static tree cp_parser_template_parameter
2322 (cp_parser *, bool *, bool *);
2323 static tree cp_parser_type_parameter
2324 (cp_parser *, bool *);
2325 static tree cp_parser_template_id
2326 (cp_parser *, bool, bool, enum tag_types, bool);
2327 static tree cp_parser_template_name
2328 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2329 static tree cp_parser_template_argument_list
2330 (cp_parser *);
2331 static tree cp_parser_template_argument
2332 (cp_parser *);
2333 static void cp_parser_explicit_instantiation
2334 (cp_parser *);
2335 static void cp_parser_explicit_specialization
2336 (cp_parser *);
2338 /* Exception handling [gram.exception] */
2340 static tree cp_parser_try_block
2341 (cp_parser *);
2342 static void cp_parser_function_try_block
2343 (cp_parser *);
2344 static void cp_parser_handler_seq
2345 (cp_parser *);
2346 static void cp_parser_handler
2347 (cp_parser *);
2348 static tree cp_parser_exception_declaration
2349 (cp_parser *);
2350 static tree cp_parser_throw_expression
2351 (cp_parser *);
2352 static tree cp_parser_exception_specification_opt
2353 (cp_parser *);
2354 static tree cp_parser_type_id_list
2355 (cp_parser *);
2357 /* GNU Extensions */
2359 static tree cp_parser_asm_specification_opt
2360 (cp_parser *);
2361 static tree cp_parser_asm_operand_list
2362 (cp_parser *);
2363 static tree cp_parser_asm_clobber_list
2364 (cp_parser *);
2365 static tree cp_parser_asm_label_list
2366 (cp_parser *);
2367 static bool cp_next_tokens_can_be_attribute_p
2368 (cp_parser *);
2369 static bool cp_next_tokens_can_be_gnu_attribute_p
2370 (cp_parser *);
2371 static bool cp_next_tokens_can_be_std_attribute_p
2372 (cp_parser *);
2373 static bool cp_nth_tokens_can_be_std_attribute_p
2374 (cp_parser *, size_t);
2375 static bool cp_nth_tokens_can_be_gnu_attribute_p
2376 (cp_parser *, size_t);
2377 static bool cp_nth_tokens_can_be_attribute_p
2378 (cp_parser *, size_t);
2379 static tree cp_parser_attributes_opt
2380 (cp_parser *);
2381 static tree cp_parser_gnu_attributes_opt
2382 (cp_parser *);
2383 static tree cp_parser_gnu_attribute_list
2384 (cp_parser *);
2385 static tree cp_parser_std_attribute
2386 (cp_parser *, tree);
2387 static tree cp_parser_std_attribute_spec
2388 (cp_parser *);
2389 static tree cp_parser_std_attribute_spec_seq
2390 (cp_parser *);
2391 static size_t cp_parser_skip_attributes_opt
2392 (cp_parser *, size_t);
2393 static bool cp_parser_extension_opt
2394 (cp_parser *, int *);
2395 static void cp_parser_label_declaration
2396 (cp_parser *);
2398 /* Concept Extensions */
2400 static tree cp_parser_requires_clause
2401 (cp_parser *);
2402 static tree cp_parser_requires_clause_opt
2403 (cp_parser *);
2404 static tree cp_parser_requires_expression
2405 (cp_parser *);
2406 static tree cp_parser_requirement_parameter_list
2407 (cp_parser *);
2408 static tree cp_parser_requirement_body
2409 (cp_parser *);
2410 static tree cp_parser_requirement_list
2411 (cp_parser *);
2412 static tree cp_parser_requirement
2413 (cp_parser *);
2414 static tree cp_parser_simple_requirement
2415 (cp_parser *);
2416 static tree cp_parser_compound_requirement
2417 (cp_parser *);
2418 static tree cp_parser_type_requirement
2419 (cp_parser *);
2420 static tree cp_parser_nested_requirement
2421 (cp_parser *);
2423 /* Transactional Memory Extensions */
2425 static tree cp_parser_transaction
2426 (cp_parser *, cp_token *);
2427 static tree cp_parser_transaction_expression
2428 (cp_parser *, enum rid);
2429 static void cp_parser_function_transaction
2430 (cp_parser *, enum rid);
2431 static tree cp_parser_transaction_cancel
2432 (cp_parser *);
2434 enum pragma_context {
2435 pragma_external,
2436 pragma_member,
2437 pragma_objc_icode,
2438 pragma_stmt,
2439 pragma_compound
2441 static bool cp_parser_pragma
2442 (cp_parser *, enum pragma_context, bool *);
2444 /* Objective-C++ Productions */
2446 static tree cp_parser_objc_message_receiver
2447 (cp_parser *);
2448 static tree cp_parser_objc_message_args
2449 (cp_parser *);
2450 static tree cp_parser_objc_message_expression
2451 (cp_parser *);
2452 static cp_expr cp_parser_objc_encode_expression
2453 (cp_parser *);
2454 static tree cp_parser_objc_defs_expression
2455 (cp_parser *);
2456 static tree cp_parser_objc_protocol_expression
2457 (cp_parser *);
2458 static tree cp_parser_objc_selector_expression
2459 (cp_parser *);
2460 static cp_expr cp_parser_objc_expression
2461 (cp_parser *);
2462 static bool cp_parser_objc_selector_p
2463 (enum cpp_ttype);
2464 static tree cp_parser_objc_selector
2465 (cp_parser *);
2466 static tree cp_parser_objc_protocol_refs_opt
2467 (cp_parser *);
2468 static void cp_parser_objc_declaration
2469 (cp_parser *, tree);
2470 static tree cp_parser_objc_statement
2471 (cp_parser *);
2472 static bool cp_parser_objc_valid_prefix_attributes
2473 (cp_parser *, tree *);
2474 static void cp_parser_objc_at_property_declaration
2475 (cp_parser *) ;
2476 static void cp_parser_objc_at_synthesize_declaration
2477 (cp_parser *) ;
2478 static void cp_parser_objc_at_dynamic_declaration
2479 (cp_parser *) ;
2480 static tree cp_parser_objc_struct_declaration
2481 (cp_parser *) ;
2483 /* Utility Routines */
2485 static cp_expr cp_parser_lookup_name
2486 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2487 static tree cp_parser_lookup_name_simple
2488 (cp_parser *, tree, location_t);
2489 static tree cp_parser_maybe_treat_template_as_class
2490 (tree, bool);
2491 static bool cp_parser_check_declarator_template_parameters
2492 (cp_parser *, cp_declarator *, location_t);
2493 static bool cp_parser_check_template_parameters
2494 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2495 static cp_expr cp_parser_simple_cast_expression
2496 (cp_parser *);
2497 static tree cp_parser_global_scope_opt
2498 (cp_parser *, bool);
2499 static bool cp_parser_constructor_declarator_p
2500 (cp_parser *, bool);
2501 static tree cp_parser_function_definition_from_specifiers_and_declarator
2502 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2503 static tree cp_parser_function_definition_after_declarator
2504 (cp_parser *, bool);
2505 static bool cp_parser_template_declaration_after_export
2506 (cp_parser *, bool);
2507 static void cp_parser_perform_template_parameter_access_checks
2508 (vec<deferred_access_check, va_gc> *);
2509 static tree cp_parser_single_declaration
2510 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2511 static cp_expr cp_parser_functional_cast
2512 (cp_parser *, tree);
2513 static tree cp_parser_save_member_function_body
2514 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2515 static tree cp_parser_save_nsdmi
2516 (cp_parser *);
2517 static tree cp_parser_enclosed_template_argument_list
2518 (cp_parser *);
2519 static void cp_parser_save_default_args
2520 (cp_parser *, tree);
2521 static void cp_parser_late_parsing_for_member
2522 (cp_parser *, tree);
2523 static tree cp_parser_late_parse_one_default_arg
2524 (cp_parser *, tree, tree, tree);
2525 static void cp_parser_late_parsing_nsdmi
2526 (cp_parser *, tree);
2527 static void cp_parser_late_parsing_default_args
2528 (cp_parser *, tree);
2529 static tree cp_parser_sizeof_operand
2530 (cp_parser *, enum rid);
2531 static cp_expr cp_parser_trait_expr
2532 (cp_parser *, enum rid);
2533 static bool cp_parser_declares_only_class_p
2534 (cp_parser *);
2535 static void cp_parser_set_storage_class
2536 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2537 static void cp_parser_set_decl_spec_type
2538 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2539 static void set_and_check_decl_spec_loc
2540 (cp_decl_specifier_seq *decl_specs,
2541 cp_decl_spec ds, cp_token *);
2542 static bool cp_parser_friend_p
2543 (const cp_decl_specifier_seq *);
2544 static void cp_parser_required_error
2545 (cp_parser *, required_token, bool, location_t);
2546 static cp_token *cp_parser_require
2547 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2548 static cp_token *cp_parser_require_keyword
2549 (cp_parser *, enum rid, required_token);
2550 static bool cp_parser_token_starts_function_definition_p
2551 (cp_token *);
2552 static bool cp_parser_next_token_starts_class_definition_p
2553 (cp_parser *);
2554 static bool cp_parser_next_token_ends_template_argument_p
2555 (cp_parser *);
2556 static bool cp_parser_nth_token_starts_template_argument_list_p
2557 (cp_parser *, size_t);
2558 static enum tag_types cp_parser_token_is_class_key
2559 (cp_token *);
2560 static enum tag_types cp_parser_token_is_type_parameter_key
2561 (cp_token *);
2562 static void cp_parser_check_class_key
2563 (enum tag_types, tree type);
2564 static void cp_parser_check_access_in_redeclaration
2565 (tree type, location_t location);
2566 static bool cp_parser_optional_template_keyword
2567 (cp_parser *);
2568 static void cp_parser_pre_parsed_nested_name_specifier
2569 (cp_parser *);
2570 static bool cp_parser_cache_group
2571 (cp_parser *, enum cpp_ttype, unsigned);
2572 static tree cp_parser_cache_defarg
2573 (cp_parser *parser, bool nsdmi);
2574 static void cp_parser_parse_tentatively
2575 (cp_parser *);
2576 static void cp_parser_commit_to_tentative_parse
2577 (cp_parser *);
2578 static void cp_parser_commit_to_topmost_tentative_parse
2579 (cp_parser *);
2580 static void cp_parser_abort_tentative_parse
2581 (cp_parser *);
2582 static bool cp_parser_parse_definitely
2583 (cp_parser *);
2584 static inline bool cp_parser_parsing_tentatively
2585 (cp_parser *);
2586 static bool cp_parser_uncommitted_to_tentative_parse_p
2587 (cp_parser *);
2588 static void cp_parser_error
2589 (cp_parser *, const char *);
2590 static void cp_parser_name_lookup_error
2591 (cp_parser *, tree, tree, name_lookup_error, location_t);
2592 static bool cp_parser_simulate_error
2593 (cp_parser *);
2594 static bool cp_parser_check_type_definition
2595 (cp_parser *);
2596 static void cp_parser_check_for_definition_in_return_type
2597 (cp_declarator *, tree, location_t type_location);
2598 static void cp_parser_check_for_invalid_template_id
2599 (cp_parser *, tree, enum tag_types, location_t location);
2600 static bool cp_parser_non_integral_constant_expression
2601 (cp_parser *, non_integral_constant);
2602 static void cp_parser_diagnose_invalid_type_name
2603 (cp_parser *, tree, location_t);
2604 static bool cp_parser_parse_and_diagnose_invalid_type_name
2605 (cp_parser *);
2606 static int cp_parser_skip_to_closing_parenthesis
2607 (cp_parser *, bool, bool, bool);
2608 static void cp_parser_skip_to_end_of_statement
2609 (cp_parser *);
2610 static void cp_parser_consume_semicolon_at_end_of_statement
2611 (cp_parser *);
2612 static void cp_parser_skip_to_end_of_block_or_statement
2613 (cp_parser *);
2614 static bool cp_parser_skip_to_closing_brace
2615 (cp_parser *);
2616 static void cp_parser_skip_to_end_of_template_parameter_list
2617 (cp_parser *);
2618 static void cp_parser_skip_to_pragma_eol
2619 (cp_parser*, cp_token *);
2620 static bool cp_parser_error_occurred
2621 (cp_parser *);
2622 static bool cp_parser_allow_gnu_extensions_p
2623 (cp_parser *);
2624 static bool cp_parser_is_pure_string_literal
2625 (cp_token *);
2626 static bool cp_parser_is_string_literal
2627 (cp_token *);
2628 static bool cp_parser_is_keyword
2629 (cp_token *, enum rid);
2630 static tree cp_parser_make_typename_type
2631 (cp_parser *, tree, location_t location);
2632 static cp_declarator * cp_parser_make_indirect_declarator
2633 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2634 static bool cp_parser_compound_literal_p
2635 (cp_parser *);
2636 static bool cp_parser_array_designator_p
2637 (cp_parser *);
2638 static bool cp_parser_init_statement_p
2639 (cp_parser *);
2640 static bool cp_parser_skip_to_closing_square_bracket
2641 (cp_parser *);
2643 /* Concept-related syntactic transformations */
2645 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2646 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2648 // -------------------------------------------------------------------------- //
2649 // Unevaluated Operand Guard
2651 // Implementation of an RAII helper for unevaluated operand parsing.
2652 cp_unevaluated::cp_unevaluated ()
2654 ++cp_unevaluated_operand;
2655 ++c_inhibit_evaluation_warnings;
2658 cp_unevaluated::~cp_unevaluated ()
2660 --c_inhibit_evaluation_warnings;
2661 --cp_unevaluated_operand;
2664 // -------------------------------------------------------------------------- //
2665 // Tentative Parsing
2667 /* Returns nonzero if we are parsing tentatively. */
2669 static inline bool
2670 cp_parser_parsing_tentatively (cp_parser* parser)
2672 return parser->context->next != NULL;
2675 /* Returns nonzero if TOKEN is a string literal. */
2677 static bool
2678 cp_parser_is_pure_string_literal (cp_token* token)
2680 return (token->type == CPP_STRING ||
2681 token->type == CPP_STRING16 ||
2682 token->type == CPP_STRING32 ||
2683 token->type == CPP_WSTRING ||
2684 token->type == CPP_UTF8STRING);
2687 /* Returns nonzero if TOKEN is a string literal
2688 of a user-defined string literal. */
2690 static bool
2691 cp_parser_is_string_literal (cp_token* token)
2693 return (cp_parser_is_pure_string_literal (token) ||
2694 token->type == CPP_STRING_USERDEF ||
2695 token->type == CPP_STRING16_USERDEF ||
2696 token->type == CPP_STRING32_USERDEF ||
2697 token->type == CPP_WSTRING_USERDEF ||
2698 token->type == CPP_UTF8STRING_USERDEF);
2701 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2703 static bool
2704 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2706 return token->keyword == keyword;
2709 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2710 PRAGMA_NONE. */
2712 static enum pragma_kind
2713 cp_parser_pragma_kind (cp_token *token)
2715 if (token->type != CPP_PRAGMA)
2716 return PRAGMA_NONE;
2717 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2718 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2721 /* Helper function for cp_parser_error.
2722 Having peeked a token of kind TOK1_KIND that might signify
2723 a conflict marker, peek successor tokens to determine
2724 if we actually do have a conflict marker.
2725 Specifically, we consider a run of 7 '<', '=' or '>' characters
2726 at the start of a line as a conflict marker.
2727 These come through the lexer as three pairs and a single,
2728 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2729 If it returns true, *OUT_LOC is written to with the location/range
2730 of the marker. */
2732 static bool
2733 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2734 location_t *out_loc)
2736 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2737 if (token2->type != tok1_kind)
2738 return false;
2739 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2740 if (token3->type != tok1_kind)
2741 return false;
2742 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2743 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2744 return false;
2746 /* It must be at the start of the line. */
2747 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2748 if (LOCATION_COLUMN (start_loc) != 1)
2749 return false;
2751 /* We have a conflict marker. Construct a location of the form:
2752 <<<<<<<
2753 ^~~~~~~
2754 with start == caret, finishing at the end of the marker. */
2755 location_t finish_loc = get_finish (token4->location);
2756 *out_loc = make_location (start_loc, start_loc, finish_loc);
2758 return true;
2761 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2762 RT_CLOSE_PAREN. */
2764 static const char *
2765 get_matching_symbol (required_token token_desc)
2767 switch (token_desc)
2769 default:
2770 gcc_unreachable ();
2771 return "";
2772 case RT_CLOSE_BRACE:
2773 return "{";
2774 case RT_CLOSE_PAREN:
2775 return "(";
2779 /* Attempt to convert TOKEN_DESC from a required_token to an
2780 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2782 static enum cpp_ttype
2783 get_required_cpp_ttype (required_token token_desc)
2785 switch (token_desc)
2787 case RT_SEMICOLON:
2788 return CPP_SEMICOLON;
2789 case RT_OPEN_PAREN:
2790 return CPP_OPEN_PAREN;
2791 case RT_CLOSE_BRACE:
2792 return CPP_CLOSE_BRACE;
2793 case RT_OPEN_BRACE:
2794 return CPP_OPEN_BRACE;
2795 case RT_CLOSE_SQUARE:
2796 return CPP_CLOSE_SQUARE;
2797 case RT_OPEN_SQUARE:
2798 return CPP_OPEN_SQUARE;
2799 case RT_COMMA:
2800 return CPP_COMMA;
2801 case RT_COLON:
2802 return CPP_COLON;
2803 case RT_CLOSE_PAREN:
2804 return CPP_CLOSE_PAREN;
2806 default:
2807 /* Use CPP_EOF as a "no completions possible" code. */
2808 return CPP_EOF;
2813 /* Subroutine of cp_parser_error and cp_parser_required_error.
2815 Issue a diagnostic of the form
2816 FILE:LINE: MESSAGE before TOKEN
2817 where TOKEN is the next token in the input stream. MESSAGE
2818 (specified by the caller) is usually of the form "expected
2819 OTHER-TOKEN".
2821 This bypasses the check for tentative passing, and potentially
2822 adds material needed by cp_parser_required_error.
2824 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2825 suggesting insertion of the missing token.
2827 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2828 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2829 location. */
2831 static void
2832 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2833 required_token missing_token_desc,
2834 location_t matching_location)
2836 cp_token *token = cp_lexer_peek_token (parser->lexer);
2837 /* This diagnostic makes more sense if it is tagged to the line
2838 of the token we just peeked at. */
2839 cp_lexer_set_source_position_from_token (token);
2841 if (token->type == CPP_PRAGMA)
2843 error_at (token->location,
2844 "%<#pragma%> is not allowed here");
2845 cp_parser_skip_to_pragma_eol (parser, token);
2846 return;
2849 /* If this is actually a conflict marker, report it as such. */
2850 if (token->type == CPP_LSHIFT
2851 || token->type == CPP_RSHIFT
2852 || token->type == CPP_EQ_EQ)
2854 location_t loc;
2855 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2857 error_at (loc, "version control conflict marker in file");
2858 expanded_location token_exploc = expand_location (token->location);
2859 /* Consume tokens until the end of the source line. */
2860 while (1)
2862 cp_lexer_consume_token (parser->lexer);
2863 cp_token *next = cp_lexer_peek_token (parser->lexer);
2864 if (next == NULL)
2865 break;
2866 expanded_location next_exploc = expand_location (next->location);
2867 if (next_exploc.file != token_exploc.file)
2868 break;
2869 if (next_exploc.line != token_exploc.line)
2870 break;
2872 return;
2876 gcc_rich_location richloc (input_location);
2878 bool added_matching_location = false;
2880 if (missing_token_desc != RT_NONE)
2882 /* Potentially supply a fix-it hint, suggesting to add the
2883 missing token immediately after the *previous* token.
2884 This may move the primary location within richloc. */
2885 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2886 location_t prev_token_loc
2887 = cp_lexer_previous_token (parser->lexer)->location;
2888 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2890 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2891 Attempt to consolidate diagnostics by printing it as a
2892 secondary range within the main diagnostic. */
2893 if (matching_location != UNKNOWN_LOCATION)
2894 added_matching_location
2895 = richloc.add_location_if_nearby (matching_location);
2898 /* Actually emit the error. */
2899 c_parse_error (gmsgid,
2900 /* Because c_parser_error does not understand
2901 CPP_KEYWORD, keywords are treated like
2902 identifiers. */
2903 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2904 token->u.value, token->flags, &richloc);
2906 if (missing_token_desc != RT_NONE)
2908 /* If we weren't able to consolidate matching_location, then
2909 print it as a secondary diagnostic. */
2910 if (matching_location != UNKNOWN_LOCATION
2911 && !added_matching_location)
2912 inform (matching_location, "to match this %qs",
2913 get_matching_symbol (missing_token_desc));
2917 /* If not parsing tentatively, issue a diagnostic of the form
2918 FILE:LINE: MESSAGE before TOKEN
2919 where TOKEN is the next token in the input stream. MESSAGE
2920 (specified by the caller) is usually of the form "expected
2921 OTHER-TOKEN". */
2923 static void
2924 cp_parser_error (cp_parser* parser, const char* gmsgid)
2926 if (!cp_parser_simulate_error (parser))
2927 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2930 /* Issue an error about name-lookup failing. NAME is the
2931 IDENTIFIER_NODE DECL is the result of
2932 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2933 the thing that we hoped to find. */
2935 static void
2936 cp_parser_name_lookup_error (cp_parser* parser,
2937 tree name,
2938 tree decl,
2939 name_lookup_error desired,
2940 location_t location)
2942 /* If name lookup completely failed, tell the user that NAME was not
2943 declared. */
2944 if (decl == error_mark_node)
2946 if (parser->scope && parser->scope != global_namespace)
2947 error_at (location, "%<%E::%E%> has not been declared",
2948 parser->scope, name);
2949 else if (parser->scope == global_namespace)
2950 error_at (location, "%<::%E%> has not been declared", name);
2951 else if (parser->object_scope
2952 && !CLASS_TYPE_P (parser->object_scope))
2953 error_at (location, "request for member %qE in non-class type %qT",
2954 name, parser->object_scope);
2955 else if (parser->object_scope)
2956 error_at (location, "%<%T::%E%> has not been declared",
2957 parser->object_scope, name);
2958 else
2959 error_at (location, "%qE has not been declared", name);
2961 else if (parser->scope && parser->scope != global_namespace)
2963 switch (desired)
2965 case NLE_TYPE:
2966 error_at (location, "%<%E::%E%> is not a type",
2967 parser->scope, name);
2968 break;
2969 case NLE_CXX98:
2970 error_at (location, "%<%E::%E%> is not a class or namespace",
2971 parser->scope, name);
2972 break;
2973 case NLE_NOT_CXX98:
2974 error_at (location,
2975 "%<%E::%E%> is not a class, namespace, or enumeration",
2976 parser->scope, name);
2977 break;
2978 default:
2979 gcc_unreachable ();
2983 else if (parser->scope == global_namespace)
2985 switch (desired)
2987 case NLE_TYPE:
2988 error_at (location, "%<::%E%> is not a type", name);
2989 break;
2990 case NLE_CXX98:
2991 error_at (location, "%<::%E%> is not a class or namespace", name);
2992 break;
2993 case NLE_NOT_CXX98:
2994 error_at (location,
2995 "%<::%E%> is not a class, namespace, or enumeration",
2996 name);
2997 break;
2998 default:
2999 gcc_unreachable ();
3002 else
3004 switch (desired)
3006 case NLE_TYPE:
3007 error_at (location, "%qE is not a type", name);
3008 break;
3009 case NLE_CXX98:
3010 error_at (location, "%qE is not a class or namespace", name);
3011 break;
3012 case NLE_NOT_CXX98:
3013 error_at (location,
3014 "%qE is not a class, namespace, or enumeration", name);
3015 break;
3016 default:
3017 gcc_unreachable ();
3022 /* If we are parsing tentatively, remember that an error has occurred
3023 during this tentative parse. Returns true if the error was
3024 simulated; false if a message should be issued by the caller. */
3026 static bool
3027 cp_parser_simulate_error (cp_parser* parser)
3029 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3031 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3032 return true;
3034 return false;
3037 /* This function is called when a type is defined. If type
3038 definitions are forbidden at this point, an error message is
3039 issued. */
3041 static bool
3042 cp_parser_check_type_definition (cp_parser* parser)
3044 /* If types are forbidden here, issue a message. */
3045 if (parser->type_definition_forbidden_message)
3047 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3048 in the message need to be interpreted. */
3049 error (parser->type_definition_forbidden_message);
3050 return false;
3052 return true;
3055 /* This function is called when the DECLARATOR is processed. The TYPE
3056 was a type defined in the decl-specifiers. If it is invalid to
3057 define a type in the decl-specifiers for DECLARATOR, an error is
3058 issued. TYPE_LOCATION is the location of TYPE and is used
3059 for error reporting. */
3061 static void
3062 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3063 tree type, location_t type_location)
3065 /* [dcl.fct] forbids type definitions in return types.
3066 Unfortunately, it's not easy to know whether or not we are
3067 processing a return type until after the fact. */
3068 while (declarator
3069 && (declarator->kind == cdk_pointer
3070 || declarator->kind == cdk_reference
3071 || declarator->kind == cdk_ptrmem))
3072 declarator = declarator->declarator;
3073 if (declarator
3074 && declarator->kind == cdk_function)
3076 error_at (type_location,
3077 "new types may not be defined in a return type");
3078 inform (type_location,
3079 "(perhaps a semicolon is missing after the definition of %qT)",
3080 type);
3084 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3085 "<" in any valid C++ program. If the next token is indeed "<",
3086 issue a message warning the user about what appears to be an
3087 invalid attempt to form a template-id. LOCATION is the location
3088 of the type-specifier (TYPE) */
3090 static void
3091 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3092 tree type,
3093 enum tag_types tag_type,
3094 location_t location)
3096 cp_token_position start = 0;
3098 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3100 if (TREE_CODE (type) == TYPE_DECL)
3101 type = TREE_TYPE (type);
3102 if (TYPE_P (type) && !template_placeholder_p (type))
3103 error_at (location, "%qT is not a template", type);
3104 else if (identifier_p (type))
3106 if (tag_type != none_type)
3107 error_at (location, "%qE is not a class template", type);
3108 else
3109 error_at (location, "%qE is not a template", type);
3111 else
3112 error_at (location, "invalid template-id");
3113 /* Remember the location of the invalid "<". */
3114 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3115 start = cp_lexer_token_position (parser->lexer, true);
3116 /* Consume the "<". */
3117 cp_lexer_consume_token (parser->lexer);
3118 /* Parse the template arguments. */
3119 cp_parser_enclosed_template_argument_list (parser);
3120 /* Permanently remove the invalid template arguments so that
3121 this error message is not issued again. */
3122 if (start)
3123 cp_lexer_purge_tokens_after (parser->lexer, start);
3127 /* If parsing an integral constant-expression, issue an error message
3128 about the fact that THING appeared and return true. Otherwise,
3129 return false. In either case, set
3130 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3132 static bool
3133 cp_parser_non_integral_constant_expression (cp_parser *parser,
3134 non_integral_constant thing)
3136 parser->non_integral_constant_expression_p = true;
3137 if (parser->integral_constant_expression_p)
3139 if (!parser->allow_non_integral_constant_expression_p)
3141 const char *msg = NULL;
3142 switch (thing)
3144 case NIC_FLOAT:
3145 pedwarn (input_location, OPT_Wpedantic,
3146 "ISO C++ forbids using a floating-point literal "
3147 "in a constant-expression");
3148 return true;
3149 case NIC_CAST:
3150 error ("a cast to a type other than an integral or "
3151 "enumeration type cannot appear in a "
3152 "constant-expression");
3153 return true;
3154 case NIC_TYPEID:
3155 error ("%<typeid%> operator "
3156 "cannot appear in a constant-expression");
3157 return true;
3158 case NIC_NCC:
3159 error ("non-constant compound literals "
3160 "cannot appear in a constant-expression");
3161 return true;
3162 case NIC_FUNC_CALL:
3163 error ("a function call "
3164 "cannot appear in a constant-expression");
3165 return true;
3166 case NIC_INC:
3167 error ("an increment "
3168 "cannot appear in a constant-expression");
3169 return true;
3170 case NIC_DEC:
3171 error ("an decrement "
3172 "cannot appear in a constant-expression");
3173 return true;
3174 case NIC_ARRAY_REF:
3175 error ("an array reference "
3176 "cannot appear in a constant-expression");
3177 return true;
3178 case NIC_ADDR_LABEL:
3179 error ("the address of a label "
3180 "cannot appear in a constant-expression");
3181 return true;
3182 case NIC_OVERLOADED:
3183 error ("calls to overloaded operators "
3184 "cannot appear in a constant-expression");
3185 return true;
3186 case NIC_ASSIGNMENT:
3187 error ("an assignment cannot appear in a constant-expression");
3188 return true;
3189 case NIC_COMMA:
3190 error ("a comma operator "
3191 "cannot appear in a constant-expression");
3192 return true;
3193 case NIC_CONSTRUCTOR:
3194 error ("a call to a constructor "
3195 "cannot appear in a constant-expression");
3196 return true;
3197 case NIC_TRANSACTION:
3198 error ("a transaction expression "
3199 "cannot appear in a constant-expression");
3200 return true;
3201 case NIC_THIS:
3202 msg = "this";
3203 break;
3204 case NIC_FUNC_NAME:
3205 msg = "__FUNCTION__";
3206 break;
3207 case NIC_PRETTY_FUNC:
3208 msg = "__PRETTY_FUNCTION__";
3209 break;
3210 case NIC_C99_FUNC:
3211 msg = "__func__";
3212 break;
3213 case NIC_VA_ARG:
3214 msg = "va_arg";
3215 break;
3216 case NIC_ARROW:
3217 msg = "->";
3218 break;
3219 case NIC_POINT:
3220 msg = ".";
3221 break;
3222 case NIC_STAR:
3223 msg = "*";
3224 break;
3225 case NIC_ADDR:
3226 msg = "&";
3227 break;
3228 case NIC_PREINCREMENT:
3229 msg = "++";
3230 break;
3231 case NIC_PREDECREMENT:
3232 msg = "--";
3233 break;
3234 case NIC_NEW:
3235 msg = "new";
3236 break;
3237 case NIC_DEL:
3238 msg = "delete";
3239 break;
3240 default:
3241 gcc_unreachable ();
3243 if (msg)
3244 error ("%qs cannot appear in a constant-expression", msg);
3245 return true;
3248 return false;
3251 /* Emit a diagnostic for an invalid type name. This function commits
3252 to the current active tentative parse, if any. (Otherwise, the
3253 problematic construct might be encountered again later, resulting
3254 in duplicate error messages.) LOCATION is the location of ID. */
3256 static void
3257 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3258 location_t location)
3260 tree decl, ambiguous_decls;
3261 cp_parser_commit_to_tentative_parse (parser);
3262 /* Try to lookup the identifier. */
3263 decl = cp_parser_lookup_name (parser, id, none_type,
3264 /*is_template=*/false,
3265 /*is_namespace=*/false,
3266 /*check_dependency=*/true,
3267 &ambiguous_decls, location);
3268 if (ambiguous_decls)
3269 /* If the lookup was ambiguous, an error will already have
3270 been issued. */
3271 return;
3272 /* If the lookup found a template-name, it means that the user forgot
3273 to specify an argument list. Emit a useful error message. */
3274 if (DECL_TYPE_TEMPLATE_P (decl))
3276 auto_diagnostic_group d;
3277 error_at (location,
3278 "invalid use of template-name %qE without an argument list",
3279 decl);
3280 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3281 inform (location, "class template argument deduction is only available "
3282 "with -std=c++17 or -std=gnu++17");
3283 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3285 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3286 error_at (location, "invalid use of destructor %qD as a type", id);
3287 else if (TREE_CODE (decl) == TYPE_DECL)
3288 /* Something like 'unsigned A a;' */
3289 error_at (location, "invalid combination of multiple type-specifiers");
3290 else if (!parser->scope)
3292 /* Issue an error message. */
3293 auto_diagnostic_group d;
3294 name_hint hint;
3295 if (TREE_CODE (id) == IDENTIFIER_NODE)
3296 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3297 if (const char *suggestion = hint.suggestion ())
3299 gcc_rich_location richloc (location);
3300 richloc.add_fixit_replace (suggestion);
3301 error_at (&richloc,
3302 "%qE does not name a type; did you mean %qs?",
3303 id, suggestion);
3305 else
3306 error_at (location, "%qE does not name a type", id);
3307 /* If we're in a template class, it's possible that the user was
3308 referring to a type from a base class. For example:
3310 template <typename T> struct A { typedef T X; };
3311 template <typename T> struct B : public A<T> { X x; };
3313 The user should have said "typename A<T>::X". */
3314 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3315 inform (location, "C++11 %<constexpr%> only available with "
3316 "-std=c++11 or -std=gnu++11");
3317 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3318 inform (location, "C++11 %<noexcept%> only available with "
3319 "-std=c++11 or -std=gnu++11");
3320 else if (cxx_dialect < cxx11
3321 && TREE_CODE (id) == IDENTIFIER_NODE
3322 && id_equal (id, "thread_local"))
3323 inform (location, "C++11 %<thread_local%> only available with "
3324 "-std=c++11 or -std=gnu++11");
3325 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3326 inform (location, "%<concept%> only available with -fconcepts");
3327 else if (processing_template_decl && current_class_type
3328 && TYPE_BINFO (current_class_type))
3330 tree b;
3332 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3334 b = TREE_CHAIN (b))
3336 tree base_type = BINFO_TYPE (b);
3337 if (CLASS_TYPE_P (base_type)
3338 && dependent_type_p (base_type))
3340 tree field;
3341 /* Go from a particular instantiation of the
3342 template (which will have an empty TYPE_FIELDs),
3343 to the main version. */
3344 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3345 for (field = TYPE_FIELDS (base_type);
3346 field;
3347 field = DECL_CHAIN (field))
3348 if (TREE_CODE (field) == TYPE_DECL
3349 && DECL_NAME (field) == id)
3351 inform (location,
3352 "(perhaps %<typename %T::%E%> was intended)",
3353 BINFO_TYPE (b), id);
3354 break;
3356 if (field)
3357 break;
3362 /* Here we diagnose qualified-ids where the scope is actually correct,
3363 but the identifier does not resolve to a valid type name. */
3364 else if (parser->scope != error_mark_node)
3366 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3368 auto_diagnostic_group d;
3369 name_hint hint;
3370 if (decl == error_mark_node)
3371 hint = suggest_alternative_in_explicit_scope (location, id,
3372 parser->scope);
3373 const char *suggestion = hint.suggestion ();
3374 gcc_rich_location richloc (location_of (id));
3375 if (suggestion)
3376 richloc.add_fixit_replace (suggestion);
3377 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3379 if (suggestion)
3380 error_at (&richloc,
3381 "%qE in namespace %qE does not name a template"
3382 " type; did you mean %qs?",
3383 id, parser->scope, suggestion);
3384 else
3385 error_at (&richloc,
3386 "%qE in namespace %qE does not name a template type",
3387 id, parser->scope);
3389 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3391 if (suggestion)
3392 error_at (&richloc,
3393 "%qE in namespace %qE does not name a template"
3394 " type; did you mean %qs?",
3395 TREE_OPERAND (id, 0), parser->scope, suggestion);
3396 else
3397 error_at (&richloc,
3398 "%qE in namespace %qE does not name a template"
3399 " type",
3400 TREE_OPERAND (id, 0), parser->scope);
3402 else
3404 if (suggestion)
3405 error_at (&richloc,
3406 "%qE in namespace %qE does not name a type"
3407 "; did you mean %qs?",
3408 id, parser->scope, suggestion);
3409 else
3410 error_at (&richloc,
3411 "%qE in namespace %qE does not name a type",
3412 id, parser->scope);
3414 if (DECL_P (decl))
3415 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3417 else if (CLASS_TYPE_P (parser->scope)
3418 && constructor_name_p (id, parser->scope))
3420 /* A<T>::A<T>() */
3421 auto_diagnostic_group d;
3422 error_at (location, "%<%T::%E%> names the constructor, not"
3423 " the type", parser->scope, id);
3424 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3425 error_at (location, "and %qT has no template constructors",
3426 parser->scope);
3428 else if (TYPE_P (parser->scope)
3429 && dependent_scope_p (parser->scope))
3431 gcc_rich_location richloc (location);
3432 richloc.add_fixit_insert_before ("typename ");
3433 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3434 error_at (&richloc,
3435 "need %<typename%> before %<%T::%D::%E%> because "
3436 "%<%T::%D%> is a dependent scope",
3437 TYPE_CONTEXT (parser->scope),
3438 TYPENAME_TYPE_FULLNAME (parser->scope),
3440 TYPE_CONTEXT (parser->scope),
3441 TYPENAME_TYPE_FULLNAME (parser->scope));
3442 else
3443 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3444 "%qT is a dependent scope",
3445 parser->scope, id, parser->scope);
3447 else if (TYPE_P (parser->scope))
3449 auto_diagnostic_group d;
3450 if (!COMPLETE_TYPE_P (parser->scope))
3451 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3452 parser->scope);
3453 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3454 error_at (location_of (id),
3455 "%qE in %q#T does not name a template type",
3456 id, parser->scope);
3457 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3458 error_at (location_of (id),
3459 "%qE in %q#T does not name a template type",
3460 TREE_OPERAND (id, 0), parser->scope);
3461 else
3462 error_at (location_of (id),
3463 "%qE in %q#T does not name a type",
3464 id, parser->scope);
3465 if (DECL_P (decl))
3466 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3468 else
3469 gcc_unreachable ();
3473 /* Check for a common situation where a type-name should be present,
3474 but is not, and issue a sensible error message. Returns true if an
3475 invalid type-name was detected.
3477 The situation handled by this function are variable declarations of the
3478 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3479 Usually, `ID' should name a type, but if we got here it means that it
3480 does not. We try to emit the best possible error message depending on
3481 how exactly the id-expression looks like. */
3483 static bool
3484 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3486 tree id;
3487 cp_token *token = cp_lexer_peek_token (parser->lexer);
3489 /* Avoid duplicate error about ambiguous lookup. */
3490 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3492 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3493 if (next->type == CPP_NAME && next->error_reported)
3494 goto out;
3497 cp_parser_parse_tentatively (parser);
3498 id = cp_parser_id_expression (parser,
3499 /*template_keyword_p=*/false,
3500 /*check_dependency_p=*/true,
3501 /*template_p=*/NULL,
3502 /*declarator_p=*/false,
3503 /*optional_p=*/false);
3504 /* If the next token is a (, this is a function with no explicit return
3505 type, i.e. constructor, destructor or conversion op. */
3506 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3507 || TREE_CODE (id) == TYPE_DECL)
3509 cp_parser_abort_tentative_parse (parser);
3510 return false;
3512 if (!cp_parser_parse_definitely (parser))
3513 return false;
3515 /* Emit a diagnostic for the invalid type. */
3516 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3517 out:
3518 /* If we aren't in the middle of a declarator (i.e. in a
3519 parameter-declaration-clause), skip to the end of the declaration;
3520 there's no point in trying to process it. */
3521 if (!parser->in_declarator_p)
3522 cp_parser_skip_to_end_of_block_or_statement (parser);
3523 return true;
3526 /* Consume tokens up to, and including, the next non-nested closing `)'.
3527 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3528 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3529 found an unnested token of that type. */
3531 static int
3532 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3533 bool recovering,
3534 cpp_ttype or_ttype,
3535 bool consume_paren)
3537 unsigned paren_depth = 0;
3538 unsigned brace_depth = 0;
3539 unsigned square_depth = 0;
3540 unsigned condop_depth = 0;
3542 if (recovering && or_ttype == CPP_EOF
3543 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3544 return 0;
3546 while (true)
3548 cp_token * token = cp_lexer_peek_token (parser->lexer);
3550 /* Have we found what we're looking for before the closing paren? */
3551 if (token->type == or_ttype && or_ttype != CPP_EOF
3552 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3553 return -1;
3555 switch (token->type)
3557 case CPP_EOF:
3558 case CPP_PRAGMA_EOL:
3559 /* If we've run out of tokens, then there is no closing `)'. */
3560 return 0;
3562 /* This is good for lambda expression capture-lists. */
3563 case CPP_OPEN_SQUARE:
3564 ++square_depth;
3565 break;
3566 case CPP_CLOSE_SQUARE:
3567 if (!square_depth--)
3568 return 0;
3569 break;
3571 case CPP_SEMICOLON:
3572 /* This matches the processing in skip_to_end_of_statement. */
3573 if (!brace_depth)
3574 return 0;
3575 break;
3577 case CPP_OPEN_BRACE:
3578 ++brace_depth;
3579 break;
3580 case CPP_CLOSE_BRACE:
3581 if (!brace_depth--)
3582 return 0;
3583 break;
3585 case CPP_OPEN_PAREN:
3586 if (!brace_depth)
3587 ++paren_depth;
3588 break;
3590 case CPP_CLOSE_PAREN:
3591 if (!brace_depth && !paren_depth--)
3593 if (consume_paren)
3594 cp_lexer_consume_token (parser->lexer);
3595 return 1;
3597 break;
3599 case CPP_QUERY:
3600 if (!brace_depth && !paren_depth && !square_depth)
3601 ++condop_depth;
3602 break;
3604 case CPP_COLON:
3605 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3606 condop_depth--;
3607 break;
3609 default:
3610 break;
3613 /* Consume the token. */
3614 cp_lexer_consume_token (parser->lexer);
3618 /* Consume tokens up to, and including, the next non-nested closing `)'.
3619 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3620 are doing error recovery. Returns -1 if OR_COMMA is true and we
3621 found an unnested token of that type. */
3623 static int
3624 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3625 bool recovering,
3626 bool or_comma,
3627 bool consume_paren)
3629 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3630 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3631 ttype, consume_paren);
3634 /* Consume tokens until we reach the end of the current statement.
3635 Normally, that will be just before consuming a `;'. However, if a
3636 non-nested `}' comes first, then we stop before consuming that. */
3638 static void
3639 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3641 unsigned nesting_depth = 0;
3643 /* Unwind generic function template scope if necessary. */
3644 if (parser->fully_implicit_function_template_p)
3645 abort_fully_implicit_template (parser);
3647 while (true)
3649 cp_token *token = cp_lexer_peek_token (parser->lexer);
3651 switch (token->type)
3653 case CPP_EOF:
3654 case CPP_PRAGMA_EOL:
3655 /* If we've run out of tokens, stop. */
3656 return;
3658 case CPP_SEMICOLON:
3659 /* If the next token is a `;', we have reached the end of the
3660 statement. */
3661 if (!nesting_depth)
3662 return;
3663 break;
3665 case CPP_CLOSE_BRACE:
3666 /* If this is a non-nested '}', stop before consuming it.
3667 That way, when confronted with something like:
3669 { 3 + }
3671 we stop before consuming the closing '}', even though we
3672 have not yet reached a `;'. */
3673 if (nesting_depth == 0)
3674 return;
3676 /* If it is the closing '}' for a block that we have
3677 scanned, stop -- but only after consuming the token.
3678 That way given:
3680 void f g () { ... }
3681 typedef int I;
3683 we will stop after the body of the erroneously declared
3684 function, but before consuming the following `typedef'
3685 declaration. */
3686 if (--nesting_depth == 0)
3688 cp_lexer_consume_token (parser->lexer);
3689 return;
3691 break;
3693 case CPP_OPEN_BRACE:
3694 ++nesting_depth;
3695 break;
3697 default:
3698 break;
3701 /* Consume the token. */
3702 cp_lexer_consume_token (parser->lexer);
3706 /* This function is called at the end of a statement or declaration.
3707 If the next token is a semicolon, it is consumed; otherwise, error
3708 recovery is attempted. */
3710 static void
3711 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3713 /* Look for the trailing `;'. */
3714 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3716 /* If there is additional (erroneous) input, skip to the end of
3717 the statement. */
3718 cp_parser_skip_to_end_of_statement (parser);
3719 /* If the next token is now a `;', consume it. */
3720 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3721 cp_lexer_consume_token (parser->lexer);
3725 /* Skip tokens until we have consumed an entire block, or until we
3726 have consumed a non-nested `;'. */
3728 static void
3729 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3731 int nesting_depth = 0;
3733 /* Unwind generic function template scope if necessary. */
3734 if (parser->fully_implicit_function_template_p)
3735 abort_fully_implicit_template (parser);
3737 while (nesting_depth >= 0)
3739 cp_token *token = cp_lexer_peek_token (parser->lexer);
3741 switch (token->type)
3743 case CPP_EOF:
3744 case CPP_PRAGMA_EOL:
3745 /* If we've run out of tokens, stop. */
3746 return;
3748 case CPP_SEMICOLON:
3749 /* Stop if this is an unnested ';'. */
3750 if (!nesting_depth)
3751 nesting_depth = -1;
3752 break;
3754 case CPP_CLOSE_BRACE:
3755 /* Stop if this is an unnested '}', or closes the outermost
3756 nesting level. */
3757 nesting_depth--;
3758 if (nesting_depth < 0)
3759 return;
3760 if (!nesting_depth)
3761 nesting_depth = -1;
3762 break;
3764 case CPP_OPEN_BRACE:
3765 /* Nest. */
3766 nesting_depth++;
3767 break;
3769 default:
3770 break;
3773 /* Consume the token. */
3774 cp_lexer_consume_token (parser->lexer);
3778 /* Skip tokens until a non-nested closing curly brace is the next
3779 token, or there are no more tokens. Return true in the first case,
3780 false otherwise. */
3782 static bool
3783 cp_parser_skip_to_closing_brace (cp_parser *parser)
3785 unsigned nesting_depth = 0;
3787 while (true)
3789 cp_token *token = cp_lexer_peek_token (parser->lexer);
3791 switch (token->type)
3793 case CPP_EOF:
3794 case CPP_PRAGMA_EOL:
3795 /* If we've run out of tokens, stop. */
3796 return false;
3798 case CPP_CLOSE_BRACE:
3799 /* If the next token is a non-nested `}', then we have reached
3800 the end of the current block. */
3801 if (nesting_depth-- == 0)
3802 return true;
3803 break;
3805 case CPP_OPEN_BRACE:
3806 /* If it the next token is a `{', then we are entering a new
3807 block. Consume the entire block. */
3808 ++nesting_depth;
3809 break;
3811 default:
3812 break;
3815 /* Consume the token. */
3816 cp_lexer_consume_token (parser->lexer);
3820 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3821 parameter is the PRAGMA token, allowing us to purge the entire pragma
3822 sequence. */
3824 static void
3825 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3827 cp_token *token;
3829 parser->lexer->in_pragma = false;
3832 token = cp_lexer_consume_token (parser->lexer);
3833 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3835 /* Ensure that the pragma is not parsed again. */
3836 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3839 /* Require pragma end of line, resyncing with it as necessary. The
3840 arguments are as for cp_parser_skip_to_pragma_eol. */
3842 static void
3843 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3845 parser->lexer->in_pragma = false;
3846 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3847 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3850 /* This is a simple wrapper around make_typename_type. When the id is
3851 an unresolved identifier node, we can provide a superior diagnostic
3852 using cp_parser_diagnose_invalid_type_name. */
3854 static tree
3855 cp_parser_make_typename_type (cp_parser *parser, tree id,
3856 location_t id_location)
3858 tree result;
3859 if (identifier_p (id))
3861 result = make_typename_type (parser->scope, id, typename_type,
3862 /*complain=*/tf_none);
3863 if (result == error_mark_node)
3864 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3865 return result;
3867 return make_typename_type (parser->scope, id, typename_type, tf_error);
3870 /* This is a wrapper around the
3871 make_{pointer,ptrmem,reference}_declarator functions that decides
3872 which one to call based on the CODE and CLASS_TYPE arguments. The
3873 CODE argument should be one of the values returned by
3874 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3875 appertain to the pointer or reference. */
3877 static cp_declarator *
3878 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3879 cp_cv_quals cv_qualifiers,
3880 cp_declarator *target,
3881 tree attributes)
3883 if (code == ERROR_MARK || target == cp_error_declarator)
3884 return cp_error_declarator;
3886 if (code == INDIRECT_REF)
3887 if (class_type == NULL_TREE)
3888 return make_pointer_declarator (cv_qualifiers, target, attributes);
3889 else
3890 return make_ptrmem_declarator (cv_qualifiers, class_type,
3891 target, attributes);
3892 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3893 return make_reference_declarator (cv_qualifiers, target,
3894 false, attributes);
3895 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3896 return make_reference_declarator (cv_qualifiers, target,
3897 true, attributes);
3898 gcc_unreachable ();
3901 /* Create a new C++ parser. */
3903 static cp_parser *
3904 cp_parser_new (void)
3906 cp_parser *parser;
3907 cp_lexer *lexer;
3908 unsigned i;
3910 /* cp_lexer_new_main is called before doing GC allocation because
3911 cp_lexer_new_main might load a PCH file. */
3912 lexer = cp_lexer_new_main ();
3914 /* Initialize the binops_by_token so that we can get the tree
3915 directly from the token. */
3916 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3917 binops_by_token[binops[i].token_type] = binops[i];
3919 parser = ggc_cleared_alloc<cp_parser> ();
3920 parser->lexer = lexer;
3921 parser->context = cp_parser_context_new (NULL);
3923 /* For now, we always accept GNU extensions. */
3924 parser->allow_gnu_extensions_p = 1;
3926 /* The `>' token is a greater-than operator, not the end of a
3927 template-id. */
3928 parser->greater_than_is_operator_p = true;
3930 parser->default_arg_ok_p = true;
3932 /* We are not parsing a constant-expression. */
3933 parser->integral_constant_expression_p = false;
3934 parser->allow_non_integral_constant_expression_p = false;
3935 parser->non_integral_constant_expression_p = false;
3937 /* Local variable names are not forbidden. */
3938 parser->local_variables_forbidden_p = false;
3940 /* We are not processing an `extern "C"' declaration. */
3941 parser->in_unbraced_linkage_specification_p = false;
3943 /* We are not processing a declarator. */
3944 parser->in_declarator_p = false;
3946 /* We are not processing a template-argument-list. */
3947 parser->in_template_argument_list_p = false;
3949 /* We are not in an iteration statement. */
3950 parser->in_statement = 0;
3952 /* We are not in a switch statement. */
3953 parser->in_switch_statement_p = false;
3955 /* We are not parsing a type-id inside an expression. */
3956 parser->in_type_id_in_expr_p = false;
3958 /* String literals should be translated to the execution character set. */
3959 parser->translate_strings_p = true;
3961 /* We are not parsing a function body. */
3962 parser->in_function_body = false;
3964 /* We can correct until told otherwise. */
3965 parser->colon_corrects_to_scope_p = true;
3967 /* The unparsed function queue is empty. */
3968 push_unparsed_function_queues (parser);
3970 /* There are no classes being defined. */
3971 parser->num_classes_being_defined = 0;
3973 /* No template parameters apply. */
3974 parser->num_template_parameter_lists = 0;
3976 /* Special parsing data structures. */
3977 parser->omp_declare_simd = NULL;
3978 parser->oacc_routine = NULL;
3980 /* Not declaring an implicit function template. */
3981 parser->auto_is_implicit_function_template_parm_p = false;
3982 parser->fully_implicit_function_template_p = false;
3983 parser->implicit_template_parms = 0;
3984 parser->implicit_template_scope = 0;
3986 /* Allow constrained-type-specifiers. */
3987 parser->prevent_constrained_type_specifiers = 0;
3989 /* We haven't yet seen an 'extern "C"'. */
3990 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3992 return parser;
3995 /* Create a cp_lexer structure which will emit the tokens in CACHE
3996 and push it onto the parser's lexer stack. This is used for delayed
3997 parsing of in-class method bodies and default arguments, and should
3998 not be confused with tentative parsing. */
3999 static void
4000 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4002 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4003 lexer->next = parser->lexer;
4004 parser->lexer = lexer;
4006 /* Move the current source position to that of the first token in the
4007 new lexer. */
4008 cp_lexer_set_source_position_from_token (lexer->next_token);
4011 /* Pop the top lexer off the parser stack. This is never used for the
4012 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4013 static void
4014 cp_parser_pop_lexer (cp_parser *parser)
4016 cp_lexer *lexer = parser->lexer;
4017 parser->lexer = lexer->next;
4018 cp_lexer_destroy (lexer);
4020 /* Put the current source position back where it was before this
4021 lexer was pushed. */
4022 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4025 /* Lexical conventions [gram.lex] */
4027 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4028 identifier. */
4030 static cp_expr
4031 cp_parser_identifier (cp_parser* parser)
4033 cp_token *token;
4035 /* Look for the identifier. */
4036 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4037 /* Return the value. */
4038 if (token)
4039 return cp_expr (token->u.value, token->location);
4040 else
4041 return error_mark_node;
4044 /* Parse a sequence of adjacent string constants. Returns a
4045 TREE_STRING representing the combined, nul-terminated string
4046 constant. If TRANSLATE is true, translate the string to the
4047 execution character set. If WIDE_OK is true, a wide string is
4048 invalid here.
4050 C++98 [lex.string] says that if a narrow string literal token is
4051 adjacent to a wide string literal token, the behavior is undefined.
4052 However, C99 6.4.5p4 says that this results in a wide string literal.
4053 We follow C99 here, for consistency with the C front end.
4055 This code is largely lifted from lex_string() in c-lex.c.
4057 FUTURE: ObjC++ will need to handle @-strings here. */
4058 static cp_expr
4059 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4060 bool lookup_udlit = true)
4062 tree value;
4063 size_t count;
4064 struct obstack str_ob;
4065 struct obstack loc_ob;
4066 cpp_string str, istr, *strs;
4067 cp_token *tok;
4068 enum cpp_ttype type, curr_type;
4069 int have_suffix_p = 0;
4070 tree string_tree;
4071 tree suffix_id = NULL_TREE;
4072 bool curr_tok_is_userdef_p = false;
4074 tok = cp_lexer_peek_token (parser->lexer);
4075 if (!cp_parser_is_string_literal (tok))
4077 cp_parser_error (parser, "expected string-literal");
4078 return error_mark_node;
4081 location_t loc = tok->location;
4083 if (cpp_userdef_string_p (tok->type))
4085 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4086 curr_type = cpp_userdef_string_remove_type (tok->type);
4087 curr_tok_is_userdef_p = true;
4089 else
4091 string_tree = tok->u.value;
4092 curr_type = tok->type;
4094 type = curr_type;
4096 /* Try to avoid the overhead of creating and destroying an obstack
4097 for the common case of just one string. */
4098 if (!cp_parser_is_string_literal
4099 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4101 cp_lexer_consume_token (parser->lexer);
4103 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4104 str.len = TREE_STRING_LENGTH (string_tree);
4105 count = 1;
4107 if (curr_tok_is_userdef_p)
4109 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4110 have_suffix_p = 1;
4111 curr_type = cpp_userdef_string_remove_type (tok->type);
4113 else
4114 curr_type = tok->type;
4116 strs = &str;
4118 else
4120 location_t last_tok_loc = tok->location;
4121 gcc_obstack_init (&str_ob);
4122 gcc_obstack_init (&loc_ob);
4123 count = 0;
4127 cp_lexer_consume_token (parser->lexer);
4128 count++;
4129 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4130 str.len = TREE_STRING_LENGTH (string_tree);
4132 if (curr_tok_is_userdef_p)
4134 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4135 if (have_suffix_p == 0)
4137 suffix_id = curr_suffix_id;
4138 have_suffix_p = 1;
4140 else if (have_suffix_p == 1
4141 && curr_suffix_id != suffix_id)
4143 error ("inconsistent user-defined literal suffixes"
4144 " %qD and %qD in string literal",
4145 suffix_id, curr_suffix_id);
4146 have_suffix_p = -1;
4148 curr_type = cpp_userdef_string_remove_type (tok->type);
4150 else
4151 curr_type = tok->type;
4153 if (type != curr_type)
4155 if (type == CPP_STRING)
4156 type = curr_type;
4157 else if (curr_type != CPP_STRING)
4159 rich_location rich_loc (line_table, tok->location);
4160 rich_loc.add_range (last_tok_loc);
4161 error_at (&rich_loc,
4162 "unsupported non-standard concatenation "
4163 "of string literals");
4167 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4168 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4170 last_tok_loc = tok->location;
4172 tok = cp_lexer_peek_token (parser->lexer);
4173 if (cpp_userdef_string_p (tok->type))
4175 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4176 curr_type = cpp_userdef_string_remove_type (tok->type);
4177 curr_tok_is_userdef_p = true;
4179 else
4181 string_tree = tok->u.value;
4182 curr_type = tok->type;
4183 curr_tok_is_userdef_p = false;
4186 while (cp_parser_is_string_literal (tok));
4188 /* A string literal built by concatenation has its caret=start at
4189 the start of the initial string, and its finish at the finish of
4190 the final string literal. */
4191 loc = make_location (loc, loc, get_finish (last_tok_loc));
4193 strs = (cpp_string *) obstack_finish (&str_ob);
4196 if (type != CPP_STRING && !wide_ok)
4198 cp_parser_error (parser, "a wide string is invalid in this context");
4199 type = CPP_STRING;
4202 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4203 (parse_in, strs, count, &istr, type))
4205 value = build_string (istr.len, (const char *)istr.text);
4206 free (CONST_CAST (unsigned char *, istr.text));
4207 if (count > 1)
4209 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4210 gcc_assert (g_string_concat_db);
4211 g_string_concat_db->record_string_concatenation (count, locs);
4214 switch (type)
4216 default:
4217 case CPP_STRING:
4218 case CPP_UTF8STRING:
4219 TREE_TYPE (value) = char_array_type_node;
4220 break;
4221 case CPP_STRING16:
4222 TREE_TYPE (value) = char16_array_type_node;
4223 break;
4224 case CPP_STRING32:
4225 TREE_TYPE (value) = char32_array_type_node;
4226 break;
4227 case CPP_WSTRING:
4228 TREE_TYPE (value) = wchar_array_type_node;
4229 break;
4232 value = fix_string_type (value);
4234 if (have_suffix_p)
4236 tree literal = build_userdef_literal (suffix_id, value,
4237 OT_NONE, NULL_TREE);
4238 if (lookup_udlit)
4239 value = cp_parser_userdef_string_literal (literal);
4240 else
4241 value = literal;
4244 else
4245 /* cpp_interpret_string has issued an error. */
4246 value = error_mark_node;
4248 if (count > 1)
4250 obstack_free (&str_ob, 0);
4251 obstack_free (&loc_ob, 0);
4254 return cp_expr (value, loc);
4257 /* Look up a literal operator with the name and the exact arguments. */
4259 static tree
4260 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4262 tree 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 tree fn = *iter;
4270 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4272 unsigned int ix;
4273 bool found = true;
4275 for (ix = 0;
4276 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4277 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4279 tree tparm = TREE_VALUE (parmtypes);
4280 tree targ = TREE_TYPE ((*args)[ix]);
4281 bool ptr = TYPE_PTR_P (tparm);
4282 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4283 if ((ptr || arr || !same_type_p (tparm, targ))
4284 && (!ptr || !arr
4285 || !same_type_p (TREE_TYPE (tparm),
4286 TREE_TYPE (targ))))
4287 found = false;
4290 if (found
4291 && ix == vec_safe_length (args)
4292 /* May be this should be sufficient_parms_p instead,
4293 depending on how exactly should user-defined literals
4294 work in presence of default arguments on the literal
4295 operator parameters. */
4296 && parmtypes == void_list_node)
4297 return decl;
4301 return error_mark_node;
4304 /* Parse a user-defined char constant. Returns a call to a user-defined
4305 literal operator taking the character as an argument. */
4307 static cp_expr
4308 cp_parser_userdef_char_literal (cp_parser *parser)
4310 cp_token *token = cp_lexer_consume_token (parser->lexer);
4311 tree literal = token->u.value;
4312 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4313 tree value = USERDEF_LITERAL_VALUE (literal);
4314 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4315 tree decl, result;
4317 /* Build up a call to the user-defined operator */
4318 /* Lookup the name we got back from the id-expression. */
4319 vec<tree, va_gc> *args = make_tree_vector ();
4320 vec_safe_push (args, value);
4321 decl = lookup_literal_operator (name, args);
4322 if (!decl || decl == error_mark_node)
4324 error ("unable to find character literal operator %qD with %qT argument",
4325 name, TREE_TYPE (value));
4326 release_tree_vector (args);
4327 return error_mark_node;
4329 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4330 release_tree_vector (args);
4331 return result;
4334 /* A subroutine of cp_parser_userdef_numeric_literal to
4335 create a char... template parameter pack from a string node. */
4337 static tree
4338 make_char_string_pack (tree value)
4340 tree charvec;
4341 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4342 const char *str = TREE_STRING_POINTER (value);
4343 int i, len = TREE_STRING_LENGTH (value) - 1;
4344 tree argvec = make_tree_vec (1);
4346 /* Fill in CHARVEC with all of the parameters. */
4347 charvec = make_tree_vec (len);
4348 for (i = 0; i < len; ++i)
4350 unsigned char s[3] = { '\'', str[i], '\'' };
4351 cpp_string in = { 3, s };
4352 cpp_string out = { 0, 0 };
4353 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4354 return NULL_TREE;
4355 gcc_assert (out.len == 2);
4356 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4357 out.text[0]);
4360 /* Build the argument packs. */
4361 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4363 TREE_VEC_ELT (argvec, 0) = argpack;
4365 return argvec;
4368 /* A subroutine of cp_parser_userdef_numeric_literal to
4369 create a char... template parameter pack from a string node. */
4371 static tree
4372 make_string_pack (tree value)
4374 tree charvec;
4375 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4376 const unsigned char *str
4377 = (const unsigned char *) TREE_STRING_POINTER (value);
4378 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4379 int len = TREE_STRING_LENGTH (value) / sz - 1;
4380 tree argvec = make_tree_vec (2);
4382 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4383 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4385 /* First template parm is character type. */
4386 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4388 /* Fill in CHARVEC with all of the parameters. */
4389 charvec = make_tree_vec (len);
4390 for (int i = 0; i < len; ++i)
4391 TREE_VEC_ELT (charvec, i)
4392 = double_int_to_tree (str_char_type_node,
4393 double_int::from_buffer (str + i * sz, sz));
4395 /* Build the argument packs. */
4396 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4398 TREE_VEC_ELT (argvec, 1) = argpack;
4400 return argvec;
4403 /* Parse a user-defined numeric constant. returns a call to a user-defined
4404 literal operator. */
4406 static cp_expr
4407 cp_parser_userdef_numeric_literal (cp_parser *parser)
4409 cp_token *token = cp_lexer_consume_token (parser->lexer);
4410 tree literal = token->u.value;
4411 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4412 tree value = USERDEF_LITERAL_VALUE (literal);
4413 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4414 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4415 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4416 tree decl, result;
4417 vec<tree, va_gc> *args;
4419 /* Look for a literal operator taking the exact type of numeric argument
4420 as the literal value. */
4421 args = make_tree_vector ();
4422 vec_safe_push (args, value);
4423 decl = lookup_literal_operator (name, args);
4424 if (decl && decl != error_mark_node)
4426 result = finish_call_expr (decl, &args, false, true,
4427 tf_warning_or_error);
4429 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4431 warning_at (token->location, OPT_Woverflow,
4432 "integer literal exceeds range of %qT type",
4433 long_long_unsigned_type_node);
4435 else
4437 if (overflow > 0)
4438 warning_at (token->location, OPT_Woverflow,
4439 "floating literal exceeds range of %qT type",
4440 long_double_type_node);
4441 else if (overflow < 0)
4442 warning_at (token->location, OPT_Woverflow,
4443 "floating literal truncated to zero");
4446 release_tree_vector (args);
4447 return result;
4449 release_tree_vector (args);
4451 /* If the numeric argument didn't work, look for a raw literal
4452 operator taking a const char* argument consisting of the number
4453 in string format. */
4454 args = make_tree_vector ();
4455 vec_safe_push (args, num_string);
4456 decl = lookup_literal_operator (name, args);
4457 if (decl && decl != error_mark_node)
4459 result = finish_call_expr (decl, &args, false, true,
4460 tf_warning_or_error);
4461 release_tree_vector (args);
4462 return result;
4464 release_tree_vector (args);
4466 /* If the raw literal didn't work, look for a non-type template
4467 function with parameter pack char.... Call the function with
4468 template parameter characters representing the number. */
4469 args = make_tree_vector ();
4470 decl = lookup_literal_operator (name, args);
4471 if (decl && decl != error_mark_node)
4473 tree tmpl_args = make_char_string_pack (num_string);
4474 if (tmpl_args == NULL_TREE)
4476 error ("failed to translate literal to execution character set %qT",
4477 num_string);
4478 return error_mark_node;
4480 decl = lookup_template_function (decl, tmpl_args);
4481 result = finish_call_expr (decl, &args, false, true,
4482 tf_warning_or_error);
4483 release_tree_vector (args);
4484 return result;
4487 release_tree_vector (args);
4489 /* In C++14 the standard library defines complex number suffixes that
4490 conflict with GNU extensions. Prefer them if <complex> is #included. */
4491 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4492 bool i14 = (cxx_dialect > cxx11
4493 && (id_equal (suffix_id, "i")
4494 || id_equal (suffix_id, "if")
4495 || id_equal (suffix_id, "il")));
4496 diagnostic_t kind = DK_ERROR;
4497 int opt = 0;
4499 if (i14 && ext)
4501 tree cxlit = lookup_qualified_name (std_node,
4502 get_identifier ("complex_literals"),
4503 0, false, false);
4504 if (cxlit == error_mark_node)
4506 /* No <complex>, so pedwarn and use GNU semantics. */
4507 kind = DK_PEDWARN;
4508 opt = OPT_Wpedantic;
4512 bool complained
4513 = emit_diagnostic (kind, input_location, opt,
4514 "unable to find numeric literal operator %qD", name);
4516 if (!complained)
4517 /* Don't inform either. */;
4518 else if (i14)
4520 inform (token->location, "add %<using namespace std::complex_literals%> "
4521 "(from <complex>) to enable the C++14 user-defined literal "
4522 "suffixes");
4523 if (ext)
4524 inform (token->location, "or use %<j%> instead of %<i%> for the "
4525 "GNU built-in suffix");
4527 else if (!ext)
4528 inform (token->location, "use -fext-numeric-literals "
4529 "to enable more built-in suffixes");
4531 if (kind == DK_ERROR)
4532 value = error_mark_node;
4533 else
4535 /* Use the built-in semantics. */
4536 tree type;
4537 if (id_equal (suffix_id, "i"))
4539 if (TREE_CODE (value) == INTEGER_CST)
4540 type = integer_type_node;
4541 else
4542 type = double_type_node;
4544 else if (id_equal (suffix_id, "if"))
4545 type = float_type_node;
4546 else /* if (id_equal (suffix_id, "il")) */
4547 type = long_double_type_node;
4549 value = build_complex (build_complex_type (type),
4550 fold_convert (type, integer_zero_node),
4551 fold_convert (type, value));
4554 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4555 /* Avoid repeated diagnostics. */
4556 token->u.value = value;
4557 return value;
4560 /* Parse a user-defined string constant. Returns a call to a user-defined
4561 literal operator taking a character pointer and the length of the string
4562 as arguments. */
4564 static tree
4565 cp_parser_userdef_string_literal (tree literal)
4567 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4568 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4569 tree value = USERDEF_LITERAL_VALUE (literal);
4570 int len = TREE_STRING_LENGTH (value)
4571 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4572 tree decl;
4574 /* Build up a call to the user-defined operator. */
4575 /* Lookup the name we got back from the id-expression. */
4576 releasing_vec rargs;
4577 vec<tree, va_gc> *&args = rargs.get_ref();
4578 vec_safe_push (args, value);
4579 vec_safe_push (args, build_int_cst (size_type_node, len));
4580 decl = lookup_literal_operator (name, args);
4582 if (decl && decl != error_mark_node)
4583 return finish_call_expr (decl, &args, false, true,
4584 tf_warning_or_error);
4586 /* Look for a suitable template function, either (C++20) with a single
4587 parameter of class type, or (N3599) with typename parameter CharT and
4588 parameter pack CharT... */
4589 args->truncate (0);
4590 decl = lookup_literal_operator (name, args);
4591 if (decl && decl != error_mark_node)
4593 /* Use resolve_nondeduced_context to try to choose one form of template
4594 or the other. */
4595 tree tmpl_args = make_tree_vec (1);
4596 TREE_VEC_ELT (tmpl_args, 0) = value;
4597 decl = lookup_template_function (decl, tmpl_args);
4598 tree res = resolve_nondeduced_context (decl, tf_none);
4599 if (DECL_P (res))
4600 decl = res;
4601 else
4603 TREE_OPERAND (decl, 1) = make_string_pack (value);
4604 res = resolve_nondeduced_context (decl, tf_none);
4605 if (DECL_P (res))
4606 decl = res;
4608 if (!DECL_P (decl) && cxx_dialect > cxx17)
4609 TREE_OPERAND (decl, 1) = tmpl_args;
4610 return finish_call_expr (decl, &args, false, true,
4611 tf_warning_or_error);
4614 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4615 name, TREE_TYPE (value), size_type_node);
4616 return error_mark_node;
4620 /* Basic concepts [gram.basic] */
4622 /* Parse a translation-unit.
4624 translation-unit:
4625 declaration-seq [opt] */
4627 static void
4628 cp_parser_translation_unit (cp_parser* parser)
4630 gcc_checking_assert (!cp_error_declarator);
4632 /* Create the declarator obstack. */
4633 gcc_obstack_init (&declarator_obstack);
4634 /* Create the error declarator. */
4635 cp_error_declarator = make_declarator (cdk_error);
4636 /* Create the empty parameter list. */
4637 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4638 UNKNOWN_LOCATION);
4639 /* Remember where the base of the declarator obstack lies. */
4640 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4642 bool implicit_extern_c = false;
4644 for (;;)
4646 cp_token *token = cp_lexer_peek_token (parser->lexer);
4648 /* If we're entering or exiting a region that's implicitly
4649 extern "C", modify the lang context appropriately. */
4650 if (implicit_extern_c
4651 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4653 implicit_extern_c = !implicit_extern_c;
4654 if (implicit_extern_c)
4655 push_lang_context (lang_name_c);
4656 else
4657 pop_lang_context ();
4660 if (token->type == CPP_EOF)
4661 break;
4663 if (token->type == CPP_CLOSE_BRACE)
4665 cp_parser_error (parser, "expected declaration");
4666 cp_lexer_consume_token (parser->lexer);
4667 /* If the next token is now a `;', consume it. */
4668 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4669 cp_lexer_consume_token (parser->lexer);
4671 else
4672 cp_parser_toplevel_declaration (parser);
4675 /* Get rid of the token array; we don't need it any more. */
4676 cp_lexer_destroy (parser->lexer);
4677 parser->lexer = NULL;
4679 /* The EOF should have reset this. */
4680 gcc_checking_assert (!implicit_extern_c);
4682 /* Make sure the declarator obstack was fully cleaned up. */
4683 gcc_assert (obstack_next_free (&declarator_obstack)
4684 == declarator_obstack_base);
4687 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4688 decltype context. */
4690 static inline tsubst_flags_t
4691 complain_flags (bool decltype_p)
4693 tsubst_flags_t complain = tf_warning_or_error;
4694 if (decltype_p)
4695 complain |= tf_decltype;
4696 return complain;
4699 /* We're about to parse a collection of statements. If we're currently
4700 parsing tentatively, set up a firewall so that any nested
4701 cp_parser_commit_to_tentative_parse won't affect the current context. */
4703 static cp_token_position
4704 cp_parser_start_tentative_firewall (cp_parser *parser)
4706 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4707 return 0;
4709 cp_parser_parse_tentatively (parser);
4710 cp_parser_commit_to_topmost_tentative_parse (parser);
4711 return cp_lexer_token_position (parser->lexer, false);
4714 /* We've finished parsing the collection of statements. Wrap up the
4715 firewall and replace the relevant tokens with the parsed form. */
4717 static void
4718 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4719 tree expr)
4721 if (!start)
4722 return;
4724 /* Finish the firewall level. */
4725 cp_parser_parse_definitely (parser);
4726 /* And remember the result of the parse for when we try again. */
4727 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4728 token->type = CPP_PREPARSED_EXPR;
4729 token->u.value = expr;
4730 token->keyword = RID_MAX;
4731 cp_lexer_purge_tokens_after (parser->lexer, start);
4734 /* Like the above functions, but let the user modify the tokens. Used by
4735 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4736 later parses, so it makes sense to localize the effects of
4737 cp_parser_commit_to_tentative_parse. */
4739 struct tentative_firewall
4741 cp_parser *parser;
4742 bool set;
4744 tentative_firewall (cp_parser *p): parser(p)
4746 /* If we're currently parsing tentatively, start a committed level as a
4747 firewall and then an inner tentative parse. */
4748 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4750 cp_parser_parse_tentatively (parser);
4751 cp_parser_commit_to_topmost_tentative_parse (parser);
4752 cp_parser_parse_tentatively (parser);
4756 ~tentative_firewall()
4758 if (set)
4760 /* Finish the inner tentative parse and the firewall, propagating any
4761 uncommitted error state to the outer tentative parse. */
4762 bool err = cp_parser_error_occurred (parser);
4763 cp_parser_parse_definitely (parser);
4764 cp_parser_parse_definitely (parser);
4765 if (err)
4766 cp_parser_simulate_error (parser);
4771 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4772 This class is for tracking such a matching pair of symbols.
4773 In particular, it tracks the location of the first token,
4774 so that if the second token is missing, we can highlight the
4775 location of the first token when notifying the user about the
4776 problem. */
4778 template <typename traits_t>
4779 class token_pair
4781 public:
4782 /* token_pair's ctor. */
4783 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4785 /* If the next token is the opening symbol for this pair, consume it and
4786 return true.
4787 Otherwise, issue an error and return false.
4788 In either case, record the location of the opening token. */
4790 bool require_open (cp_parser *parser)
4792 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4793 return cp_parser_require (parser, traits_t::open_token_type,
4794 traits_t::required_token_open);
4797 /* Consume the next token from PARSER, recording its location as
4798 that of the opening token within the pair. */
4800 cp_token * consume_open (cp_parser *parser)
4802 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4803 gcc_assert (tok->type == traits_t::open_token_type);
4804 m_open_loc = tok->location;
4805 return tok;
4808 /* If the next token is the closing symbol for this pair, consume it
4809 and return it.
4810 Otherwise, issue an error, highlighting the location of the
4811 corresponding opening token, and return NULL. */
4813 cp_token *require_close (cp_parser *parser) const
4815 return cp_parser_require (parser, traits_t::close_token_type,
4816 traits_t::required_token_close,
4817 m_open_loc);
4820 private:
4821 location_t m_open_loc;
4824 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4826 struct matching_paren_traits
4828 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4829 static const enum required_token required_token_open = RT_OPEN_PAREN;
4830 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4831 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4834 /* "matching_parens" is a token_pair<T> class for tracking matching
4835 pairs of parentheses. */
4837 typedef token_pair<matching_paren_traits> matching_parens;
4839 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4841 struct matching_brace_traits
4843 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4844 static const enum required_token required_token_open = RT_OPEN_BRACE;
4845 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4846 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4849 /* "matching_braces" is a token_pair<T> class for tracking matching
4850 pairs of braces. */
4852 typedef token_pair<matching_brace_traits> matching_braces;
4855 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4856 enclosing parentheses. */
4858 static cp_expr
4859 cp_parser_statement_expr (cp_parser *parser)
4861 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4863 /* Consume the '('. */
4864 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4865 matching_parens parens;
4866 parens.consume_open (parser);
4867 /* Start the statement-expression. */
4868 tree expr = begin_stmt_expr ();
4869 /* Parse the compound-statement. */
4870 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4871 /* Finish up. */
4872 expr = finish_stmt_expr (expr, false);
4873 /* Consume the ')'. */
4874 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4875 if (!parens.require_close (parser))
4876 cp_parser_skip_to_end_of_statement (parser);
4878 cp_parser_end_tentative_firewall (parser, start, expr);
4879 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4880 return cp_expr (expr, combined_loc);
4883 /* Expressions [gram.expr] */
4885 /* Parse a fold-operator.
4887 fold-operator:
4888 - * / % ^ & | = < > << >>
4889 = -= *= /= %= ^= &= |= <<= >>=
4890 == != <= >= && || , .* ->*
4892 This returns the tree code corresponding to the matched operator
4893 as an int. When the current token matches a compound assignment
4894 opertor, the resulting tree code is the negative value of the
4895 non-assignment operator. */
4897 static int
4898 cp_parser_fold_operator (cp_token *token)
4900 switch (token->type)
4902 case CPP_PLUS: return PLUS_EXPR;
4903 case CPP_MINUS: return MINUS_EXPR;
4904 case CPP_MULT: return MULT_EXPR;
4905 case CPP_DIV: return TRUNC_DIV_EXPR;
4906 case CPP_MOD: return TRUNC_MOD_EXPR;
4907 case CPP_XOR: return BIT_XOR_EXPR;
4908 case CPP_AND: return BIT_AND_EXPR;
4909 case CPP_OR: return BIT_IOR_EXPR;
4910 case CPP_LSHIFT: return LSHIFT_EXPR;
4911 case CPP_RSHIFT: return RSHIFT_EXPR;
4913 case CPP_EQ: return -NOP_EXPR;
4914 case CPP_PLUS_EQ: return -PLUS_EXPR;
4915 case CPP_MINUS_EQ: return -MINUS_EXPR;
4916 case CPP_MULT_EQ: return -MULT_EXPR;
4917 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4918 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4919 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4920 case CPP_AND_EQ: return -BIT_AND_EXPR;
4921 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4922 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4923 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4925 case CPP_EQ_EQ: return EQ_EXPR;
4926 case CPP_NOT_EQ: return NE_EXPR;
4927 case CPP_LESS: return LT_EXPR;
4928 case CPP_GREATER: return GT_EXPR;
4929 case CPP_LESS_EQ: return LE_EXPR;
4930 case CPP_GREATER_EQ: return GE_EXPR;
4932 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4933 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4935 case CPP_COMMA: return COMPOUND_EXPR;
4937 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4938 case CPP_DEREF_STAR: return MEMBER_REF;
4940 default: return ERROR_MARK;
4944 /* Returns true if CODE indicates a binary expression, which is not allowed in
4945 the LHS of a fold-expression. More codes will need to be added to use this
4946 function in other contexts. */
4948 static bool
4949 is_binary_op (tree_code code)
4951 switch (code)
4953 case PLUS_EXPR:
4954 case POINTER_PLUS_EXPR:
4955 case MINUS_EXPR:
4956 case MULT_EXPR:
4957 case TRUNC_DIV_EXPR:
4958 case TRUNC_MOD_EXPR:
4959 case BIT_XOR_EXPR:
4960 case BIT_AND_EXPR:
4961 case BIT_IOR_EXPR:
4962 case LSHIFT_EXPR:
4963 case RSHIFT_EXPR:
4965 case MODOP_EXPR:
4967 case EQ_EXPR:
4968 case NE_EXPR:
4969 case LE_EXPR:
4970 case GE_EXPR:
4971 case LT_EXPR:
4972 case GT_EXPR:
4974 case TRUTH_ANDIF_EXPR:
4975 case TRUTH_ORIF_EXPR:
4977 case COMPOUND_EXPR:
4979 case DOTSTAR_EXPR:
4980 case MEMBER_REF:
4981 return true;
4983 default:
4984 return false;
4988 /* If the next token is a suitable fold operator, consume it and return as
4989 the function above. */
4991 static int
4992 cp_parser_fold_operator (cp_parser *parser)
4994 cp_token* token = cp_lexer_peek_token (parser->lexer);
4995 int code = cp_parser_fold_operator (token);
4996 if (code != ERROR_MARK)
4997 cp_lexer_consume_token (parser->lexer);
4998 return code;
5001 /* Parse a fold-expression.
5003 fold-expression:
5004 ( ... folding-operator cast-expression)
5005 ( cast-expression folding-operator ... )
5006 ( cast-expression folding operator ... folding-operator cast-expression)
5008 Note that the '(' and ')' are matched in primary expression. */
5010 static cp_expr
5011 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5013 cp_id_kind pidk;
5015 // Left fold.
5016 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5018 cp_lexer_consume_token (parser->lexer);
5019 int op = cp_parser_fold_operator (parser);
5020 if (op == ERROR_MARK)
5022 cp_parser_error (parser, "expected binary operator");
5023 return error_mark_node;
5026 tree expr = cp_parser_cast_expression (parser, false, false,
5027 false, &pidk);
5028 if (expr == error_mark_node)
5029 return error_mark_node;
5030 return finish_left_unary_fold_expr (expr, op);
5033 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5034 int op = cp_parser_fold_operator (parser);
5035 if (op == ERROR_MARK)
5037 cp_parser_error (parser, "expected binary operator");
5038 return error_mark_node;
5041 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5043 cp_parser_error (parser, "expected ...");
5044 return error_mark_node;
5046 cp_lexer_consume_token (parser->lexer);
5048 /* The operands of a fold-expression are cast-expressions, so binary or
5049 conditional expressions are not allowed. We check this here to avoid
5050 tentative parsing. */
5051 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5052 /* OK, the expression was parenthesized. */;
5053 else if (is_binary_op (TREE_CODE (expr1)))
5054 error_at (location_of (expr1),
5055 "binary expression in operand of fold-expression");
5056 else if (TREE_CODE (expr1) == COND_EXPR
5057 || (REFERENCE_REF_P (expr1)
5058 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5059 error_at (location_of (expr1),
5060 "conditional expression in operand of fold-expression");
5062 // Right fold.
5063 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5064 return finish_right_unary_fold_expr (expr1, op);
5066 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5068 cp_parser_error (parser, "mismatched operator in fold-expression");
5069 return error_mark_node;
5071 cp_lexer_consume_token (parser->lexer);
5073 // Binary left or right fold.
5074 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5075 if (expr2 == error_mark_node)
5076 return error_mark_node;
5077 return finish_binary_fold_expr (expr1, expr2, op);
5080 /* Parse a primary-expression.
5082 primary-expression:
5083 literal
5084 this
5085 ( expression )
5086 id-expression
5087 lambda-expression (C++11)
5089 GNU Extensions:
5091 primary-expression:
5092 ( compound-statement )
5093 __builtin_va_arg ( assignment-expression , type-id )
5094 __builtin_offsetof ( type-id , offsetof-expression )
5096 C++ Extensions:
5097 __has_nothrow_assign ( type-id )
5098 __has_nothrow_constructor ( type-id )
5099 __has_nothrow_copy ( type-id )
5100 __has_trivial_assign ( type-id )
5101 __has_trivial_constructor ( type-id )
5102 __has_trivial_copy ( type-id )
5103 __has_trivial_destructor ( type-id )
5104 __has_virtual_destructor ( type-id )
5105 __is_abstract ( type-id )
5106 __is_base_of ( type-id , type-id )
5107 __is_class ( type-id )
5108 __is_empty ( type-id )
5109 __is_enum ( type-id )
5110 __is_final ( type-id )
5111 __is_literal_type ( type-id )
5112 __is_pod ( type-id )
5113 __is_polymorphic ( type-id )
5114 __is_std_layout ( type-id )
5115 __is_trivial ( type-id )
5116 __is_union ( type-id )
5118 Objective-C++ Extension:
5120 primary-expression:
5121 objc-expression
5123 literal:
5124 __null
5126 ADDRESS_P is true iff this expression was immediately preceded by
5127 "&" and therefore might denote a pointer-to-member. CAST_P is true
5128 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5129 true iff this expression is a template argument.
5131 Returns a representation of the expression. Upon return, *IDK
5132 indicates what kind of id-expression (if any) was present. */
5134 static cp_expr
5135 cp_parser_primary_expression (cp_parser *parser,
5136 bool address_p,
5137 bool cast_p,
5138 bool template_arg_p,
5139 bool decltype_p,
5140 cp_id_kind *idk)
5142 cp_token *token = NULL;
5144 /* Assume the primary expression is not an id-expression. */
5145 *idk = CP_ID_KIND_NONE;
5147 /* Peek at the next token. */
5148 token = cp_lexer_peek_token (parser->lexer);
5149 switch ((int) token->type)
5151 /* literal:
5152 integer-literal
5153 character-literal
5154 floating-literal
5155 string-literal
5156 boolean-literal
5157 pointer-literal
5158 user-defined-literal */
5159 case CPP_CHAR:
5160 case CPP_CHAR16:
5161 case CPP_CHAR32:
5162 case CPP_WCHAR:
5163 case CPP_UTF8CHAR:
5164 case CPP_NUMBER:
5165 case CPP_PREPARSED_EXPR:
5166 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5167 return cp_parser_userdef_numeric_literal (parser);
5168 token = cp_lexer_consume_token (parser->lexer);
5169 if (TREE_CODE (token->u.value) == FIXED_CST)
5171 error_at (token->location,
5172 "fixed-point types not supported in C++");
5173 return error_mark_node;
5175 /* Floating-point literals are only allowed in an integral
5176 constant expression if they are cast to an integral or
5177 enumeration type. */
5178 if (TREE_CODE (token->u.value) == REAL_CST
5179 && parser->integral_constant_expression_p
5180 && pedantic)
5182 /* CAST_P will be set even in invalid code like "int(2.7 +
5183 ...)". Therefore, we have to check that the next token
5184 is sure to end the cast. */
5185 if (cast_p)
5187 cp_token *next_token;
5189 next_token = cp_lexer_peek_token (parser->lexer);
5190 if (/* The comma at the end of an
5191 enumerator-definition. */
5192 next_token->type != CPP_COMMA
5193 /* The curly brace at the end of an enum-specifier. */
5194 && next_token->type != CPP_CLOSE_BRACE
5195 /* The end of a statement. */
5196 && next_token->type != CPP_SEMICOLON
5197 /* The end of the cast-expression. */
5198 && next_token->type != CPP_CLOSE_PAREN
5199 /* The end of an array bound. */
5200 && next_token->type != CPP_CLOSE_SQUARE
5201 /* The closing ">" in a template-argument-list. */
5202 && (next_token->type != CPP_GREATER
5203 || parser->greater_than_is_operator_p)
5204 /* C++0x only: A ">>" treated like two ">" tokens,
5205 in a template-argument-list. */
5206 && (next_token->type != CPP_RSHIFT
5207 || (cxx_dialect == cxx98)
5208 || parser->greater_than_is_operator_p))
5209 cast_p = false;
5212 /* If we are within a cast, then the constraint that the
5213 cast is to an integral or enumeration type will be
5214 checked at that point. If we are not within a cast, then
5215 this code is invalid. */
5216 if (!cast_p)
5217 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5219 return cp_expr (token->u.value, token->location);
5221 case CPP_CHAR_USERDEF:
5222 case CPP_CHAR16_USERDEF:
5223 case CPP_CHAR32_USERDEF:
5224 case CPP_WCHAR_USERDEF:
5225 case CPP_UTF8CHAR_USERDEF:
5226 return cp_parser_userdef_char_literal (parser);
5228 case CPP_STRING:
5229 case CPP_STRING16:
5230 case CPP_STRING32:
5231 case CPP_WSTRING:
5232 case CPP_UTF8STRING:
5233 case CPP_STRING_USERDEF:
5234 case CPP_STRING16_USERDEF:
5235 case CPP_STRING32_USERDEF:
5236 case CPP_WSTRING_USERDEF:
5237 case CPP_UTF8STRING_USERDEF:
5238 /* ??? Should wide strings be allowed when parser->translate_strings_p
5239 is false (i.e. in attributes)? If not, we can kill the third
5240 argument to cp_parser_string_literal. */
5241 return cp_parser_string_literal (parser,
5242 parser->translate_strings_p,
5243 true);
5245 case CPP_OPEN_PAREN:
5246 /* If we see `( { ' then we are looking at the beginning of
5247 a GNU statement-expression. */
5248 if (cp_parser_allow_gnu_extensions_p (parser)
5249 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5251 /* Statement-expressions are not allowed by the standard. */
5252 pedwarn (token->location, OPT_Wpedantic,
5253 "ISO C++ forbids braced-groups within expressions");
5255 /* And they're not allowed outside of a function-body; you
5256 cannot, for example, write:
5258 int i = ({ int j = 3; j + 1; });
5260 at class or namespace scope. */
5261 if (!parser->in_function_body
5262 || parser->in_template_argument_list_p)
5264 error_at (token->location,
5265 "statement-expressions are not allowed outside "
5266 "functions nor in template-argument lists");
5267 cp_parser_skip_to_end_of_block_or_statement (parser);
5268 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5269 cp_lexer_consume_token (parser->lexer);
5270 return error_mark_node;
5272 else
5273 return cp_parser_statement_expr (parser);
5275 /* Otherwise it's a normal parenthesized expression. */
5277 cp_expr expr;
5278 bool saved_greater_than_is_operator_p;
5280 location_t open_paren_loc = token->location;
5282 /* Consume the `('. */
5283 matching_parens parens;
5284 parens.consume_open (parser);
5285 /* Within a parenthesized expression, a `>' token is always
5286 the greater-than operator. */
5287 saved_greater_than_is_operator_p
5288 = parser->greater_than_is_operator_p;
5289 parser->greater_than_is_operator_p = true;
5291 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5292 /* Left fold expression. */
5293 expr = NULL_TREE;
5294 else
5295 /* Parse the parenthesized expression. */
5296 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5298 token = cp_lexer_peek_token (parser->lexer);
5299 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5301 expr = cp_parser_fold_expression (parser, expr);
5302 if (expr != error_mark_node
5303 && cxx_dialect < cxx17
5304 && !in_system_header_at (input_location))
5305 pedwarn (input_location, 0, "fold-expressions only available "
5306 "with -std=c++17 or -std=gnu++17");
5308 else
5309 /* Let the front end know that this expression was
5310 enclosed in parentheses. This matters in case, for
5311 example, the expression is of the form `A::B', since
5312 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5313 not. */
5314 expr = finish_parenthesized_expr (expr);
5316 /* DR 705: Wrapping an unqualified name in parentheses
5317 suppresses arg-dependent lookup. We want to pass back
5318 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5319 (c++/37862), but none of the others. */
5320 if (*idk != CP_ID_KIND_QUALIFIED)
5321 *idk = CP_ID_KIND_NONE;
5323 /* The `>' token might be the end of a template-id or
5324 template-parameter-list now. */
5325 parser->greater_than_is_operator_p
5326 = saved_greater_than_is_operator_p;
5328 /* Consume the `)'. */
5329 token = cp_lexer_peek_token (parser->lexer);
5330 location_t close_paren_loc = token->location;
5331 expr.set_range (open_paren_loc, close_paren_loc);
5332 if (!parens.require_close (parser)
5333 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5334 cp_parser_skip_to_end_of_statement (parser);
5336 return expr;
5339 case CPP_OPEN_SQUARE:
5341 if (c_dialect_objc ())
5343 /* We might have an Objective-C++ message. */
5344 cp_parser_parse_tentatively (parser);
5345 tree msg = cp_parser_objc_message_expression (parser);
5346 /* If that works out, we're done ... */
5347 if (cp_parser_parse_definitely (parser))
5348 return msg;
5349 /* ... else, fall though to see if it's a lambda. */
5351 cp_expr lam = cp_parser_lambda_expression (parser);
5352 /* Don't warn about a failed tentative parse. */
5353 if (cp_parser_error_occurred (parser))
5354 return error_mark_node;
5355 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5356 return lam;
5359 case CPP_OBJC_STRING:
5360 if (c_dialect_objc ())
5361 /* We have an Objective-C++ string literal. */
5362 return cp_parser_objc_expression (parser);
5363 cp_parser_error (parser, "expected primary-expression");
5364 return error_mark_node;
5366 case CPP_KEYWORD:
5367 switch (token->keyword)
5369 /* These two are the boolean literals. */
5370 case RID_TRUE:
5371 cp_lexer_consume_token (parser->lexer);
5372 return cp_expr (boolean_true_node, token->location);
5373 case RID_FALSE:
5374 cp_lexer_consume_token (parser->lexer);
5375 return cp_expr (boolean_false_node, token->location);
5377 /* The `__null' literal. */
5378 case RID_NULL:
5379 cp_lexer_consume_token (parser->lexer);
5380 return cp_expr (null_node, token->location);
5382 /* The `nullptr' literal. */
5383 case RID_NULLPTR:
5384 cp_lexer_consume_token (parser->lexer);
5385 return cp_expr (nullptr_node, token->location);
5387 /* Recognize the `this' keyword. */
5388 case RID_THIS:
5389 cp_lexer_consume_token (parser->lexer);
5390 if (parser->local_variables_forbidden_p)
5392 error_at (token->location,
5393 "%<this%> may not be used in this context");
5394 return error_mark_node;
5396 /* Pointers cannot appear in constant-expressions. */
5397 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5398 return error_mark_node;
5399 return cp_expr (finish_this_expr (), token->location);
5401 /* The `operator' keyword can be the beginning of an
5402 id-expression. */
5403 case RID_OPERATOR:
5404 goto id_expression;
5406 case RID_FUNCTION_NAME:
5407 case RID_PRETTY_FUNCTION_NAME:
5408 case RID_C99_FUNCTION_NAME:
5410 non_integral_constant name;
5412 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5413 __func__ are the names of variables -- but they are
5414 treated specially. Therefore, they are handled here,
5415 rather than relying on the generic id-expression logic
5416 below. Grammatically, these names are id-expressions.
5418 Consume the token. */
5419 token = cp_lexer_consume_token (parser->lexer);
5421 switch (token->keyword)
5423 case RID_FUNCTION_NAME:
5424 name = NIC_FUNC_NAME;
5425 break;
5426 case RID_PRETTY_FUNCTION_NAME:
5427 name = NIC_PRETTY_FUNC;
5428 break;
5429 case RID_C99_FUNCTION_NAME:
5430 name = NIC_C99_FUNC;
5431 break;
5432 default:
5433 gcc_unreachable ();
5436 if (cp_parser_non_integral_constant_expression (parser, name))
5437 return error_mark_node;
5439 /* Look up the name. */
5440 return finish_fname (token->u.value);
5443 case RID_VA_ARG:
5445 tree expression;
5446 tree type;
5447 location_t type_location;
5448 location_t start_loc
5449 = cp_lexer_peek_token (parser->lexer)->location;
5450 /* The `__builtin_va_arg' construct is used to handle
5451 `va_arg'. Consume the `__builtin_va_arg' token. */
5452 cp_lexer_consume_token (parser->lexer);
5453 /* Look for the opening `('. */
5454 matching_parens parens;
5455 parens.require_open (parser);
5456 /* Now, parse the assignment-expression. */
5457 expression = cp_parser_assignment_expression (parser);
5458 /* Look for the `,'. */
5459 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5460 type_location = cp_lexer_peek_token (parser->lexer)->location;
5461 /* Parse the type-id. */
5463 type_id_in_expr_sentinel s (parser);
5464 type = cp_parser_type_id (parser);
5466 /* Look for the closing `)'. */
5467 location_t finish_loc
5468 = cp_lexer_peek_token (parser->lexer)->location;
5469 parens.require_close (parser);
5470 /* Using `va_arg' in a constant-expression is not
5471 allowed. */
5472 if (cp_parser_non_integral_constant_expression (parser,
5473 NIC_VA_ARG))
5474 return error_mark_node;
5475 /* Construct a location of the form:
5476 __builtin_va_arg (v, int)
5477 ~~~~~~~~~~~~~~~~~~~~~^~~~
5478 with the caret at the type, ranging from the start of the
5479 "__builtin_va_arg" token to the close paren. */
5480 location_t combined_loc
5481 = make_location (type_location, start_loc, finish_loc);
5482 return build_x_va_arg (combined_loc, expression, type);
5485 case RID_OFFSETOF:
5486 return cp_parser_builtin_offsetof (parser);
5488 case RID_HAS_NOTHROW_ASSIGN:
5489 case RID_HAS_NOTHROW_CONSTRUCTOR:
5490 case RID_HAS_NOTHROW_COPY:
5491 case RID_HAS_TRIVIAL_ASSIGN:
5492 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5493 case RID_HAS_TRIVIAL_COPY:
5494 case RID_HAS_TRIVIAL_DESTRUCTOR:
5495 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5496 case RID_HAS_VIRTUAL_DESTRUCTOR:
5497 case RID_IS_ABSTRACT:
5498 case RID_IS_AGGREGATE:
5499 case RID_IS_BASE_OF:
5500 case RID_IS_CLASS:
5501 case RID_IS_EMPTY:
5502 case RID_IS_ENUM:
5503 case RID_IS_FINAL:
5504 case RID_IS_LITERAL_TYPE:
5505 case RID_IS_POD:
5506 case RID_IS_POLYMORPHIC:
5507 case RID_IS_SAME_AS:
5508 case RID_IS_STD_LAYOUT:
5509 case RID_IS_TRIVIAL:
5510 case RID_IS_TRIVIALLY_ASSIGNABLE:
5511 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5512 case RID_IS_TRIVIALLY_COPYABLE:
5513 case RID_IS_UNION:
5514 case RID_IS_ASSIGNABLE:
5515 case RID_IS_CONSTRUCTIBLE:
5516 return cp_parser_trait_expr (parser, token->keyword);
5518 // C++ concepts
5519 case RID_REQUIRES:
5520 return cp_parser_requires_expression (parser);
5522 /* Objective-C++ expressions. */
5523 case RID_AT_ENCODE:
5524 case RID_AT_PROTOCOL:
5525 case RID_AT_SELECTOR:
5526 return cp_parser_objc_expression (parser);
5528 case RID_TEMPLATE:
5529 if (parser->in_function_body
5530 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5531 == CPP_LESS))
5533 error_at (token->location,
5534 "a template declaration cannot appear at block scope");
5535 cp_parser_skip_to_end_of_block_or_statement (parser);
5536 return error_mark_node;
5538 /* FALLTHRU */
5539 default:
5540 cp_parser_error (parser, "expected primary-expression");
5541 return error_mark_node;
5544 /* An id-expression can start with either an identifier, a
5545 `::' as the beginning of a qualified-id, or the "operator"
5546 keyword. */
5547 case CPP_NAME:
5548 case CPP_SCOPE:
5549 case CPP_TEMPLATE_ID:
5550 case CPP_NESTED_NAME_SPECIFIER:
5552 id_expression:
5553 cp_expr id_expression;
5554 cp_expr decl;
5555 const char *error_msg;
5556 bool template_p;
5557 bool done;
5558 cp_token *id_expr_token;
5560 /* Parse the id-expression. */
5561 id_expression
5562 = cp_parser_id_expression (parser,
5563 /*template_keyword_p=*/false,
5564 /*check_dependency_p=*/true,
5565 &template_p,
5566 /*declarator_p=*/false,
5567 /*optional_p=*/false);
5568 if (id_expression == error_mark_node)
5569 return error_mark_node;
5570 id_expr_token = token;
5571 token = cp_lexer_peek_token (parser->lexer);
5572 done = (token->type != CPP_OPEN_SQUARE
5573 && token->type != CPP_OPEN_PAREN
5574 && token->type != CPP_DOT
5575 && token->type != CPP_DEREF
5576 && token->type != CPP_PLUS_PLUS
5577 && token->type != CPP_MINUS_MINUS);
5578 /* If we have a template-id, then no further lookup is
5579 required. If the template-id was for a template-class, we
5580 will sometimes have a TYPE_DECL at this point. */
5581 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5582 || TREE_CODE (id_expression) == TYPE_DECL)
5583 decl = id_expression;
5584 /* Look up the name. */
5585 else
5587 tree ambiguous_decls;
5589 /* If we already know that this lookup is ambiguous, then
5590 we've already issued an error message; there's no reason
5591 to check again. */
5592 if (id_expr_token->type == CPP_NAME
5593 && id_expr_token->error_reported)
5595 cp_parser_simulate_error (parser);
5596 return error_mark_node;
5599 decl = cp_parser_lookup_name (parser, id_expression,
5600 none_type,
5601 template_p,
5602 /*is_namespace=*/false,
5603 /*check_dependency=*/true,
5604 &ambiguous_decls,
5605 id_expr_token->location);
5606 /* If the lookup was ambiguous, an error will already have
5607 been issued. */
5608 if (ambiguous_decls)
5609 return error_mark_node;
5611 /* In Objective-C++, we may have an Objective-C 2.0
5612 dot-syntax for classes here. */
5613 if (c_dialect_objc ()
5614 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5615 && TREE_CODE (decl) == TYPE_DECL
5616 && objc_is_class_name (decl))
5618 tree component;
5619 cp_lexer_consume_token (parser->lexer);
5620 component = cp_parser_identifier (parser);
5621 if (component == error_mark_node)
5622 return error_mark_node;
5624 tree result = objc_build_class_component_ref (id_expression,
5625 component);
5626 /* Build a location of the form:
5627 expr.component
5628 ~~~~~^~~~~~~~~
5629 with caret at the start of the component name (at
5630 input_location), ranging from the start of the id_expression
5631 to the end of the component name. */
5632 location_t combined_loc
5633 = make_location (input_location, id_expression.get_start (),
5634 get_finish (input_location));
5635 protected_set_expr_location (result, combined_loc);
5636 return result;
5639 /* In Objective-C++, an instance variable (ivar) may be preferred
5640 to whatever cp_parser_lookup_name() found.
5641 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5642 rest of c-family, we have to do a little extra work to preserve
5643 any location information in cp_expr "decl". Given that
5644 objc_lookup_ivar is implemented in "c-family" and "objc", we
5645 have a trip through the pure "tree" type, rather than cp_expr.
5646 Naively copying it back to "decl" would implicitly give the
5647 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5648 store an EXPR_LOCATION. Hence we only update "decl" (and
5649 hence its location_t) if we get back a different tree node. */
5650 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5651 id_expression);
5652 if (decl_tree != decl.get_value ())
5653 decl = cp_expr (decl_tree);
5655 /* If name lookup gives us a SCOPE_REF, then the
5656 qualifying scope was dependent. */
5657 if (TREE_CODE (decl) == SCOPE_REF)
5659 /* At this point, we do not know if DECL is a valid
5660 integral constant expression. We assume that it is
5661 in fact such an expression, so that code like:
5663 template <int N> struct A {
5664 int a[B<N>::i];
5667 is accepted. At template-instantiation time, we
5668 will check that B<N>::i is actually a constant. */
5669 return decl;
5671 /* Check to see if DECL is a local variable in a context
5672 where that is forbidden. */
5673 if (parser->local_variables_forbidden_p
5674 && local_variable_p (decl))
5676 error_at (id_expr_token->location,
5677 "local variable %qD may not appear in this context",
5678 decl.get_value ());
5679 return error_mark_node;
5683 decl = (finish_id_expression
5684 (id_expression, decl, parser->scope,
5685 idk,
5686 parser->integral_constant_expression_p,
5687 parser->allow_non_integral_constant_expression_p,
5688 &parser->non_integral_constant_expression_p,
5689 template_p, done, address_p,
5690 template_arg_p,
5691 &error_msg,
5692 id_expression.get_location ()));
5693 if (error_msg)
5694 cp_parser_error (parser, error_msg);
5695 decl.set_location (id_expr_token->location);
5696 return decl;
5699 /* Anything else is an error. */
5700 default:
5701 cp_parser_error (parser, "expected primary-expression");
5702 return error_mark_node;
5706 static inline cp_expr
5707 cp_parser_primary_expression (cp_parser *parser,
5708 bool address_p,
5709 bool cast_p,
5710 bool template_arg_p,
5711 cp_id_kind *idk)
5713 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5714 /*decltype*/false, idk);
5717 /* Parse an id-expression.
5719 id-expression:
5720 unqualified-id
5721 qualified-id
5723 qualified-id:
5724 :: [opt] nested-name-specifier template [opt] unqualified-id
5725 :: identifier
5726 :: operator-function-id
5727 :: template-id
5729 Return a representation of the unqualified portion of the
5730 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5731 a `::' or nested-name-specifier.
5733 Often, if the id-expression was a qualified-id, the caller will
5734 want to make a SCOPE_REF to represent the qualified-id. This
5735 function does not do this in order to avoid wastefully creating
5736 SCOPE_REFs when they are not required.
5738 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5739 `template' keyword.
5741 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5742 uninstantiated templates.
5744 If *TEMPLATE_P is non-NULL, it is set to true iff the
5745 `template' keyword is used to explicitly indicate that the entity
5746 named is a template.
5748 If DECLARATOR_P is true, the id-expression is appearing as part of
5749 a declarator, rather than as part of an expression. */
5751 static cp_expr
5752 cp_parser_id_expression (cp_parser *parser,
5753 bool template_keyword_p,
5754 bool check_dependency_p,
5755 bool *template_p,
5756 bool declarator_p,
5757 bool optional_p)
5759 bool global_scope_p;
5760 bool nested_name_specifier_p;
5762 /* Assume the `template' keyword was not used. */
5763 if (template_p)
5764 *template_p = template_keyword_p;
5766 /* Look for the optional `::' operator. */
5767 global_scope_p
5768 = (!template_keyword_p
5769 && (cp_parser_global_scope_opt (parser,
5770 /*current_scope_valid_p=*/false)
5771 != NULL_TREE));
5773 /* Look for the optional nested-name-specifier. */
5774 nested_name_specifier_p
5775 = (cp_parser_nested_name_specifier_opt (parser,
5776 /*typename_keyword_p=*/false,
5777 check_dependency_p,
5778 /*type_p=*/false,
5779 declarator_p,
5780 template_keyword_p)
5781 != NULL_TREE);
5783 /* If there is a nested-name-specifier, then we are looking at
5784 the first qualified-id production. */
5785 if (nested_name_specifier_p)
5787 tree saved_scope;
5788 tree saved_object_scope;
5789 tree saved_qualifying_scope;
5790 cp_expr unqualified_id;
5791 bool is_template;
5793 /* See if the next token is the `template' keyword. */
5794 if (!template_p)
5795 template_p = &is_template;
5796 *template_p = cp_parser_optional_template_keyword (parser);
5797 /* Name lookup we do during the processing of the
5798 unqualified-id might obliterate SCOPE. */
5799 saved_scope = parser->scope;
5800 saved_object_scope = parser->object_scope;
5801 saved_qualifying_scope = parser->qualifying_scope;
5802 /* Process the final unqualified-id. */
5803 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5804 check_dependency_p,
5805 declarator_p,
5806 /*optional_p=*/false);
5807 /* Restore the SAVED_SCOPE for our caller. */
5808 parser->scope = saved_scope;
5809 parser->object_scope = saved_object_scope;
5810 parser->qualifying_scope = saved_qualifying_scope;
5812 return unqualified_id;
5814 /* Otherwise, if we are in global scope, then we are looking at one
5815 of the other qualified-id productions. */
5816 else if (global_scope_p)
5818 cp_token *token;
5819 tree id;
5821 /* Peek at the next token. */
5822 token = cp_lexer_peek_token (parser->lexer);
5824 /* If it's an identifier, and the next token is not a "<", then
5825 we can avoid the template-id case. This is an optimization
5826 for this common case. */
5827 if (token->type == CPP_NAME
5828 && !cp_parser_nth_token_starts_template_argument_list_p
5829 (parser, 2))
5830 return cp_parser_identifier (parser);
5832 cp_parser_parse_tentatively (parser);
5833 /* Try a template-id. */
5834 id = cp_parser_template_id (parser,
5835 /*template_keyword_p=*/false,
5836 /*check_dependency_p=*/true,
5837 none_type,
5838 declarator_p);
5839 /* If that worked, we're done. */
5840 if (cp_parser_parse_definitely (parser))
5841 return id;
5843 /* Peek at the next token. (Changes in the token buffer may
5844 have invalidated the pointer obtained above.) */
5845 token = cp_lexer_peek_token (parser->lexer);
5847 switch (token->type)
5849 case CPP_NAME:
5850 return cp_parser_identifier (parser);
5852 case CPP_KEYWORD:
5853 if (token->keyword == RID_OPERATOR)
5854 return cp_parser_operator_function_id (parser);
5855 /* Fall through. */
5857 default:
5858 cp_parser_error (parser, "expected id-expression");
5859 return error_mark_node;
5862 else
5863 return cp_parser_unqualified_id (parser, template_keyword_p,
5864 /*check_dependency_p=*/true,
5865 declarator_p,
5866 optional_p);
5869 /* Parse an unqualified-id.
5871 unqualified-id:
5872 identifier
5873 operator-function-id
5874 conversion-function-id
5875 ~ class-name
5876 template-id
5878 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5879 keyword, in a construct like `A::template ...'.
5881 Returns a representation of unqualified-id. For the `identifier'
5882 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5883 production a BIT_NOT_EXPR is returned; the operand of the
5884 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5885 other productions, see the documentation accompanying the
5886 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5887 names are looked up in uninstantiated templates. If DECLARATOR_P
5888 is true, the unqualified-id is appearing as part of a declarator,
5889 rather than as part of an expression. */
5891 static cp_expr
5892 cp_parser_unqualified_id (cp_parser* parser,
5893 bool template_keyword_p,
5894 bool check_dependency_p,
5895 bool declarator_p,
5896 bool optional_p)
5898 cp_token *token;
5900 /* Peek at the next token. */
5901 token = cp_lexer_peek_token (parser->lexer);
5903 switch ((int) token->type)
5905 case CPP_NAME:
5907 tree id;
5909 /* We don't know yet whether or not this will be a
5910 template-id. */
5911 cp_parser_parse_tentatively (parser);
5912 /* Try a template-id. */
5913 id = cp_parser_template_id (parser, template_keyword_p,
5914 check_dependency_p,
5915 none_type,
5916 declarator_p);
5917 /* If it worked, we're done. */
5918 if (cp_parser_parse_definitely (parser))
5919 return id;
5920 /* Otherwise, it's an ordinary identifier. */
5921 return cp_parser_identifier (parser);
5924 case CPP_TEMPLATE_ID:
5925 return cp_parser_template_id (parser, template_keyword_p,
5926 check_dependency_p,
5927 none_type,
5928 declarator_p);
5930 case CPP_COMPL:
5932 tree type_decl;
5933 tree qualifying_scope;
5934 tree object_scope;
5935 tree scope;
5936 bool done;
5938 /* Consume the `~' token. */
5939 cp_lexer_consume_token (parser->lexer);
5940 /* Parse the class-name. The standard, as written, seems to
5941 say that:
5943 template <typename T> struct S { ~S (); };
5944 template <typename T> S<T>::~S() {}
5946 is invalid, since `~' must be followed by a class-name, but
5947 `S<T>' is dependent, and so not known to be a class.
5948 That's not right; we need to look in uninstantiated
5949 templates. A further complication arises from:
5951 template <typename T> void f(T t) {
5952 t.T::~T();
5955 Here, it is not possible to look up `T' in the scope of `T'
5956 itself. We must look in both the current scope, and the
5957 scope of the containing complete expression.
5959 Yet another issue is:
5961 struct S {
5962 int S;
5963 ~S();
5966 S::~S() {}
5968 The standard does not seem to say that the `S' in `~S'
5969 should refer to the type `S' and not the data member
5970 `S::S'. */
5972 /* DR 244 says that we look up the name after the "~" in the
5973 same scope as we looked up the qualifying name. That idea
5974 isn't fully worked out; it's more complicated than that. */
5975 scope = parser->scope;
5976 object_scope = parser->object_scope;
5977 qualifying_scope = parser->qualifying_scope;
5979 /* Check for invalid scopes. */
5980 if (scope == error_mark_node)
5982 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5983 cp_lexer_consume_token (parser->lexer);
5984 return error_mark_node;
5986 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5988 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5989 error_at (token->location,
5990 "scope %qT before %<~%> is not a class-name",
5991 scope);
5992 cp_parser_simulate_error (parser);
5993 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5994 cp_lexer_consume_token (parser->lexer);
5995 return error_mark_node;
5997 gcc_assert (!scope || TYPE_P (scope));
5999 /* If the name is of the form "X::~X" it's OK even if X is a
6000 typedef. */
6001 token = cp_lexer_peek_token (parser->lexer);
6002 if (scope
6003 && token->type == CPP_NAME
6004 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6005 != CPP_LESS)
6006 && (token->u.value == TYPE_IDENTIFIER (scope)
6007 || (CLASS_TYPE_P (scope)
6008 && constructor_name_p (token->u.value, scope))))
6010 cp_lexer_consume_token (parser->lexer);
6011 return build_nt (BIT_NOT_EXPR, scope);
6014 /* ~auto means the destructor of whatever the object is. */
6015 if (cp_parser_is_keyword (token, RID_AUTO))
6017 if (cxx_dialect < cxx14)
6018 pedwarn (input_location, 0,
6019 "%<~auto%> only available with "
6020 "-std=c++14 or -std=gnu++14");
6021 cp_lexer_consume_token (parser->lexer);
6022 return build_nt (BIT_NOT_EXPR, make_auto ());
6025 /* If there was an explicit qualification (S::~T), first look
6026 in the scope given by the qualification (i.e., S).
6028 Note: in the calls to cp_parser_class_name below we pass
6029 typename_type so that lookup finds the injected-class-name
6030 rather than the constructor. */
6031 done = false;
6032 type_decl = NULL_TREE;
6033 if (scope)
6035 cp_parser_parse_tentatively (parser);
6036 type_decl = cp_parser_class_name (parser,
6037 /*typename_keyword_p=*/false,
6038 /*template_keyword_p=*/false,
6039 typename_type,
6040 /*check_dependency=*/false,
6041 /*class_head_p=*/false,
6042 declarator_p);
6043 if (cp_parser_parse_definitely (parser))
6044 done = true;
6046 /* In "N::S::~S", look in "N" as well. */
6047 if (!done && scope && qualifying_scope)
6049 cp_parser_parse_tentatively (parser);
6050 parser->scope = qualifying_scope;
6051 parser->object_scope = NULL_TREE;
6052 parser->qualifying_scope = NULL_TREE;
6053 type_decl
6054 = cp_parser_class_name (parser,
6055 /*typename_keyword_p=*/false,
6056 /*template_keyword_p=*/false,
6057 typename_type,
6058 /*check_dependency=*/false,
6059 /*class_head_p=*/false,
6060 declarator_p);
6061 if (cp_parser_parse_definitely (parser))
6062 done = true;
6064 /* In "p->S::~T", look in the scope given by "*p" as well. */
6065 else if (!done && object_scope)
6067 cp_parser_parse_tentatively (parser);
6068 parser->scope = object_scope;
6069 parser->object_scope = NULL_TREE;
6070 parser->qualifying_scope = NULL_TREE;
6071 type_decl
6072 = cp_parser_class_name (parser,
6073 /*typename_keyword_p=*/false,
6074 /*template_keyword_p=*/false,
6075 typename_type,
6076 /*check_dependency=*/false,
6077 /*class_head_p=*/false,
6078 declarator_p);
6079 if (cp_parser_parse_definitely (parser))
6080 done = true;
6082 /* Look in the surrounding context. */
6083 if (!done)
6085 parser->scope = NULL_TREE;
6086 parser->object_scope = NULL_TREE;
6087 parser->qualifying_scope = NULL_TREE;
6088 if (processing_template_decl)
6089 cp_parser_parse_tentatively (parser);
6090 type_decl
6091 = cp_parser_class_name (parser,
6092 /*typename_keyword_p=*/false,
6093 /*template_keyword_p=*/false,
6094 typename_type,
6095 /*check_dependency=*/false,
6096 /*class_head_p=*/false,
6097 declarator_p);
6098 if (processing_template_decl
6099 && ! cp_parser_parse_definitely (parser))
6101 /* We couldn't find a type with this name. If we're parsing
6102 tentatively, fail and try something else. */
6103 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6105 cp_parser_simulate_error (parser);
6106 return error_mark_node;
6108 /* Otherwise, accept it and check for a match at instantiation
6109 time. */
6110 type_decl = cp_parser_identifier (parser);
6111 if (type_decl != error_mark_node)
6112 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6113 return type_decl;
6116 /* If an error occurred, assume that the name of the
6117 destructor is the same as the name of the qualifying
6118 class. That allows us to keep parsing after running
6119 into ill-formed destructor names. */
6120 if (type_decl == error_mark_node && scope)
6121 return build_nt (BIT_NOT_EXPR, scope);
6122 else if (type_decl == error_mark_node)
6123 return error_mark_node;
6125 /* Check that destructor name and scope match. */
6126 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6128 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6129 error_at (token->location,
6130 "declaration of %<~%T%> as member of %qT",
6131 type_decl, scope);
6132 cp_parser_simulate_error (parser);
6133 return error_mark_node;
6136 /* [class.dtor]
6138 A typedef-name that names a class shall not be used as the
6139 identifier in the declarator for a destructor declaration. */
6140 if (declarator_p
6141 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6142 && !DECL_SELF_REFERENCE_P (type_decl)
6143 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6144 error_at (token->location,
6145 "typedef-name %qD used as destructor declarator",
6146 type_decl);
6148 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6151 case CPP_KEYWORD:
6152 if (token->keyword == RID_OPERATOR)
6154 cp_expr id;
6156 /* This could be a template-id, so we try that first. */
6157 cp_parser_parse_tentatively (parser);
6158 /* Try a template-id. */
6159 id = cp_parser_template_id (parser, template_keyword_p,
6160 /*check_dependency_p=*/true,
6161 none_type,
6162 declarator_p);
6163 /* If that worked, we're done. */
6164 if (cp_parser_parse_definitely (parser))
6165 return id;
6166 /* We still don't know whether we're looking at an
6167 operator-function-id or a conversion-function-id. */
6168 cp_parser_parse_tentatively (parser);
6169 /* Try an operator-function-id. */
6170 id = cp_parser_operator_function_id (parser);
6171 /* If that didn't work, try a conversion-function-id. */
6172 if (!cp_parser_parse_definitely (parser))
6173 id = cp_parser_conversion_function_id (parser);
6175 return id;
6177 /* Fall through. */
6179 default:
6180 if (optional_p)
6181 return NULL_TREE;
6182 cp_parser_error (parser, "expected unqualified-id");
6183 return error_mark_node;
6187 /* Parse an (optional) nested-name-specifier.
6189 nested-name-specifier: [C++98]
6190 class-or-namespace-name :: nested-name-specifier [opt]
6191 class-or-namespace-name :: template nested-name-specifier [opt]
6193 nested-name-specifier: [C++0x]
6194 type-name ::
6195 namespace-name ::
6196 nested-name-specifier identifier ::
6197 nested-name-specifier template [opt] simple-template-id ::
6199 PARSER->SCOPE should be set appropriately before this function is
6200 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6201 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6202 in name lookups.
6204 Sets PARSER->SCOPE to the class (TYPE) or namespace
6205 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6206 it unchanged if there is no nested-name-specifier. Returns the new
6207 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6209 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6210 part of a declaration and/or decl-specifier. */
6212 static tree
6213 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6214 bool typename_keyword_p,
6215 bool check_dependency_p,
6216 bool type_p,
6217 bool is_declaration,
6218 bool template_keyword_p /* = false */)
6220 bool success = false;
6221 cp_token_position start = 0;
6222 cp_token *token;
6224 /* Remember where the nested-name-specifier starts. */
6225 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6227 start = cp_lexer_token_position (parser->lexer, false);
6228 push_deferring_access_checks (dk_deferred);
6231 while (true)
6233 tree new_scope;
6234 tree old_scope;
6235 tree saved_qualifying_scope;
6237 /* Spot cases that cannot be the beginning of a
6238 nested-name-specifier. */
6239 token = cp_lexer_peek_token (parser->lexer);
6241 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6242 the already parsed nested-name-specifier. */
6243 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6245 /* Grab the nested-name-specifier and continue the loop. */
6246 cp_parser_pre_parsed_nested_name_specifier (parser);
6247 /* If we originally encountered this nested-name-specifier
6248 with IS_DECLARATION set to false, we will not have
6249 resolved TYPENAME_TYPEs, so we must do so here. */
6250 if (is_declaration
6251 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6253 new_scope = resolve_typename_type (parser->scope,
6254 /*only_current_p=*/false);
6255 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6256 parser->scope = new_scope;
6258 success = true;
6259 continue;
6262 /* Spot cases that cannot be the beginning of a
6263 nested-name-specifier. On the second and subsequent times
6264 through the loop, we look for the `template' keyword. */
6265 if (success && token->keyword == RID_TEMPLATE)
6267 /* A template-id can start a nested-name-specifier. */
6268 else if (token->type == CPP_TEMPLATE_ID)
6270 /* DR 743: decltype can be used in a nested-name-specifier. */
6271 else if (token_is_decltype (token))
6273 else
6275 /* If the next token is not an identifier, then it is
6276 definitely not a type-name or namespace-name. */
6277 if (token->type != CPP_NAME)
6278 break;
6279 /* If the following token is neither a `<' (to begin a
6280 template-id), nor a `::', then we are not looking at a
6281 nested-name-specifier. */
6282 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6284 if (token->type == CPP_COLON
6285 && parser->colon_corrects_to_scope_p
6286 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6288 gcc_rich_location richloc (token->location);
6289 richloc.add_fixit_replace ("::");
6290 error_at (&richloc,
6291 "found %<:%> in nested-name-specifier, "
6292 "expected %<::%>");
6293 token->type = CPP_SCOPE;
6296 if (token->type != CPP_SCOPE
6297 && !cp_parser_nth_token_starts_template_argument_list_p
6298 (parser, 2))
6299 break;
6302 /* The nested-name-specifier is optional, so we parse
6303 tentatively. */
6304 cp_parser_parse_tentatively (parser);
6306 /* Look for the optional `template' keyword, if this isn't the
6307 first time through the loop. */
6308 if (success)
6309 template_keyword_p = cp_parser_optional_template_keyword (parser);
6311 /* Save the old scope since the name lookup we are about to do
6312 might destroy it. */
6313 old_scope = parser->scope;
6314 saved_qualifying_scope = parser->qualifying_scope;
6315 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6316 look up names in "X<T>::I" in order to determine that "Y" is
6317 a template. So, if we have a typename at this point, we make
6318 an effort to look through it. */
6319 if (is_declaration
6320 && !typename_keyword_p
6321 && parser->scope
6322 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6323 parser->scope = resolve_typename_type (parser->scope,
6324 /*only_current_p=*/false);
6325 /* Parse the qualifying entity. */
6326 new_scope
6327 = cp_parser_qualifying_entity (parser,
6328 typename_keyword_p,
6329 template_keyword_p,
6330 check_dependency_p,
6331 type_p,
6332 is_declaration);
6333 /* Look for the `::' token. */
6334 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6336 /* If we found what we wanted, we keep going; otherwise, we're
6337 done. */
6338 if (!cp_parser_parse_definitely (parser))
6340 bool error_p = false;
6342 /* Restore the OLD_SCOPE since it was valid before the
6343 failed attempt at finding the last
6344 class-or-namespace-name. */
6345 parser->scope = old_scope;
6346 parser->qualifying_scope = saved_qualifying_scope;
6348 /* If the next token is a decltype, and the one after that is a
6349 `::', then the decltype has failed to resolve to a class or
6350 enumeration type. Give this error even when parsing
6351 tentatively since it can't possibly be valid--and we're going
6352 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6353 won't get another chance.*/
6354 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6355 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6356 == CPP_SCOPE))
6358 token = cp_lexer_consume_token (parser->lexer);
6359 error_at (token->location, "decltype evaluates to %qT, "
6360 "which is not a class or enumeration type",
6361 token->u.tree_check_value->value);
6362 parser->scope = error_mark_node;
6363 error_p = true;
6364 /* As below. */
6365 success = true;
6366 cp_lexer_consume_token (parser->lexer);
6369 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6370 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6372 /* If we have a non-type template-id followed by ::, it can't
6373 possibly be valid. */
6374 token = cp_lexer_peek_token (parser->lexer);
6375 tree tid = token->u.tree_check_value->value;
6376 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6377 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6379 tree tmpl = NULL_TREE;
6380 if (is_overloaded_fn (tid))
6382 tree fns = get_fns (tid);
6383 if (OVL_SINGLE_P (fns))
6384 tmpl = OVL_FIRST (fns);
6385 error_at (token->location, "function template-id %qD "
6386 "in nested-name-specifier", tid);
6388 else
6390 /* Variable template. */
6391 tmpl = TREE_OPERAND (tid, 0);
6392 gcc_assert (variable_template_p (tmpl));
6393 error_at (token->location, "variable template-id %qD "
6394 "in nested-name-specifier", tid);
6396 if (tmpl)
6397 inform (DECL_SOURCE_LOCATION (tmpl),
6398 "%qD declared here", tmpl);
6400 parser->scope = error_mark_node;
6401 error_p = true;
6402 /* As below. */
6403 success = true;
6404 cp_lexer_consume_token (parser->lexer);
6405 cp_lexer_consume_token (parser->lexer);
6409 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6410 break;
6411 /* If the next token is an identifier, and the one after
6412 that is a `::', then any valid interpretation would have
6413 found a class-or-namespace-name. */
6414 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6415 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6416 == CPP_SCOPE)
6417 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6418 != CPP_COMPL))
6420 token = cp_lexer_consume_token (parser->lexer);
6421 if (!error_p)
6423 if (!token->error_reported)
6425 tree decl;
6426 tree ambiguous_decls;
6428 decl = cp_parser_lookup_name (parser, token->u.value,
6429 none_type,
6430 /*is_template=*/false,
6431 /*is_namespace=*/false,
6432 /*check_dependency=*/true,
6433 &ambiguous_decls,
6434 token->location);
6435 if (TREE_CODE (decl) == TEMPLATE_DECL)
6436 error_at (token->location,
6437 "%qD used without template arguments",
6438 decl);
6439 else if (ambiguous_decls)
6441 // cp_parser_lookup_name has the same diagnostic,
6442 // thus make sure to emit it at most once.
6443 if (cp_parser_uncommitted_to_tentative_parse_p
6444 (parser))
6446 error_at (token->location,
6447 "reference to %qD is ambiguous",
6448 token->u.value);
6449 print_candidates (ambiguous_decls);
6451 decl = error_mark_node;
6453 else
6455 if (cxx_dialect != cxx98)
6456 cp_parser_name_lookup_error
6457 (parser, token->u.value, decl, NLE_NOT_CXX98,
6458 token->location);
6459 else
6460 cp_parser_name_lookup_error
6461 (parser, token->u.value, decl, NLE_CXX98,
6462 token->location);
6465 parser->scope = error_mark_node;
6466 error_p = true;
6467 /* Treat this as a successful nested-name-specifier
6468 due to:
6470 [basic.lookup.qual]
6472 If the name found is not a class-name (clause
6473 _class_) or namespace-name (_namespace.def_), the
6474 program is ill-formed. */
6475 success = true;
6477 cp_lexer_consume_token (parser->lexer);
6479 break;
6481 /* We've found one valid nested-name-specifier. */
6482 success = true;
6483 /* Name lookup always gives us a DECL. */
6484 if (TREE_CODE (new_scope) == TYPE_DECL)
6485 new_scope = TREE_TYPE (new_scope);
6486 /* Uses of "template" must be followed by actual templates. */
6487 if (template_keyword_p
6488 && !(CLASS_TYPE_P (new_scope)
6489 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6490 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6491 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6492 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6493 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6494 == TEMPLATE_ID_EXPR)))
6495 permerror (input_location, TYPE_P (new_scope)
6496 ? G_("%qT is not a template")
6497 : G_("%qD is not a template"),
6498 new_scope);
6499 /* If it is a class scope, try to complete it; we are about to
6500 be looking up names inside the class. */
6501 if (TYPE_P (new_scope)
6502 /* Since checking types for dependency can be expensive,
6503 avoid doing it if the type is already complete. */
6504 && !COMPLETE_TYPE_P (new_scope)
6505 /* Do not try to complete dependent types. */
6506 && !dependent_type_p (new_scope))
6508 new_scope = complete_type (new_scope);
6509 /* If it is a typedef to current class, use the current
6510 class instead, as the typedef won't have any names inside
6511 it yet. */
6512 if (!COMPLETE_TYPE_P (new_scope)
6513 && currently_open_class (new_scope))
6514 new_scope = TYPE_MAIN_VARIANT (new_scope);
6516 /* Make sure we look in the right scope the next time through
6517 the loop. */
6518 parser->scope = new_scope;
6521 /* If parsing tentatively, replace the sequence of tokens that makes
6522 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6523 token. That way, should we re-parse the token stream, we will
6524 not have to repeat the effort required to do the parse, nor will
6525 we issue duplicate error messages. */
6526 if (success && start)
6528 cp_token *token;
6530 token = cp_lexer_token_at (parser->lexer, start);
6531 /* Reset the contents of the START token. */
6532 token->type = CPP_NESTED_NAME_SPECIFIER;
6533 /* Retrieve any deferred checks. Do not pop this access checks yet
6534 so the memory will not be reclaimed during token replacing below. */
6535 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6536 token->u.tree_check_value->value = parser->scope;
6537 token->u.tree_check_value->checks = get_deferred_access_checks ();
6538 token->u.tree_check_value->qualifying_scope =
6539 parser->qualifying_scope;
6540 token->keyword = RID_MAX;
6542 /* Purge all subsequent tokens. */
6543 cp_lexer_purge_tokens_after (parser->lexer, start);
6546 if (start)
6547 pop_to_parent_deferring_access_checks ();
6549 return success ? parser->scope : NULL_TREE;
6552 /* Parse a nested-name-specifier. See
6553 cp_parser_nested_name_specifier_opt for details. This function
6554 behaves identically, except that it will an issue an error if no
6555 nested-name-specifier is present. */
6557 static tree
6558 cp_parser_nested_name_specifier (cp_parser *parser,
6559 bool typename_keyword_p,
6560 bool check_dependency_p,
6561 bool type_p,
6562 bool is_declaration)
6564 tree scope;
6566 /* Look for the nested-name-specifier. */
6567 scope = cp_parser_nested_name_specifier_opt (parser,
6568 typename_keyword_p,
6569 check_dependency_p,
6570 type_p,
6571 is_declaration);
6572 /* If it was not present, issue an error message. */
6573 if (!scope)
6575 cp_parser_error (parser, "expected nested-name-specifier");
6576 parser->scope = NULL_TREE;
6579 return scope;
6582 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6583 this is either a class-name or a namespace-name (which corresponds
6584 to the class-or-namespace-name production in the grammar). For
6585 C++0x, it can also be a type-name that refers to an enumeration
6586 type or a simple-template-id.
6588 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6589 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6590 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6591 TYPE_P is TRUE iff the next name should be taken as a class-name,
6592 even the same name is declared to be another entity in the same
6593 scope.
6595 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6596 specified by the class-or-namespace-name. If neither is found the
6597 ERROR_MARK_NODE is returned. */
6599 static tree
6600 cp_parser_qualifying_entity (cp_parser *parser,
6601 bool typename_keyword_p,
6602 bool template_keyword_p,
6603 bool check_dependency_p,
6604 bool type_p,
6605 bool is_declaration)
6607 tree saved_scope;
6608 tree saved_qualifying_scope;
6609 tree saved_object_scope;
6610 tree scope;
6611 bool only_class_p;
6612 bool successful_parse_p;
6614 /* DR 743: decltype can appear in a nested-name-specifier. */
6615 if (cp_lexer_next_token_is_decltype (parser->lexer))
6617 scope = cp_parser_decltype (parser);
6618 if (TREE_CODE (scope) != ENUMERAL_TYPE
6619 && !MAYBE_CLASS_TYPE_P (scope))
6621 cp_parser_simulate_error (parser);
6622 return error_mark_node;
6624 if (TYPE_NAME (scope))
6625 scope = TYPE_NAME (scope);
6626 return scope;
6629 /* Before we try to parse the class-name, we must save away the
6630 current PARSER->SCOPE since cp_parser_class_name will destroy
6631 it. */
6632 saved_scope = parser->scope;
6633 saved_qualifying_scope = parser->qualifying_scope;
6634 saved_object_scope = parser->object_scope;
6635 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6636 there is no need to look for a namespace-name. */
6637 only_class_p = template_keyword_p
6638 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6639 if (!only_class_p)
6640 cp_parser_parse_tentatively (parser);
6641 scope = cp_parser_class_name (parser,
6642 typename_keyword_p,
6643 template_keyword_p,
6644 type_p ? class_type : none_type,
6645 check_dependency_p,
6646 /*class_head_p=*/false,
6647 is_declaration,
6648 /*enum_ok=*/cxx_dialect > cxx98);
6649 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6650 /* If that didn't work, try for a namespace-name. */
6651 if (!only_class_p && !successful_parse_p)
6653 /* Restore the saved scope. */
6654 parser->scope = saved_scope;
6655 parser->qualifying_scope = saved_qualifying_scope;
6656 parser->object_scope = saved_object_scope;
6657 /* If we are not looking at an identifier followed by the scope
6658 resolution operator, then this is not part of a
6659 nested-name-specifier. (Note that this function is only used
6660 to parse the components of a nested-name-specifier.) */
6661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6662 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6663 return error_mark_node;
6664 scope = cp_parser_namespace_name (parser);
6667 return scope;
6670 /* Return true if we are looking at a compound-literal, false otherwise. */
6672 static bool
6673 cp_parser_compound_literal_p (cp_parser *parser)
6675 cp_lexer_save_tokens (parser->lexer);
6677 /* Skip tokens until the next token is a closing parenthesis.
6678 If we find the closing `)', and the next token is a `{', then
6679 we are looking at a compound-literal. */
6680 bool compound_literal_p
6681 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6682 /*consume_paren=*/true)
6683 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6685 /* Roll back the tokens we skipped. */
6686 cp_lexer_rollback_tokens (parser->lexer);
6688 return compound_literal_p;
6691 /* Return true if EXPR is the integer constant zero or a complex constant
6692 of zero, without any folding, but ignoring location wrappers. */
6694 bool
6695 literal_integer_zerop (const_tree expr)
6697 return (location_wrapper_p (expr)
6698 && integer_zerop (TREE_OPERAND (expr, 0)));
6701 /* Parse a postfix-expression.
6703 postfix-expression:
6704 primary-expression
6705 postfix-expression [ expression ]
6706 postfix-expression ( expression-list [opt] )
6707 simple-type-specifier ( expression-list [opt] )
6708 typename :: [opt] nested-name-specifier identifier
6709 ( expression-list [opt] )
6710 typename :: [opt] nested-name-specifier template [opt] template-id
6711 ( expression-list [opt] )
6712 postfix-expression . template [opt] id-expression
6713 postfix-expression -> template [opt] id-expression
6714 postfix-expression . pseudo-destructor-name
6715 postfix-expression -> pseudo-destructor-name
6716 postfix-expression ++
6717 postfix-expression --
6718 dynamic_cast < type-id > ( expression )
6719 static_cast < type-id > ( expression )
6720 reinterpret_cast < type-id > ( expression )
6721 const_cast < type-id > ( expression )
6722 typeid ( expression )
6723 typeid ( type-id )
6725 GNU Extension:
6727 postfix-expression:
6728 ( type-id ) { initializer-list , [opt] }
6730 This extension is a GNU version of the C99 compound-literal
6731 construct. (The C99 grammar uses `type-name' instead of `type-id',
6732 but they are essentially the same concept.)
6734 If ADDRESS_P is true, the postfix expression is the operand of the
6735 `&' operator. CAST_P is true if this expression is the target of a
6736 cast.
6738 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6739 class member access expressions [expr.ref].
6741 Returns a representation of the expression. */
6743 static cp_expr
6744 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6745 bool member_access_only_p, bool decltype_p,
6746 cp_id_kind * pidk_return)
6748 cp_token *token;
6749 location_t loc;
6750 enum rid keyword;
6751 cp_id_kind idk = CP_ID_KIND_NONE;
6752 cp_expr postfix_expression = NULL_TREE;
6753 bool is_member_access = false;
6755 /* Peek at the next token. */
6756 token = cp_lexer_peek_token (parser->lexer);
6757 loc = token->location;
6758 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6760 /* Some of the productions are determined by keywords. */
6761 keyword = token->keyword;
6762 switch (keyword)
6764 case RID_DYNCAST:
6765 case RID_STATCAST:
6766 case RID_REINTCAST:
6767 case RID_CONSTCAST:
6769 tree type;
6770 cp_expr expression;
6771 const char *saved_message;
6772 bool saved_in_type_id_in_expr_p;
6774 /* All of these can be handled in the same way from the point
6775 of view of parsing. Begin by consuming the token
6776 identifying the cast. */
6777 cp_lexer_consume_token (parser->lexer);
6779 /* New types cannot be defined in the cast. */
6780 saved_message = parser->type_definition_forbidden_message;
6781 parser->type_definition_forbidden_message
6782 = G_("types may not be defined in casts");
6784 /* Look for the opening `<'. */
6785 cp_parser_require (parser, CPP_LESS, RT_LESS);
6786 /* Parse the type to which we are casting. */
6787 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6788 parser->in_type_id_in_expr_p = true;
6789 type = cp_parser_type_id (parser);
6790 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6791 /* Look for the closing `>'. */
6792 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6793 /* Restore the old message. */
6794 parser->type_definition_forbidden_message = saved_message;
6796 bool saved_greater_than_is_operator_p
6797 = parser->greater_than_is_operator_p;
6798 parser->greater_than_is_operator_p = true;
6800 /* And the expression which is being cast. */
6801 matching_parens parens;
6802 parens.require_open (parser);
6803 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6804 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6805 RT_CLOSE_PAREN);
6806 location_t end_loc = close_paren ?
6807 close_paren->location : UNKNOWN_LOCATION;
6809 parser->greater_than_is_operator_p
6810 = saved_greater_than_is_operator_p;
6812 /* Only type conversions to integral or enumeration types
6813 can be used in constant-expressions. */
6814 if (!cast_valid_in_integral_constant_expression_p (type)
6815 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6817 postfix_expression = error_mark_node;
6818 break;
6821 switch (keyword)
6823 case RID_DYNCAST:
6824 postfix_expression
6825 = build_dynamic_cast (type, expression, tf_warning_or_error);
6826 break;
6827 case RID_STATCAST:
6828 postfix_expression
6829 = build_static_cast (type, expression, tf_warning_or_error);
6830 break;
6831 case RID_REINTCAST:
6832 postfix_expression
6833 = build_reinterpret_cast (type, expression,
6834 tf_warning_or_error);
6835 break;
6836 case RID_CONSTCAST:
6837 postfix_expression
6838 = build_const_cast (type, expression, tf_warning_or_error);
6839 break;
6840 default:
6841 gcc_unreachable ();
6844 /* Construct a location e.g. :
6845 reinterpret_cast <int *> (expr)
6846 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6847 ranging from the start of the "*_cast" token to the final closing
6848 paren, with the caret at the start. */
6849 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6850 postfix_expression.set_location (cp_cast_loc);
6852 break;
6854 case RID_TYPEID:
6856 tree type;
6857 const char *saved_message;
6858 bool saved_in_type_id_in_expr_p;
6860 /* Consume the `typeid' token. */
6861 cp_lexer_consume_token (parser->lexer);
6862 /* Look for the `(' token. */
6863 matching_parens parens;
6864 parens.require_open (parser);
6865 /* Types cannot be defined in a `typeid' expression. */
6866 saved_message = parser->type_definition_forbidden_message;
6867 parser->type_definition_forbidden_message
6868 = G_("types may not be defined in a %<typeid%> expression");
6869 /* We can't be sure yet whether we're looking at a type-id or an
6870 expression. */
6871 cp_parser_parse_tentatively (parser);
6872 /* Try a type-id first. */
6873 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6874 parser->in_type_id_in_expr_p = true;
6875 type = cp_parser_type_id (parser);
6876 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6877 /* Look for the `)' token. Otherwise, we can't be sure that
6878 we're not looking at an expression: consider `typeid (int
6879 (3))', for example. */
6880 cp_token *close_paren = parens.require_close (parser);
6881 /* If all went well, simply lookup the type-id. */
6882 if (cp_parser_parse_definitely (parser))
6883 postfix_expression = get_typeid (type, tf_warning_or_error);
6884 /* Otherwise, fall back to the expression variant. */
6885 else
6887 tree expression;
6889 /* Look for an expression. */
6890 expression = cp_parser_expression (parser, & idk);
6891 /* Compute its typeid. */
6892 postfix_expression = build_typeid (expression, tf_warning_or_error);
6893 /* Look for the `)' token. */
6894 close_paren = parens.require_close (parser);
6896 /* Restore the saved message. */
6897 parser->type_definition_forbidden_message = saved_message;
6898 /* `typeid' may not appear in an integral constant expression. */
6899 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6900 postfix_expression = error_mark_node;
6902 /* Construct a location e.g. :
6903 typeid (expr)
6904 ^~~~~~~~~~~~~
6905 ranging from the start of the "typeid" token to the final closing
6906 paren, with the caret at the start. */
6907 if (close_paren)
6909 location_t typeid_loc
6910 = make_location (start_loc, start_loc, close_paren->location);
6911 postfix_expression.set_location (typeid_loc);
6912 postfix_expression.maybe_add_location_wrapper ();
6915 break;
6917 case RID_TYPENAME:
6919 tree type;
6920 /* The syntax permitted here is the same permitted for an
6921 elaborated-type-specifier. */
6922 ++parser->prevent_constrained_type_specifiers;
6923 type = cp_parser_elaborated_type_specifier (parser,
6924 /*is_friend=*/false,
6925 /*is_declaration=*/false);
6926 --parser->prevent_constrained_type_specifiers;
6927 postfix_expression = cp_parser_functional_cast (parser, type);
6929 break;
6931 case RID_ADDRESSOF:
6932 case RID_BUILTIN_SHUFFLE:
6933 case RID_BUILTIN_LAUNDER:
6935 vec<tree, va_gc> *vec;
6936 unsigned int i;
6937 tree p;
6939 cp_lexer_consume_token (parser->lexer);
6940 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6941 /*cast_p=*/false, /*allow_expansion_p=*/true,
6942 /*non_constant_p=*/NULL);
6943 if (vec == NULL)
6945 postfix_expression = error_mark_node;
6946 break;
6949 FOR_EACH_VEC_ELT (*vec, i, p)
6950 mark_exp_read (p);
6952 switch (keyword)
6954 case RID_ADDRESSOF:
6955 if (vec->length () == 1)
6956 postfix_expression
6957 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6958 else
6960 error_at (loc, "wrong number of arguments to "
6961 "%<__builtin_addressof%>");
6962 postfix_expression = error_mark_node;
6964 break;
6966 case RID_BUILTIN_LAUNDER:
6967 if (vec->length () == 1)
6968 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6969 tf_warning_or_error);
6970 else
6972 error_at (loc, "wrong number of arguments to "
6973 "%<__builtin_launder%>");
6974 postfix_expression = error_mark_node;
6976 break;
6978 case RID_BUILTIN_SHUFFLE:
6979 if (vec->length () == 2)
6980 postfix_expression
6981 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6982 (*vec)[1], tf_warning_or_error);
6983 else if (vec->length () == 3)
6984 postfix_expression
6985 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6986 (*vec)[2], tf_warning_or_error);
6987 else
6989 error_at (loc, "wrong number of arguments to "
6990 "%<__builtin_shuffle%>");
6991 postfix_expression = error_mark_node;
6993 break;
6995 default:
6996 gcc_unreachable ();
6998 break;
7001 default:
7003 tree type;
7005 /* If the next thing is a simple-type-specifier, we may be
7006 looking at a functional cast. We could also be looking at
7007 an id-expression. So, we try the functional cast, and if
7008 that doesn't work we fall back to the primary-expression. */
7009 cp_parser_parse_tentatively (parser);
7010 /* Look for the simple-type-specifier. */
7011 ++parser->prevent_constrained_type_specifiers;
7012 type = cp_parser_simple_type_specifier (parser,
7013 /*decl_specs=*/NULL,
7014 CP_PARSER_FLAGS_NONE);
7015 --parser->prevent_constrained_type_specifiers;
7016 /* Parse the cast itself. */
7017 if (!cp_parser_error_occurred (parser))
7018 postfix_expression
7019 = cp_parser_functional_cast (parser, type);
7020 /* If that worked, we're done. */
7021 if (cp_parser_parse_definitely (parser))
7022 break;
7024 /* If the functional-cast didn't work out, try a
7025 compound-literal. */
7026 if (cp_parser_allow_gnu_extensions_p (parser)
7027 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7029 cp_expr initializer = NULL_TREE;
7031 cp_parser_parse_tentatively (parser);
7033 matching_parens parens;
7034 parens.consume_open (parser);
7036 /* Avoid calling cp_parser_type_id pointlessly, see comment
7037 in cp_parser_cast_expression about c++/29234. */
7038 if (!cp_parser_compound_literal_p (parser))
7039 cp_parser_simulate_error (parser);
7040 else
7042 /* Parse the type. */
7043 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7044 parser->in_type_id_in_expr_p = true;
7045 type = cp_parser_type_id (parser);
7046 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7047 parens.require_close (parser);
7050 /* If things aren't going well, there's no need to
7051 keep going. */
7052 if (!cp_parser_error_occurred (parser))
7054 bool non_constant_p;
7055 /* Parse the brace-enclosed initializer list. */
7056 initializer = cp_parser_braced_list (parser,
7057 &non_constant_p);
7059 /* If that worked, we're definitely looking at a
7060 compound-literal expression. */
7061 if (cp_parser_parse_definitely (parser))
7063 /* Warn the user that a compound literal is not
7064 allowed in standard C++. */
7065 pedwarn (input_location, OPT_Wpedantic,
7066 "ISO C++ forbids compound-literals");
7067 /* For simplicity, we disallow compound literals in
7068 constant-expressions. We could
7069 allow compound literals of integer type, whose
7070 initializer was a constant, in constant
7071 expressions. Permitting that usage, as a further
7072 extension, would not change the meaning of any
7073 currently accepted programs. (Of course, as
7074 compound literals are not part of ISO C++, the
7075 standard has nothing to say.) */
7076 if (cp_parser_non_integral_constant_expression (parser,
7077 NIC_NCC))
7079 postfix_expression = error_mark_node;
7080 break;
7082 /* Form the representation of the compound-literal. */
7083 postfix_expression
7084 = finish_compound_literal (type, initializer,
7085 tf_warning_or_error, fcl_c99);
7086 postfix_expression.set_location (initializer.get_location ());
7087 break;
7091 /* It must be a primary-expression. */
7092 postfix_expression
7093 = cp_parser_primary_expression (parser, address_p, cast_p,
7094 /*template_arg_p=*/false,
7095 decltype_p,
7096 &idk);
7098 break;
7101 /* Note that we don't need to worry about calling build_cplus_new on a
7102 class-valued CALL_EXPR in decltype when it isn't the end of the
7103 postfix-expression; unary_complex_lvalue will take care of that for
7104 all these cases. */
7106 /* Keep looping until the postfix-expression is complete. */
7107 while (true)
7109 if (idk == CP_ID_KIND_UNQUALIFIED
7110 && identifier_p (postfix_expression)
7111 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7112 /* It is not a Koenig lookup function call. */
7113 postfix_expression
7114 = unqualified_name_lookup_error (postfix_expression);
7116 /* Peek at the next token. */
7117 token = cp_lexer_peek_token (parser->lexer);
7119 switch (token->type)
7121 case CPP_OPEN_SQUARE:
7122 if (cp_next_tokens_can_be_std_attribute_p (parser))
7124 cp_parser_error (parser,
7125 "two consecutive %<[%> shall "
7126 "only introduce an attribute");
7127 return error_mark_node;
7129 postfix_expression
7130 = cp_parser_postfix_open_square_expression (parser,
7131 postfix_expression,
7132 false,
7133 decltype_p);
7134 postfix_expression.set_range (start_loc,
7135 postfix_expression.get_location ());
7137 idk = CP_ID_KIND_NONE;
7138 is_member_access = false;
7139 break;
7141 case CPP_OPEN_PAREN:
7142 /* postfix-expression ( expression-list [opt] ) */
7144 bool koenig_p;
7145 bool is_builtin_constant_p;
7146 bool saved_integral_constant_expression_p = false;
7147 bool saved_non_integral_constant_expression_p = false;
7148 tsubst_flags_t complain = complain_flags (decltype_p);
7149 vec<tree, va_gc> *args;
7150 location_t close_paren_loc = UNKNOWN_LOCATION;
7152 is_member_access = false;
7154 is_builtin_constant_p
7155 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7156 if (is_builtin_constant_p)
7158 /* The whole point of __builtin_constant_p is to allow
7159 non-constant expressions to appear as arguments. */
7160 saved_integral_constant_expression_p
7161 = parser->integral_constant_expression_p;
7162 saved_non_integral_constant_expression_p
7163 = parser->non_integral_constant_expression_p;
7164 parser->integral_constant_expression_p = false;
7166 args = (cp_parser_parenthesized_expression_list
7167 (parser, non_attr,
7168 /*cast_p=*/false, /*allow_expansion_p=*/true,
7169 /*non_constant_p=*/NULL,
7170 /*close_paren_loc=*/&close_paren_loc,
7171 /*wrap_locations_p=*/true));
7172 if (is_builtin_constant_p)
7174 parser->integral_constant_expression_p
7175 = saved_integral_constant_expression_p;
7176 parser->non_integral_constant_expression_p
7177 = saved_non_integral_constant_expression_p;
7180 if (args == NULL)
7182 postfix_expression = error_mark_node;
7183 break;
7186 /* Function calls are not permitted in
7187 constant-expressions. */
7188 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7189 && cp_parser_non_integral_constant_expression (parser,
7190 NIC_FUNC_CALL))
7192 postfix_expression = error_mark_node;
7193 release_tree_vector (args);
7194 break;
7197 koenig_p = false;
7198 if (idk == CP_ID_KIND_UNQUALIFIED
7199 || idk == CP_ID_KIND_TEMPLATE_ID)
7201 if (identifier_p (postfix_expression)
7202 /* In C++2A, we may need to perform ADL for a template
7203 name. */
7204 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7205 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7207 if (!args->is_empty ())
7209 koenig_p = true;
7210 if (!any_type_dependent_arguments_p (args))
7211 postfix_expression
7212 = perform_koenig_lookup (postfix_expression, args,
7213 complain);
7215 else
7216 postfix_expression
7217 = unqualified_fn_lookup_error (postfix_expression);
7219 /* We do not perform argument-dependent lookup if
7220 normal lookup finds a non-function, in accordance
7221 with the expected resolution of DR 218. */
7222 else if (!args->is_empty ()
7223 && is_overloaded_fn (postfix_expression))
7225 tree fn = get_first_fn (postfix_expression);
7226 fn = STRIP_TEMPLATE (fn);
7228 /* Do not do argument dependent lookup if regular
7229 lookup finds a member function or a block-scope
7230 function declaration. [basic.lookup.argdep]/3 */
7231 if (!DECL_FUNCTION_MEMBER_P (fn)
7232 && !DECL_LOCAL_FUNCTION_P (fn))
7234 koenig_p = true;
7235 if (!any_type_dependent_arguments_p (args))
7236 postfix_expression
7237 = perform_koenig_lookup (postfix_expression, args,
7238 complain);
7243 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7245 tree instance = TREE_OPERAND (postfix_expression, 0);
7246 tree fn = TREE_OPERAND (postfix_expression, 1);
7248 if (processing_template_decl
7249 && (type_dependent_object_expression_p (instance)
7250 || (!BASELINK_P (fn)
7251 && TREE_CODE (fn) != FIELD_DECL)
7252 || type_dependent_expression_p (fn)
7253 || any_type_dependent_arguments_p (args)))
7255 maybe_generic_this_capture (instance, fn);
7256 postfix_expression
7257 = build_min_nt_call_vec (postfix_expression, args);
7258 release_tree_vector (args);
7259 break;
7262 if (BASELINK_P (fn))
7264 postfix_expression
7265 = (build_new_method_call
7266 (instance, fn, &args, NULL_TREE,
7267 (idk == CP_ID_KIND_QUALIFIED
7268 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7269 : LOOKUP_NORMAL),
7270 /*fn_p=*/NULL,
7271 complain));
7273 else
7274 postfix_expression
7275 = finish_call_expr (postfix_expression, &args,
7276 /*disallow_virtual=*/false,
7277 /*koenig_p=*/false,
7278 complain);
7280 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7281 || TREE_CODE (postfix_expression) == MEMBER_REF
7282 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7283 postfix_expression = (build_offset_ref_call_from_tree
7284 (postfix_expression, &args,
7285 complain));
7286 else if (idk == CP_ID_KIND_QUALIFIED)
7287 /* A call to a static class member, or a namespace-scope
7288 function. */
7289 postfix_expression
7290 = finish_call_expr (postfix_expression, &args,
7291 /*disallow_virtual=*/true,
7292 koenig_p,
7293 complain);
7294 else
7295 /* All other function calls. */
7296 postfix_expression
7297 = finish_call_expr (postfix_expression, &args,
7298 /*disallow_virtual=*/false,
7299 koenig_p,
7300 complain);
7302 if (close_paren_loc != UNKNOWN_LOCATION)
7304 location_t combined_loc = make_location (token->location,
7305 start_loc,
7306 close_paren_loc);
7307 postfix_expression.set_location (combined_loc);
7310 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7311 idk = CP_ID_KIND_NONE;
7313 release_tree_vector (args);
7315 break;
7317 case CPP_DOT:
7318 case CPP_DEREF:
7319 /* postfix-expression . template [opt] id-expression
7320 postfix-expression . pseudo-destructor-name
7321 postfix-expression -> template [opt] id-expression
7322 postfix-expression -> pseudo-destructor-name */
7324 /* Consume the `.' or `->' operator. */
7325 cp_lexer_consume_token (parser->lexer);
7327 postfix_expression
7328 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7329 postfix_expression,
7330 false, &idk, loc);
7332 is_member_access = true;
7333 break;
7335 case CPP_PLUS_PLUS:
7336 /* postfix-expression ++ */
7337 /* Consume the `++' token. */
7338 cp_lexer_consume_token (parser->lexer);
7339 /* Generate a representation for the complete expression. */
7340 postfix_expression
7341 = finish_increment_expr (postfix_expression,
7342 POSTINCREMENT_EXPR);
7343 /* Increments may not appear in constant-expressions. */
7344 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7345 postfix_expression = error_mark_node;
7346 idk = CP_ID_KIND_NONE;
7347 is_member_access = false;
7348 break;
7350 case CPP_MINUS_MINUS:
7351 /* postfix-expression -- */
7352 /* Consume the `--' token. */
7353 cp_lexer_consume_token (parser->lexer);
7354 /* Generate a representation for the complete expression. */
7355 postfix_expression
7356 = finish_increment_expr (postfix_expression,
7357 POSTDECREMENT_EXPR);
7358 /* Decrements may not appear in constant-expressions. */
7359 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7360 postfix_expression = error_mark_node;
7361 idk = CP_ID_KIND_NONE;
7362 is_member_access = false;
7363 break;
7365 default:
7366 if (pidk_return != NULL)
7367 * pidk_return = idk;
7368 if (member_access_only_p)
7369 return is_member_access
7370 ? postfix_expression
7371 : cp_expr (error_mark_node);
7372 else
7373 return postfix_expression;
7377 /* We should never get here. */
7378 gcc_unreachable ();
7379 return error_mark_node;
7382 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7383 by cp_parser_builtin_offsetof. We're looking for
7385 postfix-expression [ expression ]
7386 postfix-expression [ braced-init-list ] (C++11)
7388 FOR_OFFSETOF is set if we're being called in that context, which
7389 changes how we deal with integer constant expressions. */
7391 static tree
7392 cp_parser_postfix_open_square_expression (cp_parser *parser,
7393 tree postfix_expression,
7394 bool for_offsetof,
7395 bool decltype_p)
7397 tree index = NULL_TREE;
7398 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7399 bool saved_greater_than_is_operator_p;
7401 /* Consume the `[' token. */
7402 cp_lexer_consume_token (parser->lexer);
7404 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7405 parser->greater_than_is_operator_p = true;
7407 /* Parse the index expression. */
7408 /* ??? For offsetof, there is a question of what to allow here. If
7409 offsetof is not being used in an integral constant expression context,
7410 then we *could* get the right answer by computing the value at runtime.
7411 If we are in an integral constant expression context, then we might
7412 could accept any constant expression; hard to say without analysis.
7413 Rather than open the barn door too wide right away, allow only integer
7414 constant expressions here. */
7415 if (for_offsetof)
7416 index = cp_parser_constant_expression (parser);
7417 else
7419 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7421 bool expr_nonconst_p;
7422 cp_lexer_set_source_position (parser->lexer);
7423 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7424 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7426 else
7427 index = cp_parser_expression (parser);
7430 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7432 /* Look for the closing `]'. */
7433 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7435 /* Build the ARRAY_REF. */
7436 postfix_expression = grok_array_decl (loc, postfix_expression,
7437 index, decltype_p);
7439 /* When not doing offsetof, array references are not permitted in
7440 constant-expressions. */
7441 if (!for_offsetof
7442 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7443 postfix_expression = error_mark_node;
7445 return postfix_expression;
7448 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7449 dereference of incomplete type, returns true if error_mark_node should
7450 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7451 and *DEPENDENT_P. */
7453 bool
7454 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7455 bool *dependent_p)
7457 /* In a template, be permissive by treating an object expression
7458 of incomplete type as dependent (after a pedwarn). */
7459 diagnostic_t kind = (processing_template_decl
7460 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7462 switch (TREE_CODE (*postfix_expression))
7464 case CAST_EXPR:
7465 case REINTERPRET_CAST_EXPR:
7466 case CONST_CAST_EXPR:
7467 case STATIC_CAST_EXPR:
7468 case DYNAMIC_CAST_EXPR:
7469 case IMPLICIT_CONV_EXPR:
7470 case VIEW_CONVERT_EXPR:
7471 case NON_LVALUE_EXPR:
7472 kind = DK_ERROR;
7473 break;
7474 case OVERLOAD:
7475 /* Don't emit any diagnostic for OVERLOADs. */
7476 kind = DK_IGNORED;
7477 break;
7478 default:
7479 /* Avoid clobbering e.g. DECLs. */
7480 if (!EXPR_P (*postfix_expression))
7481 kind = DK_ERROR;
7482 break;
7485 if (kind == DK_IGNORED)
7486 return false;
7488 location_t exploc = location_of (*postfix_expression);
7489 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7490 if (!MAYBE_CLASS_TYPE_P (*scope))
7491 return true;
7492 if (kind == DK_ERROR)
7493 *scope = *postfix_expression = error_mark_node;
7494 else if (processing_template_decl)
7496 *dependent_p = true;
7497 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7499 return false;
7502 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7503 by cp_parser_builtin_offsetof. We're looking for
7505 postfix-expression . template [opt] id-expression
7506 postfix-expression . pseudo-destructor-name
7507 postfix-expression -> template [opt] id-expression
7508 postfix-expression -> pseudo-destructor-name
7510 FOR_OFFSETOF is set if we're being called in that context. That sorta
7511 limits what of the above we'll actually accept, but nevermind.
7512 TOKEN_TYPE is the "." or "->" token, which will already have been
7513 removed from the stream. */
7515 static tree
7516 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7517 enum cpp_ttype token_type,
7518 cp_expr postfix_expression,
7519 bool for_offsetof, cp_id_kind *idk,
7520 location_t location)
7522 tree name;
7523 bool dependent_p;
7524 bool pseudo_destructor_p;
7525 tree scope = NULL_TREE;
7526 location_t start_loc = postfix_expression.get_start ();
7528 /* If this is a `->' operator, dereference the pointer. */
7529 if (token_type == CPP_DEREF)
7530 postfix_expression = build_x_arrow (location, postfix_expression,
7531 tf_warning_or_error);
7532 /* Check to see whether or not the expression is type-dependent and
7533 not the current instantiation. */
7534 dependent_p = type_dependent_object_expression_p (postfix_expression);
7535 /* The identifier following the `->' or `.' is not qualified. */
7536 parser->scope = NULL_TREE;
7537 parser->qualifying_scope = NULL_TREE;
7538 parser->object_scope = NULL_TREE;
7539 *idk = CP_ID_KIND_NONE;
7541 /* Enter the scope corresponding to the type of the object
7542 given by the POSTFIX_EXPRESSION. */
7543 if (!dependent_p)
7545 scope = TREE_TYPE (postfix_expression);
7546 /* According to the standard, no expression should ever have
7547 reference type. Unfortunately, we do not currently match
7548 the standard in this respect in that our internal representation
7549 of an expression may have reference type even when the standard
7550 says it does not. Therefore, we have to manually obtain the
7551 underlying type here. */
7552 scope = non_reference (scope);
7553 /* The type of the POSTFIX_EXPRESSION must be complete. */
7554 /* Unlike the object expression in other contexts, *this is not
7555 required to be of complete type for purposes of class member
7556 access (5.2.5) outside the member function body. */
7557 if (postfix_expression != current_class_ref
7558 && scope != error_mark_node
7559 && !currently_open_class (scope))
7561 scope = complete_type (scope);
7562 if (!COMPLETE_TYPE_P (scope)
7563 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7564 &dependent_p))
7565 return error_mark_node;
7568 if (!dependent_p)
7570 /* Let the name lookup machinery know that we are processing a
7571 class member access expression. */
7572 parser->context->object_type = scope;
7573 /* If something went wrong, we want to be able to discern that case,
7574 as opposed to the case where there was no SCOPE due to the type
7575 of expression being dependent. */
7576 if (!scope)
7577 scope = error_mark_node;
7578 /* If the SCOPE was erroneous, make the various semantic analysis
7579 functions exit quickly -- and without issuing additional error
7580 messages. */
7581 if (scope == error_mark_node)
7582 postfix_expression = error_mark_node;
7586 if (dependent_p)
7587 /* Tell cp_parser_lookup_name that there was an object, even though it's
7588 type-dependent. */
7589 parser->context->object_type = unknown_type_node;
7591 /* Assume this expression is not a pseudo-destructor access. */
7592 pseudo_destructor_p = false;
7594 /* If the SCOPE is a scalar type, then, if this is a valid program,
7595 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7596 is type dependent, it can be pseudo-destructor-name or something else.
7597 Try to parse it as pseudo-destructor-name first. */
7598 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7600 tree s;
7601 tree type;
7603 cp_parser_parse_tentatively (parser);
7604 /* Parse the pseudo-destructor-name. */
7605 s = NULL_TREE;
7606 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7607 &s, &type);
7608 if (dependent_p
7609 && (cp_parser_error_occurred (parser)
7610 || !SCALAR_TYPE_P (type)))
7611 cp_parser_abort_tentative_parse (parser);
7612 else if (cp_parser_parse_definitely (parser))
7614 pseudo_destructor_p = true;
7615 postfix_expression
7616 = finish_pseudo_destructor_expr (postfix_expression,
7617 s, type, location);
7621 if (!pseudo_destructor_p)
7623 /* If the SCOPE is not a scalar type, we are looking at an
7624 ordinary class member access expression, rather than a
7625 pseudo-destructor-name. */
7626 bool template_p;
7627 cp_token *token = cp_lexer_peek_token (parser->lexer);
7628 /* Parse the id-expression. */
7629 name = (cp_parser_id_expression
7630 (parser,
7631 cp_parser_optional_template_keyword (parser),
7632 /*check_dependency_p=*/true,
7633 &template_p,
7634 /*declarator_p=*/false,
7635 /*optional_p=*/false));
7636 /* In general, build a SCOPE_REF if the member name is qualified.
7637 However, if the name was not dependent and has already been
7638 resolved; there is no need to build the SCOPE_REF. For example;
7640 struct X { void f(); };
7641 template <typename T> void f(T* t) { t->X::f(); }
7643 Even though "t" is dependent, "X::f" is not and has been resolved
7644 to a BASELINK; there is no need to include scope information. */
7646 /* But we do need to remember that there was an explicit scope for
7647 virtual function calls. */
7648 if (parser->scope)
7649 *idk = CP_ID_KIND_QUALIFIED;
7651 /* If the name is a template-id that names a type, we will get a
7652 TYPE_DECL here. That is invalid code. */
7653 if (TREE_CODE (name) == TYPE_DECL)
7655 error_at (token->location, "invalid use of %qD", name);
7656 postfix_expression = error_mark_node;
7658 else
7660 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7662 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7664 error_at (token->location, "%<%D::%D%> is not a class member",
7665 parser->scope, name);
7666 postfix_expression = error_mark_node;
7668 else
7669 name = build_qualified_name (/*type=*/NULL_TREE,
7670 parser->scope,
7671 name,
7672 template_p);
7673 parser->scope = NULL_TREE;
7674 parser->qualifying_scope = NULL_TREE;
7675 parser->object_scope = NULL_TREE;
7677 if (parser->scope && name && BASELINK_P (name))
7678 adjust_result_of_qualified_name_lookup
7679 (name, parser->scope, scope);
7680 postfix_expression
7681 = finish_class_member_access_expr (postfix_expression, name,
7682 template_p,
7683 tf_warning_or_error);
7684 /* Build a location e.g.:
7685 ptr->access_expr
7686 ~~~^~~~~~~~~~~~~
7687 where the caret is at the deref token, ranging from
7688 the start of postfix_expression to the end of the access expr. */
7689 location_t end_loc
7690 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7691 location_t combined_loc
7692 = make_location (input_location, start_loc, end_loc);
7693 protected_set_expr_location (postfix_expression, combined_loc);
7697 /* We no longer need to look up names in the scope of the object on
7698 the left-hand side of the `.' or `->' operator. */
7699 parser->context->object_type = NULL_TREE;
7701 /* Outside of offsetof, these operators may not appear in
7702 constant-expressions. */
7703 if (!for_offsetof
7704 && (cp_parser_non_integral_constant_expression
7705 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7706 postfix_expression = error_mark_node;
7708 return postfix_expression;
7711 /* Parse a parenthesized expression-list.
7713 expression-list:
7714 assignment-expression
7715 expression-list, assignment-expression
7717 attribute-list:
7718 expression-list
7719 identifier
7720 identifier, expression-list
7722 CAST_P is true if this expression is the target of a cast.
7724 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7725 argument pack.
7727 WRAP_LOCATIONS_P is true if expressions within this list for which
7728 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7729 their source locations.
7731 Returns a vector of trees. Each element is a representation of an
7732 assignment-expression. NULL is returned if the ( and or ) are
7733 missing. An empty, but allocated, vector is returned on no
7734 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7735 if we are parsing an attribute list for an attribute that wants a
7736 plain identifier argument, normal_attr for an attribute that wants
7737 an expression, or non_attr if we aren't parsing an attribute list. If
7738 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7739 not all of the expressions in the list were constant.
7740 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7741 will be written to with the location of the closing parenthesis. If
7742 an error occurs, it may or may not be written to. */
7744 static vec<tree, va_gc> *
7745 cp_parser_parenthesized_expression_list (cp_parser* parser,
7746 int is_attribute_list,
7747 bool cast_p,
7748 bool allow_expansion_p,
7749 bool *non_constant_p,
7750 location_t *close_paren_loc,
7751 bool wrap_locations_p)
7753 vec<tree, va_gc> *expression_list;
7754 bool fold_expr_p = is_attribute_list != non_attr;
7755 tree identifier = NULL_TREE;
7756 bool saved_greater_than_is_operator_p;
7758 /* Assume all the expressions will be constant. */
7759 if (non_constant_p)
7760 *non_constant_p = false;
7762 matching_parens parens;
7763 if (!parens.require_open (parser))
7764 return NULL;
7766 expression_list = make_tree_vector ();
7768 /* Within a parenthesized expression, a `>' token is always
7769 the greater-than operator. */
7770 saved_greater_than_is_operator_p
7771 = parser->greater_than_is_operator_p;
7772 parser->greater_than_is_operator_p = true;
7774 cp_expr expr (NULL_TREE);
7776 /* Consume expressions until there are no more. */
7777 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7778 while (true)
7780 /* At the beginning of attribute lists, check to see if the
7781 next token is an identifier. */
7782 if (is_attribute_list == id_attr
7783 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7785 cp_token *token;
7787 /* Consume the identifier. */
7788 token = cp_lexer_consume_token (parser->lexer);
7789 /* Save the identifier. */
7790 identifier = token->u.value;
7792 else
7794 bool expr_non_constant_p;
7796 /* Parse the next assignment-expression. */
7797 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7799 /* A braced-init-list. */
7800 cp_lexer_set_source_position (parser->lexer);
7801 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7802 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7803 if (non_constant_p && expr_non_constant_p)
7804 *non_constant_p = true;
7806 else if (non_constant_p)
7808 expr = (cp_parser_constant_expression
7809 (parser, /*allow_non_constant_p=*/true,
7810 &expr_non_constant_p));
7811 if (expr_non_constant_p)
7812 *non_constant_p = true;
7814 else
7815 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7816 cast_p);
7818 if (fold_expr_p)
7819 expr = instantiate_non_dependent_expr (expr);
7821 /* If we have an ellipsis, then this is an expression
7822 expansion. */
7823 if (allow_expansion_p
7824 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7826 /* Consume the `...'. */
7827 cp_lexer_consume_token (parser->lexer);
7829 /* Build the argument pack. */
7830 expr = make_pack_expansion (expr);
7833 if (wrap_locations_p)
7834 expr.maybe_add_location_wrapper ();
7836 /* Add it to the list. We add error_mark_node
7837 expressions to the list, so that we can still tell if
7838 the correct form for a parenthesized expression-list
7839 is found. That gives better errors. */
7840 vec_safe_push (expression_list, expr.get_value ());
7842 if (expr == error_mark_node)
7843 goto skip_comma;
7846 /* After the first item, attribute lists look the same as
7847 expression lists. */
7848 is_attribute_list = non_attr;
7850 get_comma:;
7851 /* If the next token isn't a `,', then we are done. */
7852 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7853 break;
7855 /* Otherwise, consume the `,' and keep going. */
7856 cp_lexer_consume_token (parser->lexer);
7859 if (close_paren_loc)
7860 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7862 if (!parens.require_close (parser))
7864 int ending;
7866 skip_comma:;
7867 /* We try and resync to an unnested comma, as that will give the
7868 user better diagnostics. */
7869 ending = cp_parser_skip_to_closing_parenthesis (parser,
7870 /*recovering=*/true,
7871 /*or_comma=*/true,
7872 /*consume_paren=*/true);
7873 if (ending < 0)
7874 goto get_comma;
7875 if (!ending)
7877 parser->greater_than_is_operator_p
7878 = saved_greater_than_is_operator_p;
7879 return NULL;
7883 parser->greater_than_is_operator_p
7884 = saved_greater_than_is_operator_p;
7886 if (identifier)
7887 vec_safe_insert (expression_list, 0, identifier);
7889 return expression_list;
7892 /* Parse a pseudo-destructor-name.
7894 pseudo-destructor-name:
7895 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7896 :: [opt] nested-name-specifier template template-id :: ~ type-name
7897 :: [opt] nested-name-specifier [opt] ~ type-name
7899 If either of the first two productions is used, sets *SCOPE to the
7900 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7901 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7902 or ERROR_MARK_NODE if the parse fails. */
7904 static void
7905 cp_parser_pseudo_destructor_name (cp_parser* parser,
7906 tree object,
7907 tree* scope,
7908 tree* type)
7910 bool nested_name_specifier_p;
7912 /* Handle ~auto. */
7913 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7914 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7915 && !type_dependent_expression_p (object))
7917 if (cxx_dialect < cxx14)
7918 pedwarn (input_location, 0,
7919 "%<~auto%> only available with "
7920 "-std=c++14 or -std=gnu++14");
7921 cp_lexer_consume_token (parser->lexer);
7922 cp_lexer_consume_token (parser->lexer);
7923 *scope = NULL_TREE;
7924 *type = TREE_TYPE (object);
7925 return;
7928 /* Assume that things will not work out. */
7929 *type = error_mark_node;
7931 /* Look for the optional `::' operator. */
7932 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7933 /* Look for the optional nested-name-specifier. */
7934 nested_name_specifier_p
7935 = (cp_parser_nested_name_specifier_opt (parser,
7936 /*typename_keyword_p=*/false,
7937 /*check_dependency_p=*/true,
7938 /*type_p=*/false,
7939 /*is_declaration=*/false)
7940 != NULL_TREE);
7941 /* Now, if we saw a nested-name-specifier, we might be doing the
7942 second production. */
7943 if (nested_name_specifier_p
7944 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7946 /* Consume the `template' keyword. */
7947 cp_lexer_consume_token (parser->lexer);
7948 /* Parse the template-id. */
7949 cp_parser_template_id (parser,
7950 /*template_keyword_p=*/true,
7951 /*check_dependency_p=*/false,
7952 class_type,
7953 /*is_declaration=*/true);
7954 /* Look for the `::' token. */
7955 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7957 /* If the next token is not a `~', then there might be some
7958 additional qualification. */
7959 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7961 /* At this point, we're looking for "type-name :: ~". The type-name
7962 must not be a class-name, since this is a pseudo-destructor. So,
7963 it must be either an enum-name, or a typedef-name -- both of which
7964 are just identifiers. So, we peek ahead to check that the "::"
7965 and "~" tokens are present; if they are not, then we can avoid
7966 calling type_name. */
7967 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7968 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7969 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7971 cp_parser_error (parser, "non-scalar type");
7972 return;
7975 /* Look for the type-name. */
7976 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7977 if (*scope == error_mark_node)
7978 return;
7980 /* Look for the `::' token. */
7981 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7983 else
7984 *scope = NULL_TREE;
7986 /* Look for the `~'. */
7987 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7989 /* Once we see the ~, this has to be a pseudo-destructor. */
7990 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7991 cp_parser_commit_to_topmost_tentative_parse (parser);
7993 /* Look for the type-name again. We are not responsible for
7994 checking that it matches the first type-name. */
7995 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7998 /* Parse a unary-expression.
8000 unary-expression:
8001 postfix-expression
8002 ++ cast-expression
8003 -- cast-expression
8004 unary-operator cast-expression
8005 sizeof unary-expression
8006 sizeof ( type-id )
8007 alignof ( type-id ) [C++0x]
8008 new-expression
8009 delete-expression
8011 GNU Extensions:
8013 unary-expression:
8014 __extension__ cast-expression
8015 __alignof__ unary-expression
8016 __alignof__ ( type-id )
8017 alignof unary-expression [C++0x]
8018 __real__ cast-expression
8019 __imag__ cast-expression
8020 && identifier
8021 sizeof ( type-id ) { initializer-list , [opt] }
8022 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8023 __alignof__ ( type-id ) { initializer-list , [opt] }
8025 ADDRESS_P is true iff the unary-expression is appearing as the
8026 operand of the `&' operator. CAST_P is true if this expression is
8027 the target of a cast.
8029 Returns a representation of the expression. */
8031 static cp_expr
8032 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8033 bool address_p, bool cast_p, bool decltype_p)
8035 cp_token *token;
8036 enum tree_code unary_operator;
8038 /* Peek at the next token. */
8039 token = cp_lexer_peek_token (parser->lexer);
8040 /* Some keywords give away the kind of expression. */
8041 if (token->type == CPP_KEYWORD)
8043 enum rid keyword = token->keyword;
8045 switch (keyword)
8047 case RID_ALIGNOF:
8048 case RID_SIZEOF:
8050 tree operand, ret;
8051 enum tree_code op;
8052 location_t start_loc = token->location;
8054 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8055 bool std_alignof = id_equal (token->u.value, "alignof");
8057 /* Consume the token. */
8058 cp_lexer_consume_token (parser->lexer);
8059 /* Parse the operand. */
8060 operand = cp_parser_sizeof_operand (parser, keyword);
8062 if (TYPE_P (operand))
8063 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8064 true);
8065 else
8067 /* ISO C++ defines alignof only with types, not with
8068 expressions. So pedwarn if alignof is used with a non-
8069 type expression. However, __alignof__ is ok. */
8070 if (std_alignof)
8071 pedwarn (token->location, OPT_Wpedantic,
8072 "ISO C++ does not allow %<alignof%> "
8073 "with a non-type");
8075 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8077 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8078 SIZEOF_EXPR with the original operand. */
8079 if (op == SIZEOF_EXPR && ret != error_mark_node)
8081 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8083 if (!processing_template_decl && TYPE_P (operand))
8085 ret = build_min (SIZEOF_EXPR, size_type_node,
8086 build1 (NOP_EXPR, operand,
8087 error_mark_node));
8088 SIZEOF_EXPR_TYPE_P (ret) = 1;
8090 else
8091 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8092 TREE_SIDE_EFFECTS (ret) = 0;
8093 TREE_READONLY (ret) = 1;
8097 /* Construct a location e.g. :
8098 alignof (expr)
8099 ^~~~~~~~~~~~~~
8100 with start == caret at the start of the "alignof"/"sizeof"
8101 token, with the endpoint at the final closing paren. */
8102 location_t finish_loc
8103 = cp_lexer_previous_token (parser->lexer)->location;
8104 location_t compound_loc
8105 = make_location (start_loc, start_loc, finish_loc);
8107 cp_expr ret_expr (ret);
8108 ret_expr.set_location (compound_loc);
8109 ret_expr = ret_expr.maybe_add_location_wrapper ();
8110 return ret_expr;
8113 case RID_NEW:
8114 return cp_parser_new_expression (parser);
8116 case RID_DELETE:
8117 return cp_parser_delete_expression (parser);
8119 case RID_EXTENSION:
8121 /* The saved value of the PEDANTIC flag. */
8122 int saved_pedantic;
8123 tree expr;
8125 /* Save away the PEDANTIC flag. */
8126 cp_parser_extension_opt (parser, &saved_pedantic);
8127 /* Parse the cast-expression. */
8128 expr = cp_parser_simple_cast_expression (parser);
8129 /* Restore the PEDANTIC flag. */
8130 pedantic = saved_pedantic;
8132 return expr;
8135 case RID_REALPART:
8136 case RID_IMAGPART:
8138 tree expression;
8140 /* Consume the `__real__' or `__imag__' token. */
8141 cp_lexer_consume_token (parser->lexer);
8142 /* Parse the cast-expression. */
8143 expression = cp_parser_simple_cast_expression (parser);
8144 /* Create the complete representation. */
8145 return build_x_unary_op (token->location,
8146 (keyword == RID_REALPART
8147 ? REALPART_EXPR : IMAGPART_EXPR),
8148 expression,
8149 tf_warning_or_error);
8151 break;
8153 case RID_TRANSACTION_ATOMIC:
8154 case RID_TRANSACTION_RELAXED:
8155 return cp_parser_transaction_expression (parser, keyword);
8157 case RID_NOEXCEPT:
8159 tree expr;
8160 const char *saved_message;
8161 bool saved_integral_constant_expression_p;
8162 bool saved_non_integral_constant_expression_p;
8163 bool saved_greater_than_is_operator_p;
8165 location_t start_loc = token->location;
8167 cp_lexer_consume_token (parser->lexer);
8168 matching_parens parens;
8169 parens.require_open (parser);
8171 saved_message = parser->type_definition_forbidden_message;
8172 parser->type_definition_forbidden_message
8173 = G_("types may not be defined in %<noexcept%> expressions");
8175 saved_integral_constant_expression_p
8176 = parser->integral_constant_expression_p;
8177 saved_non_integral_constant_expression_p
8178 = parser->non_integral_constant_expression_p;
8179 parser->integral_constant_expression_p = false;
8181 saved_greater_than_is_operator_p
8182 = parser->greater_than_is_operator_p;
8183 parser->greater_than_is_operator_p = true;
8185 ++cp_unevaluated_operand;
8186 ++c_inhibit_evaluation_warnings;
8187 ++cp_noexcept_operand;
8188 expr = cp_parser_expression (parser);
8189 --cp_noexcept_operand;
8190 --c_inhibit_evaluation_warnings;
8191 --cp_unevaluated_operand;
8193 parser->greater_than_is_operator_p
8194 = saved_greater_than_is_operator_p;
8196 parser->integral_constant_expression_p
8197 = saved_integral_constant_expression_p;
8198 parser->non_integral_constant_expression_p
8199 = saved_non_integral_constant_expression_p;
8201 parser->type_definition_forbidden_message = saved_message;
8203 location_t finish_loc
8204 = cp_lexer_peek_token (parser->lexer)->location;
8205 parens.require_close (parser);
8207 /* Construct a location of the form:
8208 noexcept (expr)
8209 ^~~~~~~~~~~~~~~
8210 with start == caret, finishing at the close-paren. */
8211 location_t noexcept_loc
8212 = make_location (start_loc, start_loc, finish_loc);
8214 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8215 noexcept_loc);
8218 default:
8219 break;
8223 /* Look for the `:: new' and `:: delete', which also signal the
8224 beginning of a new-expression, or delete-expression,
8225 respectively. If the next token is `::', then it might be one of
8226 these. */
8227 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8229 enum rid keyword;
8231 /* See if the token after the `::' is one of the keywords in
8232 which we're interested. */
8233 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8234 /* If it's `new', we have a new-expression. */
8235 if (keyword == RID_NEW)
8236 return cp_parser_new_expression (parser);
8237 /* Similarly, for `delete'. */
8238 else if (keyword == RID_DELETE)
8239 return cp_parser_delete_expression (parser);
8242 /* Look for a unary operator. */
8243 unary_operator = cp_parser_unary_operator (token);
8244 /* The `++' and `--' operators can be handled similarly, even though
8245 they are not technically unary-operators in the grammar. */
8246 if (unary_operator == ERROR_MARK)
8248 if (token->type == CPP_PLUS_PLUS)
8249 unary_operator = PREINCREMENT_EXPR;
8250 else if (token->type == CPP_MINUS_MINUS)
8251 unary_operator = PREDECREMENT_EXPR;
8252 /* Handle the GNU address-of-label extension. */
8253 else if (cp_parser_allow_gnu_extensions_p (parser)
8254 && token->type == CPP_AND_AND)
8256 tree identifier;
8257 tree expression;
8258 location_t start_loc = token->location;
8260 /* Consume the '&&' token. */
8261 cp_lexer_consume_token (parser->lexer);
8262 /* Look for the identifier. */
8263 location_t finish_loc
8264 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8265 identifier = cp_parser_identifier (parser);
8266 /* Construct a location of the form:
8267 &&label
8268 ^~~~~~~
8269 with caret==start at the "&&", finish at the end of the label. */
8270 location_t combined_loc
8271 = make_location (start_loc, start_loc, finish_loc);
8272 /* Create an expression representing the address. */
8273 expression = finish_label_address_expr (identifier, combined_loc);
8274 if (cp_parser_non_integral_constant_expression (parser,
8275 NIC_ADDR_LABEL))
8276 expression = error_mark_node;
8277 return expression;
8280 if (unary_operator != ERROR_MARK)
8282 cp_expr cast_expression;
8283 cp_expr expression = error_mark_node;
8284 non_integral_constant non_constant_p = NIC_NONE;
8285 location_t loc = token->location;
8286 tsubst_flags_t complain = complain_flags (decltype_p);
8288 /* Consume the operator token. */
8289 token = cp_lexer_consume_token (parser->lexer);
8290 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8292 /* Parse the cast-expression. */
8293 cast_expression
8294 = cp_parser_cast_expression (parser,
8295 unary_operator == ADDR_EXPR,
8296 /*cast_p=*/false,
8297 /*decltype*/false,
8298 pidk);
8300 /* Make a location:
8301 OP_TOKEN CAST_EXPRESSION
8302 ^~~~~~~~~~~~~~~~~~~~~~~~~
8303 with start==caret at the operator token, and
8304 extending to the end of the cast_expression. */
8305 loc = make_location (loc, loc, cast_expression.get_finish ());
8307 /* Now, build an appropriate representation. */
8308 switch (unary_operator)
8310 case INDIRECT_REF:
8311 non_constant_p = NIC_STAR;
8312 expression = build_x_indirect_ref (loc, cast_expression,
8313 RO_UNARY_STAR,
8314 complain);
8315 /* TODO: build_x_indirect_ref does not always honor the
8316 location, so ensure it is set. */
8317 expression.set_location (loc);
8318 break;
8320 case ADDR_EXPR:
8321 non_constant_p = NIC_ADDR;
8322 /* Fall through. */
8323 case BIT_NOT_EXPR:
8324 expression = build_x_unary_op (loc, unary_operator,
8325 cast_expression,
8326 complain);
8327 /* TODO: build_x_unary_op does not always honor the location,
8328 so ensure it is set. */
8329 expression.set_location (loc);
8330 break;
8332 case PREINCREMENT_EXPR:
8333 case PREDECREMENT_EXPR:
8334 non_constant_p = unary_operator == PREINCREMENT_EXPR
8335 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8336 /* Fall through. */
8337 case NEGATE_EXPR:
8338 /* Immediately fold negation of a constant, unless the constant is 0
8339 (since -0 == 0) or it would overflow. */
8340 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8341 && CONSTANT_CLASS_P (cast_expression)
8342 && !integer_zerop (cast_expression)
8343 && !TREE_OVERFLOW (cast_expression))
8345 tree folded = fold_build1 (unary_operator,
8346 TREE_TYPE (cast_expression),
8347 cast_expression);
8348 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8350 expression = cp_expr (folded, loc);
8351 break;
8354 /* Fall through. */
8355 case UNARY_PLUS_EXPR:
8356 case TRUTH_NOT_EXPR:
8357 expression = finish_unary_op_expr (loc, unary_operator,
8358 cast_expression, complain);
8359 break;
8361 default:
8362 gcc_unreachable ();
8365 if (non_constant_p != NIC_NONE
8366 && cp_parser_non_integral_constant_expression (parser,
8367 non_constant_p))
8368 expression = error_mark_node;
8370 return expression;
8373 return cp_parser_postfix_expression (parser, address_p, cast_p,
8374 /*member_access_only_p=*/false,
8375 decltype_p,
8376 pidk);
8379 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8380 unary-operator, the corresponding tree code is returned. */
8382 static enum tree_code
8383 cp_parser_unary_operator (cp_token* token)
8385 switch (token->type)
8387 case CPP_MULT:
8388 return INDIRECT_REF;
8390 case CPP_AND:
8391 return ADDR_EXPR;
8393 case CPP_PLUS:
8394 return UNARY_PLUS_EXPR;
8396 case CPP_MINUS:
8397 return NEGATE_EXPR;
8399 case CPP_NOT:
8400 return TRUTH_NOT_EXPR;
8402 case CPP_COMPL:
8403 return BIT_NOT_EXPR;
8405 default:
8406 return ERROR_MARK;
8410 /* Parse a new-expression.
8412 new-expression:
8413 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8414 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8416 Returns a representation of the expression. */
8418 static tree
8419 cp_parser_new_expression (cp_parser* parser)
8421 bool global_scope_p;
8422 vec<tree, va_gc> *placement;
8423 tree type;
8424 vec<tree, va_gc> *initializer;
8425 tree nelts = NULL_TREE;
8426 tree ret;
8428 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8430 /* Look for the optional `::' operator. */
8431 global_scope_p
8432 = (cp_parser_global_scope_opt (parser,
8433 /*current_scope_valid_p=*/false)
8434 != NULL_TREE);
8435 /* Look for the `new' operator. */
8436 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8437 /* There's no easy way to tell a new-placement from the
8438 `( type-id )' construct. */
8439 cp_parser_parse_tentatively (parser);
8440 /* Look for a new-placement. */
8441 placement = cp_parser_new_placement (parser);
8442 /* If that didn't work out, there's no new-placement. */
8443 if (!cp_parser_parse_definitely (parser))
8445 if (placement != NULL)
8446 release_tree_vector (placement);
8447 placement = NULL;
8450 /* If the next token is a `(', then we have a parenthesized
8451 type-id. */
8452 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8454 cp_token *token;
8455 const char *saved_message = parser->type_definition_forbidden_message;
8457 /* Consume the `('. */
8458 matching_parens parens;
8459 parens.consume_open (parser);
8461 /* Parse the type-id. */
8462 parser->type_definition_forbidden_message
8463 = G_("types may not be defined in a new-expression");
8465 type_id_in_expr_sentinel s (parser);
8466 type = cp_parser_type_id (parser);
8468 parser->type_definition_forbidden_message = saved_message;
8470 /* Look for the closing `)'. */
8471 parens.require_close (parser);
8472 token = cp_lexer_peek_token (parser->lexer);
8473 /* There should not be a direct-new-declarator in this production,
8474 but GCC used to allowed this, so we check and emit a sensible error
8475 message for this case. */
8476 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8478 error_at (token->location,
8479 "array bound forbidden after parenthesized type-id");
8480 inform (token->location,
8481 "try removing the parentheses around the type-id");
8482 cp_parser_direct_new_declarator (parser);
8485 /* Otherwise, there must be a new-type-id. */
8486 else
8487 type = cp_parser_new_type_id (parser, &nelts);
8489 /* If the next token is a `(' or '{', then we have a new-initializer. */
8490 cp_token *token = cp_lexer_peek_token (parser->lexer);
8491 if (token->type == CPP_OPEN_PAREN
8492 || token->type == CPP_OPEN_BRACE)
8493 initializer = cp_parser_new_initializer (parser);
8494 else
8495 initializer = NULL;
8497 /* A new-expression may not appear in an integral constant
8498 expression. */
8499 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8500 ret = error_mark_node;
8501 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8502 of a new-type-id or type-id of a new-expression, the new-expression shall
8503 contain a new-initializer of the form ( assignment-expression )".
8504 Additionally, consistently with the spirit of DR 1467, we want to accept
8505 'new auto { 2 }' too. */
8506 else if ((ret = type_uses_auto (type))
8507 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8508 && (vec_safe_length (initializer) != 1
8509 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8510 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8512 error_at (token->location,
8513 "initialization of new-expression for type %<auto%> "
8514 "requires exactly one element");
8515 ret = error_mark_node;
8517 else
8519 /* Construct a location e.g.:
8520 ptr = new int[100]
8521 ^~~~~~~~~~~~
8522 with caret == start at the start of the "new" token, and the end
8523 at the end of the final token we consumed. */
8524 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8525 location_t end_loc = get_finish (end_tok->location);
8526 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8528 /* Create a representation of the new-expression. */
8529 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8530 tf_warning_or_error);
8531 protected_set_expr_location (ret, combined_loc);
8534 if (placement != NULL)
8535 release_tree_vector (placement);
8536 if (initializer != NULL)
8537 release_tree_vector (initializer);
8539 return ret;
8542 /* Parse a new-placement.
8544 new-placement:
8545 ( expression-list )
8547 Returns the same representation as for an expression-list. */
8549 static vec<tree, va_gc> *
8550 cp_parser_new_placement (cp_parser* parser)
8552 vec<tree, va_gc> *expression_list;
8554 /* Parse the expression-list. */
8555 expression_list = (cp_parser_parenthesized_expression_list
8556 (parser, non_attr, /*cast_p=*/false,
8557 /*allow_expansion_p=*/true,
8558 /*non_constant_p=*/NULL));
8560 if (expression_list && expression_list->is_empty ())
8561 error ("expected expression-list or type-id");
8563 return expression_list;
8566 /* Parse a new-type-id.
8568 new-type-id:
8569 type-specifier-seq new-declarator [opt]
8571 Returns the TYPE allocated. If the new-type-id indicates an array
8572 type, *NELTS is set to the number of elements in the last array
8573 bound; the TYPE will not include the last array bound. */
8575 static tree
8576 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8578 cp_decl_specifier_seq type_specifier_seq;
8579 cp_declarator *new_declarator;
8580 cp_declarator *declarator;
8581 cp_declarator *outer_declarator;
8582 const char *saved_message;
8584 /* The type-specifier sequence must not contain type definitions.
8585 (It cannot contain declarations of new types either, but if they
8586 are not definitions we will catch that because they are not
8587 complete.) */
8588 saved_message = parser->type_definition_forbidden_message;
8589 parser->type_definition_forbidden_message
8590 = G_("types may not be defined in a new-type-id");
8591 /* Parse the type-specifier-seq. */
8592 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8593 /*is_trailing_return=*/false,
8594 &type_specifier_seq);
8595 /* Restore the old message. */
8596 parser->type_definition_forbidden_message = saved_message;
8598 if (type_specifier_seq.type == error_mark_node)
8599 return error_mark_node;
8601 /* Parse the new-declarator. */
8602 new_declarator = cp_parser_new_declarator_opt (parser);
8604 /* Determine the number of elements in the last array dimension, if
8605 any. */
8606 *nelts = NULL_TREE;
8607 /* Skip down to the last array dimension. */
8608 declarator = new_declarator;
8609 outer_declarator = NULL;
8610 while (declarator && (declarator->kind == cdk_pointer
8611 || declarator->kind == cdk_ptrmem))
8613 outer_declarator = declarator;
8614 declarator = declarator->declarator;
8616 while (declarator
8617 && declarator->kind == cdk_array
8618 && declarator->declarator
8619 && declarator->declarator->kind == cdk_array)
8621 outer_declarator = declarator;
8622 declarator = declarator->declarator;
8625 if (declarator && declarator->kind == cdk_array)
8627 *nelts = declarator->u.array.bounds;
8628 if (*nelts == error_mark_node)
8629 *nelts = integer_one_node;
8631 if (outer_declarator)
8632 outer_declarator->declarator = declarator->declarator;
8633 else
8634 new_declarator = NULL;
8637 return groktypename (&type_specifier_seq, new_declarator, false);
8640 /* Parse an (optional) new-declarator.
8642 new-declarator:
8643 ptr-operator new-declarator [opt]
8644 direct-new-declarator
8646 Returns the declarator. */
8648 static cp_declarator *
8649 cp_parser_new_declarator_opt (cp_parser* parser)
8651 enum tree_code code;
8652 tree type, std_attributes = NULL_TREE;
8653 cp_cv_quals cv_quals;
8655 /* We don't know if there's a ptr-operator next, or not. */
8656 cp_parser_parse_tentatively (parser);
8657 /* Look for a ptr-operator. */
8658 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8659 /* If that worked, look for more new-declarators. */
8660 if (cp_parser_parse_definitely (parser))
8662 cp_declarator *declarator;
8664 /* Parse another optional declarator. */
8665 declarator = cp_parser_new_declarator_opt (parser);
8667 declarator = cp_parser_make_indirect_declarator
8668 (code, type, cv_quals, declarator, std_attributes);
8670 return declarator;
8673 /* If the next token is a `[', there is a direct-new-declarator. */
8674 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8675 return cp_parser_direct_new_declarator (parser);
8677 return NULL;
8680 /* Parse a direct-new-declarator.
8682 direct-new-declarator:
8683 [ expression ]
8684 direct-new-declarator [constant-expression]
8688 static cp_declarator *
8689 cp_parser_direct_new_declarator (cp_parser* parser)
8691 cp_declarator *declarator = NULL;
8693 while (true)
8695 tree expression;
8696 cp_token *token;
8698 /* Look for the opening `['. */
8699 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8701 token = cp_lexer_peek_token (parser->lexer);
8702 expression = cp_parser_expression (parser);
8703 /* The standard requires that the expression have integral
8704 type. DR 74 adds enumeration types. We believe that the
8705 real intent is that these expressions be handled like the
8706 expression in a `switch' condition, which also allows
8707 classes with a single conversion to integral or
8708 enumeration type. */
8709 if (!processing_template_decl)
8711 expression
8712 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8713 expression,
8714 /*complain=*/true);
8715 if (!expression)
8717 error_at (token->location,
8718 "expression in new-declarator must have integral "
8719 "or enumeration type");
8720 expression = error_mark_node;
8724 /* Look for the closing `]'. */
8725 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8727 /* Add this bound to the declarator. */
8728 declarator = make_array_declarator (declarator, expression);
8730 /* If the next token is not a `[', then there are no more
8731 bounds. */
8732 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8733 break;
8736 return declarator;
8739 /* Parse a new-initializer.
8741 new-initializer:
8742 ( expression-list [opt] )
8743 braced-init-list
8745 Returns a representation of the expression-list. */
8747 static vec<tree, va_gc> *
8748 cp_parser_new_initializer (cp_parser* parser)
8750 vec<tree, va_gc> *expression_list;
8752 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8754 tree t;
8755 bool expr_non_constant_p;
8756 cp_lexer_set_source_position (parser->lexer);
8757 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8758 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8759 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8760 expression_list = make_tree_vector_single (t);
8762 else
8763 expression_list = (cp_parser_parenthesized_expression_list
8764 (parser, non_attr, /*cast_p=*/false,
8765 /*allow_expansion_p=*/true,
8766 /*non_constant_p=*/NULL));
8768 return expression_list;
8771 /* Parse a delete-expression.
8773 delete-expression:
8774 :: [opt] delete cast-expression
8775 :: [opt] delete [ ] cast-expression
8777 Returns a representation of the expression. */
8779 static tree
8780 cp_parser_delete_expression (cp_parser* parser)
8782 bool global_scope_p;
8783 bool array_p;
8784 tree expression;
8786 /* Look for the optional `::' operator. */
8787 global_scope_p
8788 = (cp_parser_global_scope_opt (parser,
8789 /*current_scope_valid_p=*/false)
8790 != NULL_TREE);
8791 /* Look for the `delete' keyword. */
8792 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8793 /* See if the array syntax is in use. */
8794 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8796 /* Consume the `[' token. */
8797 cp_lexer_consume_token (parser->lexer);
8798 /* Look for the `]' token. */
8799 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8800 /* Remember that this is the `[]' construct. */
8801 array_p = true;
8803 else
8804 array_p = false;
8806 /* Parse the cast-expression. */
8807 expression = cp_parser_simple_cast_expression (parser);
8809 /* A delete-expression may not appear in an integral constant
8810 expression. */
8811 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8812 return error_mark_node;
8814 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8815 tf_warning_or_error);
8818 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8819 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8820 0 otherwise. */
8822 static int
8823 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8825 cp_token *token = cp_lexer_peek_token (parser->lexer);
8826 switch (token->type)
8828 case CPP_COMMA:
8829 case CPP_SEMICOLON:
8830 case CPP_QUERY:
8831 case CPP_COLON:
8832 case CPP_CLOSE_SQUARE:
8833 case CPP_CLOSE_PAREN:
8834 case CPP_CLOSE_BRACE:
8835 case CPP_OPEN_BRACE:
8836 case CPP_DOT:
8837 case CPP_DOT_STAR:
8838 case CPP_DEREF:
8839 case CPP_DEREF_STAR:
8840 case CPP_DIV:
8841 case CPP_MOD:
8842 case CPP_LSHIFT:
8843 case CPP_RSHIFT:
8844 case CPP_LESS:
8845 case CPP_GREATER:
8846 case CPP_LESS_EQ:
8847 case CPP_GREATER_EQ:
8848 case CPP_EQ_EQ:
8849 case CPP_NOT_EQ:
8850 case CPP_EQ:
8851 case CPP_MULT_EQ:
8852 case CPP_DIV_EQ:
8853 case CPP_MOD_EQ:
8854 case CPP_PLUS_EQ:
8855 case CPP_MINUS_EQ:
8856 case CPP_RSHIFT_EQ:
8857 case CPP_LSHIFT_EQ:
8858 case CPP_AND_EQ:
8859 case CPP_XOR_EQ:
8860 case CPP_OR_EQ:
8861 case CPP_XOR:
8862 case CPP_OR:
8863 case CPP_OR_OR:
8864 case CPP_EOF:
8865 case CPP_ELLIPSIS:
8866 return 0;
8868 case CPP_OPEN_PAREN:
8869 /* In ((type ()) () the last () isn't a valid cast-expression,
8870 so the whole must be parsed as postfix-expression. */
8871 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8872 != CPP_CLOSE_PAREN;
8874 case CPP_OPEN_SQUARE:
8875 /* '[' may start a primary-expression in obj-c++ and in C++11,
8876 as a lambda-expression, eg, '(void)[]{}'. */
8877 if (cxx_dialect >= cxx11)
8878 return -1;
8879 return c_dialect_objc ();
8881 case CPP_PLUS_PLUS:
8882 case CPP_MINUS_MINUS:
8883 /* '++' and '--' may or may not start a cast-expression:
8885 struct T { void operator++(int); };
8886 void f() { (T())++; }
8890 int a;
8891 (int)++a; */
8892 return -1;
8894 default:
8895 return 1;
8899 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8900 in the order: const_cast, static_cast, reinterpret_cast.
8902 Don't suggest dynamic_cast.
8904 Return the first legal cast kind found, or NULL otherwise. */
8906 static const char *
8907 get_cast_suggestion (tree dst_type, tree orig_expr)
8909 tree trial;
8911 /* Reuse the parser logic by attempting to build the various kinds of
8912 cast, with "complain" disabled.
8913 Identify the first such cast that is valid. */
8915 /* Don't attempt to run such logic within template processing. */
8916 if (processing_template_decl)
8917 return NULL;
8919 /* First try const_cast. */
8920 trial = build_const_cast (dst_type, orig_expr, tf_none);
8921 if (trial != error_mark_node)
8922 return "const_cast";
8924 /* If that fails, try static_cast. */
8925 trial = build_static_cast (dst_type, orig_expr, tf_none);
8926 if (trial != error_mark_node)
8927 return "static_cast";
8929 /* Finally, try reinterpret_cast. */
8930 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8931 if (trial != error_mark_node)
8932 return "reinterpret_cast";
8934 /* No such cast possible. */
8935 return NULL;
8938 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8939 suggesting how to convert a C-style cast of the form:
8941 (DST_TYPE)ORIG_EXPR
8943 to a C++-style cast.
8945 The primary range of RICHLOC is asssumed to be that of the original
8946 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8947 of the parens in the C-style cast. */
8949 static void
8950 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8951 location_t close_paren_loc, tree orig_expr,
8952 tree dst_type)
8954 /* This function is non-trivial, so bail out now if the warning isn't
8955 going to be emitted. */
8956 if (!warn_old_style_cast)
8957 return;
8959 /* Try to find a legal C++ cast, trying them in order:
8960 const_cast, static_cast, reinterpret_cast. */
8961 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8962 if (!cast_suggestion)
8963 return;
8965 /* Replace the open paren with "CAST_SUGGESTION<". */
8966 pretty_printer pp;
8967 pp_printf (&pp, "%s<", cast_suggestion);
8968 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8970 /* Replace the close paren with "> (". */
8971 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8973 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8974 rich_loc->add_fixit_insert_after (")");
8978 /* Parse a cast-expression.
8980 cast-expression:
8981 unary-expression
8982 ( type-id ) cast-expression
8984 ADDRESS_P is true iff the unary-expression is appearing as the
8985 operand of the `&' operator. CAST_P is true if this expression is
8986 the target of a cast.
8988 Returns a representation of the expression. */
8990 static cp_expr
8991 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8992 bool decltype_p, cp_id_kind * pidk)
8994 /* If it's a `(', then we might be looking at a cast. */
8995 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8997 tree type = NULL_TREE;
8998 cp_expr expr (NULL_TREE);
8999 int cast_expression = 0;
9000 const char *saved_message;
9002 /* There's no way to know yet whether or not this is a cast.
9003 For example, `(int (3))' is a unary-expression, while `(int)
9004 3' is a cast. So, we resort to parsing tentatively. */
9005 cp_parser_parse_tentatively (parser);
9006 /* Types may not be defined in a cast. */
9007 saved_message = parser->type_definition_forbidden_message;
9008 parser->type_definition_forbidden_message
9009 = G_("types may not be defined in casts");
9010 /* Consume the `('. */
9011 matching_parens parens;
9012 cp_token *open_paren = parens.consume_open (parser);
9013 location_t open_paren_loc = open_paren->location;
9014 location_t close_paren_loc = UNKNOWN_LOCATION;
9016 /* A very tricky bit is that `(struct S) { 3 }' is a
9017 compound-literal (which we permit in C++ as an extension).
9018 But, that construct is not a cast-expression -- it is a
9019 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9020 is legal; if the compound-literal were a cast-expression,
9021 you'd need an extra set of parentheses.) But, if we parse
9022 the type-id, and it happens to be a class-specifier, then we
9023 will commit to the parse at that point, because we cannot
9024 undo the action that is done when creating a new class. So,
9025 then we cannot back up and do a postfix-expression.
9027 Another tricky case is the following (c++/29234):
9029 struct S { void operator () (); };
9031 void foo ()
9033 ( S()() );
9036 As a type-id we parse the parenthesized S()() as a function
9037 returning a function, groktypename complains and we cannot
9038 back up in this case either.
9040 Therefore, we scan ahead to the closing `)', and check to see
9041 if the tokens after the `)' can start a cast-expression. Otherwise
9042 we are dealing with an unary-expression, a postfix-expression
9043 or something else.
9045 Yet another tricky case, in C++11, is the following (c++/54891):
9047 (void)[]{};
9049 The issue is that usually, besides the case of lambda-expressions,
9050 the parenthesized type-id cannot be followed by '[', and, eg, we
9051 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9052 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9053 we don't commit, we try a cast-expression, then an unary-expression.
9055 Save tokens so that we can put them back. */
9056 cp_lexer_save_tokens (parser->lexer);
9058 /* We may be looking at a cast-expression. */
9059 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9060 /*consume_paren=*/true))
9061 cast_expression
9062 = cp_parser_tokens_start_cast_expression (parser);
9064 /* Roll back the tokens we skipped. */
9065 cp_lexer_rollback_tokens (parser->lexer);
9066 /* If we aren't looking at a cast-expression, simulate an error so
9067 that the call to cp_parser_error_occurred below returns true. */
9068 if (!cast_expression)
9069 cp_parser_simulate_error (parser);
9070 else
9072 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9073 parser->in_type_id_in_expr_p = true;
9074 /* Look for the type-id. */
9075 type = cp_parser_type_id (parser);
9076 /* Look for the closing `)'. */
9077 cp_token *close_paren = parens.require_close (parser);
9078 if (close_paren)
9079 close_paren_loc = close_paren->location;
9080 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9083 /* Restore the saved message. */
9084 parser->type_definition_forbidden_message = saved_message;
9086 /* At this point this can only be either a cast or a
9087 parenthesized ctor such as `(T ())' that looks like a cast to
9088 function returning T. */
9089 if (!cp_parser_error_occurred (parser))
9091 /* Only commit if the cast-expression doesn't start with
9092 '++', '--', or '[' in C++11. */
9093 if (cast_expression > 0)
9094 cp_parser_commit_to_topmost_tentative_parse (parser);
9096 expr = cp_parser_cast_expression (parser,
9097 /*address_p=*/false,
9098 /*cast_p=*/true,
9099 /*decltype_p=*/false,
9100 pidk);
9102 if (cp_parser_parse_definitely (parser))
9104 /* Warn about old-style casts, if so requested. */
9105 if (warn_old_style_cast
9106 && !in_system_header_at (input_location)
9107 && !VOID_TYPE_P (type)
9108 && current_lang_name != lang_name_c)
9110 gcc_rich_location rich_loc (input_location);
9111 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9112 expr, type);
9113 warning_at (&rich_loc, OPT_Wold_style_cast,
9114 "use of old-style cast to %q#T", type);
9117 /* Only type conversions to integral or enumeration types
9118 can be used in constant-expressions. */
9119 if (!cast_valid_in_integral_constant_expression_p (type)
9120 && cp_parser_non_integral_constant_expression (parser,
9121 NIC_CAST))
9122 return error_mark_node;
9124 /* Perform the cast. */
9125 /* Make a location:
9126 (TYPE) EXPR
9127 ^~~~~~~~~~~
9128 with start==caret at the open paren, extending to the
9129 end of "expr". */
9130 location_t cast_loc = make_location (open_paren_loc,
9131 open_paren_loc,
9132 expr.get_finish ());
9133 expr = build_c_cast (cast_loc, type, expr);
9134 return expr;
9137 else
9138 cp_parser_abort_tentative_parse (parser);
9141 /* If we get here, then it's not a cast, so it must be a
9142 unary-expression. */
9143 return cp_parser_unary_expression (parser, pidk, address_p,
9144 cast_p, decltype_p);
9147 /* Parse a binary expression of the general form:
9149 pm-expression:
9150 cast-expression
9151 pm-expression .* cast-expression
9152 pm-expression ->* cast-expression
9154 multiplicative-expression:
9155 pm-expression
9156 multiplicative-expression * pm-expression
9157 multiplicative-expression / pm-expression
9158 multiplicative-expression % pm-expression
9160 additive-expression:
9161 multiplicative-expression
9162 additive-expression + multiplicative-expression
9163 additive-expression - multiplicative-expression
9165 shift-expression:
9166 additive-expression
9167 shift-expression << additive-expression
9168 shift-expression >> additive-expression
9170 relational-expression:
9171 shift-expression
9172 relational-expression < shift-expression
9173 relational-expression > shift-expression
9174 relational-expression <= shift-expression
9175 relational-expression >= shift-expression
9177 GNU Extension:
9179 relational-expression:
9180 relational-expression <? shift-expression
9181 relational-expression >? shift-expression
9183 equality-expression:
9184 relational-expression
9185 equality-expression == relational-expression
9186 equality-expression != relational-expression
9188 and-expression:
9189 equality-expression
9190 and-expression & equality-expression
9192 exclusive-or-expression:
9193 and-expression
9194 exclusive-or-expression ^ and-expression
9196 inclusive-or-expression:
9197 exclusive-or-expression
9198 inclusive-or-expression | exclusive-or-expression
9200 logical-and-expression:
9201 inclusive-or-expression
9202 logical-and-expression && inclusive-or-expression
9204 logical-or-expression:
9205 logical-and-expression
9206 logical-or-expression || logical-and-expression
9208 All these are implemented with a single function like:
9210 binary-expression:
9211 simple-cast-expression
9212 binary-expression <token> binary-expression
9214 CAST_P is true if this expression is the target of a cast.
9216 The binops_by_token map is used to get the tree codes for each <token> type.
9217 binary-expressions are associated according to a precedence table. */
9219 #define TOKEN_PRECEDENCE(token) \
9220 (((token->type == CPP_GREATER \
9221 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9222 && !parser->greater_than_is_operator_p) \
9223 ? PREC_NOT_OPERATOR \
9224 : binops_by_token[token->type].prec)
9226 static cp_expr
9227 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9228 bool no_toplevel_fold_p,
9229 bool decltype_p,
9230 enum cp_parser_prec prec,
9231 cp_id_kind * pidk)
9233 cp_parser_expression_stack stack;
9234 cp_parser_expression_stack_entry *sp = &stack[0];
9235 cp_parser_expression_stack_entry current;
9236 cp_expr rhs;
9237 cp_token *token;
9238 enum tree_code rhs_type;
9239 enum cp_parser_prec new_prec, lookahead_prec;
9240 tree overload;
9242 /* Parse the first expression. */
9243 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9244 ? TRUTH_NOT_EXPR : ERROR_MARK);
9245 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9246 cast_p, decltype_p, pidk);
9247 current.prec = prec;
9249 if (cp_parser_error_occurred (parser))
9250 return error_mark_node;
9252 for (;;)
9254 /* Get an operator token. */
9255 token = cp_lexer_peek_token (parser->lexer);
9257 if (warn_cxx11_compat
9258 && token->type == CPP_RSHIFT
9259 && !parser->greater_than_is_operator_p)
9261 if (warning_at (token->location, OPT_Wc__11_compat,
9262 "%<>>%> operator is treated"
9263 " as two right angle brackets in C++11"))
9264 inform (token->location,
9265 "suggest parentheses around %<>>%> expression");
9268 new_prec = TOKEN_PRECEDENCE (token);
9269 if (new_prec != PREC_NOT_OPERATOR
9270 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9271 /* This is a fold-expression; handle it later. */
9272 new_prec = PREC_NOT_OPERATOR;
9274 /* Popping an entry off the stack means we completed a subexpression:
9275 - either we found a token which is not an operator (`>' where it is not
9276 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9277 will happen repeatedly;
9278 - or, we found an operator which has lower priority. This is the case
9279 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9280 parsing `3 * 4'. */
9281 if (new_prec <= current.prec)
9283 if (sp == stack)
9284 break;
9285 else
9286 goto pop;
9289 get_rhs:
9290 current.tree_type = binops_by_token[token->type].tree_type;
9291 current.loc = token->location;
9293 /* We used the operator token. */
9294 cp_lexer_consume_token (parser->lexer);
9296 /* For "false && x" or "true || x", x will never be executed;
9297 disable warnings while evaluating it. */
9298 if (current.tree_type == TRUTH_ANDIF_EXPR)
9299 c_inhibit_evaluation_warnings +=
9300 cp_fully_fold (current.lhs) == truthvalue_false_node;
9301 else if (current.tree_type == TRUTH_ORIF_EXPR)
9302 c_inhibit_evaluation_warnings +=
9303 cp_fully_fold (current.lhs) == truthvalue_true_node;
9305 /* Extract another operand. It may be the RHS of this expression
9306 or the LHS of a new, higher priority expression. */
9307 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9308 ? TRUTH_NOT_EXPR : ERROR_MARK);
9309 rhs = cp_parser_simple_cast_expression (parser);
9311 /* Get another operator token. Look up its precedence to avoid
9312 building a useless (immediately popped) stack entry for common
9313 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9314 token = cp_lexer_peek_token (parser->lexer);
9315 lookahead_prec = TOKEN_PRECEDENCE (token);
9316 if (lookahead_prec != PREC_NOT_OPERATOR
9317 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9318 lookahead_prec = PREC_NOT_OPERATOR;
9319 if (lookahead_prec > new_prec)
9321 /* ... and prepare to parse the RHS of the new, higher priority
9322 expression. Since precedence levels on the stack are
9323 monotonically increasing, we do not have to care about
9324 stack overflows. */
9325 *sp = current;
9326 ++sp;
9327 current.lhs = rhs;
9328 current.lhs_type = rhs_type;
9329 current.prec = new_prec;
9330 new_prec = lookahead_prec;
9331 goto get_rhs;
9333 pop:
9334 lookahead_prec = new_prec;
9335 /* If the stack is not empty, we have parsed into LHS the right side
9336 (`4' in the example above) of an expression we had suspended.
9337 We can use the information on the stack to recover the LHS (`3')
9338 from the stack together with the tree code (`MULT_EXPR'), and
9339 the precedence of the higher level subexpression
9340 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9341 which will be used to actually build the additive expression. */
9342 rhs = current.lhs;
9343 rhs_type = current.lhs_type;
9344 --sp;
9345 current = *sp;
9348 /* Undo the disabling of warnings done above. */
9349 if (current.tree_type == TRUTH_ANDIF_EXPR)
9350 c_inhibit_evaluation_warnings -=
9351 cp_fully_fold (current.lhs) == truthvalue_false_node;
9352 else if (current.tree_type == TRUTH_ORIF_EXPR)
9353 c_inhibit_evaluation_warnings -=
9354 cp_fully_fold (current.lhs) == truthvalue_true_node;
9356 if (warn_logical_not_paren
9357 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9358 && current.lhs_type == TRUTH_NOT_EXPR
9359 /* Avoid warning for !!x == y. */
9360 && (TREE_CODE (current.lhs) != NE_EXPR
9361 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9362 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9363 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9364 /* Avoid warning for !b == y where b is boolean. */
9365 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9366 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9367 != BOOLEAN_TYPE))))
9368 /* Avoid warning for !!b == y where b is boolean. */
9369 && (!DECL_P (current.lhs)
9370 || TREE_TYPE (current.lhs) == NULL_TREE
9371 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9372 warn_logical_not_parentheses (current.loc, current.tree_type,
9373 current.lhs, maybe_constant_value (rhs));
9375 overload = NULL;
9377 location_t combined_loc = make_location (current.loc,
9378 current.lhs.get_start (),
9379 rhs.get_finish ());
9381 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9382 ERROR_MARK for everything that is not a binary expression.
9383 This makes warn_about_parentheses miss some warnings that
9384 involve unary operators. For unary expressions we should
9385 pass the correct tree_code unless the unary expression was
9386 surrounded by parentheses.
9388 if (no_toplevel_fold_p
9389 && lookahead_prec <= current.prec
9390 && sp == stack)
9392 if (current.lhs == error_mark_node || rhs == error_mark_node)
9393 current.lhs = error_mark_node;
9394 else
9396 current.lhs
9397 = build_min (current.tree_type,
9398 TREE_CODE_CLASS (current.tree_type)
9399 == tcc_comparison
9400 ? boolean_type_node : TREE_TYPE (current.lhs),
9401 current.lhs.get_value (), rhs.get_value ());
9402 SET_EXPR_LOCATION (current.lhs, combined_loc);
9405 else
9407 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9408 current.lhs, current.lhs_type,
9409 rhs, rhs_type, &overload,
9410 complain_flags (decltype_p));
9411 /* TODO: build_x_binary_op doesn't always honor the location. */
9412 current.lhs.set_location (combined_loc);
9414 current.lhs_type = current.tree_type;
9416 /* If the binary operator required the use of an overloaded operator,
9417 then this expression cannot be an integral constant-expression.
9418 An overloaded operator can be used even if both operands are
9419 otherwise permissible in an integral constant-expression if at
9420 least one of the operands is of enumeration type. */
9422 if (overload
9423 && cp_parser_non_integral_constant_expression (parser,
9424 NIC_OVERLOADED))
9425 return error_mark_node;
9428 return current.lhs;
9431 static cp_expr
9432 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9433 bool no_toplevel_fold_p,
9434 enum cp_parser_prec prec,
9435 cp_id_kind * pidk)
9437 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9438 /*decltype*/false, prec, pidk);
9441 /* Parse the `? expression : assignment-expression' part of a
9442 conditional-expression. The LOGICAL_OR_EXPR is the
9443 logical-or-expression that started the conditional-expression.
9444 Returns a representation of the entire conditional-expression.
9446 This routine is used by cp_parser_assignment_expression.
9448 ? expression : assignment-expression
9450 GNU Extensions:
9452 ? : assignment-expression */
9454 static tree
9455 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9457 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9458 cp_expr assignment_expr;
9459 struct cp_token *token;
9460 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9462 /* Consume the `?' token. */
9463 cp_lexer_consume_token (parser->lexer);
9464 token = cp_lexer_peek_token (parser->lexer);
9465 if (cp_parser_allow_gnu_extensions_p (parser)
9466 && token->type == CPP_COLON)
9468 pedwarn (token->location, OPT_Wpedantic,
9469 "ISO C++ does not allow ?: with omitted middle operand");
9470 /* Implicit true clause. */
9471 expr = NULL_TREE;
9472 c_inhibit_evaluation_warnings +=
9473 folded_logical_or_expr == truthvalue_true_node;
9474 warn_for_omitted_condop (token->location, logical_or_expr);
9476 else
9478 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9479 parser->colon_corrects_to_scope_p = false;
9480 /* Parse the expression. */
9481 c_inhibit_evaluation_warnings +=
9482 folded_logical_or_expr == truthvalue_false_node;
9483 expr = cp_parser_expression (parser);
9484 c_inhibit_evaluation_warnings +=
9485 ((folded_logical_or_expr == truthvalue_true_node)
9486 - (folded_logical_or_expr == truthvalue_false_node));
9487 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9490 /* The next token should be a `:'. */
9491 cp_parser_require (parser, CPP_COLON, RT_COLON);
9492 /* Parse the assignment-expression. */
9493 assignment_expr = cp_parser_assignment_expression (parser);
9494 c_inhibit_evaluation_warnings -=
9495 folded_logical_or_expr == truthvalue_true_node;
9497 /* Make a location:
9498 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9499 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9500 with the caret at the "?", ranging from the start of
9501 the logical_or_expr to the end of the assignment_expr. */
9502 loc = make_location (loc,
9503 logical_or_expr.get_start (),
9504 assignment_expr.get_finish ());
9506 /* Build the conditional-expression. */
9507 return build_x_conditional_expr (loc, logical_or_expr,
9508 expr,
9509 assignment_expr,
9510 tf_warning_or_error);
9513 /* Parse an assignment-expression.
9515 assignment-expression:
9516 conditional-expression
9517 logical-or-expression assignment-operator assignment_expression
9518 throw-expression
9520 CAST_P is true if this expression is the target of a cast.
9521 DECLTYPE_P is true if this expression is the operand of decltype.
9523 Returns a representation for the expression. */
9525 static cp_expr
9526 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9527 bool cast_p, bool decltype_p)
9529 cp_expr expr;
9531 /* If the next token is the `throw' keyword, then we're looking at
9532 a throw-expression. */
9533 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9534 expr = cp_parser_throw_expression (parser);
9535 /* Otherwise, it must be that we are looking at a
9536 logical-or-expression. */
9537 else
9539 /* Parse the binary expressions (logical-or-expression). */
9540 expr = cp_parser_binary_expression (parser, cast_p, false,
9541 decltype_p,
9542 PREC_NOT_OPERATOR, pidk);
9543 /* If the next token is a `?' then we're actually looking at a
9544 conditional-expression. */
9545 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9546 return cp_parser_question_colon_clause (parser, expr);
9547 else
9549 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9551 /* If it's an assignment-operator, we're using the second
9552 production. */
9553 enum tree_code assignment_operator
9554 = cp_parser_assignment_operator_opt (parser);
9555 if (assignment_operator != ERROR_MARK)
9557 bool non_constant_p;
9559 /* Parse the right-hand side of the assignment. */
9560 cp_expr rhs = cp_parser_initializer_clause (parser,
9561 &non_constant_p);
9563 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9564 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9566 /* An assignment may not appear in a
9567 constant-expression. */
9568 if (cp_parser_non_integral_constant_expression (parser,
9569 NIC_ASSIGNMENT))
9570 return error_mark_node;
9571 /* Build the assignment expression. Its default
9572 location:
9573 LHS = RHS
9574 ~~~~^~~~~
9575 is the location of the '=' token as the
9576 caret, ranging from the start of the lhs to the
9577 end of the rhs. */
9578 loc = make_location (loc,
9579 expr.get_start (),
9580 rhs.get_finish ());
9581 expr = build_x_modify_expr (loc, expr,
9582 assignment_operator,
9583 rhs,
9584 complain_flags (decltype_p));
9585 /* TODO: build_x_modify_expr doesn't honor the location,
9586 so we must set it here. */
9587 expr.set_location (loc);
9592 return expr;
9595 /* Parse an (optional) assignment-operator.
9597 assignment-operator: one of
9598 = *= /= %= += -= >>= <<= &= ^= |=
9600 GNU Extension:
9602 assignment-operator: one of
9603 <?= >?=
9605 If the next token is an assignment operator, the corresponding tree
9606 code is returned, and the token is consumed. For example, for
9607 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9608 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9609 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9610 operator, ERROR_MARK is returned. */
9612 static enum tree_code
9613 cp_parser_assignment_operator_opt (cp_parser* parser)
9615 enum tree_code op;
9616 cp_token *token;
9618 /* Peek at the next token. */
9619 token = cp_lexer_peek_token (parser->lexer);
9621 switch (token->type)
9623 case CPP_EQ:
9624 op = NOP_EXPR;
9625 break;
9627 case CPP_MULT_EQ:
9628 op = MULT_EXPR;
9629 break;
9631 case CPP_DIV_EQ:
9632 op = TRUNC_DIV_EXPR;
9633 break;
9635 case CPP_MOD_EQ:
9636 op = TRUNC_MOD_EXPR;
9637 break;
9639 case CPP_PLUS_EQ:
9640 op = PLUS_EXPR;
9641 break;
9643 case CPP_MINUS_EQ:
9644 op = MINUS_EXPR;
9645 break;
9647 case CPP_RSHIFT_EQ:
9648 op = RSHIFT_EXPR;
9649 break;
9651 case CPP_LSHIFT_EQ:
9652 op = LSHIFT_EXPR;
9653 break;
9655 case CPP_AND_EQ:
9656 op = BIT_AND_EXPR;
9657 break;
9659 case CPP_XOR_EQ:
9660 op = BIT_XOR_EXPR;
9661 break;
9663 case CPP_OR_EQ:
9664 op = BIT_IOR_EXPR;
9665 break;
9667 default:
9668 /* Nothing else is an assignment operator. */
9669 op = ERROR_MARK;
9672 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9673 if (op != ERROR_MARK
9674 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9675 op = ERROR_MARK;
9677 /* If it was an assignment operator, consume it. */
9678 if (op != ERROR_MARK)
9679 cp_lexer_consume_token (parser->lexer);
9681 return op;
9684 /* Parse an expression.
9686 expression:
9687 assignment-expression
9688 expression , assignment-expression
9690 CAST_P is true if this expression is the target of a cast.
9691 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9692 except possibly parenthesized or on the RHS of a comma (N3276).
9694 Returns a representation of the expression. */
9696 static cp_expr
9697 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9698 bool cast_p, bool decltype_p)
9700 cp_expr expression = NULL_TREE;
9701 location_t loc = UNKNOWN_LOCATION;
9703 while (true)
9705 cp_expr assignment_expression;
9707 /* Parse the next assignment-expression. */
9708 assignment_expression
9709 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9711 /* We don't create a temporary for a call that is the immediate operand
9712 of decltype or on the RHS of a comma. But when we see a comma, we
9713 need to create a temporary for a call on the LHS. */
9714 if (decltype_p && !processing_template_decl
9715 && TREE_CODE (assignment_expression) == CALL_EXPR
9716 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9717 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9718 assignment_expression
9719 = build_cplus_new (TREE_TYPE (assignment_expression),
9720 assignment_expression, tf_warning_or_error);
9722 /* If this is the first assignment-expression, we can just
9723 save it away. */
9724 if (!expression)
9725 expression = assignment_expression;
9726 else
9728 /* Create a location with caret at the comma, ranging
9729 from the start of the LHS to the end of the RHS. */
9730 loc = make_location (loc,
9731 expression.get_start (),
9732 assignment_expression.get_finish ());
9733 expression = build_x_compound_expr (loc, expression,
9734 assignment_expression,
9735 complain_flags (decltype_p));
9736 expression.set_location (loc);
9738 /* If the next token is not a comma, or we're in a fold-expression, then
9739 we are done with the expression. */
9740 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9741 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9742 break;
9743 /* Consume the `,'. */
9744 loc = cp_lexer_peek_token (parser->lexer)->location;
9745 cp_lexer_consume_token (parser->lexer);
9746 /* A comma operator cannot appear in a constant-expression. */
9747 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9748 expression = error_mark_node;
9751 return expression;
9754 /* Parse a constant-expression.
9756 constant-expression:
9757 conditional-expression
9759 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9760 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9761 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9762 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9763 only parse a conditional-expression, otherwise parse an
9764 assignment-expression. See below for rationale. */
9766 static cp_expr
9767 cp_parser_constant_expression (cp_parser* parser,
9768 bool allow_non_constant_p,
9769 bool *non_constant_p,
9770 bool strict_p)
9772 bool saved_integral_constant_expression_p;
9773 bool saved_allow_non_integral_constant_expression_p;
9774 bool saved_non_integral_constant_expression_p;
9775 cp_expr expression;
9777 /* It might seem that we could simply parse the
9778 conditional-expression, and then check to see if it were
9779 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9780 one that the compiler can figure out is constant, possibly after
9781 doing some simplifications or optimizations. The standard has a
9782 precise definition of constant-expression, and we must honor
9783 that, even though it is somewhat more restrictive.
9785 For example:
9787 int i[(2, 3)];
9789 is not a legal declaration, because `(2, 3)' is not a
9790 constant-expression. The `,' operator is forbidden in a
9791 constant-expression. However, GCC's constant-folding machinery
9792 will fold this operation to an INTEGER_CST for `3'. */
9794 /* Save the old settings. */
9795 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9796 saved_allow_non_integral_constant_expression_p
9797 = parser->allow_non_integral_constant_expression_p;
9798 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9799 /* We are now parsing a constant-expression. */
9800 parser->integral_constant_expression_p = true;
9801 parser->allow_non_integral_constant_expression_p
9802 = (allow_non_constant_p || cxx_dialect >= cxx11);
9803 parser->non_integral_constant_expression_p = false;
9804 /* Although the grammar says "conditional-expression", when not STRICT_P,
9805 we parse an "assignment-expression", which also permits
9806 "throw-expression" and the use of assignment operators. In the case
9807 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9808 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9809 actually essential that we look for an assignment-expression.
9810 For example, cp_parser_initializer_clauses uses this function to
9811 determine whether a particular assignment-expression is in fact
9812 constant. */
9813 if (strict_p)
9815 /* Parse the binary expressions (logical-or-expression). */
9816 expression = cp_parser_binary_expression (parser, false, false, false,
9817 PREC_NOT_OPERATOR, NULL);
9818 /* If the next token is a `?' then we're actually looking at
9819 a conditional-expression; otherwise we're done. */
9820 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9821 expression = cp_parser_question_colon_clause (parser, expression);
9823 else
9824 expression = cp_parser_assignment_expression (parser);
9825 /* Restore the old settings. */
9826 parser->integral_constant_expression_p
9827 = saved_integral_constant_expression_p;
9828 parser->allow_non_integral_constant_expression_p
9829 = saved_allow_non_integral_constant_expression_p;
9830 if (cxx_dialect >= cxx11)
9832 /* Require an rvalue constant expression here; that's what our
9833 callers expect. Reference constant expressions are handled
9834 separately in e.g. cp_parser_template_argument. */
9835 tree decay = expression;
9836 if (TREE_TYPE (expression)
9837 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9838 decay = build_address (expression);
9839 bool is_const = potential_rvalue_constant_expression (decay);
9840 parser->non_integral_constant_expression_p = !is_const;
9841 if (!is_const && !allow_non_constant_p)
9842 require_potential_rvalue_constant_expression (decay);
9844 if (allow_non_constant_p)
9845 *non_constant_p = parser->non_integral_constant_expression_p;
9846 parser->non_integral_constant_expression_p
9847 = saved_non_integral_constant_expression_p;
9849 return expression;
9852 /* Parse __builtin_offsetof.
9854 offsetof-expression:
9855 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9857 offsetof-member-designator:
9858 id-expression
9859 | offsetof-member-designator "." id-expression
9860 | offsetof-member-designator "[" expression "]"
9861 | offsetof-member-designator "->" id-expression */
9863 static cp_expr
9864 cp_parser_builtin_offsetof (cp_parser *parser)
9866 int save_ice_p, save_non_ice_p;
9867 tree type;
9868 cp_expr expr;
9869 cp_id_kind dummy;
9870 cp_token *token;
9871 location_t finish_loc;
9873 /* We're about to accept non-integral-constant things, but will
9874 definitely yield an integral constant expression. Save and
9875 restore these values around our local parsing. */
9876 save_ice_p = parser->integral_constant_expression_p;
9877 save_non_ice_p = parser->non_integral_constant_expression_p;
9879 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9881 /* Consume the "__builtin_offsetof" token. */
9882 cp_lexer_consume_token (parser->lexer);
9883 /* Consume the opening `('. */
9884 matching_parens parens;
9885 parens.require_open (parser);
9886 /* Parse the type-id. */
9887 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9889 const char *saved_message = parser->type_definition_forbidden_message;
9890 parser->type_definition_forbidden_message
9891 = G_("types may not be defined within __builtin_offsetof");
9892 type = cp_parser_type_id (parser);
9893 parser->type_definition_forbidden_message = saved_message;
9895 /* Look for the `,'. */
9896 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9897 token = cp_lexer_peek_token (parser->lexer);
9899 /* Build the (type *)null that begins the traditional offsetof macro. */
9900 tree object_ptr
9901 = build_static_cast (build_pointer_type (type), null_pointer_node,
9902 tf_warning_or_error);
9904 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9905 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9906 true, &dummy, token->location);
9907 while (true)
9909 token = cp_lexer_peek_token (parser->lexer);
9910 switch (token->type)
9912 case CPP_OPEN_SQUARE:
9913 /* offsetof-member-designator "[" expression "]" */
9914 expr = cp_parser_postfix_open_square_expression (parser, expr,
9915 true, false);
9916 break;
9918 case CPP_DEREF:
9919 /* offsetof-member-designator "->" identifier */
9920 expr = grok_array_decl (token->location, expr,
9921 integer_zero_node, false);
9922 /* FALLTHRU */
9924 case CPP_DOT:
9925 /* offsetof-member-designator "." identifier */
9926 cp_lexer_consume_token (parser->lexer);
9927 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9928 expr, true, &dummy,
9929 token->location);
9930 break;
9932 case CPP_CLOSE_PAREN:
9933 /* Consume the ")" token. */
9934 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9935 cp_lexer_consume_token (parser->lexer);
9936 goto success;
9938 default:
9939 /* Error. We know the following require will fail, but
9940 that gives the proper error message. */
9941 parens.require_close (parser);
9942 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9943 expr = error_mark_node;
9944 goto failure;
9948 success:
9949 /* Make a location of the form:
9950 __builtin_offsetof (struct s, f)
9951 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9952 with caret at the type-id, ranging from the start of the
9953 "_builtin_offsetof" token to the close paren. */
9954 loc = make_location (loc, start_loc, finish_loc);
9955 /* The result will be an INTEGER_CST, so we need to explicitly
9956 preserve the location. */
9957 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9959 failure:
9960 parser->integral_constant_expression_p = save_ice_p;
9961 parser->non_integral_constant_expression_p = save_non_ice_p;
9963 expr = expr.maybe_add_location_wrapper ();
9964 return expr;
9967 /* Parse a trait expression.
9969 Returns a representation of the expression, the underlying type
9970 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9972 static cp_expr
9973 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9975 cp_trait_kind kind;
9976 tree type1, type2 = NULL_TREE;
9977 bool binary = false;
9978 bool variadic = false;
9980 switch (keyword)
9982 case RID_HAS_NOTHROW_ASSIGN:
9983 kind = CPTK_HAS_NOTHROW_ASSIGN;
9984 break;
9985 case RID_HAS_NOTHROW_CONSTRUCTOR:
9986 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9987 break;
9988 case RID_HAS_NOTHROW_COPY:
9989 kind = CPTK_HAS_NOTHROW_COPY;
9990 break;
9991 case RID_HAS_TRIVIAL_ASSIGN:
9992 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9993 break;
9994 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9995 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9996 break;
9997 case RID_HAS_TRIVIAL_COPY:
9998 kind = CPTK_HAS_TRIVIAL_COPY;
9999 break;
10000 case RID_HAS_TRIVIAL_DESTRUCTOR:
10001 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10002 break;
10003 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10004 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10005 break;
10006 case RID_HAS_VIRTUAL_DESTRUCTOR:
10007 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10008 break;
10009 case RID_IS_ABSTRACT:
10010 kind = CPTK_IS_ABSTRACT;
10011 break;
10012 case RID_IS_AGGREGATE:
10013 kind = CPTK_IS_AGGREGATE;
10014 break;
10015 case RID_IS_BASE_OF:
10016 kind = CPTK_IS_BASE_OF;
10017 binary = true;
10018 break;
10019 case RID_IS_CLASS:
10020 kind = CPTK_IS_CLASS;
10021 break;
10022 case RID_IS_EMPTY:
10023 kind = CPTK_IS_EMPTY;
10024 break;
10025 case RID_IS_ENUM:
10026 kind = CPTK_IS_ENUM;
10027 break;
10028 case RID_IS_FINAL:
10029 kind = CPTK_IS_FINAL;
10030 break;
10031 case RID_IS_LITERAL_TYPE:
10032 kind = CPTK_IS_LITERAL_TYPE;
10033 break;
10034 case RID_IS_POD:
10035 kind = CPTK_IS_POD;
10036 break;
10037 case RID_IS_POLYMORPHIC:
10038 kind = CPTK_IS_POLYMORPHIC;
10039 break;
10040 case RID_IS_SAME_AS:
10041 kind = CPTK_IS_SAME_AS;
10042 binary = true;
10043 break;
10044 case RID_IS_STD_LAYOUT:
10045 kind = CPTK_IS_STD_LAYOUT;
10046 break;
10047 case RID_IS_TRIVIAL:
10048 kind = CPTK_IS_TRIVIAL;
10049 break;
10050 case RID_IS_TRIVIALLY_ASSIGNABLE:
10051 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10052 binary = true;
10053 break;
10054 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10055 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10056 variadic = true;
10057 break;
10058 case RID_IS_TRIVIALLY_COPYABLE:
10059 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10060 break;
10061 case RID_IS_UNION:
10062 kind = CPTK_IS_UNION;
10063 break;
10064 case RID_UNDERLYING_TYPE:
10065 kind = CPTK_UNDERLYING_TYPE;
10066 break;
10067 case RID_BASES:
10068 kind = CPTK_BASES;
10069 break;
10070 case RID_DIRECT_BASES:
10071 kind = CPTK_DIRECT_BASES;
10072 break;
10073 case RID_IS_ASSIGNABLE:
10074 kind = CPTK_IS_ASSIGNABLE;
10075 binary = true;
10076 break;
10077 case RID_IS_CONSTRUCTIBLE:
10078 kind = CPTK_IS_CONSTRUCTIBLE;
10079 variadic = true;
10080 break;
10081 default:
10082 gcc_unreachable ();
10085 /* Get location of initial token. */
10086 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10088 /* Consume the token. */
10089 cp_lexer_consume_token (parser->lexer);
10091 matching_parens parens;
10092 parens.require_open (parser);
10095 type_id_in_expr_sentinel s (parser);
10096 type1 = cp_parser_type_id (parser);
10099 if (type1 == error_mark_node)
10100 return error_mark_node;
10102 if (binary)
10104 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10107 type_id_in_expr_sentinel s (parser);
10108 type2 = cp_parser_type_id (parser);
10111 if (type2 == error_mark_node)
10112 return error_mark_node;
10114 else if (variadic)
10116 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10118 cp_lexer_consume_token (parser->lexer);
10119 tree elt = cp_parser_type_id (parser);
10120 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10122 cp_lexer_consume_token (parser->lexer);
10123 elt = make_pack_expansion (elt);
10125 if (elt == error_mark_node)
10126 return error_mark_node;
10127 type2 = tree_cons (NULL_TREE, elt, type2);
10131 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10132 parens.require_close (parser);
10134 /* Construct a location of the form:
10135 __is_trivially_copyable(_Tp)
10136 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10137 with start == caret, finishing at the close-paren. */
10138 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10140 /* Complete the trait expression, which may mean either processing
10141 the trait expr now or saving it for template instantiation. */
10142 switch (kind)
10144 case CPTK_UNDERLYING_TYPE:
10145 return cp_expr (finish_underlying_type (type1), trait_loc);
10146 case CPTK_BASES:
10147 return cp_expr (finish_bases (type1, false), trait_loc);
10148 case CPTK_DIRECT_BASES:
10149 return cp_expr (finish_bases (type1, true), trait_loc);
10150 default:
10151 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10155 /* Parse a lambda expression.
10157 lambda-expression:
10158 lambda-introducer lambda-declarator [opt] compound-statement
10160 Returns a representation of the expression. */
10162 static cp_expr
10163 cp_parser_lambda_expression (cp_parser* parser)
10165 tree lambda_expr = build_lambda_expr ();
10166 tree type;
10167 bool ok = true;
10168 cp_token *token = cp_lexer_peek_token (parser->lexer);
10169 cp_token_position start = 0;
10171 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10173 if (cxx_dialect >= cxx2a)
10174 /* C++20 allows lambdas in unevaluated context. */;
10175 else if (cp_unevaluated_operand)
10177 if (!token->error_reported)
10179 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10180 "lambda-expression in unevaluated context"
10181 " only available with -std=c++2a or -std=gnu++2a");
10182 token->error_reported = true;
10184 ok = false;
10186 else if (parser->in_template_argument_list_p)
10188 if (!token->error_reported)
10190 error_at (token->location, "lambda-expression in template-argument"
10191 " only available with -std=c++2a or -std=gnu++2a");
10192 token->error_reported = true;
10194 ok = false;
10197 /* We may be in the middle of deferred access check. Disable
10198 it now. */
10199 push_deferring_access_checks (dk_no_deferred);
10201 cp_parser_lambda_introducer (parser, lambda_expr);
10202 if (cp_parser_error_occurred (parser))
10203 return error_mark_node;
10205 type = begin_lambda_type (lambda_expr);
10206 if (type == error_mark_node)
10207 return error_mark_node;
10209 record_lambda_scope (lambda_expr);
10211 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10212 determine_visibility (TYPE_NAME (type));
10214 /* Now that we've started the type, add the capture fields for any
10215 explicit captures. */
10216 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10219 /* Inside the class, surrounding template-parameter-lists do not apply. */
10220 unsigned int saved_num_template_parameter_lists
10221 = parser->num_template_parameter_lists;
10222 unsigned char in_statement = parser->in_statement;
10223 bool in_switch_statement_p = parser->in_switch_statement_p;
10224 bool fully_implicit_function_template_p
10225 = parser->fully_implicit_function_template_p;
10226 tree implicit_template_parms = parser->implicit_template_parms;
10227 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10228 bool auto_is_implicit_function_template_parm_p
10229 = parser->auto_is_implicit_function_template_parm_p;
10231 parser->num_template_parameter_lists = 0;
10232 parser->in_statement = 0;
10233 parser->in_switch_statement_p = false;
10234 parser->fully_implicit_function_template_p = false;
10235 parser->implicit_template_parms = 0;
10236 parser->implicit_template_scope = 0;
10237 parser->auto_is_implicit_function_template_parm_p = false;
10239 /* By virtue of defining a local class, a lambda expression has access to
10240 the private variables of enclosing classes. */
10242 if (cp_parser_start_tentative_firewall (parser))
10243 start = token;
10245 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10247 if (ok && cp_parser_error_occurred (parser))
10248 ok = false;
10250 if (ok)
10252 cp_parser_lambda_body (parser, lambda_expr);
10254 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10256 if (cp_parser_skip_to_closing_brace (parser))
10257 cp_lexer_consume_token (parser->lexer);
10260 /* The capture list was built up in reverse order; fix that now. */
10261 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10262 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10264 if (ok)
10265 maybe_add_lambda_conv_op (type);
10267 type = finish_struct (type, /*attributes=*/NULL_TREE);
10269 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10270 parser->in_statement = in_statement;
10271 parser->in_switch_statement_p = in_switch_statement_p;
10272 parser->fully_implicit_function_template_p
10273 = fully_implicit_function_template_p;
10274 parser->implicit_template_parms = implicit_template_parms;
10275 parser->implicit_template_scope = implicit_template_scope;
10276 parser->auto_is_implicit_function_template_parm_p
10277 = auto_is_implicit_function_template_parm_p;
10280 /* This field is only used during parsing of the lambda. */
10281 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10283 /* This lambda shouldn't have any proxies left at this point. */
10284 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10285 /* And now that we're done, push proxies for an enclosing lambda. */
10286 insert_pending_capture_proxies ();
10288 /* Update the lambda expression to a range. */
10289 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10290 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10291 token->location,
10292 end_tok->location);
10294 if (ok)
10295 lambda_expr = build_lambda_object (lambda_expr);
10296 else
10297 lambda_expr = error_mark_node;
10299 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10301 pop_deferring_access_checks ();
10303 return lambda_expr;
10306 /* Parse the beginning of a lambda expression.
10308 lambda-introducer:
10309 [ lambda-capture [opt] ]
10311 LAMBDA_EXPR is the current representation of the lambda expression. */
10313 static void
10314 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10316 /* Need commas after the first capture. */
10317 bool first = true;
10319 /* Eat the leading `['. */
10320 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10322 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10323 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10324 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10325 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10326 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10327 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10329 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10331 cp_lexer_consume_token (parser->lexer);
10332 first = false;
10334 if (!(at_function_scope_p () || parsing_nsdmi ()))
10335 error ("non-local lambda expression cannot have a capture-default");
10338 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10340 cp_token* capture_token;
10341 tree capture_id;
10342 tree capture_init_expr;
10343 cp_id_kind idk = CP_ID_KIND_NONE;
10344 bool explicit_init_p = false;
10346 enum capture_kind_type
10348 BY_COPY,
10349 BY_REFERENCE
10351 enum capture_kind_type capture_kind = BY_COPY;
10353 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10355 error ("expected end of capture-list");
10356 return;
10359 if (first)
10360 first = false;
10361 else
10362 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10364 /* Possibly capture `this'. */
10365 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10367 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10368 if (cxx_dialect < cxx2a
10369 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10370 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10371 "with by-copy capture default");
10372 cp_lexer_consume_token (parser->lexer);
10373 add_capture (lambda_expr,
10374 /*id=*/this_identifier,
10375 /*initializer=*/finish_this_expr (),
10376 /*by_reference_p=*/true,
10377 explicit_init_p);
10378 continue;
10381 /* Possibly capture `*this'. */
10382 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10383 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10385 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10386 if (cxx_dialect < cxx17)
10387 pedwarn (loc, 0, "%<*this%> capture only available with "
10388 "-std=c++17 or -std=gnu++17");
10389 cp_lexer_consume_token (parser->lexer);
10390 cp_lexer_consume_token (parser->lexer);
10391 add_capture (lambda_expr,
10392 /*id=*/this_identifier,
10393 /*initializer=*/finish_this_expr (),
10394 /*by_reference_p=*/false,
10395 explicit_init_p);
10396 continue;
10399 bool init_pack_expansion = false;
10400 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10402 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10403 if (cxx_dialect < cxx2a)
10404 pedwarn (loc, 0, "pack init-capture only available with "
10405 "-std=c++2a or -std=gnu++2a");
10406 cp_lexer_consume_token (parser->lexer);
10407 init_pack_expansion = true;
10410 /* Remember whether we want to capture as a reference or not. */
10411 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10413 capture_kind = BY_REFERENCE;
10414 cp_lexer_consume_token (parser->lexer);
10417 /* Get the identifier. */
10418 capture_token = cp_lexer_peek_token (parser->lexer);
10419 capture_id = cp_parser_identifier (parser);
10421 if (capture_id == error_mark_node)
10422 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10423 delimiters, but I modified this to stop on unnested ']' as well. It
10424 was already changed to stop on unnested '}', so the
10425 "closing_parenthesis" name is no more misleading with my change. */
10427 cp_parser_skip_to_closing_parenthesis (parser,
10428 /*recovering=*/true,
10429 /*or_comma=*/true,
10430 /*consume_paren=*/true);
10431 break;
10434 /* Find the initializer for this capture. */
10435 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10436 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10437 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10439 bool direct, non_constant;
10440 /* An explicit initializer exists. */
10441 if (cxx_dialect < cxx14)
10442 pedwarn (input_location, 0,
10443 "lambda capture initializers "
10444 "only available with -std=c++14 or -std=gnu++14");
10445 capture_init_expr = cp_parser_initializer (parser, &direct,
10446 &non_constant, true);
10447 explicit_init_p = true;
10448 if (capture_init_expr == NULL_TREE)
10450 error ("empty initializer for lambda init-capture");
10451 capture_init_expr = error_mark_node;
10453 if (init_pack_expansion)
10454 capture_init_expr = make_pack_expansion (capture_init_expr);
10456 else
10458 const char* error_msg;
10460 /* Turn the identifier into an id-expression. */
10461 capture_init_expr
10462 = cp_parser_lookup_name_simple (parser, capture_id,
10463 capture_token->location);
10465 if (capture_init_expr == error_mark_node)
10467 unqualified_name_lookup_error (capture_id);
10468 continue;
10470 else if (!VAR_P (capture_init_expr)
10471 && TREE_CODE (capture_init_expr) != PARM_DECL)
10473 error_at (capture_token->location,
10474 "capture of non-variable %qE",
10475 capture_init_expr);
10476 if (DECL_P (capture_init_expr))
10477 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10478 "%q#D declared here", capture_init_expr);
10479 continue;
10481 if (VAR_P (capture_init_expr)
10482 && decl_storage_duration (capture_init_expr) != dk_auto)
10484 if (pedwarn (capture_token->location, 0, "capture of variable "
10485 "%qD with non-automatic storage duration",
10486 capture_init_expr))
10487 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10488 "%q#D declared here", capture_init_expr);
10489 continue;
10492 capture_init_expr
10493 = finish_id_expression
10494 (capture_id,
10495 capture_init_expr,
10496 parser->scope,
10497 &idk,
10498 /*integral_constant_expression_p=*/false,
10499 /*allow_non_integral_constant_expression_p=*/false,
10500 /*non_integral_constant_expression_p=*/NULL,
10501 /*template_p=*/false,
10502 /*done=*/true,
10503 /*address_p=*/false,
10504 /*template_arg_p=*/false,
10505 &error_msg,
10506 capture_token->location);
10508 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10510 cp_lexer_consume_token (parser->lexer);
10511 capture_init_expr = make_pack_expansion (capture_init_expr);
10515 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10516 && !explicit_init_p)
10518 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10519 && capture_kind == BY_COPY)
10520 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10521 "of %qD redundant with by-copy capture default",
10522 capture_id);
10523 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10524 && capture_kind == BY_REFERENCE)
10525 pedwarn (capture_token->location, 0, "explicit by-reference "
10526 "capture of %qD redundant with by-reference capture "
10527 "default", capture_id);
10530 add_capture (lambda_expr,
10531 capture_id,
10532 capture_init_expr,
10533 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10534 explicit_init_p);
10536 /* If there is any qualification still in effect, clear it
10537 now; we will be starting fresh with the next capture. */
10538 parser->scope = NULL_TREE;
10539 parser->qualifying_scope = NULL_TREE;
10540 parser->object_scope = NULL_TREE;
10543 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10546 /* Parse the (optional) middle of a lambda expression.
10548 lambda-declarator:
10549 < template-parameter-list [opt] >
10550 ( parameter-declaration-clause [opt] )
10551 attribute-specifier [opt]
10552 decl-specifier-seq [opt]
10553 exception-specification [opt]
10554 lambda-return-type-clause [opt]
10556 LAMBDA_EXPR is the current representation of the lambda expression. */
10558 static bool
10559 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10561 /* 5.1.1.4 of the standard says:
10562 If a lambda-expression does not include a lambda-declarator, it is as if
10563 the lambda-declarator were ().
10564 This means an empty parameter list, no attributes, and no exception
10565 specification. */
10566 tree param_list = void_list_node;
10567 tree attributes = NULL_TREE;
10568 tree exception_spec = NULL_TREE;
10569 tree template_param_list = NULL_TREE;
10570 tree tx_qual = NULL_TREE;
10571 tree return_type = NULL_TREE;
10572 cp_decl_specifier_seq lambda_specs;
10573 clear_decl_specs (&lambda_specs);
10575 /* The template-parameter-list is optional, but must begin with
10576 an opening angle if present. */
10577 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10579 if (cxx_dialect < cxx14)
10580 pedwarn (parser->lexer->next_token->location, 0,
10581 "lambda templates are only available with "
10582 "-std=c++14 or -std=gnu++14");
10583 else if (cxx_dialect < cxx2a)
10584 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10585 "lambda templates are only available with "
10586 "-std=c++2a or -std=gnu++2a");
10588 cp_lexer_consume_token (parser->lexer);
10590 template_param_list = cp_parser_template_parameter_list (parser);
10592 cp_parser_skip_to_end_of_template_parameter_list (parser);
10594 /* We just processed one more parameter list. */
10595 ++parser->num_template_parameter_lists;
10598 /* The parameter-declaration-clause is optional (unless
10599 template-parameter-list was given), but must begin with an
10600 opening parenthesis if present. */
10601 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10603 matching_parens parens;
10604 parens.consume_open (parser);
10606 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10608 /* Parse parameters. */
10609 param_list = cp_parser_parameter_declaration_clause (parser);
10611 /* Default arguments shall not be specified in the
10612 parameter-declaration-clause of a lambda-declarator. */
10613 if (cxx_dialect < cxx14)
10614 for (tree t = param_list; t; t = TREE_CHAIN (t))
10615 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10616 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10617 "default argument specified for lambda parameter");
10619 parens.require_close (parser);
10621 /* In the decl-specifier-seq of the lambda-declarator, each
10622 decl-specifier shall either be mutable or constexpr. */
10623 int declares_class_or_enum;
10624 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10625 cp_parser_decl_specifier_seq (parser,
10626 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10627 &lambda_specs, &declares_class_or_enum);
10628 if (lambda_specs.storage_class == sc_mutable)
10630 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10631 if (lambda_specs.conflicting_specifiers_p)
10632 error_at (lambda_specs.locations[ds_storage_class],
10633 "duplicate %<mutable%>");
10636 tx_qual = cp_parser_tx_qualifier_opt (parser);
10638 /* Parse optional exception specification. */
10639 exception_spec = cp_parser_exception_specification_opt (parser);
10641 attributes = cp_parser_std_attribute_spec_seq (parser);
10643 /* Parse optional trailing return type. */
10644 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10646 cp_lexer_consume_token (parser->lexer);
10647 return_type = cp_parser_trailing_type_id (parser);
10650 /* The function parameters must be in scope all the way until after the
10651 trailing-return-type in case of decltype. */
10652 pop_bindings_and_leave_scope ();
10654 else if (template_param_list != NULL_TREE) // generate diagnostic
10655 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10657 /* Create the function call operator.
10659 Messing with declarators like this is no uglier than building up the
10660 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10661 other code. */
10663 cp_decl_specifier_seq return_type_specs;
10664 cp_declarator* declarator;
10665 tree fco;
10666 int quals;
10667 void *p;
10669 clear_decl_specs (&return_type_specs);
10670 return_type_specs.type = make_auto ();
10672 if (lambda_specs.locations[ds_constexpr])
10674 if (cxx_dialect >= cxx17)
10675 return_type_specs.locations[ds_constexpr]
10676 = lambda_specs.locations[ds_constexpr];
10677 else
10678 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10679 "lambda only available with -std=c++17 or -std=gnu++17");
10682 p = obstack_alloc (&declarator_obstack, 0);
10684 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
10685 LAMBDA_EXPR_LOCATION (lambda_expr));
10687 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10688 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10689 declarator = make_call_declarator (declarator, param_list, quals,
10690 VIRT_SPEC_UNSPECIFIED,
10691 REF_QUAL_NONE,
10692 tx_qual,
10693 exception_spec,
10694 return_type,
10695 /*requires_clause*/NULL_TREE);
10696 declarator->std_attributes = attributes;
10698 fco = grokmethod (&return_type_specs,
10699 declarator,
10700 NULL_TREE);
10701 if (fco != error_mark_node)
10703 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10704 DECL_ARTIFICIAL (fco) = 1;
10705 /* Give the object parameter a different name. */
10706 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10707 DECL_LAMBDA_FUNCTION (fco) = 1;
10709 if (template_param_list)
10711 fco = finish_member_template_decl (fco);
10712 finish_template_decl (template_param_list);
10713 --parser->num_template_parameter_lists;
10715 else if (parser->fully_implicit_function_template_p)
10716 fco = finish_fully_implicit_template (parser, fco);
10718 finish_member_declaration (fco);
10720 obstack_free (&declarator_obstack, p);
10722 return (fco != error_mark_node);
10726 /* Parse the body of a lambda expression, which is simply
10728 compound-statement
10730 but which requires special handling.
10731 LAMBDA_EXPR is the current representation of the lambda expression. */
10733 static void
10734 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10736 bool nested = (current_function_decl != NULL_TREE);
10737 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10738 bool in_function_body = parser->in_function_body;
10740 /* The body of a lambda-expression is not a subexpression of the enclosing
10741 expression. */
10742 cp_evaluated ev;
10744 if (nested)
10745 push_function_context ();
10746 else
10747 /* Still increment function_depth so that we don't GC in the
10748 middle of an expression. */
10749 ++function_depth;
10751 vec<tree> omp_privatization_save;
10752 save_omp_privatization_clauses (omp_privatization_save);
10753 /* Clear this in case we're in the middle of a default argument. */
10754 parser->local_variables_forbidden_p = false;
10755 parser->in_function_body = true;
10758 local_specialization_stack s (lss_copy);
10759 tree fco = lambda_function (lambda_expr);
10760 tree body = start_lambda_function (fco, lambda_expr);
10761 matching_braces braces;
10763 if (braces.require_open (parser))
10765 tree compound_stmt = begin_compound_stmt (0);
10767 /* Originally C++11 required us to peek for 'return expr'; and
10768 process it specially here to deduce the return type. N3638
10769 removed the need for that. */
10771 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10772 cp_parser_label_declaration (parser);
10773 cp_parser_statement_seq_opt (parser, NULL_TREE);
10774 braces.require_close (parser);
10776 finish_compound_stmt (compound_stmt);
10779 finish_lambda_function (body);
10782 restore_omp_privatization_clauses (omp_privatization_save);
10783 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10784 parser->in_function_body = in_function_body;
10785 if (nested)
10786 pop_function_context();
10787 else
10788 --function_depth;
10791 /* Statements [gram.stmt.stmt] */
10793 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10795 static void
10796 add_debug_begin_stmt (location_t loc)
10798 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10799 return;
10800 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10801 /* A concept is never expanded normally. */
10802 return;
10804 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10805 SET_EXPR_LOCATION (stmt, loc);
10806 add_stmt (stmt);
10809 /* Parse a statement.
10811 statement:
10812 labeled-statement
10813 expression-statement
10814 compound-statement
10815 selection-statement
10816 iteration-statement
10817 jump-statement
10818 declaration-statement
10819 try-block
10821 C++11:
10823 statement:
10824 labeled-statement
10825 attribute-specifier-seq (opt) expression-statement
10826 attribute-specifier-seq (opt) compound-statement
10827 attribute-specifier-seq (opt) selection-statement
10828 attribute-specifier-seq (opt) iteration-statement
10829 attribute-specifier-seq (opt) jump-statement
10830 declaration-statement
10831 attribute-specifier-seq (opt) try-block
10833 init-statement:
10834 expression-statement
10835 simple-declaration
10837 TM Extension:
10839 statement:
10840 atomic-statement
10842 IN_COMPOUND is true when the statement is nested inside a
10843 cp_parser_compound_statement; this matters for certain pragmas.
10845 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10846 is a (possibly labeled) if statement which is not enclosed in braces
10847 and has an else clause. This is used to implement -Wparentheses.
10849 CHAIN is a vector of if-else-if conditions. */
10851 static void
10852 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10853 bool in_compound, bool *if_p, vec<tree> *chain,
10854 location_t *loc_after_labels)
10856 tree statement, std_attrs = NULL_TREE;
10857 cp_token *token;
10858 location_t statement_location, attrs_location;
10860 restart:
10861 if (if_p != NULL)
10862 *if_p = false;
10863 /* There is no statement yet. */
10864 statement = NULL_TREE;
10866 saved_token_sentinel saved_tokens (parser->lexer);
10867 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10868 if (c_dialect_objc ())
10869 /* In obj-c++, seeing '[[' might be the either the beginning of
10870 c++11 attributes, or a nested objc-message-expression. So
10871 let's parse the c++11 attributes tentatively. */
10872 cp_parser_parse_tentatively (parser);
10873 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10874 if (c_dialect_objc ())
10876 if (!cp_parser_parse_definitely (parser))
10877 std_attrs = NULL_TREE;
10880 /* Peek at the next token. */
10881 token = cp_lexer_peek_token (parser->lexer);
10882 /* Remember the location of the first token in the statement. */
10883 cp_token *statement_token = token;
10884 statement_location = token->location;
10885 add_debug_begin_stmt (statement_location);
10886 /* If this is a keyword, then that will often determine what kind of
10887 statement we have. */
10888 if (token->type == CPP_KEYWORD)
10890 enum rid keyword = token->keyword;
10892 switch (keyword)
10894 case RID_CASE:
10895 case RID_DEFAULT:
10896 /* Looks like a labeled-statement with a case label.
10897 Parse the label, and then use tail recursion to parse
10898 the statement. */
10899 cp_parser_label_for_labeled_statement (parser, std_attrs);
10900 in_compound = false;
10901 goto restart;
10903 case RID_IF:
10904 case RID_SWITCH:
10905 std_attrs = process_stmt_hotness_attribute (std_attrs);
10906 statement = cp_parser_selection_statement (parser, if_p, chain);
10907 break;
10909 case RID_WHILE:
10910 case RID_DO:
10911 case RID_FOR:
10912 std_attrs = process_stmt_hotness_attribute (std_attrs);
10913 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10914 break;
10916 case RID_BREAK:
10917 case RID_CONTINUE:
10918 case RID_RETURN:
10919 case RID_GOTO:
10920 std_attrs = process_stmt_hotness_attribute (std_attrs);
10921 statement = cp_parser_jump_statement (parser);
10922 break;
10924 /* Objective-C++ exception-handling constructs. */
10925 case RID_AT_TRY:
10926 case RID_AT_CATCH:
10927 case RID_AT_FINALLY:
10928 case RID_AT_SYNCHRONIZED:
10929 case RID_AT_THROW:
10930 std_attrs = process_stmt_hotness_attribute (std_attrs);
10931 statement = cp_parser_objc_statement (parser);
10932 break;
10934 case RID_TRY:
10935 std_attrs = process_stmt_hotness_attribute (std_attrs);
10936 statement = cp_parser_try_block (parser);
10937 break;
10939 case RID_NAMESPACE:
10940 /* This must be a namespace alias definition. */
10941 if (std_attrs != NULL_TREE)
10943 /* Attributes should be parsed as part of the the
10944 declaration, so let's un-parse them. */
10945 saved_tokens.rollback();
10946 std_attrs = NULL_TREE;
10948 cp_parser_declaration_statement (parser);
10949 return;
10951 case RID_TRANSACTION_ATOMIC:
10952 case RID_TRANSACTION_RELAXED:
10953 case RID_SYNCHRONIZED:
10954 case RID_ATOMIC_NOEXCEPT:
10955 case RID_ATOMIC_CANCEL:
10956 std_attrs = process_stmt_hotness_attribute (std_attrs);
10957 statement = cp_parser_transaction (parser, token);
10958 break;
10959 case RID_TRANSACTION_CANCEL:
10960 std_attrs = process_stmt_hotness_attribute (std_attrs);
10961 statement = cp_parser_transaction_cancel (parser);
10962 break;
10964 default:
10965 /* It might be a keyword like `int' that can start a
10966 declaration-statement. */
10967 break;
10970 else if (token->type == CPP_NAME)
10972 /* If the next token is a `:', then we are looking at a
10973 labeled-statement. */
10974 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10975 if (token->type == CPP_COLON)
10977 /* Looks like a labeled-statement with an ordinary label.
10978 Parse the label, and then use tail recursion to parse
10979 the statement. */
10981 cp_parser_label_for_labeled_statement (parser, std_attrs);
10982 in_compound = false;
10983 goto restart;
10986 /* Anything that starts with a `{' must be a compound-statement. */
10987 else if (token->type == CPP_OPEN_BRACE)
10988 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10989 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10990 a statement all its own. */
10991 else if (token->type == CPP_PRAGMA)
10993 /* Only certain OpenMP pragmas are attached to statements, and thus
10994 are considered statements themselves. All others are not. In
10995 the context of a compound, accept the pragma as a "statement" and
10996 return so that we can check for a close brace. Otherwise we
10997 require a real statement and must go back and read one. */
10998 if (in_compound)
10999 cp_parser_pragma (parser, pragma_compound, if_p);
11000 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11001 goto restart;
11002 return;
11004 else if (token->type == CPP_EOF)
11006 cp_parser_error (parser, "expected statement");
11007 return;
11010 /* Everything else must be a declaration-statement or an
11011 expression-statement. Try for the declaration-statement
11012 first, unless we are looking at a `;', in which case we know that
11013 we have an expression-statement. */
11014 if (!statement)
11016 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11018 if (std_attrs != NULL_TREE)
11019 /* Attributes should be parsed as part of the declaration,
11020 so let's un-parse them. */
11021 saved_tokens.rollback();
11023 cp_parser_parse_tentatively (parser);
11024 /* Try to parse the declaration-statement. */
11025 cp_parser_declaration_statement (parser);
11026 /* If that worked, we're done. */
11027 if (cp_parser_parse_definitely (parser))
11028 return;
11029 /* It didn't work, restore the post-attribute position. */
11030 if (std_attrs)
11031 cp_lexer_set_token_position (parser->lexer, statement_token);
11033 /* All preceding labels have been parsed at this point. */
11034 if (loc_after_labels != NULL)
11035 *loc_after_labels = statement_location;
11037 std_attrs = process_stmt_hotness_attribute (std_attrs);
11039 /* Look for an expression-statement instead. */
11040 statement = cp_parser_expression_statement (parser, in_statement_expr);
11042 /* Handle [[fallthrough]];. */
11043 if (attribute_fallthrough_p (std_attrs))
11045 /* The next token after the fallthrough attribute is ';'. */
11046 if (statement == NULL_TREE)
11048 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11049 statement = build_call_expr_internal_loc (statement_location,
11050 IFN_FALLTHROUGH,
11051 void_type_node, 0);
11052 finish_expr_stmt (statement);
11054 else
11055 warning_at (statement_location, OPT_Wattributes,
11056 "%<fallthrough%> attribute not followed by %<;%>");
11057 std_attrs = NULL_TREE;
11061 /* Set the line number for the statement. */
11062 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11063 SET_EXPR_LOCATION (statement, statement_location);
11065 /* Allow "[[fallthrough]];", but warn otherwise. */
11066 if (std_attrs != NULL_TREE)
11067 warning_at (attrs_location,
11068 OPT_Wattributes,
11069 "attributes at the beginning of statement are ignored");
11072 /* Append ATTR to attribute list ATTRS. */
11074 static tree
11075 attr_chainon (tree attrs, tree attr)
11077 if (attrs == error_mark_node)
11078 return error_mark_node;
11079 if (attr == error_mark_node)
11080 return error_mark_node;
11081 return chainon (attrs, attr);
11084 /* Parse the label for a labeled-statement, i.e.
11086 identifier :
11087 case constant-expression :
11088 default :
11090 GNU Extension:
11091 case constant-expression ... constant-expression : statement
11093 When a label is parsed without errors, the label is added to the
11094 parse tree by the finish_* functions, so this function doesn't
11095 have to return the label. */
11097 static void
11098 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11100 cp_token *token;
11101 tree label = NULL_TREE;
11102 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11104 /* The next token should be an identifier. */
11105 token = cp_lexer_peek_token (parser->lexer);
11106 if (token->type != CPP_NAME
11107 && token->type != CPP_KEYWORD)
11109 cp_parser_error (parser, "expected labeled-statement");
11110 return;
11113 /* Remember whether this case or a user-defined label is allowed to fall
11114 through to. */
11115 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11117 parser->colon_corrects_to_scope_p = false;
11118 switch (token->keyword)
11120 case RID_CASE:
11122 tree expr, expr_hi;
11123 cp_token *ellipsis;
11125 /* Consume the `case' token. */
11126 cp_lexer_consume_token (parser->lexer);
11127 /* Parse the constant-expression. */
11128 expr = cp_parser_constant_expression (parser);
11129 if (check_for_bare_parameter_packs (expr))
11130 expr = error_mark_node;
11132 ellipsis = cp_lexer_peek_token (parser->lexer);
11133 if (ellipsis->type == CPP_ELLIPSIS)
11135 /* Consume the `...' token. */
11136 cp_lexer_consume_token (parser->lexer);
11137 expr_hi = cp_parser_constant_expression (parser);
11138 if (check_for_bare_parameter_packs (expr_hi))
11139 expr_hi = error_mark_node;
11141 /* We don't need to emit warnings here, as the common code
11142 will do this for us. */
11144 else
11145 expr_hi = NULL_TREE;
11147 if (parser->in_switch_statement_p)
11149 tree l = finish_case_label (token->location, expr, expr_hi);
11150 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11152 label = CASE_LABEL (l);
11153 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11156 else
11157 error_at (token->location,
11158 "case label %qE not within a switch statement",
11159 expr);
11161 break;
11163 case RID_DEFAULT:
11164 /* Consume the `default' token. */
11165 cp_lexer_consume_token (parser->lexer);
11167 if (parser->in_switch_statement_p)
11169 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11170 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11172 label = CASE_LABEL (l);
11173 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11176 else
11177 error_at (token->location, "case label not within a switch statement");
11178 break;
11180 default:
11181 /* Anything else must be an ordinary label. */
11182 label = finish_label_stmt (cp_parser_identifier (parser));
11183 if (label && TREE_CODE (label) == LABEL_DECL)
11184 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11185 break;
11188 /* Require the `:' token. */
11189 cp_parser_require (parser, CPP_COLON, RT_COLON);
11191 /* An ordinary label may optionally be followed by attributes.
11192 However, this is only permitted if the attributes are then
11193 followed by a semicolon. This is because, for backward
11194 compatibility, when parsing
11195 lab: __attribute__ ((unused)) int i;
11196 we want the attribute to attach to "i", not "lab". */
11197 if (label != NULL_TREE
11198 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11200 tree attrs;
11201 cp_parser_parse_tentatively (parser);
11202 attrs = cp_parser_gnu_attributes_opt (parser);
11203 if (attrs == NULL_TREE
11204 /* And fallthrough always binds to the expression-statement. */
11205 || attribute_fallthrough_p (attrs)
11206 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11207 cp_parser_abort_tentative_parse (parser);
11208 else if (!cp_parser_parse_definitely (parser))
11210 else
11211 attributes = attr_chainon (attributes, attrs);
11214 if (attributes != NULL_TREE)
11215 cplus_decl_attributes (&label, attributes, 0);
11217 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11220 /* Parse an expression-statement.
11222 expression-statement:
11223 expression [opt] ;
11225 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11226 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11227 indicates whether this expression-statement is part of an
11228 expression statement. */
11230 static tree
11231 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11233 tree statement = NULL_TREE;
11234 cp_token *token = cp_lexer_peek_token (parser->lexer);
11235 location_t loc = token->location;
11237 /* There might be attribute fallthrough. */
11238 tree attr = cp_parser_gnu_attributes_opt (parser);
11240 /* If the next token is a ';', then there is no expression
11241 statement. */
11242 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11244 statement = cp_parser_expression (parser);
11245 if (statement == error_mark_node
11246 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11248 cp_parser_skip_to_end_of_block_or_statement (parser);
11249 return error_mark_node;
11253 /* Handle [[fallthrough]];. */
11254 if (attribute_fallthrough_p (attr))
11256 /* The next token after the fallthrough attribute is ';'. */
11257 if (statement == NULL_TREE)
11258 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11259 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11260 void_type_node, 0);
11261 else
11262 warning_at (loc, OPT_Wattributes,
11263 "%<fallthrough%> attribute not followed by %<;%>");
11264 attr = NULL_TREE;
11267 /* Allow "[[fallthrough]];", but warn otherwise. */
11268 if (attr != NULL_TREE)
11269 warning_at (loc, OPT_Wattributes,
11270 "attributes at the beginning of statement are ignored");
11272 /* Give a helpful message for "A<T>::type t;" and the like. */
11273 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11274 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11276 if (TREE_CODE (statement) == SCOPE_REF)
11277 error_at (token->location, "need %<typename%> before %qE because "
11278 "%qT is a dependent scope",
11279 statement, TREE_OPERAND (statement, 0));
11280 else if (is_overloaded_fn (statement)
11281 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11283 /* A::A a; */
11284 tree fn = get_first_fn (statement);
11285 error_at (token->location,
11286 "%<%T::%D%> names the constructor, not the type",
11287 DECL_CONTEXT (fn), DECL_NAME (fn));
11291 /* Consume the final `;'. */
11292 cp_parser_consume_semicolon_at_end_of_statement (parser);
11294 if (in_statement_expr
11295 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11296 /* This is the final expression statement of a statement
11297 expression. */
11298 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11299 else if (statement)
11300 statement = finish_expr_stmt (statement);
11302 return statement;
11305 /* Parse a compound-statement.
11307 compound-statement:
11308 { statement-seq [opt] }
11310 GNU extension:
11312 compound-statement:
11313 { label-declaration-seq [opt] statement-seq [opt] }
11315 label-declaration-seq:
11316 label-declaration
11317 label-declaration-seq label-declaration
11319 Returns a tree representing the statement. */
11321 static tree
11322 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11323 int bcs_flags, bool function_body)
11325 tree compound_stmt;
11326 matching_braces braces;
11328 /* Consume the `{'. */
11329 if (!braces.require_open (parser))
11330 return error_mark_node;
11331 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11332 && !function_body && cxx_dialect < cxx14)
11333 pedwarn (input_location, OPT_Wpedantic,
11334 "compound-statement in %<constexpr%> function");
11335 /* Begin the compound-statement. */
11336 compound_stmt = begin_compound_stmt (bcs_flags);
11337 /* If the next keyword is `__label__' we have a label declaration. */
11338 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11339 cp_parser_label_declaration (parser);
11340 /* Parse an (optional) statement-seq. */
11341 cp_parser_statement_seq_opt (parser, in_statement_expr);
11342 /* Finish the compound-statement. */
11343 finish_compound_stmt (compound_stmt);
11344 /* Consume the `}'. */
11345 braces.require_close (parser);
11347 return compound_stmt;
11350 /* Parse an (optional) statement-seq.
11352 statement-seq:
11353 statement
11354 statement-seq [opt] statement */
11356 static void
11357 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11359 /* Scan statements until there aren't any more. */
11360 while (true)
11362 cp_token *token = cp_lexer_peek_token (parser->lexer);
11364 /* If we are looking at a `}', then we have run out of
11365 statements; the same is true if we have reached the end
11366 of file, or have stumbled upon a stray '@end'. */
11367 if (token->type == CPP_CLOSE_BRACE
11368 || token->type == CPP_EOF
11369 || token->type == CPP_PRAGMA_EOL
11370 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11371 break;
11373 /* If we are in a compound statement and find 'else' then
11374 something went wrong. */
11375 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11377 if (parser->in_statement & IN_IF_STMT)
11378 break;
11379 else
11381 token = cp_lexer_consume_token (parser->lexer);
11382 error_at (token->location, "%<else%> without a previous %<if%>");
11386 /* Parse the statement. */
11387 cp_parser_statement (parser, in_statement_expr, true, NULL);
11391 /* Return true if this is the C++20 version of range-based-for with
11392 init-statement. */
11394 static bool
11395 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11397 bool r = false;
11399 /* Save tokens so that we can put them back. */
11400 cp_lexer_save_tokens (parser->lexer);
11402 /* There has to be an unnested ; followed by an unnested :. */
11403 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11404 /*recovering=*/false,
11405 CPP_SEMICOLON,
11406 /*consume_paren=*/false) != -1)
11407 goto out;
11409 /* We found the semicolon, eat it now. */
11410 cp_lexer_consume_token (parser->lexer);
11412 /* Now look for ':' that is not nested in () or {}. */
11413 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11414 /*recovering=*/false,
11415 CPP_COLON,
11416 /*consume_paren=*/false) == -1);
11418 out:
11419 /* Roll back the tokens we skipped. */
11420 cp_lexer_rollback_tokens (parser->lexer);
11422 return r;
11425 /* Return true if we're looking at (init; cond), false otherwise. */
11427 static bool
11428 cp_parser_init_statement_p (cp_parser *parser)
11430 /* Save tokens so that we can put them back. */
11431 cp_lexer_save_tokens (parser->lexer);
11433 /* Look for ';' that is not nested in () or {}. */
11434 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11435 /*recovering=*/false,
11436 CPP_SEMICOLON,
11437 /*consume_paren=*/false);
11439 /* Roll back the tokens we skipped. */
11440 cp_lexer_rollback_tokens (parser->lexer);
11442 return ret == -1;
11445 /* Parse a selection-statement.
11447 selection-statement:
11448 if ( init-statement [opt] condition ) statement
11449 if ( init-statement [opt] condition ) statement else statement
11450 switch ( init-statement [opt] condition ) statement
11452 Returns the new IF_STMT or SWITCH_STMT.
11454 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11455 is a (possibly labeled) if statement which is not enclosed in
11456 braces and has an else clause. This is used to implement
11457 -Wparentheses.
11459 CHAIN is a vector of if-else-if conditions. This is used to implement
11460 -Wduplicated-cond. */
11462 static tree
11463 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11464 vec<tree> *chain)
11466 cp_token *token;
11467 enum rid keyword;
11468 token_indent_info guard_tinfo;
11470 if (if_p != NULL)
11471 *if_p = false;
11473 /* Peek at the next token. */
11474 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11475 guard_tinfo = get_token_indent_info (token);
11477 /* See what kind of keyword it is. */
11478 keyword = token->keyword;
11479 switch (keyword)
11481 case RID_IF:
11482 case RID_SWITCH:
11484 tree statement;
11485 tree condition;
11487 bool cx = false;
11488 if (keyword == RID_IF
11489 && cp_lexer_next_token_is_keyword (parser->lexer,
11490 RID_CONSTEXPR))
11492 cx = true;
11493 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11494 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11495 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11496 "with -std=c++17 or -std=gnu++17");
11499 /* Look for the `('. */
11500 matching_parens parens;
11501 if (!parens.require_open (parser))
11503 cp_parser_skip_to_end_of_statement (parser);
11504 return error_mark_node;
11507 /* Begin the selection-statement. */
11508 if (keyword == RID_IF)
11510 statement = begin_if_stmt ();
11511 IF_STMT_CONSTEXPR_P (statement) = cx;
11513 else
11514 statement = begin_switch_stmt ();
11516 /* Parse the optional init-statement. */
11517 if (cp_parser_init_statement_p (parser))
11519 tree decl;
11520 if (cxx_dialect < cxx17)
11521 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11522 "init-statement in selection statements only available "
11523 "with -std=c++17 or -std=gnu++17");
11524 cp_parser_init_statement (parser, &decl);
11527 /* Parse the condition. */
11528 condition = cp_parser_condition (parser);
11529 /* Look for the `)'. */
11530 if (!parens.require_close (parser))
11531 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11532 /*consume_paren=*/true);
11534 if (keyword == RID_IF)
11536 bool nested_if;
11537 unsigned char in_statement;
11539 /* Add the condition. */
11540 condition = finish_if_stmt_cond (condition, statement);
11542 if (warn_duplicated_cond)
11543 warn_duplicated_cond_add_or_warn (token->location, condition,
11544 &chain);
11546 /* Parse the then-clause. */
11547 in_statement = parser->in_statement;
11548 parser->in_statement |= IN_IF_STMT;
11550 /* Outside a template, the non-selected branch of a constexpr
11551 if is a 'discarded statement', i.e. unevaluated. */
11552 bool was_discarded = in_discarded_stmt;
11553 bool discard_then = (cx && !processing_template_decl
11554 && integer_zerop (condition));
11555 if (discard_then)
11557 in_discarded_stmt = true;
11558 ++c_inhibit_evaluation_warnings;
11561 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11562 guard_tinfo);
11564 parser->in_statement = in_statement;
11566 finish_then_clause (statement);
11568 if (discard_then)
11570 THEN_CLAUSE (statement) = NULL_TREE;
11571 in_discarded_stmt = was_discarded;
11572 --c_inhibit_evaluation_warnings;
11575 /* If the next token is `else', parse the else-clause. */
11576 if (cp_lexer_next_token_is_keyword (parser->lexer,
11577 RID_ELSE))
11579 bool discard_else = (cx && !processing_template_decl
11580 && integer_nonzerop (condition));
11581 if (discard_else)
11583 in_discarded_stmt = true;
11584 ++c_inhibit_evaluation_warnings;
11587 guard_tinfo
11588 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11589 /* Consume the `else' keyword. */
11590 cp_lexer_consume_token (parser->lexer);
11591 if (warn_duplicated_cond)
11593 if (cp_lexer_next_token_is_keyword (parser->lexer,
11594 RID_IF)
11595 && chain == NULL)
11597 /* We've got "if (COND) else if (COND2)". Start
11598 the condition chain and add COND as the first
11599 element. */
11600 chain = new vec<tree> ();
11601 if (!CONSTANT_CLASS_P (condition)
11602 && !TREE_SIDE_EFFECTS (condition))
11604 /* Wrap it in a NOP_EXPR so that we can set the
11605 location of the condition. */
11606 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11607 condition);
11608 SET_EXPR_LOCATION (e, token->location);
11609 chain->safe_push (e);
11612 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11613 RID_IF))
11615 /* This is if-else without subsequent if. Zap the
11616 condition chain; we would have already warned at
11617 this point. */
11618 delete chain;
11619 chain = NULL;
11622 begin_else_clause (statement);
11623 /* Parse the else-clause. */
11624 cp_parser_implicitly_scoped_statement (parser, NULL,
11625 guard_tinfo, chain);
11627 finish_else_clause (statement);
11629 /* If we are currently parsing a then-clause, then
11630 IF_P will not be NULL. We set it to true to
11631 indicate that this if statement has an else clause.
11632 This may trigger the Wparentheses warning below
11633 when we get back up to the parent if statement. */
11634 if (if_p != NULL)
11635 *if_p = true;
11637 if (discard_else)
11639 ELSE_CLAUSE (statement) = NULL_TREE;
11640 in_discarded_stmt = was_discarded;
11641 --c_inhibit_evaluation_warnings;
11644 else
11646 /* This if statement does not have an else clause. If
11647 NESTED_IF is true, then the then-clause has an if
11648 statement which does have an else clause. We warn
11649 about the potential ambiguity. */
11650 if (nested_if)
11651 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11652 "suggest explicit braces to avoid ambiguous"
11653 " %<else%>");
11654 if (warn_duplicated_cond)
11656 /* We don't need the condition chain anymore. */
11657 delete chain;
11658 chain = NULL;
11662 /* Now we're all done with the if-statement. */
11663 finish_if_stmt (statement);
11665 else
11667 bool in_switch_statement_p;
11668 unsigned char in_statement;
11670 /* Add the condition. */
11671 finish_switch_cond (condition, statement);
11673 /* Parse the body of the switch-statement. */
11674 in_switch_statement_p = parser->in_switch_statement_p;
11675 in_statement = parser->in_statement;
11676 parser->in_switch_statement_p = true;
11677 parser->in_statement |= IN_SWITCH_STMT;
11678 cp_parser_implicitly_scoped_statement (parser, if_p,
11679 guard_tinfo);
11680 parser->in_switch_statement_p = in_switch_statement_p;
11681 parser->in_statement = in_statement;
11683 /* Now we're all done with the switch-statement. */
11684 finish_switch_stmt (statement);
11687 return statement;
11689 break;
11691 default:
11692 cp_parser_error (parser, "expected selection-statement");
11693 return error_mark_node;
11697 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11698 If we have seen at least one decl-specifier, and the next token
11699 is not a parenthesis, then we must be looking at a declaration.
11700 (After "int (" we might be looking at a functional cast.) */
11702 static void
11703 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11704 bool any_specifiers_p)
11706 if (any_specifiers_p
11707 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11708 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11709 && !cp_parser_error_occurred (parser))
11710 cp_parser_commit_to_tentative_parse (parser);
11713 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11714 The declarator shall not specify a function or an array. Returns
11715 TRUE if the declarator is valid, FALSE otherwise. */
11717 static bool
11718 cp_parser_check_condition_declarator (cp_parser* parser,
11719 cp_declarator *declarator,
11720 location_t loc)
11722 if (declarator == cp_error_declarator
11723 || function_declarator_p (declarator)
11724 || declarator->kind == cdk_array)
11726 if (declarator == cp_error_declarator)
11727 /* Already complained. */;
11728 else if (declarator->kind == cdk_array)
11729 error_at (loc, "condition declares an array");
11730 else
11731 error_at (loc, "condition declares a function");
11732 if (parser->fully_implicit_function_template_p)
11733 abort_fully_implicit_template (parser);
11734 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11735 /*or_comma=*/false,
11736 /*consume_paren=*/false);
11737 return false;
11739 else
11740 return true;
11743 /* Parse a condition.
11745 condition:
11746 expression
11747 type-specifier-seq declarator = initializer-clause
11748 type-specifier-seq declarator braced-init-list
11750 GNU Extension:
11752 condition:
11753 type-specifier-seq declarator asm-specification [opt]
11754 attributes [opt] = assignment-expression
11756 Returns the expression that should be tested. */
11758 static tree
11759 cp_parser_condition (cp_parser* parser)
11761 cp_decl_specifier_seq type_specifiers;
11762 const char *saved_message;
11763 int declares_class_or_enum;
11765 /* Try the declaration first. */
11766 cp_parser_parse_tentatively (parser);
11767 /* New types are not allowed in the type-specifier-seq for a
11768 condition. */
11769 saved_message = parser->type_definition_forbidden_message;
11770 parser->type_definition_forbidden_message
11771 = G_("types may not be defined in conditions");
11772 /* Parse the type-specifier-seq. */
11773 cp_parser_decl_specifier_seq (parser,
11774 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11775 &type_specifiers,
11776 &declares_class_or_enum);
11777 /* Restore the saved message. */
11778 parser->type_definition_forbidden_message = saved_message;
11780 cp_parser_maybe_commit_to_declaration (parser,
11781 type_specifiers.any_specifiers_p);
11783 /* If all is well, we might be looking at a declaration. */
11784 if (!cp_parser_error_occurred (parser))
11786 tree decl;
11787 tree asm_specification;
11788 tree attributes;
11789 cp_declarator *declarator;
11790 tree initializer = NULL_TREE;
11791 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11793 /* Parse the declarator. */
11794 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11795 /*ctor_dtor_or_conv_p=*/NULL,
11796 /*parenthesized_p=*/NULL,
11797 /*member_p=*/false,
11798 /*friend_p=*/false);
11799 /* Parse the attributes. */
11800 attributes = cp_parser_attributes_opt (parser);
11801 /* Parse the asm-specification. */
11802 asm_specification = cp_parser_asm_specification_opt (parser);
11803 /* If the next token is not an `=' or '{', then we might still be
11804 looking at an expression. For example:
11806 if (A(a).x)
11808 looks like a decl-specifier-seq and a declarator -- but then
11809 there is no `=', so this is an expression. */
11810 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11811 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11812 cp_parser_simulate_error (parser);
11814 /* If we did see an `=' or '{', then we are looking at a declaration
11815 for sure. */
11816 if (cp_parser_parse_definitely (parser))
11818 tree pushed_scope;
11819 bool non_constant_p = false;
11820 int flags = LOOKUP_ONLYCONVERTING;
11822 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11823 return error_mark_node;
11825 /* Create the declaration. */
11826 decl = start_decl (declarator, &type_specifiers,
11827 /*initialized_p=*/true,
11828 attributes, /*prefix_attributes=*/NULL_TREE,
11829 &pushed_scope);
11831 /* Parse the initializer. */
11832 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11834 initializer = cp_parser_braced_list (parser, &non_constant_p);
11835 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11836 flags = 0;
11838 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11840 /* Consume the `='. */
11841 cp_lexer_consume_token (parser->lexer);
11842 initializer = cp_parser_initializer_clause (parser,
11843 &non_constant_p);
11845 else
11847 cp_parser_error (parser, "expected initializer");
11848 initializer = error_mark_node;
11850 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11851 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11853 /* Process the initializer. */
11854 cp_finish_decl (decl,
11855 initializer, !non_constant_p,
11856 asm_specification,
11857 flags);
11859 if (pushed_scope)
11860 pop_scope (pushed_scope);
11862 return convert_from_reference (decl);
11865 /* If we didn't even get past the declarator successfully, we are
11866 definitely not looking at a declaration. */
11867 else
11868 cp_parser_abort_tentative_parse (parser);
11870 /* Otherwise, we are looking at an expression. */
11871 return cp_parser_expression (parser);
11874 /* Parses a for-statement or range-for-statement until the closing ')',
11875 not included. */
11877 static tree
11878 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11880 tree init, scope, decl;
11881 bool is_range_for;
11883 /* Begin the for-statement. */
11884 scope = begin_for_scope (&init);
11886 /* Parse the initialization. */
11887 is_range_for = cp_parser_init_statement (parser, &decl);
11889 if (is_range_for)
11890 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
11891 false);
11892 else
11893 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11896 static tree
11897 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11898 unsigned short unroll)
11900 /* Normal for loop */
11901 tree condition = NULL_TREE;
11902 tree expression = NULL_TREE;
11903 tree stmt;
11905 stmt = begin_for_stmt (scope, init);
11906 /* The init-statement has already been parsed in
11907 cp_parser_init_statement, so no work is needed here. */
11908 finish_init_stmt (stmt);
11910 /* If there's a condition, process it. */
11911 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11912 condition = cp_parser_condition (parser);
11913 else if (ivdep)
11915 cp_parser_error (parser, "missing loop condition in loop with "
11916 "%<GCC ivdep%> pragma");
11917 condition = error_mark_node;
11919 else if (unroll)
11921 cp_parser_error (parser, "missing loop condition in loop with "
11922 "%<GCC unroll%> pragma");
11923 condition = error_mark_node;
11925 finish_for_cond (condition, stmt, ivdep, unroll);
11926 /* Look for the `;'. */
11927 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11929 /* If there's an expression, process it. */
11930 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11931 expression = cp_parser_expression (parser);
11932 finish_for_expr (expression, stmt);
11934 return stmt;
11937 /* Tries to parse a range-based for-statement:
11939 range-based-for:
11940 decl-specifier-seq declarator : expression
11942 The decl-specifier-seq declarator and the `:' are already parsed by
11943 cp_parser_init_statement. If processing_template_decl it returns a
11944 newly created RANGE_FOR_STMT; if not, it is converted to a
11945 regular FOR_STMT. */
11947 static tree
11948 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11949 bool ivdep, unsigned short unroll, bool is_omp)
11951 tree stmt, range_expr;
11952 auto_vec <cxx_binding *, 16> bindings;
11953 auto_vec <tree, 16> names;
11954 tree decomp_first_name = NULL_TREE;
11955 unsigned int decomp_cnt = 0;
11957 /* Get the range declaration momentarily out of the way so that
11958 the range expression doesn't clash with it. */
11959 if (range_decl != error_mark_node)
11961 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11963 tree v = DECL_VALUE_EXPR (range_decl);
11964 /* For decomposition declaration get all of the corresponding
11965 declarations out of the way. */
11966 if (TREE_CODE (v) == ARRAY_REF
11967 && VAR_P (TREE_OPERAND (v, 0))
11968 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11970 tree d = range_decl;
11971 range_decl = TREE_OPERAND (v, 0);
11972 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11973 decomp_first_name = d;
11974 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11976 tree name = DECL_NAME (d);
11977 names.safe_push (name);
11978 bindings.safe_push (IDENTIFIER_BINDING (name));
11979 IDENTIFIER_BINDING (name)
11980 = IDENTIFIER_BINDING (name)->previous;
11984 if (names.is_empty ())
11986 tree name = DECL_NAME (range_decl);
11987 names.safe_push (name);
11988 bindings.safe_push (IDENTIFIER_BINDING (name));
11989 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11993 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11995 bool expr_non_constant_p;
11996 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11998 else
11999 range_expr = cp_parser_expression (parser);
12001 /* Put the range declaration(s) back into scope. */
12002 for (unsigned int i = 0; i < names.length (); i++)
12004 cxx_binding *binding = bindings[i];
12005 binding->previous = IDENTIFIER_BINDING (names[i]);
12006 IDENTIFIER_BINDING (names[i]) = binding;
12009 /* finish_omp_for has its own code for the following, so just
12010 return the range_expr instead. */
12011 if (is_omp)
12012 return range_expr;
12014 /* If in template, STMT is converted to a normal for-statement
12015 at instantiation. If not, it is done just ahead. */
12016 if (processing_template_decl)
12018 if (check_for_bare_parameter_packs (range_expr))
12019 range_expr = error_mark_node;
12020 stmt = begin_range_for_stmt (scope, init);
12021 if (ivdep)
12022 RANGE_FOR_IVDEP (stmt) = 1;
12023 if (unroll)
12024 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12025 finish_range_for_decl (stmt, range_decl, range_expr);
12026 if (!type_dependent_expression_p (range_expr)
12027 /* do_auto_deduction doesn't mess with template init-lists. */
12028 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12029 do_range_for_auto_deduction (range_decl, range_expr);
12031 else
12033 stmt = begin_for_stmt (scope, init);
12034 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12035 decomp_first_name, decomp_cnt, ivdep,
12036 unroll);
12038 return stmt;
12041 /* Subroutine of cp_convert_range_for: given the initializer expression,
12042 builds up the range temporary. */
12044 static tree
12045 build_range_temp (tree range_expr)
12047 tree range_type, range_temp;
12049 /* Find out the type deduced by the declaration
12050 `auto &&__range = range_expr'. */
12051 range_type = cp_build_reference_type (make_auto (), true);
12052 range_type = do_auto_deduction (range_type, range_expr,
12053 type_uses_auto (range_type));
12055 /* Create the __range variable. */
12056 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12057 range_type);
12058 TREE_USED (range_temp) = 1;
12059 DECL_ARTIFICIAL (range_temp) = 1;
12061 return range_temp;
12064 /* Used by cp_parser_range_for in template context: we aren't going to
12065 do a full conversion yet, but we still need to resolve auto in the
12066 type of the for-range-declaration if present. This is basically
12067 a shortcut version of cp_convert_range_for. */
12069 static void
12070 do_range_for_auto_deduction (tree decl, tree range_expr)
12072 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12073 if (auto_node)
12075 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12076 range_temp = convert_from_reference (build_range_temp (range_expr));
12077 iter_type = (cp_parser_perform_range_for_lookup
12078 (range_temp, &begin_dummy, &end_dummy));
12079 if (iter_type)
12081 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12082 iter_type);
12083 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12084 RO_UNARY_STAR,
12085 tf_warning_or_error);
12086 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12087 iter_decl, auto_node);
12092 /* Converts a range-based for-statement into a normal
12093 for-statement, as per the definition.
12095 for (RANGE_DECL : RANGE_EXPR)
12096 BLOCK
12098 should be equivalent to:
12101 auto &&__range = RANGE_EXPR;
12102 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12103 __begin != __end;
12104 ++__begin)
12106 RANGE_DECL = *__begin;
12107 BLOCK
12111 If RANGE_EXPR is an array:
12112 BEGIN_EXPR = __range
12113 END_EXPR = __range + ARRAY_SIZE(__range)
12114 Else if RANGE_EXPR has a member 'begin' or 'end':
12115 BEGIN_EXPR = __range.begin()
12116 END_EXPR = __range.end()
12117 Else:
12118 BEGIN_EXPR = begin(__range)
12119 END_EXPR = end(__range);
12121 If __range has a member 'begin' but not 'end', or vice versa, we must
12122 still use the second alternative (it will surely fail, however).
12123 When calling begin()/end() in the third alternative we must use
12124 argument dependent lookup, but always considering 'std' as an associated
12125 namespace. */
12127 tree
12128 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12129 tree decomp_first_name, unsigned int decomp_cnt,
12130 bool ivdep, unsigned short unroll)
12132 tree begin, end;
12133 tree iter_type, begin_expr, end_expr;
12134 tree condition, expression;
12136 range_expr = mark_lvalue_use (range_expr);
12138 if (range_decl == error_mark_node || range_expr == error_mark_node)
12139 /* If an error happened previously do nothing or else a lot of
12140 unhelpful errors would be issued. */
12141 begin_expr = end_expr = iter_type = error_mark_node;
12142 else
12144 tree range_temp;
12146 if (VAR_P (range_expr)
12147 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12148 /* Can't bind a reference to an array of runtime bound. */
12149 range_temp = range_expr;
12150 else
12152 range_temp = build_range_temp (range_expr);
12153 pushdecl (range_temp);
12154 cp_finish_decl (range_temp, range_expr,
12155 /*is_constant_init*/false, NULL_TREE,
12156 LOOKUP_ONLYCONVERTING);
12157 range_temp = convert_from_reference (range_temp);
12159 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12160 &begin_expr, &end_expr);
12163 /* The new for initialization statement. */
12164 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12165 iter_type);
12166 TREE_USED (begin) = 1;
12167 DECL_ARTIFICIAL (begin) = 1;
12168 pushdecl (begin);
12169 cp_finish_decl (begin, begin_expr,
12170 /*is_constant_init*/false, NULL_TREE,
12171 LOOKUP_ONLYCONVERTING);
12173 if (cxx_dialect >= cxx17)
12174 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12175 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12176 TREE_USED (end) = 1;
12177 DECL_ARTIFICIAL (end) = 1;
12178 pushdecl (end);
12179 cp_finish_decl (end, end_expr,
12180 /*is_constant_init*/false, NULL_TREE,
12181 LOOKUP_ONLYCONVERTING);
12183 finish_init_stmt (statement);
12185 /* The new for condition. */
12186 condition = build_x_binary_op (input_location, NE_EXPR,
12187 begin, ERROR_MARK,
12188 end, ERROR_MARK,
12189 NULL, tf_warning_or_error);
12190 finish_for_cond (condition, statement, ivdep, unroll);
12192 /* The new increment expression. */
12193 expression = finish_unary_op_expr (input_location,
12194 PREINCREMENT_EXPR, begin,
12195 tf_warning_or_error);
12196 finish_for_expr (expression, statement);
12198 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12199 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12201 /* The declaration is initialized with *__begin inside the loop body. */
12202 cp_finish_decl (range_decl,
12203 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12204 tf_warning_or_error),
12205 /*is_constant_init*/false, NULL_TREE,
12206 LOOKUP_ONLYCONVERTING);
12207 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12208 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12210 return statement;
12213 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12214 We need to solve both at the same time because the method used
12215 depends on the existence of members begin or end.
12216 Returns the type deduced for the iterator expression. */
12218 static tree
12219 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12221 if (error_operand_p (range))
12223 *begin = *end = error_mark_node;
12224 return error_mark_node;
12227 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12229 error ("range-based %<for%> expression of type %qT "
12230 "has incomplete type", TREE_TYPE (range));
12231 *begin = *end = error_mark_node;
12232 return error_mark_node;
12234 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12236 /* If RANGE is an array, we will use pointer arithmetic. */
12237 *begin = decay_conversion (range, tf_warning_or_error);
12238 *end = build_binary_op (input_location, PLUS_EXPR,
12239 range,
12240 array_type_nelts_top (TREE_TYPE (range)),
12241 false);
12242 return TREE_TYPE (*begin);
12244 else
12246 /* If it is not an array, we must do a bit of magic. */
12247 tree id_begin, id_end;
12248 tree member_begin, member_end;
12250 *begin = *end = error_mark_node;
12252 id_begin = get_identifier ("begin");
12253 id_end = get_identifier ("end");
12254 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12255 /*protect=*/2, /*want_type=*/false,
12256 tf_warning_or_error);
12257 member_end = lookup_member (TREE_TYPE (range), id_end,
12258 /*protect=*/2, /*want_type=*/false,
12259 tf_warning_or_error);
12261 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12263 /* Use the member functions. */
12264 *begin = cp_parser_range_for_member_function (range, id_begin);
12265 *end = cp_parser_range_for_member_function (range, id_end);
12267 else
12269 /* Use global functions with ADL. */
12270 vec<tree, va_gc> *vec;
12271 vec = make_tree_vector ();
12273 vec_safe_push (vec, range);
12275 member_begin = perform_koenig_lookup (id_begin, vec,
12276 tf_warning_or_error);
12277 *begin = finish_call_expr (member_begin, &vec, false, true,
12278 tf_warning_or_error);
12279 member_end = perform_koenig_lookup (id_end, vec,
12280 tf_warning_or_error);
12281 *end = finish_call_expr (member_end, &vec, false, true,
12282 tf_warning_or_error);
12284 release_tree_vector (vec);
12287 /* Last common checks. */
12288 if (*begin == error_mark_node || *end == error_mark_node)
12290 /* If one of the expressions is an error do no more checks. */
12291 *begin = *end = error_mark_node;
12292 return error_mark_node;
12294 else if (type_dependent_expression_p (*begin)
12295 || type_dependent_expression_p (*end))
12296 /* Can happen, when, eg, in a template context, Koenig lookup
12297 can't resolve begin/end (c++/58503). */
12298 return NULL_TREE;
12299 else
12301 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12302 /* The unqualified type of the __begin and __end temporaries should
12303 be the same, as required by the multiple auto declaration. */
12304 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12306 if (cxx_dialect >= cxx17
12307 && (build_x_binary_op (input_location, NE_EXPR,
12308 *begin, ERROR_MARK,
12309 *end, ERROR_MARK,
12310 NULL, tf_none)
12311 != error_mark_node))
12312 /* P0184R0 allows __begin and __end to have different types,
12313 but make sure they are comparable so we can give a better
12314 diagnostic. */;
12315 else
12316 error ("inconsistent begin/end types in range-based %<for%> "
12317 "statement: %qT and %qT",
12318 TREE_TYPE (*begin), TREE_TYPE (*end));
12320 return iter_type;
12325 /* Helper function for cp_parser_perform_range_for_lookup.
12326 Builds a tree for RANGE.IDENTIFIER(). */
12328 static tree
12329 cp_parser_range_for_member_function (tree range, tree identifier)
12331 tree member, res;
12332 vec<tree, va_gc> *vec;
12334 member = finish_class_member_access_expr (range, identifier,
12335 false, tf_warning_or_error);
12336 if (member == error_mark_node)
12337 return error_mark_node;
12339 vec = make_tree_vector ();
12340 res = finish_call_expr (member, &vec,
12341 /*disallow_virtual=*/false,
12342 /*koenig_p=*/false,
12343 tf_warning_or_error);
12344 release_tree_vector (vec);
12345 return res;
12348 /* Parse an iteration-statement.
12350 iteration-statement:
12351 while ( condition ) statement
12352 do statement while ( expression ) ;
12353 for ( init-statement condition [opt] ; expression [opt] )
12354 statement
12356 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12358 static tree
12359 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12360 unsigned short unroll)
12362 cp_token *token;
12363 enum rid keyword;
12364 tree statement;
12365 unsigned char in_statement;
12366 token_indent_info guard_tinfo;
12368 /* Peek at the next token. */
12369 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12370 if (!token)
12371 return error_mark_node;
12373 guard_tinfo = get_token_indent_info (token);
12375 /* Remember whether or not we are already within an iteration
12376 statement. */
12377 in_statement = parser->in_statement;
12379 /* See what kind of keyword it is. */
12380 keyword = token->keyword;
12381 switch (keyword)
12383 case RID_WHILE:
12385 tree condition;
12387 /* Begin the while-statement. */
12388 statement = begin_while_stmt ();
12389 /* Look for the `('. */
12390 matching_parens parens;
12391 parens.require_open (parser);
12392 /* Parse the condition. */
12393 condition = cp_parser_condition (parser);
12394 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12395 /* Look for the `)'. */
12396 parens.require_close (parser);
12397 /* Parse the dependent statement. */
12398 parser->in_statement = IN_ITERATION_STMT;
12399 bool prev = note_iteration_stmt_body_start ();
12400 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12401 note_iteration_stmt_body_end (prev);
12402 parser->in_statement = in_statement;
12403 /* We're done with the while-statement. */
12404 finish_while_stmt (statement);
12406 break;
12408 case RID_DO:
12410 tree expression;
12412 /* Begin the do-statement. */
12413 statement = begin_do_stmt ();
12414 /* Parse the body of the do-statement. */
12415 parser->in_statement = IN_ITERATION_STMT;
12416 bool prev = note_iteration_stmt_body_start ();
12417 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12418 note_iteration_stmt_body_end (prev);
12419 parser->in_statement = in_statement;
12420 finish_do_body (statement);
12421 /* Look for the `while' keyword. */
12422 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12423 /* Look for the `('. */
12424 matching_parens parens;
12425 parens.require_open (parser);
12426 /* Parse the expression. */
12427 expression = cp_parser_expression (parser);
12428 /* We're done with the do-statement. */
12429 finish_do_stmt (expression, statement, ivdep, unroll);
12430 /* Look for the `)'. */
12431 parens.require_close (parser);
12432 /* Look for the `;'. */
12433 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12435 break;
12437 case RID_FOR:
12439 /* Look for the `('. */
12440 matching_parens parens;
12441 parens.require_open (parser);
12443 statement = cp_parser_for (parser, ivdep, unroll);
12445 /* Look for the `)'. */
12446 parens.require_close (parser);
12448 /* Parse the body of the for-statement. */
12449 parser->in_statement = IN_ITERATION_STMT;
12450 bool prev = note_iteration_stmt_body_start ();
12451 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12452 note_iteration_stmt_body_end (prev);
12453 parser->in_statement = in_statement;
12455 /* We're done with the for-statement. */
12456 finish_for_stmt (statement);
12458 break;
12460 default:
12461 cp_parser_error (parser, "expected iteration-statement");
12462 statement = error_mark_node;
12463 break;
12466 return statement;
12469 /* Parse a init-statement or the declarator of a range-based-for.
12470 Returns true if a range-based-for declaration is seen.
12472 init-statement:
12473 expression-statement
12474 simple-declaration */
12476 static bool
12477 cp_parser_init_statement (cp_parser *parser, tree *decl)
12479 /* If the next token is a `;', then we have an empty
12480 expression-statement. Grammatically, this is also a
12481 simple-declaration, but an invalid one, because it does not
12482 declare anything. Therefore, if we did not handle this case
12483 specially, we would issue an error message about an invalid
12484 declaration. */
12485 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12487 bool is_range_for = false;
12488 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12490 /* Try to parse the init-statement. */
12491 if (cp_parser_range_based_for_with_init_p (parser))
12493 tree dummy;
12494 cp_parser_parse_tentatively (parser);
12495 /* Parse the declaration. */
12496 cp_parser_simple_declaration (parser,
12497 /*function_definition_allowed_p=*/false,
12498 &dummy);
12499 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12500 if (!cp_parser_parse_definitely (parser))
12501 /* That didn't work, try to parse it as an expression-statement. */
12502 cp_parser_expression_statement (parser, NULL_TREE);
12504 if (cxx_dialect < cxx2a)
12506 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12507 "range-based %<for%> loops with initializer only "
12508 "available with -std=c++2a or -std=gnu++2a");
12509 *decl = error_mark_node;
12513 /* A colon is used in range-based for. */
12514 parser->colon_corrects_to_scope_p = false;
12516 /* We're going to speculatively look for a declaration, falling back
12517 to an expression, if necessary. */
12518 cp_parser_parse_tentatively (parser);
12519 /* Parse the declaration. */
12520 cp_parser_simple_declaration (parser,
12521 /*function_definition_allowed_p=*/false,
12522 decl);
12523 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12524 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12526 /* It is a range-for, consume the ':'. */
12527 cp_lexer_consume_token (parser->lexer);
12528 is_range_for = true;
12529 if (cxx_dialect < cxx11)
12530 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12531 "range-based %<for%> loops only available with "
12532 "-std=c++11 or -std=gnu++11");
12534 else
12535 /* The ';' is not consumed yet because we told
12536 cp_parser_simple_declaration not to. */
12537 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12539 if (cp_parser_parse_definitely (parser))
12540 return is_range_for;
12541 /* If the tentative parse failed, then we shall need to look for an
12542 expression-statement. */
12544 /* If we are here, it is an expression-statement. */
12545 cp_parser_expression_statement (parser, NULL_TREE);
12546 return false;
12549 /* Parse a jump-statement.
12551 jump-statement:
12552 break ;
12553 continue ;
12554 return expression [opt] ;
12555 return braced-init-list ;
12556 goto identifier ;
12558 GNU extension:
12560 jump-statement:
12561 goto * expression ;
12563 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12565 static tree
12566 cp_parser_jump_statement (cp_parser* parser)
12568 tree statement = error_mark_node;
12569 cp_token *token;
12570 enum rid keyword;
12571 unsigned char in_statement;
12573 /* Peek at the next token. */
12574 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12575 if (!token)
12576 return error_mark_node;
12578 /* See what kind of keyword it is. */
12579 keyword = token->keyword;
12580 switch (keyword)
12582 case RID_BREAK:
12583 in_statement = parser->in_statement & ~IN_IF_STMT;
12584 switch (in_statement)
12586 case 0:
12587 error_at (token->location, "break statement not within loop or switch");
12588 break;
12589 default:
12590 gcc_assert ((in_statement & IN_SWITCH_STMT)
12591 || in_statement == IN_ITERATION_STMT);
12592 statement = finish_break_stmt ();
12593 if (in_statement == IN_ITERATION_STMT)
12594 break_maybe_infinite_loop ();
12595 break;
12596 case IN_OMP_BLOCK:
12597 error_at (token->location, "invalid exit from OpenMP structured block");
12598 break;
12599 case IN_OMP_FOR:
12600 error_at (token->location, "break statement used with OpenMP for loop");
12601 break;
12603 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12604 break;
12606 case RID_CONTINUE:
12607 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12609 case 0:
12610 error_at (token->location, "continue statement not within a loop");
12611 break;
12612 /* Fall through. */
12613 case IN_ITERATION_STMT:
12614 case IN_OMP_FOR:
12615 statement = finish_continue_stmt ();
12616 break;
12617 case IN_OMP_BLOCK:
12618 error_at (token->location, "invalid exit from OpenMP structured block");
12619 break;
12620 default:
12621 gcc_unreachable ();
12623 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12624 break;
12626 case RID_RETURN:
12628 tree expr;
12629 bool expr_non_constant_p;
12631 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12633 cp_lexer_set_source_position (parser->lexer);
12634 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12635 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12637 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12638 expr = cp_parser_expression (parser);
12639 else
12640 /* If the next token is a `;', then there is no
12641 expression. */
12642 expr = NULL_TREE;
12643 /* Build the return-statement. */
12644 if (current_function_auto_return_pattern && in_discarded_stmt)
12645 /* Don't deduce from a discarded return statement. */;
12646 else
12647 statement = finish_return_stmt (expr);
12648 /* Look for the final `;'. */
12649 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12651 break;
12653 case RID_GOTO:
12654 if (parser->in_function_body
12655 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12657 error ("%<goto%> in %<constexpr%> function");
12658 cp_function_chain->invalid_constexpr = true;
12661 /* Create the goto-statement. */
12662 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12664 /* Issue a warning about this use of a GNU extension. */
12665 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12666 /* Consume the '*' token. */
12667 cp_lexer_consume_token (parser->lexer);
12668 /* Parse the dependent expression. */
12669 finish_goto_stmt (cp_parser_expression (parser));
12671 else
12672 finish_goto_stmt (cp_parser_identifier (parser));
12673 /* Look for the final `;'. */
12674 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12675 break;
12677 default:
12678 cp_parser_error (parser, "expected jump-statement");
12679 break;
12682 return statement;
12685 /* Parse a declaration-statement.
12687 declaration-statement:
12688 block-declaration */
12690 static void
12691 cp_parser_declaration_statement (cp_parser* parser)
12693 void *p;
12695 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12696 p = obstack_alloc (&declarator_obstack, 0);
12698 /* Parse the block-declaration. */
12699 cp_parser_block_declaration (parser, /*statement_p=*/true);
12701 /* Free any declarators allocated. */
12702 obstack_free (&declarator_obstack, p);
12705 /* Some dependent statements (like `if (cond) statement'), are
12706 implicitly in their own scope. In other words, if the statement is
12707 a single statement (as opposed to a compound-statement), it is
12708 none-the-less treated as if it were enclosed in braces. Any
12709 declarations appearing in the dependent statement are out of scope
12710 after control passes that point. This function parses a statement,
12711 but ensures that is in its own scope, even if it is not a
12712 compound-statement.
12714 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12715 is a (possibly labeled) if statement which is not enclosed in
12716 braces and has an else clause. This is used to implement
12717 -Wparentheses.
12719 CHAIN is a vector of if-else-if conditions. This is used to implement
12720 -Wduplicated-cond.
12722 Returns the new statement. */
12724 static tree
12725 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12726 const token_indent_info &guard_tinfo,
12727 vec<tree> *chain)
12729 tree statement;
12730 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12731 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12732 token_indent_info body_tinfo
12733 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12735 if (if_p != NULL)
12736 *if_p = false;
12738 /* Mark if () ; with a special NOP_EXPR. */
12739 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12741 cp_lexer_consume_token (parser->lexer);
12742 statement = add_stmt (build_empty_stmt (body_loc));
12744 if (guard_tinfo.keyword == RID_IF
12745 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12746 warning_at (body_loc, OPT_Wempty_body,
12747 "suggest braces around empty body in an %<if%> statement");
12748 else if (guard_tinfo.keyword == RID_ELSE)
12749 warning_at (body_loc, OPT_Wempty_body,
12750 "suggest braces around empty body in an %<else%> statement");
12752 /* if a compound is opened, we simply parse the statement directly. */
12753 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12754 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12755 /* If the token is not a `{', then we must take special action. */
12756 else
12758 /* Create a compound-statement. */
12759 statement = begin_compound_stmt (0);
12760 /* Parse the dependent-statement. */
12761 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12762 &body_loc_after_labels);
12763 /* Finish the dummy compound-statement. */
12764 finish_compound_stmt (statement);
12767 token_indent_info next_tinfo
12768 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12769 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12771 if (body_loc_after_labels != UNKNOWN_LOCATION
12772 && next_tinfo.type != CPP_SEMICOLON)
12773 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12774 guard_tinfo.location, guard_tinfo.keyword);
12776 /* Return the statement. */
12777 return statement;
12780 /* For some dependent statements (like `while (cond) statement'), we
12781 have already created a scope. Therefore, even if the dependent
12782 statement is a compound-statement, we do not want to create another
12783 scope. */
12785 static void
12786 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12787 const token_indent_info &guard_tinfo)
12789 /* If the token is a `{', then we must take special action. */
12790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12792 token_indent_info body_tinfo
12793 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12794 location_t loc_after_labels = UNKNOWN_LOCATION;
12796 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12797 &loc_after_labels);
12798 token_indent_info next_tinfo
12799 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12800 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12802 if (loc_after_labels != UNKNOWN_LOCATION
12803 && next_tinfo.type != CPP_SEMICOLON)
12804 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12805 guard_tinfo.location,
12806 guard_tinfo.keyword);
12808 else
12810 /* Avoid calling cp_parser_compound_statement, so that we
12811 don't create a new scope. Do everything else by hand. */
12812 matching_braces braces;
12813 braces.require_open (parser);
12814 /* If the next keyword is `__label__' we have a label declaration. */
12815 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12816 cp_parser_label_declaration (parser);
12817 /* Parse an (optional) statement-seq. */
12818 cp_parser_statement_seq_opt (parser, NULL_TREE);
12819 braces.require_close (parser);
12823 /* Declarations [gram.dcl.dcl] */
12825 /* Parse an optional declaration-sequence.
12827 declaration-seq:
12828 declaration
12829 declaration-seq declaration */
12831 static void
12832 cp_parser_declaration_seq_opt (cp_parser* parser)
12834 while (true)
12836 cp_token *token = cp_lexer_peek_token (parser->lexer);
12838 if (token->type == CPP_CLOSE_BRACE
12839 || token->type == CPP_EOF)
12840 break;
12841 else
12842 cp_parser_toplevel_declaration (parser);
12846 /* Parse a declaration.
12848 declaration:
12849 block-declaration
12850 function-definition
12851 template-declaration
12852 explicit-instantiation
12853 explicit-specialization
12854 linkage-specification
12855 namespace-definition
12857 C++17:
12858 deduction-guide
12860 GNU extension:
12862 declaration:
12863 __extension__ declaration */
12865 static void
12866 cp_parser_declaration (cp_parser* parser)
12868 cp_token token1;
12869 cp_token token2;
12870 int saved_pedantic;
12871 void *p;
12872 tree attributes = NULL_TREE;
12874 /* Check for the `__extension__' keyword. */
12875 if (cp_parser_extension_opt (parser, &saved_pedantic))
12877 /* Parse the qualified declaration. */
12878 cp_parser_declaration (parser);
12879 /* Restore the PEDANTIC flag. */
12880 pedantic = saved_pedantic;
12882 return;
12885 /* Try to figure out what kind of declaration is present. */
12886 token1 = *cp_lexer_peek_token (parser->lexer);
12888 if (token1.type != CPP_EOF)
12889 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12890 else
12892 token2.type = CPP_EOF;
12893 token2.keyword = RID_MAX;
12896 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12897 p = obstack_alloc (&declarator_obstack, 0);
12899 /* If the next token is `extern' and the following token is a string
12900 literal, then we have a linkage specification. */
12901 if (token1.keyword == RID_EXTERN
12902 && cp_parser_is_pure_string_literal (&token2))
12903 cp_parser_linkage_specification (parser);
12904 /* If the next token is `template', then we have either a template
12905 declaration, an explicit instantiation, or an explicit
12906 specialization. */
12907 else if (token1.keyword == RID_TEMPLATE)
12909 /* `template <>' indicates a template specialization. */
12910 if (token2.type == CPP_LESS
12911 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12912 cp_parser_explicit_specialization (parser);
12913 /* `template <' indicates a template declaration. */
12914 else if (token2.type == CPP_LESS)
12915 cp_parser_template_declaration (parser, /*member_p=*/false);
12916 /* Anything else must be an explicit instantiation. */
12917 else
12918 cp_parser_explicit_instantiation (parser);
12920 /* If the next token is `export', then we have a template
12921 declaration. */
12922 else if (token1.keyword == RID_EXPORT)
12923 cp_parser_template_declaration (parser, /*member_p=*/false);
12924 /* If the next token is `extern', 'static' or 'inline' and the one
12925 after that is `template', we have a GNU extended explicit
12926 instantiation directive. */
12927 else if (cp_parser_allow_gnu_extensions_p (parser)
12928 && (token1.keyword == RID_EXTERN
12929 || token1.keyword == RID_STATIC
12930 || token1.keyword == RID_INLINE)
12931 && token2.keyword == RID_TEMPLATE)
12932 cp_parser_explicit_instantiation (parser);
12933 /* If the next token is `namespace', check for a named or unnamed
12934 namespace definition. */
12935 else if (token1.keyword == RID_NAMESPACE
12936 && (/* A named namespace definition. */
12937 (token2.type == CPP_NAME
12938 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12939 != CPP_EQ))
12940 || (token2.type == CPP_OPEN_SQUARE
12941 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12942 == CPP_OPEN_SQUARE)
12943 /* An unnamed namespace definition. */
12944 || token2.type == CPP_OPEN_BRACE
12945 || token2.keyword == RID_ATTRIBUTE))
12946 cp_parser_namespace_definition (parser);
12947 /* An inline (associated) namespace definition. */
12948 else if (token1.keyword == RID_INLINE
12949 && token2.keyword == RID_NAMESPACE)
12950 cp_parser_namespace_definition (parser);
12951 /* Objective-C++ declaration/definition. */
12952 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12953 cp_parser_objc_declaration (parser, NULL_TREE);
12954 else if (c_dialect_objc ()
12955 && token1.keyword == RID_ATTRIBUTE
12956 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12957 cp_parser_objc_declaration (parser, attributes);
12958 /* At this point we may have a template declared by a concept
12959 introduction. */
12960 else if (flag_concepts
12961 && cp_parser_template_declaration_after_export (parser,
12962 /*member_p=*/false))
12963 /* We did. */;
12964 else
12965 /* Try to parse a block-declaration, or a function-definition. */
12966 cp_parser_block_declaration (parser, /*statement_p=*/false);
12968 /* Free any declarators allocated. */
12969 obstack_free (&declarator_obstack, p);
12972 /* Parse a namespace-scope declaration. */
12974 static void
12975 cp_parser_toplevel_declaration (cp_parser* parser)
12977 cp_token *token = cp_lexer_peek_token (parser->lexer);
12979 if (token->type == CPP_PRAGMA)
12980 /* A top-level declaration can consist solely of a #pragma. A
12981 nested declaration cannot, so this is done here and not in
12982 cp_parser_declaration. (A #pragma at block scope is
12983 handled in cp_parser_statement.) */
12984 cp_parser_pragma (parser, pragma_external, NULL);
12985 else if (token->type == CPP_SEMICOLON)
12987 /* A declaration consisting of a single semicolon is
12988 invalid. Allow it unless we're being pedantic. */
12989 cp_lexer_consume_token (parser->lexer);
12990 if (!in_system_header_at (input_location))
12991 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12993 else
12994 /* Parse the declaration itself. */
12995 cp_parser_declaration (parser);
12998 /* Parse a block-declaration.
13000 block-declaration:
13001 simple-declaration
13002 asm-definition
13003 namespace-alias-definition
13004 using-declaration
13005 using-directive
13007 GNU Extension:
13009 block-declaration:
13010 __extension__ block-declaration
13012 C++0x Extension:
13014 block-declaration:
13015 static_assert-declaration
13017 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13018 part of a declaration-statement. */
13020 static void
13021 cp_parser_block_declaration (cp_parser *parser,
13022 bool statement_p)
13024 cp_token *token1;
13025 int saved_pedantic;
13027 /* Check for the `__extension__' keyword. */
13028 if (cp_parser_extension_opt (parser, &saved_pedantic))
13030 /* Parse the qualified declaration. */
13031 cp_parser_block_declaration (parser, statement_p);
13032 /* Restore the PEDANTIC flag. */
13033 pedantic = saved_pedantic;
13035 return;
13038 /* Peek at the next token to figure out which kind of declaration is
13039 present. */
13040 token1 = cp_lexer_peek_token (parser->lexer);
13042 /* If the next keyword is `asm', we have an asm-definition. */
13043 if (token1->keyword == RID_ASM)
13045 if (statement_p)
13046 cp_parser_commit_to_tentative_parse (parser);
13047 cp_parser_asm_definition (parser);
13049 /* If the next keyword is `namespace', we have a
13050 namespace-alias-definition. */
13051 else if (token1->keyword == RID_NAMESPACE)
13052 cp_parser_namespace_alias_definition (parser);
13053 /* If the next keyword is `using', we have a
13054 using-declaration, a using-directive, or an alias-declaration. */
13055 else if (token1->keyword == RID_USING)
13057 cp_token *token2;
13059 if (statement_p)
13060 cp_parser_commit_to_tentative_parse (parser);
13061 /* If the token after `using' is `namespace', then we have a
13062 using-directive. */
13063 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13064 if (token2->keyword == RID_NAMESPACE)
13065 cp_parser_using_directive (parser);
13066 /* If the second token after 'using' is '=', then we have an
13067 alias-declaration. */
13068 else if (cxx_dialect >= cxx11
13069 && token2->type == CPP_NAME
13070 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13071 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13072 cp_parser_alias_declaration (parser);
13073 /* Otherwise, it's a using-declaration. */
13074 else
13075 cp_parser_using_declaration (parser,
13076 /*access_declaration_p=*/false);
13078 /* If the next keyword is `__label__' we have a misplaced label
13079 declaration. */
13080 else if (token1->keyword == RID_LABEL)
13082 cp_lexer_consume_token (parser->lexer);
13083 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13084 cp_parser_skip_to_end_of_statement (parser);
13085 /* If the next token is now a `;', consume it. */
13086 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13087 cp_lexer_consume_token (parser->lexer);
13089 /* If the next token is `static_assert' we have a static assertion. */
13090 else if (token1->keyword == RID_STATIC_ASSERT)
13091 cp_parser_static_assert (parser, /*member_p=*/false);
13092 /* Anything else must be a simple-declaration. */
13093 else
13094 cp_parser_simple_declaration (parser, !statement_p,
13095 /*maybe_range_for_decl*/NULL);
13098 /* Parse a simple-declaration.
13100 simple-declaration:
13101 decl-specifier-seq [opt] init-declarator-list [opt] ;
13102 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13103 brace-or-equal-initializer ;
13105 init-declarator-list:
13106 init-declarator
13107 init-declarator-list , init-declarator
13109 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13110 function-definition as a simple-declaration.
13112 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13113 parsed declaration if it is an uninitialized single declarator not followed
13114 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13115 if present, will not be consumed. */
13117 static void
13118 cp_parser_simple_declaration (cp_parser* parser,
13119 bool function_definition_allowed_p,
13120 tree *maybe_range_for_decl)
13122 cp_decl_specifier_seq decl_specifiers;
13123 int declares_class_or_enum;
13124 bool saw_declarator;
13125 location_t comma_loc = UNKNOWN_LOCATION;
13126 location_t init_loc = UNKNOWN_LOCATION;
13128 if (maybe_range_for_decl)
13129 *maybe_range_for_decl = NULL_TREE;
13131 /* Defer access checks until we know what is being declared; the
13132 checks for names appearing in the decl-specifier-seq should be
13133 done as if we were in the scope of the thing being declared. */
13134 push_deferring_access_checks (dk_deferred);
13136 /* Parse the decl-specifier-seq. We have to keep track of whether
13137 or not the decl-specifier-seq declares a named class or
13138 enumeration type, since that is the only case in which the
13139 init-declarator-list is allowed to be empty.
13141 [dcl.dcl]
13143 In a simple-declaration, the optional init-declarator-list can be
13144 omitted only when declaring a class or enumeration, that is when
13145 the decl-specifier-seq contains either a class-specifier, an
13146 elaborated-type-specifier, or an enum-specifier. */
13147 cp_parser_decl_specifier_seq (parser,
13148 CP_PARSER_FLAGS_OPTIONAL,
13149 &decl_specifiers,
13150 &declares_class_or_enum);
13151 /* We no longer need to defer access checks. */
13152 stop_deferring_access_checks ();
13154 /* In a block scope, a valid declaration must always have a
13155 decl-specifier-seq. By not trying to parse declarators, we can
13156 resolve the declaration/expression ambiguity more quickly. */
13157 if (!function_definition_allowed_p
13158 && !decl_specifiers.any_specifiers_p)
13160 cp_parser_error (parser, "expected declaration");
13161 goto done;
13164 /* If the next two tokens are both identifiers, the code is
13165 erroneous. The usual cause of this situation is code like:
13167 T t;
13169 where "T" should name a type -- but does not. */
13170 if (!decl_specifiers.any_type_specifiers_p
13171 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13173 /* If parsing tentatively, we should commit; we really are
13174 looking at a declaration. */
13175 cp_parser_commit_to_tentative_parse (parser);
13176 /* Give up. */
13177 goto done;
13180 cp_parser_maybe_commit_to_declaration (parser,
13181 decl_specifiers.any_specifiers_p);
13183 /* Look for C++17 decomposition declaration. */
13184 for (size_t n = 1; ; n++)
13185 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13186 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13187 continue;
13188 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13189 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13190 && decl_specifiers.any_specifiers_p)
13192 tree decl
13193 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13194 maybe_range_for_decl,
13195 &init_loc);
13197 /* The next token should be either a `,' or a `;'. */
13198 cp_token *token = cp_lexer_peek_token (parser->lexer);
13199 /* If it's a `;', we are done. */
13200 if (token->type == CPP_SEMICOLON)
13201 goto finish;
13202 else if (maybe_range_for_decl)
13204 if (*maybe_range_for_decl == NULL_TREE)
13205 *maybe_range_for_decl = error_mark_node;
13206 goto finish;
13208 /* Anything else is an error. */
13209 else
13211 /* If we have already issued an error message we don't need
13212 to issue another one. */
13213 if ((decl != error_mark_node
13214 && DECL_INITIAL (decl) != error_mark_node)
13215 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13216 cp_parser_error (parser, "expected %<,%> or %<;%>");
13217 /* Skip tokens until we reach the end of the statement. */
13218 cp_parser_skip_to_end_of_statement (parser);
13219 /* If the next token is now a `;', consume it. */
13220 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13221 cp_lexer_consume_token (parser->lexer);
13222 goto done;
13225 else
13226 break;
13228 tree last_type;
13229 bool auto_specifier_p;
13230 /* NULL_TREE if both variable and function declaration are allowed,
13231 error_mark_node if function declaration are not allowed and
13232 a FUNCTION_DECL that should be diagnosed if it is followed by
13233 variable declarations. */
13234 tree auto_function_declaration;
13236 last_type = NULL_TREE;
13237 auto_specifier_p
13238 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13239 auto_function_declaration = NULL_TREE;
13241 /* Keep going until we hit the `;' at the end of the simple
13242 declaration. */
13243 saw_declarator = false;
13244 while (cp_lexer_next_token_is_not (parser->lexer,
13245 CPP_SEMICOLON))
13247 cp_token *token;
13248 bool function_definition_p;
13249 tree decl;
13250 tree auto_result = NULL_TREE;
13252 if (saw_declarator)
13254 /* If we are processing next declarator, comma is expected */
13255 token = cp_lexer_peek_token (parser->lexer);
13256 gcc_assert (token->type == CPP_COMMA);
13257 cp_lexer_consume_token (parser->lexer);
13258 if (maybe_range_for_decl)
13260 *maybe_range_for_decl = error_mark_node;
13261 if (comma_loc == UNKNOWN_LOCATION)
13262 comma_loc = token->location;
13265 else
13266 saw_declarator = true;
13268 /* Parse the init-declarator. */
13269 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13270 /*checks=*/NULL,
13271 function_definition_allowed_p,
13272 /*member_p=*/false,
13273 declares_class_or_enum,
13274 &function_definition_p,
13275 maybe_range_for_decl,
13276 &init_loc,
13277 &auto_result);
13278 /* If an error occurred while parsing tentatively, exit quickly.
13279 (That usually happens when in the body of a function; each
13280 statement is treated as a declaration-statement until proven
13281 otherwise.) */
13282 if (cp_parser_error_occurred (parser))
13283 goto done;
13285 if (auto_specifier_p && cxx_dialect >= cxx14)
13287 /* If the init-declarator-list contains more than one
13288 init-declarator, they shall all form declarations of
13289 variables. */
13290 if (auto_function_declaration == NULL_TREE)
13291 auto_function_declaration
13292 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13293 else if (TREE_CODE (decl) == FUNCTION_DECL
13294 || auto_function_declaration != error_mark_node)
13296 error_at (decl_specifiers.locations[ds_type_spec],
13297 "non-variable %qD in declaration with more than one "
13298 "declarator with placeholder type",
13299 TREE_CODE (decl) == FUNCTION_DECL
13300 ? decl : auto_function_declaration);
13301 auto_function_declaration = error_mark_node;
13305 if (auto_result
13306 && (!processing_template_decl || !type_uses_auto (auto_result)))
13308 if (last_type
13309 && last_type != error_mark_node
13310 && !same_type_p (auto_result, last_type))
13312 /* If the list of declarators contains more than one declarator,
13313 the type of each declared variable is determined as described
13314 above. If the type deduced for the template parameter U is not
13315 the same in each deduction, the program is ill-formed. */
13316 error_at (decl_specifiers.locations[ds_type_spec],
13317 "inconsistent deduction for %qT: %qT and then %qT",
13318 decl_specifiers.type, last_type, auto_result);
13319 last_type = error_mark_node;
13321 else
13322 last_type = auto_result;
13325 /* Handle function definitions specially. */
13326 if (function_definition_p)
13328 /* If the next token is a `,', then we are probably
13329 processing something like:
13331 void f() {}, *p;
13333 which is erroneous. */
13334 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13336 cp_token *token = cp_lexer_peek_token (parser->lexer);
13337 error_at (token->location,
13338 "mixing"
13339 " declarations and function-definitions is forbidden");
13341 /* Otherwise, we're done with the list of declarators. */
13342 else
13344 pop_deferring_access_checks ();
13345 return;
13348 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13349 *maybe_range_for_decl = decl;
13350 /* The next token should be either a `,' or a `;'. */
13351 token = cp_lexer_peek_token (parser->lexer);
13352 /* If it's a `,', there are more declarators to come. */
13353 if (token->type == CPP_COMMA)
13354 /* will be consumed next time around */;
13355 /* If it's a `;', we are done. */
13356 else if (token->type == CPP_SEMICOLON)
13357 break;
13358 else if (maybe_range_for_decl)
13360 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13361 permerror (decl_specifiers.locations[ds_type_spec],
13362 "types may not be defined in a for-range-declaration");
13363 break;
13365 /* Anything else is an error. */
13366 else
13368 /* If we have already issued an error message we don't need
13369 to issue another one. */
13370 if ((decl != error_mark_node
13371 && DECL_INITIAL (decl) != error_mark_node)
13372 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13373 cp_parser_error (parser, "expected %<,%> or %<;%>");
13374 /* Skip tokens until we reach the end of the statement. */
13375 cp_parser_skip_to_end_of_statement (parser);
13376 /* If the next token is now a `;', consume it. */
13377 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13378 cp_lexer_consume_token (parser->lexer);
13379 goto done;
13381 /* After the first time around, a function-definition is not
13382 allowed -- even if it was OK at first. For example:
13384 int i, f() {}
13386 is not valid. */
13387 function_definition_allowed_p = false;
13390 /* Issue an error message if no declarators are present, and the
13391 decl-specifier-seq does not itself declare a class or
13392 enumeration: [dcl.dcl]/3. */
13393 if (!saw_declarator)
13395 if (cp_parser_declares_only_class_p (parser))
13397 if (!declares_class_or_enum
13398 && decl_specifiers.type
13399 && OVERLOAD_TYPE_P (decl_specifiers.type))
13400 /* Ensure an error is issued anyway when finish_decltype_type,
13401 called via cp_parser_decl_specifier_seq, returns a class or
13402 an enumeration (c++/51786). */
13403 decl_specifiers.type = NULL_TREE;
13404 shadow_tag (&decl_specifiers);
13406 /* Perform any deferred access checks. */
13407 perform_deferred_access_checks (tf_warning_or_error);
13410 /* Consume the `;'. */
13411 finish:
13412 if (!maybe_range_for_decl)
13413 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13414 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13416 if (init_loc != UNKNOWN_LOCATION)
13417 error_at (init_loc, "initializer in range-based %<for%> loop");
13418 if (comma_loc != UNKNOWN_LOCATION)
13419 error_at (comma_loc,
13420 "multiple declarations in range-based %<for%> loop");
13423 done:
13424 pop_deferring_access_checks ();
13427 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13428 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13429 initializer ; */
13431 static tree
13432 cp_parser_decomposition_declaration (cp_parser *parser,
13433 cp_decl_specifier_seq *decl_specifiers,
13434 tree *maybe_range_for_decl,
13435 location_t *init_loc)
13437 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13438 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13439 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13441 /* Parse the identifier-list. */
13442 auto_vec<cp_expr, 10> v;
13443 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13444 while (true)
13446 cp_expr e = cp_parser_identifier (parser);
13447 if (e.get_value () == error_mark_node)
13448 break;
13449 v.safe_push (e);
13450 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13451 break;
13452 cp_lexer_consume_token (parser->lexer);
13455 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13456 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13458 end_loc = UNKNOWN_LOCATION;
13459 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13460 false);
13461 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13462 cp_lexer_consume_token (parser->lexer);
13463 else
13465 cp_parser_skip_to_end_of_statement (parser);
13466 return error_mark_node;
13470 if (cxx_dialect < cxx17)
13471 pedwarn (loc, 0, "structured bindings only available with "
13472 "-std=c++17 or -std=gnu++17");
13474 tree pushed_scope;
13475 cp_declarator *declarator = make_declarator (cdk_decomp);
13476 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13477 declarator->id_loc = loc;
13478 if (ref_qual != REF_QUAL_NONE)
13479 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13480 ref_qual == REF_QUAL_RVALUE,
13481 NULL_TREE);
13482 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13483 NULL_TREE, decl_specifiers->attributes,
13484 &pushed_scope);
13485 tree orig_decl = decl;
13487 unsigned int i;
13488 cp_expr e;
13489 cp_decl_specifier_seq decl_specs;
13490 clear_decl_specs (&decl_specs);
13491 decl_specs.type = make_auto ();
13492 tree prev = decl;
13493 FOR_EACH_VEC_ELT (v, i, e)
13495 if (i == 0)
13496 declarator = make_id_declarator (NULL_TREE, e.get_value (),
13497 sfk_none, e.get_location ());
13498 else
13500 declarator->u.id.unqualified_name = e.get_value ();
13501 declarator->id_loc = e.get_location ();
13503 tree elt_pushed_scope;
13504 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13505 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13506 if (decl2 == error_mark_node)
13507 decl = error_mark_node;
13508 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13510 /* Ensure we've diagnosed redeclaration if we aren't creating
13511 a new VAR_DECL. */
13512 gcc_assert (errorcount);
13513 decl = error_mark_node;
13515 else
13516 prev = decl2;
13517 if (elt_pushed_scope)
13518 pop_scope (elt_pushed_scope);
13521 if (v.is_empty ())
13523 error_at (loc, "empty structured binding declaration");
13524 decl = error_mark_node;
13527 if (maybe_range_for_decl == NULL
13528 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13530 bool non_constant_p = false, is_direct_init = false;
13531 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13532 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13533 &non_constant_p);
13534 if (initializer == NULL_TREE
13535 || (TREE_CODE (initializer) == TREE_LIST
13536 && TREE_CHAIN (initializer))
13537 || (is_direct_init
13538 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13539 && CONSTRUCTOR_NELTS (initializer) != 1))
13541 error_at (loc, "invalid initializer for structured binding "
13542 "declaration");
13543 initializer = error_mark_node;
13546 if (decl != error_mark_node)
13548 cp_maybe_mangle_decomp (decl, prev, v.length ());
13549 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13550 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13551 cp_finish_decomp (decl, prev, v.length ());
13554 else if (decl != error_mark_node)
13556 *maybe_range_for_decl = prev;
13557 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13558 the underlying DECL. */
13559 cp_finish_decomp (decl, prev, v.length ());
13562 if (pushed_scope)
13563 pop_scope (pushed_scope);
13565 if (decl == error_mark_node && DECL_P (orig_decl))
13567 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13568 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13571 return decl;
13574 /* Parse a decl-specifier-seq.
13576 decl-specifier-seq:
13577 decl-specifier-seq [opt] decl-specifier
13578 decl-specifier attribute-specifier-seq [opt] (C++11)
13580 decl-specifier:
13581 storage-class-specifier
13582 type-specifier
13583 function-specifier
13584 friend
13585 typedef
13587 GNU Extension:
13589 decl-specifier:
13590 attributes
13592 Concepts Extension:
13594 decl-specifier:
13595 concept
13597 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13599 The parser flags FLAGS is used to control type-specifier parsing.
13601 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13602 flags:
13604 1: one of the decl-specifiers is an elaborated-type-specifier
13605 (i.e., a type declaration)
13606 2: one of the decl-specifiers is an enum-specifier or a
13607 class-specifier (i.e., a type definition)
13611 static void
13612 cp_parser_decl_specifier_seq (cp_parser* parser,
13613 cp_parser_flags flags,
13614 cp_decl_specifier_seq *decl_specs,
13615 int* declares_class_or_enum)
13617 bool constructor_possible_p = !parser->in_declarator_p;
13618 bool found_decl_spec = false;
13619 cp_token *start_token = NULL;
13620 cp_decl_spec ds;
13622 /* Clear DECL_SPECS. */
13623 clear_decl_specs (decl_specs);
13625 /* Assume no class or enumeration type is declared. */
13626 *declares_class_or_enum = 0;
13628 /* Keep reading specifiers until there are no more to read. */
13629 while (true)
13631 bool constructor_p;
13632 cp_token *token;
13633 ds = ds_last;
13635 /* Peek at the next token. */
13636 token = cp_lexer_peek_token (parser->lexer);
13638 /* Save the first token of the decl spec list for error
13639 reporting. */
13640 if (!start_token)
13641 start_token = token;
13642 /* Handle attributes. */
13643 if (cp_next_tokens_can_be_attribute_p (parser))
13645 /* Parse the attributes. */
13646 tree attrs = cp_parser_attributes_opt (parser);
13648 /* In a sequence of declaration specifiers, c++11 attributes
13649 appertain to the type that precede them. In that case
13650 [dcl.spec]/1 says:
13652 The attribute-specifier-seq affects the type only for
13653 the declaration it appears in, not other declarations
13654 involving the same type.
13656 But for now let's force the user to position the
13657 attribute either at the beginning of the declaration or
13658 after the declarator-id, which would clearly mean that it
13659 applies to the declarator. */
13660 if (cxx11_attribute_p (attrs))
13662 if (!found_decl_spec)
13663 /* The c++11 attribute is at the beginning of the
13664 declaration. It appertains to the entity being
13665 declared. */;
13666 else
13668 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13670 /* This is an attribute following a
13671 class-specifier. */
13672 if (decl_specs->type_definition_p)
13673 warn_misplaced_attr_for_class_type (token->location,
13674 decl_specs->type);
13675 attrs = NULL_TREE;
13677 else
13679 decl_specs->std_attributes
13680 = attr_chainon (decl_specs->std_attributes, attrs);
13681 if (decl_specs->locations[ds_std_attribute] == 0)
13682 decl_specs->locations[ds_std_attribute] = token->location;
13684 continue;
13688 decl_specs->attributes
13689 = attr_chainon (decl_specs->attributes, attrs);
13690 if (decl_specs->locations[ds_attribute] == 0)
13691 decl_specs->locations[ds_attribute] = token->location;
13692 continue;
13694 /* Assume we will find a decl-specifier keyword. */
13695 found_decl_spec = true;
13696 /* If the next token is an appropriate keyword, we can simply
13697 add it to the list. */
13698 switch (token->keyword)
13700 /* decl-specifier:
13701 friend
13702 constexpr */
13703 case RID_FRIEND:
13704 if (!at_class_scope_p ())
13706 gcc_rich_location richloc (token->location);
13707 richloc.add_fixit_remove ();
13708 error_at (&richloc, "%<friend%> used outside of class");
13709 cp_lexer_purge_token (parser->lexer);
13711 else
13713 ds = ds_friend;
13714 /* Consume the token. */
13715 cp_lexer_consume_token (parser->lexer);
13717 break;
13719 case RID_CONSTEXPR:
13720 ds = ds_constexpr;
13721 cp_lexer_consume_token (parser->lexer);
13722 break;
13724 case RID_CONCEPT:
13725 ds = ds_concept;
13726 cp_lexer_consume_token (parser->lexer);
13727 break;
13729 /* function-specifier:
13730 inline
13731 virtual
13732 explicit */
13733 case RID_INLINE:
13734 case RID_VIRTUAL:
13735 case RID_EXPLICIT:
13736 cp_parser_function_specifier_opt (parser, decl_specs);
13737 break;
13739 /* decl-specifier:
13740 typedef */
13741 case RID_TYPEDEF:
13742 ds = ds_typedef;
13743 /* Consume the token. */
13744 cp_lexer_consume_token (parser->lexer);
13745 /* A constructor declarator cannot appear in a typedef. */
13746 constructor_possible_p = false;
13747 /* The "typedef" keyword can only occur in a declaration; we
13748 may as well commit at this point. */
13749 cp_parser_commit_to_tentative_parse (parser);
13751 if (decl_specs->storage_class != sc_none)
13752 decl_specs->conflicting_specifiers_p = true;
13753 break;
13755 /* storage-class-specifier:
13756 auto
13757 register
13758 static
13759 extern
13760 mutable
13762 GNU Extension:
13763 thread */
13764 case RID_AUTO:
13765 if (cxx_dialect == cxx98)
13767 /* Consume the token. */
13768 cp_lexer_consume_token (parser->lexer);
13770 /* Complain about `auto' as a storage specifier, if
13771 we're complaining about C++0x compatibility. */
13772 gcc_rich_location richloc (token->location);
13773 richloc.add_fixit_remove ();
13774 warning_at (&richloc, OPT_Wc__11_compat,
13775 "%<auto%> changes meaning in C++11; "
13776 "please remove it");
13778 /* Set the storage class anyway. */
13779 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13780 token);
13782 else
13783 /* C++0x auto type-specifier. */
13784 found_decl_spec = false;
13785 break;
13787 case RID_REGISTER:
13788 case RID_STATIC:
13789 case RID_EXTERN:
13790 case RID_MUTABLE:
13791 /* Consume the token. */
13792 cp_lexer_consume_token (parser->lexer);
13793 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13794 token);
13795 break;
13796 case RID_THREAD:
13797 /* Consume the token. */
13798 ds = ds_thread;
13799 cp_lexer_consume_token (parser->lexer);
13800 break;
13802 default:
13803 /* We did not yet find a decl-specifier yet. */
13804 found_decl_spec = false;
13805 break;
13808 if (found_decl_spec
13809 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13810 && token->keyword != RID_CONSTEXPR)
13811 error ("decl-specifier invalid in condition");
13813 if (found_decl_spec
13814 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13815 && token->keyword != RID_MUTABLE
13816 && token->keyword != RID_CONSTEXPR)
13817 error_at (token->location, "%qD invalid in lambda",
13818 ridpointers[token->keyword]);
13820 if (ds != ds_last)
13821 set_and_check_decl_spec_loc (decl_specs, ds, token);
13823 /* Constructors are a special case. The `S' in `S()' is not a
13824 decl-specifier; it is the beginning of the declarator. */
13825 constructor_p
13826 = (!found_decl_spec
13827 && constructor_possible_p
13828 && (cp_parser_constructor_declarator_p
13829 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13831 /* If we don't have a DECL_SPEC yet, then we must be looking at
13832 a type-specifier. */
13833 if (!found_decl_spec && !constructor_p)
13835 int decl_spec_declares_class_or_enum;
13836 bool is_cv_qualifier;
13837 tree type_spec;
13839 type_spec
13840 = cp_parser_type_specifier (parser, flags,
13841 decl_specs,
13842 /*is_declaration=*/true,
13843 &decl_spec_declares_class_or_enum,
13844 &is_cv_qualifier);
13845 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13847 /* If this type-specifier referenced a user-defined type
13848 (a typedef, class-name, etc.), then we can't allow any
13849 more such type-specifiers henceforth.
13851 [dcl.spec]
13853 The longest sequence of decl-specifiers that could
13854 possibly be a type name is taken as the
13855 decl-specifier-seq of a declaration. The sequence shall
13856 be self-consistent as described below.
13858 [dcl.type]
13860 As a general rule, at most one type-specifier is allowed
13861 in the complete decl-specifier-seq of a declaration. The
13862 only exceptions are the following:
13864 -- const or volatile can be combined with any other
13865 type-specifier.
13867 -- signed or unsigned can be combined with char, long,
13868 short, or int.
13870 -- ..
13872 Example:
13874 typedef char* Pc;
13875 void g (const int Pc);
13877 Here, Pc is *not* part of the decl-specifier seq; it's
13878 the declarator. Therefore, once we see a type-specifier
13879 (other than a cv-qualifier), we forbid any additional
13880 user-defined types. We *do* still allow things like `int
13881 int' to be considered a decl-specifier-seq, and issue the
13882 error message later. */
13883 if (type_spec && !is_cv_qualifier)
13884 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13885 /* A constructor declarator cannot follow a type-specifier. */
13886 if (type_spec)
13888 constructor_possible_p = false;
13889 found_decl_spec = true;
13890 if (!is_cv_qualifier)
13891 decl_specs->any_type_specifiers_p = true;
13893 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
13894 error_at (token->location, "type-specifier invalid in lambda");
13898 /* If we still do not have a DECL_SPEC, then there are no more
13899 decl-specifiers. */
13900 if (!found_decl_spec)
13901 break;
13903 decl_specs->any_specifiers_p = true;
13904 /* After we see one decl-specifier, further decl-specifiers are
13905 always optional. */
13906 flags |= CP_PARSER_FLAGS_OPTIONAL;
13909 /* Don't allow a friend specifier with a class definition. */
13910 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13911 && (*declares_class_or_enum & 2))
13912 error_at (decl_specs->locations[ds_friend],
13913 "class definition may not be declared a friend");
13916 /* Parse an (optional) storage-class-specifier.
13918 storage-class-specifier:
13919 auto
13920 register
13921 static
13922 extern
13923 mutable
13925 GNU Extension:
13927 storage-class-specifier:
13928 thread
13930 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13932 static tree
13933 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13935 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13937 case RID_AUTO:
13938 if (cxx_dialect != cxx98)
13939 return NULL_TREE;
13940 /* Fall through for C++98. */
13941 gcc_fallthrough ();
13943 case RID_REGISTER:
13944 case RID_STATIC:
13945 case RID_EXTERN:
13946 case RID_MUTABLE:
13947 case RID_THREAD:
13948 /* Consume the token. */
13949 return cp_lexer_consume_token (parser->lexer)->u.value;
13951 default:
13952 return NULL_TREE;
13956 /* Parse an (optional) function-specifier.
13958 function-specifier:
13959 inline
13960 virtual
13961 explicit
13963 C++2A Extension:
13964 explicit(constant-expression)
13966 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13967 Updates DECL_SPECS, if it is non-NULL. */
13969 static tree
13970 cp_parser_function_specifier_opt (cp_parser* parser,
13971 cp_decl_specifier_seq *decl_specs)
13973 cp_token *token = cp_lexer_peek_token (parser->lexer);
13974 switch (token->keyword)
13976 case RID_INLINE:
13977 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13978 break;
13980 case RID_VIRTUAL:
13981 /* 14.5.2.3 [temp.mem]
13983 A member function template shall not be virtual. */
13984 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13985 && current_class_type)
13986 error_at (token->location, "templates may not be %<virtual%>");
13987 else
13988 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13989 break;
13991 case RID_EXPLICIT:
13993 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
13994 /* If we see '(', it's C++20 explicit(bool). */
13995 tree expr;
13996 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13998 matching_parens parens;
13999 parens.consume_open (parser);
14001 /* New types are not allowed in an explicit-specifier. */
14002 const char *saved_message
14003 = parser->type_definition_forbidden_message;
14004 parser->type_definition_forbidden_message
14005 = G_("types may not be defined in explicit-specifier");
14007 if (cxx_dialect < cxx2a)
14008 pedwarn (token->location, 0,
14009 "%<explicit(bool)%> only available with -std=c++2a "
14010 "or -std=gnu++2a");
14012 /* Parse the constant-expression. */
14013 expr = cp_parser_constant_expression (parser);
14015 /* Restore the saved message. */
14016 parser->type_definition_forbidden_message = saved_message;
14017 parens.require_close (parser);
14019 else
14020 /* The explicit-specifier explicit without a constant-expression is
14021 equivalent to the explicit-specifier explicit(true). */
14022 expr = boolean_true_node;
14024 /* [dcl.fct.spec]
14025 "the constant-expression, if supplied, shall be a contextually
14026 converted constant expression of type bool." */
14027 expr = build_explicit_specifier (expr, tf_warning_or_error);
14028 /* We could evaluate it -- mark the decl as appropriate. */
14029 if (expr == boolean_true_node)
14030 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14031 else if (expr == boolean_false_node)
14032 /* Don't mark the decl as explicit. */;
14033 else if (decl_specs)
14034 /* The expression was value-dependent. Remember it so that we can
14035 substitute it later. */
14036 decl_specs->explicit_specifier = expr;
14037 return id;
14040 default:
14041 return NULL_TREE;
14044 /* Consume the token. */
14045 return cp_lexer_consume_token (parser->lexer)->u.value;
14048 /* Parse a linkage-specification.
14050 linkage-specification:
14051 extern string-literal { declaration-seq [opt] }
14052 extern string-literal declaration */
14054 static void
14055 cp_parser_linkage_specification (cp_parser* parser)
14057 tree linkage;
14059 /* Look for the `extern' keyword. */
14060 cp_token *extern_token
14061 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14063 /* Look for the string-literal. */
14064 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14065 linkage = cp_parser_string_literal (parser, false, false);
14067 /* Transform the literal into an identifier. If the literal is a
14068 wide-character string, or contains embedded NULs, then we can't
14069 handle it as the user wants. */
14070 if (strlen (TREE_STRING_POINTER (linkage))
14071 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14073 cp_parser_error (parser, "invalid linkage-specification");
14074 /* Assume C++ linkage. */
14075 linkage = lang_name_cplusplus;
14077 else
14078 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14080 /* We're now using the new linkage. */
14081 push_lang_context (linkage);
14083 /* Preserve the location of the the innermost linkage specification,
14084 tracking the locations of nested specifications via a local. */
14085 location_t saved_location
14086 = parser->innermost_linkage_specification_location;
14087 /* Construct a location ranging from the start of the "extern" to
14088 the end of the string-literal, with the caret at the start, e.g.:
14089 extern "C" {
14090 ^~~~~~~~~~
14092 parser->innermost_linkage_specification_location
14093 = make_location (extern_token->location,
14094 extern_token->location,
14095 get_finish (string_token->location));
14097 /* If the next token is a `{', then we're using the first
14098 production. */
14099 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14101 cp_ensure_no_omp_declare_simd (parser);
14102 cp_ensure_no_oacc_routine (parser);
14104 /* Consume the `{' token. */
14105 matching_braces braces;
14106 braces.consume_open (parser)->location;
14107 /* Parse the declarations. */
14108 cp_parser_declaration_seq_opt (parser);
14109 /* Look for the closing `}'. */
14110 braces.require_close (parser);
14112 /* Otherwise, there's just one declaration. */
14113 else
14115 bool saved_in_unbraced_linkage_specification_p;
14117 saved_in_unbraced_linkage_specification_p
14118 = parser->in_unbraced_linkage_specification_p;
14119 parser->in_unbraced_linkage_specification_p = true;
14120 cp_parser_declaration (parser);
14121 parser->in_unbraced_linkage_specification_p
14122 = saved_in_unbraced_linkage_specification_p;
14125 /* We're done with the linkage-specification. */
14126 pop_lang_context ();
14128 /* Restore location of parent linkage specification, if any. */
14129 parser->innermost_linkage_specification_location = saved_location;
14132 /* Parse a static_assert-declaration.
14134 static_assert-declaration:
14135 static_assert ( constant-expression , string-literal ) ;
14136 static_assert ( constant-expression ) ; (C++17)
14138 If MEMBER_P, this static_assert is a class member. */
14140 static void
14141 cp_parser_static_assert(cp_parser *parser, bool member_p)
14143 cp_expr condition;
14144 location_t token_loc;
14145 tree message;
14146 bool dummy;
14148 /* Peek at the `static_assert' token so we can keep track of exactly
14149 where the static assertion started. */
14150 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14152 /* Look for the `static_assert' keyword. */
14153 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14154 RT_STATIC_ASSERT))
14155 return;
14157 /* We know we are in a static assertion; commit to any tentative
14158 parse. */
14159 if (cp_parser_parsing_tentatively (parser))
14160 cp_parser_commit_to_tentative_parse (parser);
14162 /* Parse the `(' starting the static assertion condition. */
14163 matching_parens parens;
14164 parens.require_open (parser);
14166 /* Parse the constant-expression. Allow a non-constant expression
14167 here in order to give better diagnostics in finish_static_assert. */
14168 condition =
14169 cp_parser_constant_expression (parser,
14170 /*allow_non_constant_p=*/true,
14171 /*non_constant_p=*/&dummy);
14173 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14175 if (cxx_dialect < cxx17)
14176 pedwarn (input_location, OPT_Wpedantic,
14177 "static_assert without a message "
14178 "only available with -std=c++17 or -std=gnu++17");
14179 /* Eat the ')' */
14180 cp_lexer_consume_token (parser->lexer);
14181 message = build_string (1, "");
14182 TREE_TYPE (message) = char_array_type_node;
14183 fix_string_type (message);
14185 else
14187 /* Parse the separating `,'. */
14188 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14190 /* Parse the string-literal message. */
14191 message = cp_parser_string_literal (parser,
14192 /*translate=*/false,
14193 /*wide_ok=*/true);
14195 /* A `)' completes the static assertion. */
14196 if (!parens.require_close (parser))
14197 cp_parser_skip_to_closing_parenthesis (parser,
14198 /*recovering=*/true,
14199 /*or_comma=*/false,
14200 /*consume_paren=*/true);
14203 /* A semicolon terminates the declaration. */
14204 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14206 /* Get the location for the static assertion. Use that of the
14207 condition if available, otherwise, use that of the "static_assert"
14208 token. */
14209 location_t assert_loc = condition.get_location ();
14210 if (assert_loc == UNKNOWN_LOCATION)
14211 assert_loc = token_loc;
14213 /* Complete the static assertion, which may mean either processing
14214 the static assert now or saving it for template instantiation. */
14215 finish_static_assert (condition, message, assert_loc, member_p);
14218 /* Parse the expression in decltype ( expression ). */
14220 static tree
14221 cp_parser_decltype_expr (cp_parser *parser,
14222 bool &id_expression_or_member_access_p)
14224 cp_token *id_expr_start_token;
14225 tree expr;
14227 /* Since we're going to preserve any side-effects from this parse, set up a
14228 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14229 in the expression. */
14230 tentative_firewall firewall (parser);
14232 /* First, try parsing an id-expression. */
14233 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14234 cp_parser_parse_tentatively (parser);
14235 expr = cp_parser_id_expression (parser,
14236 /*template_keyword_p=*/false,
14237 /*check_dependency_p=*/true,
14238 /*template_p=*/NULL,
14239 /*declarator_p=*/false,
14240 /*optional_p=*/false);
14242 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14244 bool non_integral_constant_expression_p = false;
14245 tree id_expression = expr;
14246 cp_id_kind idk;
14247 const char *error_msg;
14249 if (identifier_p (expr))
14250 /* Lookup the name we got back from the id-expression. */
14251 expr = cp_parser_lookup_name_simple (parser, expr,
14252 id_expr_start_token->location);
14254 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14255 /* A template without args is not a complete id-expression. */
14256 expr = error_mark_node;
14258 if (expr
14259 && expr != error_mark_node
14260 && TREE_CODE (expr) != TYPE_DECL
14261 && (TREE_CODE (expr) != BIT_NOT_EXPR
14262 || !TYPE_P (TREE_OPERAND (expr, 0)))
14263 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14265 /* Complete lookup of the id-expression. */
14266 expr = (finish_id_expression
14267 (id_expression, expr, parser->scope, &idk,
14268 /*integral_constant_expression_p=*/false,
14269 /*allow_non_integral_constant_expression_p=*/true,
14270 &non_integral_constant_expression_p,
14271 /*template_p=*/false,
14272 /*done=*/true,
14273 /*address_p=*/false,
14274 /*template_arg_p=*/false,
14275 &error_msg,
14276 id_expr_start_token->location));
14278 if (expr == error_mark_node)
14279 /* We found an id-expression, but it was something that we
14280 should not have found. This is an error, not something
14281 we can recover from, so note that we found an
14282 id-expression and we'll recover as gracefully as
14283 possible. */
14284 id_expression_or_member_access_p = true;
14287 if (expr
14288 && expr != error_mark_node
14289 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14290 /* We have an id-expression. */
14291 id_expression_or_member_access_p = true;
14294 if (!id_expression_or_member_access_p)
14296 /* Abort the id-expression parse. */
14297 cp_parser_abort_tentative_parse (parser);
14299 /* Parsing tentatively, again. */
14300 cp_parser_parse_tentatively (parser);
14302 /* Parse a class member access. */
14303 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14304 /*cast_p=*/false, /*decltype*/true,
14305 /*member_access_only_p=*/true, NULL);
14307 if (expr
14308 && expr != error_mark_node
14309 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14310 /* We have an id-expression. */
14311 id_expression_or_member_access_p = true;
14314 if (id_expression_or_member_access_p)
14315 /* We have parsed the complete id-expression or member access. */
14316 cp_parser_parse_definitely (parser);
14317 else
14319 /* Abort our attempt to parse an id-expression or member access
14320 expression. */
14321 cp_parser_abort_tentative_parse (parser);
14323 /* Commit to the tentative_firewall so we get syntax errors. */
14324 cp_parser_commit_to_tentative_parse (parser);
14326 /* Parse a full expression. */
14327 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14328 /*decltype_p=*/true);
14331 return expr;
14334 /* Parse a `decltype' type. Returns the type.
14336 simple-type-specifier:
14337 decltype ( expression )
14338 C++14 proposal:
14339 decltype ( auto ) */
14341 static tree
14342 cp_parser_decltype (cp_parser *parser)
14344 bool id_expression_or_member_access_p = false;
14345 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14347 if (start_token->type == CPP_DECLTYPE)
14349 /* Already parsed. */
14350 cp_lexer_consume_token (parser->lexer);
14351 return saved_checks_value (start_token->u.tree_check_value);
14354 /* Look for the `decltype' token. */
14355 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14356 return error_mark_node;
14358 /* Parse the opening `('. */
14359 matching_parens parens;
14360 if (!parens.require_open (parser))
14361 return error_mark_node;
14363 push_deferring_access_checks (dk_deferred);
14365 tree expr = NULL_TREE;
14367 if (cxx_dialect >= cxx14
14368 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14369 /* decltype (auto) */
14370 cp_lexer_consume_token (parser->lexer);
14371 else
14373 /* decltype (expression) */
14375 /* Types cannot be defined in a `decltype' expression. Save away the
14376 old message and set the new one. */
14377 const char *saved_message = parser->type_definition_forbidden_message;
14378 parser->type_definition_forbidden_message
14379 = G_("types may not be defined in %<decltype%> expressions");
14381 /* The restrictions on constant-expressions do not apply inside
14382 decltype expressions. */
14383 bool saved_integral_constant_expression_p
14384 = parser->integral_constant_expression_p;
14385 bool saved_non_integral_constant_expression_p
14386 = parser->non_integral_constant_expression_p;
14387 parser->integral_constant_expression_p = false;
14389 /* Within a parenthesized expression, a `>' token is always
14390 the greater-than operator. */
14391 bool saved_greater_than_is_operator_p
14392 = parser->greater_than_is_operator_p;
14393 parser->greater_than_is_operator_p = true;
14395 /* Do not actually evaluate the expression. */
14396 ++cp_unevaluated_operand;
14398 /* Do not warn about problems with the expression. */
14399 ++c_inhibit_evaluation_warnings;
14401 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14403 /* Go back to evaluating expressions. */
14404 --cp_unevaluated_operand;
14405 --c_inhibit_evaluation_warnings;
14407 /* The `>' token might be the end of a template-id or
14408 template-parameter-list now. */
14409 parser->greater_than_is_operator_p
14410 = saved_greater_than_is_operator_p;
14412 /* Restore the old message and the integral constant expression
14413 flags. */
14414 parser->type_definition_forbidden_message = saved_message;
14415 parser->integral_constant_expression_p
14416 = saved_integral_constant_expression_p;
14417 parser->non_integral_constant_expression_p
14418 = saved_non_integral_constant_expression_p;
14421 /* Parse to the closing `)'. */
14422 if (!parens.require_close (parser))
14424 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14425 /*consume_paren=*/true);
14426 pop_deferring_access_checks ();
14427 return error_mark_node;
14430 if (!expr)
14432 /* Build auto. */
14433 expr = make_decltype_auto ();
14434 AUTO_IS_DECLTYPE (expr) = true;
14436 else
14437 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14438 tf_warning_or_error);
14440 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14441 it again. */
14442 start_token->type = CPP_DECLTYPE;
14443 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14444 start_token->u.tree_check_value->value = expr;
14445 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14446 start_token->keyword = RID_MAX;
14447 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14449 pop_to_parent_deferring_access_checks ();
14451 return expr;
14454 /* Special member functions [gram.special] */
14456 /* Parse a conversion-function-id.
14458 conversion-function-id:
14459 operator conversion-type-id
14461 Returns an IDENTIFIER_NODE representing the operator. */
14463 static tree
14464 cp_parser_conversion_function_id (cp_parser* parser)
14466 tree type;
14467 tree saved_scope;
14468 tree saved_qualifying_scope;
14469 tree saved_object_scope;
14470 tree pushed_scope = NULL_TREE;
14472 /* Look for the `operator' token. */
14473 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14474 return error_mark_node;
14475 /* When we parse the conversion-type-id, the current scope will be
14476 reset. However, we need that information in able to look up the
14477 conversion function later, so we save it here. */
14478 saved_scope = parser->scope;
14479 saved_qualifying_scope = parser->qualifying_scope;
14480 saved_object_scope = parser->object_scope;
14481 /* We must enter the scope of the class so that the names of
14482 entities declared within the class are available in the
14483 conversion-type-id. For example, consider:
14485 struct S {
14486 typedef int I;
14487 operator I();
14490 S::operator I() { ... }
14492 In order to see that `I' is a type-name in the definition, we
14493 must be in the scope of `S'. */
14494 if (saved_scope)
14495 pushed_scope = push_scope (saved_scope);
14496 /* Parse the conversion-type-id. */
14497 type = cp_parser_conversion_type_id (parser);
14498 /* Leave the scope of the class, if any. */
14499 if (pushed_scope)
14500 pop_scope (pushed_scope);
14501 /* Restore the saved scope. */
14502 parser->scope = saved_scope;
14503 parser->qualifying_scope = saved_qualifying_scope;
14504 parser->object_scope = saved_object_scope;
14505 /* If the TYPE is invalid, indicate failure. */
14506 if (type == error_mark_node)
14507 return error_mark_node;
14508 return make_conv_op_name (type);
14511 /* Parse a conversion-type-id:
14513 conversion-type-id:
14514 type-specifier-seq conversion-declarator [opt]
14516 Returns the TYPE specified. */
14518 static tree
14519 cp_parser_conversion_type_id (cp_parser* parser)
14521 tree attributes;
14522 cp_decl_specifier_seq type_specifiers;
14523 cp_declarator *declarator;
14524 tree type_specified;
14525 const char *saved_message;
14527 /* Parse the attributes. */
14528 attributes = cp_parser_attributes_opt (parser);
14530 saved_message = parser->type_definition_forbidden_message;
14531 parser->type_definition_forbidden_message
14532 = G_("types may not be defined in a conversion-type-id");
14534 /* Parse the type-specifiers. */
14535 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14536 /*is_trailing_return=*/false,
14537 &type_specifiers);
14539 parser->type_definition_forbidden_message = saved_message;
14541 /* If that didn't work, stop. */
14542 if (type_specifiers.type == error_mark_node)
14543 return error_mark_node;
14544 /* Parse the conversion-declarator. */
14545 declarator = cp_parser_conversion_declarator_opt (parser);
14547 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14548 /*initialized=*/0, &attributes);
14549 if (attributes)
14550 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14552 /* Don't give this error when parsing tentatively. This happens to
14553 work because we always parse this definitively once. */
14554 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14555 && type_uses_auto (type_specified))
14557 if (cxx_dialect < cxx14)
14559 error ("invalid use of %<auto%> in conversion operator");
14560 return error_mark_node;
14562 else if (template_parm_scope_p ())
14563 warning (0, "use of %<auto%> in member template "
14564 "conversion operator can never be deduced");
14567 return type_specified;
14570 /* Parse an (optional) conversion-declarator.
14572 conversion-declarator:
14573 ptr-operator conversion-declarator [opt]
14577 static cp_declarator *
14578 cp_parser_conversion_declarator_opt (cp_parser* parser)
14580 enum tree_code code;
14581 tree class_type, std_attributes = NULL_TREE;
14582 cp_cv_quals cv_quals;
14584 /* We don't know if there's a ptr-operator next, or not. */
14585 cp_parser_parse_tentatively (parser);
14586 /* Try the ptr-operator. */
14587 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14588 &std_attributes);
14589 /* If it worked, look for more conversion-declarators. */
14590 if (cp_parser_parse_definitely (parser))
14592 cp_declarator *declarator;
14594 /* Parse another optional declarator. */
14595 declarator = cp_parser_conversion_declarator_opt (parser);
14597 declarator = cp_parser_make_indirect_declarator
14598 (code, class_type, cv_quals, declarator, std_attributes);
14600 return declarator;
14603 return NULL;
14606 /* Parse an (optional) ctor-initializer.
14608 ctor-initializer:
14609 : mem-initializer-list */
14611 static void
14612 cp_parser_ctor_initializer_opt (cp_parser* parser)
14614 /* If the next token is not a `:', then there is no
14615 ctor-initializer. */
14616 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14618 /* Do default initialization of any bases and members. */
14619 if (DECL_CONSTRUCTOR_P (current_function_decl))
14620 finish_mem_initializers (NULL_TREE);
14621 return;
14624 /* Consume the `:' token. */
14625 cp_lexer_consume_token (parser->lexer);
14626 /* And the mem-initializer-list. */
14627 cp_parser_mem_initializer_list (parser);
14630 /* Parse a mem-initializer-list.
14632 mem-initializer-list:
14633 mem-initializer ... [opt]
14634 mem-initializer ... [opt] , mem-initializer-list */
14636 static void
14637 cp_parser_mem_initializer_list (cp_parser* parser)
14639 tree mem_initializer_list = NULL_TREE;
14640 tree target_ctor = error_mark_node;
14641 cp_token *token = cp_lexer_peek_token (parser->lexer);
14643 /* Let the semantic analysis code know that we are starting the
14644 mem-initializer-list. */
14645 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14646 error_at (token->location,
14647 "only constructors take member initializers");
14649 /* Loop through the list. */
14650 while (true)
14652 tree mem_initializer;
14654 token = cp_lexer_peek_token (parser->lexer);
14655 /* Parse the mem-initializer. */
14656 mem_initializer = cp_parser_mem_initializer (parser);
14657 /* If the next token is a `...', we're expanding member initializers. */
14658 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14659 if (ellipsis
14660 || (mem_initializer != error_mark_node
14661 && check_for_bare_parameter_packs (TREE_PURPOSE
14662 (mem_initializer))))
14664 /* Consume the `...'. */
14665 if (ellipsis)
14666 cp_lexer_consume_token (parser->lexer);
14668 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14669 can be expanded but members cannot. */
14670 if (mem_initializer != error_mark_node
14671 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14673 error_at (token->location,
14674 "cannot expand initializer for member %qD",
14675 TREE_PURPOSE (mem_initializer));
14676 mem_initializer = error_mark_node;
14679 /* Construct the pack expansion type. */
14680 if (mem_initializer != error_mark_node)
14681 mem_initializer = make_pack_expansion (mem_initializer);
14683 if (target_ctor != error_mark_node
14684 && mem_initializer != error_mark_node)
14686 error ("mem-initializer for %qD follows constructor delegation",
14687 TREE_PURPOSE (mem_initializer));
14688 mem_initializer = error_mark_node;
14690 /* Look for a target constructor. */
14691 if (mem_initializer != error_mark_node
14692 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14693 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14695 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14696 if (mem_initializer_list)
14698 error ("constructor delegation follows mem-initializer for %qD",
14699 TREE_PURPOSE (mem_initializer_list));
14700 mem_initializer = error_mark_node;
14702 target_ctor = mem_initializer;
14704 /* Add it to the list, unless it was erroneous. */
14705 if (mem_initializer != error_mark_node)
14707 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14708 mem_initializer_list = mem_initializer;
14710 /* If the next token is not a `,', we're done. */
14711 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14712 break;
14713 /* Consume the `,' token. */
14714 cp_lexer_consume_token (parser->lexer);
14717 /* Perform semantic analysis. */
14718 if (DECL_CONSTRUCTOR_P (current_function_decl))
14719 finish_mem_initializers (mem_initializer_list);
14722 /* Parse a mem-initializer.
14724 mem-initializer:
14725 mem-initializer-id ( expression-list [opt] )
14726 mem-initializer-id braced-init-list
14728 GNU extension:
14730 mem-initializer:
14731 ( expression-list [opt] )
14733 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14734 class) or FIELD_DECL (for a non-static data member) to initialize;
14735 the TREE_VALUE is the expression-list. An empty initialization
14736 list is represented by void_list_node. */
14738 static tree
14739 cp_parser_mem_initializer (cp_parser* parser)
14741 tree mem_initializer_id;
14742 tree expression_list;
14743 tree member;
14744 cp_token *token = cp_lexer_peek_token (parser->lexer);
14746 /* Find out what is being initialized. */
14747 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14749 permerror (token->location,
14750 "anachronistic old-style base class initializer");
14751 mem_initializer_id = NULL_TREE;
14753 else
14755 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14756 if (mem_initializer_id == error_mark_node)
14757 return mem_initializer_id;
14759 member = expand_member_init (mem_initializer_id);
14760 if (member && !DECL_P (member))
14761 in_base_initializer = 1;
14763 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14765 bool expr_non_constant_p;
14766 cp_lexer_set_source_position (parser->lexer);
14767 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14768 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14769 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14770 expression_list = build_tree_list (NULL_TREE, expression_list);
14772 else
14774 vec<tree, va_gc> *vec;
14775 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14776 /*cast_p=*/false,
14777 /*allow_expansion_p=*/true,
14778 /*non_constant_p=*/NULL);
14779 if (vec == NULL)
14780 return error_mark_node;
14781 expression_list = build_tree_list_vec (vec);
14782 release_tree_vector (vec);
14785 if (expression_list == error_mark_node)
14786 return error_mark_node;
14787 if (!expression_list)
14788 expression_list = void_type_node;
14790 in_base_initializer = 0;
14792 return member ? build_tree_list (member, expression_list) : error_mark_node;
14795 /* Parse a mem-initializer-id.
14797 mem-initializer-id:
14798 :: [opt] nested-name-specifier [opt] class-name
14799 decltype-specifier (C++11)
14800 identifier
14802 Returns a TYPE indicating the class to be initialized for the first
14803 production (and the second in C++11). Returns an IDENTIFIER_NODE
14804 indicating the data member to be initialized for the last production. */
14806 static tree
14807 cp_parser_mem_initializer_id (cp_parser* parser)
14809 bool global_scope_p;
14810 bool nested_name_specifier_p;
14811 bool template_p = false;
14812 tree id;
14814 cp_token *token = cp_lexer_peek_token (parser->lexer);
14816 /* `typename' is not allowed in this context ([temp.res]). */
14817 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14819 error_at (token->location,
14820 "keyword %<typename%> not allowed in this context (a qualified "
14821 "member initializer is implicitly a type)");
14822 cp_lexer_consume_token (parser->lexer);
14824 /* Look for the optional `::' operator. */
14825 global_scope_p
14826 = (cp_parser_global_scope_opt (parser,
14827 /*current_scope_valid_p=*/false)
14828 != NULL_TREE);
14829 /* Look for the optional nested-name-specifier. The simplest way to
14830 implement:
14832 [temp.res]
14834 The keyword `typename' is not permitted in a base-specifier or
14835 mem-initializer; in these contexts a qualified name that
14836 depends on a template-parameter is implicitly assumed to be a
14837 type name.
14839 is to assume that we have seen the `typename' keyword at this
14840 point. */
14841 nested_name_specifier_p
14842 = (cp_parser_nested_name_specifier_opt (parser,
14843 /*typename_keyword_p=*/true,
14844 /*check_dependency_p=*/true,
14845 /*type_p=*/true,
14846 /*is_declaration=*/true)
14847 != NULL_TREE);
14848 if (nested_name_specifier_p)
14849 template_p = cp_parser_optional_template_keyword (parser);
14850 /* If there is a `::' operator or a nested-name-specifier, then we
14851 are definitely looking for a class-name. */
14852 if (global_scope_p || nested_name_specifier_p)
14853 return cp_parser_class_name (parser,
14854 /*typename_keyword_p=*/true,
14855 /*template_keyword_p=*/template_p,
14856 typename_type,
14857 /*check_dependency_p=*/true,
14858 /*class_head_p=*/false,
14859 /*is_declaration=*/true);
14860 /* Otherwise, we could also be looking for an ordinary identifier. */
14861 cp_parser_parse_tentatively (parser);
14862 if (cp_lexer_next_token_is_decltype (parser->lexer))
14863 /* Try a decltype-specifier. */
14864 id = cp_parser_decltype (parser);
14865 else
14866 /* Otherwise, try a class-name. */
14867 id = cp_parser_class_name (parser,
14868 /*typename_keyword_p=*/true,
14869 /*template_keyword_p=*/false,
14870 none_type,
14871 /*check_dependency_p=*/true,
14872 /*class_head_p=*/false,
14873 /*is_declaration=*/true);
14874 /* If we found one, we're done. */
14875 if (cp_parser_parse_definitely (parser))
14876 return id;
14877 /* Otherwise, look for an ordinary identifier. */
14878 return cp_parser_identifier (parser);
14881 /* Overloading [gram.over] */
14883 /* Parse an operator-function-id.
14885 operator-function-id:
14886 operator operator
14888 Returns an IDENTIFIER_NODE for the operator which is a
14889 human-readable spelling of the identifier, e.g., `operator +'. */
14891 static cp_expr
14892 cp_parser_operator_function_id (cp_parser* parser)
14894 /* Look for the `operator' keyword. */
14895 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14896 return error_mark_node;
14897 /* And then the name of the operator itself. */
14898 return cp_parser_operator (parser);
14901 /* Return an identifier node for a user-defined literal operator.
14902 The suffix identifier is chained to the operator name identifier. */
14904 tree
14905 cp_literal_operator_id (const char* name)
14907 tree identifier;
14908 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14909 + strlen (name) + 10);
14910 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14911 identifier = get_identifier (buffer);
14913 return identifier;
14916 /* Parse an operator.
14918 operator:
14919 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14920 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14921 || ++ -- , ->* -> () []
14923 GNU Extensions:
14925 operator:
14926 <? >? <?= >?=
14928 Returns an IDENTIFIER_NODE for the operator which is a
14929 human-readable spelling of the identifier, e.g., `operator +'. */
14931 static cp_expr
14932 cp_parser_operator (cp_parser* parser)
14934 tree id = NULL_TREE;
14935 cp_token *token;
14936 bool utf8 = false;
14938 /* Peek at the next token. */
14939 token = cp_lexer_peek_token (parser->lexer);
14941 location_t start_loc = token->location;
14943 /* Figure out which operator we have. */
14944 enum tree_code op = ERROR_MARK;
14945 bool assop = false;
14946 bool consumed = false;
14947 switch (token->type)
14949 case CPP_KEYWORD:
14951 /* The keyword should be either `new' or `delete'. */
14952 if (token->keyword == RID_NEW)
14953 op = NEW_EXPR;
14954 else if (token->keyword == RID_DELETE)
14955 op = DELETE_EXPR;
14956 else
14957 break;
14959 /* Consume the `new' or `delete' token. */
14960 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14962 /* Peek at the next token. */
14963 token = cp_lexer_peek_token (parser->lexer);
14964 /* If it's a `[' token then this is the array variant of the
14965 operator. */
14966 if (token->type == CPP_OPEN_SQUARE)
14968 /* Consume the `[' token. */
14969 cp_lexer_consume_token (parser->lexer);
14970 /* Look for the `]' token. */
14971 if (cp_token *close_token
14972 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14973 end_loc = close_token->location;
14974 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14976 start_loc = make_location (start_loc, start_loc, end_loc);
14977 consumed = true;
14978 break;
14981 case CPP_PLUS:
14982 op = PLUS_EXPR;
14983 break;
14985 case CPP_MINUS:
14986 op = MINUS_EXPR;
14987 break;
14989 case CPP_MULT:
14990 op = MULT_EXPR;
14991 break;
14993 case CPP_DIV:
14994 op = TRUNC_DIV_EXPR;
14995 break;
14997 case CPP_MOD:
14998 op = TRUNC_MOD_EXPR;
14999 break;
15001 case CPP_XOR:
15002 op = BIT_XOR_EXPR;
15003 break;
15005 case CPP_AND:
15006 op = BIT_AND_EXPR;
15007 break;
15009 case CPP_OR:
15010 op = BIT_IOR_EXPR;
15011 break;
15013 case CPP_COMPL:
15014 op = BIT_NOT_EXPR;
15015 break;
15017 case CPP_NOT:
15018 op = TRUTH_NOT_EXPR;
15019 break;
15021 case CPP_EQ:
15022 assop = true;
15023 op = NOP_EXPR;
15024 break;
15026 case CPP_LESS:
15027 op = LT_EXPR;
15028 break;
15030 case CPP_GREATER:
15031 op = GT_EXPR;
15032 break;
15034 case CPP_PLUS_EQ:
15035 assop = true;
15036 op = PLUS_EXPR;
15037 break;
15039 case CPP_MINUS_EQ:
15040 assop = true;
15041 op = MINUS_EXPR;
15042 break;
15044 case CPP_MULT_EQ:
15045 assop = true;
15046 op = MULT_EXPR;
15047 break;
15049 case CPP_DIV_EQ:
15050 assop = true;
15051 op = TRUNC_DIV_EXPR;
15052 break;
15054 case CPP_MOD_EQ:
15055 assop = true;
15056 op = TRUNC_MOD_EXPR;
15057 break;
15059 case CPP_XOR_EQ:
15060 assop = true;
15061 op = BIT_XOR_EXPR;
15062 break;
15064 case CPP_AND_EQ:
15065 assop = true;
15066 op = BIT_AND_EXPR;
15067 break;
15069 case CPP_OR_EQ:
15070 assop = true;
15071 op = BIT_IOR_EXPR;
15072 break;
15074 case CPP_LSHIFT:
15075 op = LSHIFT_EXPR;
15076 break;
15078 case CPP_RSHIFT:
15079 op = RSHIFT_EXPR;
15080 break;
15082 case CPP_LSHIFT_EQ:
15083 assop = true;
15084 op = LSHIFT_EXPR;
15085 break;
15087 case CPP_RSHIFT_EQ:
15088 assop = true;
15089 op = RSHIFT_EXPR;
15090 break;
15092 case CPP_EQ_EQ:
15093 op = EQ_EXPR;
15094 break;
15096 case CPP_NOT_EQ:
15097 op = NE_EXPR;
15098 break;
15100 case CPP_LESS_EQ:
15101 op = LE_EXPR;
15102 break;
15104 case CPP_GREATER_EQ:
15105 op = GE_EXPR;
15106 break;
15108 case CPP_AND_AND:
15109 op = TRUTH_ANDIF_EXPR;
15110 break;
15112 case CPP_OR_OR:
15113 op = TRUTH_ORIF_EXPR;
15114 break;
15116 case CPP_PLUS_PLUS:
15117 op = POSTINCREMENT_EXPR;
15118 break;
15120 case CPP_MINUS_MINUS:
15121 op = PREDECREMENT_EXPR;
15122 break;
15124 case CPP_COMMA:
15125 op = COMPOUND_EXPR;
15126 break;
15128 case CPP_DEREF_STAR:
15129 op = MEMBER_REF;
15130 break;
15132 case CPP_DEREF:
15133 op = COMPONENT_REF;
15134 break;
15136 case CPP_OPEN_PAREN:
15138 /* Consume the `('. */
15139 matching_parens parens;
15140 parens.consume_open (parser);
15141 /* Look for the matching `)'. */
15142 parens.require_close (parser);
15143 op = CALL_EXPR;
15144 consumed = true;
15145 break;
15148 case CPP_OPEN_SQUARE:
15149 /* Consume the `['. */
15150 cp_lexer_consume_token (parser->lexer);
15151 /* Look for the matching `]'. */
15152 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15153 op = ARRAY_REF;
15154 consumed = true;
15155 break;
15157 case CPP_UTF8STRING:
15158 case CPP_UTF8STRING_USERDEF:
15159 utf8 = true;
15160 /* FALLTHRU */
15161 case CPP_STRING:
15162 case CPP_WSTRING:
15163 case CPP_STRING16:
15164 case CPP_STRING32:
15165 case CPP_STRING_USERDEF:
15166 case CPP_WSTRING_USERDEF:
15167 case CPP_STRING16_USERDEF:
15168 case CPP_STRING32_USERDEF:
15170 tree str, string_tree;
15171 int sz, len;
15173 if (cxx_dialect == cxx98)
15174 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15176 /* Consume the string. */
15177 str = cp_parser_string_literal (parser, /*translate=*/true,
15178 /*wide_ok=*/true, /*lookup_udlit=*/false);
15179 if (str == error_mark_node)
15180 return error_mark_node;
15181 else if (TREE_CODE (str) == USERDEF_LITERAL)
15183 string_tree = USERDEF_LITERAL_VALUE (str);
15184 id = USERDEF_LITERAL_SUFFIX_ID (str);
15186 else
15188 string_tree = str;
15189 /* Look for the suffix identifier. */
15190 token = cp_lexer_peek_token (parser->lexer);
15191 if (token->type == CPP_NAME)
15192 id = cp_parser_identifier (parser);
15193 else if (token->type == CPP_KEYWORD)
15195 error ("unexpected keyword;"
15196 " remove space between quotes and suffix identifier");
15197 return error_mark_node;
15199 else
15201 error ("expected suffix identifier");
15202 return error_mark_node;
15205 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15206 (TREE_TYPE (TREE_TYPE (string_tree))));
15207 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15208 if (len != 0)
15210 error ("expected empty string after %<operator%> keyword");
15211 return error_mark_node;
15213 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15214 != char_type_node)
15216 error ("invalid encoding prefix in literal operator");
15217 return error_mark_node;
15219 if (id != error_mark_node)
15221 const char *name = IDENTIFIER_POINTER (id);
15222 id = cp_literal_operator_id (name);
15224 return id;
15227 default:
15228 /* Anything else is an error. */
15229 break;
15232 /* If we have selected an identifier, we need to consume the
15233 operator token. */
15234 if (op != ERROR_MARK)
15236 id = ovl_op_identifier (assop, op);
15237 if (!consumed)
15238 cp_lexer_consume_token (parser->lexer);
15240 /* Otherwise, no valid operator name was present. */
15241 else
15243 cp_parser_error (parser, "expected operator");
15244 id = error_mark_node;
15247 return cp_expr (id, start_loc);
15250 /* Parse a template-declaration.
15252 template-declaration:
15253 export [opt] template < template-parameter-list > declaration
15255 If MEMBER_P is TRUE, this template-declaration occurs within a
15256 class-specifier.
15258 The grammar rule given by the standard isn't correct. What
15259 is really meant is:
15261 template-declaration:
15262 export [opt] template-parameter-list-seq
15263 decl-specifier-seq [opt] init-declarator [opt] ;
15264 export [opt] template-parameter-list-seq
15265 function-definition
15267 template-parameter-list-seq:
15268 template-parameter-list-seq [opt]
15269 template < template-parameter-list >
15271 Concept Extensions:
15273 template-parameter-list-seq:
15274 template < template-parameter-list > requires-clause [opt]
15276 requires-clause:
15277 requires logical-or-expression */
15279 static void
15280 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15282 /* Check for `export'. */
15283 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15285 /* Consume the `export' token. */
15286 cp_lexer_consume_token (parser->lexer);
15287 /* Warn that we do not support `export'. */
15288 warning (0, "keyword %<export%> not implemented, and will be ignored");
15291 cp_parser_template_declaration_after_export (parser, member_p);
15294 /* Parse a template-parameter-list.
15296 template-parameter-list:
15297 template-parameter
15298 template-parameter-list , template-parameter
15300 Returns a TREE_LIST. Each node represents a template parameter.
15301 The nodes are connected via their TREE_CHAINs. */
15303 static tree
15304 cp_parser_template_parameter_list (cp_parser* parser)
15306 tree parameter_list = NULL_TREE;
15308 begin_template_parm_list ();
15310 /* The loop below parses the template parms. We first need to know
15311 the total number of template parms to be able to compute proper
15312 canonical types of each dependent type. So after the loop, when
15313 we know the total number of template parms,
15314 end_template_parm_list computes the proper canonical types and
15315 fixes up the dependent types accordingly. */
15316 while (true)
15318 tree parameter;
15319 bool is_non_type;
15320 bool is_parameter_pack;
15321 location_t parm_loc;
15323 /* Parse the template-parameter. */
15324 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15325 parameter = cp_parser_template_parameter (parser,
15326 &is_non_type,
15327 &is_parameter_pack);
15328 /* Add it to the list. */
15329 if (parameter != error_mark_node)
15330 parameter_list = process_template_parm (parameter_list,
15331 parm_loc,
15332 parameter,
15333 is_non_type,
15334 is_parameter_pack);
15335 else
15337 tree err_parm = build_tree_list (parameter, parameter);
15338 parameter_list = chainon (parameter_list, err_parm);
15341 /* If the next token is not a `,', we're done. */
15342 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15343 break;
15344 /* Otherwise, consume the `,' token. */
15345 cp_lexer_consume_token (parser->lexer);
15348 return end_template_parm_list (parameter_list);
15351 /* Parse a introduction-list.
15353 introduction-list:
15354 introduced-parameter
15355 introduction-list , introduced-parameter
15357 introduced-parameter:
15358 ...[opt] identifier
15360 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15361 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15362 WILDCARD_DECL will also have DECL_NAME set and token location in
15363 DECL_SOURCE_LOCATION. */
15365 static tree
15366 cp_parser_introduction_list (cp_parser *parser)
15368 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15370 while (true)
15372 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15373 if (is_pack)
15374 cp_lexer_consume_token (parser->lexer);
15376 tree identifier = cp_parser_identifier (parser);
15377 if (identifier == error_mark_node)
15378 break;
15380 /* Build placeholder. */
15381 tree parm = build_nt (WILDCARD_DECL);
15382 DECL_SOURCE_LOCATION (parm)
15383 = cp_lexer_peek_token (parser->lexer)->location;
15384 DECL_NAME (parm) = identifier;
15385 WILDCARD_PACK_P (parm) = is_pack;
15386 vec_safe_push (introduction_vec, parm);
15388 /* If the next token is not a `,', we're done. */
15389 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15390 break;
15391 /* Otherwise, consume the `,' token. */
15392 cp_lexer_consume_token (parser->lexer);
15395 /* Convert the vec into a TREE_VEC. */
15396 tree introduction_list = make_tree_vec (introduction_vec->length ());
15397 unsigned int n;
15398 tree parm;
15399 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15400 TREE_VEC_ELT (introduction_list, n) = parm;
15402 release_tree_vector (introduction_vec);
15403 return introduction_list;
15406 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15407 is an abstract declarator. */
15409 static inline cp_declarator*
15410 get_id_declarator (cp_declarator *declarator)
15412 cp_declarator *d = declarator;
15413 while (d && d->kind != cdk_id)
15414 d = d->declarator;
15415 return d;
15418 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15419 is an abstract declarator. */
15421 static inline tree
15422 get_unqualified_id (cp_declarator *declarator)
15424 declarator = get_id_declarator (declarator);
15425 if (declarator)
15426 return declarator->u.id.unqualified_name;
15427 else
15428 return NULL_TREE;
15431 /* Returns true if DECL represents a constrained-parameter. */
15433 static inline bool
15434 is_constrained_parameter (tree decl)
15436 return (decl
15437 && TREE_CODE (decl) == TYPE_DECL
15438 && CONSTRAINED_PARM_CONCEPT (decl)
15439 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15442 /* Returns true if PARM declares a constrained-parameter. */
15444 static inline bool
15445 is_constrained_parameter (cp_parameter_declarator *parm)
15447 return is_constrained_parameter (parm->decl_specifiers.type);
15450 /* Check that the type parameter is only a declarator-id, and that its
15451 type is not cv-qualified. */
15453 bool
15454 cp_parser_check_constrained_type_parm (cp_parser *parser,
15455 cp_parameter_declarator *parm)
15457 if (!parm->declarator)
15458 return true;
15460 if (parm->declarator->kind != cdk_id)
15462 cp_parser_error (parser, "invalid constrained type parameter");
15463 return false;
15466 /* Don't allow cv-qualified type parameters. */
15467 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15468 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15470 cp_parser_error (parser, "cv-qualified type parameter");
15471 return false;
15474 return true;
15477 /* Finish parsing/processing a template type parameter and checking
15478 various restrictions. */
15480 static inline tree
15481 cp_parser_constrained_type_template_parm (cp_parser *parser,
15482 tree id,
15483 cp_parameter_declarator* parmdecl)
15485 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15486 return finish_template_type_parm (class_type_node, id);
15487 else
15488 return error_mark_node;
15491 static tree
15492 finish_constrained_template_template_parm (tree proto, tree id)
15494 /* FIXME: This should probably be copied, and we may need to adjust
15495 the template parameter depths. */
15496 tree saved_parms = current_template_parms;
15497 begin_template_parm_list ();
15498 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15499 end_template_parm_list ();
15501 tree parm = finish_template_template_parm (class_type_node, id);
15502 current_template_parms = saved_parms;
15504 return parm;
15507 /* Finish parsing/processing a template template parameter by borrowing
15508 the template parameter list from the prototype parameter. */
15510 static tree
15511 cp_parser_constrained_template_template_parm (cp_parser *parser,
15512 tree proto,
15513 tree id,
15514 cp_parameter_declarator *parmdecl)
15516 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15517 return error_mark_node;
15518 return finish_constrained_template_template_parm (proto, id);
15521 /* Create a new non-type template parameter from the given PARM
15522 declarator. */
15524 static tree
15525 constrained_non_type_template_parm (bool *is_non_type,
15526 cp_parameter_declarator *parm)
15528 *is_non_type = true;
15529 cp_declarator *decl = parm->declarator;
15530 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15531 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15532 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15535 /* Build a constrained template parameter based on the PARMDECL
15536 declarator. The type of PARMDECL is the constrained type, which
15537 refers to the prototype template parameter that ultimately
15538 specifies the type of the declared parameter. */
15540 static tree
15541 finish_constrained_parameter (cp_parser *parser,
15542 cp_parameter_declarator *parmdecl,
15543 bool *is_non_type,
15544 bool *is_parameter_pack)
15546 tree decl = parmdecl->decl_specifiers.type;
15547 tree id = get_unqualified_id (parmdecl->declarator);
15548 tree def = parmdecl->default_argument;
15549 tree proto = DECL_INITIAL (decl);
15551 /* A template parameter constrained by a variadic concept shall also
15552 be declared as a template parameter pack. */
15553 bool is_variadic = template_parameter_pack_p (proto);
15554 if (is_variadic && !*is_parameter_pack)
15555 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15557 /* Build the parameter. Return an error if the declarator was invalid. */
15558 tree parm;
15559 if (TREE_CODE (proto) == TYPE_DECL)
15560 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15561 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15562 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15563 parmdecl);
15564 else
15565 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15566 if (parm == error_mark_node)
15567 return error_mark_node;
15569 /* Finish the parameter decl and create a node attaching the
15570 default argument and constraint. */
15571 parm = build_tree_list (def, parm);
15572 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15574 return parm;
15577 /* Returns true if the parsed type actually represents the declaration
15578 of a type template-parameter. */
15580 static inline bool
15581 declares_constrained_type_template_parameter (tree type)
15583 return (is_constrained_parameter (type)
15584 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15588 /* Returns true if the parsed type actually represents the declaration of
15589 a template template-parameter. */
15591 static bool
15592 declares_constrained_template_template_parameter (tree type)
15594 return (is_constrained_parameter (type)
15595 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15598 /* Parse a default argument for a type template-parameter.
15599 Note that diagnostics are handled in cp_parser_template_parameter. */
15601 static tree
15602 cp_parser_default_type_template_argument (cp_parser *parser)
15604 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15606 /* Consume the `=' token. */
15607 cp_lexer_consume_token (parser->lexer);
15609 cp_token *token = cp_lexer_peek_token (parser->lexer);
15611 /* Parse the default-argument. */
15612 push_deferring_access_checks (dk_no_deferred);
15613 tree default_argument = cp_parser_type_id (parser);
15614 pop_deferring_access_checks ();
15616 if (flag_concepts && type_uses_auto (default_argument))
15618 error_at (token->location,
15619 "invalid use of %<auto%> in default template argument");
15620 return error_mark_node;
15623 return default_argument;
15626 /* Parse a default argument for a template template-parameter. */
15628 static tree
15629 cp_parser_default_template_template_argument (cp_parser *parser)
15631 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15633 bool is_template;
15635 /* Consume the `='. */
15636 cp_lexer_consume_token (parser->lexer);
15637 /* Parse the id-expression. */
15638 push_deferring_access_checks (dk_no_deferred);
15639 /* save token before parsing the id-expression, for error
15640 reporting */
15641 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15642 tree default_argument
15643 = cp_parser_id_expression (parser,
15644 /*template_keyword_p=*/false,
15645 /*check_dependency_p=*/true,
15646 /*template_p=*/&is_template,
15647 /*declarator_p=*/false,
15648 /*optional_p=*/false);
15649 if (TREE_CODE (default_argument) == TYPE_DECL)
15650 /* If the id-expression was a template-id that refers to
15651 a template-class, we already have the declaration here,
15652 so no further lookup is needed. */
15654 else
15655 /* Look up the name. */
15656 default_argument
15657 = cp_parser_lookup_name (parser, default_argument,
15658 none_type,
15659 /*is_template=*/is_template,
15660 /*is_namespace=*/false,
15661 /*check_dependency=*/true,
15662 /*ambiguous_decls=*/NULL,
15663 token->location);
15664 /* See if the default argument is valid. */
15665 default_argument = check_template_template_default_arg (default_argument);
15666 pop_deferring_access_checks ();
15667 return default_argument;
15670 /* Parse a template-parameter.
15672 template-parameter:
15673 type-parameter
15674 parameter-declaration
15676 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15677 the parameter. The TREE_PURPOSE is the default value, if any.
15678 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15679 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15680 set to true iff this parameter is a parameter pack. */
15682 static tree
15683 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15684 bool *is_parameter_pack)
15686 cp_token *token;
15687 cp_parameter_declarator *parameter_declarator;
15688 tree parm;
15690 /* Assume it is a type parameter or a template parameter. */
15691 *is_non_type = false;
15692 /* Assume it not a parameter pack. */
15693 *is_parameter_pack = false;
15694 /* Peek at the next token. */
15695 token = cp_lexer_peek_token (parser->lexer);
15696 /* If it is `template', we have a type-parameter. */
15697 if (token->keyword == RID_TEMPLATE)
15698 return cp_parser_type_parameter (parser, is_parameter_pack);
15699 /* If it is `class' or `typename' we do not know yet whether it is a
15700 type parameter or a non-type parameter. Consider:
15702 template <typename T, typename T::X X> ...
15706 template <class C, class D*> ...
15708 Here, the first parameter is a type parameter, and the second is
15709 a non-type parameter. We can tell by looking at the token after
15710 the identifier -- if it is a `,', `=', or `>' then we have a type
15711 parameter. */
15712 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15714 /* Peek at the token after `class' or `typename'. */
15715 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15716 /* If it's an ellipsis, we have a template type parameter
15717 pack. */
15718 if (token->type == CPP_ELLIPSIS)
15719 return cp_parser_type_parameter (parser, is_parameter_pack);
15720 /* If it's an identifier, skip it. */
15721 if (token->type == CPP_NAME)
15722 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15723 /* Now, see if the token looks like the end of a template
15724 parameter. */
15725 if (token->type == CPP_COMMA
15726 || token->type == CPP_EQ
15727 || token->type == CPP_GREATER)
15728 return cp_parser_type_parameter (parser, is_parameter_pack);
15731 /* Otherwise, it is a non-type parameter or a constrained parameter.
15733 [temp.param]
15735 When parsing a default template-argument for a non-type
15736 template-parameter, the first non-nested `>' is taken as the end
15737 of the template parameter-list rather than a greater-than
15738 operator. */
15739 parameter_declarator
15740 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15741 /*parenthesized_p=*/NULL);
15743 if (!parameter_declarator)
15744 return error_mark_node;
15746 /* If the parameter declaration is marked as a parameter pack, set
15747 *IS_PARAMETER_PACK to notify the caller. */
15748 if (parameter_declarator->template_parameter_pack_p)
15749 *is_parameter_pack = true;
15751 if (parameter_declarator->default_argument)
15753 /* Can happen in some cases of erroneous input (c++/34892). */
15754 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15755 /* Consume the `...' for better error recovery. */
15756 cp_lexer_consume_token (parser->lexer);
15759 // The parameter may have been constrained.
15760 if (is_constrained_parameter (parameter_declarator))
15761 return finish_constrained_parameter (parser,
15762 parameter_declarator,
15763 is_non_type,
15764 is_parameter_pack);
15766 // Now we're sure that the parameter is a non-type parameter.
15767 *is_non_type = true;
15769 parm = grokdeclarator (parameter_declarator->declarator,
15770 &parameter_declarator->decl_specifiers,
15771 TPARM, /*initialized=*/0,
15772 /*attrlist=*/NULL);
15773 if (parm == error_mark_node)
15774 return error_mark_node;
15776 return build_tree_list (parameter_declarator->default_argument, parm);
15779 /* Parse a type-parameter.
15781 type-parameter:
15782 class identifier [opt]
15783 class identifier [opt] = type-id
15784 typename identifier [opt]
15785 typename identifier [opt] = type-id
15786 template < template-parameter-list > class identifier [opt]
15787 template < template-parameter-list > class identifier [opt]
15788 = id-expression
15790 GNU Extension (variadic templates):
15792 type-parameter:
15793 class ... identifier [opt]
15794 typename ... identifier [opt]
15796 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15797 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15798 the declaration of the parameter.
15800 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15802 static tree
15803 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15805 cp_token *token;
15806 tree parameter;
15808 /* Look for a keyword to tell us what kind of parameter this is. */
15809 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15810 if (!token)
15811 return error_mark_node;
15813 switch (token->keyword)
15815 case RID_CLASS:
15816 case RID_TYPENAME:
15818 tree identifier;
15819 tree default_argument;
15821 /* If the next token is an ellipsis, we have a template
15822 argument pack. */
15823 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15825 /* Consume the `...' token. */
15826 cp_lexer_consume_token (parser->lexer);
15827 maybe_warn_variadic_templates ();
15829 *is_parameter_pack = true;
15832 /* If the next token is an identifier, then it names the
15833 parameter. */
15834 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15835 identifier = cp_parser_identifier (parser);
15836 else
15837 identifier = NULL_TREE;
15839 /* Create the parameter. */
15840 parameter = finish_template_type_parm (class_type_node, identifier);
15842 /* If the next token is an `=', we have a default argument. */
15843 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15845 default_argument
15846 = cp_parser_default_type_template_argument (parser);
15848 /* Template parameter packs cannot have default
15849 arguments. */
15850 if (*is_parameter_pack)
15852 if (identifier)
15853 error_at (token->location,
15854 "template parameter pack %qD cannot have a "
15855 "default argument", identifier);
15856 else
15857 error_at (token->location,
15858 "template parameter packs cannot have "
15859 "default arguments");
15860 default_argument = NULL_TREE;
15862 else if (check_for_bare_parameter_packs (default_argument))
15863 default_argument = error_mark_node;
15865 else
15866 default_argument = NULL_TREE;
15868 /* Create the combined representation of the parameter and the
15869 default argument. */
15870 parameter = build_tree_list (default_argument, parameter);
15872 break;
15874 case RID_TEMPLATE:
15876 tree identifier;
15877 tree default_argument;
15879 /* Look for the `<'. */
15880 cp_parser_require (parser, CPP_LESS, RT_LESS);
15881 /* Parse the template-parameter-list. */
15882 cp_parser_template_parameter_list (parser);
15883 /* Look for the `>'. */
15884 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15886 // If template requirements are present, parse them.
15887 if (flag_concepts)
15889 tree reqs = get_shorthand_constraints (current_template_parms);
15890 if (tree r = cp_parser_requires_clause_opt (parser))
15891 reqs = conjoin_constraints (reqs, normalize_expression (r));
15892 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15895 /* Look for the `class' or 'typename' keywords. */
15896 cp_parser_type_parameter_key (parser);
15897 /* If the next token is an ellipsis, we have a template
15898 argument pack. */
15899 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15901 /* Consume the `...' token. */
15902 cp_lexer_consume_token (parser->lexer);
15903 maybe_warn_variadic_templates ();
15905 *is_parameter_pack = true;
15907 /* If the next token is an `=', then there is a
15908 default-argument. If the next token is a `>', we are at
15909 the end of the parameter-list. If the next token is a `,',
15910 then we are at the end of this parameter. */
15911 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15912 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15913 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15915 identifier = cp_parser_identifier (parser);
15916 /* Treat invalid names as if the parameter were nameless. */
15917 if (identifier == error_mark_node)
15918 identifier = NULL_TREE;
15920 else
15921 identifier = NULL_TREE;
15923 /* Create the template parameter. */
15924 parameter = finish_template_template_parm (class_type_node,
15925 identifier);
15927 /* If the next token is an `=', then there is a
15928 default-argument. */
15929 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15931 default_argument
15932 = cp_parser_default_template_template_argument (parser);
15934 /* Template parameter packs cannot have default
15935 arguments. */
15936 if (*is_parameter_pack)
15938 if (identifier)
15939 error_at (token->location,
15940 "template parameter pack %qD cannot "
15941 "have a default argument",
15942 identifier);
15943 else
15944 error_at (token->location, "template parameter packs cannot "
15945 "have default arguments");
15946 default_argument = NULL_TREE;
15949 else
15950 default_argument = NULL_TREE;
15952 /* Create the combined representation of the parameter and the
15953 default argument. */
15954 parameter = build_tree_list (default_argument, parameter);
15956 break;
15958 default:
15959 gcc_unreachable ();
15960 break;
15963 return parameter;
15966 /* Parse a template-id.
15968 template-id:
15969 template-name < template-argument-list [opt] >
15971 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15972 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15973 returned. Otherwise, if the template-name names a function, or set
15974 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15975 names a class, returns a TYPE_DECL for the specialization.
15977 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15978 uninstantiated templates. */
15980 static tree
15981 cp_parser_template_id (cp_parser *parser,
15982 bool template_keyword_p,
15983 bool check_dependency_p,
15984 enum tag_types tag_type,
15985 bool is_declaration)
15987 tree templ;
15988 tree arguments;
15989 tree template_id;
15990 cp_token_position start_of_id = 0;
15991 cp_token *next_token = NULL, *next_token_2 = NULL;
15992 bool is_identifier;
15994 /* If the next token corresponds to a template-id, there is no need
15995 to reparse it. */
15996 cp_token *token = cp_lexer_peek_token (parser->lexer);
15997 if (token->type == CPP_TEMPLATE_ID)
15999 cp_lexer_consume_token (parser->lexer);
16000 return saved_checks_value (token->u.tree_check_value);
16003 /* Avoid performing name lookup if there is no possibility of
16004 finding a template-id. */
16005 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16006 || (token->type == CPP_NAME
16007 && !cp_parser_nth_token_starts_template_argument_list_p
16008 (parser, 2)))
16010 cp_parser_error (parser, "expected template-id");
16011 return error_mark_node;
16014 /* Remember where the template-id starts. */
16015 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16016 start_of_id = cp_lexer_token_position (parser->lexer, false);
16018 push_deferring_access_checks (dk_deferred);
16020 /* Parse the template-name. */
16021 is_identifier = false;
16022 templ = cp_parser_template_name (parser, template_keyword_p,
16023 check_dependency_p,
16024 is_declaration,
16025 tag_type,
16026 &is_identifier);
16027 if (templ == error_mark_node || is_identifier)
16029 pop_deferring_access_checks ();
16030 return templ;
16033 /* Since we're going to preserve any side-effects from this parse, set up a
16034 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16035 in the template arguments. */
16036 tentative_firewall firewall (parser);
16038 /* If we find the sequence `[:' after a template-name, it's probably
16039 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16040 parse correctly the argument list. */
16041 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16042 == CPP_OPEN_SQUARE)
16043 && next_token->flags & DIGRAPH
16044 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16045 == CPP_COLON)
16046 && !(next_token_2->flags & PREV_WHITE))
16048 cp_parser_parse_tentatively (parser);
16049 /* Change `:' into `::'. */
16050 next_token_2->type = CPP_SCOPE;
16051 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16052 CPP_LESS. */
16053 cp_lexer_consume_token (parser->lexer);
16055 /* Parse the arguments. */
16056 arguments = cp_parser_enclosed_template_argument_list (parser);
16057 if (!cp_parser_parse_definitely (parser))
16059 /* If we couldn't parse an argument list, then we revert our changes
16060 and return simply an error. Maybe this is not a template-id
16061 after all. */
16062 next_token_2->type = CPP_COLON;
16063 cp_parser_error (parser, "expected %<<%>");
16064 pop_deferring_access_checks ();
16065 return error_mark_node;
16067 /* Otherwise, emit an error about the invalid digraph, but continue
16068 parsing because we got our argument list. */
16069 if (permerror (next_token->location,
16070 "%<<::%> cannot begin a template-argument list"))
16072 static bool hint = false;
16073 inform (next_token->location,
16074 "%<<:%> is an alternate spelling for %<[%>."
16075 " Insert whitespace between %<<%> and %<::%>");
16076 if (!hint && !flag_permissive)
16078 inform (next_token->location, "(if you use %<-fpermissive%> "
16079 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16080 "accept your code)");
16081 hint = true;
16085 else
16087 /* Look for the `<' that starts the template-argument-list. */
16088 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16090 pop_deferring_access_checks ();
16091 return error_mark_node;
16093 /* Parse the arguments. */
16094 arguments = cp_parser_enclosed_template_argument_list (parser);
16096 if ((cxx_dialect > cxx17)
16097 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16098 && !template_keyword_p
16099 && (cp_parser_error_occurred (parser)
16100 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16102 /* This didn't go well. */
16103 if (TREE_CODE (templ) == FUNCTION_DECL)
16105 /* C++2A says that "function-name < a;" is now ill-formed. */
16106 if (cp_parser_error_occurred (parser))
16108 error_at (token->location, "invalid template-argument-list");
16109 inform (token->location, "function name as the left hand "
16110 "operand of %<<%> is ill-formed in C++2a; wrap the "
16111 "function name in %<()%>");
16113 else
16114 /* We expect "f<targs>" to be followed by "(args)". */
16115 error_at (cp_lexer_peek_token (parser->lexer)->location,
16116 "expected %<(%> after template-argument-list");
16117 if (start_of_id)
16118 /* Purge all subsequent tokens. */
16119 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16121 else
16122 cp_parser_simulate_error (parser);
16123 pop_deferring_access_checks ();
16124 return error_mark_node;
16128 /* Set the location to be of the form:
16129 template-name < template-argument-list [opt] >
16130 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16131 with caret == start at the start of the template-name,
16132 ranging until the closing '>'. */
16133 location_t finish_loc
16134 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16135 location_t combined_loc
16136 = make_location (token->location, token->location, finish_loc);
16138 /* Check for concepts autos where they don't belong. We could
16139 identify types in some cases of idnetifier TEMPL, looking ahead
16140 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16141 types. We reject them in functions, but if what we have is an
16142 identifier, even with none_type we can't conclude it's NOT a
16143 type, we have to wait for template substitution. */
16144 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16145 template_id = error_mark_node;
16146 /* Build a representation of the specialization. */
16147 else if (identifier_p (templ))
16148 template_id = build_min_nt_loc (combined_loc,
16149 TEMPLATE_ID_EXPR,
16150 templ, arguments);
16151 else if (DECL_TYPE_TEMPLATE_P (templ)
16152 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16154 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16155 template (rather than some instantiation thereof) only if
16156 is not nested within some other construct. For example, in
16157 "template <typename T> void f(T) { A<T>::", A<T> is just an
16158 instantiation of A. */
16159 bool entering_scope
16160 = (template_parm_scope_p ()
16161 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16162 template_id
16163 = finish_template_type (templ, arguments, entering_scope);
16165 /* A template-like identifier may be a partial concept id. */
16166 else if (flag_concepts
16167 && (template_id = (cp_parser_maybe_partial_concept_id
16168 (parser, templ, arguments))))
16169 return template_id;
16170 else if (variable_template_p (templ))
16172 template_id = lookup_template_variable (templ, arguments);
16173 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16174 SET_EXPR_LOCATION (template_id, combined_loc);
16176 else
16178 /* If it's not a class-template or a template-template, it should be
16179 a function-template. */
16180 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16181 || TREE_CODE (templ) == OVERLOAD
16182 || TREE_CODE (templ) == FUNCTION_DECL
16183 || BASELINK_P (templ)));
16185 template_id = lookup_template_function (templ, arguments);
16186 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16187 SET_EXPR_LOCATION (template_id, combined_loc);
16190 /* If parsing tentatively, replace the sequence of tokens that makes
16191 up the template-id with a CPP_TEMPLATE_ID token. That way,
16192 should we re-parse the token stream, we will not have to repeat
16193 the effort required to do the parse, nor will we issue duplicate
16194 error messages about problems during instantiation of the
16195 template. */
16196 if (start_of_id
16197 /* Don't do this if we had a parse error in a declarator; re-parsing
16198 might succeed if a name changes meaning (60361). */
16199 && !(cp_parser_error_occurred (parser)
16200 && cp_parser_parsing_tentatively (parser)
16201 && parser->in_declarator_p))
16203 /* Reset the contents of the START_OF_ID token. */
16204 token->type = CPP_TEMPLATE_ID;
16205 token->location = combined_loc;
16207 /* Retrieve any deferred checks. Do not pop this access checks yet
16208 so the memory will not be reclaimed during token replacing below. */
16209 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16210 token->u.tree_check_value->value = template_id;
16211 token->u.tree_check_value->checks = get_deferred_access_checks ();
16212 token->keyword = RID_MAX;
16214 /* Purge all subsequent tokens. */
16215 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16217 /* ??? Can we actually assume that, if template_id ==
16218 error_mark_node, we will have issued a diagnostic to the
16219 user, as opposed to simply marking the tentative parse as
16220 failed? */
16221 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16222 error_at (token->location, "parse error in template argument list");
16225 pop_to_parent_deferring_access_checks ();
16226 return template_id;
16229 /* Parse a template-name.
16231 template-name:
16232 identifier
16234 The standard should actually say:
16236 template-name:
16237 identifier
16238 operator-function-id
16240 A defect report has been filed about this issue.
16242 A conversion-function-id cannot be a template name because they cannot
16243 be part of a template-id. In fact, looking at this code:
16245 a.operator K<int>()
16247 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16248 It is impossible to call a templated conversion-function-id with an
16249 explicit argument list, since the only allowed template parameter is
16250 the type to which it is converting.
16252 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16253 `template' keyword, in a construction like:
16255 T::template f<3>()
16257 In that case `f' is taken to be a template-name, even though there
16258 is no way of knowing for sure.
16260 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16261 name refers to a set of overloaded functions, at least one of which
16262 is a template, or an IDENTIFIER_NODE with the name of the template,
16263 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16264 names are looked up inside uninstantiated templates. */
16266 static tree
16267 cp_parser_template_name (cp_parser* parser,
16268 bool template_keyword_p,
16269 bool check_dependency_p,
16270 bool is_declaration,
16271 enum tag_types tag_type,
16272 bool *is_identifier)
16274 tree identifier;
16275 tree decl;
16276 cp_token *token = cp_lexer_peek_token (parser->lexer);
16278 /* If the next token is `operator', then we have either an
16279 operator-function-id or a conversion-function-id. */
16280 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16282 /* We don't know whether we're looking at an
16283 operator-function-id or a conversion-function-id. */
16284 cp_parser_parse_tentatively (parser);
16285 /* Try an operator-function-id. */
16286 identifier = cp_parser_operator_function_id (parser);
16287 /* If that didn't work, try a conversion-function-id. */
16288 if (!cp_parser_parse_definitely (parser))
16290 cp_parser_error (parser, "expected template-name");
16291 return error_mark_node;
16294 /* Look for the identifier. */
16295 else
16296 identifier = cp_parser_identifier (parser);
16298 /* If we didn't find an identifier, we don't have a template-id. */
16299 if (identifier == error_mark_node)
16300 return error_mark_node;
16302 /* If the name immediately followed the `template' keyword, then it
16303 is a template-name. However, if the next token is not `<', then
16304 we do not treat it as a template-name, since it is not being used
16305 as part of a template-id. This enables us to handle constructs
16306 like:
16308 template <typename T> struct S { S(); };
16309 template <typename T> S<T>::S();
16311 correctly. We would treat `S' as a template -- if it were `S<T>'
16312 -- but we do not if there is no `<'. */
16314 if (processing_template_decl
16315 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16317 /* In a declaration, in a dependent context, we pretend that the
16318 "template" keyword was present in order to improve error
16319 recovery. For example, given:
16321 template <typename T> void f(T::X<int>);
16323 we want to treat "X<int>" as a template-id. */
16324 if (is_declaration
16325 && !template_keyword_p
16326 && parser->scope && TYPE_P (parser->scope)
16327 && check_dependency_p
16328 && dependent_scope_p (parser->scope)
16329 /* Do not do this for dtors (or ctors), since they never
16330 need the template keyword before their name. */
16331 && !constructor_name_p (identifier, parser->scope))
16333 cp_token_position start = 0;
16335 /* Explain what went wrong. */
16336 error_at (token->location, "non-template %qD used as template",
16337 identifier);
16338 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16339 parser->scope, identifier);
16340 /* If parsing tentatively, find the location of the "<" token. */
16341 if (cp_parser_simulate_error (parser))
16342 start = cp_lexer_token_position (parser->lexer, true);
16343 /* Parse the template arguments so that we can issue error
16344 messages about them. */
16345 cp_lexer_consume_token (parser->lexer);
16346 cp_parser_enclosed_template_argument_list (parser);
16347 /* Skip tokens until we find a good place from which to
16348 continue parsing. */
16349 cp_parser_skip_to_closing_parenthesis (parser,
16350 /*recovering=*/true,
16351 /*or_comma=*/true,
16352 /*consume_paren=*/false);
16353 /* If parsing tentatively, permanently remove the
16354 template argument list. That will prevent duplicate
16355 error messages from being issued about the missing
16356 "template" keyword. */
16357 if (start)
16358 cp_lexer_purge_tokens_after (parser->lexer, start);
16359 if (is_identifier)
16360 *is_identifier = true;
16361 parser->context->object_type = NULL_TREE;
16362 return identifier;
16365 /* If the "template" keyword is present, then there is generally
16366 no point in doing name-lookup, so we just return IDENTIFIER.
16367 But, if the qualifying scope is non-dependent then we can
16368 (and must) do name-lookup normally. */
16369 if (template_keyword_p)
16371 tree scope = (parser->scope ? parser->scope
16372 : parser->context->object_type);
16373 if (scope && TYPE_P (scope)
16374 && (!CLASS_TYPE_P (scope)
16375 || (check_dependency_p && dependent_type_p (scope))))
16377 /* We're optimizing away the call to cp_parser_lookup_name, but
16378 we still need to do this. */
16379 parser->context->object_type = NULL_TREE;
16380 return identifier;
16385 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16386 const bool scoped_p = ((parser->scope ? parser->scope
16387 : parser->context->object_type) != NULL_TREE);
16389 /* Look up the name. */
16390 decl = cp_parser_lookup_name (parser, identifier,
16391 tag_type,
16392 /*is_template=*/true,
16393 /*is_namespace=*/false,
16394 check_dependency_p,
16395 /*ambiguous_decls=*/NULL,
16396 token->location);
16398 decl = strip_using_decl (decl);
16400 /* If DECL is a template, then the name was a template-name. */
16401 if (TREE_CODE (decl) == TEMPLATE_DECL)
16403 if (TREE_DEPRECATED (decl)
16404 && deprecated_state != DEPRECATED_SUPPRESS)
16405 warn_deprecated_use (decl, NULL_TREE);
16407 else
16409 /* The standard does not explicitly indicate whether a name that
16410 names a set of overloaded declarations, some of which are
16411 templates, is a template-name. However, such a name should
16412 be a template-name; otherwise, there is no way to form a
16413 template-id for the overloaded templates. */
16414 bool found = false;
16416 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16417 !found && iter; ++iter)
16418 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16419 found = true;
16421 if (!found
16422 && (cxx_dialect > cxx17)
16423 && !scoped_p
16424 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16426 /* [temp.names] says "A name is also considered to refer to a template
16427 if it is an unqualified-id followed by a < and name lookup finds
16428 either one or more functions or finds nothing." */
16430 /* The "more functions" case. Just use the OVERLOAD as normally.
16431 We don't use is_overloaded_fn here to avoid considering
16432 BASELINKs. */
16433 if (TREE_CODE (decl) == OVERLOAD
16434 /* Name lookup found one function. */
16435 || TREE_CODE (decl) == FUNCTION_DECL)
16436 found = true;
16437 /* Name lookup found nothing. */
16438 else if (decl == error_mark_node)
16439 return identifier;
16442 if (!found)
16444 /* The name does not name a template. */
16445 cp_parser_error (parser, "expected template-name");
16446 return error_mark_node;
16450 return decl;
16453 /* Parse a template-argument-list.
16455 template-argument-list:
16456 template-argument ... [opt]
16457 template-argument-list , template-argument ... [opt]
16459 Returns a TREE_VEC containing the arguments. */
16461 static tree
16462 cp_parser_template_argument_list (cp_parser* parser)
16464 tree fixed_args[10];
16465 unsigned n_args = 0;
16466 unsigned alloced = 10;
16467 tree *arg_ary = fixed_args;
16468 tree vec;
16469 bool saved_in_template_argument_list_p;
16470 bool saved_ice_p;
16471 bool saved_non_ice_p;
16473 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16474 parser->in_template_argument_list_p = true;
16475 /* Even if the template-id appears in an integral
16476 constant-expression, the contents of the argument list do
16477 not. */
16478 saved_ice_p = parser->integral_constant_expression_p;
16479 parser->integral_constant_expression_p = false;
16480 saved_non_ice_p = parser->non_integral_constant_expression_p;
16481 parser->non_integral_constant_expression_p = false;
16483 /* Parse the arguments. */
16486 tree argument;
16488 if (n_args)
16489 /* Consume the comma. */
16490 cp_lexer_consume_token (parser->lexer);
16492 /* Parse the template-argument. */
16493 argument = cp_parser_template_argument (parser);
16495 /* If the next token is an ellipsis, we're expanding a template
16496 argument pack. */
16497 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16499 if (argument == error_mark_node)
16501 cp_token *token = cp_lexer_peek_token (parser->lexer);
16502 error_at (token->location,
16503 "expected parameter pack before %<...%>");
16505 /* Consume the `...' token. */
16506 cp_lexer_consume_token (parser->lexer);
16508 /* Make the argument into a TYPE_PACK_EXPANSION or
16509 EXPR_PACK_EXPANSION. */
16510 argument = make_pack_expansion (argument);
16513 if (n_args == alloced)
16515 alloced *= 2;
16517 if (arg_ary == fixed_args)
16519 arg_ary = XNEWVEC (tree, alloced);
16520 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16522 else
16523 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16525 arg_ary[n_args++] = argument;
16527 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16529 vec = make_tree_vec (n_args);
16531 while (n_args--)
16532 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16534 if (arg_ary != fixed_args)
16535 free (arg_ary);
16536 parser->non_integral_constant_expression_p = saved_non_ice_p;
16537 parser->integral_constant_expression_p = saved_ice_p;
16538 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16539 if (CHECKING_P)
16540 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16541 return vec;
16544 /* Parse a template-argument.
16546 template-argument:
16547 assignment-expression
16548 type-id
16549 id-expression
16551 The representation is that of an assignment-expression, type-id, or
16552 id-expression -- except that the qualified id-expression is
16553 evaluated, so that the value returned is either a DECL or an
16554 OVERLOAD.
16556 Although the standard says "assignment-expression", it forbids
16557 throw-expressions or assignments in the template argument.
16558 Therefore, we use "conditional-expression" instead. */
16560 static tree
16561 cp_parser_template_argument (cp_parser* parser)
16563 tree argument;
16564 bool template_p;
16565 bool address_p;
16566 bool maybe_type_id = false;
16567 cp_token *token = NULL, *argument_start_token = NULL;
16568 location_t loc = 0;
16569 cp_id_kind idk;
16571 /* There's really no way to know what we're looking at, so we just
16572 try each alternative in order.
16574 [temp.arg]
16576 In a template-argument, an ambiguity between a type-id and an
16577 expression is resolved to a type-id, regardless of the form of
16578 the corresponding template-parameter.
16580 Therefore, we try a type-id first. */
16581 cp_parser_parse_tentatively (parser);
16582 argument = cp_parser_template_type_arg (parser);
16583 /* If there was no error parsing the type-id but the next token is a
16584 '>>', our behavior depends on which dialect of C++ we're
16585 parsing. In C++98, we probably found a typo for '> >'. But there
16586 are type-id which are also valid expressions. For instance:
16588 struct X { int operator >> (int); };
16589 template <int V> struct Foo {};
16590 Foo<X () >> 5> r;
16592 Here 'X()' is a valid type-id of a function type, but the user just
16593 wanted to write the expression "X() >> 5". Thus, we remember that we
16594 found a valid type-id, but we still try to parse the argument as an
16595 expression to see what happens.
16597 In C++0x, the '>>' will be considered two separate '>'
16598 tokens. */
16599 if (!cp_parser_error_occurred (parser)
16600 && cxx_dialect == cxx98
16601 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16603 maybe_type_id = true;
16604 cp_parser_abort_tentative_parse (parser);
16606 else
16608 /* If the next token isn't a `,' or a `>', then this argument wasn't
16609 really finished. This means that the argument is not a valid
16610 type-id. */
16611 if (!cp_parser_next_token_ends_template_argument_p (parser))
16612 cp_parser_error (parser, "expected template-argument");
16613 /* If that worked, we're done. */
16614 if (cp_parser_parse_definitely (parser))
16615 return argument;
16617 /* We're still not sure what the argument will be. */
16618 cp_parser_parse_tentatively (parser);
16619 /* Try a template. */
16620 argument_start_token = cp_lexer_peek_token (parser->lexer);
16621 argument = cp_parser_id_expression (parser,
16622 /*template_keyword_p=*/false,
16623 /*check_dependency_p=*/true,
16624 &template_p,
16625 /*declarator_p=*/false,
16626 /*optional_p=*/false);
16627 /* If the next token isn't a `,' or a `>', then this argument wasn't
16628 really finished. */
16629 if (!cp_parser_next_token_ends_template_argument_p (parser))
16630 cp_parser_error (parser, "expected template-argument");
16631 if (!cp_parser_error_occurred (parser))
16633 /* Figure out what is being referred to. If the id-expression
16634 was for a class template specialization, then we will have a
16635 TYPE_DECL at this point. There is no need to do name lookup
16636 at this point in that case. */
16637 if (TREE_CODE (argument) != TYPE_DECL)
16638 argument = cp_parser_lookup_name (parser, argument,
16639 none_type,
16640 /*is_template=*/template_p,
16641 /*is_namespace=*/false,
16642 /*check_dependency=*/true,
16643 /*ambiguous_decls=*/NULL,
16644 argument_start_token->location);
16645 /* Handle a constrained-type-specifier for a non-type template
16646 parameter. */
16647 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16648 argument = decl;
16649 else if (TREE_CODE (argument) != TEMPLATE_DECL
16650 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16651 cp_parser_error (parser, "expected template-name");
16653 if (cp_parser_parse_definitely (parser))
16655 if (TREE_DEPRECATED (argument))
16656 warn_deprecated_use (argument, NULL_TREE);
16657 return argument;
16659 /* It must be a non-type argument. In C++17 any constant-expression is
16660 allowed. */
16661 if (cxx_dialect > cxx14)
16662 goto general_expr;
16664 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16666 -- an integral constant-expression of integral or enumeration
16667 type; or
16669 -- the name of a non-type template-parameter; or
16671 -- the name of an object or function with external linkage...
16673 -- the address of an object or function with external linkage...
16675 -- a pointer to member... */
16676 /* Look for a non-type template parameter. */
16677 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16679 cp_parser_parse_tentatively (parser);
16680 argument = cp_parser_primary_expression (parser,
16681 /*address_p=*/false,
16682 /*cast_p=*/false,
16683 /*template_arg_p=*/true,
16684 &idk);
16685 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16686 || !cp_parser_next_token_ends_template_argument_p (parser))
16687 cp_parser_simulate_error (parser);
16688 if (cp_parser_parse_definitely (parser))
16689 return argument;
16692 /* If the next token is "&", the argument must be the address of an
16693 object or function with external linkage. */
16694 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16695 if (address_p)
16697 loc = cp_lexer_peek_token (parser->lexer)->location;
16698 cp_lexer_consume_token (parser->lexer);
16700 /* See if we might have an id-expression. */
16701 token = cp_lexer_peek_token (parser->lexer);
16702 if (token->type == CPP_NAME
16703 || token->keyword == RID_OPERATOR
16704 || token->type == CPP_SCOPE
16705 || token->type == CPP_TEMPLATE_ID
16706 || token->type == CPP_NESTED_NAME_SPECIFIER)
16708 cp_parser_parse_tentatively (parser);
16709 argument = cp_parser_primary_expression (parser,
16710 address_p,
16711 /*cast_p=*/false,
16712 /*template_arg_p=*/true,
16713 &idk);
16714 if (cp_parser_error_occurred (parser)
16715 || !cp_parser_next_token_ends_template_argument_p (parser))
16716 cp_parser_abort_tentative_parse (parser);
16717 else
16719 tree probe;
16721 if (INDIRECT_REF_P (argument))
16723 /* Strip the dereference temporarily. */
16724 gcc_assert (REFERENCE_REF_P (argument));
16725 argument = TREE_OPERAND (argument, 0);
16728 /* If we're in a template, we represent a qualified-id referring
16729 to a static data member as a SCOPE_REF even if the scope isn't
16730 dependent so that we can check access control later. */
16731 probe = argument;
16732 if (TREE_CODE (probe) == SCOPE_REF)
16733 probe = TREE_OPERAND (probe, 1);
16734 if (VAR_P (probe))
16736 /* A variable without external linkage might still be a
16737 valid constant-expression, so no error is issued here
16738 if the external-linkage check fails. */
16739 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16740 cp_parser_simulate_error (parser);
16742 else if (is_overloaded_fn (argument))
16743 /* All overloaded functions are allowed; if the external
16744 linkage test does not pass, an error will be issued
16745 later. */
16747 else if (address_p
16748 && (TREE_CODE (argument) == OFFSET_REF
16749 || TREE_CODE (argument) == SCOPE_REF))
16750 /* A pointer-to-member. */
16752 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16754 else
16755 cp_parser_simulate_error (parser);
16757 if (cp_parser_parse_definitely (parser))
16759 if (address_p)
16760 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16761 tf_warning_or_error);
16762 else
16763 argument = convert_from_reference (argument);
16764 return argument;
16768 /* If the argument started with "&", there are no other valid
16769 alternatives at this point. */
16770 if (address_p)
16772 cp_parser_error (parser, "invalid non-type template argument");
16773 return error_mark_node;
16776 general_expr:
16777 /* If the argument wasn't successfully parsed as a type-id followed
16778 by '>>', the argument can only be a constant expression now.
16779 Otherwise, we try parsing the constant-expression tentatively,
16780 because the argument could really be a type-id. */
16781 if (maybe_type_id)
16782 cp_parser_parse_tentatively (parser);
16784 if (cxx_dialect <= cxx14)
16785 argument = cp_parser_constant_expression (parser);
16786 else
16788 /* With C++17 generalized non-type template arguments we need to handle
16789 lvalue constant expressions, too. */
16790 argument = cp_parser_assignment_expression (parser);
16791 require_potential_constant_expression (argument);
16794 if (!maybe_type_id)
16795 return argument;
16796 if (!cp_parser_next_token_ends_template_argument_p (parser))
16797 cp_parser_error (parser, "expected template-argument");
16798 if (cp_parser_parse_definitely (parser))
16799 return argument;
16800 /* We did our best to parse the argument as a non type-id, but that
16801 was the only alternative that matched (albeit with a '>' after
16802 it). We can assume it's just a typo from the user, and a
16803 diagnostic will then be issued. */
16804 return cp_parser_template_type_arg (parser);
16807 /* Parse an explicit-instantiation.
16809 explicit-instantiation:
16810 template declaration
16812 Although the standard says `declaration', what it really means is:
16814 explicit-instantiation:
16815 template decl-specifier-seq [opt] declarator [opt] ;
16817 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16818 supposed to be allowed. A defect report has been filed about this
16819 issue.
16821 GNU Extension:
16823 explicit-instantiation:
16824 storage-class-specifier template
16825 decl-specifier-seq [opt] declarator [opt] ;
16826 function-specifier template
16827 decl-specifier-seq [opt] declarator [opt] ; */
16829 static void
16830 cp_parser_explicit_instantiation (cp_parser* parser)
16832 int declares_class_or_enum;
16833 cp_decl_specifier_seq decl_specifiers;
16834 tree extension_specifier = NULL_TREE;
16836 timevar_push (TV_TEMPLATE_INST);
16838 /* Look for an (optional) storage-class-specifier or
16839 function-specifier. */
16840 if (cp_parser_allow_gnu_extensions_p (parser))
16842 extension_specifier
16843 = cp_parser_storage_class_specifier_opt (parser);
16844 if (!extension_specifier)
16845 extension_specifier
16846 = cp_parser_function_specifier_opt (parser,
16847 /*decl_specs=*/NULL);
16850 /* Look for the `template' keyword. */
16851 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16852 /* Let the front end know that we are processing an explicit
16853 instantiation. */
16854 begin_explicit_instantiation ();
16855 /* [temp.explicit] says that we are supposed to ignore access
16856 control while processing explicit instantiation directives. */
16857 push_deferring_access_checks (dk_no_check);
16858 /* Parse a decl-specifier-seq. */
16859 cp_parser_decl_specifier_seq (parser,
16860 CP_PARSER_FLAGS_OPTIONAL,
16861 &decl_specifiers,
16862 &declares_class_or_enum);
16863 /* If there was exactly one decl-specifier, and it declared a class,
16864 and there's no declarator, then we have an explicit type
16865 instantiation. */
16866 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16868 tree type;
16870 type = check_tag_decl (&decl_specifiers,
16871 /*explicit_type_instantiation_p=*/true);
16872 /* Turn access control back on for names used during
16873 template instantiation. */
16874 pop_deferring_access_checks ();
16875 if (type)
16876 do_type_instantiation (type, extension_specifier,
16877 /*complain=*/tf_error);
16879 else
16881 cp_declarator *declarator;
16882 tree decl;
16884 /* Parse the declarator. */
16885 declarator
16886 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16887 /*ctor_dtor_or_conv_p=*/NULL,
16888 /*parenthesized_p=*/NULL,
16889 /*member_p=*/false,
16890 /*friend_p=*/false);
16891 if (declares_class_or_enum & 2)
16892 cp_parser_check_for_definition_in_return_type (declarator,
16893 decl_specifiers.type,
16894 decl_specifiers.locations[ds_type_spec]);
16895 if (declarator != cp_error_declarator)
16897 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16898 permerror (decl_specifiers.locations[ds_inline],
16899 "explicit instantiation shall not use"
16900 " %<inline%> specifier");
16901 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16902 permerror (decl_specifiers.locations[ds_constexpr],
16903 "explicit instantiation shall not use"
16904 " %<constexpr%> specifier");
16906 decl = grokdeclarator (declarator, &decl_specifiers,
16907 NORMAL, 0, &decl_specifiers.attributes);
16908 /* Turn access control back on for names used during
16909 template instantiation. */
16910 pop_deferring_access_checks ();
16911 /* Do the explicit instantiation. */
16912 do_decl_instantiation (decl, extension_specifier);
16914 else
16916 pop_deferring_access_checks ();
16917 /* Skip the body of the explicit instantiation. */
16918 cp_parser_skip_to_end_of_statement (parser);
16921 /* We're done with the instantiation. */
16922 end_explicit_instantiation ();
16924 cp_parser_consume_semicolon_at_end_of_statement (parser);
16926 timevar_pop (TV_TEMPLATE_INST);
16929 /* Parse an explicit-specialization.
16931 explicit-specialization:
16932 template < > declaration
16934 Although the standard says `declaration', what it really means is:
16936 explicit-specialization:
16937 template <> decl-specifier [opt] init-declarator [opt] ;
16938 template <> function-definition
16939 template <> explicit-specialization
16940 template <> template-declaration */
16942 static void
16943 cp_parser_explicit_specialization (cp_parser* parser)
16945 bool need_lang_pop;
16946 cp_token *token = cp_lexer_peek_token (parser->lexer);
16948 /* Look for the `template' keyword. */
16949 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16950 /* Look for the `<'. */
16951 cp_parser_require (parser, CPP_LESS, RT_LESS);
16952 /* Look for the `>'. */
16953 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16954 /* We have processed another parameter list. */
16955 ++parser->num_template_parameter_lists;
16956 /* [temp]
16958 A template ... explicit specialization ... shall not have C
16959 linkage. */
16960 if (current_lang_name == lang_name_c)
16962 error_at (token->location, "template specialization with C linkage");
16963 maybe_show_extern_c_location ();
16964 /* Give it C++ linkage to avoid confusing other parts of the
16965 front end. */
16966 push_lang_context (lang_name_cplusplus);
16967 need_lang_pop = true;
16969 else
16970 need_lang_pop = false;
16971 /* Let the front end know that we are beginning a specialization. */
16972 if (!begin_specialization ())
16974 end_specialization ();
16975 return;
16978 /* If the next keyword is `template', we need to figure out whether
16979 or not we're looking a template-declaration. */
16980 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16982 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16983 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16984 cp_parser_template_declaration_after_export (parser,
16985 /*member_p=*/false);
16986 else
16987 cp_parser_explicit_specialization (parser);
16989 else
16990 /* Parse the dependent declaration. */
16991 cp_parser_single_declaration (parser,
16992 /*checks=*/NULL,
16993 /*member_p=*/false,
16994 /*explicit_specialization_p=*/true,
16995 /*friend_p=*/NULL);
16996 /* We're done with the specialization. */
16997 end_specialization ();
16998 /* For the erroneous case of a template with C linkage, we pushed an
16999 implicit C++ linkage scope; exit that scope now. */
17000 if (need_lang_pop)
17001 pop_lang_context ();
17002 /* We're done with this parameter list. */
17003 --parser->num_template_parameter_lists;
17006 /* Parse a type-specifier.
17008 type-specifier:
17009 simple-type-specifier
17010 class-specifier
17011 enum-specifier
17012 elaborated-type-specifier
17013 cv-qualifier
17015 GNU Extension:
17017 type-specifier:
17018 __complex__
17020 Returns a representation of the type-specifier. For a
17021 class-specifier, enum-specifier, or elaborated-type-specifier, a
17022 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17024 The parser flags FLAGS is used to control type-specifier parsing.
17026 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17027 in a decl-specifier-seq.
17029 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17030 class-specifier, enum-specifier, or elaborated-type-specifier, then
17031 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17032 if a type is declared; 2 if it is defined. Otherwise, it is set to
17033 zero.
17035 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17036 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17037 is set to FALSE. */
17039 static tree
17040 cp_parser_type_specifier (cp_parser* parser,
17041 cp_parser_flags flags,
17042 cp_decl_specifier_seq *decl_specs,
17043 bool is_declaration,
17044 int* declares_class_or_enum,
17045 bool* is_cv_qualifier)
17047 tree type_spec = NULL_TREE;
17048 cp_token *token;
17049 enum rid keyword;
17050 cp_decl_spec ds = ds_last;
17052 /* Assume this type-specifier does not declare a new type. */
17053 if (declares_class_or_enum)
17054 *declares_class_or_enum = 0;
17055 /* And that it does not specify a cv-qualifier. */
17056 if (is_cv_qualifier)
17057 *is_cv_qualifier = false;
17058 /* Peek at the next token. */
17059 token = cp_lexer_peek_token (parser->lexer);
17061 /* If we're looking at a keyword, we can use that to guide the
17062 production we choose. */
17063 keyword = token->keyword;
17064 switch (keyword)
17066 case RID_ENUM:
17067 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17068 goto elaborated_type_specifier;
17070 /* Look for the enum-specifier. */
17071 type_spec = cp_parser_enum_specifier (parser);
17072 /* If that worked, we're done. */
17073 if (type_spec)
17075 if (declares_class_or_enum)
17076 *declares_class_or_enum = 2;
17077 if (decl_specs)
17078 cp_parser_set_decl_spec_type (decl_specs,
17079 type_spec,
17080 token,
17081 /*type_definition_p=*/true);
17082 return type_spec;
17084 else
17085 goto elaborated_type_specifier;
17087 /* Any of these indicate either a class-specifier, or an
17088 elaborated-type-specifier. */
17089 case RID_CLASS:
17090 case RID_STRUCT:
17091 case RID_UNION:
17092 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17093 goto elaborated_type_specifier;
17095 /* Parse tentatively so that we can back up if we don't find a
17096 class-specifier. */
17097 cp_parser_parse_tentatively (parser);
17098 /* Look for the class-specifier. */
17099 type_spec = cp_parser_class_specifier (parser);
17100 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17101 /* If that worked, we're done. */
17102 if (cp_parser_parse_definitely (parser))
17104 if (declares_class_or_enum)
17105 *declares_class_or_enum = 2;
17106 if (decl_specs)
17107 cp_parser_set_decl_spec_type (decl_specs,
17108 type_spec,
17109 token,
17110 /*type_definition_p=*/true);
17111 return type_spec;
17114 /* Fall through. */
17115 elaborated_type_specifier:
17116 /* We're declaring (not defining) a class or enum. */
17117 if (declares_class_or_enum)
17118 *declares_class_or_enum = 1;
17120 /* Fall through. */
17121 case RID_TYPENAME:
17122 /* Look for an elaborated-type-specifier. */
17123 type_spec
17124 = (cp_parser_elaborated_type_specifier
17125 (parser,
17126 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17127 is_declaration));
17128 if (decl_specs)
17129 cp_parser_set_decl_spec_type (decl_specs,
17130 type_spec,
17131 token,
17132 /*type_definition_p=*/false);
17133 return type_spec;
17135 case RID_CONST:
17136 ds = ds_const;
17137 if (is_cv_qualifier)
17138 *is_cv_qualifier = true;
17139 break;
17141 case RID_VOLATILE:
17142 ds = ds_volatile;
17143 if (is_cv_qualifier)
17144 *is_cv_qualifier = true;
17145 break;
17147 case RID_RESTRICT:
17148 ds = ds_restrict;
17149 if (is_cv_qualifier)
17150 *is_cv_qualifier = true;
17151 break;
17153 case RID_COMPLEX:
17154 /* The `__complex__' keyword is a GNU extension. */
17155 ds = ds_complex;
17156 break;
17158 default:
17159 break;
17162 /* Handle simple keywords. */
17163 if (ds != ds_last)
17165 if (decl_specs)
17167 set_and_check_decl_spec_loc (decl_specs, ds, token);
17168 decl_specs->any_specifiers_p = true;
17170 return cp_lexer_consume_token (parser->lexer)->u.value;
17173 /* If we do not already have a type-specifier, assume we are looking
17174 at a simple-type-specifier. */
17175 type_spec = cp_parser_simple_type_specifier (parser,
17176 decl_specs,
17177 flags);
17179 /* If we didn't find a type-specifier, and a type-specifier was not
17180 optional in this context, issue an error message. */
17181 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17183 cp_parser_error (parser, "expected type specifier");
17184 return error_mark_node;
17187 return type_spec;
17190 /* Parse a simple-type-specifier.
17192 simple-type-specifier:
17193 :: [opt] nested-name-specifier [opt] type-name
17194 :: [opt] nested-name-specifier template template-id
17195 char
17196 wchar_t
17197 bool
17198 short
17200 long
17201 signed
17202 unsigned
17203 float
17204 double
17205 void
17207 C++11 Extension:
17209 simple-type-specifier:
17210 auto
17211 decltype ( expression )
17212 char16_t
17213 char32_t
17214 __underlying_type ( type-id )
17216 C++17 extension:
17218 nested-name-specifier(opt) template-name
17220 GNU Extension:
17222 simple-type-specifier:
17223 __int128
17224 __typeof__ unary-expression
17225 __typeof__ ( type-id )
17226 __typeof__ ( type-id ) { initializer-list , [opt] }
17228 Concepts Extension:
17230 simple-type-specifier:
17231 constrained-type-specifier
17233 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17234 appropriately updated. */
17236 static tree
17237 cp_parser_simple_type_specifier (cp_parser* parser,
17238 cp_decl_specifier_seq *decl_specs,
17239 cp_parser_flags flags)
17241 tree type = NULL_TREE;
17242 cp_token *token;
17243 int idx;
17245 /* Peek at the next token. */
17246 token = cp_lexer_peek_token (parser->lexer);
17248 /* If we're looking at a keyword, things are easy. */
17249 switch (token->keyword)
17251 case RID_CHAR:
17252 if (decl_specs)
17253 decl_specs->explicit_char_p = true;
17254 type = char_type_node;
17255 break;
17256 case RID_CHAR16:
17257 type = char16_type_node;
17258 break;
17259 case RID_CHAR32:
17260 type = char32_type_node;
17261 break;
17262 case RID_WCHAR:
17263 type = wchar_type_node;
17264 break;
17265 case RID_BOOL:
17266 type = boolean_type_node;
17267 break;
17268 case RID_SHORT:
17269 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17270 type = short_integer_type_node;
17271 break;
17272 case RID_INT:
17273 if (decl_specs)
17274 decl_specs->explicit_int_p = true;
17275 type = integer_type_node;
17276 break;
17277 case RID_INT_N_0:
17278 case RID_INT_N_1:
17279 case RID_INT_N_2:
17280 case RID_INT_N_3:
17281 idx = token->keyword - RID_INT_N_0;
17282 if (! int_n_enabled_p [idx])
17283 break;
17284 if (decl_specs)
17286 decl_specs->explicit_intN_p = true;
17287 decl_specs->int_n_idx = idx;
17289 type = int_n_trees [idx].signed_type;
17290 break;
17291 case RID_LONG:
17292 if (decl_specs)
17293 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17294 type = long_integer_type_node;
17295 break;
17296 case RID_SIGNED:
17297 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17298 type = integer_type_node;
17299 break;
17300 case RID_UNSIGNED:
17301 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17302 type = unsigned_type_node;
17303 break;
17304 case RID_FLOAT:
17305 type = float_type_node;
17306 break;
17307 case RID_DOUBLE:
17308 type = double_type_node;
17309 break;
17310 case RID_VOID:
17311 type = void_type_node;
17312 break;
17314 case RID_AUTO:
17315 maybe_warn_cpp0x (CPP0X_AUTO);
17316 if (parser->auto_is_implicit_function_template_parm_p)
17318 /* The 'auto' might be the placeholder return type for a function decl
17319 with trailing return type. */
17320 bool have_trailing_return_fn_decl = false;
17322 cp_parser_parse_tentatively (parser);
17323 cp_lexer_consume_token (parser->lexer);
17324 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17325 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17326 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17327 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17329 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17331 cp_lexer_consume_token (parser->lexer);
17332 cp_parser_skip_to_closing_parenthesis (parser,
17333 /*recovering*/false,
17334 /*or_comma*/false,
17335 /*consume_paren*/true);
17336 continue;
17339 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17341 have_trailing_return_fn_decl = true;
17342 break;
17345 cp_lexer_consume_token (parser->lexer);
17347 cp_parser_abort_tentative_parse (parser);
17349 if (have_trailing_return_fn_decl)
17351 type = make_auto ();
17352 break;
17355 if (cxx_dialect >= cxx14)
17357 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17358 type = TREE_TYPE (type);
17360 else
17361 type = error_mark_node;
17363 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17365 if (cxx_dialect < cxx14)
17366 error_at (token->location,
17367 "use of %<auto%> in lambda parameter declaration "
17368 "only available with "
17369 "-std=c++14 or -std=gnu++14");
17371 else if (cxx_dialect < cxx14)
17372 error_at (token->location,
17373 "use of %<auto%> in parameter declaration "
17374 "only available with "
17375 "-std=c++14 or -std=gnu++14");
17376 else if (!flag_concepts)
17377 pedwarn (token->location, 0,
17378 "use of %<auto%> in parameter declaration "
17379 "only available with -fconcepts");
17381 else
17382 type = make_auto ();
17383 break;
17385 case RID_DECLTYPE:
17386 /* Since DR 743, decltype can either be a simple-type-specifier by
17387 itself or begin a nested-name-specifier. Parsing it will replace
17388 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17389 handling below decide what to do. */
17390 cp_parser_decltype (parser);
17391 cp_lexer_set_token_position (parser->lexer, token);
17392 break;
17394 case RID_TYPEOF:
17395 /* Consume the `typeof' token. */
17396 cp_lexer_consume_token (parser->lexer);
17397 /* Parse the operand to `typeof'. */
17398 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17399 /* If it is not already a TYPE, take its type. */
17400 if (!TYPE_P (type))
17401 type = finish_typeof (type);
17403 if (decl_specs)
17404 cp_parser_set_decl_spec_type (decl_specs, type,
17405 token,
17406 /*type_definition_p=*/false);
17408 return type;
17410 case RID_UNDERLYING_TYPE:
17411 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17412 if (decl_specs)
17413 cp_parser_set_decl_spec_type (decl_specs, type,
17414 token,
17415 /*type_definition_p=*/false);
17417 return type;
17419 case RID_BASES:
17420 case RID_DIRECT_BASES:
17421 type = cp_parser_trait_expr (parser, token->keyword);
17422 if (decl_specs)
17423 cp_parser_set_decl_spec_type (decl_specs, type,
17424 token,
17425 /*type_definition_p=*/false);
17426 return type;
17427 default:
17428 break;
17431 /* If token is an already-parsed decltype not followed by ::,
17432 it's a simple-type-specifier. */
17433 if (token->type == CPP_DECLTYPE
17434 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17436 type = saved_checks_value (token->u.tree_check_value);
17437 if (decl_specs)
17439 cp_parser_set_decl_spec_type (decl_specs, type,
17440 token,
17441 /*type_definition_p=*/false);
17442 /* Remember that we are handling a decltype in order to
17443 implement the resolution of DR 1510 when the argument
17444 isn't instantiation dependent. */
17445 decl_specs->decltype_p = true;
17447 cp_lexer_consume_token (parser->lexer);
17448 return type;
17451 /* If the type-specifier was for a built-in type, we're done. */
17452 if (type)
17454 /* Record the type. */
17455 if (decl_specs
17456 && (token->keyword != RID_SIGNED
17457 && token->keyword != RID_UNSIGNED
17458 && token->keyword != RID_SHORT
17459 && token->keyword != RID_LONG))
17460 cp_parser_set_decl_spec_type (decl_specs,
17461 type,
17462 token,
17463 /*type_definition_p=*/false);
17464 if (decl_specs)
17465 decl_specs->any_specifiers_p = true;
17467 /* Consume the token. */
17468 cp_lexer_consume_token (parser->lexer);
17470 if (type == error_mark_node)
17471 return error_mark_node;
17473 /* There is no valid C++ program where a non-template type is
17474 followed by a "<". That usually indicates that the user thought
17475 that the type was a template. */
17476 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17477 token->location);
17479 return TYPE_NAME (type);
17482 /* The type-specifier must be a user-defined type. */
17483 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17485 bool qualified_p;
17486 bool global_p;
17488 /* Don't gobble tokens or issue error messages if this is an
17489 optional type-specifier. */
17490 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17491 cp_parser_parse_tentatively (parser);
17493 token = cp_lexer_peek_token (parser->lexer);
17495 /* Look for the optional `::' operator. */
17496 global_p
17497 = (cp_parser_global_scope_opt (parser,
17498 /*current_scope_valid_p=*/false)
17499 != NULL_TREE);
17500 /* Look for the nested-name specifier. */
17501 qualified_p
17502 = (cp_parser_nested_name_specifier_opt (parser,
17503 /*typename_keyword_p=*/false,
17504 /*check_dependency_p=*/true,
17505 /*type_p=*/false,
17506 /*is_declaration=*/false)
17507 != NULL_TREE);
17508 /* If we have seen a nested-name-specifier, and the next token
17509 is `template', then we are using the template-id production. */
17510 if (parser->scope
17511 && cp_parser_optional_template_keyword (parser))
17513 /* Look for the template-id. */
17514 type = cp_parser_template_id (parser,
17515 /*template_keyword_p=*/true,
17516 /*check_dependency_p=*/true,
17517 none_type,
17518 /*is_declaration=*/false);
17519 /* If the template-id did not name a type, we are out of
17520 luck. */
17521 if (TREE_CODE (type) != TYPE_DECL)
17523 cp_parser_error (parser, "expected template-id for type");
17524 type = NULL_TREE;
17527 /* Otherwise, look for a type-name. */
17528 else
17529 type = cp_parser_type_name (parser);
17530 /* Keep track of all name-lookups performed in class scopes. */
17531 if (type
17532 && !global_p
17533 && !qualified_p
17534 && TREE_CODE (type) == TYPE_DECL
17535 && identifier_p (DECL_NAME (type)))
17536 maybe_note_name_used_in_class (DECL_NAME (type), type);
17537 /* If it didn't work out, we don't have a TYPE. */
17538 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17539 && !cp_parser_parse_definitely (parser))
17540 type = NULL_TREE;
17541 if (!type && cxx_dialect >= cxx17)
17543 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17544 cp_parser_parse_tentatively (parser);
17546 cp_parser_global_scope_opt (parser,
17547 /*current_scope_valid_p=*/false);
17548 cp_parser_nested_name_specifier_opt (parser,
17549 /*typename_keyword_p=*/false,
17550 /*check_dependency_p=*/true,
17551 /*type_p=*/false,
17552 /*is_declaration=*/false);
17553 tree name = cp_parser_identifier (parser);
17554 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17555 && parser->scope != error_mark_node)
17557 tree tmpl = cp_parser_lookup_name (parser, name,
17558 none_type,
17559 /*is_template=*/false,
17560 /*is_namespace=*/false,
17561 /*check_dependency=*/true,
17562 /*ambiguous_decls=*/NULL,
17563 token->location);
17564 if (tmpl && tmpl != error_mark_node
17565 && (DECL_CLASS_TEMPLATE_P (tmpl)
17566 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17567 type = make_template_placeholder (tmpl);
17568 else
17570 type = error_mark_node;
17571 if (!cp_parser_simulate_error (parser))
17572 cp_parser_name_lookup_error (parser, name, tmpl,
17573 NLE_TYPE, token->location);
17576 else
17577 type = error_mark_node;
17579 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17580 && !cp_parser_parse_definitely (parser))
17581 type = NULL_TREE;
17583 if (type && decl_specs)
17584 cp_parser_set_decl_spec_type (decl_specs, type,
17585 token,
17586 /*type_definition_p=*/false);
17589 /* If we didn't get a type-name, issue an error message. */
17590 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17592 cp_parser_error (parser, "expected type-name");
17593 return error_mark_node;
17596 if (type && type != error_mark_node)
17598 /* See if TYPE is an Objective-C type, and if so, parse and
17599 accept any protocol references following it. Do this before
17600 the cp_parser_check_for_invalid_template_id() call, because
17601 Objective-C types can be followed by '<...>' which would
17602 enclose protocol names rather than template arguments, and so
17603 everything is fine. */
17604 if (c_dialect_objc () && !parser->scope
17605 && (objc_is_id (type) || objc_is_class_name (type)))
17607 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17608 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17610 /* Clobber the "unqualified" type previously entered into
17611 DECL_SPECS with the new, improved protocol-qualified version. */
17612 if (decl_specs)
17613 decl_specs->type = qual_type;
17615 return qual_type;
17618 /* There is no valid C++ program where a non-template type is
17619 followed by a "<". That usually indicates that the user
17620 thought that the type was a template. */
17621 cp_parser_check_for_invalid_template_id (parser, type,
17622 none_type,
17623 token->location);
17626 return type;
17629 /* Parse a type-name.
17631 type-name:
17632 class-name
17633 enum-name
17634 typedef-name
17635 simple-template-id [in c++0x]
17637 enum-name:
17638 identifier
17640 typedef-name:
17641 identifier
17643 Concepts:
17645 type-name:
17646 concept-name
17647 partial-concept-id
17649 concept-name:
17650 identifier
17652 Returns a TYPE_DECL for the type. */
17654 static tree
17655 cp_parser_type_name (cp_parser* parser)
17657 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17660 /* See above. */
17661 static tree
17662 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17664 tree type_decl;
17666 /* We can't know yet whether it is a class-name or not. */
17667 cp_parser_parse_tentatively (parser);
17668 /* Try a class-name. */
17669 type_decl = cp_parser_class_name (parser,
17670 typename_keyword_p,
17671 /*template_keyword_p=*/false,
17672 none_type,
17673 /*check_dependency_p=*/true,
17674 /*class_head_p=*/false,
17675 /*is_declaration=*/false);
17676 /* If it's not a class-name, keep looking. */
17677 if (!cp_parser_parse_definitely (parser))
17679 if (cxx_dialect < cxx11)
17680 /* It must be a typedef-name or an enum-name. */
17681 return cp_parser_nonclass_name (parser);
17683 cp_parser_parse_tentatively (parser);
17684 /* It is either a simple-template-id representing an
17685 instantiation of an alias template... */
17686 type_decl = cp_parser_template_id (parser,
17687 /*template_keyword_p=*/false,
17688 /*check_dependency_p=*/true,
17689 none_type,
17690 /*is_declaration=*/false);
17691 /* Note that this must be an instantiation of an alias template
17692 because [temp.names]/6 says:
17694 A template-id that names an alias template specialization
17695 is a type-name.
17697 Whereas [temp.names]/7 says:
17699 A simple-template-id that names a class template
17700 specialization is a class-name.
17702 With concepts, this could also be a partial-concept-id that
17703 declares a non-type template parameter. */
17704 if (type_decl != NULL_TREE
17705 && TREE_CODE (type_decl) == TYPE_DECL
17706 && TYPE_DECL_ALIAS_P (type_decl))
17707 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17708 else if (is_constrained_parameter (type_decl))
17709 /* Don't do anything. */ ;
17710 else
17711 cp_parser_simulate_error (parser);
17713 if (!cp_parser_parse_definitely (parser))
17714 /* ... Or a typedef-name or an enum-name. */
17715 return cp_parser_nonclass_name (parser);
17718 return type_decl;
17721 /* Check if DECL and ARGS can form a constrained-type-specifier.
17722 If ARGS is non-null, we try to form a concept check of the
17723 form DECL<?, ARGS> where ? is a wildcard that matches any
17724 kind of template argument. If ARGS is NULL, then we try to
17725 form a concept check of the form DECL<?>. */
17727 static tree
17728 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17729 tree decl, tree args)
17731 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17733 /* If we a constrained-type-specifier cannot be deduced. */
17734 if (parser->prevent_constrained_type_specifiers)
17735 return NULL_TREE;
17737 /* A constrained type specifier can only be found in an
17738 overload set or as a reference to a template declaration.
17740 FIXME: This might be masking a bug. It's possible that
17741 that the deduction below is causing template specializations
17742 to be formed with the wildcard as an argument. */
17743 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17744 return NULL_TREE;
17746 /* Try to build a call expression that evaluates the
17747 concept. This can fail if the overload set refers
17748 only to non-templates. */
17749 tree placeholder = build_nt (WILDCARD_DECL);
17750 tree check = build_concept_check (decl, placeholder, args);
17751 if (check == error_mark_node)
17752 return NULL_TREE;
17754 /* Deduce the checked constraint and the prototype parameter.
17756 FIXME: In certain cases, failure to deduce should be a
17757 diagnosable error. */
17758 tree conc;
17759 tree proto;
17760 if (!deduce_constrained_parameter (check, conc, proto))
17761 return NULL_TREE;
17763 /* In template parameter scope, this results in a constrained
17764 parameter. Return a descriptor of that parm. */
17765 if (processing_template_parmlist)
17766 return build_constrained_parameter (conc, proto, args);
17768 /* In a parameter-declaration-clause, constrained-type
17769 specifiers result in invented template parameters. */
17770 if (parser->auto_is_implicit_function_template_parm_p)
17772 tree x = build_constrained_parameter (conc, proto, args);
17773 return synthesize_implicit_template_parm (parser, x);
17775 else
17777 /* Otherwise, we're in a context where the constrained
17778 type name is deduced and the constraint applies
17779 after deduction. */
17780 return make_constrained_auto (conc, args);
17783 return NULL_TREE;
17786 /* If DECL refers to a concept, return a TYPE_DECL representing
17787 the result of using the constrained type specifier in the
17788 current context. DECL refers to a concept if
17790 - it is an overload set containing a function concept taking a single
17791 type argument, or
17793 - it is a variable concept taking a single type argument. */
17795 static tree
17796 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17798 if (flag_concepts
17799 && (TREE_CODE (decl) == OVERLOAD
17800 || BASELINK_P (decl)
17801 || variable_concept_p (decl)))
17802 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17803 else
17804 return NULL_TREE;
17807 /* Check if DECL and ARGS form a partial-concept-id. If so,
17808 assign ID to the resulting constrained placeholder.
17810 Returns true if the partial-concept-id designates a placeholder
17811 and false otherwise. Note that *id is set to NULL_TREE in
17812 this case. */
17814 static tree
17815 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17817 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17820 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17821 or a concept-name.
17823 enum-name:
17824 identifier
17826 typedef-name:
17827 identifier
17829 concept-name:
17830 identifier
17832 Returns a TYPE_DECL for the type. */
17834 static tree
17835 cp_parser_nonclass_name (cp_parser* parser)
17837 tree type_decl;
17838 tree identifier;
17840 cp_token *token = cp_lexer_peek_token (parser->lexer);
17841 identifier = cp_parser_identifier (parser);
17842 if (identifier == error_mark_node)
17843 return error_mark_node;
17845 /* Look up the type-name. */
17846 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17848 type_decl = strip_using_decl (type_decl);
17850 /* If we found an overload set, then it may refer to a concept-name. */
17851 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17852 type_decl = decl;
17854 if (TREE_CODE (type_decl) != TYPE_DECL
17855 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17857 /* See if this is an Objective-C type. */
17858 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17859 tree type = objc_get_protocol_qualified_type (identifier, protos);
17860 if (type)
17861 type_decl = TYPE_NAME (type);
17864 /* Issue an error if we did not find a type-name. */
17865 if (TREE_CODE (type_decl) != TYPE_DECL
17866 /* In Objective-C, we have the complication that class names are
17867 normally type names and start declarations (eg, the
17868 "NSObject" in "NSObject *object;"), but can be used in an
17869 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17870 is an expression. So, a classname followed by a dot is not a
17871 valid type-name. */
17872 || (objc_is_class_name (TREE_TYPE (type_decl))
17873 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17875 if (!cp_parser_simulate_error (parser))
17876 cp_parser_name_lookup_error (parser, identifier, type_decl,
17877 NLE_TYPE, token->location);
17878 return error_mark_node;
17880 /* Remember that the name was used in the definition of the
17881 current class so that we can check later to see if the
17882 meaning would have been different after the class was
17883 entirely defined. */
17884 else if (type_decl != error_mark_node
17885 && !parser->scope)
17886 maybe_note_name_used_in_class (identifier, type_decl);
17888 return type_decl;
17891 /* Parse an elaborated-type-specifier. Note that the grammar given
17892 here incorporates the resolution to DR68.
17894 elaborated-type-specifier:
17895 class-key :: [opt] nested-name-specifier [opt] identifier
17896 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17897 enum-key :: [opt] nested-name-specifier [opt] identifier
17898 typename :: [opt] nested-name-specifier identifier
17899 typename :: [opt] nested-name-specifier template [opt]
17900 template-id
17902 GNU extension:
17904 elaborated-type-specifier:
17905 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17906 class-key attributes :: [opt] nested-name-specifier [opt]
17907 template [opt] template-id
17908 enum attributes :: [opt] nested-name-specifier [opt] identifier
17910 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17911 declared `friend'. If IS_DECLARATION is TRUE, then this
17912 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17913 something is being declared.
17915 Returns the TYPE specified. */
17917 static tree
17918 cp_parser_elaborated_type_specifier (cp_parser* parser,
17919 bool is_friend,
17920 bool is_declaration)
17922 enum tag_types tag_type;
17923 tree identifier;
17924 tree type = NULL_TREE;
17925 tree attributes = NULL_TREE;
17926 tree globalscope;
17927 cp_token *token = NULL;
17929 /* See if we're looking at the `enum' keyword. */
17930 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17932 /* Consume the `enum' token. */
17933 cp_lexer_consume_token (parser->lexer);
17934 /* Remember that it's an enumeration type. */
17935 tag_type = enum_type;
17936 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17937 enums) is used here. */
17938 cp_token *token = cp_lexer_peek_token (parser->lexer);
17939 if (cp_parser_is_keyword (token, RID_CLASS)
17940 || cp_parser_is_keyword (token, RID_STRUCT))
17942 gcc_rich_location richloc (token->location);
17943 richloc.add_range (input_location);
17944 richloc.add_fixit_remove ();
17945 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17946 "a scoped enum must not use the %qD keyword",
17947 token->u.value);
17948 /* Consume the `struct' or `class' and parse it anyway. */
17949 cp_lexer_consume_token (parser->lexer);
17951 /* Parse the attributes. */
17952 attributes = cp_parser_attributes_opt (parser);
17954 /* Or, it might be `typename'. */
17955 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17956 RID_TYPENAME))
17958 /* Consume the `typename' token. */
17959 cp_lexer_consume_token (parser->lexer);
17960 /* Remember that it's a `typename' type. */
17961 tag_type = typename_type;
17963 /* Otherwise it must be a class-key. */
17964 else
17966 tag_type = cp_parser_class_key (parser);
17967 if (tag_type == none_type)
17968 return error_mark_node;
17969 /* Parse the attributes. */
17970 attributes = cp_parser_attributes_opt (parser);
17973 /* Look for the `::' operator. */
17974 globalscope = cp_parser_global_scope_opt (parser,
17975 /*current_scope_valid_p=*/false);
17976 /* Look for the nested-name-specifier. */
17977 tree nested_name_specifier;
17978 if (tag_type == typename_type && !globalscope)
17980 nested_name_specifier
17981 = cp_parser_nested_name_specifier (parser,
17982 /*typename_keyword_p=*/true,
17983 /*check_dependency_p=*/true,
17984 /*type_p=*/true,
17985 is_declaration);
17986 if (!nested_name_specifier)
17987 return error_mark_node;
17989 else
17990 /* Even though `typename' is not present, the proposed resolution
17991 to Core Issue 180 says that in `class A<T>::B', `B' should be
17992 considered a type-name, even if `A<T>' is dependent. */
17993 nested_name_specifier
17994 = cp_parser_nested_name_specifier_opt (parser,
17995 /*typename_keyword_p=*/true,
17996 /*check_dependency_p=*/true,
17997 /*type_p=*/true,
17998 is_declaration);
17999 /* For everything but enumeration types, consider a template-id.
18000 For an enumeration type, consider only a plain identifier. */
18001 if (tag_type != enum_type)
18003 bool template_p = false;
18004 tree decl;
18006 /* Allow the `template' keyword. */
18007 template_p = cp_parser_optional_template_keyword (parser);
18008 /* If we didn't see `template', we don't know if there's a
18009 template-id or not. */
18010 if (!template_p)
18011 cp_parser_parse_tentatively (parser);
18012 /* Parse the template-id. */
18013 token = cp_lexer_peek_token (parser->lexer);
18014 decl = cp_parser_template_id (parser, template_p,
18015 /*check_dependency_p=*/true,
18016 tag_type,
18017 is_declaration);
18018 /* If we didn't find a template-id, look for an ordinary
18019 identifier. */
18020 if (!template_p && !cp_parser_parse_definitely (parser))
18022 /* We can get here when cp_parser_template_id, called by
18023 cp_parser_class_name with tag_type == none_type, succeeds
18024 and caches a BASELINK. Then, when called again here,
18025 instead of failing and returning an error_mark_node
18026 returns it (see template/typename17.C in C++11).
18027 ??? Could we diagnose this earlier? */
18028 else if (tag_type == typename_type && BASELINK_P (decl))
18030 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18031 type = error_mark_node;
18033 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18034 in effect, then we must assume that, upon instantiation, the
18035 template will correspond to a class. */
18036 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18037 && tag_type == typename_type)
18038 type = make_typename_type (parser->scope, decl,
18039 typename_type,
18040 /*complain=*/tf_error);
18041 /* If the `typename' keyword is in effect and DECL is not a type
18042 decl, then type is non existent. */
18043 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18045 else if (TREE_CODE (decl) == TYPE_DECL)
18047 type = check_elaborated_type_specifier (tag_type, decl,
18048 /*allow_template_p=*/true);
18050 /* If the next token is a semicolon, this must be a specialization,
18051 instantiation, or friend declaration. Check the scope while we
18052 still know whether or not we had a nested-name-specifier. */
18053 if (type != error_mark_node
18054 && !nested_name_specifier && !is_friend
18055 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18056 check_unqualified_spec_or_inst (type, token->location);
18058 else if (decl == error_mark_node)
18059 type = error_mark_node;
18062 if (!type)
18064 token = cp_lexer_peek_token (parser->lexer);
18065 identifier = cp_parser_identifier (parser);
18067 if (identifier == error_mark_node)
18069 parser->scope = NULL_TREE;
18070 return error_mark_node;
18073 /* For a `typename', we needn't call xref_tag. */
18074 if (tag_type == typename_type
18075 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18076 return cp_parser_make_typename_type (parser, identifier,
18077 token->location);
18079 /* Template parameter lists apply only if we are not within a
18080 function parameter list. */
18081 bool template_parm_lists_apply
18082 = parser->num_template_parameter_lists;
18083 if (template_parm_lists_apply)
18084 for (cp_binding_level *s = current_binding_level;
18085 s && s->kind != sk_template_parms;
18086 s = s->level_chain)
18087 if (s->kind == sk_function_parms)
18088 template_parm_lists_apply = false;
18090 /* Look up a qualified name in the usual way. */
18091 if (parser->scope)
18093 tree decl;
18094 tree ambiguous_decls;
18096 decl = cp_parser_lookup_name (parser, identifier,
18097 tag_type,
18098 /*is_template=*/false,
18099 /*is_namespace=*/false,
18100 /*check_dependency=*/true,
18101 &ambiguous_decls,
18102 token->location);
18104 /* If the lookup was ambiguous, an error will already have been
18105 issued. */
18106 if (ambiguous_decls)
18107 return error_mark_node;
18109 /* If we are parsing friend declaration, DECL may be a
18110 TEMPLATE_DECL tree node here. However, we need to check
18111 whether this TEMPLATE_DECL results in valid code. Consider
18112 the following example:
18114 namespace N {
18115 template <class T> class C {};
18117 class X {
18118 template <class T> friend class N::C; // #1, valid code
18120 template <class T> class Y {
18121 friend class N::C; // #2, invalid code
18124 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18125 name lookup of `N::C'. We see that friend declaration must
18126 be template for the code to be valid. Note that
18127 processing_template_decl does not work here since it is
18128 always 1 for the above two cases. */
18130 decl = (cp_parser_maybe_treat_template_as_class
18131 (decl, /*tag_name_p=*/is_friend
18132 && template_parm_lists_apply));
18134 if (TREE_CODE (decl) != TYPE_DECL)
18136 cp_parser_diagnose_invalid_type_name (parser,
18137 identifier,
18138 token->location);
18139 return error_mark_node;
18142 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18144 bool allow_template = (template_parm_lists_apply
18145 || DECL_SELF_REFERENCE_P (decl));
18146 type = check_elaborated_type_specifier (tag_type, decl,
18147 allow_template);
18149 if (type == error_mark_node)
18150 return error_mark_node;
18153 /* Forward declarations of nested types, such as
18155 class C1::C2;
18156 class C1::C2::C3;
18158 are invalid unless all components preceding the final '::'
18159 are complete. If all enclosing types are complete, these
18160 declarations become merely pointless.
18162 Invalid forward declarations of nested types are errors
18163 caught elsewhere in parsing. Those that are pointless arrive
18164 here. */
18166 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18167 && !is_friend && !processing_explicit_instantiation)
18168 warning (0, "declaration %qD does not declare anything", decl);
18170 type = TREE_TYPE (decl);
18172 else
18174 /* An elaborated-type-specifier sometimes introduces a new type and
18175 sometimes names an existing type. Normally, the rule is that it
18176 introduces a new type only if there is not an existing type of
18177 the same name already in scope. For example, given:
18179 struct S {};
18180 void f() { struct S s; }
18182 the `struct S' in the body of `f' is the same `struct S' as in
18183 the global scope; the existing definition is used. However, if
18184 there were no global declaration, this would introduce a new
18185 local class named `S'.
18187 An exception to this rule applies to the following code:
18189 namespace N { struct S; }
18191 Here, the elaborated-type-specifier names a new type
18192 unconditionally; even if there is already an `S' in the
18193 containing scope this declaration names a new type.
18194 This exception only applies if the elaborated-type-specifier
18195 forms the complete declaration:
18197 [class.name]
18199 A declaration consisting solely of `class-key identifier ;' is
18200 either a redeclaration of the name in the current scope or a
18201 forward declaration of the identifier as a class name. It
18202 introduces the name into the current scope.
18204 We are in this situation precisely when the next token is a `;'.
18206 An exception to the exception is that a `friend' declaration does
18207 *not* name a new type; i.e., given:
18209 struct S { friend struct T; };
18211 `T' is not a new type in the scope of `S'.
18213 Also, `new struct S' or `sizeof (struct S)' never results in the
18214 definition of a new type; a new type can only be declared in a
18215 declaration context. */
18217 tag_scope ts;
18218 bool template_p;
18220 if (is_friend)
18221 /* Friends have special name lookup rules. */
18222 ts = ts_within_enclosing_non_class;
18223 else if (is_declaration
18224 && cp_lexer_next_token_is (parser->lexer,
18225 CPP_SEMICOLON))
18226 /* This is a `class-key identifier ;' */
18227 ts = ts_current;
18228 else
18229 ts = ts_global;
18231 template_p =
18232 (template_parm_lists_apply
18233 && (cp_parser_next_token_starts_class_definition_p (parser)
18234 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18235 /* An unqualified name was used to reference this type, so
18236 there were no qualifying templates. */
18237 if (template_parm_lists_apply
18238 && !cp_parser_check_template_parameters (parser,
18239 /*num_templates=*/0,
18240 /*template_id*/false,
18241 token->location,
18242 /*declarator=*/NULL))
18243 return error_mark_node;
18244 type = xref_tag (tag_type, identifier, ts, template_p);
18248 if (type == error_mark_node)
18249 return error_mark_node;
18251 /* Allow attributes on forward declarations of classes. */
18252 if (attributes)
18254 if (TREE_CODE (type) == TYPENAME_TYPE)
18255 warning (OPT_Wattributes,
18256 "attributes ignored on uninstantiated type");
18257 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18258 && ! processing_explicit_instantiation)
18259 warning (OPT_Wattributes,
18260 "attributes ignored on template instantiation");
18261 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18262 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18263 else
18264 warning (OPT_Wattributes,
18265 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18268 if (tag_type != enum_type)
18270 /* Indicate whether this class was declared as a `class' or as a
18271 `struct'. */
18272 if (CLASS_TYPE_P (type))
18273 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18274 cp_parser_check_class_key (tag_type, type);
18277 /* A "<" cannot follow an elaborated type specifier. If that
18278 happens, the user was probably trying to form a template-id. */
18279 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18280 token->location);
18282 return type;
18285 /* Parse an enum-specifier.
18287 enum-specifier:
18288 enum-head { enumerator-list [opt] }
18289 enum-head { enumerator-list , } [C++0x]
18291 enum-head:
18292 enum-key identifier [opt] enum-base [opt]
18293 enum-key nested-name-specifier identifier enum-base [opt]
18295 enum-key:
18296 enum
18297 enum class [C++0x]
18298 enum struct [C++0x]
18300 enum-base: [C++0x]
18301 : type-specifier-seq
18303 opaque-enum-specifier:
18304 enum-key identifier enum-base [opt] ;
18306 GNU Extensions:
18307 enum-key attributes[opt] identifier [opt] enum-base [opt]
18308 { enumerator-list [opt] }attributes[opt]
18309 enum-key attributes[opt] identifier [opt] enum-base [opt]
18310 { enumerator-list, }attributes[opt] [C++0x]
18312 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18313 if the token stream isn't an enum-specifier after all. */
18315 static tree
18316 cp_parser_enum_specifier (cp_parser* parser)
18318 tree identifier;
18319 tree type = NULL_TREE;
18320 tree prev_scope;
18321 tree nested_name_specifier = NULL_TREE;
18322 tree attributes;
18323 bool scoped_enum_p = false;
18324 bool has_underlying_type = false;
18325 bool nested_being_defined = false;
18326 bool new_value_list = false;
18327 bool is_new_type = false;
18328 bool is_unnamed = false;
18329 tree underlying_type = NULL_TREE;
18330 cp_token *type_start_token = NULL;
18331 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18333 parser->colon_corrects_to_scope_p = false;
18335 /* Parse tentatively so that we can back up if we don't find a
18336 enum-specifier. */
18337 cp_parser_parse_tentatively (parser);
18339 /* Caller guarantees that the current token is 'enum', an identifier
18340 possibly follows, and the token after that is an opening brace.
18341 If we don't have an identifier, fabricate an anonymous name for
18342 the enumeration being defined. */
18343 cp_lexer_consume_token (parser->lexer);
18345 /* Parse the "class" or "struct", which indicates a scoped
18346 enumeration type in C++0x. */
18347 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18348 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18350 if (cxx_dialect < cxx11)
18351 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18353 /* Consume the `struct' or `class' token. */
18354 cp_lexer_consume_token (parser->lexer);
18356 scoped_enum_p = true;
18359 attributes = cp_parser_attributes_opt (parser);
18361 /* Clear the qualification. */
18362 parser->scope = NULL_TREE;
18363 parser->qualifying_scope = NULL_TREE;
18364 parser->object_scope = NULL_TREE;
18366 /* Figure out in what scope the declaration is being placed. */
18367 prev_scope = current_scope ();
18369 type_start_token = cp_lexer_peek_token (parser->lexer);
18371 push_deferring_access_checks (dk_no_check);
18372 nested_name_specifier
18373 = cp_parser_nested_name_specifier_opt (parser,
18374 /*typename_keyword_p=*/true,
18375 /*check_dependency_p=*/false,
18376 /*type_p=*/false,
18377 /*is_declaration=*/false);
18379 if (nested_name_specifier)
18381 tree name;
18383 identifier = cp_parser_identifier (parser);
18384 name = cp_parser_lookup_name (parser, identifier,
18385 enum_type,
18386 /*is_template=*/false,
18387 /*is_namespace=*/false,
18388 /*check_dependency=*/true,
18389 /*ambiguous_decls=*/NULL,
18390 input_location);
18391 if (name && name != error_mark_node)
18393 type = TREE_TYPE (name);
18394 if (TREE_CODE (type) == TYPENAME_TYPE)
18396 /* Are template enums allowed in ISO? */
18397 if (template_parm_scope_p ())
18398 pedwarn (type_start_token->location, OPT_Wpedantic,
18399 "%qD is an enumeration template", name);
18400 /* ignore a typename reference, for it will be solved by name
18401 in start_enum. */
18402 type = NULL_TREE;
18405 else if (nested_name_specifier == error_mark_node)
18406 /* We already issued an error. */;
18407 else
18409 error_at (type_start_token->location,
18410 "%qD does not name an enumeration in %qT",
18411 identifier, nested_name_specifier);
18412 nested_name_specifier = error_mark_node;
18415 else
18417 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18418 identifier = cp_parser_identifier (parser);
18419 else
18421 identifier = make_anon_name ();
18422 is_unnamed = true;
18423 if (scoped_enum_p)
18424 error_at (type_start_token->location,
18425 "unnamed scoped enum is not allowed");
18428 pop_deferring_access_checks ();
18430 /* Check for the `:' that denotes a specified underlying type in C++0x.
18431 Note that a ':' could also indicate a bitfield width, however. */
18432 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18434 cp_decl_specifier_seq type_specifiers;
18436 /* Consume the `:'. */
18437 cp_lexer_consume_token (parser->lexer);
18439 /* Parse the type-specifier-seq. */
18440 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18441 /*is_trailing_return=*/false,
18442 &type_specifiers);
18444 /* At this point this is surely not elaborated type specifier. */
18445 if (!cp_parser_parse_definitely (parser))
18446 return NULL_TREE;
18448 if (cxx_dialect < cxx11)
18449 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18451 has_underlying_type = true;
18453 /* If that didn't work, stop. */
18454 if (type_specifiers.type != error_mark_node)
18456 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18457 /*initialized=*/0, NULL);
18458 if (underlying_type == error_mark_node
18459 || check_for_bare_parameter_packs (underlying_type))
18460 underlying_type = NULL_TREE;
18464 /* Look for the `{' but don't consume it yet. */
18465 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18467 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18469 cp_parser_error (parser, "expected %<{%>");
18470 if (has_underlying_type)
18472 type = NULL_TREE;
18473 goto out;
18476 /* An opaque-enum-specifier must have a ';' here. */
18477 if ((scoped_enum_p || underlying_type)
18478 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18480 cp_parser_error (parser, "expected %<;%> or %<{%>");
18481 if (has_underlying_type)
18483 type = NULL_TREE;
18484 goto out;
18489 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18490 return NULL_TREE;
18492 if (nested_name_specifier)
18494 if (CLASS_TYPE_P (nested_name_specifier))
18496 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18497 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18498 push_scope (nested_name_specifier);
18500 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18502 push_nested_namespace (nested_name_specifier);
18506 /* Issue an error message if type-definitions are forbidden here. */
18507 if (!cp_parser_check_type_definition (parser))
18508 type = error_mark_node;
18509 else
18510 /* Create the new type. We do this before consuming the opening
18511 brace so the enum will be recorded as being on the line of its
18512 tag (or the 'enum' keyword, if there is no tag). */
18513 type = start_enum (identifier, type, underlying_type,
18514 attributes, scoped_enum_p, &is_new_type);
18516 /* If the next token is not '{' it is an opaque-enum-specifier or an
18517 elaborated-type-specifier. */
18518 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18520 timevar_push (TV_PARSE_ENUM);
18521 if (nested_name_specifier
18522 && nested_name_specifier != error_mark_node)
18524 /* The following catches invalid code such as:
18525 enum class S<int>::E { A, B, C }; */
18526 if (!processing_specialization
18527 && CLASS_TYPE_P (nested_name_specifier)
18528 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18529 error_at (type_start_token->location, "cannot add an enumerator "
18530 "list to a template instantiation");
18532 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18534 error_at (type_start_token->location,
18535 "%<%T::%E%> has not been declared",
18536 TYPE_CONTEXT (nested_name_specifier),
18537 nested_name_specifier);
18538 type = error_mark_node;
18540 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18541 && !CLASS_TYPE_P (nested_name_specifier))
18543 error_at (type_start_token->location, "nested name specifier "
18544 "%qT for enum declaration does not name a class "
18545 "or namespace", nested_name_specifier);
18546 type = error_mark_node;
18548 /* If that scope does not contain the scope in which the
18549 class was originally declared, the program is invalid. */
18550 else if (prev_scope && !is_ancestor (prev_scope,
18551 nested_name_specifier))
18553 if (at_namespace_scope_p ())
18554 error_at (type_start_token->location,
18555 "declaration of %qD in namespace %qD which does not "
18556 "enclose %qD",
18557 type, prev_scope, nested_name_specifier);
18558 else
18559 error_at (type_start_token->location,
18560 "declaration of %qD in %qD which does not "
18561 "enclose %qD",
18562 type, prev_scope, nested_name_specifier);
18563 type = error_mark_node;
18565 /* If that scope is the scope where the declaration is being placed
18566 the program is invalid. */
18567 else if (CLASS_TYPE_P (nested_name_specifier)
18568 && CLASS_TYPE_P (prev_scope)
18569 && same_type_p (nested_name_specifier, prev_scope))
18571 permerror (type_start_token->location,
18572 "extra qualification not allowed");
18573 nested_name_specifier = NULL_TREE;
18577 if (scoped_enum_p)
18578 begin_scope (sk_scoped_enum, type);
18580 /* Consume the opening brace. */
18581 matching_braces braces;
18582 braces.consume_open (parser);
18584 if (type == error_mark_node)
18585 ; /* Nothing to add */
18586 else if (OPAQUE_ENUM_P (type)
18587 || (cxx_dialect > cxx98 && processing_specialization))
18589 new_value_list = true;
18590 SET_OPAQUE_ENUM_P (type, false);
18591 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18593 else
18595 error_at (type_start_token->location,
18596 "multiple definition of %q#T", type);
18597 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18598 "previous definition here");
18599 type = error_mark_node;
18602 if (type == error_mark_node)
18603 cp_parser_skip_to_end_of_block_or_statement (parser);
18604 /* If the next token is not '}', then there are some enumerators. */
18605 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18607 if (is_unnamed && !scoped_enum_p)
18608 pedwarn (type_start_token->location, OPT_Wpedantic,
18609 "ISO C++ forbids empty unnamed enum");
18611 else
18612 cp_parser_enumerator_list (parser, type);
18614 /* Consume the final '}'. */
18615 braces.require_close (parser);
18617 if (scoped_enum_p)
18618 finish_scope ();
18619 timevar_pop (TV_PARSE_ENUM);
18621 else
18623 /* If a ';' follows, then it is an opaque-enum-specifier
18624 and additional restrictions apply. */
18625 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18627 if (is_unnamed)
18628 error_at (type_start_token->location,
18629 "opaque-enum-specifier without name");
18630 else if (nested_name_specifier)
18631 error_at (type_start_token->location,
18632 "opaque-enum-specifier must use a simple identifier");
18636 /* Look for trailing attributes to apply to this enumeration, and
18637 apply them if appropriate. */
18638 if (cp_parser_allow_gnu_extensions_p (parser))
18640 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18641 cplus_decl_attributes (&type,
18642 trailing_attr,
18643 (int) ATTR_FLAG_TYPE_IN_PLACE);
18646 /* Finish up the enumeration. */
18647 if (type != error_mark_node)
18649 if (new_value_list)
18650 finish_enum_value_list (type);
18651 if (is_new_type)
18652 finish_enum (type);
18655 if (nested_name_specifier)
18657 if (CLASS_TYPE_P (nested_name_specifier))
18659 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18660 pop_scope (nested_name_specifier);
18662 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18664 pop_nested_namespace (nested_name_specifier);
18667 out:
18668 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18669 return type;
18672 /* Parse an enumerator-list. The enumerators all have the indicated
18673 TYPE.
18675 enumerator-list:
18676 enumerator-definition
18677 enumerator-list , enumerator-definition */
18679 static void
18680 cp_parser_enumerator_list (cp_parser* parser, tree type)
18682 while (true)
18684 /* Parse an enumerator-definition. */
18685 cp_parser_enumerator_definition (parser, type);
18687 /* If the next token is not a ',', we've reached the end of
18688 the list. */
18689 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18690 break;
18691 /* Otherwise, consume the `,' and keep going. */
18692 cp_lexer_consume_token (parser->lexer);
18693 /* If the next token is a `}', there is a trailing comma. */
18694 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18696 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18697 pedwarn (input_location, OPT_Wpedantic,
18698 "comma at end of enumerator list");
18699 break;
18704 /* Parse an enumerator-definition. The enumerator has the indicated
18705 TYPE.
18707 enumerator-definition:
18708 enumerator
18709 enumerator = constant-expression
18711 enumerator:
18712 identifier
18714 GNU Extensions:
18716 enumerator-definition:
18717 enumerator attributes [opt]
18718 enumerator attributes [opt] = constant-expression */
18720 static void
18721 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18723 tree identifier;
18724 tree value;
18725 location_t loc;
18727 /* Save the input location because we are interested in the location
18728 of the identifier and not the location of the explicit value. */
18729 loc = cp_lexer_peek_token (parser->lexer)->location;
18731 /* Look for the identifier. */
18732 identifier = cp_parser_identifier (parser);
18733 if (identifier == error_mark_node)
18734 return;
18736 /* Parse any specified attributes. */
18737 tree attrs = cp_parser_attributes_opt (parser);
18739 /* If the next token is an '=', then there is an explicit value. */
18740 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18742 /* Consume the `=' token. */
18743 cp_lexer_consume_token (parser->lexer);
18744 /* Parse the value. */
18745 value = cp_parser_constant_expression (parser);
18747 else
18748 value = NULL_TREE;
18750 /* If we are processing a template, make sure the initializer of the
18751 enumerator doesn't contain any bare template parameter pack. */
18752 if (check_for_bare_parameter_packs (value))
18753 value = error_mark_node;
18755 /* Create the enumerator. */
18756 build_enumerator (identifier, value, type, attrs, loc);
18759 /* Parse a namespace-name.
18761 namespace-name:
18762 original-namespace-name
18763 namespace-alias
18765 Returns the NAMESPACE_DECL for the namespace. */
18767 static tree
18768 cp_parser_namespace_name (cp_parser* parser)
18770 tree identifier;
18771 tree namespace_decl;
18773 cp_token *token = cp_lexer_peek_token (parser->lexer);
18775 /* Get the name of the namespace. */
18776 identifier = cp_parser_identifier (parser);
18777 if (identifier == error_mark_node)
18778 return error_mark_node;
18780 /* Look up the identifier in the currently active scope. Look only
18781 for namespaces, due to:
18783 [basic.lookup.udir]
18785 When looking up a namespace-name in a using-directive or alias
18786 definition, only namespace names are considered.
18788 And:
18790 [basic.lookup.qual]
18792 During the lookup of a name preceding the :: scope resolution
18793 operator, object, function, and enumerator names are ignored.
18795 (Note that cp_parser_qualifying_entity only calls this
18796 function if the token after the name is the scope resolution
18797 operator.) */
18798 namespace_decl = cp_parser_lookup_name (parser, identifier,
18799 none_type,
18800 /*is_template=*/false,
18801 /*is_namespace=*/true,
18802 /*check_dependency=*/true,
18803 /*ambiguous_decls=*/NULL,
18804 token->location);
18805 /* If it's not a namespace, issue an error. */
18806 if (namespace_decl == error_mark_node
18807 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18809 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18811 auto_diagnostic_group d;
18812 name_hint hint;
18813 if (namespace_decl == error_mark_node
18814 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18815 hint = suggest_alternative_in_explicit_scope (token->location,
18816 identifier,
18817 parser->scope);
18818 if (const char *suggestion = hint.suggestion ())
18820 gcc_rich_location richloc (token->location);
18821 richloc.add_fixit_replace (suggestion);
18822 error_at (&richloc,
18823 "%qD is not a namespace-name; did you mean %qs?",
18824 identifier, suggestion);
18826 else
18827 error_at (token->location, "%qD is not a namespace-name",
18828 identifier);
18830 else
18831 cp_parser_error (parser, "expected namespace-name");
18832 namespace_decl = error_mark_node;
18835 return namespace_decl;
18838 /* Parse a namespace-definition.
18840 namespace-definition:
18841 named-namespace-definition
18842 unnamed-namespace-definition
18844 named-namespace-definition:
18845 original-namespace-definition
18846 extension-namespace-definition
18848 original-namespace-definition:
18849 namespace identifier { namespace-body }
18851 extension-namespace-definition:
18852 namespace original-namespace-name { namespace-body }
18854 unnamed-namespace-definition:
18855 namespace { namespace-body } */
18857 static void
18858 cp_parser_namespace_definition (cp_parser* parser)
18860 tree identifier;
18861 int nested_definition_count = 0;
18863 cp_ensure_no_omp_declare_simd (parser);
18864 cp_ensure_no_oacc_routine (parser);
18866 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18868 if (is_inline)
18870 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18871 cp_lexer_consume_token (parser->lexer);
18874 /* Look for the `namespace' keyword. */
18875 cp_token* token
18876 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18878 /* Parse any specified attributes before the identifier. */
18879 tree attribs = cp_parser_attributes_opt (parser);
18881 for (;;)
18883 identifier = NULL_TREE;
18885 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18887 identifier = cp_parser_identifier (parser);
18889 if (cp_next_tokens_can_be_std_attribute_p (parser))
18890 pedwarn (input_location, OPT_Wpedantic,
18891 "standard attributes on namespaces must precede "
18892 "the namespace name");
18894 /* Parse any attributes specified after the identifier. */
18895 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18898 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18899 break;
18901 if (!nested_definition_count && cxx_dialect < cxx17)
18902 pedwarn (input_location, OPT_Wpedantic,
18903 "nested namespace definitions only available with "
18904 "-std=c++17 or -std=gnu++17");
18906 /* Nested namespace names can create new namespaces (unlike
18907 other qualified-ids). */
18908 if (int count = identifier ? push_namespace (identifier) : 0)
18909 nested_definition_count += count;
18910 else
18911 cp_parser_error (parser, "nested namespace name required");
18912 cp_lexer_consume_token (parser->lexer);
18915 if (nested_definition_count && !identifier)
18916 cp_parser_error (parser, "namespace name required");
18918 if (nested_definition_count && attribs)
18919 error_at (token->location,
18920 "a nested namespace definition cannot have attributes");
18921 if (nested_definition_count && is_inline)
18922 error_at (token->location,
18923 "a nested namespace definition cannot be inline");
18925 /* Start the namespace. */
18926 nested_definition_count += push_namespace (identifier, is_inline);
18928 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18930 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18932 /* Look for the `{' to validate starting the namespace. */
18933 matching_braces braces;
18934 if (braces.require_open (parser))
18936 /* Parse the body of the namespace. */
18937 cp_parser_namespace_body (parser);
18939 /* Look for the final `}'. */
18940 braces.require_close (parser);
18943 if (has_visibility)
18944 pop_visibility (1);
18946 /* Pop the nested namespace definitions. */
18947 while (nested_definition_count--)
18948 pop_namespace ();
18951 /* Parse a namespace-body.
18953 namespace-body:
18954 declaration-seq [opt] */
18956 static void
18957 cp_parser_namespace_body (cp_parser* parser)
18959 cp_parser_declaration_seq_opt (parser);
18962 /* Parse a namespace-alias-definition.
18964 namespace-alias-definition:
18965 namespace identifier = qualified-namespace-specifier ; */
18967 static void
18968 cp_parser_namespace_alias_definition (cp_parser* parser)
18970 tree identifier;
18971 tree namespace_specifier;
18973 cp_token *token = cp_lexer_peek_token (parser->lexer);
18975 /* Look for the `namespace' keyword. */
18976 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18977 /* Look for the identifier. */
18978 identifier = cp_parser_identifier (parser);
18979 if (identifier == error_mark_node)
18980 return;
18981 /* Look for the `=' token. */
18982 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18983 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18985 error_at (token->location, "%<namespace%> definition is not allowed here");
18986 /* Skip the definition. */
18987 cp_lexer_consume_token (parser->lexer);
18988 if (cp_parser_skip_to_closing_brace (parser))
18989 cp_lexer_consume_token (parser->lexer);
18990 return;
18992 cp_parser_require (parser, CPP_EQ, RT_EQ);
18993 /* Look for the qualified-namespace-specifier. */
18994 namespace_specifier
18995 = cp_parser_qualified_namespace_specifier (parser);
18996 /* Look for the `;' token. */
18997 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18999 /* Register the alias in the symbol table. */
19000 do_namespace_alias (identifier, namespace_specifier);
19003 /* Parse a qualified-namespace-specifier.
19005 qualified-namespace-specifier:
19006 :: [opt] nested-name-specifier [opt] namespace-name
19008 Returns a NAMESPACE_DECL corresponding to the specified
19009 namespace. */
19011 static tree
19012 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19014 /* Look for the optional `::'. */
19015 cp_parser_global_scope_opt (parser,
19016 /*current_scope_valid_p=*/false);
19018 /* Look for the optional nested-name-specifier. */
19019 cp_parser_nested_name_specifier_opt (parser,
19020 /*typename_keyword_p=*/false,
19021 /*check_dependency_p=*/true,
19022 /*type_p=*/false,
19023 /*is_declaration=*/true);
19025 return cp_parser_namespace_name (parser);
19028 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19029 access declaration.
19031 using-declaration:
19032 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19033 using :: unqualified-id ;
19035 access-declaration:
19036 qualified-id ;
19040 static bool
19041 cp_parser_using_declaration (cp_parser* parser,
19042 bool access_declaration_p)
19044 cp_token *token;
19045 bool typename_p = false;
19046 bool global_scope_p;
19047 tree decl;
19048 tree identifier;
19049 tree qscope;
19050 int oldcount = errorcount;
19051 cp_token *diag_token = NULL;
19053 if (access_declaration_p)
19055 diag_token = cp_lexer_peek_token (parser->lexer);
19056 cp_parser_parse_tentatively (parser);
19058 else
19060 /* Look for the `using' keyword. */
19061 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19063 again:
19064 /* Peek at the next token. */
19065 token = cp_lexer_peek_token (parser->lexer);
19066 /* See if it's `typename'. */
19067 if (token->keyword == RID_TYPENAME)
19069 /* Remember that we've seen it. */
19070 typename_p = true;
19071 /* Consume the `typename' token. */
19072 cp_lexer_consume_token (parser->lexer);
19076 /* Look for the optional global scope qualification. */
19077 global_scope_p
19078 = (cp_parser_global_scope_opt (parser,
19079 /*current_scope_valid_p=*/false)
19080 != NULL_TREE);
19082 /* If we saw `typename', or didn't see `::', then there must be a
19083 nested-name-specifier present. */
19084 if (typename_p || !global_scope_p)
19086 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19087 /*check_dependency_p=*/true,
19088 /*type_p=*/false,
19089 /*is_declaration=*/true);
19090 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19092 cp_parser_skip_to_end_of_block_or_statement (parser);
19093 return false;
19096 /* Otherwise, we could be in either of the two productions. In that
19097 case, treat the nested-name-specifier as optional. */
19098 else
19099 qscope = cp_parser_nested_name_specifier_opt (parser,
19100 /*typename_keyword_p=*/false,
19101 /*check_dependency_p=*/true,
19102 /*type_p=*/false,
19103 /*is_declaration=*/true);
19104 if (!qscope)
19105 qscope = global_namespace;
19106 else if (UNSCOPED_ENUM_P (qscope))
19107 qscope = CP_TYPE_CONTEXT (qscope);
19109 if (access_declaration_p && cp_parser_error_occurred (parser))
19110 /* Something has already gone wrong; there's no need to parse
19111 further. Since an error has occurred, the return value of
19112 cp_parser_parse_definitely will be false, as required. */
19113 return cp_parser_parse_definitely (parser);
19115 token = cp_lexer_peek_token (parser->lexer);
19116 /* Parse the unqualified-id. */
19117 identifier = cp_parser_unqualified_id (parser,
19118 /*template_keyword_p=*/false,
19119 /*check_dependency_p=*/true,
19120 /*declarator_p=*/true,
19121 /*optional_p=*/false);
19123 if (access_declaration_p)
19125 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19126 cp_parser_simulate_error (parser);
19127 if (!cp_parser_parse_definitely (parser))
19128 return false;
19130 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19132 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19133 if (cxx_dialect < cxx17
19134 && !in_system_header_at (ell->location))
19135 pedwarn (ell->location, 0,
19136 "pack expansion in using-declaration only available "
19137 "with -std=c++17 or -std=gnu++17");
19138 qscope = make_pack_expansion (qscope);
19141 /* The function we call to handle a using-declaration is different
19142 depending on what scope we are in. */
19143 if (qscope == error_mark_node || identifier == error_mark_node)
19145 else if (!identifier_p (identifier)
19146 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19147 /* [namespace.udecl]
19149 A using declaration shall not name a template-id. */
19150 error_at (token->location,
19151 "a template-id may not appear in a using-declaration");
19152 else
19154 if (at_class_scope_p ())
19156 /* Create the USING_DECL. */
19157 decl = do_class_using_decl (qscope, identifier);
19159 if (decl && typename_p)
19160 USING_DECL_TYPENAME_P (decl) = 1;
19162 if (check_for_bare_parameter_packs (decl))
19164 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19165 return false;
19167 else
19168 /* Add it to the list of members in this class. */
19169 finish_member_declaration (decl);
19171 else
19173 decl = cp_parser_lookup_name_simple (parser,
19174 identifier,
19175 token->location);
19176 if (decl == error_mark_node)
19177 cp_parser_name_lookup_error (parser, identifier,
19178 decl, NLE_NULL,
19179 token->location);
19180 else if (check_for_bare_parameter_packs (decl))
19182 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19183 return false;
19185 else if (!at_namespace_scope_p ())
19186 finish_local_using_decl (decl, qscope, identifier);
19187 else
19188 finish_namespace_using_decl (decl, qscope, identifier);
19192 if (!access_declaration_p
19193 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19195 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19196 if (cxx_dialect < cxx17)
19197 pedwarn (comma->location, 0,
19198 "comma-separated list in using-declaration only available "
19199 "with -std=c++17 or -std=gnu++17");
19200 goto again;
19203 /* Look for the final `;'. */
19204 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19206 if (access_declaration_p && errorcount == oldcount)
19207 warning_at (diag_token->location, OPT_Wdeprecated,
19208 "access declarations are deprecated "
19209 "in favour of using-declarations; "
19210 "suggestion: add the %<using%> keyword");
19212 return true;
19215 /* Parse an alias-declaration.
19217 alias-declaration:
19218 using identifier attribute-specifier-seq [opt] = type-id */
19220 static tree
19221 cp_parser_alias_declaration (cp_parser* parser)
19223 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19224 location_t id_location, type_location;
19225 cp_declarator *declarator;
19226 cp_decl_specifier_seq decl_specs;
19227 bool member_p;
19228 const char *saved_message = NULL;
19230 /* Look for the `using' keyword. */
19231 cp_token *using_token
19232 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19233 if (using_token == NULL)
19234 return error_mark_node;
19236 id_location = cp_lexer_peek_token (parser->lexer)->location;
19237 id = cp_parser_identifier (parser);
19238 if (id == error_mark_node)
19239 return error_mark_node;
19241 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19242 attributes = cp_parser_attributes_opt (parser);
19243 if (attributes == error_mark_node)
19244 return error_mark_node;
19246 cp_parser_require (parser, CPP_EQ, RT_EQ);
19248 if (cp_parser_error_occurred (parser))
19249 return error_mark_node;
19251 cp_parser_commit_to_tentative_parse (parser);
19253 /* Now we are going to parse the type-id of the declaration. */
19256 [dcl.type]/3 says:
19258 "A type-specifier-seq shall not define a class or enumeration
19259 unless it appears in the type-id of an alias-declaration (7.1.3) that
19260 is not the declaration of a template-declaration."
19262 In other words, if we currently are in an alias template, the
19263 type-id should not define a type.
19265 So let's set parser->type_definition_forbidden_message in that
19266 case; cp_parser_check_type_definition (called by
19267 cp_parser_class_specifier) will then emit an error if a type is
19268 defined in the type-id. */
19269 if (parser->num_template_parameter_lists)
19271 saved_message = parser->type_definition_forbidden_message;
19272 parser->type_definition_forbidden_message =
19273 G_("types may not be defined in alias template declarations");
19276 type = cp_parser_type_id (parser, &type_location);
19278 /* Restore the error message if need be. */
19279 if (parser->num_template_parameter_lists)
19280 parser->type_definition_forbidden_message = saved_message;
19282 if (type == error_mark_node
19283 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19285 cp_parser_skip_to_end_of_block_or_statement (parser);
19286 return error_mark_node;
19289 /* A typedef-name can also be introduced by an alias-declaration. The
19290 identifier following the using keyword becomes a typedef-name. It has
19291 the same semantics as if it were introduced by the typedef
19292 specifier. In particular, it does not define a new type and it shall
19293 not appear in the type-id. */
19295 clear_decl_specs (&decl_specs);
19296 decl_specs.type = type;
19297 if (attributes != NULL_TREE)
19299 decl_specs.attributes = attributes;
19300 set_and_check_decl_spec_loc (&decl_specs,
19301 ds_attribute,
19302 attrs_token);
19304 set_and_check_decl_spec_loc (&decl_specs,
19305 ds_typedef,
19306 using_token);
19307 set_and_check_decl_spec_loc (&decl_specs,
19308 ds_alias,
19309 using_token);
19310 decl_specs.locations[ds_type_spec] = type_location;
19312 if (parser->num_template_parameter_lists
19313 && !cp_parser_check_template_parameters (parser,
19314 /*num_templates=*/0,
19315 /*template_id*/false,
19316 id_location,
19317 /*declarator=*/NULL))
19318 return error_mark_node;
19320 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
19322 member_p = at_class_scope_p ();
19323 if (member_p)
19324 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19325 NULL_TREE, attributes);
19326 else
19327 decl = start_decl (declarator, &decl_specs, 0,
19328 attributes, NULL_TREE, &pushed_scope);
19329 if (decl == error_mark_node)
19330 return decl;
19332 // Attach constraints to the alias declaration.
19333 if (flag_concepts && current_template_parms)
19335 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19336 tree constr = build_constraints (reqs, NULL_TREE);
19337 set_constraints (decl, constr);
19340 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19342 if (pushed_scope)
19343 pop_scope (pushed_scope);
19345 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19346 added into the symbol table; otherwise, return the TYPE_DECL. */
19347 if (DECL_LANG_SPECIFIC (decl)
19348 && DECL_TEMPLATE_INFO (decl)
19349 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19351 decl = DECL_TI_TEMPLATE (decl);
19352 if (member_p)
19353 check_member_template (decl);
19356 return decl;
19359 /* Parse a using-directive.
19361 using-directive:
19362 using namespace :: [opt] nested-name-specifier [opt]
19363 namespace-name ; */
19365 static void
19366 cp_parser_using_directive (cp_parser* parser)
19368 tree namespace_decl;
19369 tree attribs;
19371 /* Look for the `using' keyword. */
19372 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19373 /* And the `namespace' keyword. */
19374 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19375 /* Look for the optional `::' operator. */
19376 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19377 /* And the optional nested-name-specifier. */
19378 cp_parser_nested_name_specifier_opt (parser,
19379 /*typename_keyword_p=*/false,
19380 /*check_dependency_p=*/true,
19381 /*type_p=*/false,
19382 /*is_declaration=*/true);
19383 /* Get the namespace being used. */
19384 namespace_decl = cp_parser_namespace_name (parser);
19385 /* And any specified attributes. */
19386 attribs = cp_parser_attributes_opt (parser);
19388 /* Update the symbol table. */
19389 if (namespace_bindings_p ())
19390 finish_namespace_using_directive (namespace_decl, attribs);
19391 else
19392 finish_local_using_directive (namespace_decl, attribs);
19394 /* Look for the final `;'. */
19395 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19398 /* Parse an asm-definition.
19400 asm-definition:
19401 asm ( string-literal ) ;
19403 GNU Extension:
19405 asm-definition:
19406 asm volatile [opt] ( string-literal ) ;
19407 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19408 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19409 : asm-operand-list [opt] ) ;
19410 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19411 : asm-operand-list [opt]
19412 : asm-clobber-list [opt] ) ;
19413 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19414 : asm-clobber-list [opt]
19415 : asm-goto-list ) ; */
19417 static void
19418 cp_parser_asm_definition (cp_parser* parser)
19420 tree string;
19421 tree outputs = NULL_TREE;
19422 tree inputs = NULL_TREE;
19423 tree clobbers = NULL_TREE;
19424 tree labels = NULL_TREE;
19425 tree asm_stmt;
19426 bool volatile_p = false;
19427 bool extended_p = false;
19428 bool invalid_inputs_p = false;
19429 bool invalid_outputs_p = false;
19430 bool goto_p = false;
19431 required_token missing = RT_NONE;
19433 /* Look for the `asm' keyword. */
19434 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19436 if (parser->in_function_body
19437 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19439 error ("%<asm%> in %<constexpr%> function");
19440 cp_function_chain->invalid_constexpr = true;
19443 /* See if the next token is `volatile'. */
19444 if (cp_parser_allow_gnu_extensions_p (parser)
19445 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19447 /* Remember that we saw the `volatile' keyword. */
19448 volatile_p = true;
19449 /* Consume the token. */
19450 cp_lexer_consume_token (parser->lexer);
19452 if (cp_parser_allow_gnu_extensions_p (parser)
19453 && parser->in_function_body
19454 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19456 /* Remember that we saw the `goto' keyword. */
19457 goto_p = true;
19458 /* Consume the token. */
19459 cp_lexer_consume_token (parser->lexer);
19461 /* Look for the opening `('. */
19462 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19463 return;
19464 /* Look for the string. */
19465 string = cp_parser_string_literal (parser, false, false);
19466 if (string == error_mark_node)
19468 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19469 /*consume_paren=*/true);
19470 return;
19473 /* If we're allowing GNU extensions, check for the extended assembly
19474 syntax. Unfortunately, the `:' tokens need not be separated by
19475 a space in C, and so, for compatibility, we tolerate that here
19476 too. Doing that means that we have to treat the `::' operator as
19477 two `:' tokens. */
19478 if (cp_parser_allow_gnu_extensions_p (parser)
19479 && parser->in_function_body
19480 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19481 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19483 bool inputs_p = false;
19484 bool clobbers_p = false;
19485 bool labels_p = false;
19487 /* The extended syntax was used. */
19488 extended_p = true;
19490 /* Look for outputs. */
19491 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19493 /* Consume the `:'. */
19494 cp_lexer_consume_token (parser->lexer);
19495 /* Parse the output-operands. */
19496 if (cp_lexer_next_token_is_not (parser->lexer,
19497 CPP_COLON)
19498 && cp_lexer_next_token_is_not (parser->lexer,
19499 CPP_SCOPE)
19500 && cp_lexer_next_token_is_not (parser->lexer,
19501 CPP_CLOSE_PAREN)
19502 && !goto_p)
19504 outputs = cp_parser_asm_operand_list (parser);
19505 if (outputs == error_mark_node)
19506 invalid_outputs_p = true;
19509 /* If the next token is `::', there are no outputs, and the
19510 next token is the beginning of the inputs. */
19511 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19512 /* The inputs are coming next. */
19513 inputs_p = true;
19515 /* Look for inputs. */
19516 if (inputs_p
19517 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19519 /* Consume the `:' or `::'. */
19520 cp_lexer_consume_token (parser->lexer);
19521 /* Parse the output-operands. */
19522 if (cp_lexer_next_token_is_not (parser->lexer,
19523 CPP_COLON)
19524 && cp_lexer_next_token_is_not (parser->lexer,
19525 CPP_SCOPE)
19526 && cp_lexer_next_token_is_not (parser->lexer,
19527 CPP_CLOSE_PAREN))
19529 inputs = cp_parser_asm_operand_list (parser);
19530 if (inputs == error_mark_node)
19531 invalid_inputs_p = true;
19534 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19535 /* The clobbers are coming next. */
19536 clobbers_p = true;
19538 /* Look for clobbers. */
19539 if (clobbers_p
19540 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19542 clobbers_p = true;
19543 /* Consume the `:' or `::'. */
19544 cp_lexer_consume_token (parser->lexer);
19545 /* Parse the clobbers. */
19546 if (cp_lexer_next_token_is_not (parser->lexer,
19547 CPP_COLON)
19548 && cp_lexer_next_token_is_not (parser->lexer,
19549 CPP_CLOSE_PAREN))
19550 clobbers = cp_parser_asm_clobber_list (parser);
19552 else if (goto_p
19553 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19554 /* The labels are coming next. */
19555 labels_p = true;
19557 /* Look for labels. */
19558 if (labels_p
19559 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19561 labels_p = true;
19562 /* Consume the `:' or `::'. */
19563 cp_lexer_consume_token (parser->lexer);
19564 /* Parse the labels. */
19565 labels = cp_parser_asm_label_list (parser);
19568 if (goto_p && !labels_p)
19569 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19571 else if (goto_p)
19572 missing = RT_COLON_SCOPE;
19574 /* Look for the closing `)'. */
19575 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19576 missing ? missing : RT_CLOSE_PAREN))
19577 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19578 /*consume_paren=*/true);
19579 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19581 if (!invalid_inputs_p && !invalid_outputs_p)
19583 /* Create the ASM_EXPR. */
19584 if (parser->in_function_body)
19586 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19587 inputs, clobbers, labels);
19588 /* If the extended syntax was not used, mark the ASM_EXPR. */
19589 if (!extended_p)
19591 tree temp = asm_stmt;
19592 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19593 temp = TREE_OPERAND (temp, 0);
19595 ASM_INPUT_P (temp) = 1;
19598 else
19599 symtab->finalize_toplevel_asm (string);
19603 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19604 type that comes from the decl-specifier-seq. */
19606 static tree
19607 strip_declarator_types (tree type, cp_declarator *declarator)
19609 for (cp_declarator *d = declarator; d;)
19610 switch (d->kind)
19612 case cdk_id:
19613 case cdk_decomp:
19614 case cdk_error:
19615 d = NULL;
19616 break;
19618 default:
19619 if (TYPE_PTRMEMFUNC_P (type))
19620 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19621 type = TREE_TYPE (type);
19622 d = d->declarator;
19623 break;
19626 return type;
19629 /* Declarators [gram.dcl.decl] */
19631 /* Parse an init-declarator.
19633 init-declarator:
19634 declarator initializer [opt]
19636 GNU Extension:
19638 init-declarator:
19639 declarator asm-specification [opt] attributes [opt] initializer [opt]
19641 function-definition:
19642 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19643 function-body
19644 decl-specifier-seq [opt] declarator function-try-block
19646 GNU Extension:
19648 function-definition:
19649 __extension__ function-definition
19651 TM Extension:
19653 function-definition:
19654 decl-specifier-seq [opt] declarator function-transaction-block
19656 The DECL_SPECIFIERS apply to this declarator. Returns a
19657 representation of the entity declared. If MEMBER_P is TRUE, then
19658 this declarator appears in a class scope. The new DECL created by
19659 this declarator is returned.
19661 The CHECKS are access checks that should be performed once we know
19662 what entity is being declared (and, therefore, what classes have
19663 befriended it).
19665 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19666 for a function-definition here as well. If the declarator is a
19667 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19668 be TRUE upon return. By that point, the function-definition will
19669 have been completely parsed.
19671 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19672 is FALSE.
19674 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19675 parsed declaration if it is an uninitialized single declarator not followed
19676 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19677 if present, will not be consumed. If returned, this declarator will be
19678 created with SD_INITIALIZED but will not call cp_finish_decl.
19680 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19681 and there is an initializer, the pointed location_t is set to the
19682 location of the '=' or `(', or '{' in C++11 token introducing the
19683 initializer. */
19685 static tree
19686 cp_parser_init_declarator (cp_parser* parser,
19687 cp_decl_specifier_seq *decl_specifiers,
19688 vec<deferred_access_check, va_gc> *checks,
19689 bool function_definition_allowed_p,
19690 bool member_p,
19691 int declares_class_or_enum,
19692 bool* function_definition_p,
19693 tree* maybe_range_for_decl,
19694 location_t* init_loc,
19695 tree* auto_result)
19697 cp_token *token = NULL, *asm_spec_start_token = NULL,
19698 *attributes_start_token = NULL;
19699 cp_declarator *declarator;
19700 tree prefix_attributes;
19701 tree attributes = NULL;
19702 tree asm_specification;
19703 tree initializer;
19704 tree decl = NULL_TREE;
19705 tree scope;
19706 int is_initialized;
19707 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19708 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19709 "(...)". */
19710 enum cpp_ttype initialization_kind;
19711 bool is_direct_init = false;
19712 bool is_non_constant_init;
19713 int ctor_dtor_or_conv_p;
19714 bool friend_p = cp_parser_friend_p (decl_specifiers);
19715 tree pushed_scope = NULL_TREE;
19716 bool range_for_decl_p = false;
19717 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19718 location_t tmp_init_loc = UNKNOWN_LOCATION;
19720 /* Gather the attributes that were provided with the
19721 decl-specifiers. */
19722 prefix_attributes = decl_specifiers->attributes;
19724 /* Assume that this is not the declarator for a function
19725 definition. */
19726 if (function_definition_p)
19727 *function_definition_p = false;
19729 /* Default arguments are only permitted for function parameters. */
19730 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19731 parser->default_arg_ok_p = false;
19733 /* Defer access checks while parsing the declarator; we cannot know
19734 what names are accessible until we know what is being
19735 declared. */
19736 resume_deferring_access_checks ();
19738 token = cp_lexer_peek_token (parser->lexer);
19740 /* Parse the declarator. */
19741 declarator
19742 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19743 &ctor_dtor_or_conv_p,
19744 /*parenthesized_p=*/NULL,
19745 member_p, friend_p);
19746 /* Gather up the deferred checks. */
19747 stop_deferring_access_checks ();
19749 parser->default_arg_ok_p = saved_default_arg_ok_p;
19751 /* If the DECLARATOR was erroneous, there's no need to go
19752 further. */
19753 if (declarator == cp_error_declarator)
19754 return error_mark_node;
19756 /* Check that the number of template-parameter-lists is OK. */
19757 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19758 token->location))
19759 return error_mark_node;
19761 if (declares_class_or_enum & 2)
19762 cp_parser_check_for_definition_in_return_type (declarator,
19763 decl_specifiers->type,
19764 decl_specifiers->locations[ds_type_spec]);
19766 /* Figure out what scope the entity declared by the DECLARATOR is
19767 located in. `grokdeclarator' sometimes changes the scope, so
19768 we compute it now. */
19769 scope = get_scope_of_declarator (declarator);
19771 /* Perform any lookups in the declared type which were thought to be
19772 dependent, but are not in the scope of the declarator. */
19773 decl_specifiers->type
19774 = maybe_update_decl_type (decl_specifiers->type, scope);
19776 /* If we're allowing GNU extensions, look for an
19777 asm-specification. */
19778 if (cp_parser_allow_gnu_extensions_p (parser))
19780 /* Look for an asm-specification. */
19781 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19782 asm_specification = cp_parser_asm_specification_opt (parser);
19784 else
19785 asm_specification = NULL_TREE;
19787 /* Look for attributes. */
19788 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19789 attributes = cp_parser_attributes_opt (parser);
19791 /* Peek at the next token. */
19792 token = cp_lexer_peek_token (parser->lexer);
19794 bool bogus_implicit_tmpl = false;
19796 if (function_declarator_p (declarator))
19798 /* Handle C++17 deduction guides. */
19799 if (!decl_specifiers->type
19800 && ctor_dtor_or_conv_p <= 0
19801 && cxx_dialect >= cxx17)
19803 cp_declarator *id = get_id_declarator (declarator);
19804 tree name = id->u.id.unqualified_name;
19805 parser->scope = id->u.id.qualifying_scope;
19806 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19807 if (tmpl
19808 && (DECL_CLASS_TEMPLATE_P (tmpl)
19809 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19811 id->u.id.unqualified_name = dguide_name (tmpl);
19812 id->u.id.sfk = sfk_deduction_guide;
19813 ctor_dtor_or_conv_p = 1;
19817 /* Check to see if the token indicates the start of a
19818 function-definition. */
19819 if (cp_parser_token_starts_function_definition_p (token))
19821 if (!function_definition_allowed_p)
19823 /* If a function-definition should not appear here, issue an
19824 error message. */
19825 cp_parser_error (parser,
19826 "a function-definition is not allowed here");
19827 return error_mark_node;
19830 location_t func_brace_location
19831 = cp_lexer_peek_token (parser->lexer)->location;
19833 /* Neither attributes nor an asm-specification are allowed
19834 on a function-definition. */
19835 if (asm_specification)
19836 error_at (asm_spec_start_token->location,
19837 "an asm-specification is not allowed "
19838 "on a function-definition");
19839 if (attributes)
19840 error_at (attributes_start_token->location,
19841 "attributes are not allowed "
19842 "on a function-definition");
19843 /* This is a function-definition. */
19844 *function_definition_p = true;
19846 /* Parse the function definition. */
19847 if (member_p)
19848 decl = cp_parser_save_member_function_body (parser,
19849 decl_specifiers,
19850 declarator,
19851 prefix_attributes);
19852 else
19853 decl =
19854 (cp_parser_function_definition_from_specifiers_and_declarator
19855 (parser, decl_specifiers, prefix_attributes, declarator));
19857 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19859 /* This is where the prologue starts... */
19860 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19861 = func_brace_location;
19864 return decl;
19867 else if (parser->fully_implicit_function_template_p)
19869 /* A non-template declaration involving a function parameter list
19870 containing an implicit template parameter will be made into a
19871 template. If the resulting declaration is not going to be an
19872 actual function then finish the template scope here to prevent it.
19873 An error message will be issued once we have a decl to talk about.
19875 FIXME probably we should do type deduction rather than create an
19876 implicit template, but the standard currently doesn't allow it. */
19877 bogus_implicit_tmpl = true;
19878 finish_fully_implicit_template (parser, NULL_TREE);
19881 /* [dcl.dcl]
19883 Only in function declarations for constructors, destructors, type
19884 conversions, and deduction guides can the decl-specifier-seq be omitted.
19886 We explicitly postpone this check past the point where we handle
19887 function-definitions because we tolerate function-definitions
19888 that are missing their return types in some modes. */
19889 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19891 cp_parser_error (parser,
19892 "expected constructor, destructor, or type conversion");
19893 return error_mark_node;
19896 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19897 if (token->type == CPP_EQ
19898 || token->type == CPP_OPEN_PAREN
19899 || token->type == CPP_OPEN_BRACE)
19901 is_initialized = SD_INITIALIZED;
19902 initialization_kind = token->type;
19903 if (maybe_range_for_decl)
19904 *maybe_range_for_decl = error_mark_node;
19905 tmp_init_loc = token->location;
19906 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19907 *init_loc = tmp_init_loc;
19909 if (token->type == CPP_EQ
19910 && function_declarator_p (declarator))
19912 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19913 if (t2->keyword == RID_DEFAULT)
19914 is_initialized = SD_DEFAULTED;
19915 else if (t2->keyword == RID_DELETE)
19916 is_initialized = SD_DELETED;
19919 else
19921 /* If the init-declarator isn't initialized and isn't followed by a
19922 `,' or `;', it's not a valid init-declarator. */
19923 if (token->type != CPP_COMMA
19924 && token->type != CPP_SEMICOLON)
19926 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19927 range_for_decl_p = true;
19928 else
19930 if (!maybe_range_for_decl)
19931 cp_parser_error (parser, "expected initializer");
19932 return error_mark_node;
19935 is_initialized = SD_UNINITIALIZED;
19936 initialization_kind = CPP_EOF;
19939 /* Because start_decl has side-effects, we should only call it if we
19940 know we're going ahead. By this point, we know that we cannot
19941 possibly be looking at any other construct. */
19942 cp_parser_commit_to_tentative_parse (parser);
19944 /* Enter the newly declared entry in the symbol table. If we're
19945 processing a declaration in a class-specifier, we wait until
19946 after processing the initializer. */
19947 if (!member_p)
19949 if (parser->in_unbraced_linkage_specification_p)
19950 decl_specifiers->storage_class = sc_extern;
19951 decl = start_decl (declarator, decl_specifiers,
19952 range_for_decl_p? SD_INITIALIZED : is_initialized,
19953 attributes, prefix_attributes, &pushed_scope);
19954 cp_finalize_omp_declare_simd (parser, decl);
19955 cp_finalize_oacc_routine (parser, decl, false);
19956 /* Adjust location of decl if declarator->id_loc is more appropriate:
19957 set, and decl wasn't merged with another decl, in which case its
19958 location would be different from input_location, and more accurate. */
19959 if (DECL_P (decl)
19960 && declarator->id_loc != UNKNOWN_LOCATION
19961 && DECL_SOURCE_LOCATION (decl) == input_location)
19962 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19964 else if (scope)
19965 /* Enter the SCOPE. That way unqualified names appearing in the
19966 initializer will be looked up in SCOPE. */
19967 pushed_scope = push_scope (scope);
19969 /* Perform deferred access control checks, now that we know in which
19970 SCOPE the declared entity resides. */
19971 if (!member_p && decl)
19973 tree saved_current_function_decl = NULL_TREE;
19975 /* If the entity being declared is a function, pretend that we
19976 are in its scope. If it is a `friend', it may have access to
19977 things that would not otherwise be accessible. */
19978 if (TREE_CODE (decl) == FUNCTION_DECL)
19980 saved_current_function_decl = current_function_decl;
19981 current_function_decl = decl;
19984 /* Perform access checks for template parameters. */
19985 cp_parser_perform_template_parameter_access_checks (checks);
19987 /* Perform the access control checks for the declarator and the
19988 decl-specifiers. */
19989 perform_deferred_access_checks (tf_warning_or_error);
19991 /* Restore the saved value. */
19992 if (TREE_CODE (decl) == FUNCTION_DECL)
19993 current_function_decl = saved_current_function_decl;
19996 /* Parse the initializer. */
19997 initializer = NULL_TREE;
19998 is_direct_init = false;
19999 is_non_constant_init = true;
20000 if (is_initialized)
20002 if (function_declarator_p (declarator))
20004 if (initialization_kind == CPP_EQ)
20005 initializer = cp_parser_pure_specifier (parser);
20006 else
20008 /* If the declaration was erroneous, we don't really
20009 know what the user intended, so just silently
20010 consume the initializer. */
20011 if (decl != error_mark_node)
20012 error_at (tmp_init_loc, "initializer provided for function");
20013 cp_parser_skip_to_closing_parenthesis (parser,
20014 /*recovering=*/true,
20015 /*or_comma=*/false,
20016 /*consume_paren=*/true);
20019 else
20021 /* We want to record the extra mangling scope for in-class
20022 initializers of class members and initializers of static data
20023 member templates. The former involves deferring
20024 parsing of the initializer until end of class as with default
20025 arguments. So right here we only handle the latter. */
20026 if (!member_p && processing_template_decl && decl != error_mark_node)
20027 start_lambda_scope (decl);
20028 initializer = cp_parser_initializer (parser,
20029 &is_direct_init,
20030 &is_non_constant_init);
20031 if (!member_p && processing_template_decl && decl != error_mark_node)
20032 finish_lambda_scope ();
20033 if (initializer == error_mark_node)
20034 cp_parser_skip_to_end_of_statement (parser);
20038 /* The old parser allows attributes to appear after a parenthesized
20039 initializer. Mark Mitchell proposed removing this functionality
20040 on the GCC mailing lists on 2002-08-13. This parser accepts the
20041 attributes -- but ignores them. Made a permerror in GCC 8. */
20042 if (cp_parser_allow_gnu_extensions_p (parser)
20043 && initialization_kind == CPP_OPEN_PAREN
20044 && cp_parser_attributes_opt (parser)
20045 && permerror (input_location,
20046 "attributes after parenthesized initializer ignored"))
20048 static bool hint;
20049 if (flag_permissive && !hint)
20051 hint = true;
20052 inform (input_location,
20053 "this flexibility is deprecated and will be removed");
20057 /* And now complain about a non-function implicit template. */
20058 if (bogus_implicit_tmpl && decl != error_mark_node)
20059 error_at (DECL_SOURCE_LOCATION (decl),
20060 "non-function %qD declared as implicit template", decl);
20062 /* For an in-class declaration, use `grokfield' to create the
20063 declaration. */
20064 if (member_p)
20066 if (pushed_scope)
20068 pop_scope (pushed_scope);
20069 pushed_scope = NULL_TREE;
20071 decl = grokfield (declarator, decl_specifiers,
20072 initializer, !is_non_constant_init,
20073 /*asmspec=*/NULL_TREE,
20074 attr_chainon (attributes, prefix_attributes));
20075 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20076 cp_parser_save_default_args (parser, decl);
20077 cp_finalize_omp_declare_simd (parser, decl);
20078 cp_finalize_oacc_routine (parser, decl, false);
20081 /* Finish processing the declaration. But, skip member
20082 declarations. */
20083 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20085 cp_finish_decl (decl,
20086 initializer, !is_non_constant_init,
20087 asm_specification,
20088 /* If the initializer is in parentheses, then this is
20089 a direct-initialization, which means that an
20090 `explicit' constructor is OK. Otherwise, an
20091 `explicit' constructor cannot be used. */
20092 ((is_direct_init || !is_initialized)
20093 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20095 else if ((cxx_dialect != cxx98) && friend_p
20096 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20097 /* Core issue #226 (C++0x only): A default template-argument
20098 shall not be specified in a friend class template
20099 declaration. */
20100 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20101 /*is_partial=*/false, /*is_friend_decl=*/1);
20103 if (!friend_p && pushed_scope)
20104 pop_scope (pushed_scope);
20106 if (function_declarator_p (declarator)
20107 && parser->fully_implicit_function_template_p)
20109 if (member_p)
20110 decl = finish_fully_implicit_template (parser, decl);
20111 else
20112 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20115 if (auto_result && is_initialized && decl_specifiers->type
20116 && type_uses_auto (decl_specifiers->type))
20117 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20119 return decl;
20122 /* Parse a declarator.
20124 declarator:
20125 direct-declarator
20126 ptr-operator declarator
20128 abstract-declarator:
20129 ptr-operator abstract-declarator [opt]
20130 direct-abstract-declarator
20132 GNU Extensions:
20134 declarator:
20135 attributes [opt] direct-declarator
20136 attributes [opt] ptr-operator declarator
20138 abstract-declarator:
20139 attributes [opt] ptr-operator abstract-declarator [opt]
20140 attributes [opt] direct-abstract-declarator
20142 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20143 detect constructors, destructors, deduction guides, or conversion operators.
20144 It is set to -1 if the declarator is a name, and +1 if it is a
20145 function. Otherwise it is set to zero. Usually you just want to
20146 test for >0, but internally the negative value is used.
20148 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20149 a decl-specifier-seq unless it declares a constructor, destructor,
20150 or conversion. It might seem that we could check this condition in
20151 semantic analysis, rather than parsing, but that makes it difficult
20152 to handle something like `f()'. We want to notice that there are
20153 no decl-specifiers, and therefore realize that this is an
20154 expression, not a declaration.)
20156 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20157 the declarator is a direct-declarator of the form "(...)".
20159 MEMBER_P is true iff this declarator is a member-declarator.
20161 FRIEND_P is true iff this declarator is a friend. */
20163 static cp_declarator *
20164 cp_parser_declarator (cp_parser* parser,
20165 cp_parser_declarator_kind dcl_kind,
20166 int* ctor_dtor_or_conv_p,
20167 bool* parenthesized_p,
20168 bool member_p, bool friend_p)
20170 cp_declarator *declarator;
20171 enum tree_code code;
20172 cp_cv_quals cv_quals;
20173 tree class_type;
20174 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20176 /* Assume this is not a constructor, destructor, or type-conversion
20177 operator. */
20178 if (ctor_dtor_or_conv_p)
20179 *ctor_dtor_or_conv_p = 0;
20181 if (cp_parser_allow_gnu_extensions_p (parser))
20182 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20184 /* Check for the ptr-operator production. */
20185 cp_parser_parse_tentatively (parser);
20186 /* Parse the ptr-operator. */
20187 code = cp_parser_ptr_operator (parser,
20188 &class_type,
20189 &cv_quals,
20190 &std_attributes);
20192 /* If that worked, then we have a ptr-operator. */
20193 if (cp_parser_parse_definitely (parser))
20195 /* If a ptr-operator was found, then this declarator was not
20196 parenthesized. */
20197 if (parenthesized_p)
20198 *parenthesized_p = true;
20199 /* The dependent declarator is optional if we are parsing an
20200 abstract-declarator. */
20201 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20202 cp_parser_parse_tentatively (parser);
20204 /* Parse the dependent declarator. */
20205 declarator = cp_parser_declarator (parser, dcl_kind,
20206 /*ctor_dtor_or_conv_p=*/NULL,
20207 /*parenthesized_p=*/NULL,
20208 /*member_p=*/false,
20209 friend_p);
20211 /* If we are parsing an abstract-declarator, we must handle the
20212 case where the dependent declarator is absent. */
20213 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20214 && !cp_parser_parse_definitely (parser))
20215 declarator = NULL;
20217 declarator = cp_parser_make_indirect_declarator
20218 (code, class_type, cv_quals, declarator, std_attributes);
20220 /* Everything else is a direct-declarator. */
20221 else
20223 if (parenthesized_p)
20224 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20225 CPP_OPEN_PAREN);
20226 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20227 ctor_dtor_or_conv_p,
20228 member_p, friend_p);
20231 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20232 declarator->attributes = gnu_attributes;
20233 return declarator;
20236 /* Parse a direct-declarator or direct-abstract-declarator.
20238 direct-declarator:
20239 declarator-id
20240 direct-declarator ( parameter-declaration-clause )
20241 cv-qualifier-seq [opt]
20242 ref-qualifier [opt]
20243 exception-specification [opt]
20244 direct-declarator [ constant-expression [opt] ]
20245 ( declarator )
20247 direct-abstract-declarator:
20248 direct-abstract-declarator [opt]
20249 ( parameter-declaration-clause )
20250 cv-qualifier-seq [opt]
20251 ref-qualifier [opt]
20252 exception-specification [opt]
20253 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20254 ( abstract-declarator )
20256 Returns a representation of the declarator. DCL_KIND is
20257 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20258 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20259 we are parsing a direct-declarator. It is
20260 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20261 of ambiguity we prefer an abstract declarator, as per
20262 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20263 as for cp_parser_declarator. */
20265 static cp_declarator *
20266 cp_parser_direct_declarator (cp_parser* parser,
20267 cp_parser_declarator_kind dcl_kind,
20268 int* ctor_dtor_or_conv_p,
20269 bool member_p, bool friend_p)
20271 cp_token *token;
20272 cp_declarator *declarator = NULL;
20273 tree scope = NULL_TREE;
20274 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20275 bool saved_in_declarator_p = parser->in_declarator_p;
20276 bool first = true;
20277 tree pushed_scope = NULL_TREE;
20278 cp_token *open_paren = NULL, *close_paren = NULL;
20280 while (true)
20282 /* Peek at the next token. */
20283 token = cp_lexer_peek_token (parser->lexer);
20284 if (token->type == CPP_OPEN_PAREN)
20286 /* This is either a parameter-declaration-clause, or a
20287 parenthesized declarator. When we know we are parsing a
20288 named declarator, it must be a parenthesized declarator
20289 if FIRST is true. For instance, `(int)' is a
20290 parameter-declaration-clause, with an omitted
20291 direct-abstract-declarator. But `((*))', is a
20292 parenthesized abstract declarator. Finally, when T is a
20293 template parameter `(T)' is a
20294 parameter-declaration-clause, and not a parenthesized
20295 named declarator.
20297 We first try and parse a parameter-declaration-clause,
20298 and then try a nested declarator (if FIRST is true).
20300 It is not an error for it not to be a
20301 parameter-declaration-clause, even when FIRST is
20302 false. Consider,
20304 int i (int);
20305 int i (3);
20307 The first is the declaration of a function while the
20308 second is the definition of a variable, including its
20309 initializer.
20311 Having seen only the parenthesis, we cannot know which of
20312 these two alternatives should be selected. Even more
20313 complex are examples like:
20315 int i (int (a));
20316 int i (int (3));
20318 The former is a function-declaration; the latter is a
20319 variable initialization.
20321 Thus again, we try a parameter-declaration-clause, and if
20322 that fails, we back out and return. */
20324 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20326 tree params;
20327 bool is_declarator = false;
20329 open_paren = NULL;
20331 /* In a member-declarator, the only valid interpretation
20332 of a parenthesis is the start of a
20333 parameter-declaration-clause. (It is invalid to
20334 initialize a static data member with a parenthesized
20335 initializer; only the "=" form of initialization is
20336 permitted.) */
20337 if (!member_p)
20338 cp_parser_parse_tentatively (parser);
20340 /* Consume the `('. */
20341 matching_parens parens;
20342 parens.consume_open (parser);
20343 if (first)
20345 /* If this is going to be an abstract declarator, we're
20346 in a declarator and we can't have default args. */
20347 parser->default_arg_ok_p = false;
20348 parser->in_declarator_p = true;
20351 begin_scope (sk_function_parms, NULL_TREE);
20353 /* Parse the parameter-declaration-clause. */
20354 params = cp_parser_parameter_declaration_clause (parser);
20356 /* Consume the `)'. */
20357 parens.require_close (parser);
20359 /* If all went well, parse the cv-qualifier-seq,
20360 ref-qualifier and the exception-specification. */
20361 if (member_p || cp_parser_parse_definitely (parser))
20363 cp_cv_quals cv_quals;
20364 cp_virt_specifiers virt_specifiers;
20365 cp_ref_qualifier ref_qual;
20366 tree exception_specification;
20367 tree late_return;
20368 tree attrs;
20369 bool memfn = (member_p || (pushed_scope
20370 && CLASS_TYPE_P (pushed_scope)));
20372 is_declarator = true;
20374 if (ctor_dtor_or_conv_p)
20375 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20376 first = false;
20378 /* Parse the cv-qualifier-seq. */
20379 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20380 /* Parse the ref-qualifier. */
20381 ref_qual = cp_parser_ref_qualifier_opt (parser);
20382 /* Parse the tx-qualifier. */
20383 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20384 /* And the exception-specification. */
20385 exception_specification
20386 = cp_parser_exception_specification_opt (parser);
20388 attrs = cp_parser_std_attribute_spec_seq (parser);
20390 /* In here, we handle cases where attribute is used after
20391 the function declaration. For example:
20392 void func (int x) __attribute__((vector(..))); */
20393 tree gnu_attrs = NULL_TREE;
20394 tree requires_clause = NULL_TREE;
20395 late_return = (cp_parser_late_return_type_opt
20396 (parser, declarator, requires_clause,
20397 memfn ? cv_quals : -1));
20399 /* Parse the virt-specifier-seq. */
20400 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20402 /* Create the function-declarator. */
20403 declarator = make_call_declarator (declarator,
20404 params,
20405 cv_quals,
20406 virt_specifiers,
20407 ref_qual,
20408 tx_qual,
20409 exception_specification,
20410 late_return,
20411 requires_clause);
20412 declarator->std_attributes = attrs;
20413 declarator->attributes = gnu_attrs;
20414 /* Any subsequent parameter lists are to do with
20415 return type, so are not those of the declared
20416 function. */
20417 parser->default_arg_ok_p = false;
20420 /* Remove the function parms from scope. */
20421 pop_bindings_and_leave_scope ();
20423 if (is_declarator)
20424 /* Repeat the main loop. */
20425 continue;
20428 /* If this is the first, we can try a parenthesized
20429 declarator. */
20430 if (first)
20432 bool saved_in_type_id_in_expr_p;
20434 parser->default_arg_ok_p = saved_default_arg_ok_p;
20435 parser->in_declarator_p = saved_in_declarator_p;
20437 open_paren = token;
20438 /* Consume the `('. */
20439 matching_parens parens;
20440 parens.consume_open (parser);
20441 /* Parse the nested declarator. */
20442 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20443 parser->in_type_id_in_expr_p = true;
20444 declarator
20445 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20446 /*parenthesized_p=*/NULL,
20447 member_p, friend_p);
20448 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20449 first = false;
20450 /* Expect a `)'. */
20451 close_paren = cp_lexer_peek_token (parser->lexer);
20452 if (!parens.require_close (parser))
20453 declarator = cp_error_declarator;
20454 if (declarator == cp_error_declarator)
20455 break;
20457 goto handle_declarator;
20459 /* Otherwise, we must be done. */
20460 else
20461 break;
20463 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20464 && token->type == CPP_OPEN_SQUARE
20465 && !cp_next_tokens_can_be_attribute_p (parser))
20467 /* Parse an array-declarator. */
20468 tree bounds, attrs;
20470 if (ctor_dtor_or_conv_p)
20471 *ctor_dtor_or_conv_p = 0;
20473 open_paren = NULL;
20474 first = false;
20475 parser->default_arg_ok_p = false;
20476 parser->in_declarator_p = true;
20477 /* Consume the `['. */
20478 cp_lexer_consume_token (parser->lexer);
20479 /* Peek at the next token. */
20480 token = cp_lexer_peek_token (parser->lexer);
20481 /* If the next token is `]', then there is no
20482 constant-expression. */
20483 if (token->type != CPP_CLOSE_SQUARE)
20485 bool non_constant_p;
20486 bounds
20487 = cp_parser_constant_expression (parser,
20488 /*allow_non_constant=*/true,
20489 &non_constant_p);
20490 if (!non_constant_p)
20491 /* OK */;
20492 else if (error_operand_p (bounds))
20493 /* Already gave an error. */;
20494 else if (!parser->in_function_body
20495 || current_binding_level->kind == sk_function_parms)
20497 /* Normally, the array bound must be an integral constant
20498 expression. However, as an extension, we allow VLAs
20499 in function scopes as long as they aren't part of a
20500 parameter declaration. */
20501 cp_parser_error (parser,
20502 "array bound is not an integer constant");
20503 bounds = error_mark_node;
20505 else if (processing_template_decl
20506 && !type_dependent_expression_p (bounds))
20508 /* Remember this wasn't a constant-expression. */
20509 bounds = build_nop (TREE_TYPE (bounds), bounds);
20510 TREE_SIDE_EFFECTS (bounds) = 1;
20513 else
20514 bounds = NULL_TREE;
20515 /* Look for the closing `]'. */
20516 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20518 declarator = cp_error_declarator;
20519 break;
20522 attrs = cp_parser_std_attribute_spec_seq (parser);
20523 declarator = make_array_declarator (declarator, bounds);
20524 declarator->std_attributes = attrs;
20526 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20529 tree qualifying_scope;
20530 tree unqualified_name;
20531 tree attrs;
20532 special_function_kind sfk;
20533 bool abstract_ok;
20534 bool pack_expansion_p = false;
20535 cp_token *declarator_id_start_token;
20537 /* Parse a declarator-id */
20538 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20539 if (abstract_ok)
20541 cp_parser_parse_tentatively (parser);
20543 /* If we see an ellipsis, we should be looking at a
20544 parameter pack. */
20545 if (token->type == CPP_ELLIPSIS)
20547 /* Consume the `...' */
20548 cp_lexer_consume_token (parser->lexer);
20550 pack_expansion_p = true;
20554 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20555 unqualified_name
20556 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20557 qualifying_scope = parser->scope;
20558 if (abstract_ok)
20560 bool okay = false;
20562 if (!unqualified_name && pack_expansion_p)
20564 /* Check whether an error occurred. */
20565 okay = !cp_parser_error_occurred (parser);
20567 /* We already consumed the ellipsis to mark a
20568 parameter pack, but we have no way to report it,
20569 so abort the tentative parse. We will be exiting
20570 immediately anyway. */
20571 cp_parser_abort_tentative_parse (parser);
20573 else
20574 okay = cp_parser_parse_definitely (parser);
20576 if (!okay)
20577 unqualified_name = error_mark_node;
20578 else if (unqualified_name
20579 && (qualifying_scope
20580 || (!identifier_p (unqualified_name))))
20582 cp_parser_error (parser, "expected unqualified-id");
20583 unqualified_name = error_mark_node;
20587 if (!unqualified_name)
20588 return NULL;
20589 if (unqualified_name == error_mark_node)
20591 declarator = cp_error_declarator;
20592 pack_expansion_p = false;
20593 declarator->parameter_pack_p = false;
20594 break;
20597 attrs = cp_parser_std_attribute_spec_seq (parser);
20599 if (qualifying_scope && at_namespace_scope_p ()
20600 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20602 /* In the declaration of a member of a template class
20603 outside of the class itself, the SCOPE will sometimes
20604 be a TYPENAME_TYPE. For example, given:
20606 template <typename T>
20607 int S<T>::R::i = 3;
20609 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20610 this context, we must resolve S<T>::R to an ordinary
20611 type, rather than a typename type.
20613 The reason we normally avoid resolving TYPENAME_TYPEs
20614 is that a specialization of `S' might render
20615 `S<T>::R' not a type. However, if `S' is
20616 specialized, then this `i' will not be used, so there
20617 is no harm in resolving the types here. */
20618 tree type;
20620 /* Resolve the TYPENAME_TYPE. */
20621 type = resolve_typename_type (qualifying_scope,
20622 /*only_current_p=*/false);
20623 /* If that failed, the declarator is invalid. */
20624 if (TREE_CODE (type) == TYPENAME_TYPE)
20626 if (typedef_variant_p (type))
20627 error_at (declarator_id_start_token->location,
20628 "cannot define member of dependent typedef "
20629 "%qT", type);
20630 else
20631 error_at (declarator_id_start_token->location,
20632 "%<%T::%E%> is not a type",
20633 TYPE_CONTEXT (qualifying_scope),
20634 TYPE_IDENTIFIER (qualifying_scope));
20636 qualifying_scope = type;
20639 sfk = sfk_none;
20641 if (unqualified_name)
20643 tree class_type;
20645 if (qualifying_scope
20646 && CLASS_TYPE_P (qualifying_scope))
20647 class_type = qualifying_scope;
20648 else
20649 class_type = current_class_type;
20651 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20653 tree name_type = TREE_TYPE (unqualified_name);
20655 if (!class_type || !same_type_p (name_type, class_type))
20657 /* We do not attempt to print the declarator
20658 here because we do not have enough
20659 information about its original syntactic
20660 form. */
20661 cp_parser_error (parser, "invalid declarator");
20662 declarator = cp_error_declarator;
20663 break;
20665 else if (qualifying_scope
20666 && CLASSTYPE_USE_TEMPLATE (name_type))
20668 error_at (declarator_id_start_token->location,
20669 "invalid use of constructor as a template");
20670 inform (declarator_id_start_token->location,
20671 "use %<%T::%D%> instead of %<%T::%D%> to "
20672 "name the constructor in a qualified name",
20673 class_type,
20674 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20675 class_type, name_type);
20676 declarator = cp_error_declarator;
20677 break;
20679 unqualified_name = constructor_name (class_type);
20682 if (class_type)
20684 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20685 sfk = sfk_destructor;
20686 else if (identifier_p (unqualified_name)
20687 && IDENTIFIER_CONV_OP_P (unqualified_name))
20688 sfk = sfk_conversion;
20689 else if (/* There's no way to declare a constructor
20690 for an unnamed type, even if the type
20691 got a name for linkage purposes. */
20692 !TYPE_WAS_UNNAMED (class_type)
20693 /* Handle correctly (c++/19200):
20695 struct S {
20696 struct T{};
20697 friend void S(T);
20700 and also:
20702 namespace N {
20703 void S();
20706 struct S {
20707 friend void N::S();
20708 }; */
20709 && (!friend_p || class_type == qualifying_scope)
20710 && constructor_name_p (unqualified_name,
20711 class_type))
20712 sfk = sfk_constructor;
20713 else if (is_overloaded_fn (unqualified_name)
20714 && DECL_CONSTRUCTOR_P (get_first_fn
20715 (unqualified_name)))
20716 sfk = sfk_constructor;
20718 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20719 *ctor_dtor_or_conv_p = -1;
20722 declarator = make_id_declarator (qualifying_scope,
20723 unqualified_name,
20724 sfk, token->location);
20725 declarator->std_attributes = attrs;
20726 declarator->parameter_pack_p = pack_expansion_p;
20728 if (pack_expansion_p)
20729 maybe_warn_variadic_templates ();
20732 handle_declarator:;
20733 scope = get_scope_of_declarator (declarator);
20734 if (scope)
20736 /* Any names that appear after the declarator-id for a
20737 member are looked up in the containing scope. */
20738 if (at_function_scope_p ())
20740 /* But declarations with qualified-ids can't appear in a
20741 function. */
20742 cp_parser_error (parser, "qualified-id in declaration");
20743 declarator = cp_error_declarator;
20744 break;
20746 pushed_scope = push_scope (scope);
20748 parser->in_declarator_p = true;
20749 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20750 || (declarator && declarator->kind == cdk_id))
20751 /* Default args are only allowed on function
20752 declarations. */
20753 parser->default_arg_ok_p = saved_default_arg_ok_p;
20754 else
20755 parser->default_arg_ok_p = false;
20757 first = false;
20759 /* We're done. */
20760 else
20761 break;
20764 /* For an abstract declarator, we might wind up with nothing at this
20765 point. That's an error; the declarator is not optional. */
20766 if (!declarator)
20767 cp_parser_error (parser, "expected declarator");
20768 else if (open_paren)
20770 /* Record overly parenthesized declarator so we can give a
20771 diagnostic about confusing decl/expr disambiguation. */
20772 if (declarator->kind == cdk_array)
20774 /* If the open and close parens are on different lines, this
20775 is probably a formatting thing, so ignore. */
20776 expanded_location open = expand_location (open_paren->location);
20777 expanded_location close = expand_location (close_paren->location);
20778 if (open.line != close.line || open.file != close.file)
20779 open_paren = NULL;
20781 if (open_paren)
20782 declarator->parenthesized = open_paren->location;
20785 /* If we entered a scope, we must exit it now. */
20786 if (pushed_scope)
20787 pop_scope (pushed_scope);
20789 parser->default_arg_ok_p = saved_default_arg_ok_p;
20790 parser->in_declarator_p = saved_in_declarator_p;
20792 return declarator;
20795 /* Parse a ptr-operator.
20797 ptr-operator:
20798 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20799 * cv-qualifier-seq [opt]
20801 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20802 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20804 GNU Extension:
20806 ptr-operator:
20807 & cv-qualifier-seq [opt]
20809 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20810 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20811 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20812 filled in with the TYPE containing the member. *CV_QUALS is
20813 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20814 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20815 Note that the tree codes returned by this function have nothing
20816 to do with the types of trees that will be eventually be created
20817 to represent the pointer or reference type being parsed. They are
20818 just constants with suggestive names. */
20819 static enum tree_code
20820 cp_parser_ptr_operator (cp_parser* parser,
20821 tree* type,
20822 cp_cv_quals *cv_quals,
20823 tree *attributes)
20825 enum tree_code code = ERROR_MARK;
20826 cp_token *token;
20827 tree attrs = NULL_TREE;
20829 /* Assume that it's not a pointer-to-member. */
20830 *type = NULL_TREE;
20831 /* And that there are no cv-qualifiers. */
20832 *cv_quals = TYPE_UNQUALIFIED;
20834 /* Peek at the next token. */
20835 token = cp_lexer_peek_token (parser->lexer);
20837 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20838 if (token->type == CPP_MULT)
20839 code = INDIRECT_REF;
20840 else if (token->type == CPP_AND)
20841 code = ADDR_EXPR;
20842 else if ((cxx_dialect != cxx98) &&
20843 token->type == CPP_AND_AND) /* C++0x only */
20844 code = NON_LVALUE_EXPR;
20846 if (code != ERROR_MARK)
20848 /* Consume the `*', `&' or `&&'. */
20849 cp_lexer_consume_token (parser->lexer);
20851 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20852 `&', if we are allowing GNU extensions. (The only qualifier
20853 that can legally appear after `&' is `restrict', but that is
20854 enforced during semantic analysis. */
20855 if (code == INDIRECT_REF
20856 || cp_parser_allow_gnu_extensions_p (parser))
20857 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20859 attrs = cp_parser_std_attribute_spec_seq (parser);
20860 if (attributes != NULL)
20861 *attributes = attrs;
20863 else
20865 /* Try the pointer-to-member case. */
20866 cp_parser_parse_tentatively (parser);
20867 /* Look for the optional `::' operator. */
20868 cp_parser_global_scope_opt (parser,
20869 /*current_scope_valid_p=*/false);
20870 /* Look for the nested-name specifier. */
20871 token = cp_lexer_peek_token (parser->lexer);
20872 cp_parser_nested_name_specifier (parser,
20873 /*typename_keyword_p=*/false,
20874 /*check_dependency_p=*/true,
20875 /*type_p=*/false,
20876 /*is_declaration=*/false);
20877 /* If we found it, and the next token is a `*', then we are
20878 indeed looking at a pointer-to-member operator. */
20879 if (!cp_parser_error_occurred (parser)
20880 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20882 /* Indicate that the `*' operator was used. */
20883 code = INDIRECT_REF;
20885 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20886 error_at (token->location, "%qD is a namespace", parser->scope);
20887 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20888 error_at (token->location, "cannot form pointer to member of "
20889 "non-class %q#T", parser->scope);
20890 else
20892 /* The type of which the member is a member is given by the
20893 current SCOPE. */
20894 *type = parser->scope;
20895 /* The next name will not be qualified. */
20896 parser->scope = NULL_TREE;
20897 parser->qualifying_scope = NULL_TREE;
20898 parser->object_scope = NULL_TREE;
20899 /* Look for optional c++11 attributes. */
20900 attrs = cp_parser_std_attribute_spec_seq (parser);
20901 if (attributes != NULL)
20902 *attributes = attrs;
20903 /* Look for the optional cv-qualifier-seq. */
20904 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20907 /* If that didn't work we don't have a ptr-operator. */
20908 if (!cp_parser_parse_definitely (parser))
20909 cp_parser_error (parser, "expected ptr-operator");
20912 return code;
20915 /* Parse an (optional) cv-qualifier-seq.
20917 cv-qualifier-seq:
20918 cv-qualifier cv-qualifier-seq [opt]
20920 cv-qualifier:
20921 const
20922 volatile
20924 GNU Extension:
20926 cv-qualifier:
20927 __restrict__
20929 Returns a bitmask representing the cv-qualifiers. */
20931 static cp_cv_quals
20932 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20934 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20936 while (true)
20938 cp_token *token;
20939 cp_cv_quals cv_qualifier;
20941 /* Peek at the next token. */
20942 token = cp_lexer_peek_token (parser->lexer);
20943 /* See if it's a cv-qualifier. */
20944 switch (token->keyword)
20946 case RID_CONST:
20947 cv_qualifier = TYPE_QUAL_CONST;
20948 break;
20950 case RID_VOLATILE:
20951 cv_qualifier = TYPE_QUAL_VOLATILE;
20952 break;
20954 case RID_RESTRICT:
20955 cv_qualifier = TYPE_QUAL_RESTRICT;
20956 break;
20958 default:
20959 cv_qualifier = TYPE_UNQUALIFIED;
20960 break;
20963 if (!cv_qualifier)
20964 break;
20966 if (cv_quals & cv_qualifier)
20968 gcc_rich_location richloc (token->location);
20969 richloc.add_fixit_remove ();
20970 error_at (&richloc, "duplicate cv-qualifier");
20971 cp_lexer_purge_token (parser->lexer);
20973 else
20975 cp_lexer_consume_token (parser->lexer);
20976 cv_quals |= cv_qualifier;
20980 return cv_quals;
20983 /* Parse an (optional) ref-qualifier
20985 ref-qualifier:
20989 Returns cp_ref_qualifier representing ref-qualifier. */
20991 static cp_ref_qualifier
20992 cp_parser_ref_qualifier_opt (cp_parser* parser)
20994 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20996 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20997 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20998 return ref_qual;
21000 while (true)
21002 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21003 cp_token *token = cp_lexer_peek_token (parser->lexer);
21005 switch (token->type)
21007 case CPP_AND:
21008 curr_ref_qual = REF_QUAL_LVALUE;
21009 break;
21011 case CPP_AND_AND:
21012 curr_ref_qual = REF_QUAL_RVALUE;
21013 break;
21015 default:
21016 curr_ref_qual = REF_QUAL_NONE;
21017 break;
21020 if (!curr_ref_qual)
21021 break;
21022 else if (ref_qual)
21024 error_at (token->location, "multiple ref-qualifiers");
21025 cp_lexer_purge_token (parser->lexer);
21027 else
21029 ref_qual = curr_ref_qual;
21030 cp_lexer_consume_token (parser->lexer);
21034 return ref_qual;
21037 /* Parse an optional tx-qualifier.
21039 tx-qualifier:
21040 transaction_safe
21041 transaction_safe_dynamic */
21043 static tree
21044 cp_parser_tx_qualifier_opt (cp_parser *parser)
21046 cp_token *token = cp_lexer_peek_token (parser->lexer);
21047 if (token->type == CPP_NAME)
21049 tree name = token->u.value;
21050 const char *p = IDENTIFIER_POINTER (name);
21051 const int len = strlen ("transaction_safe");
21052 if (!strncmp (p, "transaction_safe", len))
21054 p += len;
21055 if (*p == '\0'
21056 || !strcmp (p, "_dynamic"))
21058 cp_lexer_consume_token (parser->lexer);
21059 if (!flag_tm)
21061 error ("%qE requires %<-fgnu-tm%>", name);
21062 return NULL_TREE;
21064 else
21065 return name;
21069 return NULL_TREE;
21072 /* Parse an (optional) virt-specifier-seq.
21074 virt-specifier-seq:
21075 virt-specifier virt-specifier-seq [opt]
21077 virt-specifier:
21078 override
21079 final
21081 Returns a bitmask representing the virt-specifiers. */
21083 static cp_virt_specifiers
21084 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21086 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21088 while (true)
21090 cp_token *token;
21091 cp_virt_specifiers virt_specifier;
21093 /* Peek at the next token. */
21094 token = cp_lexer_peek_token (parser->lexer);
21095 /* See if it's a virt-specifier-qualifier. */
21096 if (token->type != CPP_NAME)
21097 break;
21098 if (id_equal (token->u.value, "override"))
21100 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21101 virt_specifier = VIRT_SPEC_OVERRIDE;
21103 else if (id_equal (token->u.value, "final"))
21105 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21106 virt_specifier = VIRT_SPEC_FINAL;
21108 else if (id_equal (token->u.value, "__final"))
21110 virt_specifier = VIRT_SPEC_FINAL;
21112 else
21113 break;
21115 if (virt_specifiers & virt_specifier)
21117 gcc_rich_location richloc (token->location);
21118 richloc.add_fixit_remove ();
21119 error_at (&richloc, "duplicate virt-specifier");
21120 cp_lexer_purge_token (parser->lexer);
21122 else
21124 cp_lexer_consume_token (parser->lexer);
21125 virt_specifiers |= virt_specifier;
21128 return virt_specifiers;
21131 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21132 is in scope even though it isn't real. */
21134 void
21135 inject_this_parameter (tree ctype, cp_cv_quals quals)
21137 tree this_parm;
21139 if (current_class_ptr)
21141 /* We don't clear this between NSDMIs. Is it already what we want? */
21142 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21143 if (DECL_P (current_class_ptr)
21144 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21145 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21146 && cp_type_quals (type) == quals)
21147 return;
21150 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21151 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21152 current_class_ptr = NULL_TREE;
21153 current_class_ref
21154 = cp_build_fold_indirect_ref (this_parm);
21155 current_class_ptr = this_parm;
21158 /* Return true iff our current scope is a non-static data member
21159 initializer. */
21161 bool
21162 parsing_nsdmi (void)
21164 /* We recognize NSDMI context by the context-less 'this' pointer set up
21165 by the function above. */
21166 if (current_class_ptr
21167 && TREE_CODE (current_class_ptr) == PARM_DECL
21168 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21169 return true;
21170 return false;
21173 /* Parse a late-specified return type, if any. This is not a separate
21174 non-terminal, but part of a function declarator, which looks like
21176 -> trailing-type-specifier-seq abstract-declarator(opt)
21178 Returns the type indicated by the type-id.
21180 In addition to this, parse any queued up #pragma omp declare simd
21181 clauses, and #pragma acc routine clauses.
21183 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21184 function. */
21186 static tree
21187 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21188 tree& requires_clause, cp_cv_quals quals)
21190 cp_token *token;
21191 tree type = NULL_TREE;
21192 bool declare_simd_p = (parser->omp_declare_simd
21193 && declarator
21194 && declarator->kind == cdk_id);
21196 bool oacc_routine_p = (parser->oacc_routine
21197 && declarator
21198 && declarator->kind == cdk_id);
21200 /* Peek at the next token. */
21201 token = cp_lexer_peek_token (parser->lexer);
21202 /* A late-specified return type is indicated by an initial '->'. */
21203 if (token->type != CPP_DEREF
21204 && token->keyword != RID_REQUIRES
21205 && !(token->type == CPP_NAME
21206 && token->u.value == ridpointers[RID_REQUIRES])
21207 && !(declare_simd_p || oacc_routine_p))
21208 return NULL_TREE;
21210 tree save_ccp = current_class_ptr;
21211 tree save_ccr = current_class_ref;
21212 if (quals >= 0)
21214 /* DR 1207: 'this' is in scope in the trailing return type. */
21215 inject_this_parameter (current_class_type, quals);
21218 if (token->type == CPP_DEREF)
21220 /* Consume the ->. */
21221 cp_lexer_consume_token (parser->lexer);
21223 type = cp_parser_trailing_type_id (parser);
21226 /* Function declarations may be followed by a trailing
21227 requires-clause. */
21228 requires_clause = cp_parser_requires_clause_opt (parser);
21230 if (declare_simd_p)
21231 declarator->attributes
21232 = cp_parser_late_parsing_omp_declare_simd (parser,
21233 declarator->attributes);
21234 if (oacc_routine_p)
21235 declarator->attributes
21236 = cp_parser_late_parsing_oacc_routine (parser,
21237 declarator->attributes);
21239 if (quals >= 0)
21241 current_class_ptr = save_ccp;
21242 current_class_ref = save_ccr;
21245 return type;
21248 /* Parse a declarator-id.
21250 declarator-id:
21251 id-expression
21252 :: [opt] nested-name-specifier [opt] type-name
21254 In the `id-expression' case, the value returned is as for
21255 cp_parser_id_expression if the id-expression was an unqualified-id.
21256 If the id-expression was a qualified-id, then a SCOPE_REF is
21257 returned. The first operand is the scope (either a NAMESPACE_DECL
21258 or TREE_TYPE), but the second is still just a representation of an
21259 unqualified-id. */
21261 static tree
21262 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21264 tree id;
21265 /* The expression must be an id-expression. Assume that qualified
21266 names are the names of types so that:
21268 template <class T>
21269 int S<T>::R::i = 3;
21271 will work; we must treat `S<T>::R' as the name of a type.
21272 Similarly, assume that qualified names are templates, where
21273 required, so that:
21275 template <class T>
21276 int S<T>::R<T>::i = 3;
21278 will work, too. */
21279 id = cp_parser_id_expression (parser,
21280 /*template_keyword_p=*/false,
21281 /*check_dependency_p=*/false,
21282 /*template_p=*/NULL,
21283 /*declarator_p=*/true,
21284 optional_p);
21285 if (id && BASELINK_P (id))
21286 id = BASELINK_FUNCTIONS (id);
21287 return id;
21290 /* Parse a type-id.
21292 type-id:
21293 type-specifier-seq abstract-declarator [opt]
21295 Returns the TYPE specified. */
21297 static tree
21298 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21299 bool is_trailing_return, location_t * type_location)
21301 cp_decl_specifier_seq type_specifier_seq;
21302 cp_declarator *abstract_declarator;
21304 /* Parse the type-specifier-seq. */
21305 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21306 is_trailing_return,
21307 &type_specifier_seq);
21308 if (type_location)
21309 *type_location = type_specifier_seq.locations[ds_type_spec];
21311 if (is_template_arg && type_specifier_seq.type
21312 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21313 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21314 /* A bare template name as a template argument is a template template
21315 argument, not a placeholder, so fail parsing it as a type argument. */
21317 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21318 cp_parser_simulate_error (parser);
21319 return error_mark_node;
21321 if (type_specifier_seq.type == error_mark_node)
21322 return error_mark_node;
21324 /* There might or might not be an abstract declarator. */
21325 cp_parser_parse_tentatively (parser);
21326 /* Look for the declarator. */
21327 abstract_declarator
21328 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21329 /*parenthesized_p=*/NULL,
21330 /*member_p=*/false,
21331 /*friend_p=*/false);
21332 /* Check to see if there really was a declarator. */
21333 if (!cp_parser_parse_definitely (parser))
21334 abstract_declarator = NULL;
21336 if (type_specifier_seq.type
21337 /* The concepts TS allows 'auto' as a type-id. */
21338 && (!flag_concepts || parser->in_type_id_in_expr_p)
21339 /* None of the valid uses of 'auto' in C++14 involve the type-id
21340 nonterminal, but it is valid in a trailing-return-type. */
21341 && !(cxx_dialect >= cxx14 && is_trailing_return))
21342 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21344 /* A type-id with type 'auto' is only ok if the abstract declarator
21345 is a function declarator with a late-specified return type.
21347 A type-id with 'auto' is also valid in a trailing-return-type
21348 in a compound-requirement. */
21349 if (abstract_declarator
21350 && abstract_declarator->kind == cdk_function
21351 && abstract_declarator->u.function.late_return_type)
21352 /* OK */;
21353 else if (parser->in_result_type_constraint_p)
21354 /* OK */;
21355 else
21357 location_t loc = type_specifier_seq.locations[ds_type_spec];
21358 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21360 error_at (loc, "missing template arguments after %qT",
21361 auto_node);
21362 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21363 tmpl);
21365 else
21366 error_at (loc, "invalid use of %qT", auto_node);
21367 return error_mark_node;
21371 return groktypename (&type_specifier_seq, abstract_declarator,
21372 is_template_arg);
21375 static tree
21376 cp_parser_type_id (cp_parser *parser, location_t * type_location)
21378 return cp_parser_type_id_1 (parser, false, false, type_location);
21381 static tree
21382 cp_parser_template_type_arg (cp_parser *parser)
21384 tree r;
21385 const char *saved_message = parser->type_definition_forbidden_message;
21386 parser->type_definition_forbidden_message
21387 = G_("types may not be defined in template arguments");
21388 r = cp_parser_type_id_1 (parser, true, false, NULL);
21389 parser->type_definition_forbidden_message = saved_message;
21390 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21392 error ("invalid use of %<auto%> in template argument");
21393 r = error_mark_node;
21395 return r;
21398 static tree
21399 cp_parser_trailing_type_id (cp_parser *parser)
21401 return cp_parser_type_id_1 (parser, false, true, NULL);
21404 /* Parse a type-specifier-seq.
21406 type-specifier-seq:
21407 type-specifier type-specifier-seq [opt]
21409 GNU extension:
21411 type-specifier-seq:
21412 attributes type-specifier-seq [opt]
21414 If IS_DECLARATION is true, we are at the start of a "condition" or
21415 exception-declaration, so we might be followed by a declarator-id.
21417 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21418 i.e. we've just seen "->".
21420 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21422 static void
21423 cp_parser_type_specifier_seq (cp_parser* parser,
21424 bool is_declaration,
21425 bool is_trailing_return,
21426 cp_decl_specifier_seq *type_specifier_seq)
21428 bool seen_type_specifier = false;
21429 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21430 cp_token *start_token = NULL;
21432 /* Clear the TYPE_SPECIFIER_SEQ. */
21433 clear_decl_specs (type_specifier_seq);
21435 /* In the context of a trailing return type, enum E { } is an
21436 elaborated-type-specifier followed by a function-body, not an
21437 enum-specifier. */
21438 if (is_trailing_return)
21439 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21441 /* Parse the type-specifiers and attributes. */
21442 while (true)
21444 tree type_specifier;
21445 bool is_cv_qualifier;
21447 /* Check for attributes first. */
21448 if (cp_next_tokens_can_be_attribute_p (parser))
21450 type_specifier_seq->attributes
21451 = attr_chainon (type_specifier_seq->attributes,
21452 cp_parser_attributes_opt (parser));
21453 continue;
21456 /* record the token of the beginning of the type specifier seq,
21457 for error reporting purposes*/
21458 if (!start_token)
21459 start_token = cp_lexer_peek_token (parser->lexer);
21461 /* Look for the type-specifier. */
21462 type_specifier = cp_parser_type_specifier (parser,
21463 flags,
21464 type_specifier_seq,
21465 /*is_declaration=*/false,
21466 NULL,
21467 &is_cv_qualifier);
21468 if (!type_specifier)
21470 /* If the first type-specifier could not be found, this is not a
21471 type-specifier-seq at all. */
21472 if (!seen_type_specifier)
21474 /* Set in_declarator_p to avoid skipping to the semicolon. */
21475 int in_decl = parser->in_declarator_p;
21476 parser->in_declarator_p = true;
21478 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21479 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21480 cp_parser_error (parser, "expected type-specifier");
21482 parser->in_declarator_p = in_decl;
21484 type_specifier_seq->type = error_mark_node;
21485 return;
21487 /* If subsequent type-specifiers could not be found, the
21488 type-specifier-seq is complete. */
21489 break;
21492 seen_type_specifier = true;
21493 /* The standard says that a condition can be:
21495 type-specifier-seq declarator = assignment-expression
21497 However, given:
21499 struct S {};
21500 if (int S = ...)
21502 we should treat the "S" as a declarator, not as a
21503 type-specifier. The standard doesn't say that explicitly for
21504 type-specifier-seq, but it does say that for
21505 decl-specifier-seq in an ordinary declaration. Perhaps it
21506 would be clearer just to allow a decl-specifier-seq here, and
21507 then add a semantic restriction that if any decl-specifiers
21508 that are not type-specifiers appear, the program is invalid. */
21509 if (is_declaration && !is_cv_qualifier)
21510 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21514 /* Return whether the function currently being declared has an associated
21515 template parameter list. */
21517 static bool
21518 function_being_declared_is_template_p (cp_parser* parser)
21520 if (!current_template_parms || processing_template_parmlist)
21521 return false;
21523 if (parser->implicit_template_scope)
21524 return true;
21526 if (at_class_scope_p ()
21527 && TYPE_BEING_DEFINED (current_class_type))
21528 return parser->num_template_parameter_lists != 0;
21530 return ((int) parser->num_template_parameter_lists > template_class_depth
21531 (current_class_type));
21534 /* Parse a parameter-declaration-clause.
21536 parameter-declaration-clause:
21537 parameter-declaration-list [opt] ... [opt]
21538 parameter-declaration-list , ...
21540 Returns a representation for the parameter declarations. A return
21541 value of NULL indicates a parameter-declaration-clause consisting
21542 only of an ellipsis. */
21544 static tree
21545 cp_parser_parameter_declaration_clause (cp_parser* parser)
21547 tree parameters;
21548 cp_token *token;
21549 bool ellipsis_p;
21551 temp_override<bool> cleanup
21552 (parser->auto_is_implicit_function_template_parm_p);
21554 if (!processing_specialization
21555 && !processing_template_parmlist
21556 && !processing_explicit_instantiation
21557 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21558 actual function or a random abstract declarator. */
21559 && parser->default_arg_ok_p)
21560 if (!current_function_decl
21561 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21562 parser->auto_is_implicit_function_template_parm_p = true;
21564 /* Peek at the next token. */
21565 token = cp_lexer_peek_token (parser->lexer);
21566 /* Check for trivial parameter-declaration-clauses. */
21567 if (token->type == CPP_ELLIPSIS)
21569 /* Consume the `...' token. */
21570 cp_lexer_consume_token (parser->lexer);
21571 return NULL_TREE;
21573 else if (token->type == CPP_CLOSE_PAREN)
21574 /* There are no parameters. */
21575 return void_list_node;
21576 /* Check for `(void)', too, which is a special case. */
21577 else if (token->keyword == RID_VOID
21578 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21579 == CPP_CLOSE_PAREN))
21581 /* Consume the `void' token. */
21582 cp_lexer_consume_token (parser->lexer);
21583 /* There are no parameters. */
21584 return void_list_node;
21587 /* Parse the parameter-declaration-list. */
21588 parameters = cp_parser_parameter_declaration_list (parser);
21589 /* If a parse error occurred while parsing the
21590 parameter-declaration-list, then the entire
21591 parameter-declaration-clause is erroneous. */
21592 if (parameters == error_mark_node)
21593 return NULL_TREE;
21595 /* Peek at the next token. */
21596 token = cp_lexer_peek_token (parser->lexer);
21597 /* If it's a `,', the clause should terminate with an ellipsis. */
21598 if (token->type == CPP_COMMA)
21600 /* Consume the `,'. */
21601 cp_lexer_consume_token (parser->lexer);
21602 /* Expect an ellipsis. */
21603 ellipsis_p
21604 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21606 /* It might also be `...' if the optional trailing `,' was
21607 omitted. */
21608 else if (token->type == CPP_ELLIPSIS)
21610 /* Consume the `...' token. */
21611 cp_lexer_consume_token (parser->lexer);
21612 /* And remember that we saw it. */
21613 ellipsis_p = true;
21615 else
21616 ellipsis_p = false;
21618 /* Finish the parameter list. */
21619 if (!ellipsis_p)
21620 parameters = chainon (parameters, void_list_node);
21622 return parameters;
21625 /* Parse a parameter-declaration-list.
21627 parameter-declaration-list:
21628 parameter-declaration
21629 parameter-declaration-list , parameter-declaration
21631 Returns a representation of the parameter-declaration-list, as for
21632 cp_parser_parameter_declaration_clause. However, the
21633 `void_list_node' is never appended to the list. */
21635 static tree
21636 cp_parser_parameter_declaration_list (cp_parser* parser)
21638 tree parameters = NULL_TREE;
21639 tree *tail = &parameters;
21640 bool saved_in_unbraced_linkage_specification_p;
21641 int index = 0;
21643 /* The special considerations that apply to a function within an
21644 unbraced linkage specifications do not apply to the parameters
21645 to the function. */
21646 saved_in_unbraced_linkage_specification_p
21647 = parser->in_unbraced_linkage_specification_p;
21648 parser->in_unbraced_linkage_specification_p = false;
21650 /* Look for more parameters. */
21651 while (true)
21653 cp_parameter_declarator *parameter;
21654 tree decl = error_mark_node;
21655 bool parenthesized_p = false;
21657 /* Parse the parameter. */
21658 parameter
21659 = cp_parser_parameter_declaration (parser,
21660 /*template_parm_p=*/false,
21661 &parenthesized_p);
21663 /* We don't know yet if the enclosing context is deprecated, so wait
21664 and warn in grokparms if appropriate. */
21665 deprecated_state = DEPRECATED_SUPPRESS;
21667 if (parameter)
21669 decl = grokdeclarator (parameter->declarator,
21670 &parameter->decl_specifiers,
21671 PARM,
21672 parameter->default_argument != NULL_TREE,
21673 &parameter->decl_specifiers.attributes);
21674 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21675 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21678 deprecated_state = DEPRECATED_NORMAL;
21680 /* If a parse error occurred parsing the parameter declaration,
21681 then the entire parameter-declaration-list is erroneous. */
21682 if (decl == error_mark_node)
21684 parameters = error_mark_node;
21685 break;
21688 if (parameter->decl_specifiers.attributes)
21689 cplus_decl_attributes (&decl,
21690 parameter->decl_specifiers.attributes,
21692 if (DECL_NAME (decl))
21693 decl = pushdecl (decl);
21695 if (decl != error_mark_node)
21697 retrofit_lang_decl (decl);
21698 DECL_PARM_INDEX (decl) = ++index;
21699 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21702 /* Add the new parameter to the list. */
21703 *tail = build_tree_list (parameter->default_argument, decl);
21704 tail = &TREE_CHAIN (*tail);
21706 /* Peek at the next token. */
21707 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21708 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21709 /* These are for Objective-C++ */
21710 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21711 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21712 /* The parameter-declaration-list is complete. */
21713 break;
21714 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21716 cp_token *token;
21718 /* Peek at the next token. */
21719 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21720 /* If it's an ellipsis, then the list is complete. */
21721 if (token->type == CPP_ELLIPSIS)
21722 break;
21723 /* Otherwise, there must be more parameters. Consume the
21724 `,'. */
21725 cp_lexer_consume_token (parser->lexer);
21726 /* When parsing something like:
21728 int i(float f, double d)
21730 we can tell after seeing the declaration for "f" that we
21731 are not looking at an initialization of a variable "i",
21732 but rather at the declaration of a function "i".
21734 Due to the fact that the parsing of template arguments
21735 (as specified to a template-id) requires backtracking we
21736 cannot use this technique when inside a template argument
21737 list. */
21738 if (!parser->in_template_argument_list_p
21739 && !parser->in_type_id_in_expr_p
21740 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21741 /* However, a parameter-declaration of the form
21742 "float(f)" (which is a valid declaration of a
21743 parameter "f") can also be interpreted as an
21744 expression (the conversion of "f" to "float"). */
21745 && !parenthesized_p)
21746 cp_parser_commit_to_tentative_parse (parser);
21748 else
21750 cp_parser_error (parser, "expected %<,%> or %<...%>");
21751 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21752 cp_parser_skip_to_closing_parenthesis (parser,
21753 /*recovering=*/true,
21754 /*or_comma=*/false,
21755 /*consume_paren=*/false);
21756 break;
21760 parser->in_unbraced_linkage_specification_p
21761 = saved_in_unbraced_linkage_specification_p;
21763 /* Reset implicit_template_scope if we are about to leave the function
21764 parameter list that introduced it. Note that for out-of-line member
21765 definitions, there will be one or more class scopes before we get to
21766 the template parameter scope. */
21768 if (cp_binding_level *its = parser->implicit_template_scope)
21769 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21771 while (maybe_its->kind == sk_class)
21772 maybe_its = maybe_its->level_chain;
21773 if (maybe_its == its)
21775 parser->implicit_template_parms = 0;
21776 parser->implicit_template_scope = 0;
21780 return parameters;
21783 /* Parse a parameter declaration.
21785 parameter-declaration:
21786 decl-specifier-seq ... [opt] declarator
21787 decl-specifier-seq declarator = assignment-expression
21788 decl-specifier-seq ... [opt] abstract-declarator [opt]
21789 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21791 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21792 declares a template parameter. (In that case, a non-nested `>'
21793 token encountered during the parsing of the assignment-expression
21794 is not interpreted as a greater-than operator.)
21796 Returns a representation of the parameter, or NULL if an error
21797 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21798 true iff the declarator is of the form "(p)". */
21800 static cp_parameter_declarator *
21801 cp_parser_parameter_declaration (cp_parser *parser,
21802 bool template_parm_p,
21803 bool *parenthesized_p)
21805 int declares_class_or_enum;
21806 cp_decl_specifier_seq decl_specifiers;
21807 cp_declarator *declarator;
21808 tree default_argument;
21809 cp_token *token = NULL, *declarator_token_start = NULL;
21810 const char *saved_message;
21811 bool template_parameter_pack_p = false;
21813 /* In a template parameter, `>' is not an operator.
21815 [temp.param]
21817 When parsing a default template-argument for a non-type
21818 template-parameter, the first non-nested `>' is taken as the end
21819 of the template parameter-list rather than a greater-than
21820 operator. */
21822 /* Type definitions may not appear in parameter types. */
21823 saved_message = parser->type_definition_forbidden_message;
21824 parser->type_definition_forbidden_message
21825 = G_("types may not be defined in parameter types");
21827 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21828 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21829 (current_template_parms)) : 0);
21831 /* Parse the declaration-specifiers. */
21832 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21833 cp_parser_decl_specifier_seq (parser,
21834 CP_PARSER_FLAGS_NONE,
21835 &decl_specifiers,
21836 &declares_class_or_enum);
21838 /* Complain about missing 'typename' or other invalid type names. */
21839 if (!decl_specifiers.any_type_specifiers_p
21840 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21841 decl_specifiers.type = error_mark_node;
21843 /* If an error occurred, there's no reason to attempt to parse the
21844 rest of the declaration. */
21845 if (cp_parser_error_occurred (parser))
21847 parser->type_definition_forbidden_message = saved_message;
21848 return NULL;
21851 /* Peek at the next token. */
21852 token = cp_lexer_peek_token (parser->lexer);
21854 /* If the next token is a `)', `,', `=', `>', or `...', then there
21855 is no declarator. However, when variadic templates are enabled,
21856 there may be a declarator following `...'. */
21857 if (token->type == CPP_CLOSE_PAREN
21858 || token->type == CPP_COMMA
21859 || token->type == CPP_EQ
21860 || token->type == CPP_GREATER)
21862 declarator = NULL;
21863 if (parenthesized_p)
21864 *parenthesized_p = false;
21866 /* Otherwise, there should be a declarator. */
21867 else
21869 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21870 parser->default_arg_ok_p = false;
21872 /* After seeing a decl-specifier-seq, if the next token is not a
21873 "(", there is no possibility that the code is a valid
21874 expression. Therefore, if parsing tentatively, we commit at
21875 this point. */
21876 if (!parser->in_template_argument_list_p
21877 /* In an expression context, having seen:
21879 (int((char ...
21881 we cannot be sure whether we are looking at a
21882 function-type (taking a "char" as a parameter) or a cast
21883 of some object of type "char" to "int". */
21884 && !parser->in_type_id_in_expr_p
21885 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21886 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21887 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21888 cp_parser_commit_to_tentative_parse (parser);
21889 /* Parse the declarator. */
21890 declarator_token_start = token;
21891 declarator = cp_parser_declarator (parser,
21892 CP_PARSER_DECLARATOR_EITHER,
21893 /*ctor_dtor_or_conv_p=*/NULL,
21894 parenthesized_p,
21895 /*member_p=*/false,
21896 /*friend_p=*/false);
21897 parser->default_arg_ok_p = saved_default_arg_ok_p;
21898 /* After the declarator, allow more attributes. */
21899 decl_specifiers.attributes
21900 = attr_chainon (decl_specifiers.attributes,
21901 cp_parser_attributes_opt (parser));
21903 /* If the declarator is a template parameter pack, remember that and
21904 clear the flag in the declarator itself so we don't get errors
21905 from grokdeclarator. */
21906 if (template_parm_p && declarator && declarator->parameter_pack_p)
21908 declarator->parameter_pack_p = false;
21909 template_parameter_pack_p = true;
21913 /* If the next token is an ellipsis, and we have not seen a declarator
21914 name, and if either the type of the declarator contains parameter
21915 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21916 for, eg, abbreviated integral type names), then we actually have a
21917 parameter pack expansion expression. Otherwise, leave the ellipsis
21918 for a C-style variadic function. */
21919 token = cp_lexer_peek_token (parser->lexer);
21921 /* If a function parameter pack was specified and an implicit template
21922 parameter was introduced during cp_parser_parameter_declaration,
21923 change any implicit parameters introduced into packs. */
21924 if (parser->implicit_template_parms
21925 && ((token->type == CPP_ELLIPSIS
21926 && declarator_can_be_parameter_pack (declarator))
21927 || (declarator && declarator->parameter_pack_p)))
21929 int latest_template_parm_idx = TREE_VEC_LENGTH
21930 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21932 if (latest_template_parm_idx != template_parm_idx)
21933 decl_specifiers.type = convert_generic_types_to_packs
21934 (decl_specifiers.type,
21935 template_parm_idx, latest_template_parm_idx);
21938 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21940 tree type = decl_specifiers.type;
21942 if (type && DECL_P (type))
21943 type = TREE_TYPE (type);
21945 if (((type
21946 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21947 && (template_parm_p || uses_parameter_packs (type)))
21948 || (!type && template_parm_p))
21949 && declarator_can_be_parameter_pack (declarator))
21951 /* Consume the `...'. */
21952 cp_lexer_consume_token (parser->lexer);
21953 maybe_warn_variadic_templates ();
21955 /* Build a pack expansion type */
21956 if (template_parm_p)
21957 template_parameter_pack_p = true;
21958 else if (declarator)
21959 declarator->parameter_pack_p = true;
21960 else
21961 decl_specifiers.type = make_pack_expansion (type);
21965 /* The restriction on defining new types applies only to the type
21966 of the parameter, not to the default argument. */
21967 parser->type_definition_forbidden_message = saved_message;
21969 /* If the next token is `=', then process a default argument. */
21970 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21972 tree type = decl_specifiers.type;
21973 token = cp_lexer_peek_token (parser->lexer);
21974 /* If we are defining a class, then the tokens that make up the
21975 default argument must be saved and processed later. */
21976 if (!template_parm_p && at_class_scope_p ()
21977 && TYPE_BEING_DEFINED (current_class_type)
21978 && !LAMBDA_TYPE_P (current_class_type))
21979 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21981 // A constrained-type-specifier may declare a type template-parameter.
21982 else if (declares_constrained_type_template_parameter (type))
21983 default_argument
21984 = cp_parser_default_type_template_argument (parser);
21986 // A constrained-type-specifier may declare a template-template-parameter.
21987 else if (declares_constrained_template_template_parameter (type))
21988 default_argument
21989 = cp_parser_default_template_template_argument (parser);
21991 /* Outside of a class definition, we can just parse the
21992 assignment-expression. */
21993 else
21994 default_argument
21995 = cp_parser_default_argument (parser, template_parm_p);
21997 if (!parser->default_arg_ok_p)
21999 permerror (token->location,
22000 "default arguments are only "
22001 "permitted for function parameters");
22003 else if ((declarator && declarator->parameter_pack_p)
22004 || template_parameter_pack_p
22005 || (decl_specifiers.type
22006 && PACK_EXPANSION_P (decl_specifiers.type)))
22008 /* Find the name of the parameter pack. */
22009 cp_declarator *id_declarator = declarator;
22010 while (id_declarator && id_declarator->kind != cdk_id)
22011 id_declarator = id_declarator->declarator;
22013 if (id_declarator && id_declarator->kind == cdk_id)
22014 error_at (declarator_token_start->location,
22015 template_parm_p
22016 ? G_("template parameter pack %qD "
22017 "cannot have a default argument")
22018 : G_("parameter pack %qD cannot have "
22019 "a default argument"),
22020 id_declarator->u.id.unqualified_name);
22021 else
22022 error_at (declarator_token_start->location,
22023 template_parm_p
22024 ? G_("template parameter pack cannot have "
22025 "a default argument")
22026 : G_("parameter pack cannot have a "
22027 "default argument"));
22029 default_argument = NULL_TREE;
22032 else
22033 default_argument = NULL_TREE;
22035 /* Generate a location for the parameter, ranging from the start of the
22036 initial token to the end of the final token (using input_location for
22037 the latter, set up by cp_lexer_set_source_position_from_token when
22038 consuming tokens).
22040 If we have a identifier, then use it for the caret location, e.g.
22042 extern int callee (int one, int (*two)(int, int), float three);
22043 ~~~~~~^~~~~~~~~~~~~~
22045 otherwise, reuse the start location for the caret location e.g.:
22047 extern int callee (int one, int (*)(int, int), float three);
22048 ^~~~~~~~~~~~~~~~~
22051 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22052 ? declarator->id_loc
22053 : decl_spec_token_start->location);
22054 location_t param_loc = make_location (caret_loc,
22055 decl_spec_token_start->location,
22056 input_location);
22058 return make_parameter_declarator (&decl_specifiers,
22059 declarator,
22060 default_argument,
22061 param_loc,
22062 template_parameter_pack_p);
22065 /* Parse a default argument and return it.
22067 TEMPLATE_PARM_P is true if this is a default argument for a
22068 non-type template parameter. */
22069 static tree
22070 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22072 tree default_argument = NULL_TREE;
22073 bool saved_greater_than_is_operator_p;
22074 bool saved_local_variables_forbidden_p;
22075 bool non_constant_p, is_direct_init;
22077 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22078 set correctly. */
22079 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22080 parser->greater_than_is_operator_p = !template_parm_p;
22081 /* Local variable names (and the `this' keyword) may not
22082 appear in a default argument. */
22083 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22084 parser->local_variables_forbidden_p = true;
22085 /* Parse the assignment-expression. */
22086 if (template_parm_p)
22087 push_deferring_access_checks (dk_no_deferred);
22088 tree saved_class_ptr = NULL_TREE;
22089 tree saved_class_ref = NULL_TREE;
22090 /* The "this" pointer is not valid in a default argument. */
22091 if (cfun)
22093 saved_class_ptr = current_class_ptr;
22094 cp_function_chain->x_current_class_ptr = NULL_TREE;
22095 saved_class_ref = current_class_ref;
22096 cp_function_chain->x_current_class_ref = NULL_TREE;
22098 default_argument
22099 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22100 /* Restore the "this" pointer. */
22101 if (cfun)
22103 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22104 cp_function_chain->x_current_class_ref = saved_class_ref;
22106 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22107 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22108 if (template_parm_p)
22109 pop_deferring_access_checks ();
22110 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22111 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22113 return default_argument;
22116 /* Parse a function-body.
22118 function-body:
22119 compound_statement */
22121 static void
22122 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22124 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22125 ? BCS_TRY_BLOCK : BCS_NORMAL),
22126 true);
22129 /* Parse a ctor-initializer-opt followed by a function-body. Return
22130 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22131 is true we are parsing a function-try-block. */
22133 static void
22134 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22135 bool in_function_try_block)
22137 tree body, list;
22138 const bool check_body_p =
22139 DECL_CONSTRUCTOR_P (current_function_decl)
22140 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
22141 tree last = NULL;
22143 /* Begin the function body. */
22144 body = begin_function_body ();
22145 /* Parse the optional ctor-initializer. */
22146 cp_parser_ctor_initializer_opt (parser);
22148 /* If we're parsing a constexpr constructor definition, we need
22149 to check that the constructor body is indeed empty. However,
22150 before we get to cp_parser_function_body lot of junk has been
22151 generated, so we can't just check that we have an empty block.
22152 Rather we take a snapshot of the outermost block, and check whether
22153 cp_parser_function_body changed its state. */
22154 if (check_body_p)
22156 list = cur_stmt_list;
22157 if (STATEMENT_LIST_TAIL (list))
22158 last = STATEMENT_LIST_TAIL (list)->stmt;
22160 /* Parse the function-body. */
22161 cp_parser_function_body (parser, in_function_try_block);
22162 if (check_body_p)
22163 check_constexpr_ctor_body (last, list, /*complain=*/true);
22164 /* Finish the function body. */
22165 finish_function_body (body);
22168 /* Parse an initializer.
22170 initializer:
22171 = initializer-clause
22172 ( expression-list )
22174 Returns an expression representing the initializer. If no
22175 initializer is present, NULL_TREE is returned.
22177 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22178 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22179 set to TRUE if there is no initializer present. If there is an
22180 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22181 is set to true; otherwise it is set to false. */
22183 static tree
22184 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22185 bool* non_constant_p, bool subexpression_p)
22187 cp_token *token;
22188 tree init;
22190 /* Peek at the next token. */
22191 token = cp_lexer_peek_token (parser->lexer);
22193 /* Let our caller know whether or not this initializer was
22194 parenthesized. */
22195 *is_direct_init = (token->type != CPP_EQ);
22196 /* Assume that the initializer is constant. */
22197 *non_constant_p = false;
22199 if (token->type == CPP_EQ)
22201 /* Consume the `='. */
22202 cp_lexer_consume_token (parser->lexer);
22203 /* Parse the initializer-clause. */
22204 init = cp_parser_initializer_clause (parser, non_constant_p);
22206 else if (token->type == CPP_OPEN_PAREN)
22208 vec<tree, va_gc> *vec;
22209 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22210 /*cast_p=*/false,
22211 /*allow_expansion_p=*/true,
22212 non_constant_p);
22213 if (vec == NULL)
22214 return error_mark_node;
22215 init = build_tree_list_vec (vec);
22216 release_tree_vector (vec);
22218 else if (token->type == CPP_OPEN_BRACE)
22220 cp_lexer_set_source_position (parser->lexer);
22221 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22222 init = cp_parser_braced_list (parser, non_constant_p);
22223 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22225 else
22227 /* Anything else is an error. */
22228 cp_parser_error (parser, "expected initializer");
22229 init = error_mark_node;
22232 if (!subexpression_p && check_for_bare_parameter_packs (init))
22233 init = error_mark_node;
22235 return init;
22238 /* Parse an initializer-clause.
22240 initializer-clause:
22241 assignment-expression
22242 braced-init-list
22244 Returns an expression representing the initializer.
22246 If the `assignment-expression' production is used the value
22247 returned is simply a representation for the expression.
22249 Otherwise, calls cp_parser_braced_list. */
22251 static cp_expr
22252 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22254 cp_expr initializer;
22256 /* Assume the expression is constant. */
22257 *non_constant_p = false;
22259 /* If it is not a `{', then we are looking at an
22260 assignment-expression. */
22261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22263 initializer
22264 = cp_parser_constant_expression (parser,
22265 /*allow_non_constant_p=*/true,
22266 non_constant_p);
22268 else
22269 initializer = cp_parser_braced_list (parser, non_constant_p);
22271 return initializer;
22274 /* Parse a brace-enclosed initializer list.
22276 braced-init-list:
22277 { initializer-list , [opt] }
22278 { designated-initializer-list , [opt] }
22281 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22282 the elements of the initializer-list (or NULL, if the last
22283 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22284 NULL_TREE. There is no way to detect whether or not the optional
22285 trailing `,' was provided. NON_CONSTANT_P is as for
22286 cp_parser_initializer. */
22288 static cp_expr
22289 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22291 tree initializer;
22292 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22294 /* Consume the `{' token. */
22295 matching_braces braces;
22296 braces.require_open (parser);
22297 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22298 initializer = make_node (CONSTRUCTOR);
22299 /* If it's not a `}', then there is a non-trivial initializer. */
22300 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22302 /* Parse the initializer list. */
22303 CONSTRUCTOR_ELTS (initializer)
22304 = cp_parser_initializer_list (parser, non_constant_p);
22305 /* A trailing `,' token is allowed. */
22306 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22307 cp_lexer_consume_token (parser->lexer);
22309 else
22310 *non_constant_p = false;
22311 /* Now, there should be a trailing `}'. */
22312 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22313 braces.require_close (parser);
22314 TREE_TYPE (initializer) = init_list_type_node;
22316 cp_expr result (initializer);
22317 /* Build a location of the form:
22318 { ... }
22319 ^~~~~~~
22320 with caret==start at the open brace, finish at the close brace. */
22321 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22322 result.set_location (combined_loc);
22323 return result;
22326 /* Consume tokens up to, and including, the next non-nested closing `]'.
22327 Returns true iff we found a closing `]'. */
22329 static bool
22330 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22332 unsigned square_depth = 0;
22334 while (true)
22336 cp_token * token = cp_lexer_peek_token (parser->lexer);
22338 switch (token->type)
22340 case CPP_EOF:
22341 case CPP_PRAGMA_EOL:
22342 /* If we've run out of tokens, then there is no closing `]'. */
22343 return false;
22345 case CPP_OPEN_SQUARE:
22346 ++square_depth;
22347 break;
22349 case CPP_CLOSE_SQUARE:
22350 if (!square_depth--)
22352 cp_lexer_consume_token (parser->lexer);
22353 return true;
22355 break;
22357 default:
22358 break;
22361 /* Consume the token. */
22362 cp_lexer_consume_token (parser->lexer);
22366 /* Return true if we are looking at an array-designator, false otherwise. */
22368 static bool
22369 cp_parser_array_designator_p (cp_parser *parser)
22371 /* Consume the `['. */
22372 cp_lexer_consume_token (parser->lexer);
22374 cp_lexer_save_tokens (parser->lexer);
22376 /* Skip tokens until the next token is a closing square bracket.
22377 If we find the closing `]', and the next token is a `=', then
22378 we are looking at an array designator. */
22379 bool array_designator_p
22380 = (cp_parser_skip_to_closing_square_bracket (parser)
22381 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22383 /* Roll back the tokens we skipped. */
22384 cp_lexer_rollback_tokens (parser->lexer);
22386 return array_designator_p;
22389 /* Parse an initializer-list.
22391 initializer-list:
22392 initializer-clause ... [opt]
22393 initializer-list , initializer-clause ... [opt]
22395 C++2A Extension:
22397 designated-initializer-list:
22398 designated-initializer-clause
22399 designated-initializer-list , designated-initializer-clause
22401 designated-initializer-clause:
22402 designator brace-or-equal-initializer
22404 designator:
22405 . identifier
22407 GNU Extension:
22409 initializer-list:
22410 designation initializer-clause ...[opt]
22411 initializer-list , designation initializer-clause ...[opt]
22413 designation:
22414 . identifier =
22415 identifier :
22416 [ constant-expression ] =
22418 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22419 for the initializer. If the INDEX of the elt is non-NULL, it is the
22420 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22421 as for cp_parser_initializer. */
22423 static vec<constructor_elt, va_gc> *
22424 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22426 vec<constructor_elt, va_gc> *v = NULL;
22427 bool first_p = true;
22428 tree first_designator = NULL_TREE;
22430 /* Assume all of the expressions are constant. */
22431 *non_constant_p = false;
22433 /* Parse the rest of the list. */
22434 while (true)
22436 cp_token *token;
22437 tree designator;
22438 tree initializer;
22439 bool clause_non_constant_p;
22440 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22442 /* Handle the C++2A syntax, '. id ='. */
22443 if ((cxx_dialect >= cxx2a
22444 || cp_parser_allow_gnu_extensions_p (parser))
22445 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22446 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22447 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22448 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22449 == CPP_OPEN_BRACE)))
22451 if (cxx_dialect < cxx2a)
22452 pedwarn (loc, OPT_Wpedantic,
22453 "C++ designated initializers only available with "
22454 "-std=c++2a or -std=gnu++2a");
22455 /* Consume the `.'. */
22456 cp_lexer_consume_token (parser->lexer);
22457 /* Consume the identifier. */
22458 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22459 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22460 /* Consume the `='. */
22461 cp_lexer_consume_token (parser->lexer);
22463 /* Also, if the next token is an identifier and the following one is a
22464 colon, we are looking at the GNU designated-initializer
22465 syntax. */
22466 else if (cp_parser_allow_gnu_extensions_p (parser)
22467 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22468 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22469 == CPP_COLON))
22471 /* Warn the user that they are using an extension. */
22472 pedwarn (loc, OPT_Wpedantic,
22473 "ISO C++ does not allow GNU designated initializers");
22474 /* Consume the identifier. */
22475 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22476 /* Consume the `:'. */
22477 cp_lexer_consume_token (parser->lexer);
22479 /* Also handle C99 array designators, '[ const ] ='. */
22480 else if (cp_parser_allow_gnu_extensions_p (parser)
22481 && !c_dialect_objc ()
22482 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22484 /* In C++11, [ could start a lambda-introducer. */
22485 bool non_const = false;
22487 cp_parser_parse_tentatively (parser);
22489 if (!cp_parser_array_designator_p (parser))
22491 cp_parser_simulate_error (parser);
22492 designator = NULL_TREE;
22494 else
22496 designator = cp_parser_constant_expression (parser, true,
22497 &non_const);
22498 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22499 cp_parser_require (parser, CPP_EQ, RT_EQ);
22502 if (!cp_parser_parse_definitely (parser))
22503 designator = NULL_TREE;
22504 else if (non_const
22505 && (!require_potential_rvalue_constant_expression
22506 (designator)))
22507 designator = NULL_TREE;
22508 if (designator)
22509 /* Warn the user that they are using an extension. */
22510 pedwarn (loc, OPT_Wpedantic,
22511 "ISO C++ does not allow C99 designated initializers");
22513 else
22514 designator = NULL_TREE;
22516 if (first_p)
22518 first_designator = designator;
22519 first_p = false;
22521 else if (cxx_dialect >= cxx2a
22522 && first_designator != error_mark_node
22523 && (!first_designator != !designator))
22525 error_at (loc, "either all initializer clauses should be designated "
22526 "or none of them should be");
22527 first_designator = error_mark_node;
22529 else if (cxx_dialect < cxx2a && !first_designator)
22530 first_designator = designator;
22532 /* Parse the initializer. */
22533 initializer = cp_parser_initializer_clause (parser,
22534 &clause_non_constant_p);
22535 /* If any clause is non-constant, so is the entire initializer. */
22536 if (clause_non_constant_p)
22537 *non_constant_p = true;
22539 /* If we have an ellipsis, this is an initializer pack
22540 expansion. */
22541 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22543 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22545 /* Consume the `...'. */
22546 cp_lexer_consume_token (parser->lexer);
22548 if (designator && cxx_dialect >= cxx2a)
22549 error_at (loc,
22550 "%<...%> not allowed in designated initializer list");
22552 /* Turn the initializer into an initializer expansion. */
22553 initializer = make_pack_expansion (initializer);
22556 /* Add it to the vector. */
22557 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22559 /* If the next token is not a comma, we have reached the end of
22560 the list. */
22561 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22562 break;
22564 /* Peek at the next token. */
22565 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22566 /* If the next token is a `}', then we're still done. An
22567 initializer-clause can have a trailing `,' after the
22568 initializer-list and before the closing `}'. */
22569 if (token->type == CPP_CLOSE_BRACE)
22570 break;
22572 /* Consume the `,' token. */
22573 cp_lexer_consume_token (parser->lexer);
22576 /* The same identifier shall not appear in multiple designators
22577 of a designated-initializer-list. */
22578 if (first_designator)
22580 unsigned int i;
22581 tree designator, val;
22582 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22583 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22585 if (IDENTIFIER_MARKED (designator))
22587 error_at (cp_expr_loc_or_loc (val, input_location),
22588 "%<.%s%> designator used multiple times in "
22589 "the same initializer list",
22590 IDENTIFIER_POINTER (designator));
22591 (*v)[i].index = error_mark_node;
22593 else
22594 IDENTIFIER_MARKED (designator) = 1;
22596 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22597 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22598 IDENTIFIER_MARKED (designator) = 0;
22601 return v;
22604 /* Classes [gram.class] */
22606 /* Parse a class-name.
22608 class-name:
22609 identifier
22610 template-id
22612 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22613 to indicate that names looked up in dependent types should be
22614 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22615 keyword has been used to indicate that the name that appears next
22616 is a template. TAG_TYPE indicates the explicit tag given before
22617 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22618 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22619 is the class being defined in a class-head. If ENUM_OK is TRUE,
22620 enum-names are also accepted.
22622 Returns the TYPE_DECL representing the class. */
22624 static tree
22625 cp_parser_class_name (cp_parser *parser,
22626 bool typename_keyword_p,
22627 bool template_keyword_p,
22628 enum tag_types tag_type,
22629 bool check_dependency_p,
22630 bool class_head_p,
22631 bool is_declaration,
22632 bool enum_ok)
22634 tree decl;
22635 tree scope;
22636 bool typename_p;
22637 cp_token *token;
22638 tree identifier = NULL_TREE;
22640 /* All class-names start with an identifier. */
22641 token = cp_lexer_peek_token (parser->lexer);
22642 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22644 cp_parser_error (parser, "expected class-name");
22645 return error_mark_node;
22648 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22649 to a template-id, so we save it here. */
22650 scope = parser->scope;
22651 if (scope == error_mark_node)
22652 return error_mark_node;
22654 /* Any name names a type if we're following the `typename' keyword
22655 in a qualified name where the enclosing scope is type-dependent. */
22656 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22657 && dependent_type_p (scope));
22658 /* Handle the common case (an identifier, but not a template-id)
22659 efficiently. */
22660 if (token->type == CPP_NAME
22661 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22663 cp_token *identifier_token;
22664 bool ambiguous_p;
22666 /* Look for the identifier. */
22667 identifier_token = cp_lexer_peek_token (parser->lexer);
22668 ambiguous_p = identifier_token->error_reported;
22669 identifier = cp_parser_identifier (parser);
22670 /* If the next token isn't an identifier, we are certainly not
22671 looking at a class-name. */
22672 if (identifier == error_mark_node)
22673 decl = error_mark_node;
22674 /* If we know this is a type-name, there's no need to look it
22675 up. */
22676 else if (typename_p)
22677 decl = identifier;
22678 else
22680 tree ambiguous_decls;
22681 /* If we already know that this lookup is ambiguous, then
22682 we've already issued an error message; there's no reason
22683 to check again. */
22684 if (ambiguous_p)
22686 cp_parser_simulate_error (parser);
22687 return error_mark_node;
22689 /* If the next token is a `::', then the name must be a type
22690 name.
22692 [basic.lookup.qual]
22694 During the lookup for a name preceding the :: scope
22695 resolution operator, object, function, and enumerator
22696 names are ignored. */
22697 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22698 tag_type = scope_type;
22699 /* Look up the name. */
22700 decl = cp_parser_lookup_name (parser, identifier,
22701 tag_type,
22702 /*is_template=*/false,
22703 /*is_namespace=*/false,
22704 check_dependency_p,
22705 &ambiguous_decls,
22706 identifier_token->location);
22707 if (ambiguous_decls)
22709 if (cp_parser_parsing_tentatively (parser))
22710 cp_parser_simulate_error (parser);
22711 return error_mark_node;
22715 else
22717 /* Try a template-id. */
22718 decl = cp_parser_template_id (parser, template_keyword_p,
22719 check_dependency_p,
22720 tag_type,
22721 is_declaration);
22722 if (decl == error_mark_node)
22723 return error_mark_node;
22726 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22728 /* If this is a typename, create a TYPENAME_TYPE. */
22729 if (typename_p && decl != error_mark_node)
22731 decl = make_typename_type (scope, decl, typename_type,
22732 /*complain=*/tf_error);
22733 if (decl != error_mark_node)
22734 decl = TYPE_NAME (decl);
22737 decl = strip_using_decl (decl);
22739 /* Check to see that it is really the name of a class. */
22740 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22741 && identifier_p (TREE_OPERAND (decl, 0))
22742 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22743 /* Situations like this:
22745 template <typename T> struct A {
22746 typename T::template X<int>::I i;
22749 are problematic. Is `T::template X<int>' a class-name? The
22750 standard does not seem to be definitive, but there is no other
22751 valid interpretation of the following `::'. Therefore, those
22752 names are considered class-names. */
22754 decl = make_typename_type (scope, decl, tag_type, tf_error);
22755 if (decl != error_mark_node)
22756 decl = TYPE_NAME (decl);
22758 else if (TREE_CODE (decl) != TYPE_DECL
22759 || TREE_TYPE (decl) == error_mark_node
22760 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22761 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22762 /* In Objective-C 2.0, a classname followed by '.' starts a
22763 dot-syntax expression, and it's not a type-name. */
22764 || (c_dialect_objc ()
22765 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22766 && objc_is_class_name (decl)))
22767 decl = error_mark_node;
22769 if (decl == error_mark_node)
22770 cp_parser_error (parser, "expected class-name");
22771 else if (identifier && !parser->scope)
22772 maybe_note_name_used_in_class (identifier, decl);
22774 return decl;
22777 /* Parse a class-specifier.
22779 class-specifier:
22780 class-head { member-specification [opt] }
22782 Returns the TREE_TYPE representing the class. */
22784 static tree
22785 cp_parser_class_specifier_1 (cp_parser* parser)
22787 tree type;
22788 tree attributes = NULL_TREE;
22789 bool nested_name_specifier_p;
22790 unsigned saved_num_template_parameter_lists;
22791 bool saved_in_function_body;
22792 unsigned char in_statement;
22793 bool in_switch_statement_p;
22794 bool saved_in_unbraced_linkage_specification_p;
22795 tree old_scope = NULL_TREE;
22796 tree scope = NULL_TREE;
22797 cp_token *closing_brace;
22799 push_deferring_access_checks (dk_no_deferred);
22801 /* Parse the class-head. */
22802 type = cp_parser_class_head (parser,
22803 &nested_name_specifier_p);
22804 /* If the class-head was a semantic disaster, skip the entire body
22805 of the class. */
22806 if (!type)
22808 cp_parser_skip_to_end_of_block_or_statement (parser);
22809 pop_deferring_access_checks ();
22810 return error_mark_node;
22813 /* Look for the `{'. */
22814 matching_braces braces;
22815 if (!braces.require_open (parser))
22817 pop_deferring_access_checks ();
22818 return error_mark_node;
22821 cp_ensure_no_omp_declare_simd (parser);
22822 cp_ensure_no_oacc_routine (parser);
22824 /* Issue an error message if type-definitions are forbidden here. */
22825 cp_parser_check_type_definition (parser);
22826 /* Remember that we are defining one more class. */
22827 ++parser->num_classes_being_defined;
22828 /* Inside the class, surrounding template-parameter-lists do not
22829 apply. */
22830 saved_num_template_parameter_lists
22831 = parser->num_template_parameter_lists;
22832 parser->num_template_parameter_lists = 0;
22833 /* We are not in a function body. */
22834 saved_in_function_body = parser->in_function_body;
22835 parser->in_function_body = false;
22836 /* Or in a loop. */
22837 in_statement = parser->in_statement;
22838 parser->in_statement = 0;
22839 /* Or in a switch. */
22840 in_switch_statement_p = parser->in_switch_statement_p;
22841 parser->in_switch_statement_p = false;
22842 /* We are not immediately inside an extern "lang" block. */
22843 saved_in_unbraced_linkage_specification_p
22844 = parser->in_unbraced_linkage_specification_p;
22845 parser->in_unbraced_linkage_specification_p = false;
22847 // Associate constraints with the type.
22848 if (flag_concepts)
22849 type = associate_classtype_constraints (type);
22851 /* Start the class. */
22852 if (nested_name_specifier_p)
22854 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22855 old_scope = push_inner_scope (scope);
22857 type = begin_class_definition (type);
22859 if (type == error_mark_node)
22860 /* If the type is erroneous, skip the entire body of the class. */
22861 cp_parser_skip_to_closing_brace (parser);
22862 else
22863 /* Parse the member-specification. */
22864 cp_parser_member_specification_opt (parser);
22866 /* Look for the trailing `}'. */
22867 closing_brace = braces.require_close (parser);
22868 /* Look for trailing attributes to apply to this class. */
22869 if (cp_parser_allow_gnu_extensions_p (parser))
22870 attributes = cp_parser_gnu_attributes_opt (parser);
22871 if (type != error_mark_node)
22872 type = finish_struct (type, attributes);
22873 if (nested_name_specifier_p)
22874 pop_inner_scope (old_scope, scope);
22876 /* We've finished a type definition. Check for the common syntax
22877 error of forgetting a semicolon after the definition. We need to
22878 be careful, as we can't just check for not-a-semicolon and be done
22879 with it; the user might have typed:
22881 class X { } c = ...;
22882 class X { } *p = ...;
22884 and so forth. Instead, enumerate all the possible tokens that
22885 might follow this production; if we don't see one of them, then
22886 complain and silently insert the semicolon. */
22888 cp_token *token = cp_lexer_peek_token (parser->lexer);
22889 bool want_semicolon = true;
22891 if (cp_next_tokens_can_be_std_attribute_p (parser))
22892 /* Don't try to parse c++11 attributes here. As per the
22893 grammar, that should be a task for
22894 cp_parser_decl_specifier_seq. */
22895 want_semicolon = false;
22897 switch (token->type)
22899 case CPP_NAME:
22900 case CPP_SEMICOLON:
22901 case CPP_MULT:
22902 case CPP_AND:
22903 case CPP_OPEN_PAREN:
22904 case CPP_CLOSE_PAREN:
22905 case CPP_COMMA:
22906 want_semicolon = false;
22907 break;
22909 /* While it's legal for type qualifiers and storage class
22910 specifiers to follow type definitions in the grammar, only
22911 compiler testsuites contain code like that. Assume that if
22912 we see such code, then what we're really seeing is a case
22913 like:
22915 class X { }
22916 const <type> var = ...;
22920 class Y { }
22921 static <type> func (...) ...
22923 i.e. the qualifier or specifier applies to the next
22924 declaration. To do so, however, we need to look ahead one
22925 more token to see if *that* token is a type specifier.
22927 This code could be improved to handle:
22929 class Z { }
22930 static const <type> var = ...; */
22931 case CPP_KEYWORD:
22932 if (keyword_is_decl_specifier (token->keyword))
22934 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22936 /* Handling user-defined types here would be nice, but very
22937 tricky. */
22938 want_semicolon
22939 = (lookahead->type == CPP_KEYWORD
22940 && keyword_begins_type_specifier (lookahead->keyword));
22942 break;
22943 default:
22944 break;
22947 /* If we don't have a type, then something is very wrong and we
22948 shouldn't try to do anything clever. Likewise for not seeing the
22949 closing brace. */
22950 if (closing_brace && TYPE_P (type) && want_semicolon)
22952 /* Locate the closing brace. */
22953 cp_token_position prev
22954 = cp_lexer_previous_token_position (parser->lexer);
22955 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22956 location_t loc = prev_token->location;
22958 /* We want to suggest insertion of a ';' immediately *after* the
22959 closing brace, so, if we can, offset the location by 1 column. */
22960 location_t next_loc = loc;
22961 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22962 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22964 rich_location richloc (line_table, next_loc);
22966 /* If we successfully offset the location, suggest the fix-it. */
22967 if (next_loc != loc)
22968 richloc.add_fixit_insert_before (next_loc, ";");
22970 if (CLASSTYPE_DECLARED_CLASS (type))
22971 error_at (&richloc,
22972 "expected %<;%> after class definition");
22973 else if (TREE_CODE (type) == RECORD_TYPE)
22974 error_at (&richloc,
22975 "expected %<;%> after struct definition");
22976 else if (TREE_CODE (type) == UNION_TYPE)
22977 error_at (&richloc,
22978 "expected %<;%> after union definition");
22979 else
22980 gcc_unreachable ();
22982 /* Unget one token and smash it to look as though we encountered
22983 a semicolon in the input stream. */
22984 cp_lexer_set_token_position (parser->lexer, prev);
22985 token = cp_lexer_peek_token (parser->lexer);
22986 token->type = CPP_SEMICOLON;
22987 token->keyword = RID_MAX;
22991 /* If this class is not itself within the scope of another class,
22992 then we need to parse the bodies of all of the queued function
22993 definitions. Note that the queued functions defined in a class
22994 are not always processed immediately following the
22995 class-specifier for that class. Consider:
22997 struct A {
22998 struct B { void f() { sizeof (A); } };
23001 If `f' were processed before the processing of `A' were
23002 completed, there would be no way to compute the size of `A'.
23003 Note that the nesting we are interested in here is lexical --
23004 not the semantic nesting given by TYPE_CONTEXT. In particular,
23005 for:
23007 struct A { struct B; };
23008 struct A::B { void f() { } };
23010 there is no need to delay the parsing of `A::B::f'. */
23011 if (--parser->num_classes_being_defined == 0)
23013 tree decl;
23014 tree class_type = NULL_TREE;
23015 tree pushed_scope = NULL_TREE;
23016 unsigned ix;
23017 cp_default_arg_entry *e;
23018 tree save_ccp, save_ccr;
23020 if (any_erroneous_template_args_p (type))
23022 /* Skip default arguments, NSDMIs, etc, in order to improve
23023 error recovery (c++/71169, c++/71832). */
23024 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23025 vec_safe_truncate (unparsed_nsdmis, 0);
23026 vec_safe_truncate (unparsed_classes, 0);
23027 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23030 /* In a first pass, parse default arguments to the functions.
23031 Then, in a second pass, parse the bodies of the functions.
23032 This two-phased approach handles cases like:
23034 struct S {
23035 void f() { g(); }
23036 void g(int i = 3);
23040 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23042 decl = e->decl;
23043 /* If there are default arguments that have not yet been processed,
23044 take care of them now. */
23045 if (class_type != e->class_type)
23047 if (pushed_scope)
23048 pop_scope (pushed_scope);
23049 class_type = e->class_type;
23050 pushed_scope = push_scope (class_type);
23052 /* Make sure that any template parameters are in scope. */
23053 maybe_begin_member_template_processing (decl);
23054 /* Parse the default argument expressions. */
23055 cp_parser_late_parsing_default_args (parser, decl);
23056 /* Remove any template parameters from the symbol table. */
23057 maybe_end_member_template_processing ();
23059 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23060 /* Now parse any NSDMIs. */
23061 save_ccp = current_class_ptr;
23062 save_ccr = current_class_ref;
23063 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23065 if (class_type != DECL_CONTEXT (decl))
23067 if (pushed_scope)
23068 pop_scope (pushed_scope);
23069 class_type = DECL_CONTEXT (decl);
23070 pushed_scope = push_scope (class_type);
23072 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23073 cp_parser_late_parsing_nsdmi (parser, decl);
23075 vec_safe_truncate (unparsed_nsdmis, 0);
23076 current_class_ptr = save_ccp;
23077 current_class_ref = save_ccr;
23078 if (pushed_scope)
23079 pop_scope (pushed_scope);
23081 /* Now do some post-NSDMI bookkeeping. */
23082 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23083 after_nsdmi_defaulted_late_checks (class_type);
23084 vec_safe_truncate (unparsed_classes, 0);
23085 after_nsdmi_defaulted_late_checks (type);
23087 /* Now parse the body of the functions. */
23088 if (flag_openmp)
23090 /* OpenMP UDRs need to be parsed before all other functions. */
23091 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23092 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23093 cp_parser_late_parsing_for_member (parser, decl);
23094 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23095 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23096 cp_parser_late_parsing_for_member (parser, decl);
23098 else
23099 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23100 cp_parser_late_parsing_for_member (parser, decl);
23101 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23103 else
23104 vec_safe_push (unparsed_classes, type);
23106 /* Put back any saved access checks. */
23107 pop_deferring_access_checks ();
23109 /* Restore saved state. */
23110 parser->in_switch_statement_p = in_switch_statement_p;
23111 parser->in_statement = in_statement;
23112 parser->in_function_body = saved_in_function_body;
23113 parser->num_template_parameter_lists
23114 = saved_num_template_parameter_lists;
23115 parser->in_unbraced_linkage_specification_p
23116 = saved_in_unbraced_linkage_specification_p;
23118 return type;
23121 static tree
23122 cp_parser_class_specifier (cp_parser* parser)
23124 tree ret;
23125 timevar_push (TV_PARSE_STRUCT);
23126 ret = cp_parser_class_specifier_1 (parser);
23127 timevar_pop (TV_PARSE_STRUCT);
23128 return ret;
23131 /* Parse a class-head.
23133 class-head:
23134 class-key identifier [opt] base-clause [opt]
23135 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23136 class-key nested-name-specifier [opt] template-id
23137 base-clause [opt]
23139 class-virt-specifier:
23140 final
23142 GNU Extensions:
23143 class-key attributes identifier [opt] base-clause [opt]
23144 class-key attributes nested-name-specifier identifier base-clause [opt]
23145 class-key attributes nested-name-specifier [opt] template-id
23146 base-clause [opt]
23148 Upon return BASES is initialized to the list of base classes (or
23149 NULL, if there are none) in the same form returned by
23150 cp_parser_base_clause.
23152 Returns the TYPE of the indicated class. Sets
23153 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23154 involving a nested-name-specifier was used, and FALSE otherwise.
23156 Returns error_mark_node if this is not a class-head.
23158 Returns NULL_TREE if the class-head is syntactically valid, but
23159 semantically invalid in a way that means we should skip the entire
23160 body of the class. */
23162 static tree
23163 cp_parser_class_head (cp_parser* parser,
23164 bool* nested_name_specifier_p)
23166 tree nested_name_specifier;
23167 enum tag_types class_key;
23168 tree id = NULL_TREE;
23169 tree type = NULL_TREE;
23170 tree attributes;
23171 tree bases;
23172 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23173 bool template_id_p = false;
23174 bool qualified_p = false;
23175 bool invalid_nested_name_p = false;
23176 bool invalid_explicit_specialization_p = false;
23177 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23178 tree pushed_scope = NULL_TREE;
23179 unsigned num_templates;
23180 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23181 /* Assume no nested-name-specifier will be present. */
23182 *nested_name_specifier_p = false;
23183 /* Assume no template parameter lists will be used in defining the
23184 type. */
23185 num_templates = 0;
23186 parser->colon_corrects_to_scope_p = false;
23188 /* Look for the class-key. */
23189 class_key = cp_parser_class_key (parser);
23190 if (class_key == none_type)
23191 return error_mark_node;
23193 location_t class_head_start_location = input_location;
23195 /* Parse the attributes. */
23196 attributes = cp_parser_attributes_opt (parser);
23198 /* If the next token is `::', that is invalid -- but sometimes
23199 people do try to write:
23201 struct ::S {};
23203 Handle this gracefully by accepting the extra qualifier, and then
23204 issuing an error about it later if this really is a
23205 class-head. If it turns out just to be an elaborated type
23206 specifier, remain silent. */
23207 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23208 qualified_p = true;
23210 push_deferring_access_checks (dk_no_check);
23212 /* Determine the name of the class. Begin by looking for an
23213 optional nested-name-specifier. */
23214 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23215 nested_name_specifier
23216 = cp_parser_nested_name_specifier_opt (parser,
23217 /*typename_keyword_p=*/false,
23218 /*check_dependency_p=*/false,
23219 /*type_p=*/true,
23220 /*is_declaration=*/false);
23221 /* If there was a nested-name-specifier, then there *must* be an
23222 identifier. */
23224 cp_token *bad_template_keyword = NULL;
23226 if (nested_name_specifier)
23228 type_start_token = cp_lexer_peek_token (parser->lexer);
23229 /* Although the grammar says `identifier', it really means
23230 `class-name' or `template-name'. You are only allowed to
23231 define a class that has already been declared with this
23232 syntax.
23234 The proposed resolution for Core Issue 180 says that wherever
23235 you see `class T::X' you should treat `X' as a type-name.
23237 It is OK to define an inaccessible class; for example:
23239 class A { class B; };
23240 class A::B {};
23242 We do not know if we will see a class-name, or a
23243 template-name. We look for a class-name first, in case the
23244 class-name is a template-id; if we looked for the
23245 template-name first we would stop after the template-name. */
23246 cp_parser_parse_tentatively (parser);
23247 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23248 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23249 type = cp_parser_class_name (parser,
23250 /*typename_keyword_p=*/false,
23251 /*template_keyword_p=*/false,
23252 class_type,
23253 /*check_dependency_p=*/false,
23254 /*class_head_p=*/true,
23255 /*is_declaration=*/false);
23256 /* If that didn't work, ignore the nested-name-specifier. */
23257 if (!cp_parser_parse_definitely (parser))
23259 invalid_nested_name_p = true;
23260 type_start_token = cp_lexer_peek_token (parser->lexer);
23261 id = cp_parser_identifier (parser);
23262 if (id == error_mark_node)
23263 id = NULL_TREE;
23265 /* If we could not find a corresponding TYPE, treat this
23266 declaration like an unqualified declaration. */
23267 if (type == error_mark_node)
23268 nested_name_specifier = NULL_TREE;
23269 /* Otherwise, count the number of templates used in TYPE and its
23270 containing scopes. */
23271 else
23272 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23274 /* Otherwise, the identifier is optional. */
23275 else
23277 /* We don't know whether what comes next is a template-id,
23278 an identifier, or nothing at all. */
23279 cp_parser_parse_tentatively (parser);
23280 /* Check for a template-id. */
23281 type_start_token = cp_lexer_peek_token (parser->lexer);
23282 id = cp_parser_template_id (parser,
23283 /*template_keyword_p=*/false,
23284 /*check_dependency_p=*/true,
23285 class_key,
23286 /*is_declaration=*/true);
23287 /* If that didn't work, it could still be an identifier. */
23288 if (!cp_parser_parse_definitely (parser))
23290 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23292 type_start_token = cp_lexer_peek_token (parser->lexer);
23293 id = cp_parser_identifier (parser);
23295 else
23296 id = NULL_TREE;
23298 else
23300 template_id_p = true;
23301 ++num_templates;
23305 pop_deferring_access_checks ();
23307 if (id)
23309 cp_parser_check_for_invalid_template_id (parser, id,
23310 class_key,
23311 type_start_token->location);
23313 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23315 /* If it's not a `:' or a `{' then we can't really be looking at a
23316 class-head, since a class-head only appears as part of a
23317 class-specifier. We have to detect this situation before calling
23318 xref_tag, since that has irreversible side-effects. */
23319 if (!cp_parser_next_token_starts_class_definition_p (parser))
23321 cp_parser_error (parser, "expected %<{%> or %<:%>");
23322 type = error_mark_node;
23323 goto out;
23326 /* At this point, we're going ahead with the class-specifier, even
23327 if some other problem occurs. */
23328 cp_parser_commit_to_tentative_parse (parser);
23329 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23331 cp_parser_error (parser,
23332 "cannot specify %<override%> for a class");
23333 type = error_mark_node;
23334 goto out;
23336 /* Issue the error about the overly-qualified name now. */
23337 if (qualified_p)
23339 cp_parser_error (parser,
23340 "global qualification of class name is invalid");
23341 type = error_mark_node;
23342 goto out;
23344 else if (invalid_nested_name_p)
23346 cp_parser_error (parser,
23347 "qualified name does not name a class");
23348 type = error_mark_node;
23349 goto out;
23351 else if (nested_name_specifier)
23353 tree scope;
23355 if (bad_template_keyword)
23356 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23357 keyword template shall not appear at the top level. */
23358 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23359 "keyword %<template%> not allowed in class-head-name");
23361 /* Reject typedef-names in class heads. */
23362 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23364 error_at (type_start_token->location,
23365 "invalid class name in declaration of %qD",
23366 type);
23367 type = NULL_TREE;
23368 goto done;
23371 /* Figure out in what scope the declaration is being placed. */
23372 scope = current_scope ();
23373 /* If that scope does not contain the scope in which the
23374 class was originally declared, the program is invalid. */
23375 if (scope && !is_ancestor (scope, nested_name_specifier))
23377 if (at_namespace_scope_p ())
23378 error_at (type_start_token->location,
23379 "declaration of %qD in namespace %qD which does not "
23380 "enclose %qD",
23381 type, scope, nested_name_specifier);
23382 else
23383 error_at (type_start_token->location,
23384 "declaration of %qD in %qD which does not enclose %qD",
23385 type, scope, nested_name_specifier);
23386 type = NULL_TREE;
23387 goto done;
23389 /* [dcl.meaning]
23391 A declarator-id shall not be qualified except for the
23392 definition of a ... nested class outside of its class
23393 ... [or] the definition or explicit instantiation of a
23394 class member of a namespace outside of its namespace. */
23395 if (scope == nested_name_specifier)
23397 permerror (nested_name_specifier_token_start->location,
23398 "extra qualification not allowed");
23399 nested_name_specifier = NULL_TREE;
23400 num_templates = 0;
23403 /* An explicit-specialization must be preceded by "template <>". If
23404 it is not, try to recover gracefully. */
23405 if (at_namespace_scope_p ()
23406 && parser->num_template_parameter_lists == 0
23407 && !processing_template_parmlist
23408 && template_id_p)
23410 /* Build a location of this form:
23411 struct typename <ARGS>
23412 ^~~~~~~~~~~~~~~~~~~~~~
23413 with caret==start at the start token, and
23414 finishing at the end of the type. */
23415 location_t reported_loc
23416 = make_location (class_head_start_location,
23417 class_head_start_location,
23418 get_finish (type_start_token->location));
23419 rich_location richloc (line_table, reported_loc);
23420 richloc.add_fixit_insert_before (class_head_start_location,
23421 "template <> ");
23422 error_at (&richloc,
23423 "an explicit specialization must be preceded by"
23424 " %<template <>%>");
23425 invalid_explicit_specialization_p = true;
23426 /* Take the same action that would have been taken by
23427 cp_parser_explicit_specialization. */
23428 ++parser->num_template_parameter_lists;
23429 begin_specialization ();
23431 /* There must be no "return" statements between this point and the
23432 end of this function; set "type "to the correct return value and
23433 use "goto done;" to return. */
23434 /* Make sure that the right number of template parameters were
23435 present. */
23436 if (!cp_parser_check_template_parameters (parser, num_templates,
23437 template_id_p,
23438 type_start_token->location,
23439 /*declarator=*/NULL))
23441 /* If something went wrong, there is no point in even trying to
23442 process the class-definition. */
23443 type = NULL_TREE;
23444 goto done;
23447 /* Look up the type. */
23448 if (template_id_p)
23450 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23451 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23452 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23454 error_at (type_start_token->location,
23455 "function template %qD redeclared as a class template", id);
23456 type = error_mark_node;
23458 else
23460 type = TREE_TYPE (id);
23461 type = maybe_process_partial_specialization (type);
23463 /* Check the scope while we still know whether or not we had a
23464 nested-name-specifier. */
23465 if (type != error_mark_node)
23466 check_unqualified_spec_or_inst (type, type_start_token->location);
23468 if (nested_name_specifier)
23469 pushed_scope = push_scope (nested_name_specifier);
23471 else if (nested_name_specifier)
23473 tree class_type;
23475 /* Given:
23477 template <typename T> struct S { struct T };
23478 template <typename T> struct S<T>::T { };
23480 we will get a TYPENAME_TYPE when processing the definition of
23481 `S::T'. We need to resolve it to the actual type before we
23482 try to define it. */
23483 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23485 class_type = resolve_typename_type (TREE_TYPE (type),
23486 /*only_current_p=*/false);
23487 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23488 type = TYPE_NAME (class_type);
23489 else
23491 cp_parser_error (parser, "could not resolve typename type");
23492 type = error_mark_node;
23496 if (maybe_process_partial_specialization (TREE_TYPE (type))
23497 == error_mark_node)
23499 type = NULL_TREE;
23500 goto done;
23503 class_type = current_class_type;
23504 /* Enter the scope indicated by the nested-name-specifier. */
23505 pushed_scope = push_scope (nested_name_specifier);
23506 /* Get the canonical version of this type. */
23507 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23508 /* Call push_template_decl if it seems like we should be defining a
23509 template either from the template headers or the type we're
23510 defining, so that we diagnose both extra and missing headers. */
23511 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23512 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23513 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23515 type = push_template_decl (type);
23516 if (type == error_mark_node)
23518 type = NULL_TREE;
23519 goto done;
23523 type = TREE_TYPE (type);
23524 *nested_name_specifier_p = true;
23526 else /* The name is not a nested name. */
23528 /* If the class was unnamed, create a dummy name. */
23529 if (!id)
23530 id = make_anon_name ();
23531 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23532 ? ts_within_enclosing_non_class
23533 : ts_current);
23534 type = xref_tag (class_key, id, tag_scope,
23535 parser->num_template_parameter_lists);
23538 /* Indicate whether this class was declared as a `class' or as a
23539 `struct'. */
23540 if (TREE_CODE (type) == RECORD_TYPE)
23541 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23542 cp_parser_check_class_key (class_key, type);
23544 /* If this type was already complete, and we see another definition,
23545 that's an error. */
23546 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23548 error_at (type_start_token->location, "redefinition of %q#T",
23549 type);
23550 inform (location_of (type), "previous definition of %q#T",
23551 type);
23552 type = NULL_TREE;
23553 goto done;
23555 else if (type == error_mark_node)
23556 type = NULL_TREE;
23558 if (type)
23560 /* Apply attributes now, before any use of the class as a template
23561 argument in its base list. */
23562 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23563 fixup_attribute_variants (type);
23566 /* We will have entered the scope containing the class; the names of
23567 base classes should be looked up in that context. For example:
23569 struct A { struct B {}; struct C; };
23570 struct A::C : B {};
23572 is valid. */
23574 /* Get the list of base-classes, if there is one. */
23575 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23577 /* PR59482: enter the class scope so that base-specifiers are looked
23578 up correctly. */
23579 if (type)
23580 pushclass (type);
23581 bases = cp_parser_base_clause (parser);
23582 /* PR59482: get out of the previously pushed class scope so that the
23583 subsequent pops pop the right thing. */
23584 if (type)
23585 popclass ();
23587 else
23588 bases = NULL_TREE;
23590 /* If we're really defining a class, process the base classes.
23591 If they're invalid, fail. */
23592 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23593 xref_basetypes (type, bases);
23595 done:
23596 /* Leave the scope given by the nested-name-specifier. We will
23597 enter the class scope itself while processing the members. */
23598 if (pushed_scope)
23599 pop_scope (pushed_scope);
23601 if (invalid_explicit_specialization_p)
23603 end_specialization ();
23604 --parser->num_template_parameter_lists;
23607 if (type)
23608 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23609 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23610 CLASSTYPE_FINAL (type) = 1;
23611 out:
23612 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23613 return type;
23616 /* Parse a class-key.
23618 class-key:
23619 class
23620 struct
23621 union
23623 Returns the kind of class-key specified, or none_type to indicate
23624 error. */
23626 static enum tag_types
23627 cp_parser_class_key (cp_parser* parser)
23629 cp_token *token;
23630 enum tag_types tag_type;
23632 /* Look for the class-key. */
23633 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23634 if (!token)
23635 return none_type;
23637 /* Check to see if the TOKEN is a class-key. */
23638 tag_type = cp_parser_token_is_class_key (token);
23639 if (!tag_type)
23640 cp_parser_error (parser, "expected class-key");
23641 return tag_type;
23644 /* Parse a type-parameter-key.
23646 type-parameter-key:
23647 class
23648 typename
23651 static void
23652 cp_parser_type_parameter_key (cp_parser* parser)
23654 /* Look for the type-parameter-key. */
23655 enum tag_types tag_type = none_type;
23656 cp_token *token = cp_lexer_peek_token (parser->lexer);
23657 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23659 cp_lexer_consume_token (parser->lexer);
23660 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23661 /* typename is not allowed in a template template parameter
23662 by the standard until C++17. */
23663 pedwarn (token->location, OPT_Wpedantic,
23664 "ISO C++ forbids typename key in template template parameter;"
23665 " use -std=c++17 or -std=gnu++17");
23667 else
23668 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23670 return;
23673 /* Parse an (optional) member-specification.
23675 member-specification:
23676 member-declaration member-specification [opt]
23677 access-specifier : member-specification [opt] */
23679 static void
23680 cp_parser_member_specification_opt (cp_parser* parser)
23682 while (true)
23684 cp_token *token;
23685 enum rid keyword;
23687 /* Peek at the next token. */
23688 token = cp_lexer_peek_token (parser->lexer);
23689 /* If it's a `}', or EOF then we've seen all the members. */
23690 if (token->type == CPP_CLOSE_BRACE
23691 || token->type == CPP_EOF
23692 || token->type == CPP_PRAGMA_EOL)
23693 break;
23695 /* See if this token is a keyword. */
23696 keyword = token->keyword;
23697 switch (keyword)
23699 case RID_PUBLIC:
23700 case RID_PROTECTED:
23701 case RID_PRIVATE:
23702 /* Consume the access-specifier. */
23703 cp_lexer_consume_token (parser->lexer);
23704 /* Remember which access-specifier is active. */
23705 current_access_specifier = token->u.value;
23706 /* Look for the `:'. */
23707 cp_parser_require (parser, CPP_COLON, RT_COLON);
23708 break;
23710 default:
23711 /* Accept #pragmas at class scope. */
23712 if (token->type == CPP_PRAGMA)
23714 cp_parser_pragma (parser, pragma_member, NULL);
23715 break;
23718 /* Otherwise, the next construction must be a
23719 member-declaration. */
23720 cp_parser_member_declaration (parser);
23725 /* Parse a member-declaration.
23727 member-declaration:
23728 decl-specifier-seq [opt] member-declarator-list [opt] ;
23729 function-definition ; [opt]
23730 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23731 using-declaration
23732 template-declaration
23733 alias-declaration
23735 member-declarator-list:
23736 member-declarator
23737 member-declarator-list , member-declarator
23739 member-declarator:
23740 declarator pure-specifier [opt]
23741 declarator constant-initializer [opt]
23742 identifier [opt] : constant-expression
23744 GNU Extensions:
23746 member-declaration:
23747 __extension__ member-declaration
23749 member-declarator:
23750 declarator attributes [opt] pure-specifier [opt]
23751 declarator attributes [opt] constant-initializer [opt]
23752 identifier [opt] attributes [opt] : constant-expression
23754 C++0x Extensions:
23756 member-declaration:
23757 static_assert-declaration */
23759 static void
23760 cp_parser_member_declaration (cp_parser* parser)
23762 cp_decl_specifier_seq decl_specifiers;
23763 tree prefix_attributes;
23764 tree decl;
23765 int declares_class_or_enum;
23766 bool friend_p;
23767 cp_token *token = NULL;
23768 cp_token *decl_spec_token_start = NULL;
23769 cp_token *initializer_token_start = NULL;
23770 int saved_pedantic;
23771 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23773 /* Check for the `__extension__' keyword. */
23774 if (cp_parser_extension_opt (parser, &saved_pedantic))
23776 /* Recurse. */
23777 cp_parser_member_declaration (parser);
23778 /* Restore the old value of the PEDANTIC flag. */
23779 pedantic = saved_pedantic;
23781 return;
23784 /* Check for a template-declaration. */
23785 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23787 /* An explicit specialization here is an error condition, and we
23788 expect the specialization handler to detect and report this. */
23789 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23790 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23791 cp_parser_explicit_specialization (parser);
23792 else
23793 cp_parser_template_declaration (parser, /*member_p=*/true);
23795 return;
23797 /* Check for a template introduction. */
23798 else if (cp_parser_template_declaration_after_export (parser, true))
23799 return;
23801 /* Check for a using-declaration. */
23802 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23804 if (cxx_dialect < cxx11)
23806 /* Parse the using-declaration. */
23807 cp_parser_using_declaration (parser,
23808 /*access_declaration_p=*/false);
23809 return;
23811 else
23813 tree decl;
23814 bool alias_decl_expected;
23815 cp_parser_parse_tentatively (parser);
23816 decl = cp_parser_alias_declaration (parser);
23817 /* Note that if we actually see the '=' token after the
23818 identifier, cp_parser_alias_declaration commits the
23819 tentative parse. In that case, we really expect an
23820 alias-declaration. Otherwise, we expect a using
23821 declaration. */
23822 alias_decl_expected =
23823 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23824 cp_parser_parse_definitely (parser);
23826 if (alias_decl_expected)
23827 finish_member_declaration (decl);
23828 else
23829 cp_parser_using_declaration (parser,
23830 /*access_declaration_p=*/false);
23831 return;
23835 /* Check for @defs. */
23836 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23838 tree ivar, member;
23839 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23840 ivar = ivar_chains;
23841 while (ivar)
23843 member = ivar;
23844 ivar = TREE_CHAIN (member);
23845 TREE_CHAIN (member) = NULL_TREE;
23846 finish_member_declaration (member);
23848 return;
23851 /* If the next token is `static_assert' we have a static assertion. */
23852 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23854 cp_parser_static_assert (parser, /*member_p=*/true);
23855 return;
23858 parser->colon_corrects_to_scope_p = false;
23860 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23861 goto out;
23863 /* Parse the decl-specifier-seq. */
23864 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23865 cp_parser_decl_specifier_seq (parser,
23866 CP_PARSER_FLAGS_OPTIONAL,
23867 &decl_specifiers,
23868 &declares_class_or_enum);
23869 /* Check for an invalid type-name. */
23870 if (!decl_specifiers.any_type_specifiers_p
23871 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23872 goto out;
23873 /* If there is no declarator, then the decl-specifier-seq should
23874 specify a type. */
23875 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23877 /* If there was no decl-specifier-seq, and the next token is a
23878 `;', then we have something like:
23880 struct S { ; };
23882 [class.mem]
23884 Each member-declaration shall declare at least one member
23885 name of the class. */
23886 if (!decl_specifiers.any_specifiers_p)
23888 cp_token *token = cp_lexer_peek_token (parser->lexer);
23889 if (!in_system_header_at (token->location))
23891 gcc_rich_location richloc (token->location);
23892 richloc.add_fixit_remove ();
23893 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23896 else
23898 tree type;
23900 /* See if this declaration is a friend. */
23901 friend_p = cp_parser_friend_p (&decl_specifiers);
23902 /* If there were decl-specifiers, check to see if there was
23903 a class-declaration. */
23904 type = check_tag_decl (&decl_specifiers,
23905 /*explicit_type_instantiation_p=*/false);
23906 /* Nested classes have already been added to the class, but
23907 a `friend' needs to be explicitly registered. */
23908 if (friend_p)
23910 /* If the `friend' keyword was present, the friend must
23911 be introduced with a class-key. */
23912 if (!declares_class_or_enum && cxx_dialect < cxx11)
23913 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23914 "in C++03 a class-key must be used "
23915 "when declaring a friend");
23916 /* In this case:
23918 template <typename T> struct A {
23919 friend struct A<T>::B;
23922 A<T>::B will be represented by a TYPENAME_TYPE, and
23923 therefore not recognized by check_tag_decl. */
23924 if (!type)
23926 type = decl_specifiers.type;
23927 if (type && TREE_CODE (type) == TYPE_DECL)
23928 type = TREE_TYPE (type);
23930 if (!type || !TYPE_P (type))
23931 error_at (decl_spec_token_start->location,
23932 "friend declaration does not name a class or "
23933 "function");
23934 else
23935 make_friend_class (current_class_type, type,
23936 /*complain=*/true);
23938 /* If there is no TYPE, an error message will already have
23939 been issued. */
23940 else if (!type || type == error_mark_node)
23942 /* An anonymous aggregate has to be handled specially; such
23943 a declaration really declares a data member (with a
23944 particular type), as opposed to a nested class. */
23945 else if (ANON_AGGR_TYPE_P (type))
23947 /* C++11 9.5/6. */
23948 if (decl_specifiers.storage_class != sc_none)
23949 error_at (decl_spec_token_start->location,
23950 "a storage class on an anonymous aggregate "
23951 "in class scope is not allowed");
23953 /* Remove constructors and such from TYPE, now that we
23954 know it is an anonymous aggregate. */
23955 fixup_anonymous_aggr (type);
23956 /* And make the corresponding data member. */
23957 decl = build_decl (decl_spec_token_start->location,
23958 FIELD_DECL, NULL_TREE, type);
23959 /* Add it to the class. */
23960 finish_member_declaration (decl);
23962 else
23963 cp_parser_check_access_in_redeclaration
23964 (TYPE_NAME (type),
23965 decl_spec_token_start->location);
23968 else
23970 bool assume_semicolon = false;
23972 /* Clear attributes from the decl_specifiers but keep them
23973 around as prefix attributes that apply them to the entity
23974 being declared. */
23975 prefix_attributes = decl_specifiers.attributes;
23976 decl_specifiers.attributes = NULL_TREE;
23978 /* See if these declarations will be friends. */
23979 friend_p = cp_parser_friend_p (&decl_specifiers);
23981 /* Keep going until we hit the `;' at the end of the
23982 declaration. */
23983 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23985 tree attributes = NULL_TREE;
23986 tree first_attribute;
23987 tree initializer;
23988 bool named_bitfld = false;
23990 /* Peek at the next token. */
23991 token = cp_lexer_peek_token (parser->lexer);
23993 /* The following code wants to know early if it is a bit-field
23994 or some other declaration. Attributes can appear before
23995 the `:' token. Skip over them without consuming any tokens
23996 to peek if they are followed by `:'. */
23997 if (cp_next_tokens_can_be_attribute_p (parser)
23998 || (token->type == CPP_NAME
23999 && cp_nth_tokens_can_be_attribute_p (parser, 2)
24000 && (named_bitfld = true)))
24002 size_t n
24003 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
24004 token = cp_lexer_peek_nth_token (parser->lexer, n);
24007 /* Check for a bitfield declaration. */
24008 if (token->type == CPP_COLON
24009 || (token->type == CPP_NAME
24010 && token == cp_lexer_peek_token (parser->lexer)
24011 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
24012 && (named_bitfld = true)))
24014 tree identifier;
24015 tree width;
24016 tree late_attributes = NULL_TREE;
24017 location_t id_location
24018 = cp_lexer_peek_token (parser->lexer)->location;
24020 if (named_bitfld)
24021 identifier = cp_parser_identifier (parser);
24022 else
24023 identifier = NULL_TREE;
24025 /* Look for attributes that apply to the bitfield. */
24026 attributes = cp_parser_attributes_opt (parser);
24028 /* Consume the `:' token. */
24029 cp_lexer_consume_token (parser->lexer);
24031 /* Get the width of the bitfield. */
24032 width = cp_parser_constant_expression (parser, false, NULL,
24033 cxx_dialect >= cxx11);
24035 /* In C++2A and as extension for C++11 and above we allow
24036 default member initializers for bit-fields. */
24037 initializer = NULL_TREE;
24038 if (cxx_dialect >= cxx11
24039 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24040 || cp_lexer_next_token_is (parser->lexer,
24041 CPP_OPEN_BRACE)))
24043 location_t loc
24044 = cp_lexer_peek_token (parser->lexer)->location;
24045 if (cxx_dialect < cxx2a
24046 && !in_system_header_at (loc)
24047 && identifier != NULL_TREE)
24048 pedwarn (loc, 0,
24049 "default member initializers for bit-fields "
24050 "only available with -std=c++2a or "
24051 "-std=gnu++2a");
24053 initializer = cp_parser_save_nsdmi (parser);
24054 if (identifier == NULL_TREE)
24056 error_at (loc, "default member initializer for "
24057 "unnamed bit-field");
24058 initializer = NULL_TREE;
24061 else
24063 /* Look for attributes that apply to the bitfield after
24064 the `:' token and width. This is where GCC used to
24065 parse attributes in the past, pedwarn if there is
24066 a std attribute. */
24067 if (cp_next_tokens_can_be_std_attribute_p (parser))
24068 pedwarn (input_location, OPT_Wpedantic,
24069 "ISO C++ allows bit-field attributes only "
24070 "before the %<:%> token");
24072 late_attributes = cp_parser_attributes_opt (parser);
24075 attributes = attr_chainon (attributes, late_attributes);
24077 /* Remember which attributes are prefix attributes and
24078 which are not. */
24079 first_attribute = attributes;
24080 /* Combine the attributes. */
24081 attributes = attr_chainon (prefix_attributes, attributes);
24083 /* Create the bitfield declaration. */
24084 decl = grokbitfield (identifier
24085 ? make_id_declarator (NULL_TREE,
24086 identifier,
24087 sfk_none,
24088 id_location)
24089 : NULL,
24090 &decl_specifiers,
24091 width, initializer,
24092 attributes);
24094 else
24096 cp_declarator *declarator;
24097 tree asm_specification;
24098 int ctor_dtor_or_conv_p;
24100 /* Parse the declarator. */
24101 declarator
24102 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24103 &ctor_dtor_or_conv_p,
24104 /*parenthesized_p=*/NULL,
24105 /*member_p=*/true,
24106 friend_p);
24108 /* If something went wrong parsing the declarator, make sure
24109 that we at least consume some tokens. */
24110 if (declarator == cp_error_declarator)
24112 /* Skip to the end of the statement. */
24113 cp_parser_skip_to_end_of_statement (parser);
24114 /* If the next token is not a semicolon, that is
24115 probably because we just skipped over the body of
24116 a function. So, we consume a semicolon if
24117 present, but do not issue an error message if it
24118 is not present. */
24119 if (cp_lexer_next_token_is (parser->lexer,
24120 CPP_SEMICOLON))
24121 cp_lexer_consume_token (parser->lexer);
24122 goto out;
24125 if (declares_class_or_enum & 2)
24126 cp_parser_check_for_definition_in_return_type
24127 (declarator, decl_specifiers.type,
24128 decl_specifiers.locations[ds_type_spec]);
24130 /* Look for an asm-specification. */
24131 asm_specification = cp_parser_asm_specification_opt (parser);
24132 /* Look for attributes that apply to the declaration. */
24133 attributes = cp_parser_attributes_opt (parser);
24134 /* Remember which attributes are prefix attributes and
24135 which are not. */
24136 first_attribute = attributes;
24137 /* Combine the attributes. */
24138 attributes = attr_chainon (prefix_attributes, attributes);
24140 /* If it's an `=', then we have a constant-initializer or a
24141 pure-specifier. It is not correct to parse the
24142 initializer before registering the member declaration
24143 since the member declaration should be in scope while
24144 its initializer is processed. However, the rest of the
24145 front end does not yet provide an interface that allows
24146 us to handle this correctly. */
24147 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24149 /* In [class.mem]:
24151 A pure-specifier shall be used only in the declaration of
24152 a virtual function.
24154 A member-declarator can contain a constant-initializer
24155 only if it declares a static member of integral or
24156 enumeration type.
24158 Therefore, if the DECLARATOR is for a function, we look
24159 for a pure-specifier; otherwise, we look for a
24160 constant-initializer. When we call `grokfield', it will
24161 perform more stringent semantics checks. */
24162 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24163 if (function_declarator_p (declarator)
24164 || (decl_specifiers.type
24165 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24166 && declarator->kind == cdk_id
24167 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24168 == FUNCTION_TYPE)))
24169 initializer = cp_parser_pure_specifier (parser);
24170 else if (decl_specifiers.storage_class != sc_static)
24171 initializer = cp_parser_save_nsdmi (parser);
24172 else if (cxx_dialect >= cxx11)
24174 bool nonconst;
24175 /* Don't require a constant rvalue in C++11, since we
24176 might want a reference constant. We'll enforce
24177 constancy later. */
24178 cp_lexer_consume_token (parser->lexer);
24179 /* Parse the initializer. */
24180 initializer = cp_parser_initializer_clause (parser,
24181 &nonconst);
24183 else
24184 /* Parse the initializer. */
24185 initializer = cp_parser_constant_initializer (parser);
24187 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24188 && !function_declarator_p (declarator))
24190 bool x;
24191 if (decl_specifiers.storage_class != sc_static)
24192 initializer = cp_parser_save_nsdmi (parser);
24193 else
24194 initializer = cp_parser_initializer (parser, &x, &x);
24196 /* Otherwise, there is no initializer. */
24197 else
24198 initializer = NULL_TREE;
24200 /* See if we are probably looking at a function
24201 definition. We are certainly not looking at a
24202 member-declarator. Calling `grokfield' has
24203 side-effects, so we must not do it unless we are sure
24204 that we are looking at a member-declarator. */
24205 if (cp_parser_token_starts_function_definition_p
24206 (cp_lexer_peek_token (parser->lexer)))
24208 /* The grammar does not allow a pure-specifier to be
24209 used when a member function is defined. (It is
24210 possible that this fact is an oversight in the
24211 standard, since a pure function may be defined
24212 outside of the class-specifier. */
24213 if (initializer && initializer_token_start)
24214 error_at (initializer_token_start->location,
24215 "pure-specifier on function-definition");
24216 decl = cp_parser_save_member_function_body (parser,
24217 &decl_specifiers,
24218 declarator,
24219 attributes);
24220 if (parser->fully_implicit_function_template_p)
24221 decl = finish_fully_implicit_template (parser, decl);
24222 /* If the member was not a friend, declare it here. */
24223 if (!friend_p)
24224 finish_member_declaration (decl);
24225 /* Peek at the next token. */
24226 token = cp_lexer_peek_token (parser->lexer);
24227 /* If the next token is a semicolon, consume it. */
24228 if (token->type == CPP_SEMICOLON)
24230 location_t semicolon_loc
24231 = cp_lexer_consume_token (parser->lexer)->location;
24232 gcc_rich_location richloc (semicolon_loc);
24233 richloc.add_fixit_remove ();
24234 warning_at (&richloc, OPT_Wextra_semi,
24235 "extra %<;%> after in-class "
24236 "function definition");
24238 goto out;
24240 else
24241 if (declarator->kind == cdk_function)
24242 declarator->id_loc = token->location;
24243 /* Create the declaration. */
24244 decl = grokfield (declarator, &decl_specifiers,
24245 initializer, /*init_const_expr_p=*/true,
24246 asm_specification, attributes);
24247 if (parser->fully_implicit_function_template_p)
24249 if (friend_p)
24250 finish_fully_implicit_template (parser, 0);
24251 else
24252 decl = finish_fully_implicit_template (parser, decl);
24256 cp_finalize_omp_declare_simd (parser, decl);
24257 cp_finalize_oacc_routine (parser, decl, false);
24259 /* Reset PREFIX_ATTRIBUTES. */
24260 if (attributes != error_mark_node)
24262 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24263 attributes = TREE_CHAIN (attributes);
24264 if (attributes)
24265 TREE_CHAIN (attributes) = NULL_TREE;
24268 /* If there is any qualification still in effect, clear it
24269 now; we will be starting fresh with the next declarator. */
24270 parser->scope = NULL_TREE;
24271 parser->qualifying_scope = NULL_TREE;
24272 parser->object_scope = NULL_TREE;
24273 /* If it's a `,', then there are more declarators. */
24274 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24276 cp_lexer_consume_token (parser->lexer);
24277 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24279 cp_token *token = cp_lexer_previous_token (parser->lexer);
24280 gcc_rich_location richloc (token->location);
24281 richloc.add_fixit_remove ();
24282 error_at (&richloc, "stray %<,%> at end of "
24283 "member declaration");
24286 /* If the next token isn't a `;', then we have a parse error. */
24287 else if (cp_lexer_next_token_is_not (parser->lexer,
24288 CPP_SEMICOLON))
24290 /* The next token might be a ways away from where the
24291 actual semicolon is missing. Find the previous token
24292 and use that for our error position. */
24293 cp_token *token = cp_lexer_previous_token (parser->lexer);
24294 gcc_rich_location richloc (token->location);
24295 richloc.add_fixit_insert_after (";");
24296 error_at (&richloc, "expected %<;%> at end of "
24297 "member declaration");
24299 /* Assume that the user meant to provide a semicolon. If
24300 we were to cp_parser_skip_to_end_of_statement, we might
24301 skip to a semicolon inside a member function definition
24302 and issue nonsensical error messages. */
24303 assume_semicolon = true;
24306 if (decl)
24308 /* Add DECL to the list of members. */
24309 if (!friend_p
24310 /* Explicitly include, eg, NSDMIs, for better error
24311 recovery (c++/58650). */
24312 || !DECL_DECLARES_FUNCTION_P (decl))
24313 finish_member_declaration (decl);
24315 if (TREE_CODE (decl) == FUNCTION_DECL)
24316 cp_parser_save_default_args (parser, decl);
24317 else if (TREE_CODE (decl) == FIELD_DECL
24318 && DECL_INITIAL (decl))
24319 /* Add DECL to the queue of NSDMI to be parsed later. */
24320 vec_safe_push (unparsed_nsdmis, decl);
24323 if (assume_semicolon)
24324 goto out;
24328 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24329 out:
24330 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24333 /* Parse a pure-specifier.
24335 pure-specifier:
24338 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24339 Otherwise, ERROR_MARK_NODE is returned. */
24341 static tree
24342 cp_parser_pure_specifier (cp_parser* parser)
24344 cp_token *token;
24346 /* Look for the `=' token. */
24347 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24348 return error_mark_node;
24349 /* Look for the `0' token. */
24350 token = cp_lexer_peek_token (parser->lexer);
24352 if (token->type == CPP_EOF
24353 || token->type == CPP_PRAGMA_EOL)
24354 return error_mark_node;
24356 cp_lexer_consume_token (parser->lexer);
24358 /* Accept = default or = delete in c++0x mode. */
24359 if (token->keyword == RID_DEFAULT
24360 || token->keyword == RID_DELETE)
24362 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24363 return token->u.value;
24366 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24367 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24369 cp_parser_error (parser,
24370 "invalid pure specifier (only %<= 0%> is allowed)");
24371 cp_parser_skip_to_end_of_statement (parser);
24372 return error_mark_node;
24374 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24376 error_at (token->location, "templates may not be %<virtual%>");
24377 return error_mark_node;
24380 return integer_zero_node;
24383 /* Parse a constant-initializer.
24385 constant-initializer:
24386 = constant-expression
24388 Returns a representation of the constant-expression. */
24390 static tree
24391 cp_parser_constant_initializer (cp_parser* parser)
24393 /* Look for the `=' token. */
24394 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24395 return error_mark_node;
24397 /* It is invalid to write:
24399 struct S { static const int i = { 7 }; };
24402 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24404 cp_parser_error (parser,
24405 "a brace-enclosed initializer is not allowed here");
24406 /* Consume the opening brace. */
24407 matching_braces braces;
24408 braces.consume_open (parser);
24409 /* Skip the initializer. */
24410 cp_parser_skip_to_closing_brace (parser);
24411 /* Look for the trailing `}'. */
24412 braces.require_close (parser);
24414 return error_mark_node;
24417 return cp_parser_constant_expression (parser);
24420 /* Derived classes [gram.class.derived] */
24422 /* Parse a base-clause.
24424 base-clause:
24425 : base-specifier-list
24427 base-specifier-list:
24428 base-specifier ... [opt]
24429 base-specifier-list , base-specifier ... [opt]
24431 Returns a TREE_LIST representing the base-classes, in the order in
24432 which they were declared. The representation of each node is as
24433 described by cp_parser_base_specifier.
24435 In the case that no bases are specified, this function will return
24436 NULL_TREE, not ERROR_MARK_NODE. */
24438 static tree
24439 cp_parser_base_clause (cp_parser* parser)
24441 tree bases = NULL_TREE;
24443 /* Look for the `:' that begins the list. */
24444 cp_parser_require (parser, CPP_COLON, RT_COLON);
24446 /* Scan the base-specifier-list. */
24447 while (true)
24449 cp_token *token;
24450 tree base;
24451 bool pack_expansion_p = false;
24453 /* Look for the base-specifier. */
24454 base = cp_parser_base_specifier (parser);
24455 /* Look for the (optional) ellipsis. */
24456 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24458 /* Consume the `...'. */
24459 cp_lexer_consume_token (parser->lexer);
24461 pack_expansion_p = true;
24464 /* Add BASE to the front of the list. */
24465 if (base && base != error_mark_node)
24467 if (pack_expansion_p)
24468 /* Make this a pack expansion type. */
24469 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24471 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24473 TREE_CHAIN (base) = bases;
24474 bases = base;
24477 /* Peek at the next token. */
24478 token = cp_lexer_peek_token (parser->lexer);
24479 /* If it's not a comma, then the list is complete. */
24480 if (token->type != CPP_COMMA)
24481 break;
24482 /* Consume the `,'. */
24483 cp_lexer_consume_token (parser->lexer);
24486 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24487 base class had a qualified name. However, the next name that
24488 appears is certainly not qualified. */
24489 parser->scope = NULL_TREE;
24490 parser->qualifying_scope = NULL_TREE;
24491 parser->object_scope = NULL_TREE;
24493 return nreverse (bases);
24496 /* Parse a base-specifier.
24498 base-specifier:
24499 :: [opt] nested-name-specifier [opt] class-name
24500 virtual access-specifier [opt] :: [opt] nested-name-specifier
24501 [opt] class-name
24502 access-specifier virtual [opt] :: [opt] nested-name-specifier
24503 [opt] class-name
24505 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24506 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24507 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24508 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24510 static tree
24511 cp_parser_base_specifier (cp_parser* parser)
24513 cp_token *token;
24514 bool done = false;
24515 bool virtual_p = false;
24516 bool duplicate_virtual_error_issued_p = false;
24517 bool duplicate_access_error_issued_p = false;
24518 bool class_scope_p, template_p;
24519 tree access = access_default_node;
24520 tree type;
24522 /* Process the optional `virtual' and `access-specifier'. */
24523 while (!done)
24525 /* Peek at the next token. */
24526 token = cp_lexer_peek_token (parser->lexer);
24527 /* Process `virtual'. */
24528 switch (token->keyword)
24530 case RID_VIRTUAL:
24531 /* If `virtual' appears more than once, issue an error. */
24532 if (virtual_p && !duplicate_virtual_error_issued_p)
24534 cp_parser_error (parser,
24535 "%<virtual%> specified more than once in base-specifier");
24536 duplicate_virtual_error_issued_p = true;
24539 virtual_p = true;
24541 /* Consume the `virtual' token. */
24542 cp_lexer_consume_token (parser->lexer);
24544 break;
24546 case RID_PUBLIC:
24547 case RID_PROTECTED:
24548 case RID_PRIVATE:
24549 /* If more than one access specifier appears, issue an
24550 error. */
24551 if (access != access_default_node
24552 && !duplicate_access_error_issued_p)
24554 cp_parser_error (parser,
24555 "more than one access specifier in base-specifier");
24556 duplicate_access_error_issued_p = true;
24559 access = ridpointers[(int) token->keyword];
24561 /* Consume the access-specifier. */
24562 cp_lexer_consume_token (parser->lexer);
24564 break;
24566 default:
24567 done = true;
24568 break;
24571 /* It is not uncommon to see programs mechanically, erroneously, use
24572 the 'typename' keyword to denote (dependent) qualified types
24573 as base classes. */
24574 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24576 token = cp_lexer_peek_token (parser->lexer);
24577 if (!processing_template_decl)
24578 error_at (token->location,
24579 "keyword %<typename%> not allowed outside of templates");
24580 else
24581 error_at (token->location,
24582 "keyword %<typename%> not allowed in this context "
24583 "(the base class is implicitly a type)");
24584 cp_lexer_consume_token (parser->lexer);
24587 /* Look for the optional `::' operator. */
24588 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24589 /* Look for the nested-name-specifier. The simplest way to
24590 implement:
24592 [temp.res]
24594 The keyword `typename' is not permitted in a base-specifier or
24595 mem-initializer; in these contexts a qualified name that
24596 depends on a template-parameter is implicitly assumed to be a
24597 type name.
24599 is to pretend that we have seen the `typename' keyword at this
24600 point. */
24601 cp_parser_nested_name_specifier_opt (parser,
24602 /*typename_keyword_p=*/true,
24603 /*check_dependency_p=*/true,
24604 /*type_p=*/true,
24605 /*is_declaration=*/true);
24606 /* If the base class is given by a qualified name, assume that names
24607 we see are type names or templates, as appropriate. */
24608 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24609 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24611 if (!parser->scope
24612 && cp_lexer_next_token_is_decltype (parser->lexer))
24613 /* DR 950 allows decltype as a base-specifier. */
24614 type = cp_parser_decltype (parser);
24615 else
24617 /* Otherwise, look for the class-name. */
24618 type = cp_parser_class_name (parser,
24619 class_scope_p,
24620 template_p,
24621 typename_type,
24622 /*check_dependency_p=*/true,
24623 /*class_head_p=*/false,
24624 /*is_declaration=*/true);
24625 type = TREE_TYPE (type);
24628 if (type == error_mark_node)
24629 return error_mark_node;
24631 return finish_base_specifier (type, access, virtual_p);
24634 /* Exception handling [gram.exception] */
24636 /* Parse an (optional) noexcept-specification.
24638 noexcept-specification:
24639 noexcept ( constant-expression ) [opt]
24641 If no noexcept-specification is present, returns NULL_TREE.
24642 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24643 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24644 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24645 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24646 in which case a boolean condition is returned instead. */
24648 static tree
24649 cp_parser_noexcept_specification_opt (cp_parser* parser,
24650 bool require_constexpr,
24651 bool* consumed_expr,
24652 bool return_cond)
24654 cp_token *token;
24655 const char *saved_message;
24657 /* Peek at the next token. */
24658 token = cp_lexer_peek_token (parser->lexer);
24660 /* Is it a noexcept-specification? */
24661 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24663 tree expr;
24664 cp_lexer_consume_token (parser->lexer);
24666 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24668 matching_parens parens;
24669 parens.consume_open (parser);
24671 tree save_ccp = current_class_ptr;
24672 tree save_ccr = current_class_ref;
24674 if (current_class_type)
24675 inject_this_parameter (current_class_type, TYPE_UNQUALIFIED);
24677 if (require_constexpr)
24679 /* Types may not be defined in an exception-specification. */
24680 saved_message = parser->type_definition_forbidden_message;
24681 parser->type_definition_forbidden_message
24682 = G_("types may not be defined in an exception-specification");
24684 expr = cp_parser_constant_expression (parser);
24686 /* Restore the saved message. */
24687 parser->type_definition_forbidden_message = saved_message;
24689 else
24691 expr = cp_parser_expression (parser);
24692 *consumed_expr = true;
24695 parens.require_close (parser);
24697 current_class_ptr = save_ccp;
24698 current_class_ref = save_ccr;
24700 else
24702 expr = boolean_true_node;
24703 if (!require_constexpr)
24704 *consumed_expr = false;
24707 /* We cannot build a noexcept-spec right away because this will check
24708 that expr is a constexpr. */
24709 if (!return_cond)
24710 return build_noexcept_spec (expr, tf_warning_or_error);
24711 else
24712 return expr;
24714 else
24715 return NULL_TREE;
24718 /* Parse an (optional) exception-specification.
24720 exception-specification:
24721 throw ( type-id-list [opt] )
24723 Returns a TREE_LIST representing the exception-specification. The
24724 TREE_VALUE of each node is a type. */
24726 static tree
24727 cp_parser_exception_specification_opt (cp_parser* parser)
24729 cp_token *token;
24730 tree type_id_list;
24731 const char *saved_message;
24733 /* Peek at the next token. */
24734 token = cp_lexer_peek_token (parser->lexer);
24736 /* Is it a noexcept-specification? */
24737 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24738 false);
24739 if (type_id_list != NULL_TREE)
24740 return type_id_list;
24742 /* If it's not `throw', then there's no exception-specification. */
24743 if (!cp_parser_is_keyword (token, RID_THROW))
24744 return NULL_TREE;
24746 location_t loc = token->location;
24748 /* Consume the `throw'. */
24749 cp_lexer_consume_token (parser->lexer);
24751 /* Look for the `('. */
24752 matching_parens parens;
24753 parens.require_open (parser);
24755 /* Peek at the next token. */
24756 token = cp_lexer_peek_token (parser->lexer);
24757 /* If it's not a `)', then there is a type-id-list. */
24758 if (token->type != CPP_CLOSE_PAREN)
24760 /* Types may not be defined in an exception-specification. */
24761 saved_message = parser->type_definition_forbidden_message;
24762 parser->type_definition_forbidden_message
24763 = G_("types may not be defined in an exception-specification");
24764 /* Parse the type-id-list. */
24765 type_id_list = cp_parser_type_id_list (parser);
24766 /* Restore the saved message. */
24767 parser->type_definition_forbidden_message = saved_message;
24769 if (cxx_dialect >= cxx17)
24771 error_at (loc, "ISO C++17 does not allow dynamic exception "
24772 "specifications");
24773 type_id_list = NULL_TREE;
24775 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24776 warning_at (loc, OPT_Wdeprecated,
24777 "dynamic exception specifications are deprecated in "
24778 "C++11");
24780 /* In C++17, throw() is equivalent to noexcept (true). throw()
24781 is deprecated in C++11 and above as well, but is still widely used,
24782 so don't warn about it yet. */
24783 else if (cxx_dialect >= cxx17)
24784 type_id_list = noexcept_true_spec;
24785 else
24786 type_id_list = empty_except_spec;
24788 /* Look for the `)'. */
24789 parens.require_close (parser);
24791 return type_id_list;
24794 /* Parse an (optional) type-id-list.
24796 type-id-list:
24797 type-id ... [opt]
24798 type-id-list , type-id ... [opt]
24800 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24801 in the order that the types were presented. */
24803 static tree
24804 cp_parser_type_id_list (cp_parser* parser)
24806 tree types = NULL_TREE;
24808 while (true)
24810 cp_token *token;
24811 tree type;
24813 token = cp_lexer_peek_token (parser->lexer);
24815 /* Get the next type-id. */
24816 type = cp_parser_type_id (parser);
24817 /* Check for invalid 'auto'. */
24818 if (flag_concepts && type_uses_auto (type))
24820 error_at (token->location,
24821 "invalid use of %<auto%> in exception-specification");
24822 type = error_mark_node;
24824 /* Parse the optional ellipsis. */
24825 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24827 /* Consume the `...'. */
24828 cp_lexer_consume_token (parser->lexer);
24830 /* Turn the type into a pack expansion expression. */
24831 type = make_pack_expansion (type);
24833 /* Add it to the list. */
24834 types = add_exception_specifier (types, type, /*complain=*/1);
24835 /* Peek at the next token. */
24836 token = cp_lexer_peek_token (parser->lexer);
24837 /* If it is not a `,', we are done. */
24838 if (token->type != CPP_COMMA)
24839 break;
24840 /* Consume the `,'. */
24841 cp_lexer_consume_token (parser->lexer);
24844 return nreverse (types);
24847 /* Parse a try-block.
24849 try-block:
24850 try compound-statement handler-seq */
24852 static tree
24853 cp_parser_try_block (cp_parser* parser)
24855 tree try_block;
24857 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24858 if (parser->in_function_body
24859 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24860 error ("%<try%> in %<constexpr%> function");
24862 try_block = begin_try_block ();
24863 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24864 finish_try_block (try_block);
24865 cp_parser_handler_seq (parser);
24866 finish_handler_sequence (try_block);
24868 return try_block;
24871 /* Parse a function-try-block.
24873 function-try-block:
24874 try ctor-initializer [opt] function-body handler-seq */
24876 static void
24877 cp_parser_function_try_block (cp_parser* parser)
24879 tree compound_stmt;
24880 tree try_block;
24882 /* Look for the `try' keyword. */
24883 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24884 return;
24885 /* Let the rest of the front end know where we are. */
24886 try_block = begin_function_try_block (&compound_stmt);
24887 /* Parse the function-body. */
24888 cp_parser_ctor_initializer_opt_and_function_body
24889 (parser, /*in_function_try_block=*/true);
24890 /* We're done with the `try' part. */
24891 finish_function_try_block (try_block);
24892 /* Parse the handlers. */
24893 cp_parser_handler_seq (parser);
24894 /* We're done with the handlers. */
24895 finish_function_handler_sequence (try_block, compound_stmt);
24898 /* Parse a handler-seq.
24900 handler-seq:
24901 handler handler-seq [opt] */
24903 static void
24904 cp_parser_handler_seq (cp_parser* parser)
24906 while (true)
24908 cp_token *token;
24910 /* Parse the handler. */
24911 cp_parser_handler (parser);
24912 /* Peek at the next token. */
24913 token = cp_lexer_peek_token (parser->lexer);
24914 /* If it's not `catch' then there are no more handlers. */
24915 if (!cp_parser_is_keyword (token, RID_CATCH))
24916 break;
24920 /* Parse a handler.
24922 handler:
24923 catch ( exception-declaration ) compound-statement */
24925 static void
24926 cp_parser_handler (cp_parser* parser)
24928 tree handler;
24929 tree declaration;
24931 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24932 handler = begin_handler ();
24933 matching_parens parens;
24934 parens.require_open (parser);
24935 declaration = cp_parser_exception_declaration (parser);
24936 finish_handler_parms (declaration, handler);
24937 parens.require_close (parser);
24938 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24939 finish_handler (handler);
24942 /* Parse an exception-declaration.
24944 exception-declaration:
24945 type-specifier-seq declarator
24946 type-specifier-seq abstract-declarator
24947 type-specifier-seq
24950 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24951 ellipsis variant is used. */
24953 static tree
24954 cp_parser_exception_declaration (cp_parser* parser)
24956 cp_decl_specifier_seq type_specifiers;
24957 cp_declarator *declarator;
24958 const char *saved_message;
24960 /* If it's an ellipsis, it's easy to handle. */
24961 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24963 /* Consume the `...' token. */
24964 cp_lexer_consume_token (parser->lexer);
24965 return NULL_TREE;
24968 /* Types may not be defined in exception-declarations. */
24969 saved_message = parser->type_definition_forbidden_message;
24970 parser->type_definition_forbidden_message
24971 = G_("types may not be defined in exception-declarations");
24973 /* Parse the type-specifier-seq. */
24974 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24975 /*is_trailing_return=*/false,
24976 &type_specifiers);
24977 /* If it's a `)', then there is no declarator. */
24978 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24979 declarator = NULL;
24980 else
24981 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24982 /*ctor_dtor_or_conv_p=*/NULL,
24983 /*parenthesized_p=*/NULL,
24984 /*member_p=*/false,
24985 /*friend_p=*/false);
24987 /* Restore the saved message. */
24988 parser->type_definition_forbidden_message = saved_message;
24990 if (!type_specifiers.any_specifiers_p)
24991 return error_mark_node;
24993 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24996 /* Parse a throw-expression.
24998 throw-expression:
24999 throw assignment-expression [opt]
25001 Returns a THROW_EXPR representing the throw-expression. */
25003 static tree
25004 cp_parser_throw_expression (cp_parser* parser)
25006 tree expression;
25007 cp_token* token;
25009 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
25010 token = cp_lexer_peek_token (parser->lexer);
25011 /* Figure out whether or not there is an assignment-expression
25012 following the "throw" keyword. */
25013 if (token->type == CPP_COMMA
25014 || token->type == CPP_SEMICOLON
25015 || token->type == CPP_CLOSE_PAREN
25016 || token->type == CPP_CLOSE_SQUARE
25017 || token->type == CPP_CLOSE_BRACE
25018 || token->type == CPP_COLON)
25019 expression = NULL_TREE;
25020 else
25021 expression = cp_parser_assignment_expression (parser);
25023 return build_throw (expression);
25026 /* GNU Extensions */
25028 /* Parse an (optional) asm-specification.
25030 asm-specification:
25031 asm ( string-literal )
25033 If the asm-specification is present, returns a STRING_CST
25034 corresponding to the string-literal. Otherwise, returns
25035 NULL_TREE. */
25037 static tree
25038 cp_parser_asm_specification_opt (cp_parser* parser)
25040 cp_token *token;
25041 tree asm_specification;
25043 /* Peek at the next token. */
25044 token = cp_lexer_peek_token (parser->lexer);
25045 /* If the next token isn't the `asm' keyword, then there's no
25046 asm-specification. */
25047 if (!cp_parser_is_keyword (token, RID_ASM))
25048 return NULL_TREE;
25050 /* Consume the `asm' token. */
25051 cp_lexer_consume_token (parser->lexer);
25052 /* Look for the `('. */
25053 matching_parens parens;
25054 parens.require_open (parser);
25056 /* Look for the string-literal. */
25057 asm_specification = cp_parser_string_literal (parser, false, false);
25059 /* Look for the `)'. */
25060 parens.require_close (parser);
25062 return asm_specification;
25065 /* Parse an asm-operand-list.
25067 asm-operand-list:
25068 asm-operand
25069 asm-operand-list , asm-operand
25071 asm-operand:
25072 string-literal ( expression )
25073 [ string-literal ] string-literal ( expression )
25075 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25076 each node is the expression. The TREE_PURPOSE is itself a
25077 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25078 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25079 is a STRING_CST for the string literal before the parenthesis. Returns
25080 ERROR_MARK_NODE if any of the operands are invalid. */
25082 static tree
25083 cp_parser_asm_operand_list (cp_parser* parser)
25085 tree asm_operands = NULL_TREE;
25086 bool invalid_operands = false;
25088 while (true)
25090 tree string_literal;
25091 tree expression;
25092 tree name;
25094 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25096 /* Consume the `[' token. */
25097 cp_lexer_consume_token (parser->lexer);
25098 /* Read the operand name. */
25099 name = cp_parser_identifier (parser);
25100 if (name != error_mark_node)
25101 name = build_string (IDENTIFIER_LENGTH (name),
25102 IDENTIFIER_POINTER (name));
25103 /* Look for the closing `]'. */
25104 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25106 else
25107 name = NULL_TREE;
25108 /* Look for the string-literal. */
25109 string_literal = cp_parser_string_literal (parser, false, false);
25111 /* Look for the `('. */
25112 matching_parens parens;
25113 parens.require_open (parser);
25114 /* Parse the expression. */
25115 expression = cp_parser_expression (parser);
25116 /* Look for the `)'. */
25117 parens.require_close (parser);
25119 if (name == error_mark_node
25120 || string_literal == error_mark_node
25121 || expression == error_mark_node)
25122 invalid_operands = true;
25124 /* Add this operand to the list. */
25125 asm_operands = tree_cons (build_tree_list (name, string_literal),
25126 expression,
25127 asm_operands);
25128 /* If the next token is not a `,', there are no more
25129 operands. */
25130 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25131 break;
25132 /* Consume the `,'. */
25133 cp_lexer_consume_token (parser->lexer);
25136 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25139 /* Parse an asm-clobber-list.
25141 asm-clobber-list:
25142 string-literal
25143 asm-clobber-list , string-literal
25145 Returns a TREE_LIST, indicating the clobbers in the order that they
25146 appeared. The TREE_VALUE of each node is a STRING_CST. */
25148 static tree
25149 cp_parser_asm_clobber_list (cp_parser* parser)
25151 tree clobbers = NULL_TREE;
25153 while (true)
25155 tree string_literal;
25157 /* Look for the string literal. */
25158 string_literal = cp_parser_string_literal (parser, false, false);
25159 /* Add it to the list. */
25160 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25161 /* If the next token is not a `,', then the list is
25162 complete. */
25163 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25164 break;
25165 /* Consume the `,' token. */
25166 cp_lexer_consume_token (parser->lexer);
25169 return clobbers;
25172 /* Parse an asm-label-list.
25174 asm-label-list:
25175 identifier
25176 asm-label-list , identifier
25178 Returns a TREE_LIST, indicating the labels in the order that they
25179 appeared. The TREE_VALUE of each node is a label. */
25181 static tree
25182 cp_parser_asm_label_list (cp_parser* parser)
25184 tree labels = NULL_TREE;
25186 while (true)
25188 tree identifier, label, name;
25190 /* Look for the identifier. */
25191 identifier = cp_parser_identifier (parser);
25192 if (!error_operand_p (identifier))
25194 label = lookup_label (identifier);
25195 if (TREE_CODE (label) == LABEL_DECL)
25197 TREE_USED (label) = 1;
25198 check_goto (label);
25199 name = build_string (IDENTIFIER_LENGTH (identifier),
25200 IDENTIFIER_POINTER (identifier));
25201 labels = tree_cons (name, label, labels);
25204 /* If the next token is not a `,', then the list is
25205 complete. */
25206 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25207 break;
25208 /* Consume the `,' token. */
25209 cp_lexer_consume_token (parser->lexer);
25212 return nreverse (labels);
25215 /* Return TRUE iff the next tokens in the stream are possibly the
25216 beginning of a GNU extension attribute. */
25218 static bool
25219 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25221 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25224 /* Return TRUE iff the next tokens in the stream are possibly the
25225 beginning of a standard C++-11 attribute specifier. */
25227 static bool
25228 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25230 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25233 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25234 beginning of a standard C++-11 attribute specifier. */
25236 static bool
25237 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25239 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25241 return (cxx_dialect >= cxx11
25242 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25243 || (token->type == CPP_OPEN_SQUARE
25244 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25245 && token->type == CPP_OPEN_SQUARE)));
25248 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25249 beginning of a GNU extension attribute. */
25251 static bool
25252 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25254 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25256 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25259 /* Return true iff the next tokens can be the beginning of either a
25260 GNU attribute list, or a standard C++11 attribute sequence. */
25262 static bool
25263 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25265 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25266 || cp_next_tokens_can_be_std_attribute_p (parser));
25269 /* Return true iff the next Nth tokens can be the beginning of either
25270 a GNU attribute list, or a standard C++11 attribute sequence. */
25272 static bool
25273 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25275 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25276 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25279 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25280 of GNU attributes, or return NULL. */
25282 static tree
25283 cp_parser_attributes_opt (cp_parser *parser)
25285 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25286 return cp_parser_gnu_attributes_opt (parser);
25287 return cp_parser_std_attribute_spec_seq (parser);
25290 /* Parse an (optional) series of attributes.
25292 attributes:
25293 attributes attribute
25295 attribute:
25296 __attribute__ (( attribute-list [opt] ))
25298 The return value is as for cp_parser_gnu_attribute_list. */
25300 static tree
25301 cp_parser_gnu_attributes_opt (cp_parser* parser)
25303 tree attributes = NULL_TREE;
25305 temp_override<bool> cleanup
25306 (parser->auto_is_implicit_function_template_parm_p, false);
25308 while (true)
25310 cp_token *token;
25311 tree attribute_list;
25312 bool ok = true;
25314 /* Peek at the next token. */
25315 token = cp_lexer_peek_token (parser->lexer);
25316 /* If it's not `__attribute__', then we're done. */
25317 if (token->keyword != RID_ATTRIBUTE)
25318 break;
25320 /* Consume the `__attribute__' keyword. */
25321 cp_lexer_consume_token (parser->lexer);
25322 /* Look for the two `(' tokens. */
25323 matching_parens outer_parens;
25324 outer_parens.require_open (parser);
25325 matching_parens inner_parens;
25326 inner_parens.require_open (parser);
25328 /* Peek at the next token. */
25329 token = cp_lexer_peek_token (parser->lexer);
25330 if (token->type != CPP_CLOSE_PAREN)
25331 /* Parse the attribute-list. */
25332 attribute_list = cp_parser_gnu_attribute_list (parser);
25333 else
25334 /* If the next token is a `)', then there is no attribute
25335 list. */
25336 attribute_list = NULL;
25338 /* Look for the two `)' tokens. */
25339 if (!inner_parens.require_close (parser))
25340 ok = false;
25341 if (!outer_parens.require_close (parser))
25342 ok = false;
25343 if (!ok)
25344 cp_parser_skip_to_end_of_statement (parser);
25346 /* Add these new attributes to the list. */
25347 attributes = attr_chainon (attributes, attribute_list);
25350 return attributes;
25353 /* Parse a GNU attribute-list.
25355 attribute-list:
25356 attribute
25357 attribute-list , attribute
25359 attribute:
25360 identifier
25361 identifier ( identifier )
25362 identifier ( identifier , expression-list )
25363 identifier ( expression-list )
25365 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25366 to an attribute. The TREE_PURPOSE of each node is the identifier
25367 indicating which attribute is in use. The TREE_VALUE represents
25368 the arguments, if any. */
25370 static tree
25371 cp_parser_gnu_attribute_list (cp_parser* parser)
25373 tree attribute_list = NULL_TREE;
25374 bool save_translate_strings_p = parser->translate_strings_p;
25376 parser->translate_strings_p = false;
25377 while (true)
25379 cp_token *token;
25380 tree identifier;
25381 tree attribute;
25383 /* Look for the identifier. We also allow keywords here; for
25384 example `__attribute__ ((const))' is legal. */
25385 token = cp_lexer_peek_token (parser->lexer);
25386 if (token->type == CPP_NAME
25387 || token->type == CPP_KEYWORD)
25389 tree arguments = NULL_TREE;
25391 /* Consume the token, but save it since we need it for the
25392 SIMD enabled function parsing. */
25393 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25395 /* Save away the identifier that indicates which attribute
25396 this is. */
25397 identifier = (token->type == CPP_KEYWORD)
25398 /* For keywords, use the canonical spelling, not the
25399 parsed identifier. */
25400 ? ridpointers[(int) token->keyword]
25401 : id_token->u.value;
25403 identifier = canonicalize_attr_name (identifier);
25404 attribute = build_tree_list (identifier, NULL_TREE);
25406 /* Peek at the next token. */
25407 token = cp_lexer_peek_token (parser->lexer);
25408 /* If it's an `(', then parse the attribute arguments. */
25409 if (token->type == CPP_OPEN_PAREN)
25411 vec<tree, va_gc> *vec;
25412 int attr_flag = (attribute_takes_identifier_p (identifier)
25413 ? id_attr : normal_attr);
25414 vec = cp_parser_parenthesized_expression_list
25415 (parser, attr_flag, /*cast_p=*/false,
25416 /*allow_expansion_p=*/false,
25417 /*non_constant_p=*/NULL);
25418 if (vec == NULL)
25419 arguments = error_mark_node;
25420 else
25422 arguments = build_tree_list_vec (vec);
25423 release_tree_vector (vec);
25425 /* Save the arguments away. */
25426 TREE_VALUE (attribute) = arguments;
25429 if (arguments != error_mark_node)
25431 /* Add this attribute to the list. */
25432 TREE_CHAIN (attribute) = attribute_list;
25433 attribute_list = attribute;
25436 token = cp_lexer_peek_token (parser->lexer);
25438 /* Now, look for more attributes. If the next token isn't a
25439 `,', we're done. */
25440 if (token->type != CPP_COMMA)
25441 break;
25443 /* Consume the comma and keep going. */
25444 cp_lexer_consume_token (parser->lexer);
25446 parser->translate_strings_p = save_translate_strings_p;
25448 /* We built up the list in reverse order. */
25449 return nreverse (attribute_list);
25452 /* Parse a standard C++11 attribute.
25454 The returned representation is a TREE_LIST which TREE_PURPOSE is
25455 the scoped name of the attribute, and the TREE_VALUE is its
25456 arguments list.
25458 Note that the scoped name of the attribute is itself a TREE_LIST
25459 which TREE_PURPOSE is the namespace of the attribute, and
25460 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25461 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25462 and which TREE_PURPOSE is directly the attribute name.
25464 Clients of the attribute code should use get_attribute_namespace
25465 and get_attribute_name to get the actual namespace and name of
25466 attributes, regardless of their being GNU or C++11 attributes.
25468 attribute:
25469 attribute-token attribute-argument-clause [opt]
25471 attribute-token:
25472 identifier
25473 attribute-scoped-token
25475 attribute-scoped-token:
25476 attribute-namespace :: identifier
25478 attribute-namespace:
25479 identifier
25481 attribute-argument-clause:
25482 ( balanced-token-seq )
25484 balanced-token-seq:
25485 balanced-token [opt]
25486 balanced-token-seq balanced-token
25488 balanced-token:
25489 ( balanced-token-seq )
25490 [ balanced-token-seq ]
25491 { balanced-token-seq }. */
25493 static tree
25494 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25496 tree attribute, attr_id = NULL_TREE, arguments;
25497 cp_token *token;
25499 temp_override<bool> cleanup
25500 (parser->auto_is_implicit_function_template_parm_p, false);
25502 /* First, parse name of the attribute, a.k.a attribute-token. */
25504 token = cp_lexer_peek_token (parser->lexer);
25505 if (token->type == CPP_NAME)
25506 attr_id = token->u.value;
25507 else if (token->type == CPP_KEYWORD)
25508 attr_id = ridpointers[(int) token->keyword];
25509 else if (token->flags & NAMED_OP)
25510 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25512 if (attr_id == NULL_TREE)
25513 return NULL_TREE;
25515 cp_lexer_consume_token (parser->lexer);
25517 token = cp_lexer_peek_token (parser->lexer);
25518 if (token->type == CPP_SCOPE)
25520 /* We are seeing a scoped attribute token. */
25522 cp_lexer_consume_token (parser->lexer);
25523 if (attr_ns)
25524 error_at (token->location, "attribute using prefix used together "
25525 "with scoped attribute token");
25526 attr_ns = attr_id;
25528 token = cp_lexer_consume_token (parser->lexer);
25529 if (token->type == CPP_NAME)
25530 attr_id = token->u.value;
25531 else if (token->type == CPP_KEYWORD)
25532 attr_id = ridpointers[(int) token->keyword];
25533 else if (token->flags & NAMED_OP)
25534 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25535 else
25537 error_at (token->location,
25538 "expected an identifier for the attribute name");
25539 return error_mark_node;
25542 attr_ns = canonicalize_attr_name (attr_ns);
25543 attr_id = canonicalize_attr_name (attr_id);
25544 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25545 NULL_TREE);
25546 token = cp_lexer_peek_token (parser->lexer);
25548 else if (attr_ns)
25550 attr_ns = canonicalize_attr_name (attr_ns);
25551 attr_id = canonicalize_attr_name (attr_id);
25552 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25553 NULL_TREE);
25555 else
25557 attr_id = canonicalize_attr_name (attr_id);
25558 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25559 NULL_TREE);
25560 /* C++11 noreturn attribute is equivalent to GNU's. */
25561 if (is_attribute_p ("noreturn", attr_id))
25562 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25563 /* C++14 deprecated attribute is equivalent to GNU's. */
25564 else if (is_attribute_p ("deprecated", attr_id))
25565 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25566 /* C++17 fallthrough attribute is equivalent to GNU's. */
25567 else if (is_attribute_p ("fallthrough", attr_id))
25568 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25569 /* Transactional Memory TS optimize_for_synchronized attribute is
25570 equivalent to GNU transaction_callable. */
25571 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25572 TREE_PURPOSE (attribute)
25573 = get_identifier ("transaction_callable");
25574 /* Transactional Memory attributes are GNU attributes. */
25575 else if (tm_attr_to_mask (attr_id))
25576 TREE_PURPOSE (attribute) = attr_id;
25579 /* Now parse the optional argument clause of the attribute. */
25581 if (token->type != CPP_OPEN_PAREN)
25582 return attribute;
25585 vec<tree, va_gc> *vec;
25586 int attr_flag = normal_attr;
25588 if (attr_ns == gnu_identifier
25589 && attribute_takes_identifier_p (attr_id))
25590 /* A GNU attribute that takes an identifier in parameter. */
25591 attr_flag = id_attr;
25593 vec = cp_parser_parenthesized_expression_list
25594 (parser, attr_flag, /*cast_p=*/false,
25595 /*allow_expansion_p=*/true,
25596 /*non_constant_p=*/NULL);
25597 if (vec == NULL)
25598 arguments = error_mark_node;
25599 else
25601 arguments = build_tree_list_vec (vec);
25602 release_tree_vector (vec);
25605 if (arguments == error_mark_node)
25606 attribute = error_mark_node;
25607 else
25608 TREE_VALUE (attribute) = arguments;
25611 return attribute;
25614 /* Check that the attribute ATTRIBUTE appears at most once in the
25615 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25616 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25617 isn't implemented yet in GCC. */
25619 static void
25620 cp_parser_check_std_attribute (tree attributes, tree attribute)
25622 if (attributes)
25624 tree name = get_attribute_name (attribute);
25625 if (is_attribute_p ("noreturn", name)
25626 && lookup_attribute ("noreturn", attributes))
25627 error ("attribute %<noreturn%> can appear at most once "
25628 "in an attribute-list");
25629 else if (is_attribute_p ("deprecated", name)
25630 && lookup_attribute ("deprecated", attributes))
25631 error ("attribute %<deprecated%> can appear at most once "
25632 "in an attribute-list");
25636 /* Parse a list of standard C++-11 attributes.
25638 attribute-list:
25639 attribute [opt]
25640 attribute-list , attribute[opt]
25641 attribute ...
25642 attribute-list , attribute ...
25645 static tree
25646 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25648 tree attributes = NULL_TREE, attribute = NULL_TREE;
25649 cp_token *token = NULL;
25651 while (true)
25653 attribute = cp_parser_std_attribute (parser, attr_ns);
25654 if (attribute == error_mark_node)
25655 break;
25656 if (attribute != NULL_TREE)
25658 cp_parser_check_std_attribute (attributes, attribute);
25659 TREE_CHAIN (attribute) = attributes;
25660 attributes = attribute;
25662 token = cp_lexer_peek_token (parser->lexer);
25663 if (token->type == CPP_ELLIPSIS)
25665 cp_lexer_consume_token (parser->lexer);
25666 if (attribute == NULL_TREE)
25667 error_at (token->location,
25668 "expected attribute before %<...%>");
25669 else
25671 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25672 if (pack == error_mark_node)
25673 return error_mark_node;
25674 TREE_VALUE (attribute) = pack;
25676 token = cp_lexer_peek_token (parser->lexer);
25678 if (token->type != CPP_COMMA)
25679 break;
25680 cp_lexer_consume_token (parser->lexer);
25682 attributes = nreverse (attributes);
25683 return attributes;
25686 /* Parse a standard C++-11 attribute specifier.
25688 attribute-specifier:
25689 [ [ attribute-using-prefix [opt] attribute-list ] ]
25690 alignment-specifier
25692 attribute-using-prefix:
25693 using attribute-namespace :
25695 alignment-specifier:
25696 alignas ( type-id ... [opt] )
25697 alignas ( alignment-expression ... [opt] ). */
25699 static tree
25700 cp_parser_std_attribute_spec (cp_parser *parser)
25702 tree attributes = NULL_TREE;
25703 cp_token *token = cp_lexer_peek_token (parser->lexer);
25705 if (token->type == CPP_OPEN_SQUARE
25706 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25708 tree attr_ns = NULL_TREE;
25710 cp_lexer_consume_token (parser->lexer);
25711 cp_lexer_consume_token (parser->lexer);
25713 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25715 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25716 if (token->type == CPP_NAME)
25717 attr_ns = token->u.value;
25718 else if (token->type == CPP_KEYWORD)
25719 attr_ns = ridpointers[(int) token->keyword];
25720 else if (token->flags & NAMED_OP)
25721 attr_ns = get_identifier (cpp_type2name (token->type,
25722 token->flags));
25723 if (attr_ns
25724 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25726 if (cxx_dialect < cxx17
25727 && !in_system_header_at (input_location))
25728 pedwarn (input_location, 0,
25729 "attribute using prefix only available "
25730 "with -std=c++17 or -std=gnu++17");
25732 cp_lexer_consume_token (parser->lexer);
25733 cp_lexer_consume_token (parser->lexer);
25734 cp_lexer_consume_token (parser->lexer);
25736 else
25737 attr_ns = NULL_TREE;
25740 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25742 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25743 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25744 cp_parser_skip_to_end_of_statement (parser);
25745 else
25746 /* Warn about parsing c++11 attribute in non-c++11 mode, only
25747 when we are sure that we have actually parsed them. */
25748 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25750 else
25752 tree alignas_expr;
25754 /* Look for an alignment-specifier. */
25756 token = cp_lexer_peek_token (parser->lexer);
25758 if (token->type != CPP_KEYWORD
25759 || token->keyword != RID_ALIGNAS)
25760 return NULL_TREE;
25762 cp_lexer_consume_token (parser->lexer);
25763 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25765 matching_parens parens;
25766 if (!parens.require_open (parser))
25767 return error_mark_node;
25769 cp_parser_parse_tentatively (parser);
25770 alignas_expr = cp_parser_type_id (parser);
25772 if (!cp_parser_parse_definitely (parser))
25774 alignas_expr = cp_parser_assignment_expression (parser);
25775 if (alignas_expr == error_mark_node)
25776 cp_parser_skip_to_end_of_statement (parser);
25777 if (alignas_expr == NULL_TREE
25778 || alignas_expr == error_mark_node)
25779 return alignas_expr;
25782 alignas_expr = cxx_alignas_expr (alignas_expr);
25783 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25785 /* Handle alignas (pack...). */
25786 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25788 cp_lexer_consume_token (parser->lexer);
25789 alignas_expr = make_pack_expansion (alignas_expr);
25792 /* Something went wrong, so don't build the attribute. */
25793 if (alignas_expr == error_mark_node)
25794 return error_mark_node;
25796 if (!parens.require_close (parser))
25797 return error_mark_node;
25799 /* Build the C++-11 representation of an 'aligned'
25800 attribute. */
25801 attributes
25802 = build_tree_list (build_tree_list (gnu_identifier,
25803 aligned_identifier), alignas_expr);
25806 return attributes;
25809 /* Parse a standard C++-11 attribute-specifier-seq.
25811 attribute-specifier-seq:
25812 attribute-specifier-seq [opt] attribute-specifier
25815 static tree
25816 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25818 tree attr_specs = NULL_TREE;
25819 tree attr_last = NULL_TREE;
25821 while (true)
25823 tree attr_spec = cp_parser_std_attribute_spec (parser);
25824 if (attr_spec == NULL_TREE)
25825 break;
25826 if (attr_spec == error_mark_node)
25827 return error_mark_node;
25829 if (attr_last)
25830 TREE_CHAIN (attr_last) = attr_spec;
25831 else
25832 attr_specs = attr_last = attr_spec;
25833 attr_last = tree_last (attr_last);
25836 return attr_specs;
25839 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25840 return index of the first token after balanced-token, or N on failure. */
25842 static size_t
25843 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25845 size_t orig_n = n;
25846 int nparens = 0, nbraces = 0, nsquares = 0;
25848 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25850 case CPP_EOF:
25851 case CPP_PRAGMA_EOL:
25852 /* Ran out of tokens. */
25853 return orig_n;
25854 case CPP_OPEN_PAREN:
25855 ++nparens;
25856 break;
25857 case CPP_OPEN_BRACE:
25858 ++nbraces;
25859 break;
25860 case CPP_OPEN_SQUARE:
25861 ++nsquares;
25862 break;
25863 case CPP_CLOSE_PAREN:
25864 --nparens;
25865 break;
25866 case CPP_CLOSE_BRACE:
25867 --nbraces;
25868 break;
25869 case CPP_CLOSE_SQUARE:
25870 --nsquares;
25871 break;
25872 default:
25873 break;
25875 while (nparens || nbraces || nsquares);
25876 return n;
25879 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25880 return index of the first token after the GNU attribute tokens, or N on
25881 failure. */
25883 static size_t
25884 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25886 while (true)
25888 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25889 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25890 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25891 break;
25893 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25894 if (n2 == n + 2)
25895 break;
25896 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25897 break;
25898 n = n2 + 1;
25900 return n;
25903 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25904 next token), return index of the first token after the standard C++11
25905 attribute tokens, or N on failure. */
25907 static size_t
25908 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25910 while (true)
25912 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25913 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25915 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25916 if (n2 == n + 1)
25917 break;
25918 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25919 break;
25920 n = n2 + 1;
25922 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25923 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25925 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25926 if (n2 == n + 1)
25927 break;
25928 n = n2;
25930 else
25931 break;
25933 return n;
25936 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25937 as the next token), return index of the first token after the attribute
25938 tokens, or N on failure. */
25940 static size_t
25941 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25943 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25944 return cp_parser_skip_gnu_attributes_opt (parser, n);
25945 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25948 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25949 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25950 current value of the PEDANTIC flag, regardless of whether or not
25951 the `__extension__' keyword is present. The caller is responsible
25952 for restoring the value of the PEDANTIC flag. */
25954 static bool
25955 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25957 /* Save the old value of the PEDANTIC flag. */
25958 *saved_pedantic = pedantic;
25960 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25962 /* Consume the `__extension__' token. */
25963 cp_lexer_consume_token (parser->lexer);
25964 /* We're not being pedantic while the `__extension__' keyword is
25965 in effect. */
25966 pedantic = 0;
25968 return true;
25971 return false;
25974 /* Parse a label declaration.
25976 label-declaration:
25977 __label__ label-declarator-seq ;
25979 label-declarator-seq:
25980 identifier , label-declarator-seq
25981 identifier */
25983 static void
25984 cp_parser_label_declaration (cp_parser* parser)
25986 /* Look for the `__label__' keyword. */
25987 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25989 while (true)
25991 tree identifier;
25993 /* Look for an identifier. */
25994 identifier = cp_parser_identifier (parser);
25995 /* If we failed, stop. */
25996 if (identifier == error_mark_node)
25997 break;
25998 /* Declare it as a label. */
25999 finish_label_decl (identifier);
26000 /* If the next token is a `;', stop. */
26001 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26002 break;
26003 /* Look for the `,' separating the label declarations. */
26004 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
26007 /* Look for the final `;'. */
26008 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26011 // -------------------------------------------------------------------------- //
26012 // Requires Clause
26014 // Parse a requires clause.
26016 // requires-clause:
26017 // 'requires' logical-or-expression
26019 // The required logical-or-expression must be a constant expression. Note
26020 // that we don't check that the expression is constepxr here. We defer until
26021 // we analyze constraints and then, we only check atomic constraints.
26022 static tree
26023 cp_parser_requires_clause (cp_parser *parser)
26025 // Parse the requires clause so that it is not automatically folded.
26026 ++processing_template_decl;
26027 tree expr = cp_parser_binary_expression (parser, false, false,
26028 PREC_NOT_OPERATOR, NULL);
26029 if (check_for_bare_parameter_packs (expr))
26030 expr = error_mark_node;
26031 --processing_template_decl;
26032 return expr;
26035 // Optionally parse a requires clause:
26036 static tree
26037 cp_parser_requires_clause_opt (cp_parser *parser)
26039 cp_token *tok = cp_lexer_peek_token (parser->lexer);
26040 if (tok->keyword != RID_REQUIRES)
26042 if (!flag_concepts && tok->type == CPP_NAME
26043 && tok->u.value == ridpointers[RID_REQUIRES])
26045 error_at (cp_lexer_peek_token (parser->lexer)->location,
26046 "%<requires%> only available with -fconcepts");
26047 /* Parse and discard the requires-clause. */
26048 cp_lexer_consume_token (parser->lexer);
26049 cp_parser_requires_clause (parser);
26051 return NULL_TREE;
26053 cp_lexer_consume_token (parser->lexer);
26054 return cp_parser_requires_clause (parser);
26058 /*---------------------------------------------------------------------------
26059 Requires expressions
26060 ---------------------------------------------------------------------------*/
26062 /* Parse a requires expression
26064 requirement-expression:
26065 'requires' requirement-parameter-list [opt] requirement-body */
26066 static tree
26067 cp_parser_requires_expression (cp_parser *parser)
26069 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26070 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26072 /* A requires-expression shall appear only within a concept
26073 definition or a requires-clause.
26075 TODO: Implement this diagnostic correctly. */
26076 if (!processing_template_decl)
26078 error_at (loc, "a requires expression cannot appear outside a template");
26079 cp_parser_skip_to_end_of_statement (parser);
26080 return error_mark_node;
26083 tree parms, reqs;
26085 /* Local parameters are delared as variables within the scope
26086 of the expression. They are not visible past the end of
26087 the expression. Expressions within the requires-expression
26088 are unevaluated. */
26089 struct scope_sentinel
26091 scope_sentinel ()
26093 ++cp_unevaluated_operand;
26094 begin_scope (sk_block, NULL_TREE);
26097 ~scope_sentinel ()
26099 pop_bindings_and_leave_scope ();
26100 --cp_unevaluated_operand;
26102 } s;
26104 /* Parse the optional parameter list. */
26105 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26107 parms = cp_parser_requirement_parameter_list (parser);
26108 if (parms == error_mark_node)
26109 return error_mark_node;
26111 else
26112 parms = NULL_TREE;
26114 /* Parse the requirement body. */
26115 reqs = cp_parser_requirement_body (parser);
26116 if (reqs == error_mark_node)
26117 return error_mark_node;
26120 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26121 the parm chain. */
26122 grokparms (parms, &parms);
26123 return finish_requires_expr (parms, reqs);
26126 /* Parse a parameterized requirement.
26128 requirement-parameter-list:
26129 '(' parameter-declaration-clause ')' */
26130 static tree
26131 cp_parser_requirement_parameter_list (cp_parser *parser)
26133 matching_parens parens;
26134 if (!parens.require_open (parser))
26135 return error_mark_node;
26137 tree parms = cp_parser_parameter_declaration_clause (parser);
26139 if (!parens.require_close (parser))
26140 return error_mark_node;
26142 return parms;
26145 /* Parse the body of a requirement.
26147 requirement-body:
26148 '{' requirement-list '}' */
26149 static tree
26150 cp_parser_requirement_body (cp_parser *parser)
26152 matching_braces braces;
26153 if (!braces.require_open (parser))
26154 return error_mark_node;
26156 tree reqs = cp_parser_requirement_list (parser);
26158 if (!braces.require_close (parser))
26159 return error_mark_node;
26161 return reqs;
26164 /* Parse a list of requirements.
26166 requirement-list:
26167 requirement
26168 requirement-list ';' requirement[opt] */
26169 static tree
26170 cp_parser_requirement_list (cp_parser *parser)
26172 tree result = NULL_TREE;
26173 while (true)
26175 tree req = cp_parser_requirement (parser);
26176 if (req == error_mark_node)
26177 return error_mark_node;
26179 result = tree_cons (NULL_TREE, req, result);
26181 /* If we see a semi-colon, consume it. */
26182 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26183 cp_lexer_consume_token (parser->lexer);
26185 /* Stop processing at the end of the list. */
26186 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26187 break;
26190 /* Reverse the order of requirements so they are analyzed in
26191 declaration order. */
26192 return nreverse (result);
26195 /* Parse a syntactic requirement or type requirement.
26197 requirement:
26198 simple-requirement
26199 compound-requirement
26200 type-requirement
26201 nested-requirement */
26202 static tree
26203 cp_parser_requirement (cp_parser *parser)
26205 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26206 return cp_parser_compound_requirement (parser);
26207 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26208 return cp_parser_type_requirement (parser);
26209 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26210 return cp_parser_nested_requirement (parser);
26211 else
26212 return cp_parser_simple_requirement (parser);
26215 /* Parse a simple requirement.
26217 simple-requirement:
26218 expression ';' */
26219 static tree
26220 cp_parser_simple_requirement (cp_parser *parser)
26222 tree expr = cp_parser_expression (parser, NULL, false, false);
26223 if (!expr || expr == error_mark_node)
26224 return error_mark_node;
26226 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26227 return error_mark_node;
26229 return finish_simple_requirement (expr);
26232 /* Parse a type requirement
26234 type-requirement
26235 nested-name-specifier [opt] required-type-name ';'
26237 required-type-name:
26238 type-name
26239 'template' [opt] simple-template-id */
26240 static tree
26241 cp_parser_type_requirement (cp_parser *parser)
26243 cp_lexer_consume_token (parser->lexer);
26245 // Save the scope before parsing name specifiers.
26246 tree saved_scope = parser->scope;
26247 tree saved_object_scope = parser->object_scope;
26248 tree saved_qualifying_scope = parser->qualifying_scope;
26249 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26250 cp_parser_nested_name_specifier_opt (parser,
26251 /*typename_keyword_p=*/true,
26252 /*check_dependency_p=*/false,
26253 /*type_p=*/true,
26254 /*is_declaration=*/false);
26256 tree type;
26257 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26259 cp_lexer_consume_token (parser->lexer);
26260 type = cp_parser_template_id (parser,
26261 /*template_keyword_p=*/true,
26262 /*check_dependency=*/false,
26263 /*tag_type=*/none_type,
26264 /*is_declaration=*/false);
26265 type = make_typename_type (parser->scope, type, typename_type,
26266 /*complain=*/tf_error);
26268 else
26269 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26271 if (TREE_CODE (type) == TYPE_DECL)
26272 type = TREE_TYPE (type);
26274 parser->scope = saved_scope;
26275 parser->object_scope = saved_object_scope;
26276 parser->qualifying_scope = saved_qualifying_scope;
26278 if (type == error_mark_node)
26279 cp_parser_skip_to_end_of_statement (parser);
26281 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26282 return error_mark_node;
26283 if (type == error_mark_node)
26284 return error_mark_node;
26286 return finish_type_requirement (type);
26289 /* Parse a compound requirement
26291 compound-requirement:
26292 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26293 static tree
26294 cp_parser_compound_requirement (cp_parser *parser)
26296 /* Parse an expression enclosed in '{ }'s. */
26297 matching_braces braces;
26298 if (!braces.require_open (parser))
26299 return error_mark_node;
26301 tree expr = cp_parser_expression (parser, NULL, false, false);
26302 if (!expr || expr == error_mark_node)
26303 return error_mark_node;
26305 if (!braces.require_close (parser))
26306 return error_mark_node;
26308 /* Parse the optional noexcept. */
26309 bool noexcept_p = false;
26310 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26312 cp_lexer_consume_token (parser->lexer);
26313 noexcept_p = true;
26316 /* Parse the optional trailing return type. */
26317 tree type = NULL_TREE;
26318 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26320 cp_lexer_consume_token (parser->lexer);
26321 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26322 parser->in_result_type_constraint_p = true;
26323 type = cp_parser_trailing_type_id (parser);
26324 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26325 if (type == error_mark_node)
26326 return error_mark_node;
26329 return finish_compound_requirement (expr, type, noexcept_p);
26332 /* Parse a nested requirement. This is the same as a requires clause.
26334 nested-requirement:
26335 requires-clause */
26336 static tree
26337 cp_parser_nested_requirement (cp_parser *parser)
26339 cp_lexer_consume_token (parser->lexer);
26340 tree req = cp_parser_requires_clause (parser);
26341 if (req == error_mark_node)
26342 return error_mark_node;
26343 return finish_nested_requirement (req);
26346 /* Support Functions */
26348 /* Return the appropriate prefer_type argument for lookup_name_real based on
26349 tag_type and template_mem_access. */
26351 static inline int
26352 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26354 /* DR 141: When looking in the current enclosing context for a template-name
26355 after -> or ., only consider class templates. */
26356 if (template_mem_access)
26357 return 2;
26358 switch (tag_type)
26360 case none_type: return 0; // No preference.
26361 case scope_type: return 1; // Type or namespace.
26362 default: return 2; // Type only.
26366 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26367 NAME should have one of the representations used for an
26368 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26369 is returned. If PARSER->SCOPE is a dependent type, then a
26370 SCOPE_REF is returned.
26372 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26373 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26374 was formed. Abstractly, such entities should not be passed to this
26375 function, because they do not need to be looked up, but it is
26376 simpler to check for this special case here, rather than at the
26377 call-sites.
26379 In cases not explicitly covered above, this function returns a
26380 DECL, OVERLOAD, or baselink representing the result of the lookup.
26381 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26382 is returned.
26384 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26385 (e.g., "struct") that was used. In that case bindings that do not
26386 refer to types are ignored.
26388 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26389 ignored.
26391 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26392 are ignored.
26394 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26395 types.
26397 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26398 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26399 NULL_TREE otherwise. */
26401 static cp_expr
26402 cp_parser_lookup_name (cp_parser *parser, tree name,
26403 enum tag_types tag_type,
26404 bool is_template,
26405 bool is_namespace,
26406 bool check_dependency,
26407 tree *ambiguous_decls,
26408 location_t name_location)
26410 tree decl;
26411 tree object_type = parser->context->object_type;
26413 /* Assume that the lookup will be unambiguous. */
26414 if (ambiguous_decls)
26415 *ambiguous_decls = NULL_TREE;
26417 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26418 no longer valid. Note that if we are parsing tentatively, and
26419 the parse fails, OBJECT_TYPE will be automatically restored. */
26420 parser->context->object_type = NULL_TREE;
26422 if (name == error_mark_node)
26423 return error_mark_node;
26425 /* A template-id has already been resolved; there is no lookup to
26426 do. */
26427 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26428 return name;
26429 if (BASELINK_P (name))
26431 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26432 == TEMPLATE_ID_EXPR);
26433 return name;
26436 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26437 it should already have been checked to make sure that the name
26438 used matches the type being destroyed. */
26439 if (TREE_CODE (name) == BIT_NOT_EXPR)
26441 tree type;
26443 /* Figure out to which type this destructor applies. */
26444 if (parser->scope)
26445 type = parser->scope;
26446 else if (object_type)
26447 type = object_type;
26448 else
26449 type = current_class_type;
26450 /* If that's not a class type, there is no destructor. */
26451 if (!type || !CLASS_TYPE_P (type))
26452 return error_mark_node;
26454 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26455 lazily_declare_fn (sfk_destructor, type);
26457 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26458 return dtor;
26460 return error_mark_node;
26463 /* By this point, the NAME should be an ordinary identifier. If
26464 the id-expression was a qualified name, the qualifying scope is
26465 stored in PARSER->SCOPE at this point. */
26466 gcc_assert (identifier_p (name));
26468 /* Perform the lookup. */
26469 if (parser->scope)
26471 bool dependent_p;
26473 if (parser->scope == error_mark_node)
26474 return error_mark_node;
26476 /* If the SCOPE is dependent, the lookup must be deferred until
26477 the template is instantiated -- unless we are explicitly
26478 looking up names in uninstantiated templates. Even then, we
26479 cannot look up the name if the scope is not a class type; it
26480 might, for example, be a template type parameter. */
26481 dependent_p = (TYPE_P (parser->scope)
26482 && dependent_scope_p (parser->scope));
26483 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26484 && dependent_p)
26485 /* Defer lookup. */
26486 decl = error_mark_node;
26487 else
26489 tree pushed_scope = NULL_TREE;
26491 /* If PARSER->SCOPE is a dependent type, then it must be a
26492 class type, and we must not be checking dependencies;
26493 otherwise, we would have processed this lookup above. So
26494 that PARSER->SCOPE is not considered a dependent base by
26495 lookup_member, we must enter the scope here. */
26496 if (dependent_p)
26497 pushed_scope = push_scope (parser->scope);
26499 /* If the PARSER->SCOPE is a template specialization, it
26500 may be instantiated during name lookup. In that case,
26501 errors may be issued. Even if we rollback the current
26502 tentative parse, those errors are valid. */
26503 decl = lookup_qualified_name (parser->scope, name,
26504 prefer_type_arg (tag_type),
26505 /*complain=*/true);
26507 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26508 lookup result and the nested-name-specifier nominates a class C:
26509 * if the name specified after the nested-name-specifier, when
26510 looked up in C, is the injected-class-name of C (Clause 9), or
26511 * if the name specified after the nested-name-specifier is the
26512 same as the identifier or the simple-template-id's template-
26513 name in the last component of the nested-name-specifier,
26514 the name is instead considered to name the constructor of
26515 class C. [ Note: for example, the constructor is not an
26516 acceptable lookup result in an elaborated-type-specifier so
26517 the constructor would not be used in place of the
26518 injected-class-name. --end note ] Such a constructor name
26519 shall be used only in the declarator-id of a declaration that
26520 names a constructor or in a using-declaration. */
26521 if (tag_type == none_type
26522 && DECL_SELF_REFERENCE_P (decl)
26523 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26524 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26525 prefer_type_arg (tag_type),
26526 /*complain=*/true);
26528 /* If we have a single function from a using decl, pull it out. */
26529 if (TREE_CODE (decl) == OVERLOAD
26530 && !really_overloaded_fn (decl))
26531 decl = OVL_FUNCTION (decl);
26533 if (pushed_scope)
26534 pop_scope (pushed_scope);
26537 /* If the scope is a dependent type and either we deferred lookup or
26538 we did lookup but didn't find the name, rememeber the name. */
26539 if (decl == error_mark_node && TYPE_P (parser->scope)
26540 && dependent_type_p (parser->scope))
26542 if (tag_type)
26544 tree type;
26546 /* The resolution to Core Issue 180 says that `struct
26547 A::B' should be considered a type-name, even if `A'
26548 is dependent. */
26549 type = make_typename_type (parser->scope, name, tag_type,
26550 /*complain=*/tf_error);
26551 if (type != error_mark_node)
26552 decl = TYPE_NAME (type);
26554 else if (is_template
26555 && (cp_parser_next_token_ends_template_argument_p (parser)
26556 || cp_lexer_next_token_is (parser->lexer,
26557 CPP_CLOSE_PAREN)))
26558 decl = make_unbound_class_template (parser->scope,
26559 name, NULL_TREE,
26560 /*complain=*/tf_error);
26561 else
26562 decl = build_qualified_name (/*type=*/NULL_TREE,
26563 parser->scope, name,
26564 is_template);
26566 parser->qualifying_scope = parser->scope;
26567 parser->object_scope = NULL_TREE;
26569 else if (object_type)
26571 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26572 OBJECT_TYPE is not a class. */
26573 if (CLASS_TYPE_P (object_type))
26574 /* If the OBJECT_TYPE is a template specialization, it may
26575 be instantiated during name lookup. In that case, errors
26576 may be issued. Even if we rollback the current tentative
26577 parse, those errors are valid. */
26578 decl = lookup_member (object_type,
26579 name,
26580 /*protect=*/0,
26581 prefer_type_arg (tag_type),
26582 tf_warning_or_error);
26583 else
26584 decl = NULL_TREE;
26586 if (!decl)
26587 /* Look it up in the enclosing context. DR 141: When looking for a
26588 template-name after -> or ., only consider class templates. */
26589 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26590 /*nonclass=*/0,
26591 /*block_p=*/true, is_namespace, 0);
26592 if (object_type == unknown_type_node)
26593 /* The object is type-dependent, so we can't look anything up; we used
26594 this to get the DR 141 behavior. */
26595 object_type = NULL_TREE;
26596 parser->object_scope = object_type;
26597 parser->qualifying_scope = NULL_TREE;
26599 else
26601 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26602 /*nonclass=*/0,
26603 /*block_p=*/true, is_namespace, 0);
26604 parser->qualifying_scope = NULL_TREE;
26605 parser->object_scope = NULL_TREE;
26608 /* If the lookup failed, let our caller know. */
26609 if (!decl || decl == error_mark_node)
26610 return error_mark_node;
26612 /* Pull out the template from an injected-class-name (or multiple). */
26613 if (is_template)
26614 decl = maybe_get_template_decl_from_type_decl (decl);
26616 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26617 if (TREE_CODE (decl) == TREE_LIST)
26619 if (ambiguous_decls)
26620 *ambiguous_decls = decl;
26621 /* The error message we have to print is too complicated for
26622 cp_parser_error, so we incorporate its actions directly. */
26623 if (!cp_parser_simulate_error (parser))
26625 error_at (name_location, "reference to %qD is ambiguous",
26626 name);
26627 print_candidates (decl);
26629 return error_mark_node;
26632 gcc_assert (DECL_P (decl)
26633 || TREE_CODE (decl) == OVERLOAD
26634 || TREE_CODE (decl) == SCOPE_REF
26635 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26636 || BASELINK_P (decl));
26638 /* If we have resolved the name of a member declaration, check to
26639 see if the declaration is accessible. When the name resolves to
26640 set of overloaded functions, accessibility is checked when
26641 overload resolution is done.
26643 During an explicit instantiation, access is not checked at all,
26644 as per [temp.explicit]. */
26645 if (DECL_P (decl))
26646 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26648 maybe_record_typedef_use (decl);
26650 return cp_expr (decl, name_location);
26653 /* Like cp_parser_lookup_name, but for use in the typical case where
26654 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26655 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26657 static tree
26658 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26660 return cp_parser_lookup_name (parser, name,
26661 none_type,
26662 /*is_template=*/false,
26663 /*is_namespace=*/false,
26664 /*check_dependency=*/true,
26665 /*ambiguous_decls=*/NULL,
26666 location);
26669 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26670 the current context, return the TYPE_DECL. If TAG_NAME_P is
26671 true, the DECL indicates the class being defined in a class-head,
26672 or declared in an elaborated-type-specifier.
26674 Otherwise, return DECL. */
26676 static tree
26677 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26679 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26680 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26682 struct A {
26683 template <typename T> struct B;
26686 template <typename T> struct A::B {};
26688 Similarly, in an elaborated-type-specifier:
26690 namespace N { struct X{}; }
26692 struct A {
26693 template <typename T> friend struct N::X;
26696 However, if the DECL refers to a class type, and we are in
26697 the scope of the class, then the name lookup automatically
26698 finds the TYPE_DECL created by build_self_reference rather
26699 than a TEMPLATE_DECL. For example, in:
26701 template <class T> struct S {
26702 S s;
26705 there is no need to handle such case. */
26707 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26708 return DECL_TEMPLATE_RESULT (decl);
26710 return decl;
26713 /* If too many, or too few, template-parameter lists apply to the
26714 declarator, issue an error message. Returns TRUE if all went well,
26715 and FALSE otherwise. */
26717 static bool
26718 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26719 cp_declarator *declarator,
26720 location_t declarator_location)
26722 switch (declarator->kind)
26724 case cdk_id:
26726 unsigned num_templates = 0;
26727 tree scope = declarator->u.id.qualifying_scope;
26728 bool template_id_p = false;
26730 if (scope)
26731 num_templates = num_template_headers_for_class (scope);
26732 else if (TREE_CODE (declarator->u.id.unqualified_name)
26733 == TEMPLATE_ID_EXPR)
26735 /* If the DECLARATOR has the form `X<y>' then it uses one
26736 additional level of template parameters. */
26737 ++num_templates;
26738 template_id_p = true;
26741 return cp_parser_check_template_parameters
26742 (parser, num_templates, template_id_p, declarator_location,
26743 declarator);
26746 case cdk_function:
26747 case cdk_array:
26748 case cdk_pointer:
26749 case cdk_reference:
26750 case cdk_ptrmem:
26751 return (cp_parser_check_declarator_template_parameters
26752 (parser, declarator->declarator, declarator_location));
26754 case cdk_decomp:
26755 case cdk_error:
26756 return true;
26758 default:
26759 gcc_unreachable ();
26761 return false;
26764 /* NUM_TEMPLATES were used in the current declaration. If that is
26765 invalid, return FALSE and issue an error messages. Otherwise,
26766 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26767 declarator and we can print more accurate diagnostics. */
26769 static bool
26770 cp_parser_check_template_parameters (cp_parser* parser,
26771 unsigned num_templates,
26772 bool template_id_p,
26773 location_t location,
26774 cp_declarator *declarator)
26776 /* If there are the same number of template classes and parameter
26777 lists, that's OK. */
26778 if (parser->num_template_parameter_lists == num_templates)
26779 return true;
26780 /* If there are more, but only one more, and the name ends in an identifier,
26781 then we are declaring a primary template. That's OK too. */
26782 if (!template_id_p
26783 && parser->num_template_parameter_lists == num_templates + 1)
26784 return true;
26785 /* If there are more template classes than parameter lists, we have
26786 something like:
26788 template <class T> void S<T>::R<T>::f (); */
26789 if (parser->num_template_parameter_lists < num_templates)
26791 if (declarator && !current_function_decl)
26792 error_at (location, "specializing member %<%T::%E%> "
26793 "requires %<template<>%> syntax",
26794 declarator->u.id.qualifying_scope,
26795 declarator->u.id.unqualified_name);
26796 else if (declarator)
26797 error_at (location, "invalid declaration of %<%T::%E%>",
26798 declarator->u.id.qualifying_scope,
26799 declarator->u.id.unqualified_name);
26800 else
26801 error_at (location, "too few template-parameter-lists");
26802 return false;
26804 /* Otherwise, there are too many template parameter lists. We have
26805 something like:
26807 template <class T> template <class U> void S::f(); */
26808 error_at (location, "too many template-parameter-lists");
26809 return false;
26812 /* Parse an optional `::' token indicating that the following name is
26813 from the global namespace. If so, PARSER->SCOPE is set to the
26814 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26815 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26816 Returns the new value of PARSER->SCOPE, if the `::' token is
26817 present, and NULL_TREE otherwise. */
26819 static tree
26820 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26822 cp_token *token;
26824 /* Peek at the next token. */
26825 token = cp_lexer_peek_token (parser->lexer);
26826 /* If we're looking at a `::' token then we're starting from the
26827 global namespace, not our current location. */
26828 if (token->type == CPP_SCOPE)
26830 /* Consume the `::' token. */
26831 cp_lexer_consume_token (parser->lexer);
26832 /* Set the SCOPE so that we know where to start the lookup. */
26833 parser->scope = global_namespace;
26834 parser->qualifying_scope = global_namespace;
26835 parser->object_scope = NULL_TREE;
26837 return parser->scope;
26839 else if (!current_scope_valid_p)
26841 parser->scope = NULL_TREE;
26842 parser->qualifying_scope = NULL_TREE;
26843 parser->object_scope = NULL_TREE;
26846 return NULL_TREE;
26849 /* Returns TRUE if the upcoming token sequence is the start of a
26850 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26851 declarator is preceded by the `friend' specifier. */
26853 static bool
26854 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26856 bool constructor_p;
26857 bool outside_class_specifier_p;
26858 tree nested_name_specifier;
26859 cp_token *next_token;
26861 /* The common case is that this is not a constructor declarator, so
26862 try to avoid doing lots of work if at all possible. It's not
26863 valid declare a constructor at function scope. */
26864 if (parser->in_function_body)
26865 return false;
26866 /* And only certain tokens can begin a constructor declarator. */
26867 next_token = cp_lexer_peek_token (parser->lexer);
26868 if (next_token->type != CPP_NAME
26869 && next_token->type != CPP_SCOPE
26870 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26871 && next_token->type != CPP_TEMPLATE_ID)
26872 return false;
26874 /* Parse tentatively; we are going to roll back all of the tokens
26875 consumed here. */
26876 cp_parser_parse_tentatively (parser);
26877 /* Assume that we are looking at a constructor declarator. */
26878 constructor_p = true;
26880 /* Look for the optional `::' operator. */
26881 cp_parser_global_scope_opt (parser,
26882 /*current_scope_valid_p=*/false);
26883 /* Look for the nested-name-specifier. */
26884 nested_name_specifier
26885 = (cp_parser_nested_name_specifier_opt (parser,
26886 /*typename_keyword_p=*/false,
26887 /*check_dependency_p=*/false,
26888 /*type_p=*/false,
26889 /*is_declaration=*/false));
26891 outside_class_specifier_p = (!at_class_scope_p ()
26892 || !TYPE_BEING_DEFINED (current_class_type)
26893 || friend_p);
26895 /* Outside of a class-specifier, there must be a
26896 nested-name-specifier. Except in C++17 mode, where we
26897 might be declaring a guiding declaration. */
26898 if (!nested_name_specifier && outside_class_specifier_p
26899 && cxx_dialect < cxx17)
26900 constructor_p = false;
26901 else if (nested_name_specifier == error_mark_node)
26902 constructor_p = false;
26904 /* If we have a class scope, this is easy; DR 147 says that S::S always
26905 names the constructor, and no other qualified name could. */
26906 if (constructor_p && nested_name_specifier
26907 && CLASS_TYPE_P (nested_name_specifier))
26909 tree id = cp_parser_unqualified_id (parser,
26910 /*template_keyword_p=*/false,
26911 /*check_dependency_p=*/false,
26912 /*declarator_p=*/true,
26913 /*optional_p=*/false);
26914 if (is_overloaded_fn (id))
26915 id = DECL_NAME (get_first_fn (id));
26916 if (!constructor_name_p (id, nested_name_specifier))
26917 constructor_p = false;
26919 /* If we still think that this might be a constructor-declarator,
26920 look for a class-name. */
26921 else if (constructor_p)
26923 /* If we have:
26925 template <typename T> struct S {
26926 S();
26929 we must recognize that the nested `S' names a class. */
26930 if (cxx_dialect >= cxx17)
26931 cp_parser_parse_tentatively (parser);
26933 tree type_decl;
26934 type_decl = cp_parser_class_name (parser,
26935 /*typename_keyword_p=*/false,
26936 /*template_keyword_p=*/false,
26937 none_type,
26938 /*check_dependency_p=*/false,
26939 /*class_head_p=*/false,
26940 /*is_declaration=*/false);
26942 if (cxx_dialect >= cxx17
26943 && !cp_parser_parse_definitely (parser))
26945 type_decl = NULL_TREE;
26946 tree tmpl = cp_parser_template_name (parser,
26947 /*template_keyword*/false,
26948 /*check_dependency_p*/false,
26949 /*is_declaration*/false,
26950 none_type,
26951 /*is_identifier*/NULL);
26952 if (DECL_CLASS_TEMPLATE_P (tmpl)
26953 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26954 /* It's a deduction guide, return true. */;
26955 else
26956 cp_parser_simulate_error (parser);
26959 /* If there was no class-name, then this is not a constructor.
26960 Otherwise, if we are in a class-specifier and we aren't
26961 handling a friend declaration, check that its type matches
26962 current_class_type (c++/38313). Note: error_mark_node
26963 is left alone for error recovery purposes. */
26964 constructor_p = (!cp_parser_error_occurred (parser)
26965 && (outside_class_specifier_p
26966 || type_decl == NULL_TREE
26967 || type_decl == error_mark_node
26968 || same_type_p (current_class_type,
26969 TREE_TYPE (type_decl))));
26971 /* If we're still considering a constructor, we have to see a `(',
26972 to begin the parameter-declaration-clause, followed by either a
26973 `)', an `...', or a decl-specifier. We need to check for a
26974 type-specifier to avoid being fooled into thinking that:
26976 S (f) (int);
26978 is a constructor. (It is actually a function named `f' that
26979 takes one parameter (of type `int') and returns a value of type
26980 `S'. */
26981 if (constructor_p
26982 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26983 constructor_p = false;
26985 if (constructor_p
26986 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26987 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26988 /* A parameter declaration begins with a decl-specifier,
26989 which is either the "attribute" keyword, a storage class
26990 specifier, or (usually) a type-specifier. */
26991 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26993 tree type;
26994 tree pushed_scope = NULL_TREE;
26995 unsigned saved_num_template_parameter_lists;
26997 /* Names appearing in the type-specifier should be looked up
26998 in the scope of the class. */
26999 if (current_class_type)
27000 type = NULL_TREE;
27001 else if (type_decl)
27003 type = TREE_TYPE (type_decl);
27004 if (TREE_CODE (type) == TYPENAME_TYPE)
27006 type = resolve_typename_type (type,
27007 /*only_current_p=*/false);
27008 if (TREE_CODE (type) == TYPENAME_TYPE)
27010 cp_parser_abort_tentative_parse (parser);
27011 return false;
27014 pushed_scope = push_scope (type);
27017 /* Inside the constructor parameter list, surrounding
27018 template-parameter-lists do not apply. */
27019 saved_num_template_parameter_lists
27020 = parser->num_template_parameter_lists;
27021 parser->num_template_parameter_lists = 0;
27023 /* Look for the type-specifier. */
27024 cp_parser_type_specifier (parser,
27025 CP_PARSER_FLAGS_NONE,
27026 /*decl_specs=*/NULL,
27027 /*is_declarator=*/true,
27028 /*declares_class_or_enum=*/NULL,
27029 /*is_cv_qualifier=*/NULL);
27031 parser->num_template_parameter_lists
27032 = saved_num_template_parameter_lists;
27034 /* Leave the scope of the class. */
27035 if (pushed_scope)
27036 pop_scope (pushed_scope);
27038 constructor_p = !cp_parser_error_occurred (parser);
27042 /* We did not really want to consume any tokens. */
27043 cp_parser_abort_tentative_parse (parser);
27045 return constructor_p;
27048 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27049 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27050 they must be performed once we are in the scope of the function.
27052 Returns the function defined. */
27054 static tree
27055 cp_parser_function_definition_from_specifiers_and_declarator
27056 (cp_parser* parser,
27057 cp_decl_specifier_seq *decl_specifiers,
27058 tree attributes,
27059 const cp_declarator *declarator)
27061 tree fn;
27062 bool success_p;
27064 /* Begin the function-definition. */
27065 success_p = start_function (decl_specifiers, declarator, attributes);
27067 /* The things we're about to see are not directly qualified by any
27068 template headers we've seen thus far. */
27069 reset_specialization ();
27071 /* If there were names looked up in the decl-specifier-seq that we
27072 did not check, check them now. We must wait until we are in the
27073 scope of the function to perform the checks, since the function
27074 might be a friend. */
27075 perform_deferred_access_checks (tf_warning_or_error);
27077 if (success_p)
27079 cp_finalize_omp_declare_simd (parser, current_function_decl);
27080 parser->omp_declare_simd = NULL;
27081 cp_finalize_oacc_routine (parser, current_function_decl, true);
27082 parser->oacc_routine = NULL;
27085 if (!success_p)
27087 /* Skip the entire function. */
27088 cp_parser_skip_to_end_of_block_or_statement (parser);
27089 fn = error_mark_node;
27091 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27093 /* Seen already, skip it. An error message has already been output. */
27094 cp_parser_skip_to_end_of_block_or_statement (parser);
27095 fn = current_function_decl;
27096 current_function_decl = NULL_TREE;
27097 /* If this is a function from a class, pop the nested class. */
27098 if (current_class_name)
27099 pop_nested_class ();
27101 else
27103 timevar_id_t tv;
27104 if (DECL_DECLARED_INLINE_P (current_function_decl))
27105 tv = TV_PARSE_INLINE;
27106 else
27107 tv = TV_PARSE_FUNC;
27108 timevar_push (tv);
27109 fn = cp_parser_function_definition_after_declarator (parser,
27110 /*inline_p=*/false);
27111 timevar_pop (tv);
27114 return fn;
27117 /* Parse the part of a function-definition that follows the
27118 declarator. INLINE_P is TRUE iff this function is an inline
27119 function defined within a class-specifier.
27121 Returns the function defined. */
27123 static tree
27124 cp_parser_function_definition_after_declarator (cp_parser* parser,
27125 bool inline_p)
27127 tree fn;
27128 bool saved_in_unbraced_linkage_specification_p;
27129 bool saved_in_function_body;
27130 unsigned saved_num_template_parameter_lists;
27131 cp_token *token;
27132 bool fully_implicit_function_template_p
27133 = parser->fully_implicit_function_template_p;
27134 parser->fully_implicit_function_template_p = false;
27135 tree implicit_template_parms
27136 = parser->implicit_template_parms;
27137 parser->implicit_template_parms = 0;
27138 cp_binding_level* implicit_template_scope
27139 = parser->implicit_template_scope;
27140 parser->implicit_template_scope = 0;
27142 saved_in_function_body = parser->in_function_body;
27143 parser->in_function_body = true;
27144 /* If the next token is `return', then the code may be trying to
27145 make use of the "named return value" extension that G++ used to
27146 support. */
27147 token = cp_lexer_peek_token (parser->lexer);
27148 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27150 /* Consume the `return' keyword. */
27151 cp_lexer_consume_token (parser->lexer);
27152 /* Look for the identifier that indicates what value is to be
27153 returned. */
27154 cp_parser_identifier (parser);
27155 /* Issue an error message. */
27156 error_at (token->location,
27157 "named return values are no longer supported");
27158 /* Skip tokens until we reach the start of the function body. */
27159 while (true)
27161 cp_token *token = cp_lexer_peek_token (parser->lexer);
27162 if (token->type == CPP_OPEN_BRACE
27163 || token->type == CPP_EOF
27164 || token->type == CPP_PRAGMA_EOL)
27165 break;
27166 cp_lexer_consume_token (parser->lexer);
27169 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27170 anything declared inside `f'. */
27171 saved_in_unbraced_linkage_specification_p
27172 = parser->in_unbraced_linkage_specification_p;
27173 parser->in_unbraced_linkage_specification_p = false;
27174 /* Inside the function, surrounding template-parameter-lists do not
27175 apply. */
27176 saved_num_template_parameter_lists
27177 = parser->num_template_parameter_lists;
27178 parser->num_template_parameter_lists = 0;
27180 /* If the next token is `try', `__transaction_atomic', or
27181 `__transaction_relaxed`, then we are looking at either function-try-block
27182 or function-transaction-block. Note that all of these include the
27183 function-body. */
27184 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27185 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27186 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27187 RID_TRANSACTION_RELAXED))
27188 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27189 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27190 cp_parser_function_try_block (parser);
27191 else
27192 cp_parser_ctor_initializer_opt_and_function_body
27193 (parser, /*in_function_try_block=*/false);
27195 /* Finish the function. */
27196 fn = finish_function (inline_p);
27197 /* Generate code for it, if necessary. */
27198 expand_or_defer_fn (fn);
27199 /* Restore the saved values. */
27200 parser->in_unbraced_linkage_specification_p
27201 = saved_in_unbraced_linkage_specification_p;
27202 parser->num_template_parameter_lists
27203 = saved_num_template_parameter_lists;
27204 parser->in_function_body = saved_in_function_body;
27206 parser->fully_implicit_function_template_p
27207 = fully_implicit_function_template_p;
27208 parser->implicit_template_parms
27209 = implicit_template_parms;
27210 parser->implicit_template_scope
27211 = implicit_template_scope;
27213 if (parser->fully_implicit_function_template_p)
27214 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27216 return fn;
27219 /* Parse a template-declaration body (following argument list). */
27221 static void
27222 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27223 tree parameter_list,
27224 bool member_p)
27226 tree decl = NULL_TREE;
27227 bool friend_p = false;
27229 /* We just processed one more parameter list. */
27230 ++parser->num_template_parameter_lists;
27232 /* Get the deferred access checks from the parameter list. These
27233 will be checked once we know what is being declared, as for a
27234 member template the checks must be performed in the scope of the
27235 class containing the member. */
27236 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27238 /* Tentatively parse for a new template parameter list, which can either be
27239 the template keyword or a template introduction. */
27240 if (cp_parser_template_declaration_after_export (parser, member_p))
27241 /* OK */;
27242 else if (cxx_dialect >= cxx11
27243 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27244 decl = cp_parser_alias_declaration (parser);
27245 else
27247 /* There are no access checks when parsing a template, as we do not
27248 know if a specialization will be a friend. */
27249 push_deferring_access_checks (dk_no_check);
27250 cp_token *token = cp_lexer_peek_token (parser->lexer);
27251 decl = cp_parser_single_declaration (parser,
27252 checks,
27253 member_p,
27254 /*explicit_specialization_p=*/false,
27255 &friend_p);
27256 pop_deferring_access_checks ();
27258 /* If this is a member template declaration, let the front
27259 end know. */
27260 if (member_p && !friend_p && decl)
27262 if (TREE_CODE (decl) == TYPE_DECL)
27263 cp_parser_check_access_in_redeclaration (decl, token->location);
27265 decl = finish_member_template_decl (decl);
27267 else if (friend_p && decl
27268 && DECL_DECLARES_TYPE_P (decl))
27269 make_friend_class (current_class_type, TREE_TYPE (decl),
27270 /*complain=*/true);
27272 /* We are done with the current parameter list. */
27273 --parser->num_template_parameter_lists;
27275 pop_deferring_access_checks ();
27277 /* Finish up. */
27278 finish_template_decl (parameter_list);
27280 /* Check the template arguments for a literal operator template. */
27281 if (decl
27282 && DECL_DECLARES_FUNCTION_P (decl)
27283 && UDLIT_OPER_P (DECL_NAME (decl)))
27285 bool ok = true;
27286 if (parameter_list == NULL_TREE)
27287 ok = false;
27288 else
27290 int num_parms = TREE_VEC_LENGTH (parameter_list);
27291 if (num_parms == 1)
27293 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27294 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27295 if (CLASS_TYPE_P (TREE_TYPE (parm)))
27296 /* OK, C++20 string literal operator template. We don't need
27297 to warn in lower dialects here because we will have already
27298 warned about the template parameter. */;
27299 else if (TREE_TYPE (parm) != char_type_node
27300 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27301 ok = false;
27303 else if (num_parms == 2 && cxx_dialect >= cxx14)
27305 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27306 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27307 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27308 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27309 if (parm == error_mark_node
27310 || TREE_TYPE (parm) != TREE_TYPE (type)
27311 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27312 ok = false;
27313 else
27314 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27315 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
27316 "ISO C++ did not adopt string literal operator templa"
27317 "tes taking an argument pack of characters");
27319 else
27320 ok = false;
27322 if (!ok)
27324 if (cxx_dialect > cxx17)
27325 error ("literal operator template %qD has invalid parameter list;"
27326 " Expected non-type template parameter pack <char...> "
27327 " or single non-type parameter of class type",
27328 decl);
27329 else
27330 error ("literal operator template %qD has invalid parameter list."
27331 " Expected non-type template parameter pack <char...>",
27332 decl);
27336 /* Register member declarations. */
27337 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27338 finish_member_declaration (decl);
27339 /* If DECL is a function template, we must return to parse it later.
27340 (Even though there is no definition, there might be default
27341 arguments that need handling.) */
27342 if (member_p && decl
27343 && DECL_DECLARES_FUNCTION_P (decl))
27344 vec_safe_push (unparsed_funs_with_definitions, decl);
27347 /* Parse a template introduction header for a template-declaration. Returns
27348 false if tentative parse fails. */
27350 static bool
27351 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27353 cp_parser_parse_tentatively (parser);
27355 tree saved_scope = parser->scope;
27356 tree saved_object_scope = parser->object_scope;
27357 tree saved_qualifying_scope = parser->qualifying_scope;
27359 /* Look for the optional `::' operator. */
27360 cp_parser_global_scope_opt (parser,
27361 /*current_scope_valid_p=*/false);
27362 /* Look for the nested-name-specifier. */
27363 cp_parser_nested_name_specifier_opt (parser,
27364 /*typename_keyword_p=*/false,
27365 /*check_dependency_p=*/true,
27366 /*type_p=*/false,
27367 /*is_declaration=*/false);
27369 cp_token *token = cp_lexer_peek_token (parser->lexer);
27370 tree concept_name = cp_parser_identifier (parser);
27372 /* Look up the concept for which we will be matching
27373 template parameters. */
27374 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27375 token->location);
27376 parser->scope = saved_scope;
27377 parser->object_scope = saved_object_scope;
27378 parser->qualifying_scope = saved_qualifying_scope;
27380 if (concept_name == error_mark_node)
27381 cp_parser_simulate_error (parser);
27383 /* Look for opening brace for introduction. */
27384 matching_braces braces;
27385 braces.require_open (parser);
27387 if (!cp_parser_parse_definitely (parser))
27388 return false;
27390 push_deferring_access_checks (dk_deferred);
27392 /* Build vector of placeholder parameters and grab
27393 matching identifiers. */
27394 tree introduction_list = cp_parser_introduction_list (parser);
27396 /* Look for closing brace for introduction. */
27397 if (!braces.require_close (parser))
27398 return true;
27400 /* The introduction-list shall not be empty. */
27401 int nargs = TREE_VEC_LENGTH (introduction_list);
27402 if (nargs == 0)
27404 /* In cp_parser_introduction_list we have already issued an error. */
27405 return true;
27408 if (tmpl_decl == error_mark_node)
27410 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27411 token->location);
27412 return true;
27415 /* Build and associate the constraint. */
27416 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27417 if (parms && parms != error_mark_node)
27419 cp_parser_template_declaration_after_parameters (parser, parms,
27420 member_p);
27421 return true;
27424 error_at (token->location, "no matching concept for template-introduction");
27425 return true;
27428 /* Parse a normal template-declaration following the template keyword. */
27430 static void
27431 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27433 tree parameter_list;
27434 bool need_lang_pop;
27435 location_t location = input_location;
27437 /* Look for the `<' token. */
27438 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27439 return;
27440 if (at_class_scope_p () && current_function_decl)
27442 /* 14.5.2.2 [temp.mem]
27444 A local class shall not have member templates. */
27445 error_at (location,
27446 "invalid declaration of member template in local class");
27447 cp_parser_skip_to_end_of_block_or_statement (parser);
27448 return;
27450 /* [temp]
27452 A template ... shall not have C linkage. */
27453 if (current_lang_name == lang_name_c)
27455 error_at (location, "template with C linkage");
27456 maybe_show_extern_c_location ();
27457 /* Give it C++ linkage to avoid confusing other parts of the
27458 front end. */
27459 push_lang_context (lang_name_cplusplus);
27460 need_lang_pop = true;
27462 else
27463 need_lang_pop = false;
27465 /* We cannot perform access checks on the template parameter
27466 declarations until we know what is being declared, just as we
27467 cannot check the decl-specifier list. */
27468 push_deferring_access_checks (dk_deferred);
27470 /* If the next token is `>', then we have an invalid
27471 specialization. Rather than complain about an invalid template
27472 parameter, issue an error message here. */
27473 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27475 cp_parser_error (parser, "invalid explicit specialization");
27476 begin_specialization ();
27477 parameter_list = NULL_TREE;
27479 else
27481 /* Parse the template parameters. */
27482 parameter_list = cp_parser_template_parameter_list (parser);
27485 /* Look for the `>'. */
27486 cp_parser_skip_to_end_of_template_parameter_list (parser);
27488 /* Manage template requirements */
27489 if (flag_concepts)
27491 tree reqs = get_shorthand_constraints (current_template_parms);
27492 if (tree r = cp_parser_requires_clause_opt (parser))
27493 reqs = conjoin_constraints (reqs, normalize_expression (r));
27494 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27497 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27498 member_p);
27500 /* For the erroneous case of a template with C linkage, we pushed an
27501 implicit C++ linkage scope; exit that scope now. */
27502 if (need_lang_pop)
27503 pop_lang_context ();
27506 /* Parse a template-declaration, assuming that the `export' (and
27507 `extern') keywords, if present, has already been scanned. MEMBER_P
27508 is as for cp_parser_template_declaration. */
27510 static bool
27511 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27513 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27515 cp_lexer_consume_token (parser->lexer);
27516 cp_parser_explicit_template_declaration (parser, member_p);
27517 return true;
27519 else if (flag_concepts)
27520 return cp_parser_template_introduction (parser, member_p);
27522 return false;
27525 /* Perform the deferred access checks from a template-parameter-list.
27526 CHECKS is a TREE_LIST of access checks, as returned by
27527 get_deferred_access_checks. */
27529 static void
27530 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27532 ++processing_template_parmlist;
27533 perform_access_checks (checks, tf_warning_or_error);
27534 --processing_template_parmlist;
27537 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27538 `function-definition' sequence that follows a template header.
27539 If MEMBER_P is true, this declaration appears in a class scope.
27541 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27542 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27544 static tree
27545 cp_parser_single_declaration (cp_parser* parser,
27546 vec<deferred_access_check, va_gc> *checks,
27547 bool member_p,
27548 bool explicit_specialization_p,
27549 bool* friend_p)
27551 int declares_class_or_enum;
27552 tree decl = NULL_TREE;
27553 cp_decl_specifier_seq decl_specifiers;
27554 bool function_definition_p = false;
27555 cp_token *decl_spec_token_start;
27557 /* This function is only used when processing a template
27558 declaration. */
27559 gcc_assert (innermost_scope_kind () == sk_template_parms
27560 || innermost_scope_kind () == sk_template_spec);
27562 /* Defer access checks until we know what is being declared. */
27563 push_deferring_access_checks (dk_deferred);
27565 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27566 alternative. */
27567 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27568 cp_parser_decl_specifier_seq (parser,
27569 CP_PARSER_FLAGS_OPTIONAL,
27570 &decl_specifiers,
27571 &declares_class_or_enum);
27572 if (friend_p)
27573 *friend_p = cp_parser_friend_p (&decl_specifiers);
27575 /* There are no template typedefs. */
27576 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27578 error_at (decl_spec_token_start->location,
27579 "template declaration of %<typedef%>");
27580 decl = error_mark_node;
27583 /* Gather up the access checks that occurred the
27584 decl-specifier-seq. */
27585 stop_deferring_access_checks ();
27587 /* Check for the declaration of a template class. */
27588 if (declares_class_or_enum)
27590 if (cp_parser_declares_only_class_p (parser)
27591 || (declares_class_or_enum & 2))
27593 // If this is a declaration, but not a definition, associate
27594 // any constraints with the type declaration. Constraints
27595 // are associated with definitions in cp_parser_class_specifier.
27596 if (declares_class_or_enum == 1)
27597 associate_classtype_constraints (decl_specifiers.type);
27599 decl = shadow_tag (&decl_specifiers);
27601 /* In this case:
27603 struct C {
27604 friend template <typename T> struct A<T>::B;
27607 A<T>::B will be represented by a TYPENAME_TYPE, and
27608 therefore not recognized by shadow_tag. */
27609 if (friend_p && *friend_p
27610 && !decl
27611 && decl_specifiers.type
27612 && TYPE_P (decl_specifiers.type))
27613 decl = decl_specifiers.type;
27615 if (decl && decl != error_mark_node)
27616 decl = TYPE_NAME (decl);
27617 else
27618 decl = error_mark_node;
27620 /* Perform access checks for template parameters. */
27621 cp_parser_perform_template_parameter_access_checks (checks);
27623 /* Give a helpful diagnostic for
27624 template <class T> struct A { } a;
27625 if we aren't already recovering from an error. */
27626 if (!cp_parser_declares_only_class_p (parser)
27627 && !seen_error ())
27629 error_at (cp_lexer_peek_token (parser->lexer)->location,
27630 "a class template declaration must not declare "
27631 "anything else");
27632 cp_parser_skip_to_end_of_block_or_statement (parser);
27633 goto out;
27638 /* Complain about missing 'typename' or other invalid type names. */
27639 if (!decl_specifiers.any_type_specifiers_p
27640 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27642 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27643 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27644 the rest of this declaration. */
27645 decl = error_mark_node;
27646 goto out;
27649 /* If it's not a template class, try for a template function. If
27650 the next token is a `;', then this declaration does not declare
27651 anything. But, if there were errors in the decl-specifiers, then
27652 the error might well have come from an attempted class-specifier.
27653 In that case, there's no need to warn about a missing declarator. */
27654 if (!decl
27655 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27656 || decl_specifiers.type != error_mark_node))
27658 decl = cp_parser_init_declarator (parser,
27659 &decl_specifiers,
27660 checks,
27661 /*function_definition_allowed_p=*/true,
27662 member_p,
27663 declares_class_or_enum,
27664 &function_definition_p,
27665 NULL, NULL, NULL);
27667 /* 7.1.1-1 [dcl.stc]
27669 A storage-class-specifier shall not be specified in an explicit
27670 specialization... */
27671 if (decl
27672 && explicit_specialization_p
27673 && decl_specifiers.storage_class != sc_none)
27675 error_at (decl_spec_token_start->location,
27676 "explicit template specialization cannot have a storage class");
27677 decl = error_mark_node;
27680 if (decl && VAR_P (decl))
27681 check_template_variable (decl);
27684 /* Look for a trailing `;' after the declaration. */
27685 if (!function_definition_p
27686 && (decl == error_mark_node
27687 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27688 cp_parser_skip_to_end_of_block_or_statement (parser);
27690 out:
27691 pop_deferring_access_checks ();
27693 /* Clear any current qualification; whatever comes next is the start
27694 of something new. */
27695 parser->scope = NULL_TREE;
27696 parser->qualifying_scope = NULL_TREE;
27697 parser->object_scope = NULL_TREE;
27699 return decl;
27702 /* Parse a cast-expression that is not the operand of a unary "&". */
27704 static cp_expr
27705 cp_parser_simple_cast_expression (cp_parser *parser)
27707 return cp_parser_cast_expression (parser, /*address_p=*/false,
27708 /*cast_p=*/false, /*decltype*/false, NULL);
27711 /* Parse a functional cast to TYPE. Returns an expression
27712 representing the cast. */
27714 static cp_expr
27715 cp_parser_functional_cast (cp_parser* parser, tree type)
27717 vec<tree, va_gc> *vec;
27718 tree expression_list;
27719 cp_expr cast;
27720 bool nonconst_p;
27722 location_t start_loc = input_location;
27724 if (!type)
27725 type = error_mark_node;
27727 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27729 cp_lexer_set_source_position (parser->lexer);
27730 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27731 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27732 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27733 if (TREE_CODE (type) == TYPE_DECL)
27734 type = TREE_TYPE (type);
27736 cast = finish_compound_literal (type, expression_list,
27737 tf_warning_or_error, fcl_functional);
27738 /* Create a location of the form:
27739 type_name{i, f}
27740 ^~~~~~~~~~~~~~~
27741 with caret == start at the start of the type name,
27742 finishing at the closing brace. */
27743 location_t finish_loc
27744 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27745 location_t combined_loc = make_location (start_loc, start_loc,
27746 finish_loc);
27747 cast.set_location (combined_loc);
27748 return cast;
27752 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27753 /*cast_p=*/true,
27754 /*allow_expansion_p=*/true,
27755 /*non_constant_p=*/NULL);
27756 if (vec == NULL)
27757 expression_list = error_mark_node;
27758 else
27760 expression_list = build_tree_list_vec (vec);
27761 release_tree_vector (vec);
27764 cast = build_functional_cast (type, expression_list,
27765 tf_warning_or_error);
27766 /* [expr.const]/1: In an integral constant expression "only type
27767 conversions to integral or enumeration type can be used". */
27768 if (TREE_CODE (type) == TYPE_DECL)
27769 type = TREE_TYPE (type);
27770 if (cast != error_mark_node
27771 && !cast_valid_in_integral_constant_expression_p (type)
27772 && cp_parser_non_integral_constant_expression (parser,
27773 NIC_CONSTRUCTOR))
27774 return error_mark_node;
27776 /* Create a location of the form:
27777 float(i)
27778 ^~~~~~~~
27779 with caret == start at the start of the type name,
27780 finishing at the closing paren. */
27781 location_t finish_loc
27782 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27783 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27784 cast.set_location (combined_loc);
27785 return cast;
27788 /* Save the tokens that make up the body of a member function defined
27789 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27790 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27791 specifiers applied to the declaration. Returns the FUNCTION_DECL
27792 for the member function. */
27794 static tree
27795 cp_parser_save_member_function_body (cp_parser* parser,
27796 cp_decl_specifier_seq *decl_specifiers,
27797 cp_declarator *declarator,
27798 tree attributes)
27800 cp_token *first;
27801 cp_token *last;
27802 tree fn;
27803 bool function_try_block = false;
27805 /* Create the FUNCTION_DECL. */
27806 fn = grokmethod (decl_specifiers, declarator, attributes);
27807 cp_finalize_omp_declare_simd (parser, fn);
27808 cp_finalize_oacc_routine (parser, fn, true);
27809 /* If something went badly wrong, bail out now. */
27810 if (fn == error_mark_node)
27812 /* If there's a function-body, skip it. */
27813 if (cp_parser_token_starts_function_definition_p
27814 (cp_lexer_peek_token (parser->lexer)))
27815 cp_parser_skip_to_end_of_block_or_statement (parser);
27816 return error_mark_node;
27819 /* Remember it, if there default args to post process. */
27820 cp_parser_save_default_args (parser, fn);
27822 /* Save away the tokens that make up the body of the
27823 function. */
27824 first = parser->lexer->next_token;
27826 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27827 cp_lexer_consume_token (parser->lexer);
27828 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27829 RID_TRANSACTION_ATOMIC))
27831 cp_lexer_consume_token (parser->lexer);
27832 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27833 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27834 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27835 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27836 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27837 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27838 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27840 cp_lexer_consume_token (parser->lexer);
27841 cp_lexer_consume_token (parser->lexer);
27842 cp_lexer_consume_token (parser->lexer);
27843 cp_lexer_consume_token (parser->lexer);
27844 cp_lexer_consume_token (parser->lexer);
27846 else
27847 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27848 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27850 cp_lexer_consume_token (parser->lexer);
27851 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27852 break;
27856 /* Handle function try blocks. */
27857 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27859 cp_lexer_consume_token (parser->lexer);
27860 function_try_block = true;
27862 /* We can have braced-init-list mem-initializers before the fn body. */
27863 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27865 cp_lexer_consume_token (parser->lexer);
27866 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27868 /* cache_group will stop after an un-nested { } pair, too. */
27869 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27870 break;
27872 /* variadic mem-inits have ... after the ')'. */
27873 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27874 cp_lexer_consume_token (parser->lexer);
27877 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27878 /* Handle function try blocks. */
27879 if (function_try_block)
27880 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27881 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27882 last = parser->lexer->next_token;
27884 /* Save away the inline definition; we will process it when the
27885 class is complete. */
27886 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27887 DECL_PENDING_INLINE_P (fn) = 1;
27889 /* We need to know that this was defined in the class, so that
27890 friend templates are handled correctly. */
27891 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27893 /* Add FN to the queue of functions to be parsed later. */
27894 vec_safe_push (unparsed_funs_with_definitions, fn);
27896 return fn;
27899 /* Save the tokens that make up the in-class initializer for a non-static
27900 data member. Returns a DEFAULT_ARG. */
27902 static tree
27903 cp_parser_save_nsdmi (cp_parser* parser)
27905 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27908 /* Parse a template-argument-list, as well as the trailing ">" (but
27909 not the opening "<"). See cp_parser_template_argument_list for the
27910 return value. */
27912 static tree
27913 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27915 tree arguments;
27916 tree saved_scope;
27917 tree saved_qualifying_scope;
27918 tree saved_object_scope;
27919 bool saved_greater_than_is_operator_p;
27921 /* [temp.names]
27923 When parsing a template-id, the first non-nested `>' is taken as
27924 the end of the template-argument-list rather than a greater-than
27925 operator. */
27926 saved_greater_than_is_operator_p
27927 = parser->greater_than_is_operator_p;
27928 parser->greater_than_is_operator_p = false;
27929 /* Parsing the argument list may modify SCOPE, so we save it
27930 here. */
27931 saved_scope = parser->scope;
27932 saved_qualifying_scope = parser->qualifying_scope;
27933 saved_object_scope = parser->object_scope;
27934 /* We need to evaluate the template arguments, even though this
27935 template-id may be nested within a "sizeof". */
27936 cp_evaluated ev;
27937 /* Parse the template-argument-list itself. */
27938 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27939 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27940 arguments = NULL_TREE;
27941 else
27942 arguments = cp_parser_template_argument_list (parser);
27943 /* Look for the `>' that ends the template-argument-list. If we find
27944 a '>>' instead, it's probably just a typo. */
27945 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27947 if (cxx_dialect != cxx98)
27949 /* In C++0x, a `>>' in a template argument list or cast
27950 expression is considered to be two separate `>'
27951 tokens. So, change the current token to a `>', but don't
27952 consume it: it will be consumed later when the outer
27953 template argument list (or cast expression) is parsed.
27954 Note that this replacement of `>' for `>>' is necessary
27955 even if we are parsing tentatively: in the tentative
27956 case, after calling
27957 cp_parser_enclosed_template_argument_list we will always
27958 throw away all of the template arguments and the first
27959 closing `>', either because the template argument list
27960 was erroneous or because we are replacing those tokens
27961 with a CPP_TEMPLATE_ID token. The second `>' (which will
27962 not have been thrown away) is needed either to close an
27963 outer template argument list or to complete a new-style
27964 cast. */
27965 cp_token *token = cp_lexer_peek_token (parser->lexer);
27966 token->type = CPP_GREATER;
27968 else if (!saved_greater_than_is_operator_p)
27970 /* If we're in a nested template argument list, the '>>' has
27971 to be a typo for '> >'. We emit the error message, but we
27972 continue parsing and we push a '>' as next token, so that
27973 the argument list will be parsed correctly. Note that the
27974 global source location is still on the token before the
27975 '>>', so we need to say explicitly where we want it. */
27976 cp_token *token = cp_lexer_peek_token (parser->lexer);
27977 gcc_rich_location richloc (token->location);
27978 richloc.add_fixit_replace ("> >");
27979 error_at (&richloc, "%<>>%> should be %<> >%> "
27980 "within a nested template argument list");
27982 token->type = CPP_GREATER;
27984 else
27986 /* If this is not a nested template argument list, the '>>'
27987 is a typo for '>'. Emit an error message and continue.
27988 Same deal about the token location, but here we can get it
27989 right by consuming the '>>' before issuing the diagnostic. */
27990 cp_token *token = cp_lexer_consume_token (parser->lexer);
27991 error_at (token->location,
27992 "spurious %<>>%>, use %<>%> to terminate "
27993 "a template argument list");
27996 else
27997 cp_parser_skip_to_end_of_template_parameter_list (parser);
27998 /* The `>' token might be a greater-than operator again now. */
27999 parser->greater_than_is_operator_p
28000 = saved_greater_than_is_operator_p;
28001 /* Restore the SAVED_SCOPE. */
28002 parser->scope = saved_scope;
28003 parser->qualifying_scope = saved_qualifying_scope;
28004 parser->object_scope = saved_object_scope;
28006 return arguments;
28009 /* MEMBER_FUNCTION is a member function, or a friend. If default
28010 arguments, or the body of the function have not yet been parsed,
28011 parse them now. */
28013 static void
28014 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
28016 timevar_push (TV_PARSE_INMETH);
28017 /* If this member is a template, get the underlying
28018 FUNCTION_DECL. */
28019 if (DECL_FUNCTION_TEMPLATE_P (member_function))
28020 member_function = DECL_TEMPLATE_RESULT (member_function);
28022 /* There should not be any class definitions in progress at this
28023 point; the bodies of members are only parsed outside of all class
28024 definitions. */
28025 gcc_assert (parser->num_classes_being_defined == 0);
28026 /* While we're parsing the member functions we might encounter more
28027 classes. We want to handle them right away, but we don't want
28028 them getting mixed up with functions that are currently in the
28029 queue. */
28030 push_unparsed_function_queues (parser);
28032 /* Make sure that any template parameters are in scope. */
28033 maybe_begin_member_template_processing (member_function);
28035 /* If the body of the function has not yet been parsed, parse it
28036 now. */
28037 if (DECL_PENDING_INLINE_P (member_function))
28039 tree function_scope;
28040 cp_token_cache *tokens;
28042 /* The function is no longer pending; we are processing it. */
28043 tokens = DECL_PENDING_INLINE_INFO (member_function);
28044 DECL_PENDING_INLINE_INFO (member_function) = NULL;
28045 DECL_PENDING_INLINE_P (member_function) = 0;
28047 /* If this is a local class, enter the scope of the containing
28048 function. */
28049 function_scope = current_function_decl;
28050 if (function_scope)
28051 push_function_context ();
28053 /* Push the body of the function onto the lexer stack. */
28054 cp_parser_push_lexer_for_tokens (parser, tokens);
28056 /* Let the front end know that we going to be defining this
28057 function. */
28058 start_preparsed_function (member_function, NULL_TREE,
28059 SF_PRE_PARSED | SF_INCLASS_INLINE);
28061 /* Don't do access checking if it is a templated function. */
28062 if (processing_template_decl)
28063 push_deferring_access_checks (dk_no_check);
28065 /* #pragma omp declare reduction needs special parsing. */
28066 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28068 parser->lexer->in_pragma = true;
28069 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28070 finish_function (/*inline_p=*/true);
28071 cp_check_omp_declare_reduction (member_function);
28073 else
28074 /* Now, parse the body of the function. */
28075 cp_parser_function_definition_after_declarator (parser,
28076 /*inline_p=*/true);
28078 if (processing_template_decl)
28079 pop_deferring_access_checks ();
28081 /* Leave the scope of the containing function. */
28082 if (function_scope)
28083 pop_function_context ();
28084 cp_parser_pop_lexer (parser);
28087 /* Remove any template parameters from the symbol table. */
28088 maybe_end_member_template_processing ();
28090 /* Restore the queue. */
28091 pop_unparsed_function_queues (parser);
28092 timevar_pop (TV_PARSE_INMETH);
28095 /* If DECL contains any default args, remember it on the unparsed
28096 functions queue. */
28098 static void
28099 cp_parser_save_default_args (cp_parser* parser, tree decl)
28101 tree probe;
28103 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28104 probe;
28105 probe = TREE_CHAIN (probe))
28106 if (TREE_PURPOSE (probe))
28108 cp_default_arg_entry entry = {current_class_type, decl};
28109 vec_safe_push (unparsed_funs_with_default_args, entry);
28110 break;
28114 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28115 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28116 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28117 from the parameter-type-list. */
28119 static tree
28120 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28121 tree default_arg, tree parmtype)
28123 cp_token_cache *tokens;
28124 tree parsed_arg;
28125 bool dummy;
28127 if (default_arg == error_mark_node)
28128 return error_mark_node;
28130 /* Push the saved tokens for the default argument onto the parser's
28131 lexer stack. */
28132 tokens = DEFARG_TOKENS (default_arg);
28133 cp_parser_push_lexer_for_tokens (parser, tokens);
28135 start_lambda_scope (decl);
28137 /* Parse the default argument. */
28138 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28139 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28140 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28142 finish_lambda_scope ();
28144 if (parsed_arg == error_mark_node)
28145 cp_parser_skip_to_end_of_statement (parser);
28147 if (!processing_template_decl)
28149 /* In a non-template class, check conversions now. In a template,
28150 we'll wait and instantiate these as needed. */
28151 if (TREE_CODE (decl) == PARM_DECL)
28152 parsed_arg = check_default_argument (parmtype, parsed_arg,
28153 tf_warning_or_error);
28154 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28155 parsed_arg = error_mark_node;
28156 else
28157 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28160 /* If the token stream has not been completely used up, then
28161 there was extra junk after the end of the default
28162 argument. */
28163 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28165 if (TREE_CODE (decl) == PARM_DECL)
28166 cp_parser_error (parser, "expected %<,%>");
28167 else
28168 cp_parser_error (parser, "expected %<;%>");
28171 /* Revert to the main lexer. */
28172 cp_parser_pop_lexer (parser);
28174 return parsed_arg;
28177 /* FIELD is a non-static data member with an initializer which we saved for
28178 later; parse it now. */
28180 static void
28181 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28183 tree def;
28185 maybe_begin_member_template_processing (field);
28187 push_unparsed_function_queues (parser);
28188 def = cp_parser_late_parse_one_default_arg (parser, field,
28189 DECL_INITIAL (field),
28190 NULL_TREE);
28191 pop_unparsed_function_queues (parser);
28193 maybe_end_member_template_processing ();
28195 DECL_INITIAL (field) = def;
28198 /* FN is a FUNCTION_DECL which may contains a parameter with an
28199 unparsed DEFAULT_ARG. Parse the default args now. This function
28200 assumes that the current scope is the scope in which the default
28201 argument should be processed. */
28203 static void
28204 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28206 bool saved_local_variables_forbidden_p;
28207 tree parm, parmdecl;
28209 /* While we're parsing the default args, we might (due to the
28210 statement expression extension) encounter more classes. We want
28211 to handle them right away, but we don't want them getting mixed
28212 up with default args that are currently in the queue. */
28213 push_unparsed_function_queues (parser);
28215 /* Local variable names (and the `this' keyword) may not appear
28216 in a default argument. */
28217 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28218 parser->local_variables_forbidden_p = true;
28220 push_defarg_context (fn);
28222 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28223 parmdecl = DECL_ARGUMENTS (fn);
28224 parm && parm != void_list_node;
28225 parm = TREE_CHAIN (parm),
28226 parmdecl = DECL_CHAIN (parmdecl))
28228 tree default_arg = TREE_PURPOSE (parm);
28229 tree parsed_arg;
28230 vec<tree, va_gc> *insts;
28231 tree copy;
28232 unsigned ix;
28234 if (!default_arg)
28235 continue;
28237 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28238 /* This can happen for a friend declaration for a function
28239 already declared with default arguments. */
28240 continue;
28242 parsed_arg
28243 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28244 default_arg,
28245 TREE_VALUE (parm));
28246 TREE_PURPOSE (parm) = parsed_arg;
28248 /* Update any instantiations we've already created. */
28249 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28250 vec_safe_iterate (insts, ix, &copy); ix++)
28251 TREE_PURPOSE (copy) = parsed_arg;
28254 pop_defarg_context ();
28256 /* Make sure no default arg is missing. */
28257 check_default_args (fn);
28259 /* Restore the state of local_variables_forbidden_p. */
28260 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28262 /* Restore the queue. */
28263 pop_unparsed_function_queues (parser);
28266 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28268 sizeof ... ( identifier )
28270 where the 'sizeof' token has already been consumed. */
28272 static tree
28273 cp_parser_sizeof_pack (cp_parser *parser)
28275 /* Consume the `...'. */
28276 cp_lexer_consume_token (parser->lexer);
28277 maybe_warn_variadic_templates ();
28279 matching_parens parens;
28280 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28281 if (paren)
28282 parens.consume_open (parser);
28283 else
28284 permerror (cp_lexer_peek_token (parser->lexer)->location,
28285 "%<sizeof...%> argument must be surrounded by parentheses");
28287 cp_token *token = cp_lexer_peek_token (parser->lexer);
28288 tree name = cp_parser_identifier (parser);
28289 if (name == error_mark_node)
28290 return error_mark_node;
28291 /* The name is not qualified. */
28292 parser->scope = NULL_TREE;
28293 parser->qualifying_scope = NULL_TREE;
28294 parser->object_scope = NULL_TREE;
28295 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28296 if (expr == error_mark_node)
28297 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28298 token->location);
28299 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28300 expr = TREE_TYPE (expr);
28301 else if (TREE_CODE (expr) == CONST_DECL)
28302 expr = DECL_INITIAL (expr);
28303 expr = make_pack_expansion (expr);
28304 PACK_EXPANSION_SIZEOF_P (expr) = true;
28306 if (paren)
28307 parens.require_close (parser);
28309 return expr;
28312 /* Parse the operand of `sizeof' (or a similar operator). Returns
28313 either a TYPE or an expression, depending on the form of the
28314 input. The KEYWORD indicates which kind of expression we have
28315 encountered. */
28317 static tree
28318 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28320 tree expr = NULL_TREE;
28321 const char *saved_message;
28322 char *tmp;
28323 bool saved_integral_constant_expression_p;
28324 bool saved_non_integral_constant_expression_p;
28326 /* If it's a `...', then we are computing the length of a parameter
28327 pack. */
28328 if (keyword == RID_SIZEOF
28329 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28330 return cp_parser_sizeof_pack (parser);
28332 /* Types cannot be defined in a `sizeof' expression. Save away the
28333 old message. */
28334 saved_message = parser->type_definition_forbidden_message;
28335 /* And create the new one. */
28336 tmp = concat ("types may not be defined in %<",
28337 IDENTIFIER_POINTER (ridpointers[keyword]),
28338 "%> expressions", NULL);
28339 parser->type_definition_forbidden_message = tmp;
28341 /* The restrictions on constant-expressions do not apply inside
28342 sizeof expressions. */
28343 saved_integral_constant_expression_p
28344 = parser->integral_constant_expression_p;
28345 saved_non_integral_constant_expression_p
28346 = parser->non_integral_constant_expression_p;
28347 parser->integral_constant_expression_p = false;
28349 /* Do not actually evaluate the expression. */
28350 ++cp_unevaluated_operand;
28351 ++c_inhibit_evaluation_warnings;
28352 /* If it's a `(', then we might be looking at the type-id
28353 construction. */
28354 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28356 tree type = NULL_TREE;
28358 /* We can't be sure yet whether we're looking at a type-id or an
28359 expression. */
28360 cp_parser_parse_tentatively (parser);
28362 matching_parens parens;
28363 parens.consume_open (parser);
28365 /* Note: as a GNU Extension, compound literals are considered
28366 postfix-expressions as they are in C99, so they are valid
28367 arguments to sizeof. See comment in cp_parser_cast_expression
28368 for details. */
28369 if (cp_parser_compound_literal_p (parser))
28370 cp_parser_simulate_error (parser);
28371 else
28373 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28374 parser->in_type_id_in_expr_p = true;
28375 /* Look for the type-id. */
28376 type = cp_parser_type_id (parser);
28377 /* Look for the closing `)'. */
28378 parens.require_close (parser);
28379 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28382 /* If all went well, then we're done. */
28383 if (cp_parser_parse_definitely (parser))
28384 expr = type;
28387 /* If the type-id production did not work out, then we must be
28388 looking at the unary-expression production. */
28389 if (!expr)
28390 expr = cp_parser_unary_expression (parser);
28392 /* Go back to evaluating expressions. */
28393 --cp_unevaluated_operand;
28394 --c_inhibit_evaluation_warnings;
28396 /* Free the message we created. */
28397 free (tmp);
28398 /* And restore the old one. */
28399 parser->type_definition_forbidden_message = saved_message;
28400 parser->integral_constant_expression_p
28401 = saved_integral_constant_expression_p;
28402 parser->non_integral_constant_expression_p
28403 = saved_non_integral_constant_expression_p;
28405 return expr;
28408 /* If the current declaration has no declarator, return true. */
28410 static bool
28411 cp_parser_declares_only_class_p (cp_parser *parser)
28413 /* If the next token is a `;' or a `,' then there is no
28414 declarator. */
28415 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28416 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28419 /* Update the DECL_SPECS to reflect the storage class indicated by
28420 KEYWORD. */
28422 static void
28423 cp_parser_set_storage_class (cp_parser *parser,
28424 cp_decl_specifier_seq *decl_specs,
28425 enum rid keyword,
28426 cp_token *token)
28428 cp_storage_class storage_class;
28430 if (parser->in_unbraced_linkage_specification_p)
28432 error_at (token->location, "invalid use of %qD in linkage specification",
28433 ridpointers[keyword]);
28434 return;
28436 else if (decl_specs->storage_class != sc_none)
28438 decl_specs->conflicting_specifiers_p = true;
28439 return;
28442 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28443 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28444 && decl_specs->gnu_thread_keyword_p)
28446 pedwarn (decl_specs->locations[ds_thread], 0,
28447 "%<__thread%> before %qD", ridpointers[keyword]);
28450 switch (keyword)
28452 case RID_AUTO:
28453 storage_class = sc_auto;
28454 break;
28455 case RID_REGISTER:
28456 storage_class = sc_register;
28457 break;
28458 case RID_STATIC:
28459 storage_class = sc_static;
28460 break;
28461 case RID_EXTERN:
28462 storage_class = sc_extern;
28463 break;
28464 case RID_MUTABLE:
28465 storage_class = sc_mutable;
28466 break;
28467 default:
28468 gcc_unreachable ();
28470 decl_specs->storage_class = storage_class;
28471 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28473 /* A storage class specifier cannot be applied alongside a typedef
28474 specifier. If there is a typedef specifier present then set
28475 conflicting_specifiers_p which will trigger an error later
28476 on in grokdeclarator. */
28477 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28478 decl_specs->conflicting_specifiers_p = true;
28481 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28482 is true, the type is a class or enum definition. */
28484 static void
28485 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28486 tree type_spec,
28487 cp_token *token,
28488 bool type_definition_p)
28490 decl_specs->any_specifiers_p = true;
28492 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28493 (with, for example, in "typedef int wchar_t;") we remember that
28494 this is what happened. In system headers, we ignore these
28495 declarations so that G++ can work with system headers that are not
28496 C++-safe. */
28497 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28498 && !type_definition_p
28499 && (type_spec == boolean_type_node
28500 || type_spec == char16_type_node
28501 || type_spec == char32_type_node
28502 || type_spec == wchar_type_node)
28503 && (decl_specs->type
28504 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28505 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28506 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28507 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28509 decl_specs->redefined_builtin_type = type_spec;
28510 set_and_check_decl_spec_loc (decl_specs,
28511 ds_redefined_builtin_type_spec,
28512 token);
28513 if (!decl_specs->type)
28515 decl_specs->type = type_spec;
28516 decl_specs->type_definition_p = false;
28517 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28520 else if (decl_specs->type)
28521 decl_specs->multiple_types_p = true;
28522 else
28524 decl_specs->type = type_spec;
28525 decl_specs->type_definition_p = type_definition_p;
28526 decl_specs->redefined_builtin_type = NULL_TREE;
28527 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28531 /* True iff TOKEN is the GNU keyword __thread. */
28533 static bool
28534 token_is__thread (cp_token *token)
28536 gcc_assert (token->keyword == RID_THREAD);
28537 return id_equal (token->u.value, "__thread");
28540 /* Set the location for a declarator specifier and check if it is
28541 duplicated.
28543 DECL_SPECS is the sequence of declarator specifiers onto which to
28544 set the location.
28546 DS is the single declarator specifier to set which location is to
28547 be set onto the existing sequence of declarators.
28549 LOCATION is the location for the declarator specifier to
28550 consider. */
28552 static void
28553 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28554 cp_decl_spec ds, cp_token *token)
28556 gcc_assert (ds < ds_last);
28558 if (decl_specs == NULL)
28559 return;
28561 location_t location = token->location;
28563 if (decl_specs->locations[ds] == 0)
28565 decl_specs->locations[ds] = location;
28566 if (ds == ds_thread)
28567 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28569 else
28571 if (ds == ds_long)
28573 if (decl_specs->locations[ds_long_long] != 0)
28574 error_at (location,
28575 "%<long long long%> is too long for GCC");
28576 else
28578 decl_specs->locations[ds_long_long] = location;
28579 pedwarn_cxx98 (location,
28580 OPT_Wlong_long,
28581 "ISO C++ 1998 does not support %<long long%>");
28584 else if (ds == ds_thread)
28586 bool gnu = token_is__thread (token);
28587 gcc_rich_location richloc (location);
28588 if (gnu != decl_specs->gnu_thread_keyword_p)
28590 richloc.add_range (decl_specs->locations[ds_thread]);
28591 error_at (&richloc,
28592 "both %<__thread%> and %<thread_local%> specified");
28594 else
28596 richloc.add_fixit_remove ();
28597 error_at (&richloc, "duplicate %qD", token->u.value);
28600 else
28602 static const char *const decl_spec_names[] = {
28603 "signed",
28604 "unsigned",
28605 "short",
28606 "long",
28607 "const",
28608 "volatile",
28609 "restrict",
28610 "inline",
28611 "virtual",
28612 "explicit",
28613 "friend",
28614 "typedef",
28615 "using",
28616 "constexpr",
28617 "__complex"
28619 gcc_rich_location richloc (location);
28620 richloc.add_fixit_remove ();
28621 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28626 /* Return true iff the declarator specifier DS is present in the
28627 sequence of declarator specifiers DECL_SPECS. */
28629 bool
28630 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28631 cp_decl_spec ds)
28633 gcc_assert (ds < ds_last);
28635 if (decl_specs == NULL)
28636 return false;
28638 return decl_specs->locations[ds] != 0;
28641 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28642 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28644 static bool
28645 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28647 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28650 /* Issue an error message indicating that TOKEN_DESC was expected.
28651 If KEYWORD is true, it indicated this function is called by
28652 cp_parser_require_keword and the required token can only be
28653 a indicated keyword.
28655 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28656 within any error as the location of an "opening" token matching
28657 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28658 RT_CLOSE_PAREN). */
28660 static void
28661 cp_parser_required_error (cp_parser *parser,
28662 required_token token_desc,
28663 bool keyword,
28664 location_t matching_location)
28666 if (cp_parser_simulate_error (parser))
28667 return;
28669 const char *gmsgid = NULL;
28670 switch (token_desc)
28672 case RT_NEW:
28673 gmsgid = G_("expected %<new%>");
28674 break;
28675 case RT_DELETE:
28676 gmsgid = G_("expected %<delete%>");
28677 break;
28678 case RT_RETURN:
28679 gmsgid = G_("expected %<return%>");
28680 break;
28681 case RT_WHILE:
28682 gmsgid = G_("expected %<while%>");
28683 break;
28684 case RT_EXTERN:
28685 gmsgid = G_("expected %<extern%>");
28686 break;
28687 case RT_STATIC_ASSERT:
28688 gmsgid = G_("expected %<static_assert%>");
28689 break;
28690 case RT_DECLTYPE:
28691 gmsgid = G_("expected %<decltype%>");
28692 break;
28693 case RT_OPERATOR:
28694 gmsgid = G_("expected %<operator%>");
28695 break;
28696 case RT_CLASS:
28697 gmsgid = G_("expected %<class%>");
28698 break;
28699 case RT_TEMPLATE:
28700 gmsgid = G_("expected %<template%>");
28701 break;
28702 case RT_NAMESPACE:
28703 gmsgid = G_("expected %<namespace%>");
28704 break;
28705 case RT_USING:
28706 gmsgid = G_("expected %<using%>");
28707 break;
28708 case RT_ASM:
28709 gmsgid = G_("expected %<asm%>");
28710 break;
28711 case RT_TRY:
28712 gmsgid = G_("expected %<try%>");
28713 break;
28714 case RT_CATCH:
28715 gmsgid = G_("expected %<catch%>");
28716 break;
28717 case RT_THROW:
28718 gmsgid = G_("expected %<throw%>");
28719 break;
28720 case RT_LABEL:
28721 gmsgid = G_("expected %<__label__%>");
28722 break;
28723 case RT_AT_TRY:
28724 gmsgid = G_("expected %<@try%>");
28725 break;
28726 case RT_AT_SYNCHRONIZED:
28727 gmsgid = G_("expected %<@synchronized%>");
28728 break;
28729 case RT_AT_THROW:
28730 gmsgid = G_("expected %<@throw%>");
28731 break;
28732 case RT_TRANSACTION_ATOMIC:
28733 gmsgid = G_("expected %<__transaction_atomic%>");
28734 break;
28735 case RT_TRANSACTION_RELAXED:
28736 gmsgid = G_("expected %<__transaction_relaxed%>");
28737 break;
28738 default:
28739 break;
28742 if (!gmsgid && !keyword)
28744 switch (token_desc)
28746 case RT_SEMICOLON:
28747 gmsgid = G_("expected %<;%>");
28748 break;
28749 case RT_OPEN_PAREN:
28750 gmsgid = G_("expected %<(%>");
28751 break;
28752 case RT_CLOSE_BRACE:
28753 gmsgid = G_("expected %<}%>");
28754 break;
28755 case RT_OPEN_BRACE:
28756 gmsgid = G_("expected %<{%>");
28757 break;
28758 case RT_CLOSE_SQUARE:
28759 gmsgid = G_("expected %<]%>");
28760 break;
28761 case RT_OPEN_SQUARE:
28762 gmsgid = G_("expected %<[%>");
28763 break;
28764 case RT_COMMA:
28765 gmsgid = G_("expected %<,%>");
28766 break;
28767 case RT_SCOPE:
28768 gmsgid = G_("expected %<::%>");
28769 break;
28770 case RT_LESS:
28771 gmsgid = G_("expected %<<%>");
28772 break;
28773 case RT_GREATER:
28774 gmsgid = G_("expected %<>%>");
28775 break;
28776 case RT_EQ:
28777 gmsgid = G_("expected %<=%>");
28778 break;
28779 case RT_ELLIPSIS:
28780 gmsgid = G_("expected %<...%>");
28781 break;
28782 case RT_MULT:
28783 gmsgid = G_("expected %<*%>");
28784 break;
28785 case RT_COMPL:
28786 gmsgid = G_("expected %<~%>");
28787 break;
28788 case RT_COLON:
28789 gmsgid = G_("expected %<:%>");
28790 break;
28791 case RT_COLON_SCOPE:
28792 gmsgid = G_("expected %<:%> or %<::%>");
28793 break;
28794 case RT_CLOSE_PAREN:
28795 gmsgid = G_("expected %<)%>");
28796 break;
28797 case RT_COMMA_CLOSE_PAREN:
28798 gmsgid = G_("expected %<,%> or %<)%>");
28799 break;
28800 case RT_PRAGMA_EOL:
28801 gmsgid = G_("expected end of line");
28802 break;
28803 case RT_NAME:
28804 gmsgid = G_("expected identifier");
28805 break;
28806 case RT_SELECT:
28807 gmsgid = G_("expected selection-statement");
28808 break;
28809 case RT_ITERATION:
28810 gmsgid = G_("expected iteration-statement");
28811 break;
28812 case RT_JUMP:
28813 gmsgid = G_("expected jump-statement");
28814 break;
28815 case RT_CLASS_KEY:
28816 gmsgid = G_("expected class-key");
28817 break;
28818 case RT_CLASS_TYPENAME_TEMPLATE:
28819 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28820 break;
28821 default:
28822 gcc_unreachable ();
28826 if (gmsgid)
28827 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28831 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28832 issue an error message indicating that TOKEN_DESC was expected.
28834 Returns the token consumed, if the token had the appropriate type.
28835 Otherwise, returns NULL.
28837 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28838 within any error as the location of an "opening" token matching
28839 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28840 RT_CLOSE_PAREN). */
28842 static cp_token *
28843 cp_parser_require (cp_parser* parser,
28844 enum cpp_ttype type,
28845 required_token token_desc,
28846 location_t matching_location)
28848 if (cp_lexer_next_token_is (parser->lexer, type))
28849 return cp_lexer_consume_token (parser->lexer);
28850 else
28852 /* Output the MESSAGE -- unless we're parsing tentatively. */
28853 if (!cp_parser_simulate_error (parser))
28854 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28855 matching_location);
28856 return NULL;
28860 /* An error message is produced if the next token is not '>'.
28861 All further tokens are skipped until the desired token is
28862 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28864 static void
28865 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28867 /* Current level of '< ... >'. */
28868 unsigned level = 0;
28869 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28870 unsigned nesting_depth = 0;
28872 /* Are we ready, yet? If not, issue error message. */
28873 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28874 return;
28876 /* Skip tokens until the desired token is found. */
28877 while (true)
28879 /* Peek at the next token. */
28880 switch (cp_lexer_peek_token (parser->lexer)->type)
28882 case CPP_LESS:
28883 if (!nesting_depth)
28884 ++level;
28885 break;
28887 case CPP_RSHIFT:
28888 if (cxx_dialect == cxx98)
28889 /* C++0x views the `>>' operator as two `>' tokens, but
28890 C++98 does not. */
28891 break;
28892 else if (!nesting_depth && level-- == 0)
28894 /* We've hit a `>>' where the first `>' closes the
28895 template argument list, and the second `>' is
28896 spurious. Just consume the `>>' and stop; we've
28897 already produced at least one error. */
28898 cp_lexer_consume_token (parser->lexer);
28899 return;
28901 /* Fall through for C++0x, so we handle the second `>' in
28902 the `>>'. */
28903 gcc_fallthrough ();
28905 case CPP_GREATER:
28906 if (!nesting_depth && level-- == 0)
28908 /* We've reached the token we want, consume it and stop. */
28909 cp_lexer_consume_token (parser->lexer);
28910 return;
28912 break;
28914 case CPP_OPEN_PAREN:
28915 case CPP_OPEN_SQUARE:
28916 ++nesting_depth;
28917 break;
28919 case CPP_CLOSE_PAREN:
28920 case CPP_CLOSE_SQUARE:
28921 if (nesting_depth-- == 0)
28922 return;
28923 break;
28925 case CPP_EOF:
28926 case CPP_PRAGMA_EOL:
28927 case CPP_SEMICOLON:
28928 case CPP_OPEN_BRACE:
28929 case CPP_CLOSE_BRACE:
28930 /* The '>' was probably forgotten, don't look further. */
28931 return;
28933 default:
28934 break;
28937 /* Consume this token. */
28938 cp_lexer_consume_token (parser->lexer);
28942 /* If the next token is the indicated keyword, consume it. Otherwise,
28943 issue an error message indicating that TOKEN_DESC was expected.
28945 Returns the token consumed, if the token had the appropriate type.
28946 Otherwise, returns NULL. */
28948 static cp_token *
28949 cp_parser_require_keyword (cp_parser* parser,
28950 enum rid keyword,
28951 required_token token_desc)
28953 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28955 if (token && token->keyword != keyword)
28957 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28958 UNKNOWN_LOCATION);
28959 return NULL;
28962 return token;
28965 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28966 function-definition. */
28968 static bool
28969 cp_parser_token_starts_function_definition_p (cp_token* token)
28971 return (/* An ordinary function-body begins with an `{'. */
28972 token->type == CPP_OPEN_BRACE
28973 /* A ctor-initializer begins with a `:'. */
28974 || token->type == CPP_COLON
28975 /* A function-try-block begins with `try'. */
28976 || token->keyword == RID_TRY
28977 /* A function-transaction-block begins with `__transaction_atomic'
28978 or `__transaction_relaxed'. */
28979 || token->keyword == RID_TRANSACTION_ATOMIC
28980 || token->keyword == RID_TRANSACTION_RELAXED
28981 /* The named return value extension begins with `return'. */
28982 || token->keyword == RID_RETURN);
28985 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28986 definition. */
28988 static bool
28989 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28991 cp_token *token;
28993 token = cp_lexer_peek_token (parser->lexer);
28994 return (token->type == CPP_OPEN_BRACE
28995 || (token->type == CPP_COLON
28996 && !parser->colon_doesnt_start_class_def_p));
28999 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29000 C++0x) ending a template-argument. */
29002 static bool
29003 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
29005 cp_token *token;
29007 token = cp_lexer_peek_token (parser->lexer);
29008 return (token->type == CPP_COMMA
29009 || token->type == CPP_GREATER
29010 || token->type == CPP_ELLIPSIS
29011 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
29014 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29015 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29017 static bool
29018 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
29019 size_t n)
29021 cp_token *token;
29023 token = cp_lexer_peek_nth_token (parser->lexer, n);
29024 if (token->type == CPP_LESS)
29025 return true;
29026 /* Check for the sequence `<::' in the original code. It would be lexed as
29027 `[:', where `[' is a digraph, and there is no whitespace before
29028 `:'. */
29029 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
29031 cp_token *token2;
29032 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
29033 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
29034 return true;
29036 return false;
29039 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29040 or none_type otherwise. */
29042 static enum tag_types
29043 cp_parser_token_is_class_key (cp_token* token)
29045 switch (token->keyword)
29047 case RID_CLASS:
29048 return class_type;
29049 case RID_STRUCT:
29050 return record_type;
29051 case RID_UNION:
29052 return union_type;
29054 default:
29055 return none_type;
29059 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29060 or none_type otherwise or if the token is null. */
29062 static enum tag_types
29063 cp_parser_token_is_type_parameter_key (cp_token* token)
29065 if (!token)
29066 return none_type;
29068 switch (token->keyword)
29070 case RID_CLASS:
29071 return class_type;
29072 case RID_TYPENAME:
29073 return typename_type;
29075 default:
29076 return none_type;
29080 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29082 static void
29083 cp_parser_check_class_key (enum tag_types class_key, tree type)
29085 if (type == error_mark_node)
29086 return;
29087 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29089 if (permerror (input_location, "%qs tag used in naming %q#T",
29090 class_key == union_type ? "union"
29091 : class_key == record_type ? "struct" : "class",
29092 type))
29093 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29094 "%q#T was previously declared here", type);
29098 /* Issue an error message if DECL is redeclared with different
29099 access than its original declaration [class.access.spec/3].
29100 This applies to nested classes, nested class templates and
29101 enumerations [class.mem/1]. */
29103 static void
29104 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29106 if (!decl
29107 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29108 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29109 return;
29111 if ((TREE_PRIVATE (decl)
29112 != (current_access_specifier == access_private_node))
29113 || (TREE_PROTECTED (decl)
29114 != (current_access_specifier == access_protected_node)))
29115 error_at (location, "%qD redeclared with different access", decl);
29118 /* Look for the `template' keyword, as a syntactic disambiguator.
29119 Return TRUE iff it is present, in which case it will be
29120 consumed. */
29122 static bool
29123 cp_parser_optional_template_keyword (cp_parser *parser)
29125 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29127 /* In C++98 the `template' keyword can only be used within templates;
29128 outside templates the parser can always figure out what is a
29129 template and what is not. In C++11, per the resolution of DR 468,
29130 `template' is allowed in cases where it is not strictly necessary. */
29131 if (!processing_template_decl
29132 && pedantic && cxx_dialect == cxx98)
29134 cp_token *token = cp_lexer_peek_token (parser->lexer);
29135 pedwarn (token->location, OPT_Wpedantic,
29136 "in C++98 %<template%> (as a disambiguator) is only "
29137 "allowed within templates");
29138 /* If this part of the token stream is rescanned, the same
29139 error message would be generated. So, we purge the token
29140 from the stream. */
29141 cp_lexer_purge_token (parser->lexer);
29142 return false;
29144 else
29146 /* Consume the `template' keyword. */
29147 cp_lexer_consume_token (parser->lexer);
29148 return true;
29151 return false;
29154 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29155 set PARSER->SCOPE, and perform other related actions. */
29157 static void
29158 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29160 struct tree_check *check_value;
29162 /* Get the stored value. */
29163 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29164 /* Set the scope from the stored value. */
29165 parser->scope = saved_checks_value (check_value);
29166 parser->qualifying_scope = check_value->qualifying_scope;
29167 parser->object_scope = NULL_TREE;
29170 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29171 encounter the end of a block before what we were looking for. */
29173 static bool
29174 cp_parser_cache_group (cp_parser *parser,
29175 enum cpp_ttype end,
29176 unsigned depth)
29178 while (true)
29180 cp_token *token = cp_lexer_peek_token (parser->lexer);
29182 /* Abort a parenthesized expression if we encounter a semicolon. */
29183 if ((end == CPP_CLOSE_PAREN || depth == 0)
29184 && token->type == CPP_SEMICOLON)
29185 return true;
29186 /* If we've reached the end of the file, stop. */
29187 if (token->type == CPP_EOF
29188 || (end != CPP_PRAGMA_EOL
29189 && token->type == CPP_PRAGMA_EOL))
29190 return true;
29191 if (token->type == CPP_CLOSE_BRACE && depth == 0)
29192 /* We've hit the end of an enclosing block, so there's been some
29193 kind of syntax error. */
29194 return true;
29196 /* Consume the token. */
29197 cp_lexer_consume_token (parser->lexer);
29198 /* See if it starts a new group. */
29199 if (token->type == CPP_OPEN_BRACE)
29201 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29202 /* In theory this should probably check end == '}', but
29203 cp_parser_save_member_function_body needs it to exit
29204 after either '}' or ')' when called with ')'. */
29205 if (depth == 0)
29206 return false;
29208 else if (token->type == CPP_OPEN_PAREN)
29210 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29211 if (depth == 0 && end == CPP_CLOSE_PAREN)
29212 return false;
29214 else if (token->type == CPP_PRAGMA)
29215 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29216 else if (token->type == end)
29217 return false;
29221 /* Like above, for caching a default argument or NSDMI. Both of these are
29222 terminated by a non-nested comma, but it can be unclear whether or not a
29223 comma is nested in a template argument list unless we do more parsing.
29224 In order to handle this ambiguity, when we encounter a ',' after a '<'
29225 we try to parse what follows as a parameter-declaration-list (in the
29226 case of a default argument) or a member-declarator (in the case of an
29227 NSDMI). If that succeeds, then we stop caching. */
29229 static tree
29230 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29232 unsigned depth = 0;
29233 int maybe_template_id = 0;
29234 cp_token *first_token;
29235 cp_token *token;
29236 tree default_argument;
29238 /* Add tokens until we have processed the entire default
29239 argument. We add the range [first_token, token). */
29240 first_token = cp_lexer_peek_token (parser->lexer);
29241 if (first_token->type == CPP_OPEN_BRACE)
29243 /* For list-initialization, this is straightforward. */
29244 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29245 token = cp_lexer_peek_token (parser->lexer);
29247 else while (true)
29249 bool done = false;
29251 /* Peek at the next token. */
29252 token = cp_lexer_peek_token (parser->lexer);
29253 /* What we do depends on what token we have. */
29254 switch (token->type)
29256 /* In valid code, a default argument must be
29257 immediately followed by a `,' `)', or `...'. */
29258 case CPP_COMMA:
29259 if (depth == 0 && maybe_template_id)
29261 /* If we've seen a '<', we might be in a
29262 template-argument-list. Until Core issue 325 is
29263 resolved, we don't know how this situation ought
29264 to be handled, so try to DTRT. We check whether
29265 what comes after the comma is a valid parameter
29266 declaration list. If it is, then the comma ends
29267 the default argument; otherwise the default
29268 argument continues. */
29269 bool error = false;
29270 cp_token *peek;
29272 /* Set ITALP so cp_parser_parameter_declaration_list
29273 doesn't decide to commit to this parse. */
29274 bool saved_italp = parser->in_template_argument_list_p;
29275 parser->in_template_argument_list_p = true;
29277 cp_parser_parse_tentatively (parser);
29279 if (nsdmi)
29281 /* Parse declarators until we reach a non-comma or
29282 somthing that cannot be an initializer.
29283 Just checking whether we're looking at a single
29284 declarator is insufficient. Consider:
29285 int var = tuple<T,U>::x;
29286 The template parameter 'U' looks exactly like a
29287 declarator. */
29290 int ctor_dtor_or_conv_p;
29291 cp_lexer_consume_token (parser->lexer);
29292 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29293 &ctor_dtor_or_conv_p,
29294 /*parenthesized_p=*/NULL,
29295 /*member_p=*/true,
29296 /*friend_p=*/false);
29297 peek = cp_lexer_peek_token (parser->lexer);
29298 if (cp_parser_error_occurred (parser))
29299 break;
29301 while (peek->type == CPP_COMMA);
29302 /* If we met an '=' or ';' then the original comma
29303 was the end of the NSDMI. Otherwise assume
29304 we're still in the NSDMI. */
29305 error = (peek->type != CPP_EQ
29306 && peek->type != CPP_SEMICOLON);
29308 else
29310 cp_lexer_consume_token (parser->lexer);
29311 begin_scope (sk_function_parms, NULL_TREE);
29312 if (cp_parser_parameter_declaration_list (parser)
29313 == error_mark_node)
29314 error = true;
29315 pop_bindings_and_leave_scope ();
29317 if (!cp_parser_error_occurred (parser) && !error)
29318 done = true;
29319 cp_parser_abort_tentative_parse (parser);
29321 parser->in_template_argument_list_p = saved_italp;
29322 break;
29324 /* FALLTHRU */
29325 case CPP_CLOSE_PAREN:
29326 case CPP_ELLIPSIS:
29327 /* If we run into a non-nested `;', `}', or `]',
29328 then the code is invalid -- but the default
29329 argument is certainly over. */
29330 case CPP_SEMICOLON:
29331 case CPP_CLOSE_BRACE:
29332 case CPP_CLOSE_SQUARE:
29333 if (depth == 0
29334 /* Handle correctly int n = sizeof ... ( p ); */
29335 && token->type != CPP_ELLIPSIS)
29336 done = true;
29337 /* Update DEPTH, if necessary. */
29338 else if (token->type == CPP_CLOSE_PAREN
29339 || token->type == CPP_CLOSE_BRACE
29340 || token->type == CPP_CLOSE_SQUARE)
29341 --depth;
29342 break;
29344 case CPP_OPEN_PAREN:
29345 case CPP_OPEN_SQUARE:
29346 case CPP_OPEN_BRACE:
29347 ++depth;
29348 break;
29350 case CPP_LESS:
29351 if (depth == 0)
29352 /* This might be the comparison operator, or it might
29353 start a template argument list. */
29354 ++maybe_template_id;
29355 break;
29357 case CPP_RSHIFT:
29358 if (cxx_dialect == cxx98)
29359 break;
29360 /* Fall through for C++0x, which treats the `>>'
29361 operator like two `>' tokens in certain
29362 cases. */
29363 gcc_fallthrough ();
29365 case CPP_GREATER:
29366 if (depth == 0)
29368 /* This might be an operator, or it might close a
29369 template argument list. But if a previous '<'
29370 started a template argument list, this will have
29371 closed it, so we can't be in one anymore. */
29372 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29373 if (maybe_template_id < 0)
29374 maybe_template_id = 0;
29376 break;
29378 /* If we run out of tokens, issue an error message. */
29379 case CPP_EOF:
29380 case CPP_PRAGMA_EOL:
29381 error_at (token->location, "file ends in default argument");
29382 return error_mark_node;
29384 case CPP_NAME:
29385 case CPP_SCOPE:
29386 /* In these cases, we should look for template-ids.
29387 For example, if the default argument is
29388 `X<int, double>()', we need to do name lookup to
29389 figure out whether or not `X' is a template; if
29390 so, the `,' does not end the default argument.
29392 That is not yet done. */
29393 break;
29395 default:
29396 break;
29399 /* If we've reached the end, stop. */
29400 if (done)
29401 break;
29403 /* Add the token to the token block. */
29404 token = cp_lexer_consume_token (parser->lexer);
29407 /* Create a DEFAULT_ARG to represent the unparsed default
29408 argument. */
29409 default_argument = make_node (DEFAULT_ARG);
29410 DEFARG_TOKENS (default_argument)
29411 = cp_token_cache_new (first_token, token);
29412 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29414 return default_argument;
29417 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29419 location_t
29420 defarg_location (tree default_argument)
29422 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29423 location_t start = tokens->first->location;
29424 location_t end = tokens->last->location;
29425 return make_location (start, start, end);
29428 /* Begin parsing tentatively. We always save tokens while parsing
29429 tentatively so that if the tentative parsing fails we can restore the
29430 tokens. */
29432 static void
29433 cp_parser_parse_tentatively (cp_parser* parser)
29435 /* Enter a new parsing context. */
29436 parser->context = cp_parser_context_new (parser->context);
29437 /* Begin saving tokens. */
29438 cp_lexer_save_tokens (parser->lexer);
29439 /* In order to avoid repetitive access control error messages,
29440 access checks are queued up until we are no longer parsing
29441 tentatively. */
29442 push_deferring_access_checks (dk_deferred);
29445 /* Commit to the currently active tentative parse. */
29447 static void
29448 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29450 cp_parser_context *context;
29451 cp_lexer *lexer;
29453 /* Mark all of the levels as committed. */
29454 lexer = parser->lexer;
29455 for (context = parser->context; context->next; context = context->next)
29457 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29458 break;
29459 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29460 while (!cp_lexer_saving_tokens (lexer))
29461 lexer = lexer->next;
29462 cp_lexer_commit_tokens (lexer);
29466 /* Commit to the topmost currently active tentative parse.
29468 Note that this function shouldn't be called when there are
29469 irreversible side-effects while in a tentative state. For
29470 example, we shouldn't create a permanent entry in the symbol
29471 table, or issue an error message that might not apply if the
29472 tentative parse is aborted. */
29474 static void
29475 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29477 cp_parser_context *context = parser->context;
29478 cp_lexer *lexer = parser->lexer;
29480 if (context)
29482 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29483 return;
29484 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29486 while (!cp_lexer_saving_tokens (lexer))
29487 lexer = lexer->next;
29488 cp_lexer_commit_tokens (lexer);
29492 /* Abort the currently active tentative parse. All consumed tokens
29493 will be rolled back, and no diagnostics will be issued. */
29495 static void
29496 cp_parser_abort_tentative_parse (cp_parser* parser)
29498 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29499 || errorcount > 0);
29500 cp_parser_simulate_error (parser);
29501 /* Now, pretend that we want to see if the construct was
29502 successfully parsed. */
29503 cp_parser_parse_definitely (parser);
29506 /* Stop parsing tentatively. If a parse error has occurred, restore the
29507 token stream. Otherwise, commit to the tokens we have consumed.
29508 Returns true if no error occurred; false otherwise. */
29510 static bool
29511 cp_parser_parse_definitely (cp_parser* parser)
29513 bool error_occurred;
29514 cp_parser_context *context;
29516 /* Remember whether or not an error occurred, since we are about to
29517 destroy that information. */
29518 error_occurred = cp_parser_error_occurred (parser);
29519 /* Remove the topmost context from the stack. */
29520 context = parser->context;
29521 parser->context = context->next;
29522 /* If no parse errors occurred, commit to the tentative parse. */
29523 if (!error_occurred)
29525 /* Commit to the tokens read tentatively, unless that was
29526 already done. */
29527 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29528 cp_lexer_commit_tokens (parser->lexer);
29530 pop_to_parent_deferring_access_checks ();
29532 /* Otherwise, if errors occurred, roll back our state so that things
29533 are just as they were before we began the tentative parse. */
29534 else
29536 cp_lexer_rollback_tokens (parser->lexer);
29537 pop_deferring_access_checks ();
29539 /* Add the context to the front of the free list. */
29540 context->next = cp_parser_context_free_list;
29541 cp_parser_context_free_list = context;
29543 return !error_occurred;
29546 /* Returns true if we are parsing tentatively and are not committed to
29547 this tentative parse. */
29549 static bool
29550 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29552 return (cp_parser_parsing_tentatively (parser)
29553 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29556 /* Returns nonzero iff an error has occurred during the most recent
29557 tentative parse. */
29559 static bool
29560 cp_parser_error_occurred (cp_parser* parser)
29562 return (cp_parser_parsing_tentatively (parser)
29563 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29566 /* Returns nonzero if GNU extensions are allowed. */
29568 static bool
29569 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29571 return parser->allow_gnu_extensions_p;
29574 /* Objective-C++ Productions */
29577 /* Parse an Objective-C expression, which feeds into a primary-expression
29578 above.
29580 objc-expression:
29581 objc-message-expression
29582 objc-string-literal
29583 objc-encode-expression
29584 objc-protocol-expression
29585 objc-selector-expression
29587 Returns a tree representation of the expression. */
29589 static cp_expr
29590 cp_parser_objc_expression (cp_parser* parser)
29592 /* Try to figure out what kind of declaration is present. */
29593 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29595 switch (kwd->type)
29597 case CPP_OPEN_SQUARE:
29598 return cp_parser_objc_message_expression (parser);
29600 case CPP_OBJC_STRING:
29601 kwd = cp_lexer_consume_token (parser->lexer);
29602 return objc_build_string_object (kwd->u.value);
29604 case CPP_KEYWORD:
29605 switch (kwd->keyword)
29607 case RID_AT_ENCODE:
29608 return cp_parser_objc_encode_expression (parser);
29610 case RID_AT_PROTOCOL:
29611 return cp_parser_objc_protocol_expression (parser);
29613 case RID_AT_SELECTOR:
29614 return cp_parser_objc_selector_expression (parser);
29616 default:
29617 break;
29619 /* FALLTHRU */
29620 default:
29621 error_at (kwd->location,
29622 "misplaced %<@%D%> Objective-C++ construct",
29623 kwd->u.value);
29624 cp_parser_skip_to_end_of_block_or_statement (parser);
29627 return error_mark_node;
29630 /* Parse an Objective-C message expression.
29632 objc-message-expression:
29633 [ objc-message-receiver objc-message-args ]
29635 Returns a representation of an Objective-C message. */
29637 static tree
29638 cp_parser_objc_message_expression (cp_parser* parser)
29640 tree receiver, messageargs;
29642 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29643 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29644 receiver = cp_parser_objc_message_receiver (parser);
29645 messageargs = cp_parser_objc_message_args (parser);
29646 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29647 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29649 tree result = objc_build_message_expr (receiver, messageargs);
29651 /* Construct a location e.g.
29652 [self func1:5]
29653 ^~~~~~~~~~~~~~
29654 ranging from the '[' to the ']', with the caret at the start. */
29655 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29656 protected_set_expr_location (result, combined_loc);
29658 return result;
29661 /* Parse an objc-message-receiver.
29663 objc-message-receiver:
29664 expression
29665 simple-type-specifier
29667 Returns a representation of the type or expression. */
29669 static tree
29670 cp_parser_objc_message_receiver (cp_parser* parser)
29672 tree rcv;
29674 /* An Objective-C message receiver may be either (1) a type
29675 or (2) an expression. */
29676 cp_parser_parse_tentatively (parser);
29677 rcv = cp_parser_expression (parser);
29679 /* If that worked out, fine. */
29680 if (cp_parser_parse_definitely (parser))
29681 return rcv;
29683 cp_parser_parse_tentatively (parser);
29684 rcv = cp_parser_simple_type_specifier (parser,
29685 /*decl_specs=*/NULL,
29686 CP_PARSER_FLAGS_NONE);
29688 if (cp_parser_parse_definitely (parser))
29689 return objc_get_class_reference (rcv);
29691 cp_parser_error (parser, "objective-c++ message receiver expected");
29692 return error_mark_node;
29695 /* Parse the arguments and selectors comprising an Objective-C message.
29697 objc-message-args:
29698 objc-selector
29699 objc-selector-args
29700 objc-selector-args , objc-comma-args
29702 objc-selector-args:
29703 objc-selector [opt] : assignment-expression
29704 objc-selector-args objc-selector [opt] : assignment-expression
29706 objc-comma-args:
29707 assignment-expression
29708 objc-comma-args , assignment-expression
29710 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29711 selector arguments and TREE_VALUE containing a list of comma
29712 arguments. */
29714 static tree
29715 cp_parser_objc_message_args (cp_parser* parser)
29717 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29718 bool maybe_unary_selector_p = true;
29719 cp_token *token = cp_lexer_peek_token (parser->lexer);
29721 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29723 tree selector = NULL_TREE, arg;
29725 if (token->type != CPP_COLON)
29726 selector = cp_parser_objc_selector (parser);
29728 /* Detect if we have a unary selector. */
29729 if (maybe_unary_selector_p
29730 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29731 return build_tree_list (selector, NULL_TREE);
29733 maybe_unary_selector_p = false;
29734 cp_parser_require (parser, CPP_COLON, RT_COLON);
29735 arg = cp_parser_assignment_expression (parser);
29737 sel_args
29738 = chainon (sel_args,
29739 build_tree_list (selector, arg));
29741 token = cp_lexer_peek_token (parser->lexer);
29744 /* Handle non-selector arguments, if any. */
29745 while (token->type == CPP_COMMA)
29747 tree arg;
29749 cp_lexer_consume_token (parser->lexer);
29750 arg = cp_parser_assignment_expression (parser);
29752 addl_args
29753 = chainon (addl_args,
29754 build_tree_list (NULL_TREE, arg));
29756 token = cp_lexer_peek_token (parser->lexer);
29759 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29761 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29762 return build_tree_list (error_mark_node, error_mark_node);
29765 return build_tree_list (sel_args, addl_args);
29768 /* Parse an Objective-C encode expression.
29770 objc-encode-expression:
29771 @encode objc-typename
29773 Returns an encoded representation of the type argument. */
29775 static cp_expr
29776 cp_parser_objc_encode_expression (cp_parser* parser)
29778 tree type;
29779 cp_token *token;
29780 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29782 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29783 matching_parens parens;
29784 parens.require_open (parser);
29785 token = cp_lexer_peek_token (parser->lexer);
29786 type = complete_type (cp_parser_type_id (parser));
29787 parens.require_close (parser);
29789 if (!type)
29791 error_at (token->location,
29792 "%<@encode%> must specify a type as an argument");
29793 return error_mark_node;
29796 /* This happens if we find @encode(T) (where T is a template
29797 typename or something dependent on a template typename) when
29798 parsing a template. In that case, we can't compile it
29799 immediately, but we rather create an AT_ENCODE_EXPR which will
29800 need to be instantiated when the template is used.
29802 if (dependent_type_p (type))
29804 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29805 TREE_READONLY (value) = 1;
29806 return value;
29810 /* Build a location of the form:
29811 @encode(int)
29812 ^~~~~~~~~~~~
29813 with caret==start at the @ token, finishing at the close paren. */
29814 location_t combined_loc
29815 = make_location (start_loc, start_loc,
29816 cp_lexer_previous_token (parser->lexer)->location);
29818 return cp_expr (objc_build_encode_expr (type), combined_loc);
29821 /* Parse an Objective-C @defs expression. */
29823 static tree
29824 cp_parser_objc_defs_expression (cp_parser *parser)
29826 tree name;
29828 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29829 matching_parens parens;
29830 parens.require_open (parser);
29831 name = cp_parser_identifier (parser);
29832 parens.require_close (parser);
29834 return objc_get_class_ivars (name);
29837 /* Parse an Objective-C protocol expression.
29839 objc-protocol-expression:
29840 @protocol ( identifier )
29842 Returns a representation of the protocol expression. */
29844 static tree
29845 cp_parser_objc_protocol_expression (cp_parser* parser)
29847 tree proto;
29848 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29850 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29851 matching_parens parens;
29852 parens.require_open (parser);
29853 proto = cp_parser_identifier (parser);
29854 parens.require_close (parser);
29856 /* Build a location of the form:
29857 @protocol(prot)
29858 ^~~~~~~~~~~~~~~
29859 with caret==start at the @ token, finishing at the close paren. */
29860 location_t combined_loc
29861 = make_location (start_loc, start_loc,
29862 cp_lexer_previous_token (parser->lexer)->location);
29863 tree result = objc_build_protocol_expr (proto);
29864 protected_set_expr_location (result, combined_loc);
29865 return result;
29868 /* Parse an Objective-C selector expression.
29870 objc-selector-expression:
29871 @selector ( objc-method-signature )
29873 objc-method-signature:
29874 objc-selector
29875 objc-selector-seq
29877 objc-selector-seq:
29878 objc-selector :
29879 objc-selector-seq objc-selector :
29881 Returns a representation of the method selector. */
29883 static tree
29884 cp_parser_objc_selector_expression (cp_parser* parser)
29886 tree sel_seq = NULL_TREE;
29887 bool maybe_unary_selector_p = true;
29888 cp_token *token;
29889 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29891 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29892 matching_parens parens;
29893 parens.require_open (parser);
29894 token = cp_lexer_peek_token (parser->lexer);
29896 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29897 || token->type == CPP_SCOPE)
29899 tree selector = NULL_TREE;
29901 if (token->type != CPP_COLON
29902 || token->type == CPP_SCOPE)
29903 selector = cp_parser_objc_selector (parser);
29905 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29906 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29908 /* Detect if we have a unary selector. */
29909 if (maybe_unary_selector_p)
29911 sel_seq = selector;
29912 goto finish_selector;
29914 else
29916 cp_parser_error (parser, "expected %<:%>");
29919 maybe_unary_selector_p = false;
29920 token = cp_lexer_consume_token (parser->lexer);
29922 if (token->type == CPP_SCOPE)
29924 sel_seq
29925 = chainon (sel_seq,
29926 build_tree_list (selector, NULL_TREE));
29927 sel_seq
29928 = chainon (sel_seq,
29929 build_tree_list (NULL_TREE, NULL_TREE));
29931 else
29932 sel_seq
29933 = chainon (sel_seq,
29934 build_tree_list (selector, NULL_TREE));
29936 token = cp_lexer_peek_token (parser->lexer);
29939 finish_selector:
29940 parens.require_close (parser);
29943 /* Build a location of the form:
29944 @selector(func)
29945 ^~~~~~~~~~~~~~~
29946 with caret==start at the @ token, finishing at the close paren. */
29947 location_t combined_loc
29948 = make_location (loc, loc,
29949 cp_lexer_previous_token (parser->lexer)->location);
29950 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29951 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29952 protected_set_expr_location (result, combined_loc);
29953 return result;
29956 /* Parse a list of identifiers.
29958 objc-identifier-list:
29959 identifier
29960 objc-identifier-list , identifier
29962 Returns a TREE_LIST of identifier nodes. */
29964 static tree
29965 cp_parser_objc_identifier_list (cp_parser* parser)
29967 tree identifier;
29968 tree list;
29969 cp_token *sep;
29971 identifier = cp_parser_identifier (parser);
29972 if (identifier == error_mark_node)
29973 return error_mark_node;
29975 list = build_tree_list (NULL_TREE, identifier);
29976 sep = cp_lexer_peek_token (parser->lexer);
29978 while (sep->type == CPP_COMMA)
29980 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29981 identifier = cp_parser_identifier (parser);
29982 if (identifier == error_mark_node)
29983 return list;
29985 list = chainon (list, build_tree_list (NULL_TREE,
29986 identifier));
29987 sep = cp_lexer_peek_token (parser->lexer);
29990 return list;
29993 /* Parse an Objective-C alias declaration.
29995 objc-alias-declaration:
29996 @compatibility_alias identifier identifier ;
29998 This function registers the alias mapping with the Objective-C front end.
29999 It returns nothing. */
30001 static void
30002 cp_parser_objc_alias_declaration (cp_parser* parser)
30004 tree alias, orig;
30006 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
30007 alias = cp_parser_identifier (parser);
30008 orig = cp_parser_identifier (parser);
30009 objc_declare_alias (alias, orig);
30010 cp_parser_consume_semicolon_at_end_of_statement (parser);
30013 /* Parse an Objective-C class forward-declaration.
30015 objc-class-declaration:
30016 @class objc-identifier-list ;
30018 The function registers the forward declarations with the Objective-C
30019 front end. It returns nothing. */
30021 static void
30022 cp_parser_objc_class_declaration (cp_parser* parser)
30024 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
30025 while (true)
30027 tree id;
30029 id = cp_parser_identifier (parser);
30030 if (id == error_mark_node)
30031 break;
30033 objc_declare_class (id);
30035 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30036 cp_lexer_consume_token (parser->lexer);
30037 else
30038 break;
30040 cp_parser_consume_semicolon_at_end_of_statement (parser);
30043 /* Parse a list of Objective-C protocol references.
30045 objc-protocol-refs-opt:
30046 objc-protocol-refs [opt]
30048 objc-protocol-refs:
30049 < objc-identifier-list >
30051 Returns a TREE_LIST of identifiers, if any. */
30053 static tree
30054 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30056 tree protorefs = NULL_TREE;
30058 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30060 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30061 protorefs = cp_parser_objc_identifier_list (parser);
30062 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30065 return protorefs;
30068 /* Parse a Objective-C visibility specification. */
30070 static void
30071 cp_parser_objc_visibility_spec (cp_parser* parser)
30073 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30075 switch (vis->keyword)
30077 case RID_AT_PRIVATE:
30078 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30079 break;
30080 case RID_AT_PROTECTED:
30081 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30082 break;
30083 case RID_AT_PUBLIC:
30084 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30085 break;
30086 case RID_AT_PACKAGE:
30087 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30088 break;
30089 default:
30090 return;
30093 /* Eat '@private'/'@protected'/'@public'. */
30094 cp_lexer_consume_token (parser->lexer);
30097 /* Parse an Objective-C method type. Return 'true' if it is a class
30098 (+) method, and 'false' if it is an instance (-) method. */
30100 static inline bool
30101 cp_parser_objc_method_type (cp_parser* parser)
30103 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30104 return true;
30105 else
30106 return false;
30109 /* Parse an Objective-C protocol qualifier. */
30111 static tree
30112 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30114 tree quals = NULL_TREE, node;
30115 cp_token *token = cp_lexer_peek_token (parser->lexer);
30117 node = token->u.value;
30119 while (node && identifier_p (node)
30120 && (node == ridpointers [(int) RID_IN]
30121 || node == ridpointers [(int) RID_OUT]
30122 || node == ridpointers [(int) RID_INOUT]
30123 || node == ridpointers [(int) RID_BYCOPY]
30124 || node == ridpointers [(int) RID_BYREF]
30125 || node == ridpointers [(int) RID_ONEWAY]))
30127 quals = tree_cons (NULL_TREE, node, quals);
30128 cp_lexer_consume_token (parser->lexer);
30129 token = cp_lexer_peek_token (parser->lexer);
30130 node = token->u.value;
30133 return quals;
30136 /* Parse an Objective-C typename. */
30138 static tree
30139 cp_parser_objc_typename (cp_parser* parser)
30141 tree type_name = NULL_TREE;
30143 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30145 tree proto_quals, cp_type = NULL_TREE;
30147 matching_parens parens;
30148 parens.consume_open (parser); /* Eat '('. */
30149 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30151 /* An ObjC type name may consist of just protocol qualifiers, in which
30152 case the type shall default to 'id'. */
30153 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30155 cp_type = cp_parser_type_id (parser);
30157 /* If the type could not be parsed, an error has already
30158 been produced. For error recovery, behave as if it had
30159 not been specified, which will use the default type
30160 'id'. */
30161 if (cp_type == error_mark_node)
30163 cp_type = NULL_TREE;
30164 /* We need to skip to the closing parenthesis as
30165 cp_parser_type_id() does not seem to do it for
30166 us. */
30167 cp_parser_skip_to_closing_parenthesis (parser,
30168 /*recovering=*/true,
30169 /*or_comma=*/false,
30170 /*consume_paren=*/false);
30174 parens.require_close (parser);
30175 type_name = build_tree_list (proto_quals, cp_type);
30178 return type_name;
30181 /* Check to see if TYPE refers to an Objective-C selector name. */
30183 static bool
30184 cp_parser_objc_selector_p (enum cpp_ttype type)
30186 return (type == CPP_NAME || type == CPP_KEYWORD
30187 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30188 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30189 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30190 || type == CPP_XOR || type == CPP_XOR_EQ);
30193 /* Parse an Objective-C selector. */
30195 static tree
30196 cp_parser_objc_selector (cp_parser* parser)
30198 cp_token *token = cp_lexer_consume_token (parser->lexer);
30200 if (!cp_parser_objc_selector_p (token->type))
30202 error_at (token->location, "invalid Objective-C++ selector name");
30203 return error_mark_node;
30206 /* C++ operator names are allowed to appear in ObjC selectors. */
30207 switch (token->type)
30209 case CPP_AND_AND: return get_identifier ("and");
30210 case CPP_AND_EQ: return get_identifier ("and_eq");
30211 case CPP_AND: return get_identifier ("bitand");
30212 case CPP_OR: return get_identifier ("bitor");
30213 case CPP_COMPL: return get_identifier ("compl");
30214 case CPP_NOT: return get_identifier ("not");
30215 case CPP_NOT_EQ: return get_identifier ("not_eq");
30216 case CPP_OR_OR: return get_identifier ("or");
30217 case CPP_OR_EQ: return get_identifier ("or_eq");
30218 case CPP_XOR: return get_identifier ("xor");
30219 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30220 default: return token->u.value;
30224 /* Parse an Objective-C params list. */
30226 static tree
30227 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30229 tree params = NULL_TREE;
30230 bool maybe_unary_selector_p = true;
30231 cp_token *token = cp_lexer_peek_token (parser->lexer);
30233 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30235 tree selector = NULL_TREE, type_name, identifier;
30236 tree parm_attr = NULL_TREE;
30238 if (token->keyword == RID_ATTRIBUTE)
30239 break;
30241 if (token->type != CPP_COLON)
30242 selector = cp_parser_objc_selector (parser);
30244 /* Detect if we have a unary selector. */
30245 if (maybe_unary_selector_p
30246 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30248 params = selector; /* Might be followed by attributes. */
30249 break;
30252 maybe_unary_selector_p = false;
30253 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30255 /* Something went quite wrong. There should be a colon
30256 here, but there is not. Stop parsing parameters. */
30257 break;
30259 type_name = cp_parser_objc_typename (parser);
30260 /* New ObjC allows attributes on parameters too. */
30261 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30262 parm_attr = cp_parser_attributes_opt (parser);
30263 identifier = cp_parser_identifier (parser);
30265 params
30266 = chainon (params,
30267 objc_build_keyword_decl (selector,
30268 type_name,
30269 identifier,
30270 parm_attr));
30272 token = cp_lexer_peek_token (parser->lexer);
30275 if (params == NULL_TREE)
30277 cp_parser_error (parser, "objective-c++ method declaration is expected");
30278 return error_mark_node;
30281 /* We allow tail attributes for the method. */
30282 if (token->keyword == RID_ATTRIBUTE)
30284 *attributes = cp_parser_attributes_opt (parser);
30285 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30286 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30287 return params;
30288 cp_parser_error (parser,
30289 "method attributes must be specified at the end");
30290 return error_mark_node;
30293 if (params == NULL_TREE)
30295 cp_parser_error (parser, "objective-c++ method declaration is expected");
30296 return error_mark_node;
30298 return params;
30301 /* Parse the non-keyword Objective-C params. */
30303 static tree
30304 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30305 tree* attributes)
30307 tree params = make_node (TREE_LIST);
30308 cp_token *token = cp_lexer_peek_token (parser->lexer);
30309 *ellipsisp = false; /* Initially, assume no ellipsis. */
30311 while (token->type == CPP_COMMA)
30313 cp_parameter_declarator *parmdecl;
30314 tree parm;
30316 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30317 token = cp_lexer_peek_token (parser->lexer);
30319 if (token->type == CPP_ELLIPSIS)
30321 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30322 *ellipsisp = true;
30323 token = cp_lexer_peek_token (parser->lexer);
30324 break;
30327 /* TODO: parse attributes for tail parameters. */
30328 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30329 parm = grokdeclarator (parmdecl->declarator,
30330 &parmdecl->decl_specifiers,
30331 PARM, /*initialized=*/0,
30332 /*attrlist=*/NULL);
30334 chainon (params, build_tree_list (NULL_TREE, parm));
30335 token = cp_lexer_peek_token (parser->lexer);
30338 /* We allow tail attributes for the method. */
30339 if (token->keyword == RID_ATTRIBUTE)
30341 if (*attributes == NULL_TREE)
30343 *attributes = cp_parser_attributes_opt (parser);
30344 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30345 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30346 return params;
30348 else
30349 /* We have an error, but parse the attributes, so that we can
30350 carry on. */
30351 *attributes = cp_parser_attributes_opt (parser);
30353 cp_parser_error (parser,
30354 "method attributes must be specified at the end");
30355 return error_mark_node;
30358 return params;
30361 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30363 static void
30364 cp_parser_objc_interstitial_code (cp_parser* parser)
30366 cp_token *token = cp_lexer_peek_token (parser->lexer);
30368 /* If the next token is `extern' and the following token is a string
30369 literal, then we have a linkage specification. */
30370 if (token->keyword == RID_EXTERN
30371 && cp_parser_is_pure_string_literal
30372 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30373 cp_parser_linkage_specification (parser);
30374 /* Handle #pragma, if any. */
30375 else if (token->type == CPP_PRAGMA)
30376 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30377 /* Allow stray semicolons. */
30378 else if (token->type == CPP_SEMICOLON)
30379 cp_lexer_consume_token (parser->lexer);
30380 /* Mark methods as optional or required, when building protocols. */
30381 else if (token->keyword == RID_AT_OPTIONAL)
30383 cp_lexer_consume_token (parser->lexer);
30384 objc_set_method_opt (true);
30386 else if (token->keyword == RID_AT_REQUIRED)
30388 cp_lexer_consume_token (parser->lexer);
30389 objc_set_method_opt (false);
30391 else if (token->keyword == RID_NAMESPACE)
30392 cp_parser_namespace_definition (parser);
30393 /* Other stray characters must generate errors. */
30394 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30396 cp_lexer_consume_token (parser->lexer);
30397 error ("stray %qs between Objective-C++ methods",
30398 token->type == CPP_OPEN_BRACE ? "{" : "}");
30400 /* Finally, try to parse a block-declaration, or a function-definition. */
30401 else
30402 cp_parser_block_declaration (parser, /*statement_p=*/false);
30405 /* Parse a method signature. */
30407 static tree
30408 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30410 tree rettype, kwdparms, optparms;
30411 bool ellipsis = false;
30412 bool is_class_method;
30414 is_class_method = cp_parser_objc_method_type (parser);
30415 rettype = cp_parser_objc_typename (parser);
30416 *attributes = NULL_TREE;
30417 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30418 if (kwdparms == error_mark_node)
30419 return error_mark_node;
30420 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30421 if (optparms == error_mark_node)
30422 return error_mark_node;
30424 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30427 static bool
30428 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30430 tree tattr;
30431 cp_lexer_save_tokens (parser->lexer);
30432 tattr = cp_parser_attributes_opt (parser);
30433 gcc_assert (tattr) ;
30435 /* If the attributes are followed by a method introducer, this is not allowed.
30436 Dump the attributes and flag the situation. */
30437 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30438 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30439 return true;
30441 /* Otherwise, the attributes introduce some interstitial code, possibly so
30442 rewind to allow that check. */
30443 cp_lexer_rollback_tokens (parser->lexer);
30444 return false;
30447 /* Parse an Objective-C method prototype list. */
30449 static void
30450 cp_parser_objc_method_prototype_list (cp_parser* parser)
30452 cp_token *token = cp_lexer_peek_token (parser->lexer);
30454 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30456 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30458 tree attributes, sig;
30459 bool is_class_method;
30460 if (token->type == CPP_PLUS)
30461 is_class_method = true;
30462 else
30463 is_class_method = false;
30464 sig = cp_parser_objc_method_signature (parser, &attributes);
30465 if (sig == error_mark_node)
30467 cp_parser_skip_to_end_of_block_or_statement (parser);
30468 token = cp_lexer_peek_token (parser->lexer);
30469 continue;
30471 objc_add_method_declaration (is_class_method, sig, attributes);
30472 cp_parser_consume_semicolon_at_end_of_statement (parser);
30474 else if (token->keyword == RID_AT_PROPERTY)
30475 cp_parser_objc_at_property_declaration (parser);
30476 else if (token->keyword == RID_ATTRIBUTE
30477 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30478 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30479 OPT_Wattributes,
30480 "prefix attributes are ignored for methods");
30481 else
30482 /* Allow for interspersed non-ObjC++ code. */
30483 cp_parser_objc_interstitial_code (parser);
30485 token = cp_lexer_peek_token (parser->lexer);
30488 if (token->type != CPP_EOF)
30489 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30490 else
30491 cp_parser_error (parser, "expected %<@end%>");
30493 objc_finish_interface ();
30496 /* Parse an Objective-C method definition list. */
30498 static void
30499 cp_parser_objc_method_definition_list (cp_parser* parser)
30501 cp_token *token = cp_lexer_peek_token (parser->lexer);
30503 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30505 tree meth;
30507 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30509 cp_token *ptk;
30510 tree sig, attribute;
30511 bool is_class_method;
30512 if (token->type == CPP_PLUS)
30513 is_class_method = true;
30514 else
30515 is_class_method = false;
30516 push_deferring_access_checks (dk_deferred);
30517 sig = cp_parser_objc_method_signature (parser, &attribute);
30518 if (sig == error_mark_node)
30520 cp_parser_skip_to_end_of_block_or_statement (parser);
30521 token = cp_lexer_peek_token (parser->lexer);
30522 continue;
30524 objc_start_method_definition (is_class_method, sig, attribute,
30525 NULL_TREE);
30527 /* For historical reasons, we accept an optional semicolon. */
30528 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30529 cp_lexer_consume_token (parser->lexer);
30531 ptk = cp_lexer_peek_token (parser->lexer);
30532 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30533 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30535 perform_deferred_access_checks (tf_warning_or_error);
30536 stop_deferring_access_checks ();
30537 meth = cp_parser_function_definition_after_declarator (parser,
30538 false);
30539 pop_deferring_access_checks ();
30540 objc_finish_method_definition (meth);
30543 /* The following case will be removed once @synthesize is
30544 completely implemented. */
30545 else if (token->keyword == RID_AT_PROPERTY)
30546 cp_parser_objc_at_property_declaration (parser);
30547 else if (token->keyword == RID_AT_SYNTHESIZE)
30548 cp_parser_objc_at_synthesize_declaration (parser);
30549 else if (token->keyword == RID_AT_DYNAMIC)
30550 cp_parser_objc_at_dynamic_declaration (parser);
30551 else if (token->keyword == RID_ATTRIBUTE
30552 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30553 warning_at (token->location, OPT_Wattributes,
30554 "prefix attributes are ignored for methods");
30555 else
30556 /* Allow for interspersed non-ObjC++ code. */
30557 cp_parser_objc_interstitial_code (parser);
30559 token = cp_lexer_peek_token (parser->lexer);
30562 if (token->type != CPP_EOF)
30563 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30564 else
30565 cp_parser_error (parser, "expected %<@end%>");
30567 objc_finish_implementation ();
30570 /* Parse Objective-C ivars. */
30572 static void
30573 cp_parser_objc_class_ivars (cp_parser* parser)
30575 cp_token *token = cp_lexer_peek_token (parser->lexer);
30577 if (token->type != CPP_OPEN_BRACE)
30578 return; /* No ivars specified. */
30580 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30581 token = cp_lexer_peek_token (parser->lexer);
30583 while (token->type != CPP_CLOSE_BRACE
30584 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30586 cp_decl_specifier_seq declspecs;
30587 int decl_class_or_enum_p;
30588 tree prefix_attributes;
30590 cp_parser_objc_visibility_spec (parser);
30592 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30593 break;
30595 cp_parser_decl_specifier_seq (parser,
30596 CP_PARSER_FLAGS_OPTIONAL,
30597 &declspecs,
30598 &decl_class_or_enum_p);
30600 /* auto, register, static, extern, mutable. */
30601 if (declspecs.storage_class != sc_none)
30603 cp_parser_error (parser, "invalid type for instance variable");
30604 declspecs.storage_class = sc_none;
30607 /* thread_local. */
30608 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30610 cp_parser_error (parser, "invalid type for instance variable");
30611 declspecs.locations[ds_thread] = 0;
30614 /* typedef. */
30615 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30617 cp_parser_error (parser, "invalid type for instance variable");
30618 declspecs.locations[ds_typedef] = 0;
30621 prefix_attributes = declspecs.attributes;
30622 declspecs.attributes = NULL_TREE;
30624 /* Keep going until we hit the `;' at the end of the
30625 declaration. */
30626 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30628 tree width = NULL_TREE, attributes, first_attribute, decl;
30629 cp_declarator *declarator = NULL;
30630 int ctor_dtor_or_conv_p;
30632 /* Check for a (possibly unnamed) bitfield declaration. */
30633 token = cp_lexer_peek_token (parser->lexer);
30634 if (token->type == CPP_COLON)
30635 goto eat_colon;
30637 if (token->type == CPP_NAME
30638 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30639 == CPP_COLON))
30641 /* Get the name of the bitfield. */
30642 declarator = make_id_declarator (NULL_TREE,
30643 cp_parser_identifier (parser),
30644 sfk_none, token->location);
30646 eat_colon:
30647 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30648 /* Get the width of the bitfield. */
30649 width
30650 = cp_parser_constant_expression (parser);
30652 else
30654 /* Parse the declarator. */
30655 declarator
30656 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30657 &ctor_dtor_or_conv_p,
30658 /*parenthesized_p=*/NULL,
30659 /*member_p=*/false,
30660 /*friend_p=*/false);
30663 /* Look for attributes that apply to the ivar. */
30664 attributes = cp_parser_attributes_opt (parser);
30665 /* Remember which attributes are prefix attributes and
30666 which are not. */
30667 first_attribute = attributes;
30668 /* Combine the attributes. */
30669 attributes = attr_chainon (prefix_attributes, attributes);
30671 if (width)
30672 /* Create the bitfield declaration. */
30673 decl = grokbitfield (declarator, &declspecs,
30674 width, NULL_TREE, attributes);
30675 else
30676 decl = grokfield (declarator, &declspecs,
30677 NULL_TREE, /*init_const_expr_p=*/false,
30678 NULL_TREE, attributes);
30680 /* Add the instance variable. */
30681 if (decl != error_mark_node && decl != NULL_TREE)
30682 objc_add_instance_variable (decl);
30684 /* Reset PREFIX_ATTRIBUTES. */
30685 if (attributes != error_mark_node)
30687 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30688 attributes = TREE_CHAIN (attributes);
30689 if (attributes)
30690 TREE_CHAIN (attributes) = NULL_TREE;
30693 token = cp_lexer_peek_token (parser->lexer);
30695 if (token->type == CPP_COMMA)
30697 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30698 continue;
30700 break;
30703 cp_parser_consume_semicolon_at_end_of_statement (parser);
30704 token = cp_lexer_peek_token (parser->lexer);
30707 if (token->keyword == RID_AT_END)
30708 cp_parser_error (parser, "expected %<}%>");
30710 /* Do not consume the RID_AT_END, so it will be read again as terminating
30711 the @interface of @implementation. */
30712 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30713 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30715 /* For historical reasons, we accept an optional semicolon. */
30716 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30717 cp_lexer_consume_token (parser->lexer);
30720 /* Parse an Objective-C protocol declaration. */
30722 static void
30723 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30725 tree proto, protorefs;
30726 cp_token *tok;
30728 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30729 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30731 tok = cp_lexer_peek_token (parser->lexer);
30732 error_at (tok->location, "identifier expected after %<@protocol%>");
30733 cp_parser_consume_semicolon_at_end_of_statement (parser);
30734 return;
30737 /* See if we have a forward declaration or a definition. */
30738 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30740 /* Try a forward declaration first. */
30741 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30743 while (true)
30745 tree id;
30747 id = cp_parser_identifier (parser);
30748 if (id == error_mark_node)
30749 break;
30751 objc_declare_protocol (id, attributes);
30753 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30754 cp_lexer_consume_token (parser->lexer);
30755 else
30756 break;
30758 cp_parser_consume_semicolon_at_end_of_statement (parser);
30761 /* Ok, we got a full-fledged definition (or at least should). */
30762 else
30764 proto = cp_parser_identifier (parser);
30765 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30766 objc_start_protocol (proto, protorefs, attributes);
30767 cp_parser_objc_method_prototype_list (parser);
30771 /* Parse an Objective-C superclass or category. */
30773 static void
30774 cp_parser_objc_superclass_or_category (cp_parser *parser,
30775 bool iface_p,
30776 tree *super,
30777 tree *categ, bool *is_class_extension)
30779 cp_token *next = cp_lexer_peek_token (parser->lexer);
30781 *super = *categ = NULL_TREE;
30782 *is_class_extension = false;
30783 if (next->type == CPP_COLON)
30785 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30786 *super = cp_parser_identifier (parser);
30788 else if (next->type == CPP_OPEN_PAREN)
30790 matching_parens parens;
30791 parens.consume_open (parser); /* Eat '('. */
30793 /* If there is no category name, and this is an @interface, we
30794 have a class extension. */
30795 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30797 *categ = NULL_TREE;
30798 *is_class_extension = true;
30800 else
30801 *categ = cp_parser_identifier (parser);
30803 parens.require_close (parser);
30807 /* Parse an Objective-C class interface. */
30809 static void
30810 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30812 tree name, super, categ, protos;
30813 bool is_class_extension;
30815 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30816 name = cp_parser_identifier (parser);
30817 if (name == error_mark_node)
30819 /* It's hard to recover because even if valid @interface stuff
30820 is to follow, we can't compile it (or validate it) if we
30821 don't even know which class it refers to. Let's assume this
30822 was a stray '@interface' token in the stream and skip it.
30824 return;
30826 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30827 &is_class_extension);
30828 protos = cp_parser_objc_protocol_refs_opt (parser);
30830 /* We have either a class or a category on our hands. */
30831 if (categ || is_class_extension)
30832 objc_start_category_interface (name, categ, protos, attributes);
30833 else
30835 objc_start_class_interface (name, super, protos, attributes);
30836 /* Handle instance variable declarations, if any. */
30837 cp_parser_objc_class_ivars (parser);
30838 objc_continue_interface ();
30841 cp_parser_objc_method_prototype_list (parser);
30844 /* Parse an Objective-C class implementation. */
30846 static void
30847 cp_parser_objc_class_implementation (cp_parser* parser)
30849 tree name, super, categ;
30850 bool is_class_extension;
30852 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30853 name = cp_parser_identifier (parser);
30854 if (name == error_mark_node)
30856 /* It's hard to recover because even if valid @implementation
30857 stuff is to follow, we can't compile it (or validate it) if
30858 we don't even know which class it refers to. Let's assume
30859 this was a stray '@implementation' token in the stream and
30860 skip it.
30862 return;
30864 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30865 &is_class_extension);
30867 /* We have either a class or a category on our hands. */
30868 if (categ)
30869 objc_start_category_implementation (name, categ);
30870 else
30872 objc_start_class_implementation (name, super);
30873 /* Handle instance variable declarations, if any. */
30874 cp_parser_objc_class_ivars (parser);
30875 objc_continue_implementation ();
30878 cp_parser_objc_method_definition_list (parser);
30881 /* Consume the @end token and finish off the implementation. */
30883 static void
30884 cp_parser_objc_end_implementation (cp_parser* parser)
30886 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30887 objc_finish_implementation ();
30890 /* Parse an Objective-C declaration. */
30892 static void
30893 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30895 /* Try to figure out what kind of declaration is present. */
30896 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30898 if (attributes)
30899 switch (kwd->keyword)
30901 case RID_AT_ALIAS:
30902 case RID_AT_CLASS:
30903 case RID_AT_END:
30904 error_at (kwd->location, "attributes may not be specified before"
30905 " the %<@%D%> Objective-C++ keyword",
30906 kwd->u.value);
30907 attributes = NULL;
30908 break;
30909 case RID_AT_IMPLEMENTATION:
30910 warning_at (kwd->location, OPT_Wattributes,
30911 "prefix attributes are ignored before %<@%D%>",
30912 kwd->u.value);
30913 attributes = NULL;
30914 default:
30915 break;
30918 switch (kwd->keyword)
30920 case RID_AT_ALIAS:
30921 cp_parser_objc_alias_declaration (parser);
30922 break;
30923 case RID_AT_CLASS:
30924 cp_parser_objc_class_declaration (parser);
30925 break;
30926 case RID_AT_PROTOCOL:
30927 cp_parser_objc_protocol_declaration (parser, attributes);
30928 break;
30929 case RID_AT_INTERFACE:
30930 cp_parser_objc_class_interface (parser, attributes);
30931 break;
30932 case RID_AT_IMPLEMENTATION:
30933 cp_parser_objc_class_implementation (parser);
30934 break;
30935 case RID_AT_END:
30936 cp_parser_objc_end_implementation (parser);
30937 break;
30938 default:
30939 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30940 kwd->u.value);
30941 cp_parser_skip_to_end_of_block_or_statement (parser);
30945 /* Parse an Objective-C try-catch-finally statement.
30947 objc-try-catch-finally-stmt:
30948 @try compound-statement objc-catch-clause-seq [opt]
30949 objc-finally-clause [opt]
30951 objc-catch-clause-seq:
30952 objc-catch-clause objc-catch-clause-seq [opt]
30954 objc-catch-clause:
30955 @catch ( objc-exception-declaration ) compound-statement
30957 objc-finally-clause:
30958 @finally compound-statement
30960 objc-exception-declaration:
30961 parameter-declaration
30962 '...'
30964 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30966 Returns NULL_TREE.
30968 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30969 for C. Keep them in sync. */
30971 static tree
30972 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30974 location_t location;
30975 tree stmt;
30977 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30978 location = cp_lexer_peek_token (parser->lexer)->location;
30979 objc_maybe_warn_exceptions (location);
30980 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30981 node, lest it get absorbed into the surrounding block. */
30982 stmt = push_stmt_list ();
30983 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30984 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30986 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30988 cp_parameter_declarator *parm;
30989 tree parameter_declaration = error_mark_node;
30990 bool seen_open_paren = false;
30991 matching_parens parens;
30993 cp_lexer_consume_token (parser->lexer);
30994 if (parens.require_open (parser))
30995 seen_open_paren = true;
30996 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30998 /* We have "@catch (...)" (where the '...' are literally
30999 what is in the code). Skip the '...'.
31000 parameter_declaration is set to NULL_TREE, and
31001 objc_being_catch_clauses() knows that that means
31002 '...'. */
31003 cp_lexer_consume_token (parser->lexer);
31004 parameter_declaration = NULL_TREE;
31006 else
31008 /* We have "@catch (NSException *exception)" or something
31009 like that. Parse the parameter declaration. */
31010 parm = cp_parser_parameter_declaration (parser, false, NULL);
31011 if (parm == NULL)
31012 parameter_declaration = error_mark_node;
31013 else
31014 parameter_declaration = grokdeclarator (parm->declarator,
31015 &parm->decl_specifiers,
31016 PARM, /*initialized=*/0,
31017 /*attrlist=*/NULL);
31019 if (seen_open_paren)
31020 parens.require_close (parser);
31021 else
31023 /* If there was no open parenthesis, we are recovering from
31024 an error, and we are trying to figure out what mistake
31025 the user has made. */
31027 /* If there is an immediate closing parenthesis, the user
31028 probably forgot the opening one (ie, they typed "@catch
31029 NSException *e)". Parse the closing parenthesis and keep
31030 going. */
31031 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31032 cp_lexer_consume_token (parser->lexer);
31034 /* If these is no immediate closing parenthesis, the user
31035 probably doesn't know that parenthesis are required at
31036 all (ie, they typed "@catch NSException *e"). So, just
31037 forget about the closing parenthesis and keep going. */
31039 objc_begin_catch_clause (parameter_declaration);
31040 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31041 objc_finish_catch_clause ();
31043 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
31045 cp_lexer_consume_token (parser->lexer);
31046 location = cp_lexer_peek_token (parser->lexer)->location;
31047 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31048 node, lest it get absorbed into the surrounding block. */
31049 stmt = push_stmt_list ();
31050 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31051 objc_build_finally_clause (location, pop_stmt_list (stmt));
31054 return objc_finish_try_stmt ();
31057 /* Parse an Objective-C synchronized statement.
31059 objc-synchronized-stmt:
31060 @synchronized ( expression ) compound-statement
31062 Returns NULL_TREE. */
31064 static tree
31065 cp_parser_objc_synchronized_statement (cp_parser *parser)
31067 location_t location;
31068 tree lock, stmt;
31070 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31072 location = cp_lexer_peek_token (parser->lexer)->location;
31073 objc_maybe_warn_exceptions (location);
31074 matching_parens parens;
31075 parens.require_open (parser);
31076 lock = cp_parser_expression (parser);
31077 parens.require_close (parser);
31079 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31080 node, lest it get absorbed into the surrounding block. */
31081 stmt = push_stmt_list ();
31082 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31084 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31087 /* Parse an Objective-C throw statement.
31089 objc-throw-stmt:
31090 @throw assignment-expression [opt] ;
31092 Returns a constructed '@throw' statement. */
31094 static tree
31095 cp_parser_objc_throw_statement (cp_parser *parser)
31097 tree expr = NULL_TREE;
31098 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31100 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31103 expr = cp_parser_expression (parser);
31105 cp_parser_consume_semicolon_at_end_of_statement (parser);
31107 return objc_build_throw_stmt (loc, expr);
31110 /* Parse an Objective-C statement. */
31112 static tree
31113 cp_parser_objc_statement (cp_parser * parser)
31115 /* Try to figure out what kind of declaration is present. */
31116 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31118 switch (kwd->keyword)
31120 case RID_AT_TRY:
31121 return cp_parser_objc_try_catch_finally_statement (parser);
31122 case RID_AT_SYNCHRONIZED:
31123 return cp_parser_objc_synchronized_statement (parser);
31124 case RID_AT_THROW:
31125 return cp_parser_objc_throw_statement (parser);
31126 default:
31127 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31128 kwd->u.value);
31129 cp_parser_skip_to_end_of_block_or_statement (parser);
31132 return error_mark_node;
31135 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31136 look ahead to see if an objc keyword follows the attributes. This
31137 is to detect the use of prefix attributes on ObjC @interface and
31138 @protocol. */
31140 static bool
31141 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31143 cp_lexer_save_tokens (parser->lexer);
31144 *attrib = cp_parser_attributes_opt (parser);
31145 gcc_assert (*attrib);
31146 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31148 cp_lexer_commit_tokens (parser->lexer);
31149 return true;
31151 cp_lexer_rollback_tokens (parser->lexer);
31152 return false;
31155 /* This routine is a minimal replacement for
31156 c_parser_struct_declaration () used when parsing the list of
31157 types/names or ObjC++ properties. For example, when parsing the
31158 code
31160 @property (readonly) int a, b, c;
31162 this function is responsible for parsing "int a, int b, int c" and
31163 returning the declarations as CHAIN of DECLs.
31165 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31166 similar parsing. */
31167 static tree
31168 cp_parser_objc_struct_declaration (cp_parser *parser)
31170 tree decls = NULL_TREE;
31171 cp_decl_specifier_seq declspecs;
31172 int decl_class_or_enum_p;
31173 tree prefix_attributes;
31175 cp_parser_decl_specifier_seq (parser,
31176 CP_PARSER_FLAGS_NONE,
31177 &declspecs,
31178 &decl_class_or_enum_p);
31180 if (declspecs.type == error_mark_node)
31181 return error_mark_node;
31183 /* auto, register, static, extern, mutable. */
31184 if (declspecs.storage_class != sc_none)
31186 cp_parser_error (parser, "invalid type for property");
31187 declspecs.storage_class = sc_none;
31190 /* thread_local. */
31191 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31193 cp_parser_error (parser, "invalid type for property");
31194 declspecs.locations[ds_thread] = 0;
31197 /* typedef. */
31198 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31200 cp_parser_error (parser, "invalid type for property");
31201 declspecs.locations[ds_typedef] = 0;
31204 prefix_attributes = declspecs.attributes;
31205 declspecs.attributes = NULL_TREE;
31207 /* Keep going until we hit the `;' at the end of the declaration. */
31208 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31210 tree attributes, first_attribute, decl;
31211 cp_declarator *declarator;
31212 cp_token *token;
31214 /* Parse the declarator. */
31215 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31216 NULL, NULL, false, false);
31218 /* Look for attributes that apply to the ivar. */
31219 attributes = cp_parser_attributes_opt (parser);
31220 /* Remember which attributes are prefix attributes and
31221 which are not. */
31222 first_attribute = attributes;
31223 /* Combine the attributes. */
31224 attributes = attr_chainon (prefix_attributes, attributes);
31226 decl = grokfield (declarator, &declspecs,
31227 NULL_TREE, /*init_const_expr_p=*/false,
31228 NULL_TREE, attributes);
31230 if (decl == error_mark_node || decl == NULL_TREE)
31231 return error_mark_node;
31233 /* Reset PREFIX_ATTRIBUTES. */
31234 if (attributes != error_mark_node)
31236 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31237 attributes = TREE_CHAIN (attributes);
31238 if (attributes)
31239 TREE_CHAIN (attributes) = NULL_TREE;
31242 DECL_CHAIN (decl) = decls;
31243 decls = decl;
31245 token = cp_lexer_peek_token (parser->lexer);
31246 if (token->type == CPP_COMMA)
31248 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31249 continue;
31251 else
31252 break;
31254 return decls;
31257 /* Parse an Objective-C @property declaration. The syntax is:
31259 objc-property-declaration:
31260 '@property' objc-property-attributes[opt] struct-declaration ;
31262 objc-property-attributes:
31263 '(' objc-property-attribute-list ')'
31265 objc-property-attribute-list:
31266 objc-property-attribute
31267 objc-property-attribute-list, objc-property-attribute
31269 objc-property-attribute
31270 'getter' = identifier
31271 'setter' = identifier
31272 'readonly'
31273 'readwrite'
31274 'assign'
31275 'retain'
31276 'copy'
31277 'nonatomic'
31279 For example:
31280 @property NSString *name;
31281 @property (readonly) id object;
31282 @property (retain, nonatomic, getter=getTheName) id name;
31283 @property int a, b, c;
31285 PS: This function is identical to
31286 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31287 static void
31288 cp_parser_objc_at_property_declaration (cp_parser *parser)
31290 /* The following variables hold the attributes of the properties as
31291 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31292 seen. When we see an attribute, we set them to 'true' (if they
31293 are boolean properties) or to the identifier (if they have an
31294 argument, ie, for getter and setter). Note that here we only
31295 parse the list of attributes, check the syntax and accumulate the
31296 attributes that we find. objc_add_property_declaration() will
31297 then process the information. */
31298 bool property_assign = false;
31299 bool property_copy = false;
31300 tree property_getter_ident = NULL_TREE;
31301 bool property_nonatomic = false;
31302 bool property_readonly = false;
31303 bool property_readwrite = false;
31304 bool property_retain = false;
31305 tree property_setter_ident = NULL_TREE;
31307 /* 'properties' is the list of properties that we read. Usually a
31308 single one, but maybe more (eg, in "@property int a, b, c;" there
31309 are three). */
31310 tree properties;
31311 location_t loc;
31313 loc = cp_lexer_peek_token (parser->lexer)->location;
31315 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31317 /* Parse the optional attribute list... */
31318 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31320 /* Eat the '('. */
31321 matching_parens parens;
31322 parens.consume_open (parser);
31324 while (true)
31326 bool syntax_error = false;
31327 cp_token *token = cp_lexer_peek_token (parser->lexer);
31328 enum rid keyword;
31330 if (token->type != CPP_NAME)
31332 cp_parser_error (parser, "expected identifier");
31333 break;
31335 keyword = C_RID_CODE (token->u.value);
31336 cp_lexer_consume_token (parser->lexer);
31337 switch (keyword)
31339 case RID_ASSIGN: property_assign = true; break;
31340 case RID_COPY: property_copy = true; break;
31341 case RID_NONATOMIC: property_nonatomic = true; break;
31342 case RID_READONLY: property_readonly = true; break;
31343 case RID_READWRITE: property_readwrite = true; break;
31344 case RID_RETAIN: property_retain = true; break;
31346 case RID_GETTER:
31347 case RID_SETTER:
31348 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31350 if (keyword == RID_GETTER)
31351 cp_parser_error (parser,
31352 "missing %<=%> (after %<getter%> attribute)");
31353 else
31354 cp_parser_error (parser,
31355 "missing %<=%> (after %<setter%> attribute)");
31356 syntax_error = true;
31357 break;
31359 cp_lexer_consume_token (parser->lexer); /* eat the = */
31360 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31362 cp_parser_error (parser, "expected identifier");
31363 syntax_error = true;
31364 break;
31366 if (keyword == RID_SETTER)
31368 if (property_setter_ident != NULL_TREE)
31370 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31371 cp_lexer_consume_token (parser->lexer);
31373 else
31374 property_setter_ident = cp_parser_objc_selector (parser);
31375 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31376 cp_parser_error (parser, "setter name must terminate with %<:%>");
31377 else
31378 cp_lexer_consume_token (parser->lexer);
31380 else
31382 if (property_getter_ident != NULL_TREE)
31384 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31385 cp_lexer_consume_token (parser->lexer);
31387 else
31388 property_getter_ident = cp_parser_objc_selector (parser);
31390 break;
31391 default:
31392 cp_parser_error (parser, "unknown property attribute");
31393 syntax_error = true;
31394 break;
31397 if (syntax_error)
31398 break;
31400 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31401 cp_lexer_consume_token (parser->lexer);
31402 else
31403 break;
31406 /* FIXME: "@property (setter, assign);" will generate a spurious
31407 "error: expected ‘)’ before ‘,’ token". This is because
31408 cp_parser_require, unlike the C counterpart, will produce an
31409 error even if we are in error recovery. */
31410 if (!parens.require_close (parser))
31412 cp_parser_skip_to_closing_parenthesis (parser,
31413 /*recovering=*/true,
31414 /*or_comma=*/false,
31415 /*consume_paren=*/true);
31419 /* ... and the property declaration(s). */
31420 properties = cp_parser_objc_struct_declaration (parser);
31422 if (properties == error_mark_node)
31424 cp_parser_skip_to_end_of_statement (parser);
31425 /* If the next token is now a `;', consume it. */
31426 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31427 cp_lexer_consume_token (parser->lexer);
31428 return;
31431 if (properties == NULL_TREE)
31432 cp_parser_error (parser, "expected identifier");
31433 else
31435 /* Comma-separated properties are chained together in
31436 reverse order; add them one by one. */
31437 properties = nreverse (properties);
31439 for (; properties; properties = TREE_CHAIN (properties))
31440 objc_add_property_declaration (loc, copy_node (properties),
31441 property_readonly, property_readwrite,
31442 property_assign, property_retain,
31443 property_copy, property_nonatomic,
31444 property_getter_ident, property_setter_ident);
31447 cp_parser_consume_semicolon_at_end_of_statement (parser);
31450 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31452 objc-synthesize-declaration:
31453 @synthesize objc-synthesize-identifier-list ;
31455 objc-synthesize-identifier-list:
31456 objc-synthesize-identifier
31457 objc-synthesize-identifier-list, objc-synthesize-identifier
31459 objc-synthesize-identifier
31460 identifier
31461 identifier = identifier
31463 For example:
31464 @synthesize MyProperty;
31465 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31467 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31468 for C. Keep them in sync.
31470 static void
31471 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31473 tree list = NULL_TREE;
31474 location_t loc;
31475 loc = cp_lexer_peek_token (parser->lexer)->location;
31477 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31478 while (true)
31480 tree property, ivar;
31481 property = cp_parser_identifier (parser);
31482 if (property == error_mark_node)
31484 cp_parser_consume_semicolon_at_end_of_statement (parser);
31485 return;
31487 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31489 cp_lexer_consume_token (parser->lexer);
31490 ivar = cp_parser_identifier (parser);
31491 if (ivar == error_mark_node)
31493 cp_parser_consume_semicolon_at_end_of_statement (parser);
31494 return;
31497 else
31498 ivar = NULL_TREE;
31499 list = chainon (list, build_tree_list (ivar, property));
31500 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31501 cp_lexer_consume_token (parser->lexer);
31502 else
31503 break;
31505 cp_parser_consume_semicolon_at_end_of_statement (parser);
31506 objc_add_synthesize_declaration (loc, list);
31509 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31511 objc-dynamic-declaration:
31512 @dynamic identifier-list ;
31514 For example:
31515 @dynamic MyProperty;
31516 @dynamic MyProperty, AnotherProperty;
31518 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31519 for C. Keep them in sync.
31521 static void
31522 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31524 tree list = NULL_TREE;
31525 location_t loc;
31526 loc = cp_lexer_peek_token (parser->lexer)->location;
31528 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31529 while (true)
31531 tree property;
31532 property = cp_parser_identifier (parser);
31533 if (property == error_mark_node)
31535 cp_parser_consume_semicolon_at_end_of_statement (parser);
31536 return;
31538 list = chainon (list, build_tree_list (NULL, property));
31539 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31540 cp_lexer_consume_token (parser->lexer);
31541 else
31542 break;
31544 cp_parser_consume_semicolon_at_end_of_statement (parser);
31545 objc_add_dynamic_declaration (loc, list);
31549 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
31551 /* Returns name of the next clause.
31552 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31553 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31554 returned and the token is consumed. */
31556 static pragma_omp_clause
31557 cp_parser_omp_clause_name (cp_parser *parser)
31559 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31561 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31562 result = PRAGMA_OACC_CLAUSE_AUTO;
31563 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31564 result = PRAGMA_OMP_CLAUSE_IF;
31565 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31566 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31567 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31568 result = PRAGMA_OACC_CLAUSE_DELETE;
31569 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31570 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31571 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31572 result = PRAGMA_OMP_CLAUSE_FOR;
31573 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31575 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31576 const char *p = IDENTIFIER_POINTER (id);
31578 switch (p[0])
31580 case 'a':
31581 if (!strcmp ("aligned", p))
31582 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31583 else if (!strcmp ("async", p))
31584 result = PRAGMA_OACC_CLAUSE_ASYNC;
31585 break;
31586 case 'c':
31587 if (!strcmp ("collapse", p))
31588 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31589 else if (!strcmp ("copy", p))
31590 result = PRAGMA_OACC_CLAUSE_COPY;
31591 else if (!strcmp ("copyin", p))
31592 result = PRAGMA_OMP_CLAUSE_COPYIN;
31593 else if (!strcmp ("copyout", p))
31594 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31595 else if (!strcmp ("copyprivate", p))
31596 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31597 else if (!strcmp ("create", p))
31598 result = PRAGMA_OACC_CLAUSE_CREATE;
31599 break;
31600 case 'd':
31601 if (!strcmp ("defaultmap", p))
31602 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31603 else if (!strcmp ("depend", p))
31604 result = PRAGMA_OMP_CLAUSE_DEPEND;
31605 else if (!strcmp ("device", p))
31606 result = PRAGMA_OMP_CLAUSE_DEVICE;
31607 else if (!strcmp ("deviceptr", p))
31608 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31609 else if (!strcmp ("device_resident", p))
31610 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31611 else if (!strcmp ("dist_schedule", p))
31612 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31613 break;
31614 case 'f':
31615 if (!strcmp ("final", p))
31616 result = PRAGMA_OMP_CLAUSE_FINAL;
31617 else if (!strcmp ("finalize", p))
31618 result = PRAGMA_OACC_CLAUSE_FINALIZE;
31619 else if (!strcmp ("firstprivate", p))
31620 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31621 else if (!strcmp ("from", p))
31622 result = PRAGMA_OMP_CLAUSE_FROM;
31623 break;
31624 case 'g':
31625 if (!strcmp ("gang", p))
31626 result = PRAGMA_OACC_CLAUSE_GANG;
31627 else if (!strcmp ("grainsize", p))
31628 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31629 break;
31630 case 'h':
31631 if (!strcmp ("hint", p))
31632 result = PRAGMA_OMP_CLAUSE_HINT;
31633 else if (!strcmp ("host", p))
31634 result = PRAGMA_OACC_CLAUSE_HOST;
31635 break;
31636 case 'i':
31637 if (!strcmp ("if_present", p))
31638 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
31639 else if (!strcmp ("in_reduction", p))
31640 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
31641 else if (!strcmp ("inbranch", p))
31642 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31643 else if (!strcmp ("independent", p))
31644 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31645 else if (!strcmp ("is_device_ptr", p))
31646 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31647 break;
31648 case 'l':
31649 if (!strcmp ("lastprivate", p))
31650 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31651 else if (!strcmp ("linear", p))
31652 result = PRAGMA_OMP_CLAUSE_LINEAR;
31653 else if (!strcmp ("link", p))
31654 result = PRAGMA_OMP_CLAUSE_LINK;
31655 break;
31656 case 'm':
31657 if (!strcmp ("map", p))
31658 result = PRAGMA_OMP_CLAUSE_MAP;
31659 else if (!strcmp ("mergeable", p))
31660 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31661 break;
31662 case 'n':
31663 if (!strcmp ("nogroup", p))
31664 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31665 else if (!strcmp ("nontemporal", p))
31666 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
31667 else if (!strcmp ("notinbranch", p))
31668 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31669 else if (!strcmp ("nowait", p))
31670 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31671 else if (!strcmp ("num_gangs", p))
31672 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31673 else if (!strcmp ("num_tasks", p))
31674 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31675 else if (!strcmp ("num_teams", p))
31676 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31677 else if (!strcmp ("num_threads", p))
31678 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31679 else if (!strcmp ("num_workers", p))
31680 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31681 break;
31682 case 'o':
31683 if (!strcmp ("ordered", p))
31684 result = PRAGMA_OMP_CLAUSE_ORDERED;
31685 break;
31686 case 'p':
31687 if (!strcmp ("parallel", p))
31688 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31689 else if (!strcmp ("present", p))
31690 result = PRAGMA_OACC_CLAUSE_PRESENT;
31691 else if (!strcmp ("present_or_copy", p)
31692 || !strcmp ("pcopy", p))
31693 result = PRAGMA_OACC_CLAUSE_COPY;
31694 else if (!strcmp ("present_or_copyin", p)
31695 || !strcmp ("pcopyin", p))
31696 result = PRAGMA_OACC_CLAUSE_COPYIN;
31697 else if (!strcmp ("present_or_copyout", p)
31698 || !strcmp ("pcopyout", p))
31699 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31700 else if (!strcmp ("present_or_create", p)
31701 || !strcmp ("pcreate", p))
31702 result = PRAGMA_OACC_CLAUSE_CREATE;
31703 else if (!strcmp ("priority", p))
31704 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31705 else if (!strcmp ("proc_bind", p))
31706 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31707 break;
31708 case 'r':
31709 if (!strcmp ("reduction", p))
31710 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31711 break;
31712 case 's':
31713 if (!strcmp ("safelen", p))
31714 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31715 else if (!strcmp ("schedule", p))
31716 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31717 else if (!strcmp ("sections", p))
31718 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31719 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
31720 result = PRAGMA_OACC_CLAUSE_HOST;
31721 else if (!strcmp ("seq", p))
31722 result = PRAGMA_OACC_CLAUSE_SEQ;
31723 else if (!strcmp ("shared", p))
31724 result = PRAGMA_OMP_CLAUSE_SHARED;
31725 else if (!strcmp ("simd", p))
31726 result = PRAGMA_OMP_CLAUSE_SIMD;
31727 else if (!strcmp ("simdlen", p))
31728 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31729 break;
31730 case 't':
31731 if (!strcmp ("task_reduction", p))
31732 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
31733 else if (!strcmp ("taskgroup", p))
31734 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31735 else if (!strcmp ("thread_limit", p))
31736 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31737 else if (!strcmp ("threads", p))
31738 result = PRAGMA_OMP_CLAUSE_THREADS;
31739 else if (!strcmp ("tile", p))
31740 result = PRAGMA_OACC_CLAUSE_TILE;
31741 else if (!strcmp ("to", p))
31742 result = PRAGMA_OMP_CLAUSE_TO;
31743 break;
31744 case 'u':
31745 if (!strcmp ("uniform", p))
31746 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31747 else if (!strcmp ("untied", p))
31748 result = PRAGMA_OMP_CLAUSE_UNTIED;
31749 else if (!strcmp ("use_device", p))
31750 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31751 else if (!strcmp ("use_device_ptr", p))
31752 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31753 break;
31754 case 'v':
31755 if (!strcmp ("vector", p))
31756 result = PRAGMA_OACC_CLAUSE_VECTOR;
31757 else if (!strcmp ("vector_length", p))
31758 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31759 break;
31760 case 'w':
31761 if (!strcmp ("wait", p))
31762 result = PRAGMA_OACC_CLAUSE_WAIT;
31763 else if (!strcmp ("worker", p))
31764 result = PRAGMA_OACC_CLAUSE_WORKER;
31765 break;
31769 if (result != PRAGMA_OMP_CLAUSE_NONE)
31770 cp_lexer_consume_token (parser->lexer);
31772 return result;
31775 /* Validate that a clause of the given type does not already exist. */
31777 static void
31778 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31779 const char *name, location_t location)
31781 tree c;
31783 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31784 if (OMP_CLAUSE_CODE (c) == code)
31786 error_at (location, "too many %qs clauses", name);
31787 break;
31791 /* OpenMP 2.5:
31792 variable-list:
31793 identifier
31794 variable-list , identifier
31796 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31797 colon). An opening parenthesis will have been consumed by the caller.
31799 If KIND is nonzero, create the appropriate node and install the decl
31800 in OMP_CLAUSE_DECL and add the node to the head of the list.
31802 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31803 return the list created.
31805 COLON can be NULL if only closing parenthesis should end the list,
31806 or pointer to bool which will receive false if the list is terminated
31807 by closing parenthesis or true if the list is terminated by colon. */
31809 static tree
31810 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31811 tree list, bool *colon)
31813 cp_token *token;
31814 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31815 if (colon)
31817 parser->colon_corrects_to_scope_p = false;
31818 *colon = false;
31820 while (1)
31822 tree name, decl;
31824 if (kind == OMP_CLAUSE_DEPEND)
31825 cp_parser_parse_tentatively (parser);
31826 token = cp_lexer_peek_token (parser->lexer);
31827 if (kind != 0
31828 && current_class_ptr
31829 && cp_parser_is_keyword (token, RID_THIS))
31831 decl = finish_this_expr ();
31832 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31833 || CONVERT_EXPR_P (decl))
31834 decl = TREE_OPERAND (decl, 0);
31835 cp_lexer_consume_token (parser->lexer);
31837 else
31839 name = cp_parser_id_expression (parser, /*template_p=*/false,
31840 /*check_dependency_p=*/true,
31841 /*template_p=*/NULL,
31842 /*declarator_p=*/false,
31843 /*optional_p=*/false);
31844 if (name == error_mark_node)
31846 if (kind == OMP_CLAUSE_DEPEND
31847 && cp_parser_simulate_error (parser))
31848 goto depend_lvalue;
31849 goto skip_comma;
31852 if (identifier_p (name))
31853 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31854 else
31855 decl = name;
31856 if (decl == error_mark_node)
31858 if (kind == OMP_CLAUSE_DEPEND
31859 && cp_parser_simulate_error (parser))
31860 goto depend_lvalue;
31861 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31862 token->location);
31865 if (decl == error_mark_node)
31867 else if (kind != 0)
31869 switch (kind)
31871 case OMP_CLAUSE__CACHE_:
31872 /* The OpenACC cache directive explicitly only allows "array
31873 elements or subarrays". */
31874 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31876 error_at (token->location, "expected %<[%>");
31877 decl = error_mark_node;
31878 break;
31880 /* FALLTHROUGH. */
31881 case OMP_CLAUSE_MAP:
31882 case OMP_CLAUSE_FROM:
31883 case OMP_CLAUSE_TO:
31884 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31886 location_t loc
31887 = cp_lexer_peek_token (parser->lexer)->location;
31888 cp_id_kind idk = CP_ID_KIND_NONE;
31889 cp_lexer_consume_token (parser->lexer);
31890 decl = convert_from_reference (decl);
31891 decl
31892 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31893 decl, false,
31894 &idk, loc);
31896 /* FALLTHROUGH. */
31897 case OMP_CLAUSE_DEPEND:
31898 case OMP_CLAUSE_REDUCTION:
31899 case OMP_CLAUSE_IN_REDUCTION:
31900 case OMP_CLAUSE_TASK_REDUCTION:
31901 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31903 tree low_bound = NULL_TREE, length = NULL_TREE;
31905 parser->colon_corrects_to_scope_p = false;
31906 cp_lexer_consume_token (parser->lexer);
31907 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31908 low_bound = cp_parser_expression (parser);
31909 if (!colon)
31910 parser->colon_corrects_to_scope_p
31911 = saved_colon_corrects_to_scope_p;
31912 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31913 length = integer_one_node;
31914 else
31916 /* Look for `:'. */
31917 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31919 if (kind == OMP_CLAUSE_DEPEND
31920 && cp_parser_simulate_error (parser))
31921 goto depend_lvalue;
31922 goto skip_comma;
31924 if (kind == OMP_CLAUSE_DEPEND)
31925 cp_parser_commit_to_tentative_parse (parser);
31926 if (!cp_lexer_next_token_is (parser->lexer,
31927 CPP_CLOSE_SQUARE))
31928 length = cp_parser_expression (parser);
31930 /* Look for the closing `]'. */
31931 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31932 RT_CLOSE_SQUARE))
31934 if (kind == OMP_CLAUSE_DEPEND
31935 && cp_parser_simulate_error (parser))
31936 goto depend_lvalue;
31937 goto skip_comma;
31940 decl = tree_cons (low_bound, length, decl);
31942 break;
31943 default:
31944 break;
31947 if (kind == OMP_CLAUSE_DEPEND)
31949 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
31950 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
31951 && cp_parser_simulate_error (parser))
31953 depend_lvalue:
31954 cp_parser_abort_tentative_parse (parser);
31955 decl = cp_parser_assignment_expression (parser, NULL,
31956 false, false);
31958 else
31959 cp_parser_parse_definitely (parser);
31962 tree u = build_omp_clause (token->location, kind);
31963 OMP_CLAUSE_DECL (u) = decl;
31964 OMP_CLAUSE_CHAIN (u) = list;
31965 list = u;
31967 else
31968 list = tree_cons (decl, NULL_TREE, list);
31970 get_comma:
31971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31972 break;
31973 cp_lexer_consume_token (parser->lexer);
31976 if (colon)
31977 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31979 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31981 *colon = true;
31982 cp_parser_require (parser, CPP_COLON, RT_COLON);
31983 return list;
31986 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31988 int ending;
31990 /* Try to resync to an unnested comma. Copied from
31991 cp_parser_parenthesized_expression_list. */
31992 skip_comma:
31993 if (colon)
31994 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31995 ending = cp_parser_skip_to_closing_parenthesis (parser,
31996 /*recovering=*/true,
31997 /*or_comma=*/true,
31998 /*consume_paren=*/true);
31999 if (ending < 0)
32000 goto get_comma;
32003 return list;
32006 /* Similarly, but expect leading and trailing parenthesis. This is a very
32007 common case for omp clauses. */
32009 static tree
32010 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
32012 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32013 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
32014 return list;
32017 /* OpenACC 2.0:
32018 copy ( variable-list )
32019 copyin ( variable-list )
32020 copyout ( variable-list )
32021 create ( variable-list )
32022 delete ( variable-list )
32023 present ( variable-list ) */
32025 static tree
32026 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
32027 tree list)
32029 enum gomp_map_kind kind;
32030 switch (c_kind)
32032 case PRAGMA_OACC_CLAUSE_COPY:
32033 kind = GOMP_MAP_TOFROM;
32034 break;
32035 case PRAGMA_OACC_CLAUSE_COPYIN:
32036 kind = GOMP_MAP_TO;
32037 break;
32038 case PRAGMA_OACC_CLAUSE_COPYOUT:
32039 kind = GOMP_MAP_FROM;
32040 break;
32041 case PRAGMA_OACC_CLAUSE_CREATE:
32042 kind = GOMP_MAP_ALLOC;
32043 break;
32044 case PRAGMA_OACC_CLAUSE_DELETE:
32045 kind = GOMP_MAP_RELEASE;
32046 break;
32047 case PRAGMA_OACC_CLAUSE_DEVICE:
32048 kind = GOMP_MAP_FORCE_TO;
32049 break;
32050 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32051 kind = GOMP_MAP_DEVICE_RESIDENT;
32052 break;
32053 case PRAGMA_OACC_CLAUSE_HOST:
32054 kind = GOMP_MAP_FORCE_FROM;
32055 break;
32056 case PRAGMA_OACC_CLAUSE_LINK:
32057 kind = GOMP_MAP_LINK;
32058 break;
32059 case PRAGMA_OACC_CLAUSE_PRESENT:
32060 kind = GOMP_MAP_FORCE_PRESENT;
32061 break;
32062 default:
32063 gcc_unreachable ();
32065 tree nl, c;
32066 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32068 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32069 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32071 return nl;
32074 /* OpenACC 2.0:
32075 deviceptr ( variable-list ) */
32077 static tree
32078 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32080 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32081 tree vars, t;
32083 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32084 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32085 variable-list must only allow for pointer variables. */
32086 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32087 for (t = vars; t; t = TREE_CHAIN (t))
32089 tree v = TREE_PURPOSE (t);
32090 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32091 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32092 OMP_CLAUSE_DECL (u) = v;
32093 OMP_CLAUSE_CHAIN (u) = list;
32094 list = u;
32097 return list;
32100 /* OpenACC 2.5:
32101 auto
32102 finalize
32103 independent
32104 nohost
32105 seq */
32107 static tree
32108 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
32109 enum omp_clause_code code,
32110 tree list, location_t location)
32112 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32113 tree c = build_omp_clause (location, code);
32114 OMP_CLAUSE_CHAIN (c) = list;
32115 return c;
32118 /* OpenACC:
32119 num_gangs ( expression )
32120 num_workers ( expression )
32121 vector_length ( expression ) */
32123 static tree
32124 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32125 const char *str, tree list)
32127 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32129 matching_parens parens;
32130 if (!parens.require_open (parser))
32131 return list;
32133 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32135 if (t == error_mark_node
32136 || !parens.require_close (parser))
32138 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32139 /*or_comma=*/false,
32140 /*consume_paren=*/true);
32141 return list;
32144 check_no_duplicate_clause (list, code, str, loc);
32146 tree c = build_omp_clause (loc, code);
32147 OMP_CLAUSE_OPERAND (c, 0) = t;
32148 OMP_CLAUSE_CHAIN (c) = list;
32149 return c;
32152 /* OpenACC:
32154 gang [( gang-arg-list )]
32155 worker [( [num:] int-expr )]
32156 vector [( [length:] int-expr )]
32158 where gang-arg is one of:
32160 [num:] int-expr
32161 static: size-expr
32163 and size-expr may be:
32166 int-expr
32169 static tree
32170 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
32171 const char *str, tree list)
32173 const char *id = "num";
32174 cp_lexer *lexer = parser->lexer;
32175 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32176 location_t loc = cp_lexer_peek_token (lexer)->location;
32178 if (kind == OMP_CLAUSE_VECTOR)
32179 id = "length";
32181 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32183 matching_parens parens;
32184 parens.consume_open (parser);
32188 cp_token *next = cp_lexer_peek_token (lexer);
32189 int idx = 0;
32191 /* Gang static argument. */
32192 if (kind == OMP_CLAUSE_GANG
32193 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32195 cp_lexer_consume_token (lexer);
32197 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32198 goto cleanup_error;
32200 idx = 1;
32201 if (ops[idx] != NULL)
32203 cp_parser_error (parser, "too many %<static%> arguments");
32204 goto cleanup_error;
32207 /* Check for the '*' argument. */
32208 if (cp_lexer_next_token_is (lexer, CPP_MULT)
32209 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32210 || cp_lexer_nth_token_is (parser->lexer, 2,
32211 CPP_CLOSE_PAREN)))
32213 cp_lexer_consume_token (lexer);
32214 ops[idx] = integer_minus_one_node;
32216 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32218 cp_lexer_consume_token (lexer);
32219 continue;
32221 else break;
32224 /* Worker num: argument and vector length: arguments. */
32225 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32226 && id_equal (next->u.value, id)
32227 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32229 cp_lexer_consume_token (lexer); /* id */
32230 cp_lexer_consume_token (lexer); /* ':' */
32233 /* Now collect the actual argument. */
32234 if (ops[idx] != NULL_TREE)
32236 cp_parser_error (parser, "unexpected argument");
32237 goto cleanup_error;
32240 tree expr = cp_parser_assignment_expression (parser, NULL, false,
32241 false);
32242 if (expr == error_mark_node)
32243 goto cleanup_error;
32245 mark_exp_read (expr);
32246 ops[idx] = expr;
32248 if (kind == OMP_CLAUSE_GANG
32249 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32251 cp_lexer_consume_token (lexer);
32252 continue;
32254 break;
32256 while (1);
32258 if (!parens.require_close (parser))
32259 goto cleanup_error;
32262 check_no_duplicate_clause (list, kind, str, loc);
32264 c = build_omp_clause (loc, kind);
32266 if (ops[1])
32267 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32269 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32270 OMP_CLAUSE_CHAIN (c) = list;
32272 return c;
32274 cleanup_error:
32275 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32276 return list;
32279 /* OpenACC 2.0:
32280 tile ( size-expr-list ) */
32282 static tree
32283 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32285 tree c, expr = error_mark_node;
32286 tree tile = NULL_TREE;
32288 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32289 so, but the spec authors never considered such a case and have
32290 differing opinions on what it might mean, including 'not
32291 allowed'.) */
32292 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32293 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32294 clause_loc);
32296 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32297 return list;
32301 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32302 return list;
32304 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32305 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32306 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32308 cp_lexer_consume_token (parser->lexer);
32309 expr = integer_zero_node;
32311 else
32312 expr = cp_parser_constant_expression (parser);
32314 tile = tree_cons (NULL_TREE, expr, tile);
32316 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32318 /* Consume the trailing ')'. */
32319 cp_lexer_consume_token (parser->lexer);
32321 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32322 tile = nreverse (tile);
32323 OMP_CLAUSE_TILE_LIST (c) = tile;
32324 OMP_CLAUSE_CHAIN (c) = list;
32325 return c;
32328 /* OpenACC 2.0
32329 Parse wait clause or directive parameters. */
32331 static tree
32332 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32334 vec<tree, va_gc> *args;
32335 tree t, args_tree;
32337 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32338 /*cast_p=*/false,
32339 /*allow_expansion_p=*/true,
32340 /*non_constant_p=*/NULL);
32342 if (args == NULL || args->length () == 0)
32344 cp_parser_error (parser, "expected integer expression before ')'");
32345 if (args != NULL)
32346 release_tree_vector (args);
32347 return list;
32350 args_tree = build_tree_list_vec (args);
32352 release_tree_vector (args);
32354 for (t = args_tree; t; t = TREE_CHAIN (t))
32356 tree targ = TREE_VALUE (t);
32358 if (targ != error_mark_node)
32360 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32361 error ("%<wait%> expression must be integral");
32362 else
32364 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32366 targ = mark_rvalue_use (targ);
32367 OMP_CLAUSE_DECL (c) = targ;
32368 OMP_CLAUSE_CHAIN (c) = list;
32369 list = c;
32374 return list;
32377 /* OpenACC:
32378 wait ( int-expr-list ) */
32380 static tree
32381 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32383 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32385 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32386 return list;
32388 list = cp_parser_oacc_wait_list (parser, location, list);
32390 return list;
32393 /* OpenMP 3.0:
32394 collapse ( constant-expression ) */
32396 static tree
32397 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32399 tree c, num;
32400 location_t loc;
32401 HOST_WIDE_INT n;
32403 loc = cp_lexer_peek_token (parser->lexer)->location;
32404 matching_parens parens;
32405 if (!parens.require_open (parser))
32406 return list;
32408 num = cp_parser_constant_expression (parser);
32410 if (!parens.require_close (parser))
32411 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32412 /*or_comma=*/false,
32413 /*consume_paren=*/true);
32415 if (num == error_mark_node)
32416 return list;
32417 num = fold_non_dependent_expr (num);
32418 if (!tree_fits_shwi_p (num)
32419 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32420 || (n = tree_to_shwi (num)) <= 0
32421 || (int) n != n)
32423 error_at (loc, "collapse argument needs positive constant integer expression");
32424 return list;
32427 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32428 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32429 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32430 OMP_CLAUSE_CHAIN (c) = list;
32431 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32433 return c;
32436 /* OpenMP 2.5:
32437 default ( none | shared )
32439 OpenACC:
32440 default ( none | present ) */
32442 static tree
32443 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32444 location_t location, bool is_oacc)
32446 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32447 tree c;
32449 matching_parens parens;
32450 if (!parens.require_open (parser))
32451 return list;
32452 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32454 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32455 const char *p = IDENTIFIER_POINTER (id);
32457 switch (p[0])
32459 case 'n':
32460 if (strcmp ("none", p) != 0)
32461 goto invalid_kind;
32462 kind = OMP_CLAUSE_DEFAULT_NONE;
32463 break;
32465 case 'p':
32466 if (strcmp ("present", p) != 0 || !is_oacc)
32467 goto invalid_kind;
32468 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32469 break;
32471 case 's':
32472 if (strcmp ("shared", p) != 0 || is_oacc)
32473 goto invalid_kind;
32474 kind = OMP_CLAUSE_DEFAULT_SHARED;
32475 break;
32477 default:
32478 goto invalid_kind;
32481 cp_lexer_consume_token (parser->lexer);
32483 else
32485 invalid_kind:
32486 if (is_oacc)
32487 cp_parser_error (parser, "expected %<none%> or %<present%>");
32488 else
32489 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32492 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32493 || !parens.require_close (parser))
32494 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32495 /*or_comma=*/false,
32496 /*consume_paren=*/true);
32498 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32499 return list;
32501 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32502 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32503 OMP_CLAUSE_CHAIN (c) = list;
32504 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32506 return c;
32509 /* OpenMP 3.1:
32510 final ( expression ) */
32512 static tree
32513 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32515 tree t, c;
32517 matching_parens parens;
32518 if (!parens.require_open (parser))
32519 return list;
32521 t = cp_parser_assignment_expression (parser);
32523 if (t == error_mark_node
32524 || !parens.require_close (parser))
32525 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32526 /*or_comma=*/false,
32527 /*consume_paren=*/true);
32529 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32531 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32532 OMP_CLAUSE_FINAL_EXPR (c) = t;
32533 OMP_CLAUSE_CHAIN (c) = list;
32535 return c;
32538 /* OpenMP 2.5:
32539 if ( expression )
32541 OpenMP 4.5:
32542 if ( directive-name-modifier : expression )
32544 directive-name-modifier:
32545 parallel | task | taskloop | target data | target | target update
32546 | target enter data | target exit data
32548 OpenMP 5.0:
32549 directive-name-modifier:
32550 ... | simd | cancel */
32552 static tree
32553 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32554 bool is_omp)
32556 tree t, c;
32557 enum tree_code if_modifier = ERROR_MARK;
32559 matching_parens parens;
32560 if (!parens.require_open (parser))
32561 return list;
32563 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32565 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32566 const char *p = IDENTIFIER_POINTER (id);
32567 int n = 2;
32569 if (strcmp ("cancel", p) == 0)
32570 if_modifier = VOID_CST;
32571 else if (strcmp ("parallel", p) == 0)
32572 if_modifier = OMP_PARALLEL;
32573 else if (strcmp ("simd", p) == 0)
32574 if_modifier = OMP_SIMD;
32575 else if (strcmp ("task", p) == 0)
32576 if_modifier = OMP_TASK;
32577 else if (strcmp ("taskloop", p) == 0)
32578 if_modifier = OMP_TASKLOOP;
32579 else if (strcmp ("target", p) == 0)
32581 if_modifier = OMP_TARGET;
32582 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32584 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32585 p = IDENTIFIER_POINTER (id);
32586 if (strcmp ("data", p) == 0)
32587 if_modifier = OMP_TARGET_DATA;
32588 else if (strcmp ("update", p) == 0)
32589 if_modifier = OMP_TARGET_UPDATE;
32590 else if (strcmp ("enter", p) == 0)
32591 if_modifier = OMP_TARGET_ENTER_DATA;
32592 else if (strcmp ("exit", p) == 0)
32593 if_modifier = OMP_TARGET_EXIT_DATA;
32594 if (if_modifier != OMP_TARGET)
32595 n = 3;
32596 else
32598 location_t loc
32599 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32600 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32601 "or %<exit%>");
32602 if_modifier = ERROR_MARK;
32604 if (if_modifier == OMP_TARGET_ENTER_DATA
32605 || if_modifier == OMP_TARGET_EXIT_DATA)
32607 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32609 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32610 p = IDENTIFIER_POINTER (id);
32611 if (strcmp ("data", p) == 0)
32612 n = 4;
32614 if (n != 4)
32616 location_t loc
32617 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32618 error_at (loc, "expected %<data%>");
32619 if_modifier = ERROR_MARK;
32624 if (if_modifier != ERROR_MARK)
32626 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32628 while (n-- > 0)
32629 cp_lexer_consume_token (parser->lexer);
32631 else
32633 if (n > 2)
32635 location_t loc
32636 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32637 error_at (loc, "expected %<:%>");
32639 if_modifier = ERROR_MARK;
32644 t = cp_parser_assignment_expression (parser);
32646 if (t == error_mark_node
32647 || !parens.require_close (parser))
32648 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32649 /*or_comma=*/false,
32650 /*consume_paren=*/true);
32652 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32653 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32655 if (if_modifier != ERROR_MARK
32656 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32658 const char *p = NULL;
32659 switch (if_modifier)
32661 case VOID_CST: p = "cancel"; break;
32662 case OMP_PARALLEL: p = "parallel"; break;
32663 case OMP_SIMD: p = "simd"; break;
32664 case OMP_TASK: p = "task"; break;
32665 case OMP_TASKLOOP: p = "taskloop"; break;
32666 case OMP_TARGET_DATA: p = "target data"; break;
32667 case OMP_TARGET: p = "target"; break;
32668 case OMP_TARGET_UPDATE: p = "target update"; break;
32669 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32670 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32671 default: gcc_unreachable ();
32673 error_at (location, "too many %<if%> clauses with %qs modifier",
32675 return list;
32677 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32679 if (!is_omp)
32680 error_at (location, "too many %<if%> clauses");
32681 else
32682 error_at (location, "too many %<if%> clauses without modifier");
32683 return list;
32685 else if (if_modifier == ERROR_MARK
32686 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32688 error_at (location, "if any %<if%> clause has modifier, then all "
32689 "%<if%> clauses have to use modifier");
32690 return list;
32694 c = build_omp_clause (location, OMP_CLAUSE_IF);
32695 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32696 OMP_CLAUSE_IF_EXPR (c) = t;
32697 OMP_CLAUSE_CHAIN (c) = list;
32699 return c;
32702 /* OpenMP 3.1:
32703 mergeable */
32705 static tree
32706 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32707 tree list, location_t location)
32709 tree c;
32711 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32712 location);
32714 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32715 OMP_CLAUSE_CHAIN (c) = list;
32716 return c;
32719 /* OpenMP 2.5:
32720 nowait */
32722 static tree
32723 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32724 tree list, location_t location)
32726 tree c;
32728 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32730 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32731 OMP_CLAUSE_CHAIN (c) = list;
32732 return c;
32735 /* OpenMP 2.5:
32736 num_threads ( expression ) */
32738 static tree
32739 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32740 location_t location)
32742 tree t, c;
32744 matching_parens parens;
32745 if (!parens.require_open (parser))
32746 return list;
32748 t = cp_parser_assignment_expression (parser);
32750 if (t == error_mark_node
32751 || !parens.require_close (parser))
32752 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32753 /*or_comma=*/false,
32754 /*consume_paren=*/true);
32756 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32757 "num_threads", location);
32759 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32760 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32761 OMP_CLAUSE_CHAIN (c) = list;
32763 return c;
32766 /* OpenMP 4.5:
32767 num_tasks ( expression ) */
32769 static tree
32770 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32771 location_t location)
32773 tree t, c;
32775 matching_parens parens;
32776 if (!parens.require_open (parser))
32777 return list;
32779 t = cp_parser_assignment_expression (parser);
32781 if (t == error_mark_node
32782 || !parens.require_close (parser))
32783 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32784 /*or_comma=*/false,
32785 /*consume_paren=*/true);
32787 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32788 "num_tasks", location);
32790 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32791 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32792 OMP_CLAUSE_CHAIN (c) = list;
32794 return c;
32797 /* OpenMP 4.5:
32798 grainsize ( expression ) */
32800 static tree
32801 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32802 location_t location)
32804 tree t, c;
32806 matching_parens parens;
32807 if (!parens.require_open (parser))
32808 return list;
32810 t = cp_parser_assignment_expression (parser);
32812 if (t == error_mark_node
32813 || !parens.require_close (parser))
32814 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32815 /*or_comma=*/false,
32816 /*consume_paren=*/true);
32818 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32819 "grainsize", location);
32821 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32822 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32823 OMP_CLAUSE_CHAIN (c) = list;
32825 return c;
32828 /* OpenMP 4.5:
32829 priority ( expression ) */
32831 static tree
32832 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32833 location_t location)
32835 tree t, c;
32837 matching_parens parens;
32838 if (!parens.require_open (parser))
32839 return list;
32841 t = cp_parser_assignment_expression (parser);
32843 if (t == error_mark_node
32844 || !parens.require_close (parser))
32845 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32846 /*or_comma=*/false,
32847 /*consume_paren=*/true);
32849 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32850 "priority", location);
32852 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32853 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32854 OMP_CLAUSE_CHAIN (c) = list;
32856 return c;
32859 /* OpenMP 4.5:
32860 hint ( expression ) */
32862 static tree
32863 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
32865 tree t, c;
32867 matching_parens parens;
32868 if (!parens.require_open (parser))
32869 return list;
32871 t = cp_parser_assignment_expression (parser);
32873 if (t == error_mark_node
32874 || !parens.require_close (parser))
32875 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32876 /*or_comma=*/false,
32877 /*consume_paren=*/true);
32879 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32881 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32882 OMP_CLAUSE_HINT_EXPR (c) = t;
32883 OMP_CLAUSE_CHAIN (c) = list;
32885 return c;
32888 /* OpenMP 4.5:
32889 defaultmap ( tofrom : scalar )
32891 OpenMP 5.0:
32892 defaultmap ( implicit-behavior [ : variable-category ] ) */
32894 static tree
32895 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32896 location_t location)
32898 tree c, id;
32899 const char *p;
32900 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
32901 enum omp_clause_defaultmap_kind category
32902 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
32904 matching_parens parens;
32905 if (!parens.require_open (parser))
32906 return list;
32908 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
32909 p = "default";
32910 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32912 invalid_behavior:
32913 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
32914 "%<tofrom%>, %<firstprivate%>, %<none%> "
32915 "or %<default%>");
32916 goto out_err;
32918 else
32920 id = cp_lexer_peek_token (parser->lexer)->u.value;
32921 p = IDENTIFIER_POINTER (id);
32924 switch (p[0])
32926 case 'a':
32927 if (strcmp ("alloc", p) == 0)
32928 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
32929 else
32930 goto invalid_behavior;
32931 break;
32933 case 'd':
32934 if (strcmp ("default", p) == 0)
32935 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
32936 else
32937 goto invalid_behavior;
32938 break;
32940 case 'f':
32941 if (strcmp ("firstprivate", p) == 0)
32942 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
32943 else if (strcmp ("from", p) == 0)
32944 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
32945 else
32946 goto invalid_behavior;
32947 break;
32949 case 'n':
32950 if (strcmp ("none", p) == 0)
32951 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
32952 else
32953 goto invalid_behavior;
32954 break;
32956 case 't':
32957 if (strcmp ("tofrom", p) == 0)
32958 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
32959 else if (strcmp ("to", p) == 0)
32960 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
32961 else
32962 goto invalid_behavior;
32963 break;
32965 default:
32966 goto invalid_behavior;
32968 cp_lexer_consume_token (parser->lexer);
32970 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32972 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32973 goto out_err;
32975 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32977 invalid_category:
32978 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
32979 "%<pointer%>");
32980 goto out_err;
32982 id = cp_lexer_peek_token (parser->lexer)->u.value;
32983 p = IDENTIFIER_POINTER (id);
32985 switch (p[0])
32987 case 'a':
32988 if (strcmp ("aggregate", p) == 0)
32989 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
32990 else
32991 goto invalid_category;
32992 break;
32994 case 'p':
32995 if (strcmp ("pointer", p) == 0)
32996 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
32997 else
32998 goto invalid_category;
32999 break;
33001 case 's':
33002 if (strcmp ("scalar", p) == 0)
33003 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
33004 else
33005 goto invalid_category;
33006 break;
33008 default:
33009 goto invalid_category;
33012 cp_lexer_consume_token (parser->lexer);
33014 if (!parens.require_close (parser))
33015 goto out_err;
33017 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33018 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
33019 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33020 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
33021 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
33022 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
33024 enum omp_clause_defaultmap_kind cat = category;
33025 location_t loc = OMP_CLAUSE_LOCATION (c);
33026 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
33027 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
33028 p = NULL;
33029 switch (cat)
33031 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
33032 p = NULL;
33033 break;
33034 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
33035 p = "aggregate";
33036 break;
33037 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
33038 p = "pointer";
33039 break;
33040 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
33041 p = "scalar";
33042 break;
33043 default:
33044 gcc_unreachable ();
33046 if (p)
33047 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33049 else
33050 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33051 "category");
33052 break;
33055 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33056 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33057 OMP_CLAUSE_CHAIN (c) = list;
33058 return c;
33060 out_err:
33061 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33062 /*or_comma=*/false,
33063 /*consume_paren=*/true);
33064 return list;
33067 /* OpenMP 2.5:
33068 ordered
33070 OpenMP 4.5:
33071 ordered ( constant-expression ) */
33073 static tree
33074 cp_parser_omp_clause_ordered (cp_parser *parser,
33075 tree list, location_t location)
33077 tree c, num = NULL_TREE;
33078 HOST_WIDE_INT n;
33080 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33081 "ordered", location);
33083 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33085 matching_parens parens;
33086 parens.consume_open (parser);
33088 num = cp_parser_constant_expression (parser);
33090 if (!parens.require_close (parser))
33091 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33092 /*or_comma=*/false,
33093 /*consume_paren=*/true);
33095 if (num == error_mark_node)
33096 return list;
33097 num = fold_non_dependent_expr (num);
33098 if (!tree_fits_shwi_p (num)
33099 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33100 || (n = tree_to_shwi (num)) <= 0
33101 || (int) n != n)
33103 error_at (location,
33104 "ordered argument needs positive constant integer "
33105 "expression");
33106 return list;
33110 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
33111 OMP_CLAUSE_ORDERED_EXPR (c) = num;
33112 OMP_CLAUSE_CHAIN (c) = list;
33113 return c;
33116 /* OpenMP 2.5:
33117 reduction ( reduction-operator : variable-list )
33119 reduction-operator:
33120 One of: + * - & ^ | && ||
33122 OpenMP 3.1:
33124 reduction-operator:
33125 One of: + * - & ^ | && || min max
33127 OpenMP 4.0:
33129 reduction-operator:
33130 One of: + * - & ^ | && ||
33131 id-expression
33133 OpenMP 5.0:
33134 reduction ( reduction-modifier, reduction-operator : variable-list )
33135 in_reduction ( reduction-operator : variable-list )
33136 task_reduction ( reduction-operator : variable-list ) */
33138 static tree
33139 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
33140 bool is_omp, tree list)
33142 enum tree_code code = ERROR_MARK;
33143 tree nlist, c, id = NULL_TREE;
33144 bool task = false;
33145 bool inscan = false;
33147 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33148 return list;
33150 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
33152 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
33153 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33155 cp_lexer_consume_token (parser->lexer);
33156 cp_lexer_consume_token (parser->lexer);
33158 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33159 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33161 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33162 const char *p = IDENTIFIER_POINTER (id);
33163 if (strcmp (p, "task") == 0)
33164 task = true;
33165 else if (strcmp (p, "inscan") == 0)
33167 inscan = true;
33168 sorry ("%<inscan%> modifier on %<reduction%> clause "
33169 "not supported yet");
33171 if (task || inscan)
33173 cp_lexer_consume_token (parser->lexer);
33174 cp_lexer_consume_token (parser->lexer);
33179 switch (cp_lexer_peek_token (parser->lexer)->type)
33181 case CPP_PLUS: code = PLUS_EXPR; break;
33182 case CPP_MULT: code = MULT_EXPR; break;
33183 case CPP_MINUS: code = MINUS_EXPR; break;
33184 case CPP_AND: code = BIT_AND_EXPR; break;
33185 case CPP_XOR: code = BIT_XOR_EXPR; break;
33186 case CPP_OR: code = BIT_IOR_EXPR; break;
33187 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
33188 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
33189 default: break;
33192 if (code != ERROR_MARK)
33193 cp_lexer_consume_token (parser->lexer);
33194 else
33196 bool saved_colon_corrects_to_scope_p;
33197 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33198 parser->colon_corrects_to_scope_p = false;
33199 id = cp_parser_id_expression (parser, /*template_p=*/false,
33200 /*check_dependency_p=*/true,
33201 /*template_p=*/NULL,
33202 /*declarator_p=*/false,
33203 /*optional_p=*/false);
33204 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33205 if (identifier_p (id))
33207 const char *p = IDENTIFIER_POINTER (id);
33209 if (strcmp (p, "min") == 0)
33210 code = MIN_EXPR;
33211 else if (strcmp (p, "max") == 0)
33212 code = MAX_EXPR;
33213 else if (id == ovl_op_identifier (false, PLUS_EXPR))
33214 code = PLUS_EXPR;
33215 else if (id == ovl_op_identifier (false, MULT_EXPR))
33216 code = MULT_EXPR;
33217 else if (id == ovl_op_identifier (false, MINUS_EXPR))
33218 code = MINUS_EXPR;
33219 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
33220 code = BIT_AND_EXPR;
33221 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
33222 code = BIT_IOR_EXPR;
33223 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
33224 code = BIT_XOR_EXPR;
33225 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
33226 code = TRUTH_ANDIF_EXPR;
33227 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
33228 code = TRUTH_ORIF_EXPR;
33229 id = omp_reduction_id (code, id, NULL_TREE);
33230 tree scope = parser->scope;
33231 if (scope)
33232 id = build_qualified_name (NULL_TREE, scope, id, false);
33233 parser->scope = NULL_TREE;
33234 parser->qualifying_scope = NULL_TREE;
33235 parser->object_scope = NULL_TREE;
33237 else
33239 error ("invalid reduction-identifier");
33240 resync_fail:
33241 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33242 /*or_comma=*/false,
33243 /*consume_paren=*/true);
33244 return list;
33248 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33249 goto resync_fail;
33251 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
33252 NULL);
33253 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33255 OMP_CLAUSE_REDUCTION_CODE (c) = code;
33256 if (task)
33257 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
33258 else if (inscan)
33259 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
33260 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
33263 return nlist;
33266 /* OpenMP 2.5:
33267 schedule ( schedule-kind )
33268 schedule ( schedule-kind , expression )
33270 schedule-kind:
33271 static | dynamic | guided | runtime | auto
33273 OpenMP 4.5:
33274 schedule ( schedule-modifier : schedule-kind )
33275 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33277 schedule-modifier:
33278 simd
33279 monotonic
33280 nonmonotonic */
33282 static tree
33283 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
33285 tree c, t;
33286 int modifiers = 0, nmodifiers = 0;
33288 matching_parens parens;
33289 if (!parens.require_open (parser))
33290 return list;
33292 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
33294 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33296 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33297 const char *p = IDENTIFIER_POINTER (id);
33298 if (strcmp ("simd", p) == 0)
33299 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
33300 else if (strcmp ("monotonic", p) == 0)
33301 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
33302 else if (strcmp ("nonmonotonic", p) == 0)
33303 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
33304 else
33305 break;
33306 cp_lexer_consume_token (parser->lexer);
33307 if (nmodifiers++ == 0
33308 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33309 cp_lexer_consume_token (parser->lexer);
33310 else
33312 cp_parser_require (parser, CPP_COLON, RT_COLON);
33313 break;
33317 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33319 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33320 const char *p = IDENTIFIER_POINTER (id);
33322 switch (p[0])
33324 case 'd':
33325 if (strcmp ("dynamic", p) != 0)
33326 goto invalid_kind;
33327 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
33328 break;
33330 case 'g':
33331 if (strcmp ("guided", p) != 0)
33332 goto invalid_kind;
33333 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
33334 break;
33336 case 'r':
33337 if (strcmp ("runtime", p) != 0)
33338 goto invalid_kind;
33339 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
33340 break;
33342 default:
33343 goto invalid_kind;
33346 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33347 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33348 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33349 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33350 else
33351 goto invalid_kind;
33352 cp_lexer_consume_token (parser->lexer);
33354 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33355 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33356 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33357 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33359 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33360 "specified");
33361 modifiers = 0;
33364 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33366 cp_token *token;
33367 cp_lexer_consume_token (parser->lexer);
33369 token = cp_lexer_peek_token (parser->lexer);
33370 t = cp_parser_assignment_expression (parser);
33372 if (t == error_mark_node)
33373 goto resync_fail;
33374 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
33375 error_at (token->location, "schedule %<runtime%> does not take "
33376 "a %<chunk_size%> parameter");
33377 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
33378 error_at (token->location, "schedule %<auto%> does not take "
33379 "a %<chunk_size%> parameter");
33380 else
33381 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
33383 if (!parens.require_close (parser))
33384 goto resync_fail;
33386 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33387 goto resync_fail;
33389 OMP_CLAUSE_SCHEDULE_KIND (c)
33390 = (enum omp_clause_schedule_kind)
33391 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
33393 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
33394 OMP_CLAUSE_CHAIN (c) = list;
33395 return c;
33397 invalid_kind:
33398 cp_parser_error (parser, "invalid schedule kind");
33399 resync_fail:
33400 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33401 /*or_comma=*/false,
33402 /*consume_paren=*/true);
33403 return list;
33406 /* OpenMP 3.0:
33407 untied */
33409 static tree
33410 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
33411 tree list, location_t location)
33413 tree c;
33415 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
33417 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
33418 OMP_CLAUSE_CHAIN (c) = list;
33419 return c;
33422 /* OpenMP 4.0:
33423 inbranch
33424 notinbranch */
33426 static tree
33427 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33428 tree list, location_t location)
33430 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33431 tree c = build_omp_clause (location, code);
33432 OMP_CLAUSE_CHAIN (c) = list;
33433 return c;
33436 /* OpenMP 4.0:
33437 parallel
33439 sections
33440 taskgroup */
33442 static tree
33443 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33444 enum omp_clause_code code,
33445 tree list, location_t location)
33447 tree c = build_omp_clause (location, code);
33448 OMP_CLAUSE_CHAIN (c) = list;
33449 return c;
33452 /* OpenMP 4.5:
33453 nogroup */
33455 static tree
33456 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33457 tree list, location_t location)
33459 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33460 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33461 OMP_CLAUSE_CHAIN (c) = list;
33462 return c;
33465 /* OpenMP 4.5:
33466 simd
33467 threads */
33469 static tree
33470 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33471 enum omp_clause_code code,
33472 tree list, location_t location)
33474 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33475 tree c = build_omp_clause (location, code);
33476 OMP_CLAUSE_CHAIN (c) = list;
33477 return c;
33480 /* OpenMP 4.0:
33481 num_teams ( expression ) */
33483 static tree
33484 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33485 location_t location)
33487 tree t, c;
33489 matching_parens parens;
33490 if (!parens.require_open (parser))
33491 return list;
33493 t = cp_parser_assignment_expression (parser);
33495 if (t == error_mark_node
33496 || !parens.require_close (parser))
33497 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33498 /*or_comma=*/false,
33499 /*consume_paren=*/true);
33501 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33502 "num_teams", location);
33504 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33505 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33506 OMP_CLAUSE_CHAIN (c) = list;
33508 return c;
33511 /* OpenMP 4.0:
33512 thread_limit ( expression ) */
33514 static tree
33515 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33516 location_t location)
33518 tree t, c;
33520 matching_parens parens;
33521 if (!parens.require_open (parser))
33522 return list;
33524 t = cp_parser_assignment_expression (parser);
33526 if (t == error_mark_node
33527 || !parens.require_close (parser))
33528 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33529 /*or_comma=*/false,
33530 /*consume_paren=*/true);
33532 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33533 "thread_limit", location);
33535 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33536 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33537 OMP_CLAUSE_CHAIN (c) = list;
33539 return c;
33542 /* OpenMP 4.0:
33543 aligned ( variable-list )
33544 aligned ( variable-list : constant-expression ) */
33546 static tree
33547 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33549 tree nlist, c, alignment = NULL_TREE;
33550 bool colon;
33552 matching_parens parens;
33553 if (!parens.require_open (parser))
33554 return list;
33556 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33557 &colon);
33559 if (colon)
33561 alignment = cp_parser_constant_expression (parser);
33563 if (!parens.require_close (parser))
33564 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33565 /*or_comma=*/false,
33566 /*consume_paren=*/true);
33568 if (alignment == error_mark_node)
33569 alignment = NULL_TREE;
33572 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33573 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33575 return nlist;
33578 /* OpenMP 2.5:
33579 lastprivate ( variable-list )
33581 OpenMP 5.0:
33582 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
33584 static tree
33585 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
33587 bool conditional = false;
33589 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33590 return list;
33592 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33593 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
33595 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33596 const char *p = IDENTIFIER_POINTER (id);
33598 if (strcmp ("conditional", p) == 0)
33600 conditional = true;
33601 cp_lexer_consume_token (parser->lexer);
33602 cp_lexer_consume_token (parser->lexer);
33606 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
33607 list, NULL);
33609 if (conditional)
33610 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33611 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
33612 return nlist;
33615 /* OpenMP 4.0:
33616 linear ( variable-list )
33617 linear ( variable-list : expression )
33619 OpenMP 4.5:
33620 linear ( modifier ( variable-list ) )
33621 linear ( modifier ( variable-list ) : expression ) */
33623 static tree
33624 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33625 bool declare_simd)
33627 tree nlist, c, step = integer_one_node;
33628 bool colon;
33629 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33631 matching_parens parens;
33632 if (!parens.require_open (parser))
33633 return list;
33635 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33637 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33638 const char *p = IDENTIFIER_POINTER (id);
33640 if (strcmp ("ref", p) == 0)
33641 kind = OMP_CLAUSE_LINEAR_REF;
33642 else if (strcmp ("val", p) == 0)
33643 kind = OMP_CLAUSE_LINEAR_VAL;
33644 else if (strcmp ("uval", p) == 0)
33645 kind = OMP_CLAUSE_LINEAR_UVAL;
33646 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33647 cp_lexer_consume_token (parser->lexer);
33648 else
33649 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33652 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33653 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33654 &colon);
33655 else
33657 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33658 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33659 if (colon)
33660 cp_parser_require (parser, CPP_COLON, RT_COLON);
33661 else if (!parens.require_close (parser))
33662 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33663 /*or_comma=*/false,
33664 /*consume_paren=*/true);
33667 if (colon)
33669 step = NULL_TREE;
33670 if (declare_simd
33671 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33672 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33674 cp_token *token = cp_lexer_peek_token (parser->lexer);
33675 cp_parser_parse_tentatively (parser);
33676 step = cp_parser_id_expression (parser, /*template_p=*/false,
33677 /*check_dependency_p=*/true,
33678 /*template_p=*/NULL,
33679 /*declarator_p=*/false,
33680 /*optional_p=*/false);
33681 if (step != error_mark_node)
33682 step = cp_parser_lookup_name_simple (parser, step, token->location);
33683 if (step == error_mark_node)
33685 step = NULL_TREE;
33686 cp_parser_abort_tentative_parse (parser);
33688 else if (!cp_parser_parse_definitely (parser))
33689 step = NULL_TREE;
33691 if (!step)
33692 step = cp_parser_assignment_expression (parser);
33694 if (!parens.require_close (parser))
33695 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33696 /*or_comma=*/false,
33697 /*consume_paren=*/true);
33699 if (step == error_mark_node)
33700 return list;
33703 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33705 OMP_CLAUSE_LINEAR_STEP (c) = step;
33706 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33709 return nlist;
33712 /* OpenMP 4.0:
33713 safelen ( constant-expression ) */
33715 static tree
33716 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33717 location_t location)
33719 tree t, c;
33721 matching_parens parens;
33722 if (!parens.require_open (parser))
33723 return list;
33725 t = cp_parser_constant_expression (parser);
33727 if (t == error_mark_node
33728 || !parens.require_close (parser))
33729 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33730 /*or_comma=*/false,
33731 /*consume_paren=*/true);
33733 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33735 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33736 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33737 OMP_CLAUSE_CHAIN (c) = list;
33739 return c;
33742 /* OpenMP 4.0:
33743 simdlen ( constant-expression ) */
33745 static tree
33746 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33747 location_t location)
33749 tree t, c;
33751 matching_parens parens;
33752 if (!parens.require_open (parser))
33753 return list;
33755 t = cp_parser_constant_expression (parser);
33757 if (t == error_mark_node
33758 || !parens.require_close (parser))
33759 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33760 /*or_comma=*/false,
33761 /*consume_paren=*/true);
33763 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33765 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33766 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33767 OMP_CLAUSE_CHAIN (c) = list;
33769 return c;
33772 /* OpenMP 4.5:
33773 vec:
33774 identifier [+/- integer]
33775 vec , identifier [+/- integer]
33778 static tree
33779 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33780 tree list)
33782 tree vec = NULL;
33784 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33786 cp_parser_error (parser, "expected identifier");
33787 return list;
33790 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33792 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33793 tree t, identifier = cp_parser_identifier (parser);
33794 tree addend = NULL;
33796 if (identifier == error_mark_node)
33797 t = error_mark_node;
33798 else
33800 t = cp_parser_lookup_name_simple
33801 (parser, identifier,
33802 cp_lexer_peek_token (parser->lexer)->location);
33803 if (t == error_mark_node)
33804 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33805 id_loc);
33808 bool neg = false;
33809 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33810 neg = true;
33811 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33813 addend = integer_zero_node;
33814 goto add_to_vector;
33816 cp_lexer_consume_token (parser->lexer);
33818 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33820 cp_parser_error (parser, "expected integer");
33821 return list;
33824 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33825 if (TREE_CODE (addend) != INTEGER_CST)
33827 cp_parser_error (parser, "expected integer");
33828 return list;
33830 cp_lexer_consume_token (parser->lexer);
33832 add_to_vector:
33833 if (t != error_mark_node)
33835 vec = tree_cons (addend, t, vec);
33836 if (neg)
33837 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33840 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33841 break;
33843 cp_lexer_consume_token (parser->lexer);
33846 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33848 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33849 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33850 OMP_CLAUSE_DECL (u) = nreverse (vec);
33851 OMP_CLAUSE_CHAIN (u) = list;
33852 return u;
33854 return list;
33857 /* OpenMP 5.0:
33858 iterators ( iterators-definition )
33860 iterators-definition:
33861 iterator-specifier
33862 iterator-specifier , iterators-definition
33864 iterator-specifier:
33865 identifier = range-specification
33866 iterator-type identifier = range-specification
33868 range-specification:
33869 begin : end
33870 begin : end : step */
33872 static tree
33873 cp_parser_omp_iterators (cp_parser *parser)
33875 tree ret = NULL_TREE, *last = &ret;
33876 cp_lexer_consume_token (parser->lexer);
33878 matching_parens parens;
33879 if (!parens.require_open (parser))
33880 return error_mark_node;
33882 bool saved_colon_corrects_to_scope_p
33883 = parser->colon_corrects_to_scope_p;
33884 bool saved_colon_doesnt_start_class_def_p
33885 = parser->colon_doesnt_start_class_def_p;
33889 tree iter_type;
33890 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33891 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
33892 iter_type = integer_type_node;
33893 else
33895 const char *saved_message
33896 = parser->type_definition_forbidden_message;
33897 parser->type_definition_forbidden_message
33898 = G_("types may not be defined in iterator type");
33900 iter_type = cp_parser_type_id (parser);
33902 parser->type_definition_forbidden_message = saved_message;
33905 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33906 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33908 cp_parser_error (parser, "expected identifier");
33909 break;
33912 tree id = cp_parser_identifier (parser);
33913 if (id == error_mark_node)
33914 break;
33916 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33917 break;
33919 parser->colon_corrects_to_scope_p = false;
33920 parser->colon_doesnt_start_class_def_p = true;
33921 tree begin = cp_parser_assignment_expression (parser);
33923 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33924 break;
33926 tree end = cp_parser_assignment_expression (parser);
33928 tree step = integer_one_node;
33929 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
33931 cp_lexer_consume_token (parser->lexer);
33932 step = cp_parser_assignment_expression (parser);
33935 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
33936 DECL_ARTIFICIAL (iter_var) = 1;
33937 DECL_CONTEXT (iter_var) = current_function_decl;
33938 pushdecl (iter_var);
33940 *last = make_tree_vec (6);
33941 TREE_VEC_ELT (*last, 0) = iter_var;
33942 TREE_VEC_ELT (*last, 1) = begin;
33943 TREE_VEC_ELT (*last, 2) = end;
33944 TREE_VEC_ELT (*last, 3) = step;
33945 last = &TREE_CHAIN (*last);
33947 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33949 cp_lexer_consume_token (parser->lexer);
33950 continue;
33952 break;
33954 while (1);
33956 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33957 parser->colon_doesnt_start_class_def_p
33958 = saved_colon_doesnt_start_class_def_p;
33960 if (!parens.require_close (parser))
33961 cp_parser_skip_to_closing_parenthesis (parser,
33962 /*recovering=*/true,
33963 /*or_comma=*/false,
33964 /*consume_paren=*/true);
33966 return ret ? ret : error_mark_node;
33969 /* OpenMP 4.0:
33970 depend ( depend-kind : variable-list )
33972 depend-kind:
33973 in | out | inout
33975 OpenMP 4.5:
33976 depend ( source )
33978 depend ( sink : vec )
33980 OpenMP 5.0:
33981 depend ( depend-modifier , depend-kind: variable-list )
33983 depend-kind:
33984 in | out | inout | mutexinoutset | depobj
33986 depend-modifier:
33987 iterator ( iterators-definition ) */
33989 static tree
33990 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33992 tree nlist, c, iterators = NULL_TREE;
33993 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
33995 matching_parens parens;
33996 if (!parens.require_open (parser))
33997 return list;
34001 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34002 goto invalid_kind;
34004 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34005 const char *p = IDENTIFIER_POINTER (id);
34007 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
34009 begin_scope (sk_omp, NULL);
34010 iterators = cp_parser_omp_iterators (parser);
34011 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
34012 continue;
34014 if (strcmp ("in", p) == 0)
34015 kind = OMP_CLAUSE_DEPEND_IN;
34016 else if (strcmp ("inout", p) == 0)
34017 kind = OMP_CLAUSE_DEPEND_INOUT;
34018 else if (strcmp ("mutexinoutset", p) == 0)
34019 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
34020 else if (strcmp ("out", p) == 0)
34021 kind = OMP_CLAUSE_DEPEND_OUT;
34022 else if (strcmp ("depobj", p) == 0)
34023 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
34024 else if (strcmp ("sink", p) == 0)
34025 kind = OMP_CLAUSE_DEPEND_SINK;
34026 else if (strcmp ("source", p) == 0)
34027 kind = OMP_CLAUSE_DEPEND_SOURCE;
34028 else
34029 goto invalid_kind;
34030 break;
34032 while (1);
34034 cp_lexer_consume_token (parser->lexer);
34036 if (iterators
34037 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
34039 poplevel (0, 1, 0);
34040 error_at (loc, "%<iterator%> modifier incompatible with %qs",
34041 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
34042 iterators = NULL_TREE;
34045 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
34047 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
34048 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34049 OMP_CLAUSE_DECL (c) = NULL_TREE;
34050 OMP_CLAUSE_CHAIN (c) = list;
34051 if (!parens.require_close (parser))
34052 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34053 /*or_comma=*/false,
34054 /*consume_paren=*/true);
34055 return c;
34058 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34059 goto resync_fail;
34061 if (kind == OMP_CLAUSE_DEPEND_SINK)
34062 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34063 else
34065 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34066 list, NULL);
34068 if (iterators)
34070 tree block = poplevel (1, 1, 0);
34071 if (iterators == error_mark_node)
34072 iterators = NULL_TREE;
34073 else
34074 TREE_VEC_ELT (iterators, 5) = block;
34077 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34079 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34080 if (iterators)
34081 OMP_CLAUSE_DECL (c)
34082 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34085 return nlist;
34087 invalid_kind:
34088 cp_parser_error (parser, "invalid depend kind");
34089 resync_fail:
34090 if (iterators)
34091 poplevel (0, 1, 0);
34092 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34093 /*or_comma=*/false,
34094 /*consume_paren=*/true);
34095 return list;
34098 /* OpenMP 4.0:
34099 map ( map-kind : variable-list )
34100 map ( variable-list )
34102 map-kind:
34103 alloc | to | from | tofrom
34105 OpenMP 4.5:
34106 map-kind:
34107 alloc | to | from | tofrom | release | delete
34109 map ( always [,] map-kind: variable-list ) */
34111 static tree
34112 cp_parser_omp_clause_map (cp_parser *parser, tree list)
34114 tree nlist, c;
34115 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
34116 bool always = false;
34118 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34119 return list;
34121 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34123 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34124 const char *p = IDENTIFIER_POINTER (id);
34126 if (strcmp ("always", p) == 0)
34128 int nth = 2;
34129 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
34130 nth++;
34131 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
34132 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
34133 == RID_DELETE))
34134 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
34135 == CPP_COLON))
34137 always = true;
34138 cp_lexer_consume_token (parser->lexer);
34139 if (nth == 3)
34140 cp_lexer_consume_token (parser->lexer);
34145 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34146 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34148 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34149 const char *p = IDENTIFIER_POINTER (id);
34151 if (strcmp ("alloc", p) == 0)
34152 kind = GOMP_MAP_ALLOC;
34153 else if (strcmp ("to", p) == 0)
34154 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
34155 else if (strcmp ("from", p) == 0)
34156 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
34157 else if (strcmp ("tofrom", p) == 0)
34158 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
34159 else if (strcmp ("release", p) == 0)
34160 kind = GOMP_MAP_RELEASE;
34161 else
34163 cp_parser_error (parser, "invalid map kind");
34164 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34165 /*or_comma=*/false,
34166 /*consume_paren=*/true);
34167 return list;
34169 cp_lexer_consume_token (parser->lexer);
34170 cp_lexer_consume_token (parser->lexer);
34172 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
34173 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34175 kind = GOMP_MAP_DELETE;
34176 cp_lexer_consume_token (parser->lexer);
34177 cp_lexer_consume_token (parser->lexer);
34180 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
34181 NULL);
34183 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34184 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34186 return nlist;
34189 /* OpenMP 4.0:
34190 device ( expression ) */
34192 static tree
34193 cp_parser_omp_clause_device (cp_parser *parser, tree list,
34194 location_t location)
34196 tree t, c;
34198 matching_parens parens;
34199 if (!parens.require_open (parser))
34200 return list;
34202 t = cp_parser_assignment_expression (parser);
34204 if (t == error_mark_node
34205 || !parens.require_close (parser))
34206 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34207 /*or_comma=*/false,
34208 /*consume_paren=*/true);
34210 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
34211 "device", location);
34213 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
34214 OMP_CLAUSE_DEVICE_ID (c) = t;
34215 OMP_CLAUSE_CHAIN (c) = list;
34217 return c;
34220 /* OpenMP 4.0:
34221 dist_schedule ( static )
34222 dist_schedule ( static , expression ) */
34224 static tree
34225 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
34226 location_t location)
34228 tree c, t;
34230 matching_parens parens;
34231 if (!parens.require_open (parser))
34232 return list;
34234 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
34236 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34237 goto invalid_kind;
34238 cp_lexer_consume_token (parser->lexer);
34240 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34242 cp_lexer_consume_token (parser->lexer);
34244 t = cp_parser_assignment_expression (parser);
34246 if (t == error_mark_node)
34247 goto resync_fail;
34248 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
34250 if (!parens.require_close (parser))
34251 goto resync_fail;
34253 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34254 goto resync_fail;
34256 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
34257 location);
34258 OMP_CLAUSE_CHAIN (c) = list;
34259 return c;
34261 invalid_kind:
34262 cp_parser_error (parser, "invalid dist_schedule kind");
34263 resync_fail:
34264 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34265 /*or_comma=*/false,
34266 /*consume_paren=*/true);
34267 return list;
34270 /* OpenMP 4.0:
34271 proc_bind ( proc-bind-kind )
34273 proc-bind-kind:
34274 master | close | spread */
34276 static tree
34277 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
34278 location_t location)
34280 tree c;
34281 enum omp_clause_proc_bind_kind kind;
34283 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34284 return list;
34286 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34288 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34289 const char *p = IDENTIFIER_POINTER (id);
34291 if (strcmp ("master", p) == 0)
34292 kind = OMP_CLAUSE_PROC_BIND_MASTER;
34293 else if (strcmp ("close", p) == 0)
34294 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
34295 else if (strcmp ("spread", p) == 0)
34296 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
34297 else
34298 goto invalid_kind;
34300 else
34301 goto invalid_kind;
34303 cp_lexer_consume_token (parser->lexer);
34304 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34305 goto resync_fail;
34307 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
34308 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
34309 location);
34310 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
34311 OMP_CLAUSE_CHAIN (c) = list;
34312 return c;
34314 invalid_kind:
34315 cp_parser_error (parser, "invalid depend kind");
34316 resync_fail:
34317 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34318 /*or_comma=*/false,
34319 /*consume_paren=*/true);
34320 return list;
34323 /* OpenACC:
34324 async [( int-expr )] */
34326 static tree
34327 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
34329 tree c, t;
34330 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34332 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34334 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34336 matching_parens parens;
34337 parens.consume_open (parser);
34339 t = cp_parser_expression (parser);
34340 if (t == error_mark_node
34341 || !parens.require_close (parser))
34342 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34343 /*or_comma=*/false,
34344 /*consume_paren=*/true);
34347 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
34349 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
34350 OMP_CLAUSE_ASYNC_EXPR (c) = t;
34351 OMP_CLAUSE_CHAIN (c) = list;
34352 list = c;
34354 return list;
34357 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34358 is a bitmask in MASK. Return the list of clauses found. */
34360 static tree
34361 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
34362 const char *where, cp_token *pragma_tok,
34363 bool finish_p = true)
34365 tree clauses = NULL;
34366 bool first = true;
34368 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34370 location_t here;
34371 pragma_omp_clause c_kind;
34372 omp_clause_code code;
34373 const char *c_name;
34374 tree prev = clauses;
34376 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34377 cp_lexer_consume_token (parser->lexer);
34379 here = cp_lexer_peek_token (parser->lexer)->location;
34380 c_kind = cp_parser_omp_clause_name (parser);
34382 switch (c_kind)
34384 case PRAGMA_OACC_CLAUSE_ASYNC:
34385 clauses = cp_parser_oacc_clause_async (parser, clauses);
34386 c_name = "async";
34387 break;
34388 case PRAGMA_OACC_CLAUSE_AUTO:
34389 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
34390 clauses, here);
34391 c_name = "auto";
34392 break;
34393 case PRAGMA_OACC_CLAUSE_COLLAPSE:
34394 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
34395 c_name = "collapse";
34396 break;
34397 case PRAGMA_OACC_CLAUSE_COPY:
34398 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34399 c_name = "copy";
34400 break;
34401 case PRAGMA_OACC_CLAUSE_COPYIN:
34402 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34403 c_name = "copyin";
34404 break;
34405 case PRAGMA_OACC_CLAUSE_COPYOUT:
34406 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34407 c_name = "copyout";
34408 break;
34409 case PRAGMA_OACC_CLAUSE_CREATE:
34410 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34411 c_name = "create";
34412 break;
34413 case PRAGMA_OACC_CLAUSE_DELETE:
34414 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34415 c_name = "delete";
34416 break;
34417 case PRAGMA_OMP_CLAUSE_DEFAULT:
34418 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
34419 c_name = "default";
34420 break;
34421 case PRAGMA_OACC_CLAUSE_DEVICE:
34422 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34423 c_name = "device";
34424 break;
34425 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
34426 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
34427 c_name = "deviceptr";
34428 break;
34429 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
34430 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34431 c_name = "device_resident";
34432 break;
34433 case PRAGMA_OACC_CLAUSE_FINALIZE:
34434 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
34435 clauses, here);
34436 c_name = "finalize";
34437 break;
34438 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
34439 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34440 clauses);
34441 c_name = "firstprivate";
34442 break;
34443 case PRAGMA_OACC_CLAUSE_GANG:
34444 c_name = "gang";
34445 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
34446 c_name, clauses);
34447 break;
34448 case PRAGMA_OACC_CLAUSE_HOST:
34449 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34450 c_name = "host";
34451 break;
34452 case PRAGMA_OACC_CLAUSE_IF:
34453 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
34454 c_name = "if";
34455 break;
34456 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
34457 clauses = cp_parser_oacc_simple_clause (parser,
34458 OMP_CLAUSE_IF_PRESENT,
34459 clauses, here);
34460 c_name = "if_present";
34461 break;
34462 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
34463 clauses = cp_parser_oacc_simple_clause (parser,
34464 OMP_CLAUSE_INDEPENDENT,
34465 clauses, here);
34466 c_name = "independent";
34467 break;
34468 case PRAGMA_OACC_CLAUSE_LINK:
34469 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34470 c_name = "link";
34471 break;
34472 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
34473 code = OMP_CLAUSE_NUM_GANGS;
34474 c_name = "num_gangs";
34475 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34476 clauses);
34477 break;
34478 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
34479 c_name = "num_workers";
34480 code = OMP_CLAUSE_NUM_WORKERS;
34481 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34482 clauses);
34483 break;
34484 case PRAGMA_OACC_CLAUSE_PRESENT:
34485 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34486 c_name = "present";
34487 break;
34488 case PRAGMA_OACC_CLAUSE_PRIVATE:
34489 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34490 clauses);
34491 c_name = "private";
34492 break;
34493 case PRAGMA_OACC_CLAUSE_REDUCTION:
34494 clauses
34495 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34496 false, clauses);
34497 c_name = "reduction";
34498 break;
34499 case PRAGMA_OACC_CLAUSE_SEQ:
34500 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
34501 clauses, here);
34502 c_name = "seq";
34503 break;
34504 case PRAGMA_OACC_CLAUSE_TILE:
34505 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
34506 c_name = "tile";
34507 break;
34508 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
34509 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34510 clauses);
34511 c_name = "use_device";
34512 break;
34513 case PRAGMA_OACC_CLAUSE_VECTOR:
34514 c_name = "vector";
34515 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
34516 c_name, clauses);
34517 break;
34518 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
34519 c_name = "vector_length";
34520 code = OMP_CLAUSE_VECTOR_LENGTH;
34521 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34522 clauses);
34523 break;
34524 case PRAGMA_OACC_CLAUSE_WAIT:
34525 clauses = cp_parser_oacc_clause_wait (parser, clauses);
34526 c_name = "wait";
34527 break;
34528 case PRAGMA_OACC_CLAUSE_WORKER:
34529 c_name = "worker";
34530 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
34531 c_name, clauses);
34532 break;
34533 default:
34534 cp_parser_error (parser, "expected %<#pragma acc%> clause");
34535 goto saw_error;
34538 first = false;
34540 if (((mask >> c_kind) & 1) == 0)
34542 /* Remove the invalid clause(s) from the list to avoid
34543 confusing the rest of the compiler. */
34544 clauses = prev;
34545 error_at (here, "%qs is not valid for %qs", c_name, where);
34549 saw_error:
34550 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34552 if (finish_p)
34553 return finish_omp_clauses (clauses, C_ORT_ACC);
34555 return clauses;
34558 /* Parse all OpenMP clauses. The set clauses allowed by the directive
34559 is a bitmask in MASK. Return the list of clauses found; the result
34560 of clause default goes in *pdefault. */
34562 static tree
34563 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
34564 const char *where, cp_token *pragma_tok,
34565 bool finish_p = true)
34567 tree clauses = NULL;
34568 bool first = true;
34569 cp_token *token = NULL;
34571 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34573 pragma_omp_clause c_kind;
34574 const char *c_name;
34575 tree prev = clauses;
34577 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34578 cp_lexer_consume_token (parser->lexer);
34580 token = cp_lexer_peek_token (parser->lexer);
34581 c_kind = cp_parser_omp_clause_name (parser);
34583 switch (c_kind)
34585 case PRAGMA_OMP_CLAUSE_COLLAPSE:
34586 clauses = cp_parser_omp_clause_collapse (parser, clauses,
34587 token->location);
34588 c_name = "collapse";
34589 break;
34590 case PRAGMA_OMP_CLAUSE_COPYIN:
34591 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
34592 c_name = "copyin";
34593 break;
34594 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
34595 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
34596 clauses);
34597 c_name = "copyprivate";
34598 break;
34599 case PRAGMA_OMP_CLAUSE_DEFAULT:
34600 clauses = cp_parser_omp_clause_default (parser, clauses,
34601 token->location, false);
34602 c_name = "default";
34603 break;
34604 case PRAGMA_OMP_CLAUSE_FINAL:
34605 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
34606 c_name = "final";
34607 break;
34608 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
34609 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34610 clauses);
34611 c_name = "firstprivate";
34612 break;
34613 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
34614 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
34615 token->location);
34616 c_name = "grainsize";
34617 break;
34618 case PRAGMA_OMP_CLAUSE_HINT:
34619 clauses = cp_parser_omp_clause_hint (parser, clauses,
34620 token->location);
34621 c_name = "hint";
34622 break;
34623 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
34624 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34625 token->location);
34626 c_name = "defaultmap";
34627 break;
34628 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34629 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34630 clauses);
34631 c_name = "use_device_ptr";
34632 break;
34633 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34634 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34635 clauses);
34636 c_name = "is_device_ptr";
34637 break;
34638 case PRAGMA_OMP_CLAUSE_IF:
34639 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34640 true);
34641 c_name = "if";
34642 break;
34643 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
34644 clauses
34645 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
34646 true, clauses);
34647 c_name = "in_reduction";
34648 break;
34649 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34650 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
34651 c_name = "lastprivate";
34652 break;
34653 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34654 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34655 token->location);
34656 c_name = "mergeable";
34657 break;
34658 case PRAGMA_OMP_CLAUSE_NOWAIT:
34659 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34660 c_name = "nowait";
34661 break;
34662 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34663 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34664 token->location);
34665 c_name = "num_tasks";
34666 break;
34667 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34668 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34669 token->location);
34670 c_name = "num_threads";
34671 break;
34672 case PRAGMA_OMP_CLAUSE_ORDERED:
34673 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34674 token->location);
34675 c_name = "ordered";
34676 break;
34677 case PRAGMA_OMP_CLAUSE_PRIORITY:
34678 clauses = cp_parser_omp_clause_priority (parser, clauses,
34679 token->location);
34680 c_name = "priority";
34681 break;
34682 case PRAGMA_OMP_CLAUSE_PRIVATE:
34683 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34684 clauses);
34685 c_name = "private";
34686 break;
34687 case PRAGMA_OMP_CLAUSE_REDUCTION:
34688 clauses
34689 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34690 true, clauses);
34691 c_name = "reduction";
34692 break;
34693 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34694 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34695 token->location);
34696 c_name = "schedule";
34697 break;
34698 case PRAGMA_OMP_CLAUSE_SHARED:
34699 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34700 clauses);
34701 c_name = "shared";
34702 break;
34703 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
34704 clauses
34705 = cp_parser_omp_clause_reduction (parser,
34706 OMP_CLAUSE_TASK_REDUCTION,
34707 true, clauses);
34708 c_name = "task_reduction";
34709 break;
34710 case PRAGMA_OMP_CLAUSE_UNTIED:
34711 clauses = cp_parser_omp_clause_untied (parser, clauses,
34712 token->location);
34713 c_name = "untied";
34714 break;
34715 case PRAGMA_OMP_CLAUSE_INBRANCH:
34716 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34717 clauses, token->location);
34718 c_name = "inbranch";
34719 break;
34720 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
34721 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
34722 clauses);
34723 c_name = "nontemporal";
34724 break;
34725 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34726 clauses = cp_parser_omp_clause_branch (parser,
34727 OMP_CLAUSE_NOTINBRANCH,
34728 clauses, token->location);
34729 c_name = "notinbranch";
34730 break;
34731 case PRAGMA_OMP_CLAUSE_PARALLEL:
34732 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34733 clauses, token->location);
34734 c_name = "parallel";
34735 if (!first)
34737 clause_not_first:
34738 error_at (token->location, "%qs must be the first clause of %qs",
34739 c_name, where);
34740 clauses = prev;
34742 break;
34743 case PRAGMA_OMP_CLAUSE_FOR:
34744 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34745 clauses, token->location);
34746 c_name = "for";
34747 if (!first)
34748 goto clause_not_first;
34749 break;
34750 case PRAGMA_OMP_CLAUSE_SECTIONS:
34751 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34752 clauses, token->location);
34753 c_name = "sections";
34754 if (!first)
34755 goto clause_not_first;
34756 break;
34757 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34758 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34759 clauses, token->location);
34760 c_name = "taskgroup";
34761 if (!first)
34762 goto clause_not_first;
34763 break;
34764 case PRAGMA_OMP_CLAUSE_LINK:
34765 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34766 c_name = "to";
34767 break;
34768 case PRAGMA_OMP_CLAUSE_TO:
34769 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34770 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34771 clauses);
34772 else
34773 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34774 c_name = "to";
34775 break;
34776 case PRAGMA_OMP_CLAUSE_FROM:
34777 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34778 c_name = "from";
34779 break;
34780 case PRAGMA_OMP_CLAUSE_UNIFORM:
34781 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34782 clauses);
34783 c_name = "uniform";
34784 break;
34785 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34786 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34787 token->location);
34788 c_name = "num_teams";
34789 break;
34790 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34791 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34792 token->location);
34793 c_name = "thread_limit";
34794 break;
34795 case PRAGMA_OMP_CLAUSE_ALIGNED:
34796 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34797 c_name = "aligned";
34798 break;
34799 case PRAGMA_OMP_CLAUSE_LINEAR:
34801 bool declare_simd = false;
34802 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34803 declare_simd = true;
34804 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34806 c_name = "linear";
34807 break;
34808 case PRAGMA_OMP_CLAUSE_DEPEND:
34809 clauses = cp_parser_omp_clause_depend (parser, clauses,
34810 token->location);
34811 c_name = "depend";
34812 break;
34813 case PRAGMA_OMP_CLAUSE_MAP:
34814 clauses = cp_parser_omp_clause_map (parser, clauses);
34815 c_name = "map";
34816 break;
34817 case PRAGMA_OMP_CLAUSE_DEVICE:
34818 clauses = cp_parser_omp_clause_device (parser, clauses,
34819 token->location);
34820 c_name = "device";
34821 break;
34822 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34823 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34824 token->location);
34825 c_name = "dist_schedule";
34826 break;
34827 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34828 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34829 token->location);
34830 c_name = "proc_bind";
34831 break;
34832 case PRAGMA_OMP_CLAUSE_SAFELEN:
34833 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34834 token->location);
34835 c_name = "safelen";
34836 break;
34837 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34838 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34839 token->location);
34840 c_name = "simdlen";
34841 break;
34842 case PRAGMA_OMP_CLAUSE_NOGROUP:
34843 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34844 token->location);
34845 c_name = "nogroup";
34846 break;
34847 case PRAGMA_OMP_CLAUSE_THREADS:
34848 clauses
34849 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34850 clauses, token->location);
34851 c_name = "threads";
34852 break;
34853 case PRAGMA_OMP_CLAUSE_SIMD:
34854 clauses
34855 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34856 clauses, token->location);
34857 c_name = "simd";
34858 break;
34859 default:
34860 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34861 goto saw_error;
34864 first = false;
34866 if (((mask >> c_kind) & 1) == 0)
34868 /* Remove the invalid clause(s) from the list to avoid
34869 confusing the rest of the compiler. */
34870 clauses = prev;
34871 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34874 saw_error:
34875 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34876 if (finish_p)
34878 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34879 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34880 else
34881 return finish_omp_clauses (clauses, C_ORT_OMP);
34883 return clauses;
34886 /* OpenMP 2.5:
34887 structured-block:
34888 statement
34890 In practice, we're also interested in adding the statement to an
34891 outer node. So it is convenient if we work around the fact that
34892 cp_parser_statement calls add_stmt. */
34894 static unsigned
34895 cp_parser_begin_omp_structured_block (cp_parser *parser)
34897 unsigned save = parser->in_statement;
34899 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34900 This preserves the "not within loop or switch" style error messages
34901 for nonsense cases like
34902 void foo() {
34903 #pragma omp single
34904 break;
34907 if (parser->in_statement)
34908 parser->in_statement = IN_OMP_BLOCK;
34910 return save;
34913 static void
34914 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34916 parser->in_statement = save;
34919 static tree
34920 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34922 tree stmt = begin_omp_structured_block ();
34923 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34925 cp_parser_statement (parser, NULL_TREE, false, if_p);
34927 cp_parser_end_omp_structured_block (parser, save);
34928 return finish_omp_structured_block (stmt);
34931 /* OpenMP 2.5:
34932 # pragma omp atomic new-line
34933 expression-stmt
34935 expression-stmt:
34936 x binop= expr | x++ | ++x | x-- | --x
34937 binop:
34938 +, *, -, /, &, ^, |, <<, >>
34940 where x is an lvalue expression with scalar type.
34942 OpenMP 3.1:
34943 # pragma omp atomic new-line
34944 update-stmt
34946 # pragma omp atomic read new-line
34947 read-stmt
34949 # pragma omp atomic write new-line
34950 write-stmt
34952 # pragma omp atomic update new-line
34953 update-stmt
34955 # pragma omp atomic capture new-line
34956 capture-stmt
34958 # pragma omp atomic capture new-line
34959 capture-block
34961 read-stmt:
34962 v = x
34963 write-stmt:
34964 x = expr
34965 update-stmt:
34966 expression-stmt | x = x binop expr
34967 capture-stmt:
34968 v = expression-stmt
34969 capture-block:
34970 { v = x; update-stmt; } | { update-stmt; v = x; }
34972 OpenMP 4.0:
34973 update-stmt:
34974 expression-stmt | x = x binop expr | x = expr binop x
34975 capture-stmt:
34976 v = update-stmt
34977 capture-block:
34978 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34980 where x and v are lvalue expressions with scalar type. */
34982 static void
34983 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34985 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34986 tree rhs1 = NULL_TREE, orig_lhs;
34987 location_t loc = pragma_tok->location;
34988 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
34989 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
34990 bool structured_block = false;
34991 bool first = true;
34992 tree clauses = NULL_TREE;
34994 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34996 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34997 cp_lexer_consume_token (parser->lexer);
34999 first = false;
35001 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35003 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35004 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
35005 const char *p = IDENTIFIER_POINTER (id);
35006 enum tree_code new_code = ERROR_MARK;
35007 enum omp_memory_order new_memory_order
35008 = OMP_MEMORY_ORDER_UNSPECIFIED;
35010 if (!strcmp (p, "read"))
35011 new_code = OMP_ATOMIC_READ;
35012 else if (!strcmp (p, "write"))
35013 new_code = NOP_EXPR;
35014 else if (!strcmp (p, "update"))
35015 new_code = OMP_ATOMIC;
35016 else if (!strcmp (p, "capture"))
35017 new_code = OMP_ATOMIC_CAPTURE_NEW;
35018 else if (!strcmp (p, "seq_cst"))
35019 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35020 else if (!strcmp (p, "acq_rel"))
35021 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35022 else if (!strcmp (p, "release"))
35023 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
35024 else if (!strcmp (p, "acquire"))
35025 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35026 else if (!strcmp (p, "relaxed"))
35027 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
35028 else if (!strcmp (p, "hint"))
35030 cp_lexer_consume_token (parser->lexer);
35031 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
35032 continue;
35034 else
35036 p = NULL;
35037 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
35038 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35039 "%<release%>, %<relaxed%> or %<hint%> clause");
35041 if (p)
35043 if (new_code != ERROR_MARK)
35045 if (code != ERROR_MARK)
35046 error_at (cloc, "too many atomic clauses");
35047 else
35048 code = new_code;
35050 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35052 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35053 error_at (cloc, "too many memory order clauses");
35054 else
35055 memory_order = new_memory_order;
35057 cp_lexer_consume_token (parser->lexer);
35058 continue;
35061 break;
35063 cp_parser_require_pragma_eol (parser, pragma_tok);
35065 if (code == ERROR_MARK)
35066 code = OMP_ATOMIC;
35067 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35069 omp_requires_mask
35070 = (enum omp_requires) (omp_requires_mask
35071 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35072 switch ((enum omp_memory_order)
35073 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35075 case OMP_MEMORY_ORDER_UNSPECIFIED:
35076 case OMP_MEMORY_ORDER_RELAXED:
35077 memory_order = OMP_MEMORY_ORDER_RELAXED;
35078 break;
35079 case OMP_MEMORY_ORDER_SEQ_CST:
35080 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35081 break;
35082 case OMP_MEMORY_ORDER_ACQ_REL:
35083 switch (code)
35085 case OMP_ATOMIC_READ:
35086 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35087 break;
35088 case NOP_EXPR: /* atomic write */
35089 case OMP_ATOMIC:
35090 memory_order = OMP_MEMORY_ORDER_RELEASE;
35091 break;
35092 default:
35093 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35094 break;
35096 break;
35097 default:
35098 gcc_unreachable ();
35101 else
35102 switch (code)
35104 case OMP_ATOMIC_READ:
35105 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35106 || memory_order == OMP_MEMORY_ORDER_RELEASE)
35108 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
35109 "%<acq_rel%> or %<release%> clauses");
35110 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35112 break;
35113 case NOP_EXPR: /* atomic write */
35114 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35115 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35117 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
35118 "%<acq_rel%> or %<acquire%> clauses");
35119 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35121 break;
35122 case OMP_ATOMIC:
35123 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35124 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35126 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
35127 "%<acq_rel%> or %<acquire%> clauses");
35128 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35130 break;
35131 default:
35132 break;
35135 switch (code)
35137 case OMP_ATOMIC_READ:
35138 case NOP_EXPR: /* atomic write */
35139 v = cp_parser_unary_expression (parser);
35140 if (v == error_mark_node)
35141 goto saw_error;
35142 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35143 goto saw_error;
35144 if (code == NOP_EXPR)
35145 lhs = cp_parser_expression (parser);
35146 else
35147 lhs = cp_parser_unary_expression (parser);
35148 if (lhs == error_mark_node)
35149 goto saw_error;
35150 if (code == NOP_EXPR)
35152 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35153 opcode. */
35154 code = OMP_ATOMIC;
35155 rhs = lhs;
35156 lhs = v;
35157 v = NULL_TREE;
35159 goto done;
35160 case OMP_ATOMIC_CAPTURE_NEW:
35161 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35163 cp_lexer_consume_token (parser->lexer);
35164 structured_block = true;
35166 else
35168 v = cp_parser_unary_expression (parser);
35169 if (v == error_mark_node)
35170 goto saw_error;
35171 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35172 goto saw_error;
35174 default:
35175 break;
35178 restart:
35179 lhs = cp_parser_unary_expression (parser);
35180 orig_lhs = lhs;
35181 switch (TREE_CODE (lhs))
35183 case ERROR_MARK:
35184 goto saw_error;
35186 case POSTINCREMENT_EXPR:
35187 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35188 code = OMP_ATOMIC_CAPTURE_OLD;
35189 /* FALLTHROUGH */
35190 case PREINCREMENT_EXPR:
35191 lhs = TREE_OPERAND (lhs, 0);
35192 opcode = PLUS_EXPR;
35193 rhs = integer_one_node;
35194 break;
35196 case POSTDECREMENT_EXPR:
35197 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35198 code = OMP_ATOMIC_CAPTURE_OLD;
35199 /* FALLTHROUGH */
35200 case PREDECREMENT_EXPR:
35201 lhs = TREE_OPERAND (lhs, 0);
35202 opcode = MINUS_EXPR;
35203 rhs = integer_one_node;
35204 break;
35206 case COMPOUND_EXPR:
35207 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
35208 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
35209 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
35210 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
35211 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35212 (TREE_OPERAND (lhs, 1), 0), 0)))
35213 == BOOLEAN_TYPE)
35214 /* Undo effects of boolean_increment for post {in,de}crement. */
35215 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
35216 /* FALLTHRU */
35217 case MODIFY_EXPR:
35218 if (TREE_CODE (lhs) == MODIFY_EXPR
35219 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
35221 /* Undo effects of boolean_increment. */
35222 if (integer_onep (TREE_OPERAND (lhs, 1)))
35224 /* This is pre or post increment. */
35225 rhs = TREE_OPERAND (lhs, 1);
35226 lhs = TREE_OPERAND (lhs, 0);
35227 opcode = NOP_EXPR;
35228 if (code == OMP_ATOMIC_CAPTURE_NEW
35229 && !structured_block
35230 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
35231 code = OMP_ATOMIC_CAPTURE_OLD;
35232 break;
35235 /* FALLTHRU */
35236 default:
35237 switch (cp_lexer_peek_token (parser->lexer)->type)
35239 case CPP_MULT_EQ:
35240 opcode = MULT_EXPR;
35241 break;
35242 case CPP_DIV_EQ:
35243 opcode = TRUNC_DIV_EXPR;
35244 break;
35245 case CPP_PLUS_EQ:
35246 opcode = PLUS_EXPR;
35247 break;
35248 case CPP_MINUS_EQ:
35249 opcode = MINUS_EXPR;
35250 break;
35251 case CPP_LSHIFT_EQ:
35252 opcode = LSHIFT_EXPR;
35253 break;
35254 case CPP_RSHIFT_EQ:
35255 opcode = RSHIFT_EXPR;
35256 break;
35257 case CPP_AND_EQ:
35258 opcode = BIT_AND_EXPR;
35259 break;
35260 case CPP_OR_EQ:
35261 opcode = BIT_IOR_EXPR;
35262 break;
35263 case CPP_XOR_EQ:
35264 opcode = BIT_XOR_EXPR;
35265 break;
35266 case CPP_EQ:
35267 enum cp_parser_prec oprec;
35268 cp_token *token;
35269 cp_lexer_consume_token (parser->lexer);
35270 cp_parser_parse_tentatively (parser);
35271 rhs1 = cp_parser_simple_cast_expression (parser);
35272 if (rhs1 == error_mark_node)
35274 cp_parser_abort_tentative_parse (parser);
35275 cp_parser_simple_cast_expression (parser);
35276 goto saw_error;
35278 token = cp_lexer_peek_token (parser->lexer);
35279 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
35281 cp_parser_abort_tentative_parse (parser);
35282 cp_parser_parse_tentatively (parser);
35283 rhs = cp_parser_binary_expression (parser, false, true,
35284 PREC_NOT_OPERATOR, NULL);
35285 if (rhs == error_mark_node)
35287 cp_parser_abort_tentative_parse (parser);
35288 cp_parser_binary_expression (parser, false, true,
35289 PREC_NOT_OPERATOR, NULL);
35290 goto saw_error;
35292 switch (TREE_CODE (rhs))
35294 case MULT_EXPR:
35295 case TRUNC_DIV_EXPR:
35296 case RDIV_EXPR:
35297 case PLUS_EXPR:
35298 case MINUS_EXPR:
35299 case LSHIFT_EXPR:
35300 case RSHIFT_EXPR:
35301 case BIT_AND_EXPR:
35302 case BIT_IOR_EXPR:
35303 case BIT_XOR_EXPR:
35304 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
35306 if (cp_parser_parse_definitely (parser))
35308 opcode = TREE_CODE (rhs);
35309 rhs1 = TREE_OPERAND (rhs, 0);
35310 rhs = TREE_OPERAND (rhs, 1);
35311 goto stmt_done;
35313 else
35314 goto saw_error;
35316 break;
35317 default:
35318 break;
35320 cp_parser_abort_tentative_parse (parser);
35321 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
35323 rhs = cp_parser_expression (parser);
35324 if (rhs == error_mark_node)
35325 goto saw_error;
35326 opcode = NOP_EXPR;
35327 rhs1 = NULL_TREE;
35328 goto stmt_done;
35330 cp_parser_error (parser,
35331 "invalid form of %<#pragma omp atomic%>");
35332 goto saw_error;
35334 if (!cp_parser_parse_definitely (parser))
35335 goto saw_error;
35336 switch (token->type)
35338 case CPP_SEMICOLON:
35339 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35341 code = OMP_ATOMIC_CAPTURE_OLD;
35342 v = lhs;
35343 lhs = NULL_TREE;
35344 lhs1 = rhs1;
35345 rhs1 = NULL_TREE;
35346 cp_lexer_consume_token (parser->lexer);
35347 goto restart;
35349 else if (structured_block)
35351 opcode = NOP_EXPR;
35352 rhs = rhs1;
35353 rhs1 = NULL_TREE;
35354 goto stmt_done;
35356 cp_parser_error (parser,
35357 "invalid form of %<#pragma omp atomic%>");
35358 goto saw_error;
35359 case CPP_MULT:
35360 opcode = MULT_EXPR;
35361 break;
35362 case CPP_DIV:
35363 opcode = TRUNC_DIV_EXPR;
35364 break;
35365 case CPP_PLUS:
35366 opcode = PLUS_EXPR;
35367 break;
35368 case CPP_MINUS:
35369 opcode = MINUS_EXPR;
35370 break;
35371 case CPP_LSHIFT:
35372 opcode = LSHIFT_EXPR;
35373 break;
35374 case CPP_RSHIFT:
35375 opcode = RSHIFT_EXPR;
35376 break;
35377 case CPP_AND:
35378 opcode = BIT_AND_EXPR;
35379 break;
35380 case CPP_OR:
35381 opcode = BIT_IOR_EXPR;
35382 break;
35383 case CPP_XOR:
35384 opcode = BIT_XOR_EXPR;
35385 break;
35386 default:
35387 cp_parser_error (parser,
35388 "invalid operator for %<#pragma omp atomic%>");
35389 goto saw_error;
35391 oprec = TOKEN_PRECEDENCE (token);
35392 gcc_assert (oprec != PREC_NOT_OPERATOR);
35393 if (commutative_tree_code (opcode))
35394 oprec = (enum cp_parser_prec) (oprec - 1);
35395 cp_lexer_consume_token (parser->lexer);
35396 rhs = cp_parser_binary_expression (parser, false, false,
35397 oprec, NULL);
35398 if (rhs == error_mark_node)
35399 goto saw_error;
35400 goto stmt_done;
35401 /* FALLTHROUGH */
35402 default:
35403 cp_parser_error (parser,
35404 "invalid operator for %<#pragma omp atomic%>");
35405 goto saw_error;
35407 cp_lexer_consume_token (parser->lexer);
35409 rhs = cp_parser_expression (parser);
35410 if (rhs == error_mark_node)
35411 goto saw_error;
35412 break;
35414 stmt_done:
35415 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35417 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
35418 goto saw_error;
35419 v = cp_parser_unary_expression (parser);
35420 if (v == error_mark_node)
35421 goto saw_error;
35422 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35423 goto saw_error;
35424 lhs1 = cp_parser_unary_expression (parser);
35425 if (lhs1 == error_mark_node)
35426 goto saw_error;
35428 if (structured_block)
35430 cp_parser_consume_semicolon_at_end_of_statement (parser);
35431 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35433 done:
35434 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
35435 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
35436 rhs1, clauses, memory_order);
35437 if (!structured_block)
35438 cp_parser_consume_semicolon_at_end_of_statement (parser);
35439 return;
35441 saw_error:
35442 cp_parser_skip_to_end_of_block_or_statement (parser);
35443 if (structured_block)
35445 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35446 cp_lexer_consume_token (parser->lexer);
35447 else if (code == OMP_ATOMIC_CAPTURE_NEW)
35449 cp_parser_skip_to_end_of_block_or_statement (parser);
35450 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35451 cp_lexer_consume_token (parser->lexer);
35457 /* OpenMP 2.5:
35458 # pragma omp barrier new-line */
35460 static void
35461 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
35463 cp_parser_require_pragma_eol (parser, pragma_tok);
35464 finish_omp_barrier ();
35467 /* OpenMP 2.5:
35468 # pragma omp critical [(name)] new-line
35469 structured-block
35471 OpenMP 4.5:
35472 # pragma omp critical [(name) [hint(expression)]] new-line
35473 structured-block */
35475 #define OMP_CRITICAL_CLAUSE_MASK \
35476 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35478 static tree
35479 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35481 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
35483 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35485 matching_parens parens;
35486 parens.consume_open (parser);
35488 name = cp_parser_identifier (parser);
35490 if (name == error_mark_node
35491 || !parens.require_close (parser))
35492 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35493 /*or_comma=*/false,
35494 /*consume_paren=*/true);
35495 if (name == error_mark_node)
35496 name = NULL;
35498 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
35499 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
35500 cp_lexer_consume_token (parser->lexer);
35502 clauses = cp_parser_omp_all_clauses (parser,
35503 OMP_CRITICAL_CLAUSE_MASK,
35504 "#pragma omp critical", pragma_tok);
35506 else
35507 cp_parser_require_pragma_eol (parser, pragma_tok);
35509 stmt = cp_parser_omp_structured_block (parser, if_p);
35510 return c_finish_omp_critical (input_location, stmt, name, clauses);
35513 /* OpenMP 5.0:
35514 # pragma omp depobj ( depobj ) depobj-clause new-line
35516 depobj-clause:
35517 depend (dependence-type : locator)
35518 destroy
35519 update (dependence-type)
35521 dependence-type:
35524 inout
35525 mutexinout */
35527 static void
35528 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
35530 location_t loc = pragma_tok->location;
35531 matching_parens parens;
35532 if (!parens.require_open (parser))
35534 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35535 return;
35538 tree depobj = cp_parser_assignment_expression (parser);
35540 if (!parens.require_close (parser))
35541 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35542 /*or_comma=*/false,
35543 /*consume_paren=*/true);
35545 tree clause = NULL_TREE;
35546 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
35547 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
35548 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35550 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35551 const char *p = IDENTIFIER_POINTER (id);
35553 cp_lexer_consume_token (parser->lexer);
35554 if (!strcmp ("depend", p))
35556 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
35557 if (clause)
35558 clause = finish_omp_clauses (clause, C_ORT_OMP);
35559 if (!clause)
35560 clause = error_mark_node;
35562 else if (!strcmp ("destroy", p))
35563 kind = OMP_CLAUSE_DEPEND_LAST;
35564 else if (!strcmp ("update", p))
35566 matching_parens c_parens;
35567 if (c_parens.require_open (parser))
35569 location_t c2_loc
35570 = cp_lexer_peek_token (parser->lexer)->location;
35571 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35573 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
35574 const char *p2 = IDENTIFIER_POINTER (id2);
35576 cp_lexer_consume_token (parser->lexer);
35577 if (!strcmp ("in", p2))
35578 kind = OMP_CLAUSE_DEPEND_IN;
35579 else if (!strcmp ("out", p2))
35580 kind = OMP_CLAUSE_DEPEND_OUT;
35581 else if (!strcmp ("inout", p2))
35582 kind = OMP_CLAUSE_DEPEND_INOUT;
35583 else if (!strcmp ("mutexinoutset", p2))
35584 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
35586 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
35588 clause = error_mark_node;
35589 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
35590 "%<mutexinoutset%>");
35592 if (!c_parens.require_close (parser))
35593 cp_parser_skip_to_closing_parenthesis (parser,
35594 /*recovering=*/true,
35595 /*or_comma=*/false,
35596 /*consume_paren=*/true);
35598 else
35599 clause = error_mark_node;
35602 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
35604 clause = error_mark_node;
35605 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
35607 cp_parser_require_pragma_eol (parser, pragma_tok);
35609 finish_omp_depobj (loc, depobj, kind, clause);
35613 /* OpenMP 2.5:
35614 # pragma omp flush flush-vars[opt] new-line
35616 flush-vars:
35617 ( variable-list )
35619 OpenMP 5.0:
35620 # pragma omp flush memory-order-clause new-line */
35622 static void
35623 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
35625 enum memmodel mo = MEMMODEL_LAST;
35626 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35628 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35629 const char *p = IDENTIFIER_POINTER (id);
35630 if (!strcmp (p, "acq_rel"))
35631 mo = MEMMODEL_ACQ_REL;
35632 else if (!strcmp (p, "release"))
35633 mo = MEMMODEL_RELEASE;
35634 else if (!strcmp (p, "acquire"))
35635 mo = MEMMODEL_ACQUIRE;
35636 else
35637 error_at (cp_lexer_peek_token (parser->lexer)->location,
35638 "expected %<acq_rel%>, %<release%> or %<acquire%>");
35639 cp_lexer_consume_token (parser->lexer);
35641 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35643 if (mo != MEMMODEL_LAST)
35644 error_at (cp_lexer_peek_token (parser->lexer)->location,
35645 "%<flush%> list specified together with memory order "
35646 "clause");
35647 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35649 cp_parser_require_pragma_eol (parser, pragma_tok);
35651 finish_omp_flush (mo);
35654 /* Helper function, to parse omp for increment expression. */
35656 static tree
35657 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
35659 tree cond = cp_parser_binary_expression (parser, false, true,
35660 PREC_NOT_OPERATOR, NULL);
35661 if (cond == error_mark_node
35662 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35664 cp_parser_skip_to_end_of_statement (parser);
35665 return error_mark_node;
35668 switch (TREE_CODE (cond))
35670 case GT_EXPR:
35671 case GE_EXPR:
35672 case LT_EXPR:
35673 case LE_EXPR:
35674 break;
35675 case NE_EXPR:
35676 if (code != OACC_LOOP)
35677 break;
35678 gcc_fallthrough ();
35679 default:
35680 return error_mark_node;
35683 /* If decl is an iterator, preserve LHS and RHS of the relational
35684 expr until finish_omp_for. */
35685 if (decl
35686 && (type_dependent_expression_p (decl)
35687 || CLASS_TYPE_P (TREE_TYPE (decl))))
35688 return cond;
35690 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
35691 TREE_CODE (cond),
35692 TREE_OPERAND (cond, 0), ERROR_MARK,
35693 TREE_OPERAND (cond, 1), ERROR_MARK,
35694 /*overload=*/NULL, tf_warning_or_error);
35697 /* Helper function, to parse omp for increment expression. */
35699 static tree
35700 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
35702 cp_token *token = cp_lexer_peek_token (parser->lexer);
35703 enum tree_code op;
35704 tree lhs, rhs;
35705 cp_id_kind idk;
35706 bool decl_first;
35708 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
35710 op = (token->type == CPP_PLUS_PLUS
35711 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
35712 cp_lexer_consume_token (parser->lexer);
35713 lhs = cp_parser_simple_cast_expression (parser);
35714 if (lhs != decl
35715 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
35716 return error_mark_node;
35717 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
35720 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
35721 if (lhs != decl
35722 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
35723 return error_mark_node;
35725 token = cp_lexer_peek_token (parser->lexer);
35726 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
35728 op = (token->type == CPP_PLUS_PLUS
35729 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
35730 cp_lexer_consume_token (parser->lexer);
35731 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
35734 op = cp_parser_assignment_operator_opt (parser);
35735 if (op == ERROR_MARK)
35736 return error_mark_node;
35738 if (op != NOP_EXPR)
35740 rhs = cp_parser_assignment_expression (parser);
35741 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
35742 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35745 lhs = cp_parser_binary_expression (parser, false, false,
35746 PREC_ADDITIVE_EXPRESSION, NULL);
35747 token = cp_lexer_peek_token (parser->lexer);
35748 decl_first = (lhs == decl
35749 || (processing_template_decl && cp_tree_equal (lhs, decl)));
35750 if (decl_first)
35751 lhs = NULL_TREE;
35752 if (token->type != CPP_PLUS
35753 && token->type != CPP_MINUS)
35754 return error_mark_node;
35758 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
35759 cp_lexer_consume_token (parser->lexer);
35760 rhs = cp_parser_binary_expression (parser, false, false,
35761 PREC_ADDITIVE_EXPRESSION, NULL);
35762 token = cp_lexer_peek_token (parser->lexer);
35763 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
35765 if (lhs == NULL_TREE)
35767 if (op == PLUS_EXPR)
35768 lhs = rhs;
35769 else
35770 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
35771 tf_warning_or_error);
35773 else
35774 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
35775 ERROR_MARK, NULL, tf_warning_or_error);
35778 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
35780 if (!decl_first)
35782 if ((rhs != decl
35783 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
35784 || op == MINUS_EXPR)
35785 return error_mark_node;
35786 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
35788 else
35789 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
35791 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35794 /* Parse the initialization statement of an OpenMP for loop.
35796 Return true if the resulting construct should have an
35797 OMP_CLAUSE_PRIVATE added to it. */
35799 static tree
35800 cp_parser_omp_for_loop_init (cp_parser *parser,
35801 tree &this_pre_body,
35802 vec<tree, va_gc> *&for_block,
35803 tree &init,
35804 tree &orig_init,
35805 tree &decl,
35806 tree &real_decl)
35808 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35809 return NULL_TREE;
35811 tree add_private_clause = NULL_TREE;
35813 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
35815 init-expr:
35816 var = lb
35817 integer-type var = lb
35818 random-access-iterator-type var = lb
35819 pointer-type var = lb
35821 cp_decl_specifier_seq type_specifiers;
35823 /* First, try to parse as an initialized declaration. See
35824 cp_parser_condition, from whence the bulk of this is copied. */
35826 cp_parser_parse_tentatively (parser);
35827 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
35828 /*is_trailing_return=*/false,
35829 &type_specifiers);
35830 if (cp_parser_parse_definitely (parser))
35832 /* If parsing a type specifier seq succeeded, then this
35833 MUST be a initialized declaration. */
35834 tree asm_specification, attributes;
35835 cp_declarator *declarator;
35837 declarator = cp_parser_declarator (parser,
35838 CP_PARSER_DECLARATOR_NAMED,
35839 /*ctor_dtor_or_conv_p=*/NULL,
35840 /*parenthesized_p=*/NULL,
35841 /*member_p=*/false,
35842 /*friend_p=*/false);
35843 attributes = cp_parser_attributes_opt (parser);
35844 asm_specification = cp_parser_asm_specification_opt (parser);
35846 if (declarator == cp_error_declarator)
35847 cp_parser_skip_to_end_of_statement (parser);
35849 else
35851 tree pushed_scope, auto_node;
35853 decl = start_decl (declarator, &type_specifiers,
35854 SD_INITIALIZED, attributes,
35855 /*prefix_attributes=*/NULL_TREE,
35856 &pushed_scope);
35858 auto_node = type_uses_auto (TREE_TYPE (decl));
35859 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
35861 if (cp_lexer_next_token_is (parser->lexer,
35862 CPP_OPEN_PAREN))
35863 error ("parenthesized initialization is not allowed in "
35864 "OpenMP %<for%> loop");
35865 else
35866 /* Trigger an error. */
35867 cp_parser_require (parser, CPP_EQ, RT_EQ);
35869 init = error_mark_node;
35870 cp_parser_skip_to_end_of_statement (parser);
35872 else if (CLASS_TYPE_P (TREE_TYPE (decl))
35873 || type_dependent_expression_p (decl)
35874 || auto_node)
35876 bool is_direct_init, is_non_constant_init;
35878 init = cp_parser_initializer (parser,
35879 &is_direct_init,
35880 &is_non_constant_init);
35882 if (auto_node)
35884 TREE_TYPE (decl)
35885 = do_auto_deduction (TREE_TYPE (decl), init,
35886 auto_node);
35888 if (!CLASS_TYPE_P (TREE_TYPE (decl))
35889 && !type_dependent_expression_p (decl))
35890 goto non_class;
35893 cp_finish_decl (decl, init, !is_non_constant_init,
35894 asm_specification,
35895 LOOKUP_ONLYCONVERTING);
35896 orig_init = init;
35897 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35899 vec_safe_push (for_block, this_pre_body);
35900 init = NULL_TREE;
35902 else
35904 init = pop_stmt_list (this_pre_body);
35905 if (init && TREE_CODE (init) == STATEMENT_LIST)
35907 tree_stmt_iterator i = tsi_start (init);
35908 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35909 while (!tsi_end_p (i))
35911 tree t = tsi_stmt (i);
35912 if (TREE_CODE (t) == DECL_EXPR
35913 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35915 tsi_delink (&i);
35916 vec_safe_push (for_block, t);
35917 continue;
35919 break;
35921 if (tsi_one_before_end_p (i))
35923 tree t = tsi_stmt (i);
35924 tsi_delink (&i);
35925 free_stmt_list (init);
35926 init = t;
35930 this_pre_body = NULL_TREE;
35932 else
35934 /* Consume '='. */
35935 cp_lexer_consume_token (parser->lexer);
35936 init = cp_parser_assignment_expression (parser);
35938 non_class:
35939 if (TYPE_REF_P (TREE_TYPE (decl)))
35940 init = error_mark_node;
35941 else
35942 cp_finish_decl (decl, NULL_TREE,
35943 /*init_const_expr_p=*/false,
35944 asm_specification,
35945 LOOKUP_ONLYCONVERTING);
35948 if (pushed_scope)
35949 pop_scope (pushed_scope);
35952 else
35954 cp_id_kind idk;
35955 /* If parsing a type specifier sequence failed, then
35956 this MUST be a simple expression. */
35957 cp_parser_parse_tentatively (parser);
35958 decl = cp_parser_primary_expression (parser, false, false,
35959 false, &idk);
35960 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35961 if (!cp_parser_error_occurred (parser)
35962 && decl
35963 && (TREE_CODE (decl) == COMPONENT_REF
35964 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35966 cp_parser_abort_tentative_parse (parser);
35967 cp_parser_parse_tentatively (parser);
35968 cp_token *token = cp_lexer_peek_token (parser->lexer);
35969 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35970 /*check_dependency_p=*/true,
35971 /*template_p=*/NULL,
35972 /*declarator_p=*/false,
35973 /*optional_p=*/false);
35974 if (name != error_mark_node
35975 && last_tok == cp_lexer_peek_token (parser->lexer))
35977 decl = cp_parser_lookup_name_simple (parser, name,
35978 token->location);
35979 if (TREE_CODE (decl) == FIELD_DECL)
35980 add_private_clause = omp_privatize_field (decl, false);
35982 cp_parser_abort_tentative_parse (parser);
35983 cp_parser_parse_tentatively (parser);
35984 decl = cp_parser_primary_expression (parser, false, false,
35985 false, &idk);
35987 if (!cp_parser_error_occurred (parser)
35988 && decl
35989 && DECL_P (decl)
35990 && CLASS_TYPE_P (TREE_TYPE (decl)))
35992 tree rhs;
35994 cp_parser_parse_definitely (parser);
35995 cp_parser_require (parser, CPP_EQ, RT_EQ);
35996 rhs = cp_parser_assignment_expression (parser);
35997 orig_init = rhs;
35998 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35999 decl, NOP_EXPR,
36000 rhs,
36001 tf_warning_or_error));
36002 if (!add_private_clause)
36003 add_private_clause = decl;
36005 else
36007 decl = NULL;
36008 cp_parser_abort_tentative_parse (parser);
36009 init = cp_parser_expression (parser);
36010 if (init)
36012 if (TREE_CODE (init) == MODIFY_EXPR
36013 || TREE_CODE (init) == MODOP_EXPR)
36014 real_decl = TREE_OPERAND (init, 0);
36018 return add_private_clause;
36021 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36023 void
36024 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
36025 tree &decl, tree &orig_decl, tree &init,
36026 tree &orig_init, tree &cond, tree &incr)
36028 tree begin, end, range_temp_decl = NULL_TREE;
36029 tree iter_type, begin_expr, end_expr;
36031 if (processing_template_decl)
36033 if (check_for_bare_parameter_packs (init))
36034 init = error_mark_node;
36035 if (!type_dependent_expression_p (init)
36036 /* do_auto_deduction doesn't mess with template init-lists. */
36037 && !BRACE_ENCLOSED_INITIALIZER_P (init))
36039 tree d = decl;
36040 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
36042 tree v = DECL_VALUE_EXPR (decl);
36043 if (TREE_CODE (v) == ARRAY_REF
36044 && VAR_P (TREE_OPERAND (v, 0))
36045 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36046 d = TREE_OPERAND (v, 0);
36048 do_range_for_auto_deduction (d, init);
36050 cond = global_namespace;
36051 incr = NULL_TREE;
36052 orig_init = init;
36053 if (this_pre_body)
36054 this_pre_body = pop_stmt_list (this_pre_body);
36055 return;
36058 init = mark_lvalue_use (init);
36060 if (decl == error_mark_node || init == error_mark_node)
36061 /* If an error happened previously do nothing or else a lot of
36062 unhelpful errors would be issued. */
36063 begin_expr = end_expr = iter_type = error_mark_node;
36064 else
36066 tree range_temp;
36068 if (VAR_P (init)
36069 && array_of_runtime_bound_p (TREE_TYPE (init)))
36070 /* Can't bind a reference to an array of runtime bound. */
36071 range_temp = init;
36072 else
36074 range_temp = build_range_temp (init);
36075 DECL_NAME (range_temp) = NULL_TREE;
36076 pushdecl (range_temp);
36077 cp_finish_decl (range_temp, init,
36078 /*is_constant_init*/false, NULL_TREE,
36079 LOOKUP_ONLYCONVERTING);
36080 range_temp_decl = range_temp;
36081 range_temp = convert_from_reference (range_temp);
36083 iter_type = cp_parser_perform_range_for_lookup (range_temp,
36084 &begin_expr, &end_expr);
36087 tree end_iter_type = iter_type;
36088 if (cxx_dialect >= cxx17)
36089 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36090 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
36091 TREE_USED (end) = 1;
36092 DECL_ARTIFICIAL (end) = 1;
36093 pushdecl (end);
36094 cp_finish_decl (end, end_expr,
36095 /*is_constant_init*/false, NULL_TREE,
36096 LOOKUP_ONLYCONVERTING);
36098 /* The new for initialization statement. */
36099 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
36100 TREE_USED (begin) = 1;
36101 DECL_ARTIFICIAL (begin) = 1;
36102 pushdecl (begin);
36103 orig_init = init;
36104 if (CLASS_TYPE_P (iter_type))
36105 init = NULL_TREE;
36106 else
36108 init = begin_expr;
36109 begin_expr = NULL_TREE;
36111 cp_finish_decl (begin, begin_expr,
36112 /*is_constant_init*/false, NULL_TREE,
36113 LOOKUP_ONLYCONVERTING);
36115 /* The new for condition. */
36116 if (CLASS_TYPE_P (iter_type))
36117 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
36118 else
36119 cond = build_x_binary_op (input_location, NE_EXPR,
36120 begin, ERROR_MARK,
36121 end, ERROR_MARK,
36122 NULL, tf_warning_or_error);
36124 /* The new increment expression. */
36125 if (CLASS_TYPE_P (iter_type))
36126 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
36127 else
36128 incr = finish_unary_op_expr (input_location,
36129 PREINCREMENT_EXPR, begin,
36130 tf_warning_or_error);
36132 orig_decl = decl;
36133 decl = begin;
36134 if (for_block)
36136 vec_safe_push (for_block, this_pre_body);
36137 this_pre_body = NULL_TREE;
36140 tree decomp_first_name = NULL_TREE;
36141 unsigned decomp_cnt = 0;
36142 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
36144 tree v = DECL_VALUE_EXPR (orig_decl);
36145 if (TREE_CODE (v) == ARRAY_REF
36146 && VAR_P (TREE_OPERAND (v, 0))
36147 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36149 tree d = orig_decl;
36150 orig_decl = TREE_OPERAND (v, 0);
36151 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
36152 decomp_first_name = d;
36156 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
36157 if (auto_node)
36159 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36160 tf_none);
36161 if (!error_operand_p (t))
36162 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
36163 t, auto_node);
36166 tree v = make_tree_vec (decomp_cnt + 3);
36167 TREE_VEC_ELT (v, 0) = range_temp_decl;
36168 TREE_VEC_ELT (v, 1) = end;
36169 TREE_VEC_ELT (v, 2) = orig_decl;
36170 for (unsigned i = 0; i < decomp_cnt; i++)
36172 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
36173 decomp_first_name = DECL_CHAIN (decomp_first_name);
36175 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
36178 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36179 inside of the collapsed body. */
36181 void
36182 cp_finish_omp_range_for (tree orig, tree begin)
36184 gcc_assert (TREE_CODE (orig) == TREE_LIST
36185 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
36186 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
36187 tree decomp_first_name = NULL_TREE;
36188 unsigned int decomp_cnt = 0;
36190 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36192 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
36193 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
36194 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
36197 /* The declaration is initialized with *__begin inside the loop body. */
36198 cp_finish_decl (decl,
36199 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36200 tf_warning_or_error),
36201 /*is_constant_init*/false, NULL_TREE,
36202 LOOKUP_ONLYCONVERTING);
36203 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36204 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
36207 /* Parse the restricted form of the for statement allowed by OpenMP. */
36209 static tree
36210 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
36211 tree *cclauses, bool *if_p)
36213 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
36214 tree orig_decl;
36215 tree real_decl, initv, condv, incrv, declv, orig_declv;
36216 tree this_pre_body, cl, ordered_cl = NULL_TREE;
36217 location_t loc_first;
36218 bool collapse_err = false;
36219 int i, collapse = 1, ordered = 0, count, nbraces = 0;
36220 vec<tree, va_gc> *for_block = make_tree_vector ();
36221 auto_vec<tree, 4> orig_inits;
36222 bool tiling = false;
36224 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
36225 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
36226 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
36227 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
36229 tiling = true;
36230 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
36232 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
36233 && OMP_CLAUSE_ORDERED_EXPR (cl))
36235 ordered_cl = cl;
36236 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
36239 if (ordered && ordered < collapse)
36241 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36242 "%<ordered%> clause parameter is less than %<collapse%>");
36243 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
36244 = build_int_cst (NULL_TREE, collapse);
36245 ordered = collapse;
36247 if (ordered)
36249 for (tree *pc = &clauses; *pc; )
36250 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
36252 error_at (OMP_CLAUSE_LOCATION (*pc),
36253 "%<linear%> clause may not be specified together "
36254 "with %<ordered%> clause with a parameter");
36255 *pc = OMP_CLAUSE_CHAIN (*pc);
36257 else
36258 pc = &OMP_CLAUSE_CHAIN (*pc);
36261 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
36262 count = ordered ? ordered : collapse;
36264 declv = make_tree_vec (count);
36265 initv = make_tree_vec (count);
36266 condv = make_tree_vec (count);
36267 incrv = make_tree_vec (count);
36268 orig_declv = NULL_TREE;
36270 loc_first = cp_lexer_peek_token (parser->lexer)->location;
36272 for (i = 0; i < count; i++)
36274 int bracecount = 0;
36275 tree add_private_clause = NULL_TREE;
36276 location_t loc;
36278 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36280 if (!collapse_err)
36281 cp_parser_error (parser, "for statement expected");
36282 return NULL;
36284 loc = cp_lexer_consume_token (parser->lexer)->location;
36286 matching_parens parens;
36287 if (!parens.require_open (parser))
36288 return NULL;
36290 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
36291 this_pre_body = push_stmt_list ();
36293 if (code != OACC_LOOP && cxx_dialect >= cxx11)
36295 /* Save tokens so that we can put them back. */
36296 cp_lexer_save_tokens (parser->lexer);
36298 /* Look for ':' that is not nested in () or {}. */
36299 bool is_range_for
36300 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
36301 /*recovering=*/false,
36302 CPP_COLON,
36303 /*consume_paren=*/
36304 false) == -1);
36306 /* Roll back the tokens we skipped. */
36307 cp_lexer_rollback_tokens (parser->lexer);
36309 if (is_range_for)
36311 bool saved_colon_corrects_to_scope_p
36312 = parser->colon_corrects_to_scope_p;
36314 /* A colon is used in range-based for. */
36315 parser->colon_corrects_to_scope_p = false;
36317 /* Parse the declaration. */
36318 cp_parser_simple_declaration (parser,
36319 /*function_definition_allowed_p=*/
36320 false, &decl);
36321 parser->colon_corrects_to_scope_p
36322 = saved_colon_corrects_to_scope_p;
36324 cp_parser_require (parser, CPP_COLON, RT_COLON);
36326 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
36327 false, 0, true);
36329 cp_convert_omp_range_for (this_pre_body, for_block, decl,
36330 orig_decl, init, orig_init,
36331 cond, incr);
36332 if (this_pre_body)
36334 if (pre_body)
36336 tree t = pre_body;
36337 pre_body = push_stmt_list ();
36338 add_stmt (t);
36339 add_stmt (this_pre_body);
36340 pre_body = pop_stmt_list (pre_body);
36342 else
36343 pre_body = this_pre_body;
36346 if (ordered_cl)
36347 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36348 "%<ordered%> clause with parameter on "
36349 "range-based %<for%> loop");
36351 goto parse_close_paren;
36355 add_private_clause
36356 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
36357 init, orig_init, decl, real_decl);
36359 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36360 if (this_pre_body)
36362 this_pre_body = pop_stmt_list (this_pre_body);
36363 if (pre_body)
36365 tree t = pre_body;
36366 pre_body = push_stmt_list ();
36367 add_stmt (t);
36368 add_stmt (this_pre_body);
36369 pre_body = pop_stmt_list (pre_body);
36371 else
36372 pre_body = this_pre_body;
36375 if (decl)
36376 real_decl = decl;
36377 if (cclauses != NULL
36378 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
36379 && real_decl != NULL_TREE)
36381 tree *c;
36382 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
36383 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
36384 && OMP_CLAUSE_DECL (*c) == real_decl)
36386 error_at (loc, "iteration variable %qD"
36387 " should not be firstprivate", real_decl);
36388 *c = OMP_CLAUSE_CHAIN (*c);
36390 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
36391 && OMP_CLAUSE_DECL (*c) == real_decl)
36393 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36394 tree l = *c;
36395 *c = OMP_CLAUSE_CHAIN (*c);
36396 if (code == OMP_SIMD)
36398 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36399 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
36401 else
36403 OMP_CLAUSE_CHAIN (l) = clauses;
36404 clauses = l;
36406 add_private_clause = NULL_TREE;
36408 else
36410 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
36411 && OMP_CLAUSE_DECL (*c) == real_decl)
36412 add_private_clause = NULL_TREE;
36413 c = &OMP_CLAUSE_CHAIN (*c);
36417 if (add_private_clause)
36419 tree c;
36420 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
36422 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
36423 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
36424 && OMP_CLAUSE_DECL (c) == decl)
36425 break;
36426 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
36427 && OMP_CLAUSE_DECL (c) == decl)
36428 error_at (loc, "iteration variable %qD "
36429 "should not be firstprivate",
36430 decl);
36431 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
36432 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
36433 && OMP_CLAUSE_DECL (c) == decl)
36434 error_at (loc, "iteration variable %qD should not be reduction",
36435 decl);
36437 if (c == NULL)
36439 if (code != OMP_SIMD)
36440 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
36441 else if (collapse == 1)
36442 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
36443 else
36444 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
36445 OMP_CLAUSE_DECL (c) = add_private_clause;
36446 c = finish_omp_clauses (c, C_ORT_OMP);
36447 if (c)
36449 OMP_CLAUSE_CHAIN (c) = clauses;
36450 clauses = c;
36451 /* For linear, signal that we need to fill up
36452 the so far unknown linear step. */
36453 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
36454 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
36459 cond = NULL;
36460 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36461 cond = cp_parser_omp_for_cond (parser, decl, code);
36462 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36464 incr = NULL;
36465 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
36467 /* If decl is an iterator, preserve the operator on decl
36468 until finish_omp_for. */
36469 if (real_decl
36470 && ((processing_template_decl
36471 && (TREE_TYPE (real_decl) == NULL_TREE
36472 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
36473 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
36474 incr = cp_parser_omp_for_incr (parser, real_decl);
36475 else
36476 incr = cp_parser_expression (parser);
36477 if (!EXPR_HAS_LOCATION (incr))
36478 protected_set_expr_location (incr, input_location);
36481 parse_close_paren:
36482 if (!parens.require_close (parser))
36483 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36484 /*or_comma=*/false,
36485 /*consume_paren=*/true);
36487 TREE_VEC_ELT (declv, i) = decl;
36488 TREE_VEC_ELT (initv, i) = init;
36489 TREE_VEC_ELT (condv, i) = cond;
36490 TREE_VEC_ELT (incrv, i) = incr;
36491 if (orig_init)
36493 orig_inits.safe_grow_cleared (i + 1);
36494 orig_inits[i] = orig_init;
36496 if (orig_decl)
36498 if (!orig_declv)
36499 orig_declv = copy_node (declv);
36500 TREE_VEC_ELT (orig_declv, i) = orig_decl;
36502 else if (orig_declv)
36503 TREE_VEC_ELT (orig_declv, i) = decl;
36505 if (i == count - 1)
36506 break;
36508 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
36509 in between the collapsed for loops to be still considered perfectly
36510 nested. Hopefully the final version clarifies this.
36511 For now handle (multiple) {'s and empty statements. */
36512 cp_parser_parse_tentatively (parser);
36513 for (;;)
36515 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36516 break;
36517 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36519 cp_lexer_consume_token (parser->lexer);
36520 bracecount++;
36522 else if (bracecount
36523 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36524 cp_lexer_consume_token (parser->lexer);
36525 else
36527 loc = cp_lexer_peek_token (parser->lexer)->location;
36528 error_at (loc, "not enough for loops to collapse");
36529 collapse_err = true;
36530 cp_parser_abort_tentative_parse (parser);
36531 declv = NULL_TREE;
36532 break;
36536 if (declv)
36538 cp_parser_parse_definitely (parser);
36539 nbraces += bracecount;
36543 if (nbraces)
36544 if_p = NULL;
36546 /* Note that we saved the original contents of this flag when we entered
36547 the structured block, and so we don't need to re-save it here. */
36548 parser->in_statement = IN_OMP_FOR;
36550 /* Note that the grammar doesn't call for a structured block here,
36551 though the loop as a whole is a structured block. */
36552 if (orig_declv)
36554 body = begin_omp_structured_block ();
36555 for (i = 0; i < count; i++)
36556 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
36557 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
36558 TREE_VEC_ELT (declv, i));
36560 else
36561 body = push_stmt_list ();
36562 cp_parser_statement (parser, NULL_TREE, false, if_p);
36563 if (orig_declv)
36564 body = finish_omp_structured_block (body);
36565 else
36566 body = pop_stmt_list (body);
36568 if (declv == NULL_TREE)
36569 ret = NULL_TREE;
36570 else
36571 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
36572 incrv, body, pre_body, &orig_inits, clauses);
36574 while (nbraces)
36576 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36578 cp_lexer_consume_token (parser->lexer);
36579 nbraces--;
36581 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36582 cp_lexer_consume_token (parser->lexer);
36583 else
36585 if (!collapse_err)
36587 error_at (cp_lexer_peek_token (parser->lexer)->location,
36588 "collapsed loops not perfectly nested");
36590 collapse_err = true;
36591 cp_parser_statement_seq_opt (parser, NULL);
36592 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
36593 break;
36597 while (!for_block->is_empty ())
36599 tree t = for_block->pop ();
36600 if (TREE_CODE (t) == STATEMENT_LIST)
36601 add_stmt (pop_stmt_list (t));
36602 else
36603 add_stmt (t);
36605 release_tree_vector (for_block);
36607 return ret;
36610 /* Helper function for OpenMP parsing, split clauses and call
36611 finish_omp_clauses on each of the set of clauses afterwards. */
36613 static void
36614 cp_omp_split_clauses (location_t loc, enum tree_code code,
36615 omp_clause_mask mask, tree clauses, tree *cclauses)
36617 int i;
36618 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
36619 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
36620 if (cclauses[i])
36621 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
36624 /* OpenMP 4.0:
36625 #pragma omp simd simd-clause[optseq] new-line
36626 for-loop */
36628 #define OMP_SIMD_CLAUSE_MASK \
36629 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
36630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
36640 static tree
36641 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
36642 char *p_name, omp_clause_mask mask, tree *cclauses,
36643 bool *if_p)
36645 tree clauses, sb, ret;
36646 unsigned int save;
36647 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36649 strcat (p_name, " simd");
36650 mask |= OMP_SIMD_CLAUSE_MASK;
36652 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36653 cclauses == NULL);
36654 if (cclauses)
36656 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
36657 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
36658 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
36659 OMP_CLAUSE_ORDERED);
36660 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
36662 error_at (OMP_CLAUSE_LOCATION (c),
36663 "%<ordered%> clause with parameter may not be specified "
36664 "on %qs construct", p_name);
36665 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
36669 keep_next_level (true);
36670 sb = begin_omp_structured_block ();
36671 save = cp_parser_begin_omp_structured_block (parser);
36673 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
36675 cp_parser_end_omp_structured_block (parser, save);
36676 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
36678 return ret;
36681 /* OpenMP 2.5:
36682 #pragma omp for for-clause[optseq] new-line
36683 for-loop
36685 OpenMP 4.0:
36686 #pragma omp for simd for-simd-clause[optseq] new-line
36687 for-loop */
36689 #define OMP_FOR_CLAUSE_MASK \
36690 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
36696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
36697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36700 static tree
36701 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
36702 char *p_name, omp_clause_mask mask, tree *cclauses,
36703 bool *if_p)
36705 tree clauses, sb, ret;
36706 unsigned int save;
36707 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36709 strcat (p_name, " for");
36710 mask |= OMP_FOR_CLAUSE_MASK;
36711 /* parallel for{, simd} disallows nowait clause, but for
36712 target {teams distribute ,}parallel for{, simd} it should be accepted. */
36713 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
36714 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
36715 /* Composite distribute parallel for{, simd} disallows ordered clause. */
36716 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
36717 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
36719 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36721 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36722 const char *p = IDENTIFIER_POINTER (id);
36724 if (strcmp (p, "simd") == 0)
36726 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36727 if (cclauses == NULL)
36728 cclauses = cclauses_buf;
36730 cp_lexer_consume_token (parser->lexer);
36731 if (!flag_openmp) /* flag_openmp_simd */
36732 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36733 cclauses, if_p);
36734 sb = begin_omp_structured_block ();
36735 save = cp_parser_begin_omp_structured_block (parser);
36736 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36737 cclauses, if_p);
36738 cp_parser_end_omp_structured_block (parser, save);
36739 tree body = finish_omp_structured_block (sb);
36740 if (ret == NULL)
36741 return ret;
36742 ret = make_node (OMP_FOR);
36743 TREE_TYPE (ret) = void_type_node;
36744 OMP_FOR_BODY (ret) = body;
36745 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36746 SET_EXPR_LOCATION (ret, loc);
36747 add_stmt (ret);
36748 return ret;
36751 if (!flag_openmp) /* flag_openmp_simd */
36753 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36754 return NULL_TREE;
36757 /* Composite distribute parallel for disallows linear clause. */
36758 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
36759 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
36761 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36762 cclauses == NULL);
36763 if (cclauses)
36765 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
36766 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36769 keep_next_level (true);
36770 sb = begin_omp_structured_block ();
36771 save = cp_parser_begin_omp_structured_block (parser);
36773 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
36775 cp_parser_end_omp_structured_block (parser, save);
36776 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
36778 return ret;
36781 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
36782 omp_clause_mask, tree *, bool *);
36784 /* OpenMP 2.5:
36785 # pragma omp master new-line
36786 structured-block */
36788 static tree
36789 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
36790 char *p_name, omp_clause_mask mask, tree *cclauses,
36791 bool *if_p)
36793 tree clauses, sb, ret;
36794 unsigned int save;
36795 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36797 strcat (p_name, " master");
36799 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36801 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36802 const char *p = IDENTIFIER_POINTER (id);
36804 if (strcmp (p, "taskloop") == 0)
36806 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36807 if (cclauses == NULL)
36808 cclauses = cclauses_buf;
36810 cp_lexer_consume_token (parser->lexer);
36811 if (!flag_openmp) /* flag_openmp_simd */
36812 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
36813 cclauses, if_p);
36814 sb = begin_omp_structured_block ();
36815 save = cp_parser_begin_omp_structured_block (parser);
36816 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
36817 cclauses, if_p);
36818 cp_parser_end_omp_structured_block (parser, save);
36819 tree body = finish_omp_structured_block (sb);
36820 if (ret == NULL)
36821 return ret;
36822 return c_finish_omp_master (loc, body);
36825 if (!flag_openmp) /* flag_openmp_simd */
36827 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36828 return NULL_TREE;
36831 if (cclauses)
36833 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36834 false);
36835 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
36837 else
36838 cp_parser_require_pragma_eol (parser, pragma_tok);
36840 return c_finish_omp_master (loc,
36841 cp_parser_omp_structured_block (parser, if_p));
36844 /* OpenMP 2.5:
36845 # pragma omp ordered new-line
36846 structured-block
36848 OpenMP 4.5:
36849 # pragma omp ordered ordered-clauses new-line
36850 structured-block */
36852 #define OMP_ORDERED_CLAUSE_MASK \
36853 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
36854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
36856 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
36857 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
36859 static bool
36860 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
36861 enum pragma_context context, bool *if_p)
36863 location_t loc = pragma_tok->location;
36865 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36867 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36868 const char *p = IDENTIFIER_POINTER (id);
36870 if (strcmp (p, "depend") == 0)
36872 if (!flag_openmp) /* flag_openmp_simd */
36874 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36875 return false;
36877 if (context == pragma_stmt)
36879 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
36880 "%<depend%> clause may only be used in compound "
36881 "statements");
36882 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36883 return false;
36885 tree clauses
36886 = cp_parser_omp_all_clauses (parser,
36887 OMP_ORDERED_DEPEND_CLAUSE_MASK,
36888 "#pragma omp ordered", pragma_tok);
36889 c_finish_omp_ordered (loc, clauses, NULL_TREE);
36890 return false;
36894 tree clauses
36895 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
36896 "#pragma omp ordered", pragma_tok);
36898 if (!flag_openmp /* flag_openmp_simd */
36899 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
36900 return false;
36902 c_finish_omp_ordered (loc, clauses,
36903 cp_parser_omp_structured_block (parser, if_p));
36904 return true;
36907 /* OpenMP 2.5:
36909 section-scope:
36910 { section-sequence }
36912 section-sequence:
36913 section-directive[opt] structured-block
36914 section-sequence section-directive structured-block */
36916 static tree
36917 cp_parser_omp_sections_scope (cp_parser *parser)
36919 tree stmt, substmt;
36920 bool error_suppress = false;
36921 cp_token *tok;
36923 matching_braces braces;
36924 if (!braces.require_open (parser))
36925 return NULL_TREE;
36927 stmt = push_stmt_list ();
36929 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
36930 != PRAGMA_OMP_SECTION)
36932 substmt = cp_parser_omp_structured_block (parser, NULL);
36933 substmt = build1 (OMP_SECTION, void_type_node, substmt);
36934 add_stmt (substmt);
36937 while (1)
36939 tok = cp_lexer_peek_token (parser->lexer);
36940 if (tok->type == CPP_CLOSE_BRACE)
36941 break;
36942 if (tok->type == CPP_EOF)
36943 break;
36945 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
36947 cp_lexer_consume_token (parser->lexer);
36948 cp_parser_require_pragma_eol (parser, tok);
36949 error_suppress = false;
36951 else if (!error_suppress)
36953 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
36954 error_suppress = true;
36957 substmt = cp_parser_omp_structured_block (parser, NULL);
36958 substmt = build1 (OMP_SECTION, void_type_node, substmt);
36959 add_stmt (substmt);
36961 braces.require_close (parser);
36963 substmt = pop_stmt_list (stmt);
36965 stmt = make_node (OMP_SECTIONS);
36966 TREE_TYPE (stmt) = void_type_node;
36967 OMP_SECTIONS_BODY (stmt) = substmt;
36969 add_stmt (stmt);
36970 return stmt;
36973 /* OpenMP 2.5:
36974 # pragma omp sections sections-clause[optseq] newline
36975 sections-scope */
36977 #define OMP_SECTIONS_CLAUSE_MASK \
36978 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36984 static tree
36985 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
36986 char *p_name, omp_clause_mask mask, tree *cclauses)
36988 tree clauses, ret;
36989 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36991 strcat (p_name, " sections");
36992 mask |= OMP_SECTIONS_CLAUSE_MASK;
36993 if (cclauses)
36994 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
36996 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36997 cclauses == NULL);
36998 if (cclauses)
37000 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
37001 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
37004 ret = cp_parser_omp_sections_scope (parser);
37005 if (ret)
37006 OMP_SECTIONS_CLAUSES (ret) = clauses;
37008 return ret;
37011 /* OpenMP 2.5:
37012 # pragma omp parallel parallel-clause[optseq] new-line
37013 structured-block
37014 # pragma omp parallel for parallel-for-clause[optseq] new-line
37015 structured-block
37016 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37017 structured-block
37019 OpenMP 4.0:
37020 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37021 structured-block */
37023 #define OMP_PARALLEL_CLAUSE_MASK \
37024 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37034 static tree
37035 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
37036 char *p_name, omp_clause_mask mask, tree *cclauses,
37037 bool *if_p)
37039 tree stmt, clauses, block;
37040 unsigned int save;
37041 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37043 strcat (p_name, " parallel");
37044 mask |= OMP_PARALLEL_CLAUSE_MASK;
37045 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37046 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
37047 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
37048 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
37050 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37052 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37053 if (cclauses == NULL)
37054 cclauses = cclauses_buf;
37056 cp_lexer_consume_token (parser->lexer);
37057 if (!flag_openmp) /* flag_openmp_simd */
37058 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37059 if_p);
37060 block = begin_omp_parallel ();
37061 save = cp_parser_begin_omp_structured_block (parser);
37062 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37063 if_p);
37064 cp_parser_end_omp_structured_block (parser, save);
37065 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37066 block);
37067 if (ret == NULL_TREE)
37068 return ret;
37069 OMP_PARALLEL_COMBINED (stmt) = 1;
37070 return stmt;
37072 /* When combined with distribute, parallel has to be followed by for.
37073 #pragma omp target parallel is allowed though. */
37074 else if (cclauses
37075 && (mask & (OMP_CLAUSE_MASK_1
37076 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37078 error_at (loc, "expected %<for%> after %qs", p_name);
37079 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37080 return NULL_TREE;
37082 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37084 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37085 const char *p = IDENTIFIER_POINTER (id);
37086 if (strcmp (p, "master") == 0)
37088 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37089 cclauses = cclauses_buf;
37091 cp_lexer_consume_token (parser->lexer);
37092 block = begin_omp_parallel ();
37093 save = cp_parser_begin_omp_structured_block (parser);
37094 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37095 cclauses, if_p);
37096 cp_parser_end_omp_structured_block (parser, save);
37097 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37098 block);
37099 OMP_PARALLEL_COMBINED (stmt) = 1;
37100 if (ret == NULL_TREE)
37101 return ret;
37102 return stmt;
37104 else if (!flag_openmp) /* flag_openmp_simd */
37106 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37107 return NULL_TREE;
37109 else if (strcmp (p, "sections") == 0)
37111 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37112 cclauses = cclauses_buf;
37114 cp_lexer_consume_token (parser->lexer);
37115 block = begin_omp_parallel ();
37116 save = cp_parser_begin_omp_structured_block (parser);
37117 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
37118 cp_parser_end_omp_structured_block (parser, save);
37119 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37120 block);
37121 OMP_PARALLEL_COMBINED (stmt) = 1;
37122 return stmt;
37125 else if (!flag_openmp) /* flag_openmp_simd */
37127 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37128 return NULL_TREE;
37131 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37132 cclauses == NULL);
37133 if (cclauses)
37135 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
37136 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
37139 block = begin_omp_parallel ();
37140 save = cp_parser_begin_omp_structured_block (parser);
37141 cp_parser_statement (parser, NULL_TREE, false, if_p);
37142 cp_parser_end_omp_structured_block (parser, save);
37143 stmt = finish_omp_parallel (clauses, block);
37144 return stmt;
37147 /* OpenMP 2.5:
37148 # pragma omp single single-clause[optseq] new-line
37149 structured-block */
37151 #define OMP_SINGLE_CLAUSE_MASK \
37152 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37157 static tree
37158 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37160 tree stmt = make_node (OMP_SINGLE);
37161 TREE_TYPE (stmt) = void_type_node;
37162 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37164 OMP_SINGLE_CLAUSES (stmt)
37165 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
37166 "#pragma omp single", pragma_tok);
37167 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37169 return add_stmt (stmt);
37172 /* OpenMP 3.0:
37173 # pragma omp task task-clause[optseq] new-line
37174 structured-block */
37176 #define OMP_TASK_CLAUSE_MASK \
37177 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37189 static tree
37190 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37192 tree clauses, block;
37193 unsigned int save;
37195 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
37196 "#pragma omp task", pragma_tok);
37197 block = begin_omp_task ();
37198 save = cp_parser_begin_omp_structured_block (parser);
37199 cp_parser_statement (parser, NULL_TREE, false, if_p);
37200 cp_parser_end_omp_structured_block (parser, save);
37201 return finish_omp_task (clauses, block);
37204 /* OpenMP 3.0:
37205 # pragma omp taskwait new-line
37207 OpenMP 5.0:
37208 # pragma omp taskwait taskwait-clause[opt] new-line */
37210 #define OMP_TASKWAIT_CLAUSE_MASK \
37211 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37213 static void
37214 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
37216 tree clauses
37217 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
37218 "#pragma omp taskwait", pragma_tok);
37220 if (clauses)
37222 tree stmt = make_node (OMP_TASK);
37223 TREE_TYPE (stmt) = void_node;
37224 OMP_TASK_CLAUSES (stmt) = clauses;
37225 OMP_TASK_BODY (stmt) = NULL_TREE;
37226 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37227 add_stmt (stmt);
37229 else
37230 finish_omp_taskwait ();
37233 /* OpenMP 3.1:
37234 # pragma omp taskyield new-line */
37236 static void
37237 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
37239 cp_parser_require_pragma_eol (parser, pragma_tok);
37240 finish_omp_taskyield ();
37243 /* OpenMP 4.0:
37244 # pragma omp taskgroup new-line
37245 structured-block
37247 OpenMP 5.0:
37248 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37250 #define OMP_TASKGROUP_CLAUSE_MASK \
37251 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37253 static tree
37254 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37256 tree clauses
37257 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
37258 "#pragma omp taskgroup", pragma_tok);
37259 return c_finish_omp_taskgroup (input_location,
37260 cp_parser_omp_structured_block (parser,
37261 if_p),
37262 clauses);
37266 /* OpenMP 2.5:
37267 # pragma omp threadprivate (variable-list) */
37269 static void
37270 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
37272 tree vars;
37274 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37275 cp_parser_require_pragma_eol (parser, pragma_tok);
37277 finish_omp_threadprivate (vars);
37280 /* OpenMP 4.0:
37281 # pragma omp cancel cancel-clause[optseq] new-line */
37283 #define OMP_CANCEL_CLAUSE_MASK \
37284 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37290 static void
37291 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
37293 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
37294 "#pragma omp cancel", pragma_tok);
37295 finish_omp_cancel (clauses);
37298 /* OpenMP 4.0:
37299 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37301 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37302 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37307 static void
37308 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
37309 enum pragma_context context)
37311 tree clauses;
37312 bool point_seen = false;
37314 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37316 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37317 const char *p = IDENTIFIER_POINTER (id);
37319 if (strcmp (p, "point") == 0)
37321 cp_lexer_consume_token (parser->lexer);
37322 point_seen = true;
37325 if (!point_seen)
37327 cp_parser_error (parser, "expected %<point%>");
37328 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37329 return;
37332 if (context != pragma_compound)
37334 if (context == pragma_stmt)
37335 error_at (pragma_tok->location,
37336 "%<#pragma %s%> may only be used in compound statements",
37337 "omp cancellation point");
37338 else
37339 cp_parser_error (parser, "expected declaration specifiers");
37340 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37341 return;
37344 clauses = cp_parser_omp_all_clauses (parser,
37345 OMP_CANCELLATION_POINT_CLAUSE_MASK,
37346 "#pragma omp cancellation point",
37347 pragma_tok);
37348 finish_omp_cancellation_point (clauses);
37351 /* OpenMP 4.0:
37352 #pragma omp distribute distribute-clause[optseq] new-line
37353 for-loop */
37355 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37356 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37362 static tree
37363 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
37364 char *p_name, omp_clause_mask mask, tree *cclauses,
37365 bool *if_p)
37367 tree clauses, sb, ret;
37368 unsigned int save;
37369 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37371 strcat (p_name, " distribute");
37372 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
37374 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37376 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37377 const char *p = IDENTIFIER_POINTER (id);
37378 bool simd = false;
37379 bool parallel = false;
37381 if (strcmp (p, "simd") == 0)
37382 simd = true;
37383 else
37384 parallel = strcmp (p, "parallel") == 0;
37385 if (parallel || simd)
37387 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37388 if (cclauses == NULL)
37389 cclauses = cclauses_buf;
37390 cp_lexer_consume_token (parser->lexer);
37391 if (!flag_openmp) /* flag_openmp_simd */
37393 if (simd)
37394 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37395 cclauses, if_p);
37396 else
37397 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37398 cclauses, if_p);
37400 sb = begin_omp_structured_block ();
37401 save = cp_parser_begin_omp_structured_block (parser);
37402 if (simd)
37403 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37404 cclauses, if_p);
37405 else
37406 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37407 cclauses, if_p);
37408 cp_parser_end_omp_structured_block (parser, save);
37409 tree body = finish_omp_structured_block (sb);
37410 if (ret == NULL)
37411 return ret;
37412 ret = make_node (OMP_DISTRIBUTE);
37413 TREE_TYPE (ret) = void_type_node;
37414 OMP_FOR_BODY (ret) = body;
37415 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37416 SET_EXPR_LOCATION (ret, loc);
37417 add_stmt (ret);
37418 return ret;
37421 if (!flag_openmp) /* flag_openmp_simd */
37423 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37424 return NULL_TREE;
37427 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37428 cclauses == NULL);
37429 if (cclauses)
37431 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
37432 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37435 keep_next_level (true);
37436 sb = begin_omp_structured_block ();
37437 save = cp_parser_begin_omp_structured_block (parser);
37439 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
37441 cp_parser_end_omp_structured_block (parser, save);
37442 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37444 return ret;
37447 /* OpenMP 4.0:
37448 # pragma omp teams teams-clause[optseq] new-line
37449 structured-block */
37451 #define OMP_TEAMS_CLAUSE_MASK \
37452 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37460 static tree
37461 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
37462 char *p_name, omp_clause_mask mask, tree *cclauses,
37463 bool *if_p)
37465 tree clauses, sb, ret;
37466 unsigned int save;
37467 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37469 strcat (p_name, " teams");
37470 mask |= OMP_TEAMS_CLAUSE_MASK;
37472 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37474 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37475 const char *p = IDENTIFIER_POINTER (id);
37476 if (strcmp (p, "distribute") == 0)
37478 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37479 if (cclauses == NULL)
37480 cclauses = cclauses_buf;
37482 cp_lexer_consume_token (parser->lexer);
37483 if (!flag_openmp) /* flag_openmp_simd */
37484 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37485 cclauses, if_p);
37486 keep_next_level (true);
37487 sb = begin_omp_structured_block ();
37488 save = cp_parser_begin_omp_structured_block (parser);
37489 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37490 cclauses, if_p);
37491 cp_parser_end_omp_structured_block (parser, save);
37492 tree body = finish_omp_structured_block (sb);
37493 if (ret == NULL)
37494 return ret;
37495 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37496 ret = make_node (OMP_TEAMS);
37497 TREE_TYPE (ret) = void_type_node;
37498 OMP_TEAMS_CLAUSES (ret) = clauses;
37499 OMP_TEAMS_BODY (ret) = body;
37500 OMP_TEAMS_COMBINED (ret) = 1;
37501 SET_EXPR_LOCATION (ret, loc);
37502 return add_stmt (ret);
37505 if (!flag_openmp) /* flag_openmp_simd */
37507 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37508 return NULL_TREE;
37511 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37512 cclauses == NULL);
37513 if (cclauses)
37515 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
37516 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37519 tree stmt = make_node (OMP_TEAMS);
37520 TREE_TYPE (stmt) = void_type_node;
37521 OMP_TEAMS_CLAUSES (stmt) = clauses;
37522 keep_next_level (true);
37523 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37524 SET_EXPR_LOCATION (stmt, loc);
37526 return add_stmt (stmt);
37529 /* OpenMP 4.0:
37530 # pragma omp target data target-data-clause[optseq] new-line
37531 structured-block */
37533 #define OMP_TARGET_DATA_CLAUSE_MASK \
37534 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
37539 static tree
37540 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37542 tree clauses
37543 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
37544 "#pragma omp target data", pragma_tok);
37545 int map_seen = 0;
37546 for (tree *pc = &clauses; *pc;)
37548 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37549 switch (OMP_CLAUSE_MAP_KIND (*pc))
37551 case GOMP_MAP_TO:
37552 case GOMP_MAP_ALWAYS_TO:
37553 case GOMP_MAP_FROM:
37554 case GOMP_MAP_ALWAYS_FROM:
37555 case GOMP_MAP_TOFROM:
37556 case GOMP_MAP_ALWAYS_TOFROM:
37557 case GOMP_MAP_ALLOC:
37558 map_seen = 3;
37559 break;
37560 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37561 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37562 case GOMP_MAP_ALWAYS_POINTER:
37563 break;
37564 default:
37565 map_seen |= 1;
37566 error_at (OMP_CLAUSE_LOCATION (*pc),
37567 "%<#pragma omp target data%> with map-type other "
37568 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37569 "on %<map%> clause");
37570 *pc = OMP_CLAUSE_CHAIN (*pc);
37571 continue;
37573 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
37574 map_seen = 3;
37575 pc = &OMP_CLAUSE_CHAIN (*pc);
37578 if (map_seen != 3)
37580 if (map_seen == 0)
37581 error_at (pragma_tok->location,
37582 "%<#pragma omp target data%> must contain at least "
37583 "one %<map%> or %<use_device_ptr%> clause");
37584 return NULL_TREE;
37587 tree stmt = make_node (OMP_TARGET_DATA);
37588 TREE_TYPE (stmt) = void_type_node;
37589 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
37591 keep_next_level (true);
37592 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37594 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37595 return add_stmt (stmt);
37598 /* OpenMP 4.5:
37599 # pragma omp target enter data target-enter-data-clause[optseq] new-line
37600 structured-block */
37602 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
37603 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37609 static tree
37610 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
37611 enum pragma_context context)
37613 bool data_seen = false;
37614 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37616 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37617 const char *p = IDENTIFIER_POINTER (id);
37619 if (strcmp (p, "data") == 0)
37621 cp_lexer_consume_token (parser->lexer);
37622 data_seen = true;
37625 if (!data_seen)
37627 cp_parser_error (parser, "expected %<data%>");
37628 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37629 return NULL_TREE;
37632 if (context == pragma_stmt)
37634 error_at (pragma_tok->location,
37635 "%<#pragma %s%> may only be used in compound statements",
37636 "omp target enter data");
37637 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37638 return NULL_TREE;
37641 tree clauses
37642 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
37643 "#pragma omp target enter data", pragma_tok);
37644 int map_seen = 0;
37645 for (tree *pc = &clauses; *pc;)
37647 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37648 switch (OMP_CLAUSE_MAP_KIND (*pc))
37650 case GOMP_MAP_TO:
37651 case GOMP_MAP_ALWAYS_TO:
37652 case GOMP_MAP_ALLOC:
37653 map_seen = 3;
37654 break;
37655 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37656 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37657 case GOMP_MAP_ALWAYS_POINTER:
37658 break;
37659 default:
37660 map_seen |= 1;
37661 error_at (OMP_CLAUSE_LOCATION (*pc),
37662 "%<#pragma omp target enter data%> with map-type other "
37663 "than %<to%> or %<alloc%> on %<map%> clause");
37664 *pc = OMP_CLAUSE_CHAIN (*pc);
37665 continue;
37667 pc = &OMP_CLAUSE_CHAIN (*pc);
37670 if (map_seen != 3)
37672 if (map_seen == 0)
37673 error_at (pragma_tok->location,
37674 "%<#pragma omp target enter data%> must contain at least "
37675 "one %<map%> clause");
37676 return NULL_TREE;
37679 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
37680 TREE_TYPE (stmt) = void_type_node;
37681 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
37682 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37683 return add_stmt (stmt);
37686 /* OpenMP 4.5:
37687 # pragma omp target exit data target-enter-data-clause[optseq] new-line
37688 structured-block */
37690 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
37691 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37697 static tree
37698 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
37699 enum pragma_context context)
37701 bool data_seen = false;
37702 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37704 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37705 const char *p = IDENTIFIER_POINTER (id);
37707 if (strcmp (p, "data") == 0)
37709 cp_lexer_consume_token (parser->lexer);
37710 data_seen = true;
37713 if (!data_seen)
37715 cp_parser_error (parser, "expected %<data%>");
37716 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37717 return NULL_TREE;
37720 if (context == pragma_stmt)
37722 error_at (pragma_tok->location,
37723 "%<#pragma %s%> may only be used in compound statements",
37724 "omp target exit data");
37725 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37726 return NULL_TREE;
37729 tree clauses
37730 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
37731 "#pragma omp target exit data", pragma_tok);
37732 int map_seen = 0;
37733 for (tree *pc = &clauses; *pc;)
37735 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37736 switch (OMP_CLAUSE_MAP_KIND (*pc))
37738 case GOMP_MAP_FROM:
37739 case GOMP_MAP_ALWAYS_FROM:
37740 case GOMP_MAP_RELEASE:
37741 case GOMP_MAP_DELETE:
37742 map_seen = 3;
37743 break;
37744 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37745 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37746 case GOMP_MAP_ALWAYS_POINTER:
37747 break;
37748 default:
37749 map_seen |= 1;
37750 error_at (OMP_CLAUSE_LOCATION (*pc),
37751 "%<#pragma omp target exit data%> with map-type other "
37752 "than %<from%>, %<release%> or %<delete%> on %<map%>"
37753 " clause");
37754 *pc = OMP_CLAUSE_CHAIN (*pc);
37755 continue;
37757 pc = &OMP_CLAUSE_CHAIN (*pc);
37760 if (map_seen != 3)
37762 if (map_seen == 0)
37763 error_at (pragma_tok->location,
37764 "%<#pragma omp target exit data%> must contain at least "
37765 "one %<map%> clause");
37766 return NULL_TREE;
37769 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
37770 TREE_TYPE (stmt) = void_type_node;
37771 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
37772 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37773 return add_stmt (stmt);
37776 /* OpenMP 4.0:
37777 # pragma omp target update target-update-clause[optseq] new-line */
37779 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
37780 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
37781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37787 static bool
37788 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
37789 enum pragma_context context)
37791 if (context == pragma_stmt)
37793 error_at (pragma_tok->location,
37794 "%<#pragma %s%> may only be used in compound statements",
37795 "omp target update");
37796 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37797 return false;
37800 tree clauses
37801 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
37802 "#pragma omp target update", pragma_tok);
37803 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
37804 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
37806 error_at (pragma_tok->location,
37807 "%<#pragma omp target update%> must contain at least one "
37808 "%<from%> or %<to%> clauses");
37809 return false;
37812 tree stmt = make_node (OMP_TARGET_UPDATE);
37813 TREE_TYPE (stmt) = void_type_node;
37814 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
37815 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37816 add_stmt (stmt);
37817 return false;
37820 /* OpenMP 4.0:
37821 # pragma omp target target-clause[optseq] new-line
37822 structured-block */
37824 #define OMP_TARGET_CLAUSE_MASK \
37825 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
37833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
37835 static bool
37836 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
37837 enum pragma_context context, bool *if_p)
37839 tree *pc = NULL, stmt;
37841 if (flag_openmp)
37842 omp_requires_mask
37843 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
37845 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37847 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37848 const char *p = IDENTIFIER_POINTER (id);
37849 enum tree_code ccode = ERROR_MARK;
37851 if (strcmp (p, "teams") == 0)
37852 ccode = OMP_TEAMS;
37853 else if (strcmp (p, "parallel") == 0)
37854 ccode = OMP_PARALLEL;
37855 else if (strcmp (p, "simd") == 0)
37856 ccode = OMP_SIMD;
37857 if (ccode != ERROR_MARK)
37859 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
37860 char p_name[sizeof ("#pragma omp target teams distribute "
37861 "parallel for simd")];
37863 cp_lexer_consume_token (parser->lexer);
37864 strcpy (p_name, "#pragma omp target");
37865 if (!flag_openmp) /* flag_openmp_simd */
37867 tree stmt;
37868 switch (ccode)
37870 case OMP_TEAMS:
37871 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
37872 OMP_TARGET_CLAUSE_MASK,
37873 cclauses, if_p);
37874 break;
37875 case OMP_PARALLEL:
37876 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
37877 OMP_TARGET_CLAUSE_MASK,
37878 cclauses, if_p);
37879 break;
37880 case OMP_SIMD:
37881 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
37882 OMP_TARGET_CLAUSE_MASK,
37883 cclauses, if_p);
37884 break;
37885 default:
37886 gcc_unreachable ();
37888 return stmt != NULL_TREE;
37890 keep_next_level (true);
37891 tree sb = begin_omp_structured_block (), ret;
37892 unsigned save = cp_parser_begin_omp_structured_block (parser);
37893 switch (ccode)
37895 case OMP_TEAMS:
37896 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
37897 OMP_TARGET_CLAUSE_MASK, cclauses,
37898 if_p);
37899 break;
37900 case OMP_PARALLEL:
37901 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
37902 OMP_TARGET_CLAUSE_MASK, cclauses,
37903 if_p);
37904 break;
37905 case OMP_SIMD:
37906 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
37907 OMP_TARGET_CLAUSE_MASK, cclauses,
37908 if_p);
37909 break;
37910 default:
37911 gcc_unreachable ();
37913 cp_parser_end_omp_structured_block (parser, save);
37914 tree body = finish_omp_structured_block (sb);
37915 if (ret == NULL_TREE)
37916 return false;
37917 if (ccode == OMP_TEAMS && !processing_template_decl)
37919 /* For combined target teams, ensure the num_teams and
37920 thread_limit clause expressions are evaluated on the host,
37921 before entering the target construct. */
37922 tree c;
37923 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37924 c; c = OMP_CLAUSE_CHAIN (c))
37925 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
37926 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
37927 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
37929 tree expr = OMP_CLAUSE_OPERAND (c, 0);
37930 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
37931 if (expr == error_mark_node)
37932 continue;
37933 tree tmp = TARGET_EXPR_SLOT (expr);
37934 add_stmt (expr);
37935 OMP_CLAUSE_OPERAND (c, 0) = expr;
37936 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
37937 OMP_CLAUSE_FIRSTPRIVATE);
37938 OMP_CLAUSE_DECL (tc) = tmp;
37939 OMP_CLAUSE_CHAIN (tc)
37940 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
37941 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
37944 tree stmt = make_node (OMP_TARGET);
37945 TREE_TYPE (stmt) = void_type_node;
37946 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
37947 OMP_TARGET_BODY (stmt) = body;
37948 OMP_TARGET_COMBINED (stmt) = 1;
37949 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37950 add_stmt (stmt);
37951 pc = &OMP_TARGET_CLAUSES (stmt);
37952 goto check_clauses;
37954 else if (!flag_openmp) /* flag_openmp_simd */
37956 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37957 return false;
37959 else if (strcmp (p, "data") == 0)
37961 cp_lexer_consume_token (parser->lexer);
37962 cp_parser_omp_target_data (parser, pragma_tok, if_p);
37963 return true;
37965 else if (strcmp (p, "enter") == 0)
37967 cp_lexer_consume_token (parser->lexer);
37968 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
37969 return false;
37971 else if (strcmp (p, "exit") == 0)
37973 cp_lexer_consume_token (parser->lexer);
37974 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
37975 return false;
37977 else if (strcmp (p, "update") == 0)
37979 cp_lexer_consume_token (parser->lexer);
37980 return cp_parser_omp_target_update (parser, pragma_tok, context);
37983 if (!flag_openmp) /* flag_openmp_simd */
37985 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37986 return false;
37989 stmt = make_node (OMP_TARGET);
37990 TREE_TYPE (stmt) = void_type_node;
37992 OMP_TARGET_CLAUSES (stmt)
37993 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
37994 "#pragma omp target", pragma_tok);
37995 pc = &OMP_TARGET_CLAUSES (stmt);
37996 keep_next_level (true);
37997 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37999 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38000 add_stmt (stmt);
38002 check_clauses:
38003 while (*pc)
38005 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38006 switch (OMP_CLAUSE_MAP_KIND (*pc))
38008 case GOMP_MAP_TO:
38009 case GOMP_MAP_ALWAYS_TO:
38010 case GOMP_MAP_FROM:
38011 case GOMP_MAP_ALWAYS_FROM:
38012 case GOMP_MAP_TOFROM:
38013 case GOMP_MAP_ALWAYS_TOFROM:
38014 case GOMP_MAP_ALLOC:
38015 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38016 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38017 case GOMP_MAP_ALWAYS_POINTER:
38018 break;
38019 default:
38020 error_at (OMP_CLAUSE_LOCATION (*pc),
38021 "%<#pragma omp target%> with map-type other "
38022 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38023 "on %<map%> clause");
38024 *pc = OMP_CLAUSE_CHAIN (*pc);
38025 continue;
38027 pc = &OMP_CLAUSE_CHAIN (*pc);
38029 return true;
38032 /* OpenACC 2.0:
38033 # pragma acc cache (variable-list) new-line
38036 static tree
38037 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
38039 tree stmt, clauses;
38041 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
38042 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38044 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
38046 stmt = make_node (OACC_CACHE);
38047 TREE_TYPE (stmt) = void_type_node;
38048 OACC_CACHE_CLAUSES (stmt) = clauses;
38049 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38050 add_stmt (stmt);
38052 return stmt;
38055 /* OpenACC 2.0:
38056 # pragma acc data oacc-data-clause[optseq] new-line
38057 structured-block */
38059 #define OACC_DATA_CLAUSE_MASK \
38060 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38068 static tree
38069 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38071 tree stmt, clauses, block;
38072 unsigned int save;
38074 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
38075 "#pragma acc data", pragma_tok);
38077 block = begin_omp_parallel ();
38078 save = cp_parser_begin_omp_structured_block (parser);
38079 cp_parser_statement (parser, NULL_TREE, false, if_p);
38080 cp_parser_end_omp_structured_block (parser, save);
38081 stmt = finish_oacc_data (clauses, block);
38082 return stmt;
38085 /* OpenACC 2.0:
38086 # pragma acc host_data <clauses> new-line
38087 structured-block */
38089 #define OACC_HOST_DATA_CLAUSE_MASK \
38090 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38092 static tree
38093 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38095 tree stmt, clauses, block;
38096 unsigned int save;
38098 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
38099 "#pragma acc host_data", pragma_tok);
38101 block = begin_omp_parallel ();
38102 save = cp_parser_begin_omp_structured_block (parser);
38103 cp_parser_statement (parser, NULL_TREE, false, if_p);
38104 cp_parser_end_omp_structured_block (parser, save);
38105 stmt = finish_oacc_host_data (clauses, block);
38106 return stmt;
38109 /* OpenACC 2.0:
38110 # pragma acc declare oacc-data-clause[optseq] new-line
38113 #define OACC_DECLARE_CLAUSE_MASK \
38114 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38123 static tree
38124 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
38126 tree clauses, stmt;
38127 bool error = false;
38129 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
38130 "#pragma acc declare", pragma_tok, true);
38133 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38135 error_at (pragma_tok->location,
38136 "no valid clauses specified in %<#pragma acc declare%>");
38137 return NULL_TREE;
38140 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
38142 location_t loc = OMP_CLAUSE_LOCATION (t);
38143 tree decl = OMP_CLAUSE_DECL (t);
38144 if (!DECL_P (decl))
38146 error_at (loc, "array section in %<#pragma acc declare%>");
38147 error = true;
38148 continue;
38150 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
38151 switch (OMP_CLAUSE_MAP_KIND (t))
38153 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38154 case GOMP_MAP_ALLOC:
38155 case GOMP_MAP_TO:
38156 case GOMP_MAP_FORCE_DEVICEPTR:
38157 case GOMP_MAP_DEVICE_RESIDENT:
38158 break;
38160 case GOMP_MAP_LINK:
38161 if (!global_bindings_p ()
38162 && (TREE_STATIC (decl)
38163 || !DECL_EXTERNAL (decl)))
38165 error_at (loc,
38166 "%qD must be a global variable in "
38167 "%<#pragma acc declare link%>",
38168 decl);
38169 error = true;
38170 continue;
38172 break;
38174 default:
38175 if (global_bindings_p ())
38177 error_at (loc, "invalid OpenACC clause at file scope");
38178 error = true;
38179 continue;
38181 if (DECL_EXTERNAL (decl))
38183 error_at (loc,
38184 "invalid use of %<extern%> variable %qD "
38185 "in %<#pragma acc declare%>", decl);
38186 error = true;
38187 continue;
38189 else if (TREE_PUBLIC (decl))
38191 error_at (loc,
38192 "invalid use of %<global%> variable %qD "
38193 "in %<#pragma acc declare%>", decl);
38194 error = true;
38195 continue;
38197 break;
38200 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
38201 || lookup_attribute ("omp declare target link",
38202 DECL_ATTRIBUTES (decl)))
38204 error_at (loc, "variable %qD used more than once with "
38205 "%<#pragma acc declare%>", decl);
38206 error = true;
38207 continue;
38210 if (!error)
38212 tree id;
38214 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
38215 id = get_identifier ("omp declare target link");
38216 else
38217 id = get_identifier ("omp declare target");
38219 DECL_ATTRIBUTES (decl)
38220 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
38221 if (global_bindings_p ())
38223 symtab_node *node = symtab_node::get (decl);
38224 if (node != NULL)
38226 node->offloadable = 1;
38227 if (ENABLE_OFFLOADING)
38229 g->have_offload = true;
38230 if (is_a <varpool_node *> (node))
38231 vec_safe_push (offload_vars, decl);
38238 if (error || global_bindings_p ())
38239 return NULL_TREE;
38241 stmt = make_node (OACC_DECLARE);
38242 TREE_TYPE (stmt) = void_type_node;
38243 OACC_DECLARE_CLAUSES (stmt) = clauses;
38244 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38246 add_stmt (stmt);
38248 return NULL_TREE;
38251 /* OpenACC 2.0:
38252 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38256 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38258 LOC is the location of the #pragma token.
38261 #define OACC_ENTER_DATA_CLAUSE_MASK \
38262 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38268 #define OACC_EXIT_DATA_CLAUSE_MASK \
38269 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38276 static tree
38277 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
38278 bool enter)
38280 location_t loc = pragma_tok->location;
38281 tree stmt, clauses;
38282 const char *p = "";
38284 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38285 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38287 if (strcmp (p, "data") != 0)
38289 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
38290 enter ? "enter" : "exit");
38291 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38292 return NULL_TREE;
38295 cp_lexer_consume_token (parser->lexer);
38297 if (enter)
38298 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
38299 "#pragma acc enter data", pragma_tok);
38300 else
38301 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
38302 "#pragma acc exit data", pragma_tok);
38304 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38306 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
38307 enter ? "enter" : "exit");
38308 return NULL_TREE;
38311 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
38312 TREE_TYPE (stmt) = void_type_node;
38313 OMP_STANDALONE_CLAUSES (stmt) = clauses;
38314 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38315 add_stmt (stmt);
38316 return stmt;
38319 /* OpenACC 2.0:
38320 # pragma acc loop oacc-loop-clause[optseq] new-line
38321 structured-block */
38323 #define OACC_LOOP_CLAUSE_MASK \
38324 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38335 static tree
38336 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
38337 omp_clause_mask mask, tree *cclauses, bool *if_p)
38339 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
38341 strcat (p_name, " loop");
38342 mask |= OACC_LOOP_CLAUSE_MASK;
38344 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
38345 cclauses == NULL);
38346 if (cclauses)
38348 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
38349 if (*cclauses)
38350 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
38351 if (clauses)
38352 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38355 tree block = begin_omp_structured_block ();
38356 int save = cp_parser_begin_omp_structured_block (parser);
38357 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
38358 cp_parser_end_omp_structured_block (parser, save);
38359 add_stmt (finish_omp_structured_block (block));
38361 return stmt;
38364 /* OpenACC 2.0:
38365 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38366 structured-block
38370 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38371 structured-block
38374 #define OACC_KERNELS_CLAUSE_MASK \
38375 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38389 #define OACC_PARALLEL_CLAUSE_MASK \
38390 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38407 static tree
38408 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
38409 char *p_name, bool *if_p)
38411 omp_clause_mask mask;
38412 enum tree_code code;
38413 switch (cp_parser_pragma_kind (pragma_tok))
38415 case PRAGMA_OACC_KERNELS:
38416 strcat (p_name, " kernels");
38417 mask = OACC_KERNELS_CLAUSE_MASK;
38418 code = OACC_KERNELS;
38419 break;
38420 case PRAGMA_OACC_PARALLEL:
38421 strcat (p_name, " parallel");
38422 mask = OACC_PARALLEL_CLAUSE_MASK;
38423 code = OACC_PARALLEL;
38424 break;
38425 default:
38426 gcc_unreachable ();
38429 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38431 const char *p
38432 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38433 if (strcmp (p, "loop") == 0)
38435 cp_lexer_consume_token (parser->lexer);
38436 tree block = begin_omp_parallel ();
38437 tree clauses;
38438 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
38439 if_p);
38440 return finish_omp_construct (code, block, clauses);
38444 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
38446 tree block = begin_omp_parallel ();
38447 unsigned int save = cp_parser_begin_omp_structured_block (parser);
38448 cp_parser_statement (parser, NULL_TREE, false, if_p);
38449 cp_parser_end_omp_structured_block (parser, save);
38450 return finish_omp_construct (code, block, clauses);
38453 /* OpenACC 2.0:
38454 # pragma acc update oacc-update-clause[optseq] new-line
38457 #define OACC_UPDATE_CLAUSE_MASK \
38458 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38465 static tree
38466 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
38468 tree stmt, clauses;
38470 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
38471 "#pragma acc update", pragma_tok);
38473 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38475 error_at (pragma_tok->location,
38476 "%<#pragma acc update%> must contain at least one "
38477 "%<device%> or %<host%> or %<self%> clause");
38478 return NULL_TREE;
38481 stmt = make_node (OACC_UPDATE);
38482 TREE_TYPE (stmt) = void_type_node;
38483 OACC_UPDATE_CLAUSES (stmt) = clauses;
38484 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38485 add_stmt (stmt);
38486 return stmt;
38489 /* OpenACC 2.0:
38490 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38492 LOC is the location of the #pragma token.
38495 #define OACC_WAIT_CLAUSE_MASK \
38496 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
38498 static tree
38499 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
38501 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
38502 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38504 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38505 list = cp_parser_oacc_wait_list (parser, loc, list);
38507 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
38508 "#pragma acc wait", pragma_tok);
38510 stmt = c_finish_oacc_wait (loc, list, clauses);
38511 stmt = finish_expr_stmt (stmt);
38513 return stmt;
38516 /* OpenMP 4.0:
38517 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
38519 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
38520 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
38524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
38525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
38527 static void
38528 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
38529 enum pragma_context context)
38531 bool first_p = parser->omp_declare_simd == NULL;
38532 cp_omp_declare_simd_data data;
38533 if (first_p)
38535 data.error_seen = false;
38536 data.fndecl_seen = false;
38537 data.tokens = vNULL;
38538 data.clauses = NULL_TREE;
38539 /* It is safe to take the address of a local variable; it will only be
38540 used while this scope is live. */
38541 parser->omp_declare_simd = &data;
38544 /* Store away all pragma tokens. */
38545 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38546 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38547 cp_lexer_consume_token (parser->lexer);
38548 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38549 parser->omp_declare_simd->error_seen = true;
38550 cp_parser_require_pragma_eol (parser, pragma_tok);
38551 struct cp_token_cache *cp
38552 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38553 parser->omp_declare_simd->tokens.safe_push (cp);
38555 if (first_p)
38557 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38558 cp_parser_pragma (parser, context, NULL);
38559 switch (context)
38561 case pragma_external:
38562 cp_parser_declaration (parser);
38563 break;
38564 case pragma_member:
38565 cp_parser_member_declaration (parser);
38566 break;
38567 case pragma_objc_icode:
38568 cp_parser_block_declaration (parser, /*statement_p=*/false);
38569 break;
38570 default:
38571 cp_parser_declaration_statement (parser);
38572 break;
38574 if (parser->omp_declare_simd
38575 && !parser->omp_declare_simd->error_seen
38576 && !parser->omp_declare_simd->fndecl_seen)
38577 error_at (pragma_tok->location,
38578 "%<#pragma omp declare simd%> not immediately followed by "
38579 "function declaration or definition");
38580 data.tokens.release ();
38581 parser->omp_declare_simd = NULL;
38585 /* Finalize #pragma omp declare simd clauses after direct declarator has
38586 been parsed, and put that into "omp declare simd" attribute. */
38588 static tree
38589 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
38591 struct cp_token_cache *ce;
38592 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
38593 int i;
38595 if (!data->error_seen && data->fndecl_seen)
38597 error ("%<#pragma omp declare simd%> not immediately followed by "
38598 "a single function declaration or definition");
38599 data->error_seen = true;
38601 if (data->error_seen)
38602 return attrs;
38604 FOR_EACH_VEC_ELT (data->tokens, i, ce)
38606 tree c, cl;
38608 cp_parser_push_lexer_for_tokens (parser, ce);
38609 parser->lexer->in_pragma = true;
38610 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38611 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38612 cp_lexer_consume_token (parser->lexer);
38613 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
38614 "#pragma omp declare simd", pragma_tok);
38615 cp_parser_pop_lexer (parser);
38616 if (cl)
38617 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
38618 c = build_tree_list (get_identifier ("omp declare simd"), cl);
38619 TREE_CHAIN (c) = attrs;
38620 if (processing_template_decl)
38621 ATTR_IS_DEPENDENT (c) = 1;
38622 attrs = c;
38625 data->fndecl_seen = true;
38626 return attrs;
38630 /* OpenMP 4.0:
38631 # pragma omp declare target new-line
38632 declarations and definitions
38633 # pragma omp end declare target new-line
38635 OpenMP 4.5:
38636 # pragma omp declare target ( extended-list ) new-line
38638 # pragma omp declare target declare-target-clauses[seq] new-line */
38640 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
38641 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
38644 static void
38645 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
38647 tree clauses = NULL_TREE;
38648 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38649 clauses
38650 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
38651 "#pragma omp declare target", pragma_tok);
38652 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38654 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
38655 clauses);
38656 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
38657 cp_parser_require_pragma_eol (parser, pragma_tok);
38659 else
38661 cp_parser_require_pragma_eol (parser, pragma_tok);
38662 scope_chain->omp_declare_target_attribute++;
38663 return;
38665 if (scope_chain->omp_declare_target_attribute)
38666 error_at (pragma_tok->location,
38667 "%<#pragma omp declare target%> with clauses in between "
38668 "%<#pragma omp declare target%> without clauses and "
38669 "%<#pragma omp end declare target%>");
38670 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
38672 tree t = OMP_CLAUSE_DECL (c), id;
38673 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
38674 tree at2 = lookup_attribute ("omp declare target link",
38675 DECL_ATTRIBUTES (t));
38676 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
38678 id = get_identifier ("omp declare target link");
38679 std::swap (at1, at2);
38681 else
38682 id = get_identifier ("omp declare target");
38683 if (at2)
38685 error_at (OMP_CLAUSE_LOCATION (c),
38686 "%qD specified both in declare target %<link%> and %<to%>"
38687 " clauses", t);
38688 continue;
38690 if (!at1)
38692 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
38693 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
38694 continue;
38696 symtab_node *node = symtab_node::get (t);
38697 if (node != NULL)
38699 node->offloadable = 1;
38700 if (ENABLE_OFFLOADING)
38702 g->have_offload = true;
38703 if (is_a <varpool_node *> (node))
38704 vec_safe_push (offload_vars, t);
38711 static void
38712 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
38714 const char *p = "";
38715 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38717 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38718 p = IDENTIFIER_POINTER (id);
38720 if (strcmp (p, "declare") == 0)
38722 cp_lexer_consume_token (parser->lexer);
38723 p = "";
38724 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38726 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38727 p = IDENTIFIER_POINTER (id);
38729 if (strcmp (p, "target") == 0)
38730 cp_lexer_consume_token (parser->lexer);
38731 else
38733 cp_parser_error (parser, "expected %<target%>");
38734 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38735 return;
38738 else
38740 cp_parser_error (parser, "expected %<declare%>");
38741 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38742 return;
38744 cp_parser_require_pragma_eol (parser, pragma_tok);
38745 if (!scope_chain->omp_declare_target_attribute)
38746 error_at (pragma_tok->location,
38747 "%<#pragma omp end declare target%> without corresponding "
38748 "%<#pragma omp declare target%>");
38749 else
38750 scope_chain->omp_declare_target_attribute--;
38753 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
38754 expression and optional initializer clause of
38755 #pragma omp declare reduction. We store the expression(s) as
38756 either 3, 6 or 7 special statements inside of the artificial function's
38757 body. The first two statements are DECL_EXPRs for the artificial
38758 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
38759 expression that uses those variables.
38760 If there was any INITIALIZER clause, this is followed by further statements,
38761 the fourth and fifth statements are DECL_EXPRs for the artificial
38762 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
38763 constructor variant (first token after open paren is not omp_priv),
38764 then the sixth statement is a statement with the function call expression
38765 that uses the OMP_PRIV and optionally OMP_ORIG variable.
38766 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
38767 to initialize the OMP_PRIV artificial variable and there is seventh
38768 statement, a DECL_EXPR of the OMP_PRIV statement again. */
38770 static bool
38771 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
38773 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
38774 gcc_assert (TYPE_REF_P (type));
38775 type = TREE_TYPE (type);
38776 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
38777 DECL_ARTIFICIAL (omp_out) = 1;
38778 pushdecl (omp_out);
38779 add_decl_expr (omp_out);
38780 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
38781 DECL_ARTIFICIAL (omp_in) = 1;
38782 pushdecl (omp_in);
38783 add_decl_expr (omp_in);
38784 tree combiner;
38785 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
38787 keep_next_level (true);
38788 tree block = begin_omp_structured_block ();
38789 combiner = cp_parser_expression (parser);
38790 finish_expr_stmt (combiner);
38791 block = finish_omp_structured_block (block);
38792 add_stmt (block);
38794 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38795 return false;
38797 const char *p = "";
38798 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38800 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38801 p = IDENTIFIER_POINTER (id);
38804 if (strcmp (p, "initializer") == 0)
38806 cp_lexer_consume_token (parser->lexer);
38807 matching_parens parens;
38808 if (!parens.require_open (parser))
38809 return false;
38811 p = "";
38812 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38814 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38815 p = IDENTIFIER_POINTER (id);
38818 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
38819 DECL_ARTIFICIAL (omp_priv) = 1;
38820 pushdecl (omp_priv);
38821 add_decl_expr (omp_priv);
38822 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
38823 DECL_ARTIFICIAL (omp_orig) = 1;
38824 pushdecl (omp_orig);
38825 add_decl_expr (omp_orig);
38827 keep_next_level (true);
38828 block = begin_omp_structured_block ();
38830 bool ctor = false;
38831 if (strcmp (p, "omp_priv") == 0)
38833 bool is_direct_init, is_non_constant_init;
38834 ctor = true;
38835 cp_lexer_consume_token (parser->lexer);
38836 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
38837 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
38838 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
38839 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
38840 == CPP_CLOSE_PAREN
38841 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
38842 == CPP_CLOSE_PAREN))
38844 finish_omp_structured_block (block);
38845 error ("invalid initializer clause");
38846 return false;
38848 initializer = cp_parser_initializer (parser, &is_direct_init,
38849 &is_non_constant_init);
38850 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
38851 NULL_TREE, LOOKUP_ONLYCONVERTING);
38853 else
38855 cp_parser_parse_tentatively (parser);
38856 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
38857 /*check_dependency_p=*/true,
38858 /*template_p=*/NULL,
38859 /*declarator_p=*/false,
38860 /*optional_p=*/false);
38861 vec<tree, va_gc> *args;
38862 if (fn_name == error_mark_node
38863 || cp_parser_error_occurred (parser)
38864 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
38865 || ((args = cp_parser_parenthesized_expression_list
38866 (parser, non_attr, /*cast_p=*/false,
38867 /*allow_expansion_p=*/true,
38868 /*non_constant_p=*/NULL)),
38869 cp_parser_error_occurred (parser)))
38871 finish_omp_structured_block (block);
38872 cp_parser_abort_tentative_parse (parser);
38873 cp_parser_error (parser, "expected id-expression (arguments)");
38874 return false;
38876 unsigned int i;
38877 tree arg;
38878 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
38879 if (arg == omp_priv
38880 || (TREE_CODE (arg) == ADDR_EXPR
38881 && TREE_OPERAND (arg, 0) == omp_priv))
38882 break;
38883 cp_parser_abort_tentative_parse (parser);
38884 if (arg == NULL_TREE)
38885 error ("one of the initializer call arguments should be %<omp_priv%>"
38886 " or %<&omp_priv%>");
38887 initializer = cp_parser_postfix_expression (parser, false, false, false,
38888 false, NULL);
38889 finish_expr_stmt (initializer);
38892 block = finish_omp_structured_block (block);
38893 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
38894 add_stmt (block);
38896 if (ctor)
38897 add_decl_expr (omp_orig);
38899 if (!parens.require_close (parser))
38900 return false;
38903 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
38904 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
38905 UNKNOWN_LOCATION);
38907 return true;
38910 /* OpenMP 4.0
38911 #pragma omp declare reduction (reduction-id : typename-list : expression) \
38912 initializer-clause[opt] new-line
38914 initializer-clause:
38915 initializer (omp_priv initializer)
38916 initializer (function-name (argument-list)) */
38918 static void
38919 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
38920 enum pragma_context)
38922 auto_vec<tree> types;
38923 enum tree_code reduc_code = ERROR_MARK;
38924 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
38925 unsigned int i;
38926 cp_token *first_token;
38927 cp_token_cache *cp;
38928 int errs;
38929 void *p;
38931 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
38932 p = obstack_alloc (&declarator_obstack, 0);
38934 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38935 goto fail;
38937 switch (cp_lexer_peek_token (parser->lexer)->type)
38939 case CPP_PLUS:
38940 reduc_code = PLUS_EXPR;
38941 break;
38942 case CPP_MULT:
38943 reduc_code = MULT_EXPR;
38944 break;
38945 case CPP_MINUS:
38946 reduc_code = MINUS_EXPR;
38947 break;
38948 case CPP_AND:
38949 reduc_code = BIT_AND_EXPR;
38950 break;
38951 case CPP_XOR:
38952 reduc_code = BIT_XOR_EXPR;
38953 break;
38954 case CPP_OR:
38955 reduc_code = BIT_IOR_EXPR;
38956 break;
38957 case CPP_AND_AND:
38958 reduc_code = TRUTH_ANDIF_EXPR;
38959 break;
38960 case CPP_OR_OR:
38961 reduc_code = TRUTH_ORIF_EXPR;
38962 break;
38963 case CPP_NAME:
38964 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
38965 break;
38966 default:
38967 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
38968 "%<|%>, %<&&%>, %<||%> or identifier");
38969 goto fail;
38972 if (reduc_code != ERROR_MARK)
38973 cp_lexer_consume_token (parser->lexer);
38975 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
38976 if (reduc_id == error_mark_node)
38977 goto fail;
38979 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38980 goto fail;
38982 /* Types may not be defined in declare reduction type list. */
38983 const char *saved_message;
38984 saved_message = parser->type_definition_forbidden_message;
38985 parser->type_definition_forbidden_message
38986 = G_("types may not be defined in declare reduction type list");
38987 bool saved_colon_corrects_to_scope_p;
38988 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38989 parser->colon_corrects_to_scope_p = false;
38990 bool saved_colon_doesnt_start_class_def_p;
38991 saved_colon_doesnt_start_class_def_p
38992 = parser->colon_doesnt_start_class_def_p;
38993 parser->colon_doesnt_start_class_def_p = true;
38995 while (true)
38997 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38998 type = cp_parser_type_id (parser);
38999 if (type == error_mark_node)
39001 else if (ARITHMETIC_TYPE_P (type)
39002 && (orig_reduc_id == NULL_TREE
39003 || (TREE_CODE (type) != COMPLEX_TYPE
39004 && (id_equal (orig_reduc_id, "min")
39005 || id_equal (orig_reduc_id, "max")))))
39006 error_at (loc, "predeclared arithmetic type %qT in "
39007 "%<#pragma omp declare reduction%>", type);
39008 else if (TREE_CODE (type) == FUNCTION_TYPE
39009 || TREE_CODE (type) == METHOD_TYPE
39010 || TREE_CODE (type) == ARRAY_TYPE)
39011 error_at (loc, "function or array type %qT in "
39012 "%<#pragma omp declare reduction%>", type);
39013 else if (TYPE_REF_P (type))
39014 error_at (loc, "reference type %qT in "
39015 "%<#pragma omp declare reduction%>", type);
39016 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
39017 error_at (loc, "const, volatile or __restrict qualified type %qT in "
39018 "%<#pragma omp declare reduction%>", type);
39019 else
39020 types.safe_push (type);
39022 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39023 cp_lexer_consume_token (parser->lexer);
39024 else
39025 break;
39028 /* Restore the saved message. */
39029 parser->type_definition_forbidden_message = saved_message;
39030 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39031 parser->colon_doesnt_start_class_def_p
39032 = saved_colon_doesnt_start_class_def_p;
39034 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
39035 || types.is_empty ())
39037 fail:
39038 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39039 goto done;
39042 first_token = cp_lexer_peek_token (parser->lexer);
39043 cp = NULL;
39044 errs = errorcount;
39045 FOR_EACH_VEC_ELT (types, i, type)
39047 tree fntype
39048 = build_function_type_list (void_type_node,
39049 cp_build_reference_type (type, false),
39050 NULL_TREE);
39051 tree this_reduc_id = reduc_id;
39052 if (!dependent_type_p (type))
39053 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
39054 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
39055 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
39056 DECL_ARTIFICIAL (fndecl) = 1;
39057 DECL_EXTERNAL (fndecl) = 1;
39058 DECL_DECLARED_INLINE_P (fndecl) = 1;
39059 DECL_IGNORED_P (fndecl) = 1;
39060 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
39061 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
39062 DECL_ATTRIBUTES (fndecl)
39063 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
39064 DECL_ATTRIBUTES (fndecl));
39065 if (processing_template_decl)
39066 fndecl = push_template_decl (fndecl);
39067 bool block_scope = false;
39068 tree block = NULL_TREE;
39069 if (current_function_decl)
39071 block_scope = true;
39072 DECL_CONTEXT (fndecl) = global_namespace;
39073 if (!processing_template_decl)
39074 pushdecl (fndecl);
39076 else if (current_class_type)
39078 if (cp == NULL)
39080 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39081 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39082 cp_lexer_consume_token (parser->lexer);
39083 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39084 goto fail;
39085 cp = cp_token_cache_new (first_token,
39086 cp_lexer_peek_nth_token (parser->lexer,
39087 2));
39089 DECL_STATIC_FUNCTION_P (fndecl) = 1;
39090 finish_member_declaration (fndecl);
39091 DECL_PENDING_INLINE_INFO (fndecl) = cp;
39092 DECL_PENDING_INLINE_P (fndecl) = 1;
39093 vec_safe_push (unparsed_funs_with_definitions, fndecl);
39094 continue;
39096 else
39098 DECL_CONTEXT (fndecl) = current_namespace;
39099 pushdecl (fndecl);
39101 if (!block_scope)
39102 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
39103 else
39104 block = begin_omp_structured_block ();
39105 if (cp)
39107 cp_parser_push_lexer_for_tokens (parser, cp);
39108 parser->lexer->in_pragma = true;
39110 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
39112 if (!block_scope)
39113 finish_function (/*inline_p=*/false);
39114 else
39115 DECL_CONTEXT (fndecl) = current_function_decl;
39116 if (cp)
39117 cp_parser_pop_lexer (parser);
39118 goto fail;
39120 if (cp)
39121 cp_parser_pop_lexer (parser);
39122 if (!block_scope)
39123 finish_function (/*inline_p=*/false);
39124 else
39126 DECL_CONTEXT (fndecl) = current_function_decl;
39127 block = finish_omp_structured_block (block);
39128 if (TREE_CODE (block) == BIND_EXPR)
39129 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
39130 else if (TREE_CODE (block) == STATEMENT_LIST)
39131 DECL_SAVED_TREE (fndecl) = block;
39132 if (processing_template_decl)
39133 add_decl_expr (fndecl);
39135 cp_check_omp_declare_reduction (fndecl);
39136 if (cp == NULL && types.length () > 1)
39137 cp = cp_token_cache_new (first_token,
39138 cp_lexer_peek_nth_token (parser->lexer, 2));
39139 if (errs != errorcount)
39140 break;
39143 cp_parser_require_pragma_eol (parser, pragma_tok);
39145 done:
39146 /* Free any declarators allocated. */
39147 obstack_free (&declarator_obstack, p);
39150 /* OpenMP 4.0
39151 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39152 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39153 initializer-clause[opt] new-line
39154 #pragma omp declare target new-line */
39156 static bool
39157 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
39158 enum pragma_context context)
39160 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39162 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39163 const char *p = IDENTIFIER_POINTER (id);
39165 if (strcmp (p, "simd") == 0)
39167 cp_lexer_consume_token (parser->lexer);
39168 cp_parser_omp_declare_simd (parser, pragma_tok,
39169 context);
39170 return true;
39172 cp_ensure_no_omp_declare_simd (parser);
39173 if (strcmp (p, "reduction") == 0)
39175 cp_lexer_consume_token (parser->lexer);
39176 cp_parser_omp_declare_reduction (parser, pragma_tok,
39177 context);
39178 return false;
39180 if (!flag_openmp) /* flag_openmp_simd */
39182 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39183 return false;
39185 if (strcmp (p, "target") == 0)
39187 cp_lexer_consume_token (parser->lexer);
39188 cp_parser_omp_declare_target (parser, pragma_tok);
39189 return false;
39192 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
39193 "or %<target%>");
39194 cp_parser_require_pragma_eol (parser, pragma_tok);
39195 return false;
39198 /* OpenMP 5.0
39199 #pragma omp requires clauses[optseq] new-line */
39201 static bool
39202 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
39204 bool first = true;
39205 enum omp_requires new_req = (enum omp_requires) 0;
39207 location_t loc = pragma_tok->location;
39208 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39210 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39211 cp_lexer_consume_token (parser->lexer);
39213 first = false;
39215 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39217 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39218 const char *p = IDENTIFIER_POINTER (id);
39219 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
39220 enum omp_requires this_req = (enum omp_requires) 0;
39222 if (!strcmp (p, "unified_address"))
39223 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
39224 else if (!strcmp (p, "unified_shared_memory"))
39225 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
39226 else if (!strcmp (p, "dynamic_allocators"))
39227 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
39228 else if (!strcmp (p, "reverse_offload"))
39229 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
39230 else if (!strcmp (p, "atomic_default_mem_order"))
39232 cp_lexer_consume_token (parser->lexer);
39234 matching_parens parens;
39235 if (parens.require_open (parser))
39237 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39239 id = cp_lexer_peek_token (parser->lexer)->u.value;
39240 p = IDENTIFIER_POINTER (id);
39242 if (!strcmp (p, "seq_cst"))
39243 this_req
39244 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
39245 else if (!strcmp (p, "relaxed"))
39246 this_req
39247 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
39248 else if (!strcmp (p, "acq_rel"))
39249 this_req
39250 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
39252 if (this_req == 0)
39254 error_at (cp_lexer_peek_token (parser->lexer)->location,
39255 "expected %<seq_cst%>, %<relaxed%> or "
39256 "%<acq_rel%>");
39257 if (cp_lexer_nth_token_is (parser->lexer, 2,
39258 CPP_CLOSE_PAREN))
39259 cp_lexer_consume_token (parser->lexer);
39261 else
39262 cp_lexer_consume_token (parser->lexer);
39264 if (!parens.require_close (parser))
39265 cp_parser_skip_to_closing_parenthesis (parser,
39266 /*recovering=*/true,
39267 /*or_comma=*/false,
39268 /*consume_paren=*/
39269 true);
39271 if (this_req == 0)
39273 cp_parser_require_pragma_eol (parser, pragma_tok);
39274 return false;
39277 p = NULL;
39279 else
39281 error_at (cloc, "expected %<unified_address%>, "
39282 "%<unified_shared_memory%>, "
39283 "%<dynamic_allocators%>, "
39284 "%<reverse_offload%> "
39285 "or %<atomic_default_mem_order%> clause");
39286 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39287 return false;
39289 if (p)
39290 sorry_at (cloc, "%qs clause on %<requires%> directive not "
39291 "supported yet", p);
39292 if (p)
39293 cp_lexer_consume_token (parser->lexer);
39294 if (this_req)
39296 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39298 if ((this_req & new_req) != 0)
39299 error_at (cloc, "too many %qs clauses", p);
39300 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
39301 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
39302 error_at (cloc, "%qs clause used lexically after first "
39303 "target construct or offloading API", p);
39305 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39307 error_at (cloc, "too many %qs clauses",
39308 "atomic_default_mem_order");
39309 this_req = (enum omp_requires) 0;
39311 else if ((omp_requires_mask
39312 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39314 error_at (cloc, "more than one %<atomic_default_mem_order%>"
39315 " clause in a single compilation unit");
39316 this_req
39317 = (enum omp_requires)
39318 (omp_requires_mask
39319 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
39321 else if ((omp_requires_mask
39322 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
39323 error_at (cloc, "%<atomic_default_mem_order%> clause used "
39324 "lexically after first %<atomic%> construct "
39325 "without memory order clause");
39326 new_req = (enum omp_requires) (new_req | this_req);
39327 omp_requires_mask
39328 = (enum omp_requires) (omp_requires_mask | this_req);
39329 continue;
39332 break;
39334 cp_parser_require_pragma_eol (parser, pragma_tok);
39336 if (new_req == 0)
39337 error_at (loc, "%<pragma omp requires%> requires at least one clause");
39338 return false;
39342 /* OpenMP 4.5:
39343 #pragma omp taskloop taskloop-clause[optseq] new-line
39344 for-loop
39346 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39347 for-loop */
39349 #define OMP_TASKLOOP_CLAUSE_MASK \
39350 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39367 static tree
39368 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
39369 char *p_name, omp_clause_mask mask, tree *cclauses,
39370 bool *if_p)
39372 tree clauses, sb, ret;
39373 unsigned int save;
39374 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39376 strcat (p_name, " taskloop");
39377 mask |= OMP_TASKLOOP_CLAUSE_MASK;
39378 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39379 clause. */
39380 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
39381 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
39383 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39385 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39386 const char *p = IDENTIFIER_POINTER (id);
39388 if (strcmp (p, "simd") == 0)
39390 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39391 if (cclauses == NULL)
39392 cclauses = cclauses_buf;
39394 cp_lexer_consume_token (parser->lexer);
39395 if (!flag_openmp) /* flag_openmp_simd */
39396 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39397 cclauses, if_p);
39398 sb = begin_omp_structured_block ();
39399 save = cp_parser_begin_omp_structured_block (parser);
39400 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39401 cclauses, if_p);
39402 cp_parser_end_omp_structured_block (parser, save);
39403 tree body = finish_omp_structured_block (sb);
39404 if (ret == NULL)
39405 return ret;
39406 ret = make_node (OMP_TASKLOOP);
39407 TREE_TYPE (ret) = void_type_node;
39408 OMP_FOR_BODY (ret) = body;
39409 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39410 SET_EXPR_LOCATION (ret, loc);
39411 add_stmt (ret);
39412 return ret;
39415 if (!flag_openmp) /* flag_openmp_simd */
39417 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39418 return NULL_TREE;
39421 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39422 cclauses == NULL);
39423 if (cclauses)
39425 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
39426 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39429 keep_next_level (true);
39430 sb = begin_omp_structured_block ();
39431 save = cp_parser_begin_omp_structured_block (parser);
39433 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
39434 if_p);
39436 cp_parser_end_omp_structured_block (parser, save);
39437 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39439 return ret;
39443 /* OpenACC 2.0:
39444 # pragma acc routine oacc-routine-clause[optseq] new-line
39445 function-definition
39447 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39450 #define OACC_ROUTINE_CLAUSE_MASK \
39451 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39457 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39458 component, which must resolve to a declared namespace-scope
39459 function. The clauses are either processed directly (for a named
39460 function), or defered until the immediatley following declaration
39461 is parsed. */
39463 static void
39464 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
39465 enum pragma_context context)
39467 gcc_checking_assert (context == pragma_external);
39468 /* The checking for "another pragma following this one" in the "no optional
39469 '( name )'" case makes sure that we dont re-enter. */
39470 gcc_checking_assert (parser->oacc_routine == NULL);
39472 cp_oacc_routine_data data;
39473 data.error_seen = false;
39474 data.fndecl_seen = false;
39475 data.tokens = vNULL;
39476 data.clauses = NULL_TREE;
39477 data.loc = pragma_tok->location;
39478 /* It is safe to take the address of a local variable; it will only be
39479 used while this scope is live. */
39480 parser->oacc_routine = &data;
39482 /* Look for optional '( name )'. */
39483 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39485 matching_parens parens;
39486 parens.consume_open (parser); /* '(' */
39488 /* We parse the name as an id-expression. If it resolves to
39489 anything other than a non-overloaded function at namespace
39490 scope, it's an error. */
39491 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
39492 tree name = cp_parser_id_expression (parser,
39493 /*template_keyword_p=*/false,
39494 /*check_dependency_p=*/false,
39495 /*template_p=*/NULL,
39496 /*declarator_p=*/false,
39497 /*optional_p=*/false);
39498 tree decl = (identifier_p (name)
39499 ? cp_parser_lookup_name_simple (parser, name, name_loc)
39500 : name);
39501 if (name != error_mark_node && decl == error_mark_node)
39502 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
39504 if (decl == error_mark_node
39505 || !parens.require_close (parser))
39507 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39508 parser->oacc_routine = NULL;
39509 return;
39512 data.clauses
39513 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39514 "#pragma acc routine",
39515 cp_lexer_peek_token (parser->lexer));
39517 if (decl && is_overloaded_fn (decl)
39518 && (TREE_CODE (decl) != FUNCTION_DECL
39519 || DECL_FUNCTION_TEMPLATE_P (decl)))
39521 error_at (name_loc,
39522 "%<#pragma acc routine%> names a set of overloads");
39523 parser->oacc_routine = NULL;
39524 return;
39527 /* Perhaps we should use the same rule as declarations in different
39528 namespaces? */
39529 if (!DECL_NAMESPACE_SCOPE_P (decl))
39531 error_at (name_loc,
39532 "%qD does not refer to a namespace scope function", decl);
39533 parser->oacc_routine = NULL;
39534 return;
39537 if (TREE_CODE (decl) != FUNCTION_DECL)
39539 error_at (name_loc, "%qD does not refer to a function", decl);
39540 parser->oacc_routine = NULL;
39541 return;
39544 cp_finalize_oacc_routine (parser, decl, false);
39545 parser->oacc_routine = NULL;
39547 else /* No optional '( name )'. */
39549 /* Store away all pragma tokens. */
39550 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39551 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39552 cp_lexer_consume_token (parser->lexer);
39553 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39554 parser->oacc_routine->error_seen = true;
39555 cp_parser_require_pragma_eol (parser, pragma_tok);
39556 struct cp_token_cache *cp
39557 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39558 parser->oacc_routine->tokens.safe_push (cp);
39560 /* Emit a helpful diagnostic if there's another pragma following this
39561 one. */
39562 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39564 cp_ensure_no_oacc_routine (parser);
39565 data.tokens.release ();
39566 /* ..., and then just keep going. */
39567 return;
39570 /* We only have to consider the pragma_external case here. */
39571 cp_parser_declaration (parser);
39572 if (parser->oacc_routine
39573 && !parser->oacc_routine->fndecl_seen)
39574 cp_ensure_no_oacc_routine (parser);
39575 else
39576 parser->oacc_routine = NULL;
39577 data.tokens.release ();
39581 /* Finalize #pragma acc routine clauses after direct declarator has
39582 been parsed. */
39584 static tree
39585 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
39587 struct cp_token_cache *ce;
39588 cp_oacc_routine_data *data = parser->oacc_routine;
39590 if (!data->error_seen && data->fndecl_seen)
39592 error_at (data->loc,
39593 "%<#pragma acc routine%> not immediately followed by "
39594 "a single function declaration or definition");
39595 data->error_seen = true;
39597 if (data->error_seen)
39598 return attrs;
39600 gcc_checking_assert (data->tokens.length () == 1);
39601 ce = data->tokens[0];
39603 cp_parser_push_lexer_for_tokens (parser, ce);
39604 parser->lexer->in_pragma = true;
39605 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39607 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39608 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
39609 parser->oacc_routine->clauses
39610 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39611 "#pragma acc routine", pragma_tok);
39612 cp_parser_pop_lexer (parser);
39613 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
39614 fndecl_seen. */
39616 return attrs;
39619 /* Apply any saved OpenACC routine clauses to a just-parsed
39620 declaration. */
39622 static void
39623 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
39625 if (__builtin_expect (parser->oacc_routine != NULL, 0))
39627 /* Keep going if we're in error reporting mode. */
39628 if (parser->oacc_routine->error_seen
39629 || fndecl == error_mark_node)
39630 return;
39632 if (parser->oacc_routine->fndecl_seen)
39634 error_at (parser->oacc_routine->loc,
39635 "%<#pragma acc routine%> not immediately followed by"
39636 " a single function declaration or definition");
39637 parser->oacc_routine = NULL;
39638 return;
39640 if (TREE_CODE (fndecl) != FUNCTION_DECL)
39642 cp_ensure_no_oacc_routine (parser);
39643 return;
39646 if (oacc_get_fn_attrib (fndecl))
39648 error_at (parser->oacc_routine->loc,
39649 "%<#pragma acc routine%> already applied to %qD", fndecl);
39650 parser->oacc_routine = NULL;
39651 return;
39654 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
39656 error_at (parser->oacc_routine->loc,
39657 TREE_USED (fndecl)
39658 ? G_("%<#pragma acc routine%> must be applied before use")
39659 : G_("%<#pragma acc routine%> must be applied before "
39660 "definition"));
39661 parser->oacc_routine = NULL;
39662 return;
39665 /* Process the routine's dimension clauses. */
39666 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
39667 oacc_replace_fn_attrib (fndecl, dims);
39669 /* Add an "omp declare target" attribute. */
39670 DECL_ATTRIBUTES (fndecl)
39671 = tree_cons (get_identifier ("omp declare target"),
39672 NULL_TREE, DECL_ATTRIBUTES (fndecl));
39674 /* Don't unset parser->oacc_routine here: we may still need it to
39675 diagnose wrong usage. But, remember that we've used this "#pragma acc
39676 routine". */
39677 parser->oacc_routine->fndecl_seen = true;
39681 /* Main entry point to OpenMP statement pragmas. */
39683 static void
39684 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39686 tree stmt;
39687 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
39688 omp_clause_mask mask (0);
39690 switch (cp_parser_pragma_kind (pragma_tok))
39692 case PRAGMA_OACC_ATOMIC:
39693 cp_parser_omp_atomic (parser, pragma_tok);
39694 return;
39695 case PRAGMA_OACC_CACHE:
39696 stmt = cp_parser_oacc_cache (parser, pragma_tok);
39697 break;
39698 case PRAGMA_OACC_DATA:
39699 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
39700 break;
39701 case PRAGMA_OACC_ENTER_DATA:
39702 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
39703 break;
39704 case PRAGMA_OACC_EXIT_DATA:
39705 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
39706 break;
39707 case PRAGMA_OACC_HOST_DATA:
39708 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
39709 break;
39710 case PRAGMA_OACC_KERNELS:
39711 case PRAGMA_OACC_PARALLEL:
39712 strcpy (p_name, "#pragma acc");
39713 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
39714 if_p);
39715 break;
39716 case PRAGMA_OACC_LOOP:
39717 strcpy (p_name, "#pragma acc");
39718 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
39719 if_p);
39720 break;
39721 case PRAGMA_OACC_UPDATE:
39722 stmt = cp_parser_oacc_update (parser, pragma_tok);
39723 break;
39724 case PRAGMA_OACC_WAIT:
39725 stmt = cp_parser_oacc_wait (parser, pragma_tok);
39726 break;
39727 case PRAGMA_OMP_ATOMIC:
39728 cp_parser_omp_atomic (parser, pragma_tok);
39729 return;
39730 case PRAGMA_OMP_CRITICAL:
39731 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
39732 break;
39733 case PRAGMA_OMP_DISTRIBUTE:
39734 strcpy (p_name, "#pragma omp");
39735 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
39736 if_p);
39737 break;
39738 case PRAGMA_OMP_FOR:
39739 strcpy (p_name, "#pragma omp");
39740 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
39741 if_p);
39742 break;
39743 case PRAGMA_OMP_MASTER:
39744 strcpy (p_name, "#pragma omp");
39745 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
39746 if_p);
39747 break;
39748 case PRAGMA_OMP_PARALLEL:
39749 strcpy (p_name, "#pragma omp");
39750 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
39751 if_p);
39752 break;
39753 case PRAGMA_OMP_SECTIONS:
39754 strcpy (p_name, "#pragma omp");
39755 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
39756 break;
39757 case PRAGMA_OMP_SIMD:
39758 strcpy (p_name, "#pragma omp");
39759 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
39760 if_p);
39761 break;
39762 case PRAGMA_OMP_SINGLE:
39763 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
39764 break;
39765 case PRAGMA_OMP_TASK:
39766 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
39767 break;
39768 case PRAGMA_OMP_TASKGROUP:
39769 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
39770 break;
39771 case PRAGMA_OMP_TASKLOOP:
39772 strcpy (p_name, "#pragma omp");
39773 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
39774 if_p);
39775 break;
39776 case PRAGMA_OMP_TEAMS:
39777 strcpy (p_name, "#pragma omp");
39778 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
39779 if_p);
39780 break;
39781 default:
39782 gcc_unreachable ();
39785 protected_set_expr_location (stmt, pragma_tok->location);
39788 /* Transactional Memory parsing routines. */
39790 /* Parse a transaction attribute.
39792 txn-attribute:
39793 attribute
39794 [ [ identifier ] ]
39796 We use this instead of cp_parser_attributes_opt for transactions to avoid
39797 the pedwarn in C++98 mode. */
39799 static tree
39800 cp_parser_txn_attribute_opt (cp_parser *parser)
39802 cp_token *token;
39803 tree attr_name, attr = NULL;
39805 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
39806 return cp_parser_attributes_opt (parser);
39808 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
39809 return NULL_TREE;
39810 cp_lexer_consume_token (parser->lexer);
39811 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
39812 goto error1;
39814 token = cp_lexer_peek_token (parser->lexer);
39815 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
39817 token = cp_lexer_consume_token (parser->lexer);
39819 attr_name = (token->type == CPP_KEYWORD
39820 /* For keywords, use the canonical spelling,
39821 not the parsed identifier. */
39822 ? ridpointers[(int) token->keyword]
39823 : token->u.value);
39824 attr = build_tree_list (attr_name, NULL_TREE);
39826 else
39827 cp_parser_error (parser, "expected identifier");
39829 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
39830 error1:
39831 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
39832 return attr;
39835 /* Parse a __transaction_atomic or __transaction_relaxed statement.
39837 transaction-statement:
39838 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
39839 compound-statement
39840 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
39843 static tree
39844 cp_parser_transaction (cp_parser *parser, cp_token *token)
39846 unsigned char old_in = parser->in_transaction;
39847 unsigned char this_in = 1, new_in;
39848 enum rid keyword = token->keyword;
39849 tree stmt, attrs, noex;
39851 cp_lexer_consume_token (parser->lexer);
39853 if (keyword == RID_TRANSACTION_RELAXED
39854 || keyword == RID_SYNCHRONIZED)
39855 this_in |= TM_STMT_ATTR_RELAXED;
39856 else
39858 attrs = cp_parser_txn_attribute_opt (parser);
39859 if (attrs)
39860 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
39863 /* Parse a noexcept specification. */
39864 if (keyword == RID_ATOMIC_NOEXCEPT)
39865 noex = boolean_true_node;
39866 else if (keyword == RID_ATOMIC_CANCEL)
39868 /* cancel-and-throw is unimplemented. */
39869 sorry ("atomic_cancel");
39870 noex = NULL_TREE;
39872 else
39873 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
39875 /* Keep track if we're in the lexical scope of an outer transaction. */
39876 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
39878 stmt = begin_transaction_stmt (token->location, NULL, this_in);
39880 parser->in_transaction = new_in;
39881 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
39882 parser->in_transaction = old_in;
39884 finish_transaction_stmt (stmt, NULL, this_in, noex);
39886 return stmt;
39889 /* Parse a __transaction_atomic or __transaction_relaxed expression.
39891 transaction-expression:
39892 __transaction_atomic txn-noexcept-spec[opt] ( expression )
39893 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
39896 static tree
39897 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
39899 unsigned char old_in = parser->in_transaction;
39900 unsigned char this_in = 1;
39901 cp_token *token;
39902 tree expr, noex;
39903 bool noex_expr;
39904 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39906 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
39907 || keyword == RID_TRANSACTION_RELAXED);
39909 if (!flag_tm)
39910 error_at (loc,
39911 keyword == RID_TRANSACTION_RELAXED
39912 ? G_("%<__transaction_relaxed%> without transactional memory "
39913 "support enabled")
39914 : G_("%<__transaction_atomic%> without transactional memory "
39915 "support enabled"));
39917 token = cp_parser_require_keyword (parser, keyword,
39918 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
39919 : RT_TRANSACTION_RELAXED));
39920 gcc_assert (token != NULL);
39922 if (keyword == RID_TRANSACTION_RELAXED)
39923 this_in |= TM_STMT_ATTR_RELAXED;
39925 /* Set this early. This might mean that we allow transaction_cancel in
39926 an expression that we find out later actually has to be a constexpr.
39927 However, we expect that cxx_constant_value will be able to deal with
39928 this; also, if the noexcept has no constexpr, then what we parse next
39929 really is a transaction's body. */
39930 parser->in_transaction = this_in;
39932 /* Parse a noexcept specification. */
39933 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
39934 true);
39936 if (!noex || !noex_expr
39937 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39939 matching_parens parens;
39940 parens.require_open (parser);
39942 expr = cp_parser_expression (parser);
39943 expr = finish_parenthesized_expr (expr);
39945 parens.require_close (parser);
39947 else
39949 /* The only expression that is available got parsed for the noexcept
39950 already. noexcept is true then. */
39951 expr = noex;
39952 noex = boolean_true_node;
39955 expr = build_transaction_expr (token->location, expr, this_in, noex);
39956 parser->in_transaction = old_in;
39958 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
39959 return error_mark_node;
39961 return (flag_tm ? expr : error_mark_node);
39964 /* Parse a function-transaction-block.
39966 function-transaction-block:
39967 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
39968 function-body
39969 __transaction_atomic txn-attribute[opt] function-try-block
39970 __transaction_relaxed ctor-initializer[opt] function-body
39971 __transaction_relaxed function-try-block
39974 static void
39975 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
39977 unsigned char old_in = parser->in_transaction;
39978 unsigned char new_in = 1;
39979 tree compound_stmt, stmt, attrs;
39980 cp_token *token;
39982 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
39983 || keyword == RID_TRANSACTION_RELAXED);
39984 token = cp_parser_require_keyword (parser, keyword,
39985 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
39986 : RT_TRANSACTION_RELAXED));
39987 gcc_assert (token != NULL);
39989 if (keyword == RID_TRANSACTION_RELAXED)
39990 new_in |= TM_STMT_ATTR_RELAXED;
39991 else
39993 attrs = cp_parser_txn_attribute_opt (parser);
39994 if (attrs)
39995 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
39998 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
40000 parser->in_transaction = new_in;
40002 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
40003 cp_parser_function_try_block (parser);
40004 else
40005 cp_parser_ctor_initializer_opt_and_function_body
40006 (parser, /*in_function_try_block=*/false);
40008 parser->in_transaction = old_in;
40010 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
40013 /* Parse a __transaction_cancel statement.
40015 cancel-statement:
40016 __transaction_cancel txn-attribute[opt] ;
40017 __transaction_cancel txn-attribute[opt] throw-expression ;
40019 ??? Cancel and throw is not yet implemented. */
40021 static tree
40022 cp_parser_transaction_cancel (cp_parser *parser)
40024 cp_token *token;
40025 bool is_outer = false;
40026 tree stmt, attrs;
40028 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
40029 RT_TRANSACTION_CANCEL);
40030 gcc_assert (token != NULL);
40032 attrs = cp_parser_txn_attribute_opt (parser);
40033 if (attrs)
40034 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
40036 /* ??? Parse cancel-and-throw here. */
40038 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40040 if (!flag_tm)
40042 error_at (token->location, "%<__transaction_cancel%> without "
40043 "transactional memory support enabled");
40044 return error_mark_node;
40046 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
40048 error_at (token->location, "%<__transaction_cancel%> within a "
40049 "%<__transaction_relaxed%>");
40050 return error_mark_node;
40052 else if (is_outer)
40054 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
40055 && !is_tm_may_cancel_outer (current_function_decl))
40057 error_at (token->location, "outer %<__transaction_cancel%> not "
40058 "within outer %<__transaction_atomic%>");
40059 error_at (token->location,
40060 " or a %<transaction_may_cancel_outer%> function");
40061 return error_mark_node;
40064 else if (parser->in_transaction == 0)
40066 error_at (token->location, "%<__transaction_cancel%> not within "
40067 "%<__transaction_atomic%>");
40068 return error_mark_node;
40071 stmt = build_tm_abort_call (token->location, is_outer);
40072 add_stmt (stmt);
40074 return stmt;
40077 /* The parser. */
40079 static GTY (()) cp_parser *the_parser;
40082 /* Special handling for the first token or line in the file. The first
40083 thing in the file might be #pragma GCC pch_preprocess, which loads a
40084 PCH file, which is a GC collection point. So we need to handle this
40085 first pragma without benefit of an existing lexer structure.
40087 Always returns one token to the caller in *FIRST_TOKEN. This is
40088 either the true first token of the file, or the first token after
40089 the initial pragma. */
40091 static void
40092 cp_parser_initial_pragma (cp_token *first_token)
40094 tree name = NULL;
40096 cp_lexer_get_preprocessor_token (NULL, first_token);
40097 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
40098 return;
40100 cp_lexer_get_preprocessor_token (NULL, first_token);
40101 if (first_token->type == CPP_STRING)
40103 name = first_token->u.value;
40105 cp_lexer_get_preprocessor_token (NULL, first_token);
40106 if (first_token->type != CPP_PRAGMA_EOL)
40107 error_at (first_token->location,
40108 "junk at end of %<#pragma GCC pch_preprocess%>");
40110 else
40111 error_at (first_token->location, "expected string literal");
40113 /* Skip to the end of the pragma. */
40114 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
40115 cp_lexer_get_preprocessor_token (NULL, first_token);
40117 /* Now actually load the PCH file. */
40118 if (name)
40119 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
40121 /* Read one more token to return to our caller. We have to do this
40122 after reading the PCH file in, since its pointers have to be
40123 live. */
40124 cp_lexer_get_preprocessor_token (NULL, first_token);
40127 /* Parse a pragma GCC ivdep. */
40129 static bool
40130 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
40132 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40133 return true;
40136 /* Parse a pragma GCC unroll. */
40138 static unsigned short
40139 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
40141 location_t location = cp_lexer_peek_token (parser->lexer)->location;
40142 tree expr = cp_parser_constant_expression (parser);
40143 unsigned short unroll;
40144 expr = maybe_constant_value (expr);
40145 HOST_WIDE_INT lunroll = 0;
40146 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
40147 || TREE_CODE (expr) != INTEGER_CST
40148 || (lunroll = tree_to_shwi (expr)) < 0
40149 || lunroll >= USHRT_MAX)
40151 error_at (location, "%<#pragma GCC unroll%> requires an"
40152 " assignment-expression that evaluates to a non-negative"
40153 " integral constant less than %u", USHRT_MAX);
40154 unroll = 0;
40156 else
40158 unroll = (unsigned short)lunroll;
40159 if (unroll == 0)
40160 unroll = 1;
40162 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40163 return unroll;
40166 /* Normal parsing of a pragma token. Here we can (and must) use the
40167 regular lexer. */
40169 static bool
40170 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
40172 cp_token *pragma_tok;
40173 unsigned int id;
40174 tree stmt;
40175 bool ret;
40177 pragma_tok = cp_lexer_consume_token (parser->lexer);
40178 gcc_assert (pragma_tok->type == CPP_PRAGMA);
40179 parser->lexer->in_pragma = true;
40181 id = cp_parser_pragma_kind (pragma_tok);
40182 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
40183 cp_ensure_no_omp_declare_simd (parser);
40184 switch (id)
40186 case PRAGMA_GCC_PCH_PREPROCESS:
40187 error_at (pragma_tok->location,
40188 "%<#pragma GCC pch_preprocess%> must be first");
40189 break;
40191 case PRAGMA_OMP_BARRIER:
40192 switch (context)
40194 case pragma_compound:
40195 cp_parser_omp_barrier (parser, pragma_tok);
40196 return false;
40197 case pragma_stmt:
40198 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40199 "used in compound statements", "omp barrier");
40200 break;
40201 default:
40202 goto bad_stmt;
40204 break;
40206 case PRAGMA_OMP_DEPOBJ:
40207 switch (context)
40209 case pragma_compound:
40210 cp_parser_omp_depobj (parser, pragma_tok);
40211 return false;
40212 case pragma_stmt:
40213 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40214 "used in compound statements", "omp depobj");
40215 break;
40216 default:
40217 goto bad_stmt;
40219 break;
40221 case PRAGMA_OMP_FLUSH:
40222 switch (context)
40224 case pragma_compound:
40225 cp_parser_omp_flush (parser, pragma_tok);
40226 return false;
40227 case pragma_stmt:
40228 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40229 "used in compound statements", "omp flush");
40230 break;
40231 default:
40232 goto bad_stmt;
40234 break;
40236 case PRAGMA_OMP_TASKWAIT:
40237 switch (context)
40239 case pragma_compound:
40240 cp_parser_omp_taskwait (parser, pragma_tok);
40241 return false;
40242 case pragma_stmt:
40243 error_at (pragma_tok->location,
40244 "%<#pragma %s%> may only be used in compound statements",
40245 "omp taskwait");
40246 break;
40247 default:
40248 goto bad_stmt;
40250 break;
40252 case PRAGMA_OMP_TASKYIELD:
40253 switch (context)
40255 case pragma_compound:
40256 cp_parser_omp_taskyield (parser, pragma_tok);
40257 return false;
40258 case pragma_stmt:
40259 error_at (pragma_tok->location,
40260 "%<#pragma %s%> may only be used in compound statements",
40261 "omp taskyield");
40262 break;
40263 default:
40264 goto bad_stmt;
40266 break;
40268 case PRAGMA_OMP_CANCEL:
40269 switch (context)
40271 case pragma_compound:
40272 cp_parser_omp_cancel (parser, pragma_tok);
40273 return false;
40274 case pragma_stmt:
40275 error_at (pragma_tok->location,
40276 "%<#pragma %s%> may only be used in compound statements",
40277 "omp cancel");
40278 break;
40279 default:
40280 goto bad_stmt;
40282 break;
40284 case PRAGMA_OMP_CANCELLATION_POINT:
40285 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
40286 return false;
40288 case PRAGMA_OMP_THREADPRIVATE:
40289 cp_parser_omp_threadprivate (parser, pragma_tok);
40290 return false;
40292 case PRAGMA_OMP_DECLARE:
40293 return cp_parser_omp_declare (parser, pragma_tok, context);
40295 case PRAGMA_OACC_DECLARE:
40296 cp_parser_oacc_declare (parser, pragma_tok);
40297 return false;
40299 case PRAGMA_OACC_ENTER_DATA:
40300 if (context == pragma_stmt)
40302 error_at (pragma_tok->location,
40303 "%<#pragma %s%> may only be used in compound statements",
40304 "acc enter data");
40305 break;
40307 else if (context != pragma_compound)
40308 goto bad_stmt;
40309 cp_parser_omp_construct (parser, pragma_tok, if_p);
40310 return true;
40312 case PRAGMA_OACC_EXIT_DATA:
40313 if (context == pragma_stmt)
40315 error_at (pragma_tok->location,
40316 "%<#pragma %s%> may only be used in compound statements",
40317 "acc exit data");
40318 break;
40320 else if (context != pragma_compound)
40321 goto bad_stmt;
40322 cp_parser_omp_construct (parser, pragma_tok, if_p);
40323 return true;
40325 case PRAGMA_OACC_ROUTINE:
40326 if (context != pragma_external)
40328 error_at (pragma_tok->location,
40329 "%<#pragma acc routine%> must be at file scope");
40330 break;
40332 cp_parser_oacc_routine (parser, pragma_tok, context);
40333 return false;
40335 case PRAGMA_OACC_UPDATE:
40336 if (context == pragma_stmt)
40338 error_at (pragma_tok->location,
40339 "%<#pragma %s%> may only be used in compound statements",
40340 "acc update");
40341 break;
40343 else if (context != pragma_compound)
40344 goto bad_stmt;
40345 cp_parser_omp_construct (parser, pragma_tok, if_p);
40346 return true;
40348 case PRAGMA_OACC_WAIT:
40349 if (context == pragma_stmt)
40351 error_at (pragma_tok->location,
40352 "%<#pragma %s%> may only be used in compound statements",
40353 "acc wait");
40354 break;
40356 else if (context != pragma_compound)
40357 goto bad_stmt;
40358 cp_parser_omp_construct (parser, pragma_tok, if_p);
40359 return true;
40361 case PRAGMA_OACC_ATOMIC:
40362 case PRAGMA_OACC_CACHE:
40363 case PRAGMA_OACC_DATA:
40364 case PRAGMA_OACC_HOST_DATA:
40365 case PRAGMA_OACC_KERNELS:
40366 case PRAGMA_OACC_PARALLEL:
40367 case PRAGMA_OACC_LOOP:
40368 case PRAGMA_OMP_ATOMIC:
40369 case PRAGMA_OMP_CRITICAL:
40370 case PRAGMA_OMP_DISTRIBUTE:
40371 case PRAGMA_OMP_FOR:
40372 case PRAGMA_OMP_MASTER:
40373 case PRAGMA_OMP_PARALLEL:
40374 case PRAGMA_OMP_SECTIONS:
40375 case PRAGMA_OMP_SIMD:
40376 case PRAGMA_OMP_SINGLE:
40377 case PRAGMA_OMP_TASK:
40378 case PRAGMA_OMP_TASKGROUP:
40379 case PRAGMA_OMP_TASKLOOP:
40380 case PRAGMA_OMP_TEAMS:
40381 if (context != pragma_stmt && context != pragma_compound)
40382 goto bad_stmt;
40383 stmt = push_omp_privatization_clauses (false);
40384 cp_parser_omp_construct (parser, pragma_tok, if_p);
40385 pop_omp_privatization_clauses (stmt);
40386 return true;
40388 case PRAGMA_OMP_REQUIRES:
40389 return cp_parser_omp_requires (parser, pragma_tok);
40391 case PRAGMA_OMP_ORDERED:
40392 if (context != pragma_stmt && context != pragma_compound)
40393 goto bad_stmt;
40394 stmt = push_omp_privatization_clauses (false);
40395 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
40396 pop_omp_privatization_clauses (stmt);
40397 return ret;
40399 case PRAGMA_OMP_TARGET:
40400 if (context != pragma_stmt && context != pragma_compound)
40401 goto bad_stmt;
40402 stmt = push_omp_privatization_clauses (false);
40403 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
40404 pop_omp_privatization_clauses (stmt);
40405 return ret;
40407 case PRAGMA_OMP_END_DECLARE_TARGET:
40408 cp_parser_omp_end_declare_target (parser, pragma_tok);
40409 return false;
40411 case PRAGMA_OMP_SECTION:
40412 error_at (pragma_tok->location,
40413 "%<#pragma omp section%> may only be used in "
40414 "%<#pragma omp sections%> construct");
40415 break;
40417 case PRAGMA_IVDEP:
40419 if (context == pragma_external)
40421 error_at (pragma_tok->location,
40422 "%<#pragma GCC ivdep%> must be inside a function");
40423 break;
40425 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
40426 unsigned short unroll;
40427 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40428 if (tok->type == CPP_PRAGMA
40429 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
40431 tok = cp_lexer_consume_token (parser->lexer);
40432 unroll = cp_parser_pragma_unroll (parser, tok);
40433 tok = cp_lexer_peek_token (the_parser->lexer);
40435 else
40436 unroll = 0;
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 case PRAGMA_UNROLL:
40451 if (context == pragma_external)
40453 error_at (pragma_tok->location,
40454 "%<#pragma GCC unroll%> must be inside a function");
40455 break;
40457 const unsigned short unroll
40458 = cp_parser_pragma_unroll (parser, pragma_tok);
40459 bool ivdep;
40460 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40461 if (tok->type == CPP_PRAGMA
40462 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
40464 tok = cp_lexer_consume_token (parser->lexer);
40465 ivdep = cp_parser_pragma_ivdep (parser, tok);
40466 tok = cp_lexer_peek_token (the_parser->lexer);
40468 else
40469 ivdep = false;
40470 if (tok->type != CPP_KEYWORD
40471 || (tok->keyword != RID_FOR
40472 && tok->keyword != RID_WHILE
40473 && tok->keyword != RID_DO))
40475 cp_parser_error (parser, "for, while or do statement expected");
40476 return false;
40478 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40479 return true;
40482 default:
40483 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
40484 c_invoke_pragma_handler (id);
40485 break;
40487 bad_stmt:
40488 cp_parser_error (parser, "expected declaration specifiers");
40489 break;
40492 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40493 return false;
40496 /* The interface the pragma parsers have to the lexer. */
40498 enum cpp_ttype
40499 pragma_lex (tree *value, location_t *loc)
40501 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40502 enum cpp_ttype ret = tok->type;
40504 *value = tok->u.value;
40505 if (loc)
40506 *loc = tok->location;
40508 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
40509 ret = CPP_EOF;
40510 else if (ret == CPP_STRING)
40511 *value = cp_parser_string_literal (the_parser, false, false);
40512 else
40514 if (ret == CPP_KEYWORD)
40515 ret = CPP_NAME;
40516 cp_lexer_consume_token (the_parser->lexer);
40519 return ret;
40523 /* External interface. */
40525 /* Parse one entire translation unit. */
40527 void
40528 c_parse_file (void)
40530 static bool already_called = false;
40532 if (already_called)
40533 fatal_error (input_location,
40534 "inter-module optimizations not implemented for C++");
40535 already_called = true;
40537 the_parser = cp_parser_new ();
40538 push_deferring_access_checks (flag_access_control
40539 ? dk_no_deferred : dk_no_check);
40540 cp_parser_translation_unit (the_parser);
40541 the_parser = NULL;
40543 finish_translation_unit ();
40546 /* Create an identifier for a generic parameter type (a synthesized
40547 template parameter implied by `auto' or a concept identifier). */
40549 static GTY(()) int generic_parm_count;
40550 static tree
40551 make_generic_type_name ()
40553 char buf[32];
40554 sprintf (buf, "auto:%d", ++generic_parm_count);
40555 return get_identifier (buf);
40558 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
40559 (creating a new template parameter list if necessary). Returns the newly
40560 created template type parm. */
40562 static tree
40563 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
40565 gcc_assert (current_binding_level->kind == sk_function_parms);
40567 /* Before committing to modifying any scope, if we're in an
40568 implicit template scope, and we're trying to synthesize a
40569 constrained parameter, try to find a previous parameter with
40570 the same name. This is the same-type rule for abbreviated
40571 function templates.
40573 NOTE: We can generate implicit parameters when tentatively
40574 parsing a nested name specifier, only to reject that parse
40575 later. However, matching the same template-id as part of a
40576 direct-declarator should generate an identical template
40577 parameter, so this rule will merge them. */
40578 if (parser->implicit_template_scope && constr)
40580 tree t = parser->implicit_template_parms;
40581 while (t)
40583 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
40585 tree d = TREE_VALUE (t);
40586 if (TREE_CODE (d) == PARM_DECL)
40587 /* Return the TEMPLATE_PARM_INDEX. */
40588 d = DECL_INITIAL (d);
40589 return d;
40591 t = TREE_CHAIN (t);
40595 /* We are either continuing a function template that already contains implicit
40596 template parameters, creating a new fully-implicit function template, or
40597 extending an existing explicit function template with implicit template
40598 parameters. */
40600 cp_binding_level *const entry_scope = current_binding_level;
40602 bool become_template = false;
40603 cp_binding_level *parent_scope = 0;
40605 if (parser->implicit_template_scope)
40607 gcc_assert (parser->implicit_template_parms);
40609 current_binding_level = parser->implicit_template_scope;
40611 else
40613 /* Roll back to the existing template parameter scope (in the case of
40614 extending an explicit function template) or introduce a new template
40615 parameter scope ahead of the function parameter scope (or class scope
40616 in the case of out-of-line member definitions). The function scope is
40617 added back after template parameter synthesis below. */
40619 cp_binding_level *scope = entry_scope;
40621 while (scope->kind == sk_function_parms)
40623 parent_scope = scope;
40624 scope = scope->level_chain;
40626 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
40628 /* If not defining a class, then any class scope is a scope level in
40629 an out-of-line member definition. In this case simply wind back
40630 beyond the first such scope to inject the template parameter list.
40631 Otherwise wind back to the class being defined. The latter can
40632 occur in class member friend declarations such as:
40634 class A {
40635 void foo (auto);
40637 class B {
40638 friend void A::foo (auto);
40641 The template parameter list synthesized for the friend declaration
40642 must be injected in the scope of 'B'. This can also occur in
40643 erroneous cases such as:
40645 struct A {
40646 struct B {
40647 void foo (auto);
40649 void B::foo (auto) {}
40652 Here the attempted definition of 'B::foo' within 'A' is ill-formed
40653 but, nevertheless, the template parameter list synthesized for the
40654 declarator should be injected into the scope of 'A' as if the
40655 ill-formed template was specified explicitly. */
40657 while (scope->kind == sk_class && !scope->defining_class_p)
40659 parent_scope = scope;
40660 scope = scope->level_chain;
40664 current_binding_level = scope;
40666 if (scope->kind != sk_template_parms
40667 || !function_being_declared_is_template_p (parser))
40669 /* Introduce a new template parameter list for implicit template
40670 parameters. */
40672 become_template = true;
40674 parser->implicit_template_scope
40675 = begin_scope (sk_template_parms, NULL);
40677 ++processing_template_decl;
40679 parser->fully_implicit_function_template_p = true;
40680 ++parser->num_template_parameter_lists;
40682 else
40684 /* Synthesize implicit template parameters at the end of the explicit
40685 template parameter list. */
40687 gcc_assert (current_template_parms);
40689 parser->implicit_template_scope = scope;
40691 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
40692 parser->implicit_template_parms
40693 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
40697 /* Synthesize a new template parameter and track the current template
40698 parameter chain with implicit_template_parms. */
40700 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
40701 tree synth_id = make_generic_type_name ();
40702 tree synth_tmpl_parm;
40703 bool non_type = false;
40705 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
40706 synth_tmpl_parm
40707 = finish_template_type_parm (class_type_node, synth_id);
40708 else if (TREE_CODE (proto) == TEMPLATE_DECL)
40709 synth_tmpl_parm
40710 = finish_constrained_template_template_parm (proto, synth_id);
40711 else
40713 synth_tmpl_parm = copy_decl (proto);
40714 DECL_NAME (synth_tmpl_parm) = synth_id;
40715 non_type = true;
40718 // Attach the constraint to the parm before processing.
40719 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
40720 TREE_TYPE (node) = constr;
40721 tree new_parm
40722 = process_template_parm (parser->implicit_template_parms,
40723 input_location,
40724 node,
40725 /*non_type=*/non_type,
40726 /*param_pack=*/false);
40728 // Chain the new parameter to the list of implicit parameters.
40729 if (parser->implicit_template_parms)
40730 parser->implicit_template_parms
40731 = TREE_CHAIN (parser->implicit_template_parms);
40732 else
40733 parser->implicit_template_parms = new_parm;
40735 tree new_decl = get_local_decls ();
40736 if (non_type)
40737 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
40738 new_decl = DECL_INITIAL (new_decl);
40740 /* If creating a fully implicit function template, start the new implicit
40741 template parameter list with this synthesized type, otherwise grow the
40742 current template parameter list. */
40744 if (become_template)
40746 parent_scope->level_chain = current_binding_level;
40748 tree new_parms = make_tree_vec (1);
40749 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
40750 current_template_parms = tree_cons (size_int (processing_template_decl),
40751 new_parms, current_template_parms);
40753 else
40755 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
40756 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
40757 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
40758 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
40761 // If the new parameter was constrained, we need to add that to the
40762 // constraints in the template parameter list.
40763 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
40765 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
40766 reqs = conjoin_constraints (reqs, req);
40767 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
40770 current_binding_level = entry_scope;
40772 return new_decl;
40775 /* Finish the declaration of a fully implicit function template. Such a
40776 template has no explicit template parameter list so has not been through the
40777 normal template head and tail processing. synthesize_implicit_template_parm
40778 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
40779 provided if the declaration is a class member such that its template
40780 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
40781 form is returned. Otherwise NULL_TREE is returned. */
40783 static tree
40784 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
40786 gcc_assert (parser->fully_implicit_function_template_p);
40788 if (member_decl_opt && member_decl_opt != error_mark_node
40789 && DECL_VIRTUAL_P (member_decl_opt))
40791 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
40792 "implicit templates may not be %<virtual%>");
40793 DECL_VIRTUAL_P (member_decl_opt) = false;
40796 if (member_decl_opt)
40797 member_decl_opt = finish_member_template_decl (member_decl_opt);
40798 end_template_decl ();
40800 parser->fully_implicit_function_template_p = false;
40801 parser->implicit_template_parms = 0;
40802 parser->implicit_template_scope = 0;
40803 --parser->num_template_parameter_lists;
40805 return member_decl_opt;
40808 /* Like finish_fully_implicit_template, but to be used in error
40809 recovery, rearranging scopes so that we restore the state we had
40810 before synthesize_implicit_template_parm inserted the implement
40811 template parms scope. */
40813 static void
40814 abort_fully_implicit_template (cp_parser *parser)
40816 cp_binding_level *return_to_scope = current_binding_level;
40818 if (parser->implicit_template_scope
40819 && return_to_scope != parser->implicit_template_scope)
40821 cp_binding_level *child = return_to_scope;
40822 for (cp_binding_level *scope = child->level_chain;
40823 scope != parser->implicit_template_scope;
40824 scope = child->level_chain)
40825 child = scope;
40826 child->level_chain = parser->implicit_template_scope->level_chain;
40827 parser->implicit_template_scope->level_chain = return_to_scope;
40828 current_binding_level = parser->implicit_template_scope;
40830 else
40831 return_to_scope = return_to_scope->level_chain;
40833 finish_fully_implicit_template (parser, NULL);
40835 gcc_assert (current_binding_level == return_to_scope);
40838 /* Helper function for diagnostics that have complained about things
40839 being used with 'extern "C"' linkage.
40841 Attempt to issue a note showing where the 'extern "C"' linkage began. */
40843 void
40844 maybe_show_extern_c_location (void)
40846 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
40847 inform (the_parser->innermost_linkage_specification_location,
40848 "%<extern \"C\"%> linkage started here");
40851 #include "gt-cp-parser.h"