PR libstdc++/87308 adjust regex used in std::any pretty printer
[official-gcc.git] / gcc / cp / parser.c
blob3a98ed900cdd5a9061bf81cf575d72f134946710
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
50 /* The lexer. */
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53 and c-lex.c) and the C++ parser. */
55 static cp_token eof_token =
57 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
60 /* The various kinds of non integral constant we encounter. */
61 enum non_integral_constant {
62 NIC_NONE,
63 /* floating-point literal */
64 NIC_FLOAT,
65 /* %<this%> */
66 NIC_THIS,
67 /* %<__FUNCTION__%> */
68 NIC_FUNC_NAME,
69 /* %<__PRETTY_FUNCTION__%> */
70 NIC_PRETTY_FUNC,
71 /* %<__func__%> */
72 NIC_C99_FUNC,
73 /* "%<va_arg%> */
74 NIC_VA_ARG,
75 /* a cast */
76 NIC_CAST,
77 /* %<typeid%> operator */
78 NIC_TYPEID,
79 /* non-constant compound literals */
80 NIC_NCC,
81 /* a function call */
82 NIC_FUNC_CALL,
83 /* an increment */
84 NIC_INC,
85 /* an decrement */
86 NIC_DEC,
87 /* an array reference */
88 NIC_ARRAY_REF,
89 /* %<->%> */
90 NIC_ARROW,
91 /* %<.%> */
92 NIC_POINT,
93 /* the address of a label */
94 NIC_ADDR_LABEL,
95 /* %<*%> */
96 NIC_STAR,
97 /* %<&%> */
98 NIC_ADDR,
99 /* %<++%> */
100 NIC_PREINCREMENT,
101 /* %<--%> */
102 NIC_PREDECREMENT,
103 /* %<new%> */
104 NIC_NEW,
105 /* %<delete%> */
106 NIC_DEL,
107 /* calls to overloaded operators */
108 NIC_OVERLOADED,
109 /* an assignment */
110 NIC_ASSIGNMENT,
111 /* a comma operator */
112 NIC_COMMA,
113 /* a call to a constructor */
114 NIC_CONSTRUCTOR,
115 /* a transaction expression */
116 NIC_TRANSACTION
119 /* The various kinds of errors about name-lookup failing. */
120 enum name_lookup_error {
121 /* NULL */
122 NLE_NULL,
123 /* is not a type */
124 NLE_TYPE,
125 /* is not a class or namespace */
126 NLE_CXX98,
127 /* is not a class, namespace, or enumeration */
128 NLE_NOT_CXX98
131 /* The various kinds of required token */
132 enum required_token {
133 RT_NONE,
134 RT_SEMICOLON, /* ';' */
135 RT_OPEN_PAREN, /* '(' */
136 RT_CLOSE_BRACE, /* '}' */
137 RT_OPEN_BRACE, /* '{' */
138 RT_CLOSE_SQUARE, /* ']' */
139 RT_OPEN_SQUARE, /* '[' */
140 RT_COMMA, /* ',' */
141 RT_SCOPE, /* '::' */
142 RT_LESS, /* '<' */
143 RT_GREATER, /* '>' */
144 RT_EQ, /* '=' */
145 RT_ELLIPSIS, /* '...' */
146 RT_MULT, /* '*' */
147 RT_COMPL, /* '~' */
148 RT_COLON, /* ':' */
149 RT_COLON_SCOPE, /* ':' or '::' */
150 RT_CLOSE_PAREN, /* ')' */
151 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
152 RT_PRAGMA_EOL, /* end of line */
153 RT_NAME, /* identifier */
155 /* The type is CPP_KEYWORD */
156 RT_NEW, /* new */
157 RT_DELETE, /* delete */
158 RT_RETURN, /* return */
159 RT_WHILE, /* while */
160 RT_EXTERN, /* extern */
161 RT_STATIC_ASSERT, /* static_assert */
162 RT_DECLTYPE, /* decltype */
163 RT_OPERATOR, /* operator */
164 RT_CLASS, /* class */
165 RT_TEMPLATE, /* template */
166 RT_NAMESPACE, /* namespace */
167 RT_USING, /* using */
168 RT_ASM, /* asm */
169 RT_TRY, /* try */
170 RT_CATCH, /* catch */
171 RT_THROW, /* throw */
172 RT_LABEL, /* __label__ */
173 RT_AT_TRY, /* @try */
174 RT_AT_SYNCHRONIZED, /* @synchronized */
175 RT_AT_THROW, /* @throw */
177 RT_SELECT, /* selection-statement */
178 RT_ITERATION, /* iteration-statement */
179 RT_JUMP, /* jump-statement */
180 RT_CLASS_KEY, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL /* __transaction_cancel */
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188 reverting it on destruction. */
190 class type_id_in_expr_sentinel
192 cp_parser *parser;
193 bool saved;
194 public:
195 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
196 : parser (parser),
197 saved (parser->in_type_id_in_expr_p)
198 { parser->in_type_id_in_expr_p = set; }
199 ~type_id_in_expr_sentinel ()
200 { parser->in_type_id_in_expr_p = saved; }
203 /* Prototypes. */
205 static cp_lexer *cp_lexer_new_main
206 (void);
207 static cp_lexer *cp_lexer_new_from_tokens
208 (cp_token_cache *tokens);
209 static void cp_lexer_destroy
210 (cp_lexer *);
211 static int cp_lexer_saving_tokens
212 (const cp_lexer *);
213 static cp_token *cp_lexer_token_at
214 (cp_lexer *, cp_token_position);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer *, cp_token *);
217 static inline cp_token *cp_lexer_peek_token
218 (cp_lexer *);
219 static cp_token *cp_lexer_peek_nth_token
220 (cp_lexer *, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer *, enum rid);
227 static cp_token *cp_lexer_consume_token
228 (cp_lexer *);
229 static void cp_lexer_purge_token
230 (cp_lexer *);
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer *, cp_token_position);
233 static void cp_lexer_save_tokens
234 (cp_lexer *);
235 static void cp_lexer_commit_tokens
236 (cp_lexer *);
237 static void cp_lexer_rollback_tokens
238 (cp_lexer *);
239 static void cp_lexer_print_token
240 (FILE *, cp_token *);
241 static inline bool cp_lexer_debugging_p
242 (cp_lexer *);
243 static void cp_lexer_start_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static void cp_lexer_stop_debugging
246 (cp_lexer *) ATTRIBUTE_UNUSED;
248 static cp_token_cache *cp_token_cache_new
249 (cp_token *, cp_token *);
251 static void cp_parser_initial_pragma
252 (cp_token *);
254 static bool cp_parser_omp_declare_reduction_exprs
255 (tree, cp_parser *);
256 static void cp_finalize_oacc_routine
257 (cp_parser *, tree, bool);
259 /* Manifest constants. */
260 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
261 #define CP_SAVED_TOKEN_STACK 5
263 /* Variables. */
265 /* The stream to which debugging output should be written. */
266 static FILE *cp_lexer_debug_stream;
268 /* Nonzero if we are parsing an unevaluated operand: an operand to
269 sizeof, typeof, or alignof. */
270 int cp_unevaluated_operand;
272 /* Dump up to NUM tokens in BUFFER to FILE starting with token
273 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
274 first token in BUFFER. If NUM is 0, dump all the tokens. If
275 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
276 highlighted by surrounding it in [[ ]]. */
278 static void
279 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
280 cp_token *start_token, unsigned num,
281 cp_token *curr_token)
283 unsigned i, nprinted;
284 cp_token *token;
285 bool do_print;
287 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
289 if (buffer == NULL)
290 return;
292 if (num == 0)
293 num = buffer->length ();
295 if (start_token == NULL)
296 start_token = buffer->address ();
298 if (start_token > buffer->address ())
300 cp_lexer_print_token (file, &(*buffer)[0]);
301 fprintf (file, " ... ");
304 do_print = false;
305 nprinted = 0;
306 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
308 if (token == start_token)
309 do_print = true;
311 if (!do_print)
312 continue;
314 nprinted++;
315 if (token == curr_token)
316 fprintf (file, "[[");
318 cp_lexer_print_token (file, token);
320 if (token == curr_token)
321 fprintf (file, "]]");
323 switch (token->type)
325 case CPP_SEMICOLON:
326 case CPP_OPEN_BRACE:
327 case CPP_CLOSE_BRACE:
328 case CPP_EOF:
329 fputc ('\n', file);
330 break;
332 default:
333 fputc (' ', file);
337 if (i == num && i < buffer->length ())
339 fprintf (file, " ... ");
340 cp_lexer_print_token (file, &buffer->last ());
343 fprintf (file, "\n");
347 /* Dump all tokens in BUFFER to stderr. */
349 void
350 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
352 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
355 DEBUG_FUNCTION void
356 debug (vec<cp_token, va_gc> &ref)
358 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
361 DEBUG_FUNCTION void
362 debug (vec<cp_token, va_gc> *ptr)
364 if (ptr)
365 debug (*ptr);
366 else
367 fprintf (stderr, "<nil>\n");
371 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
372 description for T. */
374 static void
375 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
377 if (t)
379 fprintf (file, "%s: ", desc);
380 print_node_brief (file, "", t, 0);
385 /* Dump parser context C to FILE. */
387 static void
388 cp_debug_print_context (FILE *file, cp_parser_context *c)
390 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
391 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
392 print_node_brief (file, "", c->object_type, 0);
393 fprintf (file, "}\n");
397 /* Print the stack of parsing contexts to FILE starting with FIRST. */
399 static void
400 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
402 unsigned i;
403 cp_parser_context *c;
405 fprintf (file, "Parsing context stack:\n");
406 for (i = 0, c = first; c; c = c->next, i++)
408 fprintf (file, "\t#%u: ", i);
409 cp_debug_print_context (file, c);
414 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
416 static void
417 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
419 if (flag)
420 fprintf (file, "%s: true\n", desc);
424 /* Print an unparsed function entry UF to FILE. */
426 static void
427 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
429 unsigned i;
430 cp_default_arg_entry *default_arg_fn;
431 tree fn;
433 fprintf (file, "\tFunctions with default args:\n");
434 for (i = 0;
435 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
436 i++)
438 fprintf (file, "\t\tClass type: ");
439 print_node_brief (file, "", default_arg_fn->class_type, 0);
440 fprintf (file, "\t\tDeclaration: ");
441 print_node_brief (file, "", default_arg_fn->decl, 0);
442 fprintf (file, "\n");
445 fprintf (file, "\n\tFunctions with definitions that require "
446 "post-processing\n\t\t");
447 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
449 print_node_brief (file, "", fn, 0);
450 fprintf (file, " ");
452 fprintf (file, "\n");
454 fprintf (file, "\n\tNon-static data members with initializers that require "
455 "post-processing\n\t\t");
456 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
458 print_node_brief (file, "", fn, 0);
459 fprintf (file, " ");
461 fprintf (file, "\n");
465 /* Print the stack of unparsed member functions S to FILE. */
467 static void
468 cp_debug_print_unparsed_queues (FILE *file,
469 vec<cp_unparsed_functions_entry, va_gc> *s)
471 unsigned i;
472 cp_unparsed_functions_entry *uf;
474 fprintf (file, "Unparsed functions\n");
475 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
477 fprintf (file, "#%u:\n", i);
478 cp_debug_print_unparsed_function (file, uf);
483 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
484 the given PARSER. If FILE is NULL, the output is printed on stderr. */
486 static void
487 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
489 cp_token *next_token, *first_token, *start_token;
491 if (file == NULL)
492 file = stderr;
494 next_token = parser->lexer->next_token;
495 first_token = parser->lexer->buffer->address ();
496 start_token = (next_token > first_token + window_size / 2)
497 ? next_token - window_size / 2
498 : first_token;
499 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
500 next_token);
504 /* Dump debugging information for the given PARSER. If FILE is NULL,
505 the output is printed on stderr. */
507 void
508 cp_debug_parser (FILE *file, cp_parser *parser)
510 const size_t window_size = 20;
511 cp_token *token;
512 expanded_location eloc;
514 if (file == NULL)
515 file = stderr;
517 fprintf (file, "Parser state\n\n");
518 fprintf (file, "Number of tokens: %u\n",
519 vec_safe_length (parser->lexer->buffer));
520 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
521 cp_debug_print_tree_if_set (file, "Object scope",
522 parser->object_scope);
523 cp_debug_print_tree_if_set (file, "Qualifying scope",
524 parser->qualifying_scope);
525 cp_debug_print_context_stack (file, parser->context);
526 cp_debug_print_flag (file, "Allow GNU extensions",
527 parser->allow_gnu_extensions_p);
528 cp_debug_print_flag (file, "'>' token is greater-than",
529 parser->greater_than_is_operator_p);
530 cp_debug_print_flag (file, "Default args allowed in current "
531 "parameter list", parser->default_arg_ok_p);
532 cp_debug_print_flag (file, "Parsing integral constant-expression",
533 parser->integral_constant_expression_p);
534 cp_debug_print_flag (file, "Allow non-constant expression in current "
535 "constant-expression",
536 parser->allow_non_integral_constant_expression_p);
537 cp_debug_print_flag (file, "Seen non-constant expression",
538 parser->non_integral_constant_expression_p);
539 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
540 "current context",
541 parser->local_variables_forbidden_p);
542 cp_debug_print_flag (file, "In unbraced linkage specification",
543 parser->in_unbraced_linkage_specification_p);
544 cp_debug_print_flag (file, "Parsing a declarator",
545 parser->in_declarator_p);
546 cp_debug_print_flag (file, "In template argument list",
547 parser->in_template_argument_list_p);
548 cp_debug_print_flag (file, "Parsing an iteration statement",
549 parser->in_statement & IN_ITERATION_STMT);
550 cp_debug_print_flag (file, "Parsing a switch statement",
551 parser->in_statement & IN_SWITCH_STMT);
552 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
553 parser->in_statement & IN_OMP_BLOCK);
554 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
555 parser->in_statement & IN_OMP_FOR);
556 cp_debug_print_flag (file, "Parsing an if statement",
557 parser->in_statement & IN_IF_STMT);
558 cp_debug_print_flag (file, "Parsing a type-id in an expression "
559 "context", parser->in_type_id_in_expr_p);
560 cp_debug_print_flag (file, "String expressions should be translated "
561 "to execution character set",
562 parser->translate_strings_p);
563 cp_debug_print_flag (file, "Parsing function body outside of a "
564 "local class", parser->in_function_body);
565 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
566 parser->colon_corrects_to_scope_p);
567 cp_debug_print_flag (file, "Colon doesn't start a class definition",
568 parser->colon_doesnt_start_class_def_p);
569 if (parser->type_definition_forbidden_message)
570 fprintf (file, "Error message for forbidden type definitions: %s\n",
571 parser->type_definition_forbidden_message);
572 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
573 fprintf (file, "Number of class definitions in progress: %u\n",
574 parser->num_classes_being_defined);
575 fprintf (file, "Number of template parameter lists for the current "
576 "declaration: %u\n", parser->num_template_parameter_lists);
577 cp_debug_parser_tokens (file, parser, window_size);
578 token = parser->lexer->next_token;
579 fprintf (file, "Next token to parse:\n");
580 fprintf (file, "\tToken: ");
581 cp_lexer_print_token (file, token);
582 eloc = expand_location (token->location);
583 fprintf (file, "\n\tFile: %s\n", eloc.file);
584 fprintf (file, "\tLine: %d\n", eloc.line);
585 fprintf (file, "\tColumn: %d\n", eloc.column);
588 DEBUG_FUNCTION void
589 debug (cp_parser &ref)
591 cp_debug_parser (stderr, &ref);
594 DEBUG_FUNCTION void
595 debug (cp_parser *ptr)
597 if (ptr)
598 debug (*ptr);
599 else
600 fprintf (stderr, "<nil>\n");
603 /* Allocate memory for a new lexer object and return it. */
605 static cp_lexer *
606 cp_lexer_alloc (void)
608 cp_lexer *lexer;
610 c_common_no_more_pch ();
612 /* Allocate the memory. */
613 lexer = ggc_cleared_alloc<cp_lexer> ();
615 /* Initially we are not debugging. */
616 lexer->debugging_p = false;
618 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
620 /* Create the buffer. */
621 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
623 return lexer;
627 /* Create a new main C++ lexer, the lexer that gets tokens from the
628 preprocessor. */
630 static cp_lexer *
631 cp_lexer_new_main (void)
633 cp_lexer *lexer;
634 cp_token token;
636 /* It's possible that parsing the first pragma will load a PCH file,
637 which is a GC collection point. So we have to do that before
638 allocating any memory. */
639 cp_parser_initial_pragma (&token);
641 lexer = cp_lexer_alloc ();
643 /* Put the first token in the buffer. */
644 lexer->buffer->quick_push (token);
646 /* Get the remaining tokens from the preprocessor. */
647 while (token.type != CPP_EOF)
649 cp_lexer_get_preprocessor_token (lexer, &token);
650 vec_safe_push (lexer->buffer, token);
653 lexer->last_token = lexer->buffer->address ()
654 + lexer->buffer->length ()
655 - 1;
656 lexer->next_token = lexer->buffer->length ()
657 ? lexer->buffer->address ()
658 : &eof_token;
660 /* Subsequent preprocessor diagnostics should use compiler
661 diagnostic functions to get the compiler source location. */
662 done_lexing = true;
664 gcc_assert (!lexer->next_token->purged_p);
665 return lexer;
668 /* Create a new lexer whose token stream is primed with the tokens in
669 CACHE. When these tokens are exhausted, no new tokens will be read. */
671 static cp_lexer *
672 cp_lexer_new_from_tokens (cp_token_cache *cache)
674 cp_token *first = cache->first;
675 cp_token *last = cache->last;
676 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
678 /* We do not own the buffer. */
679 lexer->buffer = NULL;
680 lexer->next_token = first == last ? &eof_token : first;
681 lexer->last_token = last;
683 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
685 /* Initially we are not debugging. */
686 lexer->debugging_p = false;
688 gcc_assert (!lexer->next_token->purged_p);
689 return lexer;
692 /* Frees all resources associated with LEXER. */
694 static void
695 cp_lexer_destroy (cp_lexer *lexer)
697 vec_free (lexer->buffer);
698 lexer->saved_tokens.release ();
699 ggc_free (lexer);
702 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
703 be used. The point of this flag is to help the compiler to fold away calls
704 to cp_lexer_debugging_p within this source file at compile time, when the
705 lexer is not being debugged. */
707 #define LEXER_DEBUGGING_ENABLED_P false
709 /* Returns nonzero if debugging information should be output. */
711 static inline bool
712 cp_lexer_debugging_p (cp_lexer *lexer)
714 if (!LEXER_DEBUGGING_ENABLED_P)
715 return false;
717 return lexer->debugging_p;
721 static inline cp_token_position
722 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
724 gcc_assert (!previous_p || lexer->next_token != &eof_token);
726 return lexer->next_token - previous_p;
729 static inline cp_token *
730 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
732 return pos;
735 static inline void
736 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
738 lexer->next_token = cp_lexer_token_at (lexer, pos);
741 static inline cp_token_position
742 cp_lexer_previous_token_position (cp_lexer *lexer)
744 if (lexer->next_token == &eof_token)
745 return lexer->last_token - 1;
746 else
747 return cp_lexer_token_position (lexer, true);
750 static inline cp_token *
751 cp_lexer_previous_token (cp_lexer *lexer)
753 cp_token_position tp = cp_lexer_previous_token_position (lexer);
755 /* Skip past purged tokens. */
756 while (tp->purged_p)
758 gcc_assert (tp != vec_safe_address (lexer->buffer));
759 tp--;
762 return cp_lexer_token_at (lexer, tp);
765 /* nonzero if we are presently saving tokens. */
767 static inline int
768 cp_lexer_saving_tokens (const cp_lexer* lexer)
770 return lexer->saved_tokens.length () != 0;
773 /* Store the next token from the preprocessor in *TOKEN. Return true
774 if we reach EOF. If LEXER is NULL, assume we are handling an
775 initial #pragma pch_preprocess, and thus want the lexer to return
776 processed strings. */
778 static void
779 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
781 static int is_extern_c = 0;
783 /* Get a new token from the preprocessor. */
784 token->type
785 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
786 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
787 token->keyword = RID_MAX;
788 token->purged_p = false;
789 token->error_reported = false;
791 /* On some systems, some header files are surrounded by an
792 implicit extern "C" block. Set a flag in the token if it
793 comes from such a header. */
794 is_extern_c += pending_lang_change;
795 pending_lang_change = 0;
796 token->implicit_extern_c = is_extern_c > 0;
798 /* Check to see if this token is a keyword. */
799 if (token->type == CPP_NAME)
801 if (IDENTIFIER_KEYWORD_P (token->u.value))
803 /* Mark this token as a keyword. */
804 token->type = CPP_KEYWORD;
805 /* Record which keyword. */
806 token->keyword = C_RID_CODE (token->u.value);
808 else
810 if (warn_cxx11_compat
811 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
812 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
814 /* Warn about the C++0x keyword (but still treat it as
815 an identifier). */
816 warning (OPT_Wc__11_compat,
817 "identifier %qE is a keyword in C++11",
818 token->u.value);
820 /* Clear out the C_RID_CODE so we don't warn about this
821 particular identifier-turned-keyword again. */
822 C_SET_RID_CODE (token->u.value, RID_MAX);
825 token->keyword = RID_MAX;
828 else if (token->type == CPP_AT_NAME)
830 /* This only happens in Objective-C++; it must be a keyword. */
831 token->type = CPP_KEYWORD;
832 switch (C_RID_CODE (token->u.value))
834 /* Replace 'class' with '@class', 'private' with '@private',
835 etc. This prevents confusion with the C++ keyword
836 'class', and makes the tokens consistent with other
837 Objective-C 'AT' keywords. For example '@class' is
838 reported as RID_AT_CLASS which is consistent with
839 '@synchronized', which is reported as
840 RID_AT_SYNCHRONIZED.
842 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
843 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
844 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
845 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
846 case RID_THROW: token->keyword = RID_AT_THROW; break;
847 case RID_TRY: token->keyword = RID_AT_TRY; break;
848 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
849 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
850 default: token->keyword = C_RID_CODE (token->u.value);
855 /* Update the globals input_location and the input file stack from TOKEN. */
856 static inline void
857 cp_lexer_set_source_position_from_token (cp_token *token)
859 if (token->type != CPP_EOF)
861 input_location = token->location;
865 /* Update the globals input_location and the input file stack from LEXER. */
866 static inline void
867 cp_lexer_set_source_position (cp_lexer *lexer)
869 cp_token *token = cp_lexer_peek_token (lexer);
870 cp_lexer_set_source_position_from_token (token);
873 /* Return a pointer to the next token in the token stream, but do not
874 consume it. */
876 static inline cp_token *
877 cp_lexer_peek_token (cp_lexer *lexer)
879 if (cp_lexer_debugging_p (lexer))
881 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
882 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
883 putc ('\n', cp_lexer_debug_stream);
885 return lexer->next_token;
888 /* Return true if the next token has the indicated TYPE. */
890 static inline bool
891 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
893 return cp_lexer_peek_token (lexer)->type == type;
896 /* Return true if the next token does not have the indicated TYPE. */
898 static inline bool
899 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
901 return !cp_lexer_next_token_is (lexer, type);
904 /* Return true if the next token is the indicated KEYWORD. */
906 static inline bool
907 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
909 return cp_lexer_peek_token (lexer)->keyword == keyword;
912 static inline bool
913 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
915 return cp_lexer_peek_nth_token (lexer, n)->type == type;
918 static inline bool
919 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
921 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
924 /* Return true if KEYWORD can start a decl-specifier. */
926 bool
927 cp_keyword_starts_decl_specifier_p (enum rid keyword)
929 switch (keyword)
931 /* auto specifier: storage-class-specifier in C++,
932 simple-type-specifier in C++0x. */
933 case RID_AUTO:
934 /* Storage classes. */
935 case RID_REGISTER:
936 case RID_STATIC:
937 case RID_EXTERN:
938 case RID_MUTABLE:
939 case RID_THREAD:
940 /* Elaborated type specifiers. */
941 case RID_ENUM:
942 case RID_CLASS:
943 case RID_STRUCT:
944 case RID_UNION:
945 case RID_TYPENAME:
946 /* Simple type specifiers. */
947 case RID_CHAR:
948 case RID_CHAR16:
949 case RID_CHAR32:
950 case RID_WCHAR:
951 case RID_BOOL:
952 case RID_SHORT:
953 case RID_INT:
954 case RID_LONG:
955 case RID_SIGNED:
956 case RID_UNSIGNED:
957 case RID_FLOAT:
958 case RID_DOUBLE:
959 case RID_VOID:
960 /* GNU extensions. */
961 case RID_ATTRIBUTE:
962 case RID_TYPEOF:
963 /* C++0x extensions. */
964 case RID_DECLTYPE:
965 case RID_UNDERLYING_TYPE:
966 case RID_CONSTEXPR:
967 return true;
969 default:
970 if (keyword >= RID_FIRST_INT_N
971 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
972 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
973 return true;
974 return false;
978 /* Return true if the next token is a keyword for a decl-specifier. */
980 static bool
981 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
983 cp_token *token;
985 token = cp_lexer_peek_token (lexer);
986 return cp_keyword_starts_decl_specifier_p (token->keyword);
989 /* Returns TRUE iff the token T begins a decltype type. */
991 static bool
992 token_is_decltype (cp_token *t)
994 return (t->keyword == RID_DECLTYPE
995 || t->type == CPP_DECLTYPE);
998 /* Returns TRUE iff the next token begins a decltype type. */
1000 static bool
1001 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1003 cp_token *t = cp_lexer_peek_token (lexer);
1004 return token_is_decltype (t);
1007 /* Called when processing a token with tree_check_value; perform or defer the
1008 associated checks and return the value. */
1010 static tree
1011 saved_checks_value (struct tree_check *check_value)
1013 /* Perform any access checks that were deferred. */
1014 vec<deferred_access_check, va_gc> *checks;
1015 deferred_access_check *chk;
1016 checks = check_value->checks;
1017 if (checks)
1019 int i;
1020 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1021 perform_or_defer_access_check (chk->binfo,
1022 chk->decl,
1023 chk->diag_decl, tf_warning_or_error);
1025 /* Return the stored value. */
1026 return check_value->value;
1029 /* Return a pointer to the Nth token in the token stream. If N is 1,
1030 then this is precisely equivalent to cp_lexer_peek_token (except
1031 that it is not inline). One would like to disallow that case, but
1032 there is one case (cp_parser_nth_token_starts_template_id) where
1033 the caller passes a variable for N and it might be 1. */
1035 static cp_token *
1036 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1038 cp_token *token;
1040 /* N is 1-based, not zero-based. */
1041 gcc_assert (n > 0);
1043 if (cp_lexer_debugging_p (lexer))
1044 fprintf (cp_lexer_debug_stream,
1045 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1047 --n;
1048 token = lexer->next_token;
1049 gcc_assert (!n || token != &eof_token);
1050 while (n != 0)
1052 ++token;
1053 if (token == lexer->last_token)
1055 token = &eof_token;
1056 break;
1059 if (!token->purged_p)
1060 --n;
1063 if (cp_lexer_debugging_p (lexer))
1065 cp_lexer_print_token (cp_lexer_debug_stream, token);
1066 putc ('\n', cp_lexer_debug_stream);
1069 return token;
1072 /* Return the next token, and advance the lexer's next_token pointer
1073 to point to the next non-purged token. */
1075 static cp_token *
1076 cp_lexer_consume_token (cp_lexer* lexer)
1078 cp_token *token = lexer->next_token;
1080 gcc_assert (token != &eof_token);
1081 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1085 lexer->next_token++;
1086 if (lexer->next_token == lexer->last_token)
1088 lexer->next_token = &eof_token;
1089 break;
1093 while (lexer->next_token->purged_p);
1095 cp_lexer_set_source_position_from_token (token);
1097 /* Provide debugging output. */
1098 if (cp_lexer_debugging_p (lexer))
1100 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1101 cp_lexer_print_token (cp_lexer_debug_stream, token);
1102 putc ('\n', cp_lexer_debug_stream);
1105 return token;
1108 /* Permanently remove the next token from the token stream, and
1109 advance the next_token pointer to refer to the next non-purged
1110 token. */
1112 static void
1113 cp_lexer_purge_token (cp_lexer *lexer)
1115 cp_token *tok = lexer->next_token;
1117 gcc_assert (tok != &eof_token);
1118 tok->purged_p = true;
1119 tok->location = UNKNOWN_LOCATION;
1120 tok->u.value = NULL_TREE;
1121 tok->keyword = RID_MAX;
1125 tok++;
1126 if (tok == lexer->last_token)
1128 tok = &eof_token;
1129 break;
1132 while (tok->purged_p);
1133 lexer->next_token = tok;
1136 /* Permanently remove all tokens after TOK, up to, but not
1137 including, the token that will be returned next by
1138 cp_lexer_peek_token. */
1140 static void
1141 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1143 cp_token *peek = lexer->next_token;
1145 if (peek == &eof_token)
1146 peek = lexer->last_token;
1148 gcc_assert (tok < peek);
1150 for ( tok += 1; tok != peek; tok += 1)
1152 tok->purged_p = true;
1153 tok->location = UNKNOWN_LOCATION;
1154 tok->u.value = NULL_TREE;
1155 tok->keyword = RID_MAX;
1159 /* Begin saving tokens. All tokens consumed after this point will be
1160 preserved. */
1162 static void
1163 cp_lexer_save_tokens (cp_lexer* lexer)
1165 /* Provide debugging output. */
1166 if (cp_lexer_debugging_p (lexer))
1167 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1169 lexer->saved_tokens.safe_push (lexer->next_token);
1172 /* Commit to the portion of the token stream most recently saved. */
1174 static void
1175 cp_lexer_commit_tokens (cp_lexer* lexer)
1177 /* Provide debugging output. */
1178 if (cp_lexer_debugging_p (lexer))
1179 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1181 lexer->saved_tokens.pop ();
1184 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1185 to the token stream. Stop saving tokens. */
1187 static void
1188 cp_lexer_rollback_tokens (cp_lexer* lexer)
1190 /* Provide debugging output. */
1191 if (cp_lexer_debugging_p (lexer))
1192 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1194 lexer->next_token = lexer->saved_tokens.pop ();
1197 /* RAII wrapper around the above functions, with sanity checking. Creating
1198 a variable saves tokens, which are committed when the variable is
1199 destroyed unless they are explicitly rolled back by calling the rollback
1200 member function. */
1202 struct saved_token_sentinel
1204 cp_lexer *lexer;
1205 unsigned len;
1206 bool commit;
1207 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1209 len = lexer->saved_tokens.length ();
1210 cp_lexer_save_tokens (lexer);
1212 void rollback ()
1214 cp_lexer_rollback_tokens (lexer);
1215 commit = false;
1217 ~saved_token_sentinel()
1219 if (commit)
1220 cp_lexer_commit_tokens (lexer);
1221 gcc_assert (lexer->saved_tokens.length () == len);
1225 /* Print a representation of the TOKEN on the STREAM. */
1227 static void
1228 cp_lexer_print_token (FILE * stream, cp_token *token)
1230 /* We don't use cpp_type2name here because the parser defines
1231 a few tokens of its own. */
1232 static const char *const token_names[] = {
1233 /* cpplib-defined token types */
1234 #define OP(e, s) #e,
1235 #define TK(e, s) #e,
1236 TTYPE_TABLE
1237 #undef OP
1238 #undef TK
1239 /* C++ parser token types - see "Manifest constants", above. */
1240 "KEYWORD",
1241 "TEMPLATE_ID",
1242 "NESTED_NAME_SPECIFIER",
1245 /* For some tokens, print the associated data. */
1246 switch (token->type)
1248 case CPP_KEYWORD:
1249 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1250 For example, `struct' is mapped to an INTEGER_CST. */
1251 if (!identifier_p (token->u.value))
1252 break;
1253 /* fall through */
1254 case CPP_NAME:
1255 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1256 break;
1258 case CPP_STRING:
1259 case CPP_STRING16:
1260 case CPP_STRING32:
1261 case CPP_WSTRING:
1262 case CPP_UTF8STRING:
1263 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1264 break;
1266 case CPP_NUMBER:
1267 print_generic_expr (stream, token->u.value);
1268 break;
1270 default:
1271 /* If we have a name for the token, print it out. Otherwise, we
1272 simply give the numeric code. */
1273 if (token->type < ARRAY_SIZE(token_names))
1274 fputs (token_names[token->type], stream);
1275 else
1276 fprintf (stream, "[%d]", token->type);
1277 break;
1281 DEBUG_FUNCTION void
1282 debug (cp_token &ref)
1284 cp_lexer_print_token (stderr, &ref);
1285 fprintf (stderr, "\n");
1288 DEBUG_FUNCTION void
1289 debug (cp_token *ptr)
1291 if (ptr)
1292 debug (*ptr);
1293 else
1294 fprintf (stderr, "<nil>\n");
1298 /* Start emitting debugging information. */
1300 static void
1301 cp_lexer_start_debugging (cp_lexer* lexer)
1303 if (!LEXER_DEBUGGING_ENABLED_P)
1304 fatal_error (input_location,
1305 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1307 lexer->debugging_p = true;
1308 cp_lexer_debug_stream = stderr;
1311 /* Stop emitting debugging information. */
1313 static void
1314 cp_lexer_stop_debugging (cp_lexer* lexer)
1316 if (!LEXER_DEBUGGING_ENABLED_P)
1317 fatal_error (input_location,
1318 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1320 lexer->debugging_p = false;
1321 cp_lexer_debug_stream = NULL;
1324 /* Create a new cp_token_cache, representing a range of tokens. */
1326 static cp_token_cache *
1327 cp_token_cache_new (cp_token *first, cp_token *last)
1329 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1330 cache->first = first;
1331 cache->last = last;
1332 return cache;
1335 /* Diagnose if #pragma omp declare simd isn't followed immediately
1336 by function declaration or definition. */
1338 static inline void
1339 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1341 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1343 error ("%<#pragma omp declare simd%> not immediately followed by "
1344 "function declaration or definition");
1345 parser->omp_declare_simd = NULL;
1349 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1350 and put that into "omp declare simd" attribute. */
1352 static inline void
1353 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1355 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1357 if (fndecl == error_mark_node)
1359 parser->omp_declare_simd = NULL;
1360 return;
1362 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1364 cp_ensure_no_omp_declare_simd (parser);
1365 return;
1370 /* Diagnose if #pragma acc routine isn't followed immediately by function
1371 declaration or definition. */
1373 static inline void
1374 cp_ensure_no_oacc_routine (cp_parser *parser)
1376 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1378 error_at (parser->oacc_routine->loc,
1379 "%<#pragma acc routine%> not immediately followed by "
1380 "function declaration or definition");
1381 parser->oacc_routine = NULL;
1385 /* Decl-specifiers. */
1387 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1389 static void
1390 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1392 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1395 /* Declarators. */
1397 /* Nothing other than the parser should be creating declarators;
1398 declarators are a semi-syntactic representation of C++ entities.
1399 Other parts of the front end that need to create entities (like
1400 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1402 static cp_declarator *make_call_declarator
1403 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1404 static cp_declarator *make_array_declarator
1405 (cp_declarator *, tree);
1406 static cp_declarator *make_pointer_declarator
1407 (cp_cv_quals, cp_declarator *, tree);
1408 static cp_declarator *make_reference_declarator
1409 (cp_cv_quals, cp_declarator *, bool, tree);
1410 static cp_declarator *make_ptrmem_declarator
1411 (cp_cv_quals, tree, cp_declarator *, tree);
1413 /* An erroneous declarator. */
1414 static cp_declarator *cp_error_declarator;
1416 /* The obstack on which declarators and related data structures are
1417 allocated. */
1418 static struct obstack declarator_obstack;
1420 /* Alloc BYTES from the declarator memory pool. */
1422 static inline void *
1423 alloc_declarator (size_t bytes)
1425 return obstack_alloc (&declarator_obstack, bytes);
1428 /* Allocate a declarator of the indicated KIND. Clear fields that are
1429 common to all declarators. */
1431 static cp_declarator *
1432 make_declarator (cp_declarator_kind kind)
1434 cp_declarator *declarator;
1436 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1437 declarator->kind = kind;
1438 declarator->parenthesized = UNKNOWN_LOCATION;
1439 declarator->attributes = NULL_TREE;
1440 declarator->std_attributes = NULL_TREE;
1441 declarator->declarator = NULL;
1442 declarator->parameter_pack_p = false;
1443 declarator->id_loc = UNKNOWN_LOCATION;
1445 return declarator;
1448 /* Make a declarator for a generalized identifier. If
1449 QUALIFYING_SCOPE is non-NULL, the identifier is
1450 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1451 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1452 is, if any. */
1454 static cp_declarator *
1455 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1456 special_function_kind sfk, location_t id_location)
1458 cp_declarator *declarator;
1460 /* It is valid to write:
1462 class C { void f(); };
1463 typedef C D;
1464 void D::f();
1466 The standard is not clear about whether `typedef const C D' is
1467 legal; as of 2002-09-15 the committee is considering that
1468 question. EDG 3.0 allows that syntax. Therefore, we do as
1469 well. */
1470 if (qualifying_scope && TYPE_P (qualifying_scope))
1471 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1473 gcc_assert (identifier_p (unqualified_name)
1474 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1475 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1477 declarator = make_declarator (cdk_id);
1478 declarator->u.id.qualifying_scope = qualifying_scope;
1479 declarator->u.id.unqualified_name = unqualified_name;
1480 declarator->u.id.sfk = sfk;
1481 declarator->id_loc = id_location;
1483 return declarator;
1486 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1487 of modifiers such as const or volatile to apply to the pointer
1488 type, represented as identifiers. ATTRIBUTES represent the attributes that
1489 appertain to the pointer or reference. */
1491 cp_declarator *
1492 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1493 tree attributes)
1495 cp_declarator *declarator;
1497 declarator = make_declarator (cdk_pointer);
1498 declarator->declarator = target;
1499 declarator->u.pointer.qualifiers = cv_qualifiers;
1500 declarator->u.pointer.class_type = NULL_TREE;
1501 if (target)
1503 declarator->id_loc = target->id_loc;
1504 declarator->parameter_pack_p = target->parameter_pack_p;
1505 target->parameter_pack_p = false;
1507 else
1508 declarator->parameter_pack_p = false;
1510 declarator->std_attributes = attributes;
1512 return declarator;
1515 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1516 represent the attributes that appertain to the pointer or
1517 reference. */
1519 cp_declarator *
1520 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1521 bool rvalue_ref, tree attributes)
1523 cp_declarator *declarator;
1525 declarator = make_declarator (cdk_reference);
1526 declarator->declarator = target;
1527 declarator->u.reference.qualifiers = cv_qualifiers;
1528 declarator->u.reference.rvalue_ref = rvalue_ref;
1529 if (target)
1531 declarator->id_loc = target->id_loc;
1532 declarator->parameter_pack_p = target->parameter_pack_p;
1533 target->parameter_pack_p = false;
1535 else
1536 declarator->parameter_pack_p = false;
1538 declarator->std_attributes = attributes;
1540 return declarator;
1543 /* Like make_pointer_declarator -- but for a pointer to a non-static
1544 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1545 appertain to the pointer or reference. */
1547 cp_declarator *
1548 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1549 cp_declarator *pointee,
1550 tree attributes)
1552 cp_declarator *declarator;
1554 declarator = make_declarator (cdk_ptrmem);
1555 declarator->declarator = pointee;
1556 declarator->u.pointer.qualifiers = cv_qualifiers;
1557 declarator->u.pointer.class_type = class_type;
1559 if (pointee)
1561 declarator->parameter_pack_p = pointee->parameter_pack_p;
1562 pointee->parameter_pack_p = false;
1564 else
1565 declarator->parameter_pack_p = false;
1567 declarator->std_attributes = attributes;
1569 return declarator;
1572 /* Make a declarator for the function given by TARGET, with the
1573 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1574 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1575 indicates what exceptions can be thrown. */
1577 cp_declarator *
1578 make_call_declarator (cp_declarator *target,
1579 tree parms,
1580 cp_cv_quals cv_qualifiers,
1581 cp_virt_specifiers virt_specifiers,
1582 cp_ref_qualifier ref_qualifier,
1583 tree tx_qualifier,
1584 tree exception_specification,
1585 tree late_return_type,
1586 tree requires_clause)
1588 cp_declarator *declarator;
1590 declarator = make_declarator (cdk_function);
1591 declarator->declarator = target;
1592 declarator->u.function.parameters = parms;
1593 declarator->u.function.qualifiers = cv_qualifiers;
1594 declarator->u.function.virt_specifiers = virt_specifiers;
1595 declarator->u.function.ref_qualifier = ref_qualifier;
1596 declarator->u.function.tx_qualifier = tx_qualifier;
1597 declarator->u.function.exception_specification = exception_specification;
1598 declarator->u.function.late_return_type = late_return_type;
1599 declarator->u.function.requires_clause = requires_clause;
1600 if (target)
1602 declarator->id_loc = target->id_loc;
1603 declarator->parameter_pack_p = target->parameter_pack_p;
1604 target->parameter_pack_p = false;
1606 else
1607 declarator->parameter_pack_p = false;
1609 return declarator;
1612 /* Make a declarator for an array of BOUNDS elements, each of which is
1613 defined by ELEMENT. */
1615 cp_declarator *
1616 make_array_declarator (cp_declarator *element, tree bounds)
1618 cp_declarator *declarator;
1620 declarator = make_declarator (cdk_array);
1621 declarator->declarator = element;
1622 declarator->u.array.bounds = bounds;
1623 if (element)
1625 declarator->id_loc = element->id_loc;
1626 declarator->parameter_pack_p = element->parameter_pack_p;
1627 element->parameter_pack_p = false;
1629 else
1630 declarator->parameter_pack_p = false;
1632 return declarator;
1635 /* Determine whether the declarator we've seen so far can be a
1636 parameter pack, when followed by an ellipsis. */
1637 static bool
1638 declarator_can_be_parameter_pack (cp_declarator *declarator)
1640 if (declarator && declarator->parameter_pack_p)
1641 /* We already saw an ellipsis. */
1642 return false;
1644 /* Search for a declarator name, or any other declarator that goes
1645 after the point where the ellipsis could appear in a parameter
1646 pack. If we find any of these, then this declarator can not be
1647 made into a parameter pack. */
1648 bool found = false;
1649 while (declarator && !found)
1651 switch ((int)declarator->kind)
1653 case cdk_id:
1654 case cdk_array:
1655 case cdk_decomp:
1656 found = true;
1657 break;
1659 case cdk_error:
1660 return true;
1662 default:
1663 declarator = declarator->declarator;
1664 break;
1668 return !found;
1671 cp_parameter_declarator *no_parameters;
1673 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1674 DECLARATOR and DEFAULT_ARGUMENT. */
1676 cp_parameter_declarator *
1677 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1678 cp_declarator *declarator,
1679 tree default_argument,
1680 location_t loc,
1681 bool template_parameter_pack_p = false)
1683 cp_parameter_declarator *parameter;
1685 parameter = ((cp_parameter_declarator *)
1686 alloc_declarator (sizeof (cp_parameter_declarator)));
1687 parameter->next = NULL;
1688 if (decl_specifiers)
1689 parameter->decl_specifiers = *decl_specifiers;
1690 else
1691 clear_decl_specs (&parameter->decl_specifiers);
1692 parameter->declarator = declarator;
1693 parameter->default_argument = default_argument;
1694 parameter->template_parameter_pack_p = template_parameter_pack_p;
1695 parameter->loc = loc;
1697 return parameter;
1700 /* Returns true iff DECLARATOR is a declaration for a function. */
1702 static bool
1703 function_declarator_p (const cp_declarator *declarator)
1705 while (declarator)
1707 if (declarator->kind == cdk_function
1708 && declarator->declarator->kind == cdk_id)
1709 return true;
1710 if (declarator->kind == cdk_id
1711 || declarator->kind == cdk_decomp
1712 || declarator->kind == cdk_error)
1713 return false;
1714 declarator = declarator->declarator;
1716 return false;
1719 /* The parser. */
1721 /* Overview
1722 --------
1724 A cp_parser parses the token stream as specified by the C++
1725 grammar. Its job is purely parsing, not semantic analysis. For
1726 example, the parser breaks the token stream into declarators,
1727 expressions, statements, and other similar syntactic constructs.
1728 It does not check that the types of the expressions on either side
1729 of an assignment-statement are compatible, or that a function is
1730 not declared with a parameter of type `void'.
1732 The parser invokes routines elsewhere in the compiler to perform
1733 semantic analysis and to build up the abstract syntax tree for the
1734 code processed.
1736 The parser (and the template instantiation code, which is, in a
1737 way, a close relative of parsing) are the only parts of the
1738 compiler that should be calling push_scope and pop_scope, or
1739 related functions. The parser (and template instantiation code)
1740 keeps track of what scope is presently active; everything else
1741 should simply honor that. (The code that generates static
1742 initializers may also need to set the scope, in order to check
1743 access control correctly when emitting the initializers.)
1745 Methodology
1746 -----------
1748 The parser is of the standard recursive-descent variety. Upcoming
1749 tokens in the token stream are examined in order to determine which
1750 production to use when parsing a non-terminal. Some C++ constructs
1751 require arbitrary look ahead to disambiguate. For example, it is
1752 impossible, in the general case, to tell whether a statement is an
1753 expression or declaration without scanning the entire statement.
1754 Therefore, the parser is capable of "parsing tentatively." When the
1755 parser is not sure what construct comes next, it enters this mode.
1756 Then, while we attempt to parse the construct, the parser queues up
1757 error messages, rather than issuing them immediately, and saves the
1758 tokens it consumes. If the construct is parsed successfully, the
1759 parser "commits", i.e., it issues any queued error messages and
1760 the tokens that were being preserved are permanently discarded.
1761 If, however, the construct is not parsed successfully, the parser
1762 rolls back its state completely so that it can resume parsing using
1763 a different alternative.
1765 Future Improvements
1766 -------------------
1768 The performance of the parser could probably be improved substantially.
1769 We could often eliminate the need to parse tentatively by looking ahead
1770 a little bit. In some places, this approach might not entirely eliminate
1771 the need to parse tentatively, but it might still speed up the average
1772 case. */
1774 /* Flags that are passed to some parsing functions. These values can
1775 be bitwise-ored together. */
1777 enum
1779 /* No flags. */
1780 CP_PARSER_FLAGS_NONE = 0x0,
1781 /* The construct is optional. If it is not present, then no error
1782 should be issued. */
1783 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1784 /* When parsing a type-specifier, treat user-defined type-names
1785 as non-type identifiers. */
1786 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1787 /* When parsing a type-specifier, do not try to parse a class-specifier
1788 or enum-specifier. */
1789 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1790 /* When parsing a decl-specifier-seq, only allow type-specifier or
1791 constexpr. */
1792 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1793 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1794 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1797 /* This type is used for parameters and variables which hold
1798 combinations of the above flags. */
1799 typedef int cp_parser_flags;
1801 /* The different kinds of declarators we want to parse. */
1803 enum cp_parser_declarator_kind
1805 /* We want an abstract declarator. */
1806 CP_PARSER_DECLARATOR_ABSTRACT,
1807 /* We want a named declarator. */
1808 CP_PARSER_DECLARATOR_NAMED,
1809 /* We don't mind, but the name must be an unqualified-id. */
1810 CP_PARSER_DECLARATOR_EITHER
1813 /* The precedence values used to parse binary expressions. The minimum value
1814 of PREC must be 1, because zero is reserved to quickly discriminate
1815 binary operators from other tokens. */
1817 enum cp_parser_prec
1819 PREC_NOT_OPERATOR,
1820 PREC_LOGICAL_OR_EXPRESSION,
1821 PREC_LOGICAL_AND_EXPRESSION,
1822 PREC_INCLUSIVE_OR_EXPRESSION,
1823 PREC_EXCLUSIVE_OR_EXPRESSION,
1824 PREC_AND_EXPRESSION,
1825 PREC_EQUALITY_EXPRESSION,
1826 PREC_RELATIONAL_EXPRESSION,
1827 PREC_SHIFT_EXPRESSION,
1828 PREC_ADDITIVE_EXPRESSION,
1829 PREC_MULTIPLICATIVE_EXPRESSION,
1830 PREC_PM_EXPRESSION,
1831 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1834 /* A mapping from a token type to a corresponding tree node type, with a
1835 precedence value. */
1837 struct cp_parser_binary_operations_map_node
1839 /* The token type. */
1840 enum cpp_ttype token_type;
1841 /* The corresponding tree code. */
1842 enum tree_code tree_type;
1843 /* The precedence of this operator. */
1844 enum cp_parser_prec prec;
1847 struct cp_parser_expression_stack_entry
1849 /* Left hand side of the binary operation we are currently
1850 parsing. */
1851 cp_expr lhs;
1852 /* Original tree code for left hand side, if it was a binary
1853 expression itself (used for -Wparentheses). */
1854 enum tree_code lhs_type;
1855 /* Tree code for the binary operation we are parsing. */
1856 enum tree_code tree_type;
1857 /* Precedence of the binary operation we are parsing. */
1858 enum cp_parser_prec prec;
1859 /* Location of the binary operation we are parsing. */
1860 location_t loc;
1863 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1864 entries because precedence levels on the stack are monotonically
1865 increasing. */
1866 typedef struct cp_parser_expression_stack_entry
1867 cp_parser_expression_stack[NUM_PREC_VALUES];
1869 /* Prototypes. */
1871 /* Constructors and destructors. */
1873 static cp_parser_context *cp_parser_context_new
1874 (cp_parser_context *);
1876 /* Class variables. */
1878 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1880 /* The operator-precedence table used by cp_parser_binary_expression.
1881 Transformed into an associative array (binops_by_token) by
1882 cp_parser_new. */
1884 static const cp_parser_binary_operations_map_node binops[] = {
1885 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1886 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1888 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1889 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1890 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1892 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1893 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1895 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1896 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1898 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1899 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1900 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1901 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1903 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1904 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1906 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1908 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1910 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1912 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1914 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1917 /* The same as binops, but initialized by cp_parser_new so that
1918 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1919 for speed. */
1920 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1922 /* Constructors and destructors. */
1924 /* Construct a new context. The context below this one on the stack
1925 is given by NEXT. */
1927 static cp_parser_context *
1928 cp_parser_context_new (cp_parser_context* next)
1930 cp_parser_context *context;
1932 /* Allocate the storage. */
1933 if (cp_parser_context_free_list != NULL)
1935 /* Pull the first entry from the free list. */
1936 context = cp_parser_context_free_list;
1937 cp_parser_context_free_list = context->next;
1938 memset (context, 0, sizeof (*context));
1940 else
1941 context = ggc_cleared_alloc<cp_parser_context> ();
1943 /* No errors have occurred yet in this context. */
1944 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1945 /* If this is not the bottommost context, copy information that we
1946 need from the previous context. */
1947 if (next)
1949 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1950 expression, then we are parsing one in this context, too. */
1951 context->object_type = next->object_type;
1952 /* Thread the stack. */
1953 context->next = next;
1956 return context;
1959 /* Managing the unparsed function queues. */
1961 #define unparsed_funs_with_default_args \
1962 parser->unparsed_queues->last ().funs_with_default_args
1963 #define unparsed_funs_with_definitions \
1964 parser->unparsed_queues->last ().funs_with_definitions
1965 #define unparsed_nsdmis \
1966 parser->unparsed_queues->last ().nsdmis
1967 #define unparsed_classes \
1968 parser->unparsed_queues->last ().classes
1970 static void
1971 push_unparsed_function_queues (cp_parser *parser)
1973 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1974 vec_safe_push (parser->unparsed_queues, e);
1977 static void
1978 pop_unparsed_function_queues (cp_parser *parser)
1980 release_tree_vector (unparsed_funs_with_definitions);
1981 parser->unparsed_queues->pop ();
1984 /* Prototypes. */
1986 /* Constructors and destructors. */
1988 static cp_parser *cp_parser_new
1989 (void);
1991 /* Routines to parse various constructs.
1993 Those that return `tree' will return the error_mark_node (rather
1994 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1995 Sometimes, they will return an ordinary node if error-recovery was
1996 attempted, even though a parse error occurred. So, to check
1997 whether or not a parse error occurred, you should always use
1998 cp_parser_error_occurred. If the construct is optional (indicated
1999 either by an `_opt' in the name of the function that does the
2000 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2001 the construct is not present. */
2003 /* Lexical conventions [gram.lex] */
2005 static cp_expr cp_parser_identifier
2006 (cp_parser *);
2007 static cp_expr cp_parser_string_literal
2008 (cp_parser *, bool, bool, bool);
2009 static cp_expr cp_parser_userdef_char_literal
2010 (cp_parser *);
2011 static tree cp_parser_userdef_string_literal
2012 (tree);
2013 static cp_expr cp_parser_userdef_numeric_literal
2014 (cp_parser *);
2016 /* Basic concepts [gram.basic] */
2018 static void cp_parser_translation_unit (cp_parser *);
2020 /* Expressions [gram.expr] */
2022 static cp_expr cp_parser_primary_expression
2023 (cp_parser *, bool, bool, bool, cp_id_kind *);
2024 static cp_expr cp_parser_id_expression
2025 (cp_parser *, bool, bool, bool *, bool, bool);
2026 static cp_expr cp_parser_unqualified_id
2027 (cp_parser *, bool, bool, bool, bool);
2028 static tree cp_parser_nested_name_specifier_opt
2029 (cp_parser *, bool, bool, bool, bool, bool = false);
2030 static tree cp_parser_nested_name_specifier
2031 (cp_parser *, bool, bool, bool, bool);
2032 static tree cp_parser_qualifying_entity
2033 (cp_parser *, bool, bool, bool, bool, bool);
2034 static cp_expr cp_parser_postfix_expression
2035 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2036 static tree cp_parser_postfix_open_square_expression
2037 (cp_parser *, tree, bool, bool);
2038 static tree cp_parser_postfix_dot_deref_expression
2039 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2040 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2041 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2042 bool = false);
2043 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2044 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2045 static void cp_parser_pseudo_destructor_name
2046 (cp_parser *, tree, tree *, tree *);
2047 static cp_expr cp_parser_unary_expression
2048 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2049 static enum tree_code cp_parser_unary_operator
2050 (cp_token *);
2051 static tree cp_parser_has_attribute_expression
2052 (cp_parser *);
2053 static tree cp_parser_new_expression
2054 (cp_parser *);
2055 static vec<tree, va_gc> *cp_parser_new_placement
2056 (cp_parser *);
2057 static tree cp_parser_new_type_id
2058 (cp_parser *, tree *);
2059 static cp_declarator *cp_parser_new_declarator_opt
2060 (cp_parser *);
2061 static cp_declarator *cp_parser_direct_new_declarator
2062 (cp_parser *);
2063 static vec<tree, va_gc> *cp_parser_new_initializer
2064 (cp_parser *);
2065 static tree cp_parser_delete_expression
2066 (cp_parser *);
2067 static cp_expr cp_parser_cast_expression
2068 (cp_parser *, bool, bool, bool, cp_id_kind *);
2069 static cp_expr cp_parser_binary_expression
2070 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2071 static tree cp_parser_question_colon_clause
2072 (cp_parser *, cp_expr);
2073 static cp_expr cp_parser_assignment_expression
2074 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2075 static enum tree_code cp_parser_assignment_operator_opt
2076 (cp_parser *);
2077 static cp_expr cp_parser_expression
2078 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2079 static cp_expr cp_parser_constant_expression
2080 (cp_parser *, bool = false, bool * = NULL, bool = false);
2081 static cp_expr cp_parser_builtin_offsetof
2082 (cp_parser *);
2083 static cp_expr cp_parser_lambda_expression
2084 (cp_parser *);
2085 static void cp_parser_lambda_introducer
2086 (cp_parser *, tree);
2087 static bool cp_parser_lambda_declarator_opt
2088 (cp_parser *, tree);
2089 static void cp_parser_lambda_body
2090 (cp_parser *, tree);
2092 /* Statements [gram.stmt.stmt] */
2094 static void cp_parser_statement
2095 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2096 static void cp_parser_label_for_labeled_statement
2097 (cp_parser *, tree);
2098 static tree cp_parser_expression_statement
2099 (cp_parser *, tree);
2100 static tree cp_parser_compound_statement
2101 (cp_parser *, tree, int, bool);
2102 static void cp_parser_statement_seq_opt
2103 (cp_parser *, tree);
2104 static tree cp_parser_selection_statement
2105 (cp_parser *, bool *, vec<tree> *);
2106 static tree cp_parser_condition
2107 (cp_parser *);
2108 static tree cp_parser_iteration_statement
2109 (cp_parser *, bool *, bool, unsigned short);
2110 static bool cp_parser_init_statement
2111 (cp_parser *, tree *decl);
2112 static tree cp_parser_for
2113 (cp_parser *, bool, unsigned short);
2114 static tree cp_parser_c_for
2115 (cp_parser *, tree, tree, bool, unsigned short);
2116 static tree cp_parser_range_for
2117 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2118 static void do_range_for_auto_deduction
2119 (tree, tree);
2120 static tree cp_parser_perform_range_for_lookup
2121 (tree, tree *, tree *);
2122 static tree cp_parser_range_for_member_function
2123 (tree, tree);
2124 static tree cp_parser_jump_statement
2125 (cp_parser *);
2126 static void cp_parser_declaration_statement
2127 (cp_parser *);
2129 static tree cp_parser_implicitly_scoped_statement
2130 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2131 static void cp_parser_already_scoped_statement
2132 (cp_parser *, bool *, const token_indent_info &);
2134 /* Declarations [gram.dcl.dcl] */
2136 static void cp_parser_declaration_seq_opt
2137 (cp_parser *);
2138 static void cp_parser_declaration
2139 (cp_parser *);
2140 static void cp_parser_toplevel_declaration
2141 (cp_parser *);
2142 static void cp_parser_block_declaration
2143 (cp_parser *, bool);
2144 static void cp_parser_simple_declaration
2145 (cp_parser *, bool, tree *);
2146 static void cp_parser_decl_specifier_seq
2147 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2148 static tree cp_parser_storage_class_specifier_opt
2149 (cp_parser *);
2150 static tree cp_parser_function_specifier_opt
2151 (cp_parser *, cp_decl_specifier_seq *);
2152 static tree cp_parser_type_specifier
2153 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2154 int *, bool *);
2155 static tree cp_parser_simple_type_specifier
2156 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2157 static tree cp_parser_type_name
2158 (cp_parser *, bool);
2159 static tree cp_parser_type_name
2160 (cp_parser *);
2161 static tree cp_parser_nonclass_name
2162 (cp_parser* parser);
2163 static tree cp_parser_elaborated_type_specifier
2164 (cp_parser *, bool, bool);
2165 static tree cp_parser_enum_specifier
2166 (cp_parser *);
2167 static void cp_parser_enumerator_list
2168 (cp_parser *, tree);
2169 static void cp_parser_enumerator_definition
2170 (cp_parser *, tree);
2171 static tree cp_parser_namespace_name
2172 (cp_parser *);
2173 static void cp_parser_namespace_definition
2174 (cp_parser *);
2175 static void cp_parser_namespace_body
2176 (cp_parser *);
2177 static tree cp_parser_qualified_namespace_specifier
2178 (cp_parser *);
2179 static void cp_parser_namespace_alias_definition
2180 (cp_parser *);
2181 static bool cp_parser_using_declaration
2182 (cp_parser *, bool);
2183 static void cp_parser_using_directive
2184 (cp_parser *);
2185 static tree cp_parser_alias_declaration
2186 (cp_parser *);
2187 static void cp_parser_asm_definition
2188 (cp_parser *);
2189 static void cp_parser_linkage_specification
2190 (cp_parser *);
2191 static void cp_parser_static_assert
2192 (cp_parser *, bool);
2193 static tree cp_parser_decltype
2194 (cp_parser *);
2195 static tree cp_parser_decomposition_declaration
2196 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2198 /* Declarators [gram.dcl.decl] */
2200 static tree cp_parser_init_declarator
2201 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2202 bool, bool, int, bool *, tree *, location_t *, tree *);
2203 static cp_declarator *cp_parser_declarator
2204 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2205 static cp_declarator *cp_parser_direct_declarator
2206 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2207 static enum tree_code cp_parser_ptr_operator
2208 (cp_parser *, tree *, cp_cv_quals *, tree *);
2209 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2210 (cp_parser *);
2211 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2212 (cp_parser *);
2213 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2214 (cp_parser *);
2215 static tree cp_parser_tx_qualifier_opt
2216 (cp_parser *);
2217 static tree cp_parser_late_return_type_opt
2218 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2219 static tree cp_parser_declarator_id
2220 (cp_parser *, bool);
2221 static tree cp_parser_type_id
2222 (cp_parser *, location_t * = NULL);
2223 static tree cp_parser_template_type_arg
2224 (cp_parser *);
2225 static tree cp_parser_trailing_type_id (cp_parser *);
2226 static tree cp_parser_type_id_1
2227 (cp_parser *, bool, bool, location_t *);
2228 static void cp_parser_type_specifier_seq
2229 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2230 static tree cp_parser_parameter_declaration_clause
2231 (cp_parser *);
2232 static tree cp_parser_parameter_declaration_list
2233 (cp_parser *);
2234 static cp_parameter_declarator *cp_parser_parameter_declaration
2235 (cp_parser *, bool, bool *);
2236 static tree cp_parser_default_argument
2237 (cp_parser *, bool);
2238 static void cp_parser_function_body
2239 (cp_parser *, bool);
2240 static tree cp_parser_initializer
2241 (cp_parser *, bool *, bool *, bool = false);
2242 static cp_expr cp_parser_initializer_clause
2243 (cp_parser *, bool *);
2244 static cp_expr cp_parser_braced_list
2245 (cp_parser*, bool*);
2246 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2247 (cp_parser *, bool *);
2249 static void cp_parser_ctor_initializer_opt_and_function_body
2250 (cp_parser *, bool);
2252 static tree cp_parser_late_parsing_omp_declare_simd
2253 (cp_parser *, tree);
2255 static tree cp_parser_late_parsing_oacc_routine
2256 (cp_parser *, tree);
2258 static tree synthesize_implicit_template_parm
2259 (cp_parser *, tree);
2260 static tree finish_fully_implicit_template
2261 (cp_parser *, tree);
2262 static void abort_fully_implicit_template
2263 (cp_parser *);
2265 /* Classes [gram.class] */
2267 static tree cp_parser_class_name
2268 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2269 static tree cp_parser_class_specifier
2270 (cp_parser *);
2271 static tree cp_parser_class_head
2272 (cp_parser *, bool *);
2273 static enum tag_types cp_parser_class_key
2274 (cp_parser *);
2275 static void cp_parser_type_parameter_key
2276 (cp_parser* parser);
2277 static void cp_parser_member_specification_opt
2278 (cp_parser *);
2279 static void cp_parser_member_declaration
2280 (cp_parser *);
2281 static tree cp_parser_pure_specifier
2282 (cp_parser *);
2283 static tree cp_parser_constant_initializer
2284 (cp_parser *);
2286 /* Derived classes [gram.class.derived] */
2288 static tree cp_parser_base_clause
2289 (cp_parser *);
2290 static tree cp_parser_base_specifier
2291 (cp_parser *);
2293 /* Special member functions [gram.special] */
2295 static tree cp_parser_conversion_function_id
2296 (cp_parser *);
2297 static tree cp_parser_conversion_type_id
2298 (cp_parser *);
2299 static cp_declarator *cp_parser_conversion_declarator_opt
2300 (cp_parser *);
2301 static void cp_parser_ctor_initializer_opt
2302 (cp_parser *);
2303 static void cp_parser_mem_initializer_list
2304 (cp_parser *);
2305 static tree cp_parser_mem_initializer
2306 (cp_parser *);
2307 static tree cp_parser_mem_initializer_id
2308 (cp_parser *);
2310 /* Overloading [gram.over] */
2312 static cp_expr cp_parser_operator_function_id
2313 (cp_parser *);
2314 static cp_expr cp_parser_operator
2315 (cp_parser *, location_t);
2317 /* Templates [gram.temp] */
2319 static void cp_parser_template_declaration
2320 (cp_parser *, bool);
2321 static tree cp_parser_template_parameter_list
2322 (cp_parser *);
2323 static tree cp_parser_template_parameter
2324 (cp_parser *, bool *, bool *);
2325 static tree cp_parser_type_parameter
2326 (cp_parser *, bool *);
2327 static tree cp_parser_template_id
2328 (cp_parser *, bool, bool, enum tag_types, bool);
2329 static tree cp_parser_template_name
2330 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2331 static tree cp_parser_template_argument_list
2332 (cp_parser *);
2333 static tree cp_parser_template_argument
2334 (cp_parser *);
2335 static void cp_parser_explicit_instantiation
2336 (cp_parser *);
2337 static void cp_parser_explicit_specialization
2338 (cp_parser *);
2340 /* Exception handling [gram.exception] */
2342 static tree cp_parser_try_block
2343 (cp_parser *);
2344 static void cp_parser_function_try_block
2345 (cp_parser *);
2346 static void cp_parser_handler_seq
2347 (cp_parser *);
2348 static void cp_parser_handler
2349 (cp_parser *);
2350 static tree cp_parser_exception_declaration
2351 (cp_parser *);
2352 static tree cp_parser_throw_expression
2353 (cp_parser *);
2354 static tree cp_parser_exception_specification_opt
2355 (cp_parser *);
2356 static tree cp_parser_type_id_list
2357 (cp_parser *);
2359 /* GNU Extensions */
2361 static tree cp_parser_asm_specification_opt
2362 (cp_parser *);
2363 static tree cp_parser_asm_operand_list
2364 (cp_parser *);
2365 static tree cp_parser_asm_clobber_list
2366 (cp_parser *);
2367 static tree cp_parser_asm_label_list
2368 (cp_parser *);
2369 static bool cp_next_tokens_can_be_attribute_p
2370 (cp_parser *);
2371 static bool cp_next_tokens_can_be_gnu_attribute_p
2372 (cp_parser *);
2373 static bool cp_next_tokens_can_be_std_attribute_p
2374 (cp_parser *);
2375 static bool cp_nth_tokens_can_be_std_attribute_p
2376 (cp_parser *, size_t);
2377 static bool cp_nth_tokens_can_be_gnu_attribute_p
2378 (cp_parser *, size_t);
2379 static bool cp_nth_tokens_can_be_attribute_p
2380 (cp_parser *, size_t);
2381 static tree cp_parser_attributes_opt
2382 (cp_parser *);
2383 static tree cp_parser_gnu_attributes_opt
2384 (cp_parser *);
2385 static tree cp_parser_gnu_attribute_list
2386 (cp_parser *, bool = false);
2387 static tree cp_parser_std_attribute
2388 (cp_parser *, tree);
2389 static tree cp_parser_std_attribute_spec
2390 (cp_parser *);
2391 static tree cp_parser_std_attribute_spec_seq
2392 (cp_parser *);
2393 static size_t cp_parser_skip_attributes_opt
2394 (cp_parser *, size_t);
2395 static bool cp_parser_extension_opt
2396 (cp_parser *, int *);
2397 static void cp_parser_label_declaration
2398 (cp_parser *);
2400 /* Concept Extensions */
2402 static tree cp_parser_requires_clause
2403 (cp_parser *);
2404 static tree cp_parser_requires_clause_opt
2405 (cp_parser *);
2406 static tree cp_parser_requires_expression
2407 (cp_parser *);
2408 static tree cp_parser_requirement_parameter_list
2409 (cp_parser *);
2410 static tree cp_parser_requirement_body
2411 (cp_parser *);
2412 static tree cp_parser_requirement_list
2413 (cp_parser *);
2414 static tree cp_parser_requirement
2415 (cp_parser *);
2416 static tree cp_parser_simple_requirement
2417 (cp_parser *);
2418 static tree cp_parser_compound_requirement
2419 (cp_parser *);
2420 static tree cp_parser_type_requirement
2421 (cp_parser *);
2422 static tree cp_parser_nested_requirement
2423 (cp_parser *);
2425 /* Transactional Memory Extensions */
2427 static tree cp_parser_transaction
2428 (cp_parser *, cp_token *);
2429 static tree cp_parser_transaction_expression
2430 (cp_parser *, enum rid);
2431 static void cp_parser_function_transaction
2432 (cp_parser *, enum rid);
2433 static tree cp_parser_transaction_cancel
2434 (cp_parser *);
2436 enum pragma_context {
2437 pragma_external,
2438 pragma_member,
2439 pragma_objc_icode,
2440 pragma_stmt,
2441 pragma_compound
2443 static bool cp_parser_pragma
2444 (cp_parser *, enum pragma_context, bool *);
2446 /* Objective-C++ Productions */
2448 static tree cp_parser_objc_message_receiver
2449 (cp_parser *);
2450 static tree cp_parser_objc_message_args
2451 (cp_parser *);
2452 static tree cp_parser_objc_message_expression
2453 (cp_parser *);
2454 static cp_expr cp_parser_objc_encode_expression
2455 (cp_parser *);
2456 static tree cp_parser_objc_defs_expression
2457 (cp_parser *);
2458 static tree cp_parser_objc_protocol_expression
2459 (cp_parser *);
2460 static tree cp_parser_objc_selector_expression
2461 (cp_parser *);
2462 static cp_expr cp_parser_objc_expression
2463 (cp_parser *);
2464 static bool cp_parser_objc_selector_p
2465 (enum cpp_ttype);
2466 static tree cp_parser_objc_selector
2467 (cp_parser *);
2468 static tree cp_parser_objc_protocol_refs_opt
2469 (cp_parser *);
2470 static void cp_parser_objc_declaration
2471 (cp_parser *, tree);
2472 static tree cp_parser_objc_statement
2473 (cp_parser *);
2474 static bool cp_parser_objc_valid_prefix_attributes
2475 (cp_parser *, tree *);
2476 static void cp_parser_objc_at_property_declaration
2477 (cp_parser *) ;
2478 static void cp_parser_objc_at_synthesize_declaration
2479 (cp_parser *) ;
2480 static void cp_parser_objc_at_dynamic_declaration
2481 (cp_parser *) ;
2482 static tree cp_parser_objc_struct_declaration
2483 (cp_parser *) ;
2485 /* Utility Routines */
2487 static cp_expr cp_parser_lookup_name
2488 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2489 static tree cp_parser_lookup_name_simple
2490 (cp_parser *, tree, location_t);
2491 static tree cp_parser_maybe_treat_template_as_class
2492 (tree, bool);
2493 static bool cp_parser_check_declarator_template_parameters
2494 (cp_parser *, cp_declarator *, location_t);
2495 static bool cp_parser_check_template_parameters
2496 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2497 static cp_expr cp_parser_simple_cast_expression
2498 (cp_parser *);
2499 static tree cp_parser_global_scope_opt
2500 (cp_parser *, bool);
2501 static bool cp_parser_constructor_declarator_p
2502 (cp_parser *, bool);
2503 static tree cp_parser_function_definition_from_specifiers_and_declarator
2504 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2505 static tree cp_parser_function_definition_after_declarator
2506 (cp_parser *, bool);
2507 static bool cp_parser_template_declaration_after_export
2508 (cp_parser *, bool);
2509 static void cp_parser_perform_template_parameter_access_checks
2510 (vec<deferred_access_check, va_gc> *);
2511 static tree cp_parser_single_declaration
2512 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2513 static cp_expr cp_parser_functional_cast
2514 (cp_parser *, tree);
2515 static tree cp_parser_save_member_function_body
2516 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2517 static tree cp_parser_save_nsdmi
2518 (cp_parser *);
2519 static tree cp_parser_enclosed_template_argument_list
2520 (cp_parser *);
2521 static void cp_parser_save_default_args
2522 (cp_parser *, tree);
2523 static void cp_parser_late_parsing_for_member
2524 (cp_parser *, tree);
2525 static tree cp_parser_late_parse_one_default_arg
2526 (cp_parser *, tree, tree, tree);
2527 static void cp_parser_late_parsing_nsdmi
2528 (cp_parser *, tree);
2529 static void cp_parser_late_parsing_default_args
2530 (cp_parser *, tree);
2531 static tree cp_parser_sizeof_operand
2532 (cp_parser *, enum rid);
2533 static cp_expr cp_parser_trait_expr
2534 (cp_parser *, enum rid);
2535 static bool cp_parser_declares_only_class_p
2536 (cp_parser *);
2537 static void cp_parser_set_storage_class
2538 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2539 static void cp_parser_set_decl_spec_type
2540 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2541 static void set_and_check_decl_spec_loc
2542 (cp_decl_specifier_seq *decl_specs,
2543 cp_decl_spec ds, cp_token *);
2544 static bool cp_parser_friend_p
2545 (const cp_decl_specifier_seq *);
2546 static void cp_parser_required_error
2547 (cp_parser *, required_token, bool, location_t);
2548 static cp_token *cp_parser_require
2549 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2550 static cp_token *cp_parser_require_keyword
2551 (cp_parser *, enum rid, required_token);
2552 static bool cp_parser_token_starts_function_definition_p
2553 (cp_token *);
2554 static bool cp_parser_next_token_starts_class_definition_p
2555 (cp_parser *);
2556 static bool cp_parser_next_token_ends_template_argument_p
2557 (cp_parser *);
2558 static bool cp_parser_nth_token_starts_template_argument_list_p
2559 (cp_parser *, size_t);
2560 static enum tag_types cp_parser_token_is_class_key
2561 (cp_token *);
2562 static enum tag_types cp_parser_token_is_type_parameter_key
2563 (cp_token *);
2564 static void cp_parser_check_class_key
2565 (enum tag_types, tree type);
2566 static void cp_parser_check_access_in_redeclaration
2567 (tree type, location_t location);
2568 static bool cp_parser_optional_template_keyword
2569 (cp_parser *);
2570 static void cp_parser_pre_parsed_nested_name_specifier
2571 (cp_parser *);
2572 static bool cp_parser_cache_group
2573 (cp_parser *, enum cpp_ttype, unsigned);
2574 static tree cp_parser_cache_defarg
2575 (cp_parser *parser, bool nsdmi);
2576 static void cp_parser_parse_tentatively
2577 (cp_parser *);
2578 static void cp_parser_commit_to_tentative_parse
2579 (cp_parser *);
2580 static void cp_parser_commit_to_topmost_tentative_parse
2581 (cp_parser *);
2582 static void cp_parser_abort_tentative_parse
2583 (cp_parser *);
2584 static bool cp_parser_parse_definitely
2585 (cp_parser *);
2586 static inline bool cp_parser_parsing_tentatively
2587 (cp_parser *);
2588 static bool cp_parser_uncommitted_to_tentative_parse_p
2589 (cp_parser *);
2590 static void cp_parser_error
2591 (cp_parser *, const char *);
2592 static void cp_parser_name_lookup_error
2593 (cp_parser *, tree, tree, name_lookup_error, location_t);
2594 static bool cp_parser_simulate_error
2595 (cp_parser *);
2596 static bool cp_parser_check_type_definition
2597 (cp_parser *);
2598 static void cp_parser_check_for_definition_in_return_type
2599 (cp_declarator *, tree, location_t type_location);
2600 static void cp_parser_check_for_invalid_template_id
2601 (cp_parser *, tree, enum tag_types, location_t location);
2602 static bool cp_parser_non_integral_constant_expression
2603 (cp_parser *, non_integral_constant);
2604 static void cp_parser_diagnose_invalid_type_name
2605 (cp_parser *, tree, location_t);
2606 static bool cp_parser_parse_and_diagnose_invalid_type_name
2607 (cp_parser *);
2608 static int cp_parser_skip_to_closing_parenthesis
2609 (cp_parser *, bool, bool, bool);
2610 static void cp_parser_skip_to_end_of_statement
2611 (cp_parser *);
2612 static void cp_parser_consume_semicolon_at_end_of_statement
2613 (cp_parser *);
2614 static void cp_parser_skip_to_end_of_block_or_statement
2615 (cp_parser *);
2616 static bool cp_parser_skip_to_closing_brace
2617 (cp_parser *);
2618 static void cp_parser_skip_to_end_of_template_parameter_list
2619 (cp_parser *);
2620 static void cp_parser_skip_to_pragma_eol
2621 (cp_parser*, cp_token *);
2622 static bool cp_parser_error_occurred
2623 (cp_parser *);
2624 static bool cp_parser_allow_gnu_extensions_p
2625 (cp_parser *);
2626 static bool cp_parser_is_pure_string_literal
2627 (cp_token *);
2628 static bool cp_parser_is_string_literal
2629 (cp_token *);
2630 static bool cp_parser_is_keyword
2631 (cp_token *, enum rid);
2632 static tree cp_parser_make_typename_type
2633 (cp_parser *, tree, location_t location);
2634 static cp_declarator * cp_parser_make_indirect_declarator
2635 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2636 static bool cp_parser_compound_literal_p
2637 (cp_parser *);
2638 static bool cp_parser_array_designator_p
2639 (cp_parser *);
2640 static bool cp_parser_init_statement_p
2641 (cp_parser *);
2642 static bool cp_parser_skip_to_closing_square_bracket
2643 (cp_parser *);
2645 /* Concept-related syntactic transformations */
2647 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2648 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2650 // -------------------------------------------------------------------------- //
2651 // Unevaluated Operand Guard
2653 // Implementation of an RAII helper for unevaluated operand parsing.
2654 cp_unevaluated::cp_unevaluated ()
2656 ++cp_unevaluated_operand;
2657 ++c_inhibit_evaluation_warnings;
2660 cp_unevaluated::~cp_unevaluated ()
2662 --c_inhibit_evaluation_warnings;
2663 --cp_unevaluated_operand;
2666 // -------------------------------------------------------------------------- //
2667 // Tentative Parsing
2669 /* Returns nonzero if we are parsing tentatively. */
2671 static inline bool
2672 cp_parser_parsing_tentatively (cp_parser* parser)
2674 return parser->context->next != NULL;
2677 /* Returns nonzero if TOKEN is a string literal. */
2679 static bool
2680 cp_parser_is_pure_string_literal (cp_token* token)
2682 return (token->type == CPP_STRING ||
2683 token->type == CPP_STRING16 ||
2684 token->type == CPP_STRING32 ||
2685 token->type == CPP_WSTRING ||
2686 token->type == CPP_UTF8STRING);
2689 /* Returns nonzero if TOKEN is a string literal
2690 of a user-defined string literal. */
2692 static bool
2693 cp_parser_is_string_literal (cp_token* token)
2695 return (cp_parser_is_pure_string_literal (token) ||
2696 token->type == CPP_STRING_USERDEF ||
2697 token->type == CPP_STRING16_USERDEF ||
2698 token->type == CPP_STRING32_USERDEF ||
2699 token->type == CPP_WSTRING_USERDEF ||
2700 token->type == CPP_UTF8STRING_USERDEF);
2703 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2705 static bool
2706 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2708 return token->keyword == keyword;
2711 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2712 PRAGMA_NONE. */
2714 static enum pragma_kind
2715 cp_parser_pragma_kind (cp_token *token)
2717 if (token->type != CPP_PRAGMA)
2718 return PRAGMA_NONE;
2719 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2720 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2723 /* Helper function for cp_parser_error.
2724 Having peeked a token of kind TOK1_KIND that might signify
2725 a conflict marker, peek successor tokens to determine
2726 if we actually do have a conflict marker.
2727 Specifically, we consider a run of 7 '<', '=' or '>' characters
2728 at the start of a line as a conflict marker.
2729 These come through the lexer as three pairs and a single,
2730 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2731 If it returns true, *OUT_LOC is written to with the location/range
2732 of the marker. */
2734 static bool
2735 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2736 location_t *out_loc)
2738 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2739 if (token2->type != tok1_kind)
2740 return false;
2741 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2742 if (token3->type != tok1_kind)
2743 return false;
2744 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2745 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2746 return false;
2748 /* It must be at the start of the line. */
2749 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2750 if (LOCATION_COLUMN (start_loc) != 1)
2751 return false;
2753 /* We have a conflict marker. Construct a location of the form:
2754 <<<<<<<
2755 ^~~~~~~
2756 with start == caret, finishing at the end of the marker. */
2757 location_t finish_loc = get_finish (token4->location);
2758 *out_loc = make_location (start_loc, start_loc, finish_loc);
2760 return true;
2763 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2764 RT_CLOSE_PAREN. */
2766 static const char *
2767 get_matching_symbol (required_token token_desc)
2769 switch (token_desc)
2771 default:
2772 gcc_unreachable ();
2773 return "";
2774 case RT_CLOSE_BRACE:
2775 return "{";
2776 case RT_CLOSE_PAREN:
2777 return "(";
2781 /* Attempt to convert TOKEN_DESC from a required_token to an
2782 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2784 static enum cpp_ttype
2785 get_required_cpp_ttype (required_token token_desc)
2787 switch (token_desc)
2789 case RT_SEMICOLON:
2790 return CPP_SEMICOLON;
2791 case RT_OPEN_PAREN:
2792 return CPP_OPEN_PAREN;
2793 case RT_CLOSE_BRACE:
2794 return CPP_CLOSE_BRACE;
2795 case RT_OPEN_BRACE:
2796 return CPP_OPEN_BRACE;
2797 case RT_CLOSE_SQUARE:
2798 return CPP_CLOSE_SQUARE;
2799 case RT_OPEN_SQUARE:
2800 return CPP_OPEN_SQUARE;
2801 case RT_COMMA:
2802 return CPP_COMMA;
2803 case RT_COLON:
2804 return CPP_COLON;
2805 case RT_CLOSE_PAREN:
2806 return CPP_CLOSE_PAREN;
2808 default:
2809 /* Use CPP_EOF as a "no completions possible" code. */
2810 return CPP_EOF;
2815 /* Subroutine of cp_parser_error and cp_parser_required_error.
2817 Issue a diagnostic of the form
2818 FILE:LINE: MESSAGE before TOKEN
2819 where TOKEN is the next token in the input stream. MESSAGE
2820 (specified by the caller) is usually of the form "expected
2821 OTHER-TOKEN".
2823 This bypasses the check for tentative passing, and potentially
2824 adds material needed by cp_parser_required_error.
2826 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2827 suggesting insertion of the missing token.
2829 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2830 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2831 location. */
2833 static void
2834 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2835 required_token missing_token_desc,
2836 location_t matching_location)
2838 cp_token *token = cp_lexer_peek_token (parser->lexer);
2839 /* This diagnostic makes more sense if it is tagged to the line
2840 of the token we just peeked at. */
2841 cp_lexer_set_source_position_from_token (token);
2843 if (token->type == CPP_PRAGMA)
2845 error_at (token->location,
2846 "%<#pragma%> is not allowed here");
2847 cp_parser_skip_to_pragma_eol (parser, token);
2848 return;
2851 /* If this is actually a conflict marker, report it as such. */
2852 if (token->type == CPP_LSHIFT
2853 || token->type == CPP_RSHIFT
2854 || token->type == CPP_EQ_EQ)
2856 location_t loc;
2857 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2859 error_at (loc, "version control conflict marker in file");
2860 expanded_location token_exploc = expand_location (token->location);
2861 /* Consume tokens until the end of the source line. */
2862 while (1)
2864 cp_lexer_consume_token (parser->lexer);
2865 cp_token *next = cp_lexer_peek_token (parser->lexer);
2866 if (next == NULL)
2867 break;
2868 expanded_location next_exploc = expand_location (next->location);
2869 if (next_exploc.file != token_exploc.file)
2870 break;
2871 if (next_exploc.line != token_exploc.line)
2872 break;
2874 return;
2878 gcc_rich_location richloc (input_location);
2880 bool added_matching_location = false;
2882 if (missing_token_desc != RT_NONE)
2884 /* Potentially supply a fix-it hint, suggesting to add the
2885 missing token immediately after the *previous* token.
2886 This may move the primary location within richloc. */
2887 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2888 location_t prev_token_loc
2889 = cp_lexer_previous_token (parser->lexer)->location;
2890 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2892 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2893 Attempt to consolidate diagnostics by printing it as a
2894 secondary range within the main diagnostic. */
2895 if (matching_location != UNKNOWN_LOCATION)
2896 added_matching_location
2897 = richloc.add_location_if_nearby (matching_location);
2900 /* Actually emit the error. */
2901 c_parse_error (gmsgid,
2902 /* Because c_parser_error does not understand
2903 CPP_KEYWORD, keywords are treated like
2904 identifiers. */
2905 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2906 token->u.value, token->flags, &richloc);
2908 if (missing_token_desc != RT_NONE)
2910 /* If we weren't able to consolidate matching_location, then
2911 print it as a secondary diagnostic. */
2912 if (matching_location != UNKNOWN_LOCATION
2913 && !added_matching_location)
2914 inform (matching_location, "to match this %qs",
2915 get_matching_symbol (missing_token_desc));
2919 /* If not parsing tentatively, issue a diagnostic of the form
2920 FILE:LINE: MESSAGE before TOKEN
2921 where TOKEN is the next token in the input stream. MESSAGE
2922 (specified by the caller) is usually of the form "expected
2923 OTHER-TOKEN". */
2925 static void
2926 cp_parser_error (cp_parser* parser, const char* gmsgid)
2928 if (!cp_parser_simulate_error (parser))
2929 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2932 /* Issue an error about name-lookup failing. NAME is the
2933 IDENTIFIER_NODE DECL is the result of
2934 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2935 the thing that we hoped to find. */
2937 static void
2938 cp_parser_name_lookup_error (cp_parser* parser,
2939 tree name,
2940 tree decl,
2941 name_lookup_error desired,
2942 location_t location)
2944 /* If name lookup completely failed, tell the user that NAME was not
2945 declared. */
2946 if (decl == error_mark_node)
2948 if (parser->scope && parser->scope != global_namespace)
2949 error_at (location, "%<%E::%E%> has not been declared",
2950 parser->scope, name);
2951 else if (parser->scope == global_namespace)
2952 error_at (location, "%<::%E%> has not been declared", name);
2953 else if (parser->object_scope
2954 && !CLASS_TYPE_P (parser->object_scope))
2955 error_at (location, "request for member %qE in non-class type %qT",
2956 name, parser->object_scope);
2957 else if (parser->object_scope)
2958 error_at (location, "%<%T::%E%> has not been declared",
2959 parser->object_scope, name);
2960 else
2961 error_at (location, "%qE has not been declared", name);
2963 else if (parser->scope && parser->scope != global_namespace)
2965 switch (desired)
2967 case NLE_TYPE:
2968 error_at (location, "%<%E::%E%> is not a type",
2969 parser->scope, name);
2970 break;
2971 case NLE_CXX98:
2972 error_at (location, "%<%E::%E%> is not a class or namespace",
2973 parser->scope, name);
2974 break;
2975 case NLE_NOT_CXX98:
2976 error_at (location,
2977 "%<%E::%E%> is not a class, namespace, or enumeration",
2978 parser->scope, name);
2979 break;
2980 default:
2981 gcc_unreachable ();
2985 else if (parser->scope == global_namespace)
2987 switch (desired)
2989 case NLE_TYPE:
2990 error_at (location, "%<::%E%> is not a type", name);
2991 break;
2992 case NLE_CXX98:
2993 error_at (location, "%<::%E%> is not a class or namespace", name);
2994 break;
2995 case NLE_NOT_CXX98:
2996 error_at (location,
2997 "%<::%E%> is not a class, namespace, or enumeration",
2998 name);
2999 break;
3000 default:
3001 gcc_unreachable ();
3004 else
3006 switch (desired)
3008 case NLE_TYPE:
3009 error_at (location, "%qE is not a type", name);
3010 break;
3011 case NLE_CXX98:
3012 error_at (location, "%qE is not a class or namespace", name);
3013 break;
3014 case NLE_NOT_CXX98:
3015 error_at (location,
3016 "%qE is not a class, namespace, or enumeration", name);
3017 break;
3018 default:
3019 gcc_unreachable ();
3024 /* If we are parsing tentatively, remember that an error has occurred
3025 during this tentative parse. Returns true if the error was
3026 simulated; false if a message should be issued by the caller. */
3028 static bool
3029 cp_parser_simulate_error (cp_parser* parser)
3031 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3033 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3034 return true;
3036 return false;
3039 /* This function is called when a type is defined. If type
3040 definitions are forbidden at this point, an error message is
3041 issued. */
3043 static bool
3044 cp_parser_check_type_definition (cp_parser* parser)
3046 /* If types are forbidden here, issue a message. */
3047 if (parser->type_definition_forbidden_message)
3049 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3050 in the message need to be interpreted. */
3051 error (parser->type_definition_forbidden_message);
3052 return false;
3054 return true;
3057 /* This function is called when the DECLARATOR is processed. The TYPE
3058 was a type defined in the decl-specifiers. If it is invalid to
3059 define a type in the decl-specifiers for DECLARATOR, an error is
3060 issued. TYPE_LOCATION is the location of TYPE and is used
3061 for error reporting. */
3063 static void
3064 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3065 tree type, location_t type_location)
3067 /* [dcl.fct] forbids type definitions in return types.
3068 Unfortunately, it's not easy to know whether or not we are
3069 processing a return type until after the fact. */
3070 while (declarator
3071 && (declarator->kind == cdk_pointer
3072 || declarator->kind == cdk_reference
3073 || declarator->kind == cdk_ptrmem))
3074 declarator = declarator->declarator;
3075 if (declarator
3076 && declarator->kind == cdk_function)
3078 error_at (type_location,
3079 "new types may not be defined in a return type");
3080 inform (type_location,
3081 "(perhaps a semicolon is missing after the definition of %qT)",
3082 type);
3086 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3087 "<" in any valid C++ program. If the next token is indeed "<",
3088 issue a message warning the user about what appears to be an
3089 invalid attempt to form a template-id. LOCATION is the location
3090 of the type-specifier (TYPE) */
3092 static void
3093 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3094 tree type,
3095 enum tag_types tag_type,
3096 location_t location)
3098 cp_token_position start = 0;
3100 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3102 if (TREE_CODE (type) == TYPE_DECL)
3103 type = TREE_TYPE (type);
3104 if (TYPE_P (type) && !template_placeholder_p (type))
3105 error_at (location, "%qT is not a template", type);
3106 else if (identifier_p (type))
3108 if (tag_type != none_type)
3109 error_at (location, "%qE is not a class template", type);
3110 else
3111 error_at (location, "%qE is not a template", type);
3113 else
3114 error_at (location, "invalid template-id");
3115 /* Remember the location of the invalid "<". */
3116 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3117 start = cp_lexer_token_position (parser->lexer, true);
3118 /* Consume the "<". */
3119 cp_lexer_consume_token (parser->lexer);
3120 /* Parse the template arguments. */
3121 cp_parser_enclosed_template_argument_list (parser);
3122 /* Permanently remove the invalid template arguments so that
3123 this error message is not issued again. */
3124 if (start)
3125 cp_lexer_purge_tokens_after (parser->lexer, start);
3129 /* If parsing an integral constant-expression, issue an error message
3130 about the fact that THING appeared and return true. Otherwise,
3131 return false. In either case, set
3132 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3134 static bool
3135 cp_parser_non_integral_constant_expression (cp_parser *parser,
3136 non_integral_constant thing)
3138 parser->non_integral_constant_expression_p = true;
3139 if (parser->integral_constant_expression_p)
3141 if (!parser->allow_non_integral_constant_expression_p)
3143 const char *msg = NULL;
3144 switch (thing)
3146 case NIC_FLOAT:
3147 pedwarn (input_location, OPT_Wpedantic,
3148 "ISO C++ forbids using a floating-point literal "
3149 "in a constant-expression");
3150 return true;
3151 case NIC_CAST:
3152 error ("a cast to a type other than an integral or "
3153 "enumeration type cannot appear in a "
3154 "constant-expression");
3155 return true;
3156 case NIC_TYPEID:
3157 error ("%<typeid%> operator "
3158 "cannot appear in a constant-expression");
3159 return true;
3160 case NIC_NCC:
3161 error ("non-constant compound literals "
3162 "cannot appear in a constant-expression");
3163 return true;
3164 case NIC_FUNC_CALL:
3165 error ("a function call "
3166 "cannot appear in a constant-expression");
3167 return true;
3168 case NIC_INC:
3169 error ("an increment "
3170 "cannot appear in a constant-expression");
3171 return true;
3172 case NIC_DEC:
3173 error ("an decrement "
3174 "cannot appear in a constant-expression");
3175 return true;
3176 case NIC_ARRAY_REF:
3177 error ("an array reference "
3178 "cannot appear in a constant-expression");
3179 return true;
3180 case NIC_ADDR_LABEL:
3181 error ("the address of a label "
3182 "cannot appear in a constant-expression");
3183 return true;
3184 case NIC_OVERLOADED:
3185 error ("calls to overloaded operators "
3186 "cannot appear in a constant-expression");
3187 return true;
3188 case NIC_ASSIGNMENT:
3189 error ("an assignment cannot appear in a constant-expression");
3190 return true;
3191 case NIC_COMMA:
3192 error ("a comma operator "
3193 "cannot appear in a constant-expression");
3194 return true;
3195 case NIC_CONSTRUCTOR:
3196 error ("a call to a constructor "
3197 "cannot appear in a constant-expression");
3198 return true;
3199 case NIC_TRANSACTION:
3200 error ("a transaction expression "
3201 "cannot appear in a constant-expression");
3202 return true;
3203 case NIC_THIS:
3204 msg = "this";
3205 break;
3206 case NIC_FUNC_NAME:
3207 msg = "__FUNCTION__";
3208 break;
3209 case NIC_PRETTY_FUNC:
3210 msg = "__PRETTY_FUNCTION__";
3211 break;
3212 case NIC_C99_FUNC:
3213 msg = "__func__";
3214 break;
3215 case NIC_VA_ARG:
3216 msg = "va_arg";
3217 break;
3218 case NIC_ARROW:
3219 msg = "->";
3220 break;
3221 case NIC_POINT:
3222 msg = ".";
3223 break;
3224 case NIC_STAR:
3225 msg = "*";
3226 break;
3227 case NIC_ADDR:
3228 msg = "&";
3229 break;
3230 case NIC_PREINCREMENT:
3231 msg = "++";
3232 break;
3233 case NIC_PREDECREMENT:
3234 msg = "--";
3235 break;
3236 case NIC_NEW:
3237 msg = "new";
3238 break;
3239 case NIC_DEL:
3240 msg = "delete";
3241 break;
3242 default:
3243 gcc_unreachable ();
3245 if (msg)
3246 error ("%qs cannot appear in a constant-expression", msg);
3247 return true;
3250 return false;
3253 /* Emit a diagnostic for an invalid type name. This function commits
3254 to the current active tentative parse, if any. (Otherwise, the
3255 problematic construct might be encountered again later, resulting
3256 in duplicate error messages.) LOCATION is the location of ID. */
3258 static void
3259 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3260 location_t location)
3262 tree decl, ambiguous_decls;
3263 cp_parser_commit_to_tentative_parse (parser);
3264 /* Try to lookup the identifier. */
3265 decl = cp_parser_lookup_name (parser, id, none_type,
3266 /*is_template=*/false,
3267 /*is_namespace=*/false,
3268 /*check_dependency=*/true,
3269 &ambiguous_decls, location);
3270 if (ambiguous_decls)
3271 /* If the lookup was ambiguous, an error will already have
3272 been issued. */
3273 return;
3274 /* If the lookup found a template-name, it means that the user forgot
3275 to specify an argument list. Emit a useful error message. */
3276 if (DECL_TYPE_TEMPLATE_P (decl))
3278 auto_diagnostic_group d;
3279 error_at (location,
3280 "invalid use of template-name %qE without an argument list",
3281 decl);
3282 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3283 inform (location, "class template argument deduction is only available "
3284 "with -std=c++17 or -std=gnu++17");
3285 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3287 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3288 error_at (location, "invalid use of destructor %qD as a type", id);
3289 else if (TREE_CODE (decl) == TYPE_DECL)
3290 /* Something like 'unsigned A a;' */
3291 error_at (location, "invalid combination of multiple type-specifiers");
3292 else if (!parser->scope)
3294 /* Issue an error message. */
3295 auto_diagnostic_group d;
3296 name_hint hint;
3297 if (TREE_CODE (id) == IDENTIFIER_NODE)
3298 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3299 if (const char *suggestion = hint.suggestion ())
3301 gcc_rich_location richloc (location);
3302 richloc.add_fixit_replace (suggestion);
3303 error_at (&richloc,
3304 "%qE does not name a type; did you mean %qs?",
3305 id, suggestion);
3307 else
3308 error_at (location, "%qE does not name a type", id);
3309 /* If we're in a template class, it's possible that the user was
3310 referring to a type from a base class. For example:
3312 template <typename T> struct A { typedef T X; };
3313 template <typename T> struct B : public A<T> { X x; };
3315 The user should have said "typename A<T>::X". */
3316 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3317 inform (location, "C++11 %<constexpr%> only available with "
3318 "-std=c++11 or -std=gnu++11");
3319 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3320 inform (location, "C++11 %<noexcept%> only available with "
3321 "-std=c++11 or -std=gnu++11");
3322 else if (cxx_dialect < cxx11
3323 && TREE_CODE (id) == IDENTIFIER_NODE
3324 && id_equal (id, "thread_local"))
3325 inform (location, "C++11 %<thread_local%> only available with "
3326 "-std=c++11 or -std=gnu++11");
3327 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3328 inform (location, "%<concept%> only available with -fconcepts");
3329 else if (processing_template_decl && current_class_type
3330 && TYPE_BINFO (current_class_type))
3332 tree b;
3334 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3336 b = TREE_CHAIN (b))
3338 tree base_type = BINFO_TYPE (b);
3339 if (CLASS_TYPE_P (base_type)
3340 && dependent_type_p (base_type))
3342 tree field;
3343 /* Go from a particular instantiation of the
3344 template (which will have an empty TYPE_FIELDs),
3345 to the main version. */
3346 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3347 for (field = TYPE_FIELDS (base_type);
3348 field;
3349 field = DECL_CHAIN (field))
3350 if (TREE_CODE (field) == TYPE_DECL
3351 && DECL_NAME (field) == id)
3353 inform (location,
3354 "(perhaps %<typename %T::%E%> was intended)",
3355 BINFO_TYPE (b), id);
3356 break;
3358 if (field)
3359 break;
3364 /* Here we diagnose qualified-ids where the scope is actually correct,
3365 but the identifier does not resolve to a valid type name. */
3366 else if (parser->scope != error_mark_node)
3368 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3370 auto_diagnostic_group d;
3371 name_hint hint;
3372 if (decl == error_mark_node)
3373 hint = suggest_alternative_in_explicit_scope (location, id,
3374 parser->scope);
3375 const char *suggestion = hint.suggestion ();
3376 gcc_rich_location richloc (location_of (id));
3377 if (suggestion)
3378 richloc.add_fixit_replace (suggestion);
3379 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3381 if (suggestion)
3382 error_at (&richloc,
3383 "%qE in namespace %qE does not name a template"
3384 " type; did you mean %qs?",
3385 id, parser->scope, suggestion);
3386 else
3387 error_at (&richloc,
3388 "%qE in namespace %qE does not name a template type",
3389 id, parser->scope);
3391 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3393 if (suggestion)
3394 error_at (&richloc,
3395 "%qE in namespace %qE does not name a template"
3396 " type; did you mean %qs?",
3397 TREE_OPERAND (id, 0), parser->scope, suggestion);
3398 else
3399 error_at (&richloc,
3400 "%qE in namespace %qE does not name a template"
3401 " type",
3402 TREE_OPERAND (id, 0), parser->scope);
3404 else
3406 if (suggestion)
3407 error_at (&richloc,
3408 "%qE in namespace %qE does not name a type"
3409 "; did you mean %qs?",
3410 id, parser->scope, suggestion);
3411 else
3412 error_at (&richloc,
3413 "%qE in namespace %qE does not name a type",
3414 id, parser->scope);
3416 if (DECL_P (decl))
3417 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3419 else if (CLASS_TYPE_P (parser->scope)
3420 && constructor_name_p (id, parser->scope))
3422 /* A<T>::A<T>() */
3423 auto_diagnostic_group d;
3424 error_at (location, "%<%T::%E%> names the constructor, not"
3425 " the type", parser->scope, id);
3426 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3427 error_at (location, "and %qT has no template constructors",
3428 parser->scope);
3430 else if (TYPE_P (parser->scope)
3431 && dependent_scope_p (parser->scope))
3433 gcc_rich_location richloc (location);
3434 richloc.add_fixit_insert_before ("typename ");
3435 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3436 error_at (&richloc,
3437 "need %<typename%> before %<%T::%D::%E%> because "
3438 "%<%T::%D%> is a dependent scope",
3439 TYPE_CONTEXT (parser->scope),
3440 TYPENAME_TYPE_FULLNAME (parser->scope),
3442 TYPE_CONTEXT (parser->scope),
3443 TYPENAME_TYPE_FULLNAME (parser->scope));
3444 else
3445 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3446 "%qT is a dependent scope",
3447 parser->scope, id, parser->scope);
3449 else if (TYPE_P (parser->scope))
3451 auto_diagnostic_group d;
3452 if (!COMPLETE_TYPE_P (parser->scope))
3453 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3454 parser->scope);
3455 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3456 error_at (location_of (id),
3457 "%qE in %q#T does not name a template type",
3458 id, parser->scope);
3459 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3460 error_at (location_of (id),
3461 "%qE in %q#T does not name a template type",
3462 TREE_OPERAND (id, 0), parser->scope);
3463 else
3464 error_at (location_of (id),
3465 "%qE in %q#T does not name a type",
3466 id, parser->scope);
3467 if (DECL_P (decl))
3468 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3470 else
3471 gcc_unreachable ();
3475 /* Check for a common situation where a type-name should be present,
3476 but is not, and issue a sensible error message. Returns true if an
3477 invalid type-name was detected.
3479 The situation handled by this function are variable declarations of the
3480 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3481 Usually, `ID' should name a type, but if we got here it means that it
3482 does not. We try to emit the best possible error message depending on
3483 how exactly the id-expression looks like. */
3485 static bool
3486 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3488 tree id;
3489 cp_token *token = cp_lexer_peek_token (parser->lexer);
3491 /* Avoid duplicate error about ambiguous lookup. */
3492 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3494 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3495 if (next->type == CPP_NAME && next->error_reported)
3496 goto out;
3499 cp_parser_parse_tentatively (parser);
3500 id = cp_parser_id_expression (parser,
3501 /*template_keyword_p=*/false,
3502 /*check_dependency_p=*/true,
3503 /*template_p=*/NULL,
3504 /*declarator_p=*/false,
3505 /*optional_p=*/false);
3506 /* If the next token is a (, this is a function with no explicit return
3507 type, i.e. constructor, destructor or conversion op. */
3508 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3509 || TREE_CODE (id) == TYPE_DECL)
3511 cp_parser_abort_tentative_parse (parser);
3512 return false;
3514 if (!cp_parser_parse_definitely (parser))
3515 return false;
3517 /* Emit a diagnostic for the invalid type. */
3518 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3519 out:
3520 /* If we aren't in the middle of a declarator (i.e. in a
3521 parameter-declaration-clause), skip to the end of the declaration;
3522 there's no point in trying to process it. */
3523 if (!parser->in_declarator_p)
3524 cp_parser_skip_to_end_of_block_or_statement (parser);
3525 return true;
3528 /* Consume tokens up to, and including, the next non-nested closing `)'.
3529 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3530 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3531 found an unnested token of that type. */
3533 static int
3534 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3535 bool recovering,
3536 cpp_ttype or_ttype,
3537 bool consume_paren)
3539 unsigned paren_depth = 0;
3540 unsigned brace_depth = 0;
3541 unsigned square_depth = 0;
3542 unsigned condop_depth = 0;
3544 if (recovering && or_ttype == CPP_EOF
3545 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3546 return 0;
3548 while (true)
3550 cp_token * token = cp_lexer_peek_token (parser->lexer);
3552 /* Have we found what we're looking for before the closing paren? */
3553 if (token->type == or_ttype && or_ttype != CPP_EOF
3554 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3555 return -1;
3557 switch (token->type)
3559 case CPP_EOF:
3560 case CPP_PRAGMA_EOL:
3561 /* If we've run out of tokens, then there is no closing `)'. */
3562 return 0;
3564 /* This is good for lambda expression capture-lists. */
3565 case CPP_OPEN_SQUARE:
3566 ++square_depth;
3567 break;
3568 case CPP_CLOSE_SQUARE:
3569 if (!square_depth--)
3570 return 0;
3571 break;
3573 case CPP_SEMICOLON:
3574 /* This matches the processing in skip_to_end_of_statement. */
3575 if (!brace_depth)
3576 return 0;
3577 break;
3579 case CPP_OPEN_BRACE:
3580 ++brace_depth;
3581 break;
3582 case CPP_CLOSE_BRACE:
3583 if (!brace_depth--)
3584 return 0;
3585 break;
3587 case CPP_OPEN_PAREN:
3588 if (!brace_depth)
3589 ++paren_depth;
3590 break;
3592 case CPP_CLOSE_PAREN:
3593 if (!brace_depth && !paren_depth--)
3595 if (consume_paren)
3596 cp_lexer_consume_token (parser->lexer);
3597 return 1;
3599 break;
3601 case CPP_QUERY:
3602 if (!brace_depth && !paren_depth && !square_depth)
3603 ++condop_depth;
3604 break;
3606 case CPP_COLON:
3607 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3608 condop_depth--;
3609 break;
3611 default:
3612 break;
3615 /* Consume the token. */
3616 cp_lexer_consume_token (parser->lexer);
3620 /* Consume tokens up to, and including, the next non-nested closing `)'.
3621 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3622 are doing error recovery. Returns -1 if OR_COMMA is true and we
3623 found an unnested token of that type. */
3625 static int
3626 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3627 bool recovering,
3628 bool or_comma,
3629 bool consume_paren)
3631 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3632 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3633 ttype, consume_paren);
3636 /* Consume tokens until we reach the end of the current statement.
3637 Normally, that will be just before consuming a `;'. However, if a
3638 non-nested `}' comes first, then we stop before consuming that. */
3640 static void
3641 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3643 unsigned nesting_depth = 0;
3645 /* Unwind generic function template scope if necessary. */
3646 if (parser->fully_implicit_function_template_p)
3647 abort_fully_implicit_template (parser);
3649 while (true)
3651 cp_token *token = cp_lexer_peek_token (parser->lexer);
3653 switch (token->type)
3655 case CPP_EOF:
3656 case CPP_PRAGMA_EOL:
3657 /* If we've run out of tokens, stop. */
3658 return;
3660 case CPP_SEMICOLON:
3661 /* If the next token is a `;', we have reached the end of the
3662 statement. */
3663 if (!nesting_depth)
3664 return;
3665 break;
3667 case CPP_CLOSE_BRACE:
3668 /* If this is a non-nested '}', stop before consuming it.
3669 That way, when confronted with something like:
3671 { 3 + }
3673 we stop before consuming the closing '}', even though we
3674 have not yet reached a `;'. */
3675 if (nesting_depth == 0)
3676 return;
3678 /* If it is the closing '}' for a block that we have
3679 scanned, stop -- but only after consuming the token.
3680 That way given:
3682 void f g () { ... }
3683 typedef int I;
3685 we will stop after the body of the erroneously declared
3686 function, but before consuming the following `typedef'
3687 declaration. */
3688 if (--nesting_depth == 0)
3690 cp_lexer_consume_token (parser->lexer);
3691 return;
3693 break;
3695 case CPP_OPEN_BRACE:
3696 ++nesting_depth;
3697 break;
3699 default:
3700 break;
3703 /* Consume the token. */
3704 cp_lexer_consume_token (parser->lexer);
3708 /* This function is called at the end of a statement or declaration.
3709 If the next token is a semicolon, it is consumed; otherwise, error
3710 recovery is attempted. */
3712 static void
3713 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3715 /* Look for the trailing `;'. */
3716 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3718 /* If there is additional (erroneous) input, skip to the end of
3719 the statement. */
3720 cp_parser_skip_to_end_of_statement (parser);
3721 /* If the next token is now a `;', consume it. */
3722 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3723 cp_lexer_consume_token (parser->lexer);
3727 /* Skip tokens until we have consumed an entire block, or until we
3728 have consumed a non-nested `;'. */
3730 static void
3731 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3733 int nesting_depth = 0;
3735 /* Unwind generic function template scope if necessary. */
3736 if (parser->fully_implicit_function_template_p)
3737 abort_fully_implicit_template (parser);
3739 while (nesting_depth >= 0)
3741 cp_token *token = cp_lexer_peek_token (parser->lexer);
3743 switch (token->type)
3745 case CPP_EOF:
3746 case CPP_PRAGMA_EOL:
3747 /* If we've run out of tokens, stop. */
3748 return;
3750 case CPP_SEMICOLON:
3751 /* Stop if this is an unnested ';'. */
3752 if (!nesting_depth)
3753 nesting_depth = -1;
3754 break;
3756 case CPP_CLOSE_BRACE:
3757 /* Stop if this is an unnested '}', or closes the outermost
3758 nesting level. */
3759 nesting_depth--;
3760 if (nesting_depth < 0)
3761 return;
3762 if (!nesting_depth)
3763 nesting_depth = -1;
3764 break;
3766 case CPP_OPEN_BRACE:
3767 /* Nest. */
3768 nesting_depth++;
3769 break;
3771 default:
3772 break;
3775 /* Consume the token. */
3776 cp_lexer_consume_token (parser->lexer);
3780 /* Skip tokens until a non-nested closing curly brace is the next
3781 token, or there are no more tokens. Return true in the first case,
3782 false otherwise. */
3784 static bool
3785 cp_parser_skip_to_closing_brace (cp_parser *parser)
3787 unsigned nesting_depth = 0;
3789 while (true)
3791 cp_token *token = cp_lexer_peek_token (parser->lexer);
3793 switch (token->type)
3795 case CPP_EOF:
3796 case CPP_PRAGMA_EOL:
3797 /* If we've run out of tokens, stop. */
3798 return false;
3800 case CPP_CLOSE_BRACE:
3801 /* If the next token is a non-nested `}', then we have reached
3802 the end of the current block. */
3803 if (nesting_depth-- == 0)
3804 return true;
3805 break;
3807 case CPP_OPEN_BRACE:
3808 /* If it the next token is a `{', then we are entering a new
3809 block. Consume the entire block. */
3810 ++nesting_depth;
3811 break;
3813 default:
3814 break;
3817 /* Consume the token. */
3818 cp_lexer_consume_token (parser->lexer);
3822 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3823 parameter is the PRAGMA token, allowing us to purge the entire pragma
3824 sequence. */
3826 static void
3827 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3829 cp_token *token;
3831 parser->lexer->in_pragma = false;
3834 token = cp_lexer_consume_token (parser->lexer);
3835 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3837 /* Ensure that the pragma is not parsed again. */
3838 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3841 /* Require pragma end of line, resyncing with it as necessary. The
3842 arguments are as for cp_parser_skip_to_pragma_eol. */
3844 static void
3845 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3847 parser->lexer->in_pragma = false;
3848 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3849 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3852 /* This is a simple wrapper around make_typename_type. When the id is
3853 an unresolved identifier node, we can provide a superior diagnostic
3854 using cp_parser_diagnose_invalid_type_name. */
3856 static tree
3857 cp_parser_make_typename_type (cp_parser *parser, tree id,
3858 location_t id_location)
3860 tree result;
3861 if (identifier_p (id))
3863 result = make_typename_type (parser->scope, id, typename_type,
3864 /*complain=*/tf_none);
3865 if (result == error_mark_node)
3866 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3867 return result;
3869 return make_typename_type (parser->scope, id, typename_type, tf_error);
3872 /* This is a wrapper around the
3873 make_{pointer,ptrmem,reference}_declarator functions that decides
3874 which one to call based on the CODE and CLASS_TYPE arguments. The
3875 CODE argument should be one of the values returned by
3876 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3877 appertain to the pointer or reference. */
3879 static cp_declarator *
3880 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3881 cp_cv_quals cv_qualifiers,
3882 cp_declarator *target,
3883 tree attributes)
3885 if (code == ERROR_MARK || target == cp_error_declarator)
3886 return cp_error_declarator;
3888 if (code == INDIRECT_REF)
3889 if (class_type == NULL_TREE)
3890 return make_pointer_declarator (cv_qualifiers, target, attributes);
3891 else
3892 return make_ptrmem_declarator (cv_qualifiers, class_type,
3893 target, attributes);
3894 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3895 return make_reference_declarator (cv_qualifiers, target,
3896 false, attributes);
3897 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3898 return make_reference_declarator (cv_qualifiers, target,
3899 true, attributes);
3900 gcc_unreachable ();
3903 /* Create a new C++ parser. */
3905 static cp_parser *
3906 cp_parser_new (void)
3908 cp_parser *parser;
3909 cp_lexer *lexer;
3910 unsigned i;
3912 /* cp_lexer_new_main is called before doing GC allocation because
3913 cp_lexer_new_main might load a PCH file. */
3914 lexer = cp_lexer_new_main ();
3916 /* Initialize the binops_by_token so that we can get the tree
3917 directly from the token. */
3918 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3919 binops_by_token[binops[i].token_type] = binops[i];
3921 parser = ggc_cleared_alloc<cp_parser> ();
3922 parser->lexer = lexer;
3923 parser->context = cp_parser_context_new (NULL);
3925 /* For now, we always accept GNU extensions. */
3926 parser->allow_gnu_extensions_p = 1;
3928 /* The `>' token is a greater-than operator, not the end of a
3929 template-id. */
3930 parser->greater_than_is_operator_p = true;
3932 parser->default_arg_ok_p = true;
3934 /* We are not parsing a constant-expression. */
3935 parser->integral_constant_expression_p = false;
3936 parser->allow_non_integral_constant_expression_p = false;
3937 parser->non_integral_constant_expression_p = false;
3939 /* Local variable names are not forbidden. */
3940 parser->local_variables_forbidden_p = false;
3942 /* We are not processing an `extern "C"' declaration. */
3943 parser->in_unbraced_linkage_specification_p = false;
3945 /* We are not processing a declarator. */
3946 parser->in_declarator_p = false;
3948 /* We are not processing a template-argument-list. */
3949 parser->in_template_argument_list_p = false;
3951 /* We are not in an iteration statement. */
3952 parser->in_statement = 0;
3954 /* We are not in a switch statement. */
3955 parser->in_switch_statement_p = false;
3957 /* We are not parsing a type-id inside an expression. */
3958 parser->in_type_id_in_expr_p = false;
3960 /* String literals should be translated to the execution character set. */
3961 parser->translate_strings_p = true;
3963 /* We are not parsing a function body. */
3964 parser->in_function_body = false;
3966 /* We can correct until told otherwise. */
3967 parser->colon_corrects_to_scope_p = true;
3969 /* The unparsed function queue is empty. */
3970 push_unparsed_function_queues (parser);
3972 /* There are no classes being defined. */
3973 parser->num_classes_being_defined = 0;
3975 /* No template parameters apply. */
3976 parser->num_template_parameter_lists = 0;
3978 /* Special parsing data structures. */
3979 parser->omp_declare_simd = NULL;
3980 parser->oacc_routine = NULL;
3982 /* Not declaring an implicit function template. */
3983 parser->auto_is_implicit_function_template_parm_p = false;
3984 parser->fully_implicit_function_template_p = false;
3985 parser->implicit_template_parms = 0;
3986 parser->implicit_template_scope = 0;
3988 /* Allow constrained-type-specifiers. */
3989 parser->prevent_constrained_type_specifiers = 0;
3991 /* We haven't yet seen an 'extern "C"'. */
3992 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3994 return parser;
3997 /* Create a cp_lexer structure which will emit the tokens in CACHE
3998 and push it onto the parser's lexer stack. This is used for delayed
3999 parsing of in-class method bodies and default arguments, and should
4000 not be confused with tentative parsing. */
4001 static void
4002 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4004 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4005 lexer->next = parser->lexer;
4006 parser->lexer = lexer;
4008 /* Move the current source position to that of the first token in the
4009 new lexer. */
4010 cp_lexer_set_source_position_from_token (lexer->next_token);
4013 /* Pop the top lexer off the parser stack. This is never used for the
4014 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4015 static void
4016 cp_parser_pop_lexer (cp_parser *parser)
4018 cp_lexer *lexer = parser->lexer;
4019 parser->lexer = lexer->next;
4020 cp_lexer_destroy (lexer);
4022 /* Put the current source position back where it was before this
4023 lexer was pushed. */
4024 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4027 /* Lexical conventions [gram.lex] */
4029 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4030 identifier. */
4032 static cp_expr
4033 cp_parser_identifier (cp_parser* parser)
4035 cp_token *token;
4037 /* Look for the identifier. */
4038 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4039 /* Return the value. */
4040 if (token)
4041 return cp_expr (token->u.value, token->location);
4042 else
4043 return error_mark_node;
4046 /* Parse a sequence of adjacent string constants. Returns a
4047 TREE_STRING representing the combined, nul-terminated string
4048 constant. If TRANSLATE is true, translate the string to the
4049 execution character set. If WIDE_OK is true, a wide string is
4050 invalid here.
4052 C++98 [lex.string] says that if a narrow string literal token is
4053 adjacent to a wide string literal token, the behavior is undefined.
4054 However, C99 6.4.5p4 says that this results in a wide string literal.
4055 We follow C99 here, for consistency with the C front end.
4057 This code is largely lifted from lex_string() in c-lex.c.
4059 FUTURE: ObjC++ will need to handle @-strings here. */
4060 static cp_expr
4061 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4062 bool lookup_udlit = true)
4064 tree value;
4065 size_t count;
4066 struct obstack str_ob;
4067 struct obstack loc_ob;
4068 cpp_string str, istr, *strs;
4069 cp_token *tok;
4070 enum cpp_ttype type, curr_type;
4071 int have_suffix_p = 0;
4072 tree string_tree;
4073 tree suffix_id = NULL_TREE;
4074 bool curr_tok_is_userdef_p = false;
4076 tok = cp_lexer_peek_token (parser->lexer);
4077 if (!cp_parser_is_string_literal (tok))
4079 cp_parser_error (parser, "expected string-literal");
4080 return error_mark_node;
4083 location_t loc = tok->location;
4085 if (cpp_userdef_string_p (tok->type))
4087 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4088 curr_type = cpp_userdef_string_remove_type (tok->type);
4089 curr_tok_is_userdef_p = true;
4091 else
4093 string_tree = tok->u.value;
4094 curr_type = tok->type;
4096 type = curr_type;
4098 /* Try to avoid the overhead of creating and destroying an obstack
4099 for the common case of just one string. */
4100 if (!cp_parser_is_string_literal
4101 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4103 cp_lexer_consume_token (parser->lexer);
4105 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4106 str.len = TREE_STRING_LENGTH (string_tree);
4107 count = 1;
4109 if (curr_tok_is_userdef_p)
4111 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4112 have_suffix_p = 1;
4113 curr_type = cpp_userdef_string_remove_type (tok->type);
4115 else
4116 curr_type = tok->type;
4118 strs = &str;
4120 else
4122 location_t last_tok_loc = tok->location;
4123 gcc_obstack_init (&str_ob);
4124 gcc_obstack_init (&loc_ob);
4125 count = 0;
4129 cp_lexer_consume_token (parser->lexer);
4130 count++;
4131 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4132 str.len = TREE_STRING_LENGTH (string_tree);
4134 if (curr_tok_is_userdef_p)
4136 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4137 if (have_suffix_p == 0)
4139 suffix_id = curr_suffix_id;
4140 have_suffix_p = 1;
4142 else if (have_suffix_p == 1
4143 && curr_suffix_id != suffix_id)
4145 error ("inconsistent user-defined literal suffixes"
4146 " %qD and %qD in string literal",
4147 suffix_id, curr_suffix_id);
4148 have_suffix_p = -1;
4150 curr_type = cpp_userdef_string_remove_type (tok->type);
4152 else
4153 curr_type = tok->type;
4155 if (type != curr_type)
4157 if (type == CPP_STRING)
4158 type = curr_type;
4159 else if (curr_type != CPP_STRING)
4161 rich_location rich_loc (line_table, tok->location);
4162 rich_loc.add_range (last_tok_loc);
4163 error_at (&rich_loc,
4164 "unsupported non-standard concatenation "
4165 "of string literals");
4169 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4170 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4172 last_tok_loc = tok->location;
4174 tok = cp_lexer_peek_token (parser->lexer);
4175 if (cpp_userdef_string_p (tok->type))
4177 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4178 curr_type = cpp_userdef_string_remove_type (tok->type);
4179 curr_tok_is_userdef_p = true;
4181 else
4183 string_tree = tok->u.value;
4184 curr_type = tok->type;
4185 curr_tok_is_userdef_p = false;
4188 while (cp_parser_is_string_literal (tok));
4190 /* A string literal built by concatenation has its caret=start at
4191 the start of the initial string, and its finish at the finish of
4192 the final string literal. */
4193 loc = make_location (loc, loc, get_finish (last_tok_loc));
4195 strs = (cpp_string *) obstack_finish (&str_ob);
4198 if (type != CPP_STRING && !wide_ok)
4200 cp_parser_error (parser, "a wide string is invalid in this context");
4201 type = CPP_STRING;
4204 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4205 (parse_in, strs, count, &istr, type))
4207 value = build_string (istr.len, (const char *)istr.text);
4208 free (CONST_CAST (unsigned char *, istr.text));
4209 if (count > 1)
4211 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4212 gcc_assert (g_string_concat_db);
4213 g_string_concat_db->record_string_concatenation (count, locs);
4216 switch (type)
4218 default:
4219 case CPP_STRING:
4220 case CPP_UTF8STRING:
4221 TREE_TYPE (value) = char_array_type_node;
4222 break;
4223 case CPP_STRING16:
4224 TREE_TYPE (value) = char16_array_type_node;
4225 break;
4226 case CPP_STRING32:
4227 TREE_TYPE (value) = char32_array_type_node;
4228 break;
4229 case CPP_WSTRING:
4230 TREE_TYPE (value) = wchar_array_type_node;
4231 break;
4234 value = fix_string_type (value);
4236 if (have_suffix_p)
4238 tree literal = build_userdef_literal (suffix_id, value,
4239 OT_NONE, NULL_TREE);
4240 if (lookup_udlit)
4241 value = cp_parser_userdef_string_literal (literal);
4242 else
4243 value = literal;
4246 else
4247 /* cpp_interpret_string has issued an error. */
4248 value = error_mark_node;
4250 if (count > 1)
4252 obstack_free (&str_ob, 0);
4253 obstack_free (&loc_ob, 0);
4256 return cp_expr (value, loc);
4259 /* Look up a literal operator with the name and the exact arguments. */
4261 static tree
4262 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4264 tree decl = lookup_name (name);
4265 if (!decl || !is_overloaded_fn (decl))
4266 return error_mark_node;
4268 for (lkp_iterator iter (decl); iter; ++iter)
4270 tree fn = *iter;
4272 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4274 unsigned int ix;
4275 bool found = true;
4277 for (ix = 0;
4278 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4279 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4281 tree tparm = TREE_VALUE (parmtypes);
4282 tree targ = TREE_TYPE ((*args)[ix]);
4283 bool ptr = TYPE_PTR_P (tparm);
4284 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4285 if ((ptr || arr || !same_type_p (tparm, targ))
4286 && (!ptr || !arr
4287 || !same_type_p (TREE_TYPE (tparm),
4288 TREE_TYPE (targ))))
4289 found = false;
4292 if (found
4293 && ix == vec_safe_length (args)
4294 /* May be this should be sufficient_parms_p instead,
4295 depending on how exactly should user-defined literals
4296 work in presence of default arguments on the literal
4297 operator parameters. */
4298 && parmtypes == void_list_node)
4299 return decl;
4303 return error_mark_node;
4306 /* Parse a user-defined char constant. Returns a call to a user-defined
4307 literal operator taking the character as an argument. */
4309 static cp_expr
4310 cp_parser_userdef_char_literal (cp_parser *parser)
4312 cp_token *token = cp_lexer_consume_token (parser->lexer);
4313 tree literal = token->u.value;
4314 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4315 tree value = USERDEF_LITERAL_VALUE (literal);
4316 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4317 tree decl, result;
4319 /* Build up a call to the user-defined operator */
4320 /* Lookup the name we got back from the id-expression. */
4321 vec<tree, va_gc> *args = make_tree_vector ();
4322 vec_safe_push (args, value);
4323 decl = lookup_literal_operator (name, args);
4324 if (!decl || decl == error_mark_node)
4326 error ("unable to find character literal operator %qD with %qT argument",
4327 name, TREE_TYPE (value));
4328 release_tree_vector (args);
4329 return error_mark_node;
4331 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4332 release_tree_vector (args);
4333 return result;
4336 /* A subroutine of cp_parser_userdef_numeric_literal to
4337 create a char... template parameter pack from a string node. */
4339 static tree
4340 make_char_string_pack (tree value)
4342 tree charvec;
4343 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4344 const char *str = TREE_STRING_POINTER (value);
4345 int i, len = TREE_STRING_LENGTH (value) - 1;
4346 tree argvec = make_tree_vec (1);
4348 /* Fill in CHARVEC with all of the parameters. */
4349 charvec = make_tree_vec (len);
4350 for (i = 0; i < len; ++i)
4352 unsigned char s[3] = { '\'', str[i], '\'' };
4353 cpp_string in = { 3, s };
4354 cpp_string out = { 0, 0 };
4355 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4356 return NULL_TREE;
4357 gcc_assert (out.len == 2);
4358 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4359 out.text[0]);
4362 /* Build the argument packs. */
4363 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4365 TREE_VEC_ELT (argvec, 0) = argpack;
4367 return argvec;
4370 /* A subroutine of cp_parser_userdef_numeric_literal to
4371 create a char... template parameter pack from a string node. */
4373 static tree
4374 make_string_pack (tree value)
4376 tree charvec;
4377 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4378 const unsigned char *str
4379 = (const unsigned char *) TREE_STRING_POINTER (value);
4380 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4381 int len = TREE_STRING_LENGTH (value) / sz - 1;
4382 tree argvec = make_tree_vec (2);
4384 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4385 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4387 /* First template parm is character type. */
4388 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4390 /* Fill in CHARVEC with all of the parameters. */
4391 charvec = make_tree_vec (len);
4392 for (int i = 0; i < len; ++i)
4393 TREE_VEC_ELT (charvec, i)
4394 = double_int_to_tree (str_char_type_node,
4395 double_int::from_buffer (str + i * sz, sz));
4397 /* Build the argument packs. */
4398 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4400 TREE_VEC_ELT (argvec, 1) = argpack;
4402 return argvec;
4405 /* Parse a user-defined numeric constant. returns a call to a user-defined
4406 literal operator. */
4408 static cp_expr
4409 cp_parser_userdef_numeric_literal (cp_parser *parser)
4411 cp_token *token = cp_lexer_consume_token (parser->lexer);
4412 tree literal = token->u.value;
4413 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4414 tree value = USERDEF_LITERAL_VALUE (literal);
4415 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4416 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4417 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4418 tree decl, result;
4419 vec<tree, va_gc> *args;
4421 /* Look for a literal operator taking the exact type of numeric argument
4422 as the literal value. */
4423 args = make_tree_vector ();
4424 vec_safe_push (args, value);
4425 decl = lookup_literal_operator (name, args);
4426 if (decl && decl != error_mark_node)
4428 result = finish_call_expr (decl, &args, false, true,
4429 tf_warning_or_error);
4431 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4433 warning_at (token->location, OPT_Woverflow,
4434 "integer literal exceeds range of %qT type",
4435 long_long_unsigned_type_node);
4437 else
4439 if (overflow > 0)
4440 warning_at (token->location, OPT_Woverflow,
4441 "floating literal exceeds range of %qT type",
4442 long_double_type_node);
4443 else if (overflow < 0)
4444 warning_at (token->location, OPT_Woverflow,
4445 "floating literal truncated to zero");
4448 release_tree_vector (args);
4449 return result;
4451 release_tree_vector (args);
4453 /* If the numeric argument didn't work, look for a raw literal
4454 operator taking a const char* argument consisting of the number
4455 in string format. */
4456 args = make_tree_vector ();
4457 vec_safe_push (args, num_string);
4458 decl = lookup_literal_operator (name, args);
4459 if (decl && decl != error_mark_node)
4461 result = finish_call_expr (decl, &args, false, true,
4462 tf_warning_or_error);
4463 release_tree_vector (args);
4464 return result;
4466 release_tree_vector (args);
4468 /* If the raw literal didn't work, look for a non-type template
4469 function with parameter pack char.... Call the function with
4470 template parameter characters representing the number. */
4471 args = make_tree_vector ();
4472 decl = lookup_literal_operator (name, args);
4473 if (decl && decl != error_mark_node)
4475 tree tmpl_args = make_char_string_pack (num_string);
4476 if (tmpl_args == NULL_TREE)
4478 error ("failed to translate literal to execution character set %qT",
4479 num_string);
4480 return error_mark_node;
4482 decl = lookup_template_function (decl, tmpl_args);
4483 result = finish_call_expr (decl, &args, false, true,
4484 tf_warning_or_error);
4485 release_tree_vector (args);
4486 return result;
4489 release_tree_vector (args);
4491 /* In C++14 the standard library defines complex number suffixes that
4492 conflict with GNU extensions. Prefer them if <complex> is #included. */
4493 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4494 bool i14 = (cxx_dialect > cxx11
4495 && (id_equal (suffix_id, "i")
4496 || id_equal (suffix_id, "if")
4497 || id_equal (suffix_id, "il")));
4498 diagnostic_t kind = DK_ERROR;
4499 int opt = 0;
4501 if (i14 && ext)
4503 tree cxlit = lookup_qualified_name (std_node,
4504 get_identifier ("complex_literals"),
4505 0, false, false);
4506 if (cxlit == error_mark_node)
4508 /* No <complex>, so pedwarn and use GNU semantics. */
4509 kind = DK_PEDWARN;
4510 opt = OPT_Wpedantic;
4514 bool complained
4515 = emit_diagnostic (kind, input_location, opt,
4516 "unable to find numeric literal operator %qD", name);
4518 if (!complained)
4519 /* Don't inform either. */;
4520 else if (i14)
4522 inform (token->location, "add %<using namespace std::complex_literals%> "
4523 "(from <complex>) to enable the C++14 user-defined literal "
4524 "suffixes");
4525 if (ext)
4526 inform (token->location, "or use %<j%> instead of %<i%> for the "
4527 "GNU built-in suffix");
4529 else if (!ext)
4530 inform (token->location, "use -fext-numeric-literals "
4531 "to enable more built-in suffixes");
4533 if (kind == DK_ERROR)
4534 value = error_mark_node;
4535 else
4537 /* Use the built-in semantics. */
4538 tree type;
4539 if (id_equal (suffix_id, "i"))
4541 if (TREE_CODE (value) == INTEGER_CST)
4542 type = integer_type_node;
4543 else
4544 type = double_type_node;
4546 else if (id_equal (suffix_id, "if"))
4547 type = float_type_node;
4548 else /* if (id_equal (suffix_id, "il")) */
4549 type = long_double_type_node;
4551 value = build_complex (build_complex_type (type),
4552 fold_convert (type, integer_zero_node),
4553 fold_convert (type, value));
4556 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4557 /* Avoid repeated diagnostics. */
4558 token->u.value = value;
4559 return value;
4562 /* Parse a user-defined string constant. Returns a call to a user-defined
4563 literal operator taking a character pointer and the length of the string
4564 as arguments. */
4566 static tree
4567 cp_parser_userdef_string_literal (tree literal)
4569 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4570 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4571 tree value = USERDEF_LITERAL_VALUE (literal);
4572 int len = TREE_STRING_LENGTH (value)
4573 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4574 tree decl;
4576 /* Build up a call to the user-defined operator. */
4577 /* Lookup the name we got back from the id-expression. */
4578 releasing_vec rargs;
4579 vec<tree, va_gc> *&args = rargs.get_ref();
4580 vec_safe_push (args, value);
4581 vec_safe_push (args, build_int_cst (size_type_node, len));
4582 decl = lookup_literal_operator (name, args);
4584 if (decl && decl != error_mark_node)
4585 return finish_call_expr (decl, &args, false, true,
4586 tf_warning_or_error);
4588 /* Look for a suitable template function, either (C++20) with a single
4589 parameter of class type, or (N3599) with typename parameter CharT and
4590 parameter pack CharT... */
4591 args->truncate (0);
4592 decl = lookup_literal_operator (name, args);
4593 if (decl && decl != error_mark_node)
4595 /* Use resolve_nondeduced_context to try to choose one form of template
4596 or the other. */
4597 tree tmpl_args = make_tree_vec (1);
4598 TREE_VEC_ELT (tmpl_args, 0) = value;
4599 decl = lookup_template_function (decl, tmpl_args);
4600 tree res = resolve_nondeduced_context (decl, tf_none);
4601 if (DECL_P (res))
4602 decl = res;
4603 else
4605 TREE_OPERAND (decl, 1) = make_string_pack (value);
4606 res = resolve_nondeduced_context (decl, tf_none);
4607 if (DECL_P (res))
4608 decl = res;
4610 if (!DECL_P (decl) && cxx_dialect > cxx17)
4611 TREE_OPERAND (decl, 1) = tmpl_args;
4612 return finish_call_expr (decl, &args, false, true,
4613 tf_warning_or_error);
4616 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4617 name, TREE_TYPE (value), size_type_node);
4618 return error_mark_node;
4622 /* Basic concepts [gram.basic] */
4624 /* Parse a translation-unit.
4626 translation-unit:
4627 declaration-seq [opt] */
4629 static void
4630 cp_parser_translation_unit (cp_parser* parser)
4632 gcc_checking_assert (!cp_error_declarator);
4634 /* Create the declarator obstack. */
4635 gcc_obstack_init (&declarator_obstack);
4636 /* Create the error declarator. */
4637 cp_error_declarator = make_declarator (cdk_error);
4638 /* Create the empty parameter list. */
4639 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4640 UNKNOWN_LOCATION);
4641 /* Remember where the base of the declarator obstack lies. */
4642 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4644 bool implicit_extern_c = false;
4646 for (;;)
4648 cp_token *token = cp_lexer_peek_token (parser->lexer);
4650 /* If we're entering or exiting a region that's implicitly
4651 extern "C", modify the lang context appropriately. */
4652 if (implicit_extern_c
4653 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4655 implicit_extern_c = !implicit_extern_c;
4656 if (implicit_extern_c)
4657 push_lang_context (lang_name_c);
4658 else
4659 pop_lang_context ();
4662 if (token->type == CPP_EOF)
4663 break;
4665 if (token->type == CPP_CLOSE_BRACE)
4667 cp_parser_error (parser, "expected declaration");
4668 cp_lexer_consume_token (parser->lexer);
4669 /* If the next token is now a `;', consume it. */
4670 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4671 cp_lexer_consume_token (parser->lexer);
4673 else
4674 cp_parser_toplevel_declaration (parser);
4677 /* Get rid of the token array; we don't need it any more. */
4678 cp_lexer_destroy (parser->lexer);
4679 parser->lexer = NULL;
4681 /* The EOF should have reset this. */
4682 gcc_checking_assert (!implicit_extern_c);
4684 /* Make sure the declarator obstack was fully cleaned up. */
4685 gcc_assert (obstack_next_free (&declarator_obstack)
4686 == declarator_obstack_base);
4689 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4690 decltype context. */
4692 static inline tsubst_flags_t
4693 complain_flags (bool decltype_p)
4695 tsubst_flags_t complain = tf_warning_or_error;
4696 if (decltype_p)
4697 complain |= tf_decltype;
4698 return complain;
4701 /* We're about to parse a collection of statements. If we're currently
4702 parsing tentatively, set up a firewall so that any nested
4703 cp_parser_commit_to_tentative_parse won't affect the current context. */
4705 static cp_token_position
4706 cp_parser_start_tentative_firewall (cp_parser *parser)
4708 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4709 return 0;
4711 cp_parser_parse_tentatively (parser);
4712 cp_parser_commit_to_topmost_tentative_parse (parser);
4713 return cp_lexer_token_position (parser->lexer, false);
4716 /* We've finished parsing the collection of statements. Wrap up the
4717 firewall and replace the relevant tokens with the parsed form. */
4719 static void
4720 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4721 tree expr)
4723 if (!start)
4724 return;
4726 /* Finish the firewall level. */
4727 cp_parser_parse_definitely (parser);
4728 /* And remember the result of the parse for when we try again. */
4729 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4730 token->type = CPP_PREPARSED_EXPR;
4731 token->u.value = expr;
4732 token->keyword = RID_MAX;
4733 cp_lexer_purge_tokens_after (parser->lexer, start);
4736 /* Like the above functions, but let the user modify the tokens. Used by
4737 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4738 later parses, so it makes sense to localize the effects of
4739 cp_parser_commit_to_tentative_parse. */
4741 struct tentative_firewall
4743 cp_parser *parser;
4744 bool set;
4746 tentative_firewall (cp_parser *p): parser(p)
4748 /* If we're currently parsing tentatively, start a committed level as a
4749 firewall and then an inner tentative parse. */
4750 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4752 cp_parser_parse_tentatively (parser);
4753 cp_parser_commit_to_topmost_tentative_parse (parser);
4754 cp_parser_parse_tentatively (parser);
4758 ~tentative_firewall()
4760 if (set)
4762 /* Finish the inner tentative parse and the firewall, propagating any
4763 uncommitted error state to the outer tentative parse. */
4764 bool err = cp_parser_error_occurred (parser);
4765 cp_parser_parse_definitely (parser);
4766 cp_parser_parse_definitely (parser);
4767 if (err)
4768 cp_parser_simulate_error (parser);
4773 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4774 This class is for tracking such a matching pair of symbols.
4775 In particular, it tracks the location of the first token,
4776 so that if the second token is missing, we can highlight the
4777 location of the first token when notifying the user about the
4778 problem. */
4780 template <typename traits_t>
4781 class token_pair
4783 public:
4784 /* token_pair's ctor. */
4785 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4787 /* If the next token is the opening symbol for this pair, consume it and
4788 return true.
4789 Otherwise, issue an error and return false.
4790 In either case, record the location of the opening token. */
4792 bool require_open (cp_parser *parser)
4794 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4795 return cp_parser_require (parser, traits_t::open_token_type,
4796 traits_t::required_token_open);
4799 /* Consume the next token from PARSER, recording its location as
4800 that of the opening token within the pair. */
4802 cp_token * consume_open (cp_parser *parser)
4804 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4805 gcc_assert (tok->type == traits_t::open_token_type);
4806 m_open_loc = tok->location;
4807 return tok;
4810 /* If the next token is the closing symbol for this pair, consume it
4811 and return it.
4812 Otherwise, issue an error, highlighting the location of the
4813 corresponding opening token, and return NULL. */
4815 cp_token *require_close (cp_parser *parser) const
4817 return cp_parser_require (parser, traits_t::close_token_type,
4818 traits_t::required_token_close,
4819 m_open_loc);
4822 private:
4823 location_t m_open_loc;
4826 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4828 struct matching_paren_traits
4830 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4831 static const enum required_token required_token_open = RT_OPEN_PAREN;
4832 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4833 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4836 /* "matching_parens" is a token_pair<T> class for tracking matching
4837 pairs of parentheses. */
4839 typedef token_pair<matching_paren_traits> matching_parens;
4841 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4843 struct matching_brace_traits
4845 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4846 static const enum required_token required_token_open = RT_OPEN_BRACE;
4847 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4848 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4851 /* "matching_braces" is a token_pair<T> class for tracking matching
4852 pairs of braces. */
4854 typedef token_pair<matching_brace_traits> matching_braces;
4857 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4858 enclosing parentheses. */
4860 static cp_expr
4861 cp_parser_statement_expr (cp_parser *parser)
4863 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4865 /* Consume the '('. */
4866 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4867 matching_parens parens;
4868 parens.consume_open (parser);
4869 /* Start the statement-expression. */
4870 tree expr = begin_stmt_expr ();
4871 /* Parse the compound-statement. */
4872 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4873 /* Finish up. */
4874 expr = finish_stmt_expr (expr, false);
4875 /* Consume the ')'. */
4876 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4877 if (!parens.require_close (parser))
4878 cp_parser_skip_to_end_of_statement (parser);
4880 cp_parser_end_tentative_firewall (parser, start, expr);
4881 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4882 return cp_expr (expr, combined_loc);
4885 /* Expressions [gram.expr] */
4887 /* Parse a fold-operator.
4889 fold-operator:
4890 - * / % ^ & | = < > << >>
4891 = -= *= /= %= ^= &= |= <<= >>=
4892 == != <= >= && || , .* ->*
4894 This returns the tree code corresponding to the matched operator
4895 as an int. When the current token matches a compound assignment
4896 opertor, the resulting tree code is the negative value of the
4897 non-assignment operator. */
4899 static int
4900 cp_parser_fold_operator (cp_token *token)
4902 switch (token->type)
4904 case CPP_PLUS: return PLUS_EXPR;
4905 case CPP_MINUS: return MINUS_EXPR;
4906 case CPP_MULT: return MULT_EXPR;
4907 case CPP_DIV: return TRUNC_DIV_EXPR;
4908 case CPP_MOD: return TRUNC_MOD_EXPR;
4909 case CPP_XOR: return BIT_XOR_EXPR;
4910 case CPP_AND: return BIT_AND_EXPR;
4911 case CPP_OR: return BIT_IOR_EXPR;
4912 case CPP_LSHIFT: return LSHIFT_EXPR;
4913 case CPP_RSHIFT: return RSHIFT_EXPR;
4915 case CPP_EQ: return -NOP_EXPR;
4916 case CPP_PLUS_EQ: return -PLUS_EXPR;
4917 case CPP_MINUS_EQ: return -MINUS_EXPR;
4918 case CPP_MULT_EQ: return -MULT_EXPR;
4919 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4920 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4921 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4922 case CPP_AND_EQ: return -BIT_AND_EXPR;
4923 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4924 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4925 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4927 case CPP_EQ_EQ: return EQ_EXPR;
4928 case CPP_NOT_EQ: return NE_EXPR;
4929 case CPP_LESS: return LT_EXPR;
4930 case CPP_GREATER: return GT_EXPR;
4931 case CPP_LESS_EQ: return LE_EXPR;
4932 case CPP_GREATER_EQ: return GE_EXPR;
4934 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4935 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4937 case CPP_COMMA: return COMPOUND_EXPR;
4939 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4940 case CPP_DEREF_STAR: return MEMBER_REF;
4942 default: return ERROR_MARK;
4946 /* Returns true if CODE indicates a binary expression, which is not allowed in
4947 the LHS of a fold-expression. More codes will need to be added to use this
4948 function in other contexts. */
4950 static bool
4951 is_binary_op (tree_code code)
4953 switch (code)
4955 case PLUS_EXPR:
4956 case POINTER_PLUS_EXPR:
4957 case MINUS_EXPR:
4958 case MULT_EXPR:
4959 case TRUNC_DIV_EXPR:
4960 case TRUNC_MOD_EXPR:
4961 case BIT_XOR_EXPR:
4962 case BIT_AND_EXPR:
4963 case BIT_IOR_EXPR:
4964 case LSHIFT_EXPR:
4965 case RSHIFT_EXPR:
4967 case MODOP_EXPR:
4969 case EQ_EXPR:
4970 case NE_EXPR:
4971 case LE_EXPR:
4972 case GE_EXPR:
4973 case LT_EXPR:
4974 case GT_EXPR:
4976 case TRUTH_ANDIF_EXPR:
4977 case TRUTH_ORIF_EXPR:
4979 case COMPOUND_EXPR:
4981 case DOTSTAR_EXPR:
4982 case MEMBER_REF:
4983 return true;
4985 default:
4986 return false;
4990 /* If the next token is a suitable fold operator, consume it and return as
4991 the function above. */
4993 static int
4994 cp_parser_fold_operator (cp_parser *parser)
4996 cp_token* token = cp_lexer_peek_token (parser->lexer);
4997 int code = cp_parser_fold_operator (token);
4998 if (code != ERROR_MARK)
4999 cp_lexer_consume_token (parser->lexer);
5000 return code;
5003 /* Parse a fold-expression.
5005 fold-expression:
5006 ( ... folding-operator cast-expression)
5007 ( cast-expression folding-operator ... )
5008 ( cast-expression folding operator ... folding-operator cast-expression)
5010 Note that the '(' and ')' are matched in primary expression. */
5012 static cp_expr
5013 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5015 cp_id_kind pidk;
5017 // Left fold.
5018 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5020 cp_lexer_consume_token (parser->lexer);
5021 int op = cp_parser_fold_operator (parser);
5022 if (op == ERROR_MARK)
5024 cp_parser_error (parser, "expected binary operator");
5025 return error_mark_node;
5028 tree expr = cp_parser_cast_expression (parser, false, false,
5029 false, &pidk);
5030 if (expr == error_mark_node)
5031 return error_mark_node;
5032 return finish_left_unary_fold_expr (expr, op);
5035 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5036 int op = cp_parser_fold_operator (parser);
5037 if (op == ERROR_MARK)
5039 cp_parser_error (parser, "expected binary operator");
5040 return error_mark_node;
5043 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5045 cp_parser_error (parser, "expected ...");
5046 return error_mark_node;
5048 cp_lexer_consume_token (parser->lexer);
5050 /* The operands of a fold-expression are cast-expressions, so binary or
5051 conditional expressions are not allowed. We check this here to avoid
5052 tentative parsing. */
5053 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5054 /* OK, the expression was parenthesized. */;
5055 else if (is_binary_op (TREE_CODE (expr1)))
5056 error_at (location_of (expr1),
5057 "binary expression in operand of fold-expression");
5058 else if (TREE_CODE (expr1) == COND_EXPR
5059 || (REFERENCE_REF_P (expr1)
5060 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5061 error_at (location_of (expr1),
5062 "conditional expression in operand of fold-expression");
5064 // Right fold.
5065 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5066 return finish_right_unary_fold_expr (expr1, op);
5068 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5070 cp_parser_error (parser, "mismatched operator in fold-expression");
5071 return error_mark_node;
5073 cp_lexer_consume_token (parser->lexer);
5075 // Binary left or right fold.
5076 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5077 if (expr2 == error_mark_node)
5078 return error_mark_node;
5079 return finish_binary_fold_expr (expr1, expr2, op);
5082 /* Parse a primary-expression.
5084 primary-expression:
5085 literal
5086 this
5087 ( expression )
5088 id-expression
5089 lambda-expression (C++11)
5091 GNU Extensions:
5093 primary-expression:
5094 ( compound-statement )
5095 __builtin_va_arg ( assignment-expression , type-id )
5096 __builtin_offsetof ( type-id , offsetof-expression )
5098 C++ Extensions:
5099 __has_nothrow_assign ( type-id )
5100 __has_nothrow_constructor ( type-id )
5101 __has_nothrow_copy ( type-id )
5102 __has_trivial_assign ( type-id )
5103 __has_trivial_constructor ( type-id )
5104 __has_trivial_copy ( type-id )
5105 __has_trivial_destructor ( type-id )
5106 __has_virtual_destructor ( type-id )
5107 __is_abstract ( type-id )
5108 __is_base_of ( type-id , type-id )
5109 __is_class ( type-id )
5110 __is_empty ( type-id )
5111 __is_enum ( type-id )
5112 __is_final ( type-id )
5113 __is_literal_type ( type-id )
5114 __is_pod ( type-id )
5115 __is_polymorphic ( type-id )
5116 __is_std_layout ( type-id )
5117 __is_trivial ( type-id )
5118 __is_union ( type-id )
5120 Objective-C++ Extension:
5122 primary-expression:
5123 objc-expression
5125 literal:
5126 __null
5128 ADDRESS_P is true iff this expression was immediately preceded by
5129 "&" and therefore might denote a pointer-to-member. CAST_P is true
5130 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5131 true iff this expression is a template argument.
5133 Returns a representation of the expression. Upon return, *IDK
5134 indicates what kind of id-expression (if any) was present. */
5136 static cp_expr
5137 cp_parser_primary_expression (cp_parser *parser,
5138 bool address_p,
5139 bool cast_p,
5140 bool template_arg_p,
5141 bool decltype_p,
5142 cp_id_kind *idk)
5144 cp_token *token = NULL;
5146 /* Assume the primary expression is not an id-expression. */
5147 *idk = CP_ID_KIND_NONE;
5149 /* Peek at the next token. */
5150 token = cp_lexer_peek_token (parser->lexer);
5151 switch ((int) token->type)
5153 /* literal:
5154 integer-literal
5155 character-literal
5156 floating-literal
5157 string-literal
5158 boolean-literal
5159 pointer-literal
5160 user-defined-literal */
5161 case CPP_CHAR:
5162 case CPP_CHAR16:
5163 case CPP_CHAR32:
5164 case CPP_WCHAR:
5165 case CPP_UTF8CHAR:
5166 case CPP_NUMBER:
5167 case CPP_PREPARSED_EXPR:
5168 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5169 return cp_parser_userdef_numeric_literal (parser);
5170 token = cp_lexer_consume_token (parser->lexer);
5171 if (TREE_CODE (token->u.value) == FIXED_CST)
5173 error_at (token->location,
5174 "fixed-point types not supported in C++");
5175 return error_mark_node;
5177 /* Floating-point literals are only allowed in an integral
5178 constant expression if they are cast to an integral or
5179 enumeration type. */
5180 if (TREE_CODE (token->u.value) == REAL_CST
5181 && parser->integral_constant_expression_p
5182 && pedantic)
5184 /* CAST_P will be set even in invalid code like "int(2.7 +
5185 ...)". Therefore, we have to check that the next token
5186 is sure to end the cast. */
5187 if (cast_p)
5189 cp_token *next_token;
5191 next_token = cp_lexer_peek_token (parser->lexer);
5192 if (/* The comma at the end of an
5193 enumerator-definition. */
5194 next_token->type != CPP_COMMA
5195 /* The curly brace at the end of an enum-specifier. */
5196 && next_token->type != CPP_CLOSE_BRACE
5197 /* The end of a statement. */
5198 && next_token->type != CPP_SEMICOLON
5199 /* The end of the cast-expression. */
5200 && next_token->type != CPP_CLOSE_PAREN
5201 /* The end of an array bound. */
5202 && next_token->type != CPP_CLOSE_SQUARE
5203 /* The closing ">" in a template-argument-list. */
5204 && (next_token->type != CPP_GREATER
5205 || parser->greater_than_is_operator_p)
5206 /* C++0x only: A ">>" treated like two ">" tokens,
5207 in a template-argument-list. */
5208 && (next_token->type != CPP_RSHIFT
5209 || (cxx_dialect == cxx98)
5210 || parser->greater_than_is_operator_p))
5211 cast_p = false;
5214 /* If we are within a cast, then the constraint that the
5215 cast is to an integral or enumeration type will be
5216 checked at that point. If we are not within a cast, then
5217 this code is invalid. */
5218 if (!cast_p)
5219 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5221 return cp_expr (token->u.value, token->location);
5223 case CPP_CHAR_USERDEF:
5224 case CPP_CHAR16_USERDEF:
5225 case CPP_CHAR32_USERDEF:
5226 case CPP_WCHAR_USERDEF:
5227 case CPP_UTF8CHAR_USERDEF:
5228 return cp_parser_userdef_char_literal (parser);
5230 case CPP_STRING:
5231 case CPP_STRING16:
5232 case CPP_STRING32:
5233 case CPP_WSTRING:
5234 case CPP_UTF8STRING:
5235 case CPP_STRING_USERDEF:
5236 case CPP_STRING16_USERDEF:
5237 case CPP_STRING32_USERDEF:
5238 case CPP_WSTRING_USERDEF:
5239 case CPP_UTF8STRING_USERDEF:
5240 /* ??? Should wide strings be allowed when parser->translate_strings_p
5241 is false (i.e. in attributes)? If not, we can kill the third
5242 argument to cp_parser_string_literal. */
5243 return cp_parser_string_literal (parser,
5244 parser->translate_strings_p,
5245 true);
5247 case CPP_OPEN_PAREN:
5248 /* If we see `( { ' then we are looking at the beginning of
5249 a GNU statement-expression. */
5250 if (cp_parser_allow_gnu_extensions_p (parser)
5251 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5253 /* Statement-expressions are not allowed by the standard. */
5254 pedwarn (token->location, OPT_Wpedantic,
5255 "ISO C++ forbids braced-groups within expressions");
5257 /* And they're not allowed outside of a function-body; you
5258 cannot, for example, write:
5260 int i = ({ int j = 3; j + 1; });
5262 at class or namespace scope. */
5263 if (!parser->in_function_body
5264 || parser->in_template_argument_list_p)
5266 error_at (token->location,
5267 "statement-expressions are not allowed outside "
5268 "functions nor in template-argument lists");
5269 cp_parser_skip_to_end_of_block_or_statement (parser);
5270 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5271 cp_lexer_consume_token (parser->lexer);
5272 return error_mark_node;
5274 else
5275 return cp_parser_statement_expr (parser);
5277 /* Otherwise it's a normal parenthesized expression. */
5279 cp_expr expr;
5280 bool saved_greater_than_is_operator_p;
5282 location_t open_paren_loc = token->location;
5284 /* Consume the `('. */
5285 matching_parens parens;
5286 parens.consume_open (parser);
5287 /* Within a parenthesized expression, a `>' token is always
5288 the greater-than operator. */
5289 saved_greater_than_is_operator_p
5290 = parser->greater_than_is_operator_p;
5291 parser->greater_than_is_operator_p = true;
5293 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5294 /* Left fold expression. */
5295 expr = NULL_TREE;
5296 else
5297 /* Parse the parenthesized expression. */
5298 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5300 token = cp_lexer_peek_token (parser->lexer);
5301 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5303 expr = cp_parser_fold_expression (parser, expr);
5304 if (expr != error_mark_node
5305 && cxx_dialect < cxx17
5306 && !in_system_header_at (input_location))
5307 pedwarn (input_location, 0, "fold-expressions only available "
5308 "with -std=c++17 or -std=gnu++17");
5310 else
5311 /* Let the front end know that this expression was
5312 enclosed in parentheses. This matters in case, for
5313 example, the expression is of the form `A::B', since
5314 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5315 not. */
5316 expr = finish_parenthesized_expr (expr);
5318 /* DR 705: Wrapping an unqualified name in parentheses
5319 suppresses arg-dependent lookup. We want to pass back
5320 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5321 (c++/37862), but none of the others. */
5322 if (*idk != CP_ID_KIND_QUALIFIED)
5323 *idk = CP_ID_KIND_NONE;
5325 /* The `>' token might be the end of a template-id or
5326 template-parameter-list now. */
5327 parser->greater_than_is_operator_p
5328 = saved_greater_than_is_operator_p;
5330 /* Consume the `)'. */
5331 token = cp_lexer_peek_token (parser->lexer);
5332 location_t close_paren_loc = token->location;
5333 expr.set_range (open_paren_loc, close_paren_loc);
5334 if (!parens.require_close (parser)
5335 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5336 cp_parser_skip_to_end_of_statement (parser);
5338 return expr;
5341 case CPP_OPEN_SQUARE:
5343 if (c_dialect_objc ())
5345 /* We might have an Objective-C++ message. */
5346 cp_parser_parse_tentatively (parser);
5347 tree msg = cp_parser_objc_message_expression (parser);
5348 /* If that works out, we're done ... */
5349 if (cp_parser_parse_definitely (parser))
5350 return msg;
5351 /* ... else, fall though to see if it's a lambda. */
5353 cp_expr lam = cp_parser_lambda_expression (parser);
5354 /* Don't warn about a failed tentative parse. */
5355 if (cp_parser_error_occurred (parser))
5356 return error_mark_node;
5357 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5358 return lam;
5361 case CPP_OBJC_STRING:
5362 if (c_dialect_objc ())
5363 /* We have an Objective-C++ string literal. */
5364 return cp_parser_objc_expression (parser);
5365 cp_parser_error (parser, "expected primary-expression");
5366 return error_mark_node;
5368 case CPP_KEYWORD:
5369 switch (token->keyword)
5371 /* These two are the boolean literals. */
5372 case RID_TRUE:
5373 cp_lexer_consume_token (parser->lexer);
5374 return cp_expr (boolean_true_node, token->location);
5375 case RID_FALSE:
5376 cp_lexer_consume_token (parser->lexer);
5377 return cp_expr (boolean_false_node, token->location);
5379 /* The `__null' literal. */
5380 case RID_NULL:
5381 cp_lexer_consume_token (parser->lexer);
5382 return cp_expr (null_node, token->location);
5384 /* The `nullptr' literal. */
5385 case RID_NULLPTR:
5386 cp_lexer_consume_token (parser->lexer);
5387 return cp_expr (nullptr_node, token->location);
5389 /* Recognize the `this' keyword. */
5390 case RID_THIS:
5391 cp_lexer_consume_token (parser->lexer);
5392 if (parser->local_variables_forbidden_p)
5394 error_at (token->location,
5395 "%<this%> may not be used in this context");
5396 return error_mark_node;
5398 /* Pointers cannot appear in constant-expressions. */
5399 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5400 return error_mark_node;
5401 return cp_expr (finish_this_expr (), token->location);
5403 /* The `operator' keyword can be the beginning of an
5404 id-expression. */
5405 case RID_OPERATOR:
5406 goto id_expression;
5408 case RID_FUNCTION_NAME:
5409 case RID_PRETTY_FUNCTION_NAME:
5410 case RID_C99_FUNCTION_NAME:
5412 non_integral_constant name;
5414 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5415 __func__ are the names of variables -- but they are
5416 treated specially. Therefore, they are handled here,
5417 rather than relying on the generic id-expression logic
5418 below. Grammatically, these names are id-expressions.
5420 Consume the token. */
5421 token = cp_lexer_consume_token (parser->lexer);
5423 switch (token->keyword)
5425 case RID_FUNCTION_NAME:
5426 name = NIC_FUNC_NAME;
5427 break;
5428 case RID_PRETTY_FUNCTION_NAME:
5429 name = NIC_PRETTY_FUNC;
5430 break;
5431 case RID_C99_FUNCTION_NAME:
5432 name = NIC_C99_FUNC;
5433 break;
5434 default:
5435 gcc_unreachable ();
5438 if (cp_parser_non_integral_constant_expression (parser, name))
5439 return error_mark_node;
5441 /* Look up the name. */
5442 return finish_fname (token->u.value);
5445 case RID_VA_ARG:
5447 tree expression;
5448 tree type;
5449 location_t type_location;
5450 location_t start_loc
5451 = cp_lexer_peek_token (parser->lexer)->location;
5452 /* The `__builtin_va_arg' construct is used to handle
5453 `va_arg'. Consume the `__builtin_va_arg' token. */
5454 cp_lexer_consume_token (parser->lexer);
5455 /* Look for the opening `('. */
5456 matching_parens parens;
5457 parens.require_open (parser);
5458 /* Now, parse the assignment-expression. */
5459 expression = cp_parser_assignment_expression (parser);
5460 /* Look for the `,'. */
5461 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5462 type_location = cp_lexer_peek_token (parser->lexer)->location;
5463 /* Parse the type-id. */
5465 type_id_in_expr_sentinel s (parser);
5466 type = cp_parser_type_id (parser);
5468 /* Look for the closing `)'. */
5469 location_t finish_loc
5470 = cp_lexer_peek_token (parser->lexer)->location;
5471 parens.require_close (parser);
5472 /* Using `va_arg' in a constant-expression is not
5473 allowed. */
5474 if (cp_parser_non_integral_constant_expression (parser,
5475 NIC_VA_ARG))
5476 return error_mark_node;
5477 /* Construct a location of the form:
5478 __builtin_va_arg (v, int)
5479 ~~~~~~~~~~~~~~~~~~~~~^~~~
5480 with the caret at the type, ranging from the start of the
5481 "__builtin_va_arg" token to the close paren. */
5482 location_t combined_loc
5483 = make_location (type_location, start_loc, finish_loc);
5484 return build_x_va_arg (combined_loc, expression, type);
5487 case RID_OFFSETOF:
5488 return cp_parser_builtin_offsetof (parser);
5490 case RID_HAS_NOTHROW_ASSIGN:
5491 case RID_HAS_NOTHROW_CONSTRUCTOR:
5492 case RID_HAS_NOTHROW_COPY:
5493 case RID_HAS_TRIVIAL_ASSIGN:
5494 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5495 case RID_HAS_TRIVIAL_COPY:
5496 case RID_HAS_TRIVIAL_DESTRUCTOR:
5497 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5498 case RID_HAS_VIRTUAL_DESTRUCTOR:
5499 case RID_IS_ABSTRACT:
5500 case RID_IS_AGGREGATE:
5501 case RID_IS_BASE_OF:
5502 case RID_IS_CLASS:
5503 case RID_IS_EMPTY:
5504 case RID_IS_ENUM:
5505 case RID_IS_FINAL:
5506 case RID_IS_LITERAL_TYPE:
5507 case RID_IS_POD:
5508 case RID_IS_POLYMORPHIC:
5509 case RID_IS_SAME_AS:
5510 case RID_IS_STD_LAYOUT:
5511 case RID_IS_TRIVIAL:
5512 case RID_IS_TRIVIALLY_ASSIGNABLE:
5513 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5514 case RID_IS_TRIVIALLY_COPYABLE:
5515 case RID_IS_UNION:
5516 case RID_IS_ASSIGNABLE:
5517 case RID_IS_CONSTRUCTIBLE:
5518 return cp_parser_trait_expr (parser, token->keyword);
5520 // C++ concepts
5521 case RID_REQUIRES:
5522 return cp_parser_requires_expression (parser);
5524 /* Objective-C++ expressions. */
5525 case RID_AT_ENCODE:
5526 case RID_AT_PROTOCOL:
5527 case RID_AT_SELECTOR:
5528 return cp_parser_objc_expression (parser);
5530 case RID_TEMPLATE:
5531 if (parser->in_function_body
5532 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5533 == CPP_LESS))
5535 error_at (token->location,
5536 "a template declaration cannot appear at block scope");
5537 cp_parser_skip_to_end_of_block_or_statement (parser);
5538 return error_mark_node;
5540 /* FALLTHRU */
5541 default:
5542 cp_parser_error (parser, "expected primary-expression");
5543 return error_mark_node;
5546 /* An id-expression can start with either an identifier, a
5547 `::' as the beginning of a qualified-id, or the "operator"
5548 keyword. */
5549 case CPP_NAME:
5550 case CPP_SCOPE:
5551 case CPP_TEMPLATE_ID:
5552 case CPP_NESTED_NAME_SPECIFIER:
5554 id_expression:
5555 cp_expr id_expression;
5556 cp_expr decl;
5557 const char *error_msg;
5558 bool template_p;
5559 bool done;
5560 cp_token *id_expr_token;
5562 /* Parse the id-expression. */
5563 id_expression
5564 = cp_parser_id_expression (parser,
5565 /*template_keyword_p=*/false,
5566 /*check_dependency_p=*/true,
5567 &template_p,
5568 /*declarator_p=*/false,
5569 /*optional_p=*/false);
5570 if (id_expression == error_mark_node)
5571 return error_mark_node;
5572 id_expr_token = token;
5573 token = cp_lexer_peek_token (parser->lexer);
5574 done = (token->type != CPP_OPEN_SQUARE
5575 && token->type != CPP_OPEN_PAREN
5576 && token->type != CPP_DOT
5577 && token->type != CPP_DEREF
5578 && token->type != CPP_PLUS_PLUS
5579 && token->type != CPP_MINUS_MINUS);
5580 /* If we have a template-id, then no further lookup is
5581 required. If the template-id was for a template-class, we
5582 will sometimes have a TYPE_DECL at this point. */
5583 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5584 || TREE_CODE (id_expression) == TYPE_DECL)
5585 decl = id_expression;
5586 /* Look up the name. */
5587 else
5589 tree ambiguous_decls;
5591 /* If we already know that this lookup is ambiguous, then
5592 we've already issued an error message; there's no reason
5593 to check again. */
5594 if (id_expr_token->type == CPP_NAME
5595 && id_expr_token->error_reported)
5597 cp_parser_simulate_error (parser);
5598 return error_mark_node;
5601 decl = cp_parser_lookup_name (parser, id_expression,
5602 none_type,
5603 template_p,
5604 /*is_namespace=*/false,
5605 /*check_dependency=*/true,
5606 &ambiguous_decls,
5607 id_expression.get_location ());
5608 /* If the lookup was ambiguous, an error will already have
5609 been issued. */
5610 if (ambiguous_decls)
5611 return error_mark_node;
5613 /* In Objective-C++, we may have an Objective-C 2.0
5614 dot-syntax for classes here. */
5615 if (c_dialect_objc ()
5616 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5617 && TREE_CODE (decl) == TYPE_DECL
5618 && objc_is_class_name (decl))
5620 tree component;
5621 cp_lexer_consume_token (parser->lexer);
5622 component = cp_parser_identifier (parser);
5623 if (component == error_mark_node)
5624 return error_mark_node;
5626 tree result = objc_build_class_component_ref (id_expression,
5627 component);
5628 /* Build a location of the form:
5629 expr.component
5630 ~~~~~^~~~~~~~~
5631 with caret at the start of the component name (at
5632 input_location), ranging from the start of the id_expression
5633 to the end of the component name. */
5634 location_t combined_loc
5635 = make_location (input_location, id_expression.get_start (),
5636 get_finish (input_location));
5637 protected_set_expr_location (result, combined_loc);
5638 return result;
5641 /* In Objective-C++, an instance variable (ivar) may be preferred
5642 to whatever cp_parser_lookup_name() found.
5643 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5644 rest of c-family, we have to do a little extra work to preserve
5645 any location information in cp_expr "decl". Given that
5646 objc_lookup_ivar is implemented in "c-family" and "objc", we
5647 have a trip through the pure "tree" type, rather than cp_expr.
5648 Naively copying it back to "decl" would implicitly give the
5649 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5650 store an EXPR_LOCATION. Hence we only update "decl" (and
5651 hence its location_t) if we get back a different tree node. */
5652 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5653 id_expression);
5654 if (decl_tree != decl.get_value ())
5655 decl = cp_expr (decl_tree);
5657 /* If name lookup gives us a SCOPE_REF, then the
5658 qualifying scope was dependent. */
5659 if (TREE_CODE (decl) == SCOPE_REF)
5661 /* At this point, we do not know if DECL is a valid
5662 integral constant expression. We assume that it is
5663 in fact such an expression, so that code like:
5665 template <int N> struct A {
5666 int a[B<N>::i];
5669 is accepted. At template-instantiation time, we
5670 will check that B<N>::i is actually a constant. */
5671 return decl;
5673 /* Check to see if DECL is a local variable in a context
5674 where that is forbidden. */
5675 if (parser->local_variables_forbidden_p
5676 && local_variable_p (decl))
5678 error_at (id_expression.get_location (),
5679 "local variable %qD may not appear in this context",
5680 decl.get_value ());
5681 return error_mark_node;
5685 decl = (finish_id_expression
5686 (id_expression, decl, parser->scope,
5687 idk,
5688 parser->integral_constant_expression_p,
5689 parser->allow_non_integral_constant_expression_p,
5690 &parser->non_integral_constant_expression_p,
5691 template_p, done, address_p,
5692 template_arg_p,
5693 &error_msg,
5694 id_expression.get_location ()));
5695 if (error_msg)
5696 cp_parser_error (parser, error_msg);
5697 decl.set_location (id_expression.get_location ());
5698 decl.set_range (id_expr_token->location, id_expression.get_finish ());
5699 return decl;
5702 /* Anything else is an error. */
5703 default:
5704 cp_parser_error (parser, "expected primary-expression");
5705 return error_mark_node;
5709 static inline cp_expr
5710 cp_parser_primary_expression (cp_parser *parser,
5711 bool address_p,
5712 bool cast_p,
5713 bool template_arg_p,
5714 cp_id_kind *idk)
5716 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5717 /*decltype*/false, idk);
5720 /* Parse an id-expression.
5722 id-expression:
5723 unqualified-id
5724 qualified-id
5726 qualified-id:
5727 :: [opt] nested-name-specifier template [opt] unqualified-id
5728 :: identifier
5729 :: operator-function-id
5730 :: template-id
5732 Return a representation of the unqualified portion of the
5733 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5734 a `::' or nested-name-specifier.
5736 Often, if the id-expression was a qualified-id, the caller will
5737 want to make a SCOPE_REF to represent the qualified-id. This
5738 function does not do this in order to avoid wastefully creating
5739 SCOPE_REFs when they are not required.
5741 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5742 `template' keyword.
5744 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5745 uninstantiated templates.
5747 If *TEMPLATE_P is non-NULL, it is set to true iff the
5748 `template' keyword is used to explicitly indicate that the entity
5749 named is a template.
5751 If DECLARATOR_P is true, the id-expression is appearing as part of
5752 a declarator, rather than as part of an expression. */
5754 static cp_expr
5755 cp_parser_id_expression (cp_parser *parser,
5756 bool template_keyword_p,
5757 bool check_dependency_p,
5758 bool *template_p,
5759 bool declarator_p,
5760 bool optional_p)
5762 bool global_scope_p;
5763 bool nested_name_specifier_p;
5765 /* Assume the `template' keyword was not used. */
5766 if (template_p)
5767 *template_p = template_keyword_p;
5769 /* Look for the optional `::' operator. */
5770 global_scope_p
5771 = (!template_keyword_p
5772 && (cp_parser_global_scope_opt (parser,
5773 /*current_scope_valid_p=*/false)
5774 != NULL_TREE));
5776 /* Look for the optional nested-name-specifier. */
5777 nested_name_specifier_p
5778 = (cp_parser_nested_name_specifier_opt (parser,
5779 /*typename_keyword_p=*/false,
5780 check_dependency_p,
5781 /*type_p=*/false,
5782 declarator_p,
5783 template_keyword_p)
5784 != NULL_TREE);
5786 /* If there is a nested-name-specifier, then we are looking at
5787 the first qualified-id production. */
5788 if (nested_name_specifier_p)
5790 tree saved_scope;
5791 tree saved_object_scope;
5792 tree saved_qualifying_scope;
5793 cp_expr unqualified_id;
5794 bool is_template;
5796 /* See if the next token is the `template' keyword. */
5797 if (!template_p)
5798 template_p = &is_template;
5799 *template_p = cp_parser_optional_template_keyword (parser);
5800 /* Name lookup we do during the processing of the
5801 unqualified-id might obliterate SCOPE. */
5802 saved_scope = parser->scope;
5803 saved_object_scope = parser->object_scope;
5804 saved_qualifying_scope = parser->qualifying_scope;
5805 /* Process the final unqualified-id. */
5806 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5807 check_dependency_p,
5808 declarator_p,
5809 /*optional_p=*/false);
5810 /* Restore the SAVED_SCOPE for our caller. */
5811 parser->scope = saved_scope;
5812 parser->object_scope = saved_object_scope;
5813 parser->qualifying_scope = saved_qualifying_scope;
5815 return unqualified_id;
5817 /* Otherwise, if we are in global scope, then we are looking at one
5818 of the other qualified-id productions. */
5819 else if (global_scope_p)
5821 cp_token *token;
5822 tree id;
5824 /* Peek at the next token. */
5825 token = cp_lexer_peek_token (parser->lexer);
5827 /* If it's an identifier, and the next token is not a "<", then
5828 we can avoid the template-id case. This is an optimization
5829 for this common case. */
5830 if (token->type == CPP_NAME
5831 && !cp_parser_nth_token_starts_template_argument_list_p
5832 (parser, 2))
5833 return cp_parser_identifier (parser);
5835 cp_parser_parse_tentatively (parser);
5836 /* Try a template-id. */
5837 id = cp_parser_template_id (parser,
5838 /*template_keyword_p=*/false,
5839 /*check_dependency_p=*/true,
5840 none_type,
5841 declarator_p);
5842 /* If that worked, we're done. */
5843 if (cp_parser_parse_definitely (parser))
5844 return id;
5846 /* Peek at the next token. (Changes in the token buffer may
5847 have invalidated the pointer obtained above.) */
5848 token = cp_lexer_peek_token (parser->lexer);
5850 switch (token->type)
5852 case CPP_NAME:
5853 return cp_parser_identifier (parser);
5855 case CPP_KEYWORD:
5856 if (token->keyword == RID_OPERATOR)
5857 return cp_parser_operator_function_id (parser);
5858 /* Fall through. */
5860 default:
5861 cp_parser_error (parser, "expected id-expression");
5862 return error_mark_node;
5865 else
5866 return cp_parser_unqualified_id (parser, template_keyword_p,
5867 /*check_dependency_p=*/true,
5868 declarator_p,
5869 optional_p);
5872 /* Parse an unqualified-id.
5874 unqualified-id:
5875 identifier
5876 operator-function-id
5877 conversion-function-id
5878 ~ class-name
5879 template-id
5881 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5882 keyword, in a construct like `A::template ...'.
5884 Returns a representation of unqualified-id. For the `identifier'
5885 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5886 production a BIT_NOT_EXPR is returned; the operand of the
5887 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5888 other productions, see the documentation accompanying the
5889 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5890 names are looked up in uninstantiated templates. If DECLARATOR_P
5891 is true, the unqualified-id is appearing as part of a declarator,
5892 rather than as part of an expression. */
5894 static cp_expr
5895 cp_parser_unqualified_id (cp_parser* parser,
5896 bool template_keyword_p,
5897 bool check_dependency_p,
5898 bool declarator_p,
5899 bool optional_p)
5901 cp_token *token;
5903 /* Peek at the next token. */
5904 token = cp_lexer_peek_token (parser->lexer);
5906 switch ((int) token->type)
5908 case CPP_NAME:
5910 tree id;
5912 /* We don't know yet whether or not this will be a
5913 template-id. */
5914 cp_parser_parse_tentatively (parser);
5915 /* Try a template-id. */
5916 id = cp_parser_template_id (parser, template_keyword_p,
5917 check_dependency_p,
5918 none_type,
5919 declarator_p);
5920 /* If it worked, we're done. */
5921 if (cp_parser_parse_definitely (parser))
5922 return id;
5923 /* Otherwise, it's an ordinary identifier. */
5924 return cp_parser_identifier (parser);
5927 case CPP_TEMPLATE_ID:
5928 return cp_parser_template_id (parser, template_keyword_p,
5929 check_dependency_p,
5930 none_type,
5931 declarator_p);
5933 case CPP_COMPL:
5935 tree type_decl;
5936 tree qualifying_scope;
5937 tree object_scope;
5938 tree scope;
5939 bool done;
5941 /* Consume the `~' token. */
5942 cp_lexer_consume_token (parser->lexer);
5943 /* Parse the class-name. The standard, as written, seems to
5944 say that:
5946 template <typename T> struct S { ~S (); };
5947 template <typename T> S<T>::~S() {}
5949 is invalid, since `~' must be followed by a class-name, but
5950 `S<T>' is dependent, and so not known to be a class.
5951 That's not right; we need to look in uninstantiated
5952 templates. A further complication arises from:
5954 template <typename T> void f(T t) {
5955 t.T::~T();
5958 Here, it is not possible to look up `T' in the scope of `T'
5959 itself. We must look in both the current scope, and the
5960 scope of the containing complete expression.
5962 Yet another issue is:
5964 struct S {
5965 int S;
5966 ~S();
5969 S::~S() {}
5971 The standard does not seem to say that the `S' in `~S'
5972 should refer to the type `S' and not the data member
5973 `S::S'. */
5975 /* DR 244 says that we look up the name after the "~" in the
5976 same scope as we looked up the qualifying name. That idea
5977 isn't fully worked out; it's more complicated than that. */
5978 scope = parser->scope;
5979 object_scope = parser->object_scope;
5980 qualifying_scope = parser->qualifying_scope;
5982 /* Check for invalid scopes. */
5983 if (scope == error_mark_node)
5985 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5986 cp_lexer_consume_token (parser->lexer);
5987 return error_mark_node;
5989 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5991 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5992 error_at (token->location,
5993 "scope %qT before %<~%> is not a class-name",
5994 scope);
5995 cp_parser_simulate_error (parser);
5996 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5997 cp_lexer_consume_token (parser->lexer);
5998 return error_mark_node;
6000 gcc_assert (!scope || TYPE_P (scope));
6002 /* If the name is of the form "X::~X" it's OK even if X is a
6003 typedef. */
6004 token = cp_lexer_peek_token (parser->lexer);
6005 if (scope
6006 && token->type == CPP_NAME
6007 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6008 != CPP_LESS)
6009 && (token->u.value == TYPE_IDENTIFIER (scope)
6010 || (CLASS_TYPE_P (scope)
6011 && constructor_name_p (token->u.value, scope))))
6013 cp_lexer_consume_token (parser->lexer);
6014 return build_nt (BIT_NOT_EXPR, scope);
6017 /* ~auto means the destructor of whatever the object is. */
6018 if (cp_parser_is_keyword (token, RID_AUTO))
6020 if (cxx_dialect < cxx14)
6021 pedwarn (input_location, 0,
6022 "%<~auto%> only available with "
6023 "-std=c++14 or -std=gnu++14");
6024 cp_lexer_consume_token (parser->lexer);
6025 return build_nt (BIT_NOT_EXPR, make_auto ());
6028 /* If there was an explicit qualification (S::~T), first look
6029 in the scope given by the qualification (i.e., S).
6031 Note: in the calls to cp_parser_class_name below we pass
6032 typename_type so that lookup finds the injected-class-name
6033 rather than the constructor. */
6034 done = false;
6035 type_decl = NULL_TREE;
6036 if (scope)
6038 cp_parser_parse_tentatively (parser);
6039 type_decl = cp_parser_class_name (parser,
6040 /*typename_keyword_p=*/false,
6041 /*template_keyword_p=*/false,
6042 typename_type,
6043 /*check_dependency=*/false,
6044 /*class_head_p=*/false,
6045 declarator_p);
6046 if (cp_parser_parse_definitely (parser))
6047 done = true;
6049 /* In "N::S::~S", look in "N" as well. */
6050 if (!done && scope && qualifying_scope)
6052 cp_parser_parse_tentatively (parser);
6053 parser->scope = qualifying_scope;
6054 parser->object_scope = NULL_TREE;
6055 parser->qualifying_scope = NULL_TREE;
6056 type_decl
6057 = cp_parser_class_name (parser,
6058 /*typename_keyword_p=*/false,
6059 /*template_keyword_p=*/false,
6060 typename_type,
6061 /*check_dependency=*/false,
6062 /*class_head_p=*/false,
6063 declarator_p);
6064 if (cp_parser_parse_definitely (parser))
6065 done = true;
6067 /* In "p->S::~T", look in the scope given by "*p" as well. */
6068 else if (!done && object_scope)
6070 cp_parser_parse_tentatively (parser);
6071 parser->scope = object_scope;
6072 parser->object_scope = NULL_TREE;
6073 parser->qualifying_scope = NULL_TREE;
6074 type_decl
6075 = cp_parser_class_name (parser,
6076 /*typename_keyword_p=*/false,
6077 /*template_keyword_p=*/false,
6078 typename_type,
6079 /*check_dependency=*/false,
6080 /*class_head_p=*/false,
6081 declarator_p);
6082 if (cp_parser_parse_definitely (parser))
6083 done = true;
6085 /* Look in the surrounding context. */
6086 if (!done)
6088 parser->scope = NULL_TREE;
6089 parser->object_scope = NULL_TREE;
6090 parser->qualifying_scope = NULL_TREE;
6091 if (processing_template_decl)
6092 cp_parser_parse_tentatively (parser);
6093 type_decl
6094 = cp_parser_class_name (parser,
6095 /*typename_keyword_p=*/false,
6096 /*template_keyword_p=*/false,
6097 typename_type,
6098 /*check_dependency=*/false,
6099 /*class_head_p=*/false,
6100 declarator_p);
6101 if (processing_template_decl
6102 && ! cp_parser_parse_definitely (parser))
6104 /* We couldn't find a type with this name. If we're parsing
6105 tentatively, fail and try something else. */
6106 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6108 cp_parser_simulate_error (parser);
6109 return error_mark_node;
6111 /* Otherwise, accept it and check for a match at instantiation
6112 time. */
6113 type_decl = cp_parser_identifier (parser);
6114 if (type_decl != error_mark_node)
6115 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6116 return type_decl;
6119 /* If an error occurred, assume that the name of the
6120 destructor is the same as the name of the qualifying
6121 class. That allows us to keep parsing after running
6122 into ill-formed destructor names. */
6123 if (type_decl == error_mark_node && scope)
6124 return build_nt (BIT_NOT_EXPR, scope);
6125 else if (type_decl == error_mark_node)
6126 return error_mark_node;
6128 /* Check that destructor name and scope match. */
6129 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6131 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6132 error_at (token->location,
6133 "declaration of %<~%T%> as member of %qT",
6134 type_decl, scope);
6135 cp_parser_simulate_error (parser);
6136 return error_mark_node;
6139 /* [class.dtor]
6141 A typedef-name that names a class shall not be used as the
6142 identifier in the declarator for a destructor declaration. */
6143 if (declarator_p
6144 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6145 && !DECL_SELF_REFERENCE_P (type_decl)
6146 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6147 error_at (token->location,
6148 "typedef-name %qD used as destructor declarator",
6149 type_decl);
6151 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6154 case CPP_KEYWORD:
6155 if (token->keyword == RID_OPERATOR)
6157 cp_expr id;
6159 /* This could be a template-id, so we try that first. */
6160 cp_parser_parse_tentatively (parser);
6161 /* Try a template-id. */
6162 id = cp_parser_template_id (parser, template_keyword_p,
6163 /*check_dependency_p=*/true,
6164 none_type,
6165 declarator_p);
6166 /* If that worked, we're done. */
6167 if (cp_parser_parse_definitely (parser))
6168 return id;
6169 /* We still don't know whether we're looking at an
6170 operator-function-id or a conversion-function-id. */
6171 cp_parser_parse_tentatively (parser);
6172 /* Try an operator-function-id. */
6173 id = cp_parser_operator_function_id (parser);
6174 /* If that didn't work, try a conversion-function-id. */
6175 if (!cp_parser_parse_definitely (parser))
6176 id = cp_parser_conversion_function_id (parser);
6178 return id;
6180 /* Fall through. */
6182 default:
6183 if (optional_p)
6184 return NULL_TREE;
6185 cp_parser_error (parser, "expected unqualified-id");
6186 return error_mark_node;
6190 /* Parse an (optional) nested-name-specifier.
6192 nested-name-specifier: [C++98]
6193 class-or-namespace-name :: nested-name-specifier [opt]
6194 class-or-namespace-name :: template nested-name-specifier [opt]
6196 nested-name-specifier: [C++0x]
6197 type-name ::
6198 namespace-name ::
6199 nested-name-specifier identifier ::
6200 nested-name-specifier template [opt] simple-template-id ::
6202 PARSER->SCOPE should be set appropriately before this function is
6203 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6204 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6205 in name lookups.
6207 Sets PARSER->SCOPE to the class (TYPE) or namespace
6208 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6209 it unchanged if there is no nested-name-specifier. Returns the new
6210 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6212 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6213 part of a declaration and/or decl-specifier. */
6215 static tree
6216 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6217 bool typename_keyword_p,
6218 bool check_dependency_p,
6219 bool type_p,
6220 bool is_declaration,
6221 bool template_keyword_p /* = false */)
6223 bool success = false;
6224 cp_token_position start = 0;
6225 cp_token *token;
6227 /* Remember where the nested-name-specifier starts. */
6228 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6230 start = cp_lexer_token_position (parser->lexer, false);
6231 push_deferring_access_checks (dk_deferred);
6234 while (true)
6236 tree new_scope;
6237 tree old_scope;
6238 tree saved_qualifying_scope;
6240 /* Spot cases that cannot be the beginning of a
6241 nested-name-specifier. */
6242 token = cp_lexer_peek_token (parser->lexer);
6244 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6245 the already parsed nested-name-specifier. */
6246 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6248 /* Grab the nested-name-specifier and continue the loop. */
6249 cp_parser_pre_parsed_nested_name_specifier (parser);
6250 /* If we originally encountered this nested-name-specifier
6251 with IS_DECLARATION set to false, we will not have
6252 resolved TYPENAME_TYPEs, so we must do so here. */
6253 if (is_declaration
6254 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6256 new_scope = resolve_typename_type (parser->scope,
6257 /*only_current_p=*/false);
6258 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6259 parser->scope = new_scope;
6261 success = true;
6262 continue;
6265 /* Spot cases that cannot be the beginning of a
6266 nested-name-specifier. On the second and subsequent times
6267 through the loop, we look for the `template' keyword. */
6268 if (success && token->keyword == RID_TEMPLATE)
6270 /* A template-id can start a nested-name-specifier. */
6271 else if (token->type == CPP_TEMPLATE_ID)
6273 /* DR 743: decltype can be used in a nested-name-specifier. */
6274 else if (token_is_decltype (token))
6276 else
6278 /* If the next token is not an identifier, then it is
6279 definitely not a type-name or namespace-name. */
6280 if (token->type != CPP_NAME)
6281 break;
6282 /* If the following token is neither a `<' (to begin a
6283 template-id), nor a `::', then we are not looking at a
6284 nested-name-specifier. */
6285 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6287 if (token->type == CPP_COLON
6288 && parser->colon_corrects_to_scope_p
6289 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6291 gcc_rich_location richloc (token->location);
6292 richloc.add_fixit_replace ("::");
6293 error_at (&richloc,
6294 "found %<:%> in nested-name-specifier, "
6295 "expected %<::%>");
6296 token->type = CPP_SCOPE;
6299 if (token->type != CPP_SCOPE
6300 && !cp_parser_nth_token_starts_template_argument_list_p
6301 (parser, 2))
6302 break;
6305 /* The nested-name-specifier is optional, so we parse
6306 tentatively. */
6307 cp_parser_parse_tentatively (parser);
6309 /* Look for the optional `template' keyword, if this isn't the
6310 first time through the loop. */
6311 if (success)
6312 template_keyword_p = cp_parser_optional_template_keyword (parser);
6314 /* Save the old scope since the name lookup we are about to do
6315 might destroy it. */
6316 old_scope = parser->scope;
6317 saved_qualifying_scope = parser->qualifying_scope;
6318 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6319 look up names in "X<T>::I" in order to determine that "Y" is
6320 a template. So, if we have a typename at this point, we make
6321 an effort to look through it. */
6322 if (is_declaration
6323 && !typename_keyword_p
6324 && parser->scope
6325 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6326 parser->scope = resolve_typename_type (parser->scope,
6327 /*only_current_p=*/false);
6328 /* Parse the qualifying entity. */
6329 new_scope
6330 = cp_parser_qualifying_entity (parser,
6331 typename_keyword_p,
6332 template_keyword_p,
6333 check_dependency_p,
6334 type_p,
6335 is_declaration);
6336 /* Look for the `::' token. */
6337 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6339 /* If we found what we wanted, we keep going; otherwise, we're
6340 done. */
6341 if (!cp_parser_parse_definitely (parser))
6343 bool error_p = false;
6345 /* Restore the OLD_SCOPE since it was valid before the
6346 failed attempt at finding the last
6347 class-or-namespace-name. */
6348 parser->scope = old_scope;
6349 parser->qualifying_scope = saved_qualifying_scope;
6351 /* If the next token is a decltype, and the one after that is a
6352 `::', then the decltype has failed to resolve to a class or
6353 enumeration type. Give this error even when parsing
6354 tentatively since it can't possibly be valid--and we're going
6355 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6356 won't get another chance.*/
6357 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6358 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6359 == CPP_SCOPE))
6361 token = cp_lexer_consume_token (parser->lexer);
6362 error_at (token->location, "decltype evaluates to %qT, "
6363 "which is not a class or enumeration type",
6364 token->u.tree_check_value->value);
6365 parser->scope = error_mark_node;
6366 error_p = true;
6367 /* As below. */
6368 success = true;
6369 cp_lexer_consume_token (parser->lexer);
6372 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6373 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6375 /* If we have a non-type template-id followed by ::, it can't
6376 possibly be valid. */
6377 token = cp_lexer_peek_token (parser->lexer);
6378 tree tid = token->u.tree_check_value->value;
6379 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6380 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6382 tree tmpl = NULL_TREE;
6383 if (is_overloaded_fn (tid))
6385 tree fns = get_fns (tid);
6386 if (OVL_SINGLE_P (fns))
6387 tmpl = OVL_FIRST (fns);
6388 error_at (token->location, "function template-id %qD "
6389 "in nested-name-specifier", tid);
6391 else
6393 /* Variable template. */
6394 tmpl = TREE_OPERAND (tid, 0);
6395 gcc_assert (variable_template_p (tmpl));
6396 error_at (token->location, "variable template-id %qD "
6397 "in nested-name-specifier", tid);
6399 if (tmpl)
6400 inform (DECL_SOURCE_LOCATION (tmpl),
6401 "%qD declared here", tmpl);
6403 parser->scope = error_mark_node;
6404 error_p = true;
6405 /* As below. */
6406 success = true;
6407 cp_lexer_consume_token (parser->lexer);
6408 cp_lexer_consume_token (parser->lexer);
6412 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6413 break;
6414 /* If the next token is an identifier, and the one after
6415 that is a `::', then any valid interpretation would have
6416 found a class-or-namespace-name. */
6417 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6418 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6419 == CPP_SCOPE)
6420 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6421 != CPP_COMPL))
6423 token = cp_lexer_consume_token (parser->lexer);
6424 if (!error_p)
6426 if (!token->error_reported)
6428 tree decl;
6429 tree ambiguous_decls;
6431 decl = cp_parser_lookup_name (parser, token->u.value,
6432 none_type,
6433 /*is_template=*/false,
6434 /*is_namespace=*/false,
6435 /*check_dependency=*/true,
6436 &ambiguous_decls,
6437 token->location);
6438 if (TREE_CODE (decl) == TEMPLATE_DECL)
6439 error_at (token->location,
6440 "%qD used without template arguments",
6441 decl);
6442 else if (ambiguous_decls)
6444 // cp_parser_lookup_name has the same diagnostic,
6445 // thus make sure to emit it at most once.
6446 if (cp_parser_uncommitted_to_tentative_parse_p
6447 (parser))
6449 error_at (token->location,
6450 "reference to %qD is ambiguous",
6451 token->u.value);
6452 print_candidates (ambiguous_decls);
6454 decl = error_mark_node;
6456 else
6458 if (cxx_dialect != cxx98)
6459 cp_parser_name_lookup_error
6460 (parser, token->u.value, decl, NLE_NOT_CXX98,
6461 token->location);
6462 else
6463 cp_parser_name_lookup_error
6464 (parser, token->u.value, decl, NLE_CXX98,
6465 token->location);
6468 parser->scope = error_mark_node;
6469 error_p = true;
6470 /* Treat this as a successful nested-name-specifier
6471 due to:
6473 [basic.lookup.qual]
6475 If the name found is not a class-name (clause
6476 _class_) or namespace-name (_namespace.def_), the
6477 program is ill-formed. */
6478 success = true;
6480 cp_lexer_consume_token (parser->lexer);
6482 break;
6484 /* We've found one valid nested-name-specifier. */
6485 success = true;
6486 /* Name lookup always gives us a DECL. */
6487 if (TREE_CODE (new_scope) == TYPE_DECL)
6488 new_scope = TREE_TYPE (new_scope);
6489 /* Uses of "template" must be followed by actual templates. */
6490 if (template_keyword_p
6491 && !(CLASS_TYPE_P (new_scope)
6492 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6493 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6494 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6495 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6496 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6497 == TEMPLATE_ID_EXPR)))
6498 permerror (input_location, TYPE_P (new_scope)
6499 ? G_("%qT is not a template")
6500 : G_("%qD is not a template"),
6501 new_scope);
6502 /* If it is a class scope, try to complete it; we are about to
6503 be looking up names inside the class. */
6504 if (TYPE_P (new_scope)
6505 /* Since checking types for dependency can be expensive,
6506 avoid doing it if the type is already complete. */
6507 && !COMPLETE_TYPE_P (new_scope)
6508 /* Do not try to complete dependent types. */
6509 && !dependent_type_p (new_scope))
6511 new_scope = complete_type (new_scope);
6512 /* If it is a typedef to current class, use the current
6513 class instead, as the typedef won't have any names inside
6514 it yet. */
6515 if (!COMPLETE_TYPE_P (new_scope)
6516 && currently_open_class (new_scope))
6517 new_scope = TYPE_MAIN_VARIANT (new_scope);
6519 /* Make sure we look in the right scope the next time through
6520 the loop. */
6521 parser->scope = new_scope;
6524 /* If parsing tentatively, replace the sequence of tokens that makes
6525 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6526 token. That way, should we re-parse the token stream, we will
6527 not have to repeat the effort required to do the parse, nor will
6528 we issue duplicate error messages. */
6529 if (success && start)
6531 cp_token *token;
6533 token = cp_lexer_token_at (parser->lexer, start);
6534 /* Reset the contents of the START token. */
6535 token->type = CPP_NESTED_NAME_SPECIFIER;
6536 /* Retrieve any deferred checks. Do not pop this access checks yet
6537 so the memory will not be reclaimed during token replacing below. */
6538 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6539 token->u.tree_check_value->value = parser->scope;
6540 token->u.tree_check_value->checks = get_deferred_access_checks ();
6541 token->u.tree_check_value->qualifying_scope =
6542 parser->qualifying_scope;
6543 token->keyword = RID_MAX;
6545 /* Purge all subsequent tokens. */
6546 cp_lexer_purge_tokens_after (parser->lexer, start);
6549 if (start)
6550 pop_to_parent_deferring_access_checks ();
6552 return success ? parser->scope : NULL_TREE;
6555 /* Parse a nested-name-specifier. See
6556 cp_parser_nested_name_specifier_opt for details. This function
6557 behaves identically, except that it will an issue an error if no
6558 nested-name-specifier is present. */
6560 static tree
6561 cp_parser_nested_name_specifier (cp_parser *parser,
6562 bool typename_keyword_p,
6563 bool check_dependency_p,
6564 bool type_p,
6565 bool is_declaration)
6567 tree scope;
6569 /* Look for the nested-name-specifier. */
6570 scope = cp_parser_nested_name_specifier_opt (parser,
6571 typename_keyword_p,
6572 check_dependency_p,
6573 type_p,
6574 is_declaration);
6575 /* If it was not present, issue an error message. */
6576 if (!scope)
6578 cp_parser_error (parser, "expected nested-name-specifier");
6579 parser->scope = NULL_TREE;
6582 return scope;
6585 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6586 this is either a class-name or a namespace-name (which corresponds
6587 to the class-or-namespace-name production in the grammar). For
6588 C++0x, it can also be a type-name that refers to an enumeration
6589 type or a simple-template-id.
6591 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6592 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6593 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6594 TYPE_P is TRUE iff the next name should be taken as a class-name,
6595 even the same name is declared to be another entity in the same
6596 scope.
6598 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6599 specified by the class-or-namespace-name. If neither is found the
6600 ERROR_MARK_NODE is returned. */
6602 static tree
6603 cp_parser_qualifying_entity (cp_parser *parser,
6604 bool typename_keyword_p,
6605 bool template_keyword_p,
6606 bool check_dependency_p,
6607 bool type_p,
6608 bool is_declaration)
6610 tree saved_scope;
6611 tree saved_qualifying_scope;
6612 tree saved_object_scope;
6613 tree scope;
6614 bool only_class_p;
6615 bool successful_parse_p;
6617 /* DR 743: decltype can appear in a nested-name-specifier. */
6618 if (cp_lexer_next_token_is_decltype (parser->lexer))
6620 scope = cp_parser_decltype (parser);
6621 if (TREE_CODE (scope) != ENUMERAL_TYPE
6622 && !MAYBE_CLASS_TYPE_P (scope))
6624 cp_parser_simulate_error (parser);
6625 return error_mark_node;
6627 if (TYPE_NAME (scope))
6628 scope = TYPE_NAME (scope);
6629 return scope;
6632 /* Before we try to parse the class-name, we must save away the
6633 current PARSER->SCOPE since cp_parser_class_name will destroy
6634 it. */
6635 saved_scope = parser->scope;
6636 saved_qualifying_scope = parser->qualifying_scope;
6637 saved_object_scope = parser->object_scope;
6638 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6639 there is no need to look for a namespace-name. */
6640 only_class_p = template_keyword_p
6641 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6642 if (!only_class_p)
6643 cp_parser_parse_tentatively (parser);
6644 scope = cp_parser_class_name (parser,
6645 typename_keyword_p,
6646 template_keyword_p,
6647 type_p ? class_type : none_type,
6648 check_dependency_p,
6649 /*class_head_p=*/false,
6650 is_declaration,
6651 /*enum_ok=*/cxx_dialect > cxx98);
6652 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6653 /* If that didn't work, try for a namespace-name. */
6654 if (!only_class_p && !successful_parse_p)
6656 /* Restore the saved scope. */
6657 parser->scope = saved_scope;
6658 parser->qualifying_scope = saved_qualifying_scope;
6659 parser->object_scope = saved_object_scope;
6660 /* If we are not looking at an identifier followed by the scope
6661 resolution operator, then this is not part of a
6662 nested-name-specifier. (Note that this function is only used
6663 to parse the components of a nested-name-specifier.) */
6664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6665 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6666 return error_mark_node;
6667 scope = cp_parser_namespace_name (parser);
6670 return scope;
6673 /* Return true if we are looking at a compound-literal, false otherwise. */
6675 static bool
6676 cp_parser_compound_literal_p (cp_parser *parser)
6678 cp_lexer_save_tokens (parser->lexer);
6680 /* Skip tokens until the next token is a closing parenthesis.
6681 If we find the closing `)', and the next token is a `{', then
6682 we are looking at a compound-literal. */
6683 bool compound_literal_p
6684 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6685 /*consume_paren=*/true)
6686 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6688 /* Roll back the tokens we skipped. */
6689 cp_lexer_rollback_tokens (parser->lexer);
6691 return compound_literal_p;
6694 /* Return true if EXPR is the integer constant zero or a complex constant
6695 of zero, without any folding, but ignoring location wrappers. */
6697 bool
6698 literal_integer_zerop (const_tree expr)
6700 return (location_wrapper_p (expr)
6701 && integer_zerop (TREE_OPERAND (expr, 0)));
6704 /* Parse a postfix-expression.
6706 postfix-expression:
6707 primary-expression
6708 postfix-expression [ expression ]
6709 postfix-expression ( expression-list [opt] )
6710 simple-type-specifier ( expression-list [opt] )
6711 typename :: [opt] nested-name-specifier identifier
6712 ( expression-list [opt] )
6713 typename :: [opt] nested-name-specifier template [opt] template-id
6714 ( expression-list [opt] )
6715 postfix-expression . template [opt] id-expression
6716 postfix-expression -> template [opt] id-expression
6717 postfix-expression . pseudo-destructor-name
6718 postfix-expression -> pseudo-destructor-name
6719 postfix-expression ++
6720 postfix-expression --
6721 dynamic_cast < type-id > ( expression )
6722 static_cast < type-id > ( expression )
6723 reinterpret_cast < type-id > ( expression )
6724 const_cast < type-id > ( expression )
6725 typeid ( expression )
6726 typeid ( type-id )
6728 GNU Extension:
6730 postfix-expression:
6731 ( type-id ) { initializer-list , [opt] }
6733 This extension is a GNU version of the C99 compound-literal
6734 construct. (The C99 grammar uses `type-name' instead of `type-id',
6735 but they are essentially the same concept.)
6737 If ADDRESS_P is true, the postfix expression is the operand of the
6738 `&' operator. CAST_P is true if this expression is the target of a
6739 cast.
6741 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6742 class member access expressions [expr.ref].
6744 Returns a representation of the expression. */
6746 static cp_expr
6747 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6748 bool member_access_only_p, bool decltype_p,
6749 cp_id_kind * pidk_return)
6751 cp_token *token;
6752 location_t loc;
6753 enum rid keyword;
6754 cp_id_kind idk = CP_ID_KIND_NONE;
6755 cp_expr postfix_expression = NULL_TREE;
6756 bool is_member_access = false;
6758 /* Peek at the next token. */
6759 token = cp_lexer_peek_token (parser->lexer);
6760 loc = token->location;
6761 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6763 /* Some of the productions are determined by keywords. */
6764 keyword = token->keyword;
6765 switch (keyword)
6767 case RID_DYNCAST:
6768 case RID_STATCAST:
6769 case RID_REINTCAST:
6770 case RID_CONSTCAST:
6772 tree type;
6773 cp_expr expression;
6774 const char *saved_message;
6775 bool saved_in_type_id_in_expr_p;
6777 /* All of these can be handled in the same way from the point
6778 of view of parsing. Begin by consuming the token
6779 identifying the cast. */
6780 cp_lexer_consume_token (parser->lexer);
6782 /* New types cannot be defined in the cast. */
6783 saved_message = parser->type_definition_forbidden_message;
6784 parser->type_definition_forbidden_message
6785 = G_("types may not be defined in casts");
6787 /* Look for the opening `<'. */
6788 cp_parser_require (parser, CPP_LESS, RT_LESS);
6789 /* Parse the type to which we are casting. */
6790 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6791 parser->in_type_id_in_expr_p = true;
6792 type = cp_parser_type_id (parser);
6793 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6794 /* Look for the closing `>'. */
6795 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6796 /* Restore the old message. */
6797 parser->type_definition_forbidden_message = saved_message;
6799 bool saved_greater_than_is_operator_p
6800 = parser->greater_than_is_operator_p;
6801 parser->greater_than_is_operator_p = true;
6803 /* And the expression which is being cast. */
6804 matching_parens parens;
6805 parens.require_open (parser);
6806 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6807 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6808 RT_CLOSE_PAREN);
6809 location_t end_loc = close_paren ?
6810 close_paren->location : UNKNOWN_LOCATION;
6812 parser->greater_than_is_operator_p
6813 = saved_greater_than_is_operator_p;
6815 /* Only type conversions to integral or enumeration types
6816 can be used in constant-expressions. */
6817 if (!cast_valid_in_integral_constant_expression_p (type)
6818 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6820 postfix_expression = error_mark_node;
6821 break;
6824 switch (keyword)
6826 case RID_DYNCAST:
6827 postfix_expression
6828 = build_dynamic_cast (type, expression, tf_warning_or_error);
6829 break;
6830 case RID_STATCAST:
6831 postfix_expression
6832 = build_static_cast (type, expression, tf_warning_or_error);
6833 break;
6834 case RID_REINTCAST:
6835 postfix_expression
6836 = build_reinterpret_cast (type, expression,
6837 tf_warning_or_error);
6838 break;
6839 case RID_CONSTCAST:
6840 postfix_expression
6841 = build_const_cast (type, expression, tf_warning_or_error);
6842 break;
6843 default:
6844 gcc_unreachable ();
6847 /* Construct a location e.g. :
6848 reinterpret_cast <int *> (expr)
6849 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6850 ranging from the start of the "*_cast" token to the final closing
6851 paren, with the caret at the start. */
6852 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6853 postfix_expression.set_location (cp_cast_loc);
6855 break;
6857 case RID_TYPEID:
6859 tree type;
6860 const char *saved_message;
6861 bool saved_in_type_id_in_expr_p;
6863 /* Consume the `typeid' token. */
6864 cp_lexer_consume_token (parser->lexer);
6865 /* Look for the `(' token. */
6866 matching_parens parens;
6867 parens.require_open (parser);
6868 /* Types cannot be defined in a `typeid' expression. */
6869 saved_message = parser->type_definition_forbidden_message;
6870 parser->type_definition_forbidden_message
6871 = G_("types may not be defined in a %<typeid%> expression");
6872 /* We can't be sure yet whether we're looking at a type-id or an
6873 expression. */
6874 cp_parser_parse_tentatively (parser);
6875 /* Try a type-id first. */
6876 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6877 parser->in_type_id_in_expr_p = true;
6878 type = cp_parser_type_id (parser);
6879 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6880 /* Look for the `)' token. Otherwise, we can't be sure that
6881 we're not looking at an expression: consider `typeid (int
6882 (3))', for example. */
6883 cp_token *close_paren = parens.require_close (parser);
6884 /* If all went well, simply lookup the type-id. */
6885 if (cp_parser_parse_definitely (parser))
6886 postfix_expression = get_typeid (type, tf_warning_or_error);
6887 /* Otherwise, fall back to the expression variant. */
6888 else
6890 tree expression;
6892 /* Look for an expression. */
6893 expression = cp_parser_expression (parser, & idk);
6894 /* Compute its typeid. */
6895 postfix_expression = build_typeid (expression, tf_warning_or_error);
6896 /* Look for the `)' token. */
6897 close_paren = parens.require_close (parser);
6899 /* Restore the saved message. */
6900 parser->type_definition_forbidden_message = saved_message;
6901 /* `typeid' may not appear in an integral constant expression. */
6902 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6903 postfix_expression = error_mark_node;
6905 /* Construct a location e.g. :
6906 typeid (expr)
6907 ^~~~~~~~~~~~~
6908 ranging from the start of the "typeid" token to the final closing
6909 paren, with the caret at the start. */
6910 if (close_paren)
6912 location_t typeid_loc
6913 = make_location (start_loc, start_loc, close_paren->location);
6914 postfix_expression.set_location (typeid_loc);
6915 postfix_expression.maybe_add_location_wrapper ();
6918 break;
6920 case RID_TYPENAME:
6922 tree type;
6923 /* The syntax permitted here is the same permitted for an
6924 elaborated-type-specifier. */
6925 ++parser->prevent_constrained_type_specifiers;
6926 type = cp_parser_elaborated_type_specifier (parser,
6927 /*is_friend=*/false,
6928 /*is_declaration=*/false);
6929 --parser->prevent_constrained_type_specifiers;
6930 postfix_expression = cp_parser_functional_cast (parser, type);
6932 break;
6934 case RID_ADDRESSOF:
6935 case RID_BUILTIN_SHUFFLE:
6936 case RID_BUILTIN_LAUNDER:
6938 vec<tree, va_gc> *vec;
6939 unsigned int i;
6940 tree p;
6942 cp_lexer_consume_token (parser->lexer);
6943 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6944 /*cast_p=*/false, /*allow_expansion_p=*/true,
6945 /*non_constant_p=*/NULL);
6946 if (vec == NULL)
6948 postfix_expression = error_mark_node;
6949 break;
6952 FOR_EACH_VEC_ELT (*vec, i, p)
6953 mark_exp_read (p);
6955 switch (keyword)
6957 case RID_ADDRESSOF:
6958 if (vec->length () == 1)
6959 postfix_expression
6960 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6961 else
6963 error_at (loc, "wrong number of arguments to "
6964 "%<__builtin_addressof%>");
6965 postfix_expression = error_mark_node;
6967 break;
6969 case RID_BUILTIN_LAUNDER:
6970 if (vec->length () == 1)
6971 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6972 tf_warning_or_error);
6973 else
6975 error_at (loc, "wrong number of arguments to "
6976 "%<__builtin_launder%>");
6977 postfix_expression = error_mark_node;
6979 break;
6981 case RID_BUILTIN_SHUFFLE:
6982 if (vec->length () == 2)
6983 postfix_expression
6984 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6985 (*vec)[1], tf_warning_or_error);
6986 else if (vec->length () == 3)
6987 postfix_expression
6988 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6989 (*vec)[2], tf_warning_or_error);
6990 else
6992 error_at (loc, "wrong number of arguments to "
6993 "%<__builtin_shuffle%>");
6994 postfix_expression = error_mark_node;
6996 break;
6998 default:
6999 gcc_unreachable ();
7001 break;
7004 default:
7006 tree type;
7008 /* If the next thing is a simple-type-specifier, we may be
7009 looking at a functional cast. We could also be looking at
7010 an id-expression. So, we try the functional cast, and if
7011 that doesn't work we fall back to the primary-expression. */
7012 cp_parser_parse_tentatively (parser);
7013 /* Look for the simple-type-specifier. */
7014 ++parser->prevent_constrained_type_specifiers;
7015 type = cp_parser_simple_type_specifier (parser,
7016 /*decl_specs=*/NULL,
7017 CP_PARSER_FLAGS_NONE);
7018 --parser->prevent_constrained_type_specifiers;
7019 /* Parse the cast itself. */
7020 if (!cp_parser_error_occurred (parser))
7021 postfix_expression
7022 = cp_parser_functional_cast (parser, type);
7023 /* If that worked, we're done. */
7024 if (cp_parser_parse_definitely (parser))
7025 break;
7027 /* If the functional-cast didn't work out, try a
7028 compound-literal. */
7029 if (cp_parser_allow_gnu_extensions_p (parser)
7030 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7032 cp_expr initializer = NULL_TREE;
7034 cp_parser_parse_tentatively (parser);
7036 matching_parens parens;
7037 parens.consume_open (parser);
7039 /* Avoid calling cp_parser_type_id pointlessly, see comment
7040 in cp_parser_cast_expression about c++/29234. */
7041 if (!cp_parser_compound_literal_p (parser))
7042 cp_parser_simulate_error (parser);
7043 else
7045 /* Parse the type. */
7046 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7047 parser->in_type_id_in_expr_p = true;
7048 type = cp_parser_type_id (parser);
7049 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7050 parens.require_close (parser);
7053 /* If things aren't going well, there's no need to
7054 keep going. */
7055 if (!cp_parser_error_occurred (parser))
7057 bool non_constant_p;
7058 /* Parse the brace-enclosed initializer list. */
7059 initializer = cp_parser_braced_list (parser,
7060 &non_constant_p);
7062 /* If that worked, we're definitely looking at a
7063 compound-literal expression. */
7064 if (cp_parser_parse_definitely (parser))
7066 /* Warn the user that a compound literal is not
7067 allowed in standard C++. */
7068 pedwarn (input_location, OPT_Wpedantic,
7069 "ISO C++ forbids compound-literals");
7070 /* For simplicity, we disallow compound literals in
7071 constant-expressions. We could
7072 allow compound literals of integer type, whose
7073 initializer was a constant, in constant
7074 expressions. Permitting that usage, as a further
7075 extension, would not change the meaning of any
7076 currently accepted programs. (Of course, as
7077 compound literals are not part of ISO C++, the
7078 standard has nothing to say.) */
7079 if (cp_parser_non_integral_constant_expression (parser,
7080 NIC_NCC))
7082 postfix_expression = error_mark_node;
7083 break;
7085 /* Form the representation of the compound-literal. */
7086 postfix_expression
7087 = finish_compound_literal (type, initializer,
7088 tf_warning_or_error, fcl_c99);
7089 postfix_expression.set_location (initializer.get_location ());
7090 break;
7094 /* It must be a primary-expression. */
7095 postfix_expression
7096 = cp_parser_primary_expression (parser, address_p, cast_p,
7097 /*template_arg_p=*/false,
7098 decltype_p,
7099 &idk);
7101 break;
7104 /* Note that we don't need to worry about calling build_cplus_new on a
7105 class-valued CALL_EXPR in decltype when it isn't the end of the
7106 postfix-expression; unary_complex_lvalue will take care of that for
7107 all these cases. */
7109 /* Keep looping until the postfix-expression is complete. */
7110 while (true)
7112 if (idk == CP_ID_KIND_UNQUALIFIED
7113 && identifier_p (postfix_expression)
7114 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7115 /* It is not a Koenig lookup function call. */
7116 postfix_expression
7117 = unqualified_name_lookup_error (postfix_expression);
7119 /* Peek at the next token. */
7120 token = cp_lexer_peek_token (parser->lexer);
7122 switch (token->type)
7124 case CPP_OPEN_SQUARE:
7125 if (cp_next_tokens_can_be_std_attribute_p (parser))
7127 cp_parser_error (parser,
7128 "two consecutive %<[%> shall "
7129 "only introduce an attribute");
7130 return error_mark_node;
7132 postfix_expression
7133 = cp_parser_postfix_open_square_expression (parser,
7134 postfix_expression,
7135 false,
7136 decltype_p);
7137 postfix_expression.set_range (start_loc,
7138 postfix_expression.get_location ());
7140 idk = CP_ID_KIND_NONE;
7141 is_member_access = false;
7142 break;
7144 case CPP_OPEN_PAREN:
7145 /* postfix-expression ( expression-list [opt] ) */
7147 bool koenig_p;
7148 bool is_builtin_constant_p;
7149 bool saved_integral_constant_expression_p = false;
7150 bool saved_non_integral_constant_expression_p = false;
7151 tsubst_flags_t complain = complain_flags (decltype_p);
7152 vec<tree, va_gc> *args;
7153 location_t close_paren_loc = UNKNOWN_LOCATION;
7155 is_member_access = false;
7157 is_builtin_constant_p
7158 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7159 if (is_builtin_constant_p)
7161 /* The whole point of __builtin_constant_p is to allow
7162 non-constant expressions to appear as arguments. */
7163 saved_integral_constant_expression_p
7164 = parser->integral_constant_expression_p;
7165 saved_non_integral_constant_expression_p
7166 = parser->non_integral_constant_expression_p;
7167 parser->integral_constant_expression_p = false;
7169 args = (cp_parser_parenthesized_expression_list
7170 (parser, non_attr,
7171 /*cast_p=*/false, /*allow_expansion_p=*/true,
7172 /*non_constant_p=*/NULL,
7173 /*close_paren_loc=*/&close_paren_loc,
7174 /*wrap_locations_p=*/true));
7175 if (is_builtin_constant_p)
7177 parser->integral_constant_expression_p
7178 = saved_integral_constant_expression_p;
7179 parser->non_integral_constant_expression_p
7180 = saved_non_integral_constant_expression_p;
7183 if (args == NULL)
7185 postfix_expression = error_mark_node;
7186 break;
7189 /* Function calls are not permitted in
7190 constant-expressions. */
7191 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7192 && cp_parser_non_integral_constant_expression (parser,
7193 NIC_FUNC_CALL))
7195 postfix_expression = error_mark_node;
7196 release_tree_vector (args);
7197 break;
7200 koenig_p = false;
7201 if (idk == CP_ID_KIND_UNQUALIFIED
7202 || idk == CP_ID_KIND_TEMPLATE_ID)
7204 if (identifier_p (postfix_expression)
7205 /* In C++2A, we may need to perform ADL for a template
7206 name. */
7207 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7208 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7210 if (!args->is_empty ())
7212 koenig_p = true;
7213 if (!any_type_dependent_arguments_p (args))
7214 postfix_expression
7215 = perform_koenig_lookup (postfix_expression, args,
7216 complain);
7218 else
7219 postfix_expression
7220 = unqualified_fn_lookup_error (postfix_expression);
7222 /* We do not perform argument-dependent lookup if
7223 normal lookup finds a non-function, in accordance
7224 with the expected resolution of DR 218. */
7225 else if (!args->is_empty ()
7226 && is_overloaded_fn (postfix_expression))
7228 tree fn = get_first_fn (postfix_expression);
7229 fn = STRIP_TEMPLATE (fn);
7231 /* Do not do argument dependent lookup if regular
7232 lookup finds a member function or a block-scope
7233 function declaration. [basic.lookup.argdep]/3 */
7234 if (!DECL_FUNCTION_MEMBER_P (fn)
7235 && !DECL_LOCAL_FUNCTION_P (fn))
7237 koenig_p = true;
7238 if (!any_type_dependent_arguments_p (args))
7239 postfix_expression
7240 = perform_koenig_lookup (postfix_expression, args,
7241 complain);
7246 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7248 tree instance = TREE_OPERAND (postfix_expression, 0);
7249 tree fn = TREE_OPERAND (postfix_expression, 1);
7251 if (processing_template_decl
7252 && (type_dependent_object_expression_p (instance)
7253 || (!BASELINK_P (fn)
7254 && TREE_CODE (fn) != FIELD_DECL)
7255 || type_dependent_expression_p (fn)
7256 || any_type_dependent_arguments_p (args)))
7258 maybe_generic_this_capture (instance, fn);
7259 postfix_expression
7260 = build_min_nt_call_vec (postfix_expression, args);
7261 release_tree_vector (args);
7262 break;
7265 if (BASELINK_P (fn))
7267 postfix_expression
7268 = (build_new_method_call
7269 (instance, fn, &args, NULL_TREE,
7270 (idk == CP_ID_KIND_QUALIFIED
7271 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7272 : LOOKUP_NORMAL),
7273 /*fn_p=*/NULL,
7274 complain));
7276 else
7277 postfix_expression
7278 = finish_call_expr (postfix_expression, &args,
7279 /*disallow_virtual=*/false,
7280 /*koenig_p=*/false,
7281 complain);
7283 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7284 || TREE_CODE (postfix_expression) == MEMBER_REF
7285 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7286 postfix_expression = (build_offset_ref_call_from_tree
7287 (postfix_expression, &args,
7288 complain));
7289 else if (idk == CP_ID_KIND_QUALIFIED)
7290 /* A call to a static class member, or a namespace-scope
7291 function. */
7292 postfix_expression
7293 = finish_call_expr (postfix_expression, &args,
7294 /*disallow_virtual=*/true,
7295 koenig_p,
7296 complain);
7297 else
7298 /* All other function calls. */
7299 postfix_expression
7300 = finish_call_expr (postfix_expression, &args,
7301 /*disallow_virtual=*/false,
7302 koenig_p,
7303 complain);
7305 if (close_paren_loc != UNKNOWN_LOCATION)
7307 location_t combined_loc = make_location (token->location,
7308 start_loc,
7309 close_paren_loc);
7310 postfix_expression.set_location (combined_loc);
7313 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7314 idk = CP_ID_KIND_NONE;
7316 release_tree_vector (args);
7318 break;
7320 case CPP_DOT:
7321 case CPP_DEREF:
7322 /* postfix-expression . template [opt] id-expression
7323 postfix-expression . pseudo-destructor-name
7324 postfix-expression -> template [opt] id-expression
7325 postfix-expression -> pseudo-destructor-name */
7327 /* Consume the `.' or `->' operator. */
7328 cp_lexer_consume_token (parser->lexer);
7330 postfix_expression
7331 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7332 postfix_expression,
7333 false, &idk, loc);
7335 is_member_access = true;
7336 break;
7338 case CPP_PLUS_PLUS:
7339 /* postfix-expression ++ */
7340 /* Consume the `++' token. */
7341 cp_lexer_consume_token (parser->lexer);
7342 /* Generate a representation for the complete expression. */
7343 postfix_expression
7344 = finish_increment_expr (postfix_expression,
7345 POSTINCREMENT_EXPR);
7346 /* Increments may not appear in constant-expressions. */
7347 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7348 postfix_expression = error_mark_node;
7349 idk = CP_ID_KIND_NONE;
7350 is_member_access = false;
7351 break;
7353 case CPP_MINUS_MINUS:
7354 /* postfix-expression -- */
7355 /* Consume the `--' token. */
7356 cp_lexer_consume_token (parser->lexer);
7357 /* Generate a representation for the complete expression. */
7358 postfix_expression
7359 = finish_increment_expr (postfix_expression,
7360 POSTDECREMENT_EXPR);
7361 /* Decrements may not appear in constant-expressions. */
7362 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7363 postfix_expression = error_mark_node;
7364 idk = CP_ID_KIND_NONE;
7365 is_member_access = false;
7366 break;
7368 default:
7369 if (pidk_return != NULL)
7370 * pidk_return = idk;
7371 if (member_access_only_p)
7372 return is_member_access
7373 ? postfix_expression
7374 : cp_expr (error_mark_node);
7375 else
7376 return postfix_expression;
7380 /* We should never get here. */
7381 gcc_unreachable ();
7382 return error_mark_node;
7385 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7386 by cp_parser_builtin_offsetof. We're looking for
7388 postfix-expression [ expression ]
7389 postfix-expression [ braced-init-list ] (C++11)
7391 FOR_OFFSETOF is set if we're being called in that context, which
7392 changes how we deal with integer constant expressions. */
7394 static tree
7395 cp_parser_postfix_open_square_expression (cp_parser *parser,
7396 tree postfix_expression,
7397 bool for_offsetof,
7398 bool decltype_p)
7400 tree index = NULL_TREE;
7401 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7402 bool saved_greater_than_is_operator_p;
7404 /* Consume the `[' token. */
7405 cp_lexer_consume_token (parser->lexer);
7407 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7408 parser->greater_than_is_operator_p = true;
7410 /* Parse the index expression. */
7411 /* ??? For offsetof, there is a question of what to allow here. If
7412 offsetof is not being used in an integral constant expression context,
7413 then we *could* get the right answer by computing the value at runtime.
7414 If we are in an integral constant expression context, then we might
7415 could accept any constant expression; hard to say without analysis.
7416 Rather than open the barn door too wide right away, allow only integer
7417 constant expressions here. */
7418 if (for_offsetof)
7419 index = cp_parser_constant_expression (parser);
7420 else
7422 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7424 bool expr_nonconst_p;
7425 cp_lexer_set_source_position (parser->lexer);
7426 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7427 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7429 else
7430 index = cp_parser_expression (parser);
7433 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7435 /* Look for the closing `]'. */
7436 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7438 /* Build the ARRAY_REF. */
7439 postfix_expression = grok_array_decl (loc, postfix_expression,
7440 index, decltype_p);
7442 /* When not doing offsetof, array references are not permitted in
7443 constant-expressions. */
7444 if (!for_offsetof
7445 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7446 postfix_expression = error_mark_node;
7448 return postfix_expression;
7451 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7452 dereference of incomplete type, returns true if error_mark_node should
7453 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7454 and *DEPENDENT_P. */
7456 bool
7457 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7458 bool *dependent_p)
7460 /* In a template, be permissive by treating an object expression
7461 of incomplete type as dependent (after a pedwarn). */
7462 diagnostic_t kind = (processing_template_decl
7463 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7465 switch (TREE_CODE (*postfix_expression))
7467 case CAST_EXPR:
7468 case REINTERPRET_CAST_EXPR:
7469 case CONST_CAST_EXPR:
7470 case STATIC_CAST_EXPR:
7471 case DYNAMIC_CAST_EXPR:
7472 case IMPLICIT_CONV_EXPR:
7473 case VIEW_CONVERT_EXPR:
7474 case NON_LVALUE_EXPR:
7475 kind = DK_ERROR;
7476 break;
7477 case OVERLOAD:
7478 /* Don't emit any diagnostic for OVERLOADs. */
7479 kind = DK_IGNORED;
7480 break;
7481 default:
7482 /* Avoid clobbering e.g. DECLs. */
7483 if (!EXPR_P (*postfix_expression))
7484 kind = DK_ERROR;
7485 break;
7488 if (kind == DK_IGNORED)
7489 return false;
7491 location_t exploc = location_of (*postfix_expression);
7492 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7493 if (!MAYBE_CLASS_TYPE_P (*scope))
7494 return true;
7495 if (kind == DK_ERROR)
7496 *scope = *postfix_expression = error_mark_node;
7497 else if (processing_template_decl)
7499 *dependent_p = true;
7500 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7502 return false;
7505 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7506 by cp_parser_builtin_offsetof. We're looking for
7508 postfix-expression . template [opt] id-expression
7509 postfix-expression . pseudo-destructor-name
7510 postfix-expression -> template [opt] id-expression
7511 postfix-expression -> pseudo-destructor-name
7513 FOR_OFFSETOF is set if we're being called in that context. That sorta
7514 limits what of the above we'll actually accept, but nevermind.
7515 TOKEN_TYPE is the "." or "->" token, which will already have been
7516 removed from the stream. */
7518 static tree
7519 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7520 enum cpp_ttype token_type,
7521 cp_expr postfix_expression,
7522 bool for_offsetof, cp_id_kind *idk,
7523 location_t location)
7525 tree name;
7526 bool dependent_p;
7527 bool pseudo_destructor_p;
7528 tree scope = NULL_TREE;
7529 location_t start_loc = postfix_expression.get_start ();
7531 /* If this is a `->' operator, dereference the pointer. */
7532 if (token_type == CPP_DEREF)
7533 postfix_expression = build_x_arrow (location, postfix_expression,
7534 tf_warning_or_error);
7535 /* Check to see whether or not the expression is type-dependent and
7536 not the current instantiation. */
7537 dependent_p = type_dependent_object_expression_p (postfix_expression);
7538 /* The identifier following the `->' or `.' is not qualified. */
7539 parser->scope = NULL_TREE;
7540 parser->qualifying_scope = NULL_TREE;
7541 parser->object_scope = NULL_TREE;
7542 *idk = CP_ID_KIND_NONE;
7544 /* Enter the scope corresponding to the type of the object
7545 given by the POSTFIX_EXPRESSION. */
7546 if (!dependent_p)
7548 scope = TREE_TYPE (postfix_expression);
7549 /* According to the standard, no expression should ever have
7550 reference type. Unfortunately, we do not currently match
7551 the standard in this respect in that our internal representation
7552 of an expression may have reference type even when the standard
7553 says it does not. Therefore, we have to manually obtain the
7554 underlying type here. */
7555 scope = non_reference (scope);
7556 /* The type of the POSTFIX_EXPRESSION must be complete. */
7557 /* Unlike the object expression in other contexts, *this is not
7558 required to be of complete type for purposes of class member
7559 access (5.2.5) outside the member function body. */
7560 if (postfix_expression != current_class_ref
7561 && scope != error_mark_node
7562 && !currently_open_class (scope))
7564 scope = complete_type (scope);
7565 if (!COMPLETE_TYPE_P (scope)
7566 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7567 &dependent_p))
7568 return error_mark_node;
7571 if (!dependent_p)
7573 /* Let the name lookup machinery know that we are processing a
7574 class member access expression. */
7575 parser->context->object_type = scope;
7576 /* If something went wrong, we want to be able to discern that case,
7577 as opposed to the case where there was no SCOPE due to the type
7578 of expression being dependent. */
7579 if (!scope)
7580 scope = error_mark_node;
7581 /* If the SCOPE was erroneous, make the various semantic analysis
7582 functions exit quickly -- and without issuing additional error
7583 messages. */
7584 if (scope == error_mark_node)
7585 postfix_expression = error_mark_node;
7589 if (dependent_p)
7590 /* Tell cp_parser_lookup_name that there was an object, even though it's
7591 type-dependent. */
7592 parser->context->object_type = unknown_type_node;
7594 /* Assume this expression is not a pseudo-destructor access. */
7595 pseudo_destructor_p = false;
7597 /* If the SCOPE is a scalar type, then, if this is a valid program,
7598 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7599 is type dependent, it can be pseudo-destructor-name or something else.
7600 Try to parse it as pseudo-destructor-name first. */
7601 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7603 tree s;
7604 tree type;
7606 cp_parser_parse_tentatively (parser);
7607 /* Parse the pseudo-destructor-name. */
7608 s = NULL_TREE;
7609 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7610 &s, &type);
7611 if (dependent_p
7612 && (cp_parser_error_occurred (parser)
7613 || !SCALAR_TYPE_P (type)))
7614 cp_parser_abort_tentative_parse (parser);
7615 else if (cp_parser_parse_definitely (parser))
7617 pseudo_destructor_p = true;
7618 postfix_expression
7619 = finish_pseudo_destructor_expr (postfix_expression,
7620 s, type, location);
7624 if (!pseudo_destructor_p)
7626 /* If the SCOPE is not a scalar type, we are looking at an
7627 ordinary class member access expression, rather than a
7628 pseudo-destructor-name. */
7629 bool template_p;
7630 cp_token *token = cp_lexer_peek_token (parser->lexer);
7631 /* Parse the id-expression. */
7632 name = (cp_parser_id_expression
7633 (parser,
7634 cp_parser_optional_template_keyword (parser),
7635 /*check_dependency_p=*/true,
7636 &template_p,
7637 /*declarator_p=*/false,
7638 /*optional_p=*/false));
7639 /* In general, build a SCOPE_REF if the member name is qualified.
7640 However, if the name was not dependent and has already been
7641 resolved; there is no need to build the SCOPE_REF. For example;
7643 struct X { void f(); };
7644 template <typename T> void f(T* t) { t->X::f(); }
7646 Even though "t" is dependent, "X::f" is not and has been resolved
7647 to a BASELINK; there is no need to include scope information. */
7649 /* But we do need to remember that there was an explicit scope for
7650 virtual function calls. */
7651 if (parser->scope)
7652 *idk = CP_ID_KIND_QUALIFIED;
7654 /* If the name is a template-id that names a type, we will get a
7655 TYPE_DECL here. That is invalid code. */
7656 if (TREE_CODE (name) == TYPE_DECL)
7658 error_at (token->location, "invalid use of %qD", name);
7659 postfix_expression = error_mark_node;
7661 else
7663 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7665 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7667 error_at (token->location, "%<%D::%D%> is not a class member",
7668 parser->scope, name);
7669 postfix_expression = error_mark_node;
7671 else
7672 name = build_qualified_name (/*type=*/NULL_TREE,
7673 parser->scope,
7674 name,
7675 template_p);
7676 parser->scope = NULL_TREE;
7677 parser->qualifying_scope = NULL_TREE;
7678 parser->object_scope = NULL_TREE;
7680 if (parser->scope && name && BASELINK_P (name))
7681 adjust_result_of_qualified_name_lookup
7682 (name, parser->scope, scope);
7683 postfix_expression
7684 = finish_class_member_access_expr (postfix_expression, name,
7685 template_p,
7686 tf_warning_or_error);
7687 /* Build a location e.g.:
7688 ptr->access_expr
7689 ~~~^~~~~~~~~~~~~
7690 where the caret is at the deref token, ranging from
7691 the start of postfix_expression to the end of the access expr. */
7692 location_t end_loc
7693 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7694 location_t combined_loc
7695 = make_location (input_location, start_loc, end_loc);
7696 protected_set_expr_location (postfix_expression, combined_loc);
7700 /* We no longer need to look up names in the scope of the object on
7701 the left-hand side of the `.' or `->' operator. */
7702 parser->context->object_type = NULL_TREE;
7704 /* Outside of offsetof, these operators may not appear in
7705 constant-expressions. */
7706 if (!for_offsetof
7707 && (cp_parser_non_integral_constant_expression
7708 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7709 postfix_expression = error_mark_node;
7711 return postfix_expression;
7714 /* Parse a parenthesized expression-list.
7716 expression-list:
7717 assignment-expression
7718 expression-list, assignment-expression
7720 attribute-list:
7721 expression-list
7722 identifier
7723 identifier, expression-list
7725 CAST_P is true if this expression is the target of a cast.
7727 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7728 argument pack.
7730 WRAP_LOCATIONS_P is true if expressions within this list for which
7731 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7732 their source locations.
7734 Returns a vector of trees. Each element is a representation of an
7735 assignment-expression. NULL is returned if the ( and or ) are
7736 missing. An empty, but allocated, vector is returned on no
7737 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7738 if we are parsing an attribute list for an attribute that wants a
7739 plain identifier argument, normal_attr for an attribute that wants
7740 an expression, or non_attr if we aren't parsing an attribute list. If
7741 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7742 not all of the expressions in the list were constant.
7743 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7744 will be written to with the location of the closing parenthesis. If
7745 an error occurs, it may or may not be written to. */
7747 static vec<tree, va_gc> *
7748 cp_parser_parenthesized_expression_list (cp_parser* parser,
7749 int is_attribute_list,
7750 bool cast_p,
7751 bool allow_expansion_p,
7752 bool *non_constant_p,
7753 location_t *close_paren_loc,
7754 bool wrap_locations_p)
7756 vec<tree, va_gc> *expression_list;
7757 bool fold_expr_p = is_attribute_list != non_attr;
7758 tree identifier = NULL_TREE;
7759 bool saved_greater_than_is_operator_p;
7761 /* Assume all the expressions will be constant. */
7762 if (non_constant_p)
7763 *non_constant_p = false;
7765 matching_parens parens;
7766 if (!parens.require_open (parser))
7767 return NULL;
7769 expression_list = make_tree_vector ();
7771 /* Within a parenthesized expression, a `>' token is always
7772 the greater-than operator. */
7773 saved_greater_than_is_operator_p
7774 = parser->greater_than_is_operator_p;
7775 parser->greater_than_is_operator_p = true;
7777 cp_expr expr (NULL_TREE);
7779 /* Consume expressions until there are no more. */
7780 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7781 while (true)
7783 /* At the beginning of attribute lists, check to see if the
7784 next token is an identifier. */
7785 if (is_attribute_list == id_attr
7786 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7788 cp_token *token;
7790 /* Consume the identifier. */
7791 token = cp_lexer_consume_token (parser->lexer);
7792 /* Save the identifier. */
7793 identifier = token->u.value;
7795 else
7797 bool expr_non_constant_p;
7799 /* Parse the next assignment-expression. */
7800 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7802 /* A braced-init-list. */
7803 cp_lexer_set_source_position (parser->lexer);
7804 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7805 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7806 if (non_constant_p && expr_non_constant_p)
7807 *non_constant_p = true;
7809 else if (non_constant_p)
7811 expr = (cp_parser_constant_expression
7812 (parser, /*allow_non_constant_p=*/true,
7813 &expr_non_constant_p));
7814 if (expr_non_constant_p)
7815 *non_constant_p = true;
7817 else
7818 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7819 cast_p);
7821 if (fold_expr_p)
7822 expr = instantiate_non_dependent_expr (expr);
7824 /* If we have an ellipsis, then this is an expression
7825 expansion. */
7826 if (allow_expansion_p
7827 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7829 /* Consume the `...'. */
7830 cp_lexer_consume_token (parser->lexer);
7832 /* Build the argument pack. */
7833 expr = make_pack_expansion (expr);
7836 if (wrap_locations_p)
7837 expr.maybe_add_location_wrapper ();
7839 /* Add it to the list. We add error_mark_node
7840 expressions to the list, so that we can still tell if
7841 the correct form for a parenthesized expression-list
7842 is found. That gives better errors. */
7843 vec_safe_push (expression_list, expr.get_value ());
7845 if (expr == error_mark_node)
7846 goto skip_comma;
7849 /* After the first item, attribute lists look the same as
7850 expression lists. */
7851 is_attribute_list = non_attr;
7853 get_comma:;
7854 /* If the next token isn't a `,', then we are done. */
7855 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7856 break;
7858 /* Otherwise, consume the `,' and keep going. */
7859 cp_lexer_consume_token (parser->lexer);
7862 if (close_paren_loc)
7863 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7865 if (!parens.require_close (parser))
7867 int ending;
7869 skip_comma:;
7870 /* We try and resync to an unnested comma, as that will give the
7871 user better diagnostics. */
7872 ending = cp_parser_skip_to_closing_parenthesis (parser,
7873 /*recovering=*/true,
7874 /*or_comma=*/true,
7875 /*consume_paren=*/true);
7876 if (ending < 0)
7877 goto get_comma;
7878 if (!ending)
7880 parser->greater_than_is_operator_p
7881 = saved_greater_than_is_operator_p;
7882 return NULL;
7886 parser->greater_than_is_operator_p
7887 = saved_greater_than_is_operator_p;
7889 if (identifier)
7890 vec_safe_insert (expression_list, 0, identifier);
7892 return expression_list;
7895 /* Parse a pseudo-destructor-name.
7897 pseudo-destructor-name:
7898 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7899 :: [opt] nested-name-specifier template template-id :: ~ type-name
7900 :: [opt] nested-name-specifier [opt] ~ type-name
7902 If either of the first two productions is used, sets *SCOPE to the
7903 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7904 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7905 or ERROR_MARK_NODE if the parse fails. */
7907 static void
7908 cp_parser_pseudo_destructor_name (cp_parser* parser,
7909 tree object,
7910 tree* scope,
7911 tree* type)
7913 bool nested_name_specifier_p;
7915 /* Handle ~auto. */
7916 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7917 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7918 && !type_dependent_expression_p (object))
7920 if (cxx_dialect < cxx14)
7921 pedwarn (input_location, 0,
7922 "%<~auto%> only available with "
7923 "-std=c++14 or -std=gnu++14");
7924 cp_lexer_consume_token (parser->lexer);
7925 cp_lexer_consume_token (parser->lexer);
7926 *scope = NULL_TREE;
7927 *type = TREE_TYPE (object);
7928 return;
7931 /* Assume that things will not work out. */
7932 *type = error_mark_node;
7934 /* Look for the optional `::' operator. */
7935 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7936 /* Look for the optional nested-name-specifier. */
7937 nested_name_specifier_p
7938 = (cp_parser_nested_name_specifier_opt (parser,
7939 /*typename_keyword_p=*/false,
7940 /*check_dependency_p=*/true,
7941 /*type_p=*/false,
7942 /*is_declaration=*/false)
7943 != NULL_TREE);
7944 /* Now, if we saw a nested-name-specifier, we might be doing the
7945 second production. */
7946 if (nested_name_specifier_p
7947 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7949 /* Consume the `template' keyword. */
7950 cp_lexer_consume_token (parser->lexer);
7951 /* Parse the template-id. */
7952 cp_parser_template_id (parser,
7953 /*template_keyword_p=*/true,
7954 /*check_dependency_p=*/false,
7955 class_type,
7956 /*is_declaration=*/true);
7957 /* Look for the `::' token. */
7958 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7960 /* If the next token is not a `~', then there might be some
7961 additional qualification. */
7962 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7964 /* At this point, we're looking for "type-name :: ~". The type-name
7965 must not be a class-name, since this is a pseudo-destructor. So,
7966 it must be either an enum-name, or a typedef-name -- both of which
7967 are just identifiers. So, we peek ahead to check that the "::"
7968 and "~" tokens are present; if they are not, then we can avoid
7969 calling type_name. */
7970 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7971 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7972 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7974 cp_parser_error (parser, "non-scalar type");
7975 return;
7978 /* Look for the type-name. */
7979 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7980 if (*scope == error_mark_node)
7981 return;
7983 /* Look for the `::' token. */
7984 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7986 else
7987 *scope = NULL_TREE;
7989 /* Look for the `~'. */
7990 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7992 /* Once we see the ~, this has to be a pseudo-destructor. */
7993 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7994 cp_parser_commit_to_topmost_tentative_parse (parser);
7996 /* Look for the type-name again. We are not responsible for
7997 checking that it matches the first type-name. */
7998 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8001 /* Parse a unary-expression.
8003 unary-expression:
8004 postfix-expression
8005 ++ cast-expression
8006 -- cast-expression
8007 unary-operator cast-expression
8008 sizeof unary-expression
8009 sizeof ( type-id )
8010 alignof ( type-id ) [C++0x]
8011 new-expression
8012 delete-expression
8014 GNU Extensions:
8016 unary-expression:
8017 __extension__ cast-expression
8018 __alignof__ unary-expression
8019 __alignof__ ( type-id )
8020 alignof unary-expression [C++0x]
8021 __real__ cast-expression
8022 __imag__ cast-expression
8023 && identifier
8024 sizeof ( type-id ) { initializer-list , [opt] }
8025 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8026 __alignof__ ( type-id ) { initializer-list , [opt] }
8028 ADDRESS_P is true iff the unary-expression is appearing as the
8029 operand of the `&' operator. CAST_P is true if this expression is
8030 the target of a cast.
8032 Returns a representation of the expression. */
8034 static cp_expr
8035 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8036 bool address_p, bool cast_p, bool decltype_p)
8038 cp_token *token;
8039 enum tree_code unary_operator;
8041 /* Peek at the next token. */
8042 token = cp_lexer_peek_token (parser->lexer);
8043 /* Some keywords give away the kind of expression. */
8044 if (token->type == CPP_KEYWORD)
8046 enum rid keyword = token->keyword;
8048 switch (keyword)
8050 case RID_ALIGNOF:
8051 case RID_SIZEOF:
8053 tree operand, ret;
8054 enum tree_code op;
8055 location_t start_loc = token->location;
8057 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8058 bool std_alignof = id_equal (token->u.value, "alignof");
8060 /* Consume the token. */
8061 cp_lexer_consume_token (parser->lexer);
8062 /* Parse the operand. */
8063 operand = cp_parser_sizeof_operand (parser, keyword);
8065 if (TYPE_P (operand))
8066 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8067 true);
8068 else
8070 /* ISO C++ defines alignof only with types, not with
8071 expressions. So pedwarn if alignof is used with a non-
8072 type expression. However, __alignof__ is ok. */
8073 if (std_alignof)
8074 pedwarn (token->location, OPT_Wpedantic,
8075 "ISO C++ does not allow %<alignof%> "
8076 "with a non-type");
8078 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8080 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8081 SIZEOF_EXPR with the original operand. */
8082 if (op == SIZEOF_EXPR && ret != error_mark_node)
8084 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8086 if (!processing_template_decl && TYPE_P (operand))
8088 ret = build_min (SIZEOF_EXPR, size_type_node,
8089 build1 (NOP_EXPR, operand,
8090 error_mark_node));
8091 SIZEOF_EXPR_TYPE_P (ret) = 1;
8093 else
8094 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8095 TREE_SIDE_EFFECTS (ret) = 0;
8096 TREE_READONLY (ret) = 1;
8100 /* Construct a location e.g. :
8101 alignof (expr)
8102 ^~~~~~~~~~~~~~
8103 with start == caret at the start of the "alignof"/"sizeof"
8104 token, with the endpoint at the final closing paren. */
8105 location_t finish_loc
8106 = cp_lexer_previous_token (parser->lexer)->location;
8107 location_t compound_loc
8108 = make_location (start_loc, start_loc, finish_loc);
8110 cp_expr ret_expr (ret);
8111 ret_expr.set_location (compound_loc);
8112 ret_expr = ret_expr.maybe_add_location_wrapper ();
8113 return ret_expr;
8116 case RID_BUILTIN_HAS_ATTRIBUTE:
8117 return cp_parser_has_attribute_expression (parser);
8119 case RID_NEW:
8120 return cp_parser_new_expression (parser);
8122 case RID_DELETE:
8123 return cp_parser_delete_expression (parser);
8125 case RID_EXTENSION:
8127 /* The saved value of the PEDANTIC flag. */
8128 int saved_pedantic;
8129 tree expr;
8131 /* Save away the PEDANTIC flag. */
8132 cp_parser_extension_opt (parser, &saved_pedantic);
8133 /* Parse the cast-expression. */
8134 expr = cp_parser_simple_cast_expression (parser);
8135 /* Restore the PEDANTIC flag. */
8136 pedantic = saved_pedantic;
8138 return expr;
8141 case RID_REALPART:
8142 case RID_IMAGPART:
8144 tree expression;
8146 /* Consume the `__real__' or `__imag__' token. */
8147 cp_lexer_consume_token (parser->lexer);
8148 /* Parse the cast-expression. */
8149 expression = cp_parser_simple_cast_expression (parser);
8150 /* Create the complete representation. */
8151 return build_x_unary_op (token->location,
8152 (keyword == RID_REALPART
8153 ? REALPART_EXPR : IMAGPART_EXPR),
8154 expression,
8155 tf_warning_or_error);
8157 break;
8159 case RID_TRANSACTION_ATOMIC:
8160 case RID_TRANSACTION_RELAXED:
8161 return cp_parser_transaction_expression (parser, keyword);
8163 case RID_NOEXCEPT:
8165 tree expr;
8166 const char *saved_message;
8167 bool saved_integral_constant_expression_p;
8168 bool saved_non_integral_constant_expression_p;
8169 bool saved_greater_than_is_operator_p;
8171 location_t start_loc = token->location;
8173 cp_lexer_consume_token (parser->lexer);
8174 matching_parens parens;
8175 parens.require_open (parser);
8177 saved_message = parser->type_definition_forbidden_message;
8178 parser->type_definition_forbidden_message
8179 = G_("types may not be defined in %<noexcept%> expressions");
8181 saved_integral_constant_expression_p
8182 = parser->integral_constant_expression_p;
8183 saved_non_integral_constant_expression_p
8184 = parser->non_integral_constant_expression_p;
8185 parser->integral_constant_expression_p = false;
8187 saved_greater_than_is_operator_p
8188 = parser->greater_than_is_operator_p;
8189 parser->greater_than_is_operator_p = true;
8191 ++cp_unevaluated_operand;
8192 ++c_inhibit_evaluation_warnings;
8193 ++cp_noexcept_operand;
8194 expr = cp_parser_expression (parser);
8195 --cp_noexcept_operand;
8196 --c_inhibit_evaluation_warnings;
8197 --cp_unevaluated_operand;
8199 parser->greater_than_is_operator_p
8200 = saved_greater_than_is_operator_p;
8202 parser->integral_constant_expression_p
8203 = saved_integral_constant_expression_p;
8204 parser->non_integral_constant_expression_p
8205 = saved_non_integral_constant_expression_p;
8207 parser->type_definition_forbidden_message = saved_message;
8209 location_t finish_loc
8210 = cp_lexer_peek_token (parser->lexer)->location;
8211 parens.require_close (parser);
8213 /* Construct a location of the form:
8214 noexcept (expr)
8215 ^~~~~~~~~~~~~~~
8216 with start == caret, finishing at the close-paren. */
8217 location_t noexcept_loc
8218 = make_location (start_loc, start_loc, finish_loc);
8220 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8221 noexcept_loc);
8224 default:
8225 break;
8229 /* Look for the `:: new' and `:: delete', which also signal the
8230 beginning of a new-expression, or delete-expression,
8231 respectively. If the next token is `::', then it might be one of
8232 these. */
8233 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8235 enum rid keyword;
8237 /* See if the token after the `::' is one of the keywords in
8238 which we're interested. */
8239 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8240 /* If it's `new', we have a new-expression. */
8241 if (keyword == RID_NEW)
8242 return cp_parser_new_expression (parser);
8243 /* Similarly, for `delete'. */
8244 else if (keyword == RID_DELETE)
8245 return cp_parser_delete_expression (parser);
8248 /* Look for a unary operator. */
8249 unary_operator = cp_parser_unary_operator (token);
8250 /* The `++' and `--' operators can be handled similarly, even though
8251 they are not technically unary-operators in the grammar. */
8252 if (unary_operator == ERROR_MARK)
8254 if (token->type == CPP_PLUS_PLUS)
8255 unary_operator = PREINCREMENT_EXPR;
8256 else if (token->type == CPP_MINUS_MINUS)
8257 unary_operator = PREDECREMENT_EXPR;
8258 /* Handle the GNU address-of-label extension. */
8259 else if (cp_parser_allow_gnu_extensions_p (parser)
8260 && token->type == CPP_AND_AND)
8262 tree identifier;
8263 tree expression;
8264 location_t start_loc = token->location;
8266 /* Consume the '&&' token. */
8267 cp_lexer_consume_token (parser->lexer);
8268 /* Look for the identifier. */
8269 location_t finish_loc
8270 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8271 identifier = cp_parser_identifier (parser);
8272 /* Construct a location of the form:
8273 &&label
8274 ^~~~~~~
8275 with caret==start at the "&&", finish at the end of the label. */
8276 location_t combined_loc
8277 = make_location (start_loc, start_loc, finish_loc);
8278 /* Create an expression representing the address. */
8279 expression = finish_label_address_expr (identifier, combined_loc);
8280 if (cp_parser_non_integral_constant_expression (parser,
8281 NIC_ADDR_LABEL))
8282 expression = error_mark_node;
8283 return expression;
8286 if (unary_operator != ERROR_MARK)
8288 cp_expr cast_expression;
8289 cp_expr expression = error_mark_node;
8290 non_integral_constant non_constant_p = NIC_NONE;
8291 location_t loc = token->location;
8292 tsubst_flags_t complain = complain_flags (decltype_p);
8294 /* Consume the operator token. */
8295 token = cp_lexer_consume_token (parser->lexer);
8296 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8298 /* Parse the cast-expression. */
8299 cast_expression
8300 = cp_parser_cast_expression (parser,
8301 unary_operator == ADDR_EXPR,
8302 /*cast_p=*/false,
8303 /*decltype*/false,
8304 pidk);
8306 /* Make a location:
8307 OP_TOKEN CAST_EXPRESSION
8308 ^~~~~~~~~~~~~~~~~~~~~~~~~
8309 with start==caret at the operator token, and
8310 extending to the end of the cast_expression. */
8311 loc = make_location (loc, loc, cast_expression.get_finish ());
8313 /* Now, build an appropriate representation. */
8314 switch (unary_operator)
8316 case INDIRECT_REF:
8317 non_constant_p = NIC_STAR;
8318 expression = build_x_indirect_ref (loc, cast_expression,
8319 RO_UNARY_STAR,
8320 complain);
8321 /* TODO: build_x_indirect_ref does not always honor the
8322 location, so ensure it is set. */
8323 expression.set_location (loc);
8324 break;
8326 case ADDR_EXPR:
8327 non_constant_p = NIC_ADDR;
8328 /* Fall through. */
8329 case BIT_NOT_EXPR:
8330 expression = build_x_unary_op (loc, unary_operator,
8331 cast_expression,
8332 complain);
8333 /* TODO: build_x_unary_op does not always honor the location,
8334 so ensure it is set. */
8335 expression.set_location (loc);
8336 break;
8338 case PREINCREMENT_EXPR:
8339 case PREDECREMENT_EXPR:
8340 non_constant_p = unary_operator == PREINCREMENT_EXPR
8341 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8342 /* Fall through. */
8343 case NEGATE_EXPR:
8344 /* Immediately fold negation of a constant, unless the constant is 0
8345 (since -0 == 0) or it would overflow. */
8346 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8347 && CONSTANT_CLASS_P (cast_expression)
8348 && !integer_zerop (cast_expression)
8349 && !TREE_OVERFLOW (cast_expression))
8351 tree folded = fold_build1 (unary_operator,
8352 TREE_TYPE (cast_expression),
8353 cast_expression);
8354 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8356 expression = cp_expr (folded, loc);
8357 break;
8360 /* Fall through. */
8361 case UNARY_PLUS_EXPR:
8362 case TRUTH_NOT_EXPR:
8363 expression = finish_unary_op_expr (loc, unary_operator,
8364 cast_expression, complain);
8365 break;
8367 default:
8368 gcc_unreachable ();
8371 if (non_constant_p != NIC_NONE
8372 && cp_parser_non_integral_constant_expression (parser,
8373 non_constant_p))
8374 expression = error_mark_node;
8376 return expression;
8379 return cp_parser_postfix_expression (parser, address_p, cast_p,
8380 /*member_access_only_p=*/false,
8381 decltype_p,
8382 pidk);
8385 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8386 unary-operator, the corresponding tree code is returned. */
8388 static enum tree_code
8389 cp_parser_unary_operator (cp_token* token)
8391 switch (token->type)
8393 case CPP_MULT:
8394 return INDIRECT_REF;
8396 case CPP_AND:
8397 return ADDR_EXPR;
8399 case CPP_PLUS:
8400 return UNARY_PLUS_EXPR;
8402 case CPP_MINUS:
8403 return NEGATE_EXPR;
8405 case CPP_NOT:
8406 return TRUTH_NOT_EXPR;
8408 case CPP_COMPL:
8409 return BIT_NOT_EXPR;
8411 default:
8412 return ERROR_MARK;
8416 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8417 Returns a representation of the expression. */
8419 static tree
8420 cp_parser_has_attribute_expression (cp_parser *parser)
8422 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8424 /* Consume the __builtin_has_attribute token. */
8425 cp_lexer_consume_token (parser->lexer);
8427 matching_parens parens;
8428 if (!parens.require_open (parser))
8429 return error_mark_node;
8431 /* Types cannot be defined in a `sizeof' expression. Save away the
8432 old message. */
8433 const char *saved_message = parser->type_definition_forbidden_message;
8434 /* And create the new one. */
8435 const int kwd = RID_BUILTIN_HAS_ATTRIBUTE;
8436 char *tmp = concat ("types may not be defined in %<",
8437 IDENTIFIER_POINTER (ridpointers[kwd]),
8438 "%> expressions", NULL);
8439 parser->type_definition_forbidden_message = tmp;
8441 /* The restrictions on constant-expressions do not apply inside
8442 sizeof expressions. */
8443 bool saved_integral_constant_expression_p
8444 = parser->integral_constant_expression_p;
8445 bool saved_non_integral_constant_expression_p
8446 = parser->non_integral_constant_expression_p;
8447 parser->integral_constant_expression_p = false;
8449 /* Do not actually evaluate the expression. */
8450 ++cp_unevaluated_operand;
8451 ++c_inhibit_evaluation_warnings;
8453 tree oper = NULL_TREE;
8455 /* We can't be sure yet whether we're looking at a type-id or an
8456 expression. */
8457 cp_parser_parse_tentatively (parser);
8459 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8460 parser->in_type_id_in_expr_p = true;
8461 /* Look for the type-id. */
8462 oper = cp_parser_type_id (parser);
8463 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8465 cp_parser_parse_definitely (parser);
8467 /* If the type-id production did not work out, then we must be
8468 looking at the unary-expression production. */
8469 if (!oper || oper == error_mark_node)
8470 oper = cp_parser_unary_expression (parser);
8472 /* Go back to evaluating expressions. */
8473 --cp_unevaluated_operand;
8474 --c_inhibit_evaluation_warnings;
8476 /* Free the message we created. */
8477 free (tmp);
8478 /* And restore the old one. */
8479 parser->type_definition_forbidden_message = saved_message;
8480 parser->integral_constant_expression_p
8481 = saved_integral_constant_expression_p;
8482 parser->non_integral_constant_expression_p
8483 = saved_non_integral_constant_expression_p;
8485 /* Consume the comma if it's there. */
8486 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8488 cp_parser_skip_to_closing_parenthesis (parser, false, false,
8489 /*consume_paren=*/true);
8490 return error_mark_node;
8493 /* Parse the attribute specification. */
8494 bool ret = false;
8495 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8496 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8498 if (oper != error_mark_node)
8500 /* Fold constant expressions used in attributes first. */
8501 cp_check_const_attributes (attr);
8503 /* Finally, see if OPER has been declared with ATTR. */
8504 ret = has_attribute (atloc, oper, attr, default_conversion);
8507 parens.require_close (parser);
8509 else
8511 error_at (atloc, "expected identifier");
8512 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8515 /* Construct a location e.g. :
8516 __builtin_has_attribute (oper, attr)
8517 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8518 with start == caret at the start of the built-in token,
8519 and with the endpoint at the final closing paren. */
8520 location_t finish_loc
8521 = cp_lexer_previous_token (parser->lexer)->location;
8522 location_t compound_loc
8523 = make_location (start_loc, start_loc, finish_loc);
8525 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8526 ret_expr.set_location (compound_loc);
8527 ret_expr = ret_expr.maybe_add_location_wrapper ();
8528 return ret_expr;
8531 /* Parse a new-expression.
8533 new-expression:
8534 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8535 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8537 Returns a representation of the expression. */
8539 static tree
8540 cp_parser_new_expression (cp_parser* parser)
8542 bool global_scope_p;
8543 vec<tree, va_gc> *placement;
8544 tree type;
8545 vec<tree, va_gc> *initializer;
8546 tree nelts = NULL_TREE;
8547 tree ret;
8549 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8551 /* Look for the optional `::' operator. */
8552 global_scope_p
8553 = (cp_parser_global_scope_opt (parser,
8554 /*current_scope_valid_p=*/false)
8555 != NULL_TREE);
8556 /* Look for the `new' operator. */
8557 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8558 /* There's no easy way to tell a new-placement from the
8559 `( type-id )' construct. */
8560 cp_parser_parse_tentatively (parser);
8561 /* Look for a new-placement. */
8562 placement = cp_parser_new_placement (parser);
8563 /* If that didn't work out, there's no new-placement. */
8564 if (!cp_parser_parse_definitely (parser))
8566 if (placement != NULL)
8567 release_tree_vector (placement);
8568 placement = NULL;
8571 /* If the next token is a `(', then we have a parenthesized
8572 type-id. */
8573 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8575 cp_token *token;
8576 const char *saved_message = parser->type_definition_forbidden_message;
8578 /* Consume the `('. */
8579 matching_parens parens;
8580 parens.consume_open (parser);
8582 /* Parse the type-id. */
8583 parser->type_definition_forbidden_message
8584 = G_("types may not be defined in a new-expression");
8586 type_id_in_expr_sentinel s (parser);
8587 type = cp_parser_type_id (parser);
8589 parser->type_definition_forbidden_message = saved_message;
8591 /* Look for the closing `)'. */
8592 parens.require_close (parser);
8593 token = cp_lexer_peek_token (parser->lexer);
8594 /* There should not be a direct-new-declarator in this production,
8595 but GCC used to allowed this, so we check and emit a sensible error
8596 message for this case. */
8597 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8599 error_at (token->location,
8600 "array bound forbidden after parenthesized type-id");
8601 inform (token->location,
8602 "try removing the parentheses around the type-id");
8603 cp_parser_direct_new_declarator (parser);
8606 /* Otherwise, there must be a new-type-id. */
8607 else
8608 type = cp_parser_new_type_id (parser, &nelts);
8610 /* If the next token is a `(' or '{', then we have a new-initializer. */
8611 cp_token *token = cp_lexer_peek_token (parser->lexer);
8612 if (token->type == CPP_OPEN_PAREN
8613 || token->type == CPP_OPEN_BRACE)
8614 initializer = cp_parser_new_initializer (parser);
8615 else
8616 initializer = NULL;
8618 /* A new-expression may not appear in an integral constant
8619 expression. */
8620 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8621 ret = error_mark_node;
8622 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8623 of a new-type-id or type-id of a new-expression, the new-expression shall
8624 contain a new-initializer of the form ( assignment-expression )".
8625 Additionally, consistently with the spirit of DR 1467, we want to accept
8626 'new auto { 2 }' too. */
8627 else if ((ret = type_uses_auto (type))
8628 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8629 && (vec_safe_length (initializer) != 1
8630 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8631 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8633 error_at (token->location,
8634 "initialization of new-expression for type %<auto%> "
8635 "requires exactly one element");
8636 ret = error_mark_node;
8638 else
8640 /* Construct a location e.g.:
8641 ptr = new int[100]
8642 ^~~~~~~~~~~~
8643 with caret == start at the start of the "new" token, and the end
8644 at the end of the final token we consumed. */
8645 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8646 location_t end_loc = get_finish (end_tok->location);
8647 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8649 /* Create a representation of the new-expression. */
8650 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8651 tf_warning_or_error);
8652 protected_set_expr_location (ret, combined_loc);
8655 if (placement != NULL)
8656 release_tree_vector (placement);
8657 if (initializer != NULL)
8658 release_tree_vector (initializer);
8660 return ret;
8663 /* Parse a new-placement.
8665 new-placement:
8666 ( expression-list )
8668 Returns the same representation as for an expression-list. */
8670 static vec<tree, va_gc> *
8671 cp_parser_new_placement (cp_parser* parser)
8673 vec<tree, va_gc> *expression_list;
8675 /* Parse the expression-list. */
8676 expression_list = (cp_parser_parenthesized_expression_list
8677 (parser, non_attr, /*cast_p=*/false,
8678 /*allow_expansion_p=*/true,
8679 /*non_constant_p=*/NULL));
8681 if (expression_list && expression_list->is_empty ())
8682 error ("expected expression-list or type-id");
8684 return expression_list;
8687 /* Parse a new-type-id.
8689 new-type-id:
8690 type-specifier-seq new-declarator [opt]
8692 Returns the TYPE allocated. If the new-type-id indicates an array
8693 type, *NELTS is set to the number of elements in the last array
8694 bound; the TYPE will not include the last array bound. */
8696 static tree
8697 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8699 cp_decl_specifier_seq type_specifier_seq;
8700 cp_declarator *new_declarator;
8701 cp_declarator *declarator;
8702 cp_declarator *outer_declarator;
8703 const char *saved_message;
8705 /* The type-specifier sequence must not contain type definitions.
8706 (It cannot contain declarations of new types either, but if they
8707 are not definitions we will catch that because they are not
8708 complete.) */
8709 saved_message = parser->type_definition_forbidden_message;
8710 parser->type_definition_forbidden_message
8711 = G_("types may not be defined in a new-type-id");
8712 /* Parse the type-specifier-seq. */
8713 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8714 /*is_trailing_return=*/false,
8715 &type_specifier_seq);
8716 /* Restore the old message. */
8717 parser->type_definition_forbidden_message = saved_message;
8719 if (type_specifier_seq.type == error_mark_node)
8720 return error_mark_node;
8722 /* Parse the new-declarator. */
8723 new_declarator = cp_parser_new_declarator_opt (parser);
8725 /* Determine the number of elements in the last array dimension, if
8726 any. */
8727 *nelts = NULL_TREE;
8728 /* Skip down to the last array dimension. */
8729 declarator = new_declarator;
8730 outer_declarator = NULL;
8731 while (declarator && (declarator->kind == cdk_pointer
8732 || declarator->kind == cdk_ptrmem))
8734 outer_declarator = declarator;
8735 declarator = declarator->declarator;
8737 while (declarator
8738 && declarator->kind == cdk_array
8739 && declarator->declarator
8740 && declarator->declarator->kind == cdk_array)
8742 outer_declarator = declarator;
8743 declarator = declarator->declarator;
8746 if (declarator && declarator->kind == cdk_array)
8748 *nelts = declarator->u.array.bounds;
8749 if (*nelts == error_mark_node)
8750 *nelts = integer_one_node;
8752 if (outer_declarator)
8753 outer_declarator->declarator = declarator->declarator;
8754 else
8755 new_declarator = NULL;
8758 return groktypename (&type_specifier_seq, new_declarator, false);
8761 /* Parse an (optional) new-declarator.
8763 new-declarator:
8764 ptr-operator new-declarator [opt]
8765 direct-new-declarator
8767 Returns the declarator. */
8769 static cp_declarator *
8770 cp_parser_new_declarator_opt (cp_parser* parser)
8772 enum tree_code code;
8773 tree type, std_attributes = NULL_TREE;
8774 cp_cv_quals cv_quals;
8776 /* We don't know if there's a ptr-operator next, or not. */
8777 cp_parser_parse_tentatively (parser);
8778 /* Look for a ptr-operator. */
8779 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8780 /* If that worked, look for more new-declarators. */
8781 if (cp_parser_parse_definitely (parser))
8783 cp_declarator *declarator;
8785 /* Parse another optional declarator. */
8786 declarator = cp_parser_new_declarator_opt (parser);
8788 declarator = cp_parser_make_indirect_declarator
8789 (code, type, cv_quals, declarator, std_attributes);
8791 return declarator;
8794 /* If the next token is a `[', there is a direct-new-declarator. */
8795 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8796 return cp_parser_direct_new_declarator (parser);
8798 return NULL;
8801 /* Parse a direct-new-declarator.
8803 direct-new-declarator:
8804 [ expression ]
8805 direct-new-declarator [constant-expression]
8809 static cp_declarator *
8810 cp_parser_direct_new_declarator (cp_parser* parser)
8812 cp_declarator *declarator = NULL;
8814 while (true)
8816 tree expression;
8817 cp_token *token;
8819 /* Look for the opening `['. */
8820 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8822 token = cp_lexer_peek_token (parser->lexer);
8823 expression = cp_parser_expression (parser);
8824 /* The standard requires that the expression have integral
8825 type. DR 74 adds enumeration types. We believe that the
8826 real intent is that these expressions be handled like the
8827 expression in a `switch' condition, which also allows
8828 classes with a single conversion to integral or
8829 enumeration type. */
8830 if (!processing_template_decl)
8832 expression
8833 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8834 expression,
8835 /*complain=*/true);
8836 if (!expression)
8838 error_at (token->location,
8839 "expression in new-declarator must have integral "
8840 "or enumeration type");
8841 expression = error_mark_node;
8845 /* Look for the closing `]'. */
8846 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8848 /* Add this bound to the declarator. */
8849 declarator = make_array_declarator (declarator, expression);
8851 /* If the next token is not a `[', then there are no more
8852 bounds. */
8853 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8854 break;
8857 return declarator;
8860 /* Parse a new-initializer.
8862 new-initializer:
8863 ( expression-list [opt] )
8864 braced-init-list
8866 Returns a representation of the expression-list. */
8868 static vec<tree, va_gc> *
8869 cp_parser_new_initializer (cp_parser* parser)
8871 vec<tree, va_gc> *expression_list;
8873 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8875 tree t;
8876 bool expr_non_constant_p;
8877 cp_lexer_set_source_position (parser->lexer);
8878 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8879 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8880 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8881 expression_list = make_tree_vector_single (t);
8883 else
8884 expression_list = (cp_parser_parenthesized_expression_list
8885 (parser, non_attr, /*cast_p=*/false,
8886 /*allow_expansion_p=*/true,
8887 /*non_constant_p=*/NULL));
8889 return expression_list;
8892 /* Parse a delete-expression.
8894 delete-expression:
8895 :: [opt] delete cast-expression
8896 :: [opt] delete [ ] cast-expression
8898 Returns a representation of the expression. */
8900 static tree
8901 cp_parser_delete_expression (cp_parser* parser)
8903 bool global_scope_p;
8904 bool array_p;
8905 tree expression;
8907 /* Look for the optional `::' operator. */
8908 global_scope_p
8909 = (cp_parser_global_scope_opt (parser,
8910 /*current_scope_valid_p=*/false)
8911 != NULL_TREE);
8912 /* Look for the `delete' keyword. */
8913 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8914 /* See if the array syntax is in use. */
8915 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8917 /* Consume the `[' token. */
8918 cp_lexer_consume_token (parser->lexer);
8919 /* Look for the `]' token. */
8920 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8921 /* Remember that this is the `[]' construct. */
8922 array_p = true;
8924 else
8925 array_p = false;
8927 /* Parse the cast-expression. */
8928 expression = cp_parser_simple_cast_expression (parser);
8930 /* A delete-expression may not appear in an integral constant
8931 expression. */
8932 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8933 return error_mark_node;
8935 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8936 tf_warning_or_error);
8939 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8940 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8941 0 otherwise. */
8943 static int
8944 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8946 cp_token *token = cp_lexer_peek_token (parser->lexer);
8947 switch (token->type)
8949 case CPP_COMMA:
8950 case CPP_SEMICOLON:
8951 case CPP_QUERY:
8952 case CPP_COLON:
8953 case CPP_CLOSE_SQUARE:
8954 case CPP_CLOSE_PAREN:
8955 case CPP_CLOSE_BRACE:
8956 case CPP_OPEN_BRACE:
8957 case CPP_DOT:
8958 case CPP_DOT_STAR:
8959 case CPP_DEREF:
8960 case CPP_DEREF_STAR:
8961 case CPP_DIV:
8962 case CPP_MOD:
8963 case CPP_LSHIFT:
8964 case CPP_RSHIFT:
8965 case CPP_LESS:
8966 case CPP_GREATER:
8967 case CPP_LESS_EQ:
8968 case CPP_GREATER_EQ:
8969 case CPP_EQ_EQ:
8970 case CPP_NOT_EQ:
8971 case CPP_EQ:
8972 case CPP_MULT_EQ:
8973 case CPP_DIV_EQ:
8974 case CPP_MOD_EQ:
8975 case CPP_PLUS_EQ:
8976 case CPP_MINUS_EQ:
8977 case CPP_RSHIFT_EQ:
8978 case CPP_LSHIFT_EQ:
8979 case CPP_AND_EQ:
8980 case CPP_XOR_EQ:
8981 case CPP_OR_EQ:
8982 case CPP_XOR:
8983 case CPP_OR:
8984 case CPP_OR_OR:
8985 case CPP_EOF:
8986 case CPP_ELLIPSIS:
8987 return 0;
8989 case CPP_OPEN_PAREN:
8990 /* In ((type ()) () the last () isn't a valid cast-expression,
8991 so the whole must be parsed as postfix-expression. */
8992 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8993 != CPP_CLOSE_PAREN;
8995 case CPP_OPEN_SQUARE:
8996 /* '[' may start a primary-expression in obj-c++ and in C++11,
8997 as a lambda-expression, eg, '(void)[]{}'. */
8998 if (cxx_dialect >= cxx11)
8999 return -1;
9000 return c_dialect_objc ();
9002 case CPP_PLUS_PLUS:
9003 case CPP_MINUS_MINUS:
9004 /* '++' and '--' may or may not start a cast-expression:
9006 struct T { void operator++(int); };
9007 void f() { (T())++; }
9011 int a;
9012 (int)++a; */
9013 return -1;
9015 default:
9016 return 1;
9020 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9021 in the order: const_cast, static_cast, reinterpret_cast.
9023 Don't suggest dynamic_cast.
9025 Return the first legal cast kind found, or NULL otherwise. */
9027 static const char *
9028 get_cast_suggestion (tree dst_type, tree orig_expr)
9030 tree trial;
9032 /* Reuse the parser logic by attempting to build the various kinds of
9033 cast, with "complain" disabled.
9034 Identify the first such cast that is valid. */
9036 /* Don't attempt to run such logic within template processing. */
9037 if (processing_template_decl)
9038 return NULL;
9040 /* First try const_cast. */
9041 trial = build_const_cast (dst_type, orig_expr, tf_none);
9042 if (trial != error_mark_node)
9043 return "const_cast";
9045 /* If that fails, try static_cast. */
9046 trial = build_static_cast (dst_type, orig_expr, tf_none);
9047 if (trial != error_mark_node)
9048 return "static_cast";
9050 /* Finally, try reinterpret_cast. */
9051 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
9052 if (trial != error_mark_node)
9053 return "reinterpret_cast";
9055 /* No such cast possible. */
9056 return NULL;
9059 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9060 suggesting how to convert a C-style cast of the form:
9062 (DST_TYPE)ORIG_EXPR
9064 to a C++-style cast.
9066 The primary range of RICHLOC is asssumed to be that of the original
9067 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9068 of the parens in the C-style cast. */
9070 static void
9071 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9072 location_t close_paren_loc, tree orig_expr,
9073 tree dst_type)
9075 /* This function is non-trivial, so bail out now if the warning isn't
9076 going to be emitted. */
9077 if (!warn_old_style_cast)
9078 return;
9080 /* Try to find a legal C++ cast, trying them in order:
9081 const_cast, static_cast, reinterpret_cast. */
9082 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9083 if (!cast_suggestion)
9084 return;
9086 /* Replace the open paren with "CAST_SUGGESTION<". */
9087 pretty_printer pp;
9088 pp_printf (&pp, "%s<", cast_suggestion);
9089 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9091 /* Replace the close paren with "> (". */
9092 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9094 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9095 rich_loc->add_fixit_insert_after (")");
9099 /* Parse a cast-expression.
9101 cast-expression:
9102 unary-expression
9103 ( type-id ) cast-expression
9105 ADDRESS_P is true iff the unary-expression is appearing as the
9106 operand of the `&' operator. CAST_P is true if this expression is
9107 the target of a cast.
9109 Returns a representation of the expression. */
9111 static cp_expr
9112 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9113 bool decltype_p, cp_id_kind * pidk)
9115 /* If it's a `(', then we might be looking at a cast. */
9116 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9118 tree type = NULL_TREE;
9119 cp_expr expr (NULL_TREE);
9120 int cast_expression = 0;
9121 const char *saved_message;
9123 /* There's no way to know yet whether or not this is a cast.
9124 For example, `(int (3))' is a unary-expression, while `(int)
9125 3' is a cast. So, we resort to parsing tentatively. */
9126 cp_parser_parse_tentatively (parser);
9127 /* Types may not be defined in a cast. */
9128 saved_message = parser->type_definition_forbidden_message;
9129 parser->type_definition_forbidden_message
9130 = G_("types may not be defined in casts");
9131 /* Consume the `('. */
9132 matching_parens parens;
9133 cp_token *open_paren = parens.consume_open (parser);
9134 location_t open_paren_loc = open_paren->location;
9135 location_t close_paren_loc = UNKNOWN_LOCATION;
9137 /* A very tricky bit is that `(struct S) { 3 }' is a
9138 compound-literal (which we permit in C++ as an extension).
9139 But, that construct is not a cast-expression -- it is a
9140 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9141 is legal; if the compound-literal were a cast-expression,
9142 you'd need an extra set of parentheses.) But, if we parse
9143 the type-id, and it happens to be a class-specifier, then we
9144 will commit to the parse at that point, because we cannot
9145 undo the action that is done when creating a new class. So,
9146 then we cannot back up and do a postfix-expression.
9148 Another tricky case is the following (c++/29234):
9150 struct S { void operator () (); };
9152 void foo ()
9154 ( S()() );
9157 As a type-id we parse the parenthesized S()() as a function
9158 returning a function, groktypename complains and we cannot
9159 back up in this case either.
9161 Therefore, we scan ahead to the closing `)', and check to see
9162 if the tokens after the `)' can start a cast-expression. Otherwise
9163 we are dealing with an unary-expression, a postfix-expression
9164 or something else.
9166 Yet another tricky case, in C++11, is the following (c++/54891):
9168 (void)[]{};
9170 The issue is that usually, besides the case of lambda-expressions,
9171 the parenthesized type-id cannot be followed by '[', and, eg, we
9172 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9173 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9174 we don't commit, we try a cast-expression, then an unary-expression.
9176 Save tokens so that we can put them back. */
9177 cp_lexer_save_tokens (parser->lexer);
9179 /* We may be looking at a cast-expression. */
9180 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9181 /*consume_paren=*/true))
9182 cast_expression
9183 = cp_parser_tokens_start_cast_expression (parser);
9185 /* Roll back the tokens we skipped. */
9186 cp_lexer_rollback_tokens (parser->lexer);
9187 /* If we aren't looking at a cast-expression, simulate an error so
9188 that the call to cp_parser_error_occurred below returns true. */
9189 if (!cast_expression)
9190 cp_parser_simulate_error (parser);
9191 else
9193 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9194 parser->in_type_id_in_expr_p = true;
9195 /* Look for the type-id. */
9196 type = cp_parser_type_id (parser);
9197 /* Look for the closing `)'. */
9198 cp_token *close_paren = parens.require_close (parser);
9199 if (close_paren)
9200 close_paren_loc = close_paren->location;
9201 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9204 /* Restore the saved message. */
9205 parser->type_definition_forbidden_message = saved_message;
9207 /* At this point this can only be either a cast or a
9208 parenthesized ctor such as `(T ())' that looks like a cast to
9209 function returning T. */
9210 if (!cp_parser_error_occurred (parser))
9212 /* Only commit if the cast-expression doesn't start with
9213 '++', '--', or '[' in C++11. */
9214 if (cast_expression > 0)
9215 cp_parser_commit_to_topmost_tentative_parse (parser);
9217 expr = cp_parser_cast_expression (parser,
9218 /*address_p=*/false,
9219 /*cast_p=*/true,
9220 /*decltype_p=*/false,
9221 pidk);
9223 if (cp_parser_parse_definitely (parser))
9225 /* Warn about old-style casts, if so requested. */
9226 if (warn_old_style_cast
9227 && !in_system_header_at (input_location)
9228 && !VOID_TYPE_P (type)
9229 && current_lang_name != lang_name_c)
9231 gcc_rich_location rich_loc (input_location);
9232 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9233 expr, type);
9234 warning_at (&rich_loc, OPT_Wold_style_cast,
9235 "use of old-style cast to %q#T", type);
9238 /* Only type conversions to integral or enumeration types
9239 can be used in constant-expressions. */
9240 if (!cast_valid_in_integral_constant_expression_p (type)
9241 && cp_parser_non_integral_constant_expression (parser,
9242 NIC_CAST))
9243 return error_mark_node;
9245 /* Perform the cast. */
9246 /* Make a location:
9247 (TYPE) EXPR
9248 ^~~~~~~~~~~
9249 with start==caret at the open paren, extending to the
9250 end of "expr". */
9251 location_t cast_loc = make_location (open_paren_loc,
9252 open_paren_loc,
9253 expr.get_finish ());
9254 expr = build_c_cast (cast_loc, type, expr);
9255 return expr;
9258 else
9259 cp_parser_abort_tentative_parse (parser);
9262 /* If we get here, then it's not a cast, so it must be a
9263 unary-expression. */
9264 return cp_parser_unary_expression (parser, pidk, address_p,
9265 cast_p, decltype_p);
9268 /* Parse a binary expression of the general form:
9270 pm-expression:
9271 cast-expression
9272 pm-expression .* cast-expression
9273 pm-expression ->* cast-expression
9275 multiplicative-expression:
9276 pm-expression
9277 multiplicative-expression * pm-expression
9278 multiplicative-expression / pm-expression
9279 multiplicative-expression % pm-expression
9281 additive-expression:
9282 multiplicative-expression
9283 additive-expression + multiplicative-expression
9284 additive-expression - multiplicative-expression
9286 shift-expression:
9287 additive-expression
9288 shift-expression << additive-expression
9289 shift-expression >> additive-expression
9291 relational-expression:
9292 shift-expression
9293 relational-expression < shift-expression
9294 relational-expression > shift-expression
9295 relational-expression <= shift-expression
9296 relational-expression >= shift-expression
9298 GNU Extension:
9300 relational-expression:
9301 relational-expression <? shift-expression
9302 relational-expression >? shift-expression
9304 equality-expression:
9305 relational-expression
9306 equality-expression == relational-expression
9307 equality-expression != relational-expression
9309 and-expression:
9310 equality-expression
9311 and-expression & equality-expression
9313 exclusive-or-expression:
9314 and-expression
9315 exclusive-or-expression ^ and-expression
9317 inclusive-or-expression:
9318 exclusive-or-expression
9319 inclusive-or-expression | exclusive-or-expression
9321 logical-and-expression:
9322 inclusive-or-expression
9323 logical-and-expression && inclusive-or-expression
9325 logical-or-expression:
9326 logical-and-expression
9327 logical-or-expression || logical-and-expression
9329 All these are implemented with a single function like:
9331 binary-expression:
9332 simple-cast-expression
9333 binary-expression <token> binary-expression
9335 CAST_P is true if this expression is the target of a cast.
9337 The binops_by_token map is used to get the tree codes for each <token> type.
9338 binary-expressions are associated according to a precedence table. */
9340 #define TOKEN_PRECEDENCE(token) \
9341 (((token->type == CPP_GREATER \
9342 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9343 && !parser->greater_than_is_operator_p) \
9344 ? PREC_NOT_OPERATOR \
9345 : binops_by_token[token->type].prec)
9347 static cp_expr
9348 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9349 bool no_toplevel_fold_p,
9350 bool decltype_p,
9351 enum cp_parser_prec prec,
9352 cp_id_kind * pidk)
9354 cp_parser_expression_stack stack;
9355 cp_parser_expression_stack_entry *sp = &stack[0];
9356 cp_parser_expression_stack_entry current;
9357 cp_expr rhs;
9358 cp_token *token;
9359 enum tree_code rhs_type;
9360 enum cp_parser_prec new_prec, lookahead_prec;
9361 tree overload;
9363 /* Parse the first expression. */
9364 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9365 ? TRUTH_NOT_EXPR : ERROR_MARK);
9366 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9367 cast_p, decltype_p, pidk);
9368 current.prec = prec;
9370 if (cp_parser_error_occurred (parser))
9371 return error_mark_node;
9373 for (;;)
9375 /* Get an operator token. */
9376 token = cp_lexer_peek_token (parser->lexer);
9378 if (warn_cxx11_compat
9379 && token->type == CPP_RSHIFT
9380 && !parser->greater_than_is_operator_p)
9382 if (warning_at (token->location, OPT_Wc__11_compat,
9383 "%<>>%> operator is treated"
9384 " as two right angle brackets in C++11"))
9385 inform (token->location,
9386 "suggest parentheses around %<>>%> expression");
9389 new_prec = TOKEN_PRECEDENCE (token);
9390 if (new_prec != PREC_NOT_OPERATOR
9391 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9392 /* This is a fold-expression; handle it later. */
9393 new_prec = PREC_NOT_OPERATOR;
9395 /* Popping an entry off the stack means we completed a subexpression:
9396 - either we found a token which is not an operator (`>' where it is not
9397 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9398 will happen repeatedly;
9399 - or, we found an operator which has lower priority. This is the case
9400 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9401 parsing `3 * 4'. */
9402 if (new_prec <= current.prec)
9404 if (sp == stack)
9405 break;
9406 else
9407 goto pop;
9410 get_rhs:
9411 current.tree_type = binops_by_token[token->type].tree_type;
9412 current.loc = token->location;
9414 /* We used the operator token. */
9415 cp_lexer_consume_token (parser->lexer);
9417 /* For "false && x" or "true || x", x will never be executed;
9418 disable warnings while evaluating it. */
9419 if (current.tree_type == TRUTH_ANDIF_EXPR)
9420 c_inhibit_evaluation_warnings +=
9421 cp_fully_fold (current.lhs) == truthvalue_false_node;
9422 else if (current.tree_type == TRUTH_ORIF_EXPR)
9423 c_inhibit_evaluation_warnings +=
9424 cp_fully_fold (current.lhs) == truthvalue_true_node;
9426 /* Extract another operand. It may be the RHS of this expression
9427 or the LHS of a new, higher priority expression. */
9428 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9429 ? TRUTH_NOT_EXPR : ERROR_MARK);
9430 rhs = cp_parser_simple_cast_expression (parser);
9432 /* Get another operator token. Look up its precedence to avoid
9433 building a useless (immediately popped) stack entry for common
9434 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9435 token = cp_lexer_peek_token (parser->lexer);
9436 lookahead_prec = TOKEN_PRECEDENCE (token);
9437 if (lookahead_prec != PREC_NOT_OPERATOR
9438 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9439 lookahead_prec = PREC_NOT_OPERATOR;
9440 if (lookahead_prec > new_prec)
9442 /* ... and prepare to parse the RHS of the new, higher priority
9443 expression. Since precedence levels on the stack are
9444 monotonically increasing, we do not have to care about
9445 stack overflows. */
9446 *sp = current;
9447 ++sp;
9448 current.lhs = rhs;
9449 current.lhs_type = rhs_type;
9450 current.prec = new_prec;
9451 new_prec = lookahead_prec;
9452 goto get_rhs;
9454 pop:
9455 lookahead_prec = new_prec;
9456 /* If the stack is not empty, we have parsed into LHS the right side
9457 (`4' in the example above) of an expression we had suspended.
9458 We can use the information on the stack to recover the LHS (`3')
9459 from the stack together with the tree code (`MULT_EXPR'), and
9460 the precedence of the higher level subexpression
9461 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9462 which will be used to actually build the additive expression. */
9463 rhs = current.lhs;
9464 rhs_type = current.lhs_type;
9465 --sp;
9466 current = *sp;
9469 /* Undo the disabling of warnings done above. */
9470 if (current.tree_type == TRUTH_ANDIF_EXPR)
9471 c_inhibit_evaluation_warnings -=
9472 cp_fully_fold (current.lhs) == truthvalue_false_node;
9473 else if (current.tree_type == TRUTH_ORIF_EXPR)
9474 c_inhibit_evaluation_warnings -=
9475 cp_fully_fold (current.lhs) == truthvalue_true_node;
9477 if (warn_logical_not_paren
9478 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9479 && current.lhs_type == TRUTH_NOT_EXPR
9480 /* Avoid warning for !!x == y. */
9481 && (TREE_CODE (current.lhs) != NE_EXPR
9482 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9483 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9484 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9485 /* Avoid warning for !b == y where b is boolean. */
9486 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9487 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9488 != BOOLEAN_TYPE))))
9489 /* Avoid warning for !!b == y where b is boolean. */
9490 && (!DECL_P (current.lhs)
9491 || TREE_TYPE (current.lhs) == NULL_TREE
9492 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9493 warn_logical_not_parentheses (current.loc, current.tree_type,
9494 current.lhs, maybe_constant_value (rhs));
9496 overload = NULL;
9498 location_t combined_loc = make_location (current.loc,
9499 current.lhs.get_start (),
9500 rhs.get_finish ());
9502 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9503 ERROR_MARK for everything that is not a binary expression.
9504 This makes warn_about_parentheses miss some warnings that
9505 involve unary operators. For unary expressions we should
9506 pass the correct tree_code unless the unary expression was
9507 surrounded by parentheses.
9509 if (no_toplevel_fold_p
9510 && lookahead_prec <= current.prec
9511 && sp == stack)
9513 if (current.lhs == error_mark_node || rhs == error_mark_node)
9514 current.lhs = error_mark_node;
9515 else
9517 current.lhs
9518 = build_min (current.tree_type,
9519 TREE_CODE_CLASS (current.tree_type)
9520 == tcc_comparison
9521 ? boolean_type_node : TREE_TYPE (current.lhs),
9522 current.lhs.get_value (), rhs.get_value ());
9523 SET_EXPR_LOCATION (current.lhs, combined_loc);
9526 else
9528 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9529 current.lhs, current.lhs_type,
9530 rhs, rhs_type, &overload,
9531 complain_flags (decltype_p));
9532 /* TODO: build_x_binary_op doesn't always honor the location. */
9533 current.lhs.set_location (combined_loc);
9535 current.lhs_type = current.tree_type;
9537 /* If the binary operator required the use of an overloaded operator,
9538 then this expression cannot be an integral constant-expression.
9539 An overloaded operator can be used even if both operands are
9540 otherwise permissible in an integral constant-expression if at
9541 least one of the operands is of enumeration type. */
9543 if (overload
9544 && cp_parser_non_integral_constant_expression (parser,
9545 NIC_OVERLOADED))
9546 return error_mark_node;
9549 return current.lhs;
9552 static cp_expr
9553 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9554 bool no_toplevel_fold_p,
9555 enum cp_parser_prec prec,
9556 cp_id_kind * pidk)
9558 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9559 /*decltype*/false, prec, pidk);
9562 /* Parse the `? expression : assignment-expression' part of a
9563 conditional-expression. The LOGICAL_OR_EXPR is the
9564 logical-or-expression that started the conditional-expression.
9565 Returns a representation of the entire conditional-expression.
9567 This routine is used by cp_parser_assignment_expression.
9569 ? expression : assignment-expression
9571 GNU Extensions:
9573 ? : assignment-expression */
9575 static tree
9576 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9578 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9579 cp_expr assignment_expr;
9580 struct cp_token *token;
9581 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9583 /* Consume the `?' token. */
9584 cp_lexer_consume_token (parser->lexer);
9585 token = cp_lexer_peek_token (parser->lexer);
9586 if (cp_parser_allow_gnu_extensions_p (parser)
9587 && token->type == CPP_COLON)
9589 pedwarn (token->location, OPT_Wpedantic,
9590 "ISO C++ does not allow ?: with omitted middle operand");
9591 /* Implicit true clause. */
9592 expr = NULL_TREE;
9593 c_inhibit_evaluation_warnings +=
9594 folded_logical_or_expr == truthvalue_true_node;
9595 warn_for_omitted_condop (token->location, logical_or_expr);
9597 else
9599 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9600 parser->colon_corrects_to_scope_p = false;
9601 /* Parse the expression. */
9602 c_inhibit_evaluation_warnings +=
9603 folded_logical_or_expr == truthvalue_false_node;
9604 expr = cp_parser_expression (parser);
9605 c_inhibit_evaluation_warnings +=
9606 ((folded_logical_or_expr == truthvalue_true_node)
9607 - (folded_logical_or_expr == truthvalue_false_node));
9608 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9611 /* The next token should be a `:'. */
9612 cp_parser_require (parser, CPP_COLON, RT_COLON);
9613 /* Parse the assignment-expression. */
9614 assignment_expr = cp_parser_assignment_expression (parser);
9615 c_inhibit_evaluation_warnings -=
9616 folded_logical_or_expr == truthvalue_true_node;
9618 /* Make a location:
9619 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9620 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9621 with the caret at the "?", ranging from the start of
9622 the logical_or_expr to the end of the assignment_expr. */
9623 loc = make_location (loc,
9624 logical_or_expr.get_start (),
9625 assignment_expr.get_finish ());
9627 /* Build the conditional-expression. */
9628 return build_x_conditional_expr (loc, logical_or_expr,
9629 expr,
9630 assignment_expr,
9631 tf_warning_or_error);
9634 /* Parse an assignment-expression.
9636 assignment-expression:
9637 conditional-expression
9638 logical-or-expression assignment-operator assignment_expression
9639 throw-expression
9641 CAST_P is true if this expression is the target of a cast.
9642 DECLTYPE_P is true if this expression is the operand of decltype.
9644 Returns a representation for the expression. */
9646 static cp_expr
9647 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9648 bool cast_p, bool decltype_p)
9650 cp_expr expr;
9652 /* If the next token is the `throw' keyword, then we're looking at
9653 a throw-expression. */
9654 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9655 expr = cp_parser_throw_expression (parser);
9656 /* Otherwise, it must be that we are looking at a
9657 logical-or-expression. */
9658 else
9660 /* Parse the binary expressions (logical-or-expression). */
9661 expr = cp_parser_binary_expression (parser, cast_p, false,
9662 decltype_p,
9663 PREC_NOT_OPERATOR, pidk);
9664 /* If the next token is a `?' then we're actually looking at a
9665 conditional-expression. */
9666 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9667 return cp_parser_question_colon_clause (parser, expr);
9668 else
9670 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9672 /* If it's an assignment-operator, we're using the second
9673 production. */
9674 enum tree_code assignment_operator
9675 = cp_parser_assignment_operator_opt (parser);
9676 if (assignment_operator != ERROR_MARK)
9678 bool non_constant_p;
9680 /* Parse the right-hand side of the assignment. */
9681 cp_expr rhs = cp_parser_initializer_clause (parser,
9682 &non_constant_p);
9684 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9685 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9687 /* An assignment may not appear in a
9688 constant-expression. */
9689 if (cp_parser_non_integral_constant_expression (parser,
9690 NIC_ASSIGNMENT))
9691 return error_mark_node;
9692 /* Build the assignment expression. Its default
9693 location:
9694 LHS = RHS
9695 ~~~~^~~~~
9696 is the location of the '=' token as the
9697 caret, ranging from the start of the lhs to the
9698 end of the rhs. */
9699 loc = make_location (loc,
9700 expr.get_start (),
9701 rhs.get_finish ());
9702 expr = build_x_modify_expr (loc, expr,
9703 assignment_operator,
9704 rhs,
9705 complain_flags (decltype_p));
9706 /* TODO: build_x_modify_expr doesn't honor the location,
9707 so we must set it here. */
9708 expr.set_location (loc);
9713 return expr;
9716 /* Parse an (optional) assignment-operator.
9718 assignment-operator: one of
9719 = *= /= %= += -= >>= <<= &= ^= |=
9721 GNU Extension:
9723 assignment-operator: one of
9724 <?= >?=
9726 If the next token is an assignment operator, the corresponding tree
9727 code is returned, and the token is consumed. For example, for
9728 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9729 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9730 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9731 operator, ERROR_MARK is returned. */
9733 static enum tree_code
9734 cp_parser_assignment_operator_opt (cp_parser* parser)
9736 enum tree_code op;
9737 cp_token *token;
9739 /* Peek at the next token. */
9740 token = cp_lexer_peek_token (parser->lexer);
9742 switch (token->type)
9744 case CPP_EQ:
9745 op = NOP_EXPR;
9746 break;
9748 case CPP_MULT_EQ:
9749 op = MULT_EXPR;
9750 break;
9752 case CPP_DIV_EQ:
9753 op = TRUNC_DIV_EXPR;
9754 break;
9756 case CPP_MOD_EQ:
9757 op = TRUNC_MOD_EXPR;
9758 break;
9760 case CPP_PLUS_EQ:
9761 op = PLUS_EXPR;
9762 break;
9764 case CPP_MINUS_EQ:
9765 op = MINUS_EXPR;
9766 break;
9768 case CPP_RSHIFT_EQ:
9769 op = RSHIFT_EXPR;
9770 break;
9772 case CPP_LSHIFT_EQ:
9773 op = LSHIFT_EXPR;
9774 break;
9776 case CPP_AND_EQ:
9777 op = BIT_AND_EXPR;
9778 break;
9780 case CPP_XOR_EQ:
9781 op = BIT_XOR_EXPR;
9782 break;
9784 case CPP_OR_EQ:
9785 op = BIT_IOR_EXPR;
9786 break;
9788 default:
9789 /* Nothing else is an assignment operator. */
9790 op = ERROR_MARK;
9793 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9794 if (op != ERROR_MARK
9795 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9796 op = ERROR_MARK;
9798 /* If it was an assignment operator, consume it. */
9799 if (op != ERROR_MARK)
9800 cp_lexer_consume_token (parser->lexer);
9802 return op;
9805 /* Parse an expression.
9807 expression:
9808 assignment-expression
9809 expression , assignment-expression
9811 CAST_P is true if this expression is the target of a cast.
9812 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9813 except possibly parenthesized or on the RHS of a comma (N3276).
9815 Returns a representation of the expression. */
9817 static cp_expr
9818 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9819 bool cast_p, bool decltype_p)
9821 cp_expr expression = NULL_TREE;
9822 location_t loc = UNKNOWN_LOCATION;
9824 while (true)
9826 cp_expr assignment_expression;
9828 /* Parse the next assignment-expression. */
9829 assignment_expression
9830 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9832 /* We don't create a temporary for a call that is the immediate operand
9833 of decltype or on the RHS of a comma. But when we see a comma, we
9834 need to create a temporary for a call on the LHS. */
9835 if (decltype_p && !processing_template_decl
9836 && TREE_CODE (assignment_expression) == CALL_EXPR
9837 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9838 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9839 assignment_expression
9840 = build_cplus_new (TREE_TYPE (assignment_expression),
9841 assignment_expression, tf_warning_or_error);
9843 /* If this is the first assignment-expression, we can just
9844 save it away. */
9845 if (!expression)
9846 expression = assignment_expression;
9847 else
9849 /* Create a location with caret at the comma, ranging
9850 from the start of the LHS to the end of the RHS. */
9851 loc = make_location (loc,
9852 expression.get_start (),
9853 assignment_expression.get_finish ());
9854 expression = build_x_compound_expr (loc, expression,
9855 assignment_expression,
9856 complain_flags (decltype_p));
9857 expression.set_location (loc);
9859 /* If the next token is not a comma, or we're in a fold-expression, then
9860 we are done with the expression. */
9861 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9862 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9863 break;
9864 /* Consume the `,'. */
9865 loc = cp_lexer_peek_token (parser->lexer)->location;
9866 cp_lexer_consume_token (parser->lexer);
9867 /* A comma operator cannot appear in a constant-expression. */
9868 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9869 expression = error_mark_node;
9872 return expression;
9875 /* Parse a constant-expression.
9877 constant-expression:
9878 conditional-expression
9880 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9881 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9882 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9883 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9884 only parse a conditional-expression, otherwise parse an
9885 assignment-expression. See below for rationale. */
9887 static cp_expr
9888 cp_parser_constant_expression (cp_parser* parser,
9889 bool allow_non_constant_p,
9890 bool *non_constant_p,
9891 bool strict_p)
9893 bool saved_integral_constant_expression_p;
9894 bool saved_allow_non_integral_constant_expression_p;
9895 bool saved_non_integral_constant_expression_p;
9896 cp_expr expression;
9898 /* It might seem that we could simply parse the
9899 conditional-expression, and then check to see if it were
9900 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9901 one that the compiler can figure out is constant, possibly after
9902 doing some simplifications or optimizations. The standard has a
9903 precise definition of constant-expression, and we must honor
9904 that, even though it is somewhat more restrictive.
9906 For example:
9908 int i[(2, 3)];
9910 is not a legal declaration, because `(2, 3)' is not a
9911 constant-expression. The `,' operator is forbidden in a
9912 constant-expression. However, GCC's constant-folding machinery
9913 will fold this operation to an INTEGER_CST for `3'. */
9915 /* Save the old settings. */
9916 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9917 saved_allow_non_integral_constant_expression_p
9918 = parser->allow_non_integral_constant_expression_p;
9919 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9920 /* We are now parsing a constant-expression. */
9921 parser->integral_constant_expression_p = true;
9922 parser->allow_non_integral_constant_expression_p
9923 = (allow_non_constant_p || cxx_dialect >= cxx11);
9924 parser->non_integral_constant_expression_p = false;
9925 /* Although the grammar says "conditional-expression", when not STRICT_P,
9926 we parse an "assignment-expression", which also permits
9927 "throw-expression" and the use of assignment operators. In the case
9928 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9929 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9930 actually essential that we look for an assignment-expression.
9931 For example, cp_parser_initializer_clauses uses this function to
9932 determine whether a particular assignment-expression is in fact
9933 constant. */
9934 if (strict_p)
9936 /* Parse the binary expressions (logical-or-expression). */
9937 expression = cp_parser_binary_expression (parser, false, false, false,
9938 PREC_NOT_OPERATOR, NULL);
9939 /* If the next token is a `?' then we're actually looking at
9940 a conditional-expression; otherwise we're done. */
9941 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9942 expression = cp_parser_question_colon_clause (parser, expression);
9944 else
9945 expression = cp_parser_assignment_expression (parser);
9946 /* Restore the old settings. */
9947 parser->integral_constant_expression_p
9948 = saved_integral_constant_expression_p;
9949 parser->allow_non_integral_constant_expression_p
9950 = saved_allow_non_integral_constant_expression_p;
9951 if (cxx_dialect >= cxx11)
9953 /* Require an rvalue constant expression here; that's what our
9954 callers expect. Reference constant expressions are handled
9955 separately in e.g. cp_parser_template_argument. */
9956 tree decay = expression;
9957 if (TREE_TYPE (expression)
9958 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9959 decay = build_address (expression);
9960 bool is_const = potential_rvalue_constant_expression (decay);
9961 parser->non_integral_constant_expression_p = !is_const;
9962 if (!is_const && !allow_non_constant_p)
9963 require_potential_rvalue_constant_expression (decay);
9965 if (allow_non_constant_p)
9966 *non_constant_p = parser->non_integral_constant_expression_p;
9967 parser->non_integral_constant_expression_p
9968 = saved_non_integral_constant_expression_p;
9970 return expression;
9973 /* Parse __builtin_offsetof.
9975 offsetof-expression:
9976 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9978 offsetof-member-designator:
9979 id-expression
9980 | offsetof-member-designator "." id-expression
9981 | offsetof-member-designator "[" expression "]"
9982 | offsetof-member-designator "->" id-expression */
9984 static cp_expr
9985 cp_parser_builtin_offsetof (cp_parser *parser)
9987 int save_ice_p, save_non_ice_p;
9988 tree type;
9989 cp_expr expr;
9990 cp_id_kind dummy;
9991 cp_token *token;
9992 location_t finish_loc;
9994 /* We're about to accept non-integral-constant things, but will
9995 definitely yield an integral constant expression. Save and
9996 restore these values around our local parsing. */
9997 save_ice_p = parser->integral_constant_expression_p;
9998 save_non_ice_p = parser->non_integral_constant_expression_p;
10000 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10002 /* Consume the "__builtin_offsetof" token. */
10003 cp_lexer_consume_token (parser->lexer);
10004 /* Consume the opening `('. */
10005 matching_parens parens;
10006 parens.require_open (parser);
10007 /* Parse the type-id. */
10008 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10010 const char *saved_message = parser->type_definition_forbidden_message;
10011 parser->type_definition_forbidden_message
10012 = G_("types may not be defined within __builtin_offsetof");
10013 type = cp_parser_type_id (parser);
10014 parser->type_definition_forbidden_message = saved_message;
10016 /* Look for the `,'. */
10017 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10018 token = cp_lexer_peek_token (parser->lexer);
10020 /* Build the (type *)null that begins the traditional offsetof macro. */
10021 tree object_ptr
10022 = build_static_cast (build_pointer_type (type), null_pointer_node,
10023 tf_warning_or_error);
10025 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10026 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10027 true, &dummy, token->location);
10028 while (true)
10030 token = cp_lexer_peek_token (parser->lexer);
10031 switch (token->type)
10033 case CPP_OPEN_SQUARE:
10034 /* offsetof-member-designator "[" expression "]" */
10035 expr = cp_parser_postfix_open_square_expression (parser, expr,
10036 true, false);
10037 break;
10039 case CPP_DEREF:
10040 /* offsetof-member-designator "->" identifier */
10041 expr = grok_array_decl (token->location, expr,
10042 integer_zero_node, false);
10043 /* FALLTHRU */
10045 case CPP_DOT:
10046 /* offsetof-member-designator "." identifier */
10047 cp_lexer_consume_token (parser->lexer);
10048 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10049 expr, true, &dummy,
10050 token->location);
10051 break;
10053 case CPP_CLOSE_PAREN:
10054 /* Consume the ")" token. */
10055 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10056 cp_lexer_consume_token (parser->lexer);
10057 goto success;
10059 default:
10060 /* Error. We know the following require will fail, but
10061 that gives the proper error message. */
10062 parens.require_close (parser);
10063 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10064 expr = error_mark_node;
10065 goto failure;
10069 success:
10070 /* Make a location of the form:
10071 __builtin_offsetof (struct s, f)
10072 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10073 with caret at the type-id, ranging from the start of the
10074 "_builtin_offsetof" token to the close paren. */
10075 loc = make_location (loc, start_loc, finish_loc);
10076 /* The result will be an INTEGER_CST, so we need to explicitly
10077 preserve the location. */
10078 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10080 failure:
10081 parser->integral_constant_expression_p = save_ice_p;
10082 parser->non_integral_constant_expression_p = save_non_ice_p;
10084 expr = expr.maybe_add_location_wrapper ();
10085 return expr;
10088 /* Parse a trait expression.
10090 Returns a representation of the expression, the underlying type
10091 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10093 static cp_expr
10094 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10096 cp_trait_kind kind;
10097 tree type1, type2 = NULL_TREE;
10098 bool binary = false;
10099 bool variadic = false;
10101 switch (keyword)
10103 case RID_HAS_NOTHROW_ASSIGN:
10104 kind = CPTK_HAS_NOTHROW_ASSIGN;
10105 break;
10106 case RID_HAS_NOTHROW_CONSTRUCTOR:
10107 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10108 break;
10109 case RID_HAS_NOTHROW_COPY:
10110 kind = CPTK_HAS_NOTHROW_COPY;
10111 break;
10112 case RID_HAS_TRIVIAL_ASSIGN:
10113 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10114 break;
10115 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10116 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10117 break;
10118 case RID_HAS_TRIVIAL_COPY:
10119 kind = CPTK_HAS_TRIVIAL_COPY;
10120 break;
10121 case RID_HAS_TRIVIAL_DESTRUCTOR:
10122 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10123 break;
10124 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10125 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10126 break;
10127 case RID_HAS_VIRTUAL_DESTRUCTOR:
10128 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10129 break;
10130 case RID_IS_ABSTRACT:
10131 kind = CPTK_IS_ABSTRACT;
10132 break;
10133 case RID_IS_AGGREGATE:
10134 kind = CPTK_IS_AGGREGATE;
10135 break;
10136 case RID_IS_BASE_OF:
10137 kind = CPTK_IS_BASE_OF;
10138 binary = true;
10139 break;
10140 case RID_IS_CLASS:
10141 kind = CPTK_IS_CLASS;
10142 break;
10143 case RID_IS_EMPTY:
10144 kind = CPTK_IS_EMPTY;
10145 break;
10146 case RID_IS_ENUM:
10147 kind = CPTK_IS_ENUM;
10148 break;
10149 case RID_IS_FINAL:
10150 kind = CPTK_IS_FINAL;
10151 break;
10152 case RID_IS_LITERAL_TYPE:
10153 kind = CPTK_IS_LITERAL_TYPE;
10154 break;
10155 case RID_IS_POD:
10156 kind = CPTK_IS_POD;
10157 break;
10158 case RID_IS_POLYMORPHIC:
10159 kind = CPTK_IS_POLYMORPHIC;
10160 break;
10161 case RID_IS_SAME_AS:
10162 kind = CPTK_IS_SAME_AS;
10163 binary = true;
10164 break;
10165 case RID_IS_STD_LAYOUT:
10166 kind = CPTK_IS_STD_LAYOUT;
10167 break;
10168 case RID_IS_TRIVIAL:
10169 kind = CPTK_IS_TRIVIAL;
10170 break;
10171 case RID_IS_TRIVIALLY_ASSIGNABLE:
10172 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10173 binary = true;
10174 break;
10175 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10176 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10177 variadic = true;
10178 break;
10179 case RID_IS_TRIVIALLY_COPYABLE:
10180 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10181 break;
10182 case RID_IS_UNION:
10183 kind = CPTK_IS_UNION;
10184 break;
10185 case RID_UNDERLYING_TYPE:
10186 kind = CPTK_UNDERLYING_TYPE;
10187 break;
10188 case RID_BASES:
10189 kind = CPTK_BASES;
10190 break;
10191 case RID_DIRECT_BASES:
10192 kind = CPTK_DIRECT_BASES;
10193 break;
10194 case RID_IS_ASSIGNABLE:
10195 kind = CPTK_IS_ASSIGNABLE;
10196 binary = true;
10197 break;
10198 case RID_IS_CONSTRUCTIBLE:
10199 kind = CPTK_IS_CONSTRUCTIBLE;
10200 variadic = true;
10201 break;
10202 default:
10203 gcc_unreachable ();
10206 /* Get location of initial token. */
10207 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10209 /* Consume the token. */
10210 cp_lexer_consume_token (parser->lexer);
10212 matching_parens parens;
10213 parens.require_open (parser);
10216 type_id_in_expr_sentinel s (parser);
10217 type1 = cp_parser_type_id (parser);
10220 if (type1 == error_mark_node)
10221 return error_mark_node;
10223 if (binary)
10225 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10228 type_id_in_expr_sentinel s (parser);
10229 type2 = cp_parser_type_id (parser);
10232 if (type2 == error_mark_node)
10233 return error_mark_node;
10235 else if (variadic)
10237 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10239 cp_lexer_consume_token (parser->lexer);
10240 tree elt = cp_parser_type_id (parser);
10241 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10243 cp_lexer_consume_token (parser->lexer);
10244 elt = make_pack_expansion (elt);
10246 if (elt == error_mark_node)
10247 return error_mark_node;
10248 type2 = tree_cons (NULL_TREE, elt, type2);
10252 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10253 parens.require_close (parser);
10255 /* Construct a location of the form:
10256 __is_trivially_copyable(_Tp)
10257 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10258 with start == caret, finishing at the close-paren. */
10259 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10261 /* Complete the trait expression, which may mean either processing
10262 the trait expr now or saving it for template instantiation. */
10263 switch (kind)
10265 case CPTK_UNDERLYING_TYPE:
10266 return cp_expr (finish_underlying_type (type1), trait_loc);
10267 case CPTK_BASES:
10268 return cp_expr (finish_bases (type1, false), trait_loc);
10269 case CPTK_DIRECT_BASES:
10270 return cp_expr (finish_bases (type1, true), trait_loc);
10271 default:
10272 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10276 /* Parse a lambda expression.
10278 lambda-expression:
10279 lambda-introducer lambda-declarator [opt] compound-statement
10281 Returns a representation of the expression. */
10283 static cp_expr
10284 cp_parser_lambda_expression (cp_parser* parser)
10286 tree lambda_expr = build_lambda_expr ();
10287 tree type;
10288 bool ok = true;
10289 cp_token *token = cp_lexer_peek_token (parser->lexer);
10290 cp_token_position start = 0;
10292 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10294 if (cxx_dialect >= cxx2a)
10295 /* C++20 allows lambdas in unevaluated context. */;
10296 else if (cp_unevaluated_operand)
10298 if (!token->error_reported)
10300 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10301 "lambda-expression in unevaluated context"
10302 " only available with -std=c++2a or -std=gnu++2a");
10303 token->error_reported = true;
10305 ok = false;
10307 else if (parser->in_template_argument_list_p)
10309 if (!token->error_reported)
10311 error_at (token->location, "lambda-expression in template-argument"
10312 " only available with -std=c++2a or -std=gnu++2a");
10313 token->error_reported = true;
10315 ok = false;
10318 /* We may be in the middle of deferred access check. Disable
10319 it now. */
10320 push_deferring_access_checks (dk_no_deferred);
10322 cp_parser_lambda_introducer (parser, lambda_expr);
10323 if (cp_parser_error_occurred (parser))
10324 return error_mark_node;
10326 type = begin_lambda_type (lambda_expr);
10327 if (type == error_mark_node)
10328 return error_mark_node;
10330 record_lambda_scope (lambda_expr);
10332 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10333 determine_visibility (TYPE_NAME (type));
10335 /* Now that we've started the type, add the capture fields for any
10336 explicit captures. */
10337 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10340 /* Inside the class, surrounding template-parameter-lists do not apply. */
10341 unsigned int saved_num_template_parameter_lists
10342 = parser->num_template_parameter_lists;
10343 unsigned char in_statement = parser->in_statement;
10344 bool in_switch_statement_p = parser->in_switch_statement_p;
10345 bool fully_implicit_function_template_p
10346 = parser->fully_implicit_function_template_p;
10347 tree implicit_template_parms = parser->implicit_template_parms;
10348 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10349 bool auto_is_implicit_function_template_parm_p
10350 = parser->auto_is_implicit_function_template_parm_p;
10352 parser->num_template_parameter_lists = 0;
10353 parser->in_statement = 0;
10354 parser->in_switch_statement_p = false;
10355 parser->fully_implicit_function_template_p = false;
10356 parser->implicit_template_parms = 0;
10357 parser->implicit_template_scope = 0;
10358 parser->auto_is_implicit_function_template_parm_p = false;
10360 /* By virtue of defining a local class, a lambda expression has access to
10361 the private variables of enclosing classes. */
10363 if (cp_parser_start_tentative_firewall (parser))
10364 start = token;
10366 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10368 if (ok && cp_parser_error_occurred (parser))
10369 ok = false;
10371 if (ok)
10373 cp_parser_lambda_body (parser, lambda_expr);
10375 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10377 if (cp_parser_skip_to_closing_brace (parser))
10378 cp_lexer_consume_token (parser->lexer);
10381 /* The capture list was built up in reverse order; fix that now. */
10382 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10383 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10385 if (ok)
10386 maybe_add_lambda_conv_op (type);
10388 type = finish_struct (type, /*attributes=*/NULL_TREE);
10390 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10391 parser->in_statement = in_statement;
10392 parser->in_switch_statement_p = in_switch_statement_p;
10393 parser->fully_implicit_function_template_p
10394 = fully_implicit_function_template_p;
10395 parser->implicit_template_parms = implicit_template_parms;
10396 parser->implicit_template_scope = implicit_template_scope;
10397 parser->auto_is_implicit_function_template_parm_p
10398 = auto_is_implicit_function_template_parm_p;
10401 /* This field is only used during parsing of the lambda. */
10402 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10404 /* This lambda shouldn't have any proxies left at this point. */
10405 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10406 /* And now that we're done, push proxies for an enclosing lambda. */
10407 insert_pending_capture_proxies ();
10409 /* Update the lambda expression to a range. */
10410 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10411 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10412 token->location,
10413 end_tok->location);
10415 if (ok)
10416 lambda_expr = build_lambda_object (lambda_expr);
10417 else
10418 lambda_expr = error_mark_node;
10420 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10422 pop_deferring_access_checks ();
10424 return lambda_expr;
10427 /* Parse the beginning of a lambda expression.
10429 lambda-introducer:
10430 [ lambda-capture [opt] ]
10432 LAMBDA_EXPR is the current representation of the lambda expression. */
10434 static void
10435 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10437 /* Need commas after the first capture. */
10438 bool first = true;
10440 /* Eat the leading `['. */
10441 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10443 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10444 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10445 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10446 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10447 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10448 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10450 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10452 cp_lexer_consume_token (parser->lexer);
10453 first = false;
10455 if (!(at_function_scope_p () || parsing_nsdmi ()))
10456 error ("non-local lambda expression cannot have a capture-default");
10459 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10461 cp_token* capture_token;
10462 tree capture_id;
10463 tree capture_init_expr;
10464 cp_id_kind idk = CP_ID_KIND_NONE;
10465 bool explicit_init_p = false;
10467 enum capture_kind_type
10469 BY_COPY,
10470 BY_REFERENCE
10472 enum capture_kind_type capture_kind = BY_COPY;
10474 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10476 error ("expected end of capture-list");
10477 return;
10480 if (first)
10481 first = false;
10482 else
10483 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10485 /* Possibly capture `this'. */
10486 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10488 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10489 if (cxx_dialect < cxx2a
10490 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10491 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10492 "with by-copy capture default");
10493 cp_lexer_consume_token (parser->lexer);
10494 add_capture (lambda_expr,
10495 /*id=*/this_identifier,
10496 /*initializer=*/finish_this_expr (),
10497 /*by_reference_p=*/true,
10498 explicit_init_p);
10499 continue;
10502 /* Possibly capture `*this'. */
10503 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10504 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10506 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10507 if (cxx_dialect < cxx17)
10508 pedwarn (loc, 0, "%<*this%> capture only available with "
10509 "-std=c++17 or -std=gnu++17");
10510 cp_lexer_consume_token (parser->lexer);
10511 cp_lexer_consume_token (parser->lexer);
10512 add_capture (lambda_expr,
10513 /*id=*/this_identifier,
10514 /*initializer=*/finish_this_expr (),
10515 /*by_reference_p=*/false,
10516 explicit_init_p);
10517 continue;
10520 bool init_pack_expansion = false;
10521 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10523 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10524 if (cxx_dialect < cxx2a)
10525 pedwarn (loc, 0, "pack init-capture only available with "
10526 "-std=c++2a or -std=gnu++2a");
10527 cp_lexer_consume_token (parser->lexer);
10528 init_pack_expansion = true;
10531 /* Remember whether we want to capture as a reference or not. */
10532 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10534 capture_kind = BY_REFERENCE;
10535 cp_lexer_consume_token (parser->lexer);
10538 /* Get the identifier. */
10539 capture_token = cp_lexer_peek_token (parser->lexer);
10540 capture_id = cp_parser_identifier (parser);
10542 if (capture_id == error_mark_node)
10543 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10544 delimiters, but I modified this to stop on unnested ']' as well. It
10545 was already changed to stop on unnested '}', so the
10546 "closing_parenthesis" name is no more misleading with my change. */
10548 cp_parser_skip_to_closing_parenthesis (parser,
10549 /*recovering=*/true,
10550 /*or_comma=*/true,
10551 /*consume_paren=*/true);
10552 break;
10555 /* Find the initializer for this capture. */
10556 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10557 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10558 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10560 bool direct, non_constant;
10561 /* An explicit initializer exists. */
10562 if (cxx_dialect < cxx14)
10563 pedwarn (input_location, 0,
10564 "lambda capture initializers "
10565 "only available with -std=c++14 or -std=gnu++14");
10566 capture_init_expr = cp_parser_initializer (parser, &direct,
10567 &non_constant, true);
10568 explicit_init_p = true;
10569 if (capture_init_expr == NULL_TREE)
10571 error ("empty initializer for lambda init-capture");
10572 capture_init_expr = error_mark_node;
10574 if (init_pack_expansion)
10575 capture_init_expr = make_pack_expansion (capture_init_expr);
10577 else
10579 const char* error_msg;
10581 /* Turn the identifier into an id-expression. */
10582 capture_init_expr
10583 = cp_parser_lookup_name_simple (parser, capture_id,
10584 capture_token->location);
10586 if (capture_init_expr == error_mark_node)
10588 unqualified_name_lookup_error (capture_id);
10589 continue;
10591 else if (!VAR_P (capture_init_expr)
10592 && TREE_CODE (capture_init_expr) != PARM_DECL)
10594 error_at (capture_token->location,
10595 "capture of non-variable %qE",
10596 capture_init_expr);
10597 if (DECL_P (capture_init_expr))
10598 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10599 "%q#D declared here", capture_init_expr);
10600 continue;
10602 if (VAR_P (capture_init_expr)
10603 && decl_storage_duration (capture_init_expr) != dk_auto)
10605 if (pedwarn (capture_token->location, 0, "capture of variable "
10606 "%qD with non-automatic storage duration",
10607 capture_init_expr))
10608 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10609 "%q#D declared here", capture_init_expr);
10610 continue;
10613 capture_init_expr
10614 = finish_id_expression
10615 (capture_id,
10616 capture_init_expr,
10617 parser->scope,
10618 &idk,
10619 /*integral_constant_expression_p=*/false,
10620 /*allow_non_integral_constant_expression_p=*/false,
10621 /*non_integral_constant_expression_p=*/NULL,
10622 /*template_p=*/false,
10623 /*done=*/true,
10624 /*address_p=*/false,
10625 /*template_arg_p=*/false,
10626 &error_msg,
10627 capture_token->location);
10629 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10631 cp_lexer_consume_token (parser->lexer);
10632 capture_init_expr = make_pack_expansion (capture_init_expr);
10636 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10637 && !explicit_init_p)
10639 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10640 && capture_kind == BY_COPY)
10641 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10642 "of %qD redundant with by-copy capture default",
10643 capture_id);
10644 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10645 && capture_kind == BY_REFERENCE)
10646 pedwarn (capture_token->location, 0, "explicit by-reference "
10647 "capture of %qD redundant with by-reference capture "
10648 "default", capture_id);
10651 add_capture (lambda_expr,
10652 capture_id,
10653 capture_init_expr,
10654 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10655 explicit_init_p);
10657 /* If there is any qualification still in effect, clear it
10658 now; we will be starting fresh with the next capture. */
10659 parser->scope = NULL_TREE;
10660 parser->qualifying_scope = NULL_TREE;
10661 parser->object_scope = NULL_TREE;
10664 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10667 /* Parse the (optional) middle of a lambda expression.
10669 lambda-declarator:
10670 < template-parameter-list [opt] >
10671 ( parameter-declaration-clause [opt] )
10672 attribute-specifier [opt]
10673 decl-specifier-seq [opt]
10674 exception-specification [opt]
10675 lambda-return-type-clause [opt]
10677 LAMBDA_EXPR is the current representation of the lambda expression. */
10679 static bool
10680 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10682 /* 5.1.1.4 of the standard says:
10683 If a lambda-expression does not include a lambda-declarator, it is as if
10684 the lambda-declarator were ().
10685 This means an empty parameter list, no attributes, and no exception
10686 specification. */
10687 tree param_list = void_list_node;
10688 tree attributes = NULL_TREE;
10689 tree exception_spec = NULL_TREE;
10690 tree template_param_list = NULL_TREE;
10691 tree tx_qual = NULL_TREE;
10692 tree return_type = NULL_TREE;
10693 cp_decl_specifier_seq lambda_specs;
10694 clear_decl_specs (&lambda_specs);
10696 /* The template-parameter-list is optional, but must begin with
10697 an opening angle if present. */
10698 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10700 if (cxx_dialect < cxx14)
10701 pedwarn (parser->lexer->next_token->location, 0,
10702 "lambda templates are only available with "
10703 "-std=c++14 or -std=gnu++14");
10704 else if (cxx_dialect < cxx2a)
10705 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10706 "lambda templates are only available with "
10707 "-std=c++2a or -std=gnu++2a");
10709 cp_lexer_consume_token (parser->lexer);
10711 template_param_list = cp_parser_template_parameter_list (parser);
10713 cp_parser_skip_to_end_of_template_parameter_list (parser);
10715 /* We just processed one more parameter list. */
10716 ++parser->num_template_parameter_lists;
10719 /* The parameter-declaration-clause is optional (unless
10720 template-parameter-list was given), but must begin with an
10721 opening parenthesis if present. */
10722 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10724 matching_parens parens;
10725 parens.consume_open (parser);
10727 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10729 /* Parse parameters. */
10730 param_list = cp_parser_parameter_declaration_clause (parser);
10732 /* Default arguments shall not be specified in the
10733 parameter-declaration-clause of a lambda-declarator. */
10734 if (cxx_dialect < cxx14)
10735 for (tree t = param_list; t; t = TREE_CHAIN (t))
10736 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10737 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10738 "default argument specified for lambda parameter");
10740 parens.require_close (parser);
10742 /* In the decl-specifier-seq of the lambda-declarator, each
10743 decl-specifier shall either be mutable or constexpr. */
10744 int declares_class_or_enum;
10745 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10746 cp_parser_decl_specifier_seq (parser,
10747 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10748 &lambda_specs, &declares_class_or_enum);
10749 if (lambda_specs.storage_class == sc_mutable)
10751 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10752 if (lambda_specs.conflicting_specifiers_p)
10753 error_at (lambda_specs.locations[ds_storage_class],
10754 "duplicate %<mutable%>");
10757 tx_qual = cp_parser_tx_qualifier_opt (parser);
10759 /* Parse optional exception specification. */
10760 exception_spec = cp_parser_exception_specification_opt (parser);
10762 attributes = cp_parser_std_attribute_spec_seq (parser);
10764 /* Parse optional trailing return type. */
10765 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10767 cp_lexer_consume_token (parser->lexer);
10768 return_type = cp_parser_trailing_type_id (parser);
10771 /* The function parameters must be in scope all the way until after the
10772 trailing-return-type in case of decltype. */
10773 pop_bindings_and_leave_scope ();
10775 else if (template_param_list != NULL_TREE) // generate diagnostic
10776 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10778 /* Create the function call operator.
10780 Messing with declarators like this is no uglier than building up the
10781 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10782 other code. */
10784 cp_decl_specifier_seq return_type_specs;
10785 cp_declarator* declarator;
10786 tree fco;
10787 int quals;
10788 void *p;
10790 clear_decl_specs (&return_type_specs);
10791 return_type_specs.type = make_auto ();
10793 if (lambda_specs.locations[ds_constexpr])
10795 if (cxx_dialect >= cxx17)
10796 return_type_specs.locations[ds_constexpr]
10797 = lambda_specs.locations[ds_constexpr];
10798 else
10799 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10800 "lambda only available with -std=c++17 or -std=gnu++17");
10803 p = obstack_alloc (&declarator_obstack, 0);
10805 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
10806 LAMBDA_EXPR_LOCATION (lambda_expr));
10808 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10809 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10810 declarator = make_call_declarator (declarator, param_list, quals,
10811 VIRT_SPEC_UNSPECIFIED,
10812 REF_QUAL_NONE,
10813 tx_qual,
10814 exception_spec,
10815 return_type,
10816 /*requires_clause*/NULL_TREE);
10817 declarator->std_attributes = attributes;
10819 fco = grokmethod (&return_type_specs,
10820 declarator,
10821 NULL_TREE);
10822 if (fco != error_mark_node)
10824 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10825 DECL_ARTIFICIAL (fco) = 1;
10826 /* Give the object parameter a different name. */
10827 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10828 DECL_LAMBDA_FUNCTION (fco) = 1;
10830 if (template_param_list)
10832 fco = finish_member_template_decl (fco);
10833 finish_template_decl (template_param_list);
10834 --parser->num_template_parameter_lists;
10836 else if (parser->fully_implicit_function_template_p)
10837 fco = finish_fully_implicit_template (parser, fco);
10839 finish_member_declaration (fco);
10841 obstack_free (&declarator_obstack, p);
10843 return (fco != error_mark_node);
10847 /* Parse the body of a lambda expression, which is simply
10849 compound-statement
10851 but which requires special handling.
10852 LAMBDA_EXPR is the current representation of the lambda expression. */
10854 static void
10855 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10857 bool nested = (current_function_decl != NULL_TREE);
10858 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10859 bool in_function_body = parser->in_function_body;
10861 /* The body of a lambda-expression is not a subexpression of the enclosing
10862 expression. */
10863 cp_evaluated ev;
10865 if (nested)
10866 push_function_context ();
10867 else
10868 /* Still increment function_depth so that we don't GC in the
10869 middle of an expression. */
10870 ++function_depth;
10872 vec<tree> omp_privatization_save;
10873 save_omp_privatization_clauses (omp_privatization_save);
10874 /* Clear this in case we're in the middle of a default argument. */
10875 parser->local_variables_forbidden_p = false;
10876 parser->in_function_body = true;
10879 local_specialization_stack s (lss_copy);
10880 tree fco = lambda_function (lambda_expr);
10881 tree body = start_lambda_function (fco, lambda_expr);
10882 matching_braces braces;
10884 if (braces.require_open (parser))
10886 tree compound_stmt = begin_compound_stmt (0);
10888 /* Originally C++11 required us to peek for 'return expr'; and
10889 process it specially here to deduce the return type. N3638
10890 removed the need for that. */
10892 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10893 cp_parser_label_declaration (parser);
10894 cp_parser_statement_seq_opt (parser, NULL_TREE);
10895 braces.require_close (parser);
10897 finish_compound_stmt (compound_stmt);
10900 finish_lambda_function (body);
10903 restore_omp_privatization_clauses (omp_privatization_save);
10904 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10905 parser->in_function_body = in_function_body;
10906 if (nested)
10907 pop_function_context();
10908 else
10909 --function_depth;
10912 /* Statements [gram.stmt.stmt] */
10914 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10916 static void
10917 add_debug_begin_stmt (location_t loc)
10919 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10920 return;
10921 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10922 /* A concept is never expanded normally. */
10923 return;
10925 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10926 SET_EXPR_LOCATION (stmt, loc);
10927 add_stmt (stmt);
10930 /* Parse a statement.
10932 statement:
10933 labeled-statement
10934 expression-statement
10935 compound-statement
10936 selection-statement
10937 iteration-statement
10938 jump-statement
10939 declaration-statement
10940 try-block
10942 C++11:
10944 statement:
10945 labeled-statement
10946 attribute-specifier-seq (opt) expression-statement
10947 attribute-specifier-seq (opt) compound-statement
10948 attribute-specifier-seq (opt) selection-statement
10949 attribute-specifier-seq (opt) iteration-statement
10950 attribute-specifier-seq (opt) jump-statement
10951 declaration-statement
10952 attribute-specifier-seq (opt) try-block
10954 init-statement:
10955 expression-statement
10956 simple-declaration
10958 TM Extension:
10960 statement:
10961 atomic-statement
10963 IN_COMPOUND is true when the statement is nested inside a
10964 cp_parser_compound_statement; this matters for certain pragmas.
10966 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10967 is a (possibly labeled) if statement which is not enclosed in braces
10968 and has an else clause. This is used to implement -Wparentheses.
10970 CHAIN is a vector of if-else-if conditions. */
10972 static void
10973 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10974 bool in_compound, bool *if_p, vec<tree> *chain,
10975 location_t *loc_after_labels)
10977 tree statement, std_attrs = NULL_TREE;
10978 cp_token *token;
10979 location_t statement_location, attrs_location;
10981 restart:
10982 if (if_p != NULL)
10983 *if_p = false;
10984 /* There is no statement yet. */
10985 statement = NULL_TREE;
10987 saved_token_sentinel saved_tokens (parser->lexer);
10988 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10989 if (c_dialect_objc ())
10990 /* In obj-c++, seeing '[[' might be the either the beginning of
10991 c++11 attributes, or a nested objc-message-expression. So
10992 let's parse the c++11 attributes tentatively. */
10993 cp_parser_parse_tentatively (parser);
10994 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10995 if (c_dialect_objc ())
10997 if (!cp_parser_parse_definitely (parser))
10998 std_attrs = NULL_TREE;
11001 /* Peek at the next token. */
11002 token = cp_lexer_peek_token (parser->lexer);
11003 /* Remember the location of the first token in the statement. */
11004 cp_token *statement_token = token;
11005 statement_location = token->location;
11006 add_debug_begin_stmt (statement_location);
11007 /* If this is a keyword, then that will often determine what kind of
11008 statement we have. */
11009 if (token->type == CPP_KEYWORD)
11011 enum rid keyword = token->keyword;
11013 switch (keyword)
11015 case RID_CASE:
11016 case RID_DEFAULT:
11017 /* Looks like a labeled-statement with a case label.
11018 Parse the label, and then use tail recursion to parse
11019 the statement. */
11020 cp_parser_label_for_labeled_statement (parser, std_attrs);
11021 in_compound = false;
11022 goto restart;
11024 case RID_IF:
11025 case RID_SWITCH:
11026 std_attrs = process_stmt_hotness_attribute (std_attrs);
11027 statement = cp_parser_selection_statement (parser, if_p, chain);
11028 break;
11030 case RID_WHILE:
11031 case RID_DO:
11032 case RID_FOR:
11033 std_attrs = process_stmt_hotness_attribute (std_attrs);
11034 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11035 break;
11037 case RID_BREAK:
11038 case RID_CONTINUE:
11039 case RID_RETURN:
11040 case RID_GOTO:
11041 std_attrs = process_stmt_hotness_attribute (std_attrs);
11042 statement = cp_parser_jump_statement (parser);
11043 break;
11045 /* Objective-C++ exception-handling constructs. */
11046 case RID_AT_TRY:
11047 case RID_AT_CATCH:
11048 case RID_AT_FINALLY:
11049 case RID_AT_SYNCHRONIZED:
11050 case RID_AT_THROW:
11051 std_attrs = process_stmt_hotness_attribute (std_attrs);
11052 statement = cp_parser_objc_statement (parser);
11053 break;
11055 case RID_TRY:
11056 std_attrs = process_stmt_hotness_attribute (std_attrs);
11057 statement = cp_parser_try_block (parser);
11058 break;
11060 case RID_NAMESPACE:
11061 /* This must be a namespace alias definition. */
11062 if (std_attrs != NULL_TREE)
11064 /* Attributes should be parsed as part of the the
11065 declaration, so let's un-parse them. */
11066 saved_tokens.rollback();
11067 std_attrs = NULL_TREE;
11069 cp_parser_declaration_statement (parser);
11070 return;
11072 case RID_TRANSACTION_ATOMIC:
11073 case RID_TRANSACTION_RELAXED:
11074 case RID_SYNCHRONIZED:
11075 case RID_ATOMIC_NOEXCEPT:
11076 case RID_ATOMIC_CANCEL:
11077 std_attrs = process_stmt_hotness_attribute (std_attrs);
11078 statement = cp_parser_transaction (parser, token);
11079 break;
11080 case RID_TRANSACTION_CANCEL:
11081 std_attrs = process_stmt_hotness_attribute (std_attrs);
11082 statement = cp_parser_transaction_cancel (parser);
11083 break;
11085 default:
11086 /* It might be a keyword like `int' that can start a
11087 declaration-statement. */
11088 break;
11091 else if (token->type == CPP_NAME)
11093 /* If the next token is a `:', then we are looking at a
11094 labeled-statement. */
11095 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11096 if (token->type == CPP_COLON)
11098 /* Looks like a labeled-statement with an ordinary label.
11099 Parse the label, and then use tail recursion to parse
11100 the statement. */
11102 cp_parser_label_for_labeled_statement (parser, std_attrs);
11103 in_compound = false;
11104 goto restart;
11107 /* Anything that starts with a `{' must be a compound-statement. */
11108 else if (token->type == CPP_OPEN_BRACE)
11109 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11110 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11111 a statement all its own. */
11112 else if (token->type == CPP_PRAGMA)
11114 /* Only certain OpenMP pragmas are attached to statements, and thus
11115 are considered statements themselves. All others are not. In
11116 the context of a compound, accept the pragma as a "statement" and
11117 return so that we can check for a close brace. Otherwise we
11118 require a real statement and must go back and read one. */
11119 if (in_compound)
11120 cp_parser_pragma (parser, pragma_compound, if_p);
11121 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11122 goto restart;
11123 return;
11125 else if (token->type == CPP_EOF)
11127 cp_parser_error (parser, "expected statement");
11128 return;
11131 /* Everything else must be a declaration-statement or an
11132 expression-statement. Try for the declaration-statement
11133 first, unless we are looking at a `;', in which case we know that
11134 we have an expression-statement. */
11135 if (!statement)
11137 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11139 if (std_attrs != NULL_TREE)
11140 /* Attributes should be parsed as part of the declaration,
11141 so let's un-parse them. */
11142 saved_tokens.rollback();
11144 cp_parser_parse_tentatively (parser);
11145 /* Try to parse the declaration-statement. */
11146 cp_parser_declaration_statement (parser);
11147 /* If that worked, we're done. */
11148 if (cp_parser_parse_definitely (parser))
11149 return;
11150 /* It didn't work, restore the post-attribute position. */
11151 if (std_attrs)
11152 cp_lexer_set_token_position (parser->lexer, statement_token);
11154 /* All preceding labels have been parsed at this point. */
11155 if (loc_after_labels != NULL)
11156 *loc_after_labels = statement_location;
11158 std_attrs = process_stmt_hotness_attribute (std_attrs);
11160 /* Look for an expression-statement instead. */
11161 statement = cp_parser_expression_statement (parser, in_statement_expr);
11163 /* Handle [[fallthrough]];. */
11164 if (attribute_fallthrough_p (std_attrs))
11166 /* The next token after the fallthrough attribute is ';'. */
11167 if (statement == NULL_TREE)
11169 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11170 statement = build_call_expr_internal_loc (statement_location,
11171 IFN_FALLTHROUGH,
11172 void_type_node, 0);
11173 finish_expr_stmt (statement);
11175 else
11176 warning_at (statement_location, OPT_Wattributes,
11177 "%<fallthrough%> attribute not followed by %<;%>");
11178 std_attrs = NULL_TREE;
11182 /* Set the line number for the statement. */
11183 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11184 SET_EXPR_LOCATION (statement, statement_location);
11186 /* Allow "[[fallthrough]];", but warn otherwise. */
11187 if (std_attrs != NULL_TREE)
11188 warning_at (attrs_location,
11189 OPT_Wattributes,
11190 "attributes at the beginning of statement are ignored");
11193 /* Append ATTR to attribute list ATTRS. */
11195 static tree
11196 attr_chainon (tree attrs, tree attr)
11198 if (attrs == error_mark_node)
11199 return error_mark_node;
11200 if (attr == error_mark_node)
11201 return error_mark_node;
11202 return chainon (attrs, attr);
11205 /* Parse the label for a labeled-statement, i.e.
11207 identifier :
11208 case constant-expression :
11209 default :
11211 GNU Extension:
11212 case constant-expression ... constant-expression : statement
11214 When a label is parsed without errors, the label is added to the
11215 parse tree by the finish_* functions, so this function doesn't
11216 have to return the label. */
11218 static void
11219 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11221 cp_token *token;
11222 tree label = NULL_TREE;
11223 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11225 /* The next token should be an identifier. */
11226 token = cp_lexer_peek_token (parser->lexer);
11227 if (token->type != CPP_NAME
11228 && token->type != CPP_KEYWORD)
11230 cp_parser_error (parser, "expected labeled-statement");
11231 return;
11234 /* Remember whether this case or a user-defined label is allowed to fall
11235 through to. */
11236 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11238 parser->colon_corrects_to_scope_p = false;
11239 switch (token->keyword)
11241 case RID_CASE:
11243 tree expr, expr_hi;
11244 cp_token *ellipsis;
11246 /* Consume the `case' token. */
11247 cp_lexer_consume_token (parser->lexer);
11248 /* Parse the constant-expression. */
11249 expr = cp_parser_constant_expression (parser);
11250 if (check_for_bare_parameter_packs (expr))
11251 expr = error_mark_node;
11253 ellipsis = cp_lexer_peek_token (parser->lexer);
11254 if (ellipsis->type == CPP_ELLIPSIS)
11256 /* Consume the `...' token. */
11257 cp_lexer_consume_token (parser->lexer);
11258 expr_hi = cp_parser_constant_expression (parser);
11259 if (check_for_bare_parameter_packs (expr_hi))
11260 expr_hi = error_mark_node;
11262 /* We don't need to emit warnings here, as the common code
11263 will do this for us. */
11265 else
11266 expr_hi = NULL_TREE;
11268 if (parser->in_switch_statement_p)
11270 tree l = finish_case_label (token->location, expr, expr_hi);
11271 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11273 label = CASE_LABEL (l);
11274 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11277 else
11278 error_at (token->location,
11279 "case label %qE not within a switch statement",
11280 expr);
11282 break;
11284 case RID_DEFAULT:
11285 /* Consume the `default' token. */
11286 cp_lexer_consume_token (parser->lexer);
11288 if (parser->in_switch_statement_p)
11290 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11291 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11293 label = CASE_LABEL (l);
11294 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11297 else
11298 error_at (token->location, "case label not within a switch statement");
11299 break;
11301 default:
11302 /* Anything else must be an ordinary label. */
11303 label = finish_label_stmt (cp_parser_identifier (parser));
11304 if (label && TREE_CODE (label) == LABEL_DECL)
11305 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11306 break;
11309 /* Require the `:' token. */
11310 cp_parser_require (parser, CPP_COLON, RT_COLON);
11312 /* An ordinary label may optionally be followed by attributes.
11313 However, this is only permitted if the attributes are then
11314 followed by a semicolon. This is because, for backward
11315 compatibility, when parsing
11316 lab: __attribute__ ((unused)) int i;
11317 we want the attribute to attach to "i", not "lab". */
11318 if (label != NULL_TREE
11319 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11321 tree attrs;
11322 cp_parser_parse_tentatively (parser);
11323 attrs = cp_parser_gnu_attributes_opt (parser);
11324 if (attrs == NULL_TREE
11325 /* And fallthrough always binds to the expression-statement. */
11326 || attribute_fallthrough_p (attrs)
11327 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11328 cp_parser_abort_tentative_parse (parser);
11329 else if (!cp_parser_parse_definitely (parser))
11331 else
11332 attributes = attr_chainon (attributes, attrs);
11335 if (attributes != NULL_TREE)
11336 cplus_decl_attributes (&label, attributes, 0);
11338 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11341 /* Parse an expression-statement.
11343 expression-statement:
11344 expression [opt] ;
11346 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11347 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11348 indicates whether this expression-statement is part of an
11349 expression statement. */
11351 static tree
11352 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11354 tree statement = NULL_TREE;
11355 cp_token *token = cp_lexer_peek_token (parser->lexer);
11356 location_t loc = token->location;
11358 /* There might be attribute fallthrough. */
11359 tree attr = cp_parser_gnu_attributes_opt (parser);
11361 /* If the next token is a ';', then there is no expression
11362 statement. */
11363 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11365 statement = cp_parser_expression (parser);
11366 if (statement == error_mark_node
11367 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11369 cp_parser_skip_to_end_of_block_or_statement (parser);
11370 return error_mark_node;
11374 /* Handle [[fallthrough]];. */
11375 if (attribute_fallthrough_p (attr))
11377 /* The next token after the fallthrough attribute is ';'. */
11378 if (statement == NULL_TREE)
11379 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11380 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11381 void_type_node, 0);
11382 else
11383 warning_at (loc, OPT_Wattributes,
11384 "%<fallthrough%> attribute not followed by %<;%>");
11385 attr = NULL_TREE;
11388 /* Allow "[[fallthrough]];", but warn otherwise. */
11389 if (attr != NULL_TREE)
11390 warning_at (loc, OPT_Wattributes,
11391 "attributes at the beginning of statement are ignored");
11393 /* Give a helpful message for "A<T>::type t;" and the like. */
11394 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11395 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11397 if (TREE_CODE (statement) == SCOPE_REF)
11398 error_at (token->location, "need %<typename%> before %qE because "
11399 "%qT is a dependent scope",
11400 statement, TREE_OPERAND (statement, 0));
11401 else if (is_overloaded_fn (statement)
11402 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11404 /* A::A a; */
11405 tree fn = get_first_fn (statement);
11406 error_at (token->location,
11407 "%<%T::%D%> names the constructor, not the type",
11408 DECL_CONTEXT (fn), DECL_NAME (fn));
11412 /* Consume the final `;'. */
11413 cp_parser_consume_semicolon_at_end_of_statement (parser);
11415 if (in_statement_expr
11416 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11417 /* This is the final expression statement of a statement
11418 expression. */
11419 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11420 else if (statement)
11421 statement = finish_expr_stmt (statement);
11423 return statement;
11426 /* Parse a compound-statement.
11428 compound-statement:
11429 { statement-seq [opt] }
11431 GNU extension:
11433 compound-statement:
11434 { label-declaration-seq [opt] statement-seq [opt] }
11436 label-declaration-seq:
11437 label-declaration
11438 label-declaration-seq label-declaration
11440 Returns a tree representing the statement. */
11442 static tree
11443 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11444 int bcs_flags, bool function_body)
11446 tree compound_stmt;
11447 matching_braces braces;
11449 /* Consume the `{'. */
11450 if (!braces.require_open (parser))
11451 return error_mark_node;
11452 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11453 && !function_body && cxx_dialect < cxx14)
11454 pedwarn (input_location, OPT_Wpedantic,
11455 "compound-statement in %<constexpr%> function");
11456 /* Begin the compound-statement. */
11457 compound_stmt = begin_compound_stmt (bcs_flags);
11458 /* If the next keyword is `__label__' we have a label declaration. */
11459 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11460 cp_parser_label_declaration (parser);
11461 /* Parse an (optional) statement-seq. */
11462 cp_parser_statement_seq_opt (parser, in_statement_expr);
11463 /* Finish the compound-statement. */
11464 finish_compound_stmt (compound_stmt);
11465 /* Consume the `}'. */
11466 braces.require_close (parser);
11468 return compound_stmt;
11471 /* Parse an (optional) statement-seq.
11473 statement-seq:
11474 statement
11475 statement-seq [opt] statement */
11477 static void
11478 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11480 /* Scan statements until there aren't any more. */
11481 while (true)
11483 cp_token *token = cp_lexer_peek_token (parser->lexer);
11485 /* If we are looking at a `}', then we have run out of
11486 statements; the same is true if we have reached the end
11487 of file, or have stumbled upon a stray '@end'. */
11488 if (token->type == CPP_CLOSE_BRACE
11489 || token->type == CPP_EOF
11490 || token->type == CPP_PRAGMA_EOL
11491 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11492 break;
11494 /* If we are in a compound statement and find 'else' then
11495 something went wrong. */
11496 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11498 if (parser->in_statement & IN_IF_STMT)
11499 break;
11500 else
11502 token = cp_lexer_consume_token (parser->lexer);
11503 error_at (token->location, "%<else%> without a previous %<if%>");
11507 /* Parse the statement. */
11508 cp_parser_statement (parser, in_statement_expr, true, NULL);
11512 /* Return true if this is the C++20 version of range-based-for with
11513 init-statement. */
11515 static bool
11516 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11518 bool r = false;
11520 /* Save tokens so that we can put them back. */
11521 cp_lexer_save_tokens (parser->lexer);
11523 /* There has to be an unnested ; followed by an unnested :. */
11524 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11525 /*recovering=*/false,
11526 CPP_SEMICOLON,
11527 /*consume_paren=*/false) != -1)
11528 goto out;
11530 /* We found the semicolon, eat it now. */
11531 cp_lexer_consume_token (parser->lexer);
11533 /* Now look for ':' that is not nested in () or {}. */
11534 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11535 /*recovering=*/false,
11536 CPP_COLON,
11537 /*consume_paren=*/false) == -1);
11539 out:
11540 /* Roll back the tokens we skipped. */
11541 cp_lexer_rollback_tokens (parser->lexer);
11543 return r;
11546 /* Return true if we're looking at (init; cond), false otherwise. */
11548 static bool
11549 cp_parser_init_statement_p (cp_parser *parser)
11551 /* Save tokens so that we can put them back. */
11552 cp_lexer_save_tokens (parser->lexer);
11554 /* Look for ';' that is not nested in () or {}. */
11555 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11556 /*recovering=*/false,
11557 CPP_SEMICOLON,
11558 /*consume_paren=*/false);
11560 /* Roll back the tokens we skipped. */
11561 cp_lexer_rollback_tokens (parser->lexer);
11563 return ret == -1;
11566 /* Parse a selection-statement.
11568 selection-statement:
11569 if ( init-statement [opt] condition ) statement
11570 if ( init-statement [opt] condition ) statement else statement
11571 switch ( init-statement [opt] condition ) statement
11573 Returns the new IF_STMT or SWITCH_STMT.
11575 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11576 is a (possibly labeled) if statement which is not enclosed in
11577 braces and has an else clause. This is used to implement
11578 -Wparentheses.
11580 CHAIN is a vector of if-else-if conditions. This is used to implement
11581 -Wduplicated-cond. */
11583 static tree
11584 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11585 vec<tree> *chain)
11587 cp_token *token;
11588 enum rid keyword;
11589 token_indent_info guard_tinfo;
11591 if (if_p != NULL)
11592 *if_p = false;
11594 /* Peek at the next token. */
11595 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11596 guard_tinfo = get_token_indent_info (token);
11598 /* See what kind of keyword it is. */
11599 keyword = token->keyword;
11600 switch (keyword)
11602 case RID_IF:
11603 case RID_SWITCH:
11605 tree statement;
11606 tree condition;
11608 bool cx = false;
11609 if (keyword == RID_IF
11610 && cp_lexer_next_token_is_keyword (parser->lexer,
11611 RID_CONSTEXPR))
11613 cx = true;
11614 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11615 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11616 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11617 "with -std=c++17 or -std=gnu++17");
11620 /* Look for the `('. */
11621 matching_parens parens;
11622 if (!parens.require_open (parser))
11624 cp_parser_skip_to_end_of_statement (parser);
11625 return error_mark_node;
11628 /* Begin the selection-statement. */
11629 if (keyword == RID_IF)
11631 statement = begin_if_stmt ();
11632 IF_STMT_CONSTEXPR_P (statement) = cx;
11634 else
11635 statement = begin_switch_stmt ();
11637 /* Parse the optional init-statement. */
11638 if (cp_parser_init_statement_p (parser))
11640 tree decl;
11641 if (cxx_dialect < cxx17)
11642 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11643 "init-statement in selection statements only available "
11644 "with -std=c++17 or -std=gnu++17");
11645 cp_parser_init_statement (parser, &decl);
11648 /* Parse the condition. */
11649 condition = cp_parser_condition (parser);
11650 /* Look for the `)'. */
11651 if (!parens.require_close (parser))
11652 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11653 /*consume_paren=*/true);
11655 if (keyword == RID_IF)
11657 bool nested_if;
11658 unsigned char in_statement;
11660 /* Add the condition. */
11661 condition = finish_if_stmt_cond (condition, statement);
11663 if (warn_duplicated_cond)
11664 warn_duplicated_cond_add_or_warn (token->location, condition,
11665 &chain);
11667 /* Parse the then-clause. */
11668 in_statement = parser->in_statement;
11669 parser->in_statement |= IN_IF_STMT;
11671 /* Outside a template, the non-selected branch of a constexpr
11672 if is a 'discarded statement', i.e. unevaluated. */
11673 bool was_discarded = in_discarded_stmt;
11674 bool discard_then = (cx && !processing_template_decl
11675 && integer_zerop (condition));
11676 if (discard_then)
11678 in_discarded_stmt = true;
11679 ++c_inhibit_evaluation_warnings;
11682 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11683 guard_tinfo);
11685 parser->in_statement = in_statement;
11687 finish_then_clause (statement);
11689 if (discard_then)
11691 THEN_CLAUSE (statement) = NULL_TREE;
11692 in_discarded_stmt = was_discarded;
11693 --c_inhibit_evaluation_warnings;
11696 /* If the next token is `else', parse the else-clause. */
11697 if (cp_lexer_next_token_is_keyword (parser->lexer,
11698 RID_ELSE))
11700 bool discard_else = (cx && !processing_template_decl
11701 && integer_nonzerop (condition));
11702 if (discard_else)
11704 in_discarded_stmt = true;
11705 ++c_inhibit_evaluation_warnings;
11708 guard_tinfo
11709 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11710 /* Consume the `else' keyword. */
11711 cp_lexer_consume_token (parser->lexer);
11712 if (warn_duplicated_cond)
11714 if (cp_lexer_next_token_is_keyword (parser->lexer,
11715 RID_IF)
11716 && chain == NULL)
11718 /* We've got "if (COND) else if (COND2)". Start
11719 the condition chain and add COND as the first
11720 element. */
11721 chain = new vec<tree> ();
11722 if (!CONSTANT_CLASS_P (condition)
11723 && !TREE_SIDE_EFFECTS (condition))
11725 /* Wrap it in a NOP_EXPR so that we can set the
11726 location of the condition. */
11727 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11728 condition);
11729 SET_EXPR_LOCATION (e, token->location);
11730 chain->safe_push (e);
11733 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11734 RID_IF))
11736 /* This is if-else without subsequent if. Zap the
11737 condition chain; we would have already warned at
11738 this point. */
11739 delete chain;
11740 chain = NULL;
11743 begin_else_clause (statement);
11744 /* Parse the else-clause. */
11745 cp_parser_implicitly_scoped_statement (parser, NULL,
11746 guard_tinfo, chain);
11748 finish_else_clause (statement);
11750 /* If we are currently parsing a then-clause, then
11751 IF_P will not be NULL. We set it to true to
11752 indicate that this if statement has an else clause.
11753 This may trigger the Wparentheses warning below
11754 when we get back up to the parent if statement. */
11755 if (if_p != NULL)
11756 *if_p = true;
11758 if (discard_else)
11760 ELSE_CLAUSE (statement) = NULL_TREE;
11761 in_discarded_stmt = was_discarded;
11762 --c_inhibit_evaluation_warnings;
11765 else
11767 /* This if statement does not have an else clause. If
11768 NESTED_IF is true, then the then-clause has an if
11769 statement which does have an else clause. We warn
11770 about the potential ambiguity. */
11771 if (nested_if)
11772 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11773 "suggest explicit braces to avoid ambiguous"
11774 " %<else%>");
11775 if (warn_duplicated_cond)
11777 /* We don't need the condition chain anymore. */
11778 delete chain;
11779 chain = NULL;
11783 /* Now we're all done with the if-statement. */
11784 finish_if_stmt (statement);
11786 else
11788 bool in_switch_statement_p;
11789 unsigned char in_statement;
11791 /* Add the condition. */
11792 finish_switch_cond (condition, statement);
11794 /* Parse the body of the switch-statement. */
11795 in_switch_statement_p = parser->in_switch_statement_p;
11796 in_statement = parser->in_statement;
11797 parser->in_switch_statement_p = true;
11798 parser->in_statement |= IN_SWITCH_STMT;
11799 cp_parser_implicitly_scoped_statement (parser, if_p,
11800 guard_tinfo);
11801 parser->in_switch_statement_p = in_switch_statement_p;
11802 parser->in_statement = in_statement;
11804 /* Now we're all done with the switch-statement. */
11805 finish_switch_stmt (statement);
11808 return statement;
11810 break;
11812 default:
11813 cp_parser_error (parser, "expected selection-statement");
11814 return error_mark_node;
11818 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11819 If we have seen at least one decl-specifier, and the next token
11820 is not a parenthesis, then we must be looking at a declaration.
11821 (After "int (" we might be looking at a functional cast.) */
11823 static void
11824 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11825 bool any_specifiers_p)
11827 if (any_specifiers_p
11828 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11829 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11830 && !cp_parser_error_occurred (parser))
11831 cp_parser_commit_to_tentative_parse (parser);
11834 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11835 The declarator shall not specify a function or an array. Returns
11836 TRUE if the declarator is valid, FALSE otherwise. */
11838 static bool
11839 cp_parser_check_condition_declarator (cp_parser* parser,
11840 cp_declarator *declarator,
11841 location_t loc)
11843 if (declarator == cp_error_declarator
11844 || function_declarator_p (declarator)
11845 || declarator->kind == cdk_array)
11847 if (declarator == cp_error_declarator)
11848 /* Already complained. */;
11849 else if (declarator->kind == cdk_array)
11850 error_at (loc, "condition declares an array");
11851 else
11852 error_at (loc, "condition declares a function");
11853 if (parser->fully_implicit_function_template_p)
11854 abort_fully_implicit_template (parser);
11855 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11856 /*or_comma=*/false,
11857 /*consume_paren=*/false);
11858 return false;
11860 else
11861 return true;
11864 /* Parse a condition.
11866 condition:
11867 expression
11868 type-specifier-seq declarator = initializer-clause
11869 type-specifier-seq declarator braced-init-list
11871 GNU Extension:
11873 condition:
11874 type-specifier-seq declarator asm-specification [opt]
11875 attributes [opt] = assignment-expression
11877 Returns the expression that should be tested. */
11879 static tree
11880 cp_parser_condition (cp_parser* parser)
11882 cp_decl_specifier_seq type_specifiers;
11883 const char *saved_message;
11884 int declares_class_or_enum;
11886 /* Try the declaration first. */
11887 cp_parser_parse_tentatively (parser);
11888 /* New types are not allowed in the type-specifier-seq for a
11889 condition. */
11890 saved_message = parser->type_definition_forbidden_message;
11891 parser->type_definition_forbidden_message
11892 = G_("types may not be defined in conditions");
11893 /* Parse the type-specifier-seq. */
11894 cp_parser_decl_specifier_seq (parser,
11895 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11896 &type_specifiers,
11897 &declares_class_or_enum);
11898 /* Restore the saved message. */
11899 parser->type_definition_forbidden_message = saved_message;
11901 cp_parser_maybe_commit_to_declaration (parser,
11902 type_specifiers.any_specifiers_p);
11904 /* If all is well, we might be looking at a declaration. */
11905 if (!cp_parser_error_occurred (parser))
11907 tree decl;
11908 tree asm_specification;
11909 tree attributes;
11910 cp_declarator *declarator;
11911 tree initializer = NULL_TREE;
11912 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11914 /* Parse the declarator. */
11915 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11916 /*ctor_dtor_or_conv_p=*/NULL,
11917 /*parenthesized_p=*/NULL,
11918 /*member_p=*/false,
11919 /*friend_p=*/false);
11920 /* Parse the attributes. */
11921 attributes = cp_parser_attributes_opt (parser);
11922 /* Parse the asm-specification. */
11923 asm_specification = cp_parser_asm_specification_opt (parser);
11924 /* If the next token is not an `=' or '{', then we might still be
11925 looking at an expression. For example:
11927 if (A(a).x)
11929 looks like a decl-specifier-seq and a declarator -- but then
11930 there is no `=', so this is an expression. */
11931 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11932 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11933 cp_parser_simulate_error (parser);
11935 /* If we did see an `=' or '{', then we are looking at a declaration
11936 for sure. */
11937 if (cp_parser_parse_definitely (parser))
11939 tree pushed_scope;
11940 bool non_constant_p = false;
11941 int flags = LOOKUP_ONLYCONVERTING;
11943 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11944 return error_mark_node;
11946 /* Create the declaration. */
11947 decl = start_decl (declarator, &type_specifiers,
11948 /*initialized_p=*/true,
11949 attributes, /*prefix_attributes=*/NULL_TREE,
11950 &pushed_scope);
11952 /* Parse the initializer. */
11953 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11955 initializer = cp_parser_braced_list (parser, &non_constant_p);
11956 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11957 flags = 0;
11959 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11961 /* Consume the `='. */
11962 cp_lexer_consume_token (parser->lexer);
11963 initializer = cp_parser_initializer_clause (parser,
11964 &non_constant_p);
11966 else
11968 cp_parser_error (parser, "expected initializer");
11969 initializer = error_mark_node;
11971 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11972 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11974 /* Process the initializer. */
11975 cp_finish_decl (decl,
11976 initializer, !non_constant_p,
11977 asm_specification,
11978 flags);
11980 if (pushed_scope)
11981 pop_scope (pushed_scope);
11983 return convert_from_reference (decl);
11986 /* If we didn't even get past the declarator successfully, we are
11987 definitely not looking at a declaration. */
11988 else
11989 cp_parser_abort_tentative_parse (parser);
11991 /* Otherwise, we are looking at an expression. */
11992 return cp_parser_expression (parser);
11995 /* Parses a for-statement or range-for-statement until the closing ')',
11996 not included. */
11998 static tree
11999 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12001 tree init, scope, decl;
12002 bool is_range_for;
12004 /* Begin the for-statement. */
12005 scope = begin_for_scope (&init);
12007 /* Parse the initialization. */
12008 is_range_for = cp_parser_init_statement (parser, &decl);
12010 if (is_range_for)
12011 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12012 false);
12013 else
12014 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12017 static tree
12018 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12019 unsigned short unroll)
12021 /* Normal for loop */
12022 tree condition = NULL_TREE;
12023 tree expression = NULL_TREE;
12024 tree stmt;
12026 stmt = begin_for_stmt (scope, init);
12027 /* The init-statement has already been parsed in
12028 cp_parser_init_statement, so no work is needed here. */
12029 finish_init_stmt (stmt);
12031 /* If there's a condition, process it. */
12032 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12033 condition = cp_parser_condition (parser);
12034 else if (ivdep)
12036 cp_parser_error (parser, "missing loop condition in loop with "
12037 "%<GCC ivdep%> pragma");
12038 condition = error_mark_node;
12040 else if (unroll)
12042 cp_parser_error (parser, "missing loop condition in loop with "
12043 "%<GCC unroll%> pragma");
12044 condition = error_mark_node;
12046 finish_for_cond (condition, stmt, ivdep, unroll);
12047 /* Look for the `;'. */
12048 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12050 /* If there's an expression, process it. */
12051 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12052 expression = cp_parser_expression (parser);
12053 finish_for_expr (expression, stmt);
12055 return stmt;
12058 /* Tries to parse a range-based for-statement:
12060 range-based-for:
12061 decl-specifier-seq declarator : expression
12063 The decl-specifier-seq declarator and the `:' are already parsed by
12064 cp_parser_init_statement. If processing_template_decl it returns a
12065 newly created RANGE_FOR_STMT; if not, it is converted to a
12066 regular FOR_STMT. */
12068 static tree
12069 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12070 bool ivdep, unsigned short unroll, bool is_omp)
12072 tree stmt, range_expr;
12073 auto_vec <cxx_binding *, 16> bindings;
12074 auto_vec <tree, 16> names;
12075 tree decomp_first_name = NULL_TREE;
12076 unsigned int decomp_cnt = 0;
12078 /* Get the range declaration momentarily out of the way so that
12079 the range expression doesn't clash with it. */
12080 if (range_decl != error_mark_node)
12082 if (DECL_HAS_VALUE_EXPR_P (range_decl))
12084 tree v = DECL_VALUE_EXPR (range_decl);
12085 /* For decomposition declaration get all of the corresponding
12086 declarations out of the way. */
12087 if (TREE_CODE (v) == ARRAY_REF
12088 && VAR_P (TREE_OPERAND (v, 0))
12089 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12091 tree d = range_decl;
12092 range_decl = TREE_OPERAND (v, 0);
12093 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12094 decomp_first_name = d;
12095 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12097 tree name = DECL_NAME (d);
12098 names.safe_push (name);
12099 bindings.safe_push (IDENTIFIER_BINDING (name));
12100 IDENTIFIER_BINDING (name)
12101 = IDENTIFIER_BINDING (name)->previous;
12105 if (names.is_empty ())
12107 tree name = DECL_NAME (range_decl);
12108 names.safe_push (name);
12109 bindings.safe_push (IDENTIFIER_BINDING (name));
12110 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12114 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12116 bool expr_non_constant_p;
12117 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12119 else
12120 range_expr = cp_parser_expression (parser);
12122 /* Put the range declaration(s) back into scope. */
12123 for (unsigned int i = 0; i < names.length (); i++)
12125 cxx_binding *binding = bindings[i];
12126 binding->previous = IDENTIFIER_BINDING (names[i]);
12127 IDENTIFIER_BINDING (names[i]) = binding;
12130 /* finish_omp_for has its own code for the following, so just
12131 return the range_expr instead. */
12132 if (is_omp)
12133 return range_expr;
12135 /* If in template, STMT is converted to a normal for-statement
12136 at instantiation. If not, it is done just ahead. */
12137 if (processing_template_decl)
12139 if (check_for_bare_parameter_packs (range_expr))
12140 range_expr = error_mark_node;
12141 stmt = begin_range_for_stmt (scope, init);
12142 if (ivdep)
12143 RANGE_FOR_IVDEP (stmt) = 1;
12144 if (unroll)
12145 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12146 finish_range_for_decl (stmt, range_decl, range_expr);
12147 if (!type_dependent_expression_p (range_expr)
12148 /* do_auto_deduction doesn't mess with template init-lists. */
12149 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12150 do_range_for_auto_deduction (range_decl, range_expr);
12152 else
12154 stmt = begin_for_stmt (scope, init);
12155 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12156 decomp_first_name, decomp_cnt, ivdep,
12157 unroll);
12159 return stmt;
12162 /* Subroutine of cp_convert_range_for: given the initializer expression,
12163 builds up the range temporary. */
12165 static tree
12166 build_range_temp (tree range_expr)
12168 tree range_type, range_temp;
12170 /* Find out the type deduced by the declaration
12171 `auto &&__range = range_expr'. */
12172 range_type = cp_build_reference_type (make_auto (), true);
12173 range_type = do_auto_deduction (range_type, range_expr,
12174 type_uses_auto (range_type));
12176 /* Create the __range variable. */
12177 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12178 range_type);
12179 TREE_USED (range_temp) = 1;
12180 DECL_ARTIFICIAL (range_temp) = 1;
12182 return range_temp;
12185 /* Used by cp_parser_range_for in template context: we aren't going to
12186 do a full conversion yet, but we still need to resolve auto in the
12187 type of the for-range-declaration if present. This is basically
12188 a shortcut version of cp_convert_range_for. */
12190 static void
12191 do_range_for_auto_deduction (tree decl, tree range_expr)
12193 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12194 if (auto_node)
12196 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12197 range_temp = convert_from_reference (build_range_temp (range_expr));
12198 iter_type = (cp_parser_perform_range_for_lookup
12199 (range_temp, &begin_dummy, &end_dummy));
12200 if (iter_type)
12202 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12203 iter_type);
12204 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12205 RO_UNARY_STAR,
12206 tf_warning_or_error);
12207 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12208 iter_decl, auto_node);
12213 /* Converts a range-based for-statement into a normal
12214 for-statement, as per the definition.
12216 for (RANGE_DECL : RANGE_EXPR)
12217 BLOCK
12219 should be equivalent to:
12222 auto &&__range = RANGE_EXPR;
12223 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12224 __begin != __end;
12225 ++__begin)
12227 RANGE_DECL = *__begin;
12228 BLOCK
12232 If RANGE_EXPR is an array:
12233 BEGIN_EXPR = __range
12234 END_EXPR = __range + ARRAY_SIZE(__range)
12235 Else if RANGE_EXPR has a member 'begin' or 'end':
12236 BEGIN_EXPR = __range.begin()
12237 END_EXPR = __range.end()
12238 Else:
12239 BEGIN_EXPR = begin(__range)
12240 END_EXPR = end(__range);
12242 If __range has a member 'begin' but not 'end', or vice versa, we must
12243 still use the second alternative (it will surely fail, however).
12244 When calling begin()/end() in the third alternative we must use
12245 argument dependent lookup, but always considering 'std' as an associated
12246 namespace. */
12248 tree
12249 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12250 tree decomp_first_name, unsigned int decomp_cnt,
12251 bool ivdep, unsigned short unroll)
12253 tree begin, end;
12254 tree iter_type, begin_expr, end_expr;
12255 tree condition, expression;
12257 range_expr = mark_lvalue_use (range_expr);
12259 if (range_decl == error_mark_node || range_expr == error_mark_node)
12260 /* If an error happened previously do nothing or else a lot of
12261 unhelpful errors would be issued. */
12262 begin_expr = end_expr = iter_type = error_mark_node;
12263 else
12265 tree range_temp;
12267 if (VAR_P (range_expr)
12268 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12269 /* Can't bind a reference to an array of runtime bound. */
12270 range_temp = range_expr;
12271 else
12273 range_temp = build_range_temp (range_expr);
12274 pushdecl (range_temp);
12275 cp_finish_decl (range_temp, range_expr,
12276 /*is_constant_init*/false, NULL_TREE,
12277 LOOKUP_ONLYCONVERTING);
12278 range_temp = convert_from_reference (range_temp);
12280 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12281 &begin_expr, &end_expr);
12284 /* The new for initialization statement. */
12285 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12286 iter_type);
12287 TREE_USED (begin) = 1;
12288 DECL_ARTIFICIAL (begin) = 1;
12289 pushdecl (begin);
12290 cp_finish_decl (begin, begin_expr,
12291 /*is_constant_init*/false, NULL_TREE,
12292 LOOKUP_ONLYCONVERTING);
12294 if (cxx_dialect >= cxx17)
12295 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12296 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12297 TREE_USED (end) = 1;
12298 DECL_ARTIFICIAL (end) = 1;
12299 pushdecl (end);
12300 cp_finish_decl (end, end_expr,
12301 /*is_constant_init*/false, NULL_TREE,
12302 LOOKUP_ONLYCONVERTING);
12304 finish_init_stmt (statement);
12306 /* The new for condition. */
12307 condition = build_x_binary_op (input_location, NE_EXPR,
12308 begin, ERROR_MARK,
12309 end, ERROR_MARK,
12310 NULL, tf_warning_or_error);
12311 finish_for_cond (condition, statement, ivdep, unroll);
12313 /* The new increment expression. */
12314 expression = finish_unary_op_expr (input_location,
12315 PREINCREMENT_EXPR, begin,
12316 tf_warning_or_error);
12317 finish_for_expr (expression, statement);
12319 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12320 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12322 /* The declaration is initialized with *__begin inside the loop body. */
12323 cp_finish_decl (range_decl,
12324 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12325 tf_warning_or_error),
12326 /*is_constant_init*/false, NULL_TREE,
12327 LOOKUP_ONLYCONVERTING);
12328 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12329 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12331 return statement;
12334 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12335 We need to solve both at the same time because the method used
12336 depends on the existence of members begin or end.
12337 Returns the type deduced for the iterator expression. */
12339 static tree
12340 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12342 if (error_operand_p (range))
12344 *begin = *end = error_mark_node;
12345 return error_mark_node;
12348 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12350 error ("range-based %<for%> expression of type %qT "
12351 "has incomplete type", TREE_TYPE (range));
12352 *begin = *end = error_mark_node;
12353 return error_mark_node;
12355 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12357 /* If RANGE is an array, we will use pointer arithmetic. */
12358 *begin = decay_conversion (range, tf_warning_or_error);
12359 *end = build_binary_op (input_location, PLUS_EXPR,
12360 range,
12361 array_type_nelts_top (TREE_TYPE (range)),
12362 false);
12363 return TREE_TYPE (*begin);
12365 else
12367 /* If it is not an array, we must do a bit of magic. */
12368 tree id_begin, id_end;
12369 tree member_begin, member_end;
12371 *begin = *end = error_mark_node;
12373 id_begin = get_identifier ("begin");
12374 id_end = get_identifier ("end");
12375 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12376 /*protect=*/2, /*want_type=*/false,
12377 tf_warning_or_error);
12378 member_end = lookup_member (TREE_TYPE (range), id_end,
12379 /*protect=*/2, /*want_type=*/false,
12380 tf_warning_or_error);
12382 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12384 /* Use the member functions. */
12385 *begin = cp_parser_range_for_member_function (range, id_begin);
12386 *end = cp_parser_range_for_member_function (range, id_end);
12388 else
12390 /* Use global functions with ADL. */
12391 vec<tree, va_gc> *vec;
12392 vec = make_tree_vector ();
12394 vec_safe_push (vec, range);
12396 member_begin = perform_koenig_lookup (id_begin, vec,
12397 tf_warning_or_error);
12398 *begin = finish_call_expr (member_begin, &vec, false, true,
12399 tf_warning_or_error);
12400 member_end = perform_koenig_lookup (id_end, vec,
12401 tf_warning_or_error);
12402 *end = finish_call_expr (member_end, &vec, false, true,
12403 tf_warning_or_error);
12405 release_tree_vector (vec);
12408 /* Last common checks. */
12409 if (*begin == error_mark_node || *end == error_mark_node)
12411 /* If one of the expressions is an error do no more checks. */
12412 *begin = *end = error_mark_node;
12413 return error_mark_node;
12415 else if (type_dependent_expression_p (*begin)
12416 || type_dependent_expression_p (*end))
12417 /* Can happen, when, eg, in a template context, Koenig lookup
12418 can't resolve begin/end (c++/58503). */
12419 return NULL_TREE;
12420 else
12422 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12423 /* The unqualified type of the __begin and __end temporaries should
12424 be the same, as required by the multiple auto declaration. */
12425 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12427 if (cxx_dialect >= cxx17
12428 && (build_x_binary_op (input_location, NE_EXPR,
12429 *begin, ERROR_MARK,
12430 *end, ERROR_MARK,
12431 NULL, tf_none)
12432 != error_mark_node))
12433 /* P0184R0 allows __begin and __end to have different types,
12434 but make sure they are comparable so we can give a better
12435 diagnostic. */;
12436 else
12437 error ("inconsistent begin/end types in range-based %<for%> "
12438 "statement: %qT and %qT",
12439 TREE_TYPE (*begin), TREE_TYPE (*end));
12441 return iter_type;
12446 /* Helper function for cp_parser_perform_range_for_lookup.
12447 Builds a tree for RANGE.IDENTIFIER(). */
12449 static tree
12450 cp_parser_range_for_member_function (tree range, tree identifier)
12452 tree member, res;
12453 vec<tree, va_gc> *vec;
12455 member = finish_class_member_access_expr (range, identifier,
12456 false, tf_warning_or_error);
12457 if (member == error_mark_node)
12458 return error_mark_node;
12460 vec = make_tree_vector ();
12461 res = finish_call_expr (member, &vec,
12462 /*disallow_virtual=*/false,
12463 /*koenig_p=*/false,
12464 tf_warning_or_error);
12465 release_tree_vector (vec);
12466 return res;
12469 /* Parse an iteration-statement.
12471 iteration-statement:
12472 while ( condition ) statement
12473 do statement while ( expression ) ;
12474 for ( init-statement condition [opt] ; expression [opt] )
12475 statement
12477 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12479 static tree
12480 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12481 unsigned short unroll)
12483 cp_token *token;
12484 enum rid keyword;
12485 tree statement;
12486 unsigned char in_statement;
12487 token_indent_info guard_tinfo;
12489 /* Peek at the next token. */
12490 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12491 if (!token)
12492 return error_mark_node;
12494 guard_tinfo = get_token_indent_info (token);
12496 /* Remember whether or not we are already within an iteration
12497 statement. */
12498 in_statement = parser->in_statement;
12500 /* See what kind of keyword it is. */
12501 keyword = token->keyword;
12502 switch (keyword)
12504 case RID_WHILE:
12506 tree condition;
12508 /* Begin the while-statement. */
12509 statement = begin_while_stmt ();
12510 /* Look for the `('. */
12511 matching_parens parens;
12512 parens.require_open (parser);
12513 /* Parse the condition. */
12514 condition = cp_parser_condition (parser);
12515 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12516 /* Look for the `)'. */
12517 parens.require_close (parser);
12518 /* Parse the dependent statement. */
12519 parser->in_statement = IN_ITERATION_STMT;
12520 bool prev = note_iteration_stmt_body_start ();
12521 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12522 note_iteration_stmt_body_end (prev);
12523 parser->in_statement = in_statement;
12524 /* We're done with the while-statement. */
12525 finish_while_stmt (statement);
12527 break;
12529 case RID_DO:
12531 tree expression;
12533 /* Begin the do-statement. */
12534 statement = begin_do_stmt ();
12535 /* Parse the body of the do-statement. */
12536 parser->in_statement = IN_ITERATION_STMT;
12537 bool prev = note_iteration_stmt_body_start ();
12538 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12539 note_iteration_stmt_body_end (prev);
12540 parser->in_statement = in_statement;
12541 finish_do_body (statement);
12542 /* Look for the `while' keyword. */
12543 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12544 /* Look for the `('. */
12545 matching_parens parens;
12546 parens.require_open (parser);
12547 /* Parse the expression. */
12548 expression = cp_parser_expression (parser);
12549 /* We're done with the do-statement. */
12550 finish_do_stmt (expression, statement, ivdep, unroll);
12551 /* Look for the `)'. */
12552 parens.require_close (parser);
12553 /* Look for the `;'. */
12554 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12556 break;
12558 case RID_FOR:
12560 /* Look for the `('. */
12561 matching_parens parens;
12562 parens.require_open (parser);
12564 statement = cp_parser_for (parser, ivdep, unroll);
12566 /* Look for the `)'. */
12567 parens.require_close (parser);
12569 /* Parse the body of the for-statement. */
12570 parser->in_statement = IN_ITERATION_STMT;
12571 bool prev = note_iteration_stmt_body_start ();
12572 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12573 note_iteration_stmt_body_end (prev);
12574 parser->in_statement = in_statement;
12576 /* We're done with the for-statement. */
12577 finish_for_stmt (statement);
12579 break;
12581 default:
12582 cp_parser_error (parser, "expected iteration-statement");
12583 statement = error_mark_node;
12584 break;
12587 return statement;
12590 /* Parse a init-statement or the declarator of a range-based-for.
12591 Returns true if a range-based-for declaration is seen.
12593 init-statement:
12594 expression-statement
12595 simple-declaration */
12597 static bool
12598 cp_parser_init_statement (cp_parser *parser, tree *decl)
12600 /* If the next token is a `;', then we have an empty
12601 expression-statement. Grammatically, this is also a
12602 simple-declaration, but an invalid one, because it does not
12603 declare anything. Therefore, if we did not handle this case
12604 specially, we would issue an error message about an invalid
12605 declaration. */
12606 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12608 bool is_range_for = false;
12609 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12611 /* Try to parse the init-statement. */
12612 if (cp_parser_range_based_for_with_init_p (parser))
12614 tree dummy;
12615 cp_parser_parse_tentatively (parser);
12616 /* Parse the declaration. */
12617 cp_parser_simple_declaration (parser,
12618 /*function_definition_allowed_p=*/false,
12619 &dummy);
12620 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12621 if (!cp_parser_parse_definitely (parser))
12622 /* That didn't work, try to parse it as an expression-statement. */
12623 cp_parser_expression_statement (parser, NULL_TREE);
12625 if (cxx_dialect < cxx2a)
12627 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12628 "range-based %<for%> loops with initializer only "
12629 "available with -std=c++2a or -std=gnu++2a");
12630 *decl = error_mark_node;
12634 /* A colon is used in range-based for. */
12635 parser->colon_corrects_to_scope_p = false;
12637 /* We're going to speculatively look for a declaration, falling back
12638 to an expression, if necessary. */
12639 cp_parser_parse_tentatively (parser);
12640 /* Parse the declaration. */
12641 cp_parser_simple_declaration (parser,
12642 /*function_definition_allowed_p=*/false,
12643 decl);
12644 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12645 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12647 /* It is a range-for, consume the ':'. */
12648 cp_lexer_consume_token (parser->lexer);
12649 is_range_for = true;
12650 if (cxx_dialect < cxx11)
12651 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12652 "range-based %<for%> loops only available with "
12653 "-std=c++11 or -std=gnu++11");
12655 else
12656 /* The ';' is not consumed yet because we told
12657 cp_parser_simple_declaration not to. */
12658 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12660 if (cp_parser_parse_definitely (parser))
12661 return is_range_for;
12662 /* If the tentative parse failed, then we shall need to look for an
12663 expression-statement. */
12665 /* If we are here, it is an expression-statement. */
12666 cp_parser_expression_statement (parser, NULL_TREE);
12667 return false;
12670 /* Parse a jump-statement.
12672 jump-statement:
12673 break ;
12674 continue ;
12675 return expression [opt] ;
12676 return braced-init-list ;
12677 goto identifier ;
12679 GNU extension:
12681 jump-statement:
12682 goto * expression ;
12684 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12686 static tree
12687 cp_parser_jump_statement (cp_parser* parser)
12689 tree statement = error_mark_node;
12690 cp_token *token;
12691 enum rid keyword;
12692 unsigned char in_statement;
12694 /* Peek at the next token. */
12695 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12696 if (!token)
12697 return error_mark_node;
12699 /* See what kind of keyword it is. */
12700 keyword = token->keyword;
12701 switch (keyword)
12703 case RID_BREAK:
12704 in_statement = parser->in_statement & ~IN_IF_STMT;
12705 switch (in_statement)
12707 case 0:
12708 error_at (token->location, "break statement not within loop or switch");
12709 break;
12710 default:
12711 gcc_assert ((in_statement & IN_SWITCH_STMT)
12712 || in_statement == IN_ITERATION_STMT);
12713 statement = finish_break_stmt ();
12714 if (in_statement == IN_ITERATION_STMT)
12715 break_maybe_infinite_loop ();
12716 break;
12717 case IN_OMP_BLOCK:
12718 error_at (token->location, "invalid exit from OpenMP structured block");
12719 break;
12720 case IN_OMP_FOR:
12721 error_at (token->location, "break statement used with OpenMP for loop");
12722 break;
12724 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12725 break;
12727 case RID_CONTINUE:
12728 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12730 case 0:
12731 error_at (token->location, "continue statement not within a loop");
12732 break;
12733 /* Fall through. */
12734 case IN_ITERATION_STMT:
12735 case IN_OMP_FOR:
12736 statement = finish_continue_stmt ();
12737 break;
12738 case IN_OMP_BLOCK:
12739 error_at (token->location, "invalid exit from OpenMP structured block");
12740 break;
12741 default:
12742 gcc_unreachable ();
12744 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12745 break;
12747 case RID_RETURN:
12749 tree expr;
12750 bool expr_non_constant_p;
12752 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12754 cp_lexer_set_source_position (parser->lexer);
12755 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12756 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12758 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12759 expr = cp_parser_expression (parser);
12760 else
12761 /* If the next token is a `;', then there is no
12762 expression. */
12763 expr = NULL_TREE;
12764 /* Build the return-statement. */
12765 if (current_function_auto_return_pattern && in_discarded_stmt)
12766 /* Don't deduce from a discarded return statement. */;
12767 else
12768 statement = finish_return_stmt (expr);
12769 /* Look for the final `;'. */
12770 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12772 break;
12774 case RID_GOTO:
12775 if (parser->in_function_body
12776 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12778 error ("%<goto%> in %<constexpr%> function");
12779 cp_function_chain->invalid_constexpr = true;
12782 /* Create the goto-statement. */
12783 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12785 /* Issue a warning about this use of a GNU extension. */
12786 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12787 /* Consume the '*' token. */
12788 cp_lexer_consume_token (parser->lexer);
12789 /* Parse the dependent expression. */
12790 finish_goto_stmt (cp_parser_expression (parser));
12792 else
12793 finish_goto_stmt (cp_parser_identifier (parser));
12794 /* Look for the final `;'. */
12795 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12796 break;
12798 default:
12799 cp_parser_error (parser, "expected jump-statement");
12800 break;
12803 return statement;
12806 /* Parse a declaration-statement.
12808 declaration-statement:
12809 block-declaration */
12811 static void
12812 cp_parser_declaration_statement (cp_parser* parser)
12814 void *p;
12816 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12817 p = obstack_alloc (&declarator_obstack, 0);
12819 /* Parse the block-declaration. */
12820 cp_parser_block_declaration (parser, /*statement_p=*/true);
12822 /* Free any declarators allocated. */
12823 obstack_free (&declarator_obstack, p);
12826 /* Some dependent statements (like `if (cond) statement'), are
12827 implicitly in their own scope. In other words, if the statement is
12828 a single statement (as opposed to a compound-statement), it is
12829 none-the-less treated as if it were enclosed in braces. Any
12830 declarations appearing in the dependent statement are out of scope
12831 after control passes that point. This function parses a statement,
12832 but ensures that is in its own scope, even if it is not a
12833 compound-statement.
12835 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12836 is a (possibly labeled) if statement which is not enclosed in
12837 braces and has an else clause. This is used to implement
12838 -Wparentheses.
12840 CHAIN is a vector of if-else-if conditions. This is used to implement
12841 -Wduplicated-cond.
12843 Returns the new statement. */
12845 static tree
12846 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12847 const token_indent_info &guard_tinfo,
12848 vec<tree> *chain)
12850 tree statement;
12851 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12852 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12853 token_indent_info body_tinfo
12854 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12856 if (if_p != NULL)
12857 *if_p = false;
12859 /* Mark if () ; with a special NOP_EXPR. */
12860 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12862 cp_lexer_consume_token (parser->lexer);
12863 statement = add_stmt (build_empty_stmt (body_loc));
12865 if (guard_tinfo.keyword == RID_IF
12866 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12867 warning_at (body_loc, OPT_Wempty_body,
12868 "suggest braces around empty body in an %<if%> statement");
12869 else if (guard_tinfo.keyword == RID_ELSE)
12870 warning_at (body_loc, OPT_Wempty_body,
12871 "suggest braces around empty body in an %<else%> statement");
12873 /* if a compound is opened, we simply parse the statement directly. */
12874 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12875 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12876 /* If the token is not a `{', then we must take special action. */
12877 else
12879 /* Create a compound-statement. */
12880 statement = begin_compound_stmt (0);
12881 /* Parse the dependent-statement. */
12882 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12883 &body_loc_after_labels);
12884 /* Finish the dummy compound-statement. */
12885 finish_compound_stmt (statement);
12888 token_indent_info next_tinfo
12889 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12890 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12892 if (body_loc_after_labels != UNKNOWN_LOCATION
12893 && next_tinfo.type != CPP_SEMICOLON)
12894 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12895 guard_tinfo.location, guard_tinfo.keyword);
12897 /* Return the statement. */
12898 return statement;
12901 /* For some dependent statements (like `while (cond) statement'), we
12902 have already created a scope. Therefore, even if the dependent
12903 statement is a compound-statement, we do not want to create another
12904 scope. */
12906 static void
12907 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12908 const token_indent_info &guard_tinfo)
12910 /* If the token is a `{', then we must take special action. */
12911 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12913 token_indent_info body_tinfo
12914 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12915 location_t loc_after_labels = UNKNOWN_LOCATION;
12917 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12918 &loc_after_labels);
12919 token_indent_info next_tinfo
12920 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12921 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12923 if (loc_after_labels != UNKNOWN_LOCATION
12924 && next_tinfo.type != CPP_SEMICOLON)
12925 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12926 guard_tinfo.location,
12927 guard_tinfo.keyword);
12929 else
12931 /* Avoid calling cp_parser_compound_statement, so that we
12932 don't create a new scope. Do everything else by hand. */
12933 matching_braces braces;
12934 braces.require_open (parser);
12935 /* If the next keyword is `__label__' we have a label declaration. */
12936 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12937 cp_parser_label_declaration (parser);
12938 /* Parse an (optional) statement-seq. */
12939 cp_parser_statement_seq_opt (parser, NULL_TREE);
12940 braces.require_close (parser);
12944 /* Declarations [gram.dcl.dcl] */
12946 /* Parse an optional declaration-sequence.
12948 declaration-seq:
12949 declaration
12950 declaration-seq declaration */
12952 static void
12953 cp_parser_declaration_seq_opt (cp_parser* parser)
12955 while (true)
12957 cp_token *token = cp_lexer_peek_token (parser->lexer);
12959 if (token->type == CPP_CLOSE_BRACE
12960 || token->type == CPP_EOF)
12961 break;
12962 else
12963 cp_parser_toplevel_declaration (parser);
12967 /* Parse a declaration.
12969 declaration:
12970 block-declaration
12971 function-definition
12972 template-declaration
12973 explicit-instantiation
12974 explicit-specialization
12975 linkage-specification
12976 namespace-definition
12978 C++17:
12979 deduction-guide
12981 GNU extension:
12983 declaration:
12984 __extension__ declaration */
12986 static void
12987 cp_parser_declaration (cp_parser* parser)
12989 cp_token token1;
12990 cp_token token2;
12991 int saved_pedantic;
12992 void *p;
12993 tree attributes = NULL_TREE;
12995 /* Check for the `__extension__' keyword. */
12996 if (cp_parser_extension_opt (parser, &saved_pedantic))
12998 /* Parse the qualified declaration. */
12999 cp_parser_declaration (parser);
13000 /* Restore the PEDANTIC flag. */
13001 pedantic = saved_pedantic;
13003 return;
13006 /* Try to figure out what kind of declaration is present. */
13007 token1 = *cp_lexer_peek_token (parser->lexer);
13009 if (token1.type != CPP_EOF)
13010 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
13011 else
13013 token2.type = CPP_EOF;
13014 token2.keyword = RID_MAX;
13017 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13018 p = obstack_alloc (&declarator_obstack, 0);
13020 /* If the next token is `extern' and the following token is a string
13021 literal, then we have a linkage specification. */
13022 if (token1.keyword == RID_EXTERN
13023 && cp_parser_is_pure_string_literal (&token2))
13024 cp_parser_linkage_specification (parser);
13025 /* If the next token is `template', then we have either a template
13026 declaration, an explicit instantiation, or an explicit
13027 specialization. */
13028 else if (token1.keyword == RID_TEMPLATE)
13030 /* `template <>' indicates a template specialization. */
13031 if (token2.type == CPP_LESS
13032 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13033 cp_parser_explicit_specialization (parser);
13034 /* `template <' indicates a template declaration. */
13035 else if (token2.type == CPP_LESS)
13036 cp_parser_template_declaration (parser, /*member_p=*/false);
13037 /* Anything else must be an explicit instantiation. */
13038 else
13039 cp_parser_explicit_instantiation (parser);
13041 /* If the next token is `export', then we have a template
13042 declaration. */
13043 else if (token1.keyword == RID_EXPORT)
13044 cp_parser_template_declaration (parser, /*member_p=*/false);
13045 /* If the next token is `extern', 'static' or 'inline' and the one
13046 after that is `template', we have a GNU extended explicit
13047 instantiation directive. */
13048 else if (cp_parser_allow_gnu_extensions_p (parser)
13049 && (token1.keyword == RID_EXTERN
13050 || token1.keyword == RID_STATIC
13051 || token1.keyword == RID_INLINE)
13052 && token2.keyword == RID_TEMPLATE)
13053 cp_parser_explicit_instantiation (parser);
13054 /* If the next token is `namespace', check for a named or unnamed
13055 namespace definition. */
13056 else if (token1.keyword == RID_NAMESPACE
13057 && (/* A named namespace definition. */
13058 (token2.type == CPP_NAME
13059 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13060 != CPP_EQ))
13061 || (token2.type == CPP_OPEN_SQUARE
13062 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13063 == CPP_OPEN_SQUARE)
13064 /* An unnamed namespace definition. */
13065 || token2.type == CPP_OPEN_BRACE
13066 || token2.keyword == RID_ATTRIBUTE))
13067 cp_parser_namespace_definition (parser);
13068 /* An inline (associated) namespace definition. */
13069 else if (token1.keyword == RID_INLINE
13070 && token2.keyword == RID_NAMESPACE)
13071 cp_parser_namespace_definition (parser);
13072 /* Objective-C++ declaration/definition. */
13073 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
13074 cp_parser_objc_declaration (parser, NULL_TREE);
13075 else if (c_dialect_objc ()
13076 && token1.keyword == RID_ATTRIBUTE
13077 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13078 cp_parser_objc_declaration (parser, attributes);
13079 /* At this point we may have a template declared by a concept
13080 introduction. */
13081 else if (flag_concepts
13082 && cp_parser_template_declaration_after_export (parser,
13083 /*member_p=*/false))
13084 /* We did. */;
13085 else
13086 /* Try to parse a block-declaration, or a function-definition. */
13087 cp_parser_block_declaration (parser, /*statement_p=*/false);
13089 /* Free any declarators allocated. */
13090 obstack_free (&declarator_obstack, p);
13093 /* Parse a namespace-scope declaration. */
13095 static void
13096 cp_parser_toplevel_declaration (cp_parser* parser)
13098 cp_token *token = cp_lexer_peek_token (parser->lexer);
13100 if (token->type == CPP_PRAGMA)
13101 /* A top-level declaration can consist solely of a #pragma. A
13102 nested declaration cannot, so this is done here and not in
13103 cp_parser_declaration. (A #pragma at block scope is
13104 handled in cp_parser_statement.) */
13105 cp_parser_pragma (parser, pragma_external, NULL);
13106 else if (token->type == CPP_SEMICOLON)
13108 /* A declaration consisting of a single semicolon is
13109 invalid. Allow it unless we're being pedantic. */
13110 cp_lexer_consume_token (parser->lexer);
13111 if (!in_system_header_at (input_location))
13112 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13114 else
13115 /* Parse the declaration itself. */
13116 cp_parser_declaration (parser);
13119 /* Parse a block-declaration.
13121 block-declaration:
13122 simple-declaration
13123 asm-definition
13124 namespace-alias-definition
13125 using-declaration
13126 using-directive
13128 GNU Extension:
13130 block-declaration:
13131 __extension__ block-declaration
13133 C++0x Extension:
13135 block-declaration:
13136 static_assert-declaration
13138 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13139 part of a declaration-statement. */
13141 static void
13142 cp_parser_block_declaration (cp_parser *parser,
13143 bool statement_p)
13145 cp_token *token1;
13146 int saved_pedantic;
13148 /* Check for the `__extension__' keyword. */
13149 if (cp_parser_extension_opt (parser, &saved_pedantic))
13151 /* Parse the qualified declaration. */
13152 cp_parser_block_declaration (parser, statement_p);
13153 /* Restore the PEDANTIC flag. */
13154 pedantic = saved_pedantic;
13156 return;
13159 /* Peek at the next token to figure out which kind of declaration is
13160 present. */
13161 token1 = cp_lexer_peek_token (parser->lexer);
13163 /* If the next keyword is `asm', we have an asm-definition. */
13164 if (token1->keyword == RID_ASM)
13166 if (statement_p)
13167 cp_parser_commit_to_tentative_parse (parser);
13168 cp_parser_asm_definition (parser);
13170 /* If the next keyword is `namespace', we have a
13171 namespace-alias-definition. */
13172 else if (token1->keyword == RID_NAMESPACE)
13173 cp_parser_namespace_alias_definition (parser);
13174 /* If the next keyword is `using', we have a
13175 using-declaration, a using-directive, or an alias-declaration. */
13176 else if (token1->keyword == RID_USING)
13178 cp_token *token2;
13180 if (statement_p)
13181 cp_parser_commit_to_tentative_parse (parser);
13182 /* If the token after `using' is `namespace', then we have a
13183 using-directive. */
13184 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13185 if (token2->keyword == RID_NAMESPACE)
13186 cp_parser_using_directive (parser);
13187 /* If the second token after 'using' is '=', then we have an
13188 alias-declaration. */
13189 else if (cxx_dialect >= cxx11
13190 && token2->type == CPP_NAME
13191 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13192 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13193 cp_parser_alias_declaration (parser);
13194 /* Otherwise, it's a using-declaration. */
13195 else
13196 cp_parser_using_declaration (parser,
13197 /*access_declaration_p=*/false);
13199 /* If the next keyword is `__label__' we have a misplaced label
13200 declaration. */
13201 else if (token1->keyword == RID_LABEL)
13203 cp_lexer_consume_token (parser->lexer);
13204 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13205 cp_parser_skip_to_end_of_statement (parser);
13206 /* If the next token is now a `;', consume it. */
13207 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13208 cp_lexer_consume_token (parser->lexer);
13210 /* If the next token is `static_assert' we have a static assertion. */
13211 else if (token1->keyword == RID_STATIC_ASSERT)
13212 cp_parser_static_assert (parser, /*member_p=*/false);
13213 /* Anything else must be a simple-declaration. */
13214 else
13215 cp_parser_simple_declaration (parser, !statement_p,
13216 /*maybe_range_for_decl*/NULL);
13219 /* Parse a simple-declaration.
13221 simple-declaration:
13222 decl-specifier-seq [opt] init-declarator-list [opt] ;
13223 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13224 brace-or-equal-initializer ;
13226 init-declarator-list:
13227 init-declarator
13228 init-declarator-list , init-declarator
13230 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13231 function-definition as a simple-declaration.
13233 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13234 parsed declaration if it is an uninitialized single declarator not followed
13235 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13236 if present, will not be consumed. */
13238 static void
13239 cp_parser_simple_declaration (cp_parser* parser,
13240 bool function_definition_allowed_p,
13241 tree *maybe_range_for_decl)
13243 cp_decl_specifier_seq decl_specifiers;
13244 int declares_class_or_enum;
13245 bool saw_declarator;
13246 location_t comma_loc = UNKNOWN_LOCATION;
13247 location_t init_loc = UNKNOWN_LOCATION;
13249 if (maybe_range_for_decl)
13250 *maybe_range_for_decl = NULL_TREE;
13252 /* Defer access checks until we know what is being declared; the
13253 checks for names appearing in the decl-specifier-seq should be
13254 done as if we were in the scope of the thing being declared. */
13255 push_deferring_access_checks (dk_deferred);
13257 /* Parse the decl-specifier-seq. We have to keep track of whether
13258 or not the decl-specifier-seq declares a named class or
13259 enumeration type, since that is the only case in which the
13260 init-declarator-list is allowed to be empty.
13262 [dcl.dcl]
13264 In a simple-declaration, the optional init-declarator-list can be
13265 omitted only when declaring a class or enumeration, that is when
13266 the decl-specifier-seq contains either a class-specifier, an
13267 elaborated-type-specifier, or an enum-specifier. */
13268 cp_parser_decl_specifier_seq (parser,
13269 CP_PARSER_FLAGS_OPTIONAL,
13270 &decl_specifiers,
13271 &declares_class_or_enum);
13272 /* We no longer need to defer access checks. */
13273 stop_deferring_access_checks ();
13275 /* In a block scope, a valid declaration must always have a
13276 decl-specifier-seq. By not trying to parse declarators, we can
13277 resolve the declaration/expression ambiguity more quickly. */
13278 if (!function_definition_allowed_p
13279 && !decl_specifiers.any_specifiers_p)
13281 cp_parser_error (parser, "expected declaration");
13282 goto done;
13285 /* If the next two tokens are both identifiers, the code is
13286 erroneous. The usual cause of this situation is code like:
13288 T t;
13290 where "T" should name a type -- but does not. */
13291 if (!decl_specifiers.any_type_specifiers_p
13292 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13294 /* If parsing tentatively, we should commit; we really are
13295 looking at a declaration. */
13296 cp_parser_commit_to_tentative_parse (parser);
13297 /* Give up. */
13298 goto done;
13301 cp_parser_maybe_commit_to_declaration (parser,
13302 decl_specifiers.any_specifiers_p);
13304 /* Look for C++17 decomposition declaration. */
13305 for (size_t n = 1; ; n++)
13306 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13307 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13308 continue;
13309 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13310 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13311 && decl_specifiers.any_specifiers_p)
13313 tree decl
13314 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13315 maybe_range_for_decl,
13316 &init_loc);
13318 /* The next token should be either a `,' or a `;'. */
13319 cp_token *token = cp_lexer_peek_token (parser->lexer);
13320 /* If it's a `;', we are done. */
13321 if (token->type == CPP_SEMICOLON)
13322 goto finish;
13323 else if (maybe_range_for_decl)
13325 if (*maybe_range_for_decl == NULL_TREE)
13326 *maybe_range_for_decl = error_mark_node;
13327 goto finish;
13329 /* Anything else is an error. */
13330 else
13332 /* If we have already issued an error message we don't need
13333 to issue another one. */
13334 if ((decl != error_mark_node
13335 && DECL_INITIAL (decl) != error_mark_node)
13336 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13337 cp_parser_error (parser, "expected %<,%> or %<;%>");
13338 /* Skip tokens until we reach the end of the statement. */
13339 cp_parser_skip_to_end_of_statement (parser);
13340 /* If the next token is now a `;', consume it. */
13341 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13342 cp_lexer_consume_token (parser->lexer);
13343 goto done;
13346 else
13347 break;
13349 tree last_type;
13350 bool auto_specifier_p;
13351 /* NULL_TREE if both variable and function declaration are allowed,
13352 error_mark_node if function declaration are not allowed and
13353 a FUNCTION_DECL that should be diagnosed if it is followed by
13354 variable declarations. */
13355 tree auto_function_declaration;
13357 last_type = NULL_TREE;
13358 auto_specifier_p
13359 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13360 auto_function_declaration = NULL_TREE;
13362 /* Keep going until we hit the `;' at the end of the simple
13363 declaration. */
13364 saw_declarator = false;
13365 while (cp_lexer_next_token_is_not (parser->lexer,
13366 CPP_SEMICOLON))
13368 cp_token *token;
13369 bool function_definition_p;
13370 tree decl;
13371 tree auto_result = NULL_TREE;
13373 if (saw_declarator)
13375 /* If we are processing next declarator, comma is expected */
13376 token = cp_lexer_peek_token (parser->lexer);
13377 gcc_assert (token->type == CPP_COMMA);
13378 cp_lexer_consume_token (parser->lexer);
13379 if (maybe_range_for_decl)
13381 *maybe_range_for_decl = error_mark_node;
13382 if (comma_loc == UNKNOWN_LOCATION)
13383 comma_loc = token->location;
13386 else
13387 saw_declarator = true;
13389 /* Parse the init-declarator. */
13390 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13391 /*checks=*/NULL,
13392 function_definition_allowed_p,
13393 /*member_p=*/false,
13394 declares_class_or_enum,
13395 &function_definition_p,
13396 maybe_range_for_decl,
13397 &init_loc,
13398 &auto_result);
13399 /* If an error occurred while parsing tentatively, exit quickly.
13400 (That usually happens when in the body of a function; each
13401 statement is treated as a declaration-statement until proven
13402 otherwise.) */
13403 if (cp_parser_error_occurred (parser))
13404 goto done;
13406 if (auto_specifier_p && cxx_dialect >= cxx14)
13408 /* If the init-declarator-list contains more than one
13409 init-declarator, they shall all form declarations of
13410 variables. */
13411 if (auto_function_declaration == NULL_TREE)
13412 auto_function_declaration
13413 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13414 else if (TREE_CODE (decl) == FUNCTION_DECL
13415 || auto_function_declaration != error_mark_node)
13417 error_at (decl_specifiers.locations[ds_type_spec],
13418 "non-variable %qD in declaration with more than one "
13419 "declarator with placeholder type",
13420 TREE_CODE (decl) == FUNCTION_DECL
13421 ? decl : auto_function_declaration);
13422 auto_function_declaration = error_mark_node;
13426 if (auto_result
13427 && (!processing_template_decl || !type_uses_auto (auto_result)))
13429 if (last_type
13430 && last_type != error_mark_node
13431 && !same_type_p (auto_result, last_type))
13433 /* If the list of declarators contains more than one declarator,
13434 the type of each declared variable is determined as described
13435 above. If the type deduced for the template parameter U is not
13436 the same in each deduction, the program is ill-formed. */
13437 error_at (decl_specifiers.locations[ds_type_spec],
13438 "inconsistent deduction for %qT: %qT and then %qT",
13439 decl_specifiers.type, last_type, auto_result);
13440 last_type = error_mark_node;
13442 else
13443 last_type = auto_result;
13446 /* Handle function definitions specially. */
13447 if (function_definition_p)
13449 /* If the next token is a `,', then we are probably
13450 processing something like:
13452 void f() {}, *p;
13454 which is erroneous. */
13455 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13457 cp_token *token = cp_lexer_peek_token (parser->lexer);
13458 error_at (token->location,
13459 "mixing"
13460 " declarations and function-definitions is forbidden");
13462 /* Otherwise, we're done with the list of declarators. */
13463 else
13465 pop_deferring_access_checks ();
13466 return;
13469 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13470 *maybe_range_for_decl = decl;
13471 /* The next token should be either a `,' or a `;'. */
13472 token = cp_lexer_peek_token (parser->lexer);
13473 /* If it's a `,', there are more declarators to come. */
13474 if (token->type == CPP_COMMA)
13475 /* will be consumed next time around */;
13476 /* If it's a `;', we are done. */
13477 else if (token->type == CPP_SEMICOLON)
13478 break;
13479 else if (maybe_range_for_decl)
13481 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13482 permerror (decl_specifiers.locations[ds_type_spec],
13483 "types may not be defined in a for-range-declaration");
13484 break;
13486 /* Anything else is an error. */
13487 else
13489 /* If we have already issued an error message we don't need
13490 to issue another one. */
13491 if ((decl != error_mark_node
13492 && DECL_INITIAL (decl) != error_mark_node)
13493 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13494 cp_parser_error (parser, "expected %<,%> or %<;%>");
13495 /* Skip tokens until we reach the end of the statement. */
13496 cp_parser_skip_to_end_of_statement (parser);
13497 /* If the next token is now a `;', consume it. */
13498 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13499 cp_lexer_consume_token (parser->lexer);
13500 goto done;
13502 /* After the first time around, a function-definition is not
13503 allowed -- even if it was OK at first. For example:
13505 int i, f() {}
13507 is not valid. */
13508 function_definition_allowed_p = false;
13511 /* Issue an error message if no declarators are present, and the
13512 decl-specifier-seq does not itself declare a class or
13513 enumeration: [dcl.dcl]/3. */
13514 if (!saw_declarator)
13516 if (cp_parser_declares_only_class_p (parser))
13518 if (!declares_class_or_enum
13519 && decl_specifiers.type
13520 && OVERLOAD_TYPE_P (decl_specifiers.type))
13521 /* Ensure an error is issued anyway when finish_decltype_type,
13522 called via cp_parser_decl_specifier_seq, returns a class or
13523 an enumeration (c++/51786). */
13524 decl_specifiers.type = NULL_TREE;
13525 shadow_tag (&decl_specifiers);
13527 /* Perform any deferred access checks. */
13528 perform_deferred_access_checks (tf_warning_or_error);
13531 /* Consume the `;'. */
13532 finish:
13533 if (!maybe_range_for_decl)
13534 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13535 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13537 if (init_loc != UNKNOWN_LOCATION)
13538 error_at (init_loc, "initializer in range-based %<for%> loop");
13539 if (comma_loc != UNKNOWN_LOCATION)
13540 error_at (comma_loc,
13541 "multiple declarations in range-based %<for%> loop");
13544 done:
13545 pop_deferring_access_checks ();
13548 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13549 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13550 initializer ; */
13552 static tree
13553 cp_parser_decomposition_declaration (cp_parser *parser,
13554 cp_decl_specifier_seq *decl_specifiers,
13555 tree *maybe_range_for_decl,
13556 location_t *init_loc)
13558 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13559 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13560 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13562 /* Parse the identifier-list. */
13563 auto_vec<cp_expr, 10> v;
13564 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13565 while (true)
13567 cp_expr e = cp_parser_identifier (parser);
13568 if (e.get_value () == error_mark_node)
13569 break;
13570 v.safe_push (e);
13571 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13572 break;
13573 cp_lexer_consume_token (parser->lexer);
13576 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13577 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13579 end_loc = UNKNOWN_LOCATION;
13580 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13581 false);
13582 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13583 cp_lexer_consume_token (parser->lexer);
13584 else
13586 cp_parser_skip_to_end_of_statement (parser);
13587 return error_mark_node;
13591 if (cxx_dialect < cxx17)
13592 pedwarn (loc, 0, "structured bindings only available with "
13593 "-std=c++17 or -std=gnu++17");
13595 tree pushed_scope;
13596 cp_declarator *declarator = make_declarator (cdk_decomp);
13597 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13598 declarator->id_loc = loc;
13599 if (ref_qual != REF_QUAL_NONE)
13600 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13601 ref_qual == REF_QUAL_RVALUE,
13602 NULL_TREE);
13603 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13604 NULL_TREE, decl_specifiers->attributes,
13605 &pushed_scope);
13606 tree orig_decl = decl;
13608 unsigned int i;
13609 cp_expr e;
13610 cp_decl_specifier_seq decl_specs;
13611 clear_decl_specs (&decl_specs);
13612 decl_specs.type = make_auto ();
13613 tree prev = decl;
13614 FOR_EACH_VEC_ELT (v, i, e)
13616 if (i == 0)
13617 declarator = make_id_declarator (NULL_TREE, e.get_value (),
13618 sfk_none, e.get_location ());
13619 else
13621 declarator->u.id.unqualified_name = e.get_value ();
13622 declarator->id_loc = e.get_location ();
13624 tree elt_pushed_scope;
13625 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13626 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13627 if (decl2 == error_mark_node)
13628 decl = error_mark_node;
13629 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13631 /* Ensure we've diagnosed redeclaration if we aren't creating
13632 a new VAR_DECL. */
13633 gcc_assert (errorcount);
13634 decl = error_mark_node;
13636 else
13637 prev = decl2;
13638 if (elt_pushed_scope)
13639 pop_scope (elt_pushed_scope);
13642 if (v.is_empty ())
13644 error_at (loc, "empty structured binding declaration");
13645 decl = error_mark_node;
13648 if (maybe_range_for_decl == NULL
13649 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13651 bool non_constant_p = false, is_direct_init = false;
13652 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13653 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13654 &non_constant_p);
13655 if (initializer == NULL_TREE
13656 || (TREE_CODE (initializer) == TREE_LIST
13657 && TREE_CHAIN (initializer))
13658 || (is_direct_init
13659 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13660 && CONSTRUCTOR_NELTS (initializer) != 1))
13662 error_at (loc, "invalid initializer for structured binding "
13663 "declaration");
13664 initializer = error_mark_node;
13667 if (decl != error_mark_node)
13669 cp_maybe_mangle_decomp (decl, prev, v.length ());
13670 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13671 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13672 cp_finish_decomp (decl, prev, v.length ());
13675 else if (decl != error_mark_node)
13677 *maybe_range_for_decl = prev;
13678 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13679 the underlying DECL. */
13680 cp_finish_decomp (decl, prev, v.length ());
13683 if (pushed_scope)
13684 pop_scope (pushed_scope);
13686 if (decl == error_mark_node && DECL_P (orig_decl))
13688 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13689 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13692 return decl;
13695 /* Parse a decl-specifier-seq.
13697 decl-specifier-seq:
13698 decl-specifier-seq [opt] decl-specifier
13699 decl-specifier attribute-specifier-seq [opt] (C++11)
13701 decl-specifier:
13702 storage-class-specifier
13703 type-specifier
13704 function-specifier
13705 friend
13706 typedef
13708 GNU Extension:
13710 decl-specifier:
13711 attributes
13713 Concepts Extension:
13715 decl-specifier:
13716 concept
13718 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13720 The parser flags FLAGS is used to control type-specifier parsing.
13722 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13723 flags:
13725 1: one of the decl-specifiers is an elaborated-type-specifier
13726 (i.e., a type declaration)
13727 2: one of the decl-specifiers is an enum-specifier or a
13728 class-specifier (i.e., a type definition)
13732 static void
13733 cp_parser_decl_specifier_seq (cp_parser* parser,
13734 cp_parser_flags flags,
13735 cp_decl_specifier_seq *decl_specs,
13736 int* declares_class_or_enum)
13738 bool constructor_possible_p = !parser->in_declarator_p;
13739 bool found_decl_spec = false;
13740 cp_token *start_token = NULL;
13741 cp_decl_spec ds;
13743 /* Clear DECL_SPECS. */
13744 clear_decl_specs (decl_specs);
13746 /* Assume no class or enumeration type is declared. */
13747 *declares_class_or_enum = 0;
13749 /* Keep reading specifiers until there are no more to read. */
13750 while (true)
13752 bool constructor_p;
13753 cp_token *token;
13754 ds = ds_last;
13756 /* Peek at the next token. */
13757 token = cp_lexer_peek_token (parser->lexer);
13759 /* Save the first token of the decl spec list for error
13760 reporting. */
13761 if (!start_token)
13762 start_token = token;
13763 /* Handle attributes. */
13764 if (cp_next_tokens_can_be_attribute_p (parser))
13766 /* Parse the attributes. */
13767 tree attrs = cp_parser_attributes_opt (parser);
13769 /* In a sequence of declaration specifiers, c++11 attributes
13770 appertain to the type that precede them. In that case
13771 [dcl.spec]/1 says:
13773 The attribute-specifier-seq affects the type only for
13774 the declaration it appears in, not other declarations
13775 involving the same type.
13777 But for now let's force the user to position the
13778 attribute either at the beginning of the declaration or
13779 after the declarator-id, which would clearly mean that it
13780 applies to the declarator. */
13781 if (cxx11_attribute_p (attrs))
13783 if (!found_decl_spec)
13784 /* The c++11 attribute is at the beginning of the
13785 declaration. It appertains to the entity being
13786 declared. */;
13787 else
13789 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13791 /* This is an attribute following a
13792 class-specifier. */
13793 if (decl_specs->type_definition_p)
13794 warn_misplaced_attr_for_class_type (token->location,
13795 decl_specs->type);
13796 attrs = NULL_TREE;
13798 else
13800 decl_specs->std_attributes
13801 = attr_chainon (decl_specs->std_attributes, attrs);
13802 if (decl_specs->locations[ds_std_attribute] == 0)
13803 decl_specs->locations[ds_std_attribute] = token->location;
13805 continue;
13809 decl_specs->attributes
13810 = attr_chainon (decl_specs->attributes, attrs);
13811 if (decl_specs->locations[ds_attribute] == 0)
13812 decl_specs->locations[ds_attribute] = token->location;
13813 continue;
13815 /* Assume we will find a decl-specifier keyword. */
13816 found_decl_spec = true;
13817 /* If the next token is an appropriate keyword, we can simply
13818 add it to the list. */
13819 switch (token->keyword)
13821 /* decl-specifier:
13822 friend
13823 constexpr */
13824 case RID_FRIEND:
13825 if (!at_class_scope_p ())
13827 gcc_rich_location richloc (token->location);
13828 richloc.add_fixit_remove ();
13829 error_at (&richloc, "%<friend%> used outside of class");
13830 cp_lexer_purge_token (parser->lexer);
13832 else
13834 ds = ds_friend;
13835 /* Consume the token. */
13836 cp_lexer_consume_token (parser->lexer);
13838 break;
13840 case RID_CONSTEXPR:
13841 ds = ds_constexpr;
13842 cp_lexer_consume_token (parser->lexer);
13843 break;
13845 case RID_CONCEPT:
13846 ds = ds_concept;
13847 cp_lexer_consume_token (parser->lexer);
13848 break;
13850 /* function-specifier:
13851 inline
13852 virtual
13853 explicit */
13854 case RID_INLINE:
13855 case RID_VIRTUAL:
13856 case RID_EXPLICIT:
13857 cp_parser_function_specifier_opt (parser, decl_specs);
13858 break;
13860 /* decl-specifier:
13861 typedef */
13862 case RID_TYPEDEF:
13863 ds = ds_typedef;
13864 /* Consume the token. */
13865 cp_lexer_consume_token (parser->lexer);
13866 /* A constructor declarator cannot appear in a typedef. */
13867 constructor_possible_p = false;
13868 /* The "typedef" keyword can only occur in a declaration; we
13869 may as well commit at this point. */
13870 cp_parser_commit_to_tentative_parse (parser);
13872 if (decl_specs->storage_class != sc_none)
13873 decl_specs->conflicting_specifiers_p = true;
13874 break;
13876 /* storage-class-specifier:
13877 auto
13878 register
13879 static
13880 extern
13881 mutable
13883 GNU Extension:
13884 thread */
13885 case RID_AUTO:
13886 if (cxx_dialect == cxx98)
13888 /* Consume the token. */
13889 cp_lexer_consume_token (parser->lexer);
13891 /* Complain about `auto' as a storage specifier, if
13892 we're complaining about C++0x compatibility. */
13893 gcc_rich_location richloc (token->location);
13894 richloc.add_fixit_remove ();
13895 warning_at (&richloc, OPT_Wc__11_compat,
13896 "%<auto%> changes meaning in C++11; "
13897 "please remove it");
13899 /* Set the storage class anyway. */
13900 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13901 token);
13903 else
13904 /* C++0x auto type-specifier. */
13905 found_decl_spec = false;
13906 break;
13908 case RID_REGISTER:
13909 case RID_STATIC:
13910 case RID_EXTERN:
13911 case RID_MUTABLE:
13912 /* Consume the token. */
13913 cp_lexer_consume_token (parser->lexer);
13914 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13915 token);
13916 break;
13917 case RID_THREAD:
13918 /* Consume the token. */
13919 ds = ds_thread;
13920 cp_lexer_consume_token (parser->lexer);
13921 break;
13923 default:
13924 /* We did not yet find a decl-specifier yet. */
13925 found_decl_spec = false;
13926 break;
13929 if (found_decl_spec
13930 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13931 && token->keyword != RID_CONSTEXPR)
13932 error ("decl-specifier invalid in condition");
13934 if (found_decl_spec
13935 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13936 && token->keyword != RID_MUTABLE
13937 && token->keyword != RID_CONSTEXPR)
13938 error_at (token->location, "%qD invalid in lambda",
13939 ridpointers[token->keyword]);
13941 if (ds != ds_last)
13942 set_and_check_decl_spec_loc (decl_specs, ds, token);
13944 /* Constructors are a special case. The `S' in `S()' is not a
13945 decl-specifier; it is the beginning of the declarator. */
13946 constructor_p
13947 = (!found_decl_spec
13948 && constructor_possible_p
13949 && (cp_parser_constructor_declarator_p
13950 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13952 /* If we don't have a DECL_SPEC yet, then we must be looking at
13953 a type-specifier. */
13954 if (!found_decl_spec && !constructor_p)
13956 int decl_spec_declares_class_or_enum;
13957 bool is_cv_qualifier;
13958 tree type_spec;
13960 type_spec
13961 = cp_parser_type_specifier (parser, flags,
13962 decl_specs,
13963 /*is_declaration=*/true,
13964 &decl_spec_declares_class_or_enum,
13965 &is_cv_qualifier);
13966 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13968 /* If this type-specifier referenced a user-defined type
13969 (a typedef, class-name, etc.), then we can't allow any
13970 more such type-specifiers henceforth.
13972 [dcl.spec]
13974 The longest sequence of decl-specifiers that could
13975 possibly be a type name is taken as the
13976 decl-specifier-seq of a declaration. The sequence shall
13977 be self-consistent as described below.
13979 [dcl.type]
13981 As a general rule, at most one type-specifier is allowed
13982 in the complete decl-specifier-seq of a declaration. The
13983 only exceptions are the following:
13985 -- const or volatile can be combined with any other
13986 type-specifier.
13988 -- signed or unsigned can be combined with char, long,
13989 short, or int.
13991 -- ..
13993 Example:
13995 typedef char* Pc;
13996 void g (const int Pc);
13998 Here, Pc is *not* part of the decl-specifier seq; it's
13999 the declarator. Therefore, once we see a type-specifier
14000 (other than a cv-qualifier), we forbid any additional
14001 user-defined types. We *do* still allow things like `int
14002 int' to be considered a decl-specifier-seq, and issue the
14003 error message later. */
14004 if (type_spec && !is_cv_qualifier)
14005 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14006 /* A constructor declarator cannot follow a type-specifier. */
14007 if (type_spec)
14009 constructor_possible_p = false;
14010 found_decl_spec = true;
14011 if (!is_cv_qualifier)
14012 decl_specs->any_type_specifiers_p = true;
14014 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14015 error_at (token->location, "type-specifier invalid in lambda");
14019 /* If we still do not have a DECL_SPEC, then there are no more
14020 decl-specifiers. */
14021 if (!found_decl_spec)
14022 break;
14024 decl_specs->any_specifiers_p = true;
14025 /* After we see one decl-specifier, further decl-specifiers are
14026 always optional. */
14027 flags |= CP_PARSER_FLAGS_OPTIONAL;
14030 /* Don't allow a friend specifier with a class definition. */
14031 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14032 && (*declares_class_or_enum & 2))
14033 error_at (decl_specs->locations[ds_friend],
14034 "class definition may not be declared a friend");
14037 /* Parse an (optional) storage-class-specifier.
14039 storage-class-specifier:
14040 auto
14041 register
14042 static
14043 extern
14044 mutable
14046 GNU Extension:
14048 storage-class-specifier:
14049 thread
14051 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14053 static tree
14054 cp_parser_storage_class_specifier_opt (cp_parser* parser)
14056 switch (cp_lexer_peek_token (parser->lexer)->keyword)
14058 case RID_AUTO:
14059 if (cxx_dialect != cxx98)
14060 return NULL_TREE;
14061 /* Fall through for C++98. */
14062 gcc_fallthrough ();
14064 case RID_REGISTER:
14065 case RID_STATIC:
14066 case RID_EXTERN:
14067 case RID_MUTABLE:
14068 case RID_THREAD:
14069 /* Consume the token. */
14070 return cp_lexer_consume_token (parser->lexer)->u.value;
14072 default:
14073 return NULL_TREE;
14077 /* Parse an (optional) function-specifier.
14079 function-specifier:
14080 inline
14081 virtual
14082 explicit
14084 C++2A Extension:
14085 explicit(constant-expression)
14087 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14088 Updates DECL_SPECS, if it is non-NULL. */
14090 static tree
14091 cp_parser_function_specifier_opt (cp_parser* parser,
14092 cp_decl_specifier_seq *decl_specs)
14094 cp_token *token = cp_lexer_peek_token (parser->lexer);
14095 switch (token->keyword)
14097 case RID_INLINE:
14098 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14099 break;
14101 case RID_VIRTUAL:
14102 /* 14.5.2.3 [temp.mem]
14104 A member function template shall not be virtual. */
14105 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14106 && current_class_type)
14107 error_at (token->location, "templates may not be %<virtual%>");
14108 else
14109 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14110 break;
14112 case RID_EXPLICIT:
14114 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14115 /* If we see '(', it's C++20 explicit(bool). */
14116 tree expr;
14117 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14119 matching_parens parens;
14120 parens.consume_open (parser);
14122 /* New types are not allowed in an explicit-specifier. */
14123 const char *saved_message
14124 = parser->type_definition_forbidden_message;
14125 parser->type_definition_forbidden_message
14126 = G_("types may not be defined in explicit-specifier");
14128 if (cxx_dialect < cxx2a)
14129 pedwarn (token->location, 0,
14130 "%<explicit(bool)%> only available with -std=c++2a "
14131 "or -std=gnu++2a");
14133 /* Parse the constant-expression. */
14134 expr = cp_parser_constant_expression (parser);
14136 /* Restore the saved message. */
14137 parser->type_definition_forbidden_message = saved_message;
14138 parens.require_close (parser);
14140 else
14141 /* The explicit-specifier explicit without a constant-expression is
14142 equivalent to the explicit-specifier explicit(true). */
14143 expr = boolean_true_node;
14145 /* [dcl.fct.spec]
14146 "the constant-expression, if supplied, shall be a contextually
14147 converted constant expression of type bool." */
14148 expr = build_explicit_specifier (expr, tf_warning_or_error);
14149 /* We could evaluate it -- mark the decl as appropriate. */
14150 if (expr == boolean_true_node)
14151 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14152 else if (expr == boolean_false_node)
14153 /* Don't mark the decl as explicit. */;
14154 else if (decl_specs)
14155 /* The expression was value-dependent. Remember it so that we can
14156 substitute it later. */
14157 decl_specs->explicit_specifier = expr;
14158 return id;
14161 default:
14162 return NULL_TREE;
14165 /* Consume the token. */
14166 return cp_lexer_consume_token (parser->lexer)->u.value;
14169 /* Parse a linkage-specification.
14171 linkage-specification:
14172 extern string-literal { declaration-seq [opt] }
14173 extern string-literal declaration */
14175 static void
14176 cp_parser_linkage_specification (cp_parser* parser)
14178 tree linkage;
14180 /* Look for the `extern' keyword. */
14181 cp_token *extern_token
14182 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14184 /* Look for the string-literal. */
14185 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14186 linkage = cp_parser_string_literal (parser, false, false);
14188 /* Transform the literal into an identifier. If the literal is a
14189 wide-character string, or contains embedded NULs, then we can't
14190 handle it as the user wants. */
14191 if (strlen (TREE_STRING_POINTER (linkage))
14192 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14194 cp_parser_error (parser, "invalid linkage-specification");
14195 /* Assume C++ linkage. */
14196 linkage = lang_name_cplusplus;
14198 else
14199 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14201 /* We're now using the new linkage. */
14202 push_lang_context (linkage);
14204 /* Preserve the location of the the innermost linkage specification,
14205 tracking the locations of nested specifications via a local. */
14206 location_t saved_location
14207 = parser->innermost_linkage_specification_location;
14208 /* Construct a location ranging from the start of the "extern" to
14209 the end of the string-literal, with the caret at the start, e.g.:
14210 extern "C" {
14211 ^~~~~~~~~~
14213 parser->innermost_linkage_specification_location
14214 = make_location (extern_token->location,
14215 extern_token->location,
14216 get_finish (string_token->location));
14218 /* If the next token is a `{', then we're using the first
14219 production. */
14220 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14222 cp_ensure_no_omp_declare_simd (parser);
14223 cp_ensure_no_oacc_routine (parser);
14225 /* Consume the `{' token. */
14226 matching_braces braces;
14227 braces.consume_open (parser);
14228 /* Parse the declarations. */
14229 cp_parser_declaration_seq_opt (parser);
14230 /* Look for the closing `}'. */
14231 braces.require_close (parser);
14233 /* Otherwise, there's just one declaration. */
14234 else
14236 bool saved_in_unbraced_linkage_specification_p;
14238 saved_in_unbraced_linkage_specification_p
14239 = parser->in_unbraced_linkage_specification_p;
14240 parser->in_unbraced_linkage_specification_p = true;
14241 cp_parser_declaration (parser);
14242 parser->in_unbraced_linkage_specification_p
14243 = saved_in_unbraced_linkage_specification_p;
14246 /* We're done with the linkage-specification. */
14247 pop_lang_context ();
14249 /* Restore location of parent linkage specification, if any. */
14250 parser->innermost_linkage_specification_location = saved_location;
14253 /* Parse a static_assert-declaration.
14255 static_assert-declaration:
14256 static_assert ( constant-expression , string-literal ) ;
14257 static_assert ( constant-expression ) ; (C++17)
14259 If MEMBER_P, this static_assert is a class member. */
14261 static void
14262 cp_parser_static_assert(cp_parser *parser, bool member_p)
14264 cp_expr condition;
14265 location_t token_loc;
14266 tree message;
14267 bool dummy;
14269 /* Peek at the `static_assert' token so we can keep track of exactly
14270 where the static assertion started. */
14271 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14273 /* Look for the `static_assert' keyword. */
14274 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14275 RT_STATIC_ASSERT))
14276 return;
14278 /* We know we are in a static assertion; commit to any tentative
14279 parse. */
14280 if (cp_parser_parsing_tentatively (parser))
14281 cp_parser_commit_to_tentative_parse (parser);
14283 /* Parse the `(' starting the static assertion condition. */
14284 matching_parens parens;
14285 parens.require_open (parser);
14287 /* Parse the constant-expression. Allow a non-constant expression
14288 here in order to give better diagnostics in finish_static_assert. */
14289 condition =
14290 cp_parser_constant_expression (parser,
14291 /*allow_non_constant_p=*/true,
14292 /*non_constant_p=*/&dummy);
14294 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14296 if (cxx_dialect < cxx17)
14297 pedwarn (input_location, OPT_Wpedantic,
14298 "static_assert without a message "
14299 "only available with -std=c++17 or -std=gnu++17");
14300 /* Eat the ')' */
14301 cp_lexer_consume_token (parser->lexer);
14302 message = build_string (1, "");
14303 TREE_TYPE (message) = char_array_type_node;
14304 fix_string_type (message);
14306 else
14308 /* Parse the separating `,'. */
14309 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14311 /* Parse the string-literal message. */
14312 message = cp_parser_string_literal (parser,
14313 /*translate=*/false,
14314 /*wide_ok=*/true);
14316 /* A `)' completes the static assertion. */
14317 if (!parens.require_close (parser))
14318 cp_parser_skip_to_closing_parenthesis (parser,
14319 /*recovering=*/true,
14320 /*or_comma=*/false,
14321 /*consume_paren=*/true);
14324 /* A semicolon terminates the declaration. */
14325 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14327 /* Get the location for the static assertion. Use that of the
14328 condition if available, otherwise, use that of the "static_assert"
14329 token. */
14330 location_t assert_loc = condition.get_location ();
14331 if (assert_loc == UNKNOWN_LOCATION)
14332 assert_loc = token_loc;
14334 /* Complete the static assertion, which may mean either processing
14335 the static assert now or saving it for template instantiation. */
14336 finish_static_assert (condition, message, assert_loc, member_p);
14339 /* Parse the expression in decltype ( expression ). */
14341 static tree
14342 cp_parser_decltype_expr (cp_parser *parser,
14343 bool &id_expression_or_member_access_p)
14345 cp_token *id_expr_start_token;
14346 tree expr;
14348 /* Since we're going to preserve any side-effects from this parse, set up a
14349 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14350 in the expression. */
14351 tentative_firewall firewall (parser);
14353 /* First, try parsing an id-expression. */
14354 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14355 cp_parser_parse_tentatively (parser);
14356 expr = cp_parser_id_expression (parser,
14357 /*template_keyword_p=*/false,
14358 /*check_dependency_p=*/true,
14359 /*template_p=*/NULL,
14360 /*declarator_p=*/false,
14361 /*optional_p=*/false);
14363 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14365 bool non_integral_constant_expression_p = false;
14366 tree id_expression = expr;
14367 cp_id_kind idk;
14368 const char *error_msg;
14370 if (identifier_p (expr))
14371 /* Lookup the name we got back from the id-expression. */
14372 expr = cp_parser_lookup_name_simple (parser, expr,
14373 id_expr_start_token->location);
14375 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14376 /* A template without args is not a complete id-expression. */
14377 expr = error_mark_node;
14379 if (expr
14380 && expr != error_mark_node
14381 && TREE_CODE (expr) != TYPE_DECL
14382 && (TREE_CODE (expr) != BIT_NOT_EXPR
14383 || !TYPE_P (TREE_OPERAND (expr, 0)))
14384 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14386 /* Complete lookup of the id-expression. */
14387 expr = (finish_id_expression
14388 (id_expression, expr, parser->scope, &idk,
14389 /*integral_constant_expression_p=*/false,
14390 /*allow_non_integral_constant_expression_p=*/true,
14391 &non_integral_constant_expression_p,
14392 /*template_p=*/false,
14393 /*done=*/true,
14394 /*address_p=*/false,
14395 /*template_arg_p=*/false,
14396 &error_msg,
14397 id_expr_start_token->location));
14399 if (expr == error_mark_node)
14400 /* We found an id-expression, but it was something that we
14401 should not have found. This is an error, not something
14402 we can recover from, so note that we found an
14403 id-expression and we'll recover as gracefully as
14404 possible. */
14405 id_expression_or_member_access_p = true;
14408 if (expr
14409 && expr != error_mark_node
14410 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14411 /* We have an id-expression. */
14412 id_expression_or_member_access_p = true;
14415 if (!id_expression_or_member_access_p)
14417 /* Abort the id-expression parse. */
14418 cp_parser_abort_tentative_parse (parser);
14420 /* Parsing tentatively, again. */
14421 cp_parser_parse_tentatively (parser);
14423 /* Parse a class member access. */
14424 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14425 /*cast_p=*/false, /*decltype*/true,
14426 /*member_access_only_p=*/true, NULL);
14428 if (expr
14429 && expr != error_mark_node
14430 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14431 /* We have an id-expression. */
14432 id_expression_or_member_access_p = true;
14435 if (id_expression_or_member_access_p)
14436 /* We have parsed the complete id-expression or member access. */
14437 cp_parser_parse_definitely (parser);
14438 else
14440 /* Abort our attempt to parse an id-expression or member access
14441 expression. */
14442 cp_parser_abort_tentative_parse (parser);
14444 /* Commit to the tentative_firewall so we get syntax errors. */
14445 cp_parser_commit_to_tentative_parse (parser);
14447 /* Parse a full expression. */
14448 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14449 /*decltype_p=*/true);
14452 return expr;
14455 /* Parse a `decltype' type. Returns the type.
14457 simple-type-specifier:
14458 decltype ( expression )
14459 C++14 proposal:
14460 decltype ( auto ) */
14462 static tree
14463 cp_parser_decltype (cp_parser *parser)
14465 bool id_expression_or_member_access_p = false;
14466 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14468 if (start_token->type == CPP_DECLTYPE)
14470 /* Already parsed. */
14471 cp_lexer_consume_token (parser->lexer);
14472 return saved_checks_value (start_token->u.tree_check_value);
14475 /* Look for the `decltype' token. */
14476 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14477 return error_mark_node;
14479 /* Parse the opening `('. */
14480 matching_parens parens;
14481 if (!parens.require_open (parser))
14482 return error_mark_node;
14484 push_deferring_access_checks (dk_deferred);
14486 tree expr = NULL_TREE;
14488 if (cxx_dialect >= cxx14
14489 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14490 /* decltype (auto) */
14491 cp_lexer_consume_token (parser->lexer);
14492 else
14494 /* decltype (expression) */
14496 /* Types cannot be defined in a `decltype' expression. Save away the
14497 old message and set the new one. */
14498 const char *saved_message = parser->type_definition_forbidden_message;
14499 parser->type_definition_forbidden_message
14500 = G_("types may not be defined in %<decltype%> expressions");
14502 /* The restrictions on constant-expressions do not apply inside
14503 decltype expressions. */
14504 bool saved_integral_constant_expression_p
14505 = parser->integral_constant_expression_p;
14506 bool saved_non_integral_constant_expression_p
14507 = parser->non_integral_constant_expression_p;
14508 parser->integral_constant_expression_p = false;
14510 /* Within a parenthesized expression, a `>' token is always
14511 the greater-than operator. */
14512 bool saved_greater_than_is_operator_p
14513 = parser->greater_than_is_operator_p;
14514 parser->greater_than_is_operator_p = true;
14516 /* Do not actually evaluate the expression. */
14517 ++cp_unevaluated_operand;
14519 /* Do not warn about problems with the expression. */
14520 ++c_inhibit_evaluation_warnings;
14522 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14524 /* Go back to evaluating expressions. */
14525 --cp_unevaluated_operand;
14526 --c_inhibit_evaluation_warnings;
14528 /* The `>' token might be the end of a template-id or
14529 template-parameter-list now. */
14530 parser->greater_than_is_operator_p
14531 = saved_greater_than_is_operator_p;
14533 /* Restore the old message and the integral constant expression
14534 flags. */
14535 parser->type_definition_forbidden_message = saved_message;
14536 parser->integral_constant_expression_p
14537 = saved_integral_constant_expression_p;
14538 parser->non_integral_constant_expression_p
14539 = saved_non_integral_constant_expression_p;
14542 /* Parse to the closing `)'. */
14543 if (!parens.require_close (parser))
14545 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14546 /*consume_paren=*/true);
14547 pop_deferring_access_checks ();
14548 return error_mark_node;
14551 if (!expr)
14553 /* Build auto. */
14554 expr = make_decltype_auto ();
14555 AUTO_IS_DECLTYPE (expr) = true;
14557 else
14558 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14559 tf_warning_or_error);
14561 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14562 it again. */
14563 start_token->type = CPP_DECLTYPE;
14564 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14565 start_token->u.tree_check_value->value = expr;
14566 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14567 start_token->keyword = RID_MAX;
14568 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14570 pop_to_parent_deferring_access_checks ();
14572 return expr;
14575 /* Special member functions [gram.special] */
14577 /* Parse a conversion-function-id.
14579 conversion-function-id:
14580 operator conversion-type-id
14582 Returns an IDENTIFIER_NODE representing the operator. */
14584 static tree
14585 cp_parser_conversion_function_id (cp_parser* parser)
14587 tree type;
14588 tree saved_scope;
14589 tree saved_qualifying_scope;
14590 tree saved_object_scope;
14591 tree pushed_scope = NULL_TREE;
14593 /* Look for the `operator' token. */
14594 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14595 return error_mark_node;
14596 /* When we parse the conversion-type-id, the current scope will be
14597 reset. However, we need that information in able to look up the
14598 conversion function later, so we save it here. */
14599 saved_scope = parser->scope;
14600 saved_qualifying_scope = parser->qualifying_scope;
14601 saved_object_scope = parser->object_scope;
14602 /* We must enter the scope of the class so that the names of
14603 entities declared within the class are available in the
14604 conversion-type-id. For example, consider:
14606 struct S {
14607 typedef int I;
14608 operator I();
14611 S::operator I() { ... }
14613 In order to see that `I' is a type-name in the definition, we
14614 must be in the scope of `S'. */
14615 if (saved_scope)
14616 pushed_scope = push_scope (saved_scope);
14617 /* Parse the conversion-type-id. */
14618 type = cp_parser_conversion_type_id (parser);
14619 /* Leave the scope of the class, if any. */
14620 if (pushed_scope)
14621 pop_scope (pushed_scope);
14622 /* Restore the saved scope. */
14623 parser->scope = saved_scope;
14624 parser->qualifying_scope = saved_qualifying_scope;
14625 parser->object_scope = saved_object_scope;
14626 /* If the TYPE is invalid, indicate failure. */
14627 if (type == error_mark_node)
14628 return error_mark_node;
14629 return make_conv_op_name (type);
14632 /* Parse a conversion-type-id:
14634 conversion-type-id:
14635 type-specifier-seq conversion-declarator [opt]
14637 Returns the TYPE specified. */
14639 static tree
14640 cp_parser_conversion_type_id (cp_parser* parser)
14642 tree attributes;
14643 cp_decl_specifier_seq type_specifiers;
14644 cp_declarator *declarator;
14645 tree type_specified;
14646 const char *saved_message;
14648 /* Parse the attributes. */
14649 attributes = cp_parser_attributes_opt (parser);
14651 saved_message = parser->type_definition_forbidden_message;
14652 parser->type_definition_forbidden_message
14653 = G_("types may not be defined in a conversion-type-id");
14655 /* Parse the type-specifiers. */
14656 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14657 /*is_trailing_return=*/false,
14658 &type_specifiers);
14660 parser->type_definition_forbidden_message = saved_message;
14662 /* If that didn't work, stop. */
14663 if (type_specifiers.type == error_mark_node)
14664 return error_mark_node;
14665 /* Parse the conversion-declarator. */
14666 declarator = cp_parser_conversion_declarator_opt (parser);
14668 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14669 /*initialized=*/0, &attributes);
14670 if (attributes)
14671 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14673 /* Don't give this error when parsing tentatively. This happens to
14674 work because we always parse this definitively once. */
14675 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14676 && type_uses_auto (type_specified))
14678 if (cxx_dialect < cxx14)
14680 error ("invalid use of %<auto%> in conversion operator");
14681 return error_mark_node;
14683 else if (template_parm_scope_p ())
14684 warning (0, "use of %<auto%> in member template "
14685 "conversion operator can never be deduced");
14688 return type_specified;
14691 /* Parse an (optional) conversion-declarator.
14693 conversion-declarator:
14694 ptr-operator conversion-declarator [opt]
14698 static cp_declarator *
14699 cp_parser_conversion_declarator_opt (cp_parser* parser)
14701 enum tree_code code;
14702 tree class_type, std_attributes = NULL_TREE;
14703 cp_cv_quals cv_quals;
14705 /* We don't know if there's a ptr-operator next, or not. */
14706 cp_parser_parse_tentatively (parser);
14707 /* Try the ptr-operator. */
14708 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14709 &std_attributes);
14710 /* If it worked, look for more conversion-declarators. */
14711 if (cp_parser_parse_definitely (parser))
14713 cp_declarator *declarator;
14715 /* Parse another optional declarator. */
14716 declarator = cp_parser_conversion_declarator_opt (parser);
14718 declarator = cp_parser_make_indirect_declarator
14719 (code, class_type, cv_quals, declarator, std_attributes);
14721 return declarator;
14724 return NULL;
14727 /* Parse an (optional) ctor-initializer.
14729 ctor-initializer:
14730 : mem-initializer-list */
14732 static void
14733 cp_parser_ctor_initializer_opt (cp_parser* parser)
14735 /* If the next token is not a `:', then there is no
14736 ctor-initializer. */
14737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14739 /* Do default initialization of any bases and members. */
14740 if (DECL_CONSTRUCTOR_P (current_function_decl))
14741 finish_mem_initializers (NULL_TREE);
14742 return;
14745 /* Consume the `:' token. */
14746 cp_lexer_consume_token (parser->lexer);
14747 /* And the mem-initializer-list. */
14748 cp_parser_mem_initializer_list (parser);
14751 /* Parse a mem-initializer-list.
14753 mem-initializer-list:
14754 mem-initializer ... [opt]
14755 mem-initializer ... [opt] , mem-initializer-list */
14757 static void
14758 cp_parser_mem_initializer_list (cp_parser* parser)
14760 tree mem_initializer_list = NULL_TREE;
14761 tree target_ctor = error_mark_node;
14762 cp_token *token = cp_lexer_peek_token (parser->lexer);
14764 /* Let the semantic analysis code know that we are starting the
14765 mem-initializer-list. */
14766 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14767 error_at (token->location,
14768 "only constructors take member initializers");
14770 /* Loop through the list. */
14771 while (true)
14773 tree mem_initializer;
14775 token = cp_lexer_peek_token (parser->lexer);
14776 /* Parse the mem-initializer. */
14777 mem_initializer = cp_parser_mem_initializer (parser);
14778 /* If the next token is a `...', we're expanding member initializers. */
14779 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14780 if (ellipsis
14781 || (mem_initializer != error_mark_node
14782 && check_for_bare_parameter_packs (TREE_PURPOSE
14783 (mem_initializer))))
14785 /* Consume the `...'. */
14786 if (ellipsis)
14787 cp_lexer_consume_token (parser->lexer);
14789 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14790 can be expanded but members cannot. */
14791 if (mem_initializer != error_mark_node
14792 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14794 error_at (token->location,
14795 "cannot expand initializer for member %qD",
14796 TREE_PURPOSE (mem_initializer));
14797 mem_initializer = error_mark_node;
14800 /* Construct the pack expansion type. */
14801 if (mem_initializer != error_mark_node)
14802 mem_initializer = make_pack_expansion (mem_initializer);
14804 if (target_ctor != error_mark_node
14805 && mem_initializer != error_mark_node)
14807 error ("mem-initializer for %qD follows constructor delegation",
14808 TREE_PURPOSE (mem_initializer));
14809 mem_initializer = error_mark_node;
14811 /* Look for a target constructor. */
14812 if (mem_initializer != error_mark_node
14813 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14814 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14816 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14817 if (mem_initializer_list)
14819 error ("constructor delegation follows mem-initializer for %qD",
14820 TREE_PURPOSE (mem_initializer_list));
14821 mem_initializer = error_mark_node;
14823 target_ctor = mem_initializer;
14825 /* Add it to the list, unless it was erroneous. */
14826 if (mem_initializer != error_mark_node)
14828 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14829 mem_initializer_list = mem_initializer;
14831 /* If the next token is not a `,', we're done. */
14832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14833 break;
14834 /* Consume the `,' token. */
14835 cp_lexer_consume_token (parser->lexer);
14838 /* Perform semantic analysis. */
14839 if (DECL_CONSTRUCTOR_P (current_function_decl))
14840 finish_mem_initializers (mem_initializer_list);
14843 /* Parse a mem-initializer.
14845 mem-initializer:
14846 mem-initializer-id ( expression-list [opt] )
14847 mem-initializer-id braced-init-list
14849 GNU extension:
14851 mem-initializer:
14852 ( expression-list [opt] )
14854 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14855 class) or FIELD_DECL (for a non-static data member) to initialize;
14856 the TREE_VALUE is the expression-list. An empty initialization
14857 list is represented by void_list_node. */
14859 static tree
14860 cp_parser_mem_initializer (cp_parser* parser)
14862 tree mem_initializer_id;
14863 tree expression_list;
14864 tree member;
14865 cp_token *token = cp_lexer_peek_token (parser->lexer);
14867 /* Find out what is being initialized. */
14868 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14870 permerror (token->location,
14871 "anachronistic old-style base class initializer");
14872 mem_initializer_id = NULL_TREE;
14874 else
14876 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14877 if (mem_initializer_id == error_mark_node)
14878 return mem_initializer_id;
14880 member = expand_member_init (mem_initializer_id);
14881 if (member && !DECL_P (member))
14882 in_base_initializer = 1;
14884 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14886 bool expr_non_constant_p;
14887 cp_lexer_set_source_position (parser->lexer);
14888 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14889 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14890 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14891 expression_list = build_tree_list (NULL_TREE, expression_list);
14893 else
14895 vec<tree, va_gc> *vec;
14896 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14897 /*cast_p=*/false,
14898 /*allow_expansion_p=*/true,
14899 /*non_constant_p=*/NULL);
14900 if (vec == NULL)
14901 return error_mark_node;
14902 expression_list = build_tree_list_vec (vec);
14903 release_tree_vector (vec);
14906 if (expression_list == error_mark_node)
14907 return error_mark_node;
14908 if (!expression_list)
14909 expression_list = void_type_node;
14911 in_base_initializer = 0;
14913 return member ? build_tree_list (member, expression_list) : error_mark_node;
14916 /* Parse a mem-initializer-id.
14918 mem-initializer-id:
14919 :: [opt] nested-name-specifier [opt] class-name
14920 decltype-specifier (C++11)
14921 identifier
14923 Returns a TYPE indicating the class to be initialized for the first
14924 production (and the second in C++11). Returns an IDENTIFIER_NODE
14925 indicating the data member to be initialized for the last production. */
14927 static tree
14928 cp_parser_mem_initializer_id (cp_parser* parser)
14930 bool global_scope_p;
14931 bool nested_name_specifier_p;
14932 bool template_p = false;
14933 tree id;
14935 cp_token *token = cp_lexer_peek_token (parser->lexer);
14937 /* `typename' is not allowed in this context ([temp.res]). */
14938 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14940 error_at (token->location,
14941 "keyword %<typename%> not allowed in this context (a qualified "
14942 "member initializer is implicitly a type)");
14943 cp_lexer_consume_token (parser->lexer);
14945 /* Look for the optional `::' operator. */
14946 global_scope_p
14947 = (cp_parser_global_scope_opt (parser,
14948 /*current_scope_valid_p=*/false)
14949 != NULL_TREE);
14950 /* Look for the optional nested-name-specifier. The simplest way to
14951 implement:
14953 [temp.res]
14955 The keyword `typename' is not permitted in a base-specifier or
14956 mem-initializer; in these contexts a qualified name that
14957 depends on a template-parameter is implicitly assumed to be a
14958 type name.
14960 is to assume that we have seen the `typename' keyword at this
14961 point. */
14962 nested_name_specifier_p
14963 = (cp_parser_nested_name_specifier_opt (parser,
14964 /*typename_keyword_p=*/true,
14965 /*check_dependency_p=*/true,
14966 /*type_p=*/true,
14967 /*is_declaration=*/true)
14968 != NULL_TREE);
14969 if (nested_name_specifier_p)
14970 template_p = cp_parser_optional_template_keyword (parser);
14971 /* If there is a `::' operator or a nested-name-specifier, then we
14972 are definitely looking for a class-name. */
14973 if (global_scope_p || nested_name_specifier_p)
14974 return cp_parser_class_name (parser,
14975 /*typename_keyword_p=*/true,
14976 /*template_keyword_p=*/template_p,
14977 typename_type,
14978 /*check_dependency_p=*/true,
14979 /*class_head_p=*/false,
14980 /*is_declaration=*/true);
14981 /* Otherwise, we could also be looking for an ordinary identifier. */
14982 cp_parser_parse_tentatively (parser);
14983 if (cp_lexer_next_token_is_decltype (parser->lexer))
14984 /* Try a decltype-specifier. */
14985 id = cp_parser_decltype (parser);
14986 else
14987 /* Otherwise, try a class-name. */
14988 id = cp_parser_class_name (parser,
14989 /*typename_keyword_p=*/true,
14990 /*template_keyword_p=*/false,
14991 none_type,
14992 /*check_dependency_p=*/true,
14993 /*class_head_p=*/false,
14994 /*is_declaration=*/true);
14995 /* If we found one, we're done. */
14996 if (cp_parser_parse_definitely (parser))
14997 return id;
14998 /* Otherwise, look for an ordinary identifier. */
14999 return cp_parser_identifier (parser);
15002 /* Overloading [gram.over] */
15004 /* Parse an operator-function-id.
15006 operator-function-id:
15007 operator operator
15009 Returns an IDENTIFIER_NODE for the operator which is a
15010 human-readable spelling of the identifier, e.g., `operator +'. */
15012 static cp_expr
15013 cp_parser_operator_function_id (cp_parser* parser)
15015 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15016 /* Look for the `operator' keyword. */
15017 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15018 return error_mark_node;
15019 /* And then the name of the operator itself. */
15020 return cp_parser_operator (parser, start_loc);
15023 /* Return an identifier node for a user-defined literal operator.
15024 The suffix identifier is chained to the operator name identifier. */
15026 tree
15027 cp_literal_operator_id (const char* name)
15029 tree identifier;
15030 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15031 + strlen (name) + 10);
15032 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15033 identifier = get_identifier (buffer);
15035 return identifier;
15038 /* Parse an operator.
15040 operator:
15041 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15042 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15043 || ++ -- , ->* -> () []
15045 GNU Extensions:
15047 operator:
15048 <? >? <?= >?=
15050 Returns an IDENTIFIER_NODE for the operator which is a
15051 human-readable spelling of the identifier, e.g., `operator +'. */
15053 static cp_expr
15054 cp_parser_operator (cp_parser* parser, location_t start_loc)
15056 tree id = NULL_TREE;
15057 cp_token *token;
15058 bool utf8 = false;
15060 /* Peek at the next token. */
15061 token = cp_lexer_peek_token (parser->lexer);
15063 location_t end_loc = token->location;
15065 /* Figure out which operator we have. */
15066 enum tree_code op = ERROR_MARK;
15067 bool assop = false;
15068 bool consumed = false;
15069 switch (token->type)
15071 case CPP_KEYWORD:
15073 /* The keyword should be either `new' or `delete'. */
15074 if (token->keyword == RID_NEW)
15075 op = NEW_EXPR;
15076 else if (token->keyword == RID_DELETE)
15077 op = DELETE_EXPR;
15078 else
15079 break;
15081 /* Consume the `new' or `delete' token. */
15082 end_loc = cp_lexer_consume_token (parser->lexer)->location;
15084 /* Peek at the next token. */
15085 token = cp_lexer_peek_token (parser->lexer);
15086 /* If it's a `[' token then this is the array variant of the
15087 operator. */
15088 if (token->type == CPP_OPEN_SQUARE)
15090 /* Consume the `[' token. */
15091 cp_lexer_consume_token (parser->lexer);
15092 /* Look for the `]' token. */
15093 if (cp_token *close_token
15094 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15095 end_loc = close_token->location;
15096 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15098 consumed = true;
15099 break;
15102 case CPP_PLUS:
15103 op = PLUS_EXPR;
15104 break;
15106 case CPP_MINUS:
15107 op = MINUS_EXPR;
15108 break;
15110 case CPP_MULT:
15111 op = MULT_EXPR;
15112 break;
15114 case CPP_DIV:
15115 op = TRUNC_DIV_EXPR;
15116 break;
15118 case CPP_MOD:
15119 op = TRUNC_MOD_EXPR;
15120 break;
15122 case CPP_XOR:
15123 op = BIT_XOR_EXPR;
15124 break;
15126 case CPP_AND:
15127 op = BIT_AND_EXPR;
15128 break;
15130 case CPP_OR:
15131 op = BIT_IOR_EXPR;
15132 break;
15134 case CPP_COMPL:
15135 op = BIT_NOT_EXPR;
15136 break;
15138 case CPP_NOT:
15139 op = TRUTH_NOT_EXPR;
15140 break;
15142 case CPP_EQ:
15143 assop = true;
15144 op = NOP_EXPR;
15145 break;
15147 case CPP_LESS:
15148 op = LT_EXPR;
15149 break;
15151 case CPP_GREATER:
15152 op = GT_EXPR;
15153 break;
15155 case CPP_PLUS_EQ:
15156 assop = true;
15157 op = PLUS_EXPR;
15158 break;
15160 case CPP_MINUS_EQ:
15161 assop = true;
15162 op = MINUS_EXPR;
15163 break;
15165 case CPP_MULT_EQ:
15166 assop = true;
15167 op = MULT_EXPR;
15168 break;
15170 case CPP_DIV_EQ:
15171 assop = true;
15172 op = TRUNC_DIV_EXPR;
15173 break;
15175 case CPP_MOD_EQ:
15176 assop = true;
15177 op = TRUNC_MOD_EXPR;
15178 break;
15180 case CPP_XOR_EQ:
15181 assop = true;
15182 op = BIT_XOR_EXPR;
15183 break;
15185 case CPP_AND_EQ:
15186 assop = true;
15187 op = BIT_AND_EXPR;
15188 break;
15190 case CPP_OR_EQ:
15191 assop = true;
15192 op = BIT_IOR_EXPR;
15193 break;
15195 case CPP_LSHIFT:
15196 op = LSHIFT_EXPR;
15197 break;
15199 case CPP_RSHIFT:
15200 op = RSHIFT_EXPR;
15201 break;
15203 case CPP_LSHIFT_EQ:
15204 assop = true;
15205 op = LSHIFT_EXPR;
15206 break;
15208 case CPP_RSHIFT_EQ:
15209 assop = true;
15210 op = RSHIFT_EXPR;
15211 break;
15213 case CPP_EQ_EQ:
15214 op = EQ_EXPR;
15215 break;
15217 case CPP_NOT_EQ:
15218 op = NE_EXPR;
15219 break;
15221 case CPP_LESS_EQ:
15222 op = LE_EXPR;
15223 break;
15225 case CPP_GREATER_EQ:
15226 op = GE_EXPR;
15227 break;
15229 case CPP_AND_AND:
15230 op = TRUTH_ANDIF_EXPR;
15231 break;
15233 case CPP_OR_OR:
15234 op = TRUTH_ORIF_EXPR;
15235 break;
15237 case CPP_PLUS_PLUS:
15238 op = POSTINCREMENT_EXPR;
15239 break;
15241 case CPP_MINUS_MINUS:
15242 op = PREDECREMENT_EXPR;
15243 break;
15245 case CPP_COMMA:
15246 op = COMPOUND_EXPR;
15247 break;
15249 case CPP_DEREF_STAR:
15250 op = MEMBER_REF;
15251 break;
15253 case CPP_DEREF:
15254 op = COMPONENT_REF;
15255 break;
15257 case CPP_OPEN_PAREN:
15259 /* Consume the `('. */
15260 matching_parens parens;
15261 parens.consume_open (parser);
15262 /* Look for the matching `)'. */
15263 token = parens.require_close (parser);
15264 if (token)
15265 end_loc = token->location;
15266 op = CALL_EXPR;
15267 consumed = true;
15268 break;
15271 case CPP_OPEN_SQUARE:
15272 /* Consume the `['. */
15273 cp_lexer_consume_token (parser->lexer);
15274 /* Look for the matching `]'. */
15275 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15276 if (token)
15277 end_loc = token->location;
15278 op = ARRAY_REF;
15279 consumed = true;
15280 break;
15282 case CPP_UTF8STRING:
15283 case CPP_UTF8STRING_USERDEF:
15284 utf8 = true;
15285 /* FALLTHRU */
15286 case CPP_STRING:
15287 case CPP_WSTRING:
15288 case CPP_STRING16:
15289 case CPP_STRING32:
15290 case CPP_STRING_USERDEF:
15291 case CPP_WSTRING_USERDEF:
15292 case CPP_STRING16_USERDEF:
15293 case CPP_STRING32_USERDEF:
15295 cp_expr str;
15296 tree string_tree;
15297 int sz, len;
15299 if (cxx_dialect == cxx98)
15300 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15302 /* Consume the string. */
15303 str = cp_parser_string_literal (parser, /*translate=*/true,
15304 /*wide_ok=*/true, /*lookup_udlit=*/false);
15305 if (str == error_mark_node)
15306 return error_mark_node;
15307 else if (TREE_CODE (str) == USERDEF_LITERAL)
15309 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15310 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15311 end_loc = str.get_location ();
15313 else
15315 string_tree = str;
15316 /* Look for the suffix identifier. */
15317 token = cp_lexer_peek_token (parser->lexer);
15318 if (token->type == CPP_NAME)
15320 id = cp_parser_identifier (parser);
15321 end_loc = token->location;
15323 else if (token->type == CPP_KEYWORD)
15325 error ("unexpected keyword;"
15326 " remove space between quotes and suffix identifier");
15327 return error_mark_node;
15329 else
15331 error ("expected suffix identifier");
15332 return error_mark_node;
15335 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15336 (TREE_TYPE (TREE_TYPE (string_tree))));
15337 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15338 if (len != 0)
15340 error ("expected empty string after %<operator%> keyword");
15341 return error_mark_node;
15343 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15344 != char_type_node)
15346 error ("invalid encoding prefix in literal operator");
15347 return error_mark_node;
15349 if (id != error_mark_node)
15351 const char *name = IDENTIFIER_POINTER (id);
15352 id = cp_literal_operator_id (name);
15354 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15355 return cp_expr (id, start_loc);
15358 default:
15359 /* Anything else is an error. */
15360 break;
15363 /* If we have selected an identifier, we need to consume the
15364 operator token. */
15365 if (op != ERROR_MARK)
15367 id = ovl_op_identifier (assop, op);
15368 if (!consumed)
15369 cp_lexer_consume_token (parser->lexer);
15371 /* Otherwise, no valid operator name was present. */
15372 else
15374 cp_parser_error (parser, "expected operator");
15375 id = error_mark_node;
15378 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15379 return cp_expr (id, start_loc);
15382 /* Parse a template-declaration.
15384 template-declaration:
15385 export [opt] template < template-parameter-list > declaration
15387 If MEMBER_P is TRUE, this template-declaration occurs within a
15388 class-specifier.
15390 The grammar rule given by the standard isn't correct. What
15391 is really meant is:
15393 template-declaration:
15394 export [opt] template-parameter-list-seq
15395 decl-specifier-seq [opt] init-declarator [opt] ;
15396 export [opt] template-parameter-list-seq
15397 function-definition
15399 template-parameter-list-seq:
15400 template-parameter-list-seq [opt]
15401 template < template-parameter-list >
15403 Concept Extensions:
15405 template-parameter-list-seq:
15406 template < template-parameter-list > requires-clause [opt]
15408 requires-clause:
15409 requires logical-or-expression */
15411 static void
15412 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15414 /* Check for `export'. */
15415 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15417 /* Consume the `export' token. */
15418 cp_lexer_consume_token (parser->lexer);
15419 /* Warn that we do not support `export'. */
15420 warning (0, "keyword %<export%> not implemented, and will be ignored");
15423 cp_parser_template_declaration_after_export (parser, member_p);
15426 /* Parse a template-parameter-list.
15428 template-parameter-list:
15429 template-parameter
15430 template-parameter-list , template-parameter
15432 Returns a TREE_LIST. Each node represents a template parameter.
15433 The nodes are connected via their TREE_CHAINs. */
15435 static tree
15436 cp_parser_template_parameter_list (cp_parser* parser)
15438 tree parameter_list = NULL_TREE;
15440 begin_template_parm_list ();
15442 /* The loop below parses the template parms. We first need to know
15443 the total number of template parms to be able to compute proper
15444 canonical types of each dependent type. So after the loop, when
15445 we know the total number of template parms,
15446 end_template_parm_list computes the proper canonical types and
15447 fixes up the dependent types accordingly. */
15448 while (true)
15450 tree parameter;
15451 bool is_non_type;
15452 bool is_parameter_pack;
15453 location_t parm_loc;
15455 /* Parse the template-parameter. */
15456 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15457 parameter = cp_parser_template_parameter (parser,
15458 &is_non_type,
15459 &is_parameter_pack);
15460 /* Add it to the list. */
15461 if (parameter != error_mark_node)
15462 parameter_list = process_template_parm (parameter_list,
15463 parm_loc,
15464 parameter,
15465 is_non_type,
15466 is_parameter_pack);
15467 else
15469 tree err_parm = build_tree_list (parameter, parameter);
15470 parameter_list = chainon (parameter_list, err_parm);
15473 /* If the next token is not a `,', we're done. */
15474 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15475 break;
15476 /* Otherwise, consume the `,' token. */
15477 cp_lexer_consume_token (parser->lexer);
15480 return end_template_parm_list (parameter_list);
15483 /* Parse a introduction-list.
15485 introduction-list:
15486 introduced-parameter
15487 introduction-list , introduced-parameter
15489 introduced-parameter:
15490 ...[opt] identifier
15492 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15493 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15494 WILDCARD_DECL will also have DECL_NAME set and token location in
15495 DECL_SOURCE_LOCATION. */
15497 static tree
15498 cp_parser_introduction_list (cp_parser *parser)
15500 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15502 while (true)
15504 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15505 if (is_pack)
15506 cp_lexer_consume_token (parser->lexer);
15508 tree identifier = cp_parser_identifier (parser);
15509 if (identifier == error_mark_node)
15510 break;
15512 /* Build placeholder. */
15513 tree parm = build_nt (WILDCARD_DECL);
15514 DECL_SOURCE_LOCATION (parm)
15515 = cp_lexer_peek_token (parser->lexer)->location;
15516 DECL_NAME (parm) = identifier;
15517 WILDCARD_PACK_P (parm) = is_pack;
15518 vec_safe_push (introduction_vec, parm);
15520 /* If the next token is not a `,', we're done. */
15521 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15522 break;
15523 /* Otherwise, consume the `,' token. */
15524 cp_lexer_consume_token (parser->lexer);
15527 /* Convert the vec into a TREE_VEC. */
15528 tree introduction_list = make_tree_vec (introduction_vec->length ());
15529 unsigned int n;
15530 tree parm;
15531 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15532 TREE_VEC_ELT (introduction_list, n) = parm;
15534 release_tree_vector (introduction_vec);
15535 return introduction_list;
15538 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15539 is an abstract declarator. */
15541 static inline cp_declarator*
15542 get_id_declarator (cp_declarator *declarator)
15544 cp_declarator *d = declarator;
15545 while (d && d->kind != cdk_id)
15546 d = d->declarator;
15547 return d;
15550 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15551 is an abstract declarator. */
15553 static inline tree
15554 get_unqualified_id (cp_declarator *declarator)
15556 declarator = get_id_declarator (declarator);
15557 if (declarator)
15558 return declarator->u.id.unqualified_name;
15559 else
15560 return NULL_TREE;
15563 /* Returns true if DECL represents a constrained-parameter. */
15565 static inline bool
15566 is_constrained_parameter (tree decl)
15568 return (decl
15569 && TREE_CODE (decl) == TYPE_DECL
15570 && CONSTRAINED_PARM_CONCEPT (decl)
15571 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15574 /* Returns true if PARM declares a constrained-parameter. */
15576 static inline bool
15577 is_constrained_parameter (cp_parameter_declarator *parm)
15579 return is_constrained_parameter (parm->decl_specifiers.type);
15582 /* Check that the type parameter is only a declarator-id, and that its
15583 type is not cv-qualified. */
15585 bool
15586 cp_parser_check_constrained_type_parm (cp_parser *parser,
15587 cp_parameter_declarator *parm)
15589 if (!parm->declarator)
15590 return true;
15592 if (parm->declarator->kind != cdk_id)
15594 cp_parser_error (parser, "invalid constrained type parameter");
15595 return false;
15598 /* Don't allow cv-qualified type parameters. */
15599 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15600 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15602 cp_parser_error (parser, "cv-qualified type parameter");
15603 return false;
15606 return true;
15609 /* Finish parsing/processing a template type parameter and checking
15610 various restrictions. */
15612 static inline tree
15613 cp_parser_constrained_type_template_parm (cp_parser *parser,
15614 tree id,
15615 cp_parameter_declarator* parmdecl)
15617 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15618 return finish_template_type_parm (class_type_node, id);
15619 else
15620 return error_mark_node;
15623 static tree
15624 finish_constrained_template_template_parm (tree proto, tree id)
15626 /* FIXME: This should probably be copied, and we may need to adjust
15627 the template parameter depths. */
15628 tree saved_parms = current_template_parms;
15629 begin_template_parm_list ();
15630 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15631 end_template_parm_list ();
15633 tree parm = finish_template_template_parm (class_type_node, id);
15634 current_template_parms = saved_parms;
15636 return parm;
15639 /* Finish parsing/processing a template template parameter by borrowing
15640 the template parameter list from the prototype parameter. */
15642 static tree
15643 cp_parser_constrained_template_template_parm (cp_parser *parser,
15644 tree proto,
15645 tree id,
15646 cp_parameter_declarator *parmdecl)
15648 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15649 return error_mark_node;
15650 return finish_constrained_template_template_parm (proto, id);
15653 /* Create a new non-type template parameter from the given PARM
15654 declarator. */
15656 static tree
15657 constrained_non_type_template_parm (bool *is_non_type,
15658 cp_parameter_declarator *parm)
15660 *is_non_type = true;
15661 cp_declarator *decl = parm->declarator;
15662 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15663 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15664 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15667 /* Build a constrained template parameter based on the PARMDECL
15668 declarator. The type of PARMDECL is the constrained type, which
15669 refers to the prototype template parameter that ultimately
15670 specifies the type of the declared parameter. */
15672 static tree
15673 finish_constrained_parameter (cp_parser *parser,
15674 cp_parameter_declarator *parmdecl,
15675 bool *is_non_type,
15676 bool *is_parameter_pack)
15678 tree decl = parmdecl->decl_specifiers.type;
15679 tree id = get_unqualified_id (parmdecl->declarator);
15680 tree def = parmdecl->default_argument;
15681 tree proto = DECL_INITIAL (decl);
15683 /* A template parameter constrained by a variadic concept shall also
15684 be declared as a template parameter pack. */
15685 bool is_variadic = template_parameter_pack_p (proto);
15686 if (is_variadic && !*is_parameter_pack)
15687 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15689 /* Build the parameter. Return an error if the declarator was invalid. */
15690 tree parm;
15691 if (TREE_CODE (proto) == TYPE_DECL)
15692 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15693 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15694 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15695 parmdecl);
15696 else
15697 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15698 if (parm == error_mark_node)
15699 return error_mark_node;
15701 /* Finish the parameter decl and create a node attaching the
15702 default argument and constraint. */
15703 parm = build_tree_list (def, parm);
15704 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15706 return parm;
15709 /* Returns true if the parsed type actually represents the declaration
15710 of a type template-parameter. */
15712 static inline bool
15713 declares_constrained_type_template_parameter (tree type)
15715 return (is_constrained_parameter (type)
15716 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15720 /* Returns true if the parsed type actually represents the declaration of
15721 a template template-parameter. */
15723 static bool
15724 declares_constrained_template_template_parameter (tree type)
15726 return (is_constrained_parameter (type)
15727 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15730 /* Parse a default argument for a type template-parameter.
15731 Note that diagnostics are handled in cp_parser_template_parameter. */
15733 static tree
15734 cp_parser_default_type_template_argument (cp_parser *parser)
15736 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15738 /* Consume the `=' token. */
15739 cp_lexer_consume_token (parser->lexer);
15741 cp_token *token = cp_lexer_peek_token (parser->lexer);
15743 /* Parse the default-argument. */
15744 push_deferring_access_checks (dk_no_deferred);
15745 tree default_argument = cp_parser_type_id (parser);
15746 pop_deferring_access_checks ();
15748 if (flag_concepts && type_uses_auto (default_argument))
15750 error_at (token->location,
15751 "invalid use of %<auto%> in default template argument");
15752 return error_mark_node;
15755 return default_argument;
15758 /* Parse a default argument for a template template-parameter. */
15760 static tree
15761 cp_parser_default_template_template_argument (cp_parser *parser)
15763 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15765 bool is_template;
15767 /* Consume the `='. */
15768 cp_lexer_consume_token (parser->lexer);
15769 /* Parse the id-expression. */
15770 push_deferring_access_checks (dk_no_deferred);
15771 /* save token before parsing the id-expression, for error
15772 reporting */
15773 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15774 tree default_argument
15775 = cp_parser_id_expression (parser,
15776 /*template_keyword_p=*/false,
15777 /*check_dependency_p=*/true,
15778 /*template_p=*/&is_template,
15779 /*declarator_p=*/false,
15780 /*optional_p=*/false);
15781 if (TREE_CODE (default_argument) == TYPE_DECL)
15782 /* If the id-expression was a template-id that refers to
15783 a template-class, we already have the declaration here,
15784 so no further lookup is needed. */
15786 else
15787 /* Look up the name. */
15788 default_argument
15789 = cp_parser_lookup_name (parser, default_argument,
15790 none_type,
15791 /*is_template=*/is_template,
15792 /*is_namespace=*/false,
15793 /*check_dependency=*/true,
15794 /*ambiguous_decls=*/NULL,
15795 token->location);
15796 /* See if the default argument is valid. */
15797 default_argument = check_template_template_default_arg (default_argument);
15798 pop_deferring_access_checks ();
15799 return default_argument;
15802 /* Parse a template-parameter.
15804 template-parameter:
15805 type-parameter
15806 parameter-declaration
15808 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15809 the parameter. The TREE_PURPOSE is the default value, if any.
15810 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15811 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15812 set to true iff this parameter is a parameter pack. */
15814 static tree
15815 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15816 bool *is_parameter_pack)
15818 cp_token *token;
15819 cp_parameter_declarator *parameter_declarator;
15820 tree parm;
15822 /* Assume it is a type parameter or a template parameter. */
15823 *is_non_type = false;
15824 /* Assume it not a parameter pack. */
15825 *is_parameter_pack = false;
15826 /* Peek at the next token. */
15827 token = cp_lexer_peek_token (parser->lexer);
15828 /* If it is `template', we have a type-parameter. */
15829 if (token->keyword == RID_TEMPLATE)
15830 return cp_parser_type_parameter (parser, is_parameter_pack);
15831 /* If it is `class' or `typename' we do not know yet whether it is a
15832 type parameter or a non-type parameter. Consider:
15834 template <typename T, typename T::X X> ...
15838 template <class C, class D*> ...
15840 Here, the first parameter is a type parameter, and the second is
15841 a non-type parameter. We can tell by looking at the token after
15842 the identifier -- if it is a `,', `=', or `>' then we have a type
15843 parameter. */
15844 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15846 /* Peek at the token after `class' or `typename'. */
15847 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15848 /* If it's an ellipsis, we have a template type parameter
15849 pack. */
15850 if (token->type == CPP_ELLIPSIS)
15851 return cp_parser_type_parameter (parser, is_parameter_pack);
15852 /* If it's an identifier, skip it. */
15853 if (token->type == CPP_NAME)
15854 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15855 /* Now, see if the token looks like the end of a template
15856 parameter. */
15857 if (token->type == CPP_COMMA
15858 || token->type == CPP_EQ
15859 || token->type == CPP_GREATER)
15860 return cp_parser_type_parameter (parser, is_parameter_pack);
15863 /* Otherwise, it is a non-type parameter or a constrained parameter.
15865 [temp.param]
15867 When parsing a default template-argument for a non-type
15868 template-parameter, the first non-nested `>' is taken as the end
15869 of the template parameter-list rather than a greater-than
15870 operator. */
15871 parameter_declarator
15872 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15873 /*parenthesized_p=*/NULL);
15875 if (!parameter_declarator)
15876 return error_mark_node;
15878 /* If the parameter declaration is marked as a parameter pack, set
15879 *IS_PARAMETER_PACK to notify the caller. */
15880 if (parameter_declarator->template_parameter_pack_p)
15881 *is_parameter_pack = true;
15883 if (parameter_declarator->default_argument)
15885 /* Can happen in some cases of erroneous input (c++/34892). */
15886 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15887 /* Consume the `...' for better error recovery. */
15888 cp_lexer_consume_token (parser->lexer);
15891 // The parameter may have been constrained.
15892 if (is_constrained_parameter (parameter_declarator))
15893 return finish_constrained_parameter (parser,
15894 parameter_declarator,
15895 is_non_type,
15896 is_parameter_pack);
15898 // Now we're sure that the parameter is a non-type parameter.
15899 *is_non_type = true;
15901 parm = grokdeclarator (parameter_declarator->declarator,
15902 &parameter_declarator->decl_specifiers,
15903 TPARM, /*initialized=*/0,
15904 /*attrlist=*/NULL);
15905 if (parm == error_mark_node)
15906 return error_mark_node;
15908 return build_tree_list (parameter_declarator->default_argument, parm);
15911 /* Parse a type-parameter.
15913 type-parameter:
15914 class identifier [opt]
15915 class identifier [opt] = type-id
15916 typename identifier [opt]
15917 typename identifier [opt] = type-id
15918 template < template-parameter-list > class identifier [opt]
15919 template < template-parameter-list > class identifier [opt]
15920 = id-expression
15922 GNU Extension (variadic templates):
15924 type-parameter:
15925 class ... identifier [opt]
15926 typename ... identifier [opt]
15928 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15929 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15930 the declaration of the parameter.
15932 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15934 static tree
15935 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15937 cp_token *token;
15938 tree parameter;
15940 /* Look for a keyword to tell us what kind of parameter this is. */
15941 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15942 if (!token)
15943 return error_mark_node;
15945 switch (token->keyword)
15947 case RID_CLASS:
15948 case RID_TYPENAME:
15950 tree identifier;
15951 tree default_argument;
15953 /* If the next token is an ellipsis, we have a template
15954 argument pack. */
15955 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15957 /* Consume the `...' token. */
15958 cp_lexer_consume_token (parser->lexer);
15959 maybe_warn_variadic_templates ();
15961 *is_parameter_pack = true;
15964 /* If the next token is an identifier, then it names the
15965 parameter. */
15966 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15967 identifier = cp_parser_identifier (parser);
15968 else
15969 identifier = NULL_TREE;
15971 /* Create the parameter. */
15972 parameter = finish_template_type_parm (class_type_node, identifier);
15974 /* If the next token is an `=', we have a default argument. */
15975 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15977 default_argument
15978 = cp_parser_default_type_template_argument (parser);
15980 /* Template parameter packs cannot have default
15981 arguments. */
15982 if (*is_parameter_pack)
15984 if (identifier)
15985 error_at (token->location,
15986 "template parameter pack %qD cannot have a "
15987 "default argument", identifier);
15988 else
15989 error_at (token->location,
15990 "template parameter packs cannot have "
15991 "default arguments");
15992 default_argument = NULL_TREE;
15994 else if (check_for_bare_parameter_packs (default_argument))
15995 default_argument = error_mark_node;
15997 else
15998 default_argument = NULL_TREE;
16000 /* Create the combined representation of the parameter and the
16001 default argument. */
16002 parameter = build_tree_list (default_argument, parameter);
16004 break;
16006 case RID_TEMPLATE:
16008 tree identifier;
16009 tree default_argument;
16011 /* Look for the `<'. */
16012 cp_parser_require (parser, CPP_LESS, RT_LESS);
16013 /* Parse the template-parameter-list. */
16014 cp_parser_template_parameter_list (parser);
16015 /* Look for the `>'. */
16016 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16018 // If template requirements are present, parse them.
16019 if (flag_concepts)
16021 tree reqs = get_shorthand_constraints (current_template_parms);
16022 if (tree r = cp_parser_requires_clause_opt (parser))
16023 reqs = conjoin_constraints (reqs, normalize_expression (r));
16024 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16027 /* Look for the `class' or 'typename' keywords. */
16028 cp_parser_type_parameter_key (parser);
16029 /* If the next token is an ellipsis, we have a template
16030 argument pack. */
16031 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16033 /* Consume the `...' token. */
16034 cp_lexer_consume_token (parser->lexer);
16035 maybe_warn_variadic_templates ();
16037 *is_parameter_pack = true;
16039 /* If the next token is an `=', then there is a
16040 default-argument. If the next token is a `>', we are at
16041 the end of the parameter-list. If the next token is a `,',
16042 then we are at the end of this parameter. */
16043 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16044 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16045 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16047 identifier = cp_parser_identifier (parser);
16048 /* Treat invalid names as if the parameter were nameless. */
16049 if (identifier == error_mark_node)
16050 identifier = NULL_TREE;
16052 else
16053 identifier = NULL_TREE;
16055 /* Create the template parameter. */
16056 parameter = finish_template_template_parm (class_type_node,
16057 identifier);
16059 /* If the next token is an `=', then there is a
16060 default-argument. */
16061 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16063 default_argument
16064 = cp_parser_default_template_template_argument (parser);
16066 /* Template parameter packs cannot have default
16067 arguments. */
16068 if (*is_parameter_pack)
16070 if (identifier)
16071 error_at (token->location,
16072 "template parameter pack %qD cannot "
16073 "have a default argument",
16074 identifier);
16075 else
16076 error_at (token->location, "template parameter packs cannot "
16077 "have default arguments");
16078 default_argument = NULL_TREE;
16081 else
16082 default_argument = NULL_TREE;
16084 /* Create the combined representation of the parameter and the
16085 default argument. */
16086 parameter = build_tree_list (default_argument, parameter);
16088 break;
16090 default:
16091 gcc_unreachable ();
16092 break;
16095 return parameter;
16098 /* Parse a template-id.
16100 template-id:
16101 template-name < template-argument-list [opt] >
16103 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16104 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16105 returned. Otherwise, if the template-name names a function, or set
16106 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16107 names a class, returns a TYPE_DECL for the specialization.
16109 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16110 uninstantiated templates. */
16112 static tree
16113 cp_parser_template_id (cp_parser *parser,
16114 bool template_keyword_p,
16115 bool check_dependency_p,
16116 enum tag_types tag_type,
16117 bool is_declaration)
16119 tree templ;
16120 tree arguments;
16121 tree template_id;
16122 cp_token_position start_of_id = 0;
16123 cp_token *next_token = NULL, *next_token_2 = NULL;
16124 bool is_identifier;
16126 /* If the next token corresponds to a template-id, there is no need
16127 to reparse it. */
16128 cp_token *token = cp_lexer_peek_token (parser->lexer);
16129 if (token->type == CPP_TEMPLATE_ID)
16131 cp_lexer_consume_token (parser->lexer);
16132 return saved_checks_value (token->u.tree_check_value);
16135 /* Avoid performing name lookup if there is no possibility of
16136 finding a template-id. */
16137 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16138 || (token->type == CPP_NAME
16139 && !cp_parser_nth_token_starts_template_argument_list_p
16140 (parser, 2)))
16142 cp_parser_error (parser, "expected template-id");
16143 return error_mark_node;
16146 /* Remember where the template-id starts. */
16147 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16148 start_of_id = cp_lexer_token_position (parser->lexer, false);
16150 push_deferring_access_checks (dk_deferred);
16152 /* Parse the template-name. */
16153 is_identifier = false;
16154 templ = cp_parser_template_name (parser, template_keyword_p,
16155 check_dependency_p,
16156 is_declaration,
16157 tag_type,
16158 &is_identifier);
16159 if (templ == error_mark_node || is_identifier)
16161 pop_deferring_access_checks ();
16162 return templ;
16165 /* Since we're going to preserve any side-effects from this parse, set up a
16166 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16167 in the template arguments. */
16168 tentative_firewall firewall (parser);
16170 /* If we find the sequence `[:' after a template-name, it's probably
16171 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16172 parse correctly the argument list. */
16173 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16174 == CPP_OPEN_SQUARE)
16175 && next_token->flags & DIGRAPH
16176 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16177 == CPP_COLON)
16178 && !(next_token_2->flags & PREV_WHITE))
16180 cp_parser_parse_tentatively (parser);
16181 /* Change `:' into `::'. */
16182 next_token_2->type = CPP_SCOPE;
16183 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16184 CPP_LESS. */
16185 cp_lexer_consume_token (parser->lexer);
16187 /* Parse the arguments. */
16188 arguments = cp_parser_enclosed_template_argument_list (parser);
16189 if (!cp_parser_parse_definitely (parser))
16191 /* If we couldn't parse an argument list, then we revert our changes
16192 and return simply an error. Maybe this is not a template-id
16193 after all. */
16194 next_token_2->type = CPP_COLON;
16195 cp_parser_error (parser, "expected %<<%>");
16196 pop_deferring_access_checks ();
16197 return error_mark_node;
16199 /* Otherwise, emit an error about the invalid digraph, but continue
16200 parsing because we got our argument list. */
16201 if (permerror (next_token->location,
16202 "%<<::%> cannot begin a template-argument list"))
16204 static bool hint = false;
16205 inform (next_token->location,
16206 "%<<:%> is an alternate spelling for %<[%>."
16207 " Insert whitespace between %<<%> and %<::%>");
16208 if (!hint && !flag_permissive)
16210 inform (next_token->location, "(if you use %<-fpermissive%> "
16211 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16212 "accept your code)");
16213 hint = true;
16217 else
16219 /* Look for the `<' that starts the template-argument-list. */
16220 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16222 pop_deferring_access_checks ();
16223 return error_mark_node;
16225 /* Parse the arguments. */
16226 arguments = cp_parser_enclosed_template_argument_list (parser);
16228 if ((cxx_dialect > cxx17)
16229 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16230 && !template_keyword_p
16231 && (cp_parser_error_occurred (parser)
16232 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16234 /* This didn't go well. */
16235 if (TREE_CODE (templ) == FUNCTION_DECL)
16237 /* C++2A says that "function-name < a;" is now ill-formed. */
16238 if (cp_parser_error_occurred (parser))
16240 error_at (token->location, "invalid template-argument-list");
16241 inform (token->location, "function name as the left hand "
16242 "operand of %<<%> is ill-formed in C++2a; wrap the "
16243 "function name in %<()%>");
16245 else
16246 /* We expect "f<targs>" to be followed by "(args)". */
16247 error_at (cp_lexer_peek_token (parser->lexer)->location,
16248 "expected %<(%> after template-argument-list");
16249 if (start_of_id)
16250 /* Purge all subsequent tokens. */
16251 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16253 else
16254 cp_parser_simulate_error (parser);
16255 pop_deferring_access_checks ();
16256 return error_mark_node;
16260 /* Set the location to be of the form:
16261 template-name < template-argument-list [opt] >
16262 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16263 with caret == start at the start of the template-name,
16264 ranging until the closing '>'. */
16265 location_t finish_loc
16266 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16267 location_t combined_loc
16268 = make_location (token->location, token->location, finish_loc);
16270 /* Check for concepts autos where they don't belong. We could
16271 identify types in some cases of idnetifier TEMPL, looking ahead
16272 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16273 types. We reject them in functions, but if what we have is an
16274 identifier, even with none_type we can't conclude it's NOT a
16275 type, we have to wait for template substitution. */
16276 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16277 template_id = error_mark_node;
16278 /* Build a representation of the specialization. */
16279 else if (identifier_p (templ))
16280 template_id = build_min_nt_loc (combined_loc,
16281 TEMPLATE_ID_EXPR,
16282 templ, arguments);
16283 else if (DECL_TYPE_TEMPLATE_P (templ)
16284 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16286 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16287 template (rather than some instantiation thereof) only if
16288 is not nested within some other construct. For example, in
16289 "template <typename T> void f(T) { A<T>::", A<T> is just an
16290 instantiation of A. */
16291 bool entering_scope
16292 = (template_parm_scope_p ()
16293 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16294 template_id
16295 = finish_template_type (templ, arguments, entering_scope);
16297 /* A template-like identifier may be a partial concept id. */
16298 else if (flag_concepts
16299 && (template_id = (cp_parser_maybe_partial_concept_id
16300 (parser, templ, arguments))))
16301 return template_id;
16302 else if (variable_template_p (templ))
16304 template_id = lookup_template_variable (templ, arguments);
16305 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16306 SET_EXPR_LOCATION (template_id, combined_loc);
16308 else
16310 /* If it's not a class-template or a template-template, it should be
16311 a function-template. */
16312 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16313 || TREE_CODE (templ) == OVERLOAD
16314 || TREE_CODE (templ) == FUNCTION_DECL
16315 || BASELINK_P (templ)));
16317 template_id = lookup_template_function (templ, arguments);
16318 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16319 SET_EXPR_LOCATION (template_id, combined_loc);
16322 /* If parsing tentatively, replace the sequence of tokens that makes
16323 up the template-id with a CPP_TEMPLATE_ID token. That way,
16324 should we re-parse the token stream, we will not have to repeat
16325 the effort required to do the parse, nor will we issue duplicate
16326 error messages about problems during instantiation of the
16327 template. */
16328 if (start_of_id
16329 /* Don't do this if we had a parse error in a declarator; re-parsing
16330 might succeed if a name changes meaning (60361). */
16331 && !(cp_parser_error_occurred (parser)
16332 && cp_parser_parsing_tentatively (parser)
16333 && parser->in_declarator_p))
16335 /* Reset the contents of the START_OF_ID token. */
16336 token->type = CPP_TEMPLATE_ID;
16337 token->location = combined_loc;
16339 /* Retrieve any deferred checks. Do not pop this access checks yet
16340 so the memory will not be reclaimed during token replacing below. */
16341 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16342 token->u.tree_check_value->value = template_id;
16343 token->u.tree_check_value->checks = get_deferred_access_checks ();
16344 token->keyword = RID_MAX;
16346 /* Purge all subsequent tokens. */
16347 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16349 /* ??? Can we actually assume that, if template_id ==
16350 error_mark_node, we will have issued a diagnostic to the
16351 user, as opposed to simply marking the tentative parse as
16352 failed? */
16353 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16354 error_at (token->location, "parse error in template argument list");
16357 pop_to_parent_deferring_access_checks ();
16358 return template_id;
16361 /* Parse a template-name.
16363 template-name:
16364 identifier
16366 The standard should actually say:
16368 template-name:
16369 identifier
16370 operator-function-id
16372 A defect report has been filed about this issue.
16374 A conversion-function-id cannot be a template name because they cannot
16375 be part of a template-id. In fact, looking at this code:
16377 a.operator K<int>()
16379 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16380 It is impossible to call a templated conversion-function-id with an
16381 explicit argument list, since the only allowed template parameter is
16382 the type to which it is converting.
16384 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16385 `template' keyword, in a construction like:
16387 T::template f<3>()
16389 In that case `f' is taken to be a template-name, even though there
16390 is no way of knowing for sure.
16392 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16393 name refers to a set of overloaded functions, at least one of which
16394 is a template, or an IDENTIFIER_NODE with the name of the template,
16395 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16396 names are looked up inside uninstantiated templates. */
16398 static tree
16399 cp_parser_template_name (cp_parser* parser,
16400 bool template_keyword_p,
16401 bool check_dependency_p,
16402 bool is_declaration,
16403 enum tag_types tag_type,
16404 bool *is_identifier)
16406 tree identifier;
16407 tree decl;
16408 cp_token *token = cp_lexer_peek_token (parser->lexer);
16410 /* If the next token is `operator', then we have either an
16411 operator-function-id or a conversion-function-id. */
16412 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16414 /* We don't know whether we're looking at an
16415 operator-function-id or a conversion-function-id. */
16416 cp_parser_parse_tentatively (parser);
16417 /* Try an operator-function-id. */
16418 identifier = cp_parser_operator_function_id (parser);
16419 /* If that didn't work, try a conversion-function-id. */
16420 if (!cp_parser_parse_definitely (parser))
16422 cp_parser_error (parser, "expected template-name");
16423 return error_mark_node;
16426 /* Look for the identifier. */
16427 else
16428 identifier = cp_parser_identifier (parser);
16430 /* If we didn't find an identifier, we don't have a template-id. */
16431 if (identifier == error_mark_node)
16432 return error_mark_node;
16434 /* If the name immediately followed the `template' keyword, then it
16435 is a template-name. However, if the next token is not `<', then
16436 we do not treat it as a template-name, since it is not being used
16437 as part of a template-id. This enables us to handle constructs
16438 like:
16440 template <typename T> struct S { S(); };
16441 template <typename T> S<T>::S();
16443 correctly. We would treat `S' as a template -- if it were `S<T>'
16444 -- but we do not if there is no `<'. */
16446 if (processing_template_decl
16447 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16449 /* In a declaration, in a dependent context, we pretend that the
16450 "template" keyword was present in order to improve error
16451 recovery. For example, given:
16453 template <typename T> void f(T::X<int>);
16455 we want to treat "X<int>" as a template-id. */
16456 if (is_declaration
16457 && !template_keyword_p
16458 && parser->scope && TYPE_P (parser->scope)
16459 && check_dependency_p
16460 && dependent_scope_p (parser->scope)
16461 /* Do not do this for dtors (or ctors), since they never
16462 need the template keyword before their name. */
16463 && !constructor_name_p (identifier, parser->scope))
16465 cp_token_position start = 0;
16467 /* Explain what went wrong. */
16468 error_at (token->location, "non-template %qD used as template",
16469 identifier);
16470 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16471 parser->scope, identifier);
16472 /* If parsing tentatively, find the location of the "<" token. */
16473 if (cp_parser_simulate_error (parser))
16474 start = cp_lexer_token_position (parser->lexer, true);
16475 /* Parse the template arguments so that we can issue error
16476 messages about them. */
16477 cp_lexer_consume_token (parser->lexer);
16478 cp_parser_enclosed_template_argument_list (parser);
16479 /* Skip tokens until we find a good place from which to
16480 continue parsing. */
16481 cp_parser_skip_to_closing_parenthesis (parser,
16482 /*recovering=*/true,
16483 /*or_comma=*/true,
16484 /*consume_paren=*/false);
16485 /* If parsing tentatively, permanently remove the
16486 template argument list. That will prevent duplicate
16487 error messages from being issued about the missing
16488 "template" keyword. */
16489 if (start)
16490 cp_lexer_purge_tokens_after (parser->lexer, start);
16491 if (is_identifier)
16492 *is_identifier = true;
16493 parser->context->object_type = NULL_TREE;
16494 return identifier;
16497 /* If the "template" keyword is present, then there is generally
16498 no point in doing name-lookup, so we just return IDENTIFIER.
16499 But, if the qualifying scope is non-dependent then we can
16500 (and must) do name-lookup normally. */
16501 if (template_keyword_p)
16503 tree scope = (parser->scope ? parser->scope
16504 : parser->context->object_type);
16505 if (scope && TYPE_P (scope)
16506 && (!CLASS_TYPE_P (scope)
16507 || (check_dependency_p && dependent_type_p (scope))))
16509 /* We're optimizing away the call to cp_parser_lookup_name, but
16510 we still need to do this. */
16511 parser->context->object_type = NULL_TREE;
16512 return identifier;
16517 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16518 const bool scoped_p = ((parser->scope ? parser->scope
16519 : parser->context->object_type) != NULL_TREE);
16521 /* Look up the name. */
16522 decl = cp_parser_lookup_name (parser, identifier,
16523 tag_type,
16524 /*is_template=*/true,
16525 /*is_namespace=*/false,
16526 check_dependency_p,
16527 /*ambiguous_decls=*/NULL,
16528 token->location);
16530 decl = strip_using_decl (decl);
16532 /* If DECL is a template, then the name was a template-name. */
16533 if (TREE_CODE (decl) == TEMPLATE_DECL)
16535 if (TREE_DEPRECATED (decl)
16536 && deprecated_state != DEPRECATED_SUPPRESS)
16537 warn_deprecated_use (decl, NULL_TREE);
16539 else
16541 /* The standard does not explicitly indicate whether a name that
16542 names a set of overloaded declarations, some of which are
16543 templates, is a template-name. However, such a name should
16544 be a template-name; otherwise, there is no way to form a
16545 template-id for the overloaded templates. */
16546 bool found = false;
16548 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16549 !found && iter; ++iter)
16550 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16551 found = true;
16553 if (!found
16554 && (cxx_dialect > cxx17)
16555 && !scoped_p
16556 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16558 /* [temp.names] says "A name is also considered to refer to a template
16559 if it is an unqualified-id followed by a < and name lookup finds
16560 either one or more functions or finds nothing." */
16562 /* The "more functions" case. Just use the OVERLOAD as normally.
16563 We don't use is_overloaded_fn here to avoid considering
16564 BASELINKs. */
16565 if (TREE_CODE (decl) == OVERLOAD
16566 /* Name lookup found one function. */
16567 || TREE_CODE (decl) == FUNCTION_DECL)
16568 found = true;
16569 /* Name lookup found nothing. */
16570 else if (decl == error_mark_node)
16571 return identifier;
16574 if (!found)
16576 /* The name does not name a template. */
16577 cp_parser_error (parser, "expected template-name");
16578 return error_mark_node;
16582 return decl;
16585 /* Parse a template-argument-list.
16587 template-argument-list:
16588 template-argument ... [opt]
16589 template-argument-list , template-argument ... [opt]
16591 Returns a TREE_VEC containing the arguments. */
16593 static tree
16594 cp_parser_template_argument_list (cp_parser* parser)
16596 tree fixed_args[10];
16597 unsigned n_args = 0;
16598 unsigned alloced = 10;
16599 tree *arg_ary = fixed_args;
16600 tree vec;
16601 bool saved_in_template_argument_list_p;
16602 bool saved_ice_p;
16603 bool saved_non_ice_p;
16605 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16606 parser->in_template_argument_list_p = true;
16607 /* Even if the template-id appears in an integral
16608 constant-expression, the contents of the argument list do
16609 not. */
16610 saved_ice_p = parser->integral_constant_expression_p;
16611 parser->integral_constant_expression_p = false;
16612 saved_non_ice_p = parser->non_integral_constant_expression_p;
16613 parser->non_integral_constant_expression_p = false;
16615 /* Parse the arguments. */
16618 tree argument;
16620 if (n_args)
16621 /* Consume the comma. */
16622 cp_lexer_consume_token (parser->lexer);
16624 /* Parse the template-argument. */
16625 argument = cp_parser_template_argument (parser);
16627 /* If the next token is an ellipsis, we're expanding a template
16628 argument pack. */
16629 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16631 if (argument == error_mark_node)
16633 cp_token *token = cp_lexer_peek_token (parser->lexer);
16634 error_at (token->location,
16635 "expected parameter pack before %<...%>");
16637 /* Consume the `...' token. */
16638 cp_lexer_consume_token (parser->lexer);
16640 /* Make the argument into a TYPE_PACK_EXPANSION or
16641 EXPR_PACK_EXPANSION. */
16642 argument = make_pack_expansion (argument);
16645 if (n_args == alloced)
16647 alloced *= 2;
16649 if (arg_ary == fixed_args)
16651 arg_ary = XNEWVEC (tree, alloced);
16652 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16654 else
16655 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16657 arg_ary[n_args++] = argument;
16659 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16661 vec = make_tree_vec (n_args);
16663 while (n_args--)
16664 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16666 if (arg_ary != fixed_args)
16667 free (arg_ary);
16668 parser->non_integral_constant_expression_p = saved_non_ice_p;
16669 parser->integral_constant_expression_p = saved_ice_p;
16670 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16671 if (CHECKING_P)
16672 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16673 return vec;
16676 /* Parse a template-argument.
16678 template-argument:
16679 assignment-expression
16680 type-id
16681 id-expression
16683 The representation is that of an assignment-expression, type-id, or
16684 id-expression -- except that the qualified id-expression is
16685 evaluated, so that the value returned is either a DECL or an
16686 OVERLOAD.
16688 Although the standard says "assignment-expression", it forbids
16689 throw-expressions or assignments in the template argument.
16690 Therefore, we use "conditional-expression" instead. */
16692 static tree
16693 cp_parser_template_argument (cp_parser* parser)
16695 tree argument;
16696 bool template_p;
16697 bool address_p;
16698 bool maybe_type_id = false;
16699 cp_token *token = NULL, *argument_start_token = NULL;
16700 location_t loc = 0;
16701 cp_id_kind idk;
16703 /* There's really no way to know what we're looking at, so we just
16704 try each alternative in order.
16706 [temp.arg]
16708 In a template-argument, an ambiguity between a type-id and an
16709 expression is resolved to a type-id, regardless of the form of
16710 the corresponding template-parameter.
16712 Therefore, we try a type-id first. */
16713 cp_parser_parse_tentatively (parser);
16714 argument = cp_parser_template_type_arg (parser);
16715 /* If there was no error parsing the type-id but the next token is a
16716 '>>', our behavior depends on which dialect of C++ we're
16717 parsing. In C++98, we probably found a typo for '> >'. But there
16718 are type-id which are also valid expressions. For instance:
16720 struct X { int operator >> (int); };
16721 template <int V> struct Foo {};
16722 Foo<X () >> 5> r;
16724 Here 'X()' is a valid type-id of a function type, but the user just
16725 wanted to write the expression "X() >> 5". Thus, we remember that we
16726 found a valid type-id, but we still try to parse the argument as an
16727 expression to see what happens.
16729 In C++0x, the '>>' will be considered two separate '>'
16730 tokens. */
16731 if (!cp_parser_error_occurred (parser)
16732 && cxx_dialect == cxx98
16733 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16735 maybe_type_id = true;
16736 cp_parser_abort_tentative_parse (parser);
16738 else
16740 /* If the next token isn't a `,' or a `>', then this argument wasn't
16741 really finished. This means that the argument is not a valid
16742 type-id. */
16743 if (!cp_parser_next_token_ends_template_argument_p (parser))
16744 cp_parser_error (parser, "expected template-argument");
16745 /* If that worked, we're done. */
16746 if (cp_parser_parse_definitely (parser))
16747 return argument;
16749 /* We're still not sure what the argument will be. */
16750 cp_parser_parse_tentatively (parser);
16751 /* Try a template. */
16752 argument_start_token = cp_lexer_peek_token (parser->lexer);
16753 argument = cp_parser_id_expression (parser,
16754 /*template_keyword_p=*/false,
16755 /*check_dependency_p=*/true,
16756 &template_p,
16757 /*declarator_p=*/false,
16758 /*optional_p=*/false);
16759 /* If the next token isn't a `,' or a `>', then this argument wasn't
16760 really finished. */
16761 if (!cp_parser_next_token_ends_template_argument_p (parser))
16762 cp_parser_error (parser, "expected template-argument");
16763 if (!cp_parser_error_occurred (parser))
16765 /* Figure out what is being referred to. If the id-expression
16766 was for a class template specialization, then we will have a
16767 TYPE_DECL at this point. There is no need to do name lookup
16768 at this point in that case. */
16769 if (TREE_CODE (argument) != TYPE_DECL)
16770 argument = cp_parser_lookup_name (parser, argument,
16771 none_type,
16772 /*is_template=*/template_p,
16773 /*is_namespace=*/false,
16774 /*check_dependency=*/true,
16775 /*ambiguous_decls=*/NULL,
16776 argument_start_token->location);
16777 /* Handle a constrained-type-specifier for a non-type template
16778 parameter. */
16779 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16780 argument = decl;
16781 else if (TREE_CODE (argument) != TEMPLATE_DECL
16782 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16783 cp_parser_error (parser, "expected template-name");
16785 if (cp_parser_parse_definitely (parser))
16787 if (TREE_DEPRECATED (argument))
16788 warn_deprecated_use (argument, NULL_TREE);
16789 return argument;
16791 /* It must be a non-type argument. In C++17 any constant-expression is
16792 allowed. */
16793 if (cxx_dialect > cxx14)
16794 goto general_expr;
16796 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16798 -- an integral constant-expression of integral or enumeration
16799 type; or
16801 -- the name of a non-type template-parameter; or
16803 -- the name of an object or function with external linkage...
16805 -- the address of an object or function with external linkage...
16807 -- a pointer to member... */
16808 /* Look for a non-type template parameter. */
16809 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16811 cp_parser_parse_tentatively (parser);
16812 argument = cp_parser_primary_expression (parser,
16813 /*address_p=*/false,
16814 /*cast_p=*/false,
16815 /*template_arg_p=*/true,
16816 &idk);
16817 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16818 || !cp_parser_next_token_ends_template_argument_p (parser))
16819 cp_parser_simulate_error (parser);
16820 if (cp_parser_parse_definitely (parser))
16821 return argument;
16824 /* If the next token is "&", the argument must be the address of an
16825 object or function with external linkage. */
16826 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16827 if (address_p)
16829 loc = cp_lexer_peek_token (parser->lexer)->location;
16830 cp_lexer_consume_token (parser->lexer);
16832 /* See if we might have an id-expression. */
16833 token = cp_lexer_peek_token (parser->lexer);
16834 if (token->type == CPP_NAME
16835 || token->keyword == RID_OPERATOR
16836 || token->type == CPP_SCOPE
16837 || token->type == CPP_TEMPLATE_ID
16838 || token->type == CPP_NESTED_NAME_SPECIFIER)
16840 cp_parser_parse_tentatively (parser);
16841 argument = cp_parser_primary_expression (parser,
16842 address_p,
16843 /*cast_p=*/false,
16844 /*template_arg_p=*/true,
16845 &idk);
16846 if (cp_parser_error_occurred (parser)
16847 || !cp_parser_next_token_ends_template_argument_p (parser))
16848 cp_parser_abort_tentative_parse (parser);
16849 else
16851 tree probe;
16853 if (INDIRECT_REF_P (argument))
16855 /* Strip the dereference temporarily. */
16856 gcc_assert (REFERENCE_REF_P (argument));
16857 argument = TREE_OPERAND (argument, 0);
16860 /* If we're in a template, we represent a qualified-id referring
16861 to a static data member as a SCOPE_REF even if the scope isn't
16862 dependent so that we can check access control later. */
16863 probe = argument;
16864 if (TREE_CODE (probe) == SCOPE_REF)
16865 probe = TREE_OPERAND (probe, 1);
16866 if (VAR_P (probe))
16868 /* A variable without external linkage might still be a
16869 valid constant-expression, so no error is issued here
16870 if the external-linkage check fails. */
16871 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16872 cp_parser_simulate_error (parser);
16874 else if (is_overloaded_fn (argument))
16875 /* All overloaded functions are allowed; if the external
16876 linkage test does not pass, an error will be issued
16877 later. */
16879 else if (address_p
16880 && (TREE_CODE (argument) == OFFSET_REF
16881 || TREE_CODE (argument) == SCOPE_REF))
16882 /* A pointer-to-member. */
16884 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16886 else
16887 cp_parser_simulate_error (parser);
16889 if (cp_parser_parse_definitely (parser))
16891 if (address_p)
16892 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16893 tf_warning_or_error);
16894 else
16895 argument = convert_from_reference (argument);
16896 return argument;
16900 /* If the argument started with "&", there are no other valid
16901 alternatives at this point. */
16902 if (address_p)
16904 cp_parser_error (parser, "invalid non-type template argument");
16905 return error_mark_node;
16908 general_expr:
16909 /* If the argument wasn't successfully parsed as a type-id followed
16910 by '>>', the argument can only be a constant expression now.
16911 Otherwise, we try parsing the constant-expression tentatively,
16912 because the argument could really be a type-id. */
16913 if (maybe_type_id)
16914 cp_parser_parse_tentatively (parser);
16916 if (cxx_dialect <= cxx14)
16917 argument = cp_parser_constant_expression (parser);
16918 else
16920 /* With C++17 generalized non-type template arguments we need to handle
16921 lvalue constant expressions, too. */
16922 argument = cp_parser_assignment_expression (parser);
16923 require_potential_constant_expression (argument);
16926 if (!maybe_type_id)
16927 return argument;
16928 if (!cp_parser_next_token_ends_template_argument_p (parser))
16929 cp_parser_error (parser, "expected template-argument");
16930 if (cp_parser_parse_definitely (parser))
16931 return argument;
16932 /* We did our best to parse the argument as a non type-id, but that
16933 was the only alternative that matched (albeit with a '>' after
16934 it). We can assume it's just a typo from the user, and a
16935 diagnostic will then be issued. */
16936 return cp_parser_template_type_arg (parser);
16939 /* Parse an explicit-instantiation.
16941 explicit-instantiation:
16942 template declaration
16944 Although the standard says `declaration', what it really means is:
16946 explicit-instantiation:
16947 template decl-specifier-seq [opt] declarator [opt] ;
16949 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16950 supposed to be allowed. A defect report has been filed about this
16951 issue.
16953 GNU Extension:
16955 explicit-instantiation:
16956 storage-class-specifier template
16957 decl-specifier-seq [opt] declarator [opt] ;
16958 function-specifier template
16959 decl-specifier-seq [opt] declarator [opt] ; */
16961 static void
16962 cp_parser_explicit_instantiation (cp_parser* parser)
16964 int declares_class_or_enum;
16965 cp_decl_specifier_seq decl_specifiers;
16966 tree extension_specifier = NULL_TREE;
16968 timevar_push (TV_TEMPLATE_INST);
16970 /* Look for an (optional) storage-class-specifier or
16971 function-specifier. */
16972 if (cp_parser_allow_gnu_extensions_p (parser))
16974 extension_specifier
16975 = cp_parser_storage_class_specifier_opt (parser);
16976 if (!extension_specifier)
16977 extension_specifier
16978 = cp_parser_function_specifier_opt (parser,
16979 /*decl_specs=*/NULL);
16982 /* Look for the `template' keyword. */
16983 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16984 /* Let the front end know that we are processing an explicit
16985 instantiation. */
16986 begin_explicit_instantiation ();
16987 /* [temp.explicit] says that we are supposed to ignore access
16988 control while processing explicit instantiation directives. */
16989 push_deferring_access_checks (dk_no_check);
16990 /* Parse a decl-specifier-seq. */
16991 cp_parser_decl_specifier_seq (parser,
16992 CP_PARSER_FLAGS_OPTIONAL,
16993 &decl_specifiers,
16994 &declares_class_or_enum);
16995 /* If there was exactly one decl-specifier, and it declared a class,
16996 and there's no declarator, then we have an explicit type
16997 instantiation. */
16998 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
17000 tree type;
17002 type = check_tag_decl (&decl_specifiers,
17003 /*explicit_type_instantiation_p=*/true);
17004 /* Turn access control back on for names used during
17005 template instantiation. */
17006 pop_deferring_access_checks ();
17007 if (type)
17008 do_type_instantiation (type, extension_specifier,
17009 /*complain=*/tf_error);
17011 else
17013 cp_declarator *declarator;
17014 tree decl;
17016 /* Parse the declarator. */
17017 declarator
17018 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17019 /*ctor_dtor_or_conv_p=*/NULL,
17020 /*parenthesized_p=*/NULL,
17021 /*member_p=*/false,
17022 /*friend_p=*/false);
17023 if (declares_class_or_enum & 2)
17024 cp_parser_check_for_definition_in_return_type (declarator,
17025 decl_specifiers.type,
17026 decl_specifiers.locations[ds_type_spec]);
17027 if (declarator != cp_error_declarator)
17029 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17030 permerror (decl_specifiers.locations[ds_inline],
17031 "explicit instantiation shall not use"
17032 " %<inline%> specifier");
17033 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17034 permerror (decl_specifiers.locations[ds_constexpr],
17035 "explicit instantiation shall not use"
17036 " %<constexpr%> specifier");
17038 decl = grokdeclarator (declarator, &decl_specifiers,
17039 NORMAL, 0, &decl_specifiers.attributes);
17040 /* Turn access control back on for names used during
17041 template instantiation. */
17042 pop_deferring_access_checks ();
17043 /* Do the explicit instantiation. */
17044 do_decl_instantiation (decl, extension_specifier);
17046 else
17048 pop_deferring_access_checks ();
17049 /* Skip the body of the explicit instantiation. */
17050 cp_parser_skip_to_end_of_statement (parser);
17053 /* We're done with the instantiation. */
17054 end_explicit_instantiation ();
17056 cp_parser_consume_semicolon_at_end_of_statement (parser);
17058 timevar_pop (TV_TEMPLATE_INST);
17061 /* Parse an explicit-specialization.
17063 explicit-specialization:
17064 template < > declaration
17066 Although the standard says `declaration', what it really means is:
17068 explicit-specialization:
17069 template <> decl-specifier [opt] init-declarator [opt] ;
17070 template <> function-definition
17071 template <> explicit-specialization
17072 template <> template-declaration */
17074 static void
17075 cp_parser_explicit_specialization (cp_parser* parser)
17077 bool need_lang_pop;
17078 cp_token *token = cp_lexer_peek_token (parser->lexer);
17080 /* Look for the `template' keyword. */
17081 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17082 /* Look for the `<'. */
17083 cp_parser_require (parser, CPP_LESS, RT_LESS);
17084 /* Look for the `>'. */
17085 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17086 /* We have processed another parameter list. */
17087 ++parser->num_template_parameter_lists;
17088 /* [temp]
17090 A template ... explicit specialization ... shall not have C
17091 linkage. */
17092 if (current_lang_name == lang_name_c)
17094 error_at (token->location, "template specialization with C linkage");
17095 maybe_show_extern_c_location ();
17096 /* Give it C++ linkage to avoid confusing other parts of the
17097 front end. */
17098 push_lang_context (lang_name_cplusplus);
17099 need_lang_pop = true;
17101 else
17102 need_lang_pop = false;
17103 /* Let the front end know that we are beginning a specialization. */
17104 if (!begin_specialization ())
17106 end_specialization ();
17107 return;
17110 /* If the next keyword is `template', we need to figure out whether
17111 or not we're looking a template-declaration. */
17112 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17114 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17115 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17116 cp_parser_template_declaration_after_export (parser,
17117 /*member_p=*/false);
17118 else
17119 cp_parser_explicit_specialization (parser);
17121 else
17122 /* Parse the dependent declaration. */
17123 cp_parser_single_declaration (parser,
17124 /*checks=*/NULL,
17125 /*member_p=*/false,
17126 /*explicit_specialization_p=*/true,
17127 /*friend_p=*/NULL);
17128 /* We're done with the specialization. */
17129 end_specialization ();
17130 /* For the erroneous case of a template with C linkage, we pushed an
17131 implicit C++ linkage scope; exit that scope now. */
17132 if (need_lang_pop)
17133 pop_lang_context ();
17134 /* We're done with this parameter list. */
17135 --parser->num_template_parameter_lists;
17138 /* Parse a type-specifier.
17140 type-specifier:
17141 simple-type-specifier
17142 class-specifier
17143 enum-specifier
17144 elaborated-type-specifier
17145 cv-qualifier
17147 GNU Extension:
17149 type-specifier:
17150 __complex__
17152 Returns a representation of the type-specifier. For a
17153 class-specifier, enum-specifier, or elaborated-type-specifier, a
17154 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17156 The parser flags FLAGS is used to control type-specifier parsing.
17158 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17159 in a decl-specifier-seq.
17161 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17162 class-specifier, enum-specifier, or elaborated-type-specifier, then
17163 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17164 if a type is declared; 2 if it is defined. Otherwise, it is set to
17165 zero.
17167 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17168 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17169 is set to FALSE. */
17171 static tree
17172 cp_parser_type_specifier (cp_parser* parser,
17173 cp_parser_flags flags,
17174 cp_decl_specifier_seq *decl_specs,
17175 bool is_declaration,
17176 int* declares_class_or_enum,
17177 bool* is_cv_qualifier)
17179 tree type_spec = NULL_TREE;
17180 cp_token *token;
17181 enum rid keyword;
17182 cp_decl_spec ds = ds_last;
17184 /* Assume this type-specifier does not declare a new type. */
17185 if (declares_class_or_enum)
17186 *declares_class_or_enum = 0;
17187 /* And that it does not specify a cv-qualifier. */
17188 if (is_cv_qualifier)
17189 *is_cv_qualifier = false;
17190 /* Peek at the next token. */
17191 token = cp_lexer_peek_token (parser->lexer);
17193 /* If we're looking at a keyword, we can use that to guide the
17194 production we choose. */
17195 keyword = token->keyword;
17196 switch (keyword)
17198 case RID_ENUM:
17199 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17200 goto elaborated_type_specifier;
17202 /* Look for the enum-specifier. */
17203 type_spec = cp_parser_enum_specifier (parser);
17204 /* If that worked, we're done. */
17205 if (type_spec)
17207 if (declares_class_or_enum)
17208 *declares_class_or_enum = 2;
17209 if (decl_specs)
17210 cp_parser_set_decl_spec_type (decl_specs,
17211 type_spec,
17212 token,
17213 /*type_definition_p=*/true);
17214 return type_spec;
17216 else
17217 goto elaborated_type_specifier;
17219 /* Any of these indicate either a class-specifier, or an
17220 elaborated-type-specifier. */
17221 case RID_CLASS:
17222 case RID_STRUCT:
17223 case RID_UNION:
17224 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17225 goto elaborated_type_specifier;
17227 /* Parse tentatively so that we can back up if we don't find a
17228 class-specifier. */
17229 cp_parser_parse_tentatively (parser);
17230 /* Look for the class-specifier. */
17231 type_spec = cp_parser_class_specifier (parser);
17232 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17233 /* If that worked, we're done. */
17234 if (cp_parser_parse_definitely (parser))
17236 if (declares_class_or_enum)
17237 *declares_class_or_enum = 2;
17238 if (decl_specs)
17239 cp_parser_set_decl_spec_type (decl_specs,
17240 type_spec,
17241 token,
17242 /*type_definition_p=*/true);
17243 return type_spec;
17246 /* Fall through. */
17247 elaborated_type_specifier:
17248 /* We're declaring (not defining) a class or enum. */
17249 if (declares_class_or_enum)
17250 *declares_class_or_enum = 1;
17252 /* Fall through. */
17253 case RID_TYPENAME:
17254 /* Look for an elaborated-type-specifier. */
17255 type_spec
17256 = (cp_parser_elaborated_type_specifier
17257 (parser,
17258 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17259 is_declaration));
17260 if (decl_specs)
17261 cp_parser_set_decl_spec_type (decl_specs,
17262 type_spec,
17263 token,
17264 /*type_definition_p=*/false);
17265 return type_spec;
17267 case RID_CONST:
17268 ds = ds_const;
17269 if (is_cv_qualifier)
17270 *is_cv_qualifier = true;
17271 break;
17273 case RID_VOLATILE:
17274 ds = ds_volatile;
17275 if (is_cv_qualifier)
17276 *is_cv_qualifier = true;
17277 break;
17279 case RID_RESTRICT:
17280 ds = ds_restrict;
17281 if (is_cv_qualifier)
17282 *is_cv_qualifier = true;
17283 break;
17285 case RID_COMPLEX:
17286 /* The `__complex__' keyword is a GNU extension. */
17287 ds = ds_complex;
17288 break;
17290 default:
17291 break;
17294 /* Handle simple keywords. */
17295 if (ds != ds_last)
17297 if (decl_specs)
17299 set_and_check_decl_spec_loc (decl_specs, ds, token);
17300 decl_specs->any_specifiers_p = true;
17302 return cp_lexer_consume_token (parser->lexer)->u.value;
17305 /* If we do not already have a type-specifier, assume we are looking
17306 at a simple-type-specifier. */
17307 type_spec = cp_parser_simple_type_specifier (parser,
17308 decl_specs,
17309 flags);
17311 /* If we didn't find a type-specifier, and a type-specifier was not
17312 optional in this context, issue an error message. */
17313 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17315 cp_parser_error (parser, "expected type specifier");
17316 return error_mark_node;
17319 return type_spec;
17322 /* Parse a simple-type-specifier.
17324 simple-type-specifier:
17325 :: [opt] nested-name-specifier [opt] type-name
17326 :: [opt] nested-name-specifier template template-id
17327 char
17328 wchar_t
17329 bool
17330 short
17332 long
17333 signed
17334 unsigned
17335 float
17336 double
17337 void
17339 C++11 Extension:
17341 simple-type-specifier:
17342 auto
17343 decltype ( expression )
17344 char16_t
17345 char32_t
17346 __underlying_type ( type-id )
17348 C++17 extension:
17350 nested-name-specifier(opt) template-name
17352 GNU Extension:
17354 simple-type-specifier:
17355 __int128
17356 __typeof__ unary-expression
17357 __typeof__ ( type-id )
17358 __typeof__ ( type-id ) { initializer-list , [opt] }
17360 Concepts Extension:
17362 simple-type-specifier:
17363 constrained-type-specifier
17365 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17366 appropriately updated. */
17368 static tree
17369 cp_parser_simple_type_specifier (cp_parser* parser,
17370 cp_decl_specifier_seq *decl_specs,
17371 cp_parser_flags flags)
17373 tree type = NULL_TREE;
17374 cp_token *token;
17375 int idx;
17377 /* Peek at the next token. */
17378 token = cp_lexer_peek_token (parser->lexer);
17380 /* If we're looking at a keyword, things are easy. */
17381 switch (token->keyword)
17383 case RID_CHAR:
17384 if (decl_specs)
17385 decl_specs->explicit_char_p = true;
17386 type = char_type_node;
17387 break;
17388 case RID_CHAR16:
17389 type = char16_type_node;
17390 break;
17391 case RID_CHAR32:
17392 type = char32_type_node;
17393 break;
17394 case RID_WCHAR:
17395 type = wchar_type_node;
17396 break;
17397 case RID_BOOL:
17398 type = boolean_type_node;
17399 break;
17400 case RID_SHORT:
17401 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17402 type = short_integer_type_node;
17403 break;
17404 case RID_INT:
17405 if (decl_specs)
17406 decl_specs->explicit_int_p = true;
17407 type = integer_type_node;
17408 break;
17409 case RID_INT_N_0:
17410 case RID_INT_N_1:
17411 case RID_INT_N_2:
17412 case RID_INT_N_3:
17413 idx = token->keyword - RID_INT_N_0;
17414 if (! int_n_enabled_p [idx])
17415 break;
17416 if (decl_specs)
17418 decl_specs->explicit_intN_p = true;
17419 decl_specs->int_n_idx = idx;
17421 type = int_n_trees [idx].signed_type;
17422 break;
17423 case RID_LONG:
17424 if (decl_specs)
17425 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17426 type = long_integer_type_node;
17427 break;
17428 case RID_SIGNED:
17429 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17430 type = integer_type_node;
17431 break;
17432 case RID_UNSIGNED:
17433 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17434 type = unsigned_type_node;
17435 break;
17436 case RID_FLOAT:
17437 type = float_type_node;
17438 break;
17439 case RID_DOUBLE:
17440 type = double_type_node;
17441 break;
17442 case RID_VOID:
17443 type = void_type_node;
17444 break;
17446 case RID_AUTO:
17447 maybe_warn_cpp0x (CPP0X_AUTO);
17448 if (parser->auto_is_implicit_function_template_parm_p)
17450 /* The 'auto' might be the placeholder return type for a function decl
17451 with trailing return type. */
17452 bool have_trailing_return_fn_decl = false;
17454 cp_parser_parse_tentatively (parser);
17455 cp_lexer_consume_token (parser->lexer);
17456 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17457 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17458 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17459 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17461 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17463 cp_lexer_consume_token (parser->lexer);
17464 cp_parser_skip_to_closing_parenthesis (parser,
17465 /*recovering*/false,
17466 /*or_comma*/false,
17467 /*consume_paren*/true);
17468 continue;
17471 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17473 have_trailing_return_fn_decl = true;
17474 break;
17477 cp_lexer_consume_token (parser->lexer);
17479 cp_parser_abort_tentative_parse (parser);
17481 if (have_trailing_return_fn_decl)
17483 type = make_auto ();
17484 break;
17487 if (cxx_dialect >= cxx14)
17489 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17490 type = TREE_TYPE (type);
17492 else
17493 type = error_mark_node;
17495 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17497 if (cxx_dialect < cxx14)
17498 error_at (token->location,
17499 "use of %<auto%> in lambda parameter declaration "
17500 "only available with "
17501 "-std=c++14 or -std=gnu++14");
17503 else if (cxx_dialect < cxx14)
17504 error_at (token->location,
17505 "use of %<auto%> in parameter declaration "
17506 "only available with "
17507 "-std=c++14 or -std=gnu++14");
17508 else if (!flag_concepts)
17509 pedwarn (token->location, 0,
17510 "use of %<auto%> in parameter declaration "
17511 "only available with -fconcepts");
17513 else
17514 type = make_auto ();
17515 break;
17517 case RID_DECLTYPE:
17518 /* Since DR 743, decltype can either be a simple-type-specifier by
17519 itself or begin a nested-name-specifier. Parsing it will replace
17520 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17521 handling below decide what to do. */
17522 cp_parser_decltype (parser);
17523 cp_lexer_set_token_position (parser->lexer, token);
17524 break;
17526 case RID_TYPEOF:
17527 /* Consume the `typeof' token. */
17528 cp_lexer_consume_token (parser->lexer);
17529 /* Parse the operand to `typeof'. */
17530 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17531 /* If it is not already a TYPE, take its type. */
17532 if (!TYPE_P (type))
17533 type = finish_typeof (type);
17535 if (decl_specs)
17536 cp_parser_set_decl_spec_type (decl_specs, type,
17537 token,
17538 /*type_definition_p=*/false);
17540 return type;
17542 case RID_UNDERLYING_TYPE:
17543 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17544 if (decl_specs)
17545 cp_parser_set_decl_spec_type (decl_specs, type,
17546 token,
17547 /*type_definition_p=*/false);
17549 return type;
17551 case RID_BASES:
17552 case RID_DIRECT_BASES:
17553 type = cp_parser_trait_expr (parser, token->keyword);
17554 if (decl_specs)
17555 cp_parser_set_decl_spec_type (decl_specs, type,
17556 token,
17557 /*type_definition_p=*/false);
17558 return type;
17559 default:
17560 break;
17563 /* If token is an already-parsed decltype not followed by ::,
17564 it's a simple-type-specifier. */
17565 if (token->type == CPP_DECLTYPE
17566 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17568 type = saved_checks_value (token->u.tree_check_value);
17569 if (decl_specs)
17571 cp_parser_set_decl_spec_type (decl_specs, type,
17572 token,
17573 /*type_definition_p=*/false);
17574 /* Remember that we are handling a decltype in order to
17575 implement the resolution of DR 1510 when the argument
17576 isn't instantiation dependent. */
17577 decl_specs->decltype_p = true;
17579 cp_lexer_consume_token (parser->lexer);
17580 return type;
17583 /* If the type-specifier was for a built-in type, we're done. */
17584 if (type)
17586 /* Record the type. */
17587 if (decl_specs
17588 && (token->keyword != RID_SIGNED
17589 && token->keyword != RID_UNSIGNED
17590 && token->keyword != RID_SHORT
17591 && token->keyword != RID_LONG))
17592 cp_parser_set_decl_spec_type (decl_specs,
17593 type,
17594 token,
17595 /*type_definition_p=*/false);
17596 if (decl_specs)
17597 decl_specs->any_specifiers_p = true;
17599 /* Consume the token. */
17600 cp_lexer_consume_token (parser->lexer);
17602 if (type == error_mark_node)
17603 return error_mark_node;
17605 /* There is no valid C++ program where a non-template type is
17606 followed by a "<". That usually indicates that the user thought
17607 that the type was a template. */
17608 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17609 token->location);
17611 return TYPE_NAME (type);
17614 /* The type-specifier must be a user-defined type. */
17615 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17617 bool qualified_p;
17618 bool global_p;
17620 /* Don't gobble tokens or issue error messages if this is an
17621 optional type-specifier. */
17622 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17623 cp_parser_parse_tentatively (parser);
17625 token = cp_lexer_peek_token (parser->lexer);
17627 /* Look for the optional `::' operator. */
17628 global_p
17629 = (cp_parser_global_scope_opt (parser,
17630 /*current_scope_valid_p=*/false)
17631 != NULL_TREE);
17632 /* Look for the nested-name specifier. */
17633 qualified_p
17634 = (cp_parser_nested_name_specifier_opt (parser,
17635 /*typename_keyword_p=*/false,
17636 /*check_dependency_p=*/true,
17637 /*type_p=*/false,
17638 /*is_declaration=*/false)
17639 != NULL_TREE);
17640 /* If we have seen a nested-name-specifier, and the next token
17641 is `template', then we are using the template-id production. */
17642 if (parser->scope
17643 && cp_parser_optional_template_keyword (parser))
17645 /* Look for the template-id. */
17646 type = cp_parser_template_id (parser,
17647 /*template_keyword_p=*/true,
17648 /*check_dependency_p=*/true,
17649 none_type,
17650 /*is_declaration=*/false);
17651 /* If the template-id did not name a type, we are out of
17652 luck. */
17653 if (TREE_CODE (type) != TYPE_DECL)
17655 cp_parser_error (parser, "expected template-id for type");
17656 type = NULL_TREE;
17659 /* Otherwise, look for a type-name. */
17660 else
17661 type = cp_parser_type_name (parser);
17662 /* Keep track of all name-lookups performed in class scopes. */
17663 if (type
17664 && !global_p
17665 && !qualified_p
17666 && TREE_CODE (type) == TYPE_DECL
17667 && identifier_p (DECL_NAME (type)))
17668 maybe_note_name_used_in_class (DECL_NAME (type), type);
17669 /* If it didn't work out, we don't have a TYPE. */
17670 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17671 && !cp_parser_parse_definitely (parser))
17672 type = NULL_TREE;
17673 if (!type && cxx_dialect >= cxx17)
17675 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17676 cp_parser_parse_tentatively (parser);
17678 cp_parser_global_scope_opt (parser,
17679 /*current_scope_valid_p=*/false);
17680 cp_parser_nested_name_specifier_opt (parser,
17681 /*typename_keyword_p=*/false,
17682 /*check_dependency_p=*/true,
17683 /*type_p=*/false,
17684 /*is_declaration=*/false);
17685 tree name = cp_parser_identifier (parser);
17686 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17687 && parser->scope != error_mark_node)
17689 tree tmpl = cp_parser_lookup_name (parser, name,
17690 none_type,
17691 /*is_template=*/false,
17692 /*is_namespace=*/false,
17693 /*check_dependency=*/true,
17694 /*ambiguous_decls=*/NULL,
17695 token->location);
17696 if (tmpl && tmpl != error_mark_node
17697 && (DECL_CLASS_TEMPLATE_P (tmpl)
17698 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17699 type = make_template_placeholder (tmpl);
17700 else
17702 type = error_mark_node;
17703 if (!cp_parser_simulate_error (parser))
17704 cp_parser_name_lookup_error (parser, name, tmpl,
17705 NLE_TYPE, token->location);
17708 else
17709 type = error_mark_node;
17711 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17712 && !cp_parser_parse_definitely (parser))
17713 type = NULL_TREE;
17715 if (type && decl_specs)
17716 cp_parser_set_decl_spec_type (decl_specs, type,
17717 token,
17718 /*type_definition_p=*/false);
17721 /* If we didn't get a type-name, issue an error message. */
17722 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17724 cp_parser_error (parser, "expected type-name");
17725 return error_mark_node;
17728 if (type && type != error_mark_node)
17730 /* See if TYPE is an Objective-C type, and if so, parse and
17731 accept any protocol references following it. Do this before
17732 the cp_parser_check_for_invalid_template_id() call, because
17733 Objective-C types can be followed by '<...>' which would
17734 enclose protocol names rather than template arguments, and so
17735 everything is fine. */
17736 if (c_dialect_objc () && !parser->scope
17737 && (objc_is_id (type) || objc_is_class_name (type)))
17739 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17740 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17742 /* Clobber the "unqualified" type previously entered into
17743 DECL_SPECS with the new, improved protocol-qualified version. */
17744 if (decl_specs)
17745 decl_specs->type = qual_type;
17747 return qual_type;
17750 /* There is no valid C++ program where a non-template type is
17751 followed by a "<". That usually indicates that the user
17752 thought that the type was a template. */
17753 cp_parser_check_for_invalid_template_id (parser, type,
17754 none_type,
17755 token->location);
17758 return type;
17761 /* Parse a type-name.
17763 type-name:
17764 class-name
17765 enum-name
17766 typedef-name
17767 simple-template-id [in c++0x]
17769 enum-name:
17770 identifier
17772 typedef-name:
17773 identifier
17775 Concepts:
17777 type-name:
17778 concept-name
17779 partial-concept-id
17781 concept-name:
17782 identifier
17784 Returns a TYPE_DECL for the type. */
17786 static tree
17787 cp_parser_type_name (cp_parser* parser)
17789 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17792 /* See above. */
17793 static tree
17794 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17796 tree type_decl;
17798 /* We can't know yet whether it is a class-name or not. */
17799 cp_parser_parse_tentatively (parser);
17800 /* Try a class-name. */
17801 type_decl = cp_parser_class_name (parser,
17802 typename_keyword_p,
17803 /*template_keyword_p=*/false,
17804 none_type,
17805 /*check_dependency_p=*/true,
17806 /*class_head_p=*/false,
17807 /*is_declaration=*/false);
17808 /* If it's not a class-name, keep looking. */
17809 if (!cp_parser_parse_definitely (parser))
17811 if (cxx_dialect < cxx11)
17812 /* It must be a typedef-name or an enum-name. */
17813 return cp_parser_nonclass_name (parser);
17815 cp_parser_parse_tentatively (parser);
17816 /* It is either a simple-template-id representing an
17817 instantiation of an alias template... */
17818 type_decl = cp_parser_template_id (parser,
17819 /*template_keyword_p=*/false,
17820 /*check_dependency_p=*/true,
17821 none_type,
17822 /*is_declaration=*/false);
17823 /* Note that this must be an instantiation of an alias template
17824 because [temp.names]/6 says:
17826 A template-id that names an alias template specialization
17827 is a type-name.
17829 Whereas [temp.names]/7 says:
17831 A simple-template-id that names a class template
17832 specialization is a class-name.
17834 With concepts, this could also be a partial-concept-id that
17835 declares a non-type template parameter. */
17836 if (type_decl != NULL_TREE
17837 && TREE_CODE (type_decl) == TYPE_DECL
17838 && TYPE_DECL_ALIAS_P (type_decl))
17839 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17840 else if (is_constrained_parameter (type_decl))
17841 /* Don't do anything. */ ;
17842 else
17843 cp_parser_simulate_error (parser);
17845 if (!cp_parser_parse_definitely (parser))
17846 /* ... Or a typedef-name or an enum-name. */
17847 return cp_parser_nonclass_name (parser);
17850 return type_decl;
17853 /* Check if DECL and ARGS can form a constrained-type-specifier.
17854 If ARGS is non-null, we try to form a concept check of the
17855 form DECL<?, ARGS> where ? is a wildcard that matches any
17856 kind of template argument. If ARGS is NULL, then we try to
17857 form a concept check of the form DECL<?>. */
17859 static tree
17860 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17861 tree decl, tree args)
17863 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17865 /* If we a constrained-type-specifier cannot be deduced. */
17866 if (parser->prevent_constrained_type_specifiers)
17867 return NULL_TREE;
17869 /* A constrained type specifier can only be found in an
17870 overload set or as a reference to a template declaration.
17872 FIXME: This might be masking a bug. It's possible that
17873 that the deduction below is causing template specializations
17874 to be formed with the wildcard as an argument. */
17875 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17876 return NULL_TREE;
17878 /* Try to build a call expression that evaluates the
17879 concept. This can fail if the overload set refers
17880 only to non-templates. */
17881 tree placeholder = build_nt (WILDCARD_DECL);
17882 tree check = build_concept_check (decl, placeholder, args);
17883 if (check == error_mark_node)
17884 return NULL_TREE;
17886 /* Deduce the checked constraint and the prototype parameter.
17888 FIXME: In certain cases, failure to deduce should be a
17889 diagnosable error. */
17890 tree conc;
17891 tree proto;
17892 if (!deduce_constrained_parameter (check, conc, proto))
17893 return NULL_TREE;
17895 /* In template parameter scope, this results in a constrained
17896 parameter. Return a descriptor of that parm. */
17897 if (processing_template_parmlist)
17898 return build_constrained_parameter (conc, proto, args);
17900 /* In a parameter-declaration-clause, constrained-type
17901 specifiers result in invented template parameters. */
17902 if (parser->auto_is_implicit_function_template_parm_p)
17904 tree x = build_constrained_parameter (conc, proto, args);
17905 return synthesize_implicit_template_parm (parser, x);
17907 else
17909 /* Otherwise, we're in a context where the constrained
17910 type name is deduced and the constraint applies
17911 after deduction. */
17912 return make_constrained_auto (conc, args);
17915 return NULL_TREE;
17918 /* If DECL refers to a concept, return a TYPE_DECL representing
17919 the result of using the constrained type specifier in the
17920 current context. DECL refers to a concept if
17922 - it is an overload set containing a function concept taking a single
17923 type argument, or
17925 - it is a variable concept taking a single type argument. */
17927 static tree
17928 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17930 if (flag_concepts
17931 && (TREE_CODE (decl) == OVERLOAD
17932 || BASELINK_P (decl)
17933 || variable_concept_p (decl)))
17934 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17935 else
17936 return NULL_TREE;
17939 /* Check if DECL and ARGS form a partial-concept-id. If so,
17940 assign ID to the resulting constrained placeholder.
17942 Returns true if the partial-concept-id designates a placeholder
17943 and false otherwise. Note that *id is set to NULL_TREE in
17944 this case. */
17946 static tree
17947 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17949 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17952 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17953 or a concept-name.
17955 enum-name:
17956 identifier
17958 typedef-name:
17959 identifier
17961 concept-name:
17962 identifier
17964 Returns a TYPE_DECL for the type. */
17966 static tree
17967 cp_parser_nonclass_name (cp_parser* parser)
17969 tree type_decl;
17970 tree identifier;
17972 cp_token *token = cp_lexer_peek_token (parser->lexer);
17973 identifier = cp_parser_identifier (parser);
17974 if (identifier == error_mark_node)
17975 return error_mark_node;
17977 /* Look up the type-name. */
17978 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17980 type_decl = strip_using_decl (type_decl);
17982 /* If we found an overload set, then it may refer to a concept-name. */
17983 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17984 type_decl = decl;
17986 if (TREE_CODE (type_decl) != TYPE_DECL
17987 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17989 /* See if this is an Objective-C type. */
17990 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17991 tree type = objc_get_protocol_qualified_type (identifier, protos);
17992 if (type)
17993 type_decl = TYPE_NAME (type);
17996 /* Issue an error if we did not find a type-name. */
17997 if (TREE_CODE (type_decl) != TYPE_DECL
17998 /* In Objective-C, we have the complication that class names are
17999 normally type names and start declarations (eg, the
18000 "NSObject" in "NSObject *object;"), but can be used in an
18001 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18002 is an expression. So, a classname followed by a dot is not a
18003 valid type-name. */
18004 || (objc_is_class_name (TREE_TYPE (type_decl))
18005 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18007 if (!cp_parser_simulate_error (parser))
18008 cp_parser_name_lookup_error (parser, identifier, type_decl,
18009 NLE_TYPE, token->location);
18010 return error_mark_node;
18012 /* Remember that the name was used in the definition of the
18013 current class so that we can check later to see if the
18014 meaning would have been different after the class was
18015 entirely defined. */
18016 else if (type_decl != error_mark_node
18017 && !parser->scope)
18018 maybe_note_name_used_in_class (identifier, type_decl);
18020 return type_decl;
18023 /* Parse an elaborated-type-specifier. Note that the grammar given
18024 here incorporates the resolution to DR68.
18026 elaborated-type-specifier:
18027 class-key :: [opt] nested-name-specifier [opt] identifier
18028 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18029 enum-key :: [opt] nested-name-specifier [opt] identifier
18030 typename :: [opt] nested-name-specifier identifier
18031 typename :: [opt] nested-name-specifier template [opt]
18032 template-id
18034 GNU extension:
18036 elaborated-type-specifier:
18037 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18038 class-key attributes :: [opt] nested-name-specifier [opt]
18039 template [opt] template-id
18040 enum attributes :: [opt] nested-name-specifier [opt] identifier
18042 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18043 declared `friend'. If IS_DECLARATION is TRUE, then this
18044 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18045 something is being declared.
18047 Returns the TYPE specified. */
18049 static tree
18050 cp_parser_elaborated_type_specifier (cp_parser* parser,
18051 bool is_friend,
18052 bool is_declaration)
18054 enum tag_types tag_type;
18055 tree identifier;
18056 tree type = NULL_TREE;
18057 tree attributes = NULL_TREE;
18058 tree globalscope;
18059 cp_token *token = NULL;
18061 /* See if we're looking at the `enum' keyword. */
18062 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18064 /* Consume the `enum' token. */
18065 cp_lexer_consume_token (parser->lexer);
18066 /* Remember that it's an enumeration type. */
18067 tag_type = enum_type;
18068 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18069 enums) is used here. */
18070 cp_token *token = cp_lexer_peek_token (parser->lexer);
18071 if (cp_parser_is_keyword (token, RID_CLASS)
18072 || cp_parser_is_keyword (token, RID_STRUCT))
18074 gcc_rich_location richloc (token->location);
18075 richloc.add_range (input_location);
18076 richloc.add_fixit_remove ();
18077 pedwarn (&richloc, 0, "elaborated-type-specifier for "
18078 "a scoped enum must not use the %qD keyword",
18079 token->u.value);
18080 /* Consume the `struct' or `class' and parse it anyway. */
18081 cp_lexer_consume_token (parser->lexer);
18083 /* Parse the attributes. */
18084 attributes = cp_parser_attributes_opt (parser);
18086 /* Or, it might be `typename'. */
18087 else if (cp_lexer_next_token_is_keyword (parser->lexer,
18088 RID_TYPENAME))
18090 /* Consume the `typename' token. */
18091 cp_lexer_consume_token (parser->lexer);
18092 /* Remember that it's a `typename' type. */
18093 tag_type = typename_type;
18095 /* Otherwise it must be a class-key. */
18096 else
18098 tag_type = cp_parser_class_key (parser);
18099 if (tag_type == none_type)
18100 return error_mark_node;
18101 /* Parse the attributes. */
18102 attributes = cp_parser_attributes_opt (parser);
18105 /* Look for the `::' operator. */
18106 globalscope = cp_parser_global_scope_opt (parser,
18107 /*current_scope_valid_p=*/false);
18108 /* Look for the nested-name-specifier. */
18109 tree nested_name_specifier;
18110 if (tag_type == typename_type && !globalscope)
18112 nested_name_specifier
18113 = cp_parser_nested_name_specifier (parser,
18114 /*typename_keyword_p=*/true,
18115 /*check_dependency_p=*/true,
18116 /*type_p=*/true,
18117 is_declaration);
18118 if (!nested_name_specifier)
18119 return error_mark_node;
18121 else
18122 /* Even though `typename' is not present, the proposed resolution
18123 to Core Issue 180 says that in `class A<T>::B', `B' should be
18124 considered a type-name, even if `A<T>' is dependent. */
18125 nested_name_specifier
18126 = cp_parser_nested_name_specifier_opt (parser,
18127 /*typename_keyword_p=*/true,
18128 /*check_dependency_p=*/true,
18129 /*type_p=*/true,
18130 is_declaration);
18131 /* For everything but enumeration types, consider a template-id.
18132 For an enumeration type, consider only a plain identifier. */
18133 if (tag_type != enum_type)
18135 bool template_p = false;
18136 tree decl;
18138 /* Allow the `template' keyword. */
18139 template_p = cp_parser_optional_template_keyword (parser);
18140 /* If we didn't see `template', we don't know if there's a
18141 template-id or not. */
18142 if (!template_p)
18143 cp_parser_parse_tentatively (parser);
18144 /* The `template' keyword must follow a nested-name-specifier. */
18145 else if (!nested_name_specifier)
18147 cp_parser_error (parser, "%<template%> must follow a nested-"
18148 "name-specifier");
18149 return error_mark_node;
18152 /* Parse the template-id. */
18153 token = cp_lexer_peek_token (parser->lexer);
18154 decl = cp_parser_template_id (parser, template_p,
18155 /*check_dependency_p=*/true,
18156 tag_type,
18157 is_declaration);
18158 /* If we didn't find a template-id, look for an ordinary
18159 identifier. */
18160 if (!template_p && !cp_parser_parse_definitely (parser))
18162 /* We can get here when cp_parser_template_id, called by
18163 cp_parser_class_name with tag_type == none_type, succeeds
18164 and caches a BASELINK. Then, when called again here,
18165 instead of failing and returning an error_mark_node
18166 returns it (see template/typename17.C in C++11).
18167 ??? Could we diagnose this earlier? */
18168 else if (tag_type == typename_type && BASELINK_P (decl))
18170 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18171 type = error_mark_node;
18173 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18174 in effect, then we must assume that, upon instantiation, the
18175 template will correspond to a class. */
18176 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18177 && tag_type == typename_type)
18178 type = make_typename_type (parser->scope, decl,
18179 typename_type,
18180 /*complain=*/tf_error);
18181 /* If the `typename' keyword is in effect and DECL is not a type
18182 decl, then type is non existent. */
18183 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18185 else if (TREE_CODE (decl) == TYPE_DECL)
18187 type = check_elaborated_type_specifier (tag_type, decl,
18188 /*allow_template_p=*/true);
18190 /* If the next token is a semicolon, this must be a specialization,
18191 instantiation, or friend declaration. Check the scope while we
18192 still know whether or not we had a nested-name-specifier. */
18193 if (type != error_mark_node
18194 && !nested_name_specifier && !is_friend
18195 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18196 check_unqualified_spec_or_inst (type, token->location);
18198 else if (decl == error_mark_node)
18199 type = error_mark_node;
18202 if (!type)
18204 token = cp_lexer_peek_token (parser->lexer);
18205 identifier = cp_parser_identifier (parser);
18207 if (identifier == error_mark_node)
18209 parser->scope = NULL_TREE;
18210 return error_mark_node;
18213 /* For a `typename', we needn't call xref_tag. */
18214 if (tag_type == typename_type
18215 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18216 return cp_parser_make_typename_type (parser, identifier,
18217 token->location);
18219 /* Template parameter lists apply only if we are not within a
18220 function parameter list. */
18221 bool template_parm_lists_apply
18222 = parser->num_template_parameter_lists;
18223 if (template_parm_lists_apply)
18224 for (cp_binding_level *s = current_binding_level;
18225 s && s->kind != sk_template_parms;
18226 s = s->level_chain)
18227 if (s->kind == sk_function_parms)
18228 template_parm_lists_apply = false;
18230 /* Look up a qualified name in the usual way. */
18231 if (parser->scope)
18233 tree decl;
18234 tree ambiguous_decls;
18236 decl = cp_parser_lookup_name (parser, identifier,
18237 tag_type,
18238 /*is_template=*/false,
18239 /*is_namespace=*/false,
18240 /*check_dependency=*/true,
18241 &ambiguous_decls,
18242 token->location);
18244 /* If the lookup was ambiguous, an error will already have been
18245 issued. */
18246 if (ambiguous_decls)
18247 return error_mark_node;
18249 /* If we are parsing friend declaration, DECL may be a
18250 TEMPLATE_DECL tree node here. However, we need to check
18251 whether this TEMPLATE_DECL results in valid code. Consider
18252 the following example:
18254 namespace N {
18255 template <class T> class C {};
18257 class X {
18258 template <class T> friend class N::C; // #1, valid code
18260 template <class T> class Y {
18261 friend class N::C; // #2, invalid code
18264 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18265 name lookup of `N::C'. We see that friend declaration must
18266 be template for the code to be valid. Note that
18267 processing_template_decl does not work here since it is
18268 always 1 for the above two cases. */
18270 decl = (cp_parser_maybe_treat_template_as_class
18271 (decl, /*tag_name_p=*/is_friend
18272 && template_parm_lists_apply));
18274 if (TREE_CODE (decl) != TYPE_DECL)
18276 cp_parser_diagnose_invalid_type_name (parser,
18277 identifier,
18278 token->location);
18279 return error_mark_node;
18282 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18284 bool allow_template = (template_parm_lists_apply
18285 || DECL_SELF_REFERENCE_P (decl));
18286 type = check_elaborated_type_specifier (tag_type, decl,
18287 allow_template);
18289 if (type == error_mark_node)
18290 return error_mark_node;
18293 /* Forward declarations of nested types, such as
18295 class C1::C2;
18296 class C1::C2::C3;
18298 are invalid unless all components preceding the final '::'
18299 are complete. If all enclosing types are complete, these
18300 declarations become merely pointless.
18302 Invalid forward declarations of nested types are errors
18303 caught elsewhere in parsing. Those that are pointless arrive
18304 here. */
18306 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18307 && !is_friend && !processing_explicit_instantiation)
18308 warning (0, "declaration %qD does not declare anything", decl);
18310 type = TREE_TYPE (decl);
18312 else
18314 /* An elaborated-type-specifier sometimes introduces a new type and
18315 sometimes names an existing type. Normally, the rule is that it
18316 introduces a new type only if there is not an existing type of
18317 the same name already in scope. For example, given:
18319 struct S {};
18320 void f() { struct S s; }
18322 the `struct S' in the body of `f' is the same `struct S' as in
18323 the global scope; the existing definition is used. However, if
18324 there were no global declaration, this would introduce a new
18325 local class named `S'.
18327 An exception to this rule applies to the following code:
18329 namespace N { struct S; }
18331 Here, the elaborated-type-specifier names a new type
18332 unconditionally; even if there is already an `S' in the
18333 containing scope this declaration names a new type.
18334 This exception only applies if the elaborated-type-specifier
18335 forms the complete declaration:
18337 [class.name]
18339 A declaration consisting solely of `class-key identifier ;' is
18340 either a redeclaration of the name in the current scope or a
18341 forward declaration of the identifier as a class name. It
18342 introduces the name into the current scope.
18344 We are in this situation precisely when the next token is a `;'.
18346 An exception to the exception is that a `friend' declaration does
18347 *not* name a new type; i.e., given:
18349 struct S { friend struct T; };
18351 `T' is not a new type in the scope of `S'.
18353 Also, `new struct S' or `sizeof (struct S)' never results in the
18354 definition of a new type; a new type can only be declared in a
18355 declaration context. */
18357 tag_scope ts;
18358 bool template_p;
18360 if (is_friend)
18361 /* Friends have special name lookup rules. */
18362 ts = ts_within_enclosing_non_class;
18363 else if (is_declaration
18364 && cp_lexer_next_token_is (parser->lexer,
18365 CPP_SEMICOLON))
18366 /* This is a `class-key identifier ;' */
18367 ts = ts_current;
18368 else
18369 ts = ts_global;
18371 template_p =
18372 (template_parm_lists_apply
18373 && (cp_parser_next_token_starts_class_definition_p (parser)
18374 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18375 /* An unqualified name was used to reference this type, so
18376 there were no qualifying templates. */
18377 if (template_parm_lists_apply
18378 && !cp_parser_check_template_parameters (parser,
18379 /*num_templates=*/0,
18380 /*template_id*/false,
18381 token->location,
18382 /*declarator=*/NULL))
18383 return error_mark_node;
18384 type = xref_tag (tag_type, identifier, ts, template_p);
18388 if (type == error_mark_node)
18389 return error_mark_node;
18391 /* Allow attributes on forward declarations of classes. */
18392 if (attributes)
18394 if (TREE_CODE (type) == TYPENAME_TYPE)
18395 warning (OPT_Wattributes,
18396 "attributes ignored on uninstantiated type");
18397 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18398 && ! processing_explicit_instantiation)
18399 warning (OPT_Wattributes,
18400 "attributes ignored on template instantiation");
18401 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18402 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18403 else
18404 warning (OPT_Wattributes,
18405 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18408 if (tag_type != enum_type)
18410 /* Indicate whether this class was declared as a `class' or as a
18411 `struct'. */
18412 if (CLASS_TYPE_P (type))
18413 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18414 cp_parser_check_class_key (tag_type, type);
18417 /* A "<" cannot follow an elaborated type specifier. If that
18418 happens, the user was probably trying to form a template-id. */
18419 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18420 token->location);
18422 return type;
18425 /* Parse an enum-specifier.
18427 enum-specifier:
18428 enum-head { enumerator-list [opt] }
18429 enum-head { enumerator-list , } [C++0x]
18431 enum-head:
18432 enum-key identifier [opt] enum-base [opt]
18433 enum-key nested-name-specifier identifier enum-base [opt]
18435 enum-key:
18436 enum
18437 enum class [C++0x]
18438 enum struct [C++0x]
18440 enum-base: [C++0x]
18441 : type-specifier-seq
18443 opaque-enum-specifier:
18444 enum-key identifier enum-base [opt] ;
18446 GNU Extensions:
18447 enum-key attributes[opt] identifier [opt] enum-base [opt]
18448 { enumerator-list [opt] }attributes[opt]
18449 enum-key attributes[opt] identifier [opt] enum-base [opt]
18450 { enumerator-list, }attributes[opt] [C++0x]
18452 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18453 if the token stream isn't an enum-specifier after all. */
18455 static tree
18456 cp_parser_enum_specifier (cp_parser* parser)
18458 tree identifier;
18459 tree type = NULL_TREE;
18460 tree prev_scope;
18461 tree nested_name_specifier = NULL_TREE;
18462 tree attributes;
18463 bool scoped_enum_p = false;
18464 bool has_underlying_type = false;
18465 bool nested_being_defined = false;
18466 bool new_value_list = false;
18467 bool is_new_type = false;
18468 bool is_unnamed = false;
18469 tree underlying_type = NULL_TREE;
18470 cp_token *type_start_token = NULL;
18471 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18473 parser->colon_corrects_to_scope_p = false;
18475 /* Parse tentatively so that we can back up if we don't find a
18476 enum-specifier. */
18477 cp_parser_parse_tentatively (parser);
18479 /* Caller guarantees that the current token is 'enum', an identifier
18480 possibly follows, and the token after that is an opening brace.
18481 If we don't have an identifier, fabricate an anonymous name for
18482 the enumeration being defined. */
18483 cp_lexer_consume_token (parser->lexer);
18485 /* Parse the "class" or "struct", which indicates a scoped
18486 enumeration type in C++0x. */
18487 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18488 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18490 if (cxx_dialect < cxx11)
18491 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18493 /* Consume the `struct' or `class' token. */
18494 cp_lexer_consume_token (parser->lexer);
18496 scoped_enum_p = true;
18499 attributes = cp_parser_attributes_opt (parser);
18501 /* Clear the qualification. */
18502 parser->scope = NULL_TREE;
18503 parser->qualifying_scope = NULL_TREE;
18504 parser->object_scope = NULL_TREE;
18506 /* Figure out in what scope the declaration is being placed. */
18507 prev_scope = current_scope ();
18509 type_start_token = cp_lexer_peek_token (parser->lexer);
18511 push_deferring_access_checks (dk_no_check);
18512 nested_name_specifier
18513 = cp_parser_nested_name_specifier_opt (parser,
18514 /*typename_keyword_p=*/true,
18515 /*check_dependency_p=*/false,
18516 /*type_p=*/false,
18517 /*is_declaration=*/false);
18519 if (nested_name_specifier)
18521 tree name;
18523 identifier = cp_parser_identifier (parser);
18524 name = cp_parser_lookup_name (parser, identifier,
18525 enum_type,
18526 /*is_template=*/false,
18527 /*is_namespace=*/false,
18528 /*check_dependency=*/true,
18529 /*ambiguous_decls=*/NULL,
18530 input_location);
18531 if (name && name != error_mark_node)
18533 type = TREE_TYPE (name);
18534 if (TREE_CODE (type) == TYPENAME_TYPE)
18536 /* Are template enums allowed in ISO? */
18537 if (template_parm_scope_p ())
18538 pedwarn (type_start_token->location, OPT_Wpedantic,
18539 "%qD is an enumeration template", name);
18540 /* ignore a typename reference, for it will be solved by name
18541 in start_enum. */
18542 type = NULL_TREE;
18545 else if (nested_name_specifier == error_mark_node)
18546 /* We already issued an error. */;
18547 else
18549 error_at (type_start_token->location,
18550 "%qD does not name an enumeration in %qT",
18551 identifier, nested_name_specifier);
18552 nested_name_specifier = error_mark_node;
18555 else
18557 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18558 identifier = cp_parser_identifier (parser);
18559 else
18561 identifier = make_anon_name ();
18562 is_unnamed = true;
18563 if (scoped_enum_p)
18564 error_at (type_start_token->location,
18565 "unnamed scoped enum is not allowed");
18568 pop_deferring_access_checks ();
18570 /* Check for the `:' that denotes a specified underlying type in C++0x.
18571 Note that a ':' could also indicate a bitfield width, however. */
18572 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18574 cp_decl_specifier_seq type_specifiers;
18576 /* Consume the `:'. */
18577 cp_lexer_consume_token (parser->lexer);
18579 /* Parse the type-specifier-seq. */
18580 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18581 /*is_trailing_return=*/false,
18582 &type_specifiers);
18584 /* At this point this is surely not elaborated type specifier. */
18585 if (!cp_parser_parse_definitely (parser))
18586 return NULL_TREE;
18588 if (cxx_dialect < cxx11)
18589 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18591 has_underlying_type = true;
18593 /* If that didn't work, stop. */
18594 if (type_specifiers.type != error_mark_node)
18596 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18597 /*initialized=*/0, NULL);
18598 if (underlying_type == error_mark_node
18599 || check_for_bare_parameter_packs (underlying_type))
18600 underlying_type = NULL_TREE;
18604 /* Look for the `{' but don't consume it yet. */
18605 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18607 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18609 cp_parser_error (parser, "expected %<{%>");
18610 if (has_underlying_type)
18612 type = NULL_TREE;
18613 goto out;
18616 /* An opaque-enum-specifier must have a ';' here. */
18617 if ((scoped_enum_p || underlying_type)
18618 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18620 cp_parser_error (parser, "expected %<;%> or %<{%>");
18621 if (has_underlying_type)
18623 type = NULL_TREE;
18624 goto out;
18629 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18630 return NULL_TREE;
18632 if (nested_name_specifier)
18634 if (CLASS_TYPE_P (nested_name_specifier))
18636 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18637 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18638 push_scope (nested_name_specifier);
18640 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18642 push_nested_namespace (nested_name_specifier);
18646 /* Issue an error message if type-definitions are forbidden here. */
18647 if (!cp_parser_check_type_definition (parser))
18648 type = error_mark_node;
18649 else
18650 /* Create the new type. We do this before consuming the opening
18651 brace so the enum will be recorded as being on the line of its
18652 tag (or the 'enum' keyword, if there is no tag). */
18653 type = start_enum (identifier, type, underlying_type,
18654 attributes, scoped_enum_p, &is_new_type);
18656 /* If the next token is not '{' it is an opaque-enum-specifier or an
18657 elaborated-type-specifier. */
18658 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18660 timevar_push (TV_PARSE_ENUM);
18661 if (nested_name_specifier
18662 && nested_name_specifier != error_mark_node)
18664 /* The following catches invalid code such as:
18665 enum class S<int>::E { A, B, C }; */
18666 if (!processing_specialization
18667 && CLASS_TYPE_P (nested_name_specifier)
18668 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18669 error_at (type_start_token->location, "cannot add an enumerator "
18670 "list to a template instantiation");
18672 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18674 error_at (type_start_token->location,
18675 "%<%T::%E%> has not been declared",
18676 TYPE_CONTEXT (nested_name_specifier),
18677 nested_name_specifier);
18678 type = error_mark_node;
18680 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18681 && !CLASS_TYPE_P (nested_name_specifier))
18683 error_at (type_start_token->location, "nested name specifier "
18684 "%qT for enum declaration does not name a class "
18685 "or namespace", nested_name_specifier);
18686 type = error_mark_node;
18688 /* If that scope does not contain the scope in which the
18689 class was originally declared, the program is invalid. */
18690 else if (prev_scope && !is_ancestor (prev_scope,
18691 nested_name_specifier))
18693 if (at_namespace_scope_p ())
18694 error_at (type_start_token->location,
18695 "declaration of %qD in namespace %qD which does not "
18696 "enclose %qD",
18697 type, prev_scope, nested_name_specifier);
18698 else
18699 error_at (type_start_token->location,
18700 "declaration of %qD in %qD which does not "
18701 "enclose %qD",
18702 type, prev_scope, nested_name_specifier);
18703 type = error_mark_node;
18705 /* If that scope is the scope where the declaration is being placed
18706 the program is invalid. */
18707 else if (CLASS_TYPE_P (nested_name_specifier)
18708 && CLASS_TYPE_P (prev_scope)
18709 && same_type_p (nested_name_specifier, prev_scope))
18711 permerror (type_start_token->location,
18712 "extra qualification not allowed");
18713 nested_name_specifier = NULL_TREE;
18717 if (scoped_enum_p)
18718 begin_scope (sk_scoped_enum, type);
18720 /* Consume the opening brace. */
18721 matching_braces braces;
18722 braces.consume_open (parser);
18724 if (type == error_mark_node)
18725 ; /* Nothing to add */
18726 else if (OPAQUE_ENUM_P (type)
18727 || (cxx_dialect > cxx98 && processing_specialization))
18729 new_value_list = true;
18730 SET_OPAQUE_ENUM_P (type, false);
18731 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18733 else
18735 error_at (type_start_token->location,
18736 "multiple definition of %q#T", type);
18737 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18738 "previous definition here");
18739 type = error_mark_node;
18742 if (type == error_mark_node)
18743 cp_parser_skip_to_end_of_block_or_statement (parser);
18744 /* If the next token is not '}', then there are some enumerators. */
18745 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18747 if (is_unnamed && !scoped_enum_p)
18748 pedwarn (type_start_token->location, OPT_Wpedantic,
18749 "ISO C++ forbids empty unnamed enum");
18751 else
18752 cp_parser_enumerator_list (parser, type);
18754 /* Consume the final '}'. */
18755 braces.require_close (parser);
18757 if (scoped_enum_p)
18758 finish_scope ();
18759 timevar_pop (TV_PARSE_ENUM);
18761 else
18763 /* If a ';' follows, then it is an opaque-enum-specifier
18764 and additional restrictions apply. */
18765 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18767 if (is_unnamed)
18768 error_at (type_start_token->location,
18769 "opaque-enum-specifier without name");
18770 else if (nested_name_specifier)
18771 error_at (type_start_token->location,
18772 "opaque-enum-specifier must use a simple identifier");
18776 /* Look for trailing attributes to apply to this enumeration, and
18777 apply them if appropriate. */
18778 if (cp_parser_allow_gnu_extensions_p (parser))
18780 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18781 cplus_decl_attributes (&type,
18782 trailing_attr,
18783 (int) ATTR_FLAG_TYPE_IN_PLACE);
18786 /* Finish up the enumeration. */
18787 if (type != error_mark_node)
18789 if (new_value_list)
18790 finish_enum_value_list (type);
18791 if (is_new_type)
18792 finish_enum (type);
18795 if (nested_name_specifier)
18797 if (CLASS_TYPE_P (nested_name_specifier))
18799 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18800 pop_scope (nested_name_specifier);
18802 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18804 pop_nested_namespace (nested_name_specifier);
18807 out:
18808 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18809 return type;
18812 /* Parse an enumerator-list. The enumerators all have the indicated
18813 TYPE.
18815 enumerator-list:
18816 enumerator-definition
18817 enumerator-list , enumerator-definition */
18819 static void
18820 cp_parser_enumerator_list (cp_parser* parser, tree type)
18822 while (true)
18824 /* Parse an enumerator-definition. */
18825 cp_parser_enumerator_definition (parser, type);
18827 /* If the next token is not a ',', we've reached the end of
18828 the list. */
18829 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18830 break;
18831 /* Otherwise, consume the `,' and keep going. */
18832 cp_lexer_consume_token (parser->lexer);
18833 /* If the next token is a `}', there is a trailing comma. */
18834 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18836 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18837 pedwarn (input_location, OPT_Wpedantic,
18838 "comma at end of enumerator list");
18839 break;
18844 /* Parse an enumerator-definition. The enumerator has the indicated
18845 TYPE.
18847 enumerator-definition:
18848 enumerator
18849 enumerator = constant-expression
18851 enumerator:
18852 identifier
18854 GNU Extensions:
18856 enumerator-definition:
18857 enumerator attributes [opt]
18858 enumerator attributes [opt] = constant-expression */
18860 static void
18861 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18863 tree identifier;
18864 tree value;
18865 location_t loc;
18867 /* Save the input location because we are interested in the location
18868 of the identifier and not the location of the explicit value. */
18869 loc = cp_lexer_peek_token (parser->lexer)->location;
18871 /* Look for the identifier. */
18872 identifier = cp_parser_identifier (parser);
18873 if (identifier == error_mark_node)
18874 return;
18876 /* Parse any specified attributes. */
18877 tree attrs = cp_parser_attributes_opt (parser);
18879 /* If the next token is an '=', then there is an explicit value. */
18880 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18882 /* Consume the `=' token. */
18883 cp_lexer_consume_token (parser->lexer);
18884 /* Parse the value. */
18885 value = cp_parser_constant_expression (parser);
18887 else
18888 value = NULL_TREE;
18890 /* If we are processing a template, make sure the initializer of the
18891 enumerator doesn't contain any bare template parameter pack. */
18892 if (check_for_bare_parameter_packs (value))
18893 value = error_mark_node;
18895 /* Create the enumerator. */
18896 build_enumerator (identifier, value, type, attrs, loc);
18899 /* Parse a namespace-name.
18901 namespace-name:
18902 original-namespace-name
18903 namespace-alias
18905 Returns the NAMESPACE_DECL for the namespace. */
18907 static tree
18908 cp_parser_namespace_name (cp_parser* parser)
18910 tree identifier;
18911 tree namespace_decl;
18913 cp_token *token = cp_lexer_peek_token (parser->lexer);
18915 /* Get the name of the namespace. */
18916 identifier = cp_parser_identifier (parser);
18917 if (identifier == error_mark_node)
18918 return error_mark_node;
18920 /* Look up the identifier in the currently active scope. Look only
18921 for namespaces, due to:
18923 [basic.lookup.udir]
18925 When looking up a namespace-name in a using-directive or alias
18926 definition, only namespace names are considered.
18928 And:
18930 [basic.lookup.qual]
18932 During the lookup of a name preceding the :: scope resolution
18933 operator, object, function, and enumerator names are ignored.
18935 (Note that cp_parser_qualifying_entity only calls this
18936 function if the token after the name is the scope resolution
18937 operator.) */
18938 namespace_decl = cp_parser_lookup_name (parser, identifier,
18939 none_type,
18940 /*is_template=*/false,
18941 /*is_namespace=*/true,
18942 /*check_dependency=*/true,
18943 /*ambiguous_decls=*/NULL,
18944 token->location);
18945 /* If it's not a namespace, issue an error. */
18946 if (namespace_decl == error_mark_node
18947 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18949 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18951 auto_diagnostic_group d;
18952 name_hint hint;
18953 if (namespace_decl == error_mark_node
18954 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18955 hint = suggest_alternative_in_explicit_scope (token->location,
18956 identifier,
18957 parser->scope);
18958 if (const char *suggestion = hint.suggestion ())
18960 gcc_rich_location richloc (token->location);
18961 richloc.add_fixit_replace (suggestion);
18962 error_at (&richloc,
18963 "%qD is not a namespace-name; did you mean %qs?",
18964 identifier, suggestion);
18966 else
18967 error_at (token->location, "%qD is not a namespace-name",
18968 identifier);
18970 else
18971 cp_parser_error (parser, "expected namespace-name");
18972 namespace_decl = error_mark_node;
18975 return namespace_decl;
18978 /* Parse a namespace-definition.
18980 namespace-definition:
18981 named-namespace-definition
18982 unnamed-namespace-definition
18984 named-namespace-definition:
18985 original-namespace-definition
18986 extension-namespace-definition
18988 original-namespace-definition:
18989 namespace identifier { namespace-body }
18991 extension-namespace-definition:
18992 namespace original-namespace-name { namespace-body }
18994 unnamed-namespace-definition:
18995 namespace { namespace-body } */
18997 static void
18998 cp_parser_namespace_definition (cp_parser* parser)
19000 tree identifier;
19001 int nested_definition_count = 0;
19003 cp_ensure_no_omp_declare_simd (parser);
19004 cp_ensure_no_oacc_routine (parser);
19006 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19008 if (is_inline)
19010 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19011 cp_lexer_consume_token (parser->lexer);
19014 /* Look for the `namespace' keyword. */
19015 cp_token* token
19016 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19018 /* Parse any specified attributes before the identifier. */
19019 tree attribs = cp_parser_attributes_opt (parser);
19021 for (;;)
19023 identifier = NULL_TREE;
19025 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19027 identifier = cp_parser_identifier (parser);
19029 if (cp_next_tokens_can_be_std_attribute_p (parser))
19030 pedwarn (input_location, OPT_Wpedantic,
19031 "standard attributes on namespaces must precede "
19032 "the namespace name");
19034 /* Parse any attributes specified after the identifier. */
19035 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19038 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19039 break;
19041 if (!nested_definition_count && cxx_dialect < cxx17)
19042 pedwarn (input_location, OPT_Wpedantic,
19043 "nested namespace definitions only available with "
19044 "-std=c++17 or -std=gnu++17");
19046 /* Nested namespace names can create new namespaces (unlike
19047 other qualified-ids). */
19048 if (int count = identifier ? push_namespace (identifier) : 0)
19049 nested_definition_count += count;
19050 else
19051 cp_parser_error (parser, "nested namespace name required");
19052 cp_lexer_consume_token (parser->lexer);
19055 if (nested_definition_count && !identifier)
19056 cp_parser_error (parser, "namespace name required");
19058 if (nested_definition_count && attribs)
19059 error_at (token->location,
19060 "a nested namespace definition cannot have attributes");
19061 if (nested_definition_count && is_inline)
19062 error_at (token->location,
19063 "a nested namespace definition cannot be inline");
19065 /* Start the namespace. */
19066 nested_definition_count += push_namespace (identifier, is_inline);
19068 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19070 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19072 /* Look for the `{' to validate starting the namespace. */
19073 matching_braces braces;
19074 if (braces.require_open (parser))
19076 /* Parse the body of the namespace. */
19077 cp_parser_namespace_body (parser);
19079 /* Look for the final `}'. */
19080 braces.require_close (parser);
19083 if (has_visibility)
19084 pop_visibility (1);
19086 /* Pop the nested namespace definitions. */
19087 while (nested_definition_count--)
19088 pop_namespace ();
19091 /* Parse a namespace-body.
19093 namespace-body:
19094 declaration-seq [opt] */
19096 static void
19097 cp_parser_namespace_body (cp_parser* parser)
19099 cp_parser_declaration_seq_opt (parser);
19102 /* Parse a namespace-alias-definition.
19104 namespace-alias-definition:
19105 namespace identifier = qualified-namespace-specifier ; */
19107 static void
19108 cp_parser_namespace_alias_definition (cp_parser* parser)
19110 tree identifier;
19111 tree namespace_specifier;
19113 cp_token *token = cp_lexer_peek_token (parser->lexer);
19115 /* Look for the `namespace' keyword. */
19116 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19117 /* Look for the identifier. */
19118 identifier = cp_parser_identifier (parser);
19119 if (identifier == error_mark_node)
19120 return;
19121 /* Look for the `=' token. */
19122 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19123 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19125 error_at (token->location, "%<namespace%> definition is not allowed here");
19126 /* Skip the definition. */
19127 cp_lexer_consume_token (parser->lexer);
19128 if (cp_parser_skip_to_closing_brace (parser))
19129 cp_lexer_consume_token (parser->lexer);
19130 return;
19132 cp_parser_require (parser, CPP_EQ, RT_EQ);
19133 /* Look for the qualified-namespace-specifier. */
19134 namespace_specifier
19135 = cp_parser_qualified_namespace_specifier (parser);
19136 /* Look for the `;' token. */
19137 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19139 /* Register the alias in the symbol table. */
19140 do_namespace_alias (identifier, namespace_specifier);
19143 /* Parse a qualified-namespace-specifier.
19145 qualified-namespace-specifier:
19146 :: [opt] nested-name-specifier [opt] namespace-name
19148 Returns a NAMESPACE_DECL corresponding to the specified
19149 namespace. */
19151 static tree
19152 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19154 /* Look for the optional `::'. */
19155 cp_parser_global_scope_opt (parser,
19156 /*current_scope_valid_p=*/false);
19158 /* Look for the optional nested-name-specifier. */
19159 cp_parser_nested_name_specifier_opt (parser,
19160 /*typename_keyword_p=*/false,
19161 /*check_dependency_p=*/true,
19162 /*type_p=*/false,
19163 /*is_declaration=*/true);
19165 return cp_parser_namespace_name (parser);
19168 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19169 access declaration.
19171 using-declaration:
19172 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19173 using :: unqualified-id ;
19175 access-declaration:
19176 qualified-id ;
19180 static bool
19181 cp_parser_using_declaration (cp_parser* parser,
19182 bool access_declaration_p)
19184 cp_token *token;
19185 bool typename_p = false;
19186 bool global_scope_p;
19187 tree decl;
19188 tree identifier;
19189 tree qscope;
19190 int oldcount = errorcount;
19191 cp_token *diag_token = NULL;
19193 if (access_declaration_p)
19195 diag_token = cp_lexer_peek_token (parser->lexer);
19196 cp_parser_parse_tentatively (parser);
19198 else
19200 /* Look for the `using' keyword. */
19201 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19203 again:
19204 /* Peek at the next token. */
19205 token = cp_lexer_peek_token (parser->lexer);
19206 /* See if it's `typename'. */
19207 if (token->keyword == RID_TYPENAME)
19209 /* Remember that we've seen it. */
19210 typename_p = true;
19211 /* Consume the `typename' token. */
19212 cp_lexer_consume_token (parser->lexer);
19216 /* Look for the optional global scope qualification. */
19217 global_scope_p
19218 = (cp_parser_global_scope_opt (parser,
19219 /*current_scope_valid_p=*/false)
19220 != NULL_TREE);
19222 /* If we saw `typename', or didn't see `::', then there must be a
19223 nested-name-specifier present. */
19224 if (typename_p || !global_scope_p)
19226 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19227 /*check_dependency_p=*/true,
19228 /*type_p=*/false,
19229 /*is_declaration=*/true);
19230 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19232 cp_parser_skip_to_end_of_block_or_statement (parser);
19233 return false;
19236 /* Otherwise, we could be in either of the two productions. In that
19237 case, treat the nested-name-specifier as optional. */
19238 else
19239 qscope = cp_parser_nested_name_specifier_opt (parser,
19240 /*typename_keyword_p=*/false,
19241 /*check_dependency_p=*/true,
19242 /*type_p=*/false,
19243 /*is_declaration=*/true);
19244 if (!qscope)
19245 qscope = global_namespace;
19246 else if (UNSCOPED_ENUM_P (qscope))
19247 qscope = CP_TYPE_CONTEXT (qscope);
19249 if (access_declaration_p && cp_parser_error_occurred (parser))
19250 /* Something has already gone wrong; there's no need to parse
19251 further. Since an error has occurred, the return value of
19252 cp_parser_parse_definitely will be false, as required. */
19253 return cp_parser_parse_definitely (parser);
19255 token = cp_lexer_peek_token (parser->lexer);
19256 /* Parse the unqualified-id. */
19257 identifier = cp_parser_unqualified_id (parser,
19258 /*template_keyword_p=*/false,
19259 /*check_dependency_p=*/true,
19260 /*declarator_p=*/true,
19261 /*optional_p=*/false);
19263 if (access_declaration_p)
19265 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19266 cp_parser_simulate_error (parser);
19267 if (!cp_parser_parse_definitely (parser))
19268 return false;
19270 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19272 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19273 if (cxx_dialect < cxx17
19274 && !in_system_header_at (ell->location))
19275 pedwarn (ell->location, 0,
19276 "pack expansion in using-declaration only available "
19277 "with -std=c++17 or -std=gnu++17");
19278 qscope = make_pack_expansion (qscope);
19281 /* The function we call to handle a using-declaration is different
19282 depending on what scope we are in. */
19283 if (qscope == error_mark_node || identifier == error_mark_node)
19285 else if (!identifier_p (identifier)
19286 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19287 /* [namespace.udecl]
19289 A using declaration shall not name a template-id. */
19290 error_at (token->location,
19291 "a template-id may not appear in a using-declaration");
19292 else
19294 if (at_class_scope_p ())
19296 /* Create the USING_DECL. */
19297 decl = do_class_using_decl (qscope, identifier);
19299 if (decl && typename_p)
19300 USING_DECL_TYPENAME_P (decl) = 1;
19302 if (check_for_bare_parameter_packs (decl))
19304 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19305 return false;
19307 else
19308 /* Add it to the list of members in this class. */
19309 finish_member_declaration (decl);
19311 else
19313 decl = cp_parser_lookup_name_simple (parser,
19314 identifier,
19315 token->location);
19316 if (decl == error_mark_node)
19317 cp_parser_name_lookup_error (parser, identifier,
19318 decl, NLE_NULL,
19319 token->location);
19320 else if (check_for_bare_parameter_packs (decl))
19322 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19323 return false;
19325 else if (!at_namespace_scope_p ())
19326 finish_local_using_decl (decl, qscope, identifier);
19327 else
19328 finish_namespace_using_decl (decl, qscope, identifier);
19332 if (!access_declaration_p
19333 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19335 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19336 if (cxx_dialect < cxx17)
19337 pedwarn (comma->location, 0,
19338 "comma-separated list in using-declaration only available "
19339 "with -std=c++17 or -std=gnu++17");
19340 goto again;
19343 /* Look for the final `;'. */
19344 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19346 if (access_declaration_p && errorcount == oldcount)
19347 warning_at (diag_token->location, OPT_Wdeprecated,
19348 "access declarations are deprecated "
19349 "in favour of using-declarations; "
19350 "suggestion: add the %<using%> keyword");
19352 return true;
19355 /* Parse an alias-declaration.
19357 alias-declaration:
19358 using identifier attribute-specifier-seq [opt] = type-id */
19360 static tree
19361 cp_parser_alias_declaration (cp_parser* parser)
19363 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19364 location_t id_location, type_location;
19365 cp_declarator *declarator;
19366 cp_decl_specifier_seq decl_specs;
19367 bool member_p;
19368 const char *saved_message = NULL;
19370 /* Look for the `using' keyword. */
19371 cp_token *using_token
19372 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19373 if (using_token == NULL)
19374 return error_mark_node;
19376 id_location = cp_lexer_peek_token (parser->lexer)->location;
19377 id = cp_parser_identifier (parser);
19378 if (id == error_mark_node)
19379 return error_mark_node;
19381 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19382 attributes = cp_parser_attributes_opt (parser);
19383 if (attributes == error_mark_node)
19384 return error_mark_node;
19386 cp_parser_require (parser, CPP_EQ, RT_EQ);
19388 if (cp_parser_error_occurred (parser))
19389 return error_mark_node;
19391 cp_parser_commit_to_tentative_parse (parser);
19393 /* Now we are going to parse the type-id of the declaration. */
19396 [dcl.type]/3 says:
19398 "A type-specifier-seq shall not define a class or enumeration
19399 unless it appears in the type-id of an alias-declaration (7.1.3) that
19400 is not the declaration of a template-declaration."
19402 In other words, if we currently are in an alias template, the
19403 type-id should not define a type.
19405 So let's set parser->type_definition_forbidden_message in that
19406 case; cp_parser_check_type_definition (called by
19407 cp_parser_class_specifier) will then emit an error if a type is
19408 defined in the type-id. */
19409 if (parser->num_template_parameter_lists)
19411 saved_message = parser->type_definition_forbidden_message;
19412 parser->type_definition_forbidden_message =
19413 G_("types may not be defined in alias template declarations");
19416 type = cp_parser_type_id (parser, &type_location);
19418 /* Restore the error message if need be. */
19419 if (parser->num_template_parameter_lists)
19420 parser->type_definition_forbidden_message = saved_message;
19422 if (type == error_mark_node
19423 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19425 cp_parser_skip_to_end_of_block_or_statement (parser);
19426 return error_mark_node;
19429 /* A typedef-name can also be introduced by an alias-declaration. The
19430 identifier following the using keyword becomes a typedef-name. It has
19431 the same semantics as if it were introduced by the typedef
19432 specifier. In particular, it does not define a new type and it shall
19433 not appear in the type-id. */
19435 clear_decl_specs (&decl_specs);
19436 decl_specs.type = type;
19437 if (attributes != NULL_TREE)
19439 decl_specs.attributes = attributes;
19440 set_and_check_decl_spec_loc (&decl_specs,
19441 ds_attribute,
19442 attrs_token);
19444 set_and_check_decl_spec_loc (&decl_specs,
19445 ds_typedef,
19446 using_token);
19447 set_and_check_decl_spec_loc (&decl_specs,
19448 ds_alias,
19449 using_token);
19450 decl_specs.locations[ds_type_spec] = type_location;
19452 if (parser->num_template_parameter_lists
19453 && !cp_parser_check_template_parameters (parser,
19454 /*num_templates=*/0,
19455 /*template_id*/false,
19456 id_location,
19457 /*declarator=*/NULL))
19458 return error_mark_node;
19460 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
19462 member_p = at_class_scope_p ();
19463 if (member_p)
19464 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19465 NULL_TREE, attributes);
19466 else
19467 decl = start_decl (declarator, &decl_specs, 0,
19468 attributes, NULL_TREE, &pushed_scope);
19469 if (decl == error_mark_node)
19470 return decl;
19472 // Attach constraints to the alias declaration.
19473 if (flag_concepts && current_template_parms)
19475 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19476 tree constr = build_constraints (reqs, NULL_TREE);
19477 set_constraints (decl, constr);
19480 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19482 if (pushed_scope)
19483 pop_scope (pushed_scope);
19485 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19486 added into the symbol table; otherwise, return the TYPE_DECL. */
19487 if (DECL_LANG_SPECIFIC (decl)
19488 && DECL_TEMPLATE_INFO (decl)
19489 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19491 decl = DECL_TI_TEMPLATE (decl);
19492 if (member_p)
19493 check_member_template (decl);
19496 return decl;
19499 /* Parse a using-directive.
19501 using-directive:
19502 using namespace :: [opt] nested-name-specifier [opt]
19503 namespace-name ; */
19505 static void
19506 cp_parser_using_directive (cp_parser* parser)
19508 tree namespace_decl;
19509 tree attribs;
19511 /* Look for the `using' keyword. */
19512 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19513 /* And the `namespace' keyword. */
19514 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19515 /* Look for the optional `::' operator. */
19516 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19517 /* And the optional nested-name-specifier. */
19518 cp_parser_nested_name_specifier_opt (parser,
19519 /*typename_keyword_p=*/false,
19520 /*check_dependency_p=*/true,
19521 /*type_p=*/false,
19522 /*is_declaration=*/true);
19523 /* Get the namespace being used. */
19524 namespace_decl = cp_parser_namespace_name (parser);
19525 /* And any specified attributes. */
19526 attribs = cp_parser_attributes_opt (parser);
19528 /* Update the symbol table. */
19529 if (namespace_bindings_p ())
19530 finish_namespace_using_directive (namespace_decl, attribs);
19531 else
19532 finish_local_using_directive (namespace_decl, attribs);
19534 /* Look for the final `;'. */
19535 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19538 /* Parse an asm-definition.
19540 asm-definition:
19541 asm ( string-literal ) ;
19543 GNU Extension:
19545 asm-definition:
19546 asm volatile [opt] ( string-literal ) ;
19547 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19548 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19549 : asm-operand-list [opt] ) ;
19550 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19551 : asm-operand-list [opt]
19552 : asm-clobber-list [opt] ) ;
19553 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19554 : asm-clobber-list [opt]
19555 : asm-goto-list ) ; */
19557 static void
19558 cp_parser_asm_definition (cp_parser* parser)
19560 tree string;
19561 tree outputs = NULL_TREE;
19562 tree inputs = NULL_TREE;
19563 tree clobbers = NULL_TREE;
19564 tree labels = NULL_TREE;
19565 tree asm_stmt;
19566 bool volatile_p = false;
19567 bool extended_p = false;
19568 bool invalid_inputs_p = false;
19569 bool invalid_outputs_p = false;
19570 bool goto_p = false;
19571 required_token missing = RT_NONE;
19573 /* Look for the `asm' keyword. */
19574 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19576 if (parser->in_function_body
19577 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19579 error ("%<asm%> in %<constexpr%> function");
19580 cp_function_chain->invalid_constexpr = true;
19583 /* See if the next token is `volatile'. */
19584 if (cp_parser_allow_gnu_extensions_p (parser)
19585 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19587 /* Remember that we saw the `volatile' keyword. */
19588 volatile_p = true;
19589 /* Consume the token. */
19590 cp_lexer_consume_token (parser->lexer);
19592 if (cp_parser_allow_gnu_extensions_p (parser)
19593 && parser->in_function_body
19594 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19596 /* Remember that we saw the `goto' keyword. */
19597 goto_p = true;
19598 /* Consume the token. */
19599 cp_lexer_consume_token (parser->lexer);
19601 /* Look for the opening `('. */
19602 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19603 return;
19604 /* Look for the string. */
19605 string = cp_parser_string_literal (parser, false, false);
19606 if (string == error_mark_node)
19608 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19609 /*consume_paren=*/true);
19610 return;
19613 /* If we're allowing GNU extensions, check for the extended assembly
19614 syntax. Unfortunately, the `:' tokens need not be separated by
19615 a space in C, and so, for compatibility, we tolerate that here
19616 too. Doing that means that we have to treat the `::' operator as
19617 two `:' tokens. */
19618 if (cp_parser_allow_gnu_extensions_p (parser)
19619 && parser->in_function_body
19620 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19621 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19623 bool inputs_p = false;
19624 bool clobbers_p = false;
19625 bool labels_p = false;
19627 /* The extended syntax was used. */
19628 extended_p = true;
19630 /* Look for outputs. */
19631 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19633 /* Consume the `:'. */
19634 cp_lexer_consume_token (parser->lexer);
19635 /* Parse the output-operands. */
19636 if (cp_lexer_next_token_is_not (parser->lexer,
19637 CPP_COLON)
19638 && cp_lexer_next_token_is_not (parser->lexer,
19639 CPP_SCOPE)
19640 && cp_lexer_next_token_is_not (parser->lexer,
19641 CPP_CLOSE_PAREN)
19642 && !goto_p)
19644 outputs = cp_parser_asm_operand_list (parser);
19645 if (outputs == error_mark_node)
19646 invalid_outputs_p = true;
19649 /* If the next token is `::', there are no outputs, and the
19650 next token is the beginning of the inputs. */
19651 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19652 /* The inputs are coming next. */
19653 inputs_p = true;
19655 /* Look for inputs. */
19656 if (inputs_p
19657 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19659 /* Consume the `:' or `::'. */
19660 cp_lexer_consume_token (parser->lexer);
19661 /* Parse the output-operands. */
19662 if (cp_lexer_next_token_is_not (parser->lexer,
19663 CPP_COLON)
19664 && cp_lexer_next_token_is_not (parser->lexer,
19665 CPP_SCOPE)
19666 && cp_lexer_next_token_is_not (parser->lexer,
19667 CPP_CLOSE_PAREN))
19669 inputs = cp_parser_asm_operand_list (parser);
19670 if (inputs == error_mark_node)
19671 invalid_inputs_p = true;
19674 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19675 /* The clobbers are coming next. */
19676 clobbers_p = true;
19678 /* Look for clobbers. */
19679 if (clobbers_p
19680 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19682 clobbers_p = true;
19683 /* Consume the `:' or `::'. */
19684 cp_lexer_consume_token (parser->lexer);
19685 /* Parse the clobbers. */
19686 if (cp_lexer_next_token_is_not (parser->lexer,
19687 CPP_COLON)
19688 && cp_lexer_next_token_is_not (parser->lexer,
19689 CPP_CLOSE_PAREN))
19690 clobbers = cp_parser_asm_clobber_list (parser);
19692 else if (goto_p
19693 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19694 /* The labels are coming next. */
19695 labels_p = true;
19697 /* Look for labels. */
19698 if (labels_p
19699 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19701 labels_p = true;
19702 /* Consume the `:' or `::'. */
19703 cp_lexer_consume_token (parser->lexer);
19704 /* Parse the labels. */
19705 labels = cp_parser_asm_label_list (parser);
19708 if (goto_p && !labels_p)
19709 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19711 else if (goto_p)
19712 missing = RT_COLON_SCOPE;
19714 /* Look for the closing `)'. */
19715 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19716 missing ? missing : RT_CLOSE_PAREN))
19717 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19718 /*consume_paren=*/true);
19719 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19721 if (!invalid_inputs_p && !invalid_outputs_p)
19723 /* Create the ASM_EXPR. */
19724 if (parser->in_function_body)
19726 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19727 inputs, clobbers, labels);
19728 /* If the extended syntax was not used, mark the ASM_EXPR. */
19729 if (!extended_p)
19731 tree temp = asm_stmt;
19732 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19733 temp = TREE_OPERAND (temp, 0);
19735 ASM_INPUT_P (temp) = 1;
19738 else
19739 symtab->finalize_toplevel_asm (string);
19743 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19744 type that comes from the decl-specifier-seq. */
19746 static tree
19747 strip_declarator_types (tree type, cp_declarator *declarator)
19749 for (cp_declarator *d = declarator; d;)
19750 switch (d->kind)
19752 case cdk_id:
19753 case cdk_decomp:
19754 case cdk_error:
19755 d = NULL;
19756 break;
19758 default:
19759 if (TYPE_PTRMEMFUNC_P (type))
19760 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19761 type = TREE_TYPE (type);
19762 d = d->declarator;
19763 break;
19766 return type;
19769 /* Declarators [gram.dcl.decl] */
19771 /* Parse an init-declarator.
19773 init-declarator:
19774 declarator initializer [opt]
19776 GNU Extension:
19778 init-declarator:
19779 declarator asm-specification [opt] attributes [opt] initializer [opt]
19781 function-definition:
19782 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19783 function-body
19784 decl-specifier-seq [opt] declarator function-try-block
19786 GNU Extension:
19788 function-definition:
19789 __extension__ function-definition
19791 TM Extension:
19793 function-definition:
19794 decl-specifier-seq [opt] declarator function-transaction-block
19796 The DECL_SPECIFIERS apply to this declarator. Returns a
19797 representation of the entity declared. If MEMBER_P is TRUE, then
19798 this declarator appears in a class scope. The new DECL created by
19799 this declarator is returned.
19801 The CHECKS are access checks that should be performed once we know
19802 what entity is being declared (and, therefore, what classes have
19803 befriended it).
19805 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19806 for a function-definition here as well. If the declarator is a
19807 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19808 be TRUE upon return. By that point, the function-definition will
19809 have been completely parsed.
19811 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19812 is FALSE.
19814 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19815 parsed declaration if it is an uninitialized single declarator not followed
19816 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19817 if present, will not be consumed. If returned, this declarator will be
19818 created with SD_INITIALIZED but will not call cp_finish_decl.
19820 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19821 and there is an initializer, the pointed location_t is set to the
19822 location of the '=' or `(', or '{' in C++11 token introducing the
19823 initializer. */
19825 static tree
19826 cp_parser_init_declarator (cp_parser* parser,
19827 cp_decl_specifier_seq *decl_specifiers,
19828 vec<deferred_access_check, va_gc> *checks,
19829 bool function_definition_allowed_p,
19830 bool member_p,
19831 int declares_class_or_enum,
19832 bool* function_definition_p,
19833 tree* maybe_range_for_decl,
19834 location_t* init_loc,
19835 tree* auto_result)
19837 cp_token *token = NULL, *asm_spec_start_token = NULL,
19838 *attributes_start_token = NULL;
19839 cp_declarator *declarator;
19840 tree prefix_attributes;
19841 tree attributes = NULL;
19842 tree asm_specification;
19843 tree initializer;
19844 tree decl = NULL_TREE;
19845 tree scope;
19846 int is_initialized;
19847 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19848 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19849 "(...)". */
19850 enum cpp_ttype initialization_kind;
19851 bool is_direct_init = false;
19852 bool is_non_constant_init;
19853 int ctor_dtor_or_conv_p;
19854 bool friend_p = cp_parser_friend_p (decl_specifiers);
19855 tree pushed_scope = NULL_TREE;
19856 bool range_for_decl_p = false;
19857 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19858 location_t tmp_init_loc = UNKNOWN_LOCATION;
19860 /* Gather the attributes that were provided with the
19861 decl-specifiers. */
19862 prefix_attributes = decl_specifiers->attributes;
19864 /* Assume that this is not the declarator for a function
19865 definition. */
19866 if (function_definition_p)
19867 *function_definition_p = false;
19869 /* Default arguments are only permitted for function parameters. */
19870 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19871 parser->default_arg_ok_p = false;
19873 /* Defer access checks while parsing the declarator; we cannot know
19874 what names are accessible until we know what is being
19875 declared. */
19876 resume_deferring_access_checks ();
19878 token = cp_lexer_peek_token (parser->lexer);
19880 /* Parse the declarator. */
19881 declarator
19882 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19883 &ctor_dtor_or_conv_p,
19884 /*parenthesized_p=*/NULL,
19885 member_p, friend_p);
19886 /* Gather up the deferred checks. */
19887 stop_deferring_access_checks ();
19889 parser->default_arg_ok_p = saved_default_arg_ok_p;
19891 /* If the DECLARATOR was erroneous, there's no need to go
19892 further. */
19893 if (declarator == cp_error_declarator)
19894 return error_mark_node;
19896 /* Check that the number of template-parameter-lists is OK. */
19897 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19898 token->location))
19899 return error_mark_node;
19901 if (declares_class_or_enum & 2)
19902 cp_parser_check_for_definition_in_return_type (declarator,
19903 decl_specifiers->type,
19904 decl_specifiers->locations[ds_type_spec]);
19906 /* Figure out what scope the entity declared by the DECLARATOR is
19907 located in. `grokdeclarator' sometimes changes the scope, so
19908 we compute it now. */
19909 scope = get_scope_of_declarator (declarator);
19911 /* Perform any lookups in the declared type which were thought to be
19912 dependent, but are not in the scope of the declarator. */
19913 decl_specifiers->type
19914 = maybe_update_decl_type (decl_specifiers->type, scope);
19916 /* If we're allowing GNU extensions, look for an
19917 asm-specification. */
19918 if (cp_parser_allow_gnu_extensions_p (parser))
19920 /* Look for an asm-specification. */
19921 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19922 asm_specification = cp_parser_asm_specification_opt (parser);
19924 else
19925 asm_specification = NULL_TREE;
19927 /* Look for attributes. */
19928 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19929 attributes = cp_parser_attributes_opt (parser);
19931 /* Peek at the next token. */
19932 token = cp_lexer_peek_token (parser->lexer);
19934 bool bogus_implicit_tmpl = false;
19936 if (function_declarator_p (declarator))
19938 /* Handle C++17 deduction guides. */
19939 if (!decl_specifiers->type
19940 && ctor_dtor_or_conv_p <= 0
19941 && cxx_dialect >= cxx17)
19943 cp_declarator *id = get_id_declarator (declarator);
19944 tree name = id->u.id.unqualified_name;
19945 parser->scope = id->u.id.qualifying_scope;
19946 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19947 if (tmpl
19948 && (DECL_CLASS_TEMPLATE_P (tmpl)
19949 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19951 id->u.id.unqualified_name = dguide_name (tmpl);
19952 id->u.id.sfk = sfk_deduction_guide;
19953 ctor_dtor_or_conv_p = 1;
19957 /* Check to see if the token indicates the start of a
19958 function-definition. */
19959 if (cp_parser_token_starts_function_definition_p (token))
19961 if (!function_definition_allowed_p)
19963 /* If a function-definition should not appear here, issue an
19964 error message. */
19965 cp_parser_error (parser,
19966 "a function-definition is not allowed here");
19967 return error_mark_node;
19970 location_t func_brace_location
19971 = cp_lexer_peek_token (parser->lexer)->location;
19973 /* Neither attributes nor an asm-specification are allowed
19974 on a function-definition. */
19975 if (asm_specification)
19976 error_at (asm_spec_start_token->location,
19977 "an asm-specification is not allowed "
19978 "on a function-definition");
19979 if (attributes)
19980 error_at (attributes_start_token->location,
19981 "attributes are not allowed "
19982 "on a function-definition");
19983 /* This is a function-definition. */
19984 *function_definition_p = true;
19986 /* Parse the function definition. */
19987 if (member_p)
19988 decl = cp_parser_save_member_function_body (parser,
19989 decl_specifiers,
19990 declarator,
19991 prefix_attributes);
19992 else
19993 decl =
19994 (cp_parser_function_definition_from_specifiers_and_declarator
19995 (parser, decl_specifiers, prefix_attributes, declarator));
19997 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19999 /* This is where the prologue starts... */
20000 DECL_STRUCT_FUNCTION (decl)->function_start_locus
20001 = func_brace_location;
20004 return decl;
20007 else if (parser->fully_implicit_function_template_p)
20009 /* A non-template declaration involving a function parameter list
20010 containing an implicit template parameter will be made into a
20011 template. If the resulting declaration is not going to be an
20012 actual function then finish the template scope here to prevent it.
20013 An error message will be issued once we have a decl to talk about.
20015 FIXME probably we should do type deduction rather than create an
20016 implicit template, but the standard currently doesn't allow it. */
20017 bogus_implicit_tmpl = true;
20018 finish_fully_implicit_template (parser, NULL_TREE);
20021 /* [dcl.dcl]
20023 Only in function declarations for constructors, destructors, type
20024 conversions, and deduction guides can the decl-specifier-seq be omitted.
20026 We explicitly postpone this check past the point where we handle
20027 function-definitions because we tolerate function-definitions
20028 that are missing their return types in some modes. */
20029 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
20031 cp_parser_error (parser,
20032 "expected constructor, destructor, or type conversion");
20033 return error_mark_node;
20036 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20037 if (token->type == CPP_EQ
20038 || token->type == CPP_OPEN_PAREN
20039 || token->type == CPP_OPEN_BRACE)
20041 is_initialized = SD_INITIALIZED;
20042 initialization_kind = token->type;
20043 if (maybe_range_for_decl)
20044 *maybe_range_for_decl = error_mark_node;
20045 tmp_init_loc = token->location;
20046 if (init_loc && *init_loc == UNKNOWN_LOCATION)
20047 *init_loc = tmp_init_loc;
20049 if (token->type == CPP_EQ
20050 && function_declarator_p (declarator))
20052 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
20053 if (t2->keyword == RID_DEFAULT)
20054 is_initialized = SD_DEFAULTED;
20055 else if (t2->keyword == RID_DELETE)
20056 is_initialized = SD_DELETED;
20059 else
20061 /* If the init-declarator isn't initialized and isn't followed by a
20062 `,' or `;', it's not a valid init-declarator. */
20063 if (token->type != CPP_COMMA
20064 && token->type != CPP_SEMICOLON)
20066 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
20067 range_for_decl_p = true;
20068 else
20070 if (!maybe_range_for_decl)
20071 cp_parser_error (parser, "expected initializer");
20072 return error_mark_node;
20075 is_initialized = SD_UNINITIALIZED;
20076 initialization_kind = CPP_EOF;
20079 /* Because start_decl has side-effects, we should only call it if we
20080 know we're going ahead. By this point, we know that we cannot
20081 possibly be looking at any other construct. */
20082 cp_parser_commit_to_tentative_parse (parser);
20084 /* Enter the newly declared entry in the symbol table. If we're
20085 processing a declaration in a class-specifier, we wait until
20086 after processing the initializer. */
20087 if (!member_p)
20089 if (parser->in_unbraced_linkage_specification_p)
20090 decl_specifiers->storage_class = sc_extern;
20091 decl = start_decl (declarator, decl_specifiers,
20092 range_for_decl_p? SD_INITIALIZED : is_initialized,
20093 attributes, prefix_attributes, &pushed_scope);
20094 cp_finalize_omp_declare_simd (parser, decl);
20095 cp_finalize_oacc_routine (parser, decl, false);
20096 /* Adjust location of decl if declarator->id_loc is more appropriate:
20097 set, and decl wasn't merged with another decl, in which case its
20098 location would be different from input_location, and more accurate. */
20099 if (DECL_P (decl)
20100 && declarator->id_loc != UNKNOWN_LOCATION
20101 && DECL_SOURCE_LOCATION (decl) == input_location)
20102 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
20104 else if (scope)
20105 /* Enter the SCOPE. That way unqualified names appearing in the
20106 initializer will be looked up in SCOPE. */
20107 pushed_scope = push_scope (scope);
20109 /* Perform deferred access control checks, now that we know in which
20110 SCOPE the declared entity resides. */
20111 if (!member_p && decl)
20113 tree saved_current_function_decl = NULL_TREE;
20115 /* If the entity being declared is a function, pretend that we
20116 are in its scope. If it is a `friend', it may have access to
20117 things that would not otherwise be accessible. */
20118 if (TREE_CODE (decl) == FUNCTION_DECL)
20120 saved_current_function_decl = current_function_decl;
20121 current_function_decl = decl;
20124 /* Perform access checks for template parameters. */
20125 cp_parser_perform_template_parameter_access_checks (checks);
20127 /* Perform the access control checks for the declarator and the
20128 decl-specifiers. */
20129 perform_deferred_access_checks (tf_warning_or_error);
20131 /* Restore the saved value. */
20132 if (TREE_CODE (decl) == FUNCTION_DECL)
20133 current_function_decl = saved_current_function_decl;
20136 /* Parse the initializer. */
20137 initializer = NULL_TREE;
20138 is_direct_init = false;
20139 is_non_constant_init = true;
20140 if (is_initialized)
20142 if (function_declarator_p (declarator))
20144 if (initialization_kind == CPP_EQ)
20145 initializer = cp_parser_pure_specifier (parser);
20146 else
20148 /* If the declaration was erroneous, we don't really
20149 know what the user intended, so just silently
20150 consume the initializer. */
20151 if (decl != error_mark_node)
20152 error_at (tmp_init_loc, "initializer provided for function");
20153 cp_parser_skip_to_closing_parenthesis (parser,
20154 /*recovering=*/true,
20155 /*or_comma=*/false,
20156 /*consume_paren=*/true);
20159 else
20161 /* We want to record the extra mangling scope for in-class
20162 initializers of class members and initializers of static data
20163 member templates. The former involves deferring
20164 parsing of the initializer until end of class as with default
20165 arguments. So right here we only handle the latter. */
20166 if (!member_p && processing_template_decl && decl != error_mark_node)
20167 start_lambda_scope (decl);
20168 initializer = cp_parser_initializer (parser,
20169 &is_direct_init,
20170 &is_non_constant_init);
20171 if (!member_p && processing_template_decl && decl != error_mark_node)
20172 finish_lambda_scope ();
20173 if (initializer == error_mark_node)
20174 cp_parser_skip_to_end_of_statement (parser);
20178 /* The old parser allows attributes to appear after a parenthesized
20179 initializer. Mark Mitchell proposed removing this functionality
20180 on the GCC mailing lists on 2002-08-13. This parser accepts the
20181 attributes -- but ignores them. Made a permerror in GCC 8. */
20182 if (cp_parser_allow_gnu_extensions_p (parser)
20183 && initialization_kind == CPP_OPEN_PAREN
20184 && cp_parser_attributes_opt (parser)
20185 && permerror (input_location,
20186 "attributes after parenthesized initializer ignored"))
20188 static bool hint;
20189 if (flag_permissive && !hint)
20191 hint = true;
20192 inform (input_location,
20193 "this flexibility is deprecated and will be removed");
20197 /* And now complain about a non-function implicit template. */
20198 if (bogus_implicit_tmpl && decl != error_mark_node)
20199 error_at (DECL_SOURCE_LOCATION (decl),
20200 "non-function %qD declared as implicit template", decl);
20202 /* For an in-class declaration, use `grokfield' to create the
20203 declaration. */
20204 if (member_p)
20206 if (pushed_scope)
20208 pop_scope (pushed_scope);
20209 pushed_scope = NULL_TREE;
20211 decl = grokfield (declarator, decl_specifiers,
20212 initializer, !is_non_constant_init,
20213 /*asmspec=*/NULL_TREE,
20214 attr_chainon (attributes, prefix_attributes));
20215 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20216 cp_parser_save_default_args (parser, decl);
20217 cp_finalize_omp_declare_simd (parser, decl);
20218 cp_finalize_oacc_routine (parser, decl, false);
20221 /* Finish processing the declaration. But, skip member
20222 declarations. */
20223 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20225 cp_finish_decl (decl,
20226 initializer, !is_non_constant_init,
20227 asm_specification,
20228 /* If the initializer is in parentheses, then this is
20229 a direct-initialization, which means that an
20230 `explicit' constructor is OK. Otherwise, an
20231 `explicit' constructor cannot be used. */
20232 ((is_direct_init || !is_initialized)
20233 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20235 else if ((cxx_dialect != cxx98) && friend_p
20236 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20237 /* Core issue #226 (C++0x only): A default template-argument
20238 shall not be specified in a friend class template
20239 declaration. */
20240 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20241 /*is_partial=*/false, /*is_friend_decl=*/1);
20243 if (!friend_p && pushed_scope)
20244 pop_scope (pushed_scope);
20246 if (function_declarator_p (declarator)
20247 && parser->fully_implicit_function_template_p)
20249 if (member_p)
20250 decl = finish_fully_implicit_template (parser, decl);
20251 else
20252 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20255 if (auto_result && is_initialized && decl_specifiers->type
20256 && type_uses_auto (decl_specifiers->type))
20257 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20259 return decl;
20262 /* Parse a declarator.
20264 declarator:
20265 direct-declarator
20266 ptr-operator declarator
20268 abstract-declarator:
20269 ptr-operator abstract-declarator [opt]
20270 direct-abstract-declarator
20272 GNU Extensions:
20274 declarator:
20275 attributes [opt] direct-declarator
20276 attributes [opt] ptr-operator declarator
20278 abstract-declarator:
20279 attributes [opt] ptr-operator abstract-declarator [opt]
20280 attributes [opt] direct-abstract-declarator
20282 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20283 detect constructors, destructors, deduction guides, or conversion operators.
20284 It is set to -1 if the declarator is a name, and +1 if it is a
20285 function. Otherwise it is set to zero. Usually you just want to
20286 test for >0, but internally the negative value is used.
20288 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20289 a decl-specifier-seq unless it declares a constructor, destructor,
20290 or conversion. It might seem that we could check this condition in
20291 semantic analysis, rather than parsing, but that makes it difficult
20292 to handle something like `f()'. We want to notice that there are
20293 no decl-specifiers, and therefore realize that this is an
20294 expression, not a declaration.)
20296 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20297 the declarator is a direct-declarator of the form "(...)".
20299 MEMBER_P is true iff this declarator is a member-declarator.
20301 FRIEND_P is true iff this declarator is a friend. */
20303 static cp_declarator *
20304 cp_parser_declarator (cp_parser* parser,
20305 cp_parser_declarator_kind dcl_kind,
20306 int* ctor_dtor_or_conv_p,
20307 bool* parenthesized_p,
20308 bool member_p, bool friend_p)
20310 cp_declarator *declarator;
20311 enum tree_code code;
20312 cp_cv_quals cv_quals;
20313 tree class_type;
20314 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20316 /* Assume this is not a constructor, destructor, or type-conversion
20317 operator. */
20318 if (ctor_dtor_or_conv_p)
20319 *ctor_dtor_or_conv_p = 0;
20321 if (cp_parser_allow_gnu_extensions_p (parser))
20322 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20324 /* Check for the ptr-operator production. */
20325 cp_parser_parse_tentatively (parser);
20326 /* Parse the ptr-operator. */
20327 code = cp_parser_ptr_operator (parser,
20328 &class_type,
20329 &cv_quals,
20330 &std_attributes);
20332 /* If that worked, then we have a ptr-operator. */
20333 if (cp_parser_parse_definitely (parser))
20335 /* If a ptr-operator was found, then this declarator was not
20336 parenthesized. */
20337 if (parenthesized_p)
20338 *parenthesized_p = true;
20339 /* The dependent declarator is optional if we are parsing an
20340 abstract-declarator. */
20341 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20342 cp_parser_parse_tentatively (parser);
20344 /* Parse the dependent declarator. */
20345 declarator = cp_parser_declarator (parser, dcl_kind,
20346 /*ctor_dtor_or_conv_p=*/NULL,
20347 /*parenthesized_p=*/NULL,
20348 /*member_p=*/false,
20349 friend_p);
20351 /* If we are parsing an abstract-declarator, we must handle the
20352 case where the dependent declarator is absent. */
20353 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20354 && !cp_parser_parse_definitely (parser))
20355 declarator = NULL;
20357 declarator = cp_parser_make_indirect_declarator
20358 (code, class_type, cv_quals, declarator, std_attributes);
20360 /* Everything else is a direct-declarator. */
20361 else
20363 if (parenthesized_p)
20364 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20365 CPP_OPEN_PAREN);
20366 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20367 ctor_dtor_or_conv_p,
20368 member_p, friend_p);
20371 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20372 declarator->attributes = gnu_attributes;
20373 return declarator;
20376 /* Parse a direct-declarator or direct-abstract-declarator.
20378 direct-declarator:
20379 declarator-id
20380 direct-declarator ( parameter-declaration-clause )
20381 cv-qualifier-seq [opt]
20382 ref-qualifier [opt]
20383 exception-specification [opt]
20384 direct-declarator [ constant-expression [opt] ]
20385 ( declarator )
20387 direct-abstract-declarator:
20388 direct-abstract-declarator [opt]
20389 ( parameter-declaration-clause )
20390 cv-qualifier-seq [opt]
20391 ref-qualifier [opt]
20392 exception-specification [opt]
20393 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20394 ( abstract-declarator )
20396 Returns a representation of the declarator. DCL_KIND is
20397 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20398 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20399 we are parsing a direct-declarator. It is
20400 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20401 of ambiguity we prefer an abstract declarator, as per
20402 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20403 as for cp_parser_declarator. */
20405 static cp_declarator *
20406 cp_parser_direct_declarator (cp_parser* parser,
20407 cp_parser_declarator_kind dcl_kind,
20408 int* ctor_dtor_or_conv_p,
20409 bool member_p, bool friend_p)
20411 cp_token *token;
20412 cp_declarator *declarator = NULL;
20413 tree scope = NULL_TREE;
20414 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20415 bool saved_in_declarator_p = parser->in_declarator_p;
20416 bool first = true;
20417 tree pushed_scope = NULL_TREE;
20418 cp_token *open_paren = NULL, *close_paren = NULL;
20420 while (true)
20422 /* Peek at the next token. */
20423 token = cp_lexer_peek_token (parser->lexer);
20424 if (token->type == CPP_OPEN_PAREN)
20426 /* This is either a parameter-declaration-clause, or a
20427 parenthesized declarator. When we know we are parsing a
20428 named declarator, it must be a parenthesized declarator
20429 if FIRST is true. For instance, `(int)' is a
20430 parameter-declaration-clause, with an omitted
20431 direct-abstract-declarator. But `((*))', is a
20432 parenthesized abstract declarator. Finally, when T is a
20433 template parameter `(T)' is a
20434 parameter-declaration-clause, and not a parenthesized
20435 named declarator.
20437 We first try and parse a parameter-declaration-clause,
20438 and then try a nested declarator (if FIRST is true).
20440 It is not an error for it not to be a
20441 parameter-declaration-clause, even when FIRST is
20442 false. Consider,
20444 int i (int);
20445 int i (3);
20447 The first is the declaration of a function while the
20448 second is the definition of a variable, including its
20449 initializer.
20451 Having seen only the parenthesis, we cannot know which of
20452 these two alternatives should be selected. Even more
20453 complex are examples like:
20455 int i (int (a));
20456 int i (int (3));
20458 The former is a function-declaration; the latter is a
20459 variable initialization.
20461 Thus again, we try a parameter-declaration-clause, and if
20462 that fails, we back out and return. */
20464 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20466 tree params;
20467 bool is_declarator = false;
20469 open_paren = NULL;
20471 /* In a member-declarator, the only valid interpretation
20472 of a parenthesis is the start of a
20473 parameter-declaration-clause. (It is invalid to
20474 initialize a static data member with a parenthesized
20475 initializer; only the "=" form of initialization is
20476 permitted.) */
20477 if (!member_p)
20478 cp_parser_parse_tentatively (parser);
20480 /* Consume the `('. */
20481 matching_parens parens;
20482 parens.consume_open (parser);
20483 if (first)
20485 /* If this is going to be an abstract declarator, we're
20486 in a declarator and we can't have default args. */
20487 parser->default_arg_ok_p = false;
20488 parser->in_declarator_p = true;
20491 begin_scope (sk_function_parms, NULL_TREE);
20493 /* Parse the parameter-declaration-clause. */
20494 params = cp_parser_parameter_declaration_clause (parser);
20496 /* Consume the `)'. */
20497 parens.require_close (parser);
20499 /* If all went well, parse the cv-qualifier-seq,
20500 ref-qualifier and the exception-specification. */
20501 if (member_p || cp_parser_parse_definitely (parser))
20503 cp_cv_quals cv_quals;
20504 cp_virt_specifiers virt_specifiers;
20505 cp_ref_qualifier ref_qual;
20506 tree exception_specification;
20507 tree late_return;
20508 tree attrs;
20509 bool memfn = (member_p || (pushed_scope
20510 && CLASS_TYPE_P (pushed_scope)));
20512 is_declarator = true;
20514 if (ctor_dtor_or_conv_p)
20515 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20516 first = false;
20518 /* Parse the cv-qualifier-seq. */
20519 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20520 /* Parse the ref-qualifier. */
20521 ref_qual = cp_parser_ref_qualifier_opt (parser);
20522 /* Parse the tx-qualifier. */
20523 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20524 /* And the exception-specification. */
20525 exception_specification
20526 = cp_parser_exception_specification_opt (parser);
20528 attrs = cp_parser_std_attribute_spec_seq (parser);
20530 /* In here, we handle cases where attribute is used after
20531 the function declaration. For example:
20532 void func (int x) __attribute__((vector(..))); */
20533 tree gnu_attrs = NULL_TREE;
20534 tree requires_clause = NULL_TREE;
20535 late_return = (cp_parser_late_return_type_opt
20536 (parser, declarator, requires_clause,
20537 memfn ? cv_quals : -1));
20539 /* Parse the virt-specifier-seq. */
20540 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20542 /* Create the function-declarator. */
20543 declarator = make_call_declarator (declarator,
20544 params,
20545 cv_quals,
20546 virt_specifiers,
20547 ref_qual,
20548 tx_qual,
20549 exception_specification,
20550 late_return,
20551 requires_clause);
20552 declarator->std_attributes = attrs;
20553 declarator->attributes = gnu_attrs;
20554 /* Any subsequent parameter lists are to do with
20555 return type, so are not those of the declared
20556 function. */
20557 parser->default_arg_ok_p = false;
20560 /* Remove the function parms from scope. */
20561 pop_bindings_and_leave_scope ();
20563 if (is_declarator)
20564 /* Repeat the main loop. */
20565 continue;
20568 /* If this is the first, we can try a parenthesized
20569 declarator. */
20570 if (first)
20572 bool saved_in_type_id_in_expr_p;
20574 parser->default_arg_ok_p = saved_default_arg_ok_p;
20575 parser->in_declarator_p = saved_in_declarator_p;
20577 open_paren = token;
20578 /* Consume the `('. */
20579 matching_parens parens;
20580 parens.consume_open (parser);
20581 /* Parse the nested declarator. */
20582 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20583 parser->in_type_id_in_expr_p = true;
20584 declarator
20585 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20586 /*parenthesized_p=*/NULL,
20587 member_p, friend_p);
20588 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20589 first = false;
20590 /* Expect a `)'. */
20591 close_paren = cp_lexer_peek_token (parser->lexer);
20592 if (!parens.require_close (parser))
20593 declarator = cp_error_declarator;
20594 if (declarator == cp_error_declarator)
20595 break;
20597 goto handle_declarator;
20599 /* Otherwise, we must be done. */
20600 else
20601 break;
20603 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20604 && token->type == CPP_OPEN_SQUARE
20605 && !cp_next_tokens_can_be_attribute_p (parser))
20607 /* Parse an array-declarator. */
20608 tree bounds, attrs;
20610 if (ctor_dtor_or_conv_p)
20611 *ctor_dtor_or_conv_p = 0;
20613 open_paren = NULL;
20614 first = false;
20615 parser->default_arg_ok_p = false;
20616 parser->in_declarator_p = true;
20617 /* Consume the `['. */
20618 cp_lexer_consume_token (parser->lexer);
20619 /* Peek at the next token. */
20620 token = cp_lexer_peek_token (parser->lexer);
20621 /* If the next token is `]', then there is no
20622 constant-expression. */
20623 if (token->type != CPP_CLOSE_SQUARE)
20625 bool non_constant_p;
20626 bounds
20627 = cp_parser_constant_expression (parser,
20628 /*allow_non_constant=*/true,
20629 &non_constant_p);
20630 if (!non_constant_p)
20631 /* OK */;
20632 else if (error_operand_p (bounds))
20633 /* Already gave an error. */;
20634 else if (!parser->in_function_body
20635 || current_binding_level->kind == sk_function_parms)
20637 /* Normally, the array bound must be an integral constant
20638 expression. However, as an extension, we allow VLAs
20639 in function scopes as long as they aren't part of a
20640 parameter declaration. */
20641 cp_parser_error (parser,
20642 "array bound is not an integer constant");
20643 bounds = error_mark_node;
20645 else if (processing_template_decl
20646 && !type_dependent_expression_p (bounds))
20648 /* Remember this wasn't a constant-expression. */
20649 bounds = build_nop (TREE_TYPE (bounds), bounds);
20650 TREE_SIDE_EFFECTS (bounds) = 1;
20653 else
20654 bounds = NULL_TREE;
20655 /* Look for the closing `]'. */
20656 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20658 declarator = cp_error_declarator;
20659 break;
20662 attrs = cp_parser_std_attribute_spec_seq (parser);
20663 declarator = make_array_declarator (declarator, bounds);
20664 declarator->std_attributes = attrs;
20666 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20669 tree qualifying_scope;
20670 tree unqualified_name;
20671 tree attrs;
20672 special_function_kind sfk;
20673 bool abstract_ok;
20674 bool pack_expansion_p = false;
20675 cp_token *declarator_id_start_token;
20677 /* Parse a declarator-id */
20678 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20679 if (abstract_ok)
20681 cp_parser_parse_tentatively (parser);
20683 /* If we see an ellipsis, we should be looking at a
20684 parameter pack. */
20685 if (token->type == CPP_ELLIPSIS)
20687 /* Consume the `...' */
20688 cp_lexer_consume_token (parser->lexer);
20690 pack_expansion_p = true;
20694 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20695 unqualified_name
20696 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20697 qualifying_scope = parser->scope;
20698 if (abstract_ok)
20700 bool okay = false;
20702 if (!unqualified_name && pack_expansion_p)
20704 /* Check whether an error occurred. */
20705 okay = !cp_parser_error_occurred (parser);
20707 /* We already consumed the ellipsis to mark a
20708 parameter pack, but we have no way to report it,
20709 so abort the tentative parse. We will be exiting
20710 immediately anyway. */
20711 cp_parser_abort_tentative_parse (parser);
20713 else
20714 okay = cp_parser_parse_definitely (parser);
20716 if (!okay)
20717 unqualified_name = error_mark_node;
20718 else if (unqualified_name
20719 && (qualifying_scope
20720 || (!identifier_p (unqualified_name))))
20722 cp_parser_error (parser, "expected unqualified-id");
20723 unqualified_name = error_mark_node;
20727 if (!unqualified_name)
20728 return NULL;
20729 if (unqualified_name == error_mark_node)
20731 declarator = cp_error_declarator;
20732 pack_expansion_p = false;
20733 declarator->parameter_pack_p = false;
20734 break;
20737 attrs = cp_parser_std_attribute_spec_seq (parser);
20739 if (qualifying_scope && at_namespace_scope_p ()
20740 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20742 /* In the declaration of a member of a template class
20743 outside of the class itself, the SCOPE will sometimes
20744 be a TYPENAME_TYPE. For example, given:
20746 template <typename T>
20747 int S<T>::R::i = 3;
20749 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20750 this context, we must resolve S<T>::R to an ordinary
20751 type, rather than a typename type.
20753 The reason we normally avoid resolving TYPENAME_TYPEs
20754 is that a specialization of `S' might render
20755 `S<T>::R' not a type. However, if `S' is
20756 specialized, then this `i' will not be used, so there
20757 is no harm in resolving the types here. */
20758 tree type;
20760 /* Resolve the TYPENAME_TYPE. */
20761 type = resolve_typename_type (qualifying_scope,
20762 /*only_current_p=*/false);
20763 /* If that failed, the declarator is invalid. */
20764 if (TREE_CODE (type) == TYPENAME_TYPE)
20766 if (typedef_variant_p (type))
20767 error_at (declarator_id_start_token->location,
20768 "cannot define member of dependent typedef "
20769 "%qT", type);
20770 else
20771 error_at (declarator_id_start_token->location,
20772 "%<%T::%E%> is not a type",
20773 TYPE_CONTEXT (qualifying_scope),
20774 TYPE_IDENTIFIER (qualifying_scope));
20776 qualifying_scope = type;
20779 sfk = sfk_none;
20781 if (unqualified_name)
20783 tree class_type;
20785 if (qualifying_scope
20786 && CLASS_TYPE_P (qualifying_scope))
20787 class_type = qualifying_scope;
20788 else
20789 class_type = current_class_type;
20791 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20793 tree name_type = TREE_TYPE (unqualified_name);
20795 if (!class_type || !same_type_p (name_type, class_type))
20797 /* We do not attempt to print the declarator
20798 here because we do not have enough
20799 information about its original syntactic
20800 form. */
20801 cp_parser_error (parser, "invalid declarator");
20802 declarator = cp_error_declarator;
20803 break;
20805 else if (qualifying_scope
20806 && CLASSTYPE_USE_TEMPLATE (name_type))
20808 error_at (declarator_id_start_token->location,
20809 "invalid use of constructor as a template");
20810 inform (declarator_id_start_token->location,
20811 "use %<%T::%D%> instead of %<%T::%D%> to "
20812 "name the constructor in a qualified name",
20813 class_type,
20814 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20815 class_type, name_type);
20816 declarator = cp_error_declarator;
20817 break;
20819 unqualified_name = constructor_name (class_type);
20822 if (class_type)
20824 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20825 sfk = sfk_destructor;
20826 else if (identifier_p (unqualified_name)
20827 && IDENTIFIER_CONV_OP_P (unqualified_name))
20828 sfk = sfk_conversion;
20829 else if (/* There's no way to declare a constructor
20830 for an unnamed type, even if the type
20831 got a name for linkage purposes. */
20832 !TYPE_WAS_UNNAMED (class_type)
20833 /* Handle correctly (c++/19200):
20835 struct S {
20836 struct T{};
20837 friend void S(T);
20840 and also:
20842 namespace N {
20843 void S();
20846 struct S {
20847 friend void N::S();
20848 }; */
20849 && (!friend_p || class_type == qualifying_scope)
20850 && constructor_name_p (unqualified_name,
20851 class_type))
20852 sfk = sfk_constructor;
20853 else if (is_overloaded_fn (unqualified_name)
20854 && DECL_CONSTRUCTOR_P (get_first_fn
20855 (unqualified_name)))
20856 sfk = sfk_constructor;
20858 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20859 *ctor_dtor_or_conv_p = -1;
20862 declarator = make_id_declarator (qualifying_scope,
20863 unqualified_name,
20864 sfk, token->location);
20865 declarator->std_attributes = attrs;
20866 declarator->parameter_pack_p = pack_expansion_p;
20868 if (pack_expansion_p)
20869 maybe_warn_variadic_templates ();
20872 handle_declarator:;
20873 scope = get_scope_of_declarator (declarator);
20874 if (scope)
20876 /* Any names that appear after the declarator-id for a
20877 member are looked up in the containing scope. */
20878 if (at_function_scope_p ())
20880 /* But declarations with qualified-ids can't appear in a
20881 function. */
20882 cp_parser_error (parser, "qualified-id in declaration");
20883 declarator = cp_error_declarator;
20884 break;
20886 pushed_scope = push_scope (scope);
20888 parser->in_declarator_p = true;
20889 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20890 || (declarator && declarator->kind == cdk_id))
20891 /* Default args are only allowed on function
20892 declarations. */
20893 parser->default_arg_ok_p = saved_default_arg_ok_p;
20894 else
20895 parser->default_arg_ok_p = false;
20897 first = false;
20899 /* We're done. */
20900 else
20901 break;
20904 /* For an abstract declarator, we might wind up with nothing at this
20905 point. That's an error; the declarator is not optional. */
20906 if (!declarator)
20907 cp_parser_error (parser, "expected declarator");
20908 else if (open_paren)
20910 /* Record overly parenthesized declarator so we can give a
20911 diagnostic about confusing decl/expr disambiguation. */
20912 if (declarator->kind == cdk_array)
20914 /* If the open and close parens are on different lines, this
20915 is probably a formatting thing, so ignore. */
20916 expanded_location open = expand_location (open_paren->location);
20917 expanded_location close = expand_location (close_paren->location);
20918 if (open.line != close.line || open.file != close.file)
20919 open_paren = NULL;
20921 if (open_paren)
20922 declarator->parenthesized = open_paren->location;
20925 /* If we entered a scope, we must exit it now. */
20926 if (pushed_scope)
20927 pop_scope (pushed_scope);
20929 parser->default_arg_ok_p = saved_default_arg_ok_p;
20930 parser->in_declarator_p = saved_in_declarator_p;
20932 return declarator;
20935 /* Parse a ptr-operator.
20937 ptr-operator:
20938 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20939 * cv-qualifier-seq [opt]
20941 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20942 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20944 GNU Extension:
20946 ptr-operator:
20947 & cv-qualifier-seq [opt]
20949 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20950 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20951 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20952 filled in with the TYPE containing the member. *CV_QUALS is
20953 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20954 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20955 Note that the tree codes returned by this function have nothing
20956 to do with the types of trees that will be eventually be created
20957 to represent the pointer or reference type being parsed. They are
20958 just constants with suggestive names. */
20959 static enum tree_code
20960 cp_parser_ptr_operator (cp_parser* parser,
20961 tree* type,
20962 cp_cv_quals *cv_quals,
20963 tree *attributes)
20965 enum tree_code code = ERROR_MARK;
20966 cp_token *token;
20967 tree attrs = NULL_TREE;
20969 /* Assume that it's not a pointer-to-member. */
20970 *type = NULL_TREE;
20971 /* And that there are no cv-qualifiers. */
20972 *cv_quals = TYPE_UNQUALIFIED;
20974 /* Peek at the next token. */
20975 token = cp_lexer_peek_token (parser->lexer);
20977 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20978 if (token->type == CPP_MULT)
20979 code = INDIRECT_REF;
20980 else if (token->type == CPP_AND)
20981 code = ADDR_EXPR;
20982 else if ((cxx_dialect != cxx98) &&
20983 token->type == CPP_AND_AND) /* C++0x only */
20984 code = NON_LVALUE_EXPR;
20986 if (code != ERROR_MARK)
20988 /* Consume the `*', `&' or `&&'. */
20989 cp_lexer_consume_token (parser->lexer);
20991 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20992 `&', if we are allowing GNU extensions. (The only qualifier
20993 that can legally appear after `&' is `restrict', but that is
20994 enforced during semantic analysis. */
20995 if (code == INDIRECT_REF
20996 || cp_parser_allow_gnu_extensions_p (parser))
20997 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20999 attrs = cp_parser_std_attribute_spec_seq (parser);
21000 if (attributes != NULL)
21001 *attributes = attrs;
21003 else
21005 /* Try the pointer-to-member case. */
21006 cp_parser_parse_tentatively (parser);
21007 /* Look for the optional `::' operator. */
21008 cp_parser_global_scope_opt (parser,
21009 /*current_scope_valid_p=*/false);
21010 /* Look for the nested-name specifier. */
21011 token = cp_lexer_peek_token (parser->lexer);
21012 cp_parser_nested_name_specifier (parser,
21013 /*typename_keyword_p=*/false,
21014 /*check_dependency_p=*/true,
21015 /*type_p=*/false,
21016 /*is_declaration=*/false);
21017 /* If we found it, and the next token is a `*', then we are
21018 indeed looking at a pointer-to-member operator. */
21019 if (!cp_parser_error_occurred (parser)
21020 && cp_parser_require (parser, CPP_MULT, RT_MULT))
21022 /* Indicate that the `*' operator was used. */
21023 code = INDIRECT_REF;
21025 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21026 error_at (token->location, "%qD is a namespace", parser->scope);
21027 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
21028 error_at (token->location, "cannot form pointer to member of "
21029 "non-class %q#T", parser->scope);
21030 else
21032 /* The type of which the member is a member is given by the
21033 current SCOPE. */
21034 *type = parser->scope;
21035 /* The next name will not be qualified. */
21036 parser->scope = NULL_TREE;
21037 parser->qualifying_scope = NULL_TREE;
21038 parser->object_scope = NULL_TREE;
21039 /* Look for optional c++11 attributes. */
21040 attrs = cp_parser_std_attribute_spec_seq (parser);
21041 if (attributes != NULL)
21042 *attributes = attrs;
21043 /* Look for the optional cv-qualifier-seq. */
21044 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21047 /* If that didn't work we don't have a ptr-operator. */
21048 if (!cp_parser_parse_definitely (parser))
21049 cp_parser_error (parser, "expected ptr-operator");
21052 return code;
21055 /* Parse an (optional) cv-qualifier-seq.
21057 cv-qualifier-seq:
21058 cv-qualifier cv-qualifier-seq [opt]
21060 cv-qualifier:
21061 const
21062 volatile
21064 GNU Extension:
21066 cv-qualifier:
21067 __restrict__
21069 Returns a bitmask representing the cv-qualifiers. */
21071 static cp_cv_quals
21072 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
21074 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
21076 while (true)
21078 cp_token *token;
21079 cp_cv_quals cv_qualifier;
21081 /* Peek at the next token. */
21082 token = cp_lexer_peek_token (parser->lexer);
21083 /* See if it's a cv-qualifier. */
21084 switch (token->keyword)
21086 case RID_CONST:
21087 cv_qualifier = TYPE_QUAL_CONST;
21088 break;
21090 case RID_VOLATILE:
21091 cv_qualifier = TYPE_QUAL_VOLATILE;
21092 break;
21094 case RID_RESTRICT:
21095 cv_qualifier = TYPE_QUAL_RESTRICT;
21096 break;
21098 default:
21099 cv_qualifier = TYPE_UNQUALIFIED;
21100 break;
21103 if (!cv_qualifier)
21104 break;
21106 if (cv_quals & cv_qualifier)
21108 gcc_rich_location richloc (token->location);
21109 richloc.add_fixit_remove ();
21110 error_at (&richloc, "duplicate cv-qualifier");
21111 cp_lexer_purge_token (parser->lexer);
21113 else
21115 cp_lexer_consume_token (parser->lexer);
21116 cv_quals |= cv_qualifier;
21120 return cv_quals;
21123 /* Parse an (optional) ref-qualifier
21125 ref-qualifier:
21129 Returns cp_ref_qualifier representing ref-qualifier. */
21131 static cp_ref_qualifier
21132 cp_parser_ref_qualifier_opt (cp_parser* parser)
21134 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
21136 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21137 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
21138 return ref_qual;
21140 while (true)
21142 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21143 cp_token *token = cp_lexer_peek_token (parser->lexer);
21145 switch (token->type)
21147 case CPP_AND:
21148 curr_ref_qual = REF_QUAL_LVALUE;
21149 break;
21151 case CPP_AND_AND:
21152 curr_ref_qual = REF_QUAL_RVALUE;
21153 break;
21155 default:
21156 curr_ref_qual = REF_QUAL_NONE;
21157 break;
21160 if (!curr_ref_qual)
21161 break;
21162 else if (ref_qual)
21164 error_at (token->location, "multiple ref-qualifiers");
21165 cp_lexer_purge_token (parser->lexer);
21167 else
21169 ref_qual = curr_ref_qual;
21170 cp_lexer_consume_token (parser->lexer);
21174 return ref_qual;
21177 /* Parse an optional tx-qualifier.
21179 tx-qualifier:
21180 transaction_safe
21181 transaction_safe_dynamic */
21183 static tree
21184 cp_parser_tx_qualifier_opt (cp_parser *parser)
21186 cp_token *token = cp_lexer_peek_token (parser->lexer);
21187 if (token->type == CPP_NAME)
21189 tree name = token->u.value;
21190 const char *p = IDENTIFIER_POINTER (name);
21191 const int len = strlen ("transaction_safe");
21192 if (!strncmp (p, "transaction_safe", len))
21194 p += len;
21195 if (*p == '\0'
21196 || !strcmp (p, "_dynamic"))
21198 cp_lexer_consume_token (parser->lexer);
21199 if (!flag_tm)
21201 error ("%qE requires %<-fgnu-tm%>", name);
21202 return NULL_TREE;
21204 else
21205 return name;
21209 return NULL_TREE;
21212 /* Parse an (optional) virt-specifier-seq.
21214 virt-specifier-seq:
21215 virt-specifier virt-specifier-seq [opt]
21217 virt-specifier:
21218 override
21219 final
21221 Returns a bitmask representing the virt-specifiers. */
21223 static cp_virt_specifiers
21224 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21226 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21228 while (true)
21230 cp_token *token;
21231 cp_virt_specifiers virt_specifier;
21233 /* Peek at the next token. */
21234 token = cp_lexer_peek_token (parser->lexer);
21235 /* See if it's a virt-specifier-qualifier. */
21236 if (token->type != CPP_NAME)
21237 break;
21238 if (id_equal (token->u.value, "override"))
21240 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21241 virt_specifier = VIRT_SPEC_OVERRIDE;
21243 else if (id_equal (token->u.value, "final"))
21245 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21246 virt_specifier = VIRT_SPEC_FINAL;
21248 else if (id_equal (token->u.value, "__final"))
21250 virt_specifier = VIRT_SPEC_FINAL;
21252 else
21253 break;
21255 if (virt_specifiers & virt_specifier)
21257 gcc_rich_location richloc (token->location);
21258 richloc.add_fixit_remove ();
21259 error_at (&richloc, "duplicate virt-specifier");
21260 cp_lexer_purge_token (parser->lexer);
21262 else
21264 cp_lexer_consume_token (parser->lexer);
21265 virt_specifiers |= virt_specifier;
21268 return virt_specifiers;
21271 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21272 is in scope even though it isn't real. */
21274 void
21275 inject_this_parameter (tree ctype, cp_cv_quals quals)
21277 tree this_parm;
21279 if (current_class_ptr)
21281 /* We don't clear this between NSDMIs. Is it already what we want? */
21282 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21283 if (DECL_P (current_class_ptr)
21284 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21285 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21286 && cp_type_quals (type) == quals)
21287 return;
21290 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21291 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21292 current_class_ptr = NULL_TREE;
21293 current_class_ref
21294 = cp_build_fold_indirect_ref (this_parm);
21295 current_class_ptr = this_parm;
21298 /* Return true iff our current scope is a non-static data member
21299 initializer. */
21301 bool
21302 parsing_nsdmi (void)
21304 /* We recognize NSDMI context by the context-less 'this' pointer set up
21305 by the function above. */
21306 if (current_class_ptr
21307 && TREE_CODE (current_class_ptr) == PARM_DECL
21308 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21309 return true;
21310 return false;
21313 /* Parse a late-specified return type, if any. This is not a separate
21314 non-terminal, but part of a function declarator, which looks like
21316 -> trailing-type-specifier-seq abstract-declarator(opt)
21318 Returns the type indicated by the type-id.
21320 In addition to this, parse any queued up #pragma omp declare simd
21321 clauses, and #pragma acc routine clauses.
21323 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21324 function. */
21326 static tree
21327 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21328 tree& requires_clause, cp_cv_quals quals)
21330 cp_token *token;
21331 tree type = NULL_TREE;
21332 bool declare_simd_p = (parser->omp_declare_simd
21333 && declarator
21334 && declarator->kind == cdk_id);
21336 bool oacc_routine_p = (parser->oacc_routine
21337 && declarator
21338 && declarator->kind == cdk_id);
21340 /* Peek at the next token. */
21341 token = cp_lexer_peek_token (parser->lexer);
21342 /* A late-specified return type is indicated by an initial '->'. */
21343 if (token->type != CPP_DEREF
21344 && token->keyword != RID_REQUIRES
21345 && !(token->type == CPP_NAME
21346 && token->u.value == ridpointers[RID_REQUIRES])
21347 && !(declare_simd_p || oacc_routine_p))
21348 return NULL_TREE;
21350 tree save_ccp = current_class_ptr;
21351 tree save_ccr = current_class_ref;
21352 if (quals >= 0)
21354 /* DR 1207: 'this' is in scope in the trailing return type. */
21355 inject_this_parameter (current_class_type, quals);
21358 if (token->type == CPP_DEREF)
21360 /* Consume the ->. */
21361 cp_lexer_consume_token (parser->lexer);
21363 type = cp_parser_trailing_type_id (parser);
21366 /* Function declarations may be followed by a trailing
21367 requires-clause. */
21368 requires_clause = cp_parser_requires_clause_opt (parser);
21370 if (declare_simd_p)
21371 declarator->attributes
21372 = cp_parser_late_parsing_omp_declare_simd (parser,
21373 declarator->attributes);
21374 if (oacc_routine_p)
21375 declarator->attributes
21376 = cp_parser_late_parsing_oacc_routine (parser,
21377 declarator->attributes);
21379 if (quals >= 0)
21381 current_class_ptr = save_ccp;
21382 current_class_ref = save_ccr;
21385 return type;
21388 /* Parse a declarator-id.
21390 declarator-id:
21391 id-expression
21392 :: [opt] nested-name-specifier [opt] type-name
21394 In the `id-expression' case, the value returned is as for
21395 cp_parser_id_expression if the id-expression was an unqualified-id.
21396 If the id-expression was a qualified-id, then a SCOPE_REF is
21397 returned. The first operand is the scope (either a NAMESPACE_DECL
21398 or TREE_TYPE), but the second is still just a representation of an
21399 unqualified-id. */
21401 static tree
21402 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21404 tree id;
21405 /* The expression must be an id-expression. Assume that qualified
21406 names are the names of types so that:
21408 template <class T>
21409 int S<T>::R::i = 3;
21411 will work; we must treat `S<T>::R' as the name of a type.
21412 Similarly, assume that qualified names are templates, where
21413 required, so that:
21415 template <class T>
21416 int S<T>::R<T>::i = 3;
21418 will work, too. */
21419 id = cp_parser_id_expression (parser,
21420 /*template_keyword_p=*/false,
21421 /*check_dependency_p=*/false,
21422 /*template_p=*/NULL,
21423 /*declarator_p=*/true,
21424 optional_p);
21425 if (id && BASELINK_P (id))
21426 id = BASELINK_FUNCTIONS (id);
21427 return id;
21430 /* Parse a type-id.
21432 type-id:
21433 type-specifier-seq abstract-declarator [opt]
21435 Returns the TYPE specified. */
21437 static tree
21438 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21439 bool is_trailing_return, location_t * type_location)
21441 cp_decl_specifier_seq type_specifier_seq;
21442 cp_declarator *abstract_declarator;
21444 /* Parse the type-specifier-seq. */
21445 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21446 is_trailing_return,
21447 &type_specifier_seq);
21448 if (type_location)
21449 *type_location = type_specifier_seq.locations[ds_type_spec];
21451 if (is_template_arg && type_specifier_seq.type
21452 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21453 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21454 /* A bare template name as a template argument is a template template
21455 argument, not a placeholder, so fail parsing it as a type argument. */
21457 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21458 cp_parser_simulate_error (parser);
21459 return error_mark_node;
21461 if (type_specifier_seq.type == error_mark_node)
21462 return error_mark_node;
21464 /* There might or might not be an abstract declarator. */
21465 cp_parser_parse_tentatively (parser);
21466 /* Look for the declarator. */
21467 abstract_declarator
21468 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21469 /*parenthesized_p=*/NULL,
21470 /*member_p=*/false,
21471 /*friend_p=*/false);
21472 /* Check to see if there really was a declarator. */
21473 if (!cp_parser_parse_definitely (parser))
21474 abstract_declarator = NULL;
21476 if (type_specifier_seq.type
21477 /* The concepts TS allows 'auto' as a type-id. */
21478 && (!flag_concepts || parser->in_type_id_in_expr_p)
21479 /* None of the valid uses of 'auto' in C++14 involve the type-id
21480 nonterminal, but it is valid in a trailing-return-type. */
21481 && !(cxx_dialect >= cxx14 && is_trailing_return))
21482 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21484 /* A type-id with type 'auto' is only ok if the abstract declarator
21485 is a function declarator with a late-specified return type.
21487 A type-id with 'auto' is also valid in a trailing-return-type
21488 in a compound-requirement. */
21489 if (abstract_declarator
21490 && abstract_declarator->kind == cdk_function
21491 && abstract_declarator->u.function.late_return_type)
21492 /* OK */;
21493 else if (parser->in_result_type_constraint_p)
21494 /* OK */;
21495 else
21497 location_t loc = type_specifier_seq.locations[ds_type_spec];
21498 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21500 error_at (loc, "missing template arguments after %qT",
21501 auto_node);
21502 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21503 tmpl);
21505 else
21506 error_at (loc, "invalid use of %qT", auto_node);
21507 return error_mark_node;
21511 return groktypename (&type_specifier_seq, abstract_declarator,
21512 is_template_arg);
21515 static tree
21516 cp_parser_type_id (cp_parser *parser, location_t * type_location)
21518 return cp_parser_type_id_1 (parser, false, false, type_location);
21521 static tree
21522 cp_parser_template_type_arg (cp_parser *parser)
21524 tree r;
21525 const char *saved_message = parser->type_definition_forbidden_message;
21526 parser->type_definition_forbidden_message
21527 = G_("types may not be defined in template arguments");
21528 r = cp_parser_type_id_1 (parser, true, false, NULL);
21529 parser->type_definition_forbidden_message = saved_message;
21530 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21532 error ("invalid use of %<auto%> in template argument");
21533 r = error_mark_node;
21535 return r;
21538 static tree
21539 cp_parser_trailing_type_id (cp_parser *parser)
21541 return cp_parser_type_id_1 (parser, false, true, NULL);
21544 /* Parse a type-specifier-seq.
21546 type-specifier-seq:
21547 type-specifier type-specifier-seq [opt]
21549 GNU extension:
21551 type-specifier-seq:
21552 attributes type-specifier-seq [opt]
21554 If IS_DECLARATION is true, we are at the start of a "condition" or
21555 exception-declaration, so we might be followed by a declarator-id.
21557 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21558 i.e. we've just seen "->".
21560 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21562 static void
21563 cp_parser_type_specifier_seq (cp_parser* parser,
21564 bool is_declaration,
21565 bool is_trailing_return,
21566 cp_decl_specifier_seq *type_specifier_seq)
21568 bool seen_type_specifier = false;
21569 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21570 cp_token *start_token = NULL;
21572 /* Clear the TYPE_SPECIFIER_SEQ. */
21573 clear_decl_specs (type_specifier_seq);
21575 /* In the context of a trailing return type, enum E { } is an
21576 elaborated-type-specifier followed by a function-body, not an
21577 enum-specifier. */
21578 if (is_trailing_return)
21579 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21581 /* Parse the type-specifiers and attributes. */
21582 while (true)
21584 tree type_specifier;
21585 bool is_cv_qualifier;
21587 /* Check for attributes first. */
21588 if (cp_next_tokens_can_be_attribute_p (parser))
21590 type_specifier_seq->attributes
21591 = attr_chainon (type_specifier_seq->attributes,
21592 cp_parser_attributes_opt (parser));
21593 continue;
21596 /* record the token of the beginning of the type specifier seq,
21597 for error reporting purposes*/
21598 if (!start_token)
21599 start_token = cp_lexer_peek_token (parser->lexer);
21601 /* Look for the type-specifier. */
21602 type_specifier = cp_parser_type_specifier (parser,
21603 flags,
21604 type_specifier_seq,
21605 /*is_declaration=*/false,
21606 NULL,
21607 &is_cv_qualifier);
21608 if (!type_specifier)
21610 /* If the first type-specifier could not be found, this is not a
21611 type-specifier-seq at all. */
21612 if (!seen_type_specifier)
21614 /* Set in_declarator_p to avoid skipping to the semicolon. */
21615 int in_decl = parser->in_declarator_p;
21616 parser->in_declarator_p = true;
21618 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21619 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21620 cp_parser_error (parser, "expected type-specifier");
21622 parser->in_declarator_p = in_decl;
21624 type_specifier_seq->type = error_mark_node;
21625 return;
21627 /* If subsequent type-specifiers could not be found, the
21628 type-specifier-seq is complete. */
21629 break;
21632 seen_type_specifier = true;
21633 /* The standard says that a condition can be:
21635 type-specifier-seq declarator = assignment-expression
21637 However, given:
21639 struct S {};
21640 if (int S = ...)
21642 we should treat the "S" as a declarator, not as a
21643 type-specifier. The standard doesn't say that explicitly for
21644 type-specifier-seq, but it does say that for
21645 decl-specifier-seq in an ordinary declaration. Perhaps it
21646 would be clearer just to allow a decl-specifier-seq here, and
21647 then add a semantic restriction that if any decl-specifiers
21648 that are not type-specifiers appear, the program is invalid. */
21649 if (is_declaration && !is_cv_qualifier)
21650 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21654 /* Return whether the function currently being declared has an associated
21655 template parameter list. */
21657 static bool
21658 function_being_declared_is_template_p (cp_parser* parser)
21660 if (!current_template_parms || processing_template_parmlist)
21661 return false;
21663 if (parser->implicit_template_scope)
21664 return true;
21666 if (at_class_scope_p ()
21667 && TYPE_BEING_DEFINED (current_class_type))
21668 return parser->num_template_parameter_lists != 0;
21670 return ((int) parser->num_template_parameter_lists > template_class_depth
21671 (current_class_type));
21674 /* Parse a parameter-declaration-clause.
21676 parameter-declaration-clause:
21677 parameter-declaration-list [opt] ... [opt]
21678 parameter-declaration-list , ...
21680 Returns a representation for the parameter declarations. A return
21681 value of NULL indicates a parameter-declaration-clause consisting
21682 only of an ellipsis. */
21684 static tree
21685 cp_parser_parameter_declaration_clause (cp_parser* parser)
21687 tree parameters;
21688 cp_token *token;
21689 bool ellipsis_p;
21691 temp_override<bool> cleanup
21692 (parser->auto_is_implicit_function_template_parm_p);
21694 if (!processing_specialization
21695 && !processing_template_parmlist
21696 && !processing_explicit_instantiation
21697 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21698 actual function or a random abstract declarator. */
21699 && parser->default_arg_ok_p)
21700 if (!current_function_decl
21701 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21702 parser->auto_is_implicit_function_template_parm_p = true;
21704 /* Peek at the next token. */
21705 token = cp_lexer_peek_token (parser->lexer);
21706 /* Check for trivial parameter-declaration-clauses. */
21707 if (token->type == CPP_ELLIPSIS)
21709 /* Consume the `...' token. */
21710 cp_lexer_consume_token (parser->lexer);
21711 return NULL_TREE;
21713 else if (token->type == CPP_CLOSE_PAREN)
21714 /* There are no parameters. */
21715 return void_list_node;
21716 /* Check for `(void)', too, which is a special case. */
21717 else if (token->keyword == RID_VOID
21718 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21719 == CPP_CLOSE_PAREN))
21721 /* Consume the `void' token. */
21722 cp_lexer_consume_token (parser->lexer);
21723 /* There are no parameters. */
21724 return void_list_node;
21727 /* Parse the parameter-declaration-list. */
21728 parameters = cp_parser_parameter_declaration_list (parser);
21729 /* If a parse error occurred while parsing the
21730 parameter-declaration-list, then the entire
21731 parameter-declaration-clause is erroneous. */
21732 if (parameters == error_mark_node)
21733 return NULL_TREE;
21735 /* Peek at the next token. */
21736 token = cp_lexer_peek_token (parser->lexer);
21737 /* If it's a `,', the clause should terminate with an ellipsis. */
21738 if (token->type == CPP_COMMA)
21740 /* Consume the `,'. */
21741 cp_lexer_consume_token (parser->lexer);
21742 /* Expect an ellipsis. */
21743 ellipsis_p
21744 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21746 /* It might also be `...' if the optional trailing `,' was
21747 omitted. */
21748 else if (token->type == CPP_ELLIPSIS)
21750 /* Consume the `...' token. */
21751 cp_lexer_consume_token (parser->lexer);
21752 /* And remember that we saw it. */
21753 ellipsis_p = true;
21755 else
21756 ellipsis_p = false;
21758 /* Finish the parameter list. */
21759 if (!ellipsis_p)
21760 parameters = chainon (parameters, void_list_node);
21762 return parameters;
21765 /* Parse a parameter-declaration-list.
21767 parameter-declaration-list:
21768 parameter-declaration
21769 parameter-declaration-list , parameter-declaration
21771 Returns a representation of the parameter-declaration-list, as for
21772 cp_parser_parameter_declaration_clause. However, the
21773 `void_list_node' is never appended to the list. */
21775 static tree
21776 cp_parser_parameter_declaration_list (cp_parser* parser)
21778 tree parameters = NULL_TREE;
21779 tree *tail = &parameters;
21780 bool saved_in_unbraced_linkage_specification_p;
21781 int index = 0;
21783 /* The special considerations that apply to a function within an
21784 unbraced linkage specifications do not apply to the parameters
21785 to the function. */
21786 saved_in_unbraced_linkage_specification_p
21787 = parser->in_unbraced_linkage_specification_p;
21788 parser->in_unbraced_linkage_specification_p = false;
21790 /* Look for more parameters. */
21791 while (true)
21793 cp_parameter_declarator *parameter;
21794 tree decl = error_mark_node;
21795 bool parenthesized_p = false;
21797 /* Parse the parameter. */
21798 parameter
21799 = cp_parser_parameter_declaration (parser,
21800 /*template_parm_p=*/false,
21801 &parenthesized_p);
21803 /* We don't know yet if the enclosing context is deprecated, so wait
21804 and warn in grokparms if appropriate. */
21805 deprecated_state = DEPRECATED_SUPPRESS;
21807 if (parameter)
21809 decl = grokdeclarator (parameter->declarator,
21810 &parameter->decl_specifiers,
21811 PARM,
21812 parameter->default_argument != NULL_TREE,
21813 &parameter->decl_specifiers.attributes);
21814 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21815 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21818 deprecated_state = DEPRECATED_NORMAL;
21820 /* If a parse error occurred parsing the parameter declaration,
21821 then the entire parameter-declaration-list is erroneous. */
21822 if (decl == error_mark_node)
21824 parameters = error_mark_node;
21825 break;
21828 if (parameter->decl_specifiers.attributes)
21829 cplus_decl_attributes (&decl,
21830 parameter->decl_specifiers.attributes,
21832 if (DECL_NAME (decl))
21833 decl = pushdecl (decl);
21835 if (decl != error_mark_node)
21837 retrofit_lang_decl (decl);
21838 DECL_PARM_INDEX (decl) = ++index;
21839 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21842 /* Add the new parameter to the list. */
21843 *tail = build_tree_list (parameter->default_argument, decl);
21844 tail = &TREE_CHAIN (*tail);
21846 /* Peek at the next token. */
21847 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21848 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21849 /* These are for Objective-C++ */
21850 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21851 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21852 /* The parameter-declaration-list is complete. */
21853 break;
21854 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21856 cp_token *token;
21858 /* Peek at the next token. */
21859 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21860 /* If it's an ellipsis, then the list is complete. */
21861 if (token->type == CPP_ELLIPSIS)
21862 break;
21863 /* Otherwise, there must be more parameters. Consume the
21864 `,'. */
21865 cp_lexer_consume_token (parser->lexer);
21866 /* When parsing something like:
21868 int i(float f, double d)
21870 we can tell after seeing the declaration for "f" that we
21871 are not looking at an initialization of a variable "i",
21872 but rather at the declaration of a function "i".
21874 Due to the fact that the parsing of template arguments
21875 (as specified to a template-id) requires backtracking we
21876 cannot use this technique when inside a template argument
21877 list. */
21878 if (!parser->in_template_argument_list_p
21879 && !parser->in_type_id_in_expr_p
21880 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21881 /* However, a parameter-declaration of the form
21882 "float(f)" (which is a valid declaration of a
21883 parameter "f") can also be interpreted as an
21884 expression (the conversion of "f" to "float"). */
21885 && !parenthesized_p)
21886 cp_parser_commit_to_tentative_parse (parser);
21888 else
21890 cp_parser_error (parser, "expected %<,%> or %<...%>");
21891 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21892 cp_parser_skip_to_closing_parenthesis (parser,
21893 /*recovering=*/true,
21894 /*or_comma=*/false,
21895 /*consume_paren=*/false);
21896 break;
21900 parser->in_unbraced_linkage_specification_p
21901 = saved_in_unbraced_linkage_specification_p;
21903 /* Reset implicit_template_scope if we are about to leave the function
21904 parameter list that introduced it. Note that for out-of-line member
21905 definitions, there will be one or more class scopes before we get to
21906 the template parameter scope. */
21908 if (cp_binding_level *its = parser->implicit_template_scope)
21909 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21911 while (maybe_its->kind == sk_class)
21912 maybe_its = maybe_its->level_chain;
21913 if (maybe_its == its)
21915 parser->implicit_template_parms = 0;
21916 parser->implicit_template_scope = 0;
21920 return parameters;
21923 /* Parse a parameter declaration.
21925 parameter-declaration:
21926 decl-specifier-seq ... [opt] declarator
21927 decl-specifier-seq declarator = assignment-expression
21928 decl-specifier-seq ... [opt] abstract-declarator [opt]
21929 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21931 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21932 declares a template parameter. (In that case, a non-nested `>'
21933 token encountered during the parsing of the assignment-expression
21934 is not interpreted as a greater-than operator.)
21936 Returns a representation of the parameter, or NULL if an error
21937 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21938 true iff the declarator is of the form "(p)". */
21940 static cp_parameter_declarator *
21941 cp_parser_parameter_declaration (cp_parser *parser,
21942 bool template_parm_p,
21943 bool *parenthesized_p)
21945 int declares_class_or_enum;
21946 cp_decl_specifier_seq decl_specifiers;
21947 cp_declarator *declarator;
21948 tree default_argument;
21949 cp_token *token = NULL, *declarator_token_start = NULL;
21950 const char *saved_message;
21951 bool template_parameter_pack_p = false;
21953 /* In a template parameter, `>' is not an operator.
21955 [temp.param]
21957 When parsing a default template-argument for a non-type
21958 template-parameter, the first non-nested `>' is taken as the end
21959 of the template parameter-list rather than a greater-than
21960 operator. */
21962 /* Type definitions may not appear in parameter types. */
21963 saved_message = parser->type_definition_forbidden_message;
21964 parser->type_definition_forbidden_message
21965 = G_("types may not be defined in parameter types");
21967 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21968 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21969 (current_template_parms)) : 0);
21971 /* Parse the declaration-specifiers. */
21972 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21973 cp_parser_decl_specifier_seq (parser,
21974 CP_PARSER_FLAGS_NONE,
21975 &decl_specifiers,
21976 &declares_class_or_enum);
21978 /* Complain about missing 'typename' or other invalid type names. */
21979 if (!decl_specifiers.any_type_specifiers_p
21980 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21981 decl_specifiers.type = error_mark_node;
21983 /* If an error occurred, there's no reason to attempt to parse the
21984 rest of the declaration. */
21985 if (cp_parser_error_occurred (parser))
21987 parser->type_definition_forbidden_message = saved_message;
21988 return NULL;
21991 /* Peek at the next token. */
21992 token = cp_lexer_peek_token (parser->lexer);
21994 /* If the next token is a `)', `,', `=', `>', or `...', then there
21995 is no declarator. However, when variadic templates are enabled,
21996 there may be a declarator following `...'. */
21997 if (token->type == CPP_CLOSE_PAREN
21998 || token->type == CPP_COMMA
21999 || token->type == CPP_EQ
22000 || token->type == CPP_GREATER)
22002 declarator = NULL;
22003 if (parenthesized_p)
22004 *parenthesized_p = false;
22006 /* Otherwise, there should be a declarator. */
22007 else
22009 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22010 parser->default_arg_ok_p = false;
22012 /* After seeing a decl-specifier-seq, if the next token is not a
22013 "(", there is no possibility that the code is a valid
22014 expression. Therefore, if parsing tentatively, we commit at
22015 this point. */
22016 if (!parser->in_template_argument_list_p
22017 /* In an expression context, having seen:
22019 (int((char ...
22021 we cannot be sure whether we are looking at a
22022 function-type (taking a "char" as a parameter) or a cast
22023 of some object of type "char" to "int". */
22024 && !parser->in_type_id_in_expr_p
22025 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22026 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22027 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22028 cp_parser_commit_to_tentative_parse (parser);
22029 /* Parse the declarator. */
22030 declarator_token_start = token;
22031 declarator = cp_parser_declarator (parser,
22032 CP_PARSER_DECLARATOR_EITHER,
22033 /*ctor_dtor_or_conv_p=*/NULL,
22034 parenthesized_p,
22035 /*member_p=*/false,
22036 /*friend_p=*/false);
22037 parser->default_arg_ok_p = saved_default_arg_ok_p;
22038 /* After the declarator, allow more attributes. */
22039 decl_specifiers.attributes
22040 = attr_chainon (decl_specifiers.attributes,
22041 cp_parser_attributes_opt (parser));
22043 /* If the declarator is a template parameter pack, remember that and
22044 clear the flag in the declarator itself so we don't get errors
22045 from grokdeclarator. */
22046 if (template_parm_p && declarator && declarator->parameter_pack_p)
22048 declarator->parameter_pack_p = false;
22049 template_parameter_pack_p = true;
22053 /* If the next token is an ellipsis, and we have not seen a declarator
22054 name, and if either the type of the declarator contains parameter
22055 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22056 for, eg, abbreviated integral type names), then we actually have a
22057 parameter pack expansion expression. Otherwise, leave the ellipsis
22058 for a C-style variadic function. */
22059 token = cp_lexer_peek_token (parser->lexer);
22061 /* If a function parameter pack was specified and an implicit template
22062 parameter was introduced during cp_parser_parameter_declaration,
22063 change any implicit parameters introduced into packs. */
22064 if (parser->implicit_template_parms
22065 && ((token->type == CPP_ELLIPSIS
22066 && declarator_can_be_parameter_pack (declarator))
22067 || (declarator && declarator->parameter_pack_p)))
22069 int latest_template_parm_idx = TREE_VEC_LENGTH
22070 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
22072 if (latest_template_parm_idx != template_parm_idx)
22073 decl_specifiers.type = convert_generic_types_to_packs
22074 (decl_specifiers.type,
22075 template_parm_idx, latest_template_parm_idx);
22078 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22080 tree type = decl_specifiers.type;
22082 if (type && DECL_P (type))
22083 type = TREE_TYPE (type);
22085 if (((type
22086 && TREE_CODE (type) != TYPE_PACK_EXPANSION
22087 && (template_parm_p || uses_parameter_packs (type)))
22088 || (!type && template_parm_p))
22089 && declarator_can_be_parameter_pack (declarator))
22091 /* Consume the `...'. */
22092 cp_lexer_consume_token (parser->lexer);
22093 maybe_warn_variadic_templates ();
22095 /* Build a pack expansion type */
22096 if (template_parm_p)
22097 template_parameter_pack_p = true;
22098 else if (declarator)
22099 declarator->parameter_pack_p = true;
22100 else
22101 decl_specifiers.type = make_pack_expansion (type);
22105 /* The restriction on defining new types applies only to the type
22106 of the parameter, not to the default argument. */
22107 parser->type_definition_forbidden_message = saved_message;
22109 /* If the next token is `=', then process a default argument. */
22110 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22112 tree type = decl_specifiers.type;
22113 token = cp_lexer_peek_token (parser->lexer);
22114 /* If we are defining a class, then the tokens that make up the
22115 default argument must be saved and processed later. */
22116 if (!template_parm_p && at_class_scope_p ()
22117 && TYPE_BEING_DEFINED (current_class_type)
22118 && !LAMBDA_TYPE_P (current_class_type))
22119 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
22121 // A constrained-type-specifier may declare a type template-parameter.
22122 else if (declares_constrained_type_template_parameter (type))
22123 default_argument
22124 = cp_parser_default_type_template_argument (parser);
22126 // A constrained-type-specifier may declare a template-template-parameter.
22127 else if (declares_constrained_template_template_parameter (type))
22128 default_argument
22129 = cp_parser_default_template_template_argument (parser);
22131 /* Outside of a class definition, we can just parse the
22132 assignment-expression. */
22133 else
22134 default_argument
22135 = cp_parser_default_argument (parser, template_parm_p);
22137 if (!parser->default_arg_ok_p)
22139 permerror (token->location,
22140 "default arguments are only "
22141 "permitted for function parameters");
22143 else if ((declarator && declarator->parameter_pack_p)
22144 || template_parameter_pack_p
22145 || (decl_specifiers.type
22146 && PACK_EXPANSION_P (decl_specifiers.type)))
22148 /* Find the name of the parameter pack. */
22149 cp_declarator *id_declarator = declarator;
22150 while (id_declarator && id_declarator->kind != cdk_id)
22151 id_declarator = id_declarator->declarator;
22153 if (id_declarator && id_declarator->kind == cdk_id)
22154 error_at (declarator_token_start->location,
22155 template_parm_p
22156 ? G_("template parameter pack %qD "
22157 "cannot have a default argument")
22158 : G_("parameter pack %qD cannot have "
22159 "a default argument"),
22160 id_declarator->u.id.unqualified_name);
22161 else
22162 error_at (declarator_token_start->location,
22163 template_parm_p
22164 ? G_("template parameter pack cannot have "
22165 "a default argument")
22166 : G_("parameter pack cannot have a "
22167 "default argument"));
22169 default_argument = NULL_TREE;
22172 else
22173 default_argument = NULL_TREE;
22175 /* Generate a location for the parameter, ranging from the start of the
22176 initial token to the end of the final token (using input_location for
22177 the latter, set up by cp_lexer_set_source_position_from_token when
22178 consuming tokens).
22180 If we have a identifier, then use it for the caret location, e.g.
22182 extern int callee (int one, int (*two)(int, int), float three);
22183 ~~~~~~^~~~~~~~~~~~~~
22185 otherwise, reuse the start location for the caret location e.g.:
22187 extern int callee (int one, int (*)(int, int), float three);
22188 ^~~~~~~~~~~~~~~~~
22191 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22192 ? declarator->id_loc
22193 : decl_spec_token_start->location);
22194 location_t param_loc = make_location (caret_loc,
22195 decl_spec_token_start->location,
22196 input_location);
22198 return make_parameter_declarator (&decl_specifiers,
22199 declarator,
22200 default_argument,
22201 param_loc,
22202 template_parameter_pack_p);
22205 /* Parse a default argument and return it.
22207 TEMPLATE_PARM_P is true if this is a default argument for a
22208 non-type template parameter. */
22209 static tree
22210 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22212 tree default_argument = NULL_TREE;
22213 bool saved_greater_than_is_operator_p;
22214 bool saved_local_variables_forbidden_p;
22215 bool non_constant_p, is_direct_init;
22217 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22218 set correctly. */
22219 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22220 parser->greater_than_is_operator_p = !template_parm_p;
22221 /* Local variable names (and the `this' keyword) may not
22222 appear in a default argument. */
22223 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22224 parser->local_variables_forbidden_p = true;
22225 /* Parse the assignment-expression. */
22226 if (template_parm_p)
22227 push_deferring_access_checks (dk_no_deferred);
22228 tree saved_class_ptr = NULL_TREE;
22229 tree saved_class_ref = NULL_TREE;
22230 /* The "this" pointer is not valid in a default argument. */
22231 if (cfun)
22233 saved_class_ptr = current_class_ptr;
22234 cp_function_chain->x_current_class_ptr = NULL_TREE;
22235 saved_class_ref = current_class_ref;
22236 cp_function_chain->x_current_class_ref = NULL_TREE;
22238 default_argument
22239 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22240 /* Restore the "this" pointer. */
22241 if (cfun)
22243 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22244 cp_function_chain->x_current_class_ref = saved_class_ref;
22246 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22247 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22248 if (template_parm_p)
22249 pop_deferring_access_checks ();
22250 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22251 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22253 return default_argument;
22256 /* Parse a function-body.
22258 function-body:
22259 compound_statement */
22261 static void
22262 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22264 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22265 ? BCS_TRY_BLOCK : BCS_NORMAL),
22266 true);
22269 /* Parse a ctor-initializer-opt followed by a function-body. Return
22270 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22271 is true we are parsing a function-try-block. */
22273 static void
22274 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22275 bool in_function_try_block)
22277 tree body, list;
22278 const bool check_body_p =
22279 DECL_CONSTRUCTOR_P (current_function_decl)
22280 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
22281 tree last = NULL;
22283 /* Begin the function body. */
22284 body = begin_function_body ();
22285 /* Parse the optional ctor-initializer. */
22286 cp_parser_ctor_initializer_opt (parser);
22288 /* If we're parsing a constexpr constructor definition, we need
22289 to check that the constructor body is indeed empty. However,
22290 before we get to cp_parser_function_body lot of junk has been
22291 generated, so we can't just check that we have an empty block.
22292 Rather we take a snapshot of the outermost block, and check whether
22293 cp_parser_function_body changed its state. */
22294 if (check_body_p)
22296 list = cur_stmt_list;
22297 if (STATEMENT_LIST_TAIL (list))
22298 last = STATEMENT_LIST_TAIL (list)->stmt;
22300 /* Parse the function-body. */
22301 cp_parser_function_body (parser, in_function_try_block);
22302 if (check_body_p)
22303 check_constexpr_ctor_body (last, list, /*complain=*/true);
22304 /* Finish the function body. */
22305 finish_function_body (body);
22308 /* Parse an initializer.
22310 initializer:
22311 = initializer-clause
22312 ( expression-list )
22314 Returns an expression representing the initializer. If no
22315 initializer is present, NULL_TREE is returned.
22317 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22318 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22319 set to TRUE if there is no initializer present. If there is an
22320 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22321 is set to true; otherwise it is set to false. */
22323 static tree
22324 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22325 bool* non_constant_p, bool subexpression_p)
22327 cp_token *token;
22328 tree init;
22330 /* Peek at the next token. */
22331 token = cp_lexer_peek_token (parser->lexer);
22333 /* Let our caller know whether or not this initializer was
22334 parenthesized. */
22335 *is_direct_init = (token->type != CPP_EQ);
22336 /* Assume that the initializer is constant. */
22337 *non_constant_p = false;
22339 if (token->type == CPP_EQ)
22341 /* Consume the `='. */
22342 cp_lexer_consume_token (parser->lexer);
22343 /* Parse the initializer-clause. */
22344 init = cp_parser_initializer_clause (parser, non_constant_p);
22346 else if (token->type == CPP_OPEN_PAREN)
22348 vec<tree, va_gc> *vec;
22349 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22350 /*cast_p=*/false,
22351 /*allow_expansion_p=*/true,
22352 non_constant_p);
22353 if (vec == NULL)
22354 return error_mark_node;
22355 init = build_tree_list_vec (vec);
22356 release_tree_vector (vec);
22358 else if (token->type == CPP_OPEN_BRACE)
22360 cp_lexer_set_source_position (parser->lexer);
22361 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22362 init = cp_parser_braced_list (parser, non_constant_p);
22363 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22365 else
22367 /* Anything else is an error. */
22368 cp_parser_error (parser, "expected initializer");
22369 init = error_mark_node;
22372 if (!subexpression_p && check_for_bare_parameter_packs (init))
22373 init = error_mark_node;
22375 return init;
22378 /* Parse an initializer-clause.
22380 initializer-clause:
22381 assignment-expression
22382 braced-init-list
22384 Returns an expression representing the initializer.
22386 If the `assignment-expression' production is used the value
22387 returned is simply a representation for the expression.
22389 Otherwise, calls cp_parser_braced_list. */
22391 static cp_expr
22392 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22394 cp_expr initializer;
22396 /* Assume the expression is constant. */
22397 *non_constant_p = false;
22399 /* If it is not a `{', then we are looking at an
22400 assignment-expression. */
22401 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22403 initializer
22404 = cp_parser_constant_expression (parser,
22405 /*allow_non_constant_p=*/true,
22406 non_constant_p);
22408 else
22409 initializer = cp_parser_braced_list (parser, non_constant_p);
22411 return initializer;
22414 /* Parse a brace-enclosed initializer list.
22416 braced-init-list:
22417 { initializer-list , [opt] }
22418 { designated-initializer-list , [opt] }
22421 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22422 the elements of the initializer-list (or NULL, if the last
22423 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22424 NULL_TREE. There is no way to detect whether or not the optional
22425 trailing `,' was provided. NON_CONSTANT_P is as for
22426 cp_parser_initializer. */
22428 static cp_expr
22429 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22431 tree initializer;
22432 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22434 /* Consume the `{' token. */
22435 matching_braces braces;
22436 braces.require_open (parser);
22437 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22438 initializer = make_node (CONSTRUCTOR);
22439 /* If it's not a `}', then there is a non-trivial initializer. */
22440 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22442 /* Parse the initializer list. */
22443 CONSTRUCTOR_ELTS (initializer)
22444 = cp_parser_initializer_list (parser, non_constant_p);
22445 /* A trailing `,' token is allowed. */
22446 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22447 cp_lexer_consume_token (parser->lexer);
22449 else
22450 *non_constant_p = false;
22451 /* Now, there should be a trailing `}'. */
22452 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22453 braces.require_close (parser);
22454 TREE_TYPE (initializer) = init_list_type_node;
22456 cp_expr result (initializer);
22457 /* Build a location of the form:
22458 { ... }
22459 ^~~~~~~
22460 with caret==start at the open brace, finish at the close brace. */
22461 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22462 result.set_location (combined_loc);
22463 return result;
22466 /* Consume tokens up to, and including, the next non-nested closing `]'.
22467 Returns true iff we found a closing `]'. */
22469 static bool
22470 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22472 unsigned square_depth = 0;
22474 while (true)
22476 cp_token * token = cp_lexer_peek_token (parser->lexer);
22478 switch (token->type)
22480 case CPP_EOF:
22481 case CPP_PRAGMA_EOL:
22482 /* If we've run out of tokens, then there is no closing `]'. */
22483 return false;
22485 case CPP_OPEN_SQUARE:
22486 ++square_depth;
22487 break;
22489 case CPP_CLOSE_SQUARE:
22490 if (!square_depth--)
22492 cp_lexer_consume_token (parser->lexer);
22493 return true;
22495 break;
22497 default:
22498 break;
22501 /* Consume the token. */
22502 cp_lexer_consume_token (parser->lexer);
22506 /* Return true if we are looking at an array-designator, false otherwise. */
22508 static bool
22509 cp_parser_array_designator_p (cp_parser *parser)
22511 /* Consume the `['. */
22512 cp_lexer_consume_token (parser->lexer);
22514 cp_lexer_save_tokens (parser->lexer);
22516 /* Skip tokens until the next token is a closing square bracket.
22517 If we find the closing `]', and the next token is a `=', then
22518 we are looking at an array designator. */
22519 bool array_designator_p
22520 = (cp_parser_skip_to_closing_square_bracket (parser)
22521 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22523 /* Roll back the tokens we skipped. */
22524 cp_lexer_rollback_tokens (parser->lexer);
22526 return array_designator_p;
22529 /* Parse an initializer-list.
22531 initializer-list:
22532 initializer-clause ... [opt]
22533 initializer-list , initializer-clause ... [opt]
22535 C++2A Extension:
22537 designated-initializer-list:
22538 designated-initializer-clause
22539 designated-initializer-list , designated-initializer-clause
22541 designated-initializer-clause:
22542 designator brace-or-equal-initializer
22544 designator:
22545 . identifier
22547 GNU Extension:
22549 initializer-list:
22550 designation initializer-clause ...[opt]
22551 initializer-list , designation initializer-clause ...[opt]
22553 designation:
22554 . identifier =
22555 identifier :
22556 [ constant-expression ] =
22558 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22559 for the initializer. If the INDEX of the elt is non-NULL, it is the
22560 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22561 as for cp_parser_initializer. */
22563 static vec<constructor_elt, va_gc> *
22564 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22566 vec<constructor_elt, va_gc> *v = NULL;
22567 bool first_p = true;
22568 tree first_designator = NULL_TREE;
22570 /* Assume all of the expressions are constant. */
22571 *non_constant_p = false;
22573 /* Parse the rest of the list. */
22574 while (true)
22576 cp_token *token;
22577 tree designator;
22578 tree initializer;
22579 bool clause_non_constant_p;
22580 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22582 /* Handle the C++2A syntax, '. id ='. */
22583 if ((cxx_dialect >= cxx2a
22584 || cp_parser_allow_gnu_extensions_p (parser))
22585 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22586 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22587 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22588 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22589 == CPP_OPEN_BRACE)))
22591 if (cxx_dialect < cxx2a)
22592 pedwarn (loc, OPT_Wpedantic,
22593 "C++ designated initializers only available with "
22594 "-std=c++2a or -std=gnu++2a");
22595 /* Consume the `.'. */
22596 cp_lexer_consume_token (parser->lexer);
22597 /* Consume the identifier. */
22598 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22599 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22600 /* Consume the `='. */
22601 cp_lexer_consume_token (parser->lexer);
22603 /* Also, if the next token is an identifier and the following one is a
22604 colon, we are looking at the GNU designated-initializer
22605 syntax. */
22606 else if (cp_parser_allow_gnu_extensions_p (parser)
22607 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22608 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22609 == CPP_COLON))
22611 /* Warn the user that they are using an extension. */
22612 pedwarn (loc, OPT_Wpedantic,
22613 "ISO C++ does not allow GNU designated initializers");
22614 /* Consume the identifier. */
22615 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22616 /* Consume the `:'. */
22617 cp_lexer_consume_token (parser->lexer);
22619 /* Also handle C99 array designators, '[ const ] ='. */
22620 else if (cp_parser_allow_gnu_extensions_p (parser)
22621 && !c_dialect_objc ()
22622 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22624 /* In C++11, [ could start a lambda-introducer. */
22625 bool non_const = false;
22627 cp_parser_parse_tentatively (parser);
22629 if (!cp_parser_array_designator_p (parser))
22631 cp_parser_simulate_error (parser);
22632 designator = NULL_TREE;
22634 else
22636 designator = cp_parser_constant_expression (parser, true,
22637 &non_const);
22638 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22639 cp_parser_require (parser, CPP_EQ, RT_EQ);
22642 if (!cp_parser_parse_definitely (parser))
22643 designator = NULL_TREE;
22644 else if (non_const
22645 && (!require_potential_rvalue_constant_expression
22646 (designator)))
22647 designator = NULL_TREE;
22648 if (designator)
22649 /* Warn the user that they are using an extension. */
22650 pedwarn (loc, OPT_Wpedantic,
22651 "ISO C++ does not allow C99 designated initializers");
22653 else
22654 designator = NULL_TREE;
22656 if (first_p)
22658 first_designator = designator;
22659 first_p = false;
22661 else if (cxx_dialect >= cxx2a
22662 && first_designator != error_mark_node
22663 && (!first_designator != !designator))
22665 error_at (loc, "either all initializer clauses should be designated "
22666 "or none of them should be");
22667 first_designator = error_mark_node;
22669 else if (cxx_dialect < cxx2a && !first_designator)
22670 first_designator = designator;
22672 /* Parse the initializer. */
22673 initializer = cp_parser_initializer_clause (parser,
22674 &clause_non_constant_p);
22675 /* If any clause is non-constant, so is the entire initializer. */
22676 if (clause_non_constant_p)
22677 *non_constant_p = true;
22679 /* If we have an ellipsis, this is an initializer pack
22680 expansion. */
22681 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22683 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22685 /* Consume the `...'. */
22686 cp_lexer_consume_token (parser->lexer);
22688 if (designator && cxx_dialect >= cxx2a)
22689 error_at (loc,
22690 "%<...%> not allowed in designated initializer list");
22692 /* Turn the initializer into an initializer expansion. */
22693 initializer = make_pack_expansion (initializer);
22696 /* Add it to the vector. */
22697 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22699 /* If the next token is not a comma, we have reached the end of
22700 the list. */
22701 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22702 break;
22704 /* Peek at the next token. */
22705 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22706 /* If the next token is a `}', then we're still done. An
22707 initializer-clause can have a trailing `,' after the
22708 initializer-list and before the closing `}'. */
22709 if (token->type == CPP_CLOSE_BRACE)
22710 break;
22712 /* Consume the `,' token. */
22713 cp_lexer_consume_token (parser->lexer);
22716 /* The same identifier shall not appear in multiple designators
22717 of a designated-initializer-list. */
22718 if (first_designator)
22720 unsigned int i;
22721 tree designator, val;
22722 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22723 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22725 if (IDENTIFIER_MARKED (designator))
22727 error_at (cp_expr_loc_or_loc (val, input_location),
22728 "%<.%s%> designator used multiple times in "
22729 "the same initializer list",
22730 IDENTIFIER_POINTER (designator));
22731 (*v)[i].index = error_mark_node;
22733 else
22734 IDENTIFIER_MARKED (designator) = 1;
22736 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22737 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22738 IDENTIFIER_MARKED (designator) = 0;
22741 return v;
22744 /* Classes [gram.class] */
22746 /* Parse a class-name.
22748 class-name:
22749 identifier
22750 template-id
22752 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22753 to indicate that names looked up in dependent types should be
22754 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22755 keyword has been used to indicate that the name that appears next
22756 is a template. TAG_TYPE indicates the explicit tag given before
22757 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22758 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22759 is the class being defined in a class-head. If ENUM_OK is TRUE,
22760 enum-names are also accepted.
22762 Returns the TYPE_DECL representing the class. */
22764 static tree
22765 cp_parser_class_name (cp_parser *parser,
22766 bool typename_keyword_p,
22767 bool template_keyword_p,
22768 enum tag_types tag_type,
22769 bool check_dependency_p,
22770 bool class_head_p,
22771 bool is_declaration,
22772 bool enum_ok)
22774 tree decl;
22775 tree scope;
22776 bool typename_p;
22777 cp_token *token;
22778 tree identifier = NULL_TREE;
22780 /* All class-names start with an identifier. */
22781 token = cp_lexer_peek_token (parser->lexer);
22782 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22784 cp_parser_error (parser, "expected class-name");
22785 return error_mark_node;
22788 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22789 to a template-id, so we save it here. */
22790 scope = parser->scope;
22791 if (scope == error_mark_node)
22792 return error_mark_node;
22794 /* Any name names a type if we're following the `typename' keyword
22795 in a qualified name where the enclosing scope is type-dependent. */
22796 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22797 && dependent_type_p (scope));
22798 /* Handle the common case (an identifier, but not a template-id)
22799 efficiently. */
22800 if (token->type == CPP_NAME
22801 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22803 cp_token *identifier_token;
22804 bool ambiguous_p;
22806 /* Look for the identifier. */
22807 identifier_token = cp_lexer_peek_token (parser->lexer);
22808 ambiguous_p = identifier_token->error_reported;
22809 identifier = cp_parser_identifier (parser);
22810 /* If the next token isn't an identifier, we are certainly not
22811 looking at a class-name. */
22812 if (identifier == error_mark_node)
22813 decl = error_mark_node;
22814 /* If we know this is a type-name, there's no need to look it
22815 up. */
22816 else if (typename_p)
22817 decl = identifier;
22818 else
22820 tree ambiguous_decls;
22821 /* If we already know that this lookup is ambiguous, then
22822 we've already issued an error message; there's no reason
22823 to check again. */
22824 if (ambiguous_p)
22826 cp_parser_simulate_error (parser);
22827 return error_mark_node;
22829 /* If the next token is a `::', then the name must be a type
22830 name.
22832 [basic.lookup.qual]
22834 During the lookup for a name preceding the :: scope
22835 resolution operator, object, function, and enumerator
22836 names are ignored. */
22837 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22838 tag_type = scope_type;
22839 /* Look up the name. */
22840 decl = cp_parser_lookup_name (parser, identifier,
22841 tag_type,
22842 /*is_template=*/false,
22843 /*is_namespace=*/false,
22844 check_dependency_p,
22845 &ambiguous_decls,
22846 identifier_token->location);
22847 if (ambiguous_decls)
22849 if (cp_parser_parsing_tentatively (parser))
22850 cp_parser_simulate_error (parser);
22851 return error_mark_node;
22855 else
22857 /* Try a template-id. */
22858 decl = cp_parser_template_id (parser, template_keyword_p,
22859 check_dependency_p,
22860 tag_type,
22861 is_declaration);
22862 if (decl == error_mark_node)
22863 return error_mark_node;
22866 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22868 /* If this is a typename, create a TYPENAME_TYPE. */
22869 if (typename_p && decl != error_mark_node)
22871 decl = make_typename_type (scope, decl, typename_type,
22872 /*complain=*/tf_error);
22873 if (decl != error_mark_node)
22874 decl = TYPE_NAME (decl);
22877 decl = strip_using_decl (decl);
22879 /* Check to see that it is really the name of a class. */
22880 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22881 && identifier_p (TREE_OPERAND (decl, 0))
22882 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22883 /* Situations like this:
22885 template <typename T> struct A {
22886 typename T::template X<int>::I i;
22889 are problematic. Is `T::template X<int>' a class-name? The
22890 standard does not seem to be definitive, but there is no other
22891 valid interpretation of the following `::'. Therefore, those
22892 names are considered class-names. */
22894 decl = make_typename_type (scope, decl, tag_type, tf_error);
22895 if (decl != error_mark_node)
22896 decl = TYPE_NAME (decl);
22898 else if (TREE_CODE (decl) != TYPE_DECL
22899 || TREE_TYPE (decl) == error_mark_node
22900 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22901 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22902 /* In Objective-C 2.0, a classname followed by '.' starts a
22903 dot-syntax expression, and it's not a type-name. */
22904 || (c_dialect_objc ()
22905 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22906 && objc_is_class_name (decl)))
22907 decl = error_mark_node;
22909 if (decl == error_mark_node)
22910 cp_parser_error (parser, "expected class-name");
22911 else if (identifier && !parser->scope)
22912 maybe_note_name_used_in_class (identifier, decl);
22914 return decl;
22917 /* Parse a class-specifier.
22919 class-specifier:
22920 class-head { member-specification [opt] }
22922 Returns the TREE_TYPE representing the class. */
22924 static tree
22925 cp_parser_class_specifier_1 (cp_parser* parser)
22927 tree type;
22928 tree attributes = NULL_TREE;
22929 bool nested_name_specifier_p;
22930 unsigned saved_num_template_parameter_lists;
22931 bool saved_in_function_body;
22932 unsigned char in_statement;
22933 bool in_switch_statement_p;
22934 bool saved_in_unbraced_linkage_specification_p;
22935 tree old_scope = NULL_TREE;
22936 tree scope = NULL_TREE;
22937 cp_token *closing_brace;
22939 push_deferring_access_checks (dk_no_deferred);
22941 /* Parse the class-head. */
22942 type = cp_parser_class_head (parser,
22943 &nested_name_specifier_p);
22944 /* If the class-head was a semantic disaster, skip the entire body
22945 of the class. */
22946 if (!type)
22948 cp_parser_skip_to_end_of_block_or_statement (parser);
22949 pop_deferring_access_checks ();
22950 return error_mark_node;
22953 /* Look for the `{'. */
22954 matching_braces braces;
22955 if (!braces.require_open (parser))
22957 pop_deferring_access_checks ();
22958 return error_mark_node;
22961 cp_ensure_no_omp_declare_simd (parser);
22962 cp_ensure_no_oacc_routine (parser);
22964 /* Issue an error message if type-definitions are forbidden here. */
22965 cp_parser_check_type_definition (parser);
22966 /* Remember that we are defining one more class. */
22967 ++parser->num_classes_being_defined;
22968 /* Inside the class, surrounding template-parameter-lists do not
22969 apply. */
22970 saved_num_template_parameter_lists
22971 = parser->num_template_parameter_lists;
22972 parser->num_template_parameter_lists = 0;
22973 /* We are not in a function body. */
22974 saved_in_function_body = parser->in_function_body;
22975 parser->in_function_body = false;
22976 /* Or in a loop. */
22977 in_statement = parser->in_statement;
22978 parser->in_statement = 0;
22979 /* Or in a switch. */
22980 in_switch_statement_p = parser->in_switch_statement_p;
22981 parser->in_switch_statement_p = false;
22982 /* We are not immediately inside an extern "lang" block. */
22983 saved_in_unbraced_linkage_specification_p
22984 = parser->in_unbraced_linkage_specification_p;
22985 parser->in_unbraced_linkage_specification_p = false;
22987 // Associate constraints with the type.
22988 if (flag_concepts)
22989 type = associate_classtype_constraints (type);
22991 /* Start the class. */
22992 if (nested_name_specifier_p)
22994 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22995 old_scope = push_inner_scope (scope);
22997 type = begin_class_definition (type);
22999 if (type == error_mark_node)
23000 /* If the type is erroneous, skip the entire body of the class. */
23001 cp_parser_skip_to_closing_brace (parser);
23002 else
23003 /* Parse the member-specification. */
23004 cp_parser_member_specification_opt (parser);
23006 /* Look for the trailing `}'. */
23007 closing_brace = braces.require_close (parser);
23008 /* Look for trailing attributes to apply to this class. */
23009 if (cp_parser_allow_gnu_extensions_p (parser))
23010 attributes = cp_parser_gnu_attributes_opt (parser);
23011 if (type != error_mark_node)
23012 type = finish_struct (type, attributes);
23013 if (nested_name_specifier_p)
23014 pop_inner_scope (old_scope, scope);
23016 /* We've finished a type definition. Check for the common syntax
23017 error of forgetting a semicolon after the definition. We need to
23018 be careful, as we can't just check for not-a-semicolon and be done
23019 with it; the user might have typed:
23021 class X { } c = ...;
23022 class X { } *p = ...;
23024 and so forth. Instead, enumerate all the possible tokens that
23025 might follow this production; if we don't see one of them, then
23026 complain and silently insert the semicolon. */
23028 cp_token *token = cp_lexer_peek_token (parser->lexer);
23029 bool want_semicolon = true;
23031 if (cp_next_tokens_can_be_std_attribute_p (parser))
23032 /* Don't try to parse c++11 attributes here. As per the
23033 grammar, that should be a task for
23034 cp_parser_decl_specifier_seq. */
23035 want_semicolon = false;
23037 switch (token->type)
23039 case CPP_NAME:
23040 case CPP_SEMICOLON:
23041 case CPP_MULT:
23042 case CPP_AND:
23043 case CPP_OPEN_PAREN:
23044 case CPP_CLOSE_PAREN:
23045 case CPP_COMMA:
23046 want_semicolon = false;
23047 break;
23049 /* While it's legal for type qualifiers and storage class
23050 specifiers to follow type definitions in the grammar, only
23051 compiler testsuites contain code like that. Assume that if
23052 we see such code, then what we're really seeing is a case
23053 like:
23055 class X { }
23056 const <type> var = ...;
23060 class Y { }
23061 static <type> func (...) ...
23063 i.e. the qualifier or specifier applies to the next
23064 declaration. To do so, however, we need to look ahead one
23065 more token to see if *that* token is a type specifier.
23067 This code could be improved to handle:
23069 class Z { }
23070 static const <type> var = ...; */
23071 case CPP_KEYWORD:
23072 if (keyword_is_decl_specifier (token->keyword))
23074 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
23076 /* Handling user-defined types here would be nice, but very
23077 tricky. */
23078 want_semicolon
23079 = (lookahead->type == CPP_KEYWORD
23080 && keyword_begins_type_specifier (lookahead->keyword));
23082 break;
23083 default:
23084 break;
23087 /* If we don't have a type, then something is very wrong and we
23088 shouldn't try to do anything clever. Likewise for not seeing the
23089 closing brace. */
23090 if (closing_brace && TYPE_P (type) && want_semicolon)
23092 /* Locate the closing brace. */
23093 cp_token_position prev
23094 = cp_lexer_previous_token_position (parser->lexer);
23095 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
23096 location_t loc = prev_token->location;
23098 /* We want to suggest insertion of a ';' immediately *after* the
23099 closing brace, so, if we can, offset the location by 1 column. */
23100 location_t next_loc = loc;
23101 if (!linemap_location_from_macro_expansion_p (line_table, loc))
23102 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
23104 rich_location richloc (line_table, next_loc);
23106 /* If we successfully offset the location, suggest the fix-it. */
23107 if (next_loc != loc)
23108 richloc.add_fixit_insert_before (next_loc, ";");
23110 if (CLASSTYPE_DECLARED_CLASS (type))
23111 error_at (&richloc,
23112 "expected %<;%> after class definition");
23113 else if (TREE_CODE (type) == RECORD_TYPE)
23114 error_at (&richloc,
23115 "expected %<;%> after struct definition");
23116 else if (TREE_CODE (type) == UNION_TYPE)
23117 error_at (&richloc,
23118 "expected %<;%> after union definition");
23119 else
23120 gcc_unreachable ();
23122 /* Unget one token and smash it to look as though we encountered
23123 a semicolon in the input stream. */
23124 cp_lexer_set_token_position (parser->lexer, prev);
23125 token = cp_lexer_peek_token (parser->lexer);
23126 token->type = CPP_SEMICOLON;
23127 token->keyword = RID_MAX;
23131 /* If this class is not itself within the scope of another class,
23132 then we need to parse the bodies of all of the queued function
23133 definitions. Note that the queued functions defined in a class
23134 are not always processed immediately following the
23135 class-specifier for that class. Consider:
23137 struct A {
23138 struct B { void f() { sizeof (A); } };
23141 If `f' were processed before the processing of `A' were
23142 completed, there would be no way to compute the size of `A'.
23143 Note that the nesting we are interested in here is lexical --
23144 not the semantic nesting given by TYPE_CONTEXT. In particular,
23145 for:
23147 struct A { struct B; };
23148 struct A::B { void f() { } };
23150 there is no need to delay the parsing of `A::B::f'. */
23151 if (--parser->num_classes_being_defined == 0)
23153 tree decl;
23154 tree class_type = NULL_TREE;
23155 tree pushed_scope = NULL_TREE;
23156 unsigned ix;
23157 cp_default_arg_entry *e;
23158 tree save_ccp, save_ccr;
23160 if (any_erroneous_template_args_p (type))
23162 /* Skip default arguments, NSDMIs, etc, in order to improve
23163 error recovery (c++/71169, c++/71832). */
23164 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23165 vec_safe_truncate (unparsed_nsdmis, 0);
23166 vec_safe_truncate (unparsed_classes, 0);
23167 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23170 /* In a first pass, parse default arguments to the functions.
23171 Then, in a second pass, parse the bodies of the functions.
23172 This two-phased approach handles cases like:
23174 struct S {
23175 void f() { g(); }
23176 void g(int i = 3);
23180 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23182 decl = e->decl;
23183 /* If there are default arguments that have not yet been processed,
23184 take care of them now. */
23185 if (class_type != e->class_type)
23187 if (pushed_scope)
23188 pop_scope (pushed_scope);
23189 class_type = e->class_type;
23190 pushed_scope = push_scope (class_type);
23192 /* Make sure that any template parameters are in scope. */
23193 maybe_begin_member_template_processing (decl);
23194 /* Parse the default argument expressions. */
23195 cp_parser_late_parsing_default_args (parser, decl);
23196 /* Remove any template parameters from the symbol table. */
23197 maybe_end_member_template_processing ();
23199 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23200 /* Now parse any NSDMIs. */
23201 save_ccp = current_class_ptr;
23202 save_ccr = current_class_ref;
23203 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23205 if (class_type != DECL_CONTEXT (decl))
23207 if (pushed_scope)
23208 pop_scope (pushed_scope);
23209 class_type = DECL_CONTEXT (decl);
23210 pushed_scope = push_scope (class_type);
23212 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23213 cp_parser_late_parsing_nsdmi (parser, decl);
23215 vec_safe_truncate (unparsed_nsdmis, 0);
23216 current_class_ptr = save_ccp;
23217 current_class_ref = save_ccr;
23218 if (pushed_scope)
23219 pop_scope (pushed_scope);
23221 /* Now do some post-NSDMI bookkeeping. */
23222 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23223 after_nsdmi_defaulted_late_checks (class_type);
23224 vec_safe_truncate (unparsed_classes, 0);
23225 after_nsdmi_defaulted_late_checks (type);
23227 /* Now parse the body of the functions. */
23228 if (flag_openmp)
23230 /* OpenMP UDRs need to be parsed before all other functions. */
23231 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23232 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23233 cp_parser_late_parsing_for_member (parser, decl);
23234 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23235 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23236 cp_parser_late_parsing_for_member (parser, decl);
23238 else
23239 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23240 cp_parser_late_parsing_for_member (parser, decl);
23241 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23243 else
23244 vec_safe_push (unparsed_classes, type);
23246 /* Put back any saved access checks. */
23247 pop_deferring_access_checks ();
23249 /* Restore saved state. */
23250 parser->in_switch_statement_p = in_switch_statement_p;
23251 parser->in_statement = in_statement;
23252 parser->in_function_body = saved_in_function_body;
23253 parser->num_template_parameter_lists
23254 = saved_num_template_parameter_lists;
23255 parser->in_unbraced_linkage_specification_p
23256 = saved_in_unbraced_linkage_specification_p;
23258 return type;
23261 static tree
23262 cp_parser_class_specifier (cp_parser* parser)
23264 tree ret;
23265 timevar_push (TV_PARSE_STRUCT);
23266 ret = cp_parser_class_specifier_1 (parser);
23267 timevar_pop (TV_PARSE_STRUCT);
23268 return ret;
23271 /* Parse a class-head.
23273 class-head:
23274 class-key identifier [opt] base-clause [opt]
23275 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23276 class-key nested-name-specifier [opt] template-id
23277 base-clause [opt]
23279 class-virt-specifier:
23280 final
23282 GNU Extensions:
23283 class-key attributes identifier [opt] base-clause [opt]
23284 class-key attributes nested-name-specifier identifier base-clause [opt]
23285 class-key attributes nested-name-specifier [opt] template-id
23286 base-clause [opt]
23288 Upon return BASES is initialized to the list of base classes (or
23289 NULL, if there are none) in the same form returned by
23290 cp_parser_base_clause.
23292 Returns the TYPE of the indicated class. Sets
23293 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23294 involving a nested-name-specifier was used, and FALSE otherwise.
23296 Returns error_mark_node if this is not a class-head.
23298 Returns NULL_TREE if the class-head is syntactically valid, but
23299 semantically invalid in a way that means we should skip the entire
23300 body of the class. */
23302 static tree
23303 cp_parser_class_head (cp_parser* parser,
23304 bool* nested_name_specifier_p)
23306 tree nested_name_specifier;
23307 enum tag_types class_key;
23308 tree id = NULL_TREE;
23309 tree type = NULL_TREE;
23310 tree attributes;
23311 tree bases;
23312 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23313 bool template_id_p = false;
23314 bool qualified_p = false;
23315 bool invalid_nested_name_p = false;
23316 bool invalid_explicit_specialization_p = false;
23317 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23318 tree pushed_scope = NULL_TREE;
23319 unsigned num_templates;
23320 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23321 /* Assume no nested-name-specifier will be present. */
23322 *nested_name_specifier_p = false;
23323 /* Assume no template parameter lists will be used in defining the
23324 type. */
23325 num_templates = 0;
23326 parser->colon_corrects_to_scope_p = false;
23328 /* Look for the class-key. */
23329 class_key = cp_parser_class_key (parser);
23330 if (class_key == none_type)
23331 return error_mark_node;
23333 location_t class_head_start_location = input_location;
23335 /* Parse the attributes. */
23336 attributes = cp_parser_attributes_opt (parser);
23338 /* If the next token is `::', that is invalid -- but sometimes
23339 people do try to write:
23341 struct ::S {};
23343 Handle this gracefully by accepting the extra qualifier, and then
23344 issuing an error about it later if this really is a
23345 class-head. If it turns out just to be an elaborated type
23346 specifier, remain silent. */
23347 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23348 qualified_p = true;
23350 push_deferring_access_checks (dk_no_check);
23352 /* Determine the name of the class. Begin by looking for an
23353 optional nested-name-specifier. */
23354 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23355 nested_name_specifier
23356 = cp_parser_nested_name_specifier_opt (parser,
23357 /*typename_keyword_p=*/false,
23358 /*check_dependency_p=*/false,
23359 /*type_p=*/true,
23360 /*is_declaration=*/false);
23361 /* If there was a nested-name-specifier, then there *must* be an
23362 identifier. */
23364 cp_token *bad_template_keyword = NULL;
23366 if (nested_name_specifier)
23368 type_start_token = cp_lexer_peek_token (parser->lexer);
23369 /* Although the grammar says `identifier', it really means
23370 `class-name' or `template-name'. You are only allowed to
23371 define a class that has already been declared with this
23372 syntax.
23374 The proposed resolution for Core Issue 180 says that wherever
23375 you see `class T::X' you should treat `X' as a type-name.
23377 It is OK to define an inaccessible class; for example:
23379 class A { class B; };
23380 class A::B {};
23382 We do not know if we will see a class-name, or a
23383 template-name. We look for a class-name first, in case the
23384 class-name is a template-id; if we looked for the
23385 template-name first we would stop after the template-name. */
23386 cp_parser_parse_tentatively (parser);
23387 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23388 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23389 type = cp_parser_class_name (parser,
23390 /*typename_keyword_p=*/false,
23391 /*template_keyword_p=*/false,
23392 class_type,
23393 /*check_dependency_p=*/false,
23394 /*class_head_p=*/true,
23395 /*is_declaration=*/false);
23396 /* If that didn't work, ignore the nested-name-specifier. */
23397 if (!cp_parser_parse_definitely (parser))
23399 invalid_nested_name_p = true;
23400 type_start_token = cp_lexer_peek_token (parser->lexer);
23401 id = cp_parser_identifier (parser);
23402 if (id == error_mark_node)
23403 id = NULL_TREE;
23405 /* If we could not find a corresponding TYPE, treat this
23406 declaration like an unqualified declaration. */
23407 if (type == error_mark_node)
23408 nested_name_specifier = NULL_TREE;
23409 /* Otherwise, count the number of templates used in TYPE and its
23410 containing scopes. */
23411 else
23412 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23414 /* Otherwise, the identifier is optional. */
23415 else
23417 /* We don't know whether what comes next is a template-id,
23418 an identifier, or nothing at all. */
23419 cp_parser_parse_tentatively (parser);
23420 /* Check for a template-id. */
23421 type_start_token = cp_lexer_peek_token (parser->lexer);
23422 id = cp_parser_template_id (parser,
23423 /*template_keyword_p=*/false,
23424 /*check_dependency_p=*/true,
23425 class_key,
23426 /*is_declaration=*/true);
23427 /* If that didn't work, it could still be an identifier. */
23428 if (!cp_parser_parse_definitely (parser))
23430 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23432 type_start_token = cp_lexer_peek_token (parser->lexer);
23433 id = cp_parser_identifier (parser);
23435 else
23436 id = NULL_TREE;
23438 else
23440 template_id_p = true;
23441 ++num_templates;
23445 pop_deferring_access_checks ();
23447 if (id)
23449 cp_parser_check_for_invalid_template_id (parser, id,
23450 class_key,
23451 type_start_token->location);
23453 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23455 /* If it's not a `:' or a `{' then we can't really be looking at a
23456 class-head, since a class-head only appears as part of a
23457 class-specifier. We have to detect this situation before calling
23458 xref_tag, since that has irreversible side-effects. */
23459 if (!cp_parser_next_token_starts_class_definition_p (parser))
23461 cp_parser_error (parser, "expected %<{%> or %<:%>");
23462 type = error_mark_node;
23463 goto out;
23466 /* At this point, we're going ahead with the class-specifier, even
23467 if some other problem occurs. */
23468 cp_parser_commit_to_tentative_parse (parser);
23469 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23471 cp_parser_error (parser,
23472 "cannot specify %<override%> for a class");
23473 type = error_mark_node;
23474 goto out;
23476 /* Issue the error about the overly-qualified name now. */
23477 if (qualified_p)
23479 cp_parser_error (parser,
23480 "global qualification of class name is invalid");
23481 type = error_mark_node;
23482 goto out;
23484 else if (invalid_nested_name_p)
23486 cp_parser_error (parser,
23487 "qualified name does not name a class");
23488 type = error_mark_node;
23489 goto out;
23491 else if (nested_name_specifier)
23493 tree scope;
23495 if (bad_template_keyword)
23496 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23497 keyword template shall not appear at the top level. */
23498 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23499 "keyword %<template%> not allowed in class-head-name");
23501 /* Reject typedef-names in class heads. */
23502 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23504 error_at (type_start_token->location,
23505 "invalid class name in declaration of %qD",
23506 type);
23507 type = NULL_TREE;
23508 goto done;
23511 /* Figure out in what scope the declaration is being placed. */
23512 scope = current_scope ();
23513 /* If that scope does not contain the scope in which the
23514 class was originally declared, the program is invalid. */
23515 if (scope && !is_ancestor (scope, nested_name_specifier))
23517 if (at_namespace_scope_p ())
23518 error_at (type_start_token->location,
23519 "declaration of %qD in namespace %qD which does not "
23520 "enclose %qD",
23521 type, scope, nested_name_specifier);
23522 else
23523 error_at (type_start_token->location,
23524 "declaration of %qD in %qD which does not enclose %qD",
23525 type, scope, nested_name_specifier);
23526 type = NULL_TREE;
23527 goto done;
23529 /* [dcl.meaning]
23531 A declarator-id shall not be qualified except for the
23532 definition of a ... nested class outside of its class
23533 ... [or] the definition or explicit instantiation of a
23534 class member of a namespace outside of its namespace. */
23535 if (scope == nested_name_specifier)
23537 permerror (nested_name_specifier_token_start->location,
23538 "extra qualification not allowed");
23539 nested_name_specifier = NULL_TREE;
23540 num_templates = 0;
23543 /* An explicit-specialization must be preceded by "template <>". If
23544 it is not, try to recover gracefully. */
23545 if (at_namespace_scope_p ()
23546 && parser->num_template_parameter_lists == 0
23547 && !processing_template_parmlist
23548 && template_id_p)
23550 /* Build a location of this form:
23551 struct typename <ARGS>
23552 ^~~~~~~~~~~~~~~~~~~~~~
23553 with caret==start at the start token, and
23554 finishing at the end of the type. */
23555 location_t reported_loc
23556 = make_location (class_head_start_location,
23557 class_head_start_location,
23558 get_finish (type_start_token->location));
23559 rich_location richloc (line_table, reported_loc);
23560 richloc.add_fixit_insert_before (class_head_start_location,
23561 "template <> ");
23562 error_at (&richloc,
23563 "an explicit specialization must be preceded by"
23564 " %<template <>%>");
23565 invalid_explicit_specialization_p = true;
23566 /* Take the same action that would have been taken by
23567 cp_parser_explicit_specialization. */
23568 ++parser->num_template_parameter_lists;
23569 begin_specialization ();
23571 /* There must be no "return" statements between this point and the
23572 end of this function; set "type "to the correct return value and
23573 use "goto done;" to return. */
23574 /* Make sure that the right number of template parameters were
23575 present. */
23576 if (!cp_parser_check_template_parameters (parser, num_templates,
23577 template_id_p,
23578 type_start_token->location,
23579 /*declarator=*/NULL))
23581 /* If something went wrong, there is no point in even trying to
23582 process the class-definition. */
23583 type = NULL_TREE;
23584 goto done;
23587 /* Look up the type. */
23588 if (template_id_p)
23590 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23591 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23592 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23594 error_at (type_start_token->location,
23595 "function template %qD redeclared as a class template", id);
23596 type = error_mark_node;
23598 else
23600 type = TREE_TYPE (id);
23601 type = maybe_process_partial_specialization (type);
23603 /* Check the scope while we still know whether or not we had a
23604 nested-name-specifier. */
23605 if (type != error_mark_node)
23606 check_unqualified_spec_or_inst (type, type_start_token->location);
23608 if (nested_name_specifier)
23609 pushed_scope = push_scope (nested_name_specifier);
23611 else if (nested_name_specifier)
23613 tree class_type;
23615 /* Given:
23617 template <typename T> struct S { struct T };
23618 template <typename T> struct S<T>::T { };
23620 we will get a TYPENAME_TYPE when processing the definition of
23621 `S::T'. We need to resolve it to the actual type before we
23622 try to define it. */
23623 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23625 class_type = resolve_typename_type (TREE_TYPE (type),
23626 /*only_current_p=*/false);
23627 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23628 type = TYPE_NAME (class_type);
23629 else
23631 cp_parser_error (parser, "could not resolve typename type");
23632 type = error_mark_node;
23636 if (maybe_process_partial_specialization (TREE_TYPE (type))
23637 == error_mark_node)
23639 type = NULL_TREE;
23640 goto done;
23643 class_type = current_class_type;
23644 /* Enter the scope indicated by the nested-name-specifier. */
23645 pushed_scope = push_scope (nested_name_specifier);
23646 /* Get the canonical version of this type. */
23647 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23648 /* Call push_template_decl if it seems like we should be defining a
23649 template either from the template headers or the type we're
23650 defining, so that we diagnose both extra and missing headers. */
23651 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23652 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23653 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23655 type = push_template_decl (type);
23656 if (type == error_mark_node)
23658 type = NULL_TREE;
23659 goto done;
23663 type = TREE_TYPE (type);
23664 *nested_name_specifier_p = true;
23666 else /* The name is not a nested name. */
23668 /* If the class was unnamed, create a dummy name. */
23669 if (!id)
23670 id = make_anon_name ();
23671 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23672 ? ts_within_enclosing_non_class
23673 : ts_current);
23674 type = xref_tag (class_key, id, tag_scope,
23675 parser->num_template_parameter_lists);
23678 /* Indicate whether this class was declared as a `class' or as a
23679 `struct'. */
23680 if (TREE_CODE (type) == RECORD_TYPE)
23681 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23682 cp_parser_check_class_key (class_key, type);
23684 /* If this type was already complete, and we see another definition,
23685 that's an error. */
23686 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23688 error_at (type_start_token->location, "redefinition of %q#T",
23689 type);
23690 inform (location_of (type), "previous definition of %q#T",
23691 type);
23692 type = NULL_TREE;
23693 goto done;
23695 else if (type == error_mark_node)
23696 type = NULL_TREE;
23698 if (type)
23700 /* Apply attributes now, before any use of the class as a template
23701 argument in its base list. */
23702 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23703 fixup_attribute_variants (type);
23706 /* We will have entered the scope containing the class; the names of
23707 base classes should be looked up in that context. For example:
23709 struct A { struct B {}; struct C; };
23710 struct A::C : B {};
23712 is valid. */
23714 /* Get the list of base-classes, if there is one. */
23715 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23717 /* PR59482: enter the class scope so that base-specifiers are looked
23718 up correctly. */
23719 if (type)
23720 pushclass (type);
23721 bases = cp_parser_base_clause (parser);
23722 /* PR59482: get out of the previously pushed class scope so that the
23723 subsequent pops pop the right thing. */
23724 if (type)
23725 popclass ();
23727 else
23728 bases = NULL_TREE;
23730 /* If we're really defining a class, process the base classes.
23731 If they're invalid, fail. */
23732 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23733 xref_basetypes (type, bases);
23735 done:
23736 /* Leave the scope given by the nested-name-specifier. We will
23737 enter the class scope itself while processing the members. */
23738 if (pushed_scope)
23739 pop_scope (pushed_scope);
23741 if (invalid_explicit_specialization_p)
23743 end_specialization ();
23744 --parser->num_template_parameter_lists;
23747 if (type)
23748 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23749 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23750 CLASSTYPE_FINAL (type) = 1;
23751 out:
23752 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23753 return type;
23756 /* Parse a class-key.
23758 class-key:
23759 class
23760 struct
23761 union
23763 Returns the kind of class-key specified, or none_type to indicate
23764 error. */
23766 static enum tag_types
23767 cp_parser_class_key (cp_parser* parser)
23769 cp_token *token;
23770 enum tag_types tag_type;
23772 /* Look for the class-key. */
23773 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23774 if (!token)
23775 return none_type;
23777 /* Check to see if the TOKEN is a class-key. */
23778 tag_type = cp_parser_token_is_class_key (token);
23779 if (!tag_type)
23780 cp_parser_error (parser, "expected class-key");
23781 return tag_type;
23784 /* Parse a type-parameter-key.
23786 type-parameter-key:
23787 class
23788 typename
23791 static void
23792 cp_parser_type_parameter_key (cp_parser* parser)
23794 /* Look for the type-parameter-key. */
23795 enum tag_types tag_type = none_type;
23796 cp_token *token = cp_lexer_peek_token (parser->lexer);
23797 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23799 cp_lexer_consume_token (parser->lexer);
23800 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23801 /* typename is not allowed in a template template parameter
23802 by the standard until C++17. */
23803 pedwarn (token->location, OPT_Wpedantic,
23804 "ISO C++ forbids typename key in template template parameter;"
23805 " use -std=c++17 or -std=gnu++17");
23807 else
23808 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23810 return;
23813 /* Parse an (optional) member-specification.
23815 member-specification:
23816 member-declaration member-specification [opt]
23817 access-specifier : member-specification [opt] */
23819 static void
23820 cp_parser_member_specification_opt (cp_parser* parser)
23822 while (true)
23824 cp_token *token;
23825 enum rid keyword;
23827 /* Peek at the next token. */
23828 token = cp_lexer_peek_token (parser->lexer);
23829 /* If it's a `}', or EOF then we've seen all the members. */
23830 if (token->type == CPP_CLOSE_BRACE
23831 || token->type == CPP_EOF
23832 || token->type == CPP_PRAGMA_EOL)
23833 break;
23835 /* See if this token is a keyword. */
23836 keyword = token->keyword;
23837 switch (keyword)
23839 case RID_PUBLIC:
23840 case RID_PROTECTED:
23841 case RID_PRIVATE:
23842 /* Consume the access-specifier. */
23843 cp_lexer_consume_token (parser->lexer);
23844 /* Remember which access-specifier is active. */
23845 current_access_specifier = token->u.value;
23846 /* Look for the `:'. */
23847 cp_parser_require (parser, CPP_COLON, RT_COLON);
23848 break;
23850 default:
23851 /* Accept #pragmas at class scope. */
23852 if (token->type == CPP_PRAGMA)
23854 cp_parser_pragma (parser, pragma_member, NULL);
23855 break;
23858 /* Otherwise, the next construction must be a
23859 member-declaration. */
23860 cp_parser_member_declaration (parser);
23865 /* Parse a member-declaration.
23867 member-declaration:
23868 decl-specifier-seq [opt] member-declarator-list [opt] ;
23869 function-definition ; [opt]
23870 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23871 using-declaration
23872 template-declaration
23873 alias-declaration
23875 member-declarator-list:
23876 member-declarator
23877 member-declarator-list , member-declarator
23879 member-declarator:
23880 declarator pure-specifier [opt]
23881 declarator constant-initializer [opt]
23882 identifier [opt] : constant-expression
23884 GNU Extensions:
23886 member-declaration:
23887 __extension__ member-declaration
23889 member-declarator:
23890 declarator attributes [opt] pure-specifier [opt]
23891 declarator attributes [opt] constant-initializer [opt]
23892 identifier [opt] attributes [opt] : constant-expression
23894 C++0x Extensions:
23896 member-declaration:
23897 static_assert-declaration */
23899 static void
23900 cp_parser_member_declaration (cp_parser* parser)
23902 cp_decl_specifier_seq decl_specifiers;
23903 tree prefix_attributes;
23904 tree decl;
23905 int declares_class_or_enum;
23906 bool friend_p;
23907 cp_token *token = NULL;
23908 cp_token *decl_spec_token_start = NULL;
23909 cp_token *initializer_token_start = NULL;
23910 int saved_pedantic;
23911 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23913 /* Check for the `__extension__' keyword. */
23914 if (cp_parser_extension_opt (parser, &saved_pedantic))
23916 /* Recurse. */
23917 cp_parser_member_declaration (parser);
23918 /* Restore the old value of the PEDANTIC flag. */
23919 pedantic = saved_pedantic;
23921 return;
23924 /* Check for a template-declaration. */
23925 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23927 /* An explicit specialization here is an error condition, and we
23928 expect the specialization handler to detect and report this. */
23929 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23930 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23931 cp_parser_explicit_specialization (parser);
23932 else
23933 cp_parser_template_declaration (parser, /*member_p=*/true);
23935 return;
23937 /* Check for a template introduction. */
23938 else if (cp_parser_template_declaration_after_export (parser, true))
23939 return;
23941 /* Check for a using-declaration. */
23942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23944 if (cxx_dialect < cxx11)
23946 /* Parse the using-declaration. */
23947 cp_parser_using_declaration (parser,
23948 /*access_declaration_p=*/false);
23949 return;
23951 else
23953 tree decl;
23954 bool alias_decl_expected;
23955 cp_parser_parse_tentatively (parser);
23956 decl = cp_parser_alias_declaration (parser);
23957 /* Note that if we actually see the '=' token after the
23958 identifier, cp_parser_alias_declaration commits the
23959 tentative parse. In that case, we really expect an
23960 alias-declaration. Otherwise, we expect a using
23961 declaration. */
23962 alias_decl_expected =
23963 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23964 cp_parser_parse_definitely (parser);
23966 if (alias_decl_expected)
23967 finish_member_declaration (decl);
23968 else
23969 cp_parser_using_declaration (parser,
23970 /*access_declaration_p=*/false);
23971 return;
23975 /* Check for @defs. */
23976 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23978 tree ivar, member;
23979 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23980 ivar = ivar_chains;
23981 while (ivar)
23983 member = ivar;
23984 ivar = TREE_CHAIN (member);
23985 TREE_CHAIN (member) = NULL_TREE;
23986 finish_member_declaration (member);
23988 return;
23991 /* If the next token is `static_assert' we have a static assertion. */
23992 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23994 cp_parser_static_assert (parser, /*member_p=*/true);
23995 return;
23998 parser->colon_corrects_to_scope_p = false;
24000 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
24001 goto out;
24003 /* Parse the decl-specifier-seq. */
24004 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24005 cp_parser_decl_specifier_seq (parser,
24006 CP_PARSER_FLAGS_OPTIONAL,
24007 &decl_specifiers,
24008 &declares_class_or_enum);
24009 /* Check for an invalid type-name. */
24010 if (!decl_specifiers.any_type_specifiers_p
24011 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24012 goto out;
24013 /* If there is no declarator, then the decl-specifier-seq should
24014 specify a type. */
24015 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24017 /* If there was no decl-specifier-seq, and the next token is a
24018 `;', then we have something like:
24020 struct S { ; };
24022 [class.mem]
24024 Each member-declaration shall declare at least one member
24025 name of the class. */
24026 if (!decl_specifiers.any_specifiers_p)
24028 cp_token *token = cp_lexer_peek_token (parser->lexer);
24029 if (!in_system_header_at (token->location))
24031 gcc_rich_location richloc (token->location);
24032 richloc.add_fixit_remove ();
24033 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
24036 else
24038 tree type;
24040 /* See if this declaration is a friend. */
24041 friend_p = cp_parser_friend_p (&decl_specifiers);
24042 /* If there were decl-specifiers, check to see if there was
24043 a class-declaration. */
24044 type = check_tag_decl (&decl_specifiers,
24045 /*explicit_type_instantiation_p=*/false);
24046 /* Nested classes have already been added to the class, but
24047 a `friend' needs to be explicitly registered. */
24048 if (friend_p)
24050 /* If the `friend' keyword was present, the friend must
24051 be introduced with a class-key. */
24052 if (!declares_class_or_enum && cxx_dialect < cxx11)
24053 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
24054 "in C++03 a class-key must be used "
24055 "when declaring a friend");
24056 /* In this case:
24058 template <typename T> struct A {
24059 friend struct A<T>::B;
24062 A<T>::B will be represented by a TYPENAME_TYPE, and
24063 therefore not recognized by check_tag_decl. */
24064 if (!type)
24066 type = decl_specifiers.type;
24067 if (type && TREE_CODE (type) == TYPE_DECL)
24068 type = TREE_TYPE (type);
24070 if (!type || !TYPE_P (type))
24071 error_at (decl_spec_token_start->location,
24072 "friend declaration does not name a class or "
24073 "function");
24074 else
24075 make_friend_class (current_class_type, type,
24076 /*complain=*/true);
24078 /* If there is no TYPE, an error message will already have
24079 been issued. */
24080 else if (!type || type == error_mark_node)
24082 /* An anonymous aggregate has to be handled specially; such
24083 a declaration really declares a data member (with a
24084 particular type), as opposed to a nested class. */
24085 else if (ANON_AGGR_TYPE_P (type))
24087 /* C++11 9.5/6. */
24088 if (decl_specifiers.storage_class != sc_none)
24089 error_at (decl_spec_token_start->location,
24090 "a storage class on an anonymous aggregate "
24091 "in class scope is not allowed");
24093 /* Remove constructors and such from TYPE, now that we
24094 know it is an anonymous aggregate. */
24095 fixup_anonymous_aggr (type);
24096 /* And make the corresponding data member. */
24097 decl = build_decl (decl_spec_token_start->location,
24098 FIELD_DECL, NULL_TREE, type);
24099 /* Add it to the class. */
24100 finish_member_declaration (decl);
24102 else
24103 cp_parser_check_access_in_redeclaration
24104 (TYPE_NAME (type),
24105 decl_spec_token_start->location);
24108 else
24110 bool assume_semicolon = false;
24112 /* Clear attributes from the decl_specifiers but keep them
24113 around as prefix attributes that apply them to the entity
24114 being declared. */
24115 prefix_attributes = decl_specifiers.attributes;
24116 decl_specifiers.attributes = NULL_TREE;
24118 /* See if these declarations will be friends. */
24119 friend_p = cp_parser_friend_p (&decl_specifiers);
24121 /* Keep going until we hit the `;' at the end of the
24122 declaration. */
24123 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24125 tree attributes = NULL_TREE;
24126 tree first_attribute;
24127 tree initializer;
24128 bool named_bitfld = false;
24130 /* Peek at the next token. */
24131 token = cp_lexer_peek_token (parser->lexer);
24133 /* The following code wants to know early if it is a bit-field
24134 or some other declaration. Attributes can appear before
24135 the `:' token. Skip over them without consuming any tokens
24136 to peek if they are followed by `:'. */
24137 if (cp_next_tokens_can_be_attribute_p (parser)
24138 || (token->type == CPP_NAME
24139 && cp_nth_tokens_can_be_attribute_p (parser, 2)
24140 && (named_bitfld = true)))
24142 size_t n
24143 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
24144 token = cp_lexer_peek_nth_token (parser->lexer, n);
24147 /* Check for a bitfield declaration. */
24148 if (token->type == CPP_COLON
24149 || (token->type == CPP_NAME
24150 && token == cp_lexer_peek_token (parser->lexer)
24151 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
24152 && (named_bitfld = true)))
24154 tree identifier;
24155 tree width;
24156 tree late_attributes = NULL_TREE;
24157 location_t id_location
24158 = cp_lexer_peek_token (parser->lexer)->location;
24160 if (named_bitfld)
24161 identifier = cp_parser_identifier (parser);
24162 else
24163 identifier = NULL_TREE;
24165 /* Look for attributes that apply to the bitfield. */
24166 attributes = cp_parser_attributes_opt (parser);
24168 /* Consume the `:' token. */
24169 cp_lexer_consume_token (parser->lexer);
24171 /* Get the width of the bitfield. */
24172 width = cp_parser_constant_expression (parser, false, NULL,
24173 cxx_dialect >= cxx11);
24175 /* In C++2A and as extension for C++11 and above we allow
24176 default member initializers for bit-fields. */
24177 initializer = NULL_TREE;
24178 if (cxx_dialect >= cxx11
24179 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24180 || cp_lexer_next_token_is (parser->lexer,
24181 CPP_OPEN_BRACE)))
24183 location_t loc
24184 = cp_lexer_peek_token (parser->lexer)->location;
24185 if (cxx_dialect < cxx2a
24186 && !in_system_header_at (loc)
24187 && identifier != NULL_TREE)
24188 pedwarn (loc, 0,
24189 "default member initializers for bit-fields "
24190 "only available with -std=c++2a or "
24191 "-std=gnu++2a");
24193 initializer = cp_parser_save_nsdmi (parser);
24194 if (identifier == NULL_TREE)
24196 error_at (loc, "default member initializer for "
24197 "unnamed bit-field");
24198 initializer = NULL_TREE;
24201 else
24203 /* Look for attributes that apply to the bitfield after
24204 the `:' token and width. This is where GCC used to
24205 parse attributes in the past, pedwarn if there is
24206 a std attribute. */
24207 if (cp_next_tokens_can_be_std_attribute_p (parser))
24208 pedwarn (input_location, OPT_Wpedantic,
24209 "ISO C++ allows bit-field attributes only "
24210 "before the %<:%> token");
24212 late_attributes = cp_parser_attributes_opt (parser);
24215 attributes = attr_chainon (attributes, late_attributes);
24217 /* Remember which attributes are prefix attributes and
24218 which are not. */
24219 first_attribute = attributes;
24220 /* Combine the attributes. */
24221 attributes = attr_chainon (prefix_attributes, attributes);
24223 /* Create the bitfield declaration. */
24224 decl = grokbitfield (identifier
24225 ? make_id_declarator (NULL_TREE,
24226 identifier,
24227 sfk_none,
24228 id_location)
24229 : NULL,
24230 &decl_specifiers,
24231 width, initializer,
24232 attributes);
24234 else
24236 cp_declarator *declarator;
24237 tree asm_specification;
24238 int ctor_dtor_or_conv_p;
24240 /* Parse the declarator. */
24241 declarator
24242 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24243 &ctor_dtor_or_conv_p,
24244 /*parenthesized_p=*/NULL,
24245 /*member_p=*/true,
24246 friend_p);
24248 /* If something went wrong parsing the declarator, make sure
24249 that we at least consume some tokens. */
24250 if (declarator == cp_error_declarator)
24252 /* Skip to the end of the statement. */
24253 cp_parser_skip_to_end_of_statement (parser);
24254 /* If the next token is not a semicolon, that is
24255 probably because we just skipped over the body of
24256 a function. So, we consume a semicolon if
24257 present, but do not issue an error message if it
24258 is not present. */
24259 if (cp_lexer_next_token_is (parser->lexer,
24260 CPP_SEMICOLON))
24261 cp_lexer_consume_token (parser->lexer);
24262 goto out;
24265 if (declares_class_or_enum & 2)
24266 cp_parser_check_for_definition_in_return_type
24267 (declarator, decl_specifiers.type,
24268 decl_specifiers.locations[ds_type_spec]);
24270 /* Look for an asm-specification. */
24271 asm_specification = cp_parser_asm_specification_opt (parser);
24272 /* Look for attributes that apply to the declaration. */
24273 attributes = cp_parser_attributes_opt (parser);
24274 /* Remember which attributes are prefix attributes and
24275 which are not. */
24276 first_attribute = attributes;
24277 /* Combine the attributes. */
24278 attributes = attr_chainon (prefix_attributes, attributes);
24280 /* If it's an `=', then we have a constant-initializer or a
24281 pure-specifier. It is not correct to parse the
24282 initializer before registering the member declaration
24283 since the member declaration should be in scope while
24284 its initializer is processed. However, the rest of the
24285 front end does not yet provide an interface that allows
24286 us to handle this correctly. */
24287 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24289 /* In [class.mem]:
24291 A pure-specifier shall be used only in the declaration of
24292 a virtual function.
24294 A member-declarator can contain a constant-initializer
24295 only if it declares a static member of integral or
24296 enumeration type.
24298 Therefore, if the DECLARATOR is for a function, we look
24299 for a pure-specifier; otherwise, we look for a
24300 constant-initializer. When we call `grokfield', it will
24301 perform more stringent semantics checks. */
24302 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24303 if (function_declarator_p (declarator)
24304 || (decl_specifiers.type
24305 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24306 && declarator->kind == cdk_id
24307 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24308 == FUNCTION_TYPE)))
24309 initializer = cp_parser_pure_specifier (parser);
24310 else if (decl_specifiers.storage_class != sc_static)
24311 initializer = cp_parser_save_nsdmi (parser);
24312 else if (cxx_dialect >= cxx11)
24314 bool nonconst;
24315 /* Don't require a constant rvalue in C++11, since we
24316 might want a reference constant. We'll enforce
24317 constancy later. */
24318 cp_lexer_consume_token (parser->lexer);
24319 /* Parse the initializer. */
24320 initializer = cp_parser_initializer_clause (parser,
24321 &nonconst);
24323 else
24324 /* Parse the initializer. */
24325 initializer = cp_parser_constant_initializer (parser);
24327 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24328 && !function_declarator_p (declarator))
24330 bool x;
24331 if (decl_specifiers.storage_class != sc_static)
24332 initializer = cp_parser_save_nsdmi (parser);
24333 else
24334 initializer = cp_parser_initializer (parser, &x, &x);
24336 /* Otherwise, there is no initializer. */
24337 else
24338 initializer = NULL_TREE;
24340 /* See if we are probably looking at a function
24341 definition. We are certainly not looking at a
24342 member-declarator. Calling `grokfield' has
24343 side-effects, so we must not do it unless we are sure
24344 that we are looking at a member-declarator. */
24345 if (cp_parser_token_starts_function_definition_p
24346 (cp_lexer_peek_token (parser->lexer)))
24348 /* The grammar does not allow a pure-specifier to be
24349 used when a member function is defined. (It is
24350 possible that this fact is an oversight in the
24351 standard, since a pure function may be defined
24352 outside of the class-specifier. */
24353 if (initializer && initializer_token_start)
24354 error_at (initializer_token_start->location,
24355 "pure-specifier on function-definition");
24356 decl = cp_parser_save_member_function_body (parser,
24357 &decl_specifiers,
24358 declarator,
24359 attributes);
24360 if (parser->fully_implicit_function_template_p)
24361 decl = finish_fully_implicit_template (parser, decl);
24362 /* If the member was not a friend, declare it here. */
24363 if (!friend_p)
24364 finish_member_declaration (decl);
24365 /* Peek at the next token. */
24366 token = cp_lexer_peek_token (parser->lexer);
24367 /* If the next token is a semicolon, consume it. */
24368 if (token->type == CPP_SEMICOLON)
24370 location_t semicolon_loc
24371 = cp_lexer_consume_token (parser->lexer)->location;
24372 gcc_rich_location richloc (semicolon_loc);
24373 richloc.add_fixit_remove ();
24374 warning_at (&richloc, OPT_Wextra_semi,
24375 "extra %<;%> after in-class "
24376 "function definition");
24378 goto out;
24380 else
24381 if (declarator->kind == cdk_function)
24382 declarator->id_loc = token->location;
24383 /* Create the declaration. */
24384 decl = grokfield (declarator, &decl_specifiers,
24385 initializer, /*init_const_expr_p=*/true,
24386 asm_specification, attributes);
24387 if (parser->fully_implicit_function_template_p)
24389 if (friend_p)
24390 finish_fully_implicit_template (parser, 0);
24391 else
24392 decl = finish_fully_implicit_template (parser, decl);
24396 cp_finalize_omp_declare_simd (parser, decl);
24397 cp_finalize_oacc_routine (parser, decl, false);
24399 /* Reset PREFIX_ATTRIBUTES. */
24400 if (attributes != error_mark_node)
24402 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24403 attributes = TREE_CHAIN (attributes);
24404 if (attributes)
24405 TREE_CHAIN (attributes) = NULL_TREE;
24408 /* If there is any qualification still in effect, clear it
24409 now; we will be starting fresh with the next declarator. */
24410 parser->scope = NULL_TREE;
24411 parser->qualifying_scope = NULL_TREE;
24412 parser->object_scope = NULL_TREE;
24413 /* If it's a `,', then there are more declarators. */
24414 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24416 cp_lexer_consume_token (parser->lexer);
24417 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24419 cp_token *token = cp_lexer_previous_token (parser->lexer);
24420 gcc_rich_location richloc (token->location);
24421 richloc.add_fixit_remove ();
24422 error_at (&richloc, "stray %<,%> at end of "
24423 "member declaration");
24426 /* If the next token isn't a `;', then we have a parse error. */
24427 else if (cp_lexer_next_token_is_not (parser->lexer,
24428 CPP_SEMICOLON))
24430 /* The next token might be a ways away from where the
24431 actual semicolon is missing. Find the previous token
24432 and use that for our error position. */
24433 cp_token *token = cp_lexer_previous_token (parser->lexer);
24434 gcc_rich_location richloc (token->location);
24435 richloc.add_fixit_insert_after (";");
24436 error_at (&richloc, "expected %<;%> at end of "
24437 "member declaration");
24439 /* Assume that the user meant to provide a semicolon. If
24440 we were to cp_parser_skip_to_end_of_statement, we might
24441 skip to a semicolon inside a member function definition
24442 and issue nonsensical error messages. */
24443 assume_semicolon = true;
24446 if (decl)
24448 /* Add DECL to the list of members. */
24449 if (!friend_p
24450 /* Explicitly include, eg, NSDMIs, for better error
24451 recovery (c++/58650). */
24452 || !DECL_DECLARES_FUNCTION_P (decl))
24453 finish_member_declaration (decl);
24455 if (TREE_CODE (decl) == FUNCTION_DECL)
24456 cp_parser_save_default_args (parser, decl);
24457 else if (TREE_CODE (decl) == FIELD_DECL
24458 && DECL_INITIAL (decl))
24459 /* Add DECL to the queue of NSDMI to be parsed later. */
24460 vec_safe_push (unparsed_nsdmis, decl);
24463 if (assume_semicolon)
24464 goto out;
24468 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24469 out:
24470 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24473 /* Parse a pure-specifier.
24475 pure-specifier:
24478 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24479 Otherwise, ERROR_MARK_NODE is returned. */
24481 static tree
24482 cp_parser_pure_specifier (cp_parser* parser)
24484 cp_token *token;
24486 /* Look for the `=' token. */
24487 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24488 return error_mark_node;
24489 /* Look for the `0' token. */
24490 token = cp_lexer_peek_token (parser->lexer);
24492 if (token->type == CPP_EOF
24493 || token->type == CPP_PRAGMA_EOL)
24494 return error_mark_node;
24496 cp_lexer_consume_token (parser->lexer);
24498 /* Accept = default or = delete in c++0x mode. */
24499 if (token->keyword == RID_DEFAULT
24500 || token->keyword == RID_DELETE)
24502 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24503 return token->u.value;
24506 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24507 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24509 cp_parser_error (parser,
24510 "invalid pure specifier (only %<= 0%> is allowed)");
24511 cp_parser_skip_to_end_of_statement (parser);
24512 return error_mark_node;
24514 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24516 error_at (token->location, "templates may not be %<virtual%>");
24517 return error_mark_node;
24520 return integer_zero_node;
24523 /* Parse a constant-initializer.
24525 constant-initializer:
24526 = constant-expression
24528 Returns a representation of the constant-expression. */
24530 static tree
24531 cp_parser_constant_initializer (cp_parser* parser)
24533 /* Look for the `=' token. */
24534 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24535 return error_mark_node;
24537 /* It is invalid to write:
24539 struct S { static const int i = { 7 }; };
24542 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24544 cp_parser_error (parser,
24545 "a brace-enclosed initializer is not allowed here");
24546 /* Consume the opening brace. */
24547 matching_braces braces;
24548 braces.consume_open (parser);
24549 /* Skip the initializer. */
24550 cp_parser_skip_to_closing_brace (parser);
24551 /* Look for the trailing `}'. */
24552 braces.require_close (parser);
24554 return error_mark_node;
24557 return cp_parser_constant_expression (parser);
24560 /* Derived classes [gram.class.derived] */
24562 /* Parse a base-clause.
24564 base-clause:
24565 : base-specifier-list
24567 base-specifier-list:
24568 base-specifier ... [opt]
24569 base-specifier-list , base-specifier ... [opt]
24571 Returns a TREE_LIST representing the base-classes, in the order in
24572 which they were declared. The representation of each node is as
24573 described by cp_parser_base_specifier.
24575 In the case that no bases are specified, this function will return
24576 NULL_TREE, not ERROR_MARK_NODE. */
24578 static tree
24579 cp_parser_base_clause (cp_parser* parser)
24581 tree bases = NULL_TREE;
24583 /* Look for the `:' that begins the list. */
24584 cp_parser_require (parser, CPP_COLON, RT_COLON);
24586 /* Scan the base-specifier-list. */
24587 while (true)
24589 cp_token *token;
24590 tree base;
24591 bool pack_expansion_p = false;
24593 /* Look for the base-specifier. */
24594 base = cp_parser_base_specifier (parser);
24595 /* Look for the (optional) ellipsis. */
24596 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24598 /* Consume the `...'. */
24599 cp_lexer_consume_token (parser->lexer);
24601 pack_expansion_p = true;
24604 /* Add BASE to the front of the list. */
24605 if (base && base != error_mark_node)
24607 if (pack_expansion_p)
24608 /* Make this a pack expansion type. */
24609 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24611 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24613 TREE_CHAIN (base) = bases;
24614 bases = base;
24617 /* Peek at the next token. */
24618 token = cp_lexer_peek_token (parser->lexer);
24619 /* If it's not a comma, then the list is complete. */
24620 if (token->type != CPP_COMMA)
24621 break;
24622 /* Consume the `,'. */
24623 cp_lexer_consume_token (parser->lexer);
24626 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24627 base class had a qualified name. However, the next name that
24628 appears is certainly not qualified. */
24629 parser->scope = NULL_TREE;
24630 parser->qualifying_scope = NULL_TREE;
24631 parser->object_scope = NULL_TREE;
24633 return nreverse (bases);
24636 /* Parse a base-specifier.
24638 base-specifier:
24639 :: [opt] nested-name-specifier [opt] class-name
24640 virtual access-specifier [opt] :: [opt] nested-name-specifier
24641 [opt] class-name
24642 access-specifier virtual [opt] :: [opt] nested-name-specifier
24643 [opt] class-name
24645 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24646 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24647 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24648 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24650 static tree
24651 cp_parser_base_specifier (cp_parser* parser)
24653 cp_token *token;
24654 bool done = false;
24655 bool virtual_p = false;
24656 bool duplicate_virtual_error_issued_p = false;
24657 bool duplicate_access_error_issued_p = false;
24658 bool class_scope_p, template_p;
24659 tree access = access_default_node;
24660 tree type;
24662 /* Process the optional `virtual' and `access-specifier'. */
24663 while (!done)
24665 /* Peek at the next token. */
24666 token = cp_lexer_peek_token (parser->lexer);
24667 /* Process `virtual'. */
24668 switch (token->keyword)
24670 case RID_VIRTUAL:
24671 /* If `virtual' appears more than once, issue an error. */
24672 if (virtual_p && !duplicate_virtual_error_issued_p)
24674 cp_parser_error (parser,
24675 "%<virtual%> specified more than once in base-specifier");
24676 duplicate_virtual_error_issued_p = true;
24679 virtual_p = true;
24681 /* Consume the `virtual' token. */
24682 cp_lexer_consume_token (parser->lexer);
24684 break;
24686 case RID_PUBLIC:
24687 case RID_PROTECTED:
24688 case RID_PRIVATE:
24689 /* If more than one access specifier appears, issue an
24690 error. */
24691 if (access != access_default_node
24692 && !duplicate_access_error_issued_p)
24694 cp_parser_error (parser,
24695 "more than one access specifier in base-specifier");
24696 duplicate_access_error_issued_p = true;
24699 access = ridpointers[(int) token->keyword];
24701 /* Consume the access-specifier. */
24702 cp_lexer_consume_token (parser->lexer);
24704 break;
24706 default:
24707 done = true;
24708 break;
24711 /* It is not uncommon to see programs mechanically, erroneously, use
24712 the 'typename' keyword to denote (dependent) qualified types
24713 as base classes. */
24714 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24716 token = cp_lexer_peek_token (parser->lexer);
24717 if (!processing_template_decl)
24718 error_at (token->location,
24719 "keyword %<typename%> not allowed outside of templates");
24720 else
24721 error_at (token->location,
24722 "keyword %<typename%> not allowed in this context "
24723 "(the base class is implicitly a type)");
24724 cp_lexer_consume_token (parser->lexer);
24727 /* Look for the optional `::' operator. */
24728 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24729 /* Look for the nested-name-specifier. The simplest way to
24730 implement:
24732 [temp.res]
24734 The keyword `typename' is not permitted in a base-specifier or
24735 mem-initializer; in these contexts a qualified name that
24736 depends on a template-parameter is implicitly assumed to be a
24737 type name.
24739 is to pretend that we have seen the `typename' keyword at this
24740 point. */
24741 cp_parser_nested_name_specifier_opt (parser,
24742 /*typename_keyword_p=*/true,
24743 /*check_dependency_p=*/true,
24744 /*type_p=*/true,
24745 /*is_declaration=*/true);
24746 /* If the base class is given by a qualified name, assume that names
24747 we see are type names or templates, as appropriate. */
24748 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24749 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24751 if (!parser->scope
24752 && cp_lexer_next_token_is_decltype (parser->lexer))
24753 /* DR 950 allows decltype as a base-specifier. */
24754 type = cp_parser_decltype (parser);
24755 else
24757 /* Otherwise, look for the class-name. */
24758 type = cp_parser_class_name (parser,
24759 class_scope_p,
24760 template_p,
24761 typename_type,
24762 /*check_dependency_p=*/true,
24763 /*class_head_p=*/false,
24764 /*is_declaration=*/true);
24765 type = TREE_TYPE (type);
24768 if (type == error_mark_node)
24769 return error_mark_node;
24771 return finish_base_specifier (type, access, virtual_p);
24774 /* Exception handling [gram.exception] */
24776 /* Parse an (optional) noexcept-specification.
24778 noexcept-specification:
24779 noexcept ( constant-expression ) [opt]
24781 If no noexcept-specification is present, returns NULL_TREE.
24782 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24783 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24784 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24785 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24786 in which case a boolean condition is returned instead. */
24788 static tree
24789 cp_parser_noexcept_specification_opt (cp_parser* parser,
24790 bool require_constexpr,
24791 bool* consumed_expr,
24792 bool return_cond)
24794 cp_token *token;
24795 const char *saved_message;
24797 /* Peek at the next token. */
24798 token = cp_lexer_peek_token (parser->lexer);
24800 /* Is it a noexcept-specification? */
24801 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24803 tree expr;
24804 cp_lexer_consume_token (parser->lexer);
24806 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24808 matching_parens parens;
24809 parens.consume_open (parser);
24811 tree save_ccp = current_class_ptr;
24812 tree save_ccr = current_class_ref;
24814 if (current_class_type)
24815 inject_this_parameter (current_class_type, TYPE_UNQUALIFIED);
24817 if (require_constexpr)
24819 /* Types may not be defined in an exception-specification. */
24820 saved_message = parser->type_definition_forbidden_message;
24821 parser->type_definition_forbidden_message
24822 = G_("types may not be defined in an exception-specification");
24824 expr = cp_parser_constant_expression (parser);
24826 /* Restore the saved message. */
24827 parser->type_definition_forbidden_message = saved_message;
24829 else
24831 expr = cp_parser_expression (parser);
24832 *consumed_expr = true;
24835 parens.require_close (parser);
24837 current_class_ptr = save_ccp;
24838 current_class_ref = save_ccr;
24840 else
24842 expr = boolean_true_node;
24843 if (!require_constexpr)
24844 *consumed_expr = false;
24847 /* We cannot build a noexcept-spec right away because this will check
24848 that expr is a constexpr. */
24849 if (!return_cond)
24850 return build_noexcept_spec (expr, tf_warning_or_error);
24851 else
24852 return expr;
24854 else
24855 return NULL_TREE;
24858 /* Parse an (optional) exception-specification.
24860 exception-specification:
24861 throw ( type-id-list [opt] )
24863 Returns a TREE_LIST representing the exception-specification. The
24864 TREE_VALUE of each node is a type. */
24866 static tree
24867 cp_parser_exception_specification_opt (cp_parser* parser)
24869 cp_token *token;
24870 tree type_id_list;
24871 const char *saved_message;
24873 /* Peek at the next token. */
24874 token = cp_lexer_peek_token (parser->lexer);
24876 /* Is it a noexcept-specification? */
24877 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24878 false);
24879 if (type_id_list != NULL_TREE)
24880 return type_id_list;
24882 /* If it's not `throw', then there's no exception-specification. */
24883 if (!cp_parser_is_keyword (token, RID_THROW))
24884 return NULL_TREE;
24886 location_t loc = token->location;
24888 /* Consume the `throw'. */
24889 cp_lexer_consume_token (parser->lexer);
24891 /* Look for the `('. */
24892 matching_parens parens;
24893 parens.require_open (parser);
24895 /* Peek at the next token. */
24896 token = cp_lexer_peek_token (parser->lexer);
24897 /* If it's not a `)', then there is a type-id-list. */
24898 if (token->type != CPP_CLOSE_PAREN)
24900 /* Types may not be defined in an exception-specification. */
24901 saved_message = parser->type_definition_forbidden_message;
24902 parser->type_definition_forbidden_message
24903 = G_("types may not be defined in an exception-specification");
24904 /* Parse the type-id-list. */
24905 type_id_list = cp_parser_type_id_list (parser);
24906 /* Restore the saved message. */
24907 parser->type_definition_forbidden_message = saved_message;
24909 if (cxx_dialect >= cxx17)
24911 error_at (loc, "ISO C++17 does not allow dynamic exception "
24912 "specifications");
24913 type_id_list = NULL_TREE;
24915 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24916 warning_at (loc, OPT_Wdeprecated,
24917 "dynamic exception specifications are deprecated in "
24918 "C++11");
24920 /* In C++17, throw() is equivalent to noexcept (true). throw()
24921 is deprecated in C++11 and above as well, but is still widely used,
24922 so don't warn about it yet. */
24923 else if (cxx_dialect >= cxx17)
24924 type_id_list = noexcept_true_spec;
24925 else
24926 type_id_list = empty_except_spec;
24928 /* Look for the `)'. */
24929 parens.require_close (parser);
24931 return type_id_list;
24934 /* Parse an (optional) type-id-list.
24936 type-id-list:
24937 type-id ... [opt]
24938 type-id-list , type-id ... [opt]
24940 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24941 in the order that the types were presented. */
24943 static tree
24944 cp_parser_type_id_list (cp_parser* parser)
24946 tree types = NULL_TREE;
24948 while (true)
24950 cp_token *token;
24951 tree type;
24953 token = cp_lexer_peek_token (parser->lexer);
24955 /* Get the next type-id. */
24956 type = cp_parser_type_id (parser);
24957 /* Check for invalid 'auto'. */
24958 if (flag_concepts && type_uses_auto (type))
24960 error_at (token->location,
24961 "invalid use of %<auto%> in exception-specification");
24962 type = error_mark_node;
24964 /* Parse the optional ellipsis. */
24965 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24967 /* Consume the `...'. */
24968 cp_lexer_consume_token (parser->lexer);
24970 /* Turn the type into a pack expansion expression. */
24971 type = make_pack_expansion (type);
24973 /* Add it to the list. */
24974 types = add_exception_specifier (types, type, /*complain=*/1);
24975 /* Peek at the next token. */
24976 token = cp_lexer_peek_token (parser->lexer);
24977 /* If it is not a `,', we are done. */
24978 if (token->type != CPP_COMMA)
24979 break;
24980 /* Consume the `,'. */
24981 cp_lexer_consume_token (parser->lexer);
24984 return nreverse (types);
24987 /* Parse a try-block.
24989 try-block:
24990 try compound-statement handler-seq */
24992 static tree
24993 cp_parser_try_block (cp_parser* parser)
24995 tree try_block;
24997 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24998 if (parser->in_function_body
24999 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
25000 error ("%<try%> in %<constexpr%> function");
25002 try_block = begin_try_block ();
25003 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
25004 finish_try_block (try_block);
25005 cp_parser_handler_seq (parser);
25006 finish_handler_sequence (try_block);
25008 return try_block;
25011 /* Parse a function-try-block.
25013 function-try-block:
25014 try ctor-initializer [opt] function-body handler-seq */
25016 static void
25017 cp_parser_function_try_block (cp_parser* parser)
25019 tree compound_stmt;
25020 tree try_block;
25022 /* Look for the `try' keyword. */
25023 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
25024 return;
25025 /* Let the rest of the front end know where we are. */
25026 try_block = begin_function_try_block (&compound_stmt);
25027 /* Parse the function-body. */
25028 cp_parser_ctor_initializer_opt_and_function_body
25029 (parser, /*in_function_try_block=*/true);
25030 /* We're done with the `try' part. */
25031 finish_function_try_block (try_block);
25032 /* Parse the handlers. */
25033 cp_parser_handler_seq (parser);
25034 /* We're done with the handlers. */
25035 finish_function_handler_sequence (try_block, compound_stmt);
25038 /* Parse a handler-seq.
25040 handler-seq:
25041 handler handler-seq [opt] */
25043 static void
25044 cp_parser_handler_seq (cp_parser* parser)
25046 while (true)
25048 cp_token *token;
25050 /* Parse the handler. */
25051 cp_parser_handler (parser);
25052 /* Peek at the next token. */
25053 token = cp_lexer_peek_token (parser->lexer);
25054 /* If it's not `catch' then there are no more handlers. */
25055 if (!cp_parser_is_keyword (token, RID_CATCH))
25056 break;
25060 /* Parse a handler.
25062 handler:
25063 catch ( exception-declaration ) compound-statement */
25065 static void
25066 cp_parser_handler (cp_parser* parser)
25068 tree handler;
25069 tree declaration;
25071 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
25072 handler = begin_handler ();
25073 matching_parens parens;
25074 parens.require_open (parser);
25075 declaration = cp_parser_exception_declaration (parser);
25076 finish_handler_parms (declaration, handler);
25077 parens.require_close (parser);
25078 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
25079 finish_handler (handler);
25082 /* Parse an exception-declaration.
25084 exception-declaration:
25085 type-specifier-seq declarator
25086 type-specifier-seq abstract-declarator
25087 type-specifier-seq
25090 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25091 ellipsis variant is used. */
25093 static tree
25094 cp_parser_exception_declaration (cp_parser* parser)
25096 cp_decl_specifier_seq type_specifiers;
25097 cp_declarator *declarator;
25098 const char *saved_message;
25100 /* If it's an ellipsis, it's easy to handle. */
25101 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25103 /* Consume the `...' token. */
25104 cp_lexer_consume_token (parser->lexer);
25105 return NULL_TREE;
25108 /* Types may not be defined in exception-declarations. */
25109 saved_message = parser->type_definition_forbidden_message;
25110 parser->type_definition_forbidden_message
25111 = G_("types may not be defined in exception-declarations");
25113 /* Parse the type-specifier-seq. */
25114 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
25115 /*is_trailing_return=*/false,
25116 &type_specifiers);
25117 /* If it's a `)', then there is no declarator. */
25118 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25119 declarator = NULL;
25120 else
25121 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
25122 /*ctor_dtor_or_conv_p=*/NULL,
25123 /*parenthesized_p=*/NULL,
25124 /*member_p=*/false,
25125 /*friend_p=*/false);
25127 /* Restore the saved message. */
25128 parser->type_definition_forbidden_message = saved_message;
25130 if (!type_specifiers.any_specifiers_p)
25131 return error_mark_node;
25133 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
25136 /* Parse a throw-expression.
25138 throw-expression:
25139 throw assignment-expression [opt]
25141 Returns a THROW_EXPR representing the throw-expression. */
25143 static tree
25144 cp_parser_throw_expression (cp_parser* parser)
25146 tree expression;
25147 cp_token* token;
25149 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
25150 token = cp_lexer_peek_token (parser->lexer);
25151 /* Figure out whether or not there is an assignment-expression
25152 following the "throw" keyword. */
25153 if (token->type == CPP_COMMA
25154 || token->type == CPP_SEMICOLON
25155 || token->type == CPP_CLOSE_PAREN
25156 || token->type == CPP_CLOSE_SQUARE
25157 || token->type == CPP_CLOSE_BRACE
25158 || token->type == CPP_COLON)
25159 expression = NULL_TREE;
25160 else
25161 expression = cp_parser_assignment_expression (parser);
25163 return build_throw (expression);
25166 /* GNU Extensions */
25168 /* Parse an (optional) asm-specification.
25170 asm-specification:
25171 asm ( string-literal )
25173 If the asm-specification is present, returns a STRING_CST
25174 corresponding to the string-literal. Otherwise, returns
25175 NULL_TREE. */
25177 static tree
25178 cp_parser_asm_specification_opt (cp_parser* parser)
25180 cp_token *token;
25181 tree asm_specification;
25183 /* Peek at the next token. */
25184 token = cp_lexer_peek_token (parser->lexer);
25185 /* If the next token isn't the `asm' keyword, then there's no
25186 asm-specification. */
25187 if (!cp_parser_is_keyword (token, RID_ASM))
25188 return NULL_TREE;
25190 /* Consume the `asm' token. */
25191 cp_lexer_consume_token (parser->lexer);
25192 /* Look for the `('. */
25193 matching_parens parens;
25194 parens.require_open (parser);
25196 /* Look for the string-literal. */
25197 asm_specification = cp_parser_string_literal (parser, false, false);
25199 /* Look for the `)'. */
25200 parens.require_close (parser);
25202 return asm_specification;
25205 /* Parse an asm-operand-list.
25207 asm-operand-list:
25208 asm-operand
25209 asm-operand-list , asm-operand
25211 asm-operand:
25212 string-literal ( expression )
25213 [ string-literal ] string-literal ( expression )
25215 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25216 each node is the expression. The TREE_PURPOSE is itself a
25217 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25218 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25219 is a STRING_CST for the string literal before the parenthesis. Returns
25220 ERROR_MARK_NODE if any of the operands are invalid. */
25222 static tree
25223 cp_parser_asm_operand_list (cp_parser* parser)
25225 tree asm_operands = NULL_TREE;
25226 bool invalid_operands = false;
25228 while (true)
25230 tree string_literal;
25231 tree expression;
25232 tree name;
25234 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25236 /* Consume the `[' token. */
25237 cp_lexer_consume_token (parser->lexer);
25238 /* Read the operand name. */
25239 name = cp_parser_identifier (parser);
25240 if (name != error_mark_node)
25241 name = build_string (IDENTIFIER_LENGTH (name),
25242 IDENTIFIER_POINTER (name));
25243 /* Look for the closing `]'. */
25244 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25246 else
25247 name = NULL_TREE;
25248 /* Look for the string-literal. */
25249 string_literal = cp_parser_string_literal (parser, false, false);
25251 /* Look for the `('. */
25252 matching_parens parens;
25253 parens.require_open (parser);
25254 /* Parse the expression. */
25255 expression = cp_parser_expression (parser);
25256 /* Look for the `)'. */
25257 parens.require_close (parser);
25259 if (name == error_mark_node
25260 || string_literal == error_mark_node
25261 || expression == error_mark_node)
25262 invalid_operands = true;
25264 /* Add this operand to the list. */
25265 asm_operands = tree_cons (build_tree_list (name, string_literal),
25266 expression,
25267 asm_operands);
25268 /* If the next token is not a `,', there are no more
25269 operands. */
25270 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25271 break;
25272 /* Consume the `,'. */
25273 cp_lexer_consume_token (parser->lexer);
25276 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25279 /* Parse an asm-clobber-list.
25281 asm-clobber-list:
25282 string-literal
25283 asm-clobber-list , string-literal
25285 Returns a TREE_LIST, indicating the clobbers in the order that they
25286 appeared. The TREE_VALUE of each node is a STRING_CST. */
25288 static tree
25289 cp_parser_asm_clobber_list (cp_parser* parser)
25291 tree clobbers = NULL_TREE;
25293 while (true)
25295 tree string_literal;
25297 /* Look for the string literal. */
25298 string_literal = cp_parser_string_literal (parser, false, false);
25299 /* Add it to the list. */
25300 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25301 /* If the next token is not a `,', then the list is
25302 complete. */
25303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25304 break;
25305 /* Consume the `,' token. */
25306 cp_lexer_consume_token (parser->lexer);
25309 return clobbers;
25312 /* Parse an asm-label-list.
25314 asm-label-list:
25315 identifier
25316 asm-label-list , identifier
25318 Returns a TREE_LIST, indicating the labels in the order that they
25319 appeared. The TREE_VALUE of each node is a label. */
25321 static tree
25322 cp_parser_asm_label_list (cp_parser* parser)
25324 tree labels = NULL_TREE;
25326 while (true)
25328 tree identifier, label, name;
25330 /* Look for the identifier. */
25331 identifier = cp_parser_identifier (parser);
25332 if (!error_operand_p (identifier))
25334 label = lookup_label (identifier);
25335 if (TREE_CODE (label) == LABEL_DECL)
25337 TREE_USED (label) = 1;
25338 check_goto (label);
25339 name = build_string (IDENTIFIER_LENGTH (identifier),
25340 IDENTIFIER_POINTER (identifier));
25341 labels = tree_cons (name, label, labels);
25344 /* If the next token is not a `,', then the list is
25345 complete. */
25346 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25347 break;
25348 /* Consume the `,' token. */
25349 cp_lexer_consume_token (parser->lexer);
25352 return nreverse (labels);
25355 /* Return TRUE iff the next tokens in the stream are possibly the
25356 beginning of a GNU extension attribute. */
25358 static bool
25359 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25361 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25364 /* Return TRUE iff the next tokens in the stream are possibly the
25365 beginning of a standard C++-11 attribute specifier. */
25367 static bool
25368 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25370 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25373 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25374 beginning of a standard C++-11 attribute specifier. */
25376 static bool
25377 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25379 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25381 return (cxx_dialect >= cxx11
25382 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25383 || (token->type == CPP_OPEN_SQUARE
25384 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25385 && token->type == CPP_OPEN_SQUARE)));
25388 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25389 beginning of a GNU extension attribute. */
25391 static bool
25392 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25394 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25396 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25399 /* Return true iff the next tokens can be the beginning of either a
25400 GNU attribute list, or a standard C++11 attribute sequence. */
25402 static bool
25403 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25405 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25406 || cp_next_tokens_can_be_std_attribute_p (parser));
25409 /* Return true iff the next Nth tokens can be the beginning of either
25410 a GNU attribute list, or a standard C++11 attribute sequence. */
25412 static bool
25413 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25415 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25416 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25419 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25420 of GNU attributes, or return NULL. */
25422 static tree
25423 cp_parser_attributes_opt (cp_parser *parser)
25425 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25426 return cp_parser_gnu_attributes_opt (parser);
25427 return cp_parser_std_attribute_spec_seq (parser);
25430 /* Parse an (optional) series of attributes.
25432 attributes:
25433 attributes attribute
25435 attribute:
25436 __attribute__ (( attribute-list [opt] ))
25438 The return value is as for cp_parser_gnu_attribute_list. */
25440 static tree
25441 cp_parser_gnu_attributes_opt (cp_parser* parser)
25443 tree attributes = NULL_TREE;
25445 temp_override<bool> cleanup
25446 (parser->auto_is_implicit_function_template_parm_p, false);
25448 while (true)
25450 cp_token *token;
25451 tree attribute_list;
25452 bool ok = true;
25454 /* Peek at the next token. */
25455 token = cp_lexer_peek_token (parser->lexer);
25456 /* If it's not `__attribute__', then we're done. */
25457 if (token->keyword != RID_ATTRIBUTE)
25458 break;
25460 /* Consume the `__attribute__' keyword. */
25461 cp_lexer_consume_token (parser->lexer);
25462 /* Look for the two `(' tokens. */
25463 matching_parens outer_parens;
25464 outer_parens.require_open (parser);
25465 matching_parens inner_parens;
25466 inner_parens.require_open (parser);
25468 /* Peek at the next token. */
25469 token = cp_lexer_peek_token (parser->lexer);
25470 if (token->type != CPP_CLOSE_PAREN)
25471 /* Parse the attribute-list. */
25472 attribute_list = cp_parser_gnu_attribute_list (parser);
25473 else
25474 /* If the next token is a `)', then there is no attribute
25475 list. */
25476 attribute_list = NULL;
25478 /* Look for the two `)' tokens. */
25479 if (!inner_parens.require_close (parser))
25480 ok = false;
25481 if (!outer_parens.require_close (parser))
25482 ok = false;
25483 if (!ok)
25484 cp_parser_skip_to_end_of_statement (parser);
25486 /* Add these new attributes to the list. */
25487 attributes = attr_chainon (attributes, attribute_list);
25490 return attributes;
25493 /* Parse a GNU attribute-list.
25495 attribute-list:
25496 attribute
25497 attribute-list , attribute
25499 attribute:
25500 identifier
25501 identifier ( identifier )
25502 identifier ( identifier , expression-list )
25503 identifier ( expression-list )
25505 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25506 to an attribute. The TREE_PURPOSE of each node is the identifier
25507 indicating which attribute is in use. The TREE_VALUE represents
25508 the arguments, if any. */
25510 static tree
25511 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
25513 tree attribute_list = NULL_TREE;
25514 bool save_translate_strings_p = parser->translate_strings_p;
25516 parser->translate_strings_p = false;
25517 while (true)
25519 cp_token *token;
25520 tree identifier;
25521 tree attribute;
25523 /* Look for the identifier. We also allow keywords here; for
25524 example `__attribute__ ((const))' is legal. */
25525 token = cp_lexer_peek_token (parser->lexer);
25526 if (token->type == CPP_NAME
25527 || token->type == CPP_KEYWORD)
25529 tree arguments = NULL_TREE;
25531 /* Consume the token, but save it since we need it for the
25532 SIMD enabled function parsing. */
25533 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25535 /* Save away the identifier that indicates which attribute
25536 this is. */
25537 identifier = (token->type == CPP_KEYWORD)
25538 /* For keywords, use the canonical spelling, not the
25539 parsed identifier. */
25540 ? ridpointers[(int) token->keyword]
25541 : id_token->u.value;
25543 identifier = canonicalize_attr_name (identifier);
25544 attribute = build_tree_list (identifier, NULL_TREE);
25546 /* Peek at the next token. */
25547 token = cp_lexer_peek_token (parser->lexer);
25548 /* If it's an `(', then parse the attribute arguments. */
25549 if (token->type == CPP_OPEN_PAREN)
25551 vec<tree, va_gc> *vec;
25552 int attr_flag = (attribute_takes_identifier_p (identifier)
25553 ? id_attr : normal_attr);
25554 vec = cp_parser_parenthesized_expression_list
25555 (parser, attr_flag, /*cast_p=*/false,
25556 /*allow_expansion_p=*/false,
25557 /*non_constant_p=*/NULL);
25558 if (vec == NULL)
25559 arguments = error_mark_node;
25560 else
25562 arguments = build_tree_list_vec (vec);
25563 release_tree_vector (vec);
25565 /* Save the arguments away. */
25566 TREE_VALUE (attribute) = arguments;
25569 if (arguments != error_mark_node)
25571 /* Add this attribute to the list. */
25572 TREE_CHAIN (attribute) = attribute_list;
25573 attribute_list = attribute;
25576 token = cp_lexer_peek_token (parser->lexer);
25578 /* Unless EXACTLY_ONE is set look for more attributes.
25579 If the next token isn't a `,', we're done. */
25580 if (exactly_one || token->type != CPP_COMMA)
25581 break;
25583 /* Consume the comma and keep going. */
25584 cp_lexer_consume_token (parser->lexer);
25586 parser->translate_strings_p = save_translate_strings_p;
25588 /* We built up the list in reverse order. */
25589 return nreverse (attribute_list);
25592 /* Parse a standard C++11 attribute.
25594 The returned representation is a TREE_LIST which TREE_PURPOSE is
25595 the scoped name of the attribute, and the TREE_VALUE is its
25596 arguments list.
25598 Note that the scoped name of the attribute is itself a TREE_LIST
25599 which TREE_PURPOSE is the namespace of the attribute, and
25600 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25601 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25602 and which TREE_PURPOSE is directly the attribute name.
25604 Clients of the attribute code should use get_attribute_namespace
25605 and get_attribute_name to get the actual namespace and name of
25606 attributes, regardless of their being GNU or C++11 attributes.
25608 attribute:
25609 attribute-token attribute-argument-clause [opt]
25611 attribute-token:
25612 identifier
25613 attribute-scoped-token
25615 attribute-scoped-token:
25616 attribute-namespace :: identifier
25618 attribute-namespace:
25619 identifier
25621 attribute-argument-clause:
25622 ( balanced-token-seq )
25624 balanced-token-seq:
25625 balanced-token [opt]
25626 balanced-token-seq balanced-token
25628 balanced-token:
25629 ( balanced-token-seq )
25630 [ balanced-token-seq ]
25631 { balanced-token-seq }. */
25633 static tree
25634 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25636 tree attribute, attr_id = NULL_TREE, arguments;
25637 cp_token *token;
25639 temp_override<bool> cleanup
25640 (parser->auto_is_implicit_function_template_parm_p, false);
25642 /* First, parse name of the attribute, a.k.a attribute-token. */
25644 token = cp_lexer_peek_token (parser->lexer);
25645 if (token->type == CPP_NAME)
25646 attr_id = token->u.value;
25647 else if (token->type == CPP_KEYWORD)
25648 attr_id = ridpointers[(int) token->keyword];
25649 else if (token->flags & NAMED_OP)
25650 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25652 if (attr_id == NULL_TREE)
25653 return NULL_TREE;
25655 cp_lexer_consume_token (parser->lexer);
25657 token = cp_lexer_peek_token (parser->lexer);
25658 if (token->type == CPP_SCOPE)
25660 /* We are seeing a scoped attribute token. */
25662 cp_lexer_consume_token (parser->lexer);
25663 if (attr_ns)
25664 error_at (token->location, "attribute using prefix used together "
25665 "with scoped attribute token");
25666 attr_ns = attr_id;
25668 token = cp_lexer_consume_token (parser->lexer);
25669 if (token->type == CPP_NAME)
25670 attr_id = token->u.value;
25671 else if (token->type == CPP_KEYWORD)
25672 attr_id = ridpointers[(int) token->keyword];
25673 else if (token->flags & NAMED_OP)
25674 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25675 else
25677 error_at (token->location,
25678 "expected an identifier for the attribute name");
25679 return error_mark_node;
25682 attr_ns = canonicalize_attr_name (attr_ns);
25683 attr_id = canonicalize_attr_name (attr_id);
25684 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25685 NULL_TREE);
25686 token = cp_lexer_peek_token (parser->lexer);
25688 else if (attr_ns)
25690 attr_ns = canonicalize_attr_name (attr_ns);
25691 attr_id = canonicalize_attr_name (attr_id);
25692 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25693 NULL_TREE);
25695 else
25697 attr_id = canonicalize_attr_name (attr_id);
25698 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25699 NULL_TREE);
25700 /* C++11 noreturn attribute is equivalent to GNU's. */
25701 if (is_attribute_p ("noreturn", attr_id))
25702 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25703 /* C++14 deprecated attribute is equivalent to GNU's. */
25704 else if (is_attribute_p ("deprecated", attr_id))
25705 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25706 /* C++17 fallthrough attribute is equivalent to GNU's. */
25707 else if (is_attribute_p ("fallthrough", attr_id))
25708 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25709 /* Transactional Memory TS optimize_for_synchronized attribute is
25710 equivalent to GNU transaction_callable. */
25711 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25712 TREE_PURPOSE (attribute)
25713 = get_identifier ("transaction_callable");
25714 /* Transactional Memory attributes are GNU attributes. */
25715 else if (tm_attr_to_mask (attr_id))
25716 TREE_PURPOSE (attribute) = attr_id;
25719 /* Now parse the optional argument clause of the attribute. */
25721 if (token->type != CPP_OPEN_PAREN)
25722 return attribute;
25725 vec<tree, va_gc> *vec;
25726 int attr_flag = normal_attr;
25728 if (attr_ns == gnu_identifier
25729 && attribute_takes_identifier_p (attr_id))
25730 /* A GNU attribute that takes an identifier in parameter. */
25731 attr_flag = id_attr;
25733 vec = cp_parser_parenthesized_expression_list
25734 (parser, attr_flag, /*cast_p=*/false,
25735 /*allow_expansion_p=*/true,
25736 /*non_constant_p=*/NULL);
25737 if (vec == NULL)
25738 arguments = error_mark_node;
25739 else
25741 arguments = build_tree_list_vec (vec);
25742 release_tree_vector (vec);
25745 if (arguments == error_mark_node)
25746 attribute = error_mark_node;
25747 else
25748 TREE_VALUE (attribute) = arguments;
25751 return attribute;
25754 /* Check that the attribute ATTRIBUTE appears at most once in the
25755 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25756 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25757 isn't implemented yet in GCC. */
25759 static void
25760 cp_parser_check_std_attribute (tree attributes, tree attribute)
25762 if (attributes)
25764 tree name = get_attribute_name (attribute);
25765 if (is_attribute_p ("noreturn", name)
25766 && lookup_attribute ("noreturn", attributes))
25767 error ("attribute %<noreturn%> can appear at most once "
25768 "in an attribute-list");
25769 else if (is_attribute_p ("deprecated", name)
25770 && lookup_attribute ("deprecated", attributes))
25771 error ("attribute %<deprecated%> can appear at most once "
25772 "in an attribute-list");
25776 /* Parse a list of standard C++-11 attributes.
25778 attribute-list:
25779 attribute [opt]
25780 attribute-list , attribute[opt]
25781 attribute ...
25782 attribute-list , attribute ...
25785 static tree
25786 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25788 tree attributes = NULL_TREE, attribute = NULL_TREE;
25789 cp_token *token = NULL;
25791 while (true)
25793 attribute = cp_parser_std_attribute (parser, attr_ns);
25794 if (attribute == error_mark_node)
25795 break;
25796 if (attribute != NULL_TREE)
25798 cp_parser_check_std_attribute (attributes, attribute);
25799 TREE_CHAIN (attribute) = attributes;
25800 attributes = attribute;
25802 token = cp_lexer_peek_token (parser->lexer);
25803 if (token->type == CPP_ELLIPSIS)
25805 cp_lexer_consume_token (parser->lexer);
25806 if (attribute == NULL_TREE)
25807 error_at (token->location,
25808 "expected attribute before %<...%>");
25809 else
25811 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25812 if (pack == error_mark_node)
25813 return error_mark_node;
25814 TREE_VALUE (attribute) = pack;
25816 token = cp_lexer_peek_token (parser->lexer);
25818 if (token->type != CPP_COMMA)
25819 break;
25820 cp_lexer_consume_token (parser->lexer);
25822 attributes = nreverse (attributes);
25823 return attributes;
25826 /* Parse a standard C++-11 attribute specifier.
25828 attribute-specifier:
25829 [ [ attribute-using-prefix [opt] attribute-list ] ]
25830 alignment-specifier
25832 attribute-using-prefix:
25833 using attribute-namespace :
25835 alignment-specifier:
25836 alignas ( type-id ... [opt] )
25837 alignas ( alignment-expression ... [opt] ). */
25839 static tree
25840 cp_parser_std_attribute_spec (cp_parser *parser)
25842 tree attributes = NULL_TREE;
25843 cp_token *token = cp_lexer_peek_token (parser->lexer);
25845 if (token->type == CPP_OPEN_SQUARE
25846 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25848 tree attr_ns = NULL_TREE;
25850 cp_lexer_consume_token (parser->lexer);
25851 cp_lexer_consume_token (parser->lexer);
25853 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25855 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25856 if (token->type == CPP_NAME)
25857 attr_ns = token->u.value;
25858 else if (token->type == CPP_KEYWORD)
25859 attr_ns = ridpointers[(int) token->keyword];
25860 else if (token->flags & NAMED_OP)
25861 attr_ns = get_identifier (cpp_type2name (token->type,
25862 token->flags));
25863 if (attr_ns
25864 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25866 if (cxx_dialect < cxx17
25867 && !in_system_header_at (input_location))
25868 pedwarn (input_location, 0,
25869 "attribute using prefix only available "
25870 "with -std=c++17 or -std=gnu++17");
25872 cp_lexer_consume_token (parser->lexer);
25873 cp_lexer_consume_token (parser->lexer);
25874 cp_lexer_consume_token (parser->lexer);
25876 else
25877 attr_ns = NULL_TREE;
25880 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25882 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25883 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25884 cp_parser_skip_to_end_of_statement (parser);
25885 else
25886 /* Warn about parsing c++11 attribute in non-c++11 mode, only
25887 when we are sure that we have actually parsed them. */
25888 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25890 else
25892 tree alignas_expr;
25894 /* Look for an alignment-specifier. */
25896 token = cp_lexer_peek_token (parser->lexer);
25898 if (token->type != CPP_KEYWORD
25899 || token->keyword != RID_ALIGNAS)
25900 return NULL_TREE;
25902 cp_lexer_consume_token (parser->lexer);
25903 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25905 matching_parens parens;
25906 if (!parens.require_open (parser))
25907 return error_mark_node;
25909 cp_parser_parse_tentatively (parser);
25910 alignas_expr = cp_parser_type_id (parser);
25912 if (!cp_parser_parse_definitely (parser))
25914 alignas_expr = cp_parser_assignment_expression (parser);
25915 if (alignas_expr == error_mark_node)
25916 cp_parser_skip_to_end_of_statement (parser);
25917 if (alignas_expr == NULL_TREE
25918 || alignas_expr == error_mark_node)
25919 return alignas_expr;
25922 alignas_expr = cxx_alignas_expr (alignas_expr);
25923 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25925 /* Handle alignas (pack...). */
25926 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25928 cp_lexer_consume_token (parser->lexer);
25929 alignas_expr = make_pack_expansion (alignas_expr);
25932 /* Something went wrong, so don't build the attribute. */
25933 if (alignas_expr == error_mark_node)
25934 return error_mark_node;
25936 if (!parens.require_close (parser))
25937 return error_mark_node;
25939 /* Build the C++-11 representation of an 'aligned'
25940 attribute. */
25941 attributes
25942 = build_tree_list (build_tree_list (gnu_identifier,
25943 aligned_identifier), alignas_expr);
25946 return attributes;
25949 /* Parse a standard C++-11 attribute-specifier-seq.
25951 attribute-specifier-seq:
25952 attribute-specifier-seq [opt] attribute-specifier
25955 static tree
25956 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25958 tree attr_specs = NULL_TREE;
25959 tree attr_last = NULL_TREE;
25961 while (true)
25963 tree attr_spec = cp_parser_std_attribute_spec (parser);
25964 if (attr_spec == NULL_TREE)
25965 break;
25966 if (attr_spec == error_mark_node)
25967 return error_mark_node;
25969 if (attr_last)
25970 TREE_CHAIN (attr_last) = attr_spec;
25971 else
25972 attr_specs = attr_last = attr_spec;
25973 attr_last = tree_last (attr_last);
25976 return attr_specs;
25979 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25980 return index of the first token after balanced-token, or N on failure. */
25982 static size_t
25983 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25985 size_t orig_n = n;
25986 int nparens = 0, nbraces = 0, nsquares = 0;
25988 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25990 case CPP_EOF:
25991 case CPP_PRAGMA_EOL:
25992 /* Ran out of tokens. */
25993 return orig_n;
25994 case CPP_OPEN_PAREN:
25995 ++nparens;
25996 break;
25997 case CPP_OPEN_BRACE:
25998 ++nbraces;
25999 break;
26000 case CPP_OPEN_SQUARE:
26001 ++nsquares;
26002 break;
26003 case CPP_CLOSE_PAREN:
26004 --nparens;
26005 break;
26006 case CPP_CLOSE_BRACE:
26007 --nbraces;
26008 break;
26009 case CPP_CLOSE_SQUARE:
26010 --nsquares;
26011 break;
26012 default:
26013 break;
26015 while (nparens || nbraces || nsquares);
26016 return n;
26019 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26020 return index of the first token after the GNU attribute tokens, or N on
26021 failure. */
26023 static size_t
26024 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
26026 while (true)
26028 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
26029 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
26030 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
26031 break;
26033 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
26034 if (n2 == n + 2)
26035 break;
26036 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
26037 break;
26038 n = n2 + 1;
26040 return n;
26043 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26044 next token), return index of the first token after the standard C++11
26045 attribute tokens, or N on failure. */
26047 static size_t
26048 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
26050 while (true)
26052 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
26053 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
26055 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26056 if (n2 == n + 1)
26057 break;
26058 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
26059 break;
26060 n = n2 + 1;
26062 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
26063 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
26065 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26066 if (n2 == n + 1)
26067 break;
26068 n = n2;
26070 else
26071 break;
26073 return n;
26076 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26077 as the next token), return index of the first token after the attribute
26078 tokens, or N on failure. */
26080 static size_t
26081 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
26083 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
26084 return cp_parser_skip_gnu_attributes_opt (parser, n);
26085 return cp_parser_skip_std_attribute_spec_seq (parser, n);
26088 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26089 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26090 current value of the PEDANTIC flag, regardless of whether or not
26091 the `__extension__' keyword is present. The caller is responsible
26092 for restoring the value of the PEDANTIC flag. */
26094 static bool
26095 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
26097 /* Save the old value of the PEDANTIC flag. */
26098 *saved_pedantic = pedantic;
26100 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
26102 /* Consume the `__extension__' token. */
26103 cp_lexer_consume_token (parser->lexer);
26104 /* We're not being pedantic while the `__extension__' keyword is
26105 in effect. */
26106 pedantic = 0;
26108 return true;
26111 return false;
26114 /* Parse a label declaration.
26116 label-declaration:
26117 __label__ label-declarator-seq ;
26119 label-declarator-seq:
26120 identifier , label-declarator-seq
26121 identifier */
26123 static void
26124 cp_parser_label_declaration (cp_parser* parser)
26126 /* Look for the `__label__' keyword. */
26127 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
26129 while (true)
26131 tree identifier;
26133 /* Look for an identifier. */
26134 identifier = cp_parser_identifier (parser);
26135 /* If we failed, stop. */
26136 if (identifier == error_mark_node)
26137 break;
26138 /* Declare it as a label. */
26139 finish_label_decl (identifier);
26140 /* If the next token is a `;', stop. */
26141 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26142 break;
26143 /* Look for the `,' separating the label declarations. */
26144 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
26147 /* Look for the final `;'. */
26148 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26151 // -------------------------------------------------------------------------- //
26152 // Requires Clause
26154 // Parse a requires clause.
26156 // requires-clause:
26157 // 'requires' logical-or-expression
26159 // The required logical-or-expression must be a constant expression. Note
26160 // that we don't check that the expression is constepxr here. We defer until
26161 // we analyze constraints and then, we only check atomic constraints.
26162 static tree
26163 cp_parser_requires_clause (cp_parser *parser)
26165 // Parse the requires clause so that it is not automatically folded.
26166 ++processing_template_decl;
26167 tree expr = cp_parser_binary_expression (parser, false, false,
26168 PREC_NOT_OPERATOR, NULL);
26169 if (check_for_bare_parameter_packs (expr))
26170 expr = error_mark_node;
26171 --processing_template_decl;
26172 return expr;
26175 // Optionally parse a requires clause:
26176 static tree
26177 cp_parser_requires_clause_opt (cp_parser *parser)
26179 cp_token *tok = cp_lexer_peek_token (parser->lexer);
26180 if (tok->keyword != RID_REQUIRES)
26182 if (!flag_concepts && tok->type == CPP_NAME
26183 && tok->u.value == ridpointers[RID_REQUIRES])
26185 error_at (cp_lexer_peek_token (parser->lexer)->location,
26186 "%<requires%> only available with -fconcepts");
26187 /* Parse and discard the requires-clause. */
26188 cp_lexer_consume_token (parser->lexer);
26189 cp_parser_requires_clause (parser);
26191 return NULL_TREE;
26193 cp_lexer_consume_token (parser->lexer);
26194 return cp_parser_requires_clause (parser);
26198 /*---------------------------------------------------------------------------
26199 Requires expressions
26200 ---------------------------------------------------------------------------*/
26202 /* Parse a requires expression
26204 requirement-expression:
26205 'requires' requirement-parameter-list [opt] requirement-body */
26206 static tree
26207 cp_parser_requires_expression (cp_parser *parser)
26209 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26210 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26212 /* A requires-expression shall appear only within a concept
26213 definition or a requires-clause.
26215 TODO: Implement this diagnostic correctly. */
26216 if (!processing_template_decl)
26218 error_at (loc, "a requires expression cannot appear outside a template");
26219 cp_parser_skip_to_end_of_statement (parser);
26220 return error_mark_node;
26223 tree parms, reqs;
26225 /* Local parameters are delared as variables within the scope
26226 of the expression. They are not visible past the end of
26227 the expression. Expressions within the requires-expression
26228 are unevaluated. */
26229 struct scope_sentinel
26231 scope_sentinel ()
26233 ++cp_unevaluated_operand;
26234 begin_scope (sk_block, NULL_TREE);
26237 ~scope_sentinel ()
26239 pop_bindings_and_leave_scope ();
26240 --cp_unevaluated_operand;
26242 } s;
26244 /* Parse the optional parameter list. */
26245 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26247 parms = cp_parser_requirement_parameter_list (parser);
26248 if (parms == error_mark_node)
26249 return error_mark_node;
26251 else
26252 parms = NULL_TREE;
26254 /* Parse the requirement body. */
26255 reqs = cp_parser_requirement_body (parser);
26256 if (reqs == error_mark_node)
26257 return error_mark_node;
26260 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26261 the parm chain. */
26262 grokparms (parms, &parms);
26263 return finish_requires_expr (parms, reqs);
26266 /* Parse a parameterized requirement.
26268 requirement-parameter-list:
26269 '(' parameter-declaration-clause ')' */
26270 static tree
26271 cp_parser_requirement_parameter_list (cp_parser *parser)
26273 matching_parens parens;
26274 if (!parens.require_open (parser))
26275 return error_mark_node;
26277 tree parms = cp_parser_parameter_declaration_clause (parser);
26279 if (!parens.require_close (parser))
26280 return error_mark_node;
26282 return parms;
26285 /* Parse the body of a requirement.
26287 requirement-body:
26288 '{' requirement-list '}' */
26289 static tree
26290 cp_parser_requirement_body (cp_parser *parser)
26292 matching_braces braces;
26293 if (!braces.require_open (parser))
26294 return error_mark_node;
26296 tree reqs = cp_parser_requirement_list (parser);
26298 if (!braces.require_close (parser))
26299 return error_mark_node;
26301 return reqs;
26304 /* Parse a list of requirements.
26306 requirement-list:
26307 requirement
26308 requirement-list ';' requirement[opt] */
26309 static tree
26310 cp_parser_requirement_list (cp_parser *parser)
26312 tree result = NULL_TREE;
26313 while (true)
26315 tree req = cp_parser_requirement (parser);
26316 if (req == error_mark_node)
26317 return error_mark_node;
26319 result = tree_cons (NULL_TREE, req, result);
26321 /* If we see a semi-colon, consume it. */
26322 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26323 cp_lexer_consume_token (parser->lexer);
26325 /* Stop processing at the end of the list. */
26326 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26327 break;
26330 /* Reverse the order of requirements so they are analyzed in
26331 declaration order. */
26332 return nreverse (result);
26335 /* Parse a syntactic requirement or type requirement.
26337 requirement:
26338 simple-requirement
26339 compound-requirement
26340 type-requirement
26341 nested-requirement */
26342 static tree
26343 cp_parser_requirement (cp_parser *parser)
26345 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26346 return cp_parser_compound_requirement (parser);
26347 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26348 return cp_parser_type_requirement (parser);
26349 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26350 return cp_parser_nested_requirement (parser);
26351 else
26352 return cp_parser_simple_requirement (parser);
26355 /* Parse a simple requirement.
26357 simple-requirement:
26358 expression ';' */
26359 static tree
26360 cp_parser_simple_requirement (cp_parser *parser)
26362 tree expr = cp_parser_expression (parser, NULL, false, false);
26363 if (!expr || expr == error_mark_node)
26364 return error_mark_node;
26366 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26367 return error_mark_node;
26369 return finish_simple_requirement (expr);
26372 /* Parse a type requirement
26374 type-requirement
26375 nested-name-specifier [opt] required-type-name ';'
26377 required-type-name:
26378 type-name
26379 'template' [opt] simple-template-id */
26380 static tree
26381 cp_parser_type_requirement (cp_parser *parser)
26383 cp_lexer_consume_token (parser->lexer);
26385 // Save the scope before parsing name specifiers.
26386 tree saved_scope = parser->scope;
26387 tree saved_object_scope = parser->object_scope;
26388 tree saved_qualifying_scope = parser->qualifying_scope;
26389 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26390 cp_parser_nested_name_specifier_opt (parser,
26391 /*typename_keyword_p=*/true,
26392 /*check_dependency_p=*/false,
26393 /*type_p=*/true,
26394 /*is_declaration=*/false);
26396 tree type;
26397 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26399 cp_lexer_consume_token (parser->lexer);
26400 type = cp_parser_template_id (parser,
26401 /*template_keyword_p=*/true,
26402 /*check_dependency=*/false,
26403 /*tag_type=*/none_type,
26404 /*is_declaration=*/false);
26405 type = make_typename_type (parser->scope, type, typename_type,
26406 /*complain=*/tf_error);
26408 else
26409 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26411 if (TREE_CODE (type) == TYPE_DECL)
26412 type = TREE_TYPE (type);
26414 parser->scope = saved_scope;
26415 parser->object_scope = saved_object_scope;
26416 parser->qualifying_scope = saved_qualifying_scope;
26418 if (type == error_mark_node)
26419 cp_parser_skip_to_end_of_statement (parser);
26421 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26422 return error_mark_node;
26423 if (type == error_mark_node)
26424 return error_mark_node;
26426 return finish_type_requirement (type);
26429 /* Parse a compound requirement
26431 compound-requirement:
26432 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26433 static tree
26434 cp_parser_compound_requirement (cp_parser *parser)
26436 /* Parse an expression enclosed in '{ }'s. */
26437 matching_braces braces;
26438 if (!braces.require_open (parser))
26439 return error_mark_node;
26441 tree expr = cp_parser_expression (parser, NULL, false, false);
26442 if (!expr || expr == error_mark_node)
26443 return error_mark_node;
26445 if (!braces.require_close (parser))
26446 return error_mark_node;
26448 /* Parse the optional noexcept. */
26449 bool noexcept_p = false;
26450 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26452 cp_lexer_consume_token (parser->lexer);
26453 noexcept_p = true;
26456 /* Parse the optional trailing return type. */
26457 tree type = NULL_TREE;
26458 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26460 cp_lexer_consume_token (parser->lexer);
26461 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26462 parser->in_result_type_constraint_p = true;
26463 type = cp_parser_trailing_type_id (parser);
26464 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26465 if (type == error_mark_node)
26466 return error_mark_node;
26469 return finish_compound_requirement (expr, type, noexcept_p);
26472 /* Parse a nested requirement. This is the same as a requires clause.
26474 nested-requirement:
26475 requires-clause */
26476 static tree
26477 cp_parser_nested_requirement (cp_parser *parser)
26479 cp_lexer_consume_token (parser->lexer);
26480 tree req = cp_parser_requires_clause (parser);
26481 if (req == error_mark_node)
26482 return error_mark_node;
26483 return finish_nested_requirement (req);
26486 /* Support Functions */
26488 /* Return the appropriate prefer_type argument for lookup_name_real based on
26489 tag_type and template_mem_access. */
26491 static inline int
26492 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26494 /* DR 141: When looking in the current enclosing context for a template-name
26495 after -> or ., only consider class templates. */
26496 if (template_mem_access)
26497 return 2;
26498 switch (tag_type)
26500 case none_type: return 0; // No preference.
26501 case scope_type: return 1; // Type or namespace.
26502 default: return 2; // Type only.
26506 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26507 NAME should have one of the representations used for an
26508 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26509 is returned. If PARSER->SCOPE is a dependent type, then a
26510 SCOPE_REF is returned.
26512 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26513 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26514 was formed. Abstractly, such entities should not be passed to this
26515 function, because they do not need to be looked up, but it is
26516 simpler to check for this special case here, rather than at the
26517 call-sites.
26519 In cases not explicitly covered above, this function returns a
26520 DECL, OVERLOAD, or baselink representing the result of the lookup.
26521 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26522 is returned.
26524 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26525 (e.g., "struct") that was used. In that case bindings that do not
26526 refer to types are ignored.
26528 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26529 ignored.
26531 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26532 are ignored.
26534 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26535 types.
26537 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26538 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26539 NULL_TREE otherwise. */
26541 static cp_expr
26542 cp_parser_lookup_name (cp_parser *parser, tree name,
26543 enum tag_types tag_type,
26544 bool is_template,
26545 bool is_namespace,
26546 bool check_dependency,
26547 tree *ambiguous_decls,
26548 location_t name_location)
26550 tree decl;
26551 tree object_type = parser->context->object_type;
26553 /* Assume that the lookup will be unambiguous. */
26554 if (ambiguous_decls)
26555 *ambiguous_decls = NULL_TREE;
26557 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26558 no longer valid. Note that if we are parsing tentatively, and
26559 the parse fails, OBJECT_TYPE will be automatically restored. */
26560 parser->context->object_type = NULL_TREE;
26562 if (name == error_mark_node)
26563 return error_mark_node;
26565 /* A template-id has already been resolved; there is no lookup to
26566 do. */
26567 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26568 return name;
26569 if (BASELINK_P (name))
26571 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26572 == TEMPLATE_ID_EXPR);
26573 return name;
26576 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26577 it should already have been checked to make sure that the name
26578 used matches the type being destroyed. */
26579 if (TREE_CODE (name) == BIT_NOT_EXPR)
26581 tree type;
26583 /* Figure out to which type this destructor applies. */
26584 if (parser->scope)
26585 type = parser->scope;
26586 else if (object_type)
26587 type = object_type;
26588 else
26589 type = current_class_type;
26590 /* If that's not a class type, there is no destructor. */
26591 if (!type || !CLASS_TYPE_P (type))
26592 return error_mark_node;
26594 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26595 lazily_declare_fn (sfk_destructor, type);
26597 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26598 return dtor;
26600 return error_mark_node;
26603 /* By this point, the NAME should be an ordinary identifier. If
26604 the id-expression was a qualified name, the qualifying scope is
26605 stored in PARSER->SCOPE at this point. */
26606 gcc_assert (identifier_p (name));
26608 /* Perform the lookup. */
26609 if (parser->scope)
26611 bool dependent_p;
26613 if (parser->scope == error_mark_node)
26614 return error_mark_node;
26616 /* If the SCOPE is dependent, the lookup must be deferred until
26617 the template is instantiated -- unless we are explicitly
26618 looking up names in uninstantiated templates. Even then, we
26619 cannot look up the name if the scope is not a class type; it
26620 might, for example, be a template type parameter. */
26621 dependent_p = (TYPE_P (parser->scope)
26622 && dependent_scope_p (parser->scope));
26623 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26624 && dependent_p)
26625 /* Defer lookup. */
26626 decl = error_mark_node;
26627 else
26629 tree pushed_scope = NULL_TREE;
26631 /* If PARSER->SCOPE is a dependent type, then it must be a
26632 class type, and we must not be checking dependencies;
26633 otherwise, we would have processed this lookup above. So
26634 that PARSER->SCOPE is not considered a dependent base by
26635 lookup_member, we must enter the scope here. */
26636 if (dependent_p)
26637 pushed_scope = push_scope (parser->scope);
26639 /* If the PARSER->SCOPE is a template specialization, it
26640 may be instantiated during name lookup. In that case,
26641 errors may be issued. Even if we rollback the current
26642 tentative parse, those errors are valid. */
26643 decl = lookup_qualified_name (parser->scope, name,
26644 prefer_type_arg (tag_type),
26645 /*complain=*/true);
26647 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26648 lookup result and the nested-name-specifier nominates a class C:
26649 * if the name specified after the nested-name-specifier, when
26650 looked up in C, is the injected-class-name of C (Clause 9), or
26651 * if the name specified after the nested-name-specifier is the
26652 same as the identifier or the simple-template-id's template-
26653 name in the last component of the nested-name-specifier,
26654 the name is instead considered to name the constructor of
26655 class C. [ Note: for example, the constructor is not an
26656 acceptable lookup result in an elaborated-type-specifier so
26657 the constructor would not be used in place of the
26658 injected-class-name. --end note ] Such a constructor name
26659 shall be used only in the declarator-id of a declaration that
26660 names a constructor or in a using-declaration. */
26661 if (tag_type == none_type
26662 && DECL_SELF_REFERENCE_P (decl)
26663 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26664 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26665 prefer_type_arg (tag_type),
26666 /*complain=*/true);
26668 /* If we have a single function from a using decl, pull it out. */
26669 if (TREE_CODE (decl) == OVERLOAD
26670 && !really_overloaded_fn (decl))
26671 decl = OVL_FUNCTION (decl);
26673 if (pushed_scope)
26674 pop_scope (pushed_scope);
26677 /* If the scope is a dependent type and either we deferred lookup or
26678 we did lookup but didn't find the name, rememeber the name. */
26679 if (decl == error_mark_node && TYPE_P (parser->scope)
26680 && dependent_type_p (parser->scope))
26682 if (tag_type)
26684 tree type;
26686 /* The resolution to Core Issue 180 says that `struct
26687 A::B' should be considered a type-name, even if `A'
26688 is dependent. */
26689 type = make_typename_type (parser->scope, name, tag_type,
26690 /*complain=*/tf_error);
26691 if (type != error_mark_node)
26692 decl = TYPE_NAME (type);
26694 else if (is_template
26695 && (cp_parser_next_token_ends_template_argument_p (parser)
26696 || cp_lexer_next_token_is (parser->lexer,
26697 CPP_CLOSE_PAREN)))
26698 decl = make_unbound_class_template (parser->scope,
26699 name, NULL_TREE,
26700 /*complain=*/tf_error);
26701 else
26702 decl = build_qualified_name (/*type=*/NULL_TREE,
26703 parser->scope, name,
26704 is_template);
26706 parser->qualifying_scope = parser->scope;
26707 parser->object_scope = NULL_TREE;
26709 else if (object_type)
26711 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26712 OBJECT_TYPE is not a class. */
26713 if (CLASS_TYPE_P (object_type))
26714 /* If the OBJECT_TYPE is a template specialization, it may
26715 be instantiated during name lookup. In that case, errors
26716 may be issued. Even if we rollback the current tentative
26717 parse, those errors are valid. */
26718 decl = lookup_member (object_type,
26719 name,
26720 /*protect=*/0,
26721 prefer_type_arg (tag_type),
26722 tf_warning_or_error);
26723 else
26724 decl = NULL_TREE;
26726 if (!decl)
26727 /* Look it up in the enclosing context. DR 141: When looking for a
26728 template-name after -> or ., only consider class templates. */
26729 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26730 /*nonclass=*/0,
26731 /*block_p=*/true, is_namespace, 0);
26732 if (object_type == unknown_type_node)
26733 /* The object is type-dependent, so we can't look anything up; we used
26734 this to get the DR 141 behavior. */
26735 object_type = NULL_TREE;
26736 parser->object_scope = object_type;
26737 parser->qualifying_scope = NULL_TREE;
26739 else
26741 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26742 /*nonclass=*/0,
26743 /*block_p=*/true, is_namespace, 0);
26744 parser->qualifying_scope = NULL_TREE;
26745 parser->object_scope = NULL_TREE;
26748 /* If the lookup failed, let our caller know. */
26749 if (!decl || decl == error_mark_node)
26750 return error_mark_node;
26752 /* Pull out the template from an injected-class-name (or multiple). */
26753 if (is_template)
26754 decl = maybe_get_template_decl_from_type_decl (decl);
26756 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26757 if (TREE_CODE (decl) == TREE_LIST)
26759 if (ambiguous_decls)
26760 *ambiguous_decls = decl;
26761 /* The error message we have to print is too complicated for
26762 cp_parser_error, so we incorporate its actions directly. */
26763 if (!cp_parser_simulate_error (parser))
26765 error_at (name_location, "reference to %qD is ambiguous",
26766 name);
26767 print_candidates (decl);
26769 return error_mark_node;
26772 gcc_assert (DECL_P (decl)
26773 || TREE_CODE (decl) == OVERLOAD
26774 || TREE_CODE (decl) == SCOPE_REF
26775 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26776 || BASELINK_P (decl));
26778 /* If we have resolved the name of a member declaration, check to
26779 see if the declaration is accessible. When the name resolves to
26780 set of overloaded functions, accessibility is checked when
26781 overload resolution is done.
26783 During an explicit instantiation, access is not checked at all,
26784 as per [temp.explicit]. */
26785 if (DECL_P (decl))
26786 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26788 maybe_record_typedef_use (decl);
26790 return cp_expr (decl, name_location);
26793 /* Like cp_parser_lookup_name, but for use in the typical case where
26794 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26795 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26797 static tree
26798 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26800 return cp_parser_lookup_name (parser, name,
26801 none_type,
26802 /*is_template=*/false,
26803 /*is_namespace=*/false,
26804 /*check_dependency=*/true,
26805 /*ambiguous_decls=*/NULL,
26806 location);
26809 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26810 the current context, return the TYPE_DECL. If TAG_NAME_P is
26811 true, the DECL indicates the class being defined in a class-head,
26812 or declared in an elaborated-type-specifier.
26814 Otherwise, return DECL. */
26816 static tree
26817 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26819 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26820 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26822 struct A {
26823 template <typename T> struct B;
26826 template <typename T> struct A::B {};
26828 Similarly, in an elaborated-type-specifier:
26830 namespace N { struct X{}; }
26832 struct A {
26833 template <typename T> friend struct N::X;
26836 However, if the DECL refers to a class type, and we are in
26837 the scope of the class, then the name lookup automatically
26838 finds the TYPE_DECL created by build_self_reference rather
26839 than a TEMPLATE_DECL. For example, in:
26841 template <class T> struct S {
26842 S s;
26845 there is no need to handle such case. */
26847 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26848 return DECL_TEMPLATE_RESULT (decl);
26850 return decl;
26853 /* If too many, or too few, template-parameter lists apply to the
26854 declarator, issue an error message. Returns TRUE if all went well,
26855 and FALSE otherwise. */
26857 static bool
26858 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26859 cp_declarator *declarator,
26860 location_t declarator_location)
26862 switch (declarator->kind)
26864 case cdk_id:
26866 unsigned num_templates = 0;
26867 tree scope = declarator->u.id.qualifying_scope;
26868 bool template_id_p = false;
26870 if (scope)
26871 num_templates = num_template_headers_for_class (scope);
26872 else if (TREE_CODE (declarator->u.id.unqualified_name)
26873 == TEMPLATE_ID_EXPR)
26875 /* If the DECLARATOR has the form `X<y>' then it uses one
26876 additional level of template parameters. */
26877 ++num_templates;
26878 template_id_p = true;
26881 return cp_parser_check_template_parameters
26882 (parser, num_templates, template_id_p, declarator_location,
26883 declarator);
26886 case cdk_function:
26887 case cdk_array:
26888 case cdk_pointer:
26889 case cdk_reference:
26890 case cdk_ptrmem:
26891 return (cp_parser_check_declarator_template_parameters
26892 (parser, declarator->declarator, declarator_location));
26894 case cdk_decomp:
26895 case cdk_error:
26896 return true;
26898 default:
26899 gcc_unreachable ();
26901 return false;
26904 /* NUM_TEMPLATES were used in the current declaration. If that is
26905 invalid, return FALSE and issue an error messages. Otherwise,
26906 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26907 declarator and we can print more accurate diagnostics. */
26909 static bool
26910 cp_parser_check_template_parameters (cp_parser* parser,
26911 unsigned num_templates,
26912 bool template_id_p,
26913 location_t location,
26914 cp_declarator *declarator)
26916 /* If there are the same number of template classes and parameter
26917 lists, that's OK. */
26918 if (parser->num_template_parameter_lists == num_templates)
26919 return true;
26920 /* If there are more, but only one more, and the name ends in an identifier,
26921 then we are declaring a primary template. That's OK too. */
26922 if (!template_id_p
26923 && parser->num_template_parameter_lists == num_templates + 1)
26924 return true;
26925 /* If there are more template classes than parameter lists, we have
26926 something like:
26928 template <class T> void S<T>::R<T>::f (); */
26929 if (parser->num_template_parameter_lists < num_templates)
26931 if (declarator && !current_function_decl)
26932 error_at (location, "specializing member %<%T::%E%> "
26933 "requires %<template<>%> syntax",
26934 declarator->u.id.qualifying_scope,
26935 declarator->u.id.unqualified_name);
26936 else if (declarator)
26937 error_at (location, "invalid declaration of %<%T::%E%>",
26938 declarator->u.id.qualifying_scope,
26939 declarator->u.id.unqualified_name);
26940 else
26941 error_at (location, "too few template-parameter-lists");
26942 return false;
26944 /* Otherwise, there are too many template parameter lists. We have
26945 something like:
26947 template <class T> template <class U> void S::f(); */
26948 error_at (location, "too many template-parameter-lists");
26949 return false;
26952 /* Parse an optional `::' token indicating that the following name is
26953 from the global namespace. If so, PARSER->SCOPE is set to the
26954 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26955 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26956 Returns the new value of PARSER->SCOPE, if the `::' token is
26957 present, and NULL_TREE otherwise. */
26959 static tree
26960 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26962 cp_token *token;
26964 /* Peek at the next token. */
26965 token = cp_lexer_peek_token (parser->lexer);
26966 /* If we're looking at a `::' token then we're starting from the
26967 global namespace, not our current location. */
26968 if (token->type == CPP_SCOPE)
26970 /* Consume the `::' token. */
26971 cp_lexer_consume_token (parser->lexer);
26972 /* Set the SCOPE so that we know where to start the lookup. */
26973 parser->scope = global_namespace;
26974 parser->qualifying_scope = global_namespace;
26975 parser->object_scope = NULL_TREE;
26977 return parser->scope;
26979 else if (!current_scope_valid_p)
26981 parser->scope = NULL_TREE;
26982 parser->qualifying_scope = NULL_TREE;
26983 parser->object_scope = NULL_TREE;
26986 return NULL_TREE;
26989 /* Returns TRUE if the upcoming token sequence is the start of a
26990 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26991 declarator is preceded by the `friend' specifier. */
26993 static bool
26994 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26996 bool constructor_p;
26997 bool outside_class_specifier_p;
26998 tree nested_name_specifier;
26999 cp_token *next_token;
27001 /* The common case is that this is not a constructor declarator, so
27002 try to avoid doing lots of work if at all possible. It's not
27003 valid declare a constructor at function scope. */
27004 if (parser->in_function_body)
27005 return false;
27006 /* And only certain tokens can begin a constructor declarator. */
27007 next_token = cp_lexer_peek_token (parser->lexer);
27008 if (next_token->type != CPP_NAME
27009 && next_token->type != CPP_SCOPE
27010 && next_token->type != CPP_NESTED_NAME_SPECIFIER
27011 && next_token->type != CPP_TEMPLATE_ID)
27012 return false;
27014 /* Parse tentatively; we are going to roll back all of the tokens
27015 consumed here. */
27016 cp_parser_parse_tentatively (parser);
27017 /* Assume that we are looking at a constructor declarator. */
27018 constructor_p = true;
27020 /* Look for the optional `::' operator. */
27021 cp_parser_global_scope_opt (parser,
27022 /*current_scope_valid_p=*/false);
27023 /* Look for the nested-name-specifier. */
27024 nested_name_specifier
27025 = (cp_parser_nested_name_specifier_opt (parser,
27026 /*typename_keyword_p=*/false,
27027 /*check_dependency_p=*/false,
27028 /*type_p=*/false,
27029 /*is_declaration=*/false));
27031 outside_class_specifier_p = (!at_class_scope_p ()
27032 || !TYPE_BEING_DEFINED (current_class_type)
27033 || friend_p);
27035 /* Outside of a class-specifier, there must be a
27036 nested-name-specifier. Except in C++17 mode, where we
27037 might be declaring a guiding declaration. */
27038 if (!nested_name_specifier && outside_class_specifier_p
27039 && cxx_dialect < cxx17)
27040 constructor_p = false;
27041 else if (nested_name_specifier == error_mark_node)
27042 constructor_p = false;
27044 /* If we have a class scope, this is easy; DR 147 says that S::S always
27045 names the constructor, and no other qualified name could. */
27046 if (constructor_p && nested_name_specifier
27047 && CLASS_TYPE_P (nested_name_specifier))
27049 tree id = cp_parser_unqualified_id (parser,
27050 /*template_keyword_p=*/false,
27051 /*check_dependency_p=*/false,
27052 /*declarator_p=*/true,
27053 /*optional_p=*/false);
27054 if (is_overloaded_fn (id))
27055 id = DECL_NAME (get_first_fn (id));
27056 if (!constructor_name_p (id, nested_name_specifier))
27057 constructor_p = false;
27059 /* If we still think that this might be a constructor-declarator,
27060 look for a class-name. */
27061 else if (constructor_p)
27063 /* If we have:
27065 template <typename T> struct S {
27066 S();
27069 we must recognize that the nested `S' names a class. */
27070 if (cxx_dialect >= cxx17)
27071 cp_parser_parse_tentatively (parser);
27073 tree type_decl;
27074 type_decl = cp_parser_class_name (parser,
27075 /*typename_keyword_p=*/false,
27076 /*template_keyword_p=*/false,
27077 none_type,
27078 /*check_dependency_p=*/false,
27079 /*class_head_p=*/false,
27080 /*is_declaration=*/false);
27082 if (cxx_dialect >= cxx17
27083 && !cp_parser_parse_definitely (parser))
27085 type_decl = NULL_TREE;
27086 tree tmpl = cp_parser_template_name (parser,
27087 /*template_keyword*/false,
27088 /*check_dependency_p*/false,
27089 /*is_declaration*/false,
27090 none_type,
27091 /*is_identifier*/NULL);
27092 if (DECL_CLASS_TEMPLATE_P (tmpl)
27093 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27094 /* It's a deduction guide, return true. */;
27095 else
27096 cp_parser_simulate_error (parser);
27099 /* If there was no class-name, then this is not a constructor.
27100 Otherwise, if we are in a class-specifier and we aren't
27101 handling a friend declaration, check that its type matches
27102 current_class_type (c++/38313). Note: error_mark_node
27103 is left alone for error recovery purposes. */
27104 constructor_p = (!cp_parser_error_occurred (parser)
27105 && (outside_class_specifier_p
27106 || type_decl == NULL_TREE
27107 || type_decl == error_mark_node
27108 || same_type_p (current_class_type,
27109 TREE_TYPE (type_decl))));
27111 /* If we're still considering a constructor, we have to see a `(',
27112 to begin the parameter-declaration-clause, followed by either a
27113 `)', an `...', or a decl-specifier. We need to check for a
27114 type-specifier to avoid being fooled into thinking that:
27116 S (f) (int);
27118 is a constructor. (It is actually a function named `f' that
27119 takes one parameter (of type `int') and returns a value of type
27120 `S'. */
27121 if (constructor_p
27122 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27123 constructor_p = false;
27125 if (constructor_p
27126 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
27127 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
27128 /* A parameter declaration begins with a decl-specifier,
27129 which is either the "attribute" keyword, a storage class
27130 specifier, or (usually) a type-specifier. */
27131 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
27133 tree type;
27134 tree pushed_scope = NULL_TREE;
27135 unsigned saved_num_template_parameter_lists;
27137 /* Names appearing in the type-specifier should be looked up
27138 in the scope of the class. */
27139 if (current_class_type)
27140 type = NULL_TREE;
27141 else if (type_decl)
27143 type = TREE_TYPE (type_decl);
27144 if (TREE_CODE (type) == TYPENAME_TYPE)
27146 type = resolve_typename_type (type,
27147 /*only_current_p=*/false);
27148 if (TREE_CODE (type) == TYPENAME_TYPE)
27150 cp_parser_abort_tentative_parse (parser);
27151 return false;
27154 pushed_scope = push_scope (type);
27157 /* Inside the constructor parameter list, surrounding
27158 template-parameter-lists do not apply. */
27159 saved_num_template_parameter_lists
27160 = parser->num_template_parameter_lists;
27161 parser->num_template_parameter_lists = 0;
27163 /* Look for the type-specifier. */
27164 cp_parser_type_specifier (parser,
27165 CP_PARSER_FLAGS_NONE,
27166 /*decl_specs=*/NULL,
27167 /*is_declarator=*/true,
27168 /*declares_class_or_enum=*/NULL,
27169 /*is_cv_qualifier=*/NULL);
27171 parser->num_template_parameter_lists
27172 = saved_num_template_parameter_lists;
27174 /* Leave the scope of the class. */
27175 if (pushed_scope)
27176 pop_scope (pushed_scope);
27178 constructor_p = !cp_parser_error_occurred (parser);
27182 /* We did not really want to consume any tokens. */
27183 cp_parser_abort_tentative_parse (parser);
27185 return constructor_p;
27188 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27189 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27190 they must be performed once we are in the scope of the function.
27192 Returns the function defined. */
27194 static tree
27195 cp_parser_function_definition_from_specifiers_and_declarator
27196 (cp_parser* parser,
27197 cp_decl_specifier_seq *decl_specifiers,
27198 tree attributes,
27199 const cp_declarator *declarator)
27201 tree fn;
27202 bool success_p;
27204 /* Begin the function-definition. */
27205 success_p = start_function (decl_specifiers, declarator, attributes);
27207 /* The things we're about to see are not directly qualified by any
27208 template headers we've seen thus far. */
27209 reset_specialization ();
27211 /* If there were names looked up in the decl-specifier-seq that we
27212 did not check, check them now. We must wait until we are in the
27213 scope of the function to perform the checks, since the function
27214 might be a friend. */
27215 perform_deferred_access_checks (tf_warning_or_error);
27217 if (success_p)
27219 cp_finalize_omp_declare_simd (parser, current_function_decl);
27220 parser->omp_declare_simd = NULL;
27221 cp_finalize_oacc_routine (parser, current_function_decl, true);
27222 parser->oacc_routine = NULL;
27225 if (!success_p)
27227 /* Skip the entire function. */
27228 cp_parser_skip_to_end_of_block_or_statement (parser);
27229 fn = error_mark_node;
27231 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27233 /* Seen already, skip it. An error message has already been output. */
27234 cp_parser_skip_to_end_of_block_or_statement (parser);
27235 fn = current_function_decl;
27236 current_function_decl = NULL_TREE;
27237 /* If this is a function from a class, pop the nested class. */
27238 if (current_class_name)
27239 pop_nested_class ();
27241 else
27243 timevar_id_t tv;
27244 if (DECL_DECLARED_INLINE_P (current_function_decl))
27245 tv = TV_PARSE_INLINE;
27246 else
27247 tv = TV_PARSE_FUNC;
27248 timevar_push (tv);
27249 fn = cp_parser_function_definition_after_declarator (parser,
27250 /*inline_p=*/false);
27251 timevar_pop (tv);
27254 return fn;
27257 /* Parse the part of a function-definition that follows the
27258 declarator. INLINE_P is TRUE iff this function is an inline
27259 function defined within a class-specifier.
27261 Returns the function defined. */
27263 static tree
27264 cp_parser_function_definition_after_declarator (cp_parser* parser,
27265 bool inline_p)
27267 tree fn;
27268 bool saved_in_unbraced_linkage_specification_p;
27269 bool saved_in_function_body;
27270 unsigned saved_num_template_parameter_lists;
27271 cp_token *token;
27272 bool fully_implicit_function_template_p
27273 = parser->fully_implicit_function_template_p;
27274 parser->fully_implicit_function_template_p = false;
27275 tree implicit_template_parms
27276 = parser->implicit_template_parms;
27277 parser->implicit_template_parms = 0;
27278 cp_binding_level* implicit_template_scope
27279 = parser->implicit_template_scope;
27280 parser->implicit_template_scope = 0;
27282 saved_in_function_body = parser->in_function_body;
27283 parser->in_function_body = true;
27284 /* If the next token is `return', then the code may be trying to
27285 make use of the "named return value" extension that G++ used to
27286 support. */
27287 token = cp_lexer_peek_token (parser->lexer);
27288 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27290 /* Consume the `return' keyword. */
27291 cp_lexer_consume_token (parser->lexer);
27292 /* Look for the identifier that indicates what value is to be
27293 returned. */
27294 cp_parser_identifier (parser);
27295 /* Issue an error message. */
27296 error_at (token->location,
27297 "named return values are no longer supported");
27298 /* Skip tokens until we reach the start of the function body. */
27299 while (true)
27301 cp_token *token = cp_lexer_peek_token (parser->lexer);
27302 if (token->type == CPP_OPEN_BRACE
27303 || token->type == CPP_EOF
27304 || token->type == CPP_PRAGMA_EOL)
27305 break;
27306 cp_lexer_consume_token (parser->lexer);
27309 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27310 anything declared inside `f'. */
27311 saved_in_unbraced_linkage_specification_p
27312 = parser->in_unbraced_linkage_specification_p;
27313 parser->in_unbraced_linkage_specification_p = false;
27314 /* Inside the function, surrounding template-parameter-lists do not
27315 apply. */
27316 saved_num_template_parameter_lists
27317 = parser->num_template_parameter_lists;
27318 parser->num_template_parameter_lists = 0;
27320 /* If the next token is `try', `__transaction_atomic', or
27321 `__transaction_relaxed`, then we are looking at either function-try-block
27322 or function-transaction-block. Note that all of these include the
27323 function-body. */
27324 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27325 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27326 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27327 RID_TRANSACTION_RELAXED))
27328 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27329 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27330 cp_parser_function_try_block (parser);
27331 else
27332 cp_parser_ctor_initializer_opt_and_function_body
27333 (parser, /*in_function_try_block=*/false);
27335 /* Finish the function. */
27336 fn = finish_function (inline_p);
27337 /* Generate code for it, if necessary. */
27338 expand_or_defer_fn (fn);
27339 /* Restore the saved values. */
27340 parser->in_unbraced_linkage_specification_p
27341 = saved_in_unbraced_linkage_specification_p;
27342 parser->num_template_parameter_lists
27343 = saved_num_template_parameter_lists;
27344 parser->in_function_body = saved_in_function_body;
27346 parser->fully_implicit_function_template_p
27347 = fully_implicit_function_template_p;
27348 parser->implicit_template_parms
27349 = implicit_template_parms;
27350 parser->implicit_template_scope
27351 = implicit_template_scope;
27353 if (parser->fully_implicit_function_template_p)
27354 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27356 return fn;
27359 /* Parse a template-declaration body (following argument list). */
27361 static void
27362 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27363 tree parameter_list,
27364 bool member_p)
27366 tree decl = NULL_TREE;
27367 bool friend_p = false;
27369 /* We just processed one more parameter list. */
27370 ++parser->num_template_parameter_lists;
27372 /* Get the deferred access checks from the parameter list. These
27373 will be checked once we know what is being declared, as for a
27374 member template the checks must be performed in the scope of the
27375 class containing the member. */
27376 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27378 /* Tentatively parse for a new template parameter list, which can either be
27379 the template keyword or a template introduction. */
27380 if (cp_parser_template_declaration_after_export (parser, member_p))
27381 /* OK */;
27382 else if (cxx_dialect >= cxx11
27383 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27384 decl = cp_parser_alias_declaration (parser);
27385 else
27387 /* There are no access checks when parsing a template, as we do not
27388 know if a specialization will be a friend. */
27389 push_deferring_access_checks (dk_no_check);
27390 cp_token *token = cp_lexer_peek_token (parser->lexer);
27391 decl = cp_parser_single_declaration (parser,
27392 checks,
27393 member_p,
27394 /*explicit_specialization_p=*/false,
27395 &friend_p);
27396 pop_deferring_access_checks ();
27398 /* If this is a member template declaration, let the front
27399 end know. */
27400 if (member_p && !friend_p && decl)
27402 if (TREE_CODE (decl) == TYPE_DECL)
27403 cp_parser_check_access_in_redeclaration (decl, token->location);
27405 decl = finish_member_template_decl (decl);
27407 else if (friend_p && decl
27408 && DECL_DECLARES_TYPE_P (decl))
27409 make_friend_class (current_class_type, TREE_TYPE (decl),
27410 /*complain=*/true);
27412 /* We are done with the current parameter list. */
27413 --parser->num_template_parameter_lists;
27415 pop_deferring_access_checks ();
27417 /* Finish up. */
27418 finish_template_decl (parameter_list);
27420 /* Check the template arguments for a literal operator template. */
27421 if (decl
27422 && DECL_DECLARES_FUNCTION_P (decl)
27423 && UDLIT_OPER_P (DECL_NAME (decl)))
27425 bool ok = true;
27426 if (parameter_list == NULL_TREE)
27427 ok = false;
27428 else
27430 int num_parms = TREE_VEC_LENGTH (parameter_list);
27431 if (num_parms == 1)
27433 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27434 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27435 if (CLASS_TYPE_P (TREE_TYPE (parm)))
27436 /* OK, C++20 string literal operator template. We don't need
27437 to warn in lower dialects here because we will have already
27438 warned about the template parameter. */;
27439 else if (TREE_TYPE (parm) != char_type_node
27440 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27441 ok = false;
27443 else if (num_parms == 2 && cxx_dialect >= cxx14)
27445 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27446 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27447 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27448 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27449 if (parm == error_mark_node
27450 || TREE_TYPE (parm) != TREE_TYPE (type)
27451 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27452 ok = false;
27453 else
27454 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27455 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
27456 "ISO C++ did not adopt string literal operator templa"
27457 "tes taking an argument pack of characters");
27459 else
27460 ok = false;
27462 if (!ok)
27464 if (cxx_dialect > cxx17)
27465 error ("literal operator template %qD has invalid parameter list;"
27466 " Expected non-type template parameter pack <char...> "
27467 " or single non-type parameter of class type",
27468 decl);
27469 else
27470 error ("literal operator template %qD has invalid parameter list."
27471 " Expected non-type template parameter pack <char...>",
27472 decl);
27476 /* Register member declarations. */
27477 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27478 finish_member_declaration (decl);
27479 /* If DECL is a function template, we must return to parse it later.
27480 (Even though there is no definition, there might be default
27481 arguments that need handling.) */
27482 if (member_p && decl
27483 && DECL_DECLARES_FUNCTION_P (decl))
27484 vec_safe_push (unparsed_funs_with_definitions, decl);
27487 /* Parse a template introduction header for a template-declaration. Returns
27488 false if tentative parse fails. */
27490 static bool
27491 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27493 cp_parser_parse_tentatively (parser);
27495 tree saved_scope = parser->scope;
27496 tree saved_object_scope = parser->object_scope;
27497 tree saved_qualifying_scope = parser->qualifying_scope;
27499 /* Look for the optional `::' operator. */
27500 cp_parser_global_scope_opt (parser,
27501 /*current_scope_valid_p=*/false);
27502 /* Look for the nested-name-specifier. */
27503 cp_parser_nested_name_specifier_opt (parser,
27504 /*typename_keyword_p=*/false,
27505 /*check_dependency_p=*/true,
27506 /*type_p=*/false,
27507 /*is_declaration=*/false);
27509 cp_token *token = cp_lexer_peek_token (parser->lexer);
27510 tree concept_name = cp_parser_identifier (parser);
27512 /* Look up the concept for which we will be matching
27513 template parameters. */
27514 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27515 token->location);
27516 parser->scope = saved_scope;
27517 parser->object_scope = saved_object_scope;
27518 parser->qualifying_scope = saved_qualifying_scope;
27520 if (concept_name == error_mark_node)
27521 cp_parser_simulate_error (parser);
27523 /* Look for opening brace for introduction. */
27524 matching_braces braces;
27525 braces.require_open (parser);
27527 if (!cp_parser_parse_definitely (parser))
27528 return false;
27530 push_deferring_access_checks (dk_deferred);
27532 /* Build vector of placeholder parameters and grab
27533 matching identifiers. */
27534 tree introduction_list = cp_parser_introduction_list (parser);
27536 /* Look for closing brace for introduction. */
27537 if (!braces.require_close (parser))
27538 return true;
27540 /* The introduction-list shall not be empty. */
27541 int nargs = TREE_VEC_LENGTH (introduction_list);
27542 if (nargs == 0)
27544 /* In cp_parser_introduction_list we have already issued an error. */
27545 return true;
27548 if (tmpl_decl == error_mark_node)
27550 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27551 token->location);
27552 return true;
27555 /* Build and associate the constraint. */
27556 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27557 if (parms && parms != error_mark_node)
27559 cp_parser_template_declaration_after_parameters (parser, parms,
27560 member_p);
27561 return true;
27564 error_at (token->location, "no matching concept for template-introduction");
27565 return true;
27568 /* Parse a normal template-declaration following the template keyword. */
27570 static void
27571 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27573 tree parameter_list;
27574 bool need_lang_pop;
27575 location_t location = input_location;
27577 /* Look for the `<' token. */
27578 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27579 return;
27580 if (at_class_scope_p () && current_function_decl)
27582 /* 14.5.2.2 [temp.mem]
27584 A local class shall not have member templates. */
27585 error_at (location,
27586 "invalid declaration of member template in local class");
27587 cp_parser_skip_to_end_of_block_or_statement (parser);
27588 return;
27590 /* [temp]
27592 A template ... shall not have C linkage. */
27593 if (current_lang_name == lang_name_c)
27595 error_at (location, "template with C linkage");
27596 maybe_show_extern_c_location ();
27597 /* Give it C++ linkage to avoid confusing other parts of the
27598 front end. */
27599 push_lang_context (lang_name_cplusplus);
27600 need_lang_pop = true;
27602 else
27603 need_lang_pop = false;
27605 /* We cannot perform access checks on the template parameter
27606 declarations until we know what is being declared, just as we
27607 cannot check the decl-specifier list. */
27608 push_deferring_access_checks (dk_deferred);
27610 /* If the next token is `>', then we have an invalid
27611 specialization. Rather than complain about an invalid template
27612 parameter, issue an error message here. */
27613 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27615 cp_parser_error (parser, "invalid explicit specialization");
27616 begin_specialization ();
27617 parameter_list = NULL_TREE;
27619 else
27621 /* Parse the template parameters. */
27622 parameter_list = cp_parser_template_parameter_list (parser);
27625 /* Look for the `>'. */
27626 cp_parser_skip_to_end_of_template_parameter_list (parser);
27628 /* Manage template requirements */
27629 if (flag_concepts)
27631 tree reqs = get_shorthand_constraints (current_template_parms);
27632 if (tree r = cp_parser_requires_clause_opt (parser))
27633 reqs = conjoin_constraints (reqs, normalize_expression (r));
27634 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27637 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27638 member_p);
27640 /* For the erroneous case of a template with C linkage, we pushed an
27641 implicit C++ linkage scope; exit that scope now. */
27642 if (need_lang_pop)
27643 pop_lang_context ();
27646 /* Parse a template-declaration, assuming that the `export' (and
27647 `extern') keywords, if present, has already been scanned. MEMBER_P
27648 is as for cp_parser_template_declaration. */
27650 static bool
27651 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27653 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27655 cp_lexer_consume_token (parser->lexer);
27656 cp_parser_explicit_template_declaration (parser, member_p);
27657 return true;
27659 else if (flag_concepts)
27660 return cp_parser_template_introduction (parser, member_p);
27662 return false;
27665 /* Perform the deferred access checks from a template-parameter-list.
27666 CHECKS is a TREE_LIST of access checks, as returned by
27667 get_deferred_access_checks. */
27669 static void
27670 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27672 ++processing_template_parmlist;
27673 perform_access_checks (checks, tf_warning_or_error);
27674 --processing_template_parmlist;
27677 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27678 `function-definition' sequence that follows a template header.
27679 If MEMBER_P is true, this declaration appears in a class scope.
27681 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27682 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27684 static tree
27685 cp_parser_single_declaration (cp_parser* parser,
27686 vec<deferred_access_check, va_gc> *checks,
27687 bool member_p,
27688 bool explicit_specialization_p,
27689 bool* friend_p)
27691 int declares_class_or_enum;
27692 tree decl = NULL_TREE;
27693 cp_decl_specifier_seq decl_specifiers;
27694 bool function_definition_p = false;
27695 cp_token *decl_spec_token_start;
27697 /* This function is only used when processing a template
27698 declaration. */
27699 gcc_assert (innermost_scope_kind () == sk_template_parms
27700 || innermost_scope_kind () == sk_template_spec);
27702 /* Defer access checks until we know what is being declared. */
27703 push_deferring_access_checks (dk_deferred);
27705 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27706 alternative. */
27707 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27708 cp_parser_decl_specifier_seq (parser,
27709 CP_PARSER_FLAGS_OPTIONAL,
27710 &decl_specifiers,
27711 &declares_class_or_enum);
27712 if (friend_p)
27713 *friend_p = cp_parser_friend_p (&decl_specifiers);
27715 /* There are no template typedefs. */
27716 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27718 error_at (decl_spec_token_start->location,
27719 "template declaration of %<typedef%>");
27720 decl = error_mark_node;
27723 /* Gather up the access checks that occurred the
27724 decl-specifier-seq. */
27725 stop_deferring_access_checks ();
27727 /* Check for the declaration of a template class. */
27728 if (declares_class_or_enum)
27730 if (cp_parser_declares_only_class_p (parser)
27731 || (declares_class_or_enum & 2))
27733 // If this is a declaration, but not a definition, associate
27734 // any constraints with the type declaration. Constraints
27735 // are associated with definitions in cp_parser_class_specifier.
27736 if (declares_class_or_enum == 1)
27737 associate_classtype_constraints (decl_specifiers.type);
27739 decl = shadow_tag (&decl_specifiers);
27741 /* In this case:
27743 struct C {
27744 friend template <typename T> struct A<T>::B;
27747 A<T>::B will be represented by a TYPENAME_TYPE, and
27748 therefore not recognized by shadow_tag. */
27749 if (friend_p && *friend_p
27750 && !decl
27751 && decl_specifiers.type
27752 && TYPE_P (decl_specifiers.type))
27753 decl = decl_specifiers.type;
27755 if (decl && decl != error_mark_node)
27756 decl = TYPE_NAME (decl);
27757 else
27758 decl = error_mark_node;
27760 /* Perform access checks for template parameters. */
27761 cp_parser_perform_template_parameter_access_checks (checks);
27763 /* Give a helpful diagnostic for
27764 template <class T> struct A { } a;
27765 if we aren't already recovering from an error. */
27766 if (!cp_parser_declares_only_class_p (parser)
27767 && !seen_error ())
27769 error_at (cp_lexer_peek_token (parser->lexer)->location,
27770 "a class template declaration must not declare "
27771 "anything else");
27772 cp_parser_skip_to_end_of_block_or_statement (parser);
27773 goto out;
27778 /* Complain about missing 'typename' or other invalid type names. */
27779 if (!decl_specifiers.any_type_specifiers_p
27780 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27782 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27783 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27784 the rest of this declaration. */
27785 decl = error_mark_node;
27786 goto out;
27789 /* If it's not a template class, try for a template function. If
27790 the next token is a `;', then this declaration does not declare
27791 anything. But, if there were errors in the decl-specifiers, then
27792 the error might well have come from an attempted class-specifier.
27793 In that case, there's no need to warn about a missing declarator. */
27794 if (!decl
27795 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27796 || decl_specifiers.type != error_mark_node))
27798 decl = cp_parser_init_declarator (parser,
27799 &decl_specifiers,
27800 checks,
27801 /*function_definition_allowed_p=*/true,
27802 member_p,
27803 declares_class_or_enum,
27804 &function_definition_p,
27805 NULL, NULL, NULL);
27807 /* 7.1.1-1 [dcl.stc]
27809 A storage-class-specifier shall not be specified in an explicit
27810 specialization... */
27811 if (decl
27812 && explicit_specialization_p
27813 && decl_specifiers.storage_class != sc_none)
27815 error_at (decl_spec_token_start->location,
27816 "explicit template specialization cannot have a storage class");
27817 decl = error_mark_node;
27820 if (decl && VAR_P (decl))
27821 check_template_variable (decl);
27824 /* Look for a trailing `;' after the declaration. */
27825 if (!function_definition_p
27826 && (decl == error_mark_node
27827 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27828 cp_parser_skip_to_end_of_block_or_statement (parser);
27830 out:
27831 pop_deferring_access_checks ();
27833 /* Clear any current qualification; whatever comes next is the start
27834 of something new. */
27835 parser->scope = NULL_TREE;
27836 parser->qualifying_scope = NULL_TREE;
27837 parser->object_scope = NULL_TREE;
27839 return decl;
27842 /* Parse a cast-expression that is not the operand of a unary "&". */
27844 static cp_expr
27845 cp_parser_simple_cast_expression (cp_parser *parser)
27847 return cp_parser_cast_expression (parser, /*address_p=*/false,
27848 /*cast_p=*/false, /*decltype*/false, NULL);
27851 /* Parse a functional cast to TYPE. Returns an expression
27852 representing the cast. */
27854 static cp_expr
27855 cp_parser_functional_cast (cp_parser* parser, tree type)
27857 vec<tree, va_gc> *vec;
27858 tree expression_list;
27859 cp_expr cast;
27860 bool nonconst_p;
27862 location_t start_loc = input_location;
27864 if (!type)
27865 type = error_mark_node;
27867 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27869 cp_lexer_set_source_position (parser->lexer);
27870 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27871 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27872 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27873 if (TREE_CODE (type) == TYPE_DECL)
27874 type = TREE_TYPE (type);
27876 cast = finish_compound_literal (type, expression_list,
27877 tf_warning_or_error, fcl_functional);
27878 /* Create a location of the form:
27879 type_name{i, f}
27880 ^~~~~~~~~~~~~~~
27881 with caret == start at the start of the type name,
27882 finishing at the closing brace. */
27883 location_t finish_loc
27884 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27885 location_t combined_loc = make_location (start_loc, start_loc,
27886 finish_loc);
27887 cast.set_location (combined_loc);
27888 return cast;
27892 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27893 /*cast_p=*/true,
27894 /*allow_expansion_p=*/true,
27895 /*non_constant_p=*/NULL);
27896 if (vec == NULL)
27897 expression_list = error_mark_node;
27898 else
27900 expression_list = build_tree_list_vec (vec);
27901 release_tree_vector (vec);
27904 cast = build_functional_cast (type, expression_list,
27905 tf_warning_or_error);
27906 /* [expr.const]/1: In an integral constant expression "only type
27907 conversions to integral or enumeration type can be used". */
27908 if (TREE_CODE (type) == TYPE_DECL)
27909 type = TREE_TYPE (type);
27910 if (cast != error_mark_node
27911 && !cast_valid_in_integral_constant_expression_p (type)
27912 && cp_parser_non_integral_constant_expression (parser,
27913 NIC_CONSTRUCTOR))
27914 return error_mark_node;
27916 /* Create a location of the form:
27917 float(i)
27918 ^~~~~~~~
27919 with caret == start at the start of the type name,
27920 finishing at the closing paren. */
27921 location_t finish_loc
27922 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27923 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27924 cast.set_location (combined_loc);
27925 return cast;
27928 /* Save the tokens that make up the body of a member function defined
27929 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27930 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27931 specifiers applied to the declaration. Returns the FUNCTION_DECL
27932 for the member function. */
27934 static tree
27935 cp_parser_save_member_function_body (cp_parser* parser,
27936 cp_decl_specifier_seq *decl_specifiers,
27937 cp_declarator *declarator,
27938 tree attributes)
27940 cp_token *first;
27941 cp_token *last;
27942 tree fn;
27943 bool function_try_block = false;
27945 /* Create the FUNCTION_DECL. */
27946 fn = grokmethod (decl_specifiers, declarator, attributes);
27947 cp_finalize_omp_declare_simd (parser, fn);
27948 cp_finalize_oacc_routine (parser, fn, true);
27949 /* If something went badly wrong, bail out now. */
27950 if (fn == error_mark_node)
27952 /* If there's a function-body, skip it. */
27953 if (cp_parser_token_starts_function_definition_p
27954 (cp_lexer_peek_token (parser->lexer)))
27955 cp_parser_skip_to_end_of_block_or_statement (parser);
27956 return error_mark_node;
27959 /* Remember it, if there default args to post process. */
27960 cp_parser_save_default_args (parser, fn);
27962 /* Save away the tokens that make up the body of the
27963 function. */
27964 first = parser->lexer->next_token;
27966 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27967 cp_lexer_consume_token (parser->lexer);
27968 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27969 RID_TRANSACTION_ATOMIC))
27971 cp_lexer_consume_token (parser->lexer);
27972 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27973 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27974 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27975 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27976 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27977 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27978 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27980 cp_lexer_consume_token (parser->lexer);
27981 cp_lexer_consume_token (parser->lexer);
27982 cp_lexer_consume_token (parser->lexer);
27983 cp_lexer_consume_token (parser->lexer);
27984 cp_lexer_consume_token (parser->lexer);
27986 else
27987 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27988 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27990 cp_lexer_consume_token (parser->lexer);
27991 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27992 break;
27996 /* Handle function try blocks. */
27997 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27999 cp_lexer_consume_token (parser->lexer);
28000 function_try_block = true;
28002 /* We can have braced-init-list mem-initializers before the fn body. */
28003 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28005 cp_lexer_consume_token (parser->lexer);
28006 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
28008 /* cache_group will stop after an un-nested { } pair, too. */
28009 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28010 break;
28012 /* variadic mem-inits have ... after the ')'. */
28013 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28014 cp_lexer_consume_token (parser->lexer);
28017 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28018 /* Handle function try blocks. */
28019 if (function_try_block)
28020 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
28021 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28022 last = parser->lexer->next_token;
28024 /* Save away the inline definition; we will process it when the
28025 class is complete. */
28026 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
28027 DECL_PENDING_INLINE_P (fn) = 1;
28029 /* We need to know that this was defined in the class, so that
28030 friend templates are handled correctly. */
28031 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
28033 /* Add FN to the queue of functions to be parsed later. */
28034 vec_safe_push (unparsed_funs_with_definitions, fn);
28036 return fn;
28039 /* Save the tokens that make up the in-class initializer for a non-static
28040 data member. Returns a DEFAULT_ARG. */
28042 static tree
28043 cp_parser_save_nsdmi (cp_parser* parser)
28045 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
28048 /* Parse a template-argument-list, as well as the trailing ">" (but
28049 not the opening "<"). See cp_parser_template_argument_list for the
28050 return value. */
28052 static tree
28053 cp_parser_enclosed_template_argument_list (cp_parser* parser)
28055 tree arguments;
28056 tree saved_scope;
28057 tree saved_qualifying_scope;
28058 tree saved_object_scope;
28059 bool saved_greater_than_is_operator_p;
28061 /* [temp.names]
28063 When parsing a template-id, the first non-nested `>' is taken as
28064 the end of the template-argument-list rather than a greater-than
28065 operator. */
28066 saved_greater_than_is_operator_p
28067 = parser->greater_than_is_operator_p;
28068 parser->greater_than_is_operator_p = false;
28069 /* Parsing the argument list may modify SCOPE, so we save it
28070 here. */
28071 saved_scope = parser->scope;
28072 saved_qualifying_scope = parser->qualifying_scope;
28073 saved_object_scope = parser->object_scope;
28074 /* We need to evaluate the template arguments, even though this
28075 template-id may be nested within a "sizeof". */
28076 cp_evaluated ev;
28077 /* Parse the template-argument-list itself. */
28078 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
28079 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28080 arguments = NULL_TREE;
28081 else
28082 arguments = cp_parser_template_argument_list (parser);
28083 /* Look for the `>' that ends the template-argument-list. If we find
28084 a '>>' instead, it's probably just a typo. */
28085 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28087 if (cxx_dialect != cxx98)
28089 /* In C++0x, a `>>' in a template argument list or cast
28090 expression is considered to be two separate `>'
28091 tokens. So, change the current token to a `>', but don't
28092 consume it: it will be consumed later when the outer
28093 template argument list (or cast expression) is parsed.
28094 Note that this replacement of `>' for `>>' is necessary
28095 even if we are parsing tentatively: in the tentative
28096 case, after calling
28097 cp_parser_enclosed_template_argument_list we will always
28098 throw away all of the template arguments and the first
28099 closing `>', either because the template argument list
28100 was erroneous or because we are replacing those tokens
28101 with a CPP_TEMPLATE_ID token. The second `>' (which will
28102 not have been thrown away) is needed either to close an
28103 outer template argument list or to complete a new-style
28104 cast. */
28105 cp_token *token = cp_lexer_peek_token (parser->lexer);
28106 token->type = CPP_GREATER;
28108 else if (!saved_greater_than_is_operator_p)
28110 /* If we're in a nested template argument list, the '>>' has
28111 to be a typo for '> >'. We emit the error message, but we
28112 continue parsing and we push a '>' as next token, so that
28113 the argument list will be parsed correctly. Note that the
28114 global source location is still on the token before the
28115 '>>', so we need to say explicitly where we want it. */
28116 cp_token *token = cp_lexer_peek_token (parser->lexer);
28117 gcc_rich_location richloc (token->location);
28118 richloc.add_fixit_replace ("> >");
28119 error_at (&richloc, "%<>>%> should be %<> >%> "
28120 "within a nested template argument list");
28122 token->type = CPP_GREATER;
28124 else
28126 /* If this is not a nested template argument list, the '>>'
28127 is a typo for '>'. Emit an error message and continue.
28128 Same deal about the token location, but here we can get it
28129 right by consuming the '>>' before issuing the diagnostic. */
28130 cp_token *token = cp_lexer_consume_token (parser->lexer);
28131 error_at (token->location,
28132 "spurious %<>>%>, use %<>%> to terminate "
28133 "a template argument list");
28136 else
28137 cp_parser_skip_to_end_of_template_parameter_list (parser);
28138 /* The `>' token might be a greater-than operator again now. */
28139 parser->greater_than_is_operator_p
28140 = saved_greater_than_is_operator_p;
28141 /* Restore the SAVED_SCOPE. */
28142 parser->scope = saved_scope;
28143 parser->qualifying_scope = saved_qualifying_scope;
28144 parser->object_scope = saved_object_scope;
28146 return arguments;
28149 /* MEMBER_FUNCTION is a member function, or a friend. If default
28150 arguments, or the body of the function have not yet been parsed,
28151 parse them now. */
28153 static void
28154 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
28156 timevar_push (TV_PARSE_INMETH);
28157 /* If this member is a template, get the underlying
28158 FUNCTION_DECL. */
28159 if (DECL_FUNCTION_TEMPLATE_P (member_function))
28160 member_function = DECL_TEMPLATE_RESULT (member_function);
28162 /* There should not be any class definitions in progress at this
28163 point; the bodies of members are only parsed outside of all class
28164 definitions. */
28165 gcc_assert (parser->num_classes_being_defined == 0);
28166 /* While we're parsing the member functions we might encounter more
28167 classes. We want to handle them right away, but we don't want
28168 them getting mixed up with functions that are currently in the
28169 queue. */
28170 push_unparsed_function_queues (parser);
28172 /* Make sure that any template parameters are in scope. */
28173 maybe_begin_member_template_processing (member_function);
28175 /* If the body of the function has not yet been parsed, parse it
28176 now. */
28177 if (DECL_PENDING_INLINE_P (member_function))
28179 tree function_scope;
28180 cp_token_cache *tokens;
28182 /* The function is no longer pending; we are processing it. */
28183 tokens = DECL_PENDING_INLINE_INFO (member_function);
28184 DECL_PENDING_INLINE_INFO (member_function) = NULL;
28185 DECL_PENDING_INLINE_P (member_function) = 0;
28187 /* If this is a local class, enter the scope of the containing
28188 function. */
28189 function_scope = current_function_decl;
28190 if (function_scope)
28191 push_function_context ();
28193 /* Push the body of the function onto the lexer stack. */
28194 cp_parser_push_lexer_for_tokens (parser, tokens);
28196 /* Let the front end know that we going to be defining this
28197 function. */
28198 start_preparsed_function (member_function, NULL_TREE,
28199 SF_PRE_PARSED | SF_INCLASS_INLINE);
28201 /* Don't do access checking if it is a templated function. */
28202 if (processing_template_decl)
28203 push_deferring_access_checks (dk_no_check);
28205 /* #pragma omp declare reduction needs special parsing. */
28206 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28208 parser->lexer->in_pragma = true;
28209 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28210 finish_function (/*inline_p=*/true);
28211 cp_check_omp_declare_reduction (member_function);
28213 else
28214 /* Now, parse the body of the function. */
28215 cp_parser_function_definition_after_declarator (parser,
28216 /*inline_p=*/true);
28218 if (processing_template_decl)
28219 pop_deferring_access_checks ();
28221 /* Leave the scope of the containing function. */
28222 if (function_scope)
28223 pop_function_context ();
28224 cp_parser_pop_lexer (parser);
28227 /* Remove any template parameters from the symbol table. */
28228 maybe_end_member_template_processing ();
28230 /* Restore the queue. */
28231 pop_unparsed_function_queues (parser);
28232 timevar_pop (TV_PARSE_INMETH);
28235 /* If DECL contains any default args, remember it on the unparsed
28236 functions queue. */
28238 static void
28239 cp_parser_save_default_args (cp_parser* parser, tree decl)
28241 tree probe;
28243 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28244 probe;
28245 probe = TREE_CHAIN (probe))
28246 if (TREE_PURPOSE (probe))
28248 cp_default_arg_entry entry = {current_class_type, decl};
28249 vec_safe_push (unparsed_funs_with_default_args, entry);
28250 break;
28254 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28255 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28256 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28257 from the parameter-type-list. */
28259 static tree
28260 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28261 tree default_arg, tree parmtype)
28263 cp_token_cache *tokens;
28264 tree parsed_arg;
28265 bool dummy;
28267 if (default_arg == error_mark_node)
28268 return error_mark_node;
28270 /* Push the saved tokens for the default argument onto the parser's
28271 lexer stack. */
28272 tokens = DEFARG_TOKENS (default_arg);
28273 cp_parser_push_lexer_for_tokens (parser, tokens);
28275 start_lambda_scope (decl);
28277 /* Parse the default argument. */
28278 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28279 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28280 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28282 finish_lambda_scope ();
28284 if (parsed_arg == error_mark_node)
28285 cp_parser_skip_to_end_of_statement (parser);
28287 if (!processing_template_decl)
28289 /* In a non-template class, check conversions now. In a template,
28290 we'll wait and instantiate these as needed. */
28291 if (TREE_CODE (decl) == PARM_DECL)
28292 parsed_arg = check_default_argument (parmtype, parsed_arg,
28293 tf_warning_or_error);
28294 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28295 parsed_arg = error_mark_node;
28296 else
28297 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28300 /* If the token stream has not been completely used up, then
28301 there was extra junk after the end of the default
28302 argument. */
28303 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28305 if (TREE_CODE (decl) == PARM_DECL)
28306 cp_parser_error (parser, "expected %<,%>");
28307 else
28308 cp_parser_error (parser, "expected %<;%>");
28311 /* Revert to the main lexer. */
28312 cp_parser_pop_lexer (parser);
28314 return parsed_arg;
28317 /* FIELD is a non-static data member with an initializer which we saved for
28318 later; parse it now. */
28320 static void
28321 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28323 tree def;
28325 maybe_begin_member_template_processing (field);
28327 push_unparsed_function_queues (parser);
28328 def = cp_parser_late_parse_one_default_arg (parser, field,
28329 DECL_INITIAL (field),
28330 NULL_TREE);
28331 pop_unparsed_function_queues (parser);
28333 maybe_end_member_template_processing ();
28335 DECL_INITIAL (field) = def;
28338 /* FN is a FUNCTION_DECL which may contains a parameter with an
28339 unparsed DEFAULT_ARG. Parse the default args now. This function
28340 assumes that the current scope is the scope in which the default
28341 argument should be processed. */
28343 static void
28344 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28346 bool saved_local_variables_forbidden_p;
28347 tree parm, parmdecl;
28349 /* While we're parsing the default args, we might (due to the
28350 statement expression extension) encounter more classes. We want
28351 to handle them right away, but we don't want them getting mixed
28352 up with default args that are currently in the queue. */
28353 push_unparsed_function_queues (parser);
28355 /* Local variable names (and the `this' keyword) may not appear
28356 in a default argument. */
28357 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28358 parser->local_variables_forbidden_p = true;
28360 push_defarg_context (fn);
28362 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28363 parmdecl = DECL_ARGUMENTS (fn);
28364 parm && parm != void_list_node;
28365 parm = TREE_CHAIN (parm),
28366 parmdecl = DECL_CHAIN (parmdecl))
28368 tree default_arg = TREE_PURPOSE (parm);
28369 tree parsed_arg;
28370 vec<tree, va_gc> *insts;
28371 tree copy;
28372 unsigned ix;
28374 if (!default_arg)
28375 continue;
28377 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28378 /* This can happen for a friend declaration for a function
28379 already declared with default arguments. */
28380 continue;
28382 parsed_arg
28383 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28384 default_arg,
28385 TREE_VALUE (parm));
28386 TREE_PURPOSE (parm) = parsed_arg;
28388 /* Update any instantiations we've already created. */
28389 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28390 vec_safe_iterate (insts, ix, &copy); ix++)
28391 TREE_PURPOSE (copy) = parsed_arg;
28394 pop_defarg_context ();
28396 /* Make sure no default arg is missing. */
28397 check_default_args (fn);
28399 /* Restore the state of local_variables_forbidden_p. */
28400 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28402 /* Restore the queue. */
28403 pop_unparsed_function_queues (parser);
28406 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28408 sizeof ... ( identifier )
28410 where the 'sizeof' token has already been consumed. */
28412 static tree
28413 cp_parser_sizeof_pack (cp_parser *parser)
28415 /* Consume the `...'. */
28416 cp_lexer_consume_token (parser->lexer);
28417 maybe_warn_variadic_templates ();
28419 matching_parens parens;
28420 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28421 if (paren)
28422 parens.consume_open (parser);
28423 else
28424 permerror (cp_lexer_peek_token (parser->lexer)->location,
28425 "%<sizeof...%> argument must be surrounded by parentheses");
28427 cp_token *token = cp_lexer_peek_token (parser->lexer);
28428 tree name = cp_parser_identifier (parser);
28429 if (name == error_mark_node)
28430 return error_mark_node;
28431 /* The name is not qualified. */
28432 parser->scope = NULL_TREE;
28433 parser->qualifying_scope = NULL_TREE;
28434 parser->object_scope = NULL_TREE;
28435 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28436 if (expr == error_mark_node)
28437 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28438 token->location);
28439 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28440 expr = TREE_TYPE (expr);
28441 else if (TREE_CODE (expr) == CONST_DECL)
28442 expr = DECL_INITIAL (expr);
28443 expr = make_pack_expansion (expr);
28444 PACK_EXPANSION_SIZEOF_P (expr) = true;
28446 if (paren)
28447 parens.require_close (parser);
28449 return expr;
28452 /* Parse the operand of `sizeof' (or a similar operator). Returns
28453 either a TYPE or an expression, depending on the form of the
28454 input. The KEYWORD indicates which kind of expression we have
28455 encountered. */
28457 static tree
28458 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28460 tree expr = NULL_TREE;
28461 const char *saved_message;
28462 char *tmp;
28463 bool saved_integral_constant_expression_p;
28464 bool saved_non_integral_constant_expression_p;
28466 /* If it's a `...', then we are computing the length of a parameter
28467 pack. */
28468 if (keyword == RID_SIZEOF
28469 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28470 return cp_parser_sizeof_pack (parser);
28472 /* Types cannot be defined in a `sizeof' expression. Save away the
28473 old message. */
28474 saved_message = parser->type_definition_forbidden_message;
28475 /* And create the new one. */
28476 tmp = concat ("types may not be defined in %<",
28477 IDENTIFIER_POINTER (ridpointers[keyword]),
28478 "%> expressions", NULL);
28479 parser->type_definition_forbidden_message = tmp;
28481 /* The restrictions on constant-expressions do not apply inside
28482 sizeof expressions. */
28483 saved_integral_constant_expression_p
28484 = parser->integral_constant_expression_p;
28485 saved_non_integral_constant_expression_p
28486 = parser->non_integral_constant_expression_p;
28487 parser->integral_constant_expression_p = false;
28489 /* Do not actually evaluate the expression. */
28490 ++cp_unevaluated_operand;
28491 ++c_inhibit_evaluation_warnings;
28492 /* If it's a `(', then we might be looking at the type-id
28493 construction. */
28494 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28496 tree type = NULL_TREE;
28498 /* We can't be sure yet whether we're looking at a type-id or an
28499 expression. */
28500 cp_parser_parse_tentatively (parser);
28502 matching_parens parens;
28503 parens.consume_open (parser);
28505 /* Note: as a GNU Extension, compound literals are considered
28506 postfix-expressions as they are in C99, so they are valid
28507 arguments to sizeof. See comment in cp_parser_cast_expression
28508 for details. */
28509 if (cp_parser_compound_literal_p (parser))
28510 cp_parser_simulate_error (parser);
28511 else
28513 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28514 parser->in_type_id_in_expr_p = true;
28515 /* Look for the type-id. */
28516 type = cp_parser_type_id (parser);
28517 /* Look for the closing `)'. */
28518 parens.require_close (parser);
28519 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28522 /* If all went well, then we're done. */
28523 if (cp_parser_parse_definitely (parser))
28524 expr = type;
28527 /* If the type-id production did not work out, then we must be
28528 looking at the unary-expression production. */
28529 if (!expr)
28530 expr = cp_parser_unary_expression (parser);
28532 /* Go back to evaluating expressions. */
28533 --cp_unevaluated_operand;
28534 --c_inhibit_evaluation_warnings;
28536 /* Free the message we created. */
28537 free (tmp);
28538 /* And restore the old one. */
28539 parser->type_definition_forbidden_message = saved_message;
28540 parser->integral_constant_expression_p
28541 = saved_integral_constant_expression_p;
28542 parser->non_integral_constant_expression_p
28543 = saved_non_integral_constant_expression_p;
28545 return expr;
28548 /* If the current declaration has no declarator, return true. */
28550 static bool
28551 cp_parser_declares_only_class_p (cp_parser *parser)
28553 /* If the next token is a `;' or a `,' then there is no
28554 declarator. */
28555 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28556 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28559 /* Update the DECL_SPECS to reflect the storage class indicated by
28560 KEYWORD. */
28562 static void
28563 cp_parser_set_storage_class (cp_parser *parser,
28564 cp_decl_specifier_seq *decl_specs,
28565 enum rid keyword,
28566 cp_token *token)
28568 cp_storage_class storage_class;
28570 if (parser->in_unbraced_linkage_specification_p)
28572 error_at (token->location, "invalid use of %qD in linkage specification",
28573 ridpointers[keyword]);
28574 return;
28576 else if (decl_specs->storage_class != sc_none)
28578 decl_specs->conflicting_specifiers_p = true;
28579 return;
28582 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28583 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28584 && decl_specs->gnu_thread_keyword_p)
28586 pedwarn (decl_specs->locations[ds_thread], 0,
28587 "%<__thread%> before %qD", ridpointers[keyword]);
28590 switch (keyword)
28592 case RID_AUTO:
28593 storage_class = sc_auto;
28594 break;
28595 case RID_REGISTER:
28596 storage_class = sc_register;
28597 break;
28598 case RID_STATIC:
28599 storage_class = sc_static;
28600 break;
28601 case RID_EXTERN:
28602 storage_class = sc_extern;
28603 break;
28604 case RID_MUTABLE:
28605 storage_class = sc_mutable;
28606 break;
28607 default:
28608 gcc_unreachable ();
28610 decl_specs->storage_class = storage_class;
28611 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28613 /* A storage class specifier cannot be applied alongside a typedef
28614 specifier. If there is a typedef specifier present then set
28615 conflicting_specifiers_p which will trigger an error later
28616 on in grokdeclarator. */
28617 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28618 decl_specs->conflicting_specifiers_p = true;
28621 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28622 is true, the type is a class or enum definition. */
28624 static void
28625 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28626 tree type_spec,
28627 cp_token *token,
28628 bool type_definition_p)
28630 decl_specs->any_specifiers_p = true;
28632 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28633 (with, for example, in "typedef int wchar_t;") we remember that
28634 this is what happened. In system headers, we ignore these
28635 declarations so that G++ can work with system headers that are not
28636 C++-safe. */
28637 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28638 && !type_definition_p
28639 && (type_spec == boolean_type_node
28640 || type_spec == char16_type_node
28641 || type_spec == char32_type_node
28642 || type_spec == wchar_type_node)
28643 && (decl_specs->type
28644 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28645 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28646 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28647 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28649 decl_specs->redefined_builtin_type = type_spec;
28650 set_and_check_decl_spec_loc (decl_specs,
28651 ds_redefined_builtin_type_spec,
28652 token);
28653 if (!decl_specs->type)
28655 decl_specs->type = type_spec;
28656 decl_specs->type_definition_p = false;
28657 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28660 else if (decl_specs->type)
28661 decl_specs->multiple_types_p = true;
28662 else
28664 decl_specs->type = type_spec;
28665 decl_specs->type_definition_p = type_definition_p;
28666 decl_specs->redefined_builtin_type = NULL_TREE;
28667 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28671 /* True iff TOKEN is the GNU keyword __thread. */
28673 static bool
28674 token_is__thread (cp_token *token)
28676 gcc_assert (token->keyword == RID_THREAD);
28677 return id_equal (token->u.value, "__thread");
28680 /* Set the location for a declarator specifier and check if it is
28681 duplicated.
28683 DECL_SPECS is the sequence of declarator specifiers onto which to
28684 set the location.
28686 DS is the single declarator specifier to set which location is to
28687 be set onto the existing sequence of declarators.
28689 LOCATION is the location for the declarator specifier to
28690 consider. */
28692 static void
28693 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28694 cp_decl_spec ds, cp_token *token)
28696 gcc_assert (ds < ds_last);
28698 if (decl_specs == NULL)
28699 return;
28701 location_t location = token->location;
28703 if (decl_specs->locations[ds] == 0)
28705 decl_specs->locations[ds] = location;
28706 if (ds == ds_thread)
28707 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28709 else
28711 if (ds == ds_long)
28713 if (decl_specs->locations[ds_long_long] != 0)
28714 error_at (location,
28715 "%<long long long%> is too long for GCC");
28716 else
28718 decl_specs->locations[ds_long_long] = location;
28719 pedwarn_cxx98 (location,
28720 OPT_Wlong_long,
28721 "ISO C++ 1998 does not support %<long long%>");
28724 else if (ds == ds_thread)
28726 bool gnu = token_is__thread (token);
28727 gcc_rich_location richloc (location);
28728 if (gnu != decl_specs->gnu_thread_keyword_p)
28730 richloc.add_range (decl_specs->locations[ds_thread]);
28731 error_at (&richloc,
28732 "both %<__thread%> and %<thread_local%> specified");
28734 else
28736 richloc.add_fixit_remove ();
28737 error_at (&richloc, "duplicate %qD", token->u.value);
28740 else
28742 static const char *const decl_spec_names[] = {
28743 "signed",
28744 "unsigned",
28745 "short",
28746 "long",
28747 "const",
28748 "volatile",
28749 "restrict",
28750 "inline",
28751 "virtual",
28752 "explicit",
28753 "friend",
28754 "typedef",
28755 "using",
28756 "constexpr",
28757 "__complex"
28759 gcc_rich_location richloc (location);
28760 richloc.add_fixit_remove ();
28761 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28766 /* Return true iff the declarator specifier DS is present in the
28767 sequence of declarator specifiers DECL_SPECS. */
28769 bool
28770 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28771 cp_decl_spec ds)
28773 gcc_assert (ds < ds_last);
28775 if (decl_specs == NULL)
28776 return false;
28778 return decl_specs->locations[ds] != 0;
28781 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28782 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28784 static bool
28785 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28787 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28790 /* Issue an error message indicating that TOKEN_DESC was expected.
28791 If KEYWORD is true, it indicated this function is called by
28792 cp_parser_require_keword and the required token can only be
28793 a indicated keyword.
28795 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28796 within any error as the location of an "opening" token matching
28797 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28798 RT_CLOSE_PAREN). */
28800 static void
28801 cp_parser_required_error (cp_parser *parser,
28802 required_token token_desc,
28803 bool keyword,
28804 location_t matching_location)
28806 if (cp_parser_simulate_error (parser))
28807 return;
28809 const char *gmsgid = NULL;
28810 switch (token_desc)
28812 case RT_NEW:
28813 gmsgid = G_("expected %<new%>");
28814 break;
28815 case RT_DELETE:
28816 gmsgid = G_("expected %<delete%>");
28817 break;
28818 case RT_RETURN:
28819 gmsgid = G_("expected %<return%>");
28820 break;
28821 case RT_WHILE:
28822 gmsgid = G_("expected %<while%>");
28823 break;
28824 case RT_EXTERN:
28825 gmsgid = G_("expected %<extern%>");
28826 break;
28827 case RT_STATIC_ASSERT:
28828 gmsgid = G_("expected %<static_assert%>");
28829 break;
28830 case RT_DECLTYPE:
28831 gmsgid = G_("expected %<decltype%>");
28832 break;
28833 case RT_OPERATOR:
28834 gmsgid = G_("expected %<operator%>");
28835 break;
28836 case RT_CLASS:
28837 gmsgid = G_("expected %<class%>");
28838 break;
28839 case RT_TEMPLATE:
28840 gmsgid = G_("expected %<template%>");
28841 break;
28842 case RT_NAMESPACE:
28843 gmsgid = G_("expected %<namespace%>");
28844 break;
28845 case RT_USING:
28846 gmsgid = G_("expected %<using%>");
28847 break;
28848 case RT_ASM:
28849 gmsgid = G_("expected %<asm%>");
28850 break;
28851 case RT_TRY:
28852 gmsgid = G_("expected %<try%>");
28853 break;
28854 case RT_CATCH:
28855 gmsgid = G_("expected %<catch%>");
28856 break;
28857 case RT_THROW:
28858 gmsgid = G_("expected %<throw%>");
28859 break;
28860 case RT_LABEL:
28861 gmsgid = G_("expected %<__label__%>");
28862 break;
28863 case RT_AT_TRY:
28864 gmsgid = G_("expected %<@try%>");
28865 break;
28866 case RT_AT_SYNCHRONIZED:
28867 gmsgid = G_("expected %<@synchronized%>");
28868 break;
28869 case RT_AT_THROW:
28870 gmsgid = G_("expected %<@throw%>");
28871 break;
28872 case RT_TRANSACTION_ATOMIC:
28873 gmsgid = G_("expected %<__transaction_atomic%>");
28874 break;
28875 case RT_TRANSACTION_RELAXED:
28876 gmsgid = G_("expected %<__transaction_relaxed%>");
28877 break;
28878 default:
28879 break;
28882 if (!gmsgid && !keyword)
28884 switch (token_desc)
28886 case RT_SEMICOLON:
28887 gmsgid = G_("expected %<;%>");
28888 break;
28889 case RT_OPEN_PAREN:
28890 gmsgid = G_("expected %<(%>");
28891 break;
28892 case RT_CLOSE_BRACE:
28893 gmsgid = G_("expected %<}%>");
28894 break;
28895 case RT_OPEN_BRACE:
28896 gmsgid = G_("expected %<{%>");
28897 break;
28898 case RT_CLOSE_SQUARE:
28899 gmsgid = G_("expected %<]%>");
28900 break;
28901 case RT_OPEN_SQUARE:
28902 gmsgid = G_("expected %<[%>");
28903 break;
28904 case RT_COMMA:
28905 gmsgid = G_("expected %<,%>");
28906 break;
28907 case RT_SCOPE:
28908 gmsgid = G_("expected %<::%>");
28909 break;
28910 case RT_LESS:
28911 gmsgid = G_("expected %<<%>");
28912 break;
28913 case RT_GREATER:
28914 gmsgid = G_("expected %<>%>");
28915 break;
28916 case RT_EQ:
28917 gmsgid = G_("expected %<=%>");
28918 break;
28919 case RT_ELLIPSIS:
28920 gmsgid = G_("expected %<...%>");
28921 break;
28922 case RT_MULT:
28923 gmsgid = G_("expected %<*%>");
28924 break;
28925 case RT_COMPL:
28926 gmsgid = G_("expected %<~%>");
28927 break;
28928 case RT_COLON:
28929 gmsgid = G_("expected %<:%>");
28930 break;
28931 case RT_COLON_SCOPE:
28932 gmsgid = G_("expected %<:%> or %<::%>");
28933 break;
28934 case RT_CLOSE_PAREN:
28935 gmsgid = G_("expected %<)%>");
28936 break;
28937 case RT_COMMA_CLOSE_PAREN:
28938 gmsgid = G_("expected %<,%> or %<)%>");
28939 break;
28940 case RT_PRAGMA_EOL:
28941 gmsgid = G_("expected end of line");
28942 break;
28943 case RT_NAME:
28944 gmsgid = G_("expected identifier");
28945 break;
28946 case RT_SELECT:
28947 gmsgid = G_("expected selection-statement");
28948 break;
28949 case RT_ITERATION:
28950 gmsgid = G_("expected iteration-statement");
28951 break;
28952 case RT_JUMP:
28953 gmsgid = G_("expected jump-statement");
28954 break;
28955 case RT_CLASS_KEY:
28956 gmsgid = G_("expected class-key");
28957 break;
28958 case RT_CLASS_TYPENAME_TEMPLATE:
28959 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28960 break;
28961 default:
28962 gcc_unreachable ();
28966 if (gmsgid)
28967 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28971 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28972 issue an error message indicating that TOKEN_DESC was expected.
28974 Returns the token consumed, if the token had the appropriate type.
28975 Otherwise, returns NULL.
28977 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28978 within any error as the location of an "opening" token matching
28979 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28980 RT_CLOSE_PAREN). */
28982 static cp_token *
28983 cp_parser_require (cp_parser* parser,
28984 enum cpp_ttype type,
28985 required_token token_desc,
28986 location_t matching_location)
28988 if (cp_lexer_next_token_is (parser->lexer, type))
28989 return cp_lexer_consume_token (parser->lexer);
28990 else
28992 /* Output the MESSAGE -- unless we're parsing tentatively. */
28993 if (!cp_parser_simulate_error (parser))
28994 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28995 matching_location);
28996 return NULL;
29000 /* An error message is produced if the next token is not '>'.
29001 All further tokens are skipped until the desired token is
29002 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29004 static void
29005 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
29007 /* Current level of '< ... >'. */
29008 unsigned level = 0;
29009 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29010 unsigned nesting_depth = 0;
29012 /* Are we ready, yet? If not, issue error message. */
29013 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
29014 return;
29016 /* Skip tokens until the desired token is found. */
29017 while (true)
29019 /* Peek at the next token. */
29020 switch (cp_lexer_peek_token (parser->lexer)->type)
29022 case CPP_LESS:
29023 if (!nesting_depth)
29024 ++level;
29025 break;
29027 case CPP_RSHIFT:
29028 if (cxx_dialect == cxx98)
29029 /* C++0x views the `>>' operator as two `>' tokens, but
29030 C++98 does not. */
29031 break;
29032 else if (!nesting_depth && level-- == 0)
29034 /* We've hit a `>>' where the first `>' closes the
29035 template argument list, and the second `>' is
29036 spurious. Just consume the `>>' and stop; we've
29037 already produced at least one error. */
29038 cp_lexer_consume_token (parser->lexer);
29039 return;
29041 /* Fall through for C++0x, so we handle the second `>' in
29042 the `>>'. */
29043 gcc_fallthrough ();
29045 case CPP_GREATER:
29046 if (!nesting_depth && level-- == 0)
29048 /* We've reached the token we want, consume it and stop. */
29049 cp_lexer_consume_token (parser->lexer);
29050 return;
29052 break;
29054 case CPP_OPEN_PAREN:
29055 case CPP_OPEN_SQUARE:
29056 ++nesting_depth;
29057 break;
29059 case CPP_CLOSE_PAREN:
29060 case CPP_CLOSE_SQUARE:
29061 if (nesting_depth-- == 0)
29062 return;
29063 break;
29065 case CPP_EOF:
29066 case CPP_PRAGMA_EOL:
29067 case CPP_SEMICOLON:
29068 case CPP_OPEN_BRACE:
29069 case CPP_CLOSE_BRACE:
29070 /* The '>' was probably forgotten, don't look further. */
29071 return;
29073 default:
29074 break;
29077 /* Consume this token. */
29078 cp_lexer_consume_token (parser->lexer);
29082 /* If the next token is the indicated keyword, consume it. Otherwise,
29083 issue an error message indicating that TOKEN_DESC was expected.
29085 Returns the token consumed, if the token had the appropriate type.
29086 Otherwise, returns NULL. */
29088 static cp_token *
29089 cp_parser_require_keyword (cp_parser* parser,
29090 enum rid keyword,
29091 required_token token_desc)
29093 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
29095 if (token && token->keyword != keyword)
29097 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
29098 UNKNOWN_LOCATION);
29099 return NULL;
29102 return token;
29105 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29106 function-definition. */
29108 static bool
29109 cp_parser_token_starts_function_definition_p (cp_token* token)
29111 return (/* An ordinary function-body begins with an `{'. */
29112 token->type == CPP_OPEN_BRACE
29113 /* A ctor-initializer begins with a `:'. */
29114 || token->type == CPP_COLON
29115 /* A function-try-block begins with `try'. */
29116 || token->keyword == RID_TRY
29117 /* A function-transaction-block begins with `__transaction_atomic'
29118 or `__transaction_relaxed'. */
29119 || token->keyword == RID_TRANSACTION_ATOMIC
29120 || token->keyword == RID_TRANSACTION_RELAXED
29121 /* The named return value extension begins with `return'. */
29122 || token->keyword == RID_RETURN);
29125 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29126 definition. */
29128 static bool
29129 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
29131 cp_token *token;
29133 token = cp_lexer_peek_token (parser->lexer);
29134 return (token->type == CPP_OPEN_BRACE
29135 || (token->type == CPP_COLON
29136 && !parser->colon_doesnt_start_class_def_p));
29139 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29140 C++0x) ending a template-argument. */
29142 static bool
29143 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
29145 cp_token *token;
29147 token = cp_lexer_peek_token (parser->lexer);
29148 return (token->type == CPP_COMMA
29149 || token->type == CPP_GREATER
29150 || token->type == CPP_ELLIPSIS
29151 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
29154 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29155 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29157 static bool
29158 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
29159 size_t n)
29161 cp_token *token;
29163 token = cp_lexer_peek_nth_token (parser->lexer, n);
29164 if (token->type == CPP_LESS)
29165 return true;
29166 /* Check for the sequence `<::' in the original code. It would be lexed as
29167 `[:', where `[' is a digraph, and there is no whitespace before
29168 `:'. */
29169 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
29171 cp_token *token2;
29172 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
29173 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
29174 return true;
29176 return false;
29179 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29180 or none_type otherwise. */
29182 static enum tag_types
29183 cp_parser_token_is_class_key (cp_token* token)
29185 switch (token->keyword)
29187 case RID_CLASS:
29188 return class_type;
29189 case RID_STRUCT:
29190 return record_type;
29191 case RID_UNION:
29192 return union_type;
29194 default:
29195 return none_type;
29199 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29200 or none_type otherwise or if the token is null. */
29202 static enum tag_types
29203 cp_parser_token_is_type_parameter_key (cp_token* token)
29205 if (!token)
29206 return none_type;
29208 switch (token->keyword)
29210 case RID_CLASS:
29211 return class_type;
29212 case RID_TYPENAME:
29213 return typename_type;
29215 default:
29216 return none_type;
29220 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29222 static void
29223 cp_parser_check_class_key (enum tag_types class_key, tree type)
29225 if (type == error_mark_node)
29226 return;
29227 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29229 if (permerror (input_location, "%qs tag used in naming %q#T",
29230 class_key == union_type ? "union"
29231 : class_key == record_type ? "struct" : "class",
29232 type))
29233 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29234 "%q#T was previously declared here", type);
29238 /* Issue an error message if DECL is redeclared with different
29239 access than its original declaration [class.access.spec/3].
29240 This applies to nested classes, nested class templates and
29241 enumerations [class.mem/1]. */
29243 static void
29244 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29246 if (!decl
29247 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29248 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29249 return;
29251 if ((TREE_PRIVATE (decl)
29252 != (current_access_specifier == access_private_node))
29253 || (TREE_PROTECTED (decl)
29254 != (current_access_specifier == access_protected_node)))
29255 error_at (location, "%qD redeclared with different access", decl);
29258 /* Look for the `template' keyword, as a syntactic disambiguator.
29259 Return TRUE iff it is present, in which case it will be
29260 consumed. */
29262 static bool
29263 cp_parser_optional_template_keyword (cp_parser *parser)
29265 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29267 /* In C++98 the `template' keyword can only be used within templates;
29268 outside templates the parser can always figure out what is a
29269 template and what is not. In C++11, per the resolution of DR 468,
29270 `template' is allowed in cases where it is not strictly necessary. */
29271 if (!processing_template_decl
29272 && pedantic && cxx_dialect == cxx98)
29274 cp_token *token = cp_lexer_peek_token (parser->lexer);
29275 pedwarn (token->location, OPT_Wpedantic,
29276 "in C++98 %<template%> (as a disambiguator) is only "
29277 "allowed within templates");
29278 /* If this part of the token stream is rescanned, the same
29279 error message would be generated. So, we purge the token
29280 from the stream. */
29281 cp_lexer_purge_token (parser->lexer);
29282 return false;
29284 else
29286 /* Consume the `template' keyword. */
29287 cp_lexer_consume_token (parser->lexer);
29288 return true;
29291 return false;
29294 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29295 set PARSER->SCOPE, and perform other related actions. */
29297 static void
29298 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29300 struct tree_check *check_value;
29302 /* Get the stored value. */
29303 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29304 /* Set the scope from the stored value. */
29305 parser->scope = saved_checks_value (check_value);
29306 parser->qualifying_scope = check_value->qualifying_scope;
29307 parser->object_scope = NULL_TREE;
29310 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29311 encounter the end of a block before what we were looking for. */
29313 static bool
29314 cp_parser_cache_group (cp_parser *parser,
29315 enum cpp_ttype end,
29316 unsigned depth)
29318 while (true)
29320 cp_token *token = cp_lexer_peek_token (parser->lexer);
29322 /* Abort a parenthesized expression if we encounter a semicolon. */
29323 if ((end == CPP_CLOSE_PAREN || depth == 0)
29324 && token->type == CPP_SEMICOLON)
29325 return true;
29326 /* If we've reached the end of the file, stop. */
29327 if (token->type == CPP_EOF
29328 || (end != CPP_PRAGMA_EOL
29329 && token->type == CPP_PRAGMA_EOL))
29330 return true;
29331 if (token->type == CPP_CLOSE_BRACE && depth == 0)
29332 /* We've hit the end of an enclosing block, so there's been some
29333 kind of syntax error. */
29334 return true;
29336 /* Consume the token. */
29337 cp_lexer_consume_token (parser->lexer);
29338 /* See if it starts a new group. */
29339 if (token->type == CPP_OPEN_BRACE)
29341 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29342 /* In theory this should probably check end == '}', but
29343 cp_parser_save_member_function_body needs it to exit
29344 after either '}' or ')' when called with ')'. */
29345 if (depth == 0)
29346 return false;
29348 else if (token->type == CPP_OPEN_PAREN)
29350 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29351 if (depth == 0 && end == CPP_CLOSE_PAREN)
29352 return false;
29354 else if (token->type == CPP_PRAGMA)
29355 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29356 else if (token->type == end)
29357 return false;
29361 /* Like above, for caching a default argument or NSDMI. Both of these are
29362 terminated by a non-nested comma, but it can be unclear whether or not a
29363 comma is nested in a template argument list unless we do more parsing.
29364 In order to handle this ambiguity, when we encounter a ',' after a '<'
29365 we try to parse what follows as a parameter-declaration-list (in the
29366 case of a default argument) or a member-declarator (in the case of an
29367 NSDMI). If that succeeds, then we stop caching. */
29369 static tree
29370 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29372 unsigned depth = 0;
29373 int maybe_template_id = 0;
29374 cp_token *first_token;
29375 cp_token *token;
29376 tree default_argument;
29378 /* Add tokens until we have processed the entire default
29379 argument. We add the range [first_token, token). */
29380 first_token = cp_lexer_peek_token (parser->lexer);
29381 if (first_token->type == CPP_OPEN_BRACE)
29383 /* For list-initialization, this is straightforward. */
29384 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29385 token = cp_lexer_peek_token (parser->lexer);
29387 else while (true)
29389 bool done = false;
29391 /* Peek at the next token. */
29392 token = cp_lexer_peek_token (parser->lexer);
29393 /* What we do depends on what token we have. */
29394 switch (token->type)
29396 /* In valid code, a default argument must be
29397 immediately followed by a `,' `)', or `...'. */
29398 case CPP_COMMA:
29399 if (depth == 0 && maybe_template_id)
29401 /* If we've seen a '<', we might be in a
29402 template-argument-list. Until Core issue 325 is
29403 resolved, we don't know how this situation ought
29404 to be handled, so try to DTRT. We check whether
29405 what comes after the comma is a valid parameter
29406 declaration list. If it is, then the comma ends
29407 the default argument; otherwise the default
29408 argument continues. */
29409 bool error = false;
29410 cp_token *peek;
29412 /* Set ITALP so cp_parser_parameter_declaration_list
29413 doesn't decide to commit to this parse. */
29414 bool saved_italp = parser->in_template_argument_list_p;
29415 parser->in_template_argument_list_p = true;
29417 cp_parser_parse_tentatively (parser);
29419 if (nsdmi)
29421 /* Parse declarators until we reach a non-comma or
29422 somthing that cannot be an initializer.
29423 Just checking whether we're looking at a single
29424 declarator is insufficient. Consider:
29425 int var = tuple<T,U>::x;
29426 The template parameter 'U' looks exactly like a
29427 declarator. */
29430 int ctor_dtor_or_conv_p;
29431 cp_lexer_consume_token (parser->lexer);
29432 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29433 &ctor_dtor_or_conv_p,
29434 /*parenthesized_p=*/NULL,
29435 /*member_p=*/true,
29436 /*friend_p=*/false);
29437 peek = cp_lexer_peek_token (parser->lexer);
29438 if (cp_parser_error_occurred (parser))
29439 break;
29441 while (peek->type == CPP_COMMA);
29442 /* If we met an '=' or ';' then the original comma
29443 was the end of the NSDMI. Otherwise assume
29444 we're still in the NSDMI. */
29445 error = (peek->type != CPP_EQ
29446 && peek->type != CPP_SEMICOLON);
29448 else
29450 cp_lexer_consume_token (parser->lexer);
29451 begin_scope (sk_function_parms, NULL_TREE);
29452 if (cp_parser_parameter_declaration_list (parser)
29453 == error_mark_node)
29454 error = true;
29455 pop_bindings_and_leave_scope ();
29457 if (!cp_parser_error_occurred (parser) && !error)
29458 done = true;
29459 cp_parser_abort_tentative_parse (parser);
29461 parser->in_template_argument_list_p = saved_italp;
29462 break;
29464 /* FALLTHRU */
29465 case CPP_CLOSE_PAREN:
29466 case CPP_ELLIPSIS:
29467 /* If we run into a non-nested `;', `}', or `]',
29468 then the code is invalid -- but the default
29469 argument is certainly over. */
29470 case CPP_SEMICOLON:
29471 case CPP_CLOSE_BRACE:
29472 case CPP_CLOSE_SQUARE:
29473 if (depth == 0
29474 /* Handle correctly int n = sizeof ... ( p ); */
29475 && token->type != CPP_ELLIPSIS)
29476 done = true;
29477 /* Update DEPTH, if necessary. */
29478 else if (token->type == CPP_CLOSE_PAREN
29479 || token->type == CPP_CLOSE_BRACE
29480 || token->type == CPP_CLOSE_SQUARE)
29481 --depth;
29482 break;
29484 case CPP_OPEN_PAREN:
29485 case CPP_OPEN_SQUARE:
29486 case CPP_OPEN_BRACE:
29487 ++depth;
29488 break;
29490 case CPP_LESS:
29491 if (depth == 0)
29492 /* This might be the comparison operator, or it might
29493 start a template argument list. */
29494 ++maybe_template_id;
29495 break;
29497 case CPP_RSHIFT:
29498 if (cxx_dialect == cxx98)
29499 break;
29500 /* Fall through for C++0x, which treats the `>>'
29501 operator like two `>' tokens in certain
29502 cases. */
29503 gcc_fallthrough ();
29505 case CPP_GREATER:
29506 if (depth == 0)
29508 /* This might be an operator, or it might close a
29509 template argument list. But if a previous '<'
29510 started a template argument list, this will have
29511 closed it, so we can't be in one anymore. */
29512 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29513 if (maybe_template_id < 0)
29514 maybe_template_id = 0;
29516 break;
29518 /* If we run out of tokens, issue an error message. */
29519 case CPP_EOF:
29520 case CPP_PRAGMA_EOL:
29521 error_at (token->location, "file ends in default argument");
29522 return error_mark_node;
29524 case CPP_NAME:
29525 case CPP_SCOPE:
29526 /* In these cases, we should look for template-ids.
29527 For example, if the default argument is
29528 `X<int, double>()', we need to do name lookup to
29529 figure out whether or not `X' is a template; if
29530 so, the `,' does not end the default argument.
29532 That is not yet done. */
29533 break;
29535 default:
29536 break;
29539 /* If we've reached the end, stop. */
29540 if (done)
29541 break;
29543 /* Add the token to the token block. */
29544 token = cp_lexer_consume_token (parser->lexer);
29547 /* Create a DEFAULT_ARG to represent the unparsed default
29548 argument. */
29549 default_argument = make_node (DEFAULT_ARG);
29550 DEFARG_TOKENS (default_argument)
29551 = cp_token_cache_new (first_token, token);
29552 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29554 return default_argument;
29557 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29559 location_t
29560 defarg_location (tree default_argument)
29562 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29563 location_t start = tokens->first->location;
29564 location_t end = tokens->last->location;
29565 return make_location (start, start, end);
29568 /* Begin parsing tentatively. We always save tokens while parsing
29569 tentatively so that if the tentative parsing fails we can restore the
29570 tokens. */
29572 static void
29573 cp_parser_parse_tentatively (cp_parser* parser)
29575 /* Enter a new parsing context. */
29576 parser->context = cp_parser_context_new (parser->context);
29577 /* Begin saving tokens. */
29578 cp_lexer_save_tokens (parser->lexer);
29579 /* In order to avoid repetitive access control error messages,
29580 access checks are queued up until we are no longer parsing
29581 tentatively. */
29582 push_deferring_access_checks (dk_deferred);
29585 /* Commit to the currently active tentative parse. */
29587 static void
29588 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29590 cp_parser_context *context;
29591 cp_lexer *lexer;
29593 /* Mark all of the levels as committed. */
29594 lexer = parser->lexer;
29595 for (context = parser->context; context->next; context = context->next)
29597 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29598 break;
29599 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29600 while (!cp_lexer_saving_tokens (lexer))
29601 lexer = lexer->next;
29602 cp_lexer_commit_tokens (lexer);
29606 /* Commit to the topmost currently active tentative parse.
29608 Note that this function shouldn't be called when there are
29609 irreversible side-effects while in a tentative state. For
29610 example, we shouldn't create a permanent entry in the symbol
29611 table, or issue an error message that might not apply if the
29612 tentative parse is aborted. */
29614 static void
29615 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29617 cp_parser_context *context = parser->context;
29618 cp_lexer *lexer = parser->lexer;
29620 if (context)
29622 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29623 return;
29624 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29626 while (!cp_lexer_saving_tokens (lexer))
29627 lexer = lexer->next;
29628 cp_lexer_commit_tokens (lexer);
29632 /* Abort the currently active tentative parse. All consumed tokens
29633 will be rolled back, and no diagnostics will be issued. */
29635 static void
29636 cp_parser_abort_tentative_parse (cp_parser* parser)
29638 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29639 || errorcount > 0);
29640 cp_parser_simulate_error (parser);
29641 /* Now, pretend that we want to see if the construct was
29642 successfully parsed. */
29643 cp_parser_parse_definitely (parser);
29646 /* Stop parsing tentatively. If a parse error has occurred, restore the
29647 token stream. Otherwise, commit to the tokens we have consumed.
29648 Returns true if no error occurred; false otherwise. */
29650 static bool
29651 cp_parser_parse_definitely (cp_parser* parser)
29653 bool error_occurred;
29654 cp_parser_context *context;
29656 /* Remember whether or not an error occurred, since we are about to
29657 destroy that information. */
29658 error_occurred = cp_parser_error_occurred (parser);
29659 /* Remove the topmost context from the stack. */
29660 context = parser->context;
29661 parser->context = context->next;
29662 /* If no parse errors occurred, commit to the tentative parse. */
29663 if (!error_occurred)
29665 /* Commit to the tokens read tentatively, unless that was
29666 already done. */
29667 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29668 cp_lexer_commit_tokens (parser->lexer);
29670 pop_to_parent_deferring_access_checks ();
29672 /* Otherwise, if errors occurred, roll back our state so that things
29673 are just as they were before we began the tentative parse. */
29674 else
29676 cp_lexer_rollback_tokens (parser->lexer);
29677 pop_deferring_access_checks ();
29679 /* Add the context to the front of the free list. */
29680 context->next = cp_parser_context_free_list;
29681 cp_parser_context_free_list = context;
29683 return !error_occurred;
29686 /* Returns true if we are parsing tentatively and are not committed to
29687 this tentative parse. */
29689 static bool
29690 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29692 return (cp_parser_parsing_tentatively (parser)
29693 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29696 /* Returns nonzero iff an error has occurred during the most recent
29697 tentative parse. */
29699 static bool
29700 cp_parser_error_occurred (cp_parser* parser)
29702 return (cp_parser_parsing_tentatively (parser)
29703 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29706 /* Returns nonzero if GNU extensions are allowed. */
29708 static bool
29709 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29711 return parser->allow_gnu_extensions_p;
29714 /* Objective-C++ Productions */
29717 /* Parse an Objective-C expression, which feeds into a primary-expression
29718 above.
29720 objc-expression:
29721 objc-message-expression
29722 objc-string-literal
29723 objc-encode-expression
29724 objc-protocol-expression
29725 objc-selector-expression
29727 Returns a tree representation of the expression. */
29729 static cp_expr
29730 cp_parser_objc_expression (cp_parser* parser)
29732 /* Try to figure out what kind of declaration is present. */
29733 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29735 switch (kwd->type)
29737 case CPP_OPEN_SQUARE:
29738 return cp_parser_objc_message_expression (parser);
29740 case CPP_OBJC_STRING:
29741 kwd = cp_lexer_consume_token (parser->lexer);
29742 return objc_build_string_object (kwd->u.value);
29744 case CPP_KEYWORD:
29745 switch (kwd->keyword)
29747 case RID_AT_ENCODE:
29748 return cp_parser_objc_encode_expression (parser);
29750 case RID_AT_PROTOCOL:
29751 return cp_parser_objc_protocol_expression (parser);
29753 case RID_AT_SELECTOR:
29754 return cp_parser_objc_selector_expression (parser);
29756 default:
29757 break;
29759 /* FALLTHRU */
29760 default:
29761 error_at (kwd->location,
29762 "misplaced %<@%D%> Objective-C++ construct",
29763 kwd->u.value);
29764 cp_parser_skip_to_end_of_block_or_statement (parser);
29767 return error_mark_node;
29770 /* Parse an Objective-C message expression.
29772 objc-message-expression:
29773 [ objc-message-receiver objc-message-args ]
29775 Returns a representation of an Objective-C message. */
29777 static tree
29778 cp_parser_objc_message_expression (cp_parser* parser)
29780 tree receiver, messageargs;
29782 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29783 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29784 receiver = cp_parser_objc_message_receiver (parser);
29785 messageargs = cp_parser_objc_message_args (parser);
29786 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29787 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29789 tree result = objc_build_message_expr (receiver, messageargs);
29791 /* Construct a location e.g.
29792 [self func1:5]
29793 ^~~~~~~~~~~~~~
29794 ranging from the '[' to the ']', with the caret at the start. */
29795 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29796 protected_set_expr_location (result, combined_loc);
29798 return result;
29801 /* Parse an objc-message-receiver.
29803 objc-message-receiver:
29804 expression
29805 simple-type-specifier
29807 Returns a representation of the type or expression. */
29809 static tree
29810 cp_parser_objc_message_receiver (cp_parser* parser)
29812 tree rcv;
29814 /* An Objective-C message receiver may be either (1) a type
29815 or (2) an expression. */
29816 cp_parser_parse_tentatively (parser);
29817 rcv = cp_parser_expression (parser);
29819 /* If that worked out, fine. */
29820 if (cp_parser_parse_definitely (parser))
29821 return rcv;
29823 cp_parser_parse_tentatively (parser);
29824 rcv = cp_parser_simple_type_specifier (parser,
29825 /*decl_specs=*/NULL,
29826 CP_PARSER_FLAGS_NONE);
29828 if (cp_parser_parse_definitely (parser))
29829 return objc_get_class_reference (rcv);
29831 cp_parser_error (parser, "objective-c++ message receiver expected");
29832 return error_mark_node;
29835 /* Parse the arguments and selectors comprising an Objective-C message.
29837 objc-message-args:
29838 objc-selector
29839 objc-selector-args
29840 objc-selector-args , objc-comma-args
29842 objc-selector-args:
29843 objc-selector [opt] : assignment-expression
29844 objc-selector-args objc-selector [opt] : assignment-expression
29846 objc-comma-args:
29847 assignment-expression
29848 objc-comma-args , assignment-expression
29850 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29851 selector arguments and TREE_VALUE containing a list of comma
29852 arguments. */
29854 static tree
29855 cp_parser_objc_message_args (cp_parser* parser)
29857 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29858 bool maybe_unary_selector_p = true;
29859 cp_token *token = cp_lexer_peek_token (parser->lexer);
29861 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29863 tree selector = NULL_TREE, arg;
29865 if (token->type != CPP_COLON)
29866 selector = cp_parser_objc_selector (parser);
29868 /* Detect if we have a unary selector. */
29869 if (maybe_unary_selector_p
29870 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29871 return build_tree_list (selector, NULL_TREE);
29873 maybe_unary_selector_p = false;
29874 cp_parser_require (parser, CPP_COLON, RT_COLON);
29875 arg = cp_parser_assignment_expression (parser);
29877 sel_args
29878 = chainon (sel_args,
29879 build_tree_list (selector, arg));
29881 token = cp_lexer_peek_token (parser->lexer);
29884 /* Handle non-selector arguments, if any. */
29885 while (token->type == CPP_COMMA)
29887 tree arg;
29889 cp_lexer_consume_token (parser->lexer);
29890 arg = cp_parser_assignment_expression (parser);
29892 addl_args
29893 = chainon (addl_args,
29894 build_tree_list (NULL_TREE, arg));
29896 token = cp_lexer_peek_token (parser->lexer);
29899 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29901 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29902 return build_tree_list (error_mark_node, error_mark_node);
29905 return build_tree_list (sel_args, addl_args);
29908 /* Parse an Objective-C encode expression.
29910 objc-encode-expression:
29911 @encode objc-typename
29913 Returns an encoded representation of the type argument. */
29915 static cp_expr
29916 cp_parser_objc_encode_expression (cp_parser* parser)
29918 tree type;
29919 cp_token *token;
29920 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29922 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29923 matching_parens parens;
29924 parens.require_open (parser);
29925 token = cp_lexer_peek_token (parser->lexer);
29926 type = complete_type (cp_parser_type_id (parser));
29927 parens.require_close (parser);
29929 if (!type)
29931 error_at (token->location,
29932 "%<@encode%> must specify a type as an argument");
29933 return error_mark_node;
29936 /* This happens if we find @encode(T) (where T is a template
29937 typename or something dependent on a template typename) when
29938 parsing a template. In that case, we can't compile it
29939 immediately, but we rather create an AT_ENCODE_EXPR which will
29940 need to be instantiated when the template is used.
29942 if (dependent_type_p (type))
29944 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29945 TREE_READONLY (value) = 1;
29946 return value;
29950 /* Build a location of the form:
29951 @encode(int)
29952 ^~~~~~~~~~~~
29953 with caret==start at the @ token, finishing at the close paren. */
29954 location_t combined_loc
29955 = make_location (start_loc, start_loc,
29956 cp_lexer_previous_token (parser->lexer)->location);
29958 return cp_expr (objc_build_encode_expr (type), combined_loc);
29961 /* Parse an Objective-C @defs expression. */
29963 static tree
29964 cp_parser_objc_defs_expression (cp_parser *parser)
29966 tree name;
29968 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29969 matching_parens parens;
29970 parens.require_open (parser);
29971 name = cp_parser_identifier (parser);
29972 parens.require_close (parser);
29974 return objc_get_class_ivars (name);
29977 /* Parse an Objective-C protocol expression.
29979 objc-protocol-expression:
29980 @protocol ( identifier )
29982 Returns a representation of the protocol expression. */
29984 static tree
29985 cp_parser_objc_protocol_expression (cp_parser* parser)
29987 tree proto;
29988 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29990 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29991 matching_parens parens;
29992 parens.require_open (parser);
29993 proto = cp_parser_identifier (parser);
29994 parens.require_close (parser);
29996 /* Build a location of the form:
29997 @protocol(prot)
29998 ^~~~~~~~~~~~~~~
29999 with caret==start at the @ token, finishing at the close paren. */
30000 location_t combined_loc
30001 = make_location (start_loc, start_loc,
30002 cp_lexer_previous_token (parser->lexer)->location);
30003 tree result = objc_build_protocol_expr (proto);
30004 protected_set_expr_location (result, combined_loc);
30005 return result;
30008 /* Parse an Objective-C selector expression.
30010 objc-selector-expression:
30011 @selector ( objc-method-signature )
30013 objc-method-signature:
30014 objc-selector
30015 objc-selector-seq
30017 objc-selector-seq:
30018 objc-selector :
30019 objc-selector-seq objc-selector :
30021 Returns a representation of the method selector. */
30023 static tree
30024 cp_parser_objc_selector_expression (cp_parser* parser)
30026 tree sel_seq = NULL_TREE;
30027 bool maybe_unary_selector_p = true;
30028 cp_token *token;
30029 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30031 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
30032 matching_parens parens;
30033 parens.require_open (parser);
30034 token = cp_lexer_peek_token (parser->lexer);
30036 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
30037 || token->type == CPP_SCOPE)
30039 tree selector = NULL_TREE;
30041 if (token->type != CPP_COLON
30042 || token->type == CPP_SCOPE)
30043 selector = cp_parser_objc_selector (parser);
30045 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
30046 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
30048 /* Detect if we have a unary selector. */
30049 if (maybe_unary_selector_p)
30051 sel_seq = selector;
30052 goto finish_selector;
30054 else
30056 cp_parser_error (parser, "expected %<:%>");
30059 maybe_unary_selector_p = false;
30060 token = cp_lexer_consume_token (parser->lexer);
30062 if (token->type == CPP_SCOPE)
30064 sel_seq
30065 = chainon (sel_seq,
30066 build_tree_list (selector, NULL_TREE));
30067 sel_seq
30068 = chainon (sel_seq,
30069 build_tree_list (NULL_TREE, NULL_TREE));
30071 else
30072 sel_seq
30073 = chainon (sel_seq,
30074 build_tree_list (selector, NULL_TREE));
30076 token = cp_lexer_peek_token (parser->lexer);
30079 finish_selector:
30080 parens.require_close (parser);
30083 /* Build a location of the form:
30084 @selector(func)
30085 ^~~~~~~~~~~~~~~
30086 with caret==start at the @ token, finishing at the close paren. */
30087 location_t combined_loc
30088 = make_location (loc, loc,
30089 cp_lexer_previous_token (parser->lexer)->location);
30090 tree result = objc_build_selector_expr (combined_loc, sel_seq);
30091 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30092 protected_set_expr_location (result, combined_loc);
30093 return result;
30096 /* Parse a list of identifiers.
30098 objc-identifier-list:
30099 identifier
30100 objc-identifier-list , identifier
30102 Returns a TREE_LIST of identifier nodes. */
30104 static tree
30105 cp_parser_objc_identifier_list (cp_parser* parser)
30107 tree identifier;
30108 tree list;
30109 cp_token *sep;
30111 identifier = cp_parser_identifier (parser);
30112 if (identifier == error_mark_node)
30113 return error_mark_node;
30115 list = build_tree_list (NULL_TREE, identifier);
30116 sep = cp_lexer_peek_token (parser->lexer);
30118 while (sep->type == CPP_COMMA)
30120 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30121 identifier = cp_parser_identifier (parser);
30122 if (identifier == error_mark_node)
30123 return list;
30125 list = chainon (list, build_tree_list (NULL_TREE,
30126 identifier));
30127 sep = cp_lexer_peek_token (parser->lexer);
30130 return list;
30133 /* Parse an Objective-C alias declaration.
30135 objc-alias-declaration:
30136 @compatibility_alias identifier identifier ;
30138 This function registers the alias mapping with the Objective-C front end.
30139 It returns nothing. */
30141 static void
30142 cp_parser_objc_alias_declaration (cp_parser* parser)
30144 tree alias, orig;
30146 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
30147 alias = cp_parser_identifier (parser);
30148 orig = cp_parser_identifier (parser);
30149 objc_declare_alias (alias, orig);
30150 cp_parser_consume_semicolon_at_end_of_statement (parser);
30153 /* Parse an Objective-C class forward-declaration.
30155 objc-class-declaration:
30156 @class objc-identifier-list ;
30158 The function registers the forward declarations with the Objective-C
30159 front end. It returns nothing. */
30161 static void
30162 cp_parser_objc_class_declaration (cp_parser* parser)
30164 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
30165 while (true)
30167 tree id;
30169 id = cp_parser_identifier (parser);
30170 if (id == error_mark_node)
30171 break;
30173 objc_declare_class (id);
30175 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30176 cp_lexer_consume_token (parser->lexer);
30177 else
30178 break;
30180 cp_parser_consume_semicolon_at_end_of_statement (parser);
30183 /* Parse a list of Objective-C protocol references.
30185 objc-protocol-refs-opt:
30186 objc-protocol-refs [opt]
30188 objc-protocol-refs:
30189 < objc-identifier-list >
30191 Returns a TREE_LIST of identifiers, if any. */
30193 static tree
30194 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30196 tree protorefs = NULL_TREE;
30198 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30200 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30201 protorefs = cp_parser_objc_identifier_list (parser);
30202 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30205 return protorefs;
30208 /* Parse a Objective-C visibility specification. */
30210 static void
30211 cp_parser_objc_visibility_spec (cp_parser* parser)
30213 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30215 switch (vis->keyword)
30217 case RID_AT_PRIVATE:
30218 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30219 break;
30220 case RID_AT_PROTECTED:
30221 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30222 break;
30223 case RID_AT_PUBLIC:
30224 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30225 break;
30226 case RID_AT_PACKAGE:
30227 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30228 break;
30229 default:
30230 return;
30233 /* Eat '@private'/'@protected'/'@public'. */
30234 cp_lexer_consume_token (parser->lexer);
30237 /* Parse an Objective-C method type. Return 'true' if it is a class
30238 (+) method, and 'false' if it is an instance (-) method. */
30240 static inline bool
30241 cp_parser_objc_method_type (cp_parser* parser)
30243 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30244 return true;
30245 else
30246 return false;
30249 /* Parse an Objective-C protocol qualifier. */
30251 static tree
30252 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30254 tree quals = NULL_TREE, node;
30255 cp_token *token = cp_lexer_peek_token (parser->lexer);
30257 node = token->u.value;
30259 while (node && identifier_p (node)
30260 && (node == ridpointers [(int) RID_IN]
30261 || node == ridpointers [(int) RID_OUT]
30262 || node == ridpointers [(int) RID_INOUT]
30263 || node == ridpointers [(int) RID_BYCOPY]
30264 || node == ridpointers [(int) RID_BYREF]
30265 || node == ridpointers [(int) RID_ONEWAY]))
30267 quals = tree_cons (NULL_TREE, node, quals);
30268 cp_lexer_consume_token (parser->lexer);
30269 token = cp_lexer_peek_token (parser->lexer);
30270 node = token->u.value;
30273 return quals;
30276 /* Parse an Objective-C typename. */
30278 static tree
30279 cp_parser_objc_typename (cp_parser* parser)
30281 tree type_name = NULL_TREE;
30283 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30285 tree proto_quals, cp_type = NULL_TREE;
30287 matching_parens parens;
30288 parens.consume_open (parser); /* Eat '('. */
30289 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30291 /* An ObjC type name may consist of just protocol qualifiers, in which
30292 case the type shall default to 'id'. */
30293 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30295 cp_type = cp_parser_type_id (parser);
30297 /* If the type could not be parsed, an error has already
30298 been produced. For error recovery, behave as if it had
30299 not been specified, which will use the default type
30300 'id'. */
30301 if (cp_type == error_mark_node)
30303 cp_type = NULL_TREE;
30304 /* We need to skip to the closing parenthesis as
30305 cp_parser_type_id() does not seem to do it for
30306 us. */
30307 cp_parser_skip_to_closing_parenthesis (parser,
30308 /*recovering=*/true,
30309 /*or_comma=*/false,
30310 /*consume_paren=*/false);
30314 parens.require_close (parser);
30315 type_name = build_tree_list (proto_quals, cp_type);
30318 return type_name;
30321 /* Check to see if TYPE refers to an Objective-C selector name. */
30323 static bool
30324 cp_parser_objc_selector_p (enum cpp_ttype type)
30326 return (type == CPP_NAME || type == CPP_KEYWORD
30327 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30328 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30329 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30330 || type == CPP_XOR || type == CPP_XOR_EQ);
30333 /* Parse an Objective-C selector. */
30335 static tree
30336 cp_parser_objc_selector (cp_parser* parser)
30338 cp_token *token = cp_lexer_consume_token (parser->lexer);
30340 if (!cp_parser_objc_selector_p (token->type))
30342 error_at (token->location, "invalid Objective-C++ selector name");
30343 return error_mark_node;
30346 /* C++ operator names are allowed to appear in ObjC selectors. */
30347 switch (token->type)
30349 case CPP_AND_AND: return get_identifier ("and");
30350 case CPP_AND_EQ: return get_identifier ("and_eq");
30351 case CPP_AND: return get_identifier ("bitand");
30352 case CPP_OR: return get_identifier ("bitor");
30353 case CPP_COMPL: return get_identifier ("compl");
30354 case CPP_NOT: return get_identifier ("not");
30355 case CPP_NOT_EQ: return get_identifier ("not_eq");
30356 case CPP_OR_OR: return get_identifier ("or");
30357 case CPP_OR_EQ: return get_identifier ("or_eq");
30358 case CPP_XOR: return get_identifier ("xor");
30359 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30360 default: return token->u.value;
30364 /* Parse an Objective-C params list. */
30366 static tree
30367 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30369 tree params = NULL_TREE;
30370 bool maybe_unary_selector_p = true;
30371 cp_token *token = cp_lexer_peek_token (parser->lexer);
30373 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30375 tree selector = NULL_TREE, type_name, identifier;
30376 tree parm_attr = NULL_TREE;
30378 if (token->keyword == RID_ATTRIBUTE)
30379 break;
30381 if (token->type != CPP_COLON)
30382 selector = cp_parser_objc_selector (parser);
30384 /* Detect if we have a unary selector. */
30385 if (maybe_unary_selector_p
30386 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30388 params = selector; /* Might be followed by attributes. */
30389 break;
30392 maybe_unary_selector_p = false;
30393 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30395 /* Something went quite wrong. There should be a colon
30396 here, but there is not. Stop parsing parameters. */
30397 break;
30399 type_name = cp_parser_objc_typename (parser);
30400 /* New ObjC allows attributes on parameters too. */
30401 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30402 parm_attr = cp_parser_attributes_opt (parser);
30403 identifier = cp_parser_identifier (parser);
30405 params
30406 = chainon (params,
30407 objc_build_keyword_decl (selector,
30408 type_name,
30409 identifier,
30410 parm_attr));
30412 token = cp_lexer_peek_token (parser->lexer);
30415 if (params == NULL_TREE)
30417 cp_parser_error (parser, "objective-c++ method declaration is expected");
30418 return error_mark_node;
30421 /* We allow tail attributes for the method. */
30422 if (token->keyword == RID_ATTRIBUTE)
30424 *attributes = cp_parser_attributes_opt (parser);
30425 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30426 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30427 return params;
30428 cp_parser_error (parser,
30429 "method attributes must be specified at the end");
30430 return error_mark_node;
30433 if (params == NULL_TREE)
30435 cp_parser_error (parser, "objective-c++ method declaration is expected");
30436 return error_mark_node;
30438 return params;
30441 /* Parse the non-keyword Objective-C params. */
30443 static tree
30444 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30445 tree* attributes)
30447 tree params = make_node (TREE_LIST);
30448 cp_token *token = cp_lexer_peek_token (parser->lexer);
30449 *ellipsisp = false; /* Initially, assume no ellipsis. */
30451 while (token->type == CPP_COMMA)
30453 cp_parameter_declarator *parmdecl;
30454 tree parm;
30456 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30457 token = cp_lexer_peek_token (parser->lexer);
30459 if (token->type == CPP_ELLIPSIS)
30461 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30462 *ellipsisp = true;
30463 token = cp_lexer_peek_token (parser->lexer);
30464 break;
30467 /* TODO: parse attributes for tail parameters. */
30468 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30469 parm = grokdeclarator (parmdecl->declarator,
30470 &parmdecl->decl_specifiers,
30471 PARM, /*initialized=*/0,
30472 /*attrlist=*/NULL);
30474 chainon (params, build_tree_list (NULL_TREE, parm));
30475 token = cp_lexer_peek_token (parser->lexer);
30478 /* We allow tail attributes for the method. */
30479 if (token->keyword == RID_ATTRIBUTE)
30481 if (*attributes == NULL_TREE)
30483 *attributes = cp_parser_attributes_opt (parser);
30484 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30485 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30486 return params;
30488 else
30489 /* We have an error, but parse the attributes, so that we can
30490 carry on. */
30491 *attributes = cp_parser_attributes_opt (parser);
30493 cp_parser_error (parser,
30494 "method attributes must be specified at the end");
30495 return error_mark_node;
30498 return params;
30501 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30503 static void
30504 cp_parser_objc_interstitial_code (cp_parser* parser)
30506 cp_token *token = cp_lexer_peek_token (parser->lexer);
30508 /* If the next token is `extern' and the following token is a string
30509 literal, then we have a linkage specification. */
30510 if (token->keyword == RID_EXTERN
30511 && cp_parser_is_pure_string_literal
30512 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30513 cp_parser_linkage_specification (parser);
30514 /* Handle #pragma, if any. */
30515 else if (token->type == CPP_PRAGMA)
30516 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30517 /* Allow stray semicolons. */
30518 else if (token->type == CPP_SEMICOLON)
30519 cp_lexer_consume_token (parser->lexer);
30520 /* Mark methods as optional or required, when building protocols. */
30521 else if (token->keyword == RID_AT_OPTIONAL)
30523 cp_lexer_consume_token (parser->lexer);
30524 objc_set_method_opt (true);
30526 else if (token->keyword == RID_AT_REQUIRED)
30528 cp_lexer_consume_token (parser->lexer);
30529 objc_set_method_opt (false);
30531 else if (token->keyword == RID_NAMESPACE)
30532 cp_parser_namespace_definition (parser);
30533 /* Other stray characters must generate errors. */
30534 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30536 cp_lexer_consume_token (parser->lexer);
30537 error ("stray %qs between Objective-C++ methods",
30538 token->type == CPP_OPEN_BRACE ? "{" : "}");
30540 /* Finally, try to parse a block-declaration, or a function-definition. */
30541 else
30542 cp_parser_block_declaration (parser, /*statement_p=*/false);
30545 /* Parse a method signature. */
30547 static tree
30548 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30550 tree rettype, kwdparms, optparms;
30551 bool ellipsis = false;
30552 bool is_class_method;
30554 is_class_method = cp_parser_objc_method_type (parser);
30555 rettype = cp_parser_objc_typename (parser);
30556 *attributes = NULL_TREE;
30557 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30558 if (kwdparms == error_mark_node)
30559 return error_mark_node;
30560 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30561 if (optparms == error_mark_node)
30562 return error_mark_node;
30564 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30567 static bool
30568 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30570 tree tattr;
30571 cp_lexer_save_tokens (parser->lexer);
30572 tattr = cp_parser_attributes_opt (parser);
30573 gcc_assert (tattr) ;
30575 /* If the attributes are followed by a method introducer, this is not allowed.
30576 Dump the attributes and flag the situation. */
30577 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30578 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30579 return true;
30581 /* Otherwise, the attributes introduce some interstitial code, possibly so
30582 rewind to allow that check. */
30583 cp_lexer_rollback_tokens (parser->lexer);
30584 return false;
30587 /* Parse an Objective-C method prototype list. */
30589 static void
30590 cp_parser_objc_method_prototype_list (cp_parser* parser)
30592 cp_token *token = cp_lexer_peek_token (parser->lexer);
30594 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30596 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30598 tree attributes, sig;
30599 bool is_class_method;
30600 if (token->type == CPP_PLUS)
30601 is_class_method = true;
30602 else
30603 is_class_method = false;
30604 sig = cp_parser_objc_method_signature (parser, &attributes);
30605 if (sig == error_mark_node)
30607 cp_parser_skip_to_end_of_block_or_statement (parser);
30608 token = cp_lexer_peek_token (parser->lexer);
30609 continue;
30611 objc_add_method_declaration (is_class_method, sig, attributes);
30612 cp_parser_consume_semicolon_at_end_of_statement (parser);
30614 else if (token->keyword == RID_AT_PROPERTY)
30615 cp_parser_objc_at_property_declaration (parser);
30616 else if (token->keyword == RID_ATTRIBUTE
30617 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30618 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30619 OPT_Wattributes,
30620 "prefix attributes are ignored for methods");
30621 else
30622 /* Allow for interspersed non-ObjC++ code. */
30623 cp_parser_objc_interstitial_code (parser);
30625 token = cp_lexer_peek_token (parser->lexer);
30628 if (token->type != CPP_EOF)
30629 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30630 else
30631 cp_parser_error (parser, "expected %<@end%>");
30633 objc_finish_interface ();
30636 /* Parse an Objective-C method definition list. */
30638 static void
30639 cp_parser_objc_method_definition_list (cp_parser* parser)
30641 cp_token *token = cp_lexer_peek_token (parser->lexer);
30643 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30645 tree meth;
30647 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30649 cp_token *ptk;
30650 tree sig, attribute;
30651 bool is_class_method;
30652 if (token->type == CPP_PLUS)
30653 is_class_method = true;
30654 else
30655 is_class_method = false;
30656 push_deferring_access_checks (dk_deferred);
30657 sig = cp_parser_objc_method_signature (parser, &attribute);
30658 if (sig == error_mark_node)
30660 cp_parser_skip_to_end_of_block_or_statement (parser);
30661 token = cp_lexer_peek_token (parser->lexer);
30662 continue;
30664 objc_start_method_definition (is_class_method, sig, attribute,
30665 NULL_TREE);
30667 /* For historical reasons, we accept an optional semicolon. */
30668 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30669 cp_lexer_consume_token (parser->lexer);
30671 ptk = cp_lexer_peek_token (parser->lexer);
30672 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30673 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30675 perform_deferred_access_checks (tf_warning_or_error);
30676 stop_deferring_access_checks ();
30677 meth = cp_parser_function_definition_after_declarator (parser,
30678 false);
30679 pop_deferring_access_checks ();
30680 objc_finish_method_definition (meth);
30683 /* The following case will be removed once @synthesize is
30684 completely implemented. */
30685 else if (token->keyword == RID_AT_PROPERTY)
30686 cp_parser_objc_at_property_declaration (parser);
30687 else if (token->keyword == RID_AT_SYNTHESIZE)
30688 cp_parser_objc_at_synthesize_declaration (parser);
30689 else if (token->keyword == RID_AT_DYNAMIC)
30690 cp_parser_objc_at_dynamic_declaration (parser);
30691 else if (token->keyword == RID_ATTRIBUTE
30692 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30693 warning_at (token->location, OPT_Wattributes,
30694 "prefix attributes are ignored for methods");
30695 else
30696 /* Allow for interspersed non-ObjC++ code. */
30697 cp_parser_objc_interstitial_code (parser);
30699 token = cp_lexer_peek_token (parser->lexer);
30702 if (token->type != CPP_EOF)
30703 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30704 else
30705 cp_parser_error (parser, "expected %<@end%>");
30707 objc_finish_implementation ();
30710 /* Parse Objective-C ivars. */
30712 static void
30713 cp_parser_objc_class_ivars (cp_parser* parser)
30715 cp_token *token = cp_lexer_peek_token (parser->lexer);
30717 if (token->type != CPP_OPEN_BRACE)
30718 return; /* No ivars specified. */
30720 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30721 token = cp_lexer_peek_token (parser->lexer);
30723 while (token->type != CPP_CLOSE_BRACE
30724 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30726 cp_decl_specifier_seq declspecs;
30727 int decl_class_or_enum_p;
30728 tree prefix_attributes;
30730 cp_parser_objc_visibility_spec (parser);
30732 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30733 break;
30735 cp_parser_decl_specifier_seq (parser,
30736 CP_PARSER_FLAGS_OPTIONAL,
30737 &declspecs,
30738 &decl_class_or_enum_p);
30740 /* auto, register, static, extern, mutable. */
30741 if (declspecs.storage_class != sc_none)
30743 cp_parser_error (parser, "invalid type for instance variable");
30744 declspecs.storage_class = sc_none;
30747 /* thread_local. */
30748 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30750 cp_parser_error (parser, "invalid type for instance variable");
30751 declspecs.locations[ds_thread] = 0;
30754 /* typedef. */
30755 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30757 cp_parser_error (parser, "invalid type for instance variable");
30758 declspecs.locations[ds_typedef] = 0;
30761 prefix_attributes = declspecs.attributes;
30762 declspecs.attributes = NULL_TREE;
30764 /* Keep going until we hit the `;' at the end of the
30765 declaration. */
30766 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30768 tree width = NULL_TREE, attributes, first_attribute, decl;
30769 cp_declarator *declarator = NULL;
30770 int ctor_dtor_or_conv_p;
30772 /* Check for a (possibly unnamed) bitfield declaration. */
30773 token = cp_lexer_peek_token (parser->lexer);
30774 if (token->type == CPP_COLON)
30775 goto eat_colon;
30777 if (token->type == CPP_NAME
30778 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30779 == CPP_COLON))
30781 /* Get the name of the bitfield. */
30782 declarator = make_id_declarator (NULL_TREE,
30783 cp_parser_identifier (parser),
30784 sfk_none, token->location);
30786 eat_colon:
30787 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30788 /* Get the width of the bitfield. */
30789 width
30790 = cp_parser_constant_expression (parser);
30792 else
30794 /* Parse the declarator. */
30795 declarator
30796 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30797 &ctor_dtor_or_conv_p,
30798 /*parenthesized_p=*/NULL,
30799 /*member_p=*/false,
30800 /*friend_p=*/false);
30803 /* Look for attributes that apply to the ivar. */
30804 attributes = cp_parser_attributes_opt (parser);
30805 /* Remember which attributes are prefix attributes and
30806 which are not. */
30807 first_attribute = attributes;
30808 /* Combine the attributes. */
30809 attributes = attr_chainon (prefix_attributes, attributes);
30811 if (width)
30812 /* Create the bitfield declaration. */
30813 decl = grokbitfield (declarator, &declspecs,
30814 width, NULL_TREE, attributes);
30815 else
30816 decl = grokfield (declarator, &declspecs,
30817 NULL_TREE, /*init_const_expr_p=*/false,
30818 NULL_TREE, attributes);
30820 /* Add the instance variable. */
30821 if (decl != error_mark_node && decl != NULL_TREE)
30822 objc_add_instance_variable (decl);
30824 /* Reset PREFIX_ATTRIBUTES. */
30825 if (attributes != error_mark_node)
30827 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30828 attributes = TREE_CHAIN (attributes);
30829 if (attributes)
30830 TREE_CHAIN (attributes) = NULL_TREE;
30833 token = cp_lexer_peek_token (parser->lexer);
30835 if (token->type == CPP_COMMA)
30837 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30838 continue;
30840 break;
30843 cp_parser_consume_semicolon_at_end_of_statement (parser);
30844 token = cp_lexer_peek_token (parser->lexer);
30847 if (token->keyword == RID_AT_END)
30848 cp_parser_error (parser, "expected %<}%>");
30850 /* Do not consume the RID_AT_END, so it will be read again as terminating
30851 the @interface of @implementation. */
30852 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30853 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30855 /* For historical reasons, we accept an optional semicolon. */
30856 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30857 cp_lexer_consume_token (parser->lexer);
30860 /* Parse an Objective-C protocol declaration. */
30862 static void
30863 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30865 tree proto, protorefs;
30866 cp_token *tok;
30868 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30869 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30871 tok = cp_lexer_peek_token (parser->lexer);
30872 error_at (tok->location, "identifier expected after %<@protocol%>");
30873 cp_parser_consume_semicolon_at_end_of_statement (parser);
30874 return;
30877 /* See if we have a forward declaration or a definition. */
30878 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30880 /* Try a forward declaration first. */
30881 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30883 while (true)
30885 tree id;
30887 id = cp_parser_identifier (parser);
30888 if (id == error_mark_node)
30889 break;
30891 objc_declare_protocol (id, attributes);
30893 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30894 cp_lexer_consume_token (parser->lexer);
30895 else
30896 break;
30898 cp_parser_consume_semicolon_at_end_of_statement (parser);
30901 /* Ok, we got a full-fledged definition (or at least should). */
30902 else
30904 proto = cp_parser_identifier (parser);
30905 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30906 objc_start_protocol (proto, protorefs, attributes);
30907 cp_parser_objc_method_prototype_list (parser);
30911 /* Parse an Objective-C superclass or category. */
30913 static void
30914 cp_parser_objc_superclass_or_category (cp_parser *parser,
30915 bool iface_p,
30916 tree *super,
30917 tree *categ, bool *is_class_extension)
30919 cp_token *next = cp_lexer_peek_token (parser->lexer);
30921 *super = *categ = NULL_TREE;
30922 *is_class_extension = false;
30923 if (next->type == CPP_COLON)
30925 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30926 *super = cp_parser_identifier (parser);
30928 else if (next->type == CPP_OPEN_PAREN)
30930 matching_parens parens;
30931 parens.consume_open (parser); /* Eat '('. */
30933 /* If there is no category name, and this is an @interface, we
30934 have a class extension. */
30935 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30937 *categ = NULL_TREE;
30938 *is_class_extension = true;
30940 else
30941 *categ = cp_parser_identifier (parser);
30943 parens.require_close (parser);
30947 /* Parse an Objective-C class interface. */
30949 static void
30950 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30952 tree name, super, categ, protos;
30953 bool is_class_extension;
30955 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30956 name = cp_parser_identifier (parser);
30957 if (name == error_mark_node)
30959 /* It's hard to recover because even if valid @interface stuff
30960 is to follow, we can't compile it (or validate it) if we
30961 don't even know which class it refers to. Let's assume this
30962 was a stray '@interface' token in the stream and skip it.
30964 return;
30966 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30967 &is_class_extension);
30968 protos = cp_parser_objc_protocol_refs_opt (parser);
30970 /* We have either a class or a category on our hands. */
30971 if (categ || is_class_extension)
30972 objc_start_category_interface (name, categ, protos, attributes);
30973 else
30975 objc_start_class_interface (name, super, protos, attributes);
30976 /* Handle instance variable declarations, if any. */
30977 cp_parser_objc_class_ivars (parser);
30978 objc_continue_interface ();
30981 cp_parser_objc_method_prototype_list (parser);
30984 /* Parse an Objective-C class implementation. */
30986 static void
30987 cp_parser_objc_class_implementation (cp_parser* parser)
30989 tree name, super, categ;
30990 bool is_class_extension;
30992 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30993 name = cp_parser_identifier (parser);
30994 if (name == error_mark_node)
30996 /* It's hard to recover because even if valid @implementation
30997 stuff is to follow, we can't compile it (or validate it) if
30998 we don't even know which class it refers to. Let's assume
30999 this was a stray '@implementation' token in the stream and
31000 skip it.
31002 return;
31004 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
31005 &is_class_extension);
31007 /* We have either a class or a category on our hands. */
31008 if (categ)
31009 objc_start_category_implementation (name, categ);
31010 else
31012 objc_start_class_implementation (name, super);
31013 /* Handle instance variable declarations, if any. */
31014 cp_parser_objc_class_ivars (parser);
31015 objc_continue_implementation ();
31018 cp_parser_objc_method_definition_list (parser);
31021 /* Consume the @end token and finish off the implementation. */
31023 static void
31024 cp_parser_objc_end_implementation (cp_parser* parser)
31026 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31027 objc_finish_implementation ();
31030 /* Parse an Objective-C declaration. */
31032 static void
31033 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
31035 /* Try to figure out what kind of declaration is present. */
31036 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31038 if (attributes)
31039 switch (kwd->keyword)
31041 case RID_AT_ALIAS:
31042 case RID_AT_CLASS:
31043 case RID_AT_END:
31044 error_at (kwd->location, "attributes may not be specified before"
31045 " the %<@%D%> Objective-C++ keyword",
31046 kwd->u.value);
31047 attributes = NULL;
31048 break;
31049 case RID_AT_IMPLEMENTATION:
31050 warning_at (kwd->location, OPT_Wattributes,
31051 "prefix attributes are ignored before %<@%D%>",
31052 kwd->u.value);
31053 attributes = NULL;
31054 default:
31055 break;
31058 switch (kwd->keyword)
31060 case RID_AT_ALIAS:
31061 cp_parser_objc_alias_declaration (parser);
31062 break;
31063 case RID_AT_CLASS:
31064 cp_parser_objc_class_declaration (parser);
31065 break;
31066 case RID_AT_PROTOCOL:
31067 cp_parser_objc_protocol_declaration (parser, attributes);
31068 break;
31069 case RID_AT_INTERFACE:
31070 cp_parser_objc_class_interface (parser, attributes);
31071 break;
31072 case RID_AT_IMPLEMENTATION:
31073 cp_parser_objc_class_implementation (parser);
31074 break;
31075 case RID_AT_END:
31076 cp_parser_objc_end_implementation (parser);
31077 break;
31078 default:
31079 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31080 kwd->u.value);
31081 cp_parser_skip_to_end_of_block_or_statement (parser);
31085 /* Parse an Objective-C try-catch-finally statement.
31087 objc-try-catch-finally-stmt:
31088 @try compound-statement objc-catch-clause-seq [opt]
31089 objc-finally-clause [opt]
31091 objc-catch-clause-seq:
31092 objc-catch-clause objc-catch-clause-seq [opt]
31094 objc-catch-clause:
31095 @catch ( objc-exception-declaration ) compound-statement
31097 objc-finally-clause:
31098 @finally compound-statement
31100 objc-exception-declaration:
31101 parameter-declaration
31102 '...'
31104 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31106 Returns NULL_TREE.
31108 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31109 for C. Keep them in sync. */
31111 static tree
31112 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
31114 location_t location;
31115 tree stmt;
31117 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
31118 location = cp_lexer_peek_token (parser->lexer)->location;
31119 objc_maybe_warn_exceptions (location);
31120 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31121 node, lest it get absorbed into the surrounding block. */
31122 stmt = push_stmt_list ();
31123 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31124 objc_begin_try_stmt (location, pop_stmt_list (stmt));
31126 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
31128 cp_parameter_declarator *parm;
31129 tree parameter_declaration = error_mark_node;
31130 bool seen_open_paren = false;
31131 matching_parens parens;
31133 cp_lexer_consume_token (parser->lexer);
31134 if (parens.require_open (parser))
31135 seen_open_paren = true;
31136 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31138 /* We have "@catch (...)" (where the '...' are literally
31139 what is in the code). Skip the '...'.
31140 parameter_declaration is set to NULL_TREE, and
31141 objc_being_catch_clauses() knows that that means
31142 '...'. */
31143 cp_lexer_consume_token (parser->lexer);
31144 parameter_declaration = NULL_TREE;
31146 else
31148 /* We have "@catch (NSException *exception)" or something
31149 like that. Parse the parameter declaration. */
31150 parm = cp_parser_parameter_declaration (parser, false, NULL);
31151 if (parm == NULL)
31152 parameter_declaration = error_mark_node;
31153 else
31154 parameter_declaration = grokdeclarator (parm->declarator,
31155 &parm->decl_specifiers,
31156 PARM, /*initialized=*/0,
31157 /*attrlist=*/NULL);
31159 if (seen_open_paren)
31160 parens.require_close (parser);
31161 else
31163 /* If there was no open parenthesis, we are recovering from
31164 an error, and we are trying to figure out what mistake
31165 the user has made. */
31167 /* If there is an immediate closing parenthesis, the user
31168 probably forgot the opening one (ie, they typed "@catch
31169 NSException *e)". Parse the closing parenthesis and keep
31170 going. */
31171 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31172 cp_lexer_consume_token (parser->lexer);
31174 /* If these is no immediate closing parenthesis, the user
31175 probably doesn't know that parenthesis are required at
31176 all (ie, they typed "@catch NSException *e"). So, just
31177 forget about the closing parenthesis and keep going. */
31179 objc_begin_catch_clause (parameter_declaration);
31180 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31181 objc_finish_catch_clause ();
31183 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
31185 cp_lexer_consume_token (parser->lexer);
31186 location = cp_lexer_peek_token (parser->lexer)->location;
31187 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31188 node, lest it get absorbed into the surrounding block. */
31189 stmt = push_stmt_list ();
31190 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31191 objc_build_finally_clause (location, pop_stmt_list (stmt));
31194 return objc_finish_try_stmt ();
31197 /* Parse an Objective-C synchronized statement.
31199 objc-synchronized-stmt:
31200 @synchronized ( expression ) compound-statement
31202 Returns NULL_TREE. */
31204 static tree
31205 cp_parser_objc_synchronized_statement (cp_parser *parser)
31207 location_t location;
31208 tree lock, stmt;
31210 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31212 location = cp_lexer_peek_token (parser->lexer)->location;
31213 objc_maybe_warn_exceptions (location);
31214 matching_parens parens;
31215 parens.require_open (parser);
31216 lock = cp_parser_expression (parser);
31217 parens.require_close (parser);
31219 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31220 node, lest it get absorbed into the surrounding block. */
31221 stmt = push_stmt_list ();
31222 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31224 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31227 /* Parse an Objective-C throw statement.
31229 objc-throw-stmt:
31230 @throw assignment-expression [opt] ;
31232 Returns a constructed '@throw' statement. */
31234 static tree
31235 cp_parser_objc_throw_statement (cp_parser *parser)
31237 tree expr = NULL_TREE;
31238 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31240 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31242 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31243 expr = cp_parser_expression (parser);
31245 cp_parser_consume_semicolon_at_end_of_statement (parser);
31247 return objc_build_throw_stmt (loc, expr);
31250 /* Parse an Objective-C statement. */
31252 static tree
31253 cp_parser_objc_statement (cp_parser * parser)
31255 /* Try to figure out what kind of declaration is present. */
31256 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31258 switch (kwd->keyword)
31260 case RID_AT_TRY:
31261 return cp_parser_objc_try_catch_finally_statement (parser);
31262 case RID_AT_SYNCHRONIZED:
31263 return cp_parser_objc_synchronized_statement (parser);
31264 case RID_AT_THROW:
31265 return cp_parser_objc_throw_statement (parser);
31266 default:
31267 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31268 kwd->u.value);
31269 cp_parser_skip_to_end_of_block_or_statement (parser);
31272 return error_mark_node;
31275 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31276 look ahead to see if an objc keyword follows the attributes. This
31277 is to detect the use of prefix attributes on ObjC @interface and
31278 @protocol. */
31280 static bool
31281 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31283 cp_lexer_save_tokens (parser->lexer);
31284 *attrib = cp_parser_attributes_opt (parser);
31285 gcc_assert (*attrib);
31286 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31288 cp_lexer_commit_tokens (parser->lexer);
31289 return true;
31291 cp_lexer_rollback_tokens (parser->lexer);
31292 return false;
31295 /* This routine is a minimal replacement for
31296 c_parser_struct_declaration () used when parsing the list of
31297 types/names or ObjC++ properties. For example, when parsing the
31298 code
31300 @property (readonly) int a, b, c;
31302 this function is responsible for parsing "int a, int b, int c" and
31303 returning the declarations as CHAIN of DECLs.
31305 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31306 similar parsing. */
31307 static tree
31308 cp_parser_objc_struct_declaration (cp_parser *parser)
31310 tree decls = NULL_TREE;
31311 cp_decl_specifier_seq declspecs;
31312 int decl_class_or_enum_p;
31313 tree prefix_attributes;
31315 cp_parser_decl_specifier_seq (parser,
31316 CP_PARSER_FLAGS_NONE,
31317 &declspecs,
31318 &decl_class_or_enum_p);
31320 if (declspecs.type == error_mark_node)
31321 return error_mark_node;
31323 /* auto, register, static, extern, mutable. */
31324 if (declspecs.storage_class != sc_none)
31326 cp_parser_error (parser, "invalid type for property");
31327 declspecs.storage_class = sc_none;
31330 /* thread_local. */
31331 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31333 cp_parser_error (parser, "invalid type for property");
31334 declspecs.locations[ds_thread] = 0;
31337 /* typedef. */
31338 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31340 cp_parser_error (parser, "invalid type for property");
31341 declspecs.locations[ds_typedef] = 0;
31344 prefix_attributes = declspecs.attributes;
31345 declspecs.attributes = NULL_TREE;
31347 /* Keep going until we hit the `;' at the end of the declaration. */
31348 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31350 tree attributes, first_attribute, decl;
31351 cp_declarator *declarator;
31352 cp_token *token;
31354 /* Parse the declarator. */
31355 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31356 NULL, NULL, false, false);
31358 /* Look for attributes that apply to the ivar. */
31359 attributes = cp_parser_attributes_opt (parser);
31360 /* Remember which attributes are prefix attributes and
31361 which are not. */
31362 first_attribute = attributes;
31363 /* Combine the attributes. */
31364 attributes = attr_chainon (prefix_attributes, attributes);
31366 decl = grokfield (declarator, &declspecs,
31367 NULL_TREE, /*init_const_expr_p=*/false,
31368 NULL_TREE, attributes);
31370 if (decl == error_mark_node || decl == NULL_TREE)
31371 return error_mark_node;
31373 /* Reset PREFIX_ATTRIBUTES. */
31374 if (attributes != error_mark_node)
31376 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31377 attributes = TREE_CHAIN (attributes);
31378 if (attributes)
31379 TREE_CHAIN (attributes) = NULL_TREE;
31382 DECL_CHAIN (decl) = decls;
31383 decls = decl;
31385 token = cp_lexer_peek_token (parser->lexer);
31386 if (token->type == CPP_COMMA)
31388 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31389 continue;
31391 else
31392 break;
31394 return decls;
31397 /* Parse an Objective-C @property declaration. The syntax is:
31399 objc-property-declaration:
31400 '@property' objc-property-attributes[opt] struct-declaration ;
31402 objc-property-attributes:
31403 '(' objc-property-attribute-list ')'
31405 objc-property-attribute-list:
31406 objc-property-attribute
31407 objc-property-attribute-list, objc-property-attribute
31409 objc-property-attribute
31410 'getter' = identifier
31411 'setter' = identifier
31412 'readonly'
31413 'readwrite'
31414 'assign'
31415 'retain'
31416 'copy'
31417 'nonatomic'
31419 For example:
31420 @property NSString *name;
31421 @property (readonly) id object;
31422 @property (retain, nonatomic, getter=getTheName) id name;
31423 @property int a, b, c;
31425 PS: This function is identical to
31426 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31427 static void
31428 cp_parser_objc_at_property_declaration (cp_parser *parser)
31430 /* The following variables hold the attributes of the properties as
31431 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31432 seen. When we see an attribute, we set them to 'true' (if they
31433 are boolean properties) or to the identifier (if they have an
31434 argument, ie, for getter and setter). Note that here we only
31435 parse the list of attributes, check the syntax and accumulate the
31436 attributes that we find. objc_add_property_declaration() will
31437 then process the information. */
31438 bool property_assign = false;
31439 bool property_copy = false;
31440 tree property_getter_ident = NULL_TREE;
31441 bool property_nonatomic = false;
31442 bool property_readonly = false;
31443 bool property_readwrite = false;
31444 bool property_retain = false;
31445 tree property_setter_ident = NULL_TREE;
31447 /* 'properties' is the list of properties that we read. Usually a
31448 single one, but maybe more (eg, in "@property int a, b, c;" there
31449 are three). */
31450 tree properties;
31451 location_t loc;
31453 loc = cp_lexer_peek_token (parser->lexer)->location;
31455 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31457 /* Parse the optional attribute list... */
31458 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31460 /* Eat the '('. */
31461 matching_parens parens;
31462 parens.consume_open (parser);
31464 while (true)
31466 bool syntax_error = false;
31467 cp_token *token = cp_lexer_peek_token (parser->lexer);
31468 enum rid keyword;
31470 if (token->type != CPP_NAME)
31472 cp_parser_error (parser, "expected identifier");
31473 break;
31475 keyword = C_RID_CODE (token->u.value);
31476 cp_lexer_consume_token (parser->lexer);
31477 switch (keyword)
31479 case RID_ASSIGN: property_assign = true; break;
31480 case RID_COPY: property_copy = true; break;
31481 case RID_NONATOMIC: property_nonatomic = true; break;
31482 case RID_READONLY: property_readonly = true; break;
31483 case RID_READWRITE: property_readwrite = true; break;
31484 case RID_RETAIN: property_retain = true; break;
31486 case RID_GETTER:
31487 case RID_SETTER:
31488 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31490 if (keyword == RID_GETTER)
31491 cp_parser_error (parser,
31492 "missing %<=%> (after %<getter%> attribute)");
31493 else
31494 cp_parser_error (parser,
31495 "missing %<=%> (after %<setter%> attribute)");
31496 syntax_error = true;
31497 break;
31499 cp_lexer_consume_token (parser->lexer); /* eat the = */
31500 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31502 cp_parser_error (parser, "expected identifier");
31503 syntax_error = true;
31504 break;
31506 if (keyword == RID_SETTER)
31508 if (property_setter_ident != NULL_TREE)
31510 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31511 cp_lexer_consume_token (parser->lexer);
31513 else
31514 property_setter_ident = cp_parser_objc_selector (parser);
31515 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31516 cp_parser_error (parser, "setter name must terminate with %<:%>");
31517 else
31518 cp_lexer_consume_token (parser->lexer);
31520 else
31522 if (property_getter_ident != NULL_TREE)
31524 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31525 cp_lexer_consume_token (parser->lexer);
31527 else
31528 property_getter_ident = cp_parser_objc_selector (parser);
31530 break;
31531 default:
31532 cp_parser_error (parser, "unknown property attribute");
31533 syntax_error = true;
31534 break;
31537 if (syntax_error)
31538 break;
31540 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31541 cp_lexer_consume_token (parser->lexer);
31542 else
31543 break;
31546 /* FIXME: "@property (setter, assign);" will generate a spurious
31547 "error: expected ‘)’ before ‘,’ token". This is because
31548 cp_parser_require, unlike the C counterpart, will produce an
31549 error even if we are in error recovery. */
31550 if (!parens.require_close (parser))
31552 cp_parser_skip_to_closing_parenthesis (parser,
31553 /*recovering=*/true,
31554 /*or_comma=*/false,
31555 /*consume_paren=*/true);
31559 /* ... and the property declaration(s). */
31560 properties = cp_parser_objc_struct_declaration (parser);
31562 if (properties == error_mark_node)
31564 cp_parser_skip_to_end_of_statement (parser);
31565 /* If the next token is now a `;', consume it. */
31566 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31567 cp_lexer_consume_token (parser->lexer);
31568 return;
31571 if (properties == NULL_TREE)
31572 cp_parser_error (parser, "expected identifier");
31573 else
31575 /* Comma-separated properties are chained together in
31576 reverse order; add them one by one. */
31577 properties = nreverse (properties);
31579 for (; properties; properties = TREE_CHAIN (properties))
31580 objc_add_property_declaration (loc, copy_node (properties),
31581 property_readonly, property_readwrite,
31582 property_assign, property_retain,
31583 property_copy, property_nonatomic,
31584 property_getter_ident, property_setter_ident);
31587 cp_parser_consume_semicolon_at_end_of_statement (parser);
31590 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31592 objc-synthesize-declaration:
31593 @synthesize objc-synthesize-identifier-list ;
31595 objc-synthesize-identifier-list:
31596 objc-synthesize-identifier
31597 objc-synthesize-identifier-list, objc-synthesize-identifier
31599 objc-synthesize-identifier
31600 identifier
31601 identifier = identifier
31603 For example:
31604 @synthesize MyProperty;
31605 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31607 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31608 for C. Keep them in sync.
31610 static void
31611 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31613 tree list = NULL_TREE;
31614 location_t loc;
31615 loc = cp_lexer_peek_token (parser->lexer)->location;
31617 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31618 while (true)
31620 tree property, ivar;
31621 property = cp_parser_identifier (parser);
31622 if (property == error_mark_node)
31624 cp_parser_consume_semicolon_at_end_of_statement (parser);
31625 return;
31627 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31629 cp_lexer_consume_token (parser->lexer);
31630 ivar = cp_parser_identifier (parser);
31631 if (ivar == error_mark_node)
31633 cp_parser_consume_semicolon_at_end_of_statement (parser);
31634 return;
31637 else
31638 ivar = NULL_TREE;
31639 list = chainon (list, build_tree_list (ivar, property));
31640 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31641 cp_lexer_consume_token (parser->lexer);
31642 else
31643 break;
31645 cp_parser_consume_semicolon_at_end_of_statement (parser);
31646 objc_add_synthesize_declaration (loc, list);
31649 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31651 objc-dynamic-declaration:
31652 @dynamic identifier-list ;
31654 For example:
31655 @dynamic MyProperty;
31656 @dynamic MyProperty, AnotherProperty;
31658 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31659 for C. Keep them in sync.
31661 static void
31662 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31664 tree list = NULL_TREE;
31665 location_t loc;
31666 loc = cp_lexer_peek_token (parser->lexer)->location;
31668 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31669 while (true)
31671 tree property;
31672 property = cp_parser_identifier (parser);
31673 if (property == error_mark_node)
31675 cp_parser_consume_semicolon_at_end_of_statement (parser);
31676 return;
31678 list = chainon (list, build_tree_list (NULL, property));
31679 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31680 cp_lexer_consume_token (parser->lexer);
31681 else
31682 break;
31684 cp_parser_consume_semicolon_at_end_of_statement (parser);
31685 objc_add_dynamic_declaration (loc, list);
31689 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
31691 /* Returns name of the next clause.
31692 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31693 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31694 returned and the token is consumed. */
31696 static pragma_omp_clause
31697 cp_parser_omp_clause_name (cp_parser *parser)
31699 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31701 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31702 result = PRAGMA_OACC_CLAUSE_AUTO;
31703 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31704 result = PRAGMA_OMP_CLAUSE_IF;
31705 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31706 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31707 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31708 result = PRAGMA_OACC_CLAUSE_DELETE;
31709 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31710 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31711 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31712 result = PRAGMA_OMP_CLAUSE_FOR;
31713 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31715 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31716 const char *p = IDENTIFIER_POINTER (id);
31718 switch (p[0])
31720 case 'a':
31721 if (!strcmp ("aligned", p))
31722 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31723 else if (!strcmp ("async", p))
31724 result = PRAGMA_OACC_CLAUSE_ASYNC;
31725 break;
31726 case 'c':
31727 if (!strcmp ("collapse", p))
31728 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31729 else if (!strcmp ("copy", p))
31730 result = PRAGMA_OACC_CLAUSE_COPY;
31731 else if (!strcmp ("copyin", p))
31732 result = PRAGMA_OMP_CLAUSE_COPYIN;
31733 else if (!strcmp ("copyout", p))
31734 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31735 else if (!strcmp ("copyprivate", p))
31736 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31737 else if (!strcmp ("create", p))
31738 result = PRAGMA_OACC_CLAUSE_CREATE;
31739 break;
31740 case 'd':
31741 if (!strcmp ("defaultmap", p))
31742 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31743 else if (!strcmp ("depend", p))
31744 result = PRAGMA_OMP_CLAUSE_DEPEND;
31745 else if (!strcmp ("device", p))
31746 result = PRAGMA_OMP_CLAUSE_DEVICE;
31747 else if (!strcmp ("deviceptr", p))
31748 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31749 else if (!strcmp ("device_resident", p))
31750 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31751 else if (!strcmp ("dist_schedule", p))
31752 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31753 break;
31754 case 'f':
31755 if (!strcmp ("final", p))
31756 result = PRAGMA_OMP_CLAUSE_FINAL;
31757 else if (!strcmp ("finalize", p))
31758 result = PRAGMA_OACC_CLAUSE_FINALIZE;
31759 else if (!strcmp ("firstprivate", p))
31760 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31761 else if (!strcmp ("from", p))
31762 result = PRAGMA_OMP_CLAUSE_FROM;
31763 break;
31764 case 'g':
31765 if (!strcmp ("gang", p))
31766 result = PRAGMA_OACC_CLAUSE_GANG;
31767 else if (!strcmp ("grainsize", p))
31768 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31769 break;
31770 case 'h':
31771 if (!strcmp ("hint", p))
31772 result = PRAGMA_OMP_CLAUSE_HINT;
31773 else if (!strcmp ("host", p))
31774 result = PRAGMA_OACC_CLAUSE_HOST;
31775 break;
31776 case 'i':
31777 if (!strcmp ("if_present", p))
31778 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
31779 else if (!strcmp ("in_reduction", p))
31780 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
31781 else if (!strcmp ("inbranch", p))
31782 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31783 else if (!strcmp ("independent", p))
31784 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31785 else if (!strcmp ("is_device_ptr", p))
31786 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31787 break;
31788 case 'l':
31789 if (!strcmp ("lastprivate", p))
31790 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31791 else if (!strcmp ("linear", p))
31792 result = PRAGMA_OMP_CLAUSE_LINEAR;
31793 else if (!strcmp ("link", p))
31794 result = PRAGMA_OMP_CLAUSE_LINK;
31795 break;
31796 case 'm':
31797 if (!strcmp ("map", p))
31798 result = PRAGMA_OMP_CLAUSE_MAP;
31799 else if (!strcmp ("mergeable", p))
31800 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31801 break;
31802 case 'n':
31803 if (!strcmp ("nogroup", p))
31804 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31805 else if (!strcmp ("nontemporal", p))
31806 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
31807 else if (!strcmp ("notinbranch", p))
31808 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31809 else if (!strcmp ("nowait", p))
31810 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31811 else if (!strcmp ("num_gangs", p))
31812 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31813 else if (!strcmp ("num_tasks", p))
31814 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31815 else if (!strcmp ("num_teams", p))
31816 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31817 else if (!strcmp ("num_threads", p))
31818 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31819 else if (!strcmp ("num_workers", p))
31820 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31821 break;
31822 case 'o':
31823 if (!strcmp ("ordered", p))
31824 result = PRAGMA_OMP_CLAUSE_ORDERED;
31825 break;
31826 case 'p':
31827 if (!strcmp ("parallel", p))
31828 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31829 else if (!strcmp ("present", p))
31830 result = PRAGMA_OACC_CLAUSE_PRESENT;
31831 else if (!strcmp ("present_or_copy", p)
31832 || !strcmp ("pcopy", p))
31833 result = PRAGMA_OACC_CLAUSE_COPY;
31834 else if (!strcmp ("present_or_copyin", p)
31835 || !strcmp ("pcopyin", p))
31836 result = PRAGMA_OACC_CLAUSE_COPYIN;
31837 else if (!strcmp ("present_or_copyout", p)
31838 || !strcmp ("pcopyout", p))
31839 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31840 else if (!strcmp ("present_or_create", p)
31841 || !strcmp ("pcreate", p))
31842 result = PRAGMA_OACC_CLAUSE_CREATE;
31843 else if (!strcmp ("priority", p))
31844 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31845 else if (!strcmp ("proc_bind", p))
31846 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31847 break;
31848 case 'r':
31849 if (!strcmp ("reduction", p))
31850 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31851 break;
31852 case 's':
31853 if (!strcmp ("safelen", p))
31854 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31855 else if (!strcmp ("schedule", p))
31856 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31857 else if (!strcmp ("sections", p))
31858 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31859 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
31860 result = PRAGMA_OACC_CLAUSE_HOST;
31861 else if (!strcmp ("seq", p))
31862 result = PRAGMA_OACC_CLAUSE_SEQ;
31863 else if (!strcmp ("shared", p))
31864 result = PRAGMA_OMP_CLAUSE_SHARED;
31865 else if (!strcmp ("simd", p))
31866 result = PRAGMA_OMP_CLAUSE_SIMD;
31867 else if (!strcmp ("simdlen", p))
31868 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31869 break;
31870 case 't':
31871 if (!strcmp ("task_reduction", p))
31872 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
31873 else if (!strcmp ("taskgroup", p))
31874 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31875 else if (!strcmp ("thread_limit", p))
31876 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31877 else if (!strcmp ("threads", p))
31878 result = PRAGMA_OMP_CLAUSE_THREADS;
31879 else if (!strcmp ("tile", p))
31880 result = PRAGMA_OACC_CLAUSE_TILE;
31881 else if (!strcmp ("to", p))
31882 result = PRAGMA_OMP_CLAUSE_TO;
31883 break;
31884 case 'u':
31885 if (!strcmp ("uniform", p))
31886 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31887 else if (!strcmp ("untied", p))
31888 result = PRAGMA_OMP_CLAUSE_UNTIED;
31889 else if (!strcmp ("use_device", p))
31890 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31891 else if (!strcmp ("use_device_ptr", p))
31892 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31893 break;
31894 case 'v':
31895 if (!strcmp ("vector", p))
31896 result = PRAGMA_OACC_CLAUSE_VECTOR;
31897 else if (!strcmp ("vector_length", p))
31898 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31899 break;
31900 case 'w':
31901 if (!strcmp ("wait", p))
31902 result = PRAGMA_OACC_CLAUSE_WAIT;
31903 else if (!strcmp ("worker", p))
31904 result = PRAGMA_OACC_CLAUSE_WORKER;
31905 break;
31909 if (result != PRAGMA_OMP_CLAUSE_NONE)
31910 cp_lexer_consume_token (parser->lexer);
31912 return result;
31915 /* Validate that a clause of the given type does not already exist. */
31917 static void
31918 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31919 const char *name, location_t location)
31921 tree c;
31923 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31924 if (OMP_CLAUSE_CODE (c) == code)
31926 error_at (location, "too many %qs clauses", name);
31927 break;
31931 /* OpenMP 2.5:
31932 variable-list:
31933 identifier
31934 variable-list , identifier
31936 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31937 colon). An opening parenthesis will have been consumed by the caller.
31939 If KIND is nonzero, create the appropriate node and install the decl
31940 in OMP_CLAUSE_DECL and add the node to the head of the list.
31942 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31943 return the list created.
31945 COLON can be NULL if only closing parenthesis should end the list,
31946 or pointer to bool which will receive false if the list is terminated
31947 by closing parenthesis or true if the list is terminated by colon. */
31949 static tree
31950 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31951 tree list, bool *colon)
31953 cp_token *token;
31954 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31955 if (colon)
31957 parser->colon_corrects_to_scope_p = false;
31958 *colon = false;
31960 while (1)
31962 tree name, decl;
31964 if (kind == OMP_CLAUSE_DEPEND)
31965 cp_parser_parse_tentatively (parser);
31966 token = cp_lexer_peek_token (parser->lexer);
31967 if (kind != 0
31968 && current_class_ptr
31969 && cp_parser_is_keyword (token, RID_THIS))
31971 decl = finish_this_expr ();
31972 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31973 || CONVERT_EXPR_P (decl))
31974 decl = TREE_OPERAND (decl, 0);
31975 cp_lexer_consume_token (parser->lexer);
31977 else
31979 name = cp_parser_id_expression (parser, /*template_p=*/false,
31980 /*check_dependency_p=*/true,
31981 /*template_p=*/NULL,
31982 /*declarator_p=*/false,
31983 /*optional_p=*/false);
31984 if (name == error_mark_node)
31986 if (kind == OMP_CLAUSE_DEPEND
31987 && cp_parser_simulate_error (parser))
31988 goto depend_lvalue;
31989 goto skip_comma;
31992 if (identifier_p (name))
31993 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31994 else
31995 decl = name;
31996 if (decl == error_mark_node)
31998 if (kind == OMP_CLAUSE_DEPEND
31999 && cp_parser_simulate_error (parser))
32000 goto depend_lvalue;
32001 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
32002 token->location);
32005 if (decl == error_mark_node)
32007 else if (kind != 0)
32009 switch (kind)
32011 case OMP_CLAUSE__CACHE_:
32012 /* The OpenACC cache directive explicitly only allows "array
32013 elements or subarrays". */
32014 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
32016 error_at (token->location, "expected %<[%>");
32017 decl = error_mark_node;
32018 break;
32020 /* FALLTHROUGH. */
32021 case OMP_CLAUSE_MAP:
32022 case OMP_CLAUSE_FROM:
32023 case OMP_CLAUSE_TO:
32024 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
32026 location_t loc
32027 = cp_lexer_peek_token (parser->lexer)->location;
32028 cp_id_kind idk = CP_ID_KIND_NONE;
32029 cp_lexer_consume_token (parser->lexer);
32030 decl = convert_from_reference (decl);
32031 decl
32032 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
32033 decl, false,
32034 &idk, loc);
32036 /* FALLTHROUGH. */
32037 case OMP_CLAUSE_DEPEND:
32038 case OMP_CLAUSE_REDUCTION:
32039 case OMP_CLAUSE_IN_REDUCTION:
32040 case OMP_CLAUSE_TASK_REDUCTION:
32041 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
32043 tree low_bound = NULL_TREE, length = NULL_TREE;
32045 parser->colon_corrects_to_scope_p = false;
32046 cp_lexer_consume_token (parser->lexer);
32047 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32048 low_bound = cp_parser_expression (parser);
32049 if (!colon)
32050 parser->colon_corrects_to_scope_p
32051 = saved_colon_corrects_to_scope_p;
32052 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
32053 length = integer_one_node;
32054 else
32056 /* Look for `:'. */
32057 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32059 if (kind == OMP_CLAUSE_DEPEND
32060 && cp_parser_simulate_error (parser))
32061 goto depend_lvalue;
32062 goto skip_comma;
32064 if (kind == OMP_CLAUSE_DEPEND)
32065 cp_parser_commit_to_tentative_parse (parser);
32066 if (!cp_lexer_next_token_is (parser->lexer,
32067 CPP_CLOSE_SQUARE))
32068 length = cp_parser_expression (parser);
32070 /* Look for the closing `]'. */
32071 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
32072 RT_CLOSE_SQUARE))
32074 if (kind == OMP_CLAUSE_DEPEND
32075 && cp_parser_simulate_error (parser))
32076 goto depend_lvalue;
32077 goto skip_comma;
32080 decl = tree_cons (low_bound, length, decl);
32082 break;
32083 default:
32084 break;
32087 if (kind == OMP_CLAUSE_DEPEND)
32089 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
32090 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32091 && cp_parser_simulate_error (parser))
32093 depend_lvalue:
32094 cp_parser_abort_tentative_parse (parser);
32095 decl = cp_parser_assignment_expression (parser, NULL,
32096 false, false);
32098 else
32099 cp_parser_parse_definitely (parser);
32102 tree u = build_omp_clause (token->location, kind);
32103 OMP_CLAUSE_DECL (u) = decl;
32104 OMP_CLAUSE_CHAIN (u) = list;
32105 list = u;
32107 else
32108 list = tree_cons (decl, NULL_TREE, list);
32110 get_comma:
32111 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32112 break;
32113 cp_lexer_consume_token (parser->lexer);
32116 if (colon)
32117 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32119 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32121 *colon = true;
32122 cp_parser_require (parser, CPP_COLON, RT_COLON);
32123 return list;
32126 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32128 int ending;
32130 /* Try to resync to an unnested comma. Copied from
32131 cp_parser_parenthesized_expression_list. */
32132 skip_comma:
32133 if (colon)
32134 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32135 ending = cp_parser_skip_to_closing_parenthesis (parser,
32136 /*recovering=*/true,
32137 /*or_comma=*/true,
32138 /*consume_paren=*/true);
32139 if (ending < 0)
32140 goto get_comma;
32143 return list;
32146 /* Similarly, but expect leading and trailing parenthesis. This is a very
32147 common case for omp clauses. */
32149 static tree
32150 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
32152 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32153 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
32154 return list;
32157 /* OpenACC 2.0:
32158 copy ( variable-list )
32159 copyin ( variable-list )
32160 copyout ( variable-list )
32161 create ( variable-list )
32162 delete ( variable-list )
32163 present ( variable-list ) */
32165 static tree
32166 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
32167 tree list)
32169 enum gomp_map_kind kind;
32170 switch (c_kind)
32172 case PRAGMA_OACC_CLAUSE_COPY:
32173 kind = GOMP_MAP_TOFROM;
32174 break;
32175 case PRAGMA_OACC_CLAUSE_COPYIN:
32176 kind = GOMP_MAP_TO;
32177 break;
32178 case PRAGMA_OACC_CLAUSE_COPYOUT:
32179 kind = GOMP_MAP_FROM;
32180 break;
32181 case PRAGMA_OACC_CLAUSE_CREATE:
32182 kind = GOMP_MAP_ALLOC;
32183 break;
32184 case PRAGMA_OACC_CLAUSE_DELETE:
32185 kind = GOMP_MAP_RELEASE;
32186 break;
32187 case PRAGMA_OACC_CLAUSE_DEVICE:
32188 kind = GOMP_MAP_FORCE_TO;
32189 break;
32190 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32191 kind = GOMP_MAP_DEVICE_RESIDENT;
32192 break;
32193 case PRAGMA_OACC_CLAUSE_HOST:
32194 kind = GOMP_MAP_FORCE_FROM;
32195 break;
32196 case PRAGMA_OACC_CLAUSE_LINK:
32197 kind = GOMP_MAP_LINK;
32198 break;
32199 case PRAGMA_OACC_CLAUSE_PRESENT:
32200 kind = GOMP_MAP_FORCE_PRESENT;
32201 break;
32202 default:
32203 gcc_unreachable ();
32205 tree nl, c;
32206 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32208 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32209 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32211 return nl;
32214 /* OpenACC 2.0:
32215 deviceptr ( variable-list ) */
32217 static tree
32218 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32220 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32221 tree vars, t;
32223 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32224 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32225 variable-list must only allow for pointer variables. */
32226 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32227 for (t = vars; t; t = TREE_CHAIN (t))
32229 tree v = TREE_PURPOSE (t);
32230 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32231 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32232 OMP_CLAUSE_DECL (u) = v;
32233 OMP_CLAUSE_CHAIN (u) = list;
32234 list = u;
32237 return list;
32240 /* OpenACC 2.5:
32241 auto
32242 finalize
32243 independent
32244 nohost
32245 seq */
32247 static tree
32248 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
32249 enum omp_clause_code code,
32250 tree list, location_t location)
32252 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32253 tree c = build_omp_clause (location, code);
32254 OMP_CLAUSE_CHAIN (c) = list;
32255 return c;
32258 /* OpenACC:
32259 num_gangs ( expression )
32260 num_workers ( expression )
32261 vector_length ( expression ) */
32263 static tree
32264 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32265 const char *str, tree list)
32267 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32269 matching_parens parens;
32270 if (!parens.require_open (parser))
32271 return list;
32273 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32275 if (t == error_mark_node
32276 || !parens.require_close (parser))
32278 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32279 /*or_comma=*/false,
32280 /*consume_paren=*/true);
32281 return list;
32284 check_no_duplicate_clause (list, code, str, loc);
32286 tree c = build_omp_clause (loc, code);
32287 OMP_CLAUSE_OPERAND (c, 0) = t;
32288 OMP_CLAUSE_CHAIN (c) = list;
32289 return c;
32292 /* OpenACC:
32294 gang [( gang-arg-list )]
32295 worker [( [num:] int-expr )]
32296 vector [( [length:] int-expr )]
32298 where gang-arg is one of:
32300 [num:] int-expr
32301 static: size-expr
32303 and size-expr may be:
32306 int-expr
32309 static tree
32310 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
32311 const char *str, tree list)
32313 const char *id = "num";
32314 cp_lexer *lexer = parser->lexer;
32315 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32316 location_t loc = cp_lexer_peek_token (lexer)->location;
32318 if (kind == OMP_CLAUSE_VECTOR)
32319 id = "length";
32321 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32323 matching_parens parens;
32324 parens.consume_open (parser);
32328 cp_token *next = cp_lexer_peek_token (lexer);
32329 int idx = 0;
32331 /* Gang static argument. */
32332 if (kind == OMP_CLAUSE_GANG
32333 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32335 cp_lexer_consume_token (lexer);
32337 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32338 goto cleanup_error;
32340 idx = 1;
32341 if (ops[idx] != NULL)
32343 cp_parser_error (parser, "too many %<static%> arguments");
32344 goto cleanup_error;
32347 /* Check for the '*' argument. */
32348 if (cp_lexer_next_token_is (lexer, CPP_MULT)
32349 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32350 || cp_lexer_nth_token_is (parser->lexer, 2,
32351 CPP_CLOSE_PAREN)))
32353 cp_lexer_consume_token (lexer);
32354 ops[idx] = integer_minus_one_node;
32356 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32358 cp_lexer_consume_token (lexer);
32359 continue;
32361 else break;
32364 /* Worker num: argument and vector length: arguments. */
32365 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32366 && id_equal (next->u.value, id)
32367 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32369 cp_lexer_consume_token (lexer); /* id */
32370 cp_lexer_consume_token (lexer); /* ':' */
32373 /* Now collect the actual argument. */
32374 if (ops[idx] != NULL_TREE)
32376 cp_parser_error (parser, "unexpected argument");
32377 goto cleanup_error;
32380 tree expr = cp_parser_assignment_expression (parser, NULL, false,
32381 false);
32382 if (expr == error_mark_node)
32383 goto cleanup_error;
32385 mark_exp_read (expr);
32386 ops[idx] = expr;
32388 if (kind == OMP_CLAUSE_GANG
32389 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32391 cp_lexer_consume_token (lexer);
32392 continue;
32394 break;
32396 while (1);
32398 if (!parens.require_close (parser))
32399 goto cleanup_error;
32402 check_no_duplicate_clause (list, kind, str, loc);
32404 c = build_omp_clause (loc, kind);
32406 if (ops[1])
32407 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32409 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32410 OMP_CLAUSE_CHAIN (c) = list;
32412 return c;
32414 cleanup_error:
32415 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32416 return list;
32419 /* OpenACC 2.0:
32420 tile ( size-expr-list ) */
32422 static tree
32423 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32425 tree c, expr = error_mark_node;
32426 tree tile = NULL_TREE;
32428 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32429 so, but the spec authors never considered such a case and have
32430 differing opinions on what it might mean, including 'not
32431 allowed'.) */
32432 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32433 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32434 clause_loc);
32436 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32437 return list;
32441 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32442 return list;
32444 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32445 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32446 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32448 cp_lexer_consume_token (parser->lexer);
32449 expr = integer_zero_node;
32451 else
32452 expr = cp_parser_constant_expression (parser);
32454 tile = tree_cons (NULL_TREE, expr, tile);
32456 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32458 /* Consume the trailing ')'. */
32459 cp_lexer_consume_token (parser->lexer);
32461 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32462 tile = nreverse (tile);
32463 OMP_CLAUSE_TILE_LIST (c) = tile;
32464 OMP_CLAUSE_CHAIN (c) = list;
32465 return c;
32468 /* OpenACC 2.0
32469 Parse wait clause or directive parameters. */
32471 static tree
32472 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32474 vec<tree, va_gc> *args;
32475 tree t, args_tree;
32477 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32478 /*cast_p=*/false,
32479 /*allow_expansion_p=*/true,
32480 /*non_constant_p=*/NULL);
32482 if (args == NULL || args->length () == 0)
32484 cp_parser_error (parser, "expected integer expression before ')'");
32485 if (args != NULL)
32486 release_tree_vector (args);
32487 return list;
32490 args_tree = build_tree_list_vec (args);
32492 release_tree_vector (args);
32494 for (t = args_tree; t; t = TREE_CHAIN (t))
32496 tree targ = TREE_VALUE (t);
32498 if (targ != error_mark_node)
32500 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32501 error ("%<wait%> expression must be integral");
32502 else
32504 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32506 targ = mark_rvalue_use (targ);
32507 OMP_CLAUSE_DECL (c) = targ;
32508 OMP_CLAUSE_CHAIN (c) = list;
32509 list = c;
32514 return list;
32517 /* OpenACC:
32518 wait ( int-expr-list ) */
32520 static tree
32521 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32523 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32525 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32526 return list;
32528 list = cp_parser_oacc_wait_list (parser, location, list);
32530 return list;
32533 /* OpenMP 3.0:
32534 collapse ( constant-expression ) */
32536 static tree
32537 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32539 tree c, num;
32540 location_t loc;
32541 HOST_WIDE_INT n;
32543 loc = cp_lexer_peek_token (parser->lexer)->location;
32544 matching_parens parens;
32545 if (!parens.require_open (parser))
32546 return list;
32548 num = cp_parser_constant_expression (parser);
32550 if (!parens.require_close (parser))
32551 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32552 /*or_comma=*/false,
32553 /*consume_paren=*/true);
32555 if (num == error_mark_node)
32556 return list;
32557 num = fold_non_dependent_expr (num);
32558 if (!tree_fits_shwi_p (num)
32559 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32560 || (n = tree_to_shwi (num)) <= 0
32561 || (int) n != n)
32563 error_at (loc, "collapse argument needs positive constant integer expression");
32564 return list;
32567 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32568 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32569 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32570 OMP_CLAUSE_CHAIN (c) = list;
32571 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32573 return c;
32576 /* OpenMP 2.5:
32577 default ( none | shared )
32579 OpenACC:
32580 default ( none | present ) */
32582 static tree
32583 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32584 location_t location, bool is_oacc)
32586 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32587 tree c;
32589 matching_parens parens;
32590 if (!parens.require_open (parser))
32591 return list;
32592 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32594 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32595 const char *p = IDENTIFIER_POINTER (id);
32597 switch (p[0])
32599 case 'n':
32600 if (strcmp ("none", p) != 0)
32601 goto invalid_kind;
32602 kind = OMP_CLAUSE_DEFAULT_NONE;
32603 break;
32605 case 'p':
32606 if (strcmp ("present", p) != 0 || !is_oacc)
32607 goto invalid_kind;
32608 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32609 break;
32611 case 's':
32612 if (strcmp ("shared", p) != 0 || is_oacc)
32613 goto invalid_kind;
32614 kind = OMP_CLAUSE_DEFAULT_SHARED;
32615 break;
32617 default:
32618 goto invalid_kind;
32621 cp_lexer_consume_token (parser->lexer);
32623 else
32625 invalid_kind:
32626 if (is_oacc)
32627 cp_parser_error (parser, "expected %<none%> or %<present%>");
32628 else
32629 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32632 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32633 || !parens.require_close (parser))
32634 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32635 /*or_comma=*/false,
32636 /*consume_paren=*/true);
32638 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32639 return list;
32641 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32642 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32643 OMP_CLAUSE_CHAIN (c) = list;
32644 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32646 return c;
32649 /* OpenMP 3.1:
32650 final ( expression ) */
32652 static tree
32653 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32655 tree t, c;
32657 matching_parens parens;
32658 if (!parens.require_open (parser))
32659 return list;
32661 t = cp_parser_assignment_expression (parser);
32663 if (t == error_mark_node
32664 || !parens.require_close (parser))
32665 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32666 /*or_comma=*/false,
32667 /*consume_paren=*/true);
32669 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32671 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32672 OMP_CLAUSE_FINAL_EXPR (c) = t;
32673 OMP_CLAUSE_CHAIN (c) = list;
32675 return c;
32678 /* OpenMP 2.5:
32679 if ( expression )
32681 OpenMP 4.5:
32682 if ( directive-name-modifier : expression )
32684 directive-name-modifier:
32685 parallel | task | taskloop | target data | target | target update
32686 | target enter data | target exit data
32688 OpenMP 5.0:
32689 directive-name-modifier:
32690 ... | simd | cancel */
32692 static tree
32693 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32694 bool is_omp)
32696 tree t, c;
32697 enum tree_code if_modifier = ERROR_MARK;
32699 matching_parens parens;
32700 if (!parens.require_open (parser))
32701 return list;
32703 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32705 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32706 const char *p = IDENTIFIER_POINTER (id);
32707 int n = 2;
32709 if (strcmp ("cancel", p) == 0)
32710 if_modifier = VOID_CST;
32711 else if (strcmp ("parallel", p) == 0)
32712 if_modifier = OMP_PARALLEL;
32713 else if (strcmp ("simd", p) == 0)
32714 if_modifier = OMP_SIMD;
32715 else if (strcmp ("task", p) == 0)
32716 if_modifier = OMP_TASK;
32717 else if (strcmp ("taskloop", p) == 0)
32718 if_modifier = OMP_TASKLOOP;
32719 else if (strcmp ("target", p) == 0)
32721 if_modifier = OMP_TARGET;
32722 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32724 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32725 p = IDENTIFIER_POINTER (id);
32726 if (strcmp ("data", p) == 0)
32727 if_modifier = OMP_TARGET_DATA;
32728 else if (strcmp ("update", p) == 0)
32729 if_modifier = OMP_TARGET_UPDATE;
32730 else if (strcmp ("enter", p) == 0)
32731 if_modifier = OMP_TARGET_ENTER_DATA;
32732 else if (strcmp ("exit", p) == 0)
32733 if_modifier = OMP_TARGET_EXIT_DATA;
32734 if (if_modifier != OMP_TARGET)
32735 n = 3;
32736 else
32738 location_t loc
32739 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32740 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32741 "or %<exit%>");
32742 if_modifier = ERROR_MARK;
32744 if (if_modifier == OMP_TARGET_ENTER_DATA
32745 || if_modifier == OMP_TARGET_EXIT_DATA)
32747 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32749 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32750 p = IDENTIFIER_POINTER (id);
32751 if (strcmp ("data", p) == 0)
32752 n = 4;
32754 if (n != 4)
32756 location_t loc
32757 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32758 error_at (loc, "expected %<data%>");
32759 if_modifier = ERROR_MARK;
32764 if (if_modifier != ERROR_MARK)
32766 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32768 while (n-- > 0)
32769 cp_lexer_consume_token (parser->lexer);
32771 else
32773 if (n > 2)
32775 location_t loc
32776 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32777 error_at (loc, "expected %<:%>");
32779 if_modifier = ERROR_MARK;
32784 t = cp_parser_assignment_expression (parser);
32786 if (t == error_mark_node
32787 || !parens.require_close (parser))
32788 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32789 /*or_comma=*/false,
32790 /*consume_paren=*/true);
32792 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32793 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32795 if (if_modifier != ERROR_MARK
32796 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32798 const char *p = NULL;
32799 switch (if_modifier)
32801 case VOID_CST: p = "cancel"; break;
32802 case OMP_PARALLEL: p = "parallel"; break;
32803 case OMP_SIMD: p = "simd"; break;
32804 case OMP_TASK: p = "task"; break;
32805 case OMP_TASKLOOP: p = "taskloop"; break;
32806 case OMP_TARGET_DATA: p = "target data"; break;
32807 case OMP_TARGET: p = "target"; break;
32808 case OMP_TARGET_UPDATE: p = "target update"; break;
32809 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32810 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32811 default: gcc_unreachable ();
32813 error_at (location, "too many %<if%> clauses with %qs modifier",
32815 return list;
32817 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32819 if (!is_omp)
32820 error_at (location, "too many %<if%> clauses");
32821 else
32822 error_at (location, "too many %<if%> clauses without modifier");
32823 return list;
32825 else if (if_modifier == ERROR_MARK
32826 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32828 error_at (location, "if any %<if%> clause has modifier, then all "
32829 "%<if%> clauses have to use modifier");
32830 return list;
32834 c = build_omp_clause (location, OMP_CLAUSE_IF);
32835 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32836 OMP_CLAUSE_IF_EXPR (c) = t;
32837 OMP_CLAUSE_CHAIN (c) = list;
32839 return c;
32842 /* OpenMP 3.1:
32843 mergeable */
32845 static tree
32846 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32847 tree list, location_t location)
32849 tree c;
32851 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32852 location);
32854 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32855 OMP_CLAUSE_CHAIN (c) = list;
32856 return c;
32859 /* OpenMP 2.5:
32860 nowait */
32862 static tree
32863 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32864 tree list, location_t location)
32866 tree c;
32868 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32870 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32871 OMP_CLAUSE_CHAIN (c) = list;
32872 return c;
32875 /* OpenMP 2.5:
32876 num_threads ( expression ) */
32878 static tree
32879 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32880 location_t location)
32882 tree t, c;
32884 matching_parens parens;
32885 if (!parens.require_open (parser))
32886 return list;
32888 t = cp_parser_assignment_expression (parser);
32890 if (t == error_mark_node
32891 || !parens.require_close (parser))
32892 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32893 /*or_comma=*/false,
32894 /*consume_paren=*/true);
32896 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32897 "num_threads", location);
32899 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32900 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32901 OMP_CLAUSE_CHAIN (c) = list;
32903 return c;
32906 /* OpenMP 4.5:
32907 num_tasks ( expression ) */
32909 static tree
32910 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32911 location_t location)
32913 tree t, c;
32915 matching_parens parens;
32916 if (!parens.require_open (parser))
32917 return list;
32919 t = cp_parser_assignment_expression (parser);
32921 if (t == error_mark_node
32922 || !parens.require_close (parser))
32923 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32924 /*or_comma=*/false,
32925 /*consume_paren=*/true);
32927 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32928 "num_tasks", location);
32930 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32931 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32932 OMP_CLAUSE_CHAIN (c) = list;
32934 return c;
32937 /* OpenMP 4.5:
32938 grainsize ( expression ) */
32940 static tree
32941 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32942 location_t location)
32944 tree t, c;
32946 matching_parens parens;
32947 if (!parens.require_open (parser))
32948 return list;
32950 t = cp_parser_assignment_expression (parser);
32952 if (t == error_mark_node
32953 || !parens.require_close (parser))
32954 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32955 /*or_comma=*/false,
32956 /*consume_paren=*/true);
32958 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32959 "grainsize", location);
32961 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32962 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32963 OMP_CLAUSE_CHAIN (c) = list;
32965 return c;
32968 /* OpenMP 4.5:
32969 priority ( expression ) */
32971 static tree
32972 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32973 location_t location)
32975 tree t, c;
32977 matching_parens parens;
32978 if (!parens.require_open (parser))
32979 return list;
32981 t = cp_parser_assignment_expression (parser);
32983 if (t == error_mark_node
32984 || !parens.require_close (parser))
32985 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32986 /*or_comma=*/false,
32987 /*consume_paren=*/true);
32989 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32990 "priority", location);
32992 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32993 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32994 OMP_CLAUSE_CHAIN (c) = list;
32996 return c;
32999 /* OpenMP 4.5:
33000 hint ( expression ) */
33002 static tree
33003 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
33005 tree t, c;
33007 matching_parens parens;
33008 if (!parens.require_open (parser))
33009 return list;
33011 t = cp_parser_assignment_expression (parser);
33013 if (t == error_mark_node
33014 || !parens.require_close (parser))
33015 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33016 /*or_comma=*/false,
33017 /*consume_paren=*/true);
33019 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
33021 c = build_omp_clause (location, OMP_CLAUSE_HINT);
33022 OMP_CLAUSE_HINT_EXPR (c) = t;
33023 OMP_CLAUSE_CHAIN (c) = list;
33025 return c;
33028 /* OpenMP 4.5:
33029 defaultmap ( tofrom : scalar )
33031 OpenMP 5.0:
33032 defaultmap ( implicit-behavior [ : variable-category ] ) */
33034 static tree
33035 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
33036 location_t location)
33038 tree c, id;
33039 const char *p;
33040 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33041 enum omp_clause_defaultmap_kind category
33042 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
33044 matching_parens parens;
33045 if (!parens.require_open (parser))
33046 return list;
33048 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
33049 p = "default";
33050 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33052 invalid_behavior:
33053 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
33054 "%<tofrom%>, %<firstprivate%>, %<none%> "
33055 "or %<default%>");
33056 goto out_err;
33058 else
33060 id = cp_lexer_peek_token (parser->lexer)->u.value;
33061 p = IDENTIFIER_POINTER (id);
33064 switch (p[0])
33066 case 'a':
33067 if (strcmp ("alloc", p) == 0)
33068 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
33069 else
33070 goto invalid_behavior;
33071 break;
33073 case 'd':
33074 if (strcmp ("default", p) == 0)
33075 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33076 else
33077 goto invalid_behavior;
33078 break;
33080 case 'f':
33081 if (strcmp ("firstprivate", p) == 0)
33082 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
33083 else if (strcmp ("from", p) == 0)
33084 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
33085 else
33086 goto invalid_behavior;
33087 break;
33089 case 'n':
33090 if (strcmp ("none", p) == 0)
33091 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
33092 else
33093 goto invalid_behavior;
33094 break;
33096 case 't':
33097 if (strcmp ("tofrom", p) == 0)
33098 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
33099 else if (strcmp ("to", p) == 0)
33100 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
33101 else
33102 goto invalid_behavior;
33103 break;
33105 default:
33106 goto invalid_behavior;
33108 cp_lexer_consume_token (parser->lexer);
33110 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33112 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33113 goto out_err;
33115 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33117 invalid_category:
33118 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
33119 "%<pointer%>");
33120 goto out_err;
33122 id = cp_lexer_peek_token (parser->lexer)->u.value;
33123 p = IDENTIFIER_POINTER (id);
33125 switch (p[0])
33127 case 'a':
33128 if (strcmp ("aggregate", p) == 0)
33129 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
33130 else
33131 goto invalid_category;
33132 break;
33134 case 'p':
33135 if (strcmp ("pointer", p) == 0)
33136 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
33137 else
33138 goto invalid_category;
33139 break;
33141 case 's':
33142 if (strcmp ("scalar", p) == 0)
33143 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
33144 else
33145 goto invalid_category;
33146 break;
33148 default:
33149 goto invalid_category;
33152 cp_lexer_consume_token (parser->lexer);
33154 if (!parens.require_close (parser))
33155 goto out_err;
33157 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33158 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
33159 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33160 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
33161 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
33162 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
33164 enum omp_clause_defaultmap_kind cat = category;
33165 location_t loc = OMP_CLAUSE_LOCATION (c);
33166 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
33167 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
33168 p = NULL;
33169 switch (cat)
33171 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
33172 p = NULL;
33173 break;
33174 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
33175 p = "aggregate";
33176 break;
33177 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
33178 p = "pointer";
33179 break;
33180 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
33181 p = "scalar";
33182 break;
33183 default:
33184 gcc_unreachable ();
33186 if (p)
33187 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33189 else
33190 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33191 "category");
33192 break;
33195 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33196 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33197 OMP_CLAUSE_CHAIN (c) = list;
33198 return c;
33200 out_err:
33201 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33202 /*or_comma=*/false,
33203 /*consume_paren=*/true);
33204 return list;
33207 /* OpenMP 2.5:
33208 ordered
33210 OpenMP 4.5:
33211 ordered ( constant-expression ) */
33213 static tree
33214 cp_parser_omp_clause_ordered (cp_parser *parser,
33215 tree list, location_t location)
33217 tree c, num = NULL_TREE;
33218 HOST_WIDE_INT n;
33220 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33221 "ordered", location);
33223 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33225 matching_parens parens;
33226 parens.consume_open (parser);
33228 num = cp_parser_constant_expression (parser);
33230 if (!parens.require_close (parser))
33231 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33232 /*or_comma=*/false,
33233 /*consume_paren=*/true);
33235 if (num == error_mark_node)
33236 return list;
33237 num = fold_non_dependent_expr (num);
33238 if (!tree_fits_shwi_p (num)
33239 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33240 || (n = tree_to_shwi (num)) <= 0
33241 || (int) n != n)
33243 error_at (location,
33244 "ordered argument needs positive constant integer "
33245 "expression");
33246 return list;
33250 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
33251 OMP_CLAUSE_ORDERED_EXPR (c) = num;
33252 OMP_CLAUSE_CHAIN (c) = list;
33253 return c;
33256 /* OpenMP 2.5:
33257 reduction ( reduction-operator : variable-list )
33259 reduction-operator:
33260 One of: + * - & ^ | && ||
33262 OpenMP 3.1:
33264 reduction-operator:
33265 One of: + * - & ^ | && || min max
33267 OpenMP 4.0:
33269 reduction-operator:
33270 One of: + * - & ^ | && ||
33271 id-expression
33273 OpenMP 5.0:
33274 reduction ( reduction-modifier, reduction-operator : variable-list )
33275 in_reduction ( reduction-operator : variable-list )
33276 task_reduction ( reduction-operator : variable-list ) */
33278 static tree
33279 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
33280 bool is_omp, tree list)
33282 enum tree_code code = ERROR_MARK;
33283 tree nlist, c, id = NULL_TREE;
33284 bool task = false;
33285 bool inscan = false;
33287 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33288 return list;
33290 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
33292 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
33293 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33295 cp_lexer_consume_token (parser->lexer);
33296 cp_lexer_consume_token (parser->lexer);
33298 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33299 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33301 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33302 const char *p = IDENTIFIER_POINTER (id);
33303 if (strcmp (p, "task") == 0)
33304 task = true;
33305 else if (strcmp (p, "inscan") == 0)
33307 inscan = true;
33308 sorry ("%<inscan%> modifier on %<reduction%> clause "
33309 "not supported yet");
33311 if (task || inscan)
33313 cp_lexer_consume_token (parser->lexer);
33314 cp_lexer_consume_token (parser->lexer);
33319 switch (cp_lexer_peek_token (parser->lexer)->type)
33321 case CPP_PLUS: code = PLUS_EXPR; break;
33322 case CPP_MULT: code = MULT_EXPR; break;
33323 case CPP_MINUS: code = MINUS_EXPR; break;
33324 case CPP_AND: code = BIT_AND_EXPR; break;
33325 case CPP_XOR: code = BIT_XOR_EXPR; break;
33326 case CPP_OR: code = BIT_IOR_EXPR; break;
33327 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
33328 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
33329 default: break;
33332 if (code != ERROR_MARK)
33333 cp_lexer_consume_token (parser->lexer);
33334 else
33336 bool saved_colon_corrects_to_scope_p;
33337 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33338 parser->colon_corrects_to_scope_p = false;
33339 id = cp_parser_id_expression (parser, /*template_p=*/false,
33340 /*check_dependency_p=*/true,
33341 /*template_p=*/NULL,
33342 /*declarator_p=*/false,
33343 /*optional_p=*/false);
33344 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33345 if (identifier_p (id))
33347 const char *p = IDENTIFIER_POINTER (id);
33349 if (strcmp (p, "min") == 0)
33350 code = MIN_EXPR;
33351 else if (strcmp (p, "max") == 0)
33352 code = MAX_EXPR;
33353 else if (id == ovl_op_identifier (false, PLUS_EXPR))
33354 code = PLUS_EXPR;
33355 else if (id == ovl_op_identifier (false, MULT_EXPR))
33356 code = MULT_EXPR;
33357 else if (id == ovl_op_identifier (false, MINUS_EXPR))
33358 code = MINUS_EXPR;
33359 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
33360 code = BIT_AND_EXPR;
33361 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
33362 code = BIT_IOR_EXPR;
33363 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
33364 code = BIT_XOR_EXPR;
33365 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
33366 code = TRUTH_ANDIF_EXPR;
33367 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
33368 code = TRUTH_ORIF_EXPR;
33369 id = omp_reduction_id (code, id, NULL_TREE);
33370 tree scope = parser->scope;
33371 if (scope)
33372 id = build_qualified_name (NULL_TREE, scope, id, false);
33373 parser->scope = NULL_TREE;
33374 parser->qualifying_scope = NULL_TREE;
33375 parser->object_scope = NULL_TREE;
33377 else
33379 error ("invalid reduction-identifier");
33380 resync_fail:
33381 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33382 /*or_comma=*/false,
33383 /*consume_paren=*/true);
33384 return list;
33388 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33389 goto resync_fail;
33391 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
33392 NULL);
33393 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33395 OMP_CLAUSE_REDUCTION_CODE (c) = code;
33396 if (task)
33397 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
33398 else if (inscan)
33399 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
33400 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
33403 return nlist;
33406 /* OpenMP 2.5:
33407 schedule ( schedule-kind )
33408 schedule ( schedule-kind , expression )
33410 schedule-kind:
33411 static | dynamic | guided | runtime | auto
33413 OpenMP 4.5:
33414 schedule ( schedule-modifier : schedule-kind )
33415 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33417 schedule-modifier:
33418 simd
33419 monotonic
33420 nonmonotonic */
33422 static tree
33423 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
33425 tree c, t;
33426 int modifiers = 0, nmodifiers = 0;
33428 matching_parens parens;
33429 if (!parens.require_open (parser))
33430 return list;
33432 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
33434 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33436 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33437 const char *p = IDENTIFIER_POINTER (id);
33438 if (strcmp ("simd", p) == 0)
33439 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
33440 else if (strcmp ("monotonic", p) == 0)
33441 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
33442 else if (strcmp ("nonmonotonic", p) == 0)
33443 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
33444 else
33445 break;
33446 cp_lexer_consume_token (parser->lexer);
33447 if (nmodifiers++ == 0
33448 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33449 cp_lexer_consume_token (parser->lexer);
33450 else
33452 cp_parser_require (parser, CPP_COLON, RT_COLON);
33453 break;
33457 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33459 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33460 const char *p = IDENTIFIER_POINTER (id);
33462 switch (p[0])
33464 case 'd':
33465 if (strcmp ("dynamic", p) != 0)
33466 goto invalid_kind;
33467 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
33468 break;
33470 case 'g':
33471 if (strcmp ("guided", p) != 0)
33472 goto invalid_kind;
33473 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
33474 break;
33476 case 'r':
33477 if (strcmp ("runtime", p) != 0)
33478 goto invalid_kind;
33479 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
33480 break;
33482 default:
33483 goto invalid_kind;
33486 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33487 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33488 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33489 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33490 else
33491 goto invalid_kind;
33492 cp_lexer_consume_token (parser->lexer);
33494 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33495 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33496 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33497 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33499 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33500 "specified");
33501 modifiers = 0;
33504 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33506 cp_token *token;
33507 cp_lexer_consume_token (parser->lexer);
33509 token = cp_lexer_peek_token (parser->lexer);
33510 t = cp_parser_assignment_expression (parser);
33512 if (t == error_mark_node)
33513 goto resync_fail;
33514 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
33515 error_at (token->location, "schedule %<runtime%> does not take "
33516 "a %<chunk_size%> parameter");
33517 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
33518 error_at (token->location, "schedule %<auto%> does not take "
33519 "a %<chunk_size%> parameter");
33520 else
33521 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
33523 if (!parens.require_close (parser))
33524 goto resync_fail;
33526 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33527 goto resync_fail;
33529 OMP_CLAUSE_SCHEDULE_KIND (c)
33530 = (enum omp_clause_schedule_kind)
33531 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
33533 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
33534 OMP_CLAUSE_CHAIN (c) = list;
33535 return c;
33537 invalid_kind:
33538 cp_parser_error (parser, "invalid schedule kind");
33539 resync_fail:
33540 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33541 /*or_comma=*/false,
33542 /*consume_paren=*/true);
33543 return list;
33546 /* OpenMP 3.0:
33547 untied */
33549 static tree
33550 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
33551 tree list, location_t location)
33553 tree c;
33555 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
33557 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
33558 OMP_CLAUSE_CHAIN (c) = list;
33559 return c;
33562 /* OpenMP 4.0:
33563 inbranch
33564 notinbranch */
33566 static tree
33567 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33568 tree list, location_t location)
33570 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33571 tree c = build_omp_clause (location, code);
33572 OMP_CLAUSE_CHAIN (c) = list;
33573 return c;
33576 /* OpenMP 4.0:
33577 parallel
33579 sections
33580 taskgroup */
33582 static tree
33583 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33584 enum omp_clause_code code,
33585 tree list, location_t location)
33587 tree c = build_omp_clause (location, code);
33588 OMP_CLAUSE_CHAIN (c) = list;
33589 return c;
33592 /* OpenMP 4.5:
33593 nogroup */
33595 static tree
33596 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33597 tree list, location_t location)
33599 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33600 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33601 OMP_CLAUSE_CHAIN (c) = list;
33602 return c;
33605 /* OpenMP 4.5:
33606 simd
33607 threads */
33609 static tree
33610 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33611 enum omp_clause_code code,
33612 tree list, location_t location)
33614 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33615 tree c = build_omp_clause (location, code);
33616 OMP_CLAUSE_CHAIN (c) = list;
33617 return c;
33620 /* OpenMP 4.0:
33621 num_teams ( expression ) */
33623 static tree
33624 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33625 location_t location)
33627 tree t, c;
33629 matching_parens parens;
33630 if (!parens.require_open (parser))
33631 return list;
33633 t = cp_parser_assignment_expression (parser);
33635 if (t == error_mark_node
33636 || !parens.require_close (parser))
33637 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33638 /*or_comma=*/false,
33639 /*consume_paren=*/true);
33641 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33642 "num_teams", location);
33644 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33645 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33646 OMP_CLAUSE_CHAIN (c) = list;
33648 return c;
33651 /* OpenMP 4.0:
33652 thread_limit ( expression ) */
33654 static tree
33655 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33656 location_t location)
33658 tree t, c;
33660 matching_parens parens;
33661 if (!parens.require_open (parser))
33662 return list;
33664 t = cp_parser_assignment_expression (parser);
33666 if (t == error_mark_node
33667 || !parens.require_close (parser))
33668 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33669 /*or_comma=*/false,
33670 /*consume_paren=*/true);
33672 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33673 "thread_limit", location);
33675 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33676 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33677 OMP_CLAUSE_CHAIN (c) = list;
33679 return c;
33682 /* OpenMP 4.0:
33683 aligned ( variable-list )
33684 aligned ( variable-list : constant-expression ) */
33686 static tree
33687 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33689 tree nlist, c, alignment = NULL_TREE;
33690 bool colon;
33692 matching_parens parens;
33693 if (!parens.require_open (parser))
33694 return list;
33696 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33697 &colon);
33699 if (colon)
33701 alignment = cp_parser_constant_expression (parser);
33703 if (!parens.require_close (parser))
33704 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33705 /*or_comma=*/false,
33706 /*consume_paren=*/true);
33708 if (alignment == error_mark_node)
33709 alignment = NULL_TREE;
33712 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33713 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33715 return nlist;
33718 /* OpenMP 2.5:
33719 lastprivate ( variable-list )
33721 OpenMP 5.0:
33722 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
33724 static tree
33725 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
33727 bool conditional = false;
33729 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33730 return list;
33732 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33733 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
33735 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33736 const char *p = IDENTIFIER_POINTER (id);
33738 if (strcmp ("conditional", p) == 0)
33740 conditional = true;
33741 cp_lexer_consume_token (parser->lexer);
33742 cp_lexer_consume_token (parser->lexer);
33746 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
33747 list, NULL);
33749 if (conditional)
33750 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33751 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
33752 return nlist;
33755 /* OpenMP 4.0:
33756 linear ( variable-list )
33757 linear ( variable-list : expression )
33759 OpenMP 4.5:
33760 linear ( modifier ( variable-list ) )
33761 linear ( modifier ( variable-list ) : expression ) */
33763 static tree
33764 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33765 bool declare_simd)
33767 tree nlist, c, step = integer_one_node;
33768 bool colon;
33769 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33771 matching_parens parens;
33772 if (!parens.require_open (parser))
33773 return list;
33775 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33777 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33778 const char *p = IDENTIFIER_POINTER (id);
33780 if (strcmp ("ref", p) == 0)
33781 kind = OMP_CLAUSE_LINEAR_REF;
33782 else if (strcmp ("val", p) == 0)
33783 kind = OMP_CLAUSE_LINEAR_VAL;
33784 else if (strcmp ("uval", p) == 0)
33785 kind = OMP_CLAUSE_LINEAR_UVAL;
33786 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33787 cp_lexer_consume_token (parser->lexer);
33788 else
33789 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33792 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33793 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33794 &colon);
33795 else
33797 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33798 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33799 if (colon)
33800 cp_parser_require (parser, CPP_COLON, RT_COLON);
33801 else if (!parens.require_close (parser))
33802 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33803 /*or_comma=*/false,
33804 /*consume_paren=*/true);
33807 if (colon)
33809 step = NULL_TREE;
33810 if (declare_simd
33811 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33812 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33814 cp_token *token = cp_lexer_peek_token (parser->lexer);
33815 cp_parser_parse_tentatively (parser);
33816 step = cp_parser_id_expression (parser, /*template_p=*/false,
33817 /*check_dependency_p=*/true,
33818 /*template_p=*/NULL,
33819 /*declarator_p=*/false,
33820 /*optional_p=*/false);
33821 if (step != error_mark_node)
33822 step = cp_parser_lookup_name_simple (parser, step, token->location);
33823 if (step == error_mark_node)
33825 step = NULL_TREE;
33826 cp_parser_abort_tentative_parse (parser);
33828 else if (!cp_parser_parse_definitely (parser))
33829 step = NULL_TREE;
33831 if (!step)
33832 step = cp_parser_assignment_expression (parser);
33834 if (!parens.require_close (parser))
33835 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33836 /*or_comma=*/false,
33837 /*consume_paren=*/true);
33839 if (step == error_mark_node)
33840 return list;
33843 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33845 OMP_CLAUSE_LINEAR_STEP (c) = step;
33846 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33849 return nlist;
33852 /* OpenMP 4.0:
33853 safelen ( constant-expression ) */
33855 static tree
33856 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33857 location_t location)
33859 tree t, c;
33861 matching_parens parens;
33862 if (!parens.require_open (parser))
33863 return list;
33865 t = cp_parser_constant_expression (parser);
33867 if (t == error_mark_node
33868 || !parens.require_close (parser))
33869 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33870 /*or_comma=*/false,
33871 /*consume_paren=*/true);
33873 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33875 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33876 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33877 OMP_CLAUSE_CHAIN (c) = list;
33879 return c;
33882 /* OpenMP 4.0:
33883 simdlen ( constant-expression ) */
33885 static tree
33886 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33887 location_t location)
33889 tree t, c;
33891 matching_parens parens;
33892 if (!parens.require_open (parser))
33893 return list;
33895 t = cp_parser_constant_expression (parser);
33897 if (t == error_mark_node
33898 || !parens.require_close (parser))
33899 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33900 /*or_comma=*/false,
33901 /*consume_paren=*/true);
33903 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33905 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33906 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33907 OMP_CLAUSE_CHAIN (c) = list;
33909 return c;
33912 /* OpenMP 4.5:
33913 vec:
33914 identifier [+/- integer]
33915 vec , identifier [+/- integer]
33918 static tree
33919 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33920 tree list)
33922 tree vec = NULL;
33924 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33926 cp_parser_error (parser, "expected identifier");
33927 return list;
33930 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33932 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33933 tree t, identifier = cp_parser_identifier (parser);
33934 tree addend = NULL;
33936 if (identifier == error_mark_node)
33937 t = error_mark_node;
33938 else
33940 t = cp_parser_lookup_name_simple
33941 (parser, identifier,
33942 cp_lexer_peek_token (parser->lexer)->location);
33943 if (t == error_mark_node)
33944 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33945 id_loc);
33948 bool neg = false;
33949 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33950 neg = true;
33951 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33953 addend = integer_zero_node;
33954 goto add_to_vector;
33956 cp_lexer_consume_token (parser->lexer);
33958 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33960 cp_parser_error (parser, "expected integer");
33961 return list;
33964 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33965 if (TREE_CODE (addend) != INTEGER_CST)
33967 cp_parser_error (parser, "expected integer");
33968 return list;
33970 cp_lexer_consume_token (parser->lexer);
33972 add_to_vector:
33973 if (t != error_mark_node)
33975 vec = tree_cons (addend, t, vec);
33976 if (neg)
33977 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33980 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33981 break;
33983 cp_lexer_consume_token (parser->lexer);
33986 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33988 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33989 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33990 OMP_CLAUSE_DECL (u) = nreverse (vec);
33991 OMP_CLAUSE_CHAIN (u) = list;
33992 return u;
33994 return list;
33997 /* OpenMP 5.0:
33998 iterators ( iterators-definition )
34000 iterators-definition:
34001 iterator-specifier
34002 iterator-specifier , iterators-definition
34004 iterator-specifier:
34005 identifier = range-specification
34006 iterator-type identifier = range-specification
34008 range-specification:
34009 begin : end
34010 begin : end : step */
34012 static tree
34013 cp_parser_omp_iterators (cp_parser *parser)
34015 tree ret = NULL_TREE, *last = &ret;
34016 cp_lexer_consume_token (parser->lexer);
34018 matching_parens parens;
34019 if (!parens.require_open (parser))
34020 return error_mark_node;
34022 bool saved_colon_corrects_to_scope_p
34023 = parser->colon_corrects_to_scope_p;
34024 bool saved_colon_doesnt_start_class_def_p
34025 = parser->colon_doesnt_start_class_def_p;
34029 tree iter_type;
34030 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34031 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
34032 iter_type = integer_type_node;
34033 else
34035 const char *saved_message
34036 = parser->type_definition_forbidden_message;
34037 parser->type_definition_forbidden_message
34038 = G_("types may not be defined in iterator type");
34040 iter_type = cp_parser_type_id (parser);
34042 parser->type_definition_forbidden_message = saved_message;
34045 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34046 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34048 cp_parser_error (parser, "expected identifier");
34049 break;
34052 tree id = cp_parser_identifier (parser);
34053 if (id == error_mark_node)
34054 break;
34056 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34057 break;
34059 parser->colon_corrects_to_scope_p = false;
34060 parser->colon_doesnt_start_class_def_p = true;
34061 tree begin = cp_parser_assignment_expression (parser);
34063 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34064 break;
34066 tree end = cp_parser_assignment_expression (parser);
34068 tree step = integer_one_node;
34069 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34071 cp_lexer_consume_token (parser->lexer);
34072 step = cp_parser_assignment_expression (parser);
34075 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
34076 DECL_ARTIFICIAL (iter_var) = 1;
34077 DECL_CONTEXT (iter_var) = current_function_decl;
34078 pushdecl (iter_var);
34080 *last = make_tree_vec (6);
34081 TREE_VEC_ELT (*last, 0) = iter_var;
34082 TREE_VEC_ELT (*last, 1) = begin;
34083 TREE_VEC_ELT (*last, 2) = end;
34084 TREE_VEC_ELT (*last, 3) = step;
34085 last = &TREE_CHAIN (*last);
34087 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34089 cp_lexer_consume_token (parser->lexer);
34090 continue;
34092 break;
34094 while (1);
34096 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34097 parser->colon_doesnt_start_class_def_p
34098 = saved_colon_doesnt_start_class_def_p;
34100 if (!parens.require_close (parser))
34101 cp_parser_skip_to_closing_parenthesis (parser,
34102 /*recovering=*/true,
34103 /*or_comma=*/false,
34104 /*consume_paren=*/true);
34106 return ret ? ret : error_mark_node;
34109 /* OpenMP 4.0:
34110 depend ( depend-kind : variable-list )
34112 depend-kind:
34113 in | out | inout
34115 OpenMP 4.5:
34116 depend ( source )
34118 depend ( sink : vec )
34120 OpenMP 5.0:
34121 depend ( depend-modifier , depend-kind: variable-list )
34123 depend-kind:
34124 in | out | inout | mutexinoutset | depobj
34126 depend-modifier:
34127 iterator ( iterators-definition ) */
34129 static tree
34130 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
34132 tree nlist, c, iterators = NULL_TREE;
34133 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
34135 matching_parens parens;
34136 if (!parens.require_open (parser))
34137 return list;
34141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34142 goto invalid_kind;
34144 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34145 const char *p = IDENTIFIER_POINTER (id);
34147 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
34149 begin_scope (sk_omp, NULL);
34150 iterators = cp_parser_omp_iterators (parser);
34151 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
34152 continue;
34154 if (strcmp ("in", p) == 0)
34155 kind = OMP_CLAUSE_DEPEND_IN;
34156 else if (strcmp ("inout", p) == 0)
34157 kind = OMP_CLAUSE_DEPEND_INOUT;
34158 else if (strcmp ("mutexinoutset", p) == 0)
34159 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
34160 else if (strcmp ("out", p) == 0)
34161 kind = OMP_CLAUSE_DEPEND_OUT;
34162 else if (strcmp ("depobj", p) == 0)
34163 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
34164 else if (strcmp ("sink", p) == 0)
34165 kind = OMP_CLAUSE_DEPEND_SINK;
34166 else if (strcmp ("source", p) == 0)
34167 kind = OMP_CLAUSE_DEPEND_SOURCE;
34168 else
34169 goto invalid_kind;
34170 break;
34172 while (1);
34174 cp_lexer_consume_token (parser->lexer);
34176 if (iterators
34177 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
34179 poplevel (0, 1, 0);
34180 error_at (loc, "%<iterator%> modifier incompatible with %qs",
34181 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
34182 iterators = NULL_TREE;
34185 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
34187 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
34188 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34189 OMP_CLAUSE_DECL (c) = NULL_TREE;
34190 OMP_CLAUSE_CHAIN (c) = list;
34191 if (!parens.require_close (parser))
34192 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34193 /*or_comma=*/false,
34194 /*consume_paren=*/true);
34195 return c;
34198 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34199 goto resync_fail;
34201 if (kind == OMP_CLAUSE_DEPEND_SINK)
34202 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34203 else
34205 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34206 list, NULL);
34208 if (iterators)
34210 tree block = poplevel (1, 1, 0);
34211 if (iterators == error_mark_node)
34212 iterators = NULL_TREE;
34213 else
34214 TREE_VEC_ELT (iterators, 5) = block;
34217 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34219 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34220 if (iterators)
34221 OMP_CLAUSE_DECL (c)
34222 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34225 return nlist;
34227 invalid_kind:
34228 cp_parser_error (parser, "invalid depend kind");
34229 resync_fail:
34230 if (iterators)
34231 poplevel (0, 1, 0);
34232 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34233 /*or_comma=*/false,
34234 /*consume_paren=*/true);
34235 return list;
34238 /* OpenMP 4.0:
34239 map ( map-kind : variable-list )
34240 map ( variable-list )
34242 map-kind:
34243 alloc | to | from | tofrom
34245 OpenMP 4.5:
34246 map-kind:
34247 alloc | to | from | tofrom | release | delete
34249 map ( always [,] map-kind: variable-list ) */
34251 static tree
34252 cp_parser_omp_clause_map (cp_parser *parser, tree list)
34254 tree nlist, c;
34255 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
34256 bool always = false;
34258 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34259 return list;
34261 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34263 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34264 const char *p = IDENTIFIER_POINTER (id);
34266 if (strcmp ("always", p) == 0)
34268 int nth = 2;
34269 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
34270 nth++;
34271 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
34272 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
34273 == RID_DELETE))
34274 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
34275 == CPP_COLON))
34277 always = true;
34278 cp_lexer_consume_token (parser->lexer);
34279 if (nth == 3)
34280 cp_lexer_consume_token (parser->lexer);
34285 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34286 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34288 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34289 const char *p = IDENTIFIER_POINTER (id);
34291 if (strcmp ("alloc", p) == 0)
34292 kind = GOMP_MAP_ALLOC;
34293 else if (strcmp ("to", p) == 0)
34294 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
34295 else if (strcmp ("from", p) == 0)
34296 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
34297 else if (strcmp ("tofrom", p) == 0)
34298 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
34299 else if (strcmp ("release", p) == 0)
34300 kind = GOMP_MAP_RELEASE;
34301 else
34303 cp_parser_error (parser, "invalid map kind");
34304 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34305 /*or_comma=*/false,
34306 /*consume_paren=*/true);
34307 return list;
34309 cp_lexer_consume_token (parser->lexer);
34310 cp_lexer_consume_token (parser->lexer);
34312 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
34313 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34315 kind = GOMP_MAP_DELETE;
34316 cp_lexer_consume_token (parser->lexer);
34317 cp_lexer_consume_token (parser->lexer);
34320 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
34321 NULL);
34323 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34324 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34326 return nlist;
34329 /* OpenMP 4.0:
34330 device ( expression ) */
34332 static tree
34333 cp_parser_omp_clause_device (cp_parser *parser, tree list,
34334 location_t location)
34336 tree t, c;
34338 matching_parens parens;
34339 if (!parens.require_open (parser))
34340 return list;
34342 t = cp_parser_assignment_expression (parser);
34344 if (t == error_mark_node
34345 || !parens.require_close (parser))
34346 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34347 /*or_comma=*/false,
34348 /*consume_paren=*/true);
34350 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
34351 "device", location);
34353 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
34354 OMP_CLAUSE_DEVICE_ID (c) = t;
34355 OMP_CLAUSE_CHAIN (c) = list;
34357 return c;
34360 /* OpenMP 4.0:
34361 dist_schedule ( static )
34362 dist_schedule ( static , expression ) */
34364 static tree
34365 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
34366 location_t location)
34368 tree c, t;
34370 matching_parens parens;
34371 if (!parens.require_open (parser))
34372 return list;
34374 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
34376 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34377 goto invalid_kind;
34378 cp_lexer_consume_token (parser->lexer);
34380 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34382 cp_lexer_consume_token (parser->lexer);
34384 t = cp_parser_assignment_expression (parser);
34386 if (t == error_mark_node)
34387 goto resync_fail;
34388 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
34390 if (!parens.require_close (parser))
34391 goto resync_fail;
34393 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34394 goto resync_fail;
34396 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
34397 location);
34398 OMP_CLAUSE_CHAIN (c) = list;
34399 return c;
34401 invalid_kind:
34402 cp_parser_error (parser, "invalid dist_schedule kind");
34403 resync_fail:
34404 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34405 /*or_comma=*/false,
34406 /*consume_paren=*/true);
34407 return list;
34410 /* OpenMP 4.0:
34411 proc_bind ( proc-bind-kind )
34413 proc-bind-kind:
34414 master | close | spread */
34416 static tree
34417 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
34418 location_t location)
34420 tree c;
34421 enum omp_clause_proc_bind_kind kind;
34423 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34424 return list;
34426 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34428 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34429 const char *p = IDENTIFIER_POINTER (id);
34431 if (strcmp ("master", p) == 0)
34432 kind = OMP_CLAUSE_PROC_BIND_MASTER;
34433 else if (strcmp ("close", p) == 0)
34434 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
34435 else if (strcmp ("spread", p) == 0)
34436 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
34437 else
34438 goto invalid_kind;
34440 else
34441 goto invalid_kind;
34443 cp_lexer_consume_token (parser->lexer);
34444 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34445 goto resync_fail;
34447 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
34448 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
34449 location);
34450 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
34451 OMP_CLAUSE_CHAIN (c) = list;
34452 return c;
34454 invalid_kind:
34455 cp_parser_error (parser, "invalid depend kind");
34456 resync_fail:
34457 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34458 /*or_comma=*/false,
34459 /*consume_paren=*/true);
34460 return list;
34463 /* OpenACC:
34464 async [( int-expr )] */
34466 static tree
34467 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
34469 tree c, t;
34470 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34472 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34474 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34476 matching_parens parens;
34477 parens.consume_open (parser);
34479 t = cp_parser_expression (parser);
34480 if (t == error_mark_node
34481 || !parens.require_close (parser))
34482 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34483 /*or_comma=*/false,
34484 /*consume_paren=*/true);
34487 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
34489 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
34490 OMP_CLAUSE_ASYNC_EXPR (c) = t;
34491 OMP_CLAUSE_CHAIN (c) = list;
34492 list = c;
34494 return list;
34497 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34498 is a bitmask in MASK. Return the list of clauses found. */
34500 static tree
34501 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
34502 const char *where, cp_token *pragma_tok,
34503 bool finish_p = true)
34505 tree clauses = NULL;
34506 bool first = true;
34508 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34510 location_t here;
34511 pragma_omp_clause c_kind;
34512 omp_clause_code code;
34513 const char *c_name;
34514 tree prev = clauses;
34516 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34517 cp_lexer_consume_token (parser->lexer);
34519 here = cp_lexer_peek_token (parser->lexer)->location;
34520 c_kind = cp_parser_omp_clause_name (parser);
34522 switch (c_kind)
34524 case PRAGMA_OACC_CLAUSE_ASYNC:
34525 clauses = cp_parser_oacc_clause_async (parser, clauses);
34526 c_name = "async";
34527 break;
34528 case PRAGMA_OACC_CLAUSE_AUTO:
34529 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
34530 clauses, here);
34531 c_name = "auto";
34532 break;
34533 case PRAGMA_OACC_CLAUSE_COLLAPSE:
34534 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
34535 c_name = "collapse";
34536 break;
34537 case PRAGMA_OACC_CLAUSE_COPY:
34538 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34539 c_name = "copy";
34540 break;
34541 case PRAGMA_OACC_CLAUSE_COPYIN:
34542 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34543 c_name = "copyin";
34544 break;
34545 case PRAGMA_OACC_CLAUSE_COPYOUT:
34546 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34547 c_name = "copyout";
34548 break;
34549 case PRAGMA_OACC_CLAUSE_CREATE:
34550 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34551 c_name = "create";
34552 break;
34553 case PRAGMA_OACC_CLAUSE_DELETE:
34554 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34555 c_name = "delete";
34556 break;
34557 case PRAGMA_OMP_CLAUSE_DEFAULT:
34558 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
34559 c_name = "default";
34560 break;
34561 case PRAGMA_OACC_CLAUSE_DEVICE:
34562 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34563 c_name = "device";
34564 break;
34565 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
34566 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
34567 c_name = "deviceptr";
34568 break;
34569 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
34570 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34571 c_name = "device_resident";
34572 break;
34573 case PRAGMA_OACC_CLAUSE_FINALIZE:
34574 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
34575 clauses, here);
34576 c_name = "finalize";
34577 break;
34578 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
34579 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34580 clauses);
34581 c_name = "firstprivate";
34582 break;
34583 case PRAGMA_OACC_CLAUSE_GANG:
34584 c_name = "gang";
34585 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
34586 c_name, clauses);
34587 break;
34588 case PRAGMA_OACC_CLAUSE_HOST:
34589 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34590 c_name = "host";
34591 break;
34592 case PRAGMA_OACC_CLAUSE_IF:
34593 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
34594 c_name = "if";
34595 break;
34596 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
34597 clauses = cp_parser_oacc_simple_clause (parser,
34598 OMP_CLAUSE_IF_PRESENT,
34599 clauses, here);
34600 c_name = "if_present";
34601 break;
34602 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
34603 clauses = cp_parser_oacc_simple_clause (parser,
34604 OMP_CLAUSE_INDEPENDENT,
34605 clauses, here);
34606 c_name = "independent";
34607 break;
34608 case PRAGMA_OACC_CLAUSE_LINK:
34609 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34610 c_name = "link";
34611 break;
34612 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
34613 code = OMP_CLAUSE_NUM_GANGS;
34614 c_name = "num_gangs";
34615 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34616 clauses);
34617 break;
34618 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
34619 c_name = "num_workers";
34620 code = OMP_CLAUSE_NUM_WORKERS;
34621 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34622 clauses);
34623 break;
34624 case PRAGMA_OACC_CLAUSE_PRESENT:
34625 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34626 c_name = "present";
34627 break;
34628 case PRAGMA_OACC_CLAUSE_PRIVATE:
34629 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34630 clauses);
34631 c_name = "private";
34632 break;
34633 case PRAGMA_OACC_CLAUSE_REDUCTION:
34634 clauses
34635 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34636 false, clauses);
34637 c_name = "reduction";
34638 break;
34639 case PRAGMA_OACC_CLAUSE_SEQ:
34640 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
34641 clauses, here);
34642 c_name = "seq";
34643 break;
34644 case PRAGMA_OACC_CLAUSE_TILE:
34645 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
34646 c_name = "tile";
34647 break;
34648 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
34649 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34650 clauses);
34651 c_name = "use_device";
34652 break;
34653 case PRAGMA_OACC_CLAUSE_VECTOR:
34654 c_name = "vector";
34655 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
34656 c_name, clauses);
34657 break;
34658 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
34659 c_name = "vector_length";
34660 code = OMP_CLAUSE_VECTOR_LENGTH;
34661 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34662 clauses);
34663 break;
34664 case PRAGMA_OACC_CLAUSE_WAIT:
34665 clauses = cp_parser_oacc_clause_wait (parser, clauses);
34666 c_name = "wait";
34667 break;
34668 case PRAGMA_OACC_CLAUSE_WORKER:
34669 c_name = "worker";
34670 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
34671 c_name, clauses);
34672 break;
34673 default:
34674 cp_parser_error (parser, "expected %<#pragma acc%> clause");
34675 goto saw_error;
34678 first = false;
34680 if (((mask >> c_kind) & 1) == 0)
34682 /* Remove the invalid clause(s) from the list to avoid
34683 confusing the rest of the compiler. */
34684 clauses = prev;
34685 error_at (here, "%qs is not valid for %qs", c_name, where);
34689 saw_error:
34690 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34692 if (finish_p)
34693 return finish_omp_clauses (clauses, C_ORT_ACC);
34695 return clauses;
34698 /* Parse all OpenMP clauses. The set clauses allowed by the directive
34699 is a bitmask in MASK. Return the list of clauses found; the result
34700 of clause default goes in *pdefault. */
34702 static tree
34703 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
34704 const char *where, cp_token *pragma_tok,
34705 bool finish_p = true)
34707 tree clauses = NULL;
34708 bool first = true;
34709 cp_token *token = NULL;
34711 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34713 pragma_omp_clause c_kind;
34714 const char *c_name;
34715 tree prev = clauses;
34717 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34718 cp_lexer_consume_token (parser->lexer);
34720 token = cp_lexer_peek_token (parser->lexer);
34721 c_kind = cp_parser_omp_clause_name (parser);
34723 switch (c_kind)
34725 case PRAGMA_OMP_CLAUSE_COLLAPSE:
34726 clauses = cp_parser_omp_clause_collapse (parser, clauses,
34727 token->location);
34728 c_name = "collapse";
34729 break;
34730 case PRAGMA_OMP_CLAUSE_COPYIN:
34731 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
34732 c_name = "copyin";
34733 break;
34734 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
34735 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
34736 clauses);
34737 c_name = "copyprivate";
34738 break;
34739 case PRAGMA_OMP_CLAUSE_DEFAULT:
34740 clauses = cp_parser_omp_clause_default (parser, clauses,
34741 token->location, false);
34742 c_name = "default";
34743 break;
34744 case PRAGMA_OMP_CLAUSE_FINAL:
34745 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
34746 c_name = "final";
34747 break;
34748 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
34749 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34750 clauses);
34751 c_name = "firstprivate";
34752 break;
34753 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
34754 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
34755 token->location);
34756 c_name = "grainsize";
34757 break;
34758 case PRAGMA_OMP_CLAUSE_HINT:
34759 clauses = cp_parser_omp_clause_hint (parser, clauses,
34760 token->location);
34761 c_name = "hint";
34762 break;
34763 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
34764 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34765 token->location);
34766 c_name = "defaultmap";
34767 break;
34768 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34769 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34770 clauses);
34771 c_name = "use_device_ptr";
34772 break;
34773 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34774 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34775 clauses);
34776 c_name = "is_device_ptr";
34777 break;
34778 case PRAGMA_OMP_CLAUSE_IF:
34779 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34780 true);
34781 c_name = "if";
34782 break;
34783 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
34784 clauses
34785 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
34786 true, clauses);
34787 c_name = "in_reduction";
34788 break;
34789 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34790 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
34791 c_name = "lastprivate";
34792 break;
34793 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34794 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34795 token->location);
34796 c_name = "mergeable";
34797 break;
34798 case PRAGMA_OMP_CLAUSE_NOWAIT:
34799 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34800 c_name = "nowait";
34801 break;
34802 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34803 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34804 token->location);
34805 c_name = "num_tasks";
34806 break;
34807 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34808 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34809 token->location);
34810 c_name = "num_threads";
34811 break;
34812 case PRAGMA_OMP_CLAUSE_ORDERED:
34813 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34814 token->location);
34815 c_name = "ordered";
34816 break;
34817 case PRAGMA_OMP_CLAUSE_PRIORITY:
34818 clauses = cp_parser_omp_clause_priority (parser, clauses,
34819 token->location);
34820 c_name = "priority";
34821 break;
34822 case PRAGMA_OMP_CLAUSE_PRIVATE:
34823 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34824 clauses);
34825 c_name = "private";
34826 break;
34827 case PRAGMA_OMP_CLAUSE_REDUCTION:
34828 clauses
34829 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34830 true, clauses);
34831 c_name = "reduction";
34832 break;
34833 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34834 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34835 token->location);
34836 c_name = "schedule";
34837 break;
34838 case PRAGMA_OMP_CLAUSE_SHARED:
34839 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34840 clauses);
34841 c_name = "shared";
34842 break;
34843 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
34844 clauses
34845 = cp_parser_omp_clause_reduction (parser,
34846 OMP_CLAUSE_TASK_REDUCTION,
34847 true, clauses);
34848 c_name = "task_reduction";
34849 break;
34850 case PRAGMA_OMP_CLAUSE_UNTIED:
34851 clauses = cp_parser_omp_clause_untied (parser, clauses,
34852 token->location);
34853 c_name = "untied";
34854 break;
34855 case PRAGMA_OMP_CLAUSE_INBRANCH:
34856 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34857 clauses, token->location);
34858 c_name = "inbranch";
34859 break;
34860 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
34861 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
34862 clauses);
34863 c_name = "nontemporal";
34864 break;
34865 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34866 clauses = cp_parser_omp_clause_branch (parser,
34867 OMP_CLAUSE_NOTINBRANCH,
34868 clauses, token->location);
34869 c_name = "notinbranch";
34870 break;
34871 case PRAGMA_OMP_CLAUSE_PARALLEL:
34872 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34873 clauses, token->location);
34874 c_name = "parallel";
34875 if (!first)
34877 clause_not_first:
34878 error_at (token->location, "%qs must be the first clause of %qs",
34879 c_name, where);
34880 clauses = prev;
34882 break;
34883 case PRAGMA_OMP_CLAUSE_FOR:
34884 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34885 clauses, token->location);
34886 c_name = "for";
34887 if (!first)
34888 goto clause_not_first;
34889 break;
34890 case PRAGMA_OMP_CLAUSE_SECTIONS:
34891 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34892 clauses, token->location);
34893 c_name = "sections";
34894 if (!first)
34895 goto clause_not_first;
34896 break;
34897 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34898 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34899 clauses, token->location);
34900 c_name = "taskgroup";
34901 if (!first)
34902 goto clause_not_first;
34903 break;
34904 case PRAGMA_OMP_CLAUSE_LINK:
34905 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34906 c_name = "to";
34907 break;
34908 case PRAGMA_OMP_CLAUSE_TO:
34909 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34910 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34911 clauses);
34912 else
34913 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34914 c_name = "to";
34915 break;
34916 case PRAGMA_OMP_CLAUSE_FROM:
34917 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34918 c_name = "from";
34919 break;
34920 case PRAGMA_OMP_CLAUSE_UNIFORM:
34921 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34922 clauses);
34923 c_name = "uniform";
34924 break;
34925 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34926 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34927 token->location);
34928 c_name = "num_teams";
34929 break;
34930 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34931 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34932 token->location);
34933 c_name = "thread_limit";
34934 break;
34935 case PRAGMA_OMP_CLAUSE_ALIGNED:
34936 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34937 c_name = "aligned";
34938 break;
34939 case PRAGMA_OMP_CLAUSE_LINEAR:
34941 bool declare_simd = false;
34942 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34943 declare_simd = true;
34944 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34946 c_name = "linear";
34947 break;
34948 case PRAGMA_OMP_CLAUSE_DEPEND:
34949 clauses = cp_parser_omp_clause_depend (parser, clauses,
34950 token->location);
34951 c_name = "depend";
34952 break;
34953 case PRAGMA_OMP_CLAUSE_MAP:
34954 clauses = cp_parser_omp_clause_map (parser, clauses);
34955 c_name = "map";
34956 break;
34957 case PRAGMA_OMP_CLAUSE_DEVICE:
34958 clauses = cp_parser_omp_clause_device (parser, clauses,
34959 token->location);
34960 c_name = "device";
34961 break;
34962 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34963 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34964 token->location);
34965 c_name = "dist_schedule";
34966 break;
34967 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34968 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34969 token->location);
34970 c_name = "proc_bind";
34971 break;
34972 case PRAGMA_OMP_CLAUSE_SAFELEN:
34973 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34974 token->location);
34975 c_name = "safelen";
34976 break;
34977 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34978 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34979 token->location);
34980 c_name = "simdlen";
34981 break;
34982 case PRAGMA_OMP_CLAUSE_NOGROUP:
34983 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34984 token->location);
34985 c_name = "nogroup";
34986 break;
34987 case PRAGMA_OMP_CLAUSE_THREADS:
34988 clauses
34989 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34990 clauses, token->location);
34991 c_name = "threads";
34992 break;
34993 case PRAGMA_OMP_CLAUSE_SIMD:
34994 clauses
34995 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34996 clauses, token->location);
34997 c_name = "simd";
34998 break;
34999 default:
35000 cp_parser_error (parser, "expected %<#pragma omp%> clause");
35001 goto saw_error;
35004 first = false;
35006 if (((mask >> c_kind) & 1) == 0)
35008 /* Remove the invalid clause(s) from the list to avoid
35009 confusing the rest of the compiler. */
35010 clauses = prev;
35011 error_at (token->location, "%qs is not valid for %qs", c_name, where);
35014 saw_error:
35015 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35016 if (finish_p)
35018 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
35019 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
35020 else
35021 return finish_omp_clauses (clauses, C_ORT_OMP);
35023 return clauses;
35026 /* OpenMP 2.5:
35027 structured-block:
35028 statement
35030 In practice, we're also interested in adding the statement to an
35031 outer node. So it is convenient if we work around the fact that
35032 cp_parser_statement calls add_stmt. */
35034 static unsigned
35035 cp_parser_begin_omp_structured_block (cp_parser *parser)
35037 unsigned save = parser->in_statement;
35039 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35040 This preserves the "not within loop or switch" style error messages
35041 for nonsense cases like
35042 void foo() {
35043 #pragma omp single
35044 break;
35047 if (parser->in_statement)
35048 parser->in_statement = IN_OMP_BLOCK;
35050 return save;
35053 static void
35054 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
35056 parser->in_statement = save;
35059 static tree
35060 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
35062 tree stmt = begin_omp_structured_block ();
35063 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35065 cp_parser_statement (parser, NULL_TREE, false, if_p);
35067 cp_parser_end_omp_structured_block (parser, save);
35068 return finish_omp_structured_block (stmt);
35071 /* OpenMP 2.5:
35072 # pragma omp atomic new-line
35073 expression-stmt
35075 expression-stmt:
35076 x binop= expr | x++ | ++x | x-- | --x
35077 binop:
35078 +, *, -, /, &, ^, |, <<, >>
35080 where x is an lvalue expression with scalar type.
35082 OpenMP 3.1:
35083 # pragma omp atomic new-line
35084 update-stmt
35086 # pragma omp atomic read new-line
35087 read-stmt
35089 # pragma omp atomic write new-line
35090 write-stmt
35092 # pragma omp atomic update new-line
35093 update-stmt
35095 # pragma omp atomic capture new-line
35096 capture-stmt
35098 # pragma omp atomic capture new-line
35099 capture-block
35101 read-stmt:
35102 v = x
35103 write-stmt:
35104 x = expr
35105 update-stmt:
35106 expression-stmt | x = x binop expr
35107 capture-stmt:
35108 v = expression-stmt
35109 capture-block:
35110 { v = x; update-stmt; } | { update-stmt; v = x; }
35112 OpenMP 4.0:
35113 update-stmt:
35114 expression-stmt | x = x binop expr | x = expr binop x
35115 capture-stmt:
35116 v = update-stmt
35117 capture-block:
35118 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35120 where x and v are lvalue expressions with scalar type. */
35122 static void
35123 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
35125 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
35126 tree rhs1 = NULL_TREE, orig_lhs;
35127 location_t loc = pragma_tok->location;
35128 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
35129 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
35130 bool structured_block = false;
35131 bool first = true;
35132 tree clauses = NULL_TREE;
35134 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35136 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35137 cp_lexer_consume_token (parser->lexer);
35139 first = false;
35141 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35143 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35144 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
35145 const char *p = IDENTIFIER_POINTER (id);
35146 enum tree_code new_code = ERROR_MARK;
35147 enum omp_memory_order new_memory_order
35148 = OMP_MEMORY_ORDER_UNSPECIFIED;
35150 if (!strcmp (p, "read"))
35151 new_code = OMP_ATOMIC_READ;
35152 else if (!strcmp (p, "write"))
35153 new_code = NOP_EXPR;
35154 else if (!strcmp (p, "update"))
35155 new_code = OMP_ATOMIC;
35156 else if (!strcmp (p, "capture"))
35157 new_code = OMP_ATOMIC_CAPTURE_NEW;
35158 else if (!strcmp (p, "seq_cst"))
35159 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35160 else if (!strcmp (p, "acq_rel"))
35161 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35162 else if (!strcmp (p, "release"))
35163 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
35164 else if (!strcmp (p, "acquire"))
35165 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35166 else if (!strcmp (p, "relaxed"))
35167 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
35168 else if (!strcmp (p, "hint"))
35170 cp_lexer_consume_token (parser->lexer);
35171 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
35172 continue;
35174 else
35176 p = NULL;
35177 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
35178 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35179 "%<release%>, %<relaxed%> or %<hint%> clause");
35181 if (p)
35183 if (new_code != ERROR_MARK)
35185 if (code != ERROR_MARK)
35186 error_at (cloc, "too many atomic clauses");
35187 else
35188 code = new_code;
35190 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35192 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35193 error_at (cloc, "too many memory order clauses");
35194 else
35195 memory_order = new_memory_order;
35197 cp_lexer_consume_token (parser->lexer);
35198 continue;
35201 break;
35203 cp_parser_require_pragma_eol (parser, pragma_tok);
35205 if (code == ERROR_MARK)
35206 code = OMP_ATOMIC;
35207 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35209 omp_requires_mask
35210 = (enum omp_requires) (omp_requires_mask
35211 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35212 switch ((enum omp_memory_order)
35213 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35215 case OMP_MEMORY_ORDER_UNSPECIFIED:
35216 case OMP_MEMORY_ORDER_RELAXED:
35217 memory_order = OMP_MEMORY_ORDER_RELAXED;
35218 break;
35219 case OMP_MEMORY_ORDER_SEQ_CST:
35220 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35221 break;
35222 case OMP_MEMORY_ORDER_ACQ_REL:
35223 switch (code)
35225 case OMP_ATOMIC_READ:
35226 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35227 break;
35228 case NOP_EXPR: /* atomic write */
35229 case OMP_ATOMIC:
35230 memory_order = OMP_MEMORY_ORDER_RELEASE;
35231 break;
35232 default:
35233 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35234 break;
35236 break;
35237 default:
35238 gcc_unreachable ();
35241 else
35242 switch (code)
35244 case OMP_ATOMIC_READ:
35245 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35246 || memory_order == OMP_MEMORY_ORDER_RELEASE)
35248 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
35249 "%<acq_rel%> or %<release%> clauses");
35250 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35252 break;
35253 case NOP_EXPR: /* atomic write */
35254 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35255 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35257 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
35258 "%<acq_rel%> or %<acquire%> clauses");
35259 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35261 break;
35262 case OMP_ATOMIC:
35263 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35264 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35266 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
35267 "%<acq_rel%> or %<acquire%> clauses");
35268 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35270 break;
35271 default:
35272 break;
35275 switch (code)
35277 case OMP_ATOMIC_READ:
35278 case NOP_EXPR: /* atomic write */
35279 v = cp_parser_unary_expression (parser);
35280 if (v == error_mark_node)
35281 goto saw_error;
35282 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35283 goto saw_error;
35284 if (code == NOP_EXPR)
35285 lhs = cp_parser_expression (parser);
35286 else
35287 lhs = cp_parser_unary_expression (parser);
35288 if (lhs == error_mark_node)
35289 goto saw_error;
35290 if (code == NOP_EXPR)
35292 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35293 opcode. */
35294 code = OMP_ATOMIC;
35295 rhs = lhs;
35296 lhs = v;
35297 v = NULL_TREE;
35299 goto done;
35300 case OMP_ATOMIC_CAPTURE_NEW:
35301 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35303 cp_lexer_consume_token (parser->lexer);
35304 structured_block = true;
35306 else
35308 v = cp_parser_unary_expression (parser);
35309 if (v == error_mark_node)
35310 goto saw_error;
35311 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35312 goto saw_error;
35314 default:
35315 break;
35318 restart:
35319 lhs = cp_parser_unary_expression (parser);
35320 orig_lhs = lhs;
35321 switch (TREE_CODE (lhs))
35323 case ERROR_MARK:
35324 goto saw_error;
35326 case POSTINCREMENT_EXPR:
35327 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35328 code = OMP_ATOMIC_CAPTURE_OLD;
35329 /* FALLTHROUGH */
35330 case PREINCREMENT_EXPR:
35331 lhs = TREE_OPERAND (lhs, 0);
35332 opcode = PLUS_EXPR;
35333 rhs = integer_one_node;
35334 break;
35336 case POSTDECREMENT_EXPR:
35337 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35338 code = OMP_ATOMIC_CAPTURE_OLD;
35339 /* FALLTHROUGH */
35340 case PREDECREMENT_EXPR:
35341 lhs = TREE_OPERAND (lhs, 0);
35342 opcode = MINUS_EXPR;
35343 rhs = integer_one_node;
35344 break;
35346 case COMPOUND_EXPR:
35347 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
35348 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
35349 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
35350 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
35351 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35352 (TREE_OPERAND (lhs, 1), 0), 0)))
35353 == BOOLEAN_TYPE)
35354 /* Undo effects of boolean_increment for post {in,de}crement. */
35355 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
35356 /* FALLTHRU */
35357 case MODIFY_EXPR:
35358 if (TREE_CODE (lhs) == MODIFY_EXPR
35359 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
35361 /* Undo effects of boolean_increment. */
35362 if (integer_onep (TREE_OPERAND (lhs, 1)))
35364 /* This is pre or post increment. */
35365 rhs = TREE_OPERAND (lhs, 1);
35366 lhs = TREE_OPERAND (lhs, 0);
35367 opcode = NOP_EXPR;
35368 if (code == OMP_ATOMIC_CAPTURE_NEW
35369 && !structured_block
35370 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
35371 code = OMP_ATOMIC_CAPTURE_OLD;
35372 break;
35375 /* FALLTHRU */
35376 default:
35377 switch (cp_lexer_peek_token (parser->lexer)->type)
35379 case CPP_MULT_EQ:
35380 opcode = MULT_EXPR;
35381 break;
35382 case CPP_DIV_EQ:
35383 opcode = TRUNC_DIV_EXPR;
35384 break;
35385 case CPP_PLUS_EQ:
35386 opcode = PLUS_EXPR;
35387 break;
35388 case CPP_MINUS_EQ:
35389 opcode = MINUS_EXPR;
35390 break;
35391 case CPP_LSHIFT_EQ:
35392 opcode = LSHIFT_EXPR;
35393 break;
35394 case CPP_RSHIFT_EQ:
35395 opcode = RSHIFT_EXPR;
35396 break;
35397 case CPP_AND_EQ:
35398 opcode = BIT_AND_EXPR;
35399 break;
35400 case CPP_OR_EQ:
35401 opcode = BIT_IOR_EXPR;
35402 break;
35403 case CPP_XOR_EQ:
35404 opcode = BIT_XOR_EXPR;
35405 break;
35406 case CPP_EQ:
35407 enum cp_parser_prec oprec;
35408 cp_token *token;
35409 cp_lexer_consume_token (parser->lexer);
35410 cp_parser_parse_tentatively (parser);
35411 rhs1 = cp_parser_simple_cast_expression (parser);
35412 if (rhs1 == error_mark_node)
35414 cp_parser_abort_tentative_parse (parser);
35415 cp_parser_simple_cast_expression (parser);
35416 goto saw_error;
35418 token = cp_lexer_peek_token (parser->lexer);
35419 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
35421 cp_parser_abort_tentative_parse (parser);
35422 cp_parser_parse_tentatively (parser);
35423 rhs = cp_parser_binary_expression (parser, false, true,
35424 PREC_NOT_OPERATOR, NULL);
35425 if (rhs == error_mark_node)
35427 cp_parser_abort_tentative_parse (parser);
35428 cp_parser_binary_expression (parser, false, true,
35429 PREC_NOT_OPERATOR, NULL);
35430 goto saw_error;
35432 switch (TREE_CODE (rhs))
35434 case MULT_EXPR:
35435 case TRUNC_DIV_EXPR:
35436 case RDIV_EXPR:
35437 case PLUS_EXPR:
35438 case MINUS_EXPR:
35439 case LSHIFT_EXPR:
35440 case RSHIFT_EXPR:
35441 case BIT_AND_EXPR:
35442 case BIT_IOR_EXPR:
35443 case BIT_XOR_EXPR:
35444 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
35446 if (cp_parser_parse_definitely (parser))
35448 opcode = TREE_CODE (rhs);
35449 rhs1 = TREE_OPERAND (rhs, 0);
35450 rhs = TREE_OPERAND (rhs, 1);
35451 goto stmt_done;
35453 else
35454 goto saw_error;
35456 break;
35457 default:
35458 break;
35460 cp_parser_abort_tentative_parse (parser);
35461 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
35463 rhs = cp_parser_expression (parser);
35464 if (rhs == error_mark_node)
35465 goto saw_error;
35466 opcode = NOP_EXPR;
35467 rhs1 = NULL_TREE;
35468 goto stmt_done;
35470 cp_parser_error (parser,
35471 "invalid form of %<#pragma omp atomic%>");
35472 goto saw_error;
35474 if (!cp_parser_parse_definitely (parser))
35475 goto saw_error;
35476 switch (token->type)
35478 case CPP_SEMICOLON:
35479 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35481 code = OMP_ATOMIC_CAPTURE_OLD;
35482 v = lhs;
35483 lhs = NULL_TREE;
35484 lhs1 = rhs1;
35485 rhs1 = NULL_TREE;
35486 cp_lexer_consume_token (parser->lexer);
35487 goto restart;
35489 else if (structured_block)
35491 opcode = NOP_EXPR;
35492 rhs = rhs1;
35493 rhs1 = NULL_TREE;
35494 goto stmt_done;
35496 cp_parser_error (parser,
35497 "invalid form of %<#pragma omp atomic%>");
35498 goto saw_error;
35499 case CPP_MULT:
35500 opcode = MULT_EXPR;
35501 break;
35502 case CPP_DIV:
35503 opcode = TRUNC_DIV_EXPR;
35504 break;
35505 case CPP_PLUS:
35506 opcode = PLUS_EXPR;
35507 break;
35508 case CPP_MINUS:
35509 opcode = MINUS_EXPR;
35510 break;
35511 case CPP_LSHIFT:
35512 opcode = LSHIFT_EXPR;
35513 break;
35514 case CPP_RSHIFT:
35515 opcode = RSHIFT_EXPR;
35516 break;
35517 case CPP_AND:
35518 opcode = BIT_AND_EXPR;
35519 break;
35520 case CPP_OR:
35521 opcode = BIT_IOR_EXPR;
35522 break;
35523 case CPP_XOR:
35524 opcode = BIT_XOR_EXPR;
35525 break;
35526 default:
35527 cp_parser_error (parser,
35528 "invalid operator for %<#pragma omp atomic%>");
35529 goto saw_error;
35531 oprec = TOKEN_PRECEDENCE (token);
35532 gcc_assert (oprec != PREC_NOT_OPERATOR);
35533 if (commutative_tree_code (opcode))
35534 oprec = (enum cp_parser_prec) (oprec - 1);
35535 cp_lexer_consume_token (parser->lexer);
35536 rhs = cp_parser_binary_expression (parser, false, false,
35537 oprec, NULL);
35538 if (rhs == error_mark_node)
35539 goto saw_error;
35540 goto stmt_done;
35541 /* FALLTHROUGH */
35542 default:
35543 cp_parser_error (parser,
35544 "invalid operator for %<#pragma omp atomic%>");
35545 goto saw_error;
35547 cp_lexer_consume_token (parser->lexer);
35549 rhs = cp_parser_expression (parser);
35550 if (rhs == error_mark_node)
35551 goto saw_error;
35552 break;
35554 stmt_done:
35555 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35557 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
35558 goto saw_error;
35559 v = cp_parser_unary_expression (parser);
35560 if (v == error_mark_node)
35561 goto saw_error;
35562 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35563 goto saw_error;
35564 lhs1 = cp_parser_unary_expression (parser);
35565 if (lhs1 == error_mark_node)
35566 goto saw_error;
35568 if (structured_block)
35570 cp_parser_consume_semicolon_at_end_of_statement (parser);
35571 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35573 done:
35574 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
35575 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
35576 rhs1, clauses, memory_order);
35577 if (!structured_block)
35578 cp_parser_consume_semicolon_at_end_of_statement (parser);
35579 return;
35581 saw_error:
35582 cp_parser_skip_to_end_of_block_or_statement (parser);
35583 if (structured_block)
35585 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35586 cp_lexer_consume_token (parser->lexer);
35587 else if (code == OMP_ATOMIC_CAPTURE_NEW)
35589 cp_parser_skip_to_end_of_block_or_statement (parser);
35590 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35591 cp_lexer_consume_token (parser->lexer);
35597 /* OpenMP 2.5:
35598 # pragma omp barrier new-line */
35600 static void
35601 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
35603 cp_parser_require_pragma_eol (parser, pragma_tok);
35604 finish_omp_barrier ();
35607 /* OpenMP 2.5:
35608 # pragma omp critical [(name)] new-line
35609 structured-block
35611 OpenMP 4.5:
35612 # pragma omp critical [(name) [hint(expression)]] new-line
35613 structured-block */
35615 #define OMP_CRITICAL_CLAUSE_MASK \
35616 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35618 static tree
35619 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35621 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
35623 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35625 matching_parens parens;
35626 parens.consume_open (parser);
35628 name = cp_parser_identifier (parser);
35630 if (name == error_mark_node
35631 || !parens.require_close (parser))
35632 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35633 /*or_comma=*/false,
35634 /*consume_paren=*/true);
35635 if (name == error_mark_node)
35636 name = NULL;
35638 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
35639 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
35640 cp_lexer_consume_token (parser->lexer);
35642 clauses = cp_parser_omp_all_clauses (parser,
35643 OMP_CRITICAL_CLAUSE_MASK,
35644 "#pragma omp critical", pragma_tok);
35646 else
35647 cp_parser_require_pragma_eol (parser, pragma_tok);
35649 stmt = cp_parser_omp_structured_block (parser, if_p);
35650 return c_finish_omp_critical (input_location, stmt, name, clauses);
35653 /* OpenMP 5.0:
35654 # pragma omp depobj ( depobj ) depobj-clause new-line
35656 depobj-clause:
35657 depend (dependence-type : locator)
35658 destroy
35659 update (dependence-type)
35661 dependence-type:
35664 inout
35665 mutexinout */
35667 static void
35668 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
35670 location_t loc = pragma_tok->location;
35671 matching_parens parens;
35672 if (!parens.require_open (parser))
35674 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35675 return;
35678 tree depobj = cp_parser_assignment_expression (parser);
35680 if (!parens.require_close (parser))
35681 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35682 /*or_comma=*/false,
35683 /*consume_paren=*/true);
35685 tree clause = NULL_TREE;
35686 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
35687 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
35688 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35690 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35691 const char *p = IDENTIFIER_POINTER (id);
35693 cp_lexer_consume_token (parser->lexer);
35694 if (!strcmp ("depend", p))
35696 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
35697 if (clause)
35698 clause = finish_omp_clauses (clause, C_ORT_OMP);
35699 if (!clause)
35700 clause = error_mark_node;
35702 else if (!strcmp ("destroy", p))
35703 kind = OMP_CLAUSE_DEPEND_LAST;
35704 else if (!strcmp ("update", p))
35706 matching_parens c_parens;
35707 if (c_parens.require_open (parser))
35709 location_t c2_loc
35710 = cp_lexer_peek_token (parser->lexer)->location;
35711 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35713 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
35714 const char *p2 = IDENTIFIER_POINTER (id2);
35716 cp_lexer_consume_token (parser->lexer);
35717 if (!strcmp ("in", p2))
35718 kind = OMP_CLAUSE_DEPEND_IN;
35719 else if (!strcmp ("out", p2))
35720 kind = OMP_CLAUSE_DEPEND_OUT;
35721 else if (!strcmp ("inout", p2))
35722 kind = OMP_CLAUSE_DEPEND_INOUT;
35723 else if (!strcmp ("mutexinoutset", p2))
35724 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
35726 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
35728 clause = error_mark_node;
35729 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
35730 "%<mutexinoutset%>");
35732 if (!c_parens.require_close (parser))
35733 cp_parser_skip_to_closing_parenthesis (parser,
35734 /*recovering=*/true,
35735 /*or_comma=*/false,
35736 /*consume_paren=*/true);
35738 else
35739 clause = error_mark_node;
35742 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
35744 clause = error_mark_node;
35745 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
35747 cp_parser_require_pragma_eol (parser, pragma_tok);
35749 finish_omp_depobj (loc, depobj, kind, clause);
35753 /* OpenMP 2.5:
35754 # pragma omp flush flush-vars[opt] new-line
35756 flush-vars:
35757 ( variable-list )
35759 OpenMP 5.0:
35760 # pragma omp flush memory-order-clause new-line */
35762 static void
35763 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
35765 enum memmodel mo = MEMMODEL_LAST;
35766 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35768 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35769 const char *p = IDENTIFIER_POINTER (id);
35770 if (!strcmp (p, "acq_rel"))
35771 mo = MEMMODEL_ACQ_REL;
35772 else if (!strcmp (p, "release"))
35773 mo = MEMMODEL_RELEASE;
35774 else if (!strcmp (p, "acquire"))
35775 mo = MEMMODEL_ACQUIRE;
35776 else
35777 error_at (cp_lexer_peek_token (parser->lexer)->location,
35778 "expected %<acq_rel%>, %<release%> or %<acquire%>");
35779 cp_lexer_consume_token (parser->lexer);
35781 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35783 if (mo != MEMMODEL_LAST)
35784 error_at (cp_lexer_peek_token (parser->lexer)->location,
35785 "%<flush%> list specified together with memory order "
35786 "clause");
35787 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35789 cp_parser_require_pragma_eol (parser, pragma_tok);
35791 finish_omp_flush (mo);
35794 /* Helper function, to parse omp for increment expression. */
35796 static tree
35797 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
35799 tree cond = cp_parser_binary_expression (parser, false, true,
35800 PREC_NOT_OPERATOR, NULL);
35801 if (cond == error_mark_node
35802 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35804 cp_parser_skip_to_end_of_statement (parser);
35805 return error_mark_node;
35808 switch (TREE_CODE (cond))
35810 case GT_EXPR:
35811 case GE_EXPR:
35812 case LT_EXPR:
35813 case LE_EXPR:
35814 break;
35815 case NE_EXPR:
35816 if (code != OACC_LOOP)
35817 break;
35818 gcc_fallthrough ();
35819 default:
35820 return error_mark_node;
35823 /* If decl is an iterator, preserve LHS and RHS of the relational
35824 expr until finish_omp_for. */
35825 if (decl
35826 && (type_dependent_expression_p (decl)
35827 || CLASS_TYPE_P (TREE_TYPE (decl))))
35828 return cond;
35830 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
35831 TREE_CODE (cond),
35832 TREE_OPERAND (cond, 0), ERROR_MARK,
35833 TREE_OPERAND (cond, 1), ERROR_MARK,
35834 /*overload=*/NULL, tf_warning_or_error);
35837 /* Helper function, to parse omp for increment expression. */
35839 static tree
35840 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
35842 cp_token *token = cp_lexer_peek_token (parser->lexer);
35843 enum tree_code op;
35844 tree lhs, rhs;
35845 cp_id_kind idk;
35846 bool decl_first;
35848 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
35850 op = (token->type == CPP_PLUS_PLUS
35851 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
35852 cp_lexer_consume_token (parser->lexer);
35853 lhs = cp_parser_simple_cast_expression (parser);
35854 if (lhs != decl
35855 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
35856 return error_mark_node;
35857 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
35860 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
35861 if (lhs != decl
35862 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
35863 return error_mark_node;
35865 token = cp_lexer_peek_token (parser->lexer);
35866 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
35868 op = (token->type == CPP_PLUS_PLUS
35869 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
35870 cp_lexer_consume_token (parser->lexer);
35871 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
35874 op = cp_parser_assignment_operator_opt (parser);
35875 if (op == ERROR_MARK)
35876 return error_mark_node;
35878 if (op != NOP_EXPR)
35880 rhs = cp_parser_assignment_expression (parser);
35881 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
35882 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35885 lhs = cp_parser_binary_expression (parser, false, false,
35886 PREC_ADDITIVE_EXPRESSION, NULL);
35887 token = cp_lexer_peek_token (parser->lexer);
35888 decl_first = (lhs == decl
35889 || (processing_template_decl && cp_tree_equal (lhs, decl)));
35890 if (decl_first)
35891 lhs = NULL_TREE;
35892 if (token->type != CPP_PLUS
35893 && token->type != CPP_MINUS)
35894 return error_mark_node;
35898 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
35899 cp_lexer_consume_token (parser->lexer);
35900 rhs = cp_parser_binary_expression (parser, false, false,
35901 PREC_ADDITIVE_EXPRESSION, NULL);
35902 token = cp_lexer_peek_token (parser->lexer);
35903 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
35905 if (lhs == NULL_TREE)
35907 if (op == PLUS_EXPR)
35908 lhs = rhs;
35909 else
35910 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
35911 tf_warning_or_error);
35913 else
35914 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
35915 ERROR_MARK, NULL, tf_warning_or_error);
35918 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
35920 if (!decl_first)
35922 if ((rhs != decl
35923 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
35924 || op == MINUS_EXPR)
35925 return error_mark_node;
35926 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
35928 else
35929 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
35931 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35934 /* Parse the initialization statement of an OpenMP for loop.
35936 Return true if the resulting construct should have an
35937 OMP_CLAUSE_PRIVATE added to it. */
35939 static tree
35940 cp_parser_omp_for_loop_init (cp_parser *parser,
35941 tree &this_pre_body,
35942 vec<tree, va_gc> *&for_block,
35943 tree &init,
35944 tree &orig_init,
35945 tree &decl,
35946 tree &real_decl)
35948 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35949 return NULL_TREE;
35951 tree add_private_clause = NULL_TREE;
35953 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
35955 init-expr:
35956 var = lb
35957 integer-type var = lb
35958 random-access-iterator-type var = lb
35959 pointer-type var = lb
35961 cp_decl_specifier_seq type_specifiers;
35963 /* First, try to parse as an initialized declaration. See
35964 cp_parser_condition, from whence the bulk of this is copied. */
35966 cp_parser_parse_tentatively (parser);
35967 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
35968 /*is_trailing_return=*/false,
35969 &type_specifiers);
35970 if (cp_parser_parse_definitely (parser))
35972 /* If parsing a type specifier seq succeeded, then this
35973 MUST be a initialized declaration. */
35974 tree asm_specification, attributes;
35975 cp_declarator *declarator;
35977 declarator = cp_parser_declarator (parser,
35978 CP_PARSER_DECLARATOR_NAMED,
35979 /*ctor_dtor_or_conv_p=*/NULL,
35980 /*parenthesized_p=*/NULL,
35981 /*member_p=*/false,
35982 /*friend_p=*/false);
35983 attributes = cp_parser_attributes_opt (parser);
35984 asm_specification = cp_parser_asm_specification_opt (parser);
35986 if (declarator == cp_error_declarator)
35987 cp_parser_skip_to_end_of_statement (parser);
35989 else
35991 tree pushed_scope, auto_node;
35993 decl = start_decl (declarator, &type_specifiers,
35994 SD_INITIALIZED, attributes,
35995 /*prefix_attributes=*/NULL_TREE,
35996 &pushed_scope);
35998 auto_node = type_uses_auto (TREE_TYPE (decl));
35999 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36001 if (cp_lexer_next_token_is (parser->lexer,
36002 CPP_OPEN_PAREN))
36003 error ("parenthesized initialization is not allowed in "
36004 "OpenMP %<for%> loop");
36005 else
36006 /* Trigger an error. */
36007 cp_parser_require (parser, CPP_EQ, RT_EQ);
36009 init = error_mark_node;
36010 cp_parser_skip_to_end_of_statement (parser);
36012 else if (CLASS_TYPE_P (TREE_TYPE (decl))
36013 || type_dependent_expression_p (decl)
36014 || auto_node)
36016 bool is_direct_init, is_non_constant_init;
36018 init = cp_parser_initializer (parser,
36019 &is_direct_init,
36020 &is_non_constant_init);
36022 if (auto_node)
36024 TREE_TYPE (decl)
36025 = do_auto_deduction (TREE_TYPE (decl), init,
36026 auto_node);
36028 if (!CLASS_TYPE_P (TREE_TYPE (decl))
36029 && !type_dependent_expression_p (decl))
36030 goto non_class;
36033 cp_finish_decl (decl, init, !is_non_constant_init,
36034 asm_specification,
36035 LOOKUP_ONLYCONVERTING);
36036 orig_init = init;
36037 if (CLASS_TYPE_P (TREE_TYPE (decl)))
36039 vec_safe_push (for_block, this_pre_body);
36040 init = NULL_TREE;
36042 else
36044 init = pop_stmt_list (this_pre_body);
36045 if (init && TREE_CODE (init) == STATEMENT_LIST)
36047 tree_stmt_iterator i = tsi_start (init);
36048 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36049 while (!tsi_end_p (i))
36051 tree t = tsi_stmt (i);
36052 if (TREE_CODE (t) == DECL_EXPR
36053 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
36055 tsi_delink (&i);
36056 vec_safe_push (for_block, t);
36057 continue;
36059 break;
36061 if (tsi_one_before_end_p (i))
36063 tree t = tsi_stmt (i);
36064 tsi_delink (&i);
36065 free_stmt_list (init);
36066 init = t;
36070 this_pre_body = NULL_TREE;
36072 else
36074 /* Consume '='. */
36075 cp_lexer_consume_token (parser->lexer);
36076 init = cp_parser_assignment_expression (parser);
36078 non_class:
36079 if (TYPE_REF_P (TREE_TYPE (decl)))
36080 init = error_mark_node;
36081 else
36082 cp_finish_decl (decl, NULL_TREE,
36083 /*init_const_expr_p=*/false,
36084 asm_specification,
36085 LOOKUP_ONLYCONVERTING);
36088 if (pushed_scope)
36089 pop_scope (pushed_scope);
36092 else
36094 cp_id_kind idk;
36095 /* If parsing a type specifier sequence failed, then
36096 this MUST be a simple expression. */
36097 cp_parser_parse_tentatively (parser);
36098 decl = cp_parser_primary_expression (parser, false, false,
36099 false, &idk);
36100 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
36101 if (!cp_parser_error_occurred (parser)
36102 && decl
36103 && (TREE_CODE (decl) == COMPONENT_REF
36104 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
36106 cp_parser_abort_tentative_parse (parser);
36107 cp_parser_parse_tentatively (parser);
36108 cp_token *token = cp_lexer_peek_token (parser->lexer);
36109 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
36110 /*check_dependency_p=*/true,
36111 /*template_p=*/NULL,
36112 /*declarator_p=*/false,
36113 /*optional_p=*/false);
36114 if (name != error_mark_node
36115 && last_tok == cp_lexer_peek_token (parser->lexer))
36117 decl = cp_parser_lookup_name_simple (parser, name,
36118 token->location);
36119 if (TREE_CODE (decl) == FIELD_DECL)
36120 add_private_clause = omp_privatize_field (decl, false);
36122 cp_parser_abort_tentative_parse (parser);
36123 cp_parser_parse_tentatively (parser);
36124 decl = cp_parser_primary_expression (parser, false, false,
36125 false, &idk);
36127 if (!cp_parser_error_occurred (parser)
36128 && decl
36129 && DECL_P (decl)
36130 && CLASS_TYPE_P (TREE_TYPE (decl)))
36132 tree rhs;
36134 cp_parser_parse_definitely (parser);
36135 cp_parser_require (parser, CPP_EQ, RT_EQ);
36136 rhs = cp_parser_assignment_expression (parser);
36137 orig_init = rhs;
36138 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
36139 decl, NOP_EXPR,
36140 rhs,
36141 tf_warning_or_error));
36142 if (!add_private_clause)
36143 add_private_clause = decl;
36145 else
36147 decl = NULL;
36148 cp_parser_abort_tentative_parse (parser);
36149 init = cp_parser_expression (parser);
36150 if (init)
36152 if (TREE_CODE (init) == MODIFY_EXPR
36153 || TREE_CODE (init) == MODOP_EXPR)
36154 real_decl = TREE_OPERAND (init, 0);
36158 return add_private_clause;
36161 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36163 void
36164 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
36165 tree &decl, tree &orig_decl, tree &init,
36166 tree &orig_init, tree &cond, tree &incr)
36168 tree begin, end, range_temp_decl = NULL_TREE;
36169 tree iter_type, begin_expr, end_expr;
36171 if (processing_template_decl)
36173 if (check_for_bare_parameter_packs (init))
36174 init = error_mark_node;
36175 if (!type_dependent_expression_p (init)
36176 /* do_auto_deduction doesn't mess with template init-lists. */
36177 && !BRACE_ENCLOSED_INITIALIZER_P (init))
36179 tree d = decl;
36180 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
36182 tree v = DECL_VALUE_EXPR (decl);
36183 if (TREE_CODE (v) == ARRAY_REF
36184 && VAR_P (TREE_OPERAND (v, 0))
36185 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36186 d = TREE_OPERAND (v, 0);
36188 do_range_for_auto_deduction (d, init);
36190 cond = global_namespace;
36191 incr = NULL_TREE;
36192 orig_init = init;
36193 if (this_pre_body)
36194 this_pre_body = pop_stmt_list (this_pre_body);
36195 return;
36198 init = mark_lvalue_use (init);
36200 if (decl == error_mark_node || init == error_mark_node)
36201 /* If an error happened previously do nothing or else a lot of
36202 unhelpful errors would be issued. */
36203 begin_expr = end_expr = iter_type = error_mark_node;
36204 else
36206 tree range_temp;
36208 if (VAR_P (init)
36209 && array_of_runtime_bound_p (TREE_TYPE (init)))
36210 /* Can't bind a reference to an array of runtime bound. */
36211 range_temp = init;
36212 else
36214 range_temp = build_range_temp (init);
36215 DECL_NAME (range_temp) = NULL_TREE;
36216 pushdecl (range_temp);
36217 cp_finish_decl (range_temp, init,
36218 /*is_constant_init*/false, NULL_TREE,
36219 LOOKUP_ONLYCONVERTING);
36220 range_temp_decl = range_temp;
36221 range_temp = convert_from_reference (range_temp);
36223 iter_type = cp_parser_perform_range_for_lookup (range_temp,
36224 &begin_expr, &end_expr);
36227 tree end_iter_type = iter_type;
36228 if (cxx_dialect >= cxx17)
36229 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36230 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
36231 TREE_USED (end) = 1;
36232 DECL_ARTIFICIAL (end) = 1;
36233 pushdecl (end);
36234 cp_finish_decl (end, end_expr,
36235 /*is_constant_init*/false, NULL_TREE,
36236 LOOKUP_ONLYCONVERTING);
36238 /* The new for initialization statement. */
36239 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
36240 TREE_USED (begin) = 1;
36241 DECL_ARTIFICIAL (begin) = 1;
36242 pushdecl (begin);
36243 orig_init = init;
36244 if (CLASS_TYPE_P (iter_type))
36245 init = NULL_TREE;
36246 else
36248 init = begin_expr;
36249 begin_expr = NULL_TREE;
36251 cp_finish_decl (begin, begin_expr,
36252 /*is_constant_init*/false, NULL_TREE,
36253 LOOKUP_ONLYCONVERTING);
36255 /* The new for condition. */
36256 if (CLASS_TYPE_P (iter_type))
36257 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
36258 else
36259 cond = build_x_binary_op (input_location, NE_EXPR,
36260 begin, ERROR_MARK,
36261 end, ERROR_MARK,
36262 NULL, tf_warning_or_error);
36264 /* The new increment expression. */
36265 if (CLASS_TYPE_P (iter_type))
36266 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
36267 else
36268 incr = finish_unary_op_expr (input_location,
36269 PREINCREMENT_EXPR, begin,
36270 tf_warning_or_error);
36272 orig_decl = decl;
36273 decl = begin;
36274 if (for_block)
36276 vec_safe_push (for_block, this_pre_body);
36277 this_pre_body = NULL_TREE;
36280 tree decomp_first_name = NULL_TREE;
36281 unsigned decomp_cnt = 0;
36282 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
36284 tree v = DECL_VALUE_EXPR (orig_decl);
36285 if (TREE_CODE (v) == ARRAY_REF
36286 && VAR_P (TREE_OPERAND (v, 0))
36287 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36289 tree d = orig_decl;
36290 orig_decl = TREE_OPERAND (v, 0);
36291 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
36292 decomp_first_name = d;
36296 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
36297 if (auto_node)
36299 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36300 tf_none);
36301 if (!error_operand_p (t))
36302 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
36303 t, auto_node);
36306 tree v = make_tree_vec (decomp_cnt + 3);
36307 TREE_VEC_ELT (v, 0) = range_temp_decl;
36308 TREE_VEC_ELT (v, 1) = end;
36309 TREE_VEC_ELT (v, 2) = orig_decl;
36310 for (unsigned i = 0; i < decomp_cnt; i++)
36312 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
36313 decomp_first_name = DECL_CHAIN (decomp_first_name);
36315 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
36318 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36319 inside of the collapsed body. */
36321 void
36322 cp_finish_omp_range_for (tree orig, tree begin)
36324 gcc_assert (TREE_CODE (orig) == TREE_LIST
36325 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
36326 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
36327 tree decomp_first_name = NULL_TREE;
36328 unsigned int decomp_cnt = 0;
36330 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36332 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
36333 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
36334 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
36337 /* The declaration is initialized with *__begin inside the loop body. */
36338 cp_finish_decl (decl,
36339 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36340 tf_warning_or_error),
36341 /*is_constant_init*/false, NULL_TREE,
36342 LOOKUP_ONLYCONVERTING);
36343 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36344 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
36347 /* Parse the restricted form of the for statement allowed by OpenMP. */
36349 static tree
36350 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
36351 tree *cclauses, bool *if_p)
36353 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
36354 tree orig_decl;
36355 tree real_decl, initv, condv, incrv, declv, orig_declv;
36356 tree this_pre_body, cl, ordered_cl = NULL_TREE;
36357 location_t loc_first;
36358 bool collapse_err = false;
36359 int i, collapse = 1, ordered = 0, count, nbraces = 0;
36360 vec<tree, va_gc> *for_block = make_tree_vector ();
36361 auto_vec<tree, 4> orig_inits;
36362 bool tiling = false;
36364 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
36365 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
36366 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
36367 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
36369 tiling = true;
36370 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
36372 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
36373 && OMP_CLAUSE_ORDERED_EXPR (cl))
36375 ordered_cl = cl;
36376 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
36379 if (ordered && ordered < collapse)
36381 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36382 "%<ordered%> clause parameter is less than %<collapse%>");
36383 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
36384 = build_int_cst (NULL_TREE, collapse);
36385 ordered = collapse;
36387 if (ordered)
36389 for (tree *pc = &clauses; *pc; )
36390 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
36392 error_at (OMP_CLAUSE_LOCATION (*pc),
36393 "%<linear%> clause may not be specified together "
36394 "with %<ordered%> clause with a parameter");
36395 *pc = OMP_CLAUSE_CHAIN (*pc);
36397 else
36398 pc = &OMP_CLAUSE_CHAIN (*pc);
36401 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
36402 count = ordered ? ordered : collapse;
36404 declv = make_tree_vec (count);
36405 initv = make_tree_vec (count);
36406 condv = make_tree_vec (count);
36407 incrv = make_tree_vec (count);
36408 orig_declv = NULL_TREE;
36410 loc_first = cp_lexer_peek_token (parser->lexer)->location;
36412 for (i = 0; i < count; i++)
36414 int bracecount = 0;
36415 tree add_private_clause = NULL_TREE;
36416 location_t loc;
36418 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36420 if (!collapse_err)
36421 cp_parser_error (parser, "for statement expected");
36422 return NULL;
36424 loc = cp_lexer_consume_token (parser->lexer)->location;
36426 matching_parens parens;
36427 if (!parens.require_open (parser))
36428 return NULL;
36430 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
36431 this_pre_body = push_stmt_list ();
36433 if (code != OACC_LOOP && cxx_dialect >= cxx11)
36435 /* Save tokens so that we can put them back. */
36436 cp_lexer_save_tokens (parser->lexer);
36438 /* Look for ':' that is not nested in () or {}. */
36439 bool is_range_for
36440 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
36441 /*recovering=*/false,
36442 CPP_COLON,
36443 /*consume_paren=*/
36444 false) == -1);
36446 /* Roll back the tokens we skipped. */
36447 cp_lexer_rollback_tokens (parser->lexer);
36449 if (is_range_for)
36451 bool saved_colon_corrects_to_scope_p
36452 = parser->colon_corrects_to_scope_p;
36454 /* A colon is used in range-based for. */
36455 parser->colon_corrects_to_scope_p = false;
36457 /* Parse the declaration. */
36458 cp_parser_simple_declaration (parser,
36459 /*function_definition_allowed_p=*/
36460 false, &decl);
36461 parser->colon_corrects_to_scope_p
36462 = saved_colon_corrects_to_scope_p;
36464 cp_parser_require (parser, CPP_COLON, RT_COLON);
36466 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
36467 false, 0, true);
36469 cp_convert_omp_range_for (this_pre_body, for_block, decl,
36470 orig_decl, init, orig_init,
36471 cond, incr);
36472 if (this_pre_body)
36474 if (pre_body)
36476 tree t = pre_body;
36477 pre_body = push_stmt_list ();
36478 add_stmt (t);
36479 add_stmt (this_pre_body);
36480 pre_body = pop_stmt_list (pre_body);
36482 else
36483 pre_body = this_pre_body;
36486 if (ordered_cl)
36487 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36488 "%<ordered%> clause with parameter on "
36489 "range-based %<for%> loop");
36491 goto parse_close_paren;
36495 add_private_clause
36496 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
36497 init, orig_init, decl, real_decl);
36499 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36500 if (this_pre_body)
36502 this_pre_body = pop_stmt_list (this_pre_body);
36503 if (pre_body)
36505 tree t = pre_body;
36506 pre_body = push_stmt_list ();
36507 add_stmt (t);
36508 add_stmt (this_pre_body);
36509 pre_body = pop_stmt_list (pre_body);
36511 else
36512 pre_body = this_pre_body;
36515 if (decl)
36516 real_decl = decl;
36517 if (cclauses != NULL
36518 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
36519 && real_decl != NULL_TREE)
36521 tree *c;
36522 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
36523 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
36524 && OMP_CLAUSE_DECL (*c) == real_decl)
36526 error_at (loc, "iteration variable %qD"
36527 " should not be firstprivate", real_decl);
36528 *c = OMP_CLAUSE_CHAIN (*c);
36530 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
36531 && OMP_CLAUSE_DECL (*c) == real_decl)
36533 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36534 tree l = *c;
36535 *c = OMP_CLAUSE_CHAIN (*c);
36536 if (code == OMP_SIMD)
36538 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36539 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
36541 else
36543 OMP_CLAUSE_CHAIN (l) = clauses;
36544 clauses = l;
36546 add_private_clause = NULL_TREE;
36548 else
36550 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
36551 && OMP_CLAUSE_DECL (*c) == real_decl)
36552 add_private_clause = NULL_TREE;
36553 c = &OMP_CLAUSE_CHAIN (*c);
36557 if (add_private_clause)
36559 tree c;
36560 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
36562 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
36563 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
36564 && OMP_CLAUSE_DECL (c) == decl)
36565 break;
36566 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
36567 && OMP_CLAUSE_DECL (c) == decl)
36568 error_at (loc, "iteration variable %qD "
36569 "should not be firstprivate",
36570 decl);
36571 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
36572 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
36573 && OMP_CLAUSE_DECL (c) == decl)
36574 error_at (loc, "iteration variable %qD should not be reduction",
36575 decl);
36577 if (c == NULL)
36579 if (code != OMP_SIMD)
36580 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
36581 else if (collapse == 1)
36582 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
36583 else
36584 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
36585 OMP_CLAUSE_DECL (c) = add_private_clause;
36586 c = finish_omp_clauses (c, C_ORT_OMP);
36587 if (c)
36589 OMP_CLAUSE_CHAIN (c) = clauses;
36590 clauses = c;
36591 /* For linear, signal that we need to fill up
36592 the so far unknown linear step. */
36593 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
36594 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
36599 cond = NULL;
36600 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36601 cond = cp_parser_omp_for_cond (parser, decl, code);
36602 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36604 incr = NULL;
36605 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
36607 /* If decl is an iterator, preserve the operator on decl
36608 until finish_omp_for. */
36609 if (real_decl
36610 && ((processing_template_decl
36611 && (TREE_TYPE (real_decl) == NULL_TREE
36612 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
36613 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
36614 incr = cp_parser_omp_for_incr (parser, real_decl);
36615 else
36616 incr = cp_parser_expression (parser);
36617 if (!EXPR_HAS_LOCATION (incr))
36618 protected_set_expr_location (incr, input_location);
36621 parse_close_paren:
36622 if (!parens.require_close (parser))
36623 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36624 /*or_comma=*/false,
36625 /*consume_paren=*/true);
36627 TREE_VEC_ELT (declv, i) = decl;
36628 TREE_VEC_ELT (initv, i) = init;
36629 TREE_VEC_ELT (condv, i) = cond;
36630 TREE_VEC_ELT (incrv, i) = incr;
36631 if (orig_init)
36633 orig_inits.safe_grow_cleared (i + 1);
36634 orig_inits[i] = orig_init;
36636 if (orig_decl)
36638 if (!orig_declv)
36639 orig_declv = copy_node (declv);
36640 TREE_VEC_ELT (orig_declv, i) = orig_decl;
36642 else if (orig_declv)
36643 TREE_VEC_ELT (orig_declv, i) = decl;
36645 if (i == count - 1)
36646 break;
36648 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
36649 in between the collapsed for loops to be still considered perfectly
36650 nested. Hopefully the final version clarifies this.
36651 For now handle (multiple) {'s and empty statements. */
36652 cp_parser_parse_tentatively (parser);
36653 for (;;)
36655 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36656 break;
36657 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36659 cp_lexer_consume_token (parser->lexer);
36660 bracecount++;
36662 else if (bracecount
36663 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36664 cp_lexer_consume_token (parser->lexer);
36665 else
36667 loc = cp_lexer_peek_token (parser->lexer)->location;
36668 error_at (loc, "not enough for loops to collapse");
36669 collapse_err = true;
36670 cp_parser_abort_tentative_parse (parser);
36671 declv = NULL_TREE;
36672 break;
36676 if (declv)
36678 cp_parser_parse_definitely (parser);
36679 nbraces += bracecount;
36683 if (nbraces)
36684 if_p = NULL;
36686 /* Note that we saved the original contents of this flag when we entered
36687 the structured block, and so we don't need to re-save it here. */
36688 parser->in_statement = IN_OMP_FOR;
36690 /* Note that the grammar doesn't call for a structured block here,
36691 though the loop as a whole is a structured block. */
36692 if (orig_declv)
36694 body = begin_omp_structured_block ();
36695 for (i = 0; i < count; i++)
36696 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
36697 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
36698 TREE_VEC_ELT (declv, i));
36700 else
36701 body = push_stmt_list ();
36702 cp_parser_statement (parser, NULL_TREE, false, if_p);
36703 if (orig_declv)
36704 body = finish_omp_structured_block (body);
36705 else
36706 body = pop_stmt_list (body);
36708 if (declv == NULL_TREE)
36709 ret = NULL_TREE;
36710 else
36711 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
36712 incrv, body, pre_body, &orig_inits, clauses);
36714 while (nbraces)
36716 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36718 cp_lexer_consume_token (parser->lexer);
36719 nbraces--;
36721 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36722 cp_lexer_consume_token (parser->lexer);
36723 else
36725 if (!collapse_err)
36727 error_at (cp_lexer_peek_token (parser->lexer)->location,
36728 "collapsed loops not perfectly nested");
36730 collapse_err = true;
36731 cp_parser_statement_seq_opt (parser, NULL);
36732 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
36733 break;
36737 while (!for_block->is_empty ())
36739 tree t = for_block->pop ();
36740 if (TREE_CODE (t) == STATEMENT_LIST)
36741 add_stmt (pop_stmt_list (t));
36742 else
36743 add_stmt (t);
36745 release_tree_vector (for_block);
36747 return ret;
36750 /* Helper function for OpenMP parsing, split clauses and call
36751 finish_omp_clauses on each of the set of clauses afterwards. */
36753 static void
36754 cp_omp_split_clauses (location_t loc, enum tree_code code,
36755 omp_clause_mask mask, tree clauses, tree *cclauses)
36757 int i;
36758 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
36759 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
36760 if (cclauses[i])
36761 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
36764 /* OpenMP 4.0:
36765 #pragma omp simd simd-clause[optseq] new-line
36766 for-loop */
36768 #define OMP_SIMD_CLAUSE_MASK \
36769 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
36770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
36780 static tree
36781 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
36782 char *p_name, omp_clause_mask mask, tree *cclauses,
36783 bool *if_p)
36785 tree clauses, sb, ret;
36786 unsigned int save;
36787 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36789 strcat (p_name, " simd");
36790 mask |= OMP_SIMD_CLAUSE_MASK;
36792 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36793 cclauses == NULL);
36794 if (cclauses)
36796 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
36797 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
36798 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
36799 OMP_CLAUSE_ORDERED);
36800 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
36802 error_at (OMP_CLAUSE_LOCATION (c),
36803 "%<ordered%> clause with parameter may not be specified "
36804 "on %qs construct", p_name);
36805 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
36809 keep_next_level (true);
36810 sb = begin_omp_structured_block ();
36811 save = cp_parser_begin_omp_structured_block (parser);
36813 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
36815 cp_parser_end_omp_structured_block (parser, save);
36816 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
36818 return ret;
36821 /* OpenMP 2.5:
36822 #pragma omp for for-clause[optseq] new-line
36823 for-loop
36825 OpenMP 4.0:
36826 #pragma omp for simd for-simd-clause[optseq] new-line
36827 for-loop */
36829 #define OMP_FOR_CLAUSE_MASK \
36830 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
36836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
36837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36840 static tree
36841 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
36842 char *p_name, omp_clause_mask mask, tree *cclauses,
36843 bool *if_p)
36845 tree clauses, sb, ret;
36846 unsigned int save;
36847 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36849 strcat (p_name, " for");
36850 mask |= OMP_FOR_CLAUSE_MASK;
36851 /* parallel for{, simd} disallows nowait clause, but for
36852 target {teams distribute ,}parallel for{, simd} it should be accepted. */
36853 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
36854 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
36855 /* Composite distribute parallel for{, simd} disallows ordered clause. */
36856 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
36857 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
36859 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36861 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36862 const char *p = IDENTIFIER_POINTER (id);
36864 if (strcmp (p, "simd") == 0)
36866 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36867 if (cclauses == NULL)
36868 cclauses = cclauses_buf;
36870 cp_lexer_consume_token (parser->lexer);
36871 if (!flag_openmp) /* flag_openmp_simd */
36872 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36873 cclauses, if_p);
36874 sb = begin_omp_structured_block ();
36875 save = cp_parser_begin_omp_structured_block (parser);
36876 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36877 cclauses, if_p);
36878 cp_parser_end_omp_structured_block (parser, save);
36879 tree body = finish_omp_structured_block (sb);
36880 if (ret == NULL)
36881 return ret;
36882 ret = make_node (OMP_FOR);
36883 TREE_TYPE (ret) = void_type_node;
36884 OMP_FOR_BODY (ret) = body;
36885 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36886 SET_EXPR_LOCATION (ret, loc);
36887 add_stmt (ret);
36888 return ret;
36891 if (!flag_openmp) /* flag_openmp_simd */
36893 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36894 return NULL_TREE;
36897 /* Composite distribute parallel for disallows linear clause. */
36898 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
36899 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
36901 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36902 cclauses == NULL);
36903 if (cclauses)
36905 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
36906 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36909 keep_next_level (true);
36910 sb = begin_omp_structured_block ();
36911 save = cp_parser_begin_omp_structured_block (parser);
36913 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
36915 cp_parser_end_omp_structured_block (parser, save);
36916 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
36918 return ret;
36921 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
36922 omp_clause_mask, tree *, bool *);
36924 /* OpenMP 2.5:
36925 # pragma omp master new-line
36926 structured-block */
36928 static tree
36929 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
36930 char *p_name, omp_clause_mask mask, tree *cclauses,
36931 bool *if_p)
36933 tree clauses, sb, ret;
36934 unsigned int save;
36935 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36937 strcat (p_name, " master");
36939 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36941 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36942 const char *p = IDENTIFIER_POINTER (id);
36944 if (strcmp (p, "taskloop") == 0)
36946 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36947 if (cclauses == NULL)
36948 cclauses = cclauses_buf;
36950 cp_lexer_consume_token (parser->lexer);
36951 if (!flag_openmp) /* flag_openmp_simd */
36952 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
36953 cclauses, if_p);
36954 sb = begin_omp_structured_block ();
36955 save = cp_parser_begin_omp_structured_block (parser);
36956 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
36957 cclauses, if_p);
36958 cp_parser_end_omp_structured_block (parser, save);
36959 tree body = finish_omp_structured_block (sb);
36960 if (ret == NULL)
36961 return ret;
36962 return c_finish_omp_master (loc, body);
36965 if (!flag_openmp) /* flag_openmp_simd */
36967 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36968 return NULL_TREE;
36971 if (cclauses)
36973 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36974 false);
36975 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
36977 else
36978 cp_parser_require_pragma_eol (parser, pragma_tok);
36980 return c_finish_omp_master (loc,
36981 cp_parser_omp_structured_block (parser, if_p));
36984 /* OpenMP 2.5:
36985 # pragma omp ordered new-line
36986 structured-block
36988 OpenMP 4.5:
36989 # pragma omp ordered ordered-clauses new-line
36990 structured-block */
36992 #define OMP_ORDERED_CLAUSE_MASK \
36993 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
36994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
36996 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
36997 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
36999 static bool
37000 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
37001 enum pragma_context context, bool *if_p)
37003 location_t loc = pragma_tok->location;
37005 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37007 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37008 const char *p = IDENTIFIER_POINTER (id);
37010 if (strcmp (p, "depend") == 0)
37012 if (!flag_openmp) /* flag_openmp_simd */
37014 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37015 return false;
37017 if (context == pragma_stmt)
37019 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
37020 "%<depend%> clause may only be used in compound "
37021 "statements");
37022 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37023 return false;
37025 tree clauses
37026 = cp_parser_omp_all_clauses (parser,
37027 OMP_ORDERED_DEPEND_CLAUSE_MASK,
37028 "#pragma omp ordered", pragma_tok);
37029 c_finish_omp_ordered (loc, clauses, NULL_TREE);
37030 return false;
37034 tree clauses
37035 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
37036 "#pragma omp ordered", pragma_tok);
37038 if (!flag_openmp /* flag_openmp_simd */
37039 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
37040 return false;
37042 c_finish_omp_ordered (loc, clauses,
37043 cp_parser_omp_structured_block (parser, if_p));
37044 return true;
37047 /* OpenMP 2.5:
37049 section-scope:
37050 { section-sequence }
37052 section-sequence:
37053 section-directive[opt] structured-block
37054 section-sequence section-directive structured-block */
37056 static tree
37057 cp_parser_omp_sections_scope (cp_parser *parser)
37059 tree stmt, substmt;
37060 bool error_suppress = false;
37061 cp_token *tok;
37063 matching_braces braces;
37064 if (!braces.require_open (parser))
37065 return NULL_TREE;
37067 stmt = push_stmt_list ();
37069 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
37070 != PRAGMA_OMP_SECTION)
37072 substmt = cp_parser_omp_structured_block (parser, NULL);
37073 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37074 add_stmt (substmt);
37077 while (1)
37079 tok = cp_lexer_peek_token (parser->lexer);
37080 if (tok->type == CPP_CLOSE_BRACE)
37081 break;
37082 if (tok->type == CPP_EOF)
37083 break;
37085 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
37087 cp_lexer_consume_token (parser->lexer);
37088 cp_parser_require_pragma_eol (parser, tok);
37089 error_suppress = false;
37091 else if (!error_suppress)
37093 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
37094 error_suppress = true;
37097 substmt = cp_parser_omp_structured_block (parser, NULL);
37098 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37099 add_stmt (substmt);
37101 braces.require_close (parser);
37103 substmt = pop_stmt_list (stmt);
37105 stmt = make_node (OMP_SECTIONS);
37106 TREE_TYPE (stmt) = void_type_node;
37107 OMP_SECTIONS_BODY (stmt) = substmt;
37109 add_stmt (stmt);
37110 return stmt;
37113 /* OpenMP 2.5:
37114 # pragma omp sections sections-clause[optseq] newline
37115 sections-scope */
37117 #define OMP_SECTIONS_CLAUSE_MASK \
37118 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37124 static tree
37125 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
37126 char *p_name, omp_clause_mask mask, tree *cclauses)
37128 tree clauses, ret;
37129 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37131 strcat (p_name, " sections");
37132 mask |= OMP_SECTIONS_CLAUSE_MASK;
37133 if (cclauses)
37134 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37136 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37137 cclauses == NULL);
37138 if (cclauses)
37140 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
37141 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
37144 ret = cp_parser_omp_sections_scope (parser);
37145 if (ret)
37146 OMP_SECTIONS_CLAUSES (ret) = clauses;
37148 return ret;
37151 /* OpenMP 2.5:
37152 # pragma omp parallel parallel-clause[optseq] new-line
37153 structured-block
37154 # pragma omp parallel for parallel-for-clause[optseq] new-line
37155 structured-block
37156 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37157 structured-block
37159 OpenMP 4.0:
37160 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37161 structured-block */
37163 #define OMP_PARALLEL_CLAUSE_MASK \
37164 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37174 static tree
37175 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
37176 char *p_name, omp_clause_mask mask, tree *cclauses,
37177 bool *if_p)
37179 tree stmt, clauses, block;
37180 unsigned int save;
37181 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37183 strcat (p_name, " parallel");
37184 mask |= OMP_PARALLEL_CLAUSE_MASK;
37185 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37186 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
37187 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
37188 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
37190 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37192 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37193 if (cclauses == NULL)
37194 cclauses = cclauses_buf;
37196 cp_lexer_consume_token (parser->lexer);
37197 if (!flag_openmp) /* flag_openmp_simd */
37198 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37199 if_p);
37200 block = begin_omp_parallel ();
37201 save = cp_parser_begin_omp_structured_block (parser);
37202 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37203 if_p);
37204 cp_parser_end_omp_structured_block (parser, save);
37205 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37206 block);
37207 if (ret == NULL_TREE)
37208 return ret;
37209 OMP_PARALLEL_COMBINED (stmt) = 1;
37210 return stmt;
37212 /* When combined with distribute, parallel has to be followed by for.
37213 #pragma omp target parallel is allowed though. */
37214 else if (cclauses
37215 && (mask & (OMP_CLAUSE_MASK_1
37216 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37218 error_at (loc, "expected %<for%> after %qs", p_name);
37219 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37220 return NULL_TREE;
37222 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37224 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37225 const char *p = IDENTIFIER_POINTER (id);
37226 if (strcmp (p, "master") == 0)
37228 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37229 cclauses = cclauses_buf;
37231 cp_lexer_consume_token (parser->lexer);
37232 block = begin_omp_parallel ();
37233 save = cp_parser_begin_omp_structured_block (parser);
37234 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37235 cclauses, if_p);
37236 cp_parser_end_omp_structured_block (parser, save);
37237 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37238 block);
37239 OMP_PARALLEL_COMBINED (stmt) = 1;
37240 if (ret == NULL_TREE)
37241 return ret;
37242 return stmt;
37244 else if (!flag_openmp) /* flag_openmp_simd */
37246 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37247 return NULL_TREE;
37249 else if (strcmp (p, "sections") == 0)
37251 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37252 cclauses = cclauses_buf;
37254 cp_lexer_consume_token (parser->lexer);
37255 block = begin_omp_parallel ();
37256 save = cp_parser_begin_omp_structured_block (parser);
37257 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
37258 cp_parser_end_omp_structured_block (parser, save);
37259 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37260 block);
37261 OMP_PARALLEL_COMBINED (stmt) = 1;
37262 return stmt;
37265 else if (!flag_openmp) /* flag_openmp_simd */
37267 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37268 return NULL_TREE;
37271 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37272 cclauses == NULL);
37273 if (cclauses)
37275 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
37276 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
37279 block = begin_omp_parallel ();
37280 save = cp_parser_begin_omp_structured_block (parser);
37281 cp_parser_statement (parser, NULL_TREE, false, if_p);
37282 cp_parser_end_omp_structured_block (parser, save);
37283 stmt = finish_omp_parallel (clauses, block);
37284 return stmt;
37287 /* OpenMP 2.5:
37288 # pragma omp single single-clause[optseq] new-line
37289 structured-block */
37291 #define OMP_SINGLE_CLAUSE_MASK \
37292 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37297 static tree
37298 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37300 tree stmt = make_node (OMP_SINGLE);
37301 TREE_TYPE (stmt) = void_type_node;
37302 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37304 OMP_SINGLE_CLAUSES (stmt)
37305 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
37306 "#pragma omp single", pragma_tok);
37307 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37309 return add_stmt (stmt);
37312 /* OpenMP 3.0:
37313 # pragma omp task task-clause[optseq] new-line
37314 structured-block */
37316 #define OMP_TASK_CLAUSE_MASK \
37317 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37329 static tree
37330 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37332 tree clauses, block;
37333 unsigned int save;
37335 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
37336 "#pragma omp task", pragma_tok);
37337 block = begin_omp_task ();
37338 save = cp_parser_begin_omp_structured_block (parser);
37339 cp_parser_statement (parser, NULL_TREE, false, if_p);
37340 cp_parser_end_omp_structured_block (parser, save);
37341 return finish_omp_task (clauses, block);
37344 /* OpenMP 3.0:
37345 # pragma omp taskwait new-line
37347 OpenMP 5.0:
37348 # pragma omp taskwait taskwait-clause[opt] new-line */
37350 #define OMP_TASKWAIT_CLAUSE_MASK \
37351 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37353 static void
37354 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
37356 tree clauses
37357 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
37358 "#pragma omp taskwait", pragma_tok);
37360 if (clauses)
37362 tree stmt = make_node (OMP_TASK);
37363 TREE_TYPE (stmt) = void_node;
37364 OMP_TASK_CLAUSES (stmt) = clauses;
37365 OMP_TASK_BODY (stmt) = NULL_TREE;
37366 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37367 add_stmt (stmt);
37369 else
37370 finish_omp_taskwait ();
37373 /* OpenMP 3.1:
37374 # pragma omp taskyield new-line */
37376 static void
37377 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
37379 cp_parser_require_pragma_eol (parser, pragma_tok);
37380 finish_omp_taskyield ();
37383 /* OpenMP 4.0:
37384 # pragma omp taskgroup new-line
37385 structured-block
37387 OpenMP 5.0:
37388 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37390 #define OMP_TASKGROUP_CLAUSE_MASK \
37391 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37393 static tree
37394 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37396 tree clauses
37397 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
37398 "#pragma omp taskgroup", pragma_tok);
37399 return c_finish_omp_taskgroup (input_location,
37400 cp_parser_omp_structured_block (parser,
37401 if_p),
37402 clauses);
37406 /* OpenMP 2.5:
37407 # pragma omp threadprivate (variable-list) */
37409 static void
37410 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
37412 tree vars;
37414 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37415 cp_parser_require_pragma_eol (parser, pragma_tok);
37417 finish_omp_threadprivate (vars);
37420 /* OpenMP 4.0:
37421 # pragma omp cancel cancel-clause[optseq] new-line */
37423 #define OMP_CANCEL_CLAUSE_MASK \
37424 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37430 static void
37431 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
37433 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
37434 "#pragma omp cancel", pragma_tok);
37435 finish_omp_cancel (clauses);
37438 /* OpenMP 4.0:
37439 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37441 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37442 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37447 static void
37448 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
37449 enum pragma_context context)
37451 tree clauses;
37452 bool point_seen = false;
37454 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37456 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37457 const char *p = IDENTIFIER_POINTER (id);
37459 if (strcmp (p, "point") == 0)
37461 cp_lexer_consume_token (parser->lexer);
37462 point_seen = true;
37465 if (!point_seen)
37467 cp_parser_error (parser, "expected %<point%>");
37468 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37469 return;
37472 if (context != pragma_compound)
37474 if (context == pragma_stmt)
37475 error_at (pragma_tok->location,
37476 "%<#pragma %s%> may only be used in compound statements",
37477 "omp cancellation point");
37478 else
37479 cp_parser_error (parser, "expected declaration specifiers");
37480 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37481 return;
37484 clauses = cp_parser_omp_all_clauses (parser,
37485 OMP_CANCELLATION_POINT_CLAUSE_MASK,
37486 "#pragma omp cancellation point",
37487 pragma_tok);
37488 finish_omp_cancellation_point (clauses);
37491 /* OpenMP 4.0:
37492 #pragma omp distribute distribute-clause[optseq] new-line
37493 for-loop */
37495 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37496 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37502 static tree
37503 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
37504 char *p_name, omp_clause_mask mask, tree *cclauses,
37505 bool *if_p)
37507 tree clauses, sb, ret;
37508 unsigned int save;
37509 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37511 strcat (p_name, " distribute");
37512 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
37514 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37516 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37517 const char *p = IDENTIFIER_POINTER (id);
37518 bool simd = false;
37519 bool parallel = false;
37521 if (strcmp (p, "simd") == 0)
37522 simd = true;
37523 else
37524 parallel = strcmp (p, "parallel") == 0;
37525 if (parallel || simd)
37527 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37528 if (cclauses == NULL)
37529 cclauses = cclauses_buf;
37530 cp_lexer_consume_token (parser->lexer);
37531 if (!flag_openmp) /* flag_openmp_simd */
37533 if (simd)
37534 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37535 cclauses, if_p);
37536 else
37537 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37538 cclauses, if_p);
37540 sb = begin_omp_structured_block ();
37541 save = cp_parser_begin_omp_structured_block (parser);
37542 if (simd)
37543 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37544 cclauses, if_p);
37545 else
37546 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37547 cclauses, if_p);
37548 cp_parser_end_omp_structured_block (parser, save);
37549 tree body = finish_omp_structured_block (sb);
37550 if (ret == NULL)
37551 return ret;
37552 ret = make_node (OMP_DISTRIBUTE);
37553 TREE_TYPE (ret) = void_type_node;
37554 OMP_FOR_BODY (ret) = body;
37555 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37556 SET_EXPR_LOCATION (ret, loc);
37557 add_stmt (ret);
37558 return ret;
37561 if (!flag_openmp) /* flag_openmp_simd */
37563 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37564 return NULL_TREE;
37567 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37568 cclauses == NULL);
37569 if (cclauses)
37571 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
37572 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37575 keep_next_level (true);
37576 sb = begin_omp_structured_block ();
37577 save = cp_parser_begin_omp_structured_block (parser);
37579 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
37581 cp_parser_end_omp_structured_block (parser, save);
37582 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37584 return ret;
37587 /* OpenMP 4.0:
37588 # pragma omp teams teams-clause[optseq] new-line
37589 structured-block */
37591 #define OMP_TEAMS_CLAUSE_MASK \
37592 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37600 static tree
37601 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
37602 char *p_name, omp_clause_mask mask, tree *cclauses,
37603 bool *if_p)
37605 tree clauses, sb, ret;
37606 unsigned int save;
37607 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37609 strcat (p_name, " teams");
37610 mask |= OMP_TEAMS_CLAUSE_MASK;
37612 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37614 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37615 const char *p = IDENTIFIER_POINTER (id);
37616 if (strcmp (p, "distribute") == 0)
37618 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37619 if (cclauses == NULL)
37620 cclauses = cclauses_buf;
37622 cp_lexer_consume_token (parser->lexer);
37623 if (!flag_openmp) /* flag_openmp_simd */
37624 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37625 cclauses, if_p);
37626 keep_next_level (true);
37627 sb = begin_omp_structured_block ();
37628 save = cp_parser_begin_omp_structured_block (parser);
37629 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37630 cclauses, if_p);
37631 cp_parser_end_omp_structured_block (parser, save);
37632 tree body = finish_omp_structured_block (sb);
37633 if (ret == NULL)
37634 return ret;
37635 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37636 ret = make_node (OMP_TEAMS);
37637 TREE_TYPE (ret) = void_type_node;
37638 OMP_TEAMS_CLAUSES (ret) = clauses;
37639 OMP_TEAMS_BODY (ret) = body;
37640 OMP_TEAMS_COMBINED (ret) = 1;
37641 SET_EXPR_LOCATION (ret, loc);
37642 return add_stmt (ret);
37645 if (!flag_openmp) /* flag_openmp_simd */
37647 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37648 return NULL_TREE;
37651 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37652 cclauses == NULL);
37653 if (cclauses)
37655 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
37656 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37659 tree stmt = make_node (OMP_TEAMS);
37660 TREE_TYPE (stmt) = void_type_node;
37661 OMP_TEAMS_CLAUSES (stmt) = clauses;
37662 keep_next_level (true);
37663 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37664 SET_EXPR_LOCATION (stmt, loc);
37666 return add_stmt (stmt);
37669 /* OpenMP 4.0:
37670 # pragma omp target data target-data-clause[optseq] new-line
37671 structured-block */
37673 #define OMP_TARGET_DATA_CLAUSE_MASK \
37674 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
37679 static tree
37680 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37682 tree clauses
37683 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
37684 "#pragma omp target data", pragma_tok);
37685 int map_seen = 0;
37686 for (tree *pc = &clauses; *pc;)
37688 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37689 switch (OMP_CLAUSE_MAP_KIND (*pc))
37691 case GOMP_MAP_TO:
37692 case GOMP_MAP_ALWAYS_TO:
37693 case GOMP_MAP_FROM:
37694 case GOMP_MAP_ALWAYS_FROM:
37695 case GOMP_MAP_TOFROM:
37696 case GOMP_MAP_ALWAYS_TOFROM:
37697 case GOMP_MAP_ALLOC:
37698 map_seen = 3;
37699 break;
37700 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37701 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37702 case GOMP_MAP_ALWAYS_POINTER:
37703 break;
37704 default:
37705 map_seen |= 1;
37706 error_at (OMP_CLAUSE_LOCATION (*pc),
37707 "%<#pragma omp target data%> with map-type other "
37708 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37709 "on %<map%> clause");
37710 *pc = OMP_CLAUSE_CHAIN (*pc);
37711 continue;
37713 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
37714 map_seen = 3;
37715 pc = &OMP_CLAUSE_CHAIN (*pc);
37718 if (map_seen != 3)
37720 if (map_seen == 0)
37721 error_at (pragma_tok->location,
37722 "%<#pragma omp target data%> must contain at least "
37723 "one %<map%> or %<use_device_ptr%> clause");
37724 return NULL_TREE;
37727 tree stmt = make_node (OMP_TARGET_DATA);
37728 TREE_TYPE (stmt) = void_type_node;
37729 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
37731 keep_next_level (true);
37732 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37734 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37735 return add_stmt (stmt);
37738 /* OpenMP 4.5:
37739 # pragma omp target enter data target-enter-data-clause[optseq] new-line
37740 structured-block */
37742 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
37743 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37749 static tree
37750 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
37751 enum pragma_context context)
37753 bool data_seen = false;
37754 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37756 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37757 const char *p = IDENTIFIER_POINTER (id);
37759 if (strcmp (p, "data") == 0)
37761 cp_lexer_consume_token (parser->lexer);
37762 data_seen = true;
37765 if (!data_seen)
37767 cp_parser_error (parser, "expected %<data%>");
37768 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37769 return NULL_TREE;
37772 if (context == pragma_stmt)
37774 error_at (pragma_tok->location,
37775 "%<#pragma %s%> may only be used in compound statements",
37776 "omp target enter data");
37777 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37778 return NULL_TREE;
37781 tree clauses
37782 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
37783 "#pragma omp target enter data", pragma_tok);
37784 int map_seen = 0;
37785 for (tree *pc = &clauses; *pc;)
37787 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37788 switch (OMP_CLAUSE_MAP_KIND (*pc))
37790 case GOMP_MAP_TO:
37791 case GOMP_MAP_ALWAYS_TO:
37792 case GOMP_MAP_ALLOC:
37793 map_seen = 3;
37794 break;
37795 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37796 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37797 case GOMP_MAP_ALWAYS_POINTER:
37798 break;
37799 default:
37800 map_seen |= 1;
37801 error_at (OMP_CLAUSE_LOCATION (*pc),
37802 "%<#pragma omp target enter data%> with map-type other "
37803 "than %<to%> or %<alloc%> on %<map%> clause");
37804 *pc = OMP_CLAUSE_CHAIN (*pc);
37805 continue;
37807 pc = &OMP_CLAUSE_CHAIN (*pc);
37810 if (map_seen != 3)
37812 if (map_seen == 0)
37813 error_at (pragma_tok->location,
37814 "%<#pragma omp target enter data%> must contain at least "
37815 "one %<map%> clause");
37816 return NULL_TREE;
37819 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
37820 TREE_TYPE (stmt) = void_type_node;
37821 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
37822 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37823 return add_stmt (stmt);
37826 /* OpenMP 4.5:
37827 # pragma omp target exit data target-enter-data-clause[optseq] new-line
37828 structured-block */
37830 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
37831 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37837 static tree
37838 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
37839 enum pragma_context context)
37841 bool data_seen = false;
37842 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37844 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37845 const char *p = IDENTIFIER_POINTER (id);
37847 if (strcmp (p, "data") == 0)
37849 cp_lexer_consume_token (parser->lexer);
37850 data_seen = true;
37853 if (!data_seen)
37855 cp_parser_error (parser, "expected %<data%>");
37856 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37857 return NULL_TREE;
37860 if (context == pragma_stmt)
37862 error_at (pragma_tok->location,
37863 "%<#pragma %s%> may only be used in compound statements",
37864 "omp target exit data");
37865 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37866 return NULL_TREE;
37869 tree clauses
37870 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
37871 "#pragma omp target exit data", pragma_tok);
37872 int map_seen = 0;
37873 for (tree *pc = &clauses; *pc;)
37875 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37876 switch (OMP_CLAUSE_MAP_KIND (*pc))
37878 case GOMP_MAP_FROM:
37879 case GOMP_MAP_ALWAYS_FROM:
37880 case GOMP_MAP_RELEASE:
37881 case GOMP_MAP_DELETE:
37882 map_seen = 3;
37883 break;
37884 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37885 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37886 case GOMP_MAP_ALWAYS_POINTER:
37887 break;
37888 default:
37889 map_seen |= 1;
37890 error_at (OMP_CLAUSE_LOCATION (*pc),
37891 "%<#pragma omp target exit data%> with map-type other "
37892 "than %<from%>, %<release%> or %<delete%> on %<map%>"
37893 " clause");
37894 *pc = OMP_CLAUSE_CHAIN (*pc);
37895 continue;
37897 pc = &OMP_CLAUSE_CHAIN (*pc);
37900 if (map_seen != 3)
37902 if (map_seen == 0)
37903 error_at (pragma_tok->location,
37904 "%<#pragma omp target exit data%> must contain at least "
37905 "one %<map%> clause");
37906 return NULL_TREE;
37909 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
37910 TREE_TYPE (stmt) = void_type_node;
37911 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
37912 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37913 return add_stmt (stmt);
37916 /* OpenMP 4.0:
37917 # pragma omp target update target-update-clause[optseq] new-line */
37919 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
37920 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
37921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37927 static bool
37928 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
37929 enum pragma_context context)
37931 if (context == pragma_stmt)
37933 error_at (pragma_tok->location,
37934 "%<#pragma %s%> may only be used in compound statements",
37935 "omp target update");
37936 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37937 return false;
37940 tree clauses
37941 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
37942 "#pragma omp target update", pragma_tok);
37943 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
37944 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
37946 error_at (pragma_tok->location,
37947 "%<#pragma omp target update%> must contain at least one "
37948 "%<from%> or %<to%> clauses");
37949 return false;
37952 tree stmt = make_node (OMP_TARGET_UPDATE);
37953 TREE_TYPE (stmt) = void_type_node;
37954 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
37955 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37956 add_stmt (stmt);
37957 return false;
37960 /* OpenMP 4.0:
37961 # pragma omp target target-clause[optseq] new-line
37962 structured-block */
37964 #define OMP_TARGET_CLAUSE_MASK \
37965 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
37973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
37975 static bool
37976 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
37977 enum pragma_context context, bool *if_p)
37979 tree *pc = NULL, stmt;
37981 if (flag_openmp)
37982 omp_requires_mask
37983 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
37985 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37987 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37988 const char *p = IDENTIFIER_POINTER (id);
37989 enum tree_code ccode = ERROR_MARK;
37991 if (strcmp (p, "teams") == 0)
37992 ccode = OMP_TEAMS;
37993 else if (strcmp (p, "parallel") == 0)
37994 ccode = OMP_PARALLEL;
37995 else if (strcmp (p, "simd") == 0)
37996 ccode = OMP_SIMD;
37997 if (ccode != ERROR_MARK)
37999 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
38000 char p_name[sizeof ("#pragma omp target teams distribute "
38001 "parallel for simd")];
38003 cp_lexer_consume_token (parser->lexer);
38004 strcpy (p_name, "#pragma omp target");
38005 if (!flag_openmp) /* flag_openmp_simd */
38007 tree stmt;
38008 switch (ccode)
38010 case OMP_TEAMS:
38011 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
38012 OMP_TARGET_CLAUSE_MASK,
38013 cclauses, if_p);
38014 break;
38015 case OMP_PARALLEL:
38016 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38017 OMP_TARGET_CLAUSE_MASK,
38018 cclauses, if_p);
38019 break;
38020 case OMP_SIMD:
38021 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
38022 OMP_TARGET_CLAUSE_MASK,
38023 cclauses, if_p);
38024 break;
38025 default:
38026 gcc_unreachable ();
38028 return stmt != NULL_TREE;
38030 keep_next_level (true);
38031 tree sb = begin_omp_structured_block (), ret;
38032 unsigned save = cp_parser_begin_omp_structured_block (parser);
38033 switch (ccode)
38035 case OMP_TEAMS:
38036 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
38037 OMP_TARGET_CLAUSE_MASK, cclauses,
38038 if_p);
38039 break;
38040 case OMP_PARALLEL:
38041 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38042 OMP_TARGET_CLAUSE_MASK, cclauses,
38043 if_p);
38044 break;
38045 case OMP_SIMD:
38046 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
38047 OMP_TARGET_CLAUSE_MASK, cclauses,
38048 if_p);
38049 break;
38050 default:
38051 gcc_unreachable ();
38053 cp_parser_end_omp_structured_block (parser, save);
38054 tree body = finish_omp_structured_block (sb);
38055 if (ret == NULL_TREE)
38056 return false;
38057 if (ccode == OMP_TEAMS && !processing_template_decl)
38059 /* For combined target teams, ensure the num_teams and
38060 thread_limit clause expressions are evaluated on the host,
38061 before entering the target construct. */
38062 tree c;
38063 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38064 c; c = OMP_CLAUSE_CHAIN (c))
38065 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
38066 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
38067 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
38069 tree expr = OMP_CLAUSE_OPERAND (c, 0);
38070 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
38071 if (expr == error_mark_node)
38072 continue;
38073 tree tmp = TARGET_EXPR_SLOT (expr);
38074 add_stmt (expr);
38075 OMP_CLAUSE_OPERAND (c, 0) = expr;
38076 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
38077 OMP_CLAUSE_FIRSTPRIVATE);
38078 OMP_CLAUSE_DECL (tc) = tmp;
38079 OMP_CLAUSE_CHAIN (tc)
38080 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38081 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
38084 tree stmt = make_node (OMP_TARGET);
38085 TREE_TYPE (stmt) = void_type_node;
38086 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38087 OMP_TARGET_BODY (stmt) = body;
38088 OMP_TARGET_COMBINED (stmt) = 1;
38089 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38090 add_stmt (stmt);
38091 pc = &OMP_TARGET_CLAUSES (stmt);
38092 goto check_clauses;
38094 else if (!flag_openmp) /* flag_openmp_simd */
38096 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38097 return false;
38099 else if (strcmp (p, "data") == 0)
38101 cp_lexer_consume_token (parser->lexer);
38102 cp_parser_omp_target_data (parser, pragma_tok, if_p);
38103 return true;
38105 else if (strcmp (p, "enter") == 0)
38107 cp_lexer_consume_token (parser->lexer);
38108 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
38109 return false;
38111 else if (strcmp (p, "exit") == 0)
38113 cp_lexer_consume_token (parser->lexer);
38114 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
38115 return false;
38117 else if (strcmp (p, "update") == 0)
38119 cp_lexer_consume_token (parser->lexer);
38120 return cp_parser_omp_target_update (parser, pragma_tok, context);
38123 if (!flag_openmp) /* flag_openmp_simd */
38125 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38126 return false;
38129 stmt = make_node (OMP_TARGET);
38130 TREE_TYPE (stmt) = void_type_node;
38132 OMP_TARGET_CLAUSES (stmt)
38133 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
38134 "#pragma omp target", pragma_tok);
38135 pc = &OMP_TARGET_CLAUSES (stmt);
38136 keep_next_level (true);
38137 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38139 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38140 add_stmt (stmt);
38142 check_clauses:
38143 while (*pc)
38145 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38146 switch (OMP_CLAUSE_MAP_KIND (*pc))
38148 case GOMP_MAP_TO:
38149 case GOMP_MAP_ALWAYS_TO:
38150 case GOMP_MAP_FROM:
38151 case GOMP_MAP_ALWAYS_FROM:
38152 case GOMP_MAP_TOFROM:
38153 case GOMP_MAP_ALWAYS_TOFROM:
38154 case GOMP_MAP_ALLOC:
38155 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38156 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38157 case GOMP_MAP_ALWAYS_POINTER:
38158 break;
38159 default:
38160 error_at (OMP_CLAUSE_LOCATION (*pc),
38161 "%<#pragma omp target%> with map-type other "
38162 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38163 "on %<map%> clause");
38164 *pc = OMP_CLAUSE_CHAIN (*pc);
38165 continue;
38167 pc = &OMP_CLAUSE_CHAIN (*pc);
38169 return true;
38172 /* OpenACC 2.0:
38173 # pragma acc cache (variable-list) new-line
38176 static tree
38177 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
38179 tree stmt, clauses;
38181 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
38182 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38184 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
38186 stmt = make_node (OACC_CACHE);
38187 TREE_TYPE (stmt) = void_type_node;
38188 OACC_CACHE_CLAUSES (stmt) = clauses;
38189 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38190 add_stmt (stmt);
38192 return stmt;
38195 /* OpenACC 2.0:
38196 # pragma acc data oacc-data-clause[optseq] new-line
38197 structured-block */
38199 #define OACC_DATA_CLAUSE_MASK \
38200 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38208 static tree
38209 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38211 tree stmt, clauses, block;
38212 unsigned int save;
38214 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
38215 "#pragma acc data", pragma_tok);
38217 block = begin_omp_parallel ();
38218 save = cp_parser_begin_omp_structured_block (parser);
38219 cp_parser_statement (parser, NULL_TREE, false, if_p);
38220 cp_parser_end_omp_structured_block (parser, save);
38221 stmt = finish_oacc_data (clauses, block);
38222 return stmt;
38225 /* OpenACC 2.0:
38226 # pragma acc host_data <clauses> new-line
38227 structured-block */
38229 #define OACC_HOST_DATA_CLAUSE_MASK \
38230 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38232 static tree
38233 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38235 tree stmt, clauses, block;
38236 unsigned int save;
38238 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
38239 "#pragma acc host_data", pragma_tok);
38241 block = begin_omp_parallel ();
38242 save = cp_parser_begin_omp_structured_block (parser);
38243 cp_parser_statement (parser, NULL_TREE, false, if_p);
38244 cp_parser_end_omp_structured_block (parser, save);
38245 stmt = finish_oacc_host_data (clauses, block);
38246 return stmt;
38249 /* OpenACC 2.0:
38250 # pragma acc declare oacc-data-clause[optseq] new-line
38253 #define OACC_DECLARE_CLAUSE_MASK \
38254 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38263 static tree
38264 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
38266 tree clauses, stmt;
38267 bool error = false;
38269 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
38270 "#pragma acc declare", pragma_tok, true);
38273 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38275 error_at (pragma_tok->location,
38276 "no valid clauses specified in %<#pragma acc declare%>");
38277 return NULL_TREE;
38280 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
38282 location_t loc = OMP_CLAUSE_LOCATION (t);
38283 tree decl = OMP_CLAUSE_DECL (t);
38284 if (!DECL_P (decl))
38286 error_at (loc, "array section in %<#pragma acc declare%>");
38287 error = true;
38288 continue;
38290 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
38291 switch (OMP_CLAUSE_MAP_KIND (t))
38293 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38294 case GOMP_MAP_ALLOC:
38295 case GOMP_MAP_TO:
38296 case GOMP_MAP_FORCE_DEVICEPTR:
38297 case GOMP_MAP_DEVICE_RESIDENT:
38298 break;
38300 case GOMP_MAP_LINK:
38301 if (!global_bindings_p ()
38302 && (TREE_STATIC (decl)
38303 || !DECL_EXTERNAL (decl)))
38305 error_at (loc,
38306 "%qD must be a global variable in "
38307 "%<#pragma acc declare link%>",
38308 decl);
38309 error = true;
38310 continue;
38312 break;
38314 default:
38315 if (global_bindings_p ())
38317 error_at (loc, "invalid OpenACC clause at file scope");
38318 error = true;
38319 continue;
38321 if (DECL_EXTERNAL (decl))
38323 error_at (loc,
38324 "invalid use of %<extern%> variable %qD "
38325 "in %<#pragma acc declare%>", decl);
38326 error = true;
38327 continue;
38329 else if (TREE_PUBLIC (decl))
38331 error_at (loc,
38332 "invalid use of %<global%> variable %qD "
38333 "in %<#pragma acc declare%>", decl);
38334 error = true;
38335 continue;
38337 break;
38340 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
38341 || lookup_attribute ("omp declare target link",
38342 DECL_ATTRIBUTES (decl)))
38344 error_at (loc, "variable %qD used more than once with "
38345 "%<#pragma acc declare%>", decl);
38346 error = true;
38347 continue;
38350 if (!error)
38352 tree id;
38354 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
38355 id = get_identifier ("omp declare target link");
38356 else
38357 id = get_identifier ("omp declare target");
38359 DECL_ATTRIBUTES (decl)
38360 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
38361 if (global_bindings_p ())
38363 symtab_node *node = symtab_node::get (decl);
38364 if (node != NULL)
38366 node->offloadable = 1;
38367 if (ENABLE_OFFLOADING)
38369 g->have_offload = true;
38370 if (is_a <varpool_node *> (node))
38371 vec_safe_push (offload_vars, decl);
38378 if (error || global_bindings_p ())
38379 return NULL_TREE;
38381 stmt = make_node (OACC_DECLARE);
38382 TREE_TYPE (stmt) = void_type_node;
38383 OACC_DECLARE_CLAUSES (stmt) = clauses;
38384 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38386 add_stmt (stmt);
38388 return NULL_TREE;
38391 /* OpenACC 2.0:
38392 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38396 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38398 LOC is the location of the #pragma token.
38401 #define OACC_ENTER_DATA_CLAUSE_MASK \
38402 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38408 #define OACC_EXIT_DATA_CLAUSE_MASK \
38409 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38416 static tree
38417 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
38418 bool enter)
38420 location_t loc = pragma_tok->location;
38421 tree stmt, clauses;
38422 const char *p = "";
38424 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38425 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38427 if (strcmp (p, "data") != 0)
38429 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
38430 enter ? "enter" : "exit");
38431 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38432 return NULL_TREE;
38435 cp_lexer_consume_token (parser->lexer);
38437 if (enter)
38438 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
38439 "#pragma acc enter data", pragma_tok);
38440 else
38441 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
38442 "#pragma acc exit data", pragma_tok);
38444 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38446 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
38447 enter ? "enter" : "exit");
38448 return NULL_TREE;
38451 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
38452 TREE_TYPE (stmt) = void_type_node;
38453 OMP_STANDALONE_CLAUSES (stmt) = clauses;
38454 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38455 add_stmt (stmt);
38456 return stmt;
38459 /* OpenACC 2.0:
38460 # pragma acc loop oacc-loop-clause[optseq] new-line
38461 structured-block */
38463 #define OACC_LOOP_CLAUSE_MASK \
38464 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38475 static tree
38476 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
38477 omp_clause_mask mask, tree *cclauses, bool *if_p)
38479 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
38481 strcat (p_name, " loop");
38482 mask |= OACC_LOOP_CLAUSE_MASK;
38484 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
38485 cclauses == NULL);
38486 if (cclauses)
38488 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
38489 if (*cclauses)
38490 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
38491 if (clauses)
38492 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38495 tree block = begin_omp_structured_block ();
38496 int save = cp_parser_begin_omp_structured_block (parser);
38497 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
38498 cp_parser_end_omp_structured_block (parser, save);
38499 add_stmt (finish_omp_structured_block (block));
38501 return stmt;
38504 /* OpenACC 2.0:
38505 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38506 structured-block
38510 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38511 structured-block
38514 #define OACC_KERNELS_CLAUSE_MASK \
38515 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38529 #define OACC_PARALLEL_CLAUSE_MASK \
38530 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38547 static tree
38548 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
38549 char *p_name, bool *if_p)
38551 omp_clause_mask mask;
38552 enum tree_code code;
38553 switch (cp_parser_pragma_kind (pragma_tok))
38555 case PRAGMA_OACC_KERNELS:
38556 strcat (p_name, " kernels");
38557 mask = OACC_KERNELS_CLAUSE_MASK;
38558 code = OACC_KERNELS;
38559 break;
38560 case PRAGMA_OACC_PARALLEL:
38561 strcat (p_name, " parallel");
38562 mask = OACC_PARALLEL_CLAUSE_MASK;
38563 code = OACC_PARALLEL;
38564 break;
38565 default:
38566 gcc_unreachable ();
38569 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38571 const char *p
38572 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38573 if (strcmp (p, "loop") == 0)
38575 cp_lexer_consume_token (parser->lexer);
38576 tree block = begin_omp_parallel ();
38577 tree clauses;
38578 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
38579 if_p);
38580 return finish_omp_construct (code, block, clauses);
38584 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
38586 tree block = begin_omp_parallel ();
38587 unsigned int save = cp_parser_begin_omp_structured_block (parser);
38588 cp_parser_statement (parser, NULL_TREE, false, if_p);
38589 cp_parser_end_omp_structured_block (parser, save);
38590 return finish_omp_construct (code, block, clauses);
38593 /* OpenACC 2.0:
38594 # pragma acc update oacc-update-clause[optseq] new-line
38597 #define OACC_UPDATE_CLAUSE_MASK \
38598 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38605 static tree
38606 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
38608 tree stmt, clauses;
38610 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
38611 "#pragma acc update", pragma_tok);
38613 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38615 error_at (pragma_tok->location,
38616 "%<#pragma acc update%> must contain at least one "
38617 "%<device%> or %<host%> or %<self%> clause");
38618 return NULL_TREE;
38621 stmt = make_node (OACC_UPDATE);
38622 TREE_TYPE (stmt) = void_type_node;
38623 OACC_UPDATE_CLAUSES (stmt) = clauses;
38624 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38625 add_stmt (stmt);
38626 return stmt;
38629 /* OpenACC 2.0:
38630 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38632 LOC is the location of the #pragma token.
38635 #define OACC_WAIT_CLAUSE_MASK \
38636 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
38638 static tree
38639 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
38641 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
38642 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38644 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38645 list = cp_parser_oacc_wait_list (parser, loc, list);
38647 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
38648 "#pragma acc wait", pragma_tok);
38650 stmt = c_finish_oacc_wait (loc, list, clauses);
38651 stmt = finish_expr_stmt (stmt);
38653 return stmt;
38656 /* OpenMP 4.0:
38657 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
38659 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
38660 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
38664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
38665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
38667 static void
38668 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
38669 enum pragma_context context)
38671 bool first_p = parser->omp_declare_simd == NULL;
38672 cp_omp_declare_simd_data data;
38673 if (first_p)
38675 data.error_seen = false;
38676 data.fndecl_seen = false;
38677 data.tokens = vNULL;
38678 data.clauses = NULL_TREE;
38679 /* It is safe to take the address of a local variable; it will only be
38680 used while this scope is live. */
38681 parser->omp_declare_simd = &data;
38684 /* Store away all pragma tokens. */
38685 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38686 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38687 cp_lexer_consume_token (parser->lexer);
38688 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38689 parser->omp_declare_simd->error_seen = true;
38690 cp_parser_require_pragma_eol (parser, pragma_tok);
38691 struct cp_token_cache *cp
38692 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38693 parser->omp_declare_simd->tokens.safe_push (cp);
38695 if (first_p)
38697 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38698 cp_parser_pragma (parser, context, NULL);
38699 switch (context)
38701 case pragma_external:
38702 cp_parser_declaration (parser);
38703 break;
38704 case pragma_member:
38705 cp_parser_member_declaration (parser);
38706 break;
38707 case pragma_objc_icode:
38708 cp_parser_block_declaration (parser, /*statement_p=*/false);
38709 break;
38710 default:
38711 cp_parser_declaration_statement (parser);
38712 break;
38714 if (parser->omp_declare_simd
38715 && !parser->omp_declare_simd->error_seen
38716 && !parser->omp_declare_simd->fndecl_seen)
38717 error_at (pragma_tok->location,
38718 "%<#pragma omp declare simd%> not immediately followed by "
38719 "function declaration or definition");
38720 data.tokens.release ();
38721 parser->omp_declare_simd = NULL;
38725 /* Finalize #pragma omp declare simd clauses after direct declarator has
38726 been parsed, and put that into "omp declare simd" attribute. */
38728 static tree
38729 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
38731 struct cp_token_cache *ce;
38732 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
38733 int i;
38735 if (!data->error_seen && data->fndecl_seen)
38737 error ("%<#pragma omp declare simd%> not immediately followed by "
38738 "a single function declaration or definition");
38739 data->error_seen = true;
38741 if (data->error_seen)
38742 return attrs;
38744 FOR_EACH_VEC_ELT (data->tokens, i, ce)
38746 tree c, cl;
38748 cp_parser_push_lexer_for_tokens (parser, ce);
38749 parser->lexer->in_pragma = true;
38750 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38751 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38752 cp_lexer_consume_token (parser->lexer);
38753 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
38754 "#pragma omp declare simd", pragma_tok);
38755 cp_parser_pop_lexer (parser);
38756 if (cl)
38757 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
38758 c = build_tree_list (get_identifier ("omp declare simd"), cl);
38759 TREE_CHAIN (c) = attrs;
38760 if (processing_template_decl)
38761 ATTR_IS_DEPENDENT (c) = 1;
38762 attrs = c;
38765 data->fndecl_seen = true;
38766 return attrs;
38770 /* OpenMP 4.0:
38771 # pragma omp declare target new-line
38772 declarations and definitions
38773 # pragma omp end declare target new-line
38775 OpenMP 4.5:
38776 # pragma omp declare target ( extended-list ) new-line
38778 # pragma omp declare target declare-target-clauses[seq] new-line */
38780 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
38781 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
38784 static void
38785 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
38787 tree clauses = NULL_TREE;
38788 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38789 clauses
38790 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
38791 "#pragma omp declare target", pragma_tok);
38792 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38794 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
38795 clauses);
38796 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
38797 cp_parser_require_pragma_eol (parser, pragma_tok);
38799 else
38801 cp_parser_require_pragma_eol (parser, pragma_tok);
38802 scope_chain->omp_declare_target_attribute++;
38803 return;
38805 if (scope_chain->omp_declare_target_attribute)
38806 error_at (pragma_tok->location,
38807 "%<#pragma omp declare target%> with clauses in between "
38808 "%<#pragma omp declare target%> without clauses and "
38809 "%<#pragma omp end declare target%>");
38810 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
38812 tree t = OMP_CLAUSE_DECL (c), id;
38813 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
38814 tree at2 = lookup_attribute ("omp declare target link",
38815 DECL_ATTRIBUTES (t));
38816 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
38818 id = get_identifier ("omp declare target link");
38819 std::swap (at1, at2);
38821 else
38822 id = get_identifier ("omp declare target");
38823 if (at2)
38825 error_at (OMP_CLAUSE_LOCATION (c),
38826 "%qD specified both in declare target %<link%> and %<to%>"
38827 " clauses", t);
38828 continue;
38830 if (!at1)
38832 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
38833 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
38834 continue;
38836 symtab_node *node = symtab_node::get (t);
38837 if (node != NULL)
38839 node->offloadable = 1;
38840 if (ENABLE_OFFLOADING)
38842 g->have_offload = true;
38843 if (is_a <varpool_node *> (node))
38844 vec_safe_push (offload_vars, t);
38851 static void
38852 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
38854 const char *p = "";
38855 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38857 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38858 p = IDENTIFIER_POINTER (id);
38860 if (strcmp (p, "declare") == 0)
38862 cp_lexer_consume_token (parser->lexer);
38863 p = "";
38864 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38866 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38867 p = IDENTIFIER_POINTER (id);
38869 if (strcmp (p, "target") == 0)
38870 cp_lexer_consume_token (parser->lexer);
38871 else
38873 cp_parser_error (parser, "expected %<target%>");
38874 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38875 return;
38878 else
38880 cp_parser_error (parser, "expected %<declare%>");
38881 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38882 return;
38884 cp_parser_require_pragma_eol (parser, pragma_tok);
38885 if (!scope_chain->omp_declare_target_attribute)
38886 error_at (pragma_tok->location,
38887 "%<#pragma omp end declare target%> without corresponding "
38888 "%<#pragma omp declare target%>");
38889 else
38890 scope_chain->omp_declare_target_attribute--;
38893 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
38894 expression and optional initializer clause of
38895 #pragma omp declare reduction. We store the expression(s) as
38896 either 3, 6 or 7 special statements inside of the artificial function's
38897 body. The first two statements are DECL_EXPRs for the artificial
38898 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
38899 expression that uses those variables.
38900 If there was any INITIALIZER clause, this is followed by further statements,
38901 the fourth and fifth statements are DECL_EXPRs for the artificial
38902 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
38903 constructor variant (first token after open paren is not omp_priv),
38904 then the sixth statement is a statement with the function call expression
38905 that uses the OMP_PRIV and optionally OMP_ORIG variable.
38906 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
38907 to initialize the OMP_PRIV artificial variable and there is seventh
38908 statement, a DECL_EXPR of the OMP_PRIV statement again. */
38910 static bool
38911 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
38913 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
38914 gcc_assert (TYPE_REF_P (type));
38915 type = TREE_TYPE (type);
38916 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
38917 DECL_ARTIFICIAL (omp_out) = 1;
38918 pushdecl (omp_out);
38919 add_decl_expr (omp_out);
38920 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
38921 DECL_ARTIFICIAL (omp_in) = 1;
38922 pushdecl (omp_in);
38923 add_decl_expr (omp_in);
38924 tree combiner;
38925 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
38927 keep_next_level (true);
38928 tree block = begin_omp_structured_block ();
38929 combiner = cp_parser_expression (parser);
38930 finish_expr_stmt (combiner);
38931 block = finish_omp_structured_block (block);
38932 add_stmt (block);
38934 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38935 return false;
38937 const char *p = "";
38938 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38940 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38941 p = IDENTIFIER_POINTER (id);
38944 if (strcmp (p, "initializer") == 0)
38946 cp_lexer_consume_token (parser->lexer);
38947 matching_parens parens;
38948 if (!parens.require_open (parser))
38949 return false;
38951 p = "";
38952 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38954 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38955 p = IDENTIFIER_POINTER (id);
38958 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
38959 DECL_ARTIFICIAL (omp_priv) = 1;
38960 pushdecl (omp_priv);
38961 add_decl_expr (omp_priv);
38962 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
38963 DECL_ARTIFICIAL (omp_orig) = 1;
38964 pushdecl (omp_orig);
38965 add_decl_expr (omp_orig);
38967 keep_next_level (true);
38968 block = begin_omp_structured_block ();
38970 bool ctor = false;
38971 if (strcmp (p, "omp_priv") == 0)
38973 bool is_direct_init, is_non_constant_init;
38974 ctor = true;
38975 cp_lexer_consume_token (parser->lexer);
38976 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
38977 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
38978 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
38979 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
38980 == CPP_CLOSE_PAREN
38981 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
38982 == CPP_CLOSE_PAREN))
38984 finish_omp_structured_block (block);
38985 error ("invalid initializer clause");
38986 return false;
38988 initializer = cp_parser_initializer (parser, &is_direct_init,
38989 &is_non_constant_init);
38990 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
38991 NULL_TREE, LOOKUP_ONLYCONVERTING);
38993 else
38995 cp_parser_parse_tentatively (parser);
38996 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
38997 /*check_dependency_p=*/true,
38998 /*template_p=*/NULL,
38999 /*declarator_p=*/false,
39000 /*optional_p=*/false);
39001 vec<tree, va_gc> *args;
39002 if (fn_name == error_mark_node
39003 || cp_parser_error_occurred (parser)
39004 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39005 || ((args = cp_parser_parenthesized_expression_list
39006 (parser, non_attr, /*cast_p=*/false,
39007 /*allow_expansion_p=*/true,
39008 /*non_constant_p=*/NULL)),
39009 cp_parser_error_occurred (parser)))
39011 finish_omp_structured_block (block);
39012 cp_parser_abort_tentative_parse (parser);
39013 cp_parser_error (parser, "expected id-expression (arguments)");
39014 return false;
39016 unsigned int i;
39017 tree arg;
39018 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
39019 if (arg == omp_priv
39020 || (TREE_CODE (arg) == ADDR_EXPR
39021 && TREE_OPERAND (arg, 0) == omp_priv))
39022 break;
39023 cp_parser_abort_tentative_parse (parser);
39024 if (arg == NULL_TREE)
39025 error ("one of the initializer call arguments should be %<omp_priv%>"
39026 " or %<&omp_priv%>");
39027 initializer = cp_parser_postfix_expression (parser, false, false, false,
39028 false, NULL);
39029 finish_expr_stmt (initializer);
39032 block = finish_omp_structured_block (block);
39033 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
39034 add_stmt (block);
39036 if (ctor)
39037 add_decl_expr (omp_orig);
39039 if (!parens.require_close (parser))
39040 return false;
39043 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
39044 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
39045 UNKNOWN_LOCATION);
39047 return true;
39050 /* OpenMP 4.0
39051 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39052 initializer-clause[opt] new-line
39054 initializer-clause:
39055 initializer (omp_priv initializer)
39056 initializer (function-name (argument-list)) */
39058 static void
39059 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
39060 enum pragma_context)
39062 auto_vec<tree> types;
39063 enum tree_code reduc_code = ERROR_MARK;
39064 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
39065 unsigned int i;
39066 cp_token *first_token;
39067 cp_token_cache *cp;
39068 int errs;
39069 void *p;
39071 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39072 p = obstack_alloc (&declarator_obstack, 0);
39074 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39075 goto fail;
39077 switch (cp_lexer_peek_token (parser->lexer)->type)
39079 case CPP_PLUS:
39080 reduc_code = PLUS_EXPR;
39081 break;
39082 case CPP_MULT:
39083 reduc_code = MULT_EXPR;
39084 break;
39085 case CPP_MINUS:
39086 reduc_code = MINUS_EXPR;
39087 break;
39088 case CPP_AND:
39089 reduc_code = BIT_AND_EXPR;
39090 break;
39091 case CPP_XOR:
39092 reduc_code = BIT_XOR_EXPR;
39093 break;
39094 case CPP_OR:
39095 reduc_code = BIT_IOR_EXPR;
39096 break;
39097 case CPP_AND_AND:
39098 reduc_code = TRUTH_ANDIF_EXPR;
39099 break;
39100 case CPP_OR_OR:
39101 reduc_code = TRUTH_ORIF_EXPR;
39102 break;
39103 case CPP_NAME:
39104 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
39105 break;
39106 default:
39107 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39108 "%<|%>, %<&&%>, %<||%> or identifier");
39109 goto fail;
39112 if (reduc_code != ERROR_MARK)
39113 cp_lexer_consume_token (parser->lexer);
39115 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
39116 if (reduc_id == error_mark_node)
39117 goto fail;
39119 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39120 goto fail;
39122 /* Types may not be defined in declare reduction type list. */
39123 const char *saved_message;
39124 saved_message = parser->type_definition_forbidden_message;
39125 parser->type_definition_forbidden_message
39126 = G_("types may not be defined in declare reduction type list");
39127 bool saved_colon_corrects_to_scope_p;
39128 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39129 parser->colon_corrects_to_scope_p = false;
39130 bool saved_colon_doesnt_start_class_def_p;
39131 saved_colon_doesnt_start_class_def_p
39132 = parser->colon_doesnt_start_class_def_p;
39133 parser->colon_doesnt_start_class_def_p = true;
39135 while (true)
39137 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39138 type = cp_parser_type_id (parser);
39139 if (type == error_mark_node)
39141 else if (ARITHMETIC_TYPE_P (type)
39142 && (orig_reduc_id == NULL_TREE
39143 || (TREE_CODE (type) != COMPLEX_TYPE
39144 && (id_equal (orig_reduc_id, "min")
39145 || id_equal (orig_reduc_id, "max")))))
39146 error_at (loc, "predeclared arithmetic type %qT in "
39147 "%<#pragma omp declare reduction%>", type);
39148 else if (TREE_CODE (type) == FUNCTION_TYPE
39149 || TREE_CODE (type) == METHOD_TYPE
39150 || TREE_CODE (type) == ARRAY_TYPE)
39151 error_at (loc, "function or array type %qT in "
39152 "%<#pragma omp declare reduction%>", type);
39153 else if (TYPE_REF_P (type))
39154 error_at (loc, "reference type %qT in "
39155 "%<#pragma omp declare reduction%>", type);
39156 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
39157 error_at (loc, "const, volatile or __restrict qualified type %qT in "
39158 "%<#pragma omp declare reduction%>", type);
39159 else
39160 types.safe_push (type);
39162 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39163 cp_lexer_consume_token (parser->lexer);
39164 else
39165 break;
39168 /* Restore the saved message. */
39169 parser->type_definition_forbidden_message = saved_message;
39170 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39171 parser->colon_doesnt_start_class_def_p
39172 = saved_colon_doesnt_start_class_def_p;
39174 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
39175 || types.is_empty ())
39177 fail:
39178 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39179 goto done;
39182 first_token = cp_lexer_peek_token (parser->lexer);
39183 cp = NULL;
39184 errs = errorcount;
39185 FOR_EACH_VEC_ELT (types, i, type)
39187 tree fntype
39188 = build_function_type_list (void_type_node,
39189 cp_build_reference_type (type, false),
39190 NULL_TREE);
39191 tree this_reduc_id = reduc_id;
39192 if (!dependent_type_p (type))
39193 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
39194 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
39195 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
39196 DECL_ARTIFICIAL (fndecl) = 1;
39197 DECL_EXTERNAL (fndecl) = 1;
39198 DECL_DECLARED_INLINE_P (fndecl) = 1;
39199 DECL_IGNORED_P (fndecl) = 1;
39200 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
39201 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
39202 DECL_ATTRIBUTES (fndecl)
39203 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
39204 DECL_ATTRIBUTES (fndecl));
39205 if (processing_template_decl)
39206 fndecl = push_template_decl (fndecl);
39207 bool block_scope = false;
39208 tree block = NULL_TREE;
39209 if (current_function_decl)
39211 block_scope = true;
39212 DECL_CONTEXT (fndecl) = global_namespace;
39213 if (!processing_template_decl)
39214 pushdecl (fndecl);
39216 else if (current_class_type)
39218 if (cp == NULL)
39220 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39221 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39222 cp_lexer_consume_token (parser->lexer);
39223 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39224 goto fail;
39225 cp = cp_token_cache_new (first_token,
39226 cp_lexer_peek_nth_token (parser->lexer,
39227 2));
39229 DECL_STATIC_FUNCTION_P (fndecl) = 1;
39230 finish_member_declaration (fndecl);
39231 DECL_PENDING_INLINE_INFO (fndecl) = cp;
39232 DECL_PENDING_INLINE_P (fndecl) = 1;
39233 vec_safe_push (unparsed_funs_with_definitions, fndecl);
39234 continue;
39236 else
39238 DECL_CONTEXT (fndecl) = current_namespace;
39239 pushdecl (fndecl);
39241 if (!block_scope)
39242 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
39243 else
39244 block = begin_omp_structured_block ();
39245 if (cp)
39247 cp_parser_push_lexer_for_tokens (parser, cp);
39248 parser->lexer->in_pragma = true;
39250 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
39252 if (!block_scope)
39253 finish_function (/*inline_p=*/false);
39254 else
39255 DECL_CONTEXT (fndecl) = current_function_decl;
39256 if (cp)
39257 cp_parser_pop_lexer (parser);
39258 goto fail;
39260 if (cp)
39261 cp_parser_pop_lexer (parser);
39262 if (!block_scope)
39263 finish_function (/*inline_p=*/false);
39264 else
39266 DECL_CONTEXT (fndecl) = current_function_decl;
39267 block = finish_omp_structured_block (block);
39268 if (TREE_CODE (block) == BIND_EXPR)
39269 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
39270 else if (TREE_CODE (block) == STATEMENT_LIST)
39271 DECL_SAVED_TREE (fndecl) = block;
39272 if (processing_template_decl)
39273 add_decl_expr (fndecl);
39275 cp_check_omp_declare_reduction (fndecl);
39276 if (cp == NULL && types.length () > 1)
39277 cp = cp_token_cache_new (first_token,
39278 cp_lexer_peek_nth_token (parser->lexer, 2));
39279 if (errs != errorcount)
39280 break;
39283 cp_parser_require_pragma_eol (parser, pragma_tok);
39285 done:
39286 /* Free any declarators allocated. */
39287 obstack_free (&declarator_obstack, p);
39290 /* OpenMP 4.0
39291 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39292 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39293 initializer-clause[opt] new-line
39294 #pragma omp declare target new-line */
39296 static bool
39297 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
39298 enum pragma_context context)
39300 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39302 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39303 const char *p = IDENTIFIER_POINTER (id);
39305 if (strcmp (p, "simd") == 0)
39307 cp_lexer_consume_token (parser->lexer);
39308 cp_parser_omp_declare_simd (parser, pragma_tok,
39309 context);
39310 return true;
39312 cp_ensure_no_omp_declare_simd (parser);
39313 if (strcmp (p, "reduction") == 0)
39315 cp_lexer_consume_token (parser->lexer);
39316 cp_parser_omp_declare_reduction (parser, pragma_tok,
39317 context);
39318 return false;
39320 if (!flag_openmp) /* flag_openmp_simd */
39322 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39323 return false;
39325 if (strcmp (p, "target") == 0)
39327 cp_lexer_consume_token (parser->lexer);
39328 cp_parser_omp_declare_target (parser, pragma_tok);
39329 return false;
39332 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
39333 "or %<target%>");
39334 cp_parser_require_pragma_eol (parser, pragma_tok);
39335 return false;
39338 /* OpenMP 5.0
39339 #pragma omp requires clauses[optseq] new-line */
39341 static bool
39342 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
39344 bool first = true;
39345 enum omp_requires new_req = (enum omp_requires) 0;
39347 location_t loc = pragma_tok->location;
39348 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39350 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39351 cp_lexer_consume_token (parser->lexer);
39353 first = false;
39355 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39357 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39358 const char *p = IDENTIFIER_POINTER (id);
39359 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
39360 enum omp_requires this_req = (enum omp_requires) 0;
39362 if (!strcmp (p, "unified_address"))
39363 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
39364 else if (!strcmp (p, "unified_shared_memory"))
39365 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
39366 else if (!strcmp (p, "dynamic_allocators"))
39367 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
39368 else if (!strcmp (p, "reverse_offload"))
39369 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
39370 else if (!strcmp (p, "atomic_default_mem_order"))
39372 cp_lexer_consume_token (parser->lexer);
39374 matching_parens parens;
39375 if (parens.require_open (parser))
39377 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39379 id = cp_lexer_peek_token (parser->lexer)->u.value;
39380 p = IDENTIFIER_POINTER (id);
39382 if (!strcmp (p, "seq_cst"))
39383 this_req
39384 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
39385 else if (!strcmp (p, "relaxed"))
39386 this_req
39387 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
39388 else if (!strcmp (p, "acq_rel"))
39389 this_req
39390 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
39392 if (this_req == 0)
39394 error_at (cp_lexer_peek_token (parser->lexer)->location,
39395 "expected %<seq_cst%>, %<relaxed%> or "
39396 "%<acq_rel%>");
39397 if (cp_lexer_nth_token_is (parser->lexer, 2,
39398 CPP_CLOSE_PAREN))
39399 cp_lexer_consume_token (parser->lexer);
39401 else
39402 cp_lexer_consume_token (parser->lexer);
39404 if (!parens.require_close (parser))
39405 cp_parser_skip_to_closing_parenthesis (parser,
39406 /*recovering=*/true,
39407 /*or_comma=*/false,
39408 /*consume_paren=*/
39409 true);
39411 if (this_req == 0)
39413 cp_parser_require_pragma_eol (parser, pragma_tok);
39414 return false;
39417 p = NULL;
39419 else
39421 error_at (cloc, "expected %<unified_address%>, "
39422 "%<unified_shared_memory%>, "
39423 "%<dynamic_allocators%>, "
39424 "%<reverse_offload%> "
39425 "or %<atomic_default_mem_order%> clause");
39426 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39427 return false;
39429 if (p)
39430 sorry_at (cloc, "%qs clause on %<requires%> directive not "
39431 "supported yet", p);
39432 if (p)
39433 cp_lexer_consume_token (parser->lexer);
39434 if (this_req)
39436 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39438 if ((this_req & new_req) != 0)
39439 error_at (cloc, "too many %qs clauses", p);
39440 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
39441 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
39442 error_at (cloc, "%qs clause used lexically after first "
39443 "target construct or offloading API", p);
39445 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39447 error_at (cloc, "too many %qs clauses",
39448 "atomic_default_mem_order");
39449 this_req = (enum omp_requires) 0;
39451 else if ((omp_requires_mask
39452 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39454 error_at (cloc, "more than one %<atomic_default_mem_order%>"
39455 " clause in a single compilation unit");
39456 this_req
39457 = (enum omp_requires)
39458 (omp_requires_mask
39459 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
39461 else if ((omp_requires_mask
39462 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
39463 error_at (cloc, "%<atomic_default_mem_order%> clause used "
39464 "lexically after first %<atomic%> construct "
39465 "without memory order clause");
39466 new_req = (enum omp_requires) (new_req | this_req);
39467 omp_requires_mask
39468 = (enum omp_requires) (omp_requires_mask | this_req);
39469 continue;
39472 break;
39474 cp_parser_require_pragma_eol (parser, pragma_tok);
39476 if (new_req == 0)
39477 error_at (loc, "%<pragma omp requires%> requires at least one clause");
39478 return false;
39482 /* OpenMP 4.5:
39483 #pragma omp taskloop taskloop-clause[optseq] new-line
39484 for-loop
39486 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39487 for-loop */
39489 #define OMP_TASKLOOP_CLAUSE_MASK \
39490 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39507 static tree
39508 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
39509 char *p_name, omp_clause_mask mask, tree *cclauses,
39510 bool *if_p)
39512 tree clauses, sb, ret;
39513 unsigned int save;
39514 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39516 strcat (p_name, " taskloop");
39517 mask |= OMP_TASKLOOP_CLAUSE_MASK;
39518 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39519 clause. */
39520 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
39521 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
39523 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39525 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39526 const char *p = IDENTIFIER_POINTER (id);
39528 if (strcmp (p, "simd") == 0)
39530 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39531 if (cclauses == NULL)
39532 cclauses = cclauses_buf;
39534 cp_lexer_consume_token (parser->lexer);
39535 if (!flag_openmp) /* flag_openmp_simd */
39536 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39537 cclauses, if_p);
39538 sb = begin_omp_structured_block ();
39539 save = cp_parser_begin_omp_structured_block (parser);
39540 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39541 cclauses, if_p);
39542 cp_parser_end_omp_structured_block (parser, save);
39543 tree body = finish_omp_structured_block (sb);
39544 if (ret == NULL)
39545 return ret;
39546 ret = make_node (OMP_TASKLOOP);
39547 TREE_TYPE (ret) = void_type_node;
39548 OMP_FOR_BODY (ret) = body;
39549 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39550 SET_EXPR_LOCATION (ret, loc);
39551 add_stmt (ret);
39552 return ret;
39555 if (!flag_openmp) /* flag_openmp_simd */
39557 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39558 return NULL_TREE;
39561 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39562 cclauses == NULL);
39563 if (cclauses)
39565 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
39566 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39569 keep_next_level (true);
39570 sb = begin_omp_structured_block ();
39571 save = cp_parser_begin_omp_structured_block (parser);
39573 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
39574 if_p);
39576 cp_parser_end_omp_structured_block (parser, save);
39577 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39579 return ret;
39583 /* OpenACC 2.0:
39584 # pragma acc routine oacc-routine-clause[optseq] new-line
39585 function-definition
39587 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39590 #define OACC_ROUTINE_CLAUSE_MASK \
39591 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39597 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39598 component, which must resolve to a declared namespace-scope
39599 function. The clauses are either processed directly (for a named
39600 function), or defered until the immediatley following declaration
39601 is parsed. */
39603 static void
39604 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
39605 enum pragma_context context)
39607 gcc_checking_assert (context == pragma_external);
39608 /* The checking for "another pragma following this one" in the "no optional
39609 '( name )'" case makes sure that we dont re-enter. */
39610 gcc_checking_assert (parser->oacc_routine == NULL);
39612 cp_oacc_routine_data data;
39613 data.error_seen = false;
39614 data.fndecl_seen = false;
39615 data.tokens = vNULL;
39616 data.clauses = NULL_TREE;
39617 data.loc = pragma_tok->location;
39618 /* It is safe to take the address of a local variable; it will only be
39619 used while this scope is live. */
39620 parser->oacc_routine = &data;
39622 /* Look for optional '( name )'. */
39623 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39625 matching_parens parens;
39626 parens.consume_open (parser); /* '(' */
39628 /* We parse the name as an id-expression. If it resolves to
39629 anything other than a non-overloaded function at namespace
39630 scope, it's an error. */
39631 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
39632 tree name = cp_parser_id_expression (parser,
39633 /*template_keyword_p=*/false,
39634 /*check_dependency_p=*/false,
39635 /*template_p=*/NULL,
39636 /*declarator_p=*/false,
39637 /*optional_p=*/false);
39638 tree decl = (identifier_p (name)
39639 ? cp_parser_lookup_name_simple (parser, name, name_loc)
39640 : name);
39641 if (name != error_mark_node && decl == error_mark_node)
39642 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
39644 if (decl == error_mark_node
39645 || !parens.require_close (parser))
39647 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39648 parser->oacc_routine = NULL;
39649 return;
39652 data.clauses
39653 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39654 "#pragma acc routine",
39655 cp_lexer_peek_token (parser->lexer));
39657 if (decl && is_overloaded_fn (decl)
39658 && (TREE_CODE (decl) != FUNCTION_DECL
39659 || DECL_FUNCTION_TEMPLATE_P (decl)))
39661 error_at (name_loc,
39662 "%<#pragma acc routine%> names a set of overloads");
39663 parser->oacc_routine = NULL;
39664 return;
39667 /* Perhaps we should use the same rule as declarations in different
39668 namespaces? */
39669 if (!DECL_NAMESPACE_SCOPE_P (decl))
39671 error_at (name_loc,
39672 "%qD does not refer to a namespace scope function", decl);
39673 parser->oacc_routine = NULL;
39674 return;
39677 if (TREE_CODE (decl) != FUNCTION_DECL)
39679 error_at (name_loc, "%qD does not refer to a function", decl);
39680 parser->oacc_routine = NULL;
39681 return;
39684 cp_finalize_oacc_routine (parser, decl, false);
39685 parser->oacc_routine = NULL;
39687 else /* No optional '( name )'. */
39689 /* Store away all pragma tokens. */
39690 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39691 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39692 cp_lexer_consume_token (parser->lexer);
39693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39694 parser->oacc_routine->error_seen = true;
39695 cp_parser_require_pragma_eol (parser, pragma_tok);
39696 struct cp_token_cache *cp
39697 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39698 parser->oacc_routine->tokens.safe_push (cp);
39700 /* Emit a helpful diagnostic if there's another pragma following this
39701 one. */
39702 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39704 cp_ensure_no_oacc_routine (parser);
39705 data.tokens.release ();
39706 /* ..., and then just keep going. */
39707 return;
39710 /* We only have to consider the pragma_external case here. */
39711 cp_parser_declaration (parser);
39712 if (parser->oacc_routine
39713 && !parser->oacc_routine->fndecl_seen)
39714 cp_ensure_no_oacc_routine (parser);
39715 else
39716 parser->oacc_routine = NULL;
39717 data.tokens.release ();
39721 /* Finalize #pragma acc routine clauses after direct declarator has
39722 been parsed. */
39724 static tree
39725 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
39727 struct cp_token_cache *ce;
39728 cp_oacc_routine_data *data = parser->oacc_routine;
39730 if (!data->error_seen && data->fndecl_seen)
39732 error_at (data->loc,
39733 "%<#pragma acc routine%> not immediately followed by "
39734 "a single function declaration or definition");
39735 data->error_seen = true;
39737 if (data->error_seen)
39738 return attrs;
39740 gcc_checking_assert (data->tokens.length () == 1);
39741 ce = data->tokens[0];
39743 cp_parser_push_lexer_for_tokens (parser, ce);
39744 parser->lexer->in_pragma = true;
39745 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39747 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39748 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
39749 parser->oacc_routine->clauses
39750 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39751 "#pragma acc routine", pragma_tok);
39752 cp_parser_pop_lexer (parser);
39753 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
39754 fndecl_seen. */
39756 return attrs;
39759 /* Apply any saved OpenACC routine clauses to a just-parsed
39760 declaration. */
39762 static void
39763 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
39765 if (__builtin_expect (parser->oacc_routine != NULL, 0))
39767 /* Keep going if we're in error reporting mode. */
39768 if (parser->oacc_routine->error_seen
39769 || fndecl == error_mark_node)
39770 return;
39772 if (parser->oacc_routine->fndecl_seen)
39774 error_at (parser->oacc_routine->loc,
39775 "%<#pragma acc routine%> not immediately followed by"
39776 " a single function declaration or definition");
39777 parser->oacc_routine = NULL;
39778 return;
39780 if (TREE_CODE (fndecl) != FUNCTION_DECL)
39782 cp_ensure_no_oacc_routine (parser);
39783 return;
39786 if (oacc_get_fn_attrib (fndecl))
39788 error_at (parser->oacc_routine->loc,
39789 "%<#pragma acc routine%> already applied to %qD", fndecl);
39790 parser->oacc_routine = NULL;
39791 return;
39794 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
39796 error_at (parser->oacc_routine->loc,
39797 TREE_USED (fndecl)
39798 ? G_("%<#pragma acc routine%> must be applied before use")
39799 : G_("%<#pragma acc routine%> must be applied before "
39800 "definition"));
39801 parser->oacc_routine = NULL;
39802 return;
39805 /* Process the routine's dimension clauses. */
39806 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
39807 oacc_replace_fn_attrib (fndecl, dims);
39809 /* Add an "omp declare target" attribute. */
39810 DECL_ATTRIBUTES (fndecl)
39811 = tree_cons (get_identifier ("omp declare target"),
39812 NULL_TREE, DECL_ATTRIBUTES (fndecl));
39814 /* Don't unset parser->oacc_routine here: we may still need it to
39815 diagnose wrong usage. But, remember that we've used this "#pragma acc
39816 routine". */
39817 parser->oacc_routine->fndecl_seen = true;
39821 /* Main entry point to OpenMP statement pragmas. */
39823 static void
39824 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39826 tree stmt;
39827 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
39828 omp_clause_mask mask (0);
39830 switch (cp_parser_pragma_kind (pragma_tok))
39832 case PRAGMA_OACC_ATOMIC:
39833 cp_parser_omp_atomic (parser, pragma_tok);
39834 return;
39835 case PRAGMA_OACC_CACHE:
39836 stmt = cp_parser_oacc_cache (parser, pragma_tok);
39837 break;
39838 case PRAGMA_OACC_DATA:
39839 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
39840 break;
39841 case PRAGMA_OACC_ENTER_DATA:
39842 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
39843 break;
39844 case PRAGMA_OACC_EXIT_DATA:
39845 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
39846 break;
39847 case PRAGMA_OACC_HOST_DATA:
39848 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
39849 break;
39850 case PRAGMA_OACC_KERNELS:
39851 case PRAGMA_OACC_PARALLEL:
39852 strcpy (p_name, "#pragma acc");
39853 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
39854 if_p);
39855 break;
39856 case PRAGMA_OACC_LOOP:
39857 strcpy (p_name, "#pragma acc");
39858 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
39859 if_p);
39860 break;
39861 case PRAGMA_OACC_UPDATE:
39862 stmt = cp_parser_oacc_update (parser, pragma_tok);
39863 break;
39864 case PRAGMA_OACC_WAIT:
39865 stmt = cp_parser_oacc_wait (parser, pragma_tok);
39866 break;
39867 case PRAGMA_OMP_ATOMIC:
39868 cp_parser_omp_atomic (parser, pragma_tok);
39869 return;
39870 case PRAGMA_OMP_CRITICAL:
39871 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
39872 break;
39873 case PRAGMA_OMP_DISTRIBUTE:
39874 strcpy (p_name, "#pragma omp");
39875 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
39876 if_p);
39877 break;
39878 case PRAGMA_OMP_FOR:
39879 strcpy (p_name, "#pragma omp");
39880 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
39881 if_p);
39882 break;
39883 case PRAGMA_OMP_MASTER:
39884 strcpy (p_name, "#pragma omp");
39885 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
39886 if_p);
39887 break;
39888 case PRAGMA_OMP_PARALLEL:
39889 strcpy (p_name, "#pragma omp");
39890 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
39891 if_p);
39892 break;
39893 case PRAGMA_OMP_SECTIONS:
39894 strcpy (p_name, "#pragma omp");
39895 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
39896 break;
39897 case PRAGMA_OMP_SIMD:
39898 strcpy (p_name, "#pragma omp");
39899 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
39900 if_p);
39901 break;
39902 case PRAGMA_OMP_SINGLE:
39903 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
39904 break;
39905 case PRAGMA_OMP_TASK:
39906 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
39907 break;
39908 case PRAGMA_OMP_TASKGROUP:
39909 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
39910 break;
39911 case PRAGMA_OMP_TASKLOOP:
39912 strcpy (p_name, "#pragma omp");
39913 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
39914 if_p);
39915 break;
39916 case PRAGMA_OMP_TEAMS:
39917 strcpy (p_name, "#pragma omp");
39918 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
39919 if_p);
39920 break;
39921 default:
39922 gcc_unreachable ();
39925 protected_set_expr_location (stmt, pragma_tok->location);
39928 /* Transactional Memory parsing routines. */
39930 /* Parse a transaction attribute.
39932 txn-attribute:
39933 attribute
39934 [ [ identifier ] ]
39936 We use this instead of cp_parser_attributes_opt for transactions to avoid
39937 the pedwarn in C++98 mode. */
39939 static tree
39940 cp_parser_txn_attribute_opt (cp_parser *parser)
39942 cp_token *token;
39943 tree attr_name, attr = NULL;
39945 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
39946 return cp_parser_attributes_opt (parser);
39948 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
39949 return NULL_TREE;
39950 cp_lexer_consume_token (parser->lexer);
39951 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
39952 goto error1;
39954 token = cp_lexer_peek_token (parser->lexer);
39955 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
39957 token = cp_lexer_consume_token (parser->lexer);
39959 attr_name = (token->type == CPP_KEYWORD
39960 /* For keywords, use the canonical spelling,
39961 not the parsed identifier. */
39962 ? ridpointers[(int) token->keyword]
39963 : token->u.value);
39964 attr = build_tree_list (attr_name, NULL_TREE);
39966 else
39967 cp_parser_error (parser, "expected identifier");
39969 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
39970 error1:
39971 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
39972 return attr;
39975 /* Parse a __transaction_atomic or __transaction_relaxed statement.
39977 transaction-statement:
39978 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
39979 compound-statement
39980 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
39983 static tree
39984 cp_parser_transaction (cp_parser *parser, cp_token *token)
39986 unsigned char old_in = parser->in_transaction;
39987 unsigned char this_in = 1, new_in;
39988 enum rid keyword = token->keyword;
39989 tree stmt, attrs, noex;
39991 cp_lexer_consume_token (parser->lexer);
39993 if (keyword == RID_TRANSACTION_RELAXED
39994 || keyword == RID_SYNCHRONIZED)
39995 this_in |= TM_STMT_ATTR_RELAXED;
39996 else
39998 attrs = cp_parser_txn_attribute_opt (parser);
39999 if (attrs)
40000 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40003 /* Parse a noexcept specification. */
40004 if (keyword == RID_ATOMIC_NOEXCEPT)
40005 noex = boolean_true_node;
40006 else if (keyword == RID_ATOMIC_CANCEL)
40008 /* cancel-and-throw is unimplemented. */
40009 sorry ("atomic_cancel");
40010 noex = NULL_TREE;
40012 else
40013 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
40015 /* Keep track if we're in the lexical scope of an outer transaction. */
40016 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
40018 stmt = begin_transaction_stmt (token->location, NULL, this_in);
40020 parser->in_transaction = new_in;
40021 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
40022 parser->in_transaction = old_in;
40024 finish_transaction_stmt (stmt, NULL, this_in, noex);
40026 return stmt;
40029 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40031 transaction-expression:
40032 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40033 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40036 static tree
40037 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
40039 unsigned char old_in = parser->in_transaction;
40040 unsigned char this_in = 1;
40041 cp_token *token;
40042 tree expr, noex;
40043 bool noex_expr;
40044 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40046 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40047 || keyword == RID_TRANSACTION_RELAXED);
40049 if (!flag_tm)
40050 error_at (loc,
40051 keyword == RID_TRANSACTION_RELAXED
40052 ? G_("%<__transaction_relaxed%> without transactional memory "
40053 "support enabled")
40054 : G_("%<__transaction_atomic%> without transactional memory "
40055 "support enabled"));
40057 token = cp_parser_require_keyword (parser, keyword,
40058 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40059 : RT_TRANSACTION_RELAXED));
40060 gcc_assert (token != NULL);
40062 if (keyword == RID_TRANSACTION_RELAXED)
40063 this_in |= TM_STMT_ATTR_RELAXED;
40065 /* Set this early. This might mean that we allow transaction_cancel in
40066 an expression that we find out later actually has to be a constexpr.
40067 However, we expect that cxx_constant_value will be able to deal with
40068 this; also, if the noexcept has no constexpr, then what we parse next
40069 really is a transaction's body. */
40070 parser->in_transaction = this_in;
40072 /* Parse a noexcept specification. */
40073 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
40074 true);
40076 if (!noex || !noex_expr
40077 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40079 matching_parens parens;
40080 parens.require_open (parser);
40082 expr = cp_parser_expression (parser);
40083 expr = finish_parenthesized_expr (expr);
40085 parens.require_close (parser);
40087 else
40089 /* The only expression that is available got parsed for the noexcept
40090 already. noexcept is true then. */
40091 expr = noex;
40092 noex = boolean_true_node;
40095 expr = build_transaction_expr (token->location, expr, this_in, noex);
40096 parser->in_transaction = old_in;
40098 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
40099 return error_mark_node;
40101 return (flag_tm ? expr : error_mark_node);
40104 /* Parse a function-transaction-block.
40106 function-transaction-block:
40107 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40108 function-body
40109 __transaction_atomic txn-attribute[opt] function-try-block
40110 __transaction_relaxed ctor-initializer[opt] function-body
40111 __transaction_relaxed function-try-block
40114 static void
40115 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
40117 unsigned char old_in = parser->in_transaction;
40118 unsigned char new_in = 1;
40119 tree compound_stmt, stmt, attrs;
40120 cp_token *token;
40122 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40123 || keyword == RID_TRANSACTION_RELAXED);
40124 token = cp_parser_require_keyword (parser, keyword,
40125 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40126 : RT_TRANSACTION_RELAXED));
40127 gcc_assert (token != NULL);
40129 if (keyword == RID_TRANSACTION_RELAXED)
40130 new_in |= TM_STMT_ATTR_RELAXED;
40131 else
40133 attrs = cp_parser_txn_attribute_opt (parser);
40134 if (attrs)
40135 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40138 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
40140 parser->in_transaction = new_in;
40142 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
40143 cp_parser_function_try_block (parser);
40144 else
40145 cp_parser_ctor_initializer_opt_and_function_body
40146 (parser, /*in_function_try_block=*/false);
40148 parser->in_transaction = old_in;
40150 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
40153 /* Parse a __transaction_cancel statement.
40155 cancel-statement:
40156 __transaction_cancel txn-attribute[opt] ;
40157 __transaction_cancel txn-attribute[opt] throw-expression ;
40159 ??? Cancel and throw is not yet implemented. */
40161 static tree
40162 cp_parser_transaction_cancel (cp_parser *parser)
40164 cp_token *token;
40165 bool is_outer = false;
40166 tree stmt, attrs;
40168 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
40169 RT_TRANSACTION_CANCEL);
40170 gcc_assert (token != NULL);
40172 attrs = cp_parser_txn_attribute_opt (parser);
40173 if (attrs)
40174 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
40176 /* ??? Parse cancel-and-throw here. */
40178 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40180 if (!flag_tm)
40182 error_at (token->location, "%<__transaction_cancel%> without "
40183 "transactional memory support enabled");
40184 return error_mark_node;
40186 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
40188 error_at (token->location, "%<__transaction_cancel%> within a "
40189 "%<__transaction_relaxed%>");
40190 return error_mark_node;
40192 else if (is_outer)
40194 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
40195 && !is_tm_may_cancel_outer (current_function_decl))
40197 error_at (token->location, "outer %<__transaction_cancel%> not "
40198 "within outer %<__transaction_atomic%>");
40199 error_at (token->location,
40200 " or a %<transaction_may_cancel_outer%> function");
40201 return error_mark_node;
40204 else if (parser->in_transaction == 0)
40206 error_at (token->location, "%<__transaction_cancel%> not within "
40207 "%<__transaction_atomic%>");
40208 return error_mark_node;
40211 stmt = build_tm_abort_call (token->location, is_outer);
40212 add_stmt (stmt);
40214 return stmt;
40217 /* The parser. */
40219 static GTY (()) cp_parser *the_parser;
40222 /* Special handling for the first token or line in the file. The first
40223 thing in the file might be #pragma GCC pch_preprocess, which loads a
40224 PCH file, which is a GC collection point. So we need to handle this
40225 first pragma without benefit of an existing lexer structure.
40227 Always returns one token to the caller in *FIRST_TOKEN. This is
40228 either the true first token of the file, or the first token after
40229 the initial pragma. */
40231 static void
40232 cp_parser_initial_pragma (cp_token *first_token)
40234 tree name = NULL;
40236 cp_lexer_get_preprocessor_token (NULL, first_token);
40237 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
40238 return;
40240 cp_lexer_get_preprocessor_token (NULL, first_token);
40241 if (first_token->type == CPP_STRING)
40243 name = first_token->u.value;
40245 cp_lexer_get_preprocessor_token (NULL, first_token);
40246 if (first_token->type != CPP_PRAGMA_EOL)
40247 error_at (first_token->location,
40248 "junk at end of %<#pragma GCC pch_preprocess%>");
40250 else
40251 error_at (first_token->location, "expected string literal");
40253 /* Skip to the end of the pragma. */
40254 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
40255 cp_lexer_get_preprocessor_token (NULL, first_token);
40257 /* Now actually load the PCH file. */
40258 if (name)
40259 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
40261 /* Read one more token to return to our caller. We have to do this
40262 after reading the PCH file in, since its pointers have to be
40263 live. */
40264 cp_lexer_get_preprocessor_token (NULL, first_token);
40267 /* Parse a pragma GCC ivdep. */
40269 static bool
40270 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
40272 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40273 return true;
40276 /* Parse a pragma GCC unroll. */
40278 static unsigned short
40279 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
40281 location_t location = cp_lexer_peek_token (parser->lexer)->location;
40282 tree expr = cp_parser_constant_expression (parser);
40283 unsigned short unroll;
40284 expr = maybe_constant_value (expr);
40285 HOST_WIDE_INT lunroll = 0;
40286 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
40287 || TREE_CODE (expr) != INTEGER_CST
40288 || (lunroll = tree_to_shwi (expr)) < 0
40289 || lunroll >= USHRT_MAX)
40291 error_at (location, "%<#pragma GCC unroll%> requires an"
40292 " assignment-expression that evaluates to a non-negative"
40293 " integral constant less than %u", USHRT_MAX);
40294 unroll = 0;
40296 else
40298 unroll = (unsigned short)lunroll;
40299 if (unroll == 0)
40300 unroll = 1;
40302 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40303 return unroll;
40306 /* Normal parsing of a pragma token. Here we can (and must) use the
40307 regular lexer. */
40309 static bool
40310 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
40312 cp_token *pragma_tok;
40313 unsigned int id;
40314 tree stmt;
40315 bool ret;
40317 pragma_tok = cp_lexer_consume_token (parser->lexer);
40318 gcc_assert (pragma_tok->type == CPP_PRAGMA);
40319 parser->lexer->in_pragma = true;
40321 id = cp_parser_pragma_kind (pragma_tok);
40322 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
40323 cp_ensure_no_omp_declare_simd (parser);
40324 switch (id)
40326 case PRAGMA_GCC_PCH_PREPROCESS:
40327 error_at (pragma_tok->location,
40328 "%<#pragma GCC pch_preprocess%> must be first");
40329 break;
40331 case PRAGMA_OMP_BARRIER:
40332 switch (context)
40334 case pragma_compound:
40335 cp_parser_omp_barrier (parser, pragma_tok);
40336 return false;
40337 case pragma_stmt:
40338 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40339 "used in compound statements", "omp barrier");
40340 break;
40341 default:
40342 goto bad_stmt;
40344 break;
40346 case PRAGMA_OMP_DEPOBJ:
40347 switch (context)
40349 case pragma_compound:
40350 cp_parser_omp_depobj (parser, pragma_tok);
40351 return false;
40352 case pragma_stmt:
40353 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40354 "used in compound statements", "omp depobj");
40355 break;
40356 default:
40357 goto bad_stmt;
40359 break;
40361 case PRAGMA_OMP_FLUSH:
40362 switch (context)
40364 case pragma_compound:
40365 cp_parser_omp_flush (parser, pragma_tok);
40366 return false;
40367 case pragma_stmt:
40368 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40369 "used in compound statements", "omp flush");
40370 break;
40371 default:
40372 goto bad_stmt;
40374 break;
40376 case PRAGMA_OMP_TASKWAIT:
40377 switch (context)
40379 case pragma_compound:
40380 cp_parser_omp_taskwait (parser, pragma_tok);
40381 return false;
40382 case pragma_stmt:
40383 error_at (pragma_tok->location,
40384 "%<#pragma %s%> may only be used in compound statements",
40385 "omp taskwait");
40386 break;
40387 default:
40388 goto bad_stmt;
40390 break;
40392 case PRAGMA_OMP_TASKYIELD:
40393 switch (context)
40395 case pragma_compound:
40396 cp_parser_omp_taskyield (parser, pragma_tok);
40397 return false;
40398 case pragma_stmt:
40399 error_at (pragma_tok->location,
40400 "%<#pragma %s%> may only be used in compound statements",
40401 "omp taskyield");
40402 break;
40403 default:
40404 goto bad_stmt;
40406 break;
40408 case PRAGMA_OMP_CANCEL:
40409 switch (context)
40411 case pragma_compound:
40412 cp_parser_omp_cancel (parser, pragma_tok);
40413 return false;
40414 case pragma_stmt:
40415 error_at (pragma_tok->location,
40416 "%<#pragma %s%> may only be used in compound statements",
40417 "omp cancel");
40418 break;
40419 default:
40420 goto bad_stmt;
40422 break;
40424 case PRAGMA_OMP_CANCELLATION_POINT:
40425 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
40426 return false;
40428 case PRAGMA_OMP_THREADPRIVATE:
40429 cp_parser_omp_threadprivate (parser, pragma_tok);
40430 return false;
40432 case PRAGMA_OMP_DECLARE:
40433 return cp_parser_omp_declare (parser, pragma_tok, context);
40435 case PRAGMA_OACC_DECLARE:
40436 cp_parser_oacc_declare (parser, pragma_tok);
40437 return false;
40439 case PRAGMA_OACC_ENTER_DATA:
40440 if (context == pragma_stmt)
40442 error_at (pragma_tok->location,
40443 "%<#pragma %s%> may only be used in compound statements",
40444 "acc enter data");
40445 break;
40447 else if (context != pragma_compound)
40448 goto bad_stmt;
40449 cp_parser_omp_construct (parser, pragma_tok, if_p);
40450 return true;
40452 case PRAGMA_OACC_EXIT_DATA:
40453 if (context == pragma_stmt)
40455 error_at (pragma_tok->location,
40456 "%<#pragma %s%> may only be used in compound statements",
40457 "acc exit data");
40458 break;
40460 else if (context != pragma_compound)
40461 goto bad_stmt;
40462 cp_parser_omp_construct (parser, pragma_tok, if_p);
40463 return true;
40465 case PRAGMA_OACC_ROUTINE:
40466 if (context != pragma_external)
40468 error_at (pragma_tok->location,
40469 "%<#pragma acc routine%> must be at file scope");
40470 break;
40472 cp_parser_oacc_routine (parser, pragma_tok, context);
40473 return false;
40475 case PRAGMA_OACC_UPDATE:
40476 if (context == pragma_stmt)
40478 error_at (pragma_tok->location,
40479 "%<#pragma %s%> may only be used in compound statements",
40480 "acc update");
40481 break;
40483 else if (context != pragma_compound)
40484 goto bad_stmt;
40485 cp_parser_omp_construct (parser, pragma_tok, if_p);
40486 return true;
40488 case PRAGMA_OACC_WAIT:
40489 if (context == pragma_stmt)
40491 error_at (pragma_tok->location,
40492 "%<#pragma %s%> may only be used in compound statements",
40493 "acc wait");
40494 break;
40496 else if (context != pragma_compound)
40497 goto bad_stmt;
40498 cp_parser_omp_construct (parser, pragma_tok, if_p);
40499 return true;
40501 case PRAGMA_OACC_ATOMIC:
40502 case PRAGMA_OACC_CACHE:
40503 case PRAGMA_OACC_DATA:
40504 case PRAGMA_OACC_HOST_DATA:
40505 case PRAGMA_OACC_KERNELS:
40506 case PRAGMA_OACC_PARALLEL:
40507 case PRAGMA_OACC_LOOP:
40508 case PRAGMA_OMP_ATOMIC:
40509 case PRAGMA_OMP_CRITICAL:
40510 case PRAGMA_OMP_DISTRIBUTE:
40511 case PRAGMA_OMP_FOR:
40512 case PRAGMA_OMP_MASTER:
40513 case PRAGMA_OMP_PARALLEL:
40514 case PRAGMA_OMP_SECTIONS:
40515 case PRAGMA_OMP_SIMD:
40516 case PRAGMA_OMP_SINGLE:
40517 case PRAGMA_OMP_TASK:
40518 case PRAGMA_OMP_TASKGROUP:
40519 case PRAGMA_OMP_TASKLOOP:
40520 case PRAGMA_OMP_TEAMS:
40521 if (context != pragma_stmt && context != pragma_compound)
40522 goto bad_stmt;
40523 stmt = push_omp_privatization_clauses (false);
40524 cp_parser_omp_construct (parser, pragma_tok, if_p);
40525 pop_omp_privatization_clauses (stmt);
40526 return true;
40528 case PRAGMA_OMP_REQUIRES:
40529 return cp_parser_omp_requires (parser, pragma_tok);
40531 case PRAGMA_OMP_ORDERED:
40532 if (context != pragma_stmt && context != pragma_compound)
40533 goto bad_stmt;
40534 stmt = push_omp_privatization_clauses (false);
40535 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
40536 pop_omp_privatization_clauses (stmt);
40537 return ret;
40539 case PRAGMA_OMP_TARGET:
40540 if (context != pragma_stmt && context != pragma_compound)
40541 goto bad_stmt;
40542 stmt = push_omp_privatization_clauses (false);
40543 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
40544 pop_omp_privatization_clauses (stmt);
40545 return ret;
40547 case PRAGMA_OMP_END_DECLARE_TARGET:
40548 cp_parser_omp_end_declare_target (parser, pragma_tok);
40549 return false;
40551 case PRAGMA_OMP_SECTION:
40552 error_at (pragma_tok->location,
40553 "%<#pragma omp section%> may only be used in "
40554 "%<#pragma omp sections%> construct");
40555 break;
40557 case PRAGMA_IVDEP:
40559 if (context == pragma_external)
40561 error_at (pragma_tok->location,
40562 "%<#pragma GCC ivdep%> must be inside a function");
40563 break;
40565 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
40566 unsigned short unroll;
40567 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40568 if (tok->type == CPP_PRAGMA
40569 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
40571 tok = cp_lexer_consume_token (parser->lexer);
40572 unroll = cp_parser_pragma_unroll (parser, tok);
40573 tok = cp_lexer_peek_token (the_parser->lexer);
40575 else
40576 unroll = 0;
40577 if (tok->type != CPP_KEYWORD
40578 || (tok->keyword != RID_FOR
40579 && tok->keyword != RID_WHILE
40580 && tok->keyword != RID_DO))
40582 cp_parser_error (parser, "for, while or do statement expected");
40583 return false;
40585 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40586 return true;
40589 case PRAGMA_UNROLL:
40591 if (context == pragma_external)
40593 error_at (pragma_tok->location,
40594 "%<#pragma GCC unroll%> must be inside a function");
40595 break;
40597 const unsigned short unroll
40598 = cp_parser_pragma_unroll (parser, pragma_tok);
40599 bool ivdep;
40600 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40601 if (tok->type == CPP_PRAGMA
40602 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
40604 tok = cp_lexer_consume_token (parser->lexer);
40605 ivdep = cp_parser_pragma_ivdep (parser, tok);
40606 tok = cp_lexer_peek_token (the_parser->lexer);
40608 else
40609 ivdep = false;
40610 if (tok->type != CPP_KEYWORD
40611 || (tok->keyword != RID_FOR
40612 && tok->keyword != RID_WHILE
40613 && tok->keyword != RID_DO))
40615 cp_parser_error (parser, "for, while or do statement expected");
40616 return false;
40618 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40619 return true;
40622 default:
40623 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
40624 c_invoke_pragma_handler (id);
40625 break;
40627 bad_stmt:
40628 cp_parser_error (parser, "expected declaration specifiers");
40629 break;
40632 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40633 return false;
40636 /* The interface the pragma parsers have to the lexer. */
40638 enum cpp_ttype
40639 pragma_lex (tree *value, location_t *loc)
40641 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40642 enum cpp_ttype ret = tok->type;
40644 *value = tok->u.value;
40645 if (loc)
40646 *loc = tok->location;
40648 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
40649 ret = CPP_EOF;
40650 else if (ret == CPP_STRING)
40651 *value = cp_parser_string_literal (the_parser, false, false);
40652 else
40654 if (ret == CPP_KEYWORD)
40655 ret = CPP_NAME;
40656 cp_lexer_consume_token (the_parser->lexer);
40659 return ret;
40663 /* External interface. */
40665 /* Parse one entire translation unit. */
40667 void
40668 c_parse_file (void)
40670 static bool already_called = false;
40672 if (already_called)
40673 fatal_error (input_location,
40674 "inter-module optimizations not implemented for C++");
40675 already_called = true;
40677 the_parser = cp_parser_new ();
40678 push_deferring_access_checks (flag_access_control
40679 ? dk_no_deferred : dk_no_check);
40680 cp_parser_translation_unit (the_parser);
40681 the_parser = NULL;
40683 finish_translation_unit ();
40686 /* Create an identifier for a generic parameter type (a synthesized
40687 template parameter implied by `auto' or a concept identifier). */
40689 static GTY(()) int generic_parm_count;
40690 static tree
40691 make_generic_type_name ()
40693 char buf[32];
40694 sprintf (buf, "auto:%d", ++generic_parm_count);
40695 return get_identifier (buf);
40698 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
40699 (creating a new template parameter list if necessary). Returns the newly
40700 created template type parm. */
40702 static tree
40703 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
40705 gcc_assert (current_binding_level->kind == sk_function_parms);
40707 /* Before committing to modifying any scope, if we're in an
40708 implicit template scope, and we're trying to synthesize a
40709 constrained parameter, try to find a previous parameter with
40710 the same name. This is the same-type rule for abbreviated
40711 function templates.
40713 NOTE: We can generate implicit parameters when tentatively
40714 parsing a nested name specifier, only to reject that parse
40715 later. However, matching the same template-id as part of a
40716 direct-declarator should generate an identical template
40717 parameter, so this rule will merge them. */
40718 if (parser->implicit_template_scope && constr)
40720 tree t = parser->implicit_template_parms;
40721 while (t)
40723 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
40725 tree d = TREE_VALUE (t);
40726 if (TREE_CODE (d) == PARM_DECL)
40727 /* Return the TEMPLATE_PARM_INDEX. */
40728 d = DECL_INITIAL (d);
40729 return d;
40731 t = TREE_CHAIN (t);
40735 /* We are either continuing a function template that already contains implicit
40736 template parameters, creating a new fully-implicit function template, or
40737 extending an existing explicit function template with implicit template
40738 parameters. */
40740 cp_binding_level *const entry_scope = current_binding_level;
40742 bool become_template = false;
40743 cp_binding_level *parent_scope = 0;
40745 if (parser->implicit_template_scope)
40747 gcc_assert (parser->implicit_template_parms);
40749 current_binding_level = parser->implicit_template_scope;
40751 else
40753 /* Roll back to the existing template parameter scope (in the case of
40754 extending an explicit function template) or introduce a new template
40755 parameter scope ahead of the function parameter scope (or class scope
40756 in the case of out-of-line member definitions). The function scope is
40757 added back after template parameter synthesis below. */
40759 cp_binding_level *scope = entry_scope;
40761 while (scope->kind == sk_function_parms)
40763 parent_scope = scope;
40764 scope = scope->level_chain;
40766 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
40768 /* If not defining a class, then any class scope is a scope level in
40769 an out-of-line member definition. In this case simply wind back
40770 beyond the first such scope to inject the template parameter list.
40771 Otherwise wind back to the class being defined. The latter can
40772 occur in class member friend declarations such as:
40774 class A {
40775 void foo (auto);
40777 class B {
40778 friend void A::foo (auto);
40781 The template parameter list synthesized for the friend declaration
40782 must be injected in the scope of 'B'. This can also occur in
40783 erroneous cases such as:
40785 struct A {
40786 struct B {
40787 void foo (auto);
40789 void B::foo (auto) {}
40792 Here the attempted definition of 'B::foo' within 'A' is ill-formed
40793 but, nevertheless, the template parameter list synthesized for the
40794 declarator should be injected into the scope of 'A' as if the
40795 ill-formed template was specified explicitly. */
40797 while (scope->kind == sk_class && !scope->defining_class_p)
40799 parent_scope = scope;
40800 scope = scope->level_chain;
40804 current_binding_level = scope;
40806 if (scope->kind != sk_template_parms
40807 || !function_being_declared_is_template_p (parser))
40809 /* Introduce a new template parameter list for implicit template
40810 parameters. */
40812 become_template = true;
40814 parser->implicit_template_scope
40815 = begin_scope (sk_template_parms, NULL);
40817 ++processing_template_decl;
40819 parser->fully_implicit_function_template_p = true;
40820 ++parser->num_template_parameter_lists;
40822 else
40824 /* Synthesize implicit template parameters at the end of the explicit
40825 template parameter list. */
40827 gcc_assert (current_template_parms);
40829 parser->implicit_template_scope = scope;
40831 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
40832 parser->implicit_template_parms
40833 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
40837 /* Synthesize a new template parameter and track the current template
40838 parameter chain with implicit_template_parms. */
40840 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
40841 tree synth_id = make_generic_type_name ();
40842 tree synth_tmpl_parm;
40843 bool non_type = false;
40845 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
40846 synth_tmpl_parm
40847 = finish_template_type_parm (class_type_node, synth_id);
40848 else if (TREE_CODE (proto) == TEMPLATE_DECL)
40849 synth_tmpl_parm
40850 = finish_constrained_template_template_parm (proto, synth_id);
40851 else
40853 synth_tmpl_parm = copy_decl (proto);
40854 DECL_NAME (synth_tmpl_parm) = synth_id;
40855 non_type = true;
40858 // Attach the constraint to the parm before processing.
40859 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
40860 TREE_TYPE (node) = constr;
40861 tree new_parm
40862 = process_template_parm (parser->implicit_template_parms,
40863 input_location,
40864 node,
40865 /*non_type=*/non_type,
40866 /*param_pack=*/false);
40868 // Chain the new parameter to the list of implicit parameters.
40869 if (parser->implicit_template_parms)
40870 parser->implicit_template_parms
40871 = TREE_CHAIN (parser->implicit_template_parms);
40872 else
40873 parser->implicit_template_parms = new_parm;
40875 tree new_decl = get_local_decls ();
40876 if (non_type)
40877 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
40878 new_decl = DECL_INITIAL (new_decl);
40880 /* If creating a fully implicit function template, start the new implicit
40881 template parameter list with this synthesized type, otherwise grow the
40882 current template parameter list. */
40884 if (become_template)
40886 parent_scope->level_chain = current_binding_level;
40888 tree new_parms = make_tree_vec (1);
40889 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
40890 current_template_parms = tree_cons (size_int (processing_template_decl),
40891 new_parms, current_template_parms);
40893 else
40895 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
40896 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
40897 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
40898 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
40901 // If the new parameter was constrained, we need to add that to the
40902 // constraints in the template parameter list.
40903 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
40905 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
40906 reqs = conjoin_constraints (reqs, req);
40907 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
40910 current_binding_level = entry_scope;
40912 return new_decl;
40915 /* Finish the declaration of a fully implicit function template. Such a
40916 template has no explicit template parameter list so has not been through the
40917 normal template head and tail processing. synthesize_implicit_template_parm
40918 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
40919 provided if the declaration is a class member such that its template
40920 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
40921 form is returned. Otherwise NULL_TREE is returned. */
40923 static tree
40924 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
40926 gcc_assert (parser->fully_implicit_function_template_p);
40928 if (member_decl_opt && member_decl_opt != error_mark_node
40929 && DECL_VIRTUAL_P (member_decl_opt))
40931 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
40932 "implicit templates may not be %<virtual%>");
40933 DECL_VIRTUAL_P (member_decl_opt) = false;
40936 if (member_decl_opt)
40937 member_decl_opt = finish_member_template_decl (member_decl_opt);
40938 end_template_decl ();
40940 parser->fully_implicit_function_template_p = false;
40941 parser->implicit_template_parms = 0;
40942 parser->implicit_template_scope = 0;
40943 --parser->num_template_parameter_lists;
40945 return member_decl_opt;
40948 /* Like finish_fully_implicit_template, but to be used in error
40949 recovery, rearranging scopes so that we restore the state we had
40950 before synthesize_implicit_template_parm inserted the implement
40951 template parms scope. */
40953 static void
40954 abort_fully_implicit_template (cp_parser *parser)
40956 cp_binding_level *return_to_scope = current_binding_level;
40958 if (parser->implicit_template_scope
40959 && return_to_scope != parser->implicit_template_scope)
40961 cp_binding_level *child = return_to_scope;
40962 for (cp_binding_level *scope = child->level_chain;
40963 scope != parser->implicit_template_scope;
40964 scope = child->level_chain)
40965 child = scope;
40966 child->level_chain = parser->implicit_template_scope->level_chain;
40967 parser->implicit_template_scope->level_chain = return_to_scope;
40968 current_binding_level = parser->implicit_template_scope;
40970 else
40971 return_to_scope = return_to_scope->level_chain;
40973 finish_fully_implicit_template (parser, NULL);
40975 gcc_assert (current_binding_level == return_to_scope);
40978 /* Helper function for diagnostics that have complained about things
40979 being used with 'extern "C"' linkage.
40981 Attempt to issue a note showing where the 'extern "C"' linkage began. */
40983 void
40984 maybe_show_extern_c_location (void)
40986 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
40987 inform (the_parser->innermost_linkage_specification_location,
40988 "%<extern \"C\"%> linkage started here");
40991 #include "gt-cp-parser.h"