c/
[official-gcc.git] / gcc / cp / parser.c
blob1766ef418a2204ccc87a70fea7bee9cb7aa4419d
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
50 /* The lexer. */
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53 and c-lex.c) and the C++ parser. */
55 static cp_token eof_token =
57 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
60 /* The various kinds of non integral constant we encounter. */
61 enum non_integral_constant {
62 NIC_NONE,
63 /* floating-point literal */
64 NIC_FLOAT,
65 /* %<this%> */
66 NIC_THIS,
67 /* %<__FUNCTION__%> */
68 NIC_FUNC_NAME,
69 /* %<__PRETTY_FUNCTION__%> */
70 NIC_PRETTY_FUNC,
71 /* %<__func__%> */
72 NIC_C99_FUNC,
73 /* "%<va_arg%> */
74 NIC_VA_ARG,
75 /* a cast */
76 NIC_CAST,
77 /* %<typeid%> operator */
78 NIC_TYPEID,
79 /* non-constant compound literals */
80 NIC_NCC,
81 /* a function call */
82 NIC_FUNC_CALL,
83 /* an increment */
84 NIC_INC,
85 /* an decrement */
86 NIC_DEC,
87 /* an array reference */
88 NIC_ARRAY_REF,
89 /* %<->%> */
90 NIC_ARROW,
91 /* %<.%> */
92 NIC_POINT,
93 /* the address of a label */
94 NIC_ADDR_LABEL,
95 /* %<*%> */
96 NIC_STAR,
97 /* %<&%> */
98 NIC_ADDR,
99 /* %<++%> */
100 NIC_PREINCREMENT,
101 /* %<--%> */
102 NIC_PREDECREMENT,
103 /* %<new%> */
104 NIC_NEW,
105 /* %<delete%> */
106 NIC_DEL,
107 /* calls to overloaded operators */
108 NIC_OVERLOADED,
109 /* an assignment */
110 NIC_ASSIGNMENT,
111 /* a comma operator */
112 NIC_COMMA,
113 /* a call to a constructor */
114 NIC_CONSTRUCTOR,
115 /* a transaction expression */
116 NIC_TRANSACTION
119 /* The various kinds of errors about name-lookup failing. */
120 enum name_lookup_error {
121 /* NULL */
122 NLE_NULL,
123 /* is not a type */
124 NLE_TYPE,
125 /* is not a class or namespace */
126 NLE_CXX98,
127 /* is not a class, namespace, or enumeration */
128 NLE_NOT_CXX98
131 /* The various kinds of required token */
132 enum required_token {
133 RT_NONE,
134 RT_SEMICOLON, /* ';' */
135 RT_OPEN_PAREN, /* '(' */
136 RT_CLOSE_BRACE, /* '}' */
137 RT_OPEN_BRACE, /* '{' */
138 RT_CLOSE_SQUARE, /* ']' */
139 RT_OPEN_SQUARE, /* '[' */
140 RT_COMMA, /* ',' */
141 RT_SCOPE, /* '::' */
142 RT_LESS, /* '<' */
143 RT_GREATER, /* '>' */
144 RT_EQ, /* '=' */
145 RT_ELLIPSIS, /* '...' */
146 RT_MULT, /* '*' */
147 RT_COMPL, /* '~' */
148 RT_COLON, /* ':' */
149 RT_COLON_SCOPE, /* ':' or '::' */
150 RT_CLOSE_PAREN, /* ')' */
151 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
152 RT_PRAGMA_EOL, /* end of line */
153 RT_NAME, /* identifier */
155 /* The type is CPP_KEYWORD */
156 RT_NEW, /* new */
157 RT_DELETE, /* delete */
158 RT_RETURN, /* return */
159 RT_WHILE, /* while */
160 RT_EXTERN, /* extern */
161 RT_STATIC_ASSERT, /* static_assert */
162 RT_DECLTYPE, /* decltype */
163 RT_OPERATOR, /* operator */
164 RT_CLASS, /* class */
165 RT_TEMPLATE, /* template */
166 RT_NAMESPACE, /* namespace */
167 RT_USING, /* using */
168 RT_ASM, /* asm */
169 RT_TRY, /* try */
170 RT_CATCH, /* catch */
171 RT_THROW, /* throw */
172 RT_LABEL, /* __label__ */
173 RT_AT_TRY, /* @try */
174 RT_AT_SYNCHRONIZED, /* @synchronized */
175 RT_AT_THROW, /* @throw */
177 RT_SELECT, /* selection-statement */
178 RT_ITERATION, /* iteration-statement */
179 RT_JUMP, /* jump-statement */
180 RT_CLASS_KEY, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL /* __transaction_cancel */
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188 reverting it on destruction. */
190 class type_id_in_expr_sentinel
192 cp_parser *parser;
193 bool saved;
194 public:
195 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
196 : parser (parser),
197 saved (parser->in_type_id_in_expr_p)
198 { parser->in_type_id_in_expr_p = set; }
199 ~type_id_in_expr_sentinel ()
200 { parser->in_type_id_in_expr_p = saved; }
203 /* Prototypes. */
205 static cp_lexer *cp_lexer_new_main
206 (void);
207 static cp_lexer *cp_lexer_new_from_tokens
208 (cp_token_cache *tokens);
209 static void cp_lexer_destroy
210 (cp_lexer *);
211 static int cp_lexer_saving_tokens
212 (const cp_lexer *);
213 static cp_token *cp_lexer_token_at
214 (cp_lexer *, cp_token_position);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer *, cp_token *);
217 static inline cp_token *cp_lexer_peek_token
218 (cp_lexer *);
219 static cp_token *cp_lexer_peek_nth_token
220 (cp_lexer *, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer *, enum rid);
227 static cp_token *cp_lexer_consume_token
228 (cp_lexer *);
229 static void cp_lexer_purge_token
230 (cp_lexer *);
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer *, cp_token_position);
233 static void cp_lexer_save_tokens
234 (cp_lexer *);
235 static void cp_lexer_commit_tokens
236 (cp_lexer *);
237 static void cp_lexer_rollback_tokens
238 (cp_lexer *);
239 static void cp_lexer_print_token
240 (FILE *, cp_token *);
241 static inline bool cp_lexer_debugging_p
242 (cp_lexer *);
243 static void cp_lexer_start_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static void cp_lexer_stop_debugging
246 (cp_lexer *) ATTRIBUTE_UNUSED;
248 static cp_token_cache *cp_token_cache_new
249 (cp_token *, cp_token *);
251 static void cp_parser_initial_pragma
252 (cp_token *);
254 static bool cp_parser_omp_declare_reduction_exprs
255 (tree, cp_parser *);
256 static void cp_finalize_oacc_routine
257 (cp_parser *, tree, bool);
259 /* Manifest constants. */
260 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
261 #define CP_SAVED_TOKEN_STACK 5
263 /* Variables. */
265 /* The stream to which debugging output should be written. */
266 static FILE *cp_lexer_debug_stream;
268 /* Nonzero if we are parsing an unevaluated operand: an operand to
269 sizeof, typeof, or alignof. */
270 int cp_unevaluated_operand;
272 /* Dump up to NUM tokens in BUFFER to FILE starting with token
273 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
274 first token in BUFFER. If NUM is 0, dump all the tokens. If
275 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
276 highlighted by surrounding it in [[ ]]. */
278 static void
279 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
280 cp_token *start_token, unsigned num,
281 cp_token *curr_token)
283 unsigned i, nprinted;
284 cp_token *token;
285 bool do_print;
287 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
289 if (buffer == NULL)
290 return;
292 if (num == 0)
293 num = buffer->length ();
295 if (start_token == NULL)
296 start_token = buffer->address ();
298 if (start_token > buffer->address ())
300 cp_lexer_print_token (file, &(*buffer)[0]);
301 fprintf (file, " ... ");
304 do_print = false;
305 nprinted = 0;
306 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
308 if (token == start_token)
309 do_print = true;
311 if (!do_print)
312 continue;
314 nprinted++;
315 if (token == curr_token)
316 fprintf (file, "[[");
318 cp_lexer_print_token (file, token);
320 if (token == curr_token)
321 fprintf (file, "]]");
323 switch (token->type)
325 case CPP_SEMICOLON:
326 case CPP_OPEN_BRACE:
327 case CPP_CLOSE_BRACE:
328 case CPP_EOF:
329 fputc ('\n', file);
330 break;
332 default:
333 fputc (' ', file);
337 if (i == num && i < buffer->length ())
339 fprintf (file, " ... ");
340 cp_lexer_print_token (file, &buffer->last ());
343 fprintf (file, "\n");
347 /* Dump all tokens in BUFFER to stderr. */
349 void
350 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
352 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
355 DEBUG_FUNCTION void
356 debug (vec<cp_token, va_gc> &ref)
358 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
361 DEBUG_FUNCTION void
362 debug (vec<cp_token, va_gc> *ptr)
364 if (ptr)
365 debug (*ptr);
366 else
367 fprintf (stderr, "<nil>\n");
371 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
372 description for T. */
374 static void
375 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
377 if (t)
379 fprintf (file, "%s: ", desc);
380 print_node_brief (file, "", t, 0);
385 /* Dump parser context C to FILE. */
387 static void
388 cp_debug_print_context (FILE *file, cp_parser_context *c)
390 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
391 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
392 print_node_brief (file, "", c->object_type, 0);
393 fprintf (file, "}\n");
397 /* Print the stack of parsing contexts to FILE starting with FIRST. */
399 static void
400 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
402 unsigned i;
403 cp_parser_context *c;
405 fprintf (file, "Parsing context stack:\n");
406 for (i = 0, c = first; c; c = c->next, i++)
408 fprintf (file, "\t#%u: ", i);
409 cp_debug_print_context (file, c);
414 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
416 static void
417 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
419 if (flag)
420 fprintf (file, "%s: true\n", desc);
424 /* Print an unparsed function entry UF to FILE. */
426 static void
427 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
429 unsigned i;
430 cp_default_arg_entry *default_arg_fn;
431 tree fn;
433 fprintf (file, "\tFunctions with default args:\n");
434 for (i = 0;
435 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
436 i++)
438 fprintf (file, "\t\tClass type: ");
439 print_node_brief (file, "", default_arg_fn->class_type, 0);
440 fprintf (file, "\t\tDeclaration: ");
441 print_node_brief (file, "", default_arg_fn->decl, 0);
442 fprintf (file, "\n");
445 fprintf (file, "\n\tFunctions with definitions that require "
446 "post-processing\n\t\t");
447 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
449 print_node_brief (file, "", fn, 0);
450 fprintf (file, " ");
452 fprintf (file, "\n");
454 fprintf (file, "\n\tNon-static data members with initializers that require "
455 "post-processing\n\t\t");
456 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
458 print_node_brief (file, "", fn, 0);
459 fprintf (file, " ");
461 fprintf (file, "\n");
465 /* Print the stack of unparsed member functions S to FILE. */
467 static void
468 cp_debug_print_unparsed_queues (FILE *file,
469 vec<cp_unparsed_functions_entry, va_gc> *s)
471 unsigned i;
472 cp_unparsed_functions_entry *uf;
474 fprintf (file, "Unparsed functions\n");
475 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
477 fprintf (file, "#%u:\n", i);
478 cp_debug_print_unparsed_function (file, uf);
483 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
484 the given PARSER. If FILE is NULL, the output is printed on stderr. */
486 static void
487 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
489 cp_token *next_token, *first_token, *start_token;
491 if (file == NULL)
492 file = stderr;
494 next_token = parser->lexer->next_token;
495 first_token = parser->lexer->buffer->address ();
496 start_token = (next_token > first_token + window_size / 2)
497 ? next_token - window_size / 2
498 : first_token;
499 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
500 next_token);
504 /* Dump debugging information for the given PARSER. If FILE is NULL,
505 the output is printed on stderr. */
507 void
508 cp_debug_parser (FILE *file, cp_parser *parser)
510 const size_t window_size = 20;
511 cp_token *token;
512 expanded_location eloc;
514 if (file == NULL)
515 file = stderr;
517 fprintf (file, "Parser state\n\n");
518 fprintf (file, "Number of tokens: %u\n",
519 vec_safe_length (parser->lexer->buffer));
520 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
521 cp_debug_print_tree_if_set (file, "Object scope",
522 parser->object_scope);
523 cp_debug_print_tree_if_set (file, "Qualifying scope",
524 parser->qualifying_scope);
525 cp_debug_print_context_stack (file, parser->context);
526 cp_debug_print_flag (file, "Allow GNU extensions",
527 parser->allow_gnu_extensions_p);
528 cp_debug_print_flag (file, "'>' token is greater-than",
529 parser->greater_than_is_operator_p);
530 cp_debug_print_flag (file, "Default args allowed in current "
531 "parameter list", parser->default_arg_ok_p);
532 cp_debug_print_flag (file, "Parsing integral constant-expression",
533 parser->integral_constant_expression_p);
534 cp_debug_print_flag (file, "Allow non-constant expression in current "
535 "constant-expression",
536 parser->allow_non_integral_constant_expression_p);
537 cp_debug_print_flag (file, "Seen non-constant expression",
538 parser->non_integral_constant_expression_p);
539 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
540 "current context",
541 parser->local_variables_forbidden_p);
542 cp_debug_print_flag (file, "In unbraced linkage specification",
543 parser->in_unbraced_linkage_specification_p);
544 cp_debug_print_flag (file, "Parsing a declarator",
545 parser->in_declarator_p);
546 cp_debug_print_flag (file, "In template argument list",
547 parser->in_template_argument_list_p);
548 cp_debug_print_flag (file, "Parsing an iteration statement",
549 parser->in_statement & IN_ITERATION_STMT);
550 cp_debug_print_flag (file, "Parsing a switch statement",
551 parser->in_statement & IN_SWITCH_STMT);
552 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
553 parser->in_statement & IN_OMP_BLOCK);
554 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
555 parser->in_statement & IN_OMP_FOR);
556 cp_debug_print_flag (file, "Parsing an if statement",
557 parser->in_statement & IN_IF_STMT);
558 cp_debug_print_flag (file, "Parsing a type-id in an expression "
559 "context", parser->in_type_id_in_expr_p);
560 cp_debug_print_flag (file, "String expressions should be translated "
561 "to execution character set",
562 parser->translate_strings_p);
563 cp_debug_print_flag (file, "Parsing function body outside of a "
564 "local class", parser->in_function_body);
565 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
566 parser->colon_corrects_to_scope_p);
567 cp_debug_print_flag (file, "Colon doesn't start a class definition",
568 parser->colon_doesnt_start_class_def_p);
569 if (parser->type_definition_forbidden_message)
570 fprintf (file, "Error message for forbidden type definitions: %s\n",
571 parser->type_definition_forbidden_message);
572 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
573 fprintf (file, "Number of class definitions in progress: %u\n",
574 parser->num_classes_being_defined);
575 fprintf (file, "Number of template parameter lists for the current "
576 "declaration: %u\n", parser->num_template_parameter_lists);
577 cp_debug_parser_tokens (file, parser, window_size);
578 token = parser->lexer->next_token;
579 fprintf (file, "Next token to parse:\n");
580 fprintf (file, "\tToken: ");
581 cp_lexer_print_token (file, token);
582 eloc = expand_location (token->location);
583 fprintf (file, "\n\tFile: %s\n", eloc.file);
584 fprintf (file, "\tLine: %d\n", eloc.line);
585 fprintf (file, "\tColumn: %d\n", eloc.column);
588 DEBUG_FUNCTION void
589 debug (cp_parser &ref)
591 cp_debug_parser (stderr, &ref);
594 DEBUG_FUNCTION void
595 debug (cp_parser *ptr)
597 if (ptr)
598 debug (*ptr);
599 else
600 fprintf (stderr, "<nil>\n");
603 /* Allocate memory for a new lexer object and return it. */
605 static cp_lexer *
606 cp_lexer_alloc (void)
608 cp_lexer *lexer;
610 c_common_no_more_pch ();
612 /* Allocate the memory. */
613 lexer = ggc_cleared_alloc<cp_lexer> ();
615 /* Initially we are not debugging. */
616 lexer->debugging_p = false;
618 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
620 /* Create the buffer. */
621 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
623 return lexer;
627 /* Create a new main C++ lexer, the lexer that gets tokens from the
628 preprocessor. */
630 static cp_lexer *
631 cp_lexer_new_main (void)
633 cp_lexer *lexer;
634 cp_token token;
636 /* It's possible that parsing the first pragma will load a PCH file,
637 which is a GC collection point. So we have to do that before
638 allocating any memory. */
639 cp_parser_initial_pragma (&token);
641 lexer = cp_lexer_alloc ();
643 /* Put the first token in the buffer. */
644 lexer->buffer->quick_push (token);
646 /* Get the remaining tokens from the preprocessor. */
647 while (token.type != CPP_EOF)
649 cp_lexer_get_preprocessor_token (lexer, &token);
650 vec_safe_push (lexer->buffer, token);
653 lexer->last_token = lexer->buffer->address ()
654 + lexer->buffer->length ()
655 - 1;
656 lexer->next_token = lexer->buffer->length ()
657 ? lexer->buffer->address ()
658 : &eof_token;
660 /* Subsequent preprocessor diagnostics should use compiler
661 diagnostic functions to get the compiler source location. */
662 done_lexing = true;
664 gcc_assert (!lexer->next_token->purged_p);
665 return lexer;
668 /* Create a new lexer whose token stream is primed with the tokens in
669 CACHE. When these tokens are exhausted, no new tokens will be read. */
671 static cp_lexer *
672 cp_lexer_new_from_tokens (cp_token_cache *cache)
674 cp_token *first = cache->first;
675 cp_token *last = cache->last;
676 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
678 /* We do not own the buffer. */
679 lexer->buffer = NULL;
680 lexer->next_token = first == last ? &eof_token : first;
681 lexer->last_token = last;
683 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
685 /* Initially we are not debugging. */
686 lexer->debugging_p = false;
688 gcc_assert (!lexer->next_token->purged_p);
689 return lexer;
692 /* Frees all resources associated with LEXER. */
694 static void
695 cp_lexer_destroy (cp_lexer *lexer)
697 vec_free (lexer->buffer);
698 lexer->saved_tokens.release ();
699 ggc_free (lexer);
702 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
703 be used. The point of this flag is to help the compiler to fold away calls
704 to cp_lexer_debugging_p within this source file at compile time, when the
705 lexer is not being debugged. */
707 #define LEXER_DEBUGGING_ENABLED_P false
709 /* Returns nonzero if debugging information should be output. */
711 static inline bool
712 cp_lexer_debugging_p (cp_lexer *lexer)
714 if (!LEXER_DEBUGGING_ENABLED_P)
715 return false;
717 return lexer->debugging_p;
721 static inline cp_token_position
722 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
724 gcc_assert (!previous_p || lexer->next_token != &eof_token);
726 return lexer->next_token - previous_p;
729 static inline cp_token *
730 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
732 return pos;
735 static inline void
736 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
738 lexer->next_token = cp_lexer_token_at (lexer, pos);
741 static inline cp_token_position
742 cp_lexer_previous_token_position (cp_lexer *lexer)
744 if (lexer->next_token == &eof_token)
745 return lexer->last_token - 1;
746 else
747 return cp_lexer_token_position (lexer, true);
750 static inline cp_token *
751 cp_lexer_previous_token (cp_lexer *lexer)
753 cp_token_position tp = cp_lexer_previous_token_position (lexer);
755 /* Skip past purged tokens. */
756 while (tp->purged_p)
758 gcc_assert (tp != vec_safe_address (lexer->buffer));
759 tp--;
762 return cp_lexer_token_at (lexer, tp);
765 /* nonzero if we are presently saving tokens. */
767 static inline int
768 cp_lexer_saving_tokens (const cp_lexer* lexer)
770 return lexer->saved_tokens.length () != 0;
773 /* Store the next token from the preprocessor in *TOKEN. Return true
774 if we reach EOF. If LEXER is NULL, assume we are handling an
775 initial #pragma pch_preprocess, and thus want the lexer to return
776 processed strings. */
778 static void
779 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
781 static int is_extern_c = 0;
783 /* Get a new token from the preprocessor. */
784 token->type
785 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
786 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
787 token->keyword = RID_MAX;
788 token->purged_p = false;
789 token->error_reported = false;
791 /* On some systems, some header files are surrounded by an
792 implicit extern "C" block. Set a flag in the token if it
793 comes from such a header. */
794 is_extern_c += pending_lang_change;
795 pending_lang_change = 0;
796 token->implicit_extern_c = is_extern_c > 0;
798 /* Check to see if this token is a keyword. */
799 if (token->type == CPP_NAME)
801 if (IDENTIFIER_KEYWORD_P (token->u.value))
803 /* Mark this token as a keyword. */
804 token->type = CPP_KEYWORD;
805 /* Record which keyword. */
806 token->keyword = C_RID_CODE (token->u.value);
808 else
810 if (warn_cxx11_compat
811 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
812 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
814 /* Warn about the C++0x keyword (but still treat it as
815 an identifier). */
816 warning (OPT_Wc__11_compat,
817 "identifier %qE is a keyword in C++11",
818 token->u.value);
820 /* Clear out the C_RID_CODE so we don't warn about this
821 particular identifier-turned-keyword again. */
822 C_SET_RID_CODE (token->u.value, RID_MAX);
825 token->keyword = RID_MAX;
828 else if (token->type == CPP_AT_NAME)
830 /* This only happens in Objective-C++; it must be a keyword. */
831 token->type = CPP_KEYWORD;
832 switch (C_RID_CODE (token->u.value))
834 /* Replace 'class' with '@class', 'private' with '@private',
835 etc. This prevents confusion with the C++ keyword
836 'class', and makes the tokens consistent with other
837 Objective-C 'AT' keywords. For example '@class' is
838 reported as RID_AT_CLASS which is consistent with
839 '@synchronized', which is reported as
840 RID_AT_SYNCHRONIZED.
842 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
843 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
844 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
845 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
846 case RID_THROW: token->keyword = RID_AT_THROW; break;
847 case RID_TRY: token->keyword = RID_AT_TRY; break;
848 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
849 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
850 default: token->keyword = C_RID_CODE (token->u.value);
855 /* Update the globals input_location and the input file stack from TOKEN. */
856 static inline void
857 cp_lexer_set_source_position_from_token (cp_token *token)
859 if (token->type != CPP_EOF)
861 input_location = token->location;
865 /* Update the globals input_location and the input file stack from LEXER. */
866 static inline void
867 cp_lexer_set_source_position (cp_lexer *lexer)
869 cp_token *token = cp_lexer_peek_token (lexer);
870 cp_lexer_set_source_position_from_token (token);
873 /* Return a pointer to the next token in the token stream, but do not
874 consume it. */
876 static inline cp_token *
877 cp_lexer_peek_token (cp_lexer *lexer)
879 if (cp_lexer_debugging_p (lexer))
881 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
882 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
883 putc ('\n', cp_lexer_debug_stream);
885 return lexer->next_token;
888 /* Return true if the next token has the indicated TYPE. */
890 static inline bool
891 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
893 return cp_lexer_peek_token (lexer)->type == type;
896 /* Return true if the next token does not have the indicated TYPE. */
898 static inline bool
899 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
901 return !cp_lexer_next_token_is (lexer, type);
904 /* Return true if the next token is the indicated KEYWORD. */
906 static inline bool
907 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
909 return cp_lexer_peek_token (lexer)->keyword == keyword;
912 static inline bool
913 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
915 return cp_lexer_peek_nth_token (lexer, n)->type == type;
918 static inline bool
919 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
921 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
924 /* Return true if KEYWORD can start a decl-specifier. */
926 bool
927 cp_keyword_starts_decl_specifier_p (enum rid keyword)
929 switch (keyword)
931 /* auto specifier: storage-class-specifier in C++,
932 simple-type-specifier in C++0x. */
933 case RID_AUTO:
934 /* Storage classes. */
935 case RID_REGISTER:
936 case RID_STATIC:
937 case RID_EXTERN:
938 case RID_MUTABLE:
939 case RID_THREAD:
940 /* Elaborated type specifiers. */
941 case RID_ENUM:
942 case RID_CLASS:
943 case RID_STRUCT:
944 case RID_UNION:
945 case RID_TYPENAME:
946 /* Simple type specifiers. */
947 case RID_CHAR:
948 case RID_CHAR16:
949 case RID_CHAR32:
950 case RID_WCHAR:
951 case RID_BOOL:
952 case RID_SHORT:
953 case RID_INT:
954 case RID_LONG:
955 case RID_SIGNED:
956 case RID_UNSIGNED:
957 case RID_FLOAT:
958 case RID_DOUBLE:
959 case RID_VOID:
960 /* GNU extensions. */
961 case RID_ATTRIBUTE:
962 case RID_TYPEOF:
963 /* C++0x extensions. */
964 case RID_DECLTYPE:
965 case RID_UNDERLYING_TYPE:
966 case RID_CONSTEXPR:
967 return true;
969 default:
970 if (keyword >= RID_FIRST_INT_N
971 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
972 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
973 return true;
974 return false;
978 /* Return true if the next token is a keyword for a decl-specifier. */
980 static bool
981 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
983 cp_token *token;
985 token = cp_lexer_peek_token (lexer);
986 return cp_keyword_starts_decl_specifier_p (token->keyword);
989 /* Returns TRUE iff the token T begins a decltype type. */
991 static bool
992 token_is_decltype (cp_token *t)
994 return (t->keyword == RID_DECLTYPE
995 || t->type == CPP_DECLTYPE);
998 /* Returns TRUE iff the next token begins a decltype type. */
1000 static bool
1001 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1003 cp_token *t = cp_lexer_peek_token (lexer);
1004 return token_is_decltype (t);
1007 /* Called when processing a token with tree_check_value; perform or defer the
1008 associated checks and return the value. */
1010 static tree
1011 saved_checks_value (struct tree_check *check_value)
1013 /* Perform any access checks that were deferred. */
1014 vec<deferred_access_check, va_gc> *checks;
1015 deferred_access_check *chk;
1016 checks = check_value->checks;
1017 if (checks)
1019 int i;
1020 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1021 perform_or_defer_access_check (chk->binfo,
1022 chk->decl,
1023 chk->diag_decl, tf_warning_or_error);
1025 /* Return the stored value. */
1026 return check_value->value;
1029 /* Return a pointer to the Nth token in the token stream. If N is 1,
1030 then this is precisely equivalent to cp_lexer_peek_token (except
1031 that it is not inline). One would like to disallow that case, but
1032 there is one case (cp_parser_nth_token_starts_template_id) where
1033 the caller passes a variable for N and it might be 1. */
1035 static cp_token *
1036 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1038 cp_token *token;
1040 /* N is 1-based, not zero-based. */
1041 gcc_assert (n > 0);
1043 if (cp_lexer_debugging_p (lexer))
1044 fprintf (cp_lexer_debug_stream,
1045 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1047 --n;
1048 token = lexer->next_token;
1049 gcc_assert (!n || token != &eof_token);
1050 while (n != 0)
1052 ++token;
1053 if (token == lexer->last_token)
1055 token = &eof_token;
1056 break;
1059 if (!token->purged_p)
1060 --n;
1063 if (cp_lexer_debugging_p (lexer))
1065 cp_lexer_print_token (cp_lexer_debug_stream, token);
1066 putc ('\n', cp_lexer_debug_stream);
1069 return token;
1072 /* Return the next token, and advance the lexer's next_token pointer
1073 to point to the next non-purged token. */
1075 static cp_token *
1076 cp_lexer_consume_token (cp_lexer* lexer)
1078 cp_token *token = lexer->next_token;
1080 gcc_assert (token != &eof_token);
1081 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1085 lexer->next_token++;
1086 if (lexer->next_token == lexer->last_token)
1088 lexer->next_token = &eof_token;
1089 break;
1093 while (lexer->next_token->purged_p);
1095 cp_lexer_set_source_position_from_token (token);
1097 /* Provide debugging output. */
1098 if (cp_lexer_debugging_p (lexer))
1100 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1101 cp_lexer_print_token (cp_lexer_debug_stream, token);
1102 putc ('\n', cp_lexer_debug_stream);
1105 return token;
1108 /* Permanently remove the next token from the token stream, and
1109 advance the next_token pointer to refer to the next non-purged
1110 token. */
1112 static void
1113 cp_lexer_purge_token (cp_lexer *lexer)
1115 cp_token *tok = lexer->next_token;
1117 gcc_assert (tok != &eof_token);
1118 tok->purged_p = true;
1119 tok->location = UNKNOWN_LOCATION;
1120 tok->u.value = NULL_TREE;
1121 tok->keyword = RID_MAX;
1125 tok++;
1126 if (tok == lexer->last_token)
1128 tok = &eof_token;
1129 break;
1132 while (tok->purged_p);
1133 lexer->next_token = tok;
1136 /* Permanently remove all tokens after TOK, up to, but not
1137 including, the token that will be returned next by
1138 cp_lexer_peek_token. */
1140 static void
1141 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1143 cp_token *peek = lexer->next_token;
1145 if (peek == &eof_token)
1146 peek = lexer->last_token;
1148 gcc_assert (tok < peek);
1150 for ( tok += 1; tok != peek; tok += 1)
1152 tok->purged_p = true;
1153 tok->location = UNKNOWN_LOCATION;
1154 tok->u.value = NULL_TREE;
1155 tok->keyword = RID_MAX;
1159 /* Begin saving tokens. All tokens consumed after this point will be
1160 preserved. */
1162 static void
1163 cp_lexer_save_tokens (cp_lexer* lexer)
1165 /* Provide debugging output. */
1166 if (cp_lexer_debugging_p (lexer))
1167 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1169 lexer->saved_tokens.safe_push (lexer->next_token);
1172 /* Commit to the portion of the token stream most recently saved. */
1174 static void
1175 cp_lexer_commit_tokens (cp_lexer* lexer)
1177 /* Provide debugging output. */
1178 if (cp_lexer_debugging_p (lexer))
1179 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1181 lexer->saved_tokens.pop ();
1184 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1185 to the token stream. Stop saving tokens. */
1187 static void
1188 cp_lexer_rollback_tokens (cp_lexer* lexer)
1190 /* Provide debugging output. */
1191 if (cp_lexer_debugging_p (lexer))
1192 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1194 lexer->next_token = lexer->saved_tokens.pop ();
1197 /* RAII wrapper around the above functions, with sanity checking. Creating
1198 a variable saves tokens, which are committed when the variable is
1199 destroyed unless they are explicitly rolled back by calling the rollback
1200 member function. */
1202 struct saved_token_sentinel
1204 cp_lexer *lexer;
1205 unsigned len;
1206 bool commit;
1207 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1209 len = lexer->saved_tokens.length ();
1210 cp_lexer_save_tokens (lexer);
1212 void rollback ()
1214 cp_lexer_rollback_tokens (lexer);
1215 commit = false;
1217 ~saved_token_sentinel()
1219 if (commit)
1220 cp_lexer_commit_tokens (lexer);
1221 gcc_assert (lexer->saved_tokens.length () == len);
1225 /* Print a representation of the TOKEN on the STREAM. */
1227 static void
1228 cp_lexer_print_token (FILE * stream, cp_token *token)
1230 /* We don't use cpp_type2name here because the parser defines
1231 a few tokens of its own. */
1232 static const char *const token_names[] = {
1233 /* cpplib-defined token types */
1234 #define OP(e, s) #e,
1235 #define TK(e, s) #e,
1236 TTYPE_TABLE
1237 #undef OP
1238 #undef TK
1239 /* C++ parser token types - see "Manifest constants", above. */
1240 "KEYWORD",
1241 "TEMPLATE_ID",
1242 "NESTED_NAME_SPECIFIER",
1245 /* For some tokens, print the associated data. */
1246 switch (token->type)
1248 case CPP_KEYWORD:
1249 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1250 For example, `struct' is mapped to an INTEGER_CST. */
1251 if (!identifier_p (token->u.value))
1252 break;
1253 /* fall through */
1254 case CPP_NAME:
1255 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1256 break;
1258 case CPP_STRING:
1259 case CPP_STRING16:
1260 case CPP_STRING32:
1261 case CPP_WSTRING:
1262 case CPP_UTF8STRING:
1263 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1264 break;
1266 case CPP_NUMBER:
1267 print_generic_expr (stream, token->u.value);
1268 break;
1270 default:
1271 /* If we have a name for the token, print it out. Otherwise, we
1272 simply give the numeric code. */
1273 if (token->type < ARRAY_SIZE(token_names))
1274 fputs (token_names[token->type], stream);
1275 else
1276 fprintf (stream, "[%d]", token->type);
1277 break;
1281 DEBUG_FUNCTION void
1282 debug (cp_token &ref)
1284 cp_lexer_print_token (stderr, &ref);
1285 fprintf (stderr, "\n");
1288 DEBUG_FUNCTION void
1289 debug (cp_token *ptr)
1291 if (ptr)
1292 debug (*ptr);
1293 else
1294 fprintf (stderr, "<nil>\n");
1298 /* Start emitting debugging information. */
1300 static void
1301 cp_lexer_start_debugging (cp_lexer* lexer)
1303 if (!LEXER_DEBUGGING_ENABLED_P)
1304 fatal_error (input_location,
1305 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1307 lexer->debugging_p = true;
1308 cp_lexer_debug_stream = stderr;
1311 /* Stop emitting debugging information. */
1313 static void
1314 cp_lexer_stop_debugging (cp_lexer* lexer)
1316 if (!LEXER_DEBUGGING_ENABLED_P)
1317 fatal_error (input_location,
1318 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1320 lexer->debugging_p = false;
1321 cp_lexer_debug_stream = NULL;
1324 /* Create a new cp_token_cache, representing a range of tokens. */
1326 static cp_token_cache *
1327 cp_token_cache_new (cp_token *first, cp_token *last)
1329 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1330 cache->first = first;
1331 cache->last = last;
1332 return cache;
1335 /* Diagnose if #pragma omp declare simd isn't followed immediately
1336 by function declaration or definition. */
1338 static inline void
1339 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1341 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1343 error ("%<#pragma omp declare simd%> not immediately followed by "
1344 "function declaration or definition");
1345 parser->omp_declare_simd = NULL;
1349 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1350 and put that into "omp declare simd" attribute. */
1352 static inline void
1353 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1355 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1357 if (fndecl == error_mark_node)
1359 parser->omp_declare_simd = NULL;
1360 return;
1362 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1364 cp_ensure_no_omp_declare_simd (parser);
1365 return;
1370 /* Diagnose if #pragma acc routine isn't followed immediately by function
1371 declaration or definition. */
1373 static inline void
1374 cp_ensure_no_oacc_routine (cp_parser *parser)
1376 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1378 error_at (parser->oacc_routine->loc,
1379 "%<#pragma acc routine%> not immediately followed by "
1380 "function declaration or definition");
1381 parser->oacc_routine = NULL;
1385 /* Decl-specifiers. */
1387 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1389 static void
1390 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1392 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1395 /* Declarators. */
1397 /* Nothing other than the parser should be creating declarators;
1398 declarators are a semi-syntactic representation of C++ entities.
1399 Other parts of the front end that need to create entities (like
1400 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1402 static cp_declarator *make_call_declarator
1403 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1404 static cp_declarator *make_array_declarator
1405 (cp_declarator *, tree);
1406 static cp_declarator *make_pointer_declarator
1407 (cp_cv_quals, cp_declarator *, tree);
1408 static cp_declarator *make_reference_declarator
1409 (cp_cv_quals, cp_declarator *, bool, tree);
1410 static cp_declarator *make_ptrmem_declarator
1411 (cp_cv_quals, tree, cp_declarator *, tree);
1413 /* An erroneous declarator. */
1414 static cp_declarator *cp_error_declarator;
1416 /* The obstack on which declarators and related data structures are
1417 allocated. */
1418 static struct obstack declarator_obstack;
1420 /* Alloc BYTES from the declarator memory pool. */
1422 static inline void *
1423 alloc_declarator (size_t bytes)
1425 return obstack_alloc (&declarator_obstack, bytes);
1428 /* Allocate a declarator of the indicated KIND. Clear fields that are
1429 common to all declarators. */
1431 static cp_declarator *
1432 make_declarator (cp_declarator_kind kind)
1434 cp_declarator *declarator;
1436 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1437 declarator->kind = kind;
1438 declarator->parenthesized = UNKNOWN_LOCATION;
1439 declarator->attributes = NULL_TREE;
1440 declarator->std_attributes = NULL_TREE;
1441 declarator->declarator = NULL;
1442 declarator->parameter_pack_p = false;
1443 declarator->id_loc = UNKNOWN_LOCATION;
1445 return declarator;
1448 /* Make a declarator for a generalized identifier. If
1449 QUALIFYING_SCOPE is non-NULL, the identifier is
1450 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1451 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1452 is, if any. */
1454 static cp_declarator *
1455 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1456 special_function_kind sfk)
1458 cp_declarator *declarator;
1460 /* It is valid to write:
1462 class C { void f(); };
1463 typedef C D;
1464 void D::f();
1466 The standard is not clear about whether `typedef const C D' is
1467 legal; as of 2002-09-15 the committee is considering that
1468 question. EDG 3.0 allows that syntax. Therefore, we do as
1469 well. */
1470 if (qualifying_scope && TYPE_P (qualifying_scope))
1471 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1473 gcc_assert (identifier_p (unqualified_name)
1474 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1475 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1477 declarator = make_declarator (cdk_id);
1478 declarator->u.id.qualifying_scope = qualifying_scope;
1479 declarator->u.id.unqualified_name = unqualified_name;
1480 declarator->u.id.sfk = sfk;
1482 return declarator;
1485 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1486 of modifiers such as const or volatile to apply to the pointer
1487 type, represented as identifiers. ATTRIBUTES represent the attributes that
1488 appertain to the pointer or reference. */
1490 cp_declarator *
1491 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1492 tree attributes)
1494 cp_declarator *declarator;
1496 declarator = make_declarator (cdk_pointer);
1497 declarator->declarator = target;
1498 declarator->u.pointer.qualifiers = cv_qualifiers;
1499 declarator->u.pointer.class_type = NULL_TREE;
1500 if (target)
1502 declarator->id_loc = target->id_loc;
1503 declarator->parameter_pack_p = target->parameter_pack_p;
1504 target->parameter_pack_p = false;
1506 else
1507 declarator->parameter_pack_p = false;
1509 declarator->std_attributes = attributes;
1511 return declarator;
1514 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1515 represent the attributes that appertain to the pointer or
1516 reference. */
1518 cp_declarator *
1519 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1520 bool rvalue_ref, tree attributes)
1522 cp_declarator *declarator;
1524 declarator = make_declarator (cdk_reference);
1525 declarator->declarator = target;
1526 declarator->u.reference.qualifiers = cv_qualifiers;
1527 declarator->u.reference.rvalue_ref = rvalue_ref;
1528 if (target)
1530 declarator->id_loc = target->id_loc;
1531 declarator->parameter_pack_p = target->parameter_pack_p;
1532 target->parameter_pack_p = false;
1534 else
1535 declarator->parameter_pack_p = false;
1537 declarator->std_attributes = attributes;
1539 return declarator;
1542 /* Like make_pointer_declarator -- but for a pointer to a non-static
1543 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1544 appertain to the pointer or reference. */
1546 cp_declarator *
1547 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1548 cp_declarator *pointee,
1549 tree attributes)
1551 cp_declarator *declarator;
1553 declarator = make_declarator (cdk_ptrmem);
1554 declarator->declarator = pointee;
1555 declarator->u.pointer.qualifiers = cv_qualifiers;
1556 declarator->u.pointer.class_type = class_type;
1558 if (pointee)
1560 declarator->parameter_pack_p = pointee->parameter_pack_p;
1561 pointee->parameter_pack_p = false;
1563 else
1564 declarator->parameter_pack_p = false;
1566 declarator->std_attributes = attributes;
1568 return declarator;
1571 /* Make a declarator for the function given by TARGET, with the
1572 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1573 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1574 indicates what exceptions can be thrown. */
1576 cp_declarator *
1577 make_call_declarator (cp_declarator *target,
1578 tree parms,
1579 cp_cv_quals cv_qualifiers,
1580 cp_virt_specifiers virt_specifiers,
1581 cp_ref_qualifier ref_qualifier,
1582 tree tx_qualifier,
1583 tree exception_specification,
1584 tree late_return_type,
1585 tree requires_clause)
1587 cp_declarator *declarator;
1589 declarator = make_declarator (cdk_function);
1590 declarator->declarator = target;
1591 declarator->u.function.parameters = parms;
1592 declarator->u.function.qualifiers = cv_qualifiers;
1593 declarator->u.function.virt_specifiers = virt_specifiers;
1594 declarator->u.function.ref_qualifier = ref_qualifier;
1595 declarator->u.function.tx_qualifier = tx_qualifier;
1596 declarator->u.function.exception_specification = exception_specification;
1597 declarator->u.function.late_return_type = late_return_type;
1598 declarator->u.function.requires_clause = requires_clause;
1599 if (target)
1601 declarator->id_loc = target->id_loc;
1602 declarator->parameter_pack_p = target->parameter_pack_p;
1603 target->parameter_pack_p = false;
1605 else
1606 declarator->parameter_pack_p = false;
1608 return declarator;
1611 /* Make a declarator for an array of BOUNDS elements, each of which is
1612 defined by ELEMENT. */
1614 cp_declarator *
1615 make_array_declarator (cp_declarator *element, tree bounds)
1617 cp_declarator *declarator;
1619 declarator = make_declarator (cdk_array);
1620 declarator->declarator = element;
1621 declarator->u.array.bounds = bounds;
1622 if (element)
1624 declarator->id_loc = element->id_loc;
1625 declarator->parameter_pack_p = element->parameter_pack_p;
1626 element->parameter_pack_p = false;
1628 else
1629 declarator->parameter_pack_p = false;
1631 return declarator;
1634 /* Determine whether the declarator we've seen so far can be a
1635 parameter pack, when followed by an ellipsis. */
1636 static bool
1637 declarator_can_be_parameter_pack (cp_declarator *declarator)
1639 if (declarator && declarator->parameter_pack_p)
1640 /* We already saw an ellipsis. */
1641 return false;
1643 /* Search for a declarator name, or any other declarator that goes
1644 after the point where the ellipsis could appear in a parameter
1645 pack. If we find any of these, then this declarator can not be
1646 made into a parameter pack. */
1647 bool found = false;
1648 while (declarator && !found)
1650 switch ((int)declarator->kind)
1652 case cdk_id:
1653 case cdk_array:
1654 case cdk_decomp:
1655 found = true;
1656 break;
1658 case cdk_error:
1659 return true;
1661 default:
1662 declarator = declarator->declarator;
1663 break;
1667 return !found;
1670 cp_parameter_declarator *no_parameters;
1672 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1673 DECLARATOR and DEFAULT_ARGUMENT. */
1675 cp_parameter_declarator *
1676 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1677 cp_declarator *declarator,
1678 tree default_argument,
1679 location_t loc,
1680 bool template_parameter_pack_p = false)
1682 cp_parameter_declarator *parameter;
1684 parameter = ((cp_parameter_declarator *)
1685 alloc_declarator (sizeof (cp_parameter_declarator)));
1686 parameter->next = NULL;
1687 if (decl_specifiers)
1688 parameter->decl_specifiers = *decl_specifiers;
1689 else
1690 clear_decl_specs (&parameter->decl_specifiers);
1691 parameter->declarator = declarator;
1692 parameter->default_argument = default_argument;
1693 parameter->template_parameter_pack_p = template_parameter_pack_p;
1694 parameter->loc = loc;
1696 return parameter;
1699 /* Returns true iff DECLARATOR is a declaration for a function. */
1701 static bool
1702 function_declarator_p (const cp_declarator *declarator)
1704 while (declarator)
1706 if (declarator->kind == cdk_function
1707 && declarator->declarator->kind == cdk_id)
1708 return true;
1709 if (declarator->kind == cdk_id
1710 || declarator->kind == cdk_decomp
1711 || declarator->kind == cdk_error)
1712 return false;
1713 declarator = declarator->declarator;
1715 return false;
1718 /* The parser. */
1720 /* Overview
1721 --------
1723 A cp_parser parses the token stream as specified by the C++
1724 grammar. Its job is purely parsing, not semantic analysis. For
1725 example, the parser breaks the token stream into declarators,
1726 expressions, statements, and other similar syntactic constructs.
1727 It does not check that the types of the expressions on either side
1728 of an assignment-statement are compatible, or that a function is
1729 not declared with a parameter of type `void'.
1731 The parser invokes routines elsewhere in the compiler to perform
1732 semantic analysis and to build up the abstract syntax tree for the
1733 code processed.
1735 The parser (and the template instantiation code, which is, in a
1736 way, a close relative of parsing) are the only parts of the
1737 compiler that should be calling push_scope and pop_scope, or
1738 related functions. The parser (and template instantiation code)
1739 keeps track of what scope is presently active; everything else
1740 should simply honor that. (The code that generates static
1741 initializers may also need to set the scope, in order to check
1742 access control correctly when emitting the initializers.)
1744 Methodology
1745 -----------
1747 The parser is of the standard recursive-descent variety. Upcoming
1748 tokens in the token stream are examined in order to determine which
1749 production to use when parsing a non-terminal. Some C++ constructs
1750 require arbitrary look ahead to disambiguate. For example, it is
1751 impossible, in the general case, to tell whether a statement is an
1752 expression or declaration without scanning the entire statement.
1753 Therefore, the parser is capable of "parsing tentatively." When the
1754 parser is not sure what construct comes next, it enters this mode.
1755 Then, while we attempt to parse the construct, the parser queues up
1756 error messages, rather than issuing them immediately, and saves the
1757 tokens it consumes. If the construct is parsed successfully, the
1758 parser "commits", i.e., it issues any queued error messages and
1759 the tokens that were being preserved are permanently discarded.
1760 If, however, the construct is not parsed successfully, the parser
1761 rolls back its state completely so that it can resume parsing using
1762 a different alternative.
1764 Future Improvements
1765 -------------------
1767 The performance of the parser could probably be improved substantially.
1768 We could often eliminate the need to parse tentatively by looking ahead
1769 a little bit. In some places, this approach might not entirely eliminate
1770 the need to parse tentatively, but it might still speed up the average
1771 case. */
1773 /* Flags that are passed to some parsing functions. These values can
1774 be bitwise-ored together. */
1776 enum
1778 /* No flags. */
1779 CP_PARSER_FLAGS_NONE = 0x0,
1780 /* The construct is optional. If it is not present, then no error
1781 should be issued. */
1782 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1783 /* When parsing a type-specifier, treat user-defined type-names
1784 as non-type identifiers. */
1785 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1786 /* When parsing a type-specifier, do not try to parse a class-specifier
1787 or enum-specifier. */
1788 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1789 /* When parsing a decl-specifier-seq, only allow type-specifier or
1790 constexpr. */
1791 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1792 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1793 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1796 /* This type is used for parameters and variables which hold
1797 combinations of the above flags. */
1798 typedef int cp_parser_flags;
1800 /* The different kinds of declarators we want to parse. */
1802 enum cp_parser_declarator_kind
1804 /* We want an abstract declarator. */
1805 CP_PARSER_DECLARATOR_ABSTRACT,
1806 /* We want a named declarator. */
1807 CP_PARSER_DECLARATOR_NAMED,
1808 /* We don't mind, but the name must be an unqualified-id. */
1809 CP_PARSER_DECLARATOR_EITHER
1812 /* The precedence values used to parse binary expressions. The minimum value
1813 of PREC must be 1, because zero is reserved to quickly discriminate
1814 binary operators from other tokens. */
1816 enum cp_parser_prec
1818 PREC_NOT_OPERATOR,
1819 PREC_LOGICAL_OR_EXPRESSION,
1820 PREC_LOGICAL_AND_EXPRESSION,
1821 PREC_INCLUSIVE_OR_EXPRESSION,
1822 PREC_EXCLUSIVE_OR_EXPRESSION,
1823 PREC_AND_EXPRESSION,
1824 PREC_EQUALITY_EXPRESSION,
1825 PREC_RELATIONAL_EXPRESSION,
1826 PREC_SHIFT_EXPRESSION,
1827 PREC_ADDITIVE_EXPRESSION,
1828 PREC_MULTIPLICATIVE_EXPRESSION,
1829 PREC_PM_EXPRESSION,
1830 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1833 /* A mapping from a token type to a corresponding tree node type, with a
1834 precedence value. */
1836 struct cp_parser_binary_operations_map_node
1838 /* The token type. */
1839 enum cpp_ttype token_type;
1840 /* The corresponding tree code. */
1841 enum tree_code tree_type;
1842 /* The precedence of this operator. */
1843 enum cp_parser_prec prec;
1846 struct cp_parser_expression_stack_entry
1848 /* Left hand side of the binary operation we are currently
1849 parsing. */
1850 cp_expr lhs;
1851 /* Original tree code for left hand side, if it was a binary
1852 expression itself (used for -Wparentheses). */
1853 enum tree_code lhs_type;
1854 /* Tree code for the binary operation we are parsing. */
1855 enum tree_code tree_type;
1856 /* Precedence of the binary operation we are parsing. */
1857 enum cp_parser_prec prec;
1858 /* Location of the binary operation we are parsing. */
1859 location_t loc;
1862 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1863 entries because precedence levels on the stack are monotonically
1864 increasing. */
1865 typedef struct cp_parser_expression_stack_entry
1866 cp_parser_expression_stack[NUM_PREC_VALUES];
1868 /* Prototypes. */
1870 /* Constructors and destructors. */
1872 static cp_parser_context *cp_parser_context_new
1873 (cp_parser_context *);
1875 /* Class variables. */
1877 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1879 /* The operator-precedence table used by cp_parser_binary_expression.
1880 Transformed into an associative array (binops_by_token) by
1881 cp_parser_new. */
1883 static const cp_parser_binary_operations_map_node binops[] = {
1884 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1885 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1887 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1888 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1889 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1891 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1892 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1894 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1895 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1897 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1898 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1899 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1900 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1902 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1903 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1905 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1907 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1909 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1911 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1913 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1916 /* The same as binops, but initialized by cp_parser_new so that
1917 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1918 for speed. */
1919 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1921 /* Constructors and destructors. */
1923 /* Construct a new context. The context below this one on the stack
1924 is given by NEXT. */
1926 static cp_parser_context *
1927 cp_parser_context_new (cp_parser_context* next)
1929 cp_parser_context *context;
1931 /* Allocate the storage. */
1932 if (cp_parser_context_free_list != NULL)
1934 /* Pull the first entry from the free list. */
1935 context = cp_parser_context_free_list;
1936 cp_parser_context_free_list = context->next;
1937 memset (context, 0, sizeof (*context));
1939 else
1940 context = ggc_cleared_alloc<cp_parser_context> ();
1942 /* No errors have occurred yet in this context. */
1943 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1944 /* If this is not the bottommost context, copy information that we
1945 need from the previous context. */
1946 if (next)
1948 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1949 expression, then we are parsing one in this context, too. */
1950 context->object_type = next->object_type;
1951 /* Thread the stack. */
1952 context->next = next;
1955 return context;
1958 /* Managing the unparsed function queues. */
1960 #define unparsed_funs_with_default_args \
1961 parser->unparsed_queues->last ().funs_with_default_args
1962 #define unparsed_funs_with_definitions \
1963 parser->unparsed_queues->last ().funs_with_definitions
1964 #define unparsed_nsdmis \
1965 parser->unparsed_queues->last ().nsdmis
1966 #define unparsed_classes \
1967 parser->unparsed_queues->last ().classes
1969 static void
1970 push_unparsed_function_queues (cp_parser *parser)
1972 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1973 vec_safe_push (parser->unparsed_queues, e);
1976 static void
1977 pop_unparsed_function_queues (cp_parser *parser)
1979 release_tree_vector (unparsed_funs_with_definitions);
1980 parser->unparsed_queues->pop ();
1983 /* Prototypes. */
1985 /* Constructors and destructors. */
1987 static cp_parser *cp_parser_new
1988 (void);
1990 /* Routines to parse various constructs.
1992 Those that return `tree' will return the error_mark_node (rather
1993 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1994 Sometimes, they will return an ordinary node if error-recovery was
1995 attempted, even though a parse error occurred. So, to check
1996 whether or not a parse error occurred, you should always use
1997 cp_parser_error_occurred. If the construct is optional (indicated
1998 either by an `_opt' in the name of the function that does the
1999 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2000 the construct is not present. */
2002 /* Lexical conventions [gram.lex] */
2004 static cp_expr cp_parser_identifier
2005 (cp_parser *);
2006 static cp_expr cp_parser_string_literal
2007 (cp_parser *, bool, bool, bool);
2008 static cp_expr cp_parser_userdef_char_literal
2009 (cp_parser *);
2010 static tree cp_parser_userdef_string_literal
2011 (tree);
2012 static cp_expr cp_parser_userdef_numeric_literal
2013 (cp_parser *);
2015 /* Basic concepts [gram.basic] */
2017 static void cp_parser_translation_unit (cp_parser *);
2019 /* Expressions [gram.expr] */
2021 static cp_expr cp_parser_primary_expression
2022 (cp_parser *, bool, bool, bool, cp_id_kind *);
2023 static cp_expr cp_parser_id_expression
2024 (cp_parser *, bool, bool, bool *, bool, bool);
2025 static cp_expr cp_parser_unqualified_id
2026 (cp_parser *, bool, bool, bool, bool);
2027 static tree cp_parser_nested_name_specifier_opt
2028 (cp_parser *, bool, bool, bool, bool, bool = false);
2029 static tree cp_parser_nested_name_specifier
2030 (cp_parser *, bool, bool, bool, bool);
2031 static tree cp_parser_qualifying_entity
2032 (cp_parser *, bool, bool, bool, bool, bool);
2033 static cp_expr cp_parser_postfix_expression
2034 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2035 static tree cp_parser_postfix_open_square_expression
2036 (cp_parser *, tree, bool, bool);
2037 static tree cp_parser_postfix_dot_deref_expression
2038 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2039 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2040 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2041 bool = false);
2042 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2043 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2044 static void cp_parser_pseudo_destructor_name
2045 (cp_parser *, tree, tree *, tree *);
2046 static cp_expr cp_parser_unary_expression
2047 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2048 static enum tree_code cp_parser_unary_operator
2049 (cp_token *);
2050 static tree cp_parser_new_expression
2051 (cp_parser *);
2052 static vec<tree, va_gc> *cp_parser_new_placement
2053 (cp_parser *);
2054 static tree cp_parser_new_type_id
2055 (cp_parser *, tree *);
2056 static cp_declarator *cp_parser_new_declarator_opt
2057 (cp_parser *);
2058 static cp_declarator *cp_parser_direct_new_declarator
2059 (cp_parser *);
2060 static vec<tree, va_gc> *cp_parser_new_initializer
2061 (cp_parser *);
2062 static tree cp_parser_delete_expression
2063 (cp_parser *);
2064 static cp_expr cp_parser_cast_expression
2065 (cp_parser *, bool, bool, bool, cp_id_kind *);
2066 static cp_expr cp_parser_binary_expression
2067 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2068 static tree cp_parser_question_colon_clause
2069 (cp_parser *, cp_expr);
2070 static cp_expr cp_parser_assignment_expression
2071 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2072 static enum tree_code cp_parser_assignment_operator_opt
2073 (cp_parser *);
2074 static cp_expr cp_parser_expression
2075 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2076 static cp_expr cp_parser_constant_expression
2077 (cp_parser *, bool = false, bool * = NULL, bool = false);
2078 static cp_expr cp_parser_builtin_offsetof
2079 (cp_parser *);
2080 static cp_expr cp_parser_lambda_expression
2081 (cp_parser *);
2082 static void cp_parser_lambda_introducer
2083 (cp_parser *, tree);
2084 static bool cp_parser_lambda_declarator_opt
2085 (cp_parser *, tree);
2086 static void cp_parser_lambda_body
2087 (cp_parser *, tree);
2089 /* Statements [gram.stmt.stmt] */
2091 static void cp_parser_statement
2092 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2093 static void cp_parser_label_for_labeled_statement
2094 (cp_parser *, tree);
2095 static tree cp_parser_expression_statement
2096 (cp_parser *, tree);
2097 static tree cp_parser_compound_statement
2098 (cp_parser *, tree, int, bool);
2099 static void cp_parser_statement_seq_opt
2100 (cp_parser *, tree);
2101 static tree cp_parser_selection_statement
2102 (cp_parser *, bool *, vec<tree> *);
2103 static tree cp_parser_condition
2104 (cp_parser *);
2105 static tree cp_parser_iteration_statement
2106 (cp_parser *, bool *, bool, unsigned short);
2107 static bool cp_parser_init_statement
2108 (cp_parser *, tree *decl);
2109 static tree cp_parser_for
2110 (cp_parser *, bool, unsigned short);
2111 static tree cp_parser_c_for
2112 (cp_parser *, tree, tree, bool, unsigned short);
2113 static tree cp_parser_range_for
2114 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2115 static void do_range_for_auto_deduction
2116 (tree, tree);
2117 static tree cp_parser_perform_range_for_lookup
2118 (tree, tree *, tree *);
2119 static tree cp_parser_range_for_member_function
2120 (tree, tree);
2121 static tree cp_parser_jump_statement
2122 (cp_parser *);
2123 static void cp_parser_declaration_statement
2124 (cp_parser *);
2126 static tree cp_parser_implicitly_scoped_statement
2127 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2128 static void cp_parser_already_scoped_statement
2129 (cp_parser *, bool *, const token_indent_info &);
2131 /* Declarations [gram.dcl.dcl] */
2133 static void cp_parser_declaration_seq_opt
2134 (cp_parser *);
2135 static void cp_parser_declaration
2136 (cp_parser *);
2137 static void cp_parser_toplevel_declaration
2138 (cp_parser *);
2139 static void cp_parser_block_declaration
2140 (cp_parser *, bool);
2141 static void cp_parser_simple_declaration
2142 (cp_parser *, bool, tree *);
2143 static void cp_parser_decl_specifier_seq
2144 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2145 static tree cp_parser_storage_class_specifier_opt
2146 (cp_parser *);
2147 static tree cp_parser_function_specifier_opt
2148 (cp_parser *, cp_decl_specifier_seq *);
2149 static tree cp_parser_type_specifier
2150 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2151 int *, bool *);
2152 static tree cp_parser_simple_type_specifier
2153 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2154 static tree cp_parser_type_name
2155 (cp_parser *, bool);
2156 static tree cp_parser_type_name
2157 (cp_parser *);
2158 static tree cp_parser_nonclass_name
2159 (cp_parser* parser);
2160 static tree cp_parser_elaborated_type_specifier
2161 (cp_parser *, bool, bool);
2162 static tree cp_parser_enum_specifier
2163 (cp_parser *);
2164 static void cp_parser_enumerator_list
2165 (cp_parser *, tree);
2166 static void cp_parser_enumerator_definition
2167 (cp_parser *, tree);
2168 static tree cp_parser_namespace_name
2169 (cp_parser *);
2170 static void cp_parser_namespace_definition
2171 (cp_parser *);
2172 static void cp_parser_namespace_body
2173 (cp_parser *);
2174 static tree cp_parser_qualified_namespace_specifier
2175 (cp_parser *);
2176 static void cp_parser_namespace_alias_definition
2177 (cp_parser *);
2178 static bool cp_parser_using_declaration
2179 (cp_parser *, bool);
2180 static void cp_parser_using_directive
2181 (cp_parser *);
2182 static tree cp_parser_alias_declaration
2183 (cp_parser *);
2184 static void cp_parser_asm_definition
2185 (cp_parser *);
2186 static void cp_parser_linkage_specification
2187 (cp_parser *);
2188 static void cp_parser_static_assert
2189 (cp_parser *, bool);
2190 static tree cp_parser_decltype
2191 (cp_parser *);
2192 static tree cp_parser_decomposition_declaration
2193 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2195 /* Declarators [gram.dcl.decl] */
2197 static tree cp_parser_init_declarator
2198 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2199 bool, bool, int, bool *, tree *, location_t *, tree *);
2200 static cp_declarator *cp_parser_declarator
2201 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2202 static cp_declarator *cp_parser_direct_declarator
2203 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2204 static enum tree_code cp_parser_ptr_operator
2205 (cp_parser *, tree *, cp_cv_quals *, tree *);
2206 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2207 (cp_parser *);
2208 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2209 (cp_parser *);
2210 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2211 (cp_parser *);
2212 static tree cp_parser_tx_qualifier_opt
2213 (cp_parser *);
2214 static tree cp_parser_late_return_type_opt
2215 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2216 static tree cp_parser_declarator_id
2217 (cp_parser *, bool);
2218 static tree cp_parser_type_id
2219 (cp_parser *, location_t * = NULL);
2220 static tree cp_parser_template_type_arg
2221 (cp_parser *);
2222 static tree cp_parser_trailing_type_id (cp_parser *);
2223 static tree cp_parser_type_id_1
2224 (cp_parser *, bool, bool, location_t *);
2225 static void cp_parser_type_specifier_seq
2226 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2227 static tree cp_parser_parameter_declaration_clause
2228 (cp_parser *);
2229 static tree cp_parser_parameter_declaration_list
2230 (cp_parser *);
2231 static cp_parameter_declarator *cp_parser_parameter_declaration
2232 (cp_parser *, bool, bool *);
2233 static tree cp_parser_default_argument
2234 (cp_parser *, bool);
2235 static void cp_parser_function_body
2236 (cp_parser *, bool);
2237 static tree cp_parser_initializer
2238 (cp_parser *, bool *, bool *, bool = false);
2239 static cp_expr cp_parser_initializer_clause
2240 (cp_parser *, bool *);
2241 static cp_expr cp_parser_braced_list
2242 (cp_parser*, bool*);
2243 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2244 (cp_parser *, bool *);
2246 static void cp_parser_ctor_initializer_opt_and_function_body
2247 (cp_parser *, bool);
2249 static tree cp_parser_late_parsing_omp_declare_simd
2250 (cp_parser *, tree);
2252 static tree cp_parser_late_parsing_oacc_routine
2253 (cp_parser *, tree);
2255 static tree synthesize_implicit_template_parm
2256 (cp_parser *, tree);
2257 static tree finish_fully_implicit_template
2258 (cp_parser *, tree);
2259 static void abort_fully_implicit_template
2260 (cp_parser *);
2262 /* Classes [gram.class] */
2264 static tree cp_parser_class_name
2265 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2266 static tree cp_parser_class_specifier
2267 (cp_parser *);
2268 static tree cp_parser_class_head
2269 (cp_parser *, bool *);
2270 static enum tag_types cp_parser_class_key
2271 (cp_parser *);
2272 static void cp_parser_type_parameter_key
2273 (cp_parser* parser);
2274 static void cp_parser_member_specification_opt
2275 (cp_parser *);
2276 static void cp_parser_member_declaration
2277 (cp_parser *);
2278 static tree cp_parser_pure_specifier
2279 (cp_parser *);
2280 static tree cp_parser_constant_initializer
2281 (cp_parser *);
2283 /* Derived classes [gram.class.derived] */
2285 static tree cp_parser_base_clause
2286 (cp_parser *);
2287 static tree cp_parser_base_specifier
2288 (cp_parser *);
2290 /* Special member functions [gram.special] */
2292 static tree cp_parser_conversion_function_id
2293 (cp_parser *);
2294 static tree cp_parser_conversion_type_id
2295 (cp_parser *);
2296 static cp_declarator *cp_parser_conversion_declarator_opt
2297 (cp_parser *);
2298 static void cp_parser_ctor_initializer_opt
2299 (cp_parser *);
2300 static void cp_parser_mem_initializer_list
2301 (cp_parser *);
2302 static tree cp_parser_mem_initializer
2303 (cp_parser *);
2304 static tree cp_parser_mem_initializer_id
2305 (cp_parser *);
2307 /* Overloading [gram.over] */
2309 static cp_expr cp_parser_operator_function_id
2310 (cp_parser *);
2311 static cp_expr cp_parser_operator
2312 (cp_parser *);
2314 /* Templates [gram.temp] */
2316 static void cp_parser_template_declaration
2317 (cp_parser *, bool);
2318 static tree cp_parser_template_parameter_list
2319 (cp_parser *);
2320 static tree cp_parser_template_parameter
2321 (cp_parser *, bool *, bool *);
2322 static tree cp_parser_type_parameter
2323 (cp_parser *, bool *);
2324 static tree cp_parser_template_id
2325 (cp_parser *, bool, bool, enum tag_types, bool);
2326 static tree cp_parser_template_name
2327 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2328 static tree cp_parser_template_argument_list
2329 (cp_parser *);
2330 static tree cp_parser_template_argument
2331 (cp_parser *);
2332 static void cp_parser_explicit_instantiation
2333 (cp_parser *);
2334 static void cp_parser_explicit_specialization
2335 (cp_parser *);
2337 /* Exception handling [gram.exception] */
2339 static tree cp_parser_try_block
2340 (cp_parser *);
2341 static void cp_parser_function_try_block
2342 (cp_parser *);
2343 static void cp_parser_handler_seq
2344 (cp_parser *);
2345 static void cp_parser_handler
2346 (cp_parser *);
2347 static tree cp_parser_exception_declaration
2348 (cp_parser *);
2349 static tree cp_parser_throw_expression
2350 (cp_parser *);
2351 static tree cp_parser_exception_specification_opt
2352 (cp_parser *);
2353 static tree cp_parser_type_id_list
2354 (cp_parser *);
2356 /* GNU Extensions */
2358 static tree cp_parser_asm_specification_opt
2359 (cp_parser *);
2360 static tree cp_parser_asm_operand_list
2361 (cp_parser *);
2362 static tree cp_parser_asm_clobber_list
2363 (cp_parser *);
2364 static tree cp_parser_asm_label_list
2365 (cp_parser *);
2366 static bool cp_next_tokens_can_be_attribute_p
2367 (cp_parser *);
2368 static bool cp_next_tokens_can_be_gnu_attribute_p
2369 (cp_parser *);
2370 static bool cp_next_tokens_can_be_std_attribute_p
2371 (cp_parser *);
2372 static bool cp_nth_tokens_can_be_std_attribute_p
2373 (cp_parser *, size_t);
2374 static bool cp_nth_tokens_can_be_gnu_attribute_p
2375 (cp_parser *, size_t);
2376 static bool cp_nth_tokens_can_be_attribute_p
2377 (cp_parser *, size_t);
2378 static tree cp_parser_attributes_opt
2379 (cp_parser *);
2380 static tree cp_parser_gnu_attributes_opt
2381 (cp_parser *);
2382 static tree cp_parser_gnu_attribute_list
2383 (cp_parser *);
2384 static tree cp_parser_std_attribute
2385 (cp_parser *, tree);
2386 static tree cp_parser_std_attribute_spec
2387 (cp_parser *);
2388 static tree cp_parser_std_attribute_spec_seq
2389 (cp_parser *);
2390 static size_t cp_parser_skip_attributes_opt
2391 (cp_parser *, size_t);
2392 static bool cp_parser_extension_opt
2393 (cp_parser *, int *);
2394 static void cp_parser_label_declaration
2395 (cp_parser *);
2397 /* Concept Extensions */
2399 static tree cp_parser_requires_clause
2400 (cp_parser *);
2401 static tree cp_parser_requires_clause_opt
2402 (cp_parser *);
2403 static tree cp_parser_requires_expression
2404 (cp_parser *);
2405 static tree cp_parser_requirement_parameter_list
2406 (cp_parser *);
2407 static tree cp_parser_requirement_body
2408 (cp_parser *);
2409 static tree cp_parser_requirement_list
2410 (cp_parser *);
2411 static tree cp_parser_requirement
2412 (cp_parser *);
2413 static tree cp_parser_simple_requirement
2414 (cp_parser *);
2415 static tree cp_parser_compound_requirement
2416 (cp_parser *);
2417 static tree cp_parser_type_requirement
2418 (cp_parser *);
2419 static tree cp_parser_nested_requirement
2420 (cp_parser *);
2422 /* Transactional Memory Extensions */
2424 static tree cp_parser_transaction
2425 (cp_parser *, cp_token *);
2426 static tree cp_parser_transaction_expression
2427 (cp_parser *, enum rid);
2428 static void cp_parser_function_transaction
2429 (cp_parser *, enum rid);
2430 static tree cp_parser_transaction_cancel
2431 (cp_parser *);
2433 enum pragma_context {
2434 pragma_external,
2435 pragma_member,
2436 pragma_objc_icode,
2437 pragma_stmt,
2438 pragma_compound
2440 static bool cp_parser_pragma
2441 (cp_parser *, enum pragma_context, bool *);
2443 /* Objective-C++ Productions */
2445 static tree cp_parser_objc_message_receiver
2446 (cp_parser *);
2447 static tree cp_parser_objc_message_args
2448 (cp_parser *);
2449 static tree cp_parser_objc_message_expression
2450 (cp_parser *);
2451 static cp_expr cp_parser_objc_encode_expression
2452 (cp_parser *);
2453 static tree cp_parser_objc_defs_expression
2454 (cp_parser *);
2455 static tree cp_parser_objc_protocol_expression
2456 (cp_parser *);
2457 static tree cp_parser_objc_selector_expression
2458 (cp_parser *);
2459 static cp_expr cp_parser_objc_expression
2460 (cp_parser *);
2461 static bool cp_parser_objc_selector_p
2462 (enum cpp_ttype);
2463 static tree cp_parser_objc_selector
2464 (cp_parser *);
2465 static tree cp_parser_objc_protocol_refs_opt
2466 (cp_parser *);
2467 static void cp_parser_objc_declaration
2468 (cp_parser *, tree);
2469 static tree cp_parser_objc_statement
2470 (cp_parser *);
2471 static bool cp_parser_objc_valid_prefix_attributes
2472 (cp_parser *, tree *);
2473 static void cp_parser_objc_at_property_declaration
2474 (cp_parser *) ;
2475 static void cp_parser_objc_at_synthesize_declaration
2476 (cp_parser *) ;
2477 static void cp_parser_objc_at_dynamic_declaration
2478 (cp_parser *) ;
2479 static tree cp_parser_objc_struct_declaration
2480 (cp_parser *) ;
2482 /* Utility Routines */
2484 static cp_expr cp_parser_lookup_name
2485 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2486 static tree cp_parser_lookup_name_simple
2487 (cp_parser *, tree, location_t);
2488 static tree cp_parser_maybe_treat_template_as_class
2489 (tree, bool);
2490 static bool cp_parser_check_declarator_template_parameters
2491 (cp_parser *, cp_declarator *, location_t);
2492 static bool cp_parser_check_template_parameters
2493 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2494 static cp_expr cp_parser_simple_cast_expression
2495 (cp_parser *);
2496 static tree cp_parser_global_scope_opt
2497 (cp_parser *, bool);
2498 static bool cp_parser_constructor_declarator_p
2499 (cp_parser *, bool);
2500 static tree cp_parser_function_definition_from_specifiers_and_declarator
2501 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2502 static tree cp_parser_function_definition_after_declarator
2503 (cp_parser *, bool);
2504 static bool cp_parser_template_declaration_after_export
2505 (cp_parser *, bool);
2506 static void cp_parser_perform_template_parameter_access_checks
2507 (vec<deferred_access_check, va_gc> *);
2508 static tree cp_parser_single_declaration
2509 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2510 static cp_expr cp_parser_functional_cast
2511 (cp_parser *, tree);
2512 static tree cp_parser_save_member_function_body
2513 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2514 static tree cp_parser_save_nsdmi
2515 (cp_parser *);
2516 static tree cp_parser_enclosed_template_argument_list
2517 (cp_parser *);
2518 static void cp_parser_save_default_args
2519 (cp_parser *, tree);
2520 static void cp_parser_late_parsing_for_member
2521 (cp_parser *, tree);
2522 static tree cp_parser_late_parse_one_default_arg
2523 (cp_parser *, tree, tree, tree);
2524 static void cp_parser_late_parsing_nsdmi
2525 (cp_parser *, tree);
2526 static void cp_parser_late_parsing_default_args
2527 (cp_parser *, tree);
2528 static tree cp_parser_sizeof_operand
2529 (cp_parser *, enum rid);
2530 static cp_expr cp_parser_trait_expr
2531 (cp_parser *, enum rid);
2532 static bool cp_parser_declares_only_class_p
2533 (cp_parser *);
2534 static void cp_parser_set_storage_class
2535 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2536 static void cp_parser_set_decl_spec_type
2537 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2538 static void set_and_check_decl_spec_loc
2539 (cp_decl_specifier_seq *decl_specs,
2540 cp_decl_spec ds, cp_token *);
2541 static bool cp_parser_friend_p
2542 (const cp_decl_specifier_seq *);
2543 static void cp_parser_required_error
2544 (cp_parser *, required_token, bool, location_t);
2545 static cp_token *cp_parser_require
2546 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2547 static cp_token *cp_parser_require_keyword
2548 (cp_parser *, enum rid, required_token);
2549 static bool cp_parser_token_starts_function_definition_p
2550 (cp_token *);
2551 static bool cp_parser_next_token_starts_class_definition_p
2552 (cp_parser *);
2553 static bool cp_parser_next_token_ends_template_argument_p
2554 (cp_parser *);
2555 static bool cp_parser_nth_token_starts_template_argument_list_p
2556 (cp_parser *, size_t);
2557 static enum tag_types cp_parser_token_is_class_key
2558 (cp_token *);
2559 static enum tag_types cp_parser_token_is_type_parameter_key
2560 (cp_token *);
2561 static void cp_parser_check_class_key
2562 (enum tag_types, tree type);
2563 static void cp_parser_check_access_in_redeclaration
2564 (tree type, location_t location);
2565 static bool cp_parser_optional_template_keyword
2566 (cp_parser *);
2567 static void cp_parser_pre_parsed_nested_name_specifier
2568 (cp_parser *);
2569 static bool cp_parser_cache_group
2570 (cp_parser *, enum cpp_ttype, unsigned);
2571 static tree cp_parser_cache_defarg
2572 (cp_parser *parser, bool nsdmi);
2573 static void cp_parser_parse_tentatively
2574 (cp_parser *);
2575 static void cp_parser_commit_to_tentative_parse
2576 (cp_parser *);
2577 static void cp_parser_commit_to_topmost_tentative_parse
2578 (cp_parser *);
2579 static void cp_parser_abort_tentative_parse
2580 (cp_parser *);
2581 static bool cp_parser_parse_definitely
2582 (cp_parser *);
2583 static inline bool cp_parser_parsing_tentatively
2584 (cp_parser *);
2585 static bool cp_parser_uncommitted_to_tentative_parse_p
2586 (cp_parser *);
2587 static void cp_parser_error
2588 (cp_parser *, const char *);
2589 static void cp_parser_name_lookup_error
2590 (cp_parser *, tree, tree, name_lookup_error, location_t);
2591 static bool cp_parser_simulate_error
2592 (cp_parser *);
2593 static bool cp_parser_check_type_definition
2594 (cp_parser *);
2595 static void cp_parser_check_for_definition_in_return_type
2596 (cp_declarator *, tree, location_t type_location);
2597 static void cp_parser_check_for_invalid_template_id
2598 (cp_parser *, tree, enum tag_types, location_t location);
2599 static bool cp_parser_non_integral_constant_expression
2600 (cp_parser *, non_integral_constant);
2601 static void cp_parser_diagnose_invalid_type_name
2602 (cp_parser *, tree, location_t);
2603 static bool cp_parser_parse_and_diagnose_invalid_type_name
2604 (cp_parser *);
2605 static int cp_parser_skip_to_closing_parenthesis
2606 (cp_parser *, bool, bool, bool);
2607 static void cp_parser_skip_to_end_of_statement
2608 (cp_parser *);
2609 static void cp_parser_consume_semicolon_at_end_of_statement
2610 (cp_parser *);
2611 static void cp_parser_skip_to_end_of_block_or_statement
2612 (cp_parser *);
2613 static bool cp_parser_skip_to_closing_brace
2614 (cp_parser *);
2615 static void cp_parser_skip_to_end_of_template_parameter_list
2616 (cp_parser *);
2617 static void cp_parser_skip_to_pragma_eol
2618 (cp_parser*, cp_token *);
2619 static bool cp_parser_error_occurred
2620 (cp_parser *);
2621 static bool cp_parser_allow_gnu_extensions_p
2622 (cp_parser *);
2623 static bool cp_parser_is_pure_string_literal
2624 (cp_token *);
2625 static bool cp_parser_is_string_literal
2626 (cp_token *);
2627 static bool cp_parser_is_keyword
2628 (cp_token *, enum rid);
2629 static tree cp_parser_make_typename_type
2630 (cp_parser *, tree, location_t location);
2631 static cp_declarator * cp_parser_make_indirect_declarator
2632 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2633 static bool cp_parser_compound_literal_p
2634 (cp_parser *);
2635 static bool cp_parser_array_designator_p
2636 (cp_parser *);
2637 static bool cp_parser_init_statement_p
2638 (cp_parser *);
2639 static bool cp_parser_skip_to_closing_square_bracket
2640 (cp_parser *);
2642 /* Concept-related syntactic transformations */
2644 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2645 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2647 // -------------------------------------------------------------------------- //
2648 // Unevaluated Operand Guard
2650 // Implementation of an RAII helper for unevaluated operand parsing.
2651 cp_unevaluated::cp_unevaluated ()
2653 ++cp_unevaluated_operand;
2654 ++c_inhibit_evaluation_warnings;
2657 cp_unevaluated::~cp_unevaluated ()
2659 --c_inhibit_evaluation_warnings;
2660 --cp_unevaluated_operand;
2663 // -------------------------------------------------------------------------- //
2664 // Tentative Parsing
2666 /* Returns nonzero if we are parsing tentatively. */
2668 static inline bool
2669 cp_parser_parsing_tentatively (cp_parser* parser)
2671 return parser->context->next != NULL;
2674 /* Returns nonzero if TOKEN is a string literal. */
2676 static bool
2677 cp_parser_is_pure_string_literal (cp_token* token)
2679 return (token->type == CPP_STRING ||
2680 token->type == CPP_STRING16 ||
2681 token->type == CPP_STRING32 ||
2682 token->type == CPP_WSTRING ||
2683 token->type == CPP_UTF8STRING);
2686 /* Returns nonzero if TOKEN is a string literal
2687 of a user-defined string literal. */
2689 static bool
2690 cp_parser_is_string_literal (cp_token* token)
2692 return (cp_parser_is_pure_string_literal (token) ||
2693 token->type == CPP_STRING_USERDEF ||
2694 token->type == CPP_STRING16_USERDEF ||
2695 token->type == CPP_STRING32_USERDEF ||
2696 token->type == CPP_WSTRING_USERDEF ||
2697 token->type == CPP_UTF8STRING_USERDEF);
2700 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2702 static bool
2703 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2705 return token->keyword == keyword;
2708 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2709 PRAGMA_NONE. */
2711 static enum pragma_kind
2712 cp_parser_pragma_kind (cp_token *token)
2714 if (token->type != CPP_PRAGMA)
2715 return PRAGMA_NONE;
2716 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2717 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2720 /* Helper function for cp_parser_error.
2721 Having peeked a token of kind TOK1_KIND that might signify
2722 a conflict marker, peek successor tokens to determine
2723 if we actually do have a conflict marker.
2724 Specifically, we consider a run of 7 '<', '=' or '>' characters
2725 at the start of a line as a conflict marker.
2726 These come through the lexer as three pairs and a single,
2727 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2728 If it returns true, *OUT_LOC is written to with the location/range
2729 of the marker. */
2731 static bool
2732 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2733 location_t *out_loc)
2735 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2736 if (token2->type != tok1_kind)
2737 return false;
2738 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2739 if (token3->type != tok1_kind)
2740 return false;
2741 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2742 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2743 return false;
2745 /* It must be at the start of the line. */
2746 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2747 if (LOCATION_COLUMN (start_loc) != 1)
2748 return false;
2750 /* We have a conflict marker. Construct a location of the form:
2751 <<<<<<<
2752 ^~~~~~~
2753 with start == caret, finishing at the end of the marker. */
2754 location_t finish_loc = get_finish (token4->location);
2755 *out_loc = make_location (start_loc, start_loc, finish_loc);
2757 return true;
2760 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2761 RT_CLOSE_PAREN. */
2763 static const char *
2764 get_matching_symbol (required_token token_desc)
2766 switch (token_desc)
2768 default:
2769 gcc_unreachable ();
2770 return "";
2771 case RT_CLOSE_BRACE:
2772 return "{";
2773 case RT_CLOSE_PAREN:
2774 return "(";
2778 /* Attempt to convert TOKEN_DESC from a required_token to an
2779 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2781 static enum cpp_ttype
2782 get_required_cpp_ttype (required_token token_desc)
2784 switch (token_desc)
2786 case RT_SEMICOLON:
2787 return CPP_SEMICOLON;
2788 case RT_OPEN_PAREN:
2789 return CPP_OPEN_PAREN;
2790 case RT_CLOSE_BRACE:
2791 return CPP_CLOSE_BRACE;
2792 case RT_OPEN_BRACE:
2793 return CPP_OPEN_BRACE;
2794 case RT_CLOSE_SQUARE:
2795 return CPP_CLOSE_SQUARE;
2796 case RT_OPEN_SQUARE:
2797 return CPP_OPEN_SQUARE;
2798 case RT_COMMA:
2799 return CPP_COMMA;
2800 case RT_COLON:
2801 return CPP_COLON;
2802 case RT_CLOSE_PAREN:
2803 return CPP_CLOSE_PAREN;
2805 default:
2806 /* Use CPP_EOF as a "no completions possible" code. */
2807 return CPP_EOF;
2812 /* Subroutine of cp_parser_error and cp_parser_required_error.
2814 Issue a diagnostic of the form
2815 FILE:LINE: MESSAGE before TOKEN
2816 where TOKEN is the next token in the input stream. MESSAGE
2817 (specified by the caller) is usually of the form "expected
2818 OTHER-TOKEN".
2820 This bypasses the check for tentative passing, and potentially
2821 adds material needed by cp_parser_required_error.
2823 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2824 suggesting insertion of the missing token.
2826 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2827 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2828 location. */
2830 static void
2831 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2832 required_token missing_token_desc,
2833 location_t matching_location)
2835 cp_token *token = cp_lexer_peek_token (parser->lexer);
2836 /* This diagnostic makes more sense if it is tagged to the line
2837 of the token we just peeked at. */
2838 cp_lexer_set_source_position_from_token (token);
2840 if (token->type == CPP_PRAGMA)
2842 error_at (token->location,
2843 "%<#pragma%> is not allowed here");
2844 cp_parser_skip_to_pragma_eol (parser, token);
2845 return;
2848 /* If this is actually a conflict marker, report it as such. */
2849 if (token->type == CPP_LSHIFT
2850 || token->type == CPP_RSHIFT
2851 || token->type == CPP_EQ_EQ)
2853 location_t loc;
2854 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2856 error_at (loc, "version control conflict marker in file");
2857 expanded_location token_exploc = expand_location (token->location);
2858 /* Consume tokens until the end of the source line. */
2859 while (1)
2861 cp_lexer_consume_token (parser->lexer);
2862 cp_token *next = cp_lexer_peek_token (parser->lexer);
2863 if (next == NULL)
2864 break;
2865 expanded_location next_exploc = expand_location (next->location);
2866 if (next_exploc.file != token_exploc.file)
2867 break;
2868 if (next_exploc.line != token_exploc.line)
2869 break;
2871 return;
2875 gcc_rich_location richloc (input_location);
2877 bool added_matching_location = false;
2879 if (missing_token_desc != RT_NONE)
2881 /* Potentially supply a fix-it hint, suggesting to add the
2882 missing token immediately after the *previous* token.
2883 This may move the primary location within richloc. */
2884 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2885 location_t prev_token_loc
2886 = cp_lexer_previous_token (parser->lexer)->location;
2887 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2889 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2890 Attempt to consolidate diagnostics by printing it as a
2891 secondary range within the main diagnostic. */
2892 if (matching_location != UNKNOWN_LOCATION)
2893 added_matching_location
2894 = richloc.add_location_if_nearby (matching_location);
2897 /* Actually emit the error. */
2898 c_parse_error (gmsgid,
2899 /* Because c_parser_error does not understand
2900 CPP_KEYWORD, keywords are treated like
2901 identifiers. */
2902 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2903 token->u.value, token->flags, &richloc);
2905 if (missing_token_desc != RT_NONE)
2907 /* If we weren't able to consolidate matching_location, then
2908 print it as a secondary diagnostic. */
2909 if (matching_location != UNKNOWN_LOCATION
2910 && !added_matching_location)
2911 inform (matching_location, "to match this %qs",
2912 get_matching_symbol (missing_token_desc));
2916 /* If not parsing tentatively, issue a diagnostic of the form
2917 FILE:LINE: MESSAGE before TOKEN
2918 where TOKEN is the next token in the input stream. MESSAGE
2919 (specified by the caller) is usually of the form "expected
2920 OTHER-TOKEN". */
2922 static void
2923 cp_parser_error (cp_parser* parser, const char* gmsgid)
2925 if (!cp_parser_simulate_error (parser))
2926 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2929 /* Issue an error about name-lookup failing. NAME is the
2930 IDENTIFIER_NODE DECL is the result of
2931 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2932 the thing that we hoped to find. */
2934 static void
2935 cp_parser_name_lookup_error (cp_parser* parser,
2936 tree name,
2937 tree decl,
2938 name_lookup_error desired,
2939 location_t location)
2941 /* If name lookup completely failed, tell the user that NAME was not
2942 declared. */
2943 if (decl == error_mark_node)
2945 if (parser->scope && parser->scope != global_namespace)
2946 error_at (location, "%<%E::%E%> has not been declared",
2947 parser->scope, name);
2948 else if (parser->scope == global_namespace)
2949 error_at (location, "%<::%E%> has not been declared", name);
2950 else if (parser->object_scope
2951 && !CLASS_TYPE_P (parser->object_scope))
2952 error_at (location, "request for member %qE in non-class type %qT",
2953 name, parser->object_scope);
2954 else if (parser->object_scope)
2955 error_at (location, "%<%T::%E%> has not been declared",
2956 parser->object_scope, name);
2957 else
2958 error_at (location, "%qE has not been declared", name);
2960 else if (parser->scope && parser->scope != global_namespace)
2962 switch (desired)
2964 case NLE_TYPE:
2965 error_at (location, "%<%E::%E%> is not a type",
2966 parser->scope, name);
2967 break;
2968 case NLE_CXX98:
2969 error_at (location, "%<%E::%E%> is not a class or namespace",
2970 parser->scope, name);
2971 break;
2972 case NLE_NOT_CXX98:
2973 error_at (location,
2974 "%<%E::%E%> is not a class, namespace, or enumeration",
2975 parser->scope, name);
2976 break;
2977 default:
2978 gcc_unreachable ();
2982 else if (parser->scope == global_namespace)
2984 switch (desired)
2986 case NLE_TYPE:
2987 error_at (location, "%<::%E%> is not a type", name);
2988 break;
2989 case NLE_CXX98:
2990 error_at (location, "%<::%E%> is not a class or namespace", name);
2991 break;
2992 case NLE_NOT_CXX98:
2993 error_at (location,
2994 "%<::%E%> is not a class, namespace, or enumeration",
2995 name);
2996 break;
2997 default:
2998 gcc_unreachable ();
3001 else
3003 switch (desired)
3005 case NLE_TYPE:
3006 error_at (location, "%qE is not a type", name);
3007 break;
3008 case NLE_CXX98:
3009 error_at (location, "%qE is not a class or namespace", name);
3010 break;
3011 case NLE_NOT_CXX98:
3012 error_at (location,
3013 "%qE is not a class, namespace, or enumeration", name);
3014 break;
3015 default:
3016 gcc_unreachable ();
3021 /* If we are parsing tentatively, remember that an error has occurred
3022 during this tentative parse. Returns true if the error was
3023 simulated; false if a message should be issued by the caller. */
3025 static bool
3026 cp_parser_simulate_error (cp_parser* parser)
3028 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3030 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3031 return true;
3033 return false;
3036 /* This function is called when a type is defined. If type
3037 definitions are forbidden at this point, an error message is
3038 issued. */
3040 static bool
3041 cp_parser_check_type_definition (cp_parser* parser)
3043 /* If types are forbidden here, issue a message. */
3044 if (parser->type_definition_forbidden_message)
3046 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3047 in the message need to be interpreted. */
3048 error (parser->type_definition_forbidden_message);
3049 return false;
3051 return true;
3054 /* This function is called when the DECLARATOR is processed. The TYPE
3055 was a type defined in the decl-specifiers. If it is invalid to
3056 define a type in the decl-specifiers for DECLARATOR, an error is
3057 issued. TYPE_LOCATION is the location of TYPE and is used
3058 for error reporting. */
3060 static void
3061 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3062 tree type, location_t type_location)
3064 /* [dcl.fct] forbids type definitions in return types.
3065 Unfortunately, it's not easy to know whether or not we are
3066 processing a return type until after the fact. */
3067 while (declarator
3068 && (declarator->kind == cdk_pointer
3069 || declarator->kind == cdk_reference
3070 || declarator->kind == cdk_ptrmem))
3071 declarator = declarator->declarator;
3072 if (declarator
3073 && declarator->kind == cdk_function)
3075 error_at (type_location,
3076 "new types may not be defined in a return type");
3077 inform (type_location,
3078 "(perhaps a semicolon is missing after the definition of %qT)",
3079 type);
3083 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3084 "<" in any valid C++ program. If the next token is indeed "<",
3085 issue a message warning the user about what appears to be an
3086 invalid attempt to form a template-id. LOCATION is the location
3087 of the type-specifier (TYPE) */
3089 static void
3090 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3091 tree type,
3092 enum tag_types tag_type,
3093 location_t location)
3095 cp_token_position start = 0;
3097 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3099 if (TREE_CODE (type) == TYPE_DECL)
3100 type = TREE_TYPE (type);
3101 if (TYPE_P (type) && !template_placeholder_p (type))
3102 error_at (location, "%qT is not a template", type);
3103 else if (identifier_p (type))
3105 if (tag_type != none_type)
3106 error_at (location, "%qE is not a class template", type);
3107 else
3108 error_at (location, "%qE is not a template", type);
3110 else
3111 error_at (location, "invalid template-id");
3112 /* Remember the location of the invalid "<". */
3113 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3114 start = cp_lexer_token_position (parser->lexer, true);
3115 /* Consume the "<". */
3116 cp_lexer_consume_token (parser->lexer);
3117 /* Parse the template arguments. */
3118 cp_parser_enclosed_template_argument_list (parser);
3119 /* Permanently remove the invalid template arguments so that
3120 this error message is not issued again. */
3121 if (start)
3122 cp_lexer_purge_tokens_after (parser->lexer, start);
3126 /* If parsing an integral constant-expression, issue an error message
3127 about the fact that THING appeared and return true. Otherwise,
3128 return false. In either case, set
3129 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3131 static bool
3132 cp_parser_non_integral_constant_expression (cp_parser *parser,
3133 non_integral_constant thing)
3135 parser->non_integral_constant_expression_p = true;
3136 if (parser->integral_constant_expression_p)
3138 if (!parser->allow_non_integral_constant_expression_p)
3140 const char *msg = NULL;
3141 switch (thing)
3143 case NIC_FLOAT:
3144 pedwarn (input_location, OPT_Wpedantic,
3145 "ISO C++ forbids using a floating-point literal "
3146 "in a constant-expression");
3147 return true;
3148 case NIC_CAST:
3149 error ("a cast to a type other than an integral or "
3150 "enumeration type cannot appear in a "
3151 "constant-expression");
3152 return true;
3153 case NIC_TYPEID:
3154 error ("%<typeid%> operator "
3155 "cannot appear in a constant-expression");
3156 return true;
3157 case NIC_NCC:
3158 error ("non-constant compound literals "
3159 "cannot appear in a constant-expression");
3160 return true;
3161 case NIC_FUNC_CALL:
3162 error ("a function call "
3163 "cannot appear in a constant-expression");
3164 return true;
3165 case NIC_INC:
3166 error ("an increment "
3167 "cannot appear in a constant-expression");
3168 return true;
3169 case NIC_DEC:
3170 error ("an decrement "
3171 "cannot appear in a constant-expression");
3172 return true;
3173 case NIC_ARRAY_REF:
3174 error ("an array reference "
3175 "cannot appear in a constant-expression");
3176 return true;
3177 case NIC_ADDR_LABEL:
3178 error ("the address of a label "
3179 "cannot appear in a constant-expression");
3180 return true;
3181 case NIC_OVERLOADED:
3182 error ("calls to overloaded operators "
3183 "cannot appear in a constant-expression");
3184 return true;
3185 case NIC_ASSIGNMENT:
3186 error ("an assignment cannot appear in a constant-expression");
3187 return true;
3188 case NIC_COMMA:
3189 error ("a comma operator "
3190 "cannot appear in a constant-expression");
3191 return true;
3192 case NIC_CONSTRUCTOR:
3193 error ("a call to a constructor "
3194 "cannot appear in a constant-expression");
3195 return true;
3196 case NIC_TRANSACTION:
3197 error ("a transaction expression "
3198 "cannot appear in a constant-expression");
3199 return true;
3200 case NIC_THIS:
3201 msg = "this";
3202 break;
3203 case NIC_FUNC_NAME:
3204 msg = "__FUNCTION__";
3205 break;
3206 case NIC_PRETTY_FUNC:
3207 msg = "__PRETTY_FUNCTION__";
3208 break;
3209 case NIC_C99_FUNC:
3210 msg = "__func__";
3211 break;
3212 case NIC_VA_ARG:
3213 msg = "va_arg";
3214 break;
3215 case NIC_ARROW:
3216 msg = "->";
3217 break;
3218 case NIC_POINT:
3219 msg = ".";
3220 break;
3221 case NIC_STAR:
3222 msg = "*";
3223 break;
3224 case NIC_ADDR:
3225 msg = "&";
3226 break;
3227 case NIC_PREINCREMENT:
3228 msg = "++";
3229 break;
3230 case NIC_PREDECREMENT:
3231 msg = "--";
3232 break;
3233 case NIC_NEW:
3234 msg = "new";
3235 break;
3236 case NIC_DEL:
3237 msg = "delete";
3238 break;
3239 default:
3240 gcc_unreachable ();
3242 if (msg)
3243 error ("%qs cannot appear in a constant-expression", msg);
3244 return true;
3247 return false;
3250 /* Emit a diagnostic for an invalid type name. This function commits
3251 to the current active tentative parse, if any. (Otherwise, the
3252 problematic construct might be encountered again later, resulting
3253 in duplicate error messages.) LOCATION is the location of ID. */
3255 static void
3256 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3257 location_t location)
3259 tree decl, ambiguous_decls;
3260 cp_parser_commit_to_tentative_parse (parser);
3261 /* Try to lookup the identifier. */
3262 decl = cp_parser_lookup_name (parser, id, none_type,
3263 /*is_template=*/false,
3264 /*is_namespace=*/false,
3265 /*check_dependency=*/true,
3266 &ambiguous_decls, location);
3267 if (ambiguous_decls)
3268 /* If the lookup was ambiguous, an error will already have
3269 been issued. */
3270 return;
3271 /* If the lookup found a template-name, it means that the user forgot
3272 to specify an argument list. Emit a useful error message. */
3273 if (DECL_TYPE_TEMPLATE_P (decl))
3275 auto_diagnostic_group d;
3276 error_at (location,
3277 "invalid use of template-name %qE without an argument list",
3278 decl);
3279 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3280 inform (location, "class template argument deduction is only available "
3281 "with -std=c++17 or -std=gnu++17");
3282 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3284 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3285 error_at (location, "invalid use of destructor %qD as a type", id);
3286 else if (TREE_CODE (decl) == TYPE_DECL)
3287 /* Something like 'unsigned A a;' */
3288 error_at (location, "invalid combination of multiple type-specifiers");
3289 else if (!parser->scope)
3291 /* Issue an error message. */
3292 auto_diagnostic_group d;
3293 name_hint hint;
3294 if (TREE_CODE (id) == IDENTIFIER_NODE)
3295 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3296 if (const char *suggestion = hint.suggestion ())
3298 gcc_rich_location richloc (location);
3299 richloc.add_fixit_replace (suggestion);
3300 error_at (&richloc,
3301 "%qE does not name a type; did you mean %qs?",
3302 id, suggestion);
3304 else
3305 error_at (location, "%qE does not name a type", id);
3306 /* If we're in a template class, it's possible that the user was
3307 referring to a type from a base class. For example:
3309 template <typename T> struct A { typedef T X; };
3310 template <typename T> struct B : public A<T> { X x; };
3312 The user should have said "typename A<T>::X". */
3313 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3314 inform (location, "C++11 %<constexpr%> only available with "
3315 "-std=c++11 or -std=gnu++11");
3316 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3317 inform (location, "C++11 %<noexcept%> only available with "
3318 "-std=c++11 or -std=gnu++11");
3319 else if (cxx_dialect < cxx11
3320 && TREE_CODE (id) == IDENTIFIER_NODE
3321 && id_equal (id, "thread_local"))
3322 inform (location, "C++11 %<thread_local%> only available with "
3323 "-std=c++11 or -std=gnu++11");
3324 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3325 inform (location, "%<concept%> only available with -fconcepts");
3326 else if (processing_template_decl && current_class_type
3327 && TYPE_BINFO (current_class_type))
3329 tree b;
3331 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3333 b = TREE_CHAIN (b))
3335 tree base_type = BINFO_TYPE (b);
3336 if (CLASS_TYPE_P (base_type)
3337 && dependent_type_p (base_type))
3339 tree field;
3340 /* Go from a particular instantiation of the
3341 template (which will have an empty TYPE_FIELDs),
3342 to the main version. */
3343 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3344 for (field = TYPE_FIELDS (base_type);
3345 field;
3346 field = DECL_CHAIN (field))
3347 if (TREE_CODE (field) == TYPE_DECL
3348 && DECL_NAME (field) == id)
3350 inform (location,
3351 "(perhaps %<typename %T::%E%> was intended)",
3352 BINFO_TYPE (b), id);
3353 break;
3355 if (field)
3356 break;
3361 /* Here we diagnose qualified-ids where the scope is actually correct,
3362 but the identifier does not resolve to a valid type name. */
3363 else if (parser->scope != error_mark_node)
3365 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3367 auto_diagnostic_group d;
3368 name_hint hint;
3369 if (decl == error_mark_node)
3370 hint = suggest_alternative_in_explicit_scope (location, id,
3371 parser->scope);
3372 const char *suggestion = hint.suggestion ();
3373 gcc_rich_location richloc (location_of (id));
3374 if (suggestion)
3375 richloc.add_fixit_replace (suggestion);
3376 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3378 if (suggestion)
3379 error_at (&richloc,
3380 "%qE in namespace %qE does not name a template"
3381 " type; did you mean %qs?",
3382 id, parser->scope, suggestion);
3383 else
3384 error_at (&richloc,
3385 "%qE in namespace %qE does not name a template type",
3386 id, parser->scope);
3388 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3390 if (suggestion)
3391 error_at (&richloc,
3392 "%qE in namespace %qE does not name a template"
3393 " type; did you mean %qs?",
3394 TREE_OPERAND (id, 0), parser->scope, suggestion);
3395 else
3396 error_at (&richloc,
3397 "%qE in namespace %qE does not name a template"
3398 " type",
3399 TREE_OPERAND (id, 0), parser->scope);
3401 else
3403 if (suggestion)
3404 error_at (&richloc,
3405 "%qE in namespace %qE does not name a type"
3406 "; did you mean %qs?",
3407 id, parser->scope, suggestion);
3408 else
3409 error_at (&richloc,
3410 "%qE in namespace %qE does not name a type",
3411 id, parser->scope);
3413 if (DECL_P (decl))
3414 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3416 else if (CLASS_TYPE_P (parser->scope)
3417 && constructor_name_p (id, parser->scope))
3419 /* A<T>::A<T>() */
3420 auto_diagnostic_group d;
3421 error_at (location, "%<%T::%E%> names the constructor, not"
3422 " the type", parser->scope, id);
3423 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3424 error_at (location, "and %qT has no template constructors",
3425 parser->scope);
3427 else if (TYPE_P (parser->scope)
3428 && dependent_scope_p (parser->scope))
3430 gcc_rich_location richloc (location);
3431 richloc.add_fixit_insert_before ("typename ");
3432 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3433 error_at (&richloc,
3434 "need %<typename%> before %<%T::%D::%E%> because "
3435 "%<%T::%D%> is a dependent scope",
3436 TYPE_CONTEXT (parser->scope),
3437 TYPENAME_TYPE_FULLNAME (parser->scope),
3439 TYPE_CONTEXT (parser->scope),
3440 TYPENAME_TYPE_FULLNAME (parser->scope));
3441 else
3442 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3443 "%qT is a dependent scope",
3444 parser->scope, id, parser->scope);
3446 else if (TYPE_P (parser->scope))
3448 auto_diagnostic_group d;
3449 if (!COMPLETE_TYPE_P (parser->scope))
3450 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3451 parser->scope);
3452 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3453 error_at (location_of (id),
3454 "%qE in %q#T does not name a template type",
3455 id, parser->scope);
3456 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3457 error_at (location_of (id),
3458 "%qE in %q#T does not name a template type",
3459 TREE_OPERAND (id, 0), parser->scope);
3460 else
3461 error_at (location_of (id),
3462 "%qE in %q#T does not name a type",
3463 id, parser->scope);
3464 if (DECL_P (decl))
3465 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3467 else
3468 gcc_unreachable ();
3472 /* Check for a common situation where a type-name should be present,
3473 but is not, and issue a sensible error message. Returns true if an
3474 invalid type-name was detected.
3476 The situation handled by this function are variable declarations of the
3477 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3478 Usually, `ID' should name a type, but if we got here it means that it
3479 does not. We try to emit the best possible error message depending on
3480 how exactly the id-expression looks like. */
3482 static bool
3483 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3485 tree id;
3486 cp_token *token = cp_lexer_peek_token (parser->lexer);
3488 /* Avoid duplicate error about ambiguous lookup. */
3489 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3491 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3492 if (next->type == CPP_NAME && next->error_reported)
3493 goto out;
3496 cp_parser_parse_tentatively (parser);
3497 id = cp_parser_id_expression (parser,
3498 /*template_keyword_p=*/false,
3499 /*check_dependency_p=*/true,
3500 /*template_p=*/NULL,
3501 /*declarator_p=*/false,
3502 /*optional_p=*/false);
3503 /* If the next token is a (, this is a function with no explicit return
3504 type, i.e. constructor, destructor or conversion op. */
3505 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3506 || TREE_CODE (id) == TYPE_DECL)
3508 cp_parser_abort_tentative_parse (parser);
3509 return false;
3511 if (!cp_parser_parse_definitely (parser))
3512 return false;
3514 /* Emit a diagnostic for the invalid type. */
3515 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3516 out:
3517 /* If we aren't in the middle of a declarator (i.e. in a
3518 parameter-declaration-clause), skip to the end of the declaration;
3519 there's no point in trying to process it. */
3520 if (!parser->in_declarator_p)
3521 cp_parser_skip_to_end_of_block_or_statement (parser);
3522 return true;
3525 /* Consume tokens up to, and including, the next non-nested closing `)'.
3526 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3527 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3528 found an unnested token of that type. */
3530 static int
3531 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3532 bool recovering,
3533 cpp_ttype or_ttype,
3534 bool consume_paren)
3536 unsigned paren_depth = 0;
3537 unsigned brace_depth = 0;
3538 unsigned square_depth = 0;
3539 unsigned condop_depth = 0;
3541 if (recovering && or_ttype == CPP_EOF
3542 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3543 return 0;
3545 while (true)
3547 cp_token * token = cp_lexer_peek_token (parser->lexer);
3549 /* Have we found what we're looking for before the closing paren? */
3550 if (token->type == or_ttype && or_ttype != CPP_EOF
3551 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3552 return -1;
3554 switch (token->type)
3556 case CPP_EOF:
3557 case CPP_PRAGMA_EOL:
3558 /* If we've run out of tokens, then there is no closing `)'. */
3559 return 0;
3561 /* This is good for lambda expression capture-lists. */
3562 case CPP_OPEN_SQUARE:
3563 ++square_depth;
3564 break;
3565 case CPP_CLOSE_SQUARE:
3566 if (!square_depth--)
3567 return 0;
3568 break;
3570 case CPP_SEMICOLON:
3571 /* This matches the processing in skip_to_end_of_statement. */
3572 if (!brace_depth)
3573 return 0;
3574 break;
3576 case CPP_OPEN_BRACE:
3577 ++brace_depth;
3578 break;
3579 case CPP_CLOSE_BRACE:
3580 if (!brace_depth--)
3581 return 0;
3582 break;
3584 case CPP_OPEN_PAREN:
3585 if (!brace_depth)
3586 ++paren_depth;
3587 break;
3589 case CPP_CLOSE_PAREN:
3590 if (!brace_depth && !paren_depth--)
3592 if (consume_paren)
3593 cp_lexer_consume_token (parser->lexer);
3594 return 1;
3596 break;
3598 case CPP_QUERY:
3599 if (!brace_depth && !paren_depth && !square_depth)
3600 ++condop_depth;
3601 break;
3603 case CPP_COLON:
3604 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3605 condop_depth--;
3606 break;
3608 default:
3609 break;
3612 /* Consume the token. */
3613 cp_lexer_consume_token (parser->lexer);
3617 /* Consume tokens up to, and including, the next non-nested closing `)'.
3618 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3619 are doing error recovery. Returns -1 if OR_COMMA is true and we
3620 found an unnested token of that type. */
3622 static int
3623 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3624 bool recovering,
3625 bool or_comma,
3626 bool consume_paren)
3628 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3629 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3630 ttype, consume_paren);
3633 /* Consume tokens until we reach the end of the current statement.
3634 Normally, that will be just before consuming a `;'. However, if a
3635 non-nested `}' comes first, then we stop before consuming that. */
3637 static void
3638 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3640 unsigned nesting_depth = 0;
3642 /* Unwind generic function template scope if necessary. */
3643 if (parser->fully_implicit_function_template_p)
3644 abort_fully_implicit_template (parser);
3646 while (true)
3648 cp_token *token = cp_lexer_peek_token (parser->lexer);
3650 switch (token->type)
3652 case CPP_EOF:
3653 case CPP_PRAGMA_EOL:
3654 /* If we've run out of tokens, stop. */
3655 return;
3657 case CPP_SEMICOLON:
3658 /* If the next token is a `;', we have reached the end of the
3659 statement. */
3660 if (!nesting_depth)
3661 return;
3662 break;
3664 case CPP_CLOSE_BRACE:
3665 /* If this is a non-nested '}', stop before consuming it.
3666 That way, when confronted with something like:
3668 { 3 + }
3670 we stop before consuming the closing '}', even though we
3671 have not yet reached a `;'. */
3672 if (nesting_depth == 0)
3673 return;
3675 /* If it is the closing '}' for a block that we have
3676 scanned, stop -- but only after consuming the token.
3677 That way given:
3679 void f g () { ... }
3680 typedef int I;
3682 we will stop after the body of the erroneously declared
3683 function, but before consuming the following `typedef'
3684 declaration. */
3685 if (--nesting_depth == 0)
3687 cp_lexer_consume_token (parser->lexer);
3688 return;
3690 break;
3692 case CPP_OPEN_BRACE:
3693 ++nesting_depth;
3694 break;
3696 default:
3697 break;
3700 /* Consume the token. */
3701 cp_lexer_consume_token (parser->lexer);
3705 /* This function is called at the end of a statement or declaration.
3706 If the next token is a semicolon, it is consumed; otherwise, error
3707 recovery is attempted. */
3709 static void
3710 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3712 /* Look for the trailing `;'. */
3713 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3715 /* If there is additional (erroneous) input, skip to the end of
3716 the statement. */
3717 cp_parser_skip_to_end_of_statement (parser);
3718 /* If the next token is now a `;', consume it. */
3719 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3720 cp_lexer_consume_token (parser->lexer);
3724 /* Skip tokens until we have consumed an entire block, or until we
3725 have consumed a non-nested `;'. */
3727 static void
3728 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3730 int nesting_depth = 0;
3732 /* Unwind generic function template scope if necessary. */
3733 if (parser->fully_implicit_function_template_p)
3734 abort_fully_implicit_template (parser);
3736 while (nesting_depth >= 0)
3738 cp_token *token = cp_lexer_peek_token (parser->lexer);
3740 switch (token->type)
3742 case CPP_EOF:
3743 case CPP_PRAGMA_EOL:
3744 /* If we've run out of tokens, stop. */
3745 return;
3747 case CPP_SEMICOLON:
3748 /* Stop if this is an unnested ';'. */
3749 if (!nesting_depth)
3750 nesting_depth = -1;
3751 break;
3753 case CPP_CLOSE_BRACE:
3754 /* Stop if this is an unnested '}', or closes the outermost
3755 nesting level. */
3756 nesting_depth--;
3757 if (nesting_depth < 0)
3758 return;
3759 if (!nesting_depth)
3760 nesting_depth = -1;
3761 break;
3763 case CPP_OPEN_BRACE:
3764 /* Nest. */
3765 nesting_depth++;
3766 break;
3768 default:
3769 break;
3772 /* Consume the token. */
3773 cp_lexer_consume_token (parser->lexer);
3777 /* Skip tokens until a non-nested closing curly brace is the next
3778 token, or there are no more tokens. Return true in the first case,
3779 false otherwise. */
3781 static bool
3782 cp_parser_skip_to_closing_brace (cp_parser *parser)
3784 unsigned nesting_depth = 0;
3786 while (true)
3788 cp_token *token = cp_lexer_peek_token (parser->lexer);
3790 switch (token->type)
3792 case CPP_EOF:
3793 case CPP_PRAGMA_EOL:
3794 /* If we've run out of tokens, stop. */
3795 return false;
3797 case CPP_CLOSE_BRACE:
3798 /* If the next token is a non-nested `}', then we have reached
3799 the end of the current block. */
3800 if (nesting_depth-- == 0)
3801 return true;
3802 break;
3804 case CPP_OPEN_BRACE:
3805 /* If it the next token is a `{', then we are entering a new
3806 block. Consume the entire block. */
3807 ++nesting_depth;
3808 break;
3810 default:
3811 break;
3814 /* Consume the token. */
3815 cp_lexer_consume_token (parser->lexer);
3819 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3820 parameter is the PRAGMA token, allowing us to purge the entire pragma
3821 sequence. */
3823 static void
3824 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3826 cp_token *token;
3828 parser->lexer->in_pragma = false;
3831 token = cp_lexer_consume_token (parser->lexer);
3832 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3834 /* Ensure that the pragma is not parsed again. */
3835 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3838 /* Require pragma end of line, resyncing with it as necessary. The
3839 arguments are as for cp_parser_skip_to_pragma_eol. */
3841 static void
3842 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3844 parser->lexer->in_pragma = false;
3845 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3846 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3849 /* This is a simple wrapper around make_typename_type. When the id is
3850 an unresolved identifier node, we can provide a superior diagnostic
3851 using cp_parser_diagnose_invalid_type_name. */
3853 static tree
3854 cp_parser_make_typename_type (cp_parser *parser, tree id,
3855 location_t id_location)
3857 tree result;
3858 if (identifier_p (id))
3860 result = make_typename_type (parser->scope, id, typename_type,
3861 /*complain=*/tf_none);
3862 if (result == error_mark_node)
3863 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3864 return result;
3866 return make_typename_type (parser->scope, id, typename_type, tf_error);
3869 /* This is a wrapper around the
3870 make_{pointer,ptrmem,reference}_declarator functions that decides
3871 which one to call based on the CODE and CLASS_TYPE arguments. The
3872 CODE argument should be one of the values returned by
3873 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3874 appertain to the pointer or reference. */
3876 static cp_declarator *
3877 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3878 cp_cv_quals cv_qualifiers,
3879 cp_declarator *target,
3880 tree attributes)
3882 if (code == ERROR_MARK || target == cp_error_declarator)
3883 return cp_error_declarator;
3885 if (code == INDIRECT_REF)
3886 if (class_type == NULL_TREE)
3887 return make_pointer_declarator (cv_qualifiers, target, attributes);
3888 else
3889 return make_ptrmem_declarator (cv_qualifiers, class_type,
3890 target, attributes);
3891 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3892 return make_reference_declarator (cv_qualifiers, target,
3893 false, attributes);
3894 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3895 return make_reference_declarator (cv_qualifiers, target,
3896 true, attributes);
3897 gcc_unreachable ();
3900 /* Create a new C++ parser. */
3902 static cp_parser *
3903 cp_parser_new (void)
3905 cp_parser *parser;
3906 cp_lexer *lexer;
3907 unsigned i;
3909 /* cp_lexer_new_main is called before doing GC allocation because
3910 cp_lexer_new_main might load a PCH file. */
3911 lexer = cp_lexer_new_main ();
3913 /* Initialize the binops_by_token so that we can get the tree
3914 directly from the token. */
3915 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3916 binops_by_token[binops[i].token_type] = binops[i];
3918 parser = ggc_cleared_alloc<cp_parser> ();
3919 parser->lexer = lexer;
3920 parser->context = cp_parser_context_new (NULL);
3922 /* For now, we always accept GNU extensions. */
3923 parser->allow_gnu_extensions_p = 1;
3925 /* The `>' token is a greater-than operator, not the end of a
3926 template-id. */
3927 parser->greater_than_is_operator_p = true;
3929 parser->default_arg_ok_p = true;
3931 /* We are not parsing a constant-expression. */
3932 parser->integral_constant_expression_p = false;
3933 parser->allow_non_integral_constant_expression_p = false;
3934 parser->non_integral_constant_expression_p = false;
3936 /* Local variable names are not forbidden. */
3937 parser->local_variables_forbidden_p = false;
3939 /* We are not processing an `extern "C"' declaration. */
3940 parser->in_unbraced_linkage_specification_p = false;
3942 /* We are not processing a declarator. */
3943 parser->in_declarator_p = false;
3945 /* We are not processing a template-argument-list. */
3946 parser->in_template_argument_list_p = false;
3948 /* We are not in an iteration statement. */
3949 parser->in_statement = 0;
3951 /* We are not in a switch statement. */
3952 parser->in_switch_statement_p = false;
3954 /* We are not parsing a type-id inside an expression. */
3955 parser->in_type_id_in_expr_p = false;
3957 /* String literals should be translated to the execution character set. */
3958 parser->translate_strings_p = true;
3960 /* We are not parsing a function body. */
3961 parser->in_function_body = false;
3963 /* We can correct until told otherwise. */
3964 parser->colon_corrects_to_scope_p = true;
3966 /* The unparsed function queue is empty. */
3967 push_unparsed_function_queues (parser);
3969 /* There are no classes being defined. */
3970 parser->num_classes_being_defined = 0;
3972 /* No template parameters apply. */
3973 parser->num_template_parameter_lists = 0;
3975 /* Special parsing data structures. */
3976 parser->omp_declare_simd = NULL;
3977 parser->oacc_routine = NULL;
3979 /* Not declaring an implicit function template. */
3980 parser->auto_is_implicit_function_template_parm_p = false;
3981 parser->fully_implicit_function_template_p = false;
3982 parser->implicit_template_parms = 0;
3983 parser->implicit_template_scope = 0;
3985 /* Allow constrained-type-specifiers. */
3986 parser->prevent_constrained_type_specifiers = 0;
3988 /* We haven't yet seen an 'extern "C"'. */
3989 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3991 return parser;
3994 /* Create a cp_lexer structure which will emit the tokens in CACHE
3995 and push it onto the parser's lexer stack. This is used for delayed
3996 parsing of in-class method bodies and default arguments, and should
3997 not be confused with tentative parsing. */
3998 static void
3999 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4001 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4002 lexer->next = parser->lexer;
4003 parser->lexer = lexer;
4005 /* Move the current source position to that of the first token in the
4006 new lexer. */
4007 cp_lexer_set_source_position_from_token (lexer->next_token);
4010 /* Pop the top lexer off the parser stack. This is never used for the
4011 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4012 static void
4013 cp_parser_pop_lexer (cp_parser *parser)
4015 cp_lexer *lexer = parser->lexer;
4016 parser->lexer = lexer->next;
4017 cp_lexer_destroy (lexer);
4019 /* Put the current source position back where it was before this
4020 lexer was pushed. */
4021 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4024 /* Lexical conventions [gram.lex] */
4026 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4027 identifier. */
4029 static cp_expr
4030 cp_parser_identifier (cp_parser* parser)
4032 cp_token *token;
4034 /* Look for the identifier. */
4035 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4036 /* Return the value. */
4037 if (token)
4038 return cp_expr (token->u.value, token->location);
4039 else
4040 return error_mark_node;
4043 /* Parse a sequence of adjacent string constants. Returns a
4044 TREE_STRING representing the combined, nul-terminated string
4045 constant. If TRANSLATE is true, translate the string to the
4046 execution character set. If WIDE_OK is true, a wide string is
4047 invalid here.
4049 C++98 [lex.string] says that if a narrow string literal token is
4050 adjacent to a wide string literal token, the behavior is undefined.
4051 However, C99 6.4.5p4 says that this results in a wide string literal.
4052 We follow C99 here, for consistency with the C front end.
4054 This code is largely lifted from lex_string() in c-lex.c.
4056 FUTURE: ObjC++ will need to handle @-strings here. */
4057 static cp_expr
4058 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4059 bool lookup_udlit = true)
4061 tree value;
4062 size_t count;
4063 struct obstack str_ob;
4064 struct obstack loc_ob;
4065 cpp_string str, istr, *strs;
4066 cp_token *tok;
4067 enum cpp_ttype type, curr_type;
4068 int have_suffix_p = 0;
4069 tree string_tree;
4070 tree suffix_id = NULL_TREE;
4071 bool curr_tok_is_userdef_p = false;
4073 tok = cp_lexer_peek_token (parser->lexer);
4074 if (!cp_parser_is_string_literal (tok))
4076 cp_parser_error (parser, "expected string-literal");
4077 return error_mark_node;
4080 location_t loc = tok->location;
4082 if (cpp_userdef_string_p (tok->type))
4084 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4085 curr_type = cpp_userdef_string_remove_type (tok->type);
4086 curr_tok_is_userdef_p = true;
4088 else
4090 string_tree = tok->u.value;
4091 curr_type = tok->type;
4093 type = curr_type;
4095 /* Try to avoid the overhead of creating and destroying an obstack
4096 for the common case of just one string. */
4097 if (!cp_parser_is_string_literal
4098 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4100 cp_lexer_consume_token (parser->lexer);
4102 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4103 str.len = TREE_STRING_LENGTH (string_tree);
4104 count = 1;
4106 if (curr_tok_is_userdef_p)
4108 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4109 have_suffix_p = 1;
4110 curr_type = cpp_userdef_string_remove_type (tok->type);
4112 else
4113 curr_type = tok->type;
4115 strs = &str;
4117 else
4119 location_t last_tok_loc = tok->location;
4120 gcc_obstack_init (&str_ob);
4121 gcc_obstack_init (&loc_ob);
4122 count = 0;
4126 cp_lexer_consume_token (parser->lexer);
4127 count++;
4128 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4129 str.len = TREE_STRING_LENGTH (string_tree);
4131 if (curr_tok_is_userdef_p)
4133 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4134 if (have_suffix_p == 0)
4136 suffix_id = curr_suffix_id;
4137 have_suffix_p = 1;
4139 else if (have_suffix_p == 1
4140 && curr_suffix_id != suffix_id)
4142 error ("inconsistent user-defined literal suffixes"
4143 " %qD and %qD in string literal",
4144 suffix_id, curr_suffix_id);
4145 have_suffix_p = -1;
4147 curr_type = cpp_userdef_string_remove_type (tok->type);
4149 else
4150 curr_type = tok->type;
4152 if (type != curr_type)
4154 if (type == CPP_STRING)
4155 type = curr_type;
4156 else if (curr_type != CPP_STRING)
4158 rich_location rich_loc (line_table, tok->location);
4159 rich_loc.add_range (last_tok_loc);
4160 error_at (&rich_loc,
4161 "unsupported non-standard concatenation "
4162 "of string literals");
4166 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4167 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4169 last_tok_loc = tok->location;
4171 tok = cp_lexer_peek_token (parser->lexer);
4172 if (cpp_userdef_string_p (tok->type))
4174 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4175 curr_type = cpp_userdef_string_remove_type (tok->type);
4176 curr_tok_is_userdef_p = true;
4178 else
4180 string_tree = tok->u.value;
4181 curr_type = tok->type;
4182 curr_tok_is_userdef_p = false;
4185 while (cp_parser_is_string_literal (tok));
4187 /* A string literal built by concatenation has its caret=start at
4188 the start of the initial string, and its finish at the finish of
4189 the final string literal. */
4190 loc = make_location (loc, loc, get_finish (last_tok_loc));
4192 strs = (cpp_string *) obstack_finish (&str_ob);
4195 if (type != CPP_STRING && !wide_ok)
4197 cp_parser_error (parser, "a wide string is invalid in this context");
4198 type = CPP_STRING;
4201 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4202 (parse_in, strs, count, &istr, type))
4204 value = build_string (istr.len, (const char *)istr.text);
4205 free (CONST_CAST (unsigned char *, istr.text));
4206 if (count > 1)
4208 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4209 gcc_assert (g_string_concat_db);
4210 g_string_concat_db->record_string_concatenation (count, locs);
4213 switch (type)
4215 default:
4216 case CPP_STRING:
4217 case CPP_UTF8STRING:
4218 TREE_TYPE (value) = char_array_type_node;
4219 break;
4220 case CPP_STRING16:
4221 TREE_TYPE (value) = char16_array_type_node;
4222 break;
4223 case CPP_STRING32:
4224 TREE_TYPE (value) = char32_array_type_node;
4225 break;
4226 case CPP_WSTRING:
4227 TREE_TYPE (value) = wchar_array_type_node;
4228 break;
4231 value = fix_string_type (value);
4233 if (have_suffix_p)
4235 tree literal = build_userdef_literal (suffix_id, value,
4236 OT_NONE, NULL_TREE);
4237 if (lookup_udlit)
4238 value = cp_parser_userdef_string_literal (literal);
4239 else
4240 value = literal;
4243 else
4244 /* cpp_interpret_string has issued an error. */
4245 value = error_mark_node;
4247 if (count > 1)
4249 obstack_free (&str_ob, 0);
4250 obstack_free (&loc_ob, 0);
4253 return cp_expr (value, loc);
4256 /* Look up a literal operator with the name and the exact arguments. */
4258 static tree
4259 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4261 tree decl;
4262 decl = lookup_name (name);
4263 if (!decl || !is_overloaded_fn (decl))
4264 return error_mark_node;
4266 for (lkp_iterator iter (decl); iter; ++iter)
4268 unsigned int ix;
4269 bool found = true;
4270 tree fn = *iter;
4271 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4272 if (parmtypes != NULL_TREE)
4274 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4275 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4277 tree tparm = TREE_VALUE (parmtypes);
4278 tree targ = TREE_TYPE ((*args)[ix]);
4279 bool ptr = TYPE_PTR_P (tparm);
4280 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4281 if ((ptr || arr || !same_type_p (tparm, targ))
4282 && (!ptr || !arr
4283 || !same_type_p (TREE_TYPE (tparm),
4284 TREE_TYPE (targ))))
4285 found = false;
4287 if (found
4288 && ix == vec_safe_length (args)
4289 /* May be this should be sufficient_parms_p instead,
4290 depending on how exactly should user-defined literals
4291 work in presence of default arguments on the literal
4292 operator parameters. */
4293 && parmtypes == void_list_node)
4294 return decl;
4298 return error_mark_node;
4301 /* Parse a user-defined char constant. Returns a call to a user-defined
4302 literal operator taking the character as an argument. */
4304 static cp_expr
4305 cp_parser_userdef_char_literal (cp_parser *parser)
4307 cp_token *token = cp_lexer_consume_token (parser->lexer);
4308 tree literal = token->u.value;
4309 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4310 tree value = USERDEF_LITERAL_VALUE (literal);
4311 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4312 tree decl, result;
4314 /* Build up a call to the user-defined operator */
4315 /* Lookup the name we got back from the id-expression. */
4316 vec<tree, va_gc> *args = make_tree_vector ();
4317 vec_safe_push (args, value);
4318 decl = lookup_literal_operator (name, args);
4319 if (!decl || decl == error_mark_node)
4321 error ("unable to find character literal operator %qD with %qT argument",
4322 name, TREE_TYPE (value));
4323 release_tree_vector (args);
4324 return error_mark_node;
4326 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4327 release_tree_vector (args);
4328 return result;
4331 /* A subroutine of cp_parser_userdef_numeric_literal to
4332 create a char... template parameter pack from a string node. */
4334 static tree
4335 make_char_string_pack (tree value)
4337 tree charvec;
4338 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4339 const char *str = TREE_STRING_POINTER (value);
4340 int i, len = TREE_STRING_LENGTH (value) - 1;
4341 tree argvec = make_tree_vec (1);
4343 /* Fill in CHARVEC with all of the parameters. */
4344 charvec = make_tree_vec (len);
4345 for (i = 0; i < len; ++i)
4347 unsigned char s[3] = { '\'', str[i], '\'' };
4348 cpp_string in = { 3, s };
4349 cpp_string out = { 0, 0 };
4350 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4351 return NULL_TREE;
4352 gcc_assert (out.len == 2);
4353 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4354 out.text[0]);
4357 /* Build the argument packs. */
4358 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4360 TREE_VEC_ELT (argvec, 0) = argpack;
4362 return argvec;
4365 /* A subroutine of cp_parser_userdef_numeric_literal to
4366 create a char... template parameter pack from a string node. */
4368 static tree
4369 make_string_pack (tree value)
4371 tree charvec;
4372 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4373 const unsigned char *str
4374 = (const unsigned char *) TREE_STRING_POINTER (value);
4375 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4376 int len = TREE_STRING_LENGTH (value) / sz - 1;
4377 tree argvec = make_tree_vec (2);
4379 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4380 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4382 /* First template parm is character type. */
4383 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4385 /* Fill in CHARVEC with all of the parameters. */
4386 charvec = make_tree_vec (len);
4387 for (int i = 0; i < len; ++i)
4388 TREE_VEC_ELT (charvec, i)
4389 = double_int_to_tree (str_char_type_node,
4390 double_int::from_buffer (str + i * sz, sz));
4392 /* Build the argument packs. */
4393 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4395 TREE_VEC_ELT (argvec, 1) = argpack;
4397 return argvec;
4400 /* Parse a user-defined numeric constant. returns a call to a user-defined
4401 literal operator. */
4403 static cp_expr
4404 cp_parser_userdef_numeric_literal (cp_parser *parser)
4406 cp_token *token = cp_lexer_consume_token (parser->lexer);
4407 tree literal = token->u.value;
4408 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4409 tree value = USERDEF_LITERAL_VALUE (literal);
4410 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4411 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4412 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4413 tree decl, result;
4414 vec<tree, va_gc> *args;
4416 /* Look for a literal operator taking the exact type of numeric argument
4417 as the literal value. */
4418 args = make_tree_vector ();
4419 vec_safe_push (args, value);
4420 decl = lookup_literal_operator (name, args);
4421 if (decl && decl != error_mark_node)
4423 result = finish_call_expr (decl, &args, false, true,
4424 tf_warning_or_error);
4426 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4428 warning_at (token->location, OPT_Woverflow,
4429 "integer literal exceeds range of %qT type",
4430 long_long_unsigned_type_node);
4432 else
4434 if (overflow > 0)
4435 warning_at (token->location, OPT_Woverflow,
4436 "floating literal exceeds range of %qT type",
4437 long_double_type_node);
4438 else if (overflow < 0)
4439 warning_at (token->location, OPT_Woverflow,
4440 "floating literal truncated to zero");
4443 release_tree_vector (args);
4444 return result;
4446 release_tree_vector (args);
4448 /* If the numeric argument didn't work, look for a raw literal
4449 operator taking a const char* argument consisting of the number
4450 in string format. */
4451 args = make_tree_vector ();
4452 vec_safe_push (args, num_string);
4453 decl = lookup_literal_operator (name, args);
4454 if (decl && decl != error_mark_node)
4456 result = finish_call_expr (decl, &args, false, true,
4457 tf_warning_or_error);
4458 release_tree_vector (args);
4459 return result;
4461 release_tree_vector (args);
4463 /* If the raw literal didn't work, look for a non-type template
4464 function with parameter pack char.... Call the function with
4465 template parameter characters representing the number. */
4466 args = make_tree_vector ();
4467 decl = lookup_literal_operator (name, args);
4468 if (decl && decl != error_mark_node)
4470 tree tmpl_args = make_char_string_pack (num_string);
4471 if (tmpl_args == NULL_TREE)
4473 error ("failed to translate literal to execution character set %qT",
4474 num_string);
4475 return error_mark_node;
4477 decl = lookup_template_function (decl, tmpl_args);
4478 result = finish_call_expr (decl, &args, false, true,
4479 tf_warning_or_error);
4480 release_tree_vector (args);
4481 return result;
4484 release_tree_vector (args);
4486 /* In C++14 the standard library defines complex number suffixes that
4487 conflict with GNU extensions. Prefer them if <complex> is #included. */
4488 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4489 bool i14 = (cxx_dialect > cxx11
4490 && (id_equal (suffix_id, "i")
4491 || id_equal (suffix_id, "if")
4492 || id_equal (suffix_id, "il")));
4493 diagnostic_t kind = DK_ERROR;
4494 int opt = 0;
4496 if (i14 && ext)
4498 tree cxlit = lookup_qualified_name (std_node,
4499 get_identifier ("complex_literals"),
4500 0, false, false);
4501 if (cxlit == error_mark_node)
4503 /* No <complex>, so pedwarn and use GNU semantics. */
4504 kind = DK_PEDWARN;
4505 opt = OPT_Wpedantic;
4509 bool complained
4510 = emit_diagnostic (kind, input_location, opt,
4511 "unable to find numeric literal operator %qD", name);
4513 if (!complained)
4514 /* Don't inform either. */;
4515 else if (i14)
4517 inform (token->location, "add %<using namespace std::complex_literals%> "
4518 "(from <complex>) to enable the C++14 user-defined literal "
4519 "suffixes");
4520 if (ext)
4521 inform (token->location, "or use %<j%> instead of %<i%> for the "
4522 "GNU built-in suffix");
4524 else if (!ext)
4525 inform (token->location, "use -fext-numeric-literals "
4526 "to enable more built-in suffixes");
4528 if (kind == DK_ERROR)
4529 value = error_mark_node;
4530 else
4532 /* Use the built-in semantics. */
4533 tree type;
4534 if (id_equal (suffix_id, "i"))
4536 if (TREE_CODE (value) == INTEGER_CST)
4537 type = integer_type_node;
4538 else
4539 type = double_type_node;
4541 else if (id_equal (suffix_id, "if"))
4542 type = float_type_node;
4543 else /* if (id_equal (suffix_id, "il")) */
4544 type = long_double_type_node;
4546 value = build_complex (build_complex_type (type),
4547 fold_convert (type, integer_zero_node),
4548 fold_convert (type, value));
4551 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4552 /* Avoid repeated diagnostics. */
4553 token->u.value = value;
4554 return value;
4557 /* Parse a user-defined string constant. Returns a call to a user-defined
4558 literal operator taking a character pointer and the length of the string
4559 as arguments. */
4561 static tree
4562 cp_parser_userdef_string_literal (tree literal)
4564 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4565 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4566 tree value = USERDEF_LITERAL_VALUE (literal);
4567 int len = TREE_STRING_LENGTH (value)
4568 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4569 tree decl;
4571 /* Build up a call to the user-defined operator. */
4572 /* Lookup the name we got back from the id-expression. */
4573 releasing_vec rargs;
4574 vec<tree, va_gc> *&args = rargs.get_ref();
4575 vec_safe_push (args, value);
4576 vec_safe_push (args, build_int_cst (size_type_node, len));
4577 decl = lookup_literal_operator (name, args);
4579 if (decl && decl != error_mark_node)
4580 return finish_call_expr (decl, &args, false, true,
4581 tf_warning_or_error);
4583 /* Look for a suitable template function, either (C++20) with a single
4584 parameter of class type, or (N3599) with typename parameter CharT and
4585 parameter pack CharT... */
4586 args->truncate (0);
4587 decl = lookup_literal_operator (name, args);
4588 if (decl && decl != error_mark_node)
4590 /* Use resolve_nondeduced_context to try to choose one form of template
4591 or the other. */
4592 tree tmpl_args = make_tree_vec (1);
4593 TREE_VEC_ELT (tmpl_args, 0) = value;
4594 decl = lookup_template_function (decl, tmpl_args);
4595 tree res = resolve_nondeduced_context (decl, tf_none);
4596 if (DECL_P (res))
4597 decl = res;
4598 else
4600 TREE_OPERAND (decl, 1) = make_string_pack (value);
4601 res = resolve_nondeduced_context (decl, tf_none);
4602 if (DECL_P (res))
4603 decl = res;
4605 if (!DECL_P (decl) && cxx_dialect > cxx17)
4606 TREE_OPERAND (decl, 1) = tmpl_args;
4607 return finish_call_expr (decl, &args, false, true,
4608 tf_warning_or_error);
4611 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4612 name, TREE_TYPE (value), size_type_node);
4613 return error_mark_node;
4617 /* Basic concepts [gram.basic] */
4619 /* Parse a translation-unit.
4621 translation-unit:
4622 declaration-seq [opt] */
4624 static void
4625 cp_parser_translation_unit (cp_parser* parser)
4627 gcc_checking_assert (!cp_error_declarator);
4629 /* Create the declarator obstack. */
4630 gcc_obstack_init (&declarator_obstack);
4631 /* Create the error declarator. */
4632 cp_error_declarator = make_declarator (cdk_error);
4633 /* Create the empty parameter list. */
4634 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4635 UNKNOWN_LOCATION);
4636 /* Remember where the base of the declarator obstack lies. */
4637 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4639 bool implicit_extern_c = false;
4641 for (;;)
4643 cp_token *token = cp_lexer_peek_token (parser->lexer);
4645 /* If we're entering or exiting a region that's implicitly
4646 extern "C", modify the lang context appropriately. */
4647 if (implicit_extern_c
4648 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4650 implicit_extern_c = !implicit_extern_c;
4651 if (implicit_extern_c)
4652 push_lang_context (lang_name_c);
4653 else
4654 pop_lang_context ();
4657 if (token->type == CPP_EOF)
4658 break;
4660 if (token->type == CPP_CLOSE_BRACE)
4662 cp_parser_error (parser, "expected declaration");
4663 cp_lexer_consume_token (parser->lexer);
4664 /* If the next token is now a `;', consume it. */
4665 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4666 cp_lexer_consume_token (parser->lexer);
4668 else
4669 cp_parser_toplevel_declaration (parser);
4672 /* Get rid of the token array; we don't need it any more. */
4673 cp_lexer_destroy (parser->lexer);
4674 parser->lexer = NULL;
4676 /* The EOF should have reset this. */
4677 gcc_checking_assert (!implicit_extern_c);
4679 /* Make sure the declarator obstack was fully cleaned up. */
4680 gcc_assert (obstack_next_free (&declarator_obstack)
4681 == declarator_obstack_base);
4684 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4685 decltype context. */
4687 static inline tsubst_flags_t
4688 complain_flags (bool decltype_p)
4690 tsubst_flags_t complain = tf_warning_or_error;
4691 if (decltype_p)
4692 complain |= tf_decltype;
4693 return complain;
4696 /* We're about to parse a collection of statements. If we're currently
4697 parsing tentatively, set up a firewall so that any nested
4698 cp_parser_commit_to_tentative_parse won't affect the current context. */
4700 static cp_token_position
4701 cp_parser_start_tentative_firewall (cp_parser *parser)
4703 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4704 return 0;
4706 cp_parser_parse_tentatively (parser);
4707 cp_parser_commit_to_topmost_tentative_parse (parser);
4708 return cp_lexer_token_position (parser->lexer, false);
4711 /* We've finished parsing the collection of statements. Wrap up the
4712 firewall and replace the relevant tokens with the parsed form. */
4714 static void
4715 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4716 tree expr)
4718 if (!start)
4719 return;
4721 /* Finish the firewall level. */
4722 cp_parser_parse_definitely (parser);
4723 /* And remember the result of the parse for when we try again. */
4724 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4725 token->type = CPP_PREPARSED_EXPR;
4726 token->u.value = expr;
4727 token->keyword = RID_MAX;
4728 cp_lexer_purge_tokens_after (parser->lexer, start);
4731 /* Like the above functions, but let the user modify the tokens. Used by
4732 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4733 later parses, so it makes sense to localize the effects of
4734 cp_parser_commit_to_tentative_parse. */
4736 struct tentative_firewall
4738 cp_parser *parser;
4739 bool set;
4741 tentative_firewall (cp_parser *p): parser(p)
4743 /* If we're currently parsing tentatively, start a committed level as a
4744 firewall and then an inner tentative parse. */
4745 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4747 cp_parser_parse_tentatively (parser);
4748 cp_parser_commit_to_topmost_tentative_parse (parser);
4749 cp_parser_parse_tentatively (parser);
4753 ~tentative_firewall()
4755 if (set)
4757 /* Finish the inner tentative parse and the firewall, propagating any
4758 uncommitted error state to the outer tentative parse. */
4759 bool err = cp_parser_error_occurred (parser);
4760 cp_parser_parse_definitely (parser);
4761 cp_parser_parse_definitely (parser);
4762 if (err)
4763 cp_parser_simulate_error (parser);
4768 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4769 This class is for tracking such a matching pair of symbols.
4770 In particular, it tracks the location of the first token,
4771 so that if the second token is missing, we can highlight the
4772 location of the first token when notifying the user about the
4773 problem. */
4775 template <typename traits_t>
4776 class token_pair
4778 public:
4779 /* token_pair's ctor. */
4780 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4782 /* If the next token is the opening symbol for this pair, consume it and
4783 return true.
4784 Otherwise, issue an error and return false.
4785 In either case, record the location of the opening token. */
4787 bool require_open (cp_parser *parser)
4789 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4790 return cp_parser_require (parser, traits_t::open_token_type,
4791 traits_t::required_token_open);
4794 /* Consume the next token from PARSER, recording its location as
4795 that of the opening token within the pair. */
4797 cp_token * consume_open (cp_parser *parser)
4799 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4800 gcc_assert (tok->type == traits_t::open_token_type);
4801 m_open_loc = tok->location;
4802 return tok;
4805 /* If the next token is the closing symbol for this pair, consume it
4806 and return it.
4807 Otherwise, issue an error, highlighting the location of the
4808 corresponding opening token, and return NULL. */
4810 cp_token *require_close (cp_parser *parser) const
4812 return cp_parser_require (parser, traits_t::close_token_type,
4813 traits_t::required_token_close,
4814 m_open_loc);
4817 private:
4818 location_t m_open_loc;
4821 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4823 struct matching_paren_traits
4825 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4826 static const enum required_token required_token_open = RT_OPEN_PAREN;
4827 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4828 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4831 /* "matching_parens" is a token_pair<T> class for tracking matching
4832 pairs of parentheses. */
4834 typedef token_pair<matching_paren_traits> matching_parens;
4836 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4838 struct matching_brace_traits
4840 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4841 static const enum required_token required_token_open = RT_OPEN_BRACE;
4842 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4843 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4846 /* "matching_braces" is a token_pair<T> class for tracking matching
4847 pairs of braces. */
4849 typedef token_pair<matching_brace_traits> matching_braces;
4852 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4853 enclosing parentheses. */
4855 static cp_expr
4856 cp_parser_statement_expr (cp_parser *parser)
4858 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4860 /* Consume the '('. */
4861 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4862 matching_parens parens;
4863 parens.consume_open (parser);
4864 /* Start the statement-expression. */
4865 tree expr = begin_stmt_expr ();
4866 /* Parse the compound-statement. */
4867 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4868 /* Finish up. */
4869 expr = finish_stmt_expr (expr, false);
4870 /* Consume the ')'. */
4871 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4872 if (!parens.require_close (parser))
4873 cp_parser_skip_to_end_of_statement (parser);
4875 cp_parser_end_tentative_firewall (parser, start, expr);
4876 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4877 return cp_expr (expr, combined_loc);
4880 /* Expressions [gram.expr] */
4882 /* Parse a fold-operator.
4884 fold-operator:
4885 - * / % ^ & | = < > << >>
4886 = -= *= /= %= ^= &= |= <<= >>=
4887 == != <= >= && || , .* ->*
4889 This returns the tree code corresponding to the matched operator
4890 as an int. When the current token matches a compound assignment
4891 opertor, the resulting tree code is the negative value of the
4892 non-assignment operator. */
4894 static int
4895 cp_parser_fold_operator (cp_token *token)
4897 switch (token->type)
4899 case CPP_PLUS: return PLUS_EXPR;
4900 case CPP_MINUS: return MINUS_EXPR;
4901 case CPP_MULT: return MULT_EXPR;
4902 case CPP_DIV: return TRUNC_DIV_EXPR;
4903 case CPP_MOD: return TRUNC_MOD_EXPR;
4904 case CPP_XOR: return BIT_XOR_EXPR;
4905 case CPP_AND: return BIT_AND_EXPR;
4906 case CPP_OR: return BIT_IOR_EXPR;
4907 case CPP_LSHIFT: return LSHIFT_EXPR;
4908 case CPP_RSHIFT: return RSHIFT_EXPR;
4910 case CPP_EQ: return -NOP_EXPR;
4911 case CPP_PLUS_EQ: return -PLUS_EXPR;
4912 case CPP_MINUS_EQ: return -MINUS_EXPR;
4913 case CPP_MULT_EQ: return -MULT_EXPR;
4914 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4915 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4916 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4917 case CPP_AND_EQ: return -BIT_AND_EXPR;
4918 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4919 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4920 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4922 case CPP_EQ_EQ: return EQ_EXPR;
4923 case CPP_NOT_EQ: return NE_EXPR;
4924 case CPP_LESS: return LT_EXPR;
4925 case CPP_GREATER: return GT_EXPR;
4926 case CPP_LESS_EQ: return LE_EXPR;
4927 case CPP_GREATER_EQ: return GE_EXPR;
4929 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4930 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4932 case CPP_COMMA: return COMPOUND_EXPR;
4934 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4935 case CPP_DEREF_STAR: return MEMBER_REF;
4937 default: return ERROR_MARK;
4941 /* Returns true if CODE indicates a binary expression, which is not allowed in
4942 the LHS of a fold-expression. More codes will need to be added to use this
4943 function in other contexts. */
4945 static bool
4946 is_binary_op (tree_code code)
4948 switch (code)
4950 case PLUS_EXPR:
4951 case POINTER_PLUS_EXPR:
4952 case MINUS_EXPR:
4953 case MULT_EXPR:
4954 case TRUNC_DIV_EXPR:
4955 case TRUNC_MOD_EXPR:
4956 case BIT_XOR_EXPR:
4957 case BIT_AND_EXPR:
4958 case BIT_IOR_EXPR:
4959 case LSHIFT_EXPR:
4960 case RSHIFT_EXPR:
4962 case MODOP_EXPR:
4964 case EQ_EXPR:
4965 case NE_EXPR:
4966 case LE_EXPR:
4967 case GE_EXPR:
4968 case LT_EXPR:
4969 case GT_EXPR:
4971 case TRUTH_ANDIF_EXPR:
4972 case TRUTH_ORIF_EXPR:
4974 case COMPOUND_EXPR:
4976 case DOTSTAR_EXPR:
4977 case MEMBER_REF:
4978 return true;
4980 default:
4981 return false;
4985 /* If the next token is a suitable fold operator, consume it and return as
4986 the function above. */
4988 static int
4989 cp_parser_fold_operator (cp_parser *parser)
4991 cp_token* token = cp_lexer_peek_token (parser->lexer);
4992 int code = cp_parser_fold_operator (token);
4993 if (code != ERROR_MARK)
4994 cp_lexer_consume_token (parser->lexer);
4995 return code;
4998 /* Parse a fold-expression.
5000 fold-expression:
5001 ( ... folding-operator cast-expression)
5002 ( cast-expression folding-operator ... )
5003 ( cast-expression folding operator ... folding-operator cast-expression)
5005 Note that the '(' and ')' are matched in primary expression. */
5007 static cp_expr
5008 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5010 cp_id_kind pidk;
5012 // Left fold.
5013 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5015 cp_lexer_consume_token (parser->lexer);
5016 int op = cp_parser_fold_operator (parser);
5017 if (op == ERROR_MARK)
5019 cp_parser_error (parser, "expected binary operator");
5020 return error_mark_node;
5023 tree expr = cp_parser_cast_expression (parser, false, false,
5024 false, &pidk);
5025 if (expr == error_mark_node)
5026 return error_mark_node;
5027 return finish_left_unary_fold_expr (expr, op);
5030 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5031 int op = cp_parser_fold_operator (parser);
5032 if (op == ERROR_MARK)
5034 cp_parser_error (parser, "expected binary operator");
5035 return error_mark_node;
5038 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5040 cp_parser_error (parser, "expected ...");
5041 return error_mark_node;
5043 cp_lexer_consume_token (parser->lexer);
5045 /* The operands of a fold-expression are cast-expressions, so binary or
5046 conditional expressions are not allowed. We check this here to avoid
5047 tentative parsing. */
5048 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5049 /* OK, the expression was parenthesized. */;
5050 else if (is_binary_op (TREE_CODE (expr1)))
5051 error_at (location_of (expr1),
5052 "binary expression in operand of fold-expression");
5053 else if (TREE_CODE (expr1) == COND_EXPR
5054 || (REFERENCE_REF_P (expr1)
5055 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5056 error_at (location_of (expr1),
5057 "conditional expression in operand of fold-expression");
5059 // Right fold.
5060 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5061 return finish_right_unary_fold_expr (expr1, op);
5063 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5065 cp_parser_error (parser, "mismatched operator in fold-expression");
5066 return error_mark_node;
5068 cp_lexer_consume_token (parser->lexer);
5070 // Binary left or right fold.
5071 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5072 if (expr2 == error_mark_node)
5073 return error_mark_node;
5074 return finish_binary_fold_expr (expr1, expr2, op);
5077 /* Parse a primary-expression.
5079 primary-expression:
5080 literal
5081 this
5082 ( expression )
5083 id-expression
5084 lambda-expression (C++11)
5086 GNU Extensions:
5088 primary-expression:
5089 ( compound-statement )
5090 __builtin_va_arg ( assignment-expression , type-id )
5091 __builtin_offsetof ( type-id , offsetof-expression )
5093 C++ Extensions:
5094 __has_nothrow_assign ( type-id )
5095 __has_nothrow_constructor ( type-id )
5096 __has_nothrow_copy ( type-id )
5097 __has_trivial_assign ( type-id )
5098 __has_trivial_constructor ( type-id )
5099 __has_trivial_copy ( type-id )
5100 __has_trivial_destructor ( type-id )
5101 __has_virtual_destructor ( type-id )
5102 __is_abstract ( type-id )
5103 __is_base_of ( type-id , type-id )
5104 __is_class ( type-id )
5105 __is_empty ( type-id )
5106 __is_enum ( type-id )
5107 __is_final ( type-id )
5108 __is_literal_type ( type-id )
5109 __is_pod ( type-id )
5110 __is_polymorphic ( type-id )
5111 __is_std_layout ( type-id )
5112 __is_trivial ( type-id )
5113 __is_union ( type-id )
5115 Objective-C++ Extension:
5117 primary-expression:
5118 objc-expression
5120 literal:
5121 __null
5123 ADDRESS_P is true iff this expression was immediately preceded by
5124 "&" and therefore might denote a pointer-to-member. CAST_P is true
5125 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5126 true iff this expression is a template argument.
5128 Returns a representation of the expression. Upon return, *IDK
5129 indicates what kind of id-expression (if any) was present. */
5131 static cp_expr
5132 cp_parser_primary_expression (cp_parser *parser,
5133 bool address_p,
5134 bool cast_p,
5135 bool template_arg_p,
5136 bool decltype_p,
5137 cp_id_kind *idk)
5139 cp_token *token = NULL;
5141 /* Assume the primary expression is not an id-expression. */
5142 *idk = CP_ID_KIND_NONE;
5144 /* Peek at the next token. */
5145 token = cp_lexer_peek_token (parser->lexer);
5146 switch ((int) token->type)
5148 /* literal:
5149 integer-literal
5150 character-literal
5151 floating-literal
5152 string-literal
5153 boolean-literal
5154 pointer-literal
5155 user-defined-literal */
5156 case CPP_CHAR:
5157 case CPP_CHAR16:
5158 case CPP_CHAR32:
5159 case CPP_WCHAR:
5160 case CPP_UTF8CHAR:
5161 case CPP_NUMBER:
5162 case CPP_PREPARSED_EXPR:
5163 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5164 return cp_parser_userdef_numeric_literal (parser);
5165 token = cp_lexer_consume_token (parser->lexer);
5166 if (TREE_CODE (token->u.value) == FIXED_CST)
5168 error_at (token->location,
5169 "fixed-point types not supported in C++");
5170 return error_mark_node;
5172 /* Floating-point literals are only allowed in an integral
5173 constant expression if they are cast to an integral or
5174 enumeration type. */
5175 if (TREE_CODE (token->u.value) == REAL_CST
5176 && parser->integral_constant_expression_p
5177 && pedantic)
5179 /* CAST_P will be set even in invalid code like "int(2.7 +
5180 ...)". Therefore, we have to check that the next token
5181 is sure to end the cast. */
5182 if (cast_p)
5184 cp_token *next_token;
5186 next_token = cp_lexer_peek_token (parser->lexer);
5187 if (/* The comma at the end of an
5188 enumerator-definition. */
5189 next_token->type != CPP_COMMA
5190 /* The curly brace at the end of an enum-specifier. */
5191 && next_token->type != CPP_CLOSE_BRACE
5192 /* The end of a statement. */
5193 && next_token->type != CPP_SEMICOLON
5194 /* The end of the cast-expression. */
5195 && next_token->type != CPP_CLOSE_PAREN
5196 /* The end of an array bound. */
5197 && next_token->type != CPP_CLOSE_SQUARE
5198 /* The closing ">" in a template-argument-list. */
5199 && (next_token->type != CPP_GREATER
5200 || parser->greater_than_is_operator_p)
5201 /* C++0x only: A ">>" treated like two ">" tokens,
5202 in a template-argument-list. */
5203 && (next_token->type != CPP_RSHIFT
5204 || (cxx_dialect == cxx98)
5205 || parser->greater_than_is_operator_p))
5206 cast_p = false;
5209 /* If we are within a cast, then the constraint that the
5210 cast is to an integral or enumeration type will be
5211 checked at that point. If we are not within a cast, then
5212 this code is invalid. */
5213 if (!cast_p)
5214 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5216 return cp_expr (token->u.value, token->location);
5218 case CPP_CHAR_USERDEF:
5219 case CPP_CHAR16_USERDEF:
5220 case CPP_CHAR32_USERDEF:
5221 case CPP_WCHAR_USERDEF:
5222 case CPP_UTF8CHAR_USERDEF:
5223 return cp_parser_userdef_char_literal (parser);
5225 case CPP_STRING:
5226 case CPP_STRING16:
5227 case CPP_STRING32:
5228 case CPP_WSTRING:
5229 case CPP_UTF8STRING:
5230 case CPP_STRING_USERDEF:
5231 case CPP_STRING16_USERDEF:
5232 case CPP_STRING32_USERDEF:
5233 case CPP_WSTRING_USERDEF:
5234 case CPP_UTF8STRING_USERDEF:
5235 /* ??? Should wide strings be allowed when parser->translate_strings_p
5236 is false (i.e. in attributes)? If not, we can kill the third
5237 argument to cp_parser_string_literal. */
5238 return cp_parser_string_literal (parser,
5239 parser->translate_strings_p,
5240 true);
5242 case CPP_OPEN_PAREN:
5243 /* If we see `( { ' then we are looking at the beginning of
5244 a GNU statement-expression. */
5245 if (cp_parser_allow_gnu_extensions_p (parser)
5246 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5248 /* Statement-expressions are not allowed by the standard. */
5249 pedwarn (token->location, OPT_Wpedantic,
5250 "ISO C++ forbids braced-groups within expressions");
5252 /* And they're not allowed outside of a function-body; you
5253 cannot, for example, write:
5255 int i = ({ int j = 3; j + 1; });
5257 at class or namespace scope. */
5258 if (!parser->in_function_body
5259 || parser->in_template_argument_list_p)
5261 error_at (token->location,
5262 "statement-expressions are not allowed outside "
5263 "functions nor in template-argument lists");
5264 cp_parser_skip_to_end_of_block_or_statement (parser);
5265 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5266 cp_lexer_consume_token (parser->lexer);
5267 return error_mark_node;
5269 else
5270 return cp_parser_statement_expr (parser);
5272 /* Otherwise it's a normal parenthesized expression. */
5274 cp_expr expr;
5275 bool saved_greater_than_is_operator_p;
5277 location_t open_paren_loc = token->location;
5279 /* Consume the `('. */
5280 matching_parens parens;
5281 parens.consume_open (parser);
5282 /* Within a parenthesized expression, a `>' token is always
5283 the greater-than operator. */
5284 saved_greater_than_is_operator_p
5285 = parser->greater_than_is_operator_p;
5286 parser->greater_than_is_operator_p = true;
5288 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5289 /* Left fold expression. */
5290 expr = NULL_TREE;
5291 else
5292 /* Parse the parenthesized expression. */
5293 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5295 token = cp_lexer_peek_token (parser->lexer);
5296 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5298 expr = cp_parser_fold_expression (parser, expr);
5299 if (expr != error_mark_node
5300 && cxx_dialect < cxx17
5301 && !in_system_header_at (input_location))
5302 pedwarn (input_location, 0, "fold-expressions only available "
5303 "with -std=c++17 or -std=gnu++17");
5305 else
5306 /* Let the front end know that this expression was
5307 enclosed in parentheses. This matters in case, for
5308 example, the expression is of the form `A::B', since
5309 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5310 not. */
5311 expr = finish_parenthesized_expr (expr);
5313 /* DR 705: Wrapping an unqualified name in parentheses
5314 suppresses arg-dependent lookup. We want to pass back
5315 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5316 (c++/37862), but none of the others. */
5317 if (*idk != CP_ID_KIND_QUALIFIED)
5318 *idk = CP_ID_KIND_NONE;
5320 /* The `>' token might be the end of a template-id or
5321 template-parameter-list now. */
5322 parser->greater_than_is_operator_p
5323 = saved_greater_than_is_operator_p;
5325 /* Consume the `)'. */
5326 token = cp_lexer_peek_token (parser->lexer);
5327 location_t close_paren_loc = token->location;
5328 expr.set_range (open_paren_loc, close_paren_loc);
5329 if (!parens.require_close (parser)
5330 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5331 cp_parser_skip_to_end_of_statement (parser);
5333 return expr;
5336 case CPP_OPEN_SQUARE:
5338 if (c_dialect_objc ())
5340 /* We might have an Objective-C++ message. */
5341 cp_parser_parse_tentatively (parser);
5342 tree msg = cp_parser_objc_message_expression (parser);
5343 /* If that works out, we're done ... */
5344 if (cp_parser_parse_definitely (parser))
5345 return msg;
5346 /* ... else, fall though to see if it's a lambda. */
5348 cp_expr lam = cp_parser_lambda_expression (parser);
5349 /* Don't warn about a failed tentative parse. */
5350 if (cp_parser_error_occurred (parser))
5351 return error_mark_node;
5352 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5353 return lam;
5356 case CPP_OBJC_STRING:
5357 if (c_dialect_objc ())
5358 /* We have an Objective-C++ string literal. */
5359 return cp_parser_objc_expression (parser);
5360 cp_parser_error (parser, "expected primary-expression");
5361 return error_mark_node;
5363 case CPP_KEYWORD:
5364 switch (token->keyword)
5366 /* These two are the boolean literals. */
5367 case RID_TRUE:
5368 cp_lexer_consume_token (parser->lexer);
5369 return cp_expr (boolean_true_node, token->location);
5370 case RID_FALSE:
5371 cp_lexer_consume_token (parser->lexer);
5372 return cp_expr (boolean_false_node, token->location);
5374 /* The `__null' literal. */
5375 case RID_NULL:
5376 cp_lexer_consume_token (parser->lexer);
5377 return cp_expr (null_node, token->location);
5379 /* The `nullptr' literal. */
5380 case RID_NULLPTR:
5381 cp_lexer_consume_token (parser->lexer);
5382 return cp_expr (nullptr_node, token->location);
5384 /* Recognize the `this' keyword. */
5385 case RID_THIS:
5386 cp_lexer_consume_token (parser->lexer);
5387 if (parser->local_variables_forbidden_p)
5389 error_at (token->location,
5390 "%<this%> may not be used in this context");
5391 return error_mark_node;
5393 /* Pointers cannot appear in constant-expressions. */
5394 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5395 return error_mark_node;
5396 return cp_expr (finish_this_expr (), token->location);
5398 /* The `operator' keyword can be the beginning of an
5399 id-expression. */
5400 case RID_OPERATOR:
5401 goto id_expression;
5403 case RID_FUNCTION_NAME:
5404 case RID_PRETTY_FUNCTION_NAME:
5405 case RID_C99_FUNCTION_NAME:
5407 non_integral_constant name;
5409 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5410 __func__ are the names of variables -- but they are
5411 treated specially. Therefore, they are handled here,
5412 rather than relying on the generic id-expression logic
5413 below. Grammatically, these names are id-expressions.
5415 Consume the token. */
5416 token = cp_lexer_consume_token (parser->lexer);
5418 switch (token->keyword)
5420 case RID_FUNCTION_NAME:
5421 name = NIC_FUNC_NAME;
5422 break;
5423 case RID_PRETTY_FUNCTION_NAME:
5424 name = NIC_PRETTY_FUNC;
5425 break;
5426 case RID_C99_FUNCTION_NAME:
5427 name = NIC_C99_FUNC;
5428 break;
5429 default:
5430 gcc_unreachable ();
5433 if (cp_parser_non_integral_constant_expression (parser, name))
5434 return error_mark_node;
5436 /* Look up the name. */
5437 return finish_fname (token->u.value);
5440 case RID_VA_ARG:
5442 tree expression;
5443 tree type;
5444 source_location type_location;
5445 location_t start_loc
5446 = cp_lexer_peek_token (parser->lexer)->location;
5447 /* The `__builtin_va_arg' construct is used to handle
5448 `va_arg'. Consume the `__builtin_va_arg' token. */
5449 cp_lexer_consume_token (parser->lexer);
5450 /* Look for the opening `('. */
5451 matching_parens parens;
5452 parens.require_open (parser);
5453 /* Now, parse the assignment-expression. */
5454 expression = cp_parser_assignment_expression (parser);
5455 /* Look for the `,'. */
5456 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5457 type_location = cp_lexer_peek_token (parser->lexer)->location;
5458 /* Parse the type-id. */
5460 type_id_in_expr_sentinel s (parser);
5461 type = cp_parser_type_id (parser);
5463 /* Look for the closing `)'. */
5464 location_t finish_loc
5465 = cp_lexer_peek_token (parser->lexer)->location;
5466 parens.require_close (parser);
5467 /* Using `va_arg' in a constant-expression is not
5468 allowed. */
5469 if (cp_parser_non_integral_constant_expression (parser,
5470 NIC_VA_ARG))
5471 return error_mark_node;
5472 /* Construct a location of the form:
5473 __builtin_va_arg (v, int)
5474 ~~~~~~~~~~~~~~~~~~~~~^~~~
5475 with the caret at the type, ranging from the start of the
5476 "__builtin_va_arg" token to the close paren. */
5477 location_t combined_loc
5478 = make_location (type_location, start_loc, finish_loc);
5479 return build_x_va_arg (combined_loc, expression, type);
5482 case RID_OFFSETOF:
5483 return cp_parser_builtin_offsetof (parser);
5485 case RID_HAS_NOTHROW_ASSIGN:
5486 case RID_HAS_NOTHROW_CONSTRUCTOR:
5487 case RID_HAS_NOTHROW_COPY:
5488 case RID_HAS_TRIVIAL_ASSIGN:
5489 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5490 case RID_HAS_TRIVIAL_COPY:
5491 case RID_HAS_TRIVIAL_DESTRUCTOR:
5492 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5493 case RID_HAS_VIRTUAL_DESTRUCTOR:
5494 case RID_IS_ABSTRACT:
5495 case RID_IS_AGGREGATE:
5496 case RID_IS_BASE_OF:
5497 case RID_IS_CLASS:
5498 case RID_IS_EMPTY:
5499 case RID_IS_ENUM:
5500 case RID_IS_FINAL:
5501 case RID_IS_LITERAL_TYPE:
5502 case RID_IS_POD:
5503 case RID_IS_POLYMORPHIC:
5504 case RID_IS_SAME_AS:
5505 case RID_IS_STD_LAYOUT:
5506 case RID_IS_TRIVIAL:
5507 case RID_IS_TRIVIALLY_ASSIGNABLE:
5508 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5509 case RID_IS_TRIVIALLY_COPYABLE:
5510 case RID_IS_UNION:
5511 case RID_IS_ASSIGNABLE:
5512 case RID_IS_CONSTRUCTIBLE:
5513 return cp_parser_trait_expr (parser, token->keyword);
5515 // C++ concepts
5516 case RID_REQUIRES:
5517 return cp_parser_requires_expression (parser);
5519 /* Objective-C++ expressions. */
5520 case RID_AT_ENCODE:
5521 case RID_AT_PROTOCOL:
5522 case RID_AT_SELECTOR:
5523 return cp_parser_objc_expression (parser);
5525 case RID_TEMPLATE:
5526 if (parser->in_function_body
5527 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5528 == CPP_LESS))
5530 error_at (token->location,
5531 "a template declaration cannot appear at block scope");
5532 cp_parser_skip_to_end_of_block_or_statement (parser);
5533 return error_mark_node;
5535 /* FALLTHRU */
5536 default:
5537 cp_parser_error (parser, "expected primary-expression");
5538 return error_mark_node;
5541 /* An id-expression can start with either an identifier, a
5542 `::' as the beginning of a qualified-id, or the "operator"
5543 keyword. */
5544 case CPP_NAME:
5545 case CPP_SCOPE:
5546 case CPP_TEMPLATE_ID:
5547 case CPP_NESTED_NAME_SPECIFIER:
5549 id_expression:
5550 cp_expr id_expression;
5551 cp_expr decl;
5552 const char *error_msg;
5553 bool template_p;
5554 bool done;
5555 cp_token *id_expr_token;
5557 /* Parse the id-expression. */
5558 id_expression
5559 = cp_parser_id_expression (parser,
5560 /*template_keyword_p=*/false,
5561 /*check_dependency_p=*/true,
5562 &template_p,
5563 /*declarator_p=*/false,
5564 /*optional_p=*/false);
5565 if (id_expression == error_mark_node)
5566 return error_mark_node;
5567 id_expr_token = token;
5568 token = cp_lexer_peek_token (parser->lexer);
5569 done = (token->type != CPP_OPEN_SQUARE
5570 && token->type != CPP_OPEN_PAREN
5571 && token->type != CPP_DOT
5572 && token->type != CPP_DEREF
5573 && token->type != CPP_PLUS_PLUS
5574 && token->type != CPP_MINUS_MINUS);
5575 /* If we have a template-id, then no further lookup is
5576 required. If the template-id was for a template-class, we
5577 will sometimes have a TYPE_DECL at this point. */
5578 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5579 || TREE_CODE (id_expression) == TYPE_DECL)
5580 decl = id_expression;
5581 /* Look up the name. */
5582 else
5584 tree ambiguous_decls;
5586 /* If we already know that this lookup is ambiguous, then
5587 we've already issued an error message; there's no reason
5588 to check again. */
5589 if (id_expr_token->type == CPP_NAME
5590 && id_expr_token->error_reported)
5592 cp_parser_simulate_error (parser);
5593 return error_mark_node;
5596 decl = cp_parser_lookup_name (parser, id_expression,
5597 none_type,
5598 template_p,
5599 /*is_namespace=*/false,
5600 /*check_dependency=*/true,
5601 &ambiguous_decls,
5602 id_expr_token->location);
5603 /* If the lookup was ambiguous, an error will already have
5604 been issued. */
5605 if (ambiguous_decls)
5606 return error_mark_node;
5608 /* In Objective-C++, we may have an Objective-C 2.0
5609 dot-syntax for classes here. */
5610 if (c_dialect_objc ()
5611 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5612 && TREE_CODE (decl) == TYPE_DECL
5613 && objc_is_class_name (decl))
5615 tree component;
5616 cp_lexer_consume_token (parser->lexer);
5617 component = cp_parser_identifier (parser);
5618 if (component == error_mark_node)
5619 return error_mark_node;
5621 tree result = objc_build_class_component_ref (id_expression,
5622 component);
5623 /* Build a location of the form:
5624 expr.component
5625 ~~~~~^~~~~~~~~
5626 with caret at the start of the component name (at
5627 input_location), ranging from the start of the id_expression
5628 to the end of the component name. */
5629 location_t combined_loc
5630 = make_location (input_location, id_expression.get_start (),
5631 get_finish (input_location));
5632 protected_set_expr_location (result, combined_loc);
5633 return result;
5636 /* In Objective-C++, an instance variable (ivar) may be preferred
5637 to whatever cp_parser_lookup_name() found.
5638 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5639 rest of c-family, we have to do a little extra work to preserve
5640 any location information in cp_expr "decl". Given that
5641 objc_lookup_ivar is implemented in "c-family" and "objc", we
5642 have a trip through the pure "tree" type, rather than cp_expr.
5643 Naively copying it back to "decl" would implicitly give the
5644 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5645 store an EXPR_LOCATION. Hence we only update "decl" (and
5646 hence its location_t) if we get back a different tree node. */
5647 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5648 id_expression);
5649 if (decl_tree != decl.get_value ())
5650 decl = cp_expr (decl_tree);
5652 /* If name lookup gives us a SCOPE_REF, then the
5653 qualifying scope was dependent. */
5654 if (TREE_CODE (decl) == SCOPE_REF)
5656 /* At this point, we do not know if DECL is a valid
5657 integral constant expression. We assume that it is
5658 in fact such an expression, so that code like:
5660 template <int N> struct A {
5661 int a[B<N>::i];
5664 is accepted. At template-instantiation time, we
5665 will check that B<N>::i is actually a constant. */
5666 return decl;
5668 /* Check to see if DECL is a local variable in a context
5669 where that is forbidden. */
5670 if (parser->local_variables_forbidden_p
5671 && local_variable_p (decl))
5673 error_at (id_expr_token->location,
5674 "local variable %qD may not appear in this context",
5675 decl.get_value ());
5676 return error_mark_node;
5680 if (processing_template_decl)
5681 if (tree fns = maybe_get_fns (decl))
5682 /* It's too difficult to mark ths in all the places where
5683 we know for sure we need to keep the lookup, so do it
5684 now. The cost is extra GC to recycle the lookups
5685 resolved at parse time. */
5686 lookup_keep (fns);
5688 decl = (finish_id_expression
5689 (id_expression, decl, parser->scope,
5690 idk,
5691 parser->integral_constant_expression_p,
5692 parser->allow_non_integral_constant_expression_p,
5693 &parser->non_integral_constant_expression_p,
5694 template_p, done, address_p,
5695 template_arg_p,
5696 &error_msg,
5697 id_expression.get_location ()));
5698 if (error_msg)
5699 cp_parser_error (parser, error_msg);
5700 decl.set_location (id_expr_token->location);
5701 return decl;
5704 /* Anything else is an error. */
5705 default:
5706 cp_parser_error (parser, "expected primary-expression");
5707 return error_mark_node;
5711 static inline cp_expr
5712 cp_parser_primary_expression (cp_parser *parser,
5713 bool address_p,
5714 bool cast_p,
5715 bool template_arg_p,
5716 cp_id_kind *idk)
5718 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5719 /*decltype*/false, idk);
5722 /* Parse an id-expression.
5724 id-expression:
5725 unqualified-id
5726 qualified-id
5728 qualified-id:
5729 :: [opt] nested-name-specifier template [opt] unqualified-id
5730 :: identifier
5731 :: operator-function-id
5732 :: template-id
5734 Return a representation of the unqualified portion of the
5735 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5736 a `::' or nested-name-specifier.
5738 Often, if the id-expression was a qualified-id, the caller will
5739 want to make a SCOPE_REF to represent the qualified-id. This
5740 function does not do this in order to avoid wastefully creating
5741 SCOPE_REFs when they are not required.
5743 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5744 `template' keyword.
5746 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5747 uninstantiated templates.
5749 If *TEMPLATE_P is non-NULL, it is set to true iff the
5750 `template' keyword is used to explicitly indicate that the entity
5751 named is a template.
5753 If DECLARATOR_P is true, the id-expression is appearing as part of
5754 a declarator, rather than as part of an expression. */
5756 static cp_expr
5757 cp_parser_id_expression (cp_parser *parser,
5758 bool template_keyword_p,
5759 bool check_dependency_p,
5760 bool *template_p,
5761 bool declarator_p,
5762 bool optional_p)
5764 bool global_scope_p;
5765 bool nested_name_specifier_p;
5767 /* Assume the `template' keyword was not used. */
5768 if (template_p)
5769 *template_p = template_keyword_p;
5771 /* Look for the optional `::' operator. */
5772 global_scope_p
5773 = (!template_keyword_p
5774 && (cp_parser_global_scope_opt (parser,
5775 /*current_scope_valid_p=*/false)
5776 != NULL_TREE));
5778 /* Look for the optional nested-name-specifier. */
5779 nested_name_specifier_p
5780 = (cp_parser_nested_name_specifier_opt (parser,
5781 /*typename_keyword_p=*/false,
5782 check_dependency_p,
5783 /*type_p=*/false,
5784 declarator_p,
5785 template_keyword_p)
5786 != NULL_TREE);
5788 /* If there is a nested-name-specifier, then we are looking at
5789 the first qualified-id production. */
5790 if (nested_name_specifier_p)
5792 tree saved_scope;
5793 tree saved_object_scope;
5794 tree saved_qualifying_scope;
5795 cp_expr unqualified_id;
5796 bool is_template;
5798 /* See if the next token is the `template' keyword. */
5799 if (!template_p)
5800 template_p = &is_template;
5801 *template_p = cp_parser_optional_template_keyword (parser);
5802 /* Name lookup we do during the processing of the
5803 unqualified-id might obliterate SCOPE. */
5804 saved_scope = parser->scope;
5805 saved_object_scope = parser->object_scope;
5806 saved_qualifying_scope = parser->qualifying_scope;
5807 /* Process the final unqualified-id. */
5808 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5809 check_dependency_p,
5810 declarator_p,
5811 /*optional_p=*/false);
5812 /* Restore the SAVED_SCOPE for our caller. */
5813 parser->scope = saved_scope;
5814 parser->object_scope = saved_object_scope;
5815 parser->qualifying_scope = saved_qualifying_scope;
5817 return unqualified_id;
5819 /* Otherwise, if we are in global scope, then we are looking at one
5820 of the other qualified-id productions. */
5821 else if (global_scope_p)
5823 cp_token *token;
5824 tree id;
5826 /* Peek at the next token. */
5827 token = cp_lexer_peek_token (parser->lexer);
5829 /* If it's an identifier, and the next token is not a "<", then
5830 we can avoid the template-id case. This is an optimization
5831 for this common case. */
5832 if (token->type == CPP_NAME
5833 && !cp_parser_nth_token_starts_template_argument_list_p
5834 (parser, 2))
5835 return cp_parser_identifier (parser);
5837 cp_parser_parse_tentatively (parser);
5838 /* Try a template-id. */
5839 id = cp_parser_template_id (parser,
5840 /*template_keyword_p=*/false,
5841 /*check_dependency_p=*/true,
5842 none_type,
5843 declarator_p);
5844 /* If that worked, we're done. */
5845 if (cp_parser_parse_definitely (parser))
5846 return id;
5848 /* Peek at the next token. (Changes in the token buffer may
5849 have invalidated the pointer obtained above.) */
5850 token = cp_lexer_peek_token (parser->lexer);
5852 switch (token->type)
5854 case CPP_NAME:
5855 return cp_parser_identifier (parser);
5857 case CPP_KEYWORD:
5858 if (token->keyword == RID_OPERATOR)
5859 return cp_parser_operator_function_id (parser);
5860 /* Fall through. */
5862 default:
5863 cp_parser_error (parser, "expected id-expression");
5864 return error_mark_node;
5867 else
5868 return cp_parser_unqualified_id (parser, template_keyword_p,
5869 /*check_dependency_p=*/true,
5870 declarator_p,
5871 optional_p);
5874 /* Parse an unqualified-id.
5876 unqualified-id:
5877 identifier
5878 operator-function-id
5879 conversion-function-id
5880 ~ class-name
5881 template-id
5883 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5884 keyword, in a construct like `A::template ...'.
5886 Returns a representation of unqualified-id. For the `identifier'
5887 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5888 production a BIT_NOT_EXPR is returned; the operand of the
5889 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5890 other productions, see the documentation accompanying the
5891 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5892 names are looked up in uninstantiated templates. If DECLARATOR_P
5893 is true, the unqualified-id is appearing as part of a declarator,
5894 rather than as part of an expression. */
5896 static cp_expr
5897 cp_parser_unqualified_id (cp_parser* parser,
5898 bool template_keyword_p,
5899 bool check_dependency_p,
5900 bool declarator_p,
5901 bool optional_p)
5903 cp_token *token;
5905 /* Peek at the next token. */
5906 token = cp_lexer_peek_token (parser->lexer);
5908 switch ((int) token->type)
5910 case CPP_NAME:
5912 tree id;
5914 /* We don't know yet whether or not this will be a
5915 template-id. */
5916 cp_parser_parse_tentatively (parser);
5917 /* Try a template-id. */
5918 id = cp_parser_template_id (parser, template_keyword_p,
5919 check_dependency_p,
5920 none_type,
5921 declarator_p);
5922 /* If it worked, we're done. */
5923 if (cp_parser_parse_definitely (parser))
5924 return id;
5925 /* Otherwise, it's an ordinary identifier. */
5926 return cp_parser_identifier (parser);
5929 case CPP_TEMPLATE_ID:
5930 return cp_parser_template_id (parser, template_keyword_p,
5931 check_dependency_p,
5932 none_type,
5933 declarator_p);
5935 case CPP_COMPL:
5937 tree type_decl;
5938 tree qualifying_scope;
5939 tree object_scope;
5940 tree scope;
5941 bool done;
5943 /* Consume the `~' token. */
5944 cp_lexer_consume_token (parser->lexer);
5945 /* Parse the class-name. The standard, as written, seems to
5946 say that:
5948 template <typename T> struct S { ~S (); };
5949 template <typename T> S<T>::~S() {}
5951 is invalid, since `~' must be followed by a class-name, but
5952 `S<T>' is dependent, and so not known to be a class.
5953 That's not right; we need to look in uninstantiated
5954 templates. A further complication arises from:
5956 template <typename T> void f(T t) {
5957 t.T::~T();
5960 Here, it is not possible to look up `T' in the scope of `T'
5961 itself. We must look in both the current scope, and the
5962 scope of the containing complete expression.
5964 Yet another issue is:
5966 struct S {
5967 int S;
5968 ~S();
5971 S::~S() {}
5973 The standard does not seem to say that the `S' in `~S'
5974 should refer to the type `S' and not the data member
5975 `S::S'. */
5977 /* DR 244 says that we look up the name after the "~" in the
5978 same scope as we looked up the qualifying name. That idea
5979 isn't fully worked out; it's more complicated than that. */
5980 scope = parser->scope;
5981 object_scope = parser->object_scope;
5982 qualifying_scope = parser->qualifying_scope;
5984 /* Check for invalid scopes. */
5985 if (scope == error_mark_node)
5987 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5988 cp_lexer_consume_token (parser->lexer);
5989 return error_mark_node;
5991 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5993 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5994 error_at (token->location,
5995 "scope %qT before %<~%> is not a class-name",
5996 scope);
5997 cp_parser_simulate_error (parser);
5998 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5999 cp_lexer_consume_token (parser->lexer);
6000 return error_mark_node;
6002 gcc_assert (!scope || TYPE_P (scope));
6004 /* If the name is of the form "X::~X" it's OK even if X is a
6005 typedef. */
6006 token = cp_lexer_peek_token (parser->lexer);
6007 if (scope
6008 && token->type == CPP_NAME
6009 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6010 != CPP_LESS)
6011 && (token->u.value == TYPE_IDENTIFIER (scope)
6012 || (CLASS_TYPE_P (scope)
6013 && constructor_name_p (token->u.value, scope))))
6015 cp_lexer_consume_token (parser->lexer);
6016 return build_nt (BIT_NOT_EXPR, scope);
6019 /* ~auto means the destructor of whatever the object is. */
6020 if (cp_parser_is_keyword (token, RID_AUTO))
6022 if (cxx_dialect < cxx14)
6023 pedwarn (input_location, 0,
6024 "%<~auto%> only available with "
6025 "-std=c++14 or -std=gnu++14");
6026 cp_lexer_consume_token (parser->lexer);
6027 return build_nt (BIT_NOT_EXPR, make_auto ());
6030 /* If there was an explicit qualification (S::~T), first look
6031 in the scope given by the qualification (i.e., S).
6033 Note: in the calls to cp_parser_class_name below we pass
6034 typename_type so that lookup finds the injected-class-name
6035 rather than the constructor. */
6036 done = false;
6037 type_decl = NULL_TREE;
6038 if (scope)
6040 cp_parser_parse_tentatively (parser);
6041 type_decl = cp_parser_class_name (parser,
6042 /*typename_keyword_p=*/false,
6043 /*template_keyword_p=*/false,
6044 typename_type,
6045 /*check_dependency=*/false,
6046 /*class_head_p=*/false,
6047 declarator_p);
6048 if (cp_parser_parse_definitely (parser))
6049 done = true;
6051 /* In "N::S::~S", look in "N" as well. */
6052 if (!done && scope && qualifying_scope)
6054 cp_parser_parse_tentatively (parser);
6055 parser->scope = qualifying_scope;
6056 parser->object_scope = NULL_TREE;
6057 parser->qualifying_scope = NULL_TREE;
6058 type_decl
6059 = cp_parser_class_name (parser,
6060 /*typename_keyword_p=*/false,
6061 /*template_keyword_p=*/false,
6062 typename_type,
6063 /*check_dependency=*/false,
6064 /*class_head_p=*/false,
6065 declarator_p);
6066 if (cp_parser_parse_definitely (parser))
6067 done = true;
6069 /* In "p->S::~T", look in the scope given by "*p" as well. */
6070 else if (!done && object_scope)
6072 cp_parser_parse_tentatively (parser);
6073 parser->scope = object_scope;
6074 parser->object_scope = NULL_TREE;
6075 parser->qualifying_scope = NULL_TREE;
6076 type_decl
6077 = cp_parser_class_name (parser,
6078 /*typename_keyword_p=*/false,
6079 /*template_keyword_p=*/false,
6080 typename_type,
6081 /*check_dependency=*/false,
6082 /*class_head_p=*/false,
6083 declarator_p);
6084 if (cp_parser_parse_definitely (parser))
6085 done = true;
6087 /* Look in the surrounding context. */
6088 if (!done)
6090 parser->scope = NULL_TREE;
6091 parser->object_scope = NULL_TREE;
6092 parser->qualifying_scope = NULL_TREE;
6093 if (processing_template_decl)
6094 cp_parser_parse_tentatively (parser);
6095 type_decl
6096 = cp_parser_class_name (parser,
6097 /*typename_keyword_p=*/false,
6098 /*template_keyword_p=*/false,
6099 typename_type,
6100 /*check_dependency=*/false,
6101 /*class_head_p=*/false,
6102 declarator_p);
6103 if (processing_template_decl
6104 && ! cp_parser_parse_definitely (parser))
6106 /* We couldn't find a type with this name. If we're parsing
6107 tentatively, fail and try something else. */
6108 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6110 cp_parser_simulate_error (parser);
6111 return error_mark_node;
6113 /* Otherwise, accept it and check for a match at instantiation
6114 time. */
6115 type_decl = cp_parser_identifier (parser);
6116 if (type_decl != error_mark_node)
6117 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6118 return type_decl;
6121 /* If an error occurred, assume that the name of the
6122 destructor is the same as the name of the qualifying
6123 class. That allows us to keep parsing after running
6124 into ill-formed destructor names. */
6125 if (type_decl == error_mark_node && scope)
6126 return build_nt (BIT_NOT_EXPR, scope);
6127 else if (type_decl == error_mark_node)
6128 return error_mark_node;
6130 /* Check that destructor name and scope match. */
6131 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6133 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6134 error_at (token->location,
6135 "declaration of %<~%T%> as member of %qT",
6136 type_decl, scope);
6137 cp_parser_simulate_error (parser);
6138 return error_mark_node;
6141 /* [class.dtor]
6143 A typedef-name that names a class shall not be used as the
6144 identifier in the declarator for a destructor declaration. */
6145 if (declarator_p
6146 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6147 && !DECL_SELF_REFERENCE_P (type_decl)
6148 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6149 error_at (token->location,
6150 "typedef-name %qD used as destructor declarator",
6151 type_decl);
6153 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6156 case CPP_KEYWORD:
6157 if (token->keyword == RID_OPERATOR)
6159 cp_expr id;
6161 /* This could be a template-id, so we try that first. */
6162 cp_parser_parse_tentatively (parser);
6163 /* Try a template-id. */
6164 id = cp_parser_template_id (parser, template_keyword_p,
6165 /*check_dependency_p=*/true,
6166 none_type,
6167 declarator_p);
6168 /* If that worked, we're done. */
6169 if (cp_parser_parse_definitely (parser))
6170 return id;
6171 /* We still don't know whether we're looking at an
6172 operator-function-id or a conversion-function-id. */
6173 cp_parser_parse_tentatively (parser);
6174 /* Try an operator-function-id. */
6175 id = cp_parser_operator_function_id (parser);
6176 /* If that didn't work, try a conversion-function-id. */
6177 if (!cp_parser_parse_definitely (parser))
6178 id = cp_parser_conversion_function_id (parser);
6180 return id;
6182 /* Fall through. */
6184 default:
6185 if (optional_p)
6186 return NULL_TREE;
6187 cp_parser_error (parser, "expected unqualified-id");
6188 return error_mark_node;
6192 /* Parse an (optional) nested-name-specifier.
6194 nested-name-specifier: [C++98]
6195 class-or-namespace-name :: nested-name-specifier [opt]
6196 class-or-namespace-name :: template nested-name-specifier [opt]
6198 nested-name-specifier: [C++0x]
6199 type-name ::
6200 namespace-name ::
6201 nested-name-specifier identifier ::
6202 nested-name-specifier template [opt] simple-template-id ::
6204 PARSER->SCOPE should be set appropriately before this function is
6205 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6206 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6207 in name lookups.
6209 Sets PARSER->SCOPE to the class (TYPE) or namespace
6210 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6211 it unchanged if there is no nested-name-specifier. Returns the new
6212 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6214 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6215 part of a declaration and/or decl-specifier. */
6217 static tree
6218 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6219 bool typename_keyword_p,
6220 bool check_dependency_p,
6221 bool type_p,
6222 bool is_declaration,
6223 bool template_keyword_p /* = false */)
6225 bool success = false;
6226 cp_token_position start = 0;
6227 cp_token *token;
6229 /* Remember where the nested-name-specifier starts. */
6230 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6232 start = cp_lexer_token_position (parser->lexer, false);
6233 push_deferring_access_checks (dk_deferred);
6236 while (true)
6238 tree new_scope;
6239 tree old_scope;
6240 tree saved_qualifying_scope;
6242 /* Spot cases that cannot be the beginning of a
6243 nested-name-specifier. */
6244 token = cp_lexer_peek_token (parser->lexer);
6246 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6247 the already parsed nested-name-specifier. */
6248 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6250 /* Grab the nested-name-specifier and continue the loop. */
6251 cp_parser_pre_parsed_nested_name_specifier (parser);
6252 /* If we originally encountered this nested-name-specifier
6253 with IS_DECLARATION set to false, we will not have
6254 resolved TYPENAME_TYPEs, so we must do so here. */
6255 if (is_declaration
6256 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6258 new_scope = resolve_typename_type (parser->scope,
6259 /*only_current_p=*/false);
6260 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6261 parser->scope = new_scope;
6263 success = true;
6264 continue;
6267 /* Spot cases that cannot be the beginning of a
6268 nested-name-specifier. On the second and subsequent times
6269 through the loop, we look for the `template' keyword. */
6270 if (success && token->keyword == RID_TEMPLATE)
6272 /* A template-id can start a nested-name-specifier. */
6273 else if (token->type == CPP_TEMPLATE_ID)
6275 /* DR 743: decltype can be used in a nested-name-specifier. */
6276 else if (token_is_decltype (token))
6278 else
6280 /* If the next token is not an identifier, then it is
6281 definitely not a type-name or namespace-name. */
6282 if (token->type != CPP_NAME)
6283 break;
6284 /* If the following token is neither a `<' (to begin a
6285 template-id), nor a `::', then we are not looking at a
6286 nested-name-specifier. */
6287 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6289 if (token->type == CPP_COLON
6290 && parser->colon_corrects_to_scope_p
6291 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6293 gcc_rich_location richloc (token->location);
6294 richloc.add_fixit_replace ("::");
6295 error_at (&richloc,
6296 "found %<:%> in nested-name-specifier, "
6297 "expected %<::%>");
6298 token->type = CPP_SCOPE;
6301 if (token->type != CPP_SCOPE
6302 && !cp_parser_nth_token_starts_template_argument_list_p
6303 (parser, 2))
6304 break;
6307 /* The nested-name-specifier is optional, so we parse
6308 tentatively. */
6309 cp_parser_parse_tentatively (parser);
6311 /* Look for the optional `template' keyword, if this isn't the
6312 first time through the loop. */
6313 if (success)
6314 template_keyword_p = cp_parser_optional_template_keyword (parser);
6316 /* Save the old scope since the name lookup we are about to do
6317 might destroy it. */
6318 old_scope = parser->scope;
6319 saved_qualifying_scope = parser->qualifying_scope;
6320 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6321 look up names in "X<T>::I" in order to determine that "Y" is
6322 a template. So, if we have a typename at this point, we make
6323 an effort to look through it. */
6324 if (is_declaration
6325 && !typename_keyword_p
6326 && parser->scope
6327 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6328 parser->scope = resolve_typename_type (parser->scope,
6329 /*only_current_p=*/false);
6330 /* Parse the qualifying entity. */
6331 new_scope
6332 = cp_parser_qualifying_entity (parser,
6333 typename_keyword_p,
6334 template_keyword_p,
6335 check_dependency_p,
6336 type_p,
6337 is_declaration);
6338 /* Look for the `::' token. */
6339 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6341 /* If we found what we wanted, we keep going; otherwise, we're
6342 done. */
6343 if (!cp_parser_parse_definitely (parser))
6345 bool error_p = false;
6347 /* Restore the OLD_SCOPE since it was valid before the
6348 failed attempt at finding the last
6349 class-or-namespace-name. */
6350 parser->scope = old_scope;
6351 parser->qualifying_scope = saved_qualifying_scope;
6353 /* If the next token is a decltype, and the one after that is a
6354 `::', then the decltype has failed to resolve to a class or
6355 enumeration type. Give this error even when parsing
6356 tentatively since it can't possibly be valid--and we're going
6357 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6358 won't get another chance.*/
6359 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6360 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6361 == CPP_SCOPE))
6363 token = cp_lexer_consume_token (parser->lexer);
6364 error_at (token->location, "decltype evaluates to %qT, "
6365 "which is not a class or enumeration type",
6366 token->u.tree_check_value->value);
6367 parser->scope = error_mark_node;
6368 error_p = true;
6369 /* As below. */
6370 success = true;
6371 cp_lexer_consume_token (parser->lexer);
6374 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6375 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6377 /* If we have a non-type template-id followed by ::, it can't
6378 possibly be valid. */
6379 token = cp_lexer_peek_token (parser->lexer);
6380 tree tid = token->u.tree_check_value->value;
6381 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6382 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6384 tree tmpl = NULL_TREE;
6385 if (is_overloaded_fn (tid))
6387 tree fns = get_fns (tid);
6388 if (OVL_SINGLE_P (fns))
6389 tmpl = OVL_FIRST (fns);
6390 error_at (token->location, "function template-id %qD "
6391 "in nested-name-specifier", tid);
6393 else
6395 /* Variable template. */
6396 tmpl = TREE_OPERAND (tid, 0);
6397 gcc_assert (variable_template_p (tmpl));
6398 error_at (token->location, "variable template-id %qD "
6399 "in nested-name-specifier", tid);
6401 if (tmpl)
6402 inform (DECL_SOURCE_LOCATION (tmpl),
6403 "%qD declared here", tmpl);
6405 parser->scope = error_mark_node;
6406 error_p = true;
6407 /* As below. */
6408 success = true;
6409 cp_lexer_consume_token (parser->lexer);
6410 cp_lexer_consume_token (parser->lexer);
6414 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6415 break;
6416 /* If the next token is an identifier, and the one after
6417 that is a `::', then any valid interpretation would have
6418 found a class-or-namespace-name. */
6419 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6420 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6421 == CPP_SCOPE)
6422 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6423 != CPP_COMPL))
6425 token = cp_lexer_consume_token (parser->lexer);
6426 if (!error_p)
6428 if (!token->error_reported)
6430 tree decl;
6431 tree ambiguous_decls;
6433 decl = cp_parser_lookup_name (parser, token->u.value,
6434 none_type,
6435 /*is_template=*/false,
6436 /*is_namespace=*/false,
6437 /*check_dependency=*/true,
6438 &ambiguous_decls,
6439 token->location);
6440 if (TREE_CODE (decl) == TEMPLATE_DECL)
6441 error_at (token->location,
6442 "%qD used without template arguments",
6443 decl);
6444 else if (ambiguous_decls)
6446 // cp_parser_lookup_name has the same diagnostic,
6447 // thus make sure to emit it at most once.
6448 if (cp_parser_uncommitted_to_tentative_parse_p
6449 (parser))
6451 error_at (token->location,
6452 "reference to %qD is ambiguous",
6453 token->u.value);
6454 print_candidates (ambiguous_decls);
6456 decl = error_mark_node;
6458 else
6460 if (cxx_dialect != cxx98)
6461 cp_parser_name_lookup_error
6462 (parser, token->u.value, decl, NLE_NOT_CXX98,
6463 token->location);
6464 else
6465 cp_parser_name_lookup_error
6466 (parser, token->u.value, decl, NLE_CXX98,
6467 token->location);
6470 parser->scope = error_mark_node;
6471 error_p = true;
6472 /* Treat this as a successful nested-name-specifier
6473 due to:
6475 [basic.lookup.qual]
6477 If the name found is not a class-name (clause
6478 _class_) or namespace-name (_namespace.def_), the
6479 program is ill-formed. */
6480 success = true;
6482 cp_lexer_consume_token (parser->lexer);
6484 break;
6486 /* We've found one valid nested-name-specifier. */
6487 success = true;
6488 /* Name lookup always gives us a DECL. */
6489 if (TREE_CODE (new_scope) == TYPE_DECL)
6490 new_scope = TREE_TYPE (new_scope);
6491 /* Uses of "template" must be followed by actual templates. */
6492 if (template_keyword_p
6493 && !(CLASS_TYPE_P (new_scope)
6494 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6495 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6496 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6497 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6498 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6499 == TEMPLATE_ID_EXPR)))
6500 permerror (input_location, TYPE_P (new_scope)
6501 ? G_("%qT is not a template")
6502 : G_("%qD is not a template"),
6503 new_scope);
6504 /* If it is a class scope, try to complete it; we are about to
6505 be looking up names inside the class. */
6506 if (TYPE_P (new_scope)
6507 /* Since checking types for dependency can be expensive,
6508 avoid doing it if the type is already complete. */
6509 && !COMPLETE_TYPE_P (new_scope)
6510 /* Do not try to complete dependent types. */
6511 && !dependent_type_p (new_scope))
6513 new_scope = complete_type (new_scope);
6514 /* If it is a typedef to current class, use the current
6515 class instead, as the typedef won't have any names inside
6516 it yet. */
6517 if (!COMPLETE_TYPE_P (new_scope)
6518 && currently_open_class (new_scope))
6519 new_scope = TYPE_MAIN_VARIANT (new_scope);
6521 /* Make sure we look in the right scope the next time through
6522 the loop. */
6523 parser->scope = new_scope;
6526 /* If parsing tentatively, replace the sequence of tokens that makes
6527 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6528 token. That way, should we re-parse the token stream, we will
6529 not have to repeat the effort required to do the parse, nor will
6530 we issue duplicate error messages. */
6531 if (success && start)
6533 cp_token *token;
6535 token = cp_lexer_token_at (parser->lexer, start);
6536 /* Reset the contents of the START token. */
6537 token->type = CPP_NESTED_NAME_SPECIFIER;
6538 /* Retrieve any deferred checks. Do not pop this access checks yet
6539 so the memory will not be reclaimed during token replacing below. */
6540 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6541 token->u.tree_check_value->value = parser->scope;
6542 token->u.tree_check_value->checks = get_deferred_access_checks ();
6543 token->u.tree_check_value->qualifying_scope =
6544 parser->qualifying_scope;
6545 token->keyword = RID_MAX;
6547 /* Purge all subsequent tokens. */
6548 cp_lexer_purge_tokens_after (parser->lexer, start);
6551 if (start)
6552 pop_to_parent_deferring_access_checks ();
6554 return success ? parser->scope : NULL_TREE;
6557 /* Parse a nested-name-specifier. See
6558 cp_parser_nested_name_specifier_opt for details. This function
6559 behaves identically, except that it will an issue an error if no
6560 nested-name-specifier is present. */
6562 static tree
6563 cp_parser_nested_name_specifier (cp_parser *parser,
6564 bool typename_keyword_p,
6565 bool check_dependency_p,
6566 bool type_p,
6567 bool is_declaration)
6569 tree scope;
6571 /* Look for the nested-name-specifier. */
6572 scope = cp_parser_nested_name_specifier_opt (parser,
6573 typename_keyword_p,
6574 check_dependency_p,
6575 type_p,
6576 is_declaration);
6577 /* If it was not present, issue an error message. */
6578 if (!scope)
6580 cp_parser_error (parser, "expected nested-name-specifier");
6581 parser->scope = NULL_TREE;
6584 return scope;
6587 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6588 this is either a class-name or a namespace-name (which corresponds
6589 to the class-or-namespace-name production in the grammar). For
6590 C++0x, it can also be a type-name that refers to an enumeration
6591 type or a simple-template-id.
6593 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6594 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6595 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6596 TYPE_P is TRUE iff the next name should be taken as a class-name,
6597 even the same name is declared to be another entity in the same
6598 scope.
6600 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6601 specified by the class-or-namespace-name. If neither is found the
6602 ERROR_MARK_NODE is returned. */
6604 static tree
6605 cp_parser_qualifying_entity (cp_parser *parser,
6606 bool typename_keyword_p,
6607 bool template_keyword_p,
6608 bool check_dependency_p,
6609 bool type_p,
6610 bool is_declaration)
6612 tree saved_scope;
6613 tree saved_qualifying_scope;
6614 tree saved_object_scope;
6615 tree scope;
6616 bool only_class_p;
6617 bool successful_parse_p;
6619 /* DR 743: decltype can appear in a nested-name-specifier. */
6620 if (cp_lexer_next_token_is_decltype (parser->lexer))
6622 scope = cp_parser_decltype (parser);
6623 if (TREE_CODE (scope) != ENUMERAL_TYPE
6624 && !MAYBE_CLASS_TYPE_P (scope))
6626 cp_parser_simulate_error (parser);
6627 return error_mark_node;
6629 if (TYPE_NAME (scope))
6630 scope = TYPE_NAME (scope);
6631 return scope;
6634 /* Before we try to parse the class-name, we must save away the
6635 current PARSER->SCOPE since cp_parser_class_name will destroy
6636 it. */
6637 saved_scope = parser->scope;
6638 saved_qualifying_scope = parser->qualifying_scope;
6639 saved_object_scope = parser->object_scope;
6640 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6641 there is no need to look for a namespace-name. */
6642 only_class_p = template_keyword_p
6643 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6644 if (!only_class_p)
6645 cp_parser_parse_tentatively (parser);
6646 scope = cp_parser_class_name (parser,
6647 typename_keyword_p,
6648 template_keyword_p,
6649 type_p ? class_type : none_type,
6650 check_dependency_p,
6651 /*class_head_p=*/false,
6652 is_declaration,
6653 /*enum_ok=*/cxx_dialect > cxx98);
6654 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6655 /* If that didn't work, try for a namespace-name. */
6656 if (!only_class_p && !successful_parse_p)
6658 /* Restore the saved scope. */
6659 parser->scope = saved_scope;
6660 parser->qualifying_scope = saved_qualifying_scope;
6661 parser->object_scope = saved_object_scope;
6662 /* If we are not looking at an identifier followed by the scope
6663 resolution operator, then this is not part of a
6664 nested-name-specifier. (Note that this function is only used
6665 to parse the components of a nested-name-specifier.) */
6666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6667 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6668 return error_mark_node;
6669 scope = cp_parser_namespace_name (parser);
6672 return scope;
6675 /* Return true if we are looking at a compound-literal, false otherwise. */
6677 static bool
6678 cp_parser_compound_literal_p (cp_parser *parser)
6680 cp_lexer_save_tokens (parser->lexer);
6682 /* Skip tokens until the next token is a closing parenthesis.
6683 If we find the closing `)', and the next token is a `{', then
6684 we are looking at a compound-literal. */
6685 bool compound_literal_p
6686 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6687 /*consume_paren=*/true)
6688 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6690 /* Roll back the tokens we skipped. */
6691 cp_lexer_rollback_tokens (parser->lexer);
6693 return compound_literal_p;
6696 /* Return true if EXPR is the integer constant zero or a complex constant
6697 of zero, without any folding, but ignoring location wrappers. */
6699 bool
6700 literal_integer_zerop (const_tree expr)
6702 return (location_wrapper_p (expr)
6703 && integer_zerop (TREE_OPERAND (expr, 0)));
6706 /* Parse a postfix-expression.
6708 postfix-expression:
6709 primary-expression
6710 postfix-expression [ expression ]
6711 postfix-expression ( expression-list [opt] )
6712 simple-type-specifier ( expression-list [opt] )
6713 typename :: [opt] nested-name-specifier identifier
6714 ( expression-list [opt] )
6715 typename :: [opt] nested-name-specifier template [opt] template-id
6716 ( expression-list [opt] )
6717 postfix-expression . template [opt] id-expression
6718 postfix-expression -> template [opt] id-expression
6719 postfix-expression . pseudo-destructor-name
6720 postfix-expression -> pseudo-destructor-name
6721 postfix-expression ++
6722 postfix-expression --
6723 dynamic_cast < type-id > ( expression )
6724 static_cast < type-id > ( expression )
6725 reinterpret_cast < type-id > ( expression )
6726 const_cast < type-id > ( expression )
6727 typeid ( expression )
6728 typeid ( type-id )
6730 GNU Extension:
6732 postfix-expression:
6733 ( type-id ) { initializer-list , [opt] }
6735 This extension is a GNU version of the C99 compound-literal
6736 construct. (The C99 grammar uses `type-name' instead of `type-id',
6737 but they are essentially the same concept.)
6739 If ADDRESS_P is true, the postfix expression is the operand of the
6740 `&' operator. CAST_P is true if this expression is the target of a
6741 cast.
6743 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6744 class member access expressions [expr.ref].
6746 Returns a representation of the expression. */
6748 static cp_expr
6749 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6750 bool member_access_only_p, bool decltype_p,
6751 cp_id_kind * pidk_return)
6753 cp_token *token;
6754 location_t loc;
6755 enum rid keyword;
6756 cp_id_kind idk = CP_ID_KIND_NONE;
6757 cp_expr postfix_expression = NULL_TREE;
6758 bool is_member_access = false;
6760 /* Peek at the next token. */
6761 token = cp_lexer_peek_token (parser->lexer);
6762 loc = token->location;
6763 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6765 /* Some of the productions are determined by keywords. */
6766 keyword = token->keyword;
6767 switch (keyword)
6769 case RID_DYNCAST:
6770 case RID_STATCAST:
6771 case RID_REINTCAST:
6772 case RID_CONSTCAST:
6774 tree type;
6775 cp_expr expression;
6776 const char *saved_message;
6777 bool saved_in_type_id_in_expr_p;
6779 /* All of these can be handled in the same way from the point
6780 of view of parsing. Begin by consuming the token
6781 identifying the cast. */
6782 cp_lexer_consume_token (parser->lexer);
6784 /* New types cannot be defined in the cast. */
6785 saved_message = parser->type_definition_forbidden_message;
6786 parser->type_definition_forbidden_message
6787 = G_("types may not be defined in casts");
6789 /* Look for the opening `<'. */
6790 cp_parser_require (parser, CPP_LESS, RT_LESS);
6791 /* Parse the type to which we are casting. */
6792 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6793 parser->in_type_id_in_expr_p = true;
6794 type = cp_parser_type_id (parser);
6795 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6796 /* Look for the closing `>'. */
6797 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6798 /* Restore the old message. */
6799 parser->type_definition_forbidden_message = saved_message;
6801 bool saved_greater_than_is_operator_p
6802 = parser->greater_than_is_operator_p;
6803 parser->greater_than_is_operator_p = true;
6805 /* And the expression which is being cast. */
6806 matching_parens parens;
6807 parens.require_open (parser);
6808 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6809 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6810 RT_CLOSE_PAREN);
6811 location_t end_loc = close_paren ?
6812 close_paren->location : UNKNOWN_LOCATION;
6814 parser->greater_than_is_operator_p
6815 = saved_greater_than_is_operator_p;
6817 /* Only type conversions to integral or enumeration types
6818 can be used in constant-expressions. */
6819 if (!cast_valid_in_integral_constant_expression_p (type)
6820 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6822 postfix_expression = error_mark_node;
6823 break;
6826 switch (keyword)
6828 case RID_DYNCAST:
6829 postfix_expression
6830 = build_dynamic_cast (type, expression, tf_warning_or_error);
6831 break;
6832 case RID_STATCAST:
6833 postfix_expression
6834 = build_static_cast (type, expression, tf_warning_or_error);
6835 break;
6836 case RID_REINTCAST:
6837 postfix_expression
6838 = build_reinterpret_cast (type, expression,
6839 tf_warning_or_error);
6840 break;
6841 case RID_CONSTCAST:
6842 postfix_expression
6843 = build_const_cast (type, expression, tf_warning_or_error);
6844 break;
6845 default:
6846 gcc_unreachable ();
6849 /* Construct a location e.g. :
6850 reinterpret_cast <int *> (expr)
6851 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6852 ranging from the start of the "*_cast" token to the final closing
6853 paren, with the caret at the start. */
6854 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6855 postfix_expression.set_location (cp_cast_loc);
6857 break;
6859 case RID_TYPEID:
6861 tree type;
6862 const char *saved_message;
6863 bool saved_in_type_id_in_expr_p;
6865 /* Consume the `typeid' token. */
6866 cp_lexer_consume_token (parser->lexer);
6867 /* Look for the `(' token. */
6868 matching_parens parens;
6869 parens.require_open (parser);
6870 /* Types cannot be defined in a `typeid' expression. */
6871 saved_message = parser->type_definition_forbidden_message;
6872 parser->type_definition_forbidden_message
6873 = G_("types may not be defined in a %<typeid%> expression");
6874 /* We can't be sure yet whether we're looking at a type-id or an
6875 expression. */
6876 cp_parser_parse_tentatively (parser);
6877 /* Try a type-id first. */
6878 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6879 parser->in_type_id_in_expr_p = true;
6880 type = cp_parser_type_id (parser);
6881 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6882 /* Look for the `)' token. Otherwise, we can't be sure that
6883 we're not looking at an expression: consider `typeid (int
6884 (3))', for example. */
6885 cp_token *close_paren = parens.require_close (parser);
6886 /* If all went well, simply lookup the type-id. */
6887 if (cp_parser_parse_definitely (parser))
6888 postfix_expression = get_typeid (type, tf_warning_or_error);
6889 /* Otherwise, fall back to the expression variant. */
6890 else
6892 tree expression;
6894 /* Look for an expression. */
6895 expression = cp_parser_expression (parser, & idk);
6896 /* Compute its typeid. */
6897 postfix_expression = build_typeid (expression, tf_warning_or_error);
6898 /* Look for the `)' token. */
6899 close_paren = parens.require_close (parser);
6901 /* Restore the saved message. */
6902 parser->type_definition_forbidden_message = saved_message;
6903 /* `typeid' may not appear in an integral constant expression. */
6904 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6905 postfix_expression = error_mark_node;
6907 /* Construct a location e.g. :
6908 typeid (expr)
6909 ^~~~~~~~~~~~~
6910 ranging from the start of the "typeid" token to the final closing
6911 paren, with the caret at the start. */
6912 if (close_paren)
6914 location_t typeid_loc
6915 = make_location (start_loc, start_loc, close_paren->location);
6916 postfix_expression.set_location (typeid_loc);
6917 postfix_expression.maybe_add_location_wrapper ();
6920 break;
6922 case RID_TYPENAME:
6924 tree type;
6925 /* The syntax permitted here is the same permitted for an
6926 elaborated-type-specifier. */
6927 ++parser->prevent_constrained_type_specifiers;
6928 type = cp_parser_elaborated_type_specifier (parser,
6929 /*is_friend=*/false,
6930 /*is_declaration=*/false);
6931 --parser->prevent_constrained_type_specifiers;
6932 postfix_expression = cp_parser_functional_cast (parser, type);
6934 break;
6936 case RID_ADDRESSOF:
6937 case RID_BUILTIN_SHUFFLE:
6938 case RID_BUILTIN_LAUNDER:
6940 vec<tree, va_gc> *vec;
6941 unsigned int i;
6942 tree p;
6944 cp_lexer_consume_token (parser->lexer);
6945 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6946 /*cast_p=*/false, /*allow_expansion_p=*/true,
6947 /*non_constant_p=*/NULL);
6948 if (vec == NULL)
6950 postfix_expression = error_mark_node;
6951 break;
6954 FOR_EACH_VEC_ELT (*vec, i, p)
6955 mark_exp_read (p);
6957 switch (keyword)
6959 case RID_ADDRESSOF:
6960 if (vec->length () == 1)
6961 postfix_expression
6962 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6963 else
6965 error_at (loc, "wrong number of arguments to "
6966 "%<__builtin_addressof%>");
6967 postfix_expression = error_mark_node;
6969 break;
6971 case RID_BUILTIN_LAUNDER:
6972 if (vec->length () == 1)
6973 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6974 tf_warning_or_error);
6975 else
6977 error_at (loc, "wrong number of arguments to "
6978 "%<__builtin_launder%>");
6979 postfix_expression = error_mark_node;
6981 break;
6983 case RID_BUILTIN_SHUFFLE:
6984 if (vec->length () == 2)
6985 postfix_expression
6986 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6987 (*vec)[1], tf_warning_or_error);
6988 else if (vec->length () == 3)
6989 postfix_expression
6990 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6991 (*vec)[2], tf_warning_or_error);
6992 else
6994 error_at (loc, "wrong number of arguments to "
6995 "%<__builtin_shuffle%>");
6996 postfix_expression = error_mark_node;
6998 break;
7000 default:
7001 gcc_unreachable ();
7003 break;
7006 default:
7008 tree type;
7010 /* If the next thing is a simple-type-specifier, we may be
7011 looking at a functional cast. We could also be looking at
7012 an id-expression. So, we try the functional cast, and if
7013 that doesn't work we fall back to the primary-expression. */
7014 cp_parser_parse_tentatively (parser);
7015 /* Look for the simple-type-specifier. */
7016 ++parser->prevent_constrained_type_specifiers;
7017 type = cp_parser_simple_type_specifier (parser,
7018 /*decl_specs=*/NULL,
7019 CP_PARSER_FLAGS_NONE);
7020 --parser->prevent_constrained_type_specifiers;
7021 /* Parse the cast itself. */
7022 if (!cp_parser_error_occurred (parser))
7023 postfix_expression
7024 = cp_parser_functional_cast (parser, type);
7025 /* If that worked, we're done. */
7026 if (cp_parser_parse_definitely (parser))
7027 break;
7029 /* If the functional-cast didn't work out, try a
7030 compound-literal. */
7031 if (cp_parser_allow_gnu_extensions_p (parser)
7032 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7034 cp_expr initializer = NULL_TREE;
7036 cp_parser_parse_tentatively (parser);
7038 matching_parens parens;
7039 parens.consume_open (parser);
7041 /* Avoid calling cp_parser_type_id pointlessly, see comment
7042 in cp_parser_cast_expression about c++/29234. */
7043 if (!cp_parser_compound_literal_p (parser))
7044 cp_parser_simulate_error (parser);
7045 else
7047 /* Parse the type. */
7048 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7049 parser->in_type_id_in_expr_p = true;
7050 type = cp_parser_type_id (parser);
7051 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7052 parens.require_close (parser);
7055 /* If things aren't going well, there's no need to
7056 keep going. */
7057 if (!cp_parser_error_occurred (parser))
7059 bool non_constant_p;
7060 /* Parse the brace-enclosed initializer list. */
7061 initializer = cp_parser_braced_list (parser,
7062 &non_constant_p);
7064 /* If that worked, we're definitely looking at a
7065 compound-literal expression. */
7066 if (cp_parser_parse_definitely (parser))
7068 /* Warn the user that a compound literal is not
7069 allowed in standard C++. */
7070 pedwarn (input_location, OPT_Wpedantic,
7071 "ISO C++ forbids compound-literals");
7072 /* For simplicity, we disallow compound literals in
7073 constant-expressions. We could
7074 allow compound literals of integer type, whose
7075 initializer was a constant, in constant
7076 expressions. Permitting that usage, as a further
7077 extension, would not change the meaning of any
7078 currently accepted programs. (Of course, as
7079 compound literals are not part of ISO C++, the
7080 standard has nothing to say.) */
7081 if (cp_parser_non_integral_constant_expression (parser,
7082 NIC_NCC))
7084 postfix_expression = error_mark_node;
7085 break;
7087 /* Form the representation of the compound-literal. */
7088 postfix_expression
7089 = finish_compound_literal (type, initializer,
7090 tf_warning_or_error, fcl_c99);
7091 postfix_expression.set_location (initializer.get_location ());
7092 break;
7096 /* It must be a primary-expression. */
7097 postfix_expression
7098 = cp_parser_primary_expression (parser, address_p, cast_p,
7099 /*template_arg_p=*/false,
7100 decltype_p,
7101 &idk);
7103 break;
7106 /* Note that we don't need to worry about calling build_cplus_new on a
7107 class-valued CALL_EXPR in decltype when it isn't the end of the
7108 postfix-expression; unary_complex_lvalue will take care of that for
7109 all these cases. */
7111 /* Keep looping until the postfix-expression is complete. */
7112 while (true)
7114 if (idk == CP_ID_KIND_UNQUALIFIED
7115 && identifier_p (postfix_expression)
7116 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7117 /* It is not a Koenig lookup function call. */
7118 postfix_expression
7119 = unqualified_name_lookup_error (postfix_expression);
7121 /* Peek at the next token. */
7122 token = cp_lexer_peek_token (parser->lexer);
7124 switch (token->type)
7126 case CPP_OPEN_SQUARE:
7127 if (cp_next_tokens_can_be_std_attribute_p (parser))
7129 cp_parser_error (parser,
7130 "two consecutive %<[%> shall "
7131 "only introduce an attribute");
7132 return error_mark_node;
7134 postfix_expression
7135 = cp_parser_postfix_open_square_expression (parser,
7136 postfix_expression,
7137 false,
7138 decltype_p);
7139 postfix_expression.set_range (start_loc,
7140 postfix_expression.get_location ());
7142 idk = CP_ID_KIND_NONE;
7143 is_member_access = false;
7144 break;
7146 case CPP_OPEN_PAREN:
7147 /* postfix-expression ( expression-list [opt] ) */
7149 bool koenig_p;
7150 bool is_builtin_constant_p;
7151 bool saved_integral_constant_expression_p = false;
7152 bool saved_non_integral_constant_expression_p = false;
7153 tsubst_flags_t complain = complain_flags (decltype_p);
7154 vec<tree, va_gc> *args;
7155 location_t close_paren_loc = UNKNOWN_LOCATION;
7157 is_member_access = false;
7159 is_builtin_constant_p
7160 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7161 if (is_builtin_constant_p)
7163 /* The whole point of __builtin_constant_p is to allow
7164 non-constant expressions to appear as arguments. */
7165 saved_integral_constant_expression_p
7166 = parser->integral_constant_expression_p;
7167 saved_non_integral_constant_expression_p
7168 = parser->non_integral_constant_expression_p;
7169 parser->integral_constant_expression_p = false;
7171 args = (cp_parser_parenthesized_expression_list
7172 (parser, non_attr,
7173 /*cast_p=*/false, /*allow_expansion_p=*/true,
7174 /*non_constant_p=*/NULL,
7175 /*close_paren_loc=*/&close_paren_loc,
7176 /*wrap_locations_p=*/true));
7177 if (is_builtin_constant_p)
7179 parser->integral_constant_expression_p
7180 = saved_integral_constant_expression_p;
7181 parser->non_integral_constant_expression_p
7182 = saved_non_integral_constant_expression_p;
7185 if (args == NULL)
7187 postfix_expression = error_mark_node;
7188 break;
7191 /* Function calls are not permitted in
7192 constant-expressions. */
7193 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7194 && cp_parser_non_integral_constant_expression (parser,
7195 NIC_FUNC_CALL))
7197 postfix_expression = error_mark_node;
7198 release_tree_vector (args);
7199 break;
7202 koenig_p = false;
7203 if (idk == CP_ID_KIND_UNQUALIFIED
7204 || idk == CP_ID_KIND_TEMPLATE_ID)
7206 if (identifier_p (postfix_expression)
7207 /* In C++2A, we may need to perform ADL for a template
7208 name. */
7209 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7210 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7212 if (!args->is_empty ())
7214 koenig_p = true;
7215 if (!any_type_dependent_arguments_p (args))
7216 postfix_expression
7217 = perform_koenig_lookup (postfix_expression, args,
7218 complain);
7220 else
7221 postfix_expression
7222 = unqualified_fn_lookup_error (postfix_expression);
7224 /* We do not perform argument-dependent lookup if
7225 normal lookup finds a non-function, in accordance
7226 with the expected resolution of DR 218. */
7227 else if (!args->is_empty ()
7228 && is_overloaded_fn (postfix_expression))
7230 tree fn = get_first_fn (postfix_expression);
7231 fn = STRIP_TEMPLATE (fn);
7233 /* Do not do argument dependent lookup if regular
7234 lookup finds a member function or a block-scope
7235 function declaration. [basic.lookup.argdep]/3 */
7236 if (!DECL_FUNCTION_MEMBER_P (fn)
7237 && !DECL_LOCAL_FUNCTION_P (fn))
7239 koenig_p = true;
7240 if (!any_type_dependent_arguments_p (args))
7241 postfix_expression
7242 = perform_koenig_lookup (postfix_expression, args,
7243 complain);
7248 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7250 tree instance = TREE_OPERAND (postfix_expression, 0);
7251 tree fn = TREE_OPERAND (postfix_expression, 1);
7253 if (processing_template_decl
7254 && (type_dependent_object_expression_p (instance)
7255 || (!BASELINK_P (fn)
7256 && TREE_CODE (fn) != FIELD_DECL)
7257 || type_dependent_expression_p (fn)
7258 || any_type_dependent_arguments_p (args)))
7260 maybe_generic_this_capture (instance, fn);
7261 postfix_expression
7262 = build_min_nt_call_vec (postfix_expression, args);
7263 release_tree_vector (args);
7264 break;
7267 if (BASELINK_P (fn))
7269 postfix_expression
7270 = (build_new_method_call
7271 (instance, fn, &args, NULL_TREE,
7272 (idk == CP_ID_KIND_QUALIFIED
7273 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7274 : LOOKUP_NORMAL),
7275 /*fn_p=*/NULL,
7276 complain));
7278 else
7279 postfix_expression
7280 = finish_call_expr (postfix_expression, &args,
7281 /*disallow_virtual=*/false,
7282 /*koenig_p=*/false,
7283 complain);
7285 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7286 || TREE_CODE (postfix_expression) == MEMBER_REF
7287 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7288 postfix_expression = (build_offset_ref_call_from_tree
7289 (postfix_expression, &args,
7290 complain));
7291 else if (idk == CP_ID_KIND_QUALIFIED)
7292 /* A call to a static class member, or a namespace-scope
7293 function. */
7294 postfix_expression
7295 = finish_call_expr (postfix_expression, &args,
7296 /*disallow_virtual=*/true,
7297 koenig_p,
7298 complain);
7299 else
7300 /* All other function calls. */
7301 postfix_expression
7302 = finish_call_expr (postfix_expression, &args,
7303 /*disallow_virtual=*/false,
7304 koenig_p,
7305 complain);
7307 if (close_paren_loc != UNKNOWN_LOCATION)
7309 location_t combined_loc = make_location (token->location,
7310 start_loc,
7311 close_paren_loc);
7312 postfix_expression.set_location (combined_loc);
7315 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7316 idk = CP_ID_KIND_NONE;
7318 release_tree_vector (args);
7320 break;
7322 case CPP_DOT:
7323 case CPP_DEREF:
7324 /* postfix-expression . template [opt] id-expression
7325 postfix-expression . pseudo-destructor-name
7326 postfix-expression -> template [opt] id-expression
7327 postfix-expression -> pseudo-destructor-name */
7329 /* Consume the `.' or `->' operator. */
7330 cp_lexer_consume_token (parser->lexer);
7332 postfix_expression
7333 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7334 postfix_expression,
7335 false, &idk, loc);
7337 is_member_access = true;
7338 break;
7340 case CPP_PLUS_PLUS:
7341 /* postfix-expression ++ */
7342 /* Consume the `++' token. */
7343 cp_lexer_consume_token (parser->lexer);
7344 /* Generate a representation for the complete expression. */
7345 postfix_expression
7346 = finish_increment_expr (postfix_expression,
7347 POSTINCREMENT_EXPR);
7348 /* Increments may not appear in constant-expressions. */
7349 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7350 postfix_expression = error_mark_node;
7351 idk = CP_ID_KIND_NONE;
7352 is_member_access = false;
7353 break;
7355 case CPP_MINUS_MINUS:
7356 /* postfix-expression -- */
7357 /* Consume the `--' token. */
7358 cp_lexer_consume_token (parser->lexer);
7359 /* Generate a representation for the complete expression. */
7360 postfix_expression
7361 = finish_increment_expr (postfix_expression,
7362 POSTDECREMENT_EXPR);
7363 /* Decrements may not appear in constant-expressions. */
7364 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7365 postfix_expression = error_mark_node;
7366 idk = CP_ID_KIND_NONE;
7367 is_member_access = false;
7368 break;
7370 default:
7371 if (pidk_return != NULL)
7372 * pidk_return = idk;
7373 if (member_access_only_p)
7374 return is_member_access
7375 ? postfix_expression
7376 : cp_expr (error_mark_node);
7377 else
7378 return postfix_expression;
7382 /* We should never get here. */
7383 gcc_unreachable ();
7384 return error_mark_node;
7387 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7388 by cp_parser_builtin_offsetof. We're looking for
7390 postfix-expression [ expression ]
7391 postfix-expression [ braced-init-list ] (C++11)
7393 FOR_OFFSETOF is set if we're being called in that context, which
7394 changes how we deal with integer constant expressions. */
7396 static tree
7397 cp_parser_postfix_open_square_expression (cp_parser *parser,
7398 tree postfix_expression,
7399 bool for_offsetof,
7400 bool decltype_p)
7402 tree index = NULL_TREE;
7403 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7404 bool saved_greater_than_is_operator_p;
7406 /* Consume the `[' token. */
7407 cp_lexer_consume_token (parser->lexer);
7409 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7410 parser->greater_than_is_operator_p = true;
7412 /* Parse the index expression. */
7413 /* ??? For offsetof, there is a question of what to allow here. If
7414 offsetof is not being used in an integral constant expression context,
7415 then we *could* get the right answer by computing the value at runtime.
7416 If we are in an integral constant expression context, then we might
7417 could accept any constant expression; hard to say without analysis.
7418 Rather than open the barn door too wide right away, allow only integer
7419 constant expressions here. */
7420 if (for_offsetof)
7421 index = cp_parser_constant_expression (parser);
7422 else
7424 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7426 bool expr_nonconst_p;
7427 cp_lexer_set_source_position (parser->lexer);
7428 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7429 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7431 else
7432 index = cp_parser_expression (parser);
7435 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7437 /* Look for the closing `]'. */
7438 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7440 /* Build the ARRAY_REF. */
7441 postfix_expression = grok_array_decl (loc, postfix_expression,
7442 index, decltype_p);
7444 /* When not doing offsetof, array references are not permitted in
7445 constant-expressions. */
7446 if (!for_offsetof
7447 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7448 postfix_expression = error_mark_node;
7450 return postfix_expression;
7453 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7454 dereference of incomplete type, returns true if error_mark_node should
7455 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7456 and *DEPENDENT_P. */
7458 bool
7459 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7460 bool *dependent_p)
7462 /* In a template, be permissive by treating an object expression
7463 of incomplete type as dependent (after a pedwarn). */
7464 diagnostic_t kind = (processing_template_decl
7465 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7467 switch (TREE_CODE (*postfix_expression))
7469 case CAST_EXPR:
7470 case REINTERPRET_CAST_EXPR:
7471 case CONST_CAST_EXPR:
7472 case STATIC_CAST_EXPR:
7473 case DYNAMIC_CAST_EXPR:
7474 case IMPLICIT_CONV_EXPR:
7475 case VIEW_CONVERT_EXPR:
7476 case NON_LVALUE_EXPR:
7477 kind = DK_ERROR;
7478 break;
7479 case OVERLOAD:
7480 /* Don't emit any diagnostic for OVERLOADs. */
7481 kind = DK_IGNORED;
7482 break;
7483 default:
7484 /* Avoid clobbering e.g. DECLs. */
7485 if (!EXPR_P (*postfix_expression))
7486 kind = DK_ERROR;
7487 break;
7490 if (kind == DK_IGNORED)
7491 return false;
7493 location_t exploc = location_of (*postfix_expression);
7494 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7495 if (!MAYBE_CLASS_TYPE_P (*scope))
7496 return true;
7497 if (kind == DK_ERROR)
7498 *scope = *postfix_expression = error_mark_node;
7499 else if (processing_template_decl)
7501 *dependent_p = true;
7502 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7504 return false;
7507 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7508 by cp_parser_builtin_offsetof. We're looking for
7510 postfix-expression . template [opt] id-expression
7511 postfix-expression . pseudo-destructor-name
7512 postfix-expression -> template [opt] id-expression
7513 postfix-expression -> pseudo-destructor-name
7515 FOR_OFFSETOF is set if we're being called in that context. That sorta
7516 limits what of the above we'll actually accept, but nevermind.
7517 TOKEN_TYPE is the "." or "->" token, which will already have been
7518 removed from the stream. */
7520 static tree
7521 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7522 enum cpp_ttype token_type,
7523 cp_expr postfix_expression,
7524 bool for_offsetof, cp_id_kind *idk,
7525 location_t location)
7527 tree name;
7528 bool dependent_p;
7529 bool pseudo_destructor_p;
7530 tree scope = NULL_TREE;
7531 location_t start_loc = postfix_expression.get_start ();
7533 /* If this is a `->' operator, dereference the pointer. */
7534 if (token_type == CPP_DEREF)
7535 postfix_expression = build_x_arrow (location, postfix_expression,
7536 tf_warning_or_error);
7537 /* Check to see whether or not the expression is type-dependent and
7538 not the current instantiation. */
7539 dependent_p = type_dependent_object_expression_p (postfix_expression);
7540 /* The identifier following the `->' or `.' is not qualified. */
7541 parser->scope = NULL_TREE;
7542 parser->qualifying_scope = NULL_TREE;
7543 parser->object_scope = NULL_TREE;
7544 *idk = CP_ID_KIND_NONE;
7546 /* Enter the scope corresponding to the type of the object
7547 given by the POSTFIX_EXPRESSION. */
7548 if (!dependent_p)
7550 scope = TREE_TYPE (postfix_expression);
7551 /* According to the standard, no expression should ever have
7552 reference type. Unfortunately, we do not currently match
7553 the standard in this respect in that our internal representation
7554 of an expression may have reference type even when the standard
7555 says it does not. Therefore, we have to manually obtain the
7556 underlying type here. */
7557 scope = non_reference (scope);
7558 /* The type of the POSTFIX_EXPRESSION must be complete. */
7559 /* Unlike the object expression in other contexts, *this is not
7560 required to be of complete type for purposes of class member
7561 access (5.2.5) outside the member function body. */
7562 if (postfix_expression != current_class_ref
7563 && scope != error_mark_node
7564 && !currently_open_class (scope))
7566 scope = complete_type (scope);
7567 if (!COMPLETE_TYPE_P (scope)
7568 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7569 &dependent_p))
7570 return error_mark_node;
7573 if (!dependent_p)
7575 /* Let the name lookup machinery know that we are processing a
7576 class member access expression. */
7577 parser->context->object_type = scope;
7578 /* If something went wrong, we want to be able to discern that case,
7579 as opposed to the case where there was no SCOPE due to the type
7580 of expression being dependent. */
7581 if (!scope)
7582 scope = error_mark_node;
7583 /* If the SCOPE was erroneous, make the various semantic analysis
7584 functions exit quickly -- and without issuing additional error
7585 messages. */
7586 if (scope == error_mark_node)
7587 postfix_expression = error_mark_node;
7591 if (dependent_p)
7592 /* Tell cp_parser_lookup_name that there was an object, even though it's
7593 type-dependent. */
7594 parser->context->object_type = unknown_type_node;
7596 /* Assume this expression is not a pseudo-destructor access. */
7597 pseudo_destructor_p = false;
7599 /* If the SCOPE is a scalar type, then, if this is a valid program,
7600 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7601 is type dependent, it can be pseudo-destructor-name or something else.
7602 Try to parse it as pseudo-destructor-name first. */
7603 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7605 tree s;
7606 tree type;
7608 cp_parser_parse_tentatively (parser);
7609 /* Parse the pseudo-destructor-name. */
7610 s = NULL_TREE;
7611 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7612 &s, &type);
7613 if (dependent_p
7614 && (cp_parser_error_occurred (parser)
7615 || !SCALAR_TYPE_P (type)))
7616 cp_parser_abort_tentative_parse (parser);
7617 else if (cp_parser_parse_definitely (parser))
7619 pseudo_destructor_p = true;
7620 postfix_expression
7621 = finish_pseudo_destructor_expr (postfix_expression,
7622 s, type, location);
7626 if (!pseudo_destructor_p)
7628 /* If the SCOPE is not a scalar type, we are looking at an
7629 ordinary class member access expression, rather than a
7630 pseudo-destructor-name. */
7631 bool template_p;
7632 cp_token *token = cp_lexer_peek_token (parser->lexer);
7633 /* Parse the id-expression. */
7634 name = (cp_parser_id_expression
7635 (parser,
7636 cp_parser_optional_template_keyword (parser),
7637 /*check_dependency_p=*/true,
7638 &template_p,
7639 /*declarator_p=*/false,
7640 /*optional_p=*/false));
7641 /* In general, build a SCOPE_REF if the member name is qualified.
7642 However, if the name was not dependent and has already been
7643 resolved; there is no need to build the SCOPE_REF. For example;
7645 struct X { void f(); };
7646 template <typename T> void f(T* t) { t->X::f(); }
7648 Even though "t" is dependent, "X::f" is not and has been resolved
7649 to a BASELINK; there is no need to include scope information. */
7651 /* But we do need to remember that there was an explicit scope for
7652 virtual function calls. */
7653 if (parser->scope)
7654 *idk = CP_ID_KIND_QUALIFIED;
7656 /* If the name is a template-id that names a type, we will get a
7657 TYPE_DECL here. That is invalid code. */
7658 if (TREE_CODE (name) == TYPE_DECL)
7660 error_at (token->location, "invalid use of %qD", name);
7661 postfix_expression = error_mark_node;
7663 else
7665 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7667 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7669 error_at (token->location, "%<%D::%D%> is not a class member",
7670 parser->scope, name);
7671 postfix_expression = error_mark_node;
7673 else
7674 name = build_qualified_name (/*type=*/NULL_TREE,
7675 parser->scope,
7676 name,
7677 template_p);
7678 parser->scope = NULL_TREE;
7679 parser->qualifying_scope = NULL_TREE;
7680 parser->object_scope = NULL_TREE;
7682 if (parser->scope && name && BASELINK_P (name))
7683 adjust_result_of_qualified_name_lookup
7684 (name, parser->scope, scope);
7685 postfix_expression
7686 = finish_class_member_access_expr (postfix_expression, name,
7687 template_p,
7688 tf_warning_or_error);
7689 /* Build a location e.g.:
7690 ptr->access_expr
7691 ~~~^~~~~~~~~~~~~
7692 where the caret is at the deref token, ranging from
7693 the start of postfix_expression to the end of the access expr. */
7694 location_t end_loc
7695 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7696 location_t combined_loc
7697 = make_location (input_location, start_loc, end_loc);
7698 protected_set_expr_location (postfix_expression, combined_loc);
7702 /* We no longer need to look up names in the scope of the object on
7703 the left-hand side of the `.' or `->' operator. */
7704 parser->context->object_type = NULL_TREE;
7706 /* Outside of offsetof, these operators may not appear in
7707 constant-expressions. */
7708 if (!for_offsetof
7709 && (cp_parser_non_integral_constant_expression
7710 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7711 postfix_expression = error_mark_node;
7713 return postfix_expression;
7716 /* Parse a parenthesized expression-list.
7718 expression-list:
7719 assignment-expression
7720 expression-list, assignment-expression
7722 attribute-list:
7723 expression-list
7724 identifier
7725 identifier, expression-list
7727 CAST_P is true if this expression is the target of a cast.
7729 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7730 argument pack.
7732 WRAP_LOCATIONS_P is true if expressions within this list for which
7733 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7734 their source locations.
7736 Returns a vector of trees. Each element is a representation of an
7737 assignment-expression. NULL is returned if the ( and or ) are
7738 missing. An empty, but allocated, vector is returned on no
7739 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7740 if we are parsing an attribute list for an attribute that wants a
7741 plain identifier argument, normal_attr for an attribute that wants
7742 an expression, or non_attr if we aren't parsing an attribute list. If
7743 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7744 not all of the expressions in the list were constant.
7745 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7746 will be written to with the location of the closing parenthesis. If
7747 an error occurs, it may or may not be written to. */
7749 static vec<tree, va_gc> *
7750 cp_parser_parenthesized_expression_list (cp_parser* parser,
7751 int is_attribute_list,
7752 bool cast_p,
7753 bool allow_expansion_p,
7754 bool *non_constant_p,
7755 location_t *close_paren_loc,
7756 bool wrap_locations_p)
7758 vec<tree, va_gc> *expression_list;
7759 bool fold_expr_p = is_attribute_list != non_attr;
7760 tree identifier = NULL_TREE;
7761 bool saved_greater_than_is_operator_p;
7763 /* Assume all the expressions will be constant. */
7764 if (non_constant_p)
7765 *non_constant_p = false;
7767 matching_parens parens;
7768 if (!parens.require_open (parser))
7769 return NULL;
7771 expression_list = make_tree_vector ();
7773 /* Within a parenthesized expression, a `>' token is always
7774 the greater-than operator. */
7775 saved_greater_than_is_operator_p
7776 = parser->greater_than_is_operator_p;
7777 parser->greater_than_is_operator_p = true;
7779 cp_expr expr (NULL_TREE);
7781 /* Consume expressions until there are no more. */
7782 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7783 while (true)
7785 /* At the beginning of attribute lists, check to see if the
7786 next token is an identifier. */
7787 if (is_attribute_list == id_attr
7788 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7790 cp_token *token;
7792 /* Consume the identifier. */
7793 token = cp_lexer_consume_token (parser->lexer);
7794 /* Save the identifier. */
7795 identifier = token->u.value;
7797 else
7799 bool expr_non_constant_p;
7801 /* Parse the next assignment-expression. */
7802 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7804 /* A braced-init-list. */
7805 cp_lexer_set_source_position (parser->lexer);
7806 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7807 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7808 if (non_constant_p && expr_non_constant_p)
7809 *non_constant_p = true;
7811 else if (non_constant_p)
7813 expr = (cp_parser_constant_expression
7814 (parser, /*allow_non_constant_p=*/true,
7815 &expr_non_constant_p));
7816 if (expr_non_constant_p)
7817 *non_constant_p = true;
7819 else
7820 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7821 cast_p);
7823 if (fold_expr_p)
7824 expr = instantiate_non_dependent_expr (expr);
7826 /* If we have an ellipsis, then this is an expression
7827 expansion. */
7828 if (allow_expansion_p
7829 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7831 /* Consume the `...'. */
7832 cp_lexer_consume_token (parser->lexer);
7834 /* Build the argument pack. */
7835 expr = make_pack_expansion (expr);
7838 if (wrap_locations_p)
7839 expr.maybe_add_location_wrapper ();
7841 /* Add it to the list. We add error_mark_node
7842 expressions to the list, so that we can still tell if
7843 the correct form for a parenthesized expression-list
7844 is found. That gives better errors. */
7845 vec_safe_push (expression_list, expr.get_value ());
7847 if (expr == error_mark_node)
7848 goto skip_comma;
7851 /* After the first item, attribute lists look the same as
7852 expression lists. */
7853 is_attribute_list = non_attr;
7855 get_comma:;
7856 /* If the next token isn't a `,', then we are done. */
7857 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7858 break;
7860 /* Otherwise, consume the `,' and keep going. */
7861 cp_lexer_consume_token (parser->lexer);
7864 if (close_paren_loc)
7865 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7867 if (!parens.require_close (parser))
7869 int ending;
7871 skip_comma:;
7872 /* We try and resync to an unnested comma, as that will give the
7873 user better diagnostics. */
7874 ending = cp_parser_skip_to_closing_parenthesis (parser,
7875 /*recovering=*/true,
7876 /*or_comma=*/true,
7877 /*consume_paren=*/true);
7878 if (ending < 0)
7879 goto get_comma;
7880 if (!ending)
7882 parser->greater_than_is_operator_p
7883 = saved_greater_than_is_operator_p;
7884 return NULL;
7888 parser->greater_than_is_operator_p
7889 = saved_greater_than_is_operator_p;
7891 if (identifier)
7892 vec_safe_insert (expression_list, 0, identifier);
7894 return expression_list;
7897 /* Parse a pseudo-destructor-name.
7899 pseudo-destructor-name:
7900 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7901 :: [opt] nested-name-specifier template template-id :: ~ type-name
7902 :: [opt] nested-name-specifier [opt] ~ type-name
7904 If either of the first two productions is used, sets *SCOPE to the
7905 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7906 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7907 or ERROR_MARK_NODE if the parse fails. */
7909 static void
7910 cp_parser_pseudo_destructor_name (cp_parser* parser,
7911 tree object,
7912 tree* scope,
7913 tree* type)
7915 bool nested_name_specifier_p;
7917 /* Handle ~auto. */
7918 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7919 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7920 && !type_dependent_expression_p (object))
7922 if (cxx_dialect < cxx14)
7923 pedwarn (input_location, 0,
7924 "%<~auto%> only available with "
7925 "-std=c++14 or -std=gnu++14");
7926 cp_lexer_consume_token (parser->lexer);
7927 cp_lexer_consume_token (parser->lexer);
7928 *scope = NULL_TREE;
7929 *type = TREE_TYPE (object);
7930 return;
7933 /* Assume that things will not work out. */
7934 *type = error_mark_node;
7936 /* Look for the optional `::' operator. */
7937 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7938 /* Look for the optional nested-name-specifier. */
7939 nested_name_specifier_p
7940 = (cp_parser_nested_name_specifier_opt (parser,
7941 /*typename_keyword_p=*/false,
7942 /*check_dependency_p=*/true,
7943 /*type_p=*/false,
7944 /*is_declaration=*/false)
7945 != NULL_TREE);
7946 /* Now, if we saw a nested-name-specifier, we might be doing the
7947 second production. */
7948 if (nested_name_specifier_p
7949 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7951 /* Consume the `template' keyword. */
7952 cp_lexer_consume_token (parser->lexer);
7953 /* Parse the template-id. */
7954 cp_parser_template_id (parser,
7955 /*template_keyword_p=*/true,
7956 /*check_dependency_p=*/false,
7957 class_type,
7958 /*is_declaration=*/true);
7959 /* Look for the `::' token. */
7960 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7962 /* If the next token is not a `~', then there might be some
7963 additional qualification. */
7964 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7966 /* At this point, we're looking for "type-name :: ~". The type-name
7967 must not be a class-name, since this is a pseudo-destructor. So,
7968 it must be either an enum-name, or a typedef-name -- both of which
7969 are just identifiers. So, we peek ahead to check that the "::"
7970 and "~" tokens are present; if they are not, then we can avoid
7971 calling type_name. */
7972 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7973 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7974 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7976 cp_parser_error (parser, "non-scalar type");
7977 return;
7980 /* Look for the type-name. */
7981 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7982 if (*scope == error_mark_node)
7983 return;
7985 /* Look for the `::' token. */
7986 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7988 else
7989 *scope = NULL_TREE;
7991 /* Look for the `~'. */
7992 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7994 /* Once we see the ~, this has to be a pseudo-destructor. */
7995 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7996 cp_parser_commit_to_topmost_tentative_parse (parser);
7998 /* Look for the type-name again. We are not responsible for
7999 checking that it matches the first type-name. */
8000 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8003 /* Parse a unary-expression.
8005 unary-expression:
8006 postfix-expression
8007 ++ cast-expression
8008 -- cast-expression
8009 unary-operator cast-expression
8010 sizeof unary-expression
8011 sizeof ( type-id )
8012 alignof ( type-id ) [C++0x]
8013 new-expression
8014 delete-expression
8016 GNU Extensions:
8018 unary-expression:
8019 __extension__ cast-expression
8020 __alignof__ unary-expression
8021 __alignof__ ( type-id )
8022 alignof unary-expression [C++0x]
8023 __real__ cast-expression
8024 __imag__ cast-expression
8025 && identifier
8026 sizeof ( type-id ) { initializer-list , [opt] }
8027 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8028 __alignof__ ( type-id ) { initializer-list , [opt] }
8030 ADDRESS_P is true iff the unary-expression is appearing as the
8031 operand of the `&' operator. CAST_P is true if this expression is
8032 the target of a cast.
8034 Returns a representation of the expression. */
8036 static cp_expr
8037 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8038 bool address_p, bool cast_p, bool decltype_p)
8040 cp_token *token;
8041 enum tree_code unary_operator;
8043 /* Peek at the next token. */
8044 token = cp_lexer_peek_token (parser->lexer);
8045 /* Some keywords give away the kind of expression. */
8046 if (token->type == CPP_KEYWORD)
8048 enum rid keyword = token->keyword;
8050 switch (keyword)
8052 case RID_ALIGNOF:
8053 case RID_SIZEOF:
8055 tree operand, ret;
8056 enum tree_code op;
8057 location_t start_loc = token->location;
8059 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8060 bool std_alignof = id_equal (token->u.value, "alignof");
8062 /* Consume the token. */
8063 cp_lexer_consume_token (parser->lexer);
8064 /* Parse the operand. */
8065 operand = cp_parser_sizeof_operand (parser, keyword);
8067 if (TYPE_P (operand))
8068 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8069 true);
8070 else
8072 /* ISO C++ defines alignof only with types, not with
8073 expressions. So pedwarn if alignof is used with a non-
8074 type expression. However, __alignof__ is ok. */
8075 if (std_alignof)
8076 pedwarn (token->location, OPT_Wpedantic,
8077 "ISO C++ does not allow %<alignof%> "
8078 "with a non-type");
8080 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8082 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8083 SIZEOF_EXPR with the original operand. */
8084 if (op == SIZEOF_EXPR && ret != error_mark_node)
8086 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8088 if (!processing_template_decl && TYPE_P (operand))
8090 ret = build_min (SIZEOF_EXPR, size_type_node,
8091 build1 (NOP_EXPR, operand,
8092 error_mark_node));
8093 SIZEOF_EXPR_TYPE_P (ret) = 1;
8095 else
8096 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8097 TREE_SIDE_EFFECTS (ret) = 0;
8098 TREE_READONLY (ret) = 1;
8102 /* Construct a location e.g. :
8103 alignof (expr)
8104 ^~~~~~~~~~~~~~
8105 with start == caret at the start of the "alignof"/"sizeof"
8106 token, with the endpoint at the final closing paren. */
8107 location_t finish_loc
8108 = cp_lexer_previous_token (parser->lexer)->location;
8109 location_t compound_loc
8110 = make_location (start_loc, start_loc, finish_loc);
8112 cp_expr ret_expr (ret);
8113 ret_expr.set_location (compound_loc);
8114 ret_expr = ret_expr.maybe_add_location_wrapper ();
8115 return ret_expr;
8118 case RID_NEW:
8119 return cp_parser_new_expression (parser);
8121 case RID_DELETE:
8122 return cp_parser_delete_expression (parser);
8124 case RID_EXTENSION:
8126 /* The saved value of the PEDANTIC flag. */
8127 int saved_pedantic;
8128 tree expr;
8130 /* Save away the PEDANTIC flag. */
8131 cp_parser_extension_opt (parser, &saved_pedantic);
8132 /* Parse the cast-expression. */
8133 expr = cp_parser_simple_cast_expression (parser);
8134 /* Restore the PEDANTIC flag. */
8135 pedantic = saved_pedantic;
8137 return expr;
8140 case RID_REALPART:
8141 case RID_IMAGPART:
8143 tree expression;
8145 /* Consume the `__real__' or `__imag__' token. */
8146 cp_lexer_consume_token (parser->lexer);
8147 /* Parse the cast-expression. */
8148 expression = cp_parser_simple_cast_expression (parser);
8149 /* Create the complete representation. */
8150 return build_x_unary_op (token->location,
8151 (keyword == RID_REALPART
8152 ? REALPART_EXPR : IMAGPART_EXPR),
8153 expression,
8154 tf_warning_or_error);
8156 break;
8158 case RID_TRANSACTION_ATOMIC:
8159 case RID_TRANSACTION_RELAXED:
8160 return cp_parser_transaction_expression (parser, keyword);
8162 case RID_NOEXCEPT:
8164 tree expr;
8165 const char *saved_message;
8166 bool saved_integral_constant_expression_p;
8167 bool saved_non_integral_constant_expression_p;
8168 bool saved_greater_than_is_operator_p;
8170 location_t start_loc = token->location;
8172 cp_lexer_consume_token (parser->lexer);
8173 matching_parens parens;
8174 parens.require_open (parser);
8176 saved_message = parser->type_definition_forbidden_message;
8177 parser->type_definition_forbidden_message
8178 = G_("types may not be defined in %<noexcept%> expressions");
8180 saved_integral_constant_expression_p
8181 = parser->integral_constant_expression_p;
8182 saved_non_integral_constant_expression_p
8183 = parser->non_integral_constant_expression_p;
8184 parser->integral_constant_expression_p = false;
8186 saved_greater_than_is_operator_p
8187 = parser->greater_than_is_operator_p;
8188 parser->greater_than_is_operator_p = true;
8190 ++cp_unevaluated_operand;
8191 ++c_inhibit_evaluation_warnings;
8192 ++cp_noexcept_operand;
8193 expr = cp_parser_expression (parser);
8194 --cp_noexcept_operand;
8195 --c_inhibit_evaluation_warnings;
8196 --cp_unevaluated_operand;
8198 parser->greater_than_is_operator_p
8199 = saved_greater_than_is_operator_p;
8201 parser->integral_constant_expression_p
8202 = saved_integral_constant_expression_p;
8203 parser->non_integral_constant_expression_p
8204 = saved_non_integral_constant_expression_p;
8206 parser->type_definition_forbidden_message = saved_message;
8208 location_t finish_loc
8209 = cp_lexer_peek_token (parser->lexer)->location;
8210 parens.require_close (parser);
8212 /* Construct a location of the form:
8213 noexcept (expr)
8214 ^~~~~~~~~~~~~~~
8215 with start == caret, finishing at the close-paren. */
8216 location_t noexcept_loc
8217 = make_location (start_loc, start_loc, finish_loc);
8219 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8220 noexcept_loc);
8223 default:
8224 break;
8228 /* Look for the `:: new' and `:: delete', which also signal the
8229 beginning of a new-expression, or delete-expression,
8230 respectively. If the next token is `::', then it might be one of
8231 these. */
8232 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8234 enum rid keyword;
8236 /* See if the token after the `::' is one of the keywords in
8237 which we're interested. */
8238 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8239 /* If it's `new', we have a new-expression. */
8240 if (keyword == RID_NEW)
8241 return cp_parser_new_expression (parser);
8242 /* Similarly, for `delete'. */
8243 else if (keyword == RID_DELETE)
8244 return cp_parser_delete_expression (parser);
8247 /* Look for a unary operator. */
8248 unary_operator = cp_parser_unary_operator (token);
8249 /* The `++' and `--' operators can be handled similarly, even though
8250 they are not technically unary-operators in the grammar. */
8251 if (unary_operator == ERROR_MARK)
8253 if (token->type == CPP_PLUS_PLUS)
8254 unary_operator = PREINCREMENT_EXPR;
8255 else if (token->type == CPP_MINUS_MINUS)
8256 unary_operator = PREDECREMENT_EXPR;
8257 /* Handle the GNU address-of-label extension. */
8258 else if (cp_parser_allow_gnu_extensions_p (parser)
8259 && token->type == CPP_AND_AND)
8261 tree identifier;
8262 tree expression;
8263 location_t start_loc = token->location;
8265 /* Consume the '&&' token. */
8266 cp_lexer_consume_token (parser->lexer);
8267 /* Look for the identifier. */
8268 location_t finish_loc
8269 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8270 identifier = cp_parser_identifier (parser);
8271 /* Construct a location of the form:
8272 &&label
8273 ^~~~~~~
8274 with caret==start at the "&&", finish at the end of the label. */
8275 location_t combined_loc
8276 = make_location (start_loc, start_loc, finish_loc);
8277 /* Create an expression representing the address. */
8278 expression = finish_label_address_expr (identifier, combined_loc);
8279 if (cp_parser_non_integral_constant_expression (parser,
8280 NIC_ADDR_LABEL))
8281 expression = error_mark_node;
8282 return expression;
8285 if (unary_operator != ERROR_MARK)
8287 cp_expr cast_expression;
8288 cp_expr expression = error_mark_node;
8289 non_integral_constant non_constant_p = NIC_NONE;
8290 location_t loc = token->location;
8291 tsubst_flags_t complain = complain_flags (decltype_p);
8293 /* Consume the operator token. */
8294 token = cp_lexer_consume_token (parser->lexer);
8295 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8297 /* Parse the cast-expression. */
8298 cast_expression
8299 = cp_parser_cast_expression (parser,
8300 unary_operator == ADDR_EXPR,
8301 /*cast_p=*/false,
8302 /*decltype*/false,
8303 pidk);
8305 /* Make a location:
8306 OP_TOKEN CAST_EXPRESSION
8307 ^~~~~~~~~~~~~~~~~~~~~~~~~
8308 with start==caret at the operator token, and
8309 extending to the end of the cast_expression. */
8310 loc = make_location (loc, loc, cast_expression.get_finish ());
8312 /* Now, build an appropriate representation. */
8313 switch (unary_operator)
8315 case INDIRECT_REF:
8316 non_constant_p = NIC_STAR;
8317 expression = build_x_indirect_ref (loc, cast_expression,
8318 RO_UNARY_STAR,
8319 complain);
8320 /* TODO: build_x_indirect_ref does not always honor the
8321 location, so ensure it is set. */
8322 expression.set_location (loc);
8323 break;
8325 case ADDR_EXPR:
8326 non_constant_p = NIC_ADDR;
8327 /* Fall through. */
8328 case BIT_NOT_EXPR:
8329 expression = build_x_unary_op (loc, unary_operator,
8330 cast_expression,
8331 complain);
8332 /* TODO: build_x_unary_op does not always honor the location,
8333 so ensure it is set. */
8334 expression.set_location (loc);
8335 break;
8337 case PREINCREMENT_EXPR:
8338 case PREDECREMENT_EXPR:
8339 non_constant_p = unary_operator == PREINCREMENT_EXPR
8340 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8341 /* Fall through. */
8342 case NEGATE_EXPR:
8343 /* Immediately fold negation of a constant, unless the constant is 0
8344 (since -0 == 0) or it would overflow. */
8345 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8346 && CONSTANT_CLASS_P (cast_expression)
8347 && !integer_zerop (cast_expression)
8348 && !TREE_OVERFLOW (cast_expression))
8350 tree folded = fold_build1 (unary_operator,
8351 TREE_TYPE (cast_expression),
8352 cast_expression);
8353 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8355 expression = cp_expr (folded, loc);
8356 break;
8359 /* Fall through. */
8360 case UNARY_PLUS_EXPR:
8361 case TRUTH_NOT_EXPR:
8362 expression = finish_unary_op_expr (loc, unary_operator,
8363 cast_expression, complain);
8364 break;
8366 default:
8367 gcc_unreachable ();
8370 if (non_constant_p != NIC_NONE
8371 && cp_parser_non_integral_constant_expression (parser,
8372 non_constant_p))
8373 expression = error_mark_node;
8375 return expression;
8378 return cp_parser_postfix_expression (parser, address_p, cast_p,
8379 /*member_access_only_p=*/false,
8380 decltype_p,
8381 pidk);
8384 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8385 unary-operator, the corresponding tree code is returned. */
8387 static enum tree_code
8388 cp_parser_unary_operator (cp_token* token)
8390 switch (token->type)
8392 case CPP_MULT:
8393 return INDIRECT_REF;
8395 case CPP_AND:
8396 return ADDR_EXPR;
8398 case CPP_PLUS:
8399 return UNARY_PLUS_EXPR;
8401 case CPP_MINUS:
8402 return NEGATE_EXPR;
8404 case CPP_NOT:
8405 return TRUTH_NOT_EXPR;
8407 case CPP_COMPL:
8408 return BIT_NOT_EXPR;
8410 default:
8411 return ERROR_MARK;
8415 /* Parse a new-expression.
8417 new-expression:
8418 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8419 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8421 Returns a representation of the expression. */
8423 static tree
8424 cp_parser_new_expression (cp_parser* parser)
8426 bool global_scope_p;
8427 vec<tree, va_gc> *placement;
8428 tree type;
8429 vec<tree, va_gc> *initializer;
8430 tree nelts = NULL_TREE;
8431 tree ret;
8433 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8435 /* Look for the optional `::' operator. */
8436 global_scope_p
8437 = (cp_parser_global_scope_opt (parser,
8438 /*current_scope_valid_p=*/false)
8439 != NULL_TREE);
8440 /* Look for the `new' operator. */
8441 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8442 /* There's no easy way to tell a new-placement from the
8443 `( type-id )' construct. */
8444 cp_parser_parse_tentatively (parser);
8445 /* Look for a new-placement. */
8446 placement = cp_parser_new_placement (parser);
8447 /* If that didn't work out, there's no new-placement. */
8448 if (!cp_parser_parse_definitely (parser))
8450 if (placement != NULL)
8451 release_tree_vector (placement);
8452 placement = NULL;
8455 /* If the next token is a `(', then we have a parenthesized
8456 type-id. */
8457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8459 cp_token *token;
8460 const char *saved_message = parser->type_definition_forbidden_message;
8462 /* Consume the `('. */
8463 matching_parens parens;
8464 parens.consume_open (parser);
8466 /* Parse the type-id. */
8467 parser->type_definition_forbidden_message
8468 = G_("types may not be defined in a new-expression");
8470 type_id_in_expr_sentinel s (parser);
8471 type = cp_parser_type_id (parser);
8473 parser->type_definition_forbidden_message = saved_message;
8475 /* Look for the closing `)'. */
8476 parens.require_close (parser);
8477 token = cp_lexer_peek_token (parser->lexer);
8478 /* There should not be a direct-new-declarator in this production,
8479 but GCC used to allowed this, so we check and emit a sensible error
8480 message for this case. */
8481 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8483 error_at (token->location,
8484 "array bound forbidden after parenthesized type-id");
8485 inform (token->location,
8486 "try removing the parentheses around the type-id");
8487 cp_parser_direct_new_declarator (parser);
8490 /* Otherwise, there must be a new-type-id. */
8491 else
8492 type = cp_parser_new_type_id (parser, &nelts);
8494 /* If the next token is a `(' or '{', then we have a new-initializer. */
8495 cp_token *token = cp_lexer_peek_token (parser->lexer);
8496 if (token->type == CPP_OPEN_PAREN
8497 || token->type == CPP_OPEN_BRACE)
8498 initializer = cp_parser_new_initializer (parser);
8499 else
8500 initializer = NULL;
8502 /* A new-expression may not appear in an integral constant
8503 expression. */
8504 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8505 ret = error_mark_node;
8506 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8507 of a new-type-id or type-id of a new-expression, the new-expression shall
8508 contain a new-initializer of the form ( assignment-expression )".
8509 Additionally, consistently with the spirit of DR 1467, we want to accept
8510 'new auto { 2 }' too. */
8511 else if ((ret = type_uses_auto (type))
8512 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8513 && (vec_safe_length (initializer) != 1
8514 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8515 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8517 error_at (token->location,
8518 "initialization of new-expression for type %<auto%> "
8519 "requires exactly one element");
8520 ret = error_mark_node;
8522 else
8524 /* Construct a location e.g.:
8525 ptr = new int[100]
8526 ^~~~~~~~~~~~
8527 with caret == start at the start of the "new" token, and the end
8528 at the end of the final token we consumed. */
8529 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8530 location_t end_loc = get_finish (end_tok->location);
8531 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8533 /* Create a representation of the new-expression. */
8534 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8535 tf_warning_or_error);
8536 protected_set_expr_location (ret, combined_loc);
8539 if (placement != NULL)
8540 release_tree_vector (placement);
8541 if (initializer != NULL)
8542 release_tree_vector (initializer);
8544 return ret;
8547 /* Parse a new-placement.
8549 new-placement:
8550 ( expression-list )
8552 Returns the same representation as for an expression-list. */
8554 static vec<tree, va_gc> *
8555 cp_parser_new_placement (cp_parser* parser)
8557 vec<tree, va_gc> *expression_list;
8559 /* Parse the expression-list. */
8560 expression_list = (cp_parser_parenthesized_expression_list
8561 (parser, non_attr, /*cast_p=*/false,
8562 /*allow_expansion_p=*/true,
8563 /*non_constant_p=*/NULL));
8565 if (expression_list && expression_list->is_empty ())
8566 error ("expected expression-list or type-id");
8568 return expression_list;
8571 /* Parse a new-type-id.
8573 new-type-id:
8574 type-specifier-seq new-declarator [opt]
8576 Returns the TYPE allocated. If the new-type-id indicates an array
8577 type, *NELTS is set to the number of elements in the last array
8578 bound; the TYPE will not include the last array bound. */
8580 static tree
8581 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8583 cp_decl_specifier_seq type_specifier_seq;
8584 cp_declarator *new_declarator;
8585 cp_declarator *declarator;
8586 cp_declarator *outer_declarator;
8587 const char *saved_message;
8589 /* The type-specifier sequence must not contain type definitions.
8590 (It cannot contain declarations of new types either, but if they
8591 are not definitions we will catch that because they are not
8592 complete.) */
8593 saved_message = parser->type_definition_forbidden_message;
8594 parser->type_definition_forbidden_message
8595 = G_("types may not be defined in a new-type-id");
8596 /* Parse the type-specifier-seq. */
8597 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8598 /*is_trailing_return=*/false,
8599 &type_specifier_seq);
8600 /* Restore the old message. */
8601 parser->type_definition_forbidden_message = saved_message;
8603 if (type_specifier_seq.type == error_mark_node)
8604 return error_mark_node;
8606 /* Parse the new-declarator. */
8607 new_declarator = cp_parser_new_declarator_opt (parser);
8609 /* Determine the number of elements in the last array dimension, if
8610 any. */
8611 *nelts = NULL_TREE;
8612 /* Skip down to the last array dimension. */
8613 declarator = new_declarator;
8614 outer_declarator = NULL;
8615 while (declarator && (declarator->kind == cdk_pointer
8616 || declarator->kind == cdk_ptrmem))
8618 outer_declarator = declarator;
8619 declarator = declarator->declarator;
8621 while (declarator
8622 && declarator->kind == cdk_array
8623 && declarator->declarator
8624 && declarator->declarator->kind == cdk_array)
8626 outer_declarator = declarator;
8627 declarator = declarator->declarator;
8630 if (declarator && declarator->kind == cdk_array)
8632 *nelts = declarator->u.array.bounds;
8633 if (*nelts == error_mark_node)
8634 *nelts = integer_one_node;
8636 if (outer_declarator)
8637 outer_declarator->declarator = declarator->declarator;
8638 else
8639 new_declarator = NULL;
8642 return groktypename (&type_specifier_seq, new_declarator, false);
8645 /* Parse an (optional) new-declarator.
8647 new-declarator:
8648 ptr-operator new-declarator [opt]
8649 direct-new-declarator
8651 Returns the declarator. */
8653 static cp_declarator *
8654 cp_parser_new_declarator_opt (cp_parser* parser)
8656 enum tree_code code;
8657 tree type, std_attributes = NULL_TREE;
8658 cp_cv_quals cv_quals;
8660 /* We don't know if there's a ptr-operator next, or not. */
8661 cp_parser_parse_tentatively (parser);
8662 /* Look for a ptr-operator. */
8663 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8664 /* If that worked, look for more new-declarators. */
8665 if (cp_parser_parse_definitely (parser))
8667 cp_declarator *declarator;
8669 /* Parse another optional declarator. */
8670 declarator = cp_parser_new_declarator_opt (parser);
8672 declarator = cp_parser_make_indirect_declarator
8673 (code, type, cv_quals, declarator, std_attributes);
8675 return declarator;
8678 /* If the next token is a `[', there is a direct-new-declarator. */
8679 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8680 return cp_parser_direct_new_declarator (parser);
8682 return NULL;
8685 /* Parse a direct-new-declarator.
8687 direct-new-declarator:
8688 [ expression ]
8689 direct-new-declarator [constant-expression]
8693 static cp_declarator *
8694 cp_parser_direct_new_declarator (cp_parser* parser)
8696 cp_declarator *declarator = NULL;
8698 while (true)
8700 tree expression;
8701 cp_token *token;
8703 /* Look for the opening `['. */
8704 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8706 token = cp_lexer_peek_token (parser->lexer);
8707 expression = cp_parser_expression (parser);
8708 /* The standard requires that the expression have integral
8709 type. DR 74 adds enumeration types. We believe that the
8710 real intent is that these expressions be handled like the
8711 expression in a `switch' condition, which also allows
8712 classes with a single conversion to integral or
8713 enumeration type. */
8714 if (!processing_template_decl)
8716 expression
8717 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8718 expression,
8719 /*complain=*/true);
8720 if (!expression)
8722 error_at (token->location,
8723 "expression in new-declarator must have integral "
8724 "or enumeration type");
8725 expression = error_mark_node;
8729 /* Look for the closing `]'. */
8730 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8732 /* Add this bound to the declarator. */
8733 declarator = make_array_declarator (declarator, expression);
8735 /* If the next token is not a `[', then there are no more
8736 bounds. */
8737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8738 break;
8741 return declarator;
8744 /* Parse a new-initializer.
8746 new-initializer:
8747 ( expression-list [opt] )
8748 braced-init-list
8750 Returns a representation of the expression-list. */
8752 static vec<tree, va_gc> *
8753 cp_parser_new_initializer (cp_parser* parser)
8755 vec<tree, va_gc> *expression_list;
8757 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8759 tree t;
8760 bool expr_non_constant_p;
8761 cp_lexer_set_source_position (parser->lexer);
8762 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8763 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8764 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8765 expression_list = make_tree_vector_single (t);
8767 else
8768 expression_list = (cp_parser_parenthesized_expression_list
8769 (parser, non_attr, /*cast_p=*/false,
8770 /*allow_expansion_p=*/true,
8771 /*non_constant_p=*/NULL));
8773 return expression_list;
8776 /* Parse a delete-expression.
8778 delete-expression:
8779 :: [opt] delete cast-expression
8780 :: [opt] delete [ ] cast-expression
8782 Returns a representation of the expression. */
8784 static tree
8785 cp_parser_delete_expression (cp_parser* parser)
8787 bool global_scope_p;
8788 bool array_p;
8789 tree expression;
8791 /* Look for the optional `::' operator. */
8792 global_scope_p
8793 = (cp_parser_global_scope_opt (parser,
8794 /*current_scope_valid_p=*/false)
8795 != NULL_TREE);
8796 /* Look for the `delete' keyword. */
8797 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8798 /* See if the array syntax is in use. */
8799 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8801 /* Consume the `[' token. */
8802 cp_lexer_consume_token (parser->lexer);
8803 /* Look for the `]' token. */
8804 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8805 /* Remember that this is the `[]' construct. */
8806 array_p = true;
8808 else
8809 array_p = false;
8811 /* Parse the cast-expression. */
8812 expression = cp_parser_simple_cast_expression (parser);
8814 /* A delete-expression may not appear in an integral constant
8815 expression. */
8816 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8817 return error_mark_node;
8819 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8820 tf_warning_or_error);
8823 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8824 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8825 0 otherwise. */
8827 static int
8828 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8830 cp_token *token = cp_lexer_peek_token (parser->lexer);
8831 switch (token->type)
8833 case CPP_COMMA:
8834 case CPP_SEMICOLON:
8835 case CPP_QUERY:
8836 case CPP_COLON:
8837 case CPP_CLOSE_SQUARE:
8838 case CPP_CLOSE_PAREN:
8839 case CPP_CLOSE_BRACE:
8840 case CPP_OPEN_BRACE:
8841 case CPP_DOT:
8842 case CPP_DOT_STAR:
8843 case CPP_DEREF:
8844 case CPP_DEREF_STAR:
8845 case CPP_DIV:
8846 case CPP_MOD:
8847 case CPP_LSHIFT:
8848 case CPP_RSHIFT:
8849 case CPP_LESS:
8850 case CPP_GREATER:
8851 case CPP_LESS_EQ:
8852 case CPP_GREATER_EQ:
8853 case CPP_EQ_EQ:
8854 case CPP_NOT_EQ:
8855 case CPP_EQ:
8856 case CPP_MULT_EQ:
8857 case CPP_DIV_EQ:
8858 case CPP_MOD_EQ:
8859 case CPP_PLUS_EQ:
8860 case CPP_MINUS_EQ:
8861 case CPP_RSHIFT_EQ:
8862 case CPP_LSHIFT_EQ:
8863 case CPP_AND_EQ:
8864 case CPP_XOR_EQ:
8865 case CPP_OR_EQ:
8866 case CPP_XOR:
8867 case CPP_OR:
8868 case CPP_OR_OR:
8869 case CPP_EOF:
8870 case CPP_ELLIPSIS:
8871 return 0;
8873 case CPP_OPEN_PAREN:
8874 /* In ((type ()) () the last () isn't a valid cast-expression,
8875 so the whole must be parsed as postfix-expression. */
8876 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8877 != CPP_CLOSE_PAREN;
8879 case CPP_OPEN_SQUARE:
8880 /* '[' may start a primary-expression in obj-c++ and in C++11,
8881 as a lambda-expression, eg, '(void)[]{}'. */
8882 if (cxx_dialect >= cxx11)
8883 return -1;
8884 return c_dialect_objc ();
8886 case CPP_PLUS_PLUS:
8887 case CPP_MINUS_MINUS:
8888 /* '++' and '--' may or may not start a cast-expression:
8890 struct T { void operator++(int); };
8891 void f() { (T())++; }
8895 int a;
8896 (int)++a; */
8897 return -1;
8899 default:
8900 return 1;
8904 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8905 in the order: const_cast, static_cast, reinterpret_cast.
8907 Don't suggest dynamic_cast.
8909 Return the first legal cast kind found, or NULL otherwise. */
8911 static const char *
8912 get_cast_suggestion (tree dst_type, tree orig_expr)
8914 tree trial;
8916 /* Reuse the parser logic by attempting to build the various kinds of
8917 cast, with "complain" disabled.
8918 Identify the first such cast that is valid. */
8920 /* Don't attempt to run such logic within template processing. */
8921 if (processing_template_decl)
8922 return NULL;
8924 /* First try const_cast. */
8925 trial = build_const_cast (dst_type, orig_expr, tf_none);
8926 if (trial != error_mark_node)
8927 return "const_cast";
8929 /* If that fails, try static_cast. */
8930 trial = build_static_cast (dst_type, orig_expr, tf_none);
8931 if (trial != error_mark_node)
8932 return "static_cast";
8934 /* Finally, try reinterpret_cast. */
8935 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8936 if (trial != error_mark_node)
8937 return "reinterpret_cast";
8939 /* No such cast possible. */
8940 return NULL;
8943 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8944 suggesting how to convert a C-style cast of the form:
8946 (DST_TYPE)ORIG_EXPR
8948 to a C++-style cast.
8950 The primary range of RICHLOC is asssumed to be that of the original
8951 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8952 of the parens in the C-style cast. */
8954 static void
8955 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8956 location_t close_paren_loc, tree orig_expr,
8957 tree dst_type)
8959 /* This function is non-trivial, so bail out now if the warning isn't
8960 going to be emitted. */
8961 if (!warn_old_style_cast)
8962 return;
8964 /* Try to find a legal C++ cast, trying them in order:
8965 const_cast, static_cast, reinterpret_cast. */
8966 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8967 if (!cast_suggestion)
8968 return;
8970 /* Replace the open paren with "CAST_SUGGESTION<". */
8971 pretty_printer pp;
8972 pp_printf (&pp, "%s<", cast_suggestion);
8973 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8975 /* Replace the close paren with "> (". */
8976 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8978 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8979 rich_loc->add_fixit_insert_after (")");
8983 /* Parse a cast-expression.
8985 cast-expression:
8986 unary-expression
8987 ( type-id ) cast-expression
8989 ADDRESS_P is true iff the unary-expression is appearing as the
8990 operand of the `&' operator. CAST_P is true if this expression is
8991 the target of a cast.
8993 Returns a representation of the expression. */
8995 static cp_expr
8996 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8997 bool decltype_p, cp_id_kind * pidk)
8999 /* If it's a `(', then we might be looking at a cast. */
9000 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9002 tree type = NULL_TREE;
9003 cp_expr expr (NULL_TREE);
9004 int cast_expression = 0;
9005 const char *saved_message;
9007 /* There's no way to know yet whether or not this is a cast.
9008 For example, `(int (3))' is a unary-expression, while `(int)
9009 3' is a cast. So, we resort to parsing tentatively. */
9010 cp_parser_parse_tentatively (parser);
9011 /* Types may not be defined in a cast. */
9012 saved_message = parser->type_definition_forbidden_message;
9013 parser->type_definition_forbidden_message
9014 = G_("types may not be defined in casts");
9015 /* Consume the `('. */
9016 matching_parens parens;
9017 cp_token *open_paren = parens.consume_open (parser);
9018 location_t open_paren_loc = open_paren->location;
9019 location_t close_paren_loc = UNKNOWN_LOCATION;
9021 /* A very tricky bit is that `(struct S) { 3 }' is a
9022 compound-literal (which we permit in C++ as an extension).
9023 But, that construct is not a cast-expression -- it is a
9024 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9025 is legal; if the compound-literal were a cast-expression,
9026 you'd need an extra set of parentheses.) But, if we parse
9027 the type-id, and it happens to be a class-specifier, then we
9028 will commit to the parse at that point, because we cannot
9029 undo the action that is done when creating a new class. So,
9030 then we cannot back up and do a postfix-expression.
9032 Another tricky case is the following (c++/29234):
9034 struct S { void operator () (); };
9036 void foo ()
9038 ( S()() );
9041 As a type-id we parse the parenthesized S()() as a function
9042 returning a function, groktypename complains and we cannot
9043 back up in this case either.
9045 Therefore, we scan ahead to the closing `)', and check to see
9046 if the tokens after the `)' can start a cast-expression. Otherwise
9047 we are dealing with an unary-expression, a postfix-expression
9048 or something else.
9050 Yet another tricky case, in C++11, is the following (c++/54891):
9052 (void)[]{};
9054 The issue is that usually, besides the case of lambda-expressions,
9055 the parenthesized type-id cannot be followed by '[', and, eg, we
9056 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9057 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9058 we don't commit, we try a cast-expression, then an unary-expression.
9060 Save tokens so that we can put them back. */
9061 cp_lexer_save_tokens (parser->lexer);
9063 /* We may be looking at a cast-expression. */
9064 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9065 /*consume_paren=*/true))
9066 cast_expression
9067 = cp_parser_tokens_start_cast_expression (parser);
9069 /* Roll back the tokens we skipped. */
9070 cp_lexer_rollback_tokens (parser->lexer);
9071 /* If we aren't looking at a cast-expression, simulate an error so
9072 that the call to cp_parser_error_occurred below returns true. */
9073 if (!cast_expression)
9074 cp_parser_simulate_error (parser);
9075 else
9077 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9078 parser->in_type_id_in_expr_p = true;
9079 /* Look for the type-id. */
9080 type = cp_parser_type_id (parser);
9081 /* Look for the closing `)'. */
9082 cp_token *close_paren = parens.require_close (parser);
9083 if (close_paren)
9084 close_paren_loc = close_paren->location;
9085 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9088 /* Restore the saved message. */
9089 parser->type_definition_forbidden_message = saved_message;
9091 /* At this point this can only be either a cast or a
9092 parenthesized ctor such as `(T ())' that looks like a cast to
9093 function returning T. */
9094 if (!cp_parser_error_occurred (parser))
9096 /* Only commit if the cast-expression doesn't start with
9097 '++', '--', or '[' in C++11. */
9098 if (cast_expression > 0)
9099 cp_parser_commit_to_topmost_tentative_parse (parser);
9101 expr = cp_parser_cast_expression (parser,
9102 /*address_p=*/false,
9103 /*cast_p=*/true,
9104 /*decltype_p=*/false,
9105 pidk);
9107 if (cp_parser_parse_definitely (parser))
9109 /* Warn about old-style casts, if so requested. */
9110 if (warn_old_style_cast
9111 && !in_system_header_at (input_location)
9112 && !VOID_TYPE_P (type)
9113 && current_lang_name != lang_name_c)
9115 gcc_rich_location rich_loc (input_location);
9116 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9117 expr, type);
9118 warning_at (&rich_loc, OPT_Wold_style_cast,
9119 "use of old-style cast to %q#T", type);
9122 /* Only type conversions to integral or enumeration types
9123 can be used in constant-expressions. */
9124 if (!cast_valid_in_integral_constant_expression_p (type)
9125 && cp_parser_non_integral_constant_expression (parser,
9126 NIC_CAST))
9127 return error_mark_node;
9129 /* Perform the cast. */
9130 /* Make a location:
9131 (TYPE) EXPR
9132 ^~~~~~~~~~~
9133 with start==caret at the open paren, extending to the
9134 end of "expr". */
9135 location_t cast_loc = make_location (open_paren_loc,
9136 open_paren_loc,
9137 expr.get_finish ());
9138 expr = build_c_cast (cast_loc, type, expr);
9139 return expr;
9142 else
9143 cp_parser_abort_tentative_parse (parser);
9146 /* If we get here, then it's not a cast, so it must be a
9147 unary-expression. */
9148 return cp_parser_unary_expression (parser, pidk, address_p,
9149 cast_p, decltype_p);
9152 /* Parse a binary expression of the general form:
9154 pm-expression:
9155 cast-expression
9156 pm-expression .* cast-expression
9157 pm-expression ->* cast-expression
9159 multiplicative-expression:
9160 pm-expression
9161 multiplicative-expression * pm-expression
9162 multiplicative-expression / pm-expression
9163 multiplicative-expression % pm-expression
9165 additive-expression:
9166 multiplicative-expression
9167 additive-expression + multiplicative-expression
9168 additive-expression - multiplicative-expression
9170 shift-expression:
9171 additive-expression
9172 shift-expression << additive-expression
9173 shift-expression >> additive-expression
9175 relational-expression:
9176 shift-expression
9177 relational-expression < shift-expression
9178 relational-expression > shift-expression
9179 relational-expression <= shift-expression
9180 relational-expression >= shift-expression
9182 GNU Extension:
9184 relational-expression:
9185 relational-expression <? shift-expression
9186 relational-expression >? shift-expression
9188 equality-expression:
9189 relational-expression
9190 equality-expression == relational-expression
9191 equality-expression != relational-expression
9193 and-expression:
9194 equality-expression
9195 and-expression & equality-expression
9197 exclusive-or-expression:
9198 and-expression
9199 exclusive-or-expression ^ and-expression
9201 inclusive-or-expression:
9202 exclusive-or-expression
9203 inclusive-or-expression | exclusive-or-expression
9205 logical-and-expression:
9206 inclusive-or-expression
9207 logical-and-expression && inclusive-or-expression
9209 logical-or-expression:
9210 logical-and-expression
9211 logical-or-expression || logical-and-expression
9213 All these are implemented with a single function like:
9215 binary-expression:
9216 simple-cast-expression
9217 binary-expression <token> binary-expression
9219 CAST_P is true if this expression is the target of a cast.
9221 The binops_by_token map is used to get the tree codes for each <token> type.
9222 binary-expressions are associated according to a precedence table. */
9224 #define TOKEN_PRECEDENCE(token) \
9225 (((token->type == CPP_GREATER \
9226 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9227 && !parser->greater_than_is_operator_p) \
9228 ? PREC_NOT_OPERATOR \
9229 : binops_by_token[token->type].prec)
9231 static cp_expr
9232 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9233 bool no_toplevel_fold_p,
9234 bool decltype_p,
9235 enum cp_parser_prec prec,
9236 cp_id_kind * pidk)
9238 cp_parser_expression_stack stack;
9239 cp_parser_expression_stack_entry *sp = &stack[0];
9240 cp_parser_expression_stack_entry current;
9241 cp_expr rhs;
9242 cp_token *token;
9243 enum tree_code rhs_type;
9244 enum cp_parser_prec new_prec, lookahead_prec;
9245 tree overload;
9247 /* Parse the first expression. */
9248 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9249 ? TRUTH_NOT_EXPR : ERROR_MARK);
9250 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9251 cast_p, decltype_p, pidk);
9252 current.prec = prec;
9254 if (cp_parser_error_occurred (parser))
9255 return error_mark_node;
9257 for (;;)
9259 /* Get an operator token. */
9260 token = cp_lexer_peek_token (parser->lexer);
9262 if (warn_cxx11_compat
9263 && token->type == CPP_RSHIFT
9264 && !parser->greater_than_is_operator_p)
9266 if (warning_at (token->location, OPT_Wc__11_compat,
9267 "%<>>%> operator is treated"
9268 " as two right angle brackets in C++11"))
9269 inform (token->location,
9270 "suggest parentheses around %<>>%> expression");
9273 new_prec = TOKEN_PRECEDENCE (token);
9274 if (new_prec != PREC_NOT_OPERATOR
9275 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9276 /* This is a fold-expression; handle it later. */
9277 new_prec = PREC_NOT_OPERATOR;
9279 /* Popping an entry off the stack means we completed a subexpression:
9280 - either we found a token which is not an operator (`>' where it is not
9281 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9282 will happen repeatedly;
9283 - or, we found an operator which has lower priority. This is the case
9284 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9285 parsing `3 * 4'. */
9286 if (new_prec <= current.prec)
9288 if (sp == stack)
9289 break;
9290 else
9291 goto pop;
9294 get_rhs:
9295 current.tree_type = binops_by_token[token->type].tree_type;
9296 current.loc = token->location;
9298 /* We used the operator token. */
9299 cp_lexer_consume_token (parser->lexer);
9301 /* For "false && x" or "true || x", x will never be executed;
9302 disable warnings while evaluating it. */
9303 if (current.tree_type == TRUTH_ANDIF_EXPR)
9304 c_inhibit_evaluation_warnings +=
9305 cp_fully_fold (current.lhs) == truthvalue_false_node;
9306 else if (current.tree_type == TRUTH_ORIF_EXPR)
9307 c_inhibit_evaluation_warnings +=
9308 cp_fully_fold (current.lhs) == truthvalue_true_node;
9310 /* Extract another operand. It may be the RHS of this expression
9311 or the LHS of a new, higher priority expression. */
9312 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9313 ? TRUTH_NOT_EXPR : ERROR_MARK);
9314 rhs = cp_parser_simple_cast_expression (parser);
9316 /* Get another operator token. Look up its precedence to avoid
9317 building a useless (immediately popped) stack entry for common
9318 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9319 token = cp_lexer_peek_token (parser->lexer);
9320 lookahead_prec = TOKEN_PRECEDENCE (token);
9321 if (lookahead_prec != PREC_NOT_OPERATOR
9322 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9323 lookahead_prec = PREC_NOT_OPERATOR;
9324 if (lookahead_prec > new_prec)
9326 /* ... and prepare to parse the RHS of the new, higher priority
9327 expression. Since precedence levels on the stack are
9328 monotonically increasing, we do not have to care about
9329 stack overflows. */
9330 *sp = current;
9331 ++sp;
9332 current.lhs = rhs;
9333 current.lhs_type = rhs_type;
9334 current.prec = new_prec;
9335 new_prec = lookahead_prec;
9336 goto get_rhs;
9338 pop:
9339 lookahead_prec = new_prec;
9340 /* If the stack is not empty, we have parsed into LHS the right side
9341 (`4' in the example above) of an expression we had suspended.
9342 We can use the information on the stack to recover the LHS (`3')
9343 from the stack together with the tree code (`MULT_EXPR'), and
9344 the precedence of the higher level subexpression
9345 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9346 which will be used to actually build the additive expression. */
9347 rhs = current.lhs;
9348 rhs_type = current.lhs_type;
9349 --sp;
9350 current = *sp;
9353 /* Undo the disabling of warnings done above. */
9354 if (current.tree_type == TRUTH_ANDIF_EXPR)
9355 c_inhibit_evaluation_warnings -=
9356 cp_fully_fold (current.lhs) == truthvalue_false_node;
9357 else if (current.tree_type == TRUTH_ORIF_EXPR)
9358 c_inhibit_evaluation_warnings -=
9359 cp_fully_fold (current.lhs) == truthvalue_true_node;
9361 if (warn_logical_not_paren
9362 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9363 && current.lhs_type == TRUTH_NOT_EXPR
9364 /* Avoid warning for !!x == y. */
9365 && (TREE_CODE (current.lhs) != NE_EXPR
9366 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9367 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9368 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9369 /* Avoid warning for !b == y where b is boolean. */
9370 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9371 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9372 != BOOLEAN_TYPE))))
9373 /* Avoid warning for !!b == y where b is boolean. */
9374 && (!DECL_P (current.lhs)
9375 || TREE_TYPE (current.lhs) == NULL_TREE
9376 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9377 warn_logical_not_parentheses (current.loc, current.tree_type,
9378 current.lhs, maybe_constant_value (rhs));
9380 overload = NULL;
9382 location_t combined_loc = make_location (current.loc,
9383 current.lhs.get_start (),
9384 rhs.get_finish ());
9386 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9387 ERROR_MARK for everything that is not a binary expression.
9388 This makes warn_about_parentheses miss some warnings that
9389 involve unary operators. For unary expressions we should
9390 pass the correct tree_code unless the unary expression was
9391 surrounded by parentheses.
9393 if (no_toplevel_fold_p
9394 && lookahead_prec <= current.prec
9395 && sp == stack)
9397 if (current.lhs == error_mark_node || rhs == error_mark_node)
9398 current.lhs = error_mark_node;
9399 else
9401 current.lhs
9402 = build_min (current.tree_type,
9403 TREE_CODE_CLASS (current.tree_type)
9404 == tcc_comparison
9405 ? boolean_type_node : TREE_TYPE (current.lhs),
9406 current.lhs.get_value (), rhs.get_value ());
9407 SET_EXPR_LOCATION (current.lhs, combined_loc);
9410 else
9412 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9413 current.lhs, current.lhs_type,
9414 rhs, rhs_type, &overload,
9415 complain_flags (decltype_p));
9416 /* TODO: build_x_binary_op doesn't always honor the location. */
9417 current.lhs.set_location (combined_loc);
9419 current.lhs_type = current.tree_type;
9421 /* If the binary operator required the use of an overloaded operator,
9422 then this expression cannot be an integral constant-expression.
9423 An overloaded operator can be used even if both operands are
9424 otherwise permissible in an integral constant-expression if at
9425 least one of the operands is of enumeration type. */
9427 if (overload
9428 && cp_parser_non_integral_constant_expression (parser,
9429 NIC_OVERLOADED))
9430 return error_mark_node;
9433 return current.lhs;
9436 static cp_expr
9437 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9438 bool no_toplevel_fold_p,
9439 enum cp_parser_prec prec,
9440 cp_id_kind * pidk)
9442 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9443 /*decltype*/false, prec, pidk);
9446 /* Parse the `? expression : assignment-expression' part of a
9447 conditional-expression. The LOGICAL_OR_EXPR is the
9448 logical-or-expression that started the conditional-expression.
9449 Returns a representation of the entire conditional-expression.
9451 This routine is used by cp_parser_assignment_expression.
9453 ? expression : assignment-expression
9455 GNU Extensions:
9457 ? : assignment-expression */
9459 static tree
9460 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9462 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9463 cp_expr assignment_expr;
9464 struct cp_token *token;
9465 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9467 /* Consume the `?' token. */
9468 cp_lexer_consume_token (parser->lexer);
9469 token = cp_lexer_peek_token (parser->lexer);
9470 if (cp_parser_allow_gnu_extensions_p (parser)
9471 && token->type == CPP_COLON)
9473 pedwarn (token->location, OPT_Wpedantic,
9474 "ISO C++ does not allow ?: with omitted middle operand");
9475 /* Implicit true clause. */
9476 expr = NULL_TREE;
9477 c_inhibit_evaluation_warnings +=
9478 folded_logical_or_expr == truthvalue_true_node;
9479 warn_for_omitted_condop (token->location, logical_or_expr);
9481 else
9483 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9484 parser->colon_corrects_to_scope_p = false;
9485 /* Parse the expression. */
9486 c_inhibit_evaluation_warnings +=
9487 folded_logical_or_expr == truthvalue_false_node;
9488 expr = cp_parser_expression (parser);
9489 c_inhibit_evaluation_warnings +=
9490 ((folded_logical_or_expr == truthvalue_true_node)
9491 - (folded_logical_or_expr == truthvalue_false_node));
9492 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9495 /* The next token should be a `:'. */
9496 cp_parser_require (parser, CPP_COLON, RT_COLON);
9497 /* Parse the assignment-expression. */
9498 assignment_expr = cp_parser_assignment_expression (parser);
9499 c_inhibit_evaluation_warnings -=
9500 folded_logical_or_expr == truthvalue_true_node;
9502 /* Make a location:
9503 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9504 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9505 with the caret at the "?", ranging from the start of
9506 the logical_or_expr to the end of the assignment_expr. */
9507 loc = make_location (loc,
9508 logical_or_expr.get_start (),
9509 assignment_expr.get_finish ());
9511 /* Build the conditional-expression. */
9512 return build_x_conditional_expr (loc, logical_or_expr,
9513 expr,
9514 assignment_expr,
9515 tf_warning_or_error);
9518 /* Parse an assignment-expression.
9520 assignment-expression:
9521 conditional-expression
9522 logical-or-expression assignment-operator assignment_expression
9523 throw-expression
9525 CAST_P is true if this expression is the target of a cast.
9526 DECLTYPE_P is true if this expression is the operand of decltype.
9528 Returns a representation for the expression. */
9530 static cp_expr
9531 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9532 bool cast_p, bool decltype_p)
9534 cp_expr expr;
9536 /* If the next token is the `throw' keyword, then we're looking at
9537 a throw-expression. */
9538 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9539 expr = cp_parser_throw_expression (parser);
9540 /* Otherwise, it must be that we are looking at a
9541 logical-or-expression. */
9542 else
9544 /* Parse the binary expressions (logical-or-expression). */
9545 expr = cp_parser_binary_expression (parser, cast_p, false,
9546 decltype_p,
9547 PREC_NOT_OPERATOR, pidk);
9548 /* If the next token is a `?' then we're actually looking at a
9549 conditional-expression. */
9550 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9551 return cp_parser_question_colon_clause (parser, expr);
9552 else
9554 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9556 /* If it's an assignment-operator, we're using the second
9557 production. */
9558 enum tree_code assignment_operator
9559 = cp_parser_assignment_operator_opt (parser);
9560 if (assignment_operator != ERROR_MARK)
9562 bool non_constant_p;
9564 /* Parse the right-hand side of the assignment. */
9565 cp_expr rhs = cp_parser_initializer_clause (parser,
9566 &non_constant_p);
9568 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9569 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9571 /* An assignment may not appear in a
9572 constant-expression. */
9573 if (cp_parser_non_integral_constant_expression (parser,
9574 NIC_ASSIGNMENT))
9575 return error_mark_node;
9576 /* Build the assignment expression. Its default
9577 location:
9578 LHS = RHS
9579 ~~~~^~~~~
9580 is the location of the '=' token as the
9581 caret, ranging from the start of the lhs to the
9582 end of the rhs. */
9583 loc = make_location (loc,
9584 expr.get_start (),
9585 rhs.get_finish ());
9586 expr = build_x_modify_expr (loc, expr,
9587 assignment_operator,
9588 rhs,
9589 complain_flags (decltype_p));
9590 /* TODO: build_x_modify_expr doesn't honor the location,
9591 so we must set it here. */
9592 expr.set_location (loc);
9597 return expr;
9600 /* Parse an (optional) assignment-operator.
9602 assignment-operator: one of
9603 = *= /= %= += -= >>= <<= &= ^= |=
9605 GNU Extension:
9607 assignment-operator: one of
9608 <?= >?=
9610 If the next token is an assignment operator, the corresponding tree
9611 code is returned, and the token is consumed. For example, for
9612 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9613 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9614 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9615 operator, ERROR_MARK is returned. */
9617 static enum tree_code
9618 cp_parser_assignment_operator_opt (cp_parser* parser)
9620 enum tree_code op;
9621 cp_token *token;
9623 /* Peek at the next token. */
9624 token = cp_lexer_peek_token (parser->lexer);
9626 switch (token->type)
9628 case CPP_EQ:
9629 op = NOP_EXPR;
9630 break;
9632 case CPP_MULT_EQ:
9633 op = MULT_EXPR;
9634 break;
9636 case CPP_DIV_EQ:
9637 op = TRUNC_DIV_EXPR;
9638 break;
9640 case CPP_MOD_EQ:
9641 op = TRUNC_MOD_EXPR;
9642 break;
9644 case CPP_PLUS_EQ:
9645 op = PLUS_EXPR;
9646 break;
9648 case CPP_MINUS_EQ:
9649 op = MINUS_EXPR;
9650 break;
9652 case CPP_RSHIFT_EQ:
9653 op = RSHIFT_EXPR;
9654 break;
9656 case CPP_LSHIFT_EQ:
9657 op = LSHIFT_EXPR;
9658 break;
9660 case CPP_AND_EQ:
9661 op = BIT_AND_EXPR;
9662 break;
9664 case CPP_XOR_EQ:
9665 op = BIT_XOR_EXPR;
9666 break;
9668 case CPP_OR_EQ:
9669 op = BIT_IOR_EXPR;
9670 break;
9672 default:
9673 /* Nothing else is an assignment operator. */
9674 op = ERROR_MARK;
9677 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9678 if (op != ERROR_MARK
9679 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9680 op = ERROR_MARK;
9682 /* If it was an assignment operator, consume it. */
9683 if (op != ERROR_MARK)
9684 cp_lexer_consume_token (parser->lexer);
9686 return op;
9689 /* Parse an expression.
9691 expression:
9692 assignment-expression
9693 expression , assignment-expression
9695 CAST_P is true if this expression is the target of a cast.
9696 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9697 except possibly parenthesized or on the RHS of a comma (N3276).
9699 Returns a representation of the expression. */
9701 static cp_expr
9702 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9703 bool cast_p, bool decltype_p)
9705 cp_expr expression = NULL_TREE;
9706 location_t loc = UNKNOWN_LOCATION;
9708 while (true)
9710 cp_expr assignment_expression;
9712 /* Parse the next assignment-expression. */
9713 assignment_expression
9714 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9716 /* We don't create a temporary for a call that is the immediate operand
9717 of decltype or on the RHS of a comma. But when we see a comma, we
9718 need to create a temporary for a call on the LHS. */
9719 if (decltype_p && !processing_template_decl
9720 && TREE_CODE (assignment_expression) == CALL_EXPR
9721 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9722 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9723 assignment_expression
9724 = build_cplus_new (TREE_TYPE (assignment_expression),
9725 assignment_expression, tf_warning_or_error);
9727 /* If this is the first assignment-expression, we can just
9728 save it away. */
9729 if (!expression)
9730 expression = assignment_expression;
9731 else
9733 /* Create a location with caret at the comma, ranging
9734 from the start of the LHS to the end of the RHS. */
9735 loc = make_location (loc,
9736 expression.get_start (),
9737 assignment_expression.get_finish ());
9738 expression = build_x_compound_expr (loc, expression,
9739 assignment_expression,
9740 complain_flags (decltype_p));
9741 expression.set_location (loc);
9743 /* If the next token is not a comma, or we're in a fold-expression, then
9744 we are done with the expression. */
9745 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9746 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9747 break;
9748 /* Consume the `,'. */
9749 loc = cp_lexer_peek_token (parser->lexer)->location;
9750 cp_lexer_consume_token (parser->lexer);
9751 /* A comma operator cannot appear in a constant-expression. */
9752 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9753 expression = error_mark_node;
9756 return expression;
9759 /* Parse a constant-expression.
9761 constant-expression:
9762 conditional-expression
9764 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9765 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9766 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9767 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9768 only parse a conditional-expression, otherwise parse an
9769 assignment-expression. See below for rationale. */
9771 static cp_expr
9772 cp_parser_constant_expression (cp_parser* parser,
9773 bool allow_non_constant_p,
9774 bool *non_constant_p,
9775 bool strict_p)
9777 bool saved_integral_constant_expression_p;
9778 bool saved_allow_non_integral_constant_expression_p;
9779 bool saved_non_integral_constant_expression_p;
9780 cp_expr expression;
9782 /* It might seem that we could simply parse the
9783 conditional-expression, and then check to see if it were
9784 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9785 one that the compiler can figure out is constant, possibly after
9786 doing some simplifications or optimizations. The standard has a
9787 precise definition of constant-expression, and we must honor
9788 that, even though it is somewhat more restrictive.
9790 For example:
9792 int i[(2, 3)];
9794 is not a legal declaration, because `(2, 3)' is not a
9795 constant-expression. The `,' operator is forbidden in a
9796 constant-expression. However, GCC's constant-folding machinery
9797 will fold this operation to an INTEGER_CST for `3'. */
9799 /* Save the old settings. */
9800 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9801 saved_allow_non_integral_constant_expression_p
9802 = parser->allow_non_integral_constant_expression_p;
9803 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9804 /* We are now parsing a constant-expression. */
9805 parser->integral_constant_expression_p = true;
9806 parser->allow_non_integral_constant_expression_p
9807 = (allow_non_constant_p || cxx_dialect >= cxx11);
9808 parser->non_integral_constant_expression_p = false;
9809 /* Although the grammar says "conditional-expression", when not STRICT_P,
9810 we parse an "assignment-expression", which also permits
9811 "throw-expression" and the use of assignment operators. In the case
9812 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9813 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9814 actually essential that we look for an assignment-expression.
9815 For example, cp_parser_initializer_clauses uses this function to
9816 determine whether a particular assignment-expression is in fact
9817 constant. */
9818 if (strict_p)
9820 /* Parse the binary expressions (logical-or-expression). */
9821 expression = cp_parser_binary_expression (parser, false, false, false,
9822 PREC_NOT_OPERATOR, NULL);
9823 /* If the next token is a `?' then we're actually looking at
9824 a conditional-expression; otherwise we're done. */
9825 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9826 expression = cp_parser_question_colon_clause (parser, expression);
9828 else
9829 expression = cp_parser_assignment_expression (parser);
9830 /* Restore the old settings. */
9831 parser->integral_constant_expression_p
9832 = saved_integral_constant_expression_p;
9833 parser->allow_non_integral_constant_expression_p
9834 = saved_allow_non_integral_constant_expression_p;
9835 if (cxx_dialect >= cxx11)
9837 /* Require an rvalue constant expression here; that's what our
9838 callers expect. Reference constant expressions are handled
9839 separately in e.g. cp_parser_template_argument. */
9840 tree decay = expression;
9841 if (TREE_TYPE (expression)
9842 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9843 decay = build_address (expression);
9844 bool is_const = potential_rvalue_constant_expression (decay);
9845 parser->non_integral_constant_expression_p = !is_const;
9846 if (!is_const && !allow_non_constant_p)
9847 require_potential_rvalue_constant_expression (decay);
9849 if (allow_non_constant_p)
9850 *non_constant_p = parser->non_integral_constant_expression_p;
9851 parser->non_integral_constant_expression_p
9852 = saved_non_integral_constant_expression_p;
9854 return expression;
9857 /* Parse __builtin_offsetof.
9859 offsetof-expression:
9860 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9862 offsetof-member-designator:
9863 id-expression
9864 | offsetof-member-designator "." id-expression
9865 | offsetof-member-designator "[" expression "]"
9866 | offsetof-member-designator "->" id-expression */
9868 static cp_expr
9869 cp_parser_builtin_offsetof (cp_parser *parser)
9871 int save_ice_p, save_non_ice_p;
9872 tree type;
9873 cp_expr expr;
9874 cp_id_kind dummy;
9875 cp_token *token;
9876 location_t finish_loc;
9878 /* We're about to accept non-integral-constant things, but will
9879 definitely yield an integral constant expression. Save and
9880 restore these values around our local parsing. */
9881 save_ice_p = parser->integral_constant_expression_p;
9882 save_non_ice_p = parser->non_integral_constant_expression_p;
9884 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9886 /* Consume the "__builtin_offsetof" token. */
9887 cp_lexer_consume_token (parser->lexer);
9888 /* Consume the opening `('. */
9889 matching_parens parens;
9890 parens.require_open (parser);
9891 /* Parse the type-id. */
9892 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9894 const char *saved_message = parser->type_definition_forbidden_message;
9895 parser->type_definition_forbidden_message
9896 = G_("types may not be defined within __builtin_offsetof");
9897 type = cp_parser_type_id (parser);
9898 parser->type_definition_forbidden_message = saved_message;
9900 /* Look for the `,'. */
9901 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9902 token = cp_lexer_peek_token (parser->lexer);
9904 /* Build the (type *)null that begins the traditional offsetof macro. */
9905 tree object_ptr
9906 = build_static_cast (build_pointer_type (type), null_pointer_node,
9907 tf_warning_or_error);
9909 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9910 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9911 true, &dummy, token->location);
9912 while (true)
9914 token = cp_lexer_peek_token (parser->lexer);
9915 switch (token->type)
9917 case CPP_OPEN_SQUARE:
9918 /* offsetof-member-designator "[" expression "]" */
9919 expr = cp_parser_postfix_open_square_expression (parser, expr,
9920 true, false);
9921 break;
9923 case CPP_DEREF:
9924 /* offsetof-member-designator "->" identifier */
9925 expr = grok_array_decl (token->location, expr,
9926 integer_zero_node, false);
9927 /* FALLTHRU */
9929 case CPP_DOT:
9930 /* offsetof-member-designator "." identifier */
9931 cp_lexer_consume_token (parser->lexer);
9932 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9933 expr, true, &dummy,
9934 token->location);
9935 break;
9937 case CPP_CLOSE_PAREN:
9938 /* Consume the ")" token. */
9939 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9940 cp_lexer_consume_token (parser->lexer);
9941 goto success;
9943 default:
9944 /* Error. We know the following require will fail, but
9945 that gives the proper error message. */
9946 parens.require_close (parser);
9947 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9948 expr = error_mark_node;
9949 goto failure;
9953 success:
9954 /* Make a location of the form:
9955 __builtin_offsetof (struct s, f)
9956 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9957 with caret at the type-id, ranging from the start of the
9958 "_builtin_offsetof" token to the close paren. */
9959 loc = make_location (loc, start_loc, finish_loc);
9960 /* The result will be an INTEGER_CST, so we need to explicitly
9961 preserve the location. */
9962 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9964 failure:
9965 parser->integral_constant_expression_p = save_ice_p;
9966 parser->non_integral_constant_expression_p = save_non_ice_p;
9968 expr = expr.maybe_add_location_wrapper ();
9969 return expr;
9972 /* Parse a trait expression.
9974 Returns a representation of the expression, the underlying type
9975 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9977 static cp_expr
9978 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9980 cp_trait_kind kind;
9981 tree type1, type2 = NULL_TREE;
9982 bool binary = false;
9983 bool variadic = false;
9985 switch (keyword)
9987 case RID_HAS_NOTHROW_ASSIGN:
9988 kind = CPTK_HAS_NOTHROW_ASSIGN;
9989 break;
9990 case RID_HAS_NOTHROW_CONSTRUCTOR:
9991 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9992 break;
9993 case RID_HAS_NOTHROW_COPY:
9994 kind = CPTK_HAS_NOTHROW_COPY;
9995 break;
9996 case RID_HAS_TRIVIAL_ASSIGN:
9997 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9998 break;
9999 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10000 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10001 break;
10002 case RID_HAS_TRIVIAL_COPY:
10003 kind = CPTK_HAS_TRIVIAL_COPY;
10004 break;
10005 case RID_HAS_TRIVIAL_DESTRUCTOR:
10006 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10007 break;
10008 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10009 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10010 break;
10011 case RID_HAS_VIRTUAL_DESTRUCTOR:
10012 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10013 break;
10014 case RID_IS_ABSTRACT:
10015 kind = CPTK_IS_ABSTRACT;
10016 break;
10017 case RID_IS_AGGREGATE:
10018 kind = CPTK_IS_AGGREGATE;
10019 break;
10020 case RID_IS_BASE_OF:
10021 kind = CPTK_IS_BASE_OF;
10022 binary = true;
10023 break;
10024 case RID_IS_CLASS:
10025 kind = CPTK_IS_CLASS;
10026 break;
10027 case RID_IS_EMPTY:
10028 kind = CPTK_IS_EMPTY;
10029 break;
10030 case RID_IS_ENUM:
10031 kind = CPTK_IS_ENUM;
10032 break;
10033 case RID_IS_FINAL:
10034 kind = CPTK_IS_FINAL;
10035 break;
10036 case RID_IS_LITERAL_TYPE:
10037 kind = CPTK_IS_LITERAL_TYPE;
10038 break;
10039 case RID_IS_POD:
10040 kind = CPTK_IS_POD;
10041 break;
10042 case RID_IS_POLYMORPHIC:
10043 kind = CPTK_IS_POLYMORPHIC;
10044 break;
10045 case RID_IS_SAME_AS:
10046 kind = CPTK_IS_SAME_AS;
10047 binary = true;
10048 break;
10049 case RID_IS_STD_LAYOUT:
10050 kind = CPTK_IS_STD_LAYOUT;
10051 break;
10052 case RID_IS_TRIVIAL:
10053 kind = CPTK_IS_TRIVIAL;
10054 break;
10055 case RID_IS_TRIVIALLY_ASSIGNABLE:
10056 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10057 binary = true;
10058 break;
10059 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10060 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10061 variadic = true;
10062 break;
10063 case RID_IS_TRIVIALLY_COPYABLE:
10064 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10065 break;
10066 case RID_IS_UNION:
10067 kind = CPTK_IS_UNION;
10068 break;
10069 case RID_UNDERLYING_TYPE:
10070 kind = CPTK_UNDERLYING_TYPE;
10071 break;
10072 case RID_BASES:
10073 kind = CPTK_BASES;
10074 break;
10075 case RID_DIRECT_BASES:
10076 kind = CPTK_DIRECT_BASES;
10077 break;
10078 case RID_IS_ASSIGNABLE:
10079 kind = CPTK_IS_ASSIGNABLE;
10080 binary = true;
10081 break;
10082 case RID_IS_CONSTRUCTIBLE:
10083 kind = CPTK_IS_CONSTRUCTIBLE;
10084 variadic = true;
10085 break;
10086 default:
10087 gcc_unreachable ();
10090 /* Get location of initial token. */
10091 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10093 /* Consume the token. */
10094 cp_lexer_consume_token (parser->lexer);
10096 matching_parens parens;
10097 parens.require_open (parser);
10100 type_id_in_expr_sentinel s (parser);
10101 type1 = cp_parser_type_id (parser);
10104 if (type1 == error_mark_node)
10105 return error_mark_node;
10107 if (binary)
10109 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10112 type_id_in_expr_sentinel s (parser);
10113 type2 = cp_parser_type_id (parser);
10116 if (type2 == error_mark_node)
10117 return error_mark_node;
10119 else if (variadic)
10121 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10123 cp_lexer_consume_token (parser->lexer);
10124 tree elt = cp_parser_type_id (parser);
10125 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10127 cp_lexer_consume_token (parser->lexer);
10128 elt = make_pack_expansion (elt);
10130 if (elt == error_mark_node)
10131 return error_mark_node;
10132 type2 = tree_cons (NULL_TREE, elt, type2);
10136 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10137 parens.require_close (parser);
10139 /* Construct a location of the form:
10140 __is_trivially_copyable(_Tp)
10141 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10142 with start == caret, finishing at the close-paren. */
10143 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10145 /* Complete the trait expression, which may mean either processing
10146 the trait expr now or saving it for template instantiation. */
10147 switch (kind)
10149 case CPTK_UNDERLYING_TYPE:
10150 return cp_expr (finish_underlying_type (type1), trait_loc);
10151 case CPTK_BASES:
10152 return cp_expr (finish_bases (type1, false), trait_loc);
10153 case CPTK_DIRECT_BASES:
10154 return cp_expr (finish_bases (type1, true), trait_loc);
10155 default:
10156 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10160 /* Parse a lambda expression.
10162 lambda-expression:
10163 lambda-introducer lambda-declarator [opt] compound-statement
10165 Returns a representation of the expression. */
10167 static cp_expr
10168 cp_parser_lambda_expression (cp_parser* parser)
10170 tree lambda_expr = build_lambda_expr ();
10171 tree type;
10172 bool ok = true;
10173 cp_token *token = cp_lexer_peek_token (parser->lexer);
10174 cp_token_position start = 0;
10176 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10178 if (cp_unevaluated_operand)
10180 if (!token->error_reported)
10182 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10183 "lambda-expression in unevaluated context");
10184 token->error_reported = true;
10186 ok = false;
10188 else if (parser->in_template_argument_list_p)
10190 if (!token->error_reported)
10192 error_at (token->location, "lambda-expression in template-argument");
10193 token->error_reported = true;
10195 ok = false;
10198 /* We may be in the middle of deferred access check. Disable
10199 it now. */
10200 push_deferring_access_checks (dk_no_deferred);
10202 cp_parser_lambda_introducer (parser, lambda_expr);
10204 type = begin_lambda_type (lambda_expr);
10205 if (type == error_mark_node)
10206 return error_mark_node;
10208 record_lambda_scope (lambda_expr);
10210 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10211 determine_visibility (TYPE_NAME (type));
10213 /* Now that we've started the type, add the capture fields for any
10214 explicit captures. */
10215 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10218 /* Inside the class, surrounding template-parameter-lists do not apply. */
10219 unsigned int saved_num_template_parameter_lists
10220 = parser->num_template_parameter_lists;
10221 unsigned char in_statement = parser->in_statement;
10222 bool in_switch_statement_p = parser->in_switch_statement_p;
10223 bool fully_implicit_function_template_p
10224 = parser->fully_implicit_function_template_p;
10225 tree implicit_template_parms = parser->implicit_template_parms;
10226 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10227 bool auto_is_implicit_function_template_parm_p
10228 = parser->auto_is_implicit_function_template_parm_p;
10230 parser->num_template_parameter_lists = 0;
10231 parser->in_statement = 0;
10232 parser->in_switch_statement_p = false;
10233 parser->fully_implicit_function_template_p = false;
10234 parser->implicit_template_parms = 0;
10235 parser->implicit_template_scope = 0;
10236 parser->auto_is_implicit_function_template_parm_p = false;
10238 /* By virtue of defining a local class, a lambda expression has access to
10239 the private variables of enclosing classes. */
10241 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10243 if (ok && cp_parser_error_occurred (parser))
10244 ok = false;
10246 if (ok)
10248 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10249 && cp_parser_start_tentative_firewall (parser))
10250 start = token;
10251 cp_parser_lambda_body (parser, lambda_expr);
10253 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10255 if (cp_parser_skip_to_closing_brace (parser))
10256 cp_lexer_consume_token (parser->lexer);
10259 /* The capture list was built up in reverse order; fix that now. */
10260 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10261 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10263 if (ok)
10264 maybe_add_lambda_conv_op (type);
10266 type = finish_struct (type, /*attributes=*/NULL_TREE);
10268 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10269 parser->in_statement = in_statement;
10270 parser->in_switch_statement_p = in_switch_statement_p;
10271 parser->fully_implicit_function_template_p
10272 = fully_implicit_function_template_p;
10273 parser->implicit_template_parms = implicit_template_parms;
10274 parser->implicit_template_scope = implicit_template_scope;
10275 parser->auto_is_implicit_function_template_parm_p
10276 = auto_is_implicit_function_template_parm_p;
10279 /* This field is only used during parsing of the lambda. */
10280 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10282 /* This lambda shouldn't have any proxies left at this point. */
10283 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10284 /* And now that we're done, push proxies for an enclosing lambda. */
10285 insert_pending_capture_proxies ();
10287 /* Update the lambda expression to a range. */
10288 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10289 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10290 token->location,
10291 end_tok->location);
10293 if (ok)
10294 lambda_expr = build_lambda_object (lambda_expr);
10295 else
10296 lambda_expr = error_mark_node;
10298 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10300 pop_deferring_access_checks ();
10302 return lambda_expr;
10305 /* Parse the beginning of a lambda expression.
10307 lambda-introducer:
10308 [ lambda-capture [opt] ]
10310 LAMBDA_EXPR is the current representation of the lambda expression. */
10312 static void
10313 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10315 /* Need commas after the first capture. */
10316 bool first = true;
10318 /* Eat the leading `['. */
10319 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10321 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10322 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10323 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10324 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10325 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10326 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10328 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10330 cp_lexer_consume_token (parser->lexer);
10331 first = false;
10333 if (!(at_function_scope_p () || parsing_nsdmi ()))
10334 error ("non-local lambda expression cannot have a capture-default");
10337 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10339 cp_token* capture_token;
10340 tree capture_id;
10341 tree capture_init_expr;
10342 cp_id_kind idk = CP_ID_KIND_NONE;
10343 bool explicit_init_p = false;
10345 enum capture_kind_type
10347 BY_COPY,
10348 BY_REFERENCE
10350 enum capture_kind_type capture_kind = BY_COPY;
10352 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10354 error ("expected end of capture-list");
10355 return;
10358 if (first)
10359 first = false;
10360 else
10361 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10363 /* Possibly capture `this'. */
10364 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10366 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10367 if (cxx_dialect < cxx2a
10368 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10369 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10370 "with by-copy capture default");
10371 cp_lexer_consume_token (parser->lexer);
10372 add_capture (lambda_expr,
10373 /*id=*/this_identifier,
10374 /*initializer=*/finish_this_expr (),
10375 /*by_reference_p=*/true,
10376 explicit_init_p);
10377 continue;
10380 /* Possibly capture `*this'. */
10381 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10382 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10384 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10385 if (cxx_dialect < cxx17)
10386 pedwarn (loc, 0, "%<*this%> capture only available with "
10387 "-std=c++17 or -std=gnu++17");
10388 cp_lexer_consume_token (parser->lexer);
10389 cp_lexer_consume_token (parser->lexer);
10390 add_capture (lambda_expr,
10391 /*id=*/this_identifier,
10392 /*initializer=*/finish_this_expr (),
10393 /*by_reference_p=*/false,
10394 explicit_init_p);
10395 continue;
10398 /* Remember whether we want to capture as a reference or not. */
10399 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10401 capture_kind = BY_REFERENCE;
10402 cp_lexer_consume_token (parser->lexer);
10405 /* Get the identifier. */
10406 capture_token = cp_lexer_peek_token (parser->lexer);
10407 capture_id = cp_parser_identifier (parser);
10409 if (capture_id == error_mark_node)
10410 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10411 delimiters, but I modified this to stop on unnested ']' as well. It
10412 was already changed to stop on unnested '}', so the
10413 "closing_parenthesis" name is no more misleading with my change. */
10415 cp_parser_skip_to_closing_parenthesis (parser,
10416 /*recovering=*/true,
10417 /*or_comma=*/true,
10418 /*consume_paren=*/true);
10419 break;
10422 /* Find the initializer for this capture. */
10423 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10424 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10425 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10427 bool direct, non_constant;
10428 /* An explicit initializer exists. */
10429 if (cxx_dialect < cxx14)
10430 pedwarn (input_location, 0,
10431 "lambda capture initializers "
10432 "only available with -std=c++14 or -std=gnu++14");
10433 capture_init_expr = cp_parser_initializer (parser, &direct,
10434 &non_constant, true);
10435 explicit_init_p = true;
10436 if (capture_init_expr == NULL_TREE)
10438 error ("empty initializer for lambda init-capture");
10439 capture_init_expr = error_mark_node;
10442 else
10444 const char* error_msg;
10446 /* Turn the identifier into an id-expression. */
10447 capture_init_expr
10448 = cp_parser_lookup_name_simple (parser, capture_id,
10449 capture_token->location);
10451 if (capture_init_expr == error_mark_node)
10453 unqualified_name_lookup_error (capture_id);
10454 continue;
10456 else if (!VAR_P (capture_init_expr)
10457 && TREE_CODE (capture_init_expr) != PARM_DECL)
10459 error_at (capture_token->location,
10460 "capture of non-variable %qE",
10461 capture_init_expr);
10462 if (DECL_P (capture_init_expr))
10463 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10464 "%q#D declared here", capture_init_expr);
10465 continue;
10467 if (VAR_P (capture_init_expr)
10468 && decl_storage_duration (capture_init_expr) != dk_auto)
10470 if (pedwarn (capture_token->location, 0, "capture of variable "
10471 "%qD with non-automatic storage duration",
10472 capture_init_expr))
10473 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10474 "%q#D declared here", capture_init_expr);
10475 continue;
10478 capture_init_expr
10479 = finish_id_expression
10480 (capture_id,
10481 capture_init_expr,
10482 parser->scope,
10483 &idk,
10484 /*integral_constant_expression_p=*/false,
10485 /*allow_non_integral_constant_expression_p=*/false,
10486 /*non_integral_constant_expression_p=*/NULL,
10487 /*template_p=*/false,
10488 /*done=*/true,
10489 /*address_p=*/false,
10490 /*template_arg_p=*/false,
10491 &error_msg,
10492 capture_token->location);
10494 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10496 cp_lexer_consume_token (parser->lexer);
10497 capture_init_expr = make_pack_expansion (capture_init_expr);
10501 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10502 && !explicit_init_p)
10504 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10505 && capture_kind == BY_COPY)
10506 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10507 "of %qD redundant with by-copy capture default",
10508 capture_id);
10509 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10510 && capture_kind == BY_REFERENCE)
10511 pedwarn (capture_token->location, 0, "explicit by-reference "
10512 "capture of %qD redundant with by-reference capture "
10513 "default", capture_id);
10516 add_capture (lambda_expr,
10517 capture_id,
10518 capture_init_expr,
10519 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10520 explicit_init_p);
10522 /* If there is any qualification still in effect, clear it
10523 now; we will be starting fresh with the next capture. */
10524 parser->scope = NULL_TREE;
10525 parser->qualifying_scope = NULL_TREE;
10526 parser->object_scope = NULL_TREE;
10529 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10532 /* Parse the (optional) middle of a lambda expression.
10534 lambda-declarator:
10535 < template-parameter-list [opt] >
10536 ( parameter-declaration-clause [opt] )
10537 attribute-specifier [opt]
10538 decl-specifier-seq [opt]
10539 exception-specification [opt]
10540 lambda-return-type-clause [opt]
10542 LAMBDA_EXPR is the current representation of the lambda expression. */
10544 static bool
10545 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10547 /* 5.1.1.4 of the standard says:
10548 If a lambda-expression does not include a lambda-declarator, it is as if
10549 the lambda-declarator were ().
10550 This means an empty parameter list, no attributes, and no exception
10551 specification. */
10552 tree param_list = void_list_node;
10553 tree attributes = NULL_TREE;
10554 tree exception_spec = NULL_TREE;
10555 tree template_param_list = NULL_TREE;
10556 tree tx_qual = NULL_TREE;
10557 tree return_type = NULL_TREE;
10558 cp_decl_specifier_seq lambda_specs;
10559 clear_decl_specs (&lambda_specs);
10561 /* The template-parameter-list is optional, but must begin with
10562 an opening angle if present. */
10563 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10565 if (cxx_dialect < cxx14)
10566 pedwarn (parser->lexer->next_token->location, 0,
10567 "lambda templates are only available with "
10568 "-std=c++14 or -std=gnu++14");
10569 else if (cxx_dialect < cxx2a)
10570 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10571 "lambda templates are only available with "
10572 "-std=c++2a or -std=gnu++2a");
10574 cp_lexer_consume_token (parser->lexer);
10576 template_param_list = cp_parser_template_parameter_list (parser);
10578 cp_parser_skip_to_end_of_template_parameter_list (parser);
10580 /* We just processed one more parameter list. */
10581 ++parser->num_template_parameter_lists;
10584 /* The parameter-declaration-clause is optional (unless
10585 template-parameter-list was given), but must begin with an
10586 opening parenthesis if present. */
10587 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10589 matching_parens parens;
10590 parens.consume_open (parser);
10592 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10594 /* Parse parameters. */
10595 param_list = cp_parser_parameter_declaration_clause (parser);
10597 /* Default arguments shall not be specified in the
10598 parameter-declaration-clause of a lambda-declarator. */
10599 if (cxx_dialect < cxx14)
10600 for (tree t = param_list; t; t = TREE_CHAIN (t))
10601 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10602 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10603 "default argument specified for lambda parameter");
10605 parens.require_close (parser);
10607 /* In the decl-specifier-seq of the lambda-declarator, each
10608 decl-specifier shall either be mutable or constexpr. */
10609 int declares_class_or_enum;
10610 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10611 cp_parser_decl_specifier_seq (parser,
10612 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10613 &lambda_specs, &declares_class_or_enum);
10614 if (lambda_specs.storage_class == sc_mutable)
10616 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10617 if (lambda_specs.conflicting_specifiers_p)
10618 error_at (lambda_specs.locations[ds_storage_class],
10619 "duplicate %<mutable%>");
10622 tx_qual = cp_parser_tx_qualifier_opt (parser);
10624 /* Parse optional exception specification. */
10625 exception_spec = cp_parser_exception_specification_opt (parser);
10627 attributes = cp_parser_std_attribute_spec_seq (parser);
10629 /* Parse optional trailing return type. */
10630 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10632 cp_lexer_consume_token (parser->lexer);
10633 return_type = cp_parser_trailing_type_id (parser);
10636 /* The function parameters must be in scope all the way until after the
10637 trailing-return-type in case of decltype. */
10638 pop_bindings_and_leave_scope ();
10640 else if (template_param_list != NULL_TREE) // generate diagnostic
10641 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10643 /* Create the function call operator.
10645 Messing with declarators like this is no uglier than building up the
10646 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10647 other code. */
10649 cp_decl_specifier_seq return_type_specs;
10650 cp_declarator* declarator;
10651 tree fco;
10652 int quals;
10653 void *p;
10655 clear_decl_specs (&return_type_specs);
10656 return_type_specs.type = make_auto ();
10658 if (lambda_specs.locations[ds_constexpr])
10660 if (cxx_dialect >= cxx17)
10661 return_type_specs.locations[ds_constexpr]
10662 = lambda_specs.locations[ds_constexpr];
10663 else
10664 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10665 "lambda only available with -std=c++17 or -std=gnu++17");
10668 p = obstack_alloc (&declarator_obstack, 0);
10670 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10672 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10673 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10674 declarator = make_call_declarator (declarator, param_list, quals,
10675 VIRT_SPEC_UNSPECIFIED,
10676 REF_QUAL_NONE,
10677 tx_qual,
10678 exception_spec,
10679 return_type,
10680 /*requires_clause*/NULL_TREE);
10681 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10682 declarator->std_attributes = attributes;
10684 fco = grokmethod (&return_type_specs,
10685 declarator,
10686 NULL_TREE);
10687 if (fco != error_mark_node)
10689 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10690 DECL_ARTIFICIAL (fco) = 1;
10691 /* Give the object parameter a different name. */
10692 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10693 DECL_LAMBDA_FUNCTION (fco) = 1;
10695 if (template_param_list)
10697 fco = finish_member_template_decl (fco);
10698 finish_template_decl (template_param_list);
10699 --parser->num_template_parameter_lists;
10701 else if (parser->fully_implicit_function_template_p)
10702 fco = finish_fully_implicit_template (parser, fco);
10704 finish_member_declaration (fco);
10706 obstack_free (&declarator_obstack, p);
10708 return (fco != error_mark_node);
10712 /* Parse the body of a lambda expression, which is simply
10714 compound-statement
10716 but which requires special handling.
10717 LAMBDA_EXPR is the current representation of the lambda expression. */
10719 static void
10720 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10722 bool nested = (current_function_decl != NULL_TREE);
10723 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10724 bool in_function_body = parser->in_function_body;
10726 if (nested)
10727 push_function_context ();
10728 else
10729 /* Still increment function_depth so that we don't GC in the
10730 middle of an expression. */
10731 ++function_depth;
10733 vec<tree> omp_privatization_save;
10734 save_omp_privatization_clauses (omp_privatization_save);
10735 /* Clear this in case we're in the middle of a default argument. */
10736 parser->local_variables_forbidden_p = false;
10737 parser->in_function_body = true;
10740 local_specialization_stack s (lss_copy);
10741 tree fco = lambda_function (lambda_expr);
10742 tree body = start_lambda_function (fco, lambda_expr);
10743 matching_braces braces;
10745 if (braces.require_open (parser))
10747 tree compound_stmt = begin_compound_stmt (0);
10749 /* Originally C++11 required us to peek for 'return expr'; and
10750 process it specially here to deduce the return type. N3638
10751 removed the need for that. */
10753 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10754 cp_parser_label_declaration (parser);
10755 cp_parser_statement_seq_opt (parser, NULL_TREE);
10756 braces.require_close (parser);
10758 finish_compound_stmt (compound_stmt);
10761 finish_lambda_function (body);
10764 restore_omp_privatization_clauses (omp_privatization_save);
10765 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10766 parser->in_function_body = in_function_body;
10767 if (nested)
10768 pop_function_context();
10769 else
10770 --function_depth;
10773 /* Statements [gram.stmt.stmt] */
10775 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10777 static void
10778 add_debug_begin_stmt (location_t loc)
10780 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10781 return;
10782 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10783 /* A concept is never expanded normally. */
10784 return;
10786 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10787 SET_EXPR_LOCATION (stmt, loc);
10788 add_stmt (stmt);
10791 /* Parse a statement.
10793 statement:
10794 labeled-statement
10795 expression-statement
10796 compound-statement
10797 selection-statement
10798 iteration-statement
10799 jump-statement
10800 declaration-statement
10801 try-block
10803 C++11:
10805 statement:
10806 labeled-statement
10807 attribute-specifier-seq (opt) expression-statement
10808 attribute-specifier-seq (opt) compound-statement
10809 attribute-specifier-seq (opt) selection-statement
10810 attribute-specifier-seq (opt) iteration-statement
10811 attribute-specifier-seq (opt) jump-statement
10812 declaration-statement
10813 attribute-specifier-seq (opt) try-block
10815 init-statement:
10816 expression-statement
10817 simple-declaration
10819 TM Extension:
10821 statement:
10822 atomic-statement
10824 IN_COMPOUND is true when the statement is nested inside a
10825 cp_parser_compound_statement; this matters for certain pragmas.
10827 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10828 is a (possibly labeled) if statement which is not enclosed in braces
10829 and has an else clause. This is used to implement -Wparentheses.
10831 CHAIN is a vector of if-else-if conditions. */
10833 static void
10834 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10835 bool in_compound, bool *if_p, vec<tree> *chain,
10836 location_t *loc_after_labels)
10838 tree statement, std_attrs = NULL_TREE;
10839 cp_token *token;
10840 location_t statement_location, attrs_location;
10842 restart:
10843 if (if_p != NULL)
10844 *if_p = false;
10845 /* There is no statement yet. */
10846 statement = NULL_TREE;
10848 saved_token_sentinel saved_tokens (parser->lexer);
10849 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10850 if (c_dialect_objc ())
10851 /* In obj-c++, seeing '[[' might be the either the beginning of
10852 c++11 attributes, or a nested objc-message-expression. So
10853 let's parse the c++11 attributes tentatively. */
10854 cp_parser_parse_tentatively (parser);
10855 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10856 if (c_dialect_objc ())
10858 if (!cp_parser_parse_definitely (parser))
10859 std_attrs = NULL_TREE;
10862 /* Peek at the next token. */
10863 token = cp_lexer_peek_token (parser->lexer);
10864 /* Remember the location of the first token in the statement. */
10865 statement_location = token->location;
10866 add_debug_begin_stmt (statement_location);
10867 /* If this is a keyword, then that will often determine what kind of
10868 statement we have. */
10869 if (token->type == CPP_KEYWORD)
10871 enum rid keyword = token->keyword;
10873 switch (keyword)
10875 case RID_CASE:
10876 case RID_DEFAULT:
10877 /* Looks like a labeled-statement with a case label.
10878 Parse the label, and then use tail recursion to parse
10879 the statement. */
10880 cp_parser_label_for_labeled_statement (parser, std_attrs);
10881 in_compound = false;
10882 goto restart;
10884 case RID_IF:
10885 case RID_SWITCH:
10886 statement = cp_parser_selection_statement (parser, if_p, chain);
10887 break;
10889 case RID_WHILE:
10890 case RID_DO:
10891 case RID_FOR:
10892 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10893 break;
10895 case RID_BREAK:
10896 case RID_CONTINUE:
10897 case RID_RETURN:
10898 case RID_GOTO:
10899 statement = cp_parser_jump_statement (parser);
10900 break;
10902 /* Objective-C++ exception-handling constructs. */
10903 case RID_AT_TRY:
10904 case RID_AT_CATCH:
10905 case RID_AT_FINALLY:
10906 case RID_AT_SYNCHRONIZED:
10907 case RID_AT_THROW:
10908 statement = cp_parser_objc_statement (parser);
10909 break;
10911 case RID_TRY:
10912 statement = cp_parser_try_block (parser);
10913 break;
10915 case RID_NAMESPACE:
10916 /* This must be a namespace alias definition. */
10917 cp_parser_declaration_statement (parser);
10918 return;
10920 case RID_TRANSACTION_ATOMIC:
10921 case RID_TRANSACTION_RELAXED:
10922 case RID_SYNCHRONIZED:
10923 case RID_ATOMIC_NOEXCEPT:
10924 case RID_ATOMIC_CANCEL:
10925 statement = cp_parser_transaction (parser, token);
10926 break;
10927 case RID_TRANSACTION_CANCEL:
10928 statement = cp_parser_transaction_cancel (parser);
10929 break;
10931 default:
10932 /* It might be a keyword like `int' that can start a
10933 declaration-statement. */
10934 break;
10937 else if (token->type == CPP_NAME)
10939 /* If the next token is a `:', then we are looking at a
10940 labeled-statement. */
10941 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10942 if (token->type == CPP_COLON)
10944 /* Looks like a labeled-statement with an ordinary label.
10945 Parse the label, and then use tail recursion to parse
10946 the statement. */
10948 cp_parser_label_for_labeled_statement (parser, std_attrs);
10949 in_compound = false;
10950 goto restart;
10953 /* Anything that starts with a `{' must be a compound-statement. */
10954 else if (token->type == CPP_OPEN_BRACE)
10955 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10956 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10957 a statement all its own. */
10958 else if (token->type == CPP_PRAGMA)
10960 /* Only certain OpenMP pragmas are attached to statements, and thus
10961 are considered statements themselves. All others are not. In
10962 the context of a compound, accept the pragma as a "statement" and
10963 return so that we can check for a close brace. Otherwise we
10964 require a real statement and must go back and read one. */
10965 if (in_compound)
10966 cp_parser_pragma (parser, pragma_compound, if_p);
10967 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10968 goto restart;
10969 return;
10971 else if (token->type == CPP_EOF)
10973 cp_parser_error (parser, "expected statement");
10974 return;
10977 /* Everything else must be a declaration-statement or an
10978 expression-statement. Try for the declaration-statement
10979 first, unless we are looking at a `;', in which case we know that
10980 we have an expression-statement. */
10981 if (!statement)
10983 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10985 if (std_attrs != NULL_TREE)
10987 /* Attributes should be parsed as part of the the
10988 declaration, so let's un-parse them. */
10989 saved_tokens.rollback();
10990 std_attrs = NULL_TREE;
10993 cp_parser_parse_tentatively (parser);
10994 /* Try to parse the declaration-statement. */
10995 cp_parser_declaration_statement (parser);
10996 /* If that worked, we're done. */
10997 if (cp_parser_parse_definitely (parser))
10998 return;
11000 /* All preceding labels have been parsed at this point. */
11001 if (loc_after_labels != NULL)
11002 *loc_after_labels = statement_location;
11004 /* Look for an expression-statement instead. */
11005 statement = cp_parser_expression_statement (parser, in_statement_expr);
11007 /* Handle [[fallthrough]];. */
11008 if (attribute_fallthrough_p (std_attrs))
11010 /* The next token after the fallthrough attribute is ';'. */
11011 if (statement == NULL_TREE)
11013 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11014 statement = build_call_expr_internal_loc (statement_location,
11015 IFN_FALLTHROUGH,
11016 void_type_node, 0);
11017 finish_expr_stmt (statement);
11019 else
11020 warning_at (statement_location, OPT_Wattributes,
11021 "%<fallthrough%> attribute not followed by %<;%>");
11022 std_attrs = NULL_TREE;
11026 /* Set the line number for the statement. */
11027 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11028 SET_EXPR_LOCATION (statement, statement_location);
11030 /* Allow "[[fallthrough]];", but warn otherwise. */
11031 if (std_attrs != NULL_TREE)
11032 warning_at (attrs_location,
11033 OPT_Wattributes,
11034 "attributes at the beginning of statement are ignored");
11037 /* Append ATTR to attribute list ATTRS. */
11039 static tree
11040 attr_chainon (tree attrs, tree attr)
11042 if (attrs == error_mark_node)
11043 return error_mark_node;
11044 if (attr == error_mark_node)
11045 return error_mark_node;
11046 return chainon (attrs, attr);
11049 /* Parse the label for a labeled-statement, i.e.
11051 identifier :
11052 case constant-expression :
11053 default :
11055 GNU Extension:
11056 case constant-expression ... constant-expression : statement
11058 When a label is parsed without errors, the label is added to the
11059 parse tree by the finish_* functions, so this function doesn't
11060 have to return the label. */
11062 static void
11063 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11065 cp_token *token;
11066 tree label = NULL_TREE;
11067 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11069 /* The next token should be an identifier. */
11070 token = cp_lexer_peek_token (parser->lexer);
11071 if (token->type != CPP_NAME
11072 && token->type != CPP_KEYWORD)
11074 cp_parser_error (parser, "expected labeled-statement");
11075 return;
11078 /* Remember whether this case or a user-defined label is allowed to fall
11079 through to. */
11080 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11082 parser->colon_corrects_to_scope_p = false;
11083 switch (token->keyword)
11085 case RID_CASE:
11087 tree expr, expr_hi;
11088 cp_token *ellipsis;
11090 /* Consume the `case' token. */
11091 cp_lexer_consume_token (parser->lexer);
11092 /* Parse the constant-expression. */
11093 expr = cp_parser_constant_expression (parser);
11094 if (check_for_bare_parameter_packs (expr))
11095 expr = error_mark_node;
11097 ellipsis = cp_lexer_peek_token (parser->lexer);
11098 if (ellipsis->type == CPP_ELLIPSIS)
11100 /* Consume the `...' token. */
11101 cp_lexer_consume_token (parser->lexer);
11102 expr_hi = cp_parser_constant_expression (parser);
11103 if (check_for_bare_parameter_packs (expr_hi))
11104 expr_hi = error_mark_node;
11106 /* We don't need to emit warnings here, as the common code
11107 will do this for us. */
11109 else
11110 expr_hi = NULL_TREE;
11112 if (parser->in_switch_statement_p)
11114 tree l = finish_case_label (token->location, expr, expr_hi);
11115 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11116 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11118 else
11119 error_at (token->location,
11120 "case label %qE not within a switch statement",
11121 expr);
11123 break;
11125 case RID_DEFAULT:
11126 /* Consume the `default' token. */
11127 cp_lexer_consume_token (parser->lexer);
11129 if (parser->in_switch_statement_p)
11131 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11132 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11133 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11135 else
11136 error_at (token->location, "case label not within a switch statement");
11137 break;
11139 default:
11140 /* Anything else must be an ordinary label. */
11141 label = finish_label_stmt (cp_parser_identifier (parser));
11142 if (label && TREE_CODE (label) == LABEL_DECL)
11143 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11144 break;
11147 /* Require the `:' token. */
11148 cp_parser_require (parser, CPP_COLON, RT_COLON);
11150 /* An ordinary label may optionally be followed by attributes.
11151 However, this is only permitted if the attributes are then
11152 followed by a semicolon. This is because, for backward
11153 compatibility, when parsing
11154 lab: __attribute__ ((unused)) int i;
11155 we want the attribute to attach to "i", not "lab". */
11156 if (label != NULL_TREE
11157 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11159 tree attrs;
11160 cp_parser_parse_tentatively (parser);
11161 attrs = cp_parser_gnu_attributes_opt (parser);
11162 if (attrs == NULL_TREE
11163 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11164 cp_parser_abort_tentative_parse (parser);
11165 else if (!cp_parser_parse_definitely (parser))
11167 else
11168 attributes = attr_chainon (attributes, attrs);
11171 if (attributes != NULL_TREE)
11172 cplus_decl_attributes (&label, attributes, 0);
11174 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11177 /* Parse an expression-statement.
11179 expression-statement:
11180 expression [opt] ;
11182 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11183 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11184 indicates whether this expression-statement is part of an
11185 expression statement. */
11187 static tree
11188 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11190 tree statement = NULL_TREE;
11191 cp_token *token = cp_lexer_peek_token (parser->lexer);
11192 location_t loc = token->location;
11194 /* There might be attribute fallthrough. */
11195 tree attr = cp_parser_gnu_attributes_opt (parser);
11197 /* If the next token is a ';', then there is no expression
11198 statement. */
11199 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11201 statement = cp_parser_expression (parser);
11202 if (statement == error_mark_node
11203 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11205 cp_parser_skip_to_end_of_block_or_statement (parser);
11206 return error_mark_node;
11210 /* Handle [[fallthrough]];. */
11211 if (attribute_fallthrough_p (attr))
11213 /* The next token after the fallthrough attribute is ';'. */
11214 if (statement == NULL_TREE)
11215 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11216 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11217 void_type_node, 0);
11218 else
11219 warning_at (loc, OPT_Wattributes,
11220 "%<fallthrough%> attribute not followed by %<;%>");
11221 attr = NULL_TREE;
11224 /* Allow "[[fallthrough]];", but warn otherwise. */
11225 if (attr != NULL_TREE)
11226 warning_at (loc, OPT_Wattributes,
11227 "attributes at the beginning of statement are ignored");
11229 /* Give a helpful message for "A<T>::type t;" and the like. */
11230 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11231 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11233 if (TREE_CODE (statement) == SCOPE_REF)
11234 error_at (token->location, "need %<typename%> before %qE because "
11235 "%qT is a dependent scope",
11236 statement, TREE_OPERAND (statement, 0));
11237 else if (is_overloaded_fn (statement)
11238 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11240 /* A::A a; */
11241 tree fn = get_first_fn (statement);
11242 error_at (token->location,
11243 "%<%T::%D%> names the constructor, not the type",
11244 DECL_CONTEXT (fn), DECL_NAME (fn));
11248 /* Consume the final `;'. */
11249 cp_parser_consume_semicolon_at_end_of_statement (parser);
11251 if (in_statement_expr
11252 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11253 /* This is the final expression statement of a statement
11254 expression. */
11255 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11256 else if (statement)
11257 statement = finish_expr_stmt (statement);
11259 return statement;
11262 /* Parse a compound-statement.
11264 compound-statement:
11265 { statement-seq [opt] }
11267 GNU extension:
11269 compound-statement:
11270 { label-declaration-seq [opt] statement-seq [opt] }
11272 label-declaration-seq:
11273 label-declaration
11274 label-declaration-seq label-declaration
11276 Returns a tree representing the statement. */
11278 static tree
11279 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11280 int bcs_flags, bool function_body)
11282 tree compound_stmt;
11283 matching_braces braces;
11285 /* Consume the `{'. */
11286 if (!braces.require_open (parser))
11287 return error_mark_node;
11288 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11289 && !function_body && cxx_dialect < cxx14)
11290 pedwarn (input_location, OPT_Wpedantic,
11291 "compound-statement in %<constexpr%> function");
11292 /* Begin the compound-statement. */
11293 compound_stmt = begin_compound_stmt (bcs_flags);
11294 /* If the next keyword is `__label__' we have a label declaration. */
11295 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11296 cp_parser_label_declaration (parser);
11297 /* Parse an (optional) statement-seq. */
11298 cp_parser_statement_seq_opt (parser, in_statement_expr);
11299 /* Finish the compound-statement. */
11300 finish_compound_stmt (compound_stmt);
11301 /* Consume the `}'. */
11302 braces.require_close (parser);
11304 return compound_stmt;
11307 /* Parse an (optional) statement-seq.
11309 statement-seq:
11310 statement
11311 statement-seq [opt] statement */
11313 static void
11314 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11316 /* Scan statements until there aren't any more. */
11317 while (true)
11319 cp_token *token = cp_lexer_peek_token (parser->lexer);
11321 /* If we are looking at a `}', then we have run out of
11322 statements; the same is true if we have reached the end
11323 of file, or have stumbled upon a stray '@end'. */
11324 if (token->type == CPP_CLOSE_BRACE
11325 || token->type == CPP_EOF
11326 || token->type == CPP_PRAGMA_EOL
11327 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11328 break;
11330 /* If we are in a compound statement and find 'else' then
11331 something went wrong. */
11332 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11334 if (parser->in_statement & IN_IF_STMT)
11335 break;
11336 else
11338 token = cp_lexer_consume_token (parser->lexer);
11339 error_at (token->location, "%<else%> without a previous %<if%>");
11343 /* Parse the statement. */
11344 cp_parser_statement (parser, in_statement_expr, true, NULL);
11348 /* Return true if this is the C++20 version of range-based-for with
11349 init-statement. */
11351 static bool
11352 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11354 bool r = false;
11356 /* Save tokens so that we can put them back. */
11357 cp_lexer_save_tokens (parser->lexer);
11359 /* There has to be an unnested ; followed by an unnested :. */
11360 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11361 /*recovering=*/false,
11362 CPP_SEMICOLON,
11363 /*consume_paren=*/false) != -1)
11364 goto out;
11366 /* We found the semicolon, eat it now. */
11367 cp_lexer_consume_token (parser->lexer);
11369 /* Now look for ':' that is not nested in () or {}. */
11370 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11371 /*recovering=*/false,
11372 CPP_COLON,
11373 /*consume_paren=*/false) == -1);
11375 out:
11376 /* Roll back the tokens we skipped. */
11377 cp_lexer_rollback_tokens (parser->lexer);
11379 return r;
11382 /* Return true if we're looking at (init; cond), false otherwise. */
11384 static bool
11385 cp_parser_init_statement_p (cp_parser *parser)
11387 /* Save tokens so that we can put them back. */
11388 cp_lexer_save_tokens (parser->lexer);
11390 /* Look for ';' that is not nested in () or {}. */
11391 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11392 /*recovering=*/false,
11393 CPP_SEMICOLON,
11394 /*consume_paren=*/false);
11396 /* Roll back the tokens we skipped. */
11397 cp_lexer_rollback_tokens (parser->lexer);
11399 return ret == -1;
11402 /* Parse a selection-statement.
11404 selection-statement:
11405 if ( init-statement [opt] condition ) statement
11406 if ( init-statement [opt] condition ) statement else statement
11407 switch ( init-statement [opt] condition ) statement
11409 Returns the new IF_STMT or SWITCH_STMT.
11411 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11412 is a (possibly labeled) if statement which is not enclosed in
11413 braces and has an else clause. This is used to implement
11414 -Wparentheses.
11416 CHAIN is a vector of if-else-if conditions. This is used to implement
11417 -Wduplicated-cond. */
11419 static tree
11420 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11421 vec<tree> *chain)
11423 cp_token *token;
11424 enum rid keyword;
11425 token_indent_info guard_tinfo;
11427 if (if_p != NULL)
11428 *if_p = false;
11430 /* Peek at the next token. */
11431 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11432 guard_tinfo = get_token_indent_info (token);
11434 /* See what kind of keyword it is. */
11435 keyword = token->keyword;
11436 switch (keyword)
11438 case RID_IF:
11439 case RID_SWITCH:
11441 tree statement;
11442 tree condition;
11444 bool cx = false;
11445 if (keyword == RID_IF
11446 && cp_lexer_next_token_is_keyword (parser->lexer,
11447 RID_CONSTEXPR))
11449 cx = true;
11450 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11451 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11452 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11453 "with -std=c++17 or -std=gnu++17");
11456 /* Look for the `('. */
11457 matching_parens parens;
11458 if (!parens.require_open (parser))
11460 cp_parser_skip_to_end_of_statement (parser);
11461 return error_mark_node;
11464 /* Begin the selection-statement. */
11465 if (keyword == RID_IF)
11467 statement = begin_if_stmt ();
11468 IF_STMT_CONSTEXPR_P (statement) = cx;
11470 else
11471 statement = begin_switch_stmt ();
11473 /* Parse the optional init-statement. */
11474 if (cp_parser_init_statement_p (parser))
11476 tree decl;
11477 if (cxx_dialect < cxx17)
11478 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11479 "init-statement in selection statements only available "
11480 "with -std=c++17 or -std=gnu++17");
11481 cp_parser_init_statement (parser, &decl);
11484 /* Parse the condition. */
11485 condition = cp_parser_condition (parser);
11486 /* Look for the `)'. */
11487 if (!parens.require_close (parser))
11488 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11489 /*consume_paren=*/true);
11491 if (keyword == RID_IF)
11493 bool nested_if;
11494 unsigned char in_statement;
11496 /* Add the condition. */
11497 condition = finish_if_stmt_cond (condition, statement);
11499 if (warn_duplicated_cond)
11500 warn_duplicated_cond_add_or_warn (token->location, condition,
11501 &chain);
11503 /* Parse the then-clause. */
11504 in_statement = parser->in_statement;
11505 parser->in_statement |= IN_IF_STMT;
11507 /* Outside a template, the non-selected branch of a constexpr
11508 if is a 'discarded statement', i.e. unevaluated. */
11509 bool was_discarded = in_discarded_stmt;
11510 bool discard_then = (cx && !processing_template_decl
11511 && integer_zerop (condition));
11512 if (discard_then)
11514 in_discarded_stmt = true;
11515 ++c_inhibit_evaluation_warnings;
11518 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11519 guard_tinfo);
11521 parser->in_statement = in_statement;
11523 finish_then_clause (statement);
11525 if (discard_then)
11527 THEN_CLAUSE (statement) = NULL_TREE;
11528 in_discarded_stmt = was_discarded;
11529 --c_inhibit_evaluation_warnings;
11532 /* If the next token is `else', parse the else-clause. */
11533 if (cp_lexer_next_token_is_keyword (parser->lexer,
11534 RID_ELSE))
11536 bool discard_else = (cx && !processing_template_decl
11537 && integer_nonzerop (condition));
11538 if (discard_else)
11540 in_discarded_stmt = true;
11541 ++c_inhibit_evaluation_warnings;
11544 guard_tinfo
11545 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11546 /* Consume the `else' keyword. */
11547 cp_lexer_consume_token (parser->lexer);
11548 if (warn_duplicated_cond)
11550 if (cp_lexer_next_token_is_keyword (parser->lexer,
11551 RID_IF)
11552 && chain == NULL)
11554 /* We've got "if (COND) else if (COND2)". Start
11555 the condition chain and add COND as the first
11556 element. */
11557 chain = new vec<tree> ();
11558 if (!CONSTANT_CLASS_P (condition)
11559 && !TREE_SIDE_EFFECTS (condition))
11561 /* Wrap it in a NOP_EXPR so that we can set the
11562 location of the condition. */
11563 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11564 condition);
11565 SET_EXPR_LOCATION (e, token->location);
11566 chain->safe_push (e);
11569 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11570 RID_IF))
11572 /* This is if-else without subsequent if. Zap the
11573 condition chain; we would have already warned at
11574 this point. */
11575 delete chain;
11576 chain = NULL;
11579 begin_else_clause (statement);
11580 /* Parse the else-clause. */
11581 cp_parser_implicitly_scoped_statement (parser, NULL,
11582 guard_tinfo, chain);
11584 finish_else_clause (statement);
11586 /* If we are currently parsing a then-clause, then
11587 IF_P will not be NULL. We set it to true to
11588 indicate that this if statement has an else clause.
11589 This may trigger the Wparentheses warning below
11590 when we get back up to the parent if statement. */
11591 if (if_p != NULL)
11592 *if_p = true;
11594 if (discard_else)
11596 ELSE_CLAUSE (statement) = NULL_TREE;
11597 in_discarded_stmt = was_discarded;
11598 --c_inhibit_evaluation_warnings;
11601 else
11603 /* This if statement does not have an else clause. If
11604 NESTED_IF is true, then the then-clause has an if
11605 statement which does have an else clause. We warn
11606 about the potential ambiguity. */
11607 if (nested_if)
11608 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11609 "suggest explicit braces to avoid ambiguous"
11610 " %<else%>");
11611 if (warn_duplicated_cond)
11613 /* We don't need the condition chain anymore. */
11614 delete chain;
11615 chain = NULL;
11619 /* Now we're all done with the if-statement. */
11620 finish_if_stmt (statement);
11622 else
11624 bool in_switch_statement_p;
11625 unsigned char in_statement;
11627 /* Add the condition. */
11628 finish_switch_cond (condition, statement);
11630 /* Parse the body of the switch-statement. */
11631 in_switch_statement_p = parser->in_switch_statement_p;
11632 in_statement = parser->in_statement;
11633 parser->in_switch_statement_p = true;
11634 parser->in_statement |= IN_SWITCH_STMT;
11635 cp_parser_implicitly_scoped_statement (parser, if_p,
11636 guard_tinfo);
11637 parser->in_switch_statement_p = in_switch_statement_p;
11638 parser->in_statement = in_statement;
11640 /* Now we're all done with the switch-statement. */
11641 finish_switch_stmt (statement);
11644 return statement;
11646 break;
11648 default:
11649 cp_parser_error (parser, "expected selection-statement");
11650 return error_mark_node;
11654 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11655 If we have seen at least one decl-specifier, and the next token
11656 is not a parenthesis, then we must be looking at a declaration.
11657 (After "int (" we might be looking at a functional cast.) */
11659 static void
11660 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11661 bool any_specifiers_p)
11663 if (any_specifiers_p
11664 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11665 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11666 && !cp_parser_error_occurred (parser))
11667 cp_parser_commit_to_tentative_parse (parser);
11670 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11671 The declarator shall not specify a function or an array. Returns
11672 TRUE if the declarator is valid, FALSE otherwise. */
11674 static bool
11675 cp_parser_check_condition_declarator (cp_parser* parser,
11676 cp_declarator *declarator,
11677 location_t loc)
11679 if (declarator == cp_error_declarator
11680 || function_declarator_p (declarator)
11681 || declarator->kind == cdk_array)
11683 if (declarator == cp_error_declarator)
11684 /* Already complained. */;
11685 else if (declarator->kind == cdk_array)
11686 error_at (loc, "condition declares an array");
11687 else
11688 error_at (loc, "condition declares a function");
11689 if (parser->fully_implicit_function_template_p)
11690 abort_fully_implicit_template (parser);
11691 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11692 /*or_comma=*/false,
11693 /*consume_paren=*/false);
11694 return false;
11696 else
11697 return true;
11700 /* Parse a condition.
11702 condition:
11703 expression
11704 type-specifier-seq declarator = initializer-clause
11705 type-specifier-seq declarator braced-init-list
11707 GNU Extension:
11709 condition:
11710 type-specifier-seq declarator asm-specification [opt]
11711 attributes [opt] = assignment-expression
11713 Returns the expression that should be tested. */
11715 static tree
11716 cp_parser_condition (cp_parser* parser)
11718 cp_decl_specifier_seq type_specifiers;
11719 const char *saved_message;
11720 int declares_class_or_enum;
11722 /* Try the declaration first. */
11723 cp_parser_parse_tentatively (parser);
11724 /* New types are not allowed in the type-specifier-seq for a
11725 condition. */
11726 saved_message = parser->type_definition_forbidden_message;
11727 parser->type_definition_forbidden_message
11728 = G_("types may not be defined in conditions");
11729 /* Parse the type-specifier-seq. */
11730 cp_parser_decl_specifier_seq (parser,
11731 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11732 &type_specifiers,
11733 &declares_class_or_enum);
11734 /* Restore the saved message. */
11735 parser->type_definition_forbidden_message = saved_message;
11737 cp_parser_maybe_commit_to_declaration (parser,
11738 type_specifiers.any_specifiers_p);
11740 /* If all is well, we might be looking at a declaration. */
11741 if (!cp_parser_error_occurred (parser))
11743 tree decl;
11744 tree asm_specification;
11745 tree attributes;
11746 cp_declarator *declarator;
11747 tree initializer = NULL_TREE;
11748 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11750 /* Parse the declarator. */
11751 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11752 /*ctor_dtor_or_conv_p=*/NULL,
11753 /*parenthesized_p=*/NULL,
11754 /*member_p=*/false,
11755 /*friend_p=*/false);
11756 /* Parse the attributes. */
11757 attributes = cp_parser_attributes_opt (parser);
11758 /* Parse the asm-specification. */
11759 asm_specification = cp_parser_asm_specification_opt (parser);
11760 /* If the next token is not an `=' or '{', then we might still be
11761 looking at an expression. For example:
11763 if (A(a).x)
11765 looks like a decl-specifier-seq and a declarator -- but then
11766 there is no `=', so this is an expression. */
11767 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11768 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11769 cp_parser_simulate_error (parser);
11771 /* If we did see an `=' or '{', then we are looking at a declaration
11772 for sure. */
11773 if (cp_parser_parse_definitely (parser))
11775 tree pushed_scope;
11776 bool non_constant_p = false;
11777 int flags = LOOKUP_ONLYCONVERTING;
11779 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11780 return error_mark_node;
11782 /* Create the declaration. */
11783 decl = start_decl (declarator, &type_specifiers,
11784 /*initialized_p=*/true,
11785 attributes, /*prefix_attributes=*/NULL_TREE,
11786 &pushed_scope);
11788 /* Parse the initializer. */
11789 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11791 initializer = cp_parser_braced_list (parser, &non_constant_p);
11792 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11793 flags = 0;
11795 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11797 /* Consume the `='. */
11798 cp_lexer_consume_token (parser->lexer);
11799 initializer = cp_parser_initializer_clause (parser,
11800 &non_constant_p);
11802 else
11804 cp_parser_error (parser, "expected initializer");
11805 initializer = error_mark_node;
11807 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11808 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11810 /* Process the initializer. */
11811 cp_finish_decl (decl,
11812 initializer, !non_constant_p,
11813 asm_specification,
11814 flags);
11816 if (pushed_scope)
11817 pop_scope (pushed_scope);
11819 return convert_from_reference (decl);
11822 /* If we didn't even get past the declarator successfully, we are
11823 definitely not looking at a declaration. */
11824 else
11825 cp_parser_abort_tentative_parse (parser);
11827 /* Otherwise, we are looking at an expression. */
11828 return cp_parser_expression (parser);
11831 /* Parses a for-statement or range-for-statement until the closing ')',
11832 not included. */
11834 static tree
11835 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11837 tree init, scope, decl;
11838 bool is_range_for;
11840 /* Begin the for-statement. */
11841 scope = begin_for_scope (&init);
11843 /* Parse the initialization. */
11844 is_range_for = cp_parser_init_statement (parser, &decl);
11846 if (is_range_for)
11847 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
11848 false);
11849 else
11850 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11853 static tree
11854 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11855 unsigned short unroll)
11857 /* Normal for loop */
11858 tree condition = NULL_TREE;
11859 tree expression = NULL_TREE;
11860 tree stmt;
11862 stmt = begin_for_stmt (scope, init);
11863 /* The init-statement has already been parsed in
11864 cp_parser_init_statement, so no work is needed here. */
11865 finish_init_stmt (stmt);
11867 /* If there's a condition, process it. */
11868 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11869 condition = cp_parser_condition (parser);
11870 else if (ivdep)
11872 cp_parser_error (parser, "missing loop condition in loop with "
11873 "%<GCC ivdep%> pragma");
11874 condition = error_mark_node;
11876 else if (unroll)
11878 cp_parser_error (parser, "missing loop condition in loop with "
11879 "%<GCC unroll%> pragma");
11880 condition = error_mark_node;
11882 finish_for_cond (condition, stmt, ivdep, unroll);
11883 /* Look for the `;'. */
11884 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11886 /* If there's an expression, process it. */
11887 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11888 expression = cp_parser_expression (parser);
11889 finish_for_expr (expression, stmt);
11891 return stmt;
11894 /* Tries to parse a range-based for-statement:
11896 range-based-for:
11897 decl-specifier-seq declarator : expression
11899 The decl-specifier-seq declarator and the `:' are already parsed by
11900 cp_parser_init_statement. If processing_template_decl it returns a
11901 newly created RANGE_FOR_STMT; if not, it is converted to a
11902 regular FOR_STMT. */
11904 static tree
11905 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11906 bool ivdep, unsigned short unroll, bool is_omp)
11908 tree stmt, range_expr;
11909 auto_vec <cxx_binding *, 16> bindings;
11910 auto_vec <tree, 16> names;
11911 tree decomp_first_name = NULL_TREE;
11912 unsigned int decomp_cnt = 0;
11914 /* Get the range declaration momentarily out of the way so that
11915 the range expression doesn't clash with it. */
11916 if (range_decl != error_mark_node)
11918 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11920 tree v = DECL_VALUE_EXPR (range_decl);
11921 /* For decomposition declaration get all of the corresponding
11922 declarations out of the way. */
11923 if (TREE_CODE (v) == ARRAY_REF
11924 && VAR_P (TREE_OPERAND (v, 0))
11925 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11927 tree d = range_decl;
11928 range_decl = TREE_OPERAND (v, 0);
11929 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11930 decomp_first_name = d;
11931 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11933 tree name = DECL_NAME (d);
11934 names.safe_push (name);
11935 bindings.safe_push (IDENTIFIER_BINDING (name));
11936 IDENTIFIER_BINDING (name)
11937 = IDENTIFIER_BINDING (name)->previous;
11941 if (names.is_empty ())
11943 tree name = DECL_NAME (range_decl);
11944 names.safe_push (name);
11945 bindings.safe_push (IDENTIFIER_BINDING (name));
11946 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11950 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11952 bool expr_non_constant_p;
11953 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11955 else
11956 range_expr = cp_parser_expression (parser);
11958 /* Put the range declaration(s) back into scope. */
11959 for (unsigned int i = 0; i < names.length (); i++)
11961 cxx_binding *binding = bindings[i];
11962 binding->previous = IDENTIFIER_BINDING (names[i]);
11963 IDENTIFIER_BINDING (names[i]) = binding;
11966 /* finish_omp_for has its own code for the following, so just
11967 return the range_expr instead. */
11968 if (is_omp)
11969 return range_expr;
11971 /* If in template, STMT is converted to a normal for-statement
11972 at instantiation. If not, it is done just ahead. */
11973 if (processing_template_decl)
11975 if (check_for_bare_parameter_packs (range_expr))
11976 range_expr = error_mark_node;
11977 stmt = begin_range_for_stmt (scope, init);
11978 if (ivdep)
11979 RANGE_FOR_IVDEP (stmt) = 1;
11980 if (unroll)
11981 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11982 finish_range_for_decl (stmt, range_decl, range_expr);
11983 if (!type_dependent_expression_p (range_expr)
11984 /* do_auto_deduction doesn't mess with template init-lists. */
11985 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11986 do_range_for_auto_deduction (range_decl, range_expr);
11988 else
11990 stmt = begin_for_stmt (scope, init);
11991 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11992 decomp_first_name, decomp_cnt, ivdep,
11993 unroll);
11995 return stmt;
11998 /* Subroutine of cp_convert_range_for: given the initializer expression,
11999 builds up the range temporary. */
12001 static tree
12002 build_range_temp (tree range_expr)
12004 tree range_type, range_temp;
12006 /* Find out the type deduced by the declaration
12007 `auto &&__range = range_expr'. */
12008 range_type = cp_build_reference_type (make_auto (), true);
12009 range_type = do_auto_deduction (range_type, range_expr,
12010 type_uses_auto (range_type));
12012 /* Create the __range variable. */
12013 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12014 range_type);
12015 TREE_USED (range_temp) = 1;
12016 DECL_ARTIFICIAL (range_temp) = 1;
12018 return range_temp;
12021 /* Used by cp_parser_range_for in template context: we aren't going to
12022 do a full conversion yet, but we still need to resolve auto in the
12023 type of the for-range-declaration if present. This is basically
12024 a shortcut version of cp_convert_range_for. */
12026 static void
12027 do_range_for_auto_deduction (tree decl, tree range_expr)
12029 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12030 if (auto_node)
12032 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12033 range_temp = convert_from_reference (build_range_temp (range_expr));
12034 iter_type = (cp_parser_perform_range_for_lookup
12035 (range_temp, &begin_dummy, &end_dummy));
12036 if (iter_type)
12038 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12039 iter_type);
12040 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12041 RO_UNARY_STAR,
12042 tf_warning_or_error);
12043 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12044 iter_decl, auto_node);
12049 /* Converts a range-based for-statement into a normal
12050 for-statement, as per the definition.
12052 for (RANGE_DECL : RANGE_EXPR)
12053 BLOCK
12055 should be equivalent to:
12058 auto &&__range = RANGE_EXPR;
12059 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12060 __begin != __end;
12061 ++__begin)
12063 RANGE_DECL = *__begin;
12064 BLOCK
12068 If RANGE_EXPR is an array:
12069 BEGIN_EXPR = __range
12070 END_EXPR = __range + ARRAY_SIZE(__range)
12071 Else if RANGE_EXPR has a member 'begin' or 'end':
12072 BEGIN_EXPR = __range.begin()
12073 END_EXPR = __range.end()
12074 Else:
12075 BEGIN_EXPR = begin(__range)
12076 END_EXPR = end(__range);
12078 If __range has a member 'begin' but not 'end', or vice versa, we must
12079 still use the second alternative (it will surely fail, however).
12080 When calling begin()/end() in the third alternative we must use
12081 argument dependent lookup, but always considering 'std' as an associated
12082 namespace. */
12084 tree
12085 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12086 tree decomp_first_name, unsigned int decomp_cnt,
12087 bool ivdep, unsigned short unroll)
12089 tree begin, end;
12090 tree iter_type, begin_expr, end_expr;
12091 tree condition, expression;
12093 range_expr = mark_lvalue_use (range_expr);
12095 if (range_decl == error_mark_node || range_expr == error_mark_node)
12096 /* If an error happened previously do nothing or else a lot of
12097 unhelpful errors would be issued. */
12098 begin_expr = end_expr = iter_type = error_mark_node;
12099 else
12101 tree range_temp;
12103 if (VAR_P (range_expr)
12104 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12105 /* Can't bind a reference to an array of runtime bound. */
12106 range_temp = range_expr;
12107 else
12109 range_temp = build_range_temp (range_expr);
12110 pushdecl (range_temp);
12111 cp_finish_decl (range_temp, range_expr,
12112 /*is_constant_init*/false, NULL_TREE,
12113 LOOKUP_ONLYCONVERTING);
12114 range_temp = convert_from_reference (range_temp);
12116 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12117 &begin_expr, &end_expr);
12120 /* The new for initialization statement. */
12121 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12122 iter_type);
12123 TREE_USED (begin) = 1;
12124 DECL_ARTIFICIAL (begin) = 1;
12125 pushdecl (begin);
12126 cp_finish_decl (begin, begin_expr,
12127 /*is_constant_init*/false, NULL_TREE,
12128 LOOKUP_ONLYCONVERTING);
12130 if (cxx_dialect >= cxx17)
12131 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12132 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12133 TREE_USED (end) = 1;
12134 DECL_ARTIFICIAL (end) = 1;
12135 pushdecl (end);
12136 cp_finish_decl (end, end_expr,
12137 /*is_constant_init*/false, NULL_TREE,
12138 LOOKUP_ONLYCONVERTING);
12140 finish_init_stmt (statement);
12142 /* The new for condition. */
12143 condition = build_x_binary_op (input_location, NE_EXPR,
12144 begin, ERROR_MARK,
12145 end, ERROR_MARK,
12146 NULL, tf_warning_or_error);
12147 finish_for_cond (condition, statement, ivdep, unroll);
12149 /* The new increment expression. */
12150 expression = finish_unary_op_expr (input_location,
12151 PREINCREMENT_EXPR, begin,
12152 tf_warning_or_error);
12153 finish_for_expr (expression, statement);
12155 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12156 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12158 /* The declaration is initialized with *__begin inside the loop body. */
12159 cp_finish_decl (range_decl,
12160 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12161 tf_warning_or_error),
12162 /*is_constant_init*/false, NULL_TREE,
12163 LOOKUP_ONLYCONVERTING);
12164 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12165 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12167 return statement;
12170 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12171 We need to solve both at the same time because the method used
12172 depends on the existence of members begin or end.
12173 Returns the type deduced for the iterator expression. */
12175 static tree
12176 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12178 if (error_operand_p (range))
12180 *begin = *end = error_mark_node;
12181 return error_mark_node;
12184 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12186 error ("range-based %<for%> expression of type %qT "
12187 "has incomplete type", TREE_TYPE (range));
12188 *begin = *end = error_mark_node;
12189 return error_mark_node;
12191 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12193 /* If RANGE is an array, we will use pointer arithmetic. */
12194 *begin = decay_conversion (range, tf_warning_or_error);
12195 *end = build_binary_op (input_location, PLUS_EXPR,
12196 range,
12197 array_type_nelts_top (TREE_TYPE (range)),
12198 false);
12199 return TREE_TYPE (*begin);
12201 else
12203 /* If it is not an array, we must do a bit of magic. */
12204 tree id_begin, id_end;
12205 tree member_begin, member_end;
12207 *begin = *end = error_mark_node;
12209 id_begin = get_identifier ("begin");
12210 id_end = get_identifier ("end");
12211 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12212 /*protect=*/2, /*want_type=*/false,
12213 tf_warning_or_error);
12214 member_end = lookup_member (TREE_TYPE (range), id_end,
12215 /*protect=*/2, /*want_type=*/false,
12216 tf_warning_or_error);
12218 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12220 /* Use the member functions. */
12221 *begin = cp_parser_range_for_member_function (range, id_begin);
12222 *end = cp_parser_range_for_member_function (range, id_end);
12224 else
12226 /* Use global functions with ADL. */
12227 vec<tree, va_gc> *vec;
12228 vec = make_tree_vector ();
12230 vec_safe_push (vec, range);
12232 member_begin = perform_koenig_lookup (id_begin, vec,
12233 tf_warning_or_error);
12234 *begin = finish_call_expr (member_begin, &vec, false, true,
12235 tf_warning_or_error);
12236 member_end = perform_koenig_lookup (id_end, vec,
12237 tf_warning_or_error);
12238 *end = finish_call_expr (member_end, &vec, false, true,
12239 tf_warning_or_error);
12241 release_tree_vector (vec);
12244 /* Last common checks. */
12245 if (*begin == error_mark_node || *end == error_mark_node)
12247 /* If one of the expressions is an error do no more checks. */
12248 *begin = *end = error_mark_node;
12249 return error_mark_node;
12251 else if (type_dependent_expression_p (*begin)
12252 || type_dependent_expression_p (*end))
12253 /* Can happen, when, eg, in a template context, Koenig lookup
12254 can't resolve begin/end (c++/58503). */
12255 return NULL_TREE;
12256 else
12258 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12259 /* The unqualified type of the __begin and __end temporaries should
12260 be the same, as required by the multiple auto declaration. */
12261 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12263 if (cxx_dialect >= cxx17
12264 && (build_x_binary_op (input_location, NE_EXPR,
12265 *begin, ERROR_MARK,
12266 *end, ERROR_MARK,
12267 NULL, tf_none)
12268 != error_mark_node))
12269 /* P0184R0 allows __begin and __end to have different types,
12270 but make sure they are comparable so we can give a better
12271 diagnostic. */;
12272 else
12273 error ("inconsistent begin/end types in range-based %<for%> "
12274 "statement: %qT and %qT",
12275 TREE_TYPE (*begin), TREE_TYPE (*end));
12277 return iter_type;
12282 /* Helper function for cp_parser_perform_range_for_lookup.
12283 Builds a tree for RANGE.IDENTIFIER(). */
12285 static tree
12286 cp_parser_range_for_member_function (tree range, tree identifier)
12288 tree member, res;
12289 vec<tree, va_gc> *vec;
12291 member = finish_class_member_access_expr (range, identifier,
12292 false, tf_warning_or_error);
12293 if (member == error_mark_node)
12294 return error_mark_node;
12296 vec = make_tree_vector ();
12297 res = finish_call_expr (member, &vec,
12298 /*disallow_virtual=*/false,
12299 /*koenig_p=*/false,
12300 tf_warning_or_error);
12301 release_tree_vector (vec);
12302 return res;
12305 /* Parse an iteration-statement.
12307 iteration-statement:
12308 while ( condition ) statement
12309 do statement while ( expression ) ;
12310 for ( init-statement condition [opt] ; expression [opt] )
12311 statement
12313 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12315 static tree
12316 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12317 unsigned short unroll)
12319 cp_token *token;
12320 enum rid keyword;
12321 tree statement;
12322 unsigned char in_statement;
12323 token_indent_info guard_tinfo;
12325 /* Peek at the next token. */
12326 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12327 if (!token)
12328 return error_mark_node;
12330 guard_tinfo = get_token_indent_info (token);
12332 /* Remember whether or not we are already within an iteration
12333 statement. */
12334 in_statement = parser->in_statement;
12336 /* See what kind of keyword it is. */
12337 keyword = token->keyword;
12338 switch (keyword)
12340 case RID_WHILE:
12342 tree condition;
12344 /* Begin the while-statement. */
12345 statement = begin_while_stmt ();
12346 /* Look for the `('. */
12347 matching_parens parens;
12348 parens.require_open (parser);
12349 /* Parse the condition. */
12350 condition = cp_parser_condition (parser);
12351 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12352 /* Look for the `)'. */
12353 parens.require_close (parser);
12354 /* Parse the dependent statement. */
12355 parser->in_statement = IN_ITERATION_STMT;
12356 bool prev = note_iteration_stmt_body_start ();
12357 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12358 note_iteration_stmt_body_end (prev);
12359 parser->in_statement = in_statement;
12360 /* We're done with the while-statement. */
12361 finish_while_stmt (statement);
12363 break;
12365 case RID_DO:
12367 tree expression;
12369 /* Begin the do-statement. */
12370 statement = begin_do_stmt ();
12371 /* Parse the body of the do-statement. */
12372 parser->in_statement = IN_ITERATION_STMT;
12373 bool prev = note_iteration_stmt_body_start ();
12374 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12375 note_iteration_stmt_body_end (prev);
12376 parser->in_statement = in_statement;
12377 finish_do_body (statement);
12378 /* Look for the `while' keyword. */
12379 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12380 /* Look for the `('. */
12381 matching_parens parens;
12382 parens.require_open (parser);
12383 /* Parse the expression. */
12384 expression = cp_parser_expression (parser);
12385 /* We're done with the do-statement. */
12386 finish_do_stmt (expression, statement, ivdep, unroll);
12387 /* Look for the `)'. */
12388 parens.require_close (parser);
12389 /* Look for the `;'. */
12390 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12392 break;
12394 case RID_FOR:
12396 /* Look for the `('. */
12397 matching_parens parens;
12398 parens.require_open (parser);
12400 statement = cp_parser_for (parser, ivdep, unroll);
12402 /* Look for the `)'. */
12403 parens.require_close (parser);
12405 /* Parse the body of the for-statement. */
12406 parser->in_statement = IN_ITERATION_STMT;
12407 bool prev = note_iteration_stmt_body_start ();
12408 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12409 note_iteration_stmt_body_end (prev);
12410 parser->in_statement = in_statement;
12412 /* We're done with the for-statement. */
12413 finish_for_stmt (statement);
12415 break;
12417 default:
12418 cp_parser_error (parser, "expected iteration-statement");
12419 statement = error_mark_node;
12420 break;
12423 return statement;
12426 /* Parse a init-statement or the declarator of a range-based-for.
12427 Returns true if a range-based-for declaration is seen.
12429 init-statement:
12430 expression-statement
12431 simple-declaration */
12433 static bool
12434 cp_parser_init_statement (cp_parser *parser, tree *decl)
12436 /* If the next token is a `;', then we have an empty
12437 expression-statement. Grammatically, this is also a
12438 simple-declaration, but an invalid one, because it does not
12439 declare anything. Therefore, if we did not handle this case
12440 specially, we would issue an error message about an invalid
12441 declaration. */
12442 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12444 bool is_range_for = false;
12445 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12447 /* Try to parse the init-statement. */
12448 if (cp_parser_range_based_for_with_init_p (parser))
12450 tree dummy;
12451 cp_parser_parse_tentatively (parser);
12452 /* Parse the declaration. */
12453 cp_parser_simple_declaration (parser,
12454 /*function_definition_allowed_p=*/false,
12455 &dummy);
12456 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12457 if (!cp_parser_parse_definitely (parser))
12458 /* That didn't work, try to parse it as an expression-statement. */
12459 cp_parser_expression_statement (parser, NULL_TREE);
12461 if (cxx_dialect < cxx2a)
12463 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12464 "range-based %<for%> loops with initializer only "
12465 "available with -std=c++2a or -std=gnu++2a");
12466 *decl = error_mark_node;
12470 /* A colon is used in range-based for. */
12471 parser->colon_corrects_to_scope_p = false;
12473 /* We're going to speculatively look for a declaration, falling back
12474 to an expression, if necessary. */
12475 cp_parser_parse_tentatively (parser);
12476 /* Parse the declaration. */
12477 cp_parser_simple_declaration (parser,
12478 /*function_definition_allowed_p=*/false,
12479 decl);
12480 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12481 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12483 /* It is a range-for, consume the ':'. */
12484 cp_lexer_consume_token (parser->lexer);
12485 is_range_for = true;
12486 if (cxx_dialect < cxx11)
12487 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12488 "range-based %<for%> loops only available with "
12489 "-std=c++11 or -std=gnu++11");
12491 else
12492 /* The ';' is not consumed yet because we told
12493 cp_parser_simple_declaration not to. */
12494 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12496 if (cp_parser_parse_definitely (parser))
12497 return is_range_for;
12498 /* If the tentative parse failed, then we shall need to look for an
12499 expression-statement. */
12501 /* If we are here, it is an expression-statement. */
12502 cp_parser_expression_statement (parser, NULL_TREE);
12503 return false;
12506 /* Parse a jump-statement.
12508 jump-statement:
12509 break ;
12510 continue ;
12511 return expression [opt] ;
12512 return braced-init-list ;
12513 goto identifier ;
12515 GNU extension:
12517 jump-statement:
12518 goto * expression ;
12520 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12522 static tree
12523 cp_parser_jump_statement (cp_parser* parser)
12525 tree statement = error_mark_node;
12526 cp_token *token;
12527 enum rid keyword;
12528 unsigned char in_statement;
12530 /* Peek at the next token. */
12531 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12532 if (!token)
12533 return error_mark_node;
12535 /* See what kind of keyword it is. */
12536 keyword = token->keyword;
12537 switch (keyword)
12539 case RID_BREAK:
12540 in_statement = parser->in_statement & ~IN_IF_STMT;
12541 switch (in_statement)
12543 case 0:
12544 error_at (token->location, "break statement not within loop or switch");
12545 break;
12546 default:
12547 gcc_assert ((in_statement & IN_SWITCH_STMT)
12548 || in_statement == IN_ITERATION_STMT);
12549 statement = finish_break_stmt ();
12550 if (in_statement == IN_ITERATION_STMT)
12551 break_maybe_infinite_loop ();
12552 break;
12553 case IN_OMP_BLOCK:
12554 error_at (token->location, "invalid exit from OpenMP structured block");
12555 break;
12556 case IN_OMP_FOR:
12557 error_at (token->location, "break statement used with OpenMP for loop");
12558 break;
12560 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12561 break;
12563 case RID_CONTINUE:
12564 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12566 case 0:
12567 error_at (token->location, "continue statement not within a loop");
12568 break;
12569 /* Fall through. */
12570 case IN_ITERATION_STMT:
12571 case IN_OMP_FOR:
12572 statement = finish_continue_stmt ();
12573 break;
12574 case IN_OMP_BLOCK:
12575 error_at (token->location, "invalid exit from OpenMP structured block");
12576 break;
12577 default:
12578 gcc_unreachable ();
12580 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12581 break;
12583 case RID_RETURN:
12585 tree expr;
12586 bool expr_non_constant_p;
12588 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12590 cp_lexer_set_source_position (parser->lexer);
12591 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12592 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12594 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12595 expr = cp_parser_expression (parser);
12596 else
12597 /* If the next token is a `;', then there is no
12598 expression. */
12599 expr = NULL_TREE;
12600 /* Build the return-statement. */
12601 if (current_function_auto_return_pattern && in_discarded_stmt)
12602 /* Don't deduce from a discarded return statement. */;
12603 else
12604 statement = finish_return_stmt (expr);
12605 /* Look for the final `;'. */
12606 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12608 break;
12610 case RID_GOTO:
12611 if (parser->in_function_body
12612 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12614 error ("%<goto%> in %<constexpr%> function");
12615 cp_function_chain->invalid_constexpr = true;
12618 /* Create the goto-statement. */
12619 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12621 /* Issue a warning about this use of a GNU extension. */
12622 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12623 /* Consume the '*' token. */
12624 cp_lexer_consume_token (parser->lexer);
12625 /* Parse the dependent expression. */
12626 finish_goto_stmt (cp_parser_expression (parser));
12628 else
12629 finish_goto_stmt (cp_parser_identifier (parser));
12630 /* Look for the final `;'. */
12631 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12632 break;
12634 default:
12635 cp_parser_error (parser, "expected jump-statement");
12636 break;
12639 return statement;
12642 /* Parse a declaration-statement.
12644 declaration-statement:
12645 block-declaration */
12647 static void
12648 cp_parser_declaration_statement (cp_parser* parser)
12650 void *p;
12652 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12653 p = obstack_alloc (&declarator_obstack, 0);
12655 /* Parse the block-declaration. */
12656 cp_parser_block_declaration (parser, /*statement_p=*/true);
12658 /* Free any declarators allocated. */
12659 obstack_free (&declarator_obstack, p);
12662 /* Some dependent statements (like `if (cond) statement'), are
12663 implicitly in their own scope. In other words, if the statement is
12664 a single statement (as opposed to a compound-statement), it is
12665 none-the-less treated as if it were enclosed in braces. Any
12666 declarations appearing in the dependent statement are out of scope
12667 after control passes that point. This function parses a statement,
12668 but ensures that is in its own scope, even if it is not a
12669 compound-statement.
12671 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12672 is a (possibly labeled) if statement which is not enclosed in
12673 braces and has an else clause. This is used to implement
12674 -Wparentheses.
12676 CHAIN is a vector of if-else-if conditions. This is used to implement
12677 -Wduplicated-cond.
12679 Returns the new statement. */
12681 static tree
12682 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12683 const token_indent_info &guard_tinfo,
12684 vec<tree> *chain)
12686 tree statement;
12687 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12688 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12689 token_indent_info body_tinfo
12690 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12692 if (if_p != NULL)
12693 *if_p = false;
12695 /* Mark if () ; with a special NOP_EXPR. */
12696 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12698 cp_lexer_consume_token (parser->lexer);
12699 statement = add_stmt (build_empty_stmt (body_loc));
12701 if (guard_tinfo.keyword == RID_IF
12702 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12703 warning_at (body_loc, OPT_Wempty_body,
12704 "suggest braces around empty body in an %<if%> statement");
12705 else if (guard_tinfo.keyword == RID_ELSE)
12706 warning_at (body_loc, OPT_Wempty_body,
12707 "suggest braces around empty body in an %<else%> statement");
12709 /* if a compound is opened, we simply parse the statement directly. */
12710 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12711 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12712 /* If the token is not a `{', then we must take special action. */
12713 else
12715 /* Create a compound-statement. */
12716 statement = begin_compound_stmt (0);
12717 /* Parse the dependent-statement. */
12718 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12719 &body_loc_after_labels);
12720 /* Finish the dummy compound-statement. */
12721 finish_compound_stmt (statement);
12724 token_indent_info next_tinfo
12725 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12726 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12728 if (body_loc_after_labels != UNKNOWN_LOCATION
12729 && next_tinfo.type != CPP_SEMICOLON)
12730 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12731 guard_tinfo.location, guard_tinfo.keyword);
12733 /* Return the statement. */
12734 return statement;
12737 /* For some dependent statements (like `while (cond) statement'), we
12738 have already created a scope. Therefore, even if the dependent
12739 statement is a compound-statement, we do not want to create another
12740 scope. */
12742 static void
12743 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12744 const token_indent_info &guard_tinfo)
12746 /* If the token is a `{', then we must take special action. */
12747 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12749 token_indent_info body_tinfo
12750 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12751 location_t loc_after_labels = UNKNOWN_LOCATION;
12753 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12754 &loc_after_labels);
12755 token_indent_info next_tinfo
12756 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12757 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12759 if (loc_after_labels != UNKNOWN_LOCATION
12760 && next_tinfo.type != CPP_SEMICOLON)
12761 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12762 guard_tinfo.location,
12763 guard_tinfo.keyword);
12765 else
12767 /* Avoid calling cp_parser_compound_statement, so that we
12768 don't create a new scope. Do everything else by hand. */
12769 matching_braces braces;
12770 braces.require_open (parser);
12771 /* If the next keyword is `__label__' we have a label declaration. */
12772 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12773 cp_parser_label_declaration (parser);
12774 /* Parse an (optional) statement-seq. */
12775 cp_parser_statement_seq_opt (parser, NULL_TREE);
12776 braces.require_close (parser);
12780 /* Declarations [gram.dcl.dcl] */
12782 /* Parse an optional declaration-sequence.
12784 declaration-seq:
12785 declaration
12786 declaration-seq declaration */
12788 static void
12789 cp_parser_declaration_seq_opt (cp_parser* parser)
12791 while (true)
12793 cp_token *token = cp_lexer_peek_token (parser->lexer);
12795 if (token->type == CPP_CLOSE_BRACE
12796 || token->type == CPP_EOF)
12797 break;
12798 else
12799 cp_parser_toplevel_declaration (parser);
12803 /* Parse a declaration.
12805 declaration:
12806 block-declaration
12807 function-definition
12808 template-declaration
12809 explicit-instantiation
12810 explicit-specialization
12811 linkage-specification
12812 namespace-definition
12814 C++17:
12815 deduction-guide
12817 GNU extension:
12819 declaration:
12820 __extension__ declaration */
12822 static void
12823 cp_parser_declaration (cp_parser* parser)
12825 cp_token token1;
12826 cp_token token2;
12827 int saved_pedantic;
12828 void *p;
12829 tree attributes = NULL_TREE;
12831 /* Check for the `__extension__' keyword. */
12832 if (cp_parser_extension_opt (parser, &saved_pedantic))
12834 /* Parse the qualified declaration. */
12835 cp_parser_declaration (parser);
12836 /* Restore the PEDANTIC flag. */
12837 pedantic = saved_pedantic;
12839 return;
12842 /* Try to figure out what kind of declaration is present. */
12843 token1 = *cp_lexer_peek_token (parser->lexer);
12845 if (token1.type != CPP_EOF)
12846 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12847 else
12849 token2.type = CPP_EOF;
12850 token2.keyword = RID_MAX;
12853 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12854 p = obstack_alloc (&declarator_obstack, 0);
12856 /* If the next token is `extern' and the following token is a string
12857 literal, then we have a linkage specification. */
12858 if (token1.keyword == RID_EXTERN
12859 && cp_parser_is_pure_string_literal (&token2))
12860 cp_parser_linkage_specification (parser);
12861 /* If the next token is `template', then we have either a template
12862 declaration, an explicit instantiation, or an explicit
12863 specialization. */
12864 else if (token1.keyword == RID_TEMPLATE)
12866 /* `template <>' indicates a template specialization. */
12867 if (token2.type == CPP_LESS
12868 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12869 cp_parser_explicit_specialization (parser);
12870 /* `template <' indicates a template declaration. */
12871 else if (token2.type == CPP_LESS)
12872 cp_parser_template_declaration (parser, /*member_p=*/false);
12873 /* Anything else must be an explicit instantiation. */
12874 else
12875 cp_parser_explicit_instantiation (parser);
12877 /* If the next token is `export', then we have a template
12878 declaration. */
12879 else if (token1.keyword == RID_EXPORT)
12880 cp_parser_template_declaration (parser, /*member_p=*/false);
12881 /* If the next token is `extern', 'static' or 'inline' and the one
12882 after that is `template', we have a GNU extended explicit
12883 instantiation directive. */
12884 else if (cp_parser_allow_gnu_extensions_p (parser)
12885 && (token1.keyword == RID_EXTERN
12886 || token1.keyword == RID_STATIC
12887 || token1.keyword == RID_INLINE)
12888 && token2.keyword == RID_TEMPLATE)
12889 cp_parser_explicit_instantiation (parser);
12890 /* If the next token is `namespace', check for a named or unnamed
12891 namespace definition. */
12892 else if (token1.keyword == RID_NAMESPACE
12893 && (/* A named namespace definition. */
12894 (token2.type == CPP_NAME
12895 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12896 != CPP_EQ))
12897 || (token2.type == CPP_OPEN_SQUARE
12898 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12899 == CPP_OPEN_SQUARE)
12900 /* An unnamed namespace definition. */
12901 || token2.type == CPP_OPEN_BRACE
12902 || token2.keyword == RID_ATTRIBUTE))
12903 cp_parser_namespace_definition (parser);
12904 /* An inline (associated) namespace definition. */
12905 else if (token1.keyword == RID_INLINE
12906 && token2.keyword == RID_NAMESPACE)
12907 cp_parser_namespace_definition (parser);
12908 /* Objective-C++ declaration/definition. */
12909 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12910 cp_parser_objc_declaration (parser, NULL_TREE);
12911 else if (c_dialect_objc ()
12912 && token1.keyword == RID_ATTRIBUTE
12913 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12914 cp_parser_objc_declaration (parser, attributes);
12915 /* At this point we may have a template declared by a concept
12916 introduction. */
12917 else if (flag_concepts
12918 && cp_parser_template_declaration_after_export (parser,
12919 /*member_p=*/false))
12920 /* We did. */;
12921 else
12922 /* Try to parse a block-declaration, or a function-definition. */
12923 cp_parser_block_declaration (parser, /*statement_p=*/false);
12925 /* Free any declarators allocated. */
12926 obstack_free (&declarator_obstack, p);
12929 /* Parse a namespace-scope declaration. */
12931 static void
12932 cp_parser_toplevel_declaration (cp_parser* parser)
12934 cp_token *token = cp_lexer_peek_token (parser->lexer);
12936 if (token->type == CPP_PRAGMA)
12937 /* A top-level declaration can consist solely of a #pragma. A
12938 nested declaration cannot, so this is done here and not in
12939 cp_parser_declaration. (A #pragma at block scope is
12940 handled in cp_parser_statement.) */
12941 cp_parser_pragma (parser, pragma_external, NULL);
12942 else if (token->type == CPP_SEMICOLON)
12944 /* A declaration consisting of a single semicolon is
12945 invalid. Allow it unless we're being pedantic. */
12946 cp_lexer_consume_token (parser->lexer);
12947 if (!in_system_header_at (input_location))
12948 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12950 else
12951 /* Parse the declaration itself. */
12952 cp_parser_declaration (parser);
12955 /* Parse a block-declaration.
12957 block-declaration:
12958 simple-declaration
12959 asm-definition
12960 namespace-alias-definition
12961 using-declaration
12962 using-directive
12964 GNU Extension:
12966 block-declaration:
12967 __extension__ block-declaration
12969 C++0x Extension:
12971 block-declaration:
12972 static_assert-declaration
12974 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12975 part of a declaration-statement. */
12977 static void
12978 cp_parser_block_declaration (cp_parser *parser,
12979 bool statement_p)
12981 cp_token *token1;
12982 int saved_pedantic;
12984 /* Check for the `__extension__' keyword. */
12985 if (cp_parser_extension_opt (parser, &saved_pedantic))
12987 /* Parse the qualified declaration. */
12988 cp_parser_block_declaration (parser, statement_p);
12989 /* Restore the PEDANTIC flag. */
12990 pedantic = saved_pedantic;
12992 return;
12995 /* Peek at the next token to figure out which kind of declaration is
12996 present. */
12997 token1 = cp_lexer_peek_token (parser->lexer);
12999 /* If the next keyword is `asm', we have an asm-definition. */
13000 if (token1->keyword == RID_ASM)
13002 if (statement_p)
13003 cp_parser_commit_to_tentative_parse (parser);
13004 cp_parser_asm_definition (parser);
13006 /* If the next keyword is `namespace', we have a
13007 namespace-alias-definition. */
13008 else if (token1->keyword == RID_NAMESPACE)
13009 cp_parser_namespace_alias_definition (parser);
13010 /* If the next keyword is `using', we have a
13011 using-declaration, a using-directive, or an alias-declaration. */
13012 else if (token1->keyword == RID_USING)
13014 cp_token *token2;
13016 if (statement_p)
13017 cp_parser_commit_to_tentative_parse (parser);
13018 /* If the token after `using' is `namespace', then we have a
13019 using-directive. */
13020 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13021 if (token2->keyword == RID_NAMESPACE)
13022 cp_parser_using_directive (parser);
13023 /* If the second token after 'using' is '=', then we have an
13024 alias-declaration. */
13025 else if (cxx_dialect >= cxx11
13026 && token2->type == CPP_NAME
13027 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13028 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13029 cp_parser_alias_declaration (parser);
13030 /* Otherwise, it's a using-declaration. */
13031 else
13032 cp_parser_using_declaration (parser,
13033 /*access_declaration_p=*/false);
13035 /* If the next keyword is `__label__' we have a misplaced label
13036 declaration. */
13037 else if (token1->keyword == RID_LABEL)
13039 cp_lexer_consume_token (parser->lexer);
13040 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13041 cp_parser_skip_to_end_of_statement (parser);
13042 /* If the next token is now a `;', consume it. */
13043 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13044 cp_lexer_consume_token (parser->lexer);
13046 /* If the next token is `static_assert' we have a static assertion. */
13047 else if (token1->keyword == RID_STATIC_ASSERT)
13048 cp_parser_static_assert (parser, /*member_p=*/false);
13049 /* Anything else must be a simple-declaration. */
13050 else
13051 cp_parser_simple_declaration (parser, !statement_p,
13052 /*maybe_range_for_decl*/NULL);
13055 /* Parse a simple-declaration.
13057 simple-declaration:
13058 decl-specifier-seq [opt] init-declarator-list [opt] ;
13059 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13060 brace-or-equal-initializer ;
13062 init-declarator-list:
13063 init-declarator
13064 init-declarator-list , init-declarator
13066 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13067 function-definition as a simple-declaration.
13069 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13070 parsed declaration if it is an uninitialized single declarator not followed
13071 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13072 if present, will not be consumed. */
13074 static void
13075 cp_parser_simple_declaration (cp_parser* parser,
13076 bool function_definition_allowed_p,
13077 tree *maybe_range_for_decl)
13079 cp_decl_specifier_seq decl_specifiers;
13080 int declares_class_or_enum;
13081 bool saw_declarator;
13082 location_t comma_loc = UNKNOWN_LOCATION;
13083 location_t init_loc = UNKNOWN_LOCATION;
13085 if (maybe_range_for_decl)
13086 *maybe_range_for_decl = NULL_TREE;
13088 /* Defer access checks until we know what is being declared; the
13089 checks for names appearing in the decl-specifier-seq should be
13090 done as if we were in the scope of the thing being declared. */
13091 push_deferring_access_checks (dk_deferred);
13093 /* Parse the decl-specifier-seq. We have to keep track of whether
13094 or not the decl-specifier-seq declares a named class or
13095 enumeration type, since that is the only case in which the
13096 init-declarator-list is allowed to be empty.
13098 [dcl.dcl]
13100 In a simple-declaration, the optional init-declarator-list can be
13101 omitted only when declaring a class or enumeration, that is when
13102 the decl-specifier-seq contains either a class-specifier, an
13103 elaborated-type-specifier, or an enum-specifier. */
13104 cp_parser_decl_specifier_seq (parser,
13105 CP_PARSER_FLAGS_OPTIONAL,
13106 &decl_specifiers,
13107 &declares_class_or_enum);
13108 /* We no longer need to defer access checks. */
13109 stop_deferring_access_checks ();
13111 /* In a block scope, a valid declaration must always have a
13112 decl-specifier-seq. By not trying to parse declarators, we can
13113 resolve the declaration/expression ambiguity more quickly. */
13114 if (!function_definition_allowed_p
13115 && !decl_specifiers.any_specifiers_p)
13117 cp_parser_error (parser, "expected declaration");
13118 goto done;
13121 /* If the next two tokens are both identifiers, the code is
13122 erroneous. The usual cause of this situation is code like:
13124 T t;
13126 where "T" should name a type -- but does not. */
13127 if (!decl_specifiers.any_type_specifiers_p
13128 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13130 /* If parsing tentatively, we should commit; we really are
13131 looking at a declaration. */
13132 cp_parser_commit_to_tentative_parse (parser);
13133 /* Give up. */
13134 goto done;
13137 cp_parser_maybe_commit_to_declaration (parser,
13138 decl_specifiers.any_specifiers_p);
13140 /* Look for C++17 decomposition declaration. */
13141 for (size_t n = 1; ; n++)
13142 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13143 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13144 continue;
13145 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13146 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13147 && decl_specifiers.any_specifiers_p)
13149 tree decl
13150 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13151 maybe_range_for_decl,
13152 &init_loc);
13154 /* The next token should be either a `,' or a `;'. */
13155 cp_token *token = cp_lexer_peek_token (parser->lexer);
13156 /* If it's a `;', we are done. */
13157 if (token->type == CPP_SEMICOLON)
13158 goto finish;
13159 else if (maybe_range_for_decl)
13161 if (*maybe_range_for_decl == NULL_TREE)
13162 *maybe_range_for_decl = error_mark_node;
13163 goto finish;
13165 /* Anything else is an error. */
13166 else
13168 /* If we have already issued an error message we don't need
13169 to issue another one. */
13170 if ((decl != error_mark_node
13171 && DECL_INITIAL (decl) != error_mark_node)
13172 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13173 cp_parser_error (parser, "expected %<,%> or %<;%>");
13174 /* Skip tokens until we reach the end of the statement. */
13175 cp_parser_skip_to_end_of_statement (parser);
13176 /* If the next token is now a `;', consume it. */
13177 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13178 cp_lexer_consume_token (parser->lexer);
13179 goto done;
13182 else
13183 break;
13185 tree last_type;
13186 bool auto_specifier_p;
13187 /* NULL_TREE if both variable and function declaration are allowed,
13188 error_mark_node if function declaration are not allowed and
13189 a FUNCTION_DECL that should be diagnosed if it is followed by
13190 variable declarations. */
13191 tree auto_function_declaration;
13193 last_type = NULL_TREE;
13194 auto_specifier_p
13195 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13196 auto_function_declaration = NULL_TREE;
13198 /* Keep going until we hit the `;' at the end of the simple
13199 declaration. */
13200 saw_declarator = false;
13201 while (cp_lexer_next_token_is_not (parser->lexer,
13202 CPP_SEMICOLON))
13204 cp_token *token;
13205 bool function_definition_p;
13206 tree decl;
13207 tree auto_result = NULL_TREE;
13209 if (saw_declarator)
13211 /* If we are processing next declarator, comma is expected */
13212 token = cp_lexer_peek_token (parser->lexer);
13213 gcc_assert (token->type == CPP_COMMA);
13214 cp_lexer_consume_token (parser->lexer);
13215 if (maybe_range_for_decl)
13217 *maybe_range_for_decl = error_mark_node;
13218 if (comma_loc == UNKNOWN_LOCATION)
13219 comma_loc = token->location;
13222 else
13223 saw_declarator = true;
13225 /* Parse the init-declarator. */
13226 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13227 /*checks=*/NULL,
13228 function_definition_allowed_p,
13229 /*member_p=*/false,
13230 declares_class_or_enum,
13231 &function_definition_p,
13232 maybe_range_for_decl,
13233 &init_loc,
13234 &auto_result);
13235 /* If an error occurred while parsing tentatively, exit quickly.
13236 (That usually happens when in the body of a function; each
13237 statement is treated as a declaration-statement until proven
13238 otherwise.) */
13239 if (cp_parser_error_occurred (parser))
13240 goto done;
13242 if (auto_specifier_p && cxx_dialect >= cxx14)
13244 /* If the init-declarator-list contains more than one
13245 init-declarator, they shall all form declarations of
13246 variables. */
13247 if (auto_function_declaration == NULL_TREE)
13248 auto_function_declaration
13249 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13250 else if (TREE_CODE (decl) == FUNCTION_DECL
13251 || auto_function_declaration != error_mark_node)
13253 error_at (decl_specifiers.locations[ds_type_spec],
13254 "non-variable %qD in declaration with more than one "
13255 "declarator with placeholder type",
13256 TREE_CODE (decl) == FUNCTION_DECL
13257 ? decl : auto_function_declaration);
13258 auto_function_declaration = error_mark_node;
13262 if (auto_result
13263 && (!processing_template_decl || !type_uses_auto (auto_result)))
13265 if (last_type
13266 && last_type != error_mark_node
13267 && !same_type_p (auto_result, last_type))
13269 /* If the list of declarators contains more than one declarator,
13270 the type of each declared variable is determined as described
13271 above. If the type deduced for the template parameter U is not
13272 the same in each deduction, the program is ill-formed. */
13273 error_at (decl_specifiers.locations[ds_type_spec],
13274 "inconsistent deduction for %qT: %qT and then %qT",
13275 decl_specifiers.type, last_type, auto_result);
13276 last_type = error_mark_node;
13278 else
13279 last_type = auto_result;
13282 /* Handle function definitions specially. */
13283 if (function_definition_p)
13285 /* If the next token is a `,', then we are probably
13286 processing something like:
13288 void f() {}, *p;
13290 which is erroneous. */
13291 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13293 cp_token *token = cp_lexer_peek_token (parser->lexer);
13294 error_at (token->location,
13295 "mixing"
13296 " declarations and function-definitions is forbidden");
13298 /* Otherwise, we're done with the list of declarators. */
13299 else
13301 pop_deferring_access_checks ();
13302 return;
13305 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13306 *maybe_range_for_decl = decl;
13307 /* The next token should be either a `,' or a `;'. */
13308 token = cp_lexer_peek_token (parser->lexer);
13309 /* If it's a `,', there are more declarators to come. */
13310 if (token->type == CPP_COMMA)
13311 /* will be consumed next time around */;
13312 /* If it's a `;', we are done. */
13313 else if (token->type == CPP_SEMICOLON)
13314 break;
13315 else if (maybe_range_for_decl)
13317 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13318 permerror (decl_specifiers.locations[ds_type_spec],
13319 "types may not be defined in a for-range-declaration");
13320 break;
13322 /* Anything else is an error. */
13323 else
13325 /* If we have already issued an error message we don't need
13326 to issue another one. */
13327 if ((decl != error_mark_node
13328 && DECL_INITIAL (decl) != error_mark_node)
13329 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13330 cp_parser_error (parser, "expected %<,%> or %<;%>");
13331 /* Skip tokens until we reach the end of the statement. */
13332 cp_parser_skip_to_end_of_statement (parser);
13333 /* If the next token is now a `;', consume it. */
13334 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13335 cp_lexer_consume_token (parser->lexer);
13336 goto done;
13338 /* After the first time around, a function-definition is not
13339 allowed -- even if it was OK at first. For example:
13341 int i, f() {}
13343 is not valid. */
13344 function_definition_allowed_p = false;
13347 /* Issue an error message if no declarators are present, and the
13348 decl-specifier-seq does not itself declare a class or
13349 enumeration: [dcl.dcl]/3. */
13350 if (!saw_declarator)
13352 if (cp_parser_declares_only_class_p (parser))
13354 if (!declares_class_or_enum
13355 && decl_specifiers.type
13356 && OVERLOAD_TYPE_P (decl_specifiers.type))
13357 /* Ensure an error is issued anyway when finish_decltype_type,
13358 called via cp_parser_decl_specifier_seq, returns a class or
13359 an enumeration (c++/51786). */
13360 decl_specifiers.type = NULL_TREE;
13361 shadow_tag (&decl_specifiers);
13363 /* Perform any deferred access checks. */
13364 perform_deferred_access_checks (tf_warning_or_error);
13367 /* Consume the `;'. */
13368 finish:
13369 if (!maybe_range_for_decl)
13370 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13371 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13373 if (init_loc != UNKNOWN_LOCATION)
13374 error_at (init_loc, "initializer in range-based %<for%> loop");
13375 if (comma_loc != UNKNOWN_LOCATION)
13376 error_at (comma_loc,
13377 "multiple declarations in range-based %<for%> loop");
13380 done:
13381 pop_deferring_access_checks ();
13384 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13385 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13386 initializer ; */
13388 static tree
13389 cp_parser_decomposition_declaration (cp_parser *parser,
13390 cp_decl_specifier_seq *decl_specifiers,
13391 tree *maybe_range_for_decl,
13392 location_t *init_loc)
13394 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13395 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13396 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13398 /* Parse the identifier-list. */
13399 auto_vec<cp_expr, 10> v;
13400 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13401 while (true)
13403 cp_expr e = cp_parser_identifier (parser);
13404 if (e.get_value () == error_mark_node)
13405 break;
13406 v.safe_push (e);
13407 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13408 break;
13409 cp_lexer_consume_token (parser->lexer);
13412 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13413 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13415 end_loc = UNKNOWN_LOCATION;
13416 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13417 false);
13418 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13419 cp_lexer_consume_token (parser->lexer);
13420 else
13422 cp_parser_skip_to_end_of_statement (parser);
13423 return error_mark_node;
13427 if (cxx_dialect < cxx17)
13428 pedwarn (loc, 0, "structured bindings only available with "
13429 "-std=c++17 or -std=gnu++17");
13431 tree pushed_scope;
13432 cp_declarator *declarator = make_declarator (cdk_decomp);
13433 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13434 declarator->id_loc = loc;
13435 if (ref_qual != REF_QUAL_NONE)
13436 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13437 ref_qual == REF_QUAL_RVALUE,
13438 NULL_TREE);
13439 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13440 NULL_TREE, decl_specifiers->attributes,
13441 &pushed_scope);
13442 tree orig_decl = decl;
13444 unsigned int i;
13445 cp_expr e;
13446 cp_decl_specifier_seq decl_specs;
13447 clear_decl_specs (&decl_specs);
13448 decl_specs.type = make_auto ();
13449 tree prev = decl;
13450 FOR_EACH_VEC_ELT (v, i, e)
13452 if (i == 0)
13453 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13454 else
13455 declarator->u.id.unqualified_name = e.get_value ();
13456 declarator->id_loc = e.get_location ();
13457 tree elt_pushed_scope;
13458 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13459 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13460 if (decl2 == error_mark_node)
13461 decl = error_mark_node;
13462 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13464 /* Ensure we've diagnosed redeclaration if we aren't creating
13465 a new VAR_DECL. */
13466 gcc_assert (errorcount);
13467 decl = error_mark_node;
13469 else
13470 prev = decl2;
13471 if (elt_pushed_scope)
13472 pop_scope (elt_pushed_scope);
13475 if (v.is_empty ())
13477 error_at (loc, "empty structured binding declaration");
13478 decl = error_mark_node;
13481 if (maybe_range_for_decl == NULL
13482 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13484 bool non_constant_p = false, is_direct_init = false;
13485 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13486 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13487 &non_constant_p);
13488 if (initializer == NULL_TREE
13489 || (TREE_CODE (initializer) == TREE_LIST
13490 && TREE_CHAIN (initializer))
13491 || (is_direct_init
13492 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13493 && CONSTRUCTOR_NELTS (initializer) != 1))
13495 error_at (loc, "invalid initializer for structured binding "
13496 "declaration");
13497 initializer = error_mark_node;
13500 if (decl != error_mark_node)
13502 cp_maybe_mangle_decomp (decl, prev, v.length ());
13503 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13504 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13505 cp_finish_decomp (decl, prev, v.length ());
13508 else if (decl != error_mark_node)
13510 *maybe_range_for_decl = prev;
13511 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13512 the underlying DECL. */
13513 cp_finish_decomp (decl, prev, v.length ());
13516 if (pushed_scope)
13517 pop_scope (pushed_scope);
13519 if (decl == error_mark_node && DECL_P (orig_decl))
13521 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13522 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13525 return decl;
13528 /* Parse a decl-specifier-seq.
13530 decl-specifier-seq:
13531 decl-specifier-seq [opt] decl-specifier
13532 decl-specifier attribute-specifier-seq [opt] (C++11)
13534 decl-specifier:
13535 storage-class-specifier
13536 type-specifier
13537 function-specifier
13538 friend
13539 typedef
13541 GNU Extension:
13543 decl-specifier:
13544 attributes
13546 Concepts Extension:
13548 decl-specifier:
13549 concept
13551 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13553 The parser flags FLAGS is used to control type-specifier parsing.
13555 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13556 flags:
13558 1: one of the decl-specifiers is an elaborated-type-specifier
13559 (i.e., a type declaration)
13560 2: one of the decl-specifiers is an enum-specifier or a
13561 class-specifier (i.e., a type definition)
13565 static void
13566 cp_parser_decl_specifier_seq (cp_parser* parser,
13567 cp_parser_flags flags,
13568 cp_decl_specifier_seq *decl_specs,
13569 int* declares_class_or_enum)
13571 bool constructor_possible_p = !parser->in_declarator_p;
13572 bool found_decl_spec = false;
13573 cp_token *start_token = NULL;
13574 cp_decl_spec ds;
13576 /* Clear DECL_SPECS. */
13577 clear_decl_specs (decl_specs);
13579 /* Assume no class or enumeration type is declared. */
13580 *declares_class_or_enum = 0;
13582 /* Keep reading specifiers until there are no more to read. */
13583 while (true)
13585 bool constructor_p;
13586 cp_token *token;
13587 ds = ds_last;
13589 /* Peek at the next token. */
13590 token = cp_lexer_peek_token (parser->lexer);
13592 /* Save the first token of the decl spec list for error
13593 reporting. */
13594 if (!start_token)
13595 start_token = token;
13596 /* Handle attributes. */
13597 if (cp_next_tokens_can_be_attribute_p (parser))
13599 /* Parse the attributes. */
13600 tree attrs = cp_parser_attributes_opt (parser);
13602 /* In a sequence of declaration specifiers, c++11 attributes
13603 appertain to the type that precede them. In that case
13604 [dcl.spec]/1 says:
13606 The attribute-specifier-seq affects the type only for
13607 the declaration it appears in, not other declarations
13608 involving the same type.
13610 But for now let's force the user to position the
13611 attribute either at the beginning of the declaration or
13612 after the declarator-id, which would clearly mean that it
13613 applies to the declarator. */
13614 if (cxx11_attribute_p (attrs))
13616 if (!found_decl_spec)
13617 /* The c++11 attribute is at the beginning of the
13618 declaration. It appertains to the entity being
13619 declared. */;
13620 else
13622 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13624 /* This is an attribute following a
13625 class-specifier. */
13626 if (decl_specs->type_definition_p)
13627 warn_misplaced_attr_for_class_type (token->location,
13628 decl_specs->type);
13629 attrs = NULL_TREE;
13631 else
13633 decl_specs->std_attributes
13634 = attr_chainon (decl_specs->std_attributes, attrs);
13635 if (decl_specs->locations[ds_std_attribute] == 0)
13636 decl_specs->locations[ds_std_attribute] = token->location;
13638 continue;
13642 decl_specs->attributes
13643 = attr_chainon (decl_specs->attributes, attrs);
13644 if (decl_specs->locations[ds_attribute] == 0)
13645 decl_specs->locations[ds_attribute] = token->location;
13646 continue;
13648 /* Assume we will find a decl-specifier keyword. */
13649 found_decl_spec = true;
13650 /* If the next token is an appropriate keyword, we can simply
13651 add it to the list. */
13652 switch (token->keyword)
13654 /* decl-specifier:
13655 friend
13656 constexpr */
13657 case RID_FRIEND:
13658 if (!at_class_scope_p ())
13660 gcc_rich_location richloc (token->location);
13661 richloc.add_fixit_remove ();
13662 error_at (&richloc, "%<friend%> used outside of class");
13663 cp_lexer_purge_token (parser->lexer);
13665 else
13667 ds = ds_friend;
13668 /* Consume the token. */
13669 cp_lexer_consume_token (parser->lexer);
13671 break;
13673 case RID_CONSTEXPR:
13674 ds = ds_constexpr;
13675 cp_lexer_consume_token (parser->lexer);
13676 break;
13678 case RID_CONCEPT:
13679 ds = ds_concept;
13680 cp_lexer_consume_token (parser->lexer);
13681 break;
13683 /* function-specifier:
13684 inline
13685 virtual
13686 explicit */
13687 case RID_INLINE:
13688 case RID_VIRTUAL:
13689 case RID_EXPLICIT:
13690 cp_parser_function_specifier_opt (parser, decl_specs);
13691 break;
13693 /* decl-specifier:
13694 typedef */
13695 case RID_TYPEDEF:
13696 ds = ds_typedef;
13697 /* Consume the token. */
13698 cp_lexer_consume_token (parser->lexer);
13699 /* A constructor declarator cannot appear in a typedef. */
13700 constructor_possible_p = false;
13701 /* The "typedef" keyword can only occur in a declaration; we
13702 may as well commit at this point. */
13703 cp_parser_commit_to_tentative_parse (parser);
13705 if (decl_specs->storage_class != sc_none)
13706 decl_specs->conflicting_specifiers_p = true;
13707 break;
13709 /* storage-class-specifier:
13710 auto
13711 register
13712 static
13713 extern
13714 mutable
13716 GNU Extension:
13717 thread */
13718 case RID_AUTO:
13719 if (cxx_dialect == cxx98)
13721 /* Consume the token. */
13722 cp_lexer_consume_token (parser->lexer);
13724 /* Complain about `auto' as a storage specifier, if
13725 we're complaining about C++0x compatibility. */
13726 gcc_rich_location richloc (token->location);
13727 richloc.add_fixit_remove ();
13728 warning_at (&richloc, OPT_Wc__11_compat,
13729 "%<auto%> changes meaning in C++11; "
13730 "please remove it");
13732 /* Set the storage class anyway. */
13733 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13734 token);
13736 else
13737 /* C++0x auto type-specifier. */
13738 found_decl_spec = false;
13739 break;
13741 case RID_REGISTER:
13742 case RID_STATIC:
13743 case RID_EXTERN:
13744 case RID_MUTABLE:
13745 /* Consume the token. */
13746 cp_lexer_consume_token (parser->lexer);
13747 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13748 token);
13749 break;
13750 case RID_THREAD:
13751 /* Consume the token. */
13752 ds = ds_thread;
13753 cp_lexer_consume_token (parser->lexer);
13754 break;
13756 default:
13757 /* We did not yet find a decl-specifier yet. */
13758 found_decl_spec = false;
13759 break;
13762 if (found_decl_spec
13763 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13764 && token->keyword != RID_CONSTEXPR)
13765 error ("decl-specifier invalid in condition");
13767 if (found_decl_spec
13768 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13769 && token->keyword != RID_MUTABLE
13770 && token->keyword != RID_CONSTEXPR)
13771 error_at (token->location, "%qD invalid in lambda",
13772 ridpointers[token->keyword]);
13774 if (ds != ds_last)
13775 set_and_check_decl_spec_loc (decl_specs, ds, token);
13777 /* Constructors are a special case. The `S' in `S()' is not a
13778 decl-specifier; it is the beginning of the declarator. */
13779 constructor_p
13780 = (!found_decl_spec
13781 && constructor_possible_p
13782 && (cp_parser_constructor_declarator_p
13783 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13785 /* If we don't have a DECL_SPEC yet, then we must be looking at
13786 a type-specifier. */
13787 if (!found_decl_spec && !constructor_p)
13789 int decl_spec_declares_class_or_enum;
13790 bool is_cv_qualifier;
13791 tree type_spec;
13793 type_spec
13794 = cp_parser_type_specifier (parser, flags,
13795 decl_specs,
13796 /*is_declaration=*/true,
13797 &decl_spec_declares_class_or_enum,
13798 &is_cv_qualifier);
13799 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13801 /* If this type-specifier referenced a user-defined type
13802 (a typedef, class-name, etc.), then we can't allow any
13803 more such type-specifiers henceforth.
13805 [dcl.spec]
13807 The longest sequence of decl-specifiers that could
13808 possibly be a type name is taken as the
13809 decl-specifier-seq of a declaration. The sequence shall
13810 be self-consistent as described below.
13812 [dcl.type]
13814 As a general rule, at most one type-specifier is allowed
13815 in the complete decl-specifier-seq of a declaration. The
13816 only exceptions are the following:
13818 -- const or volatile can be combined with any other
13819 type-specifier.
13821 -- signed or unsigned can be combined with char, long,
13822 short, or int.
13824 -- ..
13826 Example:
13828 typedef char* Pc;
13829 void g (const int Pc);
13831 Here, Pc is *not* part of the decl-specifier seq; it's
13832 the declarator. Therefore, once we see a type-specifier
13833 (other than a cv-qualifier), we forbid any additional
13834 user-defined types. We *do* still allow things like `int
13835 int' to be considered a decl-specifier-seq, and issue the
13836 error message later. */
13837 if (type_spec && !is_cv_qualifier)
13838 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13839 /* A constructor declarator cannot follow a type-specifier. */
13840 if (type_spec)
13842 constructor_possible_p = false;
13843 found_decl_spec = true;
13844 if (!is_cv_qualifier)
13845 decl_specs->any_type_specifiers_p = true;
13847 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
13848 error_at (token->location, "type-specifier invalid in lambda");
13852 /* If we still do not have a DECL_SPEC, then there are no more
13853 decl-specifiers. */
13854 if (!found_decl_spec)
13855 break;
13857 decl_specs->any_specifiers_p = true;
13858 /* After we see one decl-specifier, further decl-specifiers are
13859 always optional. */
13860 flags |= CP_PARSER_FLAGS_OPTIONAL;
13863 /* Don't allow a friend specifier with a class definition. */
13864 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13865 && (*declares_class_or_enum & 2))
13866 error_at (decl_specs->locations[ds_friend],
13867 "class definition may not be declared a friend");
13870 /* Parse an (optional) storage-class-specifier.
13872 storage-class-specifier:
13873 auto
13874 register
13875 static
13876 extern
13877 mutable
13879 GNU Extension:
13881 storage-class-specifier:
13882 thread
13884 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13886 static tree
13887 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13889 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13891 case RID_AUTO:
13892 if (cxx_dialect != cxx98)
13893 return NULL_TREE;
13894 /* Fall through for C++98. */
13895 gcc_fallthrough ();
13897 case RID_REGISTER:
13898 case RID_STATIC:
13899 case RID_EXTERN:
13900 case RID_MUTABLE:
13901 case RID_THREAD:
13902 /* Consume the token. */
13903 return cp_lexer_consume_token (parser->lexer)->u.value;
13905 default:
13906 return NULL_TREE;
13910 /* Parse an (optional) function-specifier.
13912 function-specifier:
13913 inline
13914 virtual
13915 explicit
13917 C++2A Extension:
13918 explicit(constant-expression)
13920 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13921 Updates DECL_SPECS, if it is non-NULL. */
13923 static tree
13924 cp_parser_function_specifier_opt (cp_parser* parser,
13925 cp_decl_specifier_seq *decl_specs)
13927 cp_token *token = cp_lexer_peek_token (parser->lexer);
13928 switch (token->keyword)
13930 case RID_INLINE:
13931 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13932 break;
13934 case RID_VIRTUAL:
13935 /* 14.5.2.3 [temp.mem]
13937 A member function template shall not be virtual. */
13938 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13939 && current_class_type)
13940 error_at (token->location, "templates may not be %<virtual%>");
13941 else
13942 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13943 break;
13945 case RID_EXPLICIT:
13947 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
13948 /* If we see '(', it's C++20 explicit(bool). */
13949 tree expr;
13950 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13952 matching_parens parens;
13953 parens.consume_open (parser);
13955 /* New types are not allowed in an explicit-specifier. */
13956 const char *saved_message
13957 = parser->type_definition_forbidden_message;
13958 parser->type_definition_forbidden_message
13959 = G_("types may not be defined in explicit-specifier");
13961 if (cxx_dialect < cxx2a)
13962 pedwarn (token->location, 0,
13963 "%<explicit(bool)%> only available with -std=c++2a "
13964 "or -std=gnu++2a");
13966 /* Parse the constant-expression. */
13967 expr = cp_parser_constant_expression (parser);
13969 /* Restore the saved message. */
13970 parser->type_definition_forbidden_message = saved_message;
13971 parens.require_close (parser);
13973 else
13974 /* The explicit-specifier explicit without a constant-expression is
13975 equivalent to the explicit-specifier explicit(true). */
13976 expr = boolean_true_node;
13978 /* [dcl.fct.spec]
13979 "the constant-expression, if supplied, shall be a contextually
13980 converted constant expression of type bool." */
13981 expr = build_explicit_specifier (expr, tf_warning_or_error);
13982 /* We could evaluate it -- mark the decl as appropriate. */
13983 if (expr == boolean_true_node)
13984 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13985 else if (expr == boolean_false_node)
13986 /* Don't mark the decl as explicit. */;
13987 else if (decl_specs)
13988 /* The expression was value-dependent. Remember it so that we can
13989 substitute it later. */
13990 decl_specs->explicit_specifier = expr;
13991 return id;
13994 default:
13995 return NULL_TREE;
13998 /* Consume the token. */
13999 return cp_lexer_consume_token (parser->lexer)->u.value;
14002 /* Parse a linkage-specification.
14004 linkage-specification:
14005 extern string-literal { declaration-seq [opt] }
14006 extern string-literal declaration */
14008 static void
14009 cp_parser_linkage_specification (cp_parser* parser)
14011 tree linkage;
14013 /* Look for the `extern' keyword. */
14014 cp_token *extern_token
14015 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14017 /* Look for the string-literal. */
14018 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14019 linkage = cp_parser_string_literal (parser, false, false);
14021 /* Transform the literal into an identifier. If the literal is a
14022 wide-character string, or contains embedded NULs, then we can't
14023 handle it as the user wants. */
14024 if (strlen (TREE_STRING_POINTER (linkage))
14025 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14027 cp_parser_error (parser, "invalid linkage-specification");
14028 /* Assume C++ linkage. */
14029 linkage = lang_name_cplusplus;
14031 else
14032 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14034 /* We're now using the new linkage. */
14035 push_lang_context (linkage);
14037 /* Preserve the location of the the innermost linkage specification,
14038 tracking the locations of nested specifications via a local. */
14039 location_t saved_location
14040 = parser->innermost_linkage_specification_location;
14041 /* Construct a location ranging from the start of the "extern" to
14042 the end of the string-literal, with the caret at the start, e.g.:
14043 extern "C" {
14044 ^~~~~~~~~~
14046 parser->innermost_linkage_specification_location
14047 = make_location (extern_token->location,
14048 extern_token->location,
14049 get_finish (string_token->location));
14051 /* If the next token is a `{', then we're using the first
14052 production. */
14053 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14055 cp_ensure_no_omp_declare_simd (parser);
14056 cp_ensure_no_oacc_routine (parser);
14058 /* Consume the `{' token. */
14059 matching_braces braces;
14060 braces.consume_open (parser)->location;
14061 /* Parse the declarations. */
14062 cp_parser_declaration_seq_opt (parser);
14063 /* Look for the closing `}'. */
14064 braces.require_close (parser);
14066 /* Otherwise, there's just one declaration. */
14067 else
14069 bool saved_in_unbraced_linkage_specification_p;
14071 saved_in_unbraced_linkage_specification_p
14072 = parser->in_unbraced_linkage_specification_p;
14073 parser->in_unbraced_linkage_specification_p = true;
14074 cp_parser_declaration (parser);
14075 parser->in_unbraced_linkage_specification_p
14076 = saved_in_unbraced_linkage_specification_p;
14079 /* We're done with the linkage-specification. */
14080 pop_lang_context ();
14082 /* Restore location of parent linkage specification, if any. */
14083 parser->innermost_linkage_specification_location = saved_location;
14086 /* Parse a static_assert-declaration.
14088 static_assert-declaration:
14089 static_assert ( constant-expression , string-literal ) ;
14090 static_assert ( constant-expression ) ; (C++17)
14092 If MEMBER_P, this static_assert is a class member. */
14094 static void
14095 cp_parser_static_assert(cp_parser *parser, bool member_p)
14097 cp_expr condition;
14098 location_t token_loc;
14099 tree message;
14100 bool dummy;
14102 /* Peek at the `static_assert' token so we can keep track of exactly
14103 where the static assertion started. */
14104 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14106 /* Look for the `static_assert' keyword. */
14107 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14108 RT_STATIC_ASSERT))
14109 return;
14111 /* We know we are in a static assertion; commit to any tentative
14112 parse. */
14113 if (cp_parser_parsing_tentatively (parser))
14114 cp_parser_commit_to_tentative_parse (parser);
14116 /* Parse the `(' starting the static assertion condition. */
14117 matching_parens parens;
14118 parens.require_open (parser);
14120 /* Parse the constant-expression. Allow a non-constant expression
14121 here in order to give better diagnostics in finish_static_assert. */
14122 condition =
14123 cp_parser_constant_expression (parser,
14124 /*allow_non_constant_p=*/true,
14125 /*non_constant_p=*/&dummy);
14127 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14129 if (cxx_dialect < cxx17)
14130 pedwarn (input_location, OPT_Wpedantic,
14131 "static_assert without a message "
14132 "only available with -std=c++17 or -std=gnu++17");
14133 /* Eat the ')' */
14134 cp_lexer_consume_token (parser->lexer);
14135 message = build_string (1, "");
14136 TREE_TYPE (message) = char_array_type_node;
14137 fix_string_type (message);
14139 else
14141 /* Parse the separating `,'. */
14142 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14144 /* Parse the string-literal message. */
14145 message = cp_parser_string_literal (parser,
14146 /*translate=*/false,
14147 /*wide_ok=*/true);
14149 /* A `)' completes the static assertion. */
14150 if (!parens.require_close (parser))
14151 cp_parser_skip_to_closing_parenthesis (parser,
14152 /*recovering=*/true,
14153 /*or_comma=*/false,
14154 /*consume_paren=*/true);
14157 /* A semicolon terminates the declaration. */
14158 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14160 /* Get the location for the static assertion. Use that of the
14161 condition if available, otherwise, use that of the "static_assert"
14162 token. */
14163 location_t assert_loc = condition.get_location ();
14164 if (assert_loc == UNKNOWN_LOCATION)
14165 assert_loc = token_loc;
14167 /* Complete the static assertion, which may mean either processing
14168 the static assert now or saving it for template instantiation. */
14169 finish_static_assert (condition, message, assert_loc, member_p);
14172 /* Parse the expression in decltype ( expression ). */
14174 static tree
14175 cp_parser_decltype_expr (cp_parser *parser,
14176 bool &id_expression_or_member_access_p)
14178 cp_token *id_expr_start_token;
14179 tree expr;
14181 /* Since we're going to preserve any side-effects from this parse, set up a
14182 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14183 in the expression. */
14184 tentative_firewall firewall (parser);
14186 /* First, try parsing an id-expression. */
14187 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14188 cp_parser_parse_tentatively (parser);
14189 expr = cp_parser_id_expression (parser,
14190 /*template_keyword_p=*/false,
14191 /*check_dependency_p=*/true,
14192 /*template_p=*/NULL,
14193 /*declarator_p=*/false,
14194 /*optional_p=*/false);
14196 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14198 bool non_integral_constant_expression_p = false;
14199 tree id_expression = expr;
14200 cp_id_kind idk;
14201 const char *error_msg;
14203 if (identifier_p (expr))
14204 /* Lookup the name we got back from the id-expression. */
14205 expr = cp_parser_lookup_name_simple (parser, expr,
14206 id_expr_start_token->location);
14208 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14209 /* A template without args is not a complete id-expression. */
14210 expr = error_mark_node;
14212 if (expr
14213 && expr != error_mark_node
14214 && TREE_CODE (expr) != TYPE_DECL
14215 && (TREE_CODE (expr) != BIT_NOT_EXPR
14216 || !TYPE_P (TREE_OPERAND (expr, 0)))
14217 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14219 /* Complete lookup of the id-expression. */
14220 expr = (finish_id_expression
14221 (id_expression, expr, parser->scope, &idk,
14222 /*integral_constant_expression_p=*/false,
14223 /*allow_non_integral_constant_expression_p=*/true,
14224 &non_integral_constant_expression_p,
14225 /*template_p=*/false,
14226 /*done=*/true,
14227 /*address_p=*/false,
14228 /*template_arg_p=*/false,
14229 &error_msg,
14230 id_expr_start_token->location));
14232 if (expr == error_mark_node)
14233 /* We found an id-expression, but it was something that we
14234 should not have found. This is an error, not something
14235 we can recover from, so note that we found an
14236 id-expression and we'll recover as gracefully as
14237 possible. */
14238 id_expression_or_member_access_p = true;
14241 if (expr
14242 && expr != error_mark_node
14243 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14244 /* We have an id-expression. */
14245 id_expression_or_member_access_p = true;
14248 if (!id_expression_or_member_access_p)
14250 /* Abort the id-expression parse. */
14251 cp_parser_abort_tentative_parse (parser);
14253 /* Parsing tentatively, again. */
14254 cp_parser_parse_tentatively (parser);
14256 /* Parse a class member access. */
14257 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14258 /*cast_p=*/false, /*decltype*/true,
14259 /*member_access_only_p=*/true, NULL);
14261 if (expr
14262 && expr != error_mark_node
14263 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14264 /* We have an id-expression. */
14265 id_expression_or_member_access_p = true;
14268 if (id_expression_or_member_access_p)
14269 /* We have parsed the complete id-expression or member access. */
14270 cp_parser_parse_definitely (parser);
14271 else
14273 /* Abort our attempt to parse an id-expression or member access
14274 expression. */
14275 cp_parser_abort_tentative_parse (parser);
14277 /* Commit to the tentative_firewall so we get syntax errors. */
14278 cp_parser_commit_to_tentative_parse (parser);
14280 /* Parse a full expression. */
14281 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14282 /*decltype_p=*/true);
14285 return expr;
14288 /* Parse a `decltype' type. Returns the type.
14290 simple-type-specifier:
14291 decltype ( expression )
14292 C++14 proposal:
14293 decltype ( auto ) */
14295 static tree
14296 cp_parser_decltype (cp_parser *parser)
14298 bool id_expression_or_member_access_p = false;
14299 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14301 if (start_token->type == CPP_DECLTYPE)
14303 /* Already parsed. */
14304 cp_lexer_consume_token (parser->lexer);
14305 return saved_checks_value (start_token->u.tree_check_value);
14308 /* Look for the `decltype' token. */
14309 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14310 return error_mark_node;
14312 /* Parse the opening `('. */
14313 matching_parens parens;
14314 if (!parens.require_open (parser))
14315 return error_mark_node;
14317 push_deferring_access_checks (dk_deferred);
14319 tree expr = NULL_TREE;
14321 if (cxx_dialect >= cxx14
14322 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14323 /* decltype (auto) */
14324 cp_lexer_consume_token (parser->lexer);
14325 else
14327 /* decltype (expression) */
14329 /* Types cannot be defined in a `decltype' expression. Save away the
14330 old message and set the new one. */
14331 const char *saved_message = parser->type_definition_forbidden_message;
14332 parser->type_definition_forbidden_message
14333 = G_("types may not be defined in %<decltype%> expressions");
14335 /* The restrictions on constant-expressions do not apply inside
14336 decltype expressions. */
14337 bool saved_integral_constant_expression_p
14338 = parser->integral_constant_expression_p;
14339 bool saved_non_integral_constant_expression_p
14340 = parser->non_integral_constant_expression_p;
14341 parser->integral_constant_expression_p = false;
14343 /* Within a parenthesized expression, a `>' token is always
14344 the greater-than operator. */
14345 bool saved_greater_than_is_operator_p
14346 = parser->greater_than_is_operator_p;
14347 parser->greater_than_is_operator_p = true;
14349 /* Do not actually evaluate the expression. */
14350 ++cp_unevaluated_operand;
14352 /* Do not warn about problems with the expression. */
14353 ++c_inhibit_evaluation_warnings;
14355 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14357 /* Go back to evaluating expressions. */
14358 --cp_unevaluated_operand;
14359 --c_inhibit_evaluation_warnings;
14361 /* The `>' token might be the end of a template-id or
14362 template-parameter-list now. */
14363 parser->greater_than_is_operator_p
14364 = saved_greater_than_is_operator_p;
14366 /* Restore the old message and the integral constant expression
14367 flags. */
14368 parser->type_definition_forbidden_message = saved_message;
14369 parser->integral_constant_expression_p
14370 = saved_integral_constant_expression_p;
14371 parser->non_integral_constant_expression_p
14372 = saved_non_integral_constant_expression_p;
14375 /* Parse to the closing `)'. */
14376 if (!parens.require_close (parser))
14378 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14379 /*consume_paren=*/true);
14380 pop_deferring_access_checks ();
14381 return error_mark_node;
14384 if (!expr)
14386 /* Build auto. */
14387 expr = make_decltype_auto ();
14388 AUTO_IS_DECLTYPE (expr) = true;
14390 else
14391 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14392 tf_warning_or_error);
14394 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14395 it again. */
14396 start_token->type = CPP_DECLTYPE;
14397 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14398 start_token->u.tree_check_value->value = expr;
14399 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14400 start_token->keyword = RID_MAX;
14401 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14403 pop_to_parent_deferring_access_checks ();
14405 return expr;
14408 /* Special member functions [gram.special] */
14410 /* Parse a conversion-function-id.
14412 conversion-function-id:
14413 operator conversion-type-id
14415 Returns an IDENTIFIER_NODE representing the operator. */
14417 static tree
14418 cp_parser_conversion_function_id (cp_parser* parser)
14420 tree type;
14421 tree saved_scope;
14422 tree saved_qualifying_scope;
14423 tree saved_object_scope;
14424 tree pushed_scope = NULL_TREE;
14426 /* Look for the `operator' token. */
14427 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14428 return error_mark_node;
14429 /* When we parse the conversion-type-id, the current scope will be
14430 reset. However, we need that information in able to look up the
14431 conversion function later, so we save it here. */
14432 saved_scope = parser->scope;
14433 saved_qualifying_scope = parser->qualifying_scope;
14434 saved_object_scope = parser->object_scope;
14435 /* We must enter the scope of the class so that the names of
14436 entities declared within the class are available in the
14437 conversion-type-id. For example, consider:
14439 struct S {
14440 typedef int I;
14441 operator I();
14444 S::operator I() { ... }
14446 In order to see that `I' is a type-name in the definition, we
14447 must be in the scope of `S'. */
14448 if (saved_scope)
14449 pushed_scope = push_scope (saved_scope);
14450 /* Parse the conversion-type-id. */
14451 type = cp_parser_conversion_type_id (parser);
14452 /* Leave the scope of the class, if any. */
14453 if (pushed_scope)
14454 pop_scope (pushed_scope);
14455 /* Restore the saved scope. */
14456 parser->scope = saved_scope;
14457 parser->qualifying_scope = saved_qualifying_scope;
14458 parser->object_scope = saved_object_scope;
14459 /* If the TYPE is invalid, indicate failure. */
14460 if (type == error_mark_node)
14461 return error_mark_node;
14462 return make_conv_op_name (type);
14465 /* Parse a conversion-type-id:
14467 conversion-type-id:
14468 type-specifier-seq conversion-declarator [opt]
14470 Returns the TYPE specified. */
14472 static tree
14473 cp_parser_conversion_type_id (cp_parser* parser)
14475 tree attributes;
14476 cp_decl_specifier_seq type_specifiers;
14477 cp_declarator *declarator;
14478 tree type_specified;
14479 const char *saved_message;
14481 /* Parse the attributes. */
14482 attributes = cp_parser_attributes_opt (parser);
14484 saved_message = parser->type_definition_forbidden_message;
14485 parser->type_definition_forbidden_message
14486 = G_("types may not be defined in a conversion-type-id");
14488 /* Parse the type-specifiers. */
14489 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14490 /*is_trailing_return=*/false,
14491 &type_specifiers);
14493 parser->type_definition_forbidden_message = saved_message;
14495 /* If that didn't work, stop. */
14496 if (type_specifiers.type == error_mark_node)
14497 return error_mark_node;
14498 /* Parse the conversion-declarator. */
14499 declarator = cp_parser_conversion_declarator_opt (parser);
14501 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14502 /*initialized=*/0, &attributes);
14503 if (attributes)
14504 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14506 /* Don't give this error when parsing tentatively. This happens to
14507 work because we always parse this definitively once. */
14508 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14509 && type_uses_auto (type_specified))
14511 if (cxx_dialect < cxx14)
14513 error ("invalid use of %<auto%> in conversion operator");
14514 return error_mark_node;
14516 else if (template_parm_scope_p ())
14517 warning (0, "use of %<auto%> in member template "
14518 "conversion operator can never be deduced");
14521 return type_specified;
14524 /* Parse an (optional) conversion-declarator.
14526 conversion-declarator:
14527 ptr-operator conversion-declarator [opt]
14531 static cp_declarator *
14532 cp_parser_conversion_declarator_opt (cp_parser* parser)
14534 enum tree_code code;
14535 tree class_type, std_attributes = NULL_TREE;
14536 cp_cv_quals cv_quals;
14538 /* We don't know if there's a ptr-operator next, or not. */
14539 cp_parser_parse_tentatively (parser);
14540 /* Try the ptr-operator. */
14541 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14542 &std_attributes);
14543 /* If it worked, look for more conversion-declarators. */
14544 if (cp_parser_parse_definitely (parser))
14546 cp_declarator *declarator;
14548 /* Parse another optional declarator. */
14549 declarator = cp_parser_conversion_declarator_opt (parser);
14551 declarator = cp_parser_make_indirect_declarator
14552 (code, class_type, cv_quals, declarator, std_attributes);
14554 return declarator;
14557 return NULL;
14560 /* Parse an (optional) ctor-initializer.
14562 ctor-initializer:
14563 : mem-initializer-list */
14565 static void
14566 cp_parser_ctor_initializer_opt (cp_parser* parser)
14568 /* If the next token is not a `:', then there is no
14569 ctor-initializer. */
14570 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14572 /* Do default initialization of any bases and members. */
14573 if (DECL_CONSTRUCTOR_P (current_function_decl))
14574 finish_mem_initializers (NULL_TREE);
14575 return;
14578 /* Consume the `:' token. */
14579 cp_lexer_consume_token (parser->lexer);
14580 /* And the mem-initializer-list. */
14581 cp_parser_mem_initializer_list (parser);
14584 /* Parse a mem-initializer-list.
14586 mem-initializer-list:
14587 mem-initializer ... [opt]
14588 mem-initializer ... [opt] , mem-initializer-list */
14590 static void
14591 cp_parser_mem_initializer_list (cp_parser* parser)
14593 tree mem_initializer_list = NULL_TREE;
14594 tree target_ctor = error_mark_node;
14595 cp_token *token = cp_lexer_peek_token (parser->lexer);
14597 /* Let the semantic analysis code know that we are starting the
14598 mem-initializer-list. */
14599 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14600 error_at (token->location,
14601 "only constructors take member initializers");
14603 /* Loop through the list. */
14604 while (true)
14606 tree mem_initializer;
14608 token = cp_lexer_peek_token (parser->lexer);
14609 /* Parse the mem-initializer. */
14610 mem_initializer = cp_parser_mem_initializer (parser);
14611 /* If the next token is a `...', we're expanding member initializers. */
14612 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14613 if (ellipsis
14614 || (mem_initializer != error_mark_node
14615 && check_for_bare_parameter_packs (TREE_PURPOSE
14616 (mem_initializer))))
14618 /* Consume the `...'. */
14619 if (ellipsis)
14620 cp_lexer_consume_token (parser->lexer);
14622 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14623 can be expanded but members cannot. */
14624 if (mem_initializer != error_mark_node
14625 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14627 error_at (token->location,
14628 "cannot expand initializer for member %qD",
14629 TREE_PURPOSE (mem_initializer));
14630 mem_initializer = error_mark_node;
14633 /* Construct the pack expansion type. */
14634 if (mem_initializer != error_mark_node)
14635 mem_initializer = make_pack_expansion (mem_initializer);
14637 if (target_ctor != error_mark_node
14638 && mem_initializer != error_mark_node)
14640 error ("mem-initializer for %qD follows constructor delegation",
14641 TREE_PURPOSE (mem_initializer));
14642 mem_initializer = error_mark_node;
14644 /* Look for a target constructor. */
14645 if (mem_initializer != error_mark_node
14646 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14647 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14649 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14650 if (mem_initializer_list)
14652 error ("constructor delegation follows mem-initializer for %qD",
14653 TREE_PURPOSE (mem_initializer_list));
14654 mem_initializer = error_mark_node;
14656 target_ctor = mem_initializer;
14658 /* Add it to the list, unless it was erroneous. */
14659 if (mem_initializer != error_mark_node)
14661 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14662 mem_initializer_list = mem_initializer;
14664 /* If the next token is not a `,', we're done. */
14665 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14666 break;
14667 /* Consume the `,' token. */
14668 cp_lexer_consume_token (parser->lexer);
14671 /* Perform semantic analysis. */
14672 if (DECL_CONSTRUCTOR_P (current_function_decl))
14673 finish_mem_initializers (mem_initializer_list);
14676 /* Parse a mem-initializer.
14678 mem-initializer:
14679 mem-initializer-id ( expression-list [opt] )
14680 mem-initializer-id braced-init-list
14682 GNU extension:
14684 mem-initializer:
14685 ( expression-list [opt] )
14687 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14688 class) or FIELD_DECL (for a non-static data member) to initialize;
14689 the TREE_VALUE is the expression-list. An empty initialization
14690 list is represented by void_list_node. */
14692 static tree
14693 cp_parser_mem_initializer (cp_parser* parser)
14695 tree mem_initializer_id;
14696 tree expression_list;
14697 tree member;
14698 cp_token *token = cp_lexer_peek_token (parser->lexer);
14700 /* Find out what is being initialized. */
14701 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14703 permerror (token->location,
14704 "anachronistic old-style base class initializer");
14705 mem_initializer_id = NULL_TREE;
14707 else
14709 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14710 if (mem_initializer_id == error_mark_node)
14711 return mem_initializer_id;
14713 member = expand_member_init (mem_initializer_id);
14714 if (member && !DECL_P (member))
14715 in_base_initializer = 1;
14717 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14719 bool expr_non_constant_p;
14720 cp_lexer_set_source_position (parser->lexer);
14721 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14722 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14723 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14724 expression_list = build_tree_list (NULL_TREE, expression_list);
14726 else
14728 vec<tree, va_gc> *vec;
14729 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14730 /*cast_p=*/false,
14731 /*allow_expansion_p=*/true,
14732 /*non_constant_p=*/NULL);
14733 if (vec == NULL)
14734 return error_mark_node;
14735 expression_list = build_tree_list_vec (vec);
14736 release_tree_vector (vec);
14739 if (expression_list == error_mark_node)
14740 return error_mark_node;
14741 if (!expression_list)
14742 expression_list = void_type_node;
14744 in_base_initializer = 0;
14746 return member ? build_tree_list (member, expression_list) : error_mark_node;
14749 /* Parse a mem-initializer-id.
14751 mem-initializer-id:
14752 :: [opt] nested-name-specifier [opt] class-name
14753 decltype-specifier (C++11)
14754 identifier
14756 Returns a TYPE indicating the class to be initialized for the first
14757 production (and the second in C++11). Returns an IDENTIFIER_NODE
14758 indicating the data member to be initialized for the last production. */
14760 static tree
14761 cp_parser_mem_initializer_id (cp_parser* parser)
14763 bool global_scope_p;
14764 bool nested_name_specifier_p;
14765 bool template_p = false;
14766 tree id;
14768 cp_token *token = cp_lexer_peek_token (parser->lexer);
14770 /* `typename' is not allowed in this context ([temp.res]). */
14771 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14773 error_at (token->location,
14774 "keyword %<typename%> not allowed in this context (a qualified "
14775 "member initializer is implicitly a type)");
14776 cp_lexer_consume_token (parser->lexer);
14778 /* Look for the optional `::' operator. */
14779 global_scope_p
14780 = (cp_parser_global_scope_opt (parser,
14781 /*current_scope_valid_p=*/false)
14782 != NULL_TREE);
14783 /* Look for the optional nested-name-specifier. The simplest way to
14784 implement:
14786 [temp.res]
14788 The keyword `typename' is not permitted in a base-specifier or
14789 mem-initializer; in these contexts a qualified name that
14790 depends on a template-parameter is implicitly assumed to be a
14791 type name.
14793 is to assume that we have seen the `typename' keyword at this
14794 point. */
14795 nested_name_specifier_p
14796 = (cp_parser_nested_name_specifier_opt (parser,
14797 /*typename_keyword_p=*/true,
14798 /*check_dependency_p=*/true,
14799 /*type_p=*/true,
14800 /*is_declaration=*/true)
14801 != NULL_TREE);
14802 if (nested_name_specifier_p)
14803 template_p = cp_parser_optional_template_keyword (parser);
14804 /* If there is a `::' operator or a nested-name-specifier, then we
14805 are definitely looking for a class-name. */
14806 if (global_scope_p || nested_name_specifier_p)
14807 return cp_parser_class_name (parser,
14808 /*typename_keyword_p=*/true,
14809 /*template_keyword_p=*/template_p,
14810 typename_type,
14811 /*check_dependency_p=*/true,
14812 /*class_head_p=*/false,
14813 /*is_declaration=*/true);
14814 /* Otherwise, we could also be looking for an ordinary identifier. */
14815 cp_parser_parse_tentatively (parser);
14816 if (cp_lexer_next_token_is_decltype (parser->lexer))
14817 /* Try a decltype-specifier. */
14818 id = cp_parser_decltype (parser);
14819 else
14820 /* Otherwise, try a class-name. */
14821 id = cp_parser_class_name (parser,
14822 /*typename_keyword_p=*/true,
14823 /*template_keyword_p=*/false,
14824 none_type,
14825 /*check_dependency_p=*/true,
14826 /*class_head_p=*/false,
14827 /*is_declaration=*/true);
14828 /* If we found one, we're done. */
14829 if (cp_parser_parse_definitely (parser))
14830 return id;
14831 /* Otherwise, look for an ordinary identifier. */
14832 return cp_parser_identifier (parser);
14835 /* Overloading [gram.over] */
14837 /* Parse an operator-function-id.
14839 operator-function-id:
14840 operator operator
14842 Returns an IDENTIFIER_NODE for the operator which is a
14843 human-readable spelling of the identifier, e.g., `operator +'. */
14845 static cp_expr
14846 cp_parser_operator_function_id (cp_parser* parser)
14848 /* Look for the `operator' keyword. */
14849 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14850 return error_mark_node;
14851 /* And then the name of the operator itself. */
14852 return cp_parser_operator (parser);
14855 /* Return an identifier node for a user-defined literal operator.
14856 The suffix identifier is chained to the operator name identifier. */
14858 tree
14859 cp_literal_operator_id (const char* name)
14861 tree identifier;
14862 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14863 + strlen (name) + 10);
14864 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14865 identifier = get_identifier (buffer);
14867 return identifier;
14870 /* Parse an operator.
14872 operator:
14873 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14874 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14875 || ++ -- , ->* -> () []
14877 GNU Extensions:
14879 operator:
14880 <? >? <?= >?=
14882 Returns an IDENTIFIER_NODE for the operator which is a
14883 human-readable spelling of the identifier, e.g., `operator +'. */
14885 static cp_expr
14886 cp_parser_operator (cp_parser* parser)
14888 tree id = NULL_TREE;
14889 cp_token *token;
14890 bool utf8 = false;
14892 /* Peek at the next token. */
14893 token = cp_lexer_peek_token (parser->lexer);
14895 location_t start_loc = token->location;
14897 /* Figure out which operator we have. */
14898 enum tree_code op = ERROR_MARK;
14899 bool assop = false;
14900 bool consumed = false;
14901 switch (token->type)
14903 case CPP_KEYWORD:
14905 /* The keyword should be either `new' or `delete'. */
14906 if (token->keyword == RID_NEW)
14907 op = NEW_EXPR;
14908 else if (token->keyword == RID_DELETE)
14909 op = DELETE_EXPR;
14910 else
14911 break;
14913 /* Consume the `new' or `delete' token. */
14914 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14916 /* Peek at the next token. */
14917 token = cp_lexer_peek_token (parser->lexer);
14918 /* If it's a `[' token then this is the array variant of the
14919 operator. */
14920 if (token->type == CPP_OPEN_SQUARE)
14922 /* Consume the `[' token. */
14923 cp_lexer_consume_token (parser->lexer);
14924 /* Look for the `]' token. */
14925 if (cp_token *close_token
14926 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14927 end_loc = close_token->location;
14928 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14930 start_loc = make_location (start_loc, start_loc, end_loc);
14931 consumed = true;
14932 break;
14935 case CPP_PLUS:
14936 op = PLUS_EXPR;
14937 break;
14939 case CPP_MINUS:
14940 op = MINUS_EXPR;
14941 break;
14943 case CPP_MULT:
14944 op = MULT_EXPR;
14945 break;
14947 case CPP_DIV:
14948 op = TRUNC_DIV_EXPR;
14949 break;
14951 case CPP_MOD:
14952 op = TRUNC_MOD_EXPR;
14953 break;
14955 case CPP_XOR:
14956 op = BIT_XOR_EXPR;
14957 break;
14959 case CPP_AND:
14960 op = BIT_AND_EXPR;
14961 break;
14963 case CPP_OR:
14964 op = BIT_IOR_EXPR;
14965 break;
14967 case CPP_COMPL:
14968 op = BIT_NOT_EXPR;
14969 break;
14971 case CPP_NOT:
14972 op = TRUTH_NOT_EXPR;
14973 break;
14975 case CPP_EQ:
14976 assop = true;
14977 op = NOP_EXPR;
14978 break;
14980 case CPP_LESS:
14981 op = LT_EXPR;
14982 break;
14984 case CPP_GREATER:
14985 op = GT_EXPR;
14986 break;
14988 case CPP_PLUS_EQ:
14989 assop = true;
14990 op = PLUS_EXPR;
14991 break;
14993 case CPP_MINUS_EQ:
14994 assop = true;
14995 op = MINUS_EXPR;
14996 break;
14998 case CPP_MULT_EQ:
14999 assop = true;
15000 op = MULT_EXPR;
15001 break;
15003 case CPP_DIV_EQ:
15004 assop = true;
15005 op = TRUNC_DIV_EXPR;
15006 break;
15008 case CPP_MOD_EQ:
15009 assop = true;
15010 op = TRUNC_MOD_EXPR;
15011 break;
15013 case CPP_XOR_EQ:
15014 assop = true;
15015 op = BIT_XOR_EXPR;
15016 break;
15018 case CPP_AND_EQ:
15019 assop = true;
15020 op = BIT_AND_EXPR;
15021 break;
15023 case CPP_OR_EQ:
15024 assop = true;
15025 op = BIT_IOR_EXPR;
15026 break;
15028 case CPP_LSHIFT:
15029 op = LSHIFT_EXPR;
15030 break;
15032 case CPP_RSHIFT:
15033 op = RSHIFT_EXPR;
15034 break;
15036 case CPP_LSHIFT_EQ:
15037 assop = true;
15038 op = LSHIFT_EXPR;
15039 break;
15041 case CPP_RSHIFT_EQ:
15042 assop = true;
15043 op = RSHIFT_EXPR;
15044 break;
15046 case CPP_EQ_EQ:
15047 op = EQ_EXPR;
15048 break;
15050 case CPP_NOT_EQ:
15051 op = NE_EXPR;
15052 break;
15054 case CPP_LESS_EQ:
15055 op = LE_EXPR;
15056 break;
15058 case CPP_GREATER_EQ:
15059 op = GE_EXPR;
15060 break;
15062 case CPP_AND_AND:
15063 op = TRUTH_ANDIF_EXPR;
15064 break;
15066 case CPP_OR_OR:
15067 op = TRUTH_ORIF_EXPR;
15068 break;
15070 case CPP_PLUS_PLUS:
15071 op = POSTINCREMENT_EXPR;
15072 break;
15074 case CPP_MINUS_MINUS:
15075 op = PREDECREMENT_EXPR;
15076 break;
15078 case CPP_COMMA:
15079 op = COMPOUND_EXPR;
15080 break;
15082 case CPP_DEREF_STAR:
15083 op = MEMBER_REF;
15084 break;
15086 case CPP_DEREF:
15087 op = COMPONENT_REF;
15088 break;
15090 case CPP_OPEN_PAREN:
15092 /* Consume the `('. */
15093 matching_parens parens;
15094 parens.consume_open (parser);
15095 /* Look for the matching `)'. */
15096 parens.require_close (parser);
15097 op = CALL_EXPR;
15098 consumed = true;
15099 break;
15102 case CPP_OPEN_SQUARE:
15103 /* Consume the `['. */
15104 cp_lexer_consume_token (parser->lexer);
15105 /* Look for the matching `]'. */
15106 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15107 op = ARRAY_REF;
15108 consumed = true;
15109 break;
15111 case CPP_UTF8STRING:
15112 case CPP_UTF8STRING_USERDEF:
15113 utf8 = true;
15114 /* FALLTHRU */
15115 case CPP_STRING:
15116 case CPP_WSTRING:
15117 case CPP_STRING16:
15118 case CPP_STRING32:
15119 case CPP_STRING_USERDEF:
15120 case CPP_WSTRING_USERDEF:
15121 case CPP_STRING16_USERDEF:
15122 case CPP_STRING32_USERDEF:
15124 tree str, string_tree;
15125 int sz, len;
15127 if (cxx_dialect == cxx98)
15128 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15130 /* Consume the string. */
15131 str = cp_parser_string_literal (parser, /*translate=*/true,
15132 /*wide_ok=*/true, /*lookup_udlit=*/false);
15133 if (str == error_mark_node)
15134 return error_mark_node;
15135 else if (TREE_CODE (str) == USERDEF_LITERAL)
15137 string_tree = USERDEF_LITERAL_VALUE (str);
15138 id = USERDEF_LITERAL_SUFFIX_ID (str);
15140 else
15142 string_tree = str;
15143 /* Look for the suffix identifier. */
15144 token = cp_lexer_peek_token (parser->lexer);
15145 if (token->type == CPP_NAME)
15146 id = cp_parser_identifier (parser);
15147 else if (token->type == CPP_KEYWORD)
15149 error ("unexpected keyword;"
15150 " remove space between quotes and suffix identifier");
15151 return error_mark_node;
15153 else
15155 error ("expected suffix identifier");
15156 return error_mark_node;
15159 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15160 (TREE_TYPE (TREE_TYPE (string_tree))));
15161 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15162 if (len != 0)
15164 error ("expected empty string after %<operator%> keyword");
15165 return error_mark_node;
15167 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15168 != char_type_node)
15170 error ("invalid encoding prefix in literal operator");
15171 return error_mark_node;
15173 if (id != error_mark_node)
15175 const char *name = IDENTIFIER_POINTER (id);
15176 id = cp_literal_operator_id (name);
15178 return id;
15181 default:
15182 /* Anything else is an error. */
15183 break;
15186 /* If we have selected an identifier, we need to consume the
15187 operator token. */
15188 if (op != ERROR_MARK)
15190 id = ovl_op_identifier (assop, op);
15191 if (!consumed)
15192 cp_lexer_consume_token (parser->lexer);
15194 /* Otherwise, no valid operator name was present. */
15195 else
15197 cp_parser_error (parser, "expected operator");
15198 id = error_mark_node;
15201 return cp_expr (id, start_loc);
15204 /* Parse a template-declaration.
15206 template-declaration:
15207 export [opt] template < template-parameter-list > declaration
15209 If MEMBER_P is TRUE, this template-declaration occurs within a
15210 class-specifier.
15212 The grammar rule given by the standard isn't correct. What
15213 is really meant is:
15215 template-declaration:
15216 export [opt] template-parameter-list-seq
15217 decl-specifier-seq [opt] init-declarator [opt] ;
15218 export [opt] template-parameter-list-seq
15219 function-definition
15221 template-parameter-list-seq:
15222 template-parameter-list-seq [opt]
15223 template < template-parameter-list >
15225 Concept Extensions:
15227 template-parameter-list-seq:
15228 template < template-parameter-list > requires-clause [opt]
15230 requires-clause:
15231 requires logical-or-expression */
15233 static void
15234 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15236 /* Check for `export'. */
15237 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15239 /* Consume the `export' token. */
15240 cp_lexer_consume_token (parser->lexer);
15241 /* Warn that we do not support `export'. */
15242 warning (0, "keyword %<export%> not implemented, and will be ignored");
15245 cp_parser_template_declaration_after_export (parser, member_p);
15248 /* Parse a template-parameter-list.
15250 template-parameter-list:
15251 template-parameter
15252 template-parameter-list , template-parameter
15254 Returns a TREE_LIST. Each node represents a template parameter.
15255 The nodes are connected via their TREE_CHAINs. */
15257 static tree
15258 cp_parser_template_parameter_list (cp_parser* parser)
15260 tree parameter_list = NULL_TREE;
15262 begin_template_parm_list ();
15264 /* The loop below parses the template parms. We first need to know
15265 the total number of template parms to be able to compute proper
15266 canonical types of each dependent type. So after the loop, when
15267 we know the total number of template parms,
15268 end_template_parm_list computes the proper canonical types and
15269 fixes up the dependent types accordingly. */
15270 while (true)
15272 tree parameter;
15273 bool is_non_type;
15274 bool is_parameter_pack;
15275 location_t parm_loc;
15277 /* Parse the template-parameter. */
15278 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15279 parameter = cp_parser_template_parameter (parser,
15280 &is_non_type,
15281 &is_parameter_pack);
15282 /* Add it to the list. */
15283 if (parameter != error_mark_node)
15284 parameter_list = process_template_parm (parameter_list,
15285 parm_loc,
15286 parameter,
15287 is_non_type,
15288 is_parameter_pack);
15289 else
15291 tree err_parm = build_tree_list (parameter, parameter);
15292 parameter_list = chainon (parameter_list, err_parm);
15295 /* If the next token is not a `,', we're done. */
15296 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15297 break;
15298 /* Otherwise, consume the `,' token. */
15299 cp_lexer_consume_token (parser->lexer);
15302 return end_template_parm_list (parameter_list);
15305 /* Parse a introduction-list.
15307 introduction-list:
15308 introduced-parameter
15309 introduction-list , introduced-parameter
15311 introduced-parameter:
15312 ...[opt] identifier
15314 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15315 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15316 WILDCARD_DECL will also have DECL_NAME set and token location in
15317 DECL_SOURCE_LOCATION. */
15319 static tree
15320 cp_parser_introduction_list (cp_parser *parser)
15322 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15324 while (true)
15326 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15327 if (is_pack)
15328 cp_lexer_consume_token (parser->lexer);
15330 tree identifier = cp_parser_identifier (parser);
15331 if (identifier == error_mark_node)
15332 break;
15334 /* Build placeholder. */
15335 tree parm = build_nt (WILDCARD_DECL);
15336 DECL_SOURCE_LOCATION (parm)
15337 = cp_lexer_peek_token (parser->lexer)->location;
15338 DECL_NAME (parm) = identifier;
15339 WILDCARD_PACK_P (parm) = is_pack;
15340 vec_safe_push (introduction_vec, parm);
15342 /* If the next token is not a `,', we're done. */
15343 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15344 break;
15345 /* Otherwise, consume the `,' token. */
15346 cp_lexer_consume_token (parser->lexer);
15349 /* Convert the vec into a TREE_VEC. */
15350 tree introduction_list = make_tree_vec (introduction_vec->length ());
15351 unsigned int n;
15352 tree parm;
15353 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15354 TREE_VEC_ELT (introduction_list, n) = parm;
15356 release_tree_vector (introduction_vec);
15357 return introduction_list;
15360 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15361 is an abstract declarator. */
15363 static inline cp_declarator*
15364 get_id_declarator (cp_declarator *declarator)
15366 cp_declarator *d = declarator;
15367 while (d && d->kind != cdk_id)
15368 d = d->declarator;
15369 return d;
15372 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15373 is an abstract declarator. */
15375 static inline tree
15376 get_unqualified_id (cp_declarator *declarator)
15378 declarator = get_id_declarator (declarator);
15379 if (declarator)
15380 return declarator->u.id.unqualified_name;
15381 else
15382 return NULL_TREE;
15385 /* Returns true if DECL represents a constrained-parameter. */
15387 static inline bool
15388 is_constrained_parameter (tree decl)
15390 return (decl
15391 && TREE_CODE (decl) == TYPE_DECL
15392 && CONSTRAINED_PARM_CONCEPT (decl)
15393 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15396 /* Returns true if PARM declares a constrained-parameter. */
15398 static inline bool
15399 is_constrained_parameter (cp_parameter_declarator *parm)
15401 return is_constrained_parameter (parm->decl_specifiers.type);
15404 /* Check that the type parameter is only a declarator-id, and that its
15405 type is not cv-qualified. */
15407 bool
15408 cp_parser_check_constrained_type_parm (cp_parser *parser,
15409 cp_parameter_declarator *parm)
15411 if (!parm->declarator)
15412 return true;
15414 if (parm->declarator->kind != cdk_id)
15416 cp_parser_error (parser, "invalid constrained type parameter");
15417 return false;
15420 /* Don't allow cv-qualified type parameters. */
15421 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15422 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15424 cp_parser_error (parser, "cv-qualified type parameter");
15425 return false;
15428 return true;
15431 /* Finish parsing/processing a template type parameter and checking
15432 various restrictions. */
15434 static inline tree
15435 cp_parser_constrained_type_template_parm (cp_parser *parser,
15436 tree id,
15437 cp_parameter_declarator* parmdecl)
15439 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15440 return finish_template_type_parm (class_type_node, id);
15441 else
15442 return error_mark_node;
15445 static tree
15446 finish_constrained_template_template_parm (tree proto, tree id)
15448 /* FIXME: This should probably be copied, and we may need to adjust
15449 the template parameter depths. */
15450 tree saved_parms = current_template_parms;
15451 begin_template_parm_list ();
15452 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15453 end_template_parm_list ();
15455 tree parm = finish_template_template_parm (class_type_node, id);
15456 current_template_parms = saved_parms;
15458 return parm;
15461 /* Finish parsing/processing a template template parameter by borrowing
15462 the template parameter list from the prototype parameter. */
15464 static tree
15465 cp_parser_constrained_template_template_parm (cp_parser *parser,
15466 tree proto,
15467 tree id,
15468 cp_parameter_declarator *parmdecl)
15470 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15471 return error_mark_node;
15472 return finish_constrained_template_template_parm (proto, id);
15475 /* Create a new non-type template parameter from the given PARM
15476 declarator. */
15478 static tree
15479 constrained_non_type_template_parm (bool *is_non_type,
15480 cp_parameter_declarator *parm)
15482 *is_non_type = true;
15483 cp_declarator *decl = parm->declarator;
15484 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15485 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15486 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15489 /* Build a constrained template parameter based on the PARMDECL
15490 declarator. The type of PARMDECL is the constrained type, which
15491 refers to the prototype template parameter that ultimately
15492 specifies the type of the declared parameter. */
15494 static tree
15495 finish_constrained_parameter (cp_parser *parser,
15496 cp_parameter_declarator *parmdecl,
15497 bool *is_non_type,
15498 bool *is_parameter_pack)
15500 tree decl = parmdecl->decl_specifiers.type;
15501 tree id = get_unqualified_id (parmdecl->declarator);
15502 tree def = parmdecl->default_argument;
15503 tree proto = DECL_INITIAL (decl);
15505 /* A template parameter constrained by a variadic concept shall also
15506 be declared as a template parameter pack. */
15507 bool is_variadic = template_parameter_pack_p (proto);
15508 if (is_variadic && !*is_parameter_pack)
15509 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15511 /* Build the parameter. Return an error if the declarator was invalid. */
15512 tree parm;
15513 if (TREE_CODE (proto) == TYPE_DECL)
15514 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15515 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15516 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15517 parmdecl);
15518 else
15519 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15520 if (parm == error_mark_node)
15521 return error_mark_node;
15523 /* Finish the parameter decl and create a node attaching the
15524 default argument and constraint. */
15525 parm = build_tree_list (def, parm);
15526 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15528 return parm;
15531 /* Returns true if the parsed type actually represents the declaration
15532 of a type template-parameter. */
15534 static inline bool
15535 declares_constrained_type_template_parameter (tree type)
15537 return (is_constrained_parameter (type)
15538 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15542 /* Returns true if the parsed type actually represents the declaration of
15543 a template template-parameter. */
15545 static bool
15546 declares_constrained_template_template_parameter (tree type)
15548 return (is_constrained_parameter (type)
15549 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15552 /* Parse a default argument for a type template-parameter.
15553 Note that diagnostics are handled in cp_parser_template_parameter. */
15555 static tree
15556 cp_parser_default_type_template_argument (cp_parser *parser)
15558 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15560 /* Consume the `=' token. */
15561 cp_lexer_consume_token (parser->lexer);
15563 cp_token *token = cp_lexer_peek_token (parser->lexer);
15565 /* Parse the default-argument. */
15566 push_deferring_access_checks (dk_no_deferred);
15567 tree default_argument = cp_parser_type_id (parser);
15568 pop_deferring_access_checks ();
15570 if (flag_concepts && type_uses_auto (default_argument))
15572 error_at (token->location,
15573 "invalid use of %<auto%> in default template argument");
15574 return error_mark_node;
15577 return default_argument;
15580 /* Parse a default argument for a template template-parameter. */
15582 static tree
15583 cp_parser_default_template_template_argument (cp_parser *parser)
15585 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15587 bool is_template;
15589 /* Consume the `='. */
15590 cp_lexer_consume_token (parser->lexer);
15591 /* Parse the id-expression. */
15592 push_deferring_access_checks (dk_no_deferred);
15593 /* save token before parsing the id-expression, for error
15594 reporting */
15595 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15596 tree default_argument
15597 = cp_parser_id_expression (parser,
15598 /*template_keyword_p=*/false,
15599 /*check_dependency_p=*/true,
15600 /*template_p=*/&is_template,
15601 /*declarator_p=*/false,
15602 /*optional_p=*/false);
15603 if (TREE_CODE (default_argument) == TYPE_DECL)
15604 /* If the id-expression was a template-id that refers to
15605 a template-class, we already have the declaration here,
15606 so no further lookup is needed. */
15608 else
15609 /* Look up the name. */
15610 default_argument
15611 = cp_parser_lookup_name (parser, default_argument,
15612 none_type,
15613 /*is_template=*/is_template,
15614 /*is_namespace=*/false,
15615 /*check_dependency=*/true,
15616 /*ambiguous_decls=*/NULL,
15617 token->location);
15618 /* See if the default argument is valid. */
15619 default_argument = check_template_template_default_arg (default_argument);
15620 pop_deferring_access_checks ();
15621 return default_argument;
15624 /* Parse a template-parameter.
15626 template-parameter:
15627 type-parameter
15628 parameter-declaration
15630 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15631 the parameter. The TREE_PURPOSE is the default value, if any.
15632 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15633 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15634 set to true iff this parameter is a parameter pack. */
15636 static tree
15637 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15638 bool *is_parameter_pack)
15640 cp_token *token;
15641 cp_parameter_declarator *parameter_declarator;
15642 tree parm;
15644 /* Assume it is a type parameter or a template parameter. */
15645 *is_non_type = false;
15646 /* Assume it not a parameter pack. */
15647 *is_parameter_pack = false;
15648 /* Peek at the next token. */
15649 token = cp_lexer_peek_token (parser->lexer);
15650 /* If it is `template', we have a type-parameter. */
15651 if (token->keyword == RID_TEMPLATE)
15652 return cp_parser_type_parameter (parser, is_parameter_pack);
15653 /* If it is `class' or `typename' we do not know yet whether it is a
15654 type parameter or a non-type parameter. Consider:
15656 template <typename T, typename T::X X> ...
15660 template <class C, class D*> ...
15662 Here, the first parameter is a type parameter, and the second is
15663 a non-type parameter. We can tell by looking at the token after
15664 the identifier -- if it is a `,', `=', or `>' then we have a type
15665 parameter. */
15666 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15668 /* Peek at the token after `class' or `typename'. */
15669 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15670 /* If it's an ellipsis, we have a template type parameter
15671 pack. */
15672 if (token->type == CPP_ELLIPSIS)
15673 return cp_parser_type_parameter (parser, is_parameter_pack);
15674 /* If it's an identifier, skip it. */
15675 if (token->type == CPP_NAME)
15676 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15677 /* Now, see if the token looks like the end of a template
15678 parameter. */
15679 if (token->type == CPP_COMMA
15680 || token->type == CPP_EQ
15681 || token->type == CPP_GREATER)
15682 return cp_parser_type_parameter (parser, is_parameter_pack);
15685 /* Otherwise, it is a non-type parameter or a constrained parameter.
15687 [temp.param]
15689 When parsing a default template-argument for a non-type
15690 template-parameter, the first non-nested `>' is taken as the end
15691 of the template parameter-list rather than a greater-than
15692 operator. */
15693 parameter_declarator
15694 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15695 /*parenthesized_p=*/NULL);
15697 if (!parameter_declarator)
15698 return error_mark_node;
15700 /* If the parameter declaration is marked as a parameter pack, set
15701 *IS_PARAMETER_PACK to notify the caller. */
15702 if (parameter_declarator->template_parameter_pack_p)
15703 *is_parameter_pack = true;
15705 if (parameter_declarator->default_argument)
15707 /* Can happen in some cases of erroneous input (c++/34892). */
15708 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15709 /* Consume the `...' for better error recovery. */
15710 cp_lexer_consume_token (parser->lexer);
15713 // The parameter may have been constrained.
15714 if (is_constrained_parameter (parameter_declarator))
15715 return finish_constrained_parameter (parser,
15716 parameter_declarator,
15717 is_non_type,
15718 is_parameter_pack);
15720 // Now we're sure that the parameter is a non-type parameter.
15721 *is_non_type = true;
15723 parm = grokdeclarator (parameter_declarator->declarator,
15724 &parameter_declarator->decl_specifiers,
15725 TPARM, /*initialized=*/0,
15726 /*attrlist=*/NULL);
15727 if (parm == error_mark_node)
15728 return error_mark_node;
15730 return build_tree_list (parameter_declarator->default_argument, parm);
15733 /* Parse a type-parameter.
15735 type-parameter:
15736 class identifier [opt]
15737 class identifier [opt] = type-id
15738 typename identifier [opt]
15739 typename identifier [opt] = type-id
15740 template < template-parameter-list > class identifier [opt]
15741 template < template-parameter-list > class identifier [opt]
15742 = id-expression
15744 GNU Extension (variadic templates):
15746 type-parameter:
15747 class ... identifier [opt]
15748 typename ... identifier [opt]
15750 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15751 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15752 the declaration of the parameter.
15754 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15756 static tree
15757 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15759 cp_token *token;
15760 tree parameter;
15762 /* Look for a keyword to tell us what kind of parameter this is. */
15763 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15764 if (!token)
15765 return error_mark_node;
15767 switch (token->keyword)
15769 case RID_CLASS:
15770 case RID_TYPENAME:
15772 tree identifier;
15773 tree default_argument;
15775 /* If the next token is an ellipsis, we have a template
15776 argument pack. */
15777 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15779 /* Consume the `...' token. */
15780 cp_lexer_consume_token (parser->lexer);
15781 maybe_warn_variadic_templates ();
15783 *is_parameter_pack = true;
15786 /* If the next token is an identifier, then it names the
15787 parameter. */
15788 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15789 identifier = cp_parser_identifier (parser);
15790 else
15791 identifier = NULL_TREE;
15793 /* Create the parameter. */
15794 parameter = finish_template_type_parm (class_type_node, identifier);
15796 /* If the next token is an `=', we have a default argument. */
15797 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15799 default_argument
15800 = cp_parser_default_type_template_argument (parser);
15802 /* Template parameter packs cannot have default
15803 arguments. */
15804 if (*is_parameter_pack)
15806 if (identifier)
15807 error_at (token->location,
15808 "template parameter pack %qD cannot have a "
15809 "default argument", identifier);
15810 else
15811 error_at (token->location,
15812 "template parameter packs cannot have "
15813 "default arguments");
15814 default_argument = NULL_TREE;
15816 else if (check_for_bare_parameter_packs (default_argument))
15817 default_argument = error_mark_node;
15819 else
15820 default_argument = NULL_TREE;
15822 /* Create the combined representation of the parameter and the
15823 default argument. */
15824 parameter = build_tree_list (default_argument, parameter);
15826 break;
15828 case RID_TEMPLATE:
15830 tree identifier;
15831 tree default_argument;
15833 /* Look for the `<'. */
15834 cp_parser_require (parser, CPP_LESS, RT_LESS);
15835 /* Parse the template-parameter-list. */
15836 cp_parser_template_parameter_list (parser);
15837 /* Look for the `>'. */
15838 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15840 // If template requirements are present, parse them.
15841 if (flag_concepts)
15843 tree reqs = get_shorthand_constraints (current_template_parms);
15844 if (tree r = cp_parser_requires_clause_opt (parser))
15845 reqs = conjoin_constraints (reqs, normalize_expression (r));
15846 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15849 /* Look for the `class' or 'typename' keywords. */
15850 cp_parser_type_parameter_key (parser);
15851 /* If the next token is an ellipsis, we have a template
15852 argument pack. */
15853 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15855 /* Consume the `...' token. */
15856 cp_lexer_consume_token (parser->lexer);
15857 maybe_warn_variadic_templates ();
15859 *is_parameter_pack = true;
15861 /* If the next token is an `=', then there is a
15862 default-argument. If the next token is a `>', we are at
15863 the end of the parameter-list. If the next token is a `,',
15864 then we are at the end of this parameter. */
15865 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15866 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15867 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15869 identifier = cp_parser_identifier (parser);
15870 /* Treat invalid names as if the parameter were nameless. */
15871 if (identifier == error_mark_node)
15872 identifier = NULL_TREE;
15874 else
15875 identifier = NULL_TREE;
15877 /* Create the template parameter. */
15878 parameter = finish_template_template_parm (class_type_node,
15879 identifier);
15881 /* If the next token is an `=', then there is a
15882 default-argument. */
15883 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15885 default_argument
15886 = cp_parser_default_template_template_argument (parser);
15888 /* Template parameter packs cannot have default
15889 arguments. */
15890 if (*is_parameter_pack)
15892 if (identifier)
15893 error_at (token->location,
15894 "template parameter pack %qD cannot "
15895 "have a default argument",
15896 identifier);
15897 else
15898 error_at (token->location, "template parameter packs cannot "
15899 "have default arguments");
15900 default_argument = NULL_TREE;
15903 else
15904 default_argument = NULL_TREE;
15906 /* Create the combined representation of the parameter and the
15907 default argument. */
15908 parameter = build_tree_list (default_argument, parameter);
15910 break;
15912 default:
15913 gcc_unreachable ();
15914 break;
15917 return parameter;
15920 /* Parse a template-id.
15922 template-id:
15923 template-name < template-argument-list [opt] >
15925 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15926 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15927 returned. Otherwise, if the template-name names a function, or set
15928 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15929 names a class, returns a TYPE_DECL for the specialization.
15931 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15932 uninstantiated templates. */
15934 static tree
15935 cp_parser_template_id (cp_parser *parser,
15936 bool template_keyword_p,
15937 bool check_dependency_p,
15938 enum tag_types tag_type,
15939 bool is_declaration)
15941 tree templ;
15942 tree arguments;
15943 tree template_id;
15944 cp_token_position start_of_id = 0;
15945 cp_token *next_token = NULL, *next_token_2 = NULL;
15946 bool is_identifier;
15948 /* If the next token corresponds to a template-id, there is no need
15949 to reparse it. */
15950 cp_token *token = cp_lexer_peek_token (parser->lexer);
15951 if (token->type == CPP_TEMPLATE_ID)
15953 cp_lexer_consume_token (parser->lexer);
15954 return saved_checks_value (token->u.tree_check_value);
15957 /* Avoid performing name lookup if there is no possibility of
15958 finding a template-id. */
15959 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15960 || (token->type == CPP_NAME
15961 && !cp_parser_nth_token_starts_template_argument_list_p
15962 (parser, 2)))
15964 cp_parser_error (parser, "expected template-id");
15965 return error_mark_node;
15968 /* Remember where the template-id starts. */
15969 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15970 start_of_id = cp_lexer_token_position (parser->lexer, false);
15972 push_deferring_access_checks (dk_deferred);
15974 /* Parse the template-name. */
15975 is_identifier = false;
15976 templ = cp_parser_template_name (parser, template_keyword_p,
15977 check_dependency_p,
15978 is_declaration,
15979 tag_type,
15980 &is_identifier);
15981 if (templ == error_mark_node || is_identifier)
15983 pop_deferring_access_checks ();
15984 return templ;
15987 /* Since we're going to preserve any side-effects from this parse, set up a
15988 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15989 in the template arguments. */
15990 tentative_firewall firewall (parser);
15992 /* If we find the sequence `[:' after a template-name, it's probably
15993 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15994 parse correctly the argument list. */
15995 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15996 == CPP_OPEN_SQUARE)
15997 && next_token->flags & DIGRAPH
15998 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15999 == CPP_COLON)
16000 && !(next_token_2->flags & PREV_WHITE))
16002 cp_parser_parse_tentatively (parser);
16003 /* Change `:' into `::'. */
16004 next_token_2->type = CPP_SCOPE;
16005 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16006 CPP_LESS. */
16007 cp_lexer_consume_token (parser->lexer);
16009 /* Parse the arguments. */
16010 arguments = cp_parser_enclosed_template_argument_list (parser);
16011 if (!cp_parser_parse_definitely (parser))
16013 /* If we couldn't parse an argument list, then we revert our changes
16014 and return simply an error. Maybe this is not a template-id
16015 after all. */
16016 next_token_2->type = CPP_COLON;
16017 cp_parser_error (parser, "expected %<<%>");
16018 pop_deferring_access_checks ();
16019 return error_mark_node;
16021 /* Otherwise, emit an error about the invalid digraph, but continue
16022 parsing because we got our argument list. */
16023 if (permerror (next_token->location,
16024 "%<<::%> cannot begin a template-argument list"))
16026 static bool hint = false;
16027 inform (next_token->location,
16028 "%<<:%> is an alternate spelling for %<[%>."
16029 " Insert whitespace between %<<%> and %<::%>");
16030 if (!hint && !flag_permissive)
16032 inform (next_token->location, "(if you use %<-fpermissive%> "
16033 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16034 "accept your code)");
16035 hint = true;
16039 else
16041 /* Look for the `<' that starts the template-argument-list. */
16042 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16044 pop_deferring_access_checks ();
16045 return error_mark_node;
16047 /* Parse the arguments. */
16048 arguments = cp_parser_enclosed_template_argument_list (parser);
16050 if ((cxx_dialect > cxx17)
16051 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16052 && !template_keyword_p
16053 && (cp_parser_error_occurred (parser)
16054 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16056 /* This didn't go well. */
16057 if (TREE_CODE (templ) == FUNCTION_DECL)
16059 /* C++2A says that "function-name < a;" is now ill-formed. */
16060 if (cp_parser_error_occurred (parser))
16062 error_at (token->location, "invalid template-argument-list");
16063 inform (token->location, "function name as the left hand "
16064 "operand of %<<%> is ill-formed in C++2a; wrap the "
16065 "function name in %<()%>");
16067 else
16068 /* We expect "f<targs>" to be followed by "(args)". */
16069 error_at (cp_lexer_peek_token (parser->lexer)->location,
16070 "expected %<(%> after template-argument-list");
16071 if (start_of_id)
16072 /* Purge all subsequent tokens. */
16073 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16075 else
16076 cp_parser_simulate_error (parser);
16077 pop_deferring_access_checks ();
16078 return error_mark_node;
16082 /* Set the location to be of the form:
16083 template-name < template-argument-list [opt] >
16084 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16085 with caret == start at the start of the template-name,
16086 ranging until the closing '>'. */
16087 location_t finish_loc
16088 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16089 location_t combined_loc
16090 = make_location (token->location, token->location, finish_loc);
16092 /* Check for concepts autos where they don't belong. We could
16093 identify types in some cases of idnetifier TEMPL, looking ahead
16094 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16095 types. We reject them in functions, but if what we have is an
16096 identifier, even with none_type we can't conclude it's NOT a
16097 type, we have to wait for template substitution. */
16098 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16099 template_id = error_mark_node;
16100 /* Build a representation of the specialization. */
16101 else if (identifier_p (templ))
16102 template_id = build_min_nt_loc (combined_loc,
16103 TEMPLATE_ID_EXPR,
16104 templ, arguments);
16105 else if (DECL_TYPE_TEMPLATE_P (templ)
16106 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16108 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16109 template (rather than some instantiation thereof) only if
16110 is not nested within some other construct. For example, in
16111 "template <typename T> void f(T) { A<T>::", A<T> is just an
16112 instantiation of A. */
16113 bool entering_scope
16114 = (template_parm_scope_p ()
16115 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16116 template_id
16117 = finish_template_type (templ, arguments, entering_scope);
16119 /* A template-like identifier may be a partial concept id. */
16120 else if (flag_concepts
16121 && (template_id = (cp_parser_maybe_partial_concept_id
16122 (parser, templ, arguments))))
16123 return template_id;
16124 else if (variable_template_p (templ))
16126 template_id = lookup_template_variable (templ, arguments);
16127 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16128 SET_EXPR_LOCATION (template_id, combined_loc);
16130 else
16132 /* If it's not a class-template or a template-template, it should be
16133 a function-template. */
16134 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16135 || TREE_CODE (templ) == OVERLOAD
16136 || TREE_CODE (templ) == FUNCTION_DECL
16137 || BASELINK_P (templ)));
16139 template_id = lookup_template_function (templ, arguments);
16140 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16141 SET_EXPR_LOCATION (template_id, combined_loc);
16144 /* If parsing tentatively, replace the sequence of tokens that makes
16145 up the template-id with a CPP_TEMPLATE_ID token. That way,
16146 should we re-parse the token stream, we will not have to repeat
16147 the effort required to do the parse, nor will we issue duplicate
16148 error messages about problems during instantiation of the
16149 template. */
16150 if (start_of_id
16151 /* Don't do this if we had a parse error in a declarator; re-parsing
16152 might succeed if a name changes meaning (60361). */
16153 && !(cp_parser_error_occurred (parser)
16154 && cp_parser_parsing_tentatively (parser)
16155 && parser->in_declarator_p))
16157 /* Reset the contents of the START_OF_ID token. */
16158 token->type = CPP_TEMPLATE_ID;
16159 token->location = combined_loc;
16161 /* Retrieve any deferred checks. Do not pop this access checks yet
16162 so the memory will not be reclaimed during token replacing below. */
16163 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16164 token->u.tree_check_value->value = template_id;
16165 token->u.tree_check_value->checks = get_deferred_access_checks ();
16166 token->keyword = RID_MAX;
16168 /* Purge all subsequent tokens. */
16169 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16171 /* ??? Can we actually assume that, if template_id ==
16172 error_mark_node, we will have issued a diagnostic to the
16173 user, as opposed to simply marking the tentative parse as
16174 failed? */
16175 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16176 error_at (token->location, "parse error in template argument list");
16179 pop_to_parent_deferring_access_checks ();
16180 return template_id;
16183 /* Parse a template-name.
16185 template-name:
16186 identifier
16188 The standard should actually say:
16190 template-name:
16191 identifier
16192 operator-function-id
16194 A defect report has been filed about this issue.
16196 A conversion-function-id cannot be a template name because they cannot
16197 be part of a template-id. In fact, looking at this code:
16199 a.operator K<int>()
16201 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16202 It is impossible to call a templated conversion-function-id with an
16203 explicit argument list, since the only allowed template parameter is
16204 the type to which it is converting.
16206 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16207 `template' keyword, in a construction like:
16209 T::template f<3>()
16211 In that case `f' is taken to be a template-name, even though there
16212 is no way of knowing for sure.
16214 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16215 name refers to a set of overloaded functions, at least one of which
16216 is a template, or an IDENTIFIER_NODE with the name of the template,
16217 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16218 names are looked up inside uninstantiated templates. */
16220 static tree
16221 cp_parser_template_name (cp_parser* parser,
16222 bool template_keyword_p,
16223 bool check_dependency_p,
16224 bool is_declaration,
16225 enum tag_types tag_type,
16226 bool *is_identifier)
16228 tree identifier;
16229 tree decl;
16230 cp_token *token = cp_lexer_peek_token (parser->lexer);
16232 /* If the next token is `operator', then we have either an
16233 operator-function-id or a conversion-function-id. */
16234 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16236 /* We don't know whether we're looking at an
16237 operator-function-id or a conversion-function-id. */
16238 cp_parser_parse_tentatively (parser);
16239 /* Try an operator-function-id. */
16240 identifier = cp_parser_operator_function_id (parser);
16241 /* If that didn't work, try a conversion-function-id. */
16242 if (!cp_parser_parse_definitely (parser))
16244 cp_parser_error (parser, "expected template-name");
16245 return error_mark_node;
16248 /* Look for the identifier. */
16249 else
16250 identifier = cp_parser_identifier (parser);
16252 /* If we didn't find an identifier, we don't have a template-id. */
16253 if (identifier == error_mark_node)
16254 return error_mark_node;
16256 /* If the name immediately followed the `template' keyword, then it
16257 is a template-name. However, if the next token is not `<', then
16258 we do not treat it as a template-name, since it is not being used
16259 as part of a template-id. This enables us to handle constructs
16260 like:
16262 template <typename T> struct S { S(); };
16263 template <typename T> S<T>::S();
16265 correctly. We would treat `S' as a template -- if it were `S<T>'
16266 -- but we do not if there is no `<'. */
16268 if (processing_template_decl
16269 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16271 /* In a declaration, in a dependent context, we pretend that the
16272 "template" keyword was present in order to improve error
16273 recovery. For example, given:
16275 template <typename T> void f(T::X<int>);
16277 we want to treat "X<int>" as a template-id. */
16278 if (is_declaration
16279 && !template_keyword_p
16280 && parser->scope && TYPE_P (parser->scope)
16281 && check_dependency_p
16282 && dependent_scope_p (parser->scope)
16283 /* Do not do this for dtors (or ctors), since they never
16284 need the template keyword before their name. */
16285 && !constructor_name_p (identifier, parser->scope))
16287 cp_token_position start = 0;
16289 /* Explain what went wrong. */
16290 error_at (token->location, "non-template %qD used as template",
16291 identifier);
16292 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16293 parser->scope, identifier);
16294 /* If parsing tentatively, find the location of the "<" token. */
16295 if (cp_parser_simulate_error (parser))
16296 start = cp_lexer_token_position (parser->lexer, true);
16297 /* Parse the template arguments so that we can issue error
16298 messages about them. */
16299 cp_lexer_consume_token (parser->lexer);
16300 cp_parser_enclosed_template_argument_list (parser);
16301 /* Skip tokens until we find a good place from which to
16302 continue parsing. */
16303 cp_parser_skip_to_closing_parenthesis (parser,
16304 /*recovering=*/true,
16305 /*or_comma=*/true,
16306 /*consume_paren=*/false);
16307 /* If parsing tentatively, permanently remove the
16308 template argument list. That will prevent duplicate
16309 error messages from being issued about the missing
16310 "template" keyword. */
16311 if (start)
16312 cp_lexer_purge_tokens_after (parser->lexer, start);
16313 if (is_identifier)
16314 *is_identifier = true;
16315 parser->context->object_type = NULL_TREE;
16316 return identifier;
16319 /* If the "template" keyword is present, then there is generally
16320 no point in doing name-lookup, so we just return IDENTIFIER.
16321 But, if the qualifying scope is non-dependent then we can
16322 (and must) do name-lookup normally. */
16323 if (template_keyword_p)
16325 tree scope = (parser->scope ? parser->scope
16326 : parser->context->object_type);
16327 if (scope && TYPE_P (scope)
16328 && (!CLASS_TYPE_P (scope)
16329 || (check_dependency_p && dependent_type_p (scope))))
16331 /* We're optimizing away the call to cp_parser_lookup_name, but
16332 we still need to do this. */
16333 parser->context->object_type = NULL_TREE;
16334 return identifier;
16339 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16340 const bool scoped_p = ((parser->scope ? parser->scope
16341 : parser->context->object_type) != NULL_TREE);
16343 /* Look up the name. */
16344 decl = cp_parser_lookup_name (parser, identifier,
16345 tag_type,
16346 /*is_template=*/true,
16347 /*is_namespace=*/false,
16348 check_dependency_p,
16349 /*ambiguous_decls=*/NULL,
16350 token->location);
16352 decl = strip_using_decl (decl);
16354 /* If DECL is a template, then the name was a template-name. */
16355 if (TREE_CODE (decl) == TEMPLATE_DECL)
16357 if (TREE_DEPRECATED (decl)
16358 && deprecated_state != DEPRECATED_SUPPRESS)
16359 warn_deprecated_use (decl, NULL_TREE);
16361 else
16363 /* The standard does not explicitly indicate whether a name that
16364 names a set of overloaded declarations, some of which are
16365 templates, is a template-name. However, such a name should
16366 be a template-name; otherwise, there is no way to form a
16367 template-id for the overloaded templates. */
16368 bool found = false;
16370 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16371 !found && iter; ++iter)
16372 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16373 found = true;
16375 if (!found
16376 && (cxx_dialect > cxx17)
16377 && !scoped_p
16378 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16380 /* [temp.names] says "A name is also considered to refer to a template
16381 if it is an unqualified-id followed by a < and name lookup finds
16382 either one or more functions or finds nothing." */
16384 /* The "more functions" case. Just use the OVERLOAD as normally.
16385 We don't use is_overloaded_fn here to avoid considering
16386 BASELINKs. */
16387 if (TREE_CODE (decl) == OVERLOAD
16388 /* Name lookup found one function. */
16389 || TREE_CODE (decl) == FUNCTION_DECL)
16390 found = true;
16391 /* Name lookup found nothing. */
16392 else if (decl == error_mark_node)
16393 return identifier;
16396 if (!found)
16398 /* The name does not name a template. */
16399 cp_parser_error (parser, "expected template-name");
16400 return error_mark_node;
16404 return decl;
16407 /* Parse a template-argument-list.
16409 template-argument-list:
16410 template-argument ... [opt]
16411 template-argument-list , template-argument ... [opt]
16413 Returns a TREE_VEC containing the arguments. */
16415 static tree
16416 cp_parser_template_argument_list (cp_parser* parser)
16418 tree fixed_args[10];
16419 unsigned n_args = 0;
16420 unsigned alloced = 10;
16421 tree *arg_ary = fixed_args;
16422 tree vec;
16423 bool saved_in_template_argument_list_p;
16424 bool saved_ice_p;
16425 bool saved_non_ice_p;
16427 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16428 parser->in_template_argument_list_p = true;
16429 /* Even if the template-id appears in an integral
16430 constant-expression, the contents of the argument list do
16431 not. */
16432 saved_ice_p = parser->integral_constant_expression_p;
16433 parser->integral_constant_expression_p = false;
16434 saved_non_ice_p = parser->non_integral_constant_expression_p;
16435 parser->non_integral_constant_expression_p = false;
16437 /* Parse the arguments. */
16440 tree argument;
16442 if (n_args)
16443 /* Consume the comma. */
16444 cp_lexer_consume_token (parser->lexer);
16446 /* Parse the template-argument. */
16447 argument = cp_parser_template_argument (parser);
16449 /* If the next token is an ellipsis, we're expanding a template
16450 argument pack. */
16451 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16453 if (argument == error_mark_node)
16455 cp_token *token = cp_lexer_peek_token (parser->lexer);
16456 error_at (token->location,
16457 "expected parameter pack before %<...%>");
16459 /* Consume the `...' token. */
16460 cp_lexer_consume_token (parser->lexer);
16462 /* Make the argument into a TYPE_PACK_EXPANSION or
16463 EXPR_PACK_EXPANSION. */
16464 argument = make_pack_expansion (argument);
16467 if (n_args == alloced)
16469 alloced *= 2;
16471 if (arg_ary == fixed_args)
16473 arg_ary = XNEWVEC (tree, alloced);
16474 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16476 else
16477 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16479 arg_ary[n_args++] = argument;
16481 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16483 vec = make_tree_vec (n_args);
16485 while (n_args--)
16486 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16488 if (arg_ary != fixed_args)
16489 free (arg_ary);
16490 parser->non_integral_constant_expression_p = saved_non_ice_p;
16491 parser->integral_constant_expression_p = saved_ice_p;
16492 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16493 if (CHECKING_P)
16494 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16495 return vec;
16498 /* Parse a template-argument.
16500 template-argument:
16501 assignment-expression
16502 type-id
16503 id-expression
16505 The representation is that of an assignment-expression, type-id, or
16506 id-expression -- except that the qualified id-expression is
16507 evaluated, so that the value returned is either a DECL or an
16508 OVERLOAD.
16510 Although the standard says "assignment-expression", it forbids
16511 throw-expressions or assignments in the template argument.
16512 Therefore, we use "conditional-expression" instead. */
16514 static tree
16515 cp_parser_template_argument (cp_parser* parser)
16517 tree argument;
16518 bool template_p;
16519 bool address_p;
16520 bool maybe_type_id = false;
16521 cp_token *token = NULL, *argument_start_token = NULL;
16522 location_t loc = 0;
16523 cp_id_kind idk;
16525 /* There's really no way to know what we're looking at, so we just
16526 try each alternative in order.
16528 [temp.arg]
16530 In a template-argument, an ambiguity between a type-id and an
16531 expression is resolved to a type-id, regardless of the form of
16532 the corresponding template-parameter.
16534 Therefore, we try a type-id first. */
16535 cp_parser_parse_tentatively (parser);
16536 argument = cp_parser_template_type_arg (parser);
16537 /* If there was no error parsing the type-id but the next token is a
16538 '>>', our behavior depends on which dialect of C++ we're
16539 parsing. In C++98, we probably found a typo for '> >'. But there
16540 are type-id which are also valid expressions. For instance:
16542 struct X { int operator >> (int); };
16543 template <int V> struct Foo {};
16544 Foo<X () >> 5> r;
16546 Here 'X()' is a valid type-id of a function type, but the user just
16547 wanted to write the expression "X() >> 5". Thus, we remember that we
16548 found a valid type-id, but we still try to parse the argument as an
16549 expression to see what happens.
16551 In C++0x, the '>>' will be considered two separate '>'
16552 tokens. */
16553 if (!cp_parser_error_occurred (parser)
16554 && cxx_dialect == cxx98
16555 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16557 maybe_type_id = true;
16558 cp_parser_abort_tentative_parse (parser);
16560 else
16562 /* If the next token isn't a `,' or a `>', then this argument wasn't
16563 really finished. This means that the argument is not a valid
16564 type-id. */
16565 if (!cp_parser_next_token_ends_template_argument_p (parser))
16566 cp_parser_error (parser, "expected template-argument");
16567 /* If that worked, we're done. */
16568 if (cp_parser_parse_definitely (parser))
16569 return argument;
16571 /* We're still not sure what the argument will be. */
16572 cp_parser_parse_tentatively (parser);
16573 /* Try a template. */
16574 argument_start_token = cp_lexer_peek_token (parser->lexer);
16575 argument = cp_parser_id_expression (parser,
16576 /*template_keyword_p=*/false,
16577 /*check_dependency_p=*/true,
16578 &template_p,
16579 /*declarator_p=*/false,
16580 /*optional_p=*/false);
16581 /* If the next token isn't a `,' or a `>', then this argument wasn't
16582 really finished. */
16583 if (!cp_parser_next_token_ends_template_argument_p (parser))
16584 cp_parser_error (parser, "expected template-argument");
16585 if (!cp_parser_error_occurred (parser))
16587 /* Figure out what is being referred to. If the id-expression
16588 was for a class template specialization, then we will have a
16589 TYPE_DECL at this point. There is no need to do name lookup
16590 at this point in that case. */
16591 if (TREE_CODE (argument) != TYPE_DECL)
16592 argument = cp_parser_lookup_name (parser, argument,
16593 none_type,
16594 /*is_template=*/template_p,
16595 /*is_namespace=*/false,
16596 /*check_dependency=*/true,
16597 /*ambiguous_decls=*/NULL,
16598 argument_start_token->location);
16599 /* Handle a constrained-type-specifier for a non-type template
16600 parameter. */
16601 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16602 argument = decl;
16603 else if (TREE_CODE (argument) != TEMPLATE_DECL
16604 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16605 cp_parser_error (parser, "expected template-name");
16607 if (cp_parser_parse_definitely (parser))
16609 if (TREE_DEPRECATED (argument))
16610 warn_deprecated_use (argument, NULL_TREE);
16611 return argument;
16613 /* It must be a non-type argument. In C++17 any constant-expression is
16614 allowed. */
16615 if (cxx_dialect > cxx14)
16616 goto general_expr;
16618 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16620 -- an integral constant-expression of integral or enumeration
16621 type; or
16623 -- the name of a non-type template-parameter; or
16625 -- the name of an object or function with external linkage...
16627 -- the address of an object or function with external linkage...
16629 -- a pointer to member... */
16630 /* Look for a non-type template parameter. */
16631 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16633 cp_parser_parse_tentatively (parser);
16634 argument = cp_parser_primary_expression (parser,
16635 /*address_p=*/false,
16636 /*cast_p=*/false,
16637 /*template_arg_p=*/true,
16638 &idk);
16639 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16640 || !cp_parser_next_token_ends_template_argument_p (parser))
16641 cp_parser_simulate_error (parser);
16642 if (cp_parser_parse_definitely (parser))
16643 return argument;
16646 /* If the next token is "&", the argument must be the address of an
16647 object or function with external linkage. */
16648 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16649 if (address_p)
16651 loc = cp_lexer_peek_token (parser->lexer)->location;
16652 cp_lexer_consume_token (parser->lexer);
16654 /* See if we might have an id-expression. */
16655 token = cp_lexer_peek_token (parser->lexer);
16656 if (token->type == CPP_NAME
16657 || token->keyword == RID_OPERATOR
16658 || token->type == CPP_SCOPE
16659 || token->type == CPP_TEMPLATE_ID
16660 || token->type == CPP_NESTED_NAME_SPECIFIER)
16662 cp_parser_parse_tentatively (parser);
16663 argument = cp_parser_primary_expression (parser,
16664 address_p,
16665 /*cast_p=*/false,
16666 /*template_arg_p=*/true,
16667 &idk);
16668 if (cp_parser_error_occurred (parser)
16669 || !cp_parser_next_token_ends_template_argument_p (parser))
16670 cp_parser_abort_tentative_parse (parser);
16671 else
16673 tree probe;
16675 if (INDIRECT_REF_P (argument))
16677 /* Strip the dereference temporarily. */
16678 gcc_assert (REFERENCE_REF_P (argument));
16679 argument = TREE_OPERAND (argument, 0);
16682 /* If we're in a template, we represent a qualified-id referring
16683 to a static data member as a SCOPE_REF even if the scope isn't
16684 dependent so that we can check access control later. */
16685 probe = argument;
16686 if (TREE_CODE (probe) == SCOPE_REF)
16687 probe = TREE_OPERAND (probe, 1);
16688 if (VAR_P (probe))
16690 /* A variable without external linkage might still be a
16691 valid constant-expression, so no error is issued here
16692 if the external-linkage check fails. */
16693 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16694 cp_parser_simulate_error (parser);
16696 else if (is_overloaded_fn (argument))
16697 /* All overloaded functions are allowed; if the external
16698 linkage test does not pass, an error will be issued
16699 later. */
16701 else if (address_p
16702 && (TREE_CODE (argument) == OFFSET_REF
16703 || TREE_CODE (argument) == SCOPE_REF))
16704 /* A pointer-to-member. */
16706 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16708 else
16709 cp_parser_simulate_error (parser);
16711 if (cp_parser_parse_definitely (parser))
16713 if (address_p)
16714 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16715 tf_warning_or_error);
16716 else
16717 argument = convert_from_reference (argument);
16718 return argument;
16722 /* If the argument started with "&", there are no other valid
16723 alternatives at this point. */
16724 if (address_p)
16726 cp_parser_error (parser, "invalid non-type template argument");
16727 return error_mark_node;
16730 general_expr:
16731 /* If the argument wasn't successfully parsed as a type-id followed
16732 by '>>', the argument can only be a constant expression now.
16733 Otherwise, we try parsing the constant-expression tentatively,
16734 because the argument could really be a type-id. */
16735 if (maybe_type_id)
16736 cp_parser_parse_tentatively (parser);
16738 if (cxx_dialect <= cxx14)
16739 argument = cp_parser_constant_expression (parser);
16740 else
16742 /* With C++17 generalized non-type template arguments we need to handle
16743 lvalue constant expressions, too. */
16744 argument = cp_parser_assignment_expression (parser);
16745 require_potential_constant_expression (argument);
16748 if (!maybe_type_id)
16749 return argument;
16750 if (!cp_parser_next_token_ends_template_argument_p (parser))
16751 cp_parser_error (parser, "expected template-argument");
16752 if (cp_parser_parse_definitely (parser))
16753 return argument;
16754 /* We did our best to parse the argument as a non type-id, but that
16755 was the only alternative that matched (albeit with a '>' after
16756 it). We can assume it's just a typo from the user, and a
16757 diagnostic will then be issued. */
16758 return cp_parser_template_type_arg (parser);
16761 /* Parse an explicit-instantiation.
16763 explicit-instantiation:
16764 template declaration
16766 Although the standard says `declaration', what it really means is:
16768 explicit-instantiation:
16769 template decl-specifier-seq [opt] declarator [opt] ;
16771 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16772 supposed to be allowed. A defect report has been filed about this
16773 issue.
16775 GNU Extension:
16777 explicit-instantiation:
16778 storage-class-specifier template
16779 decl-specifier-seq [opt] declarator [opt] ;
16780 function-specifier template
16781 decl-specifier-seq [opt] declarator [opt] ; */
16783 static void
16784 cp_parser_explicit_instantiation (cp_parser* parser)
16786 int declares_class_or_enum;
16787 cp_decl_specifier_seq decl_specifiers;
16788 tree extension_specifier = NULL_TREE;
16790 timevar_push (TV_TEMPLATE_INST);
16792 /* Look for an (optional) storage-class-specifier or
16793 function-specifier. */
16794 if (cp_parser_allow_gnu_extensions_p (parser))
16796 extension_specifier
16797 = cp_parser_storage_class_specifier_opt (parser);
16798 if (!extension_specifier)
16799 extension_specifier
16800 = cp_parser_function_specifier_opt (parser,
16801 /*decl_specs=*/NULL);
16804 /* Look for the `template' keyword. */
16805 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16806 /* Let the front end know that we are processing an explicit
16807 instantiation. */
16808 begin_explicit_instantiation ();
16809 /* [temp.explicit] says that we are supposed to ignore access
16810 control while processing explicit instantiation directives. */
16811 push_deferring_access_checks (dk_no_check);
16812 /* Parse a decl-specifier-seq. */
16813 cp_parser_decl_specifier_seq (parser,
16814 CP_PARSER_FLAGS_OPTIONAL,
16815 &decl_specifiers,
16816 &declares_class_or_enum);
16817 /* If there was exactly one decl-specifier, and it declared a class,
16818 and there's no declarator, then we have an explicit type
16819 instantiation. */
16820 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16822 tree type;
16824 type = check_tag_decl (&decl_specifiers,
16825 /*explicit_type_instantiation_p=*/true);
16826 /* Turn access control back on for names used during
16827 template instantiation. */
16828 pop_deferring_access_checks ();
16829 if (type)
16830 do_type_instantiation (type, extension_specifier,
16831 /*complain=*/tf_error);
16833 else
16835 cp_declarator *declarator;
16836 tree decl;
16838 /* Parse the declarator. */
16839 declarator
16840 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16841 /*ctor_dtor_or_conv_p=*/NULL,
16842 /*parenthesized_p=*/NULL,
16843 /*member_p=*/false,
16844 /*friend_p=*/false);
16845 if (declares_class_or_enum & 2)
16846 cp_parser_check_for_definition_in_return_type (declarator,
16847 decl_specifiers.type,
16848 decl_specifiers.locations[ds_type_spec]);
16849 if (declarator != cp_error_declarator)
16851 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16852 permerror (decl_specifiers.locations[ds_inline],
16853 "explicit instantiation shall not use"
16854 " %<inline%> specifier");
16855 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16856 permerror (decl_specifiers.locations[ds_constexpr],
16857 "explicit instantiation shall not use"
16858 " %<constexpr%> specifier");
16860 decl = grokdeclarator (declarator, &decl_specifiers,
16861 NORMAL, 0, &decl_specifiers.attributes);
16862 /* Turn access control back on for names used during
16863 template instantiation. */
16864 pop_deferring_access_checks ();
16865 /* Do the explicit instantiation. */
16866 do_decl_instantiation (decl, extension_specifier);
16868 else
16870 pop_deferring_access_checks ();
16871 /* Skip the body of the explicit instantiation. */
16872 cp_parser_skip_to_end_of_statement (parser);
16875 /* We're done with the instantiation. */
16876 end_explicit_instantiation ();
16878 cp_parser_consume_semicolon_at_end_of_statement (parser);
16880 timevar_pop (TV_TEMPLATE_INST);
16883 /* Parse an explicit-specialization.
16885 explicit-specialization:
16886 template < > declaration
16888 Although the standard says `declaration', what it really means is:
16890 explicit-specialization:
16891 template <> decl-specifier [opt] init-declarator [opt] ;
16892 template <> function-definition
16893 template <> explicit-specialization
16894 template <> template-declaration */
16896 static void
16897 cp_parser_explicit_specialization (cp_parser* parser)
16899 bool need_lang_pop;
16900 cp_token *token = cp_lexer_peek_token (parser->lexer);
16902 /* Look for the `template' keyword. */
16903 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16904 /* Look for the `<'. */
16905 cp_parser_require (parser, CPP_LESS, RT_LESS);
16906 /* Look for the `>'. */
16907 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16908 /* We have processed another parameter list. */
16909 ++parser->num_template_parameter_lists;
16910 /* [temp]
16912 A template ... explicit specialization ... shall not have C
16913 linkage. */
16914 if (current_lang_name == lang_name_c)
16916 error_at (token->location, "template specialization with C linkage");
16917 maybe_show_extern_c_location ();
16918 /* Give it C++ linkage to avoid confusing other parts of the
16919 front end. */
16920 push_lang_context (lang_name_cplusplus);
16921 need_lang_pop = true;
16923 else
16924 need_lang_pop = false;
16925 /* Let the front end know that we are beginning a specialization. */
16926 if (!begin_specialization ())
16928 end_specialization ();
16929 return;
16932 /* If the next keyword is `template', we need to figure out whether
16933 or not we're looking a template-declaration. */
16934 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16936 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16937 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16938 cp_parser_template_declaration_after_export (parser,
16939 /*member_p=*/false);
16940 else
16941 cp_parser_explicit_specialization (parser);
16943 else
16944 /* Parse the dependent declaration. */
16945 cp_parser_single_declaration (parser,
16946 /*checks=*/NULL,
16947 /*member_p=*/false,
16948 /*explicit_specialization_p=*/true,
16949 /*friend_p=*/NULL);
16950 /* We're done with the specialization. */
16951 end_specialization ();
16952 /* For the erroneous case of a template with C linkage, we pushed an
16953 implicit C++ linkage scope; exit that scope now. */
16954 if (need_lang_pop)
16955 pop_lang_context ();
16956 /* We're done with this parameter list. */
16957 --parser->num_template_parameter_lists;
16960 /* Parse a type-specifier.
16962 type-specifier:
16963 simple-type-specifier
16964 class-specifier
16965 enum-specifier
16966 elaborated-type-specifier
16967 cv-qualifier
16969 GNU Extension:
16971 type-specifier:
16972 __complex__
16974 Returns a representation of the type-specifier. For a
16975 class-specifier, enum-specifier, or elaborated-type-specifier, a
16976 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16978 The parser flags FLAGS is used to control type-specifier parsing.
16980 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16981 in a decl-specifier-seq.
16983 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16984 class-specifier, enum-specifier, or elaborated-type-specifier, then
16985 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16986 if a type is declared; 2 if it is defined. Otherwise, it is set to
16987 zero.
16989 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16990 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16991 is set to FALSE. */
16993 static tree
16994 cp_parser_type_specifier (cp_parser* parser,
16995 cp_parser_flags flags,
16996 cp_decl_specifier_seq *decl_specs,
16997 bool is_declaration,
16998 int* declares_class_or_enum,
16999 bool* is_cv_qualifier)
17001 tree type_spec = NULL_TREE;
17002 cp_token *token;
17003 enum rid keyword;
17004 cp_decl_spec ds = ds_last;
17006 /* Assume this type-specifier does not declare a new type. */
17007 if (declares_class_or_enum)
17008 *declares_class_or_enum = 0;
17009 /* And that it does not specify a cv-qualifier. */
17010 if (is_cv_qualifier)
17011 *is_cv_qualifier = false;
17012 /* Peek at the next token. */
17013 token = cp_lexer_peek_token (parser->lexer);
17015 /* If we're looking at a keyword, we can use that to guide the
17016 production we choose. */
17017 keyword = token->keyword;
17018 switch (keyword)
17020 case RID_ENUM:
17021 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17022 goto elaborated_type_specifier;
17024 /* Look for the enum-specifier. */
17025 type_spec = cp_parser_enum_specifier (parser);
17026 /* If that worked, we're done. */
17027 if (type_spec)
17029 if (declares_class_or_enum)
17030 *declares_class_or_enum = 2;
17031 if (decl_specs)
17032 cp_parser_set_decl_spec_type (decl_specs,
17033 type_spec,
17034 token,
17035 /*type_definition_p=*/true);
17036 return type_spec;
17038 else
17039 goto elaborated_type_specifier;
17041 /* Any of these indicate either a class-specifier, or an
17042 elaborated-type-specifier. */
17043 case RID_CLASS:
17044 case RID_STRUCT:
17045 case RID_UNION:
17046 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17047 goto elaborated_type_specifier;
17049 /* Parse tentatively so that we can back up if we don't find a
17050 class-specifier. */
17051 cp_parser_parse_tentatively (parser);
17052 /* Look for the class-specifier. */
17053 type_spec = cp_parser_class_specifier (parser);
17054 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17055 /* If that worked, we're done. */
17056 if (cp_parser_parse_definitely (parser))
17058 if (declares_class_or_enum)
17059 *declares_class_or_enum = 2;
17060 if (decl_specs)
17061 cp_parser_set_decl_spec_type (decl_specs,
17062 type_spec,
17063 token,
17064 /*type_definition_p=*/true);
17065 return type_spec;
17068 /* Fall through. */
17069 elaborated_type_specifier:
17070 /* We're declaring (not defining) a class or enum. */
17071 if (declares_class_or_enum)
17072 *declares_class_or_enum = 1;
17074 /* Fall through. */
17075 case RID_TYPENAME:
17076 /* Look for an elaborated-type-specifier. */
17077 type_spec
17078 = (cp_parser_elaborated_type_specifier
17079 (parser,
17080 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17081 is_declaration));
17082 if (decl_specs)
17083 cp_parser_set_decl_spec_type (decl_specs,
17084 type_spec,
17085 token,
17086 /*type_definition_p=*/false);
17087 return type_spec;
17089 case RID_CONST:
17090 ds = ds_const;
17091 if (is_cv_qualifier)
17092 *is_cv_qualifier = true;
17093 break;
17095 case RID_VOLATILE:
17096 ds = ds_volatile;
17097 if (is_cv_qualifier)
17098 *is_cv_qualifier = true;
17099 break;
17101 case RID_RESTRICT:
17102 ds = ds_restrict;
17103 if (is_cv_qualifier)
17104 *is_cv_qualifier = true;
17105 break;
17107 case RID_COMPLEX:
17108 /* The `__complex__' keyword is a GNU extension. */
17109 ds = ds_complex;
17110 break;
17112 default:
17113 break;
17116 /* Handle simple keywords. */
17117 if (ds != ds_last)
17119 if (decl_specs)
17121 set_and_check_decl_spec_loc (decl_specs, ds, token);
17122 decl_specs->any_specifiers_p = true;
17124 return cp_lexer_consume_token (parser->lexer)->u.value;
17127 /* If we do not already have a type-specifier, assume we are looking
17128 at a simple-type-specifier. */
17129 type_spec = cp_parser_simple_type_specifier (parser,
17130 decl_specs,
17131 flags);
17133 /* If we didn't find a type-specifier, and a type-specifier was not
17134 optional in this context, issue an error message. */
17135 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17137 cp_parser_error (parser, "expected type specifier");
17138 return error_mark_node;
17141 return type_spec;
17144 /* Parse a simple-type-specifier.
17146 simple-type-specifier:
17147 :: [opt] nested-name-specifier [opt] type-name
17148 :: [opt] nested-name-specifier template template-id
17149 char
17150 wchar_t
17151 bool
17152 short
17154 long
17155 signed
17156 unsigned
17157 float
17158 double
17159 void
17161 C++11 Extension:
17163 simple-type-specifier:
17164 auto
17165 decltype ( expression )
17166 char16_t
17167 char32_t
17168 __underlying_type ( type-id )
17170 C++17 extension:
17172 nested-name-specifier(opt) template-name
17174 GNU Extension:
17176 simple-type-specifier:
17177 __int128
17178 __typeof__ unary-expression
17179 __typeof__ ( type-id )
17180 __typeof__ ( type-id ) { initializer-list , [opt] }
17182 Concepts Extension:
17184 simple-type-specifier:
17185 constrained-type-specifier
17187 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17188 appropriately updated. */
17190 static tree
17191 cp_parser_simple_type_specifier (cp_parser* parser,
17192 cp_decl_specifier_seq *decl_specs,
17193 cp_parser_flags flags)
17195 tree type = NULL_TREE;
17196 cp_token *token;
17197 int idx;
17199 /* Peek at the next token. */
17200 token = cp_lexer_peek_token (parser->lexer);
17202 /* If we're looking at a keyword, things are easy. */
17203 switch (token->keyword)
17205 case RID_CHAR:
17206 if (decl_specs)
17207 decl_specs->explicit_char_p = true;
17208 type = char_type_node;
17209 break;
17210 case RID_CHAR16:
17211 type = char16_type_node;
17212 break;
17213 case RID_CHAR32:
17214 type = char32_type_node;
17215 break;
17216 case RID_WCHAR:
17217 type = wchar_type_node;
17218 break;
17219 case RID_BOOL:
17220 type = boolean_type_node;
17221 break;
17222 case RID_SHORT:
17223 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17224 type = short_integer_type_node;
17225 break;
17226 case RID_INT:
17227 if (decl_specs)
17228 decl_specs->explicit_int_p = true;
17229 type = integer_type_node;
17230 break;
17231 case RID_INT_N_0:
17232 case RID_INT_N_1:
17233 case RID_INT_N_2:
17234 case RID_INT_N_3:
17235 idx = token->keyword - RID_INT_N_0;
17236 if (! int_n_enabled_p [idx])
17237 break;
17238 if (decl_specs)
17240 decl_specs->explicit_intN_p = true;
17241 decl_specs->int_n_idx = idx;
17243 type = int_n_trees [idx].signed_type;
17244 break;
17245 case RID_LONG:
17246 if (decl_specs)
17247 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17248 type = long_integer_type_node;
17249 break;
17250 case RID_SIGNED:
17251 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17252 type = integer_type_node;
17253 break;
17254 case RID_UNSIGNED:
17255 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17256 type = unsigned_type_node;
17257 break;
17258 case RID_FLOAT:
17259 type = float_type_node;
17260 break;
17261 case RID_DOUBLE:
17262 type = double_type_node;
17263 break;
17264 case RID_VOID:
17265 type = void_type_node;
17266 break;
17268 case RID_AUTO:
17269 maybe_warn_cpp0x (CPP0X_AUTO);
17270 if (parser->auto_is_implicit_function_template_parm_p)
17272 /* The 'auto' might be the placeholder return type for a function decl
17273 with trailing return type. */
17274 bool have_trailing_return_fn_decl = false;
17276 cp_parser_parse_tentatively (parser);
17277 cp_lexer_consume_token (parser->lexer);
17278 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17279 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17280 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17281 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17283 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17285 cp_lexer_consume_token (parser->lexer);
17286 cp_parser_skip_to_closing_parenthesis (parser,
17287 /*recovering*/false,
17288 /*or_comma*/false,
17289 /*consume_paren*/true);
17290 continue;
17293 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17295 have_trailing_return_fn_decl = true;
17296 break;
17299 cp_lexer_consume_token (parser->lexer);
17301 cp_parser_abort_tentative_parse (parser);
17303 if (have_trailing_return_fn_decl)
17305 type = make_auto ();
17306 break;
17309 if (cxx_dialect >= cxx14)
17311 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17312 type = TREE_TYPE (type);
17314 else
17315 type = error_mark_node;
17317 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17319 if (cxx_dialect < cxx14)
17320 error_at (token->location,
17321 "use of %<auto%> in lambda parameter declaration "
17322 "only available with "
17323 "-std=c++14 or -std=gnu++14");
17325 else if (cxx_dialect < cxx14)
17326 error_at (token->location,
17327 "use of %<auto%> in parameter declaration "
17328 "only available with "
17329 "-std=c++14 or -std=gnu++14");
17330 else if (!flag_concepts)
17331 pedwarn (token->location, 0,
17332 "use of %<auto%> in parameter declaration "
17333 "only available with -fconcepts");
17335 else
17336 type = make_auto ();
17337 break;
17339 case RID_DECLTYPE:
17340 /* Since DR 743, decltype can either be a simple-type-specifier by
17341 itself or begin a nested-name-specifier. Parsing it will replace
17342 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17343 handling below decide what to do. */
17344 cp_parser_decltype (parser);
17345 cp_lexer_set_token_position (parser->lexer, token);
17346 break;
17348 case RID_TYPEOF:
17349 /* Consume the `typeof' token. */
17350 cp_lexer_consume_token (parser->lexer);
17351 /* Parse the operand to `typeof'. */
17352 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17353 /* If it is not already a TYPE, take its type. */
17354 if (!TYPE_P (type))
17355 type = finish_typeof (type);
17357 if (decl_specs)
17358 cp_parser_set_decl_spec_type (decl_specs, type,
17359 token,
17360 /*type_definition_p=*/false);
17362 return type;
17364 case RID_UNDERLYING_TYPE:
17365 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17366 if (decl_specs)
17367 cp_parser_set_decl_spec_type (decl_specs, type,
17368 token,
17369 /*type_definition_p=*/false);
17371 return type;
17373 case RID_BASES:
17374 case RID_DIRECT_BASES:
17375 type = cp_parser_trait_expr (parser, token->keyword);
17376 if (decl_specs)
17377 cp_parser_set_decl_spec_type (decl_specs, type,
17378 token,
17379 /*type_definition_p=*/false);
17380 return type;
17381 default:
17382 break;
17385 /* If token is an already-parsed decltype not followed by ::,
17386 it's a simple-type-specifier. */
17387 if (token->type == CPP_DECLTYPE
17388 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17390 type = saved_checks_value (token->u.tree_check_value);
17391 if (decl_specs)
17393 cp_parser_set_decl_spec_type (decl_specs, type,
17394 token,
17395 /*type_definition_p=*/false);
17396 /* Remember that we are handling a decltype in order to
17397 implement the resolution of DR 1510 when the argument
17398 isn't instantiation dependent. */
17399 decl_specs->decltype_p = true;
17401 cp_lexer_consume_token (parser->lexer);
17402 return type;
17405 /* If the type-specifier was for a built-in type, we're done. */
17406 if (type)
17408 /* Record the type. */
17409 if (decl_specs
17410 && (token->keyword != RID_SIGNED
17411 && token->keyword != RID_UNSIGNED
17412 && token->keyword != RID_SHORT
17413 && token->keyword != RID_LONG))
17414 cp_parser_set_decl_spec_type (decl_specs,
17415 type,
17416 token,
17417 /*type_definition_p=*/false);
17418 if (decl_specs)
17419 decl_specs->any_specifiers_p = true;
17421 /* Consume the token. */
17422 cp_lexer_consume_token (parser->lexer);
17424 if (type == error_mark_node)
17425 return error_mark_node;
17427 /* There is no valid C++ program where a non-template type is
17428 followed by a "<". That usually indicates that the user thought
17429 that the type was a template. */
17430 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17431 token->location);
17433 return TYPE_NAME (type);
17436 /* The type-specifier must be a user-defined type. */
17437 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17439 bool qualified_p;
17440 bool global_p;
17442 /* Don't gobble tokens or issue error messages if this is an
17443 optional type-specifier. */
17444 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17445 cp_parser_parse_tentatively (parser);
17447 token = cp_lexer_peek_token (parser->lexer);
17449 /* Look for the optional `::' operator. */
17450 global_p
17451 = (cp_parser_global_scope_opt (parser,
17452 /*current_scope_valid_p=*/false)
17453 != NULL_TREE);
17454 /* Look for the nested-name specifier. */
17455 qualified_p
17456 = (cp_parser_nested_name_specifier_opt (parser,
17457 /*typename_keyword_p=*/false,
17458 /*check_dependency_p=*/true,
17459 /*type_p=*/false,
17460 /*is_declaration=*/false)
17461 != NULL_TREE);
17462 /* If we have seen a nested-name-specifier, and the next token
17463 is `template', then we are using the template-id production. */
17464 if (parser->scope
17465 && cp_parser_optional_template_keyword (parser))
17467 /* Look for the template-id. */
17468 type = cp_parser_template_id (parser,
17469 /*template_keyword_p=*/true,
17470 /*check_dependency_p=*/true,
17471 none_type,
17472 /*is_declaration=*/false);
17473 /* If the template-id did not name a type, we are out of
17474 luck. */
17475 if (TREE_CODE (type) != TYPE_DECL)
17477 cp_parser_error (parser, "expected template-id for type");
17478 type = NULL_TREE;
17481 /* Otherwise, look for a type-name. */
17482 else
17483 type = cp_parser_type_name (parser);
17484 /* Keep track of all name-lookups performed in class scopes. */
17485 if (type
17486 && !global_p
17487 && !qualified_p
17488 && TREE_CODE (type) == TYPE_DECL
17489 && identifier_p (DECL_NAME (type)))
17490 maybe_note_name_used_in_class (DECL_NAME (type), type);
17491 /* If it didn't work out, we don't have a TYPE. */
17492 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17493 && !cp_parser_parse_definitely (parser))
17494 type = NULL_TREE;
17495 if (!type && cxx_dialect >= cxx17)
17497 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17498 cp_parser_parse_tentatively (parser);
17500 cp_parser_global_scope_opt (parser,
17501 /*current_scope_valid_p=*/false);
17502 cp_parser_nested_name_specifier_opt (parser,
17503 /*typename_keyword_p=*/false,
17504 /*check_dependency_p=*/true,
17505 /*type_p=*/false,
17506 /*is_declaration=*/false);
17507 tree name = cp_parser_identifier (parser);
17508 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17509 && parser->scope != error_mark_node)
17511 tree tmpl = cp_parser_lookup_name (parser, name,
17512 none_type,
17513 /*is_template=*/false,
17514 /*is_namespace=*/false,
17515 /*check_dependency=*/true,
17516 /*ambiguous_decls=*/NULL,
17517 token->location);
17518 if (tmpl && tmpl != error_mark_node
17519 && (DECL_CLASS_TEMPLATE_P (tmpl)
17520 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17521 type = make_template_placeholder (tmpl);
17522 else
17524 type = error_mark_node;
17525 if (!cp_parser_simulate_error (parser))
17526 cp_parser_name_lookup_error (parser, name, tmpl,
17527 NLE_TYPE, token->location);
17530 else
17531 type = error_mark_node;
17533 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17534 && !cp_parser_parse_definitely (parser))
17535 type = NULL_TREE;
17537 if (type && decl_specs)
17538 cp_parser_set_decl_spec_type (decl_specs, type,
17539 token,
17540 /*type_definition_p=*/false);
17543 /* If we didn't get a type-name, issue an error message. */
17544 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17546 cp_parser_error (parser, "expected type-name");
17547 return error_mark_node;
17550 if (type && type != error_mark_node)
17552 /* See if TYPE is an Objective-C type, and if so, parse and
17553 accept any protocol references following it. Do this before
17554 the cp_parser_check_for_invalid_template_id() call, because
17555 Objective-C types can be followed by '<...>' which would
17556 enclose protocol names rather than template arguments, and so
17557 everything is fine. */
17558 if (c_dialect_objc () && !parser->scope
17559 && (objc_is_id (type) || objc_is_class_name (type)))
17561 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17562 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17564 /* Clobber the "unqualified" type previously entered into
17565 DECL_SPECS with the new, improved protocol-qualified version. */
17566 if (decl_specs)
17567 decl_specs->type = qual_type;
17569 return qual_type;
17572 /* There is no valid C++ program where a non-template type is
17573 followed by a "<". That usually indicates that the user
17574 thought that the type was a template. */
17575 cp_parser_check_for_invalid_template_id (parser, type,
17576 none_type,
17577 token->location);
17580 return type;
17583 /* Parse a type-name.
17585 type-name:
17586 class-name
17587 enum-name
17588 typedef-name
17589 simple-template-id [in c++0x]
17591 enum-name:
17592 identifier
17594 typedef-name:
17595 identifier
17597 Concepts:
17599 type-name:
17600 concept-name
17601 partial-concept-id
17603 concept-name:
17604 identifier
17606 Returns a TYPE_DECL for the type. */
17608 static tree
17609 cp_parser_type_name (cp_parser* parser)
17611 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17614 /* See above. */
17615 static tree
17616 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17618 tree type_decl;
17620 /* We can't know yet whether it is a class-name or not. */
17621 cp_parser_parse_tentatively (parser);
17622 /* Try a class-name. */
17623 type_decl = cp_parser_class_name (parser,
17624 typename_keyword_p,
17625 /*template_keyword_p=*/false,
17626 none_type,
17627 /*check_dependency_p=*/true,
17628 /*class_head_p=*/false,
17629 /*is_declaration=*/false);
17630 /* If it's not a class-name, keep looking. */
17631 if (!cp_parser_parse_definitely (parser))
17633 if (cxx_dialect < cxx11)
17634 /* It must be a typedef-name or an enum-name. */
17635 return cp_parser_nonclass_name (parser);
17637 cp_parser_parse_tentatively (parser);
17638 /* It is either a simple-template-id representing an
17639 instantiation of an alias template... */
17640 type_decl = cp_parser_template_id (parser,
17641 /*template_keyword_p=*/false,
17642 /*check_dependency_p=*/true,
17643 none_type,
17644 /*is_declaration=*/false);
17645 /* Note that this must be an instantiation of an alias template
17646 because [temp.names]/6 says:
17648 A template-id that names an alias template specialization
17649 is a type-name.
17651 Whereas [temp.names]/7 says:
17653 A simple-template-id that names a class template
17654 specialization is a class-name.
17656 With concepts, this could also be a partial-concept-id that
17657 declares a non-type template parameter. */
17658 if (type_decl != NULL_TREE
17659 && TREE_CODE (type_decl) == TYPE_DECL
17660 && TYPE_DECL_ALIAS_P (type_decl))
17661 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17662 else if (is_constrained_parameter (type_decl))
17663 /* Don't do anything. */ ;
17664 else
17665 cp_parser_simulate_error (parser);
17667 if (!cp_parser_parse_definitely (parser))
17668 /* ... Or a typedef-name or an enum-name. */
17669 return cp_parser_nonclass_name (parser);
17672 return type_decl;
17675 /* Check if DECL and ARGS can form a constrained-type-specifier.
17676 If ARGS is non-null, we try to form a concept check of the
17677 form DECL<?, ARGS> where ? is a wildcard that matches any
17678 kind of template argument. If ARGS is NULL, then we try to
17679 form a concept check of the form DECL<?>. */
17681 static tree
17682 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17683 tree decl, tree args)
17685 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17687 /* If we a constrained-type-specifier cannot be deduced. */
17688 if (parser->prevent_constrained_type_specifiers)
17689 return NULL_TREE;
17691 /* A constrained type specifier can only be found in an
17692 overload set or as a reference to a template declaration.
17694 FIXME: This might be masking a bug. It's possible that
17695 that the deduction below is causing template specializations
17696 to be formed with the wildcard as an argument. */
17697 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17698 return NULL_TREE;
17700 /* Try to build a call expression that evaluates the
17701 concept. This can fail if the overload set refers
17702 only to non-templates. */
17703 tree placeholder = build_nt (WILDCARD_DECL);
17704 tree check = build_concept_check (decl, placeholder, args);
17705 if (check == error_mark_node)
17706 return NULL_TREE;
17708 /* Deduce the checked constraint and the prototype parameter.
17710 FIXME: In certain cases, failure to deduce should be a
17711 diagnosable error. */
17712 tree conc;
17713 tree proto;
17714 if (!deduce_constrained_parameter (check, conc, proto))
17715 return NULL_TREE;
17717 /* In template parameter scope, this results in a constrained
17718 parameter. Return a descriptor of that parm. */
17719 if (processing_template_parmlist)
17720 return build_constrained_parameter (conc, proto, args);
17722 /* In a parameter-declaration-clause, constrained-type
17723 specifiers result in invented template parameters. */
17724 if (parser->auto_is_implicit_function_template_parm_p)
17726 tree x = build_constrained_parameter (conc, proto, args);
17727 return synthesize_implicit_template_parm (parser, x);
17729 else
17731 /* Otherwise, we're in a context where the constrained
17732 type name is deduced and the constraint applies
17733 after deduction. */
17734 return make_constrained_auto (conc, args);
17737 return NULL_TREE;
17740 /* If DECL refers to a concept, return a TYPE_DECL representing
17741 the result of using the constrained type specifier in the
17742 current context. DECL refers to a concept if
17744 - it is an overload set containing a function concept taking a single
17745 type argument, or
17747 - it is a variable concept taking a single type argument. */
17749 static tree
17750 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17752 if (flag_concepts
17753 && (TREE_CODE (decl) == OVERLOAD
17754 || BASELINK_P (decl)
17755 || variable_concept_p (decl)))
17756 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17757 else
17758 return NULL_TREE;
17761 /* Check if DECL and ARGS form a partial-concept-id. If so,
17762 assign ID to the resulting constrained placeholder.
17764 Returns true if the partial-concept-id designates a placeholder
17765 and false otherwise. Note that *id is set to NULL_TREE in
17766 this case. */
17768 static tree
17769 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17771 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17774 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17775 or a concept-name.
17777 enum-name:
17778 identifier
17780 typedef-name:
17781 identifier
17783 concept-name:
17784 identifier
17786 Returns a TYPE_DECL for the type. */
17788 static tree
17789 cp_parser_nonclass_name (cp_parser* parser)
17791 tree type_decl;
17792 tree identifier;
17794 cp_token *token = cp_lexer_peek_token (parser->lexer);
17795 identifier = cp_parser_identifier (parser);
17796 if (identifier == error_mark_node)
17797 return error_mark_node;
17799 /* Look up the type-name. */
17800 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17802 type_decl = strip_using_decl (type_decl);
17804 /* If we found an overload set, then it may refer to a concept-name. */
17805 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17806 type_decl = decl;
17808 if (TREE_CODE (type_decl) != TYPE_DECL
17809 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17811 /* See if this is an Objective-C type. */
17812 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17813 tree type = objc_get_protocol_qualified_type (identifier, protos);
17814 if (type)
17815 type_decl = TYPE_NAME (type);
17818 /* Issue an error if we did not find a type-name. */
17819 if (TREE_CODE (type_decl) != TYPE_DECL
17820 /* In Objective-C, we have the complication that class names are
17821 normally type names and start declarations (eg, the
17822 "NSObject" in "NSObject *object;"), but can be used in an
17823 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17824 is an expression. So, a classname followed by a dot is not a
17825 valid type-name. */
17826 || (objc_is_class_name (TREE_TYPE (type_decl))
17827 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17829 if (!cp_parser_simulate_error (parser))
17830 cp_parser_name_lookup_error (parser, identifier, type_decl,
17831 NLE_TYPE, token->location);
17832 return error_mark_node;
17834 /* Remember that the name was used in the definition of the
17835 current class so that we can check later to see if the
17836 meaning would have been different after the class was
17837 entirely defined. */
17838 else if (type_decl != error_mark_node
17839 && !parser->scope)
17840 maybe_note_name_used_in_class (identifier, type_decl);
17842 return type_decl;
17845 /* Parse an elaborated-type-specifier. Note that the grammar given
17846 here incorporates the resolution to DR68.
17848 elaborated-type-specifier:
17849 class-key :: [opt] nested-name-specifier [opt] identifier
17850 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17851 enum-key :: [opt] nested-name-specifier [opt] identifier
17852 typename :: [opt] nested-name-specifier identifier
17853 typename :: [opt] nested-name-specifier template [opt]
17854 template-id
17856 GNU extension:
17858 elaborated-type-specifier:
17859 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17860 class-key attributes :: [opt] nested-name-specifier [opt]
17861 template [opt] template-id
17862 enum attributes :: [opt] nested-name-specifier [opt] identifier
17864 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17865 declared `friend'. If IS_DECLARATION is TRUE, then this
17866 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17867 something is being declared.
17869 Returns the TYPE specified. */
17871 static tree
17872 cp_parser_elaborated_type_specifier (cp_parser* parser,
17873 bool is_friend,
17874 bool is_declaration)
17876 enum tag_types tag_type;
17877 tree identifier;
17878 tree type = NULL_TREE;
17879 tree attributes = NULL_TREE;
17880 tree globalscope;
17881 cp_token *token = NULL;
17883 /* See if we're looking at the `enum' keyword. */
17884 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17886 /* Consume the `enum' token. */
17887 cp_lexer_consume_token (parser->lexer);
17888 /* Remember that it's an enumeration type. */
17889 tag_type = enum_type;
17890 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17891 enums) is used here. */
17892 cp_token *token = cp_lexer_peek_token (parser->lexer);
17893 if (cp_parser_is_keyword (token, RID_CLASS)
17894 || cp_parser_is_keyword (token, RID_STRUCT))
17896 gcc_rich_location richloc (token->location);
17897 richloc.add_range (input_location);
17898 richloc.add_fixit_remove ();
17899 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17900 "a scoped enum must not use the %qD keyword",
17901 token->u.value);
17902 /* Consume the `struct' or `class' and parse it anyway. */
17903 cp_lexer_consume_token (parser->lexer);
17905 /* Parse the attributes. */
17906 attributes = cp_parser_attributes_opt (parser);
17908 /* Or, it might be `typename'. */
17909 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17910 RID_TYPENAME))
17912 /* Consume the `typename' token. */
17913 cp_lexer_consume_token (parser->lexer);
17914 /* Remember that it's a `typename' type. */
17915 tag_type = typename_type;
17917 /* Otherwise it must be a class-key. */
17918 else
17920 tag_type = cp_parser_class_key (parser);
17921 if (tag_type == none_type)
17922 return error_mark_node;
17923 /* Parse the attributes. */
17924 attributes = cp_parser_attributes_opt (parser);
17927 /* Look for the `::' operator. */
17928 globalscope = cp_parser_global_scope_opt (parser,
17929 /*current_scope_valid_p=*/false);
17930 /* Look for the nested-name-specifier. */
17931 tree nested_name_specifier;
17932 if (tag_type == typename_type && !globalscope)
17934 nested_name_specifier
17935 = cp_parser_nested_name_specifier (parser,
17936 /*typename_keyword_p=*/true,
17937 /*check_dependency_p=*/true,
17938 /*type_p=*/true,
17939 is_declaration);
17940 if (!nested_name_specifier)
17941 return error_mark_node;
17943 else
17944 /* Even though `typename' is not present, the proposed resolution
17945 to Core Issue 180 says that in `class A<T>::B', `B' should be
17946 considered a type-name, even if `A<T>' is dependent. */
17947 nested_name_specifier
17948 = cp_parser_nested_name_specifier_opt (parser,
17949 /*typename_keyword_p=*/true,
17950 /*check_dependency_p=*/true,
17951 /*type_p=*/true,
17952 is_declaration);
17953 /* For everything but enumeration types, consider a template-id.
17954 For an enumeration type, consider only a plain identifier. */
17955 if (tag_type != enum_type)
17957 bool template_p = false;
17958 tree decl;
17960 /* Allow the `template' keyword. */
17961 template_p = cp_parser_optional_template_keyword (parser);
17962 /* If we didn't see `template', we don't know if there's a
17963 template-id or not. */
17964 if (!template_p)
17965 cp_parser_parse_tentatively (parser);
17966 /* Parse the template-id. */
17967 token = cp_lexer_peek_token (parser->lexer);
17968 decl = cp_parser_template_id (parser, template_p,
17969 /*check_dependency_p=*/true,
17970 tag_type,
17971 is_declaration);
17972 /* If we didn't find a template-id, look for an ordinary
17973 identifier. */
17974 if (!template_p && !cp_parser_parse_definitely (parser))
17976 /* We can get here when cp_parser_template_id, called by
17977 cp_parser_class_name with tag_type == none_type, succeeds
17978 and caches a BASELINK. Then, when called again here,
17979 instead of failing and returning an error_mark_node
17980 returns it (see template/typename17.C in C++11).
17981 ??? Could we diagnose this earlier? */
17982 else if (tag_type == typename_type && BASELINK_P (decl))
17984 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17985 type = error_mark_node;
17987 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17988 in effect, then we must assume that, upon instantiation, the
17989 template will correspond to a class. */
17990 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17991 && tag_type == typename_type)
17992 type = make_typename_type (parser->scope, decl,
17993 typename_type,
17994 /*complain=*/tf_error);
17995 /* If the `typename' keyword is in effect and DECL is not a type
17996 decl, then type is non existent. */
17997 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17999 else if (TREE_CODE (decl) == TYPE_DECL)
18001 type = check_elaborated_type_specifier (tag_type, decl,
18002 /*allow_template_p=*/true);
18004 /* If the next token is a semicolon, this must be a specialization,
18005 instantiation, or friend declaration. Check the scope while we
18006 still know whether or not we had a nested-name-specifier. */
18007 if (type != error_mark_node
18008 && !nested_name_specifier && !is_friend
18009 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18010 check_unqualified_spec_or_inst (type, token->location);
18012 else if (decl == error_mark_node)
18013 type = error_mark_node;
18016 if (!type)
18018 token = cp_lexer_peek_token (parser->lexer);
18019 identifier = cp_parser_identifier (parser);
18021 if (identifier == error_mark_node)
18023 parser->scope = NULL_TREE;
18024 return error_mark_node;
18027 /* For a `typename', we needn't call xref_tag. */
18028 if (tag_type == typename_type
18029 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18030 return cp_parser_make_typename_type (parser, identifier,
18031 token->location);
18033 /* Template parameter lists apply only if we are not within a
18034 function parameter list. */
18035 bool template_parm_lists_apply
18036 = parser->num_template_parameter_lists;
18037 if (template_parm_lists_apply)
18038 for (cp_binding_level *s = current_binding_level;
18039 s && s->kind != sk_template_parms;
18040 s = s->level_chain)
18041 if (s->kind == sk_function_parms)
18042 template_parm_lists_apply = false;
18044 /* Look up a qualified name in the usual way. */
18045 if (parser->scope)
18047 tree decl;
18048 tree ambiguous_decls;
18050 decl = cp_parser_lookup_name (parser, identifier,
18051 tag_type,
18052 /*is_template=*/false,
18053 /*is_namespace=*/false,
18054 /*check_dependency=*/true,
18055 &ambiguous_decls,
18056 token->location);
18058 /* If the lookup was ambiguous, an error will already have been
18059 issued. */
18060 if (ambiguous_decls)
18061 return error_mark_node;
18063 /* If we are parsing friend declaration, DECL may be a
18064 TEMPLATE_DECL tree node here. However, we need to check
18065 whether this TEMPLATE_DECL results in valid code. Consider
18066 the following example:
18068 namespace N {
18069 template <class T> class C {};
18071 class X {
18072 template <class T> friend class N::C; // #1, valid code
18074 template <class T> class Y {
18075 friend class N::C; // #2, invalid code
18078 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18079 name lookup of `N::C'. We see that friend declaration must
18080 be template for the code to be valid. Note that
18081 processing_template_decl does not work here since it is
18082 always 1 for the above two cases. */
18084 decl = (cp_parser_maybe_treat_template_as_class
18085 (decl, /*tag_name_p=*/is_friend
18086 && template_parm_lists_apply));
18088 if (TREE_CODE (decl) != TYPE_DECL)
18090 cp_parser_diagnose_invalid_type_name (parser,
18091 identifier,
18092 token->location);
18093 return error_mark_node;
18096 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18098 bool allow_template = (template_parm_lists_apply
18099 || DECL_SELF_REFERENCE_P (decl));
18100 type = check_elaborated_type_specifier (tag_type, decl,
18101 allow_template);
18103 if (type == error_mark_node)
18104 return error_mark_node;
18107 /* Forward declarations of nested types, such as
18109 class C1::C2;
18110 class C1::C2::C3;
18112 are invalid unless all components preceding the final '::'
18113 are complete. If all enclosing types are complete, these
18114 declarations become merely pointless.
18116 Invalid forward declarations of nested types are errors
18117 caught elsewhere in parsing. Those that are pointless arrive
18118 here. */
18120 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18121 && !is_friend && !processing_explicit_instantiation)
18122 warning (0, "declaration %qD does not declare anything", decl);
18124 type = TREE_TYPE (decl);
18126 else
18128 /* An elaborated-type-specifier sometimes introduces a new type and
18129 sometimes names an existing type. Normally, the rule is that it
18130 introduces a new type only if there is not an existing type of
18131 the same name already in scope. For example, given:
18133 struct S {};
18134 void f() { struct S s; }
18136 the `struct S' in the body of `f' is the same `struct S' as in
18137 the global scope; the existing definition is used. However, if
18138 there were no global declaration, this would introduce a new
18139 local class named `S'.
18141 An exception to this rule applies to the following code:
18143 namespace N { struct S; }
18145 Here, the elaborated-type-specifier names a new type
18146 unconditionally; even if there is already an `S' in the
18147 containing scope this declaration names a new type.
18148 This exception only applies if the elaborated-type-specifier
18149 forms the complete declaration:
18151 [class.name]
18153 A declaration consisting solely of `class-key identifier ;' is
18154 either a redeclaration of the name in the current scope or a
18155 forward declaration of the identifier as a class name. It
18156 introduces the name into the current scope.
18158 We are in this situation precisely when the next token is a `;'.
18160 An exception to the exception is that a `friend' declaration does
18161 *not* name a new type; i.e., given:
18163 struct S { friend struct T; };
18165 `T' is not a new type in the scope of `S'.
18167 Also, `new struct S' or `sizeof (struct S)' never results in the
18168 definition of a new type; a new type can only be declared in a
18169 declaration context. */
18171 tag_scope ts;
18172 bool template_p;
18174 if (is_friend)
18175 /* Friends have special name lookup rules. */
18176 ts = ts_within_enclosing_non_class;
18177 else if (is_declaration
18178 && cp_lexer_next_token_is (parser->lexer,
18179 CPP_SEMICOLON))
18180 /* This is a `class-key identifier ;' */
18181 ts = ts_current;
18182 else
18183 ts = ts_global;
18185 template_p =
18186 (template_parm_lists_apply
18187 && (cp_parser_next_token_starts_class_definition_p (parser)
18188 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18189 /* An unqualified name was used to reference this type, so
18190 there were no qualifying templates. */
18191 if (template_parm_lists_apply
18192 && !cp_parser_check_template_parameters (parser,
18193 /*num_templates=*/0,
18194 /*template_id*/false,
18195 token->location,
18196 /*declarator=*/NULL))
18197 return error_mark_node;
18198 type = xref_tag (tag_type, identifier, ts, template_p);
18202 if (type == error_mark_node)
18203 return error_mark_node;
18205 /* Allow attributes on forward declarations of classes. */
18206 if (attributes)
18208 if (TREE_CODE (type) == TYPENAME_TYPE)
18209 warning (OPT_Wattributes,
18210 "attributes ignored on uninstantiated type");
18211 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18212 && ! processing_explicit_instantiation)
18213 warning (OPT_Wattributes,
18214 "attributes ignored on template instantiation");
18215 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18216 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18217 else
18218 warning (OPT_Wattributes,
18219 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18222 if (tag_type != enum_type)
18224 /* Indicate whether this class was declared as a `class' or as a
18225 `struct'. */
18226 if (CLASS_TYPE_P (type))
18227 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18228 cp_parser_check_class_key (tag_type, type);
18231 /* A "<" cannot follow an elaborated type specifier. If that
18232 happens, the user was probably trying to form a template-id. */
18233 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18234 token->location);
18236 return type;
18239 /* Parse an enum-specifier.
18241 enum-specifier:
18242 enum-head { enumerator-list [opt] }
18243 enum-head { enumerator-list , } [C++0x]
18245 enum-head:
18246 enum-key identifier [opt] enum-base [opt]
18247 enum-key nested-name-specifier identifier enum-base [opt]
18249 enum-key:
18250 enum
18251 enum class [C++0x]
18252 enum struct [C++0x]
18254 enum-base: [C++0x]
18255 : type-specifier-seq
18257 opaque-enum-specifier:
18258 enum-key identifier enum-base [opt] ;
18260 GNU Extensions:
18261 enum-key attributes[opt] identifier [opt] enum-base [opt]
18262 { enumerator-list [opt] }attributes[opt]
18263 enum-key attributes[opt] identifier [opt] enum-base [opt]
18264 { enumerator-list, }attributes[opt] [C++0x]
18266 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18267 if the token stream isn't an enum-specifier after all. */
18269 static tree
18270 cp_parser_enum_specifier (cp_parser* parser)
18272 tree identifier;
18273 tree type = NULL_TREE;
18274 tree prev_scope;
18275 tree nested_name_specifier = NULL_TREE;
18276 tree attributes;
18277 bool scoped_enum_p = false;
18278 bool has_underlying_type = false;
18279 bool nested_being_defined = false;
18280 bool new_value_list = false;
18281 bool is_new_type = false;
18282 bool is_unnamed = false;
18283 tree underlying_type = NULL_TREE;
18284 cp_token *type_start_token = NULL;
18285 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18287 parser->colon_corrects_to_scope_p = false;
18289 /* Parse tentatively so that we can back up if we don't find a
18290 enum-specifier. */
18291 cp_parser_parse_tentatively (parser);
18293 /* Caller guarantees that the current token is 'enum', an identifier
18294 possibly follows, and the token after that is an opening brace.
18295 If we don't have an identifier, fabricate an anonymous name for
18296 the enumeration being defined. */
18297 cp_lexer_consume_token (parser->lexer);
18299 /* Parse the "class" or "struct", which indicates a scoped
18300 enumeration type in C++0x. */
18301 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18302 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18304 if (cxx_dialect < cxx11)
18305 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18307 /* Consume the `struct' or `class' token. */
18308 cp_lexer_consume_token (parser->lexer);
18310 scoped_enum_p = true;
18313 attributes = cp_parser_attributes_opt (parser);
18315 /* Clear the qualification. */
18316 parser->scope = NULL_TREE;
18317 parser->qualifying_scope = NULL_TREE;
18318 parser->object_scope = NULL_TREE;
18320 /* Figure out in what scope the declaration is being placed. */
18321 prev_scope = current_scope ();
18323 type_start_token = cp_lexer_peek_token (parser->lexer);
18325 push_deferring_access_checks (dk_no_check);
18326 nested_name_specifier
18327 = cp_parser_nested_name_specifier_opt (parser,
18328 /*typename_keyword_p=*/true,
18329 /*check_dependency_p=*/false,
18330 /*type_p=*/false,
18331 /*is_declaration=*/false);
18333 if (nested_name_specifier)
18335 tree name;
18337 identifier = cp_parser_identifier (parser);
18338 name = cp_parser_lookup_name (parser, identifier,
18339 enum_type,
18340 /*is_template=*/false,
18341 /*is_namespace=*/false,
18342 /*check_dependency=*/true,
18343 /*ambiguous_decls=*/NULL,
18344 input_location);
18345 if (name && name != error_mark_node)
18347 type = TREE_TYPE (name);
18348 if (TREE_CODE (type) == TYPENAME_TYPE)
18350 /* Are template enums allowed in ISO? */
18351 if (template_parm_scope_p ())
18352 pedwarn (type_start_token->location, OPT_Wpedantic,
18353 "%qD is an enumeration template", name);
18354 /* ignore a typename reference, for it will be solved by name
18355 in start_enum. */
18356 type = NULL_TREE;
18359 else if (nested_name_specifier == error_mark_node)
18360 /* We already issued an error. */;
18361 else
18363 error_at (type_start_token->location,
18364 "%qD does not name an enumeration in %qT",
18365 identifier, nested_name_specifier);
18366 nested_name_specifier = error_mark_node;
18369 else
18371 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18372 identifier = cp_parser_identifier (parser);
18373 else
18375 identifier = make_anon_name ();
18376 is_unnamed = true;
18377 if (scoped_enum_p)
18378 error_at (type_start_token->location,
18379 "unnamed scoped enum is not allowed");
18382 pop_deferring_access_checks ();
18384 /* Check for the `:' that denotes a specified underlying type in C++0x.
18385 Note that a ':' could also indicate a bitfield width, however. */
18386 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18388 cp_decl_specifier_seq type_specifiers;
18390 /* Consume the `:'. */
18391 cp_lexer_consume_token (parser->lexer);
18393 /* Parse the type-specifier-seq. */
18394 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18395 /*is_trailing_return=*/false,
18396 &type_specifiers);
18398 /* At this point this is surely not elaborated type specifier. */
18399 if (!cp_parser_parse_definitely (parser))
18400 return NULL_TREE;
18402 if (cxx_dialect < cxx11)
18403 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18405 has_underlying_type = true;
18407 /* If that didn't work, stop. */
18408 if (type_specifiers.type != error_mark_node)
18410 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18411 /*initialized=*/0, NULL);
18412 if (underlying_type == error_mark_node
18413 || check_for_bare_parameter_packs (underlying_type))
18414 underlying_type = NULL_TREE;
18418 /* Look for the `{' but don't consume it yet. */
18419 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18421 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18423 cp_parser_error (parser, "expected %<{%>");
18424 if (has_underlying_type)
18426 type = NULL_TREE;
18427 goto out;
18430 /* An opaque-enum-specifier must have a ';' here. */
18431 if ((scoped_enum_p || underlying_type)
18432 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18434 cp_parser_error (parser, "expected %<;%> or %<{%>");
18435 if (has_underlying_type)
18437 type = NULL_TREE;
18438 goto out;
18443 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18444 return NULL_TREE;
18446 if (nested_name_specifier)
18448 if (CLASS_TYPE_P (nested_name_specifier))
18450 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18451 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18452 push_scope (nested_name_specifier);
18454 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18456 push_nested_namespace (nested_name_specifier);
18460 /* Issue an error message if type-definitions are forbidden here. */
18461 if (!cp_parser_check_type_definition (parser))
18462 type = error_mark_node;
18463 else
18464 /* Create the new type. We do this before consuming the opening
18465 brace so the enum will be recorded as being on the line of its
18466 tag (or the 'enum' keyword, if there is no tag). */
18467 type = start_enum (identifier, type, underlying_type,
18468 attributes, scoped_enum_p, &is_new_type);
18470 /* If the next token is not '{' it is an opaque-enum-specifier or an
18471 elaborated-type-specifier. */
18472 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18474 timevar_push (TV_PARSE_ENUM);
18475 if (nested_name_specifier
18476 && nested_name_specifier != error_mark_node)
18478 /* The following catches invalid code such as:
18479 enum class S<int>::E { A, B, C }; */
18480 if (!processing_specialization
18481 && CLASS_TYPE_P (nested_name_specifier)
18482 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18483 error_at (type_start_token->location, "cannot add an enumerator "
18484 "list to a template instantiation");
18486 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18488 error_at (type_start_token->location,
18489 "%<%T::%E%> has not been declared",
18490 TYPE_CONTEXT (nested_name_specifier),
18491 nested_name_specifier);
18492 type = error_mark_node;
18494 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18495 && !CLASS_TYPE_P (nested_name_specifier))
18497 error_at (type_start_token->location, "nested name specifier "
18498 "%qT for enum declaration does not name a class "
18499 "or namespace", nested_name_specifier);
18500 type = error_mark_node;
18502 /* If that scope does not contain the scope in which the
18503 class was originally declared, the program is invalid. */
18504 else if (prev_scope && !is_ancestor (prev_scope,
18505 nested_name_specifier))
18507 if (at_namespace_scope_p ())
18508 error_at (type_start_token->location,
18509 "declaration of %qD in namespace %qD which does not "
18510 "enclose %qD",
18511 type, prev_scope, nested_name_specifier);
18512 else
18513 error_at (type_start_token->location,
18514 "declaration of %qD in %qD which does not "
18515 "enclose %qD",
18516 type, prev_scope, nested_name_specifier);
18517 type = error_mark_node;
18519 /* If that scope is the scope where the declaration is being placed
18520 the program is invalid. */
18521 else if (CLASS_TYPE_P (nested_name_specifier)
18522 && CLASS_TYPE_P (prev_scope)
18523 && same_type_p (nested_name_specifier, prev_scope))
18525 permerror (type_start_token->location,
18526 "extra qualification not allowed");
18527 nested_name_specifier = NULL_TREE;
18531 if (scoped_enum_p)
18532 begin_scope (sk_scoped_enum, type);
18534 /* Consume the opening brace. */
18535 matching_braces braces;
18536 braces.consume_open (parser);
18538 if (type == error_mark_node)
18539 ; /* Nothing to add */
18540 else if (OPAQUE_ENUM_P (type)
18541 || (cxx_dialect > cxx98 && processing_specialization))
18543 new_value_list = true;
18544 SET_OPAQUE_ENUM_P (type, false);
18545 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18547 else
18549 error_at (type_start_token->location,
18550 "multiple definition of %q#T", type);
18551 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18552 "previous definition here");
18553 type = error_mark_node;
18556 if (type == error_mark_node)
18557 cp_parser_skip_to_end_of_block_or_statement (parser);
18558 /* If the next token is not '}', then there are some enumerators. */
18559 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18561 if (is_unnamed && !scoped_enum_p)
18562 pedwarn (type_start_token->location, OPT_Wpedantic,
18563 "ISO C++ forbids empty unnamed enum");
18565 else
18566 cp_parser_enumerator_list (parser, type);
18568 /* Consume the final '}'. */
18569 braces.require_close (parser);
18571 if (scoped_enum_p)
18572 finish_scope ();
18573 timevar_pop (TV_PARSE_ENUM);
18575 else
18577 /* If a ';' follows, then it is an opaque-enum-specifier
18578 and additional restrictions apply. */
18579 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18581 if (is_unnamed)
18582 error_at (type_start_token->location,
18583 "opaque-enum-specifier without name");
18584 else if (nested_name_specifier)
18585 error_at (type_start_token->location,
18586 "opaque-enum-specifier must use a simple identifier");
18590 /* Look for trailing attributes to apply to this enumeration, and
18591 apply them if appropriate. */
18592 if (cp_parser_allow_gnu_extensions_p (parser))
18594 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18595 cplus_decl_attributes (&type,
18596 trailing_attr,
18597 (int) ATTR_FLAG_TYPE_IN_PLACE);
18600 /* Finish up the enumeration. */
18601 if (type != error_mark_node)
18603 if (new_value_list)
18604 finish_enum_value_list (type);
18605 if (is_new_type)
18606 finish_enum (type);
18609 if (nested_name_specifier)
18611 if (CLASS_TYPE_P (nested_name_specifier))
18613 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18614 pop_scope (nested_name_specifier);
18616 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18618 pop_nested_namespace (nested_name_specifier);
18621 out:
18622 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18623 return type;
18626 /* Parse an enumerator-list. The enumerators all have the indicated
18627 TYPE.
18629 enumerator-list:
18630 enumerator-definition
18631 enumerator-list , enumerator-definition */
18633 static void
18634 cp_parser_enumerator_list (cp_parser* parser, tree type)
18636 while (true)
18638 /* Parse an enumerator-definition. */
18639 cp_parser_enumerator_definition (parser, type);
18641 /* If the next token is not a ',', we've reached the end of
18642 the list. */
18643 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18644 break;
18645 /* Otherwise, consume the `,' and keep going. */
18646 cp_lexer_consume_token (parser->lexer);
18647 /* If the next token is a `}', there is a trailing comma. */
18648 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18650 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18651 pedwarn (input_location, OPT_Wpedantic,
18652 "comma at end of enumerator list");
18653 break;
18658 /* Parse an enumerator-definition. The enumerator has the indicated
18659 TYPE.
18661 enumerator-definition:
18662 enumerator
18663 enumerator = constant-expression
18665 enumerator:
18666 identifier
18668 GNU Extensions:
18670 enumerator-definition:
18671 enumerator attributes [opt]
18672 enumerator attributes [opt] = constant-expression */
18674 static void
18675 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18677 tree identifier;
18678 tree value;
18679 location_t loc;
18681 /* Save the input location because we are interested in the location
18682 of the identifier and not the location of the explicit value. */
18683 loc = cp_lexer_peek_token (parser->lexer)->location;
18685 /* Look for the identifier. */
18686 identifier = cp_parser_identifier (parser);
18687 if (identifier == error_mark_node)
18688 return;
18690 /* Parse any specified attributes. */
18691 tree attrs = cp_parser_attributes_opt (parser);
18693 /* If the next token is an '=', then there is an explicit value. */
18694 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18696 /* Consume the `=' token. */
18697 cp_lexer_consume_token (parser->lexer);
18698 /* Parse the value. */
18699 value = cp_parser_constant_expression (parser);
18701 else
18702 value = NULL_TREE;
18704 /* If we are processing a template, make sure the initializer of the
18705 enumerator doesn't contain any bare template parameter pack. */
18706 if (check_for_bare_parameter_packs (value))
18707 value = error_mark_node;
18709 /* Create the enumerator. */
18710 build_enumerator (identifier, value, type, attrs, loc);
18713 /* Parse a namespace-name.
18715 namespace-name:
18716 original-namespace-name
18717 namespace-alias
18719 Returns the NAMESPACE_DECL for the namespace. */
18721 static tree
18722 cp_parser_namespace_name (cp_parser* parser)
18724 tree identifier;
18725 tree namespace_decl;
18727 cp_token *token = cp_lexer_peek_token (parser->lexer);
18729 /* Get the name of the namespace. */
18730 identifier = cp_parser_identifier (parser);
18731 if (identifier == error_mark_node)
18732 return error_mark_node;
18734 /* Look up the identifier in the currently active scope. Look only
18735 for namespaces, due to:
18737 [basic.lookup.udir]
18739 When looking up a namespace-name in a using-directive or alias
18740 definition, only namespace names are considered.
18742 And:
18744 [basic.lookup.qual]
18746 During the lookup of a name preceding the :: scope resolution
18747 operator, object, function, and enumerator names are ignored.
18749 (Note that cp_parser_qualifying_entity only calls this
18750 function if the token after the name is the scope resolution
18751 operator.) */
18752 namespace_decl = cp_parser_lookup_name (parser, identifier,
18753 none_type,
18754 /*is_template=*/false,
18755 /*is_namespace=*/true,
18756 /*check_dependency=*/true,
18757 /*ambiguous_decls=*/NULL,
18758 token->location);
18759 /* If it's not a namespace, issue an error. */
18760 if (namespace_decl == error_mark_node
18761 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18763 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18765 auto_diagnostic_group d;
18766 name_hint hint;
18767 if (namespace_decl == error_mark_node
18768 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18769 hint = suggest_alternative_in_explicit_scope (token->location,
18770 identifier,
18771 parser->scope);
18772 if (const char *suggestion = hint.suggestion ())
18774 gcc_rich_location richloc (token->location);
18775 richloc.add_fixit_replace (suggestion);
18776 error_at (&richloc,
18777 "%qD is not a namespace-name; did you mean %qs?",
18778 identifier, suggestion);
18780 else
18781 error_at (token->location, "%qD is not a namespace-name",
18782 identifier);
18784 else
18785 cp_parser_error (parser, "expected namespace-name");
18786 namespace_decl = error_mark_node;
18789 return namespace_decl;
18792 /* Parse a namespace-definition.
18794 namespace-definition:
18795 named-namespace-definition
18796 unnamed-namespace-definition
18798 named-namespace-definition:
18799 original-namespace-definition
18800 extension-namespace-definition
18802 original-namespace-definition:
18803 namespace identifier { namespace-body }
18805 extension-namespace-definition:
18806 namespace original-namespace-name { namespace-body }
18808 unnamed-namespace-definition:
18809 namespace { namespace-body } */
18811 static void
18812 cp_parser_namespace_definition (cp_parser* parser)
18814 tree identifier;
18815 int nested_definition_count = 0;
18817 cp_ensure_no_omp_declare_simd (parser);
18818 cp_ensure_no_oacc_routine (parser);
18820 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18822 if (is_inline)
18824 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18825 cp_lexer_consume_token (parser->lexer);
18828 /* Look for the `namespace' keyword. */
18829 cp_token* token
18830 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18832 /* Parse any specified attributes before the identifier. */
18833 tree attribs = cp_parser_attributes_opt (parser);
18835 for (;;)
18837 identifier = NULL_TREE;
18839 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18841 identifier = cp_parser_identifier (parser);
18843 if (cp_next_tokens_can_be_std_attribute_p (parser))
18844 pedwarn (input_location, OPT_Wpedantic,
18845 "standard attributes on namespaces must precede "
18846 "the namespace name");
18848 /* Parse any attributes specified after the identifier. */
18849 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18852 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18853 break;
18855 if (!nested_definition_count && cxx_dialect < cxx17)
18856 pedwarn (input_location, OPT_Wpedantic,
18857 "nested namespace definitions only available with "
18858 "-std=c++17 or -std=gnu++17");
18860 /* Nested namespace names can create new namespaces (unlike
18861 other qualified-ids). */
18862 if (int count = identifier ? push_namespace (identifier) : 0)
18863 nested_definition_count += count;
18864 else
18865 cp_parser_error (parser, "nested namespace name required");
18866 cp_lexer_consume_token (parser->lexer);
18869 if (nested_definition_count && !identifier)
18870 cp_parser_error (parser, "namespace name required");
18872 if (nested_definition_count && attribs)
18873 error_at (token->location,
18874 "a nested namespace definition cannot have attributes");
18875 if (nested_definition_count && is_inline)
18876 error_at (token->location,
18877 "a nested namespace definition cannot be inline");
18879 /* Start the namespace. */
18880 nested_definition_count += push_namespace (identifier, is_inline);
18882 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18884 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18886 /* Look for the `{' to validate starting the namespace. */
18887 matching_braces braces;
18888 if (braces.require_open (parser))
18890 /* Parse the body of the namespace. */
18891 cp_parser_namespace_body (parser);
18893 /* Look for the final `}'. */
18894 braces.require_close (parser);
18897 if (has_visibility)
18898 pop_visibility (1);
18900 /* Pop the nested namespace definitions. */
18901 while (nested_definition_count--)
18902 pop_namespace ();
18905 /* Parse a namespace-body.
18907 namespace-body:
18908 declaration-seq [opt] */
18910 static void
18911 cp_parser_namespace_body (cp_parser* parser)
18913 cp_parser_declaration_seq_opt (parser);
18916 /* Parse a namespace-alias-definition.
18918 namespace-alias-definition:
18919 namespace identifier = qualified-namespace-specifier ; */
18921 static void
18922 cp_parser_namespace_alias_definition (cp_parser* parser)
18924 tree identifier;
18925 tree namespace_specifier;
18927 cp_token *token = cp_lexer_peek_token (parser->lexer);
18929 /* Look for the `namespace' keyword. */
18930 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18931 /* Look for the identifier. */
18932 identifier = cp_parser_identifier (parser);
18933 if (identifier == error_mark_node)
18934 return;
18935 /* Look for the `=' token. */
18936 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18937 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18939 error_at (token->location, "%<namespace%> definition is not allowed here");
18940 /* Skip the definition. */
18941 cp_lexer_consume_token (parser->lexer);
18942 if (cp_parser_skip_to_closing_brace (parser))
18943 cp_lexer_consume_token (parser->lexer);
18944 return;
18946 cp_parser_require (parser, CPP_EQ, RT_EQ);
18947 /* Look for the qualified-namespace-specifier. */
18948 namespace_specifier
18949 = cp_parser_qualified_namespace_specifier (parser);
18950 /* Look for the `;' token. */
18951 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18953 /* Register the alias in the symbol table. */
18954 do_namespace_alias (identifier, namespace_specifier);
18957 /* Parse a qualified-namespace-specifier.
18959 qualified-namespace-specifier:
18960 :: [opt] nested-name-specifier [opt] namespace-name
18962 Returns a NAMESPACE_DECL corresponding to the specified
18963 namespace. */
18965 static tree
18966 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18968 /* Look for the optional `::'. */
18969 cp_parser_global_scope_opt (parser,
18970 /*current_scope_valid_p=*/false);
18972 /* Look for the optional nested-name-specifier. */
18973 cp_parser_nested_name_specifier_opt (parser,
18974 /*typename_keyword_p=*/false,
18975 /*check_dependency_p=*/true,
18976 /*type_p=*/false,
18977 /*is_declaration=*/true);
18979 return cp_parser_namespace_name (parser);
18982 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18983 access declaration.
18985 using-declaration:
18986 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18987 using :: unqualified-id ;
18989 access-declaration:
18990 qualified-id ;
18994 static bool
18995 cp_parser_using_declaration (cp_parser* parser,
18996 bool access_declaration_p)
18998 cp_token *token;
18999 bool typename_p = false;
19000 bool global_scope_p;
19001 tree decl;
19002 tree identifier;
19003 tree qscope;
19004 int oldcount = errorcount;
19005 cp_token *diag_token = NULL;
19007 if (access_declaration_p)
19009 diag_token = cp_lexer_peek_token (parser->lexer);
19010 cp_parser_parse_tentatively (parser);
19012 else
19014 /* Look for the `using' keyword. */
19015 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19017 again:
19018 /* Peek at the next token. */
19019 token = cp_lexer_peek_token (parser->lexer);
19020 /* See if it's `typename'. */
19021 if (token->keyword == RID_TYPENAME)
19023 /* Remember that we've seen it. */
19024 typename_p = true;
19025 /* Consume the `typename' token. */
19026 cp_lexer_consume_token (parser->lexer);
19030 /* Look for the optional global scope qualification. */
19031 global_scope_p
19032 = (cp_parser_global_scope_opt (parser,
19033 /*current_scope_valid_p=*/false)
19034 != NULL_TREE);
19036 /* If we saw `typename', or didn't see `::', then there must be a
19037 nested-name-specifier present. */
19038 if (typename_p || !global_scope_p)
19040 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19041 /*check_dependency_p=*/true,
19042 /*type_p=*/false,
19043 /*is_declaration=*/true);
19044 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19046 cp_parser_skip_to_end_of_block_or_statement (parser);
19047 return false;
19050 /* Otherwise, we could be in either of the two productions. In that
19051 case, treat the nested-name-specifier as optional. */
19052 else
19053 qscope = cp_parser_nested_name_specifier_opt (parser,
19054 /*typename_keyword_p=*/false,
19055 /*check_dependency_p=*/true,
19056 /*type_p=*/false,
19057 /*is_declaration=*/true);
19058 if (!qscope)
19059 qscope = global_namespace;
19060 else if (UNSCOPED_ENUM_P (qscope))
19061 qscope = CP_TYPE_CONTEXT (qscope);
19063 if (access_declaration_p && cp_parser_error_occurred (parser))
19064 /* Something has already gone wrong; there's no need to parse
19065 further. Since an error has occurred, the return value of
19066 cp_parser_parse_definitely will be false, as required. */
19067 return cp_parser_parse_definitely (parser);
19069 token = cp_lexer_peek_token (parser->lexer);
19070 /* Parse the unqualified-id. */
19071 identifier = cp_parser_unqualified_id (parser,
19072 /*template_keyword_p=*/false,
19073 /*check_dependency_p=*/true,
19074 /*declarator_p=*/true,
19075 /*optional_p=*/false);
19077 if (access_declaration_p)
19079 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19080 cp_parser_simulate_error (parser);
19081 if (!cp_parser_parse_definitely (parser))
19082 return false;
19084 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19086 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19087 if (cxx_dialect < cxx17
19088 && !in_system_header_at (ell->location))
19089 pedwarn (ell->location, 0,
19090 "pack expansion in using-declaration only available "
19091 "with -std=c++17 or -std=gnu++17");
19092 qscope = make_pack_expansion (qscope);
19095 /* The function we call to handle a using-declaration is different
19096 depending on what scope we are in. */
19097 if (qscope == error_mark_node || identifier == error_mark_node)
19099 else if (!identifier_p (identifier)
19100 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19101 /* [namespace.udecl]
19103 A using declaration shall not name a template-id. */
19104 error_at (token->location,
19105 "a template-id may not appear in a using-declaration");
19106 else
19108 if (at_class_scope_p ())
19110 /* Create the USING_DECL. */
19111 decl = do_class_using_decl (qscope, identifier);
19113 if (decl && typename_p)
19114 USING_DECL_TYPENAME_P (decl) = 1;
19116 if (check_for_bare_parameter_packs (decl))
19118 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19119 return false;
19121 else
19122 /* Add it to the list of members in this class. */
19123 finish_member_declaration (decl);
19125 else
19127 decl = cp_parser_lookup_name_simple (parser,
19128 identifier,
19129 token->location);
19130 if (decl == error_mark_node)
19131 cp_parser_name_lookup_error (parser, identifier,
19132 decl, NLE_NULL,
19133 token->location);
19134 else if (check_for_bare_parameter_packs (decl))
19136 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19137 return false;
19139 else if (!at_namespace_scope_p ())
19140 finish_local_using_decl (decl, qscope, identifier);
19141 else
19142 finish_namespace_using_decl (decl, qscope, identifier);
19146 if (!access_declaration_p
19147 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19149 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19150 if (cxx_dialect < cxx17)
19151 pedwarn (comma->location, 0,
19152 "comma-separated list in using-declaration only available "
19153 "with -std=c++17 or -std=gnu++17");
19154 goto again;
19157 /* Look for the final `;'. */
19158 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19160 if (access_declaration_p && errorcount == oldcount)
19161 warning_at (diag_token->location, OPT_Wdeprecated,
19162 "access declarations are deprecated "
19163 "in favour of using-declarations; "
19164 "suggestion: add the %<using%> keyword");
19166 return true;
19169 /* Parse an alias-declaration.
19171 alias-declaration:
19172 using identifier attribute-specifier-seq [opt] = type-id */
19174 static tree
19175 cp_parser_alias_declaration (cp_parser* parser)
19177 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19178 location_t id_location, type_location;
19179 cp_declarator *declarator;
19180 cp_decl_specifier_seq decl_specs;
19181 bool member_p;
19182 const char *saved_message = NULL;
19184 /* Look for the `using' keyword. */
19185 cp_token *using_token
19186 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19187 if (using_token == NULL)
19188 return error_mark_node;
19190 id_location = cp_lexer_peek_token (parser->lexer)->location;
19191 id = cp_parser_identifier (parser);
19192 if (id == error_mark_node)
19193 return error_mark_node;
19195 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19196 attributes = cp_parser_attributes_opt (parser);
19197 if (attributes == error_mark_node)
19198 return error_mark_node;
19200 cp_parser_require (parser, CPP_EQ, RT_EQ);
19202 if (cp_parser_error_occurred (parser))
19203 return error_mark_node;
19205 cp_parser_commit_to_tentative_parse (parser);
19207 /* Now we are going to parse the type-id of the declaration. */
19210 [dcl.type]/3 says:
19212 "A type-specifier-seq shall not define a class or enumeration
19213 unless it appears in the type-id of an alias-declaration (7.1.3) that
19214 is not the declaration of a template-declaration."
19216 In other words, if we currently are in an alias template, the
19217 type-id should not define a type.
19219 So let's set parser->type_definition_forbidden_message in that
19220 case; cp_parser_check_type_definition (called by
19221 cp_parser_class_specifier) will then emit an error if a type is
19222 defined in the type-id. */
19223 if (parser->num_template_parameter_lists)
19225 saved_message = parser->type_definition_forbidden_message;
19226 parser->type_definition_forbidden_message =
19227 G_("types may not be defined in alias template declarations");
19230 type = cp_parser_type_id (parser, &type_location);
19232 /* Restore the error message if need be. */
19233 if (parser->num_template_parameter_lists)
19234 parser->type_definition_forbidden_message = saved_message;
19236 if (type == error_mark_node
19237 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19239 cp_parser_skip_to_end_of_block_or_statement (parser);
19240 return error_mark_node;
19243 /* A typedef-name can also be introduced by an alias-declaration. The
19244 identifier following the using keyword becomes a typedef-name. It has
19245 the same semantics as if it were introduced by the typedef
19246 specifier. In particular, it does not define a new type and it shall
19247 not appear in the type-id. */
19249 clear_decl_specs (&decl_specs);
19250 decl_specs.type = type;
19251 if (attributes != NULL_TREE)
19253 decl_specs.attributes = attributes;
19254 set_and_check_decl_spec_loc (&decl_specs,
19255 ds_attribute,
19256 attrs_token);
19258 set_and_check_decl_spec_loc (&decl_specs,
19259 ds_typedef,
19260 using_token);
19261 set_and_check_decl_spec_loc (&decl_specs,
19262 ds_alias,
19263 using_token);
19264 decl_specs.locations[ds_type_spec] = type_location;
19266 if (parser->num_template_parameter_lists
19267 && !cp_parser_check_template_parameters (parser,
19268 /*num_templates=*/0,
19269 /*template_id*/false,
19270 id_location,
19271 /*declarator=*/NULL))
19272 return error_mark_node;
19274 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19275 declarator->id_loc = id_location;
19277 member_p = at_class_scope_p ();
19278 if (member_p)
19279 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19280 NULL_TREE, attributes);
19281 else
19282 decl = start_decl (declarator, &decl_specs, 0,
19283 attributes, NULL_TREE, &pushed_scope);
19284 if (decl == error_mark_node)
19285 return decl;
19287 // Attach constraints to the alias declaration.
19288 if (flag_concepts && current_template_parms)
19290 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19291 tree constr = build_constraints (reqs, NULL_TREE);
19292 set_constraints (decl, constr);
19295 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19297 if (pushed_scope)
19298 pop_scope (pushed_scope);
19300 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19301 added into the symbol table; otherwise, return the TYPE_DECL. */
19302 if (DECL_LANG_SPECIFIC (decl)
19303 && DECL_TEMPLATE_INFO (decl)
19304 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19306 decl = DECL_TI_TEMPLATE (decl);
19307 if (member_p)
19308 check_member_template (decl);
19311 return decl;
19314 /* Parse a using-directive.
19316 using-directive:
19317 using namespace :: [opt] nested-name-specifier [opt]
19318 namespace-name ; */
19320 static void
19321 cp_parser_using_directive (cp_parser* parser)
19323 tree namespace_decl;
19324 tree attribs;
19326 /* Look for the `using' keyword. */
19327 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19328 /* And the `namespace' keyword. */
19329 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19330 /* Look for the optional `::' operator. */
19331 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19332 /* And the optional nested-name-specifier. */
19333 cp_parser_nested_name_specifier_opt (parser,
19334 /*typename_keyword_p=*/false,
19335 /*check_dependency_p=*/true,
19336 /*type_p=*/false,
19337 /*is_declaration=*/true);
19338 /* Get the namespace being used. */
19339 namespace_decl = cp_parser_namespace_name (parser);
19340 /* And any specified attributes. */
19341 attribs = cp_parser_attributes_opt (parser);
19343 /* Update the symbol table. */
19344 if (namespace_bindings_p ())
19345 finish_namespace_using_directive (namespace_decl, attribs);
19346 else
19347 finish_local_using_directive (namespace_decl, attribs);
19349 /* Look for the final `;'. */
19350 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19353 /* Parse an asm-definition.
19355 asm-definition:
19356 asm ( string-literal ) ;
19358 GNU Extension:
19360 asm-definition:
19361 asm volatile [opt] ( string-literal ) ;
19362 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19363 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19364 : asm-operand-list [opt] ) ;
19365 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19366 : asm-operand-list [opt]
19367 : asm-clobber-list [opt] ) ;
19368 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19369 : asm-clobber-list [opt]
19370 : asm-goto-list ) ; */
19372 static void
19373 cp_parser_asm_definition (cp_parser* parser)
19375 tree string;
19376 tree outputs = NULL_TREE;
19377 tree inputs = NULL_TREE;
19378 tree clobbers = NULL_TREE;
19379 tree labels = NULL_TREE;
19380 tree asm_stmt;
19381 bool volatile_p = false;
19382 bool extended_p = false;
19383 bool invalid_inputs_p = false;
19384 bool invalid_outputs_p = false;
19385 bool goto_p = false;
19386 required_token missing = RT_NONE;
19388 /* Look for the `asm' keyword. */
19389 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19391 if (parser->in_function_body
19392 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19394 error ("%<asm%> in %<constexpr%> function");
19395 cp_function_chain->invalid_constexpr = true;
19398 /* See if the next token is `volatile'. */
19399 if (cp_parser_allow_gnu_extensions_p (parser)
19400 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19402 /* Remember that we saw the `volatile' keyword. */
19403 volatile_p = true;
19404 /* Consume the token. */
19405 cp_lexer_consume_token (parser->lexer);
19407 if (cp_parser_allow_gnu_extensions_p (parser)
19408 && parser->in_function_body
19409 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19411 /* Remember that we saw the `goto' keyword. */
19412 goto_p = true;
19413 /* Consume the token. */
19414 cp_lexer_consume_token (parser->lexer);
19416 /* Look for the opening `('. */
19417 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19418 return;
19419 /* Look for the string. */
19420 string = cp_parser_string_literal (parser, false, false);
19421 if (string == error_mark_node)
19423 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19424 /*consume_paren=*/true);
19425 return;
19428 /* If we're allowing GNU extensions, check for the extended assembly
19429 syntax. Unfortunately, the `:' tokens need not be separated by
19430 a space in C, and so, for compatibility, we tolerate that here
19431 too. Doing that means that we have to treat the `::' operator as
19432 two `:' tokens. */
19433 if (cp_parser_allow_gnu_extensions_p (parser)
19434 && parser->in_function_body
19435 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19436 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19438 bool inputs_p = false;
19439 bool clobbers_p = false;
19440 bool labels_p = false;
19442 /* The extended syntax was used. */
19443 extended_p = true;
19445 /* Look for outputs. */
19446 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19448 /* Consume the `:'. */
19449 cp_lexer_consume_token (parser->lexer);
19450 /* Parse the output-operands. */
19451 if (cp_lexer_next_token_is_not (parser->lexer,
19452 CPP_COLON)
19453 && cp_lexer_next_token_is_not (parser->lexer,
19454 CPP_SCOPE)
19455 && cp_lexer_next_token_is_not (parser->lexer,
19456 CPP_CLOSE_PAREN)
19457 && !goto_p)
19459 outputs = cp_parser_asm_operand_list (parser);
19460 if (outputs == error_mark_node)
19461 invalid_outputs_p = true;
19464 /* If the next token is `::', there are no outputs, and the
19465 next token is the beginning of the inputs. */
19466 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19467 /* The inputs are coming next. */
19468 inputs_p = true;
19470 /* Look for inputs. */
19471 if (inputs_p
19472 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19474 /* Consume the `:' or `::'. */
19475 cp_lexer_consume_token (parser->lexer);
19476 /* Parse the output-operands. */
19477 if (cp_lexer_next_token_is_not (parser->lexer,
19478 CPP_COLON)
19479 && cp_lexer_next_token_is_not (parser->lexer,
19480 CPP_SCOPE)
19481 && cp_lexer_next_token_is_not (parser->lexer,
19482 CPP_CLOSE_PAREN))
19484 inputs = cp_parser_asm_operand_list (parser);
19485 if (inputs == error_mark_node)
19486 invalid_inputs_p = true;
19489 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19490 /* The clobbers are coming next. */
19491 clobbers_p = true;
19493 /* Look for clobbers. */
19494 if (clobbers_p
19495 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19497 clobbers_p = true;
19498 /* Consume the `:' or `::'. */
19499 cp_lexer_consume_token (parser->lexer);
19500 /* Parse the clobbers. */
19501 if (cp_lexer_next_token_is_not (parser->lexer,
19502 CPP_COLON)
19503 && cp_lexer_next_token_is_not (parser->lexer,
19504 CPP_CLOSE_PAREN))
19505 clobbers = cp_parser_asm_clobber_list (parser);
19507 else if (goto_p
19508 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19509 /* The labels are coming next. */
19510 labels_p = true;
19512 /* Look for labels. */
19513 if (labels_p
19514 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19516 labels_p = true;
19517 /* Consume the `:' or `::'. */
19518 cp_lexer_consume_token (parser->lexer);
19519 /* Parse the labels. */
19520 labels = cp_parser_asm_label_list (parser);
19523 if (goto_p && !labels_p)
19524 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19526 else if (goto_p)
19527 missing = RT_COLON_SCOPE;
19529 /* Look for the closing `)'. */
19530 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19531 missing ? missing : RT_CLOSE_PAREN))
19532 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19533 /*consume_paren=*/true);
19534 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19536 if (!invalid_inputs_p && !invalid_outputs_p)
19538 /* Create the ASM_EXPR. */
19539 if (parser->in_function_body)
19541 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19542 inputs, clobbers, labels);
19543 /* If the extended syntax was not used, mark the ASM_EXPR. */
19544 if (!extended_p)
19546 tree temp = asm_stmt;
19547 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19548 temp = TREE_OPERAND (temp, 0);
19550 ASM_INPUT_P (temp) = 1;
19553 else
19554 symtab->finalize_toplevel_asm (string);
19558 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19559 type that comes from the decl-specifier-seq. */
19561 static tree
19562 strip_declarator_types (tree type, cp_declarator *declarator)
19564 for (cp_declarator *d = declarator; d;)
19565 switch (d->kind)
19567 case cdk_id:
19568 case cdk_decomp:
19569 case cdk_error:
19570 d = NULL;
19571 break;
19573 default:
19574 if (TYPE_PTRMEMFUNC_P (type))
19575 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19576 type = TREE_TYPE (type);
19577 d = d->declarator;
19578 break;
19581 return type;
19584 /* Declarators [gram.dcl.decl] */
19586 /* Parse an init-declarator.
19588 init-declarator:
19589 declarator initializer [opt]
19591 GNU Extension:
19593 init-declarator:
19594 declarator asm-specification [opt] attributes [opt] initializer [opt]
19596 function-definition:
19597 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19598 function-body
19599 decl-specifier-seq [opt] declarator function-try-block
19601 GNU Extension:
19603 function-definition:
19604 __extension__ function-definition
19606 TM Extension:
19608 function-definition:
19609 decl-specifier-seq [opt] declarator function-transaction-block
19611 The DECL_SPECIFIERS apply to this declarator. Returns a
19612 representation of the entity declared. If MEMBER_P is TRUE, then
19613 this declarator appears in a class scope. The new DECL created by
19614 this declarator is returned.
19616 The CHECKS are access checks that should be performed once we know
19617 what entity is being declared (and, therefore, what classes have
19618 befriended it).
19620 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19621 for a function-definition here as well. If the declarator is a
19622 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19623 be TRUE upon return. By that point, the function-definition will
19624 have been completely parsed.
19626 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19627 is FALSE.
19629 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19630 parsed declaration if it is an uninitialized single declarator not followed
19631 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19632 if present, will not be consumed. If returned, this declarator will be
19633 created with SD_INITIALIZED but will not call cp_finish_decl.
19635 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19636 and there is an initializer, the pointed location_t is set to the
19637 location of the '=' or `(', or '{' in C++11 token introducing the
19638 initializer. */
19640 static tree
19641 cp_parser_init_declarator (cp_parser* parser,
19642 cp_decl_specifier_seq *decl_specifiers,
19643 vec<deferred_access_check, va_gc> *checks,
19644 bool function_definition_allowed_p,
19645 bool member_p,
19646 int declares_class_or_enum,
19647 bool* function_definition_p,
19648 tree* maybe_range_for_decl,
19649 location_t* init_loc,
19650 tree* auto_result)
19652 cp_token *token = NULL, *asm_spec_start_token = NULL,
19653 *attributes_start_token = NULL;
19654 cp_declarator *declarator;
19655 tree prefix_attributes;
19656 tree attributes = NULL;
19657 tree asm_specification;
19658 tree initializer;
19659 tree decl = NULL_TREE;
19660 tree scope;
19661 int is_initialized;
19662 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19663 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19664 "(...)". */
19665 enum cpp_ttype initialization_kind;
19666 bool is_direct_init = false;
19667 bool is_non_constant_init;
19668 int ctor_dtor_or_conv_p;
19669 bool friend_p = cp_parser_friend_p (decl_specifiers);
19670 tree pushed_scope = NULL_TREE;
19671 bool range_for_decl_p = false;
19672 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19673 location_t tmp_init_loc = UNKNOWN_LOCATION;
19675 /* Gather the attributes that were provided with the
19676 decl-specifiers. */
19677 prefix_attributes = decl_specifiers->attributes;
19679 /* Assume that this is not the declarator for a function
19680 definition. */
19681 if (function_definition_p)
19682 *function_definition_p = false;
19684 /* Default arguments are only permitted for function parameters. */
19685 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19686 parser->default_arg_ok_p = false;
19688 /* Defer access checks while parsing the declarator; we cannot know
19689 what names are accessible until we know what is being
19690 declared. */
19691 resume_deferring_access_checks ();
19693 token = cp_lexer_peek_token (parser->lexer);
19695 /* Parse the declarator. */
19696 declarator
19697 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19698 &ctor_dtor_or_conv_p,
19699 /*parenthesized_p=*/NULL,
19700 member_p, friend_p);
19701 /* Gather up the deferred checks. */
19702 stop_deferring_access_checks ();
19704 parser->default_arg_ok_p = saved_default_arg_ok_p;
19706 /* If the DECLARATOR was erroneous, there's no need to go
19707 further. */
19708 if (declarator == cp_error_declarator)
19709 return error_mark_node;
19711 /* Check that the number of template-parameter-lists is OK. */
19712 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19713 token->location))
19714 return error_mark_node;
19716 if (declares_class_or_enum & 2)
19717 cp_parser_check_for_definition_in_return_type (declarator,
19718 decl_specifiers->type,
19719 decl_specifiers->locations[ds_type_spec]);
19721 /* Figure out what scope the entity declared by the DECLARATOR is
19722 located in. `grokdeclarator' sometimes changes the scope, so
19723 we compute it now. */
19724 scope = get_scope_of_declarator (declarator);
19726 /* Perform any lookups in the declared type which were thought to be
19727 dependent, but are not in the scope of the declarator. */
19728 decl_specifiers->type
19729 = maybe_update_decl_type (decl_specifiers->type, scope);
19731 /* If we're allowing GNU extensions, look for an
19732 asm-specification. */
19733 if (cp_parser_allow_gnu_extensions_p (parser))
19735 /* Look for an asm-specification. */
19736 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19737 asm_specification = cp_parser_asm_specification_opt (parser);
19739 else
19740 asm_specification = NULL_TREE;
19742 /* Look for attributes. */
19743 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19744 attributes = cp_parser_attributes_opt (parser);
19746 /* Peek at the next token. */
19747 token = cp_lexer_peek_token (parser->lexer);
19749 bool bogus_implicit_tmpl = false;
19751 if (function_declarator_p (declarator))
19753 /* Handle C++17 deduction guides. */
19754 if (!decl_specifiers->type
19755 && ctor_dtor_or_conv_p <= 0
19756 && cxx_dialect >= cxx17)
19758 cp_declarator *id = get_id_declarator (declarator);
19759 tree name = id->u.id.unqualified_name;
19760 parser->scope = id->u.id.qualifying_scope;
19761 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19762 if (tmpl
19763 && (DECL_CLASS_TEMPLATE_P (tmpl)
19764 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19766 id->u.id.unqualified_name = dguide_name (tmpl);
19767 id->u.id.sfk = sfk_deduction_guide;
19768 ctor_dtor_or_conv_p = 1;
19772 /* Check to see if the token indicates the start of a
19773 function-definition. */
19774 if (cp_parser_token_starts_function_definition_p (token))
19776 if (!function_definition_allowed_p)
19778 /* If a function-definition should not appear here, issue an
19779 error message. */
19780 cp_parser_error (parser,
19781 "a function-definition is not allowed here");
19782 return error_mark_node;
19785 location_t func_brace_location
19786 = cp_lexer_peek_token (parser->lexer)->location;
19788 /* Neither attributes nor an asm-specification are allowed
19789 on a function-definition. */
19790 if (asm_specification)
19791 error_at (asm_spec_start_token->location,
19792 "an asm-specification is not allowed "
19793 "on a function-definition");
19794 if (attributes)
19795 error_at (attributes_start_token->location,
19796 "attributes are not allowed "
19797 "on a function-definition");
19798 /* This is a function-definition. */
19799 *function_definition_p = true;
19801 /* Parse the function definition. */
19802 if (member_p)
19803 decl = cp_parser_save_member_function_body (parser,
19804 decl_specifiers,
19805 declarator,
19806 prefix_attributes);
19807 else
19808 decl =
19809 (cp_parser_function_definition_from_specifiers_and_declarator
19810 (parser, decl_specifiers, prefix_attributes, declarator));
19812 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19814 /* This is where the prologue starts... */
19815 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19816 = func_brace_location;
19819 return decl;
19822 else if (parser->fully_implicit_function_template_p)
19824 /* A non-template declaration involving a function parameter list
19825 containing an implicit template parameter will be made into a
19826 template. If the resulting declaration is not going to be an
19827 actual function then finish the template scope here to prevent it.
19828 An error message will be issued once we have a decl to talk about.
19830 FIXME probably we should do type deduction rather than create an
19831 implicit template, but the standard currently doesn't allow it. */
19832 bogus_implicit_tmpl = true;
19833 finish_fully_implicit_template (parser, NULL_TREE);
19836 /* [dcl.dcl]
19838 Only in function declarations for constructors, destructors, type
19839 conversions, and deduction guides can the decl-specifier-seq be omitted.
19841 We explicitly postpone this check past the point where we handle
19842 function-definitions because we tolerate function-definitions
19843 that are missing their return types in some modes. */
19844 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19846 cp_parser_error (parser,
19847 "expected constructor, destructor, or type conversion");
19848 return error_mark_node;
19851 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19852 if (token->type == CPP_EQ
19853 || token->type == CPP_OPEN_PAREN
19854 || token->type == CPP_OPEN_BRACE)
19856 is_initialized = SD_INITIALIZED;
19857 initialization_kind = token->type;
19858 if (maybe_range_for_decl)
19859 *maybe_range_for_decl = error_mark_node;
19860 tmp_init_loc = token->location;
19861 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19862 *init_loc = tmp_init_loc;
19864 if (token->type == CPP_EQ
19865 && function_declarator_p (declarator))
19867 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19868 if (t2->keyword == RID_DEFAULT)
19869 is_initialized = SD_DEFAULTED;
19870 else if (t2->keyword == RID_DELETE)
19871 is_initialized = SD_DELETED;
19874 else
19876 /* If the init-declarator isn't initialized and isn't followed by a
19877 `,' or `;', it's not a valid init-declarator. */
19878 if (token->type != CPP_COMMA
19879 && token->type != CPP_SEMICOLON)
19881 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19882 range_for_decl_p = true;
19883 else
19885 if (!maybe_range_for_decl)
19886 cp_parser_error (parser, "expected initializer");
19887 return error_mark_node;
19890 is_initialized = SD_UNINITIALIZED;
19891 initialization_kind = CPP_EOF;
19894 /* Because start_decl has side-effects, we should only call it if we
19895 know we're going ahead. By this point, we know that we cannot
19896 possibly be looking at any other construct. */
19897 cp_parser_commit_to_tentative_parse (parser);
19899 /* Enter the newly declared entry in the symbol table. If we're
19900 processing a declaration in a class-specifier, we wait until
19901 after processing the initializer. */
19902 if (!member_p)
19904 if (parser->in_unbraced_linkage_specification_p)
19905 decl_specifiers->storage_class = sc_extern;
19906 decl = start_decl (declarator, decl_specifiers,
19907 range_for_decl_p? SD_INITIALIZED : is_initialized,
19908 attributes, prefix_attributes, &pushed_scope);
19909 cp_finalize_omp_declare_simd (parser, decl);
19910 cp_finalize_oacc_routine (parser, decl, false);
19911 /* Adjust location of decl if declarator->id_loc is more appropriate:
19912 set, and decl wasn't merged with another decl, in which case its
19913 location would be different from input_location, and more accurate. */
19914 if (DECL_P (decl)
19915 && declarator->id_loc != UNKNOWN_LOCATION
19916 && DECL_SOURCE_LOCATION (decl) == input_location)
19917 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19919 else if (scope)
19920 /* Enter the SCOPE. That way unqualified names appearing in the
19921 initializer will be looked up in SCOPE. */
19922 pushed_scope = push_scope (scope);
19924 /* Perform deferred access control checks, now that we know in which
19925 SCOPE the declared entity resides. */
19926 if (!member_p && decl)
19928 tree saved_current_function_decl = NULL_TREE;
19930 /* If the entity being declared is a function, pretend that we
19931 are in its scope. If it is a `friend', it may have access to
19932 things that would not otherwise be accessible. */
19933 if (TREE_CODE (decl) == FUNCTION_DECL)
19935 saved_current_function_decl = current_function_decl;
19936 current_function_decl = decl;
19939 /* Perform access checks for template parameters. */
19940 cp_parser_perform_template_parameter_access_checks (checks);
19942 /* Perform the access control checks for the declarator and the
19943 decl-specifiers. */
19944 perform_deferred_access_checks (tf_warning_or_error);
19946 /* Restore the saved value. */
19947 if (TREE_CODE (decl) == FUNCTION_DECL)
19948 current_function_decl = saved_current_function_decl;
19951 /* Parse the initializer. */
19952 initializer = NULL_TREE;
19953 is_direct_init = false;
19954 is_non_constant_init = true;
19955 if (is_initialized)
19957 if (function_declarator_p (declarator))
19959 if (initialization_kind == CPP_EQ)
19960 initializer = cp_parser_pure_specifier (parser);
19961 else
19963 /* If the declaration was erroneous, we don't really
19964 know what the user intended, so just silently
19965 consume the initializer. */
19966 if (decl != error_mark_node)
19967 error_at (tmp_init_loc, "initializer provided for function");
19968 cp_parser_skip_to_closing_parenthesis (parser,
19969 /*recovering=*/true,
19970 /*or_comma=*/false,
19971 /*consume_paren=*/true);
19974 else
19976 /* We want to record the extra mangling scope for in-class
19977 initializers of class members and initializers of static data
19978 member templates. The former involves deferring
19979 parsing of the initializer until end of class as with default
19980 arguments. So right here we only handle the latter. */
19981 if (!member_p && processing_template_decl && decl != error_mark_node)
19982 start_lambda_scope (decl);
19983 initializer = cp_parser_initializer (parser,
19984 &is_direct_init,
19985 &is_non_constant_init);
19986 if (!member_p && processing_template_decl && decl != error_mark_node)
19987 finish_lambda_scope ();
19988 if (initializer == error_mark_node)
19989 cp_parser_skip_to_end_of_statement (parser);
19993 /* The old parser allows attributes to appear after a parenthesized
19994 initializer. Mark Mitchell proposed removing this functionality
19995 on the GCC mailing lists on 2002-08-13. This parser accepts the
19996 attributes -- but ignores them. Made a permerror in GCC 8. */
19997 if (cp_parser_allow_gnu_extensions_p (parser)
19998 && initialization_kind == CPP_OPEN_PAREN
19999 && cp_parser_attributes_opt (parser)
20000 && permerror (input_location,
20001 "attributes after parenthesized initializer ignored"))
20003 static bool hint;
20004 if (flag_permissive && !hint)
20006 hint = true;
20007 inform (input_location,
20008 "this flexibility is deprecated and will be removed");
20012 /* And now complain about a non-function implicit template. */
20013 if (bogus_implicit_tmpl && decl != error_mark_node)
20014 error_at (DECL_SOURCE_LOCATION (decl),
20015 "non-function %qD declared as implicit template", decl);
20017 /* For an in-class declaration, use `grokfield' to create the
20018 declaration. */
20019 if (member_p)
20021 if (pushed_scope)
20023 pop_scope (pushed_scope);
20024 pushed_scope = NULL_TREE;
20026 decl = grokfield (declarator, decl_specifiers,
20027 initializer, !is_non_constant_init,
20028 /*asmspec=*/NULL_TREE,
20029 attr_chainon (attributes, prefix_attributes));
20030 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20031 cp_parser_save_default_args (parser, decl);
20032 cp_finalize_omp_declare_simd (parser, decl);
20033 cp_finalize_oacc_routine (parser, decl, false);
20036 /* Finish processing the declaration. But, skip member
20037 declarations. */
20038 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20040 cp_finish_decl (decl,
20041 initializer, !is_non_constant_init,
20042 asm_specification,
20043 /* If the initializer is in parentheses, then this is
20044 a direct-initialization, which means that an
20045 `explicit' constructor is OK. Otherwise, an
20046 `explicit' constructor cannot be used. */
20047 ((is_direct_init || !is_initialized)
20048 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20050 else if ((cxx_dialect != cxx98) && friend_p
20051 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20052 /* Core issue #226 (C++0x only): A default template-argument
20053 shall not be specified in a friend class template
20054 declaration. */
20055 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20056 /*is_partial=*/false, /*is_friend_decl=*/1);
20058 if (!friend_p && pushed_scope)
20059 pop_scope (pushed_scope);
20061 if (function_declarator_p (declarator)
20062 && parser->fully_implicit_function_template_p)
20064 if (member_p)
20065 decl = finish_fully_implicit_template (parser, decl);
20066 else
20067 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20070 if (auto_result && is_initialized && decl_specifiers->type
20071 && type_uses_auto (decl_specifiers->type))
20072 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20074 return decl;
20077 /* Parse a declarator.
20079 declarator:
20080 direct-declarator
20081 ptr-operator declarator
20083 abstract-declarator:
20084 ptr-operator abstract-declarator [opt]
20085 direct-abstract-declarator
20087 GNU Extensions:
20089 declarator:
20090 attributes [opt] direct-declarator
20091 attributes [opt] ptr-operator declarator
20093 abstract-declarator:
20094 attributes [opt] ptr-operator abstract-declarator [opt]
20095 attributes [opt] direct-abstract-declarator
20097 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20098 detect constructors, destructors, deduction guides, or conversion operators.
20099 It is set to -1 if the declarator is a name, and +1 if it is a
20100 function. Otherwise it is set to zero. Usually you just want to
20101 test for >0, but internally the negative value is used.
20103 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20104 a decl-specifier-seq unless it declares a constructor, destructor,
20105 or conversion. It might seem that we could check this condition in
20106 semantic analysis, rather than parsing, but that makes it difficult
20107 to handle something like `f()'. We want to notice that there are
20108 no decl-specifiers, and therefore realize that this is an
20109 expression, not a declaration.)
20111 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20112 the declarator is a direct-declarator of the form "(...)".
20114 MEMBER_P is true iff this declarator is a member-declarator.
20116 FRIEND_P is true iff this declarator is a friend. */
20118 static cp_declarator *
20119 cp_parser_declarator (cp_parser* parser,
20120 cp_parser_declarator_kind dcl_kind,
20121 int* ctor_dtor_or_conv_p,
20122 bool* parenthesized_p,
20123 bool member_p, bool friend_p)
20125 cp_declarator *declarator;
20126 enum tree_code code;
20127 cp_cv_quals cv_quals;
20128 tree class_type;
20129 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20131 /* Assume this is not a constructor, destructor, or type-conversion
20132 operator. */
20133 if (ctor_dtor_or_conv_p)
20134 *ctor_dtor_or_conv_p = 0;
20136 if (cp_parser_allow_gnu_extensions_p (parser))
20137 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20139 /* Check for the ptr-operator production. */
20140 cp_parser_parse_tentatively (parser);
20141 /* Parse the ptr-operator. */
20142 code = cp_parser_ptr_operator (parser,
20143 &class_type,
20144 &cv_quals,
20145 &std_attributes);
20147 /* If that worked, then we have a ptr-operator. */
20148 if (cp_parser_parse_definitely (parser))
20150 /* If a ptr-operator was found, then this declarator was not
20151 parenthesized. */
20152 if (parenthesized_p)
20153 *parenthesized_p = true;
20154 /* The dependent declarator is optional if we are parsing an
20155 abstract-declarator. */
20156 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20157 cp_parser_parse_tentatively (parser);
20159 /* Parse the dependent declarator. */
20160 declarator = cp_parser_declarator (parser, dcl_kind,
20161 /*ctor_dtor_or_conv_p=*/NULL,
20162 /*parenthesized_p=*/NULL,
20163 /*member_p=*/false,
20164 friend_p);
20166 /* If we are parsing an abstract-declarator, we must handle the
20167 case where the dependent declarator is absent. */
20168 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20169 && !cp_parser_parse_definitely (parser))
20170 declarator = NULL;
20172 declarator = cp_parser_make_indirect_declarator
20173 (code, class_type, cv_quals, declarator, std_attributes);
20175 /* Everything else is a direct-declarator. */
20176 else
20178 if (parenthesized_p)
20179 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20180 CPP_OPEN_PAREN);
20181 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20182 ctor_dtor_or_conv_p,
20183 member_p, friend_p);
20186 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20187 declarator->attributes = gnu_attributes;
20188 return declarator;
20191 /* Parse a direct-declarator or direct-abstract-declarator.
20193 direct-declarator:
20194 declarator-id
20195 direct-declarator ( parameter-declaration-clause )
20196 cv-qualifier-seq [opt]
20197 ref-qualifier [opt]
20198 exception-specification [opt]
20199 direct-declarator [ constant-expression [opt] ]
20200 ( declarator )
20202 direct-abstract-declarator:
20203 direct-abstract-declarator [opt]
20204 ( parameter-declaration-clause )
20205 cv-qualifier-seq [opt]
20206 ref-qualifier [opt]
20207 exception-specification [opt]
20208 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20209 ( abstract-declarator )
20211 Returns a representation of the declarator. DCL_KIND is
20212 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20213 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20214 we are parsing a direct-declarator. It is
20215 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20216 of ambiguity we prefer an abstract declarator, as per
20217 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20218 as for cp_parser_declarator. */
20220 static cp_declarator *
20221 cp_parser_direct_declarator (cp_parser* parser,
20222 cp_parser_declarator_kind dcl_kind,
20223 int* ctor_dtor_or_conv_p,
20224 bool member_p, bool friend_p)
20226 cp_token *token;
20227 cp_declarator *declarator = NULL;
20228 tree scope = NULL_TREE;
20229 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20230 bool saved_in_declarator_p = parser->in_declarator_p;
20231 bool first = true;
20232 tree pushed_scope = NULL_TREE;
20233 cp_token *open_paren = NULL, *close_paren = NULL;
20235 while (true)
20237 /* Peek at the next token. */
20238 token = cp_lexer_peek_token (parser->lexer);
20239 if (token->type == CPP_OPEN_PAREN)
20241 /* This is either a parameter-declaration-clause, or a
20242 parenthesized declarator. When we know we are parsing a
20243 named declarator, it must be a parenthesized declarator
20244 if FIRST is true. For instance, `(int)' is a
20245 parameter-declaration-clause, with an omitted
20246 direct-abstract-declarator. But `((*))', is a
20247 parenthesized abstract declarator. Finally, when T is a
20248 template parameter `(T)' is a
20249 parameter-declaration-clause, and not a parenthesized
20250 named declarator.
20252 We first try and parse a parameter-declaration-clause,
20253 and then try a nested declarator (if FIRST is true).
20255 It is not an error for it not to be a
20256 parameter-declaration-clause, even when FIRST is
20257 false. Consider,
20259 int i (int);
20260 int i (3);
20262 The first is the declaration of a function while the
20263 second is the definition of a variable, including its
20264 initializer.
20266 Having seen only the parenthesis, we cannot know which of
20267 these two alternatives should be selected. Even more
20268 complex are examples like:
20270 int i (int (a));
20271 int i (int (3));
20273 The former is a function-declaration; the latter is a
20274 variable initialization.
20276 Thus again, we try a parameter-declaration-clause, and if
20277 that fails, we back out and return. */
20279 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20281 tree params;
20282 bool is_declarator = false;
20284 open_paren = NULL;
20286 /* In a member-declarator, the only valid interpretation
20287 of a parenthesis is the start of a
20288 parameter-declaration-clause. (It is invalid to
20289 initialize a static data member with a parenthesized
20290 initializer; only the "=" form of initialization is
20291 permitted.) */
20292 if (!member_p)
20293 cp_parser_parse_tentatively (parser);
20295 /* Consume the `('. */
20296 matching_parens parens;
20297 parens.consume_open (parser);
20298 if (first)
20300 /* If this is going to be an abstract declarator, we're
20301 in a declarator and we can't have default args. */
20302 parser->default_arg_ok_p = false;
20303 parser->in_declarator_p = true;
20306 begin_scope (sk_function_parms, NULL_TREE);
20308 /* Parse the parameter-declaration-clause. */
20309 params = cp_parser_parameter_declaration_clause (parser);
20311 /* Consume the `)'. */
20312 parens.require_close (parser);
20314 /* If all went well, parse the cv-qualifier-seq,
20315 ref-qualifier and the exception-specification. */
20316 if (member_p || cp_parser_parse_definitely (parser))
20318 cp_cv_quals cv_quals;
20319 cp_virt_specifiers virt_specifiers;
20320 cp_ref_qualifier ref_qual;
20321 tree exception_specification;
20322 tree late_return;
20323 tree attrs;
20324 bool memfn = (member_p || (pushed_scope
20325 && CLASS_TYPE_P (pushed_scope)));
20327 is_declarator = true;
20329 if (ctor_dtor_or_conv_p)
20330 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20331 first = false;
20333 /* Parse the cv-qualifier-seq. */
20334 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20335 /* Parse the ref-qualifier. */
20336 ref_qual = cp_parser_ref_qualifier_opt (parser);
20337 /* Parse the tx-qualifier. */
20338 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20339 /* And the exception-specification. */
20340 exception_specification
20341 = cp_parser_exception_specification_opt (parser);
20343 attrs = cp_parser_std_attribute_spec_seq (parser);
20345 /* In here, we handle cases where attribute is used after
20346 the function declaration. For example:
20347 void func (int x) __attribute__((vector(..))); */
20348 tree gnu_attrs = NULL_TREE;
20349 tree requires_clause = NULL_TREE;
20350 late_return = (cp_parser_late_return_type_opt
20351 (parser, declarator, requires_clause,
20352 memfn ? cv_quals : -1));
20354 /* Parse the virt-specifier-seq. */
20355 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20357 /* Create the function-declarator. */
20358 declarator = make_call_declarator (declarator,
20359 params,
20360 cv_quals,
20361 virt_specifiers,
20362 ref_qual,
20363 tx_qual,
20364 exception_specification,
20365 late_return,
20366 requires_clause);
20367 declarator->std_attributes = attrs;
20368 declarator->attributes = gnu_attrs;
20369 /* Any subsequent parameter lists are to do with
20370 return type, so are not those of the declared
20371 function. */
20372 parser->default_arg_ok_p = false;
20375 /* Remove the function parms from scope. */
20376 pop_bindings_and_leave_scope ();
20378 if (is_declarator)
20379 /* Repeat the main loop. */
20380 continue;
20383 /* If this is the first, we can try a parenthesized
20384 declarator. */
20385 if (first)
20387 bool saved_in_type_id_in_expr_p;
20389 parser->default_arg_ok_p = saved_default_arg_ok_p;
20390 parser->in_declarator_p = saved_in_declarator_p;
20392 open_paren = token;
20393 /* Consume the `('. */
20394 matching_parens parens;
20395 parens.consume_open (parser);
20396 /* Parse the nested declarator. */
20397 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20398 parser->in_type_id_in_expr_p = true;
20399 declarator
20400 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20401 /*parenthesized_p=*/NULL,
20402 member_p, friend_p);
20403 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20404 first = false;
20405 /* Expect a `)'. */
20406 close_paren = cp_lexer_peek_token (parser->lexer);
20407 if (!parens.require_close (parser))
20408 declarator = cp_error_declarator;
20409 if (declarator == cp_error_declarator)
20410 break;
20412 goto handle_declarator;
20414 /* Otherwise, we must be done. */
20415 else
20416 break;
20418 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20419 && token->type == CPP_OPEN_SQUARE
20420 && !cp_next_tokens_can_be_attribute_p (parser))
20422 /* Parse an array-declarator. */
20423 tree bounds, attrs;
20425 if (ctor_dtor_or_conv_p)
20426 *ctor_dtor_or_conv_p = 0;
20428 open_paren = NULL;
20429 first = false;
20430 parser->default_arg_ok_p = false;
20431 parser->in_declarator_p = true;
20432 /* Consume the `['. */
20433 cp_lexer_consume_token (parser->lexer);
20434 /* Peek at the next token. */
20435 token = cp_lexer_peek_token (parser->lexer);
20436 /* If the next token is `]', then there is no
20437 constant-expression. */
20438 if (token->type != CPP_CLOSE_SQUARE)
20440 bool non_constant_p;
20441 bounds
20442 = cp_parser_constant_expression (parser,
20443 /*allow_non_constant=*/true,
20444 &non_constant_p);
20445 if (!non_constant_p)
20446 /* OK */;
20447 else if (error_operand_p (bounds))
20448 /* Already gave an error. */;
20449 else if (!parser->in_function_body
20450 || current_binding_level->kind == sk_function_parms)
20452 /* Normally, the array bound must be an integral constant
20453 expression. However, as an extension, we allow VLAs
20454 in function scopes as long as they aren't part of a
20455 parameter declaration. */
20456 cp_parser_error (parser,
20457 "array bound is not an integer constant");
20458 bounds = error_mark_node;
20460 else if (processing_template_decl
20461 && !type_dependent_expression_p (bounds))
20463 /* Remember this wasn't a constant-expression. */
20464 bounds = build_nop (TREE_TYPE (bounds), bounds);
20465 TREE_SIDE_EFFECTS (bounds) = 1;
20468 else
20469 bounds = NULL_TREE;
20470 /* Look for the closing `]'. */
20471 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20473 declarator = cp_error_declarator;
20474 break;
20477 attrs = cp_parser_std_attribute_spec_seq (parser);
20478 declarator = make_array_declarator (declarator, bounds);
20479 declarator->std_attributes = attrs;
20481 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20484 tree qualifying_scope;
20485 tree unqualified_name;
20486 tree attrs;
20487 special_function_kind sfk;
20488 bool abstract_ok;
20489 bool pack_expansion_p = false;
20490 cp_token *declarator_id_start_token;
20492 /* Parse a declarator-id */
20493 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20494 if (abstract_ok)
20496 cp_parser_parse_tentatively (parser);
20498 /* If we see an ellipsis, we should be looking at a
20499 parameter pack. */
20500 if (token->type == CPP_ELLIPSIS)
20502 /* Consume the `...' */
20503 cp_lexer_consume_token (parser->lexer);
20505 pack_expansion_p = true;
20509 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20510 unqualified_name
20511 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20512 qualifying_scope = parser->scope;
20513 if (abstract_ok)
20515 bool okay = false;
20517 if (!unqualified_name && pack_expansion_p)
20519 /* Check whether an error occurred. */
20520 okay = !cp_parser_error_occurred (parser);
20522 /* We already consumed the ellipsis to mark a
20523 parameter pack, but we have no way to report it,
20524 so abort the tentative parse. We will be exiting
20525 immediately anyway. */
20526 cp_parser_abort_tentative_parse (parser);
20528 else
20529 okay = cp_parser_parse_definitely (parser);
20531 if (!okay)
20532 unqualified_name = error_mark_node;
20533 else if (unqualified_name
20534 && (qualifying_scope
20535 || (!identifier_p (unqualified_name))))
20537 cp_parser_error (parser, "expected unqualified-id");
20538 unqualified_name = error_mark_node;
20542 if (!unqualified_name)
20543 return NULL;
20544 if (unqualified_name == error_mark_node)
20546 declarator = cp_error_declarator;
20547 pack_expansion_p = false;
20548 declarator->parameter_pack_p = false;
20549 break;
20552 attrs = cp_parser_std_attribute_spec_seq (parser);
20554 if (qualifying_scope && at_namespace_scope_p ()
20555 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20557 /* In the declaration of a member of a template class
20558 outside of the class itself, the SCOPE will sometimes
20559 be a TYPENAME_TYPE. For example, given:
20561 template <typename T>
20562 int S<T>::R::i = 3;
20564 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20565 this context, we must resolve S<T>::R to an ordinary
20566 type, rather than a typename type.
20568 The reason we normally avoid resolving TYPENAME_TYPEs
20569 is that a specialization of `S' might render
20570 `S<T>::R' not a type. However, if `S' is
20571 specialized, then this `i' will not be used, so there
20572 is no harm in resolving the types here. */
20573 tree type;
20575 /* Resolve the TYPENAME_TYPE. */
20576 type = resolve_typename_type (qualifying_scope,
20577 /*only_current_p=*/false);
20578 /* If that failed, the declarator is invalid. */
20579 if (TREE_CODE (type) == TYPENAME_TYPE)
20581 if (typedef_variant_p (type))
20582 error_at (declarator_id_start_token->location,
20583 "cannot define member of dependent typedef "
20584 "%qT", type);
20585 else
20586 error_at (declarator_id_start_token->location,
20587 "%<%T::%E%> is not a type",
20588 TYPE_CONTEXT (qualifying_scope),
20589 TYPE_IDENTIFIER (qualifying_scope));
20591 qualifying_scope = type;
20594 sfk = sfk_none;
20596 if (unqualified_name)
20598 tree class_type;
20600 if (qualifying_scope
20601 && CLASS_TYPE_P (qualifying_scope))
20602 class_type = qualifying_scope;
20603 else
20604 class_type = current_class_type;
20606 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20608 tree name_type = TREE_TYPE (unqualified_name);
20610 if (!class_type || !same_type_p (name_type, class_type))
20612 /* We do not attempt to print the declarator
20613 here because we do not have enough
20614 information about its original syntactic
20615 form. */
20616 cp_parser_error (parser, "invalid declarator");
20617 declarator = cp_error_declarator;
20618 break;
20620 else if (qualifying_scope
20621 && CLASSTYPE_USE_TEMPLATE (name_type))
20623 error_at (declarator_id_start_token->location,
20624 "invalid use of constructor as a template");
20625 inform (declarator_id_start_token->location,
20626 "use %<%T::%D%> instead of %<%T::%D%> to "
20627 "name the constructor in a qualified name",
20628 class_type,
20629 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20630 class_type, name_type);
20631 declarator = cp_error_declarator;
20632 break;
20634 unqualified_name = constructor_name (class_type);
20637 if (class_type)
20639 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20640 sfk = sfk_destructor;
20641 else if (identifier_p (unqualified_name)
20642 && IDENTIFIER_CONV_OP_P (unqualified_name))
20643 sfk = sfk_conversion;
20644 else if (/* There's no way to declare a constructor
20645 for an unnamed type, even if the type
20646 got a name for linkage purposes. */
20647 !TYPE_WAS_UNNAMED (class_type)
20648 /* Handle correctly (c++/19200):
20650 struct S {
20651 struct T{};
20652 friend void S(T);
20655 and also:
20657 namespace N {
20658 void S();
20661 struct S {
20662 friend void N::S();
20663 }; */
20664 && (!friend_p || class_type == qualifying_scope)
20665 && constructor_name_p (unqualified_name,
20666 class_type))
20667 sfk = sfk_constructor;
20668 else if (is_overloaded_fn (unqualified_name)
20669 && DECL_CONSTRUCTOR_P (get_first_fn
20670 (unqualified_name)))
20671 sfk = sfk_constructor;
20673 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20674 *ctor_dtor_or_conv_p = -1;
20677 declarator = make_id_declarator (qualifying_scope,
20678 unqualified_name,
20679 sfk);
20680 declarator->std_attributes = attrs;
20681 declarator->id_loc = token->location;
20682 declarator->parameter_pack_p = pack_expansion_p;
20684 if (pack_expansion_p)
20685 maybe_warn_variadic_templates ();
20688 handle_declarator:;
20689 scope = get_scope_of_declarator (declarator);
20690 if (scope)
20692 /* Any names that appear after the declarator-id for a
20693 member are looked up in the containing scope. */
20694 if (at_function_scope_p ())
20696 /* But declarations with qualified-ids can't appear in a
20697 function. */
20698 cp_parser_error (parser, "qualified-id in declaration");
20699 declarator = cp_error_declarator;
20700 break;
20702 pushed_scope = push_scope (scope);
20704 parser->in_declarator_p = true;
20705 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20706 || (declarator && declarator->kind == cdk_id))
20707 /* Default args are only allowed on function
20708 declarations. */
20709 parser->default_arg_ok_p = saved_default_arg_ok_p;
20710 else
20711 parser->default_arg_ok_p = false;
20713 first = false;
20715 /* We're done. */
20716 else
20717 break;
20720 /* For an abstract declarator, we might wind up with nothing at this
20721 point. That's an error; the declarator is not optional. */
20722 if (!declarator)
20723 cp_parser_error (parser, "expected declarator");
20724 else if (open_paren)
20726 /* Record overly parenthesized declarator so we can give a
20727 diagnostic about confusing decl/expr disambiguation. */
20728 if (declarator->kind == cdk_array)
20730 /* If the open and close parens are on different lines, this
20731 is probably a formatting thing, so ignore. */
20732 expanded_location open = expand_location (open_paren->location);
20733 expanded_location close = expand_location (close_paren->location);
20734 if (open.line != close.line || open.file != close.file)
20735 open_paren = NULL;
20737 if (open_paren)
20738 declarator->parenthesized = open_paren->location;
20741 /* If we entered a scope, we must exit it now. */
20742 if (pushed_scope)
20743 pop_scope (pushed_scope);
20745 parser->default_arg_ok_p = saved_default_arg_ok_p;
20746 parser->in_declarator_p = saved_in_declarator_p;
20748 return declarator;
20751 /* Parse a ptr-operator.
20753 ptr-operator:
20754 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20755 * cv-qualifier-seq [opt]
20757 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20758 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20760 GNU Extension:
20762 ptr-operator:
20763 & cv-qualifier-seq [opt]
20765 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20766 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20767 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20768 filled in with the TYPE containing the member. *CV_QUALS is
20769 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20770 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20771 Note that the tree codes returned by this function have nothing
20772 to do with the types of trees that will be eventually be created
20773 to represent the pointer or reference type being parsed. They are
20774 just constants with suggestive names. */
20775 static enum tree_code
20776 cp_parser_ptr_operator (cp_parser* parser,
20777 tree* type,
20778 cp_cv_quals *cv_quals,
20779 tree *attributes)
20781 enum tree_code code = ERROR_MARK;
20782 cp_token *token;
20783 tree attrs = NULL_TREE;
20785 /* Assume that it's not a pointer-to-member. */
20786 *type = NULL_TREE;
20787 /* And that there are no cv-qualifiers. */
20788 *cv_quals = TYPE_UNQUALIFIED;
20790 /* Peek at the next token. */
20791 token = cp_lexer_peek_token (parser->lexer);
20793 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20794 if (token->type == CPP_MULT)
20795 code = INDIRECT_REF;
20796 else if (token->type == CPP_AND)
20797 code = ADDR_EXPR;
20798 else if ((cxx_dialect != cxx98) &&
20799 token->type == CPP_AND_AND) /* C++0x only */
20800 code = NON_LVALUE_EXPR;
20802 if (code != ERROR_MARK)
20804 /* Consume the `*', `&' or `&&'. */
20805 cp_lexer_consume_token (parser->lexer);
20807 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20808 `&', if we are allowing GNU extensions. (The only qualifier
20809 that can legally appear after `&' is `restrict', but that is
20810 enforced during semantic analysis. */
20811 if (code == INDIRECT_REF
20812 || cp_parser_allow_gnu_extensions_p (parser))
20813 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20815 attrs = cp_parser_std_attribute_spec_seq (parser);
20816 if (attributes != NULL)
20817 *attributes = attrs;
20819 else
20821 /* Try the pointer-to-member case. */
20822 cp_parser_parse_tentatively (parser);
20823 /* Look for the optional `::' operator. */
20824 cp_parser_global_scope_opt (parser,
20825 /*current_scope_valid_p=*/false);
20826 /* Look for the nested-name specifier. */
20827 token = cp_lexer_peek_token (parser->lexer);
20828 cp_parser_nested_name_specifier (parser,
20829 /*typename_keyword_p=*/false,
20830 /*check_dependency_p=*/true,
20831 /*type_p=*/false,
20832 /*is_declaration=*/false);
20833 /* If we found it, and the next token is a `*', then we are
20834 indeed looking at a pointer-to-member operator. */
20835 if (!cp_parser_error_occurred (parser)
20836 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20838 /* Indicate that the `*' operator was used. */
20839 code = INDIRECT_REF;
20841 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20842 error_at (token->location, "%qD is a namespace", parser->scope);
20843 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20844 error_at (token->location, "cannot form pointer to member of "
20845 "non-class %q#T", parser->scope);
20846 else
20848 /* The type of which the member is a member is given by the
20849 current SCOPE. */
20850 *type = parser->scope;
20851 /* The next name will not be qualified. */
20852 parser->scope = NULL_TREE;
20853 parser->qualifying_scope = NULL_TREE;
20854 parser->object_scope = NULL_TREE;
20855 /* Look for optional c++11 attributes. */
20856 attrs = cp_parser_std_attribute_spec_seq (parser);
20857 if (attributes != NULL)
20858 *attributes = attrs;
20859 /* Look for the optional cv-qualifier-seq. */
20860 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20863 /* If that didn't work we don't have a ptr-operator. */
20864 if (!cp_parser_parse_definitely (parser))
20865 cp_parser_error (parser, "expected ptr-operator");
20868 return code;
20871 /* Parse an (optional) cv-qualifier-seq.
20873 cv-qualifier-seq:
20874 cv-qualifier cv-qualifier-seq [opt]
20876 cv-qualifier:
20877 const
20878 volatile
20880 GNU Extension:
20882 cv-qualifier:
20883 __restrict__
20885 Returns a bitmask representing the cv-qualifiers. */
20887 static cp_cv_quals
20888 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20890 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20892 while (true)
20894 cp_token *token;
20895 cp_cv_quals cv_qualifier;
20897 /* Peek at the next token. */
20898 token = cp_lexer_peek_token (parser->lexer);
20899 /* See if it's a cv-qualifier. */
20900 switch (token->keyword)
20902 case RID_CONST:
20903 cv_qualifier = TYPE_QUAL_CONST;
20904 break;
20906 case RID_VOLATILE:
20907 cv_qualifier = TYPE_QUAL_VOLATILE;
20908 break;
20910 case RID_RESTRICT:
20911 cv_qualifier = TYPE_QUAL_RESTRICT;
20912 break;
20914 default:
20915 cv_qualifier = TYPE_UNQUALIFIED;
20916 break;
20919 if (!cv_qualifier)
20920 break;
20922 if (cv_quals & cv_qualifier)
20924 gcc_rich_location richloc (token->location);
20925 richloc.add_fixit_remove ();
20926 error_at (&richloc, "duplicate cv-qualifier");
20927 cp_lexer_purge_token (parser->lexer);
20929 else
20931 cp_lexer_consume_token (parser->lexer);
20932 cv_quals |= cv_qualifier;
20936 return cv_quals;
20939 /* Parse an (optional) ref-qualifier
20941 ref-qualifier:
20945 Returns cp_ref_qualifier representing ref-qualifier. */
20947 static cp_ref_qualifier
20948 cp_parser_ref_qualifier_opt (cp_parser* parser)
20950 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20952 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20953 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20954 return ref_qual;
20956 while (true)
20958 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20959 cp_token *token = cp_lexer_peek_token (parser->lexer);
20961 switch (token->type)
20963 case CPP_AND:
20964 curr_ref_qual = REF_QUAL_LVALUE;
20965 break;
20967 case CPP_AND_AND:
20968 curr_ref_qual = REF_QUAL_RVALUE;
20969 break;
20971 default:
20972 curr_ref_qual = REF_QUAL_NONE;
20973 break;
20976 if (!curr_ref_qual)
20977 break;
20978 else if (ref_qual)
20980 error_at (token->location, "multiple ref-qualifiers");
20981 cp_lexer_purge_token (parser->lexer);
20983 else
20985 ref_qual = curr_ref_qual;
20986 cp_lexer_consume_token (parser->lexer);
20990 return ref_qual;
20993 /* Parse an optional tx-qualifier.
20995 tx-qualifier:
20996 transaction_safe
20997 transaction_safe_dynamic */
20999 static tree
21000 cp_parser_tx_qualifier_opt (cp_parser *parser)
21002 cp_token *token = cp_lexer_peek_token (parser->lexer);
21003 if (token->type == CPP_NAME)
21005 tree name = token->u.value;
21006 const char *p = IDENTIFIER_POINTER (name);
21007 const int len = strlen ("transaction_safe");
21008 if (!strncmp (p, "transaction_safe", len))
21010 p += len;
21011 if (*p == '\0'
21012 || !strcmp (p, "_dynamic"))
21014 cp_lexer_consume_token (parser->lexer);
21015 if (!flag_tm)
21017 error ("%qE requires %<-fgnu-tm%>", name);
21018 return NULL_TREE;
21020 else
21021 return name;
21025 return NULL_TREE;
21028 /* Parse an (optional) virt-specifier-seq.
21030 virt-specifier-seq:
21031 virt-specifier virt-specifier-seq [opt]
21033 virt-specifier:
21034 override
21035 final
21037 Returns a bitmask representing the virt-specifiers. */
21039 static cp_virt_specifiers
21040 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21042 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21044 while (true)
21046 cp_token *token;
21047 cp_virt_specifiers virt_specifier;
21049 /* Peek at the next token. */
21050 token = cp_lexer_peek_token (parser->lexer);
21051 /* See if it's a virt-specifier-qualifier. */
21052 if (token->type != CPP_NAME)
21053 break;
21054 if (id_equal (token->u.value, "override"))
21056 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21057 virt_specifier = VIRT_SPEC_OVERRIDE;
21059 else if (id_equal (token->u.value, "final"))
21061 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21062 virt_specifier = VIRT_SPEC_FINAL;
21064 else if (id_equal (token->u.value, "__final"))
21066 virt_specifier = VIRT_SPEC_FINAL;
21068 else
21069 break;
21071 if (virt_specifiers & virt_specifier)
21073 gcc_rich_location richloc (token->location);
21074 richloc.add_fixit_remove ();
21075 error_at (&richloc, "duplicate virt-specifier");
21076 cp_lexer_purge_token (parser->lexer);
21078 else
21080 cp_lexer_consume_token (parser->lexer);
21081 virt_specifiers |= virt_specifier;
21084 return virt_specifiers;
21087 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21088 is in scope even though it isn't real. */
21090 void
21091 inject_this_parameter (tree ctype, cp_cv_quals quals)
21093 tree this_parm;
21095 if (current_class_ptr)
21097 /* We don't clear this between NSDMIs. Is it already what we want? */
21098 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21099 if (DECL_P (current_class_ptr)
21100 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21101 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21102 && cp_type_quals (type) == quals)
21103 return;
21106 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21107 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21108 current_class_ptr = NULL_TREE;
21109 current_class_ref
21110 = cp_build_fold_indirect_ref (this_parm);
21111 current_class_ptr = this_parm;
21114 /* Return true iff our current scope is a non-static data member
21115 initializer. */
21117 bool
21118 parsing_nsdmi (void)
21120 /* We recognize NSDMI context by the context-less 'this' pointer set up
21121 by the function above. */
21122 if (current_class_ptr
21123 && TREE_CODE (current_class_ptr) == PARM_DECL
21124 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21125 return true;
21126 return false;
21129 /* Parse a late-specified return type, if any. This is not a separate
21130 non-terminal, but part of a function declarator, which looks like
21132 -> trailing-type-specifier-seq abstract-declarator(opt)
21134 Returns the type indicated by the type-id.
21136 In addition to this, parse any queued up #pragma omp declare simd
21137 clauses, and #pragma acc routine clauses.
21139 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21140 function. */
21142 static tree
21143 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21144 tree& requires_clause, cp_cv_quals quals)
21146 cp_token *token;
21147 tree type = NULL_TREE;
21148 bool declare_simd_p = (parser->omp_declare_simd
21149 && declarator
21150 && declarator->kind == cdk_id);
21152 bool oacc_routine_p = (parser->oacc_routine
21153 && declarator
21154 && declarator->kind == cdk_id);
21156 /* Peek at the next token. */
21157 token = cp_lexer_peek_token (parser->lexer);
21158 /* A late-specified return type is indicated by an initial '->'. */
21159 if (token->type != CPP_DEREF
21160 && token->keyword != RID_REQUIRES
21161 && !(token->type == CPP_NAME
21162 && token->u.value == ridpointers[RID_REQUIRES])
21163 && !(declare_simd_p || oacc_routine_p))
21164 return NULL_TREE;
21166 tree save_ccp = current_class_ptr;
21167 tree save_ccr = current_class_ref;
21168 if (quals >= 0)
21170 /* DR 1207: 'this' is in scope in the trailing return type. */
21171 inject_this_parameter (current_class_type, quals);
21174 if (token->type == CPP_DEREF)
21176 /* Consume the ->. */
21177 cp_lexer_consume_token (parser->lexer);
21179 type = cp_parser_trailing_type_id (parser);
21182 /* Function declarations may be followed by a trailing
21183 requires-clause. */
21184 requires_clause = cp_parser_requires_clause_opt (parser);
21186 if (declare_simd_p)
21187 declarator->attributes
21188 = cp_parser_late_parsing_omp_declare_simd (parser,
21189 declarator->attributes);
21190 if (oacc_routine_p)
21191 declarator->attributes
21192 = cp_parser_late_parsing_oacc_routine (parser,
21193 declarator->attributes);
21195 if (quals >= 0)
21197 current_class_ptr = save_ccp;
21198 current_class_ref = save_ccr;
21201 return type;
21204 /* Parse a declarator-id.
21206 declarator-id:
21207 id-expression
21208 :: [opt] nested-name-specifier [opt] type-name
21210 In the `id-expression' case, the value returned is as for
21211 cp_parser_id_expression if the id-expression was an unqualified-id.
21212 If the id-expression was a qualified-id, then a SCOPE_REF is
21213 returned. The first operand is the scope (either a NAMESPACE_DECL
21214 or TREE_TYPE), but the second is still just a representation of an
21215 unqualified-id. */
21217 static tree
21218 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21220 tree id;
21221 /* The expression must be an id-expression. Assume that qualified
21222 names are the names of types so that:
21224 template <class T>
21225 int S<T>::R::i = 3;
21227 will work; we must treat `S<T>::R' as the name of a type.
21228 Similarly, assume that qualified names are templates, where
21229 required, so that:
21231 template <class T>
21232 int S<T>::R<T>::i = 3;
21234 will work, too. */
21235 id = cp_parser_id_expression (parser,
21236 /*template_keyword_p=*/false,
21237 /*check_dependency_p=*/false,
21238 /*template_p=*/NULL,
21239 /*declarator_p=*/true,
21240 optional_p);
21241 if (id && BASELINK_P (id))
21242 id = BASELINK_FUNCTIONS (id);
21243 return id;
21246 /* Parse a type-id.
21248 type-id:
21249 type-specifier-seq abstract-declarator [opt]
21251 Returns the TYPE specified. */
21253 static tree
21254 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21255 bool is_trailing_return, location_t * type_location)
21257 cp_decl_specifier_seq type_specifier_seq;
21258 cp_declarator *abstract_declarator;
21260 /* Parse the type-specifier-seq. */
21261 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21262 is_trailing_return,
21263 &type_specifier_seq);
21264 if (type_location)
21265 *type_location = type_specifier_seq.locations[ds_type_spec];
21267 if (is_template_arg && type_specifier_seq.type
21268 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21269 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21270 /* A bare template name as a template argument is a template template
21271 argument, not a placeholder, so fail parsing it as a type argument. */
21273 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21274 cp_parser_simulate_error (parser);
21275 return error_mark_node;
21277 if (type_specifier_seq.type == error_mark_node)
21278 return error_mark_node;
21280 /* There might or might not be an abstract declarator. */
21281 cp_parser_parse_tentatively (parser);
21282 /* Look for the declarator. */
21283 abstract_declarator
21284 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21285 /*parenthesized_p=*/NULL,
21286 /*member_p=*/false,
21287 /*friend_p=*/false);
21288 /* Check to see if there really was a declarator. */
21289 if (!cp_parser_parse_definitely (parser))
21290 abstract_declarator = NULL;
21292 if (type_specifier_seq.type
21293 /* The concepts TS allows 'auto' as a type-id. */
21294 && (!flag_concepts || parser->in_type_id_in_expr_p)
21295 /* None of the valid uses of 'auto' in C++14 involve the type-id
21296 nonterminal, but it is valid in a trailing-return-type. */
21297 && !(cxx_dialect >= cxx14 && is_trailing_return))
21298 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21300 /* A type-id with type 'auto' is only ok if the abstract declarator
21301 is a function declarator with a late-specified return type.
21303 A type-id with 'auto' is also valid in a trailing-return-type
21304 in a compound-requirement. */
21305 if (abstract_declarator
21306 && abstract_declarator->kind == cdk_function
21307 && abstract_declarator->u.function.late_return_type)
21308 /* OK */;
21309 else if (parser->in_result_type_constraint_p)
21310 /* OK */;
21311 else
21313 location_t loc = type_specifier_seq.locations[ds_type_spec];
21314 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21316 error_at (loc, "missing template arguments after %qT",
21317 auto_node);
21318 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21319 tmpl);
21321 else
21322 error_at (loc, "invalid use of %qT", auto_node);
21323 return error_mark_node;
21327 return groktypename (&type_specifier_seq, abstract_declarator,
21328 is_template_arg);
21331 static tree
21332 cp_parser_type_id (cp_parser *parser, location_t * type_location)
21334 return cp_parser_type_id_1 (parser, false, false, type_location);
21337 static tree
21338 cp_parser_template_type_arg (cp_parser *parser)
21340 tree r;
21341 const char *saved_message = parser->type_definition_forbidden_message;
21342 parser->type_definition_forbidden_message
21343 = G_("types may not be defined in template arguments");
21344 r = cp_parser_type_id_1 (parser, true, false, NULL);
21345 parser->type_definition_forbidden_message = saved_message;
21346 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21348 error ("invalid use of %<auto%> in template argument");
21349 r = error_mark_node;
21351 return r;
21354 static tree
21355 cp_parser_trailing_type_id (cp_parser *parser)
21357 return cp_parser_type_id_1 (parser, false, true, NULL);
21360 /* Parse a type-specifier-seq.
21362 type-specifier-seq:
21363 type-specifier type-specifier-seq [opt]
21365 GNU extension:
21367 type-specifier-seq:
21368 attributes type-specifier-seq [opt]
21370 If IS_DECLARATION is true, we are at the start of a "condition" or
21371 exception-declaration, so we might be followed by a declarator-id.
21373 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21374 i.e. we've just seen "->".
21376 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21378 static void
21379 cp_parser_type_specifier_seq (cp_parser* parser,
21380 bool is_declaration,
21381 bool is_trailing_return,
21382 cp_decl_specifier_seq *type_specifier_seq)
21384 bool seen_type_specifier = false;
21385 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21386 cp_token *start_token = NULL;
21388 /* Clear the TYPE_SPECIFIER_SEQ. */
21389 clear_decl_specs (type_specifier_seq);
21391 /* In the context of a trailing return type, enum E { } is an
21392 elaborated-type-specifier followed by a function-body, not an
21393 enum-specifier. */
21394 if (is_trailing_return)
21395 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21397 /* Parse the type-specifiers and attributes. */
21398 while (true)
21400 tree type_specifier;
21401 bool is_cv_qualifier;
21403 /* Check for attributes first. */
21404 if (cp_next_tokens_can_be_attribute_p (parser))
21406 type_specifier_seq->attributes
21407 = attr_chainon (type_specifier_seq->attributes,
21408 cp_parser_attributes_opt (parser));
21409 continue;
21412 /* record the token of the beginning of the type specifier seq,
21413 for error reporting purposes*/
21414 if (!start_token)
21415 start_token = cp_lexer_peek_token (parser->lexer);
21417 /* Look for the type-specifier. */
21418 type_specifier = cp_parser_type_specifier (parser,
21419 flags,
21420 type_specifier_seq,
21421 /*is_declaration=*/false,
21422 NULL,
21423 &is_cv_qualifier);
21424 if (!type_specifier)
21426 /* If the first type-specifier could not be found, this is not a
21427 type-specifier-seq at all. */
21428 if (!seen_type_specifier)
21430 /* Set in_declarator_p to avoid skipping to the semicolon. */
21431 int in_decl = parser->in_declarator_p;
21432 parser->in_declarator_p = true;
21434 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21435 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21436 cp_parser_error (parser, "expected type-specifier");
21438 parser->in_declarator_p = in_decl;
21440 type_specifier_seq->type = error_mark_node;
21441 return;
21443 /* If subsequent type-specifiers could not be found, the
21444 type-specifier-seq is complete. */
21445 break;
21448 seen_type_specifier = true;
21449 /* The standard says that a condition can be:
21451 type-specifier-seq declarator = assignment-expression
21453 However, given:
21455 struct S {};
21456 if (int S = ...)
21458 we should treat the "S" as a declarator, not as a
21459 type-specifier. The standard doesn't say that explicitly for
21460 type-specifier-seq, but it does say that for
21461 decl-specifier-seq in an ordinary declaration. Perhaps it
21462 would be clearer just to allow a decl-specifier-seq here, and
21463 then add a semantic restriction that if any decl-specifiers
21464 that are not type-specifiers appear, the program is invalid. */
21465 if (is_declaration && !is_cv_qualifier)
21466 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21470 /* Return whether the function currently being declared has an associated
21471 template parameter list. */
21473 static bool
21474 function_being_declared_is_template_p (cp_parser* parser)
21476 if (!current_template_parms || processing_template_parmlist)
21477 return false;
21479 if (parser->implicit_template_scope)
21480 return true;
21482 if (at_class_scope_p ()
21483 && TYPE_BEING_DEFINED (current_class_type))
21484 return parser->num_template_parameter_lists != 0;
21486 return ((int) parser->num_template_parameter_lists > template_class_depth
21487 (current_class_type));
21490 /* Parse a parameter-declaration-clause.
21492 parameter-declaration-clause:
21493 parameter-declaration-list [opt] ... [opt]
21494 parameter-declaration-list , ...
21496 Returns a representation for the parameter declarations. A return
21497 value of NULL indicates a parameter-declaration-clause consisting
21498 only of an ellipsis. */
21500 static tree
21501 cp_parser_parameter_declaration_clause (cp_parser* parser)
21503 tree parameters;
21504 cp_token *token;
21505 bool ellipsis_p;
21507 temp_override<bool> cleanup
21508 (parser->auto_is_implicit_function_template_parm_p);
21510 if (!processing_specialization
21511 && !processing_template_parmlist
21512 && !processing_explicit_instantiation
21513 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21514 actual function or a random abstract declarator. */
21515 && parser->default_arg_ok_p)
21516 if (!current_function_decl
21517 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21518 parser->auto_is_implicit_function_template_parm_p = true;
21520 /* Peek at the next token. */
21521 token = cp_lexer_peek_token (parser->lexer);
21522 /* Check for trivial parameter-declaration-clauses. */
21523 if (token->type == CPP_ELLIPSIS)
21525 /* Consume the `...' token. */
21526 cp_lexer_consume_token (parser->lexer);
21527 return NULL_TREE;
21529 else if (token->type == CPP_CLOSE_PAREN)
21530 /* There are no parameters. */
21531 return void_list_node;
21532 /* Check for `(void)', too, which is a special case. */
21533 else if (token->keyword == RID_VOID
21534 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21535 == CPP_CLOSE_PAREN))
21537 /* Consume the `void' token. */
21538 cp_lexer_consume_token (parser->lexer);
21539 /* There are no parameters. */
21540 return void_list_node;
21543 /* Parse the parameter-declaration-list. */
21544 parameters = cp_parser_parameter_declaration_list (parser);
21545 /* If a parse error occurred while parsing the
21546 parameter-declaration-list, then the entire
21547 parameter-declaration-clause is erroneous. */
21548 if (parameters == error_mark_node)
21549 return NULL_TREE;
21551 /* Peek at the next token. */
21552 token = cp_lexer_peek_token (parser->lexer);
21553 /* If it's a `,', the clause should terminate with an ellipsis. */
21554 if (token->type == CPP_COMMA)
21556 /* Consume the `,'. */
21557 cp_lexer_consume_token (parser->lexer);
21558 /* Expect an ellipsis. */
21559 ellipsis_p
21560 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21562 /* It might also be `...' if the optional trailing `,' was
21563 omitted. */
21564 else if (token->type == CPP_ELLIPSIS)
21566 /* Consume the `...' token. */
21567 cp_lexer_consume_token (parser->lexer);
21568 /* And remember that we saw it. */
21569 ellipsis_p = true;
21571 else
21572 ellipsis_p = false;
21574 /* Finish the parameter list. */
21575 if (!ellipsis_p)
21576 parameters = chainon (parameters, void_list_node);
21578 return parameters;
21581 /* Parse a parameter-declaration-list.
21583 parameter-declaration-list:
21584 parameter-declaration
21585 parameter-declaration-list , parameter-declaration
21587 Returns a representation of the parameter-declaration-list, as for
21588 cp_parser_parameter_declaration_clause. However, the
21589 `void_list_node' is never appended to the list. */
21591 static tree
21592 cp_parser_parameter_declaration_list (cp_parser* parser)
21594 tree parameters = NULL_TREE;
21595 tree *tail = &parameters;
21596 bool saved_in_unbraced_linkage_specification_p;
21597 int index = 0;
21599 /* The special considerations that apply to a function within an
21600 unbraced linkage specifications do not apply to the parameters
21601 to the function. */
21602 saved_in_unbraced_linkage_specification_p
21603 = parser->in_unbraced_linkage_specification_p;
21604 parser->in_unbraced_linkage_specification_p = false;
21606 /* Look for more parameters. */
21607 while (true)
21609 cp_parameter_declarator *parameter;
21610 tree decl = error_mark_node;
21611 bool parenthesized_p = false;
21613 /* Parse the parameter. */
21614 parameter
21615 = cp_parser_parameter_declaration (parser,
21616 /*template_parm_p=*/false,
21617 &parenthesized_p);
21619 /* We don't know yet if the enclosing context is deprecated, so wait
21620 and warn in grokparms if appropriate. */
21621 deprecated_state = DEPRECATED_SUPPRESS;
21623 if (parameter)
21625 decl = grokdeclarator (parameter->declarator,
21626 &parameter->decl_specifiers,
21627 PARM,
21628 parameter->default_argument != NULL_TREE,
21629 &parameter->decl_specifiers.attributes);
21630 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21631 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21634 deprecated_state = DEPRECATED_NORMAL;
21636 /* If a parse error occurred parsing the parameter declaration,
21637 then the entire parameter-declaration-list is erroneous. */
21638 if (decl == error_mark_node)
21640 parameters = error_mark_node;
21641 break;
21644 if (parameter->decl_specifiers.attributes)
21645 cplus_decl_attributes (&decl,
21646 parameter->decl_specifiers.attributes,
21648 if (DECL_NAME (decl))
21649 decl = pushdecl (decl);
21651 if (decl != error_mark_node)
21653 retrofit_lang_decl (decl);
21654 DECL_PARM_INDEX (decl) = ++index;
21655 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21658 /* Add the new parameter to the list. */
21659 *tail = build_tree_list (parameter->default_argument, decl);
21660 tail = &TREE_CHAIN (*tail);
21662 /* Peek at the next token. */
21663 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21664 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21665 /* These are for Objective-C++ */
21666 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21667 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21668 /* The parameter-declaration-list is complete. */
21669 break;
21670 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21672 cp_token *token;
21674 /* Peek at the next token. */
21675 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21676 /* If it's an ellipsis, then the list is complete. */
21677 if (token->type == CPP_ELLIPSIS)
21678 break;
21679 /* Otherwise, there must be more parameters. Consume the
21680 `,'. */
21681 cp_lexer_consume_token (parser->lexer);
21682 /* When parsing something like:
21684 int i(float f, double d)
21686 we can tell after seeing the declaration for "f" that we
21687 are not looking at an initialization of a variable "i",
21688 but rather at the declaration of a function "i".
21690 Due to the fact that the parsing of template arguments
21691 (as specified to a template-id) requires backtracking we
21692 cannot use this technique when inside a template argument
21693 list. */
21694 if (!parser->in_template_argument_list_p
21695 && !parser->in_type_id_in_expr_p
21696 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21697 /* However, a parameter-declaration of the form
21698 "float(f)" (which is a valid declaration of a
21699 parameter "f") can also be interpreted as an
21700 expression (the conversion of "f" to "float"). */
21701 && !parenthesized_p)
21702 cp_parser_commit_to_tentative_parse (parser);
21704 else
21706 cp_parser_error (parser, "expected %<,%> or %<...%>");
21707 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21708 cp_parser_skip_to_closing_parenthesis (parser,
21709 /*recovering=*/true,
21710 /*or_comma=*/false,
21711 /*consume_paren=*/false);
21712 break;
21716 parser->in_unbraced_linkage_specification_p
21717 = saved_in_unbraced_linkage_specification_p;
21719 /* Reset implicit_template_scope if we are about to leave the function
21720 parameter list that introduced it. Note that for out-of-line member
21721 definitions, there will be one or more class scopes before we get to
21722 the template parameter scope. */
21724 if (cp_binding_level *its = parser->implicit_template_scope)
21725 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21727 while (maybe_its->kind == sk_class)
21728 maybe_its = maybe_its->level_chain;
21729 if (maybe_its == its)
21731 parser->implicit_template_parms = 0;
21732 parser->implicit_template_scope = 0;
21736 return parameters;
21739 /* Parse a parameter declaration.
21741 parameter-declaration:
21742 decl-specifier-seq ... [opt] declarator
21743 decl-specifier-seq declarator = assignment-expression
21744 decl-specifier-seq ... [opt] abstract-declarator [opt]
21745 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21747 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21748 declares a template parameter. (In that case, a non-nested `>'
21749 token encountered during the parsing of the assignment-expression
21750 is not interpreted as a greater-than operator.)
21752 Returns a representation of the parameter, or NULL if an error
21753 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21754 true iff the declarator is of the form "(p)". */
21756 static cp_parameter_declarator *
21757 cp_parser_parameter_declaration (cp_parser *parser,
21758 bool template_parm_p,
21759 bool *parenthesized_p)
21761 int declares_class_or_enum;
21762 cp_decl_specifier_seq decl_specifiers;
21763 cp_declarator *declarator;
21764 tree default_argument;
21765 cp_token *token = NULL, *declarator_token_start = NULL;
21766 const char *saved_message;
21767 bool template_parameter_pack_p = false;
21769 /* In a template parameter, `>' is not an operator.
21771 [temp.param]
21773 When parsing a default template-argument for a non-type
21774 template-parameter, the first non-nested `>' is taken as the end
21775 of the template parameter-list rather than a greater-than
21776 operator. */
21778 /* Type definitions may not appear in parameter types. */
21779 saved_message = parser->type_definition_forbidden_message;
21780 parser->type_definition_forbidden_message
21781 = G_("types may not be defined in parameter types");
21783 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21784 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21785 (current_template_parms)) : 0);
21787 /* Parse the declaration-specifiers. */
21788 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21789 cp_parser_decl_specifier_seq (parser,
21790 CP_PARSER_FLAGS_NONE,
21791 &decl_specifiers,
21792 &declares_class_or_enum);
21794 /* Complain about missing 'typename' or other invalid type names. */
21795 if (!decl_specifiers.any_type_specifiers_p
21796 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21797 decl_specifiers.type = error_mark_node;
21799 /* If an error occurred, there's no reason to attempt to parse the
21800 rest of the declaration. */
21801 if (cp_parser_error_occurred (parser))
21803 parser->type_definition_forbidden_message = saved_message;
21804 return NULL;
21807 /* Peek at the next token. */
21808 token = cp_lexer_peek_token (parser->lexer);
21810 /* If the next token is a `)', `,', `=', `>', or `...', then there
21811 is no declarator. However, when variadic templates are enabled,
21812 there may be a declarator following `...'. */
21813 if (token->type == CPP_CLOSE_PAREN
21814 || token->type == CPP_COMMA
21815 || token->type == CPP_EQ
21816 || token->type == CPP_GREATER)
21818 declarator = NULL;
21819 if (parenthesized_p)
21820 *parenthesized_p = false;
21822 /* Otherwise, there should be a declarator. */
21823 else
21825 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21826 parser->default_arg_ok_p = false;
21828 /* After seeing a decl-specifier-seq, if the next token is not a
21829 "(", there is no possibility that the code is a valid
21830 expression. Therefore, if parsing tentatively, we commit at
21831 this point. */
21832 if (!parser->in_template_argument_list_p
21833 /* In an expression context, having seen:
21835 (int((char ...
21837 we cannot be sure whether we are looking at a
21838 function-type (taking a "char" as a parameter) or a cast
21839 of some object of type "char" to "int". */
21840 && !parser->in_type_id_in_expr_p
21841 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21842 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21843 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21844 cp_parser_commit_to_tentative_parse (parser);
21845 /* Parse the declarator. */
21846 declarator_token_start = token;
21847 declarator = cp_parser_declarator (parser,
21848 CP_PARSER_DECLARATOR_EITHER,
21849 /*ctor_dtor_or_conv_p=*/NULL,
21850 parenthesized_p,
21851 /*member_p=*/false,
21852 /*friend_p=*/false);
21853 parser->default_arg_ok_p = saved_default_arg_ok_p;
21854 /* After the declarator, allow more attributes. */
21855 decl_specifiers.attributes
21856 = attr_chainon (decl_specifiers.attributes,
21857 cp_parser_attributes_opt (parser));
21859 /* If the declarator is a template parameter pack, remember that and
21860 clear the flag in the declarator itself so we don't get errors
21861 from grokdeclarator. */
21862 if (template_parm_p && declarator && declarator->parameter_pack_p)
21864 declarator->parameter_pack_p = false;
21865 template_parameter_pack_p = true;
21869 /* If the next token is an ellipsis, and we have not seen a declarator
21870 name, and if either the type of the declarator contains parameter
21871 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21872 for, eg, abbreviated integral type names), then we actually have a
21873 parameter pack expansion expression. Otherwise, leave the ellipsis
21874 for a C-style variadic function. */
21875 token = cp_lexer_peek_token (parser->lexer);
21877 /* If a function parameter pack was specified and an implicit template
21878 parameter was introduced during cp_parser_parameter_declaration,
21879 change any implicit parameters introduced into packs. */
21880 if (parser->implicit_template_parms
21881 && ((token->type == CPP_ELLIPSIS
21882 && declarator_can_be_parameter_pack (declarator))
21883 || (declarator && declarator->parameter_pack_p)))
21885 int latest_template_parm_idx = TREE_VEC_LENGTH
21886 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21888 if (latest_template_parm_idx != template_parm_idx)
21889 decl_specifiers.type = convert_generic_types_to_packs
21890 (decl_specifiers.type,
21891 template_parm_idx, latest_template_parm_idx);
21894 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21896 tree type = decl_specifiers.type;
21898 if (type && DECL_P (type))
21899 type = TREE_TYPE (type);
21901 if (((type
21902 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21903 && (template_parm_p || uses_parameter_packs (type)))
21904 || (!type && template_parm_p))
21905 && declarator_can_be_parameter_pack (declarator))
21907 /* Consume the `...'. */
21908 cp_lexer_consume_token (parser->lexer);
21909 maybe_warn_variadic_templates ();
21911 /* Build a pack expansion type */
21912 if (template_parm_p)
21913 template_parameter_pack_p = true;
21914 else if (declarator)
21915 declarator->parameter_pack_p = true;
21916 else
21917 decl_specifiers.type = make_pack_expansion (type);
21921 /* The restriction on defining new types applies only to the type
21922 of the parameter, not to the default argument. */
21923 parser->type_definition_forbidden_message = saved_message;
21925 /* If the next token is `=', then process a default argument. */
21926 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21928 tree type = decl_specifiers.type;
21929 token = cp_lexer_peek_token (parser->lexer);
21930 /* If we are defining a class, then the tokens that make up the
21931 default argument must be saved and processed later. */
21932 if (!template_parm_p && at_class_scope_p ()
21933 && TYPE_BEING_DEFINED (current_class_type)
21934 && !LAMBDA_TYPE_P (current_class_type))
21935 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21937 // A constrained-type-specifier may declare a type template-parameter.
21938 else if (declares_constrained_type_template_parameter (type))
21939 default_argument
21940 = cp_parser_default_type_template_argument (parser);
21942 // A constrained-type-specifier may declare a template-template-parameter.
21943 else if (declares_constrained_template_template_parameter (type))
21944 default_argument
21945 = cp_parser_default_template_template_argument (parser);
21947 /* Outside of a class definition, we can just parse the
21948 assignment-expression. */
21949 else
21950 default_argument
21951 = cp_parser_default_argument (parser, template_parm_p);
21953 if (!parser->default_arg_ok_p)
21955 permerror (token->location,
21956 "default arguments are only "
21957 "permitted for function parameters");
21959 else if ((declarator && declarator->parameter_pack_p)
21960 || template_parameter_pack_p
21961 || (decl_specifiers.type
21962 && PACK_EXPANSION_P (decl_specifiers.type)))
21964 /* Find the name of the parameter pack. */
21965 cp_declarator *id_declarator = declarator;
21966 while (id_declarator && id_declarator->kind != cdk_id)
21967 id_declarator = id_declarator->declarator;
21969 if (id_declarator && id_declarator->kind == cdk_id)
21970 error_at (declarator_token_start->location,
21971 template_parm_p
21972 ? G_("template parameter pack %qD "
21973 "cannot have a default argument")
21974 : G_("parameter pack %qD cannot have "
21975 "a default argument"),
21976 id_declarator->u.id.unqualified_name);
21977 else
21978 error_at (declarator_token_start->location,
21979 template_parm_p
21980 ? G_("template parameter pack cannot have "
21981 "a default argument")
21982 : G_("parameter pack cannot have a "
21983 "default argument"));
21985 default_argument = NULL_TREE;
21988 else
21989 default_argument = NULL_TREE;
21991 /* Generate a location for the parameter, ranging from the start of the
21992 initial token to the end of the final token (using input_location for
21993 the latter, set up by cp_lexer_set_source_position_from_token when
21994 consuming tokens).
21996 If we have a identifier, then use it for the caret location, e.g.
21998 extern int callee (int one, int (*two)(int, int), float three);
21999 ~~~~~~^~~~~~~~~~~~~~
22001 otherwise, reuse the start location for the caret location e.g.:
22003 extern int callee (int one, int (*)(int, int), float three);
22004 ^~~~~~~~~~~~~~~~~
22007 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22008 ? declarator->id_loc
22009 : decl_spec_token_start->location);
22010 location_t param_loc = make_location (caret_loc,
22011 decl_spec_token_start->location,
22012 input_location);
22014 return make_parameter_declarator (&decl_specifiers,
22015 declarator,
22016 default_argument,
22017 param_loc,
22018 template_parameter_pack_p);
22021 /* Parse a default argument and return it.
22023 TEMPLATE_PARM_P is true if this is a default argument for a
22024 non-type template parameter. */
22025 static tree
22026 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22028 tree default_argument = NULL_TREE;
22029 bool saved_greater_than_is_operator_p;
22030 bool saved_local_variables_forbidden_p;
22031 bool non_constant_p, is_direct_init;
22033 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22034 set correctly. */
22035 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22036 parser->greater_than_is_operator_p = !template_parm_p;
22037 /* Local variable names (and the `this' keyword) may not
22038 appear in a default argument. */
22039 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22040 parser->local_variables_forbidden_p = true;
22041 /* Parse the assignment-expression. */
22042 if (template_parm_p)
22043 push_deferring_access_checks (dk_no_deferred);
22044 tree saved_class_ptr = NULL_TREE;
22045 tree saved_class_ref = NULL_TREE;
22046 /* The "this" pointer is not valid in a default argument. */
22047 if (cfun)
22049 saved_class_ptr = current_class_ptr;
22050 cp_function_chain->x_current_class_ptr = NULL_TREE;
22051 saved_class_ref = current_class_ref;
22052 cp_function_chain->x_current_class_ref = NULL_TREE;
22054 default_argument
22055 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22056 /* Restore the "this" pointer. */
22057 if (cfun)
22059 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22060 cp_function_chain->x_current_class_ref = saved_class_ref;
22062 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22063 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22064 if (template_parm_p)
22065 pop_deferring_access_checks ();
22066 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22067 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22069 return default_argument;
22072 /* Parse a function-body.
22074 function-body:
22075 compound_statement */
22077 static void
22078 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22080 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22081 ? BCS_TRY_BLOCK : BCS_NORMAL),
22082 true);
22085 /* Parse a ctor-initializer-opt followed by a function-body. Return
22086 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22087 is true we are parsing a function-try-block. */
22089 static void
22090 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22091 bool in_function_try_block)
22093 tree body, list;
22094 const bool check_body_p =
22095 DECL_CONSTRUCTOR_P (current_function_decl)
22096 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
22097 tree last = NULL;
22099 /* Begin the function body. */
22100 body = begin_function_body ();
22101 /* Parse the optional ctor-initializer. */
22102 cp_parser_ctor_initializer_opt (parser);
22104 /* If we're parsing a constexpr constructor definition, we need
22105 to check that the constructor body is indeed empty. However,
22106 before we get to cp_parser_function_body lot of junk has been
22107 generated, so we can't just check that we have an empty block.
22108 Rather we take a snapshot of the outermost block, and check whether
22109 cp_parser_function_body changed its state. */
22110 if (check_body_p)
22112 list = cur_stmt_list;
22113 if (STATEMENT_LIST_TAIL (list))
22114 last = STATEMENT_LIST_TAIL (list)->stmt;
22116 /* Parse the function-body. */
22117 cp_parser_function_body (parser, in_function_try_block);
22118 if (check_body_p)
22119 check_constexpr_ctor_body (last, list, /*complain=*/true);
22120 /* Finish the function body. */
22121 finish_function_body (body);
22124 /* Parse an initializer.
22126 initializer:
22127 = initializer-clause
22128 ( expression-list )
22130 Returns an expression representing the initializer. If no
22131 initializer is present, NULL_TREE is returned.
22133 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22134 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22135 set to TRUE if there is no initializer present. If there is an
22136 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22137 is set to true; otherwise it is set to false. */
22139 static tree
22140 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22141 bool* non_constant_p, bool subexpression_p)
22143 cp_token *token;
22144 tree init;
22146 /* Peek at the next token. */
22147 token = cp_lexer_peek_token (parser->lexer);
22149 /* Let our caller know whether or not this initializer was
22150 parenthesized. */
22151 *is_direct_init = (token->type != CPP_EQ);
22152 /* Assume that the initializer is constant. */
22153 *non_constant_p = false;
22155 if (token->type == CPP_EQ)
22157 /* Consume the `='. */
22158 cp_lexer_consume_token (parser->lexer);
22159 /* Parse the initializer-clause. */
22160 init = cp_parser_initializer_clause (parser, non_constant_p);
22162 else if (token->type == CPP_OPEN_PAREN)
22164 vec<tree, va_gc> *vec;
22165 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22166 /*cast_p=*/false,
22167 /*allow_expansion_p=*/true,
22168 non_constant_p);
22169 if (vec == NULL)
22170 return error_mark_node;
22171 init = build_tree_list_vec (vec);
22172 release_tree_vector (vec);
22174 else if (token->type == CPP_OPEN_BRACE)
22176 cp_lexer_set_source_position (parser->lexer);
22177 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22178 init = cp_parser_braced_list (parser, non_constant_p);
22179 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22181 else
22183 /* Anything else is an error. */
22184 cp_parser_error (parser, "expected initializer");
22185 init = error_mark_node;
22188 if (!subexpression_p && check_for_bare_parameter_packs (init))
22189 init = error_mark_node;
22191 return init;
22194 /* Parse an initializer-clause.
22196 initializer-clause:
22197 assignment-expression
22198 braced-init-list
22200 Returns an expression representing the initializer.
22202 If the `assignment-expression' production is used the value
22203 returned is simply a representation for the expression.
22205 Otherwise, calls cp_parser_braced_list. */
22207 static cp_expr
22208 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22210 cp_expr initializer;
22212 /* Assume the expression is constant. */
22213 *non_constant_p = false;
22215 /* If it is not a `{', then we are looking at an
22216 assignment-expression. */
22217 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22219 initializer
22220 = cp_parser_constant_expression (parser,
22221 /*allow_non_constant_p=*/true,
22222 non_constant_p);
22224 else
22225 initializer = cp_parser_braced_list (parser, non_constant_p);
22227 return initializer;
22230 /* Parse a brace-enclosed initializer list.
22232 braced-init-list:
22233 { initializer-list , [opt] }
22234 { designated-initializer-list , [opt] }
22237 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22238 the elements of the initializer-list (or NULL, if the last
22239 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22240 NULL_TREE. There is no way to detect whether or not the optional
22241 trailing `,' was provided. NON_CONSTANT_P is as for
22242 cp_parser_initializer. */
22244 static cp_expr
22245 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22247 tree initializer;
22248 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22250 /* Consume the `{' token. */
22251 matching_braces braces;
22252 braces.require_open (parser);
22253 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22254 initializer = make_node (CONSTRUCTOR);
22255 /* If it's not a `}', then there is a non-trivial initializer. */
22256 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22258 /* Parse the initializer list. */
22259 CONSTRUCTOR_ELTS (initializer)
22260 = cp_parser_initializer_list (parser, non_constant_p);
22261 /* A trailing `,' token is allowed. */
22262 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22263 cp_lexer_consume_token (parser->lexer);
22265 else
22266 *non_constant_p = false;
22267 /* Now, there should be a trailing `}'. */
22268 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22269 braces.require_close (parser);
22270 TREE_TYPE (initializer) = init_list_type_node;
22272 cp_expr result (initializer);
22273 /* Build a location of the form:
22274 { ... }
22275 ^~~~~~~
22276 with caret==start at the open brace, finish at the close brace. */
22277 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22278 result.set_location (combined_loc);
22279 return result;
22282 /* Consume tokens up to, and including, the next non-nested closing `]'.
22283 Returns true iff we found a closing `]'. */
22285 static bool
22286 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22288 unsigned square_depth = 0;
22290 while (true)
22292 cp_token * token = cp_lexer_peek_token (parser->lexer);
22294 switch (token->type)
22296 case CPP_EOF:
22297 case CPP_PRAGMA_EOL:
22298 /* If we've run out of tokens, then there is no closing `]'. */
22299 return false;
22301 case CPP_OPEN_SQUARE:
22302 ++square_depth;
22303 break;
22305 case CPP_CLOSE_SQUARE:
22306 if (!square_depth--)
22308 cp_lexer_consume_token (parser->lexer);
22309 return true;
22311 break;
22313 default:
22314 break;
22317 /* Consume the token. */
22318 cp_lexer_consume_token (parser->lexer);
22322 /* Return true if we are looking at an array-designator, false otherwise. */
22324 static bool
22325 cp_parser_array_designator_p (cp_parser *parser)
22327 /* Consume the `['. */
22328 cp_lexer_consume_token (parser->lexer);
22330 cp_lexer_save_tokens (parser->lexer);
22332 /* Skip tokens until the next token is a closing square bracket.
22333 If we find the closing `]', and the next token is a `=', then
22334 we are looking at an array designator. */
22335 bool array_designator_p
22336 = (cp_parser_skip_to_closing_square_bracket (parser)
22337 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22339 /* Roll back the tokens we skipped. */
22340 cp_lexer_rollback_tokens (parser->lexer);
22342 return array_designator_p;
22345 /* Parse an initializer-list.
22347 initializer-list:
22348 initializer-clause ... [opt]
22349 initializer-list , initializer-clause ... [opt]
22351 C++2A Extension:
22353 designated-initializer-list:
22354 designated-initializer-clause
22355 designated-initializer-list , designated-initializer-clause
22357 designated-initializer-clause:
22358 designator brace-or-equal-initializer
22360 designator:
22361 . identifier
22363 GNU Extension:
22365 initializer-list:
22366 designation initializer-clause ...[opt]
22367 initializer-list , designation initializer-clause ...[opt]
22369 designation:
22370 . identifier =
22371 identifier :
22372 [ constant-expression ] =
22374 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22375 for the initializer. If the INDEX of the elt is non-NULL, it is the
22376 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22377 as for cp_parser_initializer. */
22379 static vec<constructor_elt, va_gc> *
22380 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22382 vec<constructor_elt, va_gc> *v = NULL;
22383 bool first_p = true;
22384 tree first_designator = NULL_TREE;
22386 /* Assume all of the expressions are constant. */
22387 *non_constant_p = false;
22389 /* Parse the rest of the list. */
22390 while (true)
22392 cp_token *token;
22393 tree designator;
22394 tree initializer;
22395 bool clause_non_constant_p;
22396 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22398 /* Handle the C++2A syntax, '. id ='. */
22399 if ((cxx_dialect >= cxx2a
22400 || cp_parser_allow_gnu_extensions_p (parser))
22401 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22402 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22403 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22404 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22405 == CPP_OPEN_BRACE)))
22407 if (cxx_dialect < cxx2a)
22408 pedwarn (loc, OPT_Wpedantic,
22409 "C++ designated initializers only available with "
22410 "-std=c++2a or -std=gnu++2a");
22411 /* Consume the `.'. */
22412 cp_lexer_consume_token (parser->lexer);
22413 /* Consume the identifier. */
22414 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22415 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22416 /* Consume the `='. */
22417 cp_lexer_consume_token (parser->lexer);
22419 /* Also, if the next token is an identifier and the following one is a
22420 colon, we are looking at the GNU designated-initializer
22421 syntax. */
22422 else if (cp_parser_allow_gnu_extensions_p (parser)
22423 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22424 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22425 == CPP_COLON))
22427 /* Warn the user that they are using an extension. */
22428 pedwarn (loc, OPT_Wpedantic,
22429 "ISO C++ does not allow GNU designated initializers");
22430 /* Consume the identifier. */
22431 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22432 /* Consume the `:'. */
22433 cp_lexer_consume_token (parser->lexer);
22435 /* Also handle C99 array designators, '[ const ] ='. */
22436 else if (cp_parser_allow_gnu_extensions_p (parser)
22437 && !c_dialect_objc ()
22438 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22440 /* In C++11, [ could start a lambda-introducer. */
22441 bool non_const = false;
22443 cp_parser_parse_tentatively (parser);
22445 if (!cp_parser_array_designator_p (parser))
22447 cp_parser_simulate_error (parser);
22448 designator = NULL_TREE;
22450 else
22452 designator = cp_parser_constant_expression (parser, true,
22453 &non_const);
22454 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22455 cp_parser_require (parser, CPP_EQ, RT_EQ);
22458 if (!cp_parser_parse_definitely (parser))
22459 designator = NULL_TREE;
22460 else if (non_const
22461 && (!require_potential_rvalue_constant_expression
22462 (designator)))
22463 designator = NULL_TREE;
22464 if (designator)
22465 /* Warn the user that they are using an extension. */
22466 pedwarn (loc, OPT_Wpedantic,
22467 "ISO C++ does not allow C99 designated initializers");
22469 else
22470 designator = NULL_TREE;
22472 if (first_p)
22474 first_designator = designator;
22475 first_p = false;
22477 else if (cxx_dialect >= cxx2a
22478 && first_designator != error_mark_node
22479 && (!first_designator != !designator))
22481 error_at (loc, "either all initializer clauses should be designated "
22482 "or none of them should be");
22483 first_designator = error_mark_node;
22485 else if (cxx_dialect < cxx2a && !first_designator)
22486 first_designator = designator;
22488 /* Parse the initializer. */
22489 initializer = cp_parser_initializer_clause (parser,
22490 &clause_non_constant_p);
22491 /* If any clause is non-constant, so is the entire initializer. */
22492 if (clause_non_constant_p)
22493 *non_constant_p = true;
22495 /* If we have an ellipsis, this is an initializer pack
22496 expansion. */
22497 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22499 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22501 /* Consume the `...'. */
22502 cp_lexer_consume_token (parser->lexer);
22504 if (designator && cxx_dialect >= cxx2a)
22505 error_at (loc,
22506 "%<...%> not allowed in designated initializer list");
22508 /* Turn the initializer into an initializer expansion. */
22509 initializer = make_pack_expansion (initializer);
22512 /* Add it to the vector. */
22513 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22515 /* If the next token is not a comma, we have reached the end of
22516 the list. */
22517 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22518 break;
22520 /* Peek at the next token. */
22521 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22522 /* If the next token is a `}', then we're still done. An
22523 initializer-clause can have a trailing `,' after the
22524 initializer-list and before the closing `}'. */
22525 if (token->type == CPP_CLOSE_BRACE)
22526 break;
22528 /* Consume the `,' token. */
22529 cp_lexer_consume_token (parser->lexer);
22532 /* The same identifier shall not appear in multiple designators
22533 of a designated-initializer-list. */
22534 if (first_designator)
22536 unsigned int i;
22537 tree designator, val;
22538 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22539 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22541 if (IDENTIFIER_MARKED (designator))
22543 error_at (cp_expr_loc_or_loc (val, input_location),
22544 "%<.%s%> designator used multiple times in "
22545 "the same initializer list",
22546 IDENTIFIER_POINTER (designator));
22547 (*v)[i].index = error_mark_node;
22549 else
22550 IDENTIFIER_MARKED (designator) = 1;
22552 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22553 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22554 IDENTIFIER_MARKED (designator) = 0;
22557 return v;
22560 /* Classes [gram.class] */
22562 /* Parse a class-name.
22564 class-name:
22565 identifier
22566 template-id
22568 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22569 to indicate that names looked up in dependent types should be
22570 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22571 keyword has been used to indicate that the name that appears next
22572 is a template. TAG_TYPE indicates the explicit tag given before
22573 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22574 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22575 is the class being defined in a class-head. If ENUM_OK is TRUE,
22576 enum-names are also accepted.
22578 Returns the TYPE_DECL representing the class. */
22580 static tree
22581 cp_parser_class_name (cp_parser *parser,
22582 bool typename_keyword_p,
22583 bool template_keyword_p,
22584 enum tag_types tag_type,
22585 bool check_dependency_p,
22586 bool class_head_p,
22587 bool is_declaration,
22588 bool enum_ok)
22590 tree decl;
22591 tree scope;
22592 bool typename_p;
22593 cp_token *token;
22594 tree identifier = NULL_TREE;
22596 /* All class-names start with an identifier. */
22597 token = cp_lexer_peek_token (parser->lexer);
22598 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22600 cp_parser_error (parser, "expected class-name");
22601 return error_mark_node;
22604 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22605 to a template-id, so we save it here. */
22606 scope = parser->scope;
22607 if (scope == error_mark_node)
22608 return error_mark_node;
22610 /* Any name names a type if we're following the `typename' keyword
22611 in a qualified name where the enclosing scope is type-dependent. */
22612 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22613 && dependent_type_p (scope));
22614 /* Handle the common case (an identifier, but not a template-id)
22615 efficiently. */
22616 if (token->type == CPP_NAME
22617 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22619 cp_token *identifier_token;
22620 bool ambiguous_p;
22622 /* Look for the identifier. */
22623 identifier_token = cp_lexer_peek_token (parser->lexer);
22624 ambiguous_p = identifier_token->error_reported;
22625 identifier = cp_parser_identifier (parser);
22626 /* If the next token isn't an identifier, we are certainly not
22627 looking at a class-name. */
22628 if (identifier == error_mark_node)
22629 decl = error_mark_node;
22630 /* If we know this is a type-name, there's no need to look it
22631 up. */
22632 else if (typename_p)
22633 decl = identifier;
22634 else
22636 tree ambiguous_decls;
22637 /* If we already know that this lookup is ambiguous, then
22638 we've already issued an error message; there's no reason
22639 to check again. */
22640 if (ambiguous_p)
22642 cp_parser_simulate_error (parser);
22643 return error_mark_node;
22645 /* If the next token is a `::', then the name must be a type
22646 name.
22648 [basic.lookup.qual]
22650 During the lookup for a name preceding the :: scope
22651 resolution operator, object, function, and enumerator
22652 names are ignored. */
22653 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22654 tag_type = scope_type;
22655 /* Look up the name. */
22656 decl = cp_parser_lookup_name (parser, identifier,
22657 tag_type,
22658 /*is_template=*/false,
22659 /*is_namespace=*/false,
22660 check_dependency_p,
22661 &ambiguous_decls,
22662 identifier_token->location);
22663 if (ambiguous_decls)
22665 if (cp_parser_parsing_tentatively (parser))
22666 cp_parser_simulate_error (parser);
22667 return error_mark_node;
22671 else
22673 /* Try a template-id. */
22674 decl = cp_parser_template_id (parser, template_keyword_p,
22675 check_dependency_p,
22676 tag_type,
22677 is_declaration);
22678 if (decl == error_mark_node)
22679 return error_mark_node;
22682 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22684 /* If this is a typename, create a TYPENAME_TYPE. */
22685 if (typename_p && decl != error_mark_node)
22687 decl = make_typename_type (scope, decl, typename_type,
22688 /*complain=*/tf_error);
22689 if (decl != error_mark_node)
22690 decl = TYPE_NAME (decl);
22693 decl = strip_using_decl (decl);
22695 /* Check to see that it is really the name of a class. */
22696 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22697 && identifier_p (TREE_OPERAND (decl, 0))
22698 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22699 /* Situations like this:
22701 template <typename T> struct A {
22702 typename T::template X<int>::I i;
22705 are problematic. Is `T::template X<int>' a class-name? The
22706 standard does not seem to be definitive, but there is no other
22707 valid interpretation of the following `::'. Therefore, those
22708 names are considered class-names. */
22710 decl = make_typename_type (scope, decl, tag_type, tf_error);
22711 if (decl != error_mark_node)
22712 decl = TYPE_NAME (decl);
22714 else if (TREE_CODE (decl) != TYPE_DECL
22715 || TREE_TYPE (decl) == error_mark_node
22716 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22717 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22718 /* In Objective-C 2.0, a classname followed by '.' starts a
22719 dot-syntax expression, and it's not a type-name. */
22720 || (c_dialect_objc ()
22721 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22722 && objc_is_class_name (decl)))
22723 decl = error_mark_node;
22725 if (decl == error_mark_node)
22726 cp_parser_error (parser, "expected class-name");
22727 else if (identifier && !parser->scope)
22728 maybe_note_name_used_in_class (identifier, decl);
22730 return decl;
22733 /* Parse a class-specifier.
22735 class-specifier:
22736 class-head { member-specification [opt] }
22738 Returns the TREE_TYPE representing the class. */
22740 static tree
22741 cp_parser_class_specifier_1 (cp_parser* parser)
22743 tree type;
22744 tree attributes = NULL_TREE;
22745 bool nested_name_specifier_p;
22746 unsigned saved_num_template_parameter_lists;
22747 bool saved_in_function_body;
22748 unsigned char in_statement;
22749 bool in_switch_statement_p;
22750 bool saved_in_unbraced_linkage_specification_p;
22751 tree old_scope = NULL_TREE;
22752 tree scope = NULL_TREE;
22753 cp_token *closing_brace;
22755 push_deferring_access_checks (dk_no_deferred);
22757 /* Parse the class-head. */
22758 type = cp_parser_class_head (parser,
22759 &nested_name_specifier_p);
22760 /* If the class-head was a semantic disaster, skip the entire body
22761 of the class. */
22762 if (!type)
22764 cp_parser_skip_to_end_of_block_or_statement (parser);
22765 pop_deferring_access_checks ();
22766 return error_mark_node;
22769 /* Look for the `{'. */
22770 matching_braces braces;
22771 if (!braces.require_open (parser))
22773 pop_deferring_access_checks ();
22774 return error_mark_node;
22777 cp_ensure_no_omp_declare_simd (parser);
22778 cp_ensure_no_oacc_routine (parser);
22780 /* Issue an error message if type-definitions are forbidden here. */
22781 cp_parser_check_type_definition (parser);
22782 /* Remember that we are defining one more class. */
22783 ++parser->num_classes_being_defined;
22784 /* Inside the class, surrounding template-parameter-lists do not
22785 apply. */
22786 saved_num_template_parameter_lists
22787 = parser->num_template_parameter_lists;
22788 parser->num_template_parameter_lists = 0;
22789 /* We are not in a function body. */
22790 saved_in_function_body = parser->in_function_body;
22791 parser->in_function_body = false;
22792 /* Or in a loop. */
22793 in_statement = parser->in_statement;
22794 parser->in_statement = 0;
22795 /* Or in a switch. */
22796 in_switch_statement_p = parser->in_switch_statement_p;
22797 parser->in_switch_statement_p = false;
22798 /* We are not immediately inside an extern "lang" block. */
22799 saved_in_unbraced_linkage_specification_p
22800 = parser->in_unbraced_linkage_specification_p;
22801 parser->in_unbraced_linkage_specification_p = false;
22803 // Associate constraints with the type.
22804 if (flag_concepts)
22805 type = associate_classtype_constraints (type);
22807 /* Start the class. */
22808 if (nested_name_specifier_p)
22810 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22811 old_scope = push_inner_scope (scope);
22813 type = begin_class_definition (type);
22815 if (type == error_mark_node)
22816 /* If the type is erroneous, skip the entire body of the class. */
22817 cp_parser_skip_to_closing_brace (parser);
22818 else
22819 /* Parse the member-specification. */
22820 cp_parser_member_specification_opt (parser);
22822 /* Look for the trailing `}'. */
22823 closing_brace = braces.require_close (parser);
22824 /* Look for trailing attributes to apply to this class. */
22825 if (cp_parser_allow_gnu_extensions_p (parser))
22826 attributes = cp_parser_gnu_attributes_opt (parser);
22827 if (type != error_mark_node)
22828 type = finish_struct (type, attributes);
22829 if (nested_name_specifier_p)
22830 pop_inner_scope (old_scope, scope);
22832 /* We've finished a type definition. Check for the common syntax
22833 error of forgetting a semicolon after the definition. We need to
22834 be careful, as we can't just check for not-a-semicolon and be done
22835 with it; the user might have typed:
22837 class X { } c = ...;
22838 class X { } *p = ...;
22840 and so forth. Instead, enumerate all the possible tokens that
22841 might follow this production; if we don't see one of them, then
22842 complain and silently insert the semicolon. */
22844 cp_token *token = cp_lexer_peek_token (parser->lexer);
22845 bool want_semicolon = true;
22847 if (cp_next_tokens_can_be_std_attribute_p (parser))
22848 /* Don't try to parse c++11 attributes here. As per the
22849 grammar, that should be a task for
22850 cp_parser_decl_specifier_seq. */
22851 want_semicolon = false;
22853 switch (token->type)
22855 case CPP_NAME:
22856 case CPP_SEMICOLON:
22857 case CPP_MULT:
22858 case CPP_AND:
22859 case CPP_OPEN_PAREN:
22860 case CPP_CLOSE_PAREN:
22861 case CPP_COMMA:
22862 want_semicolon = false;
22863 break;
22865 /* While it's legal for type qualifiers and storage class
22866 specifiers to follow type definitions in the grammar, only
22867 compiler testsuites contain code like that. Assume that if
22868 we see such code, then what we're really seeing is a case
22869 like:
22871 class X { }
22872 const <type> var = ...;
22876 class Y { }
22877 static <type> func (...) ...
22879 i.e. the qualifier or specifier applies to the next
22880 declaration. To do so, however, we need to look ahead one
22881 more token to see if *that* token is a type specifier.
22883 This code could be improved to handle:
22885 class Z { }
22886 static const <type> var = ...; */
22887 case CPP_KEYWORD:
22888 if (keyword_is_decl_specifier (token->keyword))
22890 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22892 /* Handling user-defined types here would be nice, but very
22893 tricky. */
22894 want_semicolon
22895 = (lookahead->type == CPP_KEYWORD
22896 && keyword_begins_type_specifier (lookahead->keyword));
22898 break;
22899 default:
22900 break;
22903 /* If we don't have a type, then something is very wrong and we
22904 shouldn't try to do anything clever. Likewise for not seeing the
22905 closing brace. */
22906 if (closing_brace && TYPE_P (type) && want_semicolon)
22908 /* Locate the closing brace. */
22909 cp_token_position prev
22910 = cp_lexer_previous_token_position (parser->lexer);
22911 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22912 location_t loc = prev_token->location;
22914 /* We want to suggest insertion of a ';' immediately *after* the
22915 closing brace, so, if we can, offset the location by 1 column. */
22916 location_t next_loc = loc;
22917 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22918 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22920 rich_location richloc (line_table, next_loc);
22922 /* If we successfully offset the location, suggest the fix-it. */
22923 if (next_loc != loc)
22924 richloc.add_fixit_insert_before (next_loc, ";");
22926 if (CLASSTYPE_DECLARED_CLASS (type))
22927 error_at (&richloc,
22928 "expected %<;%> after class definition");
22929 else if (TREE_CODE (type) == RECORD_TYPE)
22930 error_at (&richloc,
22931 "expected %<;%> after struct definition");
22932 else if (TREE_CODE (type) == UNION_TYPE)
22933 error_at (&richloc,
22934 "expected %<;%> after union definition");
22935 else
22936 gcc_unreachable ();
22938 /* Unget one token and smash it to look as though we encountered
22939 a semicolon in the input stream. */
22940 cp_lexer_set_token_position (parser->lexer, prev);
22941 token = cp_lexer_peek_token (parser->lexer);
22942 token->type = CPP_SEMICOLON;
22943 token->keyword = RID_MAX;
22947 /* If this class is not itself within the scope of another class,
22948 then we need to parse the bodies of all of the queued function
22949 definitions. Note that the queued functions defined in a class
22950 are not always processed immediately following the
22951 class-specifier for that class. Consider:
22953 struct A {
22954 struct B { void f() { sizeof (A); } };
22957 If `f' were processed before the processing of `A' were
22958 completed, there would be no way to compute the size of `A'.
22959 Note that the nesting we are interested in here is lexical --
22960 not the semantic nesting given by TYPE_CONTEXT. In particular,
22961 for:
22963 struct A { struct B; };
22964 struct A::B { void f() { } };
22966 there is no need to delay the parsing of `A::B::f'. */
22967 if (--parser->num_classes_being_defined == 0)
22969 tree decl;
22970 tree class_type = NULL_TREE;
22971 tree pushed_scope = NULL_TREE;
22972 unsigned ix;
22973 cp_default_arg_entry *e;
22974 tree save_ccp, save_ccr;
22976 if (any_erroneous_template_args_p (type))
22978 /* Skip default arguments, NSDMIs, etc, in order to improve
22979 error recovery (c++/71169, c++/71832). */
22980 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22981 vec_safe_truncate (unparsed_nsdmis, 0);
22982 vec_safe_truncate (unparsed_classes, 0);
22983 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22986 /* In a first pass, parse default arguments to the functions.
22987 Then, in a second pass, parse the bodies of the functions.
22988 This two-phased approach handles cases like:
22990 struct S {
22991 void f() { g(); }
22992 void g(int i = 3);
22996 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22998 decl = e->decl;
22999 /* If there are default arguments that have not yet been processed,
23000 take care of them now. */
23001 if (class_type != e->class_type)
23003 if (pushed_scope)
23004 pop_scope (pushed_scope);
23005 class_type = e->class_type;
23006 pushed_scope = push_scope (class_type);
23008 /* Make sure that any template parameters are in scope. */
23009 maybe_begin_member_template_processing (decl);
23010 /* Parse the default argument expressions. */
23011 cp_parser_late_parsing_default_args (parser, decl);
23012 /* Remove any template parameters from the symbol table. */
23013 maybe_end_member_template_processing ();
23015 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23016 /* Now parse any NSDMIs. */
23017 save_ccp = current_class_ptr;
23018 save_ccr = current_class_ref;
23019 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23021 if (class_type != DECL_CONTEXT (decl))
23023 if (pushed_scope)
23024 pop_scope (pushed_scope);
23025 class_type = DECL_CONTEXT (decl);
23026 pushed_scope = push_scope (class_type);
23028 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23029 cp_parser_late_parsing_nsdmi (parser, decl);
23031 vec_safe_truncate (unparsed_nsdmis, 0);
23032 current_class_ptr = save_ccp;
23033 current_class_ref = save_ccr;
23034 if (pushed_scope)
23035 pop_scope (pushed_scope);
23037 /* Now do some post-NSDMI bookkeeping. */
23038 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23039 after_nsdmi_defaulted_late_checks (class_type);
23040 vec_safe_truncate (unparsed_classes, 0);
23041 after_nsdmi_defaulted_late_checks (type);
23043 /* Now parse the body of the functions. */
23044 if (flag_openmp)
23046 /* OpenMP UDRs need to be parsed before all other functions. */
23047 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23048 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23049 cp_parser_late_parsing_for_member (parser, decl);
23050 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23051 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23052 cp_parser_late_parsing_for_member (parser, decl);
23054 else
23055 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23056 cp_parser_late_parsing_for_member (parser, decl);
23057 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23059 else
23060 vec_safe_push (unparsed_classes, type);
23062 /* Put back any saved access checks. */
23063 pop_deferring_access_checks ();
23065 /* Restore saved state. */
23066 parser->in_switch_statement_p = in_switch_statement_p;
23067 parser->in_statement = in_statement;
23068 parser->in_function_body = saved_in_function_body;
23069 parser->num_template_parameter_lists
23070 = saved_num_template_parameter_lists;
23071 parser->in_unbraced_linkage_specification_p
23072 = saved_in_unbraced_linkage_specification_p;
23074 return type;
23077 static tree
23078 cp_parser_class_specifier (cp_parser* parser)
23080 tree ret;
23081 timevar_push (TV_PARSE_STRUCT);
23082 ret = cp_parser_class_specifier_1 (parser);
23083 timevar_pop (TV_PARSE_STRUCT);
23084 return ret;
23087 /* Parse a class-head.
23089 class-head:
23090 class-key identifier [opt] base-clause [opt]
23091 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23092 class-key nested-name-specifier [opt] template-id
23093 base-clause [opt]
23095 class-virt-specifier:
23096 final
23098 GNU Extensions:
23099 class-key attributes identifier [opt] base-clause [opt]
23100 class-key attributes nested-name-specifier identifier base-clause [opt]
23101 class-key attributes nested-name-specifier [opt] template-id
23102 base-clause [opt]
23104 Upon return BASES is initialized to the list of base classes (or
23105 NULL, if there are none) in the same form returned by
23106 cp_parser_base_clause.
23108 Returns the TYPE of the indicated class. Sets
23109 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23110 involving a nested-name-specifier was used, and FALSE otherwise.
23112 Returns error_mark_node if this is not a class-head.
23114 Returns NULL_TREE if the class-head is syntactically valid, but
23115 semantically invalid in a way that means we should skip the entire
23116 body of the class. */
23118 static tree
23119 cp_parser_class_head (cp_parser* parser,
23120 bool* nested_name_specifier_p)
23122 tree nested_name_specifier;
23123 enum tag_types class_key;
23124 tree id = NULL_TREE;
23125 tree type = NULL_TREE;
23126 tree attributes;
23127 tree bases;
23128 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23129 bool template_id_p = false;
23130 bool qualified_p = false;
23131 bool invalid_nested_name_p = false;
23132 bool invalid_explicit_specialization_p = false;
23133 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23134 tree pushed_scope = NULL_TREE;
23135 unsigned num_templates;
23136 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23137 /* Assume no nested-name-specifier will be present. */
23138 *nested_name_specifier_p = false;
23139 /* Assume no template parameter lists will be used in defining the
23140 type. */
23141 num_templates = 0;
23142 parser->colon_corrects_to_scope_p = false;
23144 /* Look for the class-key. */
23145 class_key = cp_parser_class_key (parser);
23146 if (class_key == none_type)
23147 return error_mark_node;
23149 location_t class_head_start_location = input_location;
23151 /* Parse the attributes. */
23152 attributes = cp_parser_attributes_opt (parser);
23154 /* If the next token is `::', that is invalid -- but sometimes
23155 people do try to write:
23157 struct ::S {};
23159 Handle this gracefully by accepting the extra qualifier, and then
23160 issuing an error about it later if this really is a
23161 class-head. If it turns out just to be an elaborated type
23162 specifier, remain silent. */
23163 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23164 qualified_p = true;
23166 push_deferring_access_checks (dk_no_check);
23168 /* Determine the name of the class. Begin by looking for an
23169 optional nested-name-specifier. */
23170 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23171 nested_name_specifier
23172 = cp_parser_nested_name_specifier_opt (parser,
23173 /*typename_keyword_p=*/false,
23174 /*check_dependency_p=*/false,
23175 /*type_p=*/true,
23176 /*is_declaration=*/false);
23177 /* If there was a nested-name-specifier, then there *must* be an
23178 identifier. */
23180 cp_token *bad_template_keyword = NULL;
23182 if (nested_name_specifier)
23184 type_start_token = cp_lexer_peek_token (parser->lexer);
23185 /* Although the grammar says `identifier', it really means
23186 `class-name' or `template-name'. You are only allowed to
23187 define a class that has already been declared with this
23188 syntax.
23190 The proposed resolution for Core Issue 180 says that wherever
23191 you see `class T::X' you should treat `X' as a type-name.
23193 It is OK to define an inaccessible class; for example:
23195 class A { class B; };
23196 class A::B {};
23198 We do not know if we will see a class-name, or a
23199 template-name. We look for a class-name first, in case the
23200 class-name is a template-id; if we looked for the
23201 template-name first we would stop after the template-name. */
23202 cp_parser_parse_tentatively (parser);
23203 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23204 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23205 type = cp_parser_class_name (parser,
23206 /*typename_keyword_p=*/false,
23207 /*template_keyword_p=*/false,
23208 class_type,
23209 /*check_dependency_p=*/false,
23210 /*class_head_p=*/true,
23211 /*is_declaration=*/false);
23212 /* If that didn't work, ignore the nested-name-specifier. */
23213 if (!cp_parser_parse_definitely (parser))
23215 invalid_nested_name_p = true;
23216 type_start_token = cp_lexer_peek_token (parser->lexer);
23217 id = cp_parser_identifier (parser);
23218 if (id == error_mark_node)
23219 id = NULL_TREE;
23221 /* If we could not find a corresponding TYPE, treat this
23222 declaration like an unqualified declaration. */
23223 if (type == error_mark_node)
23224 nested_name_specifier = NULL_TREE;
23225 /* Otherwise, count the number of templates used in TYPE and its
23226 containing scopes. */
23227 else
23228 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23230 /* Otherwise, the identifier is optional. */
23231 else
23233 /* We don't know whether what comes next is a template-id,
23234 an identifier, or nothing at all. */
23235 cp_parser_parse_tentatively (parser);
23236 /* Check for a template-id. */
23237 type_start_token = cp_lexer_peek_token (parser->lexer);
23238 id = cp_parser_template_id (parser,
23239 /*template_keyword_p=*/false,
23240 /*check_dependency_p=*/true,
23241 class_key,
23242 /*is_declaration=*/true);
23243 /* If that didn't work, it could still be an identifier. */
23244 if (!cp_parser_parse_definitely (parser))
23246 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23248 type_start_token = cp_lexer_peek_token (parser->lexer);
23249 id = cp_parser_identifier (parser);
23251 else
23252 id = NULL_TREE;
23254 else
23256 template_id_p = true;
23257 ++num_templates;
23261 pop_deferring_access_checks ();
23263 if (id)
23265 cp_parser_check_for_invalid_template_id (parser, id,
23266 class_key,
23267 type_start_token->location);
23269 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23271 /* If it's not a `:' or a `{' then we can't really be looking at a
23272 class-head, since a class-head only appears as part of a
23273 class-specifier. We have to detect this situation before calling
23274 xref_tag, since that has irreversible side-effects. */
23275 if (!cp_parser_next_token_starts_class_definition_p (parser))
23277 cp_parser_error (parser, "expected %<{%> or %<:%>");
23278 type = error_mark_node;
23279 goto out;
23282 /* At this point, we're going ahead with the class-specifier, even
23283 if some other problem occurs. */
23284 cp_parser_commit_to_tentative_parse (parser);
23285 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23287 cp_parser_error (parser,
23288 "cannot specify %<override%> for a class");
23289 type = error_mark_node;
23290 goto out;
23292 /* Issue the error about the overly-qualified name now. */
23293 if (qualified_p)
23295 cp_parser_error (parser,
23296 "global qualification of class name is invalid");
23297 type = error_mark_node;
23298 goto out;
23300 else if (invalid_nested_name_p)
23302 cp_parser_error (parser,
23303 "qualified name does not name a class");
23304 type = error_mark_node;
23305 goto out;
23307 else if (nested_name_specifier)
23309 tree scope;
23311 if (bad_template_keyword)
23312 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23313 keyword template shall not appear at the top level. */
23314 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23315 "keyword %<template%> not allowed in class-head-name");
23317 /* Reject typedef-names in class heads. */
23318 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23320 error_at (type_start_token->location,
23321 "invalid class name in declaration of %qD",
23322 type);
23323 type = NULL_TREE;
23324 goto done;
23327 /* Figure out in what scope the declaration is being placed. */
23328 scope = current_scope ();
23329 /* If that scope does not contain the scope in which the
23330 class was originally declared, the program is invalid. */
23331 if (scope && !is_ancestor (scope, nested_name_specifier))
23333 if (at_namespace_scope_p ())
23334 error_at (type_start_token->location,
23335 "declaration of %qD in namespace %qD which does not "
23336 "enclose %qD",
23337 type, scope, nested_name_specifier);
23338 else
23339 error_at (type_start_token->location,
23340 "declaration of %qD in %qD which does not enclose %qD",
23341 type, scope, nested_name_specifier);
23342 type = NULL_TREE;
23343 goto done;
23345 /* [dcl.meaning]
23347 A declarator-id shall not be qualified except for the
23348 definition of a ... nested class outside of its class
23349 ... [or] the definition or explicit instantiation of a
23350 class member of a namespace outside of its namespace. */
23351 if (scope == nested_name_specifier)
23353 permerror (nested_name_specifier_token_start->location,
23354 "extra qualification not allowed");
23355 nested_name_specifier = NULL_TREE;
23356 num_templates = 0;
23359 /* An explicit-specialization must be preceded by "template <>". If
23360 it is not, try to recover gracefully. */
23361 if (at_namespace_scope_p ()
23362 && parser->num_template_parameter_lists == 0
23363 && !processing_template_parmlist
23364 && template_id_p)
23366 /* Build a location of this form:
23367 struct typename <ARGS>
23368 ^~~~~~~~~~~~~~~~~~~~~~
23369 with caret==start at the start token, and
23370 finishing at the end of the type. */
23371 location_t reported_loc
23372 = make_location (class_head_start_location,
23373 class_head_start_location,
23374 get_finish (type_start_token->location));
23375 rich_location richloc (line_table, reported_loc);
23376 richloc.add_fixit_insert_before (class_head_start_location,
23377 "template <> ");
23378 error_at (&richloc,
23379 "an explicit specialization must be preceded by"
23380 " %<template <>%>");
23381 invalid_explicit_specialization_p = true;
23382 /* Take the same action that would have been taken by
23383 cp_parser_explicit_specialization. */
23384 ++parser->num_template_parameter_lists;
23385 begin_specialization ();
23387 /* There must be no "return" statements between this point and the
23388 end of this function; set "type "to the correct return value and
23389 use "goto done;" to return. */
23390 /* Make sure that the right number of template parameters were
23391 present. */
23392 if (!cp_parser_check_template_parameters (parser, num_templates,
23393 template_id_p,
23394 type_start_token->location,
23395 /*declarator=*/NULL))
23397 /* If something went wrong, there is no point in even trying to
23398 process the class-definition. */
23399 type = NULL_TREE;
23400 goto done;
23403 /* Look up the type. */
23404 if (template_id_p)
23406 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23407 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23408 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23410 error_at (type_start_token->location,
23411 "function template %qD redeclared as a class template", id);
23412 type = error_mark_node;
23414 else
23416 type = TREE_TYPE (id);
23417 type = maybe_process_partial_specialization (type);
23419 /* Check the scope while we still know whether or not we had a
23420 nested-name-specifier. */
23421 if (type != error_mark_node)
23422 check_unqualified_spec_or_inst (type, type_start_token->location);
23424 if (nested_name_specifier)
23425 pushed_scope = push_scope (nested_name_specifier);
23427 else if (nested_name_specifier)
23429 tree class_type;
23431 /* Given:
23433 template <typename T> struct S { struct T };
23434 template <typename T> struct S<T>::T { };
23436 we will get a TYPENAME_TYPE when processing the definition of
23437 `S::T'. We need to resolve it to the actual type before we
23438 try to define it. */
23439 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23441 class_type = resolve_typename_type (TREE_TYPE (type),
23442 /*only_current_p=*/false);
23443 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23444 type = TYPE_NAME (class_type);
23445 else
23447 cp_parser_error (parser, "could not resolve typename type");
23448 type = error_mark_node;
23452 if (maybe_process_partial_specialization (TREE_TYPE (type))
23453 == error_mark_node)
23455 type = NULL_TREE;
23456 goto done;
23459 class_type = current_class_type;
23460 /* Enter the scope indicated by the nested-name-specifier. */
23461 pushed_scope = push_scope (nested_name_specifier);
23462 /* Get the canonical version of this type. */
23463 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23464 /* Call push_template_decl if it seems like we should be defining a
23465 template either from the template headers or the type we're
23466 defining, so that we diagnose both extra and missing headers. */
23467 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23468 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23469 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23471 type = push_template_decl (type);
23472 if (type == error_mark_node)
23474 type = NULL_TREE;
23475 goto done;
23479 type = TREE_TYPE (type);
23480 *nested_name_specifier_p = true;
23482 else /* The name is not a nested name. */
23484 /* If the class was unnamed, create a dummy name. */
23485 if (!id)
23486 id = make_anon_name ();
23487 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23488 ? ts_within_enclosing_non_class
23489 : ts_current);
23490 type = xref_tag (class_key, id, tag_scope,
23491 parser->num_template_parameter_lists);
23494 /* Indicate whether this class was declared as a `class' or as a
23495 `struct'. */
23496 if (TREE_CODE (type) == RECORD_TYPE)
23497 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23498 cp_parser_check_class_key (class_key, type);
23500 /* If this type was already complete, and we see another definition,
23501 that's an error. */
23502 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23504 error_at (type_start_token->location, "redefinition of %q#T",
23505 type);
23506 inform (location_of (type), "previous definition of %q#T",
23507 type);
23508 type = NULL_TREE;
23509 goto done;
23511 else if (type == error_mark_node)
23512 type = NULL_TREE;
23514 if (type)
23516 /* Apply attributes now, before any use of the class as a template
23517 argument in its base list. */
23518 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23519 fixup_attribute_variants (type);
23522 /* We will have entered the scope containing the class; the names of
23523 base classes should be looked up in that context. For example:
23525 struct A { struct B {}; struct C; };
23526 struct A::C : B {};
23528 is valid. */
23530 /* Get the list of base-classes, if there is one. */
23531 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23533 /* PR59482: enter the class scope so that base-specifiers are looked
23534 up correctly. */
23535 if (type)
23536 pushclass (type);
23537 bases = cp_parser_base_clause (parser);
23538 /* PR59482: get out of the previously pushed class scope so that the
23539 subsequent pops pop the right thing. */
23540 if (type)
23541 popclass ();
23543 else
23544 bases = NULL_TREE;
23546 /* If we're really defining a class, process the base classes.
23547 If they're invalid, fail. */
23548 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23549 xref_basetypes (type, bases);
23551 done:
23552 /* Leave the scope given by the nested-name-specifier. We will
23553 enter the class scope itself while processing the members. */
23554 if (pushed_scope)
23555 pop_scope (pushed_scope);
23557 if (invalid_explicit_specialization_p)
23559 end_specialization ();
23560 --parser->num_template_parameter_lists;
23563 if (type)
23564 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23565 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23566 CLASSTYPE_FINAL (type) = 1;
23567 out:
23568 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23569 return type;
23572 /* Parse a class-key.
23574 class-key:
23575 class
23576 struct
23577 union
23579 Returns the kind of class-key specified, or none_type to indicate
23580 error. */
23582 static enum tag_types
23583 cp_parser_class_key (cp_parser* parser)
23585 cp_token *token;
23586 enum tag_types tag_type;
23588 /* Look for the class-key. */
23589 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23590 if (!token)
23591 return none_type;
23593 /* Check to see if the TOKEN is a class-key. */
23594 tag_type = cp_parser_token_is_class_key (token);
23595 if (!tag_type)
23596 cp_parser_error (parser, "expected class-key");
23597 return tag_type;
23600 /* Parse a type-parameter-key.
23602 type-parameter-key:
23603 class
23604 typename
23607 static void
23608 cp_parser_type_parameter_key (cp_parser* parser)
23610 /* Look for the type-parameter-key. */
23611 enum tag_types tag_type = none_type;
23612 cp_token *token = cp_lexer_peek_token (parser->lexer);
23613 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23615 cp_lexer_consume_token (parser->lexer);
23616 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23617 /* typename is not allowed in a template template parameter
23618 by the standard until C++17. */
23619 pedwarn (token->location, OPT_Wpedantic,
23620 "ISO C++ forbids typename key in template template parameter;"
23621 " use -std=c++17 or -std=gnu++17");
23623 else
23624 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23626 return;
23629 /* Parse an (optional) member-specification.
23631 member-specification:
23632 member-declaration member-specification [opt]
23633 access-specifier : member-specification [opt] */
23635 static void
23636 cp_parser_member_specification_opt (cp_parser* parser)
23638 while (true)
23640 cp_token *token;
23641 enum rid keyword;
23643 /* Peek at the next token. */
23644 token = cp_lexer_peek_token (parser->lexer);
23645 /* If it's a `}', or EOF then we've seen all the members. */
23646 if (token->type == CPP_CLOSE_BRACE
23647 || token->type == CPP_EOF
23648 || token->type == CPP_PRAGMA_EOL)
23649 break;
23651 /* See if this token is a keyword. */
23652 keyword = token->keyword;
23653 switch (keyword)
23655 case RID_PUBLIC:
23656 case RID_PROTECTED:
23657 case RID_PRIVATE:
23658 /* Consume the access-specifier. */
23659 cp_lexer_consume_token (parser->lexer);
23660 /* Remember which access-specifier is active. */
23661 current_access_specifier = token->u.value;
23662 /* Look for the `:'. */
23663 cp_parser_require (parser, CPP_COLON, RT_COLON);
23664 break;
23666 default:
23667 /* Accept #pragmas at class scope. */
23668 if (token->type == CPP_PRAGMA)
23670 cp_parser_pragma (parser, pragma_member, NULL);
23671 break;
23674 /* Otherwise, the next construction must be a
23675 member-declaration. */
23676 cp_parser_member_declaration (parser);
23681 /* Parse a member-declaration.
23683 member-declaration:
23684 decl-specifier-seq [opt] member-declarator-list [opt] ;
23685 function-definition ; [opt]
23686 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23687 using-declaration
23688 template-declaration
23689 alias-declaration
23691 member-declarator-list:
23692 member-declarator
23693 member-declarator-list , member-declarator
23695 member-declarator:
23696 declarator pure-specifier [opt]
23697 declarator constant-initializer [opt]
23698 identifier [opt] : constant-expression
23700 GNU Extensions:
23702 member-declaration:
23703 __extension__ member-declaration
23705 member-declarator:
23706 declarator attributes [opt] pure-specifier [opt]
23707 declarator attributes [opt] constant-initializer [opt]
23708 identifier [opt] attributes [opt] : constant-expression
23710 C++0x Extensions:
23712 member-declaration:
23713 static_assert-declaration */
23715 static void
23716 cp_parser_member_declaration (cp_parser* parser)
23718 cp_decl_specifier_seq decl_specifiers;
23719 tree prefix_attributes;
23720 tree decl;
23721 int declares_class_or_enum;
23722 bool friend_p;
23723 cp_token *token = NULL;
23724 cp_token *decl_spec_token_start = NULL;
23725 cp_token *initializer_token_start = NULL;
23726 int saved_pedantic;
23727 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23729 /* Check for the `__extension__' keyword. */
23730 if (cp_parser_extension_opt (parser, &saved_pedantic))
23732 /* Recurse. */
23733 cp_parser_member_declaration (parser);
23734 /* Restore the old value of the PEDANTIC flag. */
23735 pedantic = saved_pedantic;
23737 return;
23740 /* Check for a template-declaration. */
23741 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23743 /* An explicit specialization here is an error condition, and we
23744 expect the specialization handler to detect and report this. */
23745 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23746 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23747 cp_parser_explicit_specialization (parser);
23748 else
23749 cp_parser_template_declaration (parser, /*member_p=*/true);
23751 return;
23753 /* Check for a template introduction. */
23754 else if (cp_parser_template_declaration_after_export (parser, true))
23755 return;
23757 /* Check for a using-declaration. */
23758 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23760 if (cxx_dialect < cxx11)
23762 /* Parse the using-declaration. */
23763 cp_parser_using_declaration (parser,
23764 /*access_declaration_p=*/false);
23765 return;
23767 else
23769 tree decl;
23770 bool alias_decl_expected;
23771 cp_parser_parse_tentatively (parser);
23772 decl = cp_parser_alias_declaration (parser);
23773 /* Note that if we actually see the '=' token after the
23774 identifier, cp_parser_alias_declaration commits the
23775 tentative parse. In that case, we really expect an
23776 alias-declaration. Otherwise, we expect a using
23777 declaration. */
23778 alias_decl_expected =
23779 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23780 cp_parser_parse_definitely (parser);
23782 if (alias_decl_expected)
23783 finish_member_declaration (decl);
23784 else
23785 cp_parser_using_declaration (parser,
23786 /*access_declaration_p=*/false);
23787 return;
23791 /* Check for @defs. */
23792 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23794 tree ivar, member;
23795 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23796 ivar = ivar_chains;
23797 while (ivar)
23799 member = ivar;
23800 ivar = TREE_CHAIN (member);
23801 TREE_CHAIN (member) = NULL_TREE;
23802 finish_member_declaration (member);
23804 return;
23807 /* If the next token is `static_assert' we have a static assertion. */
23808 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23810 cp_parser_static_assert (parser, /*member_p=*/true);
23811 return;
23814 parser->colon_corrects_to_scope_p = false;
23816 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23817 goto out;
23819 /* Parse the decl-specifier-seq. */
23820 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23821 cp_parser_decl_specifier_seq (parser,
23822 CP_PARSER_FLAGS_OPTIONAL,
23823 &decl_specifiers,
23824 &declares_class_or_enum);
23825 /* Check for an invalid type-name. */
23826 if (!decl_specifiers.any_type_specifiers_p
23827 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23828 goto out;
23829 /* If there is no declarator, then the decl-specifier-seq should
23830 specify a type. */
23831 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23833 /* If there was no decl-specifier-seq, and the next token is a
23834 `;', then we have something like:
23836 struct S { ; };
23838 [class.mem]
23840 Each member-declaration shall declare at least one member
23841 name of the class. */
23842 if (!decl_specifiers.any_specifiers_p)
23844 cp_token *token = cp_lexer_peek_token (parser->lexer);
23845 if (!in_system_header_at (token->location))
23847 gcc_rich_location richloc (token->location);
23848 richloc.add_fixit_remove ();
23849 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23852 else
23854 tree type;
23856 /* See if this declaration is a friend. */
23857 friend_p = cp_parser_friend_p (&decl_specifiers);
23858 /* If there were decl-specifiers, check to see if there was
23859 a class-declaration. */
23860 type = check_tag_decl (&decl_specifiers,
23861 /*explicit_type_instantiation_p=*/false);
23862 /* Nested classes have already been added to the class, but
23863 a `friend' needs to be explicitly registered. */
23864 if (friend_p)
23866 /* If the `friend' keyword was present, the friend must
23867 be introduced with a class-key. */
23868 if (!declares_class_or_enum && cxx_dialect < cxx11)
23869 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23870 "in C++03 a class-key must be used "
23871 "when declaring a friend");
23872 /* In this case:
23874 template <typename T> struct A {
23875 friend struct A<T>::B;
23878 A<T>::B will be represented by a TYPENAME_TYPE, and
23879 therefore not recognized by check_tag_decl. */
23880 if (!type)
23882 type = decl_specifiers.type;
23883 if (type && TREE_CODE (type) == TYPE_DECL)
23884 type = TREE_TYPE (type);
23886 if (!type || !TYPE_P (type))
23887 error_at (decl_spec_token_start->location,
23888 "friend declaration does not name a class or "
23889 "function");
23890 else
23891 make_friend_class (current_class_type, type,
23892 /*complain=*/true);
23894 /* If there is no TYPE, an error message will already have
23895 been issued. */
23896 else if (!type || type == error_mark_node)
23898 /* An anonymous aggregate has to be handled specially; such
23899 a declaration really declares a data member (with a
23900 particular type), as opposed to a nested class. */
23901 else if (ANON_AGGR_TYPE_P (type))
23903 /* C++11 9.5/6. */
23904 if (decl_specifiers.storage_class != sc_none)
23905 error_at (decl_spec_token_start->location,
23906 "a storage class on an anonymous aggregate "
23907 "in class scope is not allowed");
23909 /* Remove constructors and such from TYPE, now that we
23910 know it is an anonymous aggregate. */
23911 fixup_anonymous_aggr (type);
23912 /* And make the corresponding data member. */
23913 decl = build_decl (decl_spec_token_start->location,
23914 FIELD_DECL, NULL_TREE, type);
23915 /* Add it to the class. */
23916 finish_member_declaration (decl);
23918 else
23919 cp_parser_check_access_in_redeclaration
23920 (TYPE_NAME (type),
23921 decl_spec_token_start->location);
23924 else
23926 bool assume_semicolon = false;
23928 /* Clear attributes from the decl_specifiers but keep them
23929 around as prefix attributes that apply them to the entity
23930 being declared. */
23931 prefix_attributes = decl_specifiers.attributes;
23932 decl_specifiers.attributes = NULL_TREE;
23934 /* See if these declarations will be friends. */
23935 friend_p = cp_parser_friend_p (&decl_specifiers);
23937 /* Keep going until we hit the `;' at the end of the
23938 declaration. */
23939 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23941 tree attributes = NULL_TREE;
23942 tree first_attribute;
23943 tree initializer;
23944 bool named_bitfld = false;
23946 /* Peek at the next token. */
23947 token = cp_lexer_peek_token (parser->lexer);
23949 /* The following code wants to know early if it is a bit-field
23950 or some other declaration. Attributes can appear before
23951 the `:' token. Skip over them without consuming any tokens
23952 to peek if they are followed by `:'. */
23953 if (cp_next_tokens_can_be_attribute_p (parser)
23954 || (token->type == CPP_NAME
23955 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23956 && (named_bitfld = true)))
23958 size_t n
23959 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23960 token = cp_lexer_peek_nth_token (parser->lexer, n);
23963 /* Check for a bitfield declaration. */
23964 if (token->type == CPP_COLON
23965 || (token->type == CPP_NAME
23966 && token == cp_lexer_peek_token (parser->lexer)
23967 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23968 && (named_bitfld = true)))
23970 tree identifier;
23971 tree width;
23972 tree late_attributes = NULL_TREE;
23974 if (named_bitfld)
23975 identifier = cp_parser_identifier (parser);
23976 else
23977 identifier = NULL_TREE;
23979 /* Look for attributes that apply to the bitfield. */
23980 attributes = cp_parser_attributes_opt (parser);
23982 /* Consume the `:' token. */
23983 cp_lexer_consume_token (parser->lexer);
23985 /* Get the width of the bitfield. */
23986 width = cp_parser_constant_expression (parser, false, NULL,
23987 cxx_dialect >= cxx11);
23989 /* In C++2A and as extension for C++11 and above we allow
23990 default member initializers for bit-fields. */
23991 initializer = NULL_TREE;
23992 if (cxx_dialect >= cxx11
23993 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23994 || cp_lexer_next_token_is (parser->lexer,
23995 CPP_OPEN_BRACE)))
23997 location_t loc
23998 = cp_lexer_peek_token (parser->lexer)->location;
23999 if (cxx_dialect < cxx2a
24000 && !in_system_header_at (loc)
24001 && identifier != NULL_TREE)
24002 pedwarn (loc, 0,
24003 "default member initializers for bit-fields "
24004 "only available with -std=c++2a or "
24005 "-std=gnu++2a");
24007 initializer = cp_parser_save_nsdmi (parser);
24008 if (identifier == NULL_TREE)
24010 error_at (loc, "default member initializer for "
24011 "unnamed bit-field");
24012 initializer = NULL_TREE;
24015 else
24017 /* Look for attributes that apply to the bitfield after
24018 the `:' token and width. This is where GCC used to
24019 parse attributes in the past, pedwarn if there is
24020 a std attribute. */
24021 if (cp_next_tokens_can_be_std_attribute_p (parser))
24022 pedwarn (input_location, OPT_Wpedantic,
24023 "ISO C++ allows bit-field attributes only "
24024 "before the %<:%> token");
24026 late_attributes = cp_parser_attributes_opt (parser);
24029 attributes = attr_chainon (attributes, late_attributes);
24031 /* Remember which attributes are prefix attributes and
24032 which are not. */
24033 first_attribute = attributes;
24034 /* Combine the attributes. */
24035 attributes = attr_chainon (prefix_attributes, attributes);
24037 /* Create the bitfield declaration. */
24038 decl = grokbitfield (identifier
24039 ? make_id_declarator (NULL_TREE,
24040 identifier,
24041 sfk_none)
24042 : NULL,
24043 &decl_specifiers,
24044 width, initializer,
24045 attributes);
24047 else
24049 cp_declarator *declarator;
24050 tree asm_specification;
24051 int ctor_dtor_or_conv_p;
24053 /* Parse the declarator. */
24054 declarator
24055 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24056 &ctor_dtor_or_conv_p,
24057 /*parenthesized_p=*/NULL,
24058 /*member_p=*/true,
24059 friend_p);
24061 /* If something went wrong parsing the declarator, make sure
24062 that we at least consume some tokens. */
24063 if (declarator == cp_error_declarator)
24065 /* Skip to the end of the statement. */
24066 cp_parser_skip_to_end_of_statement (parser);
24067 /* If the next token is not a semicolon, that is
24068 probably because we just skipped over the body of
24069 a function. So, we consume a semicolon if
24070 present, but do not issue an error message if it
24071 is not present. */
24072 if (cp_lexer_next_token_is (parser->lexer,
24073 CPP_SEMICOLON))
24074 cp_lexer_consume_token (parser->lexer);
24075 goto out;
24078 if (declares_class_or_enum & 2)
24079 cp_parser_check_for_definition_in_return_type
24080 (declarator, decl_specifiers.type,
24081 decl_specifiers.locations[ds_type_spec]);
24083 /* Look for an asm-specification. */
24084 asm_specification = cp_parser_asm_specification_opt (parser);
24085 /* Look for attributes that apply to the declaration. */
24086 attributes = cp_parser_attributes_opt (parser);
24087 /* Remember which attributes are prefix attributes and
24088 which are not. */
24089 first_attribute = attributes;
24090 /* Combine the attributes. */
24091 attributes = attr_chainon (prefix_attributes, attributes);
24093 /* If it's an `=', then we have a constant-initializer or a
24094 pure-specifier. It is not correct to parse the
24095 initializer before registering the member declaration
24096 since the member declaration should be in scope while
24097 its initializer is processed. However, the rest of the
24098 front end does not yet provide an interface that allows
24099 us to handle this correctly. */
24100 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24102 /* In [class.mem]:
24104 A pure-specifier shall be used only in the declaration of
24105 a virtual function.
24107 A member-declarator can contain a constant-initializer
24108 only if it declares a static member of integral or
24109 enumeration type.
24111 Therefore, if the DECLARATOR is for a function, we look
24112 for a pure-specifier; otherwise, we look for a
24113 constant-initializer. When we call `grokfield', it will
24114 perform more stringent semantics checks. */
24115 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24116 if (function_declarator_p (declarator)
24117 || (decl_specifiers.type
24118 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24119 && declarator->kind == cdk_id
24120 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24121 == FUNCTION_TYPE)))
24122 initializer = cp_parser_pure_specifier (parser);
24123 else if (decl_specifiers.storage_class != sc_static)
24124 initializer = cp_parser_save_nsdmi (parser);
24125 else if (cxx_dialect >= cxx11)
24127 bool nonconst;
24128 /* Don't require a constant rvalue in C++11, since we
24129 might want a reference constant. We'll enforce
24130 constancy later. */
24131 cp_lexer_consume_token (parser->lexer);
24132 /* Parse the initializer. */
24133 initializer = cp_parser_initializer_clause (parser,
24134 &nonconst);
24136 else
24137 /* Parse the initializer. */
24138 initializer = cp_parser_constant_initializer (parser);
24140 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24141 && !function_declarator_p (declarator))
24143 bool x;
24144 if (decl_specifiers.storage_class != sc_static)
24145 initializer = cp_parser_save_nsdmi (parser);
24146 else
24147 initializer = cp_parser_initializer (parser, &x, &x);
24149 /* Otherwise, there is no initializer. */
24150 else
24151 initializer = NULL_TREE;
24153 /* See if we are probably looking at a function
24154 definition. We are certainly not looking at a
24155 member-declarator. Calling `grokfield' has
24156 side-effects, so we must not do it unless we are sure
24157 that we are looking at a member-declarator. */
24158 if (cp_parser_token_starts_function_definition_p
24159 (cp_lexer_peek_token (parser->lexer)))
24161 /* The grammar does not allow a pure-specifier to be
24162 used when a member function is defined. (It is
24163 possible that this fact is an oversight in the
24164 standard, since a pure function may be defined
24165 outside of the class-specifier. */
24166 if (initializer && initializer_token_start)
24167 error_at (initializer_token_start->location,
24168 "pure-specifier on function-definition");
24169 decl = cp_parser_save_member_function_body (parser,
24170 &decl_specifiers,
24171 declarator,
24172 attributes);
24173 if (parser->fully_implicit_function_template_p)
24174 decl = finish_fully_implicit_template (parser, decl);
24175 /* If the member was not a friend, declare it here. */
24176 if (!friend_p)
24177 finish_member_declaration (decl);
24178 /* Peek at the next token. */
24179 token = cp_lexer_peek_token (parser->lexer);
24180 /* If the next token is a semicolon, consume it. */
24181 if (token->type == CPP_SEMICOLON)
24183 location_t semicolon_loc
24184 = cp_lexer_consume_token (parser->lexer)->location;
24185 gcc_rich_location richloc (semicolon_loc);
24186 richloc.add_fixit_remove ();
24187 warning_at (&richloc, OPT_Wextra_semi,
24188 "extra %<;%> after in-class "
24189 "function definition");
24191 goto out;
24193 else
24194 if (declarator->kind == cdk_function)
24195 declarator->id_loc = token->location;
24196 /* Create the declaration. */
24197 decl = grokfield (declarator, &decl_specifiers,
24198 initializer, /*init_const_expr_p=*/true,
24199 asm_specification, attributes);
24200 if (parser->fully_implicit_function_template_p)
24202 if (friend_p)
24203 finish_fully_implicit_template (parser, 0);
24204 else
24205 decl = finish_fully_implicit_template (parser, decl);
24209 cp_finalize_omp_declare_simd (parser, decl);
24210 cp_finalize_oacc_routine (parser, decl, false);
24212 /* Reset PREFIX_ATTRIBUTES. */
24213 if (attributes != error_mark_node)
24215 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24216 attributes = TREE_CHAIN (attributes);
24217 if (attributes)
24218 TREE_CHAIN (attributes) = NULL_TREE;
24221 /* If there is any qualification still in effect, clear it
24222 now; we will be starting fresh with the next declarator. */
24223 parser->scope = NULL_TREE;
24224 parser->qualifying_scope = NULL_TREE;
24225 parser->object_scope = NULL_TREE;
24226 /* If it's a `,', then there are more declarators. */
24227 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24229 cp_lexer_consume_token (parser->lexer);
24230 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24232 cp_token *token = cp_lexer_previous_token (parser->lexer);
24233 gcc_rich_location richloc (token->location);
24234 richloc.add_fixit_remove ();
24235 error_at (&richloc, "stray %<,%> at end of "
24236 "member declaration");
24239 /* If the next token isn't a `;', then we have a parse error. */
24240 else if (cp_lexer_next_token_is_not (parser->lexer,
24241 CPP_SEMICOLON))
24243 /* The next token might be a ways away from where the
24244 actual semicolon is missing. Find the previous token
24245 and use that for our error position. */
24246 cp_token *token = cp_lexer_previous_token (parser->lexer);
24247 gcc_rich_location richloc (token->location);
24248 richloc.add_fixit_insert_after (";");
24249 error_at (&richloc, "expected %<;%> at end of "
24250 "member declaration");
24252 /* Assume that the user meant to provide a semicolon. If
24253 we were to cp_parser_skip_to_end_of_statement, we might
24254 skip to a semicolon inside a member function definition
24255 and issue nonsensical error messages. */
24256 assume_semicolon = true;
24259 if (decl)
24261 /* Add DECL to the list of members. */
24262 if (!friend_p
24263 /* Explicitly include, eg, NSDMIs, for better error
24264 recovery (c++/58650). */
24265 || !DECL_DECLARES_FUNCTION_P (decl))
24266 finish_member_declaration (decl);
24268 if (TREE_CODE (decl) == FUNCTION_DECL)
24269 cp_parser_save_default_args (parser, decl);
24270 else if (TREE_CODE (decl) == FIELD_DECL
24271 && DECL_INITIAL (decl))
24272 /* Add DECL to the queue of NSDMI to be parsed later. */
24273 vec_safe_push (unparsed_nsdmis, decl);
24276 if (assume_semicolon)
24277 goto out;
24281 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24282 out:
24283 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24286 /* Parse a pure-specifier.
24288 pure-specifier:
24291 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24292 Otherwise, ERROR_MARK_NODE is returned. */
24294 static tree
24295 cp_parser_pure_specifier (cp_parser* parser)
24297 cp_token *token;
24299 /* Look for the `=' token. */
24300 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24301 return error_mark_node;
24302 /* Look for the `0' token. */
24303 token = cp_lexer_peek_token (parser->lexer);
24305 if (token->type == CPP_EOF
24306 || token->type == CPP_PRAGMA_EOL)
24307 return error_mark_node;
24309 cp_lexer_consume_token (parser->lexer);
24311 /* Accept = default or = delete in c++0x mode. */
24312 if (token->keyword == RID_DEFAULT
24313 || token->keyword == RID_DELETE)
24315 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24316 return token->u.value;
24319 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24320 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24322 cp_parser_error (parser,
24323 "invalid pure specifier (only %<= 0%> is allowed)");
24324 cp_parser_skip_to_end_of_statement (parser);
24325 return error_mark_node;
24327 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24329 error_at (token->location, "templates may not be %<virtual%>");
24330 return error_mark_node;
24333 return integer_zero_node;
24336 /* Parse a constant-initializer.
24338 constant-initializer:
24339 = constant-expression
24341 Returns a representation of the constant-expression. */
24343 static tree
24344 cp_parser_constant_initializer (cp_parser* parser)
24346 /* Look for the `=' token. */
24347 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24348 return error_mark_node;
24350 /* It is invalid to write:
24352 struct S { static const int i = { 7 }; };
24355 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24357 cp_parser_error (parser,
24358 "a brace-enclosed initializer is not allowed here");
24359 /* Consume the opening brace. */
24360 matching_braces braces;
24361 braces.consume_open (parser);
24362 /* Skip the initializer. */
24363 cp_parser_skip_to_closing_brace (parser);
24364 /* Look for the trailing `}'. */
24365 braces.require_close (parser);
24367 return error_mark_node;
24370 return cp_parser_constant_expression (parser);
24373 /* Derived classes [gram.class.derived] */
24375 /* Parse a base-clause.
24377 base-clause:
24378 : base-specifier-list
24380 base-specifier-list:
24381 base-specifier ... [opt]
24382 base-specifier-list , base-specifier ... [opt]
24384 Returns a TREE_LIST representing the base-classes, in the order in
24385 which they were declared. The representation of each node is as
24386 described by cp_parser_base_specifier.
24388 In the case that no bases are specified, this function will return
24389 NULL_TREE, not ERROR_MARK_NODE. */
24391 static tree
24392 cp_parser_base_clause (cp_parser* parser)
24394 tree bases = NULL_TREE;
24396 /* Look for the `:' that begins the list. */
24397 cp_parser_require (parser, CPP_COLON, RT_COLON);
24399 /* Scan the base-specifier-list. */
24400 while (true)
24402 cp_token *token;
24403 tree base;
24404 bool pack_expansion_p = false;
24406 /* Look for the base-specifier. */
24407 base = cp_parser_base_specifier (parser);
24408 /* Look for the (optional) ellipsis. */
24409 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24411 /* Consume the `...'. */
24412 cp_lexer_consume_token (parser->lexer);
24414 pack_expansion_p = true;
24417 /* Add BASE to the front of the list. */
24418 if (base && base != error_mark_node)
24420 if (pack_expansion_p)
24421 /* Make this a pack expansion type. */
24422 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24424 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24426 TREE_CHAIN (base) = bases;
24427 bases = base;
24430 /* Peek at the next token. */
24431 token = cp_lexer_peek_token (parser->lexer);
24432 /* If it's not a comma, then the list is complete. */
24433 if (token->type != CPP_COMMA)
24434 break;
24435 /* Consume the `,'. */
24436 cp_lexer_consume_token (parser->lexer);
24439 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24440 base class had a qualified name. However, the next name that
24441 appears is certainly not qualified. */
24442 parser->scope = NULL_TREE;
24443 parser->qualifying_scope = NULL_TREE;
24444 parser->object_scope = NULL_TREE;
24446 return nreverse (bases);
24449 /* Parse a base-specifier.
24451 base-specifier:
24452 :: [opt] nested-name-specifier [opt] class-name
24453 virtual access-specifier [opt] :: [opt] nested-name-specifier
24454 [opt] class-name
24455 access-specifier virtual [opt] :: [opt] nested-name-specifier
24456 [opt] class-name
24458 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24459 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24460 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24461 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24463 static tree
24464 cp_parser_base_specifier (cp_parser* parser)
24466 cp_token *token;
24467 bool done = false;
24468 bool virtual_p = false;
24469 bool duplicate_virtual_error_issued_p = false;
24470 bool duplicate_access_error_issued_p = false;
24471 bool class_scope_p, template_p;
24472 tree access = access_default_node;
24473 tree type;
24475 /* Process the optional `virtual' and `access-specifier'. */
24476 while (!done)
24478 /* Peek at the next token. */
24479 token = cp_lexer_peek_token (parser->lexer);
24480 /* Process `virtual'. */
24481 switch (token->keyword)
24483 case RID_VIRTUAL:
24484 /* If `virtual' appears more than once, issue an error. */
24485 if (virtual_p && !duplicate_virtual_error_issued_p)
24487 cp_parser_error (parser,
24488 "%<virtual%> specified more than once in base-specifier");
24489 duplicate_virtual_error_issued_p = true;
24492 virtual_p = true;
24494 /* Consume the `virtual' token. */
24495 cp_lexer_consume_token (parser->lexer);
24497 break;
24499 case RID_PUBLIC:
24500 case RID_PROTECTED:
24501 case RID_PRIVATE:
24502 /* If more than one access specifier appears, issue an
24503 error. */
24504 if (access != access_default_node
24505 && !duplicate_access_error_issued_p)
24507 cp_parser_error (parser,
24508 "more than one access specifier in base-specifier");
24509 duplicate_access_error_issued_p = true;
24512 access = ridpointers[(int) token->keyword];
24514 /* Consume the access-specifier. */
24515 cp_lexer_consume_token (parser->lexer);
24517 break;
24519 default:
24520 done = true;
24521 break;
24524 /* It is not uncommon to see programs mechanically, erroneously, use
24525 the 'typename' keyword to denote (dependent) qualified types
24526 as base classes. */
24527 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24529 token = cp_lexer_peek_token (parser->lexer);
24530 if (!processing_template_decl)
24531 error_at (token->location,
24532 "keyword %<typename%> not allowed outside of templates");
24533 else
24534 error_at (token->location,
24535 "keyword %<typename%> not allowed in this context "
24536 "(the base class is implicitly a type)");
24537 cp_lexer_consume_token (parser->lexer);
24540 /* Look for the optional `::' operator. */
24541 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24542 /* Look for the nested-name-specifier. The simplest way to
24543 implement:
24545 [temp.res]
24547 The keyword `typename' is not permitted in a base-specifier or
24548 mem-initializer; in these contexts a qualified name that
24549 depends on a template-parameter is implicitly assumed to be a
24550 type name.
24552 is to pretend that we have seen the `typename' keyword at this
24553 point. */
24554 cp_parser_nested_name_specifier_opt (parser,
24555 /*typename_keyword_p=*/true,
24556 /*check_dependency_p=*/true,
24557 /*type_p=*/true,
24558 /*is_declaration=*/true);
24559 /* If the base class is given by a qualified name, assume that names
24560 we see are type names or templates, as appropriate. */
24561 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24562 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24564 if (!parser->scope
24565 && cp_lexer_next_token_is_decltype (parser->lexer))
24566 /* DR 950 allows decltype as a base-specifier. */
24567 type = cp_parser_decltype (parser);
24568 else
24570 /* Otherwise, look for the class-name. */
24571 type = cp_parser_class_name (parser,
24572 class_scope_p,
24573 template_p,
24574 typename_type,
24575 /*check_dependency_p=*/true,
24576 /*class_head_p=*/false,
24577 /*is_declaration=*/true);
24578 type = TREE_TYPE (type);
24581 if (type == error_mark_node)
24582 return error_mark_node;
24584 return finish_base_specifier (type, access, virtual_p);
24587 /* Exception handling [gram.exception] */
24589 /* Parse an (optional) noexcept-specification.
24591 noexcept-specification:
24592 noexcept ( constant-expression ) [opt]
24594 If no noexcept-specification is present, returns NULL_TREE.
24595 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24596 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24597 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24598 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24599 in which case a boolean condition is returned instead. */
24601 static tree
24602 cp_parser_noexcept_specification_opt (cp_parser* parser,
24603 bool require_constexpr,
24604 bool* consumed_expr,
24605 bool return_cond)
24607 cp_token *token;
24608 const char *saved_message;
24610 /* Peek at the next token. */
24611 token = cp_lexer_peek_token (parser->lexer);
24613 /* Is it a noexcept-specification? */
24614 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24616 tree expr;
24617 cp_lexer_consume_token (parser->lexer);
24619 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24621 matching_parens parens;
24622 parens.consume_open (parser);
24624 if (require_constexpr)
24626 /* Types may not be defined in an exception-specification. */
24627 saved_message = parser->type_definition_forbidden_message;
24628 parser->type_definition_forbidden_message
24629 = G_("types may not be defined in an exception-specification");
24631 expr = cp_parser_constant_expression (parser);
24633 /* Restore the saved message. */
24634 parser->type_definition_forbidden_message = saved_message;
24636 else
24638 expr = cp_parser_expression (parser);
24639 *consumed_expr = true;
24642 parens.require_close (parser);
24644 else
24646 expr = boolean_true_node;
24647 if (!require_constexpr)
24648 *consumed_expr = false;
24651 /* We cannot build a noexcept-spec right away because this will check
24652 that expr is a constexpr. */
24653 if (!return_cond)
24654 return build_noexcept_spec (expr, tf_warning_or_error);
24655 else
24656 return expr;
24658 else
24659 return NULL_TREE;
24662 /* Parse an (optional) exception-specification.
24664 exception-specification:
24665 throw ( type-id-list [opt] )
24667 Returns a TREE_LIST representing the exception-specification. The
24668 TREE_VALUE of each node is a type. */
24670 static tree
24671 cp_parser_exception_specification_opt (cp_parser* parser)
24673 cp_token *token;
24674 tree type_id_list;
24675 const char *saved_message;
24677 /* Peek at the next token. */
24678 token = cp_lexer_peek_token (parser->lexer);
24680 /* Is it a noexcept-specification? */
24681 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24682 false);
24683 if (type_id_list != NULL_TREE)
24684 return type_id_list;
24686 /* If it's not `throw', then there's no exception-specification. */
24687 if (!cp_parser_is_keyword (token, RID_THROW))
24688 return NULL_TREE;
24690 location_t loc = token->location;
24692 /* Consume the `throw'. */
24693 cp_lexer_consume_token (parser->lexer);
24695 /* Look for the `('. */
24696 matching_parens parens;
24697 parens.require_open (parser);
24699 /* Peek at the next token. */
24700 token = cp_lexer_peek_token (parser->lexer);
24701 /* If it's not a `)', then there is a type-id-list. */
24702 if (token->type != CPP_CLOSE_PAREN)
24704 /* Types may not be defined in an exception-specification. */
24705 saved_message = parser->type_definition_forbidden_message;
24706 parser->type_definition_forbidden_message
24707 = G_("types may not be defined in an exception-specification");
24708 /* Parse the type-id-list. */
24709 type_id_list = cp_parser_type_id_list (parser);
24710 /* Restore the saved message. */
24711 parser->type_definition_forbidden_message = saved_message;
24713 if (cxx_dialect >= cxx17)
24715 error_at (loc, "ISO C++17 does not allow dynamic exception "
24716 "specifications");
24717 type_id_list = NULL_TREE;
24719 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24720 warning_at (loc, OPT_Wdeprecated,
24721 "dynamic exception specifications are deprecated in "
24722 "C++11");
24724 /* In C++17, throw() is equivalent to noexcept (true). throw()
24725 is deprecated in C++11 and above as well, but is still widely used,
24726 so don't warn about it yet. */
24727 else if (cxx_dialect >= cxx17)
24728 type_id_list = noexcept_true_spec;
24729 else
24730 type_id_list = empty_except_spec;
24732 /* Look for the `)'. */
24733 parens.require_close (parser);
24735 return type_id_list;
24738 /* Parse an (optional) type-id-list.
24740 type-id-list:
24741 type-id ... [opt]
24742 type-id-list , type-id ... [opt]
24744 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24745 in the order that the types were presented. */
24747 static tree
24748 cp_parser_type_id_list (cp_parser* parser)
24750 tree types = NULL_TREE;
24752 while (true)
24754 cp_token *token;
24755 tree type;
24757 token = cp_lexer_peek_token (parser->lexer);
24759 /* Get the next type-id. */
24760 type = cp_parser_type_id (parser);
24761 /* Check for invalid 'auto'. */
24762 if (flag_concepts && type_uses_auto (type))
24764 error_at (token->location,
24765 "invalid use of %<auto%> in exception-specification");
24766 type = error_mark_node;
24768 /* Parse the optional ellipsis. */
24769 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24771 /* Consume the `...'. */
24772 cp_lexer_consume_token (parser->lexer);
24774 /* Turn the type into a pack expansion expression. */
24775 type = make_pack_expansion (type);
24777 /* Add it to the list. */
24778 types = add_exception_specifier (types, type, /*complain=*/1);
24779 /* Peek at the next token. */
24780 token = cp_lexer_peek_token (parser->lexer);
24781 /* If it is not a `,', we are done. */
24782 if (token->type != CPP_COMMA)
24783 break;
24784 /* Consume the `,'. */
24785 cp_lexer_consume_token (parser->lexer);
24788 return nreverse (types);
24791 /* Parse a try-block.
24793 try-block:
24794 try compound-statement handler-seq */
24796 static tree
24797 cp_parser_try_block (cp_parser* parser)
24799 tree try_block;
24801 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24802 if (parser->in_function_body
24803 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24804 error ("%<try%> in %<constexpr%> function");
24806 try_block = begin_try_block ();
24807 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24808 finish_try_block (try_block);
24809 cp_parser_handler_seq (parser);
24810 finish_handler_sequence (try_block);
24812 return try_block;
24815 /* Parse a function-try-block.
24817 function-try-block:
24818 try ctor-initializer [opt] function-body handler-seq */
24820 static void
24821 cp_parser_function_try_block (cp_parser* parser)
24823 tree compound_stmt;
24824 tree try_block;
24826 /* Look for the `try' keyword. */
24827 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24828 return;
24829 /* Let the rest of the front end know where we are. */
24830 try_block = begin_function_try_block (&compound_stmt);
24831 /* Parse the function-body. */
24832 cp_parser_ctor_initializer_opt_and_function_body
24833 (parser, /*in_function_try_block=*/true);
24834 /* We're done with the `try' part. */
24835 finish_function_try_block (try_block);
24836 /* Parse the handlers. */
24837 cp_parser_handler_seq (parser);
24838 /* We're done with the handlers. */
24839 finish_function_handler_sequence (try_block, compound_stmt);
24842 /* Parse a handler-seq.
24844 handler-seq:
24845 handler handler-seq [opt] */
24847 static void
24848 cp_parser_handler_seq (cp_parser* parser)
24850 while (true)
24852 cp_token *token;
24854 /* Parse the handler. */
24855 cp_parser_handler (parser);
24856 /* Peek at the next token. */
24857 token = cp_lexer_peek_token (parser->lexer);
24858 /* If it's not `catch' then there are no more handlers. */
24859 if (!cp_parser_is_keyword (token, RID_CATCH))
24860 break;
24864 /* Parse a handler.
24866 handler:
24867 catch ( exception-declaration ) compound-statement */
24869 static void
24870 cp_parser_handler (cp_parser* parser)
24872 tree handler;
24873 tree declaration;
24875 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24876 handler = begin_handler ();
24877 matching_parens parens;
24878 parens.require_open (parser);
24879 declaration = cp_parser_exception_declaration (parser);
24880 finish_handler_parms (declaration, handler);
24881 parens.require_close (parser);
24882 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24883 finish_handler (handler);
24886 /* Parse an exception-declaration.
24888 exception-declaration:
24889 type-specifier-seq declarator
24890 type-specifier-seq abstract-declarator
24891 type-specifier-seq
24894 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24895 ellipsis variant is used. */
24897 static tree
24898 cp_parser_exception_declaration (cp_parser* parser)
24900 cp_decl_specifier_seq type_specifiers;
24901 cp_declarator *declarator;
24902 const char *saved_message;
24904 /* If it's an ellipsis, it's easy to handle. */
24905 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24907 /* Consume the `...' token. */
24908 cp_lexer_consume_token (parser->lexer);
24909 return NULL_TREE;
24912 /* Types may not be defined in exception-declarations. */
24913 saved_message = parser->type_definition_forbidden_message;
24914 parser->type_definition_forbidden_message
24915 = G_("types may not be defined in exception-declarations");
24917 /* Parse the type-specifier-seq. */
24918 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24919 /*is_trailing_return=*/false,
24920 &type_specifiers);
24921 /* If it's a `)', then there is no declarator. */
24922 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24923 declarator = NULL;
24924 else
24925 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24926 /*ctor_dtor_or_conv_p=*/NULL,
24927 /*parenthesized_p=*/NULL,
24928 /*member_p=*/false,
24929 /*friend_p=*/false);
24931 /* Restore the saved message. */
24932 parser->type_definition_forbidden_message = saved_message;
24934 if (!type_specifiers.any_specifiers_p)
24935 return error_mark_node;
24937 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24940 /* Parse a throw-expression.
24942 throw-expression:
24943 throw assignment-expression [opt]
24945 Returns a THROW_EXPR representing the throw-expression. */
24947 static tree
24948 cp_parser_throw_expression (cp_parser* parser)
24950 tree expression;
24951 cp_token* token;
24953 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24954 token = cp_lexer_peek_token (parser->lexer);
24955 /* Figure out whether or not there is an assignment-expression
24956 following the "throw" keyword. */
24957 if (token->type == CPP_COMMA
24958 || token->type == CPP_SEMICOLON
24959 || token->type == CPP_CLOSE_PAREN
24960 || token->type == CPP_CLOSE_SQUARE
24961 || token->type == CPP_CLOSE_BRACE
24962 || token->type == CPP_COLON)
24963 expression = NULL_TREE;
24964 else
24965 expression = cp_parser_assignment_expression (parser);
24967 return build_throw (expression);
24970 /* GNU Extensions */
24972 /* Parse an (optional) asm-specification.
24974 asm-specification:
24975 asm ( string-literal )
24977 If the asm-specification is present, returns a STRING_CST
24978 corresponding to the string-literal. Otherwise, returns
24979 NULL_TREE. */
24981 static tree
24982 cp_parser_asm_specification_opt (cp_parser* parser)
24984 cp_token *token;
24985 tree asm_specification;
24987 /* Peek at the next token. */
24988 token = cp_lexer_peek_token (parser->lexer);
24989 /* If the next token isn't the `asm' keyword, then there's no
24990 asm-specification. */
24991 if (!cp_parser_is_keyword (token, RID_ASM))
24992 return NULL_TREE;
24994 /* Consume the `asm' token. */
24995 cp_lexer_consume_token (parser->lexer);
24996 /* Look for the `('. */
24997 matching_parens parens;
24998 parens.require_open (parser);
25000 /* Look for the string-literal. */
25001 asm_specification = cp_parser_string_literal (parser, false, false);
25003 /* Look for the `)'. */
25004 parens.require_close (parser);
25006 return asm_specification;
25009 /* Parse an asm-operand-list.
25011 asm-operand-list:
25012 asm-operand
25013 asm-operand-list , asm-operand
25015 asm-operand:
25016 string-literal ( expression )
25017 [ string-literal ] string-literal ( expression )
25019 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25020 each node is the expression. The TREE_PURPOSE is itself a
25021 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25022 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25023 is a STRING_CST for the string literal before the parenthesis. Returns
25024 ERROR_MARK_NODE if any of the operands are invalid. */
25026 static tree
25027 cp_parser_asm_operand_list (cp_parser* parser)
25029 tree asm_operands = NULL_TREE;
25030 bool invalid_operands = false;
25032 while (true)
25034 tree string_literal;
25035 tree expression;
25036 tree name;
25038 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25040 /* Consume the `[' token. */
25041 cp_lexer_consume_token (parser->lexer);
25042 /* Read the operand name. */
25043 name = cp_parser_identifier (parser);
25044 if (name != error_mark_node)
25045 name = build_string (IDENTIFIER_LENGTH (name),
25046 IDENTIFIER_POINTER (name));
25047 /* Look for the closing `]'. */
25048 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25050 else
25051 name = NULL_TREE;
25052 /* Look for the string-literal. */
25053 string_literal = cp_parser_string_literal (parser, false, false);
25055 /* Look for the `('. */
25056 matching_parens parens;
25057 parens.require_open (parser);
25058 /* Parse the expression. */
25059 expression = cp_parser_expression (parser);
25060 /* Look for the `)'. */
25061 parens.require_close (parser);
25063 if (name == error_mark_node
25064 || string_literal == error_mark_node
25065 || expression == error_mark_node)
25066 invalid_operands = true;
25068 /* Add this operand to the list. */
25069 asm_operands = tree_cons (build_tree_list (name, string_literal),
25070 expression,
25071 asm_operands);
25072 /* If the next token is not a `,', there are no more
25073 operands. */
25074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25075 break;
25076 /* Consume the `,'. */
25077 cp_lexer_consume_token (parser->lexer);
25080 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25083 /* Parse an asm-clobber-list.
25085 asm-clobber-list:
25086 string-literal
25087 asm-clobber-list , string-literal
25089 Returns a TREE_LIST, indicating the clobbers in the order that they
25090 appeared. The TREE_VALUE of each node is a STRING_CST. */
25092 static tree
25093 cp_parser_asm_clobber_list (cp_parser* parser)
25095 tree clobbers = NULL_TREE;
25097 while (true)
25099 tree string_literal;
25101 /* Look for the string literal. */
25102 string_literal = cp_parser_string_literal (parser, false, false);
25103 /* Add it to the list. */
25104 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25105 /* If the next token is not a `,', then the list is
25106 complete. */
25107 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25108 break;
25109 /* Consume the `,' token. */
25110 cp_lexer_consume_token (parser->lexer);
25113 return clobbers;
25116 /* Parse an asm-label-list.
25118 asm-label-list:
25119 identifier
25120 asm-label-list , identifier
25122 Returns a TREE_LIST, indicating the labels in the order that they
25123 appeared. The TREE_VALUE of each node is a label. */
25125 static tree
25126 cp_parser_asm_label_list (cp_parser* parser)
25128 tree labels = NULL_TREE;
25130 while (true)
25132 tree identifier, label, name;
25134 /* Look for the identifier. */
25135 identifier = cp_parser_identifier (parser);
25136 if (!error_operand_p (identifier))
25138 label = lookup_label (identifier);
25139 if (TREE_CODE (label) == LABEL_DECL)
25141 TREE_USED (label) = 1;
25142 check_goto (label);
25143 name = build_string (IDENTIFIER_LENGTH (identifier),
25144 IDENTIFIER_POINTER (identifier));
25145 labels = tree_cons (name, label, labels);
25148 /* If the next token is not a `,', then the list is
25149 complete. */
25150 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25151 break;
25152 /* Consume the `,' token. */
25153 cp_lexer_consume_token (parser->lexer);
25156 return nreverse (labels);
25159 /* Return TRUE iff the next tokens in the stream are possibly the
25160 beginning of a GNU extension attribute. */
25162 static bool
25163 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25165 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25168 /* Return TRUE iff the next tokens in the stream are possibly the
25169 beginning of a standard C++-11 attribute specifier. */
25171 static bool
25172 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25174 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25177 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25178 beginning of a standard C++-11 attribute specifier. */
25180 static bool
25181 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25183 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25185 return (cxx_dialect >= cxx11
25186 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25187 || (token->type == CPP_OPEN_SQUARE
25188 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25189 && token->type == CPP_OPEN_SQUARE)));
25192 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25193 beginning of a GNU extension attribute. */
25195 static bool
25196 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25198 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25200 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25203 /* Return true iff the next tokens can be the beginning of either a
25204 GNU attribute list, or a standard C++11 attribute sequence. */
25206 static bool
25207 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25209 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25210 || cp_next_tokens_can_be_std_attribute_p (parser));
25213 /* Return true iff the next Nth tokens can be the beginning of either
25214 a GNU attribute list, or a standard C++11 attribute sequence. */
25216 static bool
25217 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25219 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25220 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25223 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25224 of GNU attributes, or return NULL. */
25226 static tree
25227 cp_parser_attributes_opt (cp_parser *parser)
25229 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25230 return cp_parser_gnu_attributes_opt (parser);
25231 return cp_parser_std_attribute_spec_seq (parser);
25234 /* Parse an (optional) series of attributes.
25236 attributes:
25237 attributes attribute
25239 attribute:
25240 __attribute__ (( attribute-list [opt] ))
25242 The return value is as for cp_parser_gnu_attribute_list. */
25244 static tree
25245 cp_parser_gnu_attributes_opt (cp_parser* parser)
25247 tree attributes = NULL_TREE;
25249 temp_override<bool> cleanup
25250 (parser->auto_is_implicit_function_template_parm_p, false);
25252 while (true)
25254 cp_token *token;
25255 tree attribute_list;
25256 bool ok = true;
25258 /* Peek at the next token. */
25259 token = cp_lexer_peek_token (parser->lexer);
25260 /* If it's not `__attribute__', then we're done. */
25261 if (token->keyword != RID_ATTRIBUTE)
25262 break;
25264 /* Consume the `__attribute__' keyword. */
25265 cp_lexer_consume_token (parser->lexer);
25266 /* Look for the two `(' tokens. */
25267 matching_parens outer_parens;
25268 outer_parens.require_open (parser);
25269 matching_parens inner_parens;
25270 inner_parens.require_open (parser);
25272 /* Peek at the next token. */
25273 token = cp_lexer_peek_token (parser->lexer);
25274 if (token->type != CPP_CLOSE_PAREN)
25275 /* Parse the attribute-list. */
25276 attribute_list = cp_parser_gnu_attribute_list (parser);
25277 else
25278 /* If the next token is a `)', then there is no attribute
25279 list. */
25280 attribute_list = NULL;
25282 /* Look for the two `)' tokens. */
25283 if (!inner_parens.require_close (parser))
25284 ok = false;
25285 if (!outer_parens.require_close (parser))
25286 ok = false;
25287 if (!ok)
25288 cp_parser_skip_to_end_of_statement (parser);
25290 /* Add these new attributes to the list. */
25291 attributes = attr_chainon (attributes, attribute_list);
25294 return attributes;
25297 /* Parse a GNU attribute-list.
25299 attribute-list:
25300 attribute
25301 attribute-list , attribute
25303 attribute:
25304 identifier
25305 identifier ( identifier )
25306 identifier ( identifier , expression-list )
25307 identifier ( expression-list )
25309 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25310 to an attribute. The TREE_PURPOSE of each node is the identifier
25311 indicating which attribute is in use. The TREE_VALUE represents
25312 the arguments, if any. */
25314 static tree
25315 cp_parser_gnu_attribute_list (cp_parser* parser)
25317 tree attribute_list = NULL_TREE;
25318 bool save_translate_strings_p = parser->translate_strings_p;
25320 parser->translate_strings_p = false;
25321 while (true)
25323 cp_token *token;
25324 tree identifier;
25325 tree attribute;
25327 /* Look for the identifier. We also allow keywords here; for
25328 example `__attribute__ ((const))' is legal. */
25329 token = cp_lexer_peek_token (parser->lexer);
25330 if (token->type == CPP_NAME
25331 || token->type == CPP_KEYWORD)
25333 tree arguments = NULL_TREE;
25335 /* Consume the token, but save it since we need it for the
25336 SIMD enabled function parsing. */
25337 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25339 /* Save away the identifier that indicates which attribute
25340 this is. */
25341 identifier = (token->type == CPP_KEYWORD)
25342 /* For keywords, use the canonical spelling, not the
25343 parsed identifier. */
25344 ? ridpointers[(int) token->keyword]
25345 : id_token->u.value;
25347 identifier = canonicalize_attr_name (identifier);
25348 attribute = build_tree_list (identifier, NULL_TREE);
25350 /* Peek at the next token. */
25351 token = cp_lexer_peek_token (parser->lexer);
25352 /* If it's an `(', then parse the attribute arguments. */
25353 if (token->type == CPP_OPEN_PAREN)
25355 vec<tree, va_gc> *vec;
25356 int attr_flag = (attribute_takes_identifier_p (identifier)
25357 ? id_attr : normal_attr);
25358 vec = cp_parser_parenthesized_expression_list
25359 (parser, attr_flag, /*cast_p=*/false,
25360 /*allow_expansion_p=*/false,
25361 /*non_constant_p=*/NULL);
25362 if (vec == NULL)
25363 arguments = error_mark_node;
25364 else
25366 arguments = build_tree_list_vec (vec);
25367 release_tree_vector (vec);
25369 /* Save the arguments away. */
25370 TREE_VALUE (attribute) = arguments;
25373 if (arguments != error_mark_node)
25375 /* Add this attribute to the list. */
25376 TREE_CHAIN (attribute) = attribute_list;
25377 attribute_list = attribute;
25380 token = cp_lexer_peek_token (parser->lexer);
25382 /* Now, look for more attributes. If the next token isn't a
25383 `,', we're done. */
25384 if (token->type != CPP_COMMA)
25385 break;
25387 /* Consume the comma and keep going. */
25388 cp_lexer_consume_token (parser->lexer);
25390 parser->translate_strings_p = save_translate_strings_p;
25392 /* We built up the list in reverse order. */
25393 return nreverse (attribute_list);
25396 /* Parse a standard C++11 attribute.
25398 The returned representation is a TREE_LIST which TREE_PURPOSE is
25399 the scoped name of the attribute, and the TREE_VALUE is its
25400 arguments list.
25402 Note that the scoped name of the attribute is itself a TREE_LIST
25403 which TREE_PURPOSE is the namespace of the attribute, and
25404 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25405 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25406 and which TREE_PURPOSE is directly the attribute name.
25408 Clients of the attribute code should use get_attribute_namespace
25409 and get_attribute_name to get the actual namespace and name of
25410 attributes, regardless of their being GNU or C++11 attributes.
25412 attribute:
25413 attribute-token attribute-argument-clause [opt]
25415 attribute-token:
25416 identifier
25417 attribute-scoped-token
25419 attribute-scoped-token:
25420 attribute-namespace :: identifier
25422 attribute-namespace:
25423 identifier
25425 attribute-argument-clause:
25426 ( balanced-token-seq )
25428 balanced-token-seq:
25429 balanced-token [opt]
25430 balanced-token-seq balanced-token
25432 balanced-token:
25433 ( balanced-token-seq )
25434 [ balanced-token-seq ]
25435 { balanced-token-seq }. */
25437 static tree
25438 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25440 tree attribute, attr_id = NULL_TREE, arguments;
25441 cp_token *token;
25443 temp_override<bool> cleanup
25444 (parser->auto_is_implicit_function_template_parm_p, false);
25446 /* First, parse name of the attribute, a.k.a attribute-token. */
25448 token = cp_lexer_peek_token (parser->lexer);
25449 if (token->type == CPP_NAME)
25450 attr_id = token->u.value;
25451 else if (token->type == CPP_KEYWORD)
25452 attr_id = ridpointers[(int) token->keyword];
25453 else if (token->flags & NAMED_OP)
25454 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25456 if (attr_id == NULL_TREE)
25457 return NULL_TREE;
25459 cp_lexer_consume_token (parser->lexer);
25461 token = cp_lexer_peek_token (parser->lexer);
25462 if (token->type == CPP_SCOPE)
25464 /* We are seeing a scoped attribute token. */
25466 cp_lexer_consume_token (parser->lexer);
25467 if (attr_ns)
25468 error_at (token->location, "attribute using prefix used together "
25469 "with scoped attribute token");
25470 attr_ns = attr_id;
25472 token = cp_lexer_consume_token (parser->lexer);
25473 if (token->type == CPP_NAME)
25474 attr_id = token->u.value;
25475 else if (token->type == CPP_KEYWORD)
25476 attr_id = ridpointers[(int) token->keyword];
25477 else if (token->flags & NAMED_OP)
25478 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25479 else
25481 error_at (token->location,
25482 "expected an identifier for the attribute name");
25483 return error_mark_node;
25486 attr_ns = canonicalize_attr_name (attr_ns);
25487 attr_id = canonicalize_attr_name (attr_id);
25488 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25489 NULL_TREE);
25490 token = cp_lexer_peek_token (parser->lexer);
25492 else if (attr_ns)
25494 attr_ns = canonicalize_attr_name (attr_ns);
25495 attr_id = canonicalize_attr_name (attr_id);
25496 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25497 NULL_TREE);
25499 else
25501 attr_id = canonicalize_attr_name (attr_id);
25502 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25503 NULL_TREE);
25504 /* C++11 noreturn attribute is equivalent to GNU's. */
25505 if (is_attribute_p ("noreturn", attr_id))
25506 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25507 /* C++14 deprecated attribute is equivalent to GNU's. */
25508 else if (is_attribute_p ("deprecated", attr_id))
25509 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25510 /* C++17 fallthrough attribute is equivalent to GNU's. */
25511 else if (is_attribute_p ("fallthrough", attr_id))
25512 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25513 /* Transactional Memory TS optimize_for_synchronized attribute is
25514 equivalent to GNU transaction_callable. */
25515 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25516 TREE_PURPOSE (attribute)
25517 = get_identifier ("transaction_callable");
25518 /* Transactional Memory attributes are GNU attributes. */
25519 else if (tm_attr_to_mask (attr_id))
25520 TREE_PURPOSE (attribute) = attr_id;
25523 /* Now parse the optional argument clause of the attribute. */
25525 if (token->type != CPP_OPEN_PAREN)
25526 return attribute;
25529 vec<tree, va_gc> *vec;
25530 int attr_flag = normal_attr;
25532 if (attr_ns == gnu_identifier
25533 && attribute_takes_identifier_p (attr_id))
25534 /* A GNU attribute that takes an identifier in parameter. */
25535 attr_flag = id_attr;
25537 vec = cp_parser_parenthesized_expression_list
25538 (parser, attr_flag, /*cast_p=*/false,
25539 /*allow_expansion_p=*/true,
25540 /*non_constant_p=*/NULL);
25541 if (vec == NULL)
25542 arguments = error_mark_node;
25543 else
25545 arguments = build_tree_list_vec (vec);
25546 release_tree_vector (vec);
25549 if (arguments == error_mark_node)
25550 attribute = error_mark_node;
25551 else
25552 TREE_VALUE (attribute) = arguments;
25555 return attribute;
25558 /* Check that the attribute ATTRIBUTE appears at most once in the
25559 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25560 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25561 isn't implemented yet in GCC. */
25563 static void
25564 cp_parser_check_std_attribute (tree attributes, tree attribute)
25566 if (attributes)
25568 tree name = get_attribute_name (attribute);
25569 if (is_attribute_p ("noreturn", name)
25570 && lookup_attribute ("noreturn", attributes))
25571 error ("attribute %<noreturn%> can appear at most once "
25572 "in an attribute-list");
25573 else if (is_attribute_p ("deprecated", name)
25574 && lookup_attribute ("deprecated", attributes))
25575 error ("attribute %<deprecated%> can appear at most once "
25576 "in an attribute-list");
25580 /* Parse a list of standard C++-11 attributes.
25582 attribute-list:
25583 attribute [opt]
25584 attribute-list , attribute[opt]
25585 attribute ...
25586 attribute-list , attribute ...
25589 static tree
25590 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25592 tree attributes = NULL_TREE, attribute = NULL_TREE;
25593 cp_token *token = NULL;
25595 while (true)
25597 attribute = cp_parser_std_attribute (parser, attr_ns);
25598 if (attribute == error_mark_node)
25599 break;
25600 if (attribute != NULL_TREE)
25602 cp_parser_check_std_attribute (attributes, attribute);
25603 TREE_CHAIN (attribute) = attributes;
25604 attributes = attribute;
25606 token = cp_lexer_peek_token (parser->lexer);
25607 if (token->type == CPP_ELLIPSIS)
25609 cp_lexer_consume_token (parser->lexer);
25610 if (attribute == NULL_TREE)
25611 error_at (token->location,
25612 "expected attribute before %<...%>");
25613 else
25615 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25616 if (pack == error_mark_node)
25617 return error_mark_node;
25618 TREE_VALUE (attribute) = pack;
25620 token = cp_lexer_peek_token (parser->lexer);
25622 if (token->type != CPP_COMMA)
25623 break;
25624 cp_lexer_consume_token (parser->lexer);
25626 attributes = nreverse (attributes);
25627 return attributes;
25630 /* Parse a standard C++-11 attribute specifier.
25632 attribute-specifier:
25633 [ [ attribute-using-prefix [opt] attribute-list ] ]
25634 alignment-specifier
25636 attribute-using-prefix:
25637 using attribute-namespace :
25639 alignment-specifier:
25640 alignas ( type-id ... [opt] )
25641 alignas ( alignment-expression ... [opt] ). */
25643 static tree
25644 cp_parser_std_attribute_spec (cp_parser *parser)
25646 tree attributes = NULL_TREE;
25647 cp_token *token = cp_lexer_peek_token (parser->lexer);
25649 if (token->type == CPP_OPEN_SQUARE
25650 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25652 tree attr_ns = NULL_TREE;
25654 cp_lexer_consume_token (parser->lexer);
25655 cp_lexer_consume_token (parser->lexer);
25657 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25659 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25660 if (token->type == CPP_NAME)
25661 attr_ns = token->u.value;
25662 else if (token->type == CPP_KEYWORD)
25663 attr_ns = ridpointers[(int) token->keyword];
25664 else if (token->flags & NAMED_OP)
25665 attr_ns = get_identifier (cpp_type2name (token->type,
25666 token->flags));
25667 if (attr_ns
25668 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25670 if (cxx_dialect < cxx17
25671 && !in_system_header_at (input_location))
25672 pedwarn (input_location, 0,
25673 "attribute using prefix only available "
25674 "with -std=c++17 or -std=gnu++17");
25676 cp_lexer_consume_token (parser->lexer);
25677 cp_lexer_consume_token (parser->lexer);
25678 cp_lexer_consume_token (parser->lexer);
25680 else
25681 attr_ns = NULL_TREE;
25684 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25686 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25687 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25688 cp_parser_skip_to_end_of_statement (parser);
25689 else
25690 /* Warn about parsing c++11 attribute in non-c++11 mode, only
25691 when we are sure that we have actually parsed them. */
25692 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25694 else
25696 tree alignas_expr;
25698 /* Look for an alignment-specifier. */
25700 token = cp_lexer_peek_token (parser->lexer);
25702 if (token->type != CPP_KEYWORD
25703 || token->keyword != RID_ALIGNAS)
25704 return NULL_TREE;
25706 cp_lexer_consume_token (parser->lexer);
25707 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25709 matching_parens parens;
25710 if (!parens.require_open (parser))
25711 return error_mark_node;
25713 cp_parser_parse_tentatively (parser);
25714 alignas_expr = cp_parser_type_id (parser);
25716 if (!cp_parser_parse_definitely (parser))
25718 alignas_expr = cp_parser_assignment_expression (parser);
25719 if (alignas_expr == error_mark_node)
25720 cp_parser_skip_to_end_of_statement (parser);
25721 if (alignas_expr == NULL_TREE
25722 || alignas_expr == error_mark_node)
25723 return alignas_expr;
25726 alignas_expr = cxx_alignas_expr (alignas_expr);
25727 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25729 /* Handle alignas (pack...). */
25730 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25732 cp_lexer_consume_token (parser->lexer);
25733 alignas_expr = make_pack_expansion (alignas_expr);
25736 /* Something went wrong, so don't build the attribute. */
25737 if (alignas_expr == error_mark_node)
25738 return error_mark_node;
25740 if (!parens.require_close (parser))
25741 return error_mark_node;
25743 /* Build the C++-11 representation of an 'aligned'
25744 attribute. */
25745 attributes
25746 = build_tree_list (build_tree_list (gnu_identifier,
25747 aligned_identifier), alignas_expr);
25750 return attributes;
25753 /* Parse a standard C++-11 attribute-specifier-seq.
25755 attribute-specifier-seq:
25756 attribute-specifier-seq [opt] attribute-specifier
25759 static tree
25760 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25762 tree attr_specs = NULL_TREE;
25763 tree attr_last = NULL_TREE;
25765 while (true)
25767 tree attr_spec = cp_parser_std_attribute_spec (parser);
25768 if (attr_spec == NULL_TREE)
25769 break;
25770 if (attr_spec == error_mark_node)
25771 return error_mark_node;
25773 if (attr_last)
25774 TREE_CHAIN (attr_last) = attr_spec;
25775 else
25776 attr_specs = attr_last = attr_spec;
25777 attr_last = tree_last (attr_last);
25780 return attr_specs;
25783 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25784 return index of the first token after balanced-token, or N on failure. */
25786 static size_t
25787 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25789 size_t orig_n = n;
25790 int nparens = 0, nbraces = 0, nsquares = 0;
25792 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25794 case CPP_EOF:
25795 case CPP_PRAGMA_EOL:
25796 /* Ran out of tokens. */
25797 return orig_n;
25798 case CPP_OPEN_PAREN:
25799 ++nparens;
25800 break;
25801 case CPP_OPEN_BRACE:
25802 ++nbraces;
25803 break;
25804 case CPP_OPEN_SQUARE:
25805 ++nsquares;
25806 break;
25807 case CPP_CLOSE_PAREN:
25808 --nparens;
25809 break;
25810 case CPP_CLOSE_BRACE:
25811 --nbraces;
25812 break;
25813 case CPP_CLOSE_SQUARE:
25814 --nsquares;
25815 break;
25816 default:
25817 break;
25819 while (nparens || nbraces || nsquares);
25820 return n;
25823 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25824 return index of the first token after the GNU attribute tokens, or N on
25825 failure. */
25827 static size_t
25828 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25830 while (true)
25832 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25833 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25834 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25835 break;
25837 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25838 if (n2 == n + 2)
25839 break;
25840 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25841 break;
25842 n = n2 + 1;
25844 return n;
25847 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25848 next token), return index of the first token after the standard C++11
25849 attribute tokens, or N on failure. */
25851 static size_t
25852 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25854 while (true)
25856 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25857 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25859 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25860 if (n2 == n + 1)
25861 break;
25862 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25863 break;
25864 n = n2 + 1;
25866 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25867 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25869 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25870 if (n2 == n + 1)
25871 break;
25872 n = n2;
25874 else
25875 break;
25877 return n;
25880 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25881 as the next token), return index of the first token after the attribute
25882 tokens, or N on failure. */
25884 static size_t
25885 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25887 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25888 return cp_parser_skip_gnu_attributes_opt (parser, n);
25889 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25892 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25893 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25894 current value of the PEDANTIC flag, regardless of whether or not
25895 the `__extension__' keyword is present. The caller is responsible
25896 for restoring the value of the PEDANTIC flag. */
25898 static bool
25899 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25901 /* Save the old value of the PEDANTIC flag. */
25902 *saved_pedantic = pedantic;
25904 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25906 /* Consume the `__extension__' token. */
25907 cp_lexer_consume_token (parser->lexer);
25908 /* We're not being pedantic while the `__extension__' keyword is
25909 in effect. */
25910 pedantic = 0;
25912 return true;
25915 return false;
25918 /* Parse a label declaration.
25920 label-declaration:
25921 __label__ label-declarator-seq ;
25923 label-declarator-seq:
25924 identifier , label-declarator-seq
25925 identifier */
25927 static void
25928 cp_parser_label_declaration (cp_parser* parser)
25930 /* Look for the `__label__' keyword. */
25931 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25933 while (true)
25935 tree identifier;
25937 /* Look for an identifier. */
25938 identifier = cp_parser_identifier (parser);
25939 /* If we failed, stop. */
25940 if (identifier == error_mark_node)
25941 break;
25942 /* Declare it as a label. */
25943 finish_label_decl (identifier);
25944 /* If the next token is a `;', stop. */
25945 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25946 break;
25947 /* Look for the `,' separating the label declarations. */
25948 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25951 /* Look for the final `;'. */
25952 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25955 // -------------------------------------------------------------------------- //
25956 // Requires Clause
25958 // Parse a requires clause.
25960 // requires-clause:
25961 // 'requires' logical-or-expression
25963 // The required logical-or-expression must be a constant expression. Note
25964 // that we don't check that the expression is constepxr here. We defer until
25965 // we analyze constraints and then, we only check atomic constraints.
25966 static tree
25967 cp_parser_requires_clause (cp_parser *parser)
25969 // Parse the requires clause so that it is not automatically folded.
25970 ++processing_template_decl;
25971 tree expr = cp_parser_binary_expression (parser, false, false,
25972 PREC_NOT_OPERATOR, NULL);
25973 if (check_for_bare_parameter_packs (expr))
25974 expr = error_mark_node;
25975 --processing_template_decl;
25976 return expr;
25979 // Optionally parse a requires clause:
25980 static tree
25981 cp_parser_requires_clause_opt (cp_parser *parser)
25983 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25984 if (tok->keyword != RID_REQUIRES)
25986 if (!flag_concepts && tok->type == CPP_NAME
25987 && tok->u.value == ridpointers[RID_REQUIRES])
25989 error_at (cp_lexer_peek_token (parser->lexer)->location,
25990 "%<requires%> only available with -fconcepts");
25991 /* Parse and discard the requires-clause. */
25992 cp_lexer_consume_token (parser->lexer);
25993 cp_parser_requires_clause (parser);
25995 return NULL_TREE;
25997 cp_lexer_consume_token (parser->lexer);
25998 return cp_parser_requires_clause (parser);
26002 /*---------------------------------------------------------------------------
26003 Requires expressions
26004 ---------------------------------------------------------------------------*/
26006 /* Parse a requires expression
26008 requirement-expression:
26009 'requires' requirement-parameter-list [opt] requirement-body */
26010 static tree
26011 cp_parser_requires_expression (cp_parser *parser)
26013 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26014 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26016 /* A requires-expression shall appear only within a concept
26017 definition or a requires-clause.
26019 TODO: Implement this diagnostic correctly. */
26020 if (!processing_template_decl)
26022 error_at (loc, "a requires expression cannot appear outside a template");
26023 cp_parser_skip_to_end_of_statement (parser);
26024 return error_mark_node;
26027 tree parms, reqs;
26029 /* Local parameters are delared as variables within the scope
26030 of the expression. They are not visible past the end of
26031 the expression. Expressions within the requires-expression
26032 are unevaluated. */
26033 struct scope_sentinel
26035 scope_sentinel ()
26037 ++cp_unevaluated_operand;
26038 begin_scope (sk_block, NULL_TREE);
26041 ~scope_sentinel ()
26043 pop_bindings_and_leave_scope ();
26044 --cp_unevaluated_operand;
26046 } s;
26048 /* Parse the optional parameter list. */
26049 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26051 parms = cp_parser_requirement_parameter_list (parser);
26052 if (parms == error_mark_node)
26053 return error_mark_node;
26055 else
26056 parms = NULL_TREE;
26058 /* Parse the requirement body. */
26059 reqs = cp_parser_requirement_body (parser);
26060 if (reqs == error_mark_node)
26061 return error_mark_node;
26064 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26065 the parm chain. */
26066 grokparms (parms, &parms);
26067 return finish_requires_expr (parms, reqs);
26070 /* Parse a parameterized requirement.
26072 requirement-parameter-list:
26073 '(' parameter-declaration-clause ')' */
26074 static tree
26075 cp_parser_requirement_parameter_list (cp_parser *parser)
26077 matching_parens parens;
26078 if (!parens.require_open (parser))
26079 return error_mark_node;
26081 tree parms = cp_parser_parameter_declaration_clause (parser);
26083 if (!parens.require_close (parser))
26084 return error_mark_node;
26086 return parms;
26089 /* Parse the body of a requirement.
26091 requirement-body:
26092 '{' requirement-list '}' */
26093 static tree
26094 cp_parser_requirement_body (cp_parser *parser)
26096 matching_braces braces;
26097 if (!braces.require_open (parser))
26098 return error_mark_node;
26100 tree reqs = cp_parser_requirement_list (parser);
26102 if (!braces.require_close (parser))
26103 return error_mark_node;
26105 return reqs;
26108 /* Parse a list of requirements.
26110 requirement-list:
26111 requirement
26112 requirement-list ';' requirement[opt] */
26113 static tree
26114 cp_parser_requirement_list (cp_parser *parser)
26116 tree result = NULL_TREE;
26117 while (true)
26119 tree req = cp_parser_requirement (parser);
26120 if (req == error_mark_node)
26121 return error_mark_node;
26123 result = tree_cons (NULL_TREE, req, result);
26125 /* If we see a semi-colon, consume it. */
26126 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26127 cp_lexer_consume_token (parser->lexer);
26129 /* Stop processing at the end of the list. */
26130 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26131 break;
26134 /* Reverse the order of requirements so they are analyzed in
26135 declaration order. */
26136 return nreverse (result);
26139 /* Parse a syntactic requirement or type requirement.
26141 requirement:
26142 simple-requirement
26143 compound-requirement
26144 type-requirement
26145 nested-requirement */
26146 static tree
26147 cp_parser_requirement (cp_parser *parser)
26149 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26150 return cp_parser_compound_requirement (parser);
26151 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26152 return cp_parser_type_requirement (parser);
26153 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26154 return cp_parser_nested_requirement (parser);
26155 else
26156 return cp_parser_simple_requirement (parser);
26159 /* Parse a simple requirement.
26161 simple-requirement:
26162 expression ';' */
26163 static tree
26164 cp_parser_simple_requirement (cp_parser *parser)
26166 tree expr = cp_parser_expression (parser, NULL, false, false);
26167 if (!expr || expr == error_mark_node)
26168 return error_mark_node;
26170 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26171 return error_mark_node;
26173 return finish_simple_requirement (expr);
26176 /* Parse a type requirement
26178 type-requirement
26179 nested-name-specifier [opt] required-type-name ';'
26181 required-type-name:
26182 type-name
26183 'template' [opt] simple-template-id */
26184 static tree
26185 cp_parser_type_requirement (cp_parser *parser)
26187 cp_lexer_consume_token (parser->lexer);
26189 // Save the scope before parsing name specifiers.
26190 tree saved_scope = parser->scope;
26191 tree saved_object_scope = parser->object_scope;
26192 tree saved_qualifying_scope = parser->qualifying_scope;
26193 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26194 cp_parser_nested_name_specifier_opt (parser,
26195 /*typename_keyword_p=*/true,
26196 /*check_dependency_p=*/false,
26197 /*type_p=*/true,
26198 /*is_declaration=*/false);
26200 tree type;
26201 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26203 cp_lexer_consume_token (parser->lexer);
26204 type = cp_parser_template_id (parser,
26205 /*template_keyword_p=*/true,
26206 /*check_dependency=*/false,
26207 /*tag_type=*/none_type,
26208 /*is_declaration=*/false);
26209 type = make_typename_type (parser->scope, type, typename_type,
26210 /*complain=*/tf_error);
26212 else
26213 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26215 if (TREE_CODE (type) == TYPE_DECL)
26216 type = TREE_TYPE (type);
26218 parser->scope = saved_scope;
26219 parser->object_scope = saved_object_scope;
26220 parser->qualifying_scope = saved_qualifying_scope;
26222 if (type == error_mark_node)
26223 cp_parser_skip_to_end_of_statement (parser);
26225 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26226 return error_mark_node;
26227 if (type == error_mark_node)
26228 return error_mark_node;
26230 return finish_type_requirement (type);
26233 /* Parse a compound requirement
26235 compound-requirement:
26236 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26237 static tree
26238 cp_parser_compound_requirement (cp_parser *parser)
26240 /* Parse an expression enclosed in '{ }'s. */
26241 matching_braces braces;
26242 if (!braces.require_open (parser))
26243 return error_mark_node;
26245 tree expr = cp_parser_expression (parser, NULL, false, false);
26246 if (!expr || expr == error_mark_node)
26247 return error_mark_node;
26249 if (!braces.require_close (parser))
26250 return error_mark_node;
26252 /* Parse the optional noexcept. */
26253 bool noexcept_p = false;
26254 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26256 cp_lexer_consume_token (parser->lexer);
26257 noexcept_p = true;
26260 /* Parse the optional trailing return type. */
26261 tree type = NULL_TREE;
26262 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26264 cp_lexer_consume_token (parser->lexer);
26265 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26266 parser->in_result_type_constraint_p = true;
26267 type = cp_parser_trailing_type_id (parser);
26268 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26269 if (type == error_mark_node)
26270 return error_mark_node;
26273 return finish_compound_requirement (expr, type, noexcept_p);
26276 /* Parse a nested requirement. This is the same as a requires clause.
26278 nested-requirement:
26279 requires-clause */
26280 static tree
26281 cp_parser_nested_requirement (cp_parser *parser)
26283 cp_lexer_consume_token (parser->lexer);
26284 tree req = cp_parser_requires_clause (parser);
26285 if (req == error_mark_node)
26286 return error_mark_node;
26287 return finish_nested_requirement (req);
26290 /* Support Functions */
26292 /* Return the appropriate prefer_type argument for lookup_name_real based on
26293 tag_type and template_mem_access. */
26295 static inline int
26296 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26298 /* DR 141: When looking in the current enclosing context for a template-name
26299 after -> or ., only consider class templates. */
26300 if (template_mem_access)
26301 return 2;
26302 switch (tag_type)
26304 case none_type: return 0; // No preference.
26305 case scope_type: return 1; // Type or namespace.
26306 default: return 2; // Type only.
26310 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26311 NAME should have one of the representations used for an
26312 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26313 is returned. If PARSER->SCOPE is a dependent type, then a
26314 SCOPE_REF is returned.
26316 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26317 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26318 was formed. Abstractly, such entities should not be passed to this
26319 function, because they do not need to be looked up, but it is
26320 simpler to check for this special case here, rather than at the
26321 call-sites.
26323 In cases not explicitly covered above, this function returns a
26324 DECL, OVERLOAD, or baselink representing the result of the lookup.
26325 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26326 is returned.
26328 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26329 (e.g., "struct") that was used. In that case bindings that do not
26330 refer to types are ignored.
26332 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26333 ignored.
26335 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26336 are ignored.
26338 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26339 types.
26341 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26342 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26343 NULL_TREE otherwise. */
26345 static cp_expr
26346 cp_parser_lookup_name (cp_parser *parser, tree name,
26347 enum tag_types tag_type,
26348 bool is_template,
26349 bool is_namespace,
26350 bool check_dependency,
26351 tree *ambiguous_decls,
26352 location_t name_location)
26354 tree decl;
26355 tree object_type = parser->context->object_type;
26357 /* Assume that the lookup will be unambiguous. */
26358 if (ambiguous_decls)
26359 *ambiguous_decls = NULL_TREE;
26361 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26362 no longer valid. Note that if we are parsing tentatively, and
26363 the parse fails, OBJECT_TYPE will be automatically restored. */
26364 parser->context->object_type = NULL_TREE;
26366 if (name == error_mark_node)
26367 return error_mark_node;
26369 /* A template-id has already been resolved; there is no lookup to
26370 do. */
26371 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26372 return name;
26373 if (BASELINK_P (name))
26375 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26376 == TEMPLATE_ID_EXPR);
26377 return name;
26380 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26381 it should already have been checked to make sure that the name
26382 used matches the type being destroyed. */
26383 if (TREE_CODE (name) == BIT_NOT_EXPR)
26385 tree type;
26387 /* Figure out to which type this destructor applies. */
26388 if (parser->scope)
26389 type = parser->scope;
26390 else if (object_type)
26391 type = object_type;
26392 else
26393 type = current_class_type;
26394 /* If that's not a class type, there is no destructor. */
26395 if (!type || !CLASS_TYPE_P (type))
26396 return error_mark_node;
26398 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26399 lazily_declare_fn (sfk_destructor, type);
26401 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26402 return dtor;
26404 return error_mark_node;
26407 /* By this point, the NAME should be an ordinary identifier. If
26408 the id-expression was a qualified name, the qualifying scope is
26409 stored in PARSER->SCOPE at this point. */
26410 gcc_assert (identifier_p (name));
26412 /* Perform the lookup. */
26413 if (parser->scope)
26415 bool dependent_p;
26417 if (parser->scope == error_mark_node)
26418 return error_mark_node;
26420 /* If the SCOPE is dependent, the lookup must be deferred until
26421 the template is instantiated -- unless we are explicitly
26422 looking up names in uninstantiated templates. Even then, we
26423 cannot look up the name if the scope is not a class type; it
26424 might, for example, be a template type parameter. */
26425 dependent_p = (TYPE_P (parser->scope)
26426 && dependent_scope_p (parser->scope));
26427 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26428 && dependent_p)
26429 /* Defer lookup. */
26430 decl = error_mark_node;
26431 else
26433 tree pushed_scope = NULL_TREE;
26435 /* If PARSER->SCOPE is a dependent type, then it must be a
26436 class type, and we must not be checking dependencies;
26437 otherwise, we would have processed this lookup above. So
26438 that PARSER->SCOPE is not considered a dependent base by
26439 lookup_member, we must enter the scope here. */
26440 if (dependent_p)
26441 pushed_scope = push_scope (parser->scope);
26443 /* If the PARSER->SCOPE is a template specialization, it
26444 may be instantiated during name lookup. In that case,
26445 errors may be issued. Even if we rollback the current
26446 tentative parse, those errors are valid. */
26447 decl = lookup_qualified_name (parser->scope, name,
26448 prefer_type_arg (tag_type),
26449 /*complain=*/true);
26451 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26452 lookup result and the nested-name-specifier nominates a class C:
26453 * if the name specified after the nested-name-specifier, when
26454 looked up in C, is the injected-class-name of C (Clause 9), or
26455 * if the name specified after the nested-name-specifier is the
26456 same as the identifier or the simple-template-id's template-
26457 name in the last component of the nested-name-specifier,
26458 the name is instead considered to name the constructor of
26459 class C. [ Note: for example, the constructor is not an
26460 acceptable lookup result in an elaborated-type-specifier so
26461 the constructor would not be used in place of the
26462 injected-class-name. --end note ] Such a constructor name
26463 shall be used only in the declarator-id of a declaration that
26464 names a constructor or in a using-declaration. */
26465 if (tag_type == none_type
26466 && DECL_SELF_REFERENCE_P (decl)
26467 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26468 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26469 prefer_type_arg (tag_type),
26470 /*complain=*/true);
26472 /* If we have a single function from a using decl, pull it out. */
26473 if (TREE_CODE (decl) == OVERLOAD
26474 && !really_overloaded_fn (decl))
26475 decl = OVL_FUNCTION (decl);
26477 if (pushed_scope)
26478 pop_scope (pushed_scope);
26481 /* If the scope is a dependent type and either we deferred lookup or
26482 we did lookup but didn't find the name, rememeber the name. */
26483 if (decl == error_mark_node && TYPE_P (parser->scope)
26484 && dependent_type_p (parser->scope))
26486 if (tag_type)
26488 tree type;
26490 /* The resolution to Core Issue 180 says that `struct
26491 A::B' should be considered a type-name, even if `A'
26492 is dependent. */
26493 type = make_typename_type (parser->scope, name, tag_type,
26494 /*complain=*/tf_error);
26495 if (type != error_mark_node)
26496 decl = TYPE_NAME (type);
26498 else if (is_template
26499 && (cp_parser_next_token_ends_template_argument_p (parser)
26500 || cp_lexer_next_token_is (parser->lexer,
26501 CPP_CLOSE_PAREN)))
26502 decl = make_unbound_class_template (parser->scope,
26503 name, NULL_TREE,
26504 /*complain=*/tf_error);
26505 else
26506 decl = build_qualified_name (/*type=*/NULL_TREE,
26507 parser->scope, name,
26508 is_template);
26510 parser->qualifying_scope = parser->scope;
26511 parser->object_scope = NULL_TREE;
26513 else if (object_type)
26515 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26516 OBJECT_TYPE is not a class. */
26517 if (CLASS_TYPE_P (object_type))
26518 /* If the OBJECT_TYPE is a template specialization, it may
26519 be instantiated during name lookup. In that case, errors
26520 may be issued. Even if we rollback the current tentative
26521 parse, those errors are valid. */
26522 decl = lookup_member (object_type,
26523 name,
26524 /*protect=*/0,
26525 prefer_type_arg (tag_type),
26526 tf_warning_or_error);
26527 else
26528 decl = NULL_TREE;
26530 if (!decl)
26531 /* Look it up in the enclosing context. DR 141: When looking for a
26532 template-name after -> or ., only consider class templates. */
26533 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26534 /*nonclass=*/0,
26535 /*block_p=*/true, is_namespace, 0);
26536 if (object_type == unknown_type_node)
26537 /* The object is type-dependent, so we can't look anything up; we used
26538 this to get the DR 141 behavior. */
26539 object_type = NULL_TREE;
26540 parser->object_scope = object_type;
26541 parser->qualifying_scope = NULL_TREE;
26543 else
26545 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26546 /*nonclass=*/0,
26547 /*block_p=*/true, is_namespace, 0);
26548 parser->qualifying_scope = NULL_TREE;
26549 parser->object_scope = NULL_TREE;
26552 /* If the lookup failed, let our caller know. */
26553 if (!decl || decl == error_mark_node)
26554 return error_mark_node;
26556 /* Pull out the template from an injected-class-name (or multiple). */
26557 if (is_template)
26558 decl = maybe_get_template_decl_from_type_decl (decl);
26560 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26561 if (TREE_CODE (decl) == TREE_LIST)
26563 if (ambiguous_decls)
26564 *ambiguous_decls = decl;
26565 /* The error message we have to print is too complicated for
26566 cp_parser_error, so we incorporate its actions directly. */
26567 if (!cp_parser_simulate_error (parser))
26569 error_at (name_location, "reference to %qD is ambiguous",
26570 name);
26571 print_candidates (decl);
26573 return error_mark_node;
26576 gcc_assert (DECL_P (decl)
26577 || TREE_CODE (decl) == OVERLOAD
26578 || TREE_CODE (decl) == SCOPE_REF
26579 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26580 || BASELINK_P (decl));
26582 /* If we have resolved the name of a member declaration, check to
26583 see if the declaration is accessible. When the name resolves to
26584 set of overloaded functions, accessibility is checked when
26585 overload resolution is done.
26587 During an explicit instantiation, access is not checked at all,
26588 as per [temp.explicit]. */
26589 if (DECL_P (decl))
26590 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26592 maybe_record_typedef_use (decl);
26594 return cp_expr (decl, name_location);
26597 /* Like cp_parser_lookup_name, but for use in the typical case where
26598 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26599 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26601 static tree
26602 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26604 return cp_parser_lookup_name (parser, name,
26605 none_type,
26606 /*is_template=*/false,
26607 /*is_namespace=*/false,
26608 /*check_dependency=*/true,
26609 /*ambiguous_decls=*/NULL,
26610 location);
26613 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26614 the current context, return the TYPE_DECL. If TAG_NAME_P is
26615 true, the DECL indicates the class being defined in a class-head,
26616 or declared in an elaborated-type-specifier.
26618 Otherwise, return DECL. */
26620 static tree
26621 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26623 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26624 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26626 struct A {
26627 template <typename T> struct B;
26630 template <typename T> struct A::B {};
26632 Similarly, in an elaborated-type-specifier:
26634 namespace N { struct X{}; }
26636 struct A {
26637 template <typename T> friend struct N::X;
26640 However, if the DECL refers to a class type, and we are in
26641 the scope of the class, then the name lookup automatically
26642 finds the TYPE_DECL created by build_self_reference rather
26643 than a TEMPLATE_DECL. For example, in:
26645 template <class T> struct S {
26646 S s;
26649 there is no need to handle such case. */
26651 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26652 return DECL_TEMPLATE_RESULT (decl);
26654 return decl;
26657 /* If too many, or too few, template-parameter lists apply to the
26658 declarator, issue an error message. Returns TRUE if all went well,
26659 and FALSE otherwise. */
26661 static bool
26662 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26663 cp_declarator *declarator,
26664 location_t declarator_location)
26666 switch (declarator->kind)
26668 case cdk_id:
26670 unsigned num_templates = 0;
26671 tree scope = declarator->u.id.qualifying_scope;
26672 bool template_id_p = false;
26674 if (scope)
26675 num_templates = num_template_headers_for_class (scope);
26676 else if (TREE_CODE (declarator->u.id.unqualified_name)
26677 == TEMPLATE_ID_EXPR)
26679 /* If the DECLARATOR has the form `X<y>' then it uses one
26680 additional level of template parameters. */
26681 ++num_templates;
26682 template_id_p = true;
26685 return cp_parser_check_template_parameters
26686 (parser, num_templates, template_id_p, declarator_location,
26687 declarator);
26690 case cdk_function:
26691 case cdk_array:
26692 case cdk_pointer:
26693 case cdk_reference:
26694 case cdk_ptrmem:
26695 return (cp_parser_check_declarator_template_parameters
26696 (parser, declarator->declarator, declarator_location));
26698 case cdk_decomp:
26699 case cdk_error:
26700 return true;
26702 default:
26703 gcc_unreachable ();
26705 return false;
26708 /* NUM_TEMPLATES were used in the current declaration. If that is
26709 invalid, return FALSE and issue an error messages. Otherwise,
26710 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26711 declarator and we can print more accurate diagnostics. */
26713 static bool
26714 cp_parser_check_template_parameters (cp_parser* parser,
26715 unsigned num_templates,
26716 bool template_id_p,
26717 location_t location,
26718 cp_declarator *declarator)
26720 /* If there are the same number of template classes and parameter
26721 lists, that's OK. */
26722 if (parser->num_template_parameter_lists == num_templates)
26723 return true;
26724 /* If there are more, but only one more, and the name ends in an identifier,
26725 then we are declaring a primary template. That's OK too. */
26726 if (!template_id_p
26727 && parser->num_template_parameter_lists == num_templates + 1)
26728 return true;
26729 /* If there are more template classes than parameter lists, we have
26730 something like:
26732 template <class T> void S<T>::R<T>::f (); */
26733 if (parser->num_template_parameter_lists < num_templates)
26735 if (declarator && !current_function_decl)
26736 error_at (location, "specializing member %<%T::%E%> "
26737 "requires %<template<>%> syntax",
26738 declarator->u.id.qualifying_scope,
26739 declarator->u.id.unqualified_name);
26740 else if (declarator)
26741 error_at (location, "invalid declaration of %<%T::%E%>",
26742 declarator->u.id.qualifying_scope,
26743 declarator->u.id.unqualified_name);
26744 else
26745 error_at (location, "too few template-parameter-lists");
26746 return false;
26748 /* Otherwise, there are too many template parameter lists. We have
26749 something like:
26751 template <class T> template <class U> void S::f(); */
26752 error_at (location, "too many template-parameter-lists");
26753 return false;
26756 /* Parse an optional `::' token indicating that the following name is
26757 from the global namespace. If so, PARSER->SCOPE is set to the
26758 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26759 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26760 Returns the new value of PARSER->SCOPE, if the `::' token is
26761 present, and NULL_TREE otherwise. */
26763 static tree
26764 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26766 cp_token *token;
26768 /* Peek at the next token. */
26769 token = cp_lexer_peek_token (parser->lexer);
26770 /* If we're looking at a `::' token then we're starting from the
26771 global namespace, not our current location. */
26772 if (token->type == CPP_SCOPE)
26774 /* Consume the `::' token. */
26775 cp_lexer_consume_token (parser->lexer);
26776 /* Set the SCOPE so that we know where to start the lookup. */
26777 parser->scope = global_namespace;
26778 parser->qualifying_scope = global_namespace;
26779 parser->object_scope = NULL_TREE;
26781 return parser->scope;
26783 else if (!current_scope_valid_p)
26785 parser->scope = NULL_TREE;
26786 parser->qualifying_scope = NULL_TREE;
26787 parser->object_scope = NULL_TREE;
26790 return NULL_TREE;
26793 /* Returns TRUE if the upcoming token sequence is the start of a
26794 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26795 declarator is preceded by the `friend' specifier. */
26797 static bool
26798 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26800 bool constructor_p;
26801 bool outside_class_specifier_p;
26802 tree nested_name_specifier;
26803 cp_token *next_token;
26805 /* The common case is that this is not a constructor declarator, so
26806 try to avoid doing lots of work if at all possible. It's not
26807 valid declare a constructor at function scope. */
26808 if (parser->in_function_body)
26809 return false;
26810 /* And only certain tokens can begin a constructor declarator. */
26811 next_token = cp_lexer_peek_token (parser->lexer);
26812 if (next_token->type != CPP_NAME
26813 && next_token->type != CPP_SCOPE
26814 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26815 && next_token->type != CPP_TEMPLATE_ID)
26816 return false;
26818 /* Parse tentatively; we are going to roll back all of the tokens
26819 consumed here. */
26820 cp_parser_parse_tentatively (parser);
26821 /* Assume that we are looking at a constructor declarator. */
26822 constructor_p = true;
26824 /* Look for the optional `::' operator. */
26825 cp_parser_global_scope_opt (parser,
26826 /*current_scope_valid_p=*/false);
26827 /* Look for the nested-name-specifier. */
26828 nested_name_specifier
26829 = (cp_parser_nested_name_specifier_opt (parser,
26830 /*typename_keyword_p=*/false,
26831 /*check_dependency_p=*/false,
26832 /*type_p=*/false,
26833 /*is_declaration=*/false));
26835 outside_class_specifier_p = (!at_class_scope_p ()
26836 || !TYPE_BEING_DEFINED (current_class_type)
26837 || friend_p);
26839 /* Outside of a class-specifier, there must be a
26840 nested-name-specifier. Except in C++17 mode, where we
26841 might be declaring a guiding declaration. */
26842 if (!nested_name_specifier && outside_class_specifier_p
26843 && cxx_dialect < cxx17)
26844 constructor_p = false;
26845 else if (nested_name_specifier == error_mark_node)
26846 constructor_p = false;
26848 /* If we have a class scope, this is easy; DR 147 says that S::S always
26849 names the constructor, and no other qualified name could. */
26850 if (constructor_p && nested_name_specifier
26851 && CLASS_TYPE_P (nested_name_specifier))
26853 tree id = cp_parser_unqualified_id (parser,
26854 /*template_keyword_p=*/false,
26855 /*check_dependency_p=*/false,
26856 /*declarator_p=*/true,
26857 /*optional_p=*/false);
26858 if (is_overloaded_fn (id))
26859 id = DECL_NAME (get_first_fn (id));
26860 if (!constructor_name_p (id, nested_name_specifier))
26861 constructor_p = false;
26863 /* If we still think that this might be a constructor-declarator,
26864 look for a class-name. */
26865 else if (constructor_p)
26867 /* If we have:
26869 template <typename T> struct S {
26870 S();
26873 we must recognize that the nested `S' names a class. */
26874 if (cxx_dialect >= cxx17)
26875 cp_parser_parse_tentatively (parser);
26877 tree type_decl;
26878 type_decl = cp_parser_class_name (parser,
26879 /*typename_keyword_p=*/false,
26880 /*template_keyword_p=*/false,
26881 none_type,
26882 /*check_dependency_p=*/false,
26883 /*class_head_p=*/false,
26884 /*is_declaration=*/false);
26886 if (cxx_dialect >= cxx17
26887 && !cp_parser_parse_definitely (parser))
26889 type_decl = NULL_TREE;
26890 tree tmpl = cp_parser_template_name (parser,
26891 /*template_keyword*/false,
26892 /*check_dependency_p*/false,
26893 /*is_declaration*/false,
26894 none_type,
26895 /*is_identifier*/NULL);
26896 if (DECL_CLASS_TEMPLATE_P (tmpl)
26897 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26898 /* It's a deduction guide, return true. */;
26899 else
26900 cp_parser_simulate_error (parser);
26903 /* If there was no class-name, then this is not a constructor.
26904 Otherwise, if we are in a class-specifier and we aren't
26905 handling a friend declaration, check that its type matches
26906 current_class_type (c++/38313). Note: error_mark_node
26907 is left alone for error recovery purposes. */
26908 constructor_p = (!cp_parser_error_occurred (parser)
26909 && (outside_class_specifier_p
26910 || type_decl == NULL_TREE
26911 || type_decl == error_mark_node
26912 || same_type_p (current_class_type,
26913 TREE_TYPE (type_decl))));
26915 /* If we're still considering a constructor, we have to see a `(',
26916 to begin the parameter-declaration-clause, followed by either a
26917 `)', an `...', or a decl-specifier. We need to check for a
26918 type-specifier to avoid being fooled into thinking that:
26920 S (f) (int);
26922 is a constructor. (It is actually a function named `f' that
26923 takes one parameter (of type `int') and returns a value of type
26924 `S'. */
26925 if (constructor_p
26926 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26927 constructor_p = false;
26929 if (constructor_p
26930 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26931 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26932 /* A parameter declaration begins with a decl-specifier,
26933 which is either the "attribute" keyword, a storage class
26934 specifier, or (usually) a type-specifier. */
26935 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26937 tree type;
26938 tree pushed_scope = NULL_TREE;
26939 unsigned saved_num_template_parameter_lists;
26941 /* Names appearing in the type-specifier should be looked up
26942 in the scope of the class. */
26943 if (current_class_type)
26944 type = NULL_TREE;
26945 else if (type_decl)
26947 type = TREE_TYPE (type_decl);
26948 if (TREE_CODE (type) == TYPENAME_TYPE)
26950 type = resolve_typename_type (type,
26951 /*only_current_p=*/false);
26952 if (TREE_CODE (type) == TYPENAME_TYPE)
26954 cp_parser_abort_tentative_parse (parser);
26955 return false;
26958 pushed_scope = push_scope (type);
26961 /* Inside the constructor parameter list, surrounding
26962 template-parameter-lists do not apply. */
26963 saved_num_template_parameter_lists
26964 = parser->num_template_parameter_lists;
26965 parser->num_template_parameter_lists = 0;
26967 /* Look for the type-specifier. */
26968 cp_parser_type_specifier (parser,
26969 CP_PARSER_FLAGS_NONE,
26970 /*decl_specs=*/NULL,
26971 /*is_declarator=*/true,
26972 /*declares_class_or_enum=*/NULL,
26973 /*is_cv_qualifier=*/NULL);
26975 parser->num_template_parameter_lists
26976 = saved_num_template_parameter_lists;
26978 /* Leave the scope of the class. */
26979 if (pushed_scope)
26980 pop_scope (pushed_scope);
26982 constructor_p = !cp_parser_error_occurred (parser);
26986 /* We did not really want to consume any tokens. */
26987 cp_parser_abort_tentative_parse (parser);
26989 return constructor_p;
26992 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26993 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26994 they must be performed once we are in the scope of the function.
26996 Returns the function defined. */
26998 static tree
26999 cp_parser_function_definition_from_specifiers_and_declarator
27000 (cp_parser* parser,
27001 cp_decl_specifier_seq *decl_specifiers,
27002 tree attributes,
27003 const cp_declarator *declarator)
27005 tree fn;
27006 bool success_p;
27008 /* Begin the function-definition. */
27009 success_p = start_function (decl_specifiers, declarator, attributes);
27011 /* The things we're about to see are not directly qualified by any
27012 template headers we've seen thus far. */
27013 reset_specialization ();
27015 /* If there were names looked up in the decl-specifier-seq that we
27016 did not check, check them now. We must wait until we are in the
27017 scope of the function to perform the checks, since the function
27018 might be a friend. */
27019 perform_deferred_access_checks (tf_warning_or_error);
27021 if (success_p)
27023 cp_finalize_omp_declare_simd (parser, current_function_decl);
27024 parser->omp_declare_simd = NULL;
27025 cp_finalize_oacc_routine (parser, current_function_decl, true);
27026 parser->oacc_routine = NULL;
27029 if (!success_p)
27031 /* Skip the entire function. */
27032 cp_parser_skip_to_end_of_block_or_statement (parser);
27033 fn = error_mark_node;
27035 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27037 /* Seen already, skip it. An error message has already been output. */
27038 cp_parser_skip_to_end_of_block_or_statement (parser);
27039 fn = current_function_decl;
27040 current_function_decl = NULL_TREE;
27041 /* If this is a function from a class, pop the nested class. */
27042 if (current_class_name)
27043 pop_nested_class ();
27045 else
27047 timevar_id_t tv;
27048 if (DECL_DECLARED_INLINE_P (current_function_decl))
27049 tv = TV_PARSE_INLINE;
27050 else
27051 tv = TV_PARSE_FUNC;
27052 timevar_push (tv);
27053 fn = cp_parser_function_definition_after_declarator (parser,
27054 /*inline_p=*/false);
27055 timevar_pop (tv);
27058 return fn;
27061 /* Parse the part of a function-definition that follows the
27062 declarator. INLINE_P is TRUE iff this function is an inline
27063 function defined within a class-specifier.
27065 Returns the function defined. */
27067 static tree
27068 cp_parser_function_definition_after_declarator (cp_parser* parser,
27069 bool inline_p)
27071 tree fn;
27072 bool saved_in_unbraced_linkage_specification_p;
27073 bool saved_in_function_body;
27074 unsigned saved_num_template_parameter_lists;
27075 cp_token *token;
27076 bool fully_implicit_function_template_p
27077 = parser->fully_implicit_function_template_p;
27078 parser->fully_implicit_function_template_p = false;
27079 tree implicit_template_parms
27080 = parser->implicit_template_parms;
27081 parser->implicit_template_parms = 0;
27082 cp_binding_level* implicit_template_scope
27083 = parser->implicit_template_scope;
27084 parser->implicit_template_scope = 0;
27086 saved_in_function_body = parser->in_function_body;
27087 parser->in_function_body = true;
27088 /* If the next token is `return', then the code may be trying to
27089 make use of the "named return value" extension that G++ used to
27090 support. */
27091 token = cp_lexer_peek_token (parser->lexer);
27092 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27094 /* Consume the `return' keyword. */
27095 cp_lexer_consume_token (parser->lexer);
27096 /* Look for the identifier that indicates what value is to be
27097 returned. */
27098 cp_parser_identifier (parser);
27099 /* Issue an error message. */
27100 error_at (token->location,
27101 "named return values are no longer supported");
27102 /* Skip tokens until we reach the start of the function body. */
27103 while (true)
27105 cp_token *token = cp_lexer_peek_token (parser->lexer);
27106 if (token->type == CPP_OPEN_BRACE
27107 || token->type == CPP_EOF
27108 || token->type == CPP_PRAGMA_EOL)
27109 break;
27110 cp_lexer_consume_token (parser->lexer);
27113 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27114 anything declared inside `f'. */
27115 saved_in_unbraced_linkage_specification_p
27116 = parser->in_unbraced_linkage_specification_p;
27117 parser->in_unbraced_linkage_specification_p = false;
27118 /* Inside the function, surrounding template-parameter-lists do not
27119 apply. */
27120 saved_num_template_parameter_lists
27121 = parser->num_template_parameter_lists;
27122 parser->num_template_parameter_lists = 0;
27124 /* If the next token is `try', `__transaction_atomic', or
27125 `__transaction_relaxed`, then we are looking at either function-try-block
27126 or function-transaction-block. Note that all of these include the
27127 function-body. */
27128 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27129 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27130 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27131 RID_TRANSACTION_RELAXED))
27132 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27133 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27134 cp_parser_function_try_block (parser);
27135 else
27136 cp_parser_ctor_initializer_opt_and_function_body
27137 (parser, /*in_function_try_block=*/false);
27139 /* Finish the function. */
27140 fn = finish_function (inline_p);
27141 /* Generate code for it, if necessary. */
27142 expand_or_defer_fn (fn);
27143 /* Restore the saved values. */
27144 parser->in_unbraced_linkage_specification_p
27145 = saved_in_unbraced_linkage_specification_p;
27146 parser->num_template_parameter_lists
27147 = saved_num_template_parameter_lists;
27148 parser->in_function_body = saved_in_function_body;
27150 parser->fully_implicit_function_template_p
27151 = fully_implicit_function_template_p;
27152 parser->implicit_template_parms
27153 = implicit_template_parms;
27154 parser->implicit_template_scope
27155 = implicit_template_scope;
27157 if (parser->fully_implicit_function_template_p)
27158 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27160 return fn;
27163 /* Parse a template-declaration body (following argument list). */
27165 static void
27166 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27167 tree parameter_list,
27168 bool member_p)
27170 tree decl = NULL_TREE;
27171 bool friend_p = false;
27173 /* We just processed one more parameter list. */
27174 ++parser->num_template_parameter_lists;
27176 /* Get the deferred access checks from the parameter list. These
27177 will be checked once we know what is being declared, as for a
27178 member template the checks must be performed in the scope of the
27179 class containing the member. */
27180 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27182 /* Tentatively parse for a new template parameter list, which can either be
27183 the template keyword or a template introduction. */
27184 if (cp_parser_template_declaration_after_export (parser, member_p))
27185 /* OK */;
27186 else if (cxx_dialect >= cxx11
27187 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27188 decl = cp_parser_alias_declaration (parser);
27189 else
27191 /* There are no access checks when parsing a template, as we do not
27192 know if a specialization will be a friend. */
27193 push_deferring_access_checks (dk_no_check);
27194 cp_token *token = cp_lexer_peek_token (parser->lexer);
27195 decl = cp_parser_single_declaration (parser,
27196 checks,
27197 member_p,
27198 /*explicit_specialization_p=*/false,
27199 &friend_p);
27200 pop_deferring_access_checks ();
27202 /* If this is a member template declaration, let the front
27203 end know. */
27204 if (member_p && !friend_p && decl)
27206 if (TREE_CODE (decl) == TYPE_DECL)
27207 cp_parser_check_access_in_redeclaration (decl, token->location);
27209 decl = finish_member_template_decl (decl);
27211 else if (friend_p && decl
27212 && DECL_DECLARES_TYPE_P (decl))
27213 make_friend_class (current_class_type, TREE_TYPE (decl),
27214 /*complain=*/true);
27216 /* We are done with the current parameter list. */
27217 --parser->num_template_parameter_lists;
27219 pop_deferring_access_checks ();
27221 /* Finish up. */
27222 finish_template_decl (parameter_list);
27224 /* Check the template arguments for a literal operator template. */
27225 if (decl
27226 && DECL_DECLARES_FUNCTION_P (decl)
27227 && UDLIT_OPER_P (DECL_NAME (decl)))
27229 bool ok = true;
27230 if (parameter_list == NULL_TREE)
27231 ok = false;
27232 else
27234 int num_parms = TREE_VEC_LENGTH (parameter_list);
27235 if (num_parms == 1)
27237 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27238 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27239 if (CLASS_TYPE_P (TREE_TYPE (parm)))
27240 /* OK, C++20 string literal operator template. We don't need
27241 to warn in lower dialects here because we will have already
27242 warned about the template parameter. */;
27243 else if (TREE_TYPE (parm) != char_type_node
27244 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27245 ok = false;
27247 else if (num_parms == 2 && cxx_dialect >= cxx14)
27249 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27250 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27251 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27252 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27253 if (parm == error_mark_node
27254 || TREE_TYPE (parm) != TREE_TYPE (type)
27255 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27256 ok = false;
27257 else
27258 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27259 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
27260 "ISO C++ did not adopt string literal operator templa"
27261 "tes taking an argument pack of characters");
27263 else
27264 ok = false;
27266 if (!ok)
27268 if (cxx_dialect > cxx17)
27269 error ("literal operator template %qD has invalid parameter list;"
27270 " Expected non-type template parameter pack <char...> "
27271 " or single non-type parameter of class type",
27272 decl);
27273 else
27274 error ("literal operator template %qD has invalid parameter list."
27275 " Expected non-type template parameter pack <char...>",
27276 decl);
27280 /* Register member declarations. */
27281 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27282 finish_member_declaration (decl);
27283 /* If DECL is a function template, we must return to parse it later.
27284 (Even though there is no definition, there might be default
27285 arguments that need handling.) */
27286 if (member_p && decl
27287 && DECL_DECLARES_FUNCTION_P (decl))
27288 vec_safe_push (unparsed_funs_with_definitions, decl);
27291 /* Parse a template introduction header for a template-declaration. Returns
27292 false if tentative parse fails. */
27294 static bool
27295 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27297 cp_parser_parse_tentatively (parser);
27299 tree saved_scope = parser->scope;
27300 tree saved_object_scope = parser->object_scope;
27301 tree saved_qualifying_scope = parser->qualifying_scope;
27303 /* Look for the optional `::' operator. */
27304 cp_parser_global_scope_opt (parser,
27305 /*current_scope_valid_p=*/false);
27306 /* Look for the nested-name-specifier. */
27307 cp_parser_nested_name_specifier_opt (parser,
27308 /*typename_keyword_p=*/false,
27309 /*check_dependency_p=*/true,
27310 /*type_p=*/false,
27311 /*is_declaration=*/false);
27313 cp_token *token = cp_lexer_peek_token (parser->lexer);
27314 tree concept_name = cp_parser_identifier (parser);
27316 /* Look up the concept for which we will be matching
27317 template parameters. */
27318 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27319 token->location);
27320 parser->scope = saved_scope;
27321 parser->object_scope = saved_object_scope;
27322 parser->qualifying_scope = saved_qualifying_scope;
27324 if (concept_name == error_mark_node)
27325 cp_parser_simulate_error (parser);
27327 /* Look for opening brace for introduction. */
27328 matching_braces braces;
27329 braces.require_open (parser);
27331 if (!cp_parser_parse_definitely (parser))
27332 return false;
27334 push_deferring_access_checks (dk_deferred);
27336 /* Build vector of placeholder parameters and grab
27337 matching identifiers. */
27338 tree introduction_list = cp_parser_introduction_list (parser);
27340 /* Look for closing brace for introduction. */
27341 if (!braces.require_close (parser))
27342 return true;
27344 /* The introduction-list shall not be empty. */
27345 int nargs = TREE_VEC_LENGTH (introduction_list);
27346 if (nargs == 0)
27348 /* In cp_parser_introduction_list we have already issued an error. */
27349 return true;
27352 if (tmpl_decl == error_mark_node)
27354 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27355 token->location);
27356 return true;
27359 /* Build and associate the constraint. */
27360 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27361 if (parms && parms != error_mark_node)
27363 cp_parser_template_declaration_after_parameters (parser, parms,
27364 member_p);
27365 return true;
27368 error_at (token->location, "no matching concept for template-introduction");
27369 return true;
27372 /* Parse a normal template-declaration following the template keyword. */
27374 static void
27375 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27377 tree parameter_list;
27378 bool need_lang_pop;
27379 location_t location = input_location;
27381 /* Look for the `<' token. */
27382 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27383 return;
27384 if (at_class_scope_p () && current_function_decl)
27386 /* 14.5.2.2 [temp.mem]
27388 A local class shall not have member templates. */
27389 error_at (location,
27390 "invalid declaration of member template in local class");
27391 cp_parser_skip_to_end_of_block_or_statement (parser);
27392 return;
27394 /* [temp]
27396 A template ... shall not have C linkage. */
27397 if (current_lang_name == lang_name_c)
27399 error_at (location, "template with C linkage");
27400 maybe_show_extern_c_location ();
27401 /* Give it C++ linkage to avoid confusing other parts of the
27402 front end. */
27403 push_lang_context (lang_name_cplusplus);
27404 need_lang_pop = true;
27406 else
27407 need_lang_pop = false;
27409 /* We cannot perform access checks on the template parameter
27410 declarations until we know what is being declared, just as we
27411 cannot check the decl-specifier list. */
27412 push_deferring_access_checks (dk_deferred);
27414 /* If the next token is `>', then we have an invalid
27415 specialization. Rather than complain about an invalid template
27416 parameter, issue an error message here. */
27417 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27419 cp_parser_error (parser, "invalid explicit specialization");
27420 begin_specialization ();
27421 parameter_list = NULL_TREE;
27423 else
27425 /* Parse the template parameters. */
27426 parameter_list = cp_parser_template_parameter_list (parser);
27429 /* Look for the `>'. */
27430 cp_parser_skip_to_end_of_template_parameter_list (parser);
27432 /* Manage template requirements */
27433 if (flag_concepts)
27435 tree reqs = get_shorthand_constraints (current_template_parms);
27436 if (tree r = cp_parser_requires_clause_opt (parser))
27437 reqs = conjoin_constraints (reqs, normalize_expression (r));
27438 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27441 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27442 member_p);
27444 /* For the erroneous case of a template with C linkage, we pushed an
27445 implicit C++ linkage scope; exit that scope now. */
27446 if (need_lang_pop)
27447 pop_lang_context ();
27450 /* Parse a template-declaration, assuming that the `export' (and
27451 `extern') keywords, if present, has already been scanned. MEMBER_P
27452 is as for cp_parser_template_declaration. */
27454 static bool
27455 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27457 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27459 cp_lexer_consume_token (parser->lexer);
27460 cp_parser_explicit_template_declaration (parser, member_p);
27461 return true;
27463 else if (flag_concepts)
27464 return cp_parser_template_introduction (parser, member_p);
27466 return false;
27469 /* Perform the deferred access checks from a template-parameter-list.
27470 CHECKS is a TREE_LIST of access checks, as returned by
27471 get_deferred_access_checks. */
27473 static void
27474 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27476 ++processing_template_parmlist;
27477 perform_access_checks (checks, tf_warning_or_error);
27478 --processing_template_parmlist;
27481 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27482 `function-definition' sequence that follows a template header.
27483 If MEMBER_P is true, this declaration appears in a class scope.
27485 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27486 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27488 static tree
27489 cp_parser_single_declaration (cp_parser* parser,
27490 vec<deferred_access_check, va_gc> *checks,
27491 bool member_p,
27492 bool explicit_specialization_p,
27493 bool* friend_p)
27495 int declares_class_or_enum;
27496 tree decl = NULL_TREE;
27497 cp_decl_specifier_seq decl_specifiers;
27498 bool function_definition_p = false;
27499 cp_token *decl_spec_token_start;
27501 /* This function is only used when processing a template
27502 declaration. */
27503 gcc_assert (innermost_scope_kind () == sk_template_parms
27504 || innermost_scope_kind () == sk_template_spec);
27506 /* Defer access checks until we know what is being declared. */
27507 push_deferring_access_checks (dk_deferred);
27509 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27510 alternative. */
27511 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27512 cp_parser_decl_specifier_seq (parser,
27513 CP_PARSER_FLAGS_OPTIONAL,
27514 &decl_specifiers,
27515 &declares_class_or_enum);
27516 if (friend_p)
27517 *friend_p = cp_parser_friend_p (&decl_specifiers);
27519 /* There are no template typedefs. */
27520 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27522 error_at (decl_spec_token_start->location,
27523 "template declaration of %<typedef%>");
27524 decl = error_mark_node;
27527 /* Gather up the access checks that occurred the
27528 decl-specifier-seq. */
27529 stop_deferring_access_checks ();
27531 /* Check for the declaration of a template class. */
27532 if (declares_class_or_enum)
27534 if (cp_parser_declares_only_class_p (parser)
27535 || (declares_class_or_enum & 2))
27537 // If this is a declaration, but not a definition, associate
27538 // any constraints with the type declaration. Constraints
27539 // are associated with definitions in cp_parser_class_specifier.
27540 if (declares_class_or_enum == 1)
27541 associate_classtype_constraints (decl_specifiers.type);
27543 decl = shadow_tag (&decl_specifiers);
27545 /* In this case:
27547 struct C {
27548 friend template <typename T> struct A<T>::B;
27551 A<T>::B will be represented by a TYPENAME_TYPE, and
27552 therefore not recognized by shadow_tag. */
27553 if (friend_p && *friend_p
27554 && !decl
27555 && decl_specifiers.type
27556 && TYPE_P (decl_specifiers.type))
27557 decl = decl_specifiers.type;
27559 if (decl && decl != error_mark_node)
27560 decl = TYPE_NAME (decl);
27561 else
27562 decl = error_mark_node;
27564 /* Perform access checks for template parameters. */
27565 cp_parser_perform_template_parameter_access_checks (checks);
27567 /* Give a helpful diagnostic for
27568 template <class T> struct A { } a;
27569 if we aren't already recovering from an error. */
27570 if (!cp_parser_declares_only_class_p (parser)
27571 && !seen_error ())
27573 error_at (cp_lexer_peek_token (parser->lexer)->location,
27574 "a class template declaration must not declare "
27575 "anything else");
27576 cp_parser_skip_to_end_of_block_or_statement (parser);
27577 goto out;
27582 /* Complain about missing 'typename' or other invalid type names. */
27583 if (!decl_specifiers.any_type_specifiers_p
27584 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27586 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27587 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27588 the rest of this declaration. */
27589 decl = error_mark_node;
27590 goto out;
27593 /* If it's not a template class, try for a template function. If
27594 the next token is a `;', then this declaration does not declare
27595 anything. But, if there were errors in the decl-specifiers, then
27596 the error might well have come from an attempted class-specifier.
27597 In that case, there's no need to warn about a missing declarator. */
27598 if (!decl
27599 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27600 || decl_specifiers.type != error_mark_node))
27602 decl = cp_parser_init_declarator (parser,
27603 &decl_specifiers,
27604 checks,
27605 /*function_definition_allowed_p=*/true,
27606 member_p,
27607 declares_class_or_enum,
27608 &function_definition_p,
27609 NULL, NULL, NULL);
27611 /* 7.1.1-1 [dcl.stc]
27613 A storage-class-specifier shall not be specified in an explicit
27614 specialization... */
27615 if (decl
27616 && explicit_specialization_p
27617 && decl_specifiers.storage_class != sc_none)
27619 error_at (decl_spec_token_start->location,
27620 "explicit template specialization cannot have a storage class");
27621 decl = error_mark_node;
27624 if (decl && VAR_P (decl))
27625 check_template_variable (decl);
27628 /* Look for a trailing `;' after the declaration. */
27629 if (!function_definition_p
27630 && (decl == error_mark_node
27631 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27632 cp_parser_skip_to_end_of_block_or_statement (parser);
27634 out:
27635 pop_deferring_access_checks ();
27637 /* Clear any current qualification; whatever comes next is the start
27638 of something new. */
27639 parser->scope = NULL_TREE;
27640 parser->qualifying_scope = NULL_TREE;
27641 parser->object_scope = NULL_TREE;
27643 return decl;
27646 /* Parse a cast-expression that is not the operand of a unary "&". */
27648 static cp_expr
27649 cp_parser_simple_cast_expression (cp_parser *parser)
27651 return cp_parser_cast_expression (parser, /*address_p=*/false,
27652 /*cast_p=*/false, /*decltype*/false, NULL);
27655 /* Parse a functional cast to TYPE. Returns an expression
27656 representing the cast. */
27658 static cp_expr
27659 cp_parser_functional_cast (cp_parser* parser, tree type)
27661 vec<tree, va_gc> *vec;
27662 tree expression_list;
27663 cp_expr cast;
27664 bool nonconst_p;
27666 location_t start_loc = input_location;
27668 if (!type)
27669 type = error_mark_node;
27671 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27673 cp_lexer_set_source_position (parser->lexer);
27674 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27675 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27676 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27677 if (TREE_CODE (type) == TYPE_DECL)
27678 type = TREE_TYPE (type);
27680 cast = finish_compound_literal (type, expression_list,
27681 tf_warning_or_error, fcl_functional);
27682 /* Create a location of the form:
27683 type_name{i, f}
27684 ^~~~~~~~~~~~~~~
27685 with caret == start at the start of the type name,
27686 finishing at the closing brace. */
27687 location_t finish_loc
27688 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27689 location_t combined_loc = make_location (start_loc, start_loc,
27690 finish_loc);
27691 cast.set_location (combined_loc);
27692 return cast;
27696 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27697 /*cast_p=*/true,
27698 /*allow_expansion_p=*/true,
27699 /*non_constant_p=*/NULL);
27700 if (vec == NULL)
27701 expression_list = error_mark_node;
27702 else
27704 expression_list = build_tree_list_vec (vec);
27705 release_tree_vector (vec);
27708 cast = build_functional_cast (type, expression_list,
27709 tf_warning_or_error);
27710 /* [expr.const]/1: In an integral constant expression "only type
27711 conversions to integral or enumeration type can be used". */
27712 if (TREE_CODE (type) == TYPE_DECL)
27713 type = TREE_TYPE (type);
27714 if (cast != error_mark_node
27715 && !cast_valid_in_integral_constant_expression_p (type)
27716 && cp_parser_non_integral_constant_expression (parser,
27717 NIC_CONSTRUCTOR))
27718 return error_mark_node;
27720 /* Create a location of the form:
27721 float(i)
27722 ^~~~~~~~
27723 with caret == start at the start of the type name,
27724 finishing at the closing paren. */
27725 location_t finish_loc
27726 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27727 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27728 cast.set_location (combined_loc);
27729 return cast;
27732 /* Save the tokens that make up the body of a member function defined
27733 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27734 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27735 specifiers applied to the declaration. Returns the FUNCTION_DECL
27736 for the member function. */
27738 static tree
27739 cp_parser_save_member_function_body (cp_parser* parser,
27740 cp_decl_specifier_seq *decl_specifiers,
27741 cp_declarator *declarator,
27742 tree attributes)
27744 cp_token *first;
27745 cp_token *last;
27746 tree fn;
27747 bool function_try_block = false;
27749 /* Create the FUNCTION_DECL. */
27750 fn = grokmethod (decl_specifiers, declarator, attributes);
27751 cp_finalize_omp_declare_simd (parser, fn);
27752 cp_finalize_oacc_routine (parser, fn, true);
27753 /* If something went badly wrong, bail out now. */
27754 if (fn == error_mark_node)
27756 /* If there's a function-body, skip it. */
27757 if (cp_parser_token_starts_function_definition_p
27758 (cp_lexer_peek_token (parser->lexer)))
27759 cp_parser_skip_to_end_of_block_or_statement (parser);
27760 return error_mark_node;
27763 /* Remember it, if there default args to post process. */
27764 cp_parser_save_default_args (parser, fn);
27766 /* Save away the tokens that make up the body of the
27767 function. */
27768 first = parser->lexer->next_token;
27770 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27771 cp_lexer_consume_token (parser->lexer);
27772 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27773 RID_TRANSACTION_ATOMIC))
27775 cp_lexer_consume_token (parser->lexer);
27776 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27777 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27778 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27779 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27780 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27781 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27782 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27784 cp_lexer_consume_token (parser->lexer);
27785 cp_lexer_consume_token (parser->lexer);
27786 cp_lexer_consume_token (parser->lexer);
27787 cp_lexer_consume_token (parser->lexer);
27788 cp_lexer_consume_token (parser->lexer);
27790 else
27791 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27792 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27794 cp_lexer_consume_token (parser->lexer);
27795 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27796 break;
27800 /* Handle function try blocks. */
27801 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27803 cp_lexer_consume_token (parser->lexer);
27804 function_try_block = true;
27806 /* We can have braced-init-list mem-initializers before the fn body. */
27807 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27809 cp_lexer_consume_token (parser->lexer);
27810 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27812 /* cache_group will stop after an un-nested { } pair, too. */
27813 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27814 break;
27816 /* variadic mem-inits have ... after the ')'. */
27817 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27818 cp_lexer_consume_token (parser->lexer);
27821 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27822 /* Handle function try blocks. */
27823 if (function_try_block)
27824 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27825 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27826 last = parser->lexer->next_token;
27828 /* Save away the inline definition; we will process it when the
27829 class is complete. */
27830 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27831 DECL_PENDING_INLINE_P (fn) = 1;
27833 /* We need to know that this was defined in the class, so that
27834 friend templates are handled correctly. */
27835 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27837 /* Add FN to the queue of functions to be parsed later. */
27838 vec_safe_push (unparsed_funs_with_definitions, fn);
27840 return fn;
27843 /* Save the tokens that make up the in-class initializer for a non-static
27844 data member. Returns a DEFAULT_ARG. */
27846 static tree
27847 cp_parser_save_nsdmi (cp_parser* parser)
27849 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27852 /* Parse a template-argument-list, as well as the trailing ">" (but
27853 not the opening "<"). See cp_parser_template_argument_list for the
27854 return value. */
27856 static tree
27857 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27859 tree arguments;
27860 tree saved_scope;
27861 tree saved_qualifying_scope;
27862 tree saved_object_scope;
27863 bool saved_greater_than_is_operator_p;
27864 int saved_unevaluated_operand;
27865 int saved_inhibit_evaluation_warnings;
27867 /* [temp.names]
27869 When parsing a template-id, the first non-nested `>' is taken as
27870 the end of the template-argument-list rather than a greater-than
27871 operator. */
27872 saved_greater_than_is_operator_p
27873 = parser->greater_than_is_operator_p;
27874 parser->greater_than_is_operator_p = false;
27875 /* Parsing the argument list may modify SCOPE, so we save it
27876 here. */
27877 saved_scope = parser->scope;
27878 saved_qualifying_scope = parser->qualifying_scope;
27879 saved_object_scope = parser->object_scope;
27880 /* We need to evaluate the template arguments, even though this
27881 template-id may be nested within a "sizeof". */
27882 saved_unevaluated_operand = cp_unevaluated_operand;
27883 cp_unevaluated_operand = 0;
27884 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27885 c_inhibit_evaluation_warnings = 0;
27886 /* Parse the template-argument-list itself. */
27887 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27888 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27889 arguments = NULL_TREE;
27890 else
27891 arguments = cp_parser_template_argument_list (parser);
27892 /* Look for the `>' that ends the template-argument-list. If we find
27893 a '>>' instead, it's probably just a typo. */
27894 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27896 if (cxx_dialect != cxx98)
27898 /* In C++0x, a `>>' in a template argument list or cast
27899 expression is considered to be two separate `>'
27900 tokens. So, change the current token to a `>', but don't
27901 consume it: it will be consumed later when the outer
27902 template argument list (or cast expression) is parsed.
27903 Note that this replacement of `>' for `>>' is necessary
27904 even if we are parsing tentatively: in the tentative
27905 case, after calling
27906 cp_parser_enclosed_template_argument_list we will always
27907 throw away all of the template arguments and the first
27908 closing `>', either because the template argument list
27909 was erroneous or because we are replacing those tokens
27910 with a CPP_TEMPLATE_ID token. The second `>' (which will
27911 not have been thrown away) is needed either to close an
27912 outer template argument list or to complete a new-style
27913 cast. */
27914 cp_token *token = cp_lexer_peek_token (parser->lexer);
27915 token->type = CPP_GREATER;
27917 else if (!saved_greater_than_is_operator_p)
27919 /* If we're in a nested template argument list, the '>>' has
27920 to be a typo for '> >'. We emit the error message, but we
27921 continue parsing and we push a '>' as next token, so that
27922 the argument list will be parsed correctly. Note that the
27923 global source location is still on the token before the
27924 '>>', so we need to say explicitly where we want it. */
27925 cp_token *token = cp_lexer_peek_token (parser->lexer);
27926 gcc_rich_location richloc (token->location);
27927 richloc.add_fixit_replace ("> >");
27928 error_at (&richloc, "%<>>%> should be %<> >%> "
27929 "within a nested template argument list");
27931 token->type = CPP_GREATER;
27933 else
27935 /* If this is not a nested template argument list, the '>>'
27936 is a typo for '>'. Emit an error message and continue.
27937 Same deal about the token location, but here we can get it
27938 right by consuming the '>>' before issuing the diagnostic. */
27939 cp_token *token = cp_lexer_consume_token (parser->lexer);
27940 error_at (token->location,
27941 "spurious %<>>%>, use %<>%> to terminate "
27942 "a template argument list");
27945 else
27946 cp_parser_skip_to_end_of_template_parameter_list (parser);
27947 /* The `>' token might be a greater-than operator again now. */
27948 parser->greater_than_is_operator_p
27949 = saved_greater_than_is_operator_p;
27950 /* Restore the SAVED_SCOPE. */
27951 parser->scope = saved_scope;
27952 parser->qualifying_scope = saved_qualifying_scope;
27953 parser->object_scope = saved_object_scope;
27954 cp_unevaluated_operand = saved_unevaluated_operand;
27955 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27957 return arguments;
27960 /* MEMBER_FUNCTION is a member function, or a friend. If default
27961 arguments, or the body of the function have not yet been parsed,
27962 parse them now. */
27964 static void
27965 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27967 timevar_push (TV_PARSE_INMETH);
27968 /* If this member is a template, get the underlying
27969 FUNCTION_DECL. */
27970 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27971 member_function = DECL_TEMPLATE_RESULT (member_function);
27973 /* There should not be any class definitions in progress at this
27974 point; the bodies of members are only parsed outside of all class
27975 definitions. */
27976 gcc_assert (parser->num_classes_being_defined == 0);
27977 /* While we're parsing the member functions we might encounter more
27978 classes. We want to handle them right away, but we don't want
27979 them getting mixed up with functions that are currently in the
27980 queue. */
27981 push_unparsed_function_queues (parser);
27983 /* Make sure that any template parameters are in scope. */
27984 maybe_begin_member_template_processing (member_function);
27986 /* If the body of the function has not yet been parsed, parse it
27987 now. */
27988 if (DECL_PENDING_INLINE_P (member_function))
27990 tree function_scope;
27991 cp_token_cache *tokens;
27993 /* The function is no longer pending; we are processing it. */
27994 tokens = DECL_PENDING_INLINE_INFO (member_function);
27995 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27996 DECL_PENDING_INLINE_P (member_function) = 0;
27998 /* If this is a local class, enter the scope of the containing
27999 function. */
28000 function_scope = current_function_decl;
28001 if (function_scope)
28002 push_function_context ();
28004 /* Push the body of the function onto the lexer stack. */
28005 cp_parser_push_lexer_for_tokens (parser, tokens);
28007 /* Let the front end know that we going to be defining this
28008 function. */
28009 start_preparsed_function (member_function, NULL_TREE,
28010 SF_PRE_PARSED | SF_INCLASS_INLINE);
28012 /* Don't do access checking if it is a templated function. */
28013 if (processing_template_decl)
28014 push_deferring_access_checks (dk_no_check);
28016 /* #pragma omp declare reduction needs special parsing. */
28017 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28019 parser->lexer->in_pragma = true;
28020 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28021 finish_function (/*inline_p=*/true);
28022 cp_check_omp_declare_reduction (member_function);
28024 else
28025 /* Now, parse the body of the function. */
28026 cp_parser_function_definition_after_declarator (parser,
28027 /*inline_p=*/true);
28029 if (processing_template_decl)
28030 pop_deferring_access_checks ();
28032 /* Leave the scope of the containing function. */
28033 if (function_scope)
28034 pop_function_context ();
28035 cp_parser_pop_lexer (parser);
28038 /* Remove any template parameters from the symbol table. */
28039 maybe_end_member_template_processing ();
28041 /* Restore the queue. */
28042 pop_unparsed_function_queues (parser);
28043 timevar_pop (TV_PARSE_INMETH);
28046 /* If DECL contains any default args, remember it on the unparsed
28047 functions queue. */
28049 static void
28050 cp_parser_save_default_args (cp_parser* parser, tree decl)
28052 tree probe;
28054 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28055 probe;
28056 probe = TREE_CHAIN (probe))
28057 if (TREE_PURPOSE (probe))
28059 cp_default_arg_entry entry = {current_class_type, decl};
28060 vec_safe_push (unparsed_funs_with_default_args, entry);
28061 break;
28065 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28066 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28067 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28068 from the parameter-type-list. */
28070 static tree
28071 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28072 tree default_arg, tree parmtype)
28074 cp_token_cache *tokens;
28075 tree parsed_arg;
28076 bool dummy;
28078 if (default_arg == error_mark_node)
28079 return error_mark_node;
28081 /* Push the saved tokens for the default argument onto the parser's
28082 lexer stack. */
28083 tokens = DEFARG_TOKENS (default_arg);
28084 cp_parser_push_lexer_for_tokens (parser, tokens);
28086 start_lambda_scope (decl);
28088 /* Parse the default argument. */
28089 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28090 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28091 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28093 finish_lambda_scope ();
28095 if (parsed_arg == error_mark_node)
28096 cp_parser_skip_to_end_of_statement (parser);
28098 if (!processing_template_decl)
28100 /* In a non-template class, check conversions now. In a template,
28101 we'll wait and instantiate these as needed. */
28102 if (TREE_CODE (decl) == PARM_DECL)
28103 parsed_arg = check_default_argument (parmtype, parsed_arg,
28104 tf_warning_or_error);
28105 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28106 parsed_arg = error_mark_node;
28107 else
28108 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28111 /* If the token stream has not been completely used up, then
28112 there was extra junk after the end of the default
28113 argument. */
28114 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28116 if (TREE_CODE (decl) == PARM_DECL)
28117 cp_parser_error (parser, "expected %<,%>");
28118 else
28119 cp_parser_error (parser, "expected %<;%>");
28122 /* Revert to the main lexer. */
28123 cp_parser_pop_lexer (parser);
28125 return parsed_arg;
28128 /* FIELD is a non-static data member with an initializer which we saved for
28129 later; parse it now. */
28131 static void
28132 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28134 tree def;
28136 maybe_begin_member_template_processing (field);
28138 push_unparsed_function_queues (parser);
28139 def = cp_parser_late_parse_one_default_arg (parser, field,
28140 DECL_INITIAL (field),
28141 NULL_TREE);
28142 pop_unparsed_function_queues (parser);
28144 maybe_end_member_template_processing ();
28146 DECL_INITIAL (field) = def;
28149 /* FN is a FUNCTION_DECL which may contains a parameter with an
28150 unparsed DEFAULT_ARG. Parse the default args now. This function
28151 assumes that the current scope is the scope in which the default
28152 argument should be processed. */
28154 static void
28155 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28157 bool saved_local_variables_forbidden_p;
28158 tree parm, parmdecl;
28160 /* While we're parsing the default args, we might (due to the
28161 statement expression extension) encounter more classes. We want
28162 to handle them right away, but we don't want them getting mixed
28163 up with default args that are currently in the queue. */
28164 push_unparsed_function_queues (parser);
28166 /* Local variable names (and the `this' keyword) may not appear
28167 in a default argument. */
28168 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28169 parser->local_variables_forbidden_p = true;
28171 push_defarg_context (fn);
28173 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28174 parmdecl = DECL_ARGUMENTS (fn);
28175 parm && parm != void_list_node;
28176 parm = TREE_CHAIN (parm),
28177 parmdecl = DECL_CHAIN (parmdecl))
28179 tree default_arg = TREE_PURPOSE (parm);
28180 tree parsed_arg;
28181 vec<tree, va_gc> *insts;
28182 tree copy;
28183 unsigned ix;
28185 if (!default_arg)
28186 continue;
28188 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28189 /* This can happen for a friend declaration for a function
28190 already declared with default arguments. */
28191 continue;
28193 parsed_arg
28194 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28195 default_arg,
28196 TREE_VALUE (parm));
28197 TREE_PURPOSE (parm) = parsed_arg;
28199 /* Update any instantiations we've already created. */
28200 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28201 vec_safe_iterate (insts, ix, &copy); ix++)
28202 TREE_PURPOSE (copy) = parsed_arg;
28205 pop_defarg_context ();
28207 /* Make sure no default arg is missing. */
28208 check_default_args (fn);
28210 /* Restore the state of local_variables_forbidden_p. */
28211 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28213 /* Restore the queue. */
28214 pop_unparsed_function_queues (parser);
28217 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28219 sizeof ... ( identifier )
28221 where the 'sizeof' token has already been consumed. */
28223 static tree
28224 cp_parser_sizeof_pack (cp_parser *parser)
28226 /* Consume the `...'. */
28227 cp_lexer_consume_token (parser->lexer);
28228 maybe_warn_variadic_templates ();
28230 matching_parens parens;
28231 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28232 if (paren)
28233 parens.consume_open (parser);
28234 else
28235 permerror (cp_lexer_peek_token (parser->lexer)->location,
28236 "%<sizeof...%> argument must be surrounded by parentheses");
28238 cp_token *token = cp_lexer_peek_token (parser->lexer);
28239 tree name = cp_parser_identifier (parser);
28240 if (name == error_mark_node)
28241 return error_mark_node;
28242 /* The name is not qualified. */
28243 parser->scope = NULL_TREE;
28244 parser->qualifying_scope = NULL_TREE;
28245 parser->object_scope = NULL_TREE;
28246 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28247 if (expr == error_mark_node)
28248 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28249 token->location);
28250 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28251 expr = TREE_TYPE (expr);
28252 else if (TREE_CODE (expr) == CONST_DECL)
28253 expr = DECL_INITIAL (expr);
28254 expr = make_pack_expansion (expr);
28255 PACK_EXPANSION_SIZEOF_P (expr) = true;
28257 if (paren)
28258 parens.require_close (parser);
28260 return expr;
28263 /* Parse the operand of `sizeof' (or a similar operator). Returns
28264 either a TYPE or an expression, depending on the form of the
28265 input. The KEYWORD indicates which kind of expression we have
28266 encountered. */
28268 static tree
28269 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28271 tree expr = NULL_TREE;
28272 const char *saved_message;
28273 char *tmp;
28274 bool saved_integral_constant_expression_p;
28275 bool saved_non_integral_constant_expression_p;
28277 /* If it's a `...', then we are computing the length of a parameter
28278 pack. */
28279 if (keyword == RID_SIZEOF
28280 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28281 return cp_parser_sizeof_pack (parser);
28283 /* Types cannot be defined in a `sizeof' expression. Save away the
28284 old message. */
28285 saved_message = parser->type_definition_forbidden_message;
28286 /* And create the new one. */
28287 tmp = concat ("types may not be defined in %<",
28288 IDENTIFIER_POINTER (ridpointers[keyword]),
28289 "%> expressions", NULL);
28290 parser->type_definition_forbidden_message = tmp;
28292 /* The restrictions on constant-expressions do not apply inside
28293 sizeof expressions. */
28294 saved_integral_constant_expression_p
28295 = parser->integral_constant_expression_p;
28296 saved_non_integral_constant_expression_p
28297 = parser->non_integral_constant_expression_p;
28298 parser->integral_constant_expression_p = false;
28300 /* Do not actually evaluate the expression. */
28301 ++cp_unevaluated_operand;
28302 ++c_inhibit_evaluation_warnings;
28303 /* If it's a `(', then we might be looking at the type-id
28304 construction. */
28305 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28307 tree type = NULL_TREE;
28309 /* We can't be sure yet whether we're looking at a type-id or an
28310 expression. */
28311 cp_parser_parse_tentatively (parser);
28313 matching_parens parens;
28314 parens.consume_open (parser);
28316 /* Note: as a GNU Extension, compound literals are considered
28317 postfix-expressions as they are in C99, so they are valid
28318 arguments to sizeof. See comment in cp_parser_cast_expression
28319 for details. */
28320 if (cp_parser_compound_literal_p (parser))
28321 cp_parser_simulate_error (parser);
28322 else
28324 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28325 parser->in_type_id_in_expr_p = true;
28326 /* Look for the type-id. */
28327 type = cp_parser_type_id (parser);
28328 /* Look for the closing `)'. */
28329 parens.require_close (parser);
28330 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28333 /* If all went well, then we're done. */
28334 if (cp_parser_parse_definitely (parser))
28335 expr = type;
28338 /* If the type-id production did not work out, then we must be
28339 looking at the unary-expression production. */
28340 if (!expr)
28341 expr = cp_parser_unary_expression (parser);
28343 /* Go back to evaluating expressions. */
28344 --cp_unevaluated_operand;
28345 --c_inhibit_evaluation_warnings;
28347 /* Free the message we created. */
28348 free (tmp);
28349 /* And restore the old one. */
28350 parser->type_definition_forbidden_message = saved_message;
28351 parser->integral_constant_expression_p
28352 = saved_integral_constant_expression_p;
28353 parser->non_integral_constant_expression_p
28354 = saved_non_integral_constant_expression_p;
28356 return expr;
28359 /* If the current declaration has no declarator, return true. */
28361 static bool
28362 cp_parser_declares_only_class_p (cp_parser *parser)
28364 /* If the next token is a `;' or a `,' then there is no
28365 declarator. */
28366 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28367 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28370 /* Update the DECL_SPECS to reflect the storage class indicated by
28371 KEYWORD. */
28373 static void
28374 cp_parser_set_storage_class (cp_parser *parser,
28375 cp_decl_specifier_seq *decl_specs,
28376 enum rid keyword,
28377 cp_token *token)
28379 cp_storage_class storage_class;
28381 if (parser->in_unbraced_linkage_specification_p)
28383 error_at (token->location, "invalid use of %qD in linkage specification",
28384 ridpointers[keyword]);
28385 return;
28387 else if (decl_specs->storage_class != sc_none)
28389 decl_specs->conflicting_specifiers_p = true;
28390 return;
28393 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28394 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28395 && decl_specs->gnu_thread_keyword_p)
28397 pedwarn (decl_specs->locations[ds_thread], 0,
28398 "%<__thread%> before %qD", ridpointers[keyword]);
28401 switch (keyword)
28403 case RID_AUTO:
28404 storage_class = sc_auto;
28405 break;
28406 case RID_REGISTER:
28407 storage_class = sc_register;
28408 break;
28409 case RID_STATIC:
28410 storage_class = sc_static;
28411 break;
28412 case RID_EXTERN:
28413 storage_class = sc_extern;
28414 break;
28415 case RID_MUTABLE:
28416 storage_class = sc_mutable;
28417 break;
28418 default:
28419 gcc_unreachable ();
28421 decl_specs->storage_class = storage_class;
28422 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28424 /* A storage class specifier cannot be applied alongside a typedef
28425 specifier. If there is a typedef specifier present then set
28426 conflicting_specifiers_p which will trigger an error later
28427 on in grokdeclarator. */
28428 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28429 decl_specs->conflicting_specifiers_p = true;
28432 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28433 is true, the type is a class or enum definition. */
28435 static void
28436 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28437 tree type_spec,
28438 cp_token *token,
28439 bool type_definition_p)
28441 decl_specs->any_specifiers_p = true;
28443 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28444 (with, for example, in "typedef int wchar_t;") we remember that
28445 this is what happened. In system headers, we ignore these
28446 declarations so that G++ can work with system headers that are not
28447 C++-safe. */
28448 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28449 && !type_definition_p
28450 && (type_spec == boolean_type_node
28451 || type_spec == char16_type_node
28452 || type_spec == char32_type_node
28453 || type_spec == wchar_type_node)
28454 && (decl_specs->type
28455 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28456 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28457 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28458 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28460 decl_specs->redefined_builtin_type = type_spec;
28461 set_and_check_decl_spec_loc (decl_specs,
28462 ds_redefined_builtin_type_spec,
28463 token);
28464 if (!decl_specs->type)
28466 decl_specs->type = type_spec;
28467 decl_specs->type_definition_p = false;
28468 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28471 else if (decl_specs->type)
28472 decl_specs->multiple_types_p = true;
28473 else
28475 decl_specs->type = type_spec;
28476 decl_specs->type_definition_p = type_definition_p;
28477 decl_specs->redefined_builtin_type = NULL_TREE;
28478 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28482 /* True iff TOKEN is the GNU keyword __thread. */
28484 static bool
28485 token_is__thread (cp_token *token)
28487 gcc_assert (token->keyword == RID_THREAD);
28488 return id_equal (token->u.value, "__thread");
28491 /* Set the location for a declarator specifier and check if it is
28492 duplicated.
28494 DECL_SPECS is the sequence of declarator specifiers onto which to
28495 set the location.
28497 DS is the single declarator specifier to set which location is to
28498 be set onto the existing sequence of declarators.
28500 LOCATION is the location for the declarator specifier to
28501 consider. */
28503 static void
28504 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28505 cp_decl_spec ds, cp_token *token)
28507 gcc_assert (ds < ds_last);
28509 if (decl_specs == NULL)
28510 return;
28512 source_location location = token->location;
28514 if (decl_specs->locations[ds] == 0)
28516 decl_specs->locations[ds] = location;
28517 if (ds == ds_thread)
28518 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28520 else
28522 if (ds == ds_long)
28524 if (decl_specs->locations[ds_long_long] != 0)
28525 error_at (location,
28526 "%<long long long%> is too long for GCC");
28527 else
28529 decl_specs->locations[ds_long_long] = location;
28530 pedwarn_cxx98 (location,
28531 OPT_Wlong_long,
28532 "ISO C++ 1998 does not support %<long long%>");
28535 else if (ds == ds_thread)
28537 bool gnu = token_is__thread (token);
28538 gcc_rich_location richloc (location);
28539 if (gnu != decl_specs->gnu_thread_keyword_p)
28541 richloc.add_range (decl_specs->locations[ds_thread]);
28542 error_at (&richloc,
28543 "both %<__thread%> and %<thread_local%> specified");
28545 else
28547 richloc.add_fixit_remove ();
28548 error_at (&richloc, "duplicate %qD", token->u.value);
28551 else
28553 static const char *const decl_spec_names[] = {
28554 "signed",
28555 "unsigned",
28556 "short",
28557 "long",
28558 "const",
28559 "volatile",
28560 "restrict",
28561 "inline",
28562 "virtual",
28563 "explicit",
28564 "friend",
28565 "typedef",
28566 "using",
28567 "constexpr",
28568 "__complex"
28570 gcc_rich_location richloc (location);
28571 richloc.add_fixit_remove ();
28572 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28577 /* Return true iff the declarator specifier DS is present in the
28578 sequence of declarator specifiers DECL_SPECS. */
28580 bool
28581 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28582 cp_decl_spec ds)
28584 gcc_assert (ds < ds_last);
28586 if (decl_specs == NULL)
28587 return false;
28589 return decl_specs->locations[ds] != 0;
28592 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28593 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28595 static bool
28596 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28598 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28601 /* Issue an error message indicating that TOKEN_DESC was expected.
28602 If KEYWORD is true, it indicated this function is called by
28603 cp_parser_require_keword and the required token can only be
28604 a indicated keyword.
28606 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28607 within any error as the location of an "opening" token matching
28608 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28609 RT_CLOSE_PAREN). */
28611 static void
28612 cp_parser_required_error (cp_parser *parser,
28613 required_token token_desc,
28614 bool keyword,
28615 location_t matching_location)
28617 if (cp_parser_simulate_error (parser))
28618 return;
28620 const char *gmsgid = NULL;
28621 switch (token_desc)
28623 case RT_NEW:
28624 gmsgid = G_("expected %<new%>");
28625 break;
28626 case RT_DELETE:
28627 gmsgid = G_("expected %<delete%>");
28628 break;
28629 case RT_RETURN:
28630 gmsgid = G_("expected %<return%>");
28631 break;
28632 case RT_WHILE:
28633 gmsgid = G_("expected %<while%>");
28634 break;
28635 case RT_EXTERN:
28636 gmsgid = G_("expected %<extern%>");
28637 break;
28638 case RT_STATIC_ASSERT:
28639 gmsgid = G_("expected %<static_assert%>");
28640 break;
28641 case RT_DECLTYPE:
28642 gmsgid = G_("expected %<decltype%>");
28643 break;
28644 case RT_OPERATOR:
28645 gmsgid = G_("expected %<operator%>");
28646 break;
28647 case RT_CLASS:
28648 gmsgid = G_("expected %<class%>");
28649 break;
28650 case RT_TEMPLATE:
28651 gmsgid = G_("expected %<template%>");
28652 break;
28653 case RT_NAMESPACE:
28654 gmsgid = G_("expected %<namespace%>");
28655 break;
28656 case RT_USING:
28657 gmsgid = G_("expected %<using%>");
28658 break;
28659 case RT_ASM:
28660 gmsgid = G_("expected %<asm%>");
28661 break;
28662 case RT_TRY:
28663 gmsgid = G_("expected %<try%>");
28664 break;
28665 case RT_CATCH:
28666 gmsgid = G_("expected %<catch%>");
28667 break;
28668 case RT_THROW:
28669 gmsgid = G_("expected %<throw%>");
28670 break;
28671 case RT_LABEL:
28672 gmsgid = G_("expected %<__label__%>");
28673 break;
28674 case RT_AT_TRY:
28675 gmsgid = G_("expected %<@try%>");
28676 break;
28677 case RT_AT_SYNCHRONIZED:
28678 gmsgid = G_("expected %<@synchronized%>");
28679 break;
28680 case RT_AT_THROW:
28681 gmsgid = G_("expected %<@throw%>");
28682 break;
28683 case RT_TRANSACTION_ATOMIC:
28684 gmsgid = G_("expected %<__transaction_atomic%>");
28685 break;
28686 case RT_TRANSACTION_RELAXED:
28687 gmsgid = G_("expected %<__transaction_relaxed%>");
28688 break;
28689 default:
28690 break;
28693 if (!gmsgid && !keyword)
28695 switch (token_desc)
28697 case RT_SEMICOLON:
28698 gmsgid = G_("expected %<;%>");
28699 break;
28700 case RT_OPEN_PAREN:
28701 gmsgid = G_("expected %<(%>");
28702 break;
28703 case RT_CLOSE_BRACE:
28704 gmsgid = G_("expected %<}%>");
28705 break;
28706 case RT_OPEN_BRACE:
28707 gmsgid = G_("expected %<{%>");
28708 break;
28709 case RT_CLOSE_SQUARE:
28710 gmsgid = G_("expected %<]%>");
28711 break;
28712 case RT_OPEN_SQUARE:
28713 gmsgid = G_("expected %<[%>");
28714 break;
28715 case RT_COMMA:
28716 gmsgid = G_("expected %<,%>");
28717 break;
28718 case RT_SCOPE:
28719 gmsgid = G_("expected %<::%>");
28720 break;
28721 case RT_LESS:
28722 gmsgid = G_("expected %<<%>");
28723 break;
28724 case RT_GREATER:
28725 gmsgid = G_("expected %<>%>");
28726 break;
28727 case RT_EQ:
28728 gmsgid = G_("expected %<=%>");
28729 break;
28730 case RT_ELLIPSIS:
28731 gmsgid = G_("expected %<...%>");
28732 break;
28733 case RT_MULT:
28734 gmsgid = G_("expected %<*%>");
28735 break;
28736 case RT_COMPL:
28737 gmsgid = G_("expected %<~%>");
28738 break;
28739 case RT_COLON:
28740 gmsgid = G_("expected %<:%>");
28741 break;
28742 case RT_COLON_SCOPE:
28743 gmsgid = G_("expected %<:%> or %<::%>");
28744 break;
28745 case RT_CLOSE_PAREN:
28746 gmsgid = G_("expected %<)%>");
28747 break;
28748 case RT_COMMA_CLOSE_PAREN:
28749 gmsgid = G_("expected %<,%> or %<)%>");
28750 break;
28751 case RT_PRAGMA_EOL:
28752 gmsgid = G_("expected end of line");
28753 break;
28754 case RT_NAME:
28755 gmsgid = G_("expected identifier");
28756 break;
28757 case RT_SELECT:
28758 gmsgid = G_("expected selection-statement");
28759 break;
28760 case RT_ITERATION:
28761 gmsgid = G_("expected iteration-statement");
28762 break;
28763 case RT_JUMP:
28764 gmsgid = G_("expected jump-statement");
28765 break;
28766 case RT_CLASS_KEY:
28767 gmsgid = G_("expected class-key");
28768 break;
28769 case RT_CLASS_TYPENAME_TEMPLATE:
28770 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28771 break;
28772 default:
28773 gcc_unreachable ();
28777 if (gmsgid)
28778 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28782 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28783 issue an error message indicating that TOKEN_DESC was expected.
28785 Returns the token consumed, if the token had the appropriate type.
28786 Otherwise, returns NULL.
28788 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28789 within any error as the location of an "opening" token matching
28790 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28791 RT_CLOSE_PAREN). */
28793 static cp_token *
28794 cp_parser_require (cp_parser* parser,
28795 enum cpp_ttype type,
28796 required_token token_desc,
28797 location_t matching_location)
28799 if (cp_lexer_next_token_is (parser->lexer, type))
28800 return cp_lexer_consume_token (parser->lexer);
28801 else
28803 /* Output the MESSAGE -- unless we're parsing tentatively. */
28804 if (!cp_parser_simulate_error (parser))
28805 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28806 matching_location);
28807 return NULL;
28811 /* An error message is produced if the next token is not '>'.
28812 All further tokens are skipped until the desired token is
28813 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28815 static void
28816 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28818 /* Current level of '< ... >'. */
28819 unsigned level = 0;
28820 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28821 unsigned nesting_depth = 0;
28823 /* Are we ready, yet? If not, issue error message. */
28824 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28825 return;
28827 /* Skip tokens until the desired token is found. */
28828 while (true)
28830 /* Peek at the next token. */
28831 switch (cp_lexer_peek_token (parser->lexer)->type)
28833 case CPP_LESS:
28834 if (!nesting_depth)
28835 ++level;
28836 break;
28838 case CPP_RSHIFT:
28839 if (cxx_dialect == cxx98)
28840 /* C++0x views the `>>' operator as two `>' tokens, but
28841 C++98 does not. */
28842 break;
28843 else if (!nesting_depth && level-- == 0)
28845 /* We've hit a `>>' where the first `>' closes the
28846 template argument list, and the second `>' is
28847 spurious. Just consume the `>>' and stop; we've
28848 already produced at least one error. */
28849 cp_lexer_consume_token (parser->lexer);
28850 return;
28852 /* Fall through for C++0x, so we handle the second `>' in
28853 the `>>'. */
28854 gcc_fallthrough ();
28856 case CPP_GREATER:
28857 if (!nesting_depth && level-- == 0)
28859 /* We've reached the token we want, consume it and stop. */
28860 cp_lexer_consume_token (parser->lexer);
28861 return;
28863 break;
28865 case CPP_OPEN_PAREN:
28866 case CPP_OPEN_SQUARE:
28867 ++nesting_depth;
28868 break;
28870 case CPP_CLOSE_PAREN:
28871 case CPP_CLOSE_SQUARE:
28872 if (nesting_depth-- == 0)
28873 return;
28874 break;
28876 case CPP_EOF:
28877 case CPP_PRAGMA_EOL:
28878 case CPP_SEMICOLON:
28879 case CPP_OPEN_BRACE:
28880 case CPP_CLOSE_BRACE:
28881 /* The '>' was probably forgotten, don't look further. */
28882 return;
28884 default:
28885 break;
28888 /* Consume this token. */
28889 cp_lexer_consume_token (parser->lexer);
28893 /* If the next token is the indicated keyword, consume it. Otherwise,
28894 issue an error message indicating that TOKEN_DESC was expected.
28896 Returns the token consumed, if the token had the appropriate type.
28897 Otherwise, returns NULL. */
28899 static cp_token *
28900 cp_parser_require_keyword (cp_parser* parser,
28901 enum rid keyword,
28902 required_token token_desc)
28904 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28906 if (token && token->keyword != keyword)
28908 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28909 UNKNOWN_LOCATION);
28910 return NULL;
28913 return token;
28916 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28917 function-definition. */
28919 static bool
28920 cp_parser_token_starts_function_definition_p (cp_token* token)
28922 return (/* An ordinary function-body begins with an `{'. */
28923 token->type == CPP_OPEN_BRACE
28924 /* A ctor-initializer begins with a `:'. */
28925 || token->type == CPP_COLON
28926 /* A function-try-block begins with `try'. */
28927 || token->keyword == RID_TRY
28928 /* A function-transaction-block begins with `__transaction_atomic'
28929 or `__transaction_relaxed'. */
28930 || token->keyword == RID_TRANSACTION_ATOMIC
28931 || token->keyword == RID_TRANSACTION_RELAXED
28932 /* The named return value extension begins with `return'. */
28933 || token->keyword == RID_RETURN);
28936 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28937 definition. */
28939 static bool
28940 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28942 cp_token *token;
28944 token = cp_lexer_peek_token (parser->lexer);
28945 return (token->type == CPP_OPEN_BRACE
28946 || (token->type == CPP_COLON
28947 && !parser->colon_doesnt_start_class_def_p));
28950 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28951 C++0x) ending a template-argument. */
28953 static bool
28954 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28956 cp_token *token;
28958 token = cp_lexer_peek_token (parser->lexer);
28959 return (token->type == CPP_COMMA
28960 || token->type == CPP_GREATER
28961 || token->type == CPP_ELLIPSIS
28962 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28965 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28966 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28968 static bool
28969 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28970 size_t n)
28972 cp_token *token;
28974 token = cp_lexer_peek_nth_token (parser->lexer, n);
28975 if (token->type == CPP_LESS)
28976 return true;
28977 /* Check for the sequence `<::' in the original code. It would be lexed as
28978 `[:', where `[' is a digraph, and there is no whitespace before
28979 `:'. */
28980 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28982 cp_token *token2;
28983 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28984 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28985 return true;
28987 return false;
28990 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28991 or none_type otherwise. */
28993 static enum tag_types
28994 cp_parser_token_is_class_key (cp_token* token)
28996 switch (token->keyword)
28998 case RID_CLASS:
28999 return class_type;
29000 case RID_STRUCT:
29001 return record_type;
29002 case RID_UNION:
29003 return union_type;
29005 default:
29006 return none_type;
29010 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29011 or none_type otherwise or if the token is null. */
29013 static enum tag_types
29014 cp_parser_token_is_type_parameter_key (cp_token* token)
29016 if (!token)
29017 return none_type;
29019 switch (token->keyword)
29021 case RID_CLASS:
29022 return class_type;
29023 case RID_TYPENAME:
29024 return typename_type;
29026 default:
29027 return none_type;
29031 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29033 static void
29034 cp_parser_check_class_key (enum tag_types class_key, tree type)
29036 if (type == error_mark_node)
29037 return;
29038 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29040 if (permerror (input_location, "%qs tag used in naming %q#T",
29041 class_key == union_type ? "union"
29042 : class_key == record_type ? "struct" : "class",
29043 type))
29044 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29045 "%q#T was previously declared here", type);
29049 /* Issue an error message if DECL is redeclared with different
29050 access than its original declaration [class.access.spec/3].
29051 This applies to nested classes, nested class templates and
29052 enumerations [class.mem/1]. */
29054 static void
29055 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29057 if (!decl
29058 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29059 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29060 return;
29062 if ((TREE_PRIVATE (decl)
29063 != (current_access_specifier == access_private_node))
29064 || (TREE_PROTECTED (decl)
29065 != (current_access_specifier == access_protected_node)))
29066 error_at (location, "%qD redeclared with different access", decl);
29069 /* Look for the `template' keyword, as a syntactic disambiguator.
29070 Return TRUE iff it is present, in which case it will be
29071 consumed. */
29073 static bool
29074 cp_parser_optional_template_keyword (cp_parser *parser)
29076 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29078 /* In C++98 the `template' keyword can only be used within templates;
29079 outside templates the parser can always figure out what is a
29080 template and what is not. In C++11, per the resolution of DR 468,
29081 `template' is allowed in cases where it is not strictly necessary. */
29082 if (!processing_template_decl
29083 && pedantic && cxx_dialect == cxx98)
29085 cp_token *token = cp_lexer_peek_token (parser->lexer);
29086 pedwarn (token->location, OPT_Wpedantic,
29087 "in C++98 %<template%> (as a disambiguator) is only "
29088 "allowed within templates");
29089 /* If this part of the token stream is rescanned, the same
29090 error message would be generated. So, we purge the token
29091 from the stream. */
29092 cp_lexer_purge_token (parser->lexer);
29093 return false;
29095 else
29097 /* Consume the `template' keyword. */
29098 cp_lexer_consume_token (parser->lexer);
29099 return true;
29102 return false;
29105 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29106 set PARSER->SCOPE, and perform other related actions. */
29108 static void
29109 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29111 struct tree_check *check_value;
29113 /* Get the stored value. */
29114 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29115 /* Set the scope from the stored value. */
29116 parser->scope = saved_checks_value (check_value);
29117 parser->qualifying_scope = check_value->qualifying_scope;
29118 parser->object_scope = NULL_TREE;
29121 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29122 encounter the end of a block before what we were looking for. */
29124 static bool
29125 cp_parser_cache_group (cp_parser *parser,
29126 enum cpp_ttype end,
29127 unsigned depth)
29129 while (true)
29131 cp_token *token = cp_lexer_peek_token (parser->lexer);
29133 /* Abort a parenthesized expression if we encounter a semicolon. */
29134 if ((end == CPP_CLOSE_PAREN || depth == 0)
29135 && token->type == CPP_SEMICOLON)
29136 return true;
29137 /* If we've reached the end of the file, stop. */
29138 if (token->type == CPP_EOF
29139 || (end != CPP_PRAGMA_EOL
29140 && token->type == CPP_PRAGMA_EOL))
29141 return true;
29142 if (token->type == CPP_CLOSE_BRACE && depth == 0)
29143 /* We've hit the end of an enclosing block, so there's been some
29144 kind of syntax error. */
29145 return true;
29147 /* Consume the token. */
29148 cp_lexer_consume_token (parser->lexer);
29149 /* See if it starts a new group. */
29150 if (token->type == CPP_OPEN_BRACE)
29152 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29153 /* In theory this should probably check end == '}', but
29154 cp_parser_save_member_function_body needs it to exit
29155 after either '}' or ')' when called with ')'. */
29156 if (depth == 0)
29157 return false;
29159 else if (token->type == CPP_OPEN_PAREN)
29161 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29162 if (depth == 0 && end == CPP_CLOSE_PAREN)
29163 return false;
29165 else if (token->type == CPP_PRAGMA)
29166 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29167 else if (token->type == end)
29168 return false;
29172 /* Like above, for caching a default argument or NSDMI. Both of these are
29173 terminated by a non-nested comma, but it can be unclear whether or not a
29174 comma is nested in a template argument list unless we do more parsing.
29175 In order to handle this ambiguity, when we encounter a ',' after a '<'
29176 we try to parse what follows as a parameter-declaration-list (in the
29177 case of a default argument) or a member-declarator (in the case of an
29178 NSDMI). If that succeeds, then we stop caching. */
29180 static tree
29181 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29183 unsigned depth = 0;
29184 int maybe_template_id = 0;
29185 cp_token *first_token;
29186 cp_token *token;
29187 tree default_argument;
29189 /* Add tokens until we have processed the entire default
29190 argument. We add the range [first_token, token). */
29191 first_token = cp_lexer_peek_token (parser->lexer);
29192 if (first_token->type == CPP_OPEN_BRACE)
29194 /* For list-initialization, this is straightforward. */
29195 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29196 token = cp_lexer_peek_token (parser->lexer);
29198 else while (true)
29200 bool done = false;
29202 /* Peek at the next token. */
29203 token = cp_lexer_peek_token (parser->lexer);
29204 /* What we do depends on what token we have. */
29205 switch (token->type)
29207 /* In valid code, a default argument must be
29208 immediately followed by a `,' `)', or `...'. */
29209 case CPP_COMMA:
29210 if (depth == 0 && maybe_template_id)
29212 /* If we've seen a '<', we might be in a
29213 template-argument-list. Until Core issue 325 is
29214 resolved, we don't know how this situation ought
29215 to be handled, so try to DTRT. We check whether
29216 what comes after the comma is a valid parameter
29217 declaration list. If it is, then the comma ends
29218 the default argument; otherwise the default
29219 argument continues. */
29220 bool error = false;
29221 cp_token *peek;
29223 /* Set ITALP so cp_parser_parameter_declaration_list
29224 doesn't decide to commit to this parse. */
29225 bool saved_italp = parser->in_template_argument_list_p;
29226 parser->in_template_argument_list_p = true;
29228 cp_parser_parse_tentatively (parser);
29230 if (nsdmi)
29232 /* Parse declarators until we reach a non-comma or
29233 somthing that cannot be an initializer.
29234 Just checking whether we're looking at a single
29235 declarator is insufficient. Consider:
29236 int var = tuple<T,U>::x;
29237 The template parameter 'U' looks exactly like a
29238 declarator. */
29241 int ctor_dtor_or_conv_p;
29242 cp_lexer_consume_token (parser->lexer);
29243 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29244 &ctor_dtor_or_conv_p,
29245 /*parenthesized_p=*/NULL,
29246 /*member_p=*/true,
29247 /*friend_p=*/false);
29248 peek = cp_lexer_peek_token (parser->lexer);
29249 if (cp_parser_error_occurred (parser))
29250 break;
29252 while (peek->type == CPP_COMMA);
29253 /* If we met an '=' or ';' then the original comma
29254 was the end of the NSDMI. Otherwise assume
29255 we're still in the NSDMI. */
29256 error = (peek->type != CPP_EQ
29257 && peek->type != CPP_SEMICOLON);
29259 else
29261 cp_lexer_consume_token (parser->lexer);
29262 begin_scope (sk_function_parms, NULL_TREE);
29263 if (cp_parser_parameter_declaration_list (parser)
29264 == error_mark_node)
29265 error = true;
29266 pop_bindings_and_leave_scope ();
29268 if (!cp_parser_error_occurred (parser) && !error)
29269 done = true;
29270 cp_parser_abort_tentative_parse (parser);
29272 parser->in_template_argument_list_p = saved_italp;
29273 break;
29275 /* FALLTHRU */
29276 case CPP_CLOSE_PAREN:
29277 case CPP_ELLIPSIS:
29278 /* If we run into a non-nested `;', `}', or `]',
29279 then the code is invalid -- but the default
29280 argument is certainly over. */
29281 case CPP_SEMICOLON:
29282 case CPP_CLOSE_BRACE:
29283 case CPP_CLOSE_SQUARE:
29284 if (depth == 0
29285 /* Handle correctly int n = sizeof ... ( p ); */
29286 && token->type != CPP_ELLIPSIS)
29287 done = true;
29288 /* Update DEPTH, if necessary. */
29289 else if (token->type == CPP_CLOSE_PAREN
29290 || token->type == CPP_CLOSE_BRACE
29291 || token->type == CPP_CLOSE_SQUARE)
29292 --depth;
29293 break;
29295 case CPP_OPEN_PAREN:
29296 case CPP_OPEN_SQUARE:
29297 case CPP_OPEN_BRACE:
29298 ++depth;
29299 break;
29301 case CPP_LESS:
29302 if (depth == 0)
29303 /* This might be the comparison operator, or it might
29304 start a template argument list. */
29305 ++maybe_template_id;
29306 break;
29308 case CPP_RSHIFT:
29309 if (cxx_dialect == cxx98)
29310 break;
29311 /* Fall through for C++0x, which treats the `>>'
29312 operator like two `>' tokens in certain
29313 cases. */
29314 gcc_fallthrough ();
29316 case CPP_GREATER:
29317 if (depth == 0)
29319 /* This might be an operator, or it might close a
29320 template argument list. But if a previous '<'
29321 started a template argument list, this will have
29322 closed it, so we can't be in one anymore. */
29323 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29324 if (maybe_template_id < 0)
29325 maybe_template_id = 0;
29327 break;
29329 /* If we run out of tokens, issue an error message. */
29330 case CPP_EOF:
29331 case CPP_PRAGMA_EOL:
29332 error_at (token->location, "file ends in default argument");
29333 return error_mark_node;
29335 case CPP_NAME:
29336 case CPP_SCOPE:
29337 /* In these cases, we should look for template-ids.
29338 For example, if the default argument is
29339 `X<int, double>()', we need to do name lookup to
29340 figure out whether or not `X' is a template; if
29341 so, the `,' does not end the default argument.
29343 That is not yet done. */
29344 break;
29346 default:
29347 break;
29350 /* If we've reached the end, stop. */
29351 if (done)
29352 break;
29354 /* Add the token to the token block. */
29355 token = cp_lexer_consume_token (parser->lexer);
29358 /* Create a DEFAULT_ARG to represent the unparsed default
29359 argument. */
29360 default_argument = make_node (DEFAULT_ARG);
29361 DEFARG_TOKENS (default_argument)
29362 = cp_token_cache_new (first_token, token);
29363 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29365 return default_argument;
29368 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29370 location_t
29371 defarg_location (tree default_argument)
29373 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29374 location_t start = tokens->first->location;
29375 location_t end = tokens->last->location;
29376 return make_location (start, start, end);
29379 /* Begin parsing tentatively. We always save tokens while parsing
29380 tentatively so that if the tentative parsing fails we can restore the
29381 tokens. */
29383 static void
29384 cp_parser_parse_tentatively (cp_parser* parser)
29386 /* Enter a new parsing context. */
29387 parser->context = cp_parser_context_new (parser->context);
29388 /* Begin saving tokens. */
29389 cp_lexer_save_tokens (parser->lexer);
29390 /* In order to avoid repetitive access control error messages,
29391 access checks are queued up until we are no longer parsing
29392 tentatively. */
29393 push_deferring_access_checks (dk_deferred);
29396 /* Commit to the currently active tentative parse. */
29398 static void
29399 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29401 cp_parser_context *context;
29402 cp_lexer *lexer;
29404 /* Mark all of the levels as committed. */
29405 lexer = parser->lexer;
29406 for (context = parser->context; context->next; context = context->next)
29408 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29409 break;
29410 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29411 while (!cp_lexer_saving_tokens (lexer))
29412 lexer = lexer->next;
29413 cp_lexer_commit_tokens (lexer);
29417 /* Commit to the topmost currently active tentative parse.
29419 Note that this function shouldn't be called when there are
29420 irreversible side-effects while in a tentative state. For
29421 example, we shouldn't create a permanent entry in the symbol
29422 table, or issue an error message that might not apply if the
29423 tentative parse is aborted. */
29425 static void
29426 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29428 cp_parser_context *context = parser->context;
29429 cp_lexer *lexer = parser->lexer;
29431 if (context)
29433 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29434 return;
29435 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29437 while (!cp_lexer_saving_tokens (lexer))
29438 lexer = lexer->next;
29439 cp_lexer_commit_tokens (lexer);
29443 /* Abort the currently active tentative parse. All consumed tokens
29444 will be rolled back, and no diagnostics will be issued. */
29446 static void
29447 cp_parser_abort_tentative_parse (cp_parser* parser)
29449 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29450 || errorcount > 0);
29451 cp_parser_simulate_error (parser);
29452 /* Now, pretend that we want to see if the construct was
29453 successfully parsed. */
29454 cp_parser_parse_definitely (parser);
29457 /* Stop parsing tentatively. If a parse error has occurred, restore the
29458 token stream. Otherwise, commit to the tokens we have consumed.
29459 Returns true if no error occurred; false otherwise. */
29461 static bool
29462 cp_parser_parse_definitely (cp_parser* parser)
29464 bool error_occurred;
29465 cp_parser_context *context;
29467 /* Remember whether or not an error occurred, since we are about to
29468 destroy that information. */
29469 error_occurred = cp_parser_error_occurred (parser);
29470 /* Remove the topmost context from the stack. */
29471 context = parser->context;
29472 parser->context = context->next;
29473 /* If no parse errors occurred, commit to the tentative parse. */
29474 if (!error_occurred)
29476 /* Commit to the tokens read tentatively, unless that was
29477 already done. */
29478 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29479 cp_lexer_commit_tokens (parser->lexer);
29481 pop_to_parent_deferring_access_checks ();
29483 /* Otherwise, if errors occurred, roll back our state so that things
29484 are just as they were before we began the tentative parse. */
29485 else
29487 cp_lexer_rollback_tokens (parser->lexer);
29488 pop_deferring_access_checks ();
29490 /* Add the context to the front of the free list. */
29491 context->next = cp_parser_context_free_list;
29492 cp_parser_context_free_list = context;
29494 return !error_occurred;
29497 /* Returns true if we are parsing tentatively and are not committed to
29498 this tentative parse. */
29500 static bool
29501 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29503 return (cp_parser_parsing_tentatively (parser)
29504 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29507 /* Returns nonzero iff an error has occurred during the most recent
29508 tentative parse. */
29510 static bool
29511 cp_parser_error_occurred (cp_parser* parser)
29513 return (cp_parser_parsing_tentatively (parser)
29514 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29517 /* Returns nonzero if GNU extensions are allowed. */
29519 static bool
29520 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29522 return parser->allow_gnu_extensions_p;
29525 /* Objective-C++ Productions */
29528 /* Parse an Objective-C expression, which feeds into a primary-expression
29529 above.
29531 objc-expression:
29532 objc-message-expression
29533 objc-string-literal
29534 objc-encode-expression
29535 objc-protocol-expression
29536 objc-selector-expression
29538 Returns a tree representation of the expression. */
29540 static cp_expr
29541 cp_parser_objc_expression (cp_parser* parser)
29543 /* Try to figure out what kind of declaration is present. */
29544 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29546 switch (kwd->type)
29548 case CPP_OPEN_SQUARE:
29549 return cp_parser_objc_message_expression (parser);
29551 case CPP_OBJC_STRING:
29552 kwd = cp_lexer_consume_token (parser->lexer);
29553 return objc_build_string_object (kwd->u.value);
29555 case CPP_KEYWORD:
29556 switch (kwd->keyword)
29558 case RID_AT_ENCODE:
29559 return cp_parser_objc_encode_expression (parser);
29561 case RID_AT_PROTOCOL:
29562 return cp_parser_objc_protocol_expression (parser);
29564 case RID_AT_SELECTOR:
29565 return cp_parser_objc_selector_expression (parser);
29567 default:
29568 break;
29570 /* FALLTHRU */
29571 default:
29572 error_at (kwd->location,
29573 "misplaced %<@%D%> Objective-C++ construct",
29574 kwd->u.value);
29575 cp_parser_skip_to_end_of_block_or_statement (parser);
29578 return error_mark_node;
29581 /* Parse an Objective-C message expression.
29583 objc-message-expression:
29584 [ objc-message-receiver objc-message-args ]
29586 Returns a representation of an Objective-C message. */
29588 static tree
29589 cp_parser_objc_message_expression (cp_parser* parser)
29591 tree receiver, messageargs;
29593 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29594 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29595 receiver = cp_parser_objc_message_receiver (parser);
29596 messageargs = cp_parser_objc_message_args (parser);
29597 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29598 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29600 tree result = objc_build_message_expr (receiver, messageargs);
29602 /* Construct a location e.g.
29603 [self func1:5]
29604 ^~~~~~~~~~~~~~
29605 ranging from the '[' to the ']', with the caret at the start. */
29606 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29607 protected_set_expr_location (result, combined_loc);
29609 return result;
29612 /* Parse an objc-message-receiver.
29614 objc-message-receiver:
29615 expression
29616 simple-type-specifier
29618 Returns a representation of the type or expression. */
29620 static tree
29621 cp_parser_objc_message_receiver (cp_parser* parser)
29623 tree rcv;
29625 /* An Objective-C message receiver may be either (1) a type
29626 or (2) an expression. */
29627 cp_parser_parse_tentatively (parser);
29628 rcv = cp_parser_expression (parser);
29630 /* If that worked out, fine. */
29631 if (cp_parser_parse_definitely (parser))
29632 return rcv;
29634 cp_parser_parse_tentatively (parser);
29635 rcv = cp_parser_simple_type_specifier (parser,
29636 /*decl_specs=*/NULL,
29637 CP_PARSER_FLAGS_NONE);
29639 if (cp_parser_parse_definitely (parser))
29640 return objc_get_class_reference (rcv);
29642 cp_parser_error (parser, "objective-c++ message receiver expected");
29643 return error_mark_node;
29646 /* Parse the arguments and selectors comprising an Objective-C message.
29648 objc-message-args:
29649 objc-selector
29650 objc-selector-args
29651 objc-selector-args , objc-comma-args
29653 objc-selector-args:
29654 objc-selector [opt] : assignment-expression
29655 objc-selector-args objc-selector [opt] : assignment-expression
29657 objc-comma-args:
29658 assignment-expression
29659 objc-comma-args , assignment-expression
29661 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29662 selector arguments and TREE_VALUE containing a list of comma
29663 arguments. */
29665 static tree
29666 cp_parser_objc_message_args (cp_parser* parser)
29668 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29669 bool maybe_unary_selector_p = true;
29670 cp_token *token = cp_lexer_peek_token (parser->lexer);
29672 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29674 tree selector = NULL_TREE, arg;
29676 if (token->type != CPP_COLON)
29677 selector = cp_parser_objc_selector (parser);
29679 /* Detect if we have a unary selector. */
29680 if (maybe_unary_selector_p
29681 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29682 return build_tree_list (selector, NULL_TREE);
29684 maybe_unary_selector_p = false;
29685 cp_parser_require (parser, CPP_COLON, RT_COLON);
29686 arg = cp_parser_assignment_expression (parser);
29688 sel_args
29689 = chainon (sel_args,
29690 build_tree_list (selector, arg));
29692 token = cp_lexer_peek_token (parser->lexer);
29695 /* Handle non-selector arguments, if any. */
29696 while (token->type == CPP_COMMA)
29698 tree arg;
29700 cp_lexer_consume_token (parser->lexer);
29701 arg = cp_parser_assignment_expression (parser);
29703 addl_args
29704 = chainon (addl_args,
29705 build_tree_list (NULL_TREE, arg));
29707 token = cp_lexer_peek_token (parser->lexer);
29710 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29712 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29713 return build_tree_list (error_mark_node, error_mark_node);
29716 return build_tree_list (sel_args, addl_args);
29719 /* Parse an Objective-C encode expression.
29721 objc-encode-expression:
29722 @encode objc-typename
29724 Returns an encoded representation of the type argument. */
29726 static cp_expr
29727 cp_parser_objc_encode_expression (cp_parser* parser)
29729 tree type;
29730 cp_token *token;
29731 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29733 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29734 matching_parens parens;
29735 parens.require_open (parser);
29736 token = cp_lexer_peek_token (parser->lexer);
29737 type = complete_type (cp_parser_type_id (parser));
29738 parens.require_close (parser);
29740 if (!type)
29742 error_at (token->location,
29743 "%<@encode%> must specify a type as an argument");
29744 return error_mark_node;
29747 /* This happens if we find @encode(T) (where T is a template
29748 typename or something dependent on a template typename) when
29749 parsing a template. In that case, we can't compile it
29750 immediately, but we rather create an AT_ENCODE_EXPR which will
29751 need to be instantiated when the template is used.
29753 if (dependent_type_p (type))
29755 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29756 TREE_READONLY (value) = 1;
29757 return value;
29761 /* Build a location of the form:
29762 @encode(int)
29763 ^~~~~~~~~~~~
29764 with caret==start at the @ token, finishing at the close paren. */
29765 location_t combined_loc
29766 = make_location (start_loc, start_loc,
29767 cp_lexer_previous_token (parser->lexer)->location);
29769 return cp_expr (objc_build_encode_expr (type), combined_loc);
29772 /* Parse an Objective-C @defs expression. */
29774 static tree
29775 cp_parser_objc_defs_expression (cp_parser *parser)
29777 tree name;
29779 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29780 matching_parens parens;
29781 parens.require_open (parser);
29782 name = cp_parser_identifier (parser);
29783 parens.require_close (parser);
29785 return objc_get_class_ivars (name);
29788 /* Parse an Objective-C protocol expression.
29790 objc-protocol-expression:
29791 @protocol ( identifier )
29793 Returns a representation of the protocol expression. */
29795 static tree
29796 cp_parser_objc_protocol_expression (cp_parser* parser)
29798 tree proto;
29799 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29801 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29802 matching_parens parens;
29803 parens.require_open (parser);
29804 proto = cp_parser_identifier (parser);
29805 parens.require_close (parser);
29807 /* Build a location of the form:
29808 @protocol(prot)
29809 ^~~~~~~~~~~~~~~
29810 with caret==start at the @ token, finishing at the close paren. */
29811 location_t combined_loc
29812 = make_location (start_loc, start_loc,
29813 cp_lexer_previous_token (parser->lexer)->location);
29814 tree result = objc_build_protocol_expr (proto);
29815 protected_set_expr_location (result, combined_loc);
29816 return result;
29819 /* Parse an Objective-C selector expression.
29821 objc-selector-expression:
29822 @selector ( objc-method-signature )
29824 objc-method-signature:
29825 objc-selector
29826 objc-selector-seq
29828 objc-selector-seq:
29829 objc-selector :
29830 objc-selector-seq objc-selector :
29832 Returns a representation of the method selector. */
29834 static tree
29835 cp_parser_objc_selector_expression (cp_parser* parser)
29837 tree sel_seq = NULL_TREE;
29838 bool maybe_unary_selector_p = true;
29839 cp_token *token;
29840 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29842 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29843 matching_parens parens;
29844 parens.require_open (parser);
29845 token = cp_lexer_peek_token (parser->lexer);
29847 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29848 || token->type == CPP_SCOPE)
29850 tree selector = NULL_TREE;
29852 if (token->type != CPP_COLON
29853 || token->type == CPP_SCOPE)
29854 selector = cp_parser_objc_selector (parser);
29856 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29857 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29859 /* Detect if we have a unary selector. */
29860 if (maybe_unary_selector_p)
29862 sel_seq = selector;
29863 goto finish_selector;
29865 else
29867 cp_parser_error (parser, "expected %<:%>");
29870 maybe_unary_selector_p = false;
29871 token = cp_lexer_consume_token (parser->lexer);
29873 if (token->type == CPP_SCOPE)
29875 sel_seq
29876 = chainon (sel_seq,
29877 build_tree_list (selector, NULL_TREE));
29878 sel_seq
29879 = chainon (sel_seq,
29880 build_tree_list (NULL_TREE, NULL_TREE));
29882 else
29883 sel_seq
29884 = chainon (sel_seq,
29885 build_tree_list (selector, NULL_TREE));
29887 token = cp_lexer_peek_token (parser->lexer);
29890 finish_selector:
29891 parens.require_close (parser);
29894 /* Build a location of the form:
29895 @selector(func)
29896 ^~~~~~~~~~~~~~~
29897 with caret==start at the @ token, finishing at the close paren. */
29898 location_t combined_loc
29899 = make_location (loc, loc,
29900 cp_lexer_previous_token (parser->lexer)->location);
29901 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29902 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29903 protected_set_expr_location (result, combined_loc);
29904 return result;
29907 /* Parse a list of identifiers.
29909 objc-identifier-list:
29910 identifier
29911 objc-identifier-list , identifier
29913 Returns a TREE_LIST of identifier nodes. */
29915 static tree
29916 cp_parser_objc_identifier_list (cp_parser* parser)
29918 tree identifier;
29919 tree list;
29920 cp_token *sep;
29922 identifier = cp_parser_identifier (parser);
29923 if (identifier == error_mark_node)
29924 return error_mark_node;
29926 list = build_tree_list (NULL_TREE, identifier);
29927 sep = cp_lexer_peek_token (parser->lexer);
29929 while (sep->type == CPP_COMMA)
29931 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29932 identifier = cp_parser_identifier (parser);
29933 if (identifier == error_mark_node)
29934 return list;
29936 list = chainon (list, build_tree_list (NULL_TREE,
29937 identifier));
29938 sep = cp_lexer_peek_token (parser->lexer);
29941 return list;
29944 /* Parse an Objective-C alias declaration.
29946 objc-alias-declaration:
29947 @compatibility_alias identifier identifier ;
29949 This function registers the alias mapping with the Objective-C front end.
29950 It returns nothing. */
29952 static void
29953 cp_parser_objc_alias_declaration (cp_parser* parser)
29955 tree alias, orig;
29957 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29958 alias = cp_parser_identifier (parser);
29959 orig = cp_parser_identifier (parser);
29960 objc_declare_alias (alias, orig);
29961 cp_parser_consume_semicolon_at_end_of_statement (parser);
29964 /* Parse an Objective-C class forward-declaration.
29966 objc-class-declaration:
29967 @class objc-identifier-list ;
29969 The function registers the forward declarations with the Objective-C
29970 front end. It returns nothing. */
29972 static void
29973 cp_parser_objc_class_declaration (cp_parser* parser)
29975 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29976 while (true)
29978 tree id;
29980 id = cp_parser_identifier (parser);
29981 if (id == error_mark_node)
29982 break;
29984 objc_declare_class (id);
29986 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29987 cp_lexer_consume_token (parser->lexer);
29988 else
29989 break;
29991 cp_parser_consume_semicolon_at_end_of_statement (parser);
29994 /* Parse a list of Objective-C protocol references.
29996 objc-protocol-refs-opt:
29997 objc-protocol-refs [opt]
29999 objc-protocol-refs:
30000 < objc-identifier-list >
30002 Returns a TREE_LIST of identifiers, if any. */
30004 static tree
30005 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30007 tree protorefs = NULL_TREE;
30009 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30011 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30012 protorefs = cp_parser_objc_identifier_list (parser);
30013 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30016 return protorefs;
30019 /* Parse a Objective-C visibility specification. */
30021 static void
30022 cp_parser_objc_visibility_spec (cp_parser* parser)
30024 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30026 switch (vis->keyword)
30028 case RID_AT_PRIVATE:
30029 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30030 break;
30031 case RID_AT_PROTECTED:
30032 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30033 break;
30034 case RID_AT_PUBLIC:
30035 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30036 break;
30037 case RID_AT_PACKAGE:
30038 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30039 break;
30040 default:
30041 return;
30044 /* Eat '@private'/'@protected'/'@public'. */
30045 cp_lexer_consume_token (parser->lexer);
30048 /* Parse an Objective-C method type. Return 'true' if it is a class
30049 (+) method, and 'false' if it is an instance (-) method. */
30051 static inline bool
30052 cp_parser_objc_method_type (cp_parser* parser)
30054 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30055 return true;
30056 else
30057 return false;
30060 /* Parse an Objective-C protocol qualifier. */
30062 static tree
30063 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30065 tree quals = NULL_TREE, node;
30066 cp_token *token = cp_lexer_peek_token (parser->lexer);
30068 node = token->u.value;
30070 while (node && identifier_p (node)
30071 && (node == ridpointers [(int) RID_IN]
30072 || node == ridpointers [(int) RID_OUT]
30073 || node == ridpointers [(int) RID_INOUT]
30074 || node == ridpointers [(int) RID_BYCOPY]
30075 || node == ridpointers [(int) RID_BYREF]
30076 || node == ridpointers [(int) RID_ONEWAY]))
30078 quals = tree_cons (NULL_TREE, node, quals);
30079 cp_lexer_consume_token (parser->lexer);
30080 token = cp_lexer_peek_token (parser->lexer);
30081 node = token->u.value;
30084 return quals;
30087 /* Parse an Objective-C typename. */
30089 static tree
30090 cp_parser_objc_typename (cp_parser* parser)
30092 tree type_name = NULL_TREE;
30094 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30096 tree proto_quals, cp_type = NULL_TREE;
30098 matching_parens parens;
30099 parens.consume_open (parser); /* Eat '('. */
30100 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30102 /* An ObjC type name may consist of just protocol qualifiers, in which
30103 case the type shall default to 'id'. */
30104 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30106 cp_type = cp_parser_type_id (parser);
30108 /* If the type could not be parsed, an error has already
30109 been produced. For error recovery, behave as if it had
30110 not been specified, which will use the default type
30111 'id'. */
30112 if (cp_type == error_mark_node)
30114 cp_type = NULL_TREE;
30115 /* We need to skip to the closing parenthesis as
30116 cp_parser_type_id() does not seem to do it for
30117 us. */
30118 cp_parser_skip_to_closing_parenthesis (parser,
30119 /*recovering=*/true,
30120 /*or_comma=*/false,
30121 /*consume_paren=*/false);
30125 parens.require_close (parser);
30126 type_name = build_tree_list (proto_quals, cp_type);
30129 return type_name;
30132 /* Check to see if TYPE refers to an Objective-C selector name. */
30134 static bool
30135 cp_parser_objc_selector_p (enum cpp_ttype type)
30137 return (type == CPP_NAME || type == CPP_KEYWORD
30138 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30139 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30140 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30141 || type == CPP_XOR || type == CPP_XOR_EQ);
30144 /* Parse an Objective-C selector. */
30146 static tree
30147 cp_parser_objc_selector (cp_parser* parser)
30149 cp_token *token = cp_lexer_consume_token (parser->lexer);
30151 if (!cp_parser_objc_selector_p (token->type))
30153 error_at (token->location, "invalid Objective-C++ selector name");
30154 return error_mark_node;
30157 /* C++ operator names are allowed to appear in ObjC selectors. */
30158 switch (token->type)
30160 case CPP_AND_AND: return get_identifier ("and");
30161 case CPP_AND_EQ: return get_identifier ("and_eq");
30162 case CPP_AND: return get_identifier ("bitand");
30163 case CPP_OR: return get_identifier ("bitor");
30164 case CPP_COMPL: return get_identifier ("compl");
30165 case CPP_NOT: return get_identifier ("not");
30166 case CPP_NOT_EQ: return get_identifier ("not_eq");
30167 case CPP_OR_OR: return get_identifier ("or");
30168 case CPP_OR_EQ: return get_identifier ("or_eq");
30169 case CPP_XOR: return get_identifier ("xor");
30170 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30171 default: return token->u.value;
30175 /* Parse an Objective-C params list. */
30177 static tree
30178 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30180 tree params = NULL_TREE;
30181 bool maybe_unary_selector_p = true;
30182 cp_token *token = cp_lexer_peek_token (parser->lexer);
30184 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30186 tree selector = NULL_TREE, type_name, identifier;
30187 tree parm_attr = NULL_TREE;
30189 if (token->keyword == RID_ATTRIBUTE)
30190 break;
30192 if (token->type != CPP_COLON)
30193 selector = cp_parser_objc_selector (parser);
30195 /* Detect if we have a unary selector. */
30196 if (maybe_unary_selector_p
30197 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30199 params = selector; /* Might be followed by attributes. */
30200 break;
30203 maybe_unary_selector_p = false;
30204 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30206 /* Something went quite wrong. There should be a colon
30207 here, but there is not. Stop parsing parameters. */
30208 break;
30210 type_name = cp_parser_objc_typename (parser);
30211 /* New ObjC allows attributes on parameters too. */
30212 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30213 parm_attr = cp_parser_attributes_opt (parser);
30214 identifier = cp_parser_identifier (parser);
30216 params
30217 = chainon (params,
30218 objc_build_keyword_decl (selector,
30219 type_name,
30220 identifier,
30221 parm_attr));
30223 token = cp_lexer_peek_token (parser->lexer);
30226 if (params == NULL_TREE)
30228 cp_parser_error (parser, "objective-c++ method declaration is expected");
30229 return error_mark_node;
30232 /* We allow tail attributes for the method. */
30233 if (token->keyword == RID_ATTRIBUTE)
30235 *attributes = cp_parser_attributes_opt (parser);
30236 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30237 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30238 return params;
30239 cp_parser_error (parser,
30240 "method attributes must be specified at the end");
30241 return error_mark_node;
30244 if (params == NULL_TREE)
30246 cp_parser_error (parser, "objective-c++ method declaration is expected");
30247 return error_mark_node;
30249 return params;
30252 /* Parse the non-keyword Objective-C params. */
30254 static tree
30255 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30256 tree* attributes)
30258 tree params = make_node (TREE_LIST);
30259 cp_token *token = cp_lexer_peek_token (parser->lexer);
30260 *ellipsisp = false; /* Initially, assume no ellipsis. */
30262 while (token->type == CPP_COMMA)
30264 cp_parameter_declarator *parmdecl;
30265 tree parm;
30267 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30268 token = cp_lexer_peek_token (parser->lexer);
30270 if (token->type == CPP_ELLIPSIS)
30272 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30273 *ellipsisp = true;
30274 token = cp_lexer_peek_token (parser->lexer);
30275 break;
30278 /* TODO: parse attributes for tail parameters. */
30279 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30280 parm = grokdeclarator (parmdecl->declarator,
30281 &parmdecl->decl_specifiers,
30282 PARM, /*initialized=*/0,
30283 /*attrlist=*/NULL);
30285 chainon (params, build_tree_list (NULL_TREE, parm));
30286 token = cp_lexer_peek_token (parser->lexer);
30289 /* We allow tail attributes for the method. */
30290 if (token->keyword == RID_ATTRIBUTE)
30292 if (*attributes == NULL_TREE)
30294 *attributes = cp_parser_attributes_opt (parser);
30295 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30296 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30297 return params;
30299 else
30300 /* We have an error, but parse the attributes, so that we can
30301 carry on. */
30302 *attributes = cp_parser_attributes_opt (parser);
30304 cp_parser_error (parser,
30305 "method attributes must be specified at the end");
30306 return error_mark_node;
30309 return params;
30312 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30314 static void
30315 cp_parser_objc_interstitial_code (cp_parser* parser)
30317 cp_token *token = cp_lexer_peek_token (parser->lexer);
30319 /* If the next token is `extern' and the following token is a string
30320 literal, then we have a linkage specification. */
30321 if (token->keyword == RID_EXTERN
30322 && cp_parser_is_pure_string_literal
30323 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30324 cp_parser_linkage_specification (parser);
30325 /* Handle #pragma, if any. */
30326 else if (token->type == CPP_PRAGMA)
30327 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30328 /* Allow stray semicolons. */
30329 else if (token->type == CPP_SEMICOLON)
30330 cp_lexer_consume_token (parser->lexer);
30331 /* Mark methods as optional or required, when building protocols. */
30332 else if (token->keyword == RID_AT_OPTIONAL)
30334 cp_lexer_consume_token (parser->lexer);
30335 objc_set_method_opt (true);
30337 else if (token->keyword == RID_AT_REQUIRED)
30339 cp_lexer_consume_token (parser->lexer);
30340 objc_set_method_opt (false);
30342 else if (token->keyword == RID_NAMESPACE)
30343 cp_parser_namespace_definition (parser);
30344 /* Other stray characters must generate errors. */
30345 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30347 cp_lexer_consume_token (parser->lexer);
30348 error ("stray %qs between Objective-C++ methods",
30349 token->type == CPP_OPEN_BRACE ? "{" : "}");
30351 /* Finally, try to parse a block-declaration, or a function-definition. */
30352 else
30353 cp_parser_block_declaration (parser, /*statement_p=*/false);
30356 /* Parse a method signature. */
30358 static tree
30359 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30361 tree rettype, kwdparms, optparms;
30362 bool ellipsis = false;
30363 bool is_class_method;
30365 is_class_method = cp_parser_objc_method_type (parser);
30366 rettype = cp_parser_objc_typename (parser);
30367 *attributes = NULL_TREE;
30368 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30369 if (kwdparms == error_mark_node)
30370 return error_mark_node;
30371 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30372 if (optparms == error_mark_node)
30373 return error_mark_node;
30375 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30378 static bool
30379 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30381 tree tattr;
30382 cp_lexer_save_tokens (parser->lexer);
30383 tattr = cp_parser_attributes_opt (parser);
30384 gcc_assert (tattr) ;
30386 /* If the attributes are followed by a method introducer, this is not allowed.
30387 Dump the attributes and flag the situation. */
30388 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30389 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30390 return true;
30392 /* Otherwise, the attributes introduce some interstitial code, possibly so
30393 rewind to allow that check. */
30394 cp_lexer_rollback_tokens (parser->lexer);
30395 return false;
30398 /* Parse an Objective-C method prototype list. */
30400 static void
30401 cp_parser_objc_method_prototype_list (cp_parser* parser)
30403 cp_token *token = cp_lexer_peek_token (parser->lexer);
30405 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30407 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30409 tree attributes, sig;
30410 bool is_class_method;
30411 if (token->type == CPP_PLUS)
30412 is_class_method = true;
30413 else
30414 is_class_method = false;
30415 sig = cp_parser_objc_method_signature (parser, &attributes);
30416 if (sig == error_mark_node)
30418 cp_parser_skip_to_end_of_block_or_statement (parser);
30419 token = cp_lexer_peek_token (parser->lexer);
30420 continue;
30422 objc_add_method_declaration (is_class_method, sig, attributes);
30423 cp_parser_consume_semicolon_at_end_of_statement (parser);
30425 else if (token->keyword == RID_AT_PROPERTY)
30426 cp_parser_objc_at_property_declaration (parser);
30427 else if (token->keyword == RID_ATTRIBUTE
30428 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30429 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30430 OPT_Wattributes,
30431 "prefix attributes are ignored for methods");
30432 else
30433 /* Allow for interspersed non-ObjC++ code. */
30434 cp_parser_objc_interstitial_code (parser);
30436 token = cp_lexer_peek_token (parser->lexer);
30439 if (token->type != CPP_EOF)
30440 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30441 else
30442 cp_parser_error (parser, "expected %<@end%>");
30444 objc_finish_interface ();
30447 /* Parse an Objective-C method definition list. */
30449 static void
30450 cp_parser_objc_method_definition_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 tree meth;
30458 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30460 cp_token *ptk;
30461 tree sig, attribute;
30462 bool is_class_method;
30463 if (token->type == CPP_PLUS)
30464 is_class_method = true;
30465 else
30466 is_class_method = false;
30467 push_deferring_access_checks (dk_deferred);
30468 sig = cp_parser_objc_method_signature (parser, &attribute);
30469 if (sig == error_mark_node)
30471 cp_parser_skip_to_end_of_block_or_statement (parser);
30472 token = cp_lexer_peek_token (parser->lexer);
30473 continue;
30475 objc_start_method_definition (is_class_method, sig, attribute,
30476 NULL_TREE);
30478 /* For historical reasons, we accept an optional semicolon. */
30479 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30480 cp_lexer_consume_token (parser->lexer);
30482 ptk = cp_lexer_peek_token (parser->lexer);
30483 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30484 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30486 perform_deferred_access_checks (tf_warning_or_error);
30487 stop_deferring_access_checks ();
30488 meth = cp_parser_function_definition_after_declarator (parser,
30489 false);
30490 pop_deferring_access_checks ();
30491 objc_finish_method_definition (meth);
30494 /* The following case will be removed once @synthesize is
30495 completely implemented. */
30496 else if (token->keyword == RID_AT_PROPERTY)
30497 cp_parser_objc_at_property_declaration (parser);
30498 else if (token->keyword == RID_AT_SYNTHESIZE)
30499 cp_parser_objc_at_synthesize_declaration (parser);
30500 else if (token->keyword == RID_AT_DYNAMIC)
30501 cp_parser_objc_at_dynamic_declaration (parser);
30502 else if (token->keyword == RID_ATTRIBUTE
30503 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30504 warning_at (token->location, OPT_Wattributes,
30505 "prefix attributes are ignored for methods");
30506 else
30507 /* Allow for interspersed non-ObjC++ code. */
30508 cp_parser_objc_interstitial_code (parser);
30510 token = cp_lexer_peek_token (parser->lexer);
30513 if (token->type != CPP_EOF)
30514 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30515 else
30516 cp_parser_error (parser, "expected %<@end%>");
30518 objc_finish_implementation ();
30521 /* Parse Objective-C ivars. */
30523 static void
30524 cp_parser_objc_class_ivars (cp_parser* parser)
30526 cp_token *token = cp_lexer_peek_token (parser->lexer);
30528 if (token->type != CPP_OPEN_BRACE)
30529 return; /* No ivars specified. */
30531 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30532 token = cp_lexer_peek_token (parser->lexer);
30534 while (token->type != CPP_CLOSE_BRACE
30535 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30537 cp_decl_specifier_seq declspecs;
30538 int decl_class_or_enum_p;
30539 tree prefix_attributes;
30541 cp_parser_objc_visibility_spec (parser);
30543 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30544 break;
30546 cp_parser_decl_specifier_seq (parser,
30547 CP_PARSER_FLAGS_OPTIONAL,
30548 &declspecs,
30549 &decl_class_or_enum_p);
30551 /* auto, register, static, extern, mutable. */
30552 if (declspecs.storage_class != sc_none)
30554 cp_parser_error (parser, "invalid type for instance variable");
30555 declspecs.storage_class = sc_none;
30558 /* thread_local. */
30559 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30561 cp_parser_error (parser, "invalid type for instance variable");
30562 declspecs.locations[ds_thread] = 0;
30565 /* typedef. */
30566 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30568 cp_parser_error (parser, "invalid type for instance variable");
30569 declspecs.locations[ds_typedef] = 0;
30572 prefix_attributes = declspecs.attributes;
30573 declspecs.attributes = NULL_TREE;
30575 /* Keep going until we hit the `;' at the end of the
30576 declaration. */
30577 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30579 tree width = NULL_TREE, attributes, first_attribute, decl;
30580 cp_declarator *declarator = NULL;
30581 int ctor_dtor_or_conv_p;
30583 /* Check for a (possibly unnamed) bitfield declaration. */
30584 token = cp_lexer_peek_token (parser->lexer);
30585 if (token->type == CPP_COLON)
30586 goto eat_colon;
30588 if (token->type == CPP_NAME
30589 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30590 == CPP_COLON))
30592 /* Get the name of the bitfield. */
30593 declarator = make_id_declarator (NULL_TREE,
30594 cp_parser_identifier (parser),
30595 sfk_none);
30597 eat_colon:
30598 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30599 /* Get the width of the bitfield. */
30600 width
30601 = cp_parser_constant_expression (parser);
30603 else
30605 /* Parse the declarator. */
30606 declarator
30607 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30608 &ctor_dtor_or_conv_p,
30609 /*parenthesized_p=*/NULL,
30610 /*member_p=*/false,
30611 /*friend_p=*/false);
30614 /* Look for attributes that apply to the ivar. */
30615 attributes = cp_parser_attributes_opt (parser);
30616 /* Remember which attributes are prefix attributes and
30617 which are not. */
30618 first_attribute = attributes;
30619 /* Combine the attributes. */
30620 attributes = attr_chainon (prefix_attributes, attributes);
30622 if (width)
30623 /* Create the bitfield declaration. */
30624 decl = grokbitfield (declarator, &declspecs,
30625 width, NULL_TREE, attributes);
30626 else
30627 decl = grokfield (declarator, &declspecs,
30628 NULL_TREE, /*init_const_expr_p=*/false,
30629 NULL_TREE, attributes);
30631 /* Add the instance variable. */
30632 if (decl != error_mark_node && decl != NULL_TREE)
30633 objc_add_instance_variable (decl);
30635 /* Reset PREFIX_ATTRIBUTES. */
30636 if (attributes != error_mark_node)
30638 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30639 attributes = TREE_CHAIN (attributes);
30640 if (attributes)
30641 TREE_CHAIN (attributes) = NULL_TREE;
30644 token = cp_lexer_peek_token (parser->lexer);
30646 if (token->type == CPP_COMMA)
30648 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30649 continue;
30651 break;
30654 cp_parser_consume_semicolon_at_end_of_statement (parser);
30655 token = cp_lexer_peek_token (parser->lexer);
30658 if (token->keyword == RID_AT_END)
30659 cp_parser_error (parser, "expected %<}%>");
30661 /* Do not consume the RID_AT_END, so it will be read again as terminating
30662 the @interface of @implementation. */
30663 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30664 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30666 /* For historical reasons, we accept an optional semicolon. */
30667 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30668 cp_lexer_consume_token (parser->lexer);
30671 /* Parse an Objective-C protocol declaration. */
30673 static void
30674 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30676 tree proto, protorefs;
30677 cp_token *tok;
30679 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30680 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30682 tok = cp_lexer_peek_token (parser->lexer);
30683 error_at (tok->location, "identifier expected after %<@protocol%>");
30684 cp_parser_consume_semicolon_at_end_of_statement (parser);
30685 return;
30688 /* See if we have a forward declaration or a definition. */
30689 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30691 /* Try a forward declaration first. */
30692 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30694 while (true)
30696 tree id;
30698 id = cp_parser_identifier (parser);
30699 if (id == error_mark_node)
30700 break;
30702 objc_declare_protocol (id, attributes);
30704 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30705 cp_lexer_consume_token (parser->lexer);
30706 else
30707 break;
30709 cp_parser_consume_semicolon_at_end_of_statement (parser);
30712 /* Ok, we got a full-fledged definition (or at least should). */
30713 else
30715 proto = cp_parser_identifier (parser);
30716 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30717 objc_start_protocol (proto, protorefs, attributes);
30718 cp_parser_objc_method_prototype_list (parser);
30722 /* Parse an Objective-C superclass or category. */
30724 static void
30725 cp_parser_objc_superclass_or_category (cp_parser *parser,
30726 bool iface_p,
30727 tree *super,
30728 tree *categ, bool *is_class_extension)
30730 cp_token *next = cp_lexer_peek_token (parser->lexer);
30732 *super = *categ = NULL_TREE;
30733 *is_class_extension = false;
30734 if (next->type == CPP_COLON)
30736 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30737 *super = cp_parser_identifier (parser);
30739 else if (next->type == CPP_OPEN_PAREN)
30741 matching_parens parens;
30742 parens.consume_open (parser); /* Eat '('. */
30744 /* If there is no category name, and this is an @interface, we
30745 have a class extension. */
30746 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30748 *categ = NULL_TREE;
30749 *is_class_extension = true;
30751 else
30752 *categ = cp_parser_identifier (parser);
30754 parens.require_close (parser);
30758 /* Parse an Objective-C class interface. */
30760 static void
30761 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30763 tree name, super, categ, protos;
30764 bool is_class_extension;
30766 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30767 name = cp_parser_identifier (parser);
30768 if (name == error_mark_node)
30770 /* It's hard to recover because even if valid @interface stuff
30771 is to follow, we can't compile it (or validate it) if we
30772 don't even know which class it refers to. Let's assume this
30773 was a stray '@interface' token in the stream and skip it.
30775 return;
30777 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30778 &is_class_extension);
30779 protos = cp_parser_objc_protocol_refs_opt (parser);
30781 /* We have either a class or a category on our hands. */
30782 if (categ || is_class_extension)
30783 objc_start_category_interface (name, categ, protos, attributes);
30784 else
30786 objc_start_class_interface (name, super, protos, attributes);
30787 /* Handle instance variable declarations, if any. */
30788 cp_parser_objc_class_ivars (parser);
30789 objc_continue_interface ();
30792 cp_parser_objc_method_prototype_list (parser);
30795 /* Parse an Objective-C class implementation. */
30797 static void
30798 cp_parser_objc_class_implementation (cp_parser* parser)
30800 tree name, super, categ;
30801 bool is_class_extension;
30803 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30804 name = cp_parser_identifier (parser);
30805 if (name == error_mark_node)
30807 /* It's hard to recover because even if valid @implementation
30808 stuff is to follow, we can't compile it (or validate it) if
30809 we don't even know which class it refers to. Let's assume
30810 this was a stray '@implementation' token in the stream and
30811 skip it.
30813 return;
30815 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30816 &is_class_extension);
30818 /* We have either a class or a category on our hands. */
30819 if (categ)
30820 objc_start_category_implementation (name, categ);
30821 else
30823 objc_start_class_implementation (name, super);
30824 /* Handle instance variable declarations, if any. */
30825 cp_parser_objc_class_ivars (parser);
30826 objc_continue_implementation ();
30829 cp_parser_objc_method_definition_list (parser);
30832 /* Consume the @end token and finish off the implementation. */
30834 static void
30835 cp_parser_objc_end_implementation (cp_parser* parser)
30837 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30838 objc_finish_implementation ();
30841 /* Parse an Objective-C declaration. */
30843 static void
30844 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30846 /* Try to figure out what kind of declaration is present. */
30847 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30849 if (attributes)
30850 switch (kwd->keyword)
30852 case RID_AT_ALIAS:
30853 case RID_AT_CLASS:
30854 case RID_AT_END:
30855 error_at (kwd->location, "attributes may not be specified before"
30856 " the %<@%D%> Objective-C++ keyword",
30857 kwd->u.value);
30858 attributes = NULL;
30859 break;
30860 case RID_AT_IMPLEMENTATION:
30861 warning_at (kwd->location, OPT_Wattributes,
30862 "prefix attributes are ignored before %<@%D%>",
30863 kwd->u.value);
30864 attributes = NULL;
30865 default:
30866 break;
30869 switch (kwd->keyword)
30871 case RID_AT_ALIAS:
30872 cp_parser_objc_alias_declaration (parser);
30873 break;
30874 case RID_AT_CLASS:
30875 cp_parser_objc_class_declaration (parser);
30876 break;
30877 case RID_AT_PROTOCOL:
30878 cp_parser_objc_protocol_declaration (parser, attributes);
30879 break;
30880 case RID_AT_INTERFACE:
30881 cp_parser_objc_class_interface (parser, attributes);
30882 break;
30883 case RID_AT_IMPLEMENTATION:
30884 cp_parser_objc_class_implementation (parser);
30885 break;
30886 case RID_AT_END:
30887 cp_parser_objc_end_implementation (parser);
30888 break;
30889 default:
30890 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30891 kwd->u.value);
30892 cp_parser_skip_to_end_of_block_or_statement (parser);
30896 /* Parse an Objective-C try-catch-finally statement.
30898 objc-try-catch-finally-stmt:
30899 @try compound-statement objc-catch-clause-seq [opt]
30900 objc-finally-clause [opt]
30902 objc-catch-clause-seq:
30903 objc-catch-clause objc-catch-clause-seq [opt]
30905 objc-catch-clause:
30906 @catch ( objc-exception-declaration ) compound-statement
30908 objc-finally-clause:
30909 @finally compound-statement
30911 objc-exception-declaration:
30912 parameter-declaration
30913 '...'
30915 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30917 Returns NULL_TREE.
30919 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30920 for C. Keep them in sync. */
30922 static tree
30923 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30925 location_t location;
30926 tree stmt;
30928 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30929 location = cp_lexer_peek_token (parser->lexer)->location;
30930 objc_maybe_warn_exceptions (location);
30931 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30932 node, lest it get absorbed into the surrounding block. */
30933 stmt = push_stmt_list ();
30934 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30935 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30937 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30939 cp_parameter_declarator *parm;
30940 tree parameter_declaration = error_mark_node;
30941 bool seen_open_paren = false;
30942 matching_parens parens;
30944 cp_lexer_consume_token (parser->lexer);
30945 if (parens.require_open (parser))
30946 seen_open_paren = true;
30947 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30949 /* We have "@catch (...)" (where the '...' are literally
30950 what is in the code). Skip the '...'.
30951 parameter_declaration is set to NULL_TREE, and
30952 objc_being_catch_clauses() knows that that means
30953 '...'. */
30954 cp_lexer_consume_token (parser->lexer);
30955 parameter_declaration = NULL_TREE;
30957 else
30959 /* We have "@catch (NSException *exception)" or something
30960 like that. Parse the parameter declaration. */
30961 parm = cp_parser_parameter_declaration (parser, false, NULL);
30962 if (parm == NULL)
30963 parameter_declaration = error_mark_node;
30964 else
30965 parameter_declaration = grokdeclarator (parm->declarator,
30966 &parm->decl_specifiers,
30967 PARM, /*initialized=*/0,
30968 /*attrlist=*/NULL);
30970 if (seen_open_paren)
30971 parens.require_close (parser);
30972 else
30974 /* If there was no open parenthesis, we are recovering from
30975 an error, and we are trying to figure out what mistake
30976 the user has made. */
30978 /* If there is an immediate closing parenthesis, the user
30979 probably forgot the opening one (ie, they typed "@catch
30980 NSException *e)". Parse the closing parenthesis and keep
30981 going. */
30982 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30983 cp_lexer_consume_token (parser->lexer);
30985 /* If these is no immediate closing parenthesis, the user
30986 probably doesn't know that parenthesis are required at
30987 all (ie, they typed "@catch NSException *e"). So, just
30988 forget about the closing parenthesis and keep going. */
30990 objc_begin_catch_clause (parameter_declaration);
30991 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30992 objc_finish_catch_clause ();
30994 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30996 cp_lexer_consume_token (parser->lexer);
30997 location = cp_lexer_peek_token (parser->lexer)->location;
30998 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30999 node, lest it get absorbed into the surrounding block. */
31000 stmt = push_stmt_list ();
31001 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31002 objc_build_finally_clause (location, pop_stmt_list (stmt));
31005 return objc_finish_try_stmt ();
31008 /* Parse an Objective-C synchronized statement.
31010 objc-synchronized-stmt:
31011 @synchronized ( expression ) compound-statement
31013 Returns NULL_TREE. */
31015 static tree
31016 cp_parser_objc_synchronized_statement (cp_parser *parser)
31018 location_t location;
31019 tree lock, stmt;
31021 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31023 location = cp_lexer_peek_token (parser->lexer)->location;
31024 objc_maybe_warn_exceptions (location);
31025 matching_parens parens;
31026 parens.require_open (parser);
31027 lock = cp_parser_expression (parser);
31028 parens.require_close (parser);
31030 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31031 node, lest it get absorbed into the surrounding block. */
31032 stmt = push_stmt_list ();
31033 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31035 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31038 /* Parse an Objective-C throw statement.
31040 objc-throw-stmt:
31041 @throw assignment-expression [opt] ;
31043 Returns a constructed '@throw' statement. */
31045 static tree
31046 cp_parser_objc_throw_statement (cp_parser *parser)
31048 tree expr = NULL_TREE;
31049 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31051 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31053 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31054 expr = cp_parser_expression (parser);
31056 cp_parser_consume_semicolon_at_end_of_statement (parser);
31058 return objc_build_throw_stmt (loc, expr);
31061 /* Parse an Objective-C statement. */
31063 static tree
31064 cp_parser_objc_statement (cp_parser * parser)
31066 /* Try to figure out what kind of declaration is present. */
31067 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31069 switch (kwd->keyword)
31071 case RID_AT_TRY:
31072 return cp_parser_objc_try_catch_finally_statement (parser);
31073 case RID_AT_SYNCHRONIZED:
31074 return cp_parser_objc_synchronized_statement (parser);
31075 case RID_AT_THROW:
31076 return cp_parser_objc_throw_statement (parser);
31077 default:
31078 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31079 kwd->u.value);
31080 cp_parser_skip_to_end_of_block_or_statement (parser);
31083 return error_mark_node;
31086 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31087 look ahead to see if an objc keyword follows the attributes. This
31088 is to detect the use of prefix attributes on ObjC @interface and
31089 @protocol. */
31091 static bool
31092 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31094 cp_lexer_save_tokens (parser->lexer);
31095 *attrib = cp_parser_attributes_opt (parser);
31096 gcc_assert (*attrib);
31097 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31099 cp_lexer_commit_tokens (parser->lexer);
31100 return true;
31102 cp_lexer_rollback_tokens (parser->lexer);
31103 return false;
31106 /* This routine is a minimal replacement for
31107 c_parser_struct_declaration () used when parsing the list of
31108 types/names or ObjC++ properties. For example, when parsing the
31109 code
31111 @property (readonly) int a, b, c;
31113 this function is responsible for parsing "int a, int b, int c" and
31114 returning the declarations as CHAIN of DECLs.
31116 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31117 similar parsing. */
31118 static tree
31119 cp_parser_objc_struct_declaration (cp_parser *parser)
31121 tree decls = NULL_TREE;
31122 cp_decl_specifier_seq declspecs;
31123 int decl_class_or_enum_p;
31124 tree prefix_attributes;
31126 cp_parser_decl_specifier_seq (parser,
31127 CP_PARSER_FLAGS_NONE,
31128 &declspecs,
31129 &decl_class_or_enum_p);
31131 if (declspecs.type == error_mark_node)
31132 return error_mark_node;
31134 /* auto, register, static, extern, mutable. */
31135 if (declspecs.storage_class != sc_none)
31137 cp_parser_error (parser, "invalid type for property");
31138 declspecs.storage_class = sc_none;
31141 /* thread_local. */
31142 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31144 cp_parser_error (parser, "invalid type for property");
31145 declspecs.locations[ds_thread] = 0;
31148 /* typedef. */
31149 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31151 cp_parser_error (parser, "invalid type for property");
31152 declspecs.locations[ds_typedef] = 0;
31155 prefix_attributes = declspecs.attributes;
31156 declspecs.attributes = NULL_TREE;
31158 /* Keep going until we hit the `;' at the end of the declaration. */
31159 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31161 tree attributes, first_attribute, decl;
31162 cp_declarator *declarator;
31163 cp_token *token;
31165 /* Parse the declarator. */
31166 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31167 NULL, NULL, false, false);
31169 /* Look for attributes that apply to the ivar. */
31170 attributes = cp_parser_attributes_opt (parser);
31171 /* Remember which attributes are prefix attributes and
31172 which are not. */
31173 first_attribute = attributes;
31174 /* Combine the attributes. */
31175 attributes = attr_chainon (prefix_attributes, attributes);
31177 decl = grokfield (declarator, &declspecs,
31178 NULL_TREE, /*init_const_expr_p=*/false,
31179 NULL_TREE, attributes);
31181 if (decl == error_mark_node || decl == NULL_TREE)
31182 return error_mark_node;
31184 /* Reset PREFIX_ATTRIBUTES. */
31185 if (attributes != error_mark_node)
31187 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31188 attributes = TREE_CHAIN (attributes);
31189 if (attributes)
31190 TREE_CHAIN (attributes) = NULL_TREE;
31193 DECL_CHAIN (decl) = decls;
31194 decls = decl;
31196 token = cp_lexer_peek_token (parser->lexer);
31197 if (token->type == CPP_COMMA)
31199 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31200 continue;
31202 else
31203 break;
31205 return decls;
31208 /* Parse an Objective-C @property declaration. The syntax is:
31210 objc-property-declaration:
31211 '@property' objc-property-attributes[opt] struct-declaration ;
31213 objc-property-attributes:
31214 '(' objc-property-attribute-list ')'
31216 objc-property-attribute-list:
31217 objc-property-attribute
31218 objc-property-attribute-list, objc-property-attribute
31220 objc-property-attribute
31221 'getter' = identifier
31222 'setter' = identifier
31223 'readonly'
31224 'readwrite'
31225 'assign'
31226 'retain'
31227 'copy'
31228 'nonatomic'
31230 For example:
31231 @property NSString *name;
31232 @property (readonly) id object;
31233 @property (retain, nonatomic, getter=getTheName) id name;
31234 @property int a, b, c;
31236 PS: This function is identical to
31237 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31238 static void
31239 cp_parser_objc_at_property_declaration (cp_parser *parser)
31241 /* The following variables hold the attributes of the properties as
31242 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31243 seen. When we see an attribute, we set them to 'true' (if they
31244 are boolean properties) or to the identifier (if they have an
31245 argument, ie, for getter and setter). Note that here we only
31246 parse the list of attributes, check the syntax and accumulate the
31247 attributes that we find. objc_add_property_declaration() will
31248 then process the information. */
31249 bool property_assign = false;
31250 bool property_copy = false;
31251 tree property_getter_ident = NULL_TREE;
31252 bool property_nonatomic = false;
31253 bool property_readonly = false;
31254 bool property_readwrite = false;
31255 bool property_retain = false;
31256 tree property_setter_ident = NULL_TREE;
31258 /* 'properties' is the list of properties that we read. Usually a
31259 single one, but maybe more (eg, in "@property int a, b, c;" there
31260 are three). */
31261 tree properties;
31262 location_t loc;
31264 loc = cp_lexer_peek_token (parser->lexer)->location;
31266 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31268 /* Parse the optional attribute list... */
31269 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31271 /* Eat the '('. */
31272 matching_parens parens;
31273 parens.consume_open (parser);
31275 while (true)
31277 bool syntax_error = false;
31278 cp_token *token = cp_lexer_peek_token (parser->lexer);
31279 enum rid keyword;
31281 if (token->type != CPP_NAME)
31283 cp_parser_error (parser, "expected identifier");
31284 break;
31286 keyword = C_RID_CODE (token->u.value);
31287 cp_lexer_consume_token (parser->lexer);
31288 switch (keyword)
31290 case RID_ASSIGN: property_assign = true; break;
31291 case RID_COPY: property_copy = true; break;
31292 case RID_NONATOMIC: property_nonatomic = true; break;
31293 case RID_READONLY: property_readonly = true; break;
31294 case RID_READWRITE: property_readwrite = true; break;
31295 case RID_RETAIN: property_retain = true; break;
31297 case RID_GETTER:
31298 case RID_SETTER:
31299 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31301 if (keyword == RID_GETTER)
31302 cp_parser_error (parser,
31303 "missing %<=%> (after %<getter%> attribute)");
31304 else
31305 cp_parser_error (parser,
31306 "missing %<=%> (after %<setter%> attribute)");
31307 syntax_error = true;
31308 break;
31310 cp_lexer_consume_token (parser->lexer); /* eat the = */
31311 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31313 cp_parser_error (parser, "expected identifier");
31314 syntax_error = true;
31315 break;
31317 if (keyword == RID_SETTER)
31319 if (property_setter_ident != NULL_TREE)
31321 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31322 cp_lexer_consume_token (parser->lexer);
31324 else
31325 property_setter_ident = cp_parser_objc_selector (parser);
31326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31327 cp_parser_error (parser, "setter name must terminate with %<:%>");
31328 else
31329 cp_lexer_consume_token (parser->lexer);
31331 else
31333 if (property_getter_ident != NULL_TREE)
31335 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31336 cp_lexer_consume_token (parser->lexer);
31338 else
31339 property_getter_ident = cp_parser_objc_selector (parser);
31341 break;
31342 default:
31343 cp_parser_error (parser, "unknown property attribute");
31344 syntax_error = true;
31345 break;
31348 if (syntax_error)
31349 break;
31351 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31352 cp_lexer_consume_token (parser->lexer);
31353 else
31354 break;
31357 /* FIXME: "@property (setter, assign);" will generate a spurious
31358 "error: expected ‘)’ before ‘,’ token". This is because
31359 cp_parser_require, unlike the C counterpart, will produce an
31360 error even if we are in error recovery. */
31361 if (!parens.require_close (parser))
31363 cp_parser_skip_to_closing_parenthesis (parser,
31364 /*recovering=*/true,
31365 /*or_comma=*/false,
31366 /*consume_paren=*/true);
31370 /* ... and the property declaration(s). */
31371 properties = cp_parser_objc_struct_declaration (parser);
31373 if (properties == error_mark_node)
31375 cp_parser_skip_to_end_of_statement (parser);
31376 /* If the next token is now a `;', consume it. */
31377 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31378 cp_lexer_consume_token (parser->lexer);
31379 return;
31382 if (properties == NULL_TREE)
31383 cp_parser_error (parser, "expected identifier");
31384 else
31386 /* Comma-separated properties are chained together in
31387 reverse order; add them one by one. */
31388 properties = nreverse (properties);
31390 for (; properties; properties = TREE_CHAIN (properties))
31391 objc_add_property_declaration (loc, copy_node (properties),
31392 property_readonly, property_readwrite,
31393 property_assign, property_retain,
31394 property_copy, property_nonatomic,
31395 property_getter_ident, property_setter_ident);
31398 cp_parser_consume_semicolon_at_end_of_statement (parser);
31401 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31403 objc-synthesize-declaration:
31404 @synthesize objc-synthesize-identifier-list ;
31406 objc-synthesize-identifier-list:
31407 objc-synthesize-identifier
31408 objc-synthesize-identifier-list, objc-synthesize-identifier
31410 objc-synthesize-identifier
31411 identifier
31412 identifier = identifier
31414 For example:
31415 @synthesize MyProperty;
31416 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31418 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31419 for C. Keep them in sync.
31421 static void
31422 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31424 tree list = NULL_TREE;
31425 location_t loc;
31426 loc = cp_lexer_peek_token (parser->lexer)->location;
31428 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31429 while (true)
31431 tree property, ivar;
31432 property = cp_parser_identifier (parser);
31433 if (property == error_mark_node)
31435 cp_parser_consume_semicolon_at_end_of_statement (parser);
31436 return;
31438 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31440 cp_lexer_consume_token (parser->lexer);
31441 ivar = cp_parser_identifier (parser);
31442 if (ivar == error_mark_node)
31444 cp_parser_consume_semicolon_at_end_of_statement (parser);
31445 return;
31448 else
31449 ivar = NULL_TREE;
31450 list = chainon (list, build_tree_list (ivar, property));
31451 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31452 cp_lexer_consume_token (parser->lexer);
31453 else
31454 break;
31456 cp_parser_consume_semicolon_at_end_of_statement (parser);
31457 objc_add_synthesize_declaration (loc, list);
31460 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31462 objc-dynamic-declaration:
31463 @dynamic identifier-list ;
31465 For example:
31466 @dynamic MyProperty;
31467 @dynamic MyProperty, AnotherProperty;
31469 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31470 for C. Keep them in sync.
31472 static void
31473 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31475 tree list = NULL_TREE;
31476 location_t loc;
31477 loc = cp_lexer_peek_token (parser->lexer)->location;
31479 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31480 while (true)
31482 tree property;
31483 property = cp_parser_identifier (parser);
31484 if (property == error_mark_node)
31486 cp_parser_consume_semicolon_at_end_of_statement (parser);
31487 return;
31489 list = chainon (list, build_tree_list (NULL, property));
31490 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31491 cp_lexer_consume_token (parser->lexer);
31492 else
31493 break;
31495 cp_parser_consume_semicolon_at_end_of_statement (parser);
31496 objc_add_dynamic_declaration (loc, list);
31500 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
31502 /* Returns name of the next clause.
31503 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31504 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31505 returned and the token is consumed. */
31507 static pragma_omp_clause
31508 cp_parser_omp_clause_name (cp_parser *parser)
31510 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31512 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31513 result = PRAGMA_OACC_CLAUSE_AUTO;
31514 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31515 result = PRAGMA_OMP_CLAUSE_IF;
31516 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31517 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31518 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31519 result = PRAGMA_OACC_CLAUSE_DELETE;
31520 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31521 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31522 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31523 result = PRAGMA_OMP_CLAUSE_FOR;
31524 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31526 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31527 const char *p = IDENTIFIER_POINTER (id);
31529 switch (p[0])
31531 case 'a':
31532 if (!strcmp ("aligned", p))
31533 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31534 else if (!strcmp ("async", p))
31535 result = PRAGMA_OACC_CLAUSE_ASYNC;
31536 break;
31537 case 'c':
31538 if (!strcmp ("collapse", p))
31539 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31540 else if (!strcmp ("copy", p))
31541 result = PRAGMA_OACC_CLAUSE_COPY;
31542 else if (!strcmp ("copyin", p))
31543 result = PRAGMA_OMP_CLAUSE_COPYIN;
31544 else if (!strcmp ("copyout", p))
31545 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31546 else if (!strcmp ("copyprivate", p))
31547 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31548 else if (!strcmp ("create", p))
31549 result = PRAGMA_OACC_CLAUSE_CREATE;
31550 break;
31551 case 'd':
31552 if (!strcmp ("defaultmap", p))
31553 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31554 else if (!strcmp ("depend", p))
31555 result = PRAGMA_OMP_CLAUSE_DEPEND;
31556 else if (!strcmp ("device", p))
31557 result = PRAGMA_OMP_CLAUSE_DEVICE;
31558 else if (!strcmp ("deviceptr", p))
31559 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31560 else if (!strcmp ("device_resident", p))
31561 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31562 else if (!strcmp ("dist_schedule", p))
31563 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31564 break;
31565 case 'f':
31566 if (!strcmp ("final", p))
31567 result = PRAGMA_OMP_CLAUSE_FINAL;
31568 else if (!strcmp ("finalize", p))
31569 result = PRAGMA_OACC_CLAUSE_FINALIZE;
31570 else if (!strcmp ("firstprivate", p))
31571 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31572 else if (!strcmp ("from", p))
31573 result = PRAGMA_OMP_CLAUSE_FROM;
31574 break;
31575 case 'g':
31576 if (!strcmp ("gang", p))
31577 result = PRAGMA_OACC_CLAUSE_GANG;
31578 else if (!strcmp ("grainsize", p))
31579 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31580 break;
31581 case 'h':
31582 if (!strcmp ("hint", p))
31583 result = PRAGMA_OMP_CLAUSE_HINT;
31584 else if (!strcmp ("host", p))
31585 result = PRAGMA_OACC_CLAUSE_HOST;
31586 break;
31587 case 'i':
31588 if (!strcmp ("if_present", p))
31589 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
31590 else if (!strcmp ("in_reduction", p))
31591 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
31592 else if (!strcmp ("inbranch", p))
31593 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31594 else if (!strcmp ("independent", p))
31595 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31596 else if (!strcmp ("is_device_ptr", p))
31597 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31598 break;
31599 case 'l':
31600 if (!strcmp ("lastprivate", p))
31601 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31602 else if (!strcmp ("linear", p))
31603 result = PRAGMA_OMP_CLAUSE_LINEAR;
31604 else if (!strcmp ("link", p))
31605 result = PRAGMA_OMP_CLAUSE_LINK;
31606 break;
31607 case 'm':
31608 if (!strcmp ("map", p))
31609 result = PRAGMA_OMP_CLAUSE_MAP;
31610 else if (!strcmp ("mergeable", p))
31611 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31612 break;
31613 case 'n':
31614 if (!strcmp ("nogroup", p))
31615 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31616 else if (!strcmp ("nontemporal", p))
31617 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
31618 else if (!strcmp ("notinbranch", p))
31619 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31620 else if (!strcmp ("nowait", p))
31621 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31622 else if (!strcmp ("num_gangs", p))
31623 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31624 else if (!strcmp ("num_tasks", p))
31625 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31626 else if (!strcmp ("num_teams", p))
31627 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31628 else if (!strcmp ("num_threads", p))
31629 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31630 else if (!strcmp ("num_workers", p))
31631 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31632 break;
31633 case 'o':
31634 if (!strcmp ("ordered", p))
31635 result = PRAGMA_OMP_CLAUSE_ORDERED;
31636 break;
31637 case 'p':
31638 if (!strcmp ("parallel", p))
31639 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31640 else if (!strcmp ("present", p))
31641 result = PRAGMA_OACC_CLAUSE_PRESENT;
31642 else if (!strcmp ("present_or_copy", p)
31643 || !strcmp ("pcopy", p))
31644 result = PRAGMA_OACC_CLAUSE_COPY;
31645 else if (!strcmp ("present_or_copyin", p)
31646 || !strcmp ("pcopyin", p))
31647 result = PRAGMA_OACC_CLAUSE_COPYIN;
31648 else if (!strcmp ("present_or_copyout", p)
31649 || !strcmp ("pcopyout", p))
31650 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31651 else if (!strcmp ("present_or_create", p)
31652 || !strcmp ("pcreate", p))
31653 result = PRAGMA_OACC_CLAUSE_CREATE;
31654 else if (!strcmp ("priority", p))
31655 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31656 else if (!strcmp ("proc_bind", p))
31657 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31658 break;
31659 case 'r':
31660 if (!strcmp ("reduction", p))
31661 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31662 break;
31663 case 's':
31664 if (!strcmp ("safelen", p))
31665 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31666 else if (!strcmp ("schedule", p))
31667 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31668 else if (!strcmp ("sections", p))
31669 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31670 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
31671 result = PRAGMA_OACC_CLAUSE_HOST;
31672 else if (!strcmp ("seq", p))
31673 result = PRAGMA_OACC_CLAUSE_SEQ;
31674 else if (!strcmp ("shared", p))
31675 result = PRAGMA_OMP_CLAUSE_SHARED;
31676 else if (!strcmp ("simd", p))
31677 result = PRAGMA_OMP_CLAUSE_SIMD;
31678 else if (!strcmp ("simdlen", p))
31679 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31680 break;
31681 case 't':
31682 if (!strcmp ("task_reduction", p))
31683 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
31684 else if (!strcmp ("taskgroup", p))
31685 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31686 else if (!strcmp ("thread_limit", p))
31687 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31688 else if (!strcmp ("threads", p))
31689 result = PRAGMA_OMP_CLAUSE_THREADS;
31690 else if (!strcmp ("tile", p))
31691 result = PRAGMA_OACC_CLAUSE_TILE;
31692 else if (!strcmp ("to", p))
31693 result = PRAGMA_OMP_CLAUSE_TO;
31694 break;
31695 case 'u':
31696 if (!strcmp ("uniform", p))
31697 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31698 else if (!strcmp ("untied", p))
31699 result = PRAGMA_OMP_CLAUSE_UNTIED;
31700 else if (!strcmp ("use_device", p))
31701 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31702 else if (!strcmp ("use_device_ptr", p))
31703 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31704 break;
31705 case 'v':
31706 if (!strcmp ("vector", p))
31707 result = PRAGMA_OACC_CLAUSE_VECTOR;
31708 else if (!strcmp ("vector_length", p))
31709 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31710 break;
31711 case 'w':
31712 if (!strcmp ("wait", p))
31713 result = PRAGMA_OACC_CLAUSE_WAIT;
31714 else if (!strcmp ("worker", p))
31715 result = PRAGMA_OACC_CLAUSE_WORKER;
31716 break;
31720 if (result != PRAGMA_OMP_CLAUSE_NONE)
31721 cp_lexer_consume_token (parser->lexer);
31723 return result;
31726 /* Validate that a clause of the given type does not already exist. */
31728 static void
31729 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31730 const char *name, location_t location)
31732 tree c;
31734 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31735 if (OMP_CLAUSE_CODE (c) == code)
31737 error_at (location, "too many %qs clauses", name);
31738 break;
31742 /* OpenMP 2.5:
31743 variable-list:
31744 identifier
31745 variable-list , identifier
31747 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31748 colon). An opening parenthesis will have been consumed by the caller.
31750 If KIND is nonzero, create the appropriate node and install the decl
31751 in OMP_CLAUSE_DECL and add the node to the head of the list.
31753 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31754 return the list created.
31756 COLON can be NULL if only closing parenthesis should end the list,
31757 or pointer to bool which will receive false if the list is terminated
31758 by closing parenthesis or true if the list is terminated by colon. */
31760 static tree
31761 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31762 tree list, bool *colon)
31764 cp_token *token;
31765 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31766 if (colon)
31768 parser->colon_corrects_to_scope_p = false;
31769 *colon = false;
31771 while (1)
31773 tree name, decl;
31775 if (kind == OMP_CLAUSE_DEPEND)
31776 cp_parser_parse_tentatively (parser);
31777 token = cp_lexer_peek_token (parser->lexer);
31778 if (kind != 0
31779 && current_class_ptr
31780 && cp_parser_is_keyword (token, RID_THIS))
31782 decl = finish_this_expr ();
31783 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31784 || CONVERT_EXPR_P (decl))
31785 decl = TREE_OPERAND (decl, 0);
31786 cp_lexer_consume_token (parser->lexer);
31788 else
31790 name = cp_parser_id_expression (parser, /*template_p=*/false,
31791 /*check_dependency_p=*/true,
31792 /*template_p=*/NULL,
31793 /*declarator_p=*/false,
31794 /*optional_p=*/false);
31795 if (name == error_mark_node)
31797 if (kind == OMP_CLAUSE_DEPEND
31798 && cp_parser_simulate_error (parser))
31799 goto depend_lvalue;
31800 goto skip_comma;
31803 if (identifier_p (name))
31804 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31805 else
31806 decl = name;
31807 if (decl == error_mark_node)
31809 if (kind == OMP_CLAUSE_DEPEND
31810 && cp_parser_simulate_error (parser))
31811 goto depend_lvalue;
31812 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31813 token->location);
31816 if (decl == error_mark_node)
31818 else if (kind != 0)
31820 switch (kind)
31822 case OMP_CLAUSE__CACHE_:
31823 /* The OpenACC cache directive explicitly only allows "array
31824 elements or subarrays". */
31825 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31827 error_at (token->location, "expected %<[%>");
31828 decl = error_mark_node;
31829 break;
31831 /* FALLTHROUGH. */
31832 case OMP_CLAUSE_MAP:
31833 case OMP_CLAUSE_FROM:
31834 case OMP_CLAUSE_TO:
31835 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31837 location_t loc
31838 = cp_lexer_peek_token (parser->lexer)->location;
31839 cp_id_kind idk = CP_ID_KIND_NONE;
31840 cp_lexer_consume_token (parser->lexer);
31841 decl = convert_from_reference (decl);
31842 decl
31843 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31844 decl, false,
31845 &idk, loc);
31847 /* FALLTHROUGH. */
31848 case OMP_CLAUSE_DEPEND:
31849 case OMP_CLAUSE_REDUCTION:
31850 case OMP_CLAUSE_IN_REDUCTION:
31851 case OMP_CLAUSE_TASK_REDUCTION:
31852 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31854 tree low_bound = NULL_TREE, length = NULL_TREE;
31856 parser->colon_corrects_to_scope_p = false;
31857 cp_lexer_consume_token (parser->lexer);
31858 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31859 low_bound = cp_parser_expression (parser);
31860 if (!colon)
31861 parser->colon_corrects_to_scope_p
31862 = saved_colon_corrects_to_scope_p;
31863 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31864 length = integer_one_node;
31865 else
31867 /* Look for `:'. */
31868 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31870 if (kind == OMP_CLAUSE_DEPEND
31871 && cp_parser_simulate_error (parser))
31872 goto depend_lvalue;
31873 goto skip_comma;
31875 if (kind == OMP_CLAUSE_DEPEND)
31876 cp_parser_commit_to_tentative_parse (parser);
31877 if (!cp_lexer_next_token_is (parser->lexer,
31878 CPP_CLOSE_SQUARE))
31879 length = cp_parser_expression (parser);
31881 /* Look for the closing `]'. */
31882 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31883 RT_CLOSE_SQUARE))
31885 if (kind == OMP_CLAUSE_DEPEND
31886 && cp_parser_simulate_error (parser))
31887 goto depend_lvalue;
31888 goto skip_comma;
31891 decl = tree_cons (low_bound, length, decl);
31893 break;
31894 default:
31895 break;
31898 if (kind == OMP_CLAUSE_DEPEND)
31900 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
31901 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
31902 && cp_parser_simulate_error (parser))
31904 depend_lvalue:
31905 cp_parser_abort_tentative_parse (parser);
31906 decl = cp_parser_assignment_expression (parser, NULL,
31907 false, false);
31909 else
31910 cp_parser_parse_definitely (parser);
31913 tree u = build_omp_clause (token->location, kind);
31914 OMP_CLAUSE_DECL (u) = decl;
31915 OMP_CLAUSE_CHAIN (u) = list;
31916 list = u;
31918 else
31919 list = tree_cons (decl, NULL_TREE, list);
31921 get_comma:
31922 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31923 break;
31924 cp_lexer_consume_token (parser->lexer);
31927 if (colon)
31928 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31930 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31932 *colon = true;
31933 cp_parser_require (parser, CPP_COLON, RT_COLON);
31934 return list;
31937 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31939 int ending;
31941 /* Try to resync to an unnested comma. Copied from
31942 cp_parser_parenthesized_expression_list. */
31943 skip_comma:
31944 if (colon)
31945 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31946 ending = cp_parser_skip_to_closing_parenthesis (parser,
31947 /*recovering=*/true,
31948 /*or_comma=*/true,
31949 /*consume_paren=*/true);
31950 if (ending < 0)
31951 goto get_comma;
31954 return list;
31957 /* Similarly, but expect leading and trailing parenthesis. This is a very
31958 common case for omp clauses. */
31960 static tree
31961 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31963 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31964 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31965 return list;
31968 /* OpenACC 2.0:
31969 copy ( variable-list )
31970 copyin ( variable-list )
31971 copyout ( variable-list )
31972 create ( variable-list )
31973 delete ( variable-list )
31974 present ( variable-list ) */
31976 static tree
31977 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31978 tree list)
31980 enum gomp_map_kind kind;
31981 switch (c_kind)
31983 case PRAGMA_OACC_CLAUSE_COPY:
31984 kind = GOMP_MAP_TOFROM;
31985 break;
31986 case PRAGMA_OACC_CLAUSE_COPYIN:
31987 kind = GOMP_MAP_TO;
31988 break;
31989 case PRAGMA_OACC_CLAUSE_COPYOUT:
31990 kind = GOMP_MAP_FROM;
31991 break;
31992 case PRAGMA_OACC_CLAUSE_CREATE:
31993 kind = GOMP_MAP_ALLOC;
31994 break;
31995 case PRAGMA_OACC_CLAUSE_DELETE:
31996 kind = GOMP_MAP_RELEASE;
31997 break;
31998 case PRAGMA_OACC_CLAUSE_DEVICE:
31999 kind = GOMP_MAP_FORCE_TO;
32000 break;
32001 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32002 kind = GOMP_MAP_DEVICE_RESIDENT;
32003 break;
32004 case PRAGMA_OACC_CLAUSE_HOST:
32005 kind = GOMP_MAP_FORCE_FROM;
32006 break;
32007 case PRAGMA_OACC_CLAUSE_LINK:
32008 kind = GOMP_MAP_LINK;
32009 break;
32010 case PRAGMA_OACC_CLAUSE_PRESENT:
32011 kind = GOMP_MAP_FORCE_PRESENT;
32012 break;
32013 default:
32014 gcc_unreachable ();
32016 tree nl, c;
32017 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32019 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32020 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32022 return nl;
32025 /* OpenACC 2.0:
32026 deviceptr ( variable-list ) */
32028 static tree
32029 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32031 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32032 tree vars, t;
32034 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32035 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32036 variable-list must only allow for pointer variables. */
32037 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32038 for (t = vars; t; t = TREE_CHAIN (t))
32040 tree v = TREE_PURPOSE (t);
32041 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32042 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32043 OMP_CLAUSE_DECL (u) = v;
32044 OMP_CLAUSE_CHAIN (u) = list;
32045 list = u;
32048 return list;
32051 /* OpenACC 2.5:
32052 auto
32053 finalize
32054 independent
32055 nohost
32056 seq */
32058 static tree
32059 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
32060 enum omp_clause_code code,
32061 tree list, location_t location)
32063 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32064 tree c = build_omp_clause (location, code);
32065 OMP_CLAUSE_CHAIN (c) = list;
32066 return c;
32069 /* OpenACC:
32070 num_gangs ( expression )
32071 num_workers ( expression )
32072 vector_length ( expression ) */
32074 static tree
32075 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32076 const char *str, tree list)
32078 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32080 matching_parens parens;
32081 if (!parens.require_open (parser))
32082 return list;
32084 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32086 if (t == error_mark_node
32087 || !parens.require_close (parser))
32089 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32090 /*or_comma=*/false,
32091 /*consume_paren=*/true);
32092 return list;
32095 check_no_duplicate_clause (list, code, str, loc);
32097 tree c = build_omp_clause (loc, code);
32098 OMP_CLAUSE_OPERAND (c, 0) = t;
32099 OMP_CLAUSE_CHAIN (c) = list;
32100 return c;
32103 /* OpenACC:
32105 gang [( gang-arg-list )]
32106 worker [( [num:] int-expr )]
32107 vector [( [length:] int-expr )]
32109 where gang-arg is one of:
32111 [num:] int-expr
32112 static: size-expr
32114 and size-expr may be:
32117 int-expr
32120 static tree
32121 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
32122 const char *str, tree list)
32124 const char *id = "num";
32125 cp_lexer *lexer = parser->lexer;
32126 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32127 location_t loc = cp_lexer_peek_token (lexer)->location;
32129 if (kind == OMP_CLAUSE_VECTOR)
32130 id = "length";
32132 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32134 matching_parens parens;
32135 parens.consume_open (parser);
32139 cp_token *next = cp_lexer_peek_token (lexer);
32140 int idx = 0;
32142 /* Gang static argument. */
32143 if (kind == OMP_CLAUSE_GANG
32144 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32146 cp_lexer_consume_token (lexer);
32148 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32149 goto cleanup_error;
32151 idx = 1;
32152 if (ops[idx] != NULL)
32154 cp_parser_error (parser, "too many %<static%> arguments");
32155 goto cleanup_error;
32158 /* Check for the '*' argument. */
32159 if (cp_lexer_next_token_is (lexer, CPP_MULT)
32160 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32161 || cp_lexer_nth_token_is (parser->lexer, 2,
32162 CPP_CLOSE_PAREN)))
32164 cp_lexer_consume_token (lexer);
32165 ops[idx] = integer_minus_one_node;
32167 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32169 cp_lexer_consume_token (lexer);
32170 continue;
32172 else break;
32175 /* Worker num: argument and vector length: arguments. */
32176 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32177 && id_equal (next->u.value, id)
32178 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32180 cp_lexer_consume_token (lexer); /* id */
32181 cp_lexer_consume_token (lexer); /* ':' */
32184 /* Now collect the actual argument. */
32185 if (ops[idx] != NULL_TREE)
32187 cp_parser_error (parser, "unexpected argument");
32188 goto cleanup_error;
32191 tree expr = cp_parser_assignment_expression (parser, NULL, false,
32192 false);
32193 if (expr == error_mark_node)
32194 goto cleanup_error;
32196 mark_exp_read (expr);
32197 ops[idx] = expr;
32199 if (kind == OMP_CLAUSE_GANG
32200 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32202 cp_lexer_consume_token (lexer);
32203 continue;
32205 break;
32207 while (1);
32209 if (!parens.require_close (parser))
32210 goto cleanup_error;
32213 check_no_duplicate_clause (list, kind, str, loc);
32215 c = build_omp_clause (loc, kind);
32217 if (ops[1])
32218 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32220 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32221 OMP_CLAUSE_CHAIN (c) = list;
32223 return c;
32225 cleanup_error:
32226 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32227 return list;
32230 /* OpenACC 2.0:
32231 tile ( size-expr-list ) */
32233 static tree
32234 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32236 tree c, expr = error_mark_node;
32237 tree tile = NULL_TREE;
32239 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32240 so, but the spec authors never considered such a case and have
32241 differing opinions on what it might mean, including 'not
32242 allowed'.) */
32243 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32244 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32245 clause_loc);
32247 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32248 return list;
32252 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32253 return list;
32255 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32256 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32257 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32259 cp_lexer_consume_token (parser->lexer);
32260 expr = integer_zero_node;
32262 else
32263 expr = cp_parser_constant_expression (parser);
32265 tile = tree_cons (NULL_TREE, expr, tile);
32267 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32269 /* Consume the trailing ')'. */
32270 cp_lexer_consume_token (parser->lexer);
32272 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32273 tile = nreverse (tile);
32274 OMP_CLAUSE_TILE_LIST (c) = tile;
32275 OMP_CLAUSE_CHAIN (c) = list;
32276 return c;
32279 /* OpenACC 2.0
32280 Parse wait clause or directive parameters. */
32282 static tree
32283 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32285 vec<tree, va_gc> *args;
32286 tree t, args_tree;
32288 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32289 /*cast_p=*/false,
32290 /*allow_expansion_p=*/true,
32291 /*non_constant_p=*/NULL);
32293 if (args == NULL || args->length () == 0)
32295 cp_parser_error (parser, "expected integer expression before ')'");
32296 if (args != NULL)
32297 release_tree_vector (args);
32298 return list;
32301 args_tree = build_tree_list_vec (args);
32303 release_tree_vector (args);
32305 for (t = args_tree; t; t = TREE_CHAIN (t))
32307 tree targ = TREE_VALUE (t);
32309 if (targ != error_mark_node)
32311 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32312 error ("%<wait%> expression must be integral");
32313 else
32315 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32317 targ = mark_rvalue_use (targ);
32318 OMP_CLAUSE_DECL (c) = targ;
32319 OMP_CLAUSE_CHAIN (c) = list;
32320 list = c;
32325 return list;
32328 /* OpenACC:
32329 wait ( int-expr-list ) */
32331 static tree
32332 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32334 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32336 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32337 return list;
32339 list = cp_parser_oacc_wait_list (parser, location, list);
32341 return list;
32344 /* OpenMP 3.0:
32345 collapse ( constant-expression ) */
32347 static tree
32348 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32350 tree c, num;
32351 location_t loc;
32352 HOST_WIDE_INT n;
32354 loc = cp_lexer_peek_token (parser->lexer)->location;
32355 matching_parens parens;
32356 if (!parens.require_open (parser))
32357 return list;
32359 num = cp_parser_constant_expression (parser);
32361 if (!parens.require_close (parser))
32362 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32363 /*or_comma=*/false,
32364 /*consume_paren=*/true);
32366 if (num == error_mark_node)
32367 return list;
32368 num = fold_non_dependent_expr (num);
32369 if (!tree_fits_shwi_p (num)
32370 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32371 || (n = tree_to_shwi (num)) <= 0
32372 || (int) n != n)
32374 error_at (loc, "collapse argument needs positive constant integer expression");
32375 return list;
32378 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32379 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32380 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32381 OMP_CLAUSE_CHAIN (c) = list;
32382 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32384 return c;
32387 /* OpenMP 2.5:
32388 default ( none | shared )
32390 OpenACC:
32391 default ( none | present ) */
32393 static tree
32394 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32395 location_t location, bool is_oacc)
32397 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32398 tree c;
32400 matching_parens parens;
32401 if (!parens.require_open (parser))
32402 return list;
32403 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32405 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32406 const char *p = IDENTIFIER_POINTER (id);
32408 switch (p[0])
32410 case 'n':
32411 if (strcmp ("none", p) != 0)
32412 goto invalid_kind;
32413 kind = OMP_CLAUSE_DEFAULT_NONE;
32414 break;
32416 case 'p':
32417 if (strcmp ("present", p) != 0 || !is_oacc)
32418 goto invalid_kind;
32419 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32420 break;
32422 case 's':
32423 if (strcmp ("shared", p) != 0 || is_oacc)
32424 goto invalid_kind;
32425 kind = OMP_CLAUSE_DEFAULT_SHARED;
32426 break;
32428 default:
32429 goto invalid_kind;
32432 cp_lexer_consume_token (parser->lexer);
32434 else
32436 invalid_kind:
32437 if (is_oacc)
32438 cp_parser_error (parser, "expected %<none%> or %<present%>");
32439 else
32440 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32443 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32444 || !parens.require_close (parser))
32445 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32446 /*or_comma=*/false,
32447 /*consume_paren=*/true);
32449 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32450 return list;
32452 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32453 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32454 OMP_CLAUSE_CHAIN (c) = list;
32455 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32457 return c;
32460 /* OpenMP 3.1:
32461 final ( expression ) */
32463 static tree
32464 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32466 tree t, c;
32468 matching_parens parens;
32469 if (!parens.require_open (parser))
32470 return list;
32472 t = cp_parser_assignment_expression (parser);
32474 if (t == error_mark_node
32475 || !parens.require_close (parser))
32476 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32477 /*or_comma=*/false,
32478 /*consume_paren=*/true);
32480 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32482 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32483 OMP_CLAUSE_FINAL_EXPR (c) = t;
32484 OMP_CLAUSE_CHAIN (c) = list;
32486 return c;
32489 /* OpenMP 2.5:
32490 if ( expression )
32492 OpenMP 4.5:
32493 if ( directive-name-modifier : expression )
32495 directive-name-modifier:
32496 parallel | task | taskloop | target data | target | target update
32497 | target enter data | target exit data
32499 OpenMP 5.0:
32500 directive-name-modifier:
32501 ... | simd | cancel */
32503 static tree
32504 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32505 bool is_omp)
32507 tree t, c;
32508 enum tree_code if_modifier = ERROR_MARK;
32510 matching_parens parens;
32511 if (!parens.require_open (parser))
32512 return list;
32514 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32516 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32517 const char *p = IDENTIFIER_POINTER (id);
32518 int n = 2;
32520 if (strcmp ("cancel", p) == 0)
32521 if_modifier = VOID_CST;
32522 else if (strcmp ("parallel", p) == 0)
32523 if_modifier = OMP_PARALLEL;
32524 else if (strcmp ("simd", p) == 0)
32525 if_modifier = OMP_SIMD;
32526 else if (strcmp ("task", p) == 0)
32527 if_modifier = OMP_TASK;
32528 else if (strcmp ("taskloop", p) == 0)
32529 if_modifier = OMP_TASKLOOP;
32530 else if (strcmp ("target", p) == 0)
32532 if_modifier = OMP_TARGET;
32533 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32535 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32536 p = IDENTIFIER_POINTER (id);
32537 if (strcmp ("data", p) == 0)
32538 if_modifier = OMP_TARGET_DATA;
32539 else if (strcmp ("update", p) == 0)
32540 if_modifier = OMP_TARGET_UPDATE;
32541 else if (strcmp ("enter", p) == 0)
32542 if_modifier = OMP_TARGET_ENTER_DATA;
32543 else if (strcmp ("exit", p) == 0)
32544 if_modifier = OMP_TARGET_EXIT_DATA;
32545 if (if_modifier != OMP_TARGET)
32546 n = 3;
32547 else
32549 location_t loc
32550 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32551 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32552 "or %<exit%>");
32553 if_modifier = ERROR_MARK;
32555 if (if_modifier == OMP_TARGET_ENTER_DATA
32556 || if_modifier == OMP_TARGET_EXIT_DATA)
32558 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32560 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32561 p = IDENTIFIER_POINTER (id);
32562 if (strcmp ("data", p) == 0)
32563 n = 4;
32565 if (n != 4)
32567 location_t loc
32568 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32569 error_at (loc, "expected %<data%>");
32570 if_modifier = ERROR_MARK;
32575 if (if_modifier != ERROR_MARK)
32577 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32579 while (n-- > 0)
32580 cp_lexer_consume_token (parser->lexer);
32582 else
32584 if (n > 2)
32586 location_t loc
32587 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32588 error_at (loc, "expected %<:%>");
32590 if_modifier = ERROR_MARK;
32595 t = cp_parser_assignment_expression (parser);
32597 if (t == error_mark_node
32598 || !parens.require_close (parser))
32599 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32600 /*or_comma=*/false,
32601 /*consume_paren=*/true);
32603 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32604 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32606 if (if_modifier != ERROR_MARK
32607 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32609 const char *p = NULL;
32610 switch (if_modifier)
32612 case VOID_CST: p = "cancel"; break;
32613 case OMP_PARALLEL: p = "parallel"; break;
32614 case OMP_SIMD: p = "simd"; break;
32615 case OMP_TASK: p = "task"; break;
32616 case OMP_TASKLOOP: p = "taskloop"; break;
32617 case OMP_TARGET_DATA: p = "target data"; break;
32618 case OMP_TARGET: p = "target"; break;
32619 case OMP_TARGET_UPDATE: p = "target update"; break;
32620 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32621 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32622 default: gcc_unreachable ();
32624 error_at (location, "too many %<if%> clauses with %qs modifier",
32626 return list;
32628 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32630 if (!is_omp)
32631 error_at (location, "too many %<if%> clauses");
32632 else
32633 error_at (location, "too many %<if%> clauses without modifier");
32634 return list;
32636 else if (if_modifier == ERROR_MARK
32637 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32639 error_at (location, "if any %<if%> clause has modifier, then all "
32640 "%<if%> clauses have to use modifier");
32641 return list;
32645 c = build_omp_clause (location, OMP_CLAUSE_IF);
32646 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32647 OMP_CLAUSE_IF_EXPR (c) = t;
32648 OMP_CLAUSE_CHAIN (c) = list;
32650 return c;
32653 /* OpenMP 3.1:
32654 mergeable */
32656 static tree
32657 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32658 tree list, location_t location)
32660 tree c;
32662 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32663 location);
32665 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32666 OMP_CLAUSE_CHAIN (c) = list;
32667 return c;
32670 /* OpenMP 2.5:
32671 nowait */
32673 static tree
32674 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32675 tree list, location_t location)
32677 tree c;
32679 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32681 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32682 OMP_CLAUSE_CHAIN (c) = list;
32683 return c;
32686 /* OpenMP 2.5:
32687 num_threads ( expression ) */
32689 static tree
32690 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32691 location_t location)
32693 tree t, c;
32695 matching_parens parens;
32696 if (!parens.require_open (parser))
32697 return list;
32699 t = cp_parser_assignment_expression (parser);
32701 if (t == error_mark_node
32702 || !parens.require_close (parser))
32703 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32704 /*or_comma=*/false,
32705 /*consume_paren=*/true);
32707 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32708 "num_threads", location);
32710 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32711 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32712 OMP_CLAUSE_CHAIN (c) = list;
32714 return c;
32717 /* OpenMP 4.5:
32718 num_tasks ( expression ) */
32720 static tree
32721 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32722 location_t location)
32724 tree t, c;
32726 matching_parens parens;
32727 if (!parens.require_open (parser))
32728 return list;
32730 t = cp_parser_assignment_expression (parser);
32732 if (t == error_mark_node
32733 || !parens.require_close (parser))
32734 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32735 /*or_comma=*/false,
32736 /*consume_paren=*/true);
32738 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32739 "num_tasks", location);
32741 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32742 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32743 OMP_CLAUSE_CHAIN (c) = list;
32745 return c;
32748 /* OpenMP 4.5:
32749 grainsize ( expression ) */
32751 static tree
32752 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32753 location_t location)
32755 tree t, c;
32757 matching_parens parens;
32758 if (!parens.require_open (parser))
32759 return list;
32761 t = cp_parser_assignment_expression (parser);
32763 if (t == error_mark_node
32764 || !parens.require_close (parser))
32765 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32766 /*or_comma=*/false,
32767 /*consume_paren=*/true);
32769 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32770 "grainsize", location);
32772 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32773 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32774 OMP_CLAUSE_CHAIN (c) = list;
32776 return c;
32779 /* OpenMP 4.5:
32780 priority ( expression ) */
32782 static tree
32783 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32784 location_t location)
32786 tree t, c;
32788 matching_parens parens;
32789 if (!parens.require_open (parser))
32790 return list;
32792 t = cp_parser_assignment_expression (parser);
32794 if (t == error_mark_node
32795 || !parens.require_close (parser))
32796 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32797 /*or_comma=*/false,
32798 /*consume_paren=*/true);
32800 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32801 "priority", location);
32803 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32804 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32805 OMP_CLAUSE_CHAIN (c) = list;
32807 return c;
32810 /* OpenMP 4.5:
32811 hint ( expression ) */
32813 static tree
32814 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
32816 tree t, c;
32818 matching_parens parens;
32819 if (!parens.require_open (parser))
32820 return list;
32822 t = cp_parser_assignment_expression (parser);
32824 if (t == error_mark_node
32825 || !parens.require_close (parser))
32826 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32827 /*or_comma=*/false,
32828 /*consume_paren=*/true);
32830 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32832 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32833 OMP_CLAUSE_HINT_EXPR (c) = t;
32834 OMP_CLAUSE_CHAIN (c) = list;
32836 return c;
32839 /* OpenMP 4.5:
32840 defaultmap ( tofrom : scalar )
32842 OpenMP 5.0:
32843 defaultmap ( implicit-behavior [ : variable-category ] ) */
32845 static tree
32846 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32847 location_t location)
32849 tree c, id;
32850 const char *p;
32851 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
32852 enum omp_clause_defaultmap_kind category
32853 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
32855 matching_parens parens;
32856 if (!parens.require_open (parser))
32857 return list;
32859 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
32860 p = "default";
32861 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32863 invalid_behavior:
32864 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
32865 "%<tofrom%>, %<firstprivate%>, %<none%> "
32866 "or %<default%>");
32867 goto out_err;
32869 else
32871 id = cp_lexer_peek_token (parser->lexer)->u.value;
32872 p = IDENTIFIER_POINTER (id);
32875 switch (p[0])
32877 case 'a':
32878 if (strcmp ("alloc", p) == 0)
32879 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
32880 else
32881 goto invalid_behavior;
32882 break;
32884 case 'd':
32885 if (strcmp ("default", p) == 0)
32886 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
32887 else
32888 goto invalid_behavior;
32889 break;
32891 case 'f':
32892 if (strcmp ("firstprivate", p) == 0)
32893 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
32894 else if (strcmp ("from", p) == 0)
32895 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
32896 else
32897 goto invalid_behavior;
32898 break;
32900 case 'n':
32901 if (strcmp ("none", p) == 0)
32902 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
32903 else
32904 goto invalid_behavior;
32905 break;
32907 case 't':
32908 if (strcmp ("tofrom", p) == 0)
32909 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
32910 else if (strcmp ("to", p) == 0)
32911 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
32912 else
32913 goto invalid_behavior;
32914 break;
32916 default:
32917 goto invalid_behavior;
32919 cp_lexer_consume_token (parser->lexer);
32921 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32923 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32924 goto out_err;
32926 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32928 invalid_category:
32929 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
32930 "%<pointer%>");
32931 goto out_err;
32933 id = cp_lexer_peek_token (parser->lexer)->u.value;
32934 p = IDENTIFIER_POINTER (id);
32936 switch (p[0])
32938 case 'a':
32939 if (strcmp ("aggregate", p) == 0)
32940 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
32941 else
32942 goto invalid_category;
32943 break;
32945 case 'p':
32946 if (strcmp ("pointer", p) == 0)
32947 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
32948 else
32949 goto invalid_category;
32950 break;
32952 case 's':
32953 if (strcmp ("scalar", p) == 0)
32954 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
32955 else
32956 goto invalid_category;
32957 break;
32959 default:
32960 goto invalid_category;
32963 cp_lexer_consume_token (parser->lexer);
32965 if (!parens.require_close (parser))
32966 goto out_err;
32968 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32969 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
32970 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
32971 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
32972 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
32973 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
32975 enum omp_clause_defaultmap_kind cat = category;
32976 location_t loc = OMP_CLAUSE_LOCATION (c);
32977 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
32978 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
32979 p = NULL;
32980 switch (cat)
32982 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
32983 p = NULL;
32984 break;
32985 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
32986 p = "aggregate";
32987 break;
32988 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
32989 p = "pointer";
32990 break;
32991 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
32992 p = "scalar";
32993 break;
32994 default:
32995 gcc_unreachable ();
32997 if (p)
32998 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33000 else
33001 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33002 "category");
33003 break;
33006 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33007 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33008 OMP_CLAUSE_CHAIN (c) = list;
33009 return c;
33011 out_err:
33012 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33013 /*or_comma=*/false,
33014 /*consume_paren=*/true);
33015 return list;
33018 /* OpenMP 2.5:
33019 ordered
33021 OpenMP 4.5:
33022 ordered ( constant-expression ) */
33024 static tree
33025 cp_parser_omp_clause_ordered (cp_parser *parser,
33026 tree list, location_t location)
33028 tree c, num = NULL_TREE;
33029 HOST_WIDE_INT n;
33031 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33032 "ordered", location);
33034 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33036 matching_parens parens;
33037 parens.consume_open (parser);
33039 num = cp_parser_constant_expression (parser);
33041 if (!parens.require_close (parser))
33042 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33043 /*or_comma=*/false,
33044 /*consume_paren=*/true);
33046 if (num == error_mark_node)
33047 return list;
33048 num = fold_non_dependent_expr (num);
33049 if (!tree_fits_shwi_p (num)
33050 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33051 || (n = tree_to_shwi (num)) <= 0
33052 || (int) n != n)
33054 error_at (location,
33055 "ordered argument needs positive constant integer "
33056 "expression");
33057 return list;
33061 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
33062 OMP_CLAUSE_ORDERED_EXPR (c) = num;
33063 OMP_CLAUSE_CHAIN (c) = list;
33064 return c;
33067 /* OpenMP 2.5:
33068 reduction ( reduction-operator : variable-list )
33070 reduction-operator:
33071 One of: + * - & ^ | && ||
33073 OpenMP 3.1:
33075 reduction-operator:
33076 One of: + * - & ^ | && || min max
33078 OpenMP 4.0:
33080 reduction-operator:
33081 One of: + * - & ^ | && ||
33082 id-expression
33084 OpenMP 5.0:
33085 reduction ( reduction-modifier, reduction-operator : variable-list )
33086 in_reduction ( reduction-operator : variable-list )
33087 task_reduction ( reduction-operator : variable-list ) */
33089 static tree
33090 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
33091 bool is_omp, tree list)
33093 enum tree_code code = ERROR_MARK;
33094 tree nlist, c, id = NULL_TREE;
33095 bool task = false;
33096 bool inscan = false;
33098 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33099 return list;
33101 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
33103 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
33104 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33106 cp_lexer_consume_token (parser->lexer);
33107 cp_lexer_consume_token (parser->lexer);
33109 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33110 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33112 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33113 const char *p = IDENTIFIER_POINTER (id);
33114 if (strcmp (p, "task") == 0)
33115 task = true;
33116 else if (strcmp (p, "inscan") == 0)
33118 inscan = true;
33119 sorry ("%<inscan%> modifier on %<reduction%> clause "
33120 "not supported yet");
33122 if (task || inscan)
33124 cp_lexer_consume_token (parser->lexer);
33125 cp_lexer_consume_token (parser->lexer);
33130 switch (cp_lexer_peek_token (parser->lexer)->type)
33132 case CPP_PLUS: code = PLUS_EXPR; break;
33133 case CPP_MULT: code = MULT_EXPR; break;
33134 case CPP_MINUS: code = MINUS_EXPR; break;
33135 case CPP_AND: code = BIT_AND_EXPR; break;
33136 case CPP_XOR: code = BIT_XOR_EXPR; break;
33137 case CPP_OR: code = BIT_IOR_EXPR; break;
33138 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
33139 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
33140 default: break;
33143 if (code != ERROR_MARK)
33144 cp_lexer_consume_token (parser->lexer);
33145 else
33147 bool saved_colon_corrects_to_scope_p;
33148 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33149 parser->colon_corrects_to_scope_p = false;
33150 id = cp_parser_id_expression (parser, /*template_p=*/false,
33151 /*check_dependency_p=*/true,
33152 /*template_p=*/NULL,
33153 /*declarator_p=*/false,
33154 /*optional_p=*/false);
33155 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33156 if (identifier_p (id))
33158 const char *p = IDENTIFIER_POINTER (id);
33160 if (strcmp (p, "min") == 0)
33161 code = MIN_EXPR;
33162 else if (strcmp (p, "max") == 0)
33163 code = MAX_EXPR;
33164 else if (id == ovl_op_identifier (false, PLUS_EXPR))
33165 code = PLUS_EXPR;
33166 else if (id == ovl_op_identifier (false, MULT_EXPR))
33167 code = MULT_EXPR;
33168 else if (id == ovl_op_identifier (false, MINUS_EXPR))
33169 code = MINUS_EXPR;
33170 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
33171 code = BIT_AND_EXPR;
33172 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
33173 code = BIT_IOR_EXPR;
33174 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
33175 code = BIT_XOR_EXPR;
33176 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
33177 code = TRUTH_ANDIF_EXPR;
33178 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
33179 code = TRUTH_ORIF_EXPR;
33180 id = omp_reduction_id (code, id, NULL_TREE);
33181 tree scope = parser->scope;
33182 if (scope)
33183 id = build_qualified_name (NULL_TREE, scope, id, false);
33184 parser->scope = NULL_TREE;
33185 parser->qualifying_scope = NULL_TREE;
33186 parser->object_scope = NULL_TREE;
33188 else
33190 error ("invalid reduction-identifier");
33191 resync_fail:
33192 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33193 /*or_comma=*/false,
33194 /*consume_paren=*/true);
33195 return list;
33199 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33200 goto resync_fail;
33202 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
33203 NULL);
33204 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33206 OMP_CLAUSE_REDUCTION_CODE (c) = code;
33207 if (task)
33208 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
33209 else if (inscan)
33210 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
33211 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
33214 return nlist;
33217 /* OpenMP 2.5:
33218 schedule ( schedule-kind )
33219 schedule ( schedule-kind , expression )
33221 schedule-kind:
33222 static | dynamic | guided | runtime | auto
33224 OpenMP 4.5:
33225 schedule ( schedule-modifier : schedule-kind )
33226 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33228 schedule-modifier:
33229 simd
33230 monotonic
33231 nonmonotonic */
33233 static tree
33234 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
33236 tree c, t;
33237 int modifiers = 0, nmodifiers = 0;
33239 matching_parens parens;
33240 if (!parens.require_open (parser))
33241 return list;
33243 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
33245 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33247 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33248 const char *p = IDENTIFIER_POINTER (id);
33249 if (strcmp ("simd", p) == 0)
33250 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
33251 else if (strcmp ("monotonic", p) == 0)
33252 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
33253 else if (strcmp ("nonmonotonic", p) == 0)
33254 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
33255 else
33256 break;
33257 cp_lexer_consume_token (parser->lexer);
33258 if (nmodifiers++ == 0
33259 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33260 cp_lexer_consume_token (parser->lexer);
33261 else
33263 cp_parser_require (parser, CPP_COLON, RT_COLON);
33264 break;
33268 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33270 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33271 const char *p = IDENTIFIER_POINTER (id);
33273 switch (p[0])
33275 case 'd':
33276 if (strcmp ("dynamic", p) != 0)
33277 goto invalid_kind;
33278 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
33279 break;
33281 case 'g':
33282 if (strcmp ("guided", p) != 0)
33283 goto invalid_kind;
33284 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
33285 break;
33287 case 'r':
33288 if (strcmp ("runtime", p) != 0)
33289 goto invalid_kind;
33290 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
33291 break;
33293 default:
33294 goto invalid_kind;
33297 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33298 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33299 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33300 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33301 else
33302 goto invalid_kind;
33303 cp_lexer_consume_token (parser->lexer);
33305 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33306 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33307 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33308 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33310 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33311 "specified");
33312 modifiers = 0;
33315 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33317 cp_token *token;
33318 cp_lexer_consume_token (parser->lexer);
33320 token = cp_lexer_peek_token (parser->lexer);
33321 t = cp_parser_assignment_expression (parser);
33323 if (t == error_mark_node)
33324 goto resync_fail;
33325 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
33326 error_at (token->location, "schedule %<runtime%> does not take "
33327 "a %<chunk_size%> parameter");
33328 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
33329 error_at (token->location, "schedule %<auto%> does not take "
33330 "a %<chunk_size%> parameter");
33331 else
33332 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
33334 if (!parens.require_close (parser))
33335 goto resync_fail;
33337 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33338 goto resync_fail;
33340 OMP_CLAUSE_SCHEDULE_KIND (c)
33341 = (enum omp_clause_schedule_kind)
33342 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
33344 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
33345 OMP_CLAUSE_CHAIN (c) = list;
33346 return c;
33348 invalid_kind:
33349 cp_parser_error (parser, "invalid schedule kind");
33350 resync_fail:
33351 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33352 /*or_comma=*/false,
33353 /*consume_paren=*/true);
33354 return list;
33357 /* OpenMP 3.0:
33358 untied */
33360 static tree
33361 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
33362 tree list, location_t location)
33364 tree c;
33366 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
33368 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
33369 OMP_CLAUSE_CHAIN (c) = list;
33370 return c;
33373 /* OpenMP 4.0:
33374 inbranch
33375 notinbranch */
33377 static tree
33378 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33379 tree list, location_t location)
33381 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33382 tree c = build_omp_clause (location, code);
33383 OMP_CLAUSE_CHAIN (c) = list;
33384 return c;
33387 /* OpenMP 4.0:
33388 parallel
33390 sections
33391 taskgroup */
33393 static tree
33394 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33395 enum omp_clause_code code,
33396 tree list, location_t location)
33398 tree c = build_omp_clause (location, code);
33399 OMP_CLAUSE_CHAIN (c) = list;
33400 return c;
33403 /* OpenMP 4.5:
33404 nogroup */
33406 static tree
33407 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33408 tree list, location_t location)
33410 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33411 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33412 OMP_CLAUSE_CHAIN (c) = list;
33413 return c;
33416 /* OpenMP 4.5:
33417 simd
33418 threads */
33420 static tree
33421 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33422 enum omp_clause_code code,
33423 tree list, location_t location)
33425 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33426 tree c = build_omp_clause (location, code);
33427 OMP_CLAUSE_CHAIN (c) = list;
33428 return c;
33431 /* OpenMP 4.0:
33432 num_teams ( expression ) */
33434 static tree
33435 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33436 location_t location)
33438 tree t, c;
33440 matching_parens parens;
33441 if (!parens.require_open (parser))
33442 return list;
33444 t = cp_parser_assignment_expression (parser);
33446 if (t == error_mark_node
33447 || !parens.require_close (parser))
33448 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33449 /*or_comma=*/false,
33450 /*consume_paren=*/true);
33452 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33453 "num_teams", location);
33455 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33456 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33457 OMP_CLAUSE_CHAIN (c) = list;
33459 return c;
33462 /* OpenMP 4.0:
33463 thread_limit ( expression ) */
33465 static tree
33466 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33467 location_t location)
33469 tree t, c;
33471 matching_parens parens;
33472 if (!parens.require_open (parser))
33473 return list;
33475 t = cp_parser_assignment_expression (parser);
33477 if (t == error_mark_node
33478 || !parens.require_close (parser))
33479 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33480 /*or_comma=*/false,
33481 /*consume_paren=*/true);
33483 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33484 "thread_limit", location);
33486 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33487 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33488 OMP_CLAUSE_CHAIN (c) = list;
33490 return c;
33493 /* OpenMP 4.0:
33494 aligned ( variable-list )
33495 aligned ( variable-list : constant-expression ) */
33497 static tree
33498 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33500 tree nlist, c, alignment = NULL_TREE;
33501 bool colon;
33503 matching_parens parens;
33504 if (!parens.require_open (parser))
33505 return list;
33507 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33508 &colon);
33510 if (colon)
33512 alignment = cp_parser_constant_expression (parser);
33514 if (!parens.require_close (parser))
33515 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33516 /*or_comma=*/false,
33517 /*consume_paren=*/true);
33519 if (alignment == error_mark_node)
33520 alignment = NULL_TREE;
33523 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33524 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33526 return nlist;
33529 /* OpenMP 2.5:
33530 lastprivate ( variable-list )
33532 OpenMP 5.0:
33533 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
33535 static tree
33536 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
33538 bool conditional = false;
33540 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33541 return list;
33543 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33544 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
33546 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33547 const char *p = IDENTIFIER_POINTER (id);
33549 if (strcmp ("conditional", p) == 0)
33551 conditional = true;
33552 cp_lexer_consume_token (parser->lexer);
33553 cp_lexer_consume_token (parser->lexer);
33557 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
33558 list, NULL);
33560 if (conditional)
33561 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33562 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
33563 return nlist;
33566 /* OpenMP 4.0:
33567 linear ( variable-list )
33568 linear ( variable-list : expression )
33570 OpenMP 4.5:
33571 linear ( modifier ( variable-list ) )
33572 linear ( modifier ( variable-list ) : expression ) */
33574 static tree
33575 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33576 bool declare_simd)
33578 tree nlist, c, step = integer_one_node;
33579 bool colon;
33580 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33582 matching_parens parens;
33583 if (!parens.require_open (parser))
33584 return list;
33586 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33588 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33589 const char *p = IDENTIFIER_POINTER (id);
33591 if (strcmp ("ref", p) == 0)
33592 kind = OMP_CLAUSE_LINEAR_REF;
33593 else if (strcmp ("val", p) == 0)
33594 kind = OMP_CLAUSE_LINEAR_VAL;
33595 else if (strcmp ("uval", p) == 0)
33596 kind = OMP_CLAUSE_LINEAR_UVAL;
33597 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33598 cp_lexer_consume_token (parser->lexer);
33599 else
33600 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33603 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33604 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33605 &colon);
33606 else
33608 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33609 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33610 if (colon)
33611 cp_parser_require (parser, CPP_COLON, RT_COLON);
33612 else if (!parens.require_close (parser))
33613 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33614 /*or_comma=*/false,
33615 /*consume_paren=*/true);
33618 if (colon)
33620 step = NULL_TREE;
33621 if (declare_simd
33622 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33623 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33625 cp_token *token = cp_lexer_peek_token (parser->lexer);
33626 cp_parser_parse_tentatively (parser);
33627 step = cp_parser_id_expression (parser, /*template_p=*/false,
33628 /*check_dependency_p=*/true,
33629 /*template_p=*/NULL,
33630 /*declarator_p=*/false,
33631 /*optional_p=*/false);
33632 if (step != error_mark_node)
33633 step = cp_parser_lookup_name_simple (parser, step, token->location);
33634 if (step == error_mark_node)
33636 step = NULL_TREE;
33637 cp_parser_abort_tentative_parse (parser);
33639 else if (!cp_parser_parse_definitely (parser))
33640 step = NULL_TREE;
33642 if (!step)
33643 step = cp_parser_assignment_expression (parser);
33645 if (!parens.require_close (parser))
33646 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33647 /*or_comma=*/false,
33648 /*consume_paren=*/true);
33650 if (step == error_mark_node)
33651 return list;
33654 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33656 OMP_CLAUSE_LINEAR_STEP (c) = step;
33657 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33660 return nlist;
33663 /* OpenMP 4.0:
33664 safelen ( constant-expression ) */
33666 static tree
33667 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33668 location_t location)
33670 tree t, c;
33672 matching_parens parens;
33673 if (!parens.require_open (parser))
33674 return list;
33676 t = cp_parser_constant_expression (parser);
33678 if (t == error_mark_node
33679 || !parens.require_close (parser))
33680 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33681 /*or_comma=*/false,
33682 /*consume_paren=*/true);
33684 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33686 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33687 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33688 OMP_CLAUSE_CHAIN (c) = list;
33690 return c;
33693 /* OpenMP 4.0:
33694 simdlen ( constant-expression ) */
33696 static tree
33697 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33698 location_t location)
33700 tree t, c;
33702 matching_parens parens;
33703 if (!parens.require_open (parser))
33704 return list;
33706 t = cp_parser_constant_expression (parser);
33708 if (t == error_mark_node
33709 || !parens.require_close (parser))
33710 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33711 /*or_comma=*/false,
33712 /*consume_paren=*/true);
33714 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33716 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33717 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33718 OMP_CLAUSE_CHAIN (c) = list;
33720 return c;
33723 /* OpenMP 4.5:
33724 vec:
33725 identifier [+/- integer]
33726 vec , identifier [+/- integer]
33729 static tree
33730 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33731 tree list)
33733 tree vec = NULL;
33735 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33737 cp_parser_error (parser, "expected identifier");
33738 return list;
33741 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33743 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33744 tree t, identifier = cp_parser_identifier (parser);
33745 tree addend = NULL;
33747 if (identifier == error_mark_node)
33748 t = error_mark_node;
33749 else
33751 t = cp_parser_lookup_name_simple
33752 (parser, identifier,
33753 cp_lexer_peek_token (parser->lexer)->location);
33754 if (t == error_mark_node)
33755 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33756 id_loc);
33759 bool neg = false;
33760 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33761 neg = true;
33762 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33764 addend = integer_zero_node;
33765 goto add_to_vector;
33767 cp_lexer_consume_token (parser->lexer);
33769 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33771 cp_parser_error (parser, "expected integer");
33772 return list;
33775 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33776 if (TREE_CODE (addend) != INTEGER_CST)
33778 cp_parser_error (parser, "expected integer");
33779 return list;
33781 cp_lexer_consume_token (parser->lexer);
33783 add_to_vector:
33784 if (t != error_mark_node)
33786 vec = tree_cons (addend, t, vec);
33787 if (neg)
33788 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33791 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33792 break;
33794 cp_lexer_consume_token (parser->lexer);
33797 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33799 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33800 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33801 OMP_CLAUSE_DECL (u) = nreverse (vec);
33802 OMP_CLAUSE_CHAIN (u) = list;
33803 return u;
33805 return list;
33808 /* OpenMP 5.0:
33809 iterators ( iterators-definition )
33811 iterators-definition:
33812 iterator-specifier
33813 iterator-specifier , iterators-definition
33815 iterator-specifier:
33816 identifier = range-specification
33817 iterator-type identifier = range-specification
33819 range-specification:
33820 begin : end
33821 begin : end : step */
33823 static tree
33824 cp_parser_omp_iterators (cp_parser *parser)
33826 tree ret = NULL_TREE, *last = &ret;
33827 cp_lexer_consume_token (parser->lexer);
33829 matching_parens parens;
33830 if (!parens.require_open (parser))
33831 return error_mark_node;
33833 bool saved_colon_corrects_to_scope_p
33834 = parser->colon_corrects_to_scope_p;
33835 bool saved_colon_doesnt_start_class_def_p
33836 = parser->colon_doesnt_start_class_def_p;
33840 tree iter_type;
33841 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33842 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
33843 iter_type = integer_type_node;
33844 else
33846 const char *saved_message
33847 = parser->type_definition_forbidden_message;
33848 parser->type_definition_forbidden_message
33849 = G_("types may not be defined in iterator type");
33851 iter_type = cp_parser_type_id (parser);
33853 parser->type_definition_forbidden_message = saved_message;
33856 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33857 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33859 cp_parser_error (parser, "expected identifier");
33860 break;
33863 tree id = cp_parser_identifier (parser);
33864 if (id == error_mark_node)
33865 break;
33867 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33868 break;
33870 parser->colon_corrects_to_scope_p = false;
33871 parser->colon_doesnt_start_class_def_p = true;
33872 tree begin = cp_parser_assignment_expression (parser);
33874 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33875 break;
33877 tree end = cp_parser_assignment_expression (parser);
33879 tree step = integer_one_node;
33880 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
33882 cp_lexer_consume_token (parser->lexer);
33883 step = cp_parser_assignment_expression (parser);
33886 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
33887 DECL_ARTIFICIAL (iter_var) = 1;
33888 DECL_CONTEXT (iter_var) = current_function_decl;
33889 pushdecl (iter_var);
33891 *last = make_tree_vec (6);
33892 TREE_VEC_ELT (*last, 0) = iter_var;
33893 TREE_VEC_ELT (*last, 1) = begin;
33894 TREE_VEC_ELT (*last, 2) = end;
33895 TREE_VEC_ELT (*last, 3) = step;
33896 last = &TREE_CHAIN (*last);
33898 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33900 cp_lexer_consume_token (parser->lexer);
33901 continue;
33903 break;
33905 while (1);
33907 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33908 parser->colon_doesnt_start_class_def_p
33909 = saved_colon_doesnt_start_class_def_p;
33911 if (!parens.require_close (parser))
33912 cp_parser_skip_to_closing_parenthesis (parser,
33913 /*recovering=*/true,
33914 /*or_comma=*/false,
33915 /*consume_paren=*/true);
33917 return ret ? ret : error_mark_node;
33920 /* OpenMP 4.0:
33921 depend ( depend-kind : variable-list )
33923 depend-kind:
33924 in | out | inout
33926 OpenMP 4.5:
33927 depend ( source )
33929 depend ( sink : vec )
33931 OpenMP 5.0:
33932 depend ( depend-modifier , depend-kind: variable-list )
33934 depend-kind:
33935 in | out | inout | mutexinoutset | depobj
33937 depend-modifier:
33938 iterator ( iterators-definition ) */
33940 static tree
33941 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33943 tree nlist, c, iterators = NULL_TREE;
33944 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
33946 matching_parens parens;
33947 if (!parens.require_open (parser))
33948 return list;
33952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33953 goto invalid_kind;
33955 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33956 const char *p = IDENTIFIER_POINTER (id);
33958 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
33960 begin_scope (sk_omp, NULL);
33961 iterators = cp_parser_omp_iterators (parser);
33962 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
33963 continue;
33965 if (strcmp ("in", p) == 0)
33966 kind = OMP_CLAUSE_DEPEND_IN;
33967 else if (strcmp ("inout", p) == 0)
33968 kind = OMP_CLAUSE_DEPEND_INOUT;
33969 else if (strcmp ("mutexinoutset", p) == 0)
33970 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
33971 else if (strcmp ("out", p) == 0)
33972 kind = OMP_CLAUSE_DEPEND_OUT;
33973 else if (strcmp ("depobj", p) == 0)
33974 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
33975 else if (strcmp ("sink", p) == 0)
33976 kind = OMP_CLAUSE_DEPEND_SINK;
33977 else if (strcmp ("source", p) == 0)
33978 kind = OMP_CLAUSE_DEPEND_SOURCE;
33979 else
33980 goto invalid_kind;
33981 break;
33983 while (1);
33985 cp_lexer_consume_token (parser->lexer);
33987 if (iterators
33988 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
33990 poplevel (0, 1, 0);
33991 error_at (loc, "%<iterator%> modifier incompatible with %qs",
33992 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
33993 iterators = NULL_TREE;
33996 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33998 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33999 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34000 OMP_CLAUSE_DECL (c) = NULL_TREE;
34001 OMP_CLAUSE_CHAIN (c) = list;
34002 if (!parens.require_close (parser))
34003 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34004 /*or_comma=*/false,
34005 /*consume_paren=*/true);
34006 return c;
34009 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34010 goto resync_fail;
34012 if (kind == OMP_CLAUSE_DEPEND_SINK)
34013 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34014 else
34016 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34017 list, NULL);
34019 if (iterators)
34021 tree block = poplevel (1, 1, 0);
34022 if (iterators == error_mark_node)
34023 iterators = NULL_TREE;
34024 else
34025 TREE_VEC_ELT (iterators, 5) = block;
34028 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34030 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34031 if (iterators)
34032 OMP_CLAUSE_DECL (c)
34033 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34036 return nlist;
34038 invalid_kind:
34039 cp_parser_error (parser, "invalid depend kind");
34040 resync_fail:
34041 if (iterators)
34042 poplevel (0, 1, 0);
34043 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34044 /*or_comma=*/false,
34045 /*consume_paren=*/true);
34046 return list;
34049 /* OpenMP 4.0:
34050 map ( map-kind : variable-list )
34051 map ( variable-list )
34053 map-kind:
34054 alloc | to | from | tofrom
34056 OpenMP 4.5:
34057 map-kind:
34058 alloc | to | from | tofrom | release | delete
34060 map ( always [,] map-kind: variable-list ) */
34062 static tree
34063 cp_parser_omp_clause_map (cp_parser *parser, tree list)
34065 tree nlist, c;
34066 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
34067 bool always = false;
34069 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34070 return list;
34072 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34074 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34075 const char *p = IDENTIFIER_POINTER (id);
34077 if (strcmp ("always", p) == 0)
34079 int nth = 2;
34080 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
34081 nth++;
34082 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
34083 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
34084 == RID_DELETE))
34085 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
34086 == CPP_COLON))
34088 always = true;
34089 cp_lexer_consume_token (parser->lexer);
34090 if (nth == 3)
34091 cp_lexer_consume_token (parser->lexer);
34096 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34097 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34099 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34100 const char *p = IDENTIFIER_POINTER (id);
34102 if (strcmp ("alloc", p) == 0)
34103 kind = GOMP_MAP_ALLOC;
34104 else if (strcmp ("to", p) == 0)
34105 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
34106 else if (strcmp ("from", p) == 0)
34107 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
34108 else if (strcmp ("tofrom", p) == 0)
34109 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
34110 else if (strcmp ("release", p) == 0)
34111 kind = GOMP_MAP_RELEASE;
34112 else
34114 cp_parser_error (parser, "invalid map kind");
34115 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34116 /*or_comma=*/false,
34117 /*consume_paren=*/true);
34118 return list;
34120 cp_lexer_consume_token (parser->lexer);
34121 cp_lexer_consume_token (parser->lexer);
34123 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
34124 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34126 kind = GOMP_MAP_DELETE;
34127 cp_lexer_consume_token (parser->lexer);
34128 cp_lexer_consume_token (parser->lexer);
34131 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
34132 NULL);
34134 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34135 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34137 return nlist;
34140 /* OpenMP 4.0:
34141 device ( expression ) */
34143 static tree
34144 cp_parser_omp_clause_device (cp_parser *parser, tree list,
34145 location_t location)
34147 tree t, c;
34149 matching_parens parens;
34150 if (!parens.require_open (parser))
34151 return list;
34153 t = cp_parser_assignment_expression (parser);
34155 if (t == error_mark_node
34156 || !parens.require_close (parser))
34157 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34158 /*or_comma=*/false,
34159 /*consume_paren=*/true);
34161 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
34162 "device", location);
34164 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
34165 OMP_CLAUSE_DEVICE_ID (c) = t;
34166 OMP_CLAUSE_CHAIN (c) = list;
34168 return c;
34171 /* OpenMP 4.0:
34172 dist_schedule ( static )
34173 dist_schedule ( static , expression ) */
34175 static tree
34176 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
34177 location_t location)
34179 tree c, t;
34181 matching_parens parens;
34182 if (!parens.require_open (parser))
34183 return list;
34185 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
34187 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34188 goto invalid_kind;
34189 cp_lexer_consume_token (parser->lexer);
34191 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34193 cp_lexer_consume_token (parser->lexer);
34195 t = cp_parser_assignment_expression (parser);
34197 if (t == error_mark_node)
34198 goto resync_fail;
34199 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
34201 if (!parens.require_close (parser))
34202 goto resync_fail;
34204 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34205 goto resync_fail;
34207 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
34208 location);
34209 OMP_CLAUSE_CHAIN (c) = list;
34210 return c;
34212 invalid_kind:
34213 cp_parser_error (parser, "invalid dist_schedule kind");
34214 resync_fail:
34215 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34216 /*or_comma=*/false,
34217 /*consume_paren=*/true);
34218 return list;
34221 /* OpenMP 4.0:
34222 proc_bind ( proc-bind-kind )
34224 proc-bind-kind:
34225 master | close | spread */
34227 static tree
34228 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
34229 location_t location)
34231 tree c;
34232 enum omp_clause_proc_bind_kind kind;
34234 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34235 return list;
34237 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34239 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34240 const char *p = IDENTIFIER_POINTER (id);
34242 if (strcmp ("master", p) == 0)
34243 kind = OMP_CLAUSE_PROC_BIND_MASTER;
34244 else if (strcmp ("close", p) == 0)
34245 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
34246 else if (strcmp ("spread", p) == 0)
34247 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
34248 else
34249 goto invalid_kind;
34251 else
34252 goto invalid_kind;
34254 cp_lexer_consume_token (parser->lexer);
34255 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34256 goto resync_fail;
34258 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
34259 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
34260 location);
34261 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
34262 OMP_CLAUSE_CHAIN (c) = list;
34263 return c;
34265 invalid_kind:
34266 cp_parser_error (parser, "invalid depend kind");
34267 resync_fail:
34268 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34269 /*or_comma=*/false,
34270 /*consume_paren=*/true);
34271 return list;
34274 /* OpenACC:
34275 async [( int-expr )] */
34277 static tree
34278 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
34280 tree c, t;
34281 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34283 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34285 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34287 matching_parens parens;
34288 parens.consume_open (parser);
34290 t = cp_parser_expression (parser);
34291 if (t == error_mark_node
34292 || !parens.require_close (parser))
34293 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34294 /*or_comma=*/false,
34295 /*consume_paren=*/true);
34298 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
34300 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
34301 OMP_CLAUSE_ASYNC_EXPR (c) = t;
34302 OMP_CLAUSE_CHAIN (c) = list;
34303 list = c;
34305 return list;
34308 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34309 is a bitmask in MASK. Return the list of clauses found. */
34311 static tree
34312 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
34313 const char *where, cp_token *pragma_tok,
34314 bool finish_p = true)
34316 tree clauses = NULL;
34317 bool first = true;
34319 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34321 location_t here;
34322 pragma_omp_clause c_kind;
34323 omp_clause_code code;
34324 const char *c_name;
34325 tree prev = clauses;
34327 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34328 cp_lexer_consume_token (parser->lexer);
34330 here = cp_lexer_peek_token (parser->lexer)->location;
34331 c_kind = cp_parser_omp_clause_name (parser);
34333 switch (c_kind)
34335 case PRAGMA_OACC_CLAUSE_ASYNC:
34336 clauses = cp_parser_oacc_clause_async (parser, clauses);
34337 c_name = "async";
34338 break;
34339 case PRAGMA_OACC_CLAUSE_AUTO:
34340 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
34341 clauses, here);
34342 c_name = "auto";
34343 break;
34344 case PRAGMA_OACC_CLAUSE_COLLAPSE:
34345 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
34346 c_name = "collapse";
34347 break;
34348 case PRAGMA_OACC_CLAUSE_COPY:
34349 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34350 c_name = "copy";
34351 break;
34352 case PRAGMA_OACC_CLAUSE_COPYIN:
34353 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34354 c_name = "copyin";
34355 break;
34356 case PRAGMA_OACC_CLAUSE_COPYOUT:
34357 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34358 c_name = "copyout";
34359 break;
34360 case PRAGMA_OACC_CLAUSE_CREATE:
34361 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34362 c_name = "create";
34363 break;
34364 case PRAGMA_OACC_CLAUSE_DELETE:
34365 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34366 c_name = "delete";
34367 break;
34368 case PRAGMA_OMP_CLAUSE_DEFAULT:
34369 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
34370 c_name = "default";
34371 break;
34372 case PRAGMA_OACC_CLAUSE_DEVICE:
34373 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34374 c_name = "device";
34375 break;
34376 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
34377 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
34378 c_name = "deviceptr";
34379 break;
34380 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
34381 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34382 c_name = "device_resident";
34383 break;
34384 case PRAGMA_OACC_CLAUSE_FINALIZE:
34385 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
34386 clauses, here);
34387 c_name = "finalize";
34388 break;
34389 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
34390 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34391 clauses);
34392 c_name = "firstprivate";
34393 break;
34394 case PRAGMA_OACC_CLAUSE_GANG:
34395 c_name = "gang";
34396 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
34397 c_name, clauses);
34398 break;
34399 case PRAGMA_OACC_CLAUSE_HOST:
34400 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34401 c_name = "host";
34402 break;
34403 case PRAGMA_OACC_CLAUSE_IF:
34404 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
34405 c_name = "if";
34406 break;
34407 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
34408 clauses = cp_parser_oacc_simple_clause (parser,
34409 OMP_CLAUSE_IF_PRESENT,
34410 clauses, here);
34411 c_name = "if_present";
34412 break;
34413 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
34414 clauses = cp_parser_oacc_simple_clause (parser,
34415 OMP_CLAUSE_INDEPENDENT,
34416 clauses, here);
34417 c_name = "independent";
34418 break;
34419 case PRAGMA_OACC_CLAUSE_LINK:
34420 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34421 c_name = "link";
34422 break;
34423 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
34424 code = OMP_CLAUSE_NUM_GANGS;
34425 c_name = "num_gangs";
34426 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34427 clauses);
34428 break;
34429 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
34430 c_name = "num_workers";
34431 code = OMP_CLAUSE_NUM_WORKERS;
34432 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34433 clauses);
34434 break;
34435 case PRAGMA_OACC_CLAUSE_PRESENT:
34436 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34437 c_name = "present";
34438 break;
34439 case PRAGMA_OACC_CLAUSE_PRIVATE:
34440 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34441 clauses);
34442 c_name = "private";
34443 break;
34444 case PRAGMA_OACC_CLAUSE_REDUCTION:
34445 clauses
34446 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34447 false, clauses);
34448 c_name = "reduction";
34449 break;
34450 case PRAGMA_OACC_CLAUSE_SEQ:
34451 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
34452 clauses, here);
34453 c_name = "seq";
34454 break;
34455 case PRAGMA_OACC_CLAUSE_TILE:
34456 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
34457 c_name = "tile";
34458 break;
34459 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
34460 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34461 clauses);
34462 c_name = "use_device";
34463 break;
34464 case PRAGMA_OACC_CLAUSE_VECTOR:
34465 c_name = "vector";
34466 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
34467 c_name, clauses);
34468 break;
34469 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
34470 c_name = "vector_length";
34471 code = OMP_CLAUSE_VECTOR_LENGTH;
34472 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34473 clauses);
34474 break;
34475 case PRAGMA_OACC_CLAUSE_WAIT:
34476 clauses = cp_parser_oacc_clause_wait (parser, clauses);
34477 c_name = "wait";
34478 break;
34479 case PRAGMA_OACC_CLAUSE_WORKER:
34480 c_name = "worker";
34481 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
34482 c_name, clauses);
34483 break;
34484 default:
34485 cp_parser_error (parser, "expected %<#pragma acc%> clause");
34486 goto saw_error;
34489 first = false;
34491 if (((mask >> c_kind) & 1) == 0)
34493 /* Remove the invalid clause(s) from the list to avoid
34494 confusing the rest of the compiler. */
34495 clauses = prev;
34496 error_at (here, "%qs is not valid for %qs", c_name, where);
34500 saw_error:
34501 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34503 if (finish_p)
34504 return finish_omp_clauses (clauses, C_ORT_ACC);
34506 return clauses;
34509 /* Parse all OpenMP clauses. The set clauses allowed by the directive
34510 is a bitmask in MASK. Return the list of clauses found; the result
34511 of clause default goes in *pdefault. */
34513 static tree
34514 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
34515 const char *where, cp_token *pragma_tok,
34516 bool finish_p = true)
34518 tree clauses = NULL;
34519 bool first = true;
34520 cp_token *token = NULL;
34522 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34524 pragma_omp_clause c_kind;
34525 const char *c_name;
34526 tree prev = clauses;
34528 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34529 cp_lexer_consume_token (parser->lexer);
34531 token = cp_lexer_peek_token (parser->lexer);
34532 c_kind = cp_parser_omp_clause_name (parser);
34534 switch (c_kind)
34536 case PRAGMA_OMP_CLAUSE_COLLAPSE:
34537 clauses = cp_parser_omp_clause_collapse (parser, clauses,
34538 token->location);
34539 c_name = "collapse";
34540 break;
34541 case PRAGMA_OMP_CLAUSE_COPYIN:
34542 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
34543 c_name = "copyin";
34544 break;
34545 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
34546 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
34547 clauses);
34548 c_name = "copyprivate";
34549 break;
34550 case PRAGMA_OMP_CLAUSE_DEFAULT:
34551 clauses = cp_parser_omp_clause_default (parser, clauses,
34552 token->location, false);
34553 c_name = "default";
34554 break;
34555 case PRAGMA_OMP_CLAUSE_FINAL:
34556 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
34557 c_name = "final";
34558 break;
34559 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
34560 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34561 clauses);
34562 c_name = "firstprivate";
34563 break;
34564 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
34565 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
34566 token->location);
34567 c_name = "grainsize";
34568 break;
34569 case PRAGMA_OMP_CLAUSE_HINT:
34570 clauses = cp_parser_omp_clause_hint (parser, clauses,
34571 token->location);
34572 c_name = "hint";
34573 break;
34574 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
34575 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34576 token->location);
34577 c_name = "defaultmap";
34578 break;
34579 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34580 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34581 clauses);
34582 c_name = "use_device_ptr";
34583 break;
34584 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34585 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34586 clauses);
34587 c_name = "is_device_ptr";
34588 break;
34589 case PRAGMA_OMP_CLAUSE_IF:
34590 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34591 true);
34592 c_name = "if";
34593 break;
34594 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
34595 clauses
34596 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
34597 true, clauses);
34598 c_name = "in_reduction";
34599 break;
34600 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34601 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
34602 c_name = "lastprivate";
34603 break;
34604 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34605 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34606 token->location);
34607 c_name = "mergeable";
34608 break;
34609 case PRAGMA_OMP_CLAUSE_NOWAIT:
34610 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34611 c_name = "nowait";
34612 break;
34613 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34614 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34615 token->location);
34616 c_name = "num_tasks";
34617 break;
34618 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34619 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34620 token->location);
34621 c_name = "num_threads";
34622 break;
34623 case PRAGMA_OMP_CLAUSE_ORDERED:
34624 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34625 token->location);
34626 c_name = "ordered";
34627 break;
34628 case PRAGMA_OMP_CLAUSE_PRIORITY:
34629 clauses = cp_parser_omp_clause_priority (parser, clauses,
34630 token->location);
34631 c_name = "priority";
34632 break;
34633 case PRAGMA_OMP_CLAUSE_PRIVATE:
34634 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34635 clauses);
34636 c_name = "private";
34637 break;
34638 case PRAGMA_OMP_CLAUSE_REDUCTION:
34639 clauses
34640 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34641 true, clauses);
34642 c_name = "reduction";
34643 break;
34644 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34645 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34646 token->location);
34647 c_name = "schedule";
34648 break;
34649 case PRAGMA_OMP_CLAUSE_SHARED:
34650 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34651 clauses);
34652 c_name = "shared";
34653 break;
34654 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
34655 clauses
34656 = cp_parser_omp_clause_reduction (parser,
34657 OMP_CLAUSE_TASK_REDUCTION,
34658 true, clauses);
34659 c_name = "task_reduction";
34660 break;
34661 case PRAGMA_OMP_CLAUSE_UNTIED:
34662 clauses = cp_parser_omp_clause_untied (parser, clauses,
34663 token->location);
34664 c_name = "untied";
34665 break;
34666 case PRAGMA_OMP_CLAUSE_INBRANCH:
34667 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34668 clauses, token->location);
34669 c_name = "inbranch";
34670 break;
34671 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
34672 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
34673 clauses);
34674 c_name = "nontemporal";
34675 break;
34676 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34677 clauses = cp_parser_omp_clause_branch (parser,
34678 OMP_CLAUSE_NOTINBRANCH,
34679 clauses, token->location);
34680 c_name = "notinbranch";
34681 break;
34682 case PRAGMA_OMP_CLAUSE_PARALLEL:
34683 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34684 clauses, token->location);
34685 c_name = "parallel";
34686 if (!first)
34688 clause_not_first:
34689 error_at (token->location, "%qs must be the first clause of %qs",
34690 c_name, where);
34691 clauses = prev;
34693 break;
34694 case PRAGMA_OMP_CLAUSE_FOR:
34695 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34696 clauses, token->location);
34697 c_name = "for";
34698 if (!first)
34699 goto clause_not_first;
34700 break;
34701 case PRAGMA_OMP_CLAUSE_SECTIONS:
34702 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34703 clauses, token->location);
34704 c_name = "sections";
34705 if (!first)
34706 goto clause_not_first;
34707 break;
34708 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34709 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34710 clauses, token->location);
34711 c_name = "taskgroup";
34712 if (!first)
34713 goto clause_not_first;
34714 break;
34715 case PRAGMA_OMP_CLAUSE_LINK:
34716 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34717 c_name = "to";
34718 break;
34719 case PRAGMA_OMP_CLAUSE_TO:
34720 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34721 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34722 clauses);
34723 else
34724 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34725 c_name = "to";
34726 break;
34727 case PRAGMA_OMP_CLAUSE_FROM:
34728 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34729 c_name = "from";
34730 break;
34731 case PRAGMA_OMP_CLAUSE_UNIFORM:
34732 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34733 clauses);
34734 c_name = "uniform";
34735 break;
34736 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34737 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34738 token->location);
34739 c_name = "num_teams";
34740 break;
34741 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34742 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34743 token->location);
34744 c_name = "thread_limit";
34745 break;
34746 case PRAGMA_OMP_CLAUSE_ALIGNED:
34747 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34748 c_name = "aligned";
34749 break;
34750 case PRAGMA_OMP_CLAUSE_LINEAR:
34752 bool declare_simd = false;
34753 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34754 declare_simd = true;
34755 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34757 c_name = "linear";
34758 break;
34759 case PRAGMA_OMP_CLAUSE_DEPEND:
34760 clauses = cp_parser_omp_clause_depend (parser, clauses,
34761 token->location);
34762 c_name = "depend";
34763 break;
34764 case PRAGMA_OMP_CLAUSE_MAP:
34765 clauses = cp_parser_omp_clause_map (parser, clauses);
34766 c_name = "map";
34767 break;
34768 case PRAGMA_OMP_CLAUSE_DEVICE:
34769 clauses = cp_parser_omp_clause_device (parser, clauses,
34770 token->location);
34771 c_name = "device";
34772 break;
34773 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34774 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34775 token->location);
34776 c_name = "dist_schedule";
34777 break;
34778 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34779 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34780 token->location);
34781 c_name = "proc_bind";
34782 break;
34783 case PRAGMA_OMP_CLAUSE_SAFELEN:
34784 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34785 token->location);
34786 c_name = "safelen";
34787 break;
34788 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34789 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34790 token->location);
34791 c_name = "simdlen";
34792 break;
34793 case PRAGMA_OMP_CLAUSE_NOGROUP:
34794 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34795 token->location);
34796 c_name = "nogroup";
34797 break;
34798 case PRAGMA_OMP_CLAUSE_THREADS:
34799 clauses
34800 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34801 clauses, token->location);
34802 c_name = "threads";
34803 break;
34804 case PRAGMA_OMP_CLAUSE_SIMD:
34805 clauses
34806 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34807 clauses, token->location);
34808 c_name = "simd";
34809 break;
34810 default:
34811 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34812 goto saw_error;
34815 first = false;
34817 if (((mask >> c_kind) & 1) == 0)
34819 /* Remove the invalid clause(s) from the list to avoid
34820 confusing the rest of the compiler. */
34821 clauses = prev;
34822 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34825 saw_error:
34826 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34827 if (finish_p)
34829 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34830 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34831 else
34832 return finish_omp_clauses (clauses, C_ORT_OMP);
34834 return clauses;
34837 /* OpenMP 2.5:
34838 structured-block:
34839 statement
34841 In practice, we're also interested in adding the statement to an
34842 outer node. So it is convenient if we work around the fact that
34843 cp_parser_statement calls add_stmt. */
34845 static unsigned
34846 cp_parser_begin_omp_structured_block (cp_parser *parser)
34848 unsigned save = parser->in_statement;
34850 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34851 This preserves the "not within loop or switch" style error messages
34852 for nonsense cases like
34853 void foo() {
34854 #pragma omp single
34855 break;
34858 if (parser->in_statement)
34859 parser->in_statement = IN_OMP_BLOCK;
34861 return save;
34864 static void
34865 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34867 parser->in_statement = save;
34870 static tree
34871 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34873 tree stmt = begin_omp_structured_block ();
34874 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34876 cp_parser_statement (parser, NULL_TREE, false, if_p);
34878 cp_parser_end_omp_structured_block (parser, save);
34879 return finish_omp_structured_block (stmt);
34882 /* OpenMP 2.5:
34883 # pragma omp atomic new-line
34884 expression-stmt
34886 expression-stmt:
34887 x binop= expr | x++ | ++x | x-- | --x
34888 binop:
34889 +, *, -, /, &, ^, |, <<, >>
34891 where x is an lvalue expression with scalar type.
34893 OpenMP 3.1:
34894 # pragma omp atomic new-line
34895 update-stmt
34897 # pragma omp atomic read new-line
34898 read-stmt
34900 # pragma omp atomic write new-line
34901 write-stmt
34903 # pragma omp atomic update new-line
34904 update-stmt
34906 # pragma omp atomic capture new-line
34907 capture-stmt
34909 # pragma omp atomic capture new-line
34910 capture-block
34912 read-stmt:
34913 v = x
34914 write-stmt:
34915 x = expr
34916 update-stmt:
34917 expression-stmt | x = x binop expr
34918 capture-stmt:
34919 v = expression-stmt
34920 capture-block:
34921 { v = x; update-stmt; } | { update-stmt; v = x; }
34923 OpenMP 4.0:
34924 update-stmt:
34925 expression-stmt | x = x binop expr | x = expr binop x
34926 capture-stmt:
34927 v = update-stmt
34928 capture-block:
34929 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34931 where x and v are lvalue expressions with scalar type. */
34933 static void
34934 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34936 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34937 tree rhs1 = NULL_TREE, orig_lhs;
34938 location_t loc = pragma_tok->location;
34939 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
34940 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
34941 bool structured_block = false;
34942 bool first = true;
34943 tree clauses = NULL_TREE;
34945 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34947 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34948 cp_lexer_consume_token (parser->lexer);
34950 first = false;
34952 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34954 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34955 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
34956 const char *p = IDENTIFIER_POINTER (id);
34957 enum tree_code new_code = ERROR_MARK;
34958 enum omp_memory_order new_memory_order
34959 = OMP_MEMORY_ORDER_UNSPECIFIED;
34961 if (!strcmp (p, "read"))
34962 new_code = OMP_ATOMIC_READ;
34963 else if (!strcmp (p, "write"))
34964 new_code = NOP_EXPR;
34965 else if (!strcmp (p, "update"))
34966 new_code = OMP_ATOMIC;
34967 else if (!strcmp (p, "capture"))
34968 new_code = OMP_ATOMIC_CAPTURE_NEW;
34969 else if (!strcmp (p, "seq_cst"))
34970 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
34971 else if (!strcmp (p, "acq_rel"))
34972 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
34973 else if (!strcmp (p, "release"))
34974 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
34975 else if (!strcmp (p, "acquire"))
34976 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
34977 else if (!strcmp (p, "relaxed"))
34978 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
34979 else if (!strcmp (p, "hint"))
34981 cp_lexer_consume_token (parser->lexer);
34982 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
34983 continue;
34985 else
34987 p = NULL;
34988 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
34989 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
34990 "%<release%>, %<relaxed%> or %<hint%> clause");
34992 if (p)
34994 if (new_code != ERROR_MARK)
34996 if (code != ERROR_MARK)
34997 error_at (cloc, "too many atomic clauses");
34998 else
34999 code = new_code;
35001 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35003 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35004 error_at (cloc, "too many memory order clauses");
35005 else
35006 memory_order = new_memory_order;
35008 cp_lexer_consume_token (parser->lexer);
35009 continue;
35012 break;
35014 cp_parser_require_pragma_eol (parser, pragma_tok);
35016 if (code == ERROR_MARK)
35017 code = OMP_ATOMIC;
35018 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35020 omp_requires_mask
35021 = (enum omp_requires) (omp_requires_mask
35022 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35023 switch ((enum omp_memory_order)
35024 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35026 case OMP_MEMORY_ORDER_UNSPECIFIED:
35027 case OMP_MEMORY_ORDER_RELAXED:
35028 memory_order = OMP_MEMORY_ORDER_RELAXED;
35029 break;
35030 case OMP_MEMORY_ORDER_SEQ_CST:
35031 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35032 break;
35033 case OMP_MEMORY_ORDER_ACQ_REL:
35034 switch (code)
35036 case OMP_ATOMIC_READ:
35037 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35038 break;
35039 case NOP_EXPR: /* atomic write */
35040 case OMP_ATOMIC:
35041 memory_order = OMP_MEMORY_ORDER_RELEASE;
35042 break;
35043 default:
35044 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35045 break;
35047 break;
35048 default:
35049 gcc_unreachable ();
35052 else
35053 switch (code)
35055 case OMP_ATOMIC_READ:
35056 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35057 || memory_order == OMP_MEMORY_ORDER_RELEASE)
35059 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
35060 "%<acq_rel%> or %<release%> clauses");
35061 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35063 break;
35064 case NOP_EXPR: /* atomic write */
35065 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35066 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35068 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
35069 "%<acq_rel%> or %<acquire%> clauses");
35070 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35072 break;
35073 case OMP_ATOMIC:
35074 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35075 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35077 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
35078 "%<acq_rel%> or %<acquire%> clauses");
35079 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35081 break;
35082 default:
35083 break;
35086 switch (code)
35088 case OMP_ATOMIC_READ:
35089 case NOP_EXPR: /* atomic write */
35090 v = cp_parser_unary_expression (parser);
35091 if (v == error_mark_node)
35092 goto saw_error;
35093 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35094 goto saw_error;
35095 if (code == NOP_EXPR)
35096 lhs = cp_parser_expression (parser);
35097 else
35098 lhs = cp_parser_unary_expression (parser);
35099 if (lhs == error_mark_node)
35100 goto saw_error;
35101 if (code == NOP_EXPR)
35103 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35104 opcode. */
35105 code = OMP_ATOMIC;
35106 rhs = lhs;
35107 lhs = v;
35108 v = NULL_TREE;
35110 goto done;
35111 case OMP_ATOMIC_CAPTURE_NEW:
35112 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35114 cp_lexer_consume_token (parser->lexer);
35115 structured_block = true;
35117 else
35119 v = cp_parser_unary_expression (parser);
35120 if (v == error_mark_node)
35121 goto saw_error;
35122 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35123 goto saw_error;
35125 default:
35126 break;
35129 restart:
35130 lhs = cp_parser_unary_expression (parser);
35131 orig_lhs = lhs;
35132 switch (TREE_CODE (lhs))
35134 case ERROR_MARK:
35135 goto saw_error;
35137 case POSTINCREMENT_EXPR:
35138 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35139 code = OMP_ATOMIC_CAPTURE_OLD;
35140 /* FALLTHROUGH */
35141 case PREINCREMENT_EXPR:
35142 lhs = TREE_OPERAND (lhs, 0);
35143 opcode = PLUS_EXPR;
35144 rhs = integer_one_node;
35145 break;
35147 case POSTDECREMENT_EXPR:
35148 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35149 code = OMP_ATOMIC_CAPTURE_OLD;
35150 /* FALLTHROUGH */
35151 case PREDECREMENT_EXPR:
35152 lhs = TREE_OPERAND (lhs, 0);
35153 opcode = MINUS_EXPR;
35154 rhs = integer_one_node;
35155 break;
35157 case COMPOUND_EXPR:
35158 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
35159 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
35160 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
35161 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
35162 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35163 (TREE_OPERAND (lhs, 1), 0), 0)))
35164 == BOOLEAN_TYPE)
35165 /* Undo effects of boolean_increment for post {in,de}crement. */
35166 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
35167 /* FALLTHRU */
35168 case MODIFY_EXPR:
35169 if (TREE_CODE (lhs) == MODIFY_EXPR
35170 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
35172 /* Undo effects of boolean_increment. */
35173 if (integer_onep (TREE_OPERAND (lhs, 1)))
35175 /* This is pre or post increment. */
35176 rhs = TREE_OPERAND (lhs, 1);
35177 lhs = TREE_OPERAND (lhs, 0);
35178 opcode = NOP_EXPR;
35179 if (code == OMP_ATOMIC_CAPTURE_NEW
35180 && !structured_block
35181 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
35182 code = OMP_ATOMIC_CAPTURE_OLD;
35183 break;
35186 /* FALLTHRU */
35187 default:
35188 switch (cp_lexer_peek_token (parser->lexer)->type)
35190 case CPP_MULT_EQ:
35191 opcode = MULT_EXPR;
35192 break;
35193 case CPP_DIV_EQ:
35194 opcode = TRUNC_DIV_EXPR;
35195 break;
35196 case CPP_PLUS_EQ:
35197 opcode = PLUS_EXPR;
35198 break;
35199 case CPP_MINUS_EQ:
35200 opcode = MINUS_EXPR;
35201 break;
35202 case CPP_LSHIFT_EQ:
35203 opcode = LSHIFT_EXPR;
35204 break;
35205 case CPP_RSHIFT_EQ:
35206 opcode = RSHIFT_EXPR;
35207 break;
35208 case CPP_AND_EQ:
35209 opcode = BIT_AND_EXPR;
35210 break;
35211 case CPP_OR_EQ:
35212 opcode = BIT_IOR_EXPR;
35213 break;
35214 case CPP_XOR_EQ:
35215 opcode = BIT_XOR_EXPR;
35216 break;
35217 case CPP_EQ:
35218 enum cp_parser_prec oprec;
35219 cp_token *token;
35220 cp_lexer_consume_token (parser->lexer);
35221 cp_parser_parse_tentatively (parser);
35222 rhs1 = cp_parser_simple_cast_expression (parser);
35223 if (rhs1 == error_mark_node)
35225 cp_parser_abort_tentative_parse (parser);
35226 cp_parser_simple_cast_expression (parser);
35227 goto saw_error;
35229 token = cp_lexer_peek_token (parser->lexer);
35230 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
35232 cp_parser_abort_tentative_parse (parser);
35233 cp_parser_parse_tentatively (parser);
35234 rhs = cp_parser_binary_expression (parser, false, true,
35235 PREC_NOT_OPERATOR, NULL);
35236 if (rhs == error_mark_node)
35238 cp_parser_abort_tentative_parse (parser);
35239 cp_parser_binary_expression (parser, false, true,
35240 PREC_NOT_OPERATOR, NULL);
35241 goto saw_error;
35243 switch (TREE_CODE (rhs))
35245 case MULT_EXPR:
35246 case TRUNC_DIV_EXPR:
35247 case RDIV_EXPR:
35248 case PLUS_EXPR:
35249 case MINUS_EXPR:
35250 case LSHIFT_EXPR:
35251 case RSHIFT_EXPR:
35252 case BIT_AND_EXPR:
35253 case BIT_IOR_EXPR:
35254 case BIT_XOR_EXPR:
35255 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
35257 if (cp_parser_parse_definitely (parser))
35259 opcode = TREE_CODE (rhs);
35260 rhs1 = TREE_OPERAND (rhs, 0);
35261 rhs = TREE_OPERAND (rhs, 1);
35262 goto stmt_done;
35264 else
35265 goto saw_error;
35267 break;
35268 default:
35269 break;
35271 cp_parser_abort_tentative_parse (parser);
35272 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
35274 rhs = cp_parser_expression (parser);
35275 if (rhs == error_mark_node)
35276 goto saw_error;
35277 opcode = NOP_EXPR;
35278 rhs1 = NULL_TREE;
35279 goto stmt_done;
35281 cp_parser_error (parser,
35282 "invalid form of %<#pragma omp atomic%>");
35283 goto saw_error;
35285 if (!cp_parser_parse_definitely (parser))
35286 goto saw_error;
35287 switch (token->type)
35289 case CPP_SEMICOLON:
35290 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35292 code = OMP_ATOMIC_CAPTURE_OLD;
35293 v = lhs;
35294 lhs = NULL_TREE;
35295 lhs1 = rhs1;
35296 rhs1 = NULL_TREE;
35297 cp_lexer_consume_token (parser->lexer);
35298 goto restart;
35300 else if (structured_block)
35302 opcode = NOP_EXPR;
35303 rhs = rhs1;
35304 rhs1 = NULL_TREE;
35305 goto stmt_done;
35307 cp_parser_error (parser,
35308 "invalid form of %<#pragma omp atomic%>");
35309 goto saw_error;
35310 case CPP_MULT:
35311 opcode = MULT_EXPR;
35312 break;
35313 case CPP_DIV:
35314 opcode = TRUNC_DIV_EXPR;
35315 break;
35316 case CPP_PLUS:
35317 opcode = PLUS_EXPR;
35318 break;
35319 case CPP_MINUS:
35320 opcode = MINUS_EXPR;
35321 break;
35322 case CPP_LSHIFT:
35323 opcode = LSHIFT_EXPR;
35324 break;
35325 case CPP_RSHIFT:
35326 opcode = RSHIFT_EXPR;
35327 break;
35328 case CPP_AND:
35329 opcode = BIT_AND_EXPR;
35330 break;
35331 case CPP_OR:
35332 opcode = BIT_IOR_EXPR;
35333 break;
35334 case CPP_XOR:
35335 opcode = BIT_XOR_EXPR;
35336 break;
35337 default:
35338 cp_parser_error (parser,
35339 "invalid operator for %<#pragma omp atomic%>");
35340 goto saw_error;
35342 oprec = TOKEN_PRECEDENCE (token);
35343 gcc_assert (oprec != PREC_NOT_OPERATOR);
35344 if (commutative_tree_code (opcode))
35345 oprec = (enum cp_parser_prec) (oprec - 1);
35346 cp_lexer_consume_token (parser->lexer);
35347 rhs = cp_parser_binary_expression (parser, false, false,
35348 oprec, NULL);
35349 if (rhs == error_mark_node)
35350 goto saw_error;
35351 goto stmt_done;
35352 /* FALLTHROUGH */
35353 default:
35354 cp_parser_error (parser,
35355 "invalid operator for %<#pragma omp atomic%>");
35356 goto saw_error;
35358 cp_lexer_consume_token (parser->lexer);
35360 rhs = cp_parser_expression (parser);
35361 if (rhs == error_mark_node)
35362 goto saw_error;
35363 break;
35365 stmt_done:
35366 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35368 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
35369 goto saw_error;
35370 v = cp_parser_unary_expression (parser);
35371 if (v == error_mark_node)
35372 goto saw_error;
35373 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35374 goto saw_error;
35375 lhs1 = cp_parser_unary_expression (parser);
35376 if (lhs1 == error_mark_node)
35377 goto saw_error;
35379 if (structured_block)
35381 cp_parser_consume_semicolon_at_end_of_statement (parser);
35382 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35384 done:
35385 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
35386 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
35387 rhs1, clauses, memory_order);
35388 if (!structured_block)
35389 cp_parser_consume_semicolon_at_end_of_statement (parser);
35390 return;
35392 saw_error:
35393 cp_parser_skip_to_end_of_block_or_statement (parser);
35394 if (structured_block)
35396 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35397 cp_lexer_consume_token (parser->lexer);
35398 else if (code == OMP_ATOMIC_CAPTURE_NEW)
35400 cp_parser_skip_to_end_of_block_or_statement (parser);
35401 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35402 cp_lexer_consume_token (parser->lexer);
35408 /* OpenMP 2.5:
35409 # pragma omp barrier new-line */
35411 static void
35412 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
35414 cp_parser_require_pragma_eol (parser, pragma_tok);
35415 finish_omp_barrier ();
35418 /* OpenMP 2.5:
35419 # pragma omp critical [(name)] new-line
35420 structured-block
35422 OpenMP 4.5:
35423 # pragma omp critical [(name) [hint(expression)]] new-line
35424 structured-block */
35426 #define OMP_CRITICAL_CLAUSE_MASK \
35427 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35429 static tree
35430 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35432 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
35434 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35436 matching_parens parens;
35437 parens.consume_open (parser);
35439 name = cp_parser_identifier (parser);
35441 if (name == error_mark_node
35442 || !parens.require_close (parser))
35443 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35444 /*or_comma=*/false,
35445 /*consume_paren=*/true);
35446 if (name == error_mark_node)
35447 name = NULL;
35449 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
35450 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
35451 cp_lexer_consume_token (parser->lexer);
35453 clauses = cp_parser_omp_all_clauses (parser,
35454 OMP_CRITICAL_CLAUSE_MASK,
35455 "#pragma omp critical", pragma_tok);
35457 else
35458 cp_parser_require_pragma_eol (parser, pragma_tok);
35460 stmt = cp_parser_omp_structured_block (parser, if_p);
35461 return c_finish_omp_critical (input_location, stmt, name, clauses);
35464 /* OpenMP 5.0:
35465 # pragma omp depobj ( depobj ) depobj-clause new-line
35467 depobj-clause:
35468 depend (dependence-type : locator)
35469 destroy
35470 update (dependence-type)
35472 dependence-type:
35475 inout
35476 mutexinout */
35478 static void
35479 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
35481 location_t loc = pragma_tok->location;
35482 matching_parens parens;
35483 if (!parens.require_open (parser))
35485 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35486 return;
35489 tree depobj = cp_parser_assignment_expression (parser);
35491 if (!parens.require_close (parser))
35492 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35493 /*or_comma=*/false,
35494 /*consume_paren=*/true);
35496 tree clause = NULL_TREE;
35497 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
35498 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
35499 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35501 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35502 const char *p = IDENTIFIER_POINTER (id);
35504 cp_lexer_consume_token (parser->lexer);
35505 if (!strcmp ("depend", p))
35507 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
35508 if (clause)
35509 clause = finish_omp_clauses (clause, C_ORT_OMP);
35510 if (!clause)
35511 clause = error_mark_node;
35513 else if (!strcmp ("destroy", p))
35514 kind = OMP_CLAUSE_DEPEND_LAST;
35515 else if (!strcmp ("update", p))
35517 matching_parens c_parens;
35518 if (c_parens.require_open (parser))
35520 location_t c2_loc
35521 = cp_lexer_peek_token (parser->lexer)->location;
35522 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35524 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
35525 const char *p2 = IDENTIFIER_POINTER (id2);
35527 cp_lexer_consume_token (parser->lexer);
35528 if (!strcmp ("in", p2))
35529 kind = OMP_CLAUSE_DEPEND_IN;
35530 else if (!strcmp ("out", p2))
35531 kind = OMP_CLAUSE_DEPEND_OUT;
35532 else if (!strcmp ("inout", p2))
35533 kind = OMP_CLAUSE_DEPEND_INOUT;
35534 else if (!strcmp ("mutexinoutset", p2))
35535 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
35537 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
35539 clause = error_mark_node;
35540 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
35541 "%<mutexinoutset%>");
35543 if (!c_parens.require_close (parser))
35544 cp_parser_skip_to_closing_parenthesis (parser,
35545 /*recovering=*/true,
35546 /*or_comma=*/false,
35547 /*consume_paren=*/true);
35549 else
35550 clause = error_mark_node;
35553 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
35555 clause = error_mark_node;
35556 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
35558 cp_parser_require_pragma_eol (parser, pragma_tok);
35560 finish_omp_depobj (loc, depobj, kind, clause);
35564 /* OpenMP 2.5:
35565 # pragma omp flush flush-vars[opt] new-line
35567 flush-vars:
35568 ( variable-list )
35570 OpenMP 5.0:
35571 # pragma omp flush memory-order-clause new-line */
35573 static void
35574 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
35576 enum memmodel mo = MEMMODEL_LAST;
35577 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35579 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35580 const char *p = IDENTIFIER_POINTER (id);
35581 if (!strcmp (p, "acq_rel"))
35582 mo = MEMMODEL_ACQ_REL;
35583 else if (!strcmp (p, "release"))
35584 mo = MEMMODEL_RELEASE;
35585 else if (!strcmp (p, "acquire"))
35586 mo = MEMMODEL_ACQUIRE;
35587 else
35588 error_at (cp_lexer_peek_token (parser->lexer)->location,
35589 "expected %<acq_rel%>, %<release%> or %<acquire%>");
35590 cp_lexer_consume_token (parser->lexer);
35592 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35594 if (mo != MEMMODEL_LAST)
35595 error_at (cp_lexer_peek_token (parser->lexer)->location,
35596 "%<flush%> list specified together with memory order "
35597 "clause");
35598 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35600 cp_parser_require_pragma_eol (parser, pragma_tok);
35602 finish_omp_flush (mo);
35605 /* Helper function, to parse omp for increment expression. */
35607 static tree
35608 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
35610 tree cond = cp_parser_binary_expression (parser, false, true,
35611 PREC_NOT_OPERATOR, NULL);
35612 if (cond == error_mark_node
35613 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35615 cp_parser_skip_to_end_of_statement (parser);
35616 return error_mark_node;
35619 switch (TREE_CODE (cond))
35621 case GT_EXPR:
35622 case GE_EXPR:
35623 case LT_EXPR:
35624 case LE_EXPR:
35625 break;
35626 case NE_EXPR:
35627 if (code != OACC_LOOP)
35628 break;
35629 gcc_fallthrough ();
35630 default:
35631 return error_mark_node;
35634 /* If decl is an iterator, preserve LHS and RHS of the relational
35635 expr until finish_omp_for. */
35636 if (decl
35637 && (type_dependent_expression_p (decl)
35638 || CLASS_TYPE_P (TREE_TYPE (decl))))
35639 return cond;
35641 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
35642 TREE_CODE (cond),
35643 TREE_OPERAND (cond, 0), ERROR_MARK,
35644 TREE_OPERAND (cond, 1), ERROR_MARK,
35645 /*overload=*/NULL, tf_warning_or_error);
35648 /* Helper function, to parse omp for increment expression. */
35650 static tree
35651 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
35653 cp_token *token = cp_lexer_peek_token (parser->lexer);
35654 enum tree_code op;
35655 tree lhs, rhs;
35656 cp_id_kind idk;
35657 bool decl_first;
35659 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
35661 op = (token->type == CPP_PLUS_PLUS
35662 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
35663 cp_lexer_consume_token (parser->lexer);
35664 lhs = cp_parser_simple_cast_expression (parser);
35665 if (lhs != decl
35666 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
35667 return error_mark_node;
35668 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
35671 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
35672 if (lhs != decl
35673 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
35674 return error_mark_node;
35676 token = cp_lexer_peek_token (parser->lexer);
35677 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
35679 op = (token->type == CPP_PLUS_PLUS
35680 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
35681 cp_lexer_consume_token (parser->lexer);
35682 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
35685 op = cp_parser_assignment_operator_opt (parser);
35686 if (op == ERROR_MARK)
35687 return error_mark_node;
35689 if (op != NOP_EXPR)
35691 rhs = cp_parser_assignment_expression (parser);
35692 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
35693 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35696 lhs = cp_parser_binary_expression (parser, false, false,
35697 PREC_ADDITIVE_EXPRESSION, NULL);
35698 token = cp_lexer_peek_token (parser->lexer);
35699 decl_first = (lhs == decl
35700 || (processing_template_decl && cp_tree_equal (lhs, decl)));
35701 if (decl_first)
35702 lhs = NULL_TREE;
35703 if (token->type != CPP_PLUS
35704 && token->type != CPP_MINUS)
35705 return error_mark_node;
35709 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
35710 cp_lexer_consume_token (parser->lexer);
35711 rhs = cp_parser_binary_expression (parser, false, false,
35712 PREC_ADDITIVE_EXPRESSION, NULL);
35713 token = cp_lexer_peek_token (parser->lexer);
35714 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
35716 if (lhs == NULL_TREE)
35718 if (op == PLUS_EXPR)
35719 lhs = rhs;
35720 else
35721 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
35722 tf_warning_or_error);
35724 else
35725 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
35726 ERROR_MARK, NULL, tf_warning_or_error);
35729 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
35731 if (!decl_first)
35733 if ((rhs != decl
35734 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
35735 || op == MINUS_EXPR)
35736 return error_mark_node;
35737 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
35739 else
35740 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
35742 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35745 /* Parse the initialization statement of an OpenMP for loop.
35747 Return true if the resulting construct should have an
35748 OMP_CLAUSE_PRIVATE added to it. */
35750 static tree
35751 cp_parser_omp_for_loop_init (cp_parser *parser,
35752 tree &this_pre_body,
35753 vec<tree, va_gc> *&for_block,
35754 tree &init,
35755 tree &orig_init,
35756 tree &decl,
35757 tree &real_decl)
35759 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35760 return NULL_TREE;
35762 tree add_private_clause = NULL_TREE;
35764 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
35766 init-expr:
35767 var = lb
35768 integer-type var = lb
35769 random-access-iterator-type var = lb
35770 pointer-type var = lb
35772 cp_decl_specifier_seq type_specifiers;
35774 /* First, try to parse as an initialized declaration. See
35775 cp_parser_condition, from whence the bulk of this is copied. */
35777 cp_parser_parse_tentatively (parser);
35778 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
35779 /*is_trailing_return=*/false,
35780 &type_specifiers);
35781 if (cp_parser_parse_definitely (parser))
35783 /* If parsing a type specifier seq succeeded, then this
35784 MUST be a initialized declaration. */
35785 tree asm_specification, attributes;
35786 cp_declarator *declarator;
35788 declarator = cp_parser_declarator (parser,
35789 CP_PARSER_DECLARATOR_NAMED,
35790 /*ctor_dtor_or_conv_p=*/NULL,
35791 /*parenthesized_p=*/NULL,
35792 /*member_p=*/false,
35793 /*friend_p=*/false);
35794 attributes = cp_parser_attributes_opt (parser);
35795 asm_specification = cp_parser_asm_specification_opt (parser);
35797 if (declarator == cp_error_declarator)
35798 cp_parser_skip_to_end_of_statement (parser);
35800 else
35802 tree pushed_scope, auto_node;
35804 decl = start_decl (declarator, &type_specifiers,
35805 SD_INITIALIZED, attributes,
35806 /*prefix_attributes=*/NULL_TREE,
35807 &pushed_scope);
35809 auto_node = type_uses_auto (TREE_TYPE (decl));
35810 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
35812 if (cp_lexer_next_token_is (parser->lexer,
35813 CPP_OPEN_PAREN))
35814 error ("parenthesized initialization is not allowed in "
35815 "OpenMP %<for%> loop");
35816 else
35817 /* Trigger an error. */
35818 cp_parser_require (parser, CPP_EQ, RT_EQ);
35820 init = error_mark_node;
35821 cp_parser_skip_to_end_of_statement (parser);
35823 else if (CLASS_TYPE_P (TREE_TYPE (decl))
35824 || type_dependent_expression_p (decl)
35825 || auto_node)
35827 bool is_direct_init, is_non_constant_init;
35829 init = cp_parser_initializer (parser,
35830 &is_direct_init,
35831 &is_non_constant_init);
35833 if (auto_node)
35835 TREE_TYPE (decl)
35836 = do_auto_deduction (TREE_TYPE (decl), init,
35837 auto_node);
35839 if (!CLASS_TYPE_P (TREE_TYPE (decl))
35840 && !type_dependent_expression_p (decl))
35841 goto non_class;
35844 cp_finish_decl (decl, init, !is_non_constant_init,
35845 asm_specification,
35846 LOOKUP_ONLYCONVERTING);
35847 orig_init = init;
35848 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35850 vec_safe_push (for_block, this_pre_body);
35851 init = NULL_TREE;
35853 else
35855 init = pop_stmt_list (this_pre_body);
35856 if (init && TREE_CODE (init) == STATEMENT_LIST)
35858 tree_stmt_iterator i = tsi_start (init);
35859 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35860 while (!tsi_end_p (i))
35862 tree t = tsi_stmt (i);
35863 if (TREE_CODE (t) == DECL_EXPR
35864 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35866 tsi_delink (&i);
35867 vec_safe_push (for_block, t);
35868 continue;
35870 break;
35872 if (tsi_one_before_end_p (i))
35874 tree t = tsi_stmt (i);
35875 tsi_delink (&i);
35876 free_stmt_list (init);
35877 init = t;
35881 this_pre_body = NULL_TREE;
35883 else
35885 /* Consume '='. */
35886 cp_lexer_consume_token (parser->lexer);
35887 init = cp_parser_assignment_expression (parser);
35889 non_class:
35890 if (TYPE_REF_P (TREE_TYPE (decl)))
35891 init = error_mark_node;
35892 else
35893 cp_finish_decl (decl, NULL_TREE,
35894 /*init_const_expr_p=*/false,
35895 asm_specification,
35896 LOOKUP_ONLYCONVERTING);
35899 if (pushed_scope)
35900 pop_scope (pushed_scope);
35903 else
35905 cp_id_kind idk;
35906 /* If parsing a type specifier sequence failed, then
35907 this MUST be a simple expression. */
35908 cp_parser_parse_tentatively (parser);
35909 decl = cp_parser_primary_expression (parser, false, false,
35910 false, &idk);
35911 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35912 if (!cp_parser_error_occurred (parser)
35913 && decl
35914 && (TREE_CODE (decl) == COMPONENT_REF
35915 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35917 cp_parser_abort_tentative_parse (parser);
35918 cp_parser_parse_tentatively (parser);
35919 cp_token *token = cp_lexer_peek_token (parser->lexer);
35920 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35921 /*check_dependency_p=*/true,
35922 /*template_p=*/NULL,
35923 /*declarator_p=*/false,
35924 /*optional_p=*/false);
35925 if (name != error_mark_node
35926 && last_tok == cp_lexer_peek_token (parser->lexer))
35928 decl = cp_parser_lookup_name_simple (parser, name,
35929 token->location);
35930 if (TREE_CODE (decl) == FIELD_DECL)
35931 add_private_clause = omp_privatize_field (decl, false);
35933 cp_parser_abort_tentative_parse (parser);
35934 cp_parser_parse_tentatively (parser);
35935 decl = cp_parser_primary_expression (parser, false, false,
35936 false, &idk);
35938 if (!cp_parser_error_occurred (parser)
35939 && decl
35940 && DECL_P (decl)
35941 && CLASS_TYPE_P (TREE_TYPE (decl)))
35943 tree rhs;
35945 cp_parser_parse_definitely (parser);
35946 cp_parser_require (parser, CPP_EQ, RT_EQ);
35947 rhs = cp_parser_assignment_expression (parser);
35948 orig_init = rhs;
35949 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35950 decl, NOP_EXPR,
35951 rhs,
35952 tf_warning_or_error));
35953 if (!add_private_clause)
35954 add_private_clause = decl;
35956 else
35958 decl = NULL;
35959 cp_parser_abort_tentative_parse (parser);
35960 init = cp_parser_expression (parser);
35961 if (init)
35963 if (TREE_CODE (init) == MODIFY_EXPR
35964 || TREE_CODE (init) == MODOP_EXPR)
35965 real_decl = TREE_OPERAND (init, 0);
35969 return add_private_clause;
35972 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
35974 void
35975 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
35976 tree &decl, tree &orig_decl, tree &init,
35977 tree &orig_init, tree &cond, tree &incr)
35979 tree begin, end, range_temp_decl = NULL_TREE;
35980 tree iter_type, begin_expr, end_expr;
35982 if (processing_template_decl)
35984 if (check_for_bare_parameter_packs (init))
35985 init = error_mark_node;
35986 if (!type_dependent_expression_p (init)
35987 /* do_auto_deduction doesn't mess with template init-lists. */
35988 && !BRACE_ENCLOSED_INITIALIZER_P (init))
35990 tree d = decl;
35991 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
35993 tree v = DECL_VALUE_EXPR (decl);
35994 if (TREE_CODE (v) == ARRAY_REF
35995 && VAR_P (TREE_OPERAND (v, 0))
35996 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
35997 d = TREE_OPERAND (v, 0);
35999 do_range_for_auto_deduction (d, init);
36001 cond = global_namespace;
36002 incr = NULL_TREE;
36003 orig_init = init;
36004 if (this_pre_body)
36005 this_pre_body = pop_stmt_list (this_pre_body);
36006 return;
36009 init = mark_lvalue_use (init);
36011 if (decl == error_mark_node || init == error_mark_node)
36012 /* If an error happened previously do nothing or else a lot of
36013 unhelpful errors would be issued. */
36014 begin_expr = end_expr = iter_type = error_mark_node;
36015 else
36017 tree range_temp;
36019 if (VAR_P (init)
36020 && array_of_runtime_bound_p (TREE_TYPE (init)))
36021 /* Can't bind a reference to an array of runtime bound. */
36022 range_temp = init;
36023 else
36025 range_temp = build_range_temp (init);
36026 DECL_NAME (range_temp) = NULL_TREE;
36027 pushdecl (range_temp);
36028 cp_finish_decl (range_temp, init,
36029 /*is_constant_init*/false, NULL_TREE,
36030 LOOKUP_ONLYCONVERTING);
36031 range_temp_decl = range_temp;
36032 range_temp = convert_from_reference (range_temp);
36034 iter_type = cp_parser_perform_range_for_lookup (range_temp,
36035 &begin_expr, &end_expr);
36038 tree end_iter_type = iter_type;
36039 if (cxx_dialect >= cxx17)
36040 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36041 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
36042 TREE_USED (end) = 1;
36043 DECL_ARTIFICIAL (end) = 1;
36044 pushdecl (end);
36045 cp_finish_decl (end, end_expr,
36046 /*is_constant_init*/false, NULL_TREE,
36047 LOOKUP_ONLYCONVERTING);
36049 /* The new for initialization statement. */
36050 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
36051 TREE_USED (begin) = 1;
36052 DECL_ARTIFICIAL (begin) = 1;
36053 pushdecl (begin);
36054 orig_init = init;
36055 if (CLASS_TYPE_P (iter_type))
36056 init = NULL_TREE;
36057 else
36059 init = begin_expr;
36060 begin_expr = NULL_TREE;
36062 cp_finish_decl (begin, begin_expr,
36063 /*is_constant_init*/false, NULL_TREE,
36064 LOOKUP_ONLYCONVERTING);
36066 /* The new for condition. */
36067 if (CLASS_TYPE_P (iter_type))
36068 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
36069 else
36070 cond = build_x_binary_op (input_location, NE_EXPR,
36071 begin, ERROR_MARK,
36072 end, ERROR_MARK,
36073 NULL, tf_warning_or_error);
36075 /* The new increment expression. */
36076 if (CLASS_TYPE_P (iter_type))
36077 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
36078 else
36079 incr = finish_unary_op_expr (input_location,
36080 PREINCREMENT_EXPR, begin,
36081 tf_warning_or_error);
36083 orig_decl = decl;
36084 decl = begin;
36085 if (for_block)
36087 vec_safe_push (for_block, this_pre_body);
36088 this_pre_body = NULL_TREE;
36091 tree decomp_first_name = NULL_TREE;
36092 unsigned decomp_cnt = 0;
36093 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
36095 tree v = DECL_VALUE_EXPR (orig_decl);
36096 if (TREE_CODE (v) == ARRAY_REF
36097 && VAR_P (TREE_OPERAND (v, 0))
36098 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36100 tree d = orig_decl;
36101 orig_decl = TREE_OPERAND (v, 0);
36102 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
36103 decomp_first_name = d;
36107 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
36108 if (auto_node)
36110 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36111 tf_none);
36112 if (!error_operand_p (t))
36113 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
36114 t, auto_node);
36117 tree v = make_tree_vec (decomp_cnt + 3);
36118 TREE_VEC_ELT (v, 0) = range_temp_decl;
36119 TREE_VEC_ELT (v, 1) = end;
36120 TREE_VEC_ELT (v, 2) = orig_decl;
36121 for (unsigned i = 0; i < decomp_cnt; i++)
36123 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
36124 decomp_first_name = DECL_CHAIN (decomp_first_name);
36126 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
36129 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36130 inside of the collapsed body. */
36132 void
36133 cp_finish_omp_range_for (tree orig, tree begin)
36135 gcc_assert (TREE_CODE (orig) == TREE_LIST
36136 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
36137 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
36138 tree decomp_first_name = NULL_TREE;
36139 unsigned int decomp_cnt = 0;
36141 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36143 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
36144 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
36145 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
36148 /* The declaration is initialized with *__begin inside the loop body. */
36149 cp_finish_decl (decl,
36150 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36151 tf_warning_or_error),
36152 /*is_constant_init*/false, NULL_TREE,
36153 LOOKUP_ONLYCONVERTING);
36154 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36155 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
36158 /* Parse the restricted form of the for statement allowed by OpenMP. */
36160 static tree
36161 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
36162 tree *cclauses, bool *if_p)
36164 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
36165 tree orig_decl;
36166 tree real_decl, initv, condv, incrv, declv, orig_declv;
36167 tree this_pre_body, cl, ordered_cl = NULL_TREE;
36168 location_t loc_first;
36169 bool collapse_err = false;
36170 int i, collapse = 1, ordered = 0, count, nbraces = 0;
36171 vec<tree, va_gc> *for_block = make_tree_vector ();
36172 auto_vec<tree, 4> orig_inits;
36173 bool tiling = false;
36175 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
36176 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
36177 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
36178 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
36180 tiling = true;
36181 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
36183 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
36184 && OMP_CLAUSE_ORDERED_EXPR (cl))
36186 ordered_cl = cl;
36187 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
36190 if (ordered && ordered < collapse)
36192 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36193 "%<ordered%> clause parameter is less than %<collapse%>");
36194 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
36195 = build_int_cst (NULL_TREE, collapse);
36196 ordered = collapse;
36198 if (ordered)
36200 for (tree *pc = &clauses; *pc; )
36201 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
36203 error_at (OMP_CLAUSE_LOCATION (*pc),
36204 "%<linear%> clause may not be specified together "
36205 "with %<ordered%> clause with a parameter");
36206 *pc = OMP_CLAUSE_CHAIN (*pc);
36208 else
36209 pc = &OMP_CLAUSE_CHAIN (*pc);
36212 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
36213 count = ordered ? ordered : collapse;
36215 declv = make_tree_vec (count);
36216 initv = make_tree_vec (count);
36217 condv = make_tree_vec (count);
36218 incrv = make_tree_vec (count);
36219 orig_declv = NULL_TREE;
36221 loc_first = cp_lexer_peek_token (parser->lexer)->location;
36223 for (i = 0; i < count; i++)
36225 int bracecount = 0;
36226 tree add_private_clause = NULL_TREE;
36227 location_t loc;
36229 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36231 if (!collapse_err)
36232 cp_parser_error (parser, "for statement expected");
36233 return NULL;
36235 loc = cp_lexer_consume_token (parser->lexer)->location;
36237 matching_parens parens;
36238 if (!parens.require_open (parser))
36239 return NULL;
36241 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
36242 this_pre_body = push_stmt_list ();
36244 if (code != OACC_LOOP && cxx_dialect >= cxx11)
36246 /* Save tokens so that we can put them back. */
36247 cp_lexer_save_tokens (parser->lexer);
36249 /* Look for ':' that is not nested in () or {}. */
36250 bool is_range_for
36251 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
36252 /*recovering=*/false,
36253 CPP_COLON,
36254 /*consume_paren=*/
36255 false) == -1);
36257 /* Roll back the tokens we skipped. */
36258 cp_lexer_rollback_tokens (parser->lexer);
36260 if (is_range_for)
36262 bool saved_colon_corrects_to_scope_p
36263 = parser->colon_corrects_to_scope_p;
36265 /* A colon is used in range-based for. */
36266 parser->colon_corrects_to_scope_p = false;
36268 /* Parse the declaration. */
36269 cp_parser_simple_declaration (parser,
36270 /*function_definition_allowed_p=*/
36271 false, &decl);
36272 parser->colon_corrects_to_scope_p
36273 = saved_colon_corrects_to_scope_p;
36275 cp_parser_require (parser, CPP_COLON, RT_COLON);
36277 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
36278 false, 0, true);
36280 cp_convert_omp_range_for (this_pre_body, for_block, decl,
36281 orig_decl, init, orig_init,
36282 cond, incr);
36283 if (this_pre_body)
36285 if (pre_body)
36287 tree t = pre_body;
36288 pre_body = push_stmt_list ();
36289 add_stmt (t);
36290 add_stmt (this_pre_body);
36291 pre_body = pop_stmt_list (pre_body);
36293 else
36294 pre_body = this_pre_body;
36297 if (ordered_cl)
36298 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36299 "%<ordered%> clause with parameter on "
36300 "range-based %<for%> loop");
36302 goto parse_close_paren;
36306 add_private_clause
36307 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
36308 init, orig_init, decl, real_decl);
36310 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36311 if (this_pre_body)
36313 this_pre_body = pop_stmt_list (this_pre_body);
36314 if (pre_body)
36316 tree t = pre_body;
36317 pre_body = push_stmt_list ();
36318 add_stmt (t);
36319 add_stmt (this_pre_body);
36320 pre_body = pop_stmt_list (pre_body);
36322 else
36323 pre_body = this_pre_body;
36326 if (decl)
36327 real_decl = decl;
36328 if (cclauses != NULL
36329 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
36330 && real_decl != NULL_TREE)
36332 tree *c;
36333 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
36334 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
36335 && OMP_CLAUSE_DECL (*c) == real_decl)
36337 error_at (loc, "iteration variable %qD"
36338 " should not be firstprivate", real_decl);
36339 *c = OMP_CLAUSE_CHAIN (*c);
36341 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
36342 && OMP_CLAUSE_DECL (*c) == real_decl)
36344 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36345 tree l = *c;
36346 *c = OMP_CLAUSE_CHAIN (*c);
36347 if (code == OMP_SIMD)
36349 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36350 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
36352 else
36354 OMP_CLAUSE_CHAIN (l) = clauses;
36355 clauses = l;
36357 add_private_clause = NULL_TREE;
36359 else
36361 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
36362 && OMP_CLAUSE_DECL (*c) == real_decl)
36363 add_private_clause = NULL_TREE;
36364 c = &OMP_CLAUSE_CHAIN (*c);
36368 if (add_private_clause)
36370 tree c;
36371 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
36373 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
36374 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
36375 && OMP_CLAUSE_DECL (c) == decl)
36376 break;
36377 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
36378 && OMP_CLAUSE_DECL (c) == decl)
36379 error_at (loc, "iteration variable %qD "
36380 "should not be firstprivate",
36381 decl);
36382 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
36383 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
36384 && OMP_CLAUSE_DECL (c) == decl)
36385 error_at (loc, "iteration variable %qD should not be reduction",
36386 decl);
36388 if (c == NULL)
36390 if (code != OMP_SIMD)
36391 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
36392 else if (collapse == 1)
36393 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
36394 else
36395 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
36396 OMP_CLAUSE_DECL (c) = add_private_clause;
36397 c = finish_omp_clauses (c, C_ORT_OMP);
36398 if (c)
36400 OMP_CLAUSE_CHAIN (c) = clauses;
36401 clauses = c;
36402 /* For linear, signal that we need to fill up
36403 the so far unknown linear step. */
36404 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
36405 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
36410 cond = NULL;
36411 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36412 cond = cp_parser_omp_for_cond (parser, decl, code);
36413 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36415 incr = NULL;
36416 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
36418 /* If decl is an iterator, preserve the operator on decl
36419 until finish_omp_for. */
36420 if (real_decl
36421 && ((processing_template_decl
36422 && (TREE_TYPE (real_decl) == NULL_TREE
36423 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
36424 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
36425 incr = cp_parser_omp_for_incr (parser, real_decl);
36426 else
36427 incr = cp_parser_expression (parser);
36428 if (!EXPR_HAS_LOCATION (incr))
36429 protected_set_expr_location (incr, input_location);
36432 parse_close_paren:
36433 if (!parens.require_close (parser))
36434 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36435 /*or_comma=*/false,
36436 /*consume_paren=*/true);
36438 TREE_VEC_ELT (declv, i) = decl;
36439 TREE_VEC_ELT (initv, i) = init;
36440 TREE_VEC_ELT (condv, i) = cond;
36441 TREE_VEC_ELT (incrv, i) = incr;
36442 if (orig_init)
36444 orig_inits.safe_grow_cleared (i + 1);
36445 orig_inits[i] = orig_init;
36447 if (orig_decl)
36449 if (!orig_declv)
36450 orig_declv = copy_node (declv);
36451 TREE_VEC_ELT (orig_declv, i) = orig_decl;
36453 else if (orig_declv)
36454 TREE_VEC_ELT (orig_declv, i) = decl;
36456 if (i == count - 1)
36457 break;
36459 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
36460 in between the collapsed for loops to be still considered perfectly
36461 nested. Hopefully the final version clarifies this.
36462 For now handle (multiple) {'s and empty statements. */
36463 cp_parser_parse_tentatively (parser);
36464 for (;;)
36466 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36467 break;
36468 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36470 cp_lexer_consume_token (parser->lexer);
36471 bracecount++;
36473 else if (bracecount
36474 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36475 cp_lexer_consume_token (parser->lexer);
36476 else
36478 loc = cp_lexer_peek_token (parser->lexer)->location;
36479 error_at (loc, "not enough for loops to collapse");
36480 collapse_err = true;
36481 cp_parser_abort_tentative_parse (parser);
36482 declv = NULL_TREE;
36483 break;
36487 if (declv)
36489 cp_parser_parse_definitely (parser);
36490 nbraces += bracecount;
36494 if (nbraces)
36495 if_p = NULL;
36497 /* Note that we saved the original contents of this flag when we entered
36498 the structured block, and so we don't need to re-save it here. */
36499 parser->in_statement = IN_OMP_FOR;
36501 /* Note that the grammar doesn't call for a structured block here,
36502 though the loop as a whole is a structured block. */
36503 if (orig_declv)
36505 body = begin_omp_structured_block ();
36506 for (i = 0; i < count; i++)
36507 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
36508 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
36509 TREE_VEC_ELT (declv, i));
36511 else
36512 body = push_stmt_list ();
36513 cp_parser_statement (parser, NULL_TREE, false, if_p);
36514 if (orig_declv)
36515 body = finish_omp_structured_block (body);
36516 else
36517 body = pop_stmt_list (body);
36519 if (declv == NULL_TREE)
36520 ret = NULL_TREE;
36521 else
36522 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
36523 incrv, body, pre_body, &orig_inits, clauses);
36525 while (nbraces)
36527 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36529 cp_lexer_consume_token (parser->lexer);
36530 nbraces--;
36532 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36533 cp_lexer_consume_token (parser->lexer);
36534 else
36536 if (!collapse_err)
36538 error_at (cp_lexer_peek_token (parser->lexer)->location,
36539 "collapsed loops not perfectly nested");
36541 collapse_err = true;
36542 cp_parser_statement_seq_opt (parser, NULL);
36543 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
36544 break;
36548 while (!for_block->is_empty ())
36550 tree t = for_block->pop ();
36551 if (TREE_CODE (t) == STATEMENT_LIST)
36552 add_stmt (pop_stmt_list (t));
36553 else
36554 add_stmt (t);
36556 release_tree_vector (for_block);
36558 return ret;
36561 /* Helper function for OpenMP parsing, split clauses and call
36562 finish_omp_clauses on each of the set of clauses afterwards. */
36564 static void
36565 cp_omp_split_clauses (location_t loc, enum tree_code code,
36566 omp_clause_mask mask, tree clauses, tree *cclauses)
36568 int i;
36569 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
36570 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
36571 if (cclauses[i])
36572 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
36575 /* OpenMP 4.0:
36576 #pragma omp simd simd-clause[optseq] new-line
36577 for-loop */
36579 #define OMP_SIMD_CLAUSE_MASK \
36580 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
36581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
36591 static tree
36592 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
36593 char *p_name, omp_clause_mask mask, tree *cclauses,
36594 bool *if_p)
36596 tree clauses, sb, ret;
36597 unsigned int save;
36598 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36600 strcat (p_name, " simd");
36601 mask |= OMP_SIMD_CLAUSE_MASK;
36603 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36604 cclauses == NULL);
36605 if (cclauses)
36607 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
36608 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
36609 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
36610 OMP_CLAUSE_ORDERED);
36611 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
36613 error_at (OMP_CLAUSE_LOCATION (c),
36614 "%<ordered%> clause with parameter may not be specified "
36615 "on %qs construct", p_name);
36616 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
36620 keep_next_level (true);
36621 sb = begin_omp_structured_block ();
36622 save = cp_parser_begin_omp_structured_block (parser);
36624 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
36626 cp_parser_end_omp_structured_block (parser, save);
36627 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
36629 return ret;
36632 /* OpenMP 2.5:
36633 #pragma omp for for-clause[optseq] new-line
36634 for-loop
36636 OpenMP 4.0:
36637 #pragma omp for simd for-simd-clause[optseq] new-line
36638 for-loop */
36640 #define OMP_FOR_CLAUSE_MASK \
36641 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
36647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
36648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36651 static tree
36652 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
36653 char *p_name, omp_clause_mask mask, tree *cclauses,
36654 bool *if_p)
36656 tree clauses, sb, ret;
36657 unsigned int save;
36658 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36660 strcat (p_name, " for");
36661 mask |= OMP_FOR_CLAUSE_MASK;
36662 /* parallel for{, simd} disallows nowait clause, but for
36663 target {teams distribute ,}parallel for{, simd} it should be accepted. */
36664 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
36665 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
36666 /* Composite distribute parallel for{, simd} disallows ordered clause. */
36667 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
36668 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
36670 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36672 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36673 const char *p = IDENTIFIER_POINTER (id);
36675 if (strcmp (p, "simd") == 0)
36677 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36678 if (cclauses == NULL)
36679 cclauses = cclauses_buf;
36681 cp_lexer_consume_token (parser->lexer);
36682 if (!flag_openmp) /* flag_openmp_simd */
36683 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36684 cclauses, if_p);
36685 sb = begin_omp_structured_block ();
36686 save = cp_parser_begin_omp_structured_block (parser);
36687 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36688 cclauses, if_p);
36689 cp_parser_end_omp_structured_block (parser, save);
36690 tree body = finish_omp_structured_block (sb);
36691 if (ret == NULL)
36692 return ret;
36693 ret = make_node (OMP_FOR);
36694 TREE_TYPE (ret) = void_type_node;
36695 OMP_FOR_BODY (ret) = body;
36696 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36697 SET_EXPR_LOCATION (ret, loc);
36698 add_stmt (ret);
36699 return ret;
36702 if (!flag_openmp) /* flag_openmp_simd */
36704 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36705 return NULL_TREE;
36708 /* Composite distribute parallel for disallows linear clause. */
36709 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
36710 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
36712 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36713 cclauses == NULL);
36714 if (cclauses)
36716 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
36717 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36720 keep_next_level (true);
36721 sb = begin_omp_structured_block ();
36722 save = cp_parser_begin_omp_structured_block (parser);
36724 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
36726 cp_parser_end_omp_structured_block (parser, save);
36727 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
36729 return ret;
36732 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
36733 omp_clause_mask, tree *, bool *);
36735 /* OpenMP 2.5:
36736 # pragma omp master new-line
36737 structured-block */
36739 static tree
36740 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
36741 char *p_name, omp_clause_mask mask, tree *cclauses,
36742 bool *if_p)
36744 tree clauses, sb, ret;
36745 unsigned int save;
36746 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36748 strcat (p_name, " master");
36750 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36752 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36753 const char *p = IDENTIFIER_POINTER (id);
36755 if (strcmp (p, "taskloop") == 0)
36757 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36758 if (cclauses == NULL)
36759 cclauses = cclauses_buf;
36761 cp_lexer_consume_token (parser->lexer);
36762 if (!flag_openmp) /* flag_openmp_simd */
36763 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
36764 cclauses, if_p);
36765 sb = begin_omp_structured_block ();
36766 save = cp_parser_begin_omp_structured_block (parser);
36767 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
36768 cclauses, if_p);
36769 cp_parser_end_omp_structured_block (parser, save);
36770 tree body = finish_omp_structured_block (sb);
36771 if (ret == NULL)
36772 return ret;
36773 return c_finish_omp_master (loc, body);
36776 if (!flag_openmp) /* flag_openmp_simd */
36778 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36779 return NULL_TREE;
36782 if (cclauses)
36784 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36785 false);
36786 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
36788 else
36789 cp_parser_require_pragma_eol (parser, pragma_tok);
36791 return c_finish_omp_master (loc,
36792 cp_parser_omp_structured_block (parser, if_p));
36795 /* OpenMP 2.5:
36796 # pragma omp ordered new-line
36797 structured-block
36799 OpenMP 4.5:
36800 # pragma omp ordered ordered-clauses new-line
36801 structured-block */
36803 #define OMP_ORDERED_CLAUSE_MASK \
36804 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
36805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
36807 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
36808 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
36810 static bool
36811 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
36812 enum pragma_context context, bool *if_p)
36814 location_t loc = pragma_tok->location;
36816 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36818 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36819 const char *p = IDENTIFIER_POINTER (id);
36821 if (strcmp (p, "depend") == 0)
36823 if (!flag_openmp) /* flag_openmp_simd */
36825 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36826 return false;
36828 if (context == pragma_stmt)
36830 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
36831 "%<depend%> clause may only be used in compound "
36832 "statements");
36833 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36834 return false;
36836 tree clauses
36837 = cp_parser_omp_all_clauses (parser,
36838 OMP_ORDERED_DEPEND_CLAUSE_MASK,
36839 "#pragma omp ordered", pragma_tok);
36840 c_finish_omp_ordered (loc, clauses, NULL_TREE);
36841 return false;
36845 tree clauses
36846 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
36847 "#pragma omp ordered", pragma_tok);
36849 if (!flag_openmp /* flag_openmp_simd */
36850 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
36851 return false;
36853 c_finish_omp_ordered (loc, clauses,
36854 cp_parser_omp_structured_block (parser, if_p));
36855 return true;
36858 /* OpenMP 2.5:
36860 section-scope:
36861 { section-sequence }
36863 section-sequence:
36864 section-directive[opt] structured-block
36865 section-sequence section-directive structured-block */
36867 static tree
36868 cp_parser_omp_sections_scope (cp_parser *parser)
36870 tree stmt, substmt;
36871 bool error_suppress = false;
36872 cp_token *tok;
36874 matching_braces braces;
36875 if (!braces.require_open (parser))
36876 return NULL_TREE;
36878 stmt = push_stmt_list ();
36880 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
36881 != PRAGMA_OMP_SECTION)
36883 substmt = cp_parser_omp_structured_block (parser, NULL);
36884 substmt = build1 (OMP_SECTION, void_type_node, substmt);
36885 add_stmt (substmt);
36888 while (1)
36890 tok = cp_lexer_peek_token (parser->lexer);
36891 if (tok->type == CPP_CLOSE_BRACE)
36892 break;
36893 if (tok->type == CPP_EOF)
36894 break;
36896 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
36898 cp_lexer_consume_token (parser->lexer);
36899 cp_parser_require_pragma_eol (parser, tok);
36900 error_suppress = false;
36902 else if (!error_suppress)
36904 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
36905 error_suppress = true;
36908 substmt = cp_parser_omp_structured_block (parser, NULL);
36909 substmt = build1 (OMP_SECTION, void_type_node, substmt);
36910 add_stmt (substmt);
36912 braces.require_close (parser);
36914 substmt = pop_stmt_list (stmt);
36916 stmt = make_node (OMP_SECTIONS);
36917 TREE_TYPE (stmt) = void_type_node;
36918 OMP_SECTIONS_BODY (stmt) = substmt;
36920 add_stmt (stmt);
36921 return stmt;
36924 /* OpenMP 2.5:
36925 # pragma omp sections sections-clause[optseq] newline
36926 sections-scope */
36928 #define OMP_SECTIONS_CLAUSE_MASK \
36929 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36935 static tree
36936 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
36937 char *p_name, omp_clause_mask mask, tree *cclauses)
36939 tree clauses, ret;
36940 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36942 strcat (p_name, " sections");
36943 mask |= OMP_SECTIONS_CLAUSE_MASK;
36944 if (cclauses)
36945 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
36947 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36948 cclauses == NULL);
36949 if (cclauses)
36951 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
36952 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
36955 ret = cp_parser_omp_sections_scope (parser);
36956 if (ret)
36957 OMP_SECTIONS_CLAUSES (ret) = clauses;
36959 return ret;
36962 /* OpenMP 2.5:
36963 # pragma omp parallel parallel-clause[optseq] new-line
36964 structured-block
36965 # pragma omp parallel for parallel-for-clause[optseq] new-line
36966 structured-block
36967 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
36968 structured-block
36970 OpenMP 4.0:
36971 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
36972 structured-block */
36974 #define OMP_PARALLEL_CLAUSE_MASK \
36975 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
36981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
36983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
36985 static tree
36986 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
36987 char *p_name, omp_clause_mask mask, tree *cclauses,
36988 bool *if_p)
36990 tree stmt, clauses, block;
36991 unsigned int save;
36992 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36994 strcat (p_name, " parallel");
36995 mask |= OMP_PARALLEL_CLAUSE_MASK;
36996 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
36997 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
36998 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
36999 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
37001 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37003 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37004 if (cclauses == NULL)
37005 cclauses = cclauses_buf;
37007 cp_lexer_consume_token (parser->lexer);
37008 if (!flag_openmp) /* flag_openmp_simd */
37009 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37010 if_p);
37011 block = begin_omp_parallel ();
37012 save = cp_parser_begin_omp_structured_block (parser);
37013 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37014 if_p);
37015 cp_parser_end_omp_structured_block (parser, save);
37016 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37017 block);
37018 if (ret == NULL_TREE)
37019 return ret;
37020 OMP_PARALLEL_COMBINED (stmt) = 1;
37021 return stmt;
37023 /* When combined with distribute, parallel has to be followed by for.
37024 #pragma omp target parallel is allowed though. */
37025 else if (cclauses
37026 && (mask & (OMP_CLAUSE_MASK_1
37027 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37029 error_at (loc, "expected %<for%> after %qs", p_name);
37030 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37031 return NULL_TREE;
37033 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37035 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37036 const char *p = IDENTIFIER_POINTER (id);
37037 if (strcmp (p, "master") == 0)
37039 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37040 cclauses = cclauses_buf;
37042 cp_lexer_consume_token (parser->lexer);
37043 block = begin_omp_parallel ();
37044 save = cp_parser_begin_omp_structured_block (parser);
37045 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37046 cclauses, if_p);
37047 cp_parser_end_omp_structured_block (parser, save);
37048 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37049 block);
37050 OMP_PARALLEL_COMBINED (stmt) = 1;
37051 if (ret == NULL_TREE)
37052 return ret;
37053 return stmt;
37055 else if (!flag_openmp) /* flag_openmp_simd */
37057 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37058 return NULL_TREE;
37060 else if (strcmp (p, "sections") == 0)
37062 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37063 cclauses = cclauses_buf;
37065 cp_lexer_consume_token (parser->lexer);
37066 block = begin_omp_parallel ();
37067 save = cp_parser_begin_omp_structured_block (parser);
37068 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
37069 cp_parser_end_omp_structured_block (parser, save);
37070 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37071 block);
37072 OMP_PARALLEL_COMBINED (stmt) = 1;
37073 return stmt;
37076 else if (!flag_openmp) /* flag_openmp_simd */
37078 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37079 return NULL_TREE;
37082 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37083 cclauses == NULL);
37084 if (cclauses)
37086 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
37087 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
37090 block = begin_omp_parallel ();
37091 save = cp_parser_begin_omp_structured_block (parser);
37092 cp_parser_statement (parser, NULL_TREE, false, if_p);
37093 cp_parser_end_omp_structured_block (parser, save);
37094 stmt = finish_omp_parallel (clauses, block);
37095 return stmt;
37098 /* OpenMP 2.5:
37099 # pragma omp single single-clause[optseq] new-line
37100 structured-block */
37102 #define OMP_SINGLE_CLAUSE_MASK \
37103 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37108 static tree
37109 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37111 tree stmt = make_node (OMP_SINGLE);
37112 TREE_TYPE (stmt) = void_type_node;
37113 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37115 OMP_SINGLE_CLAUSES (stmt)
37116 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
37117 "#pragma omp single", pragma_tok);
37118 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37120 return add_stmt (stmt);
37123 /* OpenMP 3.0:
37124 # pragma omp task task-clause[optseq] new-line
37125 structured-block */
37127 #define OMP_TASK_CLAUSE_MASK \
37128 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37140 static tree
37141 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37143 tree clauses, block;
37144 unsigned int save;
37146 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
37147 "#pragma omp task", pragma_tok);
37148 block = begin_omp_task ();
37149 save = cp_parser_begin_omp_structured_block (parser);
37150 cp_parser_statement (parser, NULL_TREE, false, if_p);
37151 cp_parser_end_omp_structured_block (parser, save);
37152 return finish_omp_task (clauses, block);
37155 /* OpenMP 3.0:
37156 # pragma omp taskwait new-line
37158 OpenMP 5.0:
37159 # pragma omp taskwait taskwait-clause[opt] new-line */
37161 #define OMP_TASKWAIT_CLAUSE_MASK \
37162 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37164 static void
37165 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
37167 tree clauses
37168 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
37169 "#pragma omp taskwait", pragma_tok);
37171 if (clauses)
37173 tree stmt = make_node (OMP_TASK);
37174 TREE_TYPE (stmt) = void_node;
37175 OMP_TASK_CLAUSES (stmt) = clauses;
37176 OMP_TASK_BODY (stmt) = NULL_TREE;
37177 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37178 add_stmt (stmt);
37180 else
37181 finish_omp_taskwait ();
37184 /* OpenMP 3.1:
37185 # pragma omp taskyield new-line */
37187 static void
37188 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
37190 cp_parser_require_pragma_eol (parser, pragma_tok);
37191 finish_omp_taskyield ();
37194 /* OpenMP 4.0:
37195 # pragma omp taskgroup new-line
37196 structured-block
37198 OpenMP 5.0:
37199 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37201 #define OMP_TASKGROUP_CLAUSE_MASK \
37202 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37204 static tree
37205 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37207 tree clauses
37208 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
37209 "#pragma omp taskgroup", pragma_tok);
37210 return c_finish_omp_taskgroup (input_location,
37211 cp_parser_omp_structured_block (parser,
37212 if_p),
37213 clauses);
37217 /* OpenMP 2.5:
37218 # pragma omp threadprivate (variable-list) */
37220 static void
37221 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
37223 tree vars;
37225 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37226 cp_parser_require_pragma_eol (parser, pragma_tok);
37228 finish_omp_threadprivate (vars);
37231 /* OpenMP 4.0:
37232 # pragma omp cancel cancel-clause[optseq] new-line */
37234 #define OMP_CANCEL_CLAUSE_MASK \
37235 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37241 static void
37242 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
37244 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
37245 "#pragma omp cancel", pragma_tok);
37246 finish_omp_cancel (clauses);
37249 /* OpenMP 4.0:
37250 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37252 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37253 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37258 static void
37259 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
37260 enum pragma_context context)
37262 tree clauses;
37263 bool point_seen = false;
37265 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37267 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37268 const char *p = IDENTIFIER_POINTER (id);
37270 if (strcmp (p, "point") == 0)
37272 cp_lexer_consume_token (parser->lexer);
37273 point_seen = true;
37276 if (!point_seen)
37278 cp_parser_error (parser, "expected %<point%>");
37279 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37280 return;
37283 if (context != pragma_compound)
37285 if (context == pragma_stmt)
37286 error_at (pragma_tok->location,
37287 "%<#pragma %s%> may only be used in compound statements",
37288 "omp cancellation point");
37289 else
37290 cp_parser_error (parser, "expected declaration specifiers");
37291 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37292 return;
37295 clauses = cp_parser_omp_all_clauses (parser,
37296 OMP_CANCELLATION_POINT_CLAUSE_MASK,
37297 "#pragma omp cancellation point",
37298 pragma_tok);
37299 finish_omp_cancellation_point (clauses);
37302 /* OpenMP 4.0:
37303 #pragma omp distribute distribute-clause[optseq] new-line
37304 for-loop */
37306 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37307 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37313 static tree
37314 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
37315 char *p_name, omp_clause_mask mask, tree *cclauses,
37316 bool *if_p)
37318 tree clauses, sb, ret;
37319 unsigned int save;
37320 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37322 strcat (p_name, " distribute");
37323 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
37325 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37327 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37328 const char *p = IDENTIFIER_POINTER (id);
37329 bool simd = false;
37330 bool parallel = false;
37332 if (strcmp (p, "simd") == 0)
37333 simd = true;
37334 else
37335 parallel = strcmp (p, "parallel") == 0;
37336 if (parallel || simd)
37338 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37339 if (cclauses == NULL)
37340 cclauses = cclauses_buf;
37341 cp_lexer_consume_token (parser->lexer);
37342 if (!flag_openmp) /* flag_openmp_simd */
37344 if (simd)
37345 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37346 cclauses, if_p);
37347 else
37348 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37349 cclauses, if_p);
37351 sb = begin_omp_structured_block ();
37352 save = cp_parser_begin_omp_structured_block (parser);
37353 if (simd)
37354 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37355 cclauses, if_p);
37356 else
37357 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37358 cclauses, if_p);
37359 cp_parser_end_omp_structured_block (parser, save);
37360 tree body = finish_omp_structured_block (sb);
37361 if (ret == NULL)
37362 return ret;
37363 ret = make_node (OMP_DISTRIBUTE);
37364 TREE_TYPE (ret) = void_type_node;
37365 OMP_FOR_BODY (ret) = body;
37366 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37367 SET_EXPR_LOCATION (ret, loc);
37368 add_stmt (ret);
37369 return ret;
37372 if (!flag_openmp) /* flag_openmp_simd */
37374 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37375 return NULL_TREE;
37378 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37379 cclauses == NULL);
37380 if (cclauses)
37382 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
37383 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37386 keep_next_level (true);
37387 sb = begin_omp_structured_block ();
37388 save = cp_parser_begin_omp_structured_block (parser);
37390 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
37392 cp_parser_end_omp_structured_block (parser, save);
37393 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37395 return ret;
37398 /* OpenMP 4.0:
37399 # pragma omp teams teams-clause[optseq] new-line
37400 structured-block */
37402 #define OMP_TEAMS_CLAUSE_MASK \
37403 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37411 static tree
37412 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
37413 char *p_name, omp_clause_mask mask, tree *cclauses,
37414 bool *if_p)
37416 tree clauses, sb, ret;
37417 unsigned int save;
37418 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37420 strcat (p_name, " teams");
37421 mask |= OMP_TEAMS_CLAUSE_MASK;
37423 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37425 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37426 const char *p = IDENTIFIER_POINTER (id);
37427 if (strcmp (p, "distribute") == 0)
37429 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37430 if (cclauses == NULL)
37431 cclauses = cclauses_buf;
37433 cp_lexer_consume_token (parser->lexer);
37434 if (!flag_openmp) /* flag_openmp_simd */
37435 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37436 cclauses, if_p);
37437 keep_next_level (true);
37438 sb = begin_omp_structured_block ();
37439 save = cp_parser_begin_omp_structured_block (parser);
37440 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37441 cclauses, if_p);
37442 cp_parser_end_omp_structured_block (parser, save);
37443 tree body = finish_omp_structured_block (sb);
37444 if (ret == NULL)
37445 return ret;
37446 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37447 ret = make_node (OMP_TEAMS);
37448 TREE_TYPE (ret) = void_type_node;
37449 OMP_TEAMS_CLAUSES (ret) = clauses;
37450 OMP_TEAMS_BODY (ret) = body;
37451 OMP_TEAMS_COMBINED (ret) = 1;
37452 SET_EXPR_LOCATION (ret, loc);
37453 return add_stmt (ret);
37456 if (!flag_openmp) /* flag_openmp_simd */
37458 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37459 return NULL_TREE;
37462 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37463 cclauses == NULL);
37464 if (cclauses)
37466 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
37467 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37470 tree stmt = make_node (OMP_TEAMS);
37471 TREE_TYPE (stmt) = void_type_node;
37472 OMP_TEAMS_CLAUSES (stmt) = clauses;
37473 keep_next_level (true);
37474 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37475 SET_EXPR_LOCATION (stmt, loc);
37477 return add_stmt (stmt);
37480 /* OpenMP 4.0:
37481 # pragma omp target data target-data-clause[optseq] new-line
37482 structured-block */
37484 #define OMP_TARGET_DATA_CLAUSE_MASK \
37485 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
37490 static tree
37491 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37493 tree clauses
37494 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
37495 "#pragma omp target data", pragma_tok);
37496 int map_seen = 0;
37497 for (tree *pc = &clauses; *pc;)
37499 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37500 switch (OMP_CLAUSE_MAP_KIND (*pc))
37502 case GOMP_MAP_TO:
37503 case GOMP_MAP_ALWAYS_TO:
37504 case GOMP_MAP_FROM:
37505 case GOMP_MAP_ALWAYS_FROM:
37506 case GOMP_MAP_TOFROM:
37507 case GOMP_MAP_ALWAYS_TOFROM:
37508 case GOMP_MAP_ALLOC:
37509 map_seen = 3;
37510 break;
37511 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37512 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37513 case GOMP_MAP_ALWAYS_POINTER:
37514 break;
37515 default:
37516 map_seen |= 1;
37517 error_at (OMP_CLAUSE_LOCATION (*pc),
37518 "%<#pragma omp target data%> with map-type other "
37519 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37520 "on %<map%> clause");
37521 *pc = OMP_CLAUSE_CHAIN (*pc);
37522 continue;
37524 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
37525 map_seen = 3;
37526 pc = &OMP_CLAUSE_CHAIN (*pc);
37529 if (map_seen != 3)
37531 if (map_seen == 0)
37532 error_at (pragma_tok->location,
37533 "%<#pragma omp target data%> must contain at least "
37534 "one %<map%> or %<use_device_ptr%> clause");
37535 return NULL_TREE;
37538 tree stmt = make_node (OMP_TARGET_DATA);
37539 TREE_TYPE (stmt) = void_type_node;
37540 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
37542 keep_next_level (true);
37543 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37545 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37546 return add_stmt (stmt);
37549 /* OpenMP 4.5:
37550 # pragma omp target enter data target-enter-data-clause[optseq] new-line
37551 structured-block */
37553 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
37554 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37560 static tree
37561 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
37562 enum pragma_context context)
37564 bool data_seen = false;
37565 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37567 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37568 const char *p = IDENTIFIER_POINTER (id);
37570 if (strcmp (p, "data") == 0)
37572 cp_lexer_consume_token (parser->lexer);
37573 data_seen = true;
37576 if (!data_seen)
37578 cp_parser_error (parser, "expected %<data%>");
37579 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37580 return NULL_TREE;
37583 if (context == pragma_stmt)
37585 error_at (pragma_tok->location,
37586 "%<#pragma %s%> may only be used in compound statements",
37587 "omp target enter data");
37588 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37589 return NULL_TREE;
37592 tree clauses
37593 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
37594 "#pragma omp target enter data", pragma_tok);
37595 int map_seen = 0;
37596 for (tree *pc = &clauses; *pc;)
37598 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37599 switch (OMP_CLAUSE_MAP_KIND (*pc))
37601 case GOMP_MAP_TO:
37602 case GOMP_MAP_ALWAYS_TO:
37603 case GOMP_MAP_ALLOC:
37604 map_seen = 3;
37605 break;
37606 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37607 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37608 case GOMP_MAP_ALWAYS_POINTER:
37609 break;
37610 default:
37611 map_seen |= 1;
37612 error_at (OMP_CLAUSE_LOCATION (*pc),
37613 "%<#pragma omp target enter data%> with map-type other "
37614 "than %<to%> or %<alloc%> on %<map%> clause");
37615 *pc = OMP_CLAUSE_CHAIN (*pc);
37616 continue;
37618 pc = &OMP_CLAUSE_CHAIN (*pc);
37621 if (map_seen != 3)
37623 if (map_seen == 0)
37624 error_at (pragma_tok->location,
37625 "%<#pragma omp target enter data%> must contain at least "
37626 "one %<map%> clause");
37627 return NULL_TREE;
37630 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
37631 TREE_TYPE (stmt) = void_type_node;
37632 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
37633 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37634 return add_stmt (stmt);
37637 /* OpenMP 4.5:
37638 # pragma omp target exit data target-enter-data-clause[optseq] new-line
37639 structured-block */
37641 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
37642 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37648 static tree
37649 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
37650 enum pragma_context context)
37652 bool data_seen = false;
37653 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37655 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37656 const char *p = IDENTIFIER_POINTER (id);
37658 if (strcmp (p, "data") == 0)
37660 cp_lexer_consume_token (parser->lexer);
37661 data_seen = true;
37664 if (!data_seen)
37666 cp_parser_error (parser, "expected %<data%>");
37667 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37668 return NULL_TREE;
37671 if (context == pragma_stmt)
37673 error_at (pragma_tok->location,
37674 "%<#pragma %s%> may only be used in compound statements",
37675 "omp target exit data");
37676 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37677 return NULL_TREE;
37680 tree clauses
37681 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
37682 "#pragma omp target exit data", pragma_tok);
37683 int map_seen = 0;
37684 for (tree *pc = &clauses; *pc;)
37686 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37687 switch (OMP_CLAUSE_MAP_KIND (*pc))
37689 case GOMP_MAP_FROM:
37690 case GOMP_MAP_ALWAYS_FROM:
37691 case GOMP_MAP_RELEASE:
37692 case GOMP_MAP_DELETE:
37693 map_seen = 3;
37694 break;
37695 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37696 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37697 case GOMP_MAP_ALWAYS_POINTER:
37698 break;
37699 default:
37700 map_seen |= 1;
37701 error_at (OMP_CLAUSE_LOCATION (*pc),
37702 "%<#pragma omp target exit data%> with map-type other "
37703 "than %<from%>, %<release%> or %<delete%> on %<map%>"
37704 " clause");
37705 *pc = OMP_CLAUSE_CHAIN (*pc);
37706 continue;
37708 pc = &OMP_CLAUSE_CHAIN (*pc);
37711 if (map_seen != 3)
37713 if (map_seen == 0)
37714 error_at (pragma_tok->location,
37715 "%<#pragma omp target exit data%> must contain at least "
37716 "one %<map%> clause");
37717 return NULL_TREE;
37720 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
37721 TREE_TYPE (stmt) = void_type_node;
37722 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
37723 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37724 return add_stmt (stmt);
37727 /* OpenMP 4.0:
37728 # pragma omp target update target-update-clause[optseq] new-line */
37730 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
37731 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
37732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37738 static bool
37739 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
37740 enum pragma_context context)
37742 if (context == pragma_stmt)
37744 error_at (pragma_tok->location,
37745 "%<#pragma %s%> may only be used in compound statements",
37746 "omp target update");
37747 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37748 return false;
37751 tree clauses
37752 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
37753 "#pragma omp target update", pragma_tok);
37754 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
37755 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
37757 error_at (pragma_tok->location,
37758 "%<#pragma omp target update%> must contain at least one "
37759 "%<from%> or %<to%> clauses");
37760 return false;
37763 tree stmt = make_node (OMP_TARGET_UPDATE);
37764 TREE_TYPE (stmt) = void_type_node;
37765 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
37766 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37767 add_stmt (stmt);
37768 return false;
37771 /* OpenMP 4.0:
37772 # pragma omp target target-clause[optseq] new-line
37773 structured-block */
37775 #define OMP_TARGET_CLAUSE_MASK \
37776 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
37784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
37786 static bool
37787 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
37788 enum pragma_context context, bool *if_p)
37790 tree *pc = NULL, stmt;
37792 if (flag_openmp)
37793 omp_requires_mask
37794 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
37796 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37798 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37799 const char *p = IDENTIFIER_POINTER (id);
37800 enum tree_code ccode = ERROR_MARK;
37802 if (strcmp (p, "teams") == 0)
37803 ccode = OMP_TEAMS;
37804 else if (strcmp (p, "parallel") == 0)
37805 ccode = OMP_PARALLEL;
37806 else if (strcmp (p, "simd") == 0)
37807 ccode = OMP_SIMD;
37808 if (ccode != ERROR_MARK)
37810 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
37811 char p_name[sizeof ("#pragma omp target teams distribute "
37812 "parallel for simd")];
37814 cp_lexer_consume_token (parser->lexer);
37815 strcpy (p_name, "#pragma omp target");
37816 if (!flag_openmp) /* flag_openmp_simd */
37818 tree stmt;
37819 switch (ccode)
37821 case OMP_TEAMS:
37822 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
37823 OMP_TARGET_CLAUSE_MASK,
37824 cclauses, if_p);
37825 break;
37826 case OMP_PARALLEL:
37827 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
37828 OMP_TARGET_CLAUSE_MASK,
37829 cclauses, if_p);
37830 break;
37831 case OMP_SIMD:
37832 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
37833 OMP_TARGET_CLAUSE_MASK,
37834 cclauses, if_p);
37835 break;
37836 default:
37837 gcc_unreachable ();
37839 return stmt != NULL_TREE;
37841 keep_next_level (true);
37842 tree sb = begin_omp_structured_block (), ret;
37843 unsigned save = cp_parser_begin_omp_structured_block (parser);
37844 switch (ccode)
37846 case OMP_TEAMS:
37847 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
37848 OMP_TARGET_CLAUSE_MASK, cclauses,
37849 if_p);
37850 break;
37851 case OMP_PARALLEL:
37852 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
37853 OMP_TARGET_CLAUSE_MASK, cclauses,
37854 if_p);
37855 break;
37856 case OMP_SIMD:
37857 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
37858 OMP_TARGET_CLAUSE_MASK, cclauses,
37859 if_p);
37860 break;
37861 default:
37862 gcc_unreachable ();
37864 cp_parser_end_omp_structured_block (parser, save);
37865 tree body = finish_omp_structured_block (sb);
37866 if (ret == NULL_TREE)
37867 return false;
37868 if (ccode == OMP_TEAMS && !processing_template_decl)
37870 /* For combined target teams, ensure the num_teams and
37871 thread_limit clause expressions are evaluated on the host,
37872 before entering the target construct. */
37873 tree c;
37874 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37875 c; c = OMP_CLAUSE_CHAIN (c))
37876 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
37877 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
37878 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
37880 tree expr = OMP_CLAUSE_OPERAND (c, 0);
37881 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
37882 if (expr == error_mark_node)
37883 continue;
37884 tree tmp = TARGET_EXPR_SLOT (expr);
37885 add_stmt (expr);
37886 OMP_CLAUSE_OPERAND (c, 0) = expr;
37887 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
37888 OMP_CLAUSE_FIRSTPRIVATE);
37889 OMP_CLAUSE_DECL (tc) = tmp;
37890 OMP_CLAUSE_CHAIN (tc)
37891 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
37892 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
37895 tree stmt = make_node (OMP_TARGET);
37896 TREE_TYPE (stmt) = void_type_node;
37897 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
37898 OMP_TARGET_BODY (stmt) = body;
37899 OMP_TARGET_COMBINED (stmt) = 1;
37900 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37901 add_stmt (stmt);
37902 pc = &OMP_TARGET_CLAUSES (stmt);
37903 goto check_clauses;
37905 else if (!flag_openmp) /* flag_openmp_simd */
37907 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37908 return false;
37910 else if (strcmp (p, "data") == 0)
37912 cp_lexer_consume_token (parser->lexer);
37913 cp_parser_omp_target_data (parser, pragma_tok, if_p);
37914 return true;
37916 else if (strcmp (p, "enter") == 0)
37918 cp_lexer_consume_token (parser->lexer);
37919 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
37920 return false;
37922 else if (strcmp (p, "exit") == 0)
37924 cp_lexer_consume_token (parser->lexer);
37925 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
37926 return false;
37928 else if (strcmp (p, "update") == 0)
37930 cp_lexer_consume_token (parser->lexer);
37931 return cp_parser_omp_target_update (parser, pragma_tok, context);
37934 if (!flag_openmp) /* flag_openmp_simd */
37936 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37937 return false;
37940 stmt = make_node (OMP_TARGET);
37941 TREE_TYPE (stmt) = void_type_node;
37943 OMP_TARGET_CLAUSES (stmt)
37944 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
37945 "#pragma omp target", pragma_tok);
37946 pc = &OMP_TARGET_CLAUSES (stmt);
37947 keep_next_level (true);
37948 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37950 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37951 add_stmt (stmt);
37953 check_clauses:
37954 while (*pc)
37956 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37957 switch (OMP_CLAUSE_MAP_KIND (*pc))
37959 case GOMP_MAP_TO:
37960 case GOMP_MAP_ALWAYS_TO:
37961 case GOMP_MAP_FROM:
37962 case GOMP_MAP_ALWAYS_FROM:
37963 case GOMP_MAP_TOFROM:
37964 case GOMP_MAP_ALWAYS_TOFROM:
37965 case GOMP_MAP_ALLOC:
37966 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37967 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37968 case GOMP_MAP_ALWAYS_POINTER:
37969 break;
37970 default:
37971 error_at (OMP_CLAUSE_LOCATION (*pc),
37972 "%<#pragma omp target%> with map-type other "
37973 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37974 "on %<map%> clause");
37975 *pc = OMP_CLAUSE_CHAIN (*pc);
37976 continue;
37978 pc = &OMP_CLAUSE_CHAIN (*pc);
37980 return true;
37983 /* OpenACC 2.0:
37984 # pragma acc cache (variable-list) new-line
37987 static tree
37988 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
37990 tree stmt, clauses;
37992 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
37993 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37995 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
37997 stmt = make_node (OACC_CACHE);
37998 TREE_TYPE (stmt) = void_type_node;
37999 OACC_CACHE_CLAUSES (stmt) = clauses;
38000 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38001 add_stmt (stmt);
38003 return stmt;
38006 /* OpenACC 2.0:
38007 # pragma acc data oacc-data-clause[optseq] new-line
38008 structured-block */
38010 #define OACC_DATA_CLAUSE_MASK \
38011 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38019 static tree
38020 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38022 tree stmt, clauses, block;
38023 unsigned int save;
38025 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
38026 "#pragma acc data", pragma_tok);
38028 block = begin_omp_parallel ();
38029 save = cp_parser_begin_omp_structured_block (parser);
38030 cp_parser_statement (parser, NULL_TREE, false, if_p);
38031 cp_parser_end_omp_structured_block (parser, save);
38032 stmt = finish_oacc_data (clauses, block);
38033 return stmt;
38036 /* OpenACC 2.0:
38037 # pragma acc host_data <clauses> new-line
38038 structured-block */
38040 #define OACC_HOST_DATA_CLAUSE_MASK \
38041 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38043 static tree
38044 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38046 tree stmt, clauses, block;
38047 unsigned int save;
38049 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
38050 "#pragma acc host_data", pragma_tok);
38052 block = begin_omp_parallel ();
38053 save = cp_parser_begin_omp_structured_block (parser);
38054 cp_parser_statement (parser, NULL_TREE, false, if_p);
38055 cp_parser_end_omp_structured_block (parser, save);
38056 stmt = finish_oacc_host_data (clauses, block);
38057 return stmt;
38060 /* OpenACC 2.0:
38061 # pragma acc declare oacc-data-clause[optseq] new-line
38064 #define OACC_DECLARE_CLAUSE_MASK \
38065 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38074 static tree
38075 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
38077 tree clauses, stmt;
38078 bool error = false;
38080 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
38081 "#pragma acc declare", pragma_tok, true);
38084 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38086 error_at (pragma_tok->location,
38087 "no valid clauses specified in %<#pragma acc declare%>");
38088 return NULL_TREE;
38091 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
38093 location_t loc = OMP_CLAUSE_LOCATION (t);
38094 tree decl = OMP_CLAUSE_DECL (t);
38095 if (!DECL_P (decl))
38097 error_at (loc, "array section in %<#pragma acc declare%>");
38098 error = true;
38099 continue;
38101 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
38102 switch (OMP_CLAUSE_MAP_KIND (t))
38104 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38105 case GOMP_MAP_ALLOC:
38106 case GOMP_MAP_TO:
38107 case GOMP_MAP_FORCE_DEVICEPTR:
38108 case GOMP_MAP_DEVICE_RESIDENT:
38109 break;
38111 case GOMP_MAP_LINK:
38112 if (!global_bindings_p ()
38113 && (TREE_STATIC (decl)
38114 || !DECL_EXTERNAL (decl)))
38116 error_at (loc,
38117 "%qD must be a global variable in "
38118 "%<#pragma acc declare link%>",
38119 decl);
38120 error = true;
38121 continue;
38123 break;
38125 default:
38126 if (global_bindings_p ())
38128 error_at (loc, "invalid OpenACC clause at file scope");
38129 error = true;
38130 continue;
38132 if (DECL_EXTERNAL (decl))
38134 error_at (loc,
38135 "invalid use of %<extern%> variable %qD "
38136 "in %<#pragma acc declare%>", decl);
38137 error = true;
38138 continue;
38140 else if (TREE_PUBLIC (decl))
38142 error_at (loc,
38143 "invalid use of %<global%> variable %qD "
38144 "in %<#pragma acc declare%>", decl);
38145 error = true;
38146 continue;
38148 break;
38151 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
38152 || lookup_attribute ("omp declare target link",
38153 DECL_ATTRIBUTES (decl)))
38155 error_at (loc, "variable %qD used more than once with "
38156 "%<#pragma acc declare%>", decl);
38157 error = true;
38158 continue;
38161 if (!error)
38163 tree id;
38165 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
38166 id = get_identifier ("omp declare target link");
38167 else
38168 id = get_identifier ("omp declare target");
38170 DECL_ATTRIBUTES (decl)
38171 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
38172 if (global_bindings_p ())
38174 symtab_node *node = symtab_node::get (decl);
38175 if (node != NULL)
38177 node->offloadable = 1;
38178 if (ENABLE_OFFLOADING)
38180 g->have_offload = true;
38181 if (is_a <varpool_node *> (node))
38182 vec_safe_push (offload_vars, decl);
38189 if (error || global_bindings_p ())
38190 return NULL_TREE;
38192 stmt = make_node (OACC_DECLARE);
38193 TREE_TYPE (stmt) = void_type_node;
38194 OACC_DECLARE_CLAUSES (stmt) = clauses;
38195 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38197 add_stmt (stmt);
38199 return NULL_TREE;
38202 /* OpenACC 2.0:
38203 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38207 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38209 LOC is the location of the #pragma token.
38212 #define OACC_ENTER_DATA_CLAUSE_MASK \
38213 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38219 #define OACC_EXIT_DATA_CLAUSE_MASK \
38220 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38227 static tree
38228 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
38229 bool enter)
38231 location_t loc = pragma_tok->location;
38232 tree stmt, clauses;
38233 const char *p = "";
38235 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38236 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38238 if (strcmp (p, "data") != 0)
38240 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
38241 enter ? "enter" : "exit");
38242 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38243 return NULL_TREE;
38246 cp_lexer_consume_token (parser->lexer);
38248 if (enter)
38249 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
38250 "#pragma acc enter data", pragma_tok);
38251 else
38252 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
38253 "#pragma acc exit data", pragma_tok);
38255 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38257 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
38258 enter ? "enter" : "exit");
38259 return NULL_TREE;
38262 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
38263 TREE_TYPE (stmt) = void_type_node;
38264 OMP_STANDALONE_CLAUSES (stmt) = clauses;
38265 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38266 add_stmt (stmt);
38267 return stmt;
38270 /* OpenACC 2.0:
38271 # pragma acc loop oacc-loop-clause[optseq] new-line
38272 structured-block */
38274 #define OACC_LOOP_CLAUSE_MASK \
38275 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38286 static tree
38287 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
38288 omp_clause_mask mask, tree *cclauses, bool *if_p)
38290 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
38292 strcat (p_name, " loop");
38293 mask |= OACC_LOOP_CLAUSE_MASK;
38295 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
38296 cclauses == NULL);
38297 if (cclauses)
38299 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
38300 if (*cclauses)
38301 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
38302 if (clauses)
38303 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38306 tree block = begin_omp_structured_block ();
38307 int save = cp_parser_begin_omp_structured_block (parser);
38308 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
38309 cp_parser_end_omp_structured_block (parser, save);
38310 add_stmt (finish_omp_structured_block (block));
38312 return stmt;
38315 /* OpenACC 2.0:
38316 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38317 structured-block
38321 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38322 structured-block
38325 #define OACC_KERNELS_CLAUSE_MASK \
38326 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38340 #define OACC_PARALLEL_CLAUSE_MASK \
38341 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38358 static tree
38359 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
38360 char *p_name, bool *if_p)
38362 omp_clause_mask mask;
38363 enum tree_code code;
38364 switch (cp_parser_pragma_kind (pragma_tok))
38366 case PRAGMA_OACC_KERNELS:
38367 strcat (p_name, " kernels");
38368 mask = OACC_KERNELS_CLAUSE_MASK;
38369 code = OACC_KERNELS;
38370 break;
38371 case PRAGMA_OACC_PARALLEL:
38372 strcat (p_name, " parallel");
38373 mask = OACC_PARALLEL_CLAUSE_MASK;
38374 code = OACC_PARALLEL;
38375 break;
38376 default:
38377 gcc_unreachable ();
38380 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38382 const char *p
38383 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38384 if (strcmp (p, "loop") == 0)
38386 cp_lexer_consume_token (parser->lexer);
38387 tree block = begin_omp_parallel ();
38388 tree clauses;
38389 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
38390 if_p);
38391 return finish_omp_construct (code, block, clauses);
38395 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
38397 tree block = begin_omp_parallel ();
38398 unsigned int save = cp_parser_begin_omp_structured_block (parser);
38399 cp_parser_statement (parser, NULL_TREE, false, if_p);
38400 cp_parser_end_omp_structured_block (parser, save);
38401 return finish_omp_construct (code, block, clauses);
38404 /* OpenACC 2.0:
38405 # pragma acc update oacc-update-clause[optseq] new-line
38408 #define OACC_UPDATE_CLAUSE_MASK \
38409 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38416 static tree
38417 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
38419 tree stmt, clauses;
38421 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
38422 "#pragma acc update", pragma_tok);
38424 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38426 error_at (pragma_tok->location,
38427 "%<#pragma acc update%> must contain at least one "
38428 "%<device%> or %<host%> or %<self%> clause");
38429 return NULL_TREE;
38432 stmt = make_node (OACC_UPDATE);
38433 TREE_TYPE (stmt) = void_type_node;
38434 OACC_UPDATE_CLAUSES (stmt) = clauses;
38435 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38436 add_stmt (stmt);
38437 return stmt;
38440 /* OpenACC 2.0:
38441 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38443 LOC is the location of the #pragma token.
38446 #define OACC_WAIT_CLAUSE_MASK \
38447 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
38449 static tree
38450 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
38452 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
38453 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38455 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38456 list = cp_parser_oacc_wait_list (parser, loc, list);
38458 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
38459 "#pragma acc wait", pragma_tok);
38461 stmt = c_finish_oacc_wait (loc, list, clauses);
38462 stmt = finish_expr_stmt (stmt);
38464 return stmt;
38467 /* OpenMP 4.0:
38468 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
38470 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
38471 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
38475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
38476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
38478 static void
38479 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
38480 enum pragma_context context)
38482 bool first_p = parser->omp_declare_simd == NULL;
38483 cp_omp_declare_simd_data data;
38484 if (first_p)
38486 data.error_seen = false;
38487 data.fndecl_seen = false;
38488 data.tokens = vNULL;
38489 data.clauses = NULL_TREE;
38490 /* It is safe to take the address of a local variable; it will only be
38491 used while this scope is live. */
38492 parser->omp_declare_simd = &data;
38495 /* Store away all pragma tokens. */
38496 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38497 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38498 cp_lexer_consume_token (parser->lexer);
38499 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38500 parser->omp_declare_simd->error_seen = true;
38501 cp_parser_require_pragma_eol (parser, pragma_tok);
38502 struct cp_token_cache *cp
38503 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38504 parser->omp_declare_simd->tokens.safe_push (cp);
38506 if (first_p)
38508 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38509 cp_parser_pragma (parser, context, NULL);
38510 switch (context)
38512 case pragma_external:
38513 cp_parser_declaration (parser);
38514 break;
38515 case pragma_member:
38516 cp_parser_member_declaration (parser);
38517 break;
38518 case pragma_objc_icode:
38519 cp_parser_block_declaration (parser, /*statement_p=*/false);
38520 break;
38521 default:
38522 cp_parser_declaration_statement (parser);
38523 break;
38525 if (parser->omp_declare_simd
38526 && !parser->omp_declare_simd->error_seen
38527 && !parser->omp_declare_simd->fndecl_seen)
38528 error_at (pragma_tok->location,
38529 "%<#pragma omp declare simd%> not immediately followed by "
38530 "function declaration or definition");
38531 data.tokens.release ();
38532 parser->omp_declare_simd = NULL;
38536 /* Finalize #pragma omp declare simd clauses after direct declarator has
38537 been parsed, and put that into "omp declare simd" attribute. */
38539 static tree
38540 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
38542 struct cp_token_cache *ce;
38543 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
38544 int i;
38546 if (!data->error_seen && data->fndecl_seen)
38548 error ("%<#pragma omp declare simd%> not immediately followed by "
38549 "a single function declaration or definition");
38550 data->error_seen = true;
38552 if (data->error_seen)
38553 return attrs;
38555 FOR_EACH_VEC_ELT (data->tokens, i, ce)
38557 tree c, cl;
38559 cp_parser_push_lexer_for_tokens (parser, ce);
38560 parser->lexer->in_pragma = true;
38561 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38562 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38563 cp_lexer_consume_token (parser->lexer);
38564 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
38565 "#pragma omp declare simd", pragma_tok);
38566 cp_parser_pop_lexer (parser);
38567 if (cl)
38568 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
38569 c = build_tree_list (get_identifier ("omp declare simd"), cl);
38570 TREE_CHAIN (c) = attrs;
38571 if (processing_template_decl)
38572 ATTR_IS_DEPENDENT (c) = 1;
38573 attrs = c;
38576 data->fndecl_seen = true;
38577 return attrs;
38581 /* OpenMP 4.0:
38582 # pragma omp declare target new-line
38583 declarations and definitions
38584 # pragma omp end declare target new-line
38586 OpenMP 4.5:
38587 # pragma omp declare target ( extended-list ) new-line
38589 # pragma omp declare target declare-target-clauses[seq] new-line */
38591 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
38592 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
38595 static void
38596 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
38598 tree clauses = NULL_TREE;
38599 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38600 clauses
38601 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
38602 "#pragma omp declare target", pragma_tok);
38603 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38605 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
38606 clauses);
38607 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
38608 cp_parser_require_pragma_eol (parser, pragma_tok);
38610 else
38612 cp_parser_require_pragma_eol (parser, pragma_tok);
38613 scope_chain->omp_declare_target_attribute++;
38614 return;
38616 if (scope_chain->omp_declare_target_attribute)
38617 error_at (pragma_tok->location,
38618 "%<#pragma omp declare target%> with clauses in between "
38619 "%<#pragma omp declare target%> without clauses and "
38620 "%<#pragma omp end declare target%>");
38621 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
38623 tree t = OMP_CLAUSE_DECL (c), id;
38624 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
38625 tree at2 = lookup_attribute ("omp declare target link",
38626 DECL_ATTRIBUTES (t));
38627 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
38629 id = get_identifier ("omp declare target link");
38630 std::swap (at1, at2);
38632 else
38633 id = get_identifier ("omp declare target");
38634 if (at2)
38636 error_at (OMP_CLAUSE_LOCATION (c),
38637 "%qD specified both in declare target %<link%> and %<to%>"
38638 " clauses", t);
38639 continue;
38641 if (!at1)
38643 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
38644 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
38645 continue;
38647 symtab_node *node = symtab_node::get (t);
38648 if (node != NULL)
38650 node->offloadable = 1;
38651 if (ENABLE_OFFLOADING)
38653 g->have_offload = true;
38654 if (is_a <varpool_node *> (node))
38655 vec_safe_push (offload_vars, t);
38662 static void
38663 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
38665 const char *p = "";
38666 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38668 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38669 p = IDENTIFIER_POINTER (id);
38671 if (strcmp (p, "declare") == 0)
38673 cp_lexer_consume_token (parser->lexer);
38674 p = "";
38675 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38677 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38678 p = IDENTIFIER_POINTER (id);
38680 if (strcmp (p, "target") == 0)
38681 cp_lexer_consume_token (parser->lexer);
38682 else
38684 cp_parser_error (parser, "expected %<target%>");
38685 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38686 return;
38689 else
38691 cp_parser_error (parser, "expected %<declare%>");
38692 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38693 return;
38695 cp_parser_require_pragma_eol (parser, pragma_tok);
38696 if (!scope_chain->omp_declare_target_attribute)
38697 error_at (pragma_tok->location,
38698 "%<#pragma omp end declare target%> without corresponding "
38699 "%<#pragma omp declare target%>");
38700 else
38701 scope_chain->omp_declare_target_attribute--;
38704 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
38705 expression and optional initializer clause of
38706 #pragma omp declare reduction. We store the expression(s) as
38707 either 3, 6 or 7 special statements inside of the artificial function's
38708 body. The first two statements are DECL_EXPRs for the artificial
38709 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
38710 expression that uses those variables.
38711 If there was any INITIALIZER clause, this is followed by further statements,
38712 the fourth and fifth statements are DECL_EXPRs for the artificial
38713 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
38714 constructor variant (first token after open paren is not omp_priv),
38715 then the sixth statement is a statement with the function call expression
38716 that uses the OMP_PRIV and optionally OMP_ORIG variable.
38717 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
38718 to initialize the OMP_PRIV artificial variable and there is seventh
38719 statement, a DECL_EXPR of the OMP_PRIV statement again. */
38721 static bool
38722 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
38724 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
38725 gcc_assert (TYPE_REF_P (type));
38726 type = TREE_TYPE (type);
38727 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
38728 DECL_ARTIFICIAL (omp_out) = 1;
38729 pushdecl (omp_out);
38730 add_decl_expr (omp_out);
38731 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
38732 DECL_ARTIFICIAL (omp_in) = 1;
38733 pushdecl (omp_in);
38734 add_decl_expr (omp_in);
38735 tree combiner;
38736 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
38738 keep_next_level (true);
38739 tree block = begin_omp_structured_block ();
38740 combiner = cp_parser_expression (parser);
38741 finish_expr_stmt (combiner);
38742 block = finish_omp_structured_block (block);
38743 add_stmt (block);
38745 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38746 return false;
38748 const char *p = "";
38749 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38751 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38752 p = IDENTIFIER_POINTER (id);
38755 if (strcmp (p, "initializer") == 0)
38757 cp_lexer_consume_token (parser->lexer);
38758 matching_parens parens;
38759 if (!parens.require_open (parser))
38760 return false;
38762 p = "";
38763 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38765 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38766 p = IDENTIFIER_POINTER (id);
38769 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
38770 DECL_ARTIFICIAL (omp_priv) = 1;
38771 pushdecl (omp_priv);
38772 add_decl_expr (omp_priv);
38773 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
38774 DECL_ARTIFICIAL (omp_orig) = 1;
38775 pushdecl (omp_orig);
38776 add_decl_expr (omp_orig);
38778 keep_next_level (true);
38779 block = begin_omp_structured_block ();
38781 bool ctor = false;
38782 if (strcmp (p, "omp_priv") == 0)
38784 bool is_direct_init, is_non_constant_init;
38785 ctor = true;
38786 cp_lexer_consume_token (parser->lexer);
38787 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
38788 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
38789 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
38790 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
38791 == CPP_CLOSE_PAREN
38792 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
38793 == CPP_CLOSE_PAREN))
38795 finish_omp_structured_block (block);
38796 error ("invalid initializer clause");
38797 return false;
38799 initializer = cp_parser_initializer (parser, &is_direct_init,
38800 &is_non_constant_init);
38801 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
38802 NULL_TREE, LOOKUP_ONLYCONVERTING);
38804 else
38806 cp_parser_parse_tentatively (parser);
38807 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
38808 /*check_dependency_p=*/true,
38809 /*template_p=*/NULL,
38810 /*declarator_p=*/false,
38811 /*optional_p=*/false);
38812 vec<tree, va_gc> *args;
38813 if (fn_name == error_mark_node
38814 || cp_parser_error_occurred (parser)
38815 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
38816 || ((args = cp_parser_parenthesized_expression_list
38817 (parser, non_attr, /*cast_p=*/false,
38818 /*allow_expansion_p=*/true,
38819 /*non_constant_p=*/NULL)),
38820 cp_parser_error_occurred (parser)))
38822 finish_omp_structured_block (block);
38823 cp_parser_abort_tentative_parse (parser);
38824 cp_parser_error (parser, "expected id-expression (arguments)");
38825 return false;
38827 unsigned int i;
38828 tree arg;
38829 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
38830 if (arg == omp_priv
38831 || (TREE_CODE (arg) == ADDR_EXPR
38832 && TREE_OPERAND (arg, 0) == omp_priv))
38833 break;
38834 cp_parser_abort_tentative_parse (parser);
38835 if (arg == NULL_TREE)
38836 error ("one of the initializer call arguments should be %<omp_priv%>"
38837 " or %<&omp_priv%>");
38838 initializer = cp_parser_postfix_expression (parser, false, false, false,
38839 false, NULL);
38840 finish_expr_stmt (initializer);
38843 block = finish_omp_structured_block (block);
38844 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
38845 add_stmt (block);
38847 if (ctor)
38848 add_decl_expr (omp_orig);
38850 if (!parens.require_close (parser))
38851 return false;
38854 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
38855 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
38856 UNKNOWN_LOCATION);
38858 return true;
38861 /* OpenMP 4.0
38862 #pragma omp declare reduction (reduction-id : typename-list : expression) \
38863 initializer-clause[opt] new-line
38865 initializer-clause:
38866 initializer (omp_priv initializer)
38867 initializer (function-name (argument-list)) */
38869 static void
38870 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
38871 enum pragma_context)
38873 auto_vec<tree> types;
38874 enum tree_code reduc_code = ERROR_MARK;
38875 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
38876 unsigned int i;
38877 cp_token *first_token;
38878 cp_token_cache *cp;
38879 int errs;
38880 void *p;
38882 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
38883 p = obstack_alloc (&declarator_obstack, 0);
38885 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38886 goto fail;
38888 switch (cp_lexer_peek_token (parser->lexer)->type)
38890 case CPP_PLUS:
38891 reduc_code = PLUS_EXPR;
38892 break;
38893 case CPP_MULT:
38894 reduc_code = MULT_EXPR;
38895 break;
38896 case CPP_MINUS:
38897 reduc_code = MINUS_EXPR;
38898 break;
38899 case CPP_AND:
38900 reduc_code = BIT_AND_EXPR;
38901 break;
38902 case CPP_XOR:
38903 reduc_code = BIT_XOR_EXPR;
38904 break;
38905 case CPP_OR:
38906 reduc_code = BIT_IOR_EXPR;
38907 break;
38908 case CPP_AND_AND:
38909 reduc_code = TRUTH_ANDIF_EXPR;
38910 break;
38911 case CPP_OR_OR:
38912 reduc_code = TRUTH_ORIF_EXPR;
38913 break;
38914 case CPP_NAME:
38915 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
38916 break;
38917 default:
38918 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
38919 "%<|%>, %<&&%>, %<||%> or identifier");
38920 goto fail;
38923 if (reduc_code != ERROR_MARK)
38924 cp_lexer_consume_token (parser->lexer);
38926 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
38927 if (reduc_id == error_mark_node)
38928 goto fail;
38930 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38931 goto fail;
38933 /* Types may not be defined in declare reduction type list. */
38934 const char *saved_message;
38935 saved_message = parser->type_definition_forbidden_message;
38936 parser->type_definition_forbidden_message
38937 = G_("types may not be defined in declare reduction type list");
38938 bool saved_colon_corrects_to_scope_p;
38939 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38940 parser->colon_corrects_to_scope_p = false;
38941 bool saved_colon_doesnt_start_class_def_p;
38942 saved_colon_doesnt_start_class_def_p
38943 = parser->colon_doesnt_start_class_def_p;
38944 parser->colon_doesnt_start_class_def_p = true;
38946 while (true)
38948 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38949 type = cp_parser_type_id (parser);
38950 if (type == error_mark_node)
38952 else if (ARITHMETIC_TYPE_P (type)
38953 && (orig_reduc_id == NULL_TREE
38954 || (TREE_CODE (type) != COMPLEX_TYPE
38955 && (id_equal (orig_reduc_id, "min")
38956 || id_equal (orig_reduc_id, "max")))))
38957 error_at (loc, "predeclared arithmetic type %qT in "
38958 "%<#pragma omp declare reduction%>", type);
38959 else if (TREE_CODE (type) == FUNCTION_TYPE
38960 || TREE_CODE (type) == METHOD_TYPE
38961 || TREE_CODE (type) == ARRAY_TYPE)
38962 error_at (loc, "function or array type %qT in "
38963 "%<#pragma omp declare reduction%>", type);
38964 else if (TYPE_REF_P (type))
38965 error_at (loc, "reference type %qT in "
38966 "%<#pragma omp declare reduction%>", type);
38967 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
38968 error_at (loc, "const, volatile or __restrict qualified type %qT in "
38969 "%<#pragma omp declare reduction%>", type);
38970 else
38971 types.safe_push (type);
38973 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38974 cp_lexer_consume_token (parser->lexer);
38975 else
38976 break;
38979 /* Restore the saved message. */
38980 parser->type_definition_forbidden_message = saved_message;
38981 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38982 parser->colon_doesnt_start_class_def_p
38983 = saved_colon_doesnt_start_class_def_p;
38985 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
38986 || types.is_empty ())
38988 fail:
38989 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38990 goto done;
38993 first_token = cp_lexer_peek_token (parser->lexer);
38994 cp = NULL;
38995 errs = errorcount;
38996 FOR_EACH_VEC_ELT (types, i, type)
38998 tree fntype
38999 = build_function_type_list (void_type_node,
39000 cp_build_reference_type (type, false),
39001 NULL_TREE);
39002 tree this_reduc_id = reduc_id;
39003 if (!dependent_type_p (type))
39004 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
39005 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
39006 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
39007 DECL_ARTIFICIAL (fndecl) = 1;
39008 DECL_EXTERNAL (fndecl) = 1;
39009 DECL_DECLARED_INLINE_P (fndecl) = 1;
39010 DECL_IGNORED_P (fndecl) = 1;
39011 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
39012 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
39013 DECL_ATTRIBUTES (fndecl)
39014 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
39015 DECL_ATTRIBUTES (fndecl));
39016 if (processing_template_decl)
39017 fndecl = push_template_decl (fndecl);
39018 bool block_scope = false;
39019 tree block = NULL_TREE;
39020 if (current_function_decl)
39022 block_scope = true;
39023 DECL_CONTEXT (fndecl) = global_namespace;
39024 if (!processing_template_decl)
39025 pushdecl (fndecl);
39027 else if (current_class_type)
39029 if (cp == NULL)
39031 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39032 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39033 cp_lexer_consume_token (parser->lexer);
39034 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39035 goto fail;
39036 cp = cp_token_cache_new (first_token,
39037 cp_lexer_peek_nth_token (parser->lexer,
39038 2));
39040 DECL_STATIC_FUNCTION_P (fndecl) = 1;
39041 finish_member_declaration (fndecl);
39042 DECL_PENDING_INLINE_INFO (fndecl) = cp;
39043 DECL_PENDING_INLINE_P (fndecl) = 1;
39044 vec_safe_push (unparsed_funs_with_definitions, fndecl);
39045 continue;
39047 else
39049 DECL_CONTEXT (fndecl) = current_namespace;
39050 pushdecl (fndecl);
39052 if (!block_scope)
39053 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
39054 else
39055 block = begin_omp_structured_block ();
39056 if (cp)
39058 cp_parser_push_lexer_for_tokens (parser, cp);
39059 parser->lexer->in_pragma = true;
39061 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
39063 if (!block_scope)
39064 finish_function (/*inline_p=*/false);
39065 else
39066 DECL_CONTEXT (fndecl) = current_function_decl;
39067 if (cp)
39068 cp_parser_pop_lexer (parser);
39069 goto fail;
39071 if (cp)
39072 cp_parser_pop_lexer (parser);
39073 if (!block_scope)
39074 finish_function (/*inline_p=*/false);
39075 else
39077 DECL_CONTEXT (fndecl) = current_function_decl;
39078 block = finish_omp_structured_block (block);
39079 if (TREE_CODE (block) == BIND_EXPR)
39080 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
39081 else if (TREE_CODE (block) == STATEMENT_LIST)
39082 DECL_SAVED_TREE (fndecl) = block;
39083 if (processing_template_decl)
39084 add_decl_expr (fndecl);
39086 cp_check_omp_declare_reduction (fndecl);
39087 if (cp == NULL && types.length () > 1)
39088 cp = cp_token_cache_new (first_token,
39089 cp_lexer_peek_nth_token (parser->lexer, 2));
39090 if (errs != errorcount)
39091 break;
39094 cp_parser_require_pragma_eol (parser, pragma_tok);
39096 done:
39097 /* Free any declarators allocated. */
39098 obstack_free (&declarator_obstack, p);
39101 /* OpenMP 4.0
39102 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39103 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39104 initializer-clause[opt] new-line
39105 #pragma omp declare target new-line */
39107 static bool
39108 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
39109 enum pragma_context context)
39111 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39113 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39114 const char *p = IDENTIFIER_POINTER (id);
39116 if (strcmp (p, "simd") == 0)
39118 cp_lexer_consume_token (parser->lexer);
39119 cp_parser_omp_declare_simd (parser, pragma_tok,
39120 context);
39121 return true;
39123 cp_ensure_no_omp_declare_simd (parser);
39124 if (strcmp (p, "reduction") == 0)
39126 cp_lexer_consume_token (parser->lexer);
39127 cp_parser_omp_declare_reduction (parser, pragma_tok,
39128 context);
39129 return false;
39131 if (!flag_openmp) /* flag_openmp_simd */
39133 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39134 return false;
39136 if (strcmp (p, "target") == 0)
39138 cp_lexer_consume_token (parser->lexer);
39139 cp_parser_omp_declare_target (parser, pragma_tok);
39140 return false;
39143 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
39144 "or %<target%>");
39145 cp_parser_require_pragma_eol (parser, pragma_tok);
39146 return false;
39149 /* OpenMP 5.0
39150 #pragma omp requires clauses[optseq] new-line */
39152 static bool
39153 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
39155 bool first = true;
39156 enum omp_requires new_req = (enum omp_requires) 0;
39158 location_t loc = pragma_tok->location;
39159 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39161 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39162 cp_lexer_consume_token (parser->lexer);
39164 first = false;
39166 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39168 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39169 const char *p = IDENTIFIER_POINTER (id);
39170 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
39171 enum omp_requires this_req = (enum omp_requires) 0;
39173 if (!strcmp (p, "unified_address"))
39174 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
39175 else if (!strcmp (p, "unified_shared_memory"))
39176 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
39177 else if (!strcmp (p, "dynamic_allocators"))
39178 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
39179 else if (!strcmp (p, "reverse_offload"))
39180 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
39181 else if (!strcmp (p, "atomic_default_mem_order"))
39183 cp_lexer_consume_token (parser->lexer);
39185 matching_parens parens;
39186 if (parens.require_open (parser))
39188 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39190 id = cp_lexer_peek_token (parser->lexer)->u.value;
39191 p = IDENTIFIER_POINTER (id);
39193 if (!strcmp (p, "seq_cst"))
39194 this_req
39195 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
39196 else if (!strcmp (p, "relaxed"))
39197 this_req
39198 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
39199 else if (!strcmp (p, "acq_rel"))
39200 this_req
39201 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
39203 if (this_req == 0)
39205 error_at (cp_lexer_peek_token (parser->lexer)->location,
39206 "expected %<seq_cst%>, %<relaxed%> or "
39207 "%<acq_rel%>");
39208 if (cp_lexer_nth_token_is (parser->lexer, 2,
39209 CPP_CLOSE_PAREN))
39210 cp_lexer_consume_token (parser->lexer);
39212 else
39213 cp_lexer_consume_token (parser->lexer);
39215 if (!parens.require_close (parser))
39216 cp_parser_skip_to_closing_parenthesis (parser,
39217 /*recovering=*/true,
39218 /*or_comma=*/false,
39219 /*consume_paren=*/
39220 true);
39222 if (this_req == 0)
39224 cp_parser_require_pragma_eol (parser, pragma_tok);
39225 return false;
39228 p = NULL;
39230 else
39232 error_at (cloc, "expected %<unified_address%>, "
39233 "%<unified_shared_memory%>, "
39234 "%<dynamic_allocators%>, "
39235 "%<reverse_offload%> "
39236 "or %<atomic_default_mem_order%> clause");
39237 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39238 return false;
39240 if (p)
39241 sorry_at (cloc, "%qs clause on %<requires%> directive not "
39242 "supported yet", p);
39243 if (p)
39244 cp_lexer_consume_token (parser->lexer);
39245 if (this_req)
39247 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39249 if ((this_req & new_req) != 0)
39250 error_at (cloc, "too many %qs clauses", p);
39251 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
39252 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
39253 error_at (cloc, "%qs clause used lexically after first "
39254 "target construct or offloading API", p);
39256 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39258 error_at (cloc, "too many %qs clauses",
39259 "atomic_default_mem_order");
39260 this_req = (enum omp_requires) 0;
39262 else if ((omp_requires_mask
39263 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39265 error_at (cloc, "more than one %<atomic_default_mem_order%>"
39266 " clause in a single compilation unit");
39267 this_req
39268 = (enum omp_requires)
39269 (omp_requires_mask
39270 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
39272 else if ((omp_requires_mask
39273 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
39274 error_at (cloc, "%<atomic_default_mem_order%> clause used "
39275 "lexically after first %<atomic%> construct "
39276 "without memory order clause");
39277 new_req = (enum omp_requires) (new_req | this_req);
39278 omp_requires_mask
39279 = (enum omp_requires) (omp_requires_mask | this_req);
39280 continue;
39283 break;
39285 cp_parser_require_pragma_eol (parser, pragma_tok);
39287 if (new_req == 0)
39288 error_at (loc, "%<pragma omp requires%> requires at least one clause");
39289 return false;
39293 /* OpenMP 4.5:
39294 #pragma omp taskloop taskloop-clause[optseq] new-line
39295 for-loop
39297 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39298 for-loop */
39300 #define OMP_TASKLOOP_CLAUSE_MASK \
39301 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39307 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39318 static tree
39319 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
39320 char *p_name, omp_clause_mask mask, tree *cclauses,
39321 bool *if_p)
39323 tree clauses, sb, ret;
39324 unsigned int save;
39325 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39327 strcat (p_name, " taskloop");
39328 mask |= OMP_TASKLOOP_CLAUSE_MASK;
39329 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39330 clause. */
39331 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
39332 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
39334 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39336 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39337 const char *p = IDENTIFIER_POINTER (id);
39339 if (strcmp (p, "simd") == 0)
39341 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39342 if (cclauses == NULL)
39343 cclauses = cclauses_buf;
39345 cp_lexer_consume_token (parser->lexer);
39346 if (!flag_openmp) /* flag_openmp_simd */
39347 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39348 cclauses, if_p);
39349 sb = begin_omp_structured_block ();
39350 save = cp_parser_begin_omp_structured_block (parser);
39351 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39352 cclauses, if_p);
39353 cp_parser_end_omp_structured_block (parser, save);
39354 tree body = finish_omp_structured_block (sb);
39355 if (ret == NULL)
39356 return ret;
39357 ret = make_node (OMP_TASKLOOP);
39358 TREE_TYPE (ret) = void_type_node;
39359 OMP_FOR_BODY (ret) = body;
39360 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39361 SET_EXPR_LOCATION (ret, loc);
39362 add_stmt (ret);
39363 return ret;
39366 if (!flag_openmp) /* flag_openmp_simd */
39368 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39369 return NULL_TREE;
39372 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39373 cclauses == NULL);
39374 if (cclauses)
39376 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
39377 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39380 keep_next_level (true);
39381 sb = begin_omp_structured_block ();
39382 save = cp_parser_begin_omp_structured_block (parser);
39384 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
39385 if_p);
39387 cp_parser_end_omp_structured_block (parser, save);
39388 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39390 return ret;
39394 /* OpenACC 2.0:
39395 # pragma acc routine oacc-routine-clause[optseq] new-line
39396 function-definition
39398 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39401 #define OACC_ROUTINE_CLAUSE_MASK \
39402 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39408 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39409 component, which must resolve to a declared namespace-scope
39410 function. The clauses are either processed directly (for a named
39411 function), or defered until the immediatley following declaration
39412 is parsed. */
39414 static void
39415 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
39416 enum pragma_context context)
39418 gcc_checking_assert (context == pragma_external);
39419 /* The checking for "another pragma following this one" in the "no optional
39420 '( name )'" case makes sure that we dont re-enter. */
39421 gcc_checking_assert (parser->oacc_routine == NULL);
39423 cp_oacc_routine_data data;
39424 data.error_seen = false;
39425 data.fndecl_seen = false;
39426 data.tokens = vNULL;
39427 data.clauses = NULL_TREE;
39428 data.loc = pragma_tok->location;
39429 /* It is safe to take the address of a local variable; it will only be
39430 used while this scope is live. */
39431 parser->oacc_routine = &data;
39433 /* Look for optional '( name )'. */
39434 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39436 matching_parens parens;
39437 parens.consume_open (parser); /* '(' */
39439 /* We parse the name as an id-expression. If it resolves to
39440 anything other than a non-overloaded function at namespace
39441 scope, it's an error. */
39442 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
39443 tree name = cp_parser_id_expression (parser,
39444 /*template_keyword_p=*/false,
39445 /*check_dependency_p=*/false,
39446 /*template_p=*/NULL,
39447 /*declarator_p=*/false,
39448 /*optional_p=*/false);
39449 tree decl = (identifier_p (name)
39450 ? cp_parser_lookup_name_simple (parser, name, name_loc)
39451 : name);
39452 if (name != error_mark_node && decl == error_mark_node)
39453 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
39455 if (decl == error_mark_node
39456 || !parens.require_close (parser))
39458 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39459 parser->oacc_routine = NULL;
39460 return;
39463 data.clauses
39464 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39465 "#pragma acc routine",
39466 cp_lexer_peek_token (parser->lexer));
39468 if (decl && is_overloaded_fn (decl)
39469 && (TREE_CODE (decl) != FUNCTION_DECL
39470 || DECL_FUNCTION_TEMPLATE_P (decl)))
39472 error_at (name_loc,
39473 "%<#pragma acc routine%> names a set of overloads");
39474 parser->oacc_routine = NULL;
39475 return;
39478 /* Perhaps we should use the same rule as declarations in different
39479 namespaces? */
39480 if (!DECL_NAMESPACE_SCOPE_P (decl))
39482 error_at (name_loc,
39483 "%qD does not refer to a namespace scope function", decl);
39484 parser->oacc_routine = NULL;
39485 return;
39488 if (TREE_CODE (decl) != FUNCTION_DECL)
39490 error_at (name_loc, "%qD does not refer to a function", decl);
39491 parser->oacc_routine = NULL;
39492 return;
39495 cp_finalize_oacc_routine (parser, decl, false);
39496 parser->oacc_routine = NULL;
39498 else /* No optional '( name )'. */
39500 /* Store away all pragma tokens. */
39501 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39502 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39503 cp_lexer_consume_token (parser->lexer);
39504 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39505 parser->oacc_routine->error_seen = true;
39506 cp_parser_require_pragma_eol (parser, pragma_tok);
39507 struct cp_token_cache *cp
39508 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39509 parser->oacc_routine->tokens.safe_push (cp);
39511 /* Emit a helpful diagnostic if there's another pragma following this
39512 one. */
39513 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39515 cp_ensure_no_oacc_routine (parser);
39516 data.tokens.release ();
39517 /* ..., and then just keep going. */
39518 return;
39521 /* We only have to consider the pragma_external case here. */
39522 cp_parser_declaration (parser);
39523 if (parser->oacc_routine
39524 && !parser->oacc_routine->fndecl_seen)
39525 cp_ensure_no_oacc_routine (parser);
39526 else
39527 parser->oacc_routine = NULL;
39528 data.tokens.release ();
39532 /* Finalize #pragma acc routine clauses after direct declarator has
39533 been parsed. */
39535 static tree
39536 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
39538 struct cp_token_cache *ce;
39539 cp_oacc_routine_data *data = parser->oacc_routine;
39541 if (!data->error_seen && data->fndecl_seen)
39543 error_at (data->loc,
39544 "%<#pragma acc routine%> not immediately followed by "
39545 "a single function declaration or definition");
39546 data->error_seen = true;
39548 if (data->error_seen)
39549 return attrs;
39551 gcc_checking_assert (data->tokens.length () == 1);
39552 ce = data->tokens[0];
39554 cp_parser_push_lexer_for_tokens (parser, ce);
39555 parser->lexer->in_pragma = true;
39556 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39558 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39559 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
39560 parser->oacc_routine->clauses
39561 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39562 "#pragma acc routine", pragma_tok);
39563 cp_parser_pop_lexer (parser);
39564 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
39565 fndecl_seen. */
39567 return attrs;
39570 /* Apply any saved OpenACC routine clauses to a just-parsed
39571 declaration. */
39573 static void
39574 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
39576 if (__builtin_expect (parser->oacc_routine != NULL, 0))
39578 /* Keep going if we're in error reporting mode. */
39579 if (parser->oacc_routine->error_seen
39580 || fndecl == error_mark_node)
39581 return;
39583 if (parser->oacc_routine->fndecl_seen)
39585 error_at (parser->oacc_routine->loc,
39586 "%<#pragma acc routine%> not immediately followed by"
39587 " a single function declaration or definition");
39588 parser->oacc_routine = NULL;
39589 return;
39591 if (TREE_CODE (fndecl) != FUNCTION_DECL)
39593 cp_ensure_no_oacc_routine (parser);
39594 return;
39597 if (oacc_get_fn_attrib (fndecl))
39599 error_at (parser->oacc_routine->loc,
39600 "%<#pragma acc routine%> already applied to %qD", fndecl);
39601 parser->oacc_routine = NULL;
39602 return;
39605 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
39607 error_at (parser->oacc_routine->loc,
39608 TREE_USED (fndecl)
39609 ? G_("%<#pragma acc routine%> must be applied before use")
39610 : G_("%<#pragma acc routine%> must be applied before "
39611 "definition"));
39612 parser->oacc_routine = NULL;
39613 return;
39616 /* Process the routine's dimension clauses. */
39617 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
39618 oacc_replace_fn_attrib (fndecl, dims);
39620 /* Add an "omp declare target" attribute. */
39621 DECL_ATTRIBUTES (fndecl)
39622 = tree_cons (get_identifier ("omp declare target"),
39623 NULL_TREE, DECL_ATTRIBUTES (fndecl));
39625 /* Don't unset parser->oacc_routine here: we may still need it to
39626 diagnose wrong usage. But, remember that we've used this "#pragma acc
39627 routine". */
39628 parser->oacc_routine->fndecl_seen = true;
39632 /* Main entry point to OpenMP statement pragmas. */
39634 static void
39635 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39637 tree stmt;
39638 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
39639 omp_clause_mask mask (0);
39641 switch (cp_parser_pragma_kind (pragma_tok))
39643 case PRAGMA_OACC_ATOMIC:
39644 cp_parser_omp_atomic (parser, pragma_tok);
39645 return;
39646 case PRAGMA_OACC_CACHE:
39647 stmt = cp_parser_oacc_cache (parser, pragma_tok);
39648 break;
39649 case PRAGMA_OACC_DATA:
39650 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
39651 break;
39652 case PRAGMA_OACC_ENTER_DATA:
39653 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
39654 break;
39655 case PRAGMA_OACC_EXIT_DATA:
39656 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
39657 break;
39658 case PRAGMA_OACC_HOST_DATA:
39659 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
39660 break;
39661 case PRAGMA_OACC_KERNELS:
39662 case PRAGMA_OACC_PARALLEL:
39663 strcpy (p_name, "#pragma acc");
39664 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
39665 if_p);
39666 break;
39667 case PRAGMA_OACC_LOOP:
39668 strcpy (p_name, "#pragma acc");
39669 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
39670 if_p);
39671 break;
39672 case PRAGMA_OACC_UPDATE:
39673 stmt = cp_parser_oacc_update (parser, pragma_tok);
39674 break;
39675 case PRAGMA_OACC_WAIT:
39676 stmt = cp_parser_oacc_wait (parser, pragma_tok);
39677 break;
39678 case PRAGMA_OMP_ATOMIC:
39679 cp_parser_omp_atomic (parser, pragma_tok);
39680 return;
39681 case PRAGMA_OMP_CRITICAL:
39682 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
39683 break;
39684 case PRAGMA_OMP_DISTRIBUTE:
39685 strcpy (p_name, "#pragma omp");
39686 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
39687 if_p);
39688 break;
39689 case PRAGMA_OMP_FOR:
39690 strcpy (p_name, "#pragma omp");
39691 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
39692 if_p);
39693 break;
39694 case PRAGMA_OMP_MASTER:
39695 strcpy (p_name, "#pragma omp");
39696 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
39697 if_p);
39698 break;
39699 case PRAGMA_OMP_PARALLEL:
39700 strcpy (p_name, "#pragma omp");
39701 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
39702 if_p);
39703 break;
39704 case PRAGMA_OMP_SECTIONS:
39705 strcpy (p_name, "#pragma omp");
39706 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
39707 break;
39708 case PRAGMA_OMP_SIMD:
39709 strcpy (p_name, "#pragma omp");
39710 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
39711 if_p);
39712 break;
39713 case PRAGMA_OMP_SINGLE:
39714 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
39715 break;
39716 case PRAGMA_OMP_TASK:
39717 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
39718 break;
39719 case PRAGMA_OMP_TASKGROUP:
39720 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
39721 break;
39722 case PRAGMA_OMP_TASKLOOP:
39723 strcpy (p_name, "#pragma omp");
39724 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
39725 if_p);
39726 break;
39727 case PRAGMA_OMP_TEAMS:
39728 strcpy (p_name, "#pragma omp");
39729 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
39730 if_p);
39731 break;
39732 default:
39733 gcc_unreachable ();
39736 protected_set_expr_location (stmt, pragma_tok->location);
39739 /* Transactional Memory parsing routines. */
39741 /* Parse a transaction attribute.
39743 txn-attribute:
39744 attribute
39745 [ [ identifier ] ]
39747 We use this instead of cp_parser_attributes_opt for transactions to avoid
39748 the pedwarn in C++98 mode. */
39750 static tree
39751 cp_parser_txn_attribute_opt (cp_parser *parser)
39753 cp_token *token;
39754 tree attr_name, attr = NULL;
39756 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
39757 return cp_parser_attributes_opt (parser);
39759 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
39760 return NULL_TREE;
39761 cp_lexer_consume_token (parser->lexer);
39762 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
39763 goto error1;
39765 token = cp_lexer_peek_token (parser->lexer);
39766 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
39768 token = cp_lexer_consume_token (parser->lexer);
39770 attr_name = (token->type == CPP_KEYWORD
39771 /* For keywords, use the canonical spelling,
39772 not the parsed identifier. */
39773 ? ridpointers[(int) token->keyword]
39774 : token->u.value);
39775 attr = build_tree_list (attr_name, NULL_TREE);
39777 else
39778 cp_parser_error (parser, "expected identifier");
39780 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
39781 error1:
39782 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
39783 return attr;
39786 /* Parse a __transaction_atomic or __transaction_relaxed statement.
39788 transaction-statement:
39789 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
39790 compound-statement
39791 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
39794 static tree
39795 cp_parser_transaction (cp_parser *parser, cp_token *token)
39797 unsigned char old_in = parser->in_transaction;
39798 unsigned char this_in = 1, new_in;
39799 enum rid keyword = token->keyword;
39800 tree stmt, attrs, noex;
39802 cp_lexer_consume_token (parser->lexer);
39804 if (keyword == RID_TRANSACTION_RELAXED
39805 || keyword == RID_SYNCHRONIZED)
39806 this_in |= TM_STMT_ATTR_RELAXED;
39807 else
39809 attrs = cp_parser_txn_attribute_opt (parser);
39810 if (attrs)
39811 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
39814 /* Parse a noexcept specification. */
39815 if (keyword == RID_ATOMIC_NOEXCEPT)
39816 noex = boolean_true_node;
39817 else if (keyword == RID_ATOMIC_CANCEL)
39819 /* cancel-and-throw is unimplemented. */
39820 sorry ("atomic_cancel");
39821 noex = NULL_TREE;
39823 else
39824 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
39826 /* Keep track if we're in the lexical scope of an outer transaction. */
39827 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
39829 stmt = begin_transaction_stmt (token->location, NULL, this_in);
39831 parser->in_transaction = new_in;
39832 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
39833 parser->in_transaction = old_in;
39835 finish_transaction_stmt (stmt, NULL, this_in, noex);
39837 return stmt;
39840 /* Parse a __transaction_atomic or __transaction_relaxed expression.
39842 transaction-expression:
39843 __transaction_atomic txn-noexcept-spec[opt] ( expression )
39844 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
39847 static tree
39848 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
39850 unsigned char old_in = parser->in_transaction;
39851 unsigned char this_in = 1;
39852 cp_token *token;
39853 tree expr, noex;
39854 bool noex_expr;
39855 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39857 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
39858 || keyword == RID_TRANSACTION_RELAXED);
39860 if (!flag_tm)
39861 error_at (loc,
39862 keyword == RID_TRANSACTION_RELAXED
39863 ? G_("%<__transaction_relaxed%> without transactional memory "
39864 "support enabled")
39865 : G_("%<__transaction_atomic%> without transactional memory "
39866 "support enabled"));
39868 token = cp_parser_require_keyword (parser, keyword,
39869 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
39870 : RT_TRANSACTION_RELAXED));
39871 gcc_assert (token != NULL);
39873 if (keyword == RID_TRANSACTION_RELAXED)
39874 this_in |= TM_STMT_ATTR_RELAXED;
39876 /* Set this early. This might mean that we allow transaction_cancel in
39877 an expression that we find out later actually has to be a constexpr.
39878 However, we expect that cxx_constant_value will be able to deal with
39879 this; also, if the noexcept has no constexpr, then what we parse next
39880 really is a transaction's body. */
39881 parser->in_transaction = this_in;
39883 /* Parse a noexcept specification. */
39884 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
39885 true);
39887 if (!noex || !noex_expr
39888 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39890 matching_parens parens;
39891 parens.require_open (parser);
39893 expr = cp_parser_expression (parser);
39894 expr = finish_parenthesized_expr (expr);
39896 parens.require_close (parser);
39898 else
39900 /* The only expression that is available got parsed for the noexcept
39901 already. noexcept is true then. */
39902 expr = noex;
39903 noex = boolean_true_node;
39906 expr = build_transaction_expr (token->location, expr, this_in, noex);
39907 parser->in_transaction = old_in;
39909 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
39910 return error_mark_node;
39912 return (flag_tm ? expr : error_mark_node);
39915 /* Parse a function-transaction-block.
39917 function-transaction-block:
39918 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
39919 function-body
39920 __transaction_atomic txn-attribute[opt] function-try-block
39921 __transaction_relaxed ctor-initializer[opt] function-body
39922 __transaction_relaxed function-try-block
39925 static void
39926 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
39928 unsigned char old_in = parser->in_transaction;
39929 unsigned char new_in = 1;
39930 tree compound_stmt, stmt, attrs;
39931 cp_token *token;
39933 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
39934 || keyword == RID_TRANSACTION_RELAXED);
39935 token = cp_parser_require_keyword (parser, keyword,
39936 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
39937 : RT_TRANSACTION_RELAXED));
39938 gcc_assert (token != NULL);
39940 if (keyword == RID_TRANSACTION_RELAXED)
39941 new_in |= TM_STMT_ATTR_RELAXED;
39942 else
39944 attrs = cp_parser_txn_attribute_opt (parser);
39945 if (attrs)
39946 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
39949 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
39951 parser->in_transaction = new_in;
39953 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
39954 cp_parser_function_try_block (parser);
39955 else
39956 cp_parser_ctor_initializer_opt_and_function_body
39957 (parser, /*in_function_try_block=*/false);
39959 parser->in_transaction = old_in;
39961 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
39964 /* Parse a __transaction_cancel statement.
39966 cancel-statement:
39967 __transaction_cancel txn-attribute[opt] ;
39968 __transaction_cancel txn-attribute[opt] throw-expression ;
39970 ??? Cancel and throw is not yet implemented. */
39972 static tree
39973 cp_parser_transaction_cancel (cp_parser *parser)
39975 cp_token *token;
39976 bool is_outer = false;
39977 tree stmt, attrs;
39979 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
39980 RT_TRANSACTION_CANCEL);
39981 gcc_assert (token != NULL);
39983 attrs = cp_parser_txn_attribute_opt (parser);
39984 if (attrs)
39985 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
39987 /* ??? Parse cancel-and-throw here. */
39989 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
39991 if (!flag_tm)
39993 error_at (token->location, "%<__transaction_cancel%> without "
39994 "transactional memory support enabled");
39995 return error_mark_node;
39997 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
39999 error_at (token->location, "%<__transaction_cancel%> within a "
40000 "%<__transaction_relaxed%>");
40001 return error_mark_node;
40003 else if (is_outer)
40005 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
40006 && !is_tm_may_cancel_outer (current_function_decl))
40008 error_at (token->location, "outer %<__transaction_cancel%> not "
40009 "within outer %<__transaction_atomic%>");
40010 error_at (token->location,
40011 " or a %<transaction_may_cancel_outer%> function");
40012 return error_mark_node;
40015 else if (parser->in_transaction == 0)
40017 error_at (token->location, "%<__transaction_cancel%> not within "
40018 "%<__transaction_atomic%>");
40019 return error_mark_node;
40022 stmt = build_tm_abort_call (token->location, is_outer);
40023 add_stmt (stmt);
40025 return stmt;
40028 /* The parser. */
40030 static GTY (()) cp_parser *the_parser;
40033 /* Special handling for the first token or line in the file. The first
40034 thing in the file might be #pragma GCC pch_preprocess, which loads a
40035 PCH file, which is a GC collection point. So we need to handle this
40036 first pragma without benefit of an existing lexer structure.
40038 Always returns one token to the caller in *FIRST_TOKEN. This is
40039 either the true first token of the file, or the first token after
40040 the initial pragma. */
40042 static void
40043 cp_parser_initial_pragma (cp_token *first_token)
40045 tree name = NULL;
40047 cp_lexer_get_preprocessor_token (NULL, first_token);
40048 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
40049 return;
40051 cp_lexer_get_preprocessor_token (NULL, first_token);
40052 if (first_token->type == CPP_STRING)
40054 name = first_token->u.value;
40056 cp_lexer_get_preprocessor_token (NULL, first_token);
40057 if (first_token->type != CPP_PRAGMA_EOL)
40058 error_at (first_token->location,
40059 "junk at end of %<#pragma GCC pch_preprocess%>");
40061 else
40062 error_at (first_token->location, "expected string literal");
40064 /* Skip to the end of the pragma. */
40065 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
40066 cp_lexer_get_preprocessor_token (NULL, first_token);
40068 /* Now actually load the PCH file. */
40069 if (name)
40070 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
40072 /* Read one more token to return to our caller. We have to do this
40073 after reading the PCH file in, since its pointers have to be
40074 live. */
40075 cp_lexer_get_preprocessor_token (NULL, first_token);
40078 /* Parse a pragma GCC ivdep. */
40080 static bool
40081 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
40083 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40084 return true;
40087 /* Parse a pragma GCC unroll. */
40089 static unsigned short
40090 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
40092 location_t location = cp_lexer_peek_token (parser->lexer)->location;
40093 tree expr = cp_parser_constant_expression (parser);
40094 unsigned short unroll;
40095 expr = maybe_constant_value (expr);
40096 HOST_WIDE_INT lunroll = 0;
40097 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
40098 || TREE_CODE (expr) != INTEGER_CST
40099 || (lunroll = tree_to_shwi (expr)) < 0
40100 || lunroll >= USHRT_MAX)
40102 error_at (location, "%<#pragma GCC unroll%> requires an"
40103 " assignment-expression that evaluates to a non-negative"
40104 " integral constant less than %u", USHRT_MAX);
40105 unroll = 0;
40107 else
40109 unroll = (unsigned short)lunroll;
40110 if (unroll == 0)
40111 unroll = 1;
40113 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40114 return unroll;
40117 /* Normal parsing of a pragma token. Here we can (and must) use the
40118 regular lexer. */
40120 static bool
40121 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
40123 cp_token *pragma_tok;
40124 unsigned int id;
40125 tree stmt;
40126 bool ret;
40128 pragma_tok = cp_lexer_consume_token (parser->lexer);
40129 gcc_assert (pragma_tok->type == CPP_PRAGMA);
40130 parser->lexer->in_pragma = true;
40132 id = cp_parser_pragma_kind (pragma_tok);
40133 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
40134 cp_ensure_no_omp_declare_simd (parser);
40135 switch (id)
40137 case PRAGMA_GCC_PCH_PREPROCESS:
40138 error_at (pragma_tok->location,
40139 "%<#pragma GCC pch_preprocess%> must be first");
40140 break;
40142 case PRAGMA_OMP_BARRIER:
40143 switch (context)
40145 case pragma_compound:
40146 cp_parser_omp_barrier (parser, pragma_tok);
40147 return false;
40148 case pragma_stmt:
40149 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40150 "used in compound statements", "omp barrier");
40151 break;
40152 default:
40153 goto bad_stmt;
40155 break;
40157 case PRAGMA_OMP_DEPOBJ:
40158 switch (context)
40160 case pragma_compound:
40161 cp_parser_omp_depobj (parser, pragma_tok);
40162 return false;
40163 case pragma_stmt:
40164 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40165 "used in compound statements", "omp depobj");
40166 break;
40167 default:
40168 goto bad_stmt;
40170 break;
40172 case PRAGMA_OMP_FLUSH:
40173 switch (context)
40175 case pragma_compound:
40176 cp_parser_omp_flush (parser, pragma_tok);
40177 return false;
40178 case pragma_stmt:
40179 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40180 "used in compound statements", "omp flush");
40181 break;
40182 default:
40183 goto bad_stmt;
40185 break;
40187 case PRAGMA_OMP_TASKWAIT:
40188 switch (context)
40190 case pragma_compound:
40191 cp_parser_omp_taskwait (parser, pragma_tok);
40192 return false;
40193 case pragma_stmt:
40194 error_at (pragma_tok->location,
40195 "%<#pragma %s%> may only be used in compound statements",
40196 "omp taskwait");
40197 break;
40198 default:
40199 goto bad_stmt;
40201 break;
40203 case PRAGMA_OMP_TASKYIELD:
40204 switch (context)
40206 case pragma_compound:
40207 cp_parser_omp_taskyield (parser, pragma_tok);
40208 return false;
40209 case pragma_stmt:
40210 error_at (pragma_tok->location,
40211 "%<#pragma %s%> may only be used in compound statements",
40212 "omp taskyield");
40213 break;
40214 default:
40215 goto bad_stmt;
40217 break;
40219 case PRAGMA_OMP_CANCEL:
40220 switch (context)
40222 case pragma_compound:
40223 cp_parser_omp_cancel (parser, pragma_tok);
40224 return false;
40225 case pragma_stmt:
40226 error_at (pragma_tok->location,
40227 "%<#pragma %s%> may only be used in compound statements",
40228 "omp cancel");
40229 break;
40230 default:
40231 goto bad_stmt;
40233 break;
40235 case PRAGMA_OMP_CANCELLATION_POINT:
40236 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
40237 return false;
40239 case PRAGMA_OMP_THREADPRIVATE:
40240 cp_parser_omp_threadprivate (parser, pragma_tok);
40241 return false;
40243 case PRAGMA_OMP_DECLARE:
40244 return cp_parser_omp_declare (parser, pragma_tok, context);
40246 case PRAGMA_OACC_DECLARE:
40247 cp_parser_oacc_declare (parser, pragma_tok);
40248 return false;
40250 case PRAGMA_OACC_ENTER_DATA:
40251 if (context == pragma_stmt)
40253 error_at (pragma_tok->location,
40254 "%<#pragma %s%> may only be used in compound statements",
40255 "acc enter data");
40256 break;
40258 else if (context != pragma_compound)
40259 goto bad_stmt;
40260 cp_parser_omp_construct (parser, pragma_tok, if_p);
40261 return true;
40263 case PRAGMA_OACC_EXIT_DATA:
40264 if (context == pragma_stmt)
40266 error_at (pragma_tok->location,
40267 "%<#pragma %s%> may only be used in compound statements",
40268 "acc exit data");
40269 break;
40271 else if (context != pragma_compound)
40272 goto bad_stmt;
40273 cp_parser_omp_construct (parser, pragma_tok, if_p);
40274 return true;
40276 case PRAGMA_OACC_ROUTINE:
40277 if (context != pragma_external)
40279 error_at (pragma_tok->location,
40280 "%<#pragma acc routine%> must be at file scope");
40281 break;
40283 cp_parser_oacc_routine (parser, pragma_tok, context);
40284 return false;
40286 case PRAGMA_OACC_UPDATE:
40287 if (context == pragma_stmt)
40289 error_at (pragma_tok->location,
40290 "%<#pragma %s%> may only be used in compound statements",
40291 "acc update");
40292 break;
40294 else if (context != pragma_compound)
40295 goto bad_stmt;
40296 cp_parser_omp_construct (parser, pragma_tok, if_p);
40297 return true;
40299 case PRAGMA_OACC_WAIT:
40300 if (context == pragma_stmt)
40302 error_at (pragma_tok->location,
40303 "%<#pragma %s%> may only be used in compound statements",
40304 "acc wait");
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_ATOMIC:
40313 case PRAGMA_OACC_CACHE:
40314 case PRAGMA_OACC_DATA:
40315 case PRAGMA_OACC_HOST_DATA:
40316 case PRAGMA_OACC_KERNELS:
40317 case PRAGMA_OACC_PARALLEL:
40318 case PRAGMA_OACC_LOOP:
40319 case PRAGMA_OMP_ATOMIC:
40320 case PRAGMA_OMP_CRITICAL:
40321 case PRAGMA_OMP_DISTRIBUTE:
40322 case PRAGMA_OMP_FOR:
40323 case PRAGMA_OMP_MASTER:
40324 case PRAGMA_OMP_PARALLEL:
40325 case PRAGMA_OMP_SECTIONS:
40326 case PRAGMA_OMP_SIMD:
40327 case PRAGMA_OMP_SINGLE:
40328 case PRAGMA_OMP_TASK:
40329 case PRAGMA_OMP_TASKGROUP:
40330 case PRAGMA_OMP_TASKLOOP:
40331 case PRAGMA_OMP_TEAMS:
40332 if (context != pragma_stmt && context != pragma_compound)
40333 goto bad_stmt;
40334 stmt = push_omp_privatization_clauses (false);
40335 cp_parser_omp_construct (parser, pragma_tok, if_p);
40336 pop_omp_privatization_clauses (stmt);
40337 return true;
40339 case PRAGMA_OMP_REQUIRES:
40340 return cp_parser_omp_requires (parser, pragma_tok);
40342 case PRAGMA_OMP_ORDERED:
40343 if (context != pragma_stmt && context != pragma_compound)
40344 goto bad_stmt;
40345 stmt = push_omp_privatization_clauses (false);
40346 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
40347 pop_omp_privatization_clauses (stmt);
40348 return ret;
40350 case PRAGMA_OMP_TARGET:
40351 if (context != pragma_stmt && context != pragma_compound)
40352 goto bad_stmt;
40353 stmt = push_omp_privatization_clauses (false);
40354 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
40355 pop_omp_privatization_clauses (stmt);
40356 return ret;
40358 case PRAGMA_OMP_END_DECLARE_TARGET:
40359 cp_parser_omp_end_declare_target (parser, pragma_tok);
40360 return false;
40362 case PRAGMA_OMP_SECTION:
40363 error_at (pragma_tok->location,
40364 "%<#pragma omp section%> may only be used in "
40365 "%<#pragma omp sections%> construct");
40366 break;
40368 case PRAGMA_IVDEP:
40370 if (context == pragma_external)
40372 error_at (pragma_tok->location,
40373 "%<#pragma GCC ivdep%> must be inside a function");
40374 break;
40376 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
40377 unsigned short unroll;
40378 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40379 if (tok->type == CPP_PRAGMA
40380 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
40382 tok = cp_lexer_consume_token (parser->lexer);
40383 unroll = cp_parser_pragma_unroll (parser, tok);
40384 tok = cp_lexer_peek_token (the_parser->lexer);
40386 else
40387 unroll = 0;
40388 if (tok->type != CPP_KEYWORD
40389 || (tok->keyword != RID_FOR
40390 && tok->keyword != RID_WHILE
40391 && tok->keyword != RID_DO))
40393 cp_parser_error (parser, "for, while or do statement expected");
40394 return false;
40396 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40397 return true;
40400 case PRAGMA_UNROLL:
40402 if (context == pragma_external)
40404 error_at (pragma_tok->location,
40405 "%<#pragma GCC unroll%> must be inside a function");
40406 break;
40408 const unsigned short unroll
40409 = cp_parser_pragma_unroll (parser, pragma_tok);
40410 bool ivdep;
40411 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40412 if (tok->type == CPP_PRAGMA
40413 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
40415 tok = cp_lexer_consume_token (parser->lexer);
40416 ivdep = cp_parser_pragma_ivdep (parser, tok);
40417 tok = cp_lexer_peek_token (the_parser->lexer);
40419 else
40420 ivdep = false;
40421 if (tok->type != CPP_KEYWORD
40422 || (tok->keyword != RID_FOR
40423 && tok->keyword != RID_WHILE
40424 && tok->keyword != RID_DO))
40426 cp_parser_error (parser, "for, while or do statement expected");
40427 return false;
40429 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40430 return true;
40433 default:
40434 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
40435 c_invoke_pragma_handler (id);
40436 break;
40438 bad_stmt:
40439 cp_parser_error (parser, "expected declaration specifiers");
40440 break;
40443 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40444 return false;
40447 /* The interface the pragma parsers have to the lexer. */
40449 enum cpp_ttype
40450 pragma_lex (tree *value, location_t *loc)
40452 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40453 enum cpp_ttype ret = tok->type;
40455 *value = tok->u.value;
40456 if (loc)
40457 *loc = tok->location;
40459 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
40460 ret = CPP_EOF;
40461 else if (ret == CPP_STRING)
40462 *value = cp_parser_string_literal (the_parser, false, false);
40463 else
40465 if (ret == CPP_KEYWORD)
40466 ret = CPP_NAME;
40467 cp_lexer_consume_token (the_parser->lexer);
40470 return ret;
40474 /* External interface. */
40476 /* Parse one entire translation unit. */
40478 void
40479 c_parse_file (void)
40481 static bool already_called = false;
40483 if (already_called)
40484 fatal_error (input_location,
40485 "inter-module optimizations not implemented for C++");
40486 already_called = true;
40488 the_parser = cp_parser_new ();
40489 push_deferring_access_checks (flag_access_control
40490 ? dk_no_deferred : dk_no_check);
40491 cp_parser_translation_unit (the_parser);
40492 the_parser = NULL;
40494 finish_translation_unit ();
40497 /* Create an identifier for a generic parameter type (a synthesized
40498 template parameter implied by `auto' or a concept identifier). */
40500 static GTY(()) int generic_parm_count;
40501 static tree
40502 make_generic_type_name ()
40504 char buf[32];
40505 sprintf (buf, "auto:%d", ++generic_parm_count);
40506 return get_identifier (buf);
40509 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
40510 (creating a new template parameter list if necessary). Returns the newly
40511 created template type parm. */
40513 static tree
40514 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
40516 gcc_assert (current_binding_level->kind == sk_function_parms);
40518 /* Before committing to modifying any scope, if we're in an
40519 implicit template scope, and we're trying to synthesize a
40520 constrained parameter, try to find a previous parameter with
40521 the same name. This is the same-type rule for abbreviated
40522 function templates.
40524 NOTE: We can generate implicit parameters when tentatively
40525 parsing a nested name specifier, only to reject that parse
40526 later. However, matching the same template-id as part of a
40527 direct-declarator should generate an identical template
40528 parameter, so this rule will merge them. */
40529 if (parser->implicit_template_scope && constr)
40531 tree t = parser->implicit_template_parms;
40532 while (t)
40534 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
40536 tree d = TREE_VALUE (t);
40537 if (TREE_CODE (d) == PARM_DECL)
40538 /* Return the TEMPLATE_PARM_INDEX. */
40539 d = DECL_INITIAL (d);
40540 return d;
40542 t = TREE_CHAIN (t);
40546 /* We are either continuing a function template that already contains implicit
40547 template parameters, creating a new fully-implicit function template, or
40548 extending an existing explicit function template with implicit template
40549 parameters. */
40551 cp_binding_level *const entry_scope = current_binding_level;
40553 bool become_template = false;
40554 cp_binding_level *parent_scope = 0;
40556 if (parser->implicit_template_scope)
40558 gcc_assert (parser->implicit_template_parms);
40560 current_binding_level = parser->implicit_template_scope;
40562 else
40564 /* Roll back to the existing template parameter scope (in the case of
40565 extending an explicit function template) or introduce a new template
40566 parameter scope ahead of the function parameter scope (or class scope
40567 in the case of out-of-line member definitions). The function scope is
40568 added back after template parameter synthesis below. */
40570 cp_binding_level *scope = entry_scope;
40572 while (scope->kind == sk_function_parms)
40574 parent_scope = scope;
40575 scope = scope->level_chain;
40577 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
40579 /* If not defining a class, then any class scope is a scope level in
40580 an out-of-line member definition. In this case simply wind back
40581 beyond the first such scope to inject the template parameter list.
40582 Otherwise wind back to the class being defined. The latter can
40583 occur in class member friend declarations such as:
40585 class A {
40586 void foo (auto);
40588 class B {
40589 friend void A::foo (auto);
40592 The template parameter list synthesized for the friend declaration
40593 must be injected in the scope of 'B'. This can also occur in
40594 erroneous cases such as:
40596 struct A {
40597 struct B {
40598 void foo (auto);
40600 void B::foo (auto) {}
40603 Here the attempted definition of 'B::foo' within 'A' is ill-formed
40604 but, nevertheless, the template parameter list synthesized for the
40605 declarator should be injected into the scope of 'A' as if the
40606 ill-formed template was specified explicitly. */
40608 while (scope->kind == sk_class && !scope->defining_class_p)
40610 parent_scope = scope;
40611 scope = scope->level_chain;
40615 current_binding_level = scope;
40617 if (scope->kind != sk_template_parms
40618 || !function_being_declared_is_template_p (parser))
40620 /* Introduce a new template parameter list for implicit template
40621 parameters. */
40623 become_template = true;
40625 parser->implicit_template_scope
40626 = begin_scope (sk_template_parms, NULL);
40628 ++processing_template_decl;
40630 parser->fully_implicit_function_template_p = true;
40631 ++parser->num_template_parameter_lists;
40633 else
40635 /* Synthesize implicit template parameters at the end of the explicit
40636 template parameter list. */
40638 gcc_assert (current_template_parms);
40640 parser->implicit_template_scope = scope;
40642 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
40643 parser->implicit_template_parms
40644 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
40648 /* Synthesize a new template parameter and track the current template
40649 parameter chain with implicit_template_parms. */
40651 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
40652 tree synth_id = make_generic_type_name ();
40653 tree synth_tmpl_parm;
40654 bool non_type = false;
40656 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
40657 synth_tmpl_parm
40658 = finish_template_type_parm (class_type_node, synth_id);
40659 else if (TREE_CODE (proto) == TEMPLATE_DECL)
40660 synth_tmpl_parm
40661 = finish_constrained_template_template_parm (proto, synth_id);
40662 else
40664 synth_tmpl_parm = copy_decl (proto);
40665 DECL_NAME (synth_tmpl_parm) = synth_id;
40666 non_type = true;
40669 // Attach the constraint to the parm before processing.
40670 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
40671 TREE_TYPE (node) = constr;
40672 tree new_parm
40673 = process_template_parm (parser->implicit_template_parms,
40674 input_location,
40675 node,
40676 /*non_type=*/non_type,
40677 /*param_pack=*/false);
40679 // Chain the new parameter to the list of implicit parameters.
40680 if (parser->implicit_template_parms)
40681 parser->implicit_template_parms
40682 = TREE_CHAIN (parser->implicit_template_parms);
40683 else
40684 parser->implicit_template_parms = new_parm;
40686 tree new_decl = get_local_decls ();
40687 if (non_type)
40688 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
40689 new_decl = DECL_INITIAL (new_decl);
40691 /* If creating a fully implicit function template, start the new implicit
40692 template parameter list with this synthesized type, otherwise grow the
40693 current template parameter list. */
40695 if (become_template)
40697 parent_scope->level_chain = current_binding_level;
40699 tree new_parms = make_tree_vec (1);
40700 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
40701 current_template_parms = tree_cons (size_int (processing_template_decl),
40702 new_parms, current_template_parms);
40704 else
40706 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
40707 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
40708 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
40709 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
40712 // If the new parameter was constrained, we need to add that to the
40713 // constraints in the template parameter list.
40714 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
40716 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
40717 reqs = conjoin_constraints (reqs, req);
40718 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
40721 current_binding_level = entry_scope;
40723 return new_decl;
40726 /* Finish the declaration of a fully implicit function template. Such a
40727 template has no explicit template parameter list so has not been through the
40728 normal template head and tail processing. synthesize_implicit_template_parm
40729 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
40730 provided if the declaration is a class member such that its template
40731 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
40732 form is returned. Otherwise NULL_TREE is returned. */
40734 static tree
40735 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
40737 gcc_assert (parser->fully_implicit_function_template_p);
40739 if (member_decl_opt && member_decl_opt != error_mark_node
40740 && DECL_VIRTUAL_P (member_decl_opt))
40742 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
40743 "implicit templates may not be %<virtual%>");
40744 DECL_VIRTUAL_P (member_decl_opt) = false;
40747 if (member_decl_opt)
40748 member_decl_opt = finish_member_template_decl (member_decl_opt);
40749 end_template_decl ();
40751 parser->fully_implicit_function_template_p = false;
40752 parser->implicit_template_parms = 0;
40753 parser->implicit_template_scope = 0;
40754 --parser->num_template_parameter_lists;
40756 return member_decl_opt;
40759 /* Like finish_fully_implicit_template, but to be used in error
40760 recovery, rearranging scopes so that we restore the state we had
40761 before synthesize_implicit_template_parm inserted the implement
40762 template parms scope. */
40764 static void
40765 abort_fully_implicit_template (cp_parser *parser)
40767 cp_binding_level *return_to_scope = current_binding_level;
40769 if (parser->implicit_template_scope
40770 && return_to_scope != parser->implicit_template_scope)
40772 cp_binding_level *child = return_to_scope;
40773 for (cp_binding_level *scope = child->level_chain;
40774 scope != parser->implicit_template_scope;
40775 scope = child->level_chain)
40776 child = scope;
40777 child->level_chain = parser->implicit_template_scope->level_chain;
40778 parser->implicit_template_scope->level_chain = return_to_scope;
40779 current_binding_level = parser->implicit_template_scope;
40781 else
40782 return_to_scope = return_to_scope->level_chain;
40784 finish_fully_implicit_template (parser, NULL);
40786 gcc_assert (current_binding_level == return_to_scope);
40789 /* Helper function for diagnostics that have complained about things
40790 being used with 'extern "C"' linkage.
40792 Attempt to issue a note showing where the 'extern "C"' linkage began. */
40794 void
40795 maybe_show_extern_c_location (void)
40797 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
40798 inform (the_parser->innermost_linkage_specification_location,
40799 "%<extern \"C\"%> linkage started here");
40802 #include "gt-cp-parser.h"