PR rtl-optimization/82913
[official-gcc.git] / gcc / cp / parser.c
blob77b96376e1367b920887117f09574ef6ea36a261
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2017 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 #include "system.h"
23 #include "coretypes.h"
24 #include "cp-tree.h"
25 #include "c-family/c-common.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "cgraph.h"
29 #include "print-tree.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "intl.h"
33 #include "decl.h"
34 #include "c-family/c-objc.h"
35 #include "plugin.h"
36 #include "tree-pretty-print.h"
37 #include "parser.h"
38 #include "gomp-constants.h"
39 #include "omp-general.h"
40 #include "omp-offload.h"
41 #include "c-family/c-indentation.h"
42 #include "context.h"
43 #include "cp-cilkplus.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
48 /* The lexer. */
50 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
51 and c-lex.c) and the C++ parser. */
53 static cp_token eof_token =
55 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
58 /* The various kinds of non integral constant we encounter. */
59 enum non_integral_constant {
60 NIC_NONE,
61 /* floating-point literal */
62 NIC_FLOAT,
63 /* %<this%> */
64 NIC_THIS,
65 /* %<__FUNCTION__%> */
66 NIC_FUNC_NAME,
67 /* %<__PRETTY_FUNCTION__%> */
68 NIC_PRETTY_FUNC,
69 /* %<__func__%> */
70 NIC_C99_FUNC,
71 /* "%<va_arg%> */
72 NIC_VA_ARG,
73 /* a cast */
74 NIC_CAST,
75 /* %<typeid%> operator */
76 NIC_TYPEID,
77 /* non-constant compound literals */
78 NIC_NCC,
79 /* a function call */
80 NIC_FUNC_CALL,
81 /* an increment */
82 NIC_INC,
83 /* an decrement */
84 NIC_DEC,
85 /* an array reference */
86 NIC_ARRAY_REF,
87 /* %<->%> */
88 NIC_ARROW,
89 /* %<.%> */
90 NIC_POINT,
91 /* the address of a label */
92 NIC_ADDR_LABEL,
93 /* %<*%> */
94 NIC_STAR,
95 /* %<&%> */
96 NIC_ADDR,
97 /* %<++%> */
98 NIC_PREINCREMENT,
99 /* %<--%> */
100 NIC_PREDECREMENT,
101 /* %<new%> */
102 NIC_NEW,
103 /* %<delete%> */
104 NIC_DEL,
105 /* calls to overloaded operators */
106 NIC_OVERLOADED,
107 /* an assignment */
108 NIC_ASSIGNMENT,
109 /* a comma operator */
110 NIC_COMMA,
111 /* a call to a constructor */
112 NIC_CONSTRUCTOR,
113 /* a transaction expression */
114 NIC_TRANSACTION
117 /* The various kinds of errors about name-lookup failing. */
118 enum name_lookup_error {
119 /* NULL */
120 NLE_NULL,
121 /* is not a type */
122 NLE_TYPE,
123 /* is not a class or namespace */
124 NLE_CXX98,
125 /* is not a class, namespace, or enumeration */
126 NLE_NOT_CXX98
129 /* The various kinds of required token */
130 enum required_token {
131 RT_NONE,
132 RT_SEMICOLON, /* ';' */
133 RT_OPEN_PAREN, /* '(' */
134 RT_CLOSE_BRACE, /* '}' */
135 RT_OPEN_BRACE, /* '{' */
136 RT_CLOSE_SQUARE, /* ']' */
137 RT_OPEN_SQUARE, /* '[' */
138 RT_COMMA, /* ',' */
139 RT_SCOPE, /* '::' */
140 RT_LESS, /* '<' */
141 RT_GREATER, /* '>' */
142 RT_EQ, /* '=' */
143 RT_ELLIPSIS, /* '...' */
144 RT_MULT, /* '*' */
145 RT_COMPL, /* '~' */
146 RT_COLON, /* ':' */
147 RT_COLON_SCOPE, /* ':' or '::' */
148 RT_CLOSE_PAREN, /* ')' */
149 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
150 RT_PRAGMA_EOL, /* end of line */
151 RT_NAME, /* identifier */
153 /* The type is CPP_KEYWORD */
154 RT_NEW, /* new */
155 RT_DELETE, /* delete */
156 RT_RETURN, /* return */
157 RT_WHILE, /* while */
158 RT_EXTERN, /* extern */
159 RT_STATIC_ASSERT, /* static_assert */
160 RT_DECLTYPE, /* decltype */
161 RT_OPERATOR, /* operator */
162 RT_CLASS, /* class */
163 RT_TEMPLATE, /* template */
164 RT_NAMESPACE, /* namespace */
165 RT_USING, /* using */
166 RT_ASM, /* asm */
167 RT_TRY, /* try */
168 RT_CATCH, /* catch */
169 RT_THROW, /* throw */
170 RT_LABEL, /* __label__ */
171 RT_AT_TRY, /* @try */
172 RT_AT_SYNCHRONIZED, /* @synchronized */
173 RT_AT_THROW, /* @throw */
175 RT_SELECT, /* selection-statement */
176 RT_ITERATION, /* iteration-statement */
177 RT_JUMP, /* jump-statement */
178 RT_CLASS_KEY, /* class-key */
179 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
180 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
181 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
182 RT_TRANSACTION_CANCEL /* __transaction_cancel */
185 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
186 reverting it on destruction. */
188 class type_id_in_expr_sentinel
190 cp_parser *parser;
191 bool saved;
192 public:
193 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
194 : parser (parser),
195 saved (parser->in_type_id_in_expr_p)
196 { parser->in_type_id_in_expr_p = set; }
197 ~type_id_in_expr_sentinel ()
198 { parser->in_type_id_in_expr_p = saved; }
201 /* Prototypes. */
203 static cp_lexer *cp_lexer_new_main
204 (void);
205 static cp_lexer *cp_lexer_new_from_tokens
206 (cp_token_cache *tokens);
207 static void cp_lexer_destroy
208 (cp_lexer *);
209 static int cp_lexer_saving_tokens
210 (const cp_lexer *);
211 static cp_token *cp_lexer_token_at
212 (cp_lexer *, cp_token_position);
213 static void cp_lexer_get_preprocessor_token
214 (cp_lexer *, cp_token *);
215 static inline cp_token *cp_lexer_peek_token
216 (cp_lexer *);
217 static cp_token *cp_lexer_peek_nth_token
218 (cp_lexer *, size_t);
219 static inline bool cp_lexer_next_token_is
220 (cp_lexer *, enum cpp_ttype);
221 static bool cp_lexer_next_token_is_not
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_keyword
224 (cp_lexer *, enum rid);
225 static cp_token *cp_lexer_consume_token
226 (cp_lexer *);
227 static void cp_lexer_purge_token
228 (cp_lexer *);
229 static void cp_lexer_purge_tokens_after
230 (cp_lexer *, cp_token_position);
231 static void cp_lexer_save_tokens
232 (cp_lexer *);
233 static void cp_lexer_commit_tokens
234 (cp_lexer *);
235 static void cp_lexer_rollback_tokens
236 (cp_lexer *);
237 static void cp_lexer_print_token
238 (FILE *, cp_token *);
239 static inline bool cp_lexer_debugging_p
240 (cp_lexer *);
241 static void cp_lexer_start_debugging
242 (cp_lexer *) ATTRIBUTE_UNUSED;
243 static void cp_lexer_stop_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
246 static cp_token_cache *cp_token_cache_new
247 (cp_token *, cp_token *);
249 static void cp_parser_initial_pragma
250 (cp_token *);
252 static void cp_parser_cilk_simd
253 (cp_parser *, cp_token *, bool *);
254 static tree cp_parser_cilk_for
255 (cp_parser *, tree, bool *);
256 static bool cp_parser_omp_declare_reduction_exprs
257 (tree, cp_parser *);
258 static tree cp_parser_cilk_simd_vectorlength
259 (cp_parser *, tree, bool);
260 static void cp_finalize_oacc_routine
261 (cp_parser *, tree, bool);
263 /* Manifest constants. */
264 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
265 #define CP_SAVED_TOKEN_STACK 5
267 /* Variables. */
269 /* The stream to which debugging output should be written. */
270 static FILE *cp_lexer_debug_stream;
272 /* Nonzero if we are parsing an unevaluated operand: an operand to
273 sizeof, typeof, or alignof. */
274 int cp_unevaluated_operand;
276 /* Dump up to NUM tokens in BUFFER to FILE starting with token
277 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
278 first token in BUFFER. If NUM is 0, dump all the tokens. If
279 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
280 highlighted by surrounding it in [[ ]]. */
282 static void
283 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
284 cp_token *start_token, unsigned num,
285 cp_token *curr_token)
287 unsigned i, nprinted;
288 cp_token *token;
289 bool do_print;
291 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
293 if (buffer == NULL)
294 return;
296 if (num == 0)
297 num = buffer->length ();
299 if (start_token == NULL)
300 start_token = buffer->address ();
302 if (start_token > buffer->address ())
304 cp_lexer_print_token (file, &(*buffer)[0]);
305 fprintf (file, " ... ");
308 do_print = false;
309 nprinted = 0;
310 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
312 if (token == start_token)
313 do_print = true;
315 if (!do_print)
316 continue;
318 nprinted++;
319 if (token == curr_token)
320 fprintf (file, "[[");
322 cp_lexer_print_token (file, token);
324 if (token == curr_token)
325 fprintf (file, "]]");
327 switch (token->type)
329 case CPP_SEMICOLON:
330 case CPP_OPEN_BRACE:
331 case CPP_CLOSE_BRACE:
332 case CPP_EOF:
333 fputc ('\n', file);
334 break;
336 default:
337 fputc (' ', file);
341 if (i == num && i < buffer->length ())
343 fprintf (file, " ... ");
344 cp_lexer_print_token (file, &buffer->last ());
347 fprintf (file, "\n");
351 /* Dump all tokens in BUFFER to stderr. */
353 void
354 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
356 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
359 DEBUG_FUNCTION void
360 debug (vec<cp_token, va_gc> &ref)
362 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
365 DEBUG_FUNCTION void
366 debug (vec<cp_token, va_gc> *ptr)
368 if (ptr)
369 debug (*ptr);
370 else
371 fprintf (stderr, "<nil>\n");
375 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
376 description for T. */
378 static void
379 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
381 if (t)
383 fprintf (file, "%s: ", desc);
384 print_node_brief (file, "", t, 0);
389 /* Dump parser context C to FILE. */
391 static void
392 cp_debug_print_context (FILE *file, cp_parser_context *c)
394 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
395 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
396 print_node_brief (file, "", c->object_type, 0);
397 fprintf (file, "}\n");
401 /* Print the stack of parsing contexts to FILE starting with FIRST. */
403 static void
404 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
406 unsigned i;
407 cp_parser_context *c;
409 fprintf (file, "Parsing context stack:\n");
410 for (i = 0, c = first; c; c = c->next, i++)
412 fprintf (file, "\t#%u: ", i);
413 cp_debug_print_context (file, c);
418 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
420 static void
421 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
423 if (flag)
424 fprintf (file, "%s: true\n", desc);
428 /* Print an unparsed function entry UF to FILE. */
430 static void
431 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
433 unsigned i;
434 cp_default_arg_entry *default_arg_fn;
435 tree fn;
437 fprintf (file, "\tFunctions with default args:\n");
438 for (i = 0;
439 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
440 i++)
442 fprintf (file, "\t\tClass type: ");
443 print_node_brief (file, "", default_arg_fn->class_type, 0);
444 fprintf (file, "\t\tDeclaration: ");
445 print_node_brief (file, "", default_arg_fn->decl, 0);
446 fprintf (file, "\n");
449 fprintf (file, "\n\tFunctions with definitions that require "
450 "post-processing\n\t\t");
451 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
453 print_node_brief (file, "", fn, 0);
454 fprintf (file, " ");
456 fprintf (file, "\n");
458 fprintf (file, "\n\tNon-static data members with initializers that require "
459 "post-processing\n\t\t");
460 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
462 print_node_brief (file, "", fn, 0);
463 fprintf (file, " ");
465 fprintf (file, "\n");
469 /* Print the stack of unparsed member functions S to FILE. */
471 static void
472 cp_debug_print_unparsed_queues (FILE *file,
473 vec<cp_unparsed_functions_entry, va_gc> *s)
475 unsigned i;
476 cp_unparsed_functions_entry *uf;
478 fprintf (file, "Unparsed functions\n");
479 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
481 fprintf (file, "#%u:\n", i);
482 cp_debug_print_unparsed_function (file, uf);
487 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
488 the given PARSER. If FILE is NULL, the output is printed on stderr. */
490 static void
491 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
493 cp_token *next_token, *first_token, *start_token;
495 if (file == NULL)
496 file = stderr;
498 next_token = parser->lexer->next_token;
499 first_token = parser->lexer->buffer->address ();
500 start_token = (next_token > first_token + window_size / 2)
501 ? next_token - window_size / 2
502 : first_token;
503 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
504 next_token);
508 /* Dump debugging information for the given PARSER. If FILE is NULL,
509 the output is printed on stderr. */
511 void
512 cp_debug_parser (FILE *file, cp_parser *parser)
514 const size_t window_size = 20;
515 cp_token *token;
516 expanded_location eloc;
518 if (file == NULL)
519 file = stderr;
521 fprintf (file, "Parser state\n\n");
522 fprintf (file, "Number of tokens: %u\n",
523 vec_safe_length (parser->lexer->buffer));
524 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
525 cp_debug_print_tree_if_set (file, "Object scope",
526 parser->object_scope);
527 cp_debug_print_tree_if_set (file, "Qualifying scope",
528 parser->qualifying_scope);
529 cp_debug_print_context_stack (file, parser->context);
530 cp_debug_print_flag (file, "Allow GNU extensions",
531 parser->allow_gnu_extensions_p);
532 cp_debug_print_flag (file, "'>' token is greater-than",
533 parser->greater_than_is_operator_p);
534 cp_debug_print_flag (file, "Default args allowed in current "
535 "parameter list", parser->default_arg_ok_p);
536 cp_debug_print_flag (file, "Parsing integral constant-expression",
537 parser->integral_constant_expression_p);
538 cp_debug_print_flag (file, "Allow non-constant expression in current "
539 "constant-expression",
540 parser->allow_non_integral_constant_expression_p);
541 cp_debug_print_flag (file, "Seen non-constant expression",
542 parser->non_integral_constant_expression_p);
543 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
544 "current context",
545 parser->local_variables_forbidden_p);
546 cp_debug_print_flag (file, "In unbraced linkage specification",
547 parser->in_unbraced_linkage_specification_p);
548 cp_debug_print_flag (file, "Parsing a declarator",
549 parser->in_declarator_p);
550 cp_debug_print_flag (file, "In template argument list",
551 parser->in_template_argument_list_p);
552 cp_debug_print_flag (file, "Parsing an iteration statement",
553 parser->in_statement & IN_ITERATION_STMT);
554 cp_debug_print_flag (file, "Parsing a switch statement",
555 parser->in_statement & IN_SWITCH_STMT);
556 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
557 parser->in_statement & IN_OMP_BLOCK);
558 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
559 parser->in_statement & IN_CILK_SIMD_FOR);
560 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
561 parser->in_statement & IN_OMP_FOR);
562 cp_debug_print_flag (file, "Parsing an if statement",
563 parser->in_statement & IN_IF_STMT);
564 cp_debug_print_flag (file, "Parsing a type-id in an expression "
565 "context", parser->in_type_id_in_expr_p);
566 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
567 parser->implicit_extern_c);
568 cp_debug_print_flag (file, "String expressions should be translated "
569 "to execution character set",
570 parser->translate_strings_p);
571 cp_debug_print_flag (file, "Parsing function body outside of a "
572 "local class", parser->in_function_body);
573 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
574 parser->colon_corrects_to_scope_p);
575 cp_debug_print_flag (file, "Colon doesn't start a class definition",
576 parser->colon_doesnt_start_class_def_p);
577 if (parser->type_definition_forbidden_message)
578 fprintf (file, "Error message for forbidden type definitions: %s\n",
579 parser->type_definition_forbidden_message);
580 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
581 fprintf (file, "Number of class definitions in progress: %u\n",
582 parser->num_classes_being_defined);
583 fprintf (file, "Number of template parameter lists for the current "
584 "declaration: %u\n", parser->num_template_parameter_lists);
585 cp_debug_parser_tokens (file, parser, window_size);
586 token = parser->lexer->next_token;
587 fprintf (file, "Next token to parse:\n");
588 fprintf (file, "\tToken: ");
589 cp_lexer_print_token (file, token);
590 eloc = expand_location (token->location);
591 fprintf (file, "\n\tFile: %s\n", eloc.file);
592 fprintf (file, "\tLine: %d\n", eloc.line);
593 fprintf (file, "\tColumn: %d\n", eloc.column);
596 DEBUG_FUNCTION void
597 debug (cp_parser &ref)
599 cp_debug_parser (stderr, &ref);
602 DEBUG_FUNCTION void
603 debug (cp_parser *ptr)
605 if (ptr)
606 debug (*ptr);
607 else
608 fprintf (stderr, "<nil>\n");
611 /* Allocate memory for a new lexer object and return it. */
613 static cp_lexer *
614 cp_lexer_alloc (void)
616 cp_lexer *lexer;
618 c_common_no_more_pch ();
620 /* Allocate the memory. */
621 lexer = ggc_cleared_alloc<cp_lexer> ();
623 /* Initially we are not debugging. */
624 lexer->debugging_p = false;
626 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
628 /* Create the buffer. */
629 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
631 return lexer;
635 /* Create a new main C++ lexer, the lexer that gets tokens from the
636 preprocessor. */
638 static cp_lexer *
639 cp_lexer_new_main (void)
641 cp_lexer *lexer;
642 cp_token token;
644 /* It's possible that parsing the first pragma will load a PCH file,
645 which is a GC collection point. So we have to do that before
646 allocating any memory. */
647 cp_parser_initial_pragma (&token);
649 lexer = cp_lexer_alloc ();
651 /* Put the first token in the buffer. */
652 lexer->buffer->quick_push (token);
654 /* Get the remaining tokens from the preprocessor. */
655 while (token.type != CPP_EOF)
657 cp_lexer_get_preprocessor_token (lexer, &token);
658 vec_safe_push (lexer->buffer, token);
661 lexer->last_token = lexer->buffer->address ()
662 + lexer->buffer->length ()
663 - 1;
664 lexer->next_token = lexer->buffer->length ()
665 ? lexer->buffer->address ()
666 : &eof_token;
668 /* Subsequent preprocessor diagnostics should use compiler
669 diagnostic functions to get the compiler source location. */
670 done_lexing = true;
672 gcc_assert (!lexer->next_token->purged_p);
673 return lexer;
676 /* Create a new lexer whose token stream is primed with the tokens in
677 CACHE. When these tokens are exhausted, no new tokens will be read. */
679 static cp_lexer *
680 cp_lexer_new_from_tokens (cp_token_cache *cache)
682 cp_token *first = cache->first;
683 cp_token *last = cache->last;
684 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
686 /* We do not own the buffer. */
687 lexer->buffer = NULL;
688 lexer->next_token = first == last ? &eof_token : first;
689 lexer->last_token = last;
691 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
693 /* Initially we are not debugging. */
694 lexer->debugging_p = false;
696 gcc_assert (!lexer->next_token->purged_p);
697 return lexer;
700 /* Frees all resources associated with LEXER. */
702 static void
703 cp_lexer_destroy (cp_lexer *lexer)
705 vec_free (lexer->buffer);
706 lexer->saved_tokens.release ();
707 ggc_free (lexer);
710 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
711 be used. The point of this flag is to help the compiler to fold away calls
712 to cp_lexer_debugging_p within this source file at compile time, when the
713 lexer is not being debugged. */
715 #define LEXER_DEBUGGING_ENABLED_P false
717 /* Returns nonzero if debugging information should be output. */
719 static inline bool
720 cp_lexer_debugging_p (cp_lexer *lexer)
722 if (!LEXER_DEBUGGING_ENABLED_P)
723 return false;
725 return lexer->debugging_p;
729 static inline cp_token_position
730 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
732 gcc_assert (!previous_p || lexer->next_token != &eof_token);
734 return lexer->next_token - previous_p;
737 static inline cp_token *
738 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
740 return pos;
743 static inline void
744 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
746 lexer->next_token = cp_lexer_token_at (lexer, pos);
749 static inline cp_token_position
750 cp_lexer_previous_token_position (cp_lexer *lexer)
752 if (lexer->next_token == &eof_token)
753 return lexer->last_token - 1;
754 else
755 return cp_lexer_token_position (lexer, true);
758 static inline cp_token *
759 cp_lexer_previous_token (cp_lexer *lexer)
761 cp_token_position tp = cp_lexer_previous_token_position (lexer);
763 /* Skip past purged tokens. */
764 while (tp->purged_p)
766 gcc_assert (tp != vec_safe_address (lexer->buffer));
767 tp--;
770 return cp_lexer_token_at (lexer, tp);
773 /* nonzero if we are presently saving tokens. */
775 static inline int
776 cp_lexer_saving_tokens (const cp_lexer* lexer)
778 return lexer->saved_tokens.length () != 0;
781 /* Store the next token from the preprocessor in *TOKEN. Return true
782 if we reach EOF. If LEXER is NULL, assume we are handling an
783 initial #pragma pch_preprocess, and thus want the lexer to return
784 processed strings. */
786 static void
787 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
789 static int is_extern_c = 0;
791 /* Get a new token from the preprocessor. */
792 token->type
793 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
794 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
795 token->keyword = RID_MAX;
796 token->purged_p = false;
797 token->error_reported = false;
799 /* On some systems, some header files are surrounded by an
800 implicit extern "C" block. Set a flag in the token if it
801 comes from such a header. */
802 is_extern_c += pending_lang_change;
803 pending_lang_change = 0;
804 token->implicit_extern_c = is_extern_c > 0;
806 /* Check to see if this token is a keyword. */
807 if (token->type == CPP_NAME)
809 if (IDENTIFIER_KEYWORD_P (token->u.value))
811 /* Mark this token as a keyword. */
812 token->type = CPP_KEYWORD;
813 /* Record which keyword. */
814 token->keyword = C_RID_CODE (token->u.value);
816 else
818 if (warn_cxx11_compat
819 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
820 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
822 /* Warn about the C++0x keyword (but still treat it as
823 an identifier). */
824 warning (OPT_Wc__11_compat,
825 "identifier %qE is a keyword in C++11",
826 token->u.value);
828 /* Clear out the C_RID_CODE so we don't warn about this
829 particular identifier-turned-keyword again. */
830 C_SET_RID_CODE (token->u.value, RID_MAX);
833 token->keyword = RID_MAX;
836 else if (token->type == CPP_AT_NAME)
838 /* This only happens in Objective-C++; it must be a keyword. */
839 token->type = CPP_KEYWORD;
840 switch (C_RID_CODE (token->u.value))
842 /* Replace 'class' with '@class', 'private' with '@private',
843 etc. This prevents confusion with the C++ keyword
844 'class', and makes the tokens consistent with other
845 Objective-C 'AT' keywords. For example '@class' is
846 reported as RID_AT_CLASS which is consistent with
847 '@synchronized', which is reported as
848 RID_AT_SYNCHRONIZED.
850 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
851 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
852 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
853 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
854 case RID_THROW: token->keyword = RID_AT_THROW; break;
855 case RID_TRY: token->keyword = RID_AT_TRY; break;
856 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
857 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
858 default: token->keyword = C_RID_CODE (token->u.value);
863 /* Update the globals input_location and the input file stack from TOKEN. */
864 static inline void
865 cp_lexer_set_source_position_from_token (cp_token *token)
867 if (token->type != CPP_EOF)
869 input_location = token->location;
873 /* Update the globals input_location and the input file stack from LEXER. */
874 static inline void
875 cp_lexer_set_source_position (cp_lexer *lexer)
877 cp_token *token = cp_lexer_peek_token (lexer);
878 cp_lexer_set_source_position_from_token (token);
881 /* Return a pointer to the next token in the token stream, but do not
882 consume it. */
884 static inline cp_token *
885 cp_lexer_peek_token (cp_lexer *lexer)
887 if (cp_lexer_debugging_p (lexer))
889 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
890 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
891 putc ('\n', cp_lexer_debug_stream);
893 return lexer->next_token;
896 /* Return true if the next token has the indicated TYPE. */
898 static inline bool
899 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
901 return cp_lexer_peek_token (lexer)->type == type;
904 /* Return true if the next token does not have the indicated TYPE. */
906 static inline bool
907 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
909 return !cp_lexer_next_token_is (lexer, type);
912 /* Return true if the next token is the indicated KEYWORD. */
914 static inline bool
915 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
917 return cp_lexer_peek_token (lexer)->keyword == keyword;
920 static inline bool
921 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
923 return cp_lexer_peek_nth_token (lexer, n)->type == type;
926 static inline bool
927 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
929 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
932 /* Return true if the next token is not the indicated KEYWORD. */
934 static inline bool
935 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
937 return cp_lexer_peek_token (lexer)->keyword != keyword;
940 /* Return true if KEYWORD can start a decl-specifier. */
942 bool
943 cp_keyword_starts_decl_specifier_p (enum rid keyword)
945 switch (keyword)
947 /* auto specifier: storage-class-specifier in C++,
948 simple-type-specifier in C++0x. */
949 case RID_AUTO:
950 /* Storage classes. */
951 case RID_REGISTER:
952 case RID_STATIC:
953 case RID_EXTERN:
954 case RID_MUTABLE:
955 case RID_THREAD:
956 /* Elaborated type specifiers. */
957 case RID_ENUM:
958 case RID_CLASS:
959 case RID_STRUCT:
960 case RID_UNION:
961 case RID_TYPENAME:
962 /* Simple type specifiers. */
963 case RID_CHAR:
964 case RID_CHAR16:
965 case RID_CHAR32:
966 case RID_WCHAR:
967 case RID_BOOL:
968 case RID_SHORT:
969 case RID_INT:
970 case RID_LONG:
971 case RID_SIGNED:
972 case RID_UNSIGNED:
973 case RID_FLOAT:
974 case RID_DOUBLE:
975 case RID_VOID:
976 /* GNU extensions. */
977 case RID_ATTRIBUTE:
978 case RID_TYPEOF:
979 /* C++0x extensions. */
980 case RID_DECLTYPE:
981 case RID_UNDERLYING_TYPE:
982 case RID_CONSTEXPR:
983 return true;
985 default:
986 if (keyword >= RID_FIRST_INT_N
987 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
988 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
989 return true;
990 return false;
994 /* Return true if the next token is a keyword for a decl-specifier. */
996 static bool
997 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
999 cp_token *token;
1001 token = cp_lexer_peek_token (lexer);
1002 return cp_keyword_starts_decl_specifier_p (token->keyword);
1005 /* Returns TRUE iff the token T begins a decltype type. */
1007 static bool
1008 token_is_decltype (cp_token *t)
1010 return (t->keyword == RID_DECLTYPE
1011 || t->type == CPP_DECLTYPE);
1014 /* Returns TRUE iff the next token begins a decltype type. */
1016 static bool
1017 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1019 cp_token *t = cp_lexer_peek_token (lexer);
1020 return token_is_decltype (t);
1023 /* Called when processing a token with tree_check_value; perform or defer the
1024 associated checks and return the value. */
1026 static tree
1027 saved_checks_value (struct tree_check *check_value)
1029 /* Perform any access checks that were deferred. */
1030 vec<deferred_access_check, va_gc> *checks;
1031 deferred_access_check *chk;
1032 checks = check_value->checks;
1033 if (checks)
1035 int i;
1036 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1037 perform_or_defer_access_check (chk->binfo,
1038 chk->decl,
1039 chk->diag_decl, tf_warning_or_error);
1041 /* Return the stored value. */
1042 return check_value->value;
1045 /* Return a pointer to the Nth token in the token stream. If N is 1,
1046 then this is precisely equivalent to cp_lexer_peek_token (except
1047 that it is not inline). One would like to disallow that case, but
1048 there is one case (cp_parser_nth_token_starts_template_id) where
1049 the caller passes a variable for N and it might be 1. */
1051 static cp_token *
1052 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1054 cp_token *token;
1056 /* N is 1-based, not zero-based. */
1057 gcc_assert (n > 0);
1059 if (cp_lexer_debugging_p (lexer))
1060 fprintf (cp_lexer_debug_stream,
1061 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1063 --n;
1064 token = lexer->next_token;
1065 gcc_assert (!n || token != &eof_token);
1066 while (n != 0)
1068 ++token;
1069 if (token == lexer->last_token)
1071 token = &eof_token;
1072 break;
1075 if (!token->purged_p)
1076 --n;
1079 if (cp_lexer_debugging_p (lexer))
1081 cp_lexer_print_token (cp_lexer_debug_stream, token);
1082 putc ('\n', cp_lexer_debug_stream);
1085 return token;
1088 /* Return the next token, and advance the lexer's next_token pointer
1089 to point to the next non-purged token. */
1091 static cp_token *
1092 cp_lexer_consume_token (cp_lexer* lexer)
1094 cp_token *token = lexer->next_token;
1096 gcc_assert (token != &eof_token);
1097 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1101 lexer->next_token++;
1102 if (lexer->next_token == lexer->last_token)
1104 lexer->next_token = &eof_token;
1105 break;
1109 while (lexer->next_token->purged_p);
1111 cp_lexer_set_source_position_from_token (token);
1113 /* Provide debugging output. */
1114 if (cp_lexer_debugging_p (lexer))
1116 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1117 cp_lexer_print_token (cp_lexer_debug_stream, token);
1118 putc ('\n', cp_lexer_debug_stream);
1121 return token;
1124 /* Permanently remove the next token from the token stream, and
1125 advance the next_token pointer to refer to the next non-purged
1126 token. */
1128 static void
1129 cp_lexer_purge_token (cp_lexer *lexer)
1131 cp_token *tok = lexer->next_token;
1133 gcc_assert (tok != &eof_token);
1134 tok->purged_p = true;
1135 tok->location = UNKNOWN_LOCATION;
1136 tok->u.value = NULL_TREE;
1137 tok->keyword = RID_MAX;
1141 tok++;
1142 if (tok == lexer->last_token)
1144 tok = &eof_token;
1145 break;
1148 while (tok->purged_p);
1149 lexer->next_token = tok;
1152 /* Permanently remove all tokens after TOK, up to, but not
1153 including, the token that will be returned next by
1154 cp_lexer_peek_token. */
1156 static void
1157 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1159 cp_token *peek = lexer->next_token;
1161 if (peek == &eof_token)
1162 peek = lexer->last_token;
1164 gcc_assert (tok < peek);
1166 for ( tok += 1; tok != peek; tok += 1)
1168 tok->purged_p = true;
1169 tok->location = UNKNOWN_LOCATION;
1170 tok->u.value = NULL_TREE;
1171 tok->keyword = RID_MAX;
1175 /* Begin saving tokens. All tokens consumed after this point will be
1176 preserved. */
1178 static void
1179 cp_lexer_save_tokens (cp_lexer* lexer)
1181 /* Provide debugging output. */
1182 if (cp_lexer_debugging_p (lexer))
1183 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1185 lexer->saved_tokens.safe_push (lexer->next_token);
1188 /* Commit to the portion of the token stream most recently saved. */
1190 static void
1191 cp_lexer_commit_tokens (cp_lexer* lexer)
1193 /* Provide debugging output. */
1194 if (cp_lexer_debugging_p (lexer))
1195 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1197 lexer->saved_tokens.pop ();
1200 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1201 to the token stream. Stop saving tokens. */
1203 static void
1204 cp_lexer_rollback_tokens (cp_lexer* lexer)
1206 /* Provide debugging output. */
1207 if (cp_lexer_debugging_p (lexer))
1208 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1210 lexer->next_token = lexer->saved_tokens.pop ();
1213 /* RAII wrapper around the above functions, with sanity checking. Creating
1214 a variable saves tokens, which are committed when the variable is
1215 destroyed unless they are explicitly rolled back by calling the rollback
1216 member function. */
1218 struct saved_token_sentinel
1220 cp_lexer *lexer;
1221 unsigned len;
1222 bool commit;
1223 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1225 len = lexer->saved_tokens.length ();
1226 cp_lexer_save_tokens (lexer);
1228 void rollback ()
1230 cp_lexer_rollback_tokens (lexer);
1231 commit = false;
1233 ~saved_token_sentinel()
1235 if (commit)
1236 cp_lexer_commit_tokens (lexer);
1237 gcc_assert (lexer->saved_tokens.length () == len);
1241 /* Print a representation of the TOKEN on the STREAM. */
1243 static void
1244 cp_lexer_print_token (FILE * stream, cp_token *token)
1246 /* We don't use cpp_type2name here because the parser defines
1247 a few tokens of its own. */
1248 static const char *const token_names[] = {
1249 /* cpplib-defined token types */
1250 #define OP(e, s) #e,
1251 #define TK(e, s) #e,
1252 TTYPE_TABLE
1253 #undef OP
1254 #undef TK
1255 /* C++ parser token types - see "Manifest constants", above. */
1256 "KEYWORD",
1257 "TEMPLATE_ID",
1258 "NESTED_NAME_SPECIFIER",
1261 /* For some tokens, print the associated data. */
1262 switch (token->type)
1264 case CPP_KEYWORD:
1265 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1266 For example, `struct' is mapped to an INTEGER_CST. */
1267 if (!identifier_p (token->u.value))
1268 break;
1269 /* fall through */
1270 case CPP_NAME:
1271 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1272 break;
1274 case CPP_STRING:
1275 case CPP_STRING16:
1276 case CPP_STRING32:
1277 case CPP_WSTRING:
1278 case CPP_UTF8STRING:
1279 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1280 break;
1282 case CPP_NUMBER:
1283 print_generic_expr (stream, token->u.value);
1284 break;
1286 default:
1287 /* If we have a name for the token, print it out. Otherwise, we
1288 simply give the numeric code. */
1289 if (token->type < ARRAY_SIZE(token_names))
1290 fputs (token_names[token->type], stream);
1291 else
1292 fprintf (stream, "[%d]", token->type);
1293 break;
1297 DEBUG_FUNCTION void
1298 debug (cp_token &ref)
1300 cp_lexer_print_token (stderr, &ref);
1301 fprintf (stderr, "\n");
1304 DEBUG_FUNCTION void
1305 debug (cp_token *ptr)
1307 if (ptr)
1308 debug (*ptr);
1309 else
1310 fprintf (stderr, "<nil>\n");
1314 /* Start emitting debugging information. */
1316 static void
1317 cp_lexer_start_debugging (cp_lexer* lexer)
1319 if (!LEXER_DEBUGGING_ENABLED_P)
1320 fatal_error (input_location,
1321 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1323 lexer->debugging_p = true;
1324 cp_lexer_debug_stream = stderr;
1327 /* Stop emitting debugging information. */
1329 static void
1330 cp_lexer_stop_debugging (cp_lexer* lexer)
1332 if (!LEXER_DEBUGGING_ENABLED_P)
1333 fatal_error (input_location,
1334 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1336 lexer->debugging_p = false;
1337 cp_lexer_debug_stream = NULL;
1340 /* Create a new cp_token_cache, representing a range of tokens. */
1342 static cp_token_cache *
1343 cp_token_cache_new (cp_token *first, cp_token *last)
1345 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1346 cache->first = first;
1347 cache->last = last;
1348 return cache;
1351 /* Diagnose if #pragma omp declare simd isn't followed immediately
1352 by function declaration or definition. */
1354 static inline void
1355 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1357 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1359 error ("%<#pragma omp declare simd%> not immediately followed by "
1360 "function declaration or definition");
1361 parser->omp_declare_simd = NULL;
1365 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1366 and put that into "omp declare simd" attribute. */
1368 static inline void
1369 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1371 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1373 if (fndecl == error_mark_node)
1375 parser->omp_declare_simd = NULL;
1376 return;
1378 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1380 cp_ensure_no_omp_declare_simd (parser);
1381 return;
1386 /* Diagnose if #pragma acc routine isn't followed immediately by function
1387 declaration or definition. */
1389 static inline void
1390 cp_ensure_no_oacc_routine (cp_parser *parser)
1392 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1394 error_at (parser->oacc_routine->loc,
1395 "%<#pragma acc routine%> not immediately followed by "
1396 "function declaration or definition");
1397 parser->oacc_routine = NULL;
1401 /* Decl-specifiers. */
1403 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1405 static void
1406 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1408 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1411 /* Declarators. */
1413 /* Nothing other than the parser should be creating declarators;
1414 declarators are a semi-syntactic representation of C++ entities.
1415 Other parts of the front end that need to create entities (like
1416 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1418 static cp_declarator *make_call_declarator
1419 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1420 static cp_declarator *make_array_declarator
1421 (cp_declarator *, tree);
1422 static cp_declarator *make_pointer_declarator
1423 (cp_cv_quals, cp_declarator *, tree);
1424 static cp_declarator *make_reference_declarator
1425 (cp_cv_quals, cp_declarator *, bool, tree);
1426 static cp_declarator *make_ptrmem_declarator
1427 (cp_cv_quals, tree, cp_declarator *, tree);
1429 /* An erroneous declarator. */
1430 static cp_declarator *cp_error_declarator;
1432 /* The obstack on which declarators and related data structures are
1433 allocated. */
1434 static struct obstack declarator_obstack;
1436 /* Alloc BYTES from the declarator memory pool. */
1438 static inline void *
1439 alloc_declarator (size_t bytes)
1441 return obstack_alloc (&declarator_obstack, bytes);
1444 /* Allocate a declarator of the indicated KIND. Clear fields that are
1445 common to all declarators. */
1447 static cp_declarator *
1448 make_declarator (cp_declarator_kind kind)
1450 cp_declarator *declarator;
1452 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1453 declarator->kind = kind;
1454 declarator->parenthesized = UNKNOWN_LOCATION;
1455 declarator->attributes = NULL_TREE;
1456 declarator->std_attributes = NULL_TREE;
1457 declarator->declarator = NULL;
1458 declarator->parameter_pack_p = false;
1459 declarator->id_loc = UNKNOWN_LOCATION;
1461 return declarator;
1464 /* Make a declarator for a generalized identifier. If
1465 QUALIFYING_SCOPE is non-NULL, the identifier is
1466 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1467 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1468 is, if any. */
1470 static cp_declarator *
1471 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1472 special_function_kind sfk)
1474 cp_declarator *declarator;
1476 /* It is valid to write:
1478 class C { void f(); };
1479 typedef C D;
1480 void D::f();
1482 The standard is not clear about whether `typedef const C D' is
1483 legal; as of 2002-09-15 the committee is considering that
1484 question. EDG 3.0 allows that syntax. Therefore, we do as
1485 well. */
1486 if (qualifying_scope && TYPE_P (qualifying_scope))
1487 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1489 gcc_assert (identifier_p (unqualified_name)
1490 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1491 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1493 declarator = make_declarator (cdk_id);
1494 declarator->u.id.qualifying_scope = qualifying_scope;
1495 declarator->u.id.unqualified_name = unqualified_name;
1496 declarator->u.id.sfk = sfk;
1498 return declarator;
1501 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1502 of modifiers such as const or volatile to apply to the pointer
1503 type, represented as identifiers. ATTRIBUTES represent the attributes that
1504 appertain to the pointer or reference. */
1506 cp_declarator *
1507 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1508 tree attributes)
1510 cp_declarator *declarator;
1512 declarator = make_declarator (cdk_pointer);
1513 declarator->declarator = target;
1514 declarator->u.pointer.qualifiers = cv_qualifiers;
1515 declarator->u.pointer.class_type = NULL_TREE;
1516 if (target)
1518 declarator->id_loc = target->id_loc;
1519 declarator->parameter_pack_p = target->parameter_pack_p;
1520 target->parameter_pack_p = false;
1522 else
1523 declarator->parameter_pack_p = false;
1525 declarator->std_attributes = attributes;
1527 return declarator;
1530 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1531 represent the attributes that appertain to the pointer or
1532 reference. */
1534 cp_declarator *
1535 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1536 bool rvalue_ref, tree attributes)
1538 cp_declarator *declarator;
1540 declarator = make_declarator (cdk_reference);
1541 declarator->declarator = target;
1542 declarator->u.reference.qualifiers = cv_qualifiers;
1543 declarator->u.reference.rvalue_ref = rvalue_ref;
1544 if (target)
1546 declarator->id_loc = target->id_loc;
1547 declarator->parameter_pack_p = target->parameter_pack_p;
1548 target->parameter_pack_p = false;
1550 else
1551 declarator->parameter_pack_p = false;
1553 declarator->std_attributes = attributes;
1555 return declarator;
1558 /* Like make_pointer_declarator -- but for a pointer to a non-static
1559 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1560 appertain to the pointer or reference. */
1562 cp_declarator *
1563 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1564 cp_declarator *pointee,
1565 tree attributes)
1567 cp_declarator *declarator;
1569 declarator = make_declarator (cdk_ptrmem);
1570 declarator->declarator = pointee;
1571 declarator->u.pointer.qualifiers = cv_qualifiers;
1572 declarator->u.pointer.class_type = class_type;
1574 if (pointee)
1576 declarator->parameter_pack_p = pointee->parameter_pack_p;
1577 pointee->parameter_pack_p = false;
1579 else
1580 declarator->parameter_pack_p = false;
1582 declarator->std_attributes = attributes;
1584 return declarator;
1587 /* Make a declarator for the function given by TARGET, with the
1588 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1589 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1590 indicates what exceptions can be thrown. */
1592 cp_declarator *
1593 make_call_declarator (cp_declarator *target,
1594 tree parms,
1595 cp_cv_quals cv_qualifiers,
1596 cp_virt_specifiers virt_specifiers,
1597 cp_ref_qualifier ref_qualifier,
1598 tree tx_qualifier,
1599 tree exception_specification,
1600 tree late_return_type,
1601 tree requires_clause)
1603 cp_declarator *declarator;
1605 declarator = make_declarator (cdk_function);
1606 declarator->declarator = target;
1607 declarator->u.function.parameters = parms;
1608 declarator->u.function.qualifiers = cv_qualifiers;
1609 declarator->u.function.virt_specifiers = virt_specifiers;
1610 declarator->u.function.ref_qualifier = ref_qualifier;
1611 declarator->u.function.tx_qualifier = tx_qualifier;
1612 declarator->u.function.exception_specification = exception_specification;
1613 declarator->u.function.late_return_type = late_return_type;
1614 declarator->u.function.requires_clause = requires_clause;
1615 if (target)
1617 declarator->id_loc = target->id_loc;
1618 declarator->parameter_pack_p = target->parameter_pack_p;
1619 target->parameter_pack_p = false;
1621 else
1622 declarator->parameter_pack_p = false;
1624 return declarator;
1627 /* Make a declarator for an array of BOUNDS elements, each of which is
1628 defined by ELEMENT. */
1630 cp_declarator *
1631 make_array_declarator (cp_declarator *element, tree bounds)
1633 cp_declarator *declarator;
1635 declarator = make_declarator (cdk_array);
1636 declarator->declarator = element;
1637 declarator->u.array.bounds = bounds;
1638 if (element)
1640 declarator->id_loc = element->id_loc;
1641 declarator->parameter_pack_p = element->parameter_pack_p;
1642 element->parameter_pack_p = false;
1644 else
1645 declarator->parameter_pack_p = false;
1647 return declarator;
1650 /* Determine whether the declarator we've seen so far can be a
1651 parameter pack, when followed by an ellipsis. */
1652 static bool
1653 declarator_can_be_parameter_pack (cp_declarator *declarator)
1655 if (declarator && declarator->parameter_pack_p)
1656 /* We already saw an ellipsis. */
1657 return false;
1659 /* Search for a declarator name, or any other declarator that goes
1660 after the point where the ellipsis could appear in a parameter
1661 pack. If we find any of these, then this declarator can not be
1662 made into a parameter pack. */
1663 bool found = false;
1664 while (declarator && !found)
1666 switch ((int)declarator->kind)
1668 case cdk_id:
1669 case cdk_array:
1670 case cdk_decomp:
1671 found = true;
1672 break;
1674 case cdk_error:
1675 return true;
1677 default:
1678 declarator = declarator->declarator;
1679 break;
1683 return !found;
1686 cp_parameter_declarator *no_parameters;
1688 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1689 DECLARATOR and DEFAULT_ARGUMENT. */
1691 cp_parameter_declarator *
1692 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1693 cp_declarator *declarator,
1694 tree default_argument,
1695 location_t loc,
1696 bool template_parameter_pack_p = false)
1698 cp_parameter_declarator *parameter;
1700 parameter = ((cp_parameter_declarator *)
1701 alloc_declarator (sizeof (cp_parameter_declarator)));
1702 parameter->next = NULL;
1703 if (decl_specifiers)
1704 parameter->decl_specifiers = *decl_specifiers;
1705 else
1706 clear_decl_specs (&parameter->decl_specifiers);
1707 parameter->declarator = declarator;
1708 parameter->default_argument = default_argument;
1709 parameter->template_parameter_pack_p = template_parameter_pack_p;
1710 parameter->loc = loc;
1712 return parameter;
1715 /* Returns true iff DECLARATOR is a declaration for a function. */
1717 static bool
1718 function_declarator_p (const cp_declarator *declarator)
1720 while (declarator)
1722 if (declarator->kind == cdk_function
1723 && declarator->declarator->kind == cdk_id)
1724 return true;
1725 if (declarator->kind == cdk_id
1726 || declarator->kind == cdk_decomp
1727 || declarator->kind == cdk_error)
1728 return false;
1729 declarator = declarator->declarator;
1731 return false;
1734 /* The parser. */
1736 /* Overview
1737 --------
1739 A cp_parser parses the token stream as specified by the C++
1740 grammar. Its job is purely parsing, not semantic analysis. For
1741 example, the parser breaks the token stream into declarators,
1742 expressions, statements, and other similar syntactic constructs.
1743 It does not check that the types of the expressions on either side
1744 of an assignment-statement are compatible, or that a function is
1745 not declared with a parameter of type `void'.
1747 The parser invokes routines elsewhere in the compiler to perform
1748 semantic analysis and to build up the abstract syntax tree for the
1749 code processed.
1751 The parser (and the template instantiation code, which is, in a
1752 way, a close relative of parsing) are the only parts of the
1753 compiler that should be calling push_scope and pop_scope, or
1754 related functions. The parser (and template instantiation code)
1755 keeps track of what scope is presently active; everything else
1756 should simply honor that. (The code that generates static
1757 initializers may also need to set the scope, in order to check
1758 access control correctly when emitting the initializers.)
1760 Methodology
1761 -----------
1763 The parser is of the standard recursive-descent variety. Upcoming
1764 tokens in the token stream are examined in order to determine which
1765 production to use when parsing a non-terminal. Some C++ constructs
1766 require arbitrary look ahead to disambiguate. For example, it is
1767 impossible, in the general case, to tell whether a statement is an
1768 expression or declaration without scanning the entire statement.
1769 Therefore, the parser is capable of "parsing tentatively." When the
1770 parser is not sure what construct comes next, it enters this mode.
1771 Then, while we attempt to parse the construct, the parser queues up
1772 error messages, rather than issuing them immediately, and saves the
1773 tokens it consumes. If the construct is parsed successfully, the
1774 parser "commits", i.e., it issues any queued error messages and
1775 the tokens that were being preserved are permanently discarded.
1776 If, however, the construct is not parsed successfully, the parser
1777 rolls back its state completely so that it can resume parsing using
1778 a different alternative.
1780 Future Improvements
1781 -------------------
1783 The performance of the parser could probably be improved substantially.
1784 We could often eliminate the need to parse tentatively by looking ahead
1785 a little bit. In some places, this approach might not entirely eliminate
1786 the need to parse tentatively, but it might still speed up the average
1787 case. */
1789 /* Flags that are passed to some parsing functions. These values can
1790 be bitwise-ored together. */
1792 enum
1794 /* No flags. */
1795 CP_PARSER_FLAGS_NONE = 0x0,
1796 /* The construct is optional. If it is not present, then no error
1797 should be issued. */
1798 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1799 /* When parsing a type-specifier, treat user-defined type-names
1800 as non-type identifiers. */
1801 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1802 /* When parsing a type-specifier, do not try to parse a class-specifier
1803 or enum-specifier. */
1804 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1805 /* When parsing a decl-specifier-seq, only allow type-specifier or
1806 constexpr. */
1807 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1808 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1809 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1812 /* This type is used for parameters and variables which hold
1813 combinations of the above flags. */
1814 typedef int cp_parser_flags;
1816 /* The different kinds of declarators we want to parse. */
1818 enum cp_parser_declarator_kind
1820 /* We want an abstract declarator. */
1821 CP_PARSER_DECLARATOR_ABSTRACT,
1822 /* We want a named declarator. */
1823 CP_PARSER_DECLARATOR_NAMED,
1824 /* We don't mind, but the name must be an unqualified-id. */
1825 CP_PARSER_DECLARATOR_EITHER
1828 /* The precedence values used to parse binary expressions. The minimum value
1829 of PREC must be 1, because zero is reserved to quickly discriminate
1830 binary operators from other tokens. */
1832 enum cp_parser_prec
1834 PREC_NOT_OPERATOR,
1835 PREC_LOGICAL_OR_EXPRESSION,
1836 PREC_LOGICAL_AND_EXPRESSION,
1837 PREC_INCLUSIVE_OR_EXPRESSION,
1838 PREC_EXCLUSIVE_OR_EXPRESSION,
1839 PREC_AND_EXPRESSION,
1840 PREC_EQUALITY_EXPRESSION,
1841 PREC_RELATIONAL_EXPRESSION,
1842 PREC_SHIFT_EXPRESSION,
1843 PREC_ADDITIVE_EXPRESSION,
1844 PREC_MULTIPLICATIVE_EXPRESSION,
1845 PREC_PM_EXPRESSION,
1846 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1849 /* A mapping from a token type to a corresponding tree node type, with a
1850 precedence value. */
1852 struct cp_parser_binary_operations_map_node
1854 /* The token type. */
1855 enum cpp_ttype token_type;
1856 /* The corresponding tree code. */
1857 enum tree_code tree_type;
1858 /* The precedence of this operator. */
1859 enum cp_parser_prec prec;
1862 struct cp_parser_expression_stack_entry
1864 /* Left hand side of the binary operation we are currently
1865 parsing. */
1866 cp_expr lhs;
1867 /* Original tree code for left hand side, if it was a binary
1868 expression itself (used for -Wparentheses). */
1869 enum tree_code lhs_type;
1870 /* Tree code for the binary operation we are parsing. */
1871 enum tree_code tree_type;
1872 /* Precedence of the binary operation we are parsing. */
1873 enum cp_parser_prec prec;
1874 /* Location of the binary operation we are parsing. */
1875 location_t loc;
1878 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1879 entries because precedence levels on the stack are monotonically
1880 increasing. */
1881 typedef struct cp_parser_expression_stack_entry
1882 cp_parser_expression_stack[NUM_PREC_VALUES];
1884 /* Prototypes. */
1886 /* Constructors and destructors. */
1888 static cp_parser_context *cp_parser_context_new
1889 (cp_parser_context *);
1891 /* Class variables. */
1893 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1895 /* The operator-precedence table used by cp_parser_binary_expression.
1896 Transformed into an associative array (binops_by_token) by
1897 cp_parser_new. */
1899 static const cp_parser_binary_operations_map_node binops[] = {
1900 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1901 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1903 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1904 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1905 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1907 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1908 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1910 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1911 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1913 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1914 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1915 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1916 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1918 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1919 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1921 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1923 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1925 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1927 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1929 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1932 /* The same as binops, but initialized by cp_parser_new so that
1933 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1934 for speed. */
1935 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1937 /* Constructors and destructors. */
1939 /* Construct a new context. The context below this one on the stack
1940 is given by NEXT. */
1942 static cp_parser_context *
1943 cp_parser_context_new (cp_parser_context* next)
1945 cp_parser_context *context;
1947 /* Allocate the storage. */
1948 if (cp_parser_context_free_list != NULL)
1950 /* Pull the first entry from the free list. */
1951 context = cp_parser_context_free_list;
1952 cp_parser_context_free_list = context->next;
1953 memset (context, 0, sizeof (*context));
1955 else
1956 context = ggc_cleared_alloc<cp_parser_context> ();
1958 /* No errors have occurred yet in this context. */
1959 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1960 /* If this is not the bottommost context, copy information that we
1961 need from the previous context. */
1962 if (next)
1964 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1965 expression, then we are parsing one in this context, too. */
1966 context->object_type = next->object_type;
1967 /* Thread the stack. */
1968 context->next = next;
1971 return context;
1974 /* Managing the unparsed function queues. */
1976 #define unparsed_funs_with_default_args \
1977 parser->unparsed_queues->last ().funs_with_default_args
1978 #define unparsed_funs_with_definitions \
1979 parser->unparsed_queues->last ().funs_with_definitions
1980 #define unparsed_nsdmis \
1981 parser->unparsed_queues->last ().nsdmis
1982 #define unparsed_classes \
1983 parser->unparsed_queues->last ().classes
1985 static void
1986 push_unparsed_function_queues (cp_parser *parser)
1988 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1989 vec_safe_push (parser->unparsed_queues, e);
1992 static void
1993 pop_unparsed_function_queues (cp_parser *parser)
1995 release_tree_vector (unparsed_funs_with_definitions);
1996 parser->unparsed_queues->pop ();
1999 /* Prototypes. */
2001 /* Constructors and destructors. */
2003 static cp_parser *cp_parser_new
2004 (void);
2006 /* Routines to parse various constructs.
2008 Those that return `tree' will return the error_mark_node (rather
2009 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2010 Sometimes, they will return an ordinary node if error-recovery was
2011 attempted, even though a parse error occurred. So, to check
2012 whether or not a parse error occurred, you should always use
2013 cp_parser_error_occurred. If the construct is optional (indicated
2014 either by an `_opt' in the name of the function that does the
2015 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2016 the construct is not present. */
2018 /* Lexical conventions [gram.lex] */
2020 static cp_expr cp_parser_identifier
2021 (cp_parser *);
2022 static cp_expr cp_parser_string_literal
2023 (cp_parser *, bool, bool, bool);
2024 static cp_expr cp_parser_userdef_char_literal
2025 (cp_parser *);
2026 static tree cp_parser_userdef_string_literal
2027 (tree);
2028 static cp_expr cp_parser_userdef_numeric_literal
2029 (cp_parser *);
2031 /* Basic concepts [gram.basic] */
2033 static bool cp_parser_translation_unit
2034 (cp_parser *);
2036 /* Expressions [gram.expr] */
2038 static cp_expr cp_parser_primary_expression
2039 (cp_parser *, bool, bool, bool, cp_id_kind *);
2040 static cp_expr cp_parser_id_expression
2041 (cp_parser *, bool, bool, bool *, bool, bool);
2042 static cp_expr cp_parser_unqualified_id
2043 (cp_parser *, bool, bool, bool, bool);
2044 static tree cp_parser_nested_name_specifier_opt
2045 (cp_parser *, bool, bool, bool, bool, bool = false);
2046 static tree cp_parser_nested_name_specifier
2047 (cp_parser *, bool, bool, bool, bool);
2048 static tree cp_parser_qualifying_entity
2049 (cp_parser *, bool, bool, bool, bool, bool);
2050 static cp_expr cp_parser_postfix_expression
2051 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2052 static tree cp_parser_postfix_open_square_expression
2053 (cp_parser *, tree, bool, bool);
2054 static tree cp_parser_postfix_dot_deref_expression
2055 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2056 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2057 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2058 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2059 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2060 static void cp_parser_pseudo_destructor_name
2061 (cp_parser *, tree, tree *, tree *);
2062 static cp_expr cp_parser_unary_expression
2063 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2064 static enum tree_code cp_parser_unary_operator
2065 (cp_token *);
2066 static tree cp_parser_new_expression
2067 (cp_parser *);
2068 static vec<tree, va_gc> *cp_parser_new_placement
2069 (cp_parser *);
2070 static tree cp_parser_new_type_id
2071 (cp_parser *, tree *);
2072 static cp_declarator *cp_parser_new_declarator_opt
2073 (cp_parser *);
2074 static cp_declarator *cp_parser_direct_new_declarator
2075 (cp_parser *);
2076 static vec<tree, va_gc> *cp_parser_new_initializer
2077 (cp_parser *);
2078 static tree cp_parser_delete_expression
2079 (cp_parser *);
2080 static cp_expr cp_parser_cast_expression
2081 (cp_parser *, bool, bool, bool, cp_id_kind *);
2082 static cp_expr cp_parser_binary_expression
2083 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2084 static tree cp_parser_question_colon_clause
2085 (cp_parser *, cp_expr);
2086 static cp_expr cp_parser_assignment_expression
2087 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2088 static enum tree_code cp_parser_assignment_operator_opt
2089 (cp_parser *);
2090 static cp_expr cp_parser_expression
2091 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2092 static cp_expr cp_parser_constant_expression
2093 (cp_parser *, bool = false, bool * = NULL, bool = false);
2094 static cp_expr cp_parser_builtin_offsetof
2095 (cp_parser *);
2096 static cp_expr cp_parser_lambda_expression
2097 (cp_parser *);
2098 static void cp_parser_lambda_introducer
2099 (cp_parser *, tree);
2100 static bool cp_parser_lambda_declarator_opt
2101 (cp_parser *, tree);
2102 static void cp_parser_lambda_body
2103 (cp_parser *, tree);
2105 /* Statements [gram.stmt.stmt] */
2107 static void cp_parser_statement
2108 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2109 static void cp_parser_label_for_labeled_statement
2110 (cp_parser *, tree);
2111 static tree cp_parser_expression_statement
2112 (cp_parser *, tree);
2113 static tree cp_parser_compound_statement
2114 (cp_parser *, tree, int, bool);
2115 static void cp_parser_statement_seq_opt
2116 (cp_parser *, tree);
2117 static tree cp_parser_selection_statement
2118 (cp_parser *, bool *, vec<tree> *);
2119 static tree cp_parser_condition
2120 (cp_parser *);
2121 static tree cp_parser_iteration_statement
2122 (cp_parser *, bool *, bool);
2123 static bool cp_parser_init_statement
2124 (cp_parser *, tree *decl);
2125 static tree cp_parser_for
2126 (cp_parser *, bool);
2127 static tree cp_parser_c_for
2128 (cp_parser *, tree, tree, bool);
2129 static tree cp_parser_range_for
2130 (cp_parser *, tree, tree, tree, bool);
2131 static void do_range_for_auto_deduction
2132 (tree, tree);
2133 static tree cp_parser_perform_range_for_lookup
2134 (tree, tree *, tree *);
2135 static tree cp_parser_range_for_member_function
2136 (tree, tree);
2137 static tree cp_parser_jump_statement
2138 (cp_parser *);
2139 static void cp_parser_declaration_statement
2140 (cp_parser *);
2142 static tree cp_parser_implicitly_scoped_statement
2143 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2144 static void cp_parser_already_scoped_statement
2145 (cp_parser *, bool *, const token_indent_info &);
2147 /* Declarations [gram.dcl.dcl] */
2149 static void cp_parser_declaration_seq_opt
2150 (cp_parser *);
2151 static void cp_parser_declaration
2152 (cp_parser *);
2153 static void cp_parser_block_declaration
2154 (cp_parser *, bool);
2155 static void cp_parser_simple_declaration
2156 (cp_parser *, bool, tree *);
2157 static void cp_parser_decl_specifier_seq
2158 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2159 static tree cp_parser_storage_class_specifier_opt
2160 (cp_parser *);
2161 static tree cp_parser_function_specifier_opt
2162 (cp_parser *, cp_decl_specifier_seq *);
2163 static tree cp_parser_type_specifier
2164 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2165 int *, bool *);
2166 static tree cp_parser_simple_type_specifier
2167 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2168 static tree cp_parser_type_name
2169 (cp_parser *, bool);
2170 static tree cp_parser_type_name
2171 (cp_parser *);
2172 static tree cp_parser_nonclass_name
2173 (cp_parser* parser);
2174 static tree cp_parser_elaborated_type_specifier
2175 (cp_parser *, bool, bool);
2176 static tree cp_parser_enum_specifier
2177 (cp_parser *);
2178 static void cp_parser_enumerator_list
2179 (cp_parser *, tree);
2180 static void cp_parser_enumerator_definition
2181 (cp_parser *, tree);
2182 static tree cp_parser_namespace_name
2183 (cp_parser *);
2184 static void cp_parser_namespace_definition
2185 (cp_parser *);
2186 static void cp_parser_namespace_body
2187 (cp_parser *);
2188 static tree cp_parser_qualified_namespace_specifier
2189 (cp_parser *);
2190 static void cp_parser_namespace_alias_definition
2191 (cp_parser *);
2192 static bool cp_parser_using_declaration
2193 (cp_parser *, bool);
2194 static void cp_parser_using_directive
2195 (cp_parser *);
2196 static tree cp_parser_alias_declaration
2197 (cp_parser *);
2198 static void cp_parser_asm_definition
2199 (cp_parser *);
2200 static void cp_parser_linkage_specification
2201 (cp_parser *);
2202 static void cp_parser_static_assert
2203 (cp_parser *, bool);
2204 static tree cp_parser_decltype
2205 (cp_parser *);
2206 static tree cp_parser_decomposition_declaration
2207 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2209 /* Declarators [gram.dcl.decl] */
2211 static tree cp_parser_init_declarator
2212 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2213 bool, bool, int, bool *, tree *, location_t *, tree *);
2214 static cp_declarator *cp_parser_declarator
2215 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2216 static cp_declarator *cp_parser_direct_declarator
2217 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2218 static enum tree_code cp_parser_ptr_operator
2219 (cp_parser *, tree *, cp_cv_quals *, tree *);
2220 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2221 (cp_parser *);
2222 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2223 (cp_parser *);
2224 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2225 (cp_parser *);
2226 static tree cp_parser_tx_qualifier_opt
2227 (cp_parser *);
2228 static tree cp_parser_late_return_type_opt
2229 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2230 static tree cp_parser_declarator_id
2231 (cp_parser *, bool);
2232 static tree cp_parser_type_id
2233 (cp_parser *);
2234 static tree cp_parser_template_type_arg
2235 (cp_parser *);
2236 static tree cp_parser_trailing_type_id (cp_parser *);
2237 static tree cp_parser_type_id_1
2238 (cp_parser *, bool, bool);
2239 static void cp_parser_type_specifier_seq
2240 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2241 static tree cp_parser_parameter_declaration_clause
2242 (cp_parser *);
2243 static tree cp_parser_parameter_declaration_list
2244 (cp_parser *, bool *);
2245 static cp_parameter_declarator *cp_parser_parameter_declaration
2246 (cp_parser *, bool, bool *);
2247 static tree cp_parser_default_argument
2248 (cp_parser *, bool);
2249 static void cp_parser_function_body
2250 (cp_parser *, bool);
2251 static tree cp_parser_initializer
2252 (cp_parser *, bool *, bool *);
2253 static cp_expr cp_parser_initializer_clause
2254 (cp_parser *, bool *);
2255 static cp_expr cp_parser_braced_list
2256 (cp_parser*, bool*);
2257 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2258 (cp_parser *, bool *);
2260 static void cp_parser_ctor_initializer_opt_and_function_body
2261 (cp_parser *, bool);
2263 static tree cp_parser_late_parsing_omp_declare_simd
2264 (cp_parser *, tree);
2266 static tree cp_parser_late_parsing_cilk_simd_fn_info
2267 (cp_parser *, tree);
2269 static tree cp_parser_late_parsing_oacc_routine
2270 (cp_parser *, tree);
2272 static tree synthesize_implicit_template_parm
2273 (cp_parser *, tree);
2274 static tree finish_fully_implicit_template
2275 (cp_parser *, tree);
2277 /* Classes [gram.class] */
2279 static tree cp_parser_class_name
2280 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2281 static tree cp_parser_class_specifier
2282 (cp_parser *);
2283 static tree cp_parser_class_head
2284 (cp_parser *, bool *);
2285 static enum tag_types cp_parser_class_key
2286 (cp_parser *);
2287 static void cp_parser_type_parameter_key
2288 (cp_parser* parser);
2289 static void cp_parser_member_specification_opt
2290 (cp_parser *);
2291 static void cp_parser_member_declaration
2292 (cp_parser *);
2293 static tree cp_parser_pure_specifier
2294 (cp_parser *);
2295 static tree cp_parser_constant_initializer
2296 (cp_parser *);
2298 /* Derived classes [gram.class.derived] */
2300 static tree cp_parser_base_clause
2301 (cp_parser *);
2302 static tree cp_parser_base_specifier
2303 (cp_parser *);
2305 /* Special member functions [gram.special] */
2307 static tree cp_parser_conversion_function_id
2308 (cp_parser *);
2309 static tree cp_parser_conversion_type_id
2310 (cp_parser *);
2311 static cp_declarator *cp_parser_conversion_declarator_opt
2312 (cp_parser *);
2313 static void cp_parser_ctor_initializer_opt
2314 (cp_parser *);
2315 static void cp_parser_mem_initializer_list
2316 (cp_parser *);
2317 static tree cp_parser_mem_initializer
2318 (cp_parser *);
2319 static tree cp_parser_mem_initializer_id
2320 (cp_parser *);
2322 /* Overloading [gram.over] */
2324 static cp_expr cp_parser_operator_function_id
2325 (cp_parser *);
2326 static cp_expr cp_parser_operator
2327 (cp_parser *);
2329 /* Templates [gram.temp] */
2331 static void cp_parser_template_declaration
2332 (cp_parser *, bool);
2333 static tree cp_parser_template_parameter_list
2334 (cp_parser *);
2335 static tree cp_parser_template_parameter
2336 (cp_parser *, bool *, bool *);
2337 static tree cp_parser_type_parameter
2338 (cp_parser *, bool *);
2339 static tree cp_parser_template_id
2340 (cp_parser *, bool, bool, enum tag_types, bool);
2341 static tree cp_parser_template_name
2342 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2343 static tree cp_parser_template_argument_list
2344 (cp_parser *);
2345 static tree cp_parser_template_argument
2346 (cp_parser *);
2347 static void cp_parser_explicit_instantiation
2348 (cp_parser *);
2349 static void cp_parser_explicit_specialization
2350 (cp_parser *);
2352 /* Exception handling [gram.exception] */
2354 static tree cp_parser_try_block
2355 (cp_parser *);
2356 static void cp_parser_function_try_block
2357 (cp_parser *);
2358 static void cp_parser_handler_seq
2359 (cp_parser *);
2360 static void cp_parser_handler
2361 (cp_parser *);
2362 static tree cp_parser_exception_declaration
2363 (cp_parser *);
2364 static tree cp_parser_throw_expression
2365 (cp_parser *);
2366 static tree cp_parser_exception_specification_opt
2367 (cp_parser *);
2368 static tree cp_parser_type_id_list
2369 (cp_parser *);
2371 /* GNU Extensions */
2373 static tree cp_parser_asm_specification_opt
2374 (cp_parser *);
2375 static tree cp_parser_asm_operand_list
2376 (cp_parser *);
2377 static tree cp_parser_asm_clobber_list
2378 (cp_parser *);
2379 static tree cp_parser_asm_label_list
2380 (cp_parser *);
2381 static bool cp_next_tokens_can_be_attribute_p
2382 (cp_parser *);
2383 static bool cp_next_tokens_can_be_gnu_attribute_p
2384 (cp_parser *);
2385 static bool cp_next_tokens_can_be_std_attribute_p
2386 (cp_parser *);
2387 static bool cp_nth_tokens_can_be_std_attribute_p
2388 (cp_parser *, size_t);
2389 static bool cp_nth_tokens_can_be_gnu_attribute_p
2390 (cp_parser *, size_t);
2391 static bool cp_nth_tokens_can_be_attribute_p
2392 (cp_parser *, size_t);
2393 static tree cp_parser_attributes_opt
2394 (cp_parser *);
2395 static tree cp_parser_gnu_attributes_opt
2396 (cp_parser *);
2397 static tree cp_parser_gnu_attribute_list
2398 (cp_parser *);
2399 static tree cp_parser_std_attribute
2400 (cp_parser *, tree);
2401 static tree cp_parser_std_attribute_spec
2402 (cp_parser *);
2403 static tree cp_parser_std_attribute_spec_seq
2404 (cp_parser *);
2405 static bool cp_parser_extension_opt
2406 (cp_parser *, int *);
2407 static void cp_parser_label_declaration
2408 (cp_parser *);
2410 /* Concept Extensions */
2412 static tree cp_parser_requires_clause
2413 (cp_parser *);
2414 static tree cp_parser_requires_clause_opt
2415 (cp_parser *);
2416 static tree cp_parser_requires_expression
2417 (cp_parser *);
2418 static tree cp_parser_requirement_parameter_list
2419 (cp_parser *);
2420 static tree cp_parser_requirement_body
2421 (cp_parser *);
2422 static tree cp_parser_requirement_list
2423 (cp_parser *);
2424 static tree cp_parser_requirement
2425 (cp_parser *);
2426 static tree cp_parser_simple_requirement
2427 (cp_parser *);
2428 static tree cp_parser_compound_requirement
2429 (cp_parser *);
2430 static tree cp_parser_type_requirement
2431 (cp_parser *);
2432 static tree cp_parser_nested_requirement
2433 (cp_parser *);
2435 /* Transactional Memory Extensions */
2437 static tree cp_parser_transaction
2438 (cp_parser *, cp_token *);
2439 static tree cp_parser_transaction_expression
2440 (cp_parser *, enum rid);
2441 static void cp_parser_function_transaction
2442 (cp_parser *, enum rid);
2443 static tree cp_parser_transaction_cancel
2444 (cp_parser *);
2446 enum pragma_context {
2447 pragma_external,
2448 pragma_member,
2449 pragma_objc_icode,
2450 pragma_stmt,
2451 pragma_compound
2453 static bool cp_parser_pragma
2454 (cp_parser *, enum pragma_context, bool *);
2456 /* Objective-C++ Productions */
2458 static tree cp_parser_objc_message_receiver
2459 (cp_parser *);
2460 static tree cp_parser_objc_message_args
2461 (cp_parser *);
2462 static tree cp_parser_objc_message_expression
2463 (cp_parser *);
2464 static cp_expr cp_parser_objc_encode_expression
2465 (cp_parser *);
2466 static tree cp_parser_objc_defs_expression
2467 (cp_parser *);
2468 static tree cp_parser_objc_protocol_expression
2469 (cp_parser *);
2470 static tree cp_parser_objc_selector_expression
2471 (cp_parser *);
2472 static cp_expr cp_parser_objc_expression
2473 (cp_parser *);
2474 static bool cp_parser_objc_selector_p
2475 (enum cpp_ttype);
2476 static tree cp_parser_objc_selector
2477 (cp_parser *);
2478 static tree cp_parser_objc_protocol_refs_opt
2479 (cp_parser *);
2480 static void cp_parser_objc_declaration
2481 (cp_parser *, tree);
2482 static tree cp_parser_objc_statement
2483 (cp_parser *);
2484 static bool cp_parser_objc_valid_prefix_attributes
2485 (cp_parser *, tree *);
2486 static void cp_parser_objc_at_property_declaration
2487 (cp_parser *) ;
2488 static void cp_parser_objc_at_synthesize_declaration
2489 (cp_parser *) ;
2490 static void cp_parser_objc_at_dynamic_declaration
2491 (cp_parser *) ;
2492 static tree cp_parser_objc_struct_declaration
2493 (cp_parser *) ;
2495 /* Utility Routines */
2497 static cp_expr cp_parser_lookup_name
2498 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2499 static tree cp_parser_lookup_name_simple
2500 (cp_parser *, tree, location_t);
2501 static tree cp_parser_maybe_treat_template_as_class
2502 (tree, bool);
2503 static bool cp_parser_check_declarator_template_parameters
2504 (cp_parser *, cp_declarator *, location_t);
2505 static bool cp_parser_check_template_parameters
2506 (cp_parser *, unsigned, location_t, cp_declarator *);
2507 static cp_expr cp_parser_simple_cast_expression
2508 (cp_parser *);
2509 static tree cp_parser_global_scope_opt
2510 (cp_parser *, bool);
2511 static bool cp_parser_constructor_declarator_p
2512 (cp_parser *, bool);
2513 static tree cp_parser_function_definition_from_specifiers_and_declarator
2514 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2515 static tree cp_parser_function_definition_after_declarator
2516 (cp_parser *, bool);
2517 static bool cp_parser_template_declaration_after_export
2518 (cp_parser *, bool);
2519 static void cp_parser_perform_template_parameter_access_checks
2520 (vec<deferred_access_check, va_gc> *);
2521 static tree cp_parser_single_declaration
2522 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2523 static cp_expr cp_parser_functional_cast
2524 (cp_parser *, tree);
2525 static tree cp_parser_save_member_function_body
2526 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2527 static tree cp_parser_save_nsdmi
2528 (cp_parser *);
2529 static tree cp_parser_enclosed_template_argument_list
2530 (cp_parser *);
2531 static void cp_parser_save_default_args
2532 (cp_parser *, tree);
2533 static void cp_parser_late_parsing_for_member
2534 (cp_parser *, tree);
2535 static tree cp_parser_late_parse_one_default_arg
2536 (cp_parser *, tree, tree, tree);
2537 static void cp_parser_late_parsing_nsdmi
2538 (cp_parser *, tree);
2539 static void cp_parser_late_parsing_default_args
2540 (cp_parser *, tree);
2541 static tree cp_parser_sizeof_operand
2542 (cp_parser *, enum rid);
2543 static tree cp_parser_trait_expr
2544 (cp_parser *, enum rid);
2545 static bool cp_parser_declares_only_class_p
2546 (cp_parser *);
2547 static void cp_parser_set_storage_class
2548 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2549 static void cp_parser_set_decl_spec_type
2550 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2551 static void set_and_check_decl_spec_loc
2552 (cp_decl_specifier_seq *decl_specs,
2553 cp_decl_spec ds, cp_token *);
2554 static bool cp_parser_friend_p
2555 (const cp_decl_specifier_seq *);
2556 static void cp_parser_required_error
2557 (cp_parser *, required_token, bool, location_t);
2558 static cp_token *cp_parser_require
2559 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2560 static cp_token *cp_parser_require_keyword
2561 (cp_parser *, enum rid, required_token);
2562 static bool cp_parser_token_starts_function_definition_p
2563 (cp_token *);
2564 static bool cp_parser_next_token_starts_class_definition_p
2565 (cp_parser *);
2566 static bool cp_parser_next_token_ends_template_argument_p
2567 (cp_parser *);
2568 static bool cp_parser_nth_token_starts_template_argument_list_p
2569 (cp_parser *, size_t);
2570 static enum tag_types cp_parser_token_is_class_key
2571 (cp_token *);
2572 static enum tag_types cp_parser_token_is_type_parameter_key
2573 (cp_token *);
2574 static void cp_parser_check_class_key
2575 (enum tag_types, tree type);
2576 static void cp_parser_check_access_in_redeclaration
2577 (tree type, location_t location);
2578 static bool cp_parser_optional_template_keyword
2579 (cp_parser *);
2580 static void cp_parser_pre_parsed_nested_name_specifier
2581 (cp_parser *);
2582 static bool cp_parser_cache_group
2583 (cp_parser *, enum cpp_ttype, unsigned);
2584 static tree cp_parser_cache_defarg
2585 (cp_parser *parser, bool nsdmi);
2586 static void cp_parser_parse_tentatively
2587 (cp_parser *);
2588 static void cp_parser_commit_to_tentative_parse
2589 (cp_parser *);
2590 static void cp_parser_commit_to_topmost_tentative_parse
2591 (cp_parser *);
2592 static void cp_parser_abort_tentative_parse
2593 (cp_parser *);
2594 static bool cp_parser_parse_definitely
2595 (cp_parser *);
2596 static inline bool cp_parser_parsing_tentatively
2597 (cp_parser *);
2598 static bool cp_parser_uncommitted_to_tentative_parse_p
2599 (cp_parser *);
2600 static void cp_parser_error
2601 (cp_parser *, const char *);
2602 static void cp_parser_name_lookup_error
2603 (cp_parser *, tree, tree, name_lookup_error, location_t);
2604 static bool cp_parser_simulate_error
2605 (cp_parser *);
2606 static bool cp_parser_check_type_definition
2607 (cp_parser *);
2608 static void cp_parser_check_for_definition_in_return_type
2609 (cp_declarator *, tree, location_t type_location);
2610 static void cp_parser_check_for_invalid_template_id
2611 (cp_parser *, tree, enum tag_types, location_t location);
2612 static bool cp_parser_non_integral_constant_expression
2613 (cp_parser *, non_integral_constant);
2614 static void cp_parser_diagnose_invalid_type_name
2615 (cp_parser *, tree, location_t);
2616 static bool cp_parser_parse_and_diagnose_invalid_type_name
2617 (cp_parser *);
2618 static int cp_parser_skip_to_closing_parenthesis
2619 (cp_parser *, bool, bool, bool);
2620 static void cp_parser_skip_to_end_of_statement
2621 (cp_parser *);
2622 static void cp_parser_consume_semicolon_at_end_of_statement
2623 (cp_parser *);
2624 static void cp_parser_skip_to_end_of_block_or_statement
2625 (cp_parser *);
2626 static bool cp_parser_skip_to_closing_brace
2627 (cp_parser *);
2628 static void cp_parser_skip_to_end_of_template_parameter_list
2629 (cp_parser *);
2630 static void cp_parser_skip_to_pragma_eol
2631 (cp_parser*, cp_token *);
2632 static bool cp_parser_error_occurred
2633 (cp_parser *);
2634 static bool cp_parser_allow_gnu_extensions_p
2635 (cp_parser *);
2636 static bool cp_parser_is_pure_string_literal
2637 (cp_token *);
2638 static bool cp_parser_is_string_literal
2639 (cp_token *);
2640 static bool cp_parser_is_keyword
2641 (cp_token *, enum rid);
2642 static tree cp_parser_make_typename_type
2643 (cp_parser *, tree, location_t location);
2644 static cp_declarator * cp_parser_make_indirect_declarator
2645 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2646 static bool cp_parser_compound_literal_p
2647 (cp_parser *);
2648 static bool cp_parser_array_designator_p
2649 (cp_parser *);
2650 static bool cp_parser_init_statement_p
2651 (cp_parser *);
2652 static bool cp_parser_skip_to_closing_square_bracket
2653 (cp_parser *);
2655 /* Concept-related syntactic transformations */
2657 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2658 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2660 // -------------------------------------------------------------------------- //
2661 // Unevaluated Operand Guard
2663 // Implementation of an RAII helper for unevaluated operand parsing.
2664 cp_unevaluated::cp_unevaluated ()
2666 ++cp_unevaluated_operand;
2667 ++c_inhibit_evaluation_warnings;
2670 cp_unevaluated::~cp_unevaluated ()
2672 --c_inhibit_evaluation_warnings;
2673 --cp_unevaluated_operand;
2676 // -------------------------------------------------------------------------- //
2677 // Tentative Parsing
2679 /* Returns nonzero if we are parsing tentatively. */
2681 static inline bool
2682 cp_parser_parsing_tentatively (cp_parser* parser)
2684 return parser->context->next != NULL;
2687 /* Returns nonzero if TOKEN is a string literal. */
2689 static bool
2690 cp_parser_is_pure_string_literal (cp_token* token)
2692 return (token->type == CPP_STRING ||
2693 token->type == CPP_STRING16 ||
2694 token->type == CPP_STRING32 ||
2695 token->type == CPP_WSTRING ||
2696 token->type == CPP_UTF8STRING);
2699 /* Returns nonzero if TOKEN is a string literal
2700 of a user-defined string literal. */
2702 static bool
2703 cp_parser_is_string_literal (cp_token* token)
2705 return (cp_parser_is_pure_string_literal (token) ||
2706 token->type == CPP_STRING_USERDEF ||
2707 token->type == CPP_STRING16_USERDEF ||
2708 token->type == CPP_STRING32_USERDEF ||
2709 token->type == CPP_WSTRING_USERDEF ||
2710 token->type == CPP_UTF8STRING_USERDEF);
2713 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2715 static bool
2716 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2718 return token->keyword == keyword;
2721 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2722 PRAGMA_NONE. */
2724 static enum pragma_kind
2725 cp_parser_pragma_kind (cp_token *token)
2727 if (token->type != CPP_PRAGMA)
2728 return PRAGMA_NONE;
2729 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2730 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2733 /* Helper function for cp_parser_error.
2734 Having peeked a token of kind TOK1_KIND that might signify
2735 a conflict marker, peek successor tokens to determine
2736 if we actually do have a conflict marker.
2737 Specifically, we consider a run of 7 '<', '=' or '>' characters
2738 at the start of a line as a conflict marker.
2739 These come through the lexer as three pairs and a single,
2740 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2741 If it returns true, *OUT_LOC is written to with the location/range
2742 of the marker. */
2744 static bool
2745 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2746 location_t *out_loc)
2748 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2749 if (token2->type != tok1_kind)
2750 return false;
2751 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2752 if (token3->type != tok1_kind)
2753 return false;
2754 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2755 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2756 return false;
2758 /* It must be at the start of the line. */
2759 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2760 if (LOCATION_COLUMN (start_loc) != 1)
2761 return false;
2763 /* We have a conflict marker. Construct a location of the form:
2764 <<<<<<<
2765 ^~~~~~~
2766 with start == caret, finishing at the end of the marker. */
2767 location_t finish_loc = get_finish (token4->location);
2768 *out_loc = make_location (start_loc, start_loc, finish_loc);
2770 return true;
2773 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2774 RT_CLOSE_PAREN. */
2776 static const char *
2777 get_matching_symbol (required_token token_desc)
2779 switch (token_desc)
2781 default:
2782 gcc_unreachable ();
2783 return "";
2784 case RT_CLOSE_BRACE:
2785 return "{";
2786 case RT_CLOSE_PAREN:
2787 return "(";
2791 /* Attempt to convert TOKEN_DESC from a required_token to an
2792 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2794 static enum cpp_ttype
2795 get_required_cpp_ttype (required_token token_desc)
2797 switch (token_desc)
2799 case RT_SEMICOLON:
2800 return CPP_SEMICOLON;
2801 case RT_OPEN_PAREN:
2802 return CPP_OPEN_PAREN;
2803 case RT_CLOSE_BRACE:
2804 return CPP_CLOSE_BRACE;
2805 case RT_OPEN_BRACE:
2806 return CPP_OPEN_BRACE;
2807 case RT_CLOSE_SQUARE:
2808 return CPP_CLOSE_SQUARE;
2809 case RT_OPEN_SQUARE:
2810 return CPP_OPEN_SQUARE;
2811 case RT_COMMA:
2812 return CPP_COMMA;
2813 case RT_COLON:
2814 return CPP_COLON;
2815 case RT_CLOSE_PAREN:
2816 return CPP_CLOSE_PAREN;
2818 default:
2819 /* Use CPP_EOF as a "no completions possible" code. */
2820 return CPP_EOF;
2825 /* Subroutine of cp_parser_error and cp_parser_required_error.
2827 Issue a diagnostic of the form
2828 FILE:LINE: MESSAGE before TOKEN
2829 where TOKEN is the next token in the input stream. MESSAGE
2830 (specified by the caller) is usually of the form "expected
2831 OTHER-TOKEN".
2833 This bypasses the check for tentative passing, and potentially
2834 adds material needed by cp_parser_required_error.
2836 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2837 suggesting insertion of the missing token.
2839 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2840 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2841 location. */
2843 static void
2844 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2845 required_token missing_token_desc,
2846 location_t matching_location)
2848 cp_token *token = cp_lexer_peek_token (parser->lexer);
2849 /* This diagnostic makes more sense if it is tagged to the line
2850 of the token we just peeked at. */
2851 cp_lexer_set_source_position_from_token (token);
2853 if (token->type == CPP_PRAGMA)
2855 error_at (token->location,
2856 "%<#pragma%> is not allowed here");
2857 cp_parser_skip_to_pragma_eol (parser, token);
2858 return;
2861 /* If this is actually a conflict marker, report it as such. */
2862 if (token->type == CPP_LSHIFT
2863 || token->type == CPP_RSHIFT
2864 || token->type == CPP_EQ_EQ)
2866 location_t loc;
2867 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2869 error_at (loc, "version control conflict marker in file");
2870 return;
2874 gcc_rich_location richloc (input_location);
2876 bool added_matching_location = false;
2878 if (missing_token_desc != RT_NONE)
2880 /* Potentially supply a fix-it hint, suggesting to add the
2881 missing token immediately after the *previous* token.
2882 This may move the primary location within richloc. */
2883 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2884 location_t prev_token_loc
2885 = cp_lexer_previous_token (parser->lexer)->location;
2886 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2888 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2889 Attempt to consolidate diagnostics by printing it as a
2890 secondary range within the main diagnostic. */
2891 if (matching_location != UNKNOWN_LOCATION)
2892 added_matching_location
2893 = richloc.add_location_if_nearby (matching_location);
2896 /* Actually emit the error. */
2897 c_parse_error (gmsgid,
2898 /* Because c_parser_error does not understand
2899 CPP_KEYWORD, keywords are treated like
2900 identifiers. */
2901 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2902 token->u.value, token->flags, &richloc);
2904 if (missing_token_desc != RT_NONE)
2906 /* If we weren't able to consolidate matching_location, then
2907 print it as a secondary diagnostic. */
2908 if (matching_location != UNKNOWN_LOCATION
2909 && !added_matching_location)
2910 inform (matching_location, "to match this %qs",
2911 get_matching_symbol (missing_token_desc));
2915 /* If not parsing tentatively, issue a diagnostic of the form
2916 FILE:LINE: MESSAGE before TOKEN
2917 where TOKEN is the next token in the input stream. MESSAGE
2918 (specified by the caller) is usually of the form "expected
2919 OTHER-TOKEN". */
2921 static void
2922 cp_parser_error (cp_parser* parser, const char* gmsgid)
2924 if (!cp_parser_simulate_error (parser))
2925 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2928 /* Issue an error about name-lookup failing. NAME is the
2929 IDENTIFIER_NODE DECL is the result of
2930 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2931 the thing that we hoped to find. */
2933 static void
2934 cp_parser_name_lookup_error (cp_parser* parser,
2935 tree name,
2936 tree decl,
2937 name_lookup_error desired,
2938 location_t location)
2940 /* If name lookup completely failed, tell the user that NAME was not
2941 declared. */
2942 if (decl == error_mark_node)
2944 if (parser->scope && parser->scope != global_namespace)
2945 error_at (location, "%<%E::%E%> has not been declared",
2946 parser->scope, name);
2947 else if (parser->scope == global_namespace)
2948 error_at (location, "%<::%E%> has not been declared", name);
2949 else if (parser->object_scope
2950 && !CLASS_TYPE_P (parser->object_scope))
2951 error_at (location, "request for member %qE in non-class type %qT",
2952 name, parser->object_scope);
2953 else if (parser->object_scope)
2954 error_at (location, "%<%T::%E%> has not been declared",
2955 parser->object_scope, name);
2956 else
2957 error_at (location, "%qE has not been declared", name);
2959 else if (parser->scope && parser->scope != global_namespace)
2961 switch (desired)
2963 case NLE_TYPE:
2964 error_at (location, "%<%E::%E%> is not a type",
2965 parser->scope, name);
2966 break;
2967 case NLE_CXX98:
2968 error_at (location, "%<%E::%E%> is not a class or namespace",
2969 parser->scope, name);
2970 break;
2971 case NLE_NOT_CXX98:
2972 error_at (location,
2973 "%<%E::%E%> is not a class, namespace, or enumeration",
2974 parser->scope, name);
2975 break;
2976 default:
2977 gcc_unreachable ();
2981 else if (parser->scope == global_namespace)
2983 switch (desired)
2985 case NLE_TYPE:
2986 error_at (location, "%<::%E%> is not a type", name);
2987 break;
2988 case NLE_CXX98:
2989 error_at (location, "%<::%E%> is not a class or namespace", name);
2990 break;
2991 case NLE_NOT_CXX98:
2992 error_at (location,
2993 "%<::%E%> is not a class, namespace, or enumeration",
2994 name);
2995 break;
2996 default:
2997 gcc_unreachable ();
3000 else
3002 switch (desired)
3004 case NLE_TYPE:
3005 error_at (location, "%qE is not a type", name);
3006 break;
3007 case NLE_CXX98:
3008 error_at (location, "%qE is not a class or namespace", name);
3009 break;
3010 case NLE_NOT_CXX98:
3011 error_at (location,
3012 "%qE is not a class, namespace, or enumeration", name);
3013 break;
3014 default:
3015 gcc_unreachable ();
3020 /* If we are parsing tentatively, remember that an error has occurred
3021 during this tentative parse. Returns true if the error was
3022 simulated; false if a message should be issued by the caller. */
3024 static bool
3025 cp_parser_simulate_error (cp_parser* parser)
3027 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3029 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3030 return true;
3032 return false;
3035 /* This function is called when a type is defined. If type
3036 definitions are forbidden at this point, an error message is
3037 issued. */
3039 static bool
3040 cp_parser_check_type_definition (cp_parser* parser)
3042 /* If types are forbidden here, issue a message. */
3043 if (parser->type_definition_forbidden_message)
3045 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3046 in the message need to be interpreted. */
3047 error (parser->type_definition_forbidden_message);
3048 return false;
3050 return true;
3053 /* This function is called when the DECLARATOR is processed. The TYPE
3054 was a type defined in the decl-specifiers. If it is invalid to
3055 define a type in the decl-specifiers for DECLARATOR, an error is
3056 issued. TYPE_LOCATION is the location of TYPE and is used
3057 for error reporting. */
3059 static void
3060 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3061 tree type, location_t type_location)
3063 /* [dcl.fct] forbids type definitions in return types.
3064 Unfortunately, it's not easy to know whether or not we are
3065 processing a return type until after the fact. */
3066 while (declarator
3067 && (declarator->kind == cdk_pointer
3068 || declarator->kind == cdk_reference
3069 || declarator->kind == cdk_ptrmem))
3070 declarator = declarator->declarator;
3071 if (declarator
3072 && declarator->kind == cdk_function)
3074 error_at (type_location,
3075 "new types may not be defined in a return type");
3076 inform (type_location,
3077 "(perhaps a semicolon is missing after the definition of %qT)",
3078 type);
3082 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3083 "<" in any valid C++ program. If the next token is indeed "<",
3084 issue a message warning the user about what appears to be an
3085 invalid attempt to form a template-id. LOCATION is the location
3086 of the type-specifier (TYPE) */
3088 static void
3089 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3090 tree type,
3091 enum tag_types tag_type,
3092 location_t location)
3094 cp_token_position start = 0;
3096 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3098 if (TREE_CODE (type) == TYPE_DECL)
3099 type = TREE_TYPE (type);
3100 if (TYPE_P (type) && !template_placeholder_p (type))
3101 error_at (location, "%qT is not a template", type);
3102 else if (identifier_p (type))
3104 if (tag_type != none_type)
3105 error_at (location, "%qE is not a class template", type);
3106 else
3107 error_at (location, "%qE is not a template", type);
3109 else
3110 error_at (location, "invalid template-id");
3111 /* Remember the location of the invalid "<". */
3112 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3113 start = cp_lexer_token_position (parser->lexer, true);
3114 /* Consume the "<". */
3115 cp_lexer_consume_token (parser->lexer);
3116 /* Parse the template arguments. */
3117 cp_parser_enclosed_template_argument_list (parser);
3118 /* Permanently remove the invalid template arguments so that
3119 this error message is not issued again. */
3120 if (start)
3121 cp_lexer_purge_tokens_after (parser->lexer, start);
3125 /* If parsing an integral constant-expression, issue an error message
3126 about the fact that THING appeared and return true. Otherwise,
3127 return false. In either case, set
3128 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3130 static bool
3131 cp_parser_non_integral_constant_expression (cp_parser *parser,
3132 non_integral_constant thing)
3134 parser->non_integral_constant_expression_p = true;
3135 if (parser->integral_constant_expression_p)
3137 if (!parser->allow_non_integral_constant_expression_p)
3139 const char *msg = NULL;
3140 switch (thing)
3142 case NIC_FLOAT:
3143 pedwarn (input_location, OPT_Wpedantic,
3144 "ISO C++ forbids using a floating-point literal "
3145 "in a constant-expression");
3146 return true;
3147 case NIC_CAST:
3148 error ("a cast to a type other than an integral or "
3149 "enumeration type cannot appear in a "
3150 "constant-expression");
3151 return true;
3152 case NIC_TYPEID:
3153 error ("%<typeid%> operator "
3154 "cannot appear in a constant-expression");
3155 return true;
3156 case NIC_NCC:
3157 error ("non-constant compound literals "
3158 "cannot appear in a constant-expression");
3159 return true;
3160 case NIC_FUNC_CALL:
3161 error ("a function call "
3162 "cannot appear in a constant-expression");
3163 return true;
3164 case NIC_INC:
3165 error ("an increment "
3166 "cannot appear in a constant-expression");
3167 return true;
3168 case NIC_DEC:
3169 error ("an decrement "
3170 "cannot appear in a constant-expression");
3171 return true;
3172 case NIC_ARRAY_REF:
3173 error ("an array reference "
3174 "cannot appear in a constant-expression");
3175 return true;
3176 case NIC_ADDR_LABEL:
3177 error ("the address of a label "
3178 "cannot appear in a constant-expression");
3179 return true;
3180 case NIC_OVERLOADED:
3181 error ("calls to overloaded operators "
3182 "cannot appear in a constant-expression");
3183 return true;
3184 case NIC_ASSIGNMENT:
3185 error ("an assignment cannot appear in a constant-expression");
3186 return true;
3187 case NIC_COMMA:
3188 error ("a comma operator "
3189 "cannot appear in a constant-expression");
3190 return true;
3191 case NIC_CONSTRUCTOR:
3192 error ("a call to a constructor "
3193 "cannot appear in a constant-expression");
3194 return true;
3195 case NIC_TRANSACTION:
3196 error ("a transaction expression "
3197 "cannot appear in a constant-expression");
3198 return true;
3199 case NIC_THIS:
3200 msg = "this";
3201 break;
3202 case NIC_FUNC_NAME:
3203 msg = "__FUNCTION__";
3204 break;
3205 case NIC_PRETTY_FUNC:
3206 msg = "__PRETTY_FUNCTION__";
3207 break;
3208 case NIC_C99_FUNC:
3209 msg = "__func__";
3210 break;
3211 case NIC_VA_ARG:
3212 msg = "va_arg";
3213 break;
3214 case NIC_ARROW:
3215 msg = "->";
3216 break;
3217 case NIC_POINT:
3218 msg = ".";
3219 break;
3220 case NIC_STAR:
3221 msg = "*";
3222 break;
3223 case NIC_ADDR:
3224 msg = "&";
3225 break;
3226 case NIC_PREINCREMENT:
3227 msg = "++";
3228 break;
3229 case NIC_PREDECREMENT:
3230 msg = "--";
3231 break;
3232 case NIC_NEW:
3233 msg = "new";
3234 break;
3235 case NIC_DEL:
3236 msg = "delete";
3237 break;
3238 default:
3239 gcc_unreachable ();
3241 if (msg)
3242 error ("%qs cannot appear in a constant-expression", msg);
3243 return true;
3246 return false;
3249 /* Emit a diagnostic for an invalid type name. This function commits
3250 to the current active tentative parse, if any. (Otherwise, the
3251 problematic construct might be encountered again later, resulting
3252 in duplicate error messages.) LOCATION is the location of ID. */
3254 static void
3255 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3256 location_t location)
3258 tree decl, ambiguous_decls;
3259 cp_parser_commit_to_tentative_parse (parser);
3260 /* Try to lookup the identifier. */
3261 decl = cp_parser_lookup_name (parser, id, none_type,
3262 /*is_template=*/false,
3263 /*is_namespace=*/false,
3264 /*check_dependency=*/true,
3265 &ambiguous_decls, location);
3266 if (ambiguous_decls)
3267 /* If the lookup was ambiguous, an error will already have
3268 been issued. */
3269 return;
3270 /* If the lookup found a template-name, it means that the user forgot
3271 to specify an argument list. Emit a useful error message. */
3272 if (DECL_TYPE_TEMPLATE_P (decl))
3274 error_at (location,
3275 "invalid use of template-name %qE without an argument list",
3276 decl);
3277 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3278 inform (location, "class template argument deduction is only available "
3279 "with -std=c++17 or -std=gnu++17");
3280 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3282 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3283 error_at (location, "invalid use of destructor %qD as a type", id);
3284 else if (TREE_CODE (decl) == TYPE_DECL)
3285 /* Something like 'unsigned A a;' */
3286 error_at (location, "invalid combination of multiple type-specifiers");
3287 else if (!parser->scope)
3289 /* Issue an error message. */
3290 const char *suggestion = NULL;
3291 if (TREE_CODE (id) == IDENTIFIER_NODE)
3292 suggestion = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME);
3293 if (suggestion)
3295 gcc_rich_location richloc (location);
3296 richloc.add_fixit_replace (suggestion);
3297 error_at (&richloc,
3298 "%qE does not name a type; did you mean %qs?",
3299 id, suggestion);
3301 else
3302 error_at (location, "%qE does not name a type", id);
3303 /* If we're in a template class, it's possible that the user was
3304 referring to a type from a base class. For example:
3306 template <typename T> struct A { typedef T X; };
3307 template <typename T> struct B : public A<T> { X x; };
3309 The user should have said "typename A<T>::X". */
3310 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3311 inform (location, "C++11 %<constexpr%> only available with "
3312 "-std=c++11 or -std=gnu++11");
3313 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3314 inform (location, "C++11 %<noexcept%> only available with "
3315 "-std=c++11 or -std=gnu++11");
3316 else if (cxx_dialect < cxx11
3317 && TREE_CODE (id) == IDENTIFIER_NODE
3318 && id_equal (id, "thread_local"))
3319 inform (location, "C++11 %<thread_local%> only available with "
3320 "-std=c++11 or -std=gnu++11");
3321 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3322 inform (location, "%<concept%> only available with -fconcepts");
3323 else if (processing_template_decl && current_class_type
3324 && TYPE_BINFO (current_class_type))
3326 tree b;
3328 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3330 b = TREE_CHAIN (b))
3332 tree base_type = BINFO_TYPE (b);
3333 if (CLASS_TYPE_P (base_type)
3334 && dependent_type_p (base_type))
3336 tree field;
3337 /* Go from a particular instantiation of the
3338 template (which will have an empty TYPE_FIELDs),
3339 to the main version. */
3340 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3341 for (field = TYPE_FIELDS (base_type);
3342 field;
3343 field = DECL_CHAIN (field))
3344 if (TREE_CODE (field) == TYPE_DECL
3345 && DECL_NAME (field) == id)
3347 inform (location,
3348 "(perhaps %<typename %T::%E%> was intended)",
3349 BINFO_TYPE (b), id);
3350 break;
3352 if (field)
3353 break;
3358 /* Here we diagnose qualified-ids where the scope is actually correct,
3359 but the identifier does not resolve to a valid type name. */
3360 else if (parser->scope != error_mark_node)
3362 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3364 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3365 error_at (location_of (id),
3366 "%qE in namespace %qE does not name a template type",
3367 id, parser->scope);
3368 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3369 error_at (location_of (id),
3370 "%qE in namespace %qE does not name a template type",
3371 TREE_OPERAND (id, 0), parser->scope);
3372 else
3373 error_at (location_of (id),
3374 "%qE in namespace %qE does not name a type",
3375 id, parser->scope);
3376 if (DECL_P (decl))
3377 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3379 else if (CLASS_TYPE_P (parser->scope)
3380 && constructor_name_p (id, parser->scope))
3382 /* A<T>::A<T>() */
3383 error_at (location, "%<%T::%E%> names the constructor, not"
3384 " the type", parser->scope, id);
3385 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3386 error_at (location, "and %qT has no template constructors",
3387 parser->scope);
3389 else if (TYPE_P (parser->scope)
3390 && dependent_scope_p (parser->scope))
3392 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3393 error_at (location,
3394 "need %<typename%> before %<%T::%D::%E%> because "
3395 "%<%T::%D%> is a dependent scope",
3396 TYPE_CONTEXT (parser->scope),
3397 TYPENAME_TYPE_FULLNAME (parser->scope),
3399 TYPE_CONTEXT (parser->scope),
3400 TYPENAME_TYPE_FULLNAME (parser->scope));
3401 else
3402 error_at (location, "need %<typename%> before %<%T::%E%> because "
3403 "%qT is a dependent scope",
3404 parser->scope, id, parser->scope);
3406 else if (TYPE_P (parser->scope))
3408 if (!COMPLETE_TYPE_P (parser->scope))
3409 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3410 parser->scope);
3411 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3412 error_at (location_of (id),
3413 "%qE in %q#T does not name a template type",
3414 id, parser->scope);
3415 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3416 error_at (location_of (id),
3417 "%qE in %q#T does not name a template type",
3418 TREE_OPERAND (id, 0), parser->scope);
3419 else
3420 error_at (location_of (id),
3421 "%qE in %q#T does not name a type",
3422 id, parser->scope);
3423 if (DECL_P (decl))
3424 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3426 else
3427 gcc_unreachable ();
3431 /* Check for a common situation where a type-name should be present,
3432 but is not, and issue a sensible error message. Returns true if an
3433 invalid type-name was detected.
3435 The situation handled by this function are variable declarations of the
3436 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3437 Usually, `ID' should name a type, but if we got here it means that it
3438 does not. We try to emit the best possible error message depending on
3439 how exactly the id-expression looks like. */
3441 static bool
3442 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3444 tree id;
3445 cp_token *token = cp_lexer_peek_token (parser->lexer);
3447 /* Avoid duplicate error about ambiguous lookup. */
3448 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3450 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3451 if (next->type == CPP_NAME && next->error_reported)
3452 goto out;
3455 cp_parser_parse_tentatively (parser);
3456 id = cp_parser_id_expression (parser,
3457 /*template_keyword_p=*/false,
3458 /*check_dependency_p=*/true,
3459 /*template_p=*/NULL,
3460 /*declarator_p=*/true,
3461 /*optional_p=*/false);
3462 /* If the next token is a (, this is a function with no explicit return
3463 type, i.e. constructor, destructor or conversion op. */
3464 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3465 || TREE_CODE (id) == TYPE_DECL)
3467 cp_parser_abort_tentative_parse (parser);
3468 return false;
3470 if (!cp_parser_parse_definitely (parser))
3471 return false;
3473 /* Emit a diagnostic for the invalid type. */
3474 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3475 out:
3476 /* If we aren't in the middle of a declarator (i.e. in a
3477 parameter-declaration-clause), skip to the end of the declaration;
3478 there's no point in trying to process it. */
3479 if (!parser->in_declarator_p)
3480 cp_parser_skip_to_end_of_block_or_statement (parser);
3481 return true;
3484 /* Consume tokens up to, and including, the next non-nested closing `)'.
3485 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3486 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3487 found an unnested token of that type. */
3489 static int
3490 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3491 bool recovering,
3492 cpp_ttype or_ttype,
3493 bool consume_paren)
3495 unsigned paren_depth = 0;
3496 unsigned brace_depth = 0;
3497 unsigned square_depth = 0;
3499 if (recovering && or_ttype == CPP_EOF
3500 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3501 return 0;
3503 while (true)
3505 cp_token * token = cp_lexer_peek_token (parser->lexer);
3507 /* Have we found what we're looking for before the closing paren? */
3508 if (token->type == or_ttype && or_ttype != CPP_EOF
3509 && !brace_depth && !paren_depth && !square_depth)
3510 return -1;
3512 switch (token->type)
3514 case CPP_EOF:
3515 case CPP_PRAGMA_EOL:
3516 /* If we've run out of tokens, then there is no closing `)'. */
3517 return 0;
3519 /* This is good for lambda expression capture-lists. */
3520 case CPP_OPEN_SQUARE:
3521 ++square_depth;
3522 break;
3523 case CPP_CLOSE_SQUARE:
3524 if (!square_depth--)
3525 return 0;
3526 break;
3528 case CPP_SEMICOLON:
3529 /* This matches the processing in skip_to_end_of_statement. */
3530 if (!brace_depth)
3531 return 0;
3532 break;
3534 case CPP_OPEN_BRACE:
3535 ++brace_depth;
3536 break;
3537 case CPP_CLOSE_BRACE:
3538 if (!brace_depth--)
3539 return 0;
3540 break;
3542 case CPP_OPEN_PAREN:
3543 if (!brace_depth)
3544 ++paren_depth;
3545 break;
3547 case CPP_CLOSE_PAREN:
3548 if (!brace_depth && !paren_depth--)
3550 if (consume_paren)
3551 cp_lexer_consume_token (parser->lexer);
3552 return 1;
3554 break;
3556 default:
3557 break;
3560 /* Consume the token. */
3561 cp_lexer_consume_token (parser->lexer);
3565 /* Consume tokens up to, and including, the next non-nested closing `)'.
3566 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3567 are doing error recovery. Returns -1 if OR_COMMA is true and we
3568 found an unnested token of that type. */
3570 static int
3571 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3572 bool recovering,
3573 bool or_comma,
3574 bool consume_paren)
3576 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3577 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3578 ttype, consume_paren);
3581 /* Consume tokens until we reach the end of the current statement.
3582 Normally, that will be just before consuming a `;'. However, if a
3583 non-nested `}' comes first, then we stop before consuming that. */
3585 static void
3586 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3588 unsigned nesting_depth = 0;
3590 /* Unwind generic function template scope if necessary. */
3591 if (parser->fully_implicit_function_template_p)
3592 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3594 while (true)
3596 cp_token *token = cp_lexer_peek_token (parser->lexer);
3598 switch (token->type)
3600 case CPP_EOF:
3601 case CPP_PRAGMA_EOL:
3602 /* If we've run out of tokens, stop. */
3603 return;
3605 case CPP_SEMICOLON:
3606 /* If the next token is a `;', we have reached the end of the
3607 statement. */
3608 if (!nesting_depth)
3609 return;
3610 break;
3612 case CPP_CLOSE_BRACE:
3613 /* If this is a non-nested '}', stop before consuming it.
3614 That way, when confronted with something like:
3616 { 3 + }
3618 we stop before consuming the closing '}', even though we
3619 have not yet reached a `;'. */
3620 if (nesting_depth == 0)
3621 return;
3623 /* If it is the closing '}' for a block that we have
3624 scanned, stop -- but only after consuming the token.
3625 That way given:
3627 void f g () { ... }
3628 typedef int I;
3630 we will stop after the body of the erroneously declared
3631 function, but before consuming the following `typedef'
3632 declaration. */
3633 if (--nesting_depth == 0)
3635 cp_lexer_consume_token (parser->lexer);
3636 return;
3638 break;
3640 case CPP_OPEN_BRACE:
3641 ++nesting_depth;
3642 break;
3644 default:
3645 break;
3648 /* Consume the token. */
3649 cp_lexer_consume_token (parser->lexer);
3653 /* This function is called at the end of a statement or declaration.
3654 If the next token is a semicolon, it is consumed; otherwise, error
3655 recovery is attempted. */
3657 static void
3658 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3660 /* Look for the trailing `;'. */
3661 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3663 /* If there is additional (erroneous) input, skip to the end of
3664 the statement. */
3665 cp_parser_skip_to_end_of_statement (parser);
3666 /* If the next token is now a `;', consume it. */
3667 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3668 cp_lexer_consume_token (parser->lexer);
3672 /* Skip tokens until we have consumed an entire block, or until we
3673 have consumed a non-nested `;'. */
3675 static void
3676 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3678 int nesting_depth = 0;
3680 /* Unwind generic function template scope if necessary. */
3681 if (parser->fully_implicit_function_template_p)
3682 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3684 while (nesting_depth >= 0)
3686 cp_token *token = cp_lexer_peek_token (parser->lexer);
3688 switch (token->type)
3690 case CPP_EOF:
3691 case CPP_PRAGMA_EOL:
3692 /* If we've run out of tokens, stop. */
3693 return;
3695 case CPP_SEMICOLON:
3696 /* Stop if this is an unnested ';'. */
3697 if (!nesting_depth)
3698 nesting_depth = -1;
3699 break;
3701 case CPP_CLOSE_BRACE:
3702 /* Stop if this is an unnested '}', or closes the outermost
3703 nesting level. */
3704 nesting_depth--;
3705 if (nesting_depth < 0)
3706 return;
3707 if (!nesting_depth)
3708 nesting_depth = -1;
3709 break;
3711 case CPP_OPEN_BRACE:
3712 /* Nest. */
3713 nesting_depth++;
3714 break;
3716 default:
3717 break;
3720 /* Consume the token. */
3721 cp_lexer_consume_token (parser->lexer);
3725 /* Skip tokens until a non-nested closing curly brace is the next
3726 token, or there are no more tokens. Return true in the first case,
3727 false otherwise. */
3729 static bool
3730 cp_parser_skip_to_closing_brace (cp_parser *parser)
3732 unsigned nesting_depth = 0;
3734 while (true)
3736 cp_token *token = cp_lexer_peek_token (parser->lexer);
3738 switch (token->type)
3740 case CPP_EOF:
3741 case CPP_PRAGMA_EOL:
3742 /* If we've run out of tokens, stop. */
3743 return false;
3745 case CPP_CLOSE_BRACE:
3746 /* If the next token is a non-nested `}', then we have reached
3747 the end of the current block. */
3748 if (nesting_depth-- == 0)
3749 return true;
3750 break;
3752 case CPP_OPEN_BRACE:
3753 /* If it the next token is a `{', then we are entering a new
3754 block. Consume the entire block. */
3755 ++nesting_depth;
3756 break;
3758 default:
3759 break;
3762 /* Consume the token. */
3763 cp_lexer_consume_token (parser->lexer);
3767 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3768 parameter is the PRAGMA token, allowing us to purge the entire pragma
3769 sequence. */
3771 static void
3772 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3774 cp_token *token;
3776 parser->lexer->in_pragma = false;
3779 token = cp_lexer_consume_token (parser->lexer);
3780 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3782 /* Ensure that the pragma is not parsed again. */
3783 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3786 /* Require pragma end of line, resyncing with it as necessary. The
3787 arguments are as for cp_parser_skip_to_pragma_eol. */
3789 static void
3790 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3792 parser->lexer->in_pragma = false;
3793 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3794 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3797 /* This is a simple wrapper around make_typename_type. When the id is
3798 an unresolved identifier node, we can provide a superior diagnostic
3799 using cp_parser_diagnose_invalid_type_name. */
3801 static tree
3802 cp_parser_make_typename_type (cp_parser *parser, tree id,
3803 location_t id_location)
3805 tree result;
3806 if (identifier_p (id))
3808 result = make_typename_type (parser->scope, id, typename_type,
3809 /*complain=*/tf_none);
3810 if (result == error_mark_node)
3811 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3812 return result;
3814 return make_typename_type (parser->scope, id, typename_type, tf_error);
3817 /* This is a wrapper around the
3818 make_{pointer,ptrmem,reference}_declarator functions that decides
3819 which one to call based on the CODE and CLASS_TYPE arguments. The
3820 CODE argument should be one of the values returned by
3821 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3822 appertain to the pointer or reference. */
3824 static cp_declarator *
3825 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3826 cp_cv_quals cv_qualifiers,
3827 cp_declarator *target,
3828 tree attributes)
3830 if (code == ERROR_MARK)
3831 return cp_error_declarator;
3833 if (code == INDIRECT_REF)
3834 if (class_type == NULL_TREE)
3835 return make_pointer_declarator (cv_qualifiers, target, attributes);
3836 else
3837 return make_ptrmem_declarator (cv_qualifiers, class_type,
3838 target, attributes);
3839 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3840 return make_reference_declarator (cv_qualifiers, target,
3841 false, attributes);
3842 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3843 return make_reference_declarator (cv_qualifiers, target,
3844 true, attributes);
3845 gcc_unreachable ();
3848 /* Create a new C++ parser. */
3850 static cp_parser *
3851 cp_parser_new (void)
3853 cp_parser *parser;
3854 cp_lexer *lexer;
3855 unsigned i;
3857 /* cp_lexer_new_main is called before doing GC allocation because
3858 cp_lexer_new_main might load a PCH file. */
3859 lexer = cp_lexer_new_main ();
3861 /* Initialize the binops_by_token so that we can get the tree
3862 directly from the token. */
3863 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3864 binops_by_token[binops[i].token_type] = binops[i];
3866 parser = ggc_cleared_alloc<cp_parser> ();
3867 parser->lexer = lexer;
3868 parser->context = cp_parser_context_new (NULL);
3870 /* For now, we always accept GNU extensions. */
3871 parser->allow_gnu_extensions_p = 1;
3873 /* The `>' token is a greater-than operator, not the end of a
3874 template-id. */
3875 parser->greater_than_is_operator_p = true;
3877 parser->default_arg_ok_p = true;
3879 /* We are not parsing a constant-expression. */
3880 parser->integral_constant_expression_p = false;
3881 parser->allow_non_integral_constant_expression_p = false;
3882 parser->non_integral_constant_expression_p = false;
3884 /* Local variable names are not forbidden. */
3885 parser->local_variables_forbidden_p = false;
3887 /* We are not processing an `extern "C"' declaration. */
3888 parser->in_unbraced_linkage_specification_p = false;
3890 /* We are not processing a declarator. */
3891 parser->in_declarator_p = false;
3893 /* We are not processing a template-argument-list. */
3894 parser->in_template_argument_list_p = false;
3896 /* We are not in an iteration statement. */
3897 parser->in_statement = 0;
3899 /* We are not in a switch statement. */
3900 parser->in_switch_statement_p = false;
3902 /* We are not parsing a type-id inside an expression. */
3903 parser->in_type_id_in_expr_p = false;
3905 /* Declarations aren't implicitly extern "C". */
3906 parser->implicit_extern_c = false;
3908 /* String literals should be translated to the execution character set. */
3909 parser->translate_strings_p = true;
3911 /* We are not parsing a function body. */
3912 parser->in_function_body = false;
3914 /* We can correct until told otherwise. */
3915 parser->colon_corrects_to_scope_p = true;
3917 /* The unparsed function queue is empty. */
3918 push_unparsed_function_queues (parser);
3920 /* There are no classes being defined. */
3921 parser->num_classes_being_defined = 0;
3923 /* No template parameters apply. */
3924 parser->num_template_parameter_lists = 0;
3926 /* Special parsing data structures. */
3927 parser->omp_declare_simd = NULL;
3928 parser->cilk_simd_fn_info = NULL;
3929 parser->oacc_routine = NULL;
3931 /* Not declaring an implicit function template. */
3932 parser->auto_is_implicit_function_template_parm_p = false;
3933 parser->fully_implicit_function_template_p = false;
3934 parser->implicit_template_parms = 0;
3935 parser->implicit_template_scope = 0;
3937 /* Allow constrained-type-specifiers. */
3938 parser->prevent_constrained_type_specifiers = 0;
3940 /* We haven't yet seen an 'extern "C"'. */
3941 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3943 return parser;
3946 /* Create a cp_lexer structure which will emit the tokens in CACHE
3947 and push it onto the parser's lexer stack. This is used for delayed
3948 parsing of in-class method bodies and default arguments, and should
3949 not be confused with tentative parsing. */
3950 static void
3951 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3953 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3954 lexer->next = parser->lexer;
3955 parser->lexer = lexer;
3957 /* Move the current source position to that of the first token in the
3958 new lexer. */
3959 cp_lexer_set_source_position_from_token (lexer->next_token);
3962 /* Pop the top lexer off the parser stack. This is never used for the
3963 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3964 static void
3965 cp_parser_pop_lexer (cp_parser *parser)
3967 cp_lexer *lexer = parser->lexer;
3968 parser->lexer = lexer->next;
3969 cp_lexer_destroy (lexer);
3971 /* Put the current source position back where it was before this
3972 lexer was pushed. */
3973 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3976 /* Lexical conventions [gram.lex] */
3978 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3979 identifier. */
3981 static cp_expr
3982 cp_parser_identifier (cp_parser* parser)
3984 cp_token *token;
3986 /* Look for the identifier. */
3987 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3988 /* Return the value. */
3989 if (token)
3990 return cp_expr (token->u.value, token->location);
3991 else
3992 return error_mark_node;
3995 /* Parse a sequence of adjacent string constants. Returns a
3996 TREE_STRING representing the combined, nul-terminated string
3997 constant. If TRANSLATE is true, translate the string to the
3998 execution character set. If WIDE_OK is true, a wide string is
3999 invalid here.
4001 C++98 [lex.string] says that if a narrow string literal token is
4002 adjacent to a wide string literal token, the behavior is undefined.
4003 However, C99 6.4.5p4 says that this results in a wide string literal.
4004 We follow C99 here, for consistency with the C front end.
4006 This code is largely lifted from lex_string() in c-lex.c.
4008 FUTURE: ObjC++ will need to handle @-strings here. */
4009 static cp_expr
4010 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4011 bool lookup_udlit = true)
4013 tree value;
4014 size_t count;
4015 struct obstack str_ob;
4016 cpp_string str, istr, *strs;
4017 cp_token *tok;
4018 enum cpp_ttype type, curr_type;
4019 int have_suffix_p = 0;
4020 tree string_tree;
4021 tree suffix_id = NULL_TREE;
4022 bool curr_tok_is_userdef_p = false;
4024 tok = cp_lexer_peek_token (parser->lexer);
4025 if (!cp_parser_is_string_literal (tok))
4027 cp_parser_error (parser, "expected string-literal");
4028 return error_mark_node;
4031 location_t loc = tok->location;
4033 if (cpp_userdef_string_p (tok->type))
4035 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4036 curr_type = cpp_userdef_string_remove_type (tok->type);
4037 curr_tok_is_userdef_p = true;
4039 else
4041 string_tree = tok->u.value;
4042 curr_type = tok->type;
4044 type = curr_type;
4046 /* Try to avoid the overhead of creating and destroying an obstack
4047 for the common case of just one string. */
4048 if (!cp_parser_is_string_literal
4049 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4051 cp_lexer_consume_token (parser->lexer);
4053 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4054 str.len = TREE_STRING_LENGTH (string_tree);
4055 count = 1;
4057 if (curr_tok_is_userdef_p)
4059 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4060 have_suffix_p = 1;
4061 curr_type = cpp_userdef_string_remove_type (tok->type);
4063 else
4064 curr_type = tok->type;
4066 strs = &str;
4068 else
4070 location_t last_tok_loc = tok->location;
4071 gcc_obstack_init (&str_ob);
4072 count = 0;
4076 cp_lexer_consume_token (parser->lexer);
4077 count++;
4078 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4079 str.len = TREE_STRING_LENGTH (string_tree);
4081 if (curr_tok_is_userdef_p)
4083 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4084 if (have_suffix_p == 0)
4086 suffix_id = curr_suffix_id;
4087 have_suffix_p = 1;
4089 else if (have_suffix_p == 1
4090 && curr_suffix_id != suffix_id)
4092 error ("inconsistent user-defined literal suffixes"
4093 " %qD and %qD in string literal",
4094 suffix_id, curr_suffix_id);
4095 have_suffix_p = -1;
4097 curr_type = cpp_userdef_string_remove_type (tok->type);
4099 else
4100 curr_type = tok->type;
4102 if (type != curr_type)
4104 if (type == CPP_STRING)
4105 type = curr_type;
4106 else if (curr_type != CPP_STRING)
4108 rich_location rich_loc (line_table, tok->location);
4109 rich_loc.add_range (last_tok_loc, false);
4110 error_at (&rich_loc,
4111 "unsupported non-standard concatenation "
4112 "of string literals");
4116 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4118 last_tok_loc = tok->location;
4120 tok = cp_lexer_peek_token (parser->lexer);
4121 if (cpp_userdef_string_p (tok->type))
4123 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4124 curr_type = cpp_userdef_string_remove_type (tok->type);
4125 curr_tok_is_userdef_p = true;
4127 else
4129 string_tree = tok->u.value;
4130 curr_type = tok->type;
4131 curr_tok_is_userdef_p = false;
4134 while (cp_parser_is_string_literal (tok));
4136 /* A string literal built by concatenation has its caret=start at
4137 the start of the initial string, and its finish at the finish of
4138 the final string literal. */
4139 loc = make_location (loc, loc, get_finish (last_tok_loc));
4141 strs = (cpp_string *) obstack_finish (&str_ob);
4144 if (type != CPP_STRING && !wide_ok)
4146 cp_parser_error (parser, "a wide string is invalid in this context");
4147 type = CPP_STRING;
4150 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4151 (parse_in, strs, count, &istr, type))
4153 value = build_string (istr.len, (const char *)istr.text);
4154 free (CONST_CAST (unsigned char *, istr.text));
4156 switch (type)
4158 default:
4159 case CPP_STRING:
4160 case CPP_UTF8STRING:
4161 TREE_TYPE (value) = char_array_type_node;
4162 break;
4163 case CPP_STRING16:
4164 TREE_TYPE (value) = char16_array_type_node;
4165 break;
4166 case CPP_STRING32:
4167 TREE_TYPE (value) = char32_array_type_node;
4168 break;
4169 case CPP_WSTRING:
4170 TREE_TYPE (value) = wchar_array_type_node;
4171 break;
4174 value = fix_string_type (value);
4176 if (have_suffix_p)
4178 tree literal = build_userdef_literal (suffix_id, value,
4179 OT_NONE, NULL_TREE);
4180 if (lookup_udlit)
4181 value = cp_parser_userdef_string_literal (literal);
4182 else
4183 value = literal;
4186 else
4187 /* cpp_interpret_string has issued an error. */
4188 value = error_mark_node;
4190 if (count > 1)
4191 obstack_free (&str_ob, 0);
4193 return cp_expr (value, loc);
4196 /* Look up a literal operator with the name and the exact arguments. */
4198 static tree
4199 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4201 tree decl;
4202 decl = lookup_name (name);
4203 if (!decl || !is_overloaded_fn (decl))
4204 return error_mark_node;
4206 for (lkp_iterator iter (decl); iter; ++iter)
4208 unsigned int ix;
4209 bool found = true;
4210 tree fn = *iter;
4211 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4212 if (parmtypes != NULL_TREE)
4214 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4215 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4217 tree tparm = TREE_VALUE (parmtypes);
4218 tree targ = TREE_TYPE ((*args)[ix]);
4219 bool ptr = TYPE_PTR_P (tparm);
4220 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4221 if ((ptr || arr || !same_type_p (tparm, targ))
4222 && (!ptr || !arr
4223 || !same_type_p (TREE_TYPE (tparm),
4224 TREE_TYPE (targ))))
4225 found = false;
4227 if (found
4228 && ix == vec_safe_length (args)
4229 /* May be this should be sufficient_parms_p instead,
4230 depending on how exactly should user-defined literals
4231 work in presence of default arguments on the literal
4232 operator parameters. */
4233 && parmtypes == void_list_node)
4234 return decl;
4238 return error_mark_node;
4241 /* Parse a user-defined char constant. Returns a call to a user-defined
4242 literal operator taking the character as an argument. */
4244 static cp_expr
4245 cp_parser_userdef_char_literal (cp_parser *parser)
4247 cp_token *token = cp_lexer_consume_token (parser->lexer);
4248 tree literal = token->u.value;
4249 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4250 tree value = USERDEF_LITERAL_VALUE (literal);
4251 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4252 tree decl, result;
4254 /* Build up a call to the user-defined operator */
4255 /* Lookup the name we got back from the id-expression. */
4256 vec<tree, va_gc> *args = make_tree_vector ();
4257 vec_safe_push (args, value);
4258 decl = lookup_literal_operator (name, args);
4259 if (!decl || decl == error_mark_node)
4261 error ("unable to find character literal operator %qD with %qT argument",
4262 name, TREE_TYPE (value));
4263 release_tree_vector (args);
4264 return error_mark_node;
4266 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4267 release_tree_vector (args);
4268 return result;
4271 /* A subroutine of cp_parser_userdef_numeric_literal to
4272 create a char... template parameter pack from a string node. */
4274 static tree
4275 make_char_string_pack (tree value)
4277 tree charvec;
4278 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4279 const char *str = TREE_STRING_POINTER (value);
4280 int i, len = TREE_STRING_LENGTH (value) - 1;
4281 tree argvec = make_tree_vec (1);
4283 /* Fill in CHARVEC with all of the parameters. */
4284 charvec = make_tree_vec (len);
4285 for (i = 0; i < len; ++i)
4286 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4288 /* Build the argument packs. */
4289 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4291 TREE_VEC_ELT (argvec, 0) = argpack;
4293 return argvec;
4296 /* A subroutine of cp_parser_userdef_numeric_literal to
4297 create a char... template parameter pack from a string node. */
4299 static tree
4300 make_string_pack (tree value)
4302 tree charvec;
4303 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4304 const unsigned char *str
4305 = (const unsigned char *) TREE_STRING_POINTER (value);
4306 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4307 int len = TREE_STRING_LENGTH (value) / sz - 1;
4308 tree argvec = make_tree_vec (2);
4310 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4311 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4313 /* First template parm is character type. */
4314 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4316 /* Fill in CHARVEC with all of the parameters. */
4317 charvec = make_tree_vec (len);
4318 for (int i = 0; i < len; ++i)
4319 TREE_VEC_ELT (charvec, i)
4320 = double_int_to_tree (str_char_type_node,
4321 double_int::from_buffer (str + i * sz, sz));
4323 /* Build the argument packs. */
4324 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4326 TREE_VEC_ELT (argvec, 1) = argpack;
4328 return argvec;
4331 /* Parse a user-defined numeric constant. returns a call to a user-defined
4332 literal operator. */
4334 static cp_expr
4335 cp_parser_userdef_numeric_literal (cp_parser *parser)
4337 cp_token *token = cp_lexer_consume_token (parser->lexer);
4338 tree literal = token->u.value;
4339 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4340 tree value = USERDEF_LITERAL_VALUE (literal);
4341 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4342 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4343 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4344 tree decl, result;
4345 vec<tree, va_gc> *args;
4347 /* Look for a literal operator taking the exact type of numeric argument
4348 as the literal value. */
4349 args = make_tree_vector ();
4350 vec_safe_push (args, value);
4351 decl = lookup_literal_operator (name, args);
4352 if (decl && decl != error_mark_node)
4354 result = finish_call_expr (decl, &args, false, true,
4355 tf_warning_or_error);
4357 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4359 warning_at (token->location, OPT_Woverflow,
4360 "integer literal exceeds range of %qT type",
4361 long_long_unsigned_type_node);
4363 else
4365 if (overflow > 0)
4366 warning_at (token->location, OPT_Woverflow,
4367 "floating literal exceeds range of %qT type",
4368 long_double_type_node);
4369 else if (overflow < 0)
4370 warning_at (token->location, OPT_Woverflow,
4371 "floating literal truncated to zero");
4374 release_tree_vector (args);
4375 return result;
4377 release_tree_vector (args);
4379 /* If the numeric argument didn't work, look for a raw literal
4380 operator taking a const char* argument consisting of the number
4381 in string format. */
4382 args = make_tree_vector ();
4383 vec_safe_push (args, num_string);
4384 decl = lookup_literal_operator (name, args);
4385 if (decl && decl != error_mark_node)
4387 result = finish_call_expr (decl, &args, false, true,
4388 tf_warning_or_error);
4389 release_tree_vector (args);
4390 return result;
4392 release_tree_vector (args);
4394 /* If the raw literal didn't work, look for a non-type template
4395 function with parameter pack char.... Call the function with
4396 template parameter characters representing the number. */
4397 args = make_tree_vector ();
4398 decl = lookup_literal_operator (name, args);
4399 if (decl && decl != error_mark_node)
4401 tree tmpl_args = make_char_string_pack (num_string);
4402 decl = lookup_template_function (decl, tmpl_args);
4403 result = finish_call_expr (decl, &args, false, true,
4404 tf_warning_or_error);
4405 release_tree_vector (args);
4406 return result;
4409 release_tree_vector (args);
4411 error ("unable to find numeric literal operator %qD", name);
4412 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4413 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4414 "to enable more built-in suffixes");
4415 return error_mark_node;
4418 /* Parse a user-defined string constant. Returns a call to a user-defined
4419 literal operator taking a character pointer and the length of the string
4420 as arguments. */
4422 static tree
4423 cp_parser_userdef_string_literal (tree literal)
4425 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4426 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4427 tree value = USERDEF_LITERAL_VALUE (literal);
4428 int len = TREE_STRING_LENGTH (value)
4429 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4430 tree decl, result;
4431 vec<tree, va_gc> *args;
4433 /* Build up a call to the user-defined operator. */
4434 /* Lookup the name we got back from the id-expression. */
4435 args = make_tree_vector ();
4436 vec_safe_push (args, value);
4437 vec_safe_push (args, build_int_cst (size_type_node, len));
4438 decl = lookup_literal_operator (name, args);
4440 if (decl && decl != error_mark_node)
4442 result = finish_call_expr (decl, &args, false, true,
4443 tf_warning_or_error);
4444 release_tree_vector (args);
4445 return result;
4447 release_tree_vector (args);
4449 /* Look for a template function with typename parameter CharT
4450 and parameter pack CharT... Call the function with
4451 template parameter characters representing the string. */
4452 args = make_tree_vector ();
4453 decl = lookup_literal_operator (name, args);
4454 if (decl && decl != error_mark_node)
4456 tree tmpl_args = make_string_pack (value);
4457 decl = lookup_template_function (decl, tmpl_args);
4458 result = finish_call_expr (decl, &args, false, true,
4459 tf_warning_or_error);
4460 release_tree_vector (args);
4461 return result;
4463 release_tree_vector (args);
4465 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4466 name, TREE_TYPE (value), size_type_node);
4467 return error_mark_node;
4471 /* Basic concepts [gram.basic] */
4473 /* Parse a translation-unit.
4475 translation-unit:
4476 declaration-seq [opt]
4478 Returns TRUE if all went well. */
4480 static bool
4481 cp_parser_translation_unit (cp_parser* parser)
4483 /* The address of the first non-permanent object on the declarator
4484 obstack. */
4485 static void *declarator_obstack_base;
4487 bool success;
4489 /* Create the declarator obstack, if necessary. */
4490 if (!cp_error_declarator)
4492 gcc_obstack_init (&declarator_obstack);
4493 /* Create the error declarator. */
4494 cp_error_declarator = make_declarator (cdk_error);
4495 /* Create the empty parameter list. */
4496 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4497 UNKNOWN_LOCATION);
4498 /* Remember where the base of the declarator obstack lies. */
4499 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4502 cp_parser_declaration_seq_opt (parser);
4504 /* If there are no tokens left then all went well. */
4505 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4507 /* Get rid of the token array; we don't need it any more. */
4508 cp_lexer_destroy (parser->lexer);
4509 parser->lexer = NULL;
4511 /* This file might have been a context that's implicitly extern
4512 "C". If so, pop the lang context. (Only relevant for PCH.) */
4513 if (parser->implicit_extern_c)
4515 pop_lang_context ();
4516 parser->implicit_extern_c = false;
4519 /* Finish up. */
4520 finish_translation_unit ();
4522 success = true;
4524 else
4526 cp_parser_error (parser, "expected declaration");
4527 success = false;
4530 /* Make sure the declarator obstack was fully cleaned up. */
4531 gcc_assert (obstack_next_free (&declarator_obstack)
4532 == declarator_obstack_base);
4534 /* All went well. */
4535 return success;
4538 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4539 decltype context. */
4541 static inline tsubst_flags_t
4542 complain_flags (bool decltype_p)
4544 tsubst_flags_t complain = tf_warning_or_error;
4545 if (decltype_p)
4546 complain |= tf_decltype;
4547 return complain;
4550 /* We're about to parse a collection of statements. If we're currently
4551 parsing tentatively, set up a firewall so that any nested
4552 cp_parser_commit_to_tentative_parse won't affect the current context. */
4554 static cp_token_position
4555 cp_parser_start_tentative_firewall (cp_parser *parser)
4557 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4558 return 0;
4560 cp_parser_parse_tentatively (parser);
4561 cp_parser_commit_to_topmost_tentative_parse (parser);
4562 return cp_lexer_token_position (parser->lexer, false);
4565 /* We've finished parsing the collection of statements. Wrap up the
4566 firewall and replace the relevant tokens with the parsed form. */
4568 static void
4569 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4570 tree expr)
4572 if (!start)
4573 return;
4575 /* Finish the firewall level. */
4576 cp_parser_parse_definitely (parser);
4577 /* And remember the result of the parse for when we try again. */
4578 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4579 token->type = CPP_PREPARSED_EXPR;
4580 token->u.value = expr;
4581 token->keyword = RID_MAX;
4582 cp_lexer_purge_tokens_after (parser->lexer, start);
4585 /* Like the above functions, but let the user modify the tokens. Used by
4586 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4587 later parses, so it makes sense to localize the effects of
4588 cp_parser_commit_to_tentative_parse. */
4590 struct tentative_firewall
4592 cp_parser *parser;
4593 bool set;
4595 tentative_firewall (cp_parser *p): parser(p)
4597 /* If we're currently parsing tentatively, start a committed level as a
4598 firewall and then an inner tentative parse. */
4599 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4601 cp_parser_parse_tentatively (parser);
4602 cp_parser_commit_to_topmost_tentative_parse (parser);
4603 cp_parser_parse_tentatively (parser);
4607 ~tentative_firewall()
4609 if (set)
4611 /* Finish the inner tentative parse and the firewall, propagating any
4612 uncommitted error state to the outer tentative parse. */
4613 bool err = cp_parser_error_occurred (parser);
4614 cp_parser_parse_definitely (parser);
4615 cp_parser_parse_definitely (parser);
4616 if (err)
4617 cp_parser_simulate_error (parser);
4622 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4623 This class is for tracking such a matching pair of symbols.
4624 In particular, it tracks the location of the first token,
4625 so that if the second token is missing, we can highlight the
4626 location of the first token when notifying the user about the
4627 problem. */
4629 template <typename traits_t>
4630 class token_pair
4632 public:
4633 /* token_pair's ctor. */
4634 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4636 /* If the next token is the opening symbol for this pair, consume it and
4637 return true.
4638 Otherwise, issue an error and return false.
4639 In either case, record the location of the opening token. */
4641 bool require_open (cp_parser *parser)
4643 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4644 return cp_parser_require (parser, traits_t::open_token_type,
4645 traits_t::required_token_open);
4648 /* Consume the next token from PARSER, recording its location as
4649 that of the opening token within the pair. */
4651 cp_token * consume_open (cp_parser *parser)
4653 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4654 gcc_assert (tok->type == traits_t::open_token_type);
4655 m_open_loc = tok->location;
4656 return tok;
4659 /* If the next token is the closing symbol for this pair, consume it
4660 and return it.
4661 Otherwise, issue an error, highlighting the location of the
4662 corresponding opening token, and return NULL. */
4664 cp_token *require_close (cp_parser *parser) const
4666 return cp_parser_require (parser, traits_t::close_token_type,
4667 traits_t::required_token_close,
4668 m_open_loc);
4671 private:
4672 location_t m_open_loc;
4675 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4677 struct matching_paren_traits
4679 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4680 static const enum required_token required_token_open = RT_OPEN_PAREN;
4681 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4682 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4685 /* "matching_parens" is a token_pair<T> class for tracking matching
4686 pairs of parentheses. */
4688 typedef token_pair<matching_paren_traits> matching_parens;
4690 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4692 struct matching_brace_traits
4694 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4695 static const enum required_token required_token_open = RT_OPEN_BRACE;
4696 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4697 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4700 /* "matching_braces" is a token_pair<T> class for tracking matching
4701 pairs of braces. */
4703 typedef token_pair<matching_brace_traits> matching_braces;
4706 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4707 enclosing parentheses. */
4709 static cp_expr
4710 cp_parser_statement_expr (cp_parser *parser)
4712 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4714 /* Consume the '('. */
4715 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4716 matching_parens parens;
4717 parens.consume_open (parser);
4718 /* Start the statement-expression. */
4719 tree expr = begin_stmt_expr ();
4720 /* Parse the compound-statement. */
4721 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4722 /* Finish up. */
4723 expr = finish_stmt_expr (expr, false);
4724 /* Consume the ')'. */
4725 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4726 if (!parens.require_close (parser))
4727 cp_parser_skip_to_end_of_statement (parser);
4729 cp_parser_end_tentative_firewall (parser, start, expr);
4730 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4731 return cp_expr (expr, combined_loc);
4734 /* Expressions [gram.expr] */
4736 /* Parse a fold-operator.
4738 fold-operator:
4739 - * / % ^ & | = < > << >>
4740 = -= *= /= %= ^= &= |= <<= >>=
4741 == != <= >= && || , .* ->*
4743 This returns the tree code corresponding to the matched operator
4744 as an int. When the current token matches a compound assignment
4745 opertor, the resulting tree code is the negative value of the
4746 non-assignment operator. */
4748 static int
4749 cp_parser_fold_operator (cp_token *token)
4751 switch (token->type)
4753 case CPP_PLUS: return PLUS_EXPR;
4754 case CPP_MINUS: return MINUS_EXPR;
4755 case CPP_MULT: return MULT_EXPR;
4756 case CPP_DIV: return TRUNC_DIV_EXPR;
4757 case CPP_MOD: return TRUNC_MOD_EXPR;
4758 case CPP_XOR: return BIT_XOR_EXPR;
4759 case CPP_AND: return BIT_AND_EXPR;
4760 case CPP_OR: return BIT_IOR_EXPR;
4761 case CPP_LSHIFT: return LSHIFT_EXPR;
4762 case CPP_RSHIFT: return RSHIFT_EXPR;
4764 case CPP_EQ: return -NOP_EXPR;
4765 case CPP_PLUS_EQ: return -PLUS_EXPR;
4766 case CPP_MINUS_EQ: return -MINUS_EXPR;
4767 case CPP_MULT_EQ: return -MULT_EXPR;
4768 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4769 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4770 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4771 case CPP_AND_EQ: return -BIT_AND_EXPR;
4772 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4773 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4774 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4776 case CPP_EQ_EQ: return EQ_EXPR;
4777 case CPP_NOT_EQ: return NE_EXPR;
4778 case CPP_LESS: return LT_EXPR;
4779 case CPP_GREATER: return GT_EXPR;
4780 case CPP_LESS_EQ: return LE_EXPR;
4781 case CPP_GREATER_EQ: return GE_EXPR;
4783 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4784 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4786 case CPP_COMMA: return COMPOUND_EXPR;
4788 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4789 case CPP_DEREF_STAR: return MEMBER_REF;
4791 default: return ERROR_MARK;
4795 /* Returns true if CODE indicates a binary expression, which is not allowed in
4796 the LHS of a fold-expression. More codes will need to be added to use this
4797 function in other contexts. */
4799 static bool
4800 is_binary_op (tree_code code)
4802 switch (code)
4804 case PLUS_EXPR:
4805 case POINTER_PLUS_EXPR:
4806 case MINUS_EXPR:
4807 case MULT_EXPR:
4808 case TRUNC_DIV_EXPR:
4809 case TRUNC_MOD_EXPR:
4810 case BIT_XOR_EXPR:
4811 case BIT_AND_EXPR:
4812 case BIT_IOR_EXPR:
4813 case LSHIFT_EXPR:
4814 case RSHIFT_EXPR:
4816 case MODOP_EXPR:
4818 case EQ_EXPR:
4819 case NE_EXPR:
4820 case LE_EXPR:
4821 case GE_EXPR:
4822 case LT_EXPR:
4823 case GT_EXPR:
4825 case TRUTH_ANDIF_EXPR:
4826 case TRUTH_ORIF_EXPR:
4828 case COMPOUND_EXPR:
4830 case DOTSTAR_EXPR:
4831 case MEMBER_REF:
4832 return true;
4834 default:
4835 return false;
4839 /* If the next token is a suitable fold operator, consume it and return as
4840 the function above. */
4842 static int
4843 cp_parser_fold_operator (cp_parser *parser)
4845 cp_token* token = cp_lexer_peek_token (parser->lexer);
4846 int code = cp_parser_fold_operator (token);
4847 if (code != ERROR_MARK)
4848 cp_lexer_consume_token (parser->lexer);
4849 return code;
4852 /* Parse a fold-expression.
4854 fold-expression:
4855 ( ... folding-operator cast-expression)
4856 ( cast-expression folding-operator ... )
4857 ( cast-expression folding operator ... folding-operator cast-expression)
4859 Note that the '(' and ')' are matched in primary expression. */
4861 static cp_expr
4862 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4864 cp_id_kind pidk;
4866 // Left fold.
4867 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4869 cp_lexer_consume_token (parser->lexer);
4870 int op = cp_parser_fold_operator (parser);
4871 if (op == ERROR_MARK)
4873 cp_parser_error (parser, "expected binary operator");
4874 return error_mark_node;
4877 tree expr = cp_parser_cast_expression (parser, false, false,
4878 false, &pidk);
4879 if (expr == error_mark_node)
4880 return error_mark_node;
4881 return finish_left_unary_fold_expr (expr, op);
4884 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4885 int op = cp_parser_fold_operator (parser);
4886 if (op == ERROR_MARK)
4888 cp_parser_error (parser, "expected binary operator");
4889 return error_mark_node;
4892 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4894 cp_parser_error (parser, "expected ...");
4895 return error_mark_node;
4897 cp_lexer_consume_token (parser->lexer);
4899 /* The operands of a fold-expression are cast-expressions, so binary or
4900 conditional expressions are not allowed. We check this here to avoid
4901 tentative parsing. */
4902 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4903 /* OK, the expression was parenthesized. */;
4904 else if (is_binary_op (TREE_CODE (expr1)))
4905 error_at (location_of (expr1),
4906 "binary expression in operand of fold-expression");
4907 else if (TREE_CODE (expr1) == COND_EXPR)
4908 error_at (location_of (expr1),
4909 "conditional expression in operand of fold-expression");
4911 // Right fold.
4912 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4913 return finish_right_unary_fold_expr (expr1, op);
4915 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4917 cp_parser_error (parser, "mismatched operator in fold-expression");
4918 return error_mark_node;
4920 cp_lexer_consume_token (parser->lexer);
4922 // Binary left or right fold.
4923 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4924 if (expr2 == error_mark_node)
4925 return error_mark_node;
4926 return finish_binary_fold_expr (expr1, expr2, op);
4929 /* Parse a primary-expression.
4931 primary-expression:
4932 literal
4933 this
4934 ( expression )
4935 id-expression
4936 lambda-expression (C++11)
4938 GNU Extensions:
4940 primary-expression:
4941 ( compound-statement )
4942 __builtin_va_arg ( assignment-expression , type-id )
4943 __builtin_offsetof ( type-id , offsetof-expression )
4945 C++ Extensions:
4946 __has_nothrow_assign ( type-id )
4947 __has_nothrow_constructor ( type-id )
4948 __has_nothrow_copy ( type-id )
4949 __has_trivial_assign ( type-id )
4950 __has_trivial_constructor ( type-id )
4951 __has_trivial_copy ( type-id )
4952 __has_trivial_destructor ( type-id )
4953 __has_virtual_destructor ( type-id )
4954 __is_abstract ( type-id )
4955 __is_base_of ( type-id , type-id )
4956 __is_class ( type-id )
4957 __is_empty ( type-id )
4958 __is_enum ( type-id )
4959 __is_final ( type-id )
4960 __is_literal_type ( type-id )
4961 __is_pod ( type-id )
4962 __is_polymorphic ( type-id )
4963 __is_std_layout ( type-id )
4964 __is_trivial ( type-id )
4965 __is_union ( type-id )
4967 Objective-C++ Extension:
4969 primary-expression:
4970 objc-expression
4972 literal:
4973 __null
4975 ADDRESS_P is true iff this expression was immediately preceded by
4976 "&" and therefore might denote a pointer-to-member. CAST_P is true
4977 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4978 true iff this expression is a template argument.
4980 Returns a representation of the expression. Upon return, *IDK
4981 indicates what kind of id-expression (if any) was present. */
4983 static cp_expr
4984 cp_parser_primary_expression (cp_parser *parser,
4985 bool address_p,
4986 bool cast_p,
4987 bool template_arg_p,
4988 bool decltype_p,
4989 cp_id_kind *idk)
4991 cp_token *token = NULL;
4993 /* Assume the primary expression is not an id-expression. */
4994 *idk = CP_ID_KIND_NONE;
4996 /* Peek at the next token. */
4997 token = cp_lexer_peek_token (parser->lexer);
4998 switch ((int) token->type)
5000 /* literal:
5001 integer-literal
5002 character-literal
5003 floating-literal
5004 string-literal
5005 boolean-literal
5006 pointer-literal
5007 user-defined-literal */
5008 case CPP_CHAR:
5009 case CPP_CHAR16:
5010 case CPP_CHAR32:
5011 case CPP_WCHAR:
5012 case CPP_UTF8CHAR:
5013 case CPP_NUMBER:
5014 case CPP_PREPARSED_EXPR:
5015 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5016 return cp_parser_userdef_numeric_literal (parser);
5017 token = cp_lexer_consume_token (parser->lexer);
5018 if (TREE_CODE (token->u.value) == FIXED_CST)
5020 error_at (token->location,
5021 "fixed-point types not supported in C++");
5022 return error_mark_node;
5024 /* Floating-point literals are only allowed in an integral
5025 constant expression if they are cast to an integral or
5026 enumeration type. */
5027 if (TREE_CODE (token->u.value) == REAL_CST
5028 && parser->integral_constant_expression_p
5029 && pedantic)
5031 /* CAST_P will be set even in invalid code like "int(2.7 +
5032 ...)". Therefore, we have to check that the next token
5033 is sure to end the cast. */
5034 if (cast_p)
5036 cp_token *next_token;
5038 next_token = cp_lexer_peek_token (parser->lexer);
5039 if (/* The comma at the end of an
5040 enumerator-definition. */
5041 next_token->type != CPP_COMMA
5042 /* The curly brace at the end of an enum-specifier. */
5043 && next_token->type != CPP_CLOSE_BRACE
5044 /* The end of a statement. */
5045 && next_token->type != CPP_SEMICOLON
5046 /* The end of the cast-expression. */
5047 && next_token->type != CPP_CLOSE_PAREN
5048 /* The end of an array bound. */
5049 && next_token->type != CPP_CLOSE_SQUARE
5050 /* The closing ">" in a template-argument-list. */
5051 && (next_token->type != CPP_GREATER
5052 || parser->greater_than_is_operator_p)
5053 /* C++0x only: A ">>" treated like two ">" tokens,
5054 in a template-argument-list. */
5055 && (next_token->type != CPP_RSHIFT
5056 || (cxx_dialect == cxx98)
5057 || parser->greater_than_is_operator_p))
5058 cast_p = false;
5061 /* If we are within a cast, then the constraint that the
5062 cast is to an integral or enumeration type will be
5063 checked at that point. If we are not within a cast, then
5064 this code is invalid. */
5065 if (!cast_p)
5066 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5068 return cp_expr (token->u.value, token->location);
5070 case CPP_CHAR_USERDEF:
5071 case CPP_CHAR16_USERDEF:
5072 case CPP_CHAR32_USERDEF:
5073 case CPP_WCHAR_USERDEF:
5074 case CPP_UTF8CHAR_USERDEF:
5075 return cp_parser_userdef_char_literal (parser);
5077 case CPP_STRING:
5078 case CPP_STRING16:
5079 case CPP_STRING32:
5080 case CPP_WSTRING:
5081 case CPP_UTF8STRING:
5082 case CPP_STRING_USERDEF:
5083 case CPP_STRING16_USERDEF:
5084 case CPP_STRING32_USERDEF:
5085 case CPP_WSTRING_USERDEF:
5086 case CPP_UTF8STRING_USERDEF:
5087 /* ??? Should wide strings be allowed when parser->translate_strings_p
5088 is false (i.e. in attributes)? If not, we can kill the third
5089 argument to cp_parser_string_literal. */
5090 return cp_parser_string_literal (parser,
5091 parser->translate_strings_p,
5092 true);
5094 case CPP_OPEN_PAREN:
5095 /* If we see `( { ' then we are looking at the beginning of
5096 a GNU statement-expression. */
5097 if (cp_parser_allow_gnu_extensions_p (parser)
5098 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5100 /* Statement-expressions are not allowed by the standard. */
5101 pedwarn (token->location, OPT_Wpedantic,
5102 "ISO C++ forbids braced-groups within expressions");
5104 /* And they're not allowed outside of a function-body; you
5105 cannot, for example, write:
5107 int i = ({ int j = 3; j + 1; });
5109 at class or namespace scope. */
5110 if (!parser->in_function_body
5111 || parser->in_template_argument_list_p)
5113 error_at (token->location,
5114 "statement-expressions are not allowed outside "
5115 "functions nor in template-argument lists");
5116 cp_parser_skip_to_end_of_block_or_statement (parser);
5117 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5118 cp_lexer_consume_token (parser->lexer);
5119 return error_mark_node;
5121 else
5122 return cp_parser_statement_expr (parser);
5124 /* Otherwise it's a normal parenthesized expression. */
5126 cp_expr expr;
5127 bool saved_greater_than_is_operator_p;
5129 location_t open_paren_loc = token->location;
5131 /* Consume the `('. */
5132 matching_parens parens;
5133 parens.consume_open (parser);
5134 /* Within a parenthesized expression, a `>' token is always
5135 the greater-than operator. */
5136 saved_greater_than_is_operator_p
5137 = parser->greater_than_is_operator_p;
5138 parser->greater_than_is_operator_p = true;
5140 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5141 /* Left fold expression. */
5142 expr = NULL_TREE;
5143 else
5144 /* Parse the parenthesized expression. */
5145 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5147 token = cp_lexer_peek_token (parser->lexer);
5148 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5150 expr = cp_parser_fold_expression (parser, expr);
5151 if (expr != error_mark_node
5152 && cxx_dialect < cxx17
5153 && !in_system_header_at (input_location))
5154 pedwarn (input_location, 0, "fold-expressions only available "
5155 "with -std=c++17 or -std=gnu++17");
5157 else
5158 /* Let the front end know that this expression was
5159 enclosed in parentheses. This matters in case, for
5160 example, the expression is of the form `A::B', since
5161 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5162 not. */
5163 expr = finish_parenthesized_expr (expr);
5165 /* DR 705: Wrapping an unqualified name in parentheses
5166 suppresses arg-dependent lookup. We want to pass back
5167 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5168 (c++/37862), but none of the others. */
5169 if (*idk != CP_ID_KIND_QUALIFIED)
5170 *idk = CP_ID_KIND_NONE;
5172 /* The `>' token might be the end of a template-id or
5173 template-parameter-list now. */
5174 parser->greater_than_is_operator_p
5175 = saved_greater_than_is_operator_p;
5177 /* Consume the `)'. */
5178 token = cp_lexer_peek_token (parser->lexer);
5179 location_t close_paren_loc = token->location;
5180 expr.set_range (open_paren_loc, close_paren_loc);
5181 if (!parens.require_close (parser)
5182 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5183 cp_parser_skip_to_end_of_statement (parser);
5185 return expr;
5188 case CPP_OPEN_SQUARE:
5190 if (c_dialect_objc ())
5192 /* We might have an Objective-C++ message. */
5193 cp_parser_parse_tentatively (parser);
5194 tree msg = cp_parser_objc_message_expression (parser);
5195 /* If that works out, we're done ... */
5196 if (cp_parser_parse_definitely (parser))
5197 return msg;
5198 /* ... else, fall though to see if it's a lambda. */
5200 cp_expr lam = cp_parser_lambda_expression (parser);
5201 /* Don't warn about a failed tentative parse. */
5202 if (cp_parser_error_occurred (parser))
5203 return error_mark_node;
5204 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5205 return lam;
5208 case CPP_OBJC_STRING:
5209 if (c_dialect_objc ())
5210 /* We have an Objective-C++ string literal. */
5211 return cp_parser_objc_expression (parser);
5212 cp_parser_error (parser, "expected primary-expression");
5213 return error_mark_node;
5215 case CPP_KEYWORD:
5216 switch (token->keyword)
5218 /* These two are the boolean literals. */
5219 case RID_TRUE:
5220 cp_lexer_consume_token (parser->lexer);
5221 return cp_expr (boolean_true_node, token->location);
5222 case RID_FALSE:
5223 cp_lexer_consume_token (parser->lexer);
5224 return cp_expr (boolean_false_node, token->location);
5226 /* The `__null' literal. */
5227 case RID_NULL:
5228 cp_lexer_consume_token (parser->lexer);
5229 return cp_expr (null_node, token->location);
5231 /* The `nullptr' literal. */
5232 case RID_NULLPTR:
5233 cp_lexer_consume_token (parser->lexer);
5234 return cp_expr (nullptr_node, token->location);
5236 /* Recognize the `this' keyword. */
5237 case RID_THIS:
5238 cp_lexer_consume_token (parser->lexer);
5239 if (parser->local_variables_forbidden_p)
5241 error_at (token->location,
5242 "%<this%> may not be used in this context");
5243 return error_mark_node;
5245 /* Pointers cannot appear in constant-expressions. */
5246 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5247 return error_mark_node;
5248 return cp_expr (finish_this_expr (), token->location);
5250 /* The `operator' keyword can be the beginning of an
5251 id-expression. */
5252 case RID_OPERATOR:
5253 goto id_expression;
5255 case RID_FUNCTION_NAME:
5256 case RID_PRETTY_FUNCTION_NAME:
5257 case RID_C99_FUNCTION_NAME:
5259 non_integral_constant name;
5261 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5262 __func__ are the names of variables -- but they are
5263 treated specially. Therefore, they are handled here,
5264 rather than relying on the generic id-expression logic
5265 below. Grammatically, these names are id-expressions.
5267 Consume the token. */
5268 token = cp_lexer_consume_token (parser->lexer);
5270 switch (token->keyword)
5272 case RID_FUNCTION_NAME:
5273 name = NIC_FUNC_NAME;
5274 break;
5275 case RID_PRETTY_FUNCTION_NAME:
5276 name = NIC_PRETTY_FUNC;
5277 break;
5278 case RID_C99_FUNCTION_NAME:
5279 name = NIC_C99_FUNC;
5280 break;
5281 default:
5282 gcc_unreachable ();
5285 if (cp_parser_non_integral_constant_expression (parser, name))
5286 return error_mark_node;
5288 /* Look up the name. */
5289 return finish_fname (token->u.value);
5292 case RID_VA_ARG:
5294 tree expression;
5295 tree type;
5296 source_location type_location;
5297 location_t start_loc
5298 = cp_lexer_peek_token (parser->lexer)->location;
5299 /* The `__builtin_va_arg' construct is used to handle
5300 `va_arg'. Consume the `__builtin_va_arg' token. */
5301 cp_lexer_consume_token (parser->lexer);
5302 /* Look for the opening `('. */
5303 matching_parens parens;
5304 parens.require_open (parser);
5305 /* Now, parse the assignment-expression. */
5306 expression = cp_parser_assignment_expression (parser);
5307 /* Look for the `,'. */
5308 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5309 type_location = cp_lexer_peek_token (parser->lexer)->location;
5310 /* Parse the type-id. */
5312 type_id_in_expr_sentinel s (parser);
5313 type = cp_parser_type_id (parser);
5315 /* Look for the closing `)'. */
5316 location_t finish_loc
5317 = cp_lexer_peek_token (parser->lexer)->location;
5318 parens.require_close (parser);
5319 /* Using `va_arg' in a constant-expression is not
5320 allowed. */
5321 if (cp_parser_non_integral_constant_expression (parser,
5322 NIC_VA_ARG))
5323 return error_mark_node;
5324 /* Construct a location of the form:
5325 __builtin_va_arg (v, int)
5326 ~~~~~~~~~~~~~~~~~~~~~^~~~
5327 with the caret at the type, ranging from the start of the
5328 "__builtin_va_arg" token to the close paren. */
5329 location_t combined_loc
5330 = make_location (type_location, start_loc, finish_loc);
5331 return build_x_va_arg (combined_loc, expression, type);
5334 case RID_OFFSETOF:
5335 return cp_parser_builtin_offsetof (parser);
5337 case RID_HAS_NOTHROW_ASSIGN:
5338 case RID_HAS_NOTHROW_CONSTRUCTOR:
5339 case RID_HAS_NOTHROW_COPY:
5340 case RID_HAS_TRIVIAL_ASSIGN:
5341 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5342 case RID_HAS_TRIVIAL_COPY:
5343 case RID_HAS_TRIVIAL_DESTRUCTOR:
5344 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5345 case RID_HAS_VIRTUAL_DESTRUCTOR:
5346 case RID_IS_ABSTRACT:
5347 case RID_IS_AGGREGATE:
5348 case RID_IS_BASE_OF:
5349 case RID_IS_CLASS:
5350 case RID_IS_EMPTY:
5351 case RID_IS_ENUM:
5352 case RID_IS_FINAL:
5353 case RID_IS_LITERAL_TYPE:
5354 case RID_IS_POD:
5355 case RID_IS_POLYMORPHIC:
5356 case RID_IS_SAME_AS:
5357 case RID_IS_STD_LAYOUT:
5358 case RID_IS_TRIVIAL:
5359 case RID_IS_TRIVIALLY_ASSIGNABLE:
5360 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5361 case RID_IS_TRIVIALLY_COPYABLE:
5362 case RID_IS_UNION:
5363 case RID_IS_ASSIGNABLE:
5364 case RID_IS_CONSTRUCTIBLE:
5365 return cp_parser_trait_expr (parser, token->keyword);
5367 // C++ concepts
5368 case RID_REQUIRES:
5369 return cp_parser_requires_expression (parser);
5371 /* Objective-C++ expressions. */
5372 case RID_AT_ENCODE:
5373 case RID_AT_PROTOCOL:
5374 case RID_AT_SELECTOR:
5375 return cp_parser_objc_expression (parser);
5377 case RID_TEMPLATE:
5378 if (parser->in_function_body
5379 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5380 == CPP_LESS))
5382 error_at (token->location,
5383 "a template declaration cannot appear at block scope");
5384 cp_parser_skip_to_end_of_block_or_statement (parser);
5385 return error_mark_node;
5387 /* FALLTHRU */
5388 default:
5389 cp_parser_error (parser, "expected primary-expression");
5390 return error_mark_node;
5393 /* An id-expression can start with either an identifier, a
5394 `::' as the beginning of a qualified-id, or the "operator"
5395 keyword. */
5396 case CPP_NAME:
5397 case CPP_SCOPE:
5398 case CPP_TEMPLATE_ID:
5399 case CPP_NESTED_NAME_SPECIFIER:
5401 id_expression:
5402 cp_expr id_expression;
5403 cp_expr decl;
5404 const char *error_msg;
5405 bool template_p;
5406 bool done;
5407 cp_token *id_expr_token;
5409 /* Parse the id-expression. */
5410 id_expression
5411 = cp_parser_id_expression (parser,
5412 /*template_keyword_p=*/false,
5413 /*check_dependency_p=*/true,
5414 &template_p,
5415 /*declarator_p=*/false,
5416 /*optional_p=*/false);
5417 if (id_expression == error_mark_node)
5418 return error_mark_node;
5419 id_expr_token = token;
5420 token = cp_lexer_peek_token (parser->lexer);
5421 done = (token->type != CPP_OPEN_SQUARE
5422 && token->type != CPP_OPEN_PAREN
5423 && token->type != CPP_DOT
5424 && token->type != CPP_DEREF
5425 && token->type != CPP_PLUS_PLUS
5426 && token->type != CPP_MINUS_MINUS);
5427 /* If we have a template-id, then no further lookup is
5428 required. If the template-id was for a template-class, we
5429 will sometimes have a TYPE_DECL at this point. */
5430 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5431 || TREE_CODE (id_expression) == TYPE_DECL)
5432 decl = id_expression;
5433 /* Look up the name. */
5434 else
5436 tree ambiguous_decls;
5438 /* If we already know that this lookup is ambiguous, then
5439 we've already issued an error message; there's no reason
5440 to check again. */
5441 if (id_expr_token->type == CPP_NAME
5442 && id_expr_token->error_reported)
5444 cp_parser_simulate_error (parser);
5445 return error_mark_node;
5448 decl = cp_parser_lookup_name (parser, id_expression,
5449 none_type,
5450 template_p,
5451 /*is_namespace=*/false,
5452 /*check_dependency=*/true,
5453 &ambiguous_decls,
5454 id_expr_token->location);
5455 /* If the lookup was ambiguous, an error will already have
5456 been issued. */
5457 if (ambiguous_decls)
5458 return error_mark_node;
5460 /* In Objective-C++, we may have an Objective-C 2.0
5461 dot-syntax for classes here. */
5462 if (c_dialect_objc ()
5463 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5464 && TREE_CODE (decl) == TYPE_DECL
5465 && objc_is_class_name (decl))
5467 tree component;
5468 cp_lexer_consume_token (parser->lexer);
5469 component = cp_parser_identifier (parser);
5470 if (component == error_mark_node)
5471 return error_mark_node;
5473 tree result = objc_build_class_component_ref (id_expression,
5474 component);
5475 /* Build a location of the form:
5476 expr.component
5477 ~~~~~^~~~~~~~~
5478 with caret at the start of the component name (at
5479 input_location), ranging from the start of the id_expression
5480 to the end of the component name. */
5481 location_t combined_loc
5482 = make_location (input_location, id_expression.get_start (),
5483 get_finish (input_location));
5484 protected_set_expr_location (result, combined_loc);
5485 return result;
5488 /* In Objective-C++, an instance variable (ivar) may be preferred
5489 to whatever cp_parser_lookup_name() found.
5490 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5491 rest of c-family, we have to do a little extra work to preserve
5492 any location information in cp_expr "decl". Given that
5493 objc_lookup_ivar is implemented in "c-family" and "objc", we
5494 have a trip through the pure "tree" type, rather than cp_expr.
5495 Naively copying it back to "decl" would implicitly give the
5496 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5497 store an EXPR_LOCATION. Hence we only update "decl" (and
5498 hence its location_t) if we get back a different tree node. */
5499 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5500 id_expression);
5501 if (decl_tree != decl.get_value ())
5502 decl = cp_expr (decl_tree);
5504 /* If name lookup gives us a SCOPE_REF, then the
5505 qualifying scope was dependent. */
5506 if (TREE_CODE (decl) == SCOPE_REF)
5508 /* At this point, we do not know if DECL is a valid
5509 integral constant expression. We assume that it is
5510 in fact such an expression, so that code like:
5512 template <int N> struct A {
5513 int a[B<N>::i];
5516 is accepted. At template-instantiation time, we
5517 will check that B<N>::i is actually a constant. */
5518 return decl;
5520 /* Check to see if DECL is a local variable in a context
5521 where that is forbidden. */
5522 if (parser->local_variables_forbidden_p
5523 && local_variable_p (decl))
5525 /* It might be that we only found DECL because we are
5526 trying to be generous with pre-ISO scoping rules.
5527 For example, consider:
5529 int i;
5530 void g() {
5531 for (int i = 0; i < 10; ++i) {}
5532 extern void f(int j = i);
5535 Here, name look up will originally find the out
5536 of scope `i'. We need to issue a warning message,
5537 but then use the global `i'. */
5538 decl = check_for_out_of_scope_variable (decl);
5539 if (local_variable_p (decl))
5541 error_at (id_expr_token->location,
5542 "local variable %qD may not appear in this context",
5543 decl.get_value ());
5544 return error_mark_node;
5549 decl = (finish_id_expression
5550 (id_expression, decl, parser->scope,
5551 idk,
5552 parser->integral_constant_expression_p,
5553 parser->allow_non_integral_constant_expression_p,
5554 &parser->non_integral_constant_expression_p,
5555 template_p, done, address_p,
5556 template_arg_p,
5557 &error_msg,
5558 id_expression.get_location ()));
5559 if (error_msg)
5560 cp_parser_error (parser, error_msg);
5561 decl.set_location (id_expr_token->location);
5562 return decl;
5565 /* Anything else is an error. */
5566 default:
5567 cp_parser_error (parser, "expected primary-expression");
5568 return error_mark_node;
5572 static inline cp_expr
5573 cp_parser_primary_expression (cp_parser *parser,
5574 bool address_p,
5575 bool cast_p,
5576 bool template_arg_p,
5577 cp_id_kind *idk)
5579 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5580 /*decltype*/false, idk);
5583 /* Parse an id-expression.
5585 id-expression:
5586 unqualified-id
5587 qualified-id
5589 qualified-id:
5590 :: [opt] nested-name-specifier template [opt] unqualified-id
5591 :: identifier
5592 :: operator-function-id
5593 :: template-id
5595 Return a representation of the unqualified portion of the
5596 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5597 a `::' or nested-name-specifier.
5599 Often, if the id-expression was a qualified-id, the caller will
5600 want to make a SCOPE_REF to represent the qualified-id. This
5601 function does not do this in order to avoid wastefully creating
5602 SCOPE_REFs when they are not required.
5604 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5605 `template' keyword.
5607 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5608 uninstantiated templates.
5610 If *TEMPLATE_P is non-NULL, it is set to true iff the
5611 `template' keyword is used to explicitly indicate that the entity
5612 named is a template.
5614 If DECLARATOR_P is true, the id-expression is appearing as part of
5615 a declarator, rather than as part of an expression. */
5617 static cp_expr
5618 cp_parser_id_expression (cp_parser *parser,
5619 bool template_keyword_p,
5620 bool check_dependency_p,
5621 bool *template_p,
5622 bool declarator_p,
5623 bool optional_p)
5625 bool global_scope_p;
5626 bool nested_name_specifier_p;
5628 /* Assume the `template' keyword was not used. */
5629 if (template_p)
5630 *template_p = template_keyword_p;
5632 /* Look for the optional `::' operator. */
5633 global_scope_p
5634 = (!template_keyword_p
5635 && (cp_parser_global_scope_opt (parser,
5636 /*current_scope_valid_p=*/false)
5637 != NULL_TREE));
5639 /* Look for the optional nested-name-specifier. */
5640 nested_name_specifier_p
5641 = (cp_parser_nested_name_specifier_opt (parser,
5642 /*typename_keyword_p=*/false,
5643 check_dependency_p,
5644 /*type_p=*/false,
5645 declarator_p,
5646 template_keyword_p)
5647 != NULL_TREE);
5649 /* If there is a nested-name-specifier, then we are looking at
5650 the first qualified-id production. */
5651 if (nested_name_specifier_p)
5653 tree saved_scope;
5654 tree saved_object_scope;
5655 tree saved_qualifying_scope;
5656 cp_expr unqualified_id;
5657 bool is_template;
5659 /* See if the next token is the `template' keyword. */
5660 if (!template_p)
5661 template_p = &is_template;
5662 *template_p = cp_parser_optional_template_keyword (parser);
5663 /* Name lookup we do during the processing of the
5664 unqualified-id might obliterate SCOPE. */
5665 saved_scope = parser->scope;
5666 saved_object_scope = parser->object_scope;
5667 saved_qualifying_scope = parser->qualifying_scope;
5668 /* Process the final unqualified-id. */
5669 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5670 check_dependency_p,
5671 declarator_p,
5672 /*optional_p=*/false);
5673 /* Restore the SAVED_SCOPE for our caller. */
5674 parser->scope = saved_scope;
5675 parser->object_scope = saved_object_scope;
5676 parser->qualifying_scope = saved_qualifying_scope;
5678 return unqualified_id;
5680 /* Otherwise, if we are in global scope, then we are looking at one
5681 of the other qualified-id productions. */
5682 else if (global_scope_p)
5684 cp_token *token;
5685 tree id;
5687 /* Peek at the next token. */
5688 token = cp_lexer_peek_token (parser->lexer);
5690 /* If it's an identifier, and the next token is not a "<", then
5691 we can avoid the template-id case. This is an optimization
5692 for this common case. */
5693 if (token->type == CPP_NAME
5694 && !cp_parser_nth_token_starts_template_argument_list_p
5695 (parser, 2))
5696 return cp_parser_identifier (parser);
5698 cp_parser_parse_tentatively (parser);
5699 /* Try a template-id. */
5700 id = cp_parser_template_id (parser,
5701 /*template_keyword_p=*/false,
5702 /*check_dependency_p=*/true,
5703 none_type,
5704 declarator_p);
5705 /* If that worked, we're done. */
5706 if (cp_parser_parse_definitely (parser))
5707 return id;
5709 /* Peek at the next token. (Changes in the token buffer may
5710 have invalidated the pointer obtained above.) */
5711 token = cp_lexer_peek_token (parser->lexer);
5713 switch (token->type)
5715 case CPP_NAME:
5716 return cp_parser_identifier (parser);
5718 case CPP_KEYWORD:
5719 if (token->keyword == RID_OPERATOR)
5720 return cp_parser_operator_function_id (parser);
5721 /* Fall through. */
5723 default:
5724 cp_parser_error (parser, "expected id-expression");
5725 return error_mark_node;
5728 else
5729 return cp_parser_unqualified_id (parser, template_keyword_p,
5730 /*check_dependency_p=*/true,
5731 declarator_p,
5732 optional_p);
5735 /* Parse an unqualified-id.
5737 unqualified-id:
5738 identifier
5739 operator-function-id
5740 conversion-function-id
5741 ~ class-name
5742 template-id
5744 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5745 keyword, in a construct like `A::template ...'.
5747 Returns a representation of unqualified-id. For the `identifier'
5748 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5749 production a BIT_NOT_EXPR is returned; the operand of the
5750 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5751 other productions, see the documentation accompanying the
5752 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5753 names are looked up in uninstantiated templates. If DECLARATOR_P
5754 is true, the unqualified-id is appearing as part of a declarator,
5755 rather than as part of an expression. */
5757 static cp_expr
5758 cp_parser_unqualified_id (cp_parser* parser,
5759 bool template_keyword_p,
5760 bool check_dependency_p,
5761 bool declarator_p,
5762 bool optional_p)
5764 cp_token *token;
5766 /* Peek at the next token. */
5767 token = cp_lexer_peek_token (parser->lexer);
5769 switch ((int) token->type)
5771 case CPP_NAME:
5773 tree id;
5775 /* We don't know yet whether or not this will be a
5776 template-id. */
5777 cp_parser_parse_tentatively (parser);
5778 /* Try a template-id. */
5779 id = cp_parser_template_id (parser, template_keyword_p,
5780 check_dependency_p,
5781 none_type,
5782 declarator_p);
5783 /* If it worked, we're done. */
5784 if (cp_parser_parse_definitely (parser))
5785 return id;
5786 /* Otherwise, it's an ordinary identifier. */
5787 return cp_parser_identifier (parser);
5790 case CPP_TEMPLATE_ID:
5791 return cp_parser_template_id (parser, template_keyword_p,
5792 check_dependency_p,
5793 none_type,
5794 declarator_p);
5796 case CPP_COMPL:
5798 tree type_decl;
5799 tree qualifying_scope;
5800 tree object_scope;
5801 tree scope;
5802 bool done;
5804 /* Consume the `~' token. */
5805 cp_lexer_consume_token (parser->lexer);
5806 /* Parse the class-name. The standard, as written, seems to
5807 say that:
5809 template <typename T> struct S { ~S (); };
5810 template <typename T> S<T>::~S() {}
5812 is invalid, since `~' must be followed by a class-name, but
5813 `S<T>' is dependent, and so not known to be a class.
5814 That's not right; we need to look in uninstantiated
5815 templates. A further complication arises from:
5817 template <typename T> void f(T t) {
5818 t.T::~T();
5821 Here, it is not possible to look up `T' in the scope of `T'
5822 itself. We must look in both the current scope, and the
5823 scope of the containing complete expression.
5825 Yet another issue is:
5827 struct S {
5828 int S;
5829 ~S();
5832 S::~S() {}
5834 The standard does not seem to say that the `S' in `~S'
5835 should refer to the type `S' and not the data member
5836 `S::S'. */
5838 /* DR 244 says that we look up the name after the "~" in the
5839 same scope as we looked up the qualifying name. That idea
5840 isn't fully worked out; it's more complicated than that. */
5841 scope = parser->scope;
5842 object_scope = parser->object_scope;
5843 qualifying_scope = parser->qualifying_scope;
5845 /* Check for invalid scopes. */
5846 if (scope == error_mark_node)
5848 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5849 cp_lexer_consume_token (parser->lexer);
5850 return error_mark_node;
5852 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5854 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5855 error_at (token->location,
5856 "scope %qT before %<~%> is not a class-name",
5857 scope);
5858 cp_parser_simulate_error (parser);
5859 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5860 cp_lexer_consume_token (parser->lexer);
5861 return error_mark_node;
5863 gcc_assert (!scope || TYPE_P (scope));
5865 /* If the name is of the form "X::~X" it's OK even if X is a
5866 typedef. */
5867 token = cp_lexer_peek_token (parser->lexer);
5868 if (scope
5869 && token->type == CPP_NAME
5870 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5871 != CPP_LESS)
5872 && (token->u.value == TYPE_IDENTIFIER (scope)
5873 || (CLASS_TYPE_P (scope)
5874 && constructor_name_p (token->u.value, scope))))
5876 cp_lexer_consume_token (parser->lexer);
5877 return build_nt (BIT_NOT_EXPR, scope);
5880 /* ~auto means the destructor of whatever the object is. */
5881 if (cp_parser_is_keyword (token, RID_AUTO))
5883 if (cxx_dialect < cxx14)
5884 pedwarn (input_location, 0,
5885 "%<~auto%> only available with "
5886 "-std=c++14 or -std=gnu++14");
5887 cp_lexer_consume_token (parser->lexer);
5888 return build_nt (BIT_NOT_EXPR, make_auto ());
5891 /* If there was an explicit qualification (S::~T), first look
5892 in the scope given by the qualification (i.e., S).
5894 Note: in the calls to cp_parser_class_name below we pass
5895 typename_type so that lookup finds the injected-class-name
5896 rather than the constructor. */
5897 done = false;
5898 type_decl = NULL_TREE;
5899 if (scope)
5901 cp_parser_parse_tentatively (parser);
5902 type_decl = cp_parser_class_name (parser,
5903 /*typename_keyword_p=*/false,
5904 /*template_keyword_p=*/false,
5905 typename_type,
5906 /*check_dependency=*/false,
5907 /*class_head_p=*/false,
5908 declarator_p);
5909 if (cp_parser_parse_definitely (parser))
5910 done = true;
5912 /* In "N::S::~S", look in "N" as well. */
5913 if (!done && scope && qualifying_scope)
5915 cp_parser_parse_tentatively (parser);
5916 parser->scope = qualifying_scope;
5917 parser->object_scope = NULL_TREE;
5918 parser->qualifying_scope = NULL_TREE;
5919 type_decl
5920 = cp_parser_class_name (parser,
5921 /*typename_keyword_p=*/false,
5922 /*template_keyword_p=*/false,
5923 typename_type,
5924 /*check_dependency=*/false,
5925 /*class_head_p=*/false,
5926 declarator_p);
5927 if (cp_parser_parse_definitely (parser))
5928 done = true;
5930 /* In "p->S::~T", look in the scope given by "*p" as well. */
5931 else if (!done && object_scope)
5933 cp_parser_parse_tentatively (parser);
5934 parser->scope = object_scope;
5935 parser->object_scope = NULL_TREE;
5936 parser->qualifying_scope = NULL_TREE;
5937 type_decl
5938 = cp_parser_class_name (parser,
5939 /*typename_keyword_p=*/false,
5940 /*template_keyword_p=*/false,
5941 typename_type,
5942 /*check_dependency=*/false,
5943 /*class_head_p=*/false,
5944 declarator_p);
5945 if (cp_parser_parse_definitely (parser))
5946 done = true;
5948 /* Look in the surrounding context. */
5949 if (!done)
5951 parser->scope = NULL_TREE;
5952 parser->object_scope = NULL_TREE;
5953 parser->qualifying_scope = NULL_TREE;
5954 if (processing_template_decl)
5955 cp_parser_parse_tentatively (parser);
5956 type_decl
5957 = cp_parser_class_name (parser,
5958 /*typename_keyword_p=*/false,
5959 /*template_keyword_p=*/false,
5960 typename_type,
5961 /*check_dependency=*/false,
5962 /*class_head_p=*/false,
5963 declarator_p);
5964 if (processing_template_decl
5965 && ! cp_parser_parse_definitely (parser))
5967 /* We couldn't find a type with this name. If we're parsing
5968 tentatively, fail and try something else. */
5969 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5971 cp_parser_simulate_error (parser);
5972 return error_mark_node;
5974 /* Otherwise, accept it and check for a match at instantiation
5975 time. */
5976 type_decl = cp_parser_identifier (parser);
5977 if (type_decl != error_mark_node)
5978 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5979 return type_decl;
5982 /* If an error occurred, assume that the name of the
5983 destructor is the same as the name of the qualifying
5984 class. That allows us to keep parsing after running
5985 into ill-formed destructor names. */
5986 if (type_decl == error_mark_node && scope)
5987 return build_nt (BIT_NOT_EXPR, scope);
5988 else if (type_decl == error_mark_node)
5989 return error_mark_node;
5991 /* Check that destructor name and scope match. */
5992 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5994 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5995 error_at (token->location,
5996 "declaration of %<~%T%> as member of %qT",
5997 type_decl, scope);
5998 cp_parser_simulate_error (parser);
5999 return error_mark_node;
6002 /* [class.dtor]
6004 A typedef-name that names a class shall not be used as the
6005 identifier in the declarator for a destructor declaration. */
6006 if (declarator_p
6007 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6008 && !DECL_SELF_REFERENCE_P (type_decl)
6009 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6010 error_at (token->location,
6011 "typedef-name %qD used as destructor declarator",
6012 type_decl);
6014 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6017 case CPP_KEYWORD:
6018 if (token->keyword == RID_OPERATOR)
6020 cp_expr id;
6022 /* This could be a template-id, so we try that first. */
6023 cp_parser_parse_tentatively (parser);
6024 /* Try a template-id. */
6025 id = cp_parser_template_id (parser, template_keyword_p,
6026 /*check_dependency_p=*/true,
6027 none_type,
6028 declarator_p);
6029 /* If that worked, we're done. */
6030 if (cp_parser_parse_definitely (parser))
6031 return id;
6032 /* We still don't know whether we're looking at an
6033 operator-function-id or a conversion-function-id. */
6034 cp_parser_parse_tentatively (parser);
6035 /* Try an operator-function-id. */
6036 id = cp_parser_operator_function_id (parser);
6037 /* If that didn't work, try a conversion-function-id. */
6038 if (!cp_parser_parse_definitely (parser))
6039 id = cp_parser_conversion_function_id (parser);
6040 else if (UDLIT_OPER_P (id))
6042 /* 17.6.3.3.5 */
6043 const char *name = UDLIT_OP_SUFFIX (id);
6044 if (name[0] != '_' && !in_system_header_at (input_location)
6045 && declarator_p)
6046 warning (OPT_Wliteral_suffix,
6047 "literal operator suffixes not preceded by %<_%>"
6048 " are reserved for future standardization");
6051 return id;
6053 /* Fall through. */
6055 default:
6056 if (optional_p)
6057 return NULL_TREE;
6058 cp_parser_error (parser, "expected unqualified-id");
6059 return error_mark_node;
6063 /* Parse an (optional) nested-name-specifier.
6065 nested-name-specifier: [C++98]
6066 class-or-namespace-name :: nested-name-specifier [opt]
6067 class-or-namespace-name :: template nested-name-specifier [opt]
6069 nested-name-specifier: [C++0x]
6070 type-name ::
6071 namespace-name ::
6072 nested-name-specifier identifier ::
6073 nested-name-specifier template [opt] simple-template-id ::
6075 PARSER->SCOPE should be set appropriately before this function is
6076 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6077 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6078 in name lookups.
6080 Sets PARSER->SCOPE to the class (TYPE) or namespace
6081 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6082 it unchanged if there is no nested-name-specifier. Returns the new
6083 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6085 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6086 part of a declaration and/or decl-specifier. */
6088 static tree
6089 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6090 bool typename_keyword_p,
6091 bool check_dependency_p,
6092 bool type_p,
6093 bool is_declaration,
6094 bool template_keyword_p /* = false */)
6096 bool success = false;
6097 cp_token_position start = 0;
6098 cp_token *token;
6100 /* Remember where the nested-name-specifier starts. */
6101 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6103 start = cp_lexer_token_position (parser->lexer, false);
6104 push_deferring_access_checks (dk_deferred);
6107 while (true)
6109 tree new_scope;
6110 tree old_scope;
6111 tree saved_qualifying_scope;
6113 /* Spot cases that cannot be the beginning of a
6114 nested-name-specifier. */
6115 token = cp_lexer_peek_token (parser->lexer);
6117 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6118 the already parsed nested-name-specifier. */
6119 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6121 /* Grab the nested-name-specifier and continue the loop. */
6122 cp_parser_pre_parsed_nested_name_specifier (parser);
6123 /* If we originally encountered this nested-name-specifier
6124 with IS_DECLARATION set to false, we will not have
6125 resolved TYPENAME_TYPEs, so we must do so here. */
6126 if (is_declaration
6127 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6129 new_scope = resolve_typename_type (parser->scope,
6130 /*only_current_p=*/false);
6131 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6132 parser->scope = new_scope;
6134 success = true;
6135 continue;
6138 /* Spot cases that cannot be the beginning of a
6139 nested-name-specifier. On the second and subsequent times
6140 through the loop, we look for the `template' keyword. */
6141 if (success && token->keyword == RID_TEMPLATE)
6143 /* A template-id can start a nested-name-specifier. */
6144 else if (token->type == CPP_TEMPLATE_ID)
6146 /* DR 743: decltype can be used in a nested-name-specifier. */
6147 else if (token_is_decltype (token))
6149 else
6151 /* If the next token is not an identifier, then it is
6152 definitely not a type-name or namespace-name. */
6153 if (token->type != CPP_NAME)
6154 break;
6155 /* If the following token is neither a `<' (to begin a
6156 template-id), nor a `::', then we are not looking at a
6157 nested-name-specifier. */
6158 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6160 if (token->type == CPP_COLON
6161 && parser->colon_corrects_to_scope_p
6162 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6164 gcc_rich_location richloc (token->location);
6165 richloc.add_fixit_replace ("::");
6166 error_at (&richloc,
6167 "found %<:%> in nested-name-specifier, "
6168 "expected %<::%>");
6169 token->type = CPP_SCOPE;
6172 if (token->type != CPP_SCOPE
6173 && !cp_parser_nth_token_starts_template_argument_list_p
6174 (parser, 2))
6175 break;
6178 /* The nested-name-specifier is optional, so we parse
6179 tentatively. */
6180 cp_parser_parse_tentatively (parser);
6182 /* Look for the optional `template' keyword, if this isn't the
6183 first time through the loop. */
6184 if (success)
6185 template_keyword_p = cp_parser_optional_template_keyword (parser);
6187 /* Save the old scope since the name lookup we are about to do
6188 might destroy it. */
6189 old_scope = parser->scope;
6190 saved_qualifying_scope = parser->qualifying_scope;
6191 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6192 look up names in "X<T>::I" in order to determine that "Y" is
6193 a template. So, if we have a typename at this point, we make
6194 an effort to look through it. */
6195 if (is_declaration
6196 && !typename_keyword_p
6197 && parser->scope
6198 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6199 parser->scope = resolve_typename_type (parser->scope,
6200 /*only_current_p=*/false);
6201 /* Parse the qualifying entity. */
6202 new_scope
6203 = cp_parser_qualifying_entity (parser,
6204 typename_keyword_p,
6205 template_keyword_p,
6206 check_dependency_p,
6207 type_p,
6208 is_declaration);
6209 /* Look for the `::' token. */
6210 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6212 /* If we found what we wanted, we keep going; otherwise, we're
6213 done. */
6214 if (!cp_parser_parse_definitely (parser))
6216 bool error_p = false;
6218 /* Restore the OLD_SCOPE since it was valid before the
6219 failed attempt at finding the last
6220 class-or-namespace-name. */
6221 parser->scope = old_scope;
6222 parser->qualifying_scope = saved_qualifying_scope;
6224 /* If the next token is a decltype, and the one after that is a
6225 `::', then the decltype has failed to resolve to a class or
6226 enumeration type. Give this error even when parsing
6227 tentatively since it can't possibly be valid--and we're going
6228 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6229 won't get another chance.*/
6230 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6231 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6232 == CPP_SCOPE))
6234 token = cp_lexer_consume_token (parser->lexer);
6235 error_at (token->location, "decltype evaluates to %qT, "
6236 "which is not a class or enumeration type",
6237 token->u.tree_check_value->value);
6238 parser->scope = error_mark_node;
6239 error_p = true;
6240 /* As below. */
6241 success = true;
6242 cp_lexer_consume_token (parser->lexer);
6245 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6246 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6248 /* If we have a non-type template-id followed by ::, it can't
6249 possibly be valid. */
6250 token = cp_lexer_peek_token (parser->lexer);
6251 tree tid = token->u.tree_check_value->value;
6252 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6253 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6255 tree tmpl = NULL_TREE;
6256 if (is_overloaded_fn (tid))
6258 tree fns = get_fns (tid);
6259 if (OVL_SINGLE_P (fns))
6260 tmpl = OVL_FIRST (fns);
6261 error_at (token->location, "function template-id %qD "
6262 "in nested-name-specifier", tid);
6264 else
6266 /* Variable template. */
6267 tmpl = TREE_OPERAND (tid, 0);
6268 gcc_assert (variable_template_p (tmpl));
6269 error_at (token->location, "variable template-id %qD "
6270 "in nested-name-specifier", tid);
6272 if (tmpl)
6273 inform (DECL_SOURCE_LOCATION (tmpl),
6274 "%qD declared here", tmpl);
6276 parser->scope = error_mark_node;
6277 error_p = true;
6278 /* As below. */
6279 success = true;
6280 cp_lexer_consume_token (parser->lexer);
6281 cp_lexer_consume_token (parser->lexer);
6285 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6286 break;
6287 /* If the next token is an identifier, and the one after
6288 that is a `::', then any valid interpretation would have
6289 found a class-or-namespace-name. */
6290 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6291 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6292 == CPP_SCOPE)
6293 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6294 != CPP_COMPL))
6296 token = cp_lexer_consume_token (parser->lexer);
6297 if (!error_p)
6299 if (!token->error_reported)
6301 tree decl;
6302 tree ambiguous_decls;
6304 decl = cp_parser_lookup_name (parser, token->u.value,
6305 none_type,
6306 /*is_template=*/false,
6307 /*is_namespace=*/false,
6308 /*check_dependency=*/true,
6309 &ambiguous_decls,
6310 token->location);
6311 if (TREE_CODE (decl) == TEMPLATE_DECL)
6312 error_at (token->location,
6313 "%qD used without template parameters",
6314 decl);
6315 else if (ambiguous_decls)
6317 // cp_parser_lookup_name has the same diagnostic,
6318 // thus make sure to emit it at most once.
6319 if (cp_parser_uncommitted_to_tentative_parse_p
6320 (parser))
6322 error_at (token->location,
6323 "reference to %qD is ambiguous",
6324 token->u.value);
6325 print_candidates (ambiguous_decls);
6327 decl = error_mark_node;
6329 else
6331 if (cxx_dialect != cxx98)
6332 cp_parser_name_lookup_error
6333 (parser, token->u.value, decl, NLE_NOT_CXX98,
6334 token->location);
6335 else
6336 cp_parser_name_lookup_error
6337 (parser, token->u.value, decl, NLE_CXX98,
6338 token->location);
6341 parser->scope = error_mark_node;
6342 error_p = true;
6343 /* Treat this as a successful nested-name-specifier
6344 due to:
6346 [basic.lookup.qual]
6348 If the name found is not a class-name (clause
6349 _class_) or namespace-name (_namespace.def_), the
6350 program is ill-formed. */
6351 success = true;
6353 cp_lexer_consume_token (parser->lexer);
6355 break;
6357 /* We've found one valid nested-name-specifier. */
6358 success = true;
6359 /* Name lookup always gives us a DECL. */
6360 if (TREE_CODE (new_scope) == TYPE_DECL)
6361 new_scope = TREE_TYPE (new_scope);
6362 /* Uses of "template" must be followed by actual templates. */
6363 if (template_keyword_p
6364 && !(CLASS_TYPE_P (new_scope)
6365 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6366 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6367 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6368 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6369 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6370 == TEMPLATE_ID_EXPR)))
6371 permerror (input_location, TYPE_P (new_scope)
6372 ? G_("%qT is not a template")
6373 : G_("%qD is not a template"),
6374 new_scope);
6375 /* If it is a class scope, try to complete it; we are about to
6376 be looking up names inside the class. */
6377 if (TYPE_P (new_scope)
6378 /* Since checking types for dependency can be expensive,
6379 avoid doing it if the type is already complete. */
6380 && !COMPLETE_TYPE_P (new_scope)
6381 /* Do not try to complete dependent types. */
6382 && !dependent_type_p (new_scope))
6384 new_scope = complete_type (new_scope);
6385 /* If it is a typedef to current class, use the current
6386 class instead, as the typedef won't have any names inside
6387 it yet. */
6388 if (!COMPLETE_TYPE_P (new_scope)
6389 && currently_open_class (new_scope))
6390 new_scope = TYPE_MAIN_VARIANT (new_scope);
6392 /* Make sure we look in the right scope the next time through
6393 the loop. */
6394 parser->scope = new_scope;
6397 /* If parsing tentatively, replace the sequence of tokens that makes
6398 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6399 token. That way, should we re-parse the token stream, we will
6400 not have to repeat the effort required to do the parse, nor will
6401 we issue duplicate error messages. */
6402 if (success && start)
6404 cp_token *token;
6406 token = cp_lexer_token_at (parser->lexer, start);
6407 /* Reset the contents of the START token. */
6408 token->type = CPP_NESTED_NAME_SPECIFIER;
6409 /* Retrieve any deferred checks. Do not pop this access checks yet
6410 so the memory will not be reclaimed during token replacing below. */
6411 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6412 token->u.tree_check_value->value = parser->scope;
6413 token->u.tree_check_value->checks = get_deferred_access_checks ();
6414 token->u.tree_check_value->qualifying_scope =
6415 parser->qualifying_scope;
6416 token->keyword = RID_MAX;
6418 /* Purge all subsequent tokens. */
6419 cp_lexer_purge_tokens_after (parser->lexer, start);
6422 if (start)
6423 pop_to_parent_deferring_access_checks ();
6425 return success ? parser->scope : NULL_TREE;
6428 /* Parse a nested-name-specifier. See
6429 cp_parser_nested_name_specifier_opt for details. This function
6430 behaves identically, except that it will an issue an error if no
6431 nested-name-specifier is present. */
6433 static tree
6434 cp_parser_nested_name_specifier (cp_parser *parser,
6435 bool typename_keyword_p,
6436 bool check_dependency_p,
6437 bool type_p,
6438 bool is_declaration)
6440 tree scope;
6442 /* Look for the nested-name-specifier. */
6443 scope = cp_parser_nested_name_specifier_opt (parser,
6444 typename_keyword_p,
6445 check_dependency_p,
6446 type_p,
6447 is_declaration);
6448 /* If it was not present, issue an error message. */
6449 if (!scope)
6451 cp_parser_error (parser, "expected nested-name-specifier");
6452 parser->scope = NULL_TREE;
6455 return scope;
6458 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6459 this is either a class-name or a namespace-name (which corresponds
6460 to the class-or-namespace-name production in the grammar). For
6461 C++0x, it can also be a type-name that refers to an enumeration
6462 type or a simple-template-id.
6464 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6465 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6466 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6467 TYPE_P is TRUE iff the next name should be taken as a class-name,
6468 even the same name is declared to be another entity in the same
6469 scope.
6471 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6472 specified by the class-or-namespace-name. If neither is found the
6473 ERROR_MARK_NODE is returned. */
6475 static tree
6476 cp_parser_qualifying_entity (cp_parser *parser,
6477 bool typename_keyword_p,
6478 bool template_keyword_p,
6479 bool check_dependency_p,
6480 bool type_p,
6481 bool is_declaration)
6483 tree saved_scope;
6484 tree saved_qualifying_scope;
6485 tree saved_object_scope;
6486 tree scope;
6487 bool only_class_p;
6488 bool successful_parse_p;
6490 /* DR 743: decltype can appear in a nested-name-specifier. */
6491 if (cp_lexer_next_token_is_decltype (parser->lexer))
6493 scope = cp_parser_decltype (parser);
6494 if (TREE_CODE (scope) != ENUMERAL_TYPE
6495 && !MAYBE_CLASS_TYPE_P (scope))
6497 cp_parser_simulate_error (parser);
6498 return error_mark_node;
6500 if (TYPE_NAME (scope))
6501 scope = TYPE_NAME (scope);
6502 return scope;
6505 /* Before we try to parse the class-name, we must save away the
6506 current PARSER->SCOPE since cp_parser_class_name will destroy
6507 it. */
6508 saved_scope = parser->scope;
6509 saved_qualifying_scope = parser->qualifying_scope;
6510 saved_object_scope = parser->object_scope;
6511 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6512 there is no need to look for a namespace-name. */
6513 only_class_p = template_keyword_p
6514 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6515 if (!only_class_p)
6516 cp_parser_parse_tentatively (parser);
6517 scope = cp_parser_class_name (parser,
6518 typename_keyword_p,
6519 template_keyword_p,
6520 type_p ? class_type : none_type,
6521 check_dependency_p,
6522 /*class_head_p=*/false,
6523 is_declaration,
6524 /*enum_ok=*/cxx_dialect > cxx98);
6525 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6526 /* If that didn't work, try for a namespace-name. */
6527 if (!only_class_p && !successful_parse_p)
6529 /* Restore the saved scope. */
6530 parser->scope = saved_scope;
6531 parser->qualifying_scope = saved_qualifying_scope;
6532 parser->object_scope = saved_object_scope;
6533 /* If we are not looking at an identifier followed by the scope
6534 resolution operator, then this is not part of a
6535 nested-name-specifier. (Note that this function is only used
6536 to parse the components of a nested-name-specifier.) */
6537 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6538 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6539 return error_mark_node;
6540 scope = cp_parser_namespace_name (parser);
6543 return scope;
6546 /* Return true if we are looking at a compound-literal, false otherwise. */
6548 static bool
6549 cp_parser_compound_literal_p (cp_parser *parser)
6551 cp_lexer_save_tokens (parser->lexer);
6553 /* Skip tokens until the next token is a closing parenthesis.
6554 If we find the closing `)', and the next token is a `{', then
6555 we are looking at a compound-literal. */
6556 bool compound_literal_p
6557 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6558 /*consume_paren=*/true)
6559 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6561 /* Roll back the tokens we skipped. */
6562 cp_lexer_rollback_tokens (parser->lexer);
6564 return compound_literal_p;
6567 /* Parse a postfix-expression.
6569 postfix-expression:
6570 primary-expression
6571 postfix-expression [ expression ]
6572 postfix-expression ( expression-list [opt] )
6573 simple-type-specifier ( expression-list [opt] )
6574 typename :: [opt] nested-name-specifier identifier
6575 ( expression-list [opt] )
6576 typename :: [opt] nested-name-specifier template [opt] template-id
6577 ( expression-list [opt] )
6578 postfix-expression . template [opt] id-expression
6579 postfix-expression -> template [opt] id-expression
6580 postfix-expression . pseudo-destructor-name
6581 postfix-expression -> pseudo-destructor-name
6582 postfix-expression ++
6583 postfix-expression --
6584 dynamic_cast < type-id > ( expression )
6585 static_cast < type-id > ( expression )
6586 reinterpret_cast < type-id > ( expression )
6587 const_cast < type-id > ( expression )
6588 typeid ( expression )
6589 typeid ( type-id )
6591 GNU Extension:
6593 postfix-expression:
6594 ( type-id ) { initializer-list , [opt] }
6596 This extension is a GNU version of the C99 compound-literal
6597 construct. (The C99 grammar uses `type-name' instead of `type-id',
6598 but they are essentially the same concept.)
6600 If ADDRESS_P is true, the postfix expression is the operand of the
6601 `&' operator. CAST_P is true if this expression is the target of a
6602 cast.
6604 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6605 class member access expressions [expr.ref].
6607 Returns a representation of the expression. */
6609 static cp_expr
6610 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6611 bool member_access_only_p, bool decltype_p,
6612 cp_id_kind * pidk_return)
6614 cp_token *token;
6615 location_t loc;
6616 enum rid keyword;
6617 cp_id_kind idk = CP_ID_KIND_NONE;
6618 cp_expr postfix_expression = NULL_TREE;
6619 bool is_member_access = false;
6620 int saved_in_statement = -1;
6622 /* Peek at the next token. */
6623 token = cp_lexer_peek_token (parser->lexer);
6624 loc = token->location;
6625 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6627 /* Some of the productions are determined by keywords. */
6628 keyword = token->keyword;
6629 switch (keyword)
6631 case RID_DYNCAST:
6632 case RID_STATCAST:
6633 case RID_REINTCAST:
6634 case RID_CONSTCAST:
6636 tree type;
6637 cp_expr expression;
6638 const char *saved_message;
6639 bool saved_in_type_id_in_expr_p;
6641 /* All of these can be handled in the same way from the point
6642 of view of parsing. Begin by consuming the token
6643 identifying the cast. */
6644 cp_lexer_consume_token (parser->lexer);
6646 /* New types cannot be defined in the cast. */
6647 saved_message = parser->type_definition_forbidden_message;
6648 parser->type_definition_forbidden_message
6649 = G_("types may not be defined in casts");
6651 /* Look for the opening `<'. */
6652 cp_parser_require (parser, CPP_LESS, RT_LESS);
6653 /* Parse the type to which we are casting. */
6654 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6655 parser->in_type_id_in_expr_p = true;
6656 type = cp_parser_type_id (parser);
6657 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6658 /* Look for the closing `>'. */
6659 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6660 /* Restore the old message. */
6661 parser->type_definition_forbidden_message = saved_message;
6663 bool saved_greater_than_is_operator_p
6664 = parser->greater_than_is_operator_p;
6665 parser->greater_than_is_operator_p = true;
6667 /* And the expression which is being cast. */
6668 matching_parens parens;
6669 parens.require_open (parser);
6670 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6671 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6672 RT_CLOSE_PAREN);
6673 location_t end_loc = close_paren ?
6674 close_paren->location : UNKNOWN_LOCATION;
6676 parser->greater_than_is_operator_p
6677 = saved_greater_than_is_operator_p;
6679 /* Only type conversions to integral or enumeration types
6680 can be used in constant-expressions. */
6681 if (!cast_valid_in_integral_constant_expression_p (type)
6682 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6684 postfix_expression = error_mark_node;
6685 break;
6688 switch (keyword)
6690 case RID_DYNCAST:
6691 postfix_expression
6692 = build_dynamic_cast (type, expression, tf_warning_or_error);
6693 break;
6694 case RID_STATCAST:
6695 postfix_expression
6696 = build_static_cast (type, expression, tf_warning_or_error);
6697 break;
6698 case RID_REINTCAST:
6699 postfix_expression
6700 = build_reinterpret_cast (type, expression,
6701 tf_warning_or_error);
6702 break;
6703 case RID_CONSTCAST:
6704 postfix_expression
6705 = build_const_cast (type, expression, tf_warning_or_error);
6706 break;
6707 default:
6708 gcc_unreachable ();
6711 /* Construct a location e.g. :
6712 reinterpret_cast <int *> (expr)
6713 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6714 ranging from the start of the "*_cast" token to the final closing
6715 paren, with the caret at the start. */
6716 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6717 postfix_expression.set_location (cp_cast_loc);
6719 break;
6721 case RID_TYPEID:
6723 tree type;
6724 const char *saved_message;
6725 bool saved_in_type_id_in_expr_p;
6727 /* Consume the `typeid' token. */
6728 cp_lexer_consume_token (parser->lexer);
6729 /* Look for the `(' token. */
6730 matching_parens parens;
6731 parens.require_open (parser);
6732 /* Types cannot be defined in a `typeid' expression. */
6733 saved_message = parser->type_definition_forbidden_message;
6734 parser->type_definition_forbidden_message
6735 = G_("types may not be defined in a %<typeid%> expression");
6736 /* We can't be sure yet whether we're looking at a type-id or an
6737 expression. */
6738 cp_parser_parse_tentatively (parser);
6739 /* Try a type-id first. */
6740 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6741 parser->in_type_id_in_expr_p = true;
6742 type = cp_parser_type_id (parser);
6743 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6744 /* Look for the `)' token. Otherwise, we can't be sure that
6745 we're not looking at an expression: consider `typeid (int
6746 (3))', for example. */
6747 cp_token *close_paren = parens.require_close (parser);
6748 /* If all went well, simply lookup the type-id. */
6749 if (cp_parser_parse_definitely (parser))
6750 postfix_expression = get_typeid (type, tf_warning_or_error);
6751 /* Otherwise, fall back to the expression variant. */
6752 else
6754 tree expression;
6756 /* Look for an expression. */
6757 expression = cp_parser_expression (parser, & idk);
6758 /* Compute its typeid. */
6759 postfix_expression = build_typeid (expression, tf_warning_or_error);
6760 /* Look for the `)' token. */
6761 close_paren = parens.require_close (parser);
6763 /* Restore the saved message. */
6764 parser->type_definition_forbidden_message = saved_message;
6765 /* `typeid' may not appear in an integral constant expression. */
6766 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6767 postfix_expression = error_mark_node;
6769 /* Construct a location e.g. :
6770 typeid (expr)
6771 ^~~~~~~~~~~~~
6772 ranging from the start of the "typeid" token to the final closing
6773 paren, with the caret at the start. */
6774 if (close_paren)
6776 location_t typeid_loc
6777 = make_location (start_loc, start_loc, close_paren->location);
6778 postfix_expression.set_location (typeid_loc);
6781 break;
6783 case RID_TYPENAME:
6785 tree type;
6786 /* The syntax permitted here is the same permitted for an
6787 elaborated-type-specifier. */
6788 ++parser->prevent_constrained_type_specifiers;
6789 type = cp_parser_elaborated_type_specifier (parser,
6790 /*is_friend=*/false,
6791 /*is_declaration=*/false);
6792 --parser->prevent_constrained_type_specifiers;
6793 postfix_expression = cp_parser_functional_cast (parser, type);
6795 break;
6797 case RID_CILK_SPAWN:
6799 location_t cilk_spawn_loc
6800 = cp_lexer_peek_token (parser->lexer)->location;
6801 cp_lexer_consume_token (parser->lexer);
6802 token = cp_lexer_peek_token (parser->lexer);
6803 if (token->type == CPP_SEMICOLON)
6805 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6806 "an expression");
6807 postfix_expression = error_mark_node;
6808 break;
6810 else if (!current_function_decl)
6812 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6813 "inside a function");
6814 postfix_expression = error_mark_node;
6815 break;
6817 else
6819 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6820 saved_in_statement = parser->in_statement;
6821 parser->in_statement |= IN_CILK_SPAWN;
6823 cfun->calls_cilk_spawn = 1;
6824 postfix_expression =
6825 cp_parser_postfix_expression (parser, false, false,
6826 false, false, &idk);
6827 if (!flag_cilkplus)
6829 error_at (token->location, "-fcilkplus must be enabled to use"
6830 " %<_Cilk_spawn%>");
6831 cfun->calls_cilk_spawn = 0;
6833 else if (saved_in_statement & IN_CILK_SPAWN)
6835 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6836 "are not permitted");
6837 postfix_expression = error_mark_node;
6838 cfun->calls_cilk_spawn = 0;
6840 else
6842 location_t loc = postfix_expression.get_location ();
6843 postfix_expression = build_cilk_spawn (token->location,
6844 postfix_expression);
6845 /* Build a location of the form:
6846 _Cilk_spawn expr
6847 ~~~~~~~~~~~~^~~~
6848 with caret at the expr, ranging from the start of the
6849 _Cilk_spawn token to the end of the expression. */
6850 location_t combined_loc =
6851 make_location (loc, cilk_spawn_loc, get_finish (loc));
6852 postfix_expression.set_location (combined_loc);
6853 if (postfix_expression != error_mark_node)
6854 SET_EXPR_LOCATION (postfix_expression, input_location);
6855 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6857 break;
6860 case RID_ADDRESSOF:
6861 case RID_BUILTIN_SHUFFLE:
6862 case RID_BUILTIN_LAUNDER:
6864 vec<tree, va_gc> *vec;
6865 unsigned int i;
6866 tree p;
6868 cp_lexer_consume_token (parser->lexer);
6869 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6870 /*cast_p=*/false, /*allow_expansion_p=*/true,
6871 /*non_constant_p=*/NULL);
6872 if (vec == NULL)
6874 postfix_expression = error_mark_node;
6875 break;
6878 FOR_EACH_VEC_ELT (*vec, i, p)
6879 mark_exp_read (p);
6881 switch (keyword)
6883 case RID_ADDRESSOF:
6884 if (vec->length () == 1)
6885 postfix_expression
6886 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6887 else
6889 error_at (loc, "wrong number of arguments to "
6890 "%<__builtin_addressof%>");
6891 postfix_expression = error_mark_node;
6893 break;
6895 case RID_BUILTIN_LAUNDER:
6896 if (vec->length () == 1)
6897 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6898 tf_warning_or_error);
6899 else
6901 error_at (loc, "wrong number of arguments to "
6902 "%<__builtin_launder%>");
6903 postfix_expression = error_mark_node;
6905 break;
6907 case RID_BUILTIN_SHUFFLE:
6908 if (vec->length () == 2)
6909 postfix_expression
6910 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6911 (*vec)[1], tf_warning_or_error);
6912 else if (vec->length () == 3)
6913 postfix_expression
6914 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6915 (*vec)[2], tf_warning_or_error);
6916 else
6918 error_at (loc, "wrong number of arguments to "
6919 "%<__builtin_shuffle%>");
6920 postfix_expression = error_mark_node;
6922 break;
6924 default:
6925 gcc_unreachable ();
6927 break;
6930 default:
6932 tree type;
6934 /* If the next thing is a simple-type-specifier, we may be
6935 looking at a functional cast. We could also be looking at
6936 an id-expression. So, we try the functional cast, and if
6937 that doesn't work we fall back to the primary-expression. */
6938 cp_parser_parse_tentatively (parser);
6939 /* Look for the simple-type-specifier. */
6940 ++parser->prevent_constrained_type_specifiers;
6941 type = cp_parser_simple_type_specifier (parser,
6942 /*decl_specs=*/NULL,
6943 CP_PARSER_FLAGS_NONE);
6944 --parser->prevent_constrained_type_specifiers;
6945 /* Parse the cast itself. */
6946 if (!cp_parser_error_occurred (parser))
6947 postfix_expression
6948 = cp_parser_functional_cast (parser, type);
6949 /* If that worked, we're done. */
6950 if (cp_parser_parse_definitely (parser))
6951 break;
6953 /* If the functional-cast didn't work out, try a
6954 compound-literal. */
6955 if (cp_parser_allow_gnu_extensions_p (parser)
6956 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6958 cp_expr initializer = NULL_TREE;
6960 cp_parser_parse_tentatively (parser);
6962 matching_parens parens;
6963 parens.consume_open (parser);
6965 /* Avoid calling cp_parser_type_id pointlessly, see comment
6966 in cp_parser_cast_expression about c++/29234. */
6967 if (!cp_parser_compound_literal_p (parser))
6968 cp_parser_simulate_error (parser);
6969 else
6971 /* Parse the type. */
6972 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6973 parser->in_type_id_in_expr_p = true;
6974 type = cp_parser_type_id (parser);
6975 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6976 parens.require_close (parser);
6979 /* If things aren't going well, there's no need to
6980 keep going. */
6981 if (!cp_parser_error_occurred (parser))
6983 bool non_constant_p;
6984 /* Parse the brace-enclosed initializer list. */
6985 initializer = cp_parser_braced_list (parser,
6986 &non_constant_p);
6988 /* If that worked, we're definitely looking at a
6989 compound-literal expression. */
6990 if (cp_parser_parse_definitely (parser))
6992 /* Warn the user that a compound literal is not
6993 allowed in standard C++. */
6994 pedwarn (input_location, OPT_Wpedantic,
6995 "ISO C++ forbids compound-literals");
6996 /* For simplicity, we disallow compound literals in
6997 constant-expressions. We could
6998 allow compound literals of integer type, whose
6999 initializer was a constant, in constant
7000 expressions. Permitting that usage, as a further
7001 extension, would not change the meaning of any
7002 currently accepted programs. (Of course, as
7003 compound literals are not part of ISO C++, the
7004 standard has nothing to say.) */
7005 if (cp_parser_non_integral_constant_expression (parser,
7006 NIC_NCC))
7008 postfix_expression = error_mark_node;
7009 break;
7011 /* Form the representation of the compound-literal. */
7012 postfix_expression
7013 = finish_compound_literal (type, initializer,
7014 tf_warning_or_error, fcl_c99);
7015 postfix_expression.set_location (initializer.get_location ());
7016 break;
7020 /* It must be a primary-expression. */
7021 postfix_expression
7022 = cp_parser_primary_expression (parser, address_p, cast_p,
7023 /*template_arg_p=*/false,
7024 decltype_p,
7025 &idk);
7027 break;
7030 /* Note that we don't need to worry about calling build_cplus_new on a
7031 class-valued CALL_EXPR in decltype when it isn't the end of the
7032 postfix-expression; unary_complex_lvalue will take care of that for
7033 all these cases. */
7035 /* Keep looping until the postfix-expression is complete. */
7036 while (true)
7038 if (idk == CP_ID_KIND_UNQUALIFIED
7039 && identifier_p (postfix_expression)
7040 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7041 /* It is not a Koenig lookup function call. */
7042 postfix_expression
7043 = unqualified_name_lookup_error (postfix_expression);
7045 /* Peek at the next token. */
7046 token = cp_lexer_peek_token (parser->lexer);
7048 switch (token->type)
7050 case CPP_OPEN_SQUARE:
7051 if (cp_next_tokens_can_be_std_attribute_p (parser))
7053 cp_parser_error (parser,
7054 "two consecutive %<[%> shall "
7055 "only introduce an attribute");
7056 return error_mark_node;
7058 postfix_expression
7059 = cp_parser_postfix_open_square_expression (parser,
7060 postfix_expression,
7061 false,
7062 decltype_p);
7063 postfix_expression.set_range (start_loc,
7064 postfix_expression.get_location ());
7066 idk = CP_ID_KIND_NONE;
7067 is_member_access = false;
7068 break;
7070 case CPP_OPEN_PAREN:
7071 /* postfix-expression ( expression-list [opt] ) */
7073 bool koenig_p;
7074 bool is_builtin_constant_p;
7075 bool saved_integral_constant_expression_p = false;
7076 bool saved_non_integral_constant_expression_p = false;
7077 tsubst_flags_t complain = complain_flags (decltype_p);
7078 vec<tree, va_gc> *args;
7079 location_t close_paren_loc = UNKNOWN_LOCATION;
7081 is_member_access = false;
7083 is_builtin_constant_p
7084 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7085 if (is_builtin_constant_p)
7087 /* The whole point of __builtin_constant_p is to allow
7088 non-constant expressions to appear as arguments. */
7089 saved_integral_constant_expression_p
7090 = parser->integral_constant_expression_p;
7091 saved_non_integral_constant_expression_p
7092 = parser->non_integral_constant_expression_p;
7093 parser->integral_constant_expression_p = false;
7095 args = (cp_parser_parenthesized_expression_list
7096 (parser, non_attr,
7097 /*cast_p=*/false, /*allow_expansion_p=*/true,
7098 /*non_constant_p=*/NULL,
7099 /*close_paren_loc=*/&close_paren_loc));
7100 if (is_builtin_constant_p)
7102 parser->integral_constant_expression_p
7103 = saved_integral_constant_expression_p;
7104 parser->non_integral_constant_expression_p
7105 = saved_non_integral_constant_expression_p;
7108 if (args == NULL)
7110 postfix_expression = error_mark_node;
7111 break;
7114 /* Function calls are not permitted in
7115 constant-expressions. */
7116 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7117 && cp_parser_non_integral_constant_expression (parser,
7118 NIC_FUNC_CALL))
7120 postfix_expression = error_mark_node;
7121 release_tree_vector (args);
7122 break;
7125 koenig_p = false;
7126 if (idk == CP_ID_KIND_UNQUALIFIED
7127 || idk == CP_ID_KIND_TEMPLATE_ID)
7129 if (identifier_p (postfix_expression))
7131 if (!args->is_empty ())
7133 koenig_p = true;
7134 if (!any_type_dependent_arguments_p (args))
7135 postfix_expression
7136 = perform_koenig_lookup (postfix_expression, args,
7137 complain);
7139 else
7140 postfix_expression
7141 = unqualified_fn_lookup_error (postfix_expression);
7143 /* We do not perform argument-dependent lookup if
7144 normal lookup finds a non-function, in accordance
7145 with the expected resolution of DR 218. */
7146 else if (!args->is_empty ()
7147 && is_overloaded_fn (postfix_expression))
7149 tree fn = get_first_fn (postfix_expression);
7150 fn = STRIP_TEMPLATE (fn);
7152 /* Do not do argument dependent lookup if regular
7153 lookup finds a member function or a block-scope
7154 function declaration. [basic.lookup.argdep]/3 */
7155 if (!DECL_FUNCTION_MEMBER_P (fn)
7156 && !DECL_LOCAL_FUNCTION_P (fn))
7158 koenig_p = true;
7159 if (!any_type_dependent_arguments_p (args))
7160 postfix_expression
7161 = perform_koenig_lookup (postfix_expression, args,
7162 complain);
7167 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7168 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7169 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7170 && vec_safe_length (args) == 3)
7172 tree arg0 = (*args)[0];
7173 tree arg1 = (*args)[1];
7174 tree arg2 = (*args)[2];
7175 int literal_mask = ((!!integer_zerop (arg1) << 1)
7176 | (!!integer_zerop (arg2) << 2));
7177 if (TREE_CODE (arg2) == CONST_DECL)
7178 arg2 = DECL_INITIAL (arg2);
7179 warn_for_memset (input_location, arg0, arg2, literal_mask);
7182 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7184 tree instance = TREE_OPERAND (postfix_expression, 0);
7185 tree fn = TREE_OPERAND (postfix_expression, 1);
7187 if (processing_template_decl
7188 && (type_dependent_object_expression_p (instance)
7189 || (!BASELINK_P (fn)
7190 && TREE_CODE (fn) != FIELD_DECL)
7191 || type_dependent_expression_p (fn)
7192 || any_type_dependent_arguments_p (args)))
7194 maybe_generic_this_capture (instance, fn);
7195 postfix_expression
7196 = build_min_nt_call_vec (postfix_expression, args);
7197 release_tree_vector (args);
7198 break;
7201 if (BASELINK_P (fn))
7203 postfix_expression
7204 = (build_new_method_call
7205 (instance, fn, &args, NULL_TREE,
7206 (idk == CP_ID_KIND_QUALIFIED
7207 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7208 : LOOKUP_NORMAL),
7209 /*fn_p=*/NULL,
7210 complain));
7212 else
7213 postfix_expression
7214 = finish_call_expr (postfix_expression, &args,
7215 /*disallow_virtual=*/false,
7216 /*koenig_p=*/false,
7217 complain);
7219 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7220 || TREE_CODE (postfix_expression) == MEMBER_REF
7221 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7222 postfix_expression = (build_offset_ref_call_from_tree
7223 (postfix_expression, &args,
7224 complain));
7225 else if (idk == CP_ID_KIND_QUALIFIED)
7226 /* A call to a static class member, or a namespace-scope
7227 function. */
7228 postfix_expression
7229 = finish_call_expr (postfix_expression, &args,
7230 /*disallow_virtual=*/true,
7231 koenig_p,
7232 complain);
7233 else
7234 /* All other function calls. */
7235 postfix_expression
7236 = finish_call_expr (postfix_expression, &args,
7237 /*disallow_virtual=*/false,
7238 koenig_p,
7239 complain);
7241 if (close_paren_loc != UNKNOWN_LOCATION)
7243 location_t combined_loc = make_location (token->location,
7244 start_loc,
7245 close_paren_loc);
7246 postfix_expression.set_location (combined_loc);
7249 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7250 idk = CP_ID_KIND_NONE;
7252 release_tree_vector (args);
7254 break;
7256 case CPP_DOT:
7257 case CPP_DEREF:
7258 /* postfix-expression . template [opt] id-expression
7259 postfix-expression . pseudo-destructor-name
7260 postfix-expression -> template [opt] id-expression
7261 postfix-expression -> pseudo-destructor-name */
7263 /* Consume the `.' or `->' operator. */
7264 cp_lexer_consume_token (parser->lexer);
7266 postfix_expression
7267 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7268 postfix_expression,
7269 false, &idk, loc);
7271 is_member_access = true;
7272 break;
7274 case CPP_PLUS_PLUS:
7275 /* postfix-expression ++ */
7276 /* Consume the `++' token. */
7277 cp_lexer_consume_token (parser->lexer);
7278 /* Generate a representation for the complete expression. */
7279 postfix_expression
7280 = finish_increment_expr (postfix_expression,
7281 POSTINCREMENT_EXPR);
7282 /* Increments may not appear in constant-expressions. */
7283 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7284 postfix_expression = error_mark_node;
7285 idk = CP_ID_KIND_NONE;
7286 is_member_access = false;
7287 break;
7289 case CPP_MINUS_MINUS:
7290 /* postfix-expression -- */
7291 /* Consume the `--' token. */
7292 cp_lexer_consume_token (parser->lexer);
7293 /* Generate a representation for the complete expression. */
7294 postfix_expression
7295 = finish_increment_expr (postfix_expression,
7296 POSTDECREMENT_EXPR);
7297 /* Decrements may not appear in constant-expressions. */
7298 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7299 postfix_expression = error_mark_node;
7300 idk = CP_ID_KIND_NONE;
7301 is_member_access = false;
7302 break;
7304 default:
7305 if (pidk_return != NULL)
7306 * pidk_return = idk;
7307 if (member_access_only_p)
7308 return is_member_access
7309 ? postfix_expression
7310 : cp_expr (error_mark_node);
7311 else
7312 return postfix_expression;
7316 /* We should never get here. */
7317 gcc_unreachable ();
7318 return error_mark_node;
7321 /* This function parses Cilk Plus array notations. If a normal array expr. is
7322 parsed then the array index is passed back to the caller through *INIT_INDEX
7323 and the function returns a NULL_TREE. If array notation expr. is parsed,
7324 then *INIT_INDEX is ignored by the caller and the function returns
7325 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7326 error_mark_node. */
7328 static tree
7329 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7330 tree array_value)
7332 cp_token *token = NULL;
7333 tree length_index, stride = NULL_TREE, value_tree, array_type;
7334 if (!array_value || array_value == error_mark_node)
7336 cp_parser_skip_to_end_of_statement (parser);
7337 return error_mark_node;
7340 array_type = TREE_TYPE (array_value);
7342 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7343 parser->colon_corrects_to_scope_p = false;
7344 token = cp_lexer_peek_token (parser->lexer);
7346 if (!token)
7348 cp_parser_error (parser, "expected %<:%> or numeral");
7349 return error_mark_node;
7351 else if (token->type == CPP_COLON)
7353 /* Consume the ':'. */
7354 cp_lexer_consume_token (parser->lexer);
7356 /* If we are here, then we have a case like this A[:]. */
7357 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7359 cp_parser_error (parser, "expected %<]%>");
7360 cp_parser_skip_to_end_of_statement (parser);
7361 return error_mark_node;
7363 *init_index = NULL_TREE;
7364 stride = NULL_TREE;
7365 length_index = NULL_TREE;
7367 else
7369 /* If we are here, then there are three valid possibilities:
7370 1. ARRAY [ EXP ]
7371 2. ARRAY [ EXP : EXP ]
7372 3. ARRAY [ EXP : EXP : EXP ] */
7374 *init_index = cp_parser_expression (parser);
7375 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7377 /* This indicates that we have a normal array expression. */
7378 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7379 return NULL_TREE;
7382 /* Consume the ':'. */
7383 cp_lexer_consume_token (parser->lexer);
7384 length_index = cp_parser_expression (parser);
7385 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7387 cp_lexer_consume_token (parser->lexer);
7388 stride = cp_parser_expression (parser);
7391 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7393 if (*init_index == error_mark_node || length_index == error_mark_node
7394 || stride == error_mark_node || array_type == error_mark_node)
7396 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7397 cp_lexer_consume_token (parser->lexer);
7398 return error_mark_node;
7400 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7402 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7403 length_index, stride, array_type);
7404 return value_tree;
7407 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7408 by cp_parser_builtin_offsetof. We're looking for
7410 postfix-expression [ expression ]
7411 postfix-expression [ braced-init-list ] (C++11)
7413 FOR_OFFSETOF is set if we're being called in that context, which
7414 changes how we deal with integer constant expressions. */
7416 static tree
7417 cp_parser_postfix_open_square_expression (cp_parser *parser,
7418 tree postfix_expression,
7419 bool for_offsetof,
7420 bool decltype_p)
7422 tree index = NULL_TREE;
7423 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7424 bool saved_greater_than_is_operator_p;
7426 /* Consume the `[' token. */
7427 cp_lexer_consume_token (parser->lexer);
7429 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7430 parser->greater_than_is_operator_p = true;
7432 /* Parse the index expression. */
7433 /* ??? For offsetof, there is a question of what to allow here. If
7434 offsetof is not being used in an integral constant expression context,
7435 then we *could* get the right answer by computing the value at runtime.
7436 If we are in an integral constant expression context, then we might
7437 could accept any constant expression; hard to say without analysis.
7438 Rather than open the barn door too wide right away, allow only integer
7439 constant expressions here. */
7440 if (for_offsetof)
7441 index = cp_parser_constant_expression (parser);
7442 else
7444 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7446 bool expr_nonconst_p;
7447 cp_lexer_set_source_position (parser->lexer);
7448 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7449 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7450 if (flag_cilkplus
7451 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7453 error_at (cp_lexer_peek_token (parser->lexer)->location,
7454 "braced list index is not allowed with array "
7455 "notation");
7456 cp_parser_skip_to_end_of_statement (parser);
7457 return error_mark_node;
7460 else if (flag_cilkplus)
7462 /* Here are have these two options:
7463 ARRAY[EXP : EXP] - Array notation expr with default
7464 stride of 1.
7465 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7466 stride. */
7467 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7468 postfix_expression);
7469 if (an_exp)
7470 return an_exp;
7472 else
7473 index = cp_parser_expression (parser);
7476 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7478 /* Look for the closing `]'. */
7479 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7481 /* Build the ARRAY_REF. */
7482 postfix_expression = grok_array_decl (loc, postfix_expression,
7483 index, decltype_p);
7485 /* When not doing offsetof, array references are not permitted in
7486 constant-expressions. */
7487 if (!for_offsetof
7488 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7489 postfix_expression = error_mark_node;
7491 return postfix_expression;
7494 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7495 by cp_parser_builtin_offsetof. We're looking for
7497 postfix-expression . template [opt] id-expression
7498 postfix-expression . pseudo-destructor-name
7499 postfix-expression -> template [opt] id-expression
7500 postfix-expression -> pseudo-destructor-name
7502 FOR_OFFSETOF is set if we're being called in that context. That sorta
7503 limits what of the above we'll actually accept, but nevermind.
7504 TOKEN_TYPE is the "." or "->" token, which will already have been
7505 removed from the stream. */
7507 static tree
7508 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7509 enum cpp_ttype token_type,
7510 cp_expr postfix_expression,
7511 bool for_offsetof, cp_id_kind *idk,
7512 location_t location)
7514 tree name;
7515 bool dependent_p;
7516 bool pseudo_destructor_p;
7517 tree scope = NULL_TREE;
7518 location_t start_loc = postfix_expression.get_start ();
7520 /* If this is a `->' operator, dereference the pointer. */
7521 if (token_type == CPP_DEREF)
7522 postfix_expression = build_x_arrow (location, postfix_expression,
7523 tf_warning_or_error);
7524 /* Check to see whether or not the expression is type-dependent and
7525 not the current instantiation. */
7526 dependent_p = type_dependent_object_expression_p (postfix_expression);
7527 /* The identifier following the `->' or `.' is not qualified. */
7528 parser->scope = NULL_TREE;
7529 parser->qualifying_scope = NULL_TREE;
7530 parser->object_scope = NULL_TREE;
7531 *idk = CP_ID_KIND_NONE;
7533 /* Enter the scope corresponding to the type of the object
7534 given by the POSTFIX_EXPRESSION. */
7535 if (!dependent_p)
7537 scope = TREE_TYPE (postfix_expression);
7538 /* According to the standard, no expression should ever have
7539 reference type. Unfortunately, we do not currently match
7540 the standard in this respect in that our internal representation
7541 of an expression may have reference type even when the standard
7542 says it does not. Therefore, we have to manually obtain the
7543 underlying type here. */
7544 scope = non_reference (scope);
7545 /* The type of the POSTFIX_EXPRESSION must be complete. */
7546 /* Unlike the object expression in other contexts, *this is not
7547 required to be of complete type for purposes of class member
7548 access (5.2.5) outside the member function body. */
7549 if (postfix_expression != current_class_ref
7550 && scope != error_mark_node
7551 && !(processing_template_decl
7552 && current_class_type
7553 && (same_type_ignoring_top_level_qualifiers_p
7554 (scope, current_class_type))))
7556 scope = complete_type (scope);
7557 if (!COMPLETE_TYPE_P (scope)
7558 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7559 && EXPR_P (postfix_expression))
7561 /* In a template, be permissive by treating an object expression
7562 of incomplete type as dependent (after a pedwarn). */
7563 diagnostic_t kind = (processing_template_decl
7564 && MAYBE_CLASS_TYPE_P (scope)
7565 ? DK_PEDWARN
7566 : DK_ERROR);
7567 cxx_incomplete_type_diagnostic
7568 (location_of (postfix_expression),
7569 postfix_expression, scope, kind);
7570 if (!MAYBE_CLASS_TYPE_P (scope))
7571 return error_mark_node;
7572 if (processing_template_decl)
7574 dependent_p = true;
7575 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7580 if (!dependent_p)
7582 /* Let the name lookup machinery know that we are processing a
7583 class member access expression. */
7584 parser->context->object_type = scope;
7585 /* If something went wrong, we want to be able to discern that case,
7586 as opposed to the case where there was no SCOPE due to the type
7587 of expression being dependent. */
7588 if (!scope)
7589 scope = error_mark_node;
7590 /* If the SCOPE was erroneous, make the various semantic analysis
7591 functions exit quickly -- and without issuing additional error
7592 messages. */
7593 if (scope == error_mark_node)
7594 postfix_expression = error_mark_node;
7598 if (dependent_p)
7599 /* Tell cp_parser_lookup_name that there was an object, even though it's
7600 type-dependent. */
7601 parser->context->object_type = unknown_type_node;
7603 /* Assume this expression is not a pseudo-destructor access. */
7604 pseudo_destructor_p = false;
7606 /* If the SCOPE is a scalar type, then, if this is a valid program,
7607 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7608 is type dependent, it can be pseudo-destructor-name or something else.
7609 Try to parse it as pseudo-destructor-name first. */
7610 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7612 tree s;
7613 tree type;
7615 cp_parser_parse_tentatively (parser);
7616 /* Parse the pseudo-destructor-name. */
7617 s = NULL_TREE;
7618 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7619 &s, &type);
7620 if (dependent_p
7621 && (cp_parser_error_occurred (parser)
7622 || !SCALAR_TYPE_P (type)))
7623 cp_parser_abort_tentative_parse (parser);
7624 else if (cp_parser_parse_definitely (parser))
7626 pseudo_destructor_p = true;
7627 postfix_expression
7628 = finish_pseudo_destructor_expr (postfix_expression,
7629 s, type, location);
7633 if (!pseudo_destructor_p)
7635 /* If the SCOPE is not a scalar type, we are looking at an
7636 ordinary class member access expression, rather than a
7637 pseudo-destructor-name. */
7638 bool template_p;
7639 cp_token *token = cp_lexer_peek_token (parser->lexer);
7640 /* Parse the id-expression. */
7641 name = (cp_parser_id_expression
7642 (parser,
7643 cp_parser_optional_template_keyword (parser),
7644 /*check_dependency_p=*/true,
7645 &template_p,
7646 /*declarator_p=*/false,
7647 /*optional_p=*/false));
7648 /* In general, build a SCOPE_REF if the member name is qualified.
7649 However, if the name was not dependent and has already been
7650 resolved; there is no need to build the SCOPE_REF. For example;
7652 struct X { void f(); };
7653 template <typename T> void f(T* t) { t->X::f(); }
7655 Even though "t" is dependent, "X::f" is not and has been resolved
7656 to a BASELINK; there is no need to include scope information. */
7658 /* But we do need to remember that there was an explicit scope for
7659 virtual function calls. */
7660 if (parser->scope)
7661 *idk = CP_ID_KIND_QUALIFIED;
7663 /* If the name is a template-id that names a type, we will get a
7664 TYPE_DECL here. That is invalid code. */
7665 if (TREE_CODE (name) == TYPE_DECL)
7667 error_at (token->location, "invalid use of %qD", name);
7668 postfix_expression = error_mark_node;
7670 else
7672 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7674 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7676 error_at (token->location, "%<%D::%D%> is not a class member",
7677 parser->scope, name);
7678 postfix_expression = error_mark_node;
7680 else
7681 name = build_qualified_name (/*type=*/NULL_TREE,
7682 parser->scope,
7683 name,
7684 template_p);
7685 parser->scope = NULL_TREE;
7686 parser->qualifying_scope = NULL_TREE;
7687 parser->object_scope = NULL_TREE;
7689 if (parser->scope && name && BASELINK_P (name))
7690 adjust_result_of_qualified_name_lookup
7691 (name, parser->scope, scope);
7692 postfix_expression
7693 = finish_class_member_access_expr (postfix_expression, name,
7694 template_p,
7695 tf_warning_or_error);
7696 /* Build a location e.g.:
7697 ptr->access_expr
7698 ~~~^~~~~~~~~~~~~
7699 where the caret is at the deref token, ranging from
7700 the start of postfix_expression to the end of the access expr. */
7701 location_t end_loc
7702 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7703 location_t combined_loc
7704 = make_location (input_location, start_loc, end_loc);
7705 protected_set_expr_location (postfix_expression, combined_loc);
7709 /* We no longer need to look up names in the scope of the object on
7710 the left-hand side of the `.' or `->' operator. */
7711 parser->context->object_type = NULL_TREE;
7713 /* Outside of offsetof, these operators may not appear in
7714 constant-expressions. */
7715 if (!for_offsetof
7716 && (cp_parser_non_integral_constant_expression
7717 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7718 postfix_expression = error_mark_node;
7720 return postfix_expression;
7723 /* Parse a parenthesized expression-list.
7725 expression-list:
7726 assignment-expression
7727 expression-list, assignment-expression
7729 attribute-list:
7730 expression-list
7731 identifier
7732 identifier, expression-list
7734 CAST_P is true if this expression is the target of a cast.
7736 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7737 argument pack.
7739 Returns a vector of trees. Each element is a representation of an
7740 assignment-expression. NULL is returned if the ( and or ) are
7741 missing. An empty, but allocated, vector is returned on no
7742 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7743 if we are parsing an attribute list for an attribute that wants a
7744 plain identifier argument, normal_attr for an attribute that wants
7745 an expression, or non_attr if we aren't parsing an attribute list. If
7746 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7747 not all of the expressions in the list were constant.
7748 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7749 will be written to with the location of the closing parenthesis. If
7750 an error occurs, it may or may not be written to. */
7752 static vec<tree, va_gc> *
7753 cp_parser_parenthesized_expression_list (cp_parser* parser,
7754 int is_attribute_list,
7755 bool cast_p,
7756 bool allow_expansion_p,
7757 bool *non_constant_p,
7758 location_t *close_paren_loc)
7760 vec<tree, va_gc> *expression_list;
7761 bool fold_expr_p = is_attribute_list != non_attr;
7762 tree identifier = NULL_TREE;
7763 bool saved_greater_than_is_operator_p;
7765 /* Assume all the expressions will be constant. */
7766 if (non_constant_p)
7767 *non_constant_p = false;
7769 matching_parens parens;
7770 if (!parens.require_open (parser))
7771 return NULL;
7773 expression_list = make_tree_vector ();
7775 /* Within a parenthesized expression, a `>' token is always
7776 the greater-than operator. */
7777 saved_greater_than_is_operator_p
7778 = parser->greater_than_is_operator_p;
7779 parser->greater_than_is_operator_p = true;
7781 /* Consume expressions until there are no more. */
7782 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7783 while (true)
7785 tree expr;
7787 /* At the beginning of attribute lists, check to see if the
7788 next token is an identifier. */
7789 if (is_attribute_list == id_attr
7790 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7792 cp_token *token;
7794 /* Consume the identifier. */
7795 token = cp_lexer_consume_token (parser->lexer);
7796 /* Save the identifier. */
7797 identifier = token->u.value;
7799 else
7801 bool expr_non_constant_p;
7803 /* Parse the next assignment-expression. */
7804 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7806 /* A braced-init-list. */
7807 cp_lexer_set_source_position (parser->lexer);
7808 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7809 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7810 if (non_constant_p && expr_non_constant_p)
7811 *non_constant_p = true;
7813 else if (non_constant_p)
7815 expr = (cp_parser_constant_expression
7816 (parser, /*allow_non_constant_p=*/true,
7817 &expr_non_constant_p));
7818 if (expr_non_constant_p)
7819 *non_constant_p = true;
7821 else
7822 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7823 cast_p);
7825 if (fold_expr_p)
7826 expr = instantiate_non_dependent_expr (expr);
7828 /* If we have an ellipsis, then this is an expression
7829 expansion. */
7830 if (allow_expansion_p
7831 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7833 /* Consume the `...'. */
7834 cp_lexer_consume_token (parser->lexer);
7836 /* Build the argument pack. */
7837 expr = make_pack_expansion (expr);
7840 /* Add it to the list. We add error_mark_node
7841 expressions to the list, so that we can still tell if
7842 the correct form for a parenthesized expression-list
7843 is found. That gives better errors. */
7844 vec_safe_push (expression_list, expr);
7846 if (expr == error_mark_node)
7847 goto skip_comma;
7850 /* After the first item, attribute lists look the same as
7851 expression lists. */
7852 is_attribute_list = non_attr;
7854 get_comma:;
7855 /* If the next token isn't a `,', then we are done. */
7856 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7857 break;
7859 /* Otherwise, consume the `,' and keep going. */
7860 cp_lexer_consume_token (parser->lexer);
7863 if (close_paren_loc)
7864 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7866 if (!parens.require_close (parser))
7868 int ending;
7870 skip_comma:;
7871 /* We try and resync to an unnested comma, as that will give the
7872 user better diagnostics. */
7873 ending = cp_parser_skip_to_closing_parenthesis (parser,
7874 /*recovering=*/true,
7875 /*or_comma=*/true,
7876 /*consume_paren=*/true);
7877 if (ending < 0)
7878 goto get_comma;
7879 if (!ending)
7881 parser->greater_than_is_operator_p
7882 = saved_greater_than_is_operator_p;
7883 return NULL;
7887 parser->greater_than_is_operator_p
7888 = saved_greater_than_is_operator_p;
7890 if (identifier)
7891 vec_safe_insert (expression_list, 0, identifier);
7893 return expression_list;
7896 /* Parse a pseudo-destructor-name.
7898 pseudo-destructor-name:
7899 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7900 :: [opt] nested-name-specifier template template-id :: ~ type-name
7901 :: [opt] nested-name-specifier [opt] ~ type-name
7903 If either of the first two productions is used, sets *SCOPE to the
7904 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7905 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7906 or ERROR_MARK_NODE if the parse fails. */
7908 static void
7909 cp_parser_pseudo_destructor_name (cp_parser* parser,
7910 tree object,
7911 tree* scope,
7912 tree* type)
7914 bool nested_name_specifier_p;
7916 /* Handle ~auto. */
7917 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7918 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7919 && !type_dependent_expression_p (object))
7921 if (cxx_dialect < cxx14)
7922 pedwarn (input_location, 0,
7923 "%<~auto%> only available with "
7924 "-std=c++14 or -std=gnu++14");
7925 cp_lexer_consume_token (parser->lexer);
7926 cp_lexer_consume_token (parser->lexer);
7927 *scope = NULL_TREE;
7928 *type = TREE_TYPE (object);
7929 return;
7932 /* Assume that things will not work out. */
7933 *type = error_mark_node;
7935 /* Look for the optional `::' operator. */
7936 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7937 /* Look for the optional nested-name-specifier. */
7938 nested_name_specifier_p
7939 = (cp_parser_nested_name_specifier_opt (parser,
7940 /*typename_keyword_p=*/false,
7941 /*check_dependency_p=*/true,
7942 /*type_p=*/false,
7943 /*is_declaration=*/false)
7944 != NULL_TREE);
7945 /* Now, if we saw a nested-name-specifier, we might be doing the
7946 second production. */
7947 if (nested_name_specifier_p
7948 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7950 /* Consume the `template' keyword. */
7951 cp_lexer_consume_token (parser->lexer);
7952 /* Parse the template-id. */
7953 cp_parser_template_id (parser,
7954 /*template_keyword_p=*/true,
7955 /*check_dependency_p=*/false,
7956 class_type,
7957 /*is_declaration=*/true);
7958 /* Look for the `::' token. */
7959 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7961 /* If the next token is not a `~', then there might be some
7962 additional qualification. */
7963 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7965 /* At this point, we're looking for "type-name :: ~". The type-name
7966 must not be a class-name, since this is a pseudo-destructor. So,
7967 it must be either an enum-name, or a typedef-name -- both of which
7968 are just identifiers. So, we peek ahead to check that the "::"
7969 and "~" tokens are present; if they are not, then we can avoid
7970 calling type_name. */
7971 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7972 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7973 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7975 cp_parser_error (parser, "non-scalar type");
7976 return;
7979 /* Look for the type-name. */
7980 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7981 if (*scope == error_mark_node)
7982 return;
7984 /* Look for the `::' token. */
7985 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7987 else
7988 *scope = NULL_TREE;
7990 /* Look for the `~'. */
7991 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7993 /* Once we see the ~, this has to be a pseudo-destructor. */
7994 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7995 cp_parser_commit_to_topmost_tentative_parse (parser);
7997 /* Look for the type-name again. We are not responsible for
7998 checking that it matches the first type-name. */
7999 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8002 /* Parse a unary-expression.
8004 unary-expression:
8005 postfix-expression
8006 ++ cast-expression
8007 -- cast-expression
8008 unary-operator cast-expression
8009 sizeof unary-expression
8010 sizeof ( type-id )
8011 alignof ( type-id ) [C++0x]
8012 new-expression
8013 delete-expression
8015 GNU Extensions:
8017 unary-expression:
8018 __extension__ cast-expression
8019 __alignof__ unary-expression
8020 __alignof__ ( type-id )
8021 alignof unary-expression [C++0x]
8022 __real__ cast-expression
8023 __imag__ cast-expression
8024 && identifier
8025 sizeof ( type-id ) { initializer-list , [opt] }
8026 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8027 __alignof__ ( type-id ) { initializer-list , [opt] }
8029 ADDRESS_P is true iff the unary-expression is appearing as the
8030 operand of the `&' operator. CAST_P is true if this expression is
8031 the target of a cast.
8033 Returns a representation of the expression. */
8035 static cp_expr
8036 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8037 bool address_p, bool cast_p, bool decltype_p)
8039 cp_token *token;
8040 enum tree_code unary_operator;
8042 /* Peek at the next token. */
8043 token = cp_lexer_peek_token (parser->lexer);
8044 /* Some keywords give away the kind of expression. */
8045 if (token->type == CPP_KEYWORD)
8047 enum rid keyword = token->keyword;
8049 switch (keyword)
8051 case RID_ALIGNOF:
8052 case RID_SIZEOF:
8054 tree operand, ret;
8055 enum tree_code op;
8056 location_t start_loc = token->location;
8058 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8059 /* Consume the token. */
8060 cp_lexer_consume_token (parser->lexer);
8061 /* Parse the operand. */
8062 operand = cp_parser_sizeof_operand (parser, keyword);
8064 if (TYPE_P (operand))
8065 ret = cxx_sizeof_or_alignof_type (operand, op, true);
8066 else
8068 /* ISO C++ defines alignof only with types, not with
8069 expressions. So pedwarn if alignof is used with a non-
8070 type expression. However, __alignof__ is ok. */
8071 if (id_equal (token->u.value, "alignof"))
8072 pedwarn (token->location, OPT_Wpedantic,
8073 "ISO C++ does not allow %<alignof%> "
8074 "with a non-type");
8076 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8078 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8079 SIZEOF_EXPR with the original operand. */
8080 if (op == SIZEOF_EXPR && ret != error_mark_node)
8082 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8084 if (!processing_template_decl && TYPE_P (operand))
8086 ret = build_min (SIZEOF_EXPR, size_type_node,
8087 build1 (NOP_EXPR, operand,
8088 error_mark_node));
8089 SIZEOF_EXPR_TYPE_P (ret) = 1;
8091 else
8092 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8093 TREE_SIDE_EFFECTS (ret) = 0;
8094 TREE_READONLY (ret) = 1;
8098 /* Construct a location e.g. :
8099 alignof (expr)
8100 ^~~~~~~~~~~~~~
8101 with start == caret at the start of the "alignof"/"sizeof"
8102 token, with the endpoint at the final closing paren. */
8103 location_t finish_loc
8104 = cp_lexer_previous_token (parser->lexer)->location;
8105 location_t compound_loc
8106 = make_location (start_loc, start_loc, finish_loc);
8108 cp_expr ret_expr (ret);
8109 ret_expr.set_location (compound_loc);
8110 return ret_expr;
8113 case RID_NEW:
8114 return cp_parser_new_expression (parser);
8116 case RID_DELETE:
8117 return cp_parser_delete_expression (parser);
8119 case RID_EXTENSION:
8121 /* The saved value of the PEDANTIC flag. */
8122 int saved_pedantic;
8123 tree expr;
8125 /* Save away the PEDANTIC flag. */
8126 cp_parser_extension_opt (parser, &saved_pedantic);
8127 /* Parse the cast-expression. */
8128 expr = cp_parser_simple_cast_expression (parser);
8129 /* Restore the PEDANTIC flag. */
8130 pedantic = saved_pedantic;
8132 return expr;
8135 case RID_REALPART:
8136 case RID_IMAGPART:
8138 tree expression;
8140 /* Consume the `__real__' or `__imag__' token. */
8141 cp_lexer_consume_token (parser->lexer);
8142 /* Parse the cast-expression. */
8143 expression = cp_parser_simple_cast_expression (parser);
8144 /* Create the complete representation. */
8145 return build_x_unary_op (token->location,
8146 (keyword == RID_REALPART
8147 ? REALPART_EXPR : IMAGPART_EXPR),
8148 expression,
8149 tf_warning_or_error);
8151 break;
8153 case RID_TRANSACTION_ATOMIC:
8154 case RID_TRANSACTION_RELAXED:
8155 return cp_parser_transaction_expression (parser, keyword);
8157 case RID_NOEXCEPT:
8159 tree expr;
8160 const char *saved_message;
8161 bool saved_integral_constant_expression_p;
8162 bool saved_non_integral_constant_expression_p;
8163 bool saved_greater_than_is_operator_p;
8165 cp_lexer_consume_token (parser->lexer);
8166 matching_parens parens;
8167 parens.require_open (parser);
8169 saved_message = parser->type_definition_forbidden_message;
8170 parser->type_definition_forbidden_message
8171 = G_("types may not be defined in %<noexcept%> expressions");
8173 saved_integral_constant_expression_p
8174 = parser->integral_constant_expression_p;
8175 saved_non_integral_constant_expression_p
8176 = parser->non_integral_constant_expression_p;
8177 parser->integral_constant_expression_p = false;
8179 saved_greater_than_is_operator_p
8180 = parser->greater_than_is_operator_p;
8181 parser->greater_than_is_operator_p = true;
8183 ++cp_unevaluated_operand;
8184 ++c_inhibit_evaluation_warnings;
8185 ++cp_noexcept_operand;
8186 expr = cp_parser_expression (parser);
8187 --cp_noexcept_operand;
8188 --c_inhibit_evaluation_warnings;
8189 --cp_unevaluated_operand;
8191 parser->greater_than_is_operator_p
8192 = saved_greater_than_is_operator_p;
8194 parser->integral_constant_expression_p
8195 = saved_integral_constant_expression_p;
8196 parser->non_integral_constant_expression_p
8197 = saved_non_integral_constant_expression_p;
8199 parser->type_definition_forbidden_message = saved_message;
8201 parens.require_close (parser);
8202 return finish_noexcept_expr (expr, tf_warning_or_error);
8205 default:
8206 break;
8210 /* Look for the `:: new' and `:: delete', which also signal the
8211 beginning of a new-expression, or delete-expression,
8212 respectively. If the next token is `::', then it might be one of
8213 these. */
8214 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8216 enum rid keyword;
8218 /* See if the token after the `::' is one of the keywords in
8219 which we're interested. */
8220 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8221 /* If it's `new', we have a new-expression. */
8222 if (keyword == RID_NEW)
8223 return cp_parser_new_expression (parser);
8224 /* Similarly, for `delete'. */
8225 else if (keyword == RID_DELETE)
8226 return cp_parser_delete_expression (parser);
8229 /* Look for a unary operator. */
8230 unary_operator = cp_parser_unary_operator (token);
8231 /* The `++' and `--' operators can be handled similarly, even though
8232 they are not technically unary-operators in the grammar. */
8233 if (unary_operator == ERROR_MARK)
8235 if (token->type == CPP_PLUS_PLUS)
8236 unary_operator = PREINCREMENT_EXPR;
8237 else if (token->type == CPP_MINUS_MINUS)
8238 unary_operator = PREDECREMENT_EXPR;
8239 /* Handle the GNU address-of-label extension. */
8240 else if (cp_parser_allow_gnu_extensions_p (parser)
8241 && token->type == CPP_AND_AND)
8243 tree identifier;
8244 tree expression;
8245 location_t start_loc = token->location;
8247 /* Consume the '&&' token. */
8248 cp_lexer_consume_token (parser->lexer);
8249 /* Look for the identifier. */
8250 location_t finish_loc
8251 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8252 identifier = cp_parser_identifier (parser);
8253 /* Construct a location of the form:
8254 &&label
8255 ^~~~~~~
8256 with caret==start at the "&&", finish at the end of the label. */
8257 location_t combined_loc
8258 = make_location (start_loc, start_loc, finish_loc);
8259 /* Create an expression representing the address. */
8260 expression = finish_label_address_expr (identifier, combined_loc);
8261 if (cp_parser_non_integral_constant_expression (parser,
8262 NIC_ADDR_LABEL))
8263 expression = error_mark_node;
8264 return expression;
8267 if (unary_operator != ERROR_MARK)
8269 cp_expr cast_expression;
8270 cp_expr expression = error_mark_node;
8271 non_integral_constant non_constant_p = NIC_NONE;
8272 location_t loc = token->location;
8273 tsubst_flags_t complain = complain_flags (decltype_p);
8275 /* Consume the operator token. */
8276 token = cp_lexer_consume_token (parser->lexer);
8277 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8279 /* Parse the cast-expression. */
8280 cast_expression
8281 = cp_parser_cast_expression (parser,
8282 unary_operator == ADDR_EXPR,
8283 /*cast_p=*/false,
8284 /*decltype*/false,
8285 pidk);
8287 /* Make a location:
8288 OP_TOKEN CAST_EXPRESSION
8289 ^~~~~~~~~~~~~~~~~~~~~~~~~
8290 with start==caret at the operator token, and
8291 extending to the end of the cast_expression. */
8292 loc = make_location (loc, loc, cast_expression.get_finish ());
8294 /* Now, build an appropriate representation. */
8295 switch (unary_operator)
8297 case INDIRECT_REF:
8298 non_constant_p = NIC_STAR;
8299 expression = build_x_indirect_ref (loc, cast_expression,
8300 RO_UNARY_STAR,
8301 complain);
8302 /* TODO: build_x_indirect_ref does not always honor the
8303 location, so ensure it is set. */
8304 expression.set_location (loc);
8305 break;
8307 case ADDR_EXPR:
8308 non_constant_p = NIC_ADDR;
8309 /* Fall through. */
8310 case BIT_NOT_EXPR:
8311 expression = build_x_unary_op (loc, unary_operator,
8312 cast_expression,
8313 complain);
8314 /* TODO: build_x_unary_op does not always honor the location,
8315 so ensure it is set. */
8316 expression.set_location (loc);
8317 break;
8319 case PREINCREMENT_EXPR:
8320 case PREDECREMENT_EXPR:
8321 non_constant_p = unary_operator == PREINCREMENT_EXPR
8322 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8323 /* Fall through. */
8324 case NEGATE_EXPR:
8325 /* Immediately fold negation of a constant, unless the constant is 0
8326 (since -0 == 0) or it would overflow. */
8327 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8328 && CONSTANT_CLASS_P (cast_expression)
8329 && !integer_zerop (cast_expression)
8330 && !TREE_OVERFLOW (cast_expression))
8332 tree folded = fold_build1 (unary_operator,
8333 TREE_TYPE (cast_expression),
8334 cast_expression);
8335 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8337 expression = cp_expr (folded, loc);
8338 break;
8341 /* Fall through. */
8342 case UNARY_PLUS_EXPR:
8343 case TRUTH_NOT_EXPR:
8344 expression = finish_unary_op_expr (loc, unary_operator,
8345 cast_expression, complain);
8346 break;
8348 default:
8349 gcc_unreachable ();
8352 if (non_constant_p != NIC_NONE
8353 && cp_parser_non_integral_constant_expression (parser,
8354 non_constant_p))
8355 expression = error_mark_node;
8357 return expression;
8360 return cp_parser_postfix_expression (parser, address_p, cast_p,
8361 /*member_access_only_p=*/false,
8362 decltype_p,
8363 pidk);
8366 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8367 unary-operator, the corresponding tree code is returned. */
8369 static enum tree_code
8370 cp_parser_unary_operator (cp_token* token)
8372 switch (token->type)
8374 case CPP_MULT:
8375 return INDIRECT_REF;
8377 case CPP_AND:
8378 return ADDR_EXPR;
8380 case CPP_PLUS:
8381 return UNARY_PLUS_EXPR;
8383 case CPP_MINUS:
8384 return NEGATE_EXPR;
8386 case CPP_NOT:
8387 return TRUTH_NOT_EXPR;
8389 case CPP_COMPL:
8390 return BIT_NOT_EXPR;
8392 default:
8393 return ERROR_MARK;
8397 /* Parse a new-expression.
8399 new-expression:
8400 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8401 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8403 Returns a representation of the expression. */
8405 static tree
8406 cp_parser_new_expression (cp_parser* parser)
8408 bool global_scope_p;
8409 vec<tree, va_gc> *placement;
8410 tree type;
8411 vec<tree, va_gc> *initializer;
8412 tree nelts = NULL_TREE;
8413 tree ret;
8415 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8417 /* Look for the optional `::' operator. */
8418 global_scope_p
8419 = (cp_parser_global_scope_opt (parser,
8420 /*current_scope_valid_p=*/false)
8421 != NULL_TREE);
8422 /* Look for the `new' operator. */
8423 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8424 /* There's no easy way to tell a new-placement from the
8425 `( type-id )' construct. */
8426 cp_parser_parse_tentatively (parser);
8427 /* Look for a new-placement. */
8428 placement = cp_parser_new_placement (parser);
8429 /* If that didn't work out, there's no new-placement. */
8430 if (!cp_parser_parse_definitely (parser))
8432 if (placement != NULL)
8433 release_tree_vector (placement);
8434 placement = NULL;
8437 /* If the next token is a `(', then we have a parenthesized
8438 type-id. */
8439 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8441 cp_token *token;
8442 const char *saved_message = parser->type_definition_forbidden_message;
8444 /* Consume the `('. */
8445 matching_parens parens;
8446 parens.consume_open (parser);
8448 /* Parse the type-id. */
8449 parser->type_definition_forbidden_message
8450 = G_("types may not be defined in a new-expression");
8452 type_id_in_expr_sentinel s (parser);
8453 type = cp_parser_type_id (parser);
8455 parser->type_definition_forbidden_message = saved_message;
8457 /* Look for the closing `)'. */
8458 parens.require_close (parser);
8459 token = cp_lexer_peek_token (parser->lexer);
8460 /* There should not be a direct-new-declarator in this production,
8461 but GCC used to allowed this, so we check and emit a sensible error
8462 message for this case. */
8463 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8465 error_at (token->location,
8466 "array bound forbidden after parenthesized type-id");
8467 inform (token->location,
8468 "try removing the parentheses around the type-id");
8469 cp_parser_direct_new_declarator (parser);
8472 /* Otherwise, there must be a new-type-id. */
8473 else
8474 type = cp_parser_new_type_id (parser, &nelts);
8476 /* If the next token is a `(' or '{', then we have a new-initializer. */
8477 cp_token *token = cp_lexer_peek_token (parser->lexer);
8478 if (token->type == CPP_OPEN_PAREN
8479 || token->type == CPP_OPEN_BRACE)
8480 initializer = cp_parser_new_initializer (parser);
8481 else
8482 initializer = NULL;
8484 /* A new-expression may not appear in an integral constant
8485 expression. */
8486 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8487 ret = error_mark_node;
8488 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8489 of a new-type-id or type-id of a new-expression, the new-expression shall
8490 contain a new-initializer of the form ( assignment-expression )".
8491 Additionally, consistently with the spirit of DR 1467, we want to accept
8492 'new auto { 2 }' too. */
8493 else if ((ret = type_uses_auto (type))
8494 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8495 && (vec_safe_length (initializer) != 1
8496 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8497 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8499 error_at (token->location,
8500 "initialization of new-expression for type %<auto%> "
8501 "requires exactly one element");
8502 ret = error_mark_node;
8504 else
8506 /* Construct a location e.g.:
8507 ptr = new int[100]
8508 ^~~~~~~~~~~~
8509 with caret == start at the start of the "new" token, and the end
8510 at the end of the final token we consumed. */
8511 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8512 location_t end_loc = get_finish (end_tok->location);
8513 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8515 /* Create a representation of the new-expression. */
8516 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8517 tf_warning_or_error);
8518 protected_set_expr_location (ret, combined_loc);
8521 if (placement != NULL)
8522 release_tree_vector (placement);
8523 if (initializer != NULL)
8524 release_tree_vector (initializer);
8526 return ret;
8529 /* Parse a new-placement.
8531 new-placement:
8532 ( expression-list )
8534 Returns the same representation as for an expression-list. */
8536 static vec<tree, va_gc> *
8537 cp_parser_new_placement (cp_parser* parser)
8539 vec<tree, va_gc> *expression_list;
8541 /* Parse the expression-list. */
8542 expression_list = (cp_parser_parenthesized_expression_list
8543 (parser, non_attr, /*cast_p=*/false,
8544 /*allow_expansion_p=*/true,
8545 /*non_constant_p=*/NULL));
8547 if (expression_list && expression_list->is_empty ())
8548 error ("expected expression-list or type-id");
8550 return expression_list;
8553 /* Parse a new-type-id.
8555 new-type-id:
8556 type-specifier-seq new-declarator [opt]
8558 Returns the TYPE allocated. If the new-type-id indicates an array
8559 type, *NELTS is set to the number of elements in the last array
8560 bound; the TYPE will not include the last array bound. */
8562 static tree
8563 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8565 cp_decl_specifier_seq type_specifier_seq;
8566 cp_declarator *new_declarator;
8567 cp_declarator *declarator;
8568 cp_declarator *outer_declarator;
8569 const char *saved_message;
8571 /* The type-specifier sequence must not contain type definitions.
8572 (It cannot contain declarations of new types either, but if they
8573 are not definitions we will catch that because they are not
8574 complete.) */
8575 saved_message = parser->type_definition_forbidden_message;
8576 parser->type_definition_forbidden_message
8577 = G_("types may not be defined in a new-type-id");
8578 /* Parse the type-specifier-seq. */
8579 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8580 /*is_trailing_return=*/false,
8581 &type_specifier_seq);
8582 /* Restore the old message. */
8583 parser->type_definition_forbidden_message = saved_message;
8585 if (type_specifier_seq.type == error_mark_node)
8586 return error_mark_node;
8588 /* Parse the new-declarator. */
8589 new_declarator = cp_parser_new_declarator_opt (parser);
8591 /* Determine the number of elements in the last array dimension, if
8592 any. */
8593 *nelts = NULL_TREE;
8594 /* Skip down to the last array dimension. */
8595 declarator = new_declarator;
8596 outer_declarator = NULL;
8597 while (declarator && (declarator->kind == cdk_pointer
8598 || declarator->kind == cdk_ptrmem))
8600 outer_declarator = declarator;
8601 declarator = declarator->declarator;
8603 while (declarator
8604 && declarator->kind == cdk_array
8605 && declarator->declarator
8606 && declarator->declarator->kind == cdk_array)
8608 outer_declarator = declarator;
8609 declarator = declarator->declarator;
8612 if (declarator && declarator->kind == cdk_array)
8614 *nelts = declarator->u.array.bounds;
8615 if (*nelts == error_mark_node)
8616 *nelts = integer_one_node;
8618 if (outer_declarator)
8619 outer_declarator->declarator = declarator->declarator;
8620 else
8621 new_declarator = NULL;
8624 return groktypename (&type_specifier_seq, new_declarator, false);
8627 /* Parse an (optional) new-declarator.
8629 new-declarator:
8630 ptr-operator new-declarator [opt]
8631 direct-new-declarator
8633 Returns the declarator. */
8635 static cp_declarator *
8636 cp_parser_new_declarator_opt (cp_parser* parser)
8638 enum tree_code code;
8639 tree type, std_attributes = NULL_TREE;
8640 cp_cv_quals cv_quals;
8642 /* We don't know if there's a ptr-operator next, or not. */
8643 cp_parser_parse_tentatively (parser);
8644 /* Look for a ptr-operator. */
8645 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8646 /* If that worked, look for more new-declarators. */
8647 if (cp_parser_parse_definitely (parser))
8649 cp_declarator *declarator;
8651 /* Parse another optional declarator. */
8652 declarator = cp_parser_new_declarator_opt (parser);
8654 declarator = cp_parser_make_indirect_declarator
8655 (code, type, cv_quals, declarator, std_attributes);
8657 return declarator;
8660 /* If the next token is a `[', there is a direct-new-declarator. */
8661 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8662 return cp_parser_direct_new_declarator (parser);
8664 return NULL;
8667 /* Parse a direct-new-declarator.
8669 direct-new-declarator:
8670 [ expression ]
8671 direct-new-declarator [constant-expression]
8675 static cp_declarator *
8676 cp_parser_direct_new_declarator (cp_parser* parser)
8678 cp_declarator *declarator = NULL;
8680 while (true)
8682 tree expression;
8683 cp_token *token;
8685 /* Look for the opening `['. */
8686 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8688 token = cp_lexer_peek_token (parser->lexer);
8689 expression = cp_parser_expression (parser);
8690 /* The standard requires that the expression have integral
8691 type. DR 74 adds enumeration types. We believe that the
8692 real intent is that these expressions be handled like the
8693 expression in a `switch' condition, which also allows
8694 classes with a single conversion to integral or
8695 enumeration type. */
8696 if (!processing_template_decl)
8698 expression
8699 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8700 expression,
8701 /*complain=*/true);
8702 if (!expression)
8704 error_at (token->location,
8705 "expression in new-declarator must have integral "
8706 "or enumeration type");
8707 expression = error_mark_node;
8711 /* Look for the closing `]'. */
8712 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8714 /* Add this bound to the declarator. */
8715 declarator = make_array_declarator (declarator, expression);
8717 /* If the next token is not a `[', then there are no more
8718 bounds. */
8719 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8720 break;
8723 return declarator;
8726 /* Parse a new-initializer.
8728 new-initializer:
8729 ( expression-list [opt] )
8730 braced-init-list
8732 Returns a representation of the expression-list. */
8734 static vec<tree, va_gc> *
8735 cp_parser_new_initializer (cp_parser* parser)
8737 vec<tree, va_gc> *expression_list;
8739 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8741 tree t;
8742 bool expr_non_constant_p;
8743 cp_lexer_set_source_position (parser->lexer);
8744 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8745 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8746 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8747 expression_list = make_tree_vector_single (t);
8749 else
8750 expression_list = (cp_parser_parenthesized_expression_list
8751 (parser, non_attr, /*cast_p=*/false,
8752 /*allow_expansion_p=*/true,
8753 /*non_constant_p=*/NULL));
8755 return expression_list;
8758 /* Parse a delete-expression.
8760 delete-expression:
8761 :: [opt] delete cast-expression
8762 :: [opt] delete [ ] cast-expression
8764 Returns a representation of the expression. */
8766 static tree
8767 cp_parser_delete_expression (cp_parser* parser)
8769 bool global_scope_p;
8770 bool array_p;
8771 tree expression;
8773 /* Look for the optional `::' operator. */
8774 global_scope_p
8775 = (cp_parser_global_scope_opt (parser,
8776 /*current_scope_valid_p=*/false)
8777 != NULL_TREE);
8778 /* Look for the `delete' keyword. */
8779 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8780 /* See if the array syntax is in use. */
8781 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8783 /* Consume the `[' token. */
8784 cp_lexer_consume_token (parser->lexer);
8785 /* Look for the `]' token. */
8786 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8787 /* Remember that this is the `[]' construct. */
8788 array_p = true;
8790 else
8791 array_p = false;
8793 /* Parse the cast-expression. */
8794 expression = cp_parser_simple_cast_expression (parser);
8796 /* A delete-expression may not appear in an integral constant
8797 expression. */
8798 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8799 return error_mark_node;
8801 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8802 tf_warning_or_error);
8805 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8806 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8807 0 otherwise. */
8809 static int
8810 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8812 cp_token *token = cp_lexer_peek_token (parser->lexer);
8813 switch (token->type)
8815 case CPP_COMMA:
8816 case CPP_SEMICOLON:
8817 case CPP_QUERY:
8818 case CPP_COLON:
8819 case CPP_CLOSE_SQUARE:
8820 case CPP_CLOSE_PAREN:
8821 case CPP_CLOSE_BRACE:
8822 case CPP_OPEN_BRACE:
8823 case CPP_DOT:
8824 case CPP_DOT_STAR:
8825 case CPP_DEREF:
8826 case CPP_DEREF_STAR:
8827 case CPP_DIV:
8828 case CPP_MOD:
8829 case CPP_LSHIFT:
8830 case CPP_RSHIFT:
8831 case CPP_LESS:
8832 case CPP_GREATER:
8833 case CPP_LESS_EQ:
8834 case CPP_GREATER_EQ:
8835 case CPP_EQ_EQ:
8836 case CPP_NOT_EQ:
8837 case CPP_EQ:
8838 case CPP_MULT_EQ:
8839 case CPP_DIV_EQ:
8840 case CPP_MOD_EQ:
8841 case CPP_PLUS_EQ:
8842 case CPP_MINUS_EQ:
8843 case CPP_RSHIFT_EQ:
8844 case CPP_LSHIFT_EQ:
8845 case CPP_AND_EQ:
8846 case CPP_XOR_EQ:
8847 case CPP_OR_EQ:
8848 case CPP_XOR:
8849 case CPP_OR:
8850 case CPP_OR_OR:
8851 case CPP_EOF:
8852 case CPP_ELLIPSIS:
8853 return 0;
8855 case CPP_OPEN_PAREN:
8856 /* In ((type ()) () the last () isn't a valid cast-expression,
8857 so the whole must be parsed as postfix-expression. */
8858 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8859 != CPP_CLOSE_PAREN;
8861 case CPP_OPEN_SQUARE:
8862 /* '[' may start a primary-expression in obj-c++ and in C++11,
8863 as a lambda-expression, eg, '(void)[]{}'. */
8864 if (cxx_dialect >= cxx11)
8865 return -1;
8866 return c_dialect_objc ();
8868 case CPP_PLUS_PLUS:
8869 case CPP_MINUS_MINUS:
8870 /* '++' and '--' may or may not start a cast-expression:
8872 struct T { void operator++(int); };
8873 void f() { (T())++; }
8877 int a;
8878 (int)++a; */
8879 return -1;
8881 default:
8882 return 1;
8886 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8887 in the order: const_cast, static_cast, reinterpret_cast.
8889 Don't suggest dynamic_cast.
8891 Return the first legal cast kind found, or NULL otherwise. */
8893 static const char *
8894 get_cast_suggestion (tree dst_type, tree orig_expr)
8896 tree trial;
8898 /* Reuse the parser logic by attempting to build the various kinds of
8899 cast, with "complain" disabled.
8900 Identify the first such cast that is valid. */
8902 /* Don't attempt to run such logic within template processing. */
8903 if (processing_template_decl)
8904 return NULL;
8906 /* First try const_cast. */
8907 trial = build_const_cast (dst_type, orig_expr, tf_none);
8908 if (trial != error_mark_node)
8909 return "const_cast";
8911 /* If that fails, try static_cast. */
8912 trial = build_static_cast (dst_type, orig_expr, tf_none);
8913 if (trial != error_mark_node)
8914 return "static_cast";
8916 /* Finally, try reinterpret_cast. */
8917 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8918 if (trial != error_mark_node)
8919 return "reinterpret_cast";
8921 /* No such cast possible. */
8922 return NULL;
8925 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8926 suggesting how to convert a C-style cast of the form:
8928 (DST_TYPE)ORIG_EXPR
8930 to a C++-style cast.
8932 The primary range of RICHLOC is asssumed to be that of the original
8933 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8934 of the parens in the C-style cast. */
8936 static void
8937 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8938 location_t close_paren_loc, tree orig_expr,
8939 tree dst_type)
8941 /* This function is non-trivial, so bail out now if the warning isn't
8942 going to be emitted. */
8943 if (!warn_old_style_cast)
8944 return;
8946 /* Try to find a legal C++ cast, trying them in order:
8947 const_cast, static_cast, reinterpret_cast. */
8948 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8949 if (!cast_suggestion)
8950 return;
8952 /* Replace the open paren with "CAST_SUGGESTION<". */
8953 pretty_printer pp;
8954 pp_printf (&pp, "%s<", cast_suggestion);
8955 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8957 /* Replace the close paren with "> (". */
8958 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8960 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8961 rich_loc->add_fixit_insert_after (")");
8965 /* Parse a cast-expression.
8967 cast-expression:
8968 unary-expression
8969 ( type-id ) cast-expression
8971 ADDRESS_P is true iff the unary-expression is appearing as the
8972 operand of the `&' operator. CAST_P is true if this expression is
8973 the target of a cast.
8975 Returns a representation of the expression. */
8977 static cp_expr
8978 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8979 bool decltype_p, cp_id_kind * pidk)
8981 /* If it's a `(', then we might be looking at a cast. */
8982 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8984 tree type = NULL_TREE;
8985 cp_expr expr (NULL_TREE);
8986 int cast_expression = 0;
8987 const char *saved_message;
8989 /* There's no way to know yet whether or not this is a cast.
8990 For example, `(int (3))' is a unary-expression, while `(int)
8991 3' is a cast. So, we resort to parsing tentatively. */
8992 cp_parser_parse_tentatively (parser);
8993 /* Types may not be defined in a cast. */
8994 saved_message = parser->type_definition_forbidden_message;
8995 parser->type_definition_forbidden_message
8996 = G_("types may not be defined in casts");
8997 /* Consume the `('. */
8998 matching_parens parens;
8999 cp_token *open_paren = parens.consume_open (parser);
9000 location_t open_paren_loc = open_paren->location;
9001 location_t close_paren_loc = UNKNOWN_LOCATION;
9003 /* A very tricky bit is that `(struct S) { 3 }' is a
9004 compound-literal (which we permit in C++ as an extension).
9005 But, that construct is not a cast-expression -- it is a
9006 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9007 is legal; if the compound-literal were a cast-expression,
9008 you'd need an extra set of parentheses.) But, if we parse
9009 the type-id, and it happens to be a class-specifier, then we
9010 will commit to the parse at that point, because we cannot
9011 undo the action that is done when creating a new class. So,
9012 then we cannot back up and do a postfix-expression.
9014 Another tricky case is the following (c++/29234):
9016 struct S { void operator () (); };
9018 void foo ()
9020 ( S()() );
9023 As a type-id we parse the parenthesized S()() as a function
9024 returning a function, groktypename complains and we cannot
9025 back up in this case either.
9027 Therefore, we scan ahead to the closing `)', and check to see
9028 if the tokens after the `)' can start a cast-expression. Otherwise
9029 we are dealing with an unary-expression, a postfix-expression
9030 or something else.
9032 Yet another tricky case, in C++11, is the following (c++/54891):
9034 (void)[]{};
9036 The issue is that usually, besides the case of lambda-expressions,
9037 the parenthesized type-id cannot be followed by '[', and, eg, we
9038 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9039 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9040 we don't commit, we try a cast-expression, then an unary-expression.
9042 Save tokens so that we can put them back. */
9043 cp_lexer_save_tokens (parser->lexer);
9045 /* We may be looking at a cast-expression. */
9046 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9047 /*consume_paren=*/true))
9048 cast_expression
9049 = cp_parser_tokens_start_cast_expression (parser);
9051 /* Roll back the tokens we skipped. */
9052 cp_lexer_rollback_tokens (parser->lexer);
9053 /* If we aren't looking at a cast-expression, simulate an error so
9054 that the call to cp_parser_error_occurred below returns true. */
9055 if (!cast_expression)
9056 cp_parser_simulate_error (parser);
9057 else
9059 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9060 parser->in_type_id_in_expr_p = true;
9061 /* Look for the type-id. */
9062 type = cp_parser_type_id (parser);
9063 /* Look for the closing `)'. */
9064 cp_token *close_paren = parens.require_close (parser);
9065 if (close_paren)
9066 close_paren_loc = close_paren->location;
9067 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9070 /* Restore the saved message. */
9071 parser->type_definition_forbidden_message = saved_message;
9073 /* At this point this can only be either a cast or a
9074 parenthesized ctor such as `(T ())' that looks like a cast to
9075 function returning T. */
9076 if (!cp_parser_error_occurred (parser))
9078 /* Only commit if the cast-expression doesn't start with
9079 '++', '--', or '[' in C++11. */
9080 if (cast_expression > 0)
9081 cp_parser_commit_to_topmost_tentative_parse (parser);
9083 expr = cp_parser_cast_expression (parser,
9084 /*address_p=*/false,
9085 /*cast_p=*/true,
9086 /*decltype_p=*/false,
9087 pidk);
9089 if (cp_parser_parse_definitely (parser))
9091 /* Warn about old-style casts, if so requested. */
9092 if (warn_old_style_cast
9093 && !in_system_header_at (input_location)
9094 && !VOID_TYPE_P (type)
9095 && current_lang_name != lang_name_c)
9097 gcc_rich_location rich_loc (input_location);
9098 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9099 expr, type);
9100 warning_at (&rich_loc, OPT_Wold_style_cast,
9101 "use of old-style cast to %q#T", type);
9104 /* Only type conversions to integral or enumeration types
9105 can be used in constant-expressions. */
9106 if (!cast_valid_in_integral_constant_expression_p (type)
9107 && cp_parser_non_integral_constant_expression (parser,
9108 NIC_CAST))
9109 return error_mark_node;
9111 /* Perform the cast. */
9112 /* Make a location:
9113 (TYPE) EXPR
9114 ^~~~~~~~~~~
9115 with start==caret at the open paren, extending to the
9116 end of "expr". */
9117 location_t cast_loc = make_location (open_paren_loc,
9118 open_paren_loc,
9119 expr.get_finish ());
9120 expr = build_c_cast (cast_loc, type, expr);
9121 return expr;
9124 else
9125 cp_parser_abort_tentative_parse (parser);
9128 /* If we get here, then it's not a cast, so it must be a
9129 unary-expression. */
9130 return cp_parser_unary_expression (parser, pidk, address_p,
9131 cast_p, decltype_p);
9134 /* Parse a binary expression of the general form:
9136 pm-expression:
9137 cast-expression
9138 pm-expression .* cast-expression
9139 pm-expression ->* cast-expression
9141 multiplicative-expression:
9142 pm-expression
9143 multiplicative-expression * pm-expression
9144 multiplicative-expression / pm-expression
9145 multiplicative-expression % pm-expression
9147 additive-expression:
9148 multiplicative-expression
9149 additive-expression + multiplicative-expression
9150 additive-expression - multiplicative-expression
9152 shift-expression:
9153 additive-expression
9154 shift-expression << additive-expression
9155 shift-expression >> additive-expression
9157 relational-expression:
9158 shift-expression
9159 relational-expression < shift-expression
9160 relational-expression > shift-expression
9161 relational-expression <= shift-expression
9162 relational-expression >= shift-expression
9164 GNU Extension:
9166 relational-expression:
9167 relational-expression <? shift-expression
9168 relational-expression >? shift-expression
9170 equality-expression:
9171 relational-expression
9172 equality-expression == relational-expression
9173 equality-expression != relational-expression
9175 and-expression:
9176 equality-expression
9177 and-expression & equality-expression
9179 exclusive-or-expression:
9180 and-expression
9181 exclusive-or-expression ^ and-expression
9183 inclusive-or-expression:
9184 exclusive-or-expression
9185 inclusive-or-expression | exclusive-or-expression
9187 logical-and-expression:
9188 inclusive-or-expression
9189 logical-and-expression && inclusive-or-expression
9191 logical-or-expression:
9192 logical-and-expression
9193 logical-or-expression || logical-and-expression
9195 All these are implemented with a single function like:
9197 binary-expression:
9198 simple-cast-expression
9199 binary-expression <token> binary-expression
9201 CAST_P is true if this expression is the target of a cast.
9203 The binops_by_token map is used to get the tree codes for each <token> type.
9204 binary-expressions are associated according to a precedence table. */
9206 #define TOKEN_PRECEDENCE(token) \
9207 (((token->type == CPP_GREATER \
9208 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9209 && !parser->greater_than_is_operator_p) \
9210 ? PREC_NOT_OPERATOR \
9211 : binops_by_token[token->type].prec)
9213 static cp_expr
9214 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9215 bool no_toplevel_fold_p,
9216 bool decltype_p,
9217 enum cp_parser_prec prec,
9218 cp_id_kind * pidk)
9220 cp_parser_expression_stack stack;
9221 cp_parser_expression_stack_entry *sp = &stack[0];
9222 cp_parser_expression_stack_entry current;
9223 cp_expr rhs;
9224 cp_token *token;
9225 enum tree_code rhs_type;
9226 enum cp_parser_prec new_prec, lookahead_prec;
9227 tree overload;
9229 /* Parse the first expression. */
9230 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9231 ? TRUTH_NOT_EXPR : ERROR_MARK);
9232 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9233 cast_p, decltype_p, pidk);
9234 current.prec = prec;
9236 if (cp_parser_error_occurred (parser))
9237 return error_mark_node;
9239 for (;;)
9241 /* Get an operator token. */
9242 token = cp_lexer_peek_token (parser->lexer);
9244 if (warn_cxx11_compat
9245 && token->type == CPP_RSHIFT
9246 && !parser->greater_than_is_operator_p)
9248 if (warning_at (token->location, OPT_Wc__11_compat,
9249 "%<>>%> operator is treated"
9250 " as two right angle brackets in C++11"))
9251 inform (token->location,
9252 "suggest parentheses around %<>>%> expression");
9255 new_prec = TOKEN_PRECEDENCE (token);
9256 if (new_prec != PREC_NOT_OPERATOR
9257 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9258 /* This is a fold-expression; handle it later. */
9259 new_prec = PREC_NOT_OPERATOR;
9261 /* Popping an entry off the stack means we completed a subexpression:
9262 - either we found a token which is not an operator (`>' where it is not
9263 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9264 will happen repeatedly;
9265 - or, we found an operator which has lower priority. This is the case
9266 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9267 parsing `3 * 4'. */
9268 if (new_prec <= current.prec)
9270 if (sp == stack)
9271 break;
9272 else
9273 goto pop;
9276 get_rhs:
9277 current.tree_type = binops_by_token[token->type].tree_type;
9278 current.loc = token->location;
9280 /* We used the operator token. */
9281 cp_lexer_consume_token (parser->lexer);
9283 /* For "false && x" or "true || x", x will never be executed;
9284 disable warnings while evaluating it. */
9285 if (current.tree_type == TRUTH_ANDIF_EXPR)
9286 c_inhibit_evaluation_warnings +=
9287 cp_fully_fold (current.lhs) == truthvalue_false_node;
9288 else if (current.tree_type == TRUTH_ORIF_EXPR)
9289 c_inhibit_evaluation_warnings +=
9290 cp_fully_fold (current.lhs) == truthvalue_true_node;
9292 /* Extract another operand. It may be the RHS of this expression
9293 or the LHS of a new, higher priority expression. */
9294 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9295 ? TRUTH_NOT_EXPR : ERROR_MARK);
9296 rhs = cp_parser_simple_cast_expression (parser);
9298 /* Get another operator token. Look up its precedence to avoid
9299 building a useless (immediately popped) stack entry for common
9300 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9301 token = cp_lexer_peek_token (parser->lexer);
9302 lookahead_prec = TOKEN_PRECEDENCE (token);
9303 if (lookahead_prec != PREC_NOT_OPERATOR
9304 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9305 lookahead_prec = PREC_NOT_OPERATOR;
9306 if (lookahead_prec > new_prec)
9308 /* ... and prepare to parse the RHS of the new, higher priority
9309 expression. Since precedence levels on the stack are
9310 monotonically increasing, we do not have to care about
9311 stack overflows. */
9312 *sp = current;
9313 ++sp;
9314 current.lhs = rhs;
9315 current.lhs_type = rhs_type;
9316 current.prec = new_prec;
9317 new_prec = lookahead_prec;
9318 goto get_rhs;
9320 pop:
9321 lookahead_prec = new_prec;
9322 /* If the stack is not empty, we have parsed into LHS the right side
9323 (`4' in the example above) of an expression we had suspended.
9324 We can use the information on the stack to recover the LHS (`3')
9325 from the stack together with the tree code (`MULT_EXPR'), and
9326 the precedence of the higher level subexpression
9327 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9328 which will be used to actually build the additive expression. */
9329 rhs = current.lhs;
9330 rhs_type = current.lhs_type;
9331 --sp;
9332 current = *sp;
9335 /* Undo the disabling of warnings done above. */
9336 if (current.tree_type == TRUTH_ANDIF_EXPR)
9337 c_inhibit_evaluation_warnings -=
9338 cp_fully_fold (current.lhs) == truthvalue_false_node;
9339 else if (current.tree_type == TRUTH_ORIF_EXPR)
9340 c_inhibit_evaluation_warnings -=
9341 cp_fully_fold (current.lhs) == truthvalue_true_node;
9343 if (warn_logical_not_paren
9344 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9345 && current.lhs_type == TRUTH_NOT_EXPR
9346 /* Avoid warning for !!x == y. */
9347 && (TREE_CODE (current.lhs) != NE_EXPR
9348 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9349 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9350 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9351 /* Avoid warning for !b == y where b is boolean. */
9352 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9353 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9354 != BOOLEAN_TYPE))))
9355 /* Avoid warning for !!b == y where b is boolean. */
9356 && (!DECL_P (current.lhs)
9357 || TREE_TYPE (current.lhs) == NULL_TREE
9358 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9359 warn_logical_not_parentheses (current.loc, current.tree_type,
9360 current.lhs, maybe_constant_value (rhs));
9362 overload = NULL;
9364 location_t combined_loc = make_location (current.loc,
9365 current.lhs.get_start (),
9366 rhs.get_finish ());
9368 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9369 ERROR_MARK for everything that is not a binary expression.
9370 This makes warn_about_parentheses miss some warnings that
9371 involve unary operators. For unary expressions we should
9372 pass the correct tree_code unless the unary expression was
9373 surrounded by parentheses.
9375 if (no_toplevel_fold_p
9376 && lookahead_prec <= current.prec
9377 && sp == stack)
9378 current.lhs = build2_loc (combined_loc,
9379 current.tree_type,
9380 TREE_CODE_CLASS (current.tree_type)
9381 == tcc_comparison
9382 ? boolean_type_node : TREE_TYPE (current.lhs),
9383 current.lhs, rhs);
9384 else
9386 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9387 current.lhs, current.lhs_type,
9388 rhs, rhs_type, &overload,
9389 complain_flags (decltype_p));
9390 /* TODO: build_x_binary_op doesn't always honor the location. */
9391 current.lhs.set_location (combined_loc);
9393 current.lhs_type = current.tree_type;
9395 /* If the binary operator required the use of an overloaded operator,
9396 then this expression cannot be an integral constant-expression.
9397 An overloaded operator can be used even if both operands are
9398 otherwise permissible in an integral constant-expression if at
9399 least one of the operands is of enumeration type. */
9401 if (overload
9402 && cp_parser_non_integral_constant_expression (parser,
9403 NIC_OVERLOADED))
9404 return error_mark_node;
9407 return current.lhs;
9410 static cp_expr
9411 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9412 bool no_toplevel_fold_p,
9413 enum cp_parser_prec prec,
9414 cp_id_kind * pidk)
9416 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9417 /*decltype*/false, prec, pidk);
9420 /* Parse the `? expression : assignment-expression' part of a
9421 conditional-expression. The LOGICAL_OR_EXPR is the
9422 logical-or-expression that started the conditional-expression.
9423 Returns a representation of the entire conditional-expression.
9425 This routine is used by cp_parser_assignment_expression.
9427 ? expression : assignment-expression
9429 GNU Extensions:
9431 ? : assignment-expression */
9433 static tree
9434 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9436 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9437 cp_expr assignment_expr;
9438 struct cp_token *token;
9439 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9441 /* Consume the `?' token. */
9442 cp_lexer_consume_token (parser->lexer);
9443 token = cp_lexer_peek_token (parser->lexer);
9444 if (cp_parser_allow_gnu_extensions_p (parser)
9445 && token->type == CPP_COLON)
9447 pedwarn (token->location, OPT_Wpedantic,
9448 "ISO C++ does not allow ?: with omitted middle operand");
9449 /* Implicit true clause. */
9450 expr = NULL_TREE;
9451 c_inhibit_evaluation_warnings +=
9452 folded_logical_or_expr == truthvalue_true_node;
9453 warn_for_omitted_condop (token->location, logical_or_expr);
9455 else
9457 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9458 parser->colon_corrects_to_scope_p = false;
9459 /* Parse the expression. */
9460 c_inhibit_evaluation_warnings +=
9461 folded_logical_or_expr == truthvalue_false_node;
9462 expr = cp_parser_expression (parser);
9463 c_inhibit_evaluation_warnings +=
9464 ((folded_logical_or_expr == truthvalue_true_node)
9465 - (folded_logical_or_expr == truthvalue_false_node));
9466 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9469 /* The next token should be a `:'. */
9470 cp_parser_require (parser, CPP_COLON, RT_COLON);
9471 /* Parse the assignment-expression. */
9472 assignment_expr = cp_parser_assignment_expression (parser);
9473 c_inhibit_evaluation_warnings -=
9474 folded_logical_or_expr == truthvalue_true_node;
9476 /* Make a location:
9477 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9478 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9479 with the caret at the "?", ranging from the start of
9480 the logical_or_expr to the end of the assignment_expr. */
9481 loc = make_location (loc,
9482 logical_or_expr.get_start (),
9483 assignment_expr.get_finish ());
9485 /* Build the conditional-expression. */
9486 return build_x_conditional_expr (loc, logical_or_expr,
9487 expr,
9488 assignment_expr,
9489 tf_warning_or_error);
9492 /* Parse an assignment-expression.
9494 assignment-expression:
9495 conditional-expression
9496 logical-or-expression assignment-operator assignment_expression
9497 throw-expression
9499 CAST_P is true if this expression is the target of a cast.
9500 DECLTYPE_P is true if this expression is the operand of decltype.
9502 Returns a representation for the expression. */
9504 static cp_expr
9505 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9506 bool cast_p, bool decltype_p)
9508 cp_expr expr;
9510 /* If the next token is the `throw' keyword, then we're looking at
9511 a throw-expression. */
9512 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9513 expr = cp_parser_throw_expression (parser);
9514 /* Otherwise, it must be that we are looking at a
9515 logical-or-expression. */
9516 else
9518 /* Parse the binary expressions (logical-or-expression). */
9519 expr = cp_parser_binary_expression (parser, cast_p, false,
9520 decltype_p,
9521 PREC_NOT_OPERATOR, pidk);
9522 /* If the next token is a `?' then we're actually looking at a
9523 conditional-expression. */
9524 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9525 return cp_parser_question_colon_clause (parser, expr);
9526 else
9528 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9530 /* If it's an assignment-operator, we're using the second
9531 production. */
9532 enum tree_code assignment_operator
9533 = cp_parser_assignment_operator_opt (parser);
9534 if (assignment_operator != ERROR_MARK)
9536 bool non_constant_p;
9538 /* Parse the right-hand side of the assignment. */
9539 cp_expr rhs = cp_parser_initializer_clause (parser,
9540 &non_constant_p);
9542 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9543 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9545 /* An assignment may not appear in a
9546 constant-expression. */
9547 if (cp_parser_non_integral_constant_expression (parser,
9548 NIC_ASSIGNMENT))
9549 return error_mark_node;
9550 /* Build the assignment expression. Its default
9551 location:
9552 LHS = RHS
9553 ~~~~^~~~~
9554 is the location of the '=' token as the
9555 caret, ranging from the start of the lhs to the
9556 end of the rhs. */
9557 loc = make_location (loc,
9558 expr.get_start (),
9559 rhs.get_finish ());
9560 expr = build_x_modify_expr (loc, expr,
9561 assignment_operator,
9562 rhs,
9563 complain_flags (decltype_p));
9564 /* TODO: build_x_modify_expr doesn't honor the location,
9565 so we must set it here. */
9566 expr.set_location (loc);
9571 return expr;
9574 /* Parse an (optional) assignment-operator.
9576 assignment-operator: one of
9577 = *= /= %= += -= >>= <<= &= ^= |=
9579 GNU Extension:
9581 assignment-operator: one of
9582 <?= >?=
9584 If the next token is an assignment operator, the corresponding tree
9585 code is returned, and the token is consumed. For example, for
9586 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9587 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9588 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9589 operator, ERROR_MARK is returned. */
9591 static enum tree_code
9592 cp_parser_assignment_operator_opt (cp_parser* parser)
9594 enum tree_code op;
9595 cp_token *token;
9597 /* Peek at the next token. */
9598 token = cp_lexer_peek_token (parser->lexer);
9600 switch (token->type)
9602 case CPP_EQ:
9603 op = NOP_EXPR;
9604 break;
9606 case CPP_MULT_EQ:
9607 op = MULT_EXPR;
9608 break;
9610 case CPP_DIV_EQ:
9611 op = TRUNC_DIV_EXPR;
9612 break;
9614 case CPP_MOD_EQ:
9615 op = TRUNC_MOD_EXPR;
9616 break;
9618 case CPP_PLUS_EQ:
9619 op = PLUS_EXPR;
9620 break;
9622 case CPP_MINUS_EQ:
9623 op = MINUS_EXPR;
9624 break;
9626 case CPP_RSHIFT_EQ:
9627 op = RSHIFT_EXPR;
9628 break;
9630 case CPP_LSHIFT_EQ:
9631 op = LSHIFT_EXPR;
9632 break;
9634 case CPP_AND_EQ:
9635 op = BIT_AND_EXPR;
9636 break;
9638 case CPP_XOR_EQ:
9639 op = BIT_XOR_EXPR;
9640 break;
9642 case CPP_OR_EQ:
9643 op = BIT_IOR_EXPR;
9644 break;
9646 default:
9647 /* Nothing else is an assignment operator. */
9648 op = ERROR_MARK;
9651 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9652 if (op != ERROR_MARK
9653 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9654 op = ERROR_MARK;
9656 /* If it was an assignment operator, consume it. */
9657 if (op != ERROR_MARK)
9658 cp_lexer_consume_token (parser->lexer);
9660 return op;
9663 /* Parse an expression.
9665 expression:
9666 assignment-expression
9667 expression , assignment-expression
9669 CAST_P is true if this expression is the target of a cast.
9670 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9671 except possibly parenthesized or on the RHS of a comma (N3276).
9673 Returns a representation of the expression. */
9675 static cp_expr
9676 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9677 bool cast_p, bool decltype_p)
9679 cp_expr expression = NULL_TREE;
9680 location_t loc = UNKNOWN_LOCATION;
9682 while (true)
9684 cp_expr assignment_expression;
9686 /* Parse the next assignment-expression. */
9687 assignment_expression
9688 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9690 /* We don't create a temporary for a call that is the immediate operand
9691 of decltype or on the RHS of a comma. But when we see a comma, we
9692 need to create a temporary for a call on the LHS. */
9693 if (decltype_p && !processing_template_decl
9694 && TREE_CODE (assignment_expression) == CALL_EXPR
9695 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9696 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9697 assignment_expression
9698 = build_cplus_new (TREE_TYPE (assignment_expression),
9699 assignment_expression, tf_warning_or_error);
9701 /* If this is the first assignment-expression, we can just
9702 save it away. */
9703 if (!expression)
9704 expression = assignment_expression;
9705 else
9707 /* Create a location with caret at the comma, ranging
9708 from the start of the LHS to the end of the RHS. */
9709 loc = make_location (loc,
9710 expression.get_start (),
9711 assignment_expression.get_finish ());
9712 expression = build_x_compound_expr (loc, expression,
9713 assignment_expression,
9714 complain_flags (decltype_p));
9715 expression.set_location (loc);
9717 /* If the next token is not a comma, or we're in a fold-expression, then
9718 we are done with the expression. */
9719 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9720 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9721 break;
9722 /* Consume the `,'. */
9723 loc = cp_lexer_peek_token (parser->lexer)->location;
9724 cp_lexer_consume_token (parser->lexer);
9725 /* A comma operator cannot appear in a constant-expression. */
9726 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9727 expression = error_mark_node;
9730 return expression;
9733 /* Parse a constant-expression.
9735 constant-expression:
9736 conditional-expression
9738 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9739 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9740 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9741 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9742 only parse a conditional-expression, otherwise parse an
9743 assignment-expression. See below for rationale. */
9745 static cp_expr
9746 cp_parser_constant_expression (cp_parser* parser,
9747 bool allow_non_constant_p,
9748 bool *non_constant_p,
9749 bool strict_p)
9751 bool saved_integral_constant_expression_p;
9752 bool saved_allow_non_integral_constant_expression_p;
9753 bool saved_non_integral_constant_expression_p;
9754 cp_expr expression;
9756 /* It might seem that we could simply parse the
9757 conditional-expression, and then check to see if it were
9758 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9759 one that the compiler can figure out is constant, possibly after
9760 doing some simplifications or optimizations. The standard has a
9761 precise definition of constant-expression, and we must honor
9762 that, even though it is somewhat more restrictive.
9764 For example:
9766 int i[(2, 3)];
9768 is not a legal declaration, because `(2, 3)' is not a
9769 constant-expression. The `,' operator is forbidden in a
9770 constant-expression. However, GCC's constant-folding machinery
9771 will fold this operation to an INTEGER_CST for `3'. */
9773 /* Save the old settings. */
9774 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9775 saved_allow_non_integral_constant_expression_p
9776 = parser->allow_non_integral_constant_expression_p;
9777 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9778 /* We are now parsing a constant-expression. */
9779 parser->integral_constant_expression_p = true;
9780 parser->allow_non_integral_constant_expression_p
9781 = (allow_non_constant_p || cxx_dialect >= cxx11);
9782 parser->non_integral_constant_expression_p = false;
9783 /* Although the grammar says "conditional-expression", when not STRICT_P,
9784 we parse an "assignment-expression", which also permits
9785 "throw-expression" and the use of assignment operators. In the case
9786 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9787 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9788 actually essential that we look for an assignment-expression.
9789 For example, cp_parser_initializer_clauses uses this function to
9790 determine whether a particular assignment-expression is in fact
9791 constant. */
9792 if (strict_p)
9794 /* Parse the binary expressions (logical-or-expression). */
9795 expression = cp_parser_binary_expression (parser, false, false, false,
9796 PREC_NOT_OPERATOR, NULL);
9797 /* If the next token is a `?' then we're actually looking at
9798 a conditional-expression; otherwise we're done. */
9799 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9800 expression = cp_parser_question_colon_clause (parser, expression);
9802 else
9803 expression = cp_parser_assignment_expression (parser);
9804 /* Restore the old settings. */
9805 parser->integral_constant_expression_p
9806 = saved_integral_constant_expression_p;
9807 parser->allow_non_integral_constant_expression_p
9808 = saved_allow_non_integral_constant_expression_p;
9809 if (cxx_dialect >= cxx11)
9811 /* Require an rvalue constant expression here; that's what our
9812 callers expect. Reference constant expressions are handled
9813 separately in e.g. cp_parser_template_argument. */
9814 tree decay = expression;
9815 if (TREE_TYPE (expression)
9816 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9817 decay = build_address (expression);
9818 bool is_const = potential_rvalue_constant_expression (decay);
9819 parser->non_integral_constant_expression_p = !is_const;
9820 if (!is_const && !allow_non_constant_p)
9821 require_potential_rvalue_constant_expression (decay);
9823 if (allow_non_constant_p)
9824 *non_constant_p = parser->non_integral_constant_expression_p;
9825 parser->non_integral_constant_expression_p
9826 = saved_non_integral_constant_expression_p;
9828 return expression;
9831 /* Parse __builtin_offsetof.
9833 offsetof-expression:
9834 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9836 offsetof-member-designator:
9837 id-expression
9838 | offsetof-member-designator "." id-expression
9839 | offsetof-member-designator "[" expression "]"
9840 | offsetof-member-designator "->" id-expression */
9842 static cp_expr
9843 cp_parser_builtin_offsetof (cp_parser *parser)
9845 int save_ice_p, save_non_ice_p;
9846 tree type;
9847 cp_expr expr;
9848 cp_id_kind dummy;
9849 cp_token *token;
9850 location_t finish_loc;
9852 /* We're about to accept non-integral-constant things, but will
9853 definitely yield an integral constant expression. Save and
9854 restore these values around our local parsing. */
9855 save_ice_p = parser->integral_constant_expression_p;
9856 save_non_ice_p = parser->non_integral_constant_expression_p;
9858 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9860 /* Consume the "__builtin_offsetof" token. */
9861 cp_lexer_consume_token (parser->lexer);
9862 /* Consume the opening `('. */
9863 matching_parens parens;
9864 parens.require_open (parser);
9865 /* Parse the type-id. */
9866 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9867 type = cp_parser_type_id (parser);
9868 /* Look for the `,'. */
9869 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9870 token = cp_lexer_peek_token (parser->lexer);
9872 /* Build the (type *)null that begins the traditional offsetof macro. */
9873 tree object_ptr
9874 = build_static_cast (build_pointer_type (type), null_pointer_node,
9875 tf_warning_or_error);
9877 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9878 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9879 true, &dummy, token->location);
9880 while (true)
9882 token = cp_lexer_peek_token (parser->lexer);
9883 switch (token->type)
9885 case CPP_OPEN_SQUARE:
9886 /* offsetof-member-designator "[" expression "]" */
9887 expr = cp_parser_postfix_open_square_expression (parser, expr,
9888 true, false);
9889 break;
9891 case CPP_DEREF:
9892 /* offsetof-member-designator "->" identifier */
9893 expr = grok_array_decl (token->location, expr,
9894 integer_zero_node, false);
9895 /* FALLTHRU */
9897 case CPP_DOT:
9898 /* offsetof-member-designator "." identifier */
9899 cp_lexer_consume_token (parser->lexer);
9900 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9901 expr, true, &dummy,
9902 token->location);
9903 break;
9905 case CPP_CLOSE_PAREN:
9906 /* Consume the ")" token. */
9907 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9908 cp_lexer_consume_token (parser->lexer);
9909 goto success;
9911 default:
9912 /* Error. We know the following require will fail, but
9913 that gives the proper error message. */
9914 parens.require_close (parser);
9915 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9916 expr = error_mark_node;
9917 goto failure;
9921 success:
9922 /* Make a location of the form:
9923 __builtin_offsetof (struct s, f)
9924 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9925 with caret at the type-id, ranging from the start of the
9926 "_builtin_offsetof" token to the close paren. */
9927 loc = make_location (loc, start_loc, finish_loc);
9928 /* The result will be an INTEGER_CST, so we need to explicitly
9929 preserve the location. */
9930 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9932 failure:
9933 parser->integral_constant_expression_p = save_ice_p;
9934 parser->non_integral_constant_expression_p = save_non_ice_p;
9936 return expr;
9939 /* Parse a trait expression.
9941 Returns a representation of the expression, the underlying type
9942 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9944 static tree
9945 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9947 cp_trait_kind kind;
9948 tree type1, type2 = NULL_TREE;
9949 bool binary = false;
9950 bool variadic = false;
9952 switch (keyword)
9954 case RID_HAS_NOTHROW_ASSIGN:
9955 kind = CPTK_HAS_NOTHROW_ASSIGN;
9956 break;
9957 case RID_HAS_NOTHROW_CONSTRUCTOR:
9958 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9959 break;
9960 case RID_HAS_NOTHROW_COPY:
9961 kind = CPTK_HAS_NOTHROW_COPY;
9962 break;
9963 case RID_HAS_TRIVIAL_ASSIGN:
9964 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9965 break;
9966 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9967 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9968 break;
9969 case RID_HAS_TRIVIAL_COPY:
9970 kind = CPTK_HAS_TRIVIAL_COPY;
9971 break;
9972 case RID_HAS_TRIVIAL_DESTRUCTOR:
9973 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9974 break;
9975 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9976 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9977 break;
9978 case RID_HAS_VIRTUAL_DESTRUCTOR:
9979 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9980 break;
9981 case RID_IS_ABSTRACT:
9982 kind = CPTK_IS_ABSTRACT;
9983 break;
9984 case RID_IS_AGGREGATE:
9985 kind = CPTK_IS_AGGREGATE;
9986 break;
9987 case RID_IS_BASE_OF:
9988 kind = CPTK_IS_BASE_OF;
9989 binary = true;
9990 break;
9991 case RID_IS_CLASS:
9992 kind = CPTK_IS_CLASS;
9993 break;
9994 case RID_IS_EMPTY:
9995 kind = CPTK_IS_EMPTY;
9996 break;
9997 case RID_IS_ENUM:
9998 kind = CPTK_IS_ENUM;
9999 break;
10000 case RID_IS_FINAL:
10001 kind = CPTK_IS_FINAL;
10002 break;
10003 case RID_IS_LITERAL_TYPE:
10004 kind = CPTK_IS_LITERAL_TYPE;
10005 break;
10006 case RID_IS_POD:
10007 kind = CPTK_IS_POD;
10008 break;
10009 case RID_IS_POLYMORPHIC:
10010 kind = CPTK_IS_POLYMORPHIC;
10011 break;
10012 case RID_IS_SAME_AS:
10013 kind = CPTK_IS_SAME_AS;
10014 binary = true;
10015 break;
10016 case RID_IS_STD_LAYOUT:
10017 kind = CPTK_IS_STD_LAYOUT;
10018 break;
10019 case RID_IS_TRIVIAL:
10020 kind = CPTK_IS_TRIVIAL;
10021 break;
10022 case RID_IS_TRIVIALLY_ASSIGNABLE:
10023 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10024 binary = true;
10025 break;
10026 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10027 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10028 variadic = true;
10029 break;
10030 case RID_IS_TRIVIALLY_COPYABLE:
10031 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10032 break;
10033 case RID_IS_UNION:
10034 kind = CPTK_IS_UNION;
10035 break;
10036 case RID_UNDERLYING_TYPE:
10037 kind = CPTK_UNDERLYING_TYPE;
10038 break;
10039 case RID_BASES:
10040 kind = CPTK_BASES;
10041 break;
10042 case RID_DIRECT_BASES:
10043 kind = CPTK_DIRECT_BASES;
10044 break;
10045 case RID_IS_ASSIGNABLE:
10046 kind = CPTK_IS_ASSIGNABLE;
10047 binary = true;
10048 break;
10049 case RID_IS_CONSTRUCTIBLE:
10050 kind = CPTK_IS_CONSTRUCTIBLE;
10051 variadic = true;
10052 break;
10053 default:
10054 gcc_unreachable ();
10057 /* Consume the token. */
10058 cp_lexer_consume_token (parser->lexer);
10060 matching_parens parens;
10061 parens.require_open (parser);
10064 type_id_in_expr_sentinel s (parser);
10065 type1 = cp_parser_type_id (parser);
10068 if (type1 == error_mark_node)
10069 return error_mark_node;
10071 if (binary)
10073 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10076 type_id_in_expr_sentinel s (parser);
10077 type2 = cp_parser_type_id (parser);
10080 if (type2 == error_mark_node)
10081 return error_mark_node;
10083 else if (variadic)
10085 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10087 cp_lexer_consume_token (parser->lexer);
10088 tree elt = cp_parser_type_id (parser);
10089 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10091 cp_lexer_consume_token (parser->lexer);
10092 elt = make_pack_expansion (elt);
10094 if (elt == error_mark_node)
10095 return error_mark_node;
10096 type2 = tree_cons (NULL_TREE, elt, type2);
10100 parens.require_close (parser);
10102 /* Complete the trait expression, which may mean either processing
10103 the trait expr now or saving it for template instantiation. */
10104 switch (kind)
10106 case CPTK_UNDERLYING_TYPE:
10107 return finish_underlying_type (type1);
10108 case CPTK_BASES:
10109 return finish_bases (type1, false);
10110 case CPTK_DIRECT_BASES:
10111 return finish_bases (type1, true);
10112 default:
10113 return finish_trait_expr (kind, type1, type2);
10117 /* Parse a lambda expression.
10119 lambda-expression:
10120 lambda-introducer lambda-declarator [opt] compound-statement
10122 Returns a representation of the expression. */
10124 static cp_expr
10125 cp_parser_lambda_expression (cp_parser* parser)
10127 tree lambda_expr = build_lambda_expr ();
10128 tree type;
10129 bool ok = true;
10130 cp_token *token = cp_lexer_peek_token (parser->lexer);
10131 cp_token_position start = 0;
10133 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10135 if (cp_unevaluated_operand)
10137 if (!token->error_reported)
10139 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10140 "lambda-expression in unevaluated context");
10141 token->error_reported = true;
10143 ok = false;
10145 else if (parser->in_template_argument_list_p)
10147 if (!token->error_reported)
10149 error_at (token->location, "lambda-expression in template-argument");
10150 token->error_reported = true;
10152 ok = false;
10155 /* We may be in the middle of deferred access check. Disable
10156 it now. */
10157 push_deferring_access_checks (dk_no_deferred);
10159 cp_parser_lambda_introducer (parser, lambda_expr);
10161 type = begin_lambda_type (lambda_expr);
10162 if (type == error_mark_node)
10163 return error_mark_node;
10165 record_lambda_scope (lambda_expr);
10167 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10168 determine_visibility (TYPE_NAME (type));
10170 /* Now that we've started the type, add the capture fields for any
10171 explicit captures. */
10172 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10175 /* Inside the class, surrounding template-parameter-lists do not apply. */
10176 unsigned int saved_num_template_parameter_lists
10177 = parser->num_template_parameter_lists;
10178 unsigned char in_statement = parser->in_statement;
10179 bool in_switch_statement_p = parser->in_switch_statement_p;
10180 bool fully_implicit_function_template_p
10181 = parser->fully_implicit_function_template_p;
10182 tree implicit_template_parms = parser->implicit_template_parms;
10183 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10184 bool auto_is_implicit_function_template_parm_p
10185 = parser->auto_is_implicit_function_template_parm_p;
10187 parser->num_template_parameter_lists = 0;
10188 parser->in_statement = 0;
10189 parser->in_switch_statement_p = false;
10190 parser->fully_implicit_function_template_p = false;
10191 parser->implicit_template_parms = 0;
10192 parser->implicit_template_scope = 0;
10193 parser->auto_is_implicit_function_template_parm_p = false;
10195 /* By virtue of defining a local class, a lambda expression has access to
10196 the private variables of enclosing classes. */
10198 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10200 if (ok && cp_parser_error_occurred (parser))
10201 ok = false;
10203 if (ok)
10205 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10206 && cp_parser_start_tentative_firewall (parser))
10207 start = token;
10208 cp_parser_lambda_body (parser, lambda_expr);
10210 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10212 if (cp_parser_skip_to_closing_brace (parser))
10213 cp_lexer_consume_token (parser->lexer);
10216 /* The capture list was built up in reverse order; fix that now. */
10217 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10218 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10220 if (ok)
10221 maybe_add_lambda_conv_op (type);
10223 type = finish_struct (type, /*attributes=*/NULL_TREE);
10225 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10226 parser->in_statement = in_statement;
10227 parser->in_switch_statement_p = in_switch_statement_p;
10228 parser->fully_implicit_function_template_p
10229 = fully_implicit_function_template_p;
10230 parser->implicit_template_parms = implicit_template_parms;
10231 parser->implicit_template_scope = implicit_template_scope;
10232 parser->auto_is_implicit_function_template_parm_p
10233 = auto_is_implicit_function_template_parm_p;
10236 /* This field is only used during parsing of the lambda. */
10237 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10239 /* This lambda shouldn't have any proxies left at this point. */
10240 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10241 /* And now that we're done, push proxies for an enclosing lambda. */
10242 insert_pending_capture_proxies ();
10244 if (ok)
10245 lambda_expr = build_lambda_object (lambda_expr);
10246 else
10247 lambda_expr = error_mark_node;
10249 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10251 pop_deferring_access_checks ();
10253 return lambda_expr;
10256 /* Parse the beginning of a lambda expression.
10258 lambda-introducer:
10259 [ lambda-capture [opt] ]
10261 LAMBDA_EXPR is the current representation of the lambda expression. */
10263 static void
10264 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10266 /* Need commas after the first capture. */
10267 bool first = true;
10269 /* Eat the leading `['. */
10270 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10272 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10273 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10274 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10275 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10276 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10277 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10279 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10281 cp_lexer_consume_token (parser->lexer);
10282 first = false;
10285 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10287 cp_token* capture_token;
10288 tree capture_id;
10289 tree capture_init_expr;
10290 cp_id_kind idk = CP_ID_KIND_NONE;
10291 bool explicit_init_p = false;
10293 enum capture_kind_type
10295 BY_COPY,
10296 BY_REFERENCE
10298 enum capture_kind_type capture_kind = BY_COPY;
10300 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10302 error ("expected end of capture-list");
10303 return;
10306 if (first)
10307 first = false;
10308 else
10309 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10311 /* Possibly capture `this'. */
10312 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10314 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10315 if (cxx_dialect < cxx2a
10316 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10317 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10318 "with by-copy capture default");
10319 cp_lexer_consume_token (parser->lexer);
10320 add_capture (lambda_expr,
10321 /*id=*/this_identifier,
10322 /*initializer=*/finish_this_expr (),
10323 /*by_reference_p=*/true,
10324 explicit_init_p);
10325 continue;
10328 /* Possibly capture `*this'. */
10329 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10330 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10332 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10333 if (cxx_dialect < cxx17)
10334 pedwarn (loc, 0, "%<*this%> capture only available with "
10335 "-std=c++17 or -std=gnu++17");
10336 cp_lexer_consume_token (parser->lexer);
10337 cp_lexer_consume_token (parser->lexer);
10338 add_capture (lambda_expr,
10339 /*id=*/this_identifier,
10340 /*initializer=*/finish_this_expr (),
10341 /*by_reference_p=*/false,
10342 explicit_init_p);
10343 continue;
10346 /* Remember whether we want to capture as a reference or not. */
10347 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10349 capture_kind = BY_REFERENCE;
10350 cp_lexer_consume_token (parser->lexer);
10353 /* Get the identifier. */
10354 capture_token = cp_lexer_peek_token (parser->lexer);
10355 capture_id = cp_parser_identifier (parser);
10357 if (capture_id == error_mark_node)
10358 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10359 delimiters, but I modified this to stop on unnested ']' as well. It
10360 was already changed to stop on unnested '}', so the
10361 "closing_parenthesis" name is no more misleading with my change. */
10363 cp_parser_skip_to_closing_parenthesis (parser,
10364 /*recovering=*/true,
10365 /*or_comma=*/true,
10366 /*consume_paren=*/true);
10367 break;
10370 /* Find the initializer for this capture. */
10371 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10372 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10373 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10375 bool direct, non_constant;
10376 /* An explicit initializer exists. */
10377 if (cxx_dialect < cxx14)
10378 pedwarn (input_location, 0,
10379 "lambda capture initializers "
10380 "only available with -std=c++14 or -std=gnu++14");
10381 capture_init_expr = cp_parser_initializer (parser, &direct,
10382 &non_constant);
10383 explicit_init_p = true;
10384 if (capture_init_expr == NULL_TREE)
10386 error ("empty initializer for lambda init-capture");
10387 capture_init_expr = error_mark_node;
10390 else
10392 const char* error_msg;
10394 /* Turn the identifier into an id-expression. */
10395 capture_init_expr
10396 = cp_parser_lookup_name_simple (parser, capture_id,
10397 capture_token->location);
10399 if (capture_init_expr == error_mark_node)
10401 unqualified_name_lookup_error (capture_id);
10402 continue;
10404 else if (DECL_P (capture_init_expr)
10405 && (!VAR_P (capture_init_expr)
10406 && TREE_CODE (capture_init_expr) != PARM_DECL))
10408 error_at (capture_token->location,
10409 "capture of non-variable %qD ",
10410 capture_init_expr);
10411 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10412 "%q#D declared here", capture_init_expr);
10413 continue;
10415 if (VAR_P (capture_init_expr)
10416 && decl_storage_duration (capture_init_expr) != dk_auto)
10418 if (pedwarn (capture_token->location, 0, "capture of variable "
10419 "%qD with non-automatic storage duration",
10420 capture_init_expr))
10421 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10422 "%q#D declared here", capture_init_expr);
10423 continue;
10426 capture_init_expr
10427 = finish_id_expression
10428 (capture_id,
10429 capture_init_expr,
10430 parser->scope,
10431 &idk,
10432 /*integral_constant_expression_p=*/false,
10433 /*allow_non_integral_constant_expression_p=*/false,
10434 /*non_integral_constant_expression_p=*/NULL,
10435 /*template_p=*/false,
10436 /*done=*/true,
10437 /*address_p=*/false,
10438 /*template_arg_p=*/false,
10439 &error_msg,
10440 capture_token->location);
10442 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10444 cp_lexer_consume_token (parser->lexer);
10445 capture_init_expr = make_pack_expansion (capture_init_expr);
10447 else
10448 check_for_bare_parameter_packs (capture_init_expr);
10451 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10452 && !explicit_init_p)
10454 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10455 && capture_kind == BY_COPY)
10456 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10457 "of %qD redundant with by-copy capture default",
10458 capture_id);
10459 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10460 && capture_kind == BY_REFERENCE)
10461 pedwarn (capture_token->location, 0, "explicit by-reference "
10462 "capture of %qD redundant with by-reference capture "
10463 "default", capture_id);
10466 add_capture (lambda_expr,
10467 capture_id,
10468 capture_init_expr,
10469 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10470 explicit_init_p);
10473 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10476 /* Parse the (optional) middle of a lambda expression.
10478 lambda-declarator:
10479 < template-parameter-list [opt] >
10480 ( parameter-declaration-clause [opt] )
10481 attribute-specifier [opt]
10482 decl-specifier-seq [opt]
10483 exception-specification [opt]
10484 lambda-return-type-clause [opt]
10486 LAMBDA_EXPR is the current representation of the lambda expression. */
10488 static bool
10489 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10491 /* 5.1.1.4 of the standard says:
10492 If a lambda-expression does not include a lambda-declarator, it is as if
10493 the lambda-declarator were ().
10494 This means an empty parameter list, no attributes, and no exception
10495 specification. */
10496 tree param_list = void_list_node;
10497 tree attributes = NULL_TREE;
10498 tree exception_spec = NULL_TREE;
10499 tree template_param_list = NULL_TREE;
10500 tree tx_qual = NULL_TREE;
10501 tree return_type = NULL_TREE;
10502 cp_decl_specifier_seq lambda_specs;
10503 clear_decl_specs (&lambda_specs);
10505 /* The template-parameter-list is optional, but must begin with
10506 an opening angle if present. */
10507 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10509 if (cxx_dialect < cxx14)
10510 pedwarn (parser->lexer->next_token->location, 0,
10511 "lambda templates are only available with "
10512 "-std=c++14 or -std=gnu++14");
10513 else
10514 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10515 "ISO C++ does not support lambda templates");
10517 cp_lexer_consume_token (parser->lexer);
10519 template_param_list = cp_parser_template_parameter_list (parser);
10521 cp_parser_skip_to_end_of_template_parameter_list (parser);
10523 /* We just processed one more parameter list. */
10524 ++parser->num_template_parameter_lists;
10527 /* The parameter-declaration-clause is optional (unless
10528 template-parameter-list was given), but must begin with an
10529 opening parenthesis if present. */
10530 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10532 matching_parens parens;
10533 parens.consume_open (parser);
10535 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10537 /* Parse parameters. */
10538 param_list = cp_parser_parameter_declaration_clause (parser);
10540 /* Default arguments shall not be specified in the
10541 parameter-declaration-clause of a lambda-declarator. */
10542 if (cxx_dialect < cxx14)
10543 for (tree t = param_list; t; t = TREE_CHAIN (t))
10544 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10545 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10546 "default argument specified for lambda parameter");
10548 parens.require_close (parser);
10550 attributes = cp_parser_attributes_opt (parser);
10552 /* In the decl-specifier-seq of the lambda-declarator, each
10553 decl-specifier shall either be mutable or constexpr. */
10554 int declares_class_or_enum;
10555 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10556 cp_parser_decl_specifier_seq (parser,
10557 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10558 &lambda_specs, &declares_class_or_enum);
10559 if (lambda_specs.storage_class == sc_mutable)
10561 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10562 if (lambda_specs.conflicting_specifiers_p)
10563 error_at (lambda_specs.locations[ds_storage_class],
10564 "duplicate %<mutable%>");
10567 tx_qual = cp_parser_tx_qualifier_opt (parser);
10569 /* Parse optional exception specification. */
10570 exception_spec = cp_parser_exception_specification_opt (parser);
10572 /* Parse optional trailing return type. */
10573 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10575 cp_lexer_consume_token (parser->lexer);
10576 return_type = cp_parser_trailing_type_id (parser);
10579 /* The function parameters must be in scope all the way until after the
10580 trailing-return-type in case of decltype. */
10581 pop_bindings_and_leave_scope ();
10583 else if (template_param_list != NULL_TREE) // generate diagnostic
10584 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10586 /* Create the function call operator.
10588 Messing with declarators like this is no uglier than building up the
10589 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10590 other code. */
10592 cp_decl_specifier_seq return_type_specs;
10593 cp_declarator* declarator;
10594 tree fco;
10595 int quals;
10596 void *p;
10598 clear_decl_specs (&return_type_specs);
10599 if (return_type)
10600 return_type_specs.type = return_type;
10601 else
10602 /* Maybe we will deduce the return type later. */
10603 return_type_specs.type = make_auto ();
10605 if (lambda_specs.locations[ds_constexpr])
10607 if (cxx_dialect >= cxx17)
10608 return_type_specs.locations[ds_constexpr]
10609 = lambda_specs.locations[ds_constexpr];
10610 else
10611 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10612 "lambda only available with -std=c++17 or -std=gnu++17");
10615 p = obstack_alloc (&declarator_obstack, 0);
10617 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10619 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10620 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10621 declarator = make_call_declarator (declarator, param_list, quals,
10622 VIRT_SPEC_UNSPECIFIED,
10623 REF_QUAL_NONE,
10624 tx_qual,
10625 exception_spec,
10626 /*late_return_type=*/NULL_TREE,
10627 /*requires_clause*/NULL_TREE);
10628 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10630 fco = grokmethod (&return_type_specs,
10631 declarator,
10632 attributes);
10633 if (fco != error_mark_node)
10635 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10636 DECL_ARTIFICIAL (fco) = 1;
10637 /* Give the object parameter a different name. */
10638 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10639 if (return_type)
10640 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10642 if (template_param_list)
10644 fco = finish_member_template_decl (fco);
10645 finish_template_decl (template_param_list);
10646 --parser->num_template_parameter_lists;
10648 else if (parser->fully_implicit_function_template_p)
10649 fco = finish_fully_implicit_template (parser, fco);
10651 finish_member_declaration (fco);
10653 obstack_free (&declarator_obstack, p);
10655 return (fco != error_mark_node);
10659 /* Parse the body of a lambda expression, which is simply
10661 compound-statement
10663 but which requires special handling.
10664 LAMBDA_EXPR is the current representation of the lambda expression. */
10666 static void
10667 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10669 bool nested = (current_function_decl != NULL_TREE);
10670 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10671 bool in_function_body = parser->in_function_body;
10672 if (nested)
10673 push_function_context ();
10674 else
10675 /* Still increment function_depth so that we don't GC in the
10676 middle of an expression. */
10677 ++function_depth;
10678 vec<tree> omp_privatization_save;
10679 save_omp_privatization_clauses (omp_privatization_save);
10680 /* Clear this in case we're in the middle of a default argument. */
10681 parser->local_variables_forbidden_p = false;
10682 parser->in_function_body = true;
10684 /* Finish the function call operator
10685 - class_specifier
10686 + late_parsing_for_member
10687 + function_definition_after_declarator
10688 + ctor_initializer_opt_and_function_body */
10690 local_specialization_stack s (lss_copy);
10692 tree fco = lambda_function (lambda_expr);
10693 tree body = start_lambda_function (fco, lambda_expr);
10694 bool done = false;
10695 tree compound_stmt;
10697 matching_braces braces;
10698 if (!braces.require_open (parser))
10699 goto out;
10701 compound_stmt = begin_compound_stmt (0);
10703 /* 5.1.1.4 of the standard says:
10704 If a lambda-expression does not include a trailing-return-type, it
10705 is as if the trailing-return-type denotes the following type:
10706 * if the compound-statement is of the form
10707 { return attribute-specifier [opt] expression ; }
10708 the type of the returned expression after lvalue-to-rvalue
10709 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10710 (_conv.array_ 4.2), and function-to-pointer conversion
10711 (_conv.func_ 4.3);
10712 * otherwise, void. */
10714 /* In a lambda that has neither a lambda-return-type-clause
10715 nor a deducible form, errors should be reported for return statements
10716 in the body. Since we used void as the placeholder return type, parsing
10717 the body as usual will give such desired behavior. */
10718 if (is_auto (TREE_TYPE (TREE_TYPE (fco)))
10719 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10720 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10722 tree expr = NULL_TREE;
10723 cp_id_kind idk = CP_ID_KIND_NONE;
10725 /* Parse tentatively in case there's more after the initial return
10726 statement. */
10727 cp_parser_parse_tentatively (parser);
10729 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10731 expr = cp_parser_expression (parser, &idk);
10733 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10734 braces.require_close (parser);
10736 if (cp_parser_parse_definitely (parser))
10738 if (!processing_template_decl)
10740 tree type = lambda_return_type (expr);
10741 apply_deduced_return_type (fco, type);
10742 if (type == error_mark_node)
10743 expr = error_mark_node;
10746 /* Will get error here if type not deduced yet. */
10747 finish_return_stmt (expr);
10749 done = true;
10753 if (!done)
10755 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10756 cp_parser_label_declaration (parser);
10757 cp_parser_statement_seq_opt (parser, NULL_TREE);
10758 braces.require_close (parser);
10761 finish_compound_stmt (compound_stmt);
10763 out:
10764 finish_lambda_function (body);
10767 restore_omp_privatization_clauses (omp_privatization_save);
10768 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10769 parser->in_function_body = in_function_body;
10770 if (nested)
10771 pop_function_context();
10772 else
10773 --function_depth;
10776 /* Statements [gram.stmt.stmt] */
10778 /* Parse a statement.
10780 statement:
10781 labeled-statement
10782 expression-statement
10783 compound-statement
10784 selection-statement
10785 iteration-statement
10786 jump-statement
10787 declaration-statement
10788 try-block
10790 C++11:
10792 statement:
10793 labeled-statement
10794 attribute-specifier-seq (opt) expression-statement
10795 attribute-specifier-seq (opt) compound-statement
10796 attribute-specifier-seq (opt) selection-statement
10797 attribute-specifier-seq (opt) iteration-statement
10798 attribute-specifier-seq (opt) jump-statement
10799 declaration-statement
10800 attribute-specifier-seq (opt) try-block
10802 init-statement:
10803 expression-statement
10804 simple-declaration
10806 TM Extension:
10808 statement:
10809 atomic-statement
10811 IN_COMPOUND is true when the statement is nested inside a
10812 cp_parser_compound_statement; this matters for certain pragmas.
10814 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10815 is a (possibly labeled) if statement which is not enclosed in braces
10816 and has an else clause. This is used to implement -Wparentheses.
10818 CHAIN is a vector of if-else-if conditions. */
10820 static void
10821 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10822 bool in_compound, bool *if_p, vec<tree> *chain,
10823 location_t *loc_after_labels)
10825 tree statement, std_attrs = NULL_TREE;
10826 cp_token *token;
10827 location_t statement_location, attrs_location;
10829 restart:
10830 if (if_p != NULL)
10831 *if_p = false;
10832 /* There is no statement yet. */
10833 statement = NULL_TREE;
10835 saved_token_sentinel saved_tokens (parser->lexer);
10836 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10837 if (c_dialect_objc ())
10838 /* In obj-c++, seeing '[[' might be the either the beginning of
10839 c++11 attributes, or a nested objc-message-expression. So
10840 let's parse the c++11 attributes tentatively. */
10841 cp_parser_parse_tentatively (parser);
10842 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10843 if (c_dialect_objc ())
10845 if (!cp_parser_parse_definitely (parser))
10846 std_attrs = NULL_TREE;
10849 /* Peek at the next token. */
10850 token = cp_lexer_peek_token (parser->lexer);
10851 /* Remember the location of the first token in the statement. */
10852 statement_location = token->location;
10853 /* If this is a keyword, then that will often determine what kind of
10854 statement we have. */
10855 if (token->type == CPP_KEYWORD)
10857 enum rid keyword = token->keyword;
10859 switch (keyword)
10861 case RID_CASE:
10862 case RID_DEFAULT:
10863 /* Looks like a labeled-statement with a case label.
10864 Parse the label, and then use tail recursion to parse
10865 the statement. */
10866 cp_parser_label_for_labeled_statement (parser, std_attrs);
10867 in_compound = false;
10868 goto restart;
10870 case RID_IF:
10871 case RID_SWITCH:
10872 statement = cp_parser_selection_statement (parser, if_p, chain);
10873 break;
10875 case RID_WHILE:
10876 case RID_DO:
10877 case RID_FOR:
10878 statement = cp_parser_iteration_statement (parser, if_p, false);
10879 break;
10881 case RID_CILK_FOR:
10882 if (!flag_cilkplus)
10884 error_at (cp_lexer_peek_token (parser->lexer)->location,
10885 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10886 cp_lexer_consume_token (parser->lexer);
10887 statement = error_mark_node;
10889 else
10890 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10891 break;
10893 case RID_BREAK:
10894 case RID_CONTINUE:
10895 case RID_RETURN:
10896 case RID_GOTO:
10897 statement = cp_parser_jump_statement (parser);
10898 break;
10900 case RID_CILK_SYNC:
10901 cp_lexer_consume_token (parser->lexer);
10902 if (flag_cilkplus)
10904 tree sync_expr = build_cilk_sync ();
10905 SET_EXPR_LOCATION (sync_expr,
10906 token->location);
10907 statement = finish_expr_stmt (sync_expr);
10909 else
10911 error_at (token->location, "-fcilkplus must be enabled to use"
10912 " %<_Cilk_sync%>");
10913 statement = error_mark_node;
10915 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10916 break;
10918 /* Objective-C++ exception-handling constructs. */
10919 case RID_AT_TRY:
10920 case RID_AT_CATCH:
10921 case RID_AT_FINALLY:
10922 case RID_AT_SYNCHRONIZED:
10923 case RID_AT_THROW:
10924 statement = cp_parser_objc_statement (parser);
10925 break;
10927 case RID_TRY:
10928 statement = cp_parser_try_block (parser);
10929 break;
10931 case RID_NAMESPACE:
10932 /* This must be a namespace alias definition. */
10933 cp_parser_declaration_statement (parser);
10934 return;
10936 case RID_TRANSACTION_ATOMIC:
10937 case RID_TRANSACTION_RELAXED:
10938 case RID_SYNCHRONIZED:
10939 case RID_ATOMIC_NOEXCEPT:
10940 case RID_ATOMIC_CANCEL:
10941 statement = cp_parser_transaction (parser, token);
10942 break;
10943 case RID_TRANSACTION_CANCEL:
10944 statement = cp_parser_transaction_cancel (parser);
10945 break;
10947 default:
10948 /* It might be a keyword like `int' that can start a
10949 declaration-statement. */
10950 break;
10953 else if (token->type == CPP_NAME)
10955 /* If the next token is a `:', then we are looking at a
10956 labeled-statement. */
10957 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10958 if (token->type == CPP_COLON)
10960 /* Looks like a labeled-statement with an ordinary label.
10961 Parse the label, and then use tail recursion to parse
10962 the statement. */
10964 cp_parser_label_for_labeled_statement (parser, std_attrs);
10965 in_compound = false;
10966 goto restart;
10969 /* Anything that starts with a `{' must be a compound-statement. */
10970 else if (token->type == CPP_OPEN_BRACE)
10971 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10972 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10973 a statement all its own. */
10974 else if (token->type == CPP_PRAGMA)
10976 /* Only certain OpenMP pragmas are attached to statements, and thus
10977 are considered statements themselves. All others are not. In
10978 the context of a compound, accept the pragma as a "statement" and
10979 return so that we can check for a close brace. Otherwise we
10980 require a real statement and must go back and read one. */
10981 if (in_compound)
10982 cp_parser_pragma (parser, pragma_compound, if_p);
10983 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10984 goto restart;
10985 return;
10987 else if (token->type == CPP_EOF)
10989 cp_parser_error (parser, "expected statement");
10990 return;
10993 /* Everything else must be a declaration-statement or an
10994 expression-statement. Try for the declaration-statement
10995 first, unless we are looking at a `;', in which case we know that
10996 we have an expression-statement. */
10997 if (!statement)
10999 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11001 if (std_attrs != NULL_TREE)
11003 /* Attributes should be parsed as part of the the
11004 declaration, so let's un-parse them. */
11005 saved_tokens.rollback();
11006 std_attrs = NULL_TREE;
11009 cp_parser_parse_tentatively (parser);
11010 /* Try to parse the declaration-statement. */
11011 cp_parser_declaration_statement (parser);
11012 /* If that worked, we're done. */
11013 if (cp_parser_parse_definitely (parser))
11014 return;
11016 /* All preceding labels have been parsed at this point. */
11017 if (loc_after_labels != NULL)
11018 *loc_after_labels = statement_location;
11020 /* Look for an expression-statement instead. */
11021 statement = cp_parser_expression_statement (parser, in_statement_expr);
11023 /* Handle [[fallthrough]];. */
11024 if (attribute_fallthrough_p (std_attrs))
11026 /* The next token after the fallthrough attribute is ';'. */
11027 if (statement == NULL_TREE)
11029 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11030 statement = build_call_expr_internal_loc (statement_location,
11031 IFN_FALLTHROUGH,
11032 void_type_node, 0);
11033 finish_expr_stmt (statement);
11035 else
11036 warning_at (statement_location, OPT_Wattributes,
11037 "%<fallthrough%> attribute not followed by %<;%>");
11038 std_attrs = NULL_TREE;
11042 /* Set the line number for the statement. */
11043 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11044 SET_EXPR_LOCATION (statement, statement_location);
11046 /* Allow "[[fallthrough]];", but warn otherwise. */
11047 if (std_attrs != NULL_TREE)
11048 warning_at (attrs_location,
11049 OPT_Wattributes,
11050 "attributes at the beginning of statement are ignored");
11053 /* Parse the label for a labeled-statement, i.e.
11055 identifier :
11056 case constant-expression :
11057 default :
11059 GNU Extension:
11060 case constant-expression ... constant-expression : statement
11062 When a label is parsed without errors, the label is added to the
11063 parse tree by the finish_* functions, so this function doesn't
11064 have to return the label. */
11066 static void
11067 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11069 cp_token *token;
11070 tree label = NULL_TREE;
11071 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11073 /* The next token should be an identifier. */
11074 token = cp_lexer_peek_token (parser->lexer);
11075 if (token->type != CPP_NAME
11076 && token->type != CPP_KEYWORD)
11078 cp_parser_error (parser, "expected labeled-statement");
11079 return;
11082 /* Remember whether this case or a user-defined label is allowed to fall
11083 through to. */
11084 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11086 parser->colon_corrects_to_scope_p = false;
11087 switch (token->keyword)
11089 case RID_CASE:
11091 tree expr, expr_hi;
11092 cp_token *ellipsis;
11094 /* Consume the `case' token. */
11095 cp_lexer_consume_token (parser->lexer);
11096 /* Parse the constant-expression. */
11097 expr = cp_parser_constant_expression (parser);
11098 if (check_for_bare_parameter_packs (expr))
11099 expr = error_mark_node;
11101 ellipsis = cp_lexer_peek_token (parser->lexer);
11102 if (ellipsis->type == CPP_ELLIPSIS)
11104 /* Consume the `...' token. */
11105 cp_lexer_consume_token (parser->lexer);
11106 expr_hi = cp_parser_constant_expression (parser);
11107 if (check_for_bare_parameter_packs (expr_hi))
11108 expr_hi = error_mark_node;
11110 /* We don't need to emit warnings here, as the common code
11111 will do this for us. */
11113 else
11114 expr_hi = NULL_TREE;
11116 if (parser->in_switch_statement_p)
11118 tree l = finish_case_label (token->location, expr, expr_hi);
11119 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11120 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11122 else
11123 error_at (token->location,
11124 "case label %qE not within a switch statement",
11125 expr);
11127 break;
11129 case RID_DEFAULT:
11130 /* Consume the `default' token. */
11131 cp_lexer_consume_token (parser->lexer);
11133 if (parser->in_switch_statement_p)
11135 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11136 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11137 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11139 else
11140 error_at (token->location, "case label not within a switch statement");
11141 break;
11143 default:
11144 /* Anything else must be an ordinary label. */
11145 label = finish_label_stmt (cp_parser_identifier (parser));
11146 if (label && TREE_CODE (label) == LABEL_DECL)
11147 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11148 break;
11151 /* Require the `:' token. */
11152 cp_parser_require (parser, CPP_COLON, RT_COLON);
11154 /* An ordinary label may optionally be followed by attributes.
11155 However, this is only permitted if the attributes are then
11156 followed by a semicolon. This is because, for backward
11157 compatibility, when parsing
11158 lab: __attribute__ ((unused)) int i;
11159 we want the attribute to attach to "i", not "lab". */
11160 if (label != NULL_TREE
11161 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11163 tree attrs;
11164 cp_parser_parse_tentatively (parser);
11165 attrs = cp_parser_gnu_attributes_opt (parser);
11166 if (attrs == NULL_TREE
11167 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11168 cp_parser_abort_tentative_parse (parser);
11169 else if (!cp_parser_parse_definitely (parser))
11171 else
11172 attributes = chainon (attributes, attrs);
11175 if (attributes != NULL_TREE)
11176 cplus_decl_attributes (&label, attributes, 0);
11178 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11181 /* Parse an expression-statement.
11183 expression-statement:
11184 expression [opt] ;
11186 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11187 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11188 indicates whether this expression-statement is part of an
11189 expression statement. */
11191 static tree
11192 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11194 tree statement = NULL_TREE;
11195 cp_token *token = cp_lexer_peek_token (parser->lexer);
11196 location_t loc = token->location;
11198 /* There might be attribute fallthrough. */
11199 tree attr = cp_parser_gnu_attributes_opt (parser);
11201 /* If the next token is a ';', then there is no expression
11202 statement. */
11203 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11205 statement = cp_parser_expression (parser);
11206 if (statement == error_mark_node
11207 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11209 cp_parser_skip_to_end_of_block_or_statement (parser);
11210 return error_mark_node;
11214 /* Handle [[fallthrough]];. */
11215 if (attribute_fallthrough_p (attr))
11217 /* The next token after the fallthrough attribute is ';'. */
11218 if (statement == NULL_TREE)
11219 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11220 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11221 void_type_node, 0);
11222 else
11223 warning_at (loc, OPT_Wattributes,
11224 "%<fallthrough%> attribute not followed by %<;%>");
11225 attr = NULL_TREE;
11228 /* Allow "[[fallthrough]];", but warn otherwise. */
11229 if (attr != NULL_TREE)
11230 warning_at (loc, OPT_Wattributes,
11231 "attributes at the beginning of statement are ignored");
11233 /* Give a helpful message for "A<T>::type t;" and the like. */
11234 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11235 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11237 if (TREE_CODE (statement) == SCOPE_REF)
11238 error_at (token->location, "need %<typename%> before %qE because "
11239 "%qT is a dependent scope",
11240 statement, TREE_OPERAND (statement, 0));
11241 else if (is_overloaded_fn (statement)
11242 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11244 /* A::A a; */
11245 tree fn = get_first_fn (statement);
11246 error_at (token->location,
11247 "%<%T::%D%> names the constructor, not the type",
11248 DECL_CONTEXT (fn), DECL_NAME (fn));
11252 /* Consume the final `;'. */
11253 cp_parser_consume_semicolon_at_end_of_statement (parser);
11255 if (in_statement_expr
11256 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11257 /* This is the final expression statement of a statement
11258 expression. */
11259 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11260 else if (statement)
11261 statement = finish_expr_stmt (statement);
11263 return statement;
11266 /* Parse a compound-statement.
11268 compound-statement:
11269 { statement-seq [opt] }
11271 GNU extension:
11273 compound-statement:
11274 { label-declaration-seq [opt] statement-seq [opt] }
11276 label-declaration-seq:
11277 label-declaration
11278 label-declaration-seq label-declaration
11280 Returns a tree representing the statement. */
11282 static tree
11283 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11284 int bcs_flags, bool function_body)
11286 tree compound_stmt;
11287 matching_braces braces;
11289 /* Consume the `{'. */
11290 if (!braces.require_open (parser))
11291 return error_mark_node;
11292 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11293 && !function_body && cxx_dialect < cxx14)
11294 pedwarn (input_location, OPT_Wpedantic,
11295 "compound-statement in constexpr function");
11296 /* Begin the compound-statement. */
11297 compound_stmt = begin_compound_stmt (bcs_flags);
11298 /* If the next keyword is `__label__' we have a label declaration. */
11299 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11300 cp_parser_label_declaration (parser);
11301 /* Parse an (optional) statement-seq. */
11302 cp_parser_statement_seq_opt (parser, in_statement_expr);
11303 /* Finish the compound-statement. */
11304 finish_compound_stmt (compound_stmt);
11305 /* Consume the `}'. */
11306 braces.require_close (parser);
11308 return compound_stmt;
11311 /* Parse an (optional) statement-seq.
11313 statement-seq:
11314 statement
11315 statement-seq [opt] statement */
11317 static void
11318 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11320 /* Scan statements until there aren't any more. */
11321 while (true)
11323 cp_token *token = cp_lexer_peek_token (parser->lexer);
11325 /* If we are looking at a `}', then we have run out of
11326 statements; the same is true if we have reached the end
11327 of file, or have stumbled upon a stray '@end'. */
11328 if (token->type == CPP_CLOSE_BRACE
11329 || token->type == CPP_EOF
11330 || token->type == CPP_PRAGMA_EOL
11331 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11332 break;
11334 /* If we are in a compound statement and find 'else' then
11335 something went wrong. */
11336 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11338 if (parser->in_statement & IN_IF_STMT)
11339 break;
11340 else
11342 token = cp_lexer_consume_token (parser->lexer);
11343 error_at (token->location, "%<else%> without a previous %<if%>");
11347 /* Parse the statement. */
11348 cp_parser_statement (parser, in_statement_expr, true, NULL);
11352 /* Return true if we're looking at (init; cond), false otherwise. */
11354 static bool
11355 cp_parser_init_statement_p (cp_parser *parser)
11357 /* Save tokens so that we can put them back. */
11358 cp_lexer_save_tokens (parser->lexer);
11360 /* Look for ';' that is not nested in () or {}. */
11361 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11362 /*recovering=*/false,
11363 CPP_SEMICOLON,
11364 /*consume_paren=*/false);
11366 /* Roll back the tokens we skipped. */
11367 cp_lexer_rollback_tokens (parser->lexer);
11369 return ret == -1;
11372 /* Parse a selection-statement.
11374 selection-statement:
11375 if ( init-statement [opt] condition ) statement
11376 if ( init-statement [opt] condition ) statement else statement
11377 switch ( init-statement [opt] condition ) statement
11379 Returns the new IF_STMT or SWITCH_STMT.
11381 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11382 is a (possibly labeled) if statement which is not enclosed in
11383 braces and has an else clause. This is used to implement
11384 -Wparentheses.
11386 CHAIN is a vector of if-else-if conditions. This is used to implement
11387 -Wduplicated-cond. */
11389 static tree
11390 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11391 vec<tree> *chain)
11393 cp_token *token;
11394 enum rid keyword;
11395 token_indent_info guard_tinfo;
11397 if (if_p != NULL)
11398 *if_p = false;
11400 /* Peek at the next token. */
11401 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11402 guard_tinfo = get_token_indent_info (token);
11404 /* See what kind of keyword it is. */
11405 keyword = token->keyword;
11406 switch (keyword)
11408 case RID_IF:
11409 case RID_SWITCH:
11411 tree statement;
11412 tree condition;
11414 bool cx = false;
11415 if (keyword == RID_IF
11416 && cp_lexer_next_token_is_keyword (parser->lexer,
11417 RID_CONSTEXPR))
11419 cx = true;
11420 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11421 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11422 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11423 "with -std=c++17 or -std=gnu++17");
11426 /* Look for the `('. */
11427 matching_parens parens;
11428 if (!parens.require_open (parser))
11430 cp_parser_skip_to_end_of_statement (parser);
11431 return error_mark_node;
11434 /* Begin the selection-statement. */
11435 if (keyword == RID_IF)
11437 statement = begin_if_stmt ();
11438 IF_STMT_CONSTEXPR_P (statement) = cx;
11440 else
11441 statement = begin_switch_stmt ();
11443 /* Parse the optional init-statement. */
11444 if (cp_parser_init_statement_p (parser))
11446 tree decl;
11447 if (cxx_dialect < cxx17)
11448 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11449 "init-statement in selection statements only available "
11450 "with -std=c++17 or -std=gnu++17");
11451 cp_parser_init_statement (parser, &decl);
11454 /* Parse the condition. */
11455 condition = cp_parser_condition (parser);
11456 /* Look for the `)'. */
11457 if (!parens.require_close (parser))
11458 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11459 /*consume_paren=*/true);
11461 if (keyword == RID_IF)
11463 bool nested_if;
11464 unsigned char in_statement;
11466 /* Add the condition. */
11467 condition = finish_if_stmt_cond (condition, statement);
11469 if (warn_duplicated_cond)
11470 warn_duplicated_cond_add_or_warn (token->location, condition,
11471 &chain);
11473 /* Parse the then-clause. */
11474 in_statement = parser->in_statement;
11475 parser->in_statement |= IN_IF_STMT;
11477 /* Outside a template, the non-selected branch of a constexpr
11478 if is a 'discarded statement', i.e. unevaluated. */
11479 bool was_discarded = in_discarded_stmt;
11480 bool discard_then = (cx && !processing_template_decl
11481 && integer_zerop (condition));
11482 if (discard_then)
11484 in_discarded_stmt = true;
11485 ++c_inhibit_evaluation_warnings;
11488 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11489 guard_tinfo);
11491 parser->in_statement = in_statement;
11493 finish_then_clause (statement);
11495 if (discard_then)
11497 THEN_CLAUSE (statement) = NULL_TREE;
11498 in_discarded_stmt = was_discarded;
11499 --c_inhibit_evaluation_warnings;
11502 /* If the next token is `else', parse the else-clause. */
11503 if (cp_lexer_next_token_is_keyword (parser->lexer,
11504 RID_ELSE))
11506 bool discard_else = (cx && !processing_template_decl
11507 && integer_nonzerop (condition));
11508 if (discard_else)
11510 in_discarded_stmt = true;
11511 ++c_inhibit_evaluation_warnings;
11514 guard_tinfo
11515 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11516 /* Consume the `else' keyword. */
11517 cp_lexer_consume_token (parser->lexer);
11518 if (warn_duplicated_cond)
11520 if (cp_lexer_next_token_is_keyword (parser->lexer,
11521 RID_IF)
11522 && chain == NULL)
11524 /* We've got "if (COND) else if (COND2)". Start
11525 the condition chain and add COND as the first
11526 element. */
11527 chain = new vec<tree> ();
11528 if (!CONSTANT_CLASS_P (condition)
11529 && !TREE_SIDE_EFFECTS (condition))
11531 /* Wrap it in a NOP_EXPR so that we can set the
11532 location of the condition. */
11533 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11534 condition);
11535 SET_EXPR_LOCATION (e, token->location);
11536 chain->safe_push (e);
11539 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11540 RID_IF))
11542 /* This is if-else without subsequent if. Zap the
11543 condition chain; we would have already warned at
11544 this point. */
11545 delete chain;
11546 chain = NULL;
11549 begin_else_clause (statement);
11550 /* Parse the else-clause. */
11551 cp_parser_implicitly_scoped_statement (parser, NULL,
11552 guard_tinfo, chain);
11554 finish_else_clause (statement);
11556 /* If we are currently parsing a then-clause, then
11557 IF_P will not be NULL. We set it to true to
11558 indicate that this if statement has an else clause.
11559 This may trigger the Wparentheses warning below
11560 when we get back up to the parent if statement. */
11561 if (if_p != NULL)
11562 *if_p = true;
11564 if (discard_else)
11566 ELSE_CLAUSE (statement) = NULL_TREE;
11567 in_discarded_stmt = was_discarded;
11568 --c_inhibit_evaluation_warnings;
11571 else
11573 /* This if statement does not have an else clause. If
11574 NESTED_IF is true, then the then-clause has an if
11575 statement which does have an else clause. We warn
11576 about the potential ambiguity. */
11577 if (nested_if)
11578 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11579 "suggest explicit braces to avoid ambiguous"
11580 " %<else%>");
11581 if (warn_duplicated_cond)
11583 /* We don't need the condition chain anymore. */
11584 delete chain;
11585 chain = NULL;
11589 /* Now we're all done with the if-statement. */
11590 finish_if_stmt (statement);
11592 else
11594 bool in_switch_statement_p;
11595 unsigned char in_statement;
11597 /* Add the condition. */
11598 finish_switch_cond (condition, statement);
11600 /* Parse the body of the switch-statement. */
11601 in_switch_statement_p = parser->in_switch_statement_p;
11602 in_statement = parser->in_statement;
11603 parser->in_switch_statement_p = true;
11604 parser->in_statement |= IN_SWITCH_STMT;
11605 cp_parser_implicitly_scoped_statement (parser, if_p,
11606 guard_tinfo);
11607 parser->in_switch_statement_p = in_switch_statement_p;
11608 parser->in_statement = in_statement;
11610 /* Now we're all done with the switch-statement. */
11611 finish_switch_stmt (statement);
11614 return statement;
11616 break;
11618 default:
11619 cp_parser_error (parser, "expected selection-statement");
11620 return error_mark_node;
11624 /* Parse a condition.
11626 condition:
11627 expression
11628 type-specifier-seq declarator = initializer-clause
11629 type-specifier-seq declarator braced-init-list
11631 GNU Extension:
11633 condition:
11634 type-specifier-seq declarator asm-specification [opt]
11635 attributes [opt] = assignment-expression
11637 Returns the expression that should be tested. */
11639 static tree
11640 cp_parser_condition (cp_parser* parser)
11642 cp_decl_specifier_seq type_specifiers;
11643 const char *saved_message;
11644 int declares_class_or_enum;
11646 /* Try the declaration first. */
11647 cp_parser_parse_tentatively (parser);
11648 /* New types are not allowed in the type-specifier-seq for a
11649 condition. */
11650 saved_message = parser->type_definition_forbidden_message;
11651 parser->type_definition_forbidden_message
11652 = G_("types may not be defined in conditions");
11653 /* Parse the type-specifier-seq. */
11654 cp_parser_decl_specifier_seq (parser,
11655 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11656 &type_specifiers,
11657 &declares_class_or_enum);
11658 /* Restore the saved message. */
11659 parser->type_definition_forbidden_message = saved_message;
11660 /* If all is well, we might be looking at a declaration. */
11661 if (!cp_parser_error_occurred (parser))
11663 tree decl;
11664 tree asm_specification;
11665 tree attributes;
11666 cp_declarator *declarator;
11667 tree initializer = NULL_TREE;
11669 /* Parse the declarator. */
11670 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11671 /*ctor_dtor_or_conv_p=*/NULL,
11672 /*parenthesized_p=*/NULL,
11673 /*member_p=*/false,
11674 /*friend_p=*/false);
11675 /* Parse the attributes. */
11676 attributes = cp_parser_attributes_opt (parser);
11677 /* Parse the asm-specification. */
11678 asm_specification = cp_parser_asm_specification_opt (parser);
11679 /* If the next token is not an `=' or '{', then we might still be
11680 looking at an expression. For example:
11682 if (A(a).x)
11684 looks like a decl-specifier-seq and a declarator -- but then
11685 there is no `=', so this is an expression. */
11686 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11687 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11688 cp_parser_simulate_error (parser);
11690 /* If we did see an `=' or '{', then we are looking at a declaration
11691 for sure. */
11692 if (cp_parser_parse_definitely (parser))
11694 tree pushed_scope;
11695 bool non_constant_p;
11696 int flags = LOOKUP_ONLYCONVERTING;
11698 /* Create the declaration. */
11699 decl = start_decl (declarator, &type_specifiers,
11700 /*initialized_p=*/true,
11701 attributes, /*prefix_attributes=*/NULL_TREE,
11702 &pushed_scope);
11704 /* Parse the initializer. */
11705 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11707 initializer = cp_parser_braced_list (parser, &non_constant_p);
11708 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11709 flags = 0;
11711 else
11713 /* Consume the `='. */
11714 cp_parser_require (parser, CPP_EQ, RT_EQ);
11715 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11717 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11718 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11720 /* Process the initializer. */
11721 cp_finish_decl (decl,
11722 initializer, !non_constant_p,
11723 asm_specification,
11724 flags);
11726 if (pushed_scope)
11727 pop_scope (pushed_scope);
11729 return convert_from_reference (decl);
11732 /* If we didn't even get past the declarator successfully, we are
11733 definitely not looking at a declaration. */
11734 else
11735 cp_parser_abort_tentative_parse (parser);
11737 /* Otherwise, we are looking at an expression. */
11738 return cp_parser_expression (parser);
11741 /* Parses a for-statement or range-for-statement until the closing ')',
11742 not included. */
11744 static tree
11745 cp_parser_for (cp_parser *parser, bool ivdep)
11747 tree init, scope, decl;
11748 bool is_range_for;
11750 /* Begin the for-statement. */
11751 scope = begin_for_scope (&init);
11753 /* Parse the initialization. */
11754 is_range_for = cp_parser_init_statement (parser, &decl);
11756 if (is_range_for)
11757 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11758 else
11759 return cp_parser_c_for (parser, scope, init, ivdep);
11762 static tree
11763 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11765 /* Normal for loop */
11766 tree condition = NULL_TREE;
11767 tree expression = NULL_TREE;
11768 tree stmt;
11770 stmt = begin_for_stmt (scope, init);
11771 /* The init-statement has already been parsed in
11772 cp_parser_init_statement, so no work is needed here. */
11773 finish_init_stmt (stmt);
11775 /* If there's a condition, process it. */
11776 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11777 condition = cp_parser_condition (parser);
11778 else if (ivdep)
11780 cp_parser_error (parser, "missing loop condition in loop with "
11781 "%<GCC ivdep%> pragma");
11782 condition = error_mark_node;
11784 finish_for_cond (condition, stmt, ivdep);
11785 /* Look for the `;'. */
11786 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11788 /* If there's an expression, process it. */
11789 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11790 expression = cp_parser_expression (parser);
11791 finish_for_expr (expression, stmt);
11793 return stmt;
11796 /* Tries to parse a range-based for-statement:
11798 range-based-for:
11799 decl-specifier-seq declarator : expression
11801 The decl-specifier-seq declarator and the `:' are already parsed by
11802 cp_parser_init_statement. If processing_template_decl it returns a
11803 newly created RANGE_FOR_STMT; if not, it is converted to a
11804 regular FOR_STMT. */
11806 static tree
11807 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11808 bool ivdep)
11810 tree stmt, range_expr;
11811 auto_vec <cxx_binding *, 16> bindings;
11812 auto_vec <tree, 16> names;
11813 tree decomp_first_name = NULL_TREE;
11814 unsigned int decomp_cnt = 0;
11816 /* Get the range declaration momentarily out of the way so that
11817 the range expression doesn't clash with it. */
11818 if (range_decl != error_mark_node)
11820 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11822 tree v = DECL_VALUE_EXPR (range_decl);
11823 /* For decomposition declaration get all of the corresponding
11824 declarations out of the way. */
11825 if (TREE_CODE (v) == ARRAY_REF
11826 && VAR_P (TREE_OPERAND (v, 0))
11827 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11829 tree d = range_decl;
11830 range_decl = TREE_OPERAND (v, 0);
11831 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11832 decomp_first_name = d;
11833 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11835 tree name = DECL_NAME (d);
11836 names.safe_push (name);
11837 bindings.safe_push (IDENTIFIER_BINDING (name));
11838 IDENTIFIER_BINDING (name)
11839 = IDENTIFIER_BINDING (name)->previous;
11843 if (names.is_empty ())
11845 tree name = DECL_NAME (range_decl);
11846 names.safe_push (name);
11847 bindings.safe_push (IDENTIFIER_BINDING (name));
11848 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11852 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11854 bool expr_non_constant_p;
11855 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11857 else
11858 range_expr = cp_parser_expression (parser);
11860 /* Put the range declaration(s) back into scope. */
11861 for (unsigned int i = 0; i < names.length (); i++)
11863 cxx_binding *binding = bindings[i];
11864 binding->previous = IDENTIFIER_BINDING (names[i]);
11865 IDENTIFIER_BINDING (names[i]) = binding;
11868 /* If in template, STMT is converted to a normal for-statement
11869 at instantiation. If not, it is done just ahead. */
11870 if (processing_template_decl)
11872 if (check_for_bare_parameter_packs (range_expr))
11873 range_expr = error_mark_node;
11874 stmt = begin_range_for_stmt (scope, init);
11875 if (ivdep)
11876 RANGE_FOR_IVDEP (stmt) = 1;
11877 finish_range_for_decl (stmt, range_decl, range_expr);
11878 if (!type_dependent_expression_p (range_expr)
11879 /* do_auto_deduction doesn't mess with template init-lists. */
11880 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11881 do_range_for_auto_deduction (range_decl, range_expr);
11883 else
11885 stmt = begin_for_stmt (scope, init);
11886 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11887 decomp_first_name, decomp_cnt, ivdep);
11889 return stmt;
11892 /* Subroutine of cp_convert_range_for: given the initializer expression,
11893 builds up the range temporary. */
11895 static tree
11896 build_range_temp (tree range_expr)
11898 tree range_type, range_temp;
11900 /* Find out the type deduced by the declaration
11901 `auto &&__range = range_expr'. */
11902 range_type = cp_build_reference_type (make_auto (), true);
11903 range_type = do_auto_deduction (range_type, range_expr,
11904 type_uses_auto (range_type));
11906 /* Create the __range variable. */
11907 range_temp = build_decl (input_location, VAR_DECL,
11908 get_identifier ("__for_range"), range_type);
11909 TREE_USED (range_temp) = 1;
11910 DECL_ARTIFICIAL (range_temp) = 1;
11912 return range_temp;
11915 /* Used by cp_parser_range_for in template context: we aren't going to
11916 do a full conversion yet, but we still need to resolve auto in the
11917 type of the for-range-declaration if present. This is basically
11918 a shortcut version of cp_convert_range_for. */
11920 static void
11921 do_range_for_auto_deduction (tree decl, tree range_expr)
11923 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11924 if (auto_node)
11926 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11927 range_temp = convert_from_reference (build_range_temp (range_expr));
11928 iter_type = (cp_parser_perform_range_for_lookup
11929 (range_temp, &begin_dummy, &end_dummy));
11930 if (iter_type)
11932 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11933 iter_type);
11934 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11935 tf_warning_or_error);
11936 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11937 iter_decl, auto_node);
11942 /* Converts a range-based for-statement into a normal
11943 for-statement, as per the definition.
11945 for (RANGE_DECL : RANGE_EXPR)
11946 BLOCK
11948 should be equivalent to:
11951 auto &&__range = RANGE_EXPR;
11952 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11953 __begin != __end;
11954 ++__begin)
11956 RANGE_DECL = *__begin;
11957 BLOCK
11961 If RANGE_EXPR is an array:
11962 BEGIN_EXPR = __range
11963 END_EXPR = __range + ARRAY_SIZE(__range)
11964 Else if RANGE_EXPR has a member 'begin' or 'end':
11965 BEGIN_EXPR = __range.begin()
11966 END_EXPR = __range.end()
11967 Else:
11968 BEGIN_EXPR = begin(__range)
11969 END_EXPR = end(__range);
11971 If __range has a member 'begin' but not 'end', or vice versa, we must
11972 still use the second alternative (it will surely fail, however).
11973 When calling begin()/end() in the third alternative we must use
11974 argument dependent lookup, but always considering 'std' as an associated
11975 namespace. */
11977 tree
11978 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11979 tree decomp_first_name, unsigned int decomp_cnt,
11980 bool ivdep)
11982 tree begin, end;
11983 tree iter_type, begin_expr, end_expr;
11984 tree condition, expression;
11986 range_expr = mark_lvalue_use (range_expr);
11988 if (range_decl == error_mark_node || range_expr == error_mark_node)
11989 /* If an error happened previously do nothing or else a lot of
11990 unhelpful errors would be issued. */
11991 begin_expr = end_expr = iter_type = error_mark_node;
11992 else
11994 tree range_temp;
11996 if (VAR_P (range_expr)
11997 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11998 /* Can't bind a reference to an array of runtime bound. */
11999 range_temp = range_expr;
12000 else
12002 range_temp = build_range_temp (range_expr);
12003 pushdecl (range_temp);
12004 cp_finish_decl (range_temp, range_expr,
12005 /*is_constant_init*/false, NULL_TREE,
12006 LOOKUP_ONLYCONVERTING);
12007 range_temp = convert_from_reference (range_temp);
12009 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12010 &begin_expr, &end_expr);
12013 /* The new for initialization statement. */
12014 begin = build_decl (input_location, VAR_DECL,
12015 get_identifier ("__for_begin"), iter_type);
12016 TREE_USED (begin) = 1;
12017 DECL_ARTIFICIAL (begin) = 1;
12018 pushdecl (begin);
12019 cp_finish_decl (begin, begin_expr,
12020 /*is_constant_init*/false, NULL_TREE,
12021 LOOKUP_ONLYCONVERTING);
12023 if (cxx_dialect >= cxx17)
12024 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12025 end = build_decl (input_location, VAR_DECL,
12026 get_identifier ("__for_end"), iter_type);
12027 TREE_USED (end) = 1;
12028 DECL_ARTIFICIAL (end) = 1;
12029 pushdecl (end);
12030 cp_finish_decl (end, end_expr,
12031 /*is_constant_init*/false, NULL_TREE,
12032 LOOKUP_ONLYCONVERTING);
12034 finish_init_stmt (statement);
12036 /* The new for condition. */
12037 condition = build_x_binary_op (input_location, NE_EXPR,
12038 begin, ERROR_MARK,
12039 end, ERROR_MARK,
12040 NULL, tf_warning_or_error);
12041 finish_for_cond (condition, statement, ivdep);
12043 /* The new increment expression. */
12044 expression = finish_unary_op_expr (input_location,
12045 PREINCREMENT_EXPR, begin,
12046 tf_warning_or_error);
12047 finish_for_expr (expression, statement);
12049 /* The declaration is initialized with *__begin inside the loop body. */
12050 cp_finish_decl (range_decl,
12051 build_x_indirect_ref (input_location, begin, RO_NULL,
12052 tf_warning_or_error),
12053 /*is_constant_init*/false, NULL_TREE,
12054 LOOKUP_ONLYCONVERTING);
12055 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12056 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12058 return statement;
12061 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12062 We need to solve both at the same time because the method used
12063 depends on the existence of members begin or end.
12064 Returns the type deduced for the iterator expression. */
12066 static tree
12067 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12069 if (error_operand_p (range))
12071 *begin = *end = error_mark_node;
12072 return error_mark_node;
12075 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12077 error ("range-based %<for%> expression of type %qT "
12078 "has incomplete type", TREE_TYPE (range));
12079 *begin = *end = error_mark_node;
12080 return error_mark_node;
12082 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12084 /* If RANGE is an array, we will use pointer arithmetic. */
12085 *begin = decay_conversion (range, tf_warning_or_error);
12086 *end = build_binary_op (input_location, PLUS_EXPR,
12087 range,
12088 array_type_nelts_top (TREE_TYPE (range)),
12089 false);
12090 return TREE_TYPE (*begin);
12092 else
12094 /* If it is not an array, we must do a bit of magic. */
12095 tree id_begin, id_end;
12096 tree member_begin, member_end;
12098 *begin = *end = error_mark_node;
12100 id_begin = get_identifier ("begin");
12101 id_end = get_identifier ("end");
12102 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12103 /*protect=*/2, /*want_type=*/false,
12104 tf_warning_or_error);
12105 member_end = lookup_member (TREE_TYPE (range), id_end,
12106 /*protect=*/2, /*want_type=*/false,
12107 tf_warning_or_error);
12109 if (member_begin != NULL_TREE || member_end != NULL_TREE)
12111 /* Use the member functions. */
12112 if (member_begin != NULL_TREE)
12113 *begin = cp_parser_range_for_member_function (range, id_begin);
12114 else
12115 error ("range-based %<for%> expression of type %qT has an "
12116 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
12118 if (member_end != NULL_TREE)
12119 *end = cp_parser_range_for_member_function (range, id_end);
12120 else
12121 error ("range-based %<for%> expression of type %qT has a "
12122 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
12124 else
12126 /* Use global functions with ADL. */
12127 vec<tree, va_gc> *vec;
12128 vec = make_tree_vector ();
12130 vec_safe_push (vec, range);
12132 member_begin = perform_koenig_lookup (id_begin, vec,
12133 tf_warning_or_error);
12134 *begin = finish_call_expr (member_begin, &vec, false, true,
12135 tf_warning_or_error);
12136 member_end = perform_koenig_lookup (id_end, vec,
12137 tf_warning_or_error);
12138 *end = finish_call_expr (member_end, &vec, false, true,
12139 tf_warning_or_error);
12141 release_tree_vector (vec);
12144 /* Last common checks. */
12145 if (*begin == error_mark_node || *end == error_mark_node)
12147 /* If one of the expressions is an error do no more checks. */
12148 *begin = *end = error_mark_node;
12149 return error_mark_node;
12151 else if (type_dependent_expression_p (*begin)
12152 || type_dependent_expression_p (*end))
12153 /* Can happen, when, eg, in a template context, Koenig lookup
12154 can't resolve begin/end (c++/58503). */
12155 return NULL_TREE;
12156 else
12158 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12159 /* The unqualified type of the __begin and __end temporaries should
12160 be the same, as required by the multiple auto declaration. */
12161 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12163 if (cxx_dialect >= cxx17
12164 && (build_x_binary_op (input_location, NE_EXPR,
12165 *begin, ERROR_MARK,
12166 *end, ERROR_MARK,
12167 NULL, tf_none)
12168 != error_mark_node))
12169 /* P0184R0 allows __begin and __end to have different types,
12170 but make sure they are comparable so we can give a better
12171 diagnostic. */;
12172 else
12173 error ("inconsistent begin/end types in range-based %<for%> "
12174 "statement: %qT and %qT",
12175 TREE_TYPE (*begin), TREE_TYPE (*end));
12177 return iter_type;
12182 /* Helper function for cp_parser_perform_range_for_lookup.
12183 Builds a tree for RANGE.IDENTIFIER(). */
12185 static tree
12186 cp_parser_range_for_member_function (tree range, tree identifier)
12188 tree member, res;
12189 vec<tree, va_gc> *vec;
12191 member = finish_class_member_access_expr (range, identifier,
12192 false, tf_warning_or_error);
12193 if (member == error_mark_node)
12194 return error_mark_node;
12196 vec = make_tree_vector ();
12197 res = finish_call_expr (member, &vec,
12198 /*disallow_virtual=*/false,
12199 /*koenig_p=*/false,
12200 tf_warning_or_error);
12201 release_tree_vector (vec);
12202 return res;
12205 /* Parse an iteration-statement.
12207 iteration-statement:
12208 while ( condition ) statement
12209 do statement while ( expression ) ;
12210 for ( init-statement condition [opt] ; expression [opt] )
12211 statement
12213 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12215 static tree
12216 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
12218 cp_token *token;
12219 enum rid keyword;
12220 tree statement;
12221 unsigned char in_statement;
12222 token_indent_info guard_tinfo;
12224 /* Peek at the next token. */
12225 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12226 if (!token)
12227 return error_mark_node;
12229 guard_tinfo = get_token_indent_info (token);
12231 /* Remember whether or not we are already within an iteration
12232 statement. */
12233 in_statement = parser->in_statement;
12235 /* See what kind of keyword it is. */
12236 keyword = token->keyword;
12237 switch (keyword)
12239 case RID_WHILE:
12241 tree condition;
12243 /* Begin the while-statement. */
12244 statement = begin_while_stmt ();
12245 /* Look for the `('. */
12246 matching_parens parens;
12247 parens.require_open (parser);
12248 /* Parse the condition. */
12249 condition = cp_parser_condition (parser);
12250 finish_while_stmt_cond (condition, statement, ivdep);
12251 /* Look for the `)'. */
12252 parens.require_close (parser);
12253 /* Parse the dependent statement. */
12254 parser->in_statement = IN_ITERATION_STMT;
12255 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12256 parser->in_statement = in_statement;
12257 /* We're done with the while-statement. */
12258 finish_while_stmt (statement);
12260 break;
12262 case RID_DO:
12264 tree expression;
12266 /* Begin the do-statement. */
12267 statement = begin_do_stmt ();
12268 /* Parse the body of the do-statement. */
12269 parser->in_statement = IN_ITERATION_STMT;
12270 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12271 parser->in_statement = in_statement;
12272 finish_do_body (statement);
12273 /* Look for the `while' keyword. */
12274 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12275 /* Look for the `('. */
12276 matching_parens parens;
12277 parens.require_open (parser);
12278 /* Parse the expression. */
12279 expression = cp_parser_expression (parser);
12280 /* We're done with the do-statement. */
12281 finish_do_stmt (expression, statement, ivdep);
12282 /* Look for the `)'. */
12283 parens.require_close (parser);
12284 /* Look for the `;'. */
12285 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12287 break;
12289 case RID_FOR:
12291 /* Look for the `('. */
12292 matching_parens parens;
12293 parens.require_open (parser);
12295 statement = cp_parser_for (parser, ivdep);
12297 /* Look for the `)'. */
12298 parens.require_close (parser);
12300 /* Parse the body of the for-statement. */
12301 parser->in_statement = IN_ITERATION_STMT;
12302 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12303 parser->in_statement = in_statement;
12305 /* We're done with the for-statement. */
12306 finish_for_stmt (statement);
12308 break;
12310 default:
12311 cp_parser_error (parser, "expected iteration-statement");
12312 statement = error_mark_node;
12313 break;
12316 return statement;
12319 /* Parse a init-statement or the declarator of a range-based-for.
12320 Returns true if a range-based-for declaration is seen.
12322 init-statement:
12323 expression-statement
12324 simple-declaration */
12326 static bool
12327 cp_parser_init_statement (cp_parser* parser, tree *decl)
12329 /* If the next token is a `;', then we have an empty
12330 expression-statement. Grammatically, this is also a
12331 simple-declaration, but an invalid one, because it does not
12332 declare anything. Therefore, if we did not handle this case
12333 specially, we would issue an error message about an invalid
12334 declaration. */
12335 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12337 bool is_range_for = false;
12338 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12340 /* A colon is used in range-based for. */
12341 parser->colon_corrects_to_scope_p = false;
12343 /* We're going to speculatively look for a declaration, falling back
12344 to an expression, if necessary. */
12345 cp_parser_parse_tentatively (parser);
12346 /* Parse the declaration. */
12347 cp_parser_simple_declaration (parser,
12348 /*function_definition_allowed_p=*/false,
12349 decl);
12350 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12351 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12353 /* It is a range-for, consume the ':' */
12354 cp_lexer_consume_token (parser->lexer);
12355 is_range_for = true;
12356 if (cxx_dialect < cxx11)
12358 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12359 "range-based %<for%> loops only available with "
12360 "-std=c++11 or -std=gnu++11");
12361 *decl = error_mark_node;
12364 else
12365 /* The ';' is not consumed yet because we told
12366 cp_parser_simple_declaration not to. */
12367 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12369 if (cp_parser_parse_definitely (parser))
12370 return is_range_for;
12371 /* If the tentative parse failed, then we shall need to look for an
12372 expression-statement. */
12374 /* If we are here, it is an expression-statement. */
12375 cp_parser_expression_statement (parser, NULL_TREE);
12376 return false;
12379 /* Parse a jump-statement.
12381 jump-statement:
12382 break ;
12383 continue ;
12384 return expression [opt] ;
12385 return braced-init-list ;
12386 goto identifier ;
12388 GNU extension:
12390 jump-statement:
12391 goto * expression ;
12393 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12395 static tree
12396 cp_parser_jump_statement (cp_parser* parser)
12398 tree statement = error_mark_node;
12399 cp_token *token;
12400 enum rid keyword;
12401 unsigned char in_statement;
12403 /* Peek at the next token. */
12404 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12405 if (!token)
12406 return error_mark_node;
12408 /* See what kind of keyword it is. */
12409 keyword = token->keyword;
12410 switch (keyword)
12412 case RID_BREAK:
12413 in_statement = parser->in_statement & ~IN_IF_STMT;
12414 switch (in_statement)
12416 case 0:
12417 error_at (token->location, "break statement not within loop or switch");
12418 break;
12419 default:
12420 gcc_assert ((in_statement & IN_SWITCH_STMT)
12421 || in_statement == IN_ITERATION_STMT);
12422 statement = finish_break_stmt ();
12423 if (in_statement == IN_ITERATION_STMT)
12424 break_maybe_infinite_loop ();
12425 break;
12426 case IN_OMP_BLOCK:
12427 error_at (token->location, "invalid exit from OpenMP structured block");
12428 break;
12429 case IN_OMP_FOR:
12430 error_at (token->location, "break statement used with OpenMP for loop");
12431 break;
12432 case IN_CILK_SIMD_FOR:
12433 error_at (token->location, "break statement used with Cilk Plus for loop");
12434 break;
12436 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12437 break;
12439 case RID_CONTINUE:
12440 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12442 case 0:
12443 error_at (token->location, "continue statement not within a loop");
12444 break;
12445 case IN_CILK_SIMD_FOR:
12446 error_at (token->location,
12447 "continue statement within %<#pragma simd%> loop body");
12448 /* Fall through. */
12449 case IN_ITERATION_STMT:
12450 case IN_OMP_FOR:
12451 statement = finish_continue_stmt ();
12452 break;
12453 case IN_OMP_BLOCK:
12454 error_at (token->location, "invalid exit from OpenMP structured block");
12455 break;
12456 default:
12457 gcc_unreachable ();
12459 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12460 break;
12462 case RID_RETURN:
12464 tree expr;
12465 bool expr_non_constant_p;
12467 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12469 cp_lexer_set_source_position (parser->lexer);
12470 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12471 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12473 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12474 expr = cp_parser_expression (parser);
12475 else
12476 /* If the next token is a `;', then there is no
12477 expression. */
12478 expr = NULL_TREE;
12479 /* Build the return-statement. */
12480 if (current_function_auto_return_pattern && in_discarded_stmt)
12481 /* Don't deduce from a discarded return statement. */;
12482 else
12483 statement = finish_return_stmt (expr);
12484 /* Look for the final `;'. */
12485 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12487 break;
12489 case RID_GOTO:
12490 if (parser->in_function_body
12491 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12493 error ("%<goto%> in %<constexpr%> function");
12494 cp_function_chain->invalid_constexpr = true;
12497 /* Create the goto-statement. */
12498 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12500 /* Issue a warning about this use of a GNU extension. */
12501 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12502 /* Consume the '*' token. */
12503 cp_lexer_consume_token (parser->lexer);
12504 /* Parse the dependent expression. */
12505 finish_goto_stmt (cp_parser_expression (parser));
12507 else
12508 finish_goto_stmt (cp_parser_identifier (parser));
12509 /* Look for the final `;'. */
12510 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12511 break;
12513 default:
12514 cp_parser_error (parser, "expected jump-statement");
12515 break;
12518 return statement;
12521 /* Parse a declaration-statement.
12523 declaration-statement:
12524 block-declaration */
12526 static void
12527 cp_parser_declaration_statement (cp_parser* parser)
12529 void *p;
12531 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12532 p = obstack_alloc (&declarator_obstack, 0);
12534 /* Parse the block-declaration. */
12535 cp_parser_block_declaration (parser, /*statement_p=*/true);
12537 /* Free any declarators allocated. */
12538 obstack_free (&declarator_obstack, p);
12541 /* Some dependent statements (like `if (cond) statement'), are
12542 implicitly in their own scope. In other words, if the statement is
12543 a single statement (as opposed to a compound-statement), it is
12544 none-the-less treated as if it were enclosed in braces. Any
12545 declarations appearing in the dependent statement are out of scope
12546 after control passes that point. This function parses a statement,
12547 but ensures that is in its own scope, even if it is not a
12548 compound-statement.
12550 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12551 is a (possibly labeled) if statement which is not enclosed in
12552 braces and has an else clause. This is used to implement
12553 -Wparentheses.
12555 CHAIN is a vector of if-else-if conditions. This is used to implement
12556 -Wduplicated-cond.
12558 Returns the new statement. */
12560 static tree
12561 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12562 const token_indent_info &guard_tinfo,
12563 vec<tree> *chain)
12565 tree statement;
12566 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12567 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12568 token_indent_info body_tinfo
12569 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12571 if (if_p != NULL)
12572 *if_p = false;
12574 /* Mark if () ; with a special NOP_EXPR. */
12575 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12577 cp_lexer_consume_token (parser->lexer);
12578 statement = add_stmt (build_empty_stmt (body_loc));
12580 if (guard_tinfo.keyword == RID_IF
12581 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12582 warning_at (body_loc, OPT_Wempty_body,
12583 "suggest braces around empty body in an %<if%> statement");
12584 else if (guard_tinfo.keyword == RID_ELSE)
12585 warning_at (body_loc, OPT_Wempty_body,
12586 "suggest braces around empty body in an %<else%> statement");
12588 /* if a compound is opened, we simply parse the statement directly. */
12589 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12590 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12591 /* If the token is not a `{', then we must take special action. */
12592 else
12594 /* Create a compound-statement. */
12595 statement = begin_compound_stmt (0);
12596 /* Parse the dependent-statement. */
12597 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12598 &body_loc_after_labels);
12599 /* Finish the dummy compound-statement. */
12600 finish_compound_stmt (statement);
12603 token_indent_info next_tinfo
12604 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12605 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12607 if (body_loc_after_labels != UNKNOWN_LOCATION
12608 && next_tinfo.type != CPP_SEMICOLON)
12609 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12610 guard_tinfo.location, guard_tinfo.keyword);
12612 /* Return the statement. */
12613 return statement;
12616 /* For some dependent statements (like `while (cond) statement'), we
12617 have already created a scope. Therefore, even if the dependent
12618 statement is a compound-statement, we do not want to create another
12619 scope. */
12621 static void
12622 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12623 const token_indent_info &guard_tinfo)
12625 /* If the token is a `{', then we must take special action. */
12626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12628 token_indent_info body_tinfo
12629 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12630 location_t loc_after_labels = UNKNOWN_LOCATION;
12632 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12633 &loc_after_labels);
12634 token_indent_info next_tinfo
12635 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12636 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12638 if (loc_after_labels != UNKNOWN_LOCATION
12639 && next_tinfo.type != CPP_SEMICOLON)
12640 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12641 guard_tinfo.location,
12642 guard_tinfo.keyword);
12644 else
12646 /* Avoid calling cp_parser_compound_statement, so that we
12647 don't create a new scope. Do everything else by hand. */
12648 matching_braces braces;
12649 braces.require_open (parser);
12650 /* If the next keyword is `__label__' we have a label declaration. */
12651 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12652 cp_parser_label_declaration (parser);
12653 /* Parse an (optional) statement-seq. */
12654 cp_parser_statement_seq_opt (parser, NULL_TREE);
12655 braces.require_close (parser);
12659 /* Declarations [gram.dcl.dcl] */
12661 /* Parse an optional declaration-sequence.
12663 declaration-seq:
12664 declaration
12665 declaration-seq declaration */
12667 static void
12668 cp_parser_declaration_seq_opt (cp_parser* parser)
12670 while (true)
12672 cp_token *token;
12674 token = cp_lexer_peek_token (parser->lexer);
12676 if (token->type == CPP_CLOSE_BRACE
12677 || token->type == CPP_EOF
12678 || token->type == CPP_PRAGMA_EOL)
12679 break;
12681 if (token->type == CPP_SEMICOLON)
12683 /* A declaration consisting of a single semicolon is
12684 invalid. Allow it unless we're being pedantic. */
12685 cp_lexer_consume_token (parser->lexer);
12686 if (!in_system_header_at (input_location))
12687 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12688 continue;
12691 /* If we're entering or exiting a region that's implicitly
12692 extern "C", modify the lang context appropriately. */
12693 if (!parser->implicit_extern_c && token->implicit_extern_c)
12695 push_lang_context (lang_name_c);
12696 parser->implicit_extern_c = true;
12698 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12700 pop_lang_context ();
12701 parser->implicit_extern_c = false;
12704 if (token->type == CPP_PRAGMA)
12706 /* A top-level declaration can consist solely of a #pragma.
12707 A nested declaration cannot, so this is done here and not
12708 in cp_parser_declaration. (A #pragma at block scope is
12709 handled in cp_parser_statement.) */
12710 cp_parser_pragma (parser, pragma_external, NULL);
12711 continue;
12714 /* Parse the declaration itself. */
12715 cp_parser_declaration (parser);
12719 /* Parse a declaration.
12721 declaration:
12722 block-declaration
12723 function-definition
12724 template-declaration
12725 explicit-instantiation
12726 explicit-specialization
12727 linkage-specification
12728 namespace-definition
12730 C++17:
12731 deduction-guide
12733 GNU extension:
12735 declaration:
12736 __extension__ declaration */
12738 static void
12739 cp_parser_declaration (cp_parser* parser)
12741 cp_token token1;
12742 cp_token token2;
12743 int saved_pedantic;
12744 void *p;
12745 tree attributes = NULL_TREE;
12747 /* Check for the `__extension__' keyword. */
12748 if (cp_parser_extension_opt (parser, &saved_pedantic))
12750 /* Parse the qualified declaration. */
12751 cp_parser_declaration (parser);
12752 /* Restore the PEDANTIC flag. */
12753 pedantic = saved_pedantic;
12755 return;
12758 /* Try to figure out what kind of declaration is present. */
12759 token1 = *cp_lexer_peek_token (parser->lexer);
12761 if (token1.type != CPP_EOF)
12762 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12763 else
12765 token2.type = CPP_EOF;
12766 token2.keyword = RID_MAX;
12769 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12770 p = obstack_alloc (&declarator_obstack, 0);
12772 /* If the next token is `extern' and the following token is a string
12773 literal, then we have a linkage specification. */
12774 if (token1.keyword == RID_EXTERN
12775 && cp_parser_is_pure_string_literal (&token2))
12776 cp_parser_linkage_specification (parser);
12777 /* If the next token is `template', then we have either a template
12778 declaration, an explicit instantiation, or an explicit
12779 specialization. */
12780 else if (token1.keyword == RID_TEMPLATE)
12782 /* `template <>' indicates a template specialization. */
12783 if (token2.type == CPP_LESS
12784 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12785 cp_parser_explicit_specialization (parser);
12786 /* `template <' indicates a template declaration. */
12787 else if (token2.type == CPP_LESS)
12788 cp_parser_template_declaration (parser, /*member_p=*/false);
12789 /* Anything else must be an explicit instantiation. */
12790 else
12791 cp_parser_explicit_instantiation (parser);
12793 /* If the next token is `export', then we have a template
12794 declaration. */
12795 else if (token1.keyword == RID_EXPORT)
12796 cp_parser_template_declaration (parser, /*member_p=*/false);
12797 /* If the next token is `extern', 'static' or 'inline' and the one
12798 after that is `template', we have a GNU extended explicit
12799 instantiation directive. */
12800 else if (cp_parser_allow_gnu_extensions_p (parser)
12801 && (token1.keyword == RID_EXTERN
12802 || token1.keyword == RID_STATIC
12803 || token1.keyword == RID_INLINE)
12804 && token2.keyword == RID_TEMPLATE)
12805 cp_parser_explicit_instantiation (parser);
12806 /* If the next token is `namespace', check for a named or unnamed
12807 namespace definition. */
12808 else if (token1.keyword == RID_NAMESPACE
12809 && (/* A named namespace definition. */
12810 (token2.type == CPP_NAME
12811 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12812 != CPP_EQ))
12813 || (token2.type == CPP_OPEN_SQUARE
12814 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12815 == CPP_OPEN_SQUARE)
12816 /* An unnamed namespace definition. */
12817 || token2.type == CPP_OPEN_BRACE
12818 || token2.keyword == RID_ATTRIBUTE))
12819 cp_parser_namespace_definition (parser);
12820 /* An inline (associated) namespace definition. */
12821 else if (token1.keyword == RID_INLINE
12822 && token2.keyword == RID_NAMESPACE)
12823 cp_parser_namespace_definition (parser);
12824 /* Objective-C++ declaration/definition. */
12825 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12826 cp_parser_objc_declaration (parser, NULL_TREE);
12827 else if (c_dialect_objc ()
12828 && token1.keyword == RID_ATTRIBUTE
12829 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12830 cp_parser_objc_declaration (parser, attributes);
12831 /* At this point we may have a template declared by a concept
12832 introduction. */
12833 else if (flag_concepts
12834 && cp_parser_template_declaration_after_export (parser,
12835 /*member_p=*/false))
12836 /* We did. */;
12837 else
12838 /* Try to parse a block-declaration, or a function-definition. */
12839 cp_parser_block_declaration (parser, /*statement_p=*/false);
12841 /* Free any declarators allocated. */
12842 obstack_free (&declarator_obstack, p);
12845 /* Parse a block-declaration.
12847 block-declaration:
12848 simple-declaration
12849 asm-definition
12850 namespace-alias-definition
12851 using-declaration
12852 using-directive
12854 GNU Extension:
12856 block-declaration:
12857 __extension__ block-declaration
12859 C++0x Extension:
12861 block-declaration:
12862 static_assert-declaration
12864 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12865 part of a declaration-statement. */
12867 static void
12868 cp_parser_block_declaration (cp_parser *parser,
12869 bool statement_p)
12871 cp_token *token1;
12872 int saved_pedantic;
12874 /* Check for the `__extension__' keyword. */
12875 if (cp_parser_extension_opt (parser, &saved_pedantic))
12877 /* Parse the qualified declaration. */
12878 cp_parser_block_declaration (parser, statement_p);
12879 /* Restore the PEDANTIC flag. */
12880 pedantic = saved_pedantic;
12882 return;
12885 /* Peek at the next token to figure out which kind of declaration is
12886 present. */
12887 token1 = cp_lexer_peek_token (parser->lexer);
12889 /* If the next keyword is `asm', we have an asm-definition. */
12890 if (token1->keyword == RID_ASM)
12892 if (statement_p)
12893 cp_parser_commit_to_tentative_parse (parser);
12894 cp_parser_asm_definition (parser);
12896 /* If the next keyword is `namespace', we have a
12897 namespace-alias-definition. */
12898 else if (token1->keyword == RID_NAMESPACE)
12899 cp_parser_namespace_alias_definition (parser);
12900 /* If the next keyword is `using', we have a
12901 using-declaration, a using-directive, or an alias-declaration. */
12902 else if (token1->keyword == RID_USING)
12904 cp_token *token2;
12906 if (statement_p)
12907 cp_parser_commit_to_tentative_parse (parser);
12908 /* If the token after `using' is `namespace', then we have a
12909 using-directive. */
12910 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12911 if (token2->keyword == RID_NAMESPACE)
12912 cp_parser_using_directive (parser);
12913 /* If the second token after 'using' is '=', then we have an
12914 alias-declaration. */
12915 else if (cxx_dialect >= cxx11
12916 && token2->type == CPP_NAME
12917 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12918 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12919 cp_parser_alias_declaration (parser);
12920 /* Otherwise, it's a using-declaration. */
12921 else
12922 cp_parser_using_declaration (parser,
12923 /*access_declaration_p=*/false);
12925 /* If the next keyword is `__label__' we have a misplaced label
12926 declaration. */
12927 else if (token1->keyword == RID_LABEL)
12929 cp_lexer_consume_token (parser->lexer);
12930 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12931 cp_parser_skip_to_end_of_statement (parser);
12932 /* If the next token is now a `;', consume it. */
12933 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12934 cp_lexer_consume_token (parser->lexer);
12936 /* If the next token is `static_assert' we have a static assertion. */
12937 else if (token1->keyword == RID_STATIC_ASSERT)
12938 cp_parser_static_assert (parser, /*member_p=*/false);
12939 /* Anything else must be a simple-declaration. */
12940 else
12941 cp_parser_simple_declaration (parser, !statement_p,
12942 /*maybe_range_for_decl*/NULL);
12945 /* Parse a simple-declaration.
12947 simple-declaration:
12948 decl-specifier-seq [opt] init-declarator-list [opt] ;
12949 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12950 brace-or-equal-initializer ;
12952 init-declarator-list:
12953 init-declarator
12954 init-declarator-list , init-declarator
12956 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12957 function-definition as a simple-declaration.
12959 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12960 parsed declaration if it is an uninitialized single declarator not followed
12961 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12962 if present, will not be consumed. */
12964 static void
12965 cp_parser_simple_declaration (cp_parser* parser,
12966 bool function_definition_allowed_p,
12967 tree *maybe_range_for_decl)
12969 cp_decl_specifier_seq decl_specifiers;
12970 int declares_class_or_enum;
12971 bool saw_declarator;
12972 location_t comma_loc = UNKNOWN_LOCATION;
12973 location_t init_loc = UNKNOWN_LOCATION;
12975 if (maybe_range_for_decl)
12976 *maybe_range_for_decl = NULL_TREE;
12978 /* Defer access checks until we know what is being declared; the
12979 checks for names appearing in the decl-specifier-seq should be
12980 done as if we were in the scope of the thing being declared. */
12981 push_deferring_access_checks (dk_deferred);
12983 /* Parse the decl-specifier-seq. We have to keep track of whether
12984 or not the decl-specifier-seq declares a named class or
12985 enumeration type, since that is the only case in which the
12986 init-declarator-list is allowed to be empty.
12988 [dcl.dcl]
12990 In a simple-declaration, the optional init-declarator-list can be
12991 omitted only when declaring a class or enumeration, that is when
12992 the decl-specifier-seq contains either a class-specifier, an
12993 elaborated-type-specifier, or an enum-specifier. */
12994 cp_parser_decl_specifier_seq (parser,
12995 CP_PARSER_FLAGS_OPTIONAL,
12996 &decl_specifiers,
12997 &declares_class_or_enum);
12998 /* We no longer need to defer access checks. */
12999 stop_deferring_access_checks ();
13001 /* In a block scope, a valid declaration must always have a
13002 decl-specifier-seq. By not trying to parse declarators, we can
13003 resolve the declaration/expression ambiguity more quickly. */
13004 if (!function_definition_allowed_p
13005 && !decl_specifiers.any_specifiers_p)
13007 cp_parser_error (parser, "expected declaration");
13008 goto done;
13011 /* If the next two tokens are both identifiers, the code is
13012 erroneous. The usual cause of this situation is code like:
13014 T t;
13016 where "T" should name a type -- but does not. */
13017 if (!decl_specifiers.any_type_specifiers_p
13018 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13020 /* If parsing tentatively, we should commit; we really are
13021 looking at a declaration. */
13022 cp_parser_commit_to_tentative_parse (parser);
13023 /* Give up. */
13024 goto done;
13027 /* If we have seen at least one decl-specifier, and the next token
13028 is not a parenthesis, then we must be looking at a declaration.
13029 (After "int (" we might be looking at a functional cast.) */
13030 if (decl_specifiers.any_specifiers_p
13031 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13032 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13033 && !cp_parser_error_occurred (parser))
13034 cp_parser_commit_to_tentative_parse (parser);
13036 /* Look for C++17 decomposition declaration. */
13037 for (size_t n = 1; ; n++)
13038 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13039 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13040 continue;
13041 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13042 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13043 && decl_specifiers.any_specifiers_p)
13045 tree decl
13046 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13047 maybe_range_for_decl,
13048 &init_loc);
13050 /* The next token should be either a `,' or a `;'. */
13051 cp_token *token = cp_lexer_peek_token (parser->lexer);
13052 /* If it's a `;', we are done. */
13053 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
13054 goto finish;
13055 /* Anything else is an error. */
13056 else
13058 /* If we have already issued an error message we don't need
13059 to issue another one. */
13060 if ((decl != error_mark_node
13061 && DECL_INITIAL (decl) != error_mark_node)
13062 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13063 cp_parser_error (parser, "expected %<,%> or %<;%>");
13064 /* Skip tokens until we reach the end of the statement. */
13065 cp_parser_skip_to_end_of_statement (parser);
13066 /* If the next token is now a `;', consume it. */
13067 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13068 cp_lexer_consume_token (parser->lexer);
13069 goto done;
13072 else
13073 break;
13075 tree last_type;
13076 bool auto_specifier_p;
13077 /* NULL_TREE if both variable and function declaration are allowed,
13078 error_mark_node if function declaration are not allowed and
13079 a FUNCTION_DECL that should be diagnosed if it is followed by
13080 variable declarations. */
13081 tree auto_function_declaration;
13083 last_type = NULL_TREE;
13084 auto_specifier_p
13085 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13086 auto_function_declaration = NULL_TREE;
13088 /* Keep going until we hit the `;' at the end of the simple
13089 declaration. */
13090 saw_declarator = false;
13091 while (cp_lexer_next_token_is_not (parser->lexer,
13092 CPP_SEMICOLON))
13094 cp_token *token;
13095 bool function_definition_p;
13096 tree decl;
13097 tree auto_result = NULL_TREE;
13099 if (saw_declarator)
13101 /* If we are processing next declarator, comma is expected */
13102 token = cp_lexer_peek_token (parser->lexer);
13103 gcc_assert (token->type == CPP_COMMA);
13104 cp_lexer_consume_token (parser->lexer);
13105 if (maybe_range_for_decl)
13107 *maybe_range_for_decl = error_mark_node;
13108 if (comma_loc == UNKNOWN_LOCATION)
13109 comma_loc = token->location;
13112 else
13113 saw_declarator = true;
13115 /* Parse the init-declarator. */
13116 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13117 /*checks=*/NULL,
13118 function_definition_allowed_p,
13119 /*member_p=*/false,
13120 declares_class_or_enum,
13121 &function_definition_p,
13122 maybe_range_for_decl,
13123 &init_loc,
13124 &auto_result);
13125 /* If an error occurred while parsing tentatively, exit quickly.
13126 (That usually happens when in the body of a function; each
13127 statement is treated as a declaration-statement until proven
13128 otherwise.) */
13129 if (cp_parser_error_occurred (parser))
13130 goto done;
13132 if (auto_specifier_p && cxx_dialect >= cxx14)
13134 /* If the init-declarator-list contains more than one
13135 init-declarator, they shall all form declarations of
13136 variables. */
13137 if (auto_function_declaration == NULL_TREE)
13138 auto_function_declaration
13139 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13140 else if (TREE_CODE (decl) == FUNCTION_DECL
13141 || auto_function_declaration != error_mark_node)
13143 error_at (decl_specifiers.locations[ds_type_spec],
13144 "non-variable %qD in declaration with more than one "
13145 "declarator with placeholder type",
13146 TREE_CODE (decl) == FUNCTION_DECL
13147 ? decl : auto_function_declaration);
13148 auto_function_declaration = error_mark_node;
13152 if (auto_result
13153 && (!processing_template_decl || !type_uses_auto (auto_result)))
13155 if (last_type
13156 && last_type != error_mark_node
13157 && !same_type_p (auto_result, last_type))
13159 /* If the list of declarators contains more than one declarator,
13160 the type of each declared variable is determined as described
13161 above. If the type deduced for the template parameter U is not
13162 the same in each deduction, the program is ill-formed. */
13163 error_at (decl_specifiers.locations[ds_type_spec],
13164 "inconsistent deduction for %qT: %qT and then %qT",
13165 decl_specifiers.type, last_type, auto_result);
13166 last_type = error_mark_node;
13168 else
13169 last_type = auto_result;
13172 /* Handle function definitions specially. */
13173 if (function_definition_p)
13175 /* If the next token is a `,', then we are probably
13176 processing something like:
13178 void f() {}, *p;
13180 which is erroneous. */
13181 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13183 cp_token *token = cp_lexer_peek_token (parser->lexer);
13184 error_at (token->location,
13185 "mixing"
13186 " declarations and function-definitions is forbidden");
13188 /* Otherwise, we're done with the list of declarators. */
13189 else
13191 pop_deferring_access_checks ();
13192 return;
13195 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13196 *maybe_range_for_decl = decl;
13197 /* The next token should be either a `,' or a `;'. */
13198 token = cp_lexer_peek_token (parser->lexer);
13199 /* If it's a `,', there are more declarators to come. */
13200 if (token->type == CPP_COMMA)
13201 /* will be consumed next time around */;
13202 /* If it's a `;', we are done. */
13203 else if (token->type == CPP_SEMICOLON)
13204 break;
13205 else if (maybe_range_for_decl)
13207 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13208 permerror (decl_specifiers.locations[ds_type_spec],
13209 "types may not be defined in a for-range-declaration");
13210 break;
13212 /* Anything else is an error. */
13213 else
13215 /* If we have already issued an error message we don't need
13216 to issue another one. */
13217 if ((decl != error_mark_node
13218 && DECL_INITIAL (decl) != error_mark_node)
13219 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13220 cp_parser_error (parser, "expected %<,%> or %<;%>");
13221 /* Skip tokens until we reach the end of the statement. */
13222 cp_parser_skip_to_end_of_statement (parser);
13223 /* If the next token is now a `;', consume it. */
13224 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13225 cp_lexer_consume_token (parser->lexer);
13226 goto done;
13228 /* After the first time around, a function-definition is not
13229 allowed -- even if it was OK at first. For example:
13231 int i, f() {}
13233 is not valid. */
13234 function_definition_allowed_p = false;
13237 /* Issue an error message if no declarators are present, and the
13238 decl-specifier-seq does not itself declare a class or
13239 enumeration: [dcl.dcl]/3. */
13240 if (!saw_declarator)
13242 if (cp_parser_declares_only_class_p (parser))
13244 if (!declares_class_or_enum
13245 && decl_specifiers.type
13246 && OVERLOAD_TYPE_P (decl_specifiers.type))
13247 /* Ensure an error is issued anyway when finish_decltype_type,
13248 called via cp_parser_decl_specifier_seq, returns a class or
13249 an enumeration (c++/51786). */
13250 decl_specifiers.type = NULL_TREE;
13251 shadow_tag (&decl_specifiers);
13253 /* Perform any deferred access checks. */
13254 perform_deferred_access_checks (tf_warning_or_error);
13257 /* Consume the `;'. */
13258 finish:
13259 if (!maybe_range_for_decl)
13260 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13261 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13263 if (init_loc != UNKNOWN_LOCATION)
13264 error_at (init_loc, "initializer in range-based %<for%> loop");
13265 if (comma_loc != UNKNOWN_LOCATION)
13266 error_at (comma_loc,
13267 "multiple declarations in range-based %<for%> loop");
13270 done:
13271 pop_deferring_access_checks ();
13274 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13275 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13276 initializer ; */
13278 static tree
13279 cp_parser_decomposition_declaration (cp_parser *parser,
13280 cp_decl_specifier_seq *decl_specifiers,
13281 tree *maybe_range_for_decl,
13282 location_t *init_loc)
13284 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13285 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13286 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13288 /* Parse the identifier-list. */
13289 auto_vec<cp_expr, 10> v;
13290 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13291 while (true)
13293 cp_expr e = cp_parser_identifier (parser);
13294 if (e.get_value () == error_mark_node)
13295 break;
13296 v.safe_push (e);
13297 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13298 break;
13299 cp_lexer_consume_token (parser->lexer);
13302 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13303 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13305 end_loc = UNKNOWN_LOCATION;
13306 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13307 false);
13308 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13309 cp_lexer_consume_token (parser->lexer);
13310 else
13312 cp_parser_skip_to_end_of_statement (parser);
13313 return error_mark_node;
13317 if (cxx_dialect < cxx17)
13318 pedwarn (loc, 0, "structured bindings only available with "
13319 "-std=c++17 or -std=gnu++17");
13321 tree pushed_scope;
13322 cp_declarator *declarator = make_declarator (cdk_decomp);
13323 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13324 declarator->id_loc = loc;
13325 if (ref_qual != REF_QUAL_NONE)
13326 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13327 ref_qual == REF_QUAL_RVALUE,
13328 NULL_TREE);
13329 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13330 NULL_TREE, decl_specifiers->attributes,
13331 &pushed_scope);
13332 tree orig_decl = decl;
13334 unsigned int i;
13335 cp_expr e;
13336 cp_decl_specifier_seq decl_specs;
13337 clear_decl_specs (&decl_specs);
13338 decl_specs.type = make_auto ();
13339 tree prev = decl;
13340 FOR_EACH_VEC_ELT (v, i, e)
13342 if (i == 0)
13343 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13344 else
13345 declarator->u.id.unqualified_name = e.get_value ();
13346 declarator->id_loc = e.get_location ();
13347 tree elt_pushed_scope;
13348 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13349 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13350 if (decl2 == error_mark_node)
13351 decl = error_mark_node;
13352 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13354 /* Ensure we've diagnosed redeclaration if we aren't creating
13355 a new VAR_DECL. */
13356 gcc_assert (errorcount);
13357 decl = error_mark_node;
13359 else
13360 prev = decl2;
13361 if (elt_pushed_scope)
13362 pop_scope (elt_pushed_scope);
13365 if (v.is_empty ())
13367 error_at (loc, "empty structured binding declaration");
13368 decl = error_mark_node;
13371 if (maybe_range_for_decl == NULL
13372 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13374 bool non_constant_p = false, is_direct_init = false;
13375 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13376 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13377 &non_constant_p);
13378 if (initializer == NULL_TREE
13379 || (TREE_CODE (initializer) == TREE_LIST
13380 && TREE_CHAIN (initializer))
13381 || (TREE_CODE (initializer) == CONSTRUCTOR
13382 && CONSTRUCTOR_NELTS (initializer) != 1))
13384 error_at (loc, "invalid initializer for structured binding "
13385 "declaration");
13386 initializer = error_mark_node;
13389 if (decl != error_mark_node)
13391 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13392 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13393 cp_finish_decomp (decl, prev, v.length ());
13396 else if (decl != error_mark_node)
13398 *maybe_range_for_decl = prev;
13399 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13400 the underlying DECL. */
13401 cp_finish_decomp (decl, prev, v.length ());
13404 if (pushed_scope)
13405 pop_scope (pushed_scope);
13407 if (decl == error_mark_node && DECL_P (orig_decl))
13409 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13410 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13413 return decl;
13416 /* Parse a decl-specifier-seq.
13418 decl-specifier-seq:
13419 decl-specifier-seq [opt] decl-specifier
13420 decl-specifier attribute-specifier-seq [opt] (C++11)
13422 decl-specifier:
13423 storage-class-specifier
13424 type-specifier
13425 function-specifier
13426 friend
13427 typedef
13429 GNU Extension:
13431 decl-specifier:
13432 attributes
13434 Concepts Extension:
13436 decl-specifier:
13437 concept
13439 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13441 The parser flags FLAGS is used to control type-specifier parsing.
13443 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13444 flags:
13446 1: one of the decl-specifiers is an elaborated-type-specifier
13447 (i.e., a type declaration)
13448 2: one of the decl-specifiers is an enum-specifier or a
13449 class-specifier (i.e., a type definition)
13453 static void
13454 cp_parser_decl_specifier_seq (cp_parser* parser,
13455 cp_parser_flags flags,
13456 cp_decl_specifier_seq *decl_specs,
13457 int* declares_class_or_enum)
13459 bool constructor_possible_p = !parser->in_declarator_p;
13460 bool found_decl_spec = false;
13461 cp_token *start_token = NULL;
13462 cp_decl_spec ds;
13464 /* Clear DECL_SPECS. */
13465 clear_decl_specs (decl_specs);
13467 /* Assume no class or enumeration type is declared. */
13468 *declares_class_or_enum = 0;
13470 /* Keep reading specifiers until there are no more to read. */
13471 while (true)
13473 bool constructor_p;
13474 cp_token *token;
13475 ds = ds_last;
13477 /* Peek at the next token. */
13478 token = cp_lexer_peek_token (parser->lexer);
13480 /* Save the first token of the decl spec list for error
13481 reporting. */
13482 if (!start_token)
13483 start_token = token;
13484 /* Handle attributes. */
13485 if (cp_next_tokens_can_be_attribute_p (parser))
13487 /* Parse the attributes. */
13488 tree attrs = cp_parser_attributes_opt (parser);
13490 /* In a sequence of declaration specifiers, c++11 attributes
13491 appertain to the type that precede them. In that case
13492 [dcl.spec]/1 says:
13494 The attribute-specifier-seq affects the type only for
13495 the declaration it appears in, not other declarations
13496 involving the same type.
13498 But for now let's force the user to position the
13499 attribute either at the beginning of the declaration or
13500 after the declarator-id, which would clearly mean that it
13501 applies to the declarator. */
13502 if (cxx11_attribute_p (attrs))
13504 if (!found_decl_spec)
13505 /* The c++11 attribute is at the beginning of the
13506 declaration. It appertains to the entity being
13507 declared. */;
13508 else
13510 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13512 /* This is an attribute following a
13513 class-specifier. */
13514 if (decl_specs->type_definition_p)
13515 warn_misplaced_attr_for_class_type (token->location,
13516 decl_specs->type);
13517 attrs = NULL_TREE;
13519 else
13521 decl_specs->std_attributes
13522 = chainon (decl_specs->std_attributes,
13523 attrs);
13524 if (decl_specs->locations[ds_std_attribute] == 0)
13525 decl_specs->locations[ds_std_attribute] = token->location;
13527 continue;
13531 decl_specs->attributes
13532 = chainon (decl_specs->attributes,
13533 attrs);
13534 if (decl_specs->locations[ds_attribute] == 0)
13535 decl_specs->locations[ds_attribute] = token->location;
13536 continue;
13538 /* Assume we will find a decl-specifier keyword. */
13539 found_decl_spec = true;
13540 /* If the next token is an appropriate keyword, we can simply
13541 add it to the list. */
13542 switch (token->keyword)
13544 /* decl-specifier:
13545 friend
13546 constexpr */
13547 case RID_FRIEND:
13548 if (!at_class_scope_p ())
13550 gcc_rich_location richloc (token->location);
13551 richloc.add_fixit_remove ();
13552 error_at (&richloc, "%<friend%> used outside of class");
13553 cp_lexer_purge_token (parser->lexer);
13555 else
13557 ds = ds_friend;
13558 /* Consume the token. */
13559 cp_lexer_consume_token (parser->lexer);
13561 break;
13563 case RID_CONSTEXPR:
13564 ds = ds_constexpr;
13565 cp_lexer_consume_token (parser->lexer);
13566 break;
13568 case RID_CONCEPT:
13569 ds = ds_concept;
13570 cp_lexer_consume_token (parser->lexer);
13571 break;
13573 /* function-specifier:
13574 inline
13575 virtual
13576 explicit */
13577 case RID_INLINE:
13578 case RID_VIRTUAL:
13579 case RID_EXPLICIT:
13580 cp_parser_function_specifier_opt (parser, decl_specs);
13581 break;
13583 /* decl-specifier:
13584 typedef */
13585 case RID_TYPEDEF:
13586 ds = ds_typedef;
13587 /* Consume the token. */
13588 cp_lexer_consume_token (parser->lexer);
13589 /* A constructor declarator cannot appear in a typedef. */
13590 constructor_possible_p = false;
13591 /* The "typedef" keyword can only occur in a declaration; we
13592 may as well commit at this point. */
13593 cp_parser_commit_to_tentative_parse (parser);
13595 if (decl_specs->storage_class != sc_none)
13596 decl_specs->conflicting_specifiers_p = true;
13597 break;
13599 /* storage-class-specifier:
13600 auto
13601 register
13602 static
13603 extern
13604 mutable
13606 GNU Extension:
13607 thread */
13608 case RID_AUTO:
13609 if (cxx_dialect == cxx98)
13611 /* Consume the token. */
13612 cp_lexer_consume_token (parser->lexer);
13614 /* Complain about `auto' as a storage specifier, if
13615 we're complaining about C++0x compatibility. */
13616 gcc_rich_location richloc (token->location);
13617 richloc.add_fixit_remove ();
13618 warning_at (&richloc, OPT_Wc__11_compat,
13619 "%<auto%> changes meaning in C++11; "
13620 "please remove it");
13622 /* Set the storage class anyway. */
13623 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13624 token);
13626 else
13627 /* C++0x auto type-specifier. */
13628 found_decl_spec = false;
13629 break;
13631 case RID_REGISTER:
13632 case RID_STATIC:
13633 case RID_EXTERN:
13634 case RID_MUTABLE:
13635 /* Consume the token. */
13636 cp_lexer_consume_token (parser->lexer);
13637 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13638 token);
13639 break;
13640 case RID_THREAD:
13641 /* Consume the token. */
13642 ds = ds_thread;
13643 cp_lexer_consume_token (parser->lexer);
13644 break;
13646 default:
13647 /* We did not yet find a decl-specifier yet. */
13648 found_decl_spec = false;
13649 break;
13652 if (found_decl_spec
13653 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13654 && token->keyword != RID_CONSTEXPR)
13655 error ("decl-specifier invalid in condition");
13657 if (found_decl_spec
13658 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13659 && token->keyword != RID_MUTABLE
13660 && token->keyword != RID_CONSTEXPR)
13661 error_at (token->location, "%qD invalid in lambda",
13662 ridpointers[token->keyword]);
13664 if (ds != ds_last)
13665 set_and_check_decl_spec_loc (decl_specs, ds, token);
13667 /* Constructors are a special case. The `S' in `S()' is not a
13668 decl-specifier; it is the beginning of the declarator. */
13669 constructor_p
13670 = (!found_decl_spec
13671 && constructor_possible_p
13672 && (cp_parser_constructor_declarator_p
13673 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13675 /* If we don't have a DECL_SPEC yet, then we must be looking at
13676 a type-specifier. */
13677 if (!found_decl_spec && !constructor_p)
13679 int decl_spec_declares_class_or_enum;
13680 bool is_cv_qualifier;
13681 tree type_spec;
13683 type_spec
13684 = cp_parser_type_specifier (parser, flags,
13685 decl_specs,
13686 /*is_declaration=*/true,
13687 &decl_spec_declares_class_or_enum,
13688 &is_cv_qualifier);
13689 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13691 /* If this type-specifier referenced a user-defined type
13692 (a typedef, class-name, etc.), then we can't allow any
13693 more such type-specifiers henceforth.
13695 [dcl.spec]
13697 The longest sequence of decl-specifiers that could
13698 possibly be a type name is taken as the
13699 decl-specifier-seq of a declaration. The sequence shall
13700 be self-consistent as described below.
13702 [dcl.type]
13704 As a general rule, at most one type-specifier is allowed
13705 in the complete decl-specifier-seq of a declaration. The
13706 only exceptions are the following:
13708 -- const or volatile can be combined with any other
13709 type-specifier.
13711 -- signed or unsigned can be combined with char, long,
13712 short, or int.
13714 -- ..
13716 Example:
13718 typedef char* Pc;
13719 void g (const int Pc);
13721 Here, Pc is *not* part of the decl-specifier seq; it's
13722 the declarator. Therefore, once we see a type-specifier
13723 (other than a cv-qualifier), we forbid any additional
13724 user-defined types. We *do* still allow things like `int
13725 int' to be considered a decl-specifier-seq, and issue the
13726 error message later. */
13727 if (type_spec && !is_cv_qualifier)
13728 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13729 /* A constructor declarator cannot follow a type-specifier. */
13730 if (type_spec)
13732 constructor_possible_p = false;
13733 found_decl_spec = true;
13734 if (!is_cv_qualifier)
13735 decl_specs->any_type_specifiers_p = true;
13739 /* If we still do not have a DECL_SPEC, then there are no more
13740 decl-specifiers. */
13741 if (!found_decl_spec)
13742 break;
13744 decl_specs->any_specifiers_p = true;
13745 /* After we see one decl-specifier, further decl-specifiers are
13746 always optional. */
13747 flags |= CP_PARSER_FLAGS_OPTIONAL;
13750 /* Don't allow a friend specifier with a class definition. */
13751 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13752 && (*declares_class_or_enum & 2))
13753 error_at (decl_specs->locations[ds_friend],
13754 "class definition may not be declared a friend");
13757 /* Parse an (optional) storage-class-specifier.
13759 storage-class-specifier:
13760 auto
13761 register
13762 static
13763 extern
13764 mutable
13766 GNU Extension:
13768 storage-class-specifier:
13769 thread
13771 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13773 static tree
13774 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13776 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13778 case RID_AUTO:
13779 if (cxx_dialect != cxx98)
13780 return NULL_TREE;
13781 /* Fall through for C++98. */
13782 gcc_fallthrough ();
13784 case RID_REGISTER:
13785 case RID_STATIC:
13786 case RID_EXTERN:
13787 case RID_MUTABLE:
13788 case RID_THREAD:
13789 /* Consume the token. */
13790 return cp_lexer_consume_token (parser->lexer)->u.value;
13792 default:
13793 return NULL_TREE;
13797 /* Parse an (optional) function-specifier.
13799 function-specifier:
13800 inline
13801 virtual
13802 explicit
13804 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13805 Updates DECL_SPECS, if it is non-NULL. */
13807 static tree
13808 cp_parser_function_specifier_opt (cp_parser* parser,
13809 cp_decl_specifier_seq *decl_specs)
13811 cp_token *token = cp_lexer_peek_token (parser->lexer);
13812 switch (token->keyword)
13814 case RID_INLINE:
13815 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13816 break;
13818 case RID_VIRTUAL:
13819 /* 14.5.2.3 [temp.mem]
13821 A member function template shall not be virtual. */
13822 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13823 && current_class_type)
13824 error_at (token->location, "templates may not be %<virtual%>");
13825 else
13826 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13827 break;
13829 case RID_EXPLICIT:
13830 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13831 break;
13833 default:
13834 return NULL_TREE;
13837 /* Consume the token. */
13838 return cp_lexer_consume_token (parser->lexer)->u.value;
13841 /* Parse a linkage-specification.
13843 linkage-specification:
13844 extern string-literal { declaration-seq [opt] }
13845 extern string-literal declaration */
13847 static void
13848 cp_parser_linkage_specification (cp_parser* parser)
13850 tree linkage;
13852 /* Look for the `extern' keyword. */
13853 cp_token *extern_token
13854 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13856 /* Look for the string-literal. */
13857 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13858 linkage = cp_parser_string_literal (parser, false, false);
13860 /* Transform the literal into an identifier. If the literal is a
13861 wide-character string, or contains embedded NULs, then we can't
13862 handle it as the user wants. */
13863 if (strlen (TREE_STRING_POINTER (linkage))
13864 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13866 cp_parser_error (parser, "invalid linkage-specification");
13867 /* Assume C++ linkage. */
13868 linkage = lang_name_cplusplus;
13870 else
13871 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13873 /* We're now using the new linkage. */
13874 push_lang_context (linkage);
13876 /* Preserve the location of the the innermost linkage specification,
13877 tracking the locations of nested specifications via a local. */
13878 location_t saved_location
13879 = parser->innermost_linkage_specification_location;
13880 /* Construct a location ranging from the start of the "extern" to
13881 the end of the string-literal, with the caret at the start, e.g.:
13882 extern "C" {
13883 ^~~~~~~~~~
13885 parser->innermost_linkage_specification_location
13886 = make_location (extern_token->location,
13887 extern_token->location,
13888 get_finish (string_token->location));
13890 /* If the next token is a `{', then we're using the first
13891 production. */
13892 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13894 cp_ensure_no_omp_declare_simd (parser);
13895 cp_ensure_no_oacc_routine (parser);
13897 /* Consume the `{' token. */
13898 matching_braces braces;
13899 braces.consume_open (parser)->location;
13900 /* Parse the declarations. */
13901 cp_parser_declaration_seq_opt (parser);
13902 /* Look for the closing `}'. */
13903 braces.require_close (parser);
13905 /* Otherwise, there's just one declaration. */
13906 else
13908 bool saved_in_unbraced_linkage_specification_p;
13910 saved_in_unbraced_linkage_specification_p
13911 = parser->in_unbraced_linkage_specification_p;
13912 parser->in_unbraced_linkage_specification_p = true;
13913 cp_parser_declaration (parser);
13914 parser->in_unbraced_linkage_specification_p
13915 = saved_in_unbraced_linkage_specification_p;
13918 /* We're done with the linkage-specification. */
13919 pop_lang_context ();
13921 /* Restore location of parent linkage specification, if any. */
13922 parser->innermost_linkage_specification_location = saved_location;
13925 /* Parse a static_assert-declaration.
13927 static_assert-declaration:
13928 static_assert ( constant-expression , string-literal ) ;
13929 static_assert ( constant-expression ) ; (C++17)
13931 If MEMBER_P, this static_assert is a class member. */
13933 static void
13934 cp_parser_static_assert(cp_parser *parser, bool member_p)
13936 tree condition;
13937 tree message;
13938 cp_token *token;
13939 location_t saved_loc;
13940 bool dummy;
13942 /* Peek at the `static_assert' token so we can keep track of exactly
13943 where the static assertion started. */
13944 token = cp_lexer_peek_token (parser->lexer);
13945 saved_loc = token->location;
13947 /* Look for the `static_assert' keyword. */
13948 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13949 RT_STATIC_ASSERT))
13950 return;
13952 /* We know we are in a static assertion; commit to any tentative
13953 parse. */
13954 if (cp_parser_parsing_tentatively (parser))
13955 cp_parser_commit_to_tentative_parse (parser);
13957 /* Parse the `(' starting the static assertion condition. */
13958 matching_parens parens;
13959 parens.require_open (parser);
13961 /* Parse the constant-expression. Allow a non-constant expression
13962 here in order to give better diagnostics in finish_static_assert. */
13963 condition =
13964 cp_parser_constant_expression (parser,
13965 /*allow_non_constant_p=*/true,
13966 /*non_constant_p=*/&dummy);
13968 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13970 if (cxx_dialect < cxx17)
13971 pedwarn (input_location, OPT_Wpedantic,
13972 "static_assert without a message "
13973 "only available with -std=c++17 or -std=gnu++17");
13974 /* Eat the ')' */
13975 cp_lexer_consume_token (parser->lexer);
13976 message = build_string (1, "");
13977 TREE_TYPE (message) = char_array_type_node;
13978 fix_string_type (message);
13980 else
13982 /* Parse the separating `,'. */
13983 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13985 /* Parse the string-literal message. */
13986 message = cp_parser_string_literal (parser,
13987 /*translate=*/false,
13988 /*wide_ok=*/true);
13990 /* A `)' completes the static assertion. */
13991 if (!parens.require_close (parser))
13992 cp_parser_skip_to_closing_parenthesis (parser,
13993 /*recovering=*/true,
13994 /*or_comma=*/false,
13995 /*consume_paren=*/true);
13998 /* A semicolon terminates the declaration. */
13999 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14001 /* Complete the static assertion, which may mean either processing
14002 the static assert now or saving it for template instantiation. */
14003 finish_static_assert (condition, message, saved_loc, member_p);
14006 /* Parse the expression in decltype ( expression ). */
14008 static tree
14009 cp_parser_decltype_expr (cp_parser *parser,
14010 bool &id_expression_or_member_access_p)
14012 cp_token *id_expr_start_token;
14013 tree expr;
14015 /* Since we're going to preserve any side-effects from this parse, set up a
14016 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14017 in the expression. */
14018 tentative_firewall firewall (parser);
14020 /* First, try parsing an id-expression. */
14021 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14022 cp_parser_parse_tentatively (parser);
14023 expr = cp_parser_id_expression (parser,
14024 /*template_keyword_p=*/false,
14025 /*check_dependency_p=*/true,
14026 /*template_p=*/NULL,
14027 /*declarator_p=*/false,
14028 /*optional_p=*/false);
14030 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14032 bool non_integral_constant_expression_p = false;
14033 tree id_expression = expr;
14034 cp_id_kind idk;
14035 const char *error_msg;
14037 if (identifier_p (expr))
14038 /* Lookup the name we got back from the id-expression. */
14039 expr = cp_parser_lookup_name_simple (parser, expr,
14040 id_expr_start_token->location);
14042 if (expr
14043 && expr != error_mark_node
14044 && TREE_CODE (expr) != TYPE_DECL
14045 && (TREE_CODE (expr) != BIT_NOT_EXPR
14046 || !TYPE_P (TREE_OPERAND (expr, 0)))
14047 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14049 /* Complete lookup of the id-expression. */
14050 expr = (finish_id_expression
14051 (id_expression, expr, parser->scope, &idk,
14052 /*integral_constant_expression_p=*/false,
14053 /*allow_non_integral_constant_expression_p=*/true,
14054 &non_integral_constant_expression_p,
14055 /*template_p=*/false,
14056 /*done=*/true,
14057 /*address_p=*/false,
14058 /*template_arg_p=*/false,
14059 &error_msg,
14060 id_expr_start_token->location));
14062 if (expr == error_mark_node)
14063 /* We found an id-expression, but it was something that we
14064 should not have found. This is an error, not something
14065 we can recover from, so note that we found an
14066 id-expression and we'll recover as gracefully as
14067 possible. */
14068 id_expression_or_member_access_p = true;
14071 if (expr
14072 && expr != error_mark_node
14073 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14074 /* We have an id-expression. */
14075 id_expression_or_member_access_p = true;
14078 if (!id_expression_or_member_access_p)
14080 /* Abort the id-expression parse. */
14081 cp_parser_abort_tentative_parse (parser);
14083 /* Parsing tentatively, again. */
14084 cp_parser_parse_tentatively (parser);
14086 /* Parse a class member access. */
14087 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14088 /*cast_p=*/false, /*decltype*/true,
14089 /*member_access_only_p=*/true, NULL);
14091 if (expr
14092 && expr != error_mark_node
14093 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14094 /* We have an id-expression. */
14095 id_expression_or_member_access_p = true;
14098 if (id_expression_or_member_access_p)
14099 /* We have parsed the complete id-expression or member access. */
14100 cp_parser_parse_definitely (parser);
14101 else
14103 /* Abort our attempt to parse an id-expression or member access
14104 expression. */
14105 cp_parser_abort_tentative_parse (parser);
14107 /* Parse a full expression. */
14108 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14109 /*decltype_p=*/true);
14112 return expr;
14115 /* Parse a `decltype' type. Returns the type.
14117 simple-type-specifier:
14118 decltype ( expression )
14119 C++14 proposal:
14120 decltype ( auto ) */
14122 static tree
14123 cp_parser_decltype (cp_parser *parser)
14125 tree expr;
14126 bool id_expression_or_member_access_p = false;
14127 const char *saved_message;
14128 bool saved_integral_constant_expression_p;
14129 bool saved_non_integral_constant_expression_p;
14130 bool saved_greater_than_is_operator_p;
14131 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14133 if (start_token->type == CPP_DECLTYPE)
14135 /* Already parsed. */
14136 cp_lexer_consume_token (parser->lexer);
14137 return saved_checks_value (start_token->u.tree_check_value);
14140 /* Look for the `decltype' token. */
14141 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14142 return error_mark_node;
14144 /* Parse the opening `('. */
14145 matching_parens parens;
14146 if (!parens.require_open (parser))
14147 return error_mark_node;
14149 /* decltype (auto) */
14150 if (cxx_dialect >= cxx14
14151 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14153 cp_lexer_consume_token (parser->lexer);
14154 if (!parens.require_close (parser))
14155 return error_mark_node;
14156 expr = make_decltype_auto ();
14157 AUTO_IS_DECLTYPE (expr) = true;
14158 goto rewrite;
14161 /* Types cannot be defined in a `decltype' expression. Save away the
14162 old message. */
14163 saved_message = parser->type_definition_forbidden_message;
14165 /* And create the new one. */
14166 parser->type_definition_forbidden_message
14167 = G_("types may not be defined in %<decltype%> expressions");
14169 /* The restrictions on constant-expressions do not apply inside
14170 decltype expressions. */
14171 saved_integral_constant_expression_p
14172 = parser->integral_constant_expression_p;
14173 saved_non_integral_constant_expression_p
14174 = parser->non_integral_constant_expression_p;
14175 parser->integral_constant_expression_p = false;
14177 /* Within a parenthesized expression, a `>' token is always
14178 the greater-than operator. */
14179 saved_greater_than_is_operator_p
14180 = parser->greater_than_is_operator_p;
14181 parser->greater_than_is_operator_p = true;
14183 /* Do not actually evaluate the expression. */
14184 ++cp_unevaluated_operand;
14186 /* Do not warn about problems with the expression. */
14187 ++c_inhibit_evaluation_warnings;
14189 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14191 /* Go back to evaluating expressions. */
14192 --cp_unevaluated_operand;
14193 --c_inhibit_evaluation_warnings;
14195 /* The `>' token might be the end of a template-id or
14196 template-parameter-list now. */
14197 parser->greater_than_is_operator_p
14198 = saved_greater_than_is_operator_p;
14200 /* Restore the old message and the integral constant expression
14201 flags. */
14202 parser->type_definition_forbidden_message = saved_message;
14203 parser->integral_constant_expression_p
14204 = saved_integral_constant_expression_p;
14205 parser->non_integral_constant_expression_p
14206 = saved_non_integral_constant_expression_p;
14208 /* Parse to the closing `)'. */
14209 if (!parens.require_close (parser))
14211 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14212 /*consume_paren=*/true);
14213 return error_mark_node;
14216 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14217 tf_warning_or_error);
14219 rewrite:
14220 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14221 it again. */
14222 start_token->type = CPP_DECLTYPE;
14223 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14224 start_token->u.tree_check_value->value = expr;
14225 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14226 start_token->keyword = RID_MAX;
14227 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14229 return expr;
14232 /* Special member functions [gram.special] */
14234 /* Parse a conversion-function-id.
14236 conversion-function-id:
14237 operator conversion-type-id
14239 Returns an IDENTIFIER_NODE representing the operator. */
14241 static tree
14242 cp_parser_conversion_function_id (cp_parser* parser)
14244 tree type;
14245 tree saved_scope;
14246 tree saved_qualifying_scope;
14247 tree saved_object_scope;
14248 tree pushed_scope = NULL_TREE;
14250 /* Look for the `operator' token. */
14251 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14252 return error_mark_node;
14253 /* When we parse the conversion-type-id, the current scope will be
14254 reset. However, we need that information in able to look up the
14255 conversion function later, so we save it here. */
14256 saved_scope = parser->scope;
14257 saved_qualifying_scope = parser->qualifying_scope;
14258 saved_object_scope = parser->object_scope;
14259 /* We must enter the scope of the class so that the names of
14260 entities declared within the class are available in the
14261 conversion-type-id. For example, consider:
14263 struct S {
14264 typedef int I;
14265 operator I();
14268 S::operator I() { ... }
14270 In order to see that `I' is a type-name in the definition, we
14271 must be in the scope of `S'. */
14272 if (saved_scope)
14273 pushed_scope = push_scope (saved_scope);
14274 /* Parse the conversion-type-id. */
14275 type = cp_parser_conversion_type_id (parser);
14276 /* Leave the scope of the class, if any. */
14277 if (pushed_scope)
14278 pop_scope (pushed_scope);
14279 /* Restore the saved scope. */
14280 parser->scope = saved_scope;
14281 parser->qualifying_scope = saved_qualifying_scope;
14282 parser->object_scope = saved_object_scope;
14283 /* If the TYPE is invalid, indicate failure. */
14284 if (type == error_mark_node)
14285 return error_mark_node;
14286 return make_conv_op_name (type);
14289 /* Parse a conversion-type-id:
14291 conversion-type-id:
14292 type-specifier-seq conversion-declarator [opt]
14294 Returns the TYPE specified. */
14296 static tree
14297 cp_parser_conversion_type_id (cp_parser* parser)
14299 tree attributes;
14300 cp_decl_specifier_seq type_specifiers;
14301 cp_declarator *declarator;
14302 tree type_specified;
14303 const char *saved_message;
14305 /* Parse the attributes. */
14306 attributes = cp_parser_attributes_opt (parser);
14308 saved_message = parser->type_definition_forbidden_message;
14309 parser->type_definition_forbidden_message
14310 = G_("types may not be defined in a conversion-type-id");
14312 /* Parse the type-specifiers. */
14313 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14314 /*is_trailing_return=*/false,
14315 &type_specifiers);
14317 parser->type_definition_forbidden_message = saved_message;
14319 /* If that didn't work, stop. */
14320 if (type_specifiers.type == error_mark_node)
14321 return error_mark_node;
14322 /* Parse the conversion-declarator. */
14323 declarator = cp_parser_conversion_declarator_opt (parser);
14325 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14326 /*initialized=*/0, &attributes);
14327 if (attributes)
14328 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14330 /* Don't give this error when parsing tentatively. This happens to
14331 work because we always parse this definitively once. */
14332 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14333 && type_uses_auto (type_specified))
14335 if (cxx_dialect < cxx14)
14337 error ("invalid use of %<auto%> in conversion operator");
14338 return error_mark_node;
14340 else if (template_parm_scope_p ())
14341 warning (0, "use of %<auto%> in member template "
14342 "conversion operator can never be deduced");
14345 return type_specified;
14348 /* Parse an (optional) conversion-declarator.
14350 conversion-declarator:
14351 ptr-operator conversion-declarator [opt]
14355 static cp_declarator *
14356 cp_parser_conversion_declarator_opt (cp_parser* parser)
14358 enum tree_code code;
14359 tree class_type, std_attributes = NULL_TREE;
14360 cp_cv_quals cv_quals;
14362 /* We don't know if there's a ptr-operator next, or not. */
14363 cp_parser_parse_tentatively (parser);
14364 /* Try the ptr-operator. */
14365 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14366 &std_attributes);
14367 /* If it worked, look for more conversion-declarators. */
14368 if (cp_parser_parse_definitely (parser))
14370 cp_declarator *declarator;
14372 /* Parse another optional declarator. */
14373 declarator = cp_parser_conversion_declarator_opt (parser);
14375 declarator = cp_parser_make_indirect_declarator
14376 (code, class_type, cv_quals, declarator, std_attributes);
14378 return declarator;
14381 return NULL;
14384 /* Parse an (optional) ctor-initializer.
14386 ctor-initializer:
14387 : mem-initializer-list */
14389 static void
14390 cp_parser_ctor_initializer_opt (cp_parser* parser)
14392 /* If the next token is not a `:', then there is no
14393 ctor-initializer. */
14394 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14396 /* Do default initialization of any bases and members. */
14397 if (DECL_CONSTRUCTOR_P (current_function_decl))
14398 finish_mem_initializers (NULL_TREE);
14399 return;
14402 /* Consume the `:' token. */
14403 cp_lexer_consume_token (parser->lexer);
14404 /* And the mem-initializer-list. */
14405 cp_parser_mem_initializer_list (parser);
14408 /* Parse a mem-initializer-list.
14410 mem-initializer-list:
14411 mem-initializer ... [opt]
14412 mem-initializer ... [opt] , mem-initializer-list */
14414 static void
14415 cp_parser_mem_initializer_list (cp_parser* parser)
14417 tree mem_initializer_list = NULL_TREE;
14418 tree target_ctor = error_mark_node;
14419 cp_token *token = cp_lexer_peek_token (parser->lexer);
14421 /* Let the semantic analysis code know that we are starting the
14422 mem-initializer-list. */
14423 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14424 error_at (token->location,
14425 "only constructors take member initializers");
14427 /* Loop through the list. */
14428 while (true)
14430 tree mem_initializer;
14432 token = cp_lexer_peek_token (parser->lexer);
14433 /* Parse the mem-initializer. */
14434 mem_initializer = cp_parser_mem_initializer (parser);
14435 /* If the next token is a `...', we're expanding member initializers. */
14436 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14438 /* Consume the `...'. */
14439 cp_lexer_consume_token (parser->lexer);
14441 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14442 can be expanded but members cannot. */
14443 if (mem_initializer != error_mark_node
14444 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14446 error_at (token->location,
14447 "cannot expand initializer for member %qD",
14448 TREE_PURPOSE (mem_initializer));
14449 mem_initializer = error_mark_node;
14452 /* Construct the pack expansion type. */
14453 if (mem_initializer != error_mark_node)
14454 mem_initializer = make_pack_expansion (mem_initializer);
14456 if (target_ctor != error_mark_node
14457 && mem_initializer != error_mark_node)
14459 error ("mem-initializer for %qD follows constructor delegation",
14460 TREE_PURPOSE (mem_initializer));
14461 mem_initializer = error_mark_node;
14463 /* Look for a target constructor. */
14464 if (mem_initializer != error_mark_node
14465 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14466 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14468 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14469 if (mem_initializer_list)
14471 error ("constructor delegation follows mem-initializer for %qD",
14472 TREE_PURPOSE (mem_initializer_list));
14473 mem_initializer = error_mark_node;
14475 target_ctor = mem_initializer;
14477 /* Add it to the list, unless it was erroneous. */
14478 if (mem_initializer != error_mark_node)
14480 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14481 mem_initializer_list = mem_initializer;
14483 /* If the next token is not a `,', we're done. */
14484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14485 break;
14486 /* Consume the `,' token. */
14487 cp_lexer_consume_token (parser->lexer);
14490 /* Perform semantic analysis. */
14491 if (DECL_CONSTRUCTOR_P (current_function_decl))
14492 finish_mem_initializers (mem_initializer_list);
14495 /* Parse a mem-initializer.
14497 mem-initializer:
14498 mem-initializer-id ( expression-list [opt] )
14499 mem-initializer-id braced-init-list
14501 GNU extension:
14503 mem-initializer:
14504 ( expression-list [opt] )
14506 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14507 class) or FIELD_DECL (for a non-static data member) to initialize;
14508 the TREE_VALUE is the expression-list. An empty initialization
14509 list is represented by void_list_node. */
14511 static tree
14512 cp_parser_mem_initializer (cp_parser* parser)
14514 tree mem_initializer_id;
14515 tree expression_list;
14516 tree member;
14517 cp_token *token = cp_lexer_peek_token (parser->lexer);
14519 /* Find out what is being initialized. */
14520 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14522 permerror (token->location,
14523 "anachronistic old-style base class initializer");
14524 mem_initializer_id = NULL_TREE;
14526 else
14528 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14529 if (mem_initializer_id == error_mark_node)
14530 return mem_initializer_id;
14532 member = expand_member_init (mem_initializer_id);
14533 if (member && !DECL_P (member))
14534 in_base_initializer = 1;
14536 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14538 bool expr_non_constant_p;
14539 cp_lexer_set_source_position (parser->lexer);
14540 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14541 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14542 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14543 expression_list = build_tree_list (NULL_TREE, expression_list);
14545 else
14547 vec<tree, va_gc> *vec;
14548 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14549 /*cast_p=*/false,
14550 /*allow_expansion_p=*/true,
14551 /*non_constant_p=*/NULL);
14552 if (vec == NULL)
14553 return error_mark_node;
14554 expression_list = build_tree_list_vec (vec);
14555 release_tree_vector (vec);
14558 if (expression_list == error_mark_node)
14559 return error_mark_node;
14560 if (!expression_list)
14561 expression_list = void_type_node;
14563 in_base_initializer = 0;
14565 return member ? build_tree_list (member, expression_list) : error_mark_node;
14568 /* Parse a mem-initializer-id.
14570 mem-initializer-id:
14571 :: [opt] nested-name-specifier [opt] class-name
14572 decltype-specifier (C++11)
14573 identifier
14575 Returns a TYPE indicating the class to be initialized for the first
14576 production (and the second in C++11). Returns an IDENTIFIER_NODE
14577 indicating the data member to be initialized for the last production. */
14579 static tree
14580 cp_parser_mem_initializer_id (cp_parser* parser)
14582 bool global_scope_p;
14583 bool nested_name_specifier_p;
14584 bool template_p = false;
14585 tree id;
14587 cp_token *token = cp_lexer_peek_token (parser->lexer);
14589 /* `typename' is not allowed in this context ([temp.res]). */
14590 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14592 error_at (token->location,
14593 "keyword %<typename%> not allowed in this context (a qualified "
14594 "member initializer is implicitly a type)");
14595 cp_lexer_consume_token (parser->lexer);
14597 /* Look for the optional `::' operator. */
14598 global_scope_p
14599 = (cp_parser_global_scope_opt (parser,
14600 /*current_scope_valid_p=*/false)
14601 != NULL_TREE);
14602 /* Look for the optional nested-name-specifier. The simplest way to
14603 implement:
14605 [temp.res]
14607 The keyword `typename' is not permitted in a base-specifier or
14608 mem-initializer; in these contexts a qualified name that
14609 depends on a template-parameter is implicitly assumed to be a
14610 type name.
14612 is to assume that we have seen the `typename' keyword at this
14613 point. */
14614 nested_name_specifier_p
14615 = (cp_parser_nested_name_specifier_opt (parser,
14616 /*typename_keyword_p=*/true,
14617 /*check_dependency_p=*/true,
14618 /*type_p=*/true,
14619 /*is_declaration=*/true)
14620 != NULL_TREE);
14621 if (nested_name_specifier_p)
14622 template_p = cp_parser_optional_template_keyword (parser);
14623 /* If there is a `::' operator or a nested-name-specifier, then we
14624 are definitely looking for a class-name. */
14625 if (global_scope_p || nested_name_specifier_p)
14626 return cp_parser_class_name (parser,
14627 /*typename_keyword_p=*/true,
14628 /*template_keyword_p=*/template_p,
14629 typename_type,
14630 /*check_dependency_p=*/true,
14631 /*class_head_p=*/false,
14632 /*is_declaration=*/true);
14633 /* Otherwise, we could also be looking for an ordinary identifier. */
14634 cp_parser_parse_tentatively (parser);
14635 if (cp_lexer_next_token_is_decltype (parser->lexer))
14636 /* Try a decltype-specifier. */
14637 id = cp_parser_decltype (parser);
14638 else
14639 /* Otherwise, try a class-name. */
14640 id = cp_parser_class_name (parser,
14641 /*typename_keyword_p=*/true,
14642 /*template_keyword_p=*/false,
14643 none_type,
14644 /*check_dependency_p=*/true,
14645 /*class_head_p=*/false,
14646 /*is_declaration=*/true);
14647 /* If we found one, we're done. */
14648 if (cp_parser_parse_definitely (parser))
14649 return id;
14650 /* Otherwise, look for an ordinary identifier. */
14651 return cp_parser_identifier (parser);
14654 /* Overloading [gram.over] */
14656 /* Parse an operator-function-id.
14658 operator-function-id:
14659 operator operator
14661 Returns an IDENTIFIER_NODE for the operator which is a
14662 human-readable spelling of the identifier, e.g., `operator +'. */
14664 static cp_expr
14665 cp_parser_operator_function_id (cp_parser* parser)
14667 /* Look for the `operator' keyword. */
14668 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14669 return error_mark_node;
14670 /* And then the name of the operator itself. */
14671 return cp_parser_operator (parser);
14674 /* Return an identifier node for a user-defined literal operator.
14675 The suffix identifier is chained to the operator name identifier. */
14677 tree
14678 cp_literal_operator_id (const char* name)
14680 tree identifier;
14681 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14682 + strlen (name) + 10);
14683 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14684 identifier = get_identifier (buffer);
14686 return identifier;
14689 /* Parse an operator.
14691 operator:
14692 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14693 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14694 || ++ -- , ->* -> () []
14696 GNU Extensions:
14698 operator:
14699 <? >? <?= >?=
14701 Returns an IDENTIFIER_NODE for the operator which is a
14702 human-readable spelling of the identifier, e.g., `operator +'. */
14704 static cp_expr
14705 cp_parser_operator (cp_parser* parser)
14707 tree id = NULL_TREE;
14708 cp_token *token;
14709 bool utf8 = false;
14711 /* Peek at the next token. */
14712 token = cp_lexer_peek_token (parser->lexer);
14714 location_t start_loc = token->location;
14716 /* Figure out which operator we have. */
14717 enum tree_code op = ERROR_MARK;
14718 bool assop = false;
14719 bool consumed = false;
14720 switch (token->type)
14722 case CPP_KEYWORD:
14724 /* The keyword should be either `new' or `delete'. */
14725 if (token->keyword == RID_NEW)
14726 op = NEW_EXPR;
14727 else if (token->keyword == RID_DELETE)
14728 op = DELETE_EXPR;
14729 else
14730 break;
14732 /* Consume the `new' or `delete' token. */
14733 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14735 /* Peek at the next token. */
14736 token = cp_lexer_peek_token (parser->lexer);
14737 /* If it's a `[' token then this is the array variant of the
14738 operator. */
14739 if (token->type == CPP_OPEN_SQUARE)
14741 /* Consume the `[' token. */
14742 cp_lexer_consume_token (parser->lexer);
14743 /* Look for the `]' token. */
14744 if (cp_token *close_token
14745 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14746 end_loc = close_token->location;
14747 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14749 start_loc = make_location (start_loc, start_loc, end_loc);
14750 consumed = true;
14751 break;
14754 case CPP_PLUS:
14755 op = PLUS_EXPR;
14756 break;
14758 case CPP_MINUS:
14759 op = MINUS_EXPR;
14760 break;
14762 case CPP_MULT:
14763 op = MULT_EXPR;
14764 break;
14766 case CPP_DIV:
14767 op = TRUNC_DIV_EXPR;
14768 break;
14770 case CPP_MOD:
14771 op = TRUNC_MOD_EXPR;
14772 break;
14774 case CPP_XOR:
14775 op = BIT_XOR_EXPR;
14776 break;
14778 case CPP_AND:
14779 op = BIT_AND_EXPR;
14780 break;
14782 case CPP_OR:
14783 op = BIT_IOR_EXPR;
14784 break;
14786 case CPP_COMPL:
14787 op = BIT_NOT_EXPR;
14788 break;
14790 case CPP_NOT:
14791 op = TRUTH_NOT_EXPR;
14792 break;
14794 case CPP_EQ:
14795 assop = true;
14796 op = NOP_EXPR;
14797 break;
14799 case CPP_LESS:
14800 op = LT_EXPR;
14801 break;
14803 case CPP_GREATER:
14804 op = GT_EXPR;
14805 break;
14807 case CPP_PLUS_EQ:
14808 assop = true;
14809 op = PLUS_EXPR;
14810 break;
14812 case CPP_MINUS_EQ:
14813 assop = true;
14814 op = MINUS_EXPR;
14815 break;
14817 case CPP_MULT_EQ:
14818 assop = true;
14819 op = MULT_EXPR;
14820 break;
14822 case CPP_DIV_EQ:
14823 assop = true;
14824 op = TRUNC_DIV_EXPR;
14825 break;
14827 case CPP_MOD_EQ:
14828 assop = true;
14829 op = TRUNC_MOD_EXPR;
14830 break;
14832 case CPP_XOR_EQ:
14833 assop = true;
14834 op = BIT_XOR_EXPR;
14835 break;
14837 case CPP_AND_EQ:
14838 assop = true;
14839 op = BIT_AND_EXPR;
14840 break;
14842 case CPP_OR_EQ:
14843 assop = true;
14844 op = BIT_IOR_EXPR;
14845 break;
14847 case CPP_LSHIFT:
14848 op = LSHIFT_EXPR;
14849 break;
14851 case CPP_RSHIFT:
14852 op = RSHIFT_EXPR;
14853 break;
14855 case CPP_LSHIFT_EQ:
14856 assop = true;
14857 op = LSHIFT_EXPR;
14858 break;
14860 case CPP_RSHIFT_EQ:
14861 assop = true;
14862 op = RSHIFT_EXPR;
14863 break;
14865 case CPP_EQ_EQ:
14866 op = EQ_EXPR;
14867 break;
14869 case CPP_NOT_EQ:
14870 op = NE_EXPR;
14871 break;
14873 case CPP_LESS_EQ:
14874 op = LE_EXPR;
14875 break;
14877 case CPP_GREATER_EQ:
14878 op = GE_EXPR;
14879 break;
14881 case CPP_AND_AND:
14882 op = TRUTH_ANDIF_EXPR;
14883 break;
14885 case CPP_OR_OR:
14886 op = TRUTH_ORIF_EXPR;
14887 break;
14889 case CPP_PLUS_PLUS:
14890 op = POSTINCREMENT_EXPR;
14891 break;
14893 case CPP_MINUS_MINUS:
14894 op = PREDECREMENT_EXPR;
14895 break;
14897 case CPP_COMMA:
14898 op = COMPOUND_EXPR;
14899 break;
14901 case CPP_DEREF_STAR:
14902 op = MEMBER_REF;
14903 break;
14905 case CPP_DEREF:
14906 op = COMPONENT_REF;
14907 break;
14909 case CPP_OPEN_PAREN:
14911 /* Consume the `('. */
14912 matching_parens parens;
14913 parens.consume_open (parser);
14914 /* Look for the matching `)'. */
14915 parens.require_close (parser);
14916 op = CALL_EXPR;
14917 consumed = true;
14918 break;
14921 case CPP_OPEN_SQUARE:
14922 /* Consume the `['. */
14923 cp_lexer_consume_token (parser->lexer);
14924 /* Look for the matching `]'. */
14925 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14926 op = ARRAY_REF;
14927 consumed = true;
14928 break;
14930 case CPP_UTF8STRING:
14931 case CPP_UTF8STRING_USERDEF:
14932 utf8 = true;
14933 /* FALLTHRU */
14934 case CPP_STRING:
14935 case CPP_WSTRING:
14936 case CPP_STRING16:
14937 case CPP_STRING32:
14938 case CPP_STRING_USERDEF:
14939 case CPP_WSTRING_USERDEF:
14940 case CPP_STRING16_USERDEF:
14941 case CPP_STRING32_USERDEF:
14943 tree str, string_tree;
14944 int sz, len;
14946 if (cxx_dialect == cxx98)
14947 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14949 /* Consume the string. */
14950 str = cp_parser_string_literal (parser, /*translate=*/true,
14951 /*wide_ok=*/true, /*lookup_udlit=*/false);
14952 if (str == error_mark_node)
14953 return error_mark_node;
14954 else if (TREE_CODE (str) == USERDEF_LITERAL)
14956 string_tree = USERDEF_LITERAL_VALUE (str);
14957 id = USERDEF_LITERAL_SUFFIX_ID (str);
14959 else
14961 string_tree = str;
14962 /* Look for the suffix identifier. */
14963 token = cp_lexer_peek_token (parser->lexer);
14964 if (token->type == CPP_NAME)
14965 id = cp_parser_identifier (parser);
14966 else if (token->type == CPP_KEYWORD)
14968 error ("unexpected keyword;"
14969 " remove space between quotes and suffix identifier");
14970 return error_mark_node;
14972 else
14974 error ("expected suffix identifier");
14975 return error_mark_node;
14978 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14979 (TREE_TYPE (TREE_TYPE (string_tree))));
14980 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14981 if (len != 0)
14983 error ("expected empty string after %<operator%> keyword");
14984 return error_mark_node;
14986 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14987 != char_type_node)
14989 error ("invalid encoding prefix in literal operator");
14990 return error_mark_node;
14992 if (id != error_mark_node)
14994 const char *name = IDENTIFIER_POINTER (id);
14995 id = cp_literal_operator_id (name);
14997 return id;
15000 default:
15001 /* Anything else is an error. */
15002 break;
15005 /* If we have selected an identifier, we need to consume the
15006 operator token. */
15007 if (op != ERROR_MARK)
15009 id = ovl_op_identifier (assop, op);
15010 if (!consumed)
15011 cp_lexer_consume_token (parser->lexer);
15013 /* Otherwise, no valid operator name was present. */
15014 else
15016 cp_parser_error (parser, "expected operator");
15017 id = error_mark_node;
15020 return cp_expr (id, start_loc);
15023 /* Parse a template-declaration.
15025 template-declaration:
15026 export [opt] template < template-parameter-list > declaration
15028 If MEMBER_P is TRUE, this template-declaration occurs within a
15029 class-specifier.
15031 The grammar rule given by the standard isn't correct. What
15032 is really meant is:
15034 template-declaration:
15035 export [opt] template-parameter-list-seq
15036 decl-specifier-seq [opt] init-declarator [opt] ;
15037 export [opt] template-parameter-list-seq
15038 function-definition
15040 template-parameter-list-seq:
15041 template-parameter-list-seq [opt]
15042 template < template-parameter-list >
15044 Concept Extensions:
15046 template-parameter-list-seq:
15047 template < template-parameter-list > requires-clause [opt]
15049 requires-clause:
15050 requires logical-or-expression */
15052 static void
15053 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15055 /* Check for `export'. */
15056 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15058 /* Consume the `export' token. */
15059 cp_lexer_consume_token (parser->lexer);
15060 /* Warn that we do not support `export'. */
15061 warning (0, "keyword %<export%> not implemented, and will be ignored");
15064 cp_parser_template_declaration_after_export (parser, member_p);
15067 /* Parse a template-parameter-list.
15069 template-parameter-list:
15070 template-parameter
15071 template-parameter-list , template-parameter
15073 Returns a TREE_LIST. Each node represents a template parameter.
15074 The nodes are connected via their TREE_CHAINs. */
15076 static tree
15077 cp_parser_template_parameter_list (cp_parser* parser)
15079 tree parameter_list = NULL_TREE;
15081 begin_template_parm_list ();
15083 /* The loop below parses the template parms. We first need to know
15084 the total number of template parms to be able to compute proper
15085 canonical types of each dependent type. So after the loop, when
15086 we know the total number of template parms,
15087 end_template_parm_list computes the proper canonical types and
15088 fixes up the dependent types accordingly. */
15089 while (true)
15091 tree parameter;
15092 bool is_non_type;
15093 bool is_parameter_pack;
15094 location_t parm_loc;
15096 /* Parse the template-parameter. */
15097 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15098 parameter = cp_parser_template_parameter (parser,
15099 &is_non_type,
15100 &is_parameter_pack);
15101 /* Add it to the list. */
15102 if (parameter != error_mark_node)
15103 parameter_list = process_template_parm (parameter_list,
15104 parm_loc,
15105 parameter,
15106 is_non_type,
15107 is_parameter_pack);
15108 else
15110 tree err_parm = build_tree_list (parameter, parameter);
15111 parameter_list = chainon (parameter_list, err_parm);
15114 /* If the next token is not a `,', we're done. */
15115 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15116 break;
15117 /* Otherwise, consume the `,' token. */
15118 cp_lexer_consume_token (parser->lexer);
15121 return end_template_parm_list (parameter_list);
15124 /* Parse a introduction-list.
15126 introduction-list:
15127 introduced-parameter
15128 introduction-list , introduced-parameter
15130 introduced-parameter:
15131 ...[opt] identifier
15133 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15134 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15135 WILDCARD_DECL will also have DECL_NAME set and token location in
15136 DECL_SOURCE_LOCATION. */
15138 static tree
15139 cp_parser_introduction_list (cp_parser *parser)
15141 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15143 while (true)
15145 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15146 if (is_pack)
15147 cp_lexer_consume_token (parser->lexer);
15149 /* Build placeholder. */
15150 tree parm = build_nt (WILDCARD_DECL);
15151 DECL_SOURCE_LOCATION (parm)
15152 = cp_lexer_peek_token (parser->lexer)->location;
15153 DECL_NAME (parm) = cp_parser_identifier (parser);
15154 WILDCARD_PACK_P (parm) = is_pack;
15155 vec_safe_push (introduction_vec, parm);
15157 /* If the next token is not a `,', we're done. */
15158 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15159 break;
15160 /* Otherwise, consume the `,' token. */
15161 cp_lexer_consume_token (parser->lexer);
15164 /* Convert the vec into a TREE_VEC. */
15165 tree introduction_list = make_tree_vec (introduction_vec->length ());
15166 unsigned int n;
15167 tree parm;
15168 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15169 TREE_VEC_ELT (introduction_list, n) = parm;
15171 release_tree_vector (introduction_vec);
15172 return introduction_list;
15175 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15176 is an abstract declarator. */
15178 static inline cp_declarator*
15179 get_id_declarator (cp_declarator *declarator)
15181 cp_declarator *d = declarator;
15182 while (d && d->kind != cdk_id)
15183 d = d->declarator;
15184 return d;
15187 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15188 is an abstract declarator. */
15190 static inline tree
15191 get_unqualified_id (cp_declarator *declarator)
15193 declarator = get_id_declarator (declarator);
15194 if (declarator)
15195 return declarator->u.id.unqualified_name;
15196 else
15197 return NULL_TREE;
15200 /* Returns true if DECL represents a constrained-parameter. */
15202 static inline bool
15203 is_constrained_parameter (tree decl)
15205 return (decl
15206 && TREE_CODE (decl) == TYPE_DECL
15207 && CONSTRAINED_PARM_CONCEPT (decl)
15208 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15211 /* Returns true if PARM declares a constrained-parameter. */
15213 static inline bool
15214 is_constrained_parameter (cp_parameter_declarator *parm)
15216 return is_constrained_parameter (parm->decl_specifiers.type);
15219 /* Check that the type parameter is only a declarator-id, and that its
15220 type is not cv-qualified. */
15222 bool
15223 cp_parser_check_constrained_type_parm (cp_parser *parser,
15224 cp_parameter_declarator *parm)
15226 if (!parm->declarator)
15227 return true;
15229 if (parm->declarator->kind != cdk_id)
15231 cp_parser_error (parser, "invalid constrained type parameter");
15232 return false;
15235 /* Don't allow cv-qualified type parameters. */
15236 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15237 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15239 cp_parser_error (parser, "cv-qualified type parameter");
15240 return false;
15243 return true;
15246 /* Finish parsing/processing a template type parameter and checking
15247 various restrictions. */
15249 static inline tree
15250 cp_parser_constrained_type_template_parm (cp_parser *parser,
15251 tree id,
15252 cp_parameter_declarator* parmdecl)
15254 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15255 return finish_template_type_parm (class_type_node, id);
15256 else
15257 return error_mark_node;
15260 static tree
15261 finish_constrained_template_template_parm (tree proto, tree id)
15263 /* FIXME: This should probably be copied, and we may need to adjust
15264 the template parameter depths. */
15265 tree saved_parms = current_template_parms;
15266 begin_template_parm_list ();
15267 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15268 end_template_parm_list ();
15270 tree parm = finish_template_template_parm (class_type_node, id);
15271 current_template_parms = saved_parms;
15273 return parm;
15276 /* Finish parsing/processing a template template parameter by borrowing
15277 the template parameter list from the prototype parameter. */
15279 static tree
15280 cp_parser_constrained_template_template_parm (cp_parser *parser,
15281 tree proto,
15282 tree id,
15283 cp_parameter_declarator *parmdecl)
15285 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15286 return error_mark_node;
15287 return finish_constrained_template_template_parm (proto, id);
15290 /* Create a new non-type template parameter from the given PARM
15291 declarator. */
15293 static tree
15294 constrained_non_type_template_parm (bool *is_non_type,
15295 cp_parameter_declarator *parm)
15297 *is_non_type = true;
15298 cp_declarator *decl = parm->declarator;
15299 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15300 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15301 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15304 /* Build a constrained template parameter based on the PARMDECL
15305 declarator. The type of PARMDECL is the constrained type, which
15306 refers to the prototype template parameter that ultimately
15307 specifies the type of the declared parameter. */
15309 static tree
15310 finish_constrained_parameter (cp_parser *parser,
15311 cp_parameter_declarator *parmdecl,
15312 bool *is_non_type,
15313 bool *is_parameter_pack)
15315 tree decl = parmdecl->decl_specifiers.type;
15316 tree id = get_unqualified_id (parmdecl->declarator);
15317 tree def = parmdecl->default_argument;
15318 tree proto = DECL_INITIAL (decl);
15320 /* A template parameter constrained by a variadic concept shall also
15321 be declared as a template parameter pack. */
15322 bool is_variadic = template_parameter_pack_p (proto);
15323 if (is_variadic && !*is_parameter_pack)
15324 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15326 /* Build the parameter. Return an error if the declarator was invalid. */
15327 tree parm;
15328 if (TREE_CODE (proto) == TYPE_DECL)
15329 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15330 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15331 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15332 parmdecl);
15333 else
15334 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15335 if (parm == error_mark_node)
15336 return error_mark_node;
15338 /* Finish the parameter decl and create a node attaching the
15339 default argument and constraint. */
15340 parm = build_tree_list (def, parm);
15341 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15343 return parm;
15346 /* Returns true if the parsed type actually represents the declaration
15347 of a type template-parameter. */
15349 static inline bool
15350 declares_constrained_type_template_parameter (tree type)
15352 return (is_constrained_parameter (type)
15353 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15357 /* Returns true if the parsed type actually represents the declaration of
15358 a template template-parameter. */
15360 static bool
15361 declares_constrained_template_template_parameter (tree type)
15363 return (is_constrained_parameter (type)
15364 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15367 /* Parse a default argument for a type template-parameter.
15368 Note that diagnostics are handled in cp_parser_template_parameter. */
15370 static tree
15371 cp_parser_default_type_template_argument (cp_parser *parser)
15373 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15375 /* Consume the `=' token. */
15376 cp_lexer_consume_token (parser->lexer);
15378 cp_token *token = cp_lexer_peek_token (parser->lexer);
15380 /* Parse the default-argument. */
15381 push_deferring_access_checks (dk_no_deferred);
15382 tree default_argument = cp_parser_type_id (parser);
15383 pop_deferring_access_checks ();
15385 if (flag_concepts && type_uses_auto (default_argument))
15387 error_at (token->location,
15388 "invalid use of %<auto%> in default template argument");
15389 return error_mark_node;
15392 return default_argument;
15395 /* Parse a default argument for a template template-parameter. */
15397 static tree
15398 cp_parser_default_template_template_argument (cp_parser *parser)
15400 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15402 bool is_template;
15404 /* Consume the `='. */
15405 cp_lexer_consume_token (parser->lexer);
15406 /* Parse the id-expression. */
15407 push_deferring_access_checks (dk_no_deferred);
15408 /* save token before parsing the id-expression, for error
15409 reporting */
15410 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15411 tree default_argument
15412 = cp_parser_id_expression (parser,
15413 /*template_keyword_p=*/false,
15414 /*check_dependency_p=*/true,
15415 /*template_p=*/&is_template,
15416 /*declarator_p=*/false,
15417 /*optional_p=*/false);
15418 if (TREE_CODE (default_argument) == TYPE_DECL)
15419 /* If the id-expression was a template-id that refers to
15420 a template-class, we already have the declaration here,
15421 so no further lookup is needed. */
15423 else
15424 /* Look up the name. */
15425 default_argument
15426 = cp_parser_lookup_name (parser, default_argument,
15427 none_type,
15428 /*is_template=*/is_template,
15429 /*is_namespace=*/false,
15430 /*check_dependency=*/true,
15431 /*ambiguous_decls=*/NULL,
15432 token->location);
15433 /* See if the default argument is valid. */
15434 default_argument = check_template_template_default_arg (default_argument);
15435 pop_deferring_access_checks ();
15436 return default_argument;
15439 /* Parse a template-parameter.
15441 template-parameter:
15442 type-parameter
15443 parameter-declaration
15445 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15446 the parameter. The TREE_PURPOSE is the default value, if any.
15447 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15448 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15449 set to true iff this parameter is a parameter pack. */
15451 static tree
15452 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15453 bool *is_parameter_pack)
15455 cp_token *token;
15456 cp_parameter_declarator *parameter_declarator;
15457 tree parm;
15459 /* Assume it is a type parameter or a template parameter. */
15460 *is_non_type = false;
15461 /* Assume it not a parameter pack. */
15462 *is_parameter_pack = false;
15463 /* Peek at the next token. */
15464 token = cp_lexer_peek_token (parser->lexer);
15465 /* If it is `template', we have a type-parameter. */
15466 if (token->keyword == RID_TEMPLATE)
15467 return cp_parser_type_parameter (parser, is_parameter_pack);
15468 /* If it is `class' or `typename' we do not know yet whether it is a
15469 type parameter or a non-type parameter. Consider:
15471 template <typename T, typename T::X X> ...
15475 template <class C, class D*> ...
15477 Here, the first parameter is a type parameter, and the second is
15478 a non-type parameter. We can tell by looking at the token after
15479 the identifier -- if it is a `,', `=', or `>' then we have a type
15480 parameter. */
15481 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15483 /* Peek at the token after `class' or `typename'. */
15484 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15485 /* If it's an ellipsis, we have a template type parameter
15486 pack. */
15487 if (token->type == CPP_ELLIPSIS)
15488 return cp_parser_type_parameter (parser, is_parameter_pack);
15489 /* If it's an identifier, skip it. */
15490 if (token->type == CPP_NAME)
15491 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15492 /* Now, see if the token looks like the end of a template
15493 parameter. */
15494 if (token->type == CPP_COMMA
15495 || token->type == CPP_EQ
15496 || token->type == CPP_GREATER)
15497 return cp_parser_type_parameter (parser, is_parameter_pack);
15500 /* Otherwise, it is a non-type parameter or a constrained parameter.
15502 [temp.param]
15504 When parsing a default template-argument for a non-type
15505 template-parameter, the first non-nested `>' is taken as the end
15506 of the template parameter-list rather than a greater-than
15507 operator. */
15508 parameter_declarator
15509 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15510 /*parenthesized_p=*/NULL);
15512 if (!parameter_declarator)
15513 return error_mark_node;
15515 /* If the parameter declaration is marked as a parameter pack, set
15516 *IS_PARAMETER_PACK to notify the caller. */
15517 if (parameter_declarator->template_parameter_pack_p)
15518 *is_parameter_pack = true;
15520 if (parameter_declarator->default_argument)
15522 /* Can happen in some cases of erroneous input (c++/34892). */
15523 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15524 /* Consume the `...' for better error recovery. */
15525 cp_lexer_consume_token (parser->lexer);
15528 // The parameter may have been constrained.
15529 if (is_constrained_parameter (parameter_declarator))
15530 return finish_constrained_parameter (parser,
15531 parameter_declarator,
15532 is_non_type,
15533 is_parameter_pack);
15535 // Now we're sure that the parameter is a non-type parameter.
15536 *is_non_type = true;
15538 parm = grokdeclarator (parameter_declarator->declarator,
15539 &parameter_declarator->decl_specifiers,
15540 TPARM, /*initialized=*/0,
15541 /*attrlist=*/NULL);
15542 if (parm == error_mark_node)
15543 return error_mark_node;
15545 return build_tree_list (parameter_declarator->default_argument, parm);
15548 /* Parse a type-parameter.
15550 type-parameter:
15551 class identifier [opt]
15552 class identifier [opt] = type-id
15553 typename identifier [opt]
15554 typename identifier [opt] = type-id
15555 template < template-parameter-list > class identifier [opt]
15556 template < template-parameter-list > class identifier [opt]
15557 = id-expression
15559 GNU Extension (variadic templates):
15561 type-parameter:
15562 class ... identifier [opt]
15563 typename ... identifier [opt]
15565 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15566 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15567 the declaration of the parameter.
15569 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15571 static tree
15572 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15574 cp_token *token;
15575 tree parameter;
15577 /* Look for a keyword to tell us what kind of parameter this is. */
15578 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15579 if (!token)
15580 return error_mark_node;
15582 switch (token->keyword)
15584 case RID_CLASS:
15585 case RID_TYPENAME:
15587 tree identifier;
15588 tree default_argument;
15590 /* If the next token is an ellipsis, we have a template
15591 argument pack. */
15592 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15594 /* Consume the `...' token. */
15595 cp_lexer_consume_token (parser->lexer);
15596 maybe_warn_variadic_templates ();
15598 *is_parameter_pack = true;
15601 /* If the next token is an identifier, then it names the
15602 parameter. */
15603 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15604 identifier = cp_parser_identifier (parser);
15605 else
15606 identifier = NULL_TREE;
15608 /* Create the parameter. */
15609 parameter = finish_template_type_parm (class_type_node, identifier);
15611 /* If the next token is an `=', we have a default argument. */
15612 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15614 default_argument
15615 = cp_parser_default_type_template_argument (parser);
15617 /* Template parameter packs cannot have default
15618 arguments. */
15619 if (*is_parameter_pack)
15621 if (identifier)
15622 error_at (token->location,
15623 "template parameter pack %qD cannot have a "
15624 "default argument", identifier);
15625 else
15626 error_at (token->location,
15627 "template parameter packs cannot have "
15628 "default arguments");
15629 default_argument = NULL_TREE;
15631 else if (check_for_bare_parameter_packs (default_argument))
15632 default_argument = error_mark_node;
15634 else
15635 default_argument = NULL_TREE;
15637 /* Create the combined representation of the parameter and the
15638 default argument. */
15639 parameter = build_tree_list (default_argument, parameter);
15641 break;
15643 case RID_TEMPLATE:
15645 tree identifier;
15646 tree default_argument;
15648 /* Look for the `<'. */
15649 cp_parser_require (parser, CPP_LESS, RT_LESS);
15650 /* Parse the template-parameter-list. */
15651 cp_parser_template_parameter_list (parser);
15652 /* Look for the `>'. */
15653 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15655 // If template requirements are present, parse them.
15656 if (flag_concepts)
15658 tree reqs = get_shorthand_constraints (current_template_parms);
15659 if (tree r = cp_parser_requires_clause_opt (parser))
15660 reqs = conjoin_constraints (reqs, normalize_expression (r));
15661 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15664 /* Look for the `class' or 'typename' keywords. */
15665 cp_parser_type_parameter_key (parser);
15666 /* If the next token is an ellipsis, we have a template
15667 argument pack. */
15668 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15670 /* Consume the `...' token. */
15671 cp_lexer_consume_token (parser->lexer);
15672 maybe_warn_variadic_templates ();
15674 *is_parameter_pack = true;
15676 /* If the next token is an `=', then there is a
15677 default-argument. If the next token is a `>', we are at
15678 the end of the parameter-list. If the next token is a `,',
15679 then we are at the end of this parameter. */
15680 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15681 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15682 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15684 identifier = cp_parser_identifier (parser);
15685 /* Treat invalid names as if the parameter were nameless. */
15686 if (identifier == error_mark_node)
15687 identifier = NULL_TREE;
15689 else
15690 identifier = NULL_TREE;
15692 /* Create the template parameter. */
15693 parameter = finish_template_template_parm (class_type_node,
15694 identifier);
15696 /* If the next token is an `=', then there is a
15697 default-argument. */
15698 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15700 default_argument
15701 = cp_parser_default_template_template_argument (parser);
15703 /* Template parameter packs cannot have default
15704 arguments. */
15705 if (*is_parameter_pack)
15707 if (identifier)
15708 error_at (token->location,
15709 "template parameter pack %qD cannot "
15710 "have a default argument",
15711 identifier);
15712 else
15713 error_at (token->location, "template parameter packs cannot "
15714 "have default arguments");
15715 default_argument = NULL_TREE;
15718 else
15719 default_argument = NULL_TREE;
15721 /* Create the combined representation of the parameter and the
15722 default argument. */
15723 parameter = build_tree_list (default_argument, parameter);
15725 break;
15727 default:
15728 gcc_unreachable ();
15729 break;
15732 return parameter;
15735 /* Parse a template-id.
15737 template-id:
15738 template-name < template-argument-list [opt] >
15740 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15741 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15742 returned. Otherwise, if the template-name names a function, or set
15743 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15744 names a class, returns a TYPE_DECL for the specialization.
15746 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15747 uninstantiated templates. */
15749 static tree
15750 cp_parser_template_id (cp_parser *parser,
15751 bool template_keyword_p,
15752 bool check_dependency_p,
15753 enum tag_types tag_type,
15754 bool is_declaration)
15756 tree templ;
15757 tree arguments;
15758 tree template_id;
15759 cp_token_position start_of_id = 0;
15760 cp_token *next_token = NULL, *next_token_2 = NULL;
15761 bool is_identifier;
15763 /* If the next token corresponds to a template-id, there is no need
15764 to reparse it. */
15765 cp_token *token = cp_lexer_peek_token (parser->lexer);
15766 if (token->type == CPP_TEMPLATE_ID)
15768 cp_lexer_consume_token (parser->lexer);
15769 return saved_checks_value (token->u.tree_check_value);
15772 /* Avoid performing name lookup if there is no possibility of
15773 finding a template-id. */
15774 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15775 || (token->type == CPP_NAME
15776 && !cp_parser_nth_token_starts_template_argument_list_p
15777 (parser, 2)))
15779 cp_parser_error (parser, "expected template-id");
15780 return error_mark_node;
15783 /* Remember where the template-id starts. */
15784 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15785 start_of_id = cp_lexer_token_position (parser->lexer, false);
15787 push_deferring_access_checks (dk_deferred);
15789 /* Parse the template-name. */
15790 is_identifier = false;
15791 templ = cp_parser_template_name (parser, template_keyword_p,
15792 check_dependency_p,
15793 is_declaration,
15794 tag_type,
15795 &is_identifier);
15796 if (templ == error_mark_node || is_identifier)
15798 pop_deferring_access_checks ();
15799 return templ;
15802 /* Since we're going to preserve any side-effects from this parse, set up a
15803 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15804 in the template arguments. */
15805 tentative_firewall firewall (parser);
15807 /* If we find the sequence `[:' after a template-name, it's probably
15808 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15809 parse correctly the argument list. */
15810 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15811 == CPP_OPEN_SQUARE)
15812 && next_token->flags & DIGRAPH
15813 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15814 == CPP_COLON)
15815 && !(next_token_2->flags & PREV_WHITE))
15817 cp_parser_parse_tentatively (parser);
15818 /* Change `:' into `::'. */
15819 next_token_2->type = CPP_SCOPE;
15820 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15821 CPP_LESS. */
15822 cp_lexer_consume_token (parser->lexer);
15824 /* Parse the arguments. */
15825 arguments = cp_parser_enclosed_template_argument_list (parser);
15826 if (!cp_parser_parse_definitely (parser))
15828 /* If we couldn't parse an argument list, then we revert our changes
15829 and return simply an error. Maybe this is not a template-id
15830 after all. */
15831 next_token_2->type = CPP_COLON;
15832 cp_parser_error (parser, "expected %<<%>");
15833 pop_deferring_access_checks ();
15834 return error_mark_node;
15836 /* Otherwise, emit an error about the invalid digraph, but continue
15837 parsing because we got our argument list. */
15838 if (permerror (next_token->location,
15839 "%<<::%> cannot begin a template-argument list"))
15841 static bool hint = false;
15842 inform (next_token->location,
15843 "%<<:%> is an alternate spelling for %<[%>."
15844 " Insert whitespace between %<<%> and %<::%>");
15845 if (!hint && !flag_permissive)
15847 inform (next_token->location, "(if you use %<-fpermissive%> "
15848 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15849 "accept your code)");
15850 hint = true;
15854 else
15856 /* Look for the `<' that starts the template-argument-list. */
15857 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15859 pop_deferring_access_checks ();
15860 return error_mark_node;
15862 /* Parse the arguments. */
15863 arguments = cp_parser_enclosed_template_argument_list (parser);
15866 /* Set the location to be of the form:
15867 template-name < template-argument-list [opt] >
15868 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15869 with caret == start at the start of the template-name,
15870 ranging until the closing '>'. */
15871 location_t finish_loc
15872 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15873 location_t combined_loc
15874 = make_location (token->location, token->location, finish_loc);
15876 /* Build a representation of the specialization. */
15877 if (identifier_p (templ))
15878 template_id = build_min_nt_loc (combined_loc,
15879 TEMPLATE_ID_EXPR,
15880 templ, arguments);
15881 else if (DECL_TYPE_TEMPLATE_P (templ)
15882 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15884 bool entering_scope;
15885 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15886 template (rather than some instantiation thereof) only if
15887 is not nested within some other construct. For example, in
15888 "template <typename T> void f(T) { A<T>::", A<T> is just an
15889 instantiation of A. */
15890 entering_scope = (template_parm_scope_p ()
15891 && cp_lexer_next_token_is (parser->lexer,
15892 CPP_SCOPE));
15893 template_id
15894 = finish_template_type (templ, arguments, entering_scope);
15896 /* A template-like identifier may be a partial concept id. */
15897 else if (flag_concepts
15898 && (template_id = (cp_parser_maybe_partial_concept_id
15899 (parser, templ, arguments))))
15900 return template_id;
15901 else if (variable_template_p (templ))
15903 template_id = lookup_template_variable (templ, arguments);
15904 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15905 SET_EXPR_LOCATION (template_id, combined_loc);
15907 else
15909 /* If it's not a class-template or a template-template, it should be
15910 a function-template. */
15911 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15912 || TREE_CODE (templ) == OVERLOAD
15913 || BASELINK_P (templ)));
15915 template_id = lookup_template_function (templ, arguments);
15916 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15917 SET_EXPR_LOCATION (template_id, combined_loc);
15920 /* If parsing tentatively, replace the sequence of tokens that makes
15921 up the template-id with a CPP_TEMPLATE_ID token. That way,
15922 should we re-parse the token stream, we will not have to repeat
15923 the effort required to do the parse, nor will we issue duplicate
15924 error messages about problems during instantiation of the
15925 template. */
15926 if (start_of_id
15927 /* Don't do this if we had a parse error in a declarator; re-parsing
15928 might succeed if a name changes meaning (60361). */
15929 && !(cp_parser_error_occurred (parser)
15930 && cp_parser_parsing_tentatively (parser)
15931 && parser->in_declarator_p))
15933 /* Reset the contents of the START_OF_ID token. */
15934 token->type = CPP_TEMPLATE_ID;
15935 token->location = combined_loc;
15937 /* We must mark the lookup as kept, so we don't throw it away on
15938 the first parse. */
15939 if (is_overloaded_fn (template_id))
15940 lookup_keep (get_fns (template_id), true);
15942 /* Retrieve any deferred checks. Do not pop this access checks yet
15943 so the memory will not be reclaimed during token replacing below. */
15944 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15945 token->u.tree_check_value->value = template_id;
15946 token->u.tree_check_value->checks = get_deferred_access_checks ();
15947 token->keyword = RID_MAX;
15949 /* Purge all subsequent tokens. */
15950 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15952 /* ??? Can we actually assume that, if template_id ==
15953 error_mark_node, we will have issued a diagnostic to the
15954 user, as opposed to simply marking the tentative parse as
15955 failed? */
15956 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15957 error_at (token->location, "parse error in template argument list");
15960 pop_to_parent_deferring_access_checks ();
15961 return template_id;
15964 /* Parse a template-name.
15966 template-name:
15967 identifier
15969 The standard should actually say:
15971 template-name:
15972 identifier
15973 operator-function-id
15975 A defect report has been filed about this issue.
15977 A conversion-function-id cannot be a template name because they cannot
15978 be part of a template-id. In fact, looking at this code:
15980 a.operator K<int>()
15982 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15983 It is impossible to call a templated conversion-function-id with an
15984 explicit argument list, since the only allowed template parameter is
15985 the type to which it is converting.
15987 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15988 `template' keyword, in a construction like:
15990 T::template f<3>()
15992 In that case `f' is taken to be a template-name, even though there
15993 is no way of knowing for sure.
15995 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15996 name refers to a set of overloaded functions, at least one of which
15997 is a template, or an IDENTIFIER_NODE with the name of the template,
15998 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15999 names are looked up inside uninstantiated templates. */
16001 static tree
16002 cp_parser_template_name (cp_parser* parser,
16003 bool template_keyword_p,
16004 bool check_dependency_p,
16005 bool is_declaration,
16006 enum tag_types tag_type,
16007 bool *is_identifier)
16009 tree identifier;
16010 tree decl;
16011 cp_token *token = cp_lexer_peek_token (parser->lexer);
16013 /* If the next token is `operator', then we have either an
16014 operator-function-id or a conversion-function-id. */
16015 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16017 /* We don't know whether we're looking at an
16018 operator-function-id or a conversion-function-id. */
16019 cp_parser_parse_tentatively (parser);
16020 /* Try an operator-function-id. */
16021 identifier = cp_parser_operator_function_id (parser);
16022 /* If that didn't work, try a conversion-function-id. */
16023 if (!cp_parser_parse_definitely (parser))
16025 cp_parser_error (parser, "expected template-name");
16026 return error_mark_node;
16029 /* Look for the identifier. */
16030 else
16031 identifier = cp_parser_identifier (parser);
16033 /* If we didn't find an identifier, we don't have a template-id. */
16034 if (identifier == error_mark_node)
16035 return error_mark_node;
16037 /* If the name immediately followed the `template' keyword, then it
16038 is a template-name. However, if the next token is not `<', then
16039 we do not treat it as a template-name, since it is not being used
16040 as part of a template-id. This enables us to handle constructs
16041 like:
16043 template <typename T> struct S { S(); };
16044 template <typename T> S<T>::S();
16046 correctly. We would treat `S' as a template -- if it were `S<T>'
16047 -- but we do not if there is no `<'. */
16049 if (processing_template_decl
16050 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16052 /* In a declaration, in a dependent context, we pretend that the
16053 "template" keyword was present in order to improve error
16054 recovery. For example, given:
16056 template <typename T> void f(T::X<int>);
16058 we want to treat "X<int>" as a template-id. */
16059 if (is_declaration
16060 && !template_keyword_p
16061 && parser->scope && TYPE_P (parser->scope)
16062 && check_dependency_p
16063 && dependent_scope_p (parser->scope)
16064 /* Do not do this for dtors (or ctors), since they never
16065 need the template keyword before their name. */
16066 && !constructor_name_p (identifier, parser->scope))
16068 cp_token_position start = 0;
16070 /* Explain what went wrong. */
16071 error_at (token->location, "non-template %qD used as template",
16072 identifier);
16073 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16074 parser->scope, identifier);
16075 /* If parsing tentatively, find the location of the "<" token. */
16076 if (cp_parser_simulate_error (parser))
16077 start = cp_lexer_token_position (parser->lexer, true);
16078 /* Parse the template arguments so that we can issue error
16079 messages about them. */
16080 cp_lexer_consume_token (parser->lexer);
16081 cp_parser_enclosed_template_argument_list (parser);
16082 /* Skip tokens until we find a good place from which to
16083 continue parsing. */
16084 cp_parser_skip_to_closing_parenthesis (parser,
16085 /*recovering=*/true,
16086 /*or_comma=*/true,
16087 /*consume_paren=*/false);
16088 /* If parsing tentatively, permanently remove the
16089 template argument list. That will prevent duplicate
16090 error messages from being issued about the missing
16091 "template" keyword. */
16092 if (start)
16093 cp_lexer_purge_tokens_after (parser->lexer, start);
16094 if (is_identifier)
16095 *is_identifier = true;
16096 parser->context->object_type = NULL_TREE;
16097 return identifier;
16100 /* If the "template" keyword is present, then there is generally
16101 no point in doing name-lookup, so we just return IDENTIFIER.
16102 But, if the qualifying scope is non-dependent then we can
16103 (and must) do name-lookup normally. */
16104 if (template_keyword_p)
16106 tree scope = (parser->scope ? parser->scope
16107 : parser->context->object_type);
16108 if (scope && TYPE_P (scope)
16109 && (!CLASS_TYPE_P (scope)
16110 || (check_dependency_p && dependent_type_p (scope))))
16112 /* We're optimizing away the call to cp_parser_lookup_name, but
16113 we still need to do this. */
16114 parser->context->object_type = NULL_TREE;
16115 return identifier;
16120 /* Look up the name. */
16121 decl = cp_parser_lookup_name (parser, identifier,
16122 tag_type,
16123 /*is_template=*/true,
16124 /*is_namespace=*/false,
16125 check_dependency_p,
16126 /*ambiguous_decls=*/NULL,
16127 token->location);
16129 decl = strip_using_decl (decl);
16131 /* If DECL is a template, then the name was a template-name. */
16132 if (TREE_CODE (decl) == TEMPLATE_DECL)
16134 if (TREE_DEPRECATED (decl)
16135 && deprecated_state != DEPRECATED_SUPPRESS)
16136 warn_deprecated_use (decl, NULL_TREE);
16138 else
16140 /* The standard does not explicitly indicate whether a name that
16141 names a set of overloaded declarations, some of which are
16142 templates, is a template-name. However, such a name should
16143 be a template-name; otherwise, there is no way to form a
16144 template-id for the overloaded templates. */
16145 bool found = false;
16147 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16148 !found && iter; ++iter)
16149 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16150 found = true;
16152 if (!found)
16154 /* The name does not name a template. */
16155 cp_parser_error (parser, "expected template-name");
16156 return error_mark_node;
16160 /* If DECL is dependent, and refers to a function, then just return
16161 its name; we will look it up again during template instantiation. */
16162 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16164 tree scope = ovl_scope (decl);
16165 if (TYPE_P (scope) && dependent_type_p (scope))
16166 return identifier;
16169 return decl;
16172 /* Parse a template-argument-list.
16174 template-argument-list:
16175 template-argument ... [opt]
16176 template-argument-list , template-argument ... [opt]
16178 Returns a TREE_VEC containing the arguments. */
16180 static tree
16181 cp_parser_template_argument_list (cp_parser* parser)
16183 tree fixed_args[10];
16184 unsigned n_args = 0;
16185 unsigned alloced = 10;
16186 tree *arg_ary = fixed_args;
16187 tree vec;
16188 bool saved_in_template_argument_list_p;
16189 bool saved_ice_p;
16190 bool saved_non_ice_p;
16192 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16193 parser->in_template_argument_list_p = true;
16194 /* Even if the template-id appears in an integral
16195 constant-expression, the contents of the argument list do
16196 not. */
16197 saved_ice_p = parser->integral_constant_expression_p;
16198 parser->integral_constant_expression_p = false;
16199 saved_non_ice_p = parser->non_integral_constant_expression_p;
16200 parser->non_integral_constant_expression_p = false;
16202 /* Parse the arguments. */
16205 tree argument;
16207 if (n_args)
16208 /* Consume the comma. */
16209 cp_lexer_consume_token (parser->lexer);
16211 /* Parse the template-argument. */
16212 argument = cp_parser_template_argument (parser);
16214 /* If the next token is an ellipsis, we're expanding a template
16215 argument pack. */
16216 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16218 if (argument == error_mark_node)
16220 cp_token *token = cp_lexer_peek_token (parser->lexer);
16221 error_at (token->location,
16222 "expected parameter pack before %<...%>");
16224 /* Consume the `...' token. */
16225 cp_lexer_consume_token (parser->lexer);
16227 /* Make the argument into a TYPE_PACK_EXPANSION or
16228 EXPR_PACK_EXPANSION. */
16229 argument = make_pack_expansion (argument);
16232 if (n_args == alloced)
16234 alloced *= 2;
16236 if (arg_ary == fixed_args)
16238 arg_ary = XNEWVEC (tree, alloced);
16239 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16241 else
16242 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16244 arg_ary[n_args++] = argument;
16246 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16248 vec = make_tree_vec (n_args);
16250 while (n_args--)
16251 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16253 if (arg_ary != fixed_args)
16254 free (arg_ary);
16255 parser->non_integral_constant_expression_p = saved_non_ice_p;
16256 parser->integral_constant_expression_p = saved_ice_p;
16257 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16258 if (CHECKING_P)
16259 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16260 return vec;
16263 /* Parse a template-argument.
16265 template-argument:
16266 assignment-expression
16267 type-id
16268 id-expression
16270 The representation is that of an assignment-expression, type-id, or
16271 id-expression -- except that the qualified id-expression is
16272 evaluated, so that the value returned is either a DECL or an
16273 OVERLOAD.
16275 Although the standard says "assignment-expression", it forbids
16276 throw-expressions or assignments in the template argument.
16277 Therefore, we use "conditional-expression" instead. */
16279 static tree
16280 cp_parser_template_argument (cp_parser* parser)
16282 tree argument;
16283 bool template_p;
16284 bool address_p;
16285 bool maybe_type_id = false;
16286 cp_token *token = NULL, *argument_start_token = NULL;
16287 location_t loc = 0;
16288 cp_id_kind idk;
16290 /* There's really no way to know what we're looking at, so we just
16291 try each alternative in order.
16293 [temp.arg]
16295 In a template-argument, an ambiguity between a type-id and an
16296 expression is resolved to a type-id, regardless of the form of
16297 the corresponding template-parameter.
16299 Therefore, we try a type-id first. */
16300 cp_parser_parse_tentatively (parser);
16301 argument = cp_parser_template_type_arg (parser);
16302 /* If there was no error parsing the type-id but the next token is a
16303 '>>', our behavior depends on which dialect of C++ we're
16304 parsing. In C++98, we probably found a typo for '> >'. But there
16305 are type-id which are also valid expressions. For instance:
16307 struct X { int operator >> (int); };
16308 template <int V> struct Foo {};
16309 Foo<X () >> 5> r;
16311 Here 'X()' is a valid type-id of a function type, but the user just
16312 wanted to write the expression "X() >> 5". Thus, we remember that we
16313 found a valid type-id, but we still try to parse the argument as an
16314 expression to see what happens.
16316 In C++0x, the '>>' will be considered two separate '>'
16317 tokens. */
16318 if (!cp_parser_error_occurred (parser)
16319 && cxx_dialect == cxx98
16320 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16322 maybe_type_id = true;
16323 cp_parser_abort_tentative_parse (parser);
16325 else
16327 /* If the next token isn't a `,' or a `>', then this argument wasn't
16328 really finished. This means that the argument is not a valid
16329 type-id. */
16330 if (!cp_parser_next_token_ends_template_argument_p (parser))
16331 cp_parser_error (parser, "expected template-argument");
16332 /* If that worked, we're done. */
16333 if (cp_parser_parse_definitely (parser))
16334 return argument;
16336 /* We're still not sure what the argument will be. */
16337 cp_parser_parse_tentatively (parser);
16338 /* Try a template. */
16339 argument_start_token = cp_lexer_peek_token (parser->lexer);
16340 argument = cp_parser_id_expression (parser,
16341 /*template_keyword_p=*/false,
16342 /*check_dependency_p=*/true,
16343 &template_p,
16344 /*declarator_p=*/false,
16345 /*optional_p=*/false);
16346 /* If the next token isn't a `,' or a `>', then this argument wasn't
16347 really finished. */
16348 if (!cp_parser_next_token_ends_template_argument_p (parser))
16349 cp_parser_error (parser, "expected template-argument");
16350 if (!cp_parser_error_occurred (parser))
16352 /* Figure out what is being referred to. If the id-expression
16353 was for a class template specialization, then we will have a
16354 TYPE_DECL at this point. There is no need to do name lookup
16355 at this point in that case. */
16356 if (TREE_CODE (argument) != TYPE_DECL)
16357 argument = cp_parser_lookup_name (parser, argument,
16358 none_type,
16359 /*is_template=*/template_p,
16360 /*is_namespace=*/false,
16361 /*check_dependency=*/true,
16362 /*ambiguous_decls=*/NULL,
16363 argument_start_token->location);
16364 /* Handle a constrained-type-specifier for a non-type template
16365 parameter. */
16366 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16367 argument = decl;
16368 else if (TREE_CODE (argument) != TEMPLATE_DECL
16369 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16370 cp_parser_error (parser, "expected template-name");
16372 if (cp_parser_parse_definitely (parser))
16374 if (TREE_DEPRECATED (argument))
16375 warn_deprecated_use (argument, NULL_TREE);
16376 return argument;
16378 /* It must be a non-type argument. In C++17 any constant-expression is
16379 allowed. */
16380 if (cxx_dialect > cxx14)
16381 goto general_expr;
16383 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16385 -- an integral constant-expression of integral or enumeration
16386 type; or
16388 -- the name of a non-type template-parameter; or
16390 -- the name of an object or function with external linkage...
16392 -- the address of an object or function with external linkage...
16394 -- a pointer to member... */
16395 /* Look for a non-type template parameter. */
16396 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16398 cp_parser_parse_tentatively (parser);
16399 argument = cp_parser_primary_expression (parser,
16400 /*address_p=*/false,
16401 /*cast_p=*/false,
16402 /*template_arg_p=*/true,
16403 &idk);
16404 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16405 || !cp_parser_next_token_ends_template_argument_p (parser))
16406 cp_parser_simulate_error (parser);
16407 if (cp_parser_parse_definitely (parser))
16408 return argument;
16411 /* If the next token is "&", the argument must be the address of an
16412 object or function with external linkage. */
16413 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16414 if (address_p)
16416 loc = cp_lexer_peek_token (parser->lexer)->location;
16417 cp_lexer_consume_token (parser->lexer);
16419 /* See if we might have an id-expression. */
16420 token = cp_lexer_peek_token (parser->lexer);
16421 if (token->type == CPP_NAME
16422 || token->keyword == RID_OPERATOR
16423 || token->type == CPP_SCOPE
16424 || token->type == CPP_TEMPLATE_ID
16425 || token->type == CPP_NESTED_NAME_SPECIFIER)
16427 cp_parser_parse_tentatively (parser);
16428 argument = cp_parser_primary_expression (parser,
16429 address_p,
16430 /*cast_p=*/false,
16431 /*template_arg_p=*/true,
16432 &idk);
16433 if (cp_parser_error_occurred (parser)
16434 || !cp_parser_next_token_ends_template_argument_p (parser))
16435 cp_parser_abort_tentative_parse (parser);
16436 else
16438 tree probe;
16440 if (INDIRECT_REF_P (argument))
16442 /* Strip the dereference temporarily. */
16443 gcc_assert (REFERENCE_REF_P (argument));
16444 argument = TREE_OPERAND (argument, 0);
16447 /* If we're in a template, we represent a qualified-id referring
16448 to a static data member as a SCOPE_REF even if the scope isn't
16449 dependent so that we can check access control later. */
16450 probe = argument;
16451 if (TREE_CODE (probe) == SCOPE_REF)
16452 probe = TREE_OPERAND (probe, 1);
16453 if (VAR_P (probe))
16455 /* A variable without external linkage might still be a
16456 valid constant-expression, so no error is issued here
16457 if the external-linkage check fails. */
16458 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16459 cp_parser_simulate_error (parser);
16461 else if (is_overloaded_fn (argument))
16462 /* All overloaded functions are allowed; if the external
16463 linkage test does not pass, an error will be issued
16464 later. */
16466 else if (address_p
16467 && (TREE_CODE (argument) == OFFSET_REF
16468 || TREE_CODE (argument) == SCOPE_REF))
16469 /* A pointer-to-member. */
16471 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16473 else
16474 cp_parser_simulate_error (parser);
16476 if (cp_parser_parse_definitely (parser))
16478 if (address_p)
16479 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16480 tf_warning_or_error);
16481 else
16482 argument = convert_from_reference (argument);
16483 return argument;
16487 /* If the argument started with "&", there are no other valid
16488 alternatives at this point. */
16489 if (address_p)
16491 cp_parser_error (parser, "invalid non-type template argument");
16492 return error_mark_node;
16495 general_expr:
16496 /* If the argument wasn't successfully parsed as a type-id followed
16497 by '>>', the argument can only be a constant expression now.
16498 Otherwise, we try parsing the constant-expression tentatively,
16499 because the argument could really be a type-id. */
16500 if (maybe_type_id)
16501 cp_parser_parse_tentatively (parser);
16503 if (cxx_dialect <= cxx14)
16504 argument = cp_parser_constant_expression (parser);
16505 else
16507 /* With C++17 generalized non-type template arguments we need to handle
16508 lvalue constant expressions, too. */
16509 argument = cp_parser_assignment_expression (parser);
16510 require_potential_constant_expression (argument);
16513 if (!maybe_type_id)
16514 return argument;
16515 if (!cp_parser_next_token_ends_template_argument_p (parser))
16516 cp_parser_error (parser, "expected template-argument");
16517 if (cp_parser_parse_definitely (parser))
16518 return argument;
16519 /* We did our best to parse the argument as a non type-id, but that
16520 was the only alternative that matched (albeit with a '>' after
16521 it). We can assume it's just a typo from the user, and a
16522 diagnostic will then be issued. */
16523 return cp_parser_template_type_arg (parser);
16526 /* Parse an explicit-instantiation.
16528 explicit-instantiation:
16529 template declaration
16531 Although the standard says `declaration', what it really means is:
16533 explicit-instantiation:
16534 template decl-specifier-seq [opt] declarator [opt] ;
16536 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16537 supposed to be allowed. A defect report has been filed about this
16538 issue.
16540 GNU Extension:
16542 explicit-instantiation:
16543 storage-class-specifier template
16544 decl-specifier-seq [opt] declarator [opt] ;
16545 function-specifier template
16546 decl-specifier-seq [opt] declarator [opt] ; */
16548 static void
16549 cp_parser_explicit_instantiation (cp_parser* parser)
16551 int declares_class_or_enum;
16552 cp_decl_specifier_seq decl_specifiers;
16553 tree extension_specifier = NULL_TREE;
16555 timevar_push (TV_TEMPLATE_INST);
16557 /* Look for an (optional) storage-class-specifier or
16558 function-specifier. */
16559 if (cp_parser_allow_gnu_extensions_p (parser))
16561 extension_specifier
16562 = cp_parser_storage_class_specifier_opt (parser);
16563 if (!extension_specifier)
16564 extension_specifier
16565 = cp_parser_function_specifier_opt (parser,
16566 /*decl_specs=*/NULL);
16569 /* Look for the `template' keyword. */
16570 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16571 /* Let the front end know that we are processing an explicit
16572 instantiation. */
16573 begin_explicit_instantiation ();
16574 /* [temp.explicit] says that we are supposed to ignore access
16575 control while processing explicit instantiation directives. */
16576 push_deferring_access_checks (dk_no_check);
16577 /* Parse a decl-specifier-seq. */
16578 cp_parser_decl_specifier_seq (parser,
16579 CP_PARSER_FLAGS_OPTIONAL,
16580 &decl_specifiers,
16581 &declares_class_or_enum);
16582 /* If there was exactly one decl-specifier, and it declared a class,
16583 and there's no declarator, then we have an explicit type
16584 instantiation. */
16585 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16587 tree type;
16589 type = check_tag_decl (&decl_specifiers,
16590 /*explicit_type_instantiation_p=*/true);
16591 /* Turn access control back on for names used during
16592 template instantiation. */
16593 pop_deferring_access_checks ();
16594 if (type)
16595 do_type_instantiation (type, extension_specifier,
16596 /*complain=*/tf_error);
16598 else
16600 cp_declarator *declarator;
16601 tree decl;
16603 /* Parse the declarator. */
16604 declarator
16605 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16606 /*ctor_dtor_or_conv_p=*/NULL,
16607 /*parenthesized_p=*/NULL,
16608 /*member_p=*/false,
16609 /*friend_p=*/false);
16610 if (declares_class_or_enum & 2)
16611 cp_parser_check_for_definition_in_return_type (declarator,
16612 decl_specifiers.type,
16613 decl_specifiers.locations[ds_type_spec]);
16614 if (declarator != cp_error_declarator)
16616 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16617 permerror (decl_specifiers.locations[ds_inline],
16618 "explicit instantiation shall not use"
16619 " %<inline%> specifier");
16620 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16621 permerror (decl_specifiers.locations[ds_constexpr],
16622 "explicit instantiation shall not use"
16623 " %<constexpr%> specifier");
16625 decl = grokdeclarator (declarator, &decl_specifiers,
16626 NORMAL, 0, &decl_specifiers.attributes);
16627 /* Turn access control back on for names used during
16628 template instantiation. */
16629 pop_deferring_access_checks ();
16630 /* Do the explicit instantiation. */
16631 do_decl_instantiation (decl, extension_specifier);
16633 else
16635 pop_deferring_access_checks ();
16636 /* Skip the body of the explicit instantiation. */
16637 cp_parser_skip_to_end_of_statement (parser);
16640 /* We're done with the instantiation. */
16641 end_explicit_instantiation ();
16643 cp_parser_consume_semicolon_at_end_of_statement (parser);
16645 timevar_pop (TV_TEMPLATE_INST);
16648 /* Parse an explicit-specialization.
16650 explicit-specialization:
16651 template < > declaration
16653 Although the standard says `declaration', what it really means is:
16655 explicit-specialization:
16656 template <> decl-specifier [opt] init-declarator [opt] ;
16657 template <> function-definition
16658 template <> explicit-specialization
16659 template <> template-declaration */
16661 static void
16662 cp_parser_explicit_specialization (cp_parser* parser)
16664 bool need_lang_pop;
16665 cp_token *token = cp_lexer_peek_token (parser->lexer);
16667 /* Look for the `template' keyword. */
16668 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16669 /* Look for the `<'. */
16670 cp_parser_require (parser, CPP_LESS, RT_LESS);
16671 /* Look for the `>'. */
16672 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16673 /* We have processed another parameter list. */
16674 ++parser->num_template_parameter_lists;
16675 /* [temp]
16677 A template ... explicit specialization ... shall not have C
16678 linkage. */
16679 if (current_lang_name == lang_name_c)
16681 error_at (token->location, "template specialization with C linkage");
16682 maybe_show_extern_c_location ();
16683 /* Give it C++ linkage to avoid confusing other parts of the
16684 front end. */
16685 push_lang_context (lang_name_cplusplus);
16686 need_lang_pop = true;
16688 else
16689 need_lang_pop = false;
16690 /* Let the front end know that we are beginning a specialization. */
16691 if (!begin_specialization ())
16693 end_specialization ();
16694 return;
16697 /* If the next keyword is `template', we need to figure out whether
16698 or not we're looking a template-declaration. */
16699 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16701 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16702 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16703 cp_parser_template_declaration_after_export (parser,
16704 /*member_p=*/false);
16705 else
16706 cp_parser_explicit_specialization (parser);
16708 else
16709 /* Parse the dependent declaration. */
16710 cp_parser_single_declaration (parser,
16711 /*checks=*/NULL,
16712 /*member_p=*/false,
16713 /*explicit_specialization_p=*/true,
16714 /*friend_p=*/NULL);
16715 /* We're done with the specialization. */
16716 end_specialization ();
16717 /* For the erroneous case of a template with C linkage, we pushed an
16718 implicit C++ linkage scope; exit that scope now. */
16719 if (need_lang_pop)
16720 pop_lang_context ();
16721 /* We're done with this parameter list. */
16722 --parser->num_template_parameter_lists;
16725 /* Parse a type-specifier.
16727 type-specifier:
16728 simple-type-specifier
16729 class-specifier
16730 enum-specifier
16731 elaborated-type-specifier
16732 cv-qualifier
16734 GNU Extension:
16736 type-specifier:
16737 __complex__
16739 Returns a representation of the type-specifier. For a
16740 class-specifier, enum-specifier, or elaborated-type-specifier, a
16741 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16743 The parser flags FLAGS is used to control type-specifier parsing.
16745 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16746 in a decl-specifier-seq.
16748 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16749 class-specifier, enum-specifier, or elaborated-type-specifier, then
16750 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16751 if a type is declared; 2 if it is defined. Otherwise, it is set to
16752 zero.
16754 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16755 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16756 is set to FALSE. */
16758 static tree
16759 cp_parser_type_specifier (cp_parser* parser,
16760 cp_parser_flags flags,
16761 cp_decl_specifier_seq *decl_specs,
16762 bool is_declaration,
16763 int* declares_class_or_enum,
16764 bool* is_cv_qualifier)
16766 tree type_spec = NULL_TREE;
16767 cp_token *token;
16768 enum rid keyword;
16769 cp_decl_spec ds = ds_last;
16771 /* Assume this type-specifier does not declare a new type. */
16772 if (declares_class_or_enum)
16773 *declares_class_or_enum = 0;
16774 /* And that it does not specify a cv-qualifier. */
16775 if (is_cv_qualifier)
16776 *is_cv_qualifier = false;
16777 /* Peek at the next token. */
16778 token = cp_lexer_peek_token (parser->lexer);
16780 /* If we're looking at a keyword, we can use that to guide the
16781 production we choose. */
16782 keyword = token->keyword;
16783 switch (keyword)
16785 case RID_ENUM:
16786 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16787 goto elaborated_type_specifier;
16789 /* Look for the enum-specifier. */
16790 type_spec = cp_parser_enum_specifier (parser);
16791 /* If that worked, we're done. */
16792 if (type_spec)
16794 if (declares_class_or_enum)
16795 *declares_class_or_enum = 2;
16796 if (decl_specs)
16797 cp_parser_set_decl_spec_type (decl_specs,
16798 type_spec,
16799 token,
16800 /*type_definition_p=*/true);
16801 return type_spec;
16803 else
16804 goto elaborated_type_specifier;
16806 /* Any of these indicate either a class-specifier, or an
16807 elaborated-type-specifier. */
16808 case RID_CLASS:
16809 case RID_STRUCT:
16810 case RID_UNION:
16811 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16812 goto elaborated_type_specifier;
16814 /* Parse tentatively so that we can back up if we don't find a
16815 class-specifier. */
16816 cp_parser_parse_tentatively (parser);
16817 /* Look for the class-specifier. */
16818 type_spec = cp_parser_class_specifier (parser);
16819 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16820 /* If that worked, we're done. */
16821 if (cp_parser_parse_definitely (parser))
16823 if (declares_class_or_enum)
16824 *declares_class_or_enum = 2;
16825 if (decl_specs)
16826 cp_parser_set_decl_spec_type (decl_specs,
16827 type_spec,
16828 token,
16829 /*type_definition_p=*/true);
16830 return type_spec;
16833 /* Fall through. */
16834 elaborated_type_specifier:
16835 /* We're declaring (not defining) a class or enum. */
16836 if (declares_class_or_enum)
16837 *declares_class_or_enum = 1;
16839 /* Fall through. */
16840 case RID_TYPENAME:
16841 /* Look for an elaborated-type-specifier. */
16842 type_spec
16843 = (cp_parser_elaborated_type_specifier
16844 (parser,
16845 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16846 is_declaration));
16847 if (decl_specs)
16848 cp_parser_set_decl_spec_type (decl_specs,
16849 type_spec,
16850 token,
16851 /*type_definition_p=*/false);
16852 return type_spec;
16854 case RID_CONST:
16855 ds = ds_const;
16856 if (is_cv_qualifier)
16857 *is_cv_qualifier = true;
16858 break;
16860 case RID_VOLATILE:
16861 ds = ds_volatile;
16862 if (is_cv_qualifier)
16863 *is_cv_qualifier = true;
16864 break;
16866 case RID_RESTRICT:
16867 ds = ds_restrict;
16868 if (is_cv_qualifier)
16869 *is_cv_qualifier = true;
16870 break;
16872 case RID_COMPLEX:
16873 /* The `__complex__' keyword is a GNU extension. */
16874 ds = ds_complex;
16875 break;
16877 default:
16878 break;
16881 /* Handle simple keywords. */
16882 if (ds != ds_last)
16884 if (decl_specs)
16886 set_and_check_decl_spec_loc (decl_specs, ds, token);
16887 decl_specs->any_specifiers_p = true;
16889 return cp_lexer_consume_token (parser->lexer)->u.value;
16892 /* If we do not already have a type-specifier, assume we are looking
16893 at a simple-type-specifier. */
16894 type_spec = cp_parser_simple_type_specifier (parser,
16895 decl_specs,
16896 flags);
16898 /* If we didn't find a type-specifier, and a type-specifier was not
16899 optional in this context, issue an error message. */
16900 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16902 cp_parser_error (parser, "expected type specifier");
16903 return error_mark_node;
16906 return type_spec;
16909 /* Parse a simple-type-specifier.
16911 simple-type-specifier:
16912 :: [opt] nested-name-specifier [opt] type-name
16913 :: [opt] nested-name-specifier template template-id
16914 char
16915 wchar_t
16916 bool
16917 short
16919 long
16920 signed
16921 unsigned
16922 float
16923 double
16924 void
16926 C++11 Extension:
16928 simple-type-specifier:
16929 auto
16930 decltype ( expression )
16931 char16_t
16932 char32_t
16933 __underlying_type ( type-id )
16935 C++17 extension:
16937 nested-name-specifier(opt) template-name
16939 GNU Extension:
16941 simple-type-specifier:
16942 __int128
16943 __typeof__ unary-expression
16944 __typeof__ ( type-id )
16945 __typeof__ ( type-id ) { initializer-list , [opt] }
16947 Concepts Extension:
16949 simple-type-specifier:
16950 constrained-type-specifier
16952 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16953 appropriately updated. */
16955 static tree
16956 cp_parser_simple_type_specifier (cp_parser* parser,
16957 cp_decl_specifier_seq *decl_specs,
16958 cp_parser_flags flags)
16960 tree type = NULL_TREE;
16961 cp_token *token;
16962 int idx;
16964 /* Peek at the next token. */
16965 token = cp_lexer_peek_token (parser->lexer);
16967 /* If we're looking at a keyword, things are easy. */
16968 switch (token->keyword)
16970 case RID_CHAR:
16971 if (decl_specs)
16972 decl_specs->explicit_char_p = true;
16973 type = char_type_node;
16974 break;
16975 case RID_CHAR16:
16976 type = char16_type_node;
16977 break;
16978 case RID_CHAR32:
16979 type = char32_type_node;
16980 break;
16981 case RID_WCHAR:
16982 type = wchar_type_node;
16983 break;
16984 case RID_BOOL:
16985 type = boolean_type_node;
16986 break;
16987 case RID_SHORT:
16988 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16989 type = short_integer_type_node;
16990 break;
16991 case RID_INT:
16992 if (decl_specs)
16993 decl_specs->explicit_int_p = true;
16994 type = integer_type_node;
16995 break;
16996 case RID_INT_N_0:
16997 case RID_INT_N_1:
16998 case RID_INT_N_2:
16999 case RID_INT_N_3:
17000 idx = token->keyword - RID_INT_N_0;
17001 if (! int_n_enabled_p [idx])
17002 break;
17003 if (decl_specs)
17005 decl_specs->explicit_intN_p = true;
17006 decl_specs->int_n_idx = idx;
17008 type = int_n_trees [idx].signed_type;
17009 break;
17010 case RID_LONG:
17011 if (decl_specs)
17012 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17013 type = long_integer_type_node;
17014 break;
17015 case RID_SIGNED:
17016 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17017 type = integer_type_node;
17018 break;
17019 case RID_UNSIGNED:
17020 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17021 type = unsigned_type_node;
17022 break;
17023 case RID_FLOAT:
17024 type = float_type_node;
17025 break;
17026 case RID_DOUBLE:
17027 type = double_type_node;
17028 break;
17029 case RID_VOID:
17030 type = void_type_node;
17031 break;
17033 case RID_AUTO:
17034 maybe_warn_cpp0x (CPP0X_AUTO);
17035 if (parser->auto_is_implicit_function_template_parm_p)
17037 /* The 'auto' might be the placeholder return type for a function decl
17038 with trailing return type. */
17039 bool have_trailing_return_fn_decl = false;
17041 cp_parser_parse_tentatively (parser);
17042 cp_lexer_consume_token (parser->lexer);
17043 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17044 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17045 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17046 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17048 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17050 cp_lexer_consume_token (parser->lexer);
17051 cp_parser_skip_to_closing_parenthesis (parser,
17052 /*recovering*/false,
17053 /*or_comma*/false,
17054 /*consume_paren*/true);
17055 continue;
17058 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17060 have_trailing_return_fn_decl = true;
17061 break;
17064 cp_lexer_consume_token (parser->lexer);
17066 cp_parser_abort_tentative_parse (parser);
17068 if (have_trailing_return_fn_decl)
17070 type = make_auto ();
17071 break;
17074 if (cxx_dialect >= cxx14)
17076 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17077 type = TREE_TYPE (type);
17079 else
17080 type = error_mark_node;
17082 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17084 if (cxx_dialect < cxx14)
17085 error_at (token->location,
17086 "use of %<auto%> in lambda parameter declaration "
17087 "only available with "
17088 "-std=c++14 or -std=gnu++14");
17090 else if (cxx_dialect < cxx14)
17091 error_at (token->location,
17092 "use of %<auto%> in parameter declaration "
17093 "only available with "
17094 "-std=c++14 or -std=gnu++14");
17095 else if (!flag_concepts)
17096 pedwarn (token->location, OPT_Wpedantic,
17097 "ISO C++ forbids use of %<auto%> in parameter "
17098 "declaration");
17100 else
17101 type = make_auto ();
17102 break;
17104 case RID_DECLTYPE:
17105 /* Since DR 743, decltype can either be a simple-type-specifier by
17106 itself or begin a nested-name-specifier. Parsing it will replace
17107 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17108 handling below decide what to do. */
17109 cp_parser_decltype (parser);
17110 cp_lexer_set_token_position (parser->lexer, token);
17111 break;
17113 case RID_TYPEOF:
17114 /* Consume the `typeof' token. */
17115 cp_lexer_consume_token (parser->lexer);
17116 /* Parse the operand to `typeof'. */
17117 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17118 /* If it is not already a TYPE, take its type. */
17119 if (!TYPE_P (type))
17120 type = finish_typeof (type);
17122 if (decl_specs)
17123 cp_parser_set_decl_spec_type (decl_specs, type,
17124 token,
17125 /*type_definition_p=*/false);
17127 return type;
17129 case RID_UNDERLYING_TYPE:
17130 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17131 if (decl_specs)
17132 cp_parser_set_decl_spec_type (decl_specs, type,
17133 token,
17134 /*type_definition_p=*/false);
17136 return type;
17138 case RID_BASES:
17139 case RID_DIRECT_BASES:
17140 type = cp_parser_trait_expr (parser, token->keyword);
17141 if (decl_specs)
17142 cp_parser_set_decl_spec_type (decl_specs, type,
17143 token,
17144 /*type_definition_p=*/false);
17145 return type;
17146 default:
17147 break;
17150 /* If token is an already-parsed decltype not followed by ::,
17151 it's a simple-type-specifier. */
17152 if (token->type == CPP_DECLTYPE
17153 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17155 type = saved_checks_value (token->u.tree_check_value);
17156 if (decl_specs)
17158 cp_parser_set_decl_spec_type (decl_specs, type,
17159 token,
17160 /*type_definition_p=*/false);
17161 /* Remember that we are handling a decltype in order to
17162 implement the resolution of DR 1510 when the argument
17163 isn't instantiation dependent. */
17164 decl_specs->decltype_p = true;
17166 cp_lexer_consume_token (parser->lexer);
17167 return type;
17170 /* If the type-specifier was for a built-in type, we're done. */
17171 if (type)
17173 /* Record the type. */
17174 if (decl_specs
17175 && (token->keyword != RID_SIGNED
17176 && token->keyword != RID_UNSIGNED
17177 && token->keyword != RID_SHORT
17178 && token->keyword != RID_LONG))
17179 cp_parser_set_decl_spec_type (decl_specs,
17180 type,
17181 token,
17182 /*type_definition_p=*/false);
17183 if (decl_specs)
17184 decl_specs->any_specifiers_p = true;
17186 /* Consume the token. */
17187 cp_lexer_consume_token (parser->lexer);
17189 if (type == error_mark_node)
17190 return error_mark_node;
17192 /* There is no valid C++ program where a non-template type is
17193 followed by a "<". That usually indicates that the user thought
17194 that the type was a template. */
17195 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17196 token->location);
17198 return TYPE_NAME (type);
17201 /* The type-specifier must be a user-defined type. */
17202 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17204 bool qualified_p;
17205 bool global_p;
17207 /* Don't gobble tokens or issue error messages if this is an
17208 optional type-specifier. */
17209 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17210 cp_parser_parse_tentatively (parser);
17212 token = cp_lexer_peek_token (parser->lexer);
17214 /* Look for the optional `::' operator. */
17215 global_p
17216 = (cp_parser_global_scope_opt (parser,
17217 /*current_scope_valid_p=*/false)
17218 != NULL_TREE);
17219 /* Look for the nested-name specifier. */
17220 qualified_p
17221 = (cp_parser_nested_name_specifier_opt (parser,
17222 /*typename_keyword_p=*/false,
17223 /*check_dependency_p=*/true,
17224 /*type_p=*/false,
17225 /*is_declaration=*/false)
17226 != NULL_TREE);
17227 /* If we have seen a nested-name-specifier, and the next token
17228 is `template', then we are using the template-id production. */
17229 if (parser->scope
17230 && cp_parser_optional_template_keyword (parser))
17232 /* Look for the template-id. */
17233 type = cp_parser_template_id (parser,
17234 /*template_keyword_p=*/true,
17235 /*check_dependency_p=*/true,
17236 none_type,
17237 /*is_declaration=*/false);
17238 /* If the template-id did not name a type, we are out of
17239 luck. */
17240 if (TREE_CODE (type) != TYPE_DECL)
17242 cp_parser_error (parser, "expected template-id for type");
17243 type = NULL_TREE;
17246 /* Otherwise, look for a type-name. */
17247 else
17248 type = cp_parser_type_name (parser);
17249 /* Keep track of all name-lookups performed in class scopes. */
17250 if (type
17251 && !global_p
17252 && !qualified_p
17253 && TREE_CODE (type) == TYPE_DECL
17254 && identifier_p (DECL_NAME (type)))
17255 maybe_note_name_used_in_class (DECL_NAME (type), type);
17256 /* If it didn't work out, we don't have a TYPE. */
17257 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17258 && !cp_parser_parse_definitely (parser))
17259 type = NULL_TREE;
17260 if (!type && cxx_dialect >= cxx17)
17262 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17263 cp_parser_parse_tentatively (parser);
17265 cp_parser_global_scope_opt (parser,
17266 /*current_scope_valid_p=*/false);
17267 cp_parser_nested_name_specifier_opt (parser,
17268 /*typename_keyword_p=*/false,
17269 /*check_dependency_p=*/true,
17270 /*type_p=*/false,
17271 /*is_declaration=*/false);
17272 tree name = cp_parser_identifier (parser);
17273 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17274 && parser->scope != error_mark_node)
17276 tree tmpl = cp_parser_lookup_name (parser, name,
17277 none_type,
17278 /*is_template=*/false,
17279 /*is_namespace=*/false,
17280 /*check_dependency=*/true,
17281 /*ambiguous_decls=*/NULL,
17282 token->location);
17283 if (tmpl && tmpl != error_mark_node
17284 && (DECL_CLASS_TEMPLATE_P (tmpl)
17285 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17286 type = make_template_placeholder (tmpl);
17287 else
17289 type = error_mark_node;
17290 if (!cp_parser_simulate_error (parser))
17291 cp_parser_name_lookup_error (parser, name, tmpl,
17292 NLE_TYPE, token->location);
17295 else
17296 type = error_mark_node;
17298 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17299 && !cp_parser_parse_definitely (parser))
17300 type = NULL_TREE;
17302 if (type && decl_specs)
17303 cp_parser_set_decl_spec_type (decl_specs, type,
17304 token,
17305 /*type_definition_p=*/false);
17308 /* If we didn't get a type-name, issue an error message. */
17309 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17311 cp_parser_error (parser, "expected type-name");
17312 return error_mark_node;
17315 if (type && type != error_mark_node)
17317 /* See if TYPE is an Objective-C type, and if so, parse and
17318 accept any protocol references following it. Do this before
17319 the cp_parser_check_for_invalid_template_id() call, because
17320 Objective-C types can be followed by '<...>' which would
17321 enclose protocol names rather than template arguments, and so
17322 everything is fine. */
17323 if (c_dialect_objc () && !parser->scope
17324 && (objc_is_id (type) || objc_is_class_name (type)))
17326 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17327 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17329 /* Clobber the "unqualified" type previously entered into
17330 DECL_SPECS with the new, improved protocol-qualified version. */
17331 if (decl_specs)
17332 decl_specs->type = qual_type;
17334 return qual_type;
17337 /* There is no valid C++ program where a non-template type is
17338 followed by a "<". That usually indicates that the user
17339 thought that the type was a template. */
17340 cp_parser_check_for_invalid_template_id (parser, type,
17341 none_type,
17342 token->location);
17345 return type;
17348 /* Parse a type-name.
17350 type-name:
17351 class-name
17352 enum-name
17353 typedef-name
17354 simple-template-id [in c++0x]
17356 enum-name:
17357 identifier
17359 typedef-name:
17360 identifier
17362 Concepts:
17364 type-name:
17365 concept-name
17366 partial-concept-id
17368 concept-name:
17369 identifier
17371 Returns a TYPE_DECL for the type. */
17373 static tree
17374 cp_parser_type_name (cp_parser* parser)
17376 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17379 /* See above. */
17380 static tree
17381 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17383 tree type_decl;
17385 /* We can't know yet whether it is a class-name or not. */
17386 cp_parser_parse_tentatively (parser);
17387 /* Try a class-name. */
17388 type_decl = cp_parser_class_name (parser,
17389 typename_keyword_p,
17390 /*template_keyword_p=*/false,
17391 none_type,
17392 /*check_dependency_p=*/true,
17393 /*class_head_p=*/false,
17394 /*is_declaration=*/false);
17395 /* If it's not a class-name, keep looking. */
17396 if (!cp_parser_parse_definitely (parser))
17398 if (cxx_dialect < cxx11)
17399 /* It must be a typedef-name or an enum-name. */
17400 return cp_parser_nonclass_name (parser);
17402 cp_parser_parse_tentatively (parser);
17403 /* It is either a simple-template-id representing an
17404 instantiation of an alias template... */
17405 type_decl = cp_parser_template_id (parser,
17406 /*template_keyword_p=*/false,
17407 /*check_dependency_p=*/true,
17408 none_type,
17409 /*is_declaration=*/false);
17410 /* Note that this must be an instantiation of an alias template
17411 because [temp.names]/6 says:
17413 A template-id that names an alias template specialization
17414 is a type-name.
17416 Whereas [temp.names]/7 says:
17418 A simple-template-id that names a class template
17419 specialization is a class-name.
17421 With concepts, this could also be a partial-concept-id that
17422 declares a non-type template parameter. */
17423 if (type_decl != NULL_TREE
17424 && TREE_CODE (type_decl) == TYPE_DECL
17425 && TYPE_DECL_ALIAS_P (type_decl))
17426 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17427 else if (is_constrained_parameter (type_decl))
17428 /* Don't do anything. */ ;
17429 else
17430 cp_parser_simulate_error (parser);
17432 if (!cp_parser_parse_definitely (parser))
17433 /* ... Or a typedef-name or an enum-name. */
17434 return cp_parser_nonclass_name (parser);
17437 return type_decl;
17440 /* Check if DECL and ARGS can form a constrained-type-specifier.
17441 If ARGS is non-null, we try to form a concept check of the
17442 form DECL<?, ARGS> where ? is a wildcard that matches any
17443 kind of template argument. If ARGS is NULL, then we try to
17444 form a concept check of the form DECL<?>. */
17446 static tree
17447 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17448 tree decl, tree args)
17450 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17452 /* If we a constrained-type-specifier cannot be deduced. */
17453 if (parser->prevent_constrained_type_specifiers)
17454 return NULL_TREE;
17456 /* A constrained type specifier can only be found in an
17457 overload set or as a reference to a template declaration.
17459 FIXME: This might be masking a bug. It's possible that
17460 that the deduction below is causing template specializations
17461 to be formed with the wildcard as an argument. */
17462 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17463 return NULL_TREE;
17465 /* Try to build a call expression that evaluates the
17466 concept. This can fail if the overload set refers
17467 only to non-templates. */
17468 tree placeholder = build_nt (WILDCARD_DECL);
17469 tree check = build_concept_check (decl, placeholder, args);
17470 if (check == error_mark_node)
17471 return NULL_TREE;
17473 /* Deduce the checked constraint and the prototype parameter.
17475 FIXME: In certain cases, failure to deduce should be a
17476 diagnosable error. */
17477 tree conc;
17478 tree proto;
17479 if (!deduce_constrained_parameter (check, conc, proto))
17480 return NULL_TREE;
17482 /* In template parameter scope, this results in a constrained
17483 parameter. Return a descriptor of that parm. */
17484 if (processing_template_parmlist)
17485 return build_constrained_parameter (conc, proto, args);
17487 /* In a parameter-declaration-clause, constrained-type
17488 specifiers result in invented template parameters. */
17489 if (parser->auto_is_implicit_function_template_parm_p)
17491 tree x = build_constrained_parameter (conc, proto, args);
17492 return synthesize_implicit_template_parm (parser, x);
17494 else
17496 /* Otherwise, we're in a context where the constrained
17497 type name is deduced and the constraint applies
17498 after deduction. */
17499 return make_constrained_auto (conc, args);
17502 return NULL_TREE;
17505 /* If DECL refers to a concept, return a TYPE_DECL representing
17506 the result of using the constrained type specifier in the
17507 current context. DECL refers to a concept if
17509 - it is an overload set containing a function concept taking a single
17510 type argument, or
17512 - it is a variable concept taking a single type argument. */
17514 static tree
17515 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17517 if (flag_concepts
17518 && (TREE_CODE (decl) == OVERLOAD
17519 || BASELINK_P (decl)
17520 || variable_concept_p (decl)))
17521 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17522 else
17523 return NULL_TREE;
17526 /* Check if DECL and ARGS form a partial-concept-id. If so,
17527 assign ID to the resulting constrained placeholder.
17529 Returns true if the partial-concept-id designates a placeholder
17530 and false otherwise. Note that *id is set to NULL_TREE in
17531 this case. */
17533 static tree
17534 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17536 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17539 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17540 or a concept-name.
17542 enum-name:
17543 identifier
17545 typedef-name:
17546 identifier
17548 concept-name:
17549 identifier
17551 Returns a TYPE_DECL for the type. */
17553 static tree
17554 cp_parser_nonclass_name (cp_parser* parser)
17556 tree type_decl;
17557 tree identifier;
17559 cp_token *token = cp_lexer_peek_token (parser->lexer);
17560 identifier = cp_parser_identifier (parser);
17561 if (identifier == error_mark_node)
17562 return error_mark_node;
17564 /* Look up the type-name. */
17565 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17567 type_decl = strip_using_decl (type_decl);
17569 /* If we found an overload set, then it may refer to a concept-name. */
17570 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17571 type_decl = decl;
17573 if (TREE_CODE (type_decl) != TYPE_DECL
17574 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17576 /* See if this is an Objective-C type. */
17577 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17578 tree type = objc_get_protocol_qualified_type (identifier, protos);
17579 if (type)
17580 type_decl = TYPE_NAME (type);
17583 /* Issue an error if we did not find a type-name. */
17584 if (TREE_CODE (type_decl) != TYPE_DECL
17585 /* In Objective-C, we have the complication that class names are
17586 normally type names and start declarations (eg, the
17587 "NSObject" in "NSObject *object;"), but can be used in an
17588 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17589 is an expression. So, a classname followed by a dot is not a
17590 valid type-name. */
17591 || (objc_is_class_name (TREE_TYPE (type_decl))
17592 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17594 if (!cp_parser_simulate_error (parser))
17595 cp_parser_name_lookup_error (parser, identifier, type_decl,
17596 NLE_TYPE, token->location);
17597 return error_mark_node;
17599 /* Remember that the name was used in the definition of the
17600 current class so that we can check later to see if the
17601 meaning would have been different after the class was
17602 entirely defined. */
17603 else if (type_decl != error_mark_node
17604 && !parser->scope)
17605 maybe_note_name_used_in_class (identifier, type_decl);
17607 return type_decl;
17610 /* Parse an elaborated-type-specifier. Note that the grammar given
17611 here incorporates the resolution to DR68.
17613 elaborated-type-specifier:
17614 class-key :: [opt] nested-name-specifier [opt] identifier
17615 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17616 enum-key :: [opt] nested-name-specifier [opt] identifier
17617 typename :: [opt] nested-name-specifier identifier
17618 typename :: [opt] nested-name-specifier template [opt]
17619 template-id
17621 GNU extension:
17623 elaborated-type-specifier:
17624 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17625 class-key attributes :: [opt] nested-name-specifier [opt]
17626 template [opt] template-id
17627 enum attributes :: [opt] nested-name-specifier [opt] identifier
17629 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17630 declared `friend'. If IS_DECLARATION is TRUE, then this
17631 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17632 something is being declared.
17634 Returns the TYPE specified. */
17636 static tree
17637 cp_parser_elaborated_type_specifier (cp_parser* parser,
17638 bool is_friend,
17639 bool is_declaration)
17641 enum tag_types tag_type;
17642 tree identifier;
17643 tree type = NULL_TREE;
17644 tree attributes = NULL_TREE;
17645 tree globalscope;
17646 cp_token *token = NULL;
17648 /* See if we're looking at the `enum' keyword. */
17649 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17651 /* Consume the `enum' token. */
17652 cp_lexer_consume_token (parser->lexer);
17653 /* Remember that it's an enumeration type. */
17654 tag_type = enum_type;
17655 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17656 enums) is used here. */
17657 cp_token *token = cp_lexer_peek_token (parser->lexer);
17658 if (cp_parser_is_keyword (token, RID_CLASS)
17659 || cp_parser_is_keyword (token, RID_STRUCT))
17661 gcc_rich_location richloc (token->location);
17662 richloc.add_range (input_location, false);
17663 richloc.add_fixit_remove ();
17664 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17665 "a scoped enum must not use the %qD keyword",
17666 token->u.value);
17667 /* Consume the `struct' or `class' and parse it anyway. */
17668 cp_lexer_consume_token (parser->lexer);
17670 /* Parse the attributes. */
17671 attributes = cp_parser_attributes_opt (parser);
17673 /* Or, it might be `typename'. */
17674 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17675 RID_TYPENAME))
17677 /* Consume the `typename' token. */
17678 cp_lexer_consume_token (parser->lexer);
17679 /* Remember that it's a `typename' type. */
17680 tag_type = typename_type;
17682 /* Otherwise it must be a class-key. */
17683 else
17685 tag_type = cp_parser_class_key (parser);
17686 if (tag_type == none_type)
17687 return error_mark_node;
17688 /* Parse the attributes. */
17689 attributes = cp_parser_attributes_opt (parser);
17692 /* Look for the `::' operator. */
17693 globalscope = cp_parser_global_scope_opt (parser,
17694 /*current_scope_valid_p=*/false);
17695 /* Look for the nested-name-specifier. */
17696 tree nested_name_specifier;
17697 if (tag_type == typename_type && !globalscope)
17699 nested_name_specifier
17700 = cp_parser_nested_name_specifier (parser,
17701 /*typename_keyword_p=*/true,
17702 /*check_dependency_p=*/true,
17703 /*type_p=*/true,
17704 is_declaration);
17705 if (!nested_name_specifier)
17706 return error_mark_node;
17708 else
17709 /* Even though `typename' is not present, the proposed resolution
17710 to Core Issue 180 says that in `class A<T>::B', `B' should be
17711 considered a type-name, even if `A<T>' is dependent. */
17712 nested_name_specifier
17713 = cp_parser_nested_name_specifier_opt (parser,
17714 /*typename_keyword_p=*/true,
17715 /*check_dependency_p=*/true,
17716 /*type_p=*/true,
17717 is_declaration);
17718 /* For everything but enumeration types, consider a template-id.
17719 For an enumeration type, consider only a plain identifier. */
17720 if (tag_type != enum_type)
17722 bool template_p = false;
17723 tree decl;
17725 /* Allow the `template' keyword. */
17726 template_p = cp_parser_optional_template_keyword (parser);
17727 /* If we didn't see `template', we don't know if there's a
17728 template-id or not. */
17729 if (!template_p)
17730 cp_parser_parse_tentatively (parser);
17731 /* Parse the template-id. */
17732 token = cp_lexer_peek_token (parser->lexer);
17733 decl = cp_parser_template_id (parser, template_p,
17734 /*check_dependency_p=*/true,
17735 tag_type,
17736 is_declaration);
17737 /* If we didn't find a template-id, look for an ordinary
17738 identifier. */
17739 if (!template_p && !cp_parser_parse_definitely (parser))
17741 /* We can get here when cp_parser_template_id, called by
17742 cp_parser_class_name with tag_type == none_type, succeeds
17743 and caches a BASELINK. Then, when called again here,
17744 instead of failing and returning an error_mark_node
17745 returns it (see template/typename17.C in C++11).
17746 ??? Could we diagnose this earlier? */
17747 else if (tag_type == typename_type && BASELINK_P (decl))
17749 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17750 type = error_mark_node;
17752 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17753 in effect, then we must assume that, upon instantiation, the
17754 template will correspond to a class. */
17755 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17756 && tag_type == typename_type)
17757 type = make_typename_type (parser->scope, decl,
17758 typename_type,
17759 /*complain=*/tf_error);
17760 /* If the `typename' keyword is in effect and DECL is not a type
17761 decl, then type is non existent. */
17762 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17764 else if (TREE_CODE (decl) == TYPE_DECL)
17766 type = check_elaborated_type_specifier (tag_type, decl,
17767 /*allow_template_p=*/true);
17769 /* If the next token is a semicolon, this must be a specialization,
17770 instantiation, or friend declaration. Check the scope while we
17771 still know whether or not we had a nested-name-specifier. */
17772 if (type != error_mark_node
17773 && !nested_name_specifier && !is_friend
17774 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17775 check_unqualified_spec_or_inst (type, token->location);
17777 else if (decl == error_mark_node)
17778 type = error_mark_node;
17781 if (!type)
17783 token = cp_lexer_peek_token (parser->lexer);
17784 identifier = cp_parser_identifier (parser);
17786 if (identifier == error_mark_node)
17788 parser->scope = NULL_TREE;
17789 return error_mark_node;
17792 /* For a `typename', we needn't call xref_tag. */
17793 if (tag_type == typename_type
17794 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17795 return cp_parser_make_typename_type (parser, identifier,
17796 token->location);
17798 /* Template parameter lists apply only if we are not within a
17799 function parameter list. */
17800 bool template_parm_lists_apply
17801 = parser->num_template_parameter_lists;
17802 if (template_parm_lists_apply)
17803 for (cp_binding_level *s = current_binding_level;
17804 s && s->kind != sk_template_parms;
17805 s = s->level_chain)
17806 if (s->kind == sk_function_parms)
17807 template_parm_lists_apply = false;
17809 /* Look up a qualified name in the usual way. */
17810 if (parser->scope)
17812 tree decl;
17813 tree ambiguous_decls;
17815 decl = cp_parser_lookup_name (parser, identifier,
17816 tag_type,
17817 /*is_template=*/false,
17818 /*is_namespace=*/false,
17819 /*check_dependency=*/true,
17820 &ambiguous_decls,
17821 token->location);
17823 /* If the lookup was ambiguous, an error will already have been
17824 issued. */
17825 if (ambiguous_decls)
17826 return error_mark_node;
17828 /* If we are parsing friend declaration, DECL may be a
17829 TEMPLATE_DECL tree node here. However, we need to check
17830 whether this TEMPLATE_DECL results in valid code. Consider
17831 the following example:
17833 namespace N {
17834 template <class T> class C {};
17836 class X {
17837 template <class T> friend class N::C; // #1, valid code
17839 template <class T> class Y {
17840 friend class N::C; // #2, invalid code
17843 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17844 name lookup of `N::C'. We see that friend declaration must
17845 be template for the code to be valid. Note that
17846 processing_template_decl does not work here since it is
17847 always 1 for the above two cases. */
17849 decl = (cp_parser_maybe_treat_template_as_class
17850 (decl, /*tag_name_p=*/is_friend
17851 && template_parm_lists_apply));
17853 if (TREE_CODE (decl) != TYPE_DECL)
17855 cp_parser_diagnose_invalid_type_name (parser,
17856 identifier,
17857 token->location);
17858 return error_mark_node;
17861 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17863 bool allow_template = (template_parm_lists_apply
17864 || DECL_SELF_REFERENCE_P (decl));
17865 type = check_elaborated_type_specifier (tag_type, decl,
17866 allow_template);
17868 if (type == error_mark_node)
17869 return error_mark_node;
17872 /* Forward declarations of nested types, such as
17874 class C1::C2;
17875 class C1::C2::C3;
17877 are invalid unless all components preceding the final '::'
17878 are complete. If all enclosing types are complete, these
17879 declarations become merely pointless.
17881 Invalid forward declarations of nested types are errors
17882 caught elsewhere in parsing. Those that are pointless arrive
17883 here. */
17885 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17886 && !is_friend && !processing_explicit_instantiation)
17887 warning (0, "declaration %qD does not declare anything", decl);
17889 type = TREE_TYPE (decl);
17891 else
17893 /* An elaborated-type-specifier sometimes introduces a new type and
17894 sometimes names an existing type. Normally, the rule is that it
17895 introduces a new type only if there is not an existing type of
17896 the same name already in scope. For example, given:
17898 struct S {};
17899 void f() { struct S s; }
17901 the `struct S' in the body of `f' is the same `struct S' as in
17902 the global scope; the existing definition is used. However, if
17903 there were no global declaration, this would introduce a new
17904 local class named `S'.
17906 An exception to this rule applies to the following code:
17908 namespace N { struct S; }
17910 Here, the elaborated-type-specifier names a new type
17911 unconditionally; even if there is already an `S' in the
17912 containing scope this declaration names a new type.
17913 This exception only applies if the elaborated-type-specifier
17914 forms the complete declaration:
17916 [class.name]
17918 A declaration consisting solely of `class-key identifier ;' is
17919 either a redeclaration of the name in the current scope or a
17920 forward declaration of the identifier as a class name. It
17921 introduces the name into the current scope.
17923 We are in this situation precisely when the next token is a `;'.
17925 An exception to the exception is that a `friend' declaration does
17926 *not* name a new type; i.e., given:
17928 struct S { friend struct T; };
17930 `T' is not a new type in the scope of `S'.
17932 Also, `new struct S' or `sizeof (struct S)' never results in the
17933 definition of a new type; a new type can only be declared in a
17934 declaration context. */
17936 tag_scope ts;
17937 bool template_p;
17939 if (is_friend)
17940 /* Friends have special name lookup rules. */
17941 ts = ts_within_enclosing_non_class;
17942 else if (is_declaration
17943 && cp_lexer_next_token_is (parser->lexer,
17944 CPP_SEMICOLON))
17945 /* This is a `class-key identifier ;' */
17946 ts = ts_current;
17947 else
17948 ts = ts_global;
17950 template_p =
17951 (template_parm_lists_apply
17952 && (cp_parser_next_token_starts_class_definition_p (parser)
17953 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17954 /* An unqualified name was used to reference this type, so
17955 there were no qualifying templates. */
17956 if (template_parm_lists_apply
17957 && !cp_parser_check_template_parameters (parser,
17958 /*num_templates=*/0,
17959 token->location,
17960 /*declarator=*/NULL))
17961 return error_mark_node;
17962 type = xref_tag (tag_type, identifier, ts, template_p);
17966 if (type == error_mark_node)
17967 return error_mark_node;
17969 /* Allow attributes on forward declarations of classes. */
17970 if (attributes)
17972 if (TREE_CODE (type) == TYPENAME_TYPE)
17973 warning (OPT_Wattributes,
17974 "attributes ignored on uninstantiated type");
17975 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17976 && ! processing_explicit_instantiation)
17977 warning (OPT_Wattributes,
17978 "attributes ignored on template instantiation");
17979 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17980 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17981 else
17982 warning (OPT_Wattributes,
17983 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17986 if (tag_type != enum_type)
17988 /* Indicate whether this class was declared as a `class' or as a
17989 `struct'. */
17990 if (CLASS_TYPE_P (type))
17991 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17992 cp_parser_check_class_key (tag_type, type);
17995 /* A "<" cannot follow an elaborated type specifier. If that
17996 happens, the user was probably trying to form a template-id. */
17997 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17998 token->location);
18000 return type;
18003 /* Parse an enum-specifier.
18005 enum-specifier:
18006 enum-head { enumerator-list [opt] }
18007 enum-head { enumerator-list , } [C++0x]
18009 enum-head:
18010 enum-key identifier [opt] enum-base [opt]
18011 enum-key nested-name-specifier identifier enum-base [opt]
18013 enum-key:
18014 enum
18015 enum class [C++0x]
18016 enum struct [C++0x]
18018 enum-base: [C++0x]
18019 : type-specifier-seq
18021 opaque-enum-specifier:
18022 enum-key identifier enum-base [opt] ;
18024 GNU Extensions:
18025 enum-key attributes[opt] identifier [opt] enum-base [opt]
18026 { enumerator-list [opt] }attributes[opt]
18027 enum-key attributes[opt] identifier [opt] enum-base [opt]
18028 { enumerator-list, }attributes[opt] [C++0x]
18030 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18031 if the token stream isn't an enum-specifier after all. */
18033 static tree
18034 cp_parser_enum_specifier (cp_parser* parser)
18036 tree identifier;
18037 tree type = NULL_TREE;
18038 tree prev_scope;
18039 tree nested_name_specifier = NULL_TREE;
18040 tree attributes;
18041 bool scoped_enum_p = false;
18042 bool has_underlying_type = false;
18043 bool nested_being_defined = false;
18044 bool new_value_list = false;
18045 bool is_new_type = false;
18046 bool is_unnamed = false;
18047 tree underlying_type = NULL_TREE;
18048 cp_token *type_start_token = NULL;
18049 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18051 parser->colon_corrects_to_scope_p = false;
18053 /* Parse tentatively so that we can back up if we don't find a
18054 enum-specifier. */
18055 cp_parser_parse_tentatively (parser);
18057 /* Caller guarantees that the current token is 'enum', an identifier
18058 possibly follows, and the token after that is an opening brace.
18059 If we don't have an identifier, fabricate an anonymous name for
18060 the enumeration being defined. */
18061 cp_lexer_consume_token (parser->lexer);
18063 /* Parse the "class" or "struct", which indicates a scoped
18064 enumeration type in C++0x. */
18065 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18066 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18068 if (cxx_dialect < cxx11)
18069 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18071 /* Consume the `struct' or `class' token. */
18072 cp_lexer_consume_token (parser->lexer);
18074 scoped_enum_p = true;
18077 attributes = cp_parser_attributes_opt (parser);
18079 /* Clear the qualification. */
18080 parser->scope = NULL_TREE;
18081 parser->qualifying_scope = NULL_TREE;
18082 parser->object_scope = NULL_TREE;
18084 /* Figure out in what scope the declaration is being placed. */
18085 prev_scope = current_scope ();
18087 type_start_token = cp_lexer_peek_token (parser->lexer);
18089 push_deferring_access_checks (dk_no_check);
18090 nested_name_specifier
18091 = cp_parser_nested_name_specifier_opt (parser,
18092 /*typename_keyword_p=*/true,
18093 /*check_dependency_p=*/false,
18094 /*type_p=*/false,
18095 /*is_declaration=*/false);
18097 if (nested_name_specifier)
18099 tree name;
18101 identifier = cp_parser_identifier (parser);
18102 name = cp_parser_lookup_name (parser, identifier,
18103 enum_type,
18104 /*is_template=*/false,
18105 /*is_namespace=*/false,
18106 /*check_dependency=*/true,
18107 /*ambiguous_decls=*/NULL,
18108 input_location);
18109 if (name && name != error_mark_node)
18111 type = TREE_TYPE (name);
18112 if (TREE_CODE (type) == TYPENAME_TYPE)
18114 /* Are template enums allowed in ISO? */
18115 if (template_parm_scope_p ())
18116 pedwarn (type_start_token->location, OPT_Wpedantic,
18117 "%qD is an enumeration template", name);
18118 /* ignore a typename reference, for it will be solved by name
18119 in start_enum. */
18120 type = NULL_TREE;
18123 else if (nested_name_specifier == error_mark_node)
18124 /* We already issued an error. */;
18125 else
18127 error_at (type_start_token->location,
18128 "%qD does not name an enumeration in %qT",
18129 identifier, nested_name_specifier);
18130 nested_name_specifier = error_mark_node;
18133 else
18135 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18136 identifier = cp_parser_identifier (parser);
18137 else
18139 identifier = make_anon_name ();
18140 is_unnamed = true;
18141 if (scoped_enum_p)
18142 error_at (type_start_token->location,
18143 "unnamed scoped enum is not allowed");
18146 pop_deferring_access_checks ();
18148 /* Check for the `:' that denotes a specified underlying type in C++0x.
18149 Note that a ':' could also indicate a bitfield width, however. */
18150 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18152 cp_decl_specifier_seq type_specifiers;
18154 /* Consume the `:'. */
18155 cp_lexer_consume_token (parser->lexer);
18157 /* Parse the type-specifier-seq. */
18158 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18159 /*is_trailing_return=*/false,
18160 &type_specifiers);
18162 /* At this point this is surely not elaborated type specifier. */
18163 if (!cp_parser_parse_definitely (parser))
18164 return NULL_TREE;
18166 if (cxx_dialect < cxx11)
18167 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18169 has_underlying_type = true;
18171 /* If that didn't work, stop. */
18172 if (type_specifiers.type != error_mark_node)
18174 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18175 /*initialized=*/0, NULL);
18176 if (underlying_type == error_mark_node
18177 || check_for_bare_parameter_packs (underlying_type))
18178 underlying_type = NULL_TREE;
18182 /* Look for the `{' but don't consume it yet. */
18183 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18185 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18187 cp_parser_error (parser, "expected %<{%>");
18188 if (has_underlying_type)
18190 type = NULL_TREE;
18191 goto out;
18194 /* An opaque-enum-specifier must have a ';' here. */
18195 if ((scoped_enum_p || underlying_type)
18196 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18198 cp_parser_error (parser, "expected %<;%> or %<{%>");
18199 if (has_underlying_type)
18201 type = NULL_TREE;
18202 goto out;
18207 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18208 return NULL_TREE;
18210 if (nested_name_specifier)
18212 if (CLASS_TYPE_P (nested_name_specifier))
18214 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18215 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18216 push_scope (nested_name_specifier);
18218 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18220 push_nested_namespace (nested_name_specifier);
18224 /* Issue an error message if type-definitions are forbidden here. */
18225 if (!cp_parser_check_type_definition (parser))
18226 type = error_mark_node;
18227 else
18228 /* Create the new type. We do this before consuming the opening
18229 brace so the enum will be recorded as being on the line of its
18230 tag (or the 'enum' keyword, if there is no tag). */
18231 type = start_enum (identifier, type, underlying_type,
18232 attributes, scoped_enum_p, &is_new_type);
18234 /* If the next token is not '{' it is an opaque-enum-specifier or an
18235 elaborated-type-specifier. */
18236 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18238 timevar_push (TV_PARSE_ENUM);
18239 if (nested_name_specifier
18240 && nested_name_specifier != error_mark_node)
18242 /* The following catches invalid code such as:
18243 enum class S<int>::E { A, B, C }; */
18244 if (!processing_specialization
18245 && CLASS_TYPE_P (nested_name_specifier)
18246 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18247 error_at (type_start_token->location, "cannot add an enumerator "
18248 "list to a template instantiation");
18250 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18252 error_at (type_start_token->location,
18253 "%<%T::%E%> has not been declared",
18254 TYPE_CONTEXT (nested_name_specifier),
18255 nested_name_specifier);
18256 type = error_mark_node;
18258 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18259 && !CLASS_TYPE_P (nested_name_specifier))
18261 error_at (type_start_token->location, "nested name specifier "
18262 "%qT for enum declaration does not name a class "
18263 "or namespace", nested_name_specifier);
18264 type = error_mark_node;
18266 /* If that scope does not contain the scope in which the
18267 class was originally declared, the program is invalid. */
18268 else if (prev_scope && !is_ancestor (prev_scope,
18269 nested_name_specifier))
18271 if (at_namespace_scope_p ())
18272 error_at (type_start_token->location,
18273 "declaration of %qD in namespace %qD which does not "
18274 "enclose %qD",
18275 type, prev_scope, nested_name_specifier);
18276 else
18277 error_at (type_start_token->location,
18278 "declaration of %qD in %qD which does not "
18279 "enclose %qD",
18280 type, prev_scope, nested_name_specifier);
18281 type = error_mark_node;
18283 /* If that scope is the scope where the declaration is being placed
18284 the program is invalid. */
18285 else if (CLASS_TYPE_P (nested_name_specifier)
18286 && CLASS_TYPE_P (prev_scope)
18287 && same_type_p (nested_name_specifier, prev_scope))
18289 permerror (type_start_token->location,
18290 "extra qualification not allowed");
18291 nested_name_specifier = NULL_TREE;
18295 if (scoped_enum_p)
18296 begin_scope (sk_scoped_enum, type);
18298 /* Consume the opening brace. */
18299 matching_braces braces;
18300 braces.consume_open (parser);
18302 if (type == error_mark_node)
18303 ; /* Nothing to add */
18304 else if (OPAQUE_ENUM_P (type)
18305 || (cxx_dialect > cxx98 && processing_specialization))
18307 new_value_list = true;
18308 SET_OPAQUE_ENUM_P (type, false);
18309 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18311 else
18313 error_at (type_start_token->location,
18314 "multiple definition of %q#T", type);
18315 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18316 "previous definition here");
18317 type = error_mark_node;
18320 if (type == error_mark_node)
18321 cp_parser_skip_to_end_of_block_or_statement (parser);
18322 /* If the next token is not '}', then there are some enumerators. */
18323 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18325 if (is_unnamed && !scoped_enum_p)
18326 pedwarn (type_start_token->location, OPT_Wpedantic,
18327 "ISO C++ forbids empty unnamed enum");
18329 else
18330 cp_parser_enumerator_list (parser, type);
18332 /* Consume the final '}'. */
18333 braces.require_close (parser);
18335 if (scoped_enum_p)
18336 finish_scope ();
18337 timevar_pop (TV_PARSE_ENUM);
18339 else
18341 /* If a ';' follows, then it is an opaque-enum-specifier
18342 and additional restrictions apply. */
18343 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18345 if (is_unnamed)
18346 error_at (type_start_token->location,
18347 "opaque-enum-specifier without name");
18348 else if (nested_name_specifier)
18349 error_at (type_start_token->location,
18350 "opaque-enum-specifier must use a simple identifier");
18354 /* Look for trailing attributes to apply to this enumeration, and
18355 apply them if appropriate. */
18356 if (cp_parser_allow_gnu_extensions_p (parser))
18358 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18359 cplus_decl_attributes (&type,
18360 trailing_attr,
18361 (int) ATTR_FLAG_TYPE_IN_PLACE);
18364 /* Finish up the enumeration. */
18365 if (type != error_mark_node)
18367 if (new_value_list)
18368 finish_enum_value_list (type);
18369 if (is_new_type)
18370 finish_enum (type);
18373 if (nested_name_specifier)
18375 if (CLASS_TYPE_P (nested_name_specifier))
18377 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18378 pop_scope (nested_name_specifier);
18380 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18382 pop_nested_namespace (nested_name_specifier);
18385 out:
18386 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18387 return type;
18390 /* Parse an enumerator-list. The enumerators all have the indicated
18391 TYPE.
18393 enumerator-list:
18394 enumerator-definition
18395 enumerator-list , enumerator-definition */
18397 static void
18398 cp_parser_enumerator_list (cp_parser* parser, tree type)
18400 while (true)
18402 /* Parse an enumerator-definition. */
18403 cp_parser_enumerator_definition (parser, type);
18405 /* If the next token is not a ',', we've reached the end of
18406 the list. */
18407 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18408 break;
18409 /* Otherwise, consume the `,' and keep going. */
18410 cp_lexer_consume_token (parser->lexer);
18411 /* If the next token is a `}', there is a trailing comma. */
18412 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18414 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18415 pedwarn (input_location, OPT_Wpedantic,
18416 "comma at end of enumerator list");
18417 break;
18422 /* Parse an enumerator-definition. The enumerator has the indicated
18423 TYPE.
18425 enumerator-definition:
18426 enumerator
18427 enumerator = constant-expression
18429 enumerator:
18430 identifier
18432 GNU Extensions:
18434 enumerator-definition:
18435 enumerator attributes [opt]
18436 enumerator attributes [opt] = constant-expression */
18438 static void
18439 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18441 tree identifier;
18442 tree value;
18443 location_t loc;
18445 /* Save the input location because we are interested in the location
18446 of the identifier and not the location of the explicit value. */
18447 loc = cp_lexer_peek_token (parser->lexer)->location;
18449 /* Look for the identifier. */
18450 identifier = cp_parser_identifier (parser);
18451 if (identifier == error_mark_node)
18452 return;
18454 /* Parse any specified attributes. */
18455 tree attrs = cp_parser_attributes_opt (parser);
18457 /* If the next token is an '=', then there is an explicit value. */
18458 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18460 /* Consume the `=' token. */
18461 cp_lexer_consume_token (parser->lexer);
18462 /* Parse the value. */
18463 value = cp_parser_constant_expression (parser);
18465 else
18466 value = NULL_TREE;
18468 /* If we are processing a template, make sure the initializer of the
18469 enumerator doesn't contain any bare template parameter pack. */
18470 if (check_for_bare_parameter_packs (value))
18471 value = error_mark_node;
18473 /* Create the enumerator. */
18474 build_enumerator (identifier, value, type, attrs, loc);
18477 /* Parse a namespace-name.
18479 namespace-name:
18480 original-namespace-name
18481 namespace-alias
18483 Returns the NAMESPACE_DECL for the namespace. */
18485 static tree
18486 cp_parser_namespace_name (cp_parser* parser)
18488 tree identifier;
18489 tree namespace_decl;
18491 cp_token *token = cp_lexer_peek_token (parser->lexer);
18493 /* Get the name of the namespace. */
18494 identifier = cp_parser_identifier (parser);
18495 if (identifier == error_mark_node)
18496 return error_mark_node;
18498 /* Look up the identifier in the currently active scope. Look only
18499 for namespaces, due to:
18501 [basic.lookup.udir]
18503 When looking up a namespace-name in a using-directive or alias
18504 definition, only namespace names are considered.
18506 And:
18508 [basic.lookup.qual]
18510 During the lookup of a name preceding the :: scope resolution
18511 operator, object, function, and enumerator names are ignored.
18513 (Note that cp_parser_qualifying_entity only calls this
18514 function if the token after the name is the scope resolution
18515 operator.) */
18516 namespace_decl = cp_parser_lookup_name (parser, identifier,
18517 none_type,
18518 /*is_template=*/false,
18519 /*is_namespace=*/true,
18520 /*check_dependency=*/true,
18521 /*ambiguous_decls=*/NULL,
18522 token->location);
18523 /* If it's not a namespace, issue an error. */
18524 if (namespace_decl == error_mark_node
18525 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18527 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18528 error_at (token->location, "%qD is not a namespace-name", identifier);
18529 cp_parser_error (parser, "expected namespace-name");
18530 namespace_decl = error_mark_node;
18533 return namespace_decl;
18536 /* Parse a namespace-definition.
18538 namespace-definition:
18539 named-namespace-definition
18540 unnamed-namespace-definition
18542 named-namespace-definition:
18543 original-namespace-definition
18544 extension-namespace-definition
18546 original-namespace-definition:
18547 namespace identifier { namespace-body }
18549 extension-namespace-definition:
18550 namespace original-namespace-name { namespace-body }
18552 unnamed-namespace-definition:
18553 namespace { namespace-body } */
18555 static void
18556 cp_parser_namespace_definition (cp_parser* parser)
18558 tree identifier;
18559 int nested_definition_count = 0;
18561 cp_ensure_no_omp_declare_simd (parser);
18562 cp_ensure_no_oacc_routine (parser);
18564 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18566 if (is_inline)
18568 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18569 cp_lexer_consume_token (parser->lexer);
18572 /* Look for the `namespace' keyword. */
18573 cp_token* token
18574 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18576 /* Parse any specified attributes before the identifier. */
18577 tree attribs = cp_parser_attributes_opt (parser);
18579 for (;;)
18581 identifier = NULL_TREE;
18583 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18585 identifier = cp_parser_identifier (parser);
18587 /* Parse any attributes specified after the identifier. */
18588 attribs = chainon (attribs, cp_parser_attributes_opt (parser));
18591 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18592 break;
18594 if (!nested_definition_count && cxx_dialect < cxx17)
18595 pedwarn (input_location, OPT_Wpedantic,
18596 "nested namespace definitions only available with "
18597 "-std=c++17 or -std=gnu++17");
18599 /* Nested namespace names can create new namespaces (unlike
18600 other qualified-ids). */
18601 if (int count = identifier ? push_namespace (identifier) : 0)
18602 nested_definition_count += count;
18603 else
18604 cp_parser_error (parser, "nested namespace name required");
18605 cp_lexer_consume_token (parser->lexer);
18608 if (nested_definition_count && !identifier)
18609 cp_parser_error (parser, "namespace name required");
18611 if (nested_definition_count && attribs)
18612 error_at (token->location,
18613 "a nested namespace definition cannot have attributes");
18614 if (nested_definition_count && is_inline)
18615 error_at (token->location,
18616 "a nested namespace definition cannot be inline");
18618 /* Start the namespace. */
18619 nested_definition_count += push_namespace (identifier, is_inline);
18621 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18623 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18625 /* Look for the `{' to validate starting the namespace. */
18626 matching_braces braces;
18627 if (braces.require_open (parser))
18629 /* Parse the body of the namespace. */
18630 cp_parser_namespace_body (parser);
18632 /* Look for the final `}'. */
18633 braces.require_close (parser);
18636 if (has_visibility)
18637 pop_visibility (1);
18639 /* Pop the nested namespace definitions. */
18640 while (nested_definition_count--)
18641 pop_namespace ();
18644 /* Parse a namespace-body.
18646 namespace-body:
18647 declaration-seq [opt] */
18649 static void
18650 cp_parser_namespace_body (cp_parser* parser)
18652 cp_parser_declaration_seq_opt (parser);
18655 /* Parse a namespace-alias-definition.
18657 namespace-alias-definition:
18658 namespace identifier = qualified-namespace-specifier ; */
18660 static void
18661 cp_parser_namespace_alias_definition (cp_parser* parser)
18663 tree identifier;
18664 tree namespace_specifier;
18666 cp_token *token = cp_lexer_peek_token (parser->lexer);
18668 /* Look for the `namespace' keyword. */
18669 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18670 /* Look for the identifier. */
18671 identifier = cp_parser_identifier (parser);
18672 if (identifier == error_mark_node)
18673 return;
18674 /* Look for the `=' token. */
18675 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18676 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18678 error_at (token->location, "%<namespace%> definition is not allowed here");
18679 /* Skip the definition. */
18680 cp_lexer_consume_token (parser->lexer);
18681 if (cp_parser_skip_to_closing_brace (parser))
18682 cp_lexer_consume_token (parser->lexer);
18683 return;
18685 cp_parser_require (parser, CPP_EQ, RT_EQ);
18686 /* Look for the qualified-namespace-specifier. */
18687 namespace_specifier
18688 = cp_parser_qualified_namespace_specifier (parser);
18689 /* Look for the `;' token. */
18690 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18692 /* Register the alias in the symbol table. */
18693 do_namespace_alias (identifier, namespace_specifier);
18696 /* Parse a qualified-namespace-specifier.
18698 qualified-namespace-specifier:
18699 :: [opt] nested-name-specifier [opt] namespace-name
18701 Returns a NAMESPACE_DECL corresponding to the specified
18702 namespace. */
18704 static tree
18705 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18707 /* Look for the optional `::'. */
18708 cp_parser_global_scope_opt (parser,
18709 /*current_scope_valid_p=*/false);
18711 /* Look for the optional nested-name-specifier. */
18712 cp_parser_nested_name_specifier_opt (parser,
18713 /*typename_keyword_p=*/false,
18714 /*check_dependency_p=*/true,
18715 /*type_p=*/false,
18716 /*is_declaration=*/true);
18718 return cp_parser_namespace_name (parser);
18721 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18722 access declaration.
18724 using-declaration:
18725 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18726 using :: unqualified-id ;
18728 access-declaration:
18729 qualified-id ;
18733 static bool
18734 cp_parser_using_declaration (cp_parser* parser,
18735 bool access_declaration_p)
18737 cp_token *token;
18738 bool typename_p = false;
18739 bool global_scope_p;
18740 tree decl;
18741 tree identifier;
18742 tree qscope;
18743 int oldcount = errorcount;
18744 cp_token *diag_token = NULL;
18746 if (access_declaration_p)
18748 diag_token = cp_lexer_peek_token (parser->lexer);
18749 cp_parser_parse_tentatively (parser);
18751 else
18753 /* Look for the `using' keyword. */
18754 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18756 again:
18757 /* Peek at the next token. */
18758 token = cp_lexer_peek_token (parser->lexer);
18759 /* See if it's `typename'. */
18760 if (token->keyword == RID_TYPENAME)
18762 /* Remember that we've seen it. */
18763 typename_p = true;
18764 /* Consume the `typename' token. */
18765 cp_lexer_consume_token (parser->lexer);
18769 /* Look for the optional global scope qualification. */
18770 global_scope_p
18771 = (cp_parser_global_scope_opt (parser,
18772 /*current_scope_valid_p=*/false)
18773 != NULL_TREE);
18775 /* If we saw `typename', or didn't see `::', then there must be a
18776 nested-name-specifier present. */
18777 if (typename_p || !global_scope_p)
18779 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18780 /*check_dependency_p=*/true,
18781 /*type_p=*/false,
18782 /*is_declaration=*/true);
18783 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18785 cp_parser_skip_to_end_of_block_or_statement (parser);
18786 return false;
18789 /* Otherwise, we could be in either of the two productions. In that
18790 case, treat the nested-name-specifier as optional. */
18791 else
18792 qscope = cp_parser_nested_name_specifier_opt (parser,
18793 /*typename_keyword_p=*/false,
18794 /*check_dependency_p=*/true,
18795 /*type_p=*/false,
18796 /*is_declaration=*/true);
18797 if (!qscope)
18798 qscope = global_namespace;
18799 else if (UNSCOPED_ENUM_P (qscope))
18800 qscope = CP_TYPE_CONTEXT (qscope);
18802 if (access_declaration_p && cp_parser_error_occurred (parser))
18803 /* Something has already gone wrong; there's no need to parse
18804 further. Since an error has occurred, the return value of
18805 cp_parser_parse_definitely will be false, as required. */
18806 return cp_parser_parse_definitely (parser);
18808 token = cp_lexer_peek_token (parser->lexer);
18809 /* Parse the unqualified-id. */
18810 identifier = cp_parser_unqualified_id (parser,
18811 /*template_keyword_p=*/false,
18812 /*check_dependency_p=*/true,
18813 /*declarator_p=*/true,
18814 /*optional_p=*/false);
18816 if (access_declaration_p)
18818 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18819 cp_parser_simulate_error (parser);
18820 if (!cp_parser_parse_definitely (parser))
18821 return false;
18823 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18825 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18826 if (cxx_dialect < cxx17
18827 && !in_system_header_at (ell->location))
18828 pedwarn (ell->location, 0,
18829 "pack expansion in using-declaration only available "
18830 "with -std=c++17 or -std=gnu++17");
18831 qscope = make_pack_expansion (qscope);
18834 /* The function we call to handle a using-declaration is different
18835 depending on what scope we are in. */
18836 if (qscope == error_mark_node || identifier == error_mark_node)
18838 else if (!identifier_p (identifier)
18839 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18840 /* [namespace.udecl]
18842 A using declaration shall not name a template-id. */
18843 error_at (token->location,
18844 "a template-id may not appear in a using-declaration");
18845 else
18847 if (at_class_scope_p ())
18849 /* Create the USING_DECL. */
18850 decl = do_class_using_decl (qscope, identifier);
18852 if (decl && typename_p)
18853 USING_DECL_TYPENAME_P (decl) = 1;
18855 if (check_for_bare_parameter_packs (decl))
18857 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18858 return false;
18860 else
18861 /* Add it to the list of members in this class. */
18862 finish_member_declaration (decl);
18864 else
18866 decl = cp_parser_lookup_name_simple (parser,
18867 identifier,
18868 token->location);
18869 if (decl == error_mark_node)
18870 cp_parser_name_lookup_error (parser, identifier,
18871 decl, NLE_NULL,
18872 token->location);
18873 else if (check_for_bare_parameter_packs (decl))
18875 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18876 return false;
18878 else if (!at_namespace_scope_p ())
18879 finish_local_using_decl (decl, qscope, identifier);
18880 else
18881 finish_namespace_using_decl (decl, qscope, identifier);
18885 if (!access_declaration_p
18886 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18888 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18889 if (cxx_dialect < cxx17)
18890 pedwarn (comma->location, 0,
18891 "comma-separated list in using-declaration only available "
18892 "with -std=c++17 or -std=gnu++17");
18893 goto again;
18896 /* Look for the final `;'. */
18897 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18899 if (access_declaration_p && errorcount == oldcount)
18900 warning_at (diag_token->location, OPT_Wdeprecated,
18901 "access declarations are deprecated "
18902 "in favour of using-declarations; "
18903 "suggestion: add the %<using%> keyword");
18905 return true;
18908 /* Parse an alias-declaration.
18910 alias-declaration:
18911 using identifier attribute-specifier-seq [opt] = type-id */
18913 static tree
18914 cp_parser_alias_declaration (cp_parser* parser)
18916 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18917 location_t id_location;
18918 cp_declarator *declarator;
18919 cp_decl_specifier_seq decl_specs;
18920 bool member_p;
18921 const char *saved_message = NULL;
18923 /* Look for the `using' keyword. */
18924 cp_token *using_token
18925 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18926 if (using_token == NULL)
18927 return error_mark_node;
18929 id_location = cp_lexer_peek_token (parser->lexer)->location;
18930 id = cp_parser_identifier (parser);
18931 if (id == error_mark_node)
18932 return error_mark_node;
18934 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18935 attributes = cp_parser_attributes_opt (parser);
18936 if (attributes == error_mark_node)
18937 return error_mark_node;
18939 cp_parser_require (parser, CPP_EQ, RT_EQ);
18941 if (cp_parser_error_occurred (parser))
18942 return error_mark_node;
18944 cp_parser_commit_to_tentative_parse (parser);
18946 /* Now we are going to parse the type-id of the declaration. */
18949 [dcl.type]/3 says:
18951 "A type-specifier-seq shall not define a class or enumeration
18952 unless it appears in the type-id of an alias-declaration (7.1.3) that
18953 is not the declaration of a template-declaration."
18955 In other words, if we currently are in an alias template, the
18956 type-id should not define a type.
18958 So let's set parser->type_definition_forbidden_message in that
18959 case; cp_parser_check_type_definition (called by
18960 cp_parser_class_specifier) will then emit an error if a type is
18961 defined in the type-id. */
18962 if (parser->num_template_parameter_lists)
18964 saved_message = parser->type_definition_forbidden_message;
18965 parser->type_definition_forbidden_message =
18966 G_("types may not be defined in alias template declarations");
18969 type = cp_parser_type_id (parser);
18971 /* Restore the error message if need be. */
18972 if (parser->num_template_parameter_lists)
18973 parser->type_definition_forbidden_message = saved_message;
18975 if (type == error_mark_node
18976 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18978 cp_parser_skip_to_end_of_block_or_statement (parser);
18979 return error_mark_node;
18982 /* A typedef-name can also be introduced by an alias-declaration. The
18983 identifier following the using keyword becomes a typedef-name. It has
18984 the same semantics as if it were introduced by the typedef
18985 specifier. In particular, it does not define a new type and it shall
18986 not appear in the type-id. */
18988 clear_decl_specs (&decl_specs);
18989 decl_specs.type = type;
18990 if (attributes != NULL_TREE)
18992 decl_specs.attributes = attributes;
18993 set_and_check_decl_spec_loc (&decl_specs,
18994 ds_attribute,
18995 attrs_token);
18997 set_and_check_decl_spec_loc (&decl_specs,
18998 ds_typedef,
18999 using_token);
19000 set_and_check_decl_spec_loc (&decl_specs,
19001 ds_alias,
19002 using_token);
19004 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19005 declarator->id_loc = id_location;
19007 member_p = at_class_scope_p ();
19008 if (member_p)
19009 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19010 NULL_TREE, attributes);
19011 else
19012 decl = start_decl (declarator, &decl_specs, 0,
19013 attributes, NULL_TREE, &pushed_scope);
19014 if (decl == error_mark_node)
19015 return decl;
19017 // Attach constraints to the alias declaration.
19018 if (flag_concepts && current_template_parms)
19020 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19021 tree constr = build_constraints (reqs, NULL_TREE);
19022 set_constraints (decl, constr);
19025 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19027 if (pushed_scope)
19028 pop_scope (pushed_scope);
19030 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19031 added into the symbol table; otherwise, return the TYPE_DECL. */
19032 if (DECL_LANG_SPECIFIC (decl)
19033 && DECL_TEMPLATE_INFO (decl)
19034 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19036 decl = DECL_TI_TEMPLATE (decl);
19037 if (member_p)
19038 check_member_template (decl);
19041 return decl;
19044 /* Parse a using-directive.
19046 using-directive:
19047 using namespace :: [opt] nested-name-specifier [opt]
19048 namespace-name ; */
19050 static void
19051 cp_parser_using_directive (cp_parser* parser)
19053 tree namespace_decl;
19054 tree attribs;
19056 /* Look for the `using' keyword. */
19057 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19058 /* And the `namespace' keyword. */
19059 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19060 /* Look for the optional `::' operator. */
19061 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19062 /* And the optional nested-name-specifier. */
19063 cp_parser_nested_name_specifier_opt (parser,
19064 /*typename_keyword_p=*/false,
19065 /*check_dependency_p=*/true,
19066 /*type_p=*/false,
19067 /*is_declaration=*/true);
19068 /* Get the namespace being used. */
19069 namespace_decl = cp_parser_namespace_name (parser);
19070 /* And any specified attributes. */
19071 attribs = cp_parser_attributes_opt (parser);
19073 /* Update the symbol table. */
19074 if (namespace_bindings_p ())
19075 finish_namespace_using_directive (namespace_decl, attribs);
19076 else
19077 finish_local_using_directive (namespace_decl, attribs);
19079 /* Look for the final `;'. */
19080 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19083 /* Parse an asm-definition.
19085 asm-definition:
19086 asm ( string-literal ) ;
19088 GNU Extension:
19090 asm-definition:
19091 asm volatile [opt] ( string-literal ) ;
19092 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19093 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19094 : asm-operand-list [opt] ) ;
19095 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19096 : asm-operand-list [opt]
19097 : asm-clobber-list [opt] ) ;
19098 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19099 : asm-clobber-list [opt]
19100 : asm-goto-list ) ; */
19102 static void
19103 cp_parser_asm_definition (cp_parser* parser)
19105 tree string;
19106 tree outputs = NULL_TREE;
19107 tree inputs = NULL_TREE;
19108 tree clobbers = NULL_TREE;
19109 tree labels = NULL_TREE;
19110 tree asm_stmt;
19111 bool volatile_p = false;
19112 bool extended_p = false;
19113 bool invalid_inputs_p = false;
19114 bool invalid_outputs_p = false;
19115 bool goto_p = false;
19116 required_token missing = RT_NONE;
19118 /* Look for the `asm' keyword. */
19119 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19121 if (parser->in_function_body
19122 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19124 error ("%<asm%> in %<constexpr%> function");
19125 cp_function_chain->invalid_constexpr = true;
19128 /* See if the next token is `volatile'. */
19129 if (cp_parser_allow_gnu_extensions_p (parser)
19130 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19132 /* Remember that we saw the `volatile' keyword. */
19133 volatile_p = true;
19134 /* Consume the token. */
19135 cp_lexer_consume_token (parser->lexer);
19137 if (cp_parser_allow_gnu_extensions_p (parser)
19138 && parser->in_function_body
19139 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19141 /* Remember that we saw the `goto' keyword. */
19142 goto_p = true;
19143 /* Consume the token. */
19144 cp_lexer_consume_token (parser->lexer);
19146 /* Look for the opening `('. */
19147 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19148 return;
19149 /* Look for the string. */
19150 string = cp_parser_string_literal (parser, false, false);
19151 if (string == error_mark_node)
19153 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19154 /*consume_paren=*/true);
19155 return;
19158 /* If we're allowing GNU extensions, check for the extended assembly
19159 syntax. Unfortunately, the `:' tokens need not be separated by
19160 a space in C, and so, for compatibility, we tolerate that here
19161 too. Doing that means that we have to treat the `::' operator as
19162 two `:' tokens. */
19163 if (cp_parser_allow_gnu_extensions_p (parser)
19164 && parser->in_function_body
19165 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19166 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19168 bool inputs_p = false;
19169 bool clobbers_p = false;
19170 bool labels_p = false;
19172 /* The extended syntax was used. */
19173 extended_p = true;
19175 /* Look for outputs. */
19176 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19178 /* Consume the `:'. */
19179 cp_lexer_consume_token (parser->lexer);
19180 /* Parse the output-operands. */
19181 if (cp_lexer_next_token_is_not (parser->lexer,
19182 CPP_COLON)
19183 && cp_lexer_next_token_is_not (parser->lexer,
19184 CPP_SCOPE)
19185 && cp_lexer_next_token_is_not (parser->lexer,
19186 CPP_CLOSE_PAREN)
19187 && !goto_p)
19189 outputs = cp_parser_asm_operand_list (parser);
19190 if (outputs == error_mark_node)
19191 invalid_outputs_p = true;
19194 /* If the next token is `::', there are no outputs, and the
19195 next token is the beginning of the inputs. */
19196 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19197 /* The inputs are coming next. */
19198 inputs_p = true;
19200 /* Look for inputs. */
19201 if (inputs_p
19202 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19204 /* Consume the `:' or `::'. */
19205 cp_lexer_consume_token (parser->lexer);
19206 /* Parse the output-operands. */
19207 if (cp_lexer_next_token_is_not (parser->lexer,
19208 CPP_COLON)
19209 && cp_lexer_next_token_is_not (parser->lexer,
19210 CPP_SCOPE)
19211 && cp_lexer_next_token_is_not (parser->lexer,
19212 CPP_CLOSE_PAREN))
19214 inputs = cp_parser_asm_operand_list (parser);
19215 if (inputs == error_mark_node)
19216 invalid_inputs_p = true;
19219 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19220 /* The clobbers are coming next. */
19221 clobbers_p = true;
19223 /* Look for clobbers. */
19224 if (clobbers_p
19225 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19227 clobbers_p = true;
19228 /* Consume the `:' or `::'. */
19229 cp_lexer_consume_token (parser->lexer);
19230 /* Parse the clobbers. */
19231 if (cp_lexer_next_token_is_not (parser->lexer,
19232 CPP_COLON)
19233 && cp_lexer_next_token_is_not (parser->lexer,
19234 CPP_CLOSE_PAREN))
19235 clobbers = cp_parser_asm_clobber_list (parser);
19237 else if (goto_p
19238 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19239 /* The labels are coming next. */
19240 labels_p = true;
19242 /* Look for labels. */
19243 if (labels_p
19244 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19246 labels_p = true;
19247 /* Consume the `:' or `::'. */
19248 cp_lexer_consume_token (parser->lexer);
19249 /* Parse the labels. */
19250 labels = cp_parser_asm_label_list (parser);
19253 if (goto_p && !labels_p)
19254 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19256 else if (goto_p)
19257 missing = RT_COLON_SCOPE;
19259 /* Look for the closing `)'. */
19260 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19261 missing ? missing : RT_CLOSE_PAREN))
19262 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19263 /*consume_paren=*/true);
19264 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19266 if (!invalid_inputs_p && !invalid_outputs_p)
19268 /* Create the ASM_EXPR. */
19269 if (parser->in_function_body)
19271 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19272 inputs, clobbers, labels);
19273 /* If the extended syntax was not used, mark the ASM_EXPR. */
19274 if (!extended_p)
19276 tree temp = asm_stmt;
19277 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19278 temp = TREE_OPERAND (temp, 0);
19280 ASM_INPUT_P (temp) = 1;
19283 else
19284 symtab->finalize_toplevel_asm (string);
19288 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19289 type that comes from the decl-specifier-seq. */
19291 static tree
19292 strip_declarator_types (tree type, cp_declarator *declarator)
19294 for (cp_declarator *d = declarator; d;)
19295 switch (d->kind)
19297 case cdk_id:
19298 case cdk_decomp:
19299 case cdk_error:
19300 d = NULL;
19301 break;
19303 default:
19304 if (TYPE_PTRMEMFUNC_P (type))
19305 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19306 type = TREE_TYPE (type);
19307 d = d->declarator;
19308 break;
19311 return type;
19314 /* Declarators [gram.dcl.decl] */
19316 /* Parse an init-declarator.
19318 init-declarator:
19319 declarator initializer [opt]
19321 GNU Extension:
19323 init-declarator:
19324 declarator asm-specification [opt] attributes [opt] initializer [opt]
19326 function-definition:
19327 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19328 function-body
19329 decl-specifier-seq [opt] declarator function-try-block
19331 GNU Extension:
19333 function-definition:
19334 __extension__ function-definition
19336 TM Extension:
19338 function-definition:
19339 decl-specifier-seq [opt] declarator function-transaction-block
19341 The DECL_SPECIFIERS apply to this declarator. Returns a
19342 representation of the entity declared. If MEMBER_P is TRUE, then
19343 this declarator appears in a class scope. The new DECL created by
19344 this declarator is returned.
19346 The CHECKS are access checks that should be performed once we know
19347 what entity is being declared (and, therefore, what classes have
19348 befriended it).
19350 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19351 for a function-definition here as well. If the declarator is a
19352 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19353 be TRUE upon return. By that point, the function-definition will
19354 have been completely parsed.
19356 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19357 is FALSE.
19359 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19360 parsed declaration if it is an uninitialized single declarator not followed
19361 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19362 if present, will not be consumed. If returned, this declarator will be
19363 created with SD_INITIALIZED but will not call cp_finish_decl.
19365 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19366 and there is an initializer, the pointed location_t is set to the
19367 location of the '=' or `(', or '{' in C++11 token introducing the
19368 initializer. */
19370 static tree
19371 cp_parser_init_declarator (cp_parser* parser,
19372 cp_decl_specifier_seq *decl_specifiers,
19373 vec<deferred_access_check, va_gc> *checks,
19374 bool function_definition_allowed_p,
19375 bool member_p,
19376 int declares_class_or_enum,
19377 bool* function_definition_p,
19378 tree* maybe_range_for_decl,
19379 location_t* init_loc,
19380 tree* auto_result)
19382 cp_token *token = NULL, *asm_spec_start_token = NULL,
19383 *attributes_start_token = NULL;
19384 cp_declarator *declarator;
19385 tree prefix_attributes;
19386 tree attributes = NULL;
19387 tree asm_specification;
19388 tree initializer;
19389 tree decl = NULL_TREE;
19390 tree scope;
19391 int is_initialized;
19392 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19393 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19394 "(...)". */
19395 enum cpp_ttype initialization_kind;
19396 bool is_direct_init = false;
19397 bool is_non_constant_init;
19398 int ctor_dtor_or_conv_p;
19399 bool friend_p = cp_parser_friend_p (decl_specifiers);
19400 tree pushed_scope = NULL_TREE;
19401 bool range_for_decl_p = false;
19402 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19403 location_t tmp_init_loc = UNKNOWN_LOCATION;
19405 /* Gather the attributes that were provided with the
19406 decl-specifiers. */
19407 prefix_attributes = decl_specifiers->attributes;
19409 /* Assume that this is not the declarator for a function
19410 definition. */
19411 if (function_definition_p)
19412 *function_definition_p = false;
19414 /* Default arguments are only permitted for function parameters. */
19415 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19416 parser->default_arg_ok_p = false;
19418 /* Defer access checks while parsing the declarator; we cannot know
19419 what names are accessible until we know what is being
19420 declared. */
19421 resume_deferring_access_checks ();
19423 token = cp_lexer_peek_token (parser->lexer);
19425 /* Parse the declarator. */
19426 declarator
19427 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19428 &ctor_dtor_or_conv_p,
19429 /*parenthesized_p=*/NULL,
19430 member_p, friend_p);
19431 /* Gather up the deferred checks. */
19432 stop_deferring_access_checks ();
19434 parser->default_arg_ok_p = saved_default_arg_ok_p;
19436 /* If the DECLARATOR was erroneous, there's no need to go
19437 further. */
19438 if (declarator == cp_error_declarator)
19439 return error_mark_node;
19441 /* Check that the number of template-parameter-lists is OK. */
19442 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19443 token->location))
19444 return error_mark_node;
19446 if (declares_class_or_enum & 2)
19447 cp_parser_check_for_definition_in_return_type (declarator,
19448 decl_specifiers->type,
19449 decl_specifiers->locations[ds_type_spec]);
19451 /* Figure out what scope the entity declared by the DECLARATOR is
19452 located in. `grokdeclarator' sometimes changes the scope, so
19453 we compute it now. */
19454 scope = get_scope_of_declarator (declarator);
19456 /* Perform any lookups in the declared type which were thought to be
19457 dependent, but are not in the scope of the declarator. */
19458 decl_specifiers->type
19459 = maybe_update_decl_type (decl_specifiers->type, scope);
19461 /* If we're allowing GNU extensions, look for an
19462 asm-specification. */
19463 if (cp_parser_allow_gnu_extensions_p (parser))
19465 /* Look for an asm-specification. */
19466 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19467 asm_specification = cp_parser_asm_specification_opt (parser);
19469 else
19470 asm_specification = NULL_TREE;
19472 /* Look for attributes. */
19473 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19474 attributes = cp_parser_attributes_opt (parser);
19476 /* Peek at the next token. */
19477 token = cp_lexer_peek_token (parser->lexer);
19479 bool bogus_implicit_tmpl = false;
19481 if (function_declarator_p (declarator))
19483 /* Handle C++17 deduction guides. */
19484 if (!decl_specifiers->type
19485 && ctor_dtor_or_conv_p <= 0
19486 && cxx_dialect >= cxx17)
19488 cp_declarator *id = get_id_declarator (declarator);
19489 tree name = id->u.id.unqualified_name;
19490 parser->scope = id->u.id.qualifying_scope;
19491 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19492 if (tmpl
19493 && (DECL_CLASS_TEMPLATE_P (tmpl)
19494 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19496 id->u.id.unqualified_name = dguide_name (tmpl);
19497 id->u.id.sfk = sfk_deduction_guide;
19498 ctor_dtor_or_conv_p = 1;
19502 /* Check to see if the token indicates the start of a
19503 function-definition. */
19504 if (cp_parser_token_starts_function_definition_p (token))
19506 if (!function_definition_allowed_p)
19508 /* If a function-definition should not appear here, issue an
19509 error message. */
19510 cp_parser_error (parser,
19511 "a function-definition is not allowed here");
19512 return error_mark_node;
19515 location_t func_brace_location
19516 = cp_lexer_peek_token (parser->lexer)->location;
19518 /* Neither attributes nor an asm-specification are allowed
19519 on a function-definition. */
19520 if (asm_specification)
19521 error_at (asm_spec_start_token->location,
19522 "an asm-specification is not allowed "
19523 "on a function-definition");
19524 if (attributes)
19525 error_at (attributes_start_token->location,
19526 "attributes are not allowed "
19527 "on a function-definition");
19528 /* This is a function-definition. */
19529 *function_definition_p = true;
19531 /* Parse the function definition. */
19532 if (member_p)
19533 decl = cp_parser_save_member_function_body (parser,
19534 decl_specifiers,
19535 declarator,
19536 prefix_attributes);
19537 else
19538 decl =
19539 (cp_parser_function_definition_from_specifiers_and_declarator
19540 (parser, decl_specifiers, prefix_attributes, declarator));
19542 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19544 /* This is where the prologue starts... */
19545 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19546 = func_brace_location;
19549 return decl;
19552 else if (parser->fully_implicit_function_template_p)
19554 /* A non-template declaration involving a function parameter list
19555 containing an implicit template parameter will be made into a
19556 template. If the resulting declaration is not going to be an
19557 actual function then finish the template scope here to prevent it.
19558 An error message will be issued once we have a decl to talk about.
19560 FIXME probably we should do type deduction rather than create an
19561 implicit template, but the standard currently doesn't allow it. */
19562 bogus_implicit_tmpl = true;
19563 finish_fully_implicit_template (parser, NULL_TREE);
19566 /* [dcl.dcl]
19568 Only in function declarations for constructors, destructors, type
19569 conversions, and deduction guides can the decl-specifier-seq be omitted.
19571 We explicitly postpone this check past the point where we handle
19572 function-definitions because we tolerate function-definitions
19573 that are missing their return types in some modes. */
19574 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19576 cp_parser_error (parser,
19577 "expected constructor, destructor, or type conversion");
19578 return error_mark_node;
19581 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19582 if (token->type == CPP_EQ
19583 || token->type == CPP_OPEN_PAREN
19584 || token->type == CPP_OPEN_BRACE)
19586 is_initialized = SD_INITIALIZED;
19587 initialization_kind = token->type;
19588 if (maybe_range_for_decl)
19589 *maybe_range_for_decl = error_mark_node;
19590 tmp_init_loc = token->location;
19591 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19592 *init_loc = tmp_init_loc;
19594 if (token->type == CPP_EQ
19595 && function_declarator_p (declarator))
19597 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19598 if (t2->keyword == RID_DEFAULT)
19599 is_initialized = SD_DEFAULTED;
19600 else if (t2->keyword == RID_DELETE)
19601 is_initialized = SD_DELETED;
19604 else
19606 /* If the init-declarator isn't initialized and isn't followed by a
19607 `,' or `;', it's not a valid init-declarator. */
19608 if (token->type != CPP_COMMA
19609 && token->type != CPP_SEMICOLON)
19611 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19612 range_for_decl_p = true;
19613 else
19615 if (!maybe_range_for_decl)
19616 cp_parser_error (parser, "expected initializer");
19617 return error_mark_node;
19620 is_initialized = SD_UNINITIALIZED;
19621 initialization_kind = CPP_EOF;
19624 /* Because start_decl has side-effects, we should only call it if we
19625 know we're going ahead. By this point, we know that we cannot
19626 possibly be looking at any other construct. */
19627 cp_parser_commit_to_tentative_parse (parser);
19629 /* Enter the newly declared entry in the symbol table. If we're
19630 processing a declaration in a class-specifier, we wait until
19631 after processing the initializer. */
19632 if (!member_p)
19634 if (parser->in_unbraced_linkage_specification_p)
19635 decl_specifiers->storage_class = sc_extern;
19636 decl = start_decl (declarator, decl_specifiers,
19637 range_for_decl_p? SD_INITIALIZED : is_initialized,
19638 attributes, prefix_attributes, &pushed_scope);
19639 cp_finalize_omp_declare_simd (parser, decl);
19640 cp_finalize_oacc_routine (parser, decl, false);
19641 /* Adjust location of decl if declarator->id_loc is more appropriate:
19642 set, and decl wasn't merged with another decl, in which case its
19643 location would be different from input_location, and more accurate. */
19644 if (DECL_P (decl)
19645 && declarator->id_loc != UNKNOWN_LOCATION
19646 && DECL_SOURCE_LOCATION (decl) == input_location)
19647 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19649 else if (scope)
19650 /* Enter the SCOPE. That way unqualified names appearing in the
19651 initializer will be looked up in SCOPE. */
19652 pushed_scope = push_scope (scope);
19654 /* Perform deferred access control checks, now that we know in which
19655 SCOPE the declared entity resides. */
19656 if (!member_p && decl)
19658 tree saved_current_function_decl = NULL_TREE;
19660 /* If the entity being declared is a function, pretend that we
19661 are in its scope. If it is a `friend', it may have access to
19662 things that would not otherwise be accessible. */
19663 if (TREE_CODE (decl) == FUNCTION_DECL)
19665 saved_current_function_decl = current_function_decl;
19666 current_function_decl = decl;
19669 /* Perform access checks for template parameters. */
19670 cp_parser_perform_template_parameter_access_checks (checks);
19672 /* Perform the access control checks for the declarator and the
19673 decl-specifiers. */
19674 perform_deferred_access_checks (tf_warning_or_error);
19676 /* Restore the saved value. */
19677 if (TREE_CODE (decl) == FUNCTION_DECL)
19678 current_function_decl = saved_current_function_decl;
19681 /* Parse the initializer. */
19682 initializer = NULL_TREE;
19683 is_direct_init = false;
19684 is_non_constant_init = true;
19685 if (is_initialized)
19687 if (function_declarator_p (declarator))
19689 if (initialization_kind == CPP_EQ)
19690 initializer = cp_parser_pure_specifier (parser);
19691 else
19693 /* If the declaration was erroneous, we don't really
19694 know what the user intended, so just silently
19695 consume the initializer. */
19696 if (decl != error_mark_node)
19697 error_at (tmp_init_loc, "initializer provided for function");
19698 cp_parser_skip_to_closing_parenthesis (parser,
19699 /*recovering=*/true,
19700 /*or_comma=*/false,
19701 /*consume_paren=*/true);
19704 else
19706 /* We want to record the extra mangling scope for in-class
19707 initializers of class members and initializers of static data
19708 member templates. The former involves deferring
19709 parsing of the initializer until end of class as with default
19710 arguments. So right here we only handle the latter. */
19711 if (!member_p && processing_template_decl)
19712 start_lambda_scope (decl);
19713 initializer = cp_parser_initializer (parser,
19714 &is_direct_init,
19715 &is_non_constant_init);
19716 if (!member_p && processing_template_decl)
19717 finish_lambda_scope ();
19718 if (initializer == error_mark_node)
19719 cp_parser_skip_to_end_of_statement (parser);
19723 /* The old parser allows attributes to appear after a parenthesized
19724 initializer. Mark Mitchell proposed removing this functionality
19725 on the GCC mailing lists on 2002-08-13. This parser accepts the
19726 attributes -- but ignores them. */
19727 if (cp_parser_allow_gnu_extensions_p (parser)
19728 && initialization_kind == CPP_OPEN_PAREN)
19729 if (cp_parser_attributes_opt (parser))
19730 warning (OPT_Wattributes,
19731 "attributes after parenthesized initializer ignored");
19733 /* And now complain about a non-function implicit template. */
19734 if (bogus_implicit_tmpl && decl != error_mark_node)
19735 error_at (DECL_SOURCE_LOCATION (decl),
19736 "non-function %qD declared as implicit template", decl);
19738 /* For an in-class declaration, use `grokfield' to create the
19739 declaration. */
19740 if (member_p)
19742 if (pushed_scope)
19744 pop_scope (pushed_scope);
19745 pushed_scope = NULL_TREE;
19747 decl = grokfield (declarator, decl_specifiers,
19748 initializer, !is_non_constant_init,
19749 /*asmspec=*/NULL_TREE,
19750 chainon (attributes, prefix_attributes));
19751 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19752 cp_parser_save_default_args (parser, decl);
19753 cp_finalize_omp_declare_simd (parser, decl);
19754 cp_finalize_oacc_routine (parser, decl, false);
19757 /* Finish processing the declaration. But, skip member
19758 declarations. */
19759 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19761 cp_finish_decl (decl,
19762 initializer, !is_non_constant_init,
19763 asm_specification,
19764 /* If the initializer is in parentheses, then this is
19765 a direct-initialization, which means that an
19766 `explicit' constructor is OK. Otherwise, an
19767 `explicit' constructor cannot be used. */
19768 ((is_direct_init || !is_initialized)
19769 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19771 else if ((cxx_dialect != cxx98) && friend_p
19772 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19773 /* Core issue #226 (C++0x only): A default template-argument
19774 shall not be specified in a friend class template
19775 declaration. */
19776 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19777 /*is_partial=*/false, /*is_friend_decl=*/1);
19779 if (!friend_p && pushed_scope)
19780 pop_scope (pushed_scope);
19782 if (function_declarator_p (declarator)
19783 && parser->fully_implicit_function_template_p)
19785 if (member_p)
19786 decl = finish_fully_implicit_template (parser, decl);
19787 else
19788 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19791 if (auto_result && is_initialized && decl_specifiers->type
19792 && type_uses_auto (decl_specifiers->type))
19793 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19795 return decl;
19798 /* Parse a declarator.
19800 declarator:
19801 direct-declarator
19802 ptr-operator declarator
19804 abstract-declarator:
19805 ptr-operator abstract-declarator [opt]
19806 direct-abstract-declarator
19808 GNU Extensions:
19810 declarator:
19811 attributes [opt] direct-declarator
19812 attributes [opt] ptr-operator declarator
19814 abstract-declarator:
19815 attributes [opt] ptr-operator abstract-declarator [opt]
19816 attributes [opt] direct-abstract-declarator
19818 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19819 detect constructors, destructors, deduction guides, or conversion operators.
19820 It is set to -1 if the declarator is a name, and +1 if it is a
19821 function. Otherwise it is set to zero. Usually you just want to
19822 test for >0, but internally the negative value is used.
19824 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19825 a decl-specifier-seq unless it declares a constructor, destructor,
19826 or conversion. It might seem that we could check this condition in
19827 semantic analysis, rather than parsing, but that makes it difficult
19828 to handle something like `f()'. We want to notice that there are
19829 no decl-specifiers, and therefore realize that this is an
19830 expression, not a declaration.)
19832 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19833 the declarator is a direct-declarator of the form "(...)".
19835 MEMBER_P is true iff this declarator is a member-declarator.
19837 FRIEND_P is true iff this declarator is a friend. */
19839 static cp_declarator *
19840 cp_parser_declarator (cp_parser* parser,
19841 cp_parser_declarator_kind dcl_kind,
19842 int* ctor_dtor_or_conv_p,
19843 bool* parenthesized_p,
19844 bool member_p, bool friend_p)
19846 cp_declarator *declarator;
19847 enum tree_code code;
19848 cp_cv_quals cv_quals;
19849 tree class_type;
19850 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19852 /* Assume this is not a constructor, destructor, or type-conversion
19853 operator. */
19854 if (ctor_dtor_or_conv_p)
19855 *ctor_dtor_or_conv_p = 0;
19857 if (cp_parser_allow_gnu_extensions_p (parser))
19858 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19860 /* Check for the ptr-operator production. */
19861 cp_parser_parse_tentatively (parser);
19862 /* Parse the ptr-operator. */
19863 code = cp_parser_ptr_operator (parser,
19864 &class_type,
19865 &cv_quals,
19866 &std_attributes);
19868 /* If that worked, then we have a ptr-operator. */
19869 if (cp_parser_parse_definitely (parser))
19871 /* If a ptr-operator was found, then this declarator was not
19872 parenthesized. */
19873 if (parenthesized_p)
19874 *parenthesized_p = true;
19875 /* The dependent declarator is optional if we are parsing an
19876 abstract-declarator. */
19877 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19878 cp_parser_parse_tentatively (parser);
19880 /* Parse the dependent declarator. */
19881 declarator = cp_parser_declarator (parser, dcl_kind,
19882 /*ctor_dtor_or_conv_p=*/NULL,
19883 /*parenthesized_p=*/NULL,
19884 /*member_p=*/false,
19885 friend_p);
19887 /* If we are parsing an abstract-declarator, we must handle the
19888 case where the dependent declarator is absent. */
19889 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19890 && !cp_parser_parse_definitely (parser))
19891 declarator = NULL;
19893 declarator = cp_parser_make_indirect_declarator
19894 (code, class_type, cv_quals, declarator, std_attributes);
19896 /* Everything else is a direct-declarator. */
19897 else
19899 if (parenthesized_p)
19900 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19901 CPP_OPEN_PAREN);
19902 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19903 ctor_dtor_or_conv_p,
19904 member_p, friend_p);
19907 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19908 declarator->attributes = gnu_attributes;
19909 return declarator;
19912 /* Parse a direct-declarator or direct-abstract-declarator.
19914 direct-declarator:
19915 declarator-id
19916 direct-declarator ( parameter-declaration-clause )
19917 cv-qualifier-seq [opt]
19918 ref-qualifier [opt]
19919 exception-specification [opt]
19920 direct-declarator [ constant-expression [opt] ]
19921 ( declarator )
19923 direct-abstract-declarator:
19924 direct-abstract-declarator [opt]
19925 ( parameter-declaration-clause )
19926 cv-qualifier-seq [opt]
19927 ref-qualifier [opt]
19928 exception-specification [opt]
19929 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19930 ( abstract-declarator )
19932 Returns a representation of the declarator. DCL_KIND is
19933 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19934 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19935 we are parsing a direct-declarator. It is
19936 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19937 of ambiguity we prefer an abstract declarator, as per
19938 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19939 as for cp_parser_declarator. */
19941 static cp_declarator *
19942 cp_parser_direct_declarator (cp_parser* parser,
19943 cp_parser_declarator_kind dcl_kind,
19944 int* ctor_dtor_or_conv_p,
19945 bool member_p, bool friend_p)
19947 cp_token *token;
19948 cp_declarator *declarator = NULL;
19949 tree scope = NULL_TREE;
19950 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19951 bool saved_in_declarator_p = parser->in_declarator_p;
19952 bool first = true;
19953 tree pushed_scope = NULL_TREE;
19954 cp_token *open_paren = NULL, *close_paren = NULL;
19956 while (true)
19958 /* Peek at the next token. */
19959 token = cp_lexer_peek_token (parser->lexer);
19960 if (token->type == CPP_OPEN_PAREN)
19962 /* This is either a parameter-declaration-clause, or a
19963 parenthesized declarator. When we know we are parsing a
19964 named declarator, it must be a parenthesized declarator
19965 if FIRST is true. For instance, `(int)' is a
19966 parameter-declaration-clause, with an omitted
19967 direct-abstract-declarator. But `((*))', is a
19968 parenthesized abstract declarator. Finally, when T is a
19969 template parameter `(T)' is a
19970 parameter-declaration-clause, and not a parenthesized
19971 named declarator.
19973 We first try and parse a parameter-declaration-clause,
19974 and then try a nested declarator (if FIRST is true).
19976 It is not an error for it not to be a
19977 parameter-declaration-clause, even when FIRST is
19978 false. Consider,
19980 int i (int);
19981 int i (3);
19983 The first is the declaration of a function while the
19984 second is the definition of a variable, including its
19985 initializer.
19987 Having seen only the parenthesis, we cannot know which of
19988 these two alternatives should be selected. Even more
19989 complex are examples like:
19991 int i (int (a));
19992 int i (int (3));
19994 The former is a function-declaration; the latter is a
19995 variable initialization.
19997 Thus again, we try a parameter-declaration-clause, and if
19998 that fails, we back out and return. */
20000 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20002 tree params;
20003 bool is_declarator = false;
20005 open_paren = NULL;
20007 /* In a member-declarator, the only valid interpretation
20008 of a parenthesis is the start of a
20009 parameter-declaration-clause. (It is invalid to
20010 initialize a static data member with a parenthesized
20011 initializer; only the "=" form of initialization is
20012 permitted.) */
20013 if (!member_p)
20014 cp_parser_parse_tentatively (parser);
20016 /* Consume the `('. */
20017 matching_parens parens;
20018 parens.consume_open (parser);
20019 if (first)
20021 /* If this is going to be an abstract declarator, we're
20022 in a declarator and we can't have default args. */
20023 parser->default_arg_ok_p = false;
20024 parser->in_declarator_p = true;
20027 begin_scope (sk_function_parms, NULL_TREE);
20029 /* Parse the parameter-declaration-clause. */
20030 params = cp_parser_parameter_declaration_clause (parser);
20032 /* Consume the `)'. */
20033 parens.require_close (parser);
20035 /* If all went well, parse the cv-qualifier-seq,
20036 ref-qualifier and the exception-specification. */
20037 if (member_p || cp_parser_parse_definitely (parser))
20039 cp_cv_quals cv_quals;
20040 cp_virt_specifiers virt_specifiers;
20041 cp_ref_qualifier ref_qual;
20042 tree exception_specification;
20043 tree late_return;
20044 tree attrs;
20045 bool memfn = (member_p || (pushed_scope
20046 && CLASS_TYPE_P (pushed_scope)));
20048 is_declarator = true;
20050 if (ctor_dtor_or_conv_p)
20051 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20052 first = false;
20054 /* Parse the cv-qualifier-seq. */
20055 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20056 /* Parse the ref-qualifier. */
20057 ref_qual = cp_parser_ref_qualifier_opt (parser);
20058 /* Parse the tx-qualifier. */
20059 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20060 /* And the exception-specification. */
20061 exception_specification
20062 = cp_parser_exception_specification_opt (parser);
20064 attrs = cp_parser_std_attribute_spec_seq (parser);
20066 /* In here, we handle cases where attribute is used after
20067 the function declaration. For example:
20068 void func (int x) __attribute__((vector(..))); */
20069 tree gnu_attrs = NULL_TREE;
20070 if (flag_cilkplus
20071 && cp_next_tokens_can_be_gnu_attribute_p (parser))
20073 cp_parser_parse_tentatively (parser);
20074 tree attr = cp_parser_gnu_attributes_opt (parser);
20075 if (cp_lexer_next_token_is_not (parser->lexer,
20076 CPP_SEMICOLON)
20077 && cp_lexer_next_token_is_not (parser->lexer,
20078 CPP_OPEN_BRACE))
20079 cp_parser_abort_tentative_parse (parser);
20080 else if (!cp_parser_parse_definitely (parser))
20082 else
20083 gnu_attrs = attr;
20085 tree requires_clause = NULL_TREE;
20086 late_return = (cp_parser_late_return_type_opt
20087 (parser, declarator, requires_clause,
20088 memfn ? cv_quals : -1));
20090 /* Parse the virt-specifier-seq. */
20091 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20093 /* Create the function-declarator. */
20094 declarator = make_call_declarator (declarator,
20095 params,
20096 cv_quals,
20097 virt_specifiers,
20098 ref_qual,
20099 tx_qual,
20100 exception_specification,
20101 late_return,
20102 requires_clause);
20103 declarator->std_attributes = attrs;
20104 declarator->attributes = gnu_attrs;
20105 /* Any subsequent parameter lists are to do with
20106 return type, so are not those of the declared
20107 function. */
20108 parser->default_arg_ok_p = false;
20111 /* Remove the function parms from scope. */
20112 pop_bindings_and_leave_scope ();
20114 if (is_declarator)
20115 /* Repeat the main loop. */
20116 continue;
20119 /* If this is the first, we can try a parenthesized
20120 declarator. */
20121 if (first)
20123 bool saved_in_type_id_in_expr_p;
20125 parser->default_arg_ok_p = saved_default_arg_ok_p;
20126 parser->in_declarator_p = saved_in_declarator_p;
20128 open_paren = token;
20129 /* Consume the `('. */
20130 matching_parens parens;
20131 parens.consume_open (parser);
20132 /* Parse the nested declarator. */
20133 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20134 parser->in_type_id_in_expr_p = true;
20135 declarator
20136 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20137 /*parenthesized_p=*/NULL,
20138 member_p, friend_p);
20139 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20140 first = false;
20141 /* Expect a `)'. */
20142 close_paren = cp_lexer_peek_token (parser->lexer);
20143 if (!parens.require_close (parser))
20144 declarator = cp_error_declarator;
20145 if (declarator == cp_error_declarator)
20146 break;
20148 goto handle_declarator;
20150 /* Otherwise, we must be done. */
20151 else
20152 break;
20154 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20155 && token->type == CPP_OPEN_SQUARE
20156 && !cp_next_tokens_can_be_attribute_p (parser))
20158 /* Parse an array-declarator. */
20159 tree bounds, attrs;
20161 if (ctor_dtor_or_conv_p)
20162 *ctor_dtor_or_conv_p = 0;
20164 open_paren = NULL;
20165 first = false;
20166 parser->default_arg_ok_p = false;
20167 parser->in_declarator_p = true;
20168 /* Consume the `['. */
20169 cp_lexer_consume_token (parser->lexer);
20170 /* Peek at the next token. */
20171 token = cp_lexer_peek_token (parser->lexer);
20172 /* If the next token is `]', then there is no
20173 constant-expression. */
20174 if (token->type != CPP_CLOSE_SQUARE)
20176 bool non_constant_p;
20177 bounds
20178 = cp_parser_constant_expression (parser,
20179 /*allow_non_constant=*/true,
20180 &non_constant_p);
20181 if (!non_constant_p)
20182 /* OK */;
20183 else if (error_operand_p (bounds))
20184 /* Already gave an error. */;
20185 else if (!parser->in_function_body
20186 || current_binding_level->kind == sk_function_parms)
20188 /* Normally, the array bound must be an integral constant
20189 expression. However, as an extension, we allow VLAs
20190 in function scopes as long as they aren't part of a
20191 parameter declaration. */
20192 cp_parser_error (parser,
20193 "array bound is not an integer constant");
20194 bounds = error_mark_node;
20196 else if (processing_template_decl
20197 && !type_dependent_expression_p (bounds))
20199 /* Remember this wasn't a constant-expression. */
20200 bounds = build_nop (TREE_TYPE (bounds), bounds);
20201 TREE_SIDE_EFFECTS (bounds) = 1;
20204 else
20205 bounds = NULL_TREE;
20206 /* Look for the closing `]'. */
20207 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20209 declarator = cp_error_declarator;
20210 break;
20213 attrs = cp_parser_std_attribute_spec_seq (parser);
20214 declarator = make_array_declarator (declarator, bounds);
20215 declarator->std_attributes = attrs;
20217 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20220 tree qualifying_scope;
20221 tree unqualified_name;
20222 tree attrs;
20223 special_function_kind sfk;
20224 bool abstract_ok;
20225 bool pack_expansion_p = false;
20226 cp_token *declarator_id_start_token;
20228 /* Parse a declarator-id */
20229 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20230 if (abstract_ok)
20232 cp_parser_parse_tentatively (parser);
20234 /* If we see an ellipsis, we should be looking at a
20235 parameter pack. */
20236 if (token->type == CPP_ELLIPSIS)
20238 /* Consume the `...' */
20239 cp_lexer_consume_token (parser->lexer);
20241 pack_expansion_p = true;
20245 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20246 unqualified_name
20247 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20248 qualifying_scope = parser->scope;
20249 if (abstract_ok)
20251 bool okay = false;
20253 if (!unqualified_name && pack_expansion_p)
20255 /* Check whether an error occurred. */
20256 okay = !cp_parser_error_occurred (parser);
20258 /* We already consumed the ellipsis to mark a
20259 parameter pack, but we have no way to report it,
20260 so abort the tentative parse. We will be exiting
20261 immediately anyway. */
20262 cp_parser_abort_tentative_parse (parser);
20264 else
20265 okay = cp_parser_parse_definitely (parser);
20267 if (!okay)
20268 unqualified_name = error_mark_node;
20269 else if (unqualified_name
20270 && (qualifying_scope
20271 || (!identifier_p (unqualified_name))))
20273 cp_parser_error (parser, "expected unqualified-id");
20274 unqualified_name = error_mark_node;
20278 if (!unqualified_name)
20279 return NULL;
20280 if (unqualified_name == error_mark_node)
20282 declarator = cp_error_declarator;
20283 pack_expansion_p = false;
20284 declarator->parameter_pack_p = false;
20285 break;
20288 attrs = cp_parser_std_attribute_spec_seq (parser);
20290 if (qualifying_scope && at_namespace_scope_p ()
20291 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20293 /* In the declaration of a member of a template class
20294 outside of the class itself, the SCOPE will sometimes
20295 be a TYPENAME_TYPE. For example, given:
20297 template <typename T>
20298 int S<T>::R::i = 3;
20300 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20301 this context, we must resolve S<T>::R to an ordinary
20302 type, rather than a typename type.
20304 The reason we normally avoid resolving TYPENAME_TYPEs
20305 is that a specialization of `S' might render
20306 `S<T>::R' not a type. However, if `S' is
20307 specialized, then this `i' will not be used, so there
20308 is no harm in resolving the types here. */
20309 tree type;
20311 /* Resolve the TYPENAME_TYPE. */
20312 type = resolve_typename_type (qualifying_scope,
20313 /*only_current_p=*/false);
20314 /* If that failed, the declarator is invalid. */
20315 if (TREE_CODE (type) == TYPENAME_TYPE)
20317 if (typedef_variant_p (type))
20318 error_at (declarator_id_start_token->location,
20319 "cannot define member of dependent typedef "
20320 "%qT", type);
20321 else
20322 error_at (declarator_id_start_token->location,
20323 "%<%T::%E%> is not a type",
20324 TYPE_CONTEXT (qualifying_scope),
20325 TYPE_IDENTIFIER (qualifying_scope));
20327 qualifying_scope = type;
20330 sfk = sfk_none;
20332 if (unqualified_name)
20334 tree class_type;
20336 if (qualifying_scope
20337 && CLASS_TYPE_P (qualifying_scope))
20338 class_type = qualifying_scope;
20339 else
20340 class_type = current_class_type;
20342 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20344 tree name_type = TREE_TYPE (unqualified_name);
20346 if (!class_type || !same_type_p (name_type, class_type))
20348 /* We do not attempt to print the declarator
20349 here because we do not have enough
20350 information about its original syntactic
20351 form. */
20352 cp_parser_error (parser, "invalid declarator");
20353 declarator = cp_error_declarator;
20354 break;
20356 else if (qualifying_scope
20357 && CLASSTYPE_USE_TEMPLATE (name_type))
20359 error_at (declarator_id_start_token->location,
20360 "invalid use of constructor as a template");
20361 inform (declarator_id_start_token->location,
20362 "use %<%T::%D%> instead of %<%T::%D%> to "
20363 "name the constructor in a qualified name",
20364 class_type,
20365 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20366 class_type, name_type);
20367 declarator = cp_error_declarator;
20368 break;
20370 unqualified_name = constructor_name (class_type);
20373 if (class_type)
20375 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20376 sfk = sfk_destructor;
20377 else if (identifier_p (unqualified_name)
20378 && IDENTIFIER_CONV_OP_P (unqualified_name))
20379 sfk = sfk_conversion;
20380 else if (/* There's no way to declare a constructor
20381 for an unnamed type, even if the type
20382 got a name for linkage purposes. */
20383 !TYPE_WAS_UNNAMED (class_type)
20384 /* Handle correctly (c++/19200):
20386 struct S {
20387 struct T{};
20388 friend void S(T);
20391 and also:
20393 namespace N {
20394 void S();
20397 struct S {
20398 friend void N::S();
20399 }; */
20400 && (!friend_p || class_type == qualifying_scope)
20401 && constructor_name_p (unqualified_name,
20402 class_type))
20403 sfk = sfk_constructor;
20404 else if (is_overloaded_fn (unqualified_name)
20405 && DECL_CONSTRUCTOR_P (get_first_fn
20406 (unqualified_name)))
20407 sfk = sfk_constructor;
20409 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20410 *ctor_dtor_or_conv_p = -1;
20413 declarator = make_id_declarator (qualifying_scope,
20414 unqualified_name,
20415 sfk);
20416 declarator->std_attributes = attrs;
20417 declarator->id_loc = token->location;
20418 declarator->parameter_pack_p = pack_expansion_p;
20420 if (pack_expansion_p)
20421 maybe_warn_variadic_templates ();
20424 handle_declarator:;
20425 scope = get_scope_of_declarator (declarator);
20426 if (scope)
20428 /* Any names that appear after the declarator-id for a
20429 member are looked up in the containing scope. */
20430 if (at_function_scope_p ())
20432 /* But declarations with qualified-ids can't appear in a
20433 function. */
20434 cp_parser_error (parser, "qualified-id in declaration");
20435 declarator = cp_error_declarator;
20436 break;
20438 pushed_scope = push_scope (scope);
20440 parser->in_declarator_p = true;
20441 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20442 || (declarator && declarator->kind == cdk_id))
20443 /* Default args are only allowed on function
20444 declarations. */
20445 parser->default_arg_ok_p = saved_default_arg_ok_p;
20446 else
20447 parser->default_arg_ok_p = false;
20449 first = false;
20451 /* We're done. */
20452 else
20453 break;
20456 /* For an abstract declarator, we might wind up with nothing at this
20457 point. That's an error; the declarator is not optional. */
20458 if (!declarator)
20459 cp_parser_error (parser, "expected declarator");
20460 else if (open_paren)
20462 /* Record overly parenthesized declarator so we can give a
20463 diagnostic about confusing decl/expr disambiguation. */
20464 if (declarator->kind == cdk_array)
20466 /* If the open and close parens are on different lines, this
20467 is probably a formatting thing, so ignore. */
20468 expanded_location open = expand_location (open_paren->location);
20469 expanded_location close = expand_location (close_paren->location);
20470 if (open.line != close.line || open.file != close.file)
20471 open_paren = NULL;
20473 if (open_paren)
20474 declarator->parenthesized = open_paren->location;
20477 /* If we entered a scope, we must exit it now. */
20478 if (pushed_scope)
20479 pop_scope (pushed_scope);
20481 parser->default_arg_ok_p = saved_default_arg_ok_p;
20482 parser->in_declarator_p = saved_in_declarator_p;
20484 return declarator;
20487 /* Parse a ptr-operator.
20489 ptr-operator:
20490 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20491 * cv-qualifier-seq [opt]
20493 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20494 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20496 GNU Extension:
20498 ptr-operator:
20499 & cv-qualifier-seq [opt]
20501 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20502 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20503 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20504 filled in with the TYPE containing the member. *CV_QUALS is
20505 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20506 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20507 Note that the tree codes returned by this function have nothing
20508 to do with the types of trees that will be eventually be created
20509 to represent the pointer or reference type being parsed. They are
20510 just constants with suggestive names. */
20511 static enum tree_code
20512 cp_parser_ptr_operator (cp_parser* parser,
20513 tree* type,
20514 cp_cv_quals *cv_quals,
20515 tree *attributes)
20517 enum tree_code code = ERROR_MARK;
20518 cp_token *token;
20519 tree attrs = NULL_TREE;
20521 /* Assume that it's not a pointer-to-member. */
20522 *type = NULL_TREE;
20523 /* And that there are no cv-qualifiers. */
20524 *cv_quals = TYPE_UNQUALIFIED;
20526 /* Peek at the next token. */
20527 token = cp_lexer_peek_token (parser->lexer);
20529 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20530 if (token->type == CPP_MULT)
20531 code = INDIRECT_REF;
20532 else if (token->type == CPP_AND)
20533 code = ADDR_EXPR;
20534 else if ((cxx_dialect != cxx98) &&
20535 token->type == CPP_AND_AND) /* C++0x only */
20536 code = NON_LVALUE_EXPR;
20538 if (code != ERROR_MARK)
20540 /* Consume the `*', `&' or `&&'. */
20541 cp_lexer_consume_token (parser->lexer);
20543 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20544 `&', if we are allowing GNU extensions. (The only qualifier
20545 that can legally appear after `&' is `restrict', but that is
20546 enforced during semantic analysis. */
20547 if (code == INDIRECT_REF
20548 || cp_parser_allow_gnu_extensions_p (parser))
20549 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20551 attrs = cp_parser_std_attribute_spec_seq (parser);
20552 if (attributes != NULL)
20553 *attributes = attrs;
20555 else
20557 /* Try the pointer-to-member case. */
20558 cp_parser_parse_tentatively (parser);
20559 /* Look for the optional `::' operator. */
20560 cp_parser_global_scope_opt (parser,
20561 /*current_scope_valid_p=*/false);
20562 /* Look for the nested-name specifier. */
20563 token = cp_lexer_peek_token (parser->lexer);
20564 cp_parser_nested_name_specifier (parser,
20565 /*typename_keyword_p=*/false,
20566 /*check_dependency_p=*/true,
20567 /*type_p=*/false,
20568 /*is_declaration=*/false);
20569 /* If we found it, and the next token is a `*', then we are
20570 indeed looking at a pointer-to-member operator. */
20571 if (!cp_parser_error_occurred (parser)
20572 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20574 /* Indicate that the `*' operator was used. */
20575 code = INDIRECT_REF;
20577 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20578 error_at (token->location, "%qD is a namespace", parser->scope);
20579 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20580 error_at (token->location, "cannot form pointer to member of "
20581 "non-class %q#T", parser->scope);
20582 else
20584 /* The type of which the member is a member is given by the
20585 current SCOPE. */
20586 *type = parser->scope;
20587 /* The next name will not be qualified. */
20588 parser->scope = NULL_TREE;
20589 parser->qualifying_scope = NULL_TREE;
20590 parser->object_scope = NULL_TREE;
20591 /* Look for optional c++11 attributes. */
20592 attrs = cp_parser_std_attribute_spec_seq (parser);
20593 if (attributes != NULL)
20594 *attributes = attrs;
20595 /* Look for the optional cv-qualifier-seq. */
20596 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20599 /* If that didn't work we don't have a ptr-operator. */
20600 if (!cp_parser_parse_definitely (parser))
20601 cp_parser_error (parser, "expected ptr-operator");
20604 return code;
20607 /* Parse an (optional) cv-qualifier-seq.
20609 cv-qualifier-seq:
20610 cv-qualifier cv-qualifier-seq [opt]
20612 cv-qualifier:
20613 const
20614 volatile
20616 GNU Extension:
20618 cv-qualifier:
20619 __restrict__
20621 Returns a bitmask representing the cv-qualifiers. */
20623 static cp_cv_quals
20624 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20626 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20628 while (true)
20630 cp_token *token;
20631 cp_cv_quals cv_qualifier;
20633 /* Peek at the next token. */
20634 token = cp_lexer_peek_token (parser->lexer);
20635 /* See if it's a cv-qualifier. */
20636 switch (token->keyword)
20638 case RID_CONST:
20639 cv_qualifier = TYPE_QUAL_CONST;
20640 break;
20642 case RID_VOLATILE:
20643 cv_qualifier = TYPE_QUAL_VOLATILE;
20644 break;
20646 case RID_RESTRICT:
20647 cv_qualifier = TYPE_QUAL_RESTRICT;
20648 break;
20650 default:
20651 cv_qualifier = TYPE_UNQUALIFIED;
20652 break;
20655 if (!cv_qualifier)
20656 break;
20658 if (cv_quals & cv_qualifier)
20660 gcc_rich_location richloc (token->location);
20661 richloc.add_fixit_remove ();
20662 error_at (&richloc, "duplicate cv-qualifier");
20663 cp_lexer_purge_token (parser->lexer);
20665 else
20667 cp_lexer_consume_token (parser->lexer);
20668 cv_quals |= cv_qualifier;
20672 return cv_quals;
20675 /* Parse an (optional) ref-qualifier
20677 ref-qualifier:
20681 Returns cp_ref_qualifier representing ref-qualifier. */
20683 static cp_ref_qualifier
20684 cp_parser_ref_qualifier_opt (cp_parser* parser)
20686 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20688 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20689 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20690 return ref_qual;
20692 while (true)
20694 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20695 cp_token *token = cp_lexer_peek_token (parser->lexer);
20697 switch (token->type)
20699 case CPP_AND:
20700 curr_ref_qual = REF_QUAL_LVALUE;
20701 break;
20703 case CPP_AND_AND:
20704 curr_ref_qual = REF_QUAL_RVALUE;
20705 break;
20707 default:
20708 curr_ref_qual = REF_QUAL_NONE;
20709 break;
20712 if (!curr_ref_qual)
20713 break;
20714 else if (ref_qual)
20716 error_at (token->location, "multiple ref-qualifiers");
20717 cp_lexer_purge_token (parser->lexer);
20719 else
20721 ref_qual = curr_ref_qual;
20722 cp_lexer_consume_token (parser->lexer);
20726 return ref_qual;
20729 /* Parse an optional tx-qualifier.
20731 tx-qualifier:
20732 transaction_safe
20733 transaction_safe_dynamic */
20735 static tree
20736 cp_parser_tx_qualifier_opt (cp_parser *parser)
20738 cp_token *token = cp_lexer_peek_token (parser->lexer);
20739 if (token->type == CPP_NAME)
20741 tree name = token->u.value;
20742 const char *p = IDENTIFIER_POINTER (name);
20743 const int len = strlen ("transaction_safe");
20744 if (!strncmp (p, "transaction_safe", len))
20746 p += len;
20747 if (*p == '\0'
20748 || !strcmp (p, "_dynamic"))
20750 cp_lexer_consume_token (parser->lexer);
20751 if (!flag_tm)
20753 error ("%qE requires %<-fgnu-tm%>", name);
20754 return NULL_TREE;
20756 else
20757 return name;
20761 return NULL_TREE;
20764 /* Parse an (optional) virt-specifier-seq.
20766 virt-specifier-seq:
20767 virt-specifier virt-specifier-seq [opt]
20769 virt-specifier:
20770 override
20771 final
20773 Returns a bitmask representing the virt-specifiers. */
20775 static cp_virt_specifiers
20776 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20778 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20780 while (true)
20782 cp_token *token;
20783 cp_virt_specifiers virt_specifier;
20785 /* Peek at the next token. */
20786 token = cp_lexer_peek_token (parser->lexer);
20787 /* See if it's a virt-specifier-qualifier. */
20788 if (token->type != CPP_NAME)
20789 break;
20790 if (id_equal (token->u.value, "override"))
20792 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20793 virt_specifier = VIRT_SPEC_OVERRIDE;
20795 else if (id_equal (token->u.value, "final"))
20797 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20798 virt_specifier = VIRT_SPEC_FINAL;
20800 else if (id_equal (token->u.value, "__final"))
20802 virt_specifier = VIRT_SPEC_FINAL;
20804 else
20805 break;
20807 if (virt_specifiers & virt_specifier)
20809 gcc_rich_location richloc (token->location);
20810 richloc.add_fixit_remove ();
20811 error_at (&richloc, "duplicate virt-specifier");
20812 cp_lexer_purge_token (parser->lexer);
20814 else
20816 cp_lexer_consume_token (parser->lexer);
20817 virt_specifiers |= virt_specifier;
20820 return virt_specifiers;
20823 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20824 is in scope even though it isn't real. */
20826 void
20827 inject_this_parameter (tree ctype, cp_cv_quals quals)
20829 tree this_parm;
20831 if (current_class_ptr)
20833 /* We don't clear this between NSDMIs. Is it already what we want? */
20834 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20835 if (DECL_P (current_class_ptr)
20836 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20837 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20838 && cp_type_quals (type) == quals)
20839 return;
20842 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20843 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20844 current_class_ptr = NULL_TREE;
20845 current_class_ref
20846 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20847 current_class_ptr = this_parm;
20850 /* Return true iff our current scope is a non-static data member
20851 initializer. */
20853 bool
20854 parsing_nsdmi (void)
20856 /* We recognize NSDMI context by the context-less 'this' pointer set up
20857 by the function above. */
20858 if (current_class_ptr
20859 && TREE_CODE (current_class_ptr) == PARM_DECL
20860 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20861 return true;
20862 return false;
20865 /* Parse a late-specified return type, if any. This is not a separate
20866 non-terminal, but part of a function declarator, which looks like
20868 -> trailing-type-specifier-seq abstract-declarator(opt)
20870 Returns the type indicated by the type-id.
20872 In addition to this, parse any queued up #pragma omp declare simd
20873 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20874 #pragma acc routine clauses.
20876 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20877 function. */
20879 static tree
20880 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20881 tree& requires_clause, cp_cv_quals quals)
20883 cp_token *token;
20884 tree type = NULL_TREE;
20885 bool declare_simd_p = (parser->omp_declare_simd
20886 && declarator
20887 && declarator->kind == cdk_id);
20889 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20890 && declarator && declarator->kind == cdk_id);
20892 bool oacc_routine_p = (parser->oacc_routine
20893 && declarator
20894 && declarator->kind == cdk_id);
20896 /* Peek at the next token. */
20897 token = cp_lexer_peek_token (parser->lexer);
20898 /* A late-specified return type is indicated by an initial '->'. */
20899 if (token->type != CPP_DEREF
20900 && token->keyword != RID_REQUIRES
20901 && !(token->type == CPP_NAME
20902 && token->u.value == ridpointers[RID_REQUIRES])
20903 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20904 return NULL_TREE;
20906 tree save_ccp = current_class_ptr;
20907 tree save_ccr = current_class_ref;
20908 if (quals >= 0)
20910 /* DR 1207: 'this' is in scope in the trailing return type. */
20911 inject_this_parameter (current_class_type, quals);
20914 if (token->type == CPP_DEREF)
20916 /* Consume the ->. */
20917 cp_lexer_consume_token (parser->lexer);
20919 type = cp_parser_trailing_type_id (parser);
20922 /* Function declarations may be followed by a trailing
20923 requires-clause. */
20924 requires_clause = cp_parser_requires_clause_opt (parser);
20926 if (cilk_simd_fn_vector_p)
20927 declarator->attributes
20928 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20929 declarator->attributes);
20930 if (declare_simd_p)
20931 declarator->attributes
20932 = cp_parser_late_parsing_omp_declare_simd (parser,
20933 declarator->attributes);
20934 if (oacc_routine_p)
20935 declarator->attributes
20936 = cp_parser_late_parsing_oacc_routine (parser,
20937 declarator->attributes);
20939 if (quals >= 0)
20941 current_class_ptr = save_ccp;
20942 current_class_ref = save_ccr;
20945 return type;
20948 /* Parse a declarator-id.
20950 declarator-id:
20951 id-expression
20952 :: [opt] nested-name-specifier [opt] type-name
20954 In the `id-expression' case, the value returned is as for
20955 cp_parser_id_expression if the id-expression was an unqualified-id.
20956 If the id-expression was a qualified-id, then a SCOPE_REF is
20957 returned. The first operand is the scope (either a NAMESPACE_DECL
20958 or TREE_TYPE), but the second is still just a representation of an
20959 unqualified-id. */
20961 static tree
20962 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20964 tree id;
20965 /* The expression must be an id-expression. Assume that qualified
20966 names are the names of types so that:
20968 template <class T>
20969 int S<T>::R::i = 3;
20971 will work; we must treat `S<T>::R' as the name of a type.
20972 Similarly, assume that qualified names are templates, where
20973 required, so that:
20975 template <class T>
20976 int S<T>::R<T>::i = 3;
20978 will work, too. */
20979 id = cp_parser_id_expression (parser,
20980 /*template_keyword_p=*/false,
20981 /*check_dependency_p=*/false,
20982 /*template_p=*/NULL,
20983 /*declarator_p=*/true,
20984 optional_p);
20985 if (id && BASELINK_P (id))
20986 id = BASELINK_FUNCTIONS (id);
20987 return id;
20990 /* Parse a type-id.
20992 type-id:
20993 type-specifier-seq abstract-declarator [opt]
20995 Returns the TYPE specified. */
20997 static tree
20998 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20999 bool is_trailing_return)
21001 cp_decl_specifier_seq type_specifier_seq;
21002 cp_declarator *abstract_declarator;
21004 /* Parse the type-specifier-seq. */
21005 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21006 is_trailing_return,
21007 &type_specifier_seq);
21008 if (is_template_arg && type_specifier_seq.type
21009 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21010 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21011 /* A bare template name as a template argument is a template template
21012 argument, not a placeholder, so fail parsing it as a type argument. */
21014 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21015 cp_parser_simulate_error (parser);
21016 return error_mark_node;
21018 if (type_specifier_seq.type == error_mark_node)
21019 return error_mark_node;
21021 /* There might or might not be an abstract declarator. */
21022 cp_parser_parse_tentatively (parser);
21023 /* Look for the declarator. */
21024 abstract_declarator
21025 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21026 /*parenthesized_p=*/NULL,
21027 /*member_p=*/false,
21028 /*friend_p=*/false);
21029 /* Check to see if there really was a declarator. */
21030 if (!cp_parser_parse_definitely (parser))
21031 abstract_declarator = NULL;
21033 if (type_specifier_seq.type
21034 /* The concepts TS allows 'auto' as a type-id. */
21035 && (!flag_concepts || parser->in_type_id_in_expr_p)
21036 /* None of the valid uses of 'auto' in C++14 involve the type-id
21037 nonterminal, but it is valid in a trailing-return-type. */
21038 && !(cxx_dialect >= cxx14 && is_trailing_return))
21039 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21041 /* A type-id with type 'auto' is only ok if the abstract declarator
21042 is a function declarator with a late-specified return type.
21044 A type-id with 'auto' is also valid in a trailing-return-type
21045 in a compound-requirement. */
21046 if (abstract_declarator
21047 && abstract_declarator->kind == cdk_function
21048 && abstract_declarator->u.function.late_return_type)
21049 /* OK */;
21050 else if (parser->in_result_type_constraint_p)
21051 /* OK */;
21052 else
21054 location_t loc = type_specifier_seq.locations[ds_type_spec];
21055 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21057 error_at (loc, "missing template arguments after %qT",
21058 auto_node);
21059 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21060 tmpl);
21062 else
21063 error_at (loc, "invalid use of %qT", auto_node);
21064 return error_mark_node;
21068 return groktypename (&type_specifier_seq, abstract_declarator,
21069 is_template_arg);
21072 static tree
21073 cp_parser_type_id (cp_parser *parser)
21075 return cp_parser_type_id_1 (parser, false, false);
21078 static tree
21079 cp_parser_template_type_arg (cp_parser *parser)
21081 tree r;
21082 const char *saved_message = parser->type_definition_forbidden_message;
21083 parser->type_definition_forbidden_message
21084 = G_("types may not be defined in template arguments");
21085 r = cp_parser_type_id_1 (parser, true, false);
21086 parser->type_definition_forbidden_message = saved_message;
21087 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21089 error ("invalid use of %<auto%> in template argument");
21090 r = error_mark_node;
21092 return r;
21095 static tree
21096 cp_parser_trailing_type_id (cp_parser *parser)
21098 return cp_parser_type_id_1 (parser, false, true);
21101 /* Parse a type-specifier-seq.
21103 type-specifier-seq:
21104 type-specifier type-specifier-seq [opt]
21106 GNU extension:
21108 type-specifier-seq:
21109 attributes type-specifier-seq [opt]
21111 If IS_DECLARATION is true, we are at the start of a "condition" or
21112 exception-declaration, so we might be followed by a declarator-id.
21114 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21115 i.e. we've just seen "->".
21117 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21119 static void
21120 cp_parser_type_specifier_seq (cp_parser* parser,
21121 bool is_declaration,
21122 bool is_trailing_return,
21123 cp_decl_specifier_seq *type_specifier_seq)
21125 bool seen_type_specifier = false;
21126 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21127 cp_token *start_token = NULL;
21129 /* Clear the TYPE_SPECIFIER_SEQ. */
21130 clear_decl_specs (type_specifier_seq);
21132 /* In the context of a trailing return type, enum E { } is an
21133 elaborated-type-specifier followed by a function-body, not an
21134 enum-specifier. */
21135 if (is_trailing_return)
21136 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21138 /* Parse the type-specifiers and attributes. */
21139 while (true)
21141 tree type_specifier;
21142 bool is_cv_qualifier;
21144 /* Check for attributes first. */
21145 if (cp_next_tokens_can_be_attribute_p (parser))
21147 type_specifier_seq->attributes =
21148 chainon (type_specifier_seq->attributes,
21149 cp_parser_attributes_opt (parser));
21150 continue;
21153 /* record the token of the beginning of the type specifier seq,
21154 for error reporting purposes*/
21155 if (!start_token)
21156 start_token = cp_lexer_peek_token (parser->lexer);
21158 /* Look for the type-specifier. */
21159 type_specifier = cp_parser_type_specifier (parser,
21160 flags,
21161 type_specifier_seq,
21162 /*is_declaration=*/false,
21163 NULL,
21164 &is_cv_qualifier);
21165 if (!type_specifier)
21167 /* If the first type-specifier could not be found, this is not a
21168 type-specifier-seq at all. */
21169 if (!seen_type_specifier)
21171 /* Set in_declarator_p to avoid skipping to the semicolon. */
21172 int in_decl = parser->in_declarator_p;
21173 parser->in_declarator_p = true;
21175 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21176 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21177 cp_parser_error (parser, "expected type-specifier");
21179 parser->in_declarator_p = in_decl;
21181 type_specifier_seq->type = error_mark_node;
21182 return;
21184 /* If subsequent type-specifiers could not be found, the
21185 type-specifier-seq is complete. */
21186 break;
21189 seen_type_specifier = true;
21190 /* The standard says that a condition can be:
21192 type-specifier-seq declarator = assignment-expression
21194 However, given:
21196 struct S {};
21197 if (int S = ...)
21199 we should treat the "S" as a declarator, not as a
21200 type-specifier. The standard doesn't say that explicitly for
21201 type-specifier-seq, but it does say that for
21202 decl-specifier-seq in an ordinary declaration. Perhaps it
21203 would be clearer just to allow a decl-specifier-seq here, and
21204 then add a semantic restriction that if any decl-specifiers
21205 that are not type-specifiers appear, the program is invalid. */
21206 if (is_declaration && !is_cv_qualifier)
21207 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21211 /* Return whether the function currently being declared has an associated
21212 template parameter list. */
21214 static bool
21215 function_being_declared_is_template_p (cp_parser* parser)
21217 if (!current_template_parms || processing_template_parmlist)
21218 return false;
21220 if (parser->implicit_template_scope)
21221 return true;
21223 if (at_class_scope_p ()
21224 && TYPE_BEING_DEFINED (current_class_type))
21225 return parser->num_template_parameter_lists != 0;
21227 return ((int) parser->num_template_parameter_lists > template_class_depth
21228 (current_class_type));
21231 /* Parse a parameter-declaration-clause.
21233 parameter-declaration-clause:
21234 parameter-declaration-list [opt] ... [opt]
21235 parameter-declaration-list , ...
21237 Returns a representation for the parameter declarations. A return
21238 value of NULL indicates a parameter-declaration-clause consisting
21239 only of an ellipsis. */
21241 static tree
21242 cp_parser_parameter_declaration_clause (cp_parser* parser)
21244 tree parameters;
21245 cp_token *token;
21246 bool ellipsis_p;
21247 bool is_error;
21249 struct cleanup {
21250 cp_parser* parser;
21251 int auto_is_implicit_function_template_parm_p;
21252 ~cleanup() {
21253 parser->auto_is_implicit_function_template_parm_p
21254 = auto_is_implicit_function_template_parm_p;
21256 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21258 (void) cleanup;
21260 if (!processing_specialization
21261 && !processing_template_parmlist
21262 && !processing_explicit_instantiation)
21263 if (!current_function_decl
21264 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21265 parser->auto_is_implicit_function_template_parm_p = true;
21267 /* Peek at the next token. */
21268 token = cp_lexer_peek_token (parser->lexer);
21269 /* Check for trivial parameter-declaration-clauses. */
21270 if (token->type == CPP_ELLIPSIS)
21272 /* Consume the `...' token. */
21273 cp_lexer_consume_token (parser->lexer);
21274 return NULL_TREE;
21276 else if (token->type == CPP_CLOSE_PAREN)
21277 /* There are no parameters. */
21279 #ifndef NO_IMPLICIT_EXTERN_C
21280 if (in_system_header_at (input_location)
21281 && current_class_type == NULL
21282 && current_lang_name == lang_name_c)
21283 return NULL_TREE;
21284 else
21285 #endif
21286 return void_list_node;
21288 /* Check for `(void)', too, which is a special case. */
21289 else if (token->keyword == RID_VOID
21290 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21291 == CPP_CLOSE_PAREN))
21293 /* Consume the `void' token. */
21294 cp_lexer_consume_token (parser->lexer);
21295 /* There are no parameters. */
21296 return void_list_node;
21299 /* Parse the parameter-declaration-list. */
21300 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21301 /* If a parse error occurred while parsing the
21302 parameter-declaration-list, then the entire
21303 parameter-declaration-clause is erroneous. */
21304 if (is_error)
21305 return NULL;
21307 /* Peek at the next token. */
21308 token = cp_lexer_peek_token (parser->lexer);
21309 /* If it's a `,', the clause should terminate with an ellipsis. */
21310 if (token->type == CPP_COMMA)
21312 /* Consume the `,'. */
21313 cp_lexer_consume_token (parser->lexer);
21314 /* Expect an ellipsis. */
21315 ellipsis_p
21316 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21318 /* It might also be `...' if the optional trailing `,' was
21319 omitted. */
21320 else if (token->type == CPP_ELLIPSIS)
21322 /* Consume the `...' token. */
21323 cp_lexer_consume_token (parser->lexer);
21324 /* And remember that we saw it. */
21325 ellipsis_p = true;
21327 else
21328 ellipsis_p = false;
21330 /* Finish the parameter list. */
21331 if (!ellipsis_p)
21332 parameters = chainon (parameters, void_list_node);
21334 return parameters;
21337 /* Parse a parameter-declaration-list.
21339 parameter-declaration-list:
21340 parameter-declaration
21341 parameter-declaration-list , parameter-declaration
21343 Returns a representation of the parameter-declaration-list, as for
21344 cp_parser_parameter_declaration_clause. However, the
21345 `void_list_node' is never appended to the list. Upon return,
21346 *IS_ERROR will be true iff an error occurred. */
21348 static tree
21349 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21351 tree parameters = NULL_TREE;
21352 tree *tail = &parameters;
21353 bool saved_in_unbraced_linkage_specification_p;
21354 int index = 0;
21356 /* Assume all will go well. */
21357 *is_error = false;
21358 /* The special considerations that apply to a function within an
21359 unbraced linkage specifications do not apply to the parameters
21360 to the function. */
21361 saved_in_unbraced_linkage_specification_p
21362 = parser->in_unbraced_linkage_specification_p;
21363 parser->in_unbraced_linkage_specification_p = false;
21365 /* Look for more parameters. */
21366 while (true)
21368 cp_parameter_declarator *parameter;
21369 tree decl = error_mark_node;
21370 bool parenthesized_p = false;
21371 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21372 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21373 (current_template_parms)) : 0);
21375 /* Parse the parameter. */
21376 parameter
21377 = cp_parser_parameter_declaration (parser,
21378 /*template_parm_p=*/false,
21379 &parenthesized_p);
21381 /* We don't know yet if the enclosing context is deprecated, so wait
21382 and warn in grokparms if appropriate. */
21383 deprecated_state = DEPRECATED_SUPPRESS;
21385 if (parameter)
21387 /* If a function parameter pack was specified and an implicit template
21388 parameter was introduced during cp_parser_parameter_declaration,
21389 change any implicit parameters introduced into packs. */
21390 if (parser->implicit_template_parms
21391 && parameter->declarator
21392 && parameter->declarator->parameter_pack_p)
21394 int latest_template_parm_idx = TREE_VEC_LENGTH
21395 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21397 if (latest_template_parm_idx != template_parm_idx)
21398 parameter->decl_specifiers.type = convert_generic_types_to_packs
21399 (parameter->decl_specifiers.type,
21400 template_parm_idx, latest_template_parm_idx);
21403 decl = grokdeclarator (parameter->declarator,
21404 &parameter->decl_specifiers,
21405 PARM,
21406 parameter->default_argument != NULL_TREE,
21407 &parameter->decl_specifiers.attributes);
21408 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21409 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21412 deprecated_state = DEPRECATED_NORMAL;
21414 /* If a parse error occurred parsing the parameter declaration,
21415 then the entire parameter-declaration-list is erroneous. */
21416 if (decl == error_mark_node)
21418 *is_error = true;
21419 parameters = error_mark_node;
21420 break;
21423 if (parameter->decl_specifiers.attributes)
21424 cplus_decl_attributes (&decl,
21425 parameter->decl_specifiers.attributes,
21427 if (DECL_NAME (decl))
21428 decl = pushdecl (decl);
21430 if (decl != error_mark_node)
21432 retrofit_lang_decl (decl);
21433 DECL_PARM_INDEX (decl) = ++index;
21434 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21437 /* Add the new parameter to the list. */
21438 *tail = build_tree_list (parameter->default_argument, decl);
21439 tail = &TREE_CHAIN (*tail);
21441 /* Peek at the next token. */
21442 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21443 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21444 /* These are for Objective-C++ */
21445 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21446 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21447 /* The parameter-declaration-list is complete. */
21448 break;
21449 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21451 cp_token *token;
21453 /* Peek at the next token. */
21454 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21455 /* If it's an ellipsis, then the list is complete. */
21456 if (token->type == CPP_ELLIPSIS)
21457 break;
21458 /* Otherwise, there must be more parameters. Consume the
21459 `,'. */
21460 cp_lexer_consume_token (parser->lexer);
21461 /* When parsing something like:
21463 int i(float f, double d)
21465 we can tell after seeing the declaration for "f" that we
21466 are not looking at an initialization of a variable "i",
21467 but rather at the declaration of a function "i".
21469 Due to the fact that the parsing of template arguments
21470 (as specified to a template-id) requires backtracking we
21471 cannot use this technique when inside a template argument
21472 list. */
21473 if (!parser->in_template_argument_list_p
21474 && !parser->in_type_id_in_expr_p
21475 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21476 /* However, a parameter-declaration of the form
21477 "float(f)" (which is a valid declaration of a
21478 parameter "f") can also be interpreted as an
21479 expression (the conversion of "f" to "float"). */
21480 && !parenthesized_p)
21481 cp_parser_commit_to_tentative_parse (parser);
21483 else
21485 cp_parser_error (parser, "expected %<,%> or %<...%>");
21486 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21487 cp_parser_skip_to_closing_parenthesis (parser,
21488 /*recovering=*/true,
21489 /*or_comma=*/false,
21490 /*consume_paren=*/false);
21491 break;
21495 parser->in_unbraced_linkage_specification_p
21496 = saved_in_unbraced_linkage_specification_p;
21498 /* Reset implicit_template_scope if we are about to leave the function
21499 parameter list that introduced it. Note that for out-of-line member
21500 definitions, there will be one or more class scopes before we get to
21501 the template parameter scope. */
21503 if (cp_binding_level *its = parser->implicit_template_scope)
21504 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21506 while (maybe_its->kind == sk_class)
21507 maybe_its = maybe_its->level_chain;
21508 if (maybe_its == its)
21510 parser->implicit_template_parms = 0;
21511 parser->implicit_template_scope = 0;
21515 return parameters;
21518 /* Parse a parameter declaration.
21520 parameter-declaration:
21521 decl-specifier-seq ... [opt] declarator
21522 decl-specifier-seq declarator = assignment-expression
21523 decl-specifier-seq ... [opt] abstract-declarator [opt]
21524 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21526 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21527 declares a template parameter. (In that case, a non-nested `>'
21528 token encountered during the parsing of the assignment-expression
21529 is not interpreted as a greater-than operator.)
21531 Returns a representation of the parameter, or NULL if an error
21532 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21533 true iff the declarator is of the form "(p)". */
21535 static cp_parameter_declarator *
21536 cp_parser_parameter_declaration (cp_parser *parser,
21537 bool template_parm_p,
21538 bool *parenthesized_p)
21540 int declares_class_or_enum;
21541 cp_decl_specifier_seq decl_specifiers;
21542 cp_declarator *declarator;
21543 tree default_argument;
21544 cp_token *token = NULL, *declarator_token_start = NULL;
21545 const char *saved_message;
21546 bool template_parameter_pack_p = false;
21548 /* In a template parameter, `>' is not an operator.
21550 [temp.param]
21552 When parsing a default template-argument for a non-type
21553 template-parameter, the first non-nested `>' is taken as the end
21554 of the template parameter-list rather than a greater-than
21555 operator. */
21557 /* Type definitions may not appear in parameter types. */
21558 saved_message = parser->type_definition_forbidden_message;
21559 parser->type_definition_forbidden_message
21560 = G_("types may not be defined in parameter types");
21562 /* Parse the declaration-specifiers. */
21563 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21564 cp_parser_decl_specifier_seq (parser,
21565 CP_PARSER_FLAGS_NONE,
21566 &decl_specifiers,
21567 &declares_class_or_enum);
21569 /* Complain about missing 'typename' or other invalid type names. */
21570 if (!decl_specifiers.any_type_specifiers_p
21571 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21572 decl_specifiers.type = error_mark_node;
21574 /* If an error occurred, there's no reason to attempt to parse the
21575 rest of the declaration. */
21576 if (cp_parser_error_occurred (parser))
21578 parser->type_definition_forbidden_message = saved_message;
21579 return NULL;
21582 /* Peek at the next token. */
21583 token = cp_lexer_peek_token (parser->lexer);
21585 /* If the next token is a `)', `,', `=', `>', or `...', then there
21586 is no declarator. However, when variadic templates are enabled,
21587 there may be a declarator following `...'. */
21588 if (token->type == CPP_CLOSE_PAREN
21589 || token->type == CPP_COMMA
21590 || token->type == CPP_EQ
21591 || token->type == CPP_GREATER)
21593 declarator = NULL;
21594 if (parenthesized_p)
21595 *parenthesized_p = false;
21597 /* Otherwise, there should be a declarator. */
21598 else
21600 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21601 parser->default_arg_ok_p = false;
21603 /* After seeing a decl-specifier-seq, if the next token is not a
21604 "(", there is no possibility that the code is a valid
21605 expression. Therefore, if parsing tentatively, we commit at
21606 this point. */
21607 if (!parser->in_template_argument_list_p
21608 /* In an expression context, having seen:
21610 (int((char ...
21612 we cannot be sure whether we are looking at a
21613 function-type (taking a "char" as a parameter) or a cast
21614 of some object of type "char" to "int". */
21615 && !parser->in_type_id_in_expr_p
21616 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21617 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21618 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21619 cp_parser_commit_to_tentative_parse (parser);
21620 /* Parse the declarator. */
21621 declarator_token_start = token;
21622 declarator = cp_parser_declarator (parser,
21623 CP_PARSER_DECLARATOR_EITHER,
21624 /*ctor_dtor_or_conv_p=*/NULL,
21625 parenthesized_p,
21626 /*member_p=*/false,
21627 /*friend_p=*/false);
21628 parser->default_arg_ok_p = saved_default_arg_ok_p;
21629 /* After the declarator, allow more attributes. */
21630 decl_specifiers.attributes
21631 = chainon (decl_specifiers.attributes,
21632 cp_parser_attributes_opt (parser));
21634 /* If the declarator is a template parameter pack, remember that and
21635 clear the flag in the declarator itself so we don't get errors
21636 from grokdeclarator. */
21637 if (template_parm_p && declarator && declarator->parameter_pack_p)
21639 declarator->parameter_pack_p = false;
21640 template_parameter_pack_p = true;
21644 /* If the next token is an ellipsis, and we have not seen a declarator
21645 name, and if either the type of the declarator contains parameter
21646 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21647 for, eg, abbreviated integral type names), then we actually have a
21648 parameter pack expansion expression. Otherwise, leave the ellipsis
21649 for a C-style variadic function. */
21650 token = cp_lexer_peek_token (parser->lexer);
21651 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21653 tree type = decl_specifiers.type;
21655 if (type && DECL_P (type))
21656 type = TREE_TYPE (type);
21658 if (((type
21659 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21660 && (template_parm_p || uses_parameter_packs (type)))
21661 || (!type && template_parm_p))
21662 && declarator_can_be_parameter_pack (declarator))
21664 /* Consume the `...'. */
21665 cp_lexer_consume_token (parser->lexer);
21666 maybe_warn_variadic_templates ();
21668 /* Build a pack expansion type */
21669 if (template_parm_p)
21670 template_parameter_pack_p = true;
21671 else if (declarator)
21672 declarator->parameter_pack_p = true;
21673 else
21674 decl_specifiers.type = make_pack_expansion (type);
21678 /* The restriction on defining new types applies only to the type
21679 of the parameter, not to the default argument. */
21680 parser->type_definition_forbidden_message = saved_message;
21682 /* If the next token is `=', then process a default argument. */
21683 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21685 tree type = decl_specifiers.type;
21686 token = cp_lexer_peek_token (parser->lexer);
21687 /* If we are defining a class, then the tokens that make up the
21688 default argument must be saved and processed later. */
21689 if (!template_parm_p && at_class_scope_p ()
21690 && TYPE_BEING_DEFINED (current_class_type)
21691 && !LAMBDA_TYPE_P (current_class_type))
21692 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21694 // A constrained-type-specifier may declare a type template-parameter.
21695 else if (declares_constrained_type_template_parameter (type))
21696 default_argument
21697 = cp_parser_default_type_template_argument (parser);
21699 // A constrained-type-specifier may declare a template-template-parameter.
21700 else if (declares_constrained_template_template_parameter (type))
21701 default_argument
21702 = cp_parser_default_template_template_argument (parser);
21704 /* Outside of a class definition, we can just parse the
21705 assignment-expression. */
21706 else
21707 default_argument
21708 = cp_parser_default_argument (parser, template_parm_p);
21710 if (!parser->default_arg_ok_p)
21712 permerror (token->location,
21713 "default arguments are only "
21714 "permitted for function parameters");
21716 else if ((declarator && declarator->parameter_pack_p)
21717 || template_parameter_pack_p
21718 || (decl_specifiers.type
21719 && PACK_EXPANSION_P (decl_specifiers.type)))
21721 /* Find the name of the parameter pack. */
21722 cp_declarator *id_declarator = declarator;
21723 while (id_declarator && id_declarator->kind != cdk_id)
21724 id_declarator = id_declarator->declarator;
21726 if (id_declarator && id_declarator->kind == cdk_id)
21727 error_at (declarator_token_start->location,
21728 template_parm_p
21729 ? G_("template parameter pack %qD "
21730 "cannot have a default argument")
21731 : G_("parameter pack %qD cannot have "
21732 "a default argument"),
21733 id_declarator->u.id.unqualified_name);
21734 else
21735 error_at (declarator_token_start->location,
21736 template_parm_p
21737 ? G_("template parameter pack cannot have "
21738 "a default argument")
21739 : G_("parameter pack cannot have a "
21740 "default argument"));
21742 default_argument = NULL_TREE;
21745 else
21746 default_argument = NULL_TREE;
21748 /* Generate a location for the parameter, ranging from the start of the
21749 initial token to the end of the final token (using input_location for
21750 the latter, set up by cp_lexer_set_source_position_from_token when
21751 consuming tokens).
21753 If we have a identifier, then use it for the caret location, e.g.
21755 extern int callee (int one, int (*two)(int, int), float three);
21756 ~~~~~~^~~~~~~~~~~~~~
21758 otherwise, reuse the start location for the caret location e.g.:
21760 extern int callee (int one, int (*)(int, int), float three);
21761 ^~~~~~~~~~~~~~~~~
21764 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21765 ? declarator->id_loc
21766 : decl_spec_token_start->location);
21767 location_t param_loc = make_location (caret_loc,
21768 decl_spec_token_start->location,
21769 input_location);
21771 return make_parameter_declarator (&decl_specifiers,
21772 declarator,
21773 default_argument,
21774 param_loc,
21775 template_parameter_pack_p);
21778 /* Parse a default argument and return it.
21780 TEMPLATE_PARM_P is true if this is a default argument for a
21781 non-type template parameter. */
21782 static tree
21783 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21785 tree default_argument = NULL_TREE;
21786 bool saved_greater_than_is_operator_p;
21787 bool saved_local_variables_forbidden_p;
21788 bool non_constant_p, is_direct_init;
21790 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21791 set correctly. */
21792 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21793 parser->greater_than_is_operator_p = !template_parm_p;
21794 /* Local variable names (and the `this' keyword) may not
21795 appear in a default argument. */
21796 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21797 parser->local_variables_forbidden_p = true;
21798 /* Parse the assignment-expression. */
21799 if (template_parm_p)
21800 push_deferring_access_checks (dk_no_deferred);
21801 tree saved_class_ptr = NULL_TREE;
21802 tree saved_class_ref = NULL_TREE;
21803 /* The "this" pointer is not valid in a default argument. */
21804 if (cfun)
21806 saved_class_ptr = current_class_ptr;
21807 cp_function_chain->x_current_class_ptr = NULL_TREE;
21808 saved_class_ref = current_class_ref;
21809 cp_function_chain->x_current_class_ref = NULL_TREE;
21811 default_argument
21812 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21813 /* Restore the "this" pointer. */
21814 if (cfun)
21816 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21817 cp_function_chain->x_current_class_ref = saved_class_ref;
21819 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21820 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21821 if (template_parm_p)
21822 pop_deferring_access_checks ();
21823 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21824 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21826 return default_argument;
21829 /* Parse a function-body.
21831 function-body:
21832 compound_statement */
21834 static void
21835 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21837 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21838 ? BCS_TRY_BLOCK : BCS_NORMAL),
21839 true);
21842 /* Parse a ctor-initializer-opt followed by a function-body. Return
21843 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21844 is true we are parsing a function-try-block. */
21846 static void
21847 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21848 bool in_function_try_block)
21850 tree body, list;
21851 const bool check_body_p =
21852 DECL_CONSTRUCTOR_P (current_function_decl)
21853 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21854 tree last = NULL;
21856 /* Begin the function body. */
21857 body = begin_function_body ();
21858 /* Parse the optional ctor-initializer. */
21859 cp_parser_ctor_initializer_opt (parser);
21861 /* If we're parsing a constexpr constructor definition, we need
21862 to check that the constructor body is indeed empty. However,
21863 before we get to cp_parser_function_body lot of junk has been
21864 generated, so we can't just check that we have an empty block.
21865 Rather we take a snapshot of the outermost block, and check whether
21866 cp_parser_function_body changed its state. */
21867 if (check_body_p)
21869 list = cur_stmt_list;
21870 if (STATEMENT_LIST_TAIL (list))
21871 last = STATEMENT_LIST_TAIL (list)->stmt;
21873 /* Parse the function-body. */
21874 cp_parser_function_body (parser, in_function_try_block);
21875 if (check_body_p)
21876 check_constexpr_ctor_body (last, list, /*complain=*/true);
21877 /* Finish the function body. */
21878 finish_function_body (body);
21881 /* Parse an initializer.
21883 initializer:
21884 = initializer-clause
21885 ( expression-list )
21887 Returns an expression representing the initializer. If no
21888 initializer is present, NULL_TREE is returned.
21890 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21891 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21892 set to TRUE if there is no initializer present. If there is an
21893 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21894 is set to true; otherwise it is set to false. */
21896 static tree
21897 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21898 bool* non_constant_p)
21900 cp_token *token;
21901 tree init;
21903 /* Peek at the next token. */
21904 token = cp_lexer_peek_token (parser->lexer);
21906 /* Let our caller know whether or not this initializer was
21907 parenthesized. */
21908 *is_direct_init = (token->type != CPP_EQ);
21909 /* Assume that the initializer is constant. */
21910 *non_constant_p = false;
21912 if (token->type == CPP_EQ)
21914 /* Consume the `='. */
21915 cp_lexer_consume_token (parser->lexer);
21916 /* Parse the initializer-clause. */
21917 init = cp_parser_initializer_clause (parser, non_constant_p);
21919 else if (token->type == CPP_OPEN_PAREN)
21921 vec<tree, va_gc> *vec;
21922 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21923 /*cast_p=*/false,
21924 /*allow_expansion_p=*/true,
21925 non_constant_p);
21926 if (vec == NULL)
21927 return error_mark_node;
21928 init = build_tree_list_vec (vec);
21929 release_tree_vector (vec);
21931 else if (token->type == CPP_OPEN_BRACE)
21933 cp_lexer_set_source_position (parser->lexer);
21934 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21935 init = cp_parser_braced_list (parser, non_constant_p);
21936 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21938 else
21940 /* Anything else is an error. */
21941 cp_parser_error (parser, "expected initializer");
21942 init = error_mark_node;
21945 if (check_for_bare_parameter_packs (init))
21946 init = error_mark_node;
21948 return init;
21951 /* Parse an initializer-clause.
21953 initializer-clause:
21954 assignment-expression
21955 braced-init-list
21957 Returns an expression representing the initializer.
21959 If the `assignment-expression' production is used the value
21960 returned is simply a representation for the expression.
21962 Otherwise, calls cp_parser_braced_list. */
21964 static cp_expr
21965 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21967 cp_expr initializer;
21969 /* Assume the expression is constant. */
21970 *non_constant_p = false;
21972 /* If it is not a `{', then we are looking at an
21973 assignment-expression. */
21974 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21976 initializer
21977 = cp_parser_constant_expression (parser,
21978 /*allow_non_constant_p=*/true,
21979 non_constant_p);
21981 else
21982 initializer = cp_parser_braced_list (parser, non_constant_p);
21984 return initializer;
21987 /* Parse a brace-enclosed initializer list.
21989 braced-init-list:
21990 { initializer-list , [opt] }
21993 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21994 the elements of the initializer-list (or NULL, if the last
21995 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21996 NULL_TREE. There is no way to detect whether or not the optional
21997 trailing `,' was provided. NON_CONSTANT_P is as for
21998 cp_parser_initializer. */
22000 static cp_expr
22001 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22003 tree initializer;
22004 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22006 /* Consume the `{' token. */
22007 matching_braces braces;
22008 braces.consume_open (parser);
22009 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22010 initializer = make_node (CONSTRUCTOR);
22011 /* If it's not a `}', then there is a non-trivial initializer. */
22012 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22014 /* Parse the initializer list. */
22015 CONSTRUCTOR_ELTS (initializer)
22016 = cp_parser_initializer_list (parser, non_constant_p);
22017 /* A trailing `,' token is allowed. */
22018 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22019 cp_lexer_consume_token (parser->lexer);
22021 else
22022 *non_constant_p = false;
22023 /* Now, there should be a trailing `}'. */
22024 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22025 braces.require_close (parser);
22026 TREE_TYPE (initializer) = init_list_type_node;
22028 cp_expr result (initializer);
22029 /* Build a location of the form:
22030 { ... }
22031 ^~~~~~~
22032 with caret==start at the open brace, finish at the close brace. */
22033 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22034 result.set_location (combined_loc);
22035 return result;
22038 /* Consume tokens up to, and including, the next non-nested closing `]'.
22039 Returns true iff we found a closing `]'. */
22041 static bool
22042 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22044 unsigned square_depth = 0;
22046 while (true)
22048 cp_token * token = cp_lexer_peek_token (parser->lexer);
22050 switch (token->type)
22052 case CPP_EOF:
22053 case CPP_PRAGMA_EOL:
22054 /* If we've run out of tokens, then there is no closing `]'. */
22055 return false;
22057 case CPP_OPEN_SQUARE:
22058 ++square_depth;
22059 break;
22061 case CPP_CLOSE_SQUARE:
22062 if (!square_depth--)
22064 cp_lexer_consume_token (parser->lexer);
22065 return true;
22067 break;
22069 default:
22070 break;
22073 /* Consume the token. */
22074 cp_lexer_consume_token (parser->lexer);
22078 /* Return true if we are looking at an array-designator, false otherwise. */
22080 static bool
22081 cp_parser_array_designator_p (cp_parser *parser)
22083 /* Consume the `['. */
22084 cp_lexer_consume_token (parser->lexer);
22086 cp_lexer_save_tokens (parser->lexer);
22088 /* Skip tokens until the next token is a closing square bracket.
22089 If we find the closing `]', and the next token is a `=', then
22090 we are looking at an array designator. */
22091 bool array_designator_p
22092 = (cp_parser_skip_to_closing_square_bracket (parser)
22093 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22095 /* Roll back the tokens we skipped. */
22096 cp_lexer_rollback_tokens (parser->lexer);
22098 return array_designator_p;
22101 /* Parse an initializer-list.
22103 initializer-list:
22104 initializer-clause ... [opt]
22105 initializer-list , initializer-clause ... [opt]
22107 GNU Extension:
22109 initializer-list:
22110 designation initializer-clause ...[opt]
22111 initializer-list , designation initializer-clause ...[opt]
22113 designation:
22114 . identifier =
22115 identifier :
22116 [ constant-expression ] =
22118 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22119 for the initializer. If the INDEX of the elt is non-NULL, it is the
22120 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22121 as for cp_parser_initializer. */
22123 static vec<constructor_elt, va_gc> *
22124 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22126 vec<constructor_elt, va_gc> *v = NULL;
22128 /* Assume all of the expressions are constant. */
22129 *non_constant_p = false;
22131 /* Parse the rest of the list. */
22132 while (true)
22134 cp_token *token;
22135 tree designator;
22136 tree initializer;
22137 bool clause_non_constant_p;
22139 /* If the next token is an identifier and the following one is a
22140 colon, we are looking at the GNU designated-initializer
22141 syntax. */
22142 if (cp_parser_allow_gnu_extensions_p (parser)
22143 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22144 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
22146 /* Warn the user that they are using an extension. */
22147 pedwarn (input_location, OPT_Wpedantic,
22148 "ISO C++ does not allow designated initializers");
22149 /* Consume the identifier. */
22150 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22151 /* Consume the `:'. */
22152 cp_lexer_consume_token (parser->lexer);
22154 /* Also handle the C99 syntax, '. id ='. */
22155 else if (cp_parser_allow_gnu_extensions_p (parser)
22156 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22157 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22158 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
22160 /* Warn the user that they are using an extension. */
22161 pedwarn (input_location, OPT_Wpedantic,
22162 "ISO C++ does not allow C99 designated initializers");
22163 /* Consume the `.'. */
22164 cp_lexer_consume_token (parser->lexer);
22165 /* Consume the identifier. */
22166 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22167 /* Consume the `='. */
22168 cp_lexer_consume_token (parser->lexer);
22170 /* Also handle C99 array designators, '[ const ] ='. */
22171 else if (cp_parser_allow_gnu_extensions_p (parser)
22172 && !c_dialect_objc ()
22173 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22175 /* In C++11, [ could start a lambda-introducer. */
22176 bool non_const = false;
22178 cp_parser_parse_tentatively (parser);
22180 if (!cp_parser_array_designator_p (parser))
22182 cp_parser_simulate_error (parser);
22183 designator = NULL_TREE;
22185 else
22187 designator = cp_parser_constant_expression (parser, true,
22188 &non_const);
22189 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22190 cp_parser_require (parser, CPP_EQ, RT_EQ);
22193 if (!cp_parser_parse_definitely (parser))
22194 designator = NULL_TREE;
22195 else if (non_const)
22196 require_potential_rvalue_constant_expression (designator);
22198 else
22199 designator = NULL_TREE;
22201 /* Parse the initializer. */
22202 initializer = cp_parser_initializer_clause (parser,
22203 &clause_non_constant_p);
22204 /* If any clause is non-constant, so is the entire initializer. */
22205 if (clause_non_constant_p)
22206 *non_constant_p = true;
22208 /* If we have an ellipsis, this is an initializer pack
22209 expansion. */
22210 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22212 /* Consume the `...'. */
22213 cp_lexer_consume_token (parser->lexer);
22215 /* Turn the initializer into an initializer expansion. */
22216 initializer = make_pack_expansion (initializer);
22219 /* Add it to the vector. */
22220 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22222 /* If the next token is not a comma, we have reached the end of
22223 the list. */
22224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22225 break;
22227 /* Peek at the next token. */
22228 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22229 /* If the next token is a `}', then we're still done. An
22230 initializer-clause can have a trailing `,' after the
22231 initializer-list and before the closing `}'. */
22232 if (token->type == CPP_CLOSE_BRACE)
22233 break;
22235 /* Consume the `,' token. */
22236 cp_lexer_consume_token (parser->lexer);
22239 return v;
22242 /* Classes [gram.class] */
22244 /* Parse a class-name.
22246 class-name:
22247 identifier
22248 template-id
22250 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22251 to indicate that names looked up in dependent types should be
22252 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22253 keyword has been used to indicate that the name that appears next
22254 is a template. TAG_TYPE indicates the explicit tag given before
22255 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22256 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22257 is the class being defined in a class-head. If ENUM_OK is TRUE,
22258 enum-names are also accepted.
22260 Returns the TYPE_DECL representing the class. */
22262 static tree
22263 cp_parser_class_name (cp_parser *parser,
22264 bool typename_keyword_p,
22265 bool template_keyword_p,
22266 enum tag_types tag_type,
22267 bool check_dependency_p,
22268 bool class_head_p,
22269 bool is_declaration,
22270 bool enum_ok)
22272 tree decl;
22273 tree scope;
22274 bool typename_p;
22275 cp_token *token;
22276 tree identifier = NULL_TREE;
22278 /* All class-names start with an identifier. */
22279 token = cp_lexer_peek_token (parser->lexer);
22280 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22282 cp_parser_error (parser, "expected class-name");
22283 return error_mark_node;
22286 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22287 to a template-id, so we save it here. */
22288 scope = parser->scope;
22289 if (scope == error_mark_node)
22290 return error_mark_node;
22292 /* Any name names a type if we're following the `typename' keyword
22293 in a qualified name where the enclosing scope is type-dependent. */
22294 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22295 && dependent_type_p (scope));
22296 /* Handle the common case (an identifier, but not a template-id)
22297 efficiently. */
22298 if (token->type == CPP_NAME
22299 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22301 cp_token *identifier_token;
22302 bool ambiguous_p;
22304 /* Look for the identifier. */
22305 identifier_token = cp_lexer_peek_token (parser->lexer);
22306 ambiguous_p = identifier_token->error_reported;
22307 identifier = cp_parser_identifier (parser);
22308 /* If the next token isn't an identifier, we are certainly not
22309 looking at a class-name. */
22310 if (identifier == error_mark_node)
22311 decl = error_mark_node;
22312 /* If we know this is a type-name, there's no need to look it
22313 up. */
22314 else if (typename_p)
22315 decl = identifier;
22316 else
22318 tree ambiguous_decls;
22319 /* If we already know that this lookup is ambiguous, then
22320 we've already issued an error message; there's no reason
22321 to check again. */
22322 if (ambiguous_p)
22324 cp_parser_simulate_error (parser);
22325 return error_mark_node;
22327 /* If the next token is a `::', then the name must be a type
22328 name.
22330 [basic.lookup.qual]
22332 During the lookup for a name preceding the :: scope
22333 resolution operator, object, function, and enumerator
22334 names are ignored. */
22335 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22336 tag_type = scope_type;
22337 /* Look up the name. */
22338 decl = cp_parser_lookup_name (parser, identifier,
22339 tag_type,
22340 /*is_template=*/false,
22341 /*is_namespace=*/false,
22342 check_dependency_p,
22343 &ambiguous_decls,
22344 identifier_token->location);
22345 if (ambiguous_decls)
22347 if (cp_parser_parsing_tentatively (parser))
22348 cp_parser_simulate_error (parser);
22349 return error_mark_node;
22353 else
22355 /* Try a template-id. */
22356 decl = cp_parser_template_id (parser, template_keyword_p,
22357 check_dependency_p,
22358 tag_type,
22359 is_declaration);
22360 if (decl == error_mark_node)
22361 return error_mark_node;
22364 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22366 /* If this is a typename, create a TYPENAME_TYPE. */
22367 if (typename_p && decl != error_mark_node)
22369 decl = make_typename_type (scope, decl, typename_type,
22370 /*complain=*/tf_error);
22371 if (decl != error_mark_node)
22372 decl = TYPE_NAME (decl);
22375 decl = strip_using_decl (decl);
22377 /* Check to see that it is really the name of a class. */
22378 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22379 && identifier_p (TREE_OPERAND (decl, 0))
22380 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22381 /* Situations like this:
22383 template <typename T> struct A {
22384 typename T::template X<int>::I i;
22387 are problematic. Is `T::template X<int>' a class-name? The
22388 standard does not seem to be definitive, but there is no other
22389 valid interpretation of the following `::'. Therefore, those
22390 names are considered class-names. */
22392 decl = make_typename_type (scope, decl, tag_type, tf_error);
22393 if (decl != error_mark_node)
22394 decl = TYPE_NAME (decl);
22396 else if (TREE_CODE (decl) != TYPE_DECL
22397 || TREE_TYPE (decl) == error_mark_node
22398 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22399 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22400 /* In Objective-C 2.0, a classname followed by '.' starts a
22401 dot-syntax expression, and it's not a type-name. */
22402 || (c_dialect_objc ()
22403 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22404 && objc_is_class_name (decl)))
22405 decl = error_mark_node;
22407 if (decl == error_mark_node)
22408 cp_parser_error (parser, "expected class-name");
22409 else if (identifier && !parser->scope)
22410 maybe_note_name_used_in_class (identifier, decl);
22412 return decl;
22415 /* Parse a class-specifier.
22417 class-specifier:
22418 class-head { member-specification [opt] }
22420 Returns the TREE_TYPE representing the class. */
22422 static tree
22423 cp_parser_class_specifier_1 (cp_parser* parser)
22425 tree type;
22426 tree attributes = NULL_TREE;
22427 bool nested_name_specifier_p;
22428 unsigned saved_num_template_parameter_lists;
22429 bool saved_in_function_body;
22430 unsigned char in_statement;
22431 bool in_switch_statement_p;
22432 bool saved_in_unbraced_linkage_specification_p;
22433 tree old_scope = NULL_TREE;
22434 tree scope = NULL_TREE;
22435 cp_token *closing_brace;
22437 push_deferring_access_checks (dk_no_deferred);
22439 /* Parse the class-head. */
22440 type = cp_parser_class_head (parser,
22441 &nested_name_specifier_p);
22442 /* If the class-head was a semantic disaster, skip the entire body
22443 of the class. */
22444 if (!type)
22446 cp_parser_skip_to_end_of_block_or_statement (parser);
22447 pop_deferring_access_checks ();
22448 return error_mark_node;
22451 /* Look for the `{'. */
22452 matching_braces braces;
22453 if (!braces.require_open (parser))
22455 pop_deferring_access_checks ();
22456 return error_mark_node;
22459 cp_ensure_no_omp_declare_simd (parser);
22460 cp_ensure_no_oacc_routine (parser);
22462 /* Issue an error message if type-definitions are forbidden here. */
22463 cp_parser_check_type_definition (parser);
22464 /* Remember that we are defining one more class. */
22465 ++parser->num_classes_being_defined;
22466 /* Inside the class, surrounding template-parameter-lists do not
22467 apply. */
22468 saved_num_template_parameter_lists
22469 = parser->num_template_parameter_lists;
22470 parser->num_template_parameter_lists = 0;
22471 /* We are not in a function body. */
22472 saved_in_function_body = parser->in_function_body;
22473 parser->in_function_body = false;
22474 /* Or in a loop. */
22475 in_statement = parser->in_statement;
22476 parser->in_statement = 0;
22477 /* Or in a switch. */
22478 in_switch_statement_p = parser->in_switch_statement_p;
22479 parser->in_switch_statement_p = false;
22480 /* We are not immediately inside an extern "lang" block. */
22481 saved_in_unbraced_linkage_specification_p
22482 = parser->in_unbraced_linkage_specification_p;
22483 parser->in_unbraced_linkage_specification_p = false;
22485 // Associate constraints with the type.
22486 if (flag_concepts)
22487 type = associate_classtype_constraints (type);
22489 /* Start the class. */
22490 if (nested_name_specifier_p)
22492 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22493 old_scope = push_inner_scope (scope);
22495 type = begin_class_definition (type);
22497 if (type == error_mark_node)
22498 /* If the type is erroneous, skip the entire body of the class. */
22499 cp_parser_skip_to_closing_brace (parser);
22500 else
22501 /* Parse the member-specification. */
22502 cp_parser_member_specification_opt (parser);
22504 /* Look for the trailing `}'. */
22505 closing_brace = braces.require_close (parser);
22506 /* Look for trailing attributes to apply to this class. */
22507 if (cp_parser_allow_gnu_extensions_p (parser))
22508 attributes = cp_parser_gnu_attributes_opt (parser);
22509 if (type != error_mark_node)
22510 type = finish_struct (type, attributes);
22511 if (nested_name_specifier_p)
22512 pop_inner_scope (old_scope, scope);
22514 /* We've finished a type definition. Check for the common syntax
22515 error of forgetting a semicolon after the definition. We need to
22516 be careful, as we can't just check for not-a-semicolon and be done
22517 with it; the user might have typed:
22519 class X { } c = ...;
22520 class X { } *p = ...;
22522 and so forth. Instead, enumerate all the possible tokens that
22523 might follow this production; if we don't see one of them, then
22524 complain and silently insert the semicolon. */
22526 cp_token *token = cp_lexer_peek_token (parser->lexer);
22527 bool want_semicolon = true;
22529 if (cp_next_tokens_can_be_std_attribute_p (parser))
22530 /* Don't try to parse c++11 attributes here. As per the
22531 grammar, that should be a task for
22532 cp_parser_decl_specifier_seq. */
22533 want_semicolon = false;
22535 switch (token->type)
22537 case CPP_NAME:
22538 case CPP_SEMICOLON:
22539 case CPP_MULT:
22540 case CPP_AND:
22541 case CPP_OPEN_PAREN:
22542 case CPP_CLOSE_PAREN:
22543 case CPP_COMMA:
22544 want_semicolon = false;
22545 break;
22547 /* While it's legal for type qualifiers and storage class
22548 specifiers to follow type definitions in the grammar, only
22549 compiler testsuites contain code like that. Assume that if
22550 we see such code, then what we're really seeing is a case
22551 like:
22553 class X { }
22554 const <type> var = ...;
22558 class Y { }
22559 static <type> func (...) ...
22561 i.e. the qualifier or specifier applies to the next
22562 declaration. To do so, however, we need to look ahead one
22563 more token to see if *that* token is a type specifier.
22565 This code could be improved to handle:
22567 class Z { }
22568 static const <type> var = ...; */
22569 case CPP_KEYWORD:
22570 if (keyword_is_decl_specifier (token->keyword))
22572 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22574 /* Handling user-defined types here would be nice, but very
22575 tricky. */
22576 want_semicolon
22577 = (lookahead->type == CPP_KEYWORD
22578 && keyword_begins_type_specifier (lookahead->keyword));
22580 break;
22581 default:
22582 break;
22585 /* If we don't have a type, then something is very wrong and we
22586 shouldn't try to do anything clever. Likewise for not seeing the
22587 closing brace. */
22588 if (closing_brace && TYPE_P (type) && want_semicolon)
22590 /* Locate the closing brace. */
22591 cp_token_position prev
22592 = cp_lexer_previous_token_position (parser->lexer);
22593 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22594 location_t loc = prev_token->location;
22596 /* We want to suggest insertion of a ';' immediately *after* the
22597 closing brace, so, if we can, offset the location by 1 column. */
22598 location_t next_loc = loc;
22599 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22600 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22602 rich_location richloc (line_table, next_loc);
22604 /* If we successfully offset the location, suggest the fix-it. */
22605 if (next_loc != loc)
22606 richloc.add_fixit_insert_before (next_loc, ";");
22608 if (CLASSTYPE_DECLARED_CLASS (type))
22609 error_at (&richloc,
22610 "expected %<;%> after class definition");
22611 else if (TREE_CODE (type) == RECORD_TYPE)
22612 error_at (&richloc,
22613 "expected %<;%> after struct definition");
22614 else if (TREE_CODE (type) == UNION_TYPE)
22615 error_at (&richloc,
22616 "expected %<;%> after union definition");
22617 else
22618 gcc_unreachable ();
22620 /* Unget one token and smash it to look as though we encountered
22621 a semicolon in the input stream. */
22622 cp_lexer_set_token_position (parser->lexer, prev);
22623 token = cp_lexer_peek_token (parser->lexer);
22624 token->type = CPP_SEMICOLON;
22625 token->keyword = RID_MAX;
22629 /* If this class is not itself within the scope of another class,
22630 then we need to parse the bodies of all of the queued function
22631 definitions. Note that the queued functions defined in a class
22632 are not always processed immediately following the
22633 class-specifier for that class. Consider:
22635 struct A {
22636 struct B { void f() { sizeof (A); } };
22639 If `f' were processed before the processing of `A' were
22640 completed, there would be no way to compute the size of `A'.
22641 Note that the nesting we are interested in here is lexical --
22642 not the semantic nesting given by TYPE_CONTEXT. In particular,
22643 for:
22645 struct A { struct B; };
22646 struct A::B { void f() { } };
22648 there is no need to delay the parsing of `A::B::f'. */
22649 if (--parser->num_classes_being_defined == 0)
22651 tree decl;
22652 tree class_type = NULL_TREE;
22653 tree pushed_scope = NULL_TREE;
22654 unsigned ix;
22655 cp_default_arg_entry *e;
22656 tree save_ccp, save_ccr;
22658 /* In a first pass, parse default arguments to the functions.
22659 Then, in a second pass, parse the bodies of the functions.
22660 This two-phased approach handles cases like:
22662 struct S {
22663 void f() { g(); }
22664 void g(int i = 3);
22668 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22670 decl = e->decl;
22671 /* If there are default arguments that have not yet been processed,
22672 take care of them now. */
22673 if (class_type != e->class_type)
22675 if (pushed_scope)
22676 pop_scope (pushed_scope);
22677 class_type = e->class_type;
22678 pushed_scope = push_scope (class_type);
22680 /* Make sure that any template parameters are in scope. */
22681 maybe_begin_member_template_processing (decl);
22682 /* Parse the default argument expressions. */
22683 cp_parser_late_parsing_default_args (parser, decl);
22684 /* Remove any template parameters from the symbol table. */
22685 maybe_end_member_template_processing ();
22687 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22688 /* Now parse any NSDMIs. */
22689 save_ccp = current_class_ptr;
22690 save_ccr = current_class_ref;
22691 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22693 if (class_type != DECL_CONTEXT (decl))
22695 if (pushed_scope)
22696 pop_scope (pushed_scope);
22697 class_type = DECL_CONTEXT (decl);
22698 pushed_scope = push_scope (class_type);
22700 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22701 cp_parser_late_parsing_nsdmi (parser, decl);
22703 vec_safe_truncate (unparsed_nsdmis, 0);
22704 current_class_ptr = save_ccp;
22705 current_class_ref = save_ccr;
22706 if (pushed_scope)
22707 pop_scope (pushed_scope);
22709 /* Now do some post-NSDMI bookkeeping. */
22710 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22711 after_nsdmi_defaulted_late_checks (class_type);
22712 vec_safe_truncate (unparsed_classes, 0);
22713 after_nsdmi_defaulted_late_checks (type);
22715 /* Now parse the body of the functions. */
22716 if (flag_openmp)
22718 /* OpenMP UDRs need to be parsed before all other functions. */
22719 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22720 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22721 cp_parser_late_parsing_for_member (parser, decl);
22722 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22723 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22724 cp_parser_late_parsing_for_member (parser, decl);
22726 else
22727 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22728 cp_parser_late_parsing_for_member (parser, decl);
22729 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22731 else
22732 vec_safe_push (unparsed_classes, type);
22734 /* Put back any saved access checks. */
22735 pop_deferring_access_checks ();
22737 /* Restore saved state. */
22738 parser->in_switch_statement_p = in_switch_statement_p;
22739 parser->in_statement = in_statement;
22740 parser->in_function_body = saved_in_function_body;
22741 parser->num_template_parameter_lists
22742 = saved_num_template_parameter_lists;
22743 parser->in_unbraced_linkage_specification_p
22744 = saved_in_unbraced_linkage_specification_p;
22746 return type;
22749 static tree
22750 cp_parser_class_specifier (cp_parser* parser)
22752 tree ret;
22753 timevar_push (TV_PARSE_STRUCT);
22754 ret = cp_parser_class_specifier_1 (parser);
22755 timevar_pop (TV_PARSE_STRUCT);
22756 return ret;
22759 /* Parse a class-head.
22761 class-head:
22762 class-key identifier [opt] base-clause [opt]
22763 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22764 class-key nested-name-specifier [opt] template-id
22765 base-clause [opt]
22767 class-virt-specifier:
22768 final
22770 GNU Extensions:
22771 class-key attributes identifier [opt] base-clause [opt]
22772 class-key attributes nested-name-specifier identifier base-clause [opt]
22773 class-key attributes nested-name-specifier [opt] template-id
22774 base-clause [opt]
22776 Upon return BASES is initialized to the list of base classes (or
22777 NULL, if there are none) in the same form returned by
22778 cp_parser_base_clause.
22780 Returns the TYPE of the indicated class. Sets
22781 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22782 involving a nested-name-specifier was used, and FALSE otherwise.
22784 Returns error_mark_node if this is not a class-head.
22786 Returns NULL_TREE if the class-head is syntactically valid, but
22787 semantically invalid in a way that means we should skip the entire
22788 body of the class. */
22790 static tree
22791 cp_parser_class_head (cp_parser* parser,
22792 bool* nested_name_specifier_p)
22794 tree nested_name_specifier;
22795 enum tag_types class_key;
22796 tree id = NULL_TREE;
22797 tree type = NULL_TREE;
22798 tree attributes;
22799 tree bases;
22800 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22801 bool template_id_p = false;
22802 bool qualified_p = false;
22803 bool invalid_nested_name_p = false;
22804 bool invalid_explicit_specialization_p = false;
22805 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22806 tree pushed_scope = NULL_TREE;
22807 unsigned num_templates;
22808 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22809 /* Assume no nested-name-specifier will be present. */
22810 *nested_name_specifier_p = false;
22811 /* Assume no template parameter lists will be used in defining the
22812 type. */
22813 num_templates = 0;
22814 parser->colon_corrects_to_scope_p = false;
22816 /* Look for the class-key. */
22817 class_key = cp_parser_class_key (parser);
22818 if (class_key == none_type)
22819 return error_mark_node;
22821 location_t class_head_start_location = input_location;
22823 /* Parse the attributes. */
22824 attributes = cp_parser_attributes_opt (parser);
22826 /* If the next token is `::', that is invalid -- but sometimes
22827 people do try to write:
22829 struct ::S {};
22831 Handle this gracefully by accepting the extra qualifier, and then
22832 issuing an error about it later if this really is a
22833 class-head. If it turns out just to be an elaborated type
22834 specifier, remain silent. */
22835 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22836 qualified_p = true;
22838 push_deferring_access_checks (dk_no_check);
22840 /* Determine the name of the class. Begin by looking for an
22841 optional nested-name-specifier. */
22842 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22843 nested_name_specifier
22844 = cp_parser_nested_name_specifier_opt (parser,
22845 /*typename_keyword_p=*/false,
22846 /*check_dependency_p=*/false,
22847 /*type_p=*/true,
22848 /*is_declaration=*/false);
22849 /* If there was a nested-name-specifier, then there *must* be an
22850 identifier. */
22852 cp_token *bad_template_keyword = NULL;
22854 if (nested_name_specifier)
22856 type_start_token = cp_lexer_peek_token (parser->lexer);
22857 /* Although the grammar says `identifier', it really means
22858 `class-name' or `template-name'. You are only allowed to
22859 define a class that has already been declared with this
22860 syntax.
22862 The proposed resolution for Core Issue 180 says that wherever
22863 you see `class T::X' you should treat `X' as a type-name.
22865 It is OK to define an inaccessible class; for example:
22867 class A { class B; };
22868 class A::B {};
22870 We do not know if we will see a class-name, or a
22871 template-name. We look for a class-name first, in case the
22872 class-name is a template-id; if we looked for the
22873 template-name first we would stop after the template-name. */
22874 cp_parser_parse_tentatively (parser);
22875 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22876 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22877 type = cp_parser_class_name (parser,
22878 /*typename_keyword_p=*/false,
22879 /*template_keyword_p=*/false,
22880 class_type,
22881 /*check_dependency_p=*/false,
22882 /*class_head_p=*/true,
22883 /*is_declaration=*/false);
22884 /* If that didn't work, ignore the nested-name-specifier. */
22885 if (!cp_parser_parse_definitely (parser))
22887 invalid_nested_name_p = true;
22888 type_start_token = cp_lexer_peek_token (parser->lexer);
22889 id = cp_parser_identifier (parser);
22890 if (id == error_mark_node)
22891 id = NULL_TREE;
22893 /* If we could not find a corresponding TYPE, treat this
22894 declaration like an unqualified declaration. */
22895 if (type == error_mark_node)
22896 nested_name_specifier = NULL_TREE;
22897 /* Otherwise, count the number of templates used in TYPE and its
22898 containing scopes. */
22899 else
22901 tree scope;
22903 for (scope = TREE_TYPE (type);
22904 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22905 scope = get_containing_scope (scope))
22906 if (TYPE_P (scope)
22907 && CLASS_TYPE_P (scope)
22908 && CLASSTYPE_TEMPLATE_INFO (scope)
22909 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22910 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22911 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22912 ++num_templates;
22915 /* Otherwise, the identifier is optional. */
22916 else
22918 /* We don't know whether what comes next is a template-id,
22919 an identifier, or nothing at all. */
22920 cp_parser_parse_tentatively (parser);
22921 /* Check for a template-id. */
22922 type_start_token = cp_lexer_peek_token (parser->lexer);
22923 id = cp_parser_template_id (parser,
22924 /*template_keyword_p=*/false,
22925 /*check_dependency_p=*/true,
22926 class_key,
22927 /*is_declaration=*/true);
22928 /* If that didn't work, it could still be an identifier. */
22929 if (!cp_parser_parse_definitely (parser))
22931 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22933 type_start_token = cp_lexer_peek_token (parser->lexer);
22934 id = cp_parser_identifier (parser);
22936 else
22937 id = NULL_TREE;
22939 else
22941 template_id_p = true;
22942 ++num_templates;
22946 pop_deferring_access_checks ();
22948 if (id)
22950 cp_parser_check_for_invalid_template_id (parser, id,
22951 class_key,
22952 type_start_token->location);
22954 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22956 /* If it's not a `:' or a `{' then we can't really be looking at a
22957 class-head, since a class-head only appears as part of a
22958 class-specifier. We have to detect this situation before calling
22959 xref_tag, since that has irreversible side-effects. */
22960 if (!cp_parser_next_token_starts_class_definition_p (parser))
22962 cp_parser_error (parser, "expected %<{%> or %<:%>");
22963 type = error_mark_node;
22964 goto out;
22967 /* At this point, we're going ahead with the class-specifier, even
22968 if some other problem occurs. */
22969 cp_parser_commit_to_tentative_parse (parser);
22970 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22972 cp_parser_error (parser,
22973 "cannot specify %<override%> for a class");
22974 type = error_mark_node;
22975 goto out;
22977 /* Issue the error about the overly-qualified name now. */
22978 if (qualified_p)
22980 cp_parser_error (parser,
22981 "global qualification of class name is invalid");
22982 type = error_mark_node;
22983 goto out;
22985 else if (invalid_nested_name_p)
22987 cp_parser_error (parser,
22988 "qualified name does not name a class");
22989 type = error_mark_node;
22990 goto out;
22992 else if (nested_name_specifier)
22994 tree scope;
22996 if (bad_template_keyword)
22997 /* [temp.names]: in a qualified-id formed by a class-head-name, the
22998 keyword template shall not appear at the top level. */
22999 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23000 "keyword %<template%> not allowed in class-head-name");
23002 /* Reject typedef-names in class heads. */
23003 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23005 error_at (type_start_token->location,
23006 "invalid class name in declaration of %qD",
23007 type);
23008 type = NULL_TREE;
23009 goto done;
23012 /* Figure out in what scope the declaration is being placed. */
23013 scope = current_scope ();
23014 /* If that scope does not contain the scope in which the
23015 class was originally declared, the program is invalid. */
23016 if (scope && !is_ancestor (scope, nested_name_specifier))
23018 if (at_namespace_scope_p ())
23019 error_at (type_start_token->location,
23020 "declaration of %qD in namespace %qD which does not "
23021 "enclose %qD",
23022 type, scope, nested_name_specifier);
23023 else
23024 error_at (type_start_token->location,
23025 "declaration of %qD in %qD which does not enclose %qD",
23026 type, scope, nested_name_specifier);
23027 type = NULL_TREE;
23028 goto done;
23030 /* [dcl.meaning]
23032 A declarator-id shall not be qualified except for the
23033 definition of a ... nested class outside of its class
23034 ... [or] the definition or explicit instantiation of a
23035 class member of a namespace outside of its namespace. */
23036 if (scope == nested_name_specifier)
23038 permerror (nested_name_specifier_token_start->location,
23039 "extra qualification not allowed");
23040 nested_name_specifier = NULL_TREE;
23041 num_templates = 0;
23044 /* An explicit-specialization must be preceded by "template <>". If
23045 it is not, try to recover gracefully. */
23046 if (at_namespace_scope_p ()
23047 && parser->num_template_parameter_lists == 0
23048 && !processing_template_parmlist
23049 && template_id_p)
23051 /* Build a location of this form:
23052 struct typename <ARGS>
23053 ^~~~~~~~~~~~~~~~~~~~~~
23054 with caret==start at the start token, and
23055 finishing at the end of the type. */
23056 location_t reported_loc
23057 = make_location (class_head_start_location,
23058 class_head_start_location,
23059 get_finish (type_start_token->location));
23060 rich_location richloc (line_table, reported_loc);
23061 richloc.add_fixit_insert_before (class_head_start_location,
23062 "template <> ");
23063 error_at (&richloc,
23064 "an explicit specialization must be preceded by"
23065 " %<template <>%>");
23066 invalid_explicit_specialization_p = true;
23067 /* Take the same action that would have been taken by
23068 cp_parser_explicit_specialization. */
23069 ++parser->num_template_parameter_lists;
23070 begin_specialization ();
23072 /* There must be no "return" statements between this point and the
23073 end of this function; set "type "to the correct return value and
23074 use "goto done;" to return. */
23075 /* Make sure that the right number of template parameters were
23076 present. */
23077 if (!cp_parser_check_template_parameters (parser, num_templates,
23078 type_start_token->location,
23079 /*declarator=*/NULL))
23081 /* If something went wrong, there is no point in even trying to
23082 process the class-definition. */
23083 type = NULL_TREE;
23084 goto done;
23087 /* Look up the type. */
23088 if (template_id_p)
23090 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23091 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23092 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23094 error_at (type_start_token->location,
23095 "function template %qD redeclared as a class template", id);
23096 type = error_mark_node;
23098 else
23100 type = TREE_TYPE (id);
23101 type = maybe_process_partial_specialization (type);
23103 /* Check the scope while we still know whether or not we had a
23104 nested-name-specifier. */
23105 if (type != error_mark_node)
23106 check_unqualified_spec_or_inst (type, type_start_token->location);
23108 if (nested_name_specifier)
23109 pushed_scope = push_scope (nested_name_specifier);
23111 else if (nested_name_specifier)
23113 tree class_type;
23115 /* Given:
23117 template <typename T> struct S { struct T };
23118 template <typename T> struct S<T>::T { };
23120 we will get a TYPENAME_TYPE when processing the definition of
23121 `S::T'. We need to resolve it to the actual type before we
23122 try to define it. */
23123 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23125 class_type = resolve_typename_type (TREE_TYPE (type),
23126 /*only_current_p=*/false);
23127 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23128 type = TYPE_NAME (class_type);
23129 else
23131 cp_parser_error (parser, "could not resolve typename type");
23132 type = error_mark_node;
23136 if (maybe_process_partial_specialization (TREE_TYPE (type))
23137 == error_mark_node)
23139 type = NULL_TREE;
23140 goto done;
23143 class_type = current_class_type;
23144 /* Enter the scope indicated by the nested-name-specifier. */
23145 pushed_scope = push_scope (nested_name_specifier);
23146 /* Get the canonical version of this type. */
23147 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23148 /* Call push_template_decl if it seems like we should be defining a
23149 template either from the template headers or the type we're
23150 defining, so that we diagnose both extra and missing headers. */
23151 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23152 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23153 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23155 type = push_template_decl (type);
23156 if (type == error_mark_node)
23158 type = NULL_TREE;
23159 goto done;
23163 type = TREE_TYPE (type);
23164 *nested_name_specifier_p = true;
23166 else /* The name is not a nested name. */
23168 /* If the class was unnamed, create a dummy name. */
23169 if (!id)
23170 id = make_anon_name ();
23171 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23172 ? ts_within_enclosing_non_class
23173 : ts_current);
23174 type = xref_tag (class_key, id, tag_scope,
23175 parser->num_template_parameter_lists);
23178 /* Indicate whether this class was declared as a `class' or as a
23179 `struct'. */
23180 if (TREE_CODE (type) == RECORD_TYPE)
23181 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23182 cp_parser_check_class_key (class_key, type);
23184 /* If this type was already complete, and we see another definition,
23185 that's an error. */
23186 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23188 error_at (type_start_token->location, "redefinition of %q#T",
23189 type);
23190 inform (location_of (type), "previous definition of %q#T",
23191 type);
23192 type = NULL_TREE;
23193 goto done;
23195 else if (type == error_mark_node)
23196 type = NULL_TREE;
23198 if (type)
23200 /* Apply attributes now, before any use of the class as a template
23201 argument in its base list. */
23202 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23203 fixup_attribute_variants (type);
23206 /* We will have entered the scope containing the class; the names of
23207 base classes should be looked up in that context. For example:
23209 struct A { struct B {}; struct C; };
23210 struct A::C : B {};
23212 is valid. */
23214 /* Get the list of base-classes, if there is one. */
23215 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23217 /* PR59482: enter the class scope so that base-specifiers are looked
23218 up correctly. */
23219 if (type)
23220 pushclass (type);
23221 bases = cp_parser_base_clause (parser);
23222 /* PR59482: get out of the previously pushed class scope so that the
23223 subsequent pops pop the right thing. */
23224 if (type)
23225 popclass ();
23227 else
23228 bases = NULL_TREE;
23230 /* If we're really defining a class, process the base classes.
23231 If they're invalid, fail. */
23232 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23233 xref_basetypes (type, bases);
23235 done:
23236 /* Leave the scope given by the nested-name-specifier. We will
23237 enter the class scope itself while processing the members. */
23238 if (pushed_scope)
23239 pop_scope (pushed_scope);
23241 if (invalid_explicit_specialization_p)
23243 end_specialization ();
23244 --parser->num_template_parameter_lists;
23247 if (type)
23248 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23249 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23250 CLASSTYPE_FINAL (type) = 1;
23251 out:
23252 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23253 return type;
23256 /* Parse a class-key.
23258 class-key:
23259 class
23260 struct
23261 union
23263 Returns the kind of class-key specified, or none_type to indicate
23264 error. */
23266 static enum tag_types
23267 cp_parser_class_key (cp_parser* parser)
23269 cp_token *token;
23270 enum tag_types tag_type;
23272 /* Look for the class-key. */
23273 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23274 if (!token)
23275 return none_type;
23277 /* Check to see if the TOKEN is a class-key. */
23278 tag_type = cp_parser_token_is_class_key (token);
23279 if (!tag_type)
23280 cp_parser_error (parser, "expected class-key");
23281 return tag_type;
23284 /* Parse a type-parameter-key.
23286 type-parameter-key:
23287 class
23288 typename
23291 static void
23292 cp_parser_type_parameter_key (cp_parser* parser)
23294 /* Look for the type-parameter-key. */
23295 enum tag_types tag_type = none_type;
23296 cp_token *token = cp_lexer_peek_token (parser->lexer);
23297 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23299 cp_lexer_consume_token (parser->lexer);
23300 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23301 /* typename is not allowed in a template template parameter
23302 by the standard until C++17. */
23303 pedwarn (token->location, OPT_Wpedantic,
23304 "ISO C++ forbids typename key in template template parameter;"
23305 " use -std=c++17 or -std=gnu++17");
23307 else
23308 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23310 return;
23313 /* Parse an (optional) member-specification.
23315 member-specification:
23316 member-declaration member-specification [opt]
23317 access-specifier : member-specification [opt] */
23319 static void
23320 cp_parser_member_specification_opt (cp_parser* parser)
23322 while (true)
23324 cp_token *token;
23325 enum rid keyword;
23327 /* Peek at the next token. */
23328 token = cp_lexer_peek_token (parser->lexer);
23329 /* If it's a `}', or EOF then we've seen all the members. */
23330 if (token->type == CPP_CLOSE_BRACE
23331 || token->type == CPP_EOF
23332 || token->type == CPP_PRAGMA_EOL)
23333 break;
23335 /* See if this token is a keyword. */
23336 keyword = token->keyword;
23337 switch (keyword)
23339 case RID_PUBLIC:
23340 case RID_PROTECTED:
23341 case RID_PRIVATE:
23342 /* Consume the access-specifier. */
23343 cp_lexer_consume_token (parser->lexer);
23344 /* Remember which access-specifier is active. */
23345 current_access_specifier = token->u.value;
23346 /* Look for the `:'. */
23347 cp_parser_require (parser, CPP_COLON, RT_COLON);
23348 break;
23350 default:
23351 /* Accept #pragmas at class scope. */
23352 if (token->type == CPP_PRAGMA)
23354 cp_parser_pragma (parser, pragma_member, NULL);
23355 break;
23358 /* Otherwise, the next construction must be a
23359 member-declaration. */
23360 cp_parser_member_declaration (parser);
23365 /* Parse a member-declaration.
23367 member-declaration:
23368 decl-specifier-seq [opt] member-declarator-list [opt] ;
23369 function-definition ; [opt]
23370 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23371 using-declaration
23372 template-declaration
23373 alias-declaration
23375 member-declarator-list:
23376 member-declarator
23377 member-declarator-list , member-declarator
23379 member-declarator:
23380 declarator pure-specifier [opt]
23381 declarator constant-initializer [opt]
23382 identifier [opt] : constant-expression
23384 GNU Extensions:
23386 member-declaration:
23387 __extension__ member-declaration
23389 member-declarator:
23390 declarator attributes [opt] pure-specifier [opt]
23391 declarator attributes [opt] constant-initializer [opt]
23392 identifier [opt] attributes [opt] : constant-expression
23394 C++0x Extensions:
23396 member-declaration:
23397 static_assert-declaration */
23399 static void
23400 cp_parser_member_declaration (cp_parser* parser)
23402 cp_decl_specifier_seq decl_specifiers;
23403 tree prefix_attributes;
23404 tree decl;
23405 int declares_class_or_enum;
23406 bool friend_p;
23407 cp_token *token = NULL;
23408 cp_token *decl_spec_token_start = NULL;
23409 cp_token *initializer_token_start = NULL;
23410 int saved_pedantic;
23411 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23413 /* Check for the `__extension__' keyword. */
23414 if (cp_parser_extension_opt (parser, &saved_pedantic))
23416 /* Recurse. */
23417 cp_parser_member_declaration (parser);
23418 /* Restore the old value of the PEDANTIC flag. */
23419 pedantic = saved_pedantic;
23421 return;
23424 /* Check for a template-declaration. */
23425 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23427 /* An explicit specialization here is an error condition, and we
23428 expect the specialization handler to detect and report this. */
23429 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23430 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23431 cp_parser_explicit_specialization (parser);
23432 else
23433 cp_parser_template_declaration (parser, /*member_p=*/true);
23435 return;
23437 /* Check for a template introduction. */
23438 else if (cp_parser_template_declaration_after_export (parser, true))
23439 return;
23441 /* Check for a using-declaration. */
23442 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23444 if (cxx_dialect < cxx11)
23446 /* Parse the using-declaration. */
23447 cp_parser_using_declaration (parser,
23448 /*access_declaration_p=*/false);
23449 return;
23451 else
23453 tree decl;
23454 bool alias_decl_expected;
23455 cp_parser_parse_tentatively (parser);
23456 decl = cp_parser_alias_declaration (parser);
23457 /* Note that if we actually see the '=' token after the
23458 identifier, cp_parser_alias_declaration commits the
23459 tentative parse. In that case, we really expect an
23460 alias-declaration. Otherwise, we expect a using
23461 declaration. */
23462 alias_decl_expected =
23463 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23464 cp_parser_parse_definitely (parser);
23466 if (alias_decl_expected)
23467 finish_member_declaration (decl);
23468 else
23469 cp_parser_using_declaration (parser,
23470 /*access_declaration_p=*/false);
23471 return;
23475 /* Check for @defs. */
23476 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23478 tree ivar, member;
23479 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23480 ivar = ivar_chains;
23481 while (ivar)
23483 member = ivar;
23484 ivar = TREE_CHAIN (member);
23485 TREE_CHAIN (member) = NULL_TREE;
23486 finish_member_declaration (member);
23488 return;
23491 /* If the next token is `static_assert' we have a static assertion. */
23492 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23494 cp_parser_static_assert (parser, /*member_p=*/true);
23495 return;
23498 parser->colon_corrects_to_scope_p = false;
23500 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23501 goto out;
23503 /* Parse the decl-specifier-seq. */
23504 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23505 cp_parser_decl_specifier_seq (parser,
23506 CP_PARSER_FLAGS_OPTIONAL,
23507 &decl_specifiers,
23508 &declares_class_or_enum);
23509 /* Check for an invalid type-name. */
23510 if (!decl_specifiers.any_type_specifiers_p
23511 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23512 goto out;
23513 /* If there is no declarator, then the decl-specifier-seq should
23514 specify a type. */
23515 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23517 /* If there was no decl-specifier-seq, and the next token is a
23518 `;', then we have something like:
23520 struct S { ; };
23522 [class.mem]
23524 Each member-declaration shall declare at least one member
23525 name of the class. */
23526 if (!decl_specifiers.any_specifiers_p)
23528 cp_token *token = cp_lexer_peek_token (parser->lexer);
23529 if (!in_system_header_at (token->location))
23531 gcc_rich_location richloc (token->location);
23532 richloc.add_fixit_remove ();
23533 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23536 else
23538 tree type;
23540 /* See if this declaration is a friend. */
23541 friend_p = cp_parser_friend_p (&decl_specifiers);
23542 /* If there were decl-specifiers, check to see if there was
23543 a class-declaration. */
23544 type = check_tag_decl (&decl_specifiers,
23545 /*explicit_type_instantiation_p=*/false);
23546 /* Nested classes have already been added to the class, but
23547 a `friend' needs to be explicitly registered. */
23548 if (friend_p)
23550 /* If the `friend' keyword was present, the friend must
23551 be introduced with a class-key. */
23552 if (!declares_class_or_enum && cxx_dialect < cxx11)
23553 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23554 "in C++03 a class-key must be used "
23555 "when declaring a friend");
23556 /* In this case:
23558 template <typename T> struct A {
23559 friend struct A<T>::B;
23562 A<T>::B will be represented by a TYPENAME_TYPE, and
23563 therefore not recognized by check_tag_decl. */
23564 if (!type)
23566 type = decl_specifiers.type;
23567 if (type && TREE_CODE (type) == TYPE_DECL)
23568 type = TREE_TYPE (type);
23570 if (!type || !TYPE_P (type))
23571 error_at (decl_spec_token_start->location,
23572 "friend declaration does not name a class or "
23573 "function");
23574 else
23575 make_friend_class (current_class_type, type,
23576 /*complain=*/true);
23578 /* If there is no TYPE, an error message will already have
23579 been issued. */
23580 else if (!type || type == error_mark_node)
23582 /* An anonymous aggregate has to be handled specially; such
23583 a declaration really declares a data member (with a
23584 particular type), as opposed to a nested class. */
23585 else if (ANON_AGGR_TYPE_P (type))
23587 /* C++11 9.5/6. */
23588 if (decl_specifiers.storage_class != sc_none)
23589 error_at (decl_spec_token_start->location,
23590 "a storage class on an anonymous aggregate "
23591 "in class scope is not allowed");
23593 /* Remove constructors and such from TYPE, now that we
23594 know it is an anonymous aggregate. */
23595 fixup_anonymous_aggr (type);
23596 /* And make the corresponding data member. */
23597 decl = build_decl (decl_spec_token_start->location,
23598 FIELD_DECL, NULL_TREE, type);
23599 /* Add it to the class. */
23600 finish_member_declaration (decl);
23602 else
23603 cp_parser_check_access_in_redeclaration
23604 (TYPE_NAME (type),
23605 decl_spec_token_start->location);
23608 else
23610 bool assume_semicolon = false;
23612 /* Clear attributes from the decl_specifiers but keep them
23613 around as prefix attributes that apply them to the entity
23614 being declared. */
23615 prefix_attributes = decl_specifiers.attributes;
23616 decl_specifiers.attributes = NULL_TREE;
23618 /* See if these declarations will be friends. */
23619 friend_p = cp_parser_friend_p (&decl_specifiers);
23621 /* Keep going until we hit the `;' at the end of the
23622 declaration. */
23623 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23625 tree attributes = NULL_TREE;
23626 tree first_attribute;
23627 tree initializer;
23628 bool is_bitfld = false;
23629 bool named_bitfld = false;
23631 /* Peek at the next token. */
23632 token = cp_lexer_peek_token (parser->lexer);
23634 /* The following code wants to know early if it is a bit-field
23635 or some other declaration. Attributes can appear before
23636 the `:' token, but are hopefully rare enough that the
23637 simplicity of the tentative lookup pays off. */
23638 if (cp_next_tokens_can_be_attribute_p (parser)
23639 || (token->type == CPP_NAME
23640 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23641 && (named_bitfld = true)))
23643 cp_parser_parse_tentatively (parser);
23644 if (named_bitfld)
23645 cp_lexer_consume_token (parser->lexer);
23646 cp_parser_attributes_opt (parser);
23647 token = cp_lexer_peek_token (parser->lexer);
23648 is_bitfld = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
23649 cp_parser_abort_tentative_parse (parser);
23652 /* Check for a bitfield declaration. */
23653 if (is_bitfld
23654 || token->type == CPP_COLON
23655 || (token->type == CPP_NAME
23656 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23657 && (named_bitfld = true)))
23659 tree identifier;
23660 tree width;
23661 tree late_attributes = NULL_TREE;
23663 if (named_bitfld)
23664 identifier = cp_parser_identifier (parser);
23665 else
23666 identifier = NULL_TREE;
23668 /* Look for attributes that apply to the bitfield. */
23669 attributes = cp_parser_attributes_opt (parser);
23671 /* Consume the `:' token. */
23672 cp_lexer_consume_token (parser->lexer);
23674 /* Get the width of the bitfield. */
23675 width = cp_parser_constant_expression (parser, false, NULL,
23676 cxx_dialect >= cxx11);
23678 /* In C++2A and as extension for C++11 and above we allow
23679 default member initializers for bit-fields. */
23680 initializer = NULL_TREE;
23681 if (cxx_dialect >= cxx11
23682 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23683 || cp_lexer_next_token_is (parser->lexer,
23684 CPP_OPEN_BRACE)))
23686 location_t loc
23687 = cp_lexer_peek_token (parser->lexer)->location;
23688 if (cxx_dialect < cxx2a
23689 && !in_system_header_at (loc)
23690 && identifier != NULL_TREE)
23691 pedwarn (loc, 0,
23692 "default member initializers for bit-fields "
23693 "only available with -std=c++2a or "
23694 "-std=gnu++2a");
23696 initializer = cp_parser_save_nsdmi (parser);
23697 if (identifier == NULL_TREE)
23699 error_at (loc, "default member initializer for "
23700 "unnamed bit-field");
23701 initializer = NULL_TREE;
23704 else
23706 /* Look for attributes that apply to the bitfield after
23707 the `:' token and width. This is where GCC used to
23708 parse attributes in the past, pedwarn if there is
23709 a std attribute. */
23710 if (cp_next_tokens_can_be_std_attribute_p (parser))
23711 pedwarn (input_location, OPT_Wpedantic,
23712 "ISO C++ allows bit-field attributes only "
23713 "before the %<:%> token");
23715 late_attributes = cp_parser_attributes_opt (parser);
23718 attributes = chainon (attributes, late_attributes);
23720 /* Remember which attributes are prefix attributes and
23721 which are not. */
23722 first_attribute = attributes;
23723 /* Combine the attributes. */
23724 attributes = chainon (prefix_attributes, attributes);
23726 /* Create the bitfield declaration. */
23727 decl = grokbitfield (identifier
23728 ? make_id_declarator (NULL_TREE,
23729 identifier,
23730 sfk_none)
23731 : NULL,
23732 &decl_specifiers,
23733 width, initializer,
23734 attributes);
23736 else
23738 cp_declarator *declarator;
23739 tree asm_specification;
23740 int ctor_dtor_or_conv_p;
23742 /* Parse the declarator. */
23743 declarator
23744 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23745 &ctor_dtor_or_conv_p,
23746 /*parenthesized_p=*/NULL,
23747 /*member_p=*/true,
23748 friend_p);
23750 /* If something went wrong parsing the declarator, make sure
23751 that we at least consume some tokens. */
23752 if (declarator == cp_error_declarator)
23754 /* Skip to the end of the statement. */
23755 cp_parser_skip_to_end_of_statement (parser);
23756 /* If the next token is not a semicolon, that is
23757 probably because we just skipped over the body of
23758 a function. So, we consume a semicolon if
23759 present, but do not issue an error message if it
23760 is not present. */
23761 if (cp_lexer_next_token_is (parser->lexer,
23762 CPP_SEMICOLON))
23763 cp_lexer_consume_token (parser->lexer);
23764 goto out;
23767 if (declares_class_or_enum & 2)
23768 cp_parser_check_for_definition_in_return_type
23769 (declarator, decl_specifiers.type,
23770 decl_specifiers.locations[ds_type_spec]);
23772 /* Look for an asm-specification. */
23773 asm_specification = cp_parser_asm_specification_opt (parser);
23774 /* Look for attributes that apply to the declaration. */
23775 attributes = cp_parser_attributes_opt (parser);
23776 /* Remember which attributes are prefix attributes and
23777 which are not. */
23778 first_attribute = attributes;
23779 /* Combine the attributes. */
23780 attributes = chainon (prefix_attributes, attributes);
23782 /* If it's an `=', then we have a constant-initializer or a
23783 pure-specifier. It is not correct to parse the
23784 initializer before registering the member declaration
23785 since the member declaration should be in scope while
23786 its initializer is processed. However, the rest of the
23787 front end does not yet provide an interface that allows
23788 us to handle this correctly. */
23789 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23791 /* In [class.mem]:
23793 A pure-specifier shall be used only in the declaration of
23794 a virtual function.
23796 A member-declarator can contain a constant-initializer
23797 only if it declares a static member of integral or
23798 enumeration type.
23800 Therefore, if the DECLARATOR is for a function, we look
23801 for a pure-specifier; otherwise, we look for a
23802 constant-initializer. When we call `grokfield', it will
23803 perform more stringent semantics checks. */
23804 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23805 if (function_declarator_p (declarator)
23806 || (decl_specifiers.type
23807 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23808 && declarator->kind == cdk_id
23809 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23810 == FUNCTION_TYPE)))
23811 initializer = cp_parser_pure_specifier (parser);
23812 else if (decl_specifiers.storage_class != sc_static)
23813 initializer = cp_parser_save_nsdmi (parser);
23814 else if (cxx_dialect >= cxx11)
23816 bool nonconst;
23817 /* Don't require a constant rvalue in C++11, since we
23818 might want a reference constant. We'll enforce
23819 constancy later. */
23820 cp_lexer_consume_token (parser->lexer);
23821 /* Parse the initializer. */
23822 initializer = cp_parser_initializer_clause (parser,
23823 &nonconst);
23825 else
23826 /* Parse the initializer. */
23827 initializer = cp_parser_constant_initializer (parser);
23829 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23830 && !function_declarator_p (declarator))
23832 bool x;
23833 if (decl_specifiers.storage_class != sc_static)
23834 initializer = cp_parser_save_nsdmi (parser);
23835 else
23836 initializer = cp_parser_initializer (parser, &x, &x);
23838 /* Otherwise, there is no initializer. */
23839 else
23840 initializer = NULL_TREE;
23842 /* See if we are probably looking at a function
23843 definition. We are certainly not looking at a
23844 member-declarator. Calling `grokfield' has
23845 side-effects, so we must not do it unless we are sure
23846 that we are looking at a member-declarator. */
23847 if (cp_parser_token_starts_function_definition_p
23848 (cp_lexer_peek_token (parser->lexer)))
23850 /* The grammar does not allow a pure-specifier to be
23851 used when a member function is defined. (It is
23852 possible that this fact is an oversight in the
23853 standard, since a pure function may be defined
23854 outside of the class-specifier. */
23855 if (initializer && initializer_token_start)
23856 error_at (initializer_token_start->location,
23857 "pure-specifier on function-definition");
23858 decl = cp_parser_save_member_function_body (parser,
23859 &decl_specifiers,
23860 declarator,
23861 attributes);
23862 if (parser->fully_implicit_function_template_p)
23863 decl = finish_fully_implicit_template (parser, decl);
23864 /* If the member was not a friend, declare it here. */
23865 if (!friend_p)
23866 finish_member_declaration (decl);
23867 /* Peek at the next token. */
23868 token = cp_lexer_peek_token (parser->lexer);
23869 /* If the next token is a semicolon, consume it. */
23870 if (token->type == CPP_SEMICOLON)
23872 location_t semicolon_loc
23873 = cp_lexer_consume_token (parser->lexer)->location;
23874 gcc_rich_location richloc (semicolon_loc);
23875 richloc.add_fixit_remove ();
23876 warning_at (&richloc, OPT_Wextra_semi,
23877 "extra %<;%> after in-class "
23878 "function definition");
23880 goto out;
23882 else
23883 if (declarator->kind == cdk_function)
23884 declarator->id_loc = token->location;
23885 /* Create the declaration. */
23886 decl = grokfield (declarator, &decl_specifiers,
23887 initializer, /*init_const_expr_p=*/true,
23888 asm_specification, attributes);
23889 if (parser->fully_implicit_function_template_p)
23891 if (friend_p)
23892 finish_fully_implicit_template (parser, 0);
23893 else
23894 decl = finish_fully_implicit_template (parser, decl);
23898 cp_finalize_omp_declare_simd (parser, decl);
23899 cp_finalize_oacc_routine (parser, decl, false);
23901 /* Reset PREFIX_ATTRIBUTES. */
23902 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23903 attributes = TREE_CHAIN (attributes);
23904 if (attributes)
23905 TREE_CHAIN (attributes) = NULL_TREE;
23907 /* If there is any qualification still in effect, clear it
23908 now; we will be starting fresh with the next declarator. */
23909 parser->scope = NULL_TREE;
23910 parser->qualifying_scope = NULL_TREE;
23911 parser->object_scope = NULL_TREE;
23912 /* If it's a `,', then there are more declarators. */
23913 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23915 cp_lexer_consume_token (parser->lexer);
23916 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23918 cp_token *token = cp_lexer_previous_token (parser->lexer);
23919 gcc_rich_location richloc (token->location);
23920 richloc.add_fixit_remove ();
23921 error_at (&richloc, "stray %<,%> at end of "
23922 "member declaration");
23925 /* If the next token isn't a `;', then we have a parse error. */
23926 else if (cp_lexer_next_token_is_not (parser->lexer,
23927 CPP_SEMICOLON))
23929 /* The next token might be a ways away from where the
23930 actual semicolon is missing. Find the previous token
23931 and use that for our error position. */
23932 cp_token *token = cp_lexer_previous_token (parser->lexer);
23933 gcc_rich_location richloc (token->location);
23934 richloc.add_fixit_insert_after (";");
23935 error_at (&richloc, "expected %<;%> at end of "
23936 "member declaration");
23938 /* Assume that the user meant to provide a semicolon. If
23939 we were to cp_parser_skip_to_end_of_statement, we might
23940 skip to a semicolon inside a member function definition
23941 and issue nonsensical error messages. */
23942 assume_semicolon = true;
23945 if (decl)
23947 /* Add DECL to the list of members. */
23948 if (!friend_p
23949 /* Explicitly include, eg, NSDMIs, for better error
23950 recovery (c++/58650). */
23951 || !DECL_DECLARES_FUNCTION_P (decl))
23952 finish_member_declaration (decl);
23954 if (TREE_CODE (decl) == FUNCTION_DECL)
23955 cp_parser_save_default_args (parser, decl);
23956 else if (TREE_CODE (decl) == FIELD_DECL
23957 && DECL_INITIAL (decl))
23958 /* Add DECL to the queue of NSDMI to be parsed later. */
23959 vec_safe_push (unparsed_nsdmis, decl);
23962 if (assume_semicolon)
23963 goto out;
23967 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23968 out:
23969 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23972 /* Parse a pure-specifier.
23974 pure-specifier:
23977 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23978 Otherwise, ERROR_MARK_NODE is returned. */
23980 static tree
23981 cp_parser_pure_specifier (cp_parser* parser)
23983 cp_token *token;
23985 /* Look for the `=' token. */
23986 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23987 return error_mark_node;
23988 /* Look for the `0' token. */
23989 token = cp_lexer_peek_token (parser->lexer);
23991 if (token->type == CPP_EOF
23992 || token->type == CPP_PRAGMA_EOL)
23993 return error_mark_node;
23995 cp_lexer_consume_token (parser->lexer);
23997 /* Accept = default or = delete in c++0x mode. */
23998 if (token->keyword == RID_DEFAULT
23999 || token->keyword == RID_DELETE)
24001 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24002 return token->u.value;
24005 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24006 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24008 cp_parser_error (parser,
24009 "invalid pure specifier (only %<= 0%> is allowed)");
24010 cp_parser_skip_to_end_of_statement (parser);
24011 return error_mark_node;
24013 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24015 error_at (token->location, "templates may not be %<virtual%>");
24016 return error_mark_node;
24019 return integer_zero_node;
24022 /* Parse a constant-initializer.
24024 constant-initializer:
24025 = constant-expression
24027 Returns a representation of the constant-expression. */
24029 static tree
24030 cp_parser_constant_initializer (cp_parser* parser)
24032 /* Look for the `=' token. */
24033 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24034 return error_mark_node;
24036 /* It is invalid to write:
24038 struct S { static const int i = { 7 }; };
24041 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24043 cp_parser_error (parser,
24044 "a brace-enclosed initializer is not allowed here");
24045 /* Consume the opening brace. */
24046 matching_braces braces;
24047 braces.consume_open (parser);
24048 /* Skip the initializer. */
24049 cp_parser_skip_to_closing_brace (parser);
24050 /* Look for the trailing `}'. */
24051 braces.require_close (parser);
24053 return error_mark_node;
24056 return cp_parser_constant_expression (parser);
24059 /* Derived classes [gram.class.derived] */
24061 /* Parse a base-clause.
24063 base-clause:
24064 : base-specifier-list
24066 base-specifier-list:
24067 base-specifier ... [opt]
24068 base-specifier-list , base-specifier ... [opt]
24070 Returns a TREE_LIST representing the base-classes, in the order in
24071 which they were declared. The representation of each node is as
24072 described by cp_parser_base_specifier.
24074 In the case that no bases are specified, this function will return
24075 NULL_TREE, not ERROR_MARK_NODE. */
24077 static tree
24078 cp_parser_base_clause (cp_parser* parser)
24080 tree bases = NULL_TREE;
24082 /* Look for the `:' that begins the list. */
24083 cp_parser_require (parser, CPP_COLON, RT_COLON);
24085 /* Scan the base-specifier-list. */
24086 while (true)
24088 cp_token *token;
24089 tree base;
24090 bool pack_expansion_p = false;
24092 /* Look for the base-specifier. */
24093 base = cp_parser_base_specifier (parser);
24094 /* Look for the (optional) ellipsis. */
24095 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24097 /* Consume the `...'. */
24098 cp_lexer_consume_token (parser->lexer);
24100 pack_expansion_p = true;
24103 /* Add BASE to the front of the list. */
24104 if (base && base != error_mark_node)
24106 if (pack_expansion_p)
24107 /* Make this a pack expansion type. */
24108 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24110 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24112 TREE_CHAIN (base) = bases;
24113 bases = base;
24116 /* Peek at the next token. */
24117 token = cp_lexer_peek_token (parser->lexer);
24118 /* If it's not a comma, then the list is complete. */
24119 if (token->type != CPP_COMMA)
24120 break;
24121 /* Consume the `,'. */
24122 cp_lexer_consume_token (parser->lexer);
24125 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24126 base class had a qualified name. However, the next name that
24127 appears is certainly not qualified. */
24128 parser->scope = NULL_TREE;
24129 parser->qualifying_scope = NULL_TREE;
24130 parser->object_scope = NULL_TREE;
24132 return nreverse (bases);
24135 /* Parse a base-specifier.
24137 base-specifier:
24138 :: [opt] nested-name-specifier [opt] class-name
24139 virtual access-specifier [opt] :: [opt] nested-name-specifier
24140 [opt] class-name
24141 access-specifier virtual [opt] :: [opt] nested-name-specifier
24142 [opt] class-name
24144 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24145 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24146 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24147 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24149 static tree
24150 cp_parser_base_specifier (cp_parser* parser)
24152 cp_token *token;
24153 bool done = false;
24154 bool virtual_p = false;
24155 bool duplicate_virtual_error_issued_p = false;
24156 bool duplicate_access_error_issued_p = false;
24157 bool class_scope_p, template_p;
24158 tree access = access_default_node;
24159 tree type;
24161 /* Process the optional `virtual' and `access-specifier'. */
24162 while (!done)
24164 /* Peek at the next token. */
24165 token = cp_lexer_peek_token (parser->lexer);
24166 /* Process `virtual'. */
24167 switch (token->keyword)
24169 case RID_VIRTUAL:
24170 /* If `virtual' appears more than once, issue an error. */
24171 if (virtual_p && !duplicate_virtual_error_issued_p)
24173 cp_parser_error (parser,
24174 "%<virtual%> specified more than once in base-specifier");
24175 duplicate_virtual_error_issued_p = true;
24178 virtual_p = true;
24180 /* Consume the `virtual' token. */
24181 cp_lexer_consume_token (parser->lexer);
24183 break;
24185 case RID_PUBLIC:
24186 case RID_PROTECTED:
24187 case RID_PRIVATE:
24188 /* If more than one access specifier appears, issue an
24189 error. */
24190 if (access != access_default_node
24191 && !duplicate_access_error_issued_p)
24193 cp_parser_error (parser,
24194 "more than one access specifier in base-specifier");
24195 duplicate_access_error_issued_p = true;
24198 access = ridpointers[(int) token->keyword];
24200 /* Consume the access-specifier. */
24201 cp_lexer_consume_token (parser->lexer);
24203 break;
24205 default:
24206 done = true;
24207 break;
24210 /* It is not uncommon to see programs mechanically, erroneously, use
24211 the 'typename' keyword to denote (dependent) qualified types
24212 as base classes. */
24213 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24215 token = cp_lexer_peek_token (parser->lexer);
24216 if (!processing_template_decl)
24217 error_at (token->location,
24218 "keyword %<typename%> not allowed outside of templates");
24219 else
24220 error_at (token->location,
24221 "keyword %<typename%> not allowed in this context "
24222 "(the base class is implicitly a type)");
24223 cp_lexer_consume_token (parser->lexer);
24226 /* Look for the optional `::' operator. */
24227 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24228 /* Look for the nested-name-specifier. The simplest way to
24229 implement:
24231 [temp.res]
24233 The keyword `typename' is not permitted in a base-specifier or
24234 mem-initializer; in these contexts a qualified name that
24235 depends on a template-parameter is implicitly assumed to be a
24236 type name.
24238 is to pretend that we have seen the `typename' keyword at this
24239 point. */
24240 cp_parser_nested_name_specifier_opt (parser,
24241 /*typename_keyword_p=*/true,
24242 /*check_dependency_p=*/true,
24243 /*type_p=*/true,
24244 /*is_declaration=*/true);
24245 /* If the base class is given by a qualified name, assume that names
24246 we see are type names or templates, as appropriate. */
24247 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24248 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24250 if (!parser->scope
24251 && cp_lexer_next_token_is_decltype (parser->lexer))
24252 /* DR 950 allows decltype as a base-specifier. */
24253 type = cp_parser_decltype (parser);
24254 else
24256 /* Otherwise, look for the class-name. */
24257 type = cp_parser_class_name (parser,
24258 class_scope_p,
24259 template_p,
24260 typename_type,
24261 /*check_dependency_p=*/true,
24262 /*class_head_p=*/false,
24263 /*is_declaration=*/true);
24264 type = TREE_TYPE (type);
24267 if (type == error_mark_node)
24268 return error_mark_node;
24270 return finish_base_specifier (type, access, virtual_p);
24273 /* Exception handling [gram.exception] */
24275 /* Parse an (optional) noexcept-specification.
24277 noexcept-specification:
24278 noexcept ( constant-expression ) [opt]
24280 If no noexcept-specification is present, returns NULL_TREE.
24281 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24282 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24283 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24284 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24285 in which case a boolean condition is returned instead. */
24287 static tree
24288 cp_parser_noexcept_specification_opt (cp_parser* parser,
24289 bool require_constexpr,
24290 bool* consumed_expr,
24291 bool return_cond)
24293 cp_token *token;
24294 const char *saved_message;
24296 /* Peek at the next token. */
24297 token = cp_lexer_peek_token (parser->lexer);
24299 /* Is it a noexcept-specification? */
24300 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24302 tree expr;
24303 cp_lexer_consume_token (parser->lexer);
24305 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24307 matching_parens parens;
24308 parens.consume_open (parser);
24310 if (require_constexpr)
24312 /* Types may not be defined in an exception-specification. */
24313 saved_message = parser->type_definition_forbidden_message;
24314 parser->type_definition_forbidden_message
24315 = G_("types may not be defined in an exception-specification");
24317 expr = cp_parser_constant_expression (parser);
24319 /* Restore the saved message. */
24320 parser->type_definition_forbidden_message = saved_message;
24322 else
24324 expr = cp_parser_expression (parser);
24325 *consumed_expr = true;
24328 parens.require_close (parser);
24330 else
24332 expr = boolean_true_node;
24333 if (!require_constexpr)
24334 *consumed_expr = false;
24337 /* We cannot build a noexcept-spec right away because this will check
24338 that expr is a constexpr. */
24339 if (!return_cond)
24340 return build_noexcept_spec (expr, tf_warning_or_error);
24341 else
24342 return expr;
24344 else
24345 return NULL_TREE;
24348 /* Parse an (optional) exception-specification.
24350 exception-specification:
24351 throw ( type-id-list [opt] )
24353 Returns a TREE_LIST representing the exception-specification. The
24354 TREE_VALUE of each node is a type. */
24356 static tree
24357 cp_parser_exception_specification_opt (cp_parser* parser)
24359 cp_token *token;
24360 tree type_id_list;
24361 const char *saved_message;
24363 /* Peek at the next token. */
24364 token = cp_lexer_peek_token (parser->lexer);
24366 /* Is it a noexcept-specification? */
24367 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24368 false);
24369 if (type_id_list != NULL_TREE)
24370 return type_id_list;
24372 /* If it's not `throw', then there's no exception-specification. */
24373 if (!cp_parser_is_keyword (token, RID_THROW))
24374 return NULL_TREE;
24376 location_t loc = token->location;
24378 /* Consume the `throw'. */
24379 cp_lexer_consume_token (parser->lexer);
24381 /* Look for the `('. */
24382 matching_parens parens;
24383 parens.require_open (parser);
24385 /* Peek at the next token. */
24386 token = cp_lexer_peek_token (parser->lexer);
24387 /* If it's not a `)', then there is a type-id-list. */
24388 if (token->type != CPP_CLOSE_PAREN)
24390 /* Types may not be defined in an exception-specification. */
24391 saved_message = parser->type_definition_forbidden_message;
24392 parser->type_definition_forbidden_message
24393 = G_("types may not be defined in an exception-specification");
24394 /* Parse the type-id-list. */
24395 type_id_list = cp_parser_type_id_list (parser);
24396 /* Restore the saved message. */
24397 parser->type_definition_forbidden_message = saved_message;
24399 if (cxx_dialect >= cxx17)
24401 error_at (loc, "ISO C++17 does not allow dynamic exception "
24402 "specifications");
24403 type_id_list = NULL_TREE;
24405 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24406 warning_at (loc, OPT_Wdeprecated,
24407 "dynamic exception specifications are deprecated in "
24408 "C++11");
24410 /* In C++17, throw() is equivalent to noexcept (true). throw()
24411 is deprecated in C++11 and above as well, but is still widely used,
24412 so don't warn about it yet. */
24413 else if (cxx_dialect >= cxx17)
24414 type_id_list = noexcept_true_spec;
24415 else
24416 type_id_list = empty_except_spec;
24418 /* Look for the `)'. */
24419 parens.require_close (parser);
24421 return type_id_list;
24424 /* Parse an (optional) type-id-list.
24426 type-id-list:
24427 type-id ... [opt]
24428 type-id-list , type-id ... [opt]
24430 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24431 in the order that the types were presented. */
24433 static tree
24434 cp_parser_type_id_list (cp_parser* parser)
24436 tree types = NULL_TREE;
24438 while (true)
24440 cp_token *token;
24441 tree type;
24443 token = cp_lexer_peek_token (parser->lexer);
24445 /* Get the next type-id. */
24446 type = cp_parser_type_id (parser);
24447 /* Check for invalid 'auto'. */
24448 if (flag_concepts && type_uses_auto (type))
24450 error_at (token->location,
24451 "invalid use of %<auto%> in exception-specification");
24452 type = error_mark_node;
24454 /* Parse the optional ellipsis. */
24455 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24457 /* Consume the `...'. */
24458 cp_lexer_consume_token (parser->lexer);
24460 /* Turn the type into a pack expansion expression. */
24461 type = make_pack_expansion (type);
24463 /* Add it to the list. */
24464 types = add_exception_specifier (types, type, /*complain=*/1);
24465 /* Peek at the next token. */
24466 token = cp_lexer_peek_token (parser->lexer);
24467 /* If it is not a `,', we are done. */
24468 if (token->type != CPP_COMMA)
24469 break;
24470 /* Consume the `,'. */
24471 cp_lexer_consume_token (parser->lexer);
24474 return nreverse (types);
24477 /* Parse a try-block.
24479 try-block:
24480 try compound-statement handler-seq */
24482 static tree
24483 cp_parser_try_block (cp_parser* parser)
24485 tree try_block;
24487 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24488 if (parser->in_function_body
24489 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24490 error ("%<try%> in %<constexpr%> function");
24492 try_block = begin_try_block ();
24493 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24494 finish_try_block (try_block);
24495 cp_parser_handler_seq (parser);
24496 finish_handler_sequence (try_block);
24498 return try_block;
24501 /* Parse a function-try-block.
24503 function-try-block:
24504 try ctor-initializer [opt] function-body handler-seq */
24506 static void
24507 cp_parser_function_try_block (cp_parser* parser)
24509 tree compound_stmt;
24510 tree try_block;
24512 /* Look for the `try' keyword. */
24513 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24514 return;
24515 /* Let the rest of the front end know where we are. */
24516 try_block = begin_function_try_block (&compound_stmt);
24517 /* Parse the function-body. */
24518 cp_parser_ctor_initializer_opt_and_function_body
24519 (parser, /*in_function_try_block=*/true);
24520 /* We're done with the `try' part. */
24521 finish_function_try_block (try_block);
24522 /* Parse the handlers. */
24523 cp_parser_handler_seq (parser);
24524 /* We're done with the handlers. */
24525 finish_function_handler_sequence (try_block, compound_stmt);
24528 /* Parse a handler-seq.
24530 handler-seq:
24531 handler handler-seq [opt] */
24533 static void
24534 cp_parser_handler_seq (cp_parser* parser)
24536 while (true)
24538 cp_token *token;
24540 /* Parse the handler. */
24541 cp_parser_handler (parser);
24542 /* Peek at the next token. */
24543 token = cp_lexer_peek_token (parser->lexer);
24544 /* If it's not `catch' then there are no more handlers. */
24545 if (!cp_parser_is_keyword (token, RID_CATCH))
24546 break;
24550 /* Parse a handler.
24552 handler:
24553 catch ( exception-declaration ) compound-statement */
24555 static void
24556 cp_parser_handler (cp_parser* parser)
24558 tree handler;
24559 tree declaration;
24561 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24562 handler = begin_handler ();
24563 matching_parens parens;
24564 parens.require_open (parser);
24565 declaration = cp_parser_exception_declaration (parser);
24566 finish_handler_parms (declaration, handler);
24567 parens.require_close (parser);
24568 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24569 finish_handler (handler);
24572 /* Parse an exception-declaration.
24574 exception-declaration:
24575 type-specifier-seq declarator
24576 type-specifier-seq abstract-declarator
24577 type-specifier-seq
24580 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24581 ellipsis variant is used. */
24583 static tree
24584 cp_parser_exception_declaration (cp_parser* parser)
24586 cp_decl_specifier_seq type_specifiers;
24587 cp_declarator *declarator;
24588 const char *saved_message;
24590 /* If it's an ellipsis, it's easy to handle. */
24591 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24593 /* Consume the `...' token. */
24594 cp_lexer_consume_token (parser->lexer);
24595 return NULL_TREE;
24598 /* Types may not be defined in exception-declarations. */
24599 saved_message = parser->type_definition_forbidden_message;
24600 parser->type_definition_forbidden_message
24601 = G_("types may not be defined in exception-declarations");
24603 /* Parse the type-specifier-seq. */
24604 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24605 /*is_trailing_return=*/false,
24606 &type_specifiers);
24607 /* If it's a `)', then there is no declarator. */
24608 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24609 declarator = NULL;
24610 else
24611 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24612 /*ctor_dtor_or_conv_p=*/NULL,
24613 /*parenthesized_p=*/NULL,
24614 /*member_p=*/false,
24615 /*friend_p=*/false);
24617 /* Restore the saved message. */
24618 parser->type_definition_forbidden_message = saved_message;
24620 if (!type_specifiers.any_specifiers_p)
24621 return error_mark_node;
24623 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24626 /* Parse a throw-expression.
24628 throw-expression:
24629 throw assignment-expression [opt]
24631 Returns a THROW_EXPR representing the throw-expression. */
24633 static tree
24634 cp_parser_throw_expression (cp_parser* parser)
24636 tree expression;
24637 cp_token* token;
24639 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24640 token = cp_lexer_peek_token (parser->lexer);
24641 /* Figure out whether or not there is an assignment-expression
24642 following the "throw" keyword. */
24643 if (token->type == CPP_COMMA
24644 || token->type == CPP_SEMICOLON
24645 || token->type == CPP_CLOSE_PAREN
24646 || token->type == CPP_CLOSE_SQUARE
24647 || token->type == CPP_CLOSE_BRACE
24648 || token->type == CPP_COLON)
24649 expression = NULL_TREE;
24650 else
24651 expression = cp_parser_assignment_expression (parser);
24653 return build_throw (expression);
24656 /* GNU Extensions */
24658 /* Parse an (optional) asm-specification.
24660 asm-specification:
24661 asm ( string-literal )
24663 If the asm-specification is present, returns a STRING_CST
24664 corresponding to the string-literal. Otherwise, returns
24665 NULL_TREE. */
24667 static tree
24668 cp_parser_asm_specification_opt (cp_parser* parser)
24670 cp_token *token;
24671 tree asm_specification;
24673 /* Peek at the next token. */
24674 token = cp_lexer_peek_token (parser->lexer);
24675 /* If the next token isn't the `asm' keyword, then there's no
24676 asm-specification. */
24677 if (!cp_parser_is_keyword (token, RID_ASM))
24678 return NULL_TREE;
24680 /* Consume the `asm' token. */
24681 cp_lexer_consume_token (parser->lexer);
24682 /* Look for the `('. */
24683 matching_parens parens;
24684 parens.require_open (parser);
24686 /* Look for the string-literal. */
24687 asm_specification = cp_parser_string_literal (parser, false, false);
24689 /* Look for the `)'. */
24690 parens.require_close (parser);
24692 return asm_specification;
24695 /* Parse an asm-operand-list.
24697 asm-operand-list:
24698 asm-operand
24699 asm-operand-list , asm-operand
24701 asm-operand:
24702 string-literal ( expression )
24703 [ string-literal ] string-literal ( expression )
24705 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24706 each node is the expression. The TREE_PURPOSE is itself a
24707 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24708 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24709 is a STRING_CST for the string literal before the parenthesis. Returns
24710 ERROR_MARK_NODE if any of the operands are invalid. */
24712 static tree
24713 cp_parser_asm_operand_list (cp_parser* parser)
24715 tree asm_operands = NULL_TREE;
24716 bool invalid_operands = false;
24718 while (true)
24720 tree string_literal;
24721 tree expression;
24722 tree name;
24724 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24726 /* Consume the `[' token. */
24727 cp_lexer_consume_token (parser->lexer);
24728 /* Read the operand name. */
24729 name = cp_parser_identifier (parser);
24730 if (name != error_mark_node)
24731 name = build_string (IDENTIFIER_LENGTH (name),
24732 IDENTIFIER_POINTER (name));
24733 /* Look for the closing `]'. */
24734 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24736 else
24737 name = NULL_TREE;
24738 /* Look for the string-literal. */
24739 string_literal = cp_parser_string_literal (parser, false, false);
24741 /* Look for the `('. */
24742 matching_parens parens;
24743 parens.require_open (parser);
24744 /* Parse the expression. */
24745 expression = cp_parser_expression (parser);
24746 /* Look for the `)'. */
24747 parens.require_close (parser);
24749 if (name == error_mark_node
24750 || string_literal == error_mark_node
24751 || expression == error_mark_node)
24752 invalid_operands = true;
24754 /* Add this operand to the list. */
24755 asm_operands = tree_cons (build_tree_list (name, string_literal),
24756 expression,
24757 asm_operands);
24758 /* If the next token is not a `,', there are no more
24759 operands. */
24760 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24761 break;
24762 /* Consume the `,'. */
24763 cp_lexer_consume_token (parser->lexer);
24766 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24769 /* Parse an asm-clobber-list.
24771 asm-clobber-list:
24772 string-literal
24773 asm-clobber-list , string-literal
24775 Returns a TREE_LIST, indicating the clobbers in the order that they
24776 appeared. The TREE_VALUE of each node is a STRING_CST. */
24778 static tree
24779 cp_parser_asm_clobber_list (cp_parser* parser)
24781 tree clobbers = NULL_TREE;
24783 while (true)
24785 tree string_literal;
24787 /* Look for the string literal. */
24788 string_literal = cp_parser_string_literal (parser, false, false);
24789 /* Add it to the list. */
24790 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24791 /* If the next token is not a `,', then the list is
24792 complete. */
24793 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24794 break;
24795 /* Consume the `,' token. */
24796 cp_lexer_consume_token (parser->lexer);
24799 return clobbers;
24802 /* Parse an asm-label-list.
24804 asm-label-list:
24805 identifier
24806 asm-label-list , identifier
24808 Returns a TREE_LIST, indicating the labels in the order that they
24809 appeared. The TREE_VALUE of each node is a label. */
24811 static tree
24812 cp_parser_asm_label_list (cp_parser* parser)
24814 tree labels = NULL_TREE;
24816 while (true)
24818 tree identifier, label, name;
24820 /* Look for the identifier. */
24821 identifier = cp_parser_identifier (parser);
24822 if (!error_operand_p (identifier))
24824 label = lookup_label (identifier);
24825 if (TREE_CODE (label) == LABEL_DECL)
24827 TREE_USED (label) = 1;
24828 check_goto (label);
24829 name = build_string (IDENTIFIER_LENGTH (identifier),
24830 IDENTIFIER_POINTER (identifier));
24831 labels = tree_cons (name, label, labels);
24834 /* If the next token is not a `,', then the list is
24835 complete. */
24836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24837 break;
24838 /* Consume the `,' token. */
24839 cp_lexer_consume_token (parser->lexer);
24842 return nreverse (labels);
24845 /* Return TRUE iff the next tokens in the stream are possibly the
24846 beginning of a GNU extension attribute. */
24848 static bool
24849 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24851 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24854 /* Return TRUE iff the next tokens in the stream are possibly the
24855 beginning of a standard C++-11 attribute specifier. */
24857 static bool
24858 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24860 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24863 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24864 beginning of a standard C++-11 attribute specifier. */
24866 static bool
24867 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24869 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24871 return (cxx_dialect >= cxx11
24872 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24873 || (token->type == CPP_OPEN_SQUARE
24874 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24875 && token->type == CPP_OPEN_SQUARE)));
24878 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24879 beginning of a GNU extension attribute. */
24881 static bool
24882 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24884 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24886 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24889 /* Return true iff the next tokens can be the beginning of either a
24890 GNU attribute list, or a standard C++11 attribute sequence. */
24892 static bool
24893 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24895 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24896 || cp_next_tokens_can_be_std_attribute_p (parser));
24899 /* Return true iff the next Nth tokens can be the beginning of either
24900 a GNU attribute list, or a standard C++11 attribute sequence. */
24902 static bool
24903 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24905 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24906 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24909 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24910 of GNU attributes, or return NULL. */
24912 static tree
24913 cp_parser_attributes_opt (cp_parser *parser)
24915 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24916 return cp_parser_gnu_attributes_opt (parser);
24917 return cp_parser_std_attribute_spec_seq (parser);
24920 #define CILK_SIMD_FN_CLAUSE_MASK \
24921 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24922 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24923 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24924 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24925 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24927 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24928 vector [(<clauses>)] */
24930 static void
24931 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24933 bool first_p = parser->cilk_simd_fn_info == NULL;
24934 cp_token *token = v_token;
24935 if (first_p)
24937 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24938 parser->cilk_simd_fn_info->error_seen = false;
24939 parser->cilk_simd_fn_info->fndecl_seen = false;
24940 parser->cilk_simd_fn_info->tokens = vNULL;
24941 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24943 int paren_scope = 0;
24944 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24946 cp_lexer_consume_token (parser->lexer);
24947 v_token = cp_lexer_peek_token (parser->lexer);
24948 paren_scope++;
24950 while (paren_scope > 0)
24952 token = cp_lexer_peek_token (parser->lexer);
24953 if (token->type == CPP_OPEN_PAREN)
24954 paren_scope++;
24955 else if (token->type == CPP_CLOSE_PAREN)
24956 paren_scope--;
24957 /* Do not push the last ')' */
24958 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24959 cp_lexer_consume_token (parser->lexer);
24962 token->type = CPP_PRAGMA_EOL;
24963 parser->lexer->next_token = token;
24964 cp_lexer_consume_token (parser->lexer);
24966 struct cp_token_cache *cp
24967 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24968 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24971 /* Parse an (optional) series of attributes.
24973 attributes:
24974 attributes attribute
24976 attribute:
24977 __attribute__ (( attribute-list [opt] ))
24979 The return value is as for cp_parser_gnu_attribute_list. */
24981 static tree
24982 cp_parser_gnu_attributes_opt (cp_parser* parser)
24984 tree attributes = NULL_TREE;
24986 while (true)
24988 cp_token *token;
24989 tree attribute_list;
24990 bool ok = true;
24992 /* Peek at the next token. */
24993 token = cp_lexer_peek_token (parser->lexer);
24994 /* If it's not `__attribute__', then we're done. */
24995 if (token->keyword != RID_ATTRIBUTE)
24996 break;
24998 /* Consume the `__attribute__' keyword. */
24999 cp_lexer_consume_token (parser->lexer);
25000 /* Look for the two `(' tokens. */
25001 matching_parens outer_parens;
25002 outer_parens.require_open (parser);
25003 matching_parens inner_parens;
25004 inner_parens.require_open (parser);
25006 /* Peek at the next token. */
25007 token = cp_lexer_peek_token (parser->lexer);
25008 if (token->type != CPP_CLOSE_PAREN)
25009 /* Parse the attribute-list. */
25010 attribute_list = cp_parser_gnu_attribute_list (parser);
25011 else
25012 /* If the next token is a `)', then there is no attribute
25013 list. */
25014 attribute_list = NULL;
25016 /* Look for the two `)' tokens. */
25017 if (!inner_parens.require_close (parser))
25018 ok = false;
25019 if (!outer_parens.require_close (parser))
25020 ok = false;
25021 if (!ok)
25022 cp_parser_skip_to_end_of_statement (parser);
25024 /* Add these new attributes to the list. */
25025 attributes = chainon (attributes, attribute_list);
25028 return attributes;
25031 /* Parse a GNU attribute-list.
25033 attribute-list:
25034 attribute
25035 attribute-list , attribute
25037 attribute:
25038 identifier
25039 identifier ( identifier )
25040 identifier ( identifier , expression-list )
25041 identifier ( expression-list )
25043 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25044 to an attribute. The TREE_PURPOSE of each node is the identifier
25045 indicating which attribute is in use. The TREE_VALUE represents
25046 the arguments, if any. */
25048 static tree
25049 cp_parser_gnu_attribute_list (cp_parser* parser)
25051 tree attribute_list = NULL_TREE;
25052 bool save_translate_strings_p = parser->translate_strings_p;
25054 parser->translate_strings_p = false;
25055 while (true)
25057 cp_token *token;
25058 tree identifier;
25059 tree attribute;
25061 /* Look for the identifier. We also allow keywords here; for
25062 example `__attribute__ ((const))' is legal. */
25063 token = cp_lexer_peek_token (parser->lexer);
25064 if (token->type == CPP_NAME
25065 || token->type == CPP_KEYWORD)
25067 tree arguments = NULL_TREE;
25069 /* Consume the token, but save it since we need it for the
25070 SIMD enabled function parsing. */
25071 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25073 /* Save away the identifier that indicates which attribute
25074 this is. */
25075 identifier = (token->type == CPP_KEYWORD)
25076 /* For keywords, use the canonical spelling, not the
25077 parsed identifier. */
25078 ? ridpointers[(int) token->keyword]
25079 : id_token->u.value;
25081 identifier = canonicalize_attr_name (identifier);
25082 attribute = build_tree_list (identifier, NULL_TREE);
25084 /* Peek at the next token. */
25085 token = cp_lexer_peek_token (parser->lexer);
25086 /* If it's an `(', then parse the attribute arguments. */
25087 if (token->type == CPP_OPEN_PAREN)
25089 vec<tree, va_gc> *vec;
25090 int attr_flag = (attribute_takes_identifier_p (identifier)
25091 ? id_attr : normal_attr);
25092 if (is_cilkplus_vector_p (identifier))
25094 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
25095 continue;
25097 else
25098 vec = cp_parser_parenthesized_expression_list
25099 (parser, attr_flag, /*cast_p=*/false,
25100 /*allow_expansion_p=*/false,
25101 /*non_constant_p=*/NULL);
25102 if (vec == NULL)
25103 arguments = error_mark_node;
25104 else
25106 arguments = build_tree_list_vec (vec);
25107 release_tree_vector (vec);
25109 /* Save the arguments away. */
25110 TREE_VALUE (attribute) = arguments;
25112 else if (is_cilkplus_vector_p (identifier))
25114 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
25115 continue;
25118 if (arguments != error_mark_node)
25120 /* Add this attribute to the list. */
25121 TREE_CHAIN (attribute) = attribute_list;
25122 attribute_list = attribute;
25125 token = cp_lexer_peek_token (parser->lexer);
25127 /* Now, look for more attributes. If the next token isn't a
25128 `,', we're done. */
25129 if (token->type != CPP_COMMA)
25130 break;
25132 /* Consume the comma and keep going. */
25133 cp_lexer_consume_token (parser->lexer);
25135 parser->translate_strings_p = save_translate_strings_p;
25137 /* We built up the list in reverse order. */
25138 return nreverse (attribute_list);
25141 /* Parse a standard C++11 attribute.
25143 The returned representation is a TREE_LIST which TREE_PURPOSE is
25144 the scoped name of the attribute, and the TREE_VALUE is its
25145 arguments list.
25147 Note that the scoped name of the attribute is itself a TREE_LIST
25148 which TREE_PURPOSE is the namespace of the attribute, and
25149 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25150 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25151 and which TREE_PURPOSE is directly the attribute name.
25153 Clients of the attribute code should use get_attribute_namespace
25154 and get_attribute_name to get the actual namespace and name of
25155 attributes, regardless of their being GNU or C++11 attributes.
25157 attribute:
25158 attribute-token attribute-argument-clause [opt]
25160 attribute-token:
25161 identifier
25162 attribute-scoped-token
25164 attribute-scoped-token:
25165 attribute-namespace :: identifier
25167 attribute-namespace:
25168 identifier
25170 attribute-argument-clause:
25171 ( balanced-token-seq )
25173 balanced-token-seq:
25174 balanced-token [opt]
25175 balanced-token-seq balanced-token
25177 balanced-token:
25178 ( balanced-token-seq )
25179 [ balanced-token-seq ]
25180 { balanced-token-seq }. */
25182 static tree
25183 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25185 tree attribute, attr_id = NULL_TREE, arguments;
25186 cp_token *token;
25188 /* First, parse name of the attribute, a.k.a attribute-token. */
25190 token = cp_lexer_peek_token (parser->lexer);
25191 if (token->type == CPP_NAME)
25192 attr_id = token->u.value;
25193 else if (token->type == CPP_KEYWORD)
25194 attr_id = ridpointers[(int) token->keyword];
25195 else if (token->flags & NAMED_OP)
25196 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25198 if (attr_id == NULL_TREE)
25199 return NULL_TREE;
25201 cp_lexer_consume_token (parser->lexer);
25203 token = cp_lexer_peek_token (parser->lexer);
25204 if (token->type == CPP_SCOPE)
25206 /* We are seeing a scoped attribute token. */
25208 cp_lexer_consume_token (parser->lexer);
25209 if (attr_ns)
25210 error_at (token->location, "attribute using prefix used together "
25211 "with scoped attribute token");
25212 attr_ns = attr_id;
25214 token = cp_lexer_consume_token (parser->lexer);
25215 if (token->type == CPP_NAME)
25216 attr_id = token->u.value;
25217 else if (token->type == CPP_KEYWORD)
25218 attr_id = ridpointers[(int) token->keyword];
25219 else if (token->flags & NAMED_OP)
25220 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25221 else
25223 error_at (token->location,
25224 "expected an identifier for the attribute name");
25225 return error_mark_node;
25228 attr_id = canonicalize_attr_name (attr_id);
25229 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25230 NULL_TREE);
25231 token = cp_lexer_peek_token (parser->lexer);
25233 else if (attr_ns)
25234 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25235 NULL_TREE);
25236 else
25238 attr_id = canonicalize_attr_name (attr_id);
25239 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25240 NULL_TREE);
25241 /* C++11 noreturn attribute is equivalent to GNU's. */
25242 if (is_attribute_p ("noreturn", attr_id))
25243 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25244 /* C++14 deprecated attribute is equivalent to GNU's. */
25245 else if (is_attribute_p ("deprecated", attr_id))
25246 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25247 /* C++17 fallthrough attribute is equivalent to GNU's. */
25248 else if (is_attribute_p ("fallthrough", attr_id))
25249 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25250 /* Transactional Memory TS optimize_for_synchronized attribute is
25251 equivalent to GNU transaction_callable. */
25252 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25253 TREE_PURPOSE (attribute)
25254 = get_identifier ("transaction_callable");
25255 /* Transactional Memory attributes are GNU attributes. */
25256 else if (tm_attr_to_mask (attr_id))
25257 TREE_PURPOSE (attribute) = attr_id;
25260 /* Now parse the optional argument clause of the attribute. */
25262 if (token->type != CPP_OPEN_PAREN)
25263 return attribute;
25266 vec<tree, va_gc> *vec;
25267 int attr_flag = normal_attr;
25269 if (attr_ns == get_identifier ("gnu")
25270 && attribute_takes_identifier_p (attr_id))
25271 /* A GNU attribute that takes an identifier in parameter. */
25272 attr_flag = id_attr;
25274 vec = cp_parser_parenthesized_expression_list
25275 (parser, attr_flag, /*cast_p=*/false,
25276 /*allow_expansion_p=*/true,
25277 /*non_constant_p=*/NULL);
25278 if (vec == NULL)
25279 arguments = error_mark_node;
25280 else
25282 arguments = build_tree_list_vec (vec);
25283 release_tree_vector (vec);
25286 if (arguments == error_mark_node)
25287 attribute = error_mark_node;
25288 else
25289 TREE_VALUE (attribute) = arguments;
25292 return attribute;
25295 /* Check that the attribute ATTRIBUTE appears at most once in the
25296 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25297 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25298 isn't implemented yet in GCC. */
25300 static void
25301 cp_parser_check_std_attribute (tree attributes, tree attribute)
25303 if (attributes)
25305 tree name = get_attribute_name (attribute);
25306 if (is_attribute_p ("noreturn", name)
25307 && lookup_attribute ("noreturn", attributes))
25308 error ("attribute %<noreturn%> can appear at most once "
25309 "in an attribute-list");
25310 else if (is_attribute_p ("deprecated", name)
25311 && lookup_attribute ("deprecated", attributes))
25312 error ("attribute %<deprecated%> can appear at most once "
25313 "in an attribute-list");
25317 /* Parse a list of standard C++-11 attributes.
25319 attribute-list:
25320 attribute [opt]
25321 attribute-list , attribute[opt]
25322 attribute ...
25323 attribute-list , attribute ...
25326 static tree
25327 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25329 tree attributes = NULL_TREE, attribute = NULL_TREE;
25330 cp_token *token = NULL;
25332 while (true)
25334 attribute = cp_parser_std_attribute (parser, attr_ns);
25335 if (attribute == error_mark_node)
25336 break;
25337 if (attribute != NULL_TREE)
25339 cp_parser_check_std_attribute (attributes, attribute);
25340 TREE_CHAIN (attribute) = attributes;
25341 attributes = attribute;
25343 token = cp_lexer_peek_token (parser->lexer);
25344 if (token->type == CPP_ELLIPSIS)
25346 cp_lexer_consume_token (parser->lexer);
25347 if (attribute == NULL_TREE)
25348 error_at (token->location,
25349 "expected attribute before %<...%>");
25350 else
25352 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25353 if (pack == error_mark_node)
25354 return error_mark_node;
25355 TREE_VALUE (attribute) = pack;
25357 token = cp_lexer_peek_token (parser->lexer);
25359 if (token->type != CPP_COMMA)
25360 break;
25361 cp_lexer_consume_token (parser->lexer);
25363 attributes = nreverse (attributes);
25364 return attributes;
25367 /* Parse a standard C++-11 attribute specifier.
25369 attribute-specifier:
25370 [ [ attribute-using-prefix [opt] attribute-list ] ]
25371 alignment-specifier
25373 attribute-using-prefix:
25374 using attribute-namespace :
25376 alignment-specifier:
25377 alignas ( type-id ... [opt] )
25378 alignas ( alignment-expression ... [opt] ). */
25380 static tree
25381 cp_parser_std_attribute_spec (cp_parser *parser)
25383 tree attributes = NULL_TREE;
25384 cp_token *token = cp_lexer_peek_token (parser->lexer);
25386 if (token->type == CPP_OPEN_SQUARE
25387 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25389 tree attr_ns = NULL_TREE;
25391 cp_lexer_consume_token (parser->lexer);
25392 cp_lexer_consume_token (parser->lexer);
25394 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25396 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25397 if (token->type == CPP_NAME)
25398 attr_ns = token->u.value;
25399 else if (token->type == CPP_KEYWORD)
25400 attr_ns = ridpointers[(int) token->keyword];
25401 else if (token->flags & NAMED_OP)
25402 attr_ns = get_identifier (cpp_type2name (token->type,
25403 token->flags));
25404 if (attr_ns
25405 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25407 if (cxx_dialect < cxx17
25408 && !in_system_header_at (input_location))
25409 pedwarn (input_location, 0,
25410 "attribute using prefix only available "
25411 "with -std=c++17 or -std=gnu++17");
25413 cp_lexer_consume_token (parser->lexer);
25414 cp_lexer_consume_token (parser->lexer);
25415 cp_lexer_consume_token (parser->lexer);
25417 else
25418 attr_ns = NULL_TREE;
25421 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25423 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25424 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25425 cp_parser_skip_to_end_of_statement (parser);
25426 else
25427 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25428 when we are sure that we have actually parsed them. */
25429 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25431 else
25433 tree alignas_expr;
25435 /* Look for an alignment-specifier. */
25437 token = cp_lexer_peek_token (parser->lexer);
25439 if (token->type != CPP_KEYWORD
25440 || token->keyword != RID_ALIGNAS)
25441 return NULL_TREE;
25443 cp_lexer_consume_token (parser->lexer);
25444 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25446 matching_parens parens;
25447 if (!parens.require_open (parser))
25449 cp_parser_error (parser, "expected %<(%>");
25450 return error_mark_node;
25453 cp_parser_parse_tentatively (parser);
25454 alignas_expr = cp_parser_type_id (parser);
25456 if (!cp_parser_parse_definitely (parser))
25458 alignas_expr = cp_parser_assignment_expression (parser);
25459 if (alignas_expr == error_mark_node)
25460 cp_parser_skip_to_end_of_statement (parser);
25461 if (alignas_expr == NULL_TREE
25462 || alignas_expr == error_mark_node)
25463 return alignas_expr;
25466 alignas_expr = cxx_alignas_expr (alignas_expr);
25467 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25469 /* Handle alignas (pack...). */
25470 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25472 cp_lexer_consume_token (parser->lexer);
25473 alignas_expr = make_pack_expansion (alignas_expr);
25476 /* Something went wrong, so don't build the attribute. */
25477 if (alignas_expr == error_mark_node)
25478 return error_mark_node;
25480 if (!parens.require_close (parser))
25482 cp_parser_error (parser, "expected %<)%>");
25483 return error_mark_node;
25486 /* Build the C++-11 representation of an 'aligned'
25487 attribute. */
25488 attributes =
25489 build_tree_list (build_tree_list (get_identifier ("gnu"),
25490 get_identifier ("aligned")),
25491 alignas_expr);
25494 return attributes;
25497 /* Parse a standard C++-11 attribute-specifier-seq.
25499 attribute-specifier-seq:
25500 attribute-specifier-seq [opt] attribute-specifier
25503 static tree
25504 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25506 tree attr_specs = NULL_TREE;
25507 tree attr_last = NULL_TREE;
25509 while (true)
25511 tree attr_spec = cp_parser_std_attribute_spec (parser);
25512 if (attr_spec == NULL_TREE)
25513 break;
25514 if (attr_spec == error_mark_node)
25515 return error_mark_node;
25517 if (attr_last)
25518 TREE_CHAIN (attr_last) = attr_spec;
25519 else
25520 attr_specs = attr_last = attr_spec;
25521 attr_last = tree_last (attr_last);
25524 return attr_specs;
25527 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25528 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25529 current value of the PEDANTIC flag, regardless of whether or not
25530 the `__extension__' keyword is present. The caller is responsible
25531 for restoring the value of the PEDANTIC flag. */
25533 static bool
25534 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25536 /* Save the old value of the PEDANTIC flag. */
25537 *saved_pedantic = pedantic;
25539 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25541 /* Consume the `__extension__' token. */
25542 cp_lexer_consume_token (parser->lexer);
25543 /* We're not being pedantic while the `__extension__' keyword is
25544 in effect. */
25545 pedantic = 0;
25547 return true;
25550 return false;
25553 /* Parse a label declaration.
25555 label-declaration:
25556 __label__ label-declarator-seq ;
25558 label-declarator-seq:
25559 identifier , label-declarator-seq
25560 identifier */
25562 static void
25563 cp_parser_label_declaration (cp_parser* parser)
25565 /* Look for the `__label__' keyword. */
25566 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25568 while (true)
25570 tree identifier;
25572 /* Look for an identifier. */
25573 identifier = cp_parser_identifier (parser);
25574 /* If we failed, stop. */
25575 if (identifier == error_mark_node)
25576 break;
25577 /* Declare it as a label. */
25578 finish_label_decl (identifier);
25579 /* If the next token is a `;', stop. */
25580 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25581 break;
25582 /* Look for the `,' separating the label declarations. */
25583 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25586 /* Look for the final `;'. */
25587 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25590 // -------------------------------------------------------------------------- //
25591 // Requires Clause
25593 // Parse a requires clause.
25595 // requires-clause:
25596 // 'requires' logical-or-expression
25598 // The required logical-or-expression must be a constant expression. Note
25599 // that we don't check that the expression is constepxr here. We defer until
25600 // we analyze constraints and then, we only check atomic constraints.
25601 static tree
25602 cp_parser_requires_clause (cp_parser *parser)
25604 // Parse the requires clause so that it is not automatically folded.
25605 ++processing_template_decl;
25606 tree expr = cp_parser_binary_expression (parser, false, false,
25607 PREC_NOT_OPERATOR, NULL);
25608 if (check_for_bare_parameter_packs (expr))
25609 expr = error_mark_node;
25610 --processing_template_decl;
25611 return expr;
25614 // Optionally parse a requires clause:
25615 static tree
25616 cp_parser_requires_clause_opt (cp_parser *parser)
25618 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25619 if (tok->keyword != RID_REQUIRES)
25621 if (!flag_concepts && tok->type == CPP_NAME
25622 && tok->u.value == ridpointers[RID_REQUIRES])
25624 error_at (cp_lexer_peek_token (parser->lexer)->location,
25625 "%<requires%> only available with -fconcepts");
25626 /* Parse and discard the requires-clause. */
25627 cp_lexer_consume_token (parser->lexer);
25628 cp_parser_requires_clause (parser);
25630 return NULL_TREE;
25632 cp_lexer_consume_token (parser->lexer);
25633 return cp_parser_requires_clause (parser);
25637 /*---------------------------------------------------------------------------
25638 Requires expressions
25639 ---------------------------------------------------------------------------*/
25641 /* Parse a requires expression
25643 requirement-expression:
25644 'requires' requirement-parameter-list [opt] requirement-body */
25645 static tree
25646 cp_parser_requires_expression (cp_parser *parser)
25648 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25649 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25651 /* A requires-expression shall appear only within a concept
25652 definition or a requires-clause.
25654 TODO: Implement this diagnostic correctly. */
25655 if (!processing_template_decl)
25657 error_at (loc, "a requires expression cannot appear outside a template");
25658 cp_parser_skip_to_end_of_statement (parser);
25659 return error_mark_node;
25662 tree parms, reqs;
25664 /* Local parameters are delared as variables within the scope
25665 of the expression. They are not visible past the end of
25666 the expression. Expressions within the requires-expression
25667 are unevaluated. */
25668 struct scope_sentinel
25670 scope_sentinel ()
25672 ++cp_unevaluated_operand;
25673 begin_scope (sk_block, NULL_TREE);
25676 ~scope_sentinel ()
25678 pop_bindings_and_leave_scope ();
25679 --cp_unevaluated_operand;
25681 } s;
25683 /* Parse the optional parameter list. */
25684 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25686 parms = cp_parser_requirement_parameter_list (parser);
25687 if (parms == error_mark_node)
25688 return error_mark_node;
25690 else
25691 parms = NULL_TREE;
25693 /* Parse the requirement body. */
25694 reqs = cp_parser_requirement_body (parser);
25695 if (reqs == error_mark_node)
25696 return error_mark_node;
25699 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25700 the parm chain. */
25701 grokparms (parms, &parms);
25702 return finish_requires_expr (parms, reqs);
25705 /* Parse a parameterized requirement.
25707 requirement-parameter-list:
25708 '(' parameter-declaration-clause ')' */
25709 static tree
25710 cp_parser_requirement_parameter_list (cp_parser *parser)
25712 matching_parens parens;
25713 if (!parens.require_open (parser))
25714 return error_mark_node;
25716 tree parms = cp_parser_parameter_declaration_clause (parser);
25718 if (!parens.require_close (parser))
25719 return error_mark_node;
25721 return parms;
25724 /* Parse the body of a requirement.
25726 requirement-body:
25727 '{' requirement-list '}' */
25728 static tree
25729 cp_parser_requirement_body (cp_parser *parser)
25731 matching_braces braces;
25732 if (!braces.require_open (parser))
25733 return error_mark_node;
25735 tree reqs = cp_parser_requirement_list (parser);
25737 if (!braces.require_close (parser))
25738 return error_mark_node;
25740 return reqs;
25743 /* Parse a list of requirements.
25745 requirement-list:
25746 requirement
25747 requirement-list ';' requirement[opt] */
25748 static tree
25749 cp_parser_requirement_list (cp_parser *parser)
25751 tree result = NULL_TREE;
25752 while (true)
25754 tree req = cp_parser_requirement (parser);
25755 if (req == error_mark_node)
25756 return error_mark_node;
25758 result = tree_cons (NULL_TREE, req, result);
25760 /* If we see a semi-colon, consume it. */
25761 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25762 cp_lexer_consume_token (parser->lexer);
25764 /* Stop processing at the end of the list. */
25765 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25766 break;
25769 /* Reverse the order of requirements so they are analyzed in
25770 declaration order. */
25771 return nreverse (result);
25774 /* Parse a syntactic requirement or type requirement.
25776 requirement:
25777 simple-requirement
25778 compound-requirement
25779 type-requirement
25780 nested-requirement */
25781 static tree
25782 cp_parser_requirement (cp_parser *parser)
25784 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25785 return cp_parser_compound_requirement (parser);
25786 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25787 return cp_parser_type_requirement (parser);
25788 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25789 return cp_parser_nested_requirement (parser);
25790 else
25791 return cp_parser_simple_requirement (parser);
25794 /* Parse a simple requirement.
25796 simple-requirement:
25797 expression ';' */
25798 static tree
25799 cp_parser_simple_requirement (cp_parser *parser)
25801 tree expr = cp_parser_expression (parser, NULL, false, false);
25802 if (!expr || expr == error_mark_node)
25803 return error_mark_node;
25805 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25806 return error_mark_node;
25808 return finish_simple_requirement (expr);
25811 /* Parse a type requirement
25813 type-requirement
25814 nested-name-specifier [opt] required-type-name ';'
25816 required-type-name:
25817 type-name
25818 'template' [opt] simple-template-id */
25819 static tree
25820 cp_parser_type_requirement (cp_parser *parser)
25822 cp_lexer_consume_token (parser->lexer);
25824 // Save the scope before parsing name specifiers.
25825 tree saved_scope = parser->scope;
25826 tree saved_object_scope = parser->object_scope;
25827 tree saved_qualifying_scope = parser->qualifying_scope;
25828 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25829 cp_parser_nested_name_specifier_opt (parser,
25830 /*typename_keyword_p=*/true,
25831 /*check_dependency_p=*/false,
25832 /*type_p=*/true,
25833 /*is_declaration=*/false);
25835 tree type;
25836 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25838 cp_lexer_consume_token (parser->lexer);
25839 type = cp_parser_template_id (parser,
25840 /*template_keyword_p=*/true,
25841 /*check_dependency=*/false,
25842 /*tag_type=*/none_type,
25843 /*is_declaration=*/false);
25844 type = make_typename_type (parser->scope, type, typename_type,
25845 /*complain=*/tf_error);
25847 else
25848 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25850 if (TREE_CODE (type) == TYPE_DECL)
25851 type = TREE_TYPE (type);
25853 parser->scope = saved_scope;
25854 parser->object_scope = saved_object_scope;
25855 parser->qualifying_scope = saved_qualifying_scope;
25857 if (type == error_mark_node)
25858 cp_parser_skip_to_end_of_statement (parser);
25860 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25861 return error_mark_node;
25862 if (type == error_mark_node)
25863 return error_mark_node;
25865 return finish_type_requirement (type);
25868 /* Parse a compound requirement
25870 compound-requirement:
25871 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25872 static tree
25873 cp_parser_compound_requirement (cp_parser *parser)
25875 /* Parse an expression enclosed in '{ }'s. */
25876 matching_braces braces;
25877 if (!braces.require_open (parser))
25878 return error_mark_node;
25880 tree expr = cp_parser_expression (parser, NULL, false, false);
25881 if (!expr || expr == error_mark_node)
25882 return error_mark_node;
25884 if (!braces.require_close (parser))
25885 return error_mark_node;
25887 /* Parse the optional noexcept. */
25888 bool noexcept_p = false;
25889 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25891 cp_lexer_consume_token (parser->lexer);
25892 noexcept_p = true;
25895 /* Parse the optional trailing return type. */
25896 tree type = NULL_TREE;
25897 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25899 cp_lexer_consume_token (parser->lexer);
25900 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25901 parser->in_result_type_constraint_p = true;
25902 type = cp_parser_trailing_type_id (parser);
25903 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25904 if (type == error_mark_node)
25905 return error_mark_node;
25908 return finish_compound_requirement (expr, type, noexcept_p);
25911 /* Parse a nested requirement. This is the same as a requires clause.
25913 nested-requirement:
25914 requires-clause */
25915 static tree
25916 cp_parser_nested_requirement (cp_parser *parser)
25918 cp_lexer_consume_token (parser->lexer);
25919 tree req = cp_parser_requires_clause (parser);
25920 if (req == error_mark_node)
25921 return error_mark_node;
25922 return finish_nested_requirement (req);
25925 /* Support Functions */
25927 /* Return the appropriate prefer_type argument for lookup_name_real based on
25928 tag_type and template_mem_access. */
25930 static inline int
25931 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25933 /* DR 141: When looking in the current enclosing context for a template-name
25934 after -> or ., only consider class templates. */
25935 if (template_mem_access)
25936 return 2;
25937 switch (tag_type)
25939 case none_type: return 0; // No preference.
25940 case scope_type: return 1; // Type or namespace.
25941 default: return 2; // Type only.
25945 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25946 NAME should have one of the representations used for an
25947 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25948 is returned. If PARSER->SCOPE is a dependent type, then a
25949 SCOPE_REF is returned.
25951 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25952 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25953 was formed. Abstractly, such entities should not be passed to this
25954 function, because they do not need to be looked up, but it is
25955 simpler to check for this special case here, rather than at the
25956 call-sites.
25958 In cases not explicitly covered above, this function returns a
25959 DECL, OVERLOAD, or baselink representing the result of the lookup.
25960 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25961 is returned.
25963 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25964 (e.g., "struct") that was used. In that case bindings that do not
25965 refer to types are ignored.
25967 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25968 ignored.
25970 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25971 are ignored.
25973 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25974 types.
25976 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25977 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25978 NULL_TREE otherwise. */
25980 static cp_expr
25981 cp_parser_lookup_name (cp_parser *parser, tree name,
25982 enum tag_types tag_type,
25983 bool is_template,
25984 bool is_namespace,
25985 bool check_dependency,
25986 tree *ambiguous_decls,
25987 location_t name_location)
25989 tree decl;
25990 tree object_type = parser->context->object_type;
25992 /* Assume that the lookup will be unambiguous. */
25993 if (ambiguous_decls)
25994 *ambiguous_decls = NULL_TREE;
25996 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25997 no longer valid. Note that if we are parsing tentatively, and
25998 the parse fails, OBJECT_TYPE will be automatically restored. */
25999 parser->context->object_type = NULL_TREE;
26001 if (name == error_mark_node)
26002 return error_mark_node;
26004 /* A template-id has already been resolved; there is no lookup to
26005 do. */
26006 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26007 return name;
26008 if (BASELINK_P (name))
26010 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26011 == TEMPLATE_ID_EXPR);
26012 return name;
26015 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26016 it should already have been checked to make sure that the name
26017 used matches the type being destroyed. */
26018 if (TREE_CODE (name) == BIT_NOT_EXPR)
26020 tree type;
26022 /* Figure out to which type this destructor applies. */
26023 if (parser->scope)
26024 type = parser->scope;
26025 else if (object_type)
26026 type = object_type;
26027 else
26028 type = current_class_type;
26029 /* If that's not a class type, there is no destructor. */
26030 if (!type || !CLASS_TYPE_P (type))
26031 return error_mark_node;
26033 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26034 lazily_declare_fn (sfk_destructor, type);
26036 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26037 return dtor;
26039 return error_mark_node;
26042 /* By this point, the NAME should be an ordinary identifier. If
26043 the id-expression was a qualified name, the qualifying scope is
26044 stored in PARSER->SCOPE at this point. */
26045 gcc_assert (identifier_p (name));
26047 /* Perform the lookup. */
26048 if (parser->scope)
26050 bool dependent_p;
26052 if (parser->scope == error_mark_node)
26053 return error_mark_node;
26055 /* If the SCOPE is dependent, the lookup must be deferred until
26056 the template is instantiated -- unless we are explicitly
26057 looking up names in uninstantiated templates. Even then, we
26058 cannot look up the name if the scope is not a class type; it
26059 might, for example, be a template type parameter. */
26060 dependent_p = (TYPE_P (parser->scope)
26061 && dependent_scope_p (parser->scope));
26062 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26063 && dependent_p)
26064 /* Defer lookup. */
26065 decl = error_mark_node;
26066 else
26068 tree pushed_scope = NULL_TREE;
26070 /* If PARSER->SCOPE is a dependent type, then it must be a
26071 class type, and we must not be checking dependencies;
26072 otherwise, we would have processed this lookup above. So
26073 that PARSER->SCOPE is not considered a dependent base by
26074 lookup_member, we must enter the scope here. */
26075 if (dependent_p)
26076 pushed_scope = push_scope (parser->scope);
26078 /* If the PARSER->SCOPE is a template specialization, it
26079 may be instantiated during name lookup. In that case,
26080 errors may be issued. Even if we rollback the current
26081 tentative parse, those errors are valid. */
26082 decl = lookup_qualified_name (parser->scope, name,
26083 prefer_type_arg (tag_type),
26084 /*complain=*/true);
26086 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26087 lookup result and the nested-name-specifier nominates a class C:
26088 * if the name specified after the nested-name-specifier, when
26089 looked up in C, is the injected-class-name of C (Clause 9), or
26090 * if the name specified after the nested-name-specifier is the
26091 same as the identifier or the simple-template-id's template-
26092 name in the last component of the nested-name-specifier,
26093 the name is instead considered to name the constructor of
26094 class C. [ Note: for example, the constructor is not an
26095 acceptable lookup result in an elaborated-type-specifier so
26096 the constructor would not be used in place of the
26097 injected-class-name. --end note ] Such a constructor name
26098 shall be used only in the declarator-id of a declaration that
26099 names a constructor or in a using-declaration. */
26100 if (tag_type == none_type
26101 && DECL_SELF_REFERENCE_P (decl)
26102 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26103 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26104 prefer_type_arg (tag_type),
26105 /*complain=*/true);
26107 /* If we have a single function from a using decl, pull it out. */
26108 if (TREE_CODE (decl) == OVERLOAD
26109 && !really_overloaded_fn (decl))
26110 decl = OVL_FUNCTION (decl);
26112 if (pushed_scope)
26113 pop_scope (pushed_scope);
26116 /* If the scope is a dependent type and either we deferred lookup or
26117 we did lookup but didn't find the name, rememeber the name. */
26118 if (decl == error_mark_node && TYPE_P (parser->scope)
26119 && dependent_type_p (parser->scope))
26121 if (tag_type)
26123 tree type;
26125 /* The resolution to Core Issue 180 says that `struct
26126 A::B' should be considered a type-name, even if `A'
26127 is dependent. */
26128 type = make_typename_type (parser->scope, name, tag_type,
26129 /*complain=*/tf_error);
26130 if (type != error_mark_node)
26131 decl = TYPE_NAME (type);
26133 else if (is_template
26134 && (cp_parser_next_token_ends_template_argument_p (parser)
26135 || cp_lexer_next_token_is (parser->lexer,
26136 CPP_CLOSE_PAREN)))
26137 decl = make_unbound_class_template (parser->scope,
26138 name, NULL_TREE,
26139 /*complain=*/tf_error);
26140 else
26141 decl = build_qualified_name (/*type=*/NULL_TREE,
26142 parser->scope, name,
26143 is_template);
26145 parser->qualifying_scope = parser->scope;
26146 parser->object_scope = NULL_TREE;
26148 else if (object_type)
26150 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26151 OBJECT_TYPE is not a class. */
26152 if (CLASS_TYPE_P (object_type))
26153 /* If the OBJECT_TYPE is a template specialization, it may
26154 be instantiated during name lookup. In that case, errors
26155 may be issued. Even if we rollback the current tentative
26156 parse, those errors are valid. */
26157 decl = lookup_member (object_type,
26158 name,
26159 /*protect=*/0,
26160 prefer_type_arg (tag_type),
26161 tf_warning_or_error);
26162 else
26163 decl = NULL_TREE;
26165 if (!decl)
26166 /* Look it up in the enclosing context. DR 141: When looking for a
26167 template-name after -> or ., only consider class templates. */
26168 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26169 /*nonclass=*/0,
26170 /*block_p=*/true, is_namespace, 0);
26171 if (object_type == unknown_type_node)
26172 /* The object is type-dependent, so we can't look anything up; we used
26173 this to get the DR 141 behavior. */
26174 object_type = NULL_TREE;
26175 parser->object_scope = object_type;
26176 parser->qualifying_scope = NULL_TREE;
26178 else
26180 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26181 /*nonclass=*/0,
26182 /*block_p=*/true, is_namespace, 0);
26183 parser->qualifying_scope = NULL_TREE;
26184 parser->object_scope = NULL_TREE;
26187 /* If the lookup failed, let our caller know. */
26188 if (!decl || decl == error_mark_node)
26189 return error_mark_node;
26191 /* Pull out the template from an injected-class-name (or multiple). */
26192 if (is_template)
26193 decl = maybe_get_template_decl_from_type_decl (decl);
26195 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26196 if (TREE_CODE (decl) == TREE_LIST)
26198 if (ambiguous_decls)
26199 *ambiguous_decls = decl;
26200 /* The error message we have to print is too complicated for
26201 cp_parser_error, so we incorporate its actions directly. */
26202 if (!cp_parser_simulate_error (parser))
26204 error_at (name_location, "reference to %qD is ambiguous",
26205 name);
26206 print_candidates (decl);
26208 return error_mark_node;
26211 gcc_assert (DECL_P (decl)
26212 || TREE_CODE (decl) == OVERLOAD
26213 || TREE_CODE (decl) == SCOPE_REF
26214 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26215 || BASELINK_P (decl));
26217 /* If we have resolved the name of a member declaration, check to
26218 see if the declaration is accessible. When the name resolves to
26219 set of overloaded functions, accessibility is checked when
26220 overload resolution is done.
26222 During an explicit instantiation, access is not checked at all,
26223 as per [temp.explicit]. */
26224 if (DECL_P (decl))
26225 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26227 maybe_record_typedef_use (decl);
26229 return cp_expr (decl, name_location);
26232 /* Like cp_parser_lookup_name, but for use in the typical case where
26233 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26234 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26236 static tree
26237 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26239 return cp_parser_lookup_name (parser, name,
26240 none_type,
26241 /*is_template=*/false,
26242 /*is_namespace=*/false,
26243 /*check_dependency=*/true,
26244 /*ambiguous_decls=*/NULL,
26245 location);
26248 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26249 the current context, return the TYPE_DECL. If TAG_NAME_P is
26250 true, the DECL indicates the class being defined in a class-head,
26251 or declared in an elaborated-type-specifier.
26253 Otherwise, return DECL. */
26255 static tree
26256 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26258 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26259 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26261 struct A {
26262 template <typename T> struct B;
26265 template <typename T> struct A::B {};
26267 Similarly, in an elaborated-type-specifier:
26269 namespace N { struct X{}; }
26271 struct A {
26272 template <typename T> friend struct N::X;
26275 However, if the DECL refers to a class type, and we are in
26276 the scope of the class, then the name lookup automatically
26277 finds the TYPE_DECL created by build_self_reference rather
26278 than a TEMPLATE_DECL. For example, in:
26280 template <class T> struct S {
26281 S s;
26284 there is no need to handle such case. */
26286 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26287 return DECL_TEMPLATE_RESULT (decl);
26289 return decl;
26292 /* If too many, or too few, template-parameter lists apply to the
26293 declarator, issue an error message. Returns TRUE if all went well,
26294 and FALSE otherwise. */
26296 static bool
26297 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26298 cp_declarator *declarator,
26299 location_t declarator_location)
26301 switch (declarator->kind)
26303 case cdk_id:
26305 unsigned num_templates = 0;
26306 tree scope = declarator->u.id.qualifying_scope;
26308 if (scope)
26309 num_templates = num_template_headers_for_class (scope);
26310 else if (TREE_CODE (declarator->u.id.unqualified_name)
26311 == TEMPLATE_ID_EXPR)
26312 /* If the DECLARATOR has the form `X<y>' then it uses one
26313 additional level of template parameters. */
26314 ++num_templates;
26316 return cp_parser_check_template_parameters
26317 (parser, num_templates, declarator_location, declarator);
26320 case cdk_function:
26321 case cdk_array:
26322 case cdk_pointer:
26323 case cdk_reference:
26324 case cdk_ptrmem:
26325 return (cp_parser_check_declarator_template_parameters
26326 (parser, declarator->declarator, declarator_location));
26328 case cdk_decomp:
26329 case cdk_error:
26330 return true;
26332 default:
26333 gcc_unreachable ();
26335 return false;
26338 /* NUM_TEMPLATES were used in the current declaration. If that is
26339 invalid, return FALSE and issue an error messages. Otherwise,
26340 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26341 declarator and we can print more accurate diagnostics. */
26343 static bool
26344 cp_parser_check_template_parameters (cp_parser* parser,
26345 unsigned num_templates,
26346 location_t location,
26347 cp_declarator *declarator)
26349 /* If there are the same number of template classes and parameter
26350 lists, that's OK. */
26351 if (parser->num_template_parameter_lists == num_templates)
26352 return true;
26353 /* If there are more, but only one more, then we are referring to a
26354 member template. That's OK too. */
26355 if (parser->num_template_parameter_lists == num_templates + 1)
26356 return true;
26357 /* If there are more template classes than parameter lists, we have
26358 something like:
26360 template <class T> void S<T>::R<T>::f (); */
26361 if (parser->num_template_parameter_lists < num_templates)
26363 if (declarator && !current_function_decl)
26364 error_at (location, "specializing member %<%T::%E%> "
26365 "requires %<template<>%> syntax",
26366 declarator->u.id.qualifying_scope,
26367 declarator->u.id.unqualified_name);
26368 else if (declarator)
26369 error_at (location, "invalid declaration of %<%T::%E%>",
26370 declarator->u.id.qualifying_scope,
26371 declarator->u.id.unqualified_name);
26372 else
26373 error_at (location, "too few template-parameter-lists");
26374 return false;
26376 /* Otherwise, there are too many template parameter lists. We have
26377 something like:
26379 template <class T> template <class U> void S::f(); */
26380 error_at (location, "too many template-parameter-lists");
26381 return false;
26384 /* Parse an optional `::' token indicating that the following name is
26385 from the global namespace. If so, PARSER->SCOPE is set to the
26386 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26387 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26388 Returns the new value of PARSER->SCOPE, if the `::' token is
26389 present, and NULL_TREE otherwise. */
26391 static tree
26392 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26394 cp_token *token;
26396 /* Peek at the next token. */
26397 token = cp_lexer_peek_token (parser->lexer);
26398 /* If we're looking at a `::' token then we're starting from the
26399 global namespace, not our current location. */
26400 if (token->type == CPP_SCOPE)
26402 /* Consume the `::' token. */
26403 cp_lexer_consume_token (parser->lexer);
26404 /* Set the SCOPE so that we know where to start the lookup. */
26405 parser->scope = global_namespace;
26406 parser->qualifying_scope = global_namespace;
26407 parser->object_scope = NULL_TREE;
26409 return parser->scope;
26411 else if (!current_scope_valid_p)
26413 parser->scope = NULL_TREE;
26414 parser->qualifying_scope = NULL_TREE;
26415 parser->object_scope = NULL_TREE;
26418 return NULL_TREE;
26421 /* Returns TRUE if the upcoming token sequence is the start of a
26422 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26423 declarator is preceded by the `friend' specifier. */
26425 static bool
26426 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26428 bool constructor_p;
26429 bool outside_class_specifier_p;
26430 tree nested_name_specifier;
26431 cp_token *next_token;
26433 /* The common case is that this is not a constructor declarator, so
26434 try to avoid doing lots of work if at all possible. It's not
26435 valid declare a constructor at function scope. */
26436 if (parser->in_function_body)
26437 return false;
26438 /* And only certain tokens can begin a constructor declarator. */
26439 next_token = cp_lexer_peek_token (parser->lexer);
26440 if (next_token->type != CPP_NAME
26441 && next_token->type != CPP_SCOPE
26442 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26443 && next_token->type != CPP_TEMPLATE_ID)
26444 return false;
26446 /* Parse tentatively; we are going to roll back all of the tokens
26447 consumed here. */
26448 cp_parser_parse_tentatively (parser);
26449 /* Assume that we are looking at a constructor declarator. */
26450 constructor_p = true;
26452 /* Look for the optional `::' operator. */
26453 cp_parser_global_scope_opt (parser,
26454 /*current_scope_valid_p=*/false);
26455 /* Look for the nested-name-specifier. */
26456 nested_name_specifier
26457 = (cp_parser_nested_name_specifier_opt (parser,
26458 /*typename_keyword_p=*/false,
26459 /*check_dependency_p=*/false,
26460 /*type_p=*/false,
26461 /*is_declaration=*/false));
26463 outside_class_specifier_p = (!at_class_scope_p ()
26464 || !TYPE_BEING_DEFINED (current_class_type)
26465 || friend_p);
26467 /* Outside of a class-specifier, there must be a
26468 nested-name-specifier. Except in C++17 mode, where we
26469 might be declaring a guiding declaration. */
26470 if (!nested_name_specifier && outside_class_specifier_p
26471 && cxx_dialect < cxx17)
26472 constructor_p = false;
26473 else if (nested_name_specifier == error_mark_node)
26474 constructor_p = false;
26476 /* If we have a class scope, this is easy; DR 147 says that S::S always
26477 names the constructor, and no other qualified name could. */
26478 if (constructor_p && nested_name_specifier
26479 && CLASS_TYPE_P (nested_name_specifier))
26481 tree id = cp_parser_unqualified_id (parser,
26482 /*template_keyword_p=*/false,
26483 /*check_dependency_p=*/false,
26484 /*declarator_p=*/true,
26485 /*optional_p=*/false);
26486 if (is_overloaded_fn (id))
26487 id = DECL_NAME (get_first_fn (id));
26488 if (!constructor_name_p (id, nested_name_specifier))
26489 constructor_p = false;
26491 /* If we still think that this might be a constructor-declarator,
26492 look for a class-name. */
26493 else if (constructor_p)
26495 /* If we have:
26497 template <typename T> struct S {
26498 S();
26501 we must recognize that the nested `S' names a class. */
26502 if (cxx_dialect >= cxx17)
26503 cp_parser_parse_tentatively (parser);
26505 tree type_decl;
26506 type_decl = cp_parser_class_name (parser,
26507 /*typename_keyword_p=*/false,
26508 /*template_keyword_p=*/false,
26509 none_type,
26510 /*check_dependency_p=*/false,
26511 /*class_head_p=*/false,
26512 /*is_declaration=*/false);
26514 if (cxx_dialect >= cxx17
26515 && !cp_parser_parse_definitely (parser))
26517 type_decl = NULL_TREE;
26518 tree tmpl = cp_parser_template_name (parser,
26519 /*template_keyword*/false,
26520 /*check_dependency_p*/false,
26521 /*is_declaration*/false,
26522 none_type,
26523 /*is_identifier*/NULL);
26524 if (DECL_CLASS_TEMPLATE_P (tmpl)
26525 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26526 /* It's a deduction guide, return true. */;
26527 else
26528 cp_parser_simulate_error (parser);
26531 /* If there was no class-name, then this is not a constructor.
26532 Otherwise, if we are in a class-specifier and we aren't
26533 handling a friend declaration, check that its type matches
26534 current_class_type (c++/38313). Note: error_mark_node
26535 is left alone for error recovery purposes. */
26536 constructor_p = (!cp_parser_error_occurred (parser)
26537 && (outside_class_specifier_p
26538 || type_decl == NULL_TREE
26539 || type_decl == error_mark_node
26540 || same_type_p (current_class_type,
26541 TREE_TYPE (type_decl))));
26543 /* If we're still considering a constructor, we have to see a `(',
26544 to begin the parameter-declaration-clause, followed by either a
26545 `)', an `...', or a decl-specifier. We need to check for a
26546 type-specifier to avoid being fooled into thinking that:
26548 S (f) (int);
26550 is a constructor. (It is actually a function named `f' that
26551 takes one parameter (of type `int') and returns a value of type
26552 `S'. */
26553 if (constructor_p
26554 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26555 constructor_p = false;
26557 if (constructor_p
26558 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26559 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26560 /* A parameter declaration begins with a decl-specifier,
26561 which is either the "attribute" keyword, a storage class
26562 specifier, or (usually) a type-specifier. */
26563 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26565 tree type;
26566 tree pushed_scope = NULL_TREE;
26567 unsigned saved_num_template_parameter_lists;
26569 /* Names appearing in the type-specifier should be looked up
26570 in the scope of the class. */
26571 if (current_class_type)
26572 type = NULL_TREE;
26573 else if (type_decl)
26575 type = TREE_TYPE (type_decl);
26576 if (TREE_CODE (type) == TYPENAME_TYPE)
26578 type = resolve_typename_type (type,
26579 /*only_current_p=*/false);
26580 if (TREE_CODE (type) == TYPENAME_TYPE)
26582 cp_parser_abort_tentative_parse (parser);
26583 return false;
26586 pushed_scope = push_scope (type);
26589 /* Inside the constructor parameter list, surrounding
26590 template-parameter-lists do not apply. */
26591 saved_num_template_parameter_lists
26592 = parser->num_template_parameter_lists;
26593 parser->num_template_parameter_lists = 0;
26595 /* Look for the type-specifier. */
26596 cp_parser_type_specifier (parser,
26597 CP_PARSER_FLAGS_NONE,
26598 /*decl_specs=*/NULL,
26599 /*is_declarator=*/true,
26600 /*declares_class_or_enum=*/NULL,
26601 /*is_cv_qualifier=*/NULL);
26603 parser->num_template_parameter_lists
26604 = saved_num_template_parameter_lists;
26606 /* Leave the scope of the class. */
26607 if (pushed_scope)
26608 pop_scope (pushed_scope);
26610 constructor_p = !cp_parser_error_occurred (parser);
26614 /* We did not really want to consume any tokens. */
26615 cp_parser_abort_tentative_parse (parser);
26617 return constructor_p;
26620 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26621 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26622 they must be performed once we are in the scope of the function.
26624 Returns the function defined. */
26626 static tree
26627 cp_parser_function_definition_from_specifiers_and_declarator
26628 (cp_parser* parser,
26629 cp_decl_specifier_seq *decl_specifiers,
26630 tree attributes,
26631 const cp_declarator *declarator)
26633 tree fn;
26634 bool success_p;
26636 /* Begin the function-definition. */
26637 success_p = start_function (decl_specifiers, declarator, attributes);
26639 /* The things we're about to see are not directly qualified by any
26640 template headers we've seen thus far. */
26641 reset_specialization ();
26643 /* If there were names looked up in the decl-specifier-seq that we
26644 did not check, check them now. We must wait until we are in the
26645 scope of the function to perform the checks, since the function
26646 might be a friend. */
26647 perform_deferred_access_checks (tf_warning_or_error);
26649 if (success_p)
26651 cp_finalize_omp_declare_simd (parser, current_function_decl);
26652 parser->omp_declare_simd = NULL;
26653 cp_finalize_oacc_routine (parser, current_function_decl, true);
26654 parser->oacc_routine = NULL;
26657 if (!success_p)
26659 /* Skip the entire function. */
26660 cp_parser_skip_to_end_of_block_or_statement (parser);
26661 fn = error_mark_node;
26663 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26665 /* Seen already, skip it. An error message has already been output. */
26666 cp_parser_skip_to_end_of_block_or_statement (parser);
26667 fn = current_function_decl;
26668 current_function_decl = NULL_TREE;
26669 /* If this is a function from a class, pop the nested class. */
26670 if (current_class_name)
26671 pop_nested_class ();
26673 else
26675 timevar_id_t tv;
26676 if (DECL_DECLARED_INLINE_P (current_function_decl))
26677 tv = TV_PARSE_INLINE;
26678 else
26679 tv = TV_PARSE_FUNC;
26680 timevar_push (tv);
26681 fn = cp_parser_function_definition_after_declarator (parser,
26682 /*inline_p=*/false);
26683 timevar_pop (tv);
26686 return fn;
26689 /* Parse the part of a function-definition that follows the
26690 declarator. INLINE_P is TRUE iff this function is an inline
26691 function defined within a class-specifier.
26693 Returns the function defined. */
26695 static tree
26696 cp_parser_function_definition_after_declarator (cp_parser* parser,
26697 bool inline_p)
26699 tree fn;
26700 bool saved_in_unbraced_linkage_specification_p;
26701 bool saved_in_function_body;
26702 unsigned saved_num_template_parameter_lists;
26703 cp_token *token;
26704 bool fully_implicit_function_template_p
26705 = parser->fully_implicit_function_template_p;
26706 parser->fully_implicit_function_template_p = false;
26707 tree implicit_template_parms
26708 = parser->implicit_template_parms;
26709 parser->implicit_template_parms = 0;
26710 cp_binding_level* implicit_template_scope
26711 = parser->implicit_template_scope;
26712 parser->implicit_template_scope = 0;
26714 saved_in_function_body = parser->in_function_body;
26715 parser->in_function_body = true;
26716 /* If the next token is `return', then the code may be trying to
26717 make use of the "named return value" extension that G++ used to
26718 support. */
26719 token = cp_lexer_peek_token (parser->lexer);
26720 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26722 /* Consume the `return' keyword. */
26723 cp_lexer_consume_token (parser->lexer);
26724 /* Look for the identifier that indicates what value is to be
26725 returned. */
26726 cp_parser_identifier (parser);
26727 /* Issue an error message. */
26728 error_at (token->location,
26729 "named return values are no longer supported");
26730 /* Skip tokens until we reach the start of the function body. */
26731 while (true)
26733 cp_token *token = cp_lexer_peek_token (parser->lexer);
26734 if (token->type == CPP_OPEN_BRACE
26735 || token->type == CPP_EOF
26736 || token->type == CPP_PRAGMA_EOL)
26737 break;
26738 cp_lexer_consume_token (parser->lexer);
26741 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26742 anything declared inside `f'. */
26743 saved_in_unbraced_linkage_specification_p
26744 = parser->in_unbraced_linkage_specification_p;
26745 parser->in_unbraced_linkage_specification_p = false;
26746 /* Inside the function, surrounding template-parameter-lists do not
26747 apply. */
26748 saved_num_template_parameter_lists
26749 = parser->num_template_parameter_lists;
26750 parser->num_template_parameter_lists = 0;
26752 /* If the next token is `try', `__transaction_atomic', or
26753 `__transaction_relaxed`, then we are looking at either function-try-block
26754 or function-transaction-block. Note that all of these include the
26755 function-body. */
26756 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26757 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26758 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26759 RID_TRANSACTION_RELAXED))
26760 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26761 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26762 cp_parser_function_try_block (parser);
26763 else
26764 cp_parser_ctor_initializer_opt_and_function_body
26765 (parser, /*in_function_try_block=*/false);
26767 /* Finish the function. */
26768 fn = finish_function (inline_p);
26769 /* Generate code for it, if necessary. */
26770 expand_or_defer_fn (fn);
26771 /* Restore the saved values. */
26772 parser->in_unbraced_linkage_specification_p
26773 = saved_in_unbraced_linkage_specification_p;
26774 parser->num_template_parameter_lists
26775 = saved_num_template_parameter_lists;
26776 parser->in_function_body = saved_in_function_body;
26778 parser->fully_implicit_function_template_p
26779 = fully_implicit_function_template_p;
26780 parser->implicit_template_parms
26781 = implicit_template_parms;
26782 parser->implicit_template_scope
26783 = implicit_template_scope;
26785 if (parser->fully_implicit_function_template_p)
26786 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26788 return fn;
26791 /* Parse a template-declaration body (following argument list). */
26793 static void
26794 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26795 tree parameter_list,
26796 bool member_p)
26798 tree decl = NULL_TREE;
26799 bool friend_p = false;
26801 /* We just processed one more parameter list. */
26802 ++parser->num_template_parameter_lists;
26804 /* Get the deferred access checks from the parameter list. These
26805 will be checked once we know what is being declared, as for a
26806 member template the checks must be performed in the scope of the
26807 class containing the member. */
26808 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26810 /* Tentatively parse for a new template parameter list, which can either be
26811 the template keyword or a template introduction. */
26812 if (cp_parser_template_declaration_after_export (parser, member_p))
26813 /* OK */;
26814 else if (cxx_dialect >= cxx11
26815 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26816 decl = cp_parser_alias_declaration (parser);
26817 else
26819 /* There are no access checks when parsing a template, as we do not
26820 know if a specialization will be a friend. */
26821 push_deferring_access_checks (dk_no_check);
26822 cp_token *token = cp_lexer_peek_token (parser->lexer);
26823 decl = cp_parser_single_declaration (parser,
26824 checks,
26825 member_p,
26826 /*explicit_specialization_p=*/false,
26827 &friend_p);
26828 pop_deferring_access_checks ();
26830 /* If this is a member template declaration, let the front
26831 end know. */
26832 if (member_p && !friend_p && decl)
26834 if (TREE_CODE (decl) == TYPE_DECL)
26835 cp_parser_check_access_in_redeclaration (decl, token->location);
26837 decl = finish_member_template_decl (decl);
26839 else if (friend_p && decl
26840 && DECL_DECLARES_TYPE_P (decl))
26841 make_friend_class (current_class_type, TREE_TYPE (decl),
26842 /*complain=*/true);
26844 /* We are done with the current parameter list. */
26845 --parser->num_template_parameter_lists;
26847 pop_deferring_access_checks ();
26849 /* Finish up. */
26850 finish_template_decl (parameter_list);
26852 /* Check the template arguments for a literal operator template. */
26853 if (decl
26854 && DECL_DECLARES_FUNCTION_P (decl)
26855 && UDLIT_OPER_P (DECL_NAME (decl)))
26857 bool ok = true;
26858 if (parameter_list == NULL_TREE)
26859 ok = false;
26860 else
26862 int num_parms = TREE_VEC_LENGTH (parameter_list);
26863 if (num_parms == 1)
26865 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26866 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26867 if (TREE_TYPE (parm) != char_type_node
26868 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26869 ok = false;
26871 else if (num_parms == 2 && cxx_dialect >= cxx14)
26873 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26874 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26875 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26876 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26877 if (parm == error_mark_node
26878 || TREE_TYPE (parm) != TREE_TYPE (type)
26879 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26880 ok = false;
26882 else
26883 ok = false;
26885 if (!ok)
26887 if (cxx_dialect >= cxx14)
26888 error ("literal operator template %qD has invalid parameter list."
26889 " Expected non-type template argument pack <char...>"
26890 " or <typename CharT, CharT...>",
26891 decl);
26892 else
26893 error ("literal operator template %qD has invalid parameter list."
26894 " Expected non-type template argument pack <char...>",
26895 decl);
26899 /* Register member declarations. */
26900 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26901 finish_member_declaration (decl);
26902 /* If DECL is a function template, we must return to parse it later.
26903 (Even though there is no definition, there might be default
26904 arguments that need handling.) */
26905 if (member_p && decl
26906 && DECL_DECLARES_FUNCTION_P (decl))
26907 vec_safe_push (unparsed_funs_with_definitions, decl);
26910 /* Parse a template introduction header for a template-declaration. Returns
26911 false if tentative parse fails. */
26913 static bool
26914 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26916 cp_parser_parse_tentatively (parser);
26918 tree saved_scope = parser->scope;
26919 tree saved_object_scope = parser->object_scope;
26920 tree saved_qualifying_scope = parser->qualifying_scope;
26922 /* Look for the optional `::' operator. */
26923 cp_parser_global_scope_opt (parser,
26924 /*current_scope_valid_p=*/false);
26925 /* Look for the nested-name-specifier. */
26926 cp_parser_nested_name_specifier_opt (parser,
26927 /*typename_keyword_p=*/false,
26928 /*check_dependency_p=*/true,
26929 /*type_p=*/false,
26930 /*is_declaration=*/false);
26932 cp_token *token = cp_lexer_peek_token (parser->lexer);
26933 tree concept_name = cp_parser_identifier (parser);
26935 /* Look up the concept for which we will be matching
26936 template parameters. */
26937 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26938 token->location);
26939 parser->scope = saved_scope;
26940 parser->object_scope = saved_object_scope;
26941 parser->qualifying_scope = saved_qualifying_scope;
26943 if (concept_name == error_mark_node)
26944 cp_parser_simulate_error (parser);
26946 /* Look for opening brace for introduction. */
26947 matching_braces braces;
26948 braces.require_open (parser);
26950 if (!cp_parser_parse_definitely (parser))
26951 return false;
26953 push_deferring_access_checks (dk_deferred);
26955 /* Build vector of placeholder parameters and grab
26956 matching identifiers. */
26957 tree introduction_list = cp_parser_introduction_list (parser);
26959 /* The introduction-list shall not be empty. */
26960 int nargs = TREE_VEC_LENGTH (introduction_list);
26961 if (nargs == 0)
26963 error ("empty introduction-list");
26964 return true;
26967 /* Look for closing brace for introduction. */
26968 if (!braces.require_close (parser))
26969 return true;
26971 if (tmpl_decl == error_mark_node)
26973 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26974 token->location);
26975 return true;
26978 /* Build and associate the constraint. */
26979 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26980 if (parms && parms != error_mark_node)
26982 cp_parser_template_declaration_after_parameters (parser, parms,
26983 member_p);
26984 return true;
26987 error_at (token->location, "no matching concept for template-introduction");
26988 return true;
26991 /* Parse a normal template-declaration following the template keyword. */
26993 static void
26994 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26996 tree parameter_list;
26997 bool need_lang_pop;
26998 location_t location = input_location;
27000 /* Look for the `<' token. */
27001 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27002 return;
27003 if (at_class_scope_p () && current_function_decl)
27005 /* 14.5.2.2 [temp.mem]
27007 A local class shall not have member templates. */
27008 error_at (location,
27009 "invalid declaration of member template in local class");
27010 cp_parser_skip_to_end_of_block_or_statement (parser);
27011 return;
27013 /* [temp]
27015 A template ... shall not have C linkage. */
27016 if (current_lang_name == lang_name_c)
27018 error_at (location, "template with C linkage");
27019 maybe_show_extern_c_location ();
27020 /* Give it C++ linkage to avoid confusing other parts of the
27021 front end. */
27022 push_lang_context (lang_name_cplusplus);
27023 need_lang_pop = true;
27025 else
27026 need_lang_pop = false;
27028 /* We cannot perform access checks on the template parameter
27029 declarations until we know what is being declared, just as we
27030 cannot check the decl-specifier list. */
27031 push_deferring_access_checks (dk_deferred);
27033 /* If the next token is `>', then we have an invalid
27034 specialization. Rather than complain about an invalid template
27035 parameter, issue an error message here. */
27036 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27038 cp_parser_error (parser, "invalid explicit specialization");
27039 begin_specialization ();
27040 parameter_list = NULL_TREE;
27042 else
27044 /* Parse the template parameters. */
27045 parameter_list = cp_parser_template_parameter_list (parser);
27048 /* Look for the `>'. */
27049 cp_parser_skip_to_end_of_template_parameter_list (parser);
27051 /* Manage template requirements */
27052 if (flag_concepts)
27054 tree reqs = get_shorthand_constraints (current_template_parms);
27055 if (tree r = cp_parser_requires_clause_opt (parser))
27056 reqs = conjoin_constraints (reqs, normalize_expression (r));
27057 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27060 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27061 member_p);
27063 /* For the erroneous case of a template with C linkage, we pushed an
27064 implicit C++ linkage scope; exit that scope now. */
27065 if (need_lang_pop)
27066 pop_lang_context ();
27069 /* Parse a template-declaration, assuming that the `export' (and
27070 `extern') keywords, if present, has already been scanned. MEMBER_P
27071 is as for cp_parser_template_declaration. */
27073 static bool
27074 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27076 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27078 cp_lexer_consume_token (parser->lexer);
27079 cp_parser_explicit_template_declaration (parser, member_p);
27080 return true;
27082 else if (flag_concepts)
27083 return cp_parser_template_introduction (parser, member_p);
27085 return false;
27088 /* Perform the deferred access checks from a template-parameter-list.
27089 CHECKS is a TREE_LIST of access checks, as returned by
27090 get_deferred_access_checks. */
27092 static void
27093 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27095 ++processing_template_parmlist;
27096 perform_access_checks (checks, tf_warning_or_error);
27097 --processing_template_parmlist;
27100 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27101 `function-definition' sequence that follows a template header.
27102 If MEMBER_P is true, this declaration appears in a class scope.
27104 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27105 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27107 static tree
27108 cp_parser_single_declaration (cp_parser* parser,
27109 vec<deferred_access_check, va_gc> *checks,
27110 bool member_p,
27111 bool explicit_specialization_p,
27112 bool* friend_p)
27114 int declares_class_or_enum;
27115 tree decl = NULL_TREE;
27116 cp_decl_specifier_seq decl_specifiers;
27117 bool function_definition_p = false;
27118 cp_token *decl_spec_token_start;
27120 /* This function is only used when processing a template
27121 declaration. */
27122 gcc_assert (innermost_scope_kind () == sk_template_parms
27123 || innermost_scope_kind () == sk_template_spec);
27125 /* Defer access checks until we know what is being declared. */
27126 push_deferring_access_checks (dk_deferred);
27128 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27129 alternative. */
27130 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27131 cp_parser_decl_specifier_seq (parser,
27132 CP_PARSER_FLAGS_OPTIONAL,
27133 &decl_specifiers,
27134 &declares_class_or_enum);
27135 if (friend_p)
27136 *friend_p = cp_parser_friend_p (&decl_specifiers);
27138 /* There are no template typedefs. */
27139 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27141 error_at (decl_spec_token_start->location,
27142 "template declaration of %<typedef%>");
27143 decl = error_mark_node;
27146 /* Gather up the access checks that occurred the
27147 decl-specifier-seq. */
27148 stop_deferring_access_checks ();
27150 /* Check for the declaration of a template class. */
27151 if (declares_class_or_enum)
27153 if (cp_parser_declares_only_class_p (parser)
27154 || (declares_class_or_enum & 2))
27156 // If this is a declaration, but not a definition, associate
27157 // any constraints with the type declaration. Constraints
27158 // are associated with definitions in cp_parser_class_specifier.
27159 if (declares_class_or_enum == 1)
27160 associate_classtype_constraints (decl_specifiers.type);
27162 decl = shadow_tag (&decl_specifiers);
27164 /* In this case:
27166 struct C {
27167 friend template <typename T> struct A<T>::B;
27170 A<T>::B will be represented by a TYPENAME_TYPE, and
27171 therefore not recognized by shadow_tag. */
27172 if (friend_p && *friend_p
27173 && !decl
27174 && decl_specifiers.type
27175 && TYPE_P (decl_specifiers.type))
27176 decl = decl_specifiers.type;
27178 if (decl && decl != error_mark_node)
27179 decl = TYPE_NAME (decl);
27180 else
27181 decl = error_mark_node;
27183 /* Perform access checks for template parameters. */
27184 cp_parser_perform_template_parameter_access_checks (checks);
27186 /* Give a helpful diagnostic for
27187 template <class T> struct A { } a;
27188 if we aren't already recovering from an error. */
27189 if (!cp_parser_declares_only_class_p (parser)
27190 && !seen_error ())
27192 error_at (cp_lexer_peek_token (parser->lexer)->location,
27193 "a class template declaration must not declare "
27194 "anything else");
27195 cp_parser_skip_to_end_of_block_or_statement (parser);
27196 goto out;
27201 /* Complain about missing 'typename' or other invalid type names. */
27202 if (!decl_specifiers.any_type_specifiers_p
27203 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27205 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27206 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27207 the rest of this declaration. */
27208 decl = error_mark_node;
27209 goto out;
27212 /* If it's not a template class, try for a template function. If
27213 the next token is a `;', then this declaration does not declare
27214 anything. But, if there were errors in the decl-specifiers, then
27215 the error might well have come from an attempted class-specifier.
27216 In that case, there's no need to warn about a missing declarator. */
27217 if (!decl
27218 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27219 || decl_specifiers.type != error_mark_node))
27221 decl = cp_parser_init_declarator (parser,
27222 &decl_specifiers,
27223 checks,
27224 /*function_definition_allowed_p=*/true,
27225 member_p,
27226 declares_class_or_enum,
27227 &function_definition_p,
27228 NULL, NULL, NULL);
27230 /* 7.1.1-1 [dcl.stc]
27232 A storage-class-specifier shall not be specified in an explicit
27233 specialization... */
27234 if (decl
27235 && explicit_specialization_p
27236 && decl_specifiers.storage_class != sc_none)
27238 error_at (decl_spec_token_start->location,
27239 "explicit template specialization cannot have a storage class");
27240 decl = error_mark_node;
27243 if (decl && VAR_P (decl))
27244 check_template_variable (decl);
27247 /* Look for a trailing `;' after the declaration. */
27248 if (!function_definition_p
27249 && (decl == error_mark_node
27250 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27251 cp_parser_skip_to_end_of_block_or_statement (parser);
27253 out:
27254 pop_deferring_access_checks ();
27256 /* Clear any current qualification; whatever comes next is the start
27257 of something new. */
27258 parser->scope = NULL_TREE;
27259 parser->qualifying_scope = NULL_TREE;
27260 parser->object_scope = NULL_TREE;
27262 return decl;
27265 /* Parse a cast-expression that is not the operand of a unary "&". */
27267 static cp_expr
27268 cp_parser_simple_cast_expression (cp_parser *parser)
27270 return cp_parser_cast_expression (parser, /*address_p=*/false,
27271 /*cast_p=*/false, /*decltype*/false, NULL);
27274 /* Parse a functional cast to TYPE. Returns an expression
27275 representing the cast. */
27277 static cp_expr
27278 cp_parser_functional_cast (cp_parser* parser, tree type)
27280 vec<tree, va_gc> *vec;
27281 tree expression_list;
27282 cp_expr cast;
27283 bool nonconst_p;
27285 location_t start_loc = input_location;
27287 if (!type)
27288 type = error_mark_node;
27290 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27292 cp_lexer_set_source_position (parser->lexer);
27293 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27294 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27295 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27296 if (TREE_CODE (type) == TYPE_DECL)
27297 type = TREE_TYPE (type);
27299 cast = finish_compound_literal (type, expression_list,
27300 tf_warning_or_error, fcl_functional);
27301 /* Create a location of the form:
27302 type_name{i, f}
27303 ^~~~~~~~~~~~~~~
27304 with caret == start at the start of the type name,
27305 finishing at the closing brace. */
27306 location_t finish_loc
27307 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27308 location_t combined_loc = make_location (start_loc, start_loc,
27309 finish_loc);
27310 cast.set_location (combined_loc);
27311 return cast;
27315 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27316 /*cast_p=*/true,
27317 /*allow_expansion_p=*/true,
27318 /*non_constant_p=*/NULL);
27319 if (vec == NULL)
27320 expression_list = error_mark_node;
27321 else
27323 expression_list = build_tree_list_vec (vec);
27324 release_tree_vector (vec);
27327 cast = build_functional_cast (type, expression_list,
27328 tf_warning_or_error);
27329 /* [expr.const]/1: In an integral constant expression "only type
27330 conversions to integral or enumeration type can be used". */
27331 if (TREE_CODE (type) == TYPE_DECL)
27332 type = TREE_TYPE (type);
27333 if (cast != error_mark_node
27334 && !cast_valid_in_integral_constant_expression_p (type)
27335 && cp_parser_non_integral_constant_expression (parser,
27336 NIC_CONSTRUCTOR))
27337 return error_mark_node;
27339 /* Create a location of the form:
27340 float(i)
27341 ^~~~~~~~
27342 with caret == start at the start of the type name,
27343 finishing at the closing paren. */
27344 location_t finish_loc
27345 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27346 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27347 cast.set_location (combined_loc);
27348 return cast;
27351 /* Save the tokens that make up the body of a member function defined
27352 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27353 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27354 specifiers applied to the declaration. Returns the FUNCTION_DECL
27355 for the member function. */
27357 static tree
27358 cp_parser_save_member_function_body (cp_parser* parser,
27359 cp_decl_specifier_seq *decl_specifiers,
27360 cp_declarator *declarator,
27361 tree attributes)
27363 cp_token *first;
27364 cp_token *last;
27365 tree fn;
27366 bool function_try_block = false;
27368 /* Create the FUNCTION_DECL. */
27369 fn = grokmethod (decl_specifiers, declarator, attributes);
27370 cp_finalize_omp_declare_simd (parser, fn);
27371 cp_finalize_oacc_routine (parser, fn, true);
27372 /* If something went badly wrong, bail out now. */
27373 if (fn == error_mark_node)
27375 /* If there's a function-body, skip it. */
27376 if (cp_parser_token_starts_function_definition_p
27377 (cp_lexer_peek_token (parser->lexer)))
27378 cp_parser_skip_to_end_of_block_or_statement (parser);
27379 return error_mark_node;
27382 /* Remember it, if there default args to post process. */
27383 cp_parser_save_default_args (parser, fn);
27385 /* Save away the tokens that make up the body of the
27386 function. */
27387 first = parser->lexer->next_token;
27389 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27390 cp_lexer_consume_token (parser->lexer);
27391 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27392 RID_TRANSACTION_ATOMIC))
27394 cp_lexer_consume_token (parser->lexer);
27395 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27396 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27397 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27398 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27399 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27400 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27401 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27403 cp_lexer_consume_token (parser->lexer);
27404 cp_lexer_consume_token (parser->lexer);
27405 cp_lexer_consume_token (parser->lexer);
27406 cp_lexer_consume_token (parser->lexer);
27407 cp_lexer_consume_token (parser->lexer);
27409 else
27410 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27411 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27413 cp_lexer_consume_token (parser->lexer);
27414 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27415 break;
27419 /* Handle function try blocks. */
27420 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27422 cp_lexer_consume_token (parser->lexer);
27423 function_try_block = true;
27425 /* We can have braced-init-list mem-initializers before the fn body. */
27426 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27428 cp_lexer_consume_token (parser->lexer);
27429 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27431 /* cache_group will stop after an un-nested { } pair, too. */
27432 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27433 break;
27435 /* variadic mem-inits have ... after the ')'. */
27436 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27437 cp_lexer_consume_token (parser->lexer);
27440 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27441 /* Handle function try blocks. */
27442 if (function_try_block)
27443 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27444 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27445 last = parser->lexer->next_token;
27447 /* Save away the inline definition; we will process it when the
27448 class is complete. */
27449 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27450 DECL_PENDING_INLINE_P (fn) = 1;
27452 /* We need to know that this was defined in the class, so that
27453 friend templates are handled correctly. */
27454 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27456 /* Add FN to the queue of functions to be parsed later. */
27457 vec_safe_push (unparsed_funs_with_definitions, fn);
27459 return fn;
27462 /* Save the tokens that make up the in-class initializer for a non-static
27463 data member. Returns a DEFAULT_ARG. */
27465 static tree
27466 cp_parser_save_nsdmi (cp_parser* parser)
27468 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27471 /* Parse a template-argument-list, as well as the trailing ">" (but
27472 not the opening "<"). See cp_parser_template_argument_list for the
27473 return value. */
27475 static tree
27476 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27478 tree arguments;
27479 tree saved_scope;
27480 tree saved_qualifying_scope;
27481 tree saved_object_scope;
27482 bool saved_greater_than_is_operator_p;
27483 int saved_unevaluated_operand;
27484 int saved_inhibit_evaluation_warnings;
27486 /* [temp.names]
27488 When parsing a template-id, the first non-nested `>' is taken as
27489 the end of the template-argument-list rather than a greater-than
27490 operator. */
27491 saved_greater_than_is_operator_p
27492 = parser->greater_than_is_operator_p;
27493 parser->greater_than_is_operator_p = false;
27494 /* Parsing the argument list may modify SCOPE, so we save it
27495 here. */
27496 saved_scope = parser->scope;
27497 saved_qualifying_scope = parser->qualifying_scope;
27498 saved_object_scope = parser->object_scope;
27499 /* We need to evaluate the template arguments, even though this
27500 template-id may be nested within a "sizeof". */
27501 saved_unevaluated_operand = cp_unevaluated_operand;
27502 cp_unevaluated_operand = 0;
27503 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27504 c_inhibit_evaluation_warnings = 0;
27505 /* Parse the template-argument-list itself. */
27506 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27507 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27508 arguments = NULL_TREE;
27509 else
27510 arguments = cp_parser_template_argument_list (parser);
27511 /* Look for the `>' that ends the template-argument-list. If we find
27512 a '>>' instead, it's probably just a typo. */
27513 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27515 if (cxx_dialect != cxx98)
27517 /* In C++0x, a `>>' in a template argument list or cast
27518 expression is considered to be two separate `>'
27519 tokens. So, change the current token to a `>', but don't
27520 consume it: it will be consumed later when the outer
27521 template argument list (or cast expression) is parsed.
27522 Note that this replacement of `>' for `>>' is necessary
27523 even if we are parsing tentatively: in the tentative
27524 case, after calling
27525 cp_parser_enclosed_template_argument_list we will always
27526 throw away all of the template arguments and the first
27527 closing `>', either because the template argument list
27528 was erroneous or because we are replacing those tokens
27529 with a CPP_TEMPLATE_ID token. The second `>' (which will
27530 not have been thrown away) is needed either to close an
27531 outer template argument list or to complete a new-style
27532 cast. */
27533 cp_token *token = cp_lexer_peek_token (parser->lexer);
27534 token->type = CPP_GREATER;
27536 else if (!saved_greater_than_is_operator_p)
27538 /* If we're in a nested template argument list, the '>>' has
27539 to be a typo for '> >'. We emit the error message, but we
27540 continue parsing and we push a '>' as next token, so that
27541 the argument list will be parsed correctly. Note that the
27542 global source location is still on the token before the
27543 '>>', so we need to say explicitly where we want it. */
27544 cp_token *token = cp_lexer_peek_token (parser->lexer);
27545 gcc_rich_location richloc (token->location);
27546 richloc.add_fixit_replace ("> >");
27547 error_at (&richloc, "%<>>%> should be %<> >%> "
27548 "within a nested template argument list");
27550 token->type = CPP_GREATER;
27552 else
27554 /* If this is not a nested template argument list, the '>>'
27555 is a typo for '>'. Emit an error message and continue.
27556 Same deal about the token location, but here we can get it
27557 right by consuming the '>>' before issuing the diagnostic. */
27558 cp_token *token = cp_lexer_consume_token (parser->lexer);
27559 error_at (token->location,
27560 "spurious %<>>%>, use %<>%> to terminate "
27561 "a template argument list");
27564 else
27565 cp_parser_skip_to_end_of_template_parameter_list (parser);
27566 /* The `>' token might be a greater-than operator again now. */
27567 parser->greater_than_is_operator_p
27568 = saved_greater_than_is_operator_p;
27569 /* Restore the SAVED_SCOPE. */
27570 parser->scope = saved_scope;
27571 parser->qualifying_scope = saved_qualifying_scope;
27572 parser->object_scope = saved_object_scope;
27573 cp_unevaluated_operand = saved_unevaluated_operand;
27574 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27576 return arguments;
27579 /* MEMBER_FUNCTION is a member function, or a friend. If default
27580 arguments, or the body of the function have not yet been parsed,
27581 parse them now. */
27583 static void
27584 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27586 timevar_push (TV_PARSE_INMETH);
27587 /* If this member is a template, get the underlying
27588 FUNCTION_DECL. */
27589 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27590 member_function = DECL_TEMPLATE_RESULT (member_function);
27592 /* There should not be any class definitions in progress at this
27593 point; the bodies of members are only parsed outside of all class
27594 definitions. */
27595 gcc_assert (parser->num_classes_being_defined == 0);
27596 /* While we're parsing the member functions we might encounter more
27597 classes. We want to handle them right away, but we don't want
27598 them getting mixed up with functions that are currently in the
27599 queue. */
27600 push_unparsed_function_queues (parser);
27602 /* Make sure that any template parameters are in scope. */
27603 maybe_begin_member_template_processing (member_function);
27605 /* If the body of the function has not yet been parsed, parse it
27606 now. */
27607 if (DECL_PENDING_INLINE_P (member_function))
27609 tree function_scope;
27610 cp_token_cache *tokens;
27612 /* The function is no longer pending; we are processing it. */
27613 tokens = DECL_PENDING_INLINE_INFO (member_function);
27614 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27615 DECL_PENDING_INLINE_P (member_function) = 0;
27617 /* If this is a local class, enter the scope of the containing
27618 function. */
27619 function_scope = current_function_decl;
27620 if (function_scope)
27621 push_function_context ();
27623 /* Push the body of the function onto the lexer stack. */
27624 cp_parser_push_lexer_for_tokens (parser, tokens);
27626 /* Let the front end know that we going to be defining this
27627 function. */
27628 start_preparsed_function (member_function, NULL_TREE,
27629 SF_PRE_PARSED | SF_INCLASS_INLINE);
27631 /* Don't do access checking if it is a templated function. */
27632 if (processing_template_decl)
27633 push_deferring_access_checks (dk_no_check);
27635 /* #pragma omp declare reduction needs special parsing. */
27636 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27638 parser->lexer->in_pragma = true;
27639 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27640 finish_function (/*inline_p=*/true);
27641 cp_check_omp_declare_reduction (member_function);
27643 else
27644 /* Now, parse the body of the function. */
27645 cp_parser_function_definition_after_declarator (parser,
27646 /*inline_p=*/true);
27648 if (processing_template_decl)
27649 pop_deferring_access_checks ();
27651 /* Leave the scope of the containing function. */
27652 if (function_scope)
27653 pop_function_context ();
27654 cp_parser_pop_lexer (parser);
27657 /* Remove any template parameters from the symbol table. */
27658 maybe_end_member_template_processing ();
27660 /* Restore the queue. */
27661 pop_unparsed_function_queues (parser);
27662 timevar_pop (TV_PARSE_INMETH);
27665 /* If DECL contains any default args, remember it on the unparsed
27666 functions queue. */
27668 static void
27669 cp_parser_save_default_args (cp_parser* parser, tree decl)
27671 tree probe;
27673 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27674 probe;
27675 probe = TREE_CHAIN (probe))
27676 if (TREE_PURPOSE (probe))
27678 cp_default_arg_entry entry = {current_class_type, decl};
27679 vec_safe_push (unparsed_funs_with_default_args, entry);
27680 break;
27684 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27685 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27686 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27687 from the parameter-type-list. */
27689 static tree
27690 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27691 tree default_arg, tree parmtype)
27693 cp_token_cache *tokens;
27694 tree parsed_arg;
27695 bool dummy;
27697 if (default_arg == error_mark_node)
27698 return error_mark_node;
27700 /* Push the saved tokens for the default argument onto the parser's
27701 lexer stack. */
27702 tokens = DEFARG_TOKENS (default_arg);
27703 cp_parser_push_lexer_for_tokens (parser, tokens);
27705 start_lambda_scope (decl);
27707 /* Parse the default argument. */
27708 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27709 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27710 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27712 finish_lambda_scope ();
27714 if (parsed_arg == error_mark_node)
27715 cp_parser_skip_to_end_of_statement (parser);
27717 if (!processing_template_decl)
27719 /* In a non-template class, check conversions now. In a template,
27720 we'll wait and instantiate these as needed. */
27721 if (TREE_CODE (decl) == PARM_DECL)
27722 parsed_arg = check_default_argument (parmtype, parsed_arg,
27723 tf_warning_or_error);
27724 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27725 parsed_arg = error_mark_node;
27726 else
27727 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27730 /* If the token stream has not been completely used up, then
27731 there was extra junk after the end of the default
27732 argument. */
27733 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27735 if (TREE_CODE (decl) == PARM_DECL)
27736 cp_parser_error (parser, "expected %<,%>");
27737 else
27738 cp_parser_error (parser, "expected %<;%>");
27741 /* Revert to the main lexer. */
27742 cp_parser_pop_lexer (parser);
27744 return parsed_arg;
27747 /* FIELD is a non-static data member with an initializer which we saved for
27748 later; parse it now. */
27750 static void
27751 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27753 tree def;
27755 maybe_begin_member_template_processing (field);
27757 push_unparsed_function_queues (parser);
27758 def = cp_parser_late_parse_one_default_arg (parser, field,
27759 DECL_INITIAL (field),
27760 NULL_TREE);
27761 pop_unparsed_function_queues (parser);
27763 maybe_end_member_template_processing ();
27765 DECL_INITIAL (field) = def;
27768 /* FN is a FUNCTION_DECL which may contains a parameter with an
27769 unparsed DEFAULT_ARG. Parse the default args now. This function
27770 assumes that the current scope is the scope in which the default
27771 argument should be processed. */
27773 static void
27774 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27776 bool saved_local_variables_forbidden_p;
27777 tree parm, parmdecl;
27779 /* While we're parsing the default args, we might (due to the
27780 statement expression extension) encounter more classes. We want
27781 to handle them right away, but we don't want them getting mixed
27782 up with default args that are currently in the queue. */
27783 push_unparsed_function_queues (parser);
27785 /* Local variable names (and the `this' keyword) may not appear
27786 in a default argument. */
27787 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27788 parser->local_variables_forbidden_p = true;
27790 push_defarg_context (fn);
27792 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27793 parmdecl = DECL_ARGUMENTS (fn);
27794 parm && parm != void_list_node;
27795 parm = TREE_CHAIN (parm),
27796 parmdecl = DECL_CHAIN (parmdecl))
27798 tree default_arg = TREE_PURPOSE (parm);
27799 tree parsed_arg;
27800 vec<tree, va_gc> *insts;
27801 tree copy;
27802 unsigned ix;
27804 if (!default_arg)
27805 continue;
27807 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27808 /* This can happen for a friend declaration for a function
27809 already declared with default arguments. */
27810 continue;
27812 parsed_arg
27813 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27814 default_arg,
27815 TREE_VALUE (parm));
27816 TREE_PURPOSE (parm) = parsed_arg;
27818 /* Update any instantiations we've already created. */
27819 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27820 vec_safe_iterate (insts, ix, &copy); ix++)
27821 TREE_PURPOSE (copy) = parsed_arg;
27824 pop_defarg_context ();
27826 /* Make sure no default arg is missing. */
27827 check_default_args (fn);
27829 /* Restore the state of local_variables_forbidden_p. */
27830 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27832 /* Restore the queue. */
27833 pop_unparsed_function_queues (parser);
27836 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27838 sizeof ... ( identifier )
27840 where the 'sizeof' token has already been consumed. */
27842 static tree
27843 cp_parser_sizeof_pack (cp_parser *parser)
27845 /* Consume the `...'. */
27846 cp_lexer_consume_token (parser->lexer);
27847 maybe_warn_variadic_templates ();
27849 matching_parens parens;
27850 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27851 if (paren)
27852 parens.consume_open (parser);
27853 else
27854 permerror (cp_lexer_peek_token (parser->lexer)->location,
27855 "%<sizeof...%> argument must be surrounded by parentheses");
27857 cp_token *token = cp_lexer_peek_token (parser->lexer);
27858 tree name = cp_parser_identifier (parser);
27859 if (name == error_mark_node)
27860 return error_mark_node;
27861 /* The name is not qualified. */
27862 parser->scope = NULL_TREE;
27863 parser->qualifying_scope = NULL_TREE;
27864 parser->object_scope = NULL_TREE;
27865 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27866 if (expr == error_mark_node)
27867 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27868 token->location);
27869 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27870 expr = TREE_TYPE (expr);
27871 else if (TREE_CODE (expr) == CONST_DECL)
27872 expr = DECL_INITIAL (expr);
27873 expr = make_pack_expansion (expr);
27874 PACK_EXPANSION_SIZEOF_P (expr) = true;
27876 if (paren)
27877 parens.require_close (parser);
27879 return expr;
27882 /* Parse the operand of `sizeof' (or a similar operator). Returns
27883 either a TYPE or an expression, depending on the form of the
27884 input. The KEYWORD indicates which kind of expression we have
27885 encountered. */
27887 static tree
27888 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27890 tree expr = NULL_TREE;
27891 const char *saved_message;
27892 char *tmp;
27893 bool saved_integral_constant_expression_p;
27894 bool saved_non_integral_constant_expression_p;
27896 /* If it's a `...', then we are computing the length of a parameter
27897 pack. */
27898 if (keyword == RID_SIZEOF
27899 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27900 return cp_parser_sizeof_pack (parser);
27902 /* Types cannot be defined in a `sizeof' expression. Save away the
27903 old message. */
27904 saved_message = parser->type_definition_forbidden_message;
27905 /* And create the new one. */
27906 tmp = concat ("types may not be defined in %<",
27907 IDENTIFIER_POINTER (ridpointers[keyword]),
27908 "%> expressions", NULL);
27909 parser->type_definition_forbidden_message = tmp;
27911 /* The restrictions on constant-expressions do not apply inside
27912 sizeof expressions. */
27913 saved_integral_constant_expression_p
27914 = parser->integral_constant_expression_p;
27915 saved_non_integral_constant_expression_p
27916 = parser->non_integral_constant_expression_p;
27917 parser->integral_constant_expression_p = false;
27919 /* Do not actually evaluate the expression. */
27920 ++cp_unevaluated_operand;
27921 ++c_inhibit_evaluation_warnings;
27922 /* If it's a `(', then we might be looking at the type-id
27923 construction. */
27924 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27926 tree type = NULL_TREE;
27928 /* We can't be sure yet whether we're looking at a type-id or an
27929 expression. */
27930 cp_parser_parse_tentatively (parser);
27932 matching_parens parens;
27933 parens.consume_open (parser);
27935 /* Note: as a GNU Extension, compound literals are considered
27936 postfix-expressions as they are in C99, so they are valid
27937 arguments to sizeof. See comment in cp_parser_cast_expression
27938 for details. */
27939 if (cp_parser_compound_literal_p (parser))
27940 cp_parser_simulate_error (parser);
27941 else
27943 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27944 parser->in_type_id_in_expr_p = true;
27945 /* Look for the type-id. */
27946 type = cp_parser_type_id (parser);
27947 /* Look for the closing `)'. */
27948 parens.require_close (parser);
27949 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27952 /* If all went well, then we're done. */
27953 if (cp_parser_parse_definitely (parser))
27955 cp_decl_specifier_seq decl_specs;
27957 /* Build a trivial decl-specifier-seq. */
27958 clear_decl_specs (&decl_specs);
27959 decl_specs.type = type;
27961 /* Call grokdeclarator to figure out what type this is. */
27962 expr = grokdeclarator (NULL,
27963 &decl_specs,
27964 TYPENAME,
27965 /*initialized=*/0,
27966 /*attrlist=*/NULL);
27970 /* If the type-id production did not work out, then we must be
27971 looking at the unary-expression production. */
27972 if (!expr)
27973 expr = cp_parser_unary_expression (parser);
27975 /* Go back to evaluating expressions. */
27976 --cp_unevaluated_operand;
27977 --c_inhibit_evaluation_warnings;
27979 /* Free the message we created. */
27980 free (tmp);
27981 /* And restore the old one. */
27982 parser->type_definition_forbidden_message = saved_message;
27983 parser->integral_constant_expression_p
27984 = saved_integral_constant_expression_p;
27985 parser->non_integral_constant_expression_p
27986 = saved_non_integral_constant_expression_p;
27988 return expr;
27991 /* If the current declaration has no declarator, return true. */
27993 static bool
27994 cp_parser_declares_only_class_p (cp_parser *parser)
27996 /* If the next token is a `;' or a `,' then there is no
27997 declarator. */
27998 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27999 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28002 /* Update the DECL_SPECS to reflect the storage class indicated by
28003 KEYWORD. */
28005 static void
28006 cp_parser_set_storage_class (cp_parser *parser,
28007 cp_decl_specifier_seq *decl_specs,
28008 enum rid keyword,
28009 cp_token *token)
28011 cp_storage_class storage_class;
28013 if (parser->in_unbraced_linkage_specification_p)
28015 error_at (token->location, "invalid use of %qD in linkage specification",
28016 ridpointers[keyword]);
28017 return;
28019 else if (decl_specs->storage_class != sc_none)
28021 decl_specs->conflicting_specifiers_p = true;
28022 return;
28025 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28026 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28027 && decl_specs->gnu_thread_keyword_p)
28029 pedwarn (decl_specs->locations[ds_thread], 0,
28030 "%<__thread%> before %qD", ridpointers[keyword]);
28033 switch (keyword)
28035 case RID_AUTO:
28036 storage_class = sc_auto;
28037 break;
28038 case RID_REGISTER:
28039 storage_class = sc_register;
28040 break;
28041 case RID_STATIC:
28042 storage_class = sc_static;
28043 break;
28044 case RID_EXTERN:
28045 storage_class = sc_extern;
28046 break;
28047 case RID_MUTABLE:
28048 storage_class = sc_mutable;
28049 break;
28050 default:
28051 gcc_unreachable ();
28053 decl_specs->storage_class = storage_class;
28054 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28056 /* A storage class specifier cannot be applied alongside a typedef
28057 specifier. If there is a typedef specifier present then set
28058 conflicting_specifiers_p which will trigger an error later
28059 on in grokdeclarator. */
28060 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28061 decl_specs->conflicting_specifiers_p = true;
28064 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28065 is true, the type is a class or enum definition. */
28067 static void
28068 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28069 tree type_spec,
28070 cp_token *token,
28071 bool type_definition_p)
28073 decl_specs->any_specifiers_p = true;
28075 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28076 (with, for example, in "typedef int wchar_t;") we remember that
28077 this is what happened. In system headers, we ignore these
28078 declarations so that G++ can work with system headers that are not
28079 C++-safe. */
28080 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28081 && !type_definition_p
28082 && (type_spec == boolean_type_node
28083 || type_spec == char16_type_node
28084 || type_spec == char32_type_node
28085 || type_spec == wchar_type_node)
28086 && (decl_specs->type
28087 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28088 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28089 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28090 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28092 decl_specs->redefined_builtin_type = type_spec;
28093 set_and_check_decl_spec_loc (decl_specs,
28094 ds_redefined_builtin_type_spec,
28095 token);
28096 if (!decl_specs->type)
28098 decl_specs->type = type_spec;
28099 decl_specs->type_definition_p = false;
28100 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28103 else if (decl_specs->type)
28104 decl_specs->multiple_types_p = true;
28105 else
28107 decl_specs->type = type_spec;
28108 decl_specs->type_definition_p = type_definition_p;
28109 decl_specs->redefined_builtin_type = NULL_TREE;
28110 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28114 /* True iff TOKEN is the GNU keyword __thread. */
28116 static bool
28117 token_is__thread (cp_token *token)
28119 gcc_assert (token->keyword == RID_THREAD);
28120 return id_equal (token->u.value, "__thread");
28123 /* Set the location for a declarator specifier and check if it is
28124 duplicated.
28126 DECL_SPECS is the sequence of declarator specifiers onto which to
28127 set the location.
28129 DS is the single declarator specifier to set which location is to
28130 be set onto the existing sequence of declarators.
28132 LOCATION is the location for the declarator specifier to
28133 consider. */
28135 static void
28136 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28137 cp_decl_spec ds, cp_token *token)
28139 gcc_assert (ds < ds_last);
28141 if (decl_specs == NULL)
28142 return;
28144 source_location location = token->location;
28146 if (decl_specs->locations[ds] == 0)
28148 decl_specs->locations[ds] = location;
28149 if (ds == ds_thread)
28150 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28152 else
28154 if (ds == ds_long)
28156 if (decl_specs->locations[ds_long_long] != 0)
28157 error_at (location,
28158 "%<long long long%> is too long for GCC");
28159 else
28161 decl_specs->locations[ds_long_long] = location;
28162 pedwarn_cxx98 (location,
28163 OPT_Wlong_long,
28164 "ISO C++ 1998 does not support %<long long%>");
28167 else if (ds == ds_thread)
28169 bool gnu = token_is__thread (token);
28170 if (gnu != decl_specs->gnu_thread_keyword_p)
28171 error_at (location,
28172 "both %<__thread%> and %<thread_local%> specified");
28173 else
28175 gcc_rich_location richloc (location);
28176 richloc.add_fixit_remove ();
28177 error_at (&richloc, "duplicate %qD", token->u.value);
28180 else
28182 static const char *const decl_spec_names[] = {
28183 "signed",
28184 "unsigned",
28185 "short",
28186 "long",
28187 "const",
28188 "volatile",
28189 "restrict",
28190 "inline",
28191 "virtual",
28192 "explicit",
28193 "friend",
28194 "typedef",
28195 "using",
28196 "constexpr",
28197 "__complex"
28199 gcc_rich_location richloc (location);
28200 richloc.add_fixit_remove ();
28201 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28206 /* Return true iff the declarator specifier DS is present in the
28207 sequence of declarator specifiers DECL_SPECS. */
28209 bool
28210 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28211 cp_decl_spec ds)
28213 gcc_assert (ds < ds_last);
28215 if (decl_specs == NULL)
28216 return false;
28218 return decl_specs->locations[ds] != 0;
28221 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28222 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28224 static bool
28225 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28227 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28230 /* Issue an error message indicating that TOKEN_DESC was expected.
28231 If KEYWORD is true, it indicated this function is called by
28232 cp_parser_require_keword and the required token can only be
28233 a indicated keyword.
28235 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28236 within any error as the location of an "opening" token matching
28237 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28238 RT_CLOSE_PAREN). */
28240 static void
28241 cp_parser_required_error (cp_parser *parser,
28242 required_token token_desc,
28243 bool keyword,
28244 location_t matching_location)
28246 if (cp_parser_simulate_error (parser))
28247 return;
28249 const char *gmsgid = NULL;
28250 switch (token_desc)
28252 case RT_NEW:
28253 gmsgid = G_("expected %<new%>");
28254 break;
28255 case RT_DELETE:
28256 gmsgid = G_("expected %<delete%>");
28257 break;
28258 case RT_RETURN:
28259 gmsgid = G_("expected %<return%>");
28260 break;
28261 case RT_WHILE:
28262 gmsgid = G_("expected %<while%>");
28263 break;
28264 case RT_EXTERN:
28265 gmsgid = G_("expected %<extern%>");
28266 break;
28267 case RT_STATIC_ASSERT:
28268 gmsgid = G_("expected %<static_assert%>");
28269 break;
28270 case RT_DECLTYPE:
28271 gmsgid = G_("expected %<decltype%>");
28272 break;
28273 case RT_OPERATOR:
28274 gmsgid = G_("expected %<operator%>");
28275 break;
28276 case RT_CLASS:
28277 gmsgid = G_("expected %<class%>");
28278 break;
28279 case RT_TEMPLATE:
28280 gmsgid = G_("expected %<template%>");
28281 break;
28282 case RT_NAMESPACE:
28283 gmsgid = G_("expected %<namespace%>");
28284 break;
28285 case RT_USING:
28286 gmsgid = G_("expected %<using%>");
28287 break;
28288 case RT_ASM:
28289 gmsgid = G_("expected %<asm%>");
28290 break;
28291 case RT_TRY:
28292 gmsgid = G_("expected %<try%>");
28293 break;
28294 case RT_CATCH:
28295 gmsgid = G_("expected %<catch%>");
28296 break;
28297 case RT_THROW:
28298 gmsgid = G_("expected %<throw%>");
28299 break;
28300 case RT_LABEL:
28301 gmsgid = G_("expected %<__label__%>");
28302 break;
28303 case RT_AT_TRY:
28304 gmsgid = G_("expected %<@try%>");
28305 break;
28306 case RT_AT_SYNCHRONIZED:
28307 gmsgid = G_("expected %<@synchronized%>");
28308 break;
28309 case RT_AT_THROW:
28310 gmsgid = G_("expected %<@throw%>");
28311 break;
28312 case RT_TRANSACTION_ATOMIC:
28313 gmsgid = G_("expected %<__transaction_atomic%>");
28314 break;
28315 case RT_TRANSACTION_RELAXED:
28316 gmsgid = G_("expected %<__transaction_relaxed%>");
28317 break;
28318 default:
28319 break;
28322 if (!gmsgid && !keyword)
28324 switch (token_desc)
28326 case RT_SEMICOLON:
28327 gmsgid = G_("expected %<;%>");
28328 break;
28329 case RT_OPEN_PAREN:
28330 gmsgid = G_("expected %<(%>");
28331 break;
28332 case RT_CLOSE_BRACE:
28333 gmsgid = G_("expected %<}%>");
28334 break;
28335 case RT_OPEN_BRACE:
28336 gmsgid = G_("expected %<{%>");
28337 break;
28338 case RT_CLOSE_SQUARE:
28339 gmsgid = G_("expected %<]%>");
28340 break;
28341 case RT_OPEN_SQUARE:
28342 gmsgid = G_("expected %<[%>");
28343 break;
28344 case RT_COMMA:
28345 gmsgid = G_("expected %<,%>");
28346 break;
28347 case RT_SCOPE:
28348 gmsgid = G_("expected %<::%>");
28349 break;
28350 case RT_LESS:
28351 gmsgid = G_("expected %<<%>");
28352 break;
28353 case RT_GREATER:
28354 gmsgid = G_("expected %<>%>");
28355 break;
28356 case RT_EQ:
28357 gmsgid = G_("expected %<=%>");
28358 break;
28359 case RT_ELLIPSIS:
28360 gmsgid = G_("expected %<...%>");
28361 break;
28362 case RT_MULT:
28363 gmsgid = G_("expected %<*%>");
28364 break;
28365 case RT_COMPL:
28366 gmsgid = G_("expected %<~%>");
28367 break;
28368 case RT_COLON:
28369 gmsgid = G_("expected %<:%>");
28370 break;
28371 case RT_COLON_SCOPE:
28372 gmsgid = G_("expected %<:%> or %<::%>");
28373 break;
28374 case RT_CLOSE_PAREN:
28375 gmsgid = G_("expected %<)%>");
28376 break;
28377 case RT_COMMA_CLOSE_PAREN:
28378 gmsgid = G_("expected %<,%> or %<)%>");
28379 break;
28380 case RT_PRAGMA_EOL:
28381 gmsgid = G_("expected end of line");
28382 break;
28383 case RT_NAME:
28384 gmsgid = G_("expected identifier");
28385 break;
28386 case RT_SELECT:
28387 gmsgid = G_("expected selection-statement");
28388 break;
28389 case RT_ITERATION:
28390 gmsgid = G_("expected iteration-statement");
28391 break;
28392 case RT_JUMP:
28393 gmsgid = G_("expected jump-statement");
28394 break;
28395 case RT_CLASS_KEY:
28396 gmsgid = G_("expected class-key");
28397 break;
28398 case RT_CLASS_TYPENAME_TEMPLATE:
28399 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28400 break;
28401 default:
28402 gcc_unreachable ();
28406 if (gmsgid)
28407 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28411 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28412 issue an error message indicating that TOKEN_DESC was expected.
28414 Returns the token consumed, if the token had the appropriate type.
28415 Otherwise, returns NULL.
28417 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28418 within any error as the location of an "opening" token matching
28419 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28420 RT_CLOSE_PAREN). */
28422 static cp_token *
28423 cp_parser_require (cp_parser* parser,
28424 enum cpp_ttype type,
28425 required_token token_desc,
28426 location_t matching_location)
28428 if (cp_lexer_next_token_is (parser->lexer, type))
28429 return cp_lexer_consume_token (parser->lexer);
28430 else
28432 /* Output the MESSAGE -- unless we're parsing tentatively. */
28433 if (!cp_parser_simulate_error (parser))
28434 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28435 matching_location);
28436 return NULL;
28440 /* An error message is produced if the next token is not '>'.
28441 All further tokens are skipped until the desired token is
28442 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28444 static void
28445 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28447 /* Current level of '< ... >'. */
28448 unsigned level = 0;
28449 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28450 unsigned nesting_depth = 0;
28452 /* Are we ready, yet? If not, issue error message. */
28453 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28454 return;
28456 /* Skip tokens until the desired token is found. */
28457 while (true)
28459 /* Peek at the next token. */
28460 switch (cp_lexer_peek_token (parser->lexer)->type)
28462 case CPP_LESS:
28463 if (!nesting_depth)
28464 ++level;
28465 break;
28467 case CPP_RSHIFT:
28468 if (cxx_dialect == cxx98)
28469 /* C++0x views the `>>' operator as two `>' tokens, but
28470 C++98 does not. */
28471 break;
28472 else if (!nesting_depth && level-- == 0)
28474 /* We've hit a `>>' where the first `>' closes the
28475 template argument list, and the second `>' is
28476 spurious. Just consume the `>>' and stop; we've
28477 already produced at least one error. */
28478 cp_lexer_consume_token (parser->lexer);
28479 return;
28481 /* Fall through for C++0x, so we handle the second `>' in
28482 the `>>'. */
28483 gcc_fallthrough ();
28485 case CPP_GREATER:
28486 if (!nesting_depth && level-- == 0)
28488 /* We've reached the token we want, consume it and stop. */
28489 cp_lexer_consume_token (parser->lexer);
28490 return;
28492 break;
28494 case CPP_OPEN_PAREN:
28495 case CPP_OPEN_SQUARE:
28496 ++nesting_depth;
28497 break;
28499 case CPP_CLOSE_PAREN:
28500 case CPP_CLOSE_SQUARE:
28501 if (nesting_depth-- == 0)
28502 return;
28503 break;
28505 case CPP_EOF:
28506 case CPP_PRAGMA_EOL:
28507 case CPP_SEMICOLON:
28508 case CPP_OPEN_BRACE:
28509 case CPP_CLOSE_BRACE:
28510 /* The '>' was probably forgotten, don't look further. */
28511 return;
28513 default:
28514 break;
28517 /* Consume this token. */
28518 cp_lexer_consume_token (parser->lexer);
28522 /* If the next token is the indicated keyword, consume it. Otherwise,
28523 issue an error message indicating that TOKEN_DESC was expected.
28525 Returns the token consumed, if the token had the appropriate type.
28526 Otherwise, returns NULL. */
28528 static cp_token *
28529 cp_parser_require_keyword (cp_parser* parser,
28530 enum rid keyword,
28531 required_token token_desc)
28533 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28535 if (token && token->keyword != keyword)
28537 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28538 UNKNOWN_LOCATION);
28539 return NULL;
28542 return token;
28545 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28546 function-definition. */
28548 static bool
28549 cp_parser_token_starts_function_definition_p (cp_token* token)
28551 return (/* An ordinary function-body begins with an `{'. */
28552 token->type == CPP_OPEN_BRACE
28553 /* A ctor-initializer begins with a `:'. */
28554 || token->type == CPP_COLON
28555 /* A function-try-block begins with `try'. */
28556 || token->keyword == RID_TRY
28557 /* A function-transaction-block begins with `__transaction_atomic'
28558 or `__transaction_relaxed'. */
28559 || token->keyword == RID_TRANSACTION_ATOMIC
28560 || token->keyword == RID_TRANSACTION_RELAXED
28561 /* The named return value extension begins with `return'. */
28562 || token->keyword == RID_RETURN);
28565 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28566 definition. */
28568 static bool
28569 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28571 cp_token *token;
28573 token = cp_lexer_peek_token (parser->lexer);
28574 return (token->type == CPP_OPEN_BRACE
28575 || (token->type == CPP_COLON
28576 && !parser->colon_doesnt_start_class_def_p));
28579 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28580 C++0x) ending a template-argument. */
28582 static bool
28583 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28585 cp_token *token;
28587 token = cp_lexer_peek_token (parser->lexer);
28588 return (token->type == CPP_COMMA
28589 || token->type == CPP_GREATER
28590 || token->type == CPP_ELLIPSIS
28591 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28594 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28595 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28597 static bool
28598 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28599 size_t n)
28601 cp_token *token;
28603 token = cp_lexer_peek_nth_token (parser->lexer, n);
28604 if (token->type == CPP_LESS)
28605 return true;
28606 /* Check for the sequence `<::' in the original code. It would be lexed as
28607 `[:', where `[' is a digraph, and there is no whitespace before
28608 `:'. */
28609 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28611 cp_token *token2;
28612 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28613 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28614 return true;
28616 return false;
28619 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28620 or none_type otherwise. */
28622 static enum tag_types
28623 cp_parser_token_is_class_key (cp_token* token)
28625 switch (token->keyword)
28627 case RID_CLASS:
28628 return class_type;
28629 case RID_STRUCT:
28630 return record_type;
28631 case RID_UNION:
28632 return union_type;
28634 default:
28635 return none_type;
28639 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28640 or none_type otherwise or if the token is null. */
28642 static enum tag_types
28643 cp_parser_token_is_type_parameter_key (cp_token* token)
28645 if (!token)
28646 return none_type;
28648 switch (token->keyword)
28650 case RID_CLASS:
28651 return class_type;
28652 case RID_TYPENAME:
28653 return typename_type;
28655 default:
28656 return none_type;
28660 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28662 static void
28663 cp_parser_check_class_key (enum tag_types class_key, tree type)
28665 if (type == error_mark_node)
28666 return;
28667 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28669 if (permerror (input_location, "%qs tag used in naming %q#T",
28670 class_key == union_type ? "union"
28671 : class_key == record_type ? "struct" : "class",
28672 type))
28673 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28674 "%q#T was previously declared here", type);
28678 /* Issue an error message if DECL is redeclared with different
28679 access than its original declaration [class.access.spec/3].
28680 This applies to nested classes, nested class templates and
28681 enumerations [class.mem/1]. */
28683 static void
28684 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28686 if (!decl
28687 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28688 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28689 return;
28691 if ((TREE_PRIVATE (decl)
28692 != (current_access_specifier == access_private_node))
28693 || (TREE_PROTECTED (decl)
28694 != (current_access_specifier == access_protected_node)))
28695 error_at (location, "%qD redeclared with different access", decl);
28698 /* Look for the `template' keyword, as a syntactic disambiguator.
28699 Return TRUE iff it is present, in which case it will be
28700 consumed. */
28702 static bool
28703 cp_parser_optional_template_keyword (cp_parser *parser)
28705 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28707 /* In C++98 the `template' keyword can only be used within templates;
28708 outside templates the parser can always figure out what is a
28709 template and what is not. In C++11, per the resolution of DR 468,
28710 `template' is allowed in cases where it is not strictly necessary. */
28711 if (!processing_template_decl
28712 && pedantic && cxx_dialect == cxx98)
28714 cp_token *token = cp_lexer_peek_token (parser->lexer);
28715 pedwarn (token->location, OPT_Wpedantic,
28716 "in C++98 %<template%> (as a disambiguator) is only "
28717 "allowed within templates");
28718 /* If this part of the token stream is rescanned, the same
28719 error message would be generated. So, we purge the token
28720 from the stream. */
28721 cp_lexer_purge_token (parser->lexer);
28722 return false;
28724 else
28726 /* Consume the `template' keyword. */
28727 cp_lexer_consume_token (parser->lexer);
28728 return true;
28731 return false;
28734 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28735 set PARSER->SCOPE, and perform other related actions. */
28737 static void
28738 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28740 struct tree_check *check_value;
28742 /* Get the stored value. */
28743 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28744 /* Set the scope from the stored value. */
28745 parser->scope = saved_checks_value (check_value);
28746 parser->qualifying_scope = check_value->qualifying_scope;
28747 parser->object_scope = NULL_TREE;
28750 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28751 encounter the end of a block before what we were looking for. */
28753 static bool
28754 cp_parser_cache_group (cp_parser *parser,
28755 enum cpp_ttype end,
28756 unsigned depth)
28758 while (true)
28760 cp_token *token = cp_lexer_peek_token (parser->lexer);
28762 /* Abort a parenthesized expression if we encounter a semicolon. */
28763 if ((end == CPP_CLOSE_PAREN || depth == 0)
28764 && token->type == CPP_SEMICOLON)
28765 return true;
28766 /* If we've reached the end of the file, stop. */
28767 if (token->type == CPP_EOF
28768 || (end != CPP_PRAGMA_EOL
28769 && token->type == CPP_PRAGMA_EOL))
28770 return true;
28771 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28772 /* We've hit the end of an enclosing block, so there's been some
28773 kind of syntax error. */
28774 return true;
28776 /* Consume the token. */
28777 cp_lexer_consume_token (parser->lexer);
28778 /* See if it starts a new group. */
28779 if (token->type == CPP_OPEN_BRACE)
28781 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28782 /* In theory this should probably check end == '}', but
28783 cp_parser_save_member_function_body needs it to exit
28784 after either '}' or ')' when called with ')'. */
28785 if (depth == 0)
28786 return false;
28788 else if (token->type == CPP_OPEN_PAREN)
28790 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28791 if (depth == 0 && end == CPP_CLOSE_PAREN)
28792 return false;
28794 else if (token->type == CPP_PRAGMA)
28795 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28796 else if (token->type == end)
28797 return false;
28801 /* Like above, for caching a default argument or NSDMI. Both of these are
28802 terminated by a non-nested comma, but it can be unclear whether or not a
28803 comma is nested in a template argument list unless we do more parsing.
28804 In order to handle this ambiguity, when we encounter a ',' after a '<'
28805 we try to parse what follows as a parameter-declaration-list (in the
28806 case of a default argument) or a member-declarator (in the case of an
28807 NSDMI). If that succeeds, then we stop caching. */
28809 static tree
28810 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28812 unsigned depth = 0;
28813 int maybe_template_id = 0;
28814 cp_token *first_token;
28815 cp_token *token;
28816 tree default_argument;
28818 /* Add tokens until we have processed the entire default
28819 argument. We add the range [first_token, token). */
28820 first_token = cp_lexer_peek_token (parser->lexer);
28821 if (first_token->type == CPP_OPEN_BRACE)
28823 /* For list-initialization, this is straightforward. */
28824 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28825 token = cp_lexer_peek_token (parser->lexer);
28827 else while (true)
28829 bool done = false;
28831 /* Peek at the next token. */
28832 token = cp_lexer_peek_token (parser->lexer);
28833 /* What we do depends on what token we have. */
28834 switch (token->type)
28836 /* In valid code, a default argument must be
28837 immediately followed by a `,' `)', or `...'. */
28838 case CPP_COMMA:
28839 if (depth == 0 && maybe_template_id)
28841 /* If we've seen a '<', we might be in a
28842 template-argument-list. Until Core issue 325 is
28843 resolved, we don't know how this situation ought
28844 to be handled, so try to DTRT. We check whether
28845 what comes after the comma is a valid parameter
28846 declaration list. If it is, then the comma ends
28847 the default argument; otherwise the default
28848 argument continues. */
28849 bool error = false;
28850 cp_token *peek;
28852 /* Set ITALP so cp_parser_parameter_declaration_list
28853 doesn't decide to commit to this parse. */
28854 bool saved_italp = parser->in_template_argument_list_p;
28855 parser->in_template_argument_list_p = true;
28857 cp_parser_parse_tentatively (parser);
28859 if (nsdmi)
28861 /* Parse declarators until we reach a non-comma or
28862 somthing that cannot be an initializer.
28863 Just checking whether we're looking at a single
28864 declarator is insufficient. Consider:
28865 int var = tuple<T,U>::x;
28866 The template parameter 'U' looks exactly like a
28867 declarator. */
28870 int ctor_dtor_or_conv_p;
28871 cp_lexer_consume_token (parser->lexer);
28872 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28873 &ctor_dtor_or_conv_p,
28874 /*parenthesized_p=*/NULL,
28875 /*member_p=*/true,
28876 /*friend_p=*/false);
28877 peek = cp_lexer_peek_token (parser->lexer);
28878 if (cp_parser_error_occurred (parser))
28879 break;
28881 while (peek->type == CPP_COMMA);
28882 /* If we met an '=' or ';' then the original comma
28883 was the end of the NSDMI. Otherwise assume
28884 we're still in the NSDMI. */
28885 error = (peek->type != CPP_EQ
28886 && peek->type != CPP_SEMICOLON);
28888 else
28890 cp_lexer_consume_token (parser->lexer);
28891 begin_scope (sk_function_parms, NULL_TREE);
28892 cp_parser_parameter_declaration_list (parser, &error);
28893 pop_bindings_and_leave_scope ();
28895 if (!cp_parser_error_occurred (parser) && !error)
28896 done = true;
28897 cp_parser_abort_tentative_parse (parser);
28899 parser->in_template_argument_list_p = saved_italp;
28900 break;
28902 /* FALLTHRU */
28903 case CPP_CLOSE_PAREN:
28904 case CPP_ELLIPSIS:
28905 /* If we run into a non-nested `;', `}', or `]',
28906 then the code is invalid -- but the default
28907 argument is certainly over. */
28908 case CPP_SEMICOLON:
28909 case CPP_CLOSE_BRACE:
28910 case CPP_CLOSE_SQUARE:
28911 if (depth == 0
28912 /* Handle correctly int n = sizeof ... ( p ); */
28913 && token->type != CPP_ELLIPSIS)
28914 done = true;
28915 /* Update DEPTH, if necessary. */
28916 else if (token->type == CPP_CLOSE_PAREN
28917 || token->type == CPP_CLOSE_BRACE
28918 || token->type == CPP_CLOSE_SQUARE)
28919 --depth;
28920 break;
28922 case CPP_OPEN_PAREN:
28923 case CPP_OPEN_SQUARE:
28924 case CPP_OPEN_BRACE:
28925 ++depth;
28926 break;
28928 case CPP_LESS:
28929 if (depth == 0)
28930 /* This might be the comparison operator, or it might
28931 start a template argument list. */
28932 ++maybe_template_id;
28933 break;
28935 case CPP_RSHIFT:
28936 if (cxx_dialect == cxx98)
28937 break;
28938 /* Fall through for C++0x, which treats the `>>'
28939 operator like two `>' tokens in certain
28940 cases. */
28941 gcc_fallthrough ();
28943 case CPP_GREATER:
28944 if (depth == 0)
28946 /* This might be an operator, or it might close a
28947 template argument list. But if a previous '<'
28948 started a template argument list, this will have
28949 closed it, so we can't be in one anymore. */
28950 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28951 if (maybe_template_id < 0)
28952 maybe_template_id = 0;
28954 break;
28956 /* If we run out of tokens, issue an error message. */
28957 case CPP_EOF:
28958 case CPP_PRAGMA_EOL:
28959 error_at (token->location, "file ends in default argument");
28960 return error_mark_node;
28962 case CPP_NAME:
28963 case CPP_SCOPE:
28964 /* In these cases, we should look for template-ids.
28965 For example, if the default argument is
28966 `X<int, double>()', we need to do name lookup to
28967 figure out whether or not `X' is a template; if
28968 so, the `,' does not end the default argument.
28970 That is not yet done. */
28971 break;
28973 default:
28974 break;
28977 /* If we've reached the end, stop. */
28978 if (done)
28979 break;
28981 /* Add the token to the token block. */
28982 token = cp_lexer_consume_token (parser->lexer);
28985 /* Create a DEFAULT_ARG to represent the unparsed default
28986 argument. */
28987 default_argument = make_node (DEFAULT_ARG);
28988 DEFARG_TOKENS (default_argument)
28989 = cp_token_cache_new (first_token, token);
28990 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28992 return default_argument;
28995 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
28997 location_t
28998 defarg_location (tree default_argument)
29000 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29001 location_t start = tokens->first->location;
29002 location_t end = tokens->last->location;
29003 return make_location (start, start, end);
29006 /* Begin parsing tentatively. We always save tokens while parsing
29007 tentatively so that if the tentative parsing fails we can restore the
29008 tokens. */
29010 static void
29011 cp_parser_parse_tentatively (cp_parser* parser)
29013 /* Enter a new parsing context. */
29014 parser->context = cp_parser_context_new (parser->context);
29015 /* Begin saving tokens. */
29016 cp_lexer_save_tokens (parser->lexer);
29017 /* In order to avoid repetitive access control error messages,
29018 access checks are queued up until we are no longer parsing
29019 tentatively. */
29020 push_deferring_access_checks (dk_deferred);
29023 /* Commit to the currently active tentative parse. */
29025 static void
29026 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29028 cp_parser_context *context;
29029 cp_lexer *lexer;
29031 /* Mark all of the levels as committed. */
29032 lexer = parser->lexer;
29033 for (context = parser->context; context->next; context = context->next)
29035 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29036 break;
29037 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29038 while (!cp_lexer_saving_tokens (lexer))
29039 lexer = lexer->next;
29040 cp_lexer_commit_tokens (lexer);
29044 /* Commit to the topmost currently active tentative parse.
29046 Note that this function shouldn't be called when there are
29047 irreversible side-effects while in a tentative state. For
29048 example, we shouldn't create a permanent entry in the symbol
29049 table, or issue an error message that might not apply if the
29050 tentative parse is aborted. */
29052 static void
29053 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29055 cp_parser_context *context = parser->context;
29056 cp_lexer *lexer = parser->lexer;
29058 if (context)
29060 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29061 return;
29062 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29064 while (!cp_lexer_saving_tokens (lexer))
29065 lexer = lexer->next;
29066 cp_lexer_commit_tokens (lexer);
29070 /* Abort the currently active tentative parse. All consumed tokens
29071 will be rolled back, and no diagnostics will be issued. */
29073 static void
29074 cp_parser_abort_tentative_parse (cp_parser* parser)
29076 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29077 || errorcount > 0);
29078 cp_parser_simulate_error (parser);
29079 /* Now, pretend that we want to see if the construct was
29080 successfully parsed. */
29081 cp_parser_parse_definitely (parser);
29084 /* Stop parsing tentatively. If a parse error has occurred, restore the
29085 token stream. Otherwise, commit to the tokens we have consumed.
29086 Returns true if no error occurred; false otherwise. */
29088 static bool
29089 cp_parser_parse_definitely (cp_parser* parser)
29091 bool error_occurred;
29092 cp_parser_context *context;
29094 /* Remember whether or not an error occurred, since we are about to
29095 destroy that information. */
29096 error_occurred = cp_parser_error_occurred (parser);
29097 /* Remove the topmost context from the stack. */
29098 context = parser->context;
29099 parser->context = context->next;
29100 /* If no parse errors occurred, commit to the tentative parse. */
29101 if (!error_occurred)
29103 /* Commit to the tokens read tentatively, unless that was
29104 already done. */
29105 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29106 cp_lexer_commit_tokens (parser->lexer);
29108 pop_to_parent_deferring_access_checks ();
29110 /* Otherwise, if errors occurred, roll back our state so that things
29111 are just as they were before we began the tentative parse. */
29112 else
29114 cp_lexer_rollback_tokens (parser->lexer);
29115 pop_deferring_access_checks ();
29117 /* Add the context to the front of the free list. */
29118 context->next = cp_parser_context_free_list;
29119 cp_parser_context_free_list = context;
29121 return !error_occurred;
29124 /* Returns true if we are parsing tentatively and are not committed to
29125 this tentative parse. */
29127 static bool
29128 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29130 return (cp_parser_parsing_tentatively (parser)
29131 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29134 /* Returns nonzero iff an error has occurred during the most recent
29135 tentative parse. */
29137 static bool
29138 cp_parser_error_occurred (cp_parser* parser)
29140 return (cp_parser_parsing_tentatively (parser)
29141 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29144 /* Returns nonzero if GNU extensions are allowed. */
29146 static bool
29147 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29149 return parser->allow_gnu_extensions_p;
29152 /* Objective-C++ Productions */
29155 /* Parse an Objective-C expression, which feeds into a primary-expression
29156 above.
29158 objc-expression:
29159 objc-message-expression
29160 objc-string-literal
29161 objc-encode-expression
29162 objc-protocol-expression
29163 objc-selector-expression
29165 Returns a tree representation of the expression. */
29167 static cp_expr
29168 cp_parser_objc_expression (cp_parser* parser)
29170 /* Try to figure out what kind of declaration is present. */
29171 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29173 switch (kwd->type)
29175 case CPP_OPEN_SQUARE:
29176 return cp_parser_objc_message_expression (parser);
29178 case CPP_OBJC_STRING:
29179 kwd = cp_lexer_consume_token (parser->lexer);
29180 return objc_build_string_object (kwd->u.value);
29182 case CPP_KEYWORD:
29183 switch (kwd->keyword)
29185 case RID_AT_ENCODE:
29186 return cp_parser_objc_encode_expression (parser);
29188 case RID_AT_PROTOCOL:
29189 return cp_parser_objc_protocol_expression (parser);
29191 case RID_AT_SELECTOR:
29192 return cp_parser_objc_selector_expression (parser);
29194 default:
29195 break;
29197 default:
29198 error_at (kwd->location,
29199 "misplaced %<@%D%> Objective-C++ construct",
29200 kwd->u.value);
29201 cp_parser_skip_to_end_of_block_or_statement (parser);
29204 return error_mark_node;
29207 /* Parse an Objective-C message expression.
29209 objc-message-expression:
29210 [ objc-message-receiver objc-message-args ]
29212 Returns a representation of an Objective-C message. */
29214 static tree
29215 cp_parser_objc_message_expression (cp_parser* parser)
29217 tree receiver, messageargs;
29219 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29220 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29221 receiver = cp_parser_objc_message_receiver (parser);
29222 messageargs = cp_parser_objc_message_args (parser);
29223 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29224 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29226 tree result = objc_build_message_expr (receiver, messageargs);
29228 /* Construct a location e.g.
29229 [self func1:5]
29230 ^~~~~~~~~~~~~~
29231 ranging from the '[' to the ']', with the caret at the start. */
29232 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29233 protected_set_expr_location (result, combined_loc);
29235 return result;
29238 /* Parse an objc-message-receiver.
29240 objc-message-receiver:
29241 expression
29242 simple-type-specifier
29244 Returns a representation of the type or expression. */
29246 static tree
29247 cp_parser_objc_message_receiver (cp_parser* parser)
29249 tree rcv;
29251 /* An Objective-C message receiver may be either (1) a type
29252 or (2) an expression. */
29253 cp_parser_parse_tentatively (parser);
29254 rcv = cp_parser_expression (parser);
29256 /* If that worked out, fine. */
29257 if (cp_parser_parse_definitely (parser))
29258 return rcv;
29260 cp_parser_parse_tentatively (parser);
29261 rcv = cp_parser_simple_type_specifier (parser,
29262 /*decl_specs=*/NULL,
29263 CP_PARSER_FLAGS_NONE);
29265 if (cp_parser_parse_definitely (parser))
29266 return objc_get_class_reference (rcv);
29268 cp_parser_error (parser, "objective-c++ message receiver expected");
29269 return error_mark_node;
29272 /* Parse the arguments and selectors comprising an Objective-C message.
29274 objc-message-args:
29275 objc-selector
29276 objc-selector-args
29277 objc-selector-args , objc-comma-args
29279 objc-selector-args:
29280 objc-selector [opt] : assignment-expression
29281 objc-selector-args objc-selector [opt] : assignment-expression
29283 objc-comma-args:
29284 assignment-expression
29285 objc-comma-args , assignment-expression
29287 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29288 selector arguments and TREE_VALUE containing a list of comma
29289 arguments. */
29291 static tree
29292 cp_parser_objc_message_args (cp_parser* parser)
29294 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29295 bool maybe_unary_selector_p = true;
29296 cp_token *token = cp_lexer_peek_token (parser->lexer);
29298 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29300 tree selector = NULL_TREE, arg;
29302 if (token->type != CPP_COLON)
29303 selector = cp_parser_objc_selector (parser);
29305 /* Detect if we have a unary selector. */
29306 if (maybe_unary_selector_p
29307 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29308 return build_tree_list (selector, NULL_TREE);
29310 maybe_unary_selector_p = false;
29311 cp_parser_require (parser, CPP_COLON, RT_COLON);
29312 arg = cp_parser_assignment_expression (parser);
29314 sel_args
29315 = chainon (sel_args,
29316 build_tree_list (selector, arg));
29318 token = cp_lexer_peek_token (parser->lexer);
29321 /* Handle non-selector arguments, if any. */
29322 while (token->type == CPP_COMMA)
29324 tree arg;
29326 cp_lexer_consume_token (parser->lexer);
29327 arg = cp_parser_assignment_expression (parser);
29329 addl_args
29330 = chainon (addl_args,
29331 build_tree_list (NULL_TREE, arg));
29333 token = cp_lexer_peek_token (parser->lexer);
29336 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29338 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29339 return build_tree_list (error_mark_node, error_mark_node);
29342 return build_tree_list (sel_args, addl_args);
29345 /* Parse an Objective-C encode expression.
29347 objc-encode-expression:
29348 @encode objc-typename
29350 Returns an encoded representation of the type argument. */
29352 static cp_expr
29353 cp_parser_objc_encode_expression (cp_parser* parser)
29355 tree type;
29356 cp_token *token;
29357 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29359 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29360 matching_parens parens;
29361 parens.require_open (parser);
29362 token = cp_lexer_peek_token (parser->lexer);
29363 type = complete_type (cp_parser_type_id (parser));
29364 parens.require_close (parser);
29366 if (!type)
29368 error_at (token->location,
29369 "%<@encode%> must specify a type as an argument");
29370 return error_mark_node;
29373 /* This happens if we find @encode(T) (where T is a template
29374 typename or something dependent on a template typename) when
29375 parsing a template. In that case, we can't compile it
29376 immediately, but we rather create an AT_ENCODE_EXPR which will
29377 need to be instantiated when the template is used.
29379 if (dependent_type_p (type))
29381 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29382 TREE_READONLY (value) = 1;
29383 return value;
29387 /* Build a location of the form:
29388 @encode(int)
29389 ^~~~~~~~~~~~
29390 with caret==start at the @ token, finishing at the close paren. */
29391 location_t combined_loc
29392 = make_location (start_loc, start_loc,
29393 cp_lexer_previous_token (parser->lexer)->location);
29395 return cp_expr (objc_build_encode_expr (type), combined_loc);
29398 /* Parse an Objective-C @defs expression. */
29400 static tree
29401 cp_parser_objc_defs_expression (cp_parser *parser)
29403 tree name;
29405 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29406 matching_parens parens;
29407 parens.require_open (parser);
29408 name = cp_parser_identifier (parser);
29409 parens.require_close (parser);
29411 return objc_get_class_ivars (name);
29414 /* Parse an Objective-C protocol expression.
29416 objc-protocol-expression:
29417 @protocol ( identifier )
29419 Returns a representation of the protocol expression. */
29421 static tree
29422 cp_parser_objc_protocol_expression (cp_parser* parser)
29424 tree proto;
29425 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29427 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29428 matching_parens parens;
29429 parens.require_open (parser);
29430 proto = cp_parser_identifier (parser);
29431 parens.require_close (parser);
29433 /* Build a location of the form:
29434 @protocol(prot)
29435 ^~~~~~~~~~~~~~~
29436 with caret==start at the @ token, finishing at the close paren. */
29437 location_t combined_loc
29438 = make_location (start_loc, start_loc,
29439 cp_lexer_previous_token (parser->lexer)->location);
29440 tree result = objc_build_protocol_expr (proto);
29441 protected_set_expr_location (result, combined_loc);
29442 return result;
29445 /* Parse an Objective-C selector expression.
29447 objc-selector-expression:
29448 @selector ( objc-method-signature )
29450 objc-method-signature:
29451 objc-selector
29452 objc-selector-seq
29454 objc-selector-seq:
29455 objc-selector :
29456 objc-selector-seq objc-selector :
29458 Returns a representation of the method selector. */
29460 static tree
29461 cp_parser_objc_selector_expression (cp_parser* parser)
29463 tree sel_seq = NULL_TREE;
29464 bool maybe_unary_selector_p = true;
29465 cp_token *token;
29466 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29468 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29469 matching_parens parens;
29470 parens.require_open (parser);
29471 token = cp_lexer_peek_token (parser->lexer);
29473 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29474 || token->type == CPP_SCOPE)
29476 tree selector = NULL_TREE;
29478 if (token->type != CPP_COLON
29479 || token->type == CPP_SCOPE)
29480 selector = cp_parser_objc_selector (parser);
29482 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29483 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29485 /* Detect if we have a unary selector. */
29486 if (maybe_unary_selector_p)
29488 sel_seq = selector;
29489 goto finish_selector;
29491 else
29493 cp_parser_error (parser, "expected %<:%>");
29496 maybe_unary_selector_p = false;
29497 token = cp_lexer_consume_token (parser->lexer);
29499 if (token->type == CPP_SCOPE)
29501 sel_seq
29502 = chainon (sel_seq,
29503 build_tree_list (selector, NULL_TREE));
29504 sel_seq
29505 = chainon (sel_seq,
29506 build_tree_list (NULL_TREE, NULL_TREE));
29508 else
29509 sel_seq
29510 = chainon (sel_seq,
29511 build_tree_list (selector, NULL_TREE));
29513 token = cp_lexer_peek_token (parser->lexer);
29516 finish_selector:
29517 parens.require_close (parser);
29520 /* Build a location of the form:
29521 @selector(func)
29522 ^~~~~~~~~~~~~~~
29523 with caret==start at the @ token, finishing at the close paren. */
29524 location_t combined_loc
29525 = make_location (loc, loc,
29526 cp_lexer_previous_token (parser->lexer)->location);
29527 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29528 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29529 protected_set_expr_location (result, combined_loc);
29530 return result;
29533 /* Parse a list of identifiers.
29535 objc-identifier-list:
29536 identifier
29537 objc-identifier-list , identifier
29539 Returns a TREE_LIST of identifier nodes. */
29541 static tree
29542 cp_parser_objc_identifier_list (cp_parser* parser)
29544 tree identifier;
29545 tree list;
29546 cp_token *sep;
29548 identifier = cp_parser_identifier (parser);
29549 if (identifier == error_mark_node)
29550 return error_mark_node;
29552 list = build_tree_list (NULL_TREE, identifier);
29553 sep = cp_lexer_peek_token (parser->lexer);
29555 while (sep->type == CPP_COMMA)
29557 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29558 identifier = cp_parser_identifier (parser);
29559 if (identifier == error_mark_node)
29560 return list;
29562 list = chainon (list, build_tree_list (NULL_TREE,
29563 identifier));
29564 sep = cp_lexer_peek_token (parser->lexer);
29567 return list;
29570 /* Parse an Objective-C alias declaration.
29572 objc-alias-declaration:
29573 @compatibility_alias identifier identifier ;
29575 This function registers the alias mapping with the Objective-C front end.
29576 It returns nothing. */
29578 static void
29579 cp_parser_objc_alias_declaration (cp_parser* parser)
29581 tree alias, orig;
29583 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29584 alias = cp_parser_identifier (parser);
29585 orig = cp_parser_identifier (parser);
29586 objc_declare_alias (alias, orig);
29587 cp_parser_consume_semicolon_at_end_of_statement (parser);
29590 /* Parse an Objective-C class forward-declaration.
29592 objc-class-declaration:
29593 @class objc-identifier-list ;
29595 The function registers the forward declarations with the Objective-C
29596 front end. It returns nothing. */
29598 static void
29599 cp_parser_objc_class_declaration (cp_parser* parser)
29601 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29602 while (true)
29604 tree id;
29606 id = cp_parser_identifier (parser);
29607 if (id == error_mark_node)
29608 break;
29610 objc_declare_class (id);
29612 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29613 cp_lexer_consume_token (parser->lexer);
29614 else
29615 break;
29617 cp_parser_consume_semicolon_at_end_of_statement (parser);
29620 /* Parse a list of Objective-C protocol references.
29622 objc-protocol-refs-opt:
29623 objc-protocol-refs [opt]
29625 objc-protocol-refs:
29626 < objc-identifier-list >
29628 Returns a TREE_LIST of identifiers, if any. */
29630 static tree
29631 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29633 tree protorefs = NULL_TREE;
29635 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29637 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29638 protorefs = cp_parser_objc_identifier_list (parser);
29639 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29642 return protorefs;
29645 /* Parse a Objective-C visibility specification. */
29647 static void
29648 cp_parser_objc_visibility_spec (cp_parser* parser)
29650 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29652 switch (vis->keyword)
29654 case RID_AT_PRIVATE:
29655 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29656 break;
29657 case RID_AT_PROTECTED:
29658 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29659 break;
29660 case RID_AT_PUBLIC:
29661 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29662 break;
29663 case RID_AT_PACKAGE:
29664 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29665 break;
29666 default:
29667 return;
29670 /* Eat '@private'/'@protected'/'@public'. */
29671 cp_lexer_consume_token (parser->lexer);
29674 /* Parse an Objective-C method type. Return 'true' if it is a class
29675 (+) method, and 'false' if it is an instance (-) method. */
29677 static inline bool
29678 cp_parser_objc_method_type (cp_parser* parser)
29680 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29681 return true;
29682 else
29683 return false;
29686 /* Parse an Objective-C protocol qualifier. */
29688 static tree
29689 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29691 tree quals = NULL_TREE, node;
29692 cp_token *token = cp_lexer_peek_token (parser->lexer);
29694 node = token->u.value;
29696 while (node && identifier_p (node)
29697 && (node == ridpointers [(int) RID_IN]
29698 || node == ridpointers [(int) RID_OUT]
29699 || node == ridpointers [(int) RID_INOUT]
29700 || node == ridpointers [(int) RID_BYCOPY]
29701 || node == ridpointers [(int) RID_BYREF]
29702 || node == ridpointers [(int) RID_ONEWAY]))
29704 quals = tree_cons (NULL_TREE, node, quals);
29705 cp_lexer_consume_token (parser->lexer);
29706 token = cp_lexer_peek_token (parser->lexer);
29707 node = token->u.value;
29710 return quals;
29713 /* Parse an Objective-C typename. */
29715 static tree
29716 cp_parser_objc_typename (cp_parser* parser)
29718 tree type_name = NULL_TREE;
29720 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29722 tree proto_quals, cp_type = NULL_TREE;
29724 matching_parens parens;
29725 parens.consume_open (parser); /* Eat '('. */
29726 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29728 /* An ObjC type name may consist of just protocol qualifiers, in which
29729 case the type shall default to 'id'. */
29730 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29732 cp_type = cp_parser_type_id (parser);
29734 /* If the type could not be parsed, an error has already
29735 been produced. For error recovery, behave as if it had
29736 not been specified, which will use the default type
29737 'id'. */
29738 if (cp_type == error_mark_node)
29740 cp_type = NULL_TREE;
29741 /* We need to skip to the closing parenthesis as
29742 cp_parser_type_id() does not seem to do it for
29743 us. */
29744 cp_parser_skip_to_closing_parenthesis (parser,
29745 /*recovering=*/true,
29746 /*or_comma=*/false,
29747 /*consume_paren=*/false);
29751 parens.require_close (parser);
29752 type_name = build_tree_list (proto_quals, cp_type);
29755 return type_name;
29758 /* Check to see if TYPE refers to an Objective-C selector name. */
29760 static bool
29761 cp_parser_objc_selector_p (enum cpp_ttype type)
29763 return (type == CPP_NAME || type == CPP_KEYWORD
29764 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29765 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29766 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29767 || type == CPP_XOR || type == CPP_XOR_EQ);
29770 /* Parse an Objective-C selector. */
29772 static tree
29773 cp_parser_objc_selector (cp_parser* parser)
29775 cp_token *token = cp_lexer_consume_token (parser->lexer);
29777 if (!cp_parser_objc_selector_p (token->type))
29779 error_at (token->location, "invalid Objective-C++ selector name");
29780 return error_mark_node;
29783 /* C++ operator names are allowed to appear in ObjC selectors. */
29784 switch (token->type)
29786 case CPP_AND_AND: return get_identifier ("and");
29787 case CPP_AND_EQ: return get_identifier ("and_eq");
29788 case CPP_AND: return get_identifier ("bitand");
29789 case CPP_OR: return get_identifier ("bitor");
29790 case CPP_COMPL: return get_identifier ("compl");
29791 case CPP_NOT: return get_identifier ("not");
29792 case CPP_NOT_EQ: return get_identifier ("not_eq");
29793 case CPP_OR_OR: return get_identifier ("or");
29794 case CPP_OR_EQ: return get_identifier ("or_eq");
29795 case CPP_XOR: return get_identifier ("xor");
29796 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29797 default: return token->u.value;
29801 /* Parse an Objective-C params list. */
29803 static tree
29804 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29806 tree params = NULL_TREE;
29807 bool maybe_unary_selector_p = true;
29808 cp_token *token = cp_lexer_peek_token (parser->lexer);
29810 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29812 tree selector = NULL_TREE, type_name, identifier;
29813 tree parm_attr = NULL_TREE;
29815 if (token->keyword == RID_ATTRIBUTE)
29816 break;
29818 if (token->type != CPP_COLON)
29819 selector = cp_parser_objc_selector (parser);
29821 /* Detect if we have a unary selector. */
29822 if (maybe_unary_selector_p
29823 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29825 params = selector; /* Might be followed by attributes. */
29826 break;
29829 maybe_unary_selector_p = false;
29830 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29832 /* Something went quite wrong. There should be a colon
29833 here, but there is not. Stop parsing parameters. */
29834 break;
29836 type_name = cp_parser_objc_typename (parser);
29837 /* New ObjC allows attributes on parameters too. */
29838 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29839 parm_attr = cp_parser_attributes_opt (parser);
29840 identifier = cp_parser_identifier (parser);
29842 params
29843 = chainon (params,
29844 objc_build_keyword_decl (selector,
29845 type_name,
29846 identifier,
29847 parm_attr));
29849 token = cp_lexer_peek_token (parser->lexer);
29852 if (params == NULL_TREE)
29854 cp_parser_error (parser, "objective-c++ method declaration is expected");
29855 return error_mark_node;
29858 /* We allow tail attributes for the method. */
29859 if (token->keyword == RID_ATTRIBUTE)
29861 *attributes = cp_parser_attributes_opt (parser);
29862 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29863 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29864 return params;
29865 cp_parser_error (parser,
29866 "method attributes must be specified at the end");
29867 return error_mark_node;
29870 if (params == NULL_TREE)
29872 cp_parser_error (parser, "objective-c++ method declaration is expected");
29873 return error_mark_node;
29875 return params;
29878 /* Parse the non-keyword Objective-C params. */
29880 static tree
29881 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29882 tree* attributes)
29884 tree params = make_node (TREE_LIST);
29885 cp_token *token = cp_lexer_peek_token (parser->lexer);
29886 *ellipsisp = false; /* Initially, assume no ellipsis. */
29888 while (token->type == CPP_COMMA)
29890 cp_parameter_declarator *parmdecl;
29891 tree parm;
29893 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29894 token = cp_lexer_peek_token (parser->lexer);
29896 if (token->type == CPP_ELLIPSIS)
29898 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29899 *ellipsisp = true;
29900 token = cp_lexer_peek_token (parser->lexer);
29901 break;
29904 /* TODO: parse attributes for tail parameters. */
29905 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29906 parm = grokdeclarator (parmdecl->declarator,
29907 &parmdecl->decl_specifiers,
29908 PARM, /*initialized=*/0,
29909 /*attrlist=*/NULL);
29911 chainon (params, build_tree_list (NULL_TREE, parm));
29912 token = cp_lexer_peek_token (parser->lexer);
29915 /* We allow tail attributes for the method. */
29916 if (token->keyword == RID_ATTRIBUTE)
29918 if (*attributes == NULL_TREE)
29920 *attributes = cp_parser_attributes_opt (parser);
29921 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29922 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29923 return params;
29925 else
29926 /* We have an error, but parse the attributes, so that we can
29927 carry on. */
29928 *attributes = cp_parser_attributes_opt (parser);
29930 cp_parser_error (parser,
29931 "method attributes must be specified at the end");
29932 return error_mark_node;
29935 return params;
29938 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29940 static void
29941 cp_parser_objc_interstitial_code (cp_parser* parser)
29943 cp_token *token = cp_lexer_peek_token (parser->lexer);
29945 /* If the next token is `extern' and the following token is a string
29946 literal, then we have a linkage specification. */
29947 if (token->keyword == RID_EXTERN
29948 && cp_parser_is_pure_string_literal
29949 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29950 cp_parser_linkage_specification (parser);
29951 /* Handle #pragma, if any. */
29952 else if (token->type == CPP_PRAGMA)
29953 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29954 /* Allow stray semicolons. */
29955 else if (token->type == CPP_SEMICOLON)
29956 cp_lexer_consume_token (parser->lexer);
29957 /* Mark methods as optional or required, when building protocols. */
29958 else if (token->keyword == RID_AT_OPTIONAL)
29960 cp_lexer_consume_token (parser->lexer);
29961 objc_set_method_opt (true);
29963 else if (token->keyword == RID_AT_REQUIRED)
29965 cp_lexer_consume_token (parser->lexer);
29966 objc_set_method_opt (false);
29968 else if (token->keyword == RID_NAMESPACE)
29969 cp_parser_namespace_definition (parser);
29970 /* Other stray characters must generate errors. */
29971 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29973 cp_lexer_consume_token (parser->lexer);
29974 error ("stray %qs between Objective-C++ methods",
29975 token->type == CPP_OPEN_BRACE ? "{" : "}");
29977 /* Finally, try to parse a block-declaration, or a function-definition. */
29978 else
29979 cp_parser_block_declaration (parser, /*statement_p=*/false);
29982 /* Parse a method signature. */
29984 static tree
29985 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29987 tree rettype, kwdparms, optparms;
29988 bool ellipsis = false;
29989 bool is_class_method;
29991 is_class_method = cp_parser_objc_method_type (parser);
29992 rettype = cp_parser_objc_typename (parser);
29993 *attributes = NULL_TREE;
29994 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29995 if (kwdparms == error_mark_node)
29996 return error_mark_node;
29997 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29998 if (optparms == error_mark_node)
29999 return error_mark_node;
30001 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30004 static bool
30005 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30007 tree tattr;
30008 cp_lexer_save_tokens (parser->lexer);
30009 tattr = cp_parser_attributes_opt (parser);
30010 gcc_assert (tattr) ;
30012 /* If the attributes are followed by a method introducer, this is not allowed.
30013 Dump the attributes and flag the situation. */
30014 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30015 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30016 return true;
30018 /* Otherwise, the attributes introduce some interstitial code, possibly so
30019 rewind to allow that check. */
30020 cp_lexer_rollback_tokens (parser->lexer);
30021 return false;
30024 /* Parse an Objective-C method prototype list. */
30026 static void
30027 cp_parser_objc_method_prototype_list (cp_parser* parser)
30029 cp_token *token = cp_lexer_peek_token (parser->lexer);
30031 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30033 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30035 tree attributes, sig;
30036 bool is_class_method;
30037 if (token->type == CPP_PLUS)
30038 is_class_method = true;
30039 else
30040 is_class_method = false;
30041 sig = cp_parser_objc_method_signature (parser, &attributes);
30042 if (sig == error_mark_node)
30044 cp_parser_skip_to_end_of_block_or_statement (parser);
30045 token = cp_lexer_peek_token (parser->lexer);
30046 continue;
30048 objc_add_method_declaration (is_class_method, sig, attributes);
30049 cp_parser_consume_semicolon_at_end_of_statement (parser);
30051 else if (token->keyword == RID_AT_PROPERTY)
30052 cp_parser_objc_at_property_declaration (parser);
30053 else if (token->keyword == RID_ATTRIBUTE
30054 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30055 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30056 OPT_Wattributes,
30057 "prefix attributes are ignored for methods");
30058 else
30059 /* Allow for interspersed non-ObjC++ code. */
30060 cp_parser_objc_interstitial_code (parser);
30062 token = cp_lexer_peek_token (parser->lexer);
30065 if (token->type != CPP_EOF)
30066 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30067 else
30068 cp_parser_error (parser, "expected %<@end%>");
30070 objc_finish_interface ();
30073 /* Parse an Objective-C method definition list. */
30075 static void
30076 cp_parser_objc_method_definition_list (cp_parser* parser)
30078 cp_token *token = cp_lexer_peek_token (parser->lexer);
30080 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30082 tree meth;
30084 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30086 cp_token *ptk;
30087 tree sig, attribute;
30088 bool is_class_method;
30089 if (token->type == CPP_PLUS)
30090 is_class_method = true;
30091 else
30092 is_class_method = false;
30093 push_deferring_access_checks (dk_deferred);
30094 sig = cp_parser_objc_method_signature (parser, &attribute);
30095 if (sig == error_mark_node)
30097 cp_parser_skip_to_end_of_block_or_statement (parser);
30098 token = cp_lexer_peek_token (parser->lexer);
30099 continue;
30101 objc_start_method_definition (is_class_method, sig, attribute,
30102 NULL_TREE);
30104 /* For historical reasons, we accept an optional semicolon. */
30105 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30106 cp_lexer_consume_token (parser->lexer);
30108 ptk = cp_lexer_peek_token (parser->lexer);
30109 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30110 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30112 perform_deferred_access_checks (tf_warning_or_error);
30113 stop_deferring_access_checks ();
30114 meth = cp_parser_function_definition_after_declarator (parser,
30115 false);
30116 pop_deferring_access_checks ();
30117 objc_finish_method_definition (meth);
30120 /* The following case will be removed once @synthesize is
30121 completely implemented. */
30122 else if (token->keyword == RID_AT_PROPERTY)
30123 cp_parser_objc_at_property_declaration (parser);
30124 else if (token->keyword == RID_AT_SYNTHESIZE)
30125 cp_parser_objc_at_synthesize_declaration (parser);
30126 else if (token->keyword == RID_AT_DYNAMIC)
30127 cp_parser_objc_at_dynamic_declaration (parser);
30128 else if (token->keyword == RID_ATTRIBUTE
30129 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30130 warning_at (token->location, OPT_Wattributes,
30131 "prefix attributes are ignored for methods");
30132 else
30133 /* Allow for interspersed non-ObjC++ code. */
30134 cp_parser_objc_interstitial_code (parser);
30136 token = cp_lexer_peek_token (parser->lexer);
30139 if (token->type != CPP_EOF)
30140 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30141 else
30142 cp_parser_error (parser, "expected %<@end%>");
30144 objc_finish_implementation ();
30147 /* Parse Objective-C ivars. */
30149 static void
30150 cp_parser_objc_class_ivars (cp_parser* parser)
30152 cp_token *token = cp_lexer_peek_token (parser->lexer);
30154 if (token->type != CPP_OPEN_BRACE)
30155 return; /* No ivars specified. */
30157 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30158 token = cp_lexer_peek_token (parser->lexer);
30160 while (token->type != CPP_CLOSE_BRACE
30161 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30163 cp_decl_specifier_seq declspecs;
30164 int decl_class_or_enum_p;
30165 tree prefix_attributes;
30167 cp_parser_objc_visibility_spec (parser);
30169 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30170 break;
30172 cp_parser_decl_specifier_seq (parser,
30173 CP_PARSER_FLAGS_OPTIONAL,
30174 &declspecs,
30175 &decl_class_or_enum_p);
30177 /* auto, register, static, extern, mutable. */
30178 if (declspecs.storage_class != sc_none)
30180 cp_parser_error (parser, "invalid type for instance variable");
30181 declspecs.storage_class = sc_none;
30184 /* thread_local. */
30185 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30187 cp_parser_error (parser, "invalid type for instance variable");
30188 declspecs.locations[ds_thread] = 0;
30191 /* typedef. */
30192 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30194 cp_parser_error (parser, "invalid type for instance variable");
30195 declspecs.locations[ds_typedef] = 0;
30198 prefix_attributes = declspecs.attributes;
30199 declspecs.attributes = NULL_TREE;
30201 /* Keep going until we hit the `;' at the end of the
30202 declaration. */
30203 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30205 tree width = NULL_TREE, attributes, first_attribute, decl;
30206 cp_declarator *declarator = NULL;
30207 int ctor_dtor_or_conv_p;
30209 /* Check for a (possibly unnamed) bitfield declaration. */
30210 token = cp_lexer_peek_token (parser->lexer);
30211 if (token->type == CPP_COLON)
30212 goto eat_colon;
30214 if (token->type == CPP_NAME
30215 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30216 == CPP_COLON))
30218 /* Get the name of the bitfield. */
30219 declarator = make_id_declarator (NULL_TREE,
30220 cp_parser_identifier (parser),
30221 sfk_none);
30223 eat_colon:
30224 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30225 /* Get the width of the bitfield. */
30226 width
30227 = cp_parser_constant_expression (parser);
30229 else
30231 /* Parse the declarator. */
30232 declarator
30233 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30234 &ctor_dtor_or_conv_p,
30235 /*parenthesized_p=*/NULL,
30236 /*member_p=*/false,
30237 /*friend_p=*/false);
30240 /* Look for attributes that apply to the ivar. */
30241 attributes = cp_parser_attributes_opt (parser);
30242 /* Remember which attributes are prefix attributes and
30243 which are not. */
30244 first_attribute = attributes;
30245 /* Combine the attributes. */
30246 attributes = chainon (prefix_attributes, attributes);
30248 if (width)
30249 /* Create the bitfield declaration. */
30250 decl = grokbitfield (declarator, &declspecs,
30251 width, NULL_TREE, attributes);
30252 else
30253 decl = grokfield (declarator, &declspecs,
30254 NULL_TREE, /*init_const_expr_p=*/false,
30255 NULL_TREE, attributes);
30257 /* Add the instance variable. */
30258 if (decl != error_mark_node && decl != NULL_TREE)
30259 objc_add_instance_variable (decl);
30261 /* Reset PREFIX_ATTRIBUTES. */
30262 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30263 attributes = TREE_CHAIN (attributes);
30264 if (attributes)
30265 TREE_CHAIN (attributes) = NULL_TREE;
30267 token = cp_lexer_peek_token (parser->lexer);
30269 if (token->type == CPP_COMMA)
30271 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30272 continue;
30274 break;
30277 cp_parser_consume_semicolon_at_end_of_statement (parser);
30278 token = cp_lexer_peek_token (parser->lexer);
30281 if (token->keyword == RID_AT_END)
30282 cp_parser_error (parser, "expected %<}%>");
30284 /* Do not consume the RID_AT_END, so it will be read again as terminating
30285 the @interface of @implementation. */
30286 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30287 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30289 /* For historical reasons, we accept an optional semicolon. */
30290 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30291 cp_lexer_consume_token (parser->lexer);
30294 /* Parse an Objective-C protocol declaration. */
30296 static void
30297 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30299 tree proto, protorefs;
30300 cp_token *tok;
30302 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30305 tok = cp_lexer_peek_token (parser->lexer);
30306 error_at (tok->location, "identifier expected after %<@protocol%>");
30307 cp_parser_consume_semicolon_at_end_of_statement (parser);
30308 return;
30311 /* See if we have a forward declaration or a definition. */
30312 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30314 /* Try a forward declaration first. */
30315 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30317 while (true)
30319 tree id;
30321 id = cp_parser_identifier (parser);
30322 if (id == error_mark_node)
30323 break;
30325 objc_declare_protocol (id, attributes);
30327 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30328 cp_lexer_consume_token (parser->lexer);
30329 else
30330 break;
30332 cp_parser_consume_semicolon_at_end_of_statement (parser);
30335 /* Ok, we got a full-fledged definition (or at least should). */
30336 else
30338 proto = cp_parser_identifier (parser);
30339 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30340 objc_start_protocol (proto, protorefs, attributes);
30341 cp_parser_objc_method_prototype_list (parser);
30345 /* Parse an Objective-C superclass or category. */
30347 static void
30348 cp_parser_objc_superclass_or_category (cp_parser *parser,
30349 bool iface_p,
30350 tree *super,
30351 tree *categ, bool *is_class_extension)
30353 cp_token *next = cp_lexer_peek_token (parser->lexer);
30355 *super = *categ = NULL_TREE;
30356 *is_class_extension = false;
30357 if (next->type == CPP_COLON)
30359 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30360 *super = cp_parser_identifier (parser);
30362 else if (next->type == CPP_OPEN_PAREN)
30364 matching_parens parens;
30365 parens.consume_open (parser); /* Eat '('. */
30367 /* If there is no category name, and this is an @interface, we
30368 have a class extension. */
30369 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30371 *categ = NULL_TREE;
30372 *is_class_extension = true;
30374 else
30375 *categ = cp_parser_identifier (parser);
30377 parens.require_close (parser);
30381 /* Parse an Objective-C class interface. */
30383 static void
30384 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30386 tree name, super, categ, protos;
30387 bool is_class_extension;
30389 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30390 name = cp_parser_identifier (parser);
30391 if (name == error_mark_node)
30393 /* It's hard to recover because even if valid @interface stuff
30394 is to follow, we can't compile it (or validate it) if we
30395 don't even know which class it refers to. Let's assume this
30396 was a stray '@interface' token in the stream and skip it.
30398 return;
30400 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30401 &is_class_extension);
30402 protos = cp_parser_objc_protocol_refs_opt (parser);
30404 /* We have either a class or a category on our hands. */
30405 if (categ || is_class_extension)
30406 objc_start_category_interface (name, categ, protos, attributes);
30407 else
30409 objc_start_class_interface (name, super, protos, attributes);
30410 /* Handle instance variable declarations, if any. */
30411 cp_parser_objc_class_ivars (parser);
30412 objc_continue_interface ();
30415 cp_parser_objc_method_prototype_list (parser);
30418 /* Parse an Objective-C class implementation. */
30420 static void
30421 cp_parser_objc_class_implementation (cp_parser* parser)
30423 tree name, super, categ;
30424 bool is_class_extension;
30426 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30427 name = cp_parser_identifier (parser);
30428 if (name == error_mark_node)
30430 /* It's hard to recover because even if valid @implementation
30431 stuff is to follow, we can't compile it (or validate it) if
30432 we don't even know which class it refers to. Let's assume
30433 this was a stray '@implementation' token in the stream and
30434 skip it.
30436 return;
30438 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30439 &is_class_extension);
30441 /* We have either a class or a category on our hands. */
30442 if (categ)
30443 objc_start_category_implementation (name, categ);
30444 else
30446 objc_start_class_implementation (name, super);
30447 /* Handle instance variable declarations, if any. */
30448 cp_parser_objc_class_ivars (parser);
30449 objc_continue_implementation ();
30452 cp_parser_objc_method_definition_list (parser);
30455 /* Consume the @end token and finish off the implementation. */
30457 static void
30458 cp_parser_objc_end_implementation (cp_parser* parser)
30460 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30461 objc_finish_implementation ();
30464 /* Parse an Objective-C declaration. */
30466 static void
30467 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30469 /* Try to figure out what kind of declaration is present. */
30470 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30472 if (attributes)
30473 switch (kwd->keyword)
30475 case RID_AT_ALIAS:
30476 case RID_AT_CLASS:
30477 case RID_AT_END:
30478 error_at (kwd->location, "attributes may not be specified before"
30479 " the %<@%D%> Objective-C++ keyword",
30480 kwd->u.value);
30481 attributes = NULL;
30482 break;
30483 case RID_AT_IMPLEMENTATION:
30484 warning_at (kwd->location, OPT_Wattributes,
30485 "prefix attributes are ignored before %<@%D%>",
30486 kwd->u.value);
30487 attributes = NULL;
30488 default:
30489 break;
30492 switch (kwd->keyword)
30494 case RID_AT_ALIAS:
30495 cp_parser_objc_alias_declaration (parser);
30496 break;
30497 case RID_AT_CLASS:
30498 cp_parser_objc_class_declaration (parser);
30499 break;
30500 case RID_AT_PROTOCOL:
30501 cp_parser_objc_protocol_declaration (parser, attributes);
30502 break;
30503 case RID_AT_INTERFACE:
30504 cp_parser_objc_class_interface (parser, attributes);
30505 break;
30506 case RID_AT_IMPLEMENTATION:
30507 cp_parser_objc_class_implementation (parser);
30508 break;
30509 case RID_AT_END:
30510 cp_parser_objc_end_implementation (parser);
30511 break;
30512 default:
30513 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30514 kwd->u.value);
30515 cp_parser_skip_to_end_of_block_or_statement (parser);
30519 /* Parse an Objective-C try-catch-finally statement.
30521 objc-try-catch-finally-stmt:
30522 @try compound-statement objc-catch-clause-seq [opt]
30523 objc-finally-clause [opt]
30525 objc-catch-clause-seq:
30526 objc-catch-clause objc-catch-clause-seq [opt]
30528 objc-catch-clause:
30529 @catch ( objc-exception-declaration ) compound-statement
30531 objc-finally-clause:
30532 @finally compound-statement
30534 objc-exception-declaration:
30535 parameter-declaration
30536 '...'
30538 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30540 Returns NULL_TREE.
30542 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30543 for C. Keep them in sync. */
30545 static tree
30546 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30548 location_t location;
30549 tree stmt;
30551 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30552 location = cp_lexer_peek_token (parser->lexer)->location;
30553 objc_maybe_warn_exceptions (location);
30554 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30555 node, lest it get absorbed into the surrounding block. */
30556 stmt = push_stmt_list ();
30557 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30558 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30560 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30562 cp_parameter_declarator *parm;
30563 tree parameter_declaration = error_mark_node;
30564 bool seen_open_paren = false;
30565 matching_parens parens;
30567 cp_lexer_consume_token (parser->lexer);
30568 if (parens.require_open (parser))
30569 seen_open_paren = true;
30570 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30572 /* We have "@catch (...)" (where the '...' are literally
30573 what is in the code). Skip the '...'.
30574 parameter_declaration is set to NULL_TREE, and
30575 objc_being_catch_clauses() knows that that means
30576 '...'. */
30577 cp_lexer_consume_token (parser->lexer);
30578 parameter_declaration = NULL_TREE;
30580 else
30582 /* We have "@catch (NSException *exception)" or something
30583 like that. Parse the parameter declaration. */
30584 parm = cp_parser_parameter_declaration (parser, false, NULL);
30585 if (parm == NULL)
30586 parameter_declaration = error_mark_node;
30587 else
30588 parameter_declaration = grokdeclarator (parm->declarator,
30589 &parm->decl_specifiers,
30590 PARM, /*initialized=*/0,
30591 /*attrlist=*/NULL);
30593 if (seen_open_paren)
30594 parens.require_close (parser);
30595 else
30597 /* If there was no open parenthesis, we are recovering from
30598 an error, and we are trying to figure out what mistake
30599 the user has made. */
30601 /* If there is an immediate closing parenthesis, the user
30602 probably forgot the opening one (ie, they typed "@catch
30603 NSException *e)". Parse the closing parenthesis and keep
30604 going. */
30605 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30606 cp_lexer_consume_token (parser->lexer);
30608 /* If these is no immediate closing parenthesis, the user
30609 probably doesn't know that parenthesis are required at
30610 all (ie, they typed "@catch NSException *e"). So, just
30611 forget about the closing parenthesis and keep going. */
30613 objc_begin_catch_clause (parameter_declaration);
30614 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30615 objc_finish_catch_clause ();
30617 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30619 cp_lexer_consume_token (parser->lexer);
30620 location = cp_lexer_peek_token (parser->lexer)->location;
30621 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30622 node, lest it get absorbed into the surrounding block. */
30623 stmt = push_stmt_list ();
30624 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30625 objc_build_finally_clause (location, pop_stmt_list (stmt));
30628 return objc_finish_try_stmt ();
30631 /* Parse an Objective-C synchronized statement.
30633 objc-synchronized-stmt:
30634 @synchronized ( expression ) compound-statement
30636 Returns NULL_TREE. */
30638 static tree
30639 cp_parser_objc_synchronized_statement (cp_parser *parser)
30641 location_t location;
30642 tree lock, stmt;
30644 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30646 location = cp_lexer_peek_token (parser->lexer)->location;
30647 objc_maybe_warn_exceptions (location);
30648 matching_parens parens;
30649 parens.require_open (parser);
30650 lock = cp_parser_expression (parser);
30651 parens.require_close (parser);
30653 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30654 node, lest it get absorbed into the surrounding block. */
30655 stmt = push_stmt_list ();
30656 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30658 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30661 /* Parse an Objective-C throw statement.
30663 objc-throw-stmt:
30664 @throw assignment-expression [opt] ;
30666 Returns a constructed '@throw' statement. */
30668 static tree
30669 cp_parser_objc_throw_statement (cp_parser *parser)
30671 tree expr = NULL_TREE;
30672 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30674 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30677 expr = cp_parser_expression (parser);
30679 cp_parser_consume_semicolon_at_end_of_statement (parser);
30681 return objc_build_throw_stmt (loc, expr);
30684 /* Parse an Objective-C statement. */
30686 static tree
30687 cp_parser_objc_statement (cp_parser * parser)
30689 /* Try to figure out what kind of declaration is present. */
30690 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30692 switch (kwd->keyword)
30694 case RID_AT_TRY:
30695 return cp_parser_objc_try_catch_finally_statement (parser);
30696 case RID_AT_SYNCHRONIZED:
30697 return cp_parser_objc_synchronized_statement (parser);
30698 case RID_AT_THROW:
30699 return cp_parser_objc_throw_statement (parser);
30700 default:
30701 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30702 kwd->u.value);
30703 cp_parser_skip_to_end_of_block_or_statement (parser);
30706 return error_mark_node;
30709 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30710 look ahead to see if an objc keyword follows the attributes. This
30711 is to detect the use of prefix attributes on ObjC @interface and
30712 @protocol. */
30714 static bool
30715 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30717 cp_lexer_save_tokens (parser->lexer);
30718 *attrib = cp_parser_attributes_opt (parser);
30719 gcc_assert (*attrib);
30720 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30722 cp_lexer_commit_tokens (parser->lexer);
30723 return true;
30725 cp_lexer_rollback_tokens (parser->lexer);
30726 return false;
30729 /* This routine is a minimal replacement for
30730 c_parser_struct_declaration () used when parsing the list of
30731 types/names or ObjC++ properties. For example, when parsing the
30732 code
30734 @property (readonly) int a, b, c;
30736 this function is responsible for parsing "int a, int b, int c" and
30737 returning the declarations as CHAIN of DECLs.
30739 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30740 similar parsing. */
30741 static tree
30742 cp_parser_objc_struct_declaration (cp_parser *parser)
30744 tree decls = NULL_TREE;
30745 cp_decl_specifier_seq declspecs;
30746 int decl_class_or_enum_p;
30747 tree prefix_attributes;
30749 cp_parser_decl_specifier_seq (parser,
30750 CP_PARSER_FLAGS_NONE,
30751 &declspecs,
30752 &decl_class_or_enum_p);
30754 if (declspecs.type == error_mark_node)
30755 return error_mark_node;
30757 /* auto, register, static, extern, mutable. */
30758 if (declspecs.storage_class != sc_none)
30760 cp_parser_error (parser, "invalid type for property");
30761 declspecs.storage_class = sc_none;
30764 /* thread_local. */
30765 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30767 cp_parser_error (parser, "invalid type for property");
30768 declspecs.locations[ds_thread] = 0;
30771 /* typedef. */
30772 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30774 cp_parser_error (parser, "invalid type for property");
30775 declspecs.locations[ds_typedef] = 0;
30778 prefix_attributes = declspecs.attributes;
30779 declspecs.attributes = NULL_TREE;
30781 /* Keep going until we hit the `;' at the end of the declaration. */
30782 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30784 tree attributes, first_attribute, decl;
30785 cp_declarator *declarator;
30786 cp_token *token;
30788 /* Parse the declarator. */
30789 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30790 NULL, NULL, false, false);
30792 /* Look for attributes that apply to the ivar. */
30793 attributes = cp_parser_attributes_opt (parser);
30794 /* Remember which attributes are prefix attributes and
30795 which are not. */
30796 first_attribute = attributes;
30797 /* Combine the attributes. */
30798 attributes = chainon (prefix_attributes, attributes);
30800 decl = grokfield (declarator, &declspecs,
30801 NULL_TREE, /*init_const_expr_p=*/false,
30802 NULL_TREE, attributes);
30804 if (decl == error_mark_node || decl == NULL_TREE)
30805 return error_mark_node;
30807 /* Reset PREFIX_ATTRIBUTES. */
30808 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30809 attributes = TREE_CHAIN (attributes);
30810 if (attributes)
30811 TREE_CHAIN (attributes) = NULL_TREE;
30813 DECL_CHAIN (decl) = decls;
30814 decls = decl;
30816 token = cp_lexer_peek_token (parser->lexer);
30817 if (token->type == CPP_COMMA)
30819 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30820 continue;
30822 else
30823 break;
30825 return decls;
30828 /* Parse an Objective-C @property declaration. The syntax is:
30830 objc-property-declaration:
30831 '@property' objc-property-attributes[opt] struct-declaration ;
30833 objc-property-attributes:
30834 '(' objc-property-attribute-list ')'
30836 objc-property-attribute-list:
30837 objc-property-attribute
30838 objc-property-attribute-list, objc-property-attribute
30840 objc-property-attribute
30841 'getter' = identifier
30842 'setter' = identifier
30843 'readonly'
30844 'readwrite'
30845 'assign'
30846 'retain'
30847 'copy'
30848 'nonatomic'
30850 For example:
30851 @property NSString *name;
30852 @property (readonly) id object;
30853 @property (retain, nonatomic, getter=getTheName) id name;
30854 @property int a, b, c;
30856 PS: This function is identical to
30857 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30858 static void
30859 cp_parser_objc_at_property_declaration (cp_parser *parser)
30861 /* The following variables hold the attributes of the properties as
30862 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30863 seen. When we see an attribute, we set them to 'true' (if they
30864 are boolean properties) or to the identifier (if they have an
30865 argument, ie, for getter and setter). Note that here we only
30866 parse the list of attributes, check the syntax and accumulate the
30867 attributes that we find. objc_add_property_declaration() will
30868 then process the information. */
30869 bool property_assign = false;
30870 bool property_copy = false;
30871 tree property_getter_ident = NULL_TREE;
30872 bool property_nonatomic = false;
30873 bool property_readonly = false;
30874 bool property_readwrite = false;
30875 bool property_retain = false;
30876 tree property_setter_ident = NULL_TREE;
30878 /* 'properties' is the list of properties that we read. Usually a
30879 single one, but maybe more (eg, in "@property int a, b, c;" there
30880 are three). */
30881 tree properties;
30882 location_t loc;
30884 loc = cp_lexer_peek_token (parser->lexer)->location;
30886 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30888 /* Parse the optional attribute list... */
30889 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30891 /* Eat the '('. */
30892 matching_parens parens;
30893 parens.consume_open (parser);
30895 while (true)
30897 bool syntax_error = false;
30898 cp_token *token = cp_lexer_peek_token (parser->lexer);
30899 enum rid keyword;
30901 if (token->type != CPP_NAME)
30903 cp_parser_error (parser, "expected identifier");
30904 break;
30906 keyword = C_RID_CODE (token->u.value);
30907 cp_lexer_consume_token (parser->lexer);
30908 switch (keyword)
30910 case RID_ASSIGN: property_assign = true; break;
30911 case RID_COPY: property_copy = true; break;
30912 case RID_NONATOMIC: property_nonatomic = true; break;
30913 case RID_READONLY: property_readonly = true; break;
30914 case RID_READWRITE: property_readwrite = true; break;
30915 case RID_RETAIN: property_retain = true; break;
30917 case RID_GETTER:
30918 case RID_SETTER:
30919 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30921 if (keyword == RID_GETTER)
30922 cp_parser_error (parser,
30923 "missing %<=%> (after %<getter%> attribute)");
30924 else
30925 cp_parser_error (parser,
30926 "missing %<=%> (after %<setter%> attribute)");
30927 syntax_error = true;
30928 break;
30930 cp_lexer_consume_token (parser->lexer); /* eat the = */
30931 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30933 cp_parser_error (parser, "expected identifier");
30934 syntax_error = true;
30935 break;
30937 if (keyword == RID_SETTER)
30939 if (property_setter_ident != NULL_TREE)
30941 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30942 cp_lexer_consume_token (parser->lexer);
30944 else
30945 property_setter_ident = cp_parser_objc_selector (parser);
30946 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30947 cp_parser_error (parser, "setter name must terminate with %<:%>");
30948 else
30949 cp_lexer_consume_token (parser->lexer);
30951 else
30953 if (property_getter_ident != NULL_TREE)
30955 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30956 cp_lexer_consume_token (parser->lexer);
30958 else
30959 property_getter_ident = cp_parser_objc_selector (parser);
30961 break;
30962 default:
30963 cp_parser_error (parser, "unknown property attribute");
30964 syntax_error = true;
30965 break;
30968 if (syntax_error)
30969 break;
30971 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30972 cp_lexer_consume_token (parser->lexer);
30973 else
30974 break;
30977 /* FIXME: "@property (setter, assign);" will generate a spurious
30978 "error: expected ‘)’ before ‘,’ token". This is because
30979 cp_parser_require, unlike the C counterpart, will produce an
30980 error even if we are in error recovery. */
30981 if (!parens.require_close (parser))
30983 cp_parser_skip_to_closing_parenthesis (parser,
30984 /*recovering=*/true,
30985 /*or_comma=*/false,
30986 /*consume_paren=*/true);
30990 /* ... and the property declaration(s). */
30991 properties = cp_parser_objc_struct_declaration (parser);
30993 if (properties == error_mark_node)
30995 cp_parser_skip_to_end_of_statement (parser);
30996 /* If the next token is now a `;', consume it. */
30997 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30998 cp_lexer_consume_token (parser->lexer);
30999 return;
31002 if (properties == NULL_TREE)
31003 cp_parser_error (parser, "expected identifier");
31004 else
31006 /* Comma-separated properties are chained together in
31007 reverse order; add them one by one. */
31008 properties = nreverse (properties);
31010 for (; properties; properties = TREE_CHAIN (properties))
31011 objc_add_property_declaration (loc, copy_node (properties),
31012 property_readonly, property_readwrite,
31013 property_assign, property_retain,
31014 property_copy, property_nonatomic,
31015 property_getter_ident, property_setter_ident);
31018 cp_parser_consume_semicolon_at_end_of_statement (parser);
31021 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31023 objc-synthesize-declaration:
31024 @synthesize objc-synthesize-identifier-list ;
31026 objc-synthesize-identifier-list:
31027 objc-synthesize-identifier
31028 objc-synthesize-identifier-list, objc-synthesize-identifier
31030 objc-synthesize-identifier
31031 identifier
31032 identifier = identifier
31034 For example:
31035 @synthesize MyProperty;
31036 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31038 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31039 for C. Keep them in sync.
31041 static void
31042 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31044 tree list = NULL_TREE;
31045 location_t loc;
31046 loc = cp_lexer_peek_token (parser->lexer)->location;
31048 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31049 while (true)
31051 tree property, ivar;
31052 property = cp_parser_identifier (parser);
31053 if (property == error_mark_node)
31055 cp_parser_consume_semicolon_at_end_of_statement (parser);
31056 return;
31058 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31060 cp_lexer_consume_token (parser->lexer);
31061 ivar = cp_parser_identifier (parser);
31062 if (ivar == error_mark_node)
31064 cp_parser_consume_semicolon_at_end_of_statement (parser);
31065 return;
31068 else
31069 ivar = NULL_TREE;
31070 list = chainon (list, build_tree_list (ivar, property));
31071 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31072 cp_lexer_consume_token (parser->lexer);
31073 else
31074 break;
31076 cp_parser_consume_semicolon_at_end_of_statement (parser);
31077 objc_add_synthesize_declaration (loc, list);
31080 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31082 objc-dynamic-declaration:
31083 @dynamic identifier-list ;
31085 For example:
31086 @dynamic MyProperty;
31087 @dynamic MyProperty, AnotherProperty;
31089 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31090 for C. Keep them in sync.
31092 static void
31093 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31095 tree list = NULL_TREE;
31096 location_t loc;
31097 loc = cp_lexer_peek_token (parser->lexer)->location;
31099 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31100 while (true)
31102 tree property;
31103 property = cp_parser_identifier (parser);
31104 if (property == error_mark_node)
31106 cp_parser_consume_semicolon_at_end_of_statement (parser);
31107 return;
31109 list = chainon (list, build_tree_list (NULL, property));
31110 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31111 cp_lexer_consume_token (parser->lexer);
31112 else
31113 break;
31115 cp_parser_consume_semicolon_at_end_of_statement (parser);
31116 objc_add_dynamic_declaration (loc, list);
31120 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31122 /* Returns name of the next clause.
31123 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31124 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31125 returned and the token is consumed. */
31127 static pragma_omp_clause
31128 cp_parser_omp_clause_name (cp_parser *parser)
31130 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31132 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31133 result = PRAGMA_OACC_CLAUSE_AUTO;
31134 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31135 result = PRAGMA_OMP_CLAUSE_IF;
31136 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31137 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31138 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31139 result = PRAGMA_OACC_CLAUSE_DELETE;
31140 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31141 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31142 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31143 result = PRAGMA_OMP_CLAUSE_FOR;
31144 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31146 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31147 const char *p = IDENTIFIER_POINTER (id);
31149 switch (p[0])
31151 case 'a':
31152 if (!strcmp ("aligned", p))
31153 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31154 else if (!strcmp ("async", p))
31155 result = PRAGMA_OACC_CLAUSE_ASYNC;
31156 break;
31157 case 'c':
31158 if (!strcmp ("collapse", p))
31159 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31160 else if (!strcmp ("copy", p))
31161 result = PRAGMA_OACC_CLAUSE_COPY;
31162 else if (!strcmp ("copyin", p))
31163 result = PRAGMA_OMP_CLAUSE_COPYIN;
31164 else if (!strcmp ("copyout", p))
31165 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31166 else if (!strcmp ("copyprivate", p))
31167 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31168 else if (!strcmp ("create", p))
31169 result = PRAGMA_OACC_CLAUSE_CREATE;
31170 break;
31171 case 'd':
31172 if (!strcmp ("defaultmap", p))
31173 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31174 else if (!strcmp ("depend", p))
31175 result = PRAGMA_OMP_CLAUSE_DEPEND;
31176 else if (!strcmp ("device", p))
31177 result = PRAGMA_OMP_CLAUSE_DEVICE;
31178 else if (!strcmp ("deviceptr", p))
31179 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31180 else if (!strcmp ("device_resident", p))
31181 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31182 else if (!strcmp ("dist_schedule", p))
31183 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31184 break;
31185 case 'f':
31186 if (!strcmp ("final", p))
31187 result = PRAGMA_OMP_CLAUSE_FINAL;
31188 else if (!strcmp ("firstprivate", p))
31189 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31190 else if (!strcmp ("from", p))
31191 result = PRAGMA_OMP_CLAUSE_FROM;
31192 break;
31193 case 'g':
31194 if (!strcmp ("gang", p))
31195 result = PRAGMA_OACC_CLAUSE_GANG;
31196 else if (!strcmp ("grainsize", p))
31197 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31198 break;
31199 case 'h':
31200 if (!strcmp ("hint", p))
31201 result = PRAGMA_OMP_CLAUSE_HINT;
31202 else if (!strcmp ("host", p))
31203 result = PRAGMA_OACC_CLAUSE_HOST;
31204 break;
31205 case 'i':
31206 if (!strcmp ("inbranch", p))
31207 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31208 else if (!strcmp ("independent", p))
31209 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31210 else if (!strcmp ("is_device_ptr", p))
31211 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31212 break;
31213 case 'l':
31214 if (!strcmp ("lastprivate", p))
31215 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31216 else if (!strcmp ("linear", p))
31217 result = PRAGMA_OMP_CLAUSE_LINEAR;
31218 else if (!strcmp ("link", p))
31219 result = PRAGMA_OMP_CLAUSE_LINK;
31220 break;
31221 case 'm':
31222 if (!strcmp ("map", p))
31223 result = PRAGMA_OMP_CLAUSE_MAP;
31224 else if (!strcmp ("mergeable", p))
31225 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31226 else if (flag_cilkplus && !strcmp ("mask", p))
31227 result = PRAGMA_CILK_CLAUSE_MASK;
31228 break;
31229 case 'n':
31230 if (!strcmp ("nogroup", p))
31231 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31232 else if (!strcmp ("notinbranch", p))
31233 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31234 else if (!strcmp ("nowait", p))
31235 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31236 else if (flag_cilkplus && !strcmp ("nomask", p))
31237 result = PRAGMA_CILK_CLAUSE_NOMASK;
31238 else if (!strcmp ("num_gangs", p))
31239 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31240 else if (!strcmp ("num_tasks", p))
31241 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31242 else if (!strcmp ("num_teams", p))
31243 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31244 else if (!strcmp ("num_threads", p))
31245 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31246 else if (!strcmp ("num_workers", p))
31247 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31248 break;
31249 case 'o':
31250 if (!strcmp ("ordered", p))
31251 result = PRAGMA_OMP_CLAUSE_ORDERED;
31252 break;
31253 case 'p':
31254 if (!strcmp ("parallel", p))
31255 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31256 else if (!strcmp ("present", p))
31257 result = PRAGMA_OACC_CLAUSE_PRESENT;
31258 else if (!strcmp ("present_or_copy", p)
31259 || !strcmp ("pcopy", p))
31260 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31261 else if (!strcmp ("present_or_copyin", p)
31262 || !strcmp ("pcopyin", p))
31263 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31264 else if (!strcmp ("present_or_copyout", p)
31265 || !strcmp ("pcopyout", p))
31266 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31267 else if (!strcmp ("present_or_create", p)
31268 || !strcmp ("pcreate", p))
31269 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31270 else if (!strcmp ("priority", p))
31271 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31272 else if (!strcmp ("proc_bind", p))
31273 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31274 break;
31275 case 'r':
31276 if (!strcmp ("reduction", p))
31277 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31278 break;
31279 case 's':
31280 if (!strcmp ("safelen", p))
31281 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31282 else if (!strcmp ("schedule", p))
31283 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31284 else if (!strcmp ("sections", p))
31285 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31286 else if (!strcmp ("self", p))
31287 result = PRAGMA_OACC_CLAUSE_SELF;
31288 else if (!strcmp ("seq", p))
31289 result = PRAGMA_OACC_CLAUSE_SEQ;
31290 else if (!strcmp ("shared", p))
31291 result = PRAGMA_OMP_CLAUSE_SHARED;
31292 else if (!strcmp ("simd", p))
31293 result = PRAGMA_OMP_CLAUSE_SIMD;
31294 else if (!strcmp ("simdlen", p))
31295 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31296 break;
31297 case 't':
31298 if (!strcmp ("taskgroup", p))
31299 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31300 else if (!strcmp ("thread_limit", p))
31301 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31302 else if (!strcmp ("threads", p))
31303 result = PRAGMA_OMP_CLAUSE_THREADS;
31304 else if (!strcmp ("tile", p))
31305 result = PRAGMA_OACC_CLAUSE_TILE;
31306 else if (!strcmp ("to", p))
31307 result = PRAGMA_OMP_CLAUSE_TO;
31308 break;
31309 case 'u':
31310 if (!strcmp ("uniform", p))
31311 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31312 else if (!strcmp ("untied", p))
31313 result = PRAGMA_OMP_CLAUSE_UNTIED;
31314 else if (!strcmp ("use_device", p))
31315 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31316 else if (!strcmp ("use_device_ptr", p))
31317 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31318 break;
31319 case 'v':
31320 if (!strcmp ("vector", p))
31321 result = PRAGMA_OACC_CLAUSE_VECTOR;
31322 else if (!strcmp ("vector_length", p))
31323 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31324 else if (flag_cilkplus && !strcmp ("vectorlength", p))
31325 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
31326 break;
31327 case 'w':
31328 if (!strcmp ("wait", p))
31329 result = PRAGMA_OACC_CLAUSE_WAIT;
31330 else if (!strcmp ("worker", p))
31331 result = PRAGMA_OACC_CLAUSE_WORKER;
31332 break;
31336 if (result != PRAGMA_OMP_CLAUSE_NONE)
31337 cp_lexer_consume_token (parser->lexer);
31339 return result;
31342 /* Validate that a clause of the given type does not already exist. */
31344 static void
31345 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31346 const char *name, location_t location)
31348 tree c;
31350 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31351 if (OMP_CLAUSE_CODE (c) == code)
31353 error_at (location, "too many %qs clauses", name);
31354 break;
31358 /* OpenMP 2.5:
31359 variable-list:
31360 identifier
31361 variable-list , identifier
31363 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31364 colon). An opening parenthesis will have been consumed by the caller.
31366 If KIND is nonzero, create the appropriate node and install the decl
31367 in OMP_CLAUSE_DECL and add the node to the head of the list.
31369 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31370 return the list created.
31372 COLON can be NULL if only closing parenthesis should end the list,
31373 or pointer to bool which will receive false if the list is terminated
31374 by closing parenthesis or true if the list is terminated by colon. */
31376 static tree
31377 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31378 tree list, bool *colon)
31380 cp_token *token;
31381 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31382 if (colon)
31384 parser->colon_corrects_to_scope_p = false;
31385 *colon = false;
31387 while (1)
31389 tree name, decl;
31391 token = cp_lexer_peek_token (parser->lexer);
31392 if (kind != 0
31393 && current_class_ptr
31394 && cp_parser_is_keyword (token, RID_THIS))
31396 decl = finish_this_expr ();
31397 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31398 || CONVERT_EXPR_P (decl))
31399 decl = TREE_OPERAND (decl, 0);
31400 cp_lexer_consume_token (parser->lexer);
31402 else
31404 name = cp_parser_id_expression (parser, /*template_p=*/false,
31405 /*check_dependency_p=*/true,
31406 /*template_p=*/NULL,
31407 /*declarator_p=*/false,
31408 /*optional_p=*/false);
31409 if (name == error_mark_node)
31410 goto skip_comma;
31412 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31413 if (decl == error_mark_node)
31414 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31415 token->location);
31417 if (decl == error_mark_node)
31419 else if (kind != 0)
31421 switch (kind)
31423 case OMP_CLAUSE__CACHE_:
31424 /* The OpenACC cache directive explicitly only allows "array
31425 elements or subarrays". */
31426 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31428 error_at (token->location, "expected %<[%>");
31429 decl = error_mark_node;
31430 break;
31432 /* FALLTHROUGH. */
31433 case OMP_CLAUSE_MAP:
31434 case OMP_CLAUSE_FROM:
31435 case OMP_CLAUSE_TO:
31436 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31438 location_t loc
31439 = cp_lexer_peek_token (parser->lexer)->location;
31440 cp_id_kind idk = CP_ID_KIND_NONE;
31441 cp_lexer_consume_token (parser->lexer);
31442 decl = convert_from_reference (decl);
31443 decl
31444 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31445 decl, false,
31446 &idk, loc);
31448 /* FALLTHROUGH. */
31449 case OMP_CLAUSE_DEPEND:
31450 case OMP_CLAUSE_REDUCTION:
31451 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31453 tree low_bound = NULL_TREE, length = NULL_TREE;
31455 parser->colon_corrects_to_scope_p = false;
31456 cp_lexer_consume_token (parser->lexer);
31457 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31458 low_bound = cp_parser_expression (parser);
31459 if (!colon)
31460 parser->colon_corrects_to_scope_p
31461 = saved_colon_corrects_to_scope_p;
31462 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31463 length = integer_one_node;
31464 else
31466 /* Look for `:'. */
31467 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31468 goto skip_comma;
31469 if (!cp_lexer_next_token_is (parser->lexer,
31470 CPP_CLOSE_SQUARE))
31471 length = cp_parser_expression (parser);
31473 /* Look for the closing `]'. */
31474 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31475 RT_CLOSE_SQUARE))
31476 goto skip_comma;
31478 decl = tree_cons (low_bound, length, decl);
31480 break;
31481 default:
31482 break;
31485 tree u = build_omp_clause (token->location, kind);
31486 OMP_CLAUSE_DECL (u) = decl;
31487 OMP_CLAUSE_CHAIN (u) = list;
31488 list = u;
31490 else
31491 list = tree_cons (decl, NULL_TREE, list);
31493 get_comma:
31494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31495 break;
31496 cp_lexer_consume_token (parser->lexer);
31499 if (colon)
31500 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31502 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31504 *colon = true;
31505 cp_parser_require (parser, CPP_COLON, RT_COLON);
31506 return list;
31509 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31511 int ending;
31513 /* Try to resync to an unnested comma. Copied from
31514 cp_parser_parenthesized_expression_list. */
31515 skip_comma:
31516 if (colon)
31517 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31518 ending = cp_parser_skip_to_closing_parenthesis (parser,
31519 /*recovering=*/true,
31520 /*or_comma=*/true,
31521 /*consume_paren=*/true);
31522 if (ending < 0)
31523 goto get_comma;
31526 return list;
31529 /* Similarly, but expect leading and trailing parenthesis. This is a very
31530 common case for omp clauses. */
31532 static tree
31533 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31535 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31536 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31537 return list;
31540 /* OpenACC 2.0:
31541 copy ( variable-list )
31542 copyin ( variable-list )
31543 copyout ( variable-list )
31544 create ( variable-list )
31545 delete ( variable-list )
31546 present ( variable-list )
31547 present_or_copy ( variable-list )
31548 pcopy ( variable-list )
31549 present_or_copyin ( variable-list )
31550 pcopyin ( variable-list )
31551 present_or_copyout ( variable-list )
31552 pcopyout ( variable-list )
31553 present_or_create ( variable-list )
31554 pcreate ( variable-list ) */
31556 static tree
31557 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31558 tree list)
31560 enum gomp_map_kind kind;
31561 switch (c_kind)
31563 case PRAGMA_OACC_CLAUSE_COPY:
31564 kind = GOMP_MAP_FORCE_TOFROM;
31565 break;
31566 case PRAGMA_OACC_CLAUSE_COPYIN:
31567 kind = GOMP_MAP_FORCE_TO;
31568 break;
31569 case PRAGMA_OACC_CLAUSE_COPYOUT:
31570 kind = GOMP_MAP_FORCE_FROM;
31571 break;
31572 case PRAGMA_OACC_CLAUSE_CREATE:
31573 kind = GOMP_MAP_FORCE_ALLOC;
31574 break;
31575 case PRAGMA_OACC_CLAUSE_DELETE:
31576 kind = GOMP_MAP_DELETE;
31577 break;
31578 case PRAGMA_OACC_CLAUSE_DEVICE:
31579 kind = GOMP_MAP_FORCE_TO;
31580 break;
31581 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31582 kind = GOMP_MAP_DEVICE_RESIDENT;
31583 break;
31584 case PRAGMA_OACC_CLAUSE_HOST:
31585 case PRAGMA_OACC_CLAUSE_SELF:
31586 kind = GOMP_MAP_FORCE_FROM;
31587 break;
31588 case PRAGMA_OACC_CLAUSE_LINK:
31589 kind = GOMP_MAP_LINK;
31590 break;
31591 case PRAGMA_OACC_CLAUSE_PRESENT:
31592 kind = GOMP_MAP_FORCE_PRESENT;
31593 break;
31594 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31595 kind = GOMP_MAP_TOFROM;
31596 break;
31597 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31598 kind = GOMP_MAP_TO;
31599 break;
31600 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31601 kind = GOMP_MAP_FROM;
31602 break;
31603 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31604 kind = GOMP_MAP_ALLOC;
31605 break;
31606 default:
31607 gcc_unreachable ();
31609 tree nl, c;
31610 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31612 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31613 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31615 return nl;
31618 /* OpenACC 2.0:
31619 deviceptr ( variable-list ) */
31621 static tree
31622 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31624 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31625 tree vars, t;
31627 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31628 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31629 variable-list must only allow for pointer variables. */
31630 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31631 for (t = vars; t; t = TREE_CHAIN (t))
31633 tree v = TREE_PURPOSE (t);
31634 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31635 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31636 OMP_CLAUSE_DECL (u) = v;
31637 OMP_CLAUSE_CHAIN (u) = list;
31638 list = u;
31641 return list;
31644 /* OpenACC 2.0:
31645 auto
31646 independent
31647 nohost
31648 seq */
31650 static tree
31651 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31652 enum omp_clause_code code,
31653 tree list, location_t location)
31655 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31656 tree c = build_omp_clause (location, code);
31657 OMP_CLAUSE_CHAIN (c) = list;
31658 return c;
31661 /* OpenACC:
31662 num_gangs ( expression )
31663 num_workers ( expression )
31664 vector_length ( expression ) */
31666 static tree
31667 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31668 const char *str, tree list)
31670 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31672 matching_parens parens;
31673 if (!parens.require_open (parser))
31674 return list;
31676 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31678 if (t == error_mark_node
31679 || !parens.require_close (parser))
31681 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31682 /*or_comma=*/false,
31683 /*consume_paren=*/true);
31684 return list;
31687 check_no_duplicate_clause (list, code, str, loc);
31689 tree c = build_omp_clause (loc, code);
31690 OMP_CLAUSE_OPERAND (c, 0) = t;
31691 OMP_CLAUSE_CHAIN (c) = list;
31692 return c;
31695 /* OpenACC:
31697 gang [( gang-arg-list )]
31698 worker [( [num:] int-expr )]
31699 vector [( [length:] int-expr )]
31701 where gang-arg is one of:
31703 [num:] int-expr
31704 static: size-expr
31706 and size-expr may be:
31709 int-expr
31712 static tree
31713 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31714 const char *str, tree list)
31716 const char *id = "num";
31717 cp_lexer *lexer = parser->lexer;
31718 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31719 location_t loc = cp_lexer_peek_token (lexer)->location;
31721 if (kind == OMP_CLAUSE_VECTOR)
31722 id = "length";
31724 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31726 matching_parens parens;
31727 parens.consume_open (parser);
31731 cp_token *next = cp_lexer_peek_token (lexer);
31732 int idx = 0;
31734 /* Gang static argument. */
31735 if (kind == OMP_CLAUSE_GANG
31736 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31738 cp_lexer_consume_token (lexer);
31740 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31741 goto cleanup_error;
31743 idx = 1;
31744 if (ops[idx] != NULL)
31746 cp_parser_error (parser, "too many %<static%> arguments");
31747 goto cleanup_error;
31750 /* Check for the '*' argument. */
31751 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31752 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31753 || cp_lexer_nth_token_is (parser->lexer, 2,
31754 CPP_CLOSE_PAREN)))
31756 cp_lexer_consume_token (lexer);
31757 ops[idx] = integer_minus_one_node;
31759 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31761 cp_lexer_consume_token (lexer);
31762 continue;
31764 else break;
31767 /* Worker num: argument and vector length: arguments. */
31768 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31769 && id_equal (next->u.value, id)
31770 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31772 cp_lexer_consume_token (lexer); /* id */
31773 cp_lexer_consume_token (lexer); /* ':' */
31776 /* Now collect the actual argument. */
31777 if (ops[idx] != NULL_TREE)
31779 cp_parser_error (parser, "unexpected argument");
31780 goto cleanup_error;
31783 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31784 false);
31785 if (expr == error_mark_node)
31786 goto cleanup_error;
31788 mark_exp_read (expr);
31789 ops[idx] = expr;
31791 if (kind == OMP_CLAUSE_GANG
31792 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31794 cp_lexer_consume_token (lexer);
31795 continue;
31797 break;
31799 while (1);
31801 if (!parens.require_close (parser))
31802 goto cleanup_error;
31805 check_no_duplicate_clause (list, kind, str, loc);
31807 c = build_omp_clause (loc, kind);
31809 if (ops[1])
31810 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31812 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31813 OMP_CLAUSE_CHAIN (c) = list;
31815 return c;
31817 cleanup_error:
31818 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31819 return list;
31822 /* OpenACC 2.0:
31823 tile ( size-expr-list ) */
31825 static tree
31826 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31828 tree c, expr = error_mark_node;
31829 tree tile = NULL_TREE;
31831 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31832 so, but the spec authors never considered such a case and have
31833 differing opinions on what it might mean, including 'not
31834 allowed'.) */
31835 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31836 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31837 clause_loc);
31839 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31840 return list;
31844 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31845 return list;
31847 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31848 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31849 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31851 cp_lexer_consume_token (parser->lexer);
31852 expr = integer_zero_node;
31854 else
31855 expr = cp_parser_constant_expression (parser);
31857 tile = tree_cons (NULL_TREE, expr, tile);
31859 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31861 /* Consume the trailing ')'. */
31862 cp_lexer_consume_token (parser->lexer);
31864 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31865 tile = nreverse (tile);
31866 OMP_CLAUSE_TILE_LIST (c) = tile;
31867 OMP_CLAUSE_CHAIN (c) = list;
31868 return c;
31871 /* OpenACC 2.0
31872 Parse wait clause or directive parameters. */
31874 static tree
31875 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31877 vec<tree, va_gc> *args;
31878 tree t, args_tree;
31880 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31881 /*cast_p=*/false,
31882 /*allow_expansion_p=*/true,
31883 /*non_constant_p=*/NULL);
31885 if (args == NULL || args->length () == 0)
31887 cp_parser_error (parser, "expected integer expression before ')'");
31888 if (args != NULL)
31889 release_tree_vector (args);
31890 return list;
31893 args_tree = build_tree_list_vec (args);
31895 release_tree_vector (args);
31897 for (t = args_tree; t; t = TREE_CHAIN (t))
31899 tree targ = TREE_VALUE (t);
31901 if (targ != error_mark_node)
31903 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31904 error ("%<wait%> expression must be integral");
31905 else
31907 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31909 targ = mark_rvalue_use (targ);
31910 OMP_CLAUSE_DECL (c) = targ;
31911 OMP_CLAUSE_CHAIN (c) = list;
31912 list = c;
31917 return list;
31920 /* OpenACC:
31921 wait ( int-expr-list ) */
31923 static tree
31924 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31926 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31928 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31929 return list;
31931 list = cp_parser_oacc_wait_list (parser, location, list);
31933 return list;
31936 /* OpenMP 3.0:
31937 collapse ( constant-expression ) */
31939 static tree
31940 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31942 tree c, num;
31943 location_t loc;
31944 HOST_WIDE_INT n;
31946 loc = cp_lexer_peek_token (parser->lexer)->location;
31947 matching_parens parens;
31948 if (!parens.require_open (parser))
31949 return list;
31951 num = cp_parser_constant_expression (parser);
31953 if (!parens.require_close (parser))
31954 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31955 /*or_comma=*/false,
31956 /*consume_paren=*/true);
31958 if (num == error_mark_node)
31959 return list;
31960 num = fold_non_dependent_expr (num);
31961 if (!tree_fits_shwi_p (num)
31962 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31963 || (n = tree_to_shwi (num)) <= 0
31964 || (int) n != n)
31966 error_at (loc, "collapse argument needs positive constant integer expression");
31967 return list;
31970 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31971 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31972 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31973 OMP_CLAUSE_CHAIN (c) = list;
31974 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31976 return c;
31979 /* OpenMP 2.5:
31980 default ( none | shared )
31982 OpenACC:
31983 default ( none | present ) */
31985 static tree
31986 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31987 location_t location, bool is_oacc)
31989 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31990 tree c;
31992 matching_parens parens;
31993 if (!parens.require_open (parser))
31994 return list;
31995 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31997 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31998 const char *p = IDENTIFIER_POINTER (id);
32000 switch (p[0])
32002 case 'n':
32003 if (strcmp ("none", p) != 0)
32004 goto invalid_kind;
32005 kind = OMP_CLAUSE_DEFAULT_NONE;
32006 break;
32008 case 'p':
32009 if (strcmp ("present", p) != 0 || !is_oacc)
32010 goto invalid_kind;
32011 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32012 break;
32014 case 's':
32015 if (strcmp ("shared", p) != 0 || is_oacc)
32016 goto invalid_kind;
32017 kind = OMP_CLAUSE_DEFAULT_SHARED;
32018 break;
32020 default:
32021 goto invalid_kind;
32024 cp_lexer_consume_token (parser->lexer);
32026 else
32028 invalid_kind:
32029 if (is_oacc)
32030 cp_parser_error (parser, "expected %<none%> or %<present%>");
32031 else
32032 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32035 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32036 || !parens.require_close (parser))
32037 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32038 /*or_comma=*/false,
32039 /*consume_paren=*/true);
32041 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32042 return list;
32044 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32045 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32046 OMP_CLAUSE_CHAIN (c) = list;
32047 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32049 return c;
32052 /* OpenMP 3.1:
32053 final ( expression ) */
32055 static tree
32056 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32058 tree t, c;
32060 matching_parens parens;
32061 if (!parens.require_open (parser))
32062 return list;
32064 t = cp_parser_condition (parser);
32066 if (t == error_mark_node
32067 || !parens.require_close (parser))
32068 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32069 /*or_comma=*/false,
32070 /*consume_paren=*/true);
32072 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32074 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32075 OMP_CLAUSE_FINAL_EXPR (c) = t;
32076 OMP_CLAUSE_CHAIN (c) = list;
32078 return c;
32081 /* OpenMP 2.5:
32082 if ( expression )
32084 OpenMP 4.5:
32085 if ( directive-name-modifier : expression )
32087 directive-name-modifier:
32088 parallel | task | taskloop | target data | target | target update
32089 | target enter data | target exit data */
32091 static tree
32092 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32093 bool is_omp)
32095 tree t, c;
32096 enum tree_code if_modifier = ERROR_MARK;
32098 matching_parens parens;
32099 if (!parens.require_open (parser))
32100 return list;
32102 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32104 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32105 const char *p = IDENTIFIER_POINTER (id);
32106 int n = 2;
32108 if (strcmp ("parallel", p) == 0)
32109 if_modifier = OMP_PARALLEL;
32110 else if (strcmp ("task", p) == 0)
32111 if_modifier = OMP_TASK;
32112 else if (strcmp ("taskloop", p) == 0)
32113 if_modifier = OMP_TASKLOOP;
32114 else if (strcmp ("target", p) == 0)
32116 if_modifier = OMP_TARGET;
32117 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32119 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32120 p = IDENTIFIER_POINTER (id);
32121 if (strcmp ("data", p) == 0)
32122 if_modifier = OMP_TARGET_DATA;
32123 else if (strcmp ("update", p) == 0)
32124 if_modifier = OMP_TARGET_UPDATE;
32125 else if (strcmp ("enter", p) == 0)
32126 if_modifier = OMP_TARGET_ENTER_DATA;
32127 else if (strcmp ("exit", p) == 0)
32128 if_modifier = OMP_TARGET_EXIT_DATA;
32129 if (if_modifier != OMP_TARGET)
32130 n = 3;
32131 else
32133 location_t loc
32134 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32135 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32136 "or %<exit%>");
32137 if_modifier = ERROR_MARK;
32139 if (if_modifier == OMP_TARGET_ENTER_DATA
32140 || if_modifier == OMP_TARGET_EXIT_DATA)
32142 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32144 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32145 p = IDENTIFIER_POINTER (id);
32146 if (strcmp ("data", p) == 0)
32147 n = 4;
32149 if (n != 4)
32151 location_t loc
32152 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32153 error_at (loc, "expected %<data%>");
32154 if_modifier = ERROR_MARK;
32159 if (if_modifier != ERROR_MARK)
32161 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32163 while (n-- > 0)
32164 cp_lexer_consume_token (parser->lexer);
32166 else
32168 if (n > 2)
32170 location_t loc
32171 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32172 error_at (loc, "expected %<:%>");
32174 if_modifier = ERROR_MARK;
32179 t = cp_parser_condition (parser);
32181 if (t == error_mark_node
32182 || !parens.require_close (parser))
32183 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32184 /*or_comma=*/false,
32185 /*consume_paren=*/true);
32187 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32188 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32190 if (if_modifier != ERROR_MARK
32191 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32193 const char *p = NULL;
32194 switch (if_modifier)
32196 case OMP_PARALLEL: p = "parallel"; break;
32197 case OMP_TASK: p = "task"; break;
32198 case OMP_TASKLOOP: p = "taskloop"; break;
32199 case OMP_TARGET_DATA: p = "target data"; break;
32200 case OMP_TARGET: p = "target"; break;
32201 case OMP_TARGET_UPDATE: p = "target update"; break;
32202 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32203 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32204 default: gcc_unreachable ();
32206 error_at (location, "too many %<if%> clauses with %qs modifier",
32208 return list;
32210 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32212 if (!is_omp)
32213 error_at (location, "too many %<if%> clauses");
32214 else
32215 error_at (location, "too many %<if%> clauses without modifier");
32216 return list;
32218 else if (if_modifier == ERROR_MARK
32219 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32221 error_at (location, "if any %<if%> clause has modifier, then all "
32222 "%<if%> clauses have to use modifier");
32223 return list;
32227 c = build_omp_clause (location, OMP_CLAUSE_IF);
32228 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32229 OMP_CLAUSE_IF_EXPR (c) = t;
32230 OMP_CLAUSE_CHAIN (c) = list;
32232 return c;
32235 /* OpenMP 3.1:
32236 mergeable */
32238 static tree
32239 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32240 tree list, location_t location)
32242 tree c;
32244 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32245 location);
32247 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32248 OMP_CLAUSE_CHAIN (c) = list;
32249 return c;
32252 /* OpenMP 2.5:
32253 nowait */
32255 static tree
32256 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32257 tree list, location_t location)
32259 tree c;
32261 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32263 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32264 OMP_CLAUSE_CHAIN (c) = list;
32265 return c;
32268 /* OpenMP 2.5:
32269 num_threads ( expression ) */
32271 static tree
32272 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32273 location_t location)
32275 tree t, c;
32277 matching_parens parens;
32278 if (!parens.require_open (parser))
32279 return list;
32281 t = cp_parser_expression (parser);
32283 if (t == error_mark_node
32284 || !parens.require_close (parser))
32285 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32286 /*or_comma=*/false,
32287 /*consume_paren=*/true);
32289 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32290 "num_threads", location);
32292 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32293 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32294 OMP_CLAUSE_CHAIN (c) = list;
32296 return c;
32299 /* OpenMP 4.5:
32300 num_tasks ( expression ) */
32302 static tree
32303 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32304 location_t location)
32306 tree t, c;
32308 matching_parens parens;
32309 if (!parens.require_open (parser))
32310 return list;
32312 t = cp_parser_expression (parser);
32314 if (t == error_mark_node
32315 || !parens.require_close (parser))
32316 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32317 /*or_comma=*/false,
32318 /*consume_paren=*/true);
32320 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32321 "num_tasks", location);
32323 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32324 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32325 OMP_CLAUSE_CHAIN (c) = list;
32327 return c;
32330 /* OpenMP 4.5:
32331 grainsize ( expression ) */
32333 static tree
32334 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32335 location_t location)
32337 tree t, c;
32339 matching_parens parens;
32340 if (!parens.require_open (parser))
32341 return list;
32343 t = cp_parser_expression (parser);
32345 if (t == error_mark_node
32346 || !parens.require_close (parser))
32347 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32348 /*or_comma=*/false,
32349 /*consume_paren=*/true);
32351 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32352 "grainsize", location);
32354 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32355 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32356 OMP_CLAUSE_CHAIN (c) = list;
32358 return c;
32361 /* OpenMP 4.5:
32362 priority ( expression ) */
32364 static tree
32365 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32366 location_t location)
32368 tree t, c;
32370 matching_parens parens;
32371 if (!parens.require_open (parser))
32372 return list;
32374 t = cp_parser_expression (parser);
32376 if (t == error_mark_node
32377 || !parens.require_close (parser))
32378 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32379 /*or_comma=*/false,
32380 /*consume_paren=*/true);
32382 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32383 "priority", location);
32385 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32386 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32387 OMP_CLAUSE_CHAIN (c) = list;
32389 return c;
32392 /* OpenMP 4.5:
32393 hint ( expression ) */
32395 static tree
32396 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32397 location_t location)
32399 tree t, c;
32401 matching_parens parens;
32402 if (!parens.require_open (parser))
32403 return list;
32405 t = cp_parser_expression (parser);
32407 if (t == error_mark_node
32408 || !parens.require_close (parser))
32409 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32410 /*or_comma=*/false,
32411 /*consume_paren=*/true);
32413 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32415 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32416 OMP_CLAUSE_HINT_EXPR (c) = t;
32417 OMP_CLAUSE_CHAIN (c) = list;
32419 return c;
32422 /* OpenMP 4.5:
32423 defaultmap ( tofrom : scalar ) */
32425 static tree
32426 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32427 location_t location)
32429 tree c, id;
32430 const char *p;
32432 matching_parens parens;
32433 if (!parens.require_open (parser))
32434 return list;
32436 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32438 cp_parser_error (parser, "expected %<tofrom%>");
32439 goto out_err;
32441 id = cp_lexer_peek_token (parser->lexer)->u.value;
32442 p = IDENTIFIER_POINTER (id);
32443 if (strcmp (p, "tofrom") != 0)
32445 cp_parser_error (parser, "expected %<tofrom%>");
32446 goto out_err;
32448 cp_lexer_consume_token (parser->lexer);
32449 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32450 goto out_err;
32452 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32454 cp_parser_error (parser, "expected %<scalar%>");
32455 goto out_err;
32457 id = cp_lexer_peek_token (parser->lexer)->u.value;
32458 p = IDENTIFIER_POINTER (id);
32459 if (strcmp (p, "scalar") != 0)
32461 cp_parser_error (parser, "expected %<scalar%>");
32462 goto out_err;
32464 cp_lexer_consume_token (parser->lexer);
32465 if (!parens.require_close (parser))
32466 goto out_err;
32468 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32469 location);
32471 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32472 OMP_CLAUSE_CHAIN (c) = list;
32473 return c;
32475 out_err:
32476 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32477 /*or_comma=*/false,
32478 /*consume_paren=*/true);
32479 return list;
32482 /* OpenMP 2.5:
32483 ordered
32485 OpenMP 4.5:
32486 ordered ( constant-expression ) */
32488 static tree
32489 cp_parser_omp_clause_ordered (cp_parser *parser,
32490 tree list, location_t location)
32492 tree c, num = NULL_TREE;
32493 HOST_WIDE_INT n;
32495 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32496 "ordered", location);
32498 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32500 matching_parens parens;
32501 parens.consume_open (parser);
32503 num = cp_parser_constant_expression (parser);
32505 if (!parens.require_close (parser))
32506 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32507 /*or_comma=*/false,
32508 /*consume_paren=*/true);
32510 if (num == error_mark_node)
32511 return list;
32512 num = fold_non_dependent_expr (num);
32513 if (!tree_fits_shwi_p (num)
32514 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32515 || (n = tree_to_shwi (num)) <= 0
32516 || (int) n != n)
32518 error_at (location,
32519 "ordered argument needs positive constant integer "
32520 "expression");
32521 return list;
32525 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32526 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32527 OMP_CLAUSE_CHAIN (c) = list;
32528 return c;
32531 /* OpenMP 2.5:
32532 reduction ( reduction-operator : variable-list )
32534 reduction-operator:
32535 One of: + * - & ^ | && ||
32537 OpenMP 3.1:
32539 reduction-operator:
32540 One of: + * - & ^ | && || min max
32542 OpenMP 4.0:
32544 reduction-operator:
32545 One of: + * - & ^ | && ||
32546 id-expression */
32548 static tree
32549 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32551 enum tree_code code = ERROR_MARK;
32552 tree nlist, c, id = NULL_TREE;
32554 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32555 return list;
32557 switch (cp_lexer_peek_token (parser->lexer)->type)
32559 case CPP_PLUS: code = PLUS_EXPR; break;
32560 case CPP_MULT: code = MULT_EXPR; break;
32561 case CPP_MINUS: code = MINUS_EXPR; break;
32562 case CPP_AND: code = BIT_AND_EXPR; break;
32563 case CPP_XOR: code = BIT_XOR_EXPR; break;
32564 case CPP_OR: code = BIT_IOR_EXPR; break;
32565 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32566 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32567 default: break;
32570 if (code != ERROR_MARK)
32571 cp_lexer_consume_token (parser->lexer);
32572 else
32574 bool saved_colon_corrects_to_scope_p;
32575 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32576 parser->colon_corrects_to_scope_p = false;
32577 id = cp_parser_id_expression (parser, /*template_p=*/false,
32578 /*check_dependency_p=*/true,
32579 /*template_p=*/NULL,
32580 /*declarator_p=*/false,
32581 /*optional_p=*/false);
32582 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32583 if (identifier_p (id))
32585 const char *p = IDENTIFIER_POINTER (id);
32587 if (strcmp (p, "min") == 0)
32588 code = MIN_EXPR;
32589 else if (strcmp (p, "max") == 0)
32590 code = MAX_EXPR;
32591 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32592 code = PLUS_EXPR;
32593 else if (id == ovl_op_identifier (false, MULT_EXPR))
32594 code = MULT_EXPR;
32595 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32596 code = MINUS_EXPR;
32597 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32598 code = BIT_AND_EXPR;
32599 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32600 code = BIT_IOR_EXPR;
32601 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32602 code = BIT_XOR_EXPR;
32603 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32604 code = TRUTH_ANDIF_EXPR;
32605 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32606 code = TRUTH_ORIF_EXPR;
32607 id = omp_reduction_id (code, id, NULL_TREE);
32608 tree scope = parser->scope;
32609 if (scope)
32610 id = build_qualified_name (NULL_TREE, scope, id, false);
32611 parser->scope = NULL_TREE;
32612 parser->qualifying_scope = NULL_TREE;
32613 parser->object_scope = NULL_TREE;
32615 else
32617 error ("invalid reduction-identifier");
32618 resync_fail:
32619 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32620 /*or_comma=*/false,
32621 /*consume_paren=*/true);
32622 return list;
32626 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32627 goto resync_fail;
32629 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32630 NULL);
32631 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32633 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32634 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32637 return nlist;
32640 /* OpenMP 2.5:
32641 schedule ( schedule-kind )
32642 schedule ( schedule-kind , expression )
32644 schedule-kind:
32645 static | dynamic | guided | runtime | auto
32647 OpenMP 4.5:
32648 schedule ( schedule-modifier : schedule-kind )
32649 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32651 schedule-modifier:
32652 simd
32653 monotonic
32654 nonmonotonic */
32656 static tree
32657 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32659 tree c, t;
32660 int modifiers = 0, nmodifiers = 0;
32662 matching_parens parens;
32663 if (!parens.require_open (parser))
32664 return list;
32666 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32668 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32670 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32671 const char *p = IDENTIFIER_POINTER (id);
32672 if (strcmp ("simd", p) == 0)
32673 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32674 else if (strcmp ("monotonic", p) == 0)
32675 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32676 else if (strcmp ("nonmonotonic", p) == 0)
32677 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32678 else
32679 break;
32680 cp_lexer_consume_token (parser->lexer);
32681 if (nmodifiers++ == 0
32682 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32683 cp_lexer_consume_token (parser->lexer);
32684 else
32686 cp_parser_require (parser, CPP_COLON, RT_COLON);
32687 break;
32691 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32693 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32694 const char *p = IDENTIFIER_POINTER (id);
32696 switch (p[0])
32698 case 'd':
32699 if (strcmp ("dynamic", p) != 0)
32700 goto invalid_kind;
32701 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32702 break;
32704 case 'g':
32705 if (strcmp ("guided", p) != 0)
32706 goto invalid_kind;
32707 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32708 break;
32710 case 'r':
32711 if (strcmp ("runtime", p) != 0)
32712 goto invalid_kind;
32713 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32714 break;
32716 default:
32717 goto invalid_kind;
32720 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32721 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32722 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32723 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32724 else
32725 goto invalid_kind;
32726 cp_lexer_consume_token (parser->lexer);
32728 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32729 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32730 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32731 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32733 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32734 "specified");
32735 modifiers = 0;
32738 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32740 cp_token *token;
32741 cp_lexer_consume_token (parser->lexer);
32743 token = cp_lexer_peek_token (parser->lexer);
32744 t = cp_parser_assignment_expression (parser);
32746 if (t == error_mark_node)
32747 goto resync_fail;
32748 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32749 error_at (token->location, "schedule %<runtime%> does not take "
32750 "a %<chunk_size%> parameter");
32751 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32752 error_at (token->location, "schedule %<auto%> does not take "
32753 "a %<chunk_size%> parameter");
32754 else
32755 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32757 if (!parens.require_close (parser))
32758 goto resync_fail;
32760 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32761 goto resync_fail;
32763 OMP_CLAUSE_SCHEDULE_KIND (c)
32764 = (enum omp_clause_schedule_kind)
32765 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32767 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32768 OMP_CLAUSE_CHAIN (c) = list;
32769 return c;
32771 invalid_kind:
32772 cp_parser_error (parser, "invalid schedule kind");
32773 resync_fail:
32774 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32775 /*or_comma=*/false,
32776 /*consume_paren=*/true);
32777 return list;
32780 /* OpenMP 3.0:
32781 untied */
32783 static tree
32784 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32785 tree list, location_t location)
32787 tree c;
32789 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32791 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32792 OMP_CLAUSE_CHAIN (c) = list;
32793 return c;
32796 /* OpenMP 4.0:
32797 inbranch
32798 notinbranch */
32800 static tree
32801 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32802 tree list, location_t location)
32804 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32805 tree c = build_omp_clause (location, code);
32806 OMP_CLAUSE_CHAIN (c) = list;
32807 return c;
32810 /* OpenMP 4.0:
32811 parallel
32813 sections
32814 taskgroup */
32816 static tree
32817 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32818 enum omp_clause_code code,
32819 tree list, location_t location)
32821 tree c = build_omp_clause (location, code);
32822 OMP_CLAUSE_CHAIN (c) = list;
32823 return c;
32826 /* OpenMP 4.5:
32827 nogroup */
32829 static tree
32830 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32831 tree list, location_t location)
32833 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32834 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32835 OMP_CLAUSE_CHAIN (c) = list;
32836 return c;
32839 /* OpenMP 4.5:
32840 simd
32841 threads */
32843 static tree
32844 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32845 enum omp_clause_code code,
32846 tree list, location_t location)
32848 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32849 tree c = build_omp_clause (location, code);
32850 OMP_CLAUSE_CHAIN (c) = list;
32851 return c;
32854 /* OpenMP 4.0:
32855 num_teams ( expression ) */
32857 static tree
32858 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32859 location_t location)
32861 tree t, c;
32863 matching_parens parens;
32864 if (!parens.require_open (parser))
32865 return list;
32867 t = cp_parser_expression (parser);
32869 if (t == error_mark_node
32870 || !parens.require_close (parser))
32871 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32872 /*or_comma=*/false,
32873 /*consume_paren=*/true);
32875 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32876 "num_teams", location);
32878 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32879 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32880 OMP_CLAUSE_CHAIN (c) = list;
32882 return c;
32885 /* OpenMP 4.0:
32886 thread_limit ( expression ) */
32888 static tree
32889 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32890 location_t location)
32892 tree t, c;
32894 matching_parens parens;
32895 if (!parens.require_open (parser))
32896 return list;
32898 t = cp_parser_expression (parser);
32900 if (t == error_mark_node
32901 || !parens.require_close (parser))
32902 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32903 /*or_comma=*/false,
32904 /*consume_paren=*/true);
32906 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32907 "thread_limit", location);
32909 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32910 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32911 OMP_CLAUSE_CHAIN (c) = list;
32913 return c;
32916 /* OpenMP 4.0:
32917 aligned ( variable-list )
32918 aligned ( variable-list : constant-expression ) */
32920 static tree
32921 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32923 tree nlist, c, alignment = NULL_TREE;
32924 bool colon;
32926 matching_parens parens;
32927 if (!parens.require_open (parser))
32928 return list;
32930 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32931 &colon);
32933 if (colon)
32935 alignment = cp_parser_constant_expression (parser);
32937 if (!parens.require_close (parser))
32938 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32939 /*or_comma=*/false,
32940 /*consume_paren=*/true);
32942 if (alignment == error_mark_node)
32943 alignment = NULL_TREE;
32946 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32947 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32949 return nlist;
32952 /* OpenMP 4.0:
32953 linear ( variable-list )
32954 linear ( variable-list : expression )
32956 OpenMP 4.5:
32957 linear ( modifier ( variable-list ) )
32958 linear ( modifier ( variable-list ) : expression ) */
32960 static tree
32961 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32962 bool is_cilk_simd_fn, bool declare_simd)
32964 tree nlist, c, step = integer_one_node;
32965 bool colon;
32966 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32968 matching_parens parens;
32969 if (!parens.require_open (parser))
32970 return list;
32972 if (!is_cilk_simd_fn
32973 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32975 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32976 const char *p = IDENTIFIER_POINTER (id);
32978 if (strcmp ("ref", p) == 0)
32979 kind = OMP_CLAUSE_LINEAR_REF;
32980 else if (strcmp ("val", p) == 0)
32981 kind = OMP_CLAUSE_LINEAR_VAL;
32982 else if (strcmp ("uval", p) == 0)
32983 kind = OMP_CLAUSE_LINEAR_UVAL;
32984 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32985 cp_lexer_consume_token (parser->lexer);
32986 else
32987 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32990 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32991 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32992 &colon);
32993 else
32995 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32996 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32997 if (colon)
32998 cp_parser_require (parser, CPP_COLON, RT_COLON);
32999 else if (!parens.require_close (parser))
33000 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33001 /*or_comma=*/false,
33002 /*consume_paren=*/true);
33005 if (colon)
33007 step = NULL_TREE;
33008 if (declare_simd
33009 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33010 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33012 cp_token *token = cp_lexer_peek_token (parser->lexer);
33013 cp_parser_parse_tentatively (parser);
33014 step = cp_parser_id_expression (parser, /*template_p=*/false,
33015 /*check_dependency_p=*/true,
33016 /*template_p=*/NULL,
33017 /*declarator_p=*/false,
33018 /*optional_p=*/false);
33019 if (step != error_mark_node)
33020 step = cp_parser_lookup_name_simple (parser, step, token->location);
33021 if (step == error_mark_node)
33023 step = NULL_TREE;
33024 cp_parser_abort_tentative_parse (parser);
33026 else if (!cp_parser_parse_definitely (parser))
33027 step = NULL_TREE;
33029 if (!step)
33030 step = cp_parser_expression (parser);
33032 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
33034 sorry ("using parameters for %<linear%> step is not supported yet");
33035 step = integer_one_node;
33037 if (!parens.require_close (parser))
33038 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33039 /*or_comma=*/false,
33040 /*consume_paren=*/true);
33042 if (step == error_mark_node)
33043 return list;
33046 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33048 OMP_CLAUSE_LINEAR_STEP (c) = step;
33049 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33052 return nlist;
33055 /* OpenMP 4.0:
33056 safelen ( constant-expression ) */
33058 static tree
33059 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33060 location_t location)
33062 tree t, c;
33064 matching_parens parens;
33065 if (!parens.require_open (parser))
33066 return list;
33068 t = cp_parser_constant_expression (parser);
33070 if (t == error_mark_node
33071 || !parens.require_close (parser))
33072 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33073 /*or_comma=*/false,
33074 /*consume_paren=*/true);
33076 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33078 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33079 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33080 OMP_CLAUSE_CHAIN (c) = list;
33082 return c;
33085 /* OpenMP 4.0:
33086 simdlen ( constant-expression ) */
33088 static tree
33089 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33090 location_t location)
33092 tree t, c;
33094 matching_parens parens;
33095 if (!parens.require_open (parser))
33096 return list;
33098 t = cp_parser_constant_expression (parser);
33100 if (t == error_mark_node
33101 || !parens.require_close (parser))
33102 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33103 /*or_comma=*/false,
33104 /*consume_paren=*/true);
33106 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33108 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33109 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33110 OMP_CLAUSE_CHAIN (c) = list;
33112 return c;
33115 /* OpenMP 4.5:
33116 vec:
33117 identifier [+/- integer]
33118 vec , identifier [+/- integer]
33121 static tree
33122 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33123 tree list)
33125 tree vec = NULL;
33127 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33129 cp_parser_error (parser, "expected identifier");
33130 return list;
33133 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33135 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33136 tree t, identifier = cp_parser_identifier (parser);
33137 tree addend = NULL;
33139 if (identifier == error_mark_node)
33140 t = error_mark_node;
33141 else
33143 t = cp_parser_lookup_name_simple
33144 (parser, identifier,
33145 cp_lexer_peek_token (parser->lexer)->location);
33146 if (t == error_mark_node)
33147 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33148 id_loc);
33151 bool neg = false;
33152 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33153 neg = true;
33154 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33156 addend = integer_zero_node;
33157 goto add_to_vector;
33159 cp_lexer_consume_token (parser->lexer);
33161 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33163 cp_parser_error (parser, "expected integer");
33164 return list;
33167 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33168 if (TREE_CODE (addend) != INTEGER_CST)
33170 cp_parser_error (parser, "expected integer");
33171 return list;
33173 cp_lexer_consume_token (parser->lexer);
33175 add_to_vector:
33176 if (t != error_mark_node)
33178 vec = tree_cons (addend, t, vec);
33179 if (neg)
33180 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33183 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33184 break;
33186 cp_lexer_consume_token (parser->lexer);
33189 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33191 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33192 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33193 OMP_CLAUSE_DECL (u) = nreverse (vec);
33194 OMP_CLAUSE_CHAIN (u) = list;
33195 return u;
33197 return list;
33200 /* OpenMP 4.0:
33201 depend ( depend-kind : variable-list )
33203 depend-kind:
33204 in | out | inout
33206 OpenMP 4.5:
33207 depend ( source )
33209 depend ( sink : vec ) */
33211 static tree
33212 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33214 tree nlist, c;
33215 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33217 matching_parens parens;
33218 if (!parens.require_open (parser))
33219 return list;
33221 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33223 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33224 const char *p = IDENTIFIER_POINTER (id);
33226 if (strcmp ("in", p) == 0)
33227 kind = OMP_CLAUSE_DEPEND_IN;
33228 else if (strcmp ("inout", p) == 0)
33229 kind = OMP_CLAUSE_DEPEND_INOUT;
33230 else if (strcmp ("out", p) == 0)
33231 kind = OMP_CLAUSE_DEPEND_OUT;
33232 else if (strcmp ("source", p) == 0)
33233 kind = OMP_CLAUSE_DEPEND_SOURCE;
33234 else if (strcmp ("sink", p) == 0)
33235 kind = OMP_CLAUSE_DEPEND_SINK;
33236 else
33237 goto invalid_kind;
33239 else
33240 goto invalid_kind;
33242 cp_lexer_consume_token (parser->lexer);
33244 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33246 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33247 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33248 OMP_CLAUSE_DECL (c) = NULL_TREE;
33249 OMP_CLAUSE_CHAIN (c) = list;
33250 if (!parens.require_close (parser))
33251 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33252 /*or_comma=*/false,
33253 /*consume_paren=*/true);
33254 return c;
33257 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33258 goto resync_fail;
33260 if (kind == OMP_CLAUSE_DEPEND_SINK)
33261 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33262 else
33264 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33265 list, NULL);
33267 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33268 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33270 return nlist;
33272 invalid_kind:
33273 cp_parser_error (parser, "invalid depend kind");
33274 resync_fail:
33275 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33276 /*or_comma=*/false,
33277 /*consume_paren=*/true);
33278 return list;
33281 /* OpenMP 4.0:
33282 map ( map-kind : variable-list )
33283 map ( variable-list )
33285 map-kind:
33286 alloc | to | from | tofrom
33288 OpenMP 4.5:
33289 map-kind:
33290 alloc | to | from | tofrom | release | delete
33292 map ( always [,] map-kind: variable-list ) */
33294 static tree
33295 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33297 tree nlist, c;
33298 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33299 bool always = false;
33301 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33302 return list;
33304 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33306 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33307 const char *p = IDENTIFIER_POINTER (id);
33309 if (strcmp ("always", p) == 0)
33311 int nth = 2;
33312 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33313 nth++;
33314 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33315 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33316 == RID_DELETE))
33317 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33318 == CPP_COLON))
33320 always = true;
33321 cp_lexer_consume_token (parser->lexer);
33322 if (nth == 3)
33323 cp_lexer_consume_token (parser->lexer);
33328 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33329 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33331 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33332 const char *p = IDENTIFIER_POINTER (id);
33334 if (strcmp ("alloc", p) == 0)
33335 kind = GOMP_MAP_ALLOC;
33336 else if (strcmp ("to", p) == 0)
33337 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33338 else if (strcmp ("from", p) == 0)
33339 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33340 else if (strcmp ("tofrom", p) == 0)
33341 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33342 else if (strcmp ("release", p) == 0)
33343 kind = GOMP_MAP_RELEASE;
33344 else
33346 cp_parser_error (parser, "invalid map kind");
33347 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33348 /*or_comma=*/false,
33349 /*consume_paren=*/true);
33350 return list;
33352 cp_lexer_consume_token (parser->lexer);
33353 cp_lexer_consume_token (parser->lexer);
33355 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33356 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33358 kind = GOMP_MAP_DELETE;
33359 cp_lexer_consume_token (parser->lexer);
33360 cp_lexer_consume_token (parser->lexer);
33363 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33364 NULL);
33366 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33367 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33369 return nlist;
33372 /* OpenMP 4.0:
33373 device ( expression ) */
33375 static tree
33376 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33377 location_t location)
33379 tree t, c;
33381 matching_parens parens;
33382 if (!parens.require_open (parser))
33383 return list;
33385 t = cp_parser_expression (parser);
33387 if (t == error_mark_node
33388 || !parens.require_close (parser))
33389 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33390 /*or_comma=*/false,
33391 /*consume_paren=*/true);
33393 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33394 "device", location);
33396 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33397 OMP_CLAUSE_DEVICE_ID (c) = t;
33398 OMP_CLAUSE_CHAIN (c) = list;
33400 return c;
33403 /* OpenMP 4.0:
33404 dist_schedule ( static )
33405 dist_schedule ( static , expression ) */
33407 static tree
33408 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33409 location_t location)
33411 tree c, t;
33413 matching_parens parens;
33414 if (!parens.require_open (parser))
33415 return list;
33417 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33419 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33420 goto invalid_kind;
33421 cp_lexer_consume_token (parser->lexer);
33423 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33425 cp_lexer_consume_token (parser->lexer);
33427 t = cp_parser_assignment_expression (parser);
33429 if (t == error_mark_node)
33430 goto resync_fail;
33431 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33433 if (!parens.require_close (parser))
33434 goto resync_fail;
33436 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33437 goto resync_fail;
33439 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33440 location);
33441 OMP_CLAUSE_CHAIN (c) = list;
33442 return c;
33444 invalid_kind:
33445 cp_parser_error (parser, "invalid dist_schedule kind");
33446 resync_fail:
33447 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33448 /*or_comma=*/false,
33449 /*consume_paren=*/true);
33450 return list;
33453 /* OpenMP 4.0:
33454 proc_bind ( proc-bind-kind )
33456 proc-bind-kind:
33457 master | close | spread */
33459 static tree
33460 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33461 location_t location)
33463 tree c;
33464 enum omp_clause_proc_bind_kind kind;
33466 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33467 return list;
33469 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33471 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33472 const char *p = IDENTIFIER_POINTER (id);
33474 if (strcmp ("master", p) == 0)
33475 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33476 else if (strcmp ("close", p) == 0)
33477 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33478 else if (strcmp ("spread", p) == 0)
33479 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33480 else
33481 goto invalid_kind;
33483 else
33484 goto invalid_kind;
33486 cp_lexer_consume_token (parser->lexer);
33487 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33488 goto resync_fail;
33490 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33491 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33492 location);
33493 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33494 OMP_CLAUSE_CHAIN (c) = list;
33495 return c;
33497 invalid_kind:
33498 cp_parser_error (parser, "invalid depend kind");
33499 resync_fail:
33500 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33501 /*or_comma=*/false,
33502 /*consume_paren=*/true);
33503 return list;
33506 /* OpenACC:
33507 async [( int-expr )] */
33509 static tree
33510 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33512 tree c, t;
33513 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33515 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33517 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33519 matching_parens parens;
33520 parens.consume_open (parser);
33522 t = cp_parser_expression (parser);
33523 if (t == error_mark_node
33524 || !parens.require_close (parser))
33525 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33526 /*or_comma=*/false,
33527 /*consume_paren=*/true);
33530 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33532 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33533 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33534 OMP_CLAUSE_CHAIN (c) = list;
33535 list = c;
33537 return list;
33540 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33541 is a bitmask in MASK. Return the list of clauses found. */
33543 static tree
33544 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33545 const char *where, cp_token *pragma_tok,
33546 bool finish_p = true)
33548 tree clauses = NULL;
33549 bool first = true;
33551 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33553 location_t here;
33554 pragma_omp_clause c_kind;
33555 omp_clause_code code;
33556 const char *c_name;
33557 tree prev = clauses;
33559 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33560 cp_lexer_consume_token (parser->lexer);
33562 here = cp_lexer_peek_token (parser->lexer)->location;
33563 c_kind = cp_parser_omp_clause_name (parser);
33565 switch (c_kind)
33567 case PRAGMA_OACC_CLAUSE_ASYNC:
33568 clauses = cp_parser_oacc_clause_async (parser, clauses);
33569 c_name = "async";
33570 break;
33571 case PRAGMA_OACC_CLAUSE_AUTO:
33572 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33573 clauses, here);
33574 c_name = "auto";
33575 break;
33576 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33577 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33578 c_name = "collapse";
33579 break;
33580 case PRAGMA_OACC_CLAUSE_COPY:
33581 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33582 c_name = "copy";
33583 break;
33584 case PRAGMA_OACC_CLAUSE_COPYIN:
33585 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33586 c_name = "copyin";
33587 break;
33588 case PRAGMA_OACC_CLAUSE_COPYOUT:
33589 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33590 c_name = "copyout";
33591 break;
33592 case PRAGMA_OACC_CLAUSE_CREATE:
33593 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33594 c_name = "create";
33595 break;
33596 case PRAGMA_OACC_CLAUSE_DELETE:
33597 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33598 c_name = "delete";
33599 break;
33600 case PRAGMA_OMP_CLAUSE_DEFAULT:
33601 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33602 c_name = "default";
33603 break;
33604 case PRAGMA_OACC_CLAUSE_DEVICE:
33605 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33606 c_name = "device";
33607 break;
33608 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33609 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33610 c_name = "deviceptr";
33611 break;
33612 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33613 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33614 c_name = "device_resident";
33615 break;
33616 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33617 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33618 clauses);
33619 c_name = "firstprivate";
33620 break;
33621 case PRAGMA_OACC_CLAUSE_GANG:
33622 c_name = "gang";
33623 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33624 c_name, clauses);
33625 break;
33626 case PRAGMA_OACC_CLAUSE_HOST:
33627 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33628 c_name = "host";
33629 break;
33630 case PRAGMA_OACC_CLAUSE_IF:
33631 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33632 c_name = "if";
33633 break;
33634 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33635 clauses = cp_parser_oacc_simple_clause (parser,
33636 OMP_CLAUSE_INDEPENDENT,
33637 clauses, here);
33638 c_name = "independent";
33639 break;
33640 case PRAGMA_OACC_CLAUSE_LINK:
33641 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33642 c_name = "link";
33643 break;
33644 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33645 code = OMP_CLAUSE_NUM_GANGS;
33646 c_name = "num_gangs";
33647 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33648 clauses);
33649 break;
33650 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33651 c_name = "num_workers";
33652 code = OMP_CLAUSE_NUM_WORKERS;
33653 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33654 clauses);
33655 break;
33656 case PRAGMA_OACC_CLAUSE_PRESENT:
33657 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33658 c_name = "present";
33659 break;
33660 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33661 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33662 c_name = "present_or_copy";
33663 break;
33664 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33665 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33666 c_name = "present_or_copyin";
33667 break;
33668 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33669 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33670 c_name = "present_or_copyout";
33671 break;
33672 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33673 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33674 c_name = "present_or_create";
33675 break;
33676 case PRAGMA_OACC_CLAUSE_PRIVATE:
33677 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33678 clauses);
33679 c_name = "private";
33680 break;
33681 case PRAGMA_OACC_CLAUSE_REDUCTION:
33682 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33683 c_name = "reduction";
33684 break;
33685 case PRAGMA_OACC_CLAUSE_SELF:
33686 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33687 c_name = "self";
33688 break;
33689 case PRAGMA_OACC_CLAUSE_SEQ:
33690 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33691 clauses, here);
33692 c_name = "seq";
33693 break;
33694 case PRAGMA_OACC_CLAUSE_TILE:
33695 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33696 c_name = "tile";
33697 break;
33698 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33699 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33700 clauses);
33701 c_name = "use_device";
33702 break;
33703 case PRAGMA_OACC_CLAUSE_VECTOR:
33704 c_name = "vector";
33705 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33706 c_name, clauses);
33707 break;
33708 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33709 c_name = "vector_length";
33710 code = OMP_CLAUSE_VECTOR_LENGTH;
33711 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33712 clauses);
33713 break;
33714 case PRAGMA_OACC_CLAUSE_WAIT:
33715 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33716 c_name = "wait";
33717 break;
33718 case PRAGMA_OACC_CLAUSE_WORKER:
33719 c_name = "worker";
33720 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33721 c_name, clauses);
33722 break;
33723 default:
33724 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33725 goto saw_error;
33728 first = false;
33730 if (((mask >> c_kind) & 1) == 0)
33732 /* Remove the invalid clause(s) from the list to avoid
33733 confusing the rest of the compiler. */
33734 clauses = prev;
33735 error_at (here, "%qs is not valid for %qs", c_name, where);
33739 saw_error:
33740 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33742 if (finish_p)
33743 return finish_omp_clauses (clauses, C_ORT_ACC);
33745 return clauses;
33748 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33749 is a bitmask in MASK. Return the list of clauses found; the result
33750 of clause default goes in *pdefault. */
33752 static tree
33753 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33754 const char *where, cp_token *pragma_tok,
33755 bool finish_p = true)
33757 tree clauses = NULL;
33758 bool first = true;
33759 cp_token *token = NULL;
33761 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33763 pragma_omp_clause c_kind;
33764 const char *c_name;
33765 tree prev = clauses;
33767 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33768 cp_lexer_consume_token (parser->lexer);
33770 token = cp_lexer_peek_token (parser->lexer);
33771 c_kind = cp_parser_omp_clause_name (parser);
33773 switch (c_kind)
33775 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33776 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33777 token->location);
33778 c_name = "collapse";
33779 break;
33780 case PRAGMA_OMP_CLAUSE_COPYIN:
33781 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33782 c_name = "copyin";
33783 break;
33784 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33785 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33786 clauses);
33787 c_name = "copyprivate";
33788 break;
33789 case PRAGMA_OMP_CLAUSE_DEFAULT:
33790 clauses = cp_parser_omp_clause_default (parser, clauses,
33791 token->location, false);
33792 c_name = "default";
33793 break;
33794 case PRAGMA_OMP_CLAUSE_FINAL:
33795 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33796 c_name = "final";
33797 break;
33798 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33799 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33800 clauses);
33801 c_name = "firstprivate";
33802 break;
33803 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33804 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33805 token->location);
33806 c_name = "grainsize";
33807 break;
33808 case PRAGMA_OMP_CLAUSE_HINT:
33809 clauses = cp_parser_omp_clause_hint (parser, clauses,
33810 token->location);
33811 c_name = "hint";
33812 break;
33813 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33814 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33815 token->location);
33816 c_name = "defaultmap";
33817 break;
33818 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33819 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33820 clauses);
33821 c_name = "use_device_ptr";
33822 break;
33823 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33824 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33825 clauses);
33826 c_name = "is_device_ptr";
33827 break;
33828 case PRAGMA_OMP_CLAUSE_IF:
33829 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33830 true);
33831 c_name = "if";
33832 break;
33833 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33834 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33835 clauses);
33836 c_name = "lastprivate";
33837 break;
33838 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33839 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33840 token->location);
33841 c_name = "mergeable";
33842 break;
33843 case PRAGMA_OMP_CLAUSE_NOWAIT:
33844 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33845 c_name = "nowait";
33846 break;
33847 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33848 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33849 token->location);
33850 c_name = "num_tasks";
33851 break;
33852 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33853 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33854 token->location);
33855 c_name = "num_threads";
33856 break;
33857 case PRAGMA_OMP_CLAUSE_ORDERED:
33858 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33859 token->location);
33860 c_name = "ordered";
33861 break;
33862 case PRAGMA_OMP_CLAUSE_PRIORITY:
33863 clauses = cp_parser_omp_clause_priority (parser, clauses,
33864 token->location);
33865 c_name = "priority";
33866 break;
33867 case PRAGMA_OMP_CLAUSE_PRIVATE:
33868 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33869 clauses);
33870 c_name = "private";
33871 break;
33872 case PRAGMA_OMP_CLAUSE_REDUCTION:
33873 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33874 c_name = "reduction";
33875 break;
33876 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33877 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33878 token->location);
33879 c_name = "schedule";
33880 break;
33881 case PRAGMA_OMP_CLAUSE_SHARED:
33882 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33883 clauses);
33884 c_name = "shared";
33885 break;
33886 case PRAGMA_OMP_CLAUSE_UNTIED:
33887 clauses = cp_parser_omp_clause_untied (parser, clauses,
33888 token->location);
33889 c_name = "untied";
33890 break;
33891 case PRAGMA_OMP_CLAUSE_INBRANCH:
33892 case PRAGMA_CILK_CLAUSE_MASK:
33893 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33894 clauses, token->location);
33895 c_name = "inbranch";
33896 break;
33897 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33898 case PRAGMA_CILK_CLAUSE_NOMASK:
33899 clauses = cp_parser_omp_clause_branch (parser,
33900 OMP_CLAUSE_NOTINBRANCH,
33901 clauses, token->location);
33902 c_name = "notinbranch";
33903 break;
33904 case PRAGMA_OMP_CLAUSE_PARALLEL:
33905 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33906 clauses, token->location);
33907 c_name = "parallel";
33908 if (!first)
33910 clause_not_first:
33911 error_at (token->location, "%qs must be the first clause of %qs",
33912 c_name, where);
33913 clauses = prev;
33915 break;
33916 case PRAGMA_OMP_CLAUSE_FOR:
33917 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33918 clauses, token->location);
33919 c_name = "for";
33920 if (!first)
33921 goto clause_not_first;
33922 break;
33923 case PRAGMA_OMP_CLAUSE_SECTIONS:
33924 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33925 clauses, token->location);
33926 c_name = "sections";
33927 if (!first)
33928 goto clause_not_first;
33929 break;
33930 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33931 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33932 clauses, token->location);
33933 c_name = "taskgroup";
33934 if (!first)
33935 goto clause_not_first;
33936 break;
33937 case PRAGMA_OMP_CLAUSE_LINK:
33938 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33939 c_name = "to";
33940 break;
33941 case PRAGMA_OMP_CLAUSE_TO:
33942 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33943 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33944 clauses);
33945 else
33946 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33947 c_name = "to";
33948 break;
33949 case PRAGMA_OMP_CLAUSE_FROM:
33950 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33951 c_name = "from";
33952 break;
33953 case PRAGMA_OMP_CLAUSE_UNIFORM:
33954 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33955 clauses);
33956 c_name = "uniform";
33957 break;
33958 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33959 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33960 token->location);
33961 c_name = "num_teams";
33962 break;
33963 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33964 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33965 token->location);
33966 c_name = "thread_limit";
33967 break;
33968 case PRAGMA_OMP_CLAUSE_ALIGNED:
33969 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33970 c_name = "aligned";
33971 break;
33972 case PRAGMA_OMP_CLAUSE_LINEAR:
33974 bool cilk_simd_fn = false, declare_simd = false;
33975 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33976 cilk_simd_fn = true;
33977 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33978 declare_simd = true;
33979 clauses = cp_parser_omp_clause_linear (parser, clauses,
33980 cilk_simd_fn, declare_simd);
33982 c_name = "linear";
33983 break;
33984 case PRAGMA_OMP_CLAUSE_DEPEND:
33985 clauses = cp_parser_omp_clause_depend (parser, clauses,
33986 token->location);
33987 c_name = "depend";
33988 break;
33989 case PRAGMA_OMP_CLAUSE_MAP:
33990 clauses = cp_parser_omp_clause_map (parser, clauses);
33991 c_name = "map";
33992 break;
33993 case PRAGMA_OMP_CLAUSE_DEVICE:
33994 clauses = cp_parser_omp_clause_device (parser, clauses,
33995 token->location);
33996 c_name = "device";
33997 break;
33998 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33999 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34000 token->location);
34001 c_name = "dist_schedule";
34002 break;
34003 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34004 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34005 token->location);
34006 c_name = "proc_bind";
34007 break;
34008 case PRAGMA_OMP_CLAUSE_SAFELEN:
34009 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34010 token->location);
34011 c_name = "safelen";
34012 break;
34013 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34014 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34015 token->location);
34016 c_name = "simdlen";
34017 break;
34018 case PRAGMA_OMP_CLAUSE_NOGROUP:
34019 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34020 token->location);
34021 c_name = "nogroup";
34022 break;
34023 case PRAGMA_OMP_CLAUSE_THREADS:
34024 clauses
34025 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34026 clauses, token->location);
34027 c_name = "threads";
34028 break;
34029 case PRAGMA_OMP_CLAUSE_SIMD:
34030 clauses
34031 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34032 clauses, token->location);
34033 c_name = "simd";
34034 break;
34035 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
34036 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
34037 c_name = "simdlen";
34038 break;
34039 default:
34040 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34041 goto saw_error;
34044 first = false;
34046 if (((mask >> c_kind) & 1) == 0)
34048 /* Remove the invalid clause(s) from the list to avoid
34049 confusing the rest of the compiler. */
34050 clauses = prev;
34051 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34054 saw_error:
34055 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
34056 no reason to skip to the end. */
34057 if (!(flag_cilkplus && pragma_tok == NULL))
34058 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34059 if (finish_p)
34061 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34062 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34063 else
34064 return finish_omp_clauses (clauses, C_ORT_OMP);
34066 return clauses;
34069 /* OpenMP 2.5:
34070 structured-block:
34071 statement
34073 In practice, we're also interested in adding the statement to an
34074 outer node. So it is convenient if we work around the fact that
34075 cp_parser_statement calls add_stmt. */
34077 static unsigned
34078 cp_parser_begin_omp_structured_block (cp_parser *parser)
34080 unsigned save = parser->in_statement;
34082 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34083 This preserves the "not within loop or switch" style error messages
34084 for nonsense cases like
34085 void foo() {
34086 #pragma omp single
34087 break;
34090 if (parser->in_statement)
34091 parser->in_statement = IN_OMP_BLOCK;
34093 return save;
34096 static void
34097 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34099 parser->in_statement = save;
34102 static tree
34103 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34105 tree stmt = begin_omp_structured_block ();
34106 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34108 cp_parser_statement (parser, NULL_TREE, false, if_p);
34110 cp_parser_end_omp_structured_block (parser, save);
34111 return finish_omp_structured_block (stmt);
34114 /* OpenMP 2.5:
34115 # pragma omp atomic new-line
34116 expression-stmt
34118 expression-stmt:
34119 x binop= expr | x++ | ++x | x-- | --x
34120 binop:
34121 +, *, -, /, &, ^, |, <<, >>
34123 where x is an lvalue expression with scalar type.
34125 OpenMP 3.1:
34126 # pragma omp atomic new-line
34127 update-stmt
34129 # pragma omp atomic read new-line
34130 read-stmt
34132 # pragma omp atomic write new-line
34133 write-stmt
34135 # pragma omp atomic update new-line
34136 update-stmt
34138 # pragma omp atomic capture new-line
34139 capture-stmt
34141 # pragma omp atomic capture new-line
34142 capture-block
34144 read-stmt:
34145 v = x
34146 write-stmt:
34147 x = expr
34148 update-stmt:
34149 expression-stmt | x = x binop expr
34150 capture-stmt:
34151 v = expression-stmt
34152 capture-block:
34153 { v = x; update-stmt; } | { update-stmt; v = x; }
34155 OpenMP 4.0:
34156 update-stmt:
34157 expression-stmt | x = x binop expr | x = expr binop x
34158 capture-stmt:
34159 v = update-stmt
34160 capture-block:
34161 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34163 where x and v are lvalue expressions with scalar type. */
34165 static void
34166 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34168 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34169 tree rhs1 = NULL_TREE, orig_lhs;
34170 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34171 bool structured_block = false;
34172 bool seq_cst = false;
34174 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34176 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34177 const char *p = IDENTIFIER_POINTER (id);
34179 if (!strcmp (p, "seq_cst"))
34181 seq_cst = true;
34182 cp_lexer_consume_token (parser->lexer);
34183 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34184 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34185 cp_lexer_consume_token (parser->lexer);
34188 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34190 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34191 const char *p = IDENTIFIER_POINTER (id);
34193 if (!strcmp (p, "read"))
34194 code = OMP_ATOMIC_READ;
34195 else if (!strcmp (p, "write"))
34196 code = NOP_EXPR;
34197 else if (!strcmp (p, "update"))
34198 code = OMP_ATOMIC;
34199 else if (!strcmp (p, "capture"))
34200 code = OMP_ATOMIC_CAPTURE_NEW;
34201 else
34202 p = NULL;
34203 if (p)
34204 cp_lexer_consume_token (parser->lexer);
34206 if (!seq_cst)
34208 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34209 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34210 cp_lexer_consume_token (parser->lexer);
34212 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34214 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34215 const char *p = IDENTIFIER_POINTER (id);
34217 if (!strcmp (p, "seq_cst"))
34219 seq_cst = true;
34220 cp_lexer_consume_token (parser->lexer);
34224 cp_parser_require_pragma_eol (parser, pragma_tok);
34226 switch (code)
34228 case OMP_ATOMIC_READ:
34229 case NOP_EXPR: /* atomic write */
34230 v = cp_parser_unary_expression (parser);
34231 if (v == error_mark_node)
34232 goto saw_error;
34233 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34234 goto saw_error;
34235 if (code == NOP_EXPR)
34236 lhs = cp_parser_expression (parser);
34237 else
34238 lhs = cp_parser_unary_expression (parser);
34239 if (lhs == error_mark_node)
34240 goto saw_error;
34241 if (code == NOP_EXPR)
34243 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34244 opcode. */
34245 code = OMP_ATOMIC;
34246 rhs = lhs;
34247 lhs = v;
34248 v = NULL_TREE;
34250 goto done;
34251 case OMP_ATOMIC_CAPTURE_NEW:
34252 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34254 cp_lexer_consume_token (parser->lexer);
34255 structured_block = true;
34257 else
34259 v = cp_parser_unary_expression (parser);
34260 if (v == error_mark_node)
34261 goto saw_error;
34262 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34263 goto saw_error;
34265 default:
34266 break;
34269 restart:
34270 lhs = cp_parser_unary_expression (parser);
34271 orig_lhs = lhs;
34272 switch (TREE_CODE (lhs))
34274 case ERROR_MARK:
34275 goto saw_error;
34277 case POSTINCREMENT_EXPR:
34278 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34279 code = OMP_ATOMIC_CAPTURE_OLD;
34280 /* FALLTHROUGH */
34281 case PREINCREMENT_EXPR:
34282 lhs = TREE_OPERAND (lhs, 0);
34283 opcode = PLUS_EXPR;
34284 rhs = integer_one_node;
34285 break;
34287 case POSTDECREMENT_EXPR:
34288 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34289 code = OMP_ATOMIC_CAPTURE_OLD;
34290 /* FALLTHROUGH */
34291 case PREDECREMENT_EXPR:
34292 lhs = TREE_OPERAND (lhs, 0);
34293 opcode = MINUS_EXPR;
34294 rhs = integer_one_node;
34295 break;
34297 case COMPOUND_EXPR:
34298 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34299 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34300 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34301 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34302 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34303 (TREE_OPERAND (lhs, 1), 0), 0)))
34304 == BOOLEAN_TYPE)
34305 /* Undo effects of boolean_increment for post {in,de}crement. */
34306 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34307 /* FALLTHRU */
34308 case MODIFY_EXPR:
34309 if (TREE_CODE (lhs) == MODIFY_EXPR
34310 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34312 /* Undo effects of boolean_increment. */
34313 if (integer_onep (TREE_OPERAND (lhs, 1)))
34315 /* This is pre or post increment. */
34316 rhs = TREE_OPERAND (lhs, 1);
34317 lhs = TREE_OPERAND (lhs, 0);
34318 opcode = NOP_EXPR;
34319 if (code == OMP_ATOMIC_CAPTURE_NEW
34320 && !structured_block
34321 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34322 code = OMP_ATOMIC_CAPTURE_OLD;
34323 break;
34326 /* FALLTHRU */
34327 default:
34328 switch (cp_lexer_peek_token (parser->lexer)->type)
34330 case CPP_MULT_EQ:
34331 opcode = MULT_EXPR;
34332 break;
34333 case CPP_DIV_EQ:
34334 opcode = TRUNC_DIV_EXPR;
34335 break;
34336 case CPP_PLUS_EQ:
34337 opcode = PLUS_EXPR;
34338 break;
34339 case CPP_MINUS_EQ:
34340 opcode = MINUS_EXPR;
34341 break;
34342 case CPP_LSHIFT_EQ:
34343 opcode = LSHIFT_EXPR;
34344 break;
34345 case CPP_RSHIFT_EQ:
34346 opcode = RSHIFT_EXPR;
34347 break;
34348 case CPP_AND_EQ:
34349 opcode = BIT_AND_EXPR;
34350 break;
34351 case CPP_OR_EQ:
34352 opcode = BIT_IOR_EXPR;
34353 break;
34354 case CPP_XOR_EQ:
34355 opcode = BIT_XOR_EXPR;
34356 break;
34357 case CPP_EQ:
34358 enum cp_parser_prec oprec;
34359 cp_token *token;
34360 cp_lexer_consume_token (parser->lexer);
34361 cp_parser_parse_tentatively (parser);
34362 rhs1 = cp_parser_simple_cast_expression (parser);
34363 if (rhs1 == error_mark_node)
34365 cp_parser_abort_tentative_parse (parser);
34366 cp_parser_simple_cast_expression (parser);
34367 goto saw_error;
34369 token = cp_lexer_peek_token (parser->lexer);
34370 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34372 cp_parser_abort_tentative_parse (parser);
34373 cp_parser_parse_tentatively (parser);
34374 rhs = cp_parser_binary_expression (parser, false, true,
34375 PREC_NOT_OPERATOR, NULL);
34376 if (rhs == error_mark_node)
34378 cp_parser_abort_tentative_parse (parser);
34379 cp_parser_binary_expression (parser, false, true,
34380 PREC_NOT_OPERATOR, NULL);
34381 goto saw_error;
34383 switch (TREE_CODE (rhs))
34385 case MULT_EXPR:
34386 case TRUNC_DIV_EXPR:
34387 case RDIV_EXPR:
34388 case PLUS_EXPR:
34389 case MINUS_EXPR:
34390 case LSHIFT_EXPR:
34391 case RSHIFT_EXPR:
34392 case BIT_AND_EXPR:
34393 case BIT_IOR_EXPR:
34394 case BIT_XOR_EXPR:
34395 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34397 if (cp_parser_parse_definitely (parser))
34399 opcode = TREE_CODE (rhs);
34400 rhs1 = TREE_OPERAND (rhs, 0);
34401 rhs = TREE_OPERAND (rhs, 1);
34402 goto stmt_done;
34404 else
34405 goto saw_error;
34407 break;
34408 default:
34409 break;
34411 cp_parser_abort_tentative_parse (parser);
34412 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34414 rhs = cp_parser_expression (parser);
34415 if (rhs == error_mark_node)
34416 goto saw_error;
34417 opcode = NOP_EXPR;
34418 rhs1 = NULL_TREE;
34419 goto stmt_done;
34421 cp_parser_error (parser,
34422 "invalid form of %<#pragma omp atomic%>");
34423 goto saw_error;
34425 if (!cp_parser_parse_definitely (parser))
34426 goto saw_error;
34427 switch (token->type)
34429 case CPP_SEMICOLON:
34430 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34432 code = OMP_ATOMIC_CAPTURE_OLD;
34433 v = lhs;
34434 lhs = NULL_TREE;
34435 lhs1 = rhs1;
34436 rhs1 = NULL_TREE;
34437 cp_lexer_consume_token (parser->lexer);
34438 goto restart;
34440 else if (structured_block)
34442 opcode = NOP_EXPR;
34443 rhs = rhs1;
34444 rhs1 = NULL_TREE;
34445 goto stmt_done;
34447 cp_parser_error (parser,
34448 "invalid form of %<#pragma omp atomic%>");
34449 goto saw_error;
34450 case CPP_MULT:
34451 opcode = MULT_EXPR;
34452 break;
34453 case CPP_DIV:
34454 opcode = TRUNC_DIV_EXPR;
34455 break;
34456 case CPP_PLUS:
34457 opcode = PLUS_EXPR;
34458 break;
34459 case CPP_MINUS:
34460 opcode = MINUS_EXPR;
34461 break;
34462 case CPP_LSHIFT:
34463 opcode = LSHIFT_EXPR;
34464 break;
34465 case CPP_RSHIFT:
34466 opcode = RSHIFT_EXPR;
34467 break;
34468 case CPP_AND:
34469 opcode = BIT_AND_EXPR;
34470 break;
34471 case CPP_OR:
34472 opcode = BIT_IOR_EXPR;
34473 break;
34474 case CPP_XOR:
34475 opcode = BIT_XOR_EXPR;
34476 break;
34477 default:
34478 cp_parser_error (parser,
34479 "invalid operator for %<#pragma omp atomic%>");
34480 goto saw_error;
34482 oprec = TOKEN_PRECEDENCE (token);
34483 gcc_assert (oprec != PREC_NOT_OPERATOR);
34484 if (commutative_tree_code (opcode))
34485 oprec = (enum cp_parser_prec) (oprec - 1);
34486 cp_lexer_consume_token (parser->lexer);
34487 rhs = cp_parser_binary_expression (parser, false, false,
34488 oprec, NULL);
34489 if (rhs == error_mark_node)
34490 goto saw_error;
34491 goto stmt_done;
34492 /* FALLTHROUGH */
34493 default:
34494 cp_parser_error (parser,
34495 "invalid operator for %<#pragma omp atomic%>");
34496 goto saw_error;
34498 cp_lexer_consume_token (parser->lexer);
34500 rhs = cp_parser_expression (parser);
34501 if (rhs == error_mark_node)
34502 goto saw_error;
34503 break;
34505 stmt_done:
34506 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34508 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34509 goto saw_error;
34510 v = cp_parser_unary_expression (parser);
34511 if (v == error_mark_node)
34512 goto saw_error;
34513 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34514 goto saw_error;
34515 lhs1 = cp_parser_unary_expression (parser);
34516 if (lhs1 == error_mark_node)
34517 goto saw_error;
34519 if (structured_block)
34521 cp_parser_consume_semicolon_at_end_of_statement (parser);
34522 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34524 done:
34525 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34526 if (!structured_block)
34527 cp_parser_consume_semicolon_at_end_of_statement (parser);
34528 return;
34530 saw_error:
34531 cp_parser_skip_to_end_of_block_or_statement (parser);
34532 if (structured_block)
34534 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34535 cp_lexer_consume_token (parser->lexer);
34536 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34538 cp_parser_skip_to_end_of_block_or_statement (parser);
34539 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34540 cp_lexer_consume_token (parser->lexer);
34546 /* OpenMP 2.5:
34547 # pragma omp barrier new-line */
34549 static void
34550 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34552 cp_parser_require_pragma_eol (parser, pragma_tok);
34553 finish_omp_barrier ();
34556 /* OpenMP 2.5:
34557 # pragma omp critical [(name)] new-line
34558 structured-block
34560 OpenMP 4.5:
34561 # pragma omp critical [(name) [hint(expression)]] new-line
34562 structured-block */
34564 #define OMP_CRITICAL_CLAUSE_MASK \
34565 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34567 static tree
34568 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34570 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34572 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34574 matching_parens parens;
34575 parens.consume_open (parser);
34577 name = cp_parser_identifier (parser);
34579 if (name == error_mark_node
34580 || !parens.require_close (parser))
34581 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34582 /*or_comma=*/false,
34583 /*consume_paren=*/true);
34584 if (name == error_mark_node)
34585 name = NULL;
34587 clauses = cp_parser_omp_all_clauses (parser,
34588 OMP_CRITICAL_CLAUSE_MASK,
34589 "#pragma omp critical", pragma_tok);
34591 else
34592 cp_parser_require_pragma_eol (parser, pragma_tok);
34594 stmt = cp_parser_omp_structured_block (parser, if_p);
34595 return c_finish_omp_critical (input_location, stmt, name, clauses);
34598 /* OpenMP 2.5:
34599 # pragma omp flush flush-vars[opt] new-line
34601 flush-vars:
34602 ( variable-list ) */
34604 static void
34605 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34607 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34608 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34609 cp_parser_require_pragma_eol (parser, pragma_tok);
34611 finish_omp_flush ();
34614 /* Helper function, to parse omp for increment expression. */
34616 static tree
34617 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34619 tree cond = cp_parser_binary_expression (parser, false, true,
34620 PREC_NOT_OPERATOR, NULL);
34621 if (cond == error_mark_node
34622 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34624 cp_parser_skip_to_end_of_statement (parser);
34625 return error_mark_node;
34628 switch (TREE_CODE (cond))
34630 case GT_EXPR:
34631 case GE_EXPR:
34632 case LT_EXPR:
34633 case LE_EXPR:
34634 break;
34635 case NE_EXPR:
34636 if (code == CILK_SIMD || code == CILK_FOR)
34637 break;
34638 /* Fall through: OpenMP disallows NE_EXPR. */
34639 gcc_fallthrough ();
34640 default:
34641 return error_mark_node;
34644 /* If decl is an iterator, preserve LHS and RHS of the relational
34645 expr until finish_omp_for. */
34646 if (decl
34647 && (type_dependent_expression_p (decl)
34648 || CLASS_TYPE_P (TREE_TYPE (decl))))
34649 return cond;
34651 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34652 TREE_CODE (cond),
34653 TREE_OPERAND (cond, 0), ERROR_MARK,
34654 TREE_OPERAND (cond, 1), ERROR_MARK,
34655 /*overload=*/NULL, tf_warning_or_error);
34658 /* Helper function, to parse omp for increment expression. */
34660 static tree
34661 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34663 cp_token *token = cp_lexer_peek_token (parser->lexer);
34664 enum tree_code op;
34665 tree lhs, rhs;
34666 cp_id_kind idk;
34667 bool decl_first;
34669 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34671 op = (token->type == CPP_PLUS_PLUS
34672 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34673 cp_lexer_consume_token (parser->lexer);
34674 lhs = cp_parser_simple_cast_expression (parser);
34675 if (lhs != decl
34676 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34677 return error_mark_node;
34678 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34681 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34682 if (lhs != decl
34683 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34684 return error_mark_node;
34686 token = cp_lexer_peek_token (parser->lexer);
34687 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34689 op = (token->type == CPP_PLUS_PLUS
34690 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34691 cp_lexer_consume_token (parser->lexer);
34692 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34695 op = cp_parser_assignment_operator_opt (parser);
34696 if (op == ERROR_MARK)
34697 return error_mark_node;
34699 if (op != NOP_EXPR)
34701 rhs = cp_parser_assignment_expression (parser);
34702 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34703 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34706 lhs = cp_parser_binary_expression (parser, false, false,
34707 PREC_ADDITIVE_EXPRESSION, NULL);
34708 token = cp_lexer_peek_token (parser->lexer);
34709 decl_first = (lhs == decl
34710 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34711 if (decl_first)
34712 lhs = NULL_TREE;
34713 if (token->type != CPP_PLUS
34714 && token->type != CPP_MINUS)
34715 return error_mark_node;
34719 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34720 cp_lexer_consume_token (parser->lexer);
34721 rhs = cp_parser_binary_expression (parser, false, false,
34722 PREC_ADDITIVE_EXPRESSION, NULL);
34723 token = cp_lexer_peek_token (parser->lexer);
34724 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34726 if (lhs == NULL_TREE)
34728 if (op == PLUS_EXPR)
34729 lhs = rhs;
34730 else
34731 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34732 tf_warning_or_error);
34734 else
34735 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34736 ERROR_MARK, NULL, tf_warning_or_error);
34739 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34741 if (!decl_first)
34743 if ((rhs != decl
34744 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34745 || op == MINUS_EXPR)
34746 return error_mark_node;
34747 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34749 else
34750 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34752 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34755 /* Parse the initialization statement of either an OpenMP for loop or
34756 a Cilk Plus for loop.
34758 Return true if the resulting construct should have an
34759 OMP_CLAUSE_PRIVATE added to it. */
34761 static tree
34762 cp_parser_omp_for_loop_init (cp_parser *parser,
34763 enum tree_code code,
34764 tree &this_pre_body,
34765 vec<tree, va_gc> *for_block,
34766 tree &init,
34767 tree &orig_init,
34768 tree &decl,
34769 tree &real_decl)
34771 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34772 return NULL_TREE;
34774 tree add_private_clause = NULL_TREE;
34776 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34778 init-expr:
34779 var = lb
34780 integer-type var = lb
34781 random-access-iterator-type var = lb
34782 pointer-type var = lb
34784 cp_decl_specifier_seq type_specifiers;
34786 /* First, try to parse as an initialized declaration. See
34787 cp_parser_condition, from whence the bulk of this is copied. */
34789 cp_parser_parse_tentatively (parser);
34790 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34791 /*is_trailing_return=*/false,
34792 &type_specifiers);
34793 if (cp_parser_parse_definitely (parser))
34795 /* If parsing a type specifier seq succeeded, then this
34796 MUST be a initialized declaration. */
34797 tree asm_specification, attributes;
34798 cp_declarator *declarator;
34800 declarator = cp_parser_declarator (parser,
34801 CP_PARSER_DECLARATOR_NAMED,
34802 /*ctor_dtor_or_conv_p=*/NULL,
34803 /*parenthesized_p=*/NULL,
34804 /*member_p=*/false,
34805 /*friend_p=*/false);
34806 attributes = cp_parser_attributes_opt (parser);
34807 asm_specification = cp_parser_asm_specification_opt (parser);
34809 if (declarator == cp_error_declarator)
34810 cp_parser_skip_to_end_of_statement (parser);
34812 else
34814 tree pushed_scope, auto_node;
34816 decl = start_decl (declarator, &type_specifiers,
34817 SD_INITIALIZED, attributes,
34818 /*prefix_attributes=*/NULL_TREE,
34819 &pushed_scope);
34821 auto_node = type_uses_auto (TREE_TYPE (decl));
34822 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34824 if (cp_lexer_next_token_is (parser->lexer,
34825 CPP_OPEN_PAREN))
34827 if (code != CILK_SIMD && code != CILK_FOR)
34828 error ("parenthesized initialization is not allowed in "
34829 "OpenMP %<for%> loop");
34830 else
34831 error ("parenthesized initialization is "
34832 "not allowed in for-loop");
34834 else
34835 /* Trigger an error. */
34836 cp_parser_require (parser, CPP_EQ, RT_EQ);
34838 init = error_mark_node;
34839 cp_parser_skip_to_end_of_statement (parser);
34841 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34842 || type_dependent_expression_p (decl)
34843 || auto_node)
34845 bool is_direct_init, is_non_constant_init;
34847 init = cp_parser_initializer (parser,
34848 &is_direct_init,
34849 &is_non_constant_init);
34851 if (auto_node)
34853 TREE_TYPE (decl)
34854 = do_auto_deduction (TREE_TYPE (decl), init,
34855 auto_node);
34857 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34858 && !type_dependent_expression_p (decl))
34859 goto non_class;
34862 cp_finish_decl (decl, init, !is_non_constant_init,
34863 asm_specification,
34864 LOOKUP_ONLYCONVERTING);
34865 orig_init = init;
34866 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34868 vec_safe_push (for_block, this_pre_body);
34869 init = NULL_TREE;
34871 else
34873 init = pop_stmt_list (this_pre_body);
34874 if (init && TREE_CODE (init) == STATEMENT_LIST)
34876 tree_stmt_iterator i = tsi_start (init);
34877 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34878 while (!tsi_end_p (i))
34880 tree t = tsi_stmt (i);
34881 if (TREE_CODE (t) == DECL_EXPR
34882 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34884 tsi_delink (&i);
34885 vec_safe_push (for_block, t);
34886 continue;
34888 break;
34890 if (tsi_one_before_end_p (i))
34892 tree t = tsi_stmt (i);
34893 tsi_delink (&i);
34894 free_stmt_list (init);
34895 init = t;
34899 this_pre_body = NULL_TREE;
34901 else
34903 /* Consume '='. */
34904 cp_lexer_consume_token (parser->lexer);
34905 init = cp_parser_assignment_expression (parser);
34907 non_class:
34908 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34909 init = error_mark_node;
34910 else
34911 cp_finish_decl (decl, NULL_TREE,
34912 /*init_const_expr_p=*/false,
34913 asm_specification,
34914 LOOKUP_ONLYCONVERTING);
34917 if (pushed_scope)
34918 pop_scope (pushed_scope);
34921 else
34923 cp_id_kind idk;
34924 /* If parsing a type specifier sequence failed, then
34925 this MUST be a simple expression. */
34926 if (code == CILK_FOR)
34927 error ("%<_Cilk_for%> allows expression instead of declaration only "
34928 "in C, not in C++");
34929 cp_parser_parse_tentatively (parser);
34930 decl = cp_parser_primary_expression (parser, false, false,
34931 false, &idk);
34932 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34933 if (!cp_parser_error_occurred (parser)
34934 && decl
34935 && (TREE_CODE (decl) == COMPONENT_REF
34936 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34938 cp_parser_abort_tentative_parse (parser);
34939 cp_parser_parse_tentatively (parser);
34940 cp_token *token = cp_lexer_peek_token (parser->lexer);
34941 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34942 /*check_dependency_p=*/true,
34943 /*template_p=*/NULL,
34944 /*declarator_p=*/false,
34945 /*optional_p=*/false);
34946 if (name != error_mark_node
34947 && last_tok == cp_lexer_peek_token (parser->lexer))
34949 decl = cp_parser_lookup_name_simple (parser, name,
34950 token->location);
34951 if (TREE_CODE (decl) == FIELD_DECL)
34952 add_private_clause = omp_privatize_field (decl, false);
34954 cp_parser_abort_tentative_parse (parser);
34955 cp_parser_parse_tentatively (parser);
34956 decl = cp_parser_primary_expression (parser, false, false,
34957 false, &idk);
34959 if (!cp_parser_error_occurred (parser)
34960 && decl
34961 && DECL_P (decl)
34962 && CLASS_TYPE_P (TREE_TYPE (decl)))
34964 tree rhs;
34966 cp_parser_parse_definitely (parser);
34967 cp_parser_require (parser, CPP_EQ, RT_EQ);
34968 rhs = cp_parser_assignment_expression (parser);
34969 orig_init = rhs;
34970 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34971 decl, NOP_EXPR,
34972 rhs,
34973 tf_warning_or_error));
34974 if (!add_private_clause)
34975 add_private_clause = decl;
34977 else
34979 decl = NULL;
34980 cp_parser_abort_tentative_parse (parser);
34981 init = cp_parser_expression (parser);
34982 if (init)
34984 if (TREE_CODE (init) == MODIFY_EXPR
34985 || TREE_CODE (init) == MODOP_EXPR)
34986 real_decl = TREE_OPERAND (init, 0);
34990 return add_private_clause;
34993 /* Parse the restricted form of the for statement allowed by OpenMP. */
34995 static tree
34996 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34997 tree *cclauses, bool *if_p)
34999 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35000 tree real_decl, initv, condv, incrv, declv;
35001 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35002 location_t loc_first;
35003 bool collapse_err = false;
35004 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35005 vec<tree, va_gc> *for_block = make_tree_vector ();
35006 auto_vec<tree, 4> orig_inits;
35007 bool tiling = false;
35009 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35010 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35011 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35012 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35014 tiling = true;
35015 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35017 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35018 && OMP_CLAUSE_ORDERED_EXPR (cl))
35020 ordered_cl = cl;
35021 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35024 if (ordered && ordered < collapse)
35026 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35027 "%<ordered%> clause parameter is less than %<collapse%>");
35028 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35029 = build_int_cst (NULL_TREE, collapse);
35030 ordered = collapse;
35032 if (ordered)
35034 for (tree *pc = &clauses; *pc; )
35035 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35037 error_at (OMP_CLAUSE_LOCATION (*pc),
35038 "%<linear%> clause may not be specified together "
35039 "with %<ordered%> clause with a parameter");
35040 *pc = OMP_CLAUSE_CHAIN (*pc);
35042 else
35043 pc = &OMP_CLAUSE_CHAIN (*pc);
35046 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35047 count = ordered ? ordered : collapse;
35049 declv = make_tree_vec (count);
35050 initv = make_tree_vec (count);
35051 condv = make_tree_vec (count);
35052 incrv = make_tree_vec (count);
35054 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35056 for (i = 0; i < count; i++)
35058 int bracecount = 0;
35059 tree add_private_clause = NULL_TREE;
35060 location_t loc;
35062 if (code != CILK_FOR
35063 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35065 if (!collapse_err)
35066 cp_parser_error (parser, "for statement expected");
35067 return NULL;
35069 if (code == CILK_FOR
35070 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
35072 if (!collapse_err)
35073 cp_parser_error (parser, "_Cilk_for statement expected");
35074 return NULL;
35076 loc = cp_lexer_consume_token (parser->lexer)->location;
35078 matching_parens parens;
35079 if (!parens.require_open (parser))
35080 return NULL;
35082 init = orig_init = decl = real_decl = NULL;
35083 this_pre_body = push_stmt_list ();
35085 add_private_clause
35086 = cp_parser_omp_for_loop_init (parser, code,
35087 this_pre_body, for_block,
35088 init, orig_init, decl, real_decl);
35090 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35091 if (this_pre_body)
35093 this_pre_body = pop_stmt_list (this_pre_body);
35094 if (pre_body)
35096 tree t = pre_body;
35097 pre_body = push_stmt_list ();
35098 add_stmt (t);
35099 add_stmt (this_pre_body);
35100 pre_body = pop_stmt_list (pre_body);
35102 else
35103 pre_body = this_pre_body;
35106 if (decl)
35107 real_decl = decl;
35108 if (cclauses != NULL
35109 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35110 && real_decl != NULL_TREE)
35112 tree *c;
35113 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35114 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35115 && OMP_CLAUSE_DECL (*c) == real_decl)
35117 error_at (loc, "iteration variable %qD"
35118 " should not be firstprivate", real_decl);
35119 *c = OMP_CLAUSE_CHAIN (*c);
35121 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35122 && OMP_CLAUSE_DECL (*c) == real_decl)
35124 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35125 tree l = *c;
35126 *c = OMP_CLAUSE_CHAIN (*c);
35127 if (code == OMP_SIMD)
35129 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35130 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35132 else
35134 OMP_CLAUSE_CHAIN (l) = clauses;
35135 clauses = l;
35137 add_private_clause = NULL_TREE;
35139 else
35141 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35142 && OMP_CLAUSE_DECL (*c) == real_decl)
35143 add_private_clause = NULL_TREE;
35144 c = &OMP_CLAUSE_CHAIN (*c);
35148 if (add_private_clause)
35150 tree c;
35151 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35153 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35154 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35155 && OMP_CLAUSE_DECL (c) == decl)
35156 break;
35157 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35158 && OMP_CLAUSE_DECL (c) == decl)
35159 error_at (loc, "iteration variable %qD "
35160 "should not be firstprivate",
35161 decl);
35162 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35163 && OMP_CLAUSE_DECL (c) == decl)
35164 error_at (loc, "iteration variable %qD should not be reduction",
35165 decl);
35167 if (c == NULL)
35169 if (code != OMP_SIMD)
35170 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35171 else if (collapse == 1)
35172 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35173 else
35174 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35175 OMP_CLAUSE_DECL (c) = add_private_clause;
35176 c = finish_omp_clauses (c, C_ORT_OMP);
35177 if (c)
35179 OMP_CLAUSE_CHAIN (c) = clauses;
35180 clauses = c;
35181 /* For linear, signal that we need to fill up
35182 the so far unknown linear step. */
35183 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35184 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35189 cond = NULL;
35190 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35191 cond = cp_parser_omp_for_cond (parser, decl, code);
35192 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35194 incr = NULL;
35195 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35197 /* If decl is an iterator, preserve the operator on decl
35198 until finish_omp_for. */
35199 if (real_decl
35200 && ((processing_template_decl
35201 && (TREE_TYPE (real_decl) == NULL_TREE
35202 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35203 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35204 incr = cp_parser_omp_for_incr (parser, real_decl);
35205 else
35206 incr = cp_parser_expression (parser);
35207 if (!EXPR_HAS_LOCATION (incr))
35208 protected_set_expr_location (incr, input_location);
35211 if (!parens.require_close (parser))
35212 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35213 /*or_comma=*/false,
35214 /*consume_paren=*/true);
35216 TREE_VEC_ELT (declv, i) = decl;
35217 TREE_VEC_ELT (initv, i) = init;
35218 TREE_VEC_ELT (condv, i) = cond;
35219 TREE_VEC_ELT (incrv, i) = incr;
35220 if (orig_init)
35222 orig_inits.safe_grow_cleared (i + 1);
35223 orig_inits[i] = orig_init;
35226 if (i == count - 1)
35227 break;
35229 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35230 in between the collapsed for loops to be still considered perfectly
35231 nested. Hopefully the final version clarifies this.
35232 For now handle (multiple) {'s and empty statements. */
35233 cp_parser_parse_tentatively (parser);
35234 for (;;)
35236 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35237 break;
35238 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35240 cp_lexer_consume_token (parser->lexer);
35241 bracecount++;
35243 else if (bracecount
35244 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35245 cp_lexer_consume_token (parser->lexer);
35246 else
35248 loc = cp_lexer_peek_token (parser->lexer)->location;
35249 error_at (loc, "not enough for loops to collapse");
35250 collapse_err = true;
35251 cp_parser_abort_tentative_parse (parser);
35252 declv = NULL_TREE;
35253 break;
35257 if (declv)
35259 cp_parser_parse_definitely (parser);
35260 nbraces += bracecount;
35264 if (nbraces)
35265 if_p = NULL;
35267 /* Note that we saved the original contents of this flag when we entered
35268 the structured block, and so we don't need to re-save it here. */
35269 if (code == CILK_SIMD || code == CILK_FOR)
35270 parser->in_statement = IN_CILK_SIMD_FOR;
35271 else
35272 parser->in_statement = IN_OMP_FOR;
35274 /* Note that the grammar doesn't call for a structured block here,
35275 though the loop as a whole is a structured block. */
35276 body = push_stmt_list ();
35277 cp_parser_statement (parser, NULL_TREE, false, if_p);
35278 body = pop_stmt_list (body);
35280 if (declv == NULL_TREE)
35281 ret = NULL_TREE;
35282 else
35283 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35284 body, pre_body, &orig_inits, clauses);
35286 while (nbraces)
35288 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35290 cp_lexer_consume_token (parser->lexer);
35291 nbraces--;
35293 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35294 cp_lexer_consume_token (parser->lexer);
35295 else
35297 if (!collapse_err)
35299 error_at (cp_lexer_peek_token (parser->lexer)->location,
35300 "collapsed loops not perfectly nested");
35302 collapse_err = true;
35303 cp_parser_statement_seq_opt (parser, NULL);
35304 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35305 break;
35309 while (!for_block->is_empty ())
35311 tree t = for_block->pop ();
35312 if (TREE_CODE (t) == STATEMENT_LIST)
35313 add_stmt (pop_stmt_list (t));
35314 else
35315 add_stmt (t);
35317 release_tree_vector (for_block);
35319 return ret;
35322 /* Helper function for OpenMP parsing, split clauses and call
35323 finish_omp_clauses on each of the set of clauses afterwards. */
35325 static void
35326 cp_omp_split_clauses (location_t loc, enum tree_code code,
35327 omp_clause_mask mask, tree clauses, tree *cclauses)
35329 int i;
35330 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35331 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35332 if (cclauses[i])
35333 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35336 /* OpenMP 4.0:
35337 #pragma omp simd simd-clause[optseq] new-line
35338 for-loop */
35340 #define OMP_SIMD_CLAUSE_MASK \
35341 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35350 static tree
35351 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35352 char *p_name, omp_clause_mask mask, tree *cclauses,
35353 bool *if_p)
35355 tree clauses, sb, ret;
35356 unsigned int save;
35357 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35359 strcat (p_name, " simd");
35360 mask |= OMP_SIMD_CLAUSE_MASK;
35362 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35363 cclauses == NULL);
35364 if (cclauses)
35366 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35367 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35368 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35369 OMP_CLAUSE_ORDERED);
35370 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35372 error_at (OMP_CLAUSE_LOCATION (c),
35373 "%<ordered%> clause with parameter may not be specified "
35374 "on %qs construct", p_name);
35375 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35379 sb = begin_omp_structured_block ();
35380 save = cp_parser_begin_omp_structured_block (parser);
35382 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35384 cp_parser_end_omp_structured_block (parser, save);
35385 add_stmt (finish_omp_structured_block (sb));
35387 return ret;
35390 /* OpenMP 2.5:
35391 #pragma omp for for-clause[optseq] new-line
35392 for-loop
35394 OpenMP 4.0:
35395 #pragma omp for simd for-simd-clause[optseq] new-line
35396 for-loop */
35398 #define OMP_FOR_CLAUSE_MASK \
35399 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35409 static tree
35410 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35411 char *p_name, omp_clause_mask mask, tree *cclauses,
35412 bool *if_p)
35414 tree clauses, sb, ret;
35415 unsigned int save;
35416 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35418 strcat (p_name, " for");
35419 mask |= OMP_FOR_CLAUSE_MASK;
35420 /* parallel for{, simd} disallows nowait clause, but for
35421 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35422 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35423 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35424 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35425 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35426 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35428 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35430 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35431 const char *p = IDENTIFIER_POINTER (id);
35433 if (strcmp (p, "simd") == 0)
35435 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35436 if (cclauses == NULL)
35437 cclauses = cclauses_buf;
35439 cp_lexer_consume_token (parser->lexer);
35440 if (!flag_openmp) /* flag_openmp_simd */
35441 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35442 cclauses, if_p);
35443 sb = begin_omp_structured_block ();
35444 save = cp_parser_begin_omp_structured_block (parser);
35445 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35446 cclauses, if_p);
35447 cp_parser_end_omp_structured_block (parser, save);
35448 tree body = finish_omp_structured_block (sb);
35449 if (ret == NULL)
35450 return ret;
35451 ret = make_node (OMP_FOR);
35452 TREE_TYPE (ret) = void_type_node;
35453 OMP_FOR_BODY (ret) = body;
35454 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35455 SET_EXPR_LOCATION (ret, loc);
35456 add_stmt (ret);
35457 return ret;
35460 if (!flag_openmp) /* flag_openmp_simd */
35462 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35463 return NULL_TREE;
35466 /* Composite distribute parallel for disallows linear clause. */
35467 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35468 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35470 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35471 cclauses == NULL);
35472 if (cclauses)
35474 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35475 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35478 sb = begin_omp_structured_block ();
35479 save = cp_parser_begin_omp_structured_block (parser);
35481 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35483 cp_parser_end_omp_structured_block (parser, save);
35484 add_stmt (finish_omp_structured_block (sb));
35486 return ret;
35489 /* OpenMP 2.5:
35490 # pragma omp master new-line
35491 structured-block */
35493 static tree
35494 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35496 cp_parser_require_pragma_eol (parser, pragma_tok);
35497 return c_finish_omp_master (input_location,
35498 cp_parser_omp_structured_block (parser, if_p));
35501 /* OpenMP 2.5:
35502 # pragma omp ordered new-line
35503 structured-block
35505 OpenMP 4.5:
35506 # pragma omp ordered ordered-clauses new-line
35507 structured-block */
35509 #define OMP_ORDERED_CLAUSE_MASK \
35510 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35513 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35514 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35516 static bool
35517 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35518 enum pragma_context context, bool *if_p)
35520 location_t loc = pragma_tok->location;
35522 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35524 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35525 const char *p = IDENTIFIER_POINTER (id);
35527 if (strcmp (p, "depend") == 0)
35529 if (!flag_openmp) /* flag_openmp_simd */
35531 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35532 return false;
35534 if (context == pragma_stmt)
35536 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35537 "%<depend%> clause may only be used in compound "
35538 "statements");
35539 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35540 return false;
35542 tree clauses
35543 = cp_parser_omp_all_clauses (parser,
35544 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35545 "#pragma omp ordered", pragma_tok);
35546 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35547 return false;
35551 tree clauses
35552 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35553 "#pragma omp ordered", pragma_tok);
35555 if (!flag_openmp /* flag_openmp_simd */
35556 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35557 return false;
35559 c_finish_omp_ordered (loc, clauses,
35560 cp_parser_omp_structured_block (parser, if_p));
35561 return true;
35564 /* OpenMP 2.5:
35566 section-scope:
35567 { section-sequence }
35569 section-sequence:
35570 section-directive[opt] structured-block
35571 section-sequence section-directive structured-block */
35573 static tree
35574 cp_parser_omp_sections_scope (cp_parser *parser)
35576 tree stmt, substmt;
35577 bool error_suppress = false;
35578 cp_token *tok;
35580 matching_braces braces;
35581 if (!braces.require_open (parser))
35582 return NULL_TREE;
35584 stmt = push_stmt_list ();
35586 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35587 != PRAGMA_OMP_SECTION)
35589 substmt = cp_parser_omp_structured_block (parser, NULL);
35590 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35591 add_stmt (substmt);
35594 while (1)
35596 tok = cp_lexer_peek_token (parser->lexer);
35597 if (tok->type == CPP_CLOSE_BRACE)
35598 break;
35599 if (tok->type == CPP_EOF)
35600 break;
35602 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35604 cp_lexer_consume_token (parser->lexer);
35605 cp_parser_require_pragma_eol (parser, tok);
35606 error_suppress = false;
35608 else if (!error_suppress)
35610 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35611 error_suppress = true;
35614 substmt = cp_parser_omp_structured_block (parser, NULL);
35615 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35616 add_stmt (substmt);
35618 braces.require_close (parser);
35620 substmt = pop_stmt_list (stmt);
35622 stmt = make_node (OMP_SECTIONS);
35623 TREE_TYPE (stmt) = void_type_node;
35624 OMP_SECTIONS_BODY (stmt) = substmt;
35626 add_stmt (stmt);
35627 return stmt;
35630 /* OpenMP 2.5:
35631 # pragma omp sections sections-clause[optseq] newline
35632 sections-scope */
35634 #define OMP_SECTIONS_CLAUSE_MASK \
35635 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35641 static tree
35642 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35643 char *p_name, omp_clause_mask mask, tree *cclauses)
35645 tree clauses, ret;
35646 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35648 strcat (p_name, " sections");
35649 mask |= OMP_SECTIONS_CLAUSE_MASK;
35650 if (cclauses)
35651 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35653 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35654 cclauses == NULL);
35655 if (cclauses)
35657 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35658 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35661 ret = cp_parser_omp_sections_scope (parser);
35662 if (ret)
35663 OMP_SECTIONS_CLAUSES (ret) = clauses;
35665 return ret;
35668 /* OpenMP 2.5:
35669 # pragma omp parallel parallel-clause[optseq] new-line
35670 structured-block
35671 # pragma omp parallel for parallel-for-clause[optseq] new-line
35672 structured-block
35673 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35674 structured-block
35676 OpenMP 4.0:
35677 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35678 structured-block */
35680 #define OMP_PARALLEL_CLAUSE_MASK \
35681 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35691 static tree
35692 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35693 char *p_name, omp_clause_mask mask, tree *cclauses,
35694 bool *if_p)
35696 tree stmt, clauses, block;
35697 unsigned int save;
35698 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35700 strcat (p_name, " parallel");
35701 mask |= OMP_PARALLEL_CLAUSE_MASK;
35702 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35703 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35704 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35705 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35707 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35709 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35710 if (cclauses == NULL)
35711 cclauses = cclauses_buf;
35713 cp_lexer_consume_token (parser->lexer);
35714 if (!flag_openmp) /* flag_openmp_simd */
35715 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35716 if_p);
35717 block = begin_omp_parallel ();
35718 save = cp_parser_begin_omp_structured_block (parser);
35719 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35720 if_p);
35721 cp_parser_end_omp_structured_block (parser, save);
35722 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35723 block);
35724 if (ret == NULL_TREE)
35725 return ret;
35726 OMP_PARALLEL_COMBINED (stmt) = 1;
35727 return stmt;
35729 /* When combined with distribute, parallel has to be followed by for.
35730 #pragma omp target parallel is allowed though. */
35731 else if (cclauses
35732 && (mask & (OMP_CLAUSE_MASK_1
35733 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35735 error_at (loc, "expected %<for%> after %qs", p_name);
35736 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35737 return NULL_TREE;
35739 else if (!flag_openmp) /* flag_openmp_simd */
35741 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35742 return NULL_TREE;
35744 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35746 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35747 const char *p = IDENTIFIER_POINTER (id);
35748 if (strcmp (p, "sections") == 0)
35750 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35751 cclauses = cclauses_buf;
35753 cp_lexer_consume_token (parser->lexer);
35754 block = begin_omp_parallel ();
35755 save = cp_parser_begin_omp_structured_block (parser);
35756 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35757 cp_parser_end_omp_structured_block (parser, save);
35758 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35759 block);
35760 OMP_PARALLEL_COMBINED (stmt) = 1;
35761 return stmt;
35765 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35766 cclauses == NULL);
35767 if (cclauses)
35769 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35770 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35773 block = begin_omp_parallel ();
35774 save = cp_parser_begin_omp_structured_block (parser);
35775 cp_parser_statement (parser, NULL_TREE, false, if_p);
35776 cp_parser_end_omp_structured_block (parser, save);
35777 stmt = finish_omp_parallel (clauses, block);
35778 return stmt;
35781 /* OpenMP 2.5:
35782 # pragma omp single single-clause[optseq] new-line
35783 structured-block */
35785 #define OMP_SINGLE_CLAUSE_MASK \
35786 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35791 static tree
35792 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35794 tree stmt = make_node (OMP_SINGLE);
35795 TREE_TYPE (stmt) = void_type_node;
35797 OMP_SINGLE_CLAUSES (stmt)
35798 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35799 "#pragma omp single", pragma_tok);
35800 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35802 return add_stmt (stmt);
35805 /* OpenMP 3.0:
35806 # pragma omp task task-clause[optseq] new-line
35807 structured-block */
35809 #define OMP_TASK_CLAUSE_MASK \
35810 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35821 static tree
35822 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35824 tree clauses, block;
35825 unsigned int save;
35827 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35828 "#pragma omp task", pragma_tok);
35829 block = begin_omp_task ();
35830 save = cp_parser_begin_omp_structured_block (parser);
35831 cp_parser_statement (parser, NULL_TREE, false, if_p);
35832 cp_parser_end_omp_structured_block (parser, save);
35833 return finish_omp_task (clauses, block);
35836 /* OpenMP 3.0:
35837 # pragma omp taskwait new-line */
35839 static void
35840 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35842 cp_parser_require_pragma_eol (parser, pragma_tok);
35843 finish_omp_taskwait ();
35846 /* OpenMP 3.1:
35847 # pragma omp taskyield new-line */
35849 static void
35850 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35852 cp_parser_require_pragma_eol (parser, pragma_tok);
35853 finish_omp_taskyield ();
35856 /* OpenMP 4.0:
35857 # pragma omp taskgroup new-line
35858 structured-block */
35860 static tree
35861 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35863 cp_parser_require_pragma_eol (parser, pragma_tok);
35864 return c_finish_omp_taskgroup (input_location,
35865 cp_parser_omp_structured_block (parser,
35866 if_p));
35870 /* OpenMP 2.5:
35871 # pragma omp threadprivate (variable-list) */
35873 static void
35874 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35876 tree vars;
35878 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35879 cp_parser_require_pragma_eol (parser, pragma_tok);
35881 finish_omp_threadprivate (vars);
35884 /* OpenMP 4.0:
35885 # pragma omp cancel cancel-clause[optseq] new-line */
35887 #define OMP_CANCEL_CLAUSE_MASK \
35888 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35894 static void
35895 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35897 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35898 "#pragma omp cancel", pragma_tok);
35899 finish_omp_cancel (clauses);
35902 /* OpenMP 4.0:
35903 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35905 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35906 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35911 static void
35912 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35913 enum pragma_context context)
35915 tree clauses;
35916 bool point_seen = false;
35918 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35920 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35921 const char *p = IDENTIFIER_POINTER (id);
35923 if (strcmp (p, "point") == 0)
35925 cp_lexer_consume_token (parser->lexer);
35926 point_seen = true;
35929 if (!point_seen)
35931 cp_parser_error (parser, "expected %<point%>");
35932 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35933 return;
35936 if (context != pragma_compound)
35938 if (context == pragma_stmt)
35939 error_at (pragma_tok->location,
35940 "%<#pragma %s%> may only be used in compound statements",
35941 "omp cancellation point");
35942 else
35943 cp_parser_error (parser, "expected declaration specifiers");
35944 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35945 return;
35948 clauses = cp_parser_omp_all_clauses (parser,
35949 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35950 "#pragma omp cancellation point",
35951 pragma_tok);
35952 finish_omp_cancellation_point (clauses);
35955 /* OpenMP 4.0:
35956 #pragma omp distribute distribute-clause[optseq] new-line
35957 for-loop */
35959 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35960 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35966 static tree
35967 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35968 char *p_name, omp_clause_mask mask, tree *cclauses,
35969 bool *if_p)
35971 tree clauses, sb, ret;
35972 unsigned int save;
35973 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35975 strcat (p_name, " distribute");
35976 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35978 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35980 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35981 const char *p = IDENTIFIER_POINTER (id);
35982 bool simd = false;
35983 bool parallel = false;
35985 if (strcmp (p, "simd") == 0)
35986 simd = true;
35987 else
35988 parallel = strcmp (p, "parallel") == 0;
35989 if (parallel || simd)
35991 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35992 if (cclauses == NULL)
35993 cclauses = cclauses_buf;
35994 cp_lexer_consume_token (parser->lexer);
35995 if (!flag_openmp) /* flag_openmp_simd */
35997 if (simd)
35998 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35999 cclauses, if_p);
36000 else
36001 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36002 cclauses, if_p);
36004 sb = begin_omp_structured_block ();
36005 save = cp_parser_begin_omp_structured_block (parser);
36006 if (simd)
36007 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36008 cclauses, if_p);
36009 else
36010 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36011 cclauses, if_p);
36012 cp_parser_end_omp_structured_block (parser, save);
36013 tree body = finish_omp_structured_block (sb);
36014 if (ret == NULL)
36015 return ret;
36016 ret = make_node (OMP_DISTRIBUTE);
36017 TREE_TYPE (ret) = void_type_node;
36018 OMP_FOR_BODY (ret) = body;
36019 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36020 SET_EXPR_LOCATION (ret, loc);
36021 add_stmt (ret);
36022 return ret;
36025 if (!flag_openmp) /* flag_openmp_simd */
36027 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36028 return NULL_TREE;
36031 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36032 cclauses == NULL);
36033 if (cclauses)
36035 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36036 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36039 sb = begin_omp_structured_block ();
36040 save = cp_parser_begin_omp_structured_block (parser);
36042 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36044 cp_parser_end_omp_structured_block (parser, save);
36045 add_stmt (finish_omp_structured_block (sb));
36047 return ret;
36050 /* OpenMP 4.0:
36051 # pragma omp teams teams-clause[optseq] new-line
36052 structured-block */
36054 #define OMP_TEAMS_CLAUSE_MASK \
36055 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36063 static tree
36064 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36065 char *p_name, omp_clause_mask mask, tree *cclauses,
36066 bool *if_p)
36068 tree clauses, sb, ret;
36069 unsigned int save;
36070 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36072 strcat (p_name, " teams");
36073 mask |= OMP_TEAMS_CLAUSE_MASK;
36075 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36077 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36078 const char *p = IDENTIFIER_POINTER (id);
36079 if (strcmp (p, "distribute") == 0)
36081 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36082 if (cclauses == NULL)
36083 cclauses = cclauses_buf;
36085 cp_lexer_consume_token (parser->lexer);
36086 if (!flag_openmp) /* flag_openmp_simd */
36087 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36088 cclauses, if_p);
36089 sb = begin_omp_structured_block ();
36090 save = cp_parser_begin_omp_structured_block (parser);
36091 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36092 cclauses, if_p);
36093 cp_parser_end_omp_structured_block (parser, save);
36094 tree body = finish_omp_structured_block (sb);
36095 if (ret == NULL)
36096 return ret;
36097 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36098 ret = make_node (OMP_TEAMS);
36099 TREE_TYPE (ret) = void_type_node;
36100 OMP_TEAMS_CLAUSES (ret) = clauses;
36101 OMP_TEAMS_BODY (ret) = body;
36102 OMP_TEAMS_COMBINED (ret) = 1;
36103 SET_EXPR_LOCATION (ret, loc);
36104 return add_stmt (ret);
36107 if (!flag_openmp) /* flag_openmp_simd */
36109 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36110 return NULL_TREE;
36113 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36114 cclauses == NULL);
36115 if (cclauses)
36117 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36118 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36121 tree stmt = make_node (OMP_TEAMS);
36122 TREE_TYPE (stmt) = void_type_node;
36123 OMP_TEAMS_CLAUSES (stmt) = clauses;
36124 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36125 SET_EXPR_LOCATION (stmt, loc);
36127 return add_stmt (stmt);
36130 /* OpenMP 4.0:
36131 # pragma omp target data target-data-clause[optseq] new-line
36132 structured-block */
36134 #define OMP_TARGET_DATA_CLAUSE_MASK \
36135 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36140 static tree
36141 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36143 tree clauses
36144 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36145 "#pragma omp target data", pragma_tok);
36146 int map_seen = 0;
36147 for (tree *pc = &clauses; *pc;)
36149 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36150 switch (OMP_CLAUSE_MAP_KIND (*pc))
36152 case GOMP_MAP_TO:
36153 case GOMP_MAP_ALWAYS_TO:
36154 case GOMP_MAP_FROM:
36155 case GOMP_MAP_ALWAYS_FROM:
36156 case GOMP_MAP_TOFROM:
36157 case GOMP_MAP_ALWAYS_TOFROM:
36158 case GOMP_MAP_ALLOC:
36159 map_seen = 3;
36160 break;
36161 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36162 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36163 case GOMP_MAP_ALWAYS_POINTER:
36164 break;
36165 default:
36166 map_seen |= 1;
36167 error_at (OMP_CLAUSE_LOCATION (*pc),
36168 "%<#pragma omp target data%> with map-type other "
36169 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36170 "on %<map%> clause");
36171 *pc = OMP_CLAUSE_CHAIN (*pc);
36172 continue;
36174 pc = &OMP_CLAUSE_CHAIN (*pc);
36177 if (map_seen != 3)
36179 if (map_seen == 0)
36180 error_at (pragma_tok->location,
36181 "%<#pragma omp target data%> must contain at least "
36182 "one %<map%> clause");
36183 return NULL_TREE;
36186 tree stmt = make_node (OMP_TARGET_DATA);
36187 TREE_TYPE (stmt) = void_type_node;
36188 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36190 keep_next_level (true);
36191 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36193 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36194 return add_stmt (stmt);
36197 /* OpenMP 4.5:
36198 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36199 structured-block */
36201 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36202 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36208 static tree
36209 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36210 enum pragma_context context)
36212 bool data_seen = false;
36213 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36215 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36216 const char *p = IDENTIFIER_POINTER (id);
36218 if (strcmp (p, "data") == 0)
36220 cp_lexer_consume_token (parser->lexer);
36221 data_seen = true;
36224 if (!data_seen)
36226 cp_parser_error (parser, "expected %<data%>");
36227 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36228 return NULL_TREE;
36231 if (context == pragma_stmt)
36233 error_at (pragma_tok->location,
36234 "%<#pragma %s%> may only be used in compound statements",
36235 "omp target enter data");
36236 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36237 return NULL_TREE;
36240 tree clauses
36241 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36242 "#pragma omp target enter data", pragma_tok);
36243 int map_seen = 0;
36244 for (tree *pc = &clauses; *pc;)
36246 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36247 switch (OMP_CLAUSE_MAP_KIND (*pc))
36249 case GOMP_MAP_TO:
36250 case GOMP_MAP_ALWAYS_TO:
36251 case GOMP_MAP_ALLOC:
36252 map_seen = 3;
36253 break;
36254 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36255 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36256 case GOMP_MAP_ALWAYS_POINTER:
36257 break;
36258 default:
36259 map_seen |= 1;
36260 error_at (OMP_CLAUSE_LOCATION (*pc),
36261 "%<#pragma omp target enter data%> with map-type other "
36262 "than %<to%> or %<alloc%> on %<map%> clause");
36263 *pc = OMP_CLAUSE_CHAIN (*pc);
36264 continue;
36266 pc = &OMP_CLAUSE_CHAIN (*pc);
36269 if (map_seen != 3)
36271 if (map_seen == 0)
36272 error_at (pragma_tok->location,
36273 "%<#pragma omp target enter data%> must contain at least "
36274 "one %<map%> clause");
36275 return NULL_TREE;
36278 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36279 TREE_TYPE (stmt) = void_type_node;
36280 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36281 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36282 return add_stmt (stmt);
36285 /* OpenMP 4.5:
36286 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36287 structured-block */
36289 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36290 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36296 static tree
36297 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36298 enum pragma_context context)
36300 bool data_seen = false;
36301 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36303 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36304 const char *p = IDENTIFIER_POINTER (id);
36306 if (strcmp (p, "data") == 0)
36308 cp_lexer_consume_token (parser->lexer);
36309 data_seen = true;
36312 if (!data_seen)
36314 cp_parser_error (parser, "expected %<data%>");
36315 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36316 return NULL_TREE;
36319 if (context == pragma_stmt)
36321 error_at (pragma_tok->location,
36322 "%<#pragma %s%> may only be used in compound statements",
36323 "omp target exit data");
36324 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36325 return NULL_TREE;
36328 tree clauses
36329 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36330 "#pragma omp target exit data", pragma_tok);
36331 int map_seen = 0;
36332 for (tree *pc = &clauses; *pc;)
36334 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36335 switch (OMP_CLAUSE_MAP_KIND (*pc))
36337 case GOMP_MAP_FROM:
36338 case GOMP_MAP_ALWAYS_FROM:
36339 case GOMP_MAP_RELEASE:
36340 case GOMP_MAP_DELETE:
36341 map_seen = 3;
36342 break;
36343 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36344 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36345 case GOMP_MAP_ALWAYS_POINTER:
36346 break;
36347 default:
36348 map_seen |= 1;
36349 error_at (OMP_CLAUSE_LOCATION (*pc),
36350 "%<#pragma omp target exit data%> with map-type other "
36351 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36352 " clause");
36353 *pc = OMP_CLAUSE_CHAIN (*pc);
36354 continue;
36356 pc = &OMP_CLAUSE_CHAIN (*pc);
36359 if (map_seen != 3)
36361 if (map_seen == 0)
36362 error_at (pragma_tok->location,
36363 "%<#pragma omp target exit data%> must contain at least "
36364 "one %<map%> clause");
36365 return NULL_TREE;
36368 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36369 TREE_TYPE (stmt) = void_type_node;
36370 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36371 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36372 return add_stmt (stmt);
36375 /* OpenMP 4.0:
36376 # pragma omp target update target-update-clause[optseq] new-line */
36378 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36379 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36386 static bool
36387 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36388 enum pragma_context context)
36390 if (context == pragma_stmt)
36392 error_at (pragma_tok->location,
36393 "%<#pragma %s%> may only be used in compound statements",
36394 "omp target update");
36395 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36396 return false;
36399 tree clauses
36400 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36401 "#pragma omp target update", pragma_tok);
36402 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36403 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36405 error_at (pragma_tok->location,
36406 "%<#pragma omp target update%> must contain at least one "
36407 "%<from%> or %<to%> clauses");
36408 return false;
36411 tree stmt = make_node (OMP_TARGET_UPDATE);
36412 TREE_TYPE (stmt) = void_type_node;
36413 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36414 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36415 add_stmt (stmt);
36416 return false;
36419 /* OpenMP 4.0:
36420 # pragma omp target target-clause[optseq] new-line
36421 structured-block */
36423 #define OMP_TARGET_CLAUSE_MASK \
36424 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36434 static bool
36435 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36436 enum pragma_context context, bool *if_p)
36438 tree *pc = NULL, stmt;
36440 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36442 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36443 const char *p = IDENTIFIER_POINTER (id);
36444 enum tree_code ccode = ERROR_MARK;
36446 if (strcmp (p, "teams") == 0)
36447 ccode = OMP_TEAMS;
36448 else if (strcmp (p, "parallel") == 0)
36449 ccode = OMP_PARALLEL;
36450 else if (strcmp (p, "simd") == 0)
36451 ccode = OMP_SIMD;
36452 if (ccode != ERROR_MARK)
36454 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36455 char p_name[sizeof ("#pragma omp target teams distribute "
36456 "parallel for simd")];
36458 cp_lexer_consume_token (parser->lexer);
36459 strcpy (p_name, "#pragma omp target");
36460 if (!flag_openmp) /* flag_openmp_simd */
36462 tree stmt;
36463 switch (ccode)
36465 case OMP_TEAMS:
36466 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36467 OMP_TARGET_CLAUSE_MASK,
36468 cclauses, if_p);
36469 break;
36470 case OMP_PARALLEL:
36471 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36472 OMP_TARGET_CLAUSE_MASK,
36473 cclauses, if_p);
36474 break;
36475 case OMP_SIMD:
36476 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36477 OMP_TARGET_CLAUSE_MASK,
36478 cclauses, if_p);
36479 break;
36480 default:
36481 gcc_unreachable ();
36483 return stmt != NULL_TREE;
36485 keep_next_level (true);
36486 tree sb = begin_omp_structured_block (), ret;
36487 unsigned save = cp_parser_begin_omp_structured_block (parser);
36488 switch (ccode)
36490 case OMP_TEAMS:
36491 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36492 OMP_TARGET_CLAUSE_MASK, cclauses,
36493 if_p);
36494 break;
36495 case OMP_PARALLEL:
36496 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36497 OMP_TARGET_CLAUSE_MASK, cclauses,
36498 if_p);
36499 break;
36500 case OMP_SIMD:
36501 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36502 OMP_TARGET_CLAUSE_MASK, cclauses,
36503 if_p);
36504 break;
36505 default:
36506 gcc_unreachable ();
36508 cp_parser_end_omp_structured_block (parser, save);
36509 tree body = finish_omp_structured_block (sb);
36510 if (ret == NULL_TREE)
36511 return false;
36512 if (ccode == OMP_TEAMS && !processing_template_decl)
36514 /* For combined target teams, ensure the num_teams and
36515 thread_limit clause expressions are evaluated on the host,
36516 before entering the target construct. */
36517 tree c;
36518 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36519 c; c = OMP_CLAUSE_CHAIN (c))
36520 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36521 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36522 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36524 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36525 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36526 if (expr == error_mark_node)
36527 continue;
36528 tree tmp = TARGET_EXPR_SLOT (expr);
36529 add_stmt (expr);
36530 OMP_CLAUSE_OPERAND (c, 0) = expr;
36531 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36532 OMP_CLAUSE_FIRSTPRIVATE);
36533 OMP_CLAUSE_DECL (tc) = tmp;
36534 OMP_CLAUSE_CHAIN (tc)
36535 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36536 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36539 tree stmt = make_node (OMP_TARGET);
36540 TREE_TYPE (stmt) = void_type_node;
36541 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36542 OMP_TARGET_BODY (stmt) = body;
36543 OMP_TARGET_COMBINED (stmt) = 1;
36544 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36545 add_stmt (stmt);
36546 pc = &OMP_TARGET_CLAUSES (stmt);
36547 goto check_clauses;
36549 else if (!flag_openmp) /* flag_openmp_simd */
36551 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36552 return false;
36554 else if (strcmp (p, "data") == 0)
36556 cp_lexer_consume_token (parser->lexer);
36557 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36558 return true;
36560 else if (strcmp (p, "enter") == 0)
36562 cp_lexer_consume_token (parser->lexer);
36563 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36564 return false;
36566 else if (strcmp (p, "exit") == 0)
36568 cp_lexer_consume_token (parser->lexer);
36569 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36570 return false;
36572 else if (strcmp (p, "update") == 0)
36574 cp_lexer_consume_token (parser->lexer);
36575 return cp_parser_omp_target_update (parser, pragma_tok, context);
36578 if (!flag_openmp) /* flag_openmp_simd */
36580 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36581 return false;
36584 stmt = make_node (OMP_TARGET);
36585 TREE_TYPE (stmt) = void_type_node;
36587 OMP_TARGET_CLAUSES (stmt)
36588 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36589 "#pragma omp target", pragma_tok);
36590 pc = &OMP_TARGET_CLAUSES (stmt);
36591 keep_next_level (true);
36592 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36594 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36595 add_stmt (stmt);
36597 check_clauses:
36598 while (*pc)
36600 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36601 switch (OMP_CLAUSE_MAP_KIND (*pc))
36603 case GOMP_MAP_TO:
36604 case GOMP_MAP_ALWAYS_TO:
36605 case GOMP_MAP_FROM:
36606 case GOMP_MAP_ALWAYS_FROM:
36607 case GOMP_MAP_TOFROM:
36608 case GOMP_MAP_ALWAYS_TOFROM:
36609 case GOMP_MAP_ALLOC:
36610 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36611 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36612 case GOMP_MAP_ALWAYS_POINTER:
36613 break;
36614 default:
36615 error_at (OMP_CLAUSE_LOCATION (*pc),
36616 "%<#pragma omp target%> with map-type other "
36617 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36618 "on %<map%> clause");
36619 *pc = OMP_CLAUSE_CHAIN (*pc);
36620 continue;
36622 pc = &OMP_CLAUSE_CHAIN (*pc);
36624 return true;
36627 /* OpenACC 2.0:
36628 # pragma acc cache (variable-list) new-line
36631 static tree
36632 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36634 tree stmt, clauses;
36636 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36637 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36639 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36641 stmt = make_node (OACC_CACHE);
36642 TREE_TYPE (stmt) = void_type_node;
36643 OACC_CACHE_CLAUSES (stmt) = clauses;
36644 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36645 add_stmt (stmt);
36647 return stmt;
36650 /* OpenACC 2.0:
36651 # pragma acc data oacc-data-clause[optseq] new-line
36652 structured-block */
36654 #define OACC_DATA_CLAUSE_MASK \
36655 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36667 static tree
36668 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36670 tree stmt, clauses, block;
36671 unsigned int save;
36673 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36674 "#pragma acc data", pragma_tok);
36676 block = begin_omp_parallel ();
36677 save = cp_parser_begin_omp_structured_block (parser);
36678 cp_parser_statement (parser, NULL_TREE, false, if_p);
36679 cp_parser_end_omp_structured_block (parser, save);
36680 stmt = finish_oacc_data (clauses, block);
36681 return stmt;
36684 /* OpenACC 2.0:
36685 # pragma acc host_data <clauses> new-line
36686 structured-block */
36688 #define OACC_HOST_DATA_CLAUSE_MASK \
36689 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36691 static tree
36692 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36694 tree stmt, clauses, block;
36695 unsigned int save;
36697 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36698 "#pragma acc host_data", pragma_tok);
36700 block = begin_omp_parallel ();
36701 save = cp_parser_begin_omp_structured_block (parser);
36702 cp_parser_statement (parser, NULL_TREE, false, if_p);
36703 cp_parser_end_omp_structured_block (parser, save);
36704 stmt = finish_oacc_host_data (clauses, block);
36705 return stmt;
36708 /* OpenACC 2.0:
36709 # pragma acc declare oacc-data-clause[optseq] new-line
36712 #define OACC_DECLARE_CLAUSE_MASK \
36713 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36726 static tree
36727 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36729 tree clauses, stmt;
36730 bool error = false;
36732 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36733 "#pragma acc declare", pragma_tok, true);
36736 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36738 error_at (pragma_tok->location,
36739 "no valid clauses specified in %<#pragma acc declare%>");
36740 return NULL_TREE;
36743 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36745 location_t loc = OMP_CLAUSE_LOCATION (t);
36746 tree decl = OMP_CLAUSE_DECL (t);
36747 if (!DECL_P (decl))
36749 error_at (loc, "array section in %<#pragma acc declare%>");
36750 error = true;
36751 continue;
36753 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36754 switch (OMP_CLAUSE_MAP_KIND (t))
36756 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36757 case GOMP_MAP_FORCE_ALLOC:
36758 case GOMP_MAP_FORCE_TO:
36759 case GOMP_MAP_FORCE_DEVICEPTR:
36760 case GOMP_MAP_DEVICE_RESIDENT:
36761 break;
36763 case GOMP_MAP_LINK:
36764 if (!global_bindings_p ()
36765 && (TREE_STATIC (decl)
36766 || !DECL_EXTERNAL (decl)))
36768 error_at (loc,
36769 "%qD must be a global variable in "
36770 "%<#pragma acc declare link%>",
36771 decl);
36772 error = true;
36773 continue;
36775 break;
36777 default:
36778 if (global_bindings_p ())
36780 error_at (loc, "invalid OpenACC clause at file scope");
36781 error = true;
36782 continue;
36784 if (DECL_EXTERNAL (decl))
36786 error_at (loc,
36787 "invalid use of %<extern%> variable %qD "
36788 "in %<#pragma acc declare%>", decl);
36789 error = true;
36790 continue;
36792 else if (TREE_PUBLIC (decl))
36794 error_at (loc,
36795 "invalid use of %<global%> variable %qD "
36796 "in %<#pragma acc declare%>", decl);
36797 error = true;
36798 continue;
36800 break;
36803 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36804 || lookup_attribute ("omp declare target link",
36805 DECL_ATTRIBUTES (decl)))
36807 error_at (loc, "variable %qD used more than once with "
36808 "%<#pragma acc declare%>", decl);
36809 error = true;
36810 continue;
36813 if (!error)
36815 tree id;
36817 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36818 id = get_identifier ("omp declare target link");
36819 else
36820 id = get_identifier ("omp declare target");
36822 DECL_ATTRIBUTES (decl)
36823 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36824 if (global_bindings_p ())
36826 symtab_node *node = symtab_node::get (decl);
36827 if (node != NULL)
36829 node->offloadable = 1;
36830 if (ENABLE_OFFLOADING)
36832 g->have_offload = true;
36833 if (is_a <varpool_node *> (node))
36834 vec_safe_push (offload_vars, decl);
36841 if (error || global_bindings_p ())
36842 return NULL_TREE;
36844 stmt = make_node (OACC_DECLARE);
36845 TREE_TYPE (stmt) = void_type_node;
36846 OACC_DECLARE_CLAUSES (stmt) = clauses;
36847 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36849 add_stmt (stmt);
36851 return NULL_TREE;
36854 /* OpenACC 2.0:
36855 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36859 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36861 LOC is the location of the #pragma token.
36864 #define OACC_ENTER_DATA_CLAUSE_MASK \
36865 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36873 #define OACC_EXIT_DATA_CLAUSE_MASK \
36874 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36880 static tree
36881 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36882 bool enter)
36884 location_t loc = pragma_tok->location;
36885 tree stmt, clauses;
36886 const char *p = "";
36888 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36889 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36891 if (strcmp (p, "data") != 0)
36893 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36894 enter ? "enter" : "exit");
36895 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36896 return NULL_TREE;
36899 cp_lexer_consume_token (parser->lexer);
36901 if (enter)
36902 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36903 "#pragma acc enter data", pragma_tok);
36904 else
36905 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36906 "#pragma acc exit data", pragma_tok);
36908 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36910 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36911 enter ? "enter" : "exit");
36912 return NULL_TREE;
36915 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36916 TREE_TYPE (stmt) = void_type_node;
36917 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36918 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36919 add_stmt (stmt);
36920 return stmt;
36923 /* OpenACC 2.0:
36924 # pragma acc loop oacc-loop-clause[optseq] new-line
36925 structured-block */
36927 #define OACC_LOOP_CLAUSE_MASK \
36928 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36939 static tree
36940 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36941 omp_clause_mask mask, tree *cclauses, bool *if_p)
36943 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36945 strcat (p_name, " loop");
36946 mask |= OACC_LOOP_CLAUSE_MASK;
36948 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36949 cclauses == NULL);
36950 if (cclauses)
36952 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36953 if (*cclauses)
36954 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36955 if (clauses)
36956 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36959 tree block = begin_omp_structured_block ();
36960 int save = cp_parser_begin_omp_structured_block (parser);
36961 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36962 cp_parser_end_omp_structured_block (parser, save);
36963 add_stmt (finish_omp_structured_block (block));
36965 return stmt;
36968 /* OpenACC 2.0:
36969 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36970 structured-block
36974 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36975 structured-block
36978 #define OACC_KERNELS_CLAUSE_MASK \
36979 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36997 #define OACC_PARALLEL_CLAUSE_MASK \
36998 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37019 static tree
37020 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37021 char *p_name, bool *if_p)
37023 omp_clause_mask mask;
37024 enum tree_code code;
37025 switch (cp_parser_pragma_kind (pragma_tok))
37027 case PRAGMA_OACC_KERNELS:
37028 strcat (p_name, " kernels");
37029 mask = OACC_KERNELS_CLAUSE_MASK;
37030 code = OACC_KERNELS;
37031 break;
37032 case PRAGMA_OACC_PARALLEL:
37033 strcat (p_name, " parallel");
37034 mask = OACC_PARALLEL_CLAUSE_MASK;
37035 code = OACC_PARALLEL;
37036 break;
37037 default:
37038 gcc_unreachable ();
37041 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37043 const char *p
37044 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37045 if (strcmp (p, "loop") == 0)
37047 cp_lexer_consume_token (parser->lexer);
37048 tree block = begin_omp_parallel ();
37049 tree clauses;
37050 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37051 if_p);
37052 return finish_omp_construct (code, block, clauses);
37056 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37058 tree block = begin_omp_parallel ();
37059 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37060 cp_parser_statement (parser, NULL_TREE, false, if_p);
37061 cp_parser_end_omp_structured_block (parser, save);
37062 return finish_omp_construct (code, block, clauses);
37065 /* OpenACC 2.0:
37066 # pragma acc update oacc-update-clause[optseq] new-line
37069 #define OACC_UPDATE_CLAUSE_MASK \
37070 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37077 static tree
37078 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37080 tree stmt, clauses;
37082 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37083 "#pragma acc update", pragma_tok);
37085 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37087 error_at (pragma_tok->location,
37088 "%<#pragma acc update%> must contain at least one "
37089 "%<device%> or %<host%> or %<self%> clause");
37090 return NULL_TREE;
37093 stmt = make_node (OACC_UPDATE);
37094 TREE_TYPE (stmt) = void_type_node;
37095 OACC_UPDATE_CLAUSES (stmt) = clauses;
37096 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37097 add_stmt (stmt);
37098 return stmt;
37101 /* OpenACC 2.0:
37102 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37104 LOC is the location of the #pragma token.
37107 #define OACC_WAIT_CLAUSE_MASK \
37108 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37110 static tree
37111 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37113 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37114 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37116 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37117 list = cp_parser_oacc_wait_list (parser, loc, list);
37119 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37120 "#pragma acc wait", pragma_tok);
37122 stmt = c_finish_oacc_wait (loc, list, clauses);
37123 stmt = finish_expr_stmt (stmt);
37125 return stmt;
37128 /* OpenMP 4.0:
37129 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37131 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37132 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37139 static void
37140 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37141 enum pragma_context context)
37143 bool first_p = parser->omp_declare_simd == NULL;
37144 cp_omp_declare_simd_data data;
37145 if (first_p)
37147 data.error_seen = false;
37148 data.fndecl_seen = false;
37149 data.tokens = vNULL;
37150 data.clauses = NULL_TREE;
37151 /* It is safe to take the address of a local variable; it will only be
37152 used while this scope is live. */
37153 parser->omp_declare_simd = &data;
37156 /* Store away all pragma tokens. */
37157 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37158 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37159 cp_lexer_consume_token (parser->lexer);
37160 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37161 parser->omp_declare_simd->error_seen = true;
37162 cp_parser_require_pragma_eol (parser, pragma_tok);
37163 struct cp_token_cache *cp
37164 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37165 parser->omp_declare_simd->tokens.safe_push (cp);
37167 if (first_p)
37169 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37170 cp_parser_pragma (parser, context, NULL);
37171 switch (context)
37173 case pragma_external:
37174 cp_parser_declaration (parser);
37175 break;
37176 case pragma_member:
37177 cp_parser_member_declaration (parser);
37178 break;
37179 case pragma_objc_icode:
37180 cp_parser_block_declaration (parser, /*statement_p=*/false);
37181 break;
37182 default:
37183 cp_parser_declaration_statement (parser);
37184 break;
37186 if (parser->omp_declare_simd
37187 && !parser->omp_declare_simd->error_seen
37188 && !parser->omp_declare_simd->fndecl_seen)
37189 error_at (pragma_tok->location,
37190 "%<#pragma omp declare simd%> not immediately followed by "
37191 "function declaration or definition");
37192 data.tokens.release ();
37193 parser->omp_declare_simd = NULL;
37197 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
37198 This function is modelled similar to the late parsing of omp declare
37199 simd. */
37201 static tree
37202 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
37204 struct cp_token_cache *ce;
37205 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
37206 int ii = 0;
37208 if (parser->omp_declare_simd != NULL
37209 || lookup_attribute ("simd", attrs))
37211 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
37212 "used in the same function marked as a Cilk Plus SIMD-enabled "
37213 "function");
37214 parser->cilk_simd_fn_info->tokens.release ();
37215 XDELETE (parser->cilk_simd_fn_info);
37216 parser->cilk_simd_fn_info = NULL;
37217 return attrs;
37219 if (!info->error_seen && info->fndecl_seen)
37221 error ("vector attribute not immediately followed by a single function"
37222 " declaration or definition");
37223 info->error_seen = true;
37225 if (info->error_seen)
37226 return attrs;
37228 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
37230 tree c, cl;
37232 cp_parser_push_lexer_for_tokens (parser, ce);
37233 parser->lexer->in_pragma = true;
37234 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
37235 "SIMD-enabled functions attribute",
37236 NULL);
37237 cp_parser_pop_lexer (parser);
37238 if (cl)
37239 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37241 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
37242 TREE_CHAIN (c) = attrs;
37243 attrs = c;
37245 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37246 TREE_CHAIN (c) = attrs;
37247 if (processing_template_decl)
37248 ATTR_IS_DEPENDENT (c) = 1;
37249 attrs = c;
37251 info->fndecl_seen = true;
37252 parser->cilk_simd_fn_info->tokens.release ();
37253 XDELETE (parser->cilk_simd_fn_info);
37254 parser->cilk_simd_fn_info = NULL;
37255 return attrs;
37258 /* Finalize #pragma omp declare simd clauses after direct declarator has
37259 been parsed, and put that into "omp declare simd" attribute. */
37261 static tree
37262 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37264 struct cp_token_cache *ce;
37265 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37266 int i;
37268 if (!data->error_seen && data->fndecl_seen)
37270 error ("%<#pragma omp declare simd%> not immediately followed by "
37271 "a single function declaration or definition");
37272 data->error_seen = true;
37274 if (data->error_seen)
37275 return attrs;
37277 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37279 tree c, cl;
37281 cp_parser_push_lexer_for_tokens (parser, ce);
37282 parser->lexer->in_pragma = true;
37283 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37284 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37285 cp_lexer_consume_token (parser->lexer);
37286 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37287 "#pragma omp declare simd", pragma_tok);
37288 cp_parser_pop_lexer (parser);
37289 if (cl)
37290 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37291 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37292 TREE_CHAIN (c) = attrs;
37293 if (processing_template_decl)
37294 ATTR_IS_DEPENDENT (c) = 1;
37295 attrs = c;
37298 data->fndecl_seen = true;
37299 return attrs;
37303 /* OpenMP 4.0:
37304 # pragma omp declare target new-line
37305 declarations and definitions
37306 # pragma omp end declare target new-line
37308 OpenMP 4.5:
37309 # pragma omp declare target ( extended-list ) new-line
37311 # pragma omp declare target declare-target-clauses[seq] new-line */
37313 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37314 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37317 static void
37318 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37320 tree clauses = NULL_TREE;
37321 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37322 clauses
37323 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37324 "#pragma omp declare target", pragma_tok);
37325 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37327 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37328 clauses);
37329 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37330 cp_parser_require_pragma_eol (parser, pragma_tok);
37332 else
37334 cp_parser_require_pragma_eol (parser, pragma_tok);
37335 scope_chain->omp_declare_target_attribute++;
37336 return;
37338 if (scope_chain->omp_declare_target_attribute)
37339 error_at (pragma_tok->location,
37340 "%<#pragma omp declare target%> with clauses in between "
37341 "%<#pragma omp declare target%> without clauses and "
37342 "%<#pragma omp end declare target%>");
37343 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37345 tree t = OMP_CLAUSE_DECL (c), id;
37346 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37347 tree at2 = lookup_attribute ("omp declare target link",
37348 DECL_ATTRIBUTES (t));
37349 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37351 id = get_identifier ("omp declare target link");
37352 std::swap (at1, at2);
37354 else
37355 id = get_identifier ("omp declare target");
37356 if (at2)
37358 error_at (OMP_CLAUSE_LOCATION (c),
37359 "%qD specified both in declare target %<link%> and %<to%>"
37360 " clauses", t);
37361 continue;
37363 if (!at1)
37365 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37366 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37367 continue;
37369 symtab_node *node = symtab_node::get (t);
37370 if (node != NULL)
37372 node->offloadable = 1;
37373 if (ENABLE_OFFLOADING)
37375 g->have_offload = true;
37376 if (is_a <varpool_node *> (node))
37377 vec_safe_push (offload_vars, t);
37384 static void
37385 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37387 const char *p = "";
37388 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37390 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37391 p = IDENTIFIER_POINTER (id);
37393 if (strcmp (p, "declare") == 0)
37395 cp_lexer_consume_token (parser->lexer);
37396 p = "";
37397 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37399 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37400 p = IDENTIFIER_POINTER (id);
37402 if (strcmp (p, "target") == 0)
37403 cp_lexer_consume_token (parser->lexer);
37404 else
37406 cp_parser_error (parser, "expected %<target%>");
37407 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37408 return;
37411 else
37413 cp_parser_error (parser, "expected %<declare%>");
37414 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37415 return;
37417 cp_parser_require_pragma_eol (parser, pragma_tok);
37418 if (!scope_chain->omp_declare_target_attribute)
37419 error_at (pragma_tok->location,
37420 "%<#pragma omp end declare target%> without corresponding "
37421 "%<#pragma omp declare target%>");
37422 else
37423 scope_chain->omp_declare_target_attribute--;
37426 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37427 expression and optional initializer clause of
37428 #pragma omp declare reduction. We store the expression(s) as
37429 either 3, 6 or 7 special statements inside of the artificial function's
37430 body. The first two statements are DECL_EXPRs for the artificial
37431 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37432 expression that uses those variables.
37433 If there was any INITIALIZER clause, this is followed by further statements,
37434 the fourth and fifth statements are DECL_EXPRs for the artificial
37435 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37436 constructor variant (first token after open paren is not omp_priv),
37437 then the sixth statement is a statement with the function call expression
37438 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37439 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37440 to initialize the OMP_PRIV artificial variable and there is seventh
37441 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37443 static bool
37444 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37446 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37447 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37448 type = TREE_TYPE (type);
37449 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37450 DECL_ARTIFICIAL (omp_out) = 1;
37451 pushdecl (omp_out);
37452 add_decl_expr (omp_out);
37453 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37454 DECL_ARTIFICIAL (omp_in) = 1;
37455 pushdecl (omp_in);
37456 add_decl_expr (omp_in);
37457 tree combiner;
37458 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37460 keep_next_level (true);
37461 tree block = begin_omp_structured_block ();
37462 combiner = cp_parser_expression (parser);
37463 finish_expr_stmt (combiner);
37464 block = finish_omp_structured_block (block);
37465 add_stmt (block);
37467 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37468 return false;
37470 const char *p = "";
37471 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37473 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37474 p = IDENTIFIER_POINTER (id);
37477 if (strcmp (p, "initializer") == 0)
37479 cp_lexer_consume_token (parser->lexer);
37480 matching_parens parens;
37481 if (!parens.require_open (parser))
37482 return false;
37484 p = "";
37485 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37487 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37488 p = IDENTIFIER_POINTER (id);
37491 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37492 DECL_ARTIFICIAL (omp_priv) = 1;
37493 pushdecl (omp_priv);
37494 add_decl_expr (omp_priv);
37495 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37496 DECL_ARTIFICIAL (omp_orig) = 1;
37497 pushdecl (omp_orig);
37498 add_decl_expr (omp_orig);
37500 keep_next_level (true);
37501 block = begin_omp_structured_block ();
37503 bool ctor = false;
37504 if (strcmp (p, "omp_priv") == 0)
37506 bool is_direct_init, is_non_constant_init;
37507 ctor = true;
37508 cp_lexer_consume_token (parser->lexer);
37509 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37510 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37511 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37512 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37513 == CPP_CLOSE_PAREN
37514 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37515 == CPP_CLOSE_PAREN))
37517 finish_omp_structured_block (block);
37518 error ("invalid initializer clause");
37519 return false;
37521 initializer = cp_parser_initializer (parser, &is_direct_init,
37522 &is_non_constant_init);
37523 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37524 NULL_TREE, LOOKUP_ONLYCONVERTING);
37526 else
37528 cp_parser_parse_tentatively (parser);
37529 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37530 /*check_dependency_p=*/true,
37531 /*template_p=*/NULL,
37532 /*declarator_p=*/false,
37533 /*optional_p=*/false);
37534 vec<tree, va_gc> *args;
37535 if (fn_name == error_mark_node
37536 || cp_parser_error_occurred (parser)
37537 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37538 || ((args = cp_parser_parenthesized_expression_list
37539 (parser, non_attr, /*cast_p=*/false,
37540 /*allow_expansion_p=*/true,
37541 /*non_constant_p=*/NULL)),
37542 cp_parser_error_occurred (parser)))
37544 finish_omp_structured_block (block);
37545 cp_parser_abort_tentative_parse (parser);
37546 cp_parser_error (parser, "expected id-expression (arguments)");
37547 return false;
37549 unsigned int i;
37550 tree arg;
37551 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37552 if (arg == omp_priv
37553 || (TREE_CODE (arg) == ADDR_EXPR
37554 && TREE_OPERAND (arg, 0) == omp_priv))
37555 break;
37556 cp_parser_abort_tentative_parse (parser);
37557 if (arg == NULL_TREE)
37558 error ("one of the initializer call arguments should be %<omp_priv%>"
37559 " or %<&omp_priv%>");
37560 initializer = cp_parser_postfix_expression (parser, false, false, false,
37561 false, NULL);
37562 finish_expr_stmt (initializer);
37565 block = finish_omp_structured_block (block);
37566 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37567 add_stmt (block);
37569 if (ctor)
37570 add_decl_expr (omp_orig);
37572 if (!parens.require_close (parser))
37573 return false;
37576 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37577 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37578 UNKNOWN_LOCATION);
37580 return true;
37583 /* OpenMP 4.0
37584 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37585 initializer-clause[opt] new-line
37587 initializer-clause:
37588 initializer (omp_priv initializer)
37589 initializer (function-name (argument-list)) */
37591 static void
37592 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37593 enum pragma_context)
37595 auto_vec<tree> types;
37596 enum tree_code reduc_code = ERROR_MARK;
37597 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37598 unsigned int i;
37599 cp_token *first_token;
37600 cp_token_cache *cp;
37601 int errs;
37602 void *p;
37604 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37605 p = obstack_alloc (&declarator_obstack, 0);
37607 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37608 goto fail;
37610 switch (cp_lexer_peek_token (parser->lexer)->type)
37612 case CPP_PLUS:
37613 reduc_code = PLUS_EXPR;
37614 break;
37615 case CPP_MULT:
37616 reduc_code = MULT_EXPR;
37617 break;
37618 case CPP_MINUS:
37619 reduc_code = MINUS_EXPR;
37620 break;
37621 case CPP_AND:
37622 reduc_code = BIT_AND_EXPR;
37623 break;
37624 case CPP_XOR:
37625 reduc_code = BIT_XOR_EXPR;
37626 break;
37627 case CPP_OR:
37628 reduc_code = BIT_IOR_EXPR;
37629 break;
37630 case CPP_AND_AND:
37631 reduc_code = TRUTH_ANDIF_EXPR;
37632 break;
37633 case CPP_OR_OR:
37634 reduc_code = TRUTH_ORIF_EXPR;
37635 break;
37636 case CPP_NAME:
37637 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37638 break;
37639 default:
37640 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37641 "%<|%>, %<&&%>, %<||%> or identifier");
37642 goto fail;
37645 if (reduc_code != ERROR_MARK)
37646 cp_lexer_consume_token (parser->lexer);
37648 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37649 if (reduc_id == error_mark_node)
37650 goto fail;
37652 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37653 goto fail;
37655 /* Types may not be defined in declare reduction type list. */
37656 const char *saved_message;
37657 saved_message = parser->type_definition_forbidden_message;
37658 parser->type_definition_forbidden_message
37659 = G_("types may not be defined in declare reduction type list");
37660 bool saved_colon_corrects_to_scope_p;
37661 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37662 parser->colon_corrects_to_scope_p = false;
37663 bool saved_colon_doesnt_start_class_def_p;
37664 saved_colon_doesnt_start_class_def_p
37665 = parser->colon_doesnt_start_class_def_p;
37666 parser->colon_doesnt_start_class_def_p = true;
37668 while (true)
37670 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37671 type = cp_parser_type_id (parser);
37672 if (type == error_mark_node)
37674 else if (ARITHMETIC_TYPE_P (type)
37675 && (orig_reduc_id == NULL_TREE
37676 || (TREE_CODE (type) != COMPLEX_TYPE
37677 && (id_equal (orig_reduc_id, "min")
37678 || id_equal (orig_reduc_id, "max")))))
37679 error_at (loc, "predeclared arithmetic type %qT in "
37680 "%<#pragma omp declare reduction%>", type);
37681 else if (TREE_CODE (type) == FUNCTION_TYPE
37682 || TREE_CODE (type) == METHOD_TYPE
37683 || TREE_CODE (type) == ARRAY_TYPE)
37684 error_at (loc, "function or array type %qT in "
37685 "%<#pragma omp declare reduction%>", type);
37686 else if (TREE_CODE (type) == REFERENCE_TYPE)
37687 error_at (loc, "reference type %qT in "
37688 "%<#pragma omp declare reduction%>", type);
37689 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37690 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37691 "%<#pragma omp declare reduction%>", type);
37692 else
37693 types.safe_push (type);
37695 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37696 cp_lexer_consume_token (parser->lexer);
37697 else
37698 break;
37701 /* Restore the saved message. */
37702 parser->type_definition_forbidden_message = saved_message;
37703 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37704 parser->colon_doesnt_start_class_def_p
37705 = saved_colon_doesnt_start_class_def_p;
37707 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37708 || types.is_empty ())
37710 fail:
37711 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37712 goto done;
37715 first_token = cp_lexer_peek_token (parser->lexer);
37716 cp = NULL;
37717 errs = errorcount;
37718 FOR_EACH_VEC_ELT (types, i, type)
37720 tree fntype
37721 = build_function_type_list (void_type_node,
37722 cp_build_reference_type (type, false),
37723 NULL_TREE);
37724 tree this_reduc_id = reduc_id;
37725 if (!dependent_type_p (type))
37726 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37727 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37728 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37729 DECL_ARTIFICIAL (fndecl) = 1;
37730 DECL_EXTERNAL (fndecl) = 1;
37731 DECL_DECLARED_INLINE_P (fndecl) = 1;
37732 DECL_IGNORED_P (fndecl) = 1;
37733 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37734 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37735 DECL_ATTRIBUTES (fndecl)
37736 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37737 DECL_ATTRIBUTES (fndecl));
37738 if (processing_template_decl)
37739 fndecl = push_template_decl (fndecl);
37740 bool block_scope = false;
37741 tree block = NULL_TREE;
37742 if (current_function_decl)
37744 block_scope = true;
37745 DECL_CONTEXT (fndecl) = global_namespace;
37746 if (!processing_template_decl)
37747 pushdecl (fndecl);
37749 else if (current_class_type)
37751 if (cp == NULL)
37753 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37754 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37755 cp_lexer_consume_token (parser->lexer);
37756 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37757 goto fail;
37758 cp = cp_token_cache_new (first_token,
37759 cp_lexer_peek_nth_token (parser->lexer,
37760 2));
37762 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37763 finish_member_declaration (fndecl);
37764 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37765 DECL_PENDING_INLINE_P (fndecl) = 1;
37766 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37767 continue;
37769 else
37771 DECL_CONTEXT (fndecl) = current_namespace;
37772 pushdecl (fndecl);
37774 if (!block_scope)
37775 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37776 else
37777 block = begin_omp_structured_block ();
37778 if (cp)
37780 cp_parser_push_lexer_for_tokens (parser, cp);
37781 parser->lexer->in_pragma = true;
37783 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37785 if (!block_scope)
37786 finish_function (/*inline_p=*/false);
37787 else
37788 DECL_CONTEXT (fndecl) = current_function_decl;
37789 if (cp)
37790 cp_parser_pop_lexer (parser);
37791 goto fail;
37793 if (cp)
37794 cp_parser_pop_lexer (parser);
37795 if (!block_scope)
37796 finish_function (/*inline_p=*/false);
37797 else
37799 DECL_CONTEXT (fndecl) = current_function_decl;
37800 block = finish_omp_structured_block (block);
37801 if (TREE_CODE (block) == BIND_EXPR)
37802 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37803 else if (TREE_CODE (block) == STATEMENT_LIST)
37804 DECL_SAVED_TREE (fndecl) = block;
37805 if (processing_template_decl)
37806 add_decl_expr (fndecl);
37808 cp_check_omp_declare_reduction (fndecl);
37809 if (cp == NULL && types.length () > 1)
37810 cp = cp_token_cache_new (first_token,
37811 cp_lexer_peek_nth_token (parser->lexer, 2));
37812 if (errs != errorcount)
37813 break;
37816 cp_parser_require_pragma_eol (parser, pragma_tok);
37818 done:
37819 /* Free any declarators allocated. */
37820 obstack_free (&declarator_obstack, p);
37823 /* OpenMP 4.0
37824 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37825 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37826 initializer-clause[opt] new-line
37827 #pragma omp declare target new-line */
37829 static void
37830 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37831 enum pragma_context context)
37833 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37835 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37836 const char *p = IDENTIFIER_POINTER (id);
37838 if (strcmp (p, "simd") == 0)
37840 cp_lexer_consume_token (parser->lexer);
37841 cp_parser_omp_declare_simd (parser, pragma_tok,
37842 context);
37843 return;
37845 cp_ensure_no_omp_declare_simd (parser);
37846 if (strcmp (p, "reduction") == 0)
37848 cp_lexer_consume_token (parser->lexer);
37849 cp_parser_omp_declare_reduction (parser, pragma_tok,
37850 context);
37851 return;
37853 if (!flag_openmp) /* flag_openmp_simd */
37855 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37856 return;
37858 if (strcmp (p, "target") == 0)
37860 cp_lexer_consume_token (parser->lexer);
37861 cp_parser_omp_declare_target (parser, pragma_tok);
37862 return;
37865 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37866 "or %<target%>");
37867 cp_parser_require_pragma_eol (parser, pragma_tok);
37870 /* OpenMP 4.5:
37871 #pragma omp taskloop taskloop-clause[optseq] new-line
37872 for-loop
37874 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37875 for-loop */
37877 #define OMP_TASKLOOP_CLAUSE_MASK \
37878 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37893 static tree
37894 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37895 char *p_name, omp_clause_mask mask, tree *cclauses,
37896 bool *if_p)
37898 tree clauses, sb, ret;
37899 unsigned int save;
37900 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37902 strcat (p_name, " taskloop");
37903 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37905 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37907 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37908 const char *p = IDENTIFIER_POINTER (id);
37910 if (strcmp (p, "simd") == 0)
37912 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37913 if (cclauses == NULL)
37914 cclauses = cclauses_buf;
37916 cp_lexer_consume_token (parser->lexer);
37917 if (!flag_openmp) /* flag_openmp_simd */
37918 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37919 cclauses, if_p);
37920 sb = begin_omp_structured_block ();
37921 save = cp_parser_begin_omp_structured_block (parser);
37922 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37923 cclauses, if_p);
37924 cp_parser_end_omp_structured_block (parser, save);
37925 tree body = finish_omp_structured_block (sb);
37926 if (ret == NULL)
37927 return ret;
37928 ret = make_node (OMP_TASKLOOP);
37929 TREE_TYPE (ret) = void_type_node;
37930 OMP_FOR_BODY (ret) = body;
37931 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37932 SET_EXPR_LOCATION (ret, loc);
37933 add_stmt (ret);
37934 return ret;
37937 if (!flag_openmp) /* flag_openmp_simd */
37939 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37940 return NULL_TREE;
37943 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37944 cclauses == NULL);
37945 if (cclauses)
37947 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37948 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37951 sb = begin_omp_structured_block ();
37952 save = cp_parser_begin_omp_structured_block (parser);
37954 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37955 if_p);
37957 cp_parser_end_omp_structured_block (parser, save);
37958 add_stmt (finish_omp_structured_block (sb));
37960 return ret;
37964 /* OpenACC 2.0:
37965 # pragma acc routine oacc-routine-clause[optseq] new-line
37966 function-definition
37968 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37971 #define OACC_ROUTINE_CLAUSE_MASK \
37972 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37978 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37979 component, which must resolve to a declared namespace-scope
37980 function. The clauses are either processed directly (for a named
37981 function), or defered until the immediatley following declaration
37982 is parsed. */
37984 static void
37985 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37986 enum pragma_context context)
37988 gcc_checking_assert (context == pragma_external);
37989 /* The checking for "another pragma following this one" in the "no optional
37990 '( name )'" case makes sure that we dont re-enter. */
37991 gcc_checking_assert (parser->oacc_routine == NULL);
37993 cp_oacc_routine_data data;
37994 data.error_seen = false;
37995 data.fndecl_seen = false;
37996 data.tokens = vNULL;
37997 data.clauses = NULL_TREE;
37998 data.loc = pragma_tok->location;
37999 /* It is safe to take the address of a local variable; it will only be
38000 used while this scope is live. */
38001 parser->oacc_routine = &data;
38003 /* Look for optional '( name )'. */
38004 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38006 matching_parens parens;
38007 parens.consume_open (parser); /* '(' */
38009 /* We parse the name as an id-expression. If it resolves to
38010 anything other than a non-overloaded function at namespace
38011 scope, it's an error. */
38012 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38013 tree name = cp_parser_id_expression (parser,
38014 /*template_keyword_p=*/false,
38015 /*check_dependency_p=*/false,
38016 /*template_p=*/NULL,
38017 /*declarator_p=*/false,
38018 /*optional_p=*/false);
38019 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
38020 if (name != error_mark_node && decl == error_mark_node)
38021 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38023 if (decl == error_mark_node
38024 || !parens.require_close (parser))
38026 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38027 parser->oacc_routine = NULL;
38028 return;
38031 data.clauses
38032 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38033 "#pragma acc routine",
38034 cp_lexer_peek_token (parser->lexer));
38036 if (decl && is_overloaded_fn (decl)
38037 && (TREE_CODE (decl) != FUNCTION_DECL
38038 || DECL_FUNCTION_TEMPLATE_P (decl)))
38040 error_at (name_loc,
38041 "%<#pragma acc routine%> names a set of overloads");
38042 parser->oacc_routine = NULL;
38043 return;
38046 /* Perhaps we should use the same rule as declarations in different
38047 namespaces? */
38048 if (!DECL_NAMESPACE_SCOPE_P (decl))
38050 error_at (name_loc,
38051 "%qD does not refer to a namespace scope function", decl);
38052 parser->oacc_routine = NULL;
38053 return;
38056 if (TREE_CODE (decl) != FUNCTION_DECL)
38058 error_at (name_loc, "%qD does not refer to a function", decl);
38059 parser->oacc_routine = NULL;
38060 return;
38063 cp_finalize_oacc_routine (parser, decl, false);
38064 parser->oacc_routine = NULL;
38066 else /* No optional '( name )'. */
38068 /* Store away all pragma tokens. */
38069 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38070 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38071 cp_lexer_consume_token (parser->lexer);
38072 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38073 parser->oacc_routine->error_seen = true;
38074 cp_parser_require_pragma_eol (parser, pragma_tok);
38075 struct cp_token_cache *cp
38076 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38077 parser->oacc_routine->tokens.safe_push (cp);
38079 /* Emit a helpful diagnostic if there's another pragma following this
38080 one. */
38081 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38083 cp_ensure_no_oacc_routine (parser);
38084 data.tokens.release ();
38085 /* ..., and then just keep going. */
38086 return;
38089 /* We only have to consider the pragma_external case here. */
38090 cp_parser_declaration (parser);
38091 if (parser->oacc_routine
38092 && !parser->oacc_routine->fndecl_seen)
38093 cp_ensure_no_oacc_routine (parser);
38094 else
38095 parser->oacc_routine = NULL;
38096 data.tokens.release ();
38100 /* Finalize #pragma acc routine clauses after direct declarator has
38101 been parsed. */
38103 static tree
38104 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38106 struct cp_token_cache *ce;
38107 cp_oacc_routine_data *data = parser->oacc_routine;
38109 if (!data->error_seen && data->fndecl_seen)
38111 error_at (data->loc,
38112 "%<#pragma acc routine%> not immediately followed by "
38113 "a single function declaration or definition");
38114 data->error_seen = true;
38116 if (data->error_seen)
38117 return attrs;
38119 gcc_checking_assert (data->tokens.length () == 1);
38120 ce = data->tokens[0];
38122 cp_parser_push_lexer_for_tokens (parser, ce);
38123 parser->lexer->in_pragma = true;
38124 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38126 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38127 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38128 parser->oacc_routine->clauses
38129 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38130 "#pragma acc routine", pragma_tok);
38131 cp_parser_pop_lexer (parser);
38132 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38133 fndecl_seen. */
38135 return attrs;
38138 /* Apply any saved OpenACC routine clauses to a just-parsed
38139 declaration. */
38141 static void
38142 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38144 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38146 /* Keep going if we're in error reporting mode. */
38147 if (parser->oacc_routine->error_seen
38148 || fndecl == error_mark_node)
38149 return;
38151 if (parser->oacc_routine->fndecl_seen)
38153 error_at (parser->oacc_routine->loc,
38154 "%<#pragma acc routine%> not immediately followed by"
38155 " a single function declaration or definition");
38156 parser->oacc_routine = NULL;
38157 return;
38159 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38161 cp_ensure_no_oacc_routine (parser);
38162 return;
38165 if (oacc_get_fn_attrib (fndecl))
38167 error_at (parser->oacc_routine->loc,
38168 "%<#pragma acc routine%> already applied to %qD", fndecl);
38169 parser->oacc_routine = NULL;
38170 return;
38173 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38175 error_at (parser->oacc_routine->loc,
38176 TREE_USED (fndecl)
38177 ? G_("%<#pragma acc routine%> must be applied before use")
38178 : G_("%<#pragma acc routine%> must be applied before "
38179 "definition"));
38180 parser->oacc_routine = NULL;
38181 return;
38184 /* Process the routine's dimension clauses. */
38185 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38186 oacc_replace_fn_attrib (fndecl, dims);
38188 /* Add an "omp declare target" attribute. */
38189 DECL_ATTRIBUTES (fndecl)
38190 = tree_cons (get_identifier ("omp declare target"),
38191 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38193 /* Don't unset parser->oacc_routine here: we may still need it to
38194 diagnose wrong usage. But, remember that we've used this "#pragma acc
38195 routine". */
38196 parser->oacc_routine->fndecl_seen = true;
38200 /* Main entry point to OpenMP statement pragmas. */
38202 static void
38203 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38205 tree stmt;
38206 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38207 omp_clause_mask mask (0);
38209 switch (cp_parser_pragma_kind (pragma_tok))
38211 case PRAGMA_OACC_ATOMIC:
38212 cp_parser_omp_atomic (parser, pragma_tok);
38213 return;
38214 case PRAGMA_OACC_CACHE:
38215 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38216 break;
38217 case PRAGMA_OACC_DATA:
38218 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38219 break;
38220 case PRAGMA_OACC_ENTER_DATA:
38221 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38222 break;
38223 case PRAGMA_OACC_EXIT_DATA:
38224 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38225 break;
38226 case PRAGMA_OACC_HOST_DATA:
38227 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38228 break;
38229 case PRAGMA_OACC_KERNELS:
38230 case PRAGMA_OACC_PARALLEL:
38231 strcpy (p_name, "#pragma acc");
38232 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38233 if_p);
38234 break;
38235 case PRAGMA_OACC_LOOP:
38236 strcpy (p_name, "#pragma acc");
38237 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38238 if_p);
38239 break;
38240 case PRAGMA_OACC_UPDATE:
38241 stmt = cp_parser_oacc_update (parser, pragma_tok);
38242 break;
38243 case PRAGMA_OACC_WAIT:
38244 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38245 break;
38246 case PRAGMA_OMP_ATOMIC:
38247 cp_parser_omp_atomic (parser, pragma_tok);
38248 return;
38249 case PRAGMA_OMP_CRITICAL:
38250 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38251 break;
38252 case PRAGMA_OMP_DISTRIBUTE:
38253 strcpy (p_name, "#pragma omp");
38254 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38255 if_p);
38256 break;
38257 case PRAGMA_OMP_FOR:
38258 strcpy (p_name, "#pragma omp");
38259 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38260 if_p);
38261 break;
38262 case PRAGMA_OMP_MASTER:
38263 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38264 break;
38265 case PRAGMA_OMP_PARALLEL:
38266 strcpy (p_name, "#pragma omp");
38267 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38268 if_p);
38269 break;
38270 case PRAGMA_OMP_SECTIONS:
38271 strcpy (p_name, "#pragma omp");
38272 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38273 break;
38274 case PRAGMA_OMP_SIMD:
38275 strcpy (p_name, "#pragma omp");
38276 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38277 if_p);
38278 break;
38279 case PRAGMA_OMP_SINGLE:
38280 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38281 break;
38282 case PRAGMA_OMP_TASK:
38283 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38284 break;
38285 case PRAGMA_OMP_TASKGROUP:
38286 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38287 break;
38288 case PRAGMA_OMP_TASKLOOP:
38289 strcpy (p_name, "#pragma omp");
38290 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38291 if_p);
38292 break;
38293 case PRAGMA_OMP_TEAMS:
38294 strcpy (p_name, "#pragma omp");
38295 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38296 if_p);
38297 break;
38298 default:
38299 gcc_unreachable ();
38302 protected_set_expr_location (stmt, pragma_tok->location);
38305 /* Transactional Memory parsing routines. */
38307 /* Parse a transaction attribute.
38309 txn-attribute:
38310 attribute
38311 [ [ identifier ] ]
38313 We use this instead of cp_parser_attributes_opt for transactions to avoid
38314 the pedwarn in C++98 mode. */
38316 static tree
38317 cp_parser_txn_attribute_opt (cp_parser *parser)
38319 cp_token *token;
38320 tree attr_name, attr = NULL;
38322 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38323 return cp_parser_attributes_opt (parser);
38325 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38326 return NULL_TREE;
38327 cp_lexer_consume_token (parser->lexer);
38328 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38329 goto error1;
38331 token = cp_lexer_peek_token (parser->lexer);
38332 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38334 token = cp_lexer_consume_token (parser->lexer);
38336 attr_name = (token->type == CPP_KEYWORD
38337 /* For keywords, use the canonical spelling,
38338 not the parsed identifier. */
38339 ? ridpointers[(int) token->keyword]
38340 : token->u.value);
38341 attr = build_tree_list (attr_name, NULL_TREE);
38343 else
38344 cp_parser_error (parser, "expected identifier");
38346 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38347 error1:
38348 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38349 return attr;
38352 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38354 transaction-statement:
38355 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38356 compound-statement
38357 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38360 static tree
38361 cp_parser_transaction (cp_parser *parser, cp_token *token)
38363 unsigned char old_in = parser->in_transaction;
38364 unsigned char this_in = 1, new_in;
38365 enum rid keyword = token->keyword;
38366 tree stmt, attrs, noex;
38368 cp_lexer_consume_token (parser->lexer);
38370 if (keyword == RID_TRANSACTION_RELAXED
38371 || keyword == RID_SYNCHRONIZED)
38372 this_in |= TM_STMT_ATTR_RELAXED;
38373 else
38375 attrs = cp_parser_txn_attribute_opt (parser);
38376 if (attrs)
38377 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38380 /* Parse a noexcept specification. */
38381 if (keyword == RID_ATOMIC_NOEXCEPT)
38382 noex = boolean_true_node;
38383 else if (keyword == RID_ATOMIC_CANCEL)
38385 /* cancel-and-throw is unimplemented. */
38386 sorry ("atomic_cancel");
38387 noex = NULL_TREE;
38389 else
38390 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38392 /* Keep track if we're in the lexical scope of an outer transaction. */
38393 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38395 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38397 parser->in_transaction = new_in;
38398 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38399 parser->in_transaction = old_in;
38401 finish_transaction_stmt (stmt, NULL, this_in, noex);
38403 return stmt;
38406 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38408 transaction-expression:
38409 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38410 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38413 static tree
38414 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38416 unsigned char old_in = parser->in_transaction;
38417 unsigned char this_in = 1;
38418 cp_token *token;
38419 tree expr, noex;
38420 bool noex_expr;
38421 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38423 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38424 || keyword == RID_TRANSACTION_RELAXED);
38426 if (!flag_tm)
38427 error_at (loc,
38428 keyword == RID_TRANSACTION_RELAXED
38429 ? G_("%<__transaction_relaxed%> without transactional memory "
38430 "support enabled")
38431 : G_("%<__transaction_atomic%> without transactional memory "
38432 "support enabled"));
38434 token = cp_parser_require_keyword (parser, keyword,
38435 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38436 : RT_TRANSACTION_RELAXED));
38437 gcc_assert (token != NULL);
38439 if (keyword == RID_TRANSACTION_RELAXED)
38440 this_in |= TM_STMT_ATTR_RELAXED;
38442 /* Set this early. This might mean that we allow transaction_cancel in
38443 an expression that we find out later actually has to be a constexpr.
38444 However, we expect that cxx_constant_value will be able to deal with
38445 this; also, if the noexcept has no constexpr, then what we parse next
38446 really is a transaction's body. */
38447 parser->in_transaction = this_in;
38449 /* Parse a noexcept specification. */
38450 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38451 true);
38453 if (!noex || !noex_expr
38454 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38456 matching_parens parens;
38457 parens.require_open (parser);
38459 expr = cp_parser_expression (parser);
38460 expr = finish_parenthesized_expr (expr);
38462 parens.require_close (parser);
38464 else
38466 /* The only expression that is available got parsed for the noexcept
38467 already. noexcept is true then. */
38468 expr = noex;
38469 noex = boolean_true_node;
38472 expr = build_transaction_expr (token->location, expr, this_in, noex);
38473 parser->in_transaction = old_in;
38475 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38476 return error_mark_node;
38478 return (flag_tm ? expr : error_mark_node);
38481 /* Parse a function-transaction-block.
38483 function-transaction-block:
38484 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38485 function-body
38486 __transaction_atomic txn-attribute[opt] function-try-block
38487 __transaction_relaxed ctor-initializer[opt] function-body
38488 __transaction_relaxed function-try-block
38491 static void
38492 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38494 unsigned char old_in = parser->in_transaction;
38495 unsigned char new_in = 1;
38496 tree compound_stmt, stmt, attrs;
38497 cp_token *token;
38499 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38500 || keyword == RID_TRANSACTION_RELAXED);
38501 token = cp_parser_require_keyword (parser, keyword,
38502 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38503 : RT_TRANSACTION_RELAXED));
38504 gcc_assert (token != NULL);
38506 if (keyword == RID_TRANSACTION_RELAXED)
38507 new_in |= TM_STMT_ATTR_RELAXED;
38508 else
38510 attrs = cp_parser_txn_attribute_opt (parser);
38511 if (attrs)
38512 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38515 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38517 parser->in_transaction = new_in;
38519 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38520 cp_parser_function_try_block (parser);
38521 else
38522 cp_parser_ctor_initializer_opt_and_function_body
38523 (parser, /*in_function_try_block=*/false);
38525 parser->in_transaction = old_in;
38527 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38530 /* Parse a __transaction_cancel statement.
38532 cancel-statement:
38533 __transaction_cancel txn-attribute[opt] ;
38534 __transaction_cancel txn-attribute[opt] throw-expression ;
38536 ??? Cancel and throw is not yet implemented. */
38538 static tree
38539 cp_parser_transaction_cancel (cp_parser *parser)
38541 cp_token *token;
38542 bool is_outer = false;
38543 tree stmt, attrs;
38545 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38546 RT_TRANSACTION_CANCEL);
38547 gcc_assert (token != NULL);
38549 attrs = cp_parser_txn_attribute_opt (parser);
38550 if (attrs)
38551 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38553 /* ??? Parse cancel-and-throw here. */
38555 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38557 if (!flag_tm)
38559 error_at (token->location, "%<__transaction_cancel%> without "
38560 "transactional memory support enabled");
38561 return error_mark_node;
38563 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38565 error_at (token->location, "%<__transaction_cancel%> within a "
38566 "%<__transaction_relaxed%>");
38567 return error_mark_node;
38569 else if (is_outer)
38571 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38572 && !is_tm_may_cancel_outer (current_function_decl))
38574 error_at (token->location, "outer %<__transaction_cancel%> not "
38575 "within outer %<__transaction_atomic%>");
38576 error_at (token->location,
38577 " or a %<transaction_may_cancel_outer%> function");
38578 return error_mark_node;
38581 else if (parser->in_transaction == 0)
38583 error_at (token->location, "%<__transaction_cancel%> not within "
38584 "%<__transaction_atomic%>");
38585 return error_mark_node;
38588 stmt = build_tm_abort_call (token->location, is_outer);
38589 add_stmt (stmt);
38591 return stmt;
38594 /* The parser. */
38596 static GTY (()) cp_parser *the_parser;
38599 /* Special handling for the first token or line in the file. The first
38600 thing in the file might be #pragma GCC pch_preprocess, which loads a
38601 PCH file, which is a GC collection point. So we need to handle this
38602 first pragma without benefit of an existing lexer structure.
38604 Always returns one token to the caller in *FIRST_TOKEN. This is
38605 either the true first token of the file, or the first token after
38606 the initial pragma. */
38608 static void
38609 cp_parser_initial_pragma (cp_token *first_token)
38611 tree name = NULL;
38613 cp_lexer_get_preprocessor_token (NULL, first_token);
38614 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38615 return;
38617 cp_lexer_get_preprocessor_token (NULL, first_token);
38618 if (first_token->type == CPP_STRING)
38620 name = first_token->u.value;
38622 cp_lexer_get_preprocessor_token (NULL, first_token);
38623 if (first_token->type != CPP_PRAGMA_EOL)
38624 error_at (first_token->location,
38625 "junk at end of %<#pragma GCC pch_preprocess%>");
38627 else
38628 error_at (first_token->location, "expected string literal");
38630 /* Skip to the end of the pragma. */
38631 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38632 cp_lexer_get_preprocessor_token (NULL, first_token);
38634 /* Now actually load the PCH file. */
38635 if (name)
38636 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38638 /* Read one more token to return to our caller. We have to do this
38639 after reading the PCH file in, since its pointers have to be
38640 live. */
38641 cp_lexer_get_preprocessor_token (NULL, first_token);
38644 /* Parses the grainsize pragma for the _Cilk_for statement.
38645 Syntax:
38646 #pragma cilk grainsize = <VALUE>. */
38648 static void
38649 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38651 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38653 tree exp = cp_parser_binary_expression (parser, false, false,
38654 PREC_NOT_OPERATOR, NULL);
38655 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38656 if (!exp || exp == error_mark_node)
38658 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38659 return;
38662 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38664 cp_parser_cilk_for (parser, exp, if_p);
38665 else
38666 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38667 "%<#pragma cilk grainsize%> is not followed by "
38668 "%<_Cilk_for%>");
38669 return;
38671 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38674 /* Normal parsing of a pragma token. Here we can (and must) use the
38675 regular lexer. */
38677 static bool
38678 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38680 cp_token *pragma_tok;
38681 unsigned int id;
38682 tree stmt;
38683 bool ret;
38685 pragma_tok = cp_lexer_consume_token (parser->lexer);
38686 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38687 parser->lexer->in_pragma = true;
38689 id = cp_parser_pragma_kind (pragma_tok);
38690 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38691 cp_ensure_no_omp_declare_simd (parser);
38692 switch (id)
38694 case PRAGMA_GCC_PCH_PREPROCESS:
38695 error_at (pragma_tok->location,
38696 "%<#pragma GCC pch_preprocess%> must be first");
38697 break;
38699 case PRAGMA_OMP_BARRIER:
38700 switch (context)
38702 case pragma_compound:
38703 cp_parser_omp_barrier (parser, pragma_tok);
38704 return false;
38705 case pragma_stmt:
38706 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38707 "used in compound statements", "omp barrier");
38708 break;
38709 default:
38710 goto bad_stmt;
38712 break;
38714 case PRAGMA_OMP_FLUSH:
38715 switch (context)
38717 case pragma_compound:
38718 cp_parser_omp_flush (parser, pragma_tok);
38719 return false;
38720 case pragma_stmt:
38721 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38722 "used in compound statements", "omp flush");
38723 break;
38724 default:
38725 goto bad_stmt;
38727 break;
38729 case PRAGMA_OMP_TASKWAIT:
38730 switch (context)
38732 case pragma_compound:
38733 cp_parser_omp_taskwait (parser, pragma_tok);
38734 return false;
38735 case pragma_stmt:
38736 error_at (pragma_tok->location,
38737 "%<#pragma %s%> may only be used in compound statements",
38738 "omp taskwait");
38739 break;
38740 default:
38741 goto bad_stmt;
38743 break;
38745 case PRAGMA_OMP_TASKYIELD:
38746 switch (context)
38748 case pragma_compound:
38749 cp_parser_omp_taskyield (parser, pragma_tok);
38750 return false;
38751 case pragma_stmt:
38752 error_at (pragma_tok->location,
38753 "%<#pragma %s%> may only be used in compound statements",
38754 "omp taskyield");
38755 break;
38756 default:
38757 goto bad_stmt;
38759 break;
38761 case PRAGMA_OMP_CANCEL:
38762 switch (context)
38764 case pragma_compound:
38765 cp_parser_omp_cancel (parser, pragma_tok);
38766 return false;
38767 case pragma_stmt:
38768 error_at (pragma_tok->location,
38769 "%<#pragma %s%> may only be used in compound statements",
38770 "omp cancel");
38771 break;
38772 default:
38773 goto bad_stmt;
38775 break;
38777 case PRAGMA_OMP_CANCELLATION_POINT:
38778 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38779 return false;
38781 case PRAGMA_OMP_THREADPRIVATE:
38782 cp_parser_omp_threadprivate (parser, pragma_tok);
38783 return false;
38785 case PRAGMA_OMP_DECLARE:
38786 cp_parser_omp_declare (parser, pragma_tok, context);
38787 return false;
38789 case PRAGMA_OACC_DECLARE:
38790 cp_parser_oacc_declare (parser, pragma_tok);
38791 return false;
38793 case PRAGMA_OACC_ENTER_DATA:
38794 if (context == pragma_stmt)
38796 error_at (pragma_tok->location,
38797 "%<#pragma %s%> may only be used in compound statements",
38798 "acc enter data");
38799 break;
38801 else if (context != pragma_compound)
38802 goto bad_stmt;
38803 cp_parser_omp_construct (parser, pragma_tok, if_p);
38804 return true;
38806 case PRAGMA_OACC_EXIT_DATA:
38807 if (context == pragma_stmt)
38809 error_at (pragma_tok->location,
38810 "%<#pragma %s%> may only be used in compound statements",
38811 "acc exit data");
38812 break;
38814 else if (context != pragma_compound)
38815 goto bad_stmt;
38816 cp_parser_omp_construct (parser, pragma_tok, if_p);
38817 return true;
38819 case PRAGMA_OACC_ROUTINE:
38820 if (context != pragma_external)
38822 error_at (pragma_tok->location,
38823 "%<#pragma acc routine%> must be at file scope");
38824 break;
38826 cp_parser_oacc_routine (parser, pragma_tok, context);
38827 return false;
38829 case PRAGMA_OACC_UPDATE:
38830 if (context == pragma_stmt)
38832 error_at (pragma_tok->location,
38833 "%<#pragma %s%> may only be used in compound statements",
38834 "acc update");
38835 break;
38837 else if (context != pragma_compound)
38838 goto bad_stmt;
38839 cp_parser_omp_construct (parser, pragma_tok, if_p);
38840 return true;
38842 case PRAGMA_OACC_WAIT:
38843 if (context == pragma_stmt)
38845 error_at (pragma_tok->location,
38846 "%<#pragma %s%> may only be used in compound statements",
38847 "acc wait");
38848 break;
38850 else if (context != pragma_compound)
38851 goto bad_stmt;
38852 cp_parser_omp_construct (parser, pragma_tok, if_p);
38853 return true;
38855 case PRAGMA_OACC_ATOMIC:
38856 case PRAGMA_OACC_CACHE:
38857 case PRAGMA_OACC_DATA:
38858 case PRAGMA_OACC_HOST_DATA:
38859 case PRAGMA_OACC_KERNELS:
38860 case PRAGMA_OACC_PARALLEL:
38861 case PRAGMA_OACC_LOOP:
38862 case PRAGMA_OMP_ATOMIC:
38863 case PRAGMA_OMP_CRITICAL:
38864 case PRAGMA_OMP_DISTRIBUTE:
38865 case PRAGMA_OMP_FOR:
38866 case PRAGMA_OMP_MASTER:
38867 case PRAGMA_OMP_PARALLEL:
38868 case PRAGMA_OMP_SECTIONS:
38869 case PRAGMA_OMP_SIMD:
38870 case PRAGMA_OMP_SINGLE:
38871 case PRAGMA_OMP_TASK:
38872 case PRAGMA_OMP_TASKGROUP:
38873 case PRAGMA_OMP_TASKLOOP:
38874 case PRAGMA_OMP_TEAMS:
38875 if (context != pragma_stmt && context != pragma_compound)
38876 goto bad_stmt;
38877 stmt = push_omp_privatization_clauses (false);
38878 cp_parser_omp_construct (parser, pragma_tok, if_p);
38879 pop_omp_privatization_clauses (stmt);
38880 return true;
38882 case PRAGMA_OMP_ORDERED:
38883 if (context != pragma_stmt && context != pragma_compound)
38884 goto bad_stmt;
38885 stmt = push_omp_privatization_clauses (false);
38886 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38887 pop_omp_privatization_clauses (stmt);
38888 return ret;
38890 case PRAGMA_OMP_TARGET:
38891 if (context != pragma_stmt && context != pragma_compound)
38892 goto bad_stmt;
38893 stmt = push_omp_privatization_clauses (false);
38894 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38895 pop_omp_privatization_clauses (stmt);
38896 return ret;
38898 case PRAGMA_OMP_END_DECLARE_TARGET:
38899 cp_parser_omp_end_declare_target (parser, pragma_tok);
38900 return false;
38902 case PRAGMA_OMP_SECTION:
38903 error_at (pragma_tok->location,
38904 "%<#pragma omp section%> may only be used in "
38905 "%<#pragma omp sections%> construct");
38906 break;
38908 case PRAGMA_IVDEP:
38910 if (context == pragma_external)
38912 error_at (pragma_tok->location,
38913 "%<#pragma GCC ivdep%> must be inside a function");
38914 break;
38916 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38917 cp_token *tok;
38918 tok = cp_lexer_peek_token (the_parser->lexer);
38919 if (tok->type != CPP_KEYWORD
38920 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38921 && tok->keyword != RID_DO))
38923 cp_parser_error (parser, "for, while or do statement expected");
38924 return false;
38926 cp_parser_iteration_statement (parser, if_p, true);
38927 return true;
38930 case PRAGMA_CILK_SIMD:
38931 if (context == pragma_external)
38933 error_at (pragma_tok->location,
38934 "%<#pragma simd%> must be inside a function");
38935 break;
38937 stmt = push_omp_privatization_clauses (false);
38938 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38939 pop_omp_privatization_clauses (stmt);
38940 return true;
38942 case PRAGMA_CILK_GRAINSIZE:
38943 if (context == pragma_external)
38945 error_at (pragma_tok->location,
38946 "%<#pragma cilk grainsize%> must be inside a function");
38947 break;
38950 /* Ignore the pragma if Cilk Plus is not enabled. */
38951 if (flag_cilkplus)
38953 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38954 return true;
38956 else
38958 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38959 "%<#pragma cilk grainsize%>");
38960 break;
38963 default:
38964 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38965 c_invoke_pragma_handler (id);
38966 break;
38968 bad_stmt:
38969 cp_parser_error (parser, "expected declaration specifiers");
38970 break;
38973 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38974 return false;
38977 /* The interface the pragma parsers have to the lexer. */
38979 enum cpp_ttype
38980 pragma_lex (tree *value, location_t *loc)
38982 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38983 enum cpp_ttype ret = tok->type;
38985 *value = tok->u.value;
38986 if (loc)
38987 *loc = tok->location;
38989 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38990 ret = CPP_EOF;
38991 else if (ret == CPP_STRING)
38992 *value = cp_parser_string_literal (the_parser, false, false);
38993 else
38995 if (ret == CPP_KEYWORD)
38996 ret = CPP_NAME;
38997 cp_lexer_consume_token (the_parser->lexer);
39000 return ret;
39004 /* External interface. */
39006 /* Parse one entire translation unit. */
39008 void
39009 c_parse_file (void)
39011 static bool already_called = false;
39013 if (already_called)
39014 fatal_error (input_location,
39015 "inter-module optimizations not implemented for C++");
39016 already_called = true;
39018 the_parser = cp_parser_new ();
39019 push_deferring_access_checks (flag_access_control
39020 ? dk_no_deferred : dk_no_check);
39021 cp_parser_translation_unit (the_parser);
39022 the_parser = NULL;
39025 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
39026 vectorlength clause:
39027 Syntax:
39028 vectorlength ( constant-expression ) */
39030 static tree
39031 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
39032 bool is_simd_fn)
39034 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39035 tree expr;
39036 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
39037 safelen clause. Thus, vectorlength is represented as OMP 4.0
39038 safelen. For SIMD-enabled function it is represented by OMP 4.0
39039 simdlen. */
39040 if (!is_simd_fn)
39041 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
39042 loc);
39043 else
39044 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
39045 loc);
39047 matching_parens parens;
39048 if (!parens.require_open (parser))
39049 return error_mark_node;
39051 expr = cp_parser_constant_expression (parser);
39052 expr = maybe_constant_value (expr);
39054 /* If expr == error_mark_node, then don't emit any errors nor
39055 create a clause. if any of the above functions returns
39056 error mark node then they would have emitted an error message. */
39057 if (expr == error_mark_node)
39059 else if (!TREE_TYPE (expr)
39060 || !TREE_CONSTANT (expr)
39061 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
39062 error_at (loc, "vectorlength must be an integer constant");
39063 else if (TREE_CONSTANT (expr)
39064 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
39065 error_at (loc, "vectorlength must be a power of 2");
39066 else
39068 tree c;
39069 if (!is_simd_fn)
39071 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
39072 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
39073 OMP_CLAUSE_CHAIN (c) = clauses;
39074 clauses = c;
39076 else
39078 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
39079 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
39080 OMP_CLAUSE_CHAIN (c) = clauses;
39081 clauses = c;
39085 if (!parens.require_close (parser))
39086 return error_mark_node;
39087 return clauses;
39090 /* Handles the Cilk Plus #pragma simd linear clause.
39091 Syntax:
39092 linear ( simd-linear-variable-list )
39094 simd-linear-variable-list:
39095 simd-linear-variable
39096 simd-linear-variable-list , simd-linear-variable
39098 simd-linear-variable:
39099 id-expression
39100 id-expression : simd-linear-step
39102 simd-linear-step:
39103 conditional-expression */
39105 static tree
39106 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
39108 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39110 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39111 return clauses;
39112 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39114 cp_parser_error (parser, "expected identifier");
39115 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
39116 return error_mark_node;
39119 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39120 parser->colon_corrects_to_scope_p = false;
39121 while (1)
39123 cp_token *token = cp_lexer_peek_token (parser->lexer);
39124 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39126 cp_parser_error (parser, "expected variable-name");
39127 clauses = error_mark_node;
39128 break;
39131 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
39132 false, false);
39133 tree decl = cp_parser_lookup_name_simple (parser, var_name,
39134 token->location);
39135 if (decl == error_mark_node)
39137 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
39138 token->location);
39139 clauses = error_mark_node;
39141 else
39143 tree e = NULL_TREE;
39144 tree step_size = integer_one_node;
39146 /* If present, parse the linear step. Otherwise, assume the default
39147 value of 1. */
39148 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
39150 cp_lexer_consume_token (parser->lexer);
39152 e = cp_parser_assignment_expression (parser);
39153 e = maybe_constant_value (e);
39155 if (e == error_mark_node)
39157 /* If an error has occurred, then the whole pragma is
39158 considered ill-formed. Thus, no reason to keep
39159 parsing. */
39160 clauses = error_mark_node;
39161 break;
39163 else if (type_dependent_expression_p (e)
39164 || value_dependent_expression_p (e)
39165 || (TREE_TYPE (e)
39166 && INTEGRAL_TYPE_P (TREE_TYPE (e))
39167 && (TREE_CONSTANT (e)
39168 || DECL_P (e))))
39169 step_size = e;
39170 else
39171 cp_parser_error (parser,
39172 "step size must be an integer constant "
39173 "expression or an integer variable");
39176 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
39177 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
39178 OMP_CLAUSE_DECL (l) = decl;
39179 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
39180 OMP_CLAUSE_CHAIN (l) = clauses;
39181 clauses = l;
39183 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39184 cp_lexer_consume_token (parser->lexer);
39185 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
39186 break;
39187 else
39189 error_at (cp_lexer_peek_token (parser->lexer)->location,
39190 "expected %<,%> or %<)%> after %qE", decl);
39191 clauses = error_mark_node;
39192 break;
39195 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39196 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
39197 return clauses;
39200 /* Returns the name of the next clause. If the clause is not
39201 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
39202 token is not consumed. Otherwise, the appropriate enum from the
39203 pragma_simd_clause is returned and the token is consumed. */
39205 static pragma_omp_clause
39206 cp_parser_cilk_simd_clause_name (cp_parser *parser)
39208 pragma_omp_clause clause_type;
39209 cp_token *token = cp_lexer_peek_token (parser->lexer);
39211 if (token->keyword == RID_PRIVATE)
39212 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
39213 else if (!token->u.value || token->type != CPP_NAME)
39214 return PRAGMA_CILK_CLAUSE_NONE;
39215 else if (id_equal (token->u.value, "vectorlength"))
39216 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
39217 else if (id_equal (token->u.value, "linear"))
39218 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
39219 else if (id_equal (token->u.value, "firstprivate"))
39220 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
39221 else if (id_equal (token->u.value, "lastprivate"))
39222 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
39223 else if (id_equal (token->u.value, "reduction"))
39224 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
39225 else
39226 return PRAGMA_CILK_CLAUSE_NONE;
39228 cp_lexer_consume_token (parser->lexer);
39229 return clause_type;
39232 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
39234 static tree
39235 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
39237 tree clauses = NULL_TREE;
39239 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39240 && clauses != error_mark_node)
39242 pragma_omp_clause c_kind;
39243 c_kind = cp_parser_cilk_simd_clause_name (parser);
39244 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
39245 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
39246 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
39247 clauses = cp_parser_cilk_simd_linear (parser, clauses);
39248 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
39249 /* Use the OpenMP 4.0 equivalent function. */
39250 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
39251 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
39252 /* Use the OpenMP 4.0 equivalent function. */
39253 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
39254 clauses);
39255 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
39256 /* Use the OMP 4.0 equivalent function. */
39257 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
39258 clauses);
39259 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
39260 /* Use the OMP 4.0 equivalent function. */
39261 clauses = cp_parser_omp_clause_reduction (parser, clauses);
39262 else
39264 clauses = error_mark_node;
39265 cp_parser_error (parser, "expected %<#pragma simd%> clause");
39266 break;
39270 cp_parser_skip_to_pragma_eol (parser, pragma_token);
39272 if (clauses == error_mark_node)
39273 return error_mark_node;
39274 else
39275 return finish_omp_clauses (clauses, C_ORT_CILK);
39278 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
39280 static void
39281 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
39283 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
39285 if (clauses == error_mark_node)
39286 return;
39288 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
39290 error_at (cp_lexer_peek_token (parser->lexer)->location,
39291 "for statement expected");
39292 return;
39295 tree sb = begin_omp_structured_block ();
39296 int save = cp_parser_begin_omp_structured_block (parser);
39297 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
39298 if (ret)
39299 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
39300 cp_parser_end_omp_structured_block (parser, save);
39301 add_stmt (finish_omp_structured_block (sb));
39304 /* Main entry-point for parsing Cilk Plus _Cilk_for
39305 loops. The return value is error_mark_node
39306 when errors happen and CILK_FOR tree on success. */
39308 static tree
39309 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
39311 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
39312 gcc_unreachable ();
39314 tree sb = begin_omp_structured_block ();
39315 int save = cp_parser_begin_omp_structured_block (parser);
39317 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
39318 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
39319 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
39320 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
39322 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
39323 if (ret)
39324 cpp_validate_cilk_plus_loop (ret);
39325 else
39326 ret = error_mark_node;
39328 cp_parser_end_omp_structured_block (parser, save);
39329 add_stmt (finish_omp_structured_block (sb));
39330 return ret;
39333 /* Create an identifier for a generic parameter type (a synthesized
39334 template parameter implied by `auto' or a concept identifier). */
39336 static GTY(()) int generic_parm_count;
39337 static tree
39338 make_generic_type_name ()
39340 char buf[32];
39341 sprintf (buf, "auto:%d", ++generic_parm_count);
39342 return get_identifier (buf);
39345 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39346 (creating a new template parameter list if necessary). Returns the newly
39347 created template type parm. */
39349 static tree
39350 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39352 gcc_assert (current_binding_level->kind == sk_function_parms);
39354 /* Before committing to modifying any scope, if we're in an
39355 implicit template scope, and we're trying to synthesize a
39356 constrained parameter, try to find a previous parameter with
39357 the same name. This is the same-type rule for abbreviated
39358 function templates.
39360 NOTE: We can generate implicit parameters when tentatively
39361 parsing a nested name specifier, only to reject that parse
39362 later. However, matching the same template-id as part of a
39363 direct-declarator should generate an identical template
39364 parameter, so this rule will merge them. */
39365 if (parser->implicit_template_scope && constr)
39367 tree t = parser->implicit_template_parms;
39368 while (t)
39370 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39372 tree d = TREE_VALUE (t);
39373 if (TREE_CODE (d) == PARM_DECL)
39374 /* Return the TEMPLATE_PARM_INDEX. */
39375 d = DECL_INITIAL (d);
39376 return d;
39378 t = TREE_CHAIN (t);
39382 /* We are either continuing a function template that already contains implicit
39383 template parameters, creating a new fully-implicit function template, or
39384 extending an existing explicit function template with implicit template
39385 parameters. */
39387 cp_binding_level *const entry_scope = current_binding_level;
39389 bool become_template = false;
39390 cp_binding_level *parent_scope = 0;
39392 if (parser->implicit_template_scope)
39394 gcc_assert (parser->implicit_template_parms);
39396 current_binding_level = parser->implicit_template_scope;
39398 else
39400 /* Roll back to the existing template parameter scope (in the case of
39401 extending an explicit function template) or introduce a new template
39402 parameter scope ahead of the function parameter scope (or class scope
39403 in the case of out-of-line member definitions). The function scope is
39404 added back after template parameter synthesis below. */
39406 cp_binding_level *scope = entry_scope;
39408 while (scope->kind == sk_function_parms)
39410 parent_scope = scope;
39411 scope = scope->level_chain;
39413 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39415 /* If not defining a class, then any class scope is a scope level in
39416 an out-of-line member definition. In this case simply wind back
39417 beyond the first such scope to inject the template parameter list.
39418 Otherwise wind back to the class being defined. The latter can
39419 occur in class member friend declarations such as:
39421 class A {
39422 void foo (auto);
39424 class B {
39425 friend void A::foo (auto);
39428 The template parameter list synthesized for the friend declaration
39429 must be injected in the scope of 'B'. This can also occur in
39430 erroneous cases such as:
39432 struct A {
39433 struct B {
39434 void foo (auto);
39436 void B::foo (auto) {}
39439 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39440 but, nevertheless, the template parameter list synthesized for the
39441 declarator should be injected into the scope of 'A' as if the
39442 ill-formed template was specified explicitly. */
39444 while (scope->kind == sk_class && !scope->defining_class_p)
39446 parent_scope = scope;
39447 scope = scope->level_chain;
39451 current_binding_level = scope;
39453 if (scope->kind != sk_template_parms
39454 || !function_being_declared_is_template_p (parser))
39456 /* Introduce a new template parameter list for implicit template
39457 parameters. */
39459 become_template = true;
39461 parser->implicit_template_scope
39462 = begin_scope (sk_template_parms, NULL);
39464 ++processing_template_decl;
39466 parser->fully_implicit_function_template_p = true;
39467 ++parser->num_template_parameter_lists;
39469 else
39471 /* Synthesize implicit template parameters at the end of the explicit
39472 template parameter list. */
39474 gcc_assert (current_template_parms);
39476 parser->implicit_template_scope = scope;
39478 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39479 parser->implicit_template_parms
39480 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39484 /* Synthesize a new template parameter and track the current template
39485 parameter chain with implicit_template_parms. */
39487 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39488 tree synth_id = make_generic_type_name ();
39489 tree synth_tmpl_parm;
39490 bool non_type = false;
39492 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39493 synth_tmpl_parm
39494 = finish_template_type_parm (class_type_node, synth_id);
39495 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39496 synth_tmpl_parm
39497 = finish_constrained_template_template_parm (proto, synth_id);
39498 else
39500 synth_tmpl_parm = copy_decl (proto);
39501 DECL_NAME (synth_tmpl_parm) = synth_id;
39502 non_type = true;
39505 // Attach the constraint to the parm before processing.
39506 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39507 TREE_TYPE (node) = constr;
39508 tree new_parm
39509 = process_template_parm (parser->implicit_template_parms,
39510 input_location,
39511 node,
39512 /*non_type=*/non_type,
39513 /*param_pack=*/false);
39515 // Chain the new parameter to the list of implicit parameters.
39516 if (parser->implicit_template_parms)
39517 parser->implicit_template_parms
39518 = TREE_CHAIN (parser->implicit_template_parms);
39519 else
39520 parser->implicit_template_parms = new_parm;
39522 tree new_decl = get_local_decls ();
39523 if (non_type)
39524 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39525 new_decl = DECL_INITIAL (new_decl);
39527 /* If creating a fully implicit function template, start the new implicit
39528 template parameter list with this synthesized type, otherwise grow the
39529 current template parameter list. */
39531 if (become_template)
39533 parent_scope->level_chain = current_binding_level;
39535 tree new_parms = make_tree_vec (1);
39536 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39537 current_template_parms = tree_cons (size_int (processing_template_decl),
39538 new_parms, current_template_parms);
39540 else
39542 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39543 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39544 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39545 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39548 // If the new parameter was constrained, we need to add that to the
39549 // constraints in the template parameter list.
39550 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39552 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39553 reqs = conjoin_constraints (reqs, req);
39554 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39557 current_binding_level = entry_scope;
39559 return new_decl;
39562 /* Finish the declaration of a fully implicit function template. Such a
39563 template has no explicit template parameter list so has not been through the
39564 normal template head and tail processing. synthesize_implicit_template_parm
39565 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39566 provided if the declaration is a class member such that its template
39567 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39568 form is returned. Otherwise NULL_TREE is returned. */
39570 static tree
39571 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39573 gcc_assert (parser->fully_implicit_function_template_p);
39575 if (member_decl_opt && member_decl_opt != error_mark_node
39576 && DECL_VIRTUAL_P (member_decl_opt))
39578 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39579 "implicit templates may not be %<virtual%>");
39580 DECL_VIRTUAL_P (member_decl_opt) = false;
39583 if (member_decl_opt)
39584 member_decl_opt = finish_member_template_decl (member_decl_opt);
39585 end_template_decl ();
39587 parser->fully_implicit_function_template_p = false;
39588 --parser->num_template_parameter_lists;
39590 return member_decl_opt;
39593 /* Helper function for diagnostics that have complained about things
39594 being used with 'extern "C"' linkage.
39596 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39598 void
39599 maybe_show_extern_c_location (void)
39601 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39602 inform (the_parser->innermost_linkage_specification_location,
39603 "%<extern \"C\"%> linkage started here");
39606 #include "gt-cp-parser.h"