Fix location of sizeof/alignof (PR c++/80016)
[official-gcc.git] / gcc / cp / parser.c
blob3a0e0cb050fcf37b1760f8f2c61e7df8aac7cac1
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_INTERATION, /* 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 (C_IS_RESERVED_WORD (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, 0);
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->attributes = NULL_TREE;
1455 declarator->std_attributes = NULL_TREE;
1456 declarator->declarator = NULL;
1457 declarator->parameter_pack_p = false;
1458 declarator->id_loc = UNKNOWN_LOCATION;
1460 return declarator;
1463 /* Make a declarator for a generalized identifier. If
1464 QUALIFYING_SCOPE is non-NULL, the identifier is
1465 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1466 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1467 is, if any. */
1469 static cp_declarator *
1470 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1471 special_function_kind sfk)
1473 cp_declarator *declarator;
1475 /* It is valid to write:
1477 class C { void f(); };
1478 typedef C D;
1479 void D::f();
1481 The standard is not clear about whether `typedef const C D' is
1482 legal; as of 2002-09-15 the committee is considering that
1483 question. EDG 3.0 allows that syntax. Therefore, we do as
1484 well. */
1485 if (qualifying_scope && TYPE_P (qualifying_scope))
1486 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1488 gcc_assert (identifier_p (unqualified_name)
1489 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1490 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1492 declarator = make_declarator (cdk_id);
1493 declarator->u.id.qualifying_scope = qualifying_scope;
1494 declarator->u.id.unqualified_name = unqualified_name;
1495 declarator->u.id.sfk = sfk;
1497 return declarator;
1500 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1501 of modifiers such as const or volatile to apply to the pointer
1502 type, represented as identifiers. ATTRIBUTES represent the attributes that
1503 appertain to the pointer or reference. */
1505 cp_declarator *
1506 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1507 tree attributes)
1509 cp_declarator *declarator;
1511 declarator = make_declarator (cdk_pointer);
1512 declarator->declarator = target;
1513 declarator->u.pointer.qualifiers = cv_qualifiers;
1514 declarator->u.pointer.class_type = NULL_TREE;
1515 if (target)
1517 declarator->id_loc = target->id_loc;
1518 declarator->parameter_pack_p = target->parameter_pack_p;
1519 target->parameter_pack_p = false;
1521 else
1522 declarator->parameter_pack_p = false;
1524 declarator->std_attributes = attributes;
1526 return declarator;
1529 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1530 represent the attributes that appertain to the pointer or
1531 reference. */
1533 cp_declarator *
1534 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1535 bool rvalue_ref, tree attributes)
1537 cp_declarator *declarator;
1539 declarator = make_declarator (cdk_reference);
1540 declarator->declarator = target;
1541 declarator->u.reference.qualifiers = cv_qualifiers;
1542 declarator->u.reference.rvalue_ref = rvalue_ref;
1543 if (target)
1545 declarator->id_loc = target->id_loc;
1546 declarator->parameter_pack_p = target->parameter_pack_p;
1547 target->parameter_pack_p = false;
1549 else
1550 declarator->parameter_pack_p = false;
1552 declarator->std_attributes = attributes;
1554 return declarator;
1557 /* Like make_pointer_declarator -- but for a pointer to a non-static
1558 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1559 appertain to the pointer or reference. */
1561 cp_declarator *
1562 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1563 cp_declarator *pointee,
1564 tree attributes)
1566 cp_declarator *declarator;
1568 declarator = make_declarator (cdk_ptrmem);
1569 declarator->declarator = pointee;
1570 declarator->u.pointer.qualifiers = cv_qualifiers;
1571 declarator->u.pointer.class_type = class_type;
1573 if (pointee)
1575 declarator->parameter_pack_p = pointee->parameter_pack_p;
1576 pointee->parameter_pack_p = false;
1578 else
1579 declarator->parameter_pack_p = false;
1581 declarator->std_attributes = attributes;
1583 return declarator;
1586 /* Make a declarator for the function given by TARGET, with the
1587 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1588 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1589 indicates what exceptions can be thrown. */
1591 cp_declarator *
1592 make_call_declarator (cp_declarator *target,
1593 tree parms,
1594 cp_cv_quals cv_qualifiers,
1595 cp_virt_specifiers virt_specifiers,
1596 cp_ref_qualifier ref_qualifier,
1597 tree tx_qualifier,
1598 tree exception_specification,
1599 tree late_return_type,
1600 tree requires_clause)
1602 cp_declarator *declarator;
1604 declarator = make_declarator (cdk_function);
1605 declarator->declarator = target;
1606 declarator->u.function.parameters = parms;
1607 declarator->u.function.qualifiers = cv_qualifiers;
1608 declarator->u.function.virt_specifiers = virt_specifiers;
1609 declarator->u.function.ref_qualifier = ref_qualifier;
1610 declarator->u.function.tx_qualifier = tx_qualifier;
1611 declarator->u.function.exception_specification = exception_specification;
1612 declarator->u.function.late_return_type = late_return_type;
1613 declarator->u.function.requires_clause = requires_clause;
1614 if (target)
1616 declarator->id_loc = target->id_loc;
1617 declarator->parameter_pack_p = target->parameter_pack_p;
1618 target->parameter_pack_p = false;
1620 else
1621 declarator->parameter_pack_p = false;
1623 return declarator;
1626 /* Make a declarator for an array of BOUNDS elements, each of which is
1627 defined by ELEMENT. */
1629 cp_declarator *
1630 make_array_declarator (cp_declarator *element, tree bounds)
1632 cp_declarator *declarator;
1634 declarator = make_declarator (cdk_array);
1635 declarator->declarator = element;
1636 declarator->u.array.bounds = bounds;
1637 if (element)
1639 declarator->id_loc = element->id_loc;
1640 declarator->parameter_pack_p = element->parameter_pack_p;
1641 element->parameter_pack_p = false;
1643 else
1644 declarator->parameter_pack_p = false;
1646 return declarator;
1649 /* Determine whether the declarator we've seen so far can be a
1650 parameter pack, when followed by an ellipsis. */
1651 static bool
1652 declarator_can_be_parameter_pack (cp_declarator *declarator)
1654 if (declarator && declarator->parameter_pack_p)
1655 /* We already saw an ellipsis. */
1656 return false;
1658 /* Search for a declarator name, or any other declarator that goes
1659 after the point where the ellipsis could appear in a parameter
1660 pack. If we find any of these, then this declarator can not be
1661 made into a parameter pack. */
1662 bool found = false;
1663 while (declarator && !found)
1665 switch ((int)declarator->kind)
1667 case cdk_id:
1668 case cdk_array:
1669 case cdk_decomp:
1670 found = true;
1671 break;
1673 case cdk_error:
1674 return true;
1676 default:
1677 declarator = declarator->declarator;
1678 break;
1682 return !found;
1685 cp_parameter_declarator *no_parameters;
1687 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1688 DECLARATOR and DEFAULT_ARGUMENT. */
1690 cp_parameter_declarator *
1691 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1692 cp_declarator *declarator,
1693 tree default_argument,
1694 bool template_parameter_pack_p = false)
1696 cp_parameter_declarator *parameter;
1698 parameter = ((cp_parameter_declarator *)
1699 alloc_declarator (sizeof (cp_parameter_declarator)));
1700 parameter->next = NULL;
1701 if (decl_specifiers)
1702 parameter->decl_specifiers = *decl_specifiers;
1703 else
1704 clear_decl_specs (&parameter->decl_specifiers);
1705 parameter->declarator = declarator;
1706 parameter->default_argument = default_argument;
1707 parameter->template_parameter_pack_p = template_parameter_pack_p;
1709 return parameter;
1712 /* Returns true iff DECLARATOR is a declaration for a function. */
1714 static bool
1715 function_declarator_p (const cp_declarator *declarator)
1717 while (declarator)
1719 if (declarator->kind == cdk_function
1720 && declarator->declarator->kind == cdk_id)
1721 return true;
1722 if (declarator->kind == cdk_id
1723 || declarator->kind == cdk_decomp
1724 || declarator->kind == cdk_error)
1725 return false;
1726 declarator = declarator->declarator;
1728 return false;
1731 /* The parser. */
1733 /* Overview
1734 --------
1736 A cp_parser parses the token stream as specified by the C++
1737 grammar. Its job is purely parsing, not semantic analysis. For
1738 example, the parser breaks the token stream into declarators,
1739 expressions, statements, and other similar syntactic constructs.
1740 It does not check that the types of the expressions on either side
1741 of an assignment-statement are compatible, or that a function is
1742 not declared with a parameter of type `void'.
1744 The parser invokes routines elsewhere in the compiler to perform
1745 semantic analysis and to build up the abstract syntax tree for the
1746 code processed.
1748 The parser (and the template instantiation code, which is, in a
1749 way, a close relative of parsing) are the only parts of the
1750 compiler that should be calling push_scope and pop_scope, or
1751 related functions. The parser (and template instantiation code)
1752 keeps track of what scope is presently active; everything else
1753 should simply honor that. (The code that generates static
1754 initializers may also need to set the scope, in order to check
1755 access control correctly when emitting the initializers.)
1757 Methodology
1758 -----------
1760 The parser is of the standard recursive-descent variety. Upcoming
1761 tokens in the token stream are examined in order to determine which
1762 production to use when parsing a non-terminal. Some C++ constructs
1763 require arbitrary look ahead to disambiguate. For example, it is
1764 impossible, in the general case, to tell whether a statement is an
1765 expression or declaration without scanning the entire statement.
1766 Therefore, the parser is capable of "parsing tentatively." When the
1767 parser is not sure what construct comes next, it enters this mode.
1768 Then, while we attempt to parse the construct, the parser queues up
1769 error messages, rather than issuing them immediately, and saves the
1770 tokens it consumes. If the construct is parsed successfully, the
1771 parser "commits", i.e., it issues any queued error messages and
1772 the tokens that were being preserved are permanently discarded.
1773 If, however, the construct is not parsed successfully, the parser
1774 rolls back its state completely so that it can resume parsing using
1775 a different alternative.
1777 Future Improvements
1778 -------------------
1780 The performance of the parser could probably be improved substantially.
1781 We could often eliminate the need to parse tentatively by looking ahead
1782 a little bit. In some places, this approach might not entirely eliminate
1783 the need to parse tentatively, but it might still speed up the average
1784 case. */
1786 /* Flags that are passed to some parsing functions. These values can
1787 be bitwise-ored together. */
1789 enum
1791 /* No flags. */
1792 CP_PARSER_FLAGS_NONE = 0x0,
1793 /* The construct is optional. If it is not present, then no error
1794 should be issued. */
1795 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1796 /* When parsing a type-specifier, treat user-defined type-names
1797 as non-type identifiers. */
1798 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1799 /* When parsing a type-specifier, do not try to parse a class-specifier
1800 or enum-specifier. */
1801 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1802 /* When parsing a decl-specifier-seq, only allow type-specifier or
1803 constexpr. */
1804 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1805 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1806 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1809 /* This type is used for parameters and variables which hold
1810 combinations of the above flags. */
1811 typedef int cp_parser_flags;
1813 /* The different kinds of declarators we want to parse. */
1815 enum cp_parser_declarator_kind
1817 /* We want an abstract declarator. */
1818 CP_PARSER_DECLARATOR_ABSTRACT,
1819 /* We want a named declarator. */
1820 CP_PARSER_DECLARATOR_NAMED,
1821 /* We don't mind, but the name must be an unqualified-id. */
1822 CP_PARSER_DECLARATOR_EITHER
1825 /* The precedence values used to parse binary expressions. The minimum value
1826 of PREC must be 1, because zero is reserved to quickly discriminate
1827 binary operators from other tokens. */
1829 enum cp_parser_prec
1831 PREC_NOT_OPERATOR,
1832 PREC_LOGICAL_OR_EXPRESSION,
1833 PREC_LOGICAL_AND_EXPRESSION,
1834 PREC_INCLUSIVE_OR_EXPRESSION,
1835 PREC_EXCLUSIVE_OR_EXPRESSION,
1836 PREC_AND_EXPRESSION,
1837 PREC_EQUALITY_EXPRESSION,
1838 PREC_RELATIONAL_EXPRESSION,
1839 PREC_SHIFT_EXPRESSION,
1840 PREC_ADDITIVE_EXPRESSION,
1841 PREC_MULTIPLICATIVE_EXPRESSION,
1842 PREC_PM_EXPRESSION,
1843 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1846 /* A mapping from a token type to a corresponding tree node type, with a
1847 precedence value. */
1849 struct cp_parser_binary_operations_map_node
1851 /* The token type. */
1852 enum cpp_ttype token_type;
1853 /* The corresponding tree code. */
1854 enum tree_code tree_type;
1855 /* The precedence of this operator. */
1856 enum cp_parser_prec prec;
1859 struct cp_parser_expression_stack_entry
1861 /* Left hand side of the binary operation we are currently
1862 parsing. */
1863 cp_expr lhs;
1864 /* Original tree code for left hand side, if it was a binary
1865 expression itself (used for -Wparentheses). */
1866 enum tree_code lhs_type;
1867 /* Tree code for the binary operation we are parsing. */
1868 enum tree_code tree_type;
1869 /* Precedence of the binary operation we are parsing. */
1870 enum cp_parser_prec prec;
1871 /* Location of the binary operation we are parsing. */
1872 location_t loc;
1875 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1876 entries because precedence levels on the stack are monotonically
1877 increasing. */
1878 typedef struct cp_parser_expression_stack_entry
1879 cp_parser_expression_stack[NUM_PREC_VALUES];
1881 /* Prototypes. */
1883 /* Constructors and destructors. */
1885 static cp_parser_context *cp_parser_context_new
1886 (cp_parser_context *);
1888 /* Class variables. */
1890 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1892 /* The operator-precedence table used by cp_parser_binary_expression.
1893 Transformed into an associative array (binops_by_token) by
1894 cp_parser_new. */
1896 static const cp_parser_binary_operations_map_node binops[] = {
1897 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1898 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1900 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1901 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1902 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1904 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1905 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1907 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1908 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1910 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1912 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1913 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1915 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1916 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1918 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1920 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1922 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1924 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1926 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1929 /* The same as binops, but initialized by cp_parser_new so that
1930 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1931 for speed. */
1932 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1934 /* Constructors and destructors. */
1936 /* Construct a new context. The context below this one on the stack
1937 is given by NEXT. */
1939 static cp_parser_context *
1940 cp_parser_context_new (cp_parser_context* next)
1942 cp_parser_context *context;
1944 /* Allocate the storage. */
1945 if (cp_parser_context_free_list != NULL)
1947 /* Pull the first entry from the free list. */
1948 context = cp_parser_context_free_list;
1949 cp_parser_context_free_list = context->next;
1950 memset (context, 0, sizeof (*context));
1952 else
1953 context = ggc_cleared_alloc<cp_parser_context> ();
1955 /* No errors have occurred yet in this context. */
1956 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1957 /* If this is not the bottommost context, copy information that we
1958 need from the previous context. */
1959 if (next)
1961 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1962 expression, then we are parsing one in this context, too. */
1963 context->object_type = next->object_type;
1964 /* Thread the stack. */
1965 context->next = next;
1968 return context;
1971 /* Managing the unparsed function queues. */
1973 #define unparsed_funs_with_default_args \
1974 parser->unparsed_queues->last ().funs_with_default_args
1975 #define unparsed_funs_with_definitions \
1976 parser->unparsed_queues->last ().funs_with_definitions
1977 #define unparsed_nsdmis \
1978 parser->unparsed_queues->last ().nsdmis
1979 #define unparsed_classes \
1980 parser->unparsed_queues->last ().classes
1982 static void
1983 push_unparsed_function_queues (cp_parser *parser)
1985 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1986 vec_safe_push (parser->unparsed_queues, e);
1989 static void
1990 pop_unparsed_function_queues (cp_parser *parser)
1992 release_tree_vector (unparsed_funs_with_definitions);
1993 parser->unparsed_queues->pop ();
1996 /* Prototypes. */
1998 /* Constructors and destructors. */
2000 static cp_parser *cp_parser_new
2001 (void);
2003 /* Routines to parse various constructs.
2005 Those that return `tree' will return the error_mark_node (rather
2006 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2007 Sometimes, they will return an ordinary node if error-recovery was
2008 attempted, even though a parse error occurred. So, to check
2009 whether or not a parse error occurred, you should always use
2010 cp_parser_error_occurred. If the construct is optional (indicated
2011 either by an `_opt' in the name of the function that does the
2012 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2013 the construct is not present. */
2015 /* Lexical conventions [gram.lex] */
2017 static cp_expr cp_parser_identifier
2018 (cp_parser *);
2019 static cp_expr cp_parser_string_literal
2020 (cp_parser *, bool, bool, bool);
2021 static cp_expr cp_parser_userdef_char_literal
2022 (cp_parser *);
2023 static tree cp_parser_userdef_string_literal
2024 (tree);
2025 static cp_expr cp_parser_userdef_numeric_literal
2026 (cp_parser *);
2028 /* Basic concepts [gram.basic] */
2030 static bool cp_parser_translation_unit
2031 (cp_parser *);
2033 /* Expressions [gram.expr] */
2035 static cp_expr cp_parser_primary_expression
2036 (cp_parser *, bool, bool, bool, cp_id_kind *);
2037 static cp_expr cp_parser_id_expression
2038 (cp_parser *, bool, bool, bool *, bool, bool);
2039 static cp_expr cp_parser_unqualified_id
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_nested_name_specifier_opt
2042 (cp_parser *, bool, bool, bool, bool);
2043 static tree cp_parser_nested_name_specifier
2044 (cp_parser *, bool, bool, bool, bool);
2045 static tree cp_parser_qualifying_entity
2046 (cp_parser *, bool, bool, bool, bool, bool);
2047 static cp_expr cp_parser_postfix_expression
2048 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2049 static tree cp_parser_postfix_open_square_expression
2050 (cp_parser *, tree, bool, bool);
2051 static tree cp_parser_postfix_dot_deref_expression
2052 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2053 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2054 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2055 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2056 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2057 static void cp_parser_pseudo_destructor_name
2058 (cp_parser *, tree, tree *, tree *);
2059 static cp_expr cp_parser_unary_expression
2060 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2061 static enum tree_code cp_parser_unary_operator
2062 (cp_token *);
2063 static tree cp_parser_new_expression
2064 (cp_parser *);
2065 static vec<tree, va_gc> *cp_parser_new_placement
2066 (cp_parser *);
2067 static tree cp_parser_new_type_id
2068 (cp_parser *, tree *);
2069 static cp_declarator *cp_parser_new_declarator_opt
2070 (cp_parser *);
2071 static cp_declarator *cp_parser_direct_new_declarator
2072 (cp_parser *);
2073 static vec<tree, va_gc> *cp_parser_new_initializer
2074 (cp_parser *);
2075 static tree cp_parser_delete_expression
2076 (cp_parser *);
2077 static cp_expr cp_parser_cast_expression
2078 (cp_parser *, bool, bool, bool, cp_id_kind *);
2079 static cp_expr cp_parser_binary_expression
2080 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2081 static tree cp_parser_question_colon_clause
2082 (cp_parser *, cp_expr);
2083 static cp_expr cp_parser_assignment_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static enum tree_code cp_parser_assignment_operator_opt
2086 (cp_parser *);
2087 static cp_expr cp_parser_expression
2088 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2089 static cp_expr cp_parser_constant_expression
2090 (cp_parser *, bool = false, bool * = NULL);
2091 static cp_expr cp_parser_builtin_offsetof
2092 (cp_parser *);
2093 static cp_expr cp_parser_lambda_expression
2094 (cp_parser *);
2095 static void cp_parser_lambda_introducer
2096 (cp_parser *, tree);
2097 static bool cp_parser_lambda_declarator_opt
2098 (cp_parser *, tree);
2099 static void cp_parser_lambda_body
2100 (cp_parser *, tree);
2102 /* Statements [gram.stmt.stmt] */
2104 static void cp_parser_statement
2105 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2106 static void cp_parser_label_for_labeled_statement
2107 (cp_parser *, tree);
2108 static tree cp_parser_expression_statement
2109 (cp_parser *, tree);
2110 static tree cp_parser_compound_statement
2111 (cp_parser *, tree, int, bool);
2112 static void cp_parser_statement_seq_opt
2113 (cp_parser *, tree);
2114 static tree cp_parser_selection_statement
2115 (cp_parser *, bool *, vec<tree> *);
2116 static tree cp_parser_condition
2117 (cp_parser *);
2118 static tree cp_parser_iteration_statement
2119 (cp_parser *, bool *, bool);
2120 static bool cp_parser_init_statement
2121 (cp_parser *, tree *decl);
2122 static tree cp_parser_for
2123 (cp_parser *, bool);
2124 static tree cp_parser_c_for
2125 (cp_parser *, tree, tree, bool);
2126 static tree cp_parser_range_for
2127 (cp_parser *, tree, tree, tree, bool);
2128 static void do_range_for_auto_deduction
2129 (tree, tree);
2130 static tree cp_parser_perform_range_for_lookup
2131 (tree, tree *, tree *);
2132 static tree cp_parser_range_for_member_function
2133 (tree, tree);
2134 static tree cp_parser_jump_statement
2135 (cp_parser *);
2136 static void cp_parser_declaration_statement
2137 (cp_parser *);
2139 static tree cp_parser_implicitly_scoped_statement
2140 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2141 static void cp_parser_already_scoped_statement
2142 (cp_parser *, bool *, const token_indent_info &);
2144 /* Declarations [gram.dcl.dcl] */
2146 static void cp_parser_declaration_seq_opt
2147 (cp_parser *);
2148 static void cp_parser_declaration
2149 (cp_parser *);
2150 static void cp_parser_block_declaration
2151 (cp_parser *, bool);
2152 static void cp_parser_simple_declaration
2153 (cp_parser *, bool, tree *);
2154 static void cp_parser_decl_specifier_seq
2155 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2156 static tree cp_parser_storage_class_specifier_opt
2157 (cp_parser *);
2158 static tree cp_parser_function_specifier_opt
2159 (cp_parser *, cp_decl_specifier_seq *);
2160 static tree cp_parser_type_specifier
2161 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2162 int *, bool *);
2163 static tree cp_parser_simple_type_specifier
2164 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2165 static tree cp_parser_type_name
2166 (cp_parser *, bool);
2167 static tree cp_parser_type_name
2168 (cp_parser *);
2169 static tree cp_parser_nonclass_name
2170 (cp_parser* parser);
2171 static tree cp_parser_elaborated_type_specifier
2172 (cp_parser *, bool, bool);
2173 static tree cp_parser_enum_specifier
2174 (cp_parser *);
2175 static void cp_parser_enumerator_list
2176 (cp_parser *, tree);
2177 static void cp_parser_enumerator_definition
2178 (cp_parser *, tree);
2179 static tree cp_parser_namespace_name
2180 (cp_parser *);
2181 static void cp_parser_namespace_definition
2182 (cp_parser *);
2183 static void cp_parser_namespace_body
2184 (cp_parser *);
2185 static tree cp_parser_qualified_namespace_specifier
2186 (cp_parser *);
2187 static void cp_parser_namespace_alias_definition
2188 (cp_parser *);
2189 static bool cp_parser_using_declaration
2190 (cp_parser *, bool);
2191 static void cp_parser_using_directive
2192 (cp_parser *);
2193 static tree cp_parser_alias_declaration
2194 (cp_parser *);
2195 static void cp_parser_asm_definition
2196 (cp_parser *);
2197 static void cp_parser_linkage_specification
2198 (cp_parser *);
2199 static void cp_parser_static_assert
2200 (cp_parser *, bool);
2201 static tree cp_parser_decltype
2202 (cp_parser *);
2203 static tree cp_parser_decomposition_declaration
2204 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2206 /* Declarators [gram.dcl.decl] */
2208 static tree cp_parser_init_declarator
2209 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2210 bool, bool, int, bool *, tree *, location_t *, tree *);
2211 static cp_declarator *cp_parser_declarator
2212 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2213 static cp_declarator *cp_parser_direct_declarator
2214 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2215 static enum tree_code cp_parser_ptr_operator
2216 (cp_parser *, tree *, cp_cv_quals *, tree *);
2217 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2218 (cp_parser *);
2219 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2220 (cp_parser *);
2221 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2222 (cp_parser *);
2223 static tree cp_parser_tx_qualifier_opt
2224 (cp_parser *);
2225 static tree cp_parser_late_return_type_opt
2226 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2227 static tree cp_parser_declarator_id
2228 (cp_parser *, bool);
2229 static tree cp_parser_type_id
2230 (cp_parser *);
2231 static tree cp_parser_template_type_arg
2232 (cp_parser *);
2233 static tree cp_parser_trailing_type_id (cp_parser *);
2234 static tree cp_parser_type_id_1
2235 (cp_parser *, bool, bool);
2236 static void cp_parser_type_specifier_seq
2237 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2238 static tree cp_parser_parameter_declaration_clause
2239 (cp_parser *);
2240 static tree cp_parser_parameter_declaration_list
2241 (cp_parser *, bool *);
2242 static cp_parameter_declarator *cp_parser_parameter_declaration
2243 (cp_parser *, bool, bool *);
2244 static tree cp_parser_default_argument
2245 (cp_parser *, bool);
2246 static void cp_parser_function_body
2247 (cp_parser *, bool);
2248 static tree cp_parser_initializer
2249 (cp_parser *, bool *, bool *);
2250 static cp_expr cp_parser_initializer_clause
2251 (cp_parser *, bool *);
2252 static cp_expr cp_parser_braced_list
2253 (cp_parser*, bool*);
2254 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2255 (cp_parser *, bool *);
2257 static bool cp_parser_ctor_initializer_opt_and_function_body
2258 (cp_parser *, bool);
2260 static tree cp_parser_late_parsing_omp_declare_simd
2261 (cp_parser *, tree);
2263 static tree cp_parser_late_parsing_cilk_simd_fn_info
2264 (cp_parser *, tree);
2266 static tree cp_parser_late_parsing_oacc_routine
2267 (cp_parser *, tree);
2269 static tree synthesize_implicit_template_parm
2270 (cp_parser *, tree);
2271 static tree finish_fully_implicit_template
2272 (cp_parser *, tree);
2274 /* Classes [gram.class] */
2276 static tree cp_parser_class_name
2277 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2278 static tree cp_parser_class_specifier
2279 (cp_parser *);
2280 static tree cp_parser_class_head
2281 (cp_parser *, bool *);
2282 static enum tag_types cp_parser_class_key
2283 (cp_parser *);
2284 static void cp_parser_type_parameter_key
2285 (cp_parser* parser);
2286 static void cp_parser_member_specification_opt
2287 (cp_parser *);
2288 static void cp_parser_member_declaration
2289 (cp_parser *);
2290 static tree cp_parser_pure_specifier
2291 (cp_parser *);
2292 static tree cp_parser_constant_initializer
2293 (cp_parser *);
2295 /* Derived classes [gram.class.derived] */
2297 static tree cp_parser_base_clause
2298 (cp_parser *);
2299 static tree cp_parser_base_specifier
2300 (cp_parser *);
2302 /* Special member functions [gram.special] */
2304 static tree cp_parser_conversion_function_id
2305 (cp_parser *);
2306 static tree cp_parser_conversion_type_id
2307 (cp_parser *);
2308 static cp_declarator *cp_parser_conversion_declarator_opt
2309 (cp_parser *);
2310 static bool cp_parser_ctor_initializer_opt
2311 (cp_parser *);
2312 static void cp_parser_mem_initializer_list
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer
2315 (cp_parser *);
2316 static tree cp_parser_mem_initializer_id
2317 (cp_parser *);
2319 /* Overloading [gram.over] */
2321 static cp_expr cp_parser_operator_function_id
2322 (cp_parser *);
2323 static cp_expr cp_parser_operator
2324 (cp_parser *);
2326 /* Templates [gram.temp] */
2328 static void cp_parser_template_declaration
2329 (cp_parser *, bool);
2330 static tree cp_parser_template_parameter_list
2331 (cp_parser *);
2332 static tree cp_parser_template_parameter
2333 (cp_parser *, bool *, bool *);
2334 static tree cp_parser_type_parameter
2335 (cp_parser *, bool *);
2336 static tree cp_parser_template_id
2337 (cp_parser *, bool, bool, enum tag_types, bool);
2338 static tree cp_parser_template_name
2339 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2340 static tree cp_parser_template_argument_list
2341 (cp_parser *);
2342 static tree cp_parser_template_argument
2343 (cp_parser *);
2344 static void cp_parser_explicit_instantiation
2345 (cp_parser *);
2346 static void cp_parser_explicit_specialization
2347 (cp_parser *);
2349 /* Exception handling [gram.exception] */
2351 static tree cp_parser_try_block
2352 (cp_parser *);
2353 static bool cp_parser_function_try_block
2354 (cp_parser *);
2355 static void cp_parser_handler_seq
2356 (cp_parser *);
2357 static void cp_parser_handler
2358 (cp_parser *);
2359 static tree cp_parser_exception_declaration
2360 (cp_parser *);
2361 static tree cp_parser_throw_expression
2362 (cp_parser *);
2363 static tree cp_parser_exception_specification_opt
2364 (cp_parser *);
2365 static tree cp_parser_type_id_list
2366 (cp_parser *);
2368 /* GNU Extensions */
2370 static tree cp_parser_asm_specification_opt
2371 (cp_parser *);
2372 static tree cp_parser_asm_operand_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_clobber_list
2375 (cp_parser *);
2376 static tree cp_parser_asm_label_list
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_gnu_attribute_p
2381 (cp_parser *);
2382 static bool cp_next_tokens_can_be_std_attribute_p
2383 (cp_parser *);
2384 static bool cp_nth_tokens_can_be_std_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_gnu_attribute_p
2387 (cp_parser *, size_t);
2388 static bool cp_nth_tokens_can_be_attribute_p
2389 (cp_parser *, size_t);
2390 static tree cp_parser_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attributes_opt
2393 (cp_parser *);
2394 static tree cp_parser_gnu_attribute_list
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute
2397 (cp_parser *, tree);
2398 static tree cp_parser_std_attribute_spec
2399 (cp_parser *);
2400 static tree cp_parser_std_attribute_spec_seq
2401 (cp_parser *);
2402 static bool cp_parser_extension_opt
2403 (cp_parser *, int *);
2404 static void cp_parser_label_declaration
2405 (cp_parser *);
2407 /* Concept Extensions */
2409 static tree cp_parser_requires_clause
2410 (cp_parser *);
2411 static tree cp_parser_requires_clause_opt
2412 (cp_parser *);
2413 static tree cp_parser_requires_expression
2414 (cp_parser *);
2415 static tree cp_parser_requirement_parameter_list
2416 (cp_parser *);
2417 static tree cp_parser_requirement_body
2418 (cp_parser *);
2419 static tree cp_parser_requirement_list
2420 (cp_parser *);
2421 static tree cp_parser_requirement
2422 (cp_parser *);
2423 static tree cp_parser_simple_requirement
2424 (cp_parser *);
2425 static tree cp_parser_compound_requirement
2426 (cp_parser *);
2427 static tree cp_parser_type_requirement
2428 (cp_parser *);
2429 static tree cp_parser_nested_requirement
2430 (cp_parser *);
2432 /* Transactional Memory Extensions */
2434 static tree cp_parser_transaction
2435 (cp_parser *, cp_token *);
2436 static tree cp_parser_transaction_expression
2437 (cp_parser *, enum rid);
2438 static bool cp_parser_function_transaction
2439 (cp_parser *, enum rid);
2440 static tree cp_parser_transaction_cancel
2441 (cp_parser *);
2443 enum pragma_context {
2444 pragma_external,
2445 pragma_member,
2446 pragma_objc_icode,
2447 pragma_stmt,
2448 pragma_compound
2450 static bool cp_parser_pragma
2451 (cp_parser *, enum pragma_context, bool *);
2453 /* Objective-C++ Productions */
2455 static tree cp_parser_objc_message_receiver
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_args
2458 (cp_parser *);
2459 static tree cp_parser_objc_message_expression
2460 (cp_parser *);
2461 static cp_expr cp_parser_objc_encode_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_defs_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_protocol_expression
2466 (cp_parser *);
2467 static tree cp_parser_objc_selector_expression
2468 (cp_parser *);
2469 static cp_expr cp_parser_objc_expression
2470 (cp_parser *);
2471 static bool cp_parser_objc_selector_p
2472 (enum cpp_ttype);
2473 static tree cp_parser_objc_selector
2474 (cp_parser *);
2475 static tree cp_parser_objc_protocol_refs_opt
2476 (cp_parser *);
2477 static void cp_parser_objc_declaration
2478 (cp_parser *, tree);
2479 static tree cp_parser_objc_statement
2480 (cp_parser *);
2481 static bool cp_parser_objc_valid_prefix_attributes
2482 (cp_parser *, tree *);
2483 static void cp_parser_objc_at_property_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_synthesize_declaration
2486 (cp_parser *) ;
2487 static void cp_parser_objc_at_dynamic_declaration
2488 (cp_parser *) ;
2489 static tree cp_parser_objc_struct_declaration
2490 (cp_parser *) ;
2492 /* Utility Routines */
2494 static cp_expr cp_parser_lookup_name
2495 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2496 static tree cp_parser_lookup_name_simple
2497 (cp_parser *, tree, location_t);
2498 static tree cp_parser_maybe_treat_template_as_class
2499 (tree, bool);
2500 static bool cp_parser_check_declarator_template_parameters
2501 (cp_parser *, cp_declarator *, location_t);
2502 static bool cp_parser_check_template_parameters
2503 (cp_parser *, unsigned, location_t, cp_declarator *);
2504 static cp_expr cp_parser_simple_cast_expression
2505 (cp_parser *);
2506 static tree cp_parser_global_scope_opt
2507 (cp_parser *, bool);
2508 static bool cp_parser_constructor_declarator_p
2509 (cp_parser *, bool);
2510 static tree cp_parser_function_definition_from_specifiers_and_declarator
2511 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2512 static tree cp_parser_function_definition_after_declarator
2513 (cp_parser *, bool);
2514 static bool cp_parser_template_declaration_after_export
2515 (cp_parser *, bool);
2516 static void cp_parser_perform_template_parameter_access_checks
2517 (vec<deferred_access_check, va_gc> *);
2518 static tree cp_parser_single_declaration
2519 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2520 static cp_expr cp_parser_functional_cast
2521 (cp_parser *, tree);
2522 static tree cp_parser_save_member_function_body
2523 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2524 static tree cp_parser_save_nsdmi
2525 (cp_parser *);
2526 static tree cp_parser_enclosed_template_argument_list
2527 (cp_parser *);
2528 static void cp_parser_save_default_args
2529 (cp_parser *, tree);
2530 static void cp_parser_late_parsing_for_member
2531 (cp_parser *, tree);
2532 static tree cp_parser_late_parse_one_default_arg
2533 (cp_parser *, tree, tree, tree);
2534 static void cp_parser_late_parsing_nsdmi
2535 (cp_parser *, tree);
2536 static void cp_parser_late_parsing_default_args
2537 (cp_parser *, tree);
2538 static tree cp_parser_sizeof_operand
2539 (cp_parser *, enum rid);
2540 static tree cp_parser_trait_expr
2541 (cp_parser *, enum rid);
2542 static bool cp_parser_declares_only_class_p
2543 (cp_parser *);
2544 static void cp_parser_set_storage_class
2545 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2546 static void cp_parser_set_decl_spec_type
2547 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2548 static void set_and_check_decl_spec_loc
2549 (cp_decl_specifier_seq *decl_specs,
2550 cp_decl_spec ds, cp_token *);
2551 static bool cp_parser_friend_p
2552 (const cp_decl_specifier_seq *);
2553 static void cp_parser_required_error
2554 (cp_parser *, required_token, bool);
2555 static cp_token *cp_parser_require
2556 (cp_parser *, enum cpp_ttype, required_token);
2557 static cp_token *cp_parser_require_keyword
2558 (cp_parser *, enum rid, required_token);
2559 static bool cp_parser_token_starts_function_definition_p
2560 (cp_token *);
2561 static bool cp_parser_next_token_starts_class_definition_p
2562 (cp_parser *);
2563 static bool cp_parser_next_token_ends_template_argument_p
2564 (cp_parser *);
2565 static bool cp_parser_nth_token_starts_template_argument_list_p
2566 (cp_parser *, size_t);
2567 static enum tag_types cp_parser_token_is_class_key
2568 (cp_token *);
2569 static enum tag_types cp_parser_token_is_type_parameter_key
2570 (cp_token *);
2571 static void cp_parser_check_class_key
2572 (enum tag_types, tree type);
2573 static void cp_parser_check_access_in_redeclaration
2574 (tree type, location_t location);
2575 static bool cp_parser_optional_template_keyword
2576 (cp_parser *);
2577 static void cp_parser_pre_parsed_nested_name_specifier
2578 (cp_parser *);
2579 static bool cp_parser_cache_group
2580 (cp_parser *, enum cpp_ttype, unsigned);
2581 static tree cp_parser_cache_defarg
2582 (cp_parser *parser, bool nsdmi);
2583 static void cp_parser_parse_tentatively
2584 (cp_parser *);
2585 static void cp_parser_commit_to_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_commit_to_topmost_tentative_parse
2588 (cp_parser *);
2589 static void cp_parser_abort_tentative_parse
2590 (cp_parser *);
2591 static bool cp_parser_parse_definitely
2592 (cp_parser *);
2593 static inline bool cp_parser_parsing_tentatively
2594 (cp_parser *);
2595 static bool cp_parser_uncommitted_to_tentative_parse_p
2596 (cp_parser *);
2597 static void cp_parser_error
2598 (cp_parser *, const char *);
2599 static void cp_parser_name_lookup_error
2600 (cp_parser *, tree, tree, name_lookup_error, location_t);
2601 static bool cp_parser_simulate_error
2602 (cp_parser *);
2603 static bool cp_parser_check_type_definition
2604 (cp_parser *);
2605 static void cp_parser_check_for_definition_in_return_type
2606 (cp_declarator *, tree, location_t type_location);
2607 static void cp_parser_check_for_invalid_template_id
2608 (cp_parser *, tree, enum tag_types, location_t location);
2609 static bool cp_parser_non_integral_constant_expression
2610 (cp_parser *, non_integral_constant);
2611 static void cp_parser_diagnose_invalid_type_name
2612 (cp_parser *, tree, location_t);
2613 static bool cp_parser_parse_and_diagnose_invalid_type_name
2614 (cp_parser *);
2615 static int cp_parser_skip_to_closing_parenthesis
2616 (cp_parser *, bool, bool, bool);
2617 static void cp_parser_skip_to_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_consume_semicolon_at_end_of_statement
2620 (cp_parser *);
2621 static void cp_parser_skip_to_end_of_block_or_statement
2622 (cp_parser *);
2623 static bool cp_parser_skip_to_closing_brace
2624 (cp_parser *);
2625 static void cp_parser_skip_to_end_of_template_parameter_list
2626 (cp_parser *);
2627 static void cp_parser_skip_to_pragma_eol
2628 (cp_parser*, cp_token *);
2629 static bool cp_parser_error_occurred
2630 (cp_parser *);
2631 static bool cp_parser_allow_gnu_extensions_p
2632 (cp_parser *);
2633 static bool cp_parser_is_pure_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_string_literal
2636 (cp_token *);
2637 static bool cp_parser_is_keyword
2638 (cp_token *, enum rid);
2639 static tree cp_parser_make_typename_type
2640 (cp_parser *, tree, location_t location);
2641 static cp_declarator * cp_parser_make_indirect_declarator
2642 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2643 static bool cp_parser_compound_literal_p
2644 (cp_parser *);
2645 static bool cp_parser_array_designator_p
2646 (cp_parser *);
2647 static bool cp_parser_init_statement_p
2648 (cp_parser *);
2649 static bool cp_parser_skip_to_closing_square_bracket
2650 (cp_parser *);
2652 /* Concept-related syntactic transformations */
2654 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2655 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2657 // -------------------------------------------------------------------------- //
2658 // Unevaluated Operand Guard
2660 // Implementation of an RAII helper for unevaluated operand parsing.
2661 cp_unevaluated::cp_unevaluated ()
2663 ++cp_unevaluated_operand;
2664 ++c_inhibit_evaluation_warnings;
2667 cp_unevaluated::~cp_unevaluated ()
2669 --c_inhibit_evaluation_warnings;
2670 --cp_unevaluated_operand;
2673 // -------------------------------------------------------------------------- //
2674 // Tentative Parsing
2676 /* Returns nonzero if we are parsing tentatively. */
2678 static inline bool
2679 cp_parser_parsing_tentatively (cp_parser* parser)
2681 return parser->context->next != NULL;
2684 /* Returns nonzero if TOKEN is a string literal. */
2686 static bool
2687 cp_parser_is_pure_string_literal (cp_token* token)
2689 return (token->type == CPP_STRING ||
2690 token->type == CPP_STRING16 ||
2691 token->type == CPP_STRING32 ||
2692 token->type == CPP_WSTRING ||
2693 token->type == CPP_UTF8STRING);
2696 /* Returns nonzero if TOKEN is a string literal
2697 of a user-defined string literal. */
2699 static bool
2700 cp_parser_is_string_literal (cp_token* token)
2702 return (cp_parser_is_pure_string_literal (token) ||
2703 token->type == CPP_STRING_USERDEF ||
2704 token->type == CPP_STRING16_USERDEF ||
2705 token->type == CPP_STRING32_USERDEF ||
2706 token->type == CPP_WSTRING_USERDEF ||
2707 token->type == CPP_UTF8STRING_USERDEF);
2710 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2712 static bool
2713 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2715 return token->keyword == keyword;
2718 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 PRAGMA_NONE. */
2721 static enum pragma_kind
2722 cp_parser_pragma_kind (cp_token *token)
2724 if (token->type != CPP_PRAGMA)
2725 return PRAGMA_NONE;
2726 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2727 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2730 /* Helper function for cp_parser_error.
2731 Having peeked a token of kind TOK1_KIND that might signify
2732 a conflict marker, peek successor tokens to determine
2733 if we actually do have a conflict marker.
2734 Specifically, we consider a run of 7 '<', '=' or '>' characters
2735 at the start of a line as a conflict marker.
2736 These come through the lexer as three pairs and a single,
2737 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2738 If it returns true, *OUT_LOC is written to with the location/range
2739 of the marker. */
2741 static bool
2742 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2743 location_t *out_loc)
2745 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2746 if (token2->type != tok1_kind)
2747 return false;
2748 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2749 if (token3->type != tok1_kind)
2750 return false;
2751 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2752 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2753 return false;
2755 /* It must be at the start of the line. */
2756 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2757 if (LOCATION_COLUMN (start_loc) != 1)
2758 return false;
2760 /* We have a conflict marker. Construct a location of the form:
2761 <<<<<<<
2762 ^~~~~~~
2763 with start == caret, finishing at the end of the marker. */
2764 location_t finish_loc = get_finish (token4->location);
2765 *out_loc = make_location (start_loc, start_loc, finish_loc);
2767 return true;
2770 /* If not parsing tentatively, issue a diagnostic of the form
2771 FILE:LINE: MESSAGE before TOKEN
2772 where TOKEN is the next token in the input stream. MESSAGE
2773 (specified by the caller) is usually of the form "expected
2774 OTHER-TOKEN". */
2776 static void
2777 cp_parser_error (cp_parser* parser, const char* gmsgid)
2779 if (!cp_parser_simulate_error (parser))
2781 cp_token *token = cp_lexer_peek_token (parser->lexer);
2782 /* This diagnostic makes more sense if it is tagged to the line
2783 of the token we just peeked at. */
2784 cp_lexer_set_source_position_from_token (token);
2786 if (token->type == CPP_PRAGMA)
2788 error_at (token->location,
2789 "%<#pragma%> is not allowed here");
2790 cp_parser_skip_to_pragma_eol (parser, token);
2791 return;
2794 /* If this is actually a conflict marker, report it as such. */
2795 if (token->type == CPP_LSHIFT
2796 || token->type == CPP_RSHIFT
2797 || token->type == CPP_EQ_EQ)
2799 location_t loc;
2800 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2802 error_at (loc, "version control conflict marker in file");
2803 return;
2807 c_parse_error (gmsgid,
2808 /* Because c_parser_error does not understand
2809 CPP_KEYWORD, keywords are treated like
2810 identifiers. */
2811 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2812 token->u.value, token->flags);
2816 /* Issue an error about name-lookup failing. NAME is the
2817 IDENTIFIER_NODE DECL is the result of
2818 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2819 the thing that we hoped to find. */
2821 static void
2822 cp_parser_name_lookup_error (cp_parser* parser,
2823 tree name,
2824 tree decl,
2825 name_lookup_error desired,
2826 location_t location)
2828 /* If name lookup completely failed, tell the user that NAME was not
2829 declared. */
2830 if (decl == error_mark_node)
2832 if (parser->scope && parser->scope != global_namespace)
2833 error_at (location, "%<%E::%E%> has not been declared",
2834 parser->scope, name);
2835 else if (parser->scope == global_namespace)
2836 error_at (location, "%<::%E%> has not been declared", name);
2837 else if (parser->object_scope
2838 && !CLASS_TYPE_P (parser->object_scope))
2839 error_at (location, "request for member %qE in non-class type %qT",
2840 name, parser->object_scope);
2841 else if (parser->object_scope)
2842 error_at (location, "%<%T::%E%> has not been declared",
2843 parser->object_scope, name);
2844 else
2845 error_at (location, "%qE has not been declared", name);
2847 else if (parser->scope && parser->scope != global_namespace)
2849 switch (desired)
2851 case NLE_TYPE:
2852 error_at (location, "%<%E::%E%> is not a type",
2853 parser->scope, name);
2854 break;
2855 case NLE_CXX98:
2856 error_at (location, "%<%E::%E%> is not a class or namespace",
2857 parser->scope, name);
2858 break;
2859 case NLE_NOT_CXX98:
2860 error_at (location,
2861 "%<%E::%E%> is not a class, namespace, or enumeration",
2862 parser->scope, name);
2863 break;
2864 default:
2865 gcc_unreachable ();
2869 else if (parser->scope == global_namespace)
2871 switch (desired)
2873 case NLE_TYPE:
2874 error_at (location, "%<::%E%> is not a type", name);
2875 break;
2876 case NLE_CXX98:
2877 error_at (location, "%<::%E%> is not a class or namespace", name);
2878 break;
2879 case NLE_NOT_CXX98:
2880 error_at (location,
2881 "%<::%E%> is not a class, namespace, or enumeration",
2882 name);
2883 break;
2884 default:
2885 gcc_unreachable ();
2888 else
2890 switch (desired)
2892 case NLE_TYPE:
2893 error_at (location, "%qE is not a type", name);
2894 break;
2895 case NLE_CXX98:
2896 error_at (location, "%qE is not a class or namespace", name);
2897 break;
2898 case NLE_NOT_CXX98:
2899 error_at (location,
2900 "%qE is not a class, namespace, or enumeration", name);
2901 break;
2902 default:
2903 gcc_unreachable ();
2908 /* If we are parsing tentatively, remember that an error has occurred
2909 during this tentative parse. Returns true if the error was
2910 simulated; false if a message should be issued by the caller. */
2912 static bool
2913 cp_parser_simulate_error (cp_parser* parser)
2915 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2917 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2918 return true;
2920 return false;
2923 /* This function is called when a type is defined. If type
2924 definitions are forbidden at this point, an error message is
2925 issued. */
2927 static bool
2928 cp_parser_check_type_definition (cp_parser* parser)
2930 /* If types are forbidden here, issue a message. */
2931 if (parser->type_definition_forbidden_message)
2933 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2934 in the message need to be interpreted. */
2935 error (parser->type_definition_forbidden_message);
2936 return false;
2938 return true;
2941 /* This function is called when the DECLARATOR is processed. The TYPE
2942 was a type defined in the decl-specifiers. If it is invalid to
2943 define a type in the decl-specifiers for DECLARATOR, an error is
2944 issued. TYPE_LOCATION is the location of TYPE and is used
2945 for error reporting. */
2947 static void
2948 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2949 tree type, location_t type_location)
2951 /* [dcl.fct] forbids type definitions in return types.
2952 Unfortunately, it's not easy to know whether or not we are
2953 processing a return type until after the fact. */
2954 while (declarator
2955 && (declarator->kind == cdk_pointer
2956 || declarator->kind == cdk_reference
2957 || declarator->kind == cdk_ptrmem))
2958 declarator = declarator->declarator;
2959 if (declarator
2960 && declarator->kind == cdk_function)
2962 error_at (type_location,
2963 "new types may not be defined in a return type");
2964 inform (type_location,
2965 "(perhaps a semicolon is missing after the definition of %qT)",
2966 type);
2970 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2971 "<" in any valid C++ program. If the next token is indeed "<",
2972 issue a message warning the user about what appears to be an
2973 invalid attempt to form a template-id. LOCATION is the location
2974 of the type-specifier (TYPE) */
2976 static void
2977 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2978 tree type,
2979 enum tag_types tag_type,
2980 location_t location)
2982 cp_token_position start = 0;
2984 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2986 if (TYPE_P (type))
2987 error_at (location, "%qT is not a template", type);
2988 else if (identifier_p (type))
2990 if (tag_type != none_type)
2991 error_at (location, "%qE is not a class template", type);
2992 else
2993 error_at (location, "%qE is not a template", type);
2995 else
2996 error_at (location, "invalid template-id");
2997 /* Remember the location of the invalid "<". */
2998 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2999 start = cp_lexer_token_position (parser->lexer, true);
3000 /* Consume the "<". */
3001 cp_lexer_consume_token (parser->lexer);
3002 /* Parse the template arguments. */
3003 cp_parser_enclosed_template_argument_list (parser);
3004 /* Permanently remove the invalid template arguments so that
3005 this error message is not issued again. */
3006 if (start)
3007 cp_lexer_purge_tokens_after (parser->lexer, start);
3011 /* If parsing an integral constant-expression, issue an error message
3012 about the fact that THING appeared and return true. Otherwise,
3013 return false. In either case, set
3014 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3016 static bool
3017 cp_parser_non_integral_constant_expression (cp_parser *parser,
3018 non_integral_constant thing)
3020 parser->non_integral_constant_expression_p = true;
3021 if (parser->integral_constant_expression_p)
3023 if (!parser->allow_non_integral_constant_expression_p)
3025 const char *msg = NULL;
3026 switch (thing)
3028 case NIC_FLOAT:
3029 pedwarn (input_location, OPT_Wpedantic,
3030 "ISO C++ forbids using a floating-point literal "
3031 "in a constant-expression");
3032 return true;
3033 case NIC_CAST:
3034 error ("a cast to a type other than an integral or "
3035 "enumeration type cannot appear in a "
3036 "constant-expression");
3037 return true;
3038 case NIC_TYPEID:
3039 error ("%<typeid%> operator "
3040 "cannot appear in a constant-expression");
3041 return true;
3042 case NIC_NCC:
3043 error ("non-constant compound literals "
3044 "cannot appear in a constant-expression");
3045 return true;
3046 case NIC_FUNC_CALL:
3047 error ("a function call "
3048 "cannot appear in a constant-expression");
3049 return true;
3050 case NIC_INC:
3051 error ("an increment "
3052 "cannot appear in a constant-expression");
3053 return true;
3054 case NIC_DEC:
3055 error ("an decrement "
3056 "cannot appear in a constant-expression");
3057 return true;
3058 case NIC_ARRAY_REF:
3059 error ("an array reference "
3060 "cannot appear in a constant-expression");
3061 return true;
3062 case NIC_ADDR_LABEL:
3063 error ("the address of a label "
3064 "cannot appear in a constant-expression");
3065 return true;
3066 case NIC_OVERLOADED:
3067 error ("calls to overloaded operators "
3068 "cannot appear in a constant-expression");
3069 return true;
3070 case NIC_ASSIGNMENT:
3071 error ("an assignment cannot appear in a constant-expression");
3072 return true;
3073 case NIC_COMMA:
3074 error ("a comma operator "
3075 "cannot appear in a constant-expression");
3076 return true;
3077 case NIC_CONSTRUCTOR:
3078 error ("a call to a constructor "
3079 "cannot appear in a constant-expression");
3080 return true;
3081 case NIC_TRANSACTION:
3082 error ("a transaction expression "
3083 "cannot appear in a constant-expression");
3084 return true;
3085 case NIC_THIS:
3086 msg = "this";
3087 break;
3088 case NIC_FUNC_NAME:
3089 msg = "__FUNCTION__";
3090 break;
3091 case NIC_PRETTY_FUNC:
3092 msg = "__PRETTY_FUNCTION__";
3093 break;
3094 case NIC_C99_FUNC:
3095 msg = "__func__";
3096 break;
3097 case NIC_VA_ARG:
3098 msg = "va_arg";
3099 break;
3100 case NIC_ARROW:
3101 msg = "->";
3102 break;
3103 case NIC_POINT:
3104 msg = ".";
3105 break;
3106 case NIC_STAR:
3107 msg = "*";
3108 break;
3109 case NIC_ADDR:
3110 msg = "&";
3111 break;
3112 case NIC_PREINCREMENT:
3113 msg = "++";
3114 break;
3115 case NIC_PREDECREMENT:
3116 msg = "--";
3117 break;
3118 case NIC_NEW:
3119 msg = "new";
3120 break;
3121 case NIC_DEL:
3122 msg = "delete";
3123 break;
3124 default:
3125 gcc_unreachable ();
3127 if (msg)
3128 error ("%qs cannot appear in a constant-expression", msg);
3129 return true;
3132 return false;
3135 /* Emit a diagnostic for an invalid type name. This function commits
3136 to the current active tentative parse, if any. (Otherwise, the
3137 problematic construct might be encountered again later, resulting
3138 in duplicate error messages.) LOCATION is the location of ID. */
3140 static void
3141 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3142 location_t location)
3144 tree decl, ambiguous_decls;
3145 cp_parser_commit_to_tentative_parse (parser);
3146 /* Try to lookup the identifier. */
3147 decl = cp_parser_lookup_name (parser, id, none_type,
3148 /*is_template=*/false,
3149 /*is_namespace=*/false,
3150 /*check_dependency=*/true,
3151 &ambiguous_decls, location);
3152 if (ambiguous_decls)
3153 /* If the lookup was ambiguous, an error will already have
3154 been issued. */
3155 return;
3156 /* If the lookup found a template-name, it means that the user forgot
3157 to specify an argument list. Emit a useful error message. */
3158 if (DECL_TYPE_TEMPLATE_P (decl))
3160 error_at (location,
3161 "invalid use of template-name %qE without an argument list",
3162 decl);
3163 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx1z)
3164 inform (location, "class template argument deduction is only available "
3165 "with -std=c++1z or -std=gnu++1z");
3166 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3168 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3169 error_at (location, "invalid use of destructor %qD as a type", id);
3170 else if (TREE_CODE (decl) == TYPE_DECL)
3171 /* Something like 'unsigned A a;' */
3172 error_at (location, "invalid combination of multiple type-specifiers");
3173 else if (!parser->scope)
3175 /* Issue an error message. */
3176 const char *suggestion = NULL;
3177 if (TREE_CODE (id) == IDENTIFIER_NODE)
3178 suggestion = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME);
3179 if (suggestion)
3181 gcc_rich_location richloc (location);
3182 richloc.add_fixit_replace (suggestion);
3183 error_at_rich_loc (&richloc,
3184 "%qE does not name a type; did you mean %qs?",
3185 id, suggestion);
3187 else
3188 error_at (location, "%qE does not name a type", id);
3189 /* If we're in a template class, it's possible that the user was
3190 referring to a type from a base class. For example:
3192 template <typename T> struct A { typedef T X; };
3193 template <typename T> struct B : public A<T> { X x; };
3195 The user should have said "typename A<T>::X". */
3196 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3197 inform (location, "C++11 %<constexpr%> only available with "
3198 "-std=c++11 or -std=gnu++11");
3199 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3200 inform (location, "C++11 %<noexcept%> only available with "
3201 "-std=c++11 or -std=gnu++11");
3202 else if (cxx_dialect < cxx11
3203 && TREE_CODE (id) == IDENTIFIER_NODE
3204 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3205 inform (location, "C++11 %<thread_local%> only available with "
3206 "-std=c++11 or -std=gnu++11");
3207 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3208 inform (location, "%<concept%> only available with -fconcepts");
3209 else if (processing_template_decl && current_class_type
3210 && TYPE_BINFO (current_class_type))
3212 tree b;
3214 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3216 b = TREE_CHAIN (b))
3218 tree base_type = BINFO_TYPE (b);
3219 if (CLASS_TYPE_P (base_type)
3220 && dependent_type_p (base_type))
3222 tree field;
3223 /* Go from a particular instantiation of the
3224 template (which will have an empty TYPE_FIELDs),
3225 to the main version. */
3226 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3227 for (field = TYPE_FIELDS (base_type);
3228 field;
3229 field = DECL_CHAIN (field))
3230 if (TREE_CODE (field) == TYPE_DECL
3231 && DECL_NAME (field) == id)
3233 inform (location,
3234 "(perhaps %<typename %T::%E%> was intended)",
3235 BINFO_TYPE (b), id);
3236 break;
3238 if (field)
3239 break;
3244 /* Here we diagnose qualified-ids where the scope is actually correct,
3245 but the identifier does not resolve to a valid type name. */
3246 else if (parser->scope != error_mark_node)
3248 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3250 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3251 error_at (location_of (id),
3252 "%qE in namespace %qE does not name a template type",
3253 id, parser->scope);
3254 else
3255 error_at (location_of (id),
3256 "%qE in namespace %qE does not name a type",
3257 id, parser->scope);
3258 if (DECL_P (decl))
3259 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3261 else if (CLASS_TYPE_P (parser->scope)
3262 && constructor_name_p (id, parser->scope))
3264 /* A<T>::A<T>() */
3265 error_at (location, "%<%T::%E%> names the constructor, not"
3266 " the type", parser->scope, id);
3267 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3268 error_at (location, "and %qT has no template constructors",
3269 parser->scope);
3271 else if (TYPE_P (parser->scope)
3272 && dependent_scope_p (parser->scope))
3273 error_at (location, "need %<typename%> before %<%T::%E%> because "
3274 "%qT is a dependent scope",
3275 parser->scope, id, parser->scope);
3276 else if (TYPE_P (parser->scope))
3278 if (!COMPLETE_TYPE_P (parser->scope))
3279 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3280 parser->scope);
3281 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3282 error_at (location_of (id),
3283 "%qE in %q#T does not name a template type",
3284 id, parser->scope);
3285 else
3286 error_at (location_of (id),
3287 "%qE in %q#T does not name a type",
3288 id, parser->scope);
3289 if (DECL_P (decl))
3290 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3292 else
3293 gcc_unreachable ();
3297 /* Check for a common situation where a type-name should be present,
3298 but is not, and issue a sensible error message. Returns true if an
3299 invalid type-name was detected.
3301 The situation handled by this function are variable declarations of the
3302 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3303 Usually, `ID' should name a type, but if we got here it means that it
3304 does not. We try to emit the best possible error message depending on
3305 how exactly the id-expression looks like. */
3307 static bool
3308 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3310 tree id;
3311 cp_token *token = cp_lexer_peek_token (parser->lexer);
3313 /* Avoid duplicate error about ambiguous lookup. */
3314 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3316 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3317 if (next->type == CPP_NAME && next->error_reported)
3318 goto out;
3321 cp_parser_parse_tentatively (parser);
3322 id = cp_parser_id_expression (parser,
3323 /*template_keyword_p=*/false,
3324 /*check_dependency_p=*/true,
3325 /*template_p=*/NULL,
3326 /*declarator_p=*/true,
3327 /*optional_p=*/false);
3328 /* If the next token is a (, this is a function with no explicit return
3329 type, i.e. constructor, destructor or conversion op. */
3330 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3331 || TREE_CODE (id) == TYPE_DECL)
3333 cp_parser_abort_tentative_parse (parser);
3334 return false;
3336 if (!cp_parser_parse_definitely (parser))
3337 return false;
3339 /* Emit a diagnostic for the invalid type. */
3340 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3341 out:
3342 /* If we aren't in the middle of a declarator (i.e. in a
3343 parameter-declaration-clause), skip to the end of the declaration;
3344 there's no point in trying to process it. */
3345 if (!parser->in_declarator_p)
3346 cp_parser_skip_to_end_of_block_or_statement (parser);
3347 return true;
3350 /* Consume tokens up to, and including, the next non-nested closing `)'.
3351 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3352 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3353 found an unnested token of that type. */
3355 static int
3356 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3357 bool recovering,
3358 cpp_ttype or_ttype,
3359 bool consume_paren)
3361 unsigned paren_depth = 0;
3362 unsigned brace_depth = 0;
3363 unsigned square_depth = 0;
3365 if (recovering && or_ttype == CPP_EOF
3366 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3367 return 0;
3369 while (true)
3371 cp_token * token = cp_lexer_peek_token (parser->lexer);
3373 /* Have we found what we're looking for before the closing paren? */
3374 if (token->type == or_ttype && or_ttype != CPP_EOF
3375 && !brace_depth && !paren_depth && !square_depth)
3376 return -1;
3378 switch (token->type)
3380 case CPP_EOF:
3381 case CPP_PRAGMA_EOL:
3382 /* If we've run out of tokens, then there is no closing `)'. */
3383 return 0;
3385 /* This is good for lambda expression capture-lists. */
3386 case CPP_OPEN_SQUARE:
3387 ++square_depth;
3388 break;
3389 case CPP_CLOSE_SQUARE:
3390 if (!square_depth--)
3391 return 0;
3392 break;
3394 case CPP_SEMICOLON:
3395 /* This matches the processing in skip_to_end_of_statement. */
3396 if (!brace_depth)
3397 return 0;
3398 break;
3400 case CPP_OPEN_BRACE:
3401 ++brace_depth;
3402 break;
3403 case CPP_CLOSE_BRACE:
3404 if (!brace_depth--)
3405 return 0;
3406 break;
3408 case CPP_OPEN_PAREN:
3409 if (!brace_depth)
3410 ++paren_depth;
3411 break;
3413 case CPP_CLOSE_PAREN:
3414 if (!brace_depth && !paren_depth--)
3416 if (consume_paren)
3417 cp_lexer_consume_token (parser->lexer);
3418 return 1;
3420 break;
3422 default:
3423 break;
3426 /* Consume the token. */
3427 cp_lexer_consume_token (parser->lexer);
3431 /* Consume tokens up to, and including, the next non-nested closing `)'.
3432 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3433 are doing error recovery. Returns -1 if OR_COMMA is true and we
3434 found an unnested token of that type. */
3436 static int
3437 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3438 bool recovering,
3439 bool or_comma,
3440 bool consume_paren)
3442 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3443 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3444 ttype, consume_paren);
3447 /* Consume tokens until we reach the end of the current statement.
3448 Normally, that will be just before consuming a `;'. However, if a
3449 non-nested `}' comes first, then we stop before consuming that. */
3451 static void
3452 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3454 unsigned nesting_depth = 0;
3456 /* Unwind generic function template scope if necessary. */
3457 if (parser->fully_implicit_function_template_p)
3458 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3460 while (true)
3462 cp_token *token = cp_lexer_peek_token (parser->lexer);
3464 switch (token->type)
3466 case CPP_EOF:
3467 case CPP_PRAGMA_EOL:
3468 /* If we've run out of tokens, stop. */
3469 return;
3471 case CPP_SEMICOLON:
3472 /* If the next token is a `;', we have reached the end of the
3473 statement. */
3474 if (!nesting_depth)
3475 return;
3476 break;
3478 case CPP_CLOSE_BRACE:
3479 /* If this is a non-nested '}', stop before consuming it.
3480 That way, when confronted with something like:
3482 { 3 + }
3484 we stop before consuming the closing '}', even though we
3485 have not yet reached a `;'. */
3486 if (nesting_depth == 0)
3487 return;
3489 /* If it is the closing '}' for a block that we have
3490 scanned, stop -- but only after consuming the token.
3491 That way given:
3493 void f g () { ... }
3494 typedef int I;
3496 we will stop after the body of the erroneously declared
3497 function, but before consuming the following `typedef'
3498 declaration. */
3499 if (--nesting_depth == 0)
3501 cp_lexer_consume_token (parser->lexer);
3502 return;
3504 break;
3506 case CPP_OPEN_BRACE:
3507 ++nesting_depth;
3508 break;
3510 default:
3511 break;
3514 /* Consume the token. */
3515 cp_lexer_consume_token (parser->lexer);
3519 /* This function is called at the end of a statement or declaration.
3520 If the next token is a semicolon, it is consumed; otherwise, error
3521 recovery is attempted. */
3523 static void
3524 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3526 /* Look for the trailing `;'. */
3527 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3529 /* If there is additional (erroneous) input, skip to the end of
3530 the statement. */
3531 cp_parser_skip_to_end_of_statement (parser);
3532 /* If the next token is now a `;', consume it. */
3533 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3534 cp_lexer_consume_token (parser->lexer);
3538 /* Skip tokens until we have consumed an entire block, or until we
3539 have consumed a non-nested `;'. */
3541 static void
3542 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3544 int nesting_depth = 0;
3546 /* Unwind generic function template scope if necessary. */
3547 if (parser->fully_implicit_function_template_p)
3548 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3550 while (nesting_depth >= 0)
3552 cp_token *token = cp_lexer_peek_token (parser->lexer);
3554 switch (token->type)
3556 case CPP_EOF:
3557 case CPP_PRAGMA_EOL:
3558 /* If we've run out of tokens, stop. */
3559 return;
3561 case CPP_SEMICOLON:
3562 /* Stop if this is an unnested ';'. */
3563 if (!nesting_depth)
3564 nesting_depth = -1;
3565 break;
3567 case CPP_CLOSE_BRACE:
3568 /* Stop if this is an unnested '}', or closes the outermost
3569 nesting level. */
3570 nesting_depth--;
3571 if (nesting_depth < 0)
3572 return;
3573 if (!nesting_depth)
3574 nesting_depth = -1;
3575 break;
3577 case CPP_OPEN_BRACE:
3578 /* Nest. */
3579 nesting_depth++;
3580 break;
3582 default:
3583 break;
3586 /* Consume the token. */
3587 cp_lexer_consume_token (parser->lexer);
3591 /* Skip tokens until a non-nested closing curly brace is the next
3592 token, or there are no more tokens. Return true in the first case,
3593 false otherwise. */
3595 static bool
3596 cp_parser_skip_to_closing_brace (cp_parser *parser)
3598 unsigned nesting_depth = 0;
3600 while (true)
3602 cp_token *token = cp_lexer_peek_token (parser->lexer);
3604 switch (token->type)
3606 case CPP_EOF:
3607 case CPP_PRAGMA_EOL:
3608 /* If we've run out of tokens, stop. */
3609 return false;
3611 case CPP_CLOSE_BRACE:
3612 /* If the next token is a non-nested `}', then we have reached
3613 the end of the current block. */
3614 if (nesting_depth-- == 0)
3615 return true;
3616 break;
3618 case CPP_OPEN_BRACE:
3619 /* If it the next token is a `{', then we are entering a new
3620 block. Consume the entire block. */
3621 ++nesting_depth;
3622 break;
3624 default:
3625 break;
3628 /* Consume the token. */
3629 cp_lexer_consume_token (parser->lexer);
3633 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3634 parameter is the PRAGMA token, allowing us to purge the entire pragma
3635 sequence. */
3637 static void
3638 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3640 cp_token *token;
3642 parser->lexer->in_pragma = false;
3645 token = cp_lexer_consume_token (parser->lexer);
3646 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3648 /* Ensure that the pragma is not parsed again. */
3649 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3652 /* Require pragma end of line, resyncing with it as necessary. The
3653 arguments are as for cp_parser_skip_to_pragma_eol. */
3655 static void
3656 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3658 parser->lexer->in_pragma = false;
3659 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3660 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3663 /* This is a simple wrapper around make_typename_type. When the id is
3664 an unresolved identifier node, we can provide a superior diagnostic
3665 using cp_parser_diagnose_invalid_type_name. */
3667 static tree
3668 cp_parser_make_typename_type (cp_parser *parser, tree id,
3669 location_t id_location)
3671 tree result;
3672 if (identifier_p (id))
3674 result = make_typename_type (parser->scope, id, typename_type,
3675 /*complain=*/tf_none);
3676 if (result == error_mark_node)
3677 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3678 return result;
3680 return make_typename_type (parser->scope, id, typename_type, tf_error);
3683 /* This is a wrapper around the
3684 make_{pointer,ptrmem,reference}_declarator functions that decides
3685 which one to call based on the CODE and CLASS_TYPE arguments. The
3686 CODE argument should be one of the values returned by
3687 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3688 appertain to the pointer or reference. */
3690 static cp_declarator *
3691 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3692 cp_cv_quals cv_qualifiers,
3693 cp_declarator *target,
3694 tree attributes)
3696 if (code == ERROR_MARK)
3697 return cp_error_declarator;
3699 if (code == INDIRECT_REF)
3700 if (class_type == NULL_TREE)
3701 return make_pointer_declarator (cv_qualifiers, target, attributes);
3702 else
3703 return make_ptrmem_declarator (cv_qualifiers, class_type,
3704 target, attributes);
3705 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3706 return make_reference_declarator (cv_qualifiers, target,
3707 false, attributes);
3708 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3709 return make_reference_declarator (cv_qualifiers, target,
3710 true, attributes);
3711 gcc_unreachable ();
3714 /* Create a new C++ parser. */
3716 static cp_parser *
3717 cp_parser_new (void)
3719 cp_parser *parser;
3720 cp_lexer *lexer;
3721 unsigned i;
3723 /* cp_lexer_new_main is called before doing GC allocation because
3724 cp_lexer_new_main might load a PCH file. */
3725 lexer = cp_lexer_new_main ();
3727 /* Initialize the binops_by_token so that we can get the tree
3728 directly from the token. */
3729 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3730 binops_by_token[binops[i].token_type] = binops[i];
3732 parser = ggc_cleared_alloc<cp_parser> ();
3733 parser->lexer = lexer;
3734 parser->context = cp_parser_context_new (NULL);
3736 /* For now, we always accept GNU extensions. */
3737 parser->allow_gnu_extensions_p = 1;
3739 /* The `>' token is a greater-than operator, not the end of a
3740 template-id. */
3741 parser->greater_than_is_operator_p = true;
3743 parser->default_arg_ok_p = true;
3745 /* We are not parsing a constant-expression. */
3746 parser->integral_constant_expression_p = false;
3747 parser->allow_non_integral_constant_expression_p = false;
3748 parser->non_integral_constant_expression_p = false;
3750 /* Local variable names are not forbidden. */
3751 parser->local_variables_forbidden_p = false;
3753 /* We are not processing an `extern "C"' declaration. */
3754 parser->in_unbraced_linkage_specification_p = false;
3756 /* We are not processing a declarator. */
3757 parser->in_declarator_p = false;
3759 /* We are not processing a template-argument-list. */
3760 parser->in_template_argument_list_p = false;
3762 /* We are not in an iteration statement. */
3763 parser->in_statement = 0;
3765 /* We are not in a switch statement. */
3766 parser->in_switch_statement_p = false;
3768 /* We are not parsing a type-id inside an expression. */
3769 parser->in_type_id_in_expr_p = false;
3771 /* Declarations aren't implicitly extern "C". */
3772 parser->implicit_extern_c = false;
3774 /* String literals should be translated to the execution character set. */
3775 parser->translate_strings_p = true;
3777 /* We are not parsing a function body. */
3778 parser->in_function_body = false;
3780 /* We can correct until told otherwise. */
3781 parser->colon_corrects_to_scope_p = true;
3783 /* The unparsed function queue is empty. */
3784 push_unparsed_function_queues (parser);
3786 /* There are no classes being defined. */
3787 parser->num_classes_being_defined = 0;
3789 /* No template parameters apply. */
3790 parser->num_template_parameter_lists = 0;
3792 /* Special parsing data structures. */
3793 parser->omp_declare_simd = NULL;
3794 parser->cilk_simd_fn_info = NULL;
3795 parser->oacc_routine = NULL;
3797 /* Not declaring an implicit function template. */
3798 parser->auto_is_implicit_function_template_parm_p = false;
3799 parser->fully_implicit_function_template_p = false;
3800 parser->implicit_template_parms = 0;
3801 parser->implicit_template_scope = 0;
3803 /* Allow constrained-type-specifiers. */
3804 parser->prevent_constrained_type_specifiers = 0;
3806 return parser;
3809 /* Create a cp_lexer structure which will emit the tokens in CACHE
3810 and push it onto the parser's lexer stack. This is used for delayed
3811 parsing of in-class method bodies and default arguments, and should
3812 not be confused with tentative parsing. */
3813 static void
3814 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3816 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3817 lexer->next = parser->lexer;
3818 parser->lexer = lexer;
3820 /* Move the current source position to that of the first token in the
3821 new lexer. */
3822 cp_lexer_set_source_position_from_token (lexer->next_token);
3825 /* Pop the top lexer off the parser stack. This is never used for the
3826 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3827 static void
3828 cp_parser_pop_lexer (cp_parser *parser)
3830 cp_lexer *lexer = parser->lexer;
3831 parser->lexer = lexer->next;
3832 cp_lexer_destroy (lexer);
3834 /* Put the current source position back where it was before this
3835 lexer was pushed. */
3836 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3839 /* Lexical conventions [gram.lex] */
3841 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3842 identifier. */
3844 static cp_expr
3845 cp_parser_identifier (cp_parser* parser)
3847 cp_token *token;
3849 /* Look for the identifier. */
3850 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3851 /* Return the value. */
3852 if (token)
3853 return cp_expr (token->u.value, token->location);
3854 else
3855 return error_mark_node;
3858 /* Parse a sequence of adjacent string constants. Returns a
3859 TREE_STRING representing the combined, nul-terminated string
3860 constant. If TRANSLATE is true, translate the string to the
3861 execution character set. If WIDE_OK is true, a wide string is
3862 invalid here.
3864 C++98 [lex.string] says that if a narrow string literal token is
3865 adjacent to a wide string literal token, the behavior is undefined.
3866 However, C99 6.4.5p4 says that this results in a wide string literal.
3867 We follow C99 here, for consistency with the C front end.
3869 This code is largely lifted from lex_string() in c-lex.c.
3871 FUTURE: ObjC++ will need to handle @-strings here. */
3872 static cp_expr
3873 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3874 bool lookup_udlit = true)
3876 tree value;
3877 size_t count;
3878 struct obstack str_ob;
3879 cpp_string str, istr, *strs;
3880 cp_token *tok;
3881 enum cpp_ttype type, curr_type;
3882 int have_suffix_p = 0;
3883 tree string_tree;
3884 tree suffix_id = NULL_TREE;
3885 bool curr_tok_is_userdef_p = false;
3887 tok = cp_lexer_peek_token (parser->lexer);
3888 if (!cp_parser_is_string_literal (tok))
3890 cp_parser_error (parser, "expected string-literal");
3891 return error_mark_node;
3894 location_t loc = tok->location;
3896 if (cpp_userdef_string_p (tok->type))
3898 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3899 curr_type = cpp_userdef_string_remove_type (tok->type);
3900 curr_tok_is_userdef_p = true;
3902 else
3904 string_tree = tok->u.value;
3905 curr_type = tok->type;
3907 type = curr_type;
3909 /* Try to avoid the overhead of creating and destroying an obstack
3910 for the common case of just one string. */
3911 if (!cp_parser_is_string_literal
3912 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3914 cp_lexer_consume_token (parser->lexer);
3916 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3917 str.len = TREE_STRING_LENGTH (string_tree);
3918 count = 1;
3920 if (curr_tok_is_userdef_p)
3922 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3923 have_suffix_p = 1;
3924 curr_type = cpp_userdef_string_remove_type (tok->type);
3926 else
3927 curr_type = tok->type;
3929 strs = &str;
3931 else
3933 location_t last_tok_loc = tok->location;
3934 gcc_obstack_init (&str_ob);
3935 count = 0;
3939 cp_lexer_consume_token (parser->lexer);
3940 count++;
3941 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3942 str.len = TREE_STRING_LENGTH (string_tree);
3944 if (curr_tok_is_userdef_p)
3946 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3947 if (have_suffix_p == 0)
3949 suffix_id = curr_suffix_id;
3950 have_suffix_p = 1;
3952 else if (have_suffix_p == 1
3953 && curr_suffix_id != suffix_id)
3955 error ("inconsistent user-defined literal suffixes"
3956 " %qD and %qD in string literal",
3957 suffix_id, curr_suffix_id);
3958 have_suffix_p = -1;
3960 curr_type = cpp_userdef_string_remove_type (tok->type);
3962 else
3963 curr_type = tok->type;
3965 if (type != curr_type)
3967 if (type == CPP_STRING)
3968 type = curr_type;
3969 else if (curr_type != CPP_STRING)
3971 rich_location rich_loc (line_table, tok->location);
3972 rich_loc.add_range (last_tok_loc, false);
3973 error_at_rich_loc (&rich_loc,
3974 "unsupported non-standard concatenation "
3975 "of string literals");
3979 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3981 last_tok_loc = tok->location;
3983 tok = cp_lexer_peek_token (parser->lexer);
3984 if (cpp_userdef_string_p (tok->type))
3986 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3987 curr_type = cpp_userdef_string_remove_type (tok->type);
3988 curr_tok_is_userdef_p = true;
3990 else
3992 string_tree = tok->u.value;
3993 curr_type = tok->type;
3994 curr_tok_is_userdef_p = false;
3997 while (cp_parser_is_string_literal (tok));
3999 /* A string literal built by concatenation has its caret=start at
4000 the start of the initial string, and its finish at the finish of
4001 the final string literal. */
4002 loc = make_location (loc, loc, get_finish (last_tok_loc));
4004 strs = (cpp_string *) obstack_finish (&str_ob);
4007 if (type != CPP_STRING && !wide_ok)
4009 cp_parser_error (parser, "a wide string is invalid in this context");
4010 type = CPP_STRING;
4013 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4014 (parse_in, strs, count, &istr, type))
4016 value = build_string (istr.len, (const char *)istr.text);
4017 free (CONST_CAST (unsigned char *, istr.text));
4019 switch (type)
4021 default:
4022 case CPP_STRING:
4023 case CPP_UTF8STRING:
4024 TREE_TYPE (value) = char_array_type_node;
4025 break;
4026 case CPP_STRING16:
4027 TREE_TYPE (value) = char16_array_type_node;
4028 break;
4029 case CPP_STRING32:
4030 TREE_TYPE (value) = char32_array_type_node;
4031 break;
4032 case CPP_WSTRING:
4033 TREE_TYPE (value) = wchar_array_type_node;
4034 break;
4037 value = fix_string_type (value);
4039 if (have_suffix_p)
4041 tree literal = build_userdef_literal (suffix_id, value,
4042 OT_NONE, NULL_TREE);
4043 if (lookup_udlit)
4044 value = cp_parser_userdef_string_literal (literal);
4045 else
4046 value = literal;
4049 else
4050 /* cpp_interpret_string has issued an error. */
4051 value = error_mark_node;
4053 if (count > 1)
4054 obstack_free (&str_ob, 0);
4056 return cp_expr (value, loc);
4059 /* Look up a literal operator with the name and the exact arguments. */
4061 static tree
4062 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4064 tree decl, fns;
4065 decl = lookup_name (name);
4066 if (!decl || !is_overloaded_fn (decl))
4067 return error_mark_node;
4069 for (fns = decl; fns; fns = OVL_NEXT (fns))
4071 unsigned int ix;
4072 bool found = true;
4073 tree fn = OVL_CURRENT (fns);
4074 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4075 if (parmtypes != NULL_TREE)
4077 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4078 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4080 tree tparm = TREE_VALUE (parmtypes);
4081 tree targ = TREE_TYPE ((*args)[ix]);
4082 bool ptr = TYPE_PTR_P (tparm);
4083 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4084 if ((ptr || arr || !same_type_p (tparm, targ))
4085 && (!ptr || !arr
4086 || !same_type_p (TREE_TYPE (tparm),
4087 TREE_TYPE (targ))))
4088 found = false;
4090 if (found
4091 && ix == vec_safe_length (args)
4092 /* May be this should be sufficient_parms_p instead,
4093 depending on how exactly should user-defined literals
4094 work in presence of default arguments on the literal
4095 operator parameters. */
4096 && parmtypes == void_list_node)
4097 return decl;
4101 return error_mark_node;
4104 /* Parse a user-defined char constant. Returns a call to a user-defined
4105 literal operator taking the character as an argument. */
4107 static cp_expr
4108 cp_parser_userdef_char_literal (cp_parser *parser)
4110 cp_token *token = cp_lexer_consume_token (parser->lexer);
4111 tree literal = token->u.value;
4112 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4113 tree value = USERDEF_LITERAL_VALUE (literal);
4114 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4115 tree decl, result;
4117 /* Build up a call to the user-defined operator */
4118 /* Lookup the name we got back from the id-expression. */
4119 vec<tree, va_gc> *args = make_tree_vector ();
4120 vec_safe_push (args, value);
4121 decl = lookup_literal_operator (name, args);
4122 if (!decl || decl == error_mark_node)
4124 error ("unable to find character literal operator %qD with %qT argument",
4125 name, TREE_TYPE (value));
4126 release_tree_vector (args);
4127 return error_mark_node;
4129 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4130 release_tree_vector (args);
4131 return result;
4134 /* A subroutine of cp_parser_userdef_numeric_literal to
4135 create a char... template parameter pack from a string node. */
4137 static tree
4138 make_char_string_pack (tree value)
4140 tree charvec;
4141 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4142 const char *str = TREE_STRING_POINTER (value);
4143 int i, len = TREE_STRING_LENGTH (value) - 1;
4144 tree argvec = make_tree_vec (1);
4146 /* Fill in CHARVEC with all of the parameters. */
4147 charvec = make_tree_vec (len);
4148 for (i = 0; i < len; ++i)
4149 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4151 /* Build the argument packs. */
4152 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4153 TREE_TYPE (argpack) = char_type_node;
4155 TREE_VEC_ELT (argvec, 0) = argpack;
4157 return argvec;
4160 /* A subroutine of cp_parser_userdef_numeric_literal to
4161 create a char... template parameter pack from a string node. */
4163 static tree
4164 make_string_pack (tree value)
4166 tree charvec;
4167 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4168 const unsigned char *str
4169 = (const unsigned char *) TREE_STRING_POINTER (value);
4170 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4171 int len = TREE_STRING_LENGTH (value) / sz - 1;
4172 tree argvec = make_tree_vec (2);
4174 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4175 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4177 /* First template parm is character type. */
4178 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4180 /* Fill in CHARVEC with all of the parameters. */
4181 charvec = make_tree_vec (len);
4182 for (int i = 0; i < len; ++i)
4183 TREE_VEC_ELT (charvec, i)
4184 = double_int_to_tree (str_char_type_node,
4185 double_int::from_buffer (str + i * sz, sz));
4187 /* Build the argument packs. */
4188 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4189 TREE_TYPE (argpack) = str_char_type_node;
4191 TREE_VEC_ELT (argvec, 1) = argpack;
4193 return argvec;
4196 /* Parse a user-defined numeric constant. returns a call to a user-defined
4197 literal operator. */
4199 static cp_expr
4200 cp_parser_userdef_numeric_literal (cp_parser *parser)
4202 cp_token *token = cp_lexer_consume_token (parser->lexer);
4203 tree literal = token->u.value;
4204 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4205 tree value = USERDEF_LITERAL_VALUE (literal);
4206 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4207 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4208 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4209 tree decl, result;
4210 vec<tree, va_gc> *args;
4212 /* Look for a literal operator taking the exact type of numeric argument
4213 as the literal value. */
4214 args = make_tree_vector ();
4215 vec_safe_push (args, value);
4216 decl = lookup_literal_operator (name, args);
4217 if (decl && decl != error_mark_node)
4219 result = finish_call_expr (decl, &args, false, true,
4220 tf_warning_or_error);
4222 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4224 warning_at (token->location, OPT_Woverflow,
4225 "integer literal exceeds range of %qT type",
4226 long_long_unsigned_type_node);
4228 else
4230 if (overflow > 0)
4231 warning_at (token->location, OPT_Woverflow,
4232 "floating literal exceeds range of %qT type",
4233 long_double_type_node);
4234 else if (overflow < 0)
4235 warning_at (token->location, OPT_Woverflow,
4236 "floating literal truncated to zero");
4239 release_tree_vector (args);
4240 return result;
4242 release_tree_vector (args);
4244 /* If the numeric argument didn't work, look for a raw literal
4245 operator taking a const char* argument consisting of the number
4246 in string format. */
4247 args = make_tree_vector ();
4248 vec_safe_push (args, num_string);
4249 decl = lookup_literal_operator (name, args);
4250 if (decl && decl != error_mark_node)
4252 result = finish_call_expr (decl, &args, false, true,
4253 tf_warning_or_error);
4254 release_tree_vector (args);
4255 return result;
4257 release_tree_vector (args);
4259 /* If the raw literal didn't work, look for a non-type template
4260 function with parameter pack char.... Call the function with
4261 template parameter characters representing the number. */
4262 args = make_tree_vector ();
4263 decl = lookup_literal_operator (name, args);
4264 if (decl && decl != error_mark_node)
4266 tree tmpl_args = make_char_string_pack (num_string);
4267 decl = lookup_template_function (decl, tmpl_args);
4268 result = finish_call_expr (decl, &args, false, true,
4269 tf_warning_or_error);
4270 release_tree_vector (args);
4271 return result;
4274 release_tree_vector (args);
4276 error ("unable to find numeric literal operator %qD", name);
4277 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4278 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4279 "to enable more built-in suffixes");
4280 return error_mark_node;
4283 /* Parse a user-defined string constant. Returns a call to a user-defined
4284 literal operator taking a character pointer and the length of the string
4285 as arguments. */
4287 static tree
4288 cp_parser_userdef_string_literal (tree literal)
4290 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4291 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4292 tree value = USERDEF_LITERAL_VALUE (literal);
4293 int len = TREE_STRING_LENGTH (value)
4294 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4295 tree decl, result;
4296 vec<tree, va_gc> *args;
4298 /* Build up a call to the user-defined operator. */
4299 /* Lookup the name we got back from the id-expression. */
4300 args = make_tree_vector ();
4301 vec_safe_push (args, value);
4302 vec_safe_push (args, build_int_cst (size_type_node, len));
4303 decl = lookup_literal_operator (name, args);
4305 if (decl && decl != error_mark_node)
4307 result = finish_call_expr (decl, &args, false, true,
4308 tf_warning_or_error);
4309 release_tree_vector (args);
4310 return result;
4312 release_tree_vector (args);
4314 /* Look for a template function with typename parameter CharT
4315 and parameter pack CharT... Call the function with
4316 template parameter characters representing the string. */
4317 args = make_tree_vector ();
4318 decl = lookup_literal_operator (name, args);
4319 if (decl && decl != error_mark_node)
4321 tree tmpl_args = make_string_pack (value);
4322 decl = lookup_template_function (decl, tmpl_args);
4323 result = finish_call_expr (decl, &args, false, true,
4324 tf_warning_or_error);
4325 release_tree_vector (args);
4326 return result;
4328 release_tree_vector (args);
4330 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4331 name, TREE_TYPE (value), size_type_node);
4332 return error_mark_node;
4336 /* Basic concepts [gram.basic] */
4338 /* Parse a translation-unit.
4340 translation-unit:
4341 declaration-seq [opt]
4343 Returns TRUE if all went well. */
4345 static bool
4346 cp_parser_translation_unit (cp_parser* parser)
4348 /* The address of the first non-permanent object on the declarator
4349 obstack. */
4350 static void *declarator_obstack_base;
4352 bool success;
4354 /* Create the declarator obstack, if necessary. */
4355 if (!cp_error_declarator)
4357 gcc_obstack_init (&declarator_obstack);
4358 /* Create the error declarator. */
4359 cp_error_declarator = make_declarator (cdk_error);
4360 /* Create the empty parameter list. */
4361 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4362 /* Remember where the base of the declarator obstack lies. */
4363 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4366 cp_parser_declaration_seq_opt (parser);
4368 /* If there are no tokens left then all went well. */
4369 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4371 /* Get rid of the token array; we don't need it any more. */
4372 cp_lexer_destroy (parser->lexer);
4373 parser->lexer = NULL;
4375 /* This file might have been a context that's implicitly extern
4376 "C". If so, pop the lang context. (Only relevant for PCH.) */
4377 if (parser->implicit_extern_c)
4379 pop_lang_context ();
4380 parser->implicit_extern_c = false;
4383 /* Finish up. */
4384 finish_translation_unit ();
4386 success = true;
4388 else
4390 cp_parser_error (parser, "expected declaration");
4391 success = false;
4394 /* Make sure the declarator obstack was fully cleaned up. */
4395 gcc_assert (obstack_next_free (&declarator_obstack)
4396 == declarator_obstack_base);
4398 /* All went well. */
4399 return success;
4402 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4403 decltype context. */
4405 static inline tsubst_flags_t
4406 complain_flags (bool decltype_p)
4408 tsubst_flags_t complain = tf_warning_or_error;
4409 if (decltype_p)
4410 complain |= tf_decltype;
4411 return complain;
4414 /* We're about to parse a collection of statements. If we're currently
4415 parsing tentatively, set up a firewall so that any nested
4416 cp_parser_commit_to_tentative_parse won't affect the current context. */
4418 static cp_token_position
4419 cp_parser_start_tentative_firewall (cp_parser *parser)
4421 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4422 return 0;
4424 cp_parser_parse_tentatively (parser);
4425 cp_parser_commit_to_topmost_tentative_parse (parser);
4426 return cp_lexer_token_position (parser->lexer, false);
4429 /* We've finished parsing the collection of statements. Wrap up the
4430 firewall and replace the relevant tokens with the parsed form. */
4432 static void
4433 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4434 tree expr)
4436 if (!start)
4437 return;
4439 /* Finish the firewall level. */
4440 cp_parser_parse_definitely (parser);
4441 /* And remember the result of the parse for when we try again. */
4442 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4443 token->type = CPP_PREPARSED_EXPR;
4444 token->u.value = expr;
4445 token->keyword = RID_MAX;
4446 cp_lexer_purge_tokens_after (parser->lexer, start);
4449 /* Like the above functions, but let the user modify the tokens. Used by
4450 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4451 later parses, so it makes sense to localize the effects of
4452 cp_parser_commit_to_tentative_parse. */
4454 struct tentative_firewall
4456 cp_parser *parser;
4457 bool set;
4459 tentative_firewall (cp_parser *p): parser(p)
4461 /* If we're currently parsing tentatively, start a committed level as a
4462 firewall and then an inner tentative parse. */
4463 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4465 cp_parser_parse_tentatively (parser);
4466 cp_parser_commit_to_topmost_tentative_parse (parser);
4467 cp_parser_parse_tentatively (parser);
4471 ~tentative_firewall()
4473 if (set)
4475 /* Finish the inner tentative parse and the firewall, propagating any
4476 uncommitted error state to the outer tentative parse. */
4477 bool err = cp_parser_error_occurred (parser);
4478 cp_parser_parse_definitely (parser);
4479 cp_parser_parse_definitely (parser);
4480 if (err)
4481 cp_parser_simulate_error (parser);
4486 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4487 enclosing parentheses. */
4489 static cp_expr
4490 cp_parser_statement_expr (cp_parser *parser)
4492 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4494 /* Consume the '('. */
4495 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4496 cp_lexer_consume_token (parser->lexer);
4497 /* Start the statement-expression. */
4498 tree expr = begin_stmt_expr ();
4499 /* Parse the compound-statement. */
4500 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4501 /* Finish up. */
4502 expr = finish_stmt_expr (expr, false);
4503 /* Consume the ')'. */
4504 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4505 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4506 cp_parser_skip_to_end_of_statement (parser);
4508 cp_parser_end_tentative_firewall (parser, start, expr);
4509 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4510 return cp_expr (expr, combined_loc);
4513 /* Expressions [gram.expr] */
4515 /* Parse a fold-operator.
4517 fold-operator:
4518 - * / % ^ & | = < > << >>
4519 = -= *= /= %= ^= &= |= <<= >>=
4520 == != <= >= && || , .* ->*
4522 This returns the tree code corresponding to the matched operator
4523 as an int. When the current token matches a compound assignment
4524 opertor, the resulting tree code is the negative value of the
4525 non-assignment operator. */
4527 static int
4528 cp_parser_fold_operator (cp_token *token)
4530 switch (token->type)
4532 case CPP_PLUS: return PLUS_EXPR;
4533 case CPP_MINUS: return MINUS_EXPR;
4534 case CPP_MULT: return MULT_EXPR;
4535 case CPP_DIV: return TRUNC_DIV_EXPR;
4536 case CPP_MOD: return TRUNC_MOD_EXPR;
4537 case CPP_XOR: return BIT_XOR_EXPR;
4538 case CPP_AND: return BIT_AND_EXPR;
4539 case CPP_OR: return BIT_IOR_EXPR;
4540 case CPP_LSHIFT: return LSHIFT_EXPR;
4541 case CPP_RSHIFT: return RSHIFT_EXPR;
4543 case CPP_EQ: return -NOP_EXPR;
4544 case CPP_PLUS_EQ: return -PLUS_EXPR;
4545 case CPP_MINUS_EQ: return -MINUS_EXPR;
4546 case CPP_MULT_EQ: return -MULT_EXPR;
4547 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4548 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4549 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4550 case CPP_AND_EQ: return -BIT_AND_EXPR;
4551 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4552 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4553 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4555 case CPP_EQ_EQ: return EQ_EXPR;
4556 case CPP_NOT_EQ: return NE_EXPR;
4557 case CPP_LESS: return LT_EXPR;
4558 case CPP_GREATER: return GT_EXPR;
4559 case CPP_LESS_EQ: return LE_EXPR;
4560 case CPP_GREATER_EQ: return GE_EXPR;
4562 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4563 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4565 case CPP_COMMA: return COMPOUND_EXPR;
4567 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4568 case CPP_DEREF_STAR: return MEMBER_REF;
4570 default: return ERROR_MARK;
4574 /* Returns true if CODE indicates a binary expression, which is not allowed in
4575 the LHS of a fold-expression. More codes will need to be added to use this
4576 function in other contexts. */
4578 static bool
4579 is_binary_op (tree_code code)
4581 switch (code)
4583 case PLUS_EXPR:
4584 case POINTER_PLUS_EXPR:
4585 case MINUS_EXPR:
4586 case MULT_EXPR:
4587 case TRUNC_DIV_EXPR:
4588 case TRUNC_MOD_EXPR:
4589 case BIT_XOR_EXPR:
4590 case BIT_AND_EXPR:
4591 case BIT_IOR_EXPR:
4592 case LSHIFT_EXPR:
4593 case RSHIFT_EXPR:
4595 case MODOP_EXPR:
4597 case EQ_EXPR:
4598 case NE_EXPR:
4599 case LE_EXPR:
4600 case GE_EXPR:
4601 case LT_EXPR:
4602 case GT_EXPR:
4604 case TRUTH_ANDIF_EXPR:
4605 case TRUTH_ORIF_EXPR:
4607 case COMPOUND_EXPR:
4609 case DOTSTAR_EXPR:
4610 case MEMBER_REF:
4611 return true;
4613 default:
4614 return false;
4618 /* If the next token is a suitable fold operator, consume it and return as
4619 the function above. */
4621 static int
4622 cp_parser_fold_operator (cp_parser *parser)
4624 cp_token* token = cp_lexer_peek_token (parser->lexer);
4625 int code = cp_parser_fold_operator (token);
4626 if (code != ERROR_MARK)
4627 cp_lexer_consume_token (parser->lexer);
4628 return code;
4631 /* Parse a fold-expression.
4633 fold-expression:
4634 ( ... folding-operator cast-expression)
4635 ( cast-expression folding-operator ... )
4636 ( cast-expression folding operator ... folding-operator cast-expression)
4638 Note that the '(' and ')' are matched in primary expression. */
4640 static cp_expr
4641 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4643 cp_id_kind pidk;
4645 // Left fold.
4646 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4648 cp_lexer_consume_token (parser->lexer);
4649 int op = cp_parser_fold_operator (parser);
4650 if (op == ERROR_MARK)
4652 cp_parser_error (parser, "expected binary operator");
4653 return error_mark_node;
4656 tree expr = cp_parser_cast_expression (parser, false, false,
4657 false, &pidk);
4658 if (expr == error_mark_node)
4659 return error_mark_node;
4660 return finish_left_unary_fold_expr (expr, op);
4663 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4664 int op = cp_parser_fold_operator (parser);
4665 if (op == ERROR_MARK)
4667 cp_parser_error (parser, "expected binary operator");
4668 return error_mark_node;
4671 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4673 cp_parser_error (parser, "expected ...");
4674 return error_mark_node;
4676 cp_lexer_consume_token (parser->lexer);
4678 /* The operands of a fold-expression are cast-expressions, so binary or
4679 conditional expressions are not allowed. We check this here to avoid
4680 tentative parsing. */
4681 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4682 /* OK, the expression was parenthesized. */;
4683 else if (is_binary_op (TREE_CODE (expr1)))
4684 error_at (location_of (expr1),
4685 "binary expression in operand of fold-expression");
4686 else if (TREE_CODE (expr1) == COND_EXPR)
4687 error_at (location_of (expr1),
4688 "conditional expression in operand of fold-expression");
4690 // Right fold.
4691 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4692 return finish_right_unary_fold_expr (expr1, op);
4694 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4696 cp_parser_error (parser, "mismatched operator in fold-expression");
4697 return error_mark_node;
4699 cp_lexer_consume_token (parser->lexer);
4701 // Binary left or right fold.
4702 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4703 if (expr2 == error_mark_node)
4704 return error_mark_node;
4705 return finish_binary_fold_expr (expr1, expr2, op);
4708 /* Parse a primary-expression.
4710 primary-expression:
4711 literal
4712 this
4713 ( expression )
4714 id-expression
4715 lambda-expression (C++11)
4717 GNU Extensions:
4719 primary-expression:
4720 ( compound-statement )
4721 __builtin_va_arg ( assignment-expression , type-id )
4722 __builtin_offsetof ( type-id , offsetof-expression )
4724 C++ Extensions:
4725 __has_nothrow_assign ( type-id )
4726 __has_nothrow_constructor ( type-id )
4727 __has_nothrow_copy ( type-id )
4728 __has_trivial_assign ( type-id )
4729 __has_trivial_constructor ( type-id )
4730 __has_trivial_copy ( type-id )
4731 __has_trivial_destructor ( type-id )
4732 __has_virtual_destructor ( type-id )
4733 __is_abstract ( type-id )
4734 __is_base_of ( type-id , type-id )
4735 __is_class ( type-id )
4736 __is_empty ( type-id )
4737 __is_enum ( type-id )
4738 __is_final ( type-id )
4739 __is_literal_type ( type-id )
4740 __is_pod ( type-id )
4741 __is_polymorphic ( type-id )
4742 __is_std_layout ( type-id )
4743 __is_trivial ( type-id )
4744 __is_union ( type-id )
4746 Objective-C++ Extension:
4748 primary-expression:
4749 objc-expression
4751 literal:
4752 __null
4754 ADDRESS_P is true iff this expression was immediately preceded by
4755 "&" and therefore might denote a pointer-to-member. CAST_P is true
4756 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4757 true iff this expression is a template argument.
4759 Returns a representation of the expression. Upon return, *IDK
4760 indicates what kind of id-expression (if any) was present. */
4762 static cp_expr
4763 cp_parser_primary_expression (cp_parser *parser,
4764 bool address_p,
4765 bool cast_p,
4766 bool template_arg_p,
4767 bool decltype_p,
4768 cp_id_kind *idk)
4770 cp_token *token = NULL;
4772 /* Assume the primary expression is not an id-expression. */
4773 *idk = CP_ID_KIND_NONE;
4775 /* Peek at the next token. */
4776 token = cp_lexer_peek_token (parser->lexer);
4777 switch ((int) token->type)
4779 /* literal:
4780 integer-literal
4781 character-literal
4782 floating-literal
4783 string-literal
4784 boolean-literal
4785 pointer-literal
4786 user-defined-literal */
4787 case CPP_CHAR:
4788 case CPP_CHAR16:
4789 case CPP_CHAR32:
4790 case CPP_WCHAR:
4791 case CPP_UTF8CHAR:
4792 case CPP_NUMBER:
4793 case CPP_PREPARSED_EXPR:
4794 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4795 return cp_parser_userdef_numeric_literal (parser);
4796 token = cp_lexer_consume_token (parser->lexer);
4797 if (TREE_CODE (token->u.value) == FIXED_CST)
4799 error_at (token->location,
4800 "fixed-point types not supported in C++");
4801 return error_mark_node;
4803 /* Floating-point literals are only allowed in an integral
4804 constant expression if they are cast to an integral or
4805 enumeration type. */
4806 if (TREE_CODE (token->u.value) == REAL_CST
4807 && parser->integral_constant_expression_p
4808 && pedantic)
4810 /* CAST_P will be set even in invalid code like "int(2.7 +
4811 ...)". Therefore, we have to check that the next token
4812 is sure to end the cast. */
4813 if (cast_p)
4815 cp_token *next_token;
4817 next_token = cp_lexer_peek_token (parser->lexer);
4818 if (/* The comma at the end of an
4819 enumerator-definition. */
4820 next_token->type != CPP_COMMA
4821 /* The curly brace at the end of an enum-specifier. */
4822 && next_token->type != CPP_CLOSE_BRACE
4823 /* The end of a statement. */
4824 && next_token->type != CPP_SEMICOLON
4825 /* The end of the cast-expression. */
4826 && next_token->type != CPP_CLOSE_PAREN
4827 /* The end of an array bound. */
4828 && next_token->type != CPP_CLOSE_SQUARE
4829 /* The closing ">" in a template-argument-list. */
4830 && (next_token->type != CPP_GREATER
4831 || parser->greater_than_is_operator_p)
4832 /* C++0x only: A ">>" treated like two ">" tokens,
4833 in a template-argument-list. */
4834 && (next_token->type != CPP_RSHIFT
4835 || (cxx_dialect == cxx98)
4836 || parser->greater_than_is_operator_p))
4837 cast_p = false;
4840 /* If we are within a cast, then the constraint that the
4841 cast is to an integral or enumeration type will be
4842 checked at that point. If we are not within a cast, then
4843 this code is invalid. */
4844 if (!cast_p)
4845 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4847 return cp_expr (token->u.value, token->location);
4849 case CPP_CHAR_USERDEF:
4850 case CPP_CHAR16_USERDEF:
4851 case CPP_CHAR32_USERDEF:
4852 case CPP_WCHAR_USERDEF:
4853 case CPP_UTF8CHAR_USERDEF:
4854 return cp_parser_userdef_char_literal (parser);
4856 case CPP_STRING:
4857 case CPP_STRING16:
4858 case CPP_STRING32:
4859 case CPP_WSTRING:
4860 case CPP_UTF8STRING:
4861 case CPP_STRING_USERDEF:
4862 case CPP_STRING16_USERDEF:
4863 case CPP_STRING32_USERDEF:
4864 case CPP_WSTRING_USERDEF:
4865 case CPP_UTF8STRING_USERDEF:
4866 /* ??? Should wide strings be allowed when parser->translate_strings_p
4867 is false (i.e. in attributes)? If not, we can kill the third
4868 argument to cp_parser_string_literal. */
4869 return cp_parser_string_literal (parser,
4870 parser->translate_strings_p,
4871 true);
4873 case CPP_OPEN_PAREN:
4874 /* If we see `( { ' then we are looking at the beginning of
4875 a GNU statement-expression. */
4876 if (cp_parser_allow_gnu_extensions_p (parser)
4877 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4879 /* Statement-expressions are not allowed by the standard. */
4880 pedwarn (token->location, OPT_Wpedantic,
4881 "ISO C++ forbids braced-groups within expressions");
4883 /* And they're not allowed outside of a function-body; you
4884 cannot, for example, write:
4886 int i = ({ int j = 3; j + 1; });
4888 at class or namespace scope. */
4889 if (!parser->in_function_body
4890 || parser->in_template_argument_list_p)
4892 error_at (token->location,
4893 "statement-expressions are not allowed outside "
4894 "functions nor in template-argument lists");
4895 cp_parser_skip_to_end_of_block_or_statement (parser);
4896 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4897 cp_lexer_consume_token (parser->lexer);
4898 return error_mark_node;
4900 else
4901 return cp_parser_statement_expr (parser);
4903 /* Otherwise it's a normal parenthesized expression. */
4905 cp_expr expr;
4906 bool saved_greater_than_is_operator_p;
4908 location_t open_paren_loc = token->location;
4910 /* Consume the `('. */
4911 cp_lexer_consume_token (parser->lexer);
4912 /* Within a parenthesized expression, a `>' token is always
4913 the greater-than operator. */
4914 saved_greater_than_is_operator_p
4915 = parser->greater_than_is_operator_p;
4916 parser->greater_than_is_operator_p = true;
4918 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4919 /* Left fold expression. */
4920 expr = NULL_TREE;
4921 else
4922 /* Parse the parenthesized expression. */
4923 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4925 token = cp_lexer_peek_token (parser->lexer);
4926 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4928 expr = cp_parser_fold_expression (parser, expr);
4929 if (expr != error_mark_node
4930 && cxx_dialect < cxx1z
4931 && !in_system_header_at (input_location))
4932 pedwarn (input_location, 0, "fold-expressions only available "
4933 "with -std=c++1z or -std=gnu++1z");
4935 else
4936 /* Let the front end know that this expression was
4937 enclosed in parentheses. This matters in case, for
4938 example, the expression is of the form `A::B', since
4939 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4940 not. */
4941 expr = finish_parenthesized_expr (expr);
4943 /* DR 705: Wrapping an unqualified name in parentheses
4944 suppresses arg-dependent lookup. We want to pass back
4945 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4946 (c++/37862), but none of the others. */
4947 if (*idk != CP_ID_KIND_QUALIFIED)
4948 *idk = CP_ID_KIND_NONE;
4950 /* The `>' token might be the end of a template-id or
4951 template-parameter-list now. */
4952 parser->greater_than_is_operator_p
4953 = saved_greater_than_is_operator_p;
4955 /* Consume the `)'. */
4956 token = cp_lexer_peek_token (parser->lexer);
4957 location_t close_paren_loc = token->location;
4958 expr.set_range (open_paren_loc, close_paren_loc);
4959 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4960 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4961 cp_parser_skip_to_end_of_statement (parser);
4963 return expr;
4966 case CPP_OPEN_SQUARE:
4968 if (c_dialect_objc ())
4970 /* We might have an Objective-C++ message. */
4971 cp_parser_parse_tentatively (parser);
4972 tree msg = cp_parser_objc_message_expression (parser);
4973 /* If that works out, we're done ... */
4974 if (cp_parser_parse_definitely (parser))
4975 return msg;
4976 /* ... else, fall though to see if it's a lambda. */
4978 cp_expr lam = cp_parser_lambda_expression (parser);
4979 /* Don't warn about a failed tentative parse. */
4980 if (cp_parser_error_occurred (parser))
4981 return error_mark_node;
4982 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4983 return lam;
4986 case CPP_OBJC_STRING:
4987 if (c_dialect_objc ())
4988 /* We have an Objective-C++ string literal. */
4989 return cp_parser_objc_expression (parser);
4990 cp_parser_error (parser, "expected primary-expression");
4991 return error_mark_node;
4993 case CPP_KEYWORD:
4994 switch (token->keyword)
4996 /* These two are the boolean literals. */
4997 case RID_TRUE:
4998 cp_lexer_consume_token (parser->lexer);
4999 return cp_expr (boolean_true_node, token->location);
5000 case RID_FALSE:
5001 cp_lexer_consume_token (parser->lexer);
5002 return cp_expr (boolean_false_node, token->location);
5004 /* The `__null' literal. */
5005 case RID_NULL:
5006 cp_lexer_consume_token (parser->lexer);
5007 return cp_expr (null_node, token->location);
5009 /* The `nullptr' literal. */
5010 case RID_NULLPTR:
5011 cp_lexer_consume_token (parser->lexer);
5012 return cp_expr (nullptr_node, token->location);
5014 /* Recognize the `this' keyword. */
5015 case RID_THIS:
5016 cp_lexer_consume_token (parser->lexer);
5017 if (parser->local_variables_forbidden_p)
5019 error_at (token->location,
5020 "%<this%> may not be used in this context");
5021 return error_mark_node;
5023 /* Pointers cannot appear in constant-expressions. */
5024 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5025 return error_mark_node;
5026 return cp_expr (finish_this_expr (), token->location);
5028 /* The `operator' keyword can be the beginning of an
5029 id-expression. */
5030 case RID_OPERATOR:
5031 goto id_expression;
5033 case RID_FUNCTION_NAME:
5034 case RID_PRETTY_FUNCTION_NAME:
5035 case RID_C99_FUNCTION_NAME:
5037 non_integral_constant name;
5039 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5040 __func__ are the names of variables -- but they are
5041 treated specially. Therefore, they are handled here,
5042 rather than relying on the generic id-expression logic
5043 below. Grammatically, these names are id-expressions.
5045 Consume the token. */
5046 token = cp_lexer_consume_token (parser->lexer);
5048 switch (token->keyword)
5050 case RID_FUNCTION_NAME:
5051 name = NIC_FUNC_NAME;
5052 break;
5053 case RID_PRETTY_FUNCTION_NAME:
5054 name = NIC_PRETTY_FUNC;
5055 break;
5056 case RID_C99_FUNCTION_NAME:
5057 name = NIC_C99_FUNC;
5058 break;
5059 default:
5060 gcc_unreachable ();
5063 if (cp_parser_non_integral_constant_expression (parser, name))
5064 return error_mark_node;
5066 /* Look up the name. */
5067 return finish_fname (token->u.value);
5070 case RID_VA_ARG:
5072 tree expression;
5073 tree type;
5074 source_location type_location;
5075 location_t start_loc
5076 = cp_lexer_peek_token (parser->lexer)->location;
5077 /* The `__builtin_va_arg' construct is used to handle
5078 `va_arg'. Consume the `__builtin_va_arg' token. */
5079 cp_lexer_consume_token (parser->lexer);
5080 /* Look for the opening `('. */
5081 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5082 /* Now, parse the assignment-expression. */
5083 expression = cp_parser_assignment_expression (parser);
5084 /* Look for the `,'. */
5085 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5086 type_location = cp_lexer_peek_token (parser->lexer)->location;
5087 /* Parse the type-id. */
5089 type_id_in_expr_sentinel s (parser);
5090 type = cp_parser_type_id (parser);
5092 /* Look for the closing `)'. */
5093 location_t finish_loc
5094 = cp_lexer_peek_token (parser->lexer)->location;
5095 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5096 /* Using `va_arg' in a constant-expression is not
5097 allowed. */
5098 if (cp_parser_non_integral_constant_expression (parser,
5099 NIC_VA_ARG))
5100 return error_mark_node;
5101 /* Construct a location of the form:
5102 __builtin_va_arg (v, int)
5103 ~~~~~~~~~~~~~~~~~~~~~^~~~
5104 with the caret at the type, ranging from the start of the
5105 "__builtin_va_arg" token to the close paren. */
5106 location_t combined_loc
5107 = make_location (type_location, start_loc, finish_loc);
5108 return build_x_va_arg (combined_loc, expression, type);
5111 case RID_OFFSETOF:
5112 return cp_parser_builtin_offsetof (parser);
5114 case RID_HAS_NOTHROW_ASSIGN:
5115 case RID_HAS_NOTHROW_CONSTRUCTOR:
5116 case RID_HAS_NOTHROW_COPY:
5117 case RID_HAS_TRIVIAL_ASSIGN:
5118 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5119 case RID_HAS_TRIVIAL_COPY:
5120 case RID_HAS_TRIVIAL_DESTRUCTOR:
5121 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5122 case RID_HAS_VIRTUAL_DESTRUCTOR:
5123 case RID_IS_ABSTRACT:
5124 case RID_IS_AGGREGATE:
5125 case RID_IS_BASE_OF:
5126 case RID_IS_CLASS:
5127 case RID_IS_EMPTY:
5128 case RID_IS_ENUM:
5129 case RID_IS_FINAL:
5130 case RID_IS_LITERAL_TYPE:
5131 case RID_IS_POD:
5132 case RID_IS_POLYMORPHIC:
5133 case RID_IS_SAME_AS:
5134 case RID_IS_STD_LAYOUT:
5135 case RID_IS_TRIVIAL:
5136 case RID_IS_TRIVIALLY_ASSIGNABLE:
5137 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5138 case RID_IS_TRIVIALLY_COPYABLE:
5139 case RID_IS_UNION:
5140 return cp_parser_trait_expr (parser, token->keyword);
5142 // C++ concepts
5143 case RID_REQUIRES:
5144 return cp_parser_requires_expression (parser);
5146 /* Objective-C++ expressions. */
5147 case RID_AT_ENCODE:
5148 case RID_AT_PROTOCOL:
5149 case RID_AT_SELECTOR:
5150 return cp_parser_objc_expression (parser);
5152 case RID_TEMPLATE:
5153 if (parser->in_function_body
5154 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5155 == CPP_LESS))
5157 error_at (token->location,
5158 "a template declaration cannot appear at block scope");
5159 cp_parser_skip_to_end_of_block_or_statement (parser);
5160 return error_mark_node;
5162 /* FALLTHRU */
5163 default:
5164 cp_parser_error (parser, "expected primary-expression");
5165 return error_mark_node;
5168 /* An id-expression can start with either an identifier, a
5169 `::' as the beginning of a qualified-id, or the "operator"
5170 keyword. */
5171 case CPP_NAME:
5172 case CPP_SCOPE:
5173 case CPP_TEMPLATE_ID:
5174 case CPP_NESTED_NAME_SPECIFIER:
5176 id_expression:
5177 cp_expr id_expression;
5178 cp_expr decl;
5179 const char *error_msg;
5180 bool template_p;
5181 bool done;
5182 cp_token *id_expr_token;
5184 /* Parse the id-expression. */
5185 id_expression
5186 = cp_parser_id_expression (parser,
5187 /*template_keyword_p=*/false,
5188 /*check_dependency_p=*/true,
5189 &template_p,
5190 /*declarator_p=*/false,
5191 /*optional_p=*/false);
5192 if (id_expression == error_mark_node)
5193 return error_mark_node;
5194 id_expr_token = token;
5195 token = cp_lexer_peek_token (parser->lexer);
5196 done = (token->type != CPP_OPEN_SQUARE
5197 && token->type != CPP_OPEN_PAREN
5198 && token->type != CPP_DOT
5199 && token->type != CPP_DEREF
5200 && token->type != CPP_PLUS_PLUS
5201 && token->type != CPP_MINUS_MINUS);
5202 /* If we have a template-id, then no further lookup is
5203 required. If the template-id was for a template-class, we
5204 will sometimes have a TYPE_DECL at this point. */
5205 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5206 || TREE_CODE (id_expression) == TYPE_DECL)
5207 decl = id_expression;
5208 /* Look up the name. */
5209 else
5211 tree ambiguous_decls;
5213 /* If we already know that this lookup is ambiguous, then
5214 we've already issued an error message; there's no reason
5215 to check again. */
5216 if (id_expr_token->type == CPP_NAME
5217 && id_expr_token->error_reported)
5219 cp_parser_simulate_error (parser);
5220 return error_mark_node;
5223 decl = cp_parser_lookup_name (parser, id_expression,
5224 none_type,
5225 template_p,
5226 /*is_namespace=*/false,
5227 /*check_dependency=*/true,
5228 &ambiguous_decls,
5229 id_expr_token->location);
5230 /* If the lookup was ambiguous, an error will already have
5231 been issued. */
5232 if (ambiguous_decls)
5233 return error_mark_node;
5235 /* In Objective-C++, we may have an Objective-C 2.0
5236 dot-syntax for classes here. */
5237 if (c_dialect_objc ()
5238 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5239 && TREE_CODE (decl) == TYPE_DECL
5240 && objc_is_class_name (decl))
5242 tree component;
5243 cp_lexer_consume_token (parser->lexer);
5244 component = cp_parser_identifier (parser);
5245 if (component == error_mark_node)
5246 return error_mark_node;
5248 tree result = objc_build_class_component_ref (id_expression,
5249 component);
5250 /* Build a location of the form:
5251 expr.component
5252 ~~~~~^~~~~~~~~
5253 with caret at the start of the component name (at
5254 input_location), ranging from the start of the id_expression
5255 to the end of the component name. */
5256 location_t combined_loc
5257 = make_location (input_location, id_expression.get_start (),
5258 get_finish (input_location));
5259 protected_set_expr_location (result, combined_loc);
5260 return result;
5263 /* In Objective-C++, an instance variable (ivar) may be preferred
5264 to whatever cp_parser_lookup_name() found.
5265 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5266 rest of c-family, we have to do a little extra work to preserve
5267 any location information in cp_expr "decl". Given that
5268 objc_lookup_ivar is implemented in "c-family" and "objc", we
5269 have a trip through the pure "tree" type, rather than cp_expr.
5270 Naively copying it back to "decl" would implicitly give the
5271 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5272 store an EXPR_LOCATION. Hence we only update "decl" (and
5273 hence its location_t) if we get back a different tree node. */
5274 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5275 id_expression);
5276 if (decl_tree != decl.get_value ())
5277 decl = cp_expr (decl_tree);
5279 /* If name lookup gives us a SCOPE_REF, then the
5280 qualifying scope was dependent. */
5281 if (TREE_CODE (decl) == SCOPE_REF)
5283 /* At this point, we do not know if DECL is a valid
5284 integral constant expression. We assume that it is
5285 in fact such an expression, so that code like:
5287 template <int N> struct A {
5288 int a[B<N>::i];
5291 is accepted. At template-instantiation time, we
5292 will check that B<N>::i is actually a constant. */
5293 return decl;
5295 /* Check to see if DECL is a local variable in a context
5296 where that is forbidden. */
5297 if (parser->local_variables_forbidden_p
5298 && local_variable_p (decl))
5300 /* It might be that we only found DECL because we are
5301 trying to be generous with pre-ISO scoping rules.
5302 For example, consider:
5304 int i;
5305 void g() {
5306 for (int i = 0; i < 10; ++i) {}
5307 extern void f(int j = i);
5310 Here, name look up will originally find the out
5311 of scope `i'. We need to issue a warning message,
5312 but then use the global `i'. */
5313 decl = check_for_out_of_scope_variable (decl);
5314 if (local_variable_p (decl))
5316 error_at (id_expr_token->location,
5317 "local variable %qD may not appear in this context",
5318 decl.get_value ());
5319 return error_mark_node;
5324 decl = (finish_id_expression
5325 (id_expression, decl, parser->scope,
5326 idk,
5327 parser->integral_constant_expression_p,
5328 parser->allow_non_integral_constant_expression_p,
5329 &parser->non_integral_constant_expression_p,
5330 template_p, done, address_p,
5331 template_arg_p,
5332 &error_msg,
5333 id_expression.get_location ()));
5334 if (error_msg)
5335 cp_parser_error (parser, error_msg);
5336 decl.set_location (id_expr_token->location);
5337 return decl;
5340 /* Anything else is an error. */
5341 default:
5342 cp_parser_error (parser, "expected primary-expression");
5343 return error_mark_node;
5347 static inline cp_expr
5348 cp_parser_primary_expression (cp_parser *parser,
5349 bool address_p,
5350 bool cast_p,
5351 bool template_arg_p,
5352 cp_id_kind *idk)
5354 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5355 /*decltype*/false, idk);
5358 /* Parse an id-expression.
5360 id-expression:
5361 unqualified-id
5362 qualified-id
5364 qualified-id:
5365 :: [opt] nested-name-specifier template [opt] unqualified-id
5366 :: identifier
5367 :: operator-function-id
5368 :: template-id
5370 Return a representation of the unqualified portion of the
5371 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5372 a `::' or nested-name-specifier.
5374 Often, if the id-expression was a qualified-id, the caller will
5375 want to make a SCOPE_REF to represent the qualified-id. This
5376 function does not do this in order to avoid wastefully creating
5377 SCOPE_REFs when they are not required.
5379 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5380 `template' keyword.
5382 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5383 uninstantiated templates.
5385 If *TEMPLATE_P is non-NULL, it is set to true iff the
5386 `template' keyword is used to explicitly indicate that the entity
5387 named is a template.
5389 If DECLARATOR_P is true, the id-expression is appearing as part of
5390 a declarator, rather than as part of an expression. */
5392 static cp_expr
5393 cp_parser_id_expression (cp_parser *parser,
5394 bool template_keyword_p,
5395 bool check_dependency_p,
5396 bool *template_p,
5397 bool declarator_p,
5398 bool optional_p)
5400 bool global_scope_p;
5401 bool nested_name_specifier_p;
5403 /* Assume the `template' keyword was not used. */
5404 if (template_p)
5405 *template_p = template_keyword_p;
5407 /* Look for the optional `::' operator. */
5408 global_scope_p
5409 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5410 != NULL_TREE);
5411 /* Look for the optional nested-name-specifier. */
5412 nested_name_specifier_p
5413 = (cp_parser_nested_name_specifier_opt (parser,
5414 /*typename_keyword_p=*/false,
5415 check_dependency_p,
5416 /*type_p=*/false,
5417 declarator_p)
5418 != NULL_TREE);
5419 /* If there is a nested-name-specifier, then we are looking at
5420 the first qualified-id production. */
5421 if (nested_name_specifier_p)
5423 tree saved_scope;
5424 tree saved_object_scope;
5425 tree saved_qualifying_scope;
5426 cp_expr unqualified_id;
5427 bool is_template;
5429 /* See if the next token is the `template' keyword. */
5430 if (!template_p)
5431 template_p = &is_template;
5432 *template_p = cp_parser_optional_template_keyword (parser);
5433 /* Name lookup we do during the processing of the
5434 unqualified-id might obliterate SCOPE. */
5435 saved_scope = parser->scope;
5436 saved_object_scope = parser->object_scope;
5437 saved_qualifying_scope = parser->qualifying_scope;
5438 /* Process the final unqualified-id. */
5439 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5440 check_dependency_p,
5441 declarator_p,
5442 /*optional_p=*/false);
5443 /* Restore the SAVED_SCOPE for our caller. */
5444 parser->scope = saved_scope;
5445 parser->object_scope = saved_object_scope;
5446 parser->qualifying_scope = saved_qualifying_scope;
5448 return unqualified_id;
5450 /* Otherwise, if we are in global scope, then we are looking at one
5451 of the other qualified-id productions. */
5452 else if (global_scope_p)
5454 cp_token *token;
5455 tree id;
5457 /* Peek at the next token. */
5458 token = cp_lexer_peek_token (parser->lexer);
5460 /* If it's an identifier, and the next token is not a "<", then
5461 we can avoid the template-id case. This is an optimization
5462 for this common case. */
5463 if (token->type == CPP_NAME
5464 && !cp_parser_nth_token_starts_template_argument_list_p
5465 (parser, 2))
5466 return cp_parser_identifier (parser);
5468 cp_parser_parse_tentatively (parser);
5469 /* Try a template-id. */
5470 id = cp_parser_template_id (parser,
5471 /*template_keyword_p=*/false,
5472 /*check_dependency_p=*/true,
5473 none_type,
5474 declarator_p);
5475 /* If that worked, we're done. */
5476 if (cp_parser_parse_definitely (parser))
5477 return id;
5479 /* Peek at the next token. (Changes in the token buffer may
5480 have invalidated the pointer obtained above.) */
5481 token = cp_lexer_peek_token (parser->lexer);
5483 switch (token->type)
5485 case CPP_NAME:
5486 return cp_parser_identifier (parser);
5488 case CPP_KEYWORD:
5489 if (token->keyword == RID_OPERATOR)
5490 return cp_parser_operator_function_id (parser);
5491 /* Fall through. */
5493 default:
5494 cp_parser_error (parser, "expected id-expression");
5495 return error_mark_node;
5498 else
5499 return cp_parser_unqualified_id (parser, template_keyword_p,
5500 /*check_dependency_p=*/true,
5501 declarator_p,
5502 optional_p);
5505 /* Parse an unqualified-id.
5507 unqualified-id:
5508 identifier
5509 operator-function-id
5510 conversion-function-id
5511 ~ class-name
5512 template-id
5514 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5515 keyword, in a construct like `A::template ...'.
5517 Returns a representation of unqualified-id. For the `identifier'
5518 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5519 production a BIT_NOT_EXPR is returned; the operand of the
5520 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5521 other productions, see the documentation accompanying the
5522 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5523 names are looked up in uninstantiated templates. If DECLARATOR_P
5524 is true, the unqualified-id is appearing as part of a declarator,
5525 rather than as part of an expression. */
5527 static cp_expr
5528 cp_parser_unqualified_id (cp_parser* parser,
5529 bool template_keyword_p,
5530 bool check_dependency_p,
5531 bool declarator_p,
5532 bool optional_p)
5534 cp_token *token;
5536 /* Peek at the next token. */
5537 token = cp_lexer_peek_token (parser->lexer);
5539 switch ((int) token->type)
5541 case CPP_NAME:
5543 tree id;
5545 /* We don't know yet whether or not this will be a
5546 template-id. */
5547 cp_parser_parse_tentatively (parser);
5548 /* Try a template-id. */
5549 id = cp_parser_template_id (parser, template_keyword_p,
5550 check_dependency_p,
5551 none_type,
5552 declarator_p);
5553 /* If it worked, we're done. */
5554 if (cp_parser_parse_definitely (parser))
5555 return id;
5556 /* Otherwise, it's an ordinary identifier. */
5557 return cp_parser_identifier (parser);
5560 case CPP_TEMPLATE_ID:
5561 return cp_parser_template_id (parser, template_keyword_p,
5562 check_dependency_p,
5563 none_type,
5564 declarator_p);
5566 case CPP_COMPL:
5568 tree type_decl;
5569 tree qualifying_scope;
5570 tree object_scope;
5571 tree scope;
5572 bool done;
5574 /* Consume the `~' token. */
5575 cp_lexer_consume_token (parser->lexer);
5576 /* Parse the class-name. The standard, as written, seems to
5577 say that:
5579 template <typename T> struct S { ~S (); };
5580 template <typename T> S<T>::~S() {}
5582 is invalid, since `~' must be followed by a class-name, but
5583 `S<T>' is dependent, and so not known to be a class.
5584 That's not right; we need to look in uninstantiated
5585 templates. A further complication arises from:
5587 template <typename T> void f(T t) {
5588 t.T::~T();
5591 Here, it is not possible to look up `T' in the scope of `T'
5592 itself. We must look in both the current scope, and the
5593 scope of the containing complete expression.
5595 Yet another issue is:
5597 struct S {
5598 int S;
5599 ~S();
5602 S::~S() {}
5604 The standard does not seem to say that the `S' in `~S'
5605 should refer to the type `S' and not the data member
5606 `S::S'. */
5608 /* DR 244 says that we look up the name after the "~" in the
5609 same scope as we looked up the qualifying name. That idea
5610 isn't fully worked out; it's more complicated than that. */
5611 scope = parser->scope;
5612 object_scope = parser->object_scope;
5613 qualifying_scope = parser->qualifying_scope;
5615 /* Check for invalid scopes. */
5616 if (scope == error_mark_node)
5618 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5619 cp_lexer_consume_token (parser->lexer);
5620 return error_mark_node;
5622 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5624 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5625 error_at (token->location,
5626 "scope %qT before %<~%> is not a class-name",
5627 scope);
5628 cp_parser_simulate_error (parser);
5629 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5630 cp_lexer_consume_token (parser->lexer);
5631 return error_mark_node;
5633 gcc_assert (!scope || TYPE_P (scope));
5635 /* If the name is of the form "X::~X" it's OK even if X is a
5636 typedef. */
5637 token = cp_lexer_peek_token (parser->lexer);
5638 if (scope
5639 && token->type == CPP_NAME
5640 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5641 != CPP_LESS)
5642 && (token->u.value == TYPE_IDENTIFIER (scope)
5643 || (CLASS_TYPE_P (scope)
5644 && constructor_name_p (token->u.value, scope))))
5646 cp_lexer_consume_token (parser->lexer);
5647 return build_nt (BIT_NOT_EXPR, scope);
5650 /* ~auto means the destructor of whatever the object is. */
5651 if (cp_parser_is_keyword (token, RID_AUTO))
5653 if (cxx_dialect < cxx14)
5654 pedwarn (input_location, 0,
5655 "%<~auto%> only available with "
5656 "-std=c++14 or -std=gnu++14");
5657 cp_lexer_consume_token (parser->lexer);
5658 return build_nt (BIT_NOT_EXPR, make_auto ());
5661 /* If there was an explicit qualification (S::~T), first look
5662 in the scope given by the qualification (i.e., S).
5664 Note: in the calls to cp_parser_class_name below we pass
5665 typename_type so that lookup finds the injected-class-name
5666 rather than the constructor. */
5667 done = false;
5668 type_decl = NULL_TREE;
5669 if (scope)
5671 cp_parser_parse_tentatively (parser);
5672 type_decl = cp_parser_class_name (parser,
5673 /*typename_keyword_p=*/false,
5674 /*template_keyword_p=*/false,
5675 typename_type,
5676 /*check_dependency=*/false,
5677 /*class_head_p=*/false,
5678 declarator_p);
5679 if (cp_parser_parse_definitely (parser))
5680 done = true;
5682 /* In "N::S::~S", look in "N" as well. */
5683 if (!done && scope && qualifying_scope)
5685 cp_parser_parse_tentatively (parser);
5686 parser->scope = qualifying_scope;
5687 parser->object_scope = NULL_TREE;
5688 parser->qualifying_scope = NULL_TREE;
5689 type_decl
5690 = cp_parser_class_name (parser,
5691 /*typename_keyword_p=*/false,
5692 /*template_keyword_p=*/false,
5693 typename_type,
5694 /*check_dependency=*/false,
5695 /*class_head_p=*/false,
5696 declarator_p);
5697 if (cp_parser_parse_definitely (parser))
5698 done = true;
5700 /* In "p->S::~T", look in the scope given by "*p" as well. */
5701 else if (!done && object_scope)
5703 cp_parser_parse_tentatively (parser);
5704 parser->scope = object_scope;
5705 parser->object_scope = NULL_TREE;
5706 parser->qualifying_scope = NULL_TREE;
5707 type_decl
5708 = cp_parser_class_name (parser,
5709 /*typename_keyword_p=*/false,
5710 /*template_keyword_p=*/false,
5711 typename_type,
5712 /*check_dependency=*/false,
5713 /*class_head_p=*/false,
5714 declarator_p);
5715 if (cp_parser_parse_definitely (parser))
5716 done = true;
5718 /* Look in the surrounding context. */
5719 if (!done)
5721 parser->scope = NULL_TREE;
5722 parser->object_scope = NULL_TREE;
5723 parser->qualifying_scope = NULL_TREE;
5724 if (processing_template_decl)
5725 cp_parser_parse_tentatively (parser);
5726 type_decl
5727 = cp_parser_class_name (parser,
5728 /*typename_keyword_p=*/false,
5729 /*template_keyword_p=*/false,
5730 typename_type,
5731 /*check_dependency=*/false,
5732 /*class_head_p=*/false,
5733 declarator_p);
5734 if (processing_template_decl
5735 && ! cp_parser_parse_definitely (parser))
5737 /* We couldn't find a type with this name. If we're parsing
5738 tentatively, fail and try something else. */
5739 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5741 cp_parser_simulate_error (parser);
5742 return error_mark_node;
5744 /* Otherwise, accept it and check for a match at instantiation
5745 time. */
5746 type_decl = cp_parser_identifier (parser);
5747 if (type_decl != error_mark_node)
5748 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5749 return type_decl;
5752 /* If an error occurred, assume that the name of the
5753 destructor is the same as the name of the qualifying
5754 class. That allows us to keep parsing after running
5755 into ill-formed destructor names. */
5756 if (type_decl == error_mark_node && scope)
5757 return build_nt (BIT_NOT_EXPR, scope);
5758 else if (type_decl == error_mark_node)
5759 return error_mark_node;
5761 /* Check that destructor name and scope match. */
5762 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5764 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5765 error_at (token->location,
5766 "declaration of %<~%T%> as member of %qT",
5767 type_decl, scope);
5768 cp_parser_simulate_error (parser);
5769 return error_mark_node;
5772 /* [class.dtor]
5774 A typedef-name that names a class shall not be used as the
5775 identifier in the declarator for a destructor declaration. */
5776 if (declarator_p
5777 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5778 && !DECL_SELF_REFERENCE_P (type_decl)
5779 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5780 error_at (token->location,
5781 "typedef-name %qD used as destructor declarator",
5782 type_decl);
5784 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5787 case CPP_KEYWORD:
5788 if (token->keyword == RID_OPERATOR)
5790 cp_expr id;
5792 /* This could be a template-id, so we try that first. */
5793 cp_parser_parse_tentatively (parser);
5794 /* Try a template-id. */
5795 id = cp_parser_template_id (parser, template_keyword_p,
5796 /*check_dependency_p=*/true,
5797 none_type,
5798 declarator_p);
5799 /* If that worked, we're done. */
5800 if (cp_parser_parse_definitely (parser))
5801 return id;
5802 /* We still don't know whether we're looking at an
5803 operator-function-id or a conversion-function-id. */
5804 cp_parser_parse_tentatively (parser);
5805 /* Try an operator-function-id. */
5806 id = cp_parser_operator_function_id (parser);
5807 /* If that didn't work, try a conversion-function-id. */
5808 if (!cp_parser_parse_definitely (parser))
5809 id = cp_parser_conversion_function_id (parser);
5810 else if (UDLIT_OPER_P (id))
5812 /* 17.6.3.3.5 */
5813 const char *name = UDLIT_OP_SUFFIX (id);
5814 if (name[0] != '_' && !in_system_header_at (input_location)
5815 && declarator_p)
5816 warning (OPT_Wliteral_suffix,
5817 "literal operator suffixes not preceded by %<_%>"
5818 " are reserved for future standardization");
5821 return id;
5823 /* Fall through. */
5825 default:
5826 if (optional_p)
5827 return NULL_TREE;
5828 cp_parser_error (parser, "expected unqualified-id");
5829 return error_mark_node;
5833 /* Parse an (optional) nested-name-specifier.
5835 nested-name-specifier: [C++98]
5836 class-or-namespace-name :: nested-name-specifier [opt]
5837 class-or-namespace-name :: template nested-name-specifier [opt]
5839 nested-name-specifier: [C++0x]
5840 type-name ::
5841 namespace-name ::
5842 nested-name-specifier identifier ::
5843 nested-name-specifier template [opt] simple-template-id ::
5845 PARSER->SCOPE should be set appropriately before this function is
5846 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5847 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5848 in name lookups.
5850 Sets PARSER->SCOPE to the class (TYPE) or namespace
5851 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5852 it unchanged if there is no nested-name-specifier. Returns the new
5853 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5855 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5856 part of a declaration and/or decl-specifier. */
5858 static tree
5859 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5860 bool typename_keyword_p,
5861 bool check_dependency_p,
5862 bool type_p,
5863 bool is_declaration)
5865 bool success = false;
5866 cp_token_position start = 0;
5867 cp_token *token;
5869 /* Remember where the nested-name-specifier starts. */
5870 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5872 start = cp_lexer_token_position (parser->lexer, false);
5873 push_deferring_access_checks (dk_deferred);
5876 while (true)
5878 tree new_scope;
5879 tree old_scope;
5880 tree saved_qualifying_scope;
5881 bool template_keyword_p;
5883 /* Spot cases that cannot be the beginning of a
5884 nested-name-specifier. */
5885 token = cp_lexer_peek_token (parser->lexer);
5887 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5888 the already parsed nested-name-specifier. */
5889 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5891 /* Grab the nested-name-specifier and continue the loop. */
5892 cp_parser_pre_parsed_nested_name_specifier (parser);
5893 /* If we originally encountered this nested-name-specifier
5894 with IS_DECLARATION set to false, we will not have
5895 resolved TYPENAME_TYPEs, so we must do so here. */
5896 if (is_declaration
5897 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5899 new_scope = resolve_typename_type (parser->scope,
5900 /*only_current_p=*/false);
5901 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5902 parser->scope = new_scope;
5904 success = true;
5905 continue;
5908 /* Spot cases that cannot be the beginning of a
5909 nested-name-specifier. On the second and subsequent times
5910 through the loop, we look for the `template' keyword. */
5911 if (success && token->keyword == RID_TEMPLATE)
5913 /* A template-id can start a nested-name-specifier. */
5914 else if (token->type == CPP_TEMPLATE_ID)
5916 /* DR 743: decltype can be used in a nested-name-specifier. */
5917 else if (token_is_decltype (token))
5919 else
5921 /* If the next token is not an identifier, then it is
5922 definitely not a type-name or namespace-name. */
5923 if (token->type != CPP_NAME)
5924 break;
5925 /* If the following token is neither a `<' (to begin a
5926 template-id), nor a `::', then we are not looking at a
5927 nested-name-specifier. */
5928 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5930 if (token->type == CPP_COLON
5931 && parser->colon_corrects_to_scope_p
5932 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5934 error_at (token->location,
5935 "found %<:%> in nested-name-specifier, expected %<::%>");
5936 token->type = CPP_SCOPE;
5939 if (token->type != CPP_SCOPE
5940 && !cp_parser_nth_token_starts_template_argument_list_p
5941 (parser, 2))
5942 break;
5945 /* The nested-name-specifier is optional, so we parse
5946 tentatively. */
5947 cp_parser_parse_tentatively (parser);
5949 /* Look for the optional `template' keyword, if this isn't the
5950 first time through the loop. */
5951 if (success)
5952 template_keyword_p = cp_parser_optional_template_keyword (parser);
5953 else
5954 template_keyword_p = false;
5956 /* Save the old scope since the name lookup we are about to do
5957 might destroy it. */
5958 old_scope = parser->scope;
5959 saved_qualifying_scope = parser->qualifying_scope;
5960 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5961 look up names in "X<T>::I" in order to determine that "Y" is
5962 a template. So, if we have a typename at this point, we make
5963 an effort to look through it. */
5964 if (is_declaration
5965 && !typename_keyword_p
5966 && parser->scope
5967 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5968 parser->scope = resolve_typename_type (parser->scope,
5969 /*only_current_p=*/false);
5970 /* Parse the qualifying entity. */
5971 new_scope
5972 = cp_parser_qualifying_entity (parser,
5973 typename_keyword_p,
5974 template_keyword_p,
5975 check_dependency_p,
5976 type_p,
5977 is_declaration);
5978 /* Look for the `::' token. */
5979 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5981 /* If we found what we wanted, we keep going; otherwise, we're
5982 done. */
5983 if (!cp_parser_parse_definitely (parser))
5985 bool error_p = false;
5987 /* Restore the OLD_SCOPE since it was valid before the
5988 failed attempt at finding the last
5989 class-or-namespace-name. */
5990 parser->scope = old_scope;
5991 parser->qualifying_scope = saved_qualifying_scope;
5993 /* If the next token is a decltype, and the one after that is a
5994 `::', then the decltype has failed to resolve to a class or
5995 enumeration type. Give this error even when parsing
5996 tentatively since it can't possibly be valid--and we're going
5997 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5998 won't get another chance.*/
5999 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6000 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6001 == CPP_SCOPE))
6003 token = cp_lexer_consume_token (parser->lexer);
6004 error_at (token->location, "decltype evaluates to %qT, "
6005 "which is not a class or enumeration type",
6006 token->u.tree_check_value->value);
6007 parser->scope = error_mark_node;
6008 error_p = true;
6009 /* As below. */
6010 success = true;
6011 cp_lexer_consume_token (parser->lexer);
6014 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6015 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6017 /* If we have a non-type template-id followed by ::, it can't
6018 possibly be valid. */
6019 token = cp_lexer_peek_token (parser->lexer);
6020 tree tid = token->u.tree_check_value->value;
6021 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6022 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6024 tree tmpl = NULL_TREE;
6025 if (is_overloaded_fn (tid))
6027 tree fns = get_fns (tid);
6028 if (!OVL_CHAIN (fns))
6029 tmpl = OVL_CURRENT (fns);
6030 error_at (token->location, "function template-id %qD "
6031 "in nested-name-specifier", tid);
6033 else
6035 /* Variable template. */
6036 tmpl = TREE_OPERAND (tid, 0);
6037 gcc_assert (variable_template_p (tmpl));
6038 error_at (token->location, "variable template-id %qD "
6039 "in nested-name-specifier", tid);
6041 if (tmpl)
6042 inform (DECL_SOURCE_LOCATION (tmpl),
6043 "%qD declared here", tmpl);
6045 parser->scope = error_mark_node;
6046 error_p = true;
6047 /* As below. */
6048 success = true;
6049 cp_lexer_consume_token (parser->lexer);
6050 cp_lexer_consume_token (parser->lexer);
6054 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6055 break;
6056 /* If the next token is an identifier, and the one after
6057 that is a `::', then any valid interpretation would have
6058 found a class-or-namespace-name. */
6059 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6060 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6061 == CPP_SCOPE)
6062 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6063 != CPP_COMPL))
6065 token = cp_lexer_consume_token (parser->lexer);
6066 if (!error_p)
6068 if (!token->error_reported)
6070 tree decl;
6071 tree ambiguous_decls;
6073 decl = cp_parser_lookup_name (parser, token->u.value,
6074 none_type,
6075 /*is_template=*/false,
6076 /*is_namespace=*/false,
6077 /*check_dependency=*/true,
6078 &ambiguous_decls,
6079 token->location);
6080 if (TREE_CODE (decl) == TEMPLATE_DECL)
6081 error_at (token->location,
6082 "%qD used without template parameters",
6083 decl);
6084 else if (ambiguous_decls)
6086 // cp_parser_lookup_name has the same diagnostic,
6087 // thus make sure to emit it at most once.
6088 if (cp_parser_uncommitted_to_tentative_parse_p
6089 (parser))
6091 error_at (token->location,
6092 "reference to %qD is ambiguous",
6093 token->u.value);
6094 print_candidates (ambiguous_decls);
6096 decl = error_mark_node;
6098 else
6100 if (cxx_dialect != cxx98)
6101 cp_parser_name_lookup_error
6102 (parser, token->u.value, decl, NLE_NOT_CXX98,
6103 token->location);
6104 else
6105 cp_parser_name_lookup_error
6106 (parser, token->u.value, decl, NLE_CXX98,
6107 token->location);
6110 parser->scope = error_mark_node;
6111 error_p = true;
6112 /* Treat this as a successful nested-name-specifier
6113 due to:
6115 [basic.lookup.qual]
6117 If the name found is not a class-name (clause
6118 _class_) or namespace-name (_namespace.def_), the
6119 program is ill-formed. */
6120 success = true;
6122 cp_lexer_consume_token (parser->lexer);
6124 break;
6126 /* We've found one valid nested-name-specifier. */
6127 success = true;
6128 /* Name lookup always gives us a DECL. */
6129 if (TREE_CODE (new_scope) == TYPE_DECL)
6130 new_scope = TREE_TYPE (new_scope);
6131 /* Uses of "template" must be followed by actual templates. */
6132 if (template_keyword_p
6133 && !(CLASS_TYPE_P (new_scope)
6134 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6135 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6136 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6137 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6138 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6139 == TEMPLATE_ID_EXPR)))
6140 permerror (input_location, TYPE_P (new_scope)
6141 ? G_("%qT is not a template")
6142 : G_("%qD is not a template"),
6143 new_scope);
6144 /* If it is a class scope, try to complete it; we are about to
6145 be looking up names inside the class. */
6146 if (TYPE_P (new_scope)
6147 /* Since checking types for dependency can be expensive,
6148 avoid doing it if the type is already complete. */
6149 && !COMPLETE_TYPE_P (new_scope)
6150 /* Do not try to complete dependent types. */
6151 && !dependent_type_p (new_scope))
6153 new_scope = complete_type (new_scope);
6154 /* If it is a typedef to current class, use the current
6155 class instead, as the typedef won't have any names inside
6156 it yet. */
6157 if (!COMPLETE_TYPE_P (new_scope)
6158 && currently_open_class (new_scope))
6159 new_scope = TYPE_MAIN_VARIANT (new_scope);
6161 /* Make sure we look in the right scope the next time through
6162 the loop. */
6163 parser->scope = new_scope;
6166 /* If parsing tentatively, replace the sequence of tokens that makes
6167 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6168 token. That way, should we re-parse the token stream, we will
6169 not have to repeat the effort required to do the parse, nor will
6170 we issue duplicate error messages. */
6171 if (success && start)
6173 cp_token *token;
6175 token = cp_lexer_token_at (parser->lexer, start);
6176 /* Reset the contents of the START token. */
6177 token->type = CPP_NESTED_NAME_SPECIFIER;
6178 /* Retrieve any deferred checks. Do not pop this access checks yet
6179 so the memory will not be reclaimed during token replacing below. */
6180 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6181 token->u.tree_check_value->value = parser->scope;
6182 token->u.tree_check_value->checks = get_deferred_access_checks ();
6183 token->u.tree_check_value->qualifying_scope =
6184 parser->qualifying_scope;
6185 token->keyword = RID_MAX;
6187 /* Purge all subsequent tokens. */
6188 cp_lexer_purge_tokens_after (parser->lexer, start);
6191 if (start)
6192 pop_to_parent_deferring_access_checks ();
6194 return success ? parser->scope : NULL_TREE;
6197 /* Parse a nested-name-specifier. See
6198 cp_parser_nested_name_specifier_opt for details. This function
6199 behaves identically, except that it will an issue an error if no
6200 nested-name-specifier is present. */
6202 static tree
6203 cp_parser_nested_name_specifier (cp_parser *parser,
6204 bool typename_keyword_p,
6205 bool check_dependency_p,
6206 bool type_p,
6207 bool is_declaration)
6209 tree scope;
6211 /* Look for the nested-name-specifier. */
6212 scope = cp_parser_nested_name_specifier_opt (parser,
6213 typename_keyword_p,
6214 check_dependency_p,
6215 type_p,
6216 is_declaration);
6217 /* If it was not present, issue an error message. */
6218 if (!scope)
6220 cp_parser_error (parser, "expected nested-name-specifier");
6221 parser->scope = NULL_TREE;
6224 return scope;
6227 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6228 this is either a class-name or a namespace-name (which corresponds
6229 to the class-or-namespace-name production in the grammar). For
6230 C++0x, it can also be a type-name that refers to an enumeration
6231 type or a simple-template-id.
6233 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6234 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6235 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6236 TYPE_P is TRUE iff the next name should be taken as a class-name,
6237 even the same name is declared to be another entity in the same
6238 scope.
6240 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6241 specified by the class-or-namespace-name. If neither is found the
6242 ERROR_MARK_NODE is returned. */
6244 static tree
6245 cp_parser_qualifying_entity (cp_parser *parser,
6246 bool typename_keyword_p,
6247 bool template_keyword_p,
6248 bool check_dependency_p,
6249 bool type_p,
6250 bool is_declaration)
6252 tree saved_scope;
6253 tree saved_qualifying_scope;
6254 tree saved_object_scope;
6255 tree scope;
6256 bool only_class_p;
6257 bool successful_parse_p;
6259 /* DR 743: decltype can appear in a nested-name-specifier. */
6260 if (cp_lexer_next_token_is_decltype (parser->lexer))
6262 scope = cp_parser_decltype (parser);
6263 if (TREE_CODE (scope) != ENUMERAL_TYPE
6264 && !MAYBE_CLASS_TYPE_P (scope))
6266 cp_parser_simulate_error (parser);
6267 return error_mark_node;
6269 if (TYPE_NAME (scope))
6270 scope = TYPE_NAME (scope);
6271 return scope;
6274 /* Before we try to parse the class-name, we must save away the
6275 current PARSER->SCOPE since cp_parser_class_name will destroy
6276 it. */
6277 saved_scope = parser->scope;
6278 saved_qualifying_scope = parser->qualifying_scope;
6279 saved_object_scope = parser->object_scope;
6280 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6281 there is no need to look for a namespace-name. */
6282 only_class_p = template_keyword_p
6283 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6284 if (!only_class_p)
6285 cp_parser_parse_tentatively (parser);
6286 scope = cp_parser_class_name (parser,
6287 typename_keyword_p,
6288 template_keyword_p,
6289 type_p ? class_type : none_type,
6290 check_dependency_p,
6291 /*class_head_p=*/false,
6292 is_declaration,
6293 /*enum_ok=*/cxx_dialect > cxx98);
6294 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6295 /* If that didn't work, try for a namespace-name. */
6296 if (!only_class_p && !successful_parse_p)
6298 /* Restore the saved scope. */
6299 parser->scope = saved_scope;
6300 parser->qualifying_scope = saved_qualifying_scope;
6301 parser->object_scope = saved_object_scope;
6302 /* If we are not looking at an identifier followed by the scope
6303 resolution operator, then this is not part of a
6304 nested-name-specifier. (Note that this function is only used
6305 to parse the components of a nested-name-specifier.) */
6306 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6307 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6308 return error_mark_node;
6309 scope = cp_parser_namespace_name (parser);
6312 return scope;
6315 /* Return true if we are looking at a compound-literal, false otherwise. */
6317 static bool
6318 cp_parser_compound_literal_p (cp_parser *parser)
6320 /* Consume the `('. */
6321 cp_lexer_consume_token (parser->lexer);
6323 cp_lexer_save_tokens (parser->lexer);
6325 /* Skip tokens until the next token is a closing parenthesis.
6326 If we find the closing `)', and the next token is a `{', then
6327 we are looking at a compound-literal. */
6328 bool compound_literal_p
6329 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6330 /*consume_paren=*/true)
6331 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6333 /* Roll back the tokens we skipped. */
6334 cp_lexer_rollback_tokens (parser->lexer);
6336 return compound_literal_p;
6339 /* Parse a postfix-expression.
6341 postfix-expression:
6342 primary-expression
6343 postfix-expression [ expression ]
6344 postfix-expression ( expression-list [opt] )
6345 simple-type-specifier ( expression-list [opt] )
6346 typename :: [opt] nested-name-specifier identifier
6347 ( expression-list [opt] )
6348 typename :: [opt] nested-name-specifier template [opt] template-id
6349 ( expression-list [opt] )
6350 postfix-expression . template [opt] id-expression
6351 postfix-expression -> template [opt] id-expression
6352 postfix-expression . pseudo-destructor-name
6353 postfix-expression -> pseudo-destructor-name
6354 postfix-expression ++
6355 postfix-expression --
6356 dynamic_cast < type-id > ( expression )
6357 static_cast < type-id > ( expression )
6358 reinterpret_cast < type-id > ( expression )
6359 const_cast < type-id > ( expression )
6360 typeid ( expression )
6361 typeid ( type-id )
6363 GNU Extension:
6365 postfix-expression:
6366 ( type-id ) { initializer-list , [opt] }
6368 This extension is a GNU version of the C99 compound-literal
6369 construct. (The C99 grammar uses `type-name' instead of `type-id',
6370 but they are essentially the same concept.)
6372 If ADDRESS_P is true, the postfix expression is the operand of the
6373 `&' operator. CAST_P is true if this expression is the target of a
6374 cast.
6376 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6377 class member access expressions [expr.ref].
6379 Returns a representation of the expression. */
6381 static cp_expr
6382 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6383 bool member_access_only_p, bool decltype_p,
6384 cp_id_kind * pidk_return)
6386 cp_token *token;
6387 location_t loc;
6388 enum rid keyword;
6389 cp_id_kind idk = CP_ID_KIND_NONE;
6390 cp_expr postfix_expression = NULL_TREE;
6391 bool is_member_access = false;
6392 int saved_in_statement = -1;
6394 /* Peek at the next token. */
6395 token = cp_lexer_peek_token (parser->lexer);
6396 loc = token->location;
6397 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6399 /* Some of the productions are determined by keywords. */
6400 keyword = token->keyword;
6401 switch (keyword)
6403 case RID_DYNCAST:
6404 case RID_STATCAST:
6405 case RID_REINTCAST:
6406 case RID_CONSTCAST:
6408 tree type;
6409 cp_expr expression;
6410 const char *saved_message;
6411 bool saved_in_type_id_in_expr_p;
6413 /* All of these can be handled in the same way from the point
6414 of view of parsing. Begin by consuming the token
6415 identifying the cast. */
6416 cp_lexer_consume_token (parser->lexer);
6418 /* New types cannot be defined in the cast. */
6419 saved_message = parser->type_definition_forbidden_message;
6420 parser->type_definition_forbidden_message
6421 = G_("types may not be defined in casts");
6423 /* Look for the opening `<'. */
6424 cp_parser_require (parser, CPP_LESS, RT_LESS);
6425 /* Parse the type to which we are casting. */
6426 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6427 parser->in_type_id_in_expr_p = true;
6428 type = cp_parser_type_id (parser);
6429 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6430 /* Look for the closing `>'. */
6431 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6432 /* Restore the old message. */
6433 parser->type_definition_forbidden_message = saved_message;
6435 bool saved_greater_than_is_operator_p
6436 = parser->greater_than_is_operator_p;
6437 parser->greater_than_is_operator_p = true;
6439 /* And the expression which is being cast. */
6440 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6441 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6442 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6443 RT_CLOSE_PAREN);
6444 location_t end_loc = close_paren ?
6445 close_paren->location : UNKNOWN_LOCATION;
6447 parser->greater_than_is_operator_p
6448 = saved_greater_than_is_operator_p;
6450 /* Only type conversions to integral or enumeration types
6451 can be used in constant-expressions. */
6452 if (!cast_valid_in_integral_constant_expression_p (type)
6453 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6455 postfix_expression = error_mark_node;
6456 break;
6459 switch (keyword)
6461 case RID_DYNCAST:
6462 postfix_expression
6463 = build_dynamic_cast (type, expression, tf_warning_or_error);
6464 break;
6465 case RID_STATCAST:
6466 postfix_expression
6467 = build_static_cast (type, expression, tf_warning_or_error);
6468 break;
6469 case RID_REINTCAST:
6470 postfix_expression
6471 = build_reinterpret_cast (type, expression,
6472 tf_warning_or_error);
6473 break;
6474 case RID_CONSTCAST:
6475 postfix_expression
6476 = build_const_cast (type, expression, tf_warning_or_error);
6477 break;
6478 default:
6479 gcc_unreachable ();
6482 /* Construct a location e.g. :
6483 reinterpret_cast <int *> (expr)
6484 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6485 ranging from the start of the "*_cast" token to the final closing
6486 paren, with the caret at the start. */
6487 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6488 postfix_expression.set_location (cp_cast_loc);
6490 break;
6492 case RID_TYPEID:
6494 tree type;
6495 const char *saved_message;
6496 bool saved_in_type_id_in_expr_p;
6498 /* Consume the `typeid' token. */
6499 cp_lexer_consume_token (parser->lexer);
6500 /* Look for the `(' token. */
6501 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6502 /* Types cannot be defined in a `typeid' expression. */
6503 saved_message = parser->type_definition_forbidden_message;
6504 parser->type_definition_forbidden_message
6505 = G_("types may not be defined in a %<typeid%> expression");
6506 /* We can't be sure yet whether we're looking at a type-id or an
6507 expression. */
6508 cp_parser_parse_tentatively (parser);
6509 /* Try a type-id first. */
6510 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6511 parser->in_type_id_in_expr_p = true;
6512 type = cp_parser_type_id (parser);
6513 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6514 /* Look for the `)' token. Otherwise, we can't be sure that
6515 we're not looking at an expression: consider `typeid (int
6516 (3))', for example. */
6517 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6518 /* If all went well, simply lookup the type-id. */
6519 if (cp_parser_parse_definitely (parser))
6520 postfix_expression = get_typeid (type, tf_warning_or_error);
6521 /* Otherwise, fall back to the expression variant. */
6522 else
6524 tree expression;
6526 /* Look for an expression. */
6527 expression = cp_parser_expression (parser, & idk);
6528 /* Compute its typeid. */
6529 postfix_expression = build_typeid (expression, tf_warning_or_error);
6530 /* Look for the `)' token. */
6531 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6533 /* Restore the saved message. */
6534 parser->type_definition_forbidden_message = saved_message;
6535 /* `typeid' may not appear in an integral constant expression. */
6536 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6537 postfix_expression = error_mark_node;
6539 break;
6541 case RID_TYPENAME:
6543 tree type;
6544 /* The syntax permitted here is the same permitted for an
6545 elaborated-type-specifier. */
6546 ++parser->prevent_constrained_type_specifiers;
6547 type = cp_parser_elaborated_type_specifier (parser,
6548 /*is_friend=*/false,
6549 /*is_declaration=*/false);
6550 --parser->prevent_constrained_type_specifiers;
6551 postfix_expression = cp_parser_functional_cast (parser, type);
6553 break;
6555 case RID_CILK_SPAWN:
6557 location_t cilk_spawn_loc
6558 = cp_lexer_peek_token (parser->lexer)->location;
6559 cp_lexer_consume_token (parser->lexer);
6560 token = cp_lexer_peek_token (parser->lexer);
6561 if (token->type == CPP_SEMICOLON)
6563 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6564 "an expression");
6565 postfix_expression = error_mark_node;
6566 break;
6568 else if (!current_function_decl)
6570 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6571 "inside a function");
6572 postfix_expression = error_mark_node;
6573 break;
6575 else
6577 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6578 saved_in_statement = parser->in_statement;
6579 parser->in_statement |= IN_CILK_SPAWN;
6581 cfun->calls_cilk_spawn = 1;
6582 postfix_expression =
6583 cp_parser_postfix_expression (parser, false, false,
6584 false, false, &idk);
6585 if (!flag_cilkplus)
6587 error_at (token->location, "-fcilkplus must be enabled to use"
6588 " %<_Cilk_spawn%>");
6589 cfun->calls_cilk_spawn = 0;
6591 else if (saved_in_statement & IN_CILK_SPAWN)
6593 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6594 "are not permitted");
6595 postfix_expression = error_mark_node;
6596 cfun->calls_cilk_spawn = 0;
6598 else
6600 location_t loc = postfix_expression.get_location ();
6601 postfix_expression = build_cilk_spawn (token->location,
6602 postfix_expression);
6603 /* Build a location of the form:
6604 _Cilk_spawn expr
6605 ~~~~~~~~~~~~^~~~
6606 with caret at the expr, ranging from the start of the
6607 _Cilk_spawn token to the end of the expression. */
6608 location_t combined_loc =
6609 make_location (loc, cilk_spawn_loc, get_finish (loc));
6610 postfix_expression.set_location (combined_loc);
6611 if (postfix_expression != error_mark_node)
6612 SET_EXPR_LOCATION (postfix_expression, input_location);
6613 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6615 break;
6618 case RID_ADDRESSOF:
6619 case RID_BUILTIN_SHUFFLE:
6620 case RID_BUILTIN_LAUNDER:
6622 vec<tree, va_gc> *vec;
6623 unsigned int i;
6624 tree p;
6626 cp_lexer_consume_token (parser->lexer);
6627 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6628 /*cast_p=*/false, /*allow_expansion_p=*/true,
6629 /*non_constant_p=*/NULL);
6630 if (vec == NULL)
6632 postfix_expression = error_mark_node;
6633 break;
6636 FOR_EACH_VEC_ELT (*vec, i, p)
6637 mark_exp_read (p);
6639 switch (keyword)
6641 case RID_ADDRESSOF:
6642 if (vec->length () == 1)
6643 postfix_expression
6644 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6645 else
6647 error_at (loc, "wrong number of arguments to "
6648 "%<__builtin_addressof%>");
6649 postfix_expression = error_mark_node;
6651 break;
6653 case RID_BUILTIN_LAUNDER:
6654 if (vec->length () == 1)
6655 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6656 tf_warning_or_error);
6657 else
6659 error_at (loc, "wrong number of arguments to "
6660 "%<__builtin_launder%>");
6661 postfix_expression = error_mark_node;
6663 break;
6665 case RID_BUILTIN_SHUFFLE:
6666 if (vec->length () == 2)
6667 postfix_expression
6668 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6669 (*vec)[1], tf_warning_or_error);
6670 else if (vec->length () == 3)
6671 postfix_expression
6672 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6673 (*vec)[2], tf_warning_or_error);
6674 else
6676 error_at (loc, "wrong number of arguments to "
6677 "%<__builtin_shuffle%>");
6678 postfix_expression = error_mark_node;
6680 break;
6682 default:
6683 gcc_unreachable ();
6685 break;
6688 default:
6690 tree type;
6692 /* If the next thing is a simple-type-specifier, we may be
6693 looking at a functional cast. We could also be looking at
6694 an id-expression. So, we try the functional cast, and if
6695 that doesn't work we fall back to the primary-expression. */
6696 cp_parser_parse_tentatively (parser);
6697 /* Look for the simple-type-specifier. */
6698 ++parser->prevent_constrained_type_specifiers;
6699 type = cp_parser_simple_type_specifier (parser,
6700 /*decl_specs=*/NULL,
6701 CP_PARSER_FLAGS_NONE);
6702 --parser->prevent_constrained_type_specifiers;
6703 /* Parse the cast itself. */
6704 if (!cp_parser_error_occurred (parser))
6705 postfix_expression
6706 = cp_parser_functional_cast (parser, type);
6707 /* If that worked, we're done. */
6708 if (cp_parser_parse_definitely (parser))
6709 break;
6711 /* If the functional-cast didn't work out, try a
6712 compound-literal. */
6713 if (cp_parser_allow_gnu_extensions_p (parser)
6714 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6716 cp_expr initializer = NULL_TREE;
6718 cp_parser_parse_tentatively (parser);
6720 /* Avoid calling cp_parser_type_id pointlessly, see comment
6721 in cp_parser_cast_expression about c++/29234. */
6722 if (!cp_parser_compound_literal_p (parser))
6723 cp_parser_simulate_error (parser);
6724 else
6726 /* Parse the type. */
6727 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6728 parser->in_type_id_in_expr_p = true;
6729 type = cp_parser_type_id (parser);
6730 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6731 /* Look for the `)'. */
6732 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6735 /* If things aren't going well, there's no need to
6736 keep going. */
6737 if (!cp_parser_error_occurred (parser))
6739 bool non_constant_p;
6740 /* Parse the brace-enclosed initializer list. */
6741 initializer = cp_parser_braced_list (parser,
6742 &non_constant_p);
6744 /* If that worked, we're definitely looking at a
6745 compound-literal expression. */
6746 if (cp_parser_parse_definitely (parser))
6748 /* Warn the user that a compound literal is not
6749 allowed in standard C++. */
6750 pedwarn (input_location, OPT_Wpedantic,
6751 "ISO C++ forbids compound-literals");
6752 /* For simplicity, we disallow compound literals in
6753 constant-expressions. We could
6754 allow compound literals of integer type, whose
6755 initializer was a constant, in constant
6756 expressions. Permitting that usage, as a further
6757 extension, would not change the meaning of any
6758 currently accepted programs. (Of course, as
6759 compound literals are not part of ISO C++, the
6760 standard has nothing to say.) */
6761 if (cp_parser_non_integral_constant_expression (parser,
6762 NIC_NCC))
6764 postfix_expression = error_mark_node;
6765 break;
6767 /* Form the representation of the compound-literal. */
6768 postfix_expression
6769 = finish_compound_literal (type, initializer,
6770 tf_warning_or_error);
6771 postfix_expression.set_location (initializer.get_location ());
6772 break;
6776 /* It must be a primary-expression. */
6777 postfix_expression
6778 = cp_parser_primary_expression (parser, address_p, cast_p,
6779 /*template_arg_p=*/false,
6780 decltype_p,
6781 &idk);
6783 break;
6786 /* Note that we don't need to worry about calling build_cplus_new on a
6787 class-valued CALL_EXPR in decltype when it isn't the end of the
6788 postfix-expression; unary_complex_lvalue will take care of that for
6789 all these cases. */
6791 /* Keep looping until the postfix-expression is complete. */
6792 while (true)
6794 if (idk == CP_ID_KIND_UNQUALIFIED
6795 && identifier_p (postfix_expression)
6796 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6797 /* It is not a Koenig lookup function call. */
6798 postfix_expression
6799 = unqualified_name_lookup_error (postfix_expression);
6801 /* Peek at the next token. */
6802 token = cp_lexer_peek_token (parser->lexer);
6804 switch (token->type)
6806 case CPP_OPEN_SQUARE:
6807 if (cp_next_tokens_can_be_std_attribute_p (parser))
6809 cp_parser_error (parser,
6810 "two consecutive %<[%> shall "
6811 "only introduce an attribute");
6812 return error_mark_node;
6814 postfix_expression
6815 = cp_parser_postfix_open_square_expression (parser,
6816 postfix_expression,
6817 false,
6818 decltype_p);
6819 postfix_expression.set_range (start_loc,
6820 postfix_expression.get_location ());
6822 idk = CP_ID_KIND_NONE;
6823 is_member_access = false;
6824 break;
6826 case CPP_OPEN_PAREN:
6827 /* postfix-expression ( expression-list [opt] ) */
6829 bool koenig_p;
6830 bool is_builtin_constant_p;
6831 bool saved_integral_constant_expression_p = false;
6832 bool saved_non_integral_constant_expression_p = false;
6833 tsubst_flags_t complain = complain_flags (decltype_p);
6834 vec<tree, va_gc> *args;
6835 location_t close_paren_loc = UNKNOWN_LOCATION;
6837 is_member_access = false;
6839 is_builtin_constant_p
6840 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6841 if (is_builtin_constant_p)
6843 /* The whole point of __builtin_constant_p is to allow
6844 non-constant expressions to appear as arguments. */
6845 saved_integral_constant_expression_p
6846 = parser->integral_constant_expression_p;
6847 saved_non_integral_constant_expression_p
6848 = parser->non_integral_constant_expression_p;
6849 parser->integral_constant_expression_p = false;
6851 args = (cp_parser_parenthesized_expression_list
6852 (parser, non_attr,
6853 /*cast_p=*/false, /*allow_expansion_p=*/true,
6854 /*non_constant_p=*/NULL,
6855 /*close_paren_loc=*/&close_paren_loc));
6856 if (is_builtin_constant_p)
6858 parser->integral_constant_expression_p
6859 = saved_integral_constant_expression_p;
6860 parser->non_integral_constant_expression_p
6861 = saved_non_integral_constant_expression_p;
6864 if (args == NULL)
6866 postfix_expression = error_mark_node;
6867 break;
6870 /* Function calls are not permitted in
6871 constant-expressions. */
6872 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6873 && cp_parser_non_integral_constant_expression (parser,
6874 NIC_FUNC_CALL))
6876 postfix_expression = error_mark_node;
6877 release_tree_vector (args);
6878 break;
6881 koenig_p = false;
6882 if (idk == CP_ID_KIND_UNQUALIFIED
6883 || idk == CP_ID_KIND_TEMPLATE_ID)
6885 if (identifier_p (postfix_expression))
6887 if (!args->is_empty ())
6889 koenig_p = true;
6890 if (!any_type_dependent_arguments_p (args))
6891 postfix_expression
6892 = perform_koenig_lookup (postfix_expression, args,
6893 complain);
6895 else
6896 postfix_expression
6897 = unqualified_fn_lookup_error (postfix_expression);
6899 /* We do not perform argument-dependent lookup if
6900 normal lookup finds a non-function, in accordance
6901 with the expected resolution of DR 218. */
6902 else if (!args->is_empty ()
6903 && is_overloaded_fn (postfix_expression))
6905 tree fn = get_first_fn (postfix_expression);
6906 fn = STRIP_TEMPLATE (fn);
6908 /* Do not do argument dependent lookup if regular
6909 lookup finds a member function or a block-scope
6910 function declaration. [basic.lookup.argdep]/3 */
6911 if (!DECL_FUNCTION_MEMBER_P (fn)
6912 && !DECL_LOCAL_FUNCTION_P (fn))
6914 koenig_p = true;
6915 if (!any_type_dependent_arguments_p (args))
6916 postfix_expression
6917 = perform_koenig_lookup (postfix_expression, args,
6918 complain);
6923 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6924 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6925 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6926 && vec_safe_length (args) == 3)
6928 tree arg0 = (*args)[0];
6929 tree arg1 = (*args)[1];
6930 tree arg2 = (*args)[2];
6931 int literal_mask = ((!!integer_zerop (arg1) << 1)
6932 | (!!integer_zerop (arg2) << 2));
6933 if (TREE_CODE (arg2) == CONST_DECL)
6934 arg2 = DECL_INITIAL (arg2);
6935 warn_for_memset (input_location, arg0, arg2, literal_mask);
6938 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6940 tree instance = TREE_OPERAND (postfix_expression, 0);
6941 tree fn = TREE_OPERAND (postfix_expression, 1);
6943 if (processing_template_decl
6944 && (type_dependent_object_expression_p (instance)
6945 || (!BASELINK_P (fn)
6946 && TREE_CODE (fn) != FIELD_DECL)
6947 || type_dependent_expression_p (fn)
6948 || any_type_dependent_arguments_p (args)))
6950 maybe_generic_this_capture (instance, fn);
6951 postfix_expression
6952 = build_nt_call_vec (postfix_expression, args);
6953 release_tree_vector (args);
6954 break;
6957 if (BASELINK_P (fn))
6959 postfix_expression
6960 = (build_new_method_call
6961 (instance, fn, &args, NULL_TREE,
6962 (idk == CP_ID_KIND_QUALIFIED
6963 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6964 : LOOKUP_NORMAL),
6965 /*fn_p=*/NULL,
6966 complain));
6968 else
6969 postfix_expression
6970 = finish_call_expr (postfix_expression, &args,
6971 /*disallow_virtual=*/false,
6972 /*koenig_p=*/false,
6973 complain);
6975 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6976 || TREE_CODE (postfix_expression) == MEMBER_REF
6977 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6978 postfix_expression = (build_offset_ref_call_from_tree
6979 (postfix_expression, &args,
6980 complain));
6981 else if (idk == CP_ID_KIND_QUALIFIED)
6982 /* A call to a static class member, or a namespace-scope
6983 function. */
6984 postfix_expression
6985 = finish_call_expr (postfix_expression, &args,
6986 /*disallow_virtual=*/true,
6987 koenig_p,
6988 complain);
6989 else
6990 /* All other function calls. */
6991 postfix_expression
6992 = finish_call_expr (postfix_expression, &args,
6993 /*disallow_virtual=*/false,
6994 koenig_p,
6995 complain);
6997 if (close_paren_loc != UNKNOWN_LOCATION)
6999 location_t combined_loc = make_location (token->location,
7000 start_loc,
7001 close_paren_loc);
7002 postfix_expression.set_location (combined_loc);
7005 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7006 idk = CP_ID_KIND_NONE;
7008 release_tree_vector (args);
7010 break;
7012 case CPP_DOT:
7013 case CPP_DEREF:
7014 /* postfix-expression . template [opt] id-expression
7015 postfix-expression . pseudo-destructor-name
7016 postfix-expression -> template [opt] id-expression
7017 postfix-expression -> pseudo-destructor-name */
7019 /* Consume the `.' or `->' operator. */
7020 cp_lexer_consume_token (parser->lexer);
7022 postfix_expression
7023 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7024 postfix_expression,
7025 false, &idk, loc);
7027 is_member_access = true;
7028 break;
7030 case CPP_PLUS_PLUS:
7031 /* postfix-expression ++ */
7032 /* Consume the `++' token. */
7033 cp_lexer_consume_token (parser->lexer);
7034 /* Generate a representation for the complete expression. */
7035 postfix_expression
7036 = finish_increment_expr (postfix_expression,
7037 POSTINCREMENT_EXPR);
7038 /* Increments may not appear in constant-expressions. */
7039 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7040 postfix_expression = error_mark_node;
7041 idk = CP_ID_KIND_NONE;
7042 is_member_access = false;
7043 break;
7045 case CPP_MINUS_MINUS:
7046 /* postfix-expression -- */
7047 /* Consume the `--' token. */
7048 cp_lexer_consume_token (parser->lexer);
7049 /* Generate a representation for the complete expression. */
7050 postfix_expression
7051 = finish_increment_expr (postfix_expression,
7052 POSTDECREMENT_EXPR);
7053 /* Decrements may not appear in constant-expressions. */
7054 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7055 postfix_expression = error_mark_node;
7056 idk = CP_ID_KIND_NONE;
7057 is_member_access = false;
7058 break;
7060 default:
7061 if (pidk_return != NULL)
7062 * pidk_return = idk;
7063 if (member_access_only_p)
7064 return is_member_access
7065 ? postfix_expression
7066 : cp_expr (error_mark_node);
7067 else
7068 return postfix_expression;
7072 /* We should never get here. */
7073 gcc_unreachable ();
7074 return error_mark_node;
7077 /* This function parses Cilk Plus array notations. If a normal array expr. is
7078 parsed then the array index is passed back to the caller through *INIT_INDEX
7079 and the function returns a NULL_TREE. If array notation expr. is parsed,
7080 then *INIT_INDEX is ignored by the caller and the function returns
7081 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7082 error_mark_node. */
7084 static tree
7085 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7086 tree array_value)
7088 cp_token *token = NULL;
7089 tree length_index, stride = NULL_TREE, value_tree, array_type;
7090 if (!array_value || array_value == error_mark_node)
7092 cp_parser_skip_to_end_of_statement (parser);
7093 return error_mark_node;
7096 array_type = TREE_TYPE (array_value);
7098 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7099 parser->colon_corrects_to_scope_p = false;
7100 token = cp_lexer_peek_token (parser->lexer);
7102 if (!token)
7104 cp_parser_error (parser, "expected %<:%> or numeral");
7105 return error_mark_node;
7107 else if (token->type == CPP_COLON)
7109 /* Consume the ':'. */
7110 cp_lexer_consume_token (parser->lexer);
7112 /* If we are here, then we have a case like this A[:]. */
7113 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7115 cp_parser_error (parser, "expected %<]%>");
7116 cp_parser_skip_to_end_of_statement (parser);
7117 return error_mark_node;
7119 *init_index = NULL_TREE;
7120 stride = NULL_TREE;
7121 length_index = NULL_TREE;
7123 else
7125 /* If we are here, then there are three valid possibilities:
7126 1. ARRAY [ EXP ]
7127 2. ARRAY [ EXP : EXP ]
7128 3. ARRAY [ EXP : EXP : EXP ] */
7130 *init_index = cp_parser_expression (parser);
7131 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7133 /* This indicates that we have a normal array expression. */
7134 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7135 return NULL_TREE;
7138 /* Consume the ':'. */
7139 cp_lexer_consume_token (parser->lexer);
7140 length_index = cp_parser_expression (parser);
7141 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7143 cp_lexer_consume_token (parser->lexer);
7144 stride = cp_parser_expression (parser);
7147 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7149 if (*init_index == error_mark_node || length_index == error_mark_node
7150 || stride == error_mark_node || array_type == error_mark_node)
7152 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7153 cp_lexer_consume_token (parser->lexer);
7154 return error_mark_node;
7156 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7158 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7159 length_index, stride, array_type);
7160 return value_tree;
7163 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7164 by cp_parser_builtin_offsetof. We're looking for
7166 postfix-expression [ expression ]
7167 postfix-expression [ braced-init-list ] (C++11)
7169 FOR_OFFSETOF is set if we're being called in that context, which
7170 changes how we deal with integer constant expressions. */
7172 static tree
7173 cp_parser_postfix_open_square_expression (cp_parser *parser,
7174 tree postfix_expression,
7175 bool for_offsetof,
7176 bool decltype_p)
7178 tree index = NULL_TREE;
7179 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7180 bool saved_greater_than_is_operator_p;
7182 /* Consume the `[' token. */
7183 cp_lexer_consume_token (parser->lexer);
7185 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7186 parser->greater_than_is_operator_p = true;
7188 /* Parse the index expression. */
7189 /* ??? For offsetof, there is a question of what to allow here. If
7190 offsetof is not being used in an integral constant expression context,
7191 then we *could* get the right answer by computing the value at runtime.
7192 If we are in an integral constant expression context, then we might
7193 could accept any constant expression; hard to say without analysis.
7194 Rather than open the barn door too wide right away, allow only integer
7195 constant expressions here. */
7196 if (for_offsetof)
7197 index = cp_parser_constant_expression (parser);
7198 else
7200 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7202 bool expr_nonconst_p;
7203 cp_lexer_set_source_position (parser->lexer);
7204 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7205 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7206 if (flag_cilkplus
7207 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7209 error_at (cp_lexer_peek_token (parser->lexer)->location,
7210 "braced list index is not allowed with array "
7211 "notation");
7212 cp_parser_skip_to_end_of_statement (parser);
7213 return error_mark_node;
7216 else if (flag_cilkplus)
7218 /* Here are have these two options:
7219 ARRAY[EXP : EXP] - Array notation expr with default
7220 stride of 1.
7221 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7222 stride. */
7223 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7224 postfix_expression);
7225 if (an_exp)
7226 return an_exp;
7228 else
7229 index = cp_parser_expression (parser);
7232 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7234 /* Look for the closing `]'. */
7235 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7237 /* Build the ARRAY_REF. */
7238 postfix_expression = grok_array_decl (loc, postfix_expression,
7239 index, decltype_p);
7241 /* When not doing offsetof, array references are not permitted in
7242 constant-expressions. */
7243 if (!for_offsetof
7244 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7245 postfix_expression = error_mark_node;
7247 return postfix_expression;
7250 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7251 by cp_parser_builtin_offsetof. We're looking for
7253 postfix-expression . template [opt] id-expression
7254 postfix-expression . pseudo-destructor-name
7255 postfix-expression -> template [opt] id-expression
7256 postfix-expression -> pseudo-destructor-name
7258 FOR_OFFSETOF is set if we're being called in that context. That sorta
7259 limits what of the above we'll actually accept, but nevermind.
7260 TOKEN_TYPE is the "." or "->" token, which will already have been
7261 removed from the stream. */
7263 static tree
7264 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7265 enum cpp_ttype token_type,
7266 cp_expr postfix_expression,
7267 bool for_offsetof, cp_id_kind *idk,
7268 location_t location)
7270 tree name;
7271 bool dependent_p;
7272 bool pseudo_destructor_p;
7273 tree scope = NULL_TREE;
7274 location_t start_loc = postfix_expression.get_start ();
7276 /* If this is a `->' operator, dereference the pointer. */
7277 if (token_type == CPP_DEREF)
7278 postfix_expression = build_x_arrow (location, postfix_expression,
7279 tf_warning_or_error);
7280 /* Check to see whether or not the expression is type-dependent and
7281 not the current instantiation. */
7282 dependent_p = type_dependent_object_expression_p (postfix_expression);
7283 /* The identifier following the `->' or `.' is not qualified. */
7284 parser->scope = NULL_TREE;
7285 parser->qualifying_scope = NULL_TREE;
7286 parser->object_scope = NULL_TREE;
7287 *idk = CP_ID_KIND_NONE;
7289 /* Enter the scope corresponding to the type of the object
7290 given by the POSTFIX_EXPRESSION. */
7291 if (!dependent_p)
7293 scope = TREE_TYPE (postfix_expression);
7294 /* According to the standard, no expression should ever have
7295 reference type. Unfortunately, we do not currently match
7296 the standard in this respect in that our internal representation
7297 of an expression may have reference type even when the standard
7298 says it does not. Therefore, we have to manually obtain the
7299 underlying type here. */
7300 scope = non_reference (scope);
7301 /* The type of the POSTFIX_EXPRESSION must be complete. */
7302 /* Unlike the object expression in other contexts, *this is not
7303 required to be of complete type for purposes of class member
7304 access (5.2.5) outside the member function body. */
7305 if (postfix_expression != current_class_ref
7306 && scope != error_mark_node
7307 && !(processing_template_decl
7308 && current_class_type
7309 && (same_type_ignoring_top_level_qualifiers_p
7310 (scope, current_class_type))))
7312 scope = complete_type (scope);
7313 if (!COMPLETE_TYPE_P (scope)
7314 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7315 && EXPR_P (postfix_expression))
7317 /* In a template, be permissive by treating an object expression
7318 of incomplete type as dependent (after a pedwarn). */
7319 diagnostic_t kind = (processing_template_decl
7320 ? DK_PEDWARN
7321 : DK_ERROR);
7322 cxx_incomplete_type_diagnostic
7323 (location_of (postfix_expression),
7324 postfix_expression, scope, kind);
7325 if (processing_template_decl)
7327 dependent_p = true;
7328 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7333 if (!dependent_p)
7335 /* Let the name lookup machinery know that we are processing a
7336 class member access expression. */
7337 parser->context->object_type = scope;
7338 /* If something went wrong, we want to be able to discern that case,
7339 as opposed to the case where there was no SCOPE due to the type
7340 of expression being dependent. */
7341 if (!scope)
7342 scope = error_mark_node;
7343 /* If the SCOPE was erroneous, make the various semantic analysis
7344 functions exit quickly -- and without issuing additional error
7345 messages. */
7346 if (scope == error_mark_node)
7347 postfix_expression = error_mark_node;
7351 if (dependent_p)
7352 /* Tell cp_parser_lookup_name that there was an object, even though it's
7353 type-dependent. */
7354 parser->context->object_type = unknown_type_node;
7356 /* Assume this expression is not a pseudo-destructor access. */
7357 pseudo_destructor_p = false;
7359 /* If the SCOPE is a scalar type, then, if this is a valid program,
7360 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7361 is type dependent, it can be pseudo-destructor-name or something else.
7362 Try to parse it as pseudo-destructor-name first. */
7363 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7365 tree s;
7366 tree type;
7368 cp_parser_parse_tentatively (parser);
7369 /* Parse the pseudo-destructor-name. */
7370 s = NULL_TREE;
7371 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7372 &s, &type);
7373 if (dependent_p
7374 && (cp_parser_error_occurred (parser)
7375 || !SCALAR_TYPE_P (type)))
7376 cp_parser_abort_tentative_parse (parser);
7377 else if (cp_parser_parse_definitely (parser))
7379 pseudo_destructor_p = true;
7380 postfix_expression
7381 = finish_pseudo_destructor_expr (postfix_expression,
7382 s, type, location);
7386 if (!pseudo_destructor_p)
7388 /* If the SCOPE is not a scalar type, we are looking at an
7389 ordinary class member access expression, rather than a
7390 pseudo-destructor-name. */
7391 bool template_p;
7392 cp_token *token = cp_lexer_peek_token (parser->lexer);
7393 /* Parse the id-expression. */
7394 name = (cp_parser_id_expression
7395 (parser,
7396 cp_parser_optional_template_keyword (parser),
7397 /*check_dependency_p=*/true,
7398 &template_p,
7399 /*declarator_p=*/false,
7400 /*optional_p=*/false));
7401 /* In general, build a SCOPE_REF if the member name is qualified.
7402 However, if the name was not dependent and has already been
7403 resolved; there is no need to build the SCOPE_REF. For example;
7405 struct X { void f(); };
7406 template <typename T> void f(T* t) { t->X::f(); }
7408 Even though "t" is dependent, "X::f" is not and has been resolved
7409 to a BASELINK; there is no need to include scope information. */
7411 /* But we do need to remember that there was an explicit scope for
7412 virtual function calls. */
7413 if (parser->scope)
7414 *idk = CP_ID_KIND_QUALIFIED;
7416 /* If the name is a template-id that names a type, we will get a
7417 TYPE_DECL here. That is invalid code. */
7418 if (TREE_CODE (name) == TYPE_DECL)
7420 error_at (token->location, "invalid use of %qD", name);
7421 postfix_expression = error_mark_node;
7423 else
7425 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7427 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7429 error_at (token->location, "%<%D::%D%> is not a class member",
7430 parser->scope, name);
7431 postfix_expression = error_mark_node;
7433 else
7434 name = build_qualified_name (/*type=*/NULL_TREE,
7435 parser->scope,
7436 name,
7437 template_p);
7438 parser->scope = NULL_TREE;
7439 parser->qualifying_scope = NULL_TREE;
7440 parser->object_scope = NULL_TREE;
7442 if (parser->scope && name && BASELINK_P (name))
7443 adjust_result_of_qualified_name_lookup
7444 (name, parser->scope, scope);
7445 postfix_expression
7446 = finish_class_member_access_expr (postfix_expression, name,
7447 template_p,
7448 tf_warning_or_error);
7449 /* Build a location e.g.:
7450 ptr->access_expr
7451 ~~~^~~~~~~~~~~~~
7452 where the caret is at the deref token, ranging from
7453 the start of postfix_expression to the end of the access expr. */
7454 location_t end_loc
7455 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7456 location_t combined_loc
7457 = make_location (input_location, start_loc, end_loc);
7458 protected_set_expr_location (postfix_expression, combined_loc);
7462 /* We no longer need to look up names in the scope of the object on
7463 the left-hand side of the `.' or `->' operator. */
7464 parser->context->object_type = NULL_TREE;
7466 /* Outside of offsetof, these operators may not appear in
7467 constant-expressions. */
7468 if (!for_offsetof
7469 && (cp_parser_non_integral_constant_expression
7470 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7471 postfix_expression = error_mark_node;
7473 return postfix_expression;
7476 /* Parse a parenthesized expression-list.
7478 expression-list:
7479 assignment-expression
7480 expression-list, assignment-expression
7482 attribute-list:
7483 expression-list
7484 identifier
7485 identifier, expression-list
7487 CAST_P is true if this expression is the target of a cast.
7489 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7490 argument pack.
7492 Returns a vector of trees. Each element is a representation of an
7493 assignment-expression. NULL is returned if the ( and or ) are
7494 missing. An empty, but allocated, vector is returned on no
7495 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7496 if we are parsing an attribute list for an attribute that wants a
7497 plain identifier argument, normal_attr for an attribute that wants
7498 an expression, or non_attr if we aren't parsing an attribute list. If
7499 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7500 not all of the expressions in the list were constant.
7501 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7502 will be written to with the location of the closing parenthesis. If
7503 an error occurs, it may or may not be written to. */
7505 static vec<tree, va_gc> *
7506 cp_parser_parenthesized_expression_list (cp_parser* parser,
7507 int is_attribute_list,
7508 bool cast_p,
7509 bool allow_expansion_p,
7510 bool *non_constant_p,
7511 location_t *close_paren_loc)
7513 vec<tree, va_gc> *expression_list;
7514 bool fold_expr_p = is_attribute_list != non_attr;
7515 tree identifier = NULL_TREE;
7516 bool saved_greater_than_is_operator_p;
7518 /* Assume all the expressions will be constant. */
7519 if (non_constant_p)
7520 *non_constant_p = false;
7522 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7523 return NULL;
7525 expression_list = make_tree_vector ();
7527 /* Within a parenthesized expression, a `>' token is always
7528 the greater-than operator. */
7529 saved_greater_than_is_operator_p
7530 = parser->greater_than_is_operator_p;
7531 parser->greater_than_is_operator_p = true;
7533 /* Consume expressions until there are no more. */
7534 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7535 while (true)
7537 tree expr;
7539 /* At the beginning of attribute lists, check to see if the
7540 next token is an identifier. */
7541 if (is_attribute_list == id_attr
7542 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7544 cp_token *token;
7546 /* Consume the identifier. */
7547 token = cp_lexer_consume_token (parser->lexer);
7548 /* Save the identifier. */
7549 identifier = token->u.value;
7551 else
7553 bool expr_non_constant_p;
7555 /* Parse the next assignment-expression. */
7556 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7558 /* A braced-init-list. */
7559 cp_lexer_set_source_position (parser->lexer);
7560 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7561 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7562 if (non_constant_p && expr_non_constant_p)
7563 *non_constant_p = true;
7565 else if (non_constant_p)
7567 expr = (cp_parser_constant_expression
7568 (parser, /*allow_non_constant_p=*/true,
7569 &expr_non_constant_p));
7570 if (expr_non_constant_p)
7571 *non_constant_p = true;
7573 else
7574 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7575 cast_p);
7577 if (fold_expr_p)
7578 expr = instantiate_non_dependent_expr (expr);
7580 /* If we have an ellipsis, then this is an expression
7581 expansion. */
7582 if (allow_expansion_p
7583 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7585 /* Consume the `...'. */
7586 cp_lexer_consume_token (parser->lexer);
7588 /* Build the argument pack. */
7589 expr = make_pack_expansion (expr);
7592 /* Add it to the list. We add error_mark_node
7593 expressions to the list, so that we can still tell if
7594 the correct form for a parenthesized expression-list
7595 is found. That gives better errors. */
7596 vec_safe_push (expression_list, expr);
7598 if (expr == error_mark_node)
7599 goto skip_comma;
7602 /* After the first item, attribute lists look the same as
7603 expression lists. */
7604 is_attribute_list = non_attr;
7606 get_comma:;
7607 /* If the next token isn't a `,', then we are done. */
7608 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7609 break;
7611 /* Otherwise, consume the `,' and keep going. */
7612 cp_lexer_consume_token (parser->lexer);
7615 if (close_paren_loc)
7616 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7618 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7620 int ending;
7622 skip_comma:;
7623 /* We try and resync to an unnested comma, as that will give the
7624 user better diagnostics. */
7625 ending = cp_parser_skip_to_closing_parenthesis (parser,
7626 /*recovering=*/true,
7627 /*or_comma=*/true,
7628 /*consume_paren=*/true);
7629 if (ending < 0)
7630 goto get_comma;
7631 if (!ending)
7633 parser->greater_than_is_operator_p
7634 = saved_greater_than_is_operator_p;
7635 return NULL;
7639 parser->greater_than_is_operator_p
7640 = saved_greater_than_is_operator_p;
7642 if (identifier)
7643 vec_safe_insert (expression_list, 0, identifier);
7645 return expression_list;
7648 /* Parse a pseudo-destructor-name.
7650 pseudo-destructor-name:
7651 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7652 :: [opt] nested-name-specifier template template-id :: ~ type-name
7653 :: [opt] nested-name-specifier [opt] ~ type-name
7655 If either of the first two productions is used, sets *SCOPE to the
7656 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7657 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7658 or ERROR_MARK_NODE if the parse fails. */
7660 static void
7661 cp_parser_pseudo_destructor_name (cp_parser* parser,
7662 tree object,
7663 tree* scope,
7664 tree* type)
7666 bool nested_name_specifier_p;
7668 /* Handle ~auto. */
7669 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7670 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7671 && !type_dependent_expression_p (object))
7673 if (cxx_dialect < cxx14)
7674 pedwarn (input_location, 0,
7675 "%<~auto%> only available with "
7676 "-std=c++14 or -std=gnu++14");
7677 cp_lexer_consume_token (parser->lexer);
7678 cp_lexer_consume_token (parser->lexer);
7679 *scope = NULL_TREE;
7680 *type = TREE_TYPE (object);
7681 return;
7684 /* Assume that things will not work out. */
7685 *type = error_mark_node;
7687 /* Look for the optional `::' operator. */
7688 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7689 /* Look for the optional nested-name-specifier. */
7690 nested_name_specifier_p
7691 = (cp_parser_nested_name_specifier_opt (parser,
7692 /*typename_keyword_p=*/false,
7693 /*check_dependency_p=*/true,
7694 /*type_p=*/false,
7695 /*is_declaration=*/false)
7696 != NULL_TREE);
7697 /* Now, if we saw a nested-name-specifier, we might be doing the
7698 second production. */
7699 if (nested_name_specifier_p
7700 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7702 /* Consume the `template' keyword. */
7703 cp_lexer_consume_token (parser->lexer);
7704 /* Parse the template-id. */
7705 cp_parser_template_id (parser,
7706 /*template_keyword_p=*/true,
7707 /*check_dependency_p=*/false,
7708 class_type,
7709 /*is_declaration=*/true);
7710 /* Look for the `::' token. */
7711 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7713 /* If the next token is not a `~', then there might be some
7714 additional qualification. */
7715 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7717 /* At this point, we're looking for "type-name :: ~". The type-name
7718 must not be a class-name, since this is a pseudo-destructor. So,
7719 it must be either an enum-name, or a typedef-name -- both of which
7720 are just identifiers. So, we peek ahead to check that the "::"
7721 and "~" tokens are present; if they are not, then we can avoid
7722 calling type_name. */
7723 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7724 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7725 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7727 cp_parser_error (parser, "non-scalar type");
7728 return;
7731 /* Look for the type-name. */
7732 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7733 if (*scope == error_mark_node)
7734 return;
7736 /* Look for the `::' token. */
7737 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7739 else
7740 *scope = NULL_TREE;
7742 /* Look for the `~'. */
7743 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7745 /* Once we see the ~, this has to be a pseudo-destructor. */
7746 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7747 cp_parser_commit_to_topmost_tentative_parse (parser);
7749 /* Look for the type-name again. We are not responsible for
7750 checking that it matches the first type-name. */
7751 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7754 /* Parse a unary-expression.
7756 unary-expression:
7757 postfix-expression
7758 ++ cast-expression
7759 -- cast-expression
7760 unary-operator cast-expression
7761 sizeof unary-expression
7762 sizeof ( type-id )
7763 alignof ( type-id ) [C++0x]
7764 new-expression
7765 delete-expression
7767 GNU Extensions:
7769 unary-expression:
7770 __extension__ cast-expression
7771 __alignof__ unary-expression
7772 __alignof__ ( type-id )
7773 alignof unary-expression [C++0x]
7774 __real__ cast-expression
7775 __imag__ cast-expression
7776 && identifier
7777 sizeof ( type-id ) { initializer-list , [opt] }
7778 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7779 __alignof__ ( type-id ) { initializer-list , [opt] }
7781 ADDRESS_P is true iff the unary-expression is appearing as the
7782 operand of the `&' operator. CAST_P is true if this expression is
7783 the target of a cast.
7785 Returns a representation of the expression. */
7787 static cp_expr
7788 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7789 bool address_p, bool cast_p, bool decltype_p)
7791 cp_token *token;
7792 enum tree_code unary_operator;
7794 /* Peek at the next token. */
7795 token = cp_lexer_peek_token (parser->lexer);
7796 /* Some keywords give away the kind of expression. */
7797 if (token->type == CPP_KEYWORD)
7799 enum rid keyword = token->keyword;
7801 switch (keyword)
7803 case RID_ALIGNOF:
7804 case RID_SIZEOF:
7806 tree operand, ret;
7807 enum tree_code op;
7808 location_t start_loc = token->location;
7810 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7811 /* Consume the token. */
7812 cp_lexer_consume_token (parser->lexer);
7813 /* Parse the operand. */
7814 operand = cp_parser_sizeof_operand (parser, keyword);
7816 if (TYPE_P (operand))
7817 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7818 else
7820 /* ISO C++ defines alignof only with types, not with
7821 expressions. So pedwarn if alignof is used with a non-
7822 type expression. However, __alignof__ is ok. */
7823 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7824 pedwarn (token->location, OPT_Wpedantic,
7825 "ISO C++ does not allow %<alignof%> "
7826 "with a non-type");
7828 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7830 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7831 SIZEOF_EXPR with the original operand. */
7832 if (op == SIZEOF_EXPR && ret != error_mark_node)
7834 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7836 if (!processing_template_decl && TYPE_P (operand))
7838 ret = build_min (SIZEOF_EXPR, size_type_node,
7839 build1 (NOP_EXPR, operand,
7840 error_mark_node));
7841 SIZEOF_EXPR_TYPE_P (ret) = 1;
7843 else
7844 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7845 TREE_SIDE_EFFECTS (ret) = 0;
7846 TREE_READONLY (ret) = 1;
7850 /* Construct a location e.g. :
7851 alignof (expr)
7852 ^~~~~~~~~~~~~~
7853 with start == caret at the start of the "alignof"/"sizeof"
7854 token, with the endpoint at the final closing paren. */
7855 location_t finish_loc
7856 = cp_lexer_previous_token (parser->lexer)->location;
7857 location_t compound_loc
7858 = make_location (start_loc, start_loc, finish_loc);
7860 cp_expr ret_expr (ret);
7861 ret_expr.set_location (compound_loc);
7862 return ret_expr;
7865 case RID_NEW:
7866 return cp_parser_new_expression (parser);
7868 case RID_DELETE:
7869 return cp_parser_delete_expression (parser);
7871 case RID_EXTENSION:
7873 /* The saved value of the PEDANTIC flag. */
7874 int saved_pedantic;
7875 tree expr;
7877 /* Save away the PEDANTIC flag. */
7878 cp_parser_extension_opt (parser, &saved_pedantic);
7879 /* Parse the cast-expression. */
7880 expr = cp_parser_simple_cast_expression (parser);
7881 /* Restore the PEDANTIC flag. */
7882 pedantic = saved_pedantic;
7884 return expr;
7887 case RID_REALPART:
7888 case RID_IMAGPART:
7890 tree expression;
7892 /* Consume the `__real__' or `__imag__' token. */
7893 cp_lexer_consume_token (parser->lexer);
7894 /* Parse the cast-expression. */
7895 expression = cp_parser_simple_cast_expression (parser);
7896 /* Create the complete representation. */
7897 return build_x_unary_op (token->location,
7898 (keyword == RID_REALPART
7899 ? REALPART_EXPR : IMAGPART_EXPR),
7900 expression,
7901 tf_warning_or_error);
7903 break;
7905 case RID_TRANSACTION_ATOMIC:
7906 case RID_TRANSACTION_RELAXED:
7907 return cp_parser_transaction_expression (parser, keyword);
7909 case RID_NOEXCEPT:
7911 tree expr;
7912 const char *saved_message;
7913 bool saved_integral_constant_expression_p;
7914 bool saved_non_integral_constant_expression_p;
7915 bool saved_greater_than_is_operator_p;
7917 cp_lexer_consume_token (parser->lexer);
7918 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7920 saved_message = parser->type_definition_forbidden_message;
7921 parser->type_definition_forbidden_message
7922 = G_("types may not be defined in %<noexcept%> expressions");
7924 saved_integral_constant_expression_p
7925 = parser->integral_constant_expression_p;
7926 saved_non_integral_constant_expression_p
7927 = parser->non_integral_constant_expression_p;
7928 parser->integral_constant_expression_p = false;
7930 saved_greater_than_is_operator_p
7931 = parser->greater_than_is_operator_p;
7932 parser->greater_than_is_operator_p = true;
7934 ++cp_unevaluated_operand;
7935 ++c_inhibit_evaluation_warnings;
7936 ++cp_noexcept_operand;
7937 expr = cp_parser_expression (parser);
7938 --cp_noexcept_operand;
7939 --c_inhibit_evaluation_warnings;
7940 --cp_unevaluated_operand;
7942 parser->greater_than_is_operator_p
7943 = saved_greater_than_is_operator_p;
7945 parser->integral_constant_expression_p
7946 = saved_integral_constant_expression_p;
7947 parser->non_integral_constant_expression_p
7948 = saved_non_integral_constant_expression_p;
7950 parser->type_definition_forbidden_message = saved_message;
7952 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7953 return finish_noexcept_expr (expr, tf_warning_or_error);
7956 default:
7957 break;
7961 /* Look for the `:: new' and `:: delete', which also signal the
7962 beginning of a new-expression, or delete-expression,
7963 respectively. If the next token is `::', then it might be one of
7964 these. */
7965 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7967 enum rid keyword;
7969 /* See if the token after the `::' is one of the keywords in
7970 which we're interested. */
7971 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7972 /* If it's `new', we have a new-expression. */
7973 if (keyword == RID_NEW)
7974 return cp_parser_new_expression (parser);
7975 /* Similarly, for `delete'. */
7976 else if (keyword == RID_DELETE)
7977 return cp_parser_delete_expression (parser);
7980 /* Look for a unary operator. */
7981 unary_operator = cp_parser_unary_operator (token);
7982 /* The `++' and `--' operators can be handled similarly, even though
7983 they are not technically unary-operators in the grammar. */
7984 if (unary_operator == ERROR_MARK)
7986 if (token->type == CPP_PLUS_PLUS)
7987 unary_operator = PREINCREMENT_EXPR;
7988 else if (token->type == CPP_MINUS_MINUS)
7989 unary_operator = PREDECREMENT_EXPR;
7990 /* Handle the GNU address-of-label extension. */
7991 else if (cp_parser_allow_gnu_extensions_p (parser)
7992 && token->type == CPP_AND_AND)
7994 tree identifier;
7995 tree expression;
7996 location_t start_loc = token->location;
7998 /* Consume the '&&' token. */
7999 cp_lexer_consume_token (parser->lexer);
8000 /* Look for the identifier. */
8001 location_t finish_loc
8002 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8003 identifier = cp_parser_identifier (parser);
8004 /* Construct a location of the form:
8005 &&label
8006 ^~~~~~~
8007 with caret==start at the "&&", finish at the end of the label. */
8008 location_t combined_loc
8009 = make_location (start_loc, start_loc, finish_loc);
8010 /* Create an expression representing the address. */
8011 expression = finish_label_address_expr (identifier, combined_loc);
8012 if (cp_parser_non_integral_constant_expression (parser,
8013 NIC_ADDR_LABEL))
8014 expression = error_mark_node;
8015 return expression;
8018 if (unary_operator != ERROR_MARK)
8020 cp_expr cast_expression;
8021 cp_expr expression = error_mark_node;
8022 non_integral_constant non_constant_p = NIC_NONE;
8023 location_t loc = token->location;
8024 tsubst_flags_t complain = complain_flags (decltype_p);
8026 /* Consume the operator token. */
8027 token = cp_lexer_consume_token (parser->lexer);
8028 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8030 /* Parse the cast-expression. */
8031 cast_expression
8032 = cp_parser_cast_expression (parser,
8033 unary_operator == ADDR_EXPR,
8034 /*cast_p=*/false,
8035 /*decltype*/false,
8036 pidk);
8038 /* Make a location:
8039 OP_TOKEN CAST_EXPRESSION
8040 ^~~~~~~~~~~~~~~~~~~~~~~~~
8041 with start==caret at the operator token, and
8042 extending to the end of the cast_expression. */
8043 loc = make_location (loc, loc, cast_expression.get_finish ());
8045 /* Now, build an appropriate representation. */
8046 switch (unary_operator)
8048 case INDIRECT_REF:
8049 non_constant_p = NIC_STAR;
8050 expression = build_x_indirect_ref (loc, cast_expression,
8051 RO_UNARY_STAR,
8052 complain);
8053 /* TODO: build_x_indirect_ref does not always honor the
8054 location, so ensure it is set. */
8055 expression.set_location (loc);
8056 break;
8058 case ADDR_EXPR:
8059 non_constant_p = NIC_ADDR;
8060 /* Fall through. */
8061 case BIT_NOT_EXPR:
8062 expression = build_x_unary_op (loc, unary_operator,
8063 cast_expression,
8064 complain);
8065 /* TODO: build_x_unary_op does not always honor the location,
8066 so ensure it is set. */
8067 expression.set_location (loc);
8068 break;
8070 case PREINCREMENT_EXPR:
8071 case PREDECREMENT_EXPR:
8072 non_constant_p = unary_operator == PREINCREMENT_EXPR
8073 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8074 /* Fall through. */
8075 case NEGATE_EXPR:
8076 /* Immediately fold negation of a constant, unless the constant is 0
8077 (since -0 == 0) or it would overflow. */
8078 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8079 && CONSTANT_CLASS_P (cast_expression)
8080 && !integer_zerop (cast_expression)
8081 && !TREE_OVERFLOW (cast_expression))
8083 tree folded = fold_build1 (unary_operator,
8084 TREE_TYPE (cast_expression),
8085 cast_expression);
8086 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8088 expression = cp_expr (folded, loc);
8089 break;
8092 /* Fall through. */
8093 case UNARY_PLUS_EXPR:
8094 case TRUTH_NOT_EXPR:
8095 expression = finish_unary_op_expr (loc, unary_operator,
8096 cast_expression, complain);
8097 break;
8099 default:
8100 gcc_unreachable ();
8103 if (non_constant_p != NIC_NONE
8104 && cp_parser_non_integral_constant_expression (parser,
8105 non_constant_p))
8106 expression = error_mark_node;
8108 return expression;
8111 return cp_parser_postfix_expression (parser, address_p, cast_p,
8112 /*member_access_only_p=*/false,
8113 decltype_p,
8114 pidk);
8117 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8118 unary-operator, the corresponding tree code is returned. */
8120 static enum tree_code
8121 cp_parser_unary_operator (cp_token* token)
8123 switch (token->type)
8125 case CPP_MULT:
8126 return INDIRECT_REF;
8128 case CPP_AND:
8129 return ADDR_EXPR;
8131 case CPP_PLUS:
8132 return UNARY_PLUS_EXPR;
8134 case CPP_MINUS:
8135 return NEGATE_EXPR;
8137 case CPP_NOT:
8138 return TRUTH_NOT_EXPR;
8140 case CPP_COMPL:
8141 return BIT_NOT_EXPR;
8143 default:
8144 return ERROR_MARK;
8148 /* Parse a new-expression.
8150 new-expression:
8151 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8152 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8154 Returns a representation of the expression. */
8156 static tree
8157 cp_parser_new_expression (cp_parser* parser)
8159 bool global_scope_p;
8160 vec<tree, va_gc> *placement;
8161 tree type;
8162 vec<tree, va_gc> *initializer;
8163 tree nelts = NULL_TREE;
8164 tree ret;
8166 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8168 /* Look for the optional `::' operator. */
8169 global_scope_p
8170 = (cp_parser_global_scope_opt (parser,
8171 /*current_scope_valid_p=*/false)
8172 != NULL_TREE);
8173 /* Look for the `new' operator. */
8174 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8175 /* There's no easy way to tell a new-placement from the
8176 `( type-id )' construct. */
8177 cp_parser_parse_tentatively (parser);
8178 /* Look for a new-placement. */
8179 placement = cp_parser_new_placement (parser);
8180 /* If that didn't work out, there's no new-placement. */
8181 if (!cp_parser_parse_definitely (parser))
8183 if (placement != NULL)
8184 release_tree_vector (placement);
8185 placement = NULL;
8188 /* If the next token is a `(', then we have a parenthesized
8189 type-id. */
8190 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8192 cp_token *token;
8193 const char *saved_message = parser->type_definition_forbidden_message;
8195 /* Consume the `('. */
8196 cp_lexer_consume_token (parser->lexer);
8198 /* Parse the type-id. */
8199 parser->type_definition_forbidden_message
8200 = G_("types may not be defined in a new-expression");
8202 type_id_in_expr_sentinel s (parser);
8203 type = cp_parser_type_id (parser);
8205 parser->type_definition_forbidden_message = saved_message;
8207 /* Look for the closing `)'. */
8208 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8209 token = cp_lexer_peek_token (parser->lexer);
8210 /* There should not be a direct-new-declarator in this production,
8211 but GCC used to allowed this, so we check and emit a sensible error
8212 message for this case. */
8213 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8215 error_at (token->location,
8216 "array bound forbidden after parenthesized type-id");
8217 inform (token->location,
8218 "try removing the parentheses around the type-id");
8219 cp_parser_direct_new_declarator (parser);
8222 /* Otherwise, there must be a new-type-id. */
8223 else
8224 type = cp_parser_new_type_id (parser, &nelts);
8226 /* If the next token is a `(' or '{', then we have a new-initializer. */
8227 cp_token *token = cp_lexer_peek_token (parser->lexer);
8228 if (token->type == CPP_OPEN_PAREN
8229 || token->type == CPP_OPEN_BRACE)
8230 initializer = cp_parser_new_initializer (parser);
8231 else
8232 initializer = NULL;
8234 /* A new-expression may not appear in an integral constant
8235 expression. */
8236 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8237 ret = error_mark_node;
8238 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8239 of a new-type-id or type-id of a new-expression, the new-expression shall
8240 contain a new-initializer of the form ( assignment-expression )".
8241 Additionally, consistently with the spirit of DR 1467, we want to accept
8242 'new auto { 2 }' too. */
8243 else if ((ret = type_uses_auto (type))
8244 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8245 && (vec_safe_length (initializer) != 1
8246 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8247 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8249 error_at (token->location,
8250 "initialization of new-expression for type %<auto%> "
8251 "requires exactly one element");
8252 ret = error_mark_node;
8254 else
8256 /* Construct a location e.g.:
8257 ptr = new int[100]
8258 ^~~~~~~~~~~~
8259 with caret == start at the start of the "new" token, and the end
8260 at the end of the final token we consumed. */
8261 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8262 location_t end_loc = get_finish (end_tok->location);
8263 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8265 /* Create a representation of the new-expression. */
8266 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8267 tf_warning_or_error);
8268 protected_set_expr_location (ret, combined_loc);
8271 if (placement != NULL)
8272 release_tree_vector (placement);
8273 if (initializer != NULL)
8274 release_tree_vector (initializer);
8276 return ret;
8279 /* Parse a new-placement.
8281 new-placement:
8282 ( expression-list )
8284 Returns the same representation as for an expression-list. */
8286 static vec<tree, va_gc> *
8287 cp_parser_new_placement (cp_parser* parser)
8289 vec<tree, va_gc> *expression_list;
8291 /* Parse the expression-list. */
8292 expression_list = (cp_parser_parenthesized_expression_list
8293 (parser, non_attr, /*cast_p=*/false,
8294 /*allow_expansion_p=*/true,
8295 /*non_constant_p=*/NULL));
8297 if (expression_list && expression_list->is_empty ())
8298 error ("expected expression-list or type-id");
8300 return expression_list;
8303 /* Parse a new-type-id.
8305 new-type-id:
8306 type-specifier-seq new-declarator [opt]
8308 Returns the TYPE allocated. If the new-type-id indicates an array
8309 type, *NELTS is set to the number of elements in the last array
8310 bound; the TYPE will not include the last array bound. */
8312 static tree
8313 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8315 cp_decl_specifier_seq type_specifier_seq;
8316 cp_declarator *new_declarator;
8317 cp_declarator *declarator;
8318 cp_declarator *outer_declarator;
8319 const char *saved_message;
8321 /* The type-specifier sequence must not contain type definitions.
8322 (It cannot contain declarations of new types either, but if they
8323 are not definitions we will catch that because they are not
8324 complete.) */
8325 saved_message = parser->type_definition_forbidden_message;
8326 parser->type_definition_forbidden_message
8327 = G_("types may not be defined in a new-type-id");
8328 /* Parse the type-specifier-seq. */
8329 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8330 /*is_trailing_return=*/false,
8331 &type_specifier_seq);
8332 /* Restore the old message. */
8333 parser->type_definition_forbidden_message = saved_message;
8335 if (type_specifier_seq.type == error_mark_node)
8336 return error_mark_node;
8338 /* Parse the new-declarator. */
8339 new_declarator = cp_parser_new_declarator_opt (parser);
8341 /* Determine the number of elements in the last array dimension, if
8342 any. */
8343 *nelts = NULL_TREE;
8344 /* Skip down to the last array dimension. */
8345 declarator = new_declarator;
8346 outer_declarator = NULL;
8347 while (declarator && (declarator->kind == cdk_pointer
8348 || declarator->kind == cdk_ptrmem))
8350 outer_declarator = declarator;
8351 declarator = declarator->declarator;
8353 while (declarator
8354 && declarator->kind == cdk_array
8355 && declarator->declarator
8356 && declarator->declarator->kind == cdk_array)
8358 outer_declarator = declarator;
8359 declarator = declarator->declarator;
8362 if (declarator && declarator->kind == cdk_array)
8364 *nelts = declarator->u.array.bounds;
8365 if (*nelts == error_mark_node)
8366 *nelts = integer_one_node;
8368 if (outer_declarator)
8369 outer_declarator->declarator = declarator->declarator;
8370 else
8371 new_declarator = NULL;
8374 return groktypename (&type_specifier_seq, new_declarator, false);
8377 /* Parse an (optional) new-declarator.
8379 new-declarator:
8380 ptr-operator new-declarator [opt]
8381 direct-new-declarator
8383 Returns the declarator. */
8385 static cp_declarator *
8386 cp_parser_new_declarator_opt (cp_parser* parser)
8388 enum tree_code code;
8389 tree type, std_attributes = NULL_TREE;
8390 cp_cv_quals cv_quals;
8392 /* We don't know if there's a ptr-operator next, or not. */
8393 cp_parser_parse_tentatively (parser);
8394 /* Look for a ptr-operator. */
8395 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8396 /* If that worked, look for more new-declarators. */
8397 if (cp_parser_parse_definitely (parser))
8399 cp_declarator *declarator;
8401 /* Parse another optional declarator. */
8402 declarator = cp_parser_new_declarator_opt (parser);
8404 declarator = cp_parser_make_indirect_declarator
8405 (code, type, cv_quals, declarator, std_attributes);
8407 return declarator;
8410 /* If the next token is a `[', there is a direct-new-declarator. */
8411 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8412 return cp_parser_direct_new_declarator (parser);
8414 return NULL;
8417 /* Parse a direct-new-declarator.
8419 direct-new-declarator:
8420 [ expression ]
8421 direct-new-declarator [constant-expression]
8425 static cp_declarator *
8426 cp_parser_direct_new_declarator (cp_parser* parser)
8428 cp_declarator *declarator = NULL;
8430 while (true)
8432 tree expression;
8433 cp_token *token;
8435 /* Look for the opening `['. */
8436 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8438 token = cp_lexer_peek_token (parser->lexer);
8439 expression = cp_parser_expression (parser);
8440 /* The standard requires that the expression have integral
8441 type. DR 74 adds enumeration types. We believe that the
8442 real intent is that these expressions be handled like the
8443 expression in a `switch' condition, which also allows
8444 classes with a single conversion to integral or
8445 enumeration type. */
8446 if (!processing_template_decl)
8448 expression
8449 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8450 expression,
8451 /*complain=*/true);
8452 if (!expression)
8454 error_at (token->location,
8455 "expression in new-declarator must have integral "
8456 "or enumeration type");
8457 expression = error_mark_node;
8461 /* Look for the closing `]'. */
8462 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8464 /* Add this bound to the declarator. */
8465 declarator = make_array_declarator (declarator, expression);
8467 /* If the next token is not a `[', then there are no more
8468 bounds. */
8469 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8470 break;
8473 return declarator;
8476 /* Parse a new-initializer.
8478 new-initializer:
8479 ( expression-list [opt] )
8480 braced-init-list
8482 Returns a representation of the expression-list. */
8484 static vec<tree, va_gc> *
8485 cp_parser_new_initializer (cp_parser* parser)
8487 vec<tree, va_gc> *expression_list;
8489 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8491 tree t;
8492 bool expr_non_constant_p;
8493 cp_lexer_set_source_position (parser->lexer);
8494 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8495 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8496 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8497 expression_list = make_tree_vector_single (t);
8499 else
8500 expression_list = (cp_parser_parenthesized_expression_list
8501 (parser, non_attr, /*cast_p=*/false,
8502 /*allow_expansion_p=*/true,
8503 /*non_constant_p=*/NULL));
8505 return expression_list;
8508 /* Parse a delete-expression.
8510 delete-expression:
8511 :: [opt] delete cast-expression
8512 :: [opt] delete [ ] cast-expression
8514 Returns a representation of the expression. */
8516 static tree
8517 cp_parser_delete_expression (cp_parser* parser)
8519 bool global_scope_p;
8520 bool array_p;
8521 tree expression;
8523 /* Look for the optional `::' operator. */
8524 global_scope_p
8525 = (cp_parser_global_scope_opt (parser,
8526 /*current_scope_valid_p=*/false)
8527 != NULL_TREE);
8528 /* Look for the `delete' keyword. */
8529 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8530 /* See if the array syntax is in use. */
8531 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8533 /* Consume the `[' token. */
8534 cp_lexer_consume_token (parser->lexer);
8535 /* Look for the `]' token. */
8536 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8537 /* Remember that this is the `[]' construct. */
8538 array_p = true;
8540 else
8541 array_p = false;
8543 /* Parse the cast-expression. */
8544 expression = cp_parser_simple_cast_expression (parser);
8546 /* A delete-expression may not appear in an integral constant
8547 expression. */
8548 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8549 return error_mark_node;
8551 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8552 tf_warning_or_error);
8555 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8556 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8557 0 otherwise. */
8559 static int
8560 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8562 cp_token *token = cp_lexer_peek_token (parser->lexer);
8563 switch (token->type)
8565 case CPP_COMMA:
8566 case CPP_SEMICOLON:
8567 case CPP_QUERY:
8568 case CPP_COLON:
8569 case CPP_CLOSE_SQUARE:
8570 case CPP_CLOSE_PAREN:
8571 case CPP_CLOSE_BRACE:
8572 case CPP_OPEN_BRACE:
8573 case CPP_DOT:
8574 case CPP_DOT_STAR:
8575 case CPP_DEREF:
8576 case CPP_DEREF_STAR:
8577 case CPP_DIV:
8578 case CPP_MOD:
8579 case CPP_LSHIFT:
8580 case CPP_RSHIFT:
8581 case CPP_LESS:
8582 case CPP_GREATER:
8583 case CPP_LESS_EQ:
8584 case CPP_GREATER_EQ:
8585 case CPP_EQ_EQ:
8586 case CPP_NOT_EQ:
8587 case CPP_EQ:
8588 case CPP_MULT_EQ:
8589 case CPP_DIV_EQ:
8590 case CPP_MOD_EQ:
8591 case CPP_PLUS_EQ:
8592 case CPP_MINUS_EQ:
8593 case CPP_RSHIFT_EQ:
8594 case CPP_LSHIFT_EQ:
8595 case CPP_AND_EQ:
8596 case CPP_XOR_EQ:
8597 case CPP_OR_EQ:
8598 case CPP_XOR:
8599 case CPP_OR:
8600 case CPP_OR_OR:
8601 case CPP_EOF:
8602 case CPP_ELLIPSIS:
8603 return 0;
8605 case CPP_OPEN_PAREN:
8606 /* In ((type ()) () the last () isn't a valid cast-expression,
8607 so the whole must be parsed as postfix-expression. */
8608 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8609 != CPP_CLOSE_PAREN;
8611 case CPP_OPEN_SQUARE:
8612 /* '[' may start a primary-expression in obj-c++ and in C++11,
8613 as a lambda-expression, eg, '(void)[]{}'. */
8614 if (cxx_dialect >= cxx11)
8615 return -1;
8616 return c_dialect_objc ();
8618 case CPP_PLUS_PLUS:
8619 case CPP_MINUS_MINUS:
8620 /* '++' and '--' may or may not start a cast-expression:
8622 struct T { void operator++(int); };
8623 void f() { (T())++; }
8627 int a;
8628 (int)++a; */
8629 return -1;
8631 default:
8632 return 1;
8636 /* Parse a cast-expression.
8638 cast-expression:
8639 unary-expression
8640 ( type-id ) cast-expression
8642 ADDRESS_P is true iff the unary-expression is appearing as the
8643 operand of the `&' operator. CAST_P is true if this expression is
8644 the target of a cast.
8646 Returns a representation of the expression. */
8648 static cp_expr
8649 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8650 bool decltype_p, cp_id_kind * pidk)
8652 /* If it's a `(', then we might be looking at a cast. */
8653 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8655 tree type = NULL_TREE;
8656 cp_expr expr (NULL_TREE);
8657 int cast_expression = 0;
8658 const char *saved_message;
8660 /* There's no way to know yet whether or not this is a cast.
8661 For example, `(int (3))' is a unary-expression, while `(int)
8662 3' is a cast. So, we resort to parsing tentatively. */
8663 cp_parser_parse_tentatively (parser);
8664 /* Types may not be defined in a cast. */
8665 saved_message = parser->type_definition_forbidden_message;
8666 parser->type_definition_forbidden_message
8667 = G_("types may not be defined in casts");
8668 /* Consume the `('. */
8669 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8670 location_t open_paren_loc = open_paren->location;
8672 /* A very tricky bit is that `(struct S) { 3 }' is a
8673 compound-literal (which we permit in C++ as an extension).
8674 But, that construct is not a cast-expression -- it is a
8675 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8676 is legal; if the compound-literal were a cast-expression,
8677 you'd need an extra set of parentheses.) But, if we parse
8678 the type-id, and it happens to be a class-specifier, then we
8679 will commit to the parse at that point, because we cannot
8680 undo the action that is done when creating a new class. So,
8681 then we cannot back up and do a postfix-expression.
8683 Another tricky case is the following (c++/29234):
8685 struct S { void operator () (); };
8687 void foo ()
8689 ( S()() );
8692 As a type-id we parse the parenthesized S()() as a function
8693 returning a function, groktypename complains and we cannot
8694 back up in this case either.
8696 Therefore, we scan ahead to the closing `)', and check to see
8697 if the tokens after the `)' can start a cast-expression. Otherwise
8698 we are dealing with an unary-expression, a postfix-expression
8699 or something else.
8701 Yet another tricky case, in C++11, is the following (c++/54891):
8703 (void)[]{};
8705 The issue is that usually, besides the case of lambda-expressions,
8706 the parenthesized type-id cannot be followed by '[', and, eg, we
8707 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8708 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8709 we don't commit, we try a cast-expression, then an unary-expression.
8711 Save tokens so that we can put them back. */
8712 cp_lexer_save_tokens (parser->lexer);
8714 /* We may be looking at a cast-expression. */
8715 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8716 /*consume_paren=*/true))
8717 cast_expression
8718 = cp_parser_tokens_start_cast_expression (parser);
8720 /* Roll back the tokens we skipped. */
8721 cp_lexer_rollback_tokens (parser->lexer);
8722 /* If we aren't looking at a cast-expression, simulate an error so
8723 that the call to cp_parser_error_occurred below returns true. */
8724 if (!cast_expression)
8725 cp_parser_simulate_error (parser);
8726 else
8728 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8729 parser->in_type_id_in_expr_p = true;
8730 /* Look for the type-id. */
8731 type = cp_parser_type_id (parser);
8732 /* Look for the closing `)'. */
8733 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8734 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8737 /* Restore the saved message. */
8738 parser->type_definition_forbidden_message = saved_message;
8740 /* At this point this can only be either a cast or a
8741 parenthesized ctor such as `(T ())' that looks like a cast to
8742 function returning T. */
8743 if (!cp_parser_error_occurred (parser))
8745 /* Only commit if the cast-expression doesn't start with
8746 '++', '--', or '[' in C++11. */
8747 if (cast_expression > 0)
8748 cp_parser_commit_to_topmost_tentative_parse (parser);
8750 expr = cp_parser_cast_expression (parser,
8751 /*address_p=*/false,
8752 /*cast_p=*/true,
8753 /*decltype_p=*/false,
8754 pidk);
8756 if (cp_parser_parse_definitely (parser))
8758 /* Warn about old-style casts, if so requested. */
8759 if (warn_old_style_cast
8760 && !in_system_header_at (input_location)
8761 && !VOID_TYPE_P (type)
8762 && current_lang_name != lang_name_c)
8763 warning (OPT_Wold_style_cast, "use of old-style cast");
8765 /* Only type conversions to integral or enumeration types
8766 can be used in constant-expressions. */
8767 if (!cast_valid_in_integral_constant_expression_p (type)
8768 && cp_parser_non_integral_constant_expression (parser,
8769 NIC_CAST))
8770 return error_mark_node;
8772 /* Perform the cast. */
8773 /* Make a location:
8774 (TYPE) EXPR
8775 ^~~~~~~~~~~
8776 with start==caret at the open paren, extending to the
8777 end of "expr". */
8778 location_t cast_loc = make_location (open_paren_loc,
8779 open_paren_loc,
8780 expr.get_finish ());
8781 expr = build_c_cast (cast_loc, type, expr);
8782 return expr;
8785 else
8786 cp_parser_abort_tentative_parse (parser);
8789 /* If we get here, then it's not a cast, so it must be a
8790 unary-expression. */
8791 return cp_parser_unary_expression (parser, pidk, address_p,
8792 cast_p, decltype_p);
8795 /* Parse a binary expression of the general form:
8797 pm-expression:
8798 cast-expression
8799 pm-expression .* cast-expression
8800 pm-expression ->* cast-expression
8802 multiplicative-expression:
8803 pm-expression
8804 multiplicative-expression * pm-expression
8805 multiplicative-expression / pm-expression
8806 multiplicative-expression % pm-expression
8808 additive-expression:
8809 multiplicative-expression
8810 additive-expression + multiplicative-expression
8811 additive-expression - multiplicative-expression
8813 shift-expression:
8814 additive-expression
8815 shift-expression << additive-expression
8816 shift-expression >> additive-expression
8818 relational-expression:
8819 shift-expression
8820 relational-expression < shift-expression
8821 relational-expression > shift-expression
8822 relational-expression <= shift-expression
8823 relational-expression >= shift-expression
8825 GNU Extension:
8827 relational-expression:
8828 relational-expression <? shift-expression
8829 relational-expression >? shift-expression
8831 equality-expression:
8832 relational-expression
8833 equality-expression == relational-expression
8834 equality-expression != relational-expression
8836 and-expression:
8837 equality-expression
8838 and-expression & equality-expression
8840 exclusive-or-expression:
8841 and-expression
8842 exclusive-or-expression ^ and-expression
8844 inclusive-or-expression:
8845 exclusive-or-expression
8846 inclusive-or-expression | exclusive-or-expression
8848 logical-and-expression:
8849 inclusive-or-expression
8850 logical-and-expression && inclusive-or-expression
8852 logical-or-expression:
8853 logical-and-expression
8854 logical-or-expression || logical-and-expression
8856 All these are implemented with a single function like:
8858 binary-expression:
8859 simple-cast-expression
8860 binary-expression <token> binary-expression
8862 CAST_P is true if this expression is the target of a cast.
8864 The binops_by_token map is used to get the tree codes for each <token> type.
8865 binary-expressions are associated according to a precedence table. */
8867 #define TOKEN_PRECEDENCE(token) \
8868 (((token->type == CPP_GREATER \
8869 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8870 && !parser->greater_than_is_operator_p) \
8871 ? PREC_NOT_OPERATOR \
8872 : binops_by_token[token->type].prec)
8874 static cp_expr
8875 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8876 bool no_toplevel_fold_p,
8877 bool decltype_p,
8878 enum cp_parser_prec prec,
8879 cp_id_kind * pidk)
8881 cp_parser_expression_stack stack;
8882 cp_parser_expression_stack_entry *sp = &stack[0];
8883 cp_parser_expression_stack_entry current;
8884 cp_expr rhs;
8885 cp_token *token;
8886 enum tree_code rhs_type;
8887 enum cp_parser_prec new_prec, lookahead_prec;
8888 tree overload;
8890 /* Parse the first expression. */
8891 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8892 ? TRUTH_NOT_EXPR : ERROR_MARK);
8893 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8894 cast_p, decltype_p, pidk);
8895 current.prec = prec;
8897 if (cp_parser_error_occurred (parser))
8898 return error_mark_node;
8900 for (;;)
8902 /* Get an operator token. */
8903 token = cp_lexer_peek_token (parser->lexer);
8905 if (warn_cxx11_compat
8906 && token->type == CPP_RSHIFT
8907 && !parser->greater_than_is_operator_p)
8909 if (warning_at (token->location, OPT_Wc__11_compat,
8910 "%<>>%> operator is treated"
8911 " as two right angle brackets in C++11"))
8912 inform (token->location,
8913 "suggest parentheses around %<>>%> expression");
8916 new_prec = TOKEN_PRECEDENCE (token);
8917 if (new_prec != PREC_NOT_OPERATOR
8918 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8919 /* This is a fold-expression; handle it later. */
8920 new_prec = PREC_NOT_OPERATOR;
8922 /* Popping an entry off the stack means we completed a subexpression:
8923 - either we found a token which is not an operator (`>' where it is not
8924 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8925 will happen repeatedly;
8926 - or, we found an operator which has lower priority. This is the case
8927 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8928 parsing `3 * 4'. */
8929 if (new_prec <= current.prec)
8931 if (sp == stack)
8932 break;
8933 else
8934 goto pop;
8937 get_rhs:
8938 current.tree_type = binops_by_token[token->type].tree_type;
8939 current.loc = token->location;
8941 /* We used the operator token. */
8942 cp_lexer_consume_token (parser->lexer);
8944 /* For "false && x" or "true || x", x will never be executed;
8945 disable warnings while evaluating it. */
8946 if (current.tree_type == TRUTH_ANDIF_EXPR)
8947 c_inhibit_evaluation_warnings +=
8948 cp_fully_fold (current.lhs) == truthvalue_false_node;
8949 else if (current.tree_type == TRUTH_ORIF_EXPR)
8950 c_inhibit_evaluation_warnings +=
8951 cp_fully_fold (current.lhs) == truthvalue_true_node;
8953 /* Extract another operand. It may be the RHS of this expression
8954 or the LHS of a new, higher priority expression. */
8955 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8956 ? TRUTH_NOT_EXPR : ERROR_MARK);
8957 rhs = cp_parser_simple_cast_expression (parser);
8959 /* Get another operator token. Look up its precedence to avoid
8960 building a useless (immediately popped) stack entry for common
8961 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8962 token = cp_lexer_peek_token (parser->lexer);
8963 lookahead_prec = TOKEN_PRECEDENCE (token);
8964 if (lookahead_prec != PREC_NOT_OPERATOR
8965 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8966 lookahead_prec = PREC_NOT_OPERATOR;
8967 if (lookahead_prec > new_prec)
8969 /* ... and prepare to parse the RHS of the new, higher priority
8970 expression. Since precedence levels on the stack are
8971 monotonically increasing, we do not have to care about
8972 stack overflows. */
8973 *sp = current;
8974 ++sp;
8975 current.lhs = rhs;
8976 current.lhs_type = rhs_type;
8977 current.prec = new_prec;
8978 new_prec = lookahead_prec;
8979 goto get_rhs;
8981 pop:
8982 lookahead_prec = new_prec;
8983 /* If the stack is not empty, we have parsed into LHS the right side
8984 (`4' in the example above) of an expression we had suspended.
8985 We can use the information on the stack to recover the LHS (`3')
8986 from the stack together with the tree code (`MULT_EXPR'), and
8987 the precedence of the higher level subexpression
8988 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8989 which will be used to actually build the additive expression. */
8990 rhs = current.lhs;
8991 rhs_type = current.lhs_type;
8992 --sp;
8993 current = *sp;
8996 /* Undo the disabling of warnings done above. */
8997 if (current.tree_type == TRUTH_ANDIF_EXPR)
8998 c_inhibit_evaluation_warnings -=
8999 cp_fully_fold (current.lhs) == truthvalue_false_node;
9000 else if (current.tree_type == TRUTH_ORIF_EXPR)
9001 c_inhibit_evaluation_warnings -=
9002 cp_fully_fold (current.lhs) == truthvalue_true_node;
9004 if (warn_logical_not_paren
9005 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9006 && current.lhs_type == TRUTH_NOT_EXPR
9007 /* Avoid warning for !!x == y. */
9008 && (TREE_CODE (current.lhs) != NE_EXPR
9009 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9010 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9011 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9012 /* Avoid warning for !b == y where b is boolean. */
9013 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9014 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9015 != BOOLEAN_TYPE))))
9016 /* Avoid warning for !!b == y where b is boolean. */
9017 && (!DECL_P (current.lhs)
9018 || TREE_TYPE (current.lhs) == NULL_TREE
9019 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9020 warn_logical_not_parentheses (current.loc, current.tree_type,
9021 current.lhs, maybe_constant_value (rhs));
9023 overload = NULL;
9025 location_t combined_loc = make_location (current.loc,
9026 current.lhs.get_start (),
9027 rhs.get_finish ());
9029 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9030 ERROR_MARK for everything that is not a binary expression.
9031 This makes warn_about_parentheses miss some warnings that
9032 involve unary operators. For unary expressions we should
9033 pass the correct tree_code unless the unary expression was
9034 surrounded by parentheses.
9036 if (no_toplevel_fold_p
9037 && lookahead_prec <= current.prec
9038 && sp == stack)
9039 current.lhs = build2_loc (combined_loc,
9040 current.tree_type,
9041 TREE_CODE_CLASS (current.tree_type)
9042 == tcc_comparison
9043 ? boolean_type_node : TREE_TYPE (current.lhs),
9044 current.lhs, rhs);
9045 else
9047 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9048 current.lhs, current.lhs_type,
9049 rhs, rhs_type, &overload,
9050 complain_flags (decltype_p));
9051 /* TODO: build_x_binary_op doesn't always honor the location. */
9052 current.lhs.set_location (combined_loc);
9054 current.lhs_type = current.tree_type;
9056 /* If the binary operator required the use of an overloaded operator,
9057 then this expression cannot be an integral constant-expression.
9058 An overloaded operator can be used even if both operands are
9059 otherwise permissible in an integral constant-expression if at
9060 least one of the operands is of enumeration type. */
9062 if (overload
9063 && cp_parser_non_integral_constant_expression (parser,
9064 NIC_OVERLOADED))
9065 return error_mark_node;
9068 return current.lhs;
9071 static cp_expr
9072 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9073 bool no_toplevel_fold_p,
9074 enum cp_parser_prec prec,
9075 cp_id_kind * pidk)
9077 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9078 /*decltype*/false, prec, pidk);
9081 /* Parse the `? expression : assignment-expression' part of a
9082 conditional-expression. The LOGICAL_OR_EXPR is the
9083 logical-or-expression that started the conditional-expression.
9084 Returns a representation of the entire conditional-expression.
9086 This routine is used by cp_parser_assignment_expression.
9088 ? expression : assignment-expression
9090 GNU Extensions:
9092 ? : assignment-expression */
9094 static tree
9095 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9097 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9098 cp_expr assignment_expr;
9099 struct cp_token *token;
9100 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9102 /* Consume the `?' token. */
9103 cp_lexer_consume_token (parser->lexer);
9104 token = cp_lexer_peek_token (parser->lexer);
9105 if (cp_parser_allow_gnu_extensions_p (parser)
9106 && token->type == CPP_COLON)
9108 pedwarn (token->location, OPT_Wpedantic,
9109 "ISO C++ does not allow ?: with omitted middle operand");
9110 /* Implicit true clause. */
9111 expr = NULL_TREE;
9112 c_inhibit_evaluation_warnings +=
9113 folded_logical_or_expr == truthvalue_true_node;
9114 warn_for_omitted_condop (token->location, logical_or_expr);
9116 else
9118 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9119 parser->colon_corrects_to_scope_p = false;
9120 /* Parse the expression. */
9121 c_inhibit_evaluation_warnings +=
9122 folded_logical_or_expr == truthvalue_false_node;
9123 expr = cp_parser_expression (parser);
9124 c_inhibit_evaluation_warnings +=
9125 ((folded_logical_or_expr == truthvalue_true_node)
9126 - (folded_logical_or_expr == truthvalue_false_node));
9127 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9130 /* The next token should be a `:'. */
9131 cp_parser_require (parser, CPP_COLON, RT_COLON);
9132 /* Parse the assignment-expression. */
9133 assignment_expr = cp_parser_assignment_expression (parser);
9134 c_inhibit_evaluation_warnings -=
9135 folded_logical_or_expr == truthvalue_true_node;
9137 /* Make a location:
9138 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9139 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9140 with the caret at the "?", ranging from the start of
9141 the logical_or_expr to the end of the assignment_expr. */
9142 loc = make_location (loc,
9143 logical_or_expr.get_start (),
9144 assignment_expr.get_finish ());
9146 /* Build the conditional-expression. */
9147 return build_x_conditional_expr (loc, logical_or_expr,
9148 expr,
9149 assignment_expr,
9150 tf_warning_or_error);
9153 /* Parse an assignment-expression.
9155 assignment-expression:
9156 conditional-expression
9157 logical-or-expression assignment-operator assignment_expression
9158 throw-expression
9160 CAST_P is true if this expression is the target of a cast.
9161 DECLTYPE_P is true if this expression is the operand of decltype.
9163 Returns a representation for the expression. */
9165 static cp_expr
9166 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9167 bool cast_p, bool decltype_p)
9169 cp_expr expr;
9171 /* If the next token is the `throw' keyword, then we're looking at
9172 a throw-expression. */
9173 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9174 expr = cp_parser_throw_expression (parser);
9175 /* Otherwise, it must be that we are looking at a
9176 logical-or-expression. */
9177 else
9179 /* Parse the binary expressions (logical-or-expression). */
9180 expr = cp_parser_binary_expression (parser, cast_p, false,
9181 decltype_p,
9182 PREC_NOT_OPERATOR, pidk);
9183 /* If the next token is a `?' then we're actually looking at a
9184 conditional-expression. */
9185 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9186 return cp_parser_question_colon_clause (parser, expr);
9187 else
9189 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9191 /* If it's an assignment-operator, we're using the second
9192 production. */
9193 enum tree_code assignment_operator
9194 = cp_parser_assignment_operator_opt (parser);
9195 if (assignment_operator != ERROR_MARK)
9197 bool non_constant_p;
9199 /* Parse the right-hand side of the assignment. */
9200 cp_expr rhs = cp_parser_initializer_clause (parser,
9201 &non_constant_p);
9203 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9204 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9206 /* An assignment may not appear in a
9207 constant-expression. */
9208 if (cp_parser_non_integral_constant_expression (parser,
9209 NIC_ASSIGNMENT))
9210 return error_mark_node;
9211 /* Build the assignment expression. Its default
9212 location:
9213 LHS = RHS
9214 ~~~~^~~~~
9215 is the location of the '=' token as the
9216 caret, ranging from the start of the lhs to the
9217 end of the rhs. */
9218 loc = make_location (loc,
9219 expr.get_start (),
9220 rhs.get_finish ());
9221 expr = build_x_modify_expr (loc, expr,
9222 assignment_operator,
9223 rhs,
9224 complain_flags (decltype_p));
9225 /* TODO: build_x_modify_expr doesn't honor the location,
9226 so we must set it here. */
9227 expr.set_location (loc);
9232 return expr;
9235 /* Parse an (optional) assignment-operator.
9237 assignment-operator: one of
9238 = *= /= %= += -= >>= <<= &= ^= |=
9240 GNU Extension:
9242 assignment-operator: one of
9243 <?= >?=
9245 If the next token is an assignment operator, the corresponding tree
9246 code is returned, and the token is consumed. For example, for
9247 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9248 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9249 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9250 operator, ERROR_MARK is returned. */
9252 static enum tree_code
9253 cp_parser_assignment_operator_opt (cp_parser* parser)
9255 enum tree_code op;
9256 cp_token *token;
9258 /* Peek at the next token. */
9259 token = cp_lexer_peek_token (parser->lexer);
9261 switch (token->type)
9263 case CPP_EQ:
9264 op = NOP_EXPR;
9265 break;
9267 case CPP_MULT_EQ:
9268 op = MULT_EXPR;
9269 break;
9271 case CPP_DIV_EQ:
9272 op = TRUNC_DIV_EXPR;
9273 break;
9275 case CPP_MOD_EQ:
9276 op = TRUNC_MOD_EXPR;
9277 break;
9279 case CPP_PLUS_EQ:
9280 op = PLUS_EXPR;
9281 break;
9283 case CPP_MINUS_EQ:
9284 op = MINUS_EXPR;
9285 break;
9287 case CPP_RSHIFT_EQ:
9288 op = RSHIFT_EXPR;
9289 break;
9291 case CPP_LSHIFT_EQ:
9292 op = LSHIFT_EXPR;
9293 break;
9295 case CPP_AND_EQ:
9296 op = BIT_AND_EXPR;
9297 break;
9299 case CPP_XOR_EQ:
9300 op = BIT_XOR_EXPR;
9301 break;
9303 case CPP_OR_EQ:
9304 op = BIT_IOR_EXPR;
9305 break;
9307 default:
9308 /* Nothing else is an assignment operator. */
9309 op = ERROR_MARK;
9312 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9313 if (op != ERROR_MARK
9314 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9315 op = ERROR_MARK;
9317 /* If it was an assignment operator, consume it. */
9318 if (op != ERROR_MARK)
9319 cp_lexer_consume_token (parser->lexer);
9321 return op;
9324 /* Parse an expression.
9326 expression:
9327 assignment-expression
9328 expression , assignment-expression
9330 CAST_P is true if this expression is the target of a cast.
9331 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9332 except possibly parenthesized or on the RHS of a comma (N3276).
9334 Returns a representation of the expression. */
9336 static cp_expr
9337 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9338 bool cast_p, bool decltype_p)
9340 cp_expr expression = NULL_TREE;
9341 location_t loc = UNKNOWN_LOCATION;
9343 while (true)
9345 cp_expr assignment_expression;
9347 /* Parse the next assignment-expression. */
9348 assignment_expression
9349 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9351 /* We don't create a temporary for a call that is the immediate operand
9352 of decltype or on the RHS of a comma. But when we see a comma, we
9353 need to create a temporary for a call on the LHS. */
9354 if (decltype_p && !processing_template_decl
9355 && TREE_CODE (assignment_expression) == CALL_EXPR
9356 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9357 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9358 assignment_expression
9359 = build_cplus_new (TREE_TYPE (assignment_expression),
9360 assignment_expression, tf_warning_or_error);
9362 /* If this is the first assignment-expression, we can just
9363 save it away. */
9364 if (!expression)
9365 expression = assignment_expression;
9366 else
9368 /* Create a location with caret at the comma, ranging
9369 from the start of the LHS to the end of the RHS. */
9370 loc = make_location (loc,
9371 expression.get_start (),
9372 assignment_expression.get_finish ());
9373 expression = build_x_compound_expr (loc, expression,
9374 assignment_expression,
9375 complain_flags (decltype_p));
9376 expression.set_location (loc);
9378 /* If the next token is not a comma, or we're in a fold-expression, then
9379 we are done with the expression. */
9380 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9381 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9382 break;
9383 /* Consume the `,'. */
9384 loc = cp_lexer_peek_token (parser->lexer)->location;
9385 cp_lexer_consume_token (parser->lexer);
9386 /* A comma operator cannot appear in a constant-expression. */
9387 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9388 expression = error_mark_node;
9391 return expression;
9394 /* Parse a constant-expression.
9396 constant-expression:
9397 conditional-expression
9399 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9400 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9401 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9402 is false, NON_CONSTANT_P should be NULL. */
9404 static cp_expr
9405 cp_parser_constant_expression (cp_parser* parser,
9406 bool allow_non_constant_p,
9407 bool *non_constant_p)
9409 bool saved_integral_constant_expression_p;
9410 bool saved_allow_non_integral_constant_expression_p;
9411 bool saved_non_integral_constant_expression_p;
9412 cp_expr expression;
9414 /* It might seem that we could simply parse the
9415 conditional-expression, and then check to see if it were
9416 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9417 one that the compiler can figure out is constant, possibly after
9418 doing some simplifications or optimizations. The standard has a
9419 precise definition of constant-expression, and we must honor
9420 that, even though it is somewhat more restrictive.
9422 For example:
9424 int i[(2, 3)];
9426 is not a legal declaration, because `(2, 3)' is not a
9427 constant-expression. The `,' operator is forbidden in a
9428 constant-expression. However, GCC's constant-folding machinery
9429 will fold this operation to an INTEGER_CST for `3'. */
9431 /* Save the old settings. */
9432 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9433 saved_allow_non_integral_constant_expression_p
9434 = parser->allow_non_integral_constant_expression_p;
9435 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9436 /* We are now parsing a constant-expression. */
9437 parser->integral_constant_expression_p = true;
9438 parser->allow_non_integral_constant_expression_p
9439 = (allow_non_constant_p || cxx_dialect >= cxx11);
9440 parser->non_integral_constant_expression_p = false;
9441 /* Although the grammar says "conditional-expression", we parse an
9442 "assignment-expression", which also permits "throw-expression"
9443 and the use of assignment operators. In the case that
9444 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9445 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9446 actually essential that we look for an assignment-expression.
9447 For example, cp_parser_initializer_clauses uses this function to
9448 determine whether a particular assignment-expression is in fact
9449 constant. */
9450 expression = cp_parser_assignment_expression (parser);
9451 /* Restore the old settings. */
9452 parser->integral_constant_expression_p
9453 = saved_integral_constant_expression_p;
9454 parser->allow_non_integral_constant_expression_p
9455 = saved_allow_non_integral_constant_expression_p;
9456 if (cxx_dialect >= cxx11)
9458 /* Require an rvalue constant expression here; that's what our
9459 callers expect. Reference constant expressions are handled
9460 separately in e.g. cp_parser_template_argument. */
9461 bool is_const = potential_rvalue_constant_expression (expression);
9462 parser->non_integral_constant_expression_p = !is_const;
9463 if (!is_const && !allow_non_constant_p)
9464 require_potential_rvalue_constant_expression (expression);
9466 if (allow_non_constant_p)
9467 *non_constant_p = parser->non_integral_constant_expression_p;
9468 parser->non_integral_constant_expression_p
9469 = saved_non_integral_constant_expression_p;
9471 return expression;
9474 /* Parse __builtin_offsetof.
9476 offsetof-expression:
9477 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9479 offsetof-member-designator:
9480 id-expression
9481 | offsetof-member-designator "." id-expression
9482 | offsetof-member-designator "[" expression "]"
9483 | offsetof-member-designator "->" id-expression */
9485 static cp_expr
9486 cp_parser_builtin_offsetof (cp_parser *parser)
9488 int save_ice_p, save_non_ice_p;
9489 tree type;
9490 cp_expr expr;
9491 cp_id_kind dummy;
9492 cp_token *token;
9493 location_t finish_loc;
9495 /* We're about to accept non-integral-constant things, but will
9496 definitely yield an integral constant expression. Save and
9497 restore these values around our local parsing. */
9498 save_ice_p = parser->integral_constant_expression_p;
9499 save_non_ice_p = parser->non_integral_constant_expression_p;
9501 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9503 /* Consume the "__builtin_offsetof" token. */
9504 cp_lexer_consume_token (parser->lexer);
9505 /* Consume the opening `('. */
9506 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9507 /* Parse the type-id. */
9508 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9509 type = cp_parser_type_id (parser);
9510 /* Look for the `,'. */
9511 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9512 token = cp_lexer_peek_token (parser->lexer);
9514 /* Build the (type *)null that begins the traditional offsetof macro. */
9515 tree object_ptr
9516 = build_static_cast (build_pointer_type (type), null_pointer_node,
9517 tf_warning_or_error);
9519 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9520 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9521 true, &dummy, token->location);
9522 while (true)
9524 token = cp_lexer_peek_token (parser->lexer);
9525 switch (token->type)
9527 case CPP_OPEN_SQUARE:
9528 /* offsetof-member-designator "[" expression "]" */
9529 expr = cp_parser_postfix_open_square_expression (parser, expr,
9530 true, false);
9531 break;
9533 case CPP_DEREF:
9534 /* offsetof-member-designator "->" identifier */
9535 expr = grok_array_decl (token->location, expr,
9536 integer_zero_node, false);
9537 /* FALLTHRU */
9539 case CPP_DOT:
9540 /* offsetof-member-designator "." identifier */
9541 cp_lexer_consume_token (parser->lexer);
9542 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9543 expr, true, &dummy,
9544 token->location);
9545 break;
9547 case CPP_CLOSE_PAREN:
9548 /* Consume the ")" token. */
9549 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9550 cp_lexer_consume_token (parser->lexer);
9551 goto success;
9553 default:
9554 /* Error. We know the following require will fail, but
9555 that gives the proper error message. */
9556 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9557 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9558 expr = error_mark_node;
9559 goto failure;
9563 success:
9564 /* Make a location of the form:
9565 __builtin_offsetof (struct s, f)
9566 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9567 with caret at the type-id, ranging from the start of the
9568 "_builtin_offsetof" token to the close paren. */
9569 loc = make_location (loc, start_loc, finish_loc);
9570 /* The result will be an INTEGER_CST, so we need to explicitly
9571 preserve the location. */
9572 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9574 failure:
9575 parser->integral_constant_expression_p = save_ice_p;
9576 parser->non_integral_constant_expression_p = save_non_ice_p;
9578 return expr;
9581 /* Parse a trait expression.
9583 Returns a representation of the expression, the underlying type
9584 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9586 static tree
9587 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9589 cp_trait_kind kind;
9590 tree type1, type2 = NULL_TREE;
9591 bool binary = false;
9592 bool variadic = false;
9594 switch (keyword)
9596 case RID_HAS_NOTHROW_ASSIGN:
9597 kind = CPTK_HAS_NOTHROW_ASSIGN;
9598 break;
9599 case RID_HAS_NOTHROW_CONSTRUCTOR:
9600 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9601 break;
9602 case RID_HAS_NOTHROW_COPY:
9603 kind = CPTK_HAS_NOTHROW_COPY;
9604 break;
9605 case RID_HAS_TRIVIAL_ASSIGN:
9606 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9607 break;
9608 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9609 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9610 break;
9611 case RID_HAS_TRIVIAL_COPY:
9612 kind = CPTK_HAS_TRIVIAL_COPY;
9613 break;
9614 case RID_HAS_TRIVIAL_DESTRUCTOR:
9615 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9616 break;
9617 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9618 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9619 break;
9620 case RID_HAS_VIRTUAL_DESTRUCTOR:
9621 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9622 break;
9623 case RID_IS_ABSTRACT:
9624 kind = CPTK_IS_ABSTRACT;
9625 break;
9626 case RID_IS_AGGREGATE:
9627 kind = CPTK_IS_AGGREGATE;
9628 break;
9629 case RID_IS_BASE_OF:
9630 kind = CPTK_IS_BASE_OF;
9631 binary = true;
9632 break;
9633 case RID_IS_CLASS:
9634 kind = CPTK_IS_CLASS;
9635 break;
9636 case RID_IS_EMPTY:
9637 kind = CPTK_IS_EMPTY;
9638 break;
9639 case RID_IS_ENUM:
9640 kind = CPTK_IS_ENUM;
9641 break;
9642 case RID_IS_FINAL:
9643 kind = CPTK_IS_FINAL;
9644 break;
9645 case RID_IS_LITERAL_TYPE:
9646 kind = CPTK_IS_LITERAL_TYPE;
9647 break;
9648 case RID_IS_POD:
9649 kind = CPTK_IS_POD;
9650 break;
9651 case RID_IS_POLYMORPHIC:
9652 kind = CPTK_IS_POLYMORPHIC;
9653 break;
9654 case RID_IS_SAME_AS:
9655 kind = CPTK_IS_SAME_AS;
9656 binary = true;
9657 break;
9658 case RID_IS_STD_LAYOUT:
9659 kind = CPTK_IS_STD_LAYOUT;
9660 break;
9661 case RID_IS_TRIVIAL:
9662 kind = CPTK_IS_TRIVIAL;
9663 break;
9664 case RID_IS_TRIVIALLY_ASSIGNABLE:
9665 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9666 binary = true;
9667 break;
9668 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9669 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9670 variadic = true;
9671 break;
9672 case RID_IS_TRIVIALLY_COPYABLE:
9673 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9674 break;
9675 case RID_IS_UNION:
9676 kind = CPTK_IS_UNION;
9677 break;
9678 case RID_UNDERLYING_TYPE:
9679 kind = CPTK_UNDERLYING_TYPE;
9680 break;
9681 case RID_BASES:
9682 kind = CPTK_BASES;
9683 break;
9684 case RID_DIRECT_BASES:
9685 kind = CPTK_DIRECT_BASES;
9686 break;
9687 default:
9688 gcc_unreachable ();
9691 /* Consume the token. */
9692 cp_lexer_consume_token (parser->lexer);
9694 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9697 type_id_in_expr_sentinel s (parser);
9698 type1 = cp_parser_type_id (parser);
9701 if (type1 == error_mark_node)
9702 return error_mark_node;
9704 if (binary)
9706 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9709 type_id_in_expr_sentinel s (parser);
9710 type2 = cp_parser_type_id (parser);
9713 if (type2 == error_mark_node)
9714 return error_mark_node;
9716 else if (variadic)
9718 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9720 cp_lexer_consume_token (parser->lexer);
9721 tree elt = cp_parser_type_id (parser);
9722 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9724 cp_lexer_consume_token (parser->lexer);
9725 elt = make_pack_expansion (elt);
9727 if (elt == error_mark_node)
9728 return error_mark_node;
9729 type2 = tree_cons (NULL_TREE, elt, type2);
9733 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9735 /* Complete the trait expression, which may mean either processing
9736 the trait expr now or saving it for template instantiation. */
9737 switch (kind)
9739 case CPTK_UNDERLYING_TYPE:
9740 return finish_underlying_type (type1);
9741 case CPTK_BASES:
9742 return finish_bases (type1, false);
9743 case CPTK_DIRECT_BASES:
9744 return finish_bases (type1, true);
9745 default:
9746 return finish_trait_expr (kind, type1, type2);
9750 /* Lambdas that appear in variable initializer or default argument scope
9751 get that in their mangling, so we need to record it. We might as well
9752 use the count for function and namespace scopes as well. */
9753 static GTY(()) tree lambda_scope;
9754 static GTY(()) int lambda_count;
9755 struct GTY(()) tree_int
9757 tree t;
9758 int i;
9760 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9762 static void
9763 start_lambda_scope (tree decl)
9765 tree_int ti;
9766 gcc_assert (decl);
9767 /* Once we're inside a function, we ignore other scopes and just push
9768 the function again so that popping works properly. */
9769 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9770 decl = current_function_decl;
9771 ti.t = lambda_scope;
9772 ti.i = lambda_count;
9773 vec_safe_push (lambda_scope_stack, ti);
9774 if (lambda_scope != decl)
9776 /* Don't reset the count if we're still in the same function. */
9777 lambda_scope = decl;
9778 lambda_count = 0;
9782 static void
9783 record_lambda_scope (tree lambda)
9785 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9786 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9789 static void
9790 finish_lambda_scope (void)
9792 tree_int *p = &lambda_scope_stack->last ();
9793 if (lambda_scope != p->t)
9795 lambda_scope = p->t;
9796 lambda_count = p->i;
9798 lambda_scope_stack->pop ();
9801 /* Parse a lambda expression.
9803 lambda-expression:
9804 lambda-introducer lambda-declarator [opt] compound-statement
9806 Returns a representation of the expression. */
9808 static cp_expr
9809 cp_parser_lambda_expression (cp_parser* parser)
9811 tree lambda_expr = build_lambda_expr ();
9812 tree type;
9813 bool ok = true;
9814 cp_token *token = cp_lexer_peek_token (parser->lexer);
9815 cp_token_position start = 0;
9817 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9819 if (cp_unevaluated_operand)
9821 if (!token->error_reported)
9823 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9824 "lambda-expression in unevaluated context");
9825 token->error_reported = true;
9827 ok = false;
9829 else if (parser->in_template_argument_list_p)
9831 if (!token->error_reported)
9833 error_at (token->location, "lambda-expression in template-argument");
9834 token->error_reported = true;
9836 ok = false;
9839 /* We may be in the middle of deferred access check. Disable
9840 it now. */
9841 push_deferring_access_checks (dk_no_deferred);
9843 cp_parser_lambda_introducer (parser, lambda_expr);
9845 type = begin_lambda_type (lambda_expr);
9846 if (type == error_mark_node)
9847 return error_mark_node;
9849 record_lambda_scope (lambda_expr);
9851 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9852 determine_visibility (TYPE_NAME (type));
9854 /* Now that we've started the type, add the capture fields for any
9855 explicit captures. */
9856 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9859 /* Inside the class, surrounding template-parameter-lists do not apply. */
9860 unsigned int saved_num_template_parameter_lists
9861 = parser->num_template_parameter_lists;
9862 unsigned char in_statement = parser->in_statement;
9863 bool in_switch_statement_p = parser->in_switch_statement_p;
9864 bool fully_implicit_function_template_p
9865 = parser->fully_implicit_function_template_p;
9866 tree implicit_template_parms = parser->implicit_template_parms;
9867 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9868 bool auto_is_implicit_function_template_parm_p
9869 = parser->auto_is_implicit_function_template_parm_p;
9871 parser->num_template_parameter_lists = 0;
9872 parser->in_statement = 0;
9873 parser->in_switch_statement_p = false;
9874 parser->fully_implicit_function_template_p = false;
9875 parser->implicit_template_parms = 0;
9876 parser->implicit_template_scope = 0;
9877 parser->auto_is_implicit_function_template_parm_p = false;
9879 /* By virtue of defining a local class, a lambda expression has access to
9880 the private variables of enclosing classes. */
9882 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9884 if (ok && cp_parser_error_occurred (parser))
9885 ok = false;
9887 if (ok)
9889 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9890 && cp_parser_start_tentative_firewall (parser))
9891 start = token;
9892 cp_parser_lambda_body (parser, lambda_expr);
9894 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9896 if (cp_parser_skip_to_closing_brace (parser))
9897 cp_lexer_consume_token (parser->lexer);
9900 /* The capture list was built up in reverse order; fix that now. */
9901 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9902 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9904 if (ok)
9905 maybe_add_lambda_conv_op (type);
9907 type = finish_struct (type, /*attributes=*/NULL_TREE);
9909 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9910 parser->in_statement = in_statement;
9911 parser->in_switch_statement_p = in_switch_statement_p;
9912 parser->fully_implicit_function_template_p
9913 = fully_implicit_function_template_p;
9914 parser->implicit_template_parms = implicit_template_parms;
9915 parser->implicit_template_scope = implicit_template_scope;
9916 parser->auto_is_implicit_function_template_parm_p
9917 = auto_is_implicit_function_template_parm_p;
9920 /* This field is only used during parsing of the lambda. */
9921 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9923 /* This lambda shouldn't have any proxies left at this point. */
9924 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9925 /* And now that we're done, push proxies for an enclosing lambda. */
9926 insert_pending_capture_proxies ();
9928 if (ok)
9929 lambda_expr = build_lambda_object (lambda_expr);
9930 else
9931 lambda_expr = error_mark_node;
9933 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9935 pop_deferring_access_checks ();
9937 return lambda_expr;
9940 /* Parse the beginning of a lambda expression.
9942 lambda-introducer:
9943 [ lambda-capture [opt] ]
9945 LAMBDA_EXPR is the current representation of the lambda expression. */
9947 static void
9948 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9950 /* Need commas after the first capture. */
9951 bool first = true;
9953 /* Eat the leading `['. */
9954 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9956 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9957 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9958 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9959 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9960 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9961 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9963 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9965 cp_lexer_consume_token (parser->lexer);
9966 first = false;
9969 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9971 cp_token* capture_token;
9972 tree capture_id;
9973 tree capture_init_expr;
9974 cp_id_kind idk = CP_ID_KIND_NONE;
9975 bool explicit_init_p = false;
9977 enum capture_kind_type
9979 BY_COPY,
9980 BY_REFERENCE
9982 enum capture_kind_type capture_kind = BY_COPY;
9984 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9986 error ("expected end of capture-list");
9987 return;
9990 if (first)
9991 first = false;
9992 else
9993 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9995 /* Possibly capture `this'. */
9996 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9998 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9999 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10000 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10001 "with by-copy capture default");
10002 cp_lexer_consume_token (parser->lexer);
10003 add_capture (lambda_expr,
10004 /*id=*/this_identifier,
10005 /*initializer=*/finish_this_expr (),
10006 /*by_reference_p=*/true,
10007 explicit_init_p);
10008 continue;
10011 /* Possibly capture `*this'. */
10012 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10013 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10015 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10016 if (cxx_dialect < cxx1z)
10017 pedwarn (loc, 0, "%<*this%> capture only available with "
10018 "-std=c++1z or -std=gnu++1z");
10019 cp_lexer_consume_token (parser->lexer);
10020 cp_lexer_consume_token (parser->lexer);
10021 add_capture (lambda_expr,
10022 /*id=*/this_identifier,
10023 /*initializer=*/finish_this_expr (),
10024 /*by_reference_p=*/false,
10025 explicit_init_p);
10026 continue;
10029 /* Remember whether we want to capture as a reference or not. */
10030 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10032 capture_kind = BY_REFERENCE;
10033 cp_lexer_consume_token (parser->lexer);
10036 /* Get the identifier. */
10037 capture_token = cp_lexer_peek_token (parser->lexer);
10038 capture_id = cp_parser_identifier (parser);
10040 if (capture_id == error_mark_node)
10041 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10042 delimiters, but I modified this to stop on unnested ']' as well. It
10043 was already changed to stop on unnested '}', so the
10044 "closing_parenthesis" name is no more misleading with my change. */
10046 cp_parser_skip_to_closing_parenthesis (parser,
10047 /*recovering=*/true,
10048 /*or_comma=*/true,
10049 /*consume_paren=*/true);
10050 break;
10053 /* Find the initializer for this capture. */
10054 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10055 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10056 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10058 bool direct, non_constant;
10059 /* An explicit initializer exists. */
10060 if (cxx_dialect < cxx14)
10061 pedwarn (input_location, 0,
10062 "lambda capture initializers "
10063 "only available with -std=c++14 or -std=gnu++14");
10064 capture_init_expr = cp_parser_initializer (parser, &direct,
10065 &non_constant);
10066 explicit_init_p = true;
10067 if (capture_init_expr == NULL_TREE)
10069 error ("empty initializer for lambda init-capture");
10070 capture_init_expr = error_mark_node;
10073 else
10075 const char* error_msg;
10077 /* Turn the identifier into an id-expression. */
10078 capture_init_expr
10079 = cp_parser_lookup_name_simple (parser, capture_id,
10080 capture_token->location);
10082 if (capture_init_expr == error_mark_node)
10084 unqualified_name_lookup_error (capture_id);
10085 continue;
10087 else if (DECL_P (capture_init_expr)
10088 && (!VAR_P (capture_init_expr)
10089 && TREE_CODE (capture_init_expr) != PARM_DECL))
10091 error_at (capture_token->location,
10092 "capture of non-variable %qD ",
10093 capture_init_expr);
10094 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10095 "%q#D declared here", capture_init_expr);
10096 continue;
10098 if (VAR_P (capture_init_expr)
10099 && decl_storage_duration (capture_init_expr) != dk_auto)
10101 if (pedwarn (capture_token->location, 0, "capture of variable "
10102 "%qD with non-automatic storage duration",
10103 capture_init_expr))
10104 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10105 "%q#D declared here", capture_init_expr);
10106 continue;
10109 capture_init_expr
10110 = finish_id_expression
10111 (capture_id,
10112 capture_init_expr,
10113 parser->scope,
10114 &idk,
10115 /*integral_constant_expression_p=*/false,
10116 /*allow_non_integral_constant_expression_p=*/false,
10117 /*non_integral_constant_expression_p=*/NULL,
10118 /*template_p=*/false,
10119 /*done=*/true,
10120 /*address_p=*/false,
10121 /*template_arg_p=*/false,
10122 &error_msg,
10123 capture_token->location);
10125 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10127 cp_lexer_consume_token (parser->lexer);
10128 capture_init_expr = make_pack_expansion (capture_init_expr);
10130 else
10131 check_for_bare_parameter_packs (capture_init_expr);
10134 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10135 && !explicit_init_p)
10137 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10138 && capture_kind == BY_COPY)
10139 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10140 "of %qD redundant with by-copy capture default",
10141 capture_id);
10142 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10143 && capture_kind == BY_REFERENCE)
10144 pedwarn (capture_token->location, 0, "explicit by-reference "
10145 "capture of %qD redundant with by-reference capture "
10146 "default", capture_id);
10149 add_capture (lambda_expr,
10150 capture_id,
10151 capture_init_expr,
10152 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10153 explicit_init_p);
10156 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10159 /* Parse the (optional) middle of a lambda expression.
10161 lambda-declarator:
10162 < template-parameter-list [opt] >
10163 ( parameter-declaration-clause [opt] )
10164 attribute-specifier [opt]
10165 decl-specifier-seq [opt]
10166 exception-specification [opt]
10167 lambda-return-type-clause [opt]
10169 LAMBDA_EXPR is the current representation of the lambda expression. */
10171 static bool
10172 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10174 /* 5.1.1.4 of the standard says:
10175 If a lambda-expression does not include a lambda-declarator, it is as if
10176 the lambda-declarator were ().
10177 This means an empty parameter list, no attributes, and no exception
10178 specification. */
10179 tree param_list = void_list_node;
10180 tree attributes = NULL_TREE;
10181 tree exception_spec = NULL_TREE;
10182 tree template_param_list = NULL_TREE;
10183 tree tx_qual = NULL_TREE;
10184 cp_decl_specifier_seq lambda_specs;
10185 clear_decl_specs (&lambda_specs);
10187 /* The template-parameter-list is optional, but must begin with
10188 an opening angle if present. */
10189 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10191 if (cxx_dialect < cxx14)
10192 pedwarn (parser->lexer->next_token->location, 0,
10193 "lambda templates are only available with "
10194 "-std=c++14 or -std=gnu++14");
10195 else
10196 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10197 "ISO C++ does not support lambda templates");
10199 cp_lexer_consume_token (parser->lexer);
10201 template_param_list = cp_parser_template_parameter_list (parser);
10203 cp_parser_skip_to_end_of_template_parameter_list (parser);
10205 /* We just processed one more parameter list. */
10206 ++parser->num_template_parameter_lists;
10209 /* The parameter-declaration-clause is optional (unless
10210 template-parameter-list was given), but must begin with an
10211 opening parenthesis if present. */
10212 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10214 cp_lexer_consume_token (parser->lexer);
10216 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10218 /* Parse parameters. */
10219 param_list = cp_parser_parameter_declaration_clause (parser);
10221 /* Default arguments shall not be specified in the
10222 parameter-declaration-clause of a lambda-declarator. */
10223 if (cxx_dialect < cxx14)
10224 for (tree t = param_list; t; t = TREE_CHAIN (t))
10225 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10226 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10227 "default argument specified for lambda parameter");
10229 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10231 attributes = cp_parser_attributes_opt (parser);
10233 /* In the decl-specifier-seq of the lambda-declarator, each
10234 decl-specifier shall either be mutable or constexpr. */
10235 int declares_class_or_enum;
10236 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10237 cp_parser_decl_specifier_seq (parser,
10238 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10239 &lambda_specs, &declares_class_or_enum);
10240 if (lambda_specs.storage_class == sc_mutable)
10242 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10243 if (lambda_specs.conflicting_specifiers_p)
10244 error_at (lambda_specs.locations[ds_storage_class],
10245 "duplicate %<mutable%>");
10248 tx_qual = cp_parser_tx_qualifier_opt (parser);
10250 /* Parse optional exception specification. */
10251 exception_spec = cp_parser_exception_specification_opt (parser);
10253 /* Parse optional trailing return type. */
10254 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10256 cp_lexer_consume_token (parser->lexer);
10257 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10258 = cp_parser_trailing_type_id (parser);
10261 /* The function parameters must be in scope all the way until after the
10262 trailing-return-type in case of decltype. */
10263 pop_bindings_and_leave_scope ();
10265 else if (template_param_list != NULL_TREE) // generate diagnostic
10266 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10268 /* Create the function call operator.
10270 Messing with declarators like this is no uglier than building up the
10271 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10272 other code. */
10274 cp_decl_specifier_seq return_type_specs;
10275 cp_declarator* declarator;
10276 tree fco;
10277 int quals;
10278 void *p;
10280 clear_decl_specs (&return_type_specs);
10281 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10282 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10283 else
10284 /* Maybe we will deduce the return type later. */
10285 return_type_specs.type = make_auto ();
10287 if (lambda_specs.locations[ds_constexpr])
10289 if (cxx_dialect >= cxx1z)
10290 return_type_specs.locations[ds_constexpr]
10291 = lambda_specs.locations[ds_constexpr];
10292 else
10293 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10294 "lambda only available with -std=c++1z or -std=gnu++1z");
10297 p = obstack_alloc (&declarator_obstack, 0);
10299 declarator = make_id_declarator (NULL_TREE, cp_operator_id (CALL_EXPR),
10300 sfk_none);
10302 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10303 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10304 declarator = make_call_declarator (declarator, param_list, quals,
10305 VIRT_SPEC_UNSPECIFIED,
10306 REF_QUAL_NONE,
10307 tx_qual,
10308 exception_spec,
10309 /*late_return_type=*/NULL_TREE,
10310 /*requires_clause*/NULL_TREE);
10311 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10313 fco = grokmethod (&return_type_specs,
10314 declarator,
10315 attributes);
10316 if (fco != error_mark_node)
10318 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10319 DECL_ARTIFICIAL (fco) = 1;
10320 /* Give the object parameter a different name. */
10321 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10322 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10323 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10325 if (template_param_list)
10327 fco = finish_member_template_decl (fco);
10328 finish_template_decl (template_param_list);
10329 --parser->num_template_parameter_lists;
10331 else if (parser->fully_implicit_function_template_p)
10332 fco = finish_fully_implicit_template (parser, fco);
10334 finish_member_declaration (fco);
10336 obstack_free (&declarator_obstack, p);
10338 return (fco != error_mark_node);
10342 /* Parse the body of a lambda expression, which is simply
10344 compound-statement
10346 but which requires special handling.
10347 LAMBDA_EXPR is the current representation of the lambda expression. */
10349 static void
10350 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10352 bool nested = (current_function_decl != NULL_TREE);
10353 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10354 if (nested)
10355 push_function_context ();
10356 else
10357 /* Still increment function_depth so that we don't GC in the
10358 middle of an expression. */
10359 ++function_depth;
10360 vec<tree> omp_privatization_save;
10361 save_omp_privatization_clauses (omp_privatization_save);
10362 /* Clear this in case we're in the middle of a default argument. */
10363 parser->local_variables_forbidden_p = false;
10365 /* Finish the function call operator
10366 - class_specifier
10367 + late_parsing_for_member
10368 + function_definition_after_declarator
10369 + ctor_initializer_opt_and_function_body */
10371 tree fco = lambda_function (lambda_expr);
10372 tree body;
10373 bool done = false;
10374 tree compound_stmt;
10375 tree cap;
10377 /* Let the front end know that we are going to be defining this
10378 function. */
10379 start_preparsed_function (fco,
10380 NULL_TREE,
10381 SF_PRE_PARSED | SF_INCLASS_INLINE);
10383 start_lambda_scope (fco);
10384 body = begin_function_body ();
10386 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10387 goto out;
10389 /* Push the proxies for any explicit captures. */
10390 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10391 cap = TREE_CHAIN (cap))
10392 build_capture_proxy (TREE_PURPOSE (cap));
10394 compound_stmt = begin_compound_stmt (0);
10396 /* 5.1.1.4 of the standard says:
10397 If a lambda-expression does not include a trailing-return-type, it
10398 is as if the trailing-return-type denotes the following type:
10399 * if the compound-statement is of the form
10400 { return attribute-specifier [opt] expression ; }
10401 the type of the returned expression after lvalue-to-rvalue
10402 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10403 (_conv.array_ 4.2), and function-to-pointer conversion
10404 (_conv.func_ 4.3);
10405 * otherwise, void. */
10407 /* In a lambda that has neither a lambda-return-type-clause
10408 nor a deducible form, errors should be reported for return statements
10409 in the body. Since we used void as the placeholder return type, parsing
10410 the body as usual will give such desired behavior. */
10411 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10412 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10413 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10415 tree expr = NULL_TREE;
10416 cp_id_kind idk = CP_ID_KIND_NONE;
10418 /* Parse tentatively in case there's more after the initial return
10419 statement. */
10420 cp_parser_parse_tentatively (parser);
10422 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10424 expr = cp_parser_expression (parser, &idk);
10426 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10427 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10429 if (cp_parser_parse_definitely (parser))
10431 if (!processing_template_decl)
10433 tree type = lambda_return_type (expr);
10434 apply_deduced_return_type (fco, type);
10435 if (type == error_mark_node)
10436 expr = error_mark_node;
10439 /* Will get error here if type not deduced yet. */
10440 finish_return_stmt (expr);
10442 done = true;
10446 if (!done)
10448 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10449 cp_parser_label_declaration (parser);
10450 cp_parser_statement_seq_opt (parser, NULL_TREE);
10451 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10454 finish_compound_stmt (compound_stmt);
10456 out:
10457 finish_function_body (body);
10458 finish_lambda_scope ();
10460 /* Finish the function and generate code for it if necessary. */
10461 tree fn = finish_function (/*inline*/2);
10463 /* Only expand if the call op is not a template. */
10464 if (!DECL_TEMPLATE_INFO (fco))
10465 expand_or_defer_fn (fn);
10468 restore_omp_privatization_clauses (omp_privatization_save);
10469 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10470 if (nested)
10471 pop_function_context();
10472 else
10473 --function_depth;
10476 /* Statements [gram.stmt.stmt] */
10478 /* Parse a statement.
10480 statement:
10481 labeled-statement
10482 expression-statement
10483 compound-statement
10484 selection-statement
10485 iteration-statement
10486 jump-statement
10487 declaration-statement
10488 try-block
10490 C++11:
10492 statement:
10493 labeled-statement
10494 attribute-specifier-seq (opt) expression-statement
10495 attribute-specifier-seq (opt) compound-statement
10496 attribute-specifier-seq (opt) selection-statement
10497 attribute-specifier-seq (opt) iteration-statement
10498 attribute-specifier-seq (opt) jump-statement
10499 declaration-statement
10500 attribute-specifier-seq (opt) try-block
10502 init-statement:
10503 expression-statement
10504 simple-declaration
10506 TM Extension:
10508 statement:
10509 atomic-statement
10511 IN_COMPOUND is true when the statement is nested inside a
10512 cp_parser_compound_statement; this matters for certain pragmas.
10514 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10515 is a (possibly labeled) if statement which is not enclosed in braces
10516 and has an else clause. This is used to implement -Wparentheses.
10518 CHAIN is a vector of if-else-if conditions. */
10520 static void
10521 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10522 bool in_compound, bool *if_p, vec<tree> *chain)
10524 tree statement, std_attrs = NULL_TREE;
10525 cp_token *token;
10526 location_t statement_location, attrs_location;
10528 restart:
10529 if (if_p != NULL)
10530 *if_p = false;
10531 /* There is no statement yet. */
10532 statement = NULL_TREE;
10534 saved_token_sentinel saved_tokens (parser->lexer);
10535 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10536 if (c_dialect_objc ())
10537 /* In obj-c++, seeing '[[' might be the either the beginning of
10538 c++11 attributes, or a nested objc-message-expression. So
10539 let's parse the c++11 attributes tentatively. */
10540 cp_parser_parse_tentatively (parser);
10541 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10542 if (c_dialect_objc ())
10544 if (!cp_parser_parse_definitely (parser))
10545 std_attrs = NULL_TREE;
10548 /* Peek at the next token. */
10549 token = cp_lexer_peek_token (parser->lexer);
10550 /* Remember the location of the first token in the statement. */
10551 statement_location = token->location;
10552 /* If this is a keyword, then that will often determine what kind of
10553 statement we have. */
10554 if (token->type == CPP_KEYWORD)
10556 enum rid keyword = token->keyword;
10558 switch (keyword)
10560 case RID_CASE:
10561 case RID_DEFAULT:
10562 /* Looks like a labeled-statement with a case label.
10563 Parse the label, and then use tail recursion to parse
10564 the statement. */
10565 cp_parser_label_for_labeled_statement (parser, std_attrs);
10566 in_compound = false;
10567 goto restart;
10569 case RID_IF:
10570 case RID_SWITCH:
10571 statement = cp_parser_selection_statement (parser, if_p, chain);
10572 break;
10574 case RID_WHILE:
10575 case RID_DO:
10576 case RID_FOR:
10577 statement = cp_parser_iteration_statement (parser, if_p, false);
10578 break;
10580 case RID_CILK_FOR:
10581 if (!flag_cilkplus)
10583 error_at (cp_lexer_peek_token (parser->lexer)->location,
10584 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10585 cp_lexer_consume_token (parser->lexer);
10586 statement = error_mark_node;
10588 else
10589 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10590 break;
10592 case RID_BREAK:
10593 case RID_CONTINUE:
10594 case RID_RETURN:
10595 case RID_GOTO:
10596 statement = cp_parser_jump_statement (parser);
10597 break;
10599 case RID_CILK_SYNC:
10600 cp_lexer_consume_token (parser->lexer);
10601 if (flag_cilkplus)
10603 tree sync_expr = build_cilk_sync ();
10604 SET_EXPR_LOCATION (sync_expr,
10605 token->location);
10606 statement = finish_expr_stmt (sync_expr);
10608 else
10610 error_at (token->location, "-fcilkplus must be enabled to use"
10611 " %<_Cilk_sync%>");
10612 statement = error_mark_node;
10614 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10615 break;
10617 /* Objective-C++ exception-handling constructs. */
10618 case RID_AT_TRY:
10619 case RID_AT_CATCH:
10620 case RID_AT_FINALLY:
10621 case RID_AT_SYNCHRONIZED:
10622 case RID_AT_THROW:
10623 statement = cp_parser_objc_statement (parser);
10624 break;
10626 case RID_TRY:
10627 statement = cp_parser_try_block (parser);
10628 break;
10630 case RID_NAMESPACE:
10631 /* This must be a namespace alias definition. */
10632 cp_parser_declaration_statement (parser);
10633 return;
10635 case RID_TRANSACTION_ATOMIC:
10636 case RID_TRANSACTION_RELAXED:
10637 case RID_SYNCHRONIZED:
10638 case RID_ATOMIC_NOEXCEPT:
10639 case RID_ATOMIC_CANCEL:
10640 statement = cp_parser_transaction (parser, token);
10641 break;
10642 case RID_TRANSACTION_CANCEL:
10643 statement = cp_parser_transaction_cancel (parser);
10644 break;
10646 default:
10647 /* It might be a keyword like `int' that can start a
10648 declaration-statement. */
10649 break;
10652 else if (token->type == CPP_NAME)
10654 /* If the next token is a `:', then we are looking at a
10655 labeled-statement. */
10656 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10657 if (token->type == CPP_COLON)
10659 /* Looks like a labeled-statement with an ordinary label.
10660 Parse the label, and then use tail recursion to parse
10661 the statement. */
10663 cp_parser_label_for_labeled_statement (parser, std_attrs);
10664 in_compound = false;
10665 goto restart;
10668 /* Anything that starts with a `{' must be a compound-statement. */
10669 else if (token->type == CPP_OPEN_BRACE)
10670 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10671 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10672 a statement all its own. */
10673 else if (token->type == CPP_PRAGMA)
10675 /* Only certain OpenMP pragmas are attached to statements, and thus
10676 are considered statements themselves. All others are not. In
10677 the context of a compound, accept the pragma as a "statement" and
10678 return so that we can check for a close brace. Otherwise we
10679 require a real statement and must go back and read one. */
10680 if (in_compound)
10681 cp_parser_pragma (parser, pragma_compound, if_p);
10682 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10683 goto restart;
10684 return;
10686 else if (token->type == CPP_EOF)
10688 cp_parser_error (parser, "expected statement");
10689 return;
10692 /* Everything else must be a declaration-statement or an
10693 expression-statement. Try for the declaration-statement
10694 first, unless we are looking at a `;', in which case we know that
10695 we have an expression-statement. */
10696 if (!statement)
10698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10700 if (std_attrs != NULL_TREE)
10702 /* Attributes should be parsed as part of the the
10703 declaration, so let's un-parse them. */
10704 saved_tokens.rollback();
10705 std_attrs = NULL_TREE;
10708 cp_parser_parse_tentatively (parser);
10709 /* Try to parse the declaration-statement. */
10710 cp_parser_declaration_statement (parser);
10711 /* If that worked, we're done. */
10712 if (cp_parser_parse_definitely (parser))
10713 return;
10715 /* Look for an expression-statement instead. */
10716 statement = cp_parser_expression_statement (parser, in_statement_expr);
10718 /* Handle [[fallthrough]];. */
10719 if (attribute_fallthrough_p (std_attrs))
10721 /* The next token after the fallthrough attribute is ';'. */
10722 if (statement == NULL_TREE)
10724 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10725 statement = build_call_expr_internal_loc (statement_location,
10726 IFN_FALLTHROUGH,
10727 void_type_node, 0);
10728 finish_expr_stmt (statement);
10730 else
10731 warning_at (statement_location, OPT_Wattributes,
10732 "%<fallthrough%> attribute not followed by %<;%>");
10733 std_attrs = NULL_TREE;
10737 /* Set the line number for the statement. */
10738 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10739 SET_EXPR_LOCATION (statement, statement_location);
10741 /* Allow "[[fallthrough]];", but warn otherwise. */
10742 if (std_attrs != NULL_TREE)
10743 warning_at (attrs_location,
10744 OPT_Wattributes,
10745 "attributes at the beginning of statement are ignored");
10748 /* Parse the label for a labeled-statement, i.e.
10750 identifier :
10751 case constant-expression :
10752 default :
10754 GNU Extension:
10755 case constant-expression ... constant-expression : statement
10757 When a label is parsed without errors, the label is added to the
10758 parse tree by the finish_* functions, so this function doesn't
10759 have to return the label. */
10761 static void
10762 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10764 cp_token *token;
10765 tree label = NULL_TREE;
10766 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10768 /* The next token should be an identifier. */
10769 token = cp_lexer_peek_token (parser->lexer);
10770 if (token->type != CPP_NAME
10771 && token->type != CPP_KEYWORD)
10773 cp_parser_error (parser, "expected labeled-statement");
10774 return;
10777 /* Remember whether this case or a user-defined label is allowed to fall
10778 through to. */
10779 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10781 parser->colon_corrects_to_scope_p = false;
10782 switch (token->keyword)
10784 case RID_CASE:
10786 tree expr, expr_hi;
10787 cp_token *ellipsis;
10789 /* Consume the `case' token. */
10790 cp_lexer_consume_token (parser->lexer);
10791 /* Parse the constant-expression. */
10792 expr = cp_parser_constant_expression (parser);
10793 if (check_for_bare_parameter_packs (expr))
10794 expr = error_mark_node;
10796 ellipsis = cp_lexer_peek_token (parser->lexer);
10797 if (ellipsis->type == CPP_ELLIPSIS)
10799 /* Consume the `...' token. */
10800 cp_lexer_consume_token (parser->lexer);
10801 expr_hi = cp_parser_constant_expression (parser);
10802 if (check_for_bare_parameter_packs (expr_hi))
10803 expr_hi = error_mark_node;
10805 /* We don't need to emit warnings here, as the common code
10806 will do this for us. */
10808 else
10809 expr_hi = NULL_TREE;
10811 if (parser->in_switch_statement_p)
10813 tree l = finish_case_label (token->location, expr, expr_hi);
10814 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10815 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10817 else
10818 error_at (token->location,
10819 "case label %qE not within a switch statement",
10820 expr);
10822 break;
10824 case RID_DEFAULT:
10825 /* Consume the `default' token. */
10826 cp_lexer_consume_token (parser->lexer);
10828 if (parser->in_switch_statement_p)
10830 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10831 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10832 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10834 else
10835 error_at (token->location, "case label not within a switch statement");
10836 break;
10838 default:
10839 /* Anything else must be an ordinary label. */
10840 label = finish_label_stmt (cp_parser_identifier (parser));
10841 if (label && TREE_CODE (label) == LABEL_DECL)
10842 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
10843 break;
10846 /* Require the `:' token. */
10847 cp_parser_require (parser, CPP_COLON, RT_COLON);
10849 /* An ordinary label may optionally be followed by attributes.
10850 However, this is only permitted if the attributes are then
10851 followed by a semicolon. This is because, for backward
10852 compatibility, when parsing
10853 lab: __attribute__ ((unused)) int i;
10854 we want the attribute to attach to "i", not "lab". */
10855 if (label != NULL_TREE
10856 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10858 tree attrs;
10859 cp_parser_parse_tentatively (parser);
10860 attrs = cp_parser_gnu_attributes_opt (parser);
10861 if (attrs == NULL_TREE
10862 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10863 cp_parser_abort_tentative_parse (parser);
10864 else if (!cp_parser_parse_definitely (parser))
10866 else
10867 attributes = chainon (attributes, attrs);
10870 if (attributes != NULL_TREE)
10871 cplus_decl_attributes (&label, attributes, 0);
10873 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10876 /* Parse an expression-statement.
10878 expression-statement:
10879 expression [opt] ;
10881 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10882 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10883 indicates whether this expression-statement is part of an
10884 expression statement. */
10886 static tree
10887 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10889 tree statement = NULL_TREE;
10890 cp_token *token = cp_lexer_peek_token (parser->lexer);
10891 location_t loc = token->location;
10893 /* There might be attribute fallthrough. */
10894 tree attr = cp_parser_gnu_attributes_opt (parser);
10896 /* If the next token is a ';', then there is no expression
10897 statement. */
10898 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10900 statement = cp_parser_expression (parser);
10901 if (statement == error_mark_node
10902 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10904 cp_parser_skip_to_end_of_block_or_statement (parser);
10905 return error_mark_node;
10909 /* Handle [[fallthrough]];. */
10910 if (attribute_fallthrough_p (attr))
10912 /* The next token after the fallthrough attribute is ';'. */
10913 if (statement == NULL_TREE)
10914 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10915 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
10916 void_type_node, 0);
10917 else
10918 warning_at (loc, OPT_Wattributes,
10919 "%<fallthrough%> attribute not followed by %<;%>");
10920 attr = NULL_TREE;
10923 /* Allow "[[fallthrough]];", but warn otherwise. */
10924 if (attr != NULL_TREE)
10925 warning_at (loc, OPT_Wattributes,
10926 "attributes at the beginning of statement are ignored");
10928 /* Give a helpful message for "A<T>::type t;" and the like. */
10929 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10930 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10932 if (TREE_CODE (statement) == SCOPE_REF)
10933 error_at (token->location, "need %<typename%> before %qE because "
10934 "%qT is a dependent scope",
10935 statement, TREE_OPERAND (statement, 0));
10936 else if (is_overloaded_fn (statement)
10937 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10939 /* A::A a; */
10940 tree fn = get_first_fn (statement);
10941 error_at (token->location,
10942 "%<%T::%D%> names the constructor, not the type",
10943 DECL_CONTEXT (fn), DECL_NAME (fn));
10947 /* Consume the final `;'. */
10948 cp_parser_consume_semicolon_at_end_of_statement (parser);
10950 if (in_statement_expr
10951 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10952 /* This is the final expression statement of a statement
10953 expression. */
10954 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10955 else if (statement)
10956 statement = finish_expr_stmt (statement);
10958 return statement;
10961 /* Parse a compound-statement.
10963 compound-statement:
10964 { statement-seq [opt] }
10966 GNU extension:
10968 compound-statement:
10969 { label-declaration-seq [opt] statement-seq [opt] }
10971 label-declaration-seq:
10972 label-declaration
10973 label-declaration-seq label-declaration
10975 Returns a tree representing the statement. */
10977 static tree
10978 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10979 int bcs_flags, bool function_body)
10981 tree compound_stmt;
10983 /* Consume the `{'. */
10984 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10985 return error_mark_node;
10986 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10987 && !function_body && cxx_dialect < cxx14)
10988 pedwarn (input_location, OPT_Wpedantic,
10989 "compound-statement in constexpr function");
10990 /* Begin the compound-statement. */
10991 compound_stmt = begin_compound_stmt (bcs_flags);
10992 /* If the next keyword is `__label__' we have a label declaration. */
10993 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10994 cp_parser_label_declaration (parser);
10995 /* Parse an (optional) statement-seq. */
10996 cp_parser_statement_seq_opt (parser, in_statement_expr);
10997 /* Finish the compound-statement. */
10998 finish_compound_stmt (compound_stmt);
10999 /* Consume the `}'. */
11000 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11002 return compound_stmt;
11005 /* Parse an (optional) statement-seq.
11007 statement-seq:
11008 statement
11009 statement-seq [opt] statement */
11011 static void
11012 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11014 /* Scan statements until there aren't any more. */
11015 while (true)
11017 cp_token *token = cp_lexer_peek_token (parser->lexer);
11019 /* If we are looking at a `}', then we have run out of
11020 statements; the same is true if we have reached the end
11021 of file, or have stumbled upon a stray '@end'. */
11022 if (token->type == CPP_CLOSE_BRACE
11023 || token->type == CPP_EOF
11024 || token->type == CPP_PRAGMA_EOL
11025 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11026 break;
11028 /* If we are in a compound statement and find 'else' then
11029 something went wrong. */
11030 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11032 if (parser->in_statement & IN_IF_STMT)
11033 break;
11034 else
11036 token = cp_lexer_consume_token (parser->lexer);
11037 error_at (token->location, "%<else%> without a previous %<if%>");
11041 /* Parse the statement. */
11042 cp_parser_statement (parser, in_statement_expr, true, NULL);
11046 /* Return true if we're looking at (init; cond), false otherwise. */
11048 static bool
11049 cp_parser_init_statement_p (cp_parser *parser)
11051 /* Save tokens so that we can put them back. */
11052 cp_lexer_save_tokens (parser->lexer);
11054 /* Look for ';' that is not nested in () or {}. */
11055 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11056 /*recovering=*/false,
11057 CPP_SEMICOLON,
11058 /*consume_paren=*/false);
11060 /* Roll back the tokens we skipped. */
11061 cp_lexer_rollback_tokens (parser->lexer);
11063 return ret == -1;
11066 /* Parse a selection-statement.
11068 selection-statement:
11069 if ( init-statement [opt] condition ) statement
11070 if ( init-statement [opt] condition ) statement else statement
11071 switch ( init-statement [opt] condition ) statement
11073 Returns the new IF_STMT or SWITCH_STMT.
11075 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11076 is a (possibly labeled) if statement which is not enclosed in
11077 braces and has an else clause. This is used to implement
11078 -Wparentheses.
11080 CHAIN is a vector of if-else-if conditions. This is used to implement
11081 -Wduplicated-cond. */
11083 static tree
11084 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11085 vec<tree> *chain)
11087 cp_token *token;
11088 enum rid keyword;
11089 token_indent_info guard_tinfo;
11091 if (if_p != NULL)
11092 *if_p = false;
11094 /* Peek at the next token. */
11095 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11096 guard_tinfo = get_token_indent_info (token);
11098 /* See what kind of keyword it is. */
11099 keyword = token->keyword;
11100 switch (keyword)
11102 case RID_IF:
11103 case RID_SWITCH:
11105 tree statement;
11106 tree condition;
11108 bool cx = false;
11109 if (keyword == RID_IF
11110 && cp_lexer_next_token_is_keyword (parser->lexer,
11111 RID_CONSTEXPR))
11113 cx = true;
11114 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11115 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
11116 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11117 "with -std=c++1z or -std=gnu++1z");
11120 /* Look for the `('. */
11121 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11123 cp_parser_skip_to_end_of_statement (parser);
11124 return error_mark_node;
11127 /* Begin the selection-statement. */
11128 if (keyword == RID_IF)
11130 statement = begin_if_stmt ();
11131 IF_STMT_CONSTEXPR_P (statement) = cx;
11133 else
11134 statement = begin_switch_stmt ();
11136 /* Parse the optional init-statement. */
11137 if (cp_parser_init_statement_p (parser))
11139 tree decl;
11140 if (cxx_dialect < cxx1z)
11141 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11142 "init-statement in selection statements only available "
11143 "with -std=c++1z or -std=gnu++1z");
11144 cp_parser_init_statement (parser, &decl);
11147 /* Parse the condition. */
11148 condition = cp_parser_condition (parser);
11149 /* Look for the `)'. */
11150 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11151 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11152 /*consume_paren=*/true);
11154 if (keyword == RID_IF)
11156 bool nested_if;
11157 unsigned char in_statement;
11159 /* Add the condition. */
11160 condition = finish_if_stmt_cond (condition, statement);
11162 if (warn_duplicated_cond)
11163 warn_duplicated_cond_add_or_warn (token->location, condition,
11164 &chain);
11166 /* Parse the then-clause. */
11167 in_statement = parser->in_statement;
11168 parser->in_statement |= IN_IF_STMT;
11170 /* Outside a template, the non-selected branch of a constexpr
11171 if is a 'discarded statement', i.e. unevaluated. */
11172 bool was_discarded = in_discarded_stmt;
11173 bool discard_then = (cx && !processing_template_decl
11174 && integer_zerop (condition));
11175 if (discard_then)
11177 in_discarded_stmt = true;
11178 ++c_inhibit_evaluation_warnings;
11181 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11182 guard_tinfo);
11184 parser->in_statement = in_statement;
11186 finish_then_clause (statement);
11188 if (discard_then)
11190 THEN_CLAUSE (statement) = NULL_TREE;
11191 in_discarded_stmt = was_discarded;
11192 --c_inhibit_evaluation_warnings;
11195 /* If the next token is `else', parse the else-clause. */
11196 if (cp_lexer_next_token_is_keyword (parser->lexer,
11197 RID_ELSE))
11199 bool discard_else = (cx && !processing_template_decl
11200 && integer_nonzerop (condition));
11201 if (discard_else)
11203 in_discarded_stmt = true;
11204 ++c_inhibit_evaluation_warnings;
11207 guard_tinfo
11208 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11209 /* Consume the `else' keyword. */
11210 cp_lexer_consume_token (parser->lexer);
11211 if (warn_duplicated_cond)
11213 if (cp_lexer_next_token_is_keyword (parser->lexer,
11214 RID_IF)
11215 && chain == NULL)
11217 /* We've got "if (COND) else if (COND2)". Start
11218 the condition chain and add COND as the first
11219 element. */
11220 chain = new vec<tree> ();
11221 if (!CONSTANT_CLASS_P (condition)
11222 && !TREE_SIDE_EFFECTS (condition))
11224 /* Wrap it in a NOP_EXPR so that we can set the
11225 location of the condition. */
11226 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11227 condition);
11228 SET_EXPR_LOCATION (e, token->location);
11229 chain->safe_push (e);
11232 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11233 RID_IF))
11235 /* This is if-else without subsequent if. Zap the
11236 condition chain; we would have already warned at
11237 this point. */
11238 delete chain;
11239 chain = NULL;
11242 begin_else_clause (statement);
11243 /* Parse the else-clause. */
11244 cp_parser_implicitly_scoped_statement (parser, NULL,
11245 guard_tinfo, chain);
11247 finish_else_clause (statement);
11249 /* If we are currently parsing a then-clause, then
11250 IF_P will not be NULL. We set it to true to
11251 indicate that this if statement has an else clause.
11252 This may trigger the Wparentheses warning below
11253 when we get back up to the parent if statement. */
11254 if (if_p != NULL)
11255 *if_p = true;
11257 if (discard_else)
11259 ELSE_CLAUSE (statement) = NULL_TREE;
11260 in_discarded_stmt = was_discarded;
11261 --c_inhibit_evaluation_warnings;
11264 else
11266 /* This if statement does not have an else clause. If
11267 NESTED_IF is true, then the then-clause has an if
11268 statement which does have an else clause. We warn
11269 about the potential ambiguity. */
11270 if (nested_if)
11271 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11272 "suggest explicit braces to avoid ambiguous"
11273 " %<else%>");
11274 if (warn_duplicated_cond)
11276 /* We don't need the condition chain anymore. */
11277 delete chain;
11278 chain = NULL;
11282 /* Now we're all done with the if-statement. */
11283 finish_if_stmt (statement);
11285 else
11287 bool in_switch_statement_p;
11288 unsigned char in_statement;
11290 /* Add the condition. */
11291 finish_switch_cond (condition, statement);
11293 /* Parse the body of the switch-statement. */
11294 in_switch_statement_p = parser->in_switch_statement_p;
11295 in_statement = parser->in_statement;
11296 parser->in_switch_statement_p = true;
11297 parser->in_statement |= IN_SWITCH_STMT;
11298 cp_parser_implicitly_scoped_statement (parser, if_p,
11299 guard_tinfo);
11300 parser->in_switch_statement_p = in_switch_statement_p;
11301 parser->in_statement = in_statement;
11303 /* Now we're all done with the switch-statement. */
11304 finish_switch_stmt (statement);
11307 return statement;
11309 break;
11311 default:
11312 cp_parser_error (parser, "expected selection-statement");
11313 return error_mark_node;
11317 /* Parse a condition.
11319 condition:
11320 expression
11321 type-specifier-seq declarator = initializer-clause
11322 type-specifier-seq declarator braced-init-list
11324 GNU Extension:
11326 condition:
11327 type-specifier-seq declarator asm-specification [opt]
11328 attributes [opt] = assignment-expression
11330 Returns the expression that should be tested. */
11332 static tree
11333 cp_parser_condition (cp_parser* parser)
11335 cp_decl_specifier_seq type_specifiers;
11336 const char *saved_message;
11337 int declares_class_or_enum;
11339 /* Try the declaration first. */
11340 cp_parser_parse_tentatively (parser);
11341 /* New types are not allowed in the type-specifier-seq for a
11342 condition. */
11343 saved_message = parser->type_definition_forbidden_message;
11344 parser->type_definition_forbidden_message
11345 = G_("types may not be defined in conditions");
11346 /* Parse the type-specifier-seq. */
11347 cp_parser_decl_specifier_seq (parser,
11348 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11349 &type_specifiers,
11350 &declares_class_or_enum);
11351 /* Restore the saved message. */
11352 parser->type_definition_forbidden_message = saved_message;
11353 /* If all is well, we might be looking at a declaration. */
11354 if (!cp_parser_error_occurred (parser))
11356 tree decl;
11357 tree asm_specification;
11358 tree attributes;
11359 cp_declarator *declarator;
11360 tree initializer = NULL_TREE;
11362 /* Parse the declarator. */
11363 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11364 /*ctor_dtor_or_conv_p=*/NULL,
11365 /*parenthesized_p=*/NULL,
11366 /*member_p=*/false,
11367 /*friend_p=*/false);
11368 /* Parse the attributes. */
11369 attributes = cp_parser_attributes_opt (parser);
11370 /* Parse the asm-specification. */
11371 asm_specification = cp_parser_asm_specification_opt (parser);
11372 /* If the next token is not an `=' or '{', then we might still be
11373 looking at an expression. For example:
11375 if (A(a).x)
11377 looks like a decl-specifier-seq and a declarator -- but then
11378 there is no `=', so this is an expression. */
11379 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11380 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11381 cp_parser_simulate_error (parser);
11383 /* If we did see an `=' or '{', then we are looking at a declaration
11384 for sure. */
11385 if (cp_parser_parse_definitely (parser))
11387 tree pushed_scope;
11388 bool non_constant_p;
11389 int flags = LOOKUP_ONLYCONVERTING;
11391 /* Create the declaration. */
11392 decl = start_decl (declarator, &type_specifiers,
11393 /*initialized_p=*/true,
11394 attributes, /*prefix_attributes=*/NULL_TREE,
11395 &pushed_scope);
11397 /* Parse the initializer. */
11398 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11400 initializer = cp_parser_braced_list (parser, &non_constant_p);
11401 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11402 flags = 0;
11404 else
11406 /* Consume the `='. */
11407 cp_parser_require (parser, CPP_EQ, RT_EQ);
11408 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11410 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11411 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11413 /* Process the initializer. */
11414 cp_finish_decl (decl,
11415 initializer, !non_constant_p,
11416 asm_specification,
11417 flags);
11419 if (pushed_scope)
11420 pop_scope (pushed_scope);
11422 return convert_from_reference (decl);
11425 /* If we didn't even get past the declarator successfully, we are
11426 definitely not looking at a declaration. */
11427 else
11428 cp_parser_abort_tentative_parse (parser);
11430 /* Otherwise, we are looking at an expression. */
11431 return cp_parser_expression (parser);
11434 /* Parses a for-statement or range-for-statement until the closing ')',
11435 not included. */
11437 static tree
11438 cp_parser_for (cp_parser *parser, bool ivdep)
11440 tree init, scope, decl;
11441 bool is_range_for;
11443 /* Begin the for-statement. */
11444 scope = begin_for_scope (&init);
11446 /* Parse the initialization. */
11447 is_range_for = cp_parser_init_statement (parser, &decl);
11449 if (is_range_for)
11450 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11451 else
11452 return cp_parser_c_for (parser, scope, init, ivdep);
11455 static tree
11456 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11458 /* Normal for loop */
11459 tree condition = NULL_TREE;
11460 tree expression = NULL_TREE;
11461 tree stmt;
11463 stmt = begin_for_stmt (scope, init);
11464 /* The init-statement has already been parsed in
11465 cp_parser_init_statement, so no work is needed here. */
11466 finish_init_stmt (stmt);
11468 /* If there's a condition, process it. */
11469 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11470 condition = cp_parser_condition (parser);
11471 else if (ivdep)
11473 cp_parser_error (parser, "missing loop condition in loop with "
11474 "%<GCC ivdep%> pragma");
11475 condition = error_mark_node;
11477 finish_for_cond (condition, stmt, ivdep);
11478 /* Look for the `;'. */
11479 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11481 /* If there's an expression, process it. */
11482 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11483 expression = cp_parser_expression (parser);
11484 finish_for_expr (expression, stmt);
11486 return stmt;
11489 /* Tries to parse a range-based for-statement:
11491 range-based-for:
11492 decl-specifier-seq declarator : expression
11494 The decl-specifier-seq declarator and the `:' are already parsed by
11495 cp_parser_init_statement. If processing_template_decl it returns a
11496 newly created RANGE_FOR_STMT; if not, it is converted to a
11497 regular FOR_STMT. */
11499 static tree
11500 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11501 bool ivdep)
11503 tree stmt, range_expr;
11504 auto_vec <cxx_binding *, 16> bindings;
11505 auto_vec <tree, 16> names;
11506 tree decomp_first_name = NULL_TREE;
11507 unsigned int decomp_cnt = 0;
11509 /* Get the range declaration momentarily out of the way so that
11510 the range expression doesn't clash with it. */
11511 if (range_decl != error_mark_node)
11513 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11515 tree v = DECL_VALUE_EXPR (range_decl);
11516 /* For decomposition declaration get all of the corresponding
11517 declarations out of the way. */
11518 if (TREE_CODE (v) == ARRAY_REF
11519 && VAR_P (TREE_OPERAND (v, 0))
11520 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11522 tree d = range_decl;
11523 range_decl = TREE_OPERAND (v, 0);
11524 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11525 decomp_first_name = d;
11526 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11528 tree name = DECL_NAME (d);
11529 names.safe_push (name);
11530 bindings.safe_push (IDENTIFIER_BINDING (name));
11531 IDENTIFIER_BINDING (name)
11532 = IDENTIFIER_BINDING (name)->previous;
11536 if (names.is_empty ())
11538 tree name = DECL_NAME (range_decl);
11539 names.safe_push (name);
11540 bindings.safe_push (IDENTIFIER_BINDING (name));
11541 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11545 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11547 bool expr_non_constant_p;
11548 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11550 else
11551 range_expr = cp_parser_expression (parser);
11553 /* Put the range declaration(s) back into scope. */
11554 for (unsigned int i = 0; i < names.length (); i++)
11556 cxx_binding *binding = bindings[i];
11557 binding->previous = IDENTIFIER_BINDING (names[i]);
11558 IDENTIFIER_BINDING (names[i]) = binding;
11561 /* If in template, STMT is converted to a normal for-statement
11562 at instantiation. If not, it is done just ahead. */
11563 if (processing_template_decl)
11565 if (check_for_bare_parameter_packs (range_expr))
11566 range_expr = error_mark_node;
11567 stmt = begin_range_for_stmt (scope, init);
11568 if (ivdep)
11569 RANGE_FOR_IVDEP (stmt) = 1;
11570 finish_range_for_decl (stmt, range_decl, range_expr);
11571 if (!type_dependent_expression_p (range_expr)
11572 /* do_auto_deduction doesn't mess with template init-lists. */
11573 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11574 do_range_for_auto_deduction (range_decl, range_expr);
11576 else
11578 stmt = begin_for_stmt (scope, init);
11579 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11580 decomp_first_name, decomp_cnt, ivdep);
11582 return stmt;
11585 /* Subroutine of cp_convert_range_for: given the initializer expression,
11586 builds up the range temporary. */
11588 static tree
11589 build_range_temp (tree range_expr)
11591 tree range_type, range_temp;
11593 /* Find out the type deduced by the declaration
11594 `auto &&__range = range_expr'. */
11595 range_type = cp_build_reference_type (make_auto (), true);
11596 range_type = do_auto_deduction (range_type, range_expr,
11597 type_uses_auto (range_type));
11599 /* Create the __range variable. */
11600 range_temp = build_decl (input_location, VAR_DECL,
11601 get_identifier ("__for_range"), range_type);
11602 TREE_USED (range_temp) = 1;
11603 DECL_ARTIFICIAL (range_temp) = 1;
11605 return range_temp;
11608 /* Used by cp_parser_range_for in template context: we aren't going to
11609 do a full conversion yet, but we still need to resolve auto in the
11610 type of the for-range-declaration if present. This is basically
11611 a shortcut version of cp_convert_range_for. */
11613 static void
11614 do_range_for_auto_deduction (tree decl, tree range_expr)
11616 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11617 if (auto_node)
11619 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11620 range_temp = convert_from_reference (build_range_temp (range_expr));
11621 iter_type = (cp_parser_perform_range_for_lookup
11622 (range_temp, &begin_dummy, &end_dummy));
11623 if (iter_type)
11625 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11626 iter_type);
11627 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11628 tf_warning_or_error);
11629 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11630 iter_decl, auto_node);
11635 /* Converts a range-based for-statement into a normal
11636 for-statement, as per the definition.
11638 for (RANGE_DECL : RANGE_EXPR)
11639 BLOCK
11641 should be equivalent to:
11644 auto &&__range = RANGE_EXPR;
11645 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11646 __begin != __end;
11647 ++__begin)
11649 RANGE_DECL = *__begin;
11650 BLOCK
11654 If RANGE_EXPR is an array:
11655 BEGIN_EXPR = __range
11656 END_EXPR = __range + ARRAY_SIZE(__range)
11657 Else if RANGE_EXPR has a member 'begin' or 'end':
11658 BEGIN_EXPR = __range.begin()
11659 END_EXPR = __range.end()
11660 Else:
11661 BEGIN_EXPR = begin(__range)
11662 END_EXPR = end(__range);
11664 If __range has a member 'begin' but not 'end', or vice versa, we must
11665 still use the second alternative (it will surely fail, however).
11666 When calling begin()/end() in the third alternative we must use
11667 argument dependent lookup, but always considering 'std' as an associated
11668 namespace. */
11670 tree
11671 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11672 tree decomp_first_name, unsigned int decomp_cnt,
11673 bool ivdep)
11675 tree begin, end;
11676 tree iter_type, begin_expr, end_expr;
11677 tree condition, expression;
11679 if (range_decl == error_mark_node || range_expr == error_mark_node)
11680 /* If an error happened previously do nothing or else a lot of
11681 unhelpful errors would be issued. */
11682 begin_expr = end_expr = iter_type = error_mark_node;
11683 else
11685 tree range_temp;
11687 if (VAR_P (range_expr)
11688 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11689 /* Can't bind a reference to an array of runtime bound. */
11690 range_temp = range_expr;
11691 else
11693 range_temp = build_range_temp (range_expr);
11694 pushdecl (range_temp);
11695 cp_finish_decl (range_temp, range_expr,
11696 /*is_constant_init*/false, NULL_TREE,
11697 LOOKUP_ONLYCONVERTING);
11698 range_temp = convert_from_reference (range_temp);
11700 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11701 &begin_expr, &end_expr);
11704 /* The new for initialization statement. */
11705 begin = build_decl (input_location, VAR_DECL,
11706 get_identifier ("__for_begin"), iter_type);
11707 TREE_USED (begin) = 1;
11708 DECL_ARTIFICIAL (begin) = 1;
11709 pushdecl (begin);
11710 cp_finish_decl (begin, begin_expr,
11711 /*is_constant_init*/false, NULL_TREE,
11712 LOOKUP_ONLYCONVERTING);
11714 if (cxx_dialect >= cxx1z)
11715 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11716 end = build_decl (input_location, VAR_DECL,
11717 get_identifier ("__for_end"), iter_type);
11718 TREE_USED (end) = 1;
11719 DECL_ARTIFICIAL (end) = 1;
11720 pushdecl (end);
11721 cp_finish_decl (end, end_expr,
11722 /*is_constant_init*/false, NULL_TREE,
11723 LOOKUP_ONLYCONVERTING);
11725 finish_init_stmt (statement);
11727 /* The new for condition. */
11728 condition = build_x_binary_op (input_location, NE_EXPR,
11729 begin, ERROR_MARK,
11730 end, ERROR_MARK,
11731 NULL, tf_warning_or_error);
11732 finish_for_cond (condition, statement, ivdep);
11734 /* The new increment expression. */
11735 expression = finish_unary_op_expr (input_location,
11736 PREINCREMENT_EXPR, begin,
11737 tf_warning_or_error);
11738 finish_for_expr (expression, statement);
11740 /* The declaration is initialized with *__begin inside the loop body. */
11741 cp_finish_decl (range_decl,
11742 build_x_indirect_ref (input_location, begin, RO_NULL,
11743 tf_warning_or_error),
11744 /*is_constant_init*/false, NULL_TREE,
11745 LOOKUP_ONLYCONVERTING);
11746 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11747 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11749 return statement;
11752 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11753 We need to solve both at the same time because the method used
11754 depends on the existence of members begin or end.
11755 Returns the type deduced for the iterator expression. */
11757 static tree
11758 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11760 if (error_operand_p (range))
11762 *begin = *end = error_mark_node;
11763 return error_mark_node;
11766 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11768 error ("range-based %<for%> expression of type %qT "
11769 "has incomplete type", TREE_TYPE (range));
11770 *begin = *end = error_mark_node;
11771 return error_mark_node;
11773 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11775 /* If RANGE is an array, we will use pointer arithmetic. */
11776 *begin = decay_conversion (range, tf_warning_or_error);
11777 *end = build_binary_op (input_location, PLUS_EXPR,
11778 range,
11779 array_type_nelts_top (TREE_TYPE (range)),
11781 return TREE_TYPE (*begin);
11783 else
11785 /* If it is not an array, we must do a bit of magic. */
11786 tree id_begin, id_end;
11787 tree member_begin, member_end;
11789 *begin = *end = error_mark_node;
11791 id_begin = get_identifier ("begin");
11792 id_end = get_identifier ("end");
11793 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11794 /*protect=*/2, /*want_type=*/false,
11795 tf_warning_or_error);
11796 member_end = lookup_member (TREE_TYPE (range), id_end,
11797 /*protect=*/2, /*want_type=*/false,
11798 tf_warning_or_error);
11800 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11802 /* Use the member functions. */
11803 if (member_begin != NULL_TREE)
11804 *begin = cp_parser_range_for_member_function (range, id_begin);
11805 else
11806 error ("range-based %<for%> expression of type %qT has an "
11807 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11809 if (member_end != NULL_TREE)
11810 *end = cp_parser_range_for_member_function (range, id_end);
11811 else
11812 error ("range-based %<for%> expression of type %qT has a "
11813 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11815 else
11817 /* Use global functions with ADL. */
11818 vec<tree, va_gc> *vec;
11819 vec = make_tree_vector ();
11821 vec_safe_push (vec, range);
11823 member_begin = perform_koenig_lookup (id_begin, vec,
11824 tf_warning_or_error);
11825 *begin = finish_call_expr (member_begin, &vec, false, true,
11826 tf_warning_or_error);
11827 member_end = perform_koenig_lookup (id_end, vec,
11828 tf_warning_or_error);
11829 *end = finish_call_expr (member_end, &vec, false, true,
11830 tf_warning_or_error);
11832 release_tree_vector (vec);
11835 /* Last common checks. */
11836 if (*begin == error_mark_node || *end == error_mark_node)
11838 /* If one of the expressions is an error do no more checks. */
11839 *begin = *end = error_mark_node;
11840 return error_mark_node;
11842 else if (type_dependent_expression_p (*begin)
11843 || type_dependent_expression_p (*end))
11844 /* Can happen, when, eg, in a template context, Koenig lookup
11845 can't resolve begin/end (c++/58503). */
11846 return NULL_TREE;
11847 else
11849 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11850 /* The unqualified type of the __begin and __end temporaries should
11851 be the same, as required by the multiple auto declaration. */
11852 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11854 if (cxx_dialect >= cxx1z
11855 && (build_x_binary_op (input_location, NE_EXPR,
11856 *begin, ERROR_MARK,
11857 *end, ERROR_MARK,
11858 NULL, tf_none)
11859 != error_mark_node))
11860 /* P0184R0 allows __begin and __end to have different types,
11861 but make sure they are comparable so we can give a better
11862 diagnostic. */;
11863 else
11864 error ("inconsistent begin/end types in range-based %<for%> "
11865 "statement: %qT and %qT",
11866 TREE_TYPE (*begin), TREE_TYPE (*end));
11868 return iter_type;
11873 /* Helper function for cp_parser_perform_range_for_lookup.
11874 Builds a tree for RANGE.IDENTIFIER(). */
11876 static tree
11877 cp_parser_range_for_member_function (tree range, tree identifier)
11879 tree member, res;
11880 vec<tree, va_gc> *vec;
11882 member = finish_class_member_access_expr (range, identifier,
11883 false, tf_warning_or_error);
11884 if (member == error_mark_node)
11885 return error_mark_node;
11887 vec = make_tree_vector ();
11888 res = finish_call_expr (member, &vec,
11889 /*disallow_virtual=*/false,
11890 /*koenig_p=*/false,
11891 tf_warning_or_error);
11892 release_tree_vector (vec);
11893 return res;
11896 /* Parse an iteration-statement.
11898 iteration-statement:
11899 while ( condition ) statement
11900 do statement while ( expression ) ;
11901 for ( init-statement condition [opt] ; expression [opt] )
11902 statement
11904 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11906 static tree
11907 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11909 cp_token *token;
11910 enum rid keyword;
11911 tree statement;
11912 unsigned char in_statement;
11913 token_indent_info guard_tinfo;
11915 /* Peek at the next token. */
11916 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11917 if (!token)
11918 return error_mark_node;
11920 guard_tinfo = get_token_indent_info (token);
11922 /* Remember whether or not we are already within an iteration
11923 statement. */
11924 in_statement = parser->in_statement;
11926 /* See what kind of keyword it is. */
11927 keyword = token->keyword;
11928 switch (keyword)
11930 case RID_WHILE:
11932 tree condition;
11934 /* Begin the while-statement. */
11935 statement = begin_while_stmt ();
11936 /* Look for the `('. */
11937 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11938 /* Parse the condition. */
11939 condition = cp_parser_condition (parser);
11940 finish_while_stmt_cond (condition, statement, ivdep);
11941 /* Look for the `)'. */
11942 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11943 /* Parse the dependent statement. */
11944 parser->in_statement = IN_ITERATION_STMT;
11945 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11946 parser->in_statement = in_statement;
11947 /* We're done with the while-statement. */
11948 finish_while_stmt (statement);
11950 break;
11952 case RID_DO:
11954 tree expression;
11956 /* Begin the do-statement. */
11957 statement = begin_do_stmt ();
11958 /* Parse the body of the do-statement. */
11959 parser->in_statement = IN_ITERATION_STMT;
11960 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11961 parser->in_statement = in_statement;
11962 finish_do_body (statement);
11963 /* Look for the `while' keyword. */
11964 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11965 /* Look for the `('. */
11966 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11967 /* Parse the expression. */
11968 expression = cp_parser_expression (parser);
11969 /* We're done with the do-statement. */
11970 finish_do_stmt (expression, statement, ivdep);
11971 /* Look for the `)'. */
11972 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11973 /* Look for the `;'. */
11974 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11976 break;
11978 case RID_FOR:
11980 /* Look for the `('. */
11981 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11983 statement = cp_parser_for (parser, ivdep);
11985 /* Look for the `)'. */
11986 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11988 /* Parse the body of the for-statement. */
11989 parser->in_statement = IN_ITERATION_STMT;
11990 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11991 parser->in_statement = in_statement;
11993 /* We're done with the for-statement. */
11994 finish_for_stmt (statement);
11996 break;
11998 default:
11999 cp_parser_error (parser, "expected iteration-statement");
12000 statement = error_mark_node;
12001 break;
12004 return statement;
12007 /* Parse a init-statement or the declarator of a range-based-for.
12008 Returns true if a range-based-for declaration is seen.
12010 init-statement:
12011 expression-statement
12012 simple-declaration */
12014 static bool
12015 cp_parser_init_statement (cp_parser* parser, tree *decl)
12017 /* If the next token is a `;', then we have an empty
12018 expression-statement. Grammatically, this is also a
12019 simple-declaration, but an invalid one, because it does not
12020 declare anything. Therefore, if we did not handle this case
12021 specially, we would issue an error message about an invalid
12022 declaration. */
12023 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12025 bool is_range_for = false;
12026 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12028 /* A colon is used in range-based for. */
12029 parser->colon_corrects_to_scope_p = false;
12031 /* We're going to speculatively look for a declaration, falling back
12032 to an expression, if necessary. */
12033 cp_parser_parse_tentatively (parser);
12034 /* Parse the declaration. */
12035 cp_parser_simple_declaration (parser,
12036 /*function_definition_allowed_p=*/false,
12037 decl);
12038 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12039 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12041 /* It is a range-for, consume the ':' */
12042 cp_lexer_consume_token (parser->lexer);
12043 is_range_for = true;
12044 if (cxx_dialect < cxx11)
12046 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12047 "range-based %<for%> loops only available with "
12048 "-std=c++11 or -std=gnu++11");
12049 *decl = error_mark_node;
12052 else
12053 /* The ';' is not consumed yet because we told
12054 cp_parser_simple_declaration not to. */
12055 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12057 if (cp_parser_parse_definitely (parser))
12058 return is_range_for;
12059 /* If the tentative parse failed, then we shall need to look for an
12060 expression-statement. */
12062 /* If we are here, it is an expression-statement. */
12063 cp_parser_expression_statement (parser, NULL_TREE);
12064 return false;
12067 /* Parse a jump-statement.
12069 jump-statement:
12070 break ;
12071 continue ;
12072 return expression [opt] ;
12073 return braced-init-list ;
12074 goto identifier ;
12076 GNU extension:
12078 jump-statement:
12079 goto * expression ;
12081 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12083 static tree
12084 cp_parser_jump_statement (cp_parser* parser)
12086 tree statement = error_mark_node;
12087 cp_token *token;
12088 enum rid keyword;
12089 unsigned char in_statement;
12091 /* Peek at the next token. */
12092 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12093 if (!token)
12094 return error_mark_node;
12096 /* See what kind of keyword it is. */
12097 keyword = token->keyword;
12098 switch (keyword)
12100 case RID_BREAK:
12101 in_statement = parser->in_statement & ~IN_IF_STMT;
12102 switch (in_statement)
12104 case 0:
12105 error_at (token->location, "break statement not within loop or switch");
12106 break;
12107 default:
12108 gcc_assert ((in_statement & IN_SWITCH_STMT)
12109 || in_statement == IN_ITERATION_STMT);
12110 statement = finish_break_stmt ();
12111 if (in_statement == IN_ITERATION_STMT)
12112 break_maybe_infinite_loop ();
12113 break;
12114 case IN_OMP_BLOCK:
12115 error_at (token->location, "invalid exit from OpenMP structured block");
12116 break;
12117 case IN_OMP_FOR:
12118 error_at (token->location, "break statement used with OpenMP for loop");
12119 break;
12120 case IN_CILK_SIMD_FOR:
12121 error_at (token->location, "break statement used with Cilk Plus for loop");
12122 break;
12124 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12125 break;
12127 case RID_CONTINUE:
12128 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12130 case 0:
12131 error_at (token->location, "continue statement not within a loop");
12132 break;
12133 case IN_CILK_SIMD_FOR:
12134 error_at (token->location,
12135 "continue statement within %<#pragma simd%> loop body");
12136 /* Fall through. */
12137 case IN_ITERATION_STMT:
12138 case IN_OMP_FOR:
12139 statement = finish_continue_stmt ();
12140 break;
12141 case IN_OMP_BLOCK:
12142 error_at (token->location, "invalid exit from OpenMP structured block");
12143 break;
12144 default:
12145 gcc_unreachable ();
12147 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12148 break;
12150 case RID_RETURN:
12152 tree expr;
12153 bool expr_non_constant_p;
12155 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12157 cp_lexer_set_source_position (parser->lexer);
12158 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12159 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12161 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12162 expr = cp_parser_expression (parser);
12163 else
12164 /* If the next token is a `;', then there is no
12165 expression. */
12166 expr = NULL_TREE;
12167 /* Build the return-statement. */
12168 if (current_function_auto_return_pattern && in_discarded_stmt)
12169 /* Don't deduce from a discarded return statement. */;
12170 else
12171 statement = finish_return_stmt (expr);
12172 /* Look for the final `;'. */
12173 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12175 break;
12177 case RID_GOTO:
12178 if (parser->in_function_body
12179 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12181 error ("%<goto%> in %<constexpr%> function");
12182 cp_function_chain->invalid_constexpr = true;
12185 /* Create the goto-statement. */
12186 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12188 /* Issue a warning about this use of a GNU extension. */
12189 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12190 /* Consume the '*' token. */
12191 cp_lexer_consume_token (parser->lexer);
12192 /* Parse the dependent expression. */
12193 finish_goto_stmt (cp_parser_expression (parser));
12195 else
12196 finish_goto_stmt (cp_parser_identifier (parser));
12197 /* Look for the final `;'. */
12198 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12199 break;
12201 default:
12202 cp_parser_error (parser, "expected jump-statement");
12203 break;
12206 return statement;
12209 /* Parse a declaration-statement.
12211 declaration-statement:
12212 block-declaration */
12214 static void
12215 cp_parser_declaration_statement (cp_parser* parser)
12217 void *p;
12219 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12220 p = obstack_alloc (&declarator_obstack, 0);
12222 /* Parse the block-declaration. */
12223 cp_parser_block_declaration (parser, /*statement_p=*/true);
12225 /* Free any declarators allocated. */
12226 obstack_free (&declarator_obstack, p);
12229 /* Some dependent statements (like `if (cond) statement'), are
12230 implicitly in their own scope. In other words, if the statement is
12231 a single statement (as opposed to a compound-statement), it is
12232 none-the-less treated as if it were enclosed in braces. Any
12233 declarations appearing in the dependent statement are out of scope
12234 after control passes that point. This function parses a statement,
12235 but ensures that is in its own scope, even if it is not a
12236 compound-statement.
12238 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12239 is a (possibly labeled) if statement which is not enclosed in
12240 braces and has an else clause. This is used to implement
12241 -Wparentheses.
12243 CHAIN is a vector of if-else-if conditions. This is used to implement
12244 -Wduplicated-cond.
12246 Returns the new statement. */
12248 static tree
12249 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12250 const token_indent_info &guard_tinfo,
12251 vec<tree> *chain)
12253 tree statement;
12254 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12255 token_indent_info body_tinfo
12256 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12258 if (if_p != NULL)
12259 *if_p = false;
12261 /* Mark if () ; with a special NOP_EXPR. */
12262 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12264 cp_lexer_consume_token (parser->lexer);
12265 statement = add_stmt (build_empty_stmt (body_loc));
12267 if (guard_tinfo.keyword == RID_IF
12268 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12269 warning_at (body_loc, OPT_Wempty_body,
12270 "suggest braces around empty body in an %<if%> statement");
12271 else if (guard_tinfo.keyword == RID_ELSE)
12272 warning_at (body_loc, OPT_Wempty_body,
12273 "suggest braces around empty body in an %<else%> statement");
12275 /* if a compound is opened, we simply parse the statement directly. */
12276 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12277 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12278 /* If the token is not a `{', then we must take special action. */
12279 else
12281 /* Create a compound-statement. */
12282 statement = begin_compound_stmt (0);
12283 /* Parse the dependent-statement. */
12284 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
12285 /* Finish the dummy compound-statement. */
12286 finish_compound_stmt (statement);
12289 token_indent_info next_tinfo
12290 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12291 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12293 /* Return the statement. */
12294 return statement;
12297 /* For some dependent statements (like `while (cond) statement'), we
12298 have already created a scope. Therefore, even if the dependent
12299 statement is a compound-statement, we do not want to create another
12300 scope. */
12302 static void
12303 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12304 const token_indent_info &guard_tinfo)
12306 /* If the token is a `{', then we must take special action. */
12307 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12309 token_indent_info body_tinfo
12310 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12312 cp_parser_statement (parser, NULL_TREE, false, if_p);
12313 token_indent_info next_tinfo
12314 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12315 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12317 else
12319 /* Avoid calling cp_parser_compound_statement, so that we
12320 don't create a new scope. Do everything else by hand. */
12321 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12322 /* If the next keyword is `__label__' we have a label declaration. */
12323 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12324 cp_parser_label_declaration (parser);
12325 /* Parse an (optional) statement-seq. */
12326 cp_parser_statement_seq_opt (parser, NULL_TREE);
12327 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12331 /* Declarations [gram.dcl.dcl] */
12333 /* Parse an optional declaration-sequence.
12335 declaration-seq:
12336 declaration
12337 declaration-seq declaration */
12339 static void
12340 cp_parser_declaration_seq_opt (cp_parser* parser)
12342 while (true)
12344 cp_token *token;
12346 token = cp_lexer_peek_token (parser->lexer);
12348 if (token->type == CPP_CLOSE_BRACE
12349 || token->type == CPP_EOF
12350 || token->type == CPP_PRAGMA_EOL)
12351 break;
12353 if (token->type == CPP_SEMICOLON)
12355 /* A declaration consisting of a single semicolon is
12356 invalid. Allow it unless we're being pedantic. */
12357 cp_lexer_consume_token (parser->lexer);
12358 if (!in_system_header_at (input_location))
12359 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12360 continue;
12363 /* If we're entering or exiting a region that's implicitly
12364 extern "C", modify the lang context appropriately. */
12365 if (!parser->implicit_extern_c && token->implicit_extern_c)
12367 push_lang_context (lang_name_c);
12368 parser->implicit_extern_c = true;
12370 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12372 pop_lang_context ();
12373 parser->implicit_extern_c = false;
12376 if (token->type == CPP_PRAGMA)
12378 /* A top-level declaration can consist solely of a #pragma.
12379 A nested declaration cannot, so this is done here and not
12380 in cp_parser_declaration. (A #pragma at block scope is
12381 handled in cp_parser_statement.) */
12382 cp_parser_pragma (parser, pragma_external, NULL);
12383 continue;
12386 /* Parse the declaration itself. */
12387 cp_parser_declaration (parser);
12391 /* Parse a declaration.
12393 declaration:
12394 block-declaration
12395 function-definition
12396 template-declaration
12397 explicit-instantiation
12398 explicit-specialization
12399 linkage-specification
12400 namespace-definition
12402 C++17:
12403 deduction-guide
12405 GNU extension:
12407 declaration:
12408 __extension__ declaration */
12410 static void
12411 cp_parser_declaration (cp_parser* parser)
12413 cp_token token1;
12414 cp_token token2;
12415 int saved_pedantic;
12416 void *p;
12417 tree attributes = NULL_TREE;
12419 /* Check for the `__extension__' keyword. */
12420 if (cp_parser_extension_opt (parser, &saved_pedantic))
12422 /* Parse the qualified declaration. */
12423 cp_parser_declaration (parser);
12424 /* Restore the PEDANTIC flag. */
12425 pedantic = saved_pedantic;
12427 return;
12430 /* Try to figure out what kind of declaration is present. */
12431 token1 = *cp_lexer_peek_token (parser->lexer);
12433 if (token1.type != CPP_EOF)
12434 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12435 else
12437 token2.type = CPP_EOF;
12438 token2.keyword = RID_MAX;
12441 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12442 p = obstack_alloc (&declarator_obstack, 0);
12444 /* If the next token is `extern' and the following token is a string
12445 literal, then we have a linkage specification. */
12446 if (token1.keyword == RID_EXTERN
12447 && cp_parser_is_pure_string_literal (&token2))
12448 cp_parser_linkage_specification (parser);
12449 /* If the next token is `template', then we have either a template
12450 declaration, an explicit instantiation, or an explicit
12451 specialization. */
12452 else if (token1.keyword == RID_TEMPLATE)
12454 /* `template <>' indicates a template specialization. */
12455 if (token2.type == CPP_LESS
12456 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12457 cp_parser_explicit_specialization (parser);
12458 /* `template <' indicates a template declaration. */
12459 else if (token2.type == CPP_LESS)
12460 cp_parser_template_declaration (parser, /*member_p=*/false);
12461 /* Anything else must be an explicit instantiation. */
12462 else
12463 cp_parser_explicit_instantiation (parser);
12465 /* If the next token is `export', then we have a template
12466 declaration. */
12467 else if (token1.keyword == RID_EXPORT)
12468 cp_parser_template_declaration (parser, /*member_p=*/false);
12469 /* If the next token is `extern', 'static' or 'inline' and the one
12470 after that is `template', we have a GNU extended explicit
12471 instantiation directive. */
12472 else if (cp_parser_allow_gnu_extensions_p (parser)
12473 && (token1.keyword == RID_EXTERN
12474 || token1.keyword == RID_STATIC
12475 || token1.keyword == RID_INLINE)
12476 && token2.keyword == RID_TEMPLATE)
12477 cp_parser_explicit_instantiation (parser);
12478 /* If the next token is `namespace', check for a named or unnamed
12479 namespace definition. */
12480 else if (token1.keyword == RID_NAMESPACE
12481 && (/* A named namespace definition. */
12482 (token2.type == CPP_NAME
12483 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12484 != CPP_EQ))
12485 || (token2.type == CPP_OPEN_SQUARE
12486 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12487 == CPP_OPEN_SQUARE)
12488 /* An unnamed namespace definition. */
12489 || token2.type == CPP_OPEN_BRACE
12490 || token2.keyword == RID_ATTRIBUTE))
12491 cp_parser_namespace_definition (parser);
12492 /* An inline (associated) namespace definition. */
12493 else if (token1.keyword == RID_INLINE
12494 && token2.keyword == RID_NAMESPACE)
12495 cp_parser_namespace_definition (parser);
12496 /* Objective-C++ declaration/definition. */
12497 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12498 cp_parser_objc_declaration (parser, NULL_TREE);
12499 else if (c_dialect_objc ()
12500 && token1.keyword == RID_ATTRIBUTE
12501 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12502 cp_parser_objc_declaration (parser, attributes);
12503 /* At this point we may have a template declared by a concept
12504 introduction. */
12505 else if (flag_concepts
12506 && cp_parser_template_declaration_after_export (parser,
12507 /*member_p=*/false))
12508 /* We did. */;
12509 else
12510 /* Try to parse a block-declaration, or a function-definition. */
12511 cp_parser_block_declaration (parser, /*statement_p=*/false);
12513 /* Free any declarators allocated. */
12514 obstack_free (&declarator_obstack, p);
12517 /* Parse a block-declaration.
12519 block-declaration:
12520 simple-declaration
12521 asm-definition
12522 namespace-alias-definition
12523 using-declaration
12524 using-directive
12526 GNU Extension:
12528 block-declaration:
12529 __extension__ block-declaration
12531 C++0x Extension:
12533 block-declaration:
12534 static_assert-declaration
12536 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12537 part of a declaration-statement. */
12539 static void
12540 cp_parser_block_declaration (cp_parser *parser,
12541 bool statement_p)
12543 cp_token *token1;
12544 int saved_pedantic;
12546 /* Check for the `__extension__' keyword. */
12547 if (cp_parser_extension_opt (parser, &saved_pedantic))
12549 /* Parse the qualified declaration. */
12550 cp_parser_block_declaration (parser, statement_p);
12551 /* Restore the PEDANTIC flag. */
12552 pedantic = saved_pedantic;
12554 return;
12557 /* Peek at the next token to figure out which kind of declaration is
12558 present. */
12559 token1 = cp_lexer_peek_token (parser->lexer);
12561 /* If the next keyword is `asm', we have an asm-definition. */
12562 if (token1->keyword == RID_ASM)
12564 if (statement_p)
12565 cp_parser_commit_to_tentative_parse (parser);
12566 cp_parser_asm_definition (parser);
12568 /* If the next keyword is `namespace', we have a
12569 namespace-alias-definition. */
12570 else if (token1->keyword == RID_NAMESPACE)
12571 cp_parser_namespace_alias_definition (parser);
12572 /* If the next keyword is `using', we have a
12573 using-declaration, a using-directive, or an alias-declaration. */
12574 else if (token1->keyword == RID_USING)
12576 cp_token *token2;
12578 if (statement_p)
12579 cp_parser_commit_to_tentative_parse (parser);
12580 /* If the token after `using' is `namespace', then we have a
12581 using-directive. */
12582 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12583 if (token2->keyword == RID_NAMESPACE)
12584 cp_parser_using_directive (parser);
12585 /* If the second token after 'using' is '=', then we have an
12586 alias-declaration. */
12587 else if (cxx_dialect >= cxx11
12588 && token2->type == CPP_NAME
12589 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12590 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12591 cp_parser_alias_declaration (parser);
12592 /* Otherwise, it's a using-declaration. */
12593 else
12594 cp_parser_using_declaration (parser,
12595 /*access_declaration_p=*/false);
12597 /* If the next keyword is `__label__' we have a misplaced label
12598 declaration. */
12599 else if (token1->keyword == RID_LABEL)
12601 cp_lexer_consume_token (parser->lexer);
12602 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12603 cp_parser_skip_to_end_of_statement (parser);
12604 /* If the next token is now a `;', consume it. */
12605 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12606 cp_lexer_consume_token (parser->lexer);
12608 /* If the next token is `static_assert' we have a static assertion. */
12609 else if (token1->keyword == RID_STATIC_ASSERT)
12610 cp_parser_static_assert (parser, /*member_p=*/false);
12611 /* Anything else must be a simple-declaration. */
12612 else
12613 cp_parser_simple_declaration (parser, !statement_p,
12614 /*maybe_range_for_decl*/NULL);
12617 /* Parse a simple-declaration.
12619 simple-declaration:
12620 decl-specifier-seq [opt] init-declarator-list [opt] ;
12621 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12622 brace-or-equal-initializer ;
12624 init-declarator-list:
12625 init-declarator
12626 init-declarator-list , init-declarator
12628 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12629 function-definition as a simple-declaration.
12631 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12632 parsed declaration if it is an uninitialized single declarator not followed
12633 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12634 if present, will not be consumed. */
12636 static void
12637 cp_parser_simple_declaration (cp_parser* parser,
12638 bool function_definition_allowed_p,
12639 tree *maybe_range_for_decl)
12641 cp_decl_specifier_seq decl_specifiers;
12642 int declares_class_or_enum;
12643 bool saw_declarator;
12644 location_t comma_loc = UNKNOWN_LOCATION;
12645 location_t init_loc = UNKNOWN_LOCATION;
12647 if (maybe_range_for_decl)
12648 *maybe_range_for_decl = NULL_TREE;
12650 /* Defer access checks until we know what is being declared; the
12651 checks for names appearing in the decl-specifier-seq should be
12652 done as if we were in the scope of the thing being declared. */
12653 push_deferring_access_checks (dk_deferred);
12655 /* Parse the decl-specifier-seq. We have to keep track of whether
12656 or not the decl-specifier-seq declares a named class or
12657 enumeration type, since that is the only case in which the
12658 init-declarator-list is allowed to be empty.
12660 [dcl.dcl]
12662 In a simple-declaration, the optional init-declarator-list can be
12663 omitted only when declaring a class or enumeration, that is when
12664 the decl-specifier-seq contains either a class-specifier, an
12665 elaborated-type-specifier, or an enum-specifier. */
12666 cp_parser_decl_specifier_seq (parser,
12667 CP_PARSER_FLAGS_OPTIONAL,
12668 &decl_specifiers,
12669 &declares_class_or_enum);
12670 /* We no longer need to defer access checks. */
12671 stop_deferring_access_checks ();
12673 /* In a block scope, a valid declaration must always have a
12674 decl-specifier-seq. By not trying to parse declarators, we can
12675 resolve the declaration/expression ambiguity more quickly. */
12676 if (!function_definition_allowed_p
12677 && !decl_specifiers.any_specifiers_p)
12679 cp_parser_error (parser, "expected declaration");
12680 goto done;
12683 /* If the next two tokens are both identifiers, the code is
12684 erroneous. The usual cause of this situation is code like:
12686 T t;
12688 where "T" should name a type -- but does not. */
12689 if (!decl_specifiers.any_type_specifiers_p
12690 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12692 /* If parsing tentatively, we should commit; we really are
12693 looking at a declaration. */
12694 cp_parser_commit_to_tentative_parse (parser);
12695 /* Give up. */
12696 goto done;
12699 /* If we have seen at least one decl-specifier, and the next token
12700 is not a parenthesis, then we must be looking at a declaration.
12701 (After "int (" we might be looking at a functional cast.) */
12702 if (decl_specifiers.any_specifiers_p
12703 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12704 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12705 && !cp_parser_error_occurred (parser))
12706 cp_parser_commit_to_tentative_parse (parser);
12708 /* Look for C++17 decomposition declaration. */
12709 for (size_t n = 1; ; n++)
12710 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12711 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12712 continue;
12713 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12714 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12715 && decl_specifiers.any_specifiers_p)
12717 tree decl
12718 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12719 maybe_range_for_decl,
12720 &init_loc);
12722 /* The next token should be either a `,' or a `;'. */
12723 cp_token *token = cp_lexer_peek_token (parser->lexer);
12724 /* If it's a `;', we are done. */
12725 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12726 goto finish;
12727 /* Anything else is an error. */
12728 else
12730 /* If we have already issued an error message we don't need
12731 to issue another one. */
12732 if ((decl != error_mark_node
12733 && DECL_INITIAL (decl) != error_mark_node)
12734 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12735 cp_parser_error (parser, "expected %<,%> or %<;%>");
12736 /* Skip tokens until we reach the end of the statement. */
12737 cp_parser_skip_to_end_of_statement (parser);
12738 /* If the next token is now a `;', consume it. */
12739 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12740 cp_lexer_consume_token (parser->lexer);
12741 goto done;
12744 else
12745 break;
12747 tree last_type;
12748 bool auto_specifier_p;
12749 /* NULL_TREE if both variable and function declaration are allowed,
12750 error_mark_node if function declaration are not allowed and
12751 a FUNCTION_DECL that should be diagnosed if it is followed by
12752 variable declarations. */
12753 tree auto_function_declaration;
12755 last_type = NULL_TREE;
12756 auto_specifier_p
12757 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12758 auto_function_declaration = NULL_TREE;
12760 /* Keep going until we hit the `;' at the end of the simple
12761 declaration. */
12762 saw_declarator = false;
12763 while (cp_lexer_next_token_is_not (parser->lexer,
12764 CPP_SEMICOLON))
12766 cp_token *token;
12767 bool function_definition_p;
12768 tree decl;
12769 tree auto_result = NULL_TREE;
12771 if (saw_declarator)
12773 /* If we are processing next declarator, comma is expected */
12774 token = cp_lexer_peek_token (parser->lexer);
12775 gcc_assert (token->type == CPP_COMMA);
12776 cp_lexer_consume_token (parser->lexer);
12777 if (maybe_range_for_decl)
12779 *maybe_range_for_decl = error_mark_node;
12780 if (comma_loc == UNKNOWN_LOCATION)
12781 comma_loc = token->location;
12784 else
12785 saw_declarator = true;
12787 /* Parse the init-declarator. */
12788 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12789 /*checks=*/NULL,
12790 function_definition_allowed_p,
12791 /*member_p=*/false,
12792 declares_class_or_enum,
12793 &function_definition_p,
12794 maybe_range_for_decl,
12795 &init_loc,
12796 &auto_result);
12797 /* If an error occurred while parsing tentatively, exit quickly.
12798 (That usually happens when in the body of a function; each
12799 statement is treated as a declaration-statement until proven
12800 otherwise.) */
12801 if (cp_parser_error_occurred (parser))
12802 goto done;
12804 if (auto_specifier_p && cxx_dialect >= cxx14)
12806 /* If the init-declarator-list contains more than one
12807 init-declarator, they shall all form declarations of
12808 variables. */
12809 if (auto_function_declaration == NULL_TREE)
12810 auto_function_declaration
12811 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
12812 else if (TREE_CODE (decl) == FUNCTION_DECL
12813 || auto_function_declaration != error_mark_node)
12815 error_at (decl_specifiers.locations[ds_type_spec],
12816 "non-variable %qD in declaration with more than one "
12817 "declarator with placeholder type",
12818 TREE_CODE (decl) == FUNCTION_DECL
12819 ? decl : auto_function_declaration);
12820 auto_function_declaration = error_mark_node;
12824 if (auto_result
12825 && (!processing_template_decl || !type_uses_auto (auto_result)))
12827 if (last_type
12828 && last_type != error_mark_node
12829 && !same_type_p (auto_result, last_type))
12831 /* If the list of declarators contains more than one declarator,
12832 the type of each declared variable is determined as described
12833 above. If the type deduced for the template parameter U is not
12834 the same in each deduction, the program is ill-formed. */
12835 error_at (decl_specifiers.locations[ds_type_spec],
12836 "inconsistent deduction for %qT: %qT and then %qT",
12837 decl_specifiers.type, last_type, auto_result);
12838 last_type = error_mark_node;
12840 else
12841 last_type = auto_result;
12844 /* Handle function definitions specially. */
12845 if (function_definition_p)
12847 /* If the next token is a `,', then we are probably
12848 processing something like:
12850 void f() {}, *p;
12852 which is erroneous. */
12853 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12855 cp_token *token = cp_lexer_peek_token (parser->lexer);
12856 error_at (token->location,
12857 "mixing"
12858 " declarations and function-definitions is forbidden");
12860 /* Otherwise, we're done with the list of declarators. */
12861 else
12863 pop_deferring_access_checks ();
12864 return;
12867 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12868 *maybe_range_for_decl = decl;
12869 /* The next token should be either a `,' or a `;'. */
12870 token = cp_lexer_peek_token (parser->lexer);
12871 /* If it's a `,', there are more declarators to come. */
12872 if (token->type == CPP_COMMA)
12873 /* will be consumed next time around */;
12874 /* If it's a `;', we are done. */
12875 else if (token->type == CPP_SEMICOLON)
12876 break;
12877 else if (maybe_range_for_decl)
12879 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
12880 permerror (decl_specifiers.locations[ds_type_spec],
12881 "types may not be defined in a for-range-declaration");
12882 break;
12884 /* Anything else is an error. */
12885 else
12887 /* If we have already issued an error message we don't need
12888 to issue another one. */
12889 if ((decl != error_mark_node
12890 && DECL_INITIAL (decl) != error_mark_node)
12891 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12892 cp_parser_error (parser, "expected %<,%> or %<;%>");
12893 /* Skip tokens until we reach the end of the statement. */
12894 cp_parser_skip_to_end_of_statement (parser);
12895 /* If the next token is now a `;', consume it. */
12896 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12897 cp_lexer_consume_token (parser->lexer);
12898 goto done;
12900 /* After the first time around, a function-definition is not
12901 allowed -- even if it was OK at first. For example:
12903 int i, f() {}
12905 is not valid. */
12906 function_definition_allowed_p = false;
12909 /* Issue an error message if no declarators are present, and the
12910 decl-specifier-seq does not itself declare a class or
12911 enumeration: [dcl.dcl]/3. */
12912 if (!saw_declarator)
12914 if (cp_parser_declares_only_class_p (parser))
12916 if (!declares_class_or_enum
12917 && decl_specifiers.type
12918 && OVERLOAD_TYPE_P (decl_specifiers.type))
12919 /* Ensure an error is issued anyway when finish_decltype_type,
12920 called via cp_parser_decl_specifier_seq, returns a class or
12921 an enumeration (c++/51786). */
12922 decl_specifiers.type = NULL_TREE;
12923 shadow_tag (&decl_specifiers);
12925 /* Perform any deferred access checks. */
12926 perform_deferred_access_checks (tf_warning_or_error);
12929 /* Consume the `;'. */
12930 finish:
12931 if (!maybe_range_for_decl)
12932 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12933 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12935 if (init_loc != UNKNOWN_LOCATION)
12936 error_at (init_loc, "initializer in range-based %<for%> loop");
12937 if (comma_loc != UNKNOWN_LOCATION)
12938 error_at (comma_loc,
12939 "multiple declarations in range-based %<for%> loop");
12942 done:
12943 pop_deferring_access_checks ();
12946 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
12947 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12948 initializer ; */
12950 static tree
12951 cp_parser_decomposition_declaration (cp_parser *parser,
12952 cp_decl_specifier_seq *decl_specifiers,
12953 tree *maybe_range_for_decl,
12954 location_t *init_loc)
12956 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
12957 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12958 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
12960 /* Parse the identifier-list. */
12961 auto_vec<cp_expr, 10> v;
12962 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12963 while (true)
12965 cp_expr e = cp_parser_identifier (parser);
12966 if (e.get_value () == error_mark_node)
12967 break;
12968 v.safe_push (e);
12969 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12970 break;
12971 cp_lexer_consume_token (parser->lexer);
12974 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
12975 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
12977 end_loc = UNKNOWN_LOCATION;
12978 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
12979 false);
12980 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12981 cp_lexer_consume_token (parser->lexer);
12982 else
12984 cp_parser_skip_to_end_of_statement (parser);
12985 return error_mark_node;
12989 if (cxx_dialect < cxx1z)
12990 pedwarn (loc, 0, "decomposition declaration only available with "
12991 "-std=c++1z or -std=gnu++1z");
12993 tree pushed_scope;
12994 cp_declarator *declarator = make_declarator (cdk_decomp);
12995 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
12996 declarator->id_loc = loc;
12997 if (ref_qual != REF_QUAL_NONE)
12998 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
12999 ref_qual == REF_QUAL_RVALUE,
13000 NULL_TREE);
13001 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13002 NULL_TREE, decl_specifiers->attributes,
13003 &pushed_scope);
13004 tree orig_decl = decl;
13006 unsigned int i;
13007 cp_expr e;
13008 cp_decl_specifier_seq decl_specs;
13009 clear_decl_specs (&decl_specs);
13010 decl_specs.type = make_auto ();
13011 tree prev = decl;
13012 FOR_EACH_VEC_ELT (v, i, e)
13014 if (i == 0)
13015 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13016 else
13017 declarator->u.id.unqualified_name = e.get_value ();
13018 declarator->id_loc = e.get_location ();
13019 tree elt_pushed_scope;
13020 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13021 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13022 if (decl2 == error_mark_node)
13023 decl = error_mark_node;
13024 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13026 /* Ensure we've diagnosed redeclaration if we aren't creating
13027 a new VAR_DECL. */
13028 gcc_assert (errorcount);
13029 decl = error_mark_node;
13031 else
13032 prev = decl2;
13033 if (elt_pushed_scope)
13034 pop_scope (elt_pushed_scope);
13037 if (v.is_empty ())
13039 error_at (loc, "empty decomposition declaration");
13040 decl = error_mark_node;
13043 if (maybe_range_for_decl == NULL
13044 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13046 bool non_constant_p = false, is_direct_init = false;
13047 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13048 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13049 &non_constant_p);
13051 if (decl != error_mark_node)
13053 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13054 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13055 cp_finish_decomp (decl, prev, v.length ());
13058 else if (decl != error_mark_node)
13060 *maybe_range_for_decl = prev;
13061 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13062 the underlying DECL. */
13063 cp_finish_decomp (decl, prev, v.length ());
13066 if (pushed_scope)
13067 pop_scope (pushed_scope);
13069 if (decl == error_mark_node && DECL_P (orig_decl))
13071 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13072 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13075 return decl;
13078 /* Parse a decl-specifier-seq.
13080 decl-specifier-seq:
13081 decl-specifier-seq [opt] decl-specifier
13082 decl-specifier attribute-specifier-seq [opt] (C++11)
13084 decl-specifier:
13085 storage-class-specifier
13086 type-specifier
13087 function-specifier
13088 friend
13089 typedef
13091 GNU Extension:
13093 decl-specifier:
13094 attributes
13096 Concepts Extension:
13098 decl-specifier:
13099 concept
13101 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13103 The parser flags FLAGS is used to control type-specifier parsing.
13105 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13106 flags:
13108 1: one of the decl-specifiers is an elaborated-type-specifier
13109 (i.e., a type declaration)
13110 2: one of the decl-specifiers is an enum-specifier or a
13111 class-specifier (i.e., a type definition)
13115 static void
13116 cp_parser_decl_specifier_seq (cp_parser* parser,
13117 cp_parser_flags flags,
13118 cp_decl_specifier_seq *decl_specs,
13119 int* declares_class_or_enum)
13121 bool constructor_possible_p = !parser->in_declarator_p;
13122 bool found_decl_spec = false;
13123 cp_token *start_token = NULL;
13124 cp_decl_spec ds;
13126 /* Clear DECL_SPECS. */
13127 clear_decl_specs (decl_specs);
13129 /* Assume no class or enumeration type is declared. */
13130 *declares_class_or_enum = 0;
13132 /* Keep reading specifiers until there are no more to read. */
13133 while (true)
13135 bool constructor_p;
13136 cp_token *token;
13137 ds = ds_last;
13139 /* Peek at the next token. */
13140 token = cp_lexer_peek_token (parser->lexer);
13142 /* Save the first token of the decl spec list for error
13143 reporting. */
13144 if (!start_token)
13145 start_token = token;
13146 /* Handle attributes. */
13147 if (cp_next_tokens_can_be_attribute_p (parser))
13149 /* Parse the attributes. */
13150 tree attrs = cp_parser_attributes_opt (parser);
13152 /* In a sequence of declaration specifiers, c++11 attributes
13153 appertain to the type that precede them. In that case
13154 [dcl.spec]/1 says:
13156 The attribute-specifier-seq affects the type only for
13157 the declaration it appears in, not other declarations
13158 involving the same type.
13160 But for now let's force the user to position the
13161 attribute either at the beginning of the declaration or
13162 after the declarator-id, which would clearly mean that it
13163 applies to the declarator. */
13164 if (cxx11_attribute_p (attrs))
13166 if (!found_decl_spec)
13167 /* The c++11 attribute is at the beginning of the
13168 declaration. It appertains to the entity being
13169 declared. */;
13170 else
13172 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13174 /* This is an attribute following a
13175 class-specifier. */
13176 if (decl_specs->type_definition_p)
13177 warn_misplaced_attr_for_class_type (token->location,
13178 decl_specs->type);
13179 attrs = NULL_TREE;
13181 else
13183 decl_specs->std_attributes
13184 = chainon (decl_specs->std_attributes,
13185 attrs);
13186 if (decl_specs->locations[ds_std_attribute] == 0)
13187 decl_specs->locations[ds_std_attribute] = token->location;
13189 continue;
13193 decl_specs->attributes
13194 = chainon (decl_specs->attributes,
13195 attrs);
13196 if (decl_specs->locations[ds_attribute] == 0)
13197 decl_specs->locations[ds_attribute] = token->location;
13198 continue;
13200 /* Assume we will find a decl-specifier keyword. */
13201 found_decl_spec = true;
13202 /* If the next token is an appropriate keyword, we can simply
13203 add it to the list. */
13204 switch (token->keyword)
13206 /* decl-specifier:
13207 friend
13208 constexpr */
13209 case RID_FRIEND:
13210 if (!at_class_scope_p ())
13212 error_at (token->location, "%<friend%> used outside of class");
13213 cp_lexer_purge_token (parser->lexer);
13215 else
13217 ds = ds_friend;
13218 /* Consume the token. */
13219 cp_lexer_consume_token (parser->lexer);
13221 break;
13223 case RID_CONSTEXPR:
13224 ds = ds_constexpr;
13225 cp_lexer_consume_token (parser->lexer);
13226 break;
13228 case RID_CONCEPT:
13229 ds = ds_concept;
13230 cp_lexer_consume_token (parser->lexer);
13231 break;
13233 /* function-specifier:
13234 inline
13235 virtual
13236 explicit */
13237 case RID_INLINE:
13238 case RID_VIRTUAL:
13239 case RID_EXPLICIT:
13240 cp_parser_function_specifier_opt (parser, decl_specs);
13241 break;
13243 /* decl-specifier:
13244 typedef */
13245 case RID_TYPEDEF:
13246 ds = ds_typedef;
13247 /* Consume the token. */
13248 cp_lexer_consume_token (parser->lexer);
13249 /* A constructor declarator cannot appear in a typedef. */
13250 constructor_possible_p = false;
13251 /* The "typedef" keyword can only occur in a declaration; we
13252 may as well commit at this point. */
13253 cp_parser_commit_to_tentative_parse (parser);
13255 if (decl_specs->storage_class != sc_none)
13256 decl_specs->conflicting_specifiers_p = true;
13257 break;
13259 /* storage-class-specifier:
13260 auto
13261 register
13262 static
13263 extern
13264 mutable
13266 GNU Extension:
13267 thread */
13268 case RID_AUTO:
13269 if (cxx_dialect == cxx98)
13271 /* Consume the token. */
13272 cp_lexer_consume_token (parser->lexer);
13274 /* Complain about `auto' as a storage specifier, if
13275 we're complaining about C++0x compatibility. */
13276 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
13277 " changes meaning in C++11; please remove it");
13279 /* Set the storage class anyway. */
13280 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13281 token);
13283 else
13284 /* C++0x auto type-specifier. */
13285 found_decl_spec = false;
13286 break;
13288 case RID_REGISTER:
13289 case RID_STATIC:
13290 case RID_EXTERN:
13291 case RID_MUTABLE:
13292 /* Consume the token. */
13293 cp_lexer_consume_token (parser->lexer);
13294 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13295 token);
13296 break;
13297 case RID_THREAD:
13298 /* Consume the token. */
13299 ds = ds_thread;
13300 cp_lexer_consume_token (parser->lexer);
13301 break;
13303 default:
13304 /* We did not yet find a decl-specifier yet. */
13305 found_decl_spec = false;
13306 break;
13309 if (found_decl_spec
13310 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13311 && token->keyword != RID_CONSTEXPR)
13312 error ("decl-specifier invalid in condition");
13314 if (found_decl_spec
13315 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13316 && token->keyword != RID_MUTABLE
13317 && token->keyword != RID_CONSTEXPR)
13318 error_at (token->location, "%qD invalid in lambda",
13319 ridpointers[token->keyword]);
13321 if (ds != ds_last)
13322 set_and_check_decl_spec_loc (decl_specs, ds, token);
13324 /* Constructors are a special case. The `S' in `S()' is not a
13325 decl-specifier; it is the beginning of the declarator. */
13326 constructor_p
13327 = (!found_decl_spec
13328 && constructor_possible_p
13329 && (cp_parser_constructor_declarator_p
13330 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13332 /* If we don't have a DECL_SPEC yet, then we must be looking at
13333 a type-specifier. */
13334 if (!found_decl_spec && !constructor_p)
13336 int decl_spec_declares_class_or_enum;
13337 bool is_cv_qualifier;
13338 tree type_spec;
13340 type_spec
13341 = cp_parser_type_specifier (parser, flags,
13342 decl_specs,
13343 /*is_declaration=*/true,
13344 &decl_spec_declares_class_or_enum,
13345 &is_cv_qualifier);
13346 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13348 /* If this type-specifier referenced a user-defined type
13349 (a typedef, class-name, etc.), then we can't allow any
13350 more such type-specifiers henceforth.
13352 [dcl.spec]
13354 The longest sequence of decl-specifiers that could
13355 possibly be a type name is taken as the
13356 decl-specifier-seq of a declaration. The sequence shall
13357 be self-consistent as described below.
13359 [dcl.type]
13361 As a general rule, at most one type-specifier is allowed
13362 in the complete decl-specifier-seq of a declaration. The
13363 only exceptions are the following:
13365 -- const or volatile can be combined with any other
13366 type-specifier.
13368 -- signed or unsigned can be combined with char, long,
13369 short, or int.
13371 -- ..
13373 Example:
13375 typedef char* Pc;
13376 void g (const int Pc);
13378 Here, Pc is *not* part of the decl-specifier seq; it's
13379 the declarator. Therefore, once we see a type-specifier
13380 (other than a cv-qualifier), we forbid any additional
13381 user-defined types. We *do* still allow things like `int
13382 int' to be considered a decl-specifier-seq, and issue the
13383 error message later. */
13384 if (type_spec && !is_cv_qualifier)
13385 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13386 /* A constructor declarator cannot follow a type-specifier. */
13387 if (type_spec)
13389 constructor_possible_p = false;
13390 found_decl_spec = true;
13391 if (!is_cv_qualifier)
13392 decl_specs->any_type_specifiers_p = true;
13396 /* If we still do not have a DECL_SPEC, then there are no more
13397 decl-specifiers. */
13398 if (!found_decl_spec)
13399 break;
13401 decl_specs->any_specifiers_p = true;
13402 /* After we see one decl-specifier, further decl-specifiers are
13403 always optional. */
13404 flags |= CP_PARSER_FLAGS_OPTIONAL;
13407 /* Don't allow a friend specifier with a class definition. */
13408 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13409 && (*declares_class_or_enum & 2))
13410 error_at (decl_specs->locations[ds_friend],
13411 "class definition may not be declared a friend");
13414 /* Parse an (optional) storage-class-specifier.
13416 storage-class-specifier:
13417 auto
13418 register
13419 static
13420 extern
13421 mutable
13423 GNU Extension:
13425 storage-class-specifier:
13426 thread
13428 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13430 static tree
13431 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13433 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13435 case RID_AUTO:
13436 if (cxx_dialect != cxx98)
13437 return NULL_TREE;
13438 /* Fall through for C++98. */
13439 gcc_fallthrough ();
13441 case RID_REGISTER:
13442 case RID_STATIC:
13443 case RID_EXTERN:
13444 case RID_MUTABLE:
13445 case RID_THREAD:
13446 /* Consume the token. */
13447 return cp_lexer_consume_token (parser->lexer)->u.value;
13449 default:
13450 return NULL_TREE;
13454 /* Parse an (optional) function-specifier.
13456 function-specifier:
13457 inline
13458 virtual
13459 explicit
13461 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13462 Updates DECL_SPECS, if it is non-NULL. */
13464 static tree
13465 cp_parser_function_specifier_opt (cp_parser* parser,
13466 cp_decl_specifier_seq *decl_specs)
13468 cp_token *token = cp_lexer_peek_token (parser->lexer);
13469 switch (token->keyword)
13471 case RID_INLINE:
13472 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13473 break;
13475 case RID_VIRTUAL:
13476 /* 14.5.2.3 [temp.mem]
13478 A member function template shall not be virtual. */
13479 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13480 && current_class_type)
13481 error_at (token->location, "templates may not be %<virtual%>");
13482 else
13483 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13484 break;
13486 case RID_EXPLICIT:
13487 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13488 break;
13490 default:
13491 return NULL_TREE;
13494 /* Consume the token. */
13495 return cp_lexer_consume_token (parser->lexer)->u.value;
13498 /* Parse a linkage-specification.
13500 linkage-specification:
13501 extern string-literal { declaration-seq [opt] }
13502 extern string-literal declaration */
13504 static void
13505 cp_parser_linkage_specification (cp_parser* parser)
13507 tree linkage;
13509 /* Look for the `extern' keyword. */
13510 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13512 /* Look for the string-literal. */
13513 linkage = cp_parser_string_literal (parser, false, false);
13515 /* Transform the literal into an identifier. If the literal is a
13516 wide-character string, or contains embedded NULs, then we can't
13517 handle it as the user wants. */
13518 if (strlen (TREE_STRING_POINTER (linkage))
13519 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13521 cp_parser_error (parser, "invalid linkage-specification");
13522 /* Assume C++ linkage. */
13523 linkage = lang_name_cplusplus;
13525 else
13526 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13528 /* We're now using the new linkage. */
13529 push_lang_context (linkage);
13531 /* If the next token is a `{', then we're using the first
13532 production. */
13533 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13535 cp_ensure_no_omp_declare_simd (parser);
13536 cp_ensure_no_oacc_routine (parser);
13538 /* Consume the `{' token. */
13539 cp_lexer_consume_token (parser->lexer);
13540 /* Parse the declarations. */
13541 cp_parser_declaration_seq_opt (parser);
13542 /* Look for the closing `}'. */
13543 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13545 /* Otherwise, there's just one declaration. */
13546 else
13548 bool saved_in_unbraced_linkage_specification_p;
13550 saved_in_unbraced_linkage_specification_p
13551 = parser->in_unbraced_linkage_specification_p;
13552 parser->in_unbraced_linkage_specification_p = true;
13553 cp_parser_declaration (parser);
13554 parser->in_unbraced_linkage_specification_p
13555 = saved_in_unbraced_linkage_specification_p;
13558 /* We're done with the linkage-specification. */
13559 pop_lang_context ();
13562 /* Parse a static_assert-declaration.
13564 static_assert-declaration:
13565 static_assert ( constant-expression , string-literal ) ;
13566 static_assert ( constant-expression ) ; (C++1Z)
13568 If MEMBER_P, this static_assert is a class member. */
13570 static void
13571 cp_parser_static_assert(cp_parser *parser, bool member_p)
13573 tree condition;
13574 tree message;
13575 cp_token *token;
13576 location_t saved_loc;
13577 bool dummy;
13579 /* Peek at the `static_assert' token so we can keep track of exactly
13580 where the static assertion started. */
13581 token = cp_lexer_peek_token (parser->lexer);
13582 saved_loc = token->location;
13584 /* Look for the `static_assert' keyword. */
13585 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13586 RT_STATIC_ASSERT))
13587 return;
13589 /* We know we are in a static assertion; commit to any tentative
13590 parse. */
13591 if (cp_parser_parsing_tentatively (parser))
13592 cp_parser_commit_to_tentative_parse (parser);
13594 /* Parse the `(' starting the static assertion condition. */
13595 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13597 /* Parse the constant-expression. Allow a non-constant expression
13598 here in order to give better diagnostics in finish_static_assert. */
13599 condition =
13600 cp_parser_constant_expression (parser,
13601 /*allow_non_constant_p=*/true,
13602 /*non_constant_p=*/&dummy);
13604 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13606 if (cxx_dialect < cxx1z)
13607 pedwarn (input_location, OPT_Wpedantic,
13608 "static_assert without a message "
13609 "only available with -std=c++1z or -std=gnu++1z");
13610 /* Eat the ')' */
13611 cp_lexer_consume_token (parser->lexer);
13612 message = build_string (1, "");
13613 TREE_TYPE (message) = char_array_type_node;
13614 fix_string_type (message);
13616 else
13618 /* Parse the separating `,'. */
13619 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13621 /* Parse the string-literal message. */
13622 message = cp_parser_string_literal (parser,
13623 /*translate=*/false,
13624 /*wide_ok=*/true);
13626 /* A `)' completes the static assertion. */
13627 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13628 cp_parser_skip_to_closing_parenthesis (parser,
13629 /*recovering=*/true,
13630 /*or_comma=*/false,
13631 /*consume_paren=*/true);
13634 /* A semicolon terminates the declaration. */
13635 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13637 /* Complete the static assertion, which may mean either processing
13638 the static assert now or saving it for template instantiation. */
13639 finish_static_assert (condition, message, saved_loc, member_p);
13642 /* Parse the expression in decltype ( expression ). */
13644 static tree
13645 cp_parser_decltype_expr (cp_parser *parser,
13646 bool &id_expression_or_member_access_p)
13648 cp_token *id_expr_start_token;
13649 tree expr;
13651 /* Since we're going to preserve any side-effects from this parse, set up a
13652 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13653 in the expression. */
13654 tentative_firewall firewall (parser);
13656 /* First, try parsing an id-expression. */
13657 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13658 cp_parser_parse_tentatively (parser);
13659 expr = cp_parser_id_expression (parser,
13660 /*template_keyword_p=*/false,
13661 /*check_dependency_p=*/true,
13662 /*template_p=*/NULL,
13663 /*declarator_p=*/false,
13664 /*optional_p=*/false);
13666 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13668 bool non_integral_constant_expression_p = false;
13669 tree id_expression = expr;
13670 cp_id_kind idk;
13671 const char *error_msg;
13673 if (identifier_p (expr))
13674 /* Lookup the name we got back from the id-expression. */
13675 expr = cp_parser_lookup_name_simple (parser, expr,
13676 id_expr_start_token->location);
13678 if (expr
13679 && expr != error_mark_node
13680 && TREE_CODE (expr) != TYPE_DECL
13681 && (TREE_CODE (expr) != BIT_NOT_EXPR
13682 || !TYPE_P (TREE_OPERAND (expr, 0)))
13683 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13685 /* Complete lookup of the id-expression. */
13686 expr = (finish_id_expression
13687 (id_expression, expr, parser->scope, &idk,
13688 /*integral_constant_expression_p=*/false,
13689 /*allow_non_integral_constant_expression_p=*/true,
13690 &non_integral_constant_expression_p,
13691 /*template_p=*/false,
13692 /*done=*/true,
13693 /*address_p=*/false,
13694 /*template_arg_p=*/false,
13695 &error_msg,
13696 id_expr_start_token->location));
13698 if (expr == error_mark_node)
13699 /* We found an id-expression, but it was something that we
13700 should not have found. This is an error, not something
13701 we can recover from, so note that we found an
13702 id-expression and we'll recover as gracefully as
13703 possible. */
13704 id_expression_or_member_access_p = true;
13707 if (expr
13708 && expr != error_mark_node
13709 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13710 /* We have an id-expression. */
13711 id_expression_or_member_access_p = true;
13714 if (!id_expression_or_member_access_p)
13716 /* Abort the id-expression parse. */
13717 cp_parser_abort_tentative_parse (parser);
13719 /* Parsing tentatively, again. */
13720 cp_parser_parse_tentatively (parser);
13722 /* Parse a class member access. */
13723 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13724 /*cast_p=*/false, /*decltype*/true,
13725 /*member_access_only_p=*/true, NULL);
13727 if (expr
13728 && expr != error_mark_node
13729 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13730 /* We have an id-expression. */
13731 id_expression_or_member_access_p = true;
13734 if (id_expression_or_member_access_p)
13735 /* We have parsed the complete id-expression or member access. */
13736 cp_parser_parse_definitely (parser);
13737 else
13739 /* Abort our attempt to parse an id-expression or member access
13740 expression. */
13741 cp_parser_abort_tentative_parse (parser);
13743 /* Parse a full expression. */
13744 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13745 /*decltype_p=*/true);
13748 return expr;
13751 /* Parse a `decltype' type. Returns the type.
13753 simple-type-specifier:
13754 decltype ( expression )
13755 C++14 proposal:
13756 decltype ( auto ) */
13758 static tree
13759 cp_parser_decltype (cp_parser *parser)
13761 tree expr;
13762 bool id_expression_or_member_access_p = false;
13763 const char *saved_message;
13764 bool saved_integral_constant_expression_p;
13765 bool saved_non_integral_constant_expression_p;
13766 bool saved_greater_than_is_operator_p;
13767 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13769 if (start_token->type == CPP_DECLTYPE)
13771 /* Already parsed. */
13772 cp_lexer_consume_token (parser->lexer);
13773 return saved_checks_value (start_token->u.tree_check_value);
13776 /* Look for the `decltype' token. */
13777 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13778 return error_mark_node;
13780 /* Parse the opening `('. */
13781 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13782 return error_mark_node;
13784 /* decltype (auto) */
13785 if (cxx_dialect >= cxx14
13786 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13788 cp_lexer_consume_token (parser->lexer);
13789 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13790 return error_mark_node;
13791 expr = make_decltype_auto ();
13792 AUTO_IS_DECLTYPE (expr) = true;
13793 goto rewrite;
13796 /* Types cannot be defined in a `decltype' expression. Save away the
13797 old message. */
13798 saved_message = parser->type_definition_forbidden_message;
13800 /* And create the new one. */
13801 parser->type_definition_forbidden_message
13802 = G_("types may not be defined in %<decltype%> expressions");
13804 /* The restrictions on constant-expressions do not apply inside
13805 decltype expressions. */
13806 saved_integral_constant_expression_p
13807 = parser->integral_constant_expression_p;
13808 saved_non_integral_constant_expression_p
13809 = parser->non_integral_constant_expression_p;
13810 parser->integral_constant_expression_p = false;
13812 /* Within a parenthesized expression, a `>' token is always
13813 the greater-than operator. */
13814 saved_greater_than_is_operator_p
13815 = parser->greater_than_is_operator_p;
13816 parser->greater_than_is_operator_p = true;
13818 /* Do not actually evaluate the expression. */
13819 ++cp_unevaluated_operand;
13821 /* Do not warn about problems with the expression. */
13822 ++c_inhibit_evaluation_warnings;
13824 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13826 /* Go back to evaluating expressions. */
13827 --cp_unevaluated_operand;
13828 --c_inhibit_evaluation_warnings;
13830 /* The `>' token might be the end of a template-id or
13831 template-parameter-list now. */
13832 parser->greater_than_is_operator_p
13833 = saved_greater_than_is_operator_p;
13835 /* Restore the old message and the integral constant expression
13836 flags. */
13837 parser->type_definition_forbidden_message = saved_message;
13838 parser->integral_constant_expression_p
13839 = saved_integral_constant_expression_p;
13840 parser->non_integral_constant_expression_p
13841 = saved_non_integral_constant_expression_p;
13843 /* Parse to the closing `)'. */
13844 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13846 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13847 /*consume_paren=*/true);
13848 return error_mark_node;
13851 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13852 tf_warning_or_error);
13854 rewrite:
13855 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13856 it again. */
13857 start_token->type = CPP_DECLTYPE;
13858 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13859 start_token->u.tree_check_value->value = expr;
13860 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13861 start_token->keyword = RID_MAX;
13862 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13864 return expr;
13867 /* Special member functions [gram.special] */
13869 /* Parse a conversion-function-id.
13871 conversion-function-id:
13872 operator conversion-type-id
13874 Returns an IDENTIFIER_NODE representing the operator. */
13876 static tree
13877 cp_parser_conversion_function_id (cp_parser* parser)
13879 tree type;
13880 tree saved_scope;
13881 tree saved_qualifying_scope;
13882 tree saved_object_scope;
13883 tree pushed_scope = NULL_TREE;
13885 /* Look for the `operator' token. */
13886 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13887 return error_mark_node;
13888 /* When we parse the conversion-type-id, the current scope will be
13889 reset. However, we need that information in able to look up the
13890 conversion function later, so we save it here. */
13891 saved_scope = parser->scope;
13892 saved_qualifying_scope = parser->qualifying_scope;
13893 saved_object_scope = parser->object_scope;
13894 /* We must enter the scope of the class so that the names of
13895 entities declared within the class are available in the
13896 conversion-type-id. For example, consider:
13898 struct S {
13899 typedef int I;
13900 operator I();
13903 S::operator I() { ... }
13905 In order to see that `I' is a type-name in the definition, we
13906 must be in the scope of `S'. */
13907 if (saved_scope)
13908 pushed_scope = push_scope (saved_scope);
13909 /* Parse the conversion-type-id. */
13910 type = cp_parser_conversion_type_id (parser);
13911 /* Leave the scope of the class, if any. */
13912 if (pushed_scope)
13913 pop_scope (pushed_scope);
13914 /* Restore the saved scope. */
13915 parser->scope = saved_scope;
13916 parser->qualifying_scope = saved_qualifying_scope;
13917 parser->object_scope = saved_object_scope;
13918 /* If the TYPE is invalid, indicate failure. */
13919 if (type == error_mark_node)
13920 return error_mark_node;
13921 return mangle_conv_op_name_for_type (type);
13924 /* Parse a conversion-type-id:
13926 conversion-type-id:
13927 type-specifier-seq conversion-declarator [opt]
13929 Returns the TYPE specified. */
13931 static tree
13932 cp_parser_conversion_type_id (cp_parser* parser)
13934 tree attributes;
13935 cp_decl_specifier_seq type_specifiers;
13936 cp_declarator *declarator;
13937 tree type_specified;
13938 const char *saved_message;
13940 /* Parse the attributes. */
13941 attributes = cp_parser_attributes_opt (parser);
13943 saved_message = parser->type_definition_forbidden_message;
13944 parser->type_definition_forbidden_message
13945 = G_("types may not be defined in a conversion-type-id");
13947 /* Parse the type-specifiers. */
13948 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13949 /*is_trailing_return=*/false,
13950 &type_specifiers);
13952 parser->type_definition_forbidden_message = saved_message;
13954 /* If that didn't work, stop. */
13955 if (type_specifiers.type == error_mark_node)
13956 return error_mark_node;
13957 /* Parse the conversion-declarator. */
13958 declarator = cp_parser_conversion_declarator_opt (parser);
13960 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13961 /*initialized=*/0, &attributes);
13962 if (attributes)
13963 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13965 /* Don't give this error when parsing tentatively. This happens to
13966 work because we always parse this definitively once. */
13967 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13968 && type_uses_auto (type_specified))
13970 if (cxx_dialect < cxx14)
13972 error ("invalid use of %<auto%> in conversion operator");
13973 return error_mark_node;
13975 else if (template_parm_scope_p ())
13976 warning (0, "use of %<auto%> in member template "
13977 "conversion operator can never be deduced");
13980 return type_specified;
13983 /* Parse an (optional) conversion-declarator.
13985 conversion-declarator:
13986 ptr-operator conversion-declarator [opt]
13990 static cp_declarator *
13991 cp_parser_conversion_declarator_opt (cp_parser* parser)
13993 enum tree_code code;
13994 tree class_type, std_attributes = NULL_TREE;
13995 cp_cv_quals cv_quals;
13997 /* We don't know if there's a ptr-operator next, or not. */
13998 cp_parser_parse_tentatively (parser);
13999 /* Try the ptr-operator. */
14000 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14001 &std_attributes);
14002 /* If it worked, look for more conversion-declarators. */
14003 if (cp_parser_parse_definitely (parser))
14005 cp_declarator *declarator;
14007 /* Parse another optional declarator. */
14008 declarator = cp_parser_conversion_declarator_opt (parser);
14010 declarator = cp_parser_make_indirect_declarator
14011 (code, class_type, cv_quals, declarator, std_attributes);
14013 return declarator;
14016 return NULL;
14019 /* Parse an (optional) ctor-initializer.
14021 ctor-initializer:
14022 : mem-initializer-list
14024 Returns TRUE iff the ctor-initializer was actually present. */
14026 static bool
14027 cp_parser_ctor_initializer_opt (cp_parser* parser)
14029 /* If the next token is not a `:', then there is no
14030 ctor-initializer. */
14031 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14033 /* Do default initialization of any bases and members. */
14034 if (DECL_CONSTRUCTOR_P (current_function_decl))
14035 finish_mem_initializers (NULL_TREE);
14037 return false;
14040 /* Consume the `:' token. */
14041 cp_lexer_consume_token (parser->lexer);
14042 /* And the mem-initializer-list. */
14043 cp_parser_mem_initializer_list (parser);
14045 return true;
14048 /* Parse a mem-initializer-list.
14050 mem-initializer-list:
14051 mem-initializer ... [opt]
14052 mem-initializer ... [opt] , mem-initializer-list */
14054 static void
14055 cp_parser_mem_initializer_list (cp_parser* parser)
14057 tree mem_initializer_list = NULL_TREE;
14058 tree target_ctor = error_mark_node;
14059 cp_token *token = cp_lexer_peek_token (parser->lexer);
14061 /* Let the semantic analysis code know that we are starting the
14062 mem-initializer-list. */
14063 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14064 error_at (token->location,
14065 "only constructors take member initializers");
14067 /* Loop through the list. */
14068 while (true)
14070 tree mem_initializer;
14072 token = cp_lexer_peek_token (parser->lexer);
14073 /* Parse the mem-initializer. */
14074 mem_initializer = cp_parser_mem_initializer (parser);
14075 /* If the next token is a `...', we're expanding member initializers. */
14076 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14078 /* Consume the `...'. */
14079 cp_lexer_consume_token (parser->lexer);
14081 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14082 can be expanded but members cannot. */
14083 if (mem_initializer != error_mark_node
14084 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14086 error_at (token->location,
14087 "cannot expand initializer for member %<%D%>",
14088 TREE_PURPOSE (mem_initializer));
14089 mem_initializer = error_mark_node;
14092 /* Construct the pack expansion type. */
14093 if (mem_initializer != error_mark_node)
14094 mem_initializer = make_pack_expansion (mem_initializer);
14096 if (target_ctor != error_mark_node
14097 && mem_initializer != error_mark_node)
14099 error ("mem-initializer for %qD follows constructor delegation",
14100 TREE_PURPOSE (mem_initializer));
14101 mem_initializer = error_mark_node;
14103 /* Look for a target constructor. */
14104 if (mem_initializer != error_mark_node
14105 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14106 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14108 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14109 if (mem_initializer_list)
14111 error ("constructor delegation follows mem-initializer for %qD",
14112 TREE_PURPOSE (mem_initializer_list));
14113 mem_initializer = error_mark_node;
14115 target_ctor = mem_initializer;
14117 /* Add it to the list, unless it was erroneous. */
14118 if (mem_initializer != error_mark_node)
14120 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14121 mem_initializer_list = mem_initializer;
14123 /* If the next token is not a `,', we're done. */
14124 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14125 break;
14126 /* Consume the `,' token. */
14127 cp_lexer_consume_token (parser->lexer);
14130 /* Perform semantic analysis. */
14131 if (DECL_CONSTRUCTOR_P (current_function_decl))
14132 finish_mem_initializers (mem_initializer_list);
14135 /* Parse a mem-initializer.
14137 mem-initializer:
14138 mem-initializer-id ( expression-list [opt] )
14139 mem-initializer-id braced-init-list
14141 GNU extension:
14143 mem-initializer:
14144 ( expression-list [opt] )
14146 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14147 class) or FIELD_DECL (for a non-static data member) to initialize;
14148 the TREE_VALUE is the expression-list. An empty initialization
14149 list is represented by void_list_node. */
14151 static tree
14152 cp_parser_mem_initializer (cp_parser* parser)
14154 tree mem_initializer_id;
14155 tree expression_list;
14156 tree member;
14157 cp_token *token = cp_lexer_peek_token (parser->lexer);
14159 /* Find out what is being initialized. */
14160 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14162 permerror (token->location,
14163 "anachronistic old-style base class initializer");
14164 mem_initializer_id = NULL_TREE;
14166 else
14168 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14169 if (mem_initializer_id == error_mark_node)
14170 return mem_initializer_id;
14172 member = expand_member_init (mem_initializer_id);
14173 if (member && !DECL_P (member))
14174 in_base_initializer = 1;
14176 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14178 bool expr_non_constant_p;
14179 cp_lexer_set_source_position (parser->lexer);
14180 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14181 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14182 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14183 expression_list = build_tree_list (NULL_TREE, expression_list);
14185 else
14187 vec<tree, va_gc> *vec;
14188 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14189 /*cast_p=*/false,
14190 /*allow_expansion_p=*/true,
14191 /*non_constant_p=*/NULL);
14192 if (vec == NULL)
14193 return error_mark_node;
14194 expression_list = build_tree_list_vec (vec);
14195 release_tree_vector (vec);
14198 if (expression_list == error_mark_node)
14199 return error_mark_node;
14200 if (!expression_list)
14201 expression_list = void_type_node;
14203 in_base_initializer = 0;
14205 return member ? build_tree_list (member, expression_list) : error_mark_node;
14208 /* Parse a mem-initializer-id.
14210 mem-initializer-id:
14211 :: [opt] nested-name-specifier [opt] class-name
14212 decltype-specifier (C++11)
14213 identifier
14215 Returns a TYPE indicating the class to be initialized for the first
14216 production (and the second in C++11). Returns an IDENTIFIER_NODE
14217 indicating the data member to be initialized for the last production. */
14219 static tree
14220 cp_parser_mem_initializer_id (cp_parser* parser)
14222 bool global_scope_p;
14223 bool nested_name_specifier_p;
14224 bool template_p = false;
14225 tree id;
14227 cp_token *token = cp_lexer_peek_token (parser->lexer);
14229 /* `typename' is not allowed in this context ([temp.res]). */
14230 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14232 error_at (token->location,
14233 "keyword %<typename%> not allowed in this context (a qualified "
14234 "member initializer is implicitly a type)");
14235 cp_lexer_consume_token (parser->lexer);
14237 /* Look for the optional `::' operator. */
14238 global_scope_p
14239 = (cp_parser_global_scope_opt (parser,
14240 /*current_scope_valid_p=*/false)
14241 != NULL_TREE);
14242 /* Look for the optional nested-name-specifier. The simplest way to
14243 implement:
14245 [temp.res]
14247 The keyword `typename' is not permitted in a base-specifier or
14248 mem-initializer; in these contexts a qualified name that
14249 depends on a template-parameter is implicitly assumed to be a
14250 type name.
14252 is to assume that we have seen the `typename' keyword at this
14253 point. */
14254 nested_name_specifier_p
14255 = (cp_parser_nested_name_specifier_opt (parser,
14256 /*typename_keyword_p=*/true,
14257 /*check_dependency_p=*/true,
14258 /*type_p=*/true,
14259 /*is_declaration=*/true)
14260 != NULL_TREE);
14261 if (nested_name_specifier_p)
14262 template_p = cp_parser_optional_template_keyword (parser);
14263 /* If there is a `::' operator or a nested-name-specifier, then we
14264 are definitely looking for a class-name. */
14265 if (global_scope_p || nested_name_specifier_p)
14266 return cp_parser_class_name (parser,
14267 /*typename_keyword_p=*/true,
14268 /*template_keyword_p=*/template_p,
14269 typename_type,
14270 /*check_dependency_p=*/true,
14271 /*class_head_p=*/false,
14272 /*is_declaration=*/true);
14273 /* Otherwise, we could also be looking for an ordinary identifier. */
14274 cp_parser_parse_tentatively (parser);
14275 if (cp_lexer_next_token_is_decltype (parser->lexer))
14276 /* Try a decltype-specifier. */
14277 id = cp_parser_decltype (parser);
14278 else
14279 /* Otherwise, try a class-name. */
14280 id = cp_parser_class_name (parser,
14281 /*typename_keyword_p=*/true,
14282 /*template_keyword_p=*/false,
14283 none_type,
14284 /*check_dependency_p=*/true,
14285 /*class_head_p=*/false,
14286 /*is_declaration=*/true);
14287 /* If we found one, we're done. */
14288 if (cp_parser_parse_definitely (parser))
14289 return id;
14290 /* Otherwise, look for an ordinary identifier. */
14291 return cp_parser_identifier (parser);
14294 /* Overloading [gram.over] */
14296 /* Parse an operator-function-id.
14298 operator-function-id:
14299 operator operator
14301 Returns an IDENTIFIER_NODE for the operator which is a
14302 human-readable spelling of the identifier, e.g., `operator +'. */
14304 static cp_expr
14305 cp_parser_operator_function_id (cp_parser* parser)
14307 /* Look for the `operator' keyword. */
14308 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14309 return error_mark_node;
14310 /* And then the name of the operator itself. */
14311 return cp_parser_operator (parser);
14314 /* Return an identifier node for a user-defined literal operator.
14315 The suffix identifier is chained to the operator name identifier. */
14317 tree
14318 cp_literal_operator_id (const char* name)
14320 tree identifier;
14321 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14322 + strlen (name) + 10);
14323 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14324 identifier = get_identifier (buffer);
14326 return identifier;
14329 /* Parse an operator.
14331 operator:
14332 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14333 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14334 || ++ -- , ->* -> () []
14336 GNU Extensions:
14338 operator:
14339 <? >? <?= >?=
14341 Returns an IDENTIFIER_NODE for the operator which is a
14342 human-readable spelling of the identifier, e.g., `operator +'. */
14344 static cp_expr
14345 cp_parser_operator (cp_parser* parser)
14347 tree id = NULL_TREE;
14348 cp_token *token;
14349 bool utf8 = false;
14351 /* Peek at the next token. */
14352 token = cp_lexer_peek_token (parser->lexer);
14354 location_t start_loc = token->location;
14356 /* Figure out which operator we have. */
14357 switch (token->type)
14359 case CPP_KEYWORD:
14361 enum tree_code op;
14363 /* The keyword should be either `new' or `delete'. */
14364 if (token->keyword == RID_NEW)
14365 op = NEW_EXPR;
14366 else if (token->keyword == RID_DELETE)
14367 op = DELETE_EXPR;
14368 else
14369 break;
14371 /* Consume the `new' or `delete' token. */
14372 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14374 /* Peek at the next token. */
14375 token = cp_lexer_peek_token (parser->lexer);
14376 /* If it's a `[' token then this is the array variant of the
14377 operator. */
14378 if (token->type == CPP_OPEN_SQUARE)
14380 /* Consume the `[' token. */
14381 cp_lexer_consume_token (parser->lexer);
14382 /* Look for the `]' token. */
14383 if (cp_token *close_token
14384 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14385 end_loc = close_token->location;
14386 id = cp_operator_id (op == NEW_EXPR
14387 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14389 /* Otherwise, we have the non-array variant. */
14390 else
14391 id = cp_operator_id (op);
14393 location_t loc = make_location (start_loc, start_loc, end_loc);
14395 return cp_expr (id, loc);
14398 case CPP_PLUS:
14399 id = cp_operator_id (PLUS_EXPR);
14400 break;
14402 case CPP_MINUS:
14403 id = cp_operator_id (MINUS_EXPR);
14404 break;
14406 case CPP_MULT:
14407 id = cp_operator_id (MULT_EXPR);
14408 break;
14410 case CPP_DIV:
14411 id = cp_operator_id (TRUNC_DIV_EXPR);
14412 break;
14414 case CPP_MOD:
14415 id = cp_operator_id (TRUNC_MOD_EXPR);
14416 break;
14418 case CPP_XOR:
14419 id = cp_operator_id (BIT_XOR_EXPR);
14420 break;
14422 case CPP_AND:
14423 id = cp_operator_id (BIT_AND_EXPR);
14424 break;
14426 case CPP_OR:
14427 id = cp_operator_id (BIT_IOR_EXPR);
14428 break;
14430 case CPP_COMPL:
14431 id = cp_operator_id (BIT_NOT_EXPR);
14432 break;
14434 case CPP_NOT:
14435 id = cp_operator_id (TRUTH_NOT_EXPR);
14436 break;
14438 case CPP_EQ:
14439 id = cp_assignment_operator_id (NOP_EXPR);
14440 break;
14442 case CPP_LESS:
14443 id = cp_operator_id (LT_EXPR);
14444 break;
14446 case CPP_GREATER:
14447 id = cp_operator_id (GT_EXPR);
14448 break;
14450 case CPP_PLUS_EQ:
14451 id = cp_assignment_operator_id (PLUS_EXPR);
14452 break;
14454 case CPP_MINUS_EQ:
14455 id = cp_assignment_operator_id (MINUS_EXPR);
14456 break;
14458 case CPP_MULT_EQ:
14459 id = cp_assignment_operator_id (MULT_EXPR);
14460 break;
14462 case CPP_DIV_EQ:
14463 id = cp_assignment_operator_id (TRUNC_DIV_EXPR);
14464 break;
14466 case CPP_MOD_EQ:
14467 id = cp_assignment_operator_id (TRUNC_MOD_EXPR);
14468 break;
14470 case CPP_XOR_EQ:
14471 id = cp_assignment_operator_id (BIT_XOR_EXPR);
14472 break;
14474 case CPP_AND_EQ:
14475 id = cp_assignment_operator_id (BIT_AND_EXPR);
14476 break;
14478 case CPP_OR_EQ:
14479 id = cp_assignment_operator_id (BIT_IOR_EXPR);
14480 break;
14482 case CPP_LSHIFT:
14483 id = cp_operator_id (LSHIFT_EXPR);
14484 break;
14486 case CPP_RSHIFT:
14487 id = cp_operator_id (RSHIFT_EXPR);
14488 break;
14490 case CPP_LSHIFT_EQ:
14491 id = cp_assignment_operator_id (LSHIFT_EXPR);
14492 break;
14494 case CPP_RSHIFT_EQ:
14495 id = cp_assignment_operator_id (RSHIFT_EXPR);
14496 break;
14498 case CPP_EQ_EQ:
14499 id = cp_operator_id (EQ_EXPR);
14500 break;
14502 case CPP_NOT_EQ:
14503 id = cp_operator_id (NE_EXPR);
14504 break;
14506 case CPP_LESS_EQ:
14507 id = cp_operator_id (LE_EXPR);
14508 break;
14510 case CPP_GREATER_EQ:
14511 id = cp_operator_id (GE_EXPR);
14512 break;
14514 case CPP_AND_AND:
14515 id = cp_operator_id (TRUTH_ANDIF_EXPR);
14516 break;
14518 case CPP_OR_OR:
14519 id = cp_operator_id (TRUTH_ORIF_EXPR);
14520 break;
14522 case CPP_PLUS_PLUS:
14523 id = cp_operator_id (POSTINCREMENT_EXPR);
14524 break;
14526 case CPP_MINUS_MINUS:
14527 id = cp_operator_id (PREDECREMENT_EXPR);
14528 break;
14530 case CPP_COMMA:
14531 id = cp_operator_id (COMPOUND_EXPR);
14532 break;
14534 case CPP_DEREF_STAR:
14535 id = cp_operator_id (MEMBER_REF);
14536 break;
14538 case CPP_DEREF:
14539 id = cp_operator_id (COMPONENT_REF);
14540 break;
14542 case CPP_OPEN_PAREN:
14543 /* Consume the `('. */
14544 cp_lexer_consume_token (parser->lexer);
14545 /* Look for the matching `)'. */
14546 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14547 return cp_operator_id (CALL_EXPR);
14549 case CPP_OPEN_SQUARE:
14550 /* Consume the `['. */
14551 cp_lexer_consume_token (parser->lexer);
14552 /* Look for the matching `]'. */
14553 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14554 return cp_operator_id (ARRAY_REF);
14556 case CPP_UTF8STRING:
14557 case CPP_UTF8STRING_USERDEF:
14558 utf8 = true;
14559 /* FALLTHRU */
14560 case CPP_STRING:
14561 case CPP_WSTRING:
14562 case CPP_STRING16:
14563 case CPP_STRING32:
14564 case CPP_STRING_USERDEF:
14565 case CPP_WSTRING_USERDEF:
14566 case CPP_STRING16_USERDEF:
14567 case CPP_STRING32_USERDEF:
14569 tree str, string_tree;
14570 int sz, len;
14572 if (cxx_dialect == cxx98)
14573 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14575 /* Consume the string. */
14576 str = cp_parser_string_literal (parser, /*translate=*/true,
14577 /*wide_ok=*/true, /*lookup_udlit=*/false);
14578 if (str == error_mark_node)
14579 return error_mark_node;
14580 else if (TREE_CODE (str) == USERDEF_LITERAL)
14582 string_tree = USERDEF_LITERAL_VALUE (str);
14583 id = USERDEF_LITERAL_SUFFIX_ID (str);
14585 else
14587 string_tree = str;
14588 /* Look for the suffix identifier. */
14589 token = cp_lexer_peek_token (parser->lexer);
14590 if (token->type == CPP_NAME)
14591 id = cp_parser_identifier (parser);
14592 else if (token->type == CPP_KEYWORD)
14594 error ("unexpected keyword;"
14595 " remove space between quotes and suffix identifier");
14596 return error_mark_node;
14598 else
14600 error ("expected suffix identifier");
14601 return error_mark_node;
14604 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14605 (TREE_TYPE (TREE_TYPE (string_tree))));
14606 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14607 if (len != 0)
14609 error ("expected empty string after %<operator%> keyword");
14610 return error_mark_node;
14612 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14613 != char_type_node)
14615 error ("invalid encoding prefix in literal operator");
14616 return error_mark_node;
14618 if (id != error_mark_node)
14620 const char *name = IDENTIFIER_POINTER (id);
14621 id = cp_literal_operator_id (name);
14623 return id;
14626 default:
14627 /* Anything else is an error. */
14628 break;
14631 /* If we have selected an identifier, we need to consume the
14632 operator token. */
14633 if (id)
14634 cp_lexer_consume_token (parser->lexer);
14635 /* Otherwise, no valid operator name was present. */
14636 else
14638 cp_parser_error (parser, "expected operator");
14639 id = error_mark_node;
14642 return cp_expr (id, start_loc);
14645 /* Parse a template-declaration.
14647 template-declaration:
14648 export [opt] template < template-parameter-list > declaration
14650 If MEMBER_P is TRUE, this template-declaration occurs within a
14651 class-specifier.
14653 The grammar rule given by the standard isn't correct. What
14654 is really meant is:
14656 template-declaration:
14657 export [opt] template-parameter-list-seq
14658 decl-specifier-seq [opt] init-declarator [opt] ;
14659 export [opt] template-parameter-list-seq
14660 function-definition
14662 template-parameter-list-seq:
14663 template-parameter-list-seq [opt]
14664 template < template-parameter-list >
14666 Concept Extensions:
14668 template-parameter-list-seq:
14669 template < template-parameter-list > requires-clause [opt]
14671 requires-clause:
14672 requires logical-or-expression */
14674 static void
14675 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14677 /* Check for `export'. */
14678 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14680 /* Consume the `export' token. */
14681 cp_lexer_consume_token (parser->lexer);
14682 /* Warn that we do not support `export'. */
14683 warning (0, "keyword %<export%> not implemented, and will be ignored");
14686 cp_parser_template_declaration_after_export (parser, member_p);
14689 /* Parse a template-parameter-list.
14691 template-parameter-list:
14692 template-parameter
14693 template-parameter-list , template-parameter
14695 Returns a TREE_LIST. Each node represents a template parameter.
14696 The nodes are connected via their TREE_CHAINs. */
14698 static tree
14699 cp_parser_template_parameter_list (cp_parser* parser)
14701 tree parameter_list = NULL_TREE;
14703 begin_template_parm_list ();
14705 /* The loop below parses the template parms. We first need to know
14706 the total number of template parms to be able to compute proper
14707 canonical types of each dependent type. So after the loop, when
14708 we know the total number of template parms,
14709 end_template_parm_list computes the proper canonical types and
14710 fixes up the dependent types accordingly. */
14711 while (true)
14713 tree parameter;
14714 bool is_non_type;
14715 bool is_parameter_pack;
14716 location_t parm_loc;
14718 /* Parse the template-parameter. */
14719 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14720 parameter = cp_parser_template_parameter (parser,
14721 &is_non_type,
14722 &is_parameter_pack);
14723 /* Add it to the list. */
14724 if (parameter != error_mark_node)
14725 parameter_list = process_template_parm (parameter_list,
14726 parm_loc,
14727 parameter,
14728 is_non_type,
14729 is_parameter_pack);
14730 else
14732 tree err_parm = build_tree_list (parameter, parameter);
14733 parameter_list = chainon (parameter_list, err_parm);
14736 /* If the next token is not a `,', we're done. */
14737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14738 break;
14739 /* Otherwise, consume the `,' token. */
14740 cp_lexer_consume_token (parser->lexer);
14743 return end_template_parm_list (parameter_list);
14746 /* Parse a introduction-list.
14748 introduction-list:
14749 introduced-parameter
14750 introduction-list , introduced-parameter
14752 introduced-parameter:
14753 ...[opt] identifier
14755 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14756 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14757 WILDCARD_DECL will also have DECL_NAME set and token location in
14758 DECL_SOURCE_LOCATION. */
14760 static tree
14761 cp_parser_introduction_list (cp_parser *parser)
14763 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14765 while (true)
14767 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14768 if (is_pack)
14769 cp_lexer_consume_token (parser->lexer);
14771 /* Build placeholder. */
14772 tree parm = build_nt (WILDCARD_DECL);
14773 DECL_SOURCE_LOCATION (parm)
14774 = cp_lexer_peek_token (parser->lexer)->location;
14775 DECL_NAME (parm) = cp_parser_identifier (parser);
14776 WILDCARD_PACK_P (parm) = is_pack;
14777 vec_safe_push (introduction_vec, parm);
14779 /* If the next token is not a `,', we're done. */
14780 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14781 break;
14782 /* Otherwise, consume the `,' token. */
14783 cp_lexer_consume_token (parser->lexer);
14786 /* Convert the vec into a TREE_VEC. */
14787 tree introduction_list = make_tree_vec (introduction_vec->length ());
14788 unsigned int n;
14789 tree parm;
14790 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14791 TREE_VEC_ELT (introduction_list, n) = parm;
14793 release_tree_vector (introduction_vec);
14794 return introduction_list;
14797 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14798 is an abstract declarator. */
14800 static inline cp_declarator*
14801 get_id_declarator (cp_declarator *declarator)
14803 cp_declarator *d = declarator;
14804 while (d && d->kind != cdk_id)
14805 d = d->declarator;
14806 return d;
14809 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14810 is an abstract declarator. */
14812 static inline tree
14813 get_unqualified_id (cp_declarator *declarator)
14815 declarator = get_id_declarator (declarator);
14816 if (declarator)
14817 return declarator->u.id.unqualified_name;
14818 else
14819 return NULL_TREE;
14822 /* Returns true if DECL represents a constrained-parameter. */
14824 static inline bool
14825 is_constrained_parameter (tree decl)
14827 return (decl
14828 && TREE_CODE (decl) == TYPE_DECL
14829 && CONSTRAINED_PARM_CONCEPT (decl)
14830 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14833 /* Returns true if PARM declares a constrained-parameter. */
14835 static inline bool
14836 is_constrained_parameter (cp_parameter_declarator *parm)
14838 return is_constrained_parameter (parm->decl_specifiers.type);
14841 /* Check that the type parameter is only a declarator-id, and that its
14842 type is not cv-qualified. */
14844 bool
14845 cp_parser_check_constrained_type_parm (cp_parser *parser,
14846 cp_parameter_declarator *parm)
14848 if (!parm->declarator)
14849 return true;
14851 if (parm->declarator->kind != cdk_id)
14853 cp_parser_error (parser, "invalid constrained type parameter");
14854 return false;
14857 /* Don't allow cv-qualified type parameters. */
14858 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14859 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14861 cp_parser_error (parser, "cv-qualified type parameter");
14862 return false;
14865 return true;
14868 /* Finish parsing/processing a template type parameter and checking
14869 various restrictions. */
14871 static inline tree
14872 cp_parser_constrained_type_template_parm (cp_parser *parser,
14873 tree id,
14874 cp_parameter_declarator* parmdecl)
14876 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14877 return finish_template_type_parm (class_type_node, id);
14878 else
14879 return error_mark_node;
14882 static tree
14883 finish_constrained_template_template_parm (tree proto, tree id)
14885 /* FIXME: This should probably be copied, and we may need to adjust
14886 the template parameter depths. */
14887 tree saved_parms = current_template_parms;
14888 begin_template_parm_list ();
14889 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14890 end_template_parm_list ();
14892 tree parm = finish_template_template_parm (class_type_node, id);
14893 current_template_parms = saved_parms;
14895 return parm;
14898 /* Finish parsing/processing a template template parameter by borrowing
14899 the template parameter list from the prototype parameter. */
14901 static tree
14902 cp_parser_constrained_template_template_parm (cp_parser *parser,
14903 tree proto,
14904 tree id,
14905 cp_parameter_declarator *parmdecl)
14907 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14908 return error_mark_node;
14909 return finish_constrained_template_template_parm (proto, id);
14912 /* Create a new non-type template parameter from the given PARM
14913 declarator. */
14915 static tree
14916 constrained_non_type_template_parm (bool *is_non_type,
14917 cp_parameter_declarator *parm)
14919 *is_non_type = true;
14920 cp_declarator *decl = parm->declarator;
14921 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14922 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14923 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14926 /* Build a constrained template parameter based on the PARMDECL
14927 declarator. The type of PARMDECL is the constrained type, which
14928 refers to the prototype template parameter that ultimately
14929 specifies the type of the declared parameter. */
14931 static tree
14932 finish_constrained_parameter (cp_parser *parser,
14933 cp_parameter_declarator *parmdecl,
14934 bool *is_non_type,
14935 bool *is_parameter_pack)
14937 tree decl = parmdecl->decl_specifiers.type;
14938 tree id = get_unqualified_id (parmdecl->declarator);
14939 tree def = parmdecl->default_argument;
14940 tree proto = DECL_INITIAL (decl);
14942 /* A template parameter constrained by a variadic concept shall also
14943 be declared as a template parameter pack. */
14944 bool is_variadic = template_parameter_pack_p (proto);
14945 if (is_variadic && !*is_parameter_pack)
14946 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14948 /* Build the parameter. Return an error if the declarator was invalid. */
14949 tree parm;
14950 if (TREE_CODE (proto) == TYPE_DECL)
14951 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14952 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14953 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14954 parmdecl);
14955 else
14956 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14957 if (parm == error_mark_node)
14958 return error_mark_node;
14960 /* Finish the parameter decl and create a node attaching the
14961 default argument and constraint. */
14962 parm = build_tree_list (def, parm);
14963 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14965 return parm;
14968 /* Returns true if the parsed type actually represents the declaration
14969 of a type template-parameter. */
14971 static inline bool
14972 declares_constrained_type_template_parameter (tree type)
14974 return (is_constrained_parameter (type)
14975 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14979 /* Returns true if the parsed type actually represents the declaration of
14980 a template template-parameter. */
14982 static bool
14983 declares_constrained_template_template_parameter (tree type)
14985 return (is_constrained_parameter (type)
14986 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14989 /* Parse a default argument for a type template-parameter.
14990 Note that diagnostics are handled in cp_parser_template_parameter. */
14992 static tree
14993 cp_parser_default_type_template_argument (cp_parser *parser)
14995 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14997 /* Consume the `=' token. */
14998 cp_lexer_consume_token (parser->lexer);
15000 cp_token *token = cp_lexer_peek_token (parser->lexer);
15002 /* Parse the default-argument. */
15003 push_deferring_access_checks (dk_no_deferred);
15004 tree default_argument = cp_parser_type_id (parser);
15005 pop_deferring_access_checks ();
15007 if (flag_concepts && type_uses_auto (default_argument))
15009 error_at (token->location,
15010 "invalid use of %<auto%> in default template argument");
15011 return error_mark_node;
15014 return default_argument;
15017 /* Parse a default argument for a template template-parameter. */
15019 static tree
15020 cp_parser_default_template_template_argument (cp_parser *parser)
15022 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15024 bool is_template;
15026 /* Consume the `='. */
15027 cp_lexer_consume_token (parser->lexer);
15028 /* Parse the id-expression. */
15029 push_deferring_access_checks (dk_no_deferred);
15030 /* save token before parsing the id-expression, for error
15031 reporting */
15032 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15033 tree default_argument
15034 = cp_parser_id_expression (parser,
15035 /*template_keyword_p=*/false,
15036 /*check_dependency_p=*/true,
15037 /*template_p=*/&is_template,
15038 /*declarator_p=*/false,
15039 /*optional_p=*/false);
15040 if (TREE_CODE (default_argument) == TYPE_DECL)
15041 /* If the id-expression was a template-id that refers to
15042 a template-class, we already have the declaration here,
15043 so no further lookup is needed. */
15045 else
15046 /* Look up the name. */
15047 default_argument
15048 = cp_parser_lookup_name (parser, default_argument,
15049 none_type,
15050 /*is_template=*/is_template,
15051 /*is_namespace=*/false,
15052 /*check_dependency=*/true,
15053 /*ambiguous_decls=*/NULL,
15054 token->location);
15055 /* See if the default argument is valid. */
15056 default_argument = check_template_template_default_arg (default_argument);
15057 pop_deferring_access_checks ();
15058 return default_argument;
15061 /* Parse a template-parameter.
15063 template-parameter:
15064 type-parameter
15065 parameter-declaration
15067 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15068 the parameter. The TREE_PURPOSE is the default value, if any.
15069 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15070 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15071 set to true iff this parameter is a parameter pack. */
15073 static tree
15074 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15075 bool *is_parameter_pack)
15077 cp_token *token;
15078 cp_parameter_declarator *parameter_declarator;
15079 tree parm;
15081 /* Assume it is a type parameter or a template parameter. */
15082 *is_non_type = false;
15083 /* Assume it not a parameter pack. */
15084 *is_parameter_pack = false;
15085 /* Peek at the next token. */
15086 token = cp_lexer_peek_token (parser->lexer);
15087 /* If it is `class' or `template', we have a type-parameter. */
15088 if (token->keyword == RID_TEMPLATE)
15089 return cp_parser_type_parameter (parser, is_parameter_pack);
15090 /* If it is `class' or `typename' we do not know yet whether it is a
15091 type parameter or a non-type parameter. Consider:
15093 template <typename T, typename T::X X> ...
15097 template <class C, class D*> ...
15099 Here, the first parameter is a type parameter, and the second is
15100 a non-type parameter. We can tell by looking at the token after
15101 the identifier -- if it is a `,', `=', or `>' then we have a type
15102 parameter. */
15103 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15105 /* Peek at the token after `class' or `typename'. */
15106 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15107 /* If it's an ellipsis, we have a template type parameter
15108 pack. */
15109 if (token->type == CPP_ELLIPSIS)
15110 return cp_parser_type_parameter (parser, is_parameter_pack);
15111 /* If it's an identifier, skip it. */
15112 if (token->type == CPP_NAME)
15113 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15114 /* Now, see if the token looks like the end of a template
15115 parameter. */
15116 if (token->type == CPP_COMMA
15117 || token->type == CPP_EQ
15118 || token->type == CPP_GREATER)
15119 return cp_parser_type_parameter (parser, is_parameter_pack);
15122 /* Otherwise, it is a non-type parameter or a constrained parameter.
15124 [temp.param]
15126 When parsing a default template-argument for a non-type
15127 template-parameter, the first non-nested `>' is taken as the end
15128 of the template parameter-list rather than a greater-than
15129 operator. */
15130 parameter_declarator
15131 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15132 /*parenthesized_p=*/NULL);
15134 if (!parameter_declarator)
15135 return error_mark_node;
15137 /* If the parameter declaration is marked as a parameter pack, set
15138 *IS_PARAMETER_PACK to notify the caller. */
15139 if (parameter_declarator->template_parameter_pack_p)
15140 *is_parameter_pack = true;
15142 if (parameter_declarator->default_argument)
15144 /* Can happen in some cases of erroneous input (c++/34892). */
15145 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15146 /* Consume the `...' for better error recovery. */
15147 cp_lexer_consume_token (parser->lexer);
15150 // The parameter may have been constrained.
15151 if (is_constrained_parameter (parameter_declarator))
15152 return finish_constrained_parameter (parser,
15153 parameter_declarator,
15154 is_non_type,
15155 is_parameter_pack);
15157 // Now we're sure that the parameter is a non-type parameter.
15158 *is_non_type = true;
15160 parm = grokdeclarator (parameter_declarator->declarator,
15161 &parameter_declarator->decl_specifiers,
15162 TPARM, /*initialized=*/0,
15163 /*attrlist=*/NULL);
15164 if (parm == error_mark_node)
15165 return error_mark_node;
15167 return build_tree_list (parameter_declarator->default_argument, parm);
15170 /* Parse a type-parameter.
15172 type-parameter:
15173 class identifier [opt]
15174 class identifier [opt] = type-id
15175 typename identifier [opt]
15176 typename identifier [opt] = type-id
15177 template < template-parameter-list > class identifier [opt]
15178 template < template-parameter-list > class identifier [opt]
15179 = id-expression
15181 GNU Extension (variadic templates):
15183 type-parameter:
15184 class ... identifier [opt]
15185 typename ... identifier [opt]
15187 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15188 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15189 the declaration of the parameter.
15191 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15193 static tree
15194 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15196 cp_token *token;
15197 tree parameter;
15199 /* Look for a keyword to tell us what kind of parameter this is. */
15200 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15201 if (!token)
15202 return error_mark_node;
15204 switch (token->keyword)
15206 case RID_CLASS:
15207 case RID_TYPENAME:
15209 tree identifier;
15210 tree default_argument;
15212 /* If the next token is an ellipsis, we have a template
15213 argument pack. */
15214 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15216 /* Consume the `...' token. */
15217 cp_lexer_consume_token (parser->lexer);
15218 maybe_warn_variadic_templates ();
15220 *is_parameter_pack = true;
15223 /* If the next token is an identifier, then it names the
15224 parameter. */
15225 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15226 identifier = cp_parser_identifier (parser);
15227 else
15228 identifier = NULL_TREE;
15230 /* Create the parameter. */
15231 parameter = finish_template_type_parm (class_type_node, identifier);
15233 /* If the next token is an `=', we have a default argument. */
15234 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15236 default_argument
15237 = cp_parser_default_type_template_argument (parser);
15239 /* Template parameter packs cannot have default
15240 arguments. */
15241 if (*is_parameter_pack)
15243 if (identifier)
15244 error_at (token->location,
15245 "template parameter pack %qD cannot have a "
15246 "default argument", identifier);
15247 else
15248 error_at (token->location,
15249 "template parameter packs cannot have "
15250 "default arguments");
15251 default_argument = NULL_TREE;
15253 else if (check_for_bare_parameter_packs (default_argument))
15254 default_argument = error_mark_node;
15256 else
15257 default_argument = NULL_TREE;
15259 /* Create the combined representation of the parameter and the
15260 default argument. */
15261 parameter = build_tree_list (default_argument, parameter);
15263 break;
15265 case RID_TEMPLATE:
15267 tree identifier;
15268 tree default_argument;
15270 /* Look for the `<'. */
15271 cp_parser_require (parser, CPP_LESS, RT_LESS);
15272 /* Parse the template-parameter-list. */
15273 cp_parser_template_parameter_list (parser);
15274 /* Look for the `>'. */
15275 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15277 // If template requirements are present, parse them.
15278 if (flag_concepts)
15280 tree reqs = get_shorthand_constraints (current_template_parms);
15281 if (tree r = cp_parser_requires_clause_opt (parser))
15282 reqs = conjoin_constraints (reqs, normalize_expression (r));
15283 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15286 /* Look for the `class' or 'typename' keywords. */
15287 cp_parser_type_parameter_key (parser);
15288 /* If the next token is an ellipsis, we have a template
15289 argument pack. */
15290 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15292 /* Consume the `...' token. */
15293 cp_lexer_consume_token (parser->lexer);
15294 maybe_warn_variadic_templates ();
15296 *is_parameter_pack = true;
15298 /* If the next token is an `=', then there is a
15299 default-argument. If the next token is a `>', we are at
15300 the end of the parameter-list. If the next token is a `,',
15301 then we are at the end of this parameter. */
15302 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15303 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15304 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15306 identifier = cp_parser_identifier (parser);
15307 /* Treat invalid names as if the parameter were nameless. */
15308 if (identifier == error_mark_node)
15309 identifier = NULL_TREE;
15311 else
15312 identifier = NULL_TREE;
15314 /* Create the template parameter. */
15315 parameter = finish_template_template_parm (class_type_node,
15316 identifier);
15318 /* If the next token is an `=', then there is a
15319 default-argument. */
15320 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15322 default_argument
15323 = cp_parser_default_template_template_argument (parser);
15325 /* Template parameter packs cannot have default
15326 arguments. */
15327 if (*is_parameter_pack)
15329 if (identifier)
15330 error_at (token->location,
15331 "template parameter pack %qD cannot "
15332 "have a default argument",
15333 identifier);
15334 else
15335 error_at (token->location, "template parameter packs cannot "
15336 "have default arguments");
15337 default_argument = NULL_TREE;
15340 else
15341 default_argument = NULL_TREE;
15343 /* Create the combined representation of the parameter and the
15344 default argument. */
15345 parameter = build_tree_list (default_argument, parameter);
15347 break;
15349 default:
15350 gcc_unreachable ();
15351 break;
15354 return parameter;
15357 /* Parse a template-id.
15359 template-id:
15360 template-name < template-argument-list [opt] >
15362 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15363 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15364 returned. Otherwise, if the template-name names a function, or set
15365 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15366 names a class, returns a TYPE_DECL for the specialization.
15368 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15369 uninstantiated templates. */
15371 static tree
15372 cp_parser_template_id (cp_parser *parser,
15373 bool template_keyword_p,
15374 bool check_dependency_p,
15375 enum tag_types tag_type,
15376 bool is_declaration)
15378 tree templ;
15379 tree arguments;
15380 tree template_id;
15381 cp_token_position start_of_id = 0;
15382 cp_token *next_token = NULL, *next_token_2 = NULL;
15383 bool is_identifier;
15385 /* If the next token corresponds to a template-id, there is no need
15386 to reparse it. */
15387 next_token = cp_lexer_peek_token (parser->lexer);
15388 if (next_token->type == CPP_TEMPLATE_ID)
15390 cp_lexer_consume_token (parser->lexer);
15391 return saved_checks_value (next_token->u.tree_check_value);
15394 /* Avoid performing name lookup if there is no possibility of
15395 finding a template-id. */
15396 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
15397 || (next_token->type == CPP_NAME
15398 && !cp_parser_nth_token_starts_template_argument_list_p
15399 (parser, 2)))
15401 cp_parser_error (parser, "expected template-id");
15402 return error_mark_node;
15405 /* Remember where the template-id starts. */
15406 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15407 start_of_id = cp_lexer_token_position (parser->lexer, false);
15409 push_deferring_access_checks (dk_deferred);
15411 /* Parse the template-name. */
15412 is_identifier = false;
15413 templ = cp_parser_template_name (parser, template_keyword_p,
15414 check_dependency_p,
15415 is_declaration,
15416 tag_type,
15417 &is_identifier);
15418 if (templ == error_mark_node || is_identifier)
15420 pop_deferring_access_checks ();
15421 return templ;
15424 /* Since we're going to preserve any side-effects from this parse, set up a
15425 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15426 in the template arguments. */
15427 tentative_firewall firewall (parser);
15429 /* If we find the sequence `[:' after a template-name, it's probably
15430 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15431 parse correctly the argument list. */
15432 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15433 == CPP_OPEN_SQUARE)
15434 && next_token->flags & DIGRAPH
15435 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15436 == CPP_COLON)
15437 && !(next_token_2->flags & PREV_WHITE))
15439 cp_parser_parse_tentatively (parser);
15440 /* Change `:' into `::'. */
15441 next_token_2->type = CPP_SCOPE;
15442 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15443 CPP_LESS. */
15444 cp_lexer_consume_token (parser->lexer);
15446 /* Parse the arguments. */
15447 arguments = cp_parser_enclosed_template_argument_list (parser);
15448 if (!cp_parser_parse_definitely (parser))
15450 /* If we couldn't parse an argument list, then we revert our changes
15451 and return simply an error. Maybe this is not a template-id
15452 after all. */
15453 next_token_2->type = CPP_COLON;
15454 cp_parser_error (parser, "expected %<<%>");
15455 pop_deferring_access_checks ();
15456 return error_mark_node;
15458 /* Otherwise, emit an error about the invalid digraph, but continue
15459 parsing because we got our argument list. */
15460 if (permerror (next_token->location,
15461 "%<<::%> cannot begin a template-argument list"))
15463 static bool hint = false;
15464 inform (next_token->location,
15465 "%<<:%> is an alternate spelling for %<[%>."
15466 " Insert whitespace between %<<%> and %<::%>");
15467 if (!hint && !flag_permissive)
15469 inform (next_token->location, "(if you use %<-fpermissive%> "
15470 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15471 "accept your code)");
15472 hint = true;
15476 else
15478 /* Look for the `<' that starts the template-argument-list. */
15479 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15481 pop_deferring_access_checks ();
15482 return error_mark_node;
15484 /* Parse the arguments. */
15485 arguments = cp_parser_enclosed_template_argument_list (parser);
15488 /* Build a representation of the specialization. */
15489 if (identifier_p (templ))
15490 template_id = build_min_nt_loc (next_token->location,
15491 TEMPLATE_ID_EXPR,
15492 templ, arguments);
15493 else if (DECL_TYPE_TEMPLATE_P (templ)
15494 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15496 bool entering_scope;
15497 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15498 template (rather than some instantiation thereof) only if
15499 is not nested within some other construct. For example, in
15500 "template <typename T> void f(T) { A<T>::", A<T> is just an
15501 instantiation of A. */
15502 entering_scope = (template_parm_scope_p ()
15503 && cp_lexer_next_token_is (parser->lexer,
15504 CPP_SCOPE));
15505 template_id
15506 = finish_template_type (templ, arguments, entering_scope);
15508 /* A template-like identifier may be a partial concept id. */
15509 else if (flag_concepts
15510 && (template_id = (cp_parser_maybe_partial_concept_id
15511 (parser, templ, arguments))))
15512 return template_id;
15513 else if (variable_template_p (templ))
15515 template_id = lookup_template_variable (templ, arguments);
15516 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15517 SET_EXPR_LOCATION (template_id, next_token->location);
15519 else
15521 /* If it's not a class-template or a template-template, it should be
15522 a function-template. */
15523 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15524 || TREE_CODE (templ) == OVERLOAD
15525 || BASELINK_P (templ)));
15527 template_id = lookup_template_function (templ, arguments);
15528 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15529 SET_EXPR_LOCATION (template_id, next_token->location);
15532 /* If parsing tentatively, replace the sequence of tokens that makes
15533 up the template-id with a CPP_TEMPLATE_ID token. That way,
15534 should we re-parse the token stream, we will not have to repeat
15535 the effort required to do the parse, nor will we issue duplicate
15536 error messages about problems during instantiation of the
15537 template. */
15538 if (start_of_id
15539 /* Don't do this if we had a parse error in a declarator; re-parsing
15540 might succeed if a name changes meaning (60361). */
15541 && !(cp_parser_error_occurred (parser)
15542 && cp_parser_parsing_tentatively (parser)
15543 && parser->in_declarator_p))
15545 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
15547 /* Reset the contents of the START_OF_ID token. */
15548 token->type = CPP_TEMPLATE_ID;
15550 /* Update the location to be of the form:
15551 template-name < template-argument-list [opt] >
15552 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15553 with caret == start at the start of the template-name,
15554 ranging until the closing '>'. */
15555 location_t finish_loc
15556 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15557 location_t combined_loc
15558 = make_location (token->location, token->location, finish_loc);
15559 token->location = combined_loc;
15561 /* Retrieve any deferred checks. Do not pop this access checks yet
15562 so the memory will not be reclaimed during token replacing below. */
15563 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15564 token->u.tree_check_value->value = template_id;
15565 token->u.tree_check_value->checks = get_deferred_access_checks ();
15566 token->keyword = RID_MAX;
15568 /* Purge all subsequent tokens. */
15569 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15571 /* ??? Can we actually assume that, if template_id ==
15572 error_mark_node, we will have issued a diagnostic to the
15573 user, as opposed to simply marking the tentative parse as
15574 failed? */
15575 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15576 error_at (token->location, "parse error in template argument list");
15579 pop_to_parent_deferring_access_checks ();
15580 return template_id;
15583 /* Parse a template-name.
15585 template-name:
15586 identifier
15588 The standard should actually say:
15590 template-name:
15591 identifier
15592 operator-function-id
15594 A defect report has been filed about this issue.
15596 A conversion-function-id cannot be a template name because they cannot
15597 be part of a template-id. In fact, looking at this code:
15599 a.operator K<int>()
15601 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15602 It is impossible to call a templated conversion-function-id with an
15603 explicit argument list, since the only allowed template parameter is
15604 the type to which it is converting.
15606 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15607 `template' keyword, in a construction like:
15609 T::template f<3>()
15611 In that case `f' is taken to be a template-name, even though there
15612 is no way of knowing for sure.
15614 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15615 name refers to a set of overloaded functions, at least one of which
15616 is a template, or an IDENTIFIER_NODE with the name of the template,
15617 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15618 names are looked up inside uninstantiated templates. */
15620 static tree
15621 cp_parser_template_name (cp_parser* parser,
15622 bool template_keyword_p,
15623 bool check_dependency_p,
15624 bool is_declaration,
15625 enum tag_types tag_type,
15626 bool *is_identifier)
15628 tree identifier;
15629 tree decl;
15630 tree fns;
15631 cp_token *token = cp_lexer_peek_token (parser->lexer);
15633 /* If the next token is `operator', then we have either an
15634 operator-function-id or a conversion-function-id. */
15635 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15637 /* We don't know whether we're looking at an
15638 operator-function-id or a conversion-function-id. */
15639 cp_parser_parse_tentatively (parser);
15640 /* Try an operator-function-id. */
15641 identifier = cp_parser_operator_function_id (parser);
15642 /* If that didn't work, try a conversion-function-id. */
15643 if (!cp_parser_parse_definitely (parser))
15645 cp_parser_error (parser, "expected template-name");
15646 return error_mark_node;
15649 /* Look for the identifier. */
15650 else
15651 identifier = cp_parser_identifier (parser);
15653 /* If we didn't find an identifier, we don't have a template-id. */
15654 if (identifier == error_mark_node)
15655 return error_mark_node;
15657 /* If the name immediately followed the `template' keyword, then it
15658 is a template-name. However, if the next token is not `<', then
15659 we do not treat it as a template-name, since it is not being used
15660 as part of a template-id. This enables us to handle constructs
15661 like:
15663 template <typename T> struct S { S(); };
15664 template <typename T> S<T>::S();
15666 correctly. We would treat `S' as a template -- if it were `S<T>'
15667 -- but we do not if there is no `<'. */
15669 if (processing_template_decl
15670 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15672 /* In a declaration, in a dependent context, we pretend that the
15673 "template" keyword was present in order to improve error
15674 recovery. For example, given:
15676 template <typename T> void f(T::X<int>);
15678 we want to treat "X<int>" as a template-id. */
15679 if (is_declaration
15680 && !template_keyword_p
15681 && parser->scope && TYPE_P (parser->scope)
15682 && check_dependency_p
15683 && dependent_scope_p (parser->scope)
15684 /* Do not do this for dtors (or ctors), since they never
15685 need the template keyword before their name. */
15686 && !constructor_name_p (identifier, parser->scope))
15688 cp_token_position start = 0;
15690 /* Explain what went wrong. */
15691 error_at (token->location, "non-template %qD used as template",
15692 identifier);
15693 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15694 parser->scope, identifier);
15695 /* If parsing tentatively, find the location of the "<" token. */
15696 if (cp_parser_simulate_error (parser))
15697 start = cp_lexer_token_position (parser->lexer, true);
15698 /* Parse the template arguments so that we can issue error
15699 messages about them. */
15700 cp_lexer_consume_token (parser->lexer);
15701 cp_parser_enclosed_template_argument_list (parser);
15702 /* Skip tokens until we find a good place from which to
15703 continue parsing. */
15704 cp_parser_skip_to_closing_parenthesis (parser,
15705 /*recovering=*/true,
15706 /*or_comma=*/true,
15707 /*consume_paren=*/false);
15708 /* If parsing tentatively, permanently remove the
15709 template argument list. That will prevent duplicate
15710 error messages from being issued about the missing
15711 "template" keyword. */
15712 if (start)
15713 cp_lexer_purge_tokens_after (parser->lexer, start);
15714 if (is_identifier)
15715 *is_identifier = true;
15716 parser->context->object_type = NULL_TREE;
15717 return identifier;
15720 /* If the "template" keyword is present, then there is generally
15721 no point in doing name-lookup, so we just return IDENTIFIER.
15722 But, if the qualifying scope is non-dependent then we can
15723 (and must) do name-lookup normally. */
15724 if (template_keyword_p
15725 && (!parser->scope
15726 || (TYPE_P (parser->scope)
15727 && dependent_type_p (parser->scope))))
15729 /* We're optimizing away the call to cp_parser_lookup_name, but we
15730 still need to do this. */
15731 parser->context->object_type = NULL_TREE;
15732 return identifier;
15736 /* Look up the name. */
15737 decl = cp_parser_lookup_name (parser, identifier,
15738 tag_type,
15739 /*is_template=*/true,
15740 /*is_namespace=*/false,
15741 check_dependency_p,
15742 /*ambiguous_decls=*/NULL,
15743 token->location);
15745 decl = strip_using_decl (decl);
15747 /* If DECL is a template, then the name was a template-name. */
15748 if (TREE_CODE (decl) == TEMPLATE_DECL)
15750 if (TREE_DEPRECATED (decl)
15751 && deprecated_state != DEPRECATED_SUPPRESS)
15752 warn_deprecated_use (decl, NULL_TREE);
15754 else
15756 tree fn = NULL_TREE;
15758 /* The standard does not explicitly indicate whether a name that
15759 names a set of overloaded declarations, some of which are
15760 templates, is a template-name. However, such a name should
15761 be a template-name; otherwise, there is no way to form a
15762 template-id for the overloaded templates. */
15763 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15764 if (TREE_CODE (fns) == OVERLOAD)
15765 for (fn = fns; fn; fn = OVL_NEXT (fn))
15766 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15767 break;
15769 if (!fn)
15771 /* The name does not name a template. */
15772 cp_parser_error (parser, "expected template-name");
15773 return error_mark_node;
15777 /* If DECL is dependent, and refers to a function, then just return
15778 its name; we will look it up again during template instantiation. */
15779 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15781 tree scope = ovl_scope (decl);
15782 if (TYPE_P (scope) && dependent_type_p (scope))
15783 return identifier;
15786 return decl;
15789 /* Parse a template-argument-list.
15791 template-argument-list:
15792 template-argument ... [opt]
15793 template-argument-list , template-argument ... [opt]
15795 Returns a TREE_VEC containing the arguments. */
15797 static tree
15798 cp_parser_template_argument_list (cp_parser* parser)
15800 tree fixed_args[10];
15801 unsigned n_args = 0;
15802 unsigned alloced = 10;
15803 tree *arg_ary = fixed_args;
15804 tree vec;
15805 bool saved_in_template_argument_list_p;
15806 bool saved_ice_p;
15807 bool saved_non_ice_p;
15809 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15810 parser->in_template_argument_list_p = true;
15811 /* Even if the template-id appears in an integral
15812 constant-expression, the contents of the argument list do
15813 not. */
15814 saved_ice_p = parser->integral_constant_expression_p;
15815 parser->integral_constant_expression_p = false;
15816 saved_non_ice_p = parser->non_integral_constant_expression_p;
15817 parser->non_integral_constant_expression_p = false;
15819 /* Parse the arguments. */
15822 tree argument;
15824 if (n_args)
15825 /* Consume the comma. */
15826 cp_lexer_consume_token (parser->lexer);
15828 /* Parse the template-argument. */
15829 argument = cp_parser_template_argument (parser);
15831 /* If the next token is an ellipsis, we're expanding a template
15832 argument pack. */
15833 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15835 if (argument == error_mark_node)
15837 cp_token *token = cp_lexer_peek_token (parser->lexer);
15838 error_at (token->location,
15839 "expected parameter pack before %<...%>");
15841 /* Consume the `...' token. */
15842 cp_lexer_consume_token (parser->lexer);
15844 /* Make the argument into a TYPE_PACK_EXPANSION or
15845 EXPR_PACK_EXPANSION. */
15846 argument = make_pack_expansion (argument);
15849 if (n_args == alloced)
15851 alloced *= 2;
15853 if (arg_ary == fixed_args)
15855 arg_ary = XNEWVEC (tree, alloced);
15856 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15858 else
15859 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15861 arg_ary[n_args++] = argument;
15863 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15865 vec = make_tree_vec (n_args);
15867 while (n_args--)
15868 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15870 if (arg_ary != fixed_args)
15871 free (arg_ary);
15872 parser->non_integral_constant_expression_p = saved_non_ice_p;
15873 parser->integral_constant_expression_p = saved_ice_p;
15874 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15875 if (CHECKING_P)
15876 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15877 return vec;
15880 /* Parse a template-argument.
15882 template-argument:
15883 assignment-expression
15884 type-id
15885 id-expression
15887 The representation is that of an assignment-expression, type-id, or
15888 id-expression -- except that the qualified id-expression is
15889 evaluated, so that the value returned is either a DECL or an
15890 OVERLOAD.
15892 Although the standard says "assignment-expression", it forbids
15893 throw-expressions or assignments in the template argument.
15894 Therefore, we use "conditional-expression" instead. */
15896 static tree
15897 cp_parser_template_argument (cp_parser* parser)
15899 tree argument;
15900 bool template_p;
15901 bool address_p;
15902 bool maybe_type_id = false;
15903 cp_token *token = NULL, *argument_start_token = NULL;
15904 location_t loc = 0;
15905 cp_id_kind idk;
15907 /* There's really no way to know what we're looking at, so we just
15908 try each alternative in order.
15910 [temp.arg]
15912 In a template-argument, an ambiguity between a type-id and an
15913 expression is resolved to a type-id, regardless of the form of
15914 the corresponding template-parameter.
15916 Therefore, we try a type-id first. */
15917 cp_parser_parse_tentatively (parser);
15918 argument = cp_parser_template_type_arg (parser);
15919 /* If there was no error parsing the type-id but the next token is a
15920 '>>', our behavior depends on which dialect of C++ we're
15921 parsing. In C++98, we probably found a typo for '> >'. But there
15922 are type-id which are also valid expressions. For instance:
15924 struct X { int operator >> (int); };
15925 template <int V> struct Foo {};
15926 Foo<X () >> 5> r;
15928 Here 'X()' is a valid type-id of a function type, but the user just
15929 wanted to write the expression "X() >> 5". Thus, we remember that we
15930 found a valid type-id, but we still try to parse the argument as an
15931 expression to see what happens.
15933 In C++0x, the '>>' will be considered two separate '>'
15934 tokens. */
15935 if (!cp_parser_error_occurred (parser)
15936 && cxx_dialect == cxx98
15937 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15939 maybe_type_id = true;
15940 cp_parser_abort_tentative_parse (parser);
15942 else
15944 /* If the next token isn't a `,' or a `>', then this argument wasn't
15945 really finished. This means that the argument is not a valid
15946 type-id. */
15947 if (!cp_parser_next_token_ends_template_argument_p (parser))
15948 cp_parser_error (parser, "expected template-argument");
15949 /* If that worked, we're done. */
15950 if (cp_parser_parse_definitely (parser))
15951 return argument;
15953 /* We're still not sure what the argument will be. */
15954 cp_parser_parse_tentatively (parser);
15955 /* Try a template. */
15956 argument_start_token = cp_lexer_peek_token (parser->lexer);
15957 argument = cp_parser_id_expression (parser,
15958 /*template_keyword_p=*/false,
15959 /*check_dependency_p=*/true,
15960 &template_p,
15961 /*declarator_p=*/false,
15962 /*optional_p=*/false);
15963 /* If the next token isn't a `,' or a `>', then this argument wasn't
15964 really finished. */
15965 if (!cp_parser_next_token_ends_template_argument_p (parser))
15966 cp_parser_error (parser, "expected template-argument");
15967 if (!cp_parser_error_occurred (parser))
15969 /* Figure out what is being referred to. If the id-expression
15970 was for a class template specialization, then we will have a
15971 TYPE_DECL at this point. There is no need to do name lookup
15972 at this point in that case. */
15973 if (TREE_CODE (argument) != TYPE_DECL)
15974 argument = cp_parser_lookup_name (parser, argument,
15975 none_type,
15976 /*is_template=*/template_p,
15977 /*is_namespace=*/false,
15978 /*check_dependency=*/true,
15979 /*ambiguous_decls=*/NULL,
15980 argument_start_token->location);
15981 /* Handle a constrained-type-specifier for a non-type template
15982 parameter. */
15983 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15984 argument = decl;
15985 else if (TREE_CODE (argument) != TEMPLATE_DECL
15986 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15987 cp_parser_error (parser, "expected template-name");
15989 if (cp_parser_parse_definitely (parser))
15991 if (TREE_DEPRECATED (argument))
15992 warn_deprecated_use (argument, NULL_TREE);
15993 return argument;
15995 /* It must be a non-type argument. In C++17 any constant-expression is
15996 allowed. */
15997 if (cxx_dialect > cxx14)
15998 goto general_expr;
16000 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16002 -- an integral constant-expression of integral or enumeration
16003 type; or
16005 -- the name of a non-type template-parameter; or
16007 -- the name of an object or function with external linkage...
16009 -- the address of an object or function with external linkage...
16011 -- a pointer to member... */
16012 /* Look for a non-type template parameter. */
16013 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16015 cp_parser_parse_tentatively (parser);
16016 argument = cp_parser_primary_expression (parser,
16017 /*address_p=*/false,
16018 /*cast_p=*/false,
16019 /*template_arg_p=*/true,
16020 &idk);
16021 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16022 || !cp_parser_next_token_ends_template_argument_p (parser))
16023 cp_parser_simulate_error (parser);
16024 if (cp_parser_parse_definitely (parser))
16025 return argument;
16028 /* If the next token is "&", the argument must be the address of an
16029 object or function with external linkage. */
16030 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16031 if (address_p)
16033 loc = cp_lexer_peek_token (parser->lexer)->location;
16034 cp_lexer_consume_token (parser->lexer);
16036 /* See if we might have an id-expression. */
16037 token = cp_lexer_peek_token (parser->lexer);
16038 if (token->type == CPP_NAME
16039 || token->keyword == RID_OPERATOR
16040 || token->type == CPP_SCOPE
16041 || token->type == CPP_TEMPLATE_ID
16042 || token->type == CPP_NESTED_NAME_SPECIFIER)
16044 cp_parser_parse_tentatively (parser);
16045 argument = cp_parser_primary_expression (parser,
16046 address_p,
16047 /*cast_p=*/false,
16048 /*template_arg_p=*/true,
16049 &idk);
16050 if (cp_parser_error_occurred (parser)
16051 || !cp_parser_next_token_ends_template_argument_p (parser))
16052 cp_parser_abort_tentative_parse (parser);
16053 else
16055 tree probe;
16057 if (INDIRECT_REF_P (argument))
16059 /* Strip the dereference temporarily. */
16060 gcc_assert (REFERENCE_REF_P (argument));
16061 argument = TREE_OPERAND (argument, 0);
16064 /* If we're in a template, we represent a qualified-id referring
16065 to a static data member as a SCOPE_REF even if the scope isn't
16066 dependent so that we can check access control later. */
16067 probe = argument;
16068 if (TREE_CODE (probe) == SCOPE_REF)
16069 probe = TREE_OPERAND (probe, 1);
16070 if (VAR_P (probe))
16072 /* A variable without external linkage might still be a
16073 valid constant-expression, so no error is issued here
16074 if the external-linkage check fails. */
16075 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16076 cp_parser_simulate_error (parser);
16078 else if (is_overloaded_fn (argument))
16079 /* All overloaded functions are allowed; if the external
16080 linkage test does not pass, an error will be issued
16081 later. */
16083 else if (address_p
16084 && (TREE_CODE (argument) == OFFSET_REF
16085 || TREE_CODE (argument) == SCOPE_REF))
16086 /* A pointer-to-member. */
16088 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16090 else
16091 cp_parser_simulate_error (parser);
16093 if (cp_parser_parse_definitely (parser))
16095 if (address_p)
16096 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16097 tf_warning_or_error);
16098 else
16099 argument = convert_from_reference (argument);
16100 return argument;
16104 /* If the argument started with "&", there are no other valid
16105 alternatives at this point. */
16106 if (address_p)
16108 cp_parser_error (parser, "invalid non-type template argument");
16109 return error_mark_node;
16112 general_expr:
16113 /* If the argument wasn't successfully parsed as a type-id followed
16114 by '>>', the argument can only be a constant expression now.
16115 Otherwise, we try parsing the constant-expression tentatively,
16116 because the argument could really be a type-id. */
16117 if (maybe_type_id)
16118 cp_parser_parse_tentatively (parser);
16120 if (cxx_dialect <= cxx14)
16121 argument = cp_parser_constant_expression (parser);
16122 else
16124 /* With C++17 generalized non-type template arguments we need to handle
16125 lvalue constant expressions, too. */
16126 argument = cp_parser_assignment_expression (parser);
16127 require_potential_constant_expression (argument);
16130 if (!maybe_type_id)
16131 return argument;
16132 if (!cp_parser_next_token_ends_template_argument_p (parser))
16133 cp_parser_error (parser, "expected template-argument");
16134 if (cp_parser_parse_definitely (parser))
16135 return argument;
16136 /* We did our best to parse the argument as a non type-id, but that
16137 was the only alternative that matched (albeit with a '>' after
16138 it). We can assume it's just a typo from the user, and a
16139 diagnostic will then be issued. */
16140 return cp_parser_template_type_arg (parser);
16143 /* Parse an explicit-instantiation.
16145 explicit-instantiation:
16146 template declaration
16148 Although the standard says `declaration', what it really means is:
16150 explicit-instantiation:
16151 template decl-specifier-seq [opt] declarator [opt] ;
16153 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16154 supposed to be allowed. A defect report has been filed about this
16155 issue.
16157 GNU Extension:
16159 explicit-instantiation:
16160 storage-class-specifier template
16161 decl-specifier-seq [opt] declarator [opt] ;
16162 function-specifier template
16163 decl-specifier-seq [opt] declarator [opt] ; */
16165 static void
16166 cp_parser_explicit_instantiation (cp_parser* parser)
16168 int declares_class_or_enum;
16169 cp_decl_specifier_seq decl_specifiers;
16170 tree extension_specifier = NULL_TREE;
16172 timevar_push (TV_TEMPLATE_INST);
16174 /* Look for an (optional) storage-class-specifier or
16175 function-specifier. */
16176 if (cp_parser_allow_gnu_extensions_p (parser))
16178 extension_specifier
16179 = cp_parser_storage_class_specifier_opt (parser);
16180 if (!extension_specifier)
16181 extension_specifier
16182 = cp_parser_function_specifier_opt (parser,
16183 /*decl_specs=*/NULL);
16186 /* Look for the `template' keyword. */
16187 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16188 /* Let the front end know that we are processing an explicit
16189 instantiation. */
16190 begin_explicit_instantiation ();
16191 /* [temp.explicit] says that we are supposed to ignore access
16192 control while processing explicit instantiation directives. */
16193 push_deferring_access_checks (dk_no_check);
16194 /* Parse a decl-specifier-seq. */
16195 cp_parser_decl_specifier_seq (parser,
16196 CP_PARSER_FLAGS_OPTIONAL,
16197 &decl_specifiers,
16198 &declares_class_or_enum);
16199 /* If there was exactly one decl-specifier, and it declared a class,
16200 and there's no declarator, then we have an explicit type
16201 instantiation. */
16202 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16204 tree type;
16206 type = check_tag_decl (&decl_specifiers,
16207 /*explicit_type_instantiation_p=*/true);
16208 /* Turn access control back on for names used during
16209 template instantiation. */
16210 pop_deferring_access_checks ();
16211 if (type)
16212 do_type_instantiation (type, extension_specifier,
16213 /*complain=*/tf_error);
16215 else
16217 cp_declarator *declarator;
16218 tree decl;
16220 /* Parse the declarator. */
16221 declarator
16222 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16223 /*ctor_dtor_or_conv_p=*/NULL,
16224 /*parenthesized_p=*/NULL,
16225 /*member_p=*/false,
16226 /*friend_p=*/false);
16227 if (declares_class_or_enum & 2)
16228 cp_parser_check_for_definition_in_return_type (declarator,
16229 decl_specifiers.type,
16230 decl_specifiers.locations[ds_type_spec]);
16231 if (declarator != cp_error_declarator)
16233 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16234 permerror (decl_specifiers.locations[ds_inline],
16235 "explicit instantiation shall not use"
16236 " %<inline%> specifier");
16237 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16238 permerror (decl_specifiers.locations[ds_constexpr],
16239 "explicit instantiation shall not use"
16240 " %<constexpr%> specifier");
16242 decl = grokdeclarator (declarator, &decl_specifiers,
16243 NORMAL, 0, &decl_specifiers.attributes);
16244 /* Turn access control back on for names used during
16245 template instantiation. */
16246 pop_deferring_access_checks ();
16247 /* Do the explicit instantiation. */
16248 do_decl_instantiation (decl, extension_specifier);
16250 else
16252 pop_deferring_access_checks ();
16253 /* Skip the body of the explicit instantiation. */
16254 cp_parser_skip_to_end_of_statement (parser);
16257 /* We're done with the instantiation. */
16258 end_explicit_instantiation ();
16260 cp_parser_consume_semicolon_at_end_of_statement (parser);
16262 timevar_pop (TV_TEMPLATE_INST);
16265 /* Parse an explicit-specialization.
16267 explicit-specialization:
16268 template < > declaration
16270 Although the standard says `declaration', what it really means is:
16272 explicit-specialization:
16273 template <> decl-specifier [opt] init-declarator [opt] ;
16274 template <> function-definition
16275 template <> explicit-specialization
16276 template <> template-declaration */
16278 static void
16279 cp_parser_explicit_specialization (cp_parser* parser)
16281 bool need_lang_pop;
16282 cp_token *token = cp_lexer_peek_token (parser->lexer);
16284 /* Look for the `template' keyword. */
16285 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16286 /* Look for the `<'. */
16287 cp_parser_require (parser, CPP_LESS, RT_LESS);
16288 /* Look for the `>'. */
16289 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16290 /* We have processed another parameter list. */
16291 ++parser->num_template_parameter_lists;
16292 /* [temp]
16294 A template ... explicit specialization ... shall not have C
16295 linkage. */
16296 if (current_lang_name == lang_name_c)
16298 error_at (token->location, "template specialization with C linkage");
16299 /* Give it C++ linkage to avoid confusing other parts of the
16300 front end. */
16301 push_lang_context (lang_name_cplusplus);
16302 need_lang_pop = true;
16304 else
16305 need_lang_pop = false;
16306 /* Let the front end know that we are beginning a specialization. */
16307 if (!begin_specialization ())
16309 end_specialization ();
16310 return;
16313 /* If the next keyword is `template', we need to figure out whether
16314 or not we're looking a template-declaration. */
16315 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16317 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16318 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16319 cp_parser_template_declaration_after_export (parser,
16320 /*member_p=*/false);
16321 else
16322 cp_parser_explicit_specialization (parser);
16324 else
16325 /* Parse the dependent declaration. */
16326 cp_parser_single_declaration (parser,
16327 /*checks=*/NULL,
16328 /*member_p=*/false,
16329 /*explicit_specialization_p=*/true,
16330 /*friend_p=*/NULL);
16331 /* We're done with the specialization. */
16332 end_specialization ();
16333 /* For the erroneous case of a template with C linkage, we pushed an
16334 implicit C++ linkage scope; exit that scope now. */
16335 if (need_lang_pop)
16336 pop_lang_context ();
16337 /* We're done with this parameter list. */
16338 --parser->num_template_parameter_lists;
16341 /* Parse a type-specifier.
16343 type-specifier:
16344 simple-type-specifier
16345 class-specifier
16346 enum-specifier
16347 elaborated-type-specifier
16348 cv-qualifier
16350 GNU Extension:
16352 type-specifier:
16353 __complex__
16355 Returns a representation of the type-specifier. For a
16356 class-specifier, enum-specifier, or elaborated-type-specifier, a
16357 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16359 The parser flags FLAGS is used to control type-specifier parsing.
16361 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16362 in a decl-specifier-seq.
16364 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16365 class-specifier, enum-specifier, or elaborated-type-specifier, then
16366 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16367 if a type is declared; 2 if it is defined. Otherwise, it is set to
16368 zero.
16370 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16371 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16372 is set to FALSE. */
16374 static tree
16375 cp_parser_type_specifier (cp_parser* parser,
16376 cp_parser_flags flags,
16377 cp_decl_specifier_seq *decl_specs,
16378 bool is_declaration,
16379 int* declares_class_or_enum,
16380 bool* is_cv_qualifier)
16382 tree type_spec = NULL_TREE;
16383 cp_token *token;
16384 enum rid keyword;
16385 cp_decl_spec ds = ds_last;
16387 /* Assume this type-specifier does not declare a new type. */
16388 if (declares_class_or_enum)
16389 *declares_class_or_enum = 0;
16390 /* And that it does not specify a cv-qualifier. */
16391 if (is_cv_qualifier)
16392 *is_cv_qualifier = false;
16393 /* Peek at the next token. */
16394 token = cp_lexer_peek_token (parser->lexer);
16396 /* If we're looking at a keyword, we can use that to guide the
16397 production we choose. */
16398 keyword = token->keyword;
16399 switch (keyword)
16401 case RID_ENUM:
16402 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16403 goto elaborated_type_specifier;
16405 /* Look for the enum-specifier. */
16406 type_spec = cp_parser_enum_specifier (parser);
16407 /* If that worked, we're done. */
16408 if (type_spec)
16410 if (declares_class_or_enum)
16411 *declares_class_or_enum = 2;
16412 if (decl_specs)
16413 cp_parser_set_decl_spec_type (decl_specs,
16414 type_spec,
16415 token,
16416 /*type_definition_p=*/true);
16417 return type_spec;
16419 else
16420 goto elaborated_type_specifier;
16422 /* Any of these indicate either a class-specifier, or an
16423 elaborated-type-specifier. */
16424 case RID_CLASS:
16425 case RID_STRUCT:
16426 case RID_UNION:
16427 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16428 goto elaborated_type_specifier;
16430 /* Parse tentatively so that we can back up if we don't find a
16431 class-specifier. */
16432 cp_parser_parse_tentatively (parser);
16433 /* Look for the class-specifier. */
16434 type_spec = cp_parser_class_specifier (parser);
16435 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16436 /* If that worked, we're done. */
16437 if (cp_parser_parse_definitely (parser))
16439 if (declares_class_or_enum)
16440 *declares_class_or_enum = 2;
16441 if (decl_specs)
16442 cp_parser_set_decl_spec_type (decl_specs,
16443 type_spec,
16444 token,
16445 /*type_definition_p=*/true);
16446 return type_spec;
16449 /* Fall through. */
16450 elaborated_type_specifier:
16451 /* We're declaring (not defining) a class or enum. */
16452 if (declares_class_or_enum)
16453 *declares_class_or_enum = 1;
16455 /* Fall through. */
16456 case RID_TYPENAME:
16457 /* Look for an elaborated-type-specifier. */
16458 type_spec
16459 = (cp_parser_elaborated_type_specifier
16460 (parser,
16461 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16462 is_declaration));
16463 if (decl_specs)
16464 cp_parser_set_decl_spec_type (decl_specs,
16465 type_spec,
16466 token,
16467 /*type_definition_p=*/false);
16468 return type_spec;
16470 case RID_CONST:
16471 ds = ds_const;
16472 if (is_cv_qualifier)
16473 *is_cv_qualifier = true;
16474 break;
16476 case RID_VOLATILE:
16477 ds = ds_volatile;
16478 if (is_cv_qualifier)
16479 *is_cv_qualifier = true;
16480 break;
16482 case RID_RESTRICT:
16483 ds = ds_restrict;
16484 if (is_cv_qualifier)
16485 *is_cv_qualifier = true;
16486 break;
16488 case RID_COMPLEX:
16489 /* The `__complex__' keyword is a GNU extension. */
16490 ds = ds_complex;
16491 break;
16493 default:
16494 break;
16497 /* Handle simple keywords. */
16498 if (ds != ds_last)
16500 if (decl_specs)
16502 set_and_check_decl_spec_loc (decl_specs, ds, token);
16503 decl_specs->any_specifiers_p = true;
16505 return cp_lexer_consume_token (parser->lexer)->u.value;
16508 /* If we do not already have a type-specifier, assume we are looking
16509 at a simple-type-specifier. */
16510 type_spec = cp_parser_simple_type_specifier (parser,
16511 decl_specs,
16512 flags);
16514 /* If we didn't find a type-specifier, and a type-specifier was not
16515 optional in this context, issue an error message. */
16516 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16518 cp_parser_error (parser, "expected type specifier");
16519 return error_mark_node;
16522 return type_spec;
16525 /* Parse a simple-type-specifier.
16527 simple-type-specifier:
16528 :: [opt] nested-name-specifier [opt] type-name
16529 :: [opt] nested-name-specifier template template-id
16530 char
16531 wchar_t
16532 bool
16533 short
16535 long
16536 signed
16537 unsigned
16538 float
16539 double
16540 void
16542 C++11 Extension:
16544 simple-type-specifier:
16545 auto
16546 decltype ( expression )
16547 char16_t
16548 char32_t
16549 __underlying_type ( type-id )
16551 C++17 extension:
16553 nested-name-specifier(opt) template-name
16555 GNU Extension:
16557 simple-type-specifier:
16558 __int128
16559 __typeof__ unary-expression
16560 __typeof__ ( type-id )
16561 __typeof__ ( type-id ) { initializer-list , [opt] }
16563 Concepts Extension:
16565 simple-type-specifier:
16566 constrained-type-specifier
16568 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16569 appropriately updated. */
16571 static tree
16572 cp_parser_simple_type_specifier (cp_parser* parser,
16573 cp_decl_specifier_seq *decl_specs,
16574 cp_parser_flags flags)
16576 tree type = NULL_TREE;
16577 cp_token *token;
16578 int idx;
16580 /* Peek at the next token. */
16581 token = cp_lexer_peek_token (parser->lexer);
16583 /* If we're looking at a keyword, things are easy. */
16584 switch (token->keyword)
16586 case RID_CHAR:
16587 if (decl_specs)
16588 decl_specs->explicit_char_p = true;
16589 type = char_type_node;
16590 break;
16591 case RID_CHAR16:
16592 type = char16_type_node;
16593 break;
16594 case RID_CHAR32:
16595 type = char32_type_node;
16596 break;
16597 case RID_WCHAR:
16598 type = wchar_type_node;
16599 break;
16600 case RID_BOOL:
16601 type = boolean_type_node;
16602 break;
16603 case RID_SHORT:
16604 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16605 type = short_integer_type_node;
16606 break;
16607 case RID_INT:
16608 if (decl_specs)
16609 decl_specs->explicit_int_p = true;
16610 type = integer_type_node;
16611 break;
16612 case RID_INT_N_0:
16613 case RID_INT_N_1:
16614 case RID_INT_N_2:
16615 case RID_INT_N_3:
16616 idx = token->keyword - RID_INT_N_0;
16617 if (! int_n_enabled_p [idx])
16618 break;
16619 if (decl_specs)
16621 decl_specs->explicit_intN_p = true;
16622 decl_specs->int_n_idx = idx;
16624 type = int_n_trees [idx].signed_type;
16625 break;
16626 case RID_LONG:
16627 if (decl_specs)
16628 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16629 type = long_integer_type_node;
16630 break;
16631 case RID_SIGNED:
16632 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16633 type = integer_type_node;
16634 break;
16635 case RID_UNSIGNED:
16636 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16637 type = unsigned_type_node;
16638 break;
16639 case RID_FLOAT:
16640 type = float_type_node;
16641 break;
16642 case RID_DOUBLE:
16643 type = double_type_node;
16644 break;
16645 case RID_VOID:
16646 type = void_type_node;
16647 break;
16649 case RID_AUTO:
16650 maybe_warn_cpp0x (CPP0X_AUTO);
16651 if (parser->auto_is_implicit_function_template_parm_p)
16653 /* The 'auto' might be the placeholder return type for a function decl
16654 with trailing return type. */
16655 bool have_trailing_return_fn_decl = false;
16657 cp_parser_parse_tentatively (parser);
16658 cp_lexer_consume_token (parser->lexer);
16659 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16660 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16661 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16662 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16664 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16666 cp_lexer_consume_token (parser->lexer);
16667 cp_parser_skip_to_closing_parenthesis (parser,
16668 /*recovering*/false,
16669 /*or_comma*/false,
16670 /*consume_paren*/true);
16671 continue;
16674 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16676 have_trailing_return_fn_decl = true;
16677 break;
16680 cp_lexer_consume_token (parser->lexer);
16682 cp_parser_abort_tentative_parse (parser);
16684 if (have_trailing_return_fn_decl)
16686 type = make_auto ();
16687 break;
16690 if (cxx_dialect >= cxx14)
16692 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16693 type = TREE_TYPE (type);
16695 else
16696 type = error_mark_node;
16698 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16700 if (cxx_dialect < cxx14)
16701 error_at (token->location,
16702 "use of %<auto%> in lambda parameter declaration "
16703 "only available with "
16704 "-std=c++14 or -std=gnu++14");
16706 else if (cxx_dialect < cxx14)
16707 error_at (token->location,
16708 "use of %<auto%> in parameter declaration "
16709 "only available with "
16710 "-std=c++14 or -std=gnu++14");
16711 else if (!flag_concepts)
16712 pedwarn (token->location, OPT_Wpedantic,
16713 "ISO C++ forbids use of %<auto%> in parameter "
16714 "declaration");
16716 else
16717 type = make_auto ();
16718 break;
16720 case RID_DECLTYPE:
16721 /* Since DR 743, decltype can either be a simple-type-specifier by
16722 itself or begin a nested-name-specifier. Parsing it will replace
16723 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16724 handling below decide what to do. */
16725 cp_parser_decltype (parser);
16726 cp_lexer_set_token_position (parser->lexer, token);
16727 break;
16729 case RID_TYPEOF:
16730 /* Consume the `typeof' token. */
16731 cp_lexer_consume_token (parser->lexer);
16732 /* Parse the operand to `typeof'. */
16733 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16734 /* If it is not already a TYPE, take its type. */
16735 if (!TYPE_P (type))
16736 type = finish_typeof (type);
16738 if (decl_specs)
16739 cp_parser_set_decl_spec_type (decl_specs, type,
16740 token,
16741 /*type_definition_p=*/false);
16743 return type;
16745 case RID_UNDERLYING_TYPE:
16746 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16747 if (decl_specs)
16748 cp_parser_set_decl_spec_type (decl_specs, type,
16749 token,
16750 /*type_definition_p=*/false);
16752 return type;
16754 case RID_BASES:
16755 case RID_DIRECT_BASES:
16756 type = cp_parser_trait_expr (parser, token->keyword);
16757 if (decl_specs)
16758 cp_parser_set_decl_spec_type (decl_specs, type,
16759 token,
16760 /*type_definition_p=*/false);
16761 return type;
16762 default:
16763 break;
16766 /* If token is an already-parsed decltype not followed by ::,
16767 it's a simple-type-specifier. */
16768 if (token->type == CPP_DECLTYPE
16769 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16771 type = saved_checks_value (token->u.tree_check_value);
16772 if (decl_specs)
16774 cp_parser_set_decl_spec_type (decl_specs, type,
16775 token,
16776 /*type_definition_p=*/false);
16777 /* Remember that we are handling a decltype in order to
16778 implement the resolution of DR 1510 when the argument
16779 isn't instantiation dependent. */
16780 decl_specs->decltype_p = true;
16782 cp_lexer_consume_token (parser->lexer);
16783 return type;
16786 /* If the type-specifier was for a built-in type, we're done. */
16787 if (type)
16789 /* Record the type. */
16790 if (decl_specs
16791 && (token->keyword != RID_SIGNED
16792 && token->keyword != RID_UNSIGNED
16793 && token->keyword != RID_SHORT
16794 && token->keyword != RID_LONG))
16795 cp_parser_set_decl_spec_type (decl_specs,
16796 type,
16797 token,
16798 /*type_definition_p=*/false);
16799 if (decl_specs)
16800 decl_specs->any_specifiers_p = true;
16802 /* Consume the token. */
16803 cp_lexer_consume_token (parser->lexer);
16805 if (type == error_mark_node)
16806 return error_mark_node;
16808 /* There is no valid C++ program where a non-template type is
16809 followed by a "<". That usually indicates that the user thought
16810 that the type was a template. */
16811 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16812 token->location);
16814 return TYPE_NAME (type);
16817 /* The type-specifier must be a user-defined type. */
16818 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16820 bool qualified_p;
16821 bool global_p;
16823 /* Don't gobble tokens or issue error messages if this is an
16824 optional type-specifier. */
16825 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16826 cp_parser_parse_tentatively (parser);
16828 token = cp_lexer_peek_token (parser->lexer);
16830 /* Look for the optional `::' operator. */
16831 global_p
16832 = (cp_parser_global_scope_opt (parser,
16833 /*current_scope_valid_p=*/false)
16834 != NULL_TREE);
16835 /* Look for the nested-name specifier. */
16836 qualified_p
16837 = (cp_parser_nested_name_specifier_opt (parser,
16838 /*typename_keyword_p=*/false,
16839 /*check_dependency_p=*/true,
16840 /*type_p=*/false,
16841 /*is_declaration=*/false)
16842 != NULL_TREE);
16843 /* If we have seen a nested-name-specifier, and the next token
16844 is `template', then we are using the template-id production. */
16845 if (parser->scope
16846 && cp_parser_optional_template_keyword (parser))
16848 /* Look for the template-id. */
16849 type = cp_parser_template_id (parser,
16850 /*template_keyword_p=*/true,
16851 /*check_dependency_p=*/true,
16852 none_type,
16853 /*is_declaration=*/false);
16854 /* If the template-id did not name a type, we are out of
16855 luck. */
16856 if (TREE_CODE (type) != TYPE_DECL)
16858 cp_parser_error (parser, "expected template-id for type");
16859 type = NULL_TREE;
16862 /* Otherwise, look for a type-name. */
16863 else
16864 type = cp_parser_type_name (parser);
16865 /* Keep track of all name-lookups performed in class scopes. */
16866 if (type
16867 && !global_p
16868 && !qualified_p
16869 && TREE_CODE (type) == TYPE_DECL
16870 && identifier_p (DECL_NAME (type)))
16871 maybe_note_name_used_in_class (DECL_NAME (type), type);
16872 /* If it didn't work out, we don't have a TYPE. */
16873 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16874 && !cp_parser_parse_definitely (parser))
16875 type = NULL_TREE;
16876 if (!type && cxx_dialect >= cxx1z)
16878 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16879 cp_parser_parse_tentatively (parser);
16881 cp_parser_global_scope_opt (parser,
16882 /*current_scope_valid_p=*/false);
16883 cp_parser_nested_name_specifier_opt (parser,
16884 /*typename_keyword_p=*/false,
16885 /*check_dependency_p=*/true,
16886 /*type_p=*/false,
16887 /*is_declaration=*/false);
16888 tree name = cp_parser_identifier (parser);
16889 if (name && TREE_CODE (name) == IDENTIFIER_NODE
16890 && parser->scope != error_mark_node)
16892 tree tmpl = cp_parser_lookup_name (parser, name,
16893 none_type,
16894 /*is_template=*/false,
16895 /*is_namespace=*/false,
16896 /*check_dependency=*/true,
16897 /*ambiguous_decls=*/NULL,
16898 token->location);
16899 if (tmpl && tmpl != error_mark_node
16900 && (DECL_CLASS_TEMPLATE_P (tmpl)
16901 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
16902 type = make_template_placeholder (tmpl);
16903 else
16905 type = error_mark_node;
16906 if (!cp_parser_simulate_error (parser))
16907 cp_parser_name_lookup_error (parser, name, tmpl,
16908 NLE_TYPE, token->location);
16911 else
16912 type = error_mark_node;
16914 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16915 && !cp_parser_parse_definitely (parser))
16916 type = NULL_TREE;
16918 if (type && decl_specs)
16919 cp_parser_set_decl_spec_type (decl_specs, type,
16920 token,
16921 /*type_definition_p=*/false);
16924 /* If we didn't get a type-name, issue an error message. */
16925 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16927 cp_parser_error (parser, "expected type-name");
16928 return error_mark_node;
16931 if (type && type != error_mark_node)
16933 /* See if TYPE is an Objective-C type, and if so, parse and
16934 accept any protocol references following it. Do this before
16935 the cp_parser_check_for_invalid_template_id() call, because
16936 Objective-C types can be followed by '<...>' which would
16937 enclose protocol names rather than template arguments, and so
16938 everything is fine. */
16939 if (c_dialect_objc () && !parser->scope
16940 && (objc_is_id (type) || objc_is_class_name (type)))
16942 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16943 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16945 /* Clobber the "unqualified" type previously entered into
16946 DECL_SPECS with the new, improved protocol-qualified version. */
16947 if (decl_specs)
16948 decl_specs->type = qual_type;
16950 return qual_type;
16953 /* There is no valid C++ program where a non-template type is
16954 followed by a "<". That usually indicates that the user
16955 thought that the type was a template. */
16956 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16957 none_type,
16958 token->location);
16961 return type;
16964 /* Parse a type-name.
16966 type-name:
16967 class-name
16968 enum-name
16969 typedef-name
16970 simple-template-id [in c++0x]
16972 enum-name:
16973 identifier
16975 typedef-name:
16976 identifier
16978 Concepts:
16980 type-name:
16981 concept-name
16982 partial-concept-id
16984 concept-name:
16985 identifier
16987 Returns a TYPE_DECL for the type. */
16989 static tree
16990 cp_parser_type_name (cp_parser* parser)
16992 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16995 /* See above. */
16996 static tree
16997 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16999 tree type_decl;
17001 /* We can't know yet whether it is a class-name or not. */
17002 cp_parser_parse_tentatively (parser);
17003 /* Try a class-name. */
17004 type_decl = cp_parser_class_name (parser,
17005 typename_keyword_p,
17006 /*template_keyword_p=*/false,
17007 none_type,
17008 /*check_dependency_p=*/true,
17009 /*class_head_p=*/false,
17010 /*is_declaration=*/false);
17011 /* If it's not a class-name, keep looking. */
17012 if (!cp_parser_parse_definitely (parser))
17014 if (cxx_dialect < cxx11)
17015 /* It must be a typedef-name or an enum-name. */
17016 return cp_parser_nonclass_name (parser);
17018 cp_parser_parse_tentatively (parser);
17019 /* It is either a simple-template-id representing an
17020 instantiation of an alias template... */
17021 type_decl = cp_parser_template_id (parser,
17022 /*template_keyword_p=*/false,
17023 /*check_dependency_p=*/true,
17024 none_type,
17025 /*is_declaration=*/false);
17026 /* Note that this must be an instantiation of an alias template
17027 because [temp.names]/6 says:
17029 A template-id that names an alias template specialization
17030 is a type-name.
17032 Whereas [temp.names]/7 says:
17034 A simple-template-id that names a class template
17035 specialization is a class-name.
17037 With concepts, this could also be a partial-concept-id that
17038 declares a non-type template parameter. */
17039 if (type_decl != NULL_TREE
17040 && TREE_CODE (type_decl) == TYPE_DECL
17041 && TYPE_DECL_ALIAS_P (type_decl))
17042 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17043 else if (is_constrained_parameter (type_decl))
17044 /* Don't do anything. */ ;
17045 else
17046 cp_parser_simulate_error (parser);
17048 if (!cp_parser_parse_definitely (parser))
17049 /* ... Or a typedef-name or an enum-name. */
17050 return cp_parser_nonclass_name (parser);
17053 return type_decl;
17056 /* Check if DECL and ARGS can form a constrained-type-specifier.
17057 If ARGS is non-null, we try to form a concept check of the
17058 form DECL<?, ARGS> where ? is a wildcard that matches any
17059 kind of template argument. If ARGS is NULL, then we try to
17060 form a concept check of the form DECL<?>. */
17062 static tree
17063 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17064 tree decl, tree args)
17066 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17068 /* If we a constrained-type-specifier cannot be deduced. */
17069 if (parser->prevent_constrained_type_specifiers)
17070 return NULL_TREE;
17072 /* A constrained type specifier can only be found in an
17073 overload set or as a reference to a template declaration.
17075 FIXME: This might be masking a bug. It's possible that
17076 that the deduction below is causing template specializations
17077 to be formed with the wildcard as an argument. */
17078 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17079 return NULL_TREE;
17081 /* Try to build a call expression that evaluates the
17082 concept. This can fail if the overload set refers
17083 only to non-templates. */
17084 tree placeholder = build_nt (WILDCARD_DECL);
17085 tree check = build_concept_check (decl, placeholder, args);
17086 if (check == error_mark_node)
17087 return NULL_TREE;
17089 /* Deduce the checked constraint and the prototype parameter.
17091 FIXME: In certain cases, failure to deduce should be a
17092 diagnosable error. */
17093 tree conc;
17094 tree proto;
17095 if (!deduce_constrained_parameter (check, conc, proto))
17096 return NULL_TREE;
17098 /* In template parameter scope, this results in a constrained
17099 parameter. Return a descriptor of that parm. */
17100 if (processing_template_parmlist)
17101 return build_constrained_parameter (conc, proto, args);
17103 /* In a parameter-declaration-clause, constrained-type
17104 specifiers result in invented template parameters. */
17105 if (parser->auto_is_implicit_function_template_parm_p)
17107 tree x = build_constrained_parameter (conc, proto, args);
17108 return synthesize_implicit_template_parm (parser, x);
17110 else
17112 /* Otherwise, we're in a context where the constrained
17113 type name is deduced and the constraint applies
17114 after deduction. */
17115 return make_constrained_auto (conc, args);
17118 return NULL_TREE;
17121 /* If DECL refers to a concept, return a TYPE_DECL representing
17122 the result of using the constrained type specifier in the
17123 current context. DECL refers to a concept if
17125 - it is an overload set containing a function concept taking a single
17126 type argument, or
17128 - it is a variable concept taking a single type argument. */
17130 static tree
17131 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17133 if (flag_concepts
17134 && (TREE_CODE (decl) == OVERLOAD
17135 || BASELINK_P (decl)
17136 || variable_concept_p (decl)))
17137 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17138 else
17139 return NULL_TREE;
17142 /* Check if DECL and ARGS form a partial-concept-id. If so,
17143 assign ID to the resulting constrained placeholder.
17145 Returns true if the partial-concept-id designates a placeholder
17146 and false otherwise. Note that *id is set to NULL_TREE in
17147 this case. */
17149 static tree
17150 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17152 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17155 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17156 or a concept-name.
17158 enum-name:
17159 identifier
17161 typedef-name:
17162 identifier
17164 concept-name:
17165 identifier
17167 Returns a TYPE_DECL for the type. */
17169 static tree
17170 cp_parser_nonclass_name (cp_parser* parser)
17172 tree type_decl;
17173 tree identifier;
17175 cp_token *token = cp_lexer_peek_token (parser->lexer);
17176 identifier = cp_parser_identifier (parser);
17177 if (identifier == error_mark_node)
17178 return error_mark_node;
17180 /* Look up the type-name. */
17181 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17183 type_decl = strip_using_decl (type_decl);
17185 /* If we found an overload set, then it may refer to a concept-name. */
17186 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17187 type_decl = decl;
17189 if (TREE_CODE (type_decl) != TYPE_DECL
17190 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17192 /* See if this is an Objective-C type. */
17193 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17194 tree type = objc_get_protocol_qualified_type (identifier, protos);
17195 if (type)
17196 type_decl = TYPE_NAME (type);
17199 /* Issue an error if we did not find a type-name. */
17200 if (TREE_CODE (type_decl) != TYPE_DECL
17201 /* In Objective-C, we have the complication that class names are
17202 normally type names and start declarations (eg, the
17203 "NSObject" in "NSObject *object;"), but can be used in an
17204 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17205 is an expression. So, a classname followed by a dot is not a
17206 valid type-name. */
17207 || (objc_is_class_name (TREE_TYPE (type_decl))
17208 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17210 if (!cp_parser_simulate_error (parser))
17211 cp_parser_name_lookup_error (parser, identifier, type_decl,
17212 NLE_TYPE, token->location);
17213 return error_mark_node;
17215 /* Remember that the name was used in the definition of the
17216 current class so that we can check later to see if the
17217 meaning would have been different after the class was
17218 entirely defined. */
17219 else if (type_decl != error_mark_node
17220 && !parser->scope)
17221 maybe_note_name_used_in_class (identifier, type_decl);
17223 return type_decl;
17226 /* Parse an elaborated-type-specifier. Note that the grammar given
17227 here incorporates the resolution to DR68.
17229 elaborated-type-specifier:
17230 class-key :: [opt] nested-name-specifier [opt] identifier
17231 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17232 enum-key :: [opt] nested-name-specifier [opt] identifier
17233 typename :: [opt] nested-name-specifier identifier
17234 typename :: [opt] nested-name-specifier template [opt]
17235 template-id
17237 GNU extension:
17239 elaborated-type-specifier:
17240 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17241 class-key attributes :: [opt] nested-name-specifier [opt]
17242 template [opt] template-id
17243 enum attributes :: [opt] nested-name-specifier [opt] identifier
17245 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17246 declared `friend'. If IS_DECLARATION is TRUE, then this
17247 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17248 something is being declared.
17250 Returns the TYPE specified. */
17252 static tree
17253 cp_parser_elaborated_type_specifier (cp_parser* parser,
17254 bool is_friend,
17255 bool is_declaration)
17257 enum tag_types tag_type;
17258 tree identifier;
17259 tree type = NULL_TREE;
17260 tree attributes = NULL_TREE;
17261 tree globalscope;
17262 cp_token *token = NULL;
17264 /* See if we're looking at the `enum' keyword. */
17265 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17267 /* Consume the `enum' token. */
17268 cp_lexer_consume_token (parser->lexer);
17269 /* Remember that it's an enumeration type. */
17270 tag_type = enum_type;
17271 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17272 enums) is used here. */
17273 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17274 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17276 pedwarn (input_location, 0, "elaborated-type-specifier "
17277 "for a scoped enum must not use the %<%D%> keyword",
17278 cp_lexer_peek_token (parser->lexer)->u.value);
17279 /* Consume the `struct' or `class' and parse it anyway. */
17280 cp_lexer_consume_token (parser->lexer);
17282 /* Parse the attributes. */
17283 attributes = cp_parser_attributes_opt (parser);
17285 /* Or, it might be `typename'. */
17286 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17287 RID_TYPENAME))
17289 /* Consume the `typename' token. */
17290 cp_lexer_consume_token (parser->lexer);
17291 /* Remember that it's a `typename' type. */
17292 tag_type = typename_type;
17294 /* Otherwise it must be a class-key. */
17295 else
17297 tag_type = cp_parser_class_key (parser);
17298 if (tag_type == none_type)
17299 return error_mark_node;
17300 /* Parse the attributes. */
17301 attributes = cp_parser_attributes_opt (parser);
17304 /* Look for the `::' operator. */
17305 globalscope = cp_parser_global_scope_opt (parser,
17306 /*current_scope_valid_p=*/false);
17307 /* Look for the nested-name-specifier. */
17308 tree nested_name_specifier;
17309 if (tag_type == typename_type && !globalscope)
17311 nested_name_specifier
17312 = cp_parser_nested_name_specifier (parser,
17313 /*typename_keyword_p=*/true,
17314 /*check_dependency_p=*/true,
17315 /*type_p=*/true,
17316 is_declaration);
17317 if (!nested_name_specifier)
17318 return error_mark_node;
17320 else
17321 /* Even though `typename' is not present, the proposed resolution
17322 to Core Issue 180 says that in `class A<T>::B', `B' should be
17323 considered a type-name, even if `A<T>' is dependent. */
17324 nested_name_specifier
17325 = cp_parser_nested_name_specifier_opt (parser,
17326 /*typename_keyword_p=*/true,
17327 /*check_dependency_p=*/true,
17328 /*type_p=*/true,
17329 is_declaration);
17330 /* For everything but enumeration types, consider a template-id.
17331 For an enumeration type, consider only a plain identifier. */
17332 if (tag_type != enum_type)
17334 bool template_p = false;
17335 tree decl;
17337 /* Allow the `template' keyword. */
17338 template_p = cp_parser_optional_template_keyword (parser);
17339 /* If we didn't see `template', we don't know if there's a
17340 template-id or not. */
17341 if (!template_p)
17342 cp_parser_parse_tentatively (parser);
17343 /* Parse the template-id. */
17344 token = cp_lexer_peek_token (parser->lexer);
17345 decl = cp_parser_template_id (parser, template_p,
17346 /*check_dependency_p=*/true,
17347 tag_type,
17348 is_declaration);
17349 /* If we didn't find a template-id, look for an ordinary
17350 identifier. */
17351 if (!template_p && !cp_parser_parse_definitely (parser))
17353 /* We can get here when cp_parser_template_id, called by
17354 cp_parser_class_name with tag_type == none_type, succeeds
17355 and caches a BASELINK. Then, when called again here,
17356 instead of failing and returning an error_mark_node
17357 returns it (see template/typename17.C in C++11).
17358 ??? Could we diagnose this earlier? */
17359 else if (tag_type == typename_type && BASELINK_P (decl))
17361 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17362 type = error_mark_node;
17364 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17365 in effect, then we must assume that, upon instantiation, the
17366 template will correspond to a class. */
17367 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17368 && tag_type == typename_type)
17369 type = make_typename_type (parser->scope, decl,
17370 typename_type,
17371 /*complain=*/tf_error);
17372 /* If the `typename' keyword is in effect and DECL is not a type
17373 decl, then type is non existent. */
17374 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17376 else if (TREE_CODE (decl) == TYPE_DECL)
17378 type = check_elaborated_type_specifier (tag_type, decl,
17379 /*allow_template_p=*/true);
17381 /* If the next token is a semicolon, this must be a specialization,
17382 instantiation, or friend declaration. Check the scope while we
17383 still know whether or not we had a nested-name-specifier. */
17384 if (type != error_mark_node
17385 && !nested_name_specifier && !is_friend
17386 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17387 check_unqualified_spec_or_inst (type, token->location);
17389 else if (decl == error_mark_node)
17390 type = error_mark_node;
17393 if (!type)
17395 token = cp_lexer_peek_token (parser->lexer);
17396 identifier = cp_parser_identifier (parser);
17398 if (identifier == error_mark_node)
17400 parser->scope = NULL_TREE;
17401 return error_mark_node;
17404 /* For a `typename', we needn't call xref_tag. */
17405 if (tag_type == typename_type
17406 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17407 return cp_parser_make_typename_type (parser, identifier,
17408 token->location);
17410 /* Template parameter lists apply only if we are not within a
17411 function parameter list. */
17412 bool template_parm_lists_apply
17413 = parser->num_template_parameter_lists;
17414 if (template_parm_lists_apply)
17415 for (cp_binding_level *s = current_binding_level;
17416 s && s->kind != sk_template_parms;
17417 s = s->level_chain)
17418 if (s->kind == sk_function_parms)
17419 template_parm_lists_apply = false;
17421 /* Look up a qualified name in the usual way. */
17422 if (parser->scope)
17424 tree decl;
17425 tree ambiguous_decls;
17427 decl = cp_parser_lookup_name (parser, identifier,
17428 tag_type,
17429 /*is_template=*/false,
17430 /*is_namespace=*/false,
17431 /*check_dependency=*/true,
17432 &ambiguous_decls,
17433 token->location);
17435 /* If the lookup was ambiguous, an error will already have been
17436 issued. */
17437 if (ambiguous_decls)
17438 return error_mark_node;
17440 /* If we are parsing friend declaration, DECL may be a
17441 TEMPLATE_DECL tree node here. However, we need to check
17442 whether this TEMPLATE_DECL results in valid code. Consider
17443 the following example:
17445 namespace N {
17446 template <class T> class C {};
17448 class X {
17449 template <class T> friend class N::C; // #1, valid code
17451 template <class T> class Y {
17452 friend class N::C; // #2, invalid code
17455 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17456 name lookup of `N::C'. We see that friend declaration must
17457 be template for the code to be valid. Note that
17458 processing_template_decl does not work here since it is
17459 always 1 for the above two cases. */
17461 decl = (cp_parser_maybe_treat_template_as_class
17462 (decl, /*tag_name_p=*/is_friend
17463 && template_parm_lists_apply));
17465 if (TREE_CODE (decl) != TYPE_DECL)
17467 cp_parser_diagnose_invalid_type_name (parser,
17468 identifier,
17469 token->location);
17470 return error_mark_node;
17473 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17475 bool allow_template = (template_parm_lists_apply
17476 || DECL_SELF_REFERENCE_P (decl));
17477 type = check_elaborated_type_specifier (tag_type, decl,
17478 allow_template);
17480 if (type == error_mark_node)
17481 return error_mark_node;
17484 /* Forward declarations of nested types, such as
17486 class C1::C2;
17487 class C1::C2::C3;
17489 are invalid unless all components preceding the final '::'
17490 are complete. If all enclosing types are complete, these
17491 declarations become merely pointless.
17493 Invalid forward declarations of nested types are errors
17494 caught elsewhere in parsing. Those that are pointless arrive
17495 here. */
17497 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17498 && !is_friend && !processing_explicit_instantiation)
17499 warning (0, "declaration %qD does not declare anything", decl);
17501 type = TREE_TYPE (decl);
17503 else
17505 /* An elaborated-type-specifier sometimes introduces a new type and
17506 sometimes names an existing type. Normally, the rule is that it
17507 introduces a new type only if there is not an existing type of
17508 the same name already in scope. For example, given:
17510 struct S {};
17511 void f() { struct S s; }
17513 the `struct S' in the body of `f' is the same `struct S' as in
17514 the global scope; the existing definition is used. However, if
17515 there were no global declaration, this would introduce a new
17516 local class named `S'.
17518 An exception to this rule applies to the following code:
17520 namespace N { struct S; }
17522 Here, the elaborated-type-specifier names a new type
17523 unconditionally; even if there is already an `S' in the
17524 containing scope this declaration names a new type.
17525 This exception only applies if the elaborated-type-specifier
17526 forms the complete declaration:
17528 [class.name]
17530 A declaration consisting solely of `class-key identifier ;' is
17531 either a redeclaration of the name in the current scope or a
17532 forward declaration of the identifier as a class name. It
17533 introduces the name into the current scope.
17535 We are in this situation precisely when the next token is a `;'.
17537 An exception to the exception is that a `friend' declaration does
17538 *not* name a new type; i.e., given:
17540 struct S { friend struct T; };
17542 `T' is not a new type in the scope of `S'.
17544 Also, `new struct S' or `sizeof (struct S)' never results in the
17545 definition of a new type; a new type can only be declared in a
17546 declaration context. */
17548 tag_scope ts;
17549 bool template_p;
17551 if (is_friend)
17552 /* Friends have special name lookup rules. */
17553 ts = ts_within_enclosing_non_class;
17554 else if (is_declaration
17555 && cp_lexer_next_token_is (parser->lexer,
17556 CPP_SEMICOLON))
17557 /* This is a `class-key identifier ;' */
17558 ts = ts_current;
17559 else
17560 ts = ts_global;
17562 template_p =
17563 (template_parm_lists_apply
17564 && (cp_parser_next_token_starts_class_definition_p (parser)
17565 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17566 /* An unqualified name was used to reference this type, so
17567 there were no qualifying templates. */
17568 if (template_parm_lists_apply
17569 && !cp_parser_check_template_parameters (parser,
17570 /*num_templates=*/0,
17571 token->location,
17572 /*declarator=*/NULL))
17573 return error_mark_node;
17574 type = xref_tag (tag_type, identifier, ts, template_p);
17578 if (type == error_mark_node)
17579 return error_mark_node;
17581 /* Allow attributes on forward declarations of classes. */
17582 if (attributes)
17584 if (TREE_CODE (type) == TYPENAME_TYPE)
17585 warning (OPT_Wattributes,
17586 "attributes ignored on uninstantiated type");
17587 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17588 && ! processing_explicit_instantiation)
17589 warning (OPT_Wattributes,
17590 "attributes ignored on template instantiation");
17591 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17592 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17593 else
17594 warning (OPT_Wattributes,
17595 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17598 if (tag_type != enum_type)
17600 /* Indicate whether this class was declared as a `class' or as a
17601 `struct'. */
17602 if (CLASS_TYPE_P (type))
17603 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17604 cp_parser_check_class_key (tag_type, type);
17607 /* A "<" cannot follow an elaborated type specifier. If that
17608 happens, the user was probably trying to form a template-id. */
17609 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17610 token->location);
17612 return type;
17615 /* Parse an enum-specifier.
17617 enum-specifier:
17618 enum-head { enumerator-list [opt] }
17619 enum-head { enumerator-list , } [C++0x]
17621 enum-head:
17622 enum-key identifier [opt] enum-base [opt]
17623 enum-key nested-name-specifier identifier enum-base [opt]
17625 enum-key:
17626 enum
17627 enum class [C++0x]
17628 enum struct [C++0x]
17630 enum-base: [C++0x]
17631 : type-specifier-seq
17633 opaque-enum-specifier:
17634 enum-key identifier enum-base [opt] ;
17636 GNU Extensions:
17637 enum-key attributes[opt] identifier [opt] enum-base [opt]
17638 { enumerator-list [opt] }attributes[opt]
17639 enum-key attributes[opt] identifier [opt] enum-base [opt]
17640 { enumerator-list, }attributes[opt] [C++0x]
17642 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17643 if the token stream isn't an enum-specifier after all. */
17645 static tree
17646 cp_parser_enum_specifier (cp_parser* parser)
17648 tree identifier;
17649 tree type = NULL_TREE;
17650 tree prev_scope;
17651 tree nested_name_specifier = NULL_TREE;
17652 tree attributes;
17653 bool scoped_enum_p = false;
17654 bool has_underlying_type = false;
17655 bool nested_being_defined = false;
17656 bool new_value_list = false;
17657 bool is_new_type = false;
17658 bool is_unnamed = false;
17659 tree underlying_type = NULL_TREE;
17660 cp_token *type_start_token = NULL;
17661 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17663 parser->colon_corrects_to_scope_p = false;
17665 /* Parse tentatively so that we can back up if we don't find a
17666 enum-specifier. */
17667 cp_parser_parse_tentatively (parser);
17669 /* Caller guarantees that the current token is 'enum', an identifier
17670 possibly follows, and the token after that is an opening brace.
17671 If we don't have an identifier, fabricate an anonymous name for
17672 the enumeration being defined. */
17673 cp_lexer_consume_token (parser->lexer);
17675 /* Parse the "class" or "struct", which indicates a scoped
17676 enumeration type in C++0x. */
17677 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17678 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17680 if (cxx_dialect < cxx11)
17681 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17683 /* Consume the `struct' or `class' token. */
17684 cp_lexer_consume_token (parser->lexer);
17686 scoped_enum_p = true;
17689 attributes = cp_parser_attributes_opt (parser);
17691 /* Clear the qualification. */
17692 parser->scope = NULL_TREE;
17693 parser->qualifying_scope = NULL_TREE;
17694 parser->object_scope = NULL_TREE;
17696 /* Figure out in what scope the declaration is being placed. */
17697 prev_scope = current_scope ();
17699 type_start_token = cp_lexer_peek_token (parser->lexer);
17701 push_deferring_access_checks (dk_no_check);
17702 nested_name_specifier
17703 = cp_parser_nested_name_specifier_opt (parser,
17704 /*typename_keyword_p=*/true,
17705 /*check_dependency_p=*/false,
17706 /*type_p=*/false,
17707 /*is_declaration=*/false);
17709 if (nested_name_specifier)
17711 tree name;
17713 identifier = cp_parser_identifier (parser);
17714 name = cp_parser_lookup_name (parser, identifier,
17715 enum_type,
17716 /*is_template=*/false,
17717 /*is_namespace=*/false,
17718 /*check_dependency=*/true,
17719 /*ambiguous_decls=*/NULL,
17720 input_location);
17721 if (name && name != error_mark_node)
17723 type = TREE_TYPE (name);
17724 if (TREE_CODE (type) == TYPENAME_TYPE)
17726 /* Are template enums allowed in ISO? */
17727 if (template_parm_scope_p ())
17728 pedwarn (type_start_token->location, OPT_Wpedantic,
17729 "%qD is an enumeration template", name);
17730 /* ignore a typename reference, for it will be solved by name
17731 in start_enum. */
17732 type = NULL_TREE;
17735 else if (nested_name_specifier == error_mark_node)
17736 /* We already issued an error. */;
17737 else
17739 error_at (type_start_token->location,
17740 "%qD does not name an enumeration in %qT",
17741 identifier, nested_name_specifier);
17742 nested_name_specifier = error_mark_node;
17745 else
17747 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17748 identifier = cp_parser_identifier (parser);
17749 else
17751 identifier = make_anon_name ();
17752 is_unnamed = true;
17753 if (scoped_enum_p)
17754 error_at (type_start_token->location,
17755 "unnamed scoped enum is not allowed");
17758 pop_deferring_access_checks ();
17760 /* Check for the `:' that denotes a specified underlying type in C++0x.
17761 Note that a ':' could also indicate a bitfield width, however. */
17762 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17764 cp_decl_specifier_seq type_specifiers;
17766 /* Consume the `:'. */
17767 cp_lexer_consume_token (parser->lexer);
17769 /* Parse the type-specifier-seq. */
17770 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17771 /*is_trailing_return=*/false,
17772 &type_specifiers);
17774 /* At this point this is surely not elaborated type specifier. */
17775 if (!cp_parser_parse_definitely (parser))
17776 return NULL_TREE;
17778 if (cxx_dialect < cxx11)
17779 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17781 has_underlying_type = true;
17783 /* If that didn't work, stop. */
17784 if (type_specifiers.type != error_mark_node)
17786 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17787 /*initialized=*/0, NULL);
17788 if (underlying_type == error_mark_node
17789 || check_for_bare_parameter_packs (underlying_type))
17790 underlying_type = NULL_TREE;
17794 /* Look for the `{' but don't consume it yet. */
17795 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17797 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17799 cp_parser_error (parser, "expected %<{%>");
17800 if (has_underlying_type)
17802 type = NULL_TREE;
17803 goto out;
17806 /* An opaque-enum-specifier must have a ';' here. */
17807 if ((scoped_enum_p || underlying_type)
17808 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17810 cp_parser_error (parser, "expected %<;%> or %<{%>");
17811 if (has_underlying_type)
17813 type = NULL_TREE;
17814 goto out;
17819 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17820 return NULL_TREE;
17822 if (nested_name_specifier)
17824 if (CLASS_TYPE_P (nested_name_specifier))
17826 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17827 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17828 push_scope (nested_name_specifier);
17830 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17832 push_nested_namespace (nested_name_specifier);
17836 /* Issue an error message if type-definitions are forbidden here. */
17837 if (!cp_parser_check_type_definition (parser))
17838 type = error_mark_node;
17839 else
17840 /* Create the new type. We do this before consuming the opening
17841 brace so the enum will be recorded as being on the line of its
17842 tag (or the 'enum' keyword, if there is no tag). */
17843 type = start_enum (identifier, type, underlying_type,
17844 attributes, scoped_enum_p, &is_new_type);
17846 /* If the next token is not '{' it is an opaque-enum-specifier or an
17847 elaborated-type-specifier. */
17848 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17850 timevar_push (TV_PARSE_ENUM);
17851 if (nested_name_specifier
17852 && nested_name_specifier != error_mark_node)
17854 /* The following catches invalid code such as:
17855 enum class S<int>::E { A, B, C }; */
17856 if (!processing_specialization
17857 && CLASS_TYPE_P (nested_name_specifier)
17858 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17859 error_at (type_start_token->location, "cannot add an enumerator "
17860 "list to a template instantiation");
17862 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17864 error_at (type_start_token->location,
17865 "%<%T::%E%> has not been declared",
17866 TYPE_CONTEXT (nested_name_specifier),
17867 nested_name_specifier);
17868 type = error_mark_node;
17870 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17871 && !CLASS_TYPE_P (nested_name_specifier))
17873 error_at (type_start_token->location, "nested name specifier "
17874 "%qT for enum declaration does not name a class "
17875 "or namespace", nested_name_specifier);
17876 type = error_mark_node;
17878 /* If that scope does not contain the scope in which the
17879 class was originally declared, the program is invalid. */
17880 else if (prev_scope && !is_ancestor (prev_scope,
17881 nested_name_specifier))
17883 if (at_namespace_scope_p ())
17884 error_at (type_start_token->location,
17885 "declaration of %qD in namespace %qD which does not "
17886 "enclose %qD",
17887 type, prev_scope, nested_name_specifier);
17888 else
17889 error_at (type_start_token->location,
17890 "declaration of %qD in %qD which does not "
17891 "enclose %qD",
17892 type, prev_scope, nested_name_specifier);
17893 type = error_mark_node;
17895 /* If that scope is the scope where the declaration is being placed
17896 the program is invalid. */
17897 else if (CLASS_TYPE_P (nested_name_specifier)
17898 && CLASS_TYPE_P (prev_scope)
17899 && same_type_p (nested_name_specifier, prev_scope))
17901 permerror (type_start_token->location,
17902 "extra qualification not allowed");
17903 nested_name_specifier = NULL_TREE;
17907 if (scoped_enum_p)
17908 begin_scope (sk_scoped_enum, type);
17910 /* Consume the opening brace. */
17911 cp_lexer_consume_token (parser->lexer);
17913 if (type == error_mark_node)
17914 ; /* Nothing to add */
17915 else if (OPAQUE_ENUM_P (type)
17916 || (cxx_dialect > cxx98 && processing_specialization))
17918 new_value_list = true;
17919 SET_OPAQUE_ENUM_P (type, false);
17920 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17922 else
17924 error_at (type_start_token->location,
17925 "multiple definition of %q#T", type);
17926 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17927 "previous definition here");
17928 type = error_mark_node;
17931 if (type == error_mark_node)
17932 cp_parser_skip_to_end_of_block_or_statement (parser);
17933 /* If the next token is not '}', then there are some enumerators. */
17934 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17936 if (is_unnamed && !scoped_enum_p)
17937 pedwarn (type_start_token->location, OPT_Wpedantic,
17938 "ISO C++ forbids empty unnamed enum");
17940 else
17941 cp_parser_enumerator_list (parser, type);
17943 /* Consume the final '}'. */
17944 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17946 if (scoped_enum_p)
17947 finish_scope ();
17948 timevar_pop (TV_PARSE_ENUM);
17950 else
17952 /* If a ';' follows, then it is an opaque-enum-specifier
17953 and additional restrictions apply. */
17954 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17956 if (is_unnamed)
17957 error_at (type_start_token->location,
17958 "opaque-enum-specifier without name");
17959 else if (nested_name_specifier)
17960 error_at (type_start_token->location,
17961 "opaque-enum-specifier must use a simple identifier");
17965 /* Look for trailing attributes to apply to this enumeration, and
17966 apply them if appropriate. */
17967 if (cp_parser_allow_gnu_extensions_p (parser))
17969 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17970 cplus_decl_attributes (&type,
17971 trailing_attr,
17972 (int) ATTR_FLAG_TYPE_IN_PLACE);
17975 /* Finish up the enumeration. */
17976 if (type != error_mark_node)
17978 if (new_value_list)
17979 finish_enum_value_list (type);
17980 if (is_new_type)
17981 finish_enum (type);
17984 if (nested_name_specifier)
17986 if (CLASS_TYPE_P (nested_name_specifier))
17988 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17989 pop_scope (nested_name_specifier);
17991 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17993 pop_nested_namespace (nested_name_specifier);
17996 out:
17997 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17998 return type;
18001 /* Parse an enumerator-list. The enumerators all have the indicated
18002 TYPE.
18004 enumerator-list:
18005 enumerator-definition
18006 enumerator-list , enumerator-definition */
18008 static void
18009 cp_parser_enumerator_list (cp_parser* parser, tree type)
18011 while (true)
18013 /* Parse an enumerator-definition. */
18014 cp_parser_enumerator_definition (parser, type);
18016 /* If the next token is not a ',', we've reached the end of
18017 the list. */
18018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18019 break;
18020 /* Otherwise, consume the `,' and keep going. */
18021 cp_lexer_consume_token (parser->lexer);
18022 /* If the next token is a `}', there is a trailing comma. */
18023 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18025 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18026 pedwarn (input_location, OPT_Wpedantic,
18027 "comma at end of enumerator list");
18028 break;
18033 /* Parse an enumerator-definition. The enumerator has the indicated
18034 TYPE.
18036 enumerator-definition:
18037 enumerator
18038 enumerator = constant-expression
18040 enumerator:
18041 identifier
18043 GNU Extensions:
18045 enumerator-definition:
18046 enumerator attributes [opt]
18047 enumerator attributes [opt] = constant-expression */
18049 static void
18050 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18052 tree identifier;
18053 tree value;
18054 location_t loc;
18056 /* Save the input location because we are interested in the location
18057 of the identifier and not the location of the explicit value. */
18058 loc = cp_lexer_peek_token (parser->lexer)->location;
18060 /* Look for the identifier. */
18061 identifier = cp_parser_identifier (parser);
18062 if (identifier == error_mark_node)
18063 return;
18065 /* Parse any specified attributes. */
18066 tree attrs = cp_parser_attributes_opt (parser);
18068 /* If the next token is an '=', then there is an explicit value. */
18069 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18071 /* Consume the `=' token. */
18072 cp_lexer_consume_token (parser->lexer);
18073 /* Parse the value. */
18074 value = cp_parser_constant_expression (parser);
18076 else
18077 value = NULL_TREE;
18079 /* If we are processing a template, make sure the initializer of the
18080 enumerator doesn't contain any bare template parameter pack. */
18081 if (check_for_bare_parameter_packs (value))
18082 value = error_mark_node;
18084 /* Create the enumerator. */
18085 build_enumerator (identifier, value, type, attrs, loc);
18088 /* Parse a namespace-name.
18090 namespace-name:
18091 original-namespace-name
18092 namespace-alias
18094 Returns the NAMESPACE_DECL for the namespace. */
18096 static tree
18097 cp_parser_namespace_name (cp_parser* parser)
18099 tree identifier;
18100 tree namespace_decl;
18102 cp_token *token = cp_lexer_peek_token (parser->lexer);
18104 /* Get the name of the namespace. */
18105 identifier = cp_parser_identifier (parser);
18106 if (identifier == error_mark_node)
18107 return error_mark_node;
18109 /* Look up the identifier in the currently active scope. Look only
18110 for namespaces, due to:
18112 [basic.lookup.udir]
18114 When looking up a namespace-name in a using-directive or alias
18115 definition, only namespace names are considered.
18117 And:
18119 [basic.lookup.qual]
18121 During the lookup of a name preceding the :: scope resolution
18122 operator, object, function, and enumerator names are ignored.
18124 (Note that cp_parser_qualifying_entity only calls this
18125 function if the token after the name is the scope resolution
18126 operator.) */
18127 namespace_decl = cp_parser_lookup_name (parser, identifier,
18128 none_type,
18129 /*is_template=*/false,
18130 /*is_namespace=*/true,
18131 /*check_dependency=*/true,
18132 /*ambiguous_decls=*/NULL,
18133 token->location);
18134 /* If it's not a namespace, issue an error. */
18135 if (namespace_decl == error_mark_node
18136 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18138 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18139 error_at (token->location, "%qD is not a namespace-name", identifier);
18140 cp_parser_error (parser, "expected namespace-name");
18141 namespace_decl = error_mark_node;
18144 return namespace_decl;
18147 /* Parse a namespace-definition.
18149 namespace-definition:
18150 named-namespace-definition
18151 unnamed-namespace-definition
18153 named-namespace-definition:
18154 original-namespace-definition
18155 extension-namespace-definition
18157 original-namespace-definition:
18158 namespace identifier { namespace-body }
18160 extension-namespace-definition:
18161 namespace original-namespace-name { namespace-body }
18163 unnamed-namespace-definition:
18164 namespace { namespace-body } */
18166 static void
18167 cp_parser_namespace_definition (cp_parser* parser)
18169 tree identifier, attribs;
18170 bool has_visibility;
18171 bool is_inline;
18172 cp_token* token;
18173 int nested_definition_count = 0;
18175 cp_ensure_no_omp_declare_simd (parser);
18176 cp_ensure_no_oacc_routine (parser);
18177 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
18179 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18180 is_inline = true;
18181 cp_lexer_consume_token (parser->lexer);
18183 else
18184 is_inline = false;
18186 /* Look for the `namespace' keyword. */
18187 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18189 /* Parse any specified attributes before the identifier. */
18190 attribs = cp_parser_attributes_opt (parser);
18192 /* Get the name of the namespace. We do not attempt to distinguish
18193 between an original-namespace-definition and an
18194 extension-namespace-definition at this point. The semantic
18195 analysis routines are responsible for that. */
18196 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18197 identifier = cp_parser_identifier (parser);
18198 else
18199 identifier = NULL_TREE;
18201 /* Parse any specified attributes after the identifier. */
18202 tree post_ident_attribs = cp_parser_attributes_opt (parser);
18203 if (post_ident_attribs)
18205 if (attribs)
18206 attribs = chainon (attribs, post_ident_attribs);
18207 else
18208 attribs = post_ident_attribs;
18211 /* Start the namespace. */
18212 bool ok = push_namespace (identifier);
18214 /* Parse any nested namespace definition. */
18215 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18217 if (attribs)
18218 error_at (token->location, "a nested namespace definition cannot have attributes");
18219 if (cxx_dialect < cxx1z)
18220 pedwarn (input_location, OPT_Wpedantic,
18221 "nested namespace definitions only available with "
18222 "-std=c++1z or -std=gnu++1z");
18223 if (is_inline)
18224 error_at (token->location, "a nested namespace definition cannot be inline");
18225 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18227 cp_lexer_consume_token (parser->lexer);
18228 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18229 identifier = cp_parser_identifier (parser);
18230 else
18232 cp_parser_error (parser, "nested identifier required");
18233 break;
18235 if (push_namespace (identifier))
18236 ++nested_definition_count;
18240 /* Look for the `{' to validate starting the namespace. */
18241 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
18243 /* "inline namespace" is equivalent to a stub namespace definition
18244 followed by a strong using directive. */
18245 if (is_inline && ok)
18247 tree name_space = current_namespace;
18248 /* Set up namespace association. */
18249 DECL_NAMESPACE_ASSOCIATIONS (name_space)
18250 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
18251 DECL_NAMESPACE_ASSOCIATIONS (name_space));
18252 /* Import the contents of the inline namespace. */
18253 pop_namespace ();
18254 do_using_directive (name_space);
18255 push_namespace (identifier);
18258 has_visibility = handle_namespace_attrs (current_namespace, attribs);
18260 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18262 /* Parse the body of the namespace. */
18263 cp_parser_namespace_body (parser);
18265 if (has_visibility)
18266 pop_visibility (1);
18268 /* Finish the nested namespace definitions. */
18269 while (nested_definition_count--)
18270 pop_namespace ();
18272 /* Finish the namespace. */
18273 if (ok)
18274 pop_namespace ();
18275 /* Look for the final `}'. */
18276 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18279 /* Parse a namespace-body.
18281 namespace-body:
18282 declaration-seq [opt] */
18284 static void
18285 cp_parser_namespace_body (cp_parser* parser)
18287 cp_parser_declaration_seq_opt (parser);
18290 /* Parse a namespace-alias-definition.
18292 namespace-alias-definition:
18293 namespace identifier = qualified-namespace-specifier ; */
18295 static void
18296 cp_parser_namespace_alias_definition (cp_parser* parser)
18298 tree identifier;
18299 tree namespace_specifier;
18301 cp_token *token = cp_lexer_peek_token (parser->lexer);
18303 /* Look for the `namespace' keyword. */
18304 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18305 /* Look for the identifier. */
18306 identifier = cp_parser_identifier (parser);
18307 if (identifier == error_mark_node)
18308 return;
18309 /* Look for the `=' token. */
18310 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18311 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18313 error_at (token->location, "%<namespace%> definition is not allowed here");
18314 /* Skip the definition. */
18315 cp_lexer_consume_token (parser->lexer);
18316 if (cp_parser_skip_to_closing_brace (parser))
18317 cp_lexer_consume_token (parser->lexer);
18318 return;
18320 cp_parser_require (parser, CPP_EQ, RT_EQ);
18321 /* Look for the qualified-namespace-specifier. */
18322 namespace_specifier
18323 = cp_parser_qualified_namespace_specifier (parser);
18324 /* Look for the `;' token. */
18325 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18327 /* Register the alias in the symbol table. */
18328 do_namespace_alias (identifier, namespace_specifier);
18331 /* Parse a qualified-namespace-specifier.
18333 qualified-namespace-specifier:
18334 :: [opt] nested-name-specifier [opt] namespace-name
18336 Returns a NAMESPACE_DECL corresponding to the specified
18337 namespace. */
18339 static tree
18340 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18342 /* Look for the optional `::'. */
18343 cp_parser_global_scope_opt (parser,
18344 /*current_scope_valid_p=*/false);
18346 /* Look for the optional nested-name-specifier. */
18347 cp_parser_nested_name_specifier_opt (parser,
18348 /*typename_keyword_p=*/false,
18349 /*check_dependency_p=*/true,
18350 /*type_p=*/false,
18351 /*is_declaration=*/true);
18353 return cp_parser_namespace_name (parser);
18356 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18357 access declaration.
18359 using-declaration:
18360 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18361 using :: unqualified-id ;
18363 access-declaration:
18364 qualified-id ;
18368 static bool
18369 cp_parser_using_declaration (cp_parser* parser,
18370 bool access_declaration_p)
18372 cp_token *token;
18373 bool typename_p = false;
18374 bool global_scope_p;
18375 tree decl;
18376 tree identifier;
18377 tree qscope;
18378 int oldcount = errorcount;
18379 cp_token *diag_token = NULL;
18381 if (access_declaration_p)
18383 diag_token = cp_lexer_peek_token (parser->lexer);
18384 cp_parser_parse_tentatively (parser);
18386 else
18388 /* Look for the `using' keyword. */
18389 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18391 again:
18392 /* Peek at the next token. */
18393 token = cp_lexer_peek_token (parser->lexer);
18394 /* See if it's `typename'. */
18395 if (token->keyword == RID_TYPENAME)
18397 /* Remember that we've seen it. */
18398 typename_p = true;
18399 /* Consume the `typename' token. */
18400 cp_lexer_consume_token (parser->lexer);
18404 /* Look for the optional global scope qualification. */
18405 global_scope_p
18406 = (cp_parser_global_scope_opt (parser,
18407 /*current_scope_valid_p=*/false)
18408 != NULL_TREE);
18410 /* If we saw `typename', or didn't see `::', then there must be a
18411 nested-name-specifier present. */
18412 if (typename_p || !global_scope_p)
18414 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18415 /*check_dependency_p=*/true,
18416 /*type_p=*/false,
18417 /*is_declaration=*/true);
18418 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18420 cp_parser_skip_to_end_of_block_or_statement (parser);
18421 return false;
18424 /* Otherwise, we could be in either of the two productions. In that
18425 case, treat the nested-name-specifier as optional. */
18426 else
18427 qscope = cp_parser_nested_name_specifier_opt (parser,
18428 /*typename_keyword_p=*/false,
18429 /*check_dependency_p=*/true,
18430 /*type_p=*/false,
18431 /*is_declaration=*/true);
18432 if (!qscope)
18433 qscope = global_namespace;
18434 else if (UNSCOPED_ENUM_P (qscope))
18435 qscope = CP_TYPE_CONTEXT (qscope);
18437 if (access_declaration_p && cp_parser_error_occurred (parser))
18438 /* Something has already gone wrong; there's no need to parse
18439 further. Since an error has occurred, the return value of
18440 cp_parser_parse_definitely will be false, as required. */
18441 return cp_parser_parse_definitely (parser);
18443 token = cp_lexer_peek_token (parser->lexer);
18444 /* Parse the unqualified-id. */
18445 identifier = cp_parser_unqualified_id (parser,
18446 /*template_keyword_p=*/false,
18447 /*check_dependency_p=*/true,
18448 /*declarator_p=*/true,
18449 /*optional_p=*/false);
18451 if (access_declaration_p)
18453 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18454 cp_parser_simulate_error (parser);
18455 if (!cp_parser_parse_definitely (parser))
18456 return false;
18458 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18460 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18461 if (cxx_dialect < cxx1z
18462 && !in_system_header_at (ell->location))
18463 pedwarn (ell->location, 0,
18464 "pack expansion in using-declaration only available "
18465 "with -std=c++1z or -std=gnu++1z");
18466 qscope = make_pack_expansion (qscope);
18469 /* The function we call to handle a using-declaration is different
18470 depending on what scope we are in. */
18471 if (qscope == error_mark_node || identifier == error_mark_node)
18473 else if (!identifier_p (identifier)
18474 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18475 /* [namespace.udecl]
18477 A using declaration shall not name a template-id. */
18478 error_at (token->location,
18479 "a template-id may not appear in a using-declaration");
18480 else
18482 if (at_class_scope_p ())
18484 /* Create the USING_DECL. */
18485 decl = do_class_using_decl (qscope, identifier);
18487 if (decl && typename_p)
18488 USING_DECL_TYPENAME_P (decl) = 1;
18490 if (check_for_bare_parameter_packs (decl))
18492 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18493 return false;
18495 else
18496 /* Add it to the list of members in this class. */
18497 finish_member_declaration (decl);
18499 else
18501 decl = cp_parser_lookup_name_simple (parser,
18502 identifier,
18503 token->location);
18504 if (decl == error_mark_node)
18505 cp_parser_name_lookup_error (parser, identifier,
18506 decl, NLE_NULL,
18507 token->location);
18508 else if (check_for_bare_parameter_packs (decl))
18510 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18511 return false;
18513 else if (!at_namespace_scope_p ())
18514 do_local_using_decl (decl, qscope, identifier);
18515 else
18516 do_toplevel_using_decl (decl, qscope, identifier);
18520 if (!access_declaration_p
18521 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18523 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18524 if (cxx_dialect < cxx1z)
18525 pedwarn (comma->location, 0,
18526 "comma-separated list in using-declaration only available "
18527 "with -std=c++1z or -std=gnu++1z");
18528 goto again;
18531 /* Look for the final `;'. */
18532 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18534 if (access_declaration_p && errorcount == oldcount)
18535 warning_at (diag_token->location, OPT_Wdeprecated,
18536 "access declarations are deprecated "
18537 "in favour of using-declarations; "
18538 "suggestion: add the %<using%> keyword");
18540 return true;
18543 /* Parse an alias-declaration.
18545 alias-declaration:
18546 using identifier attribute-specifier-seq [opt] = type-id */
18548 static tree
18549 cp_parser_alias_declaration (cp_parser* parser)
18551 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18552 location_t id_location;
18553 cp_declarator *declarator;
18554 cp_decl_specifier_seq decl_specs;
18555 bool member_p;
18556 const char *saved_message = NULL;
18558 /* Look for the `using' keyword. */
18559 cp_token *using_token
18560 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18561 if (using_token == NULL)
18562 return error_mark_node;
18564 id_location = cp_lexer_peek_token (parser->lexer)->location;
18565 id = cp_parser_identifier (parser);
18566 if (id == error_mark_node)
18567 return error_mark_node;
18569 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18570 attributes = cp_parser_attributes_opt (parser);
18571 if (attributes == error_mark_node)
18572 return error_mark_node;
18574 cp_parser_require (parser, CPP_EQ, RT_EQ);
18576 if (cp_parser_error_occurred (parser))
18577 return error_mark_node;
18579 cp_parser_commit_to_tentative_parse (parser);
18581 /* Now we are going to parse the type-id of the declaration. */
18584 [dcl.type]/3 says:
18586 "A type-specifier-seq shall not define a class or enumeration
18587 unless it appears in the type-id of an alias-declaration (7.1.3) that
18588 is not the declaration of a template-declaration."
18590 In other words, if we currently are in an alias template, the
18591 type-id should not define a type.
18593 So let's set parser->type_definition_forbidden_message in that
18594 case; cp_parser_check_type_definition (called by
18595 cp_parser_class_specifier) will then emit an error if a type is
18596 defined in the type-id. */
18597 if (parser->num_template_parameter_lists)
18599 saved_message = parser->type_definition_forbidden_message;
18600 parser->type_definition_forbidden_message =
18601 G_("types may not be defined in alias template declarations");
18604 type = cp_parser_type_id (parser);
18606 /* Restore the error message if need be. */
18607 if (parser->num_template_parameter_lists)
18608 parser->type_definition_forbidden_message = saved_message;
18610 if (type == error_mark_node
18611 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18613 cp_parser_skip_to_end_of_block_or_statement (parser);
18614 return error_mark_node;
18617 /* A typedef-name can also be introduced by an alias-declaration. The
18618 identifier following the using keyword becomes a typedef-name. It has
18619 the same semantics as if it were introduced by the typedef
18620 specifier. In particular, it does not define a new type and it shall
18621 not appear in the type-id. */
18623 clear_decl_specs (&decl_specs);
18624 decl_specs.type = type;
18625 if (attributes != NULL_TREE)
18627 decl_specs.attributes = attributes;
18628 set_and_check_decl_spec_loc (&decl_specs,
18629 ds_attribute,
18630 attrs_token);
18632 set_and_check_decl_spec_loc (&decl_specs,
18633 ds_typedef,
18634 using_token);
18635 set_and_check_decl_spec_loc (&decl_specs,
18636 ds_alias,
18637 using_token);
18639 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18640 declarator->id_loc = id_location;
18642 member_p = at_class_scope_p ();
18643 if (member_p)
18644 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18645 NULL_TREE, attributes);
18646 else
18647 decl = start_decl (declarator, &decl_specs, 0,
18648 attributes, NULL_TREE, &pushed_scope);
18649 if (decl == error_mark_node)
18650 return decl;
18652 // Attach constraints to the alias declaration.
18653 if (flag_concepts && current_template_parms)
18655 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18656 tree constr = build_constraints (reqs, NULL_TREE);
18657 set_constraints (decl, constr);
18660 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18662 if (pushed_scope)
18663 pop_scope (pushed_scope);
18665 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18666 added into the symbol table; otherwise, return the TYPE_DECL. */
18667 if (DECL_LANG_SPECIFIC (decl)
18668 && DECL_TEMPLATE_INFO (decl)
18669 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18671 decl = DECL_TI_TEMPLATE (decl);
18672 if (member_p)
18673 check_member_template (decl);
18676 return decl;
18679 /* Parse a using-directive.
18681 using-directive:
18682 using namespace :: [opt] nested-name-specifier [opt]
18683 namespace-name ; */
18685 static void
18686 cp_parser_using_directive (cp_parser* parser)
18688 tree namespace_decl;
18689 tree attribs;
18691 /* Look for the `using' keyword. */
18692 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18693 /* And the `namespace' keyword. */
18694 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18695 /* Look for the optional `::' operator. */
18696 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18697 /* And the optional nested-name-specifier. */
18698 cp_parser_nested_name_specifier_opt (parser,
18699 /*typename_keyword_p=*/false,
18700 /*check_dependency_p=*/true,
18701 /*type_p=*/false,
18702 /*is_declaration=*/true);
18703 /* Get the namespace being used. */
18704 namespace_decl = cp_parser_namespace_name (parser);
18705 /* And any specified attributes. */
18706 attribs = cp_parser_attributes_opt (parser);
18707 /* Update the symbol table. */
18708 parse_using_directive (namespace_decl, attribs);
18709 /* Look for the final `;'. */
18710 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18713 /* Parse an asm-definition.
18715 asm-definition:
18716 asm ( string-literal ) ;
18718 GNU Extension:
18720 asm-definition:
18721 asm volatile [opt] ( string-literal ) ;
18722 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18723 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18724 : asm-operand-list [opt] ) ;
18725 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18726 : asm-operand-list [opt]
18727 : asm-clobber-list [opt] ) ;
18728 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18729 : asm-clobber-list [opt]
18730 : asm-goto-list ) ; */
18732 static void
18733 cp_parser_asm_definition (cp_parser* parser)
18735 tree string;
18736 tree outputs = NULL_TREE;
18737 tree inputs = NULL_TREE;
18738 tree clobbers = NULL_TREE;
18739 tree labels = NULL_TREE;
18740 tree asm_stmt;
18741 bool volatile_p = false;
18742 bool extended_p = false;
18743 bool invalid_inputs_p = false;
18744 bool invalid_outputs_p = false;
18745 bool goto_p = false;
18746 required_token missing = RT_NONE;
18748 /* Look for the `asm' keyword. */
18749 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18751 if (parser->in_function_body
18752 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18754 error ("%<asm%> in %<constexpr%> function");
18755 cp_function_chain->invalid_constexpr = true;
18758 /* See if the next token is `volatile'. */
18759 if (cp_parser_allow_gnu_extensions_p (parser)
18760 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18762 /* Remember that we saw the `volatile' keyword. */
18763 volatile_p = true;
18764 /* Consume the token. */
18765 cp_lexer_consume_token (parser->lexer);
18767 if (cp_parser_allow_gnu_extensions_p (parser)
18768 && parser->in_function_body
18769 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18771 /* Remember that we saw the `goto' keyword. */
18772 goto_p = true;
18773 /* Consume the token. */
18774 cp_lexer_consume_token (parser->lexer);
18776 /* Look for the opening `('. */
18777 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18778 return;
18779 /* Look for the string. */
18780 string = cp_parser_string_literal (parser, false, false);
18781 if (string == error_mark_node)
18783 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18784 /*consume_paren=*/true);
18785 return;
18788 /* If we're allowing GNU extensions, check for the extended assembly
18789 syntax. Unfortunately, the `:' tokens need not be separated by
18790 a space in C, and so, for compatibility, we tolerate that here
18791 too. Doing that means that we have to treat the `::' operator as
18792 two `:' tokens. */
18793 if (cp_parser_allow_gnu_extensions_p (parser)
18794 && parser->in_function_body
18795 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18796 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18798 bool inputs_p = false;
18799 bool clobbers_p = false;
18800 bool labels_p = false;
18802 /* The extended syntax was used. */
18803 extended_p = true;
18805 /* Look for outputs. */
18806 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18808 /* Consume the `:'. */
18809 cp_lexer_consume_token (parser->lexer);
18810 /* Parse the output-operands. */
18811 if (cp_lexer_next_token_is_not (parser->lexer,
18812 CPP_COLON)
18813 && cp_lexer_next_token_is_not (parser->lexer,
18814 CPP_SCOPE)
18815 && cp_lexer_next_token_is_not (parser->lexer,
18816 CPP_CLOSE_PAREN)
18817 && !goto_p)
18819 outputs = cp_parser_asm_operand_list (parser);
18820 if (outputs == error_mark_node)
18821 invalid_outputs_p = true;
18824 /* If the next token is `::', there are no outputs, and the
18825 next token is the beginning of the inputs. */
18826 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18827 /* The inputs are coming next. */
18828 inputs_p = true;
18830 /* Look for inputs. */
18831 if (inputs_p
18832 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18834 /* Consume the `:' or `::'. */
18835 cp_lexer_consume_token (parser->lexer);
18836 /* Parse the output-operands. */
18837 if (cp_lexer_next_token_is_not (parser->lexer,
18838 CPP_COLON)
18839 && cp_lexer_next_token_is_not (parser->lexer,
18840 CPP_SCOPE)
18841 && cp_lexer_next_token_is_not (parser->lexer,
18842 CPP_CLOSE_PAREN))
18844 inputs = cp_parser_asm_operand_list (parser);
18845 if (inputs == error_mark_node)
18846 invalid_inputs_p = true;
18849 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18850 /* The clobbers are coming next. */
18851 clobbers_p = true;
18853 /* Look for clobbers. */
18854 if (clobbers_p
18855 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18857 clobbers_p = true;
18858 /* Consume the `:' or `::'. */
18859 cp_lexer_consume_token (parser->lexer);
18860 /* Parse the clobbers. */
18861 if (cp_lexer_next_token_is_not (parser->lexer,
18862 CPP_COLON)
18863 && cp_lexer_next_token_is_not (parser->lexer,
18864 CPP_CLOSE_PAREN))
18865 clobbers = cp_parser_asm_clobber_list (parser);
18867 else if (goto_p
18868 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18869 /* The labels are coming next. */
18870 labels_p = true;
18872 /* Look for labels. */
18873 if (labels_p
18874 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18876 labels_p = true;
18877 /* Consume the `:' or `::'. */
18878 cp_lexer_consume_token (parser->lexer);
18879 /* Parse the labels. */
18880 labels = cp_parser_asm_label_list (parser);
18883 if (goto_p && !labels_p)
18884 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18886 else if (goto_p)
18887 missing = RT_COLON_SCOPE;
18889 /* Look for the closing `)'. */
18890 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18891 missing ? missing : RT_CLOSE_PAREN))
18892 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18893 /*consume_paren=*/true);
18894 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18896 if (!invalid_inputs_p && !invalid_outputs_p)
18898 /* Create the ASM_EXPR. */
18899 if (parser->in_function_body)
18901 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18902 inputs, clobbers, labels);
18903 /* If the extended syntax was not used, mark the ASM_EXPR. */
18904 if (!extended_p)
18906 tree temp = asm_stmt;
18907 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18908 temp = TREE_OPERAND (temp, 0);
18910 ASM_INPUT_P (temp) = 1;
18913 else
18914 symtab->finalize_toplevel_asm (string);
18918 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18919 type that comes from the decl-specifier-seq. */
18921 static tree
18922 strip_declarator_types (tree type, cp_declarator *declarator)
18924 for (cp_declarator *d = declarator; d;)
18925 switch (d->kind)
18927 case cdk_id:
18928 case cdk_decomp:
18929 case cdk_error:
18930 d = NULL;
18931 break;
18933 default:
18934 if (TYPE_PTRMEMFUNC_P (type))
18935 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18936 type = TREE_TYPE (type);
18937 d = d->declarator;
18938 break;
18941 return type;
18944 /* Declarators [gram.dcl.decl] */
18946 /* Parse an init-declarator.
18948 init-declarator:
18949 declarator initializer [opt]
18951 GNU Extension:
18953 init-declarator:
18954 declarator asm-specification [opt] attributes [opt] initializer [opt]
18956 function-definition:
18957 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18958 function-body
18959 decl-specifier-seq [opt] declarator function-try-block
18961 GNU Extension:
18963 function-definition:
18964 __extension__ function-definition
18966 TM Extension:
18968 function-definition:
18969 decl-specifier-seq [opt] declarator function-transaction-block
18971 The DECL_SPECIFIERS apply to this declarator. Returns a
18972 representation of the entity declared. If MEMBER_P is TRUE, then
18973 this declarator appears in a class scope. The new DECL created by
18974 this declarator is returned.
18976 The CHECKS are access checks that should be performed once we know
18977 what entity is being declared (and, therefore, what classes have
18978 befriended it).
18980 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18981 for a function-definition here as well. If the declarator is a
18982 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18983 be TRUE upon return. By that point, the function-definition will
18984 have been completely parsed.
18986 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18987 is FALSE.
18989 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18990 parsed declaration if it is an uninitialized single declarator not followed
18991 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18992 if present, will not be consumed. If returned, this declarator will be
18993 created with SD_INITIALIZED but will not call cp_finish_decl.
18995 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18996 and there is an initializer, the pointed location_t is set to the
18997 location of the '=' or `(', or '{' in C++11 token introducing the
18998 initializer. */
19000 static tree
19001 cp_parser_init_declarator (cp_parser* parser,
19002 cp_decl_specifier_seq *decl_specifiers,
19003 vec<deferred_access_check, va_gc> *checks,
19004 bool function_definition_allowed_p,
19005 bool member_p,
19006 int declares_class_or_enum,
19007 bool* function_definition_p,
19008 tree* maybe_range_for_decl,
19009 location_t* init_loc,
19010 tree* auto_result)
19012 cp_token *token = NULL, *asm_spec_start_token = NULL,
19013 *attributes_start_token = NULL;
19014 cp_declarator *declarator;
19015 tree prefix_attributes;
19016 tree attributes = NULL;
19017 tree asm_specification;
19018 tree initializer;
19019 tree decl = NULL_TREE;
19020 tree scope;
19021 int is_initialized;
19022 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19023 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19024 "(...)". */
19025 enum cpp_ttype initialization_kind;
19026 bool is_direct_init = false;
19027 bool is_non_constant_init;
19028 int ctor_dtor_or_conv_p;
19029 bool friend_p = cp_parser_friend_p (decl_specifiers);
19030 tree pushed_scope = NULL_TREE;
19031 bool range_for_decl_p = false;
19032 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19033 location_t tmp_init_loc = UNKNOWN_LOCATION;
19035 /* Gather the attributes that were provided with the
19036 decl-specifiers. */
19037 prefix_attributes = decl_specifiers->attributes;
19039 /* Assume that this is not the declarator for a function
19040 definition. */
19041 if (function_definition_p)
19042 *function_definition_p = false;
19044 /* Default arguments are only permitted for function parameters. */
19045 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19046 parser->default_arg_ok_p = false;
19048 /* Defer access checks while parsing the declarator; we cannot know
19049 what names are accessible until we know what is being
19050 declared. */
19051 resume_deferring_access_checks ();
19053 token = cp_lexer_peek_token (parser->lexer);
19055 /* Parse the declarator. */
19056 declarator
19057 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19058 &ctor_dtor_or_conv_p,
19059 /*parenthesized_p=*/NULL,
19060 member_p, friend_p);
19061 /* Gather up the deferred checks. */
19062 stop_deferring_access_checks ();
19064 parser->default_arg_ok_p = saved_default_arg_ok_p;
19066 /* If the DECLARATOR was erroneous, there's no need to go
19067 further. */
19068 if (declarator == cp_error_declarator)
19069 return error_mark_node;
19071 /* Check that the number of template-parameter-lists is OK. */
19072 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19073 token->location))
19074 return error_mark_node;
19076 if (declares_class_or_enum & 2)
19077 cp_parser_check_for_definition_in_return_type (declarator,
19078 decl_specifiers->type,
19079 decl_specifiers->locations[ds_type_spec]);
19081 /* Figure out what scope the entity declared by the DECLARATOR is
19082 located in. `grokdeclarator' sometimes changes the scope, so
19083 we compute it now. */
19084 scope = get_scope_of_declarator (declarator);
19086 /* Perform any lookups in the declared type which were thought to be
19087 dependent, but are not in the scope of the declarator. */
19088 decl_specifiers->type
19089 = maybe_update_decl_type (decl_specifiers->type, scope);
19091 /* If we're allowing GNU extensions, look for an
19092 asm-specification. */
19093 if (cp_parser_allow_gnu_extensions_p (parser))
19095 /* Look for an asm-specification. */
19096 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19097 asm_specification = cp_parser_asm_specification_opt (parser);
19099 else
19100 asm_specification = NULL_TREE;
19102 /* Look for attributes. */
19103 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19104 attributes = cp_parser_attributes_opt (parser);
19106 /* Peek at the next token. */
19107 token = cp_lexer_peek_token (parser->lexer);
19109 bool bogus_implicit_tmpl = false;
19111 if (function_declarator_p (declarator))
19113 /* Handle C++17 deduction guides. */
19114 if (!decl_specifiers->type
19115 && ctor_dtor_or_conv_p <= 0
19116 && cxx_dialect >= cxx1z)
19118 cp_declarator *id = get_id_declarator (declarator);
19119 tree name = id->u.id.unqualified_name;
19120 parser->scope = id->u.id.qualifying_scope;
19121 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19122 if (tmpl
19123 && (DECL_CLASS_TEMPLATE_P (tmpl)
19124 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19126 id->u.id.unqualified_name = dguide_name (tmpl);
19127 id->u.id.sfk = sfk_deduction_guide;
19128 ctor_dtor_or_conv_p = 1;
19132 /* Check to see if the token indicates the start of a
19133 function-definition. */
19134 if (cp_parser_token_starts_function_definition_p (token))
19136 if (!function_definition_allowed_p)
19138 /* If a function-definition should not appear here, issue an
19139 error message. */
19140 cp_parser_error (parser,
19141 "a function-definition is not allowed here");
19142 return error_mark_node;
19145 location_t func_brace_location
19146 = cp_lexer_peek_token (parser->lexer)->location;
19148 /* Neither attributes nor an asm-specification are allowed
19149 on a function-definition. */
19150 if (asm_specification)
19151 error_at (asm_spec_start_token->location,
19152 "an asm-specification is not allowed "
19153 "on a function-definition");
19154 if (attributes)
19155 error_at (attributes_start_token->location,
19156 "attributes are not allowed "
19157 "on a function-definition");
19158 /* This is a function-definition. */
19159 *function_definition_p = true;
19161 /* Parse the function definition. */
19162 if (member_p)
19163 decl = cp_parser_save_member_function_body (parser,
19164 decl_specifiers,
19165 declarator,
19166 prefix_attributes);
19167 else
19168 decl =
19169 (cp_parser_function_definition_from_specifiers_and_declarator
19170 (parser, decl_specifiers, prefix_attributes, declarator));
19172 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19174 /* This is where the prologue starts... */
19175 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19176 = func_brace_location;
19179 return decl;
19182 else if (parser->fully_implicit_function_template_p)
19184 /* A non-template declaration involving a function parameter list
19185 containing an implicit template parameter will be made into a
19186 template. If the resulting declaration is not going to be an
19187 actual function then finish the template scope here to prevent it.
19188 An error message will be issued once we have a decl to talk about.
19190 FIXME probably we should do type deduction rather than create an
19191 implicit template, but the standard currently doesn't allow it. */
19192 bogus_implicit_tmpl = true;
19193 finish_fully_implicit_template (parser, NULL_TREE);
19196 /* [dcl.dcl]
19198 Only in function declarations for constructors, destructors, type
19199 conversions, and deduction guides can the decl-specifier-seq be omitted.
19201 We explicitly postpone this check past the point where we handle
19202 function-definitions because we tolerate function-definitions
19203 that are missing their return types in some modes. */
19204 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19206 cp_parser_error (parser,
19207 "expected constructor, destructor, or type conversion");
19208 return error_mark_node;
19211 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19212 if (token->type == CPP_EQ
19213 || token->type == CPP_OPEN_PAREN
19214 || token->type == CPP_OPEN_BRACE)
19216 is_initialized = SD_INITIALIZED;
19217 initialization_kind = token->type;
19218 if (maybe_range_for_decl)
19219 *maybe_range_for_decl = error_mark_node;
19220 tmp_init_loc = token->location;
19221 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19222 *init_loc = tmp_init_loc;
19224 if (token->type == CPP_EQ
19225 && function_declarator_p (declarator))
19227 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19228 if (t2->keyword == RID_DEFAULT)
19229 is_initialized = SD_DEFAULTED;
19230 else if (t2->keyword == RID_DELETE)
19231 is_initialized = SD_DELETED;
19234 else
19236 /* If the init-declarator isn't initialized and isn't followed by a
19237 `,' or `;', it's not a valid init-declarator. */
19238 if (token->type != CPP_COMMA
19239 && token->type != CPP_SEMICOLON)
19241 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19242 range_for_decl_p = true;
19243 else
19245 if (!maybe_range_for_decl)
19246 cp_parser_error (parser, "expected initializer");
19247 return error_mark_node;
19250 is_initialized = SD_UNINITIALIZED;
19251 initialization_kind = CPP_EOF;
19254 /* Because start_decl has side-effects, we should only call it if we
19255 know we're going ahead. By this point, we know that we cannot
19256 possibly be looking at any other construct. */
19257 cp_parser_commit_to_tentative_parse (parser);
19259 /* Enter the newly declared entry in the symbol table. If we're
19260 processing a declaration in a class-specifier, we wait until
19261 after processing the initializer. */
19262 if (!member_p)
19264 if (parser->in_unbraced_linkage_specification_p)
19265 decl_specifiers->storage_class = sc_extern;
19266 decl = start_decl (declarator, decl_specifiers,
19267 range_for_decl_p? SD_INITIALIZED : is_initialized,
19268 attributes, prefix_attributes, &pushed_scope);
19269 cp_finalize_omp_declare_simd (parser, decl);
19270 cp_finalize_oacc_routine (parser, decl, false);
19271 /* Adjust location of decl if declarator->id_loc is more appropriate:
19272 set, and decl wasn't merged with another decl, in which case its
19273 location would be different from input_location, and more accurate. */
19274 if (DECL_P (decl)
19275 && declarator->id_loc != UNKNOWN_LOCATION
19276 && DECL_SOURCE_LOCATION (decl) == input_location)
19277 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19279 else if (scope)
19280 /* Enter the SCOPE. That way unqualified names appearing in the
19281 initializer will be looked up in SCOPE. */
19282 pushed_scope = push_scope (scope);
19284 /* Perform deferred access control checks, now that we know in which
19285 SCOPE the declared entity resides. */
19286 if (!member_p && decl)
19288 tree saved_current_function_decl = NULL_TREE;
19290 /* If the entity being declared is a function, pretend that we
19291 are in its scope. If it is a `friend', it may have access to
19292 things that would not otherwise be accessible. */
19293 if (TREE_CODE (decl) == FUNCTION_DECL)
19295 saved_current_function_decl = current_function_decl;
19296 current_function_decl = decl;
19299 /* Perform access checks for template parameters. */
19300 cp_parser_perform_template_parameter_access_checks (checks);
19302 /* Perform the access control checks for the declarator and the
19303 decl-specifiers. */
19304 perform_deferred_access_checks (tf_warning_or_error);
19306 /* Restore the saved value. */
19307 if (TREE_CODE (decl) == FUNCTION_DECL)
19308 current_function_decl = saved_current_function_decl;
19311 /* Parse the initializer. */
19312 initializer = NULL_TREE;
19313 is_direct_init = false;
19314 is_non_constant_init = true;
19315 if (is_initialized)
19317 if (function_declarator_p (declarator))
19319 if (initialization_kind == CPP_EQ)
19320 initializer = cp_parser_pure_specifier (parser);
19321 else
19323 /* If the declaration was erroneous, we don't really
19324 know what the user intended, so just silently
19325 consume the initializer. */
19326 if (decl != error_mark_node)
19327 error_at (tmp_init_loc, "initializer provided for function");
19328 cp_parser_skip_to_closing_parenthesis (parser,
19329 /*recovering=*/true,
19330 /*or_comma=*/false,
19331 /*consume_paren=*/true);
19334 else
19336 /* We want to record the extra mangling scope for in-class
19337 initializers of class members and initializers of static data
19338 member templates. The former involves deferring
19339 parsing of the initializer until end of class as with default
19340 arguments. So right here we only handle the latter. */
19341 if (!member_p && processing_template_decl)
19342 start_lambda_scope (decl);
19343 initializer = cp_parser_initializer (parser,
19344 &is_direct_init,
19345 &is_non_constant_init);
19346 if (!member_p && processing_template_decl)
19347 finish_lambda_scope ();
19348 if (initializer == error_mark_node)
19349 cp_parser_skip_to_end_of_statement (parser);
19353 /* The old parser allows attributes to appear after a parenthesized
19354 initializer. Mark Mitchell proposed removing this functionality
19355 on the GCC mailing lists on 2002-08-13. This parser accepts the
19356 attributes -- but ignores them. */
19357 if (cp_parser_allow_gnu_extensions_p (parser)
19358 && initialization_kind == CPP_OPEN_PAREN)
19359 if (cp_parser_attributes_opt (parser))
19360 warning (OPT_Wattributes,
19361 "attributes after parenthesized initializer ignored");
19363 /* And now complain about a non-function implicit template. */
19364 if (bogus_implicit_tmpl && decl != error_mark_node)
19365 error_at (DECL_SOURCE_LOCATION (decl),
19366 "non-function %qD declared as implicit template", decl);
19368 /* For an in-class declaration, use `grokfield' to create the
19369 declaration. */
19370 if (member_p)
19372 if (pushed_scope)
19374 pop_scope (pushed_scope);
19375 pushed_scope = NULL_TREE;
19377 decl = grokfield (declarator, decl_specifiers,
19378 initializer, !is_non_constant_init,
19379 /*asmspec=*/NULL_TREE,
19380 chainon (attributes, prefix_attributes));
19381 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19382 cp_parser_save_default_args (parser, decl);
19383 cp_finalize_omp_declare_simd (parser, decl);
19384 cp_finalize_oacc_routine (parser, decl, false);
19387 /* Finish processing the declaration. But, skip member
19388 declarations. */
19389 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19391 cp_finish_decl (decl,
19392 initializer, !is_non_constant_init,
19393 asm_specification,
19394 /* If the initializer is in parentheses, then this is
19395 a direct-initialization, which means that an
19396 `explicit' constructor is OK. Otherwise, an
19397 `explicit' constructor cannot be used. */
19398 ((is_direct_init || !is_initialized)
19399 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19401 else if ((cxx_dialect != cxx98) && friend_p
19402 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19403 /* Core issue #226 (C++0x only): A default template-argument
19404 shall not be specified in a friend class template
19405 declaration. */
19406 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19407 /*is_partial=*/false, /*is_friend_decl=*/1);
19409 if (!friend_p && pushed_scope)
19410 pop_scope (pushed_scope);
19412 if (function_declarator_p (declarator)
19413 && parser->fully_implicit_function_template_p)
19415 if (member_p)
19416 decl = finish_fully_implicit_template (parser, decl);
19417 else
19418 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19421 if (auto_result && is_initialized && decl_specifiers->type
19422 && type_uses_auto (decl_specifiers->type))
19423 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19425 return decl;
19428 /* Parse a declarator.
19430 declarator:
19431 direct-declarator
19432 ptr-operator declarator
19434 abstract-declarator:
19435 ptr-operator abstract-declarator [opt]
19436 direct-abstract-declarator
19438 GNU Extensions:
19440 declarator:
19441 attributes [opt] direct-declarator
19442 attributes [opt] ptr-operator declarator
19444 abstract-declarator:
19445 attributes [opt] ptr-operator abstract-declarator [opt]
19446 attributes [opt] direct-abstract-declarator
19448 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19449 detect constructors, destructors, deduction guides, or conversion operators.
19450 It is set to -1 if the declarator is a name, and +1 if it is a
19451 function. Otherwise it is set to zero. Usually you just want to
19452 test for >0, but internally the negative value is used.
19454 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19455 a decl-specifier-seq unless it declares a constructor, destructor,
19456 or conversion. It might seem that we could check this condition in
19457 semantic analysis, rather than parsing, but that makes it difficult
19458 to handle something like `f()'. We want to notice that there are
19459 no decl-specifiers, and therefore realize that this is an
19460 expression, not a declaration.)
19462 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19463 the declarator is a direct-declarator of the form "(...)".
19465 MEMBER_P is true iff this declarator is a member-declarator.
19467 FRIEND_P is true iff this declarator is a friend. */
19469 static cp_declarator *
19470 cp_parser_declarator (cp_parser* parser,
19471 cp_parser_declarator_kind dcl_kind,
19472 int* ctor_dtor_or_conv_p,
19473 bool* parenthesized_p,
19474 bool member_p, bool friend_p)
19476 cp_declarator *declarator;
19477 enum tree_code code;
19478 cp_cv_quals cv_quals;
19479 tree class_type;
19480 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19482 /* Assume this is not a constructor, destructor, or type-conversion
19483 operator. */
19484 if (ctor_dtor_or_conv_p)
19485 *ctor_dtor_or_conv_p = 0;
19487 if (cp_parser_allow_gnu_extensions_p (parser))
19488 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19490 /* Check for the ptr-operator production. */
19491 cp_parser_parse_tentatively (parser);
19492 /* Parse the ptr-operator. */
19493 code = cp_parser_ptr_operator (parser,
19494 &class_type,
19495 &cv_quals,
19496 &std_attributes);
19498 /* If that worked, then we have a ptr-operator. */
19499 if (cp_parser_parse_definitely (parser))
19501 /* If a ptr-operator was found, then this declarator was not
19502 parenthesized. */
19503 if (parenthesized_p)
19504 *parenthesized_p = true;
19505 /* The dependent declarator is optional if we are parsing an
19506 abstract-declarator. */
19507 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19508 cp_parser_parse_tentatively (parser);
19510 /* Parse the dependent declarator. */
19511 declarator = cp_parser_declarator (parser, dcl_kind,
19512 /*ctor_dtor_or_conv_p=*/NULL,
19513 /*parenthesized_p=*/NULL,
19514 /*member_p=*/false,
19515 friend_p);
19517 /* If we are parsing an abstract-declarator, we must handle the
19518 case where the dependent declarator is absent. */
19519 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19520 && !cp_parser_parse_definitely (parser))
19521 declarator = NULL;
19523 declarator = cp_parser_make_indirect_declarator
19524 (code, class_type, cv_quals, declarator, std_attributes);
19526 /* Everything else is a direct-declarator. */
19527 else
19529 if (parenthesized_p)
19530 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19531 CPP_OPEN_PAREN);
19532 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19533 ctor_dtor_or_conv_p,
19534 member_p, friend_p);
19537 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19538 declarator->attributes = gnu_attributes;
19539 return declarator;
19542 /* Parse a direct-declarator or direct-abstract-declarator.
19544 direct-declarator:
19545 declarator-id
19546 direct-declarator ( parameter-declaration-clause )
19547 cv-qualifier-seq [opt]
19548 ref-qualifier [opt]
19549 exception-specification [opt]
19550 direct-declarator [ constant-expression [opt] ]
19551 ( declarator )
19553 direct-abstract-declarator:
19554 direct-abstract-declarator [opt]
19555 ( parameter-declaration-clause )
19556 cv-qualifier-seq [opt]
19557 ref-qualifier [opt]
19558 exception-specification [opt]
19559 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19560 ( abstract-declarator )
19562 Returns a representation of the declarator. DCL_KIND is
19563 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19564 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19565 we are parsing a direct-declarator. It is
19566 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19567 of ambiguity we prefer an abstract declarator, as per
19568 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19569 as for cp_parser_declarator. */
19571 static cp_declarator *
19572 cp_parser_direct_declarator (cp_parser* parser,
19573 cp_parser_declarator_kind dcl_kind,
19574 int* ctor_dtor_or_conv_p,
19575 bool member_p, bool friend_p)
19577 cp_token *token;
19578 cp_declarator *declarator = NULL;
19579 tree scope = NULL_TREE;
19580 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19581 bool saved_in_declarator_p = parser->in_declarator_p;
19582 bool first = true;
19583 tree pushed_scope = NULL_TREE;
19585 while (true)
19587 /* Peek at the next token. */
19588 token = cp_lexer_peek_token (parser->lexer);
19589 if (token->type == CPP_OPEN_PAREN)
19591 /* This is either a parameter-declaration-clause, or a
19592 parenthesized declarator. When we know we are parsing a
19593 named declarator, it must be a parenthesized declarator
19594 if FIRST is true. For instance, `(int)' is a
19595 parameter-declaration-clause, with an omitted
19596 direct-abstract-declarator. But `((*))', is a
19597 parenthesized abstract declarator. Finally, when T is a
19598 template parameter `(T)' is a
19599 parameter-declaration-clause, and not a parenthesized
19600 named declarator.
19602 We first try and parse a parameter-declaration-clause,
19603 and then try a nested declarator (if FIRST is true).
19605 It is not an error for it not to be a
19606 parameter-declaration-clause, even when FIRST is
19607 false. Consider,
19609 int i (int);
19610 int i (3);
19612 The first is the declaration of a function while the
19613 second is the definition of a variable, including its
19614 initializer.
19616 Having seen only the parenthesis, we cannot know which of
19617 these two alternatives should be selected. Even more
19618 complex are examples like:
19620 int i (int (a));
19621 int i (int (3));
19623 The former is a function-declaration; the latter is a
19624 variable initialization.
19626 Thus again, we try a parameter-declaration-clause, and if
19627 that fails, we back out and return. */
19629 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19631 tree params;
19632 bool is_declarator = false;
19634 /* In a member-declarator, the only valid interpretation
19635 of a parenthesis is the start of a
19636 parameter-declaration-clause. (It is invalid to
19637 initialize a static data member with a parenthesized
19638 initializer; only the "=" form of initialization is
19639 permitted.) */
19640 if (!member_p)
19641 cp_parser_parse_tentatively (parser);
19643 /* Consume the `('. */
19644 cp_lexer_consume_token (parser->lexer);
19645 if (first)
19647 /* If this is going to be an abstract declarator, we're
19648 in a declarator and we can't have default args. */
19649 parser->default_arg_ok_p = false;
19650 parser->in_declarator_p = true;
19653 begin_scope (sk_function_parms, NULL_TREE);
19655 /* Parse the parameter-declaration-clause. */
19656 params = cp_parser_parameter_declaration_clause (parser);
19658 /* Consume the `)'. */
19659 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19661 /* If all went well, parse the cv-qualifier-seq,
19662 ref-qualifier and the exception-specification. */
19663 if (member_p || cp_parser_parse_definitely (parser))
19665 cp_cv_quals cv_quals;
19666 cp_virt_specifiers virt_specifiers;
19667 cp_ref_qualifier ref_qual;
19668 tree exception_specification;
19669 tree late_return;
19670 tree attrs;
19671 bool memfn = (member_p || (pushed_scope
19672 && CLASS_TYPE_P (pushed_scope)));
19674 is_declarator = true;
19676 if (ctor_dtor_or_conv_p)
19677 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19678 first = false;
19680 /* Parse the cv-qualifier-seq. */
19681 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19682 /* Parse the ref-qualifier. */
19683 ref_qual = cp_parser_ref_qualifier_opt (parser);
19684 /* Parse the tx-qualifier. */
19685 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19686 /* And the exception-specification. */
19687 exception_specification
19688 = cp_parser_exception_specification_opt (parser);
19690 attrs = cp_parser_std_attribute_spec_seq (parser);
19692 /* In here, we handle cases where attribute is used after
19693 the function declaration. For example:
19694 void func (int x) __attribute__((vector(..))); */
19695 tree gnu_attrs = NULL_TREE;
19696 if (flag_cilkplus
19697 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19699 cp_parser_parse_tentatively (parser);
19700 tree attr = cp_parser_gnu_attributes_opt (parser);
19701 if (cp_lexer_next_token_is_not (parser->lexer,
19702 CPP_SEMICOLON)
19703 && cp_lexer_next_token_is_not (parser->lexer,
19704 CPP_OPEN_BRACE))
19705 cp_parser_abort_tentative_parse (parser);
19706 else if (!cp_parser_parse_definitely (parser))
19708 else
19709 gnu_attrs = attr;
19711 tree requires_clause = NULL_TREE;
19712 late_return = (cp_parser_late_return_type_opt
19713 (parser, declarator, requires_clause,
19714 memfn ? cv_quals : -1));
19716 /* Parse the virt-specifier-seq. */
19717 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19719 /* Create the function-declarator. */
19720 declarator = make_call_declarator (declarator,
19721 params,
19722 cv_quals,
19723 virt_specifiers,
19724 ref_qual,
19725 tx_qual,
19726 exception_specification,
19727 late_return,
19728 requires_clause);
19729 declarator->std_attributes = attrs;
19730 declarator->attributes = gnu_attrs;
19731 /* Any subsequent parameter lists are to do with
19732 return type, so are not those of the declared
19733 function. */
19734 parser->default_arg_ok_p = false;
19737 /* Remove the function parms from scope. */
19738 pop_bindings_and_leave_scope ();
19740 if (is_declarator)
19741 /* Repeat the main loop. */
19742 continue;
19745 /* If this is the first, we can try a parenthesized
19746 declarator. */
19747 if (first)
19749 bool saved_in_type_id_in_expr_p;
19751 parser->default_arg_ok_p = saved_default_arg_ok_p;
19752 parser->in_declarator_p = saved_in_declarator_p;
19754 /* Consume the `('. */
19755 cp_lexer_consume_token (parser->lexer);
19756 /* Parse the nested declarator. */
19757 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19758 parser->in_type_id_in_expr_p = true;
19759 declarator
19760 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19761 /*parenthesized_p=*/NULL,
19762 member_p, friend_p);
19763 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19764 first = false;
19765 /* Expect a `)'. */
19766 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19767 declarator = cp_error_declarator;
19768 if (declarator == cp_error_declarator)
19769 break;
19771 goto handle_declarator;
19773 /* Otherwise, we must be done. */
19774 else
19775 break;
19777 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19778 && token->type == CPP_OPEN_SQUARE
19779 && !cp_next_tokens_can_be_attribute_p (parser))
19781 /* Parse an array-declarator. */
19782 tree bounds, attrs;
19784 if (ctor_dtor_or_conv_p)
19785 *ctor_dtor_or_conv_p = 0;
19787 first = false;
19788 parser->default_arg_ok_p = false;
19789 parser->in_declarator_p = true;
19790 /* Consume the `['. */
19791 cp_lexer_consume_token (parser->lexer);
19792 /* Peek at the next token. */
19793 token = cp_lexer_peek_token (parser->lexer);
19794 /* If the next token is `]', then there is no
19795 constant-expression. */
19796 if (token->type != CPP_CLOSE_SQUARE)
19798 bool non_constant_p;
19799 bounds
19800 = cp_parser_constant_expression (parser,
19801 /*allow_non_constant=*/true,
19802 &non_constant_p);
19803 if (!non_constant_p)
19804 /* OK */;
19805 else if (error_operand_p (bounds))
19806 /* Already gave an error. */;
19807 else if (!parser->in_function_body
19808 || current_binding_level->kind == sk_function_parms)
19810 /* Normally, the array bound must be an integral constant
19811 expression. However, as an extension, we allow VLAs
19812 in function scopes as long as they aren't part of a
19813 parameter declaration. */
19814 cp_parser_error (parser,
19815 "array bound is not an integer constant");
19816 bounds = error_mark_node;
19818 else if (processing_template_decl
19819 && !type_dependent_expression_p (bounds))
19821 /* Remember this wasn't a constant-expression. */
19822 bounds = build_nop (TREE_TYPE (bounds), bounds);
19823 TREE_SIDE_EFFECTS (bounds) = 1;
19826 else
19827 bounds = NULL_TREE;
19828 /* Look for the closing `]'. */
19829 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19831 declarator = cp_error_declarator;
19832 break;
19835 attrs = cp_parser_std_attribute_spec_seq (parser);
19836 declarator = make_array_declarator (declarator, bounds);
19837 declarator->std_attributes = attrs;
19839 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19842 tree qualifying_scope;
19843 tree unqualified_name;
19844 tree attrs;
19845 special_function_kind sfk;
19846 bool abstract_ok;
19847 bool pack_expansion_p = false;
19848 cp_token *declarator_id_start_token;
19850 /* Parse a declarator-id */
19851 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19852 if (abstract_ok)
19854 cp_parser_parse_tentatively (parser);
19856 /* If we see an ellipsis, we should be looking at a
19857 parameter pack. */
19858 if (token->type == CPP_ELLIPSIS)
19860 /* Consume the `...' */
19861 cp_lexer_consume_token (parser->lexer);
19863 pack_expansion_p = true;
19867 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19868 unqualified_name
19869 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19870 qualifying_scope = parser->scope;
19871 if (abstract_ok)
19873 bool okay = false;
19875 if (!unqualified_name && pack_expansion_p)
19877 /* Check whether an error occurred. */
19878 okay = !cp_parser_error_occurred (parser);
19880 /* We already consumed the ellipsis to mark a
19881 parameter pack, but we have no way to report it,
19882 so abort the tentative parse. We will be exiting
19883 immediately anyway. */
19884 cp_parser_abort_tentative_parse (parser);
19886 else
19887 okay = cp_parser_parse_definitely (parser);
19889 if (!okay)
19890 unqualified_name = error_mark_node;
19891 else if (unqualified_name
19892 && (qualifying_scope
19893 || (!identifier_p (unqualified_name))))
19895 cp_parser_error (parser, "expected unqualified-id");
19896 unqualified_name = error_mark_node;
19900 if (!unqualified_name)
19901 return NULL;
19902 if (unqualified_name == error_mark_node)
19904 declarator = cp_error_declarator;
19905 pack_expansion_p = false;
19906 declarator->parameter_pack_p = false;
19907 break;
19910 attrs = cp_parser_std_attribute_spec_seq (parser);
19912 if (qualifying_scope && at_namespace_scope_p ()
19913 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19915 /* In the declaration of a member of a template class
19916 outside of the class itself, the SCOPE will sometimes
19917 be a TYPENAME_TYPE. For example, given:
19919 template <typename T>
19920 int S<T>::R::i = 3;
19922 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19923 this context, we must resolve S<T>::R to an ordinary
19924 type, rather than a typename type.
19926 The reason we normally avoid resolving TYPENAME_TYPEs
19927 is that a specialization of `S' might render
19928 `S<T>::R' not a type. However, if `S' is
19929 specialized, then this `i' will not be used, so there
19930 is no harm in resolving the types here. */
19931 tree type;
19933 /* Resolve the TYPENAME_TYPE. */
19934 type = resolve_typename_type (qualifying_scope,
19935 /*only_current_p=*/false);
19936 /* If that failed, the declarator is invalid. */
19937 if (TREE_CODE (type) == TYPENAME_TYPE)
19939 if (typedef_variant_p (type))
19940 error_at (declarator_id_start_token->location,
19941 "cannot define member of dependent typedef "
19942 "%qT", type);
19943 else
19944 error_at (declarator_id_start_token->location,
19945 "%<%T::%E%> is not a type",
19946 TYPE_CONTEXT (qualifying_scope),
19947 TYPE_IDENTIFIER (qualifying_scope));
19949 qualifying_scope = type;
19952 sfk = sfk_none;
19954 if (unqualified_name)
19956 tree class_type;
19958 if (qualifying_scope
19959 && CLASS_TYPE_P (qualifying_scope))
19960 class_type = qualifying_scope;
19961 else
19962 class_type = current_class_type;
19964 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19966 tree name_type = TREE_TYPE (unqualified_name);
19967 if (class_type && same_type_p (name_type, class_type))
19969 if (qualifying_scope
19970 && CLASSTYPE_USE_TEMPLATE (name_type))
19972 error_at (declarator_id_start_token->location,
19973 "invalid use of constructor as a template");
19974 inform (declarator_id_start_token->location,
19975 "use %<%T::%D%> instead of %<%T::%D%> to "
19976 "name the constructor in a qualified name",
19977 class_type,
19978 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19979 class_type, name_type);
19980 declarator = cp_error_declarator;
19981 break;
19983 else
19984 unqualified_name = constructor_name (class_type);
19986 else
19988 /* We do not attempt to print the declarator
19989 here because we do not have enough
19990 information about its original syntactic
19991 form. */
19992 cp_parser_error (parser, "invalid declarator");
19993 declarator = cp_error_declarator;
19994 break;
19998 if (class_type)
20000 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20001 sfk = sfk_destructor;
20002 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
20003 sfk = sfk_conversion;
20004 else if (/* There's no way to declare a constructor
20005 for an unnamed type, even if the type
20006 got a name for linkage purposes. */
20007 !TYPE_WAS_UNNAMED (class_type)
20008 /* Handle correctly (c++/19200):
20010 struct S {
20011 struct T{};
20012 friend void S(T);
20015 and also:
20017 namespace N {
20018 void S();
20021 struct S {
20022 friend void N::S();
20023 }; */
20024 && !(friend_p
20025 && class_type != qualifying_scope)
20026 && constructor_name_p (unqualified_name,
20027 class_type))
20029 unqualified_name = constructor_name (class_type);
20030 sfk = sfk_constructor;
20032 else if (is_overloaded_fn (unqualified_name)
20033 && DECL_CONSTRUCTOR_P (get_first_fn
20034 (unqualified_name)))
20035 sfk = sfk_constructor;
20037 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20038 *ctor_dtor_or_conv_p = -1;
20041 declarator = make_id_declarator (qualifying_scope,
20042 unqualified_name,
20043 sfk);
20044 declarator->std_attributes = attrs;
20045 declarator->id_loc = token->location;
20046 declarator->parameter_pack_p = pack_expansion_p;
20048 if (pack_expansion_p)
20049 maybe_warn_variadic_templates ();
20052 handle_declarator:;
20053 scope = get_scope_of_declarator (declarator);
20054 if (scope)
20056 /* Any names that appear after the declarator-id for a
20057 member are looked up in the containing scope. */
20058 if (at_function_scope_p ())
20060 /* But declarations with qualified-ids can't appear in a
20061 function. */
20062 cp_parser_error (parser, "qualified-id in declaration");
20063 declarator = cp_error_declarator;
20064 break;
20066 pushed_scope = push_scope (scope);
20068 parser->in_declarator_p = true;
20069 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20070 || (declarator && declarator->kind == cdk_id))
20071 /* Default args are only allowed on function
20072 declarations. */
20073 parser->default_arg_ok_p = saved_default_arg_ok_p;
20074 else
20075 parser->default_arg_ok_p = false;
20077 first = false;
20079 /* We're done. */
20080 else
20081 break;
20084 /* For an abstract declarator, we might wind up with nothing at this
20085 point. That's an error; the declarator is not optional. */
20086 if (!declarator)
20087 cp_parser_error (parser, "expected declarator");
20089 /* If we entered a scope, we must exit it now. */
20090 if (pushed_scope)
20091 pop_scope (pushed_scope);
20093 parser->default_arg_ok_p = saved_default_arg_ok_p;
20094 parser->in_declarator_p = saved_in_declarator_p;
20096 return declarator;
20099 /* Parse a ptr-operator.
20101 ptr-operator:
20102 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20103 * cv-qualifier-seq [opt]
20105 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20106 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20108 GNU Extension:
20110 ptr-operator:
20111 & cv-qualifier-seq [opt]
20113 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20114 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20115 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20116 filled in with the TYPE containing the member. *CV_QUALS is
20117 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20118 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20119 Note that the tree codes returned by this function have nothing
20120 to do with the types of trees that will be eventually be created
20121 to represent the pointer or reference type being parsed. They are
20122 just constants with suggestive names. */
20123 static enum tree_code
20124 cp_parser_ptr_operator (cp_parser* parser,
20125 tree* type,
20126 cp_cv_quals *cv_quals,
20127 tree *attributes)
20129 enum tree_code code = ERROR_MARK;
20130 cp_token *token;
20131 tree attrs = NULL_TREE;
20133 /* Assume that it's not a pointer-to-member. */
20134 *type = NULL_TREE;
20135 /* And that there are no cv-qualifiers. */
20136 *cv_quals = TYPE_UNQUALIFIED;
20138 /* Peek at the next token. */
20139 token = cp_lexer_peek_token (parser->lexer);
20141 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20142 if (token->type == CPP_MULT)
20143 code = INDIRECT_REF;
20144 else if (token->type == CPP_AND)
20145 code = ADDR_EXPR;
20146 else if ((cxx_dialect != cxx98) &&
20147 token->type == CPP_AND_AND) /* C++0x only */
20148 code = NON_LVALUE_EXPR;
20150 if (code != ERROR_MARK)
20152 /* Consume the `*', `&' or `&&'. */
20153 cp_lexer_consume_token (parser->lexer);
20155 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20156 `&', if we are allowing GNU extensions. (The only qualifier
20157 that can legally appear after `&' is `restrict', but that is
20158 enforced during semantic analysis. */
20159 if (code == INDIRECT_REF
20160 || cp_parser_allow_gnu_extensions_p (parser))
20161 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20163 attrs = cp_parser_std_attribute_spec_seq (parser);
20164 if (attributes != NULL)
20165 *attributes = attrs;
20167 else
20169 /* Try the pointer-to-member case. */
20170 cp_parser_parse_tentatively (parser);
20171 /* Look for the optional `::' operator. */
20172 cp_parser_global_scope_opt (parser,
20173 /*current_scope_valid_p=*/false);
20174 /* Look for the nested-name specifier. */
20175 token = cp_lexer_peek_token (parser->lexer);
20176 cp_parser_nested_name_specifier (parser,
20177 /*typename_keyword_p=*/false,
20178 /*check_dependency_p=*/true,
20179 /*type_p=*/false,
20180 /*is_declaration=*/false);
20181 /* If we found it, and the next token is a `*', then we are
20182 indeed looking at a pointer-to-member operator. */
20183 if (!cp_parser_error_occurred (parser)
20184 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20186 /* Indicate that the `*' operator was used. */
20187 code = INDIRECT_REF;
20189 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20190 error_at (token->location, "%qD is a namespace", parser->scope);
20191 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20192 error_at (token->location, "cannot form pointer to member of "
20193 "non-class %q#T", parser->scope);
20194 else
20196 /* The type of which the member is a member is given by the
20197 current SCOPE. */
20198 *type = parser->scope;
20199 /* The next name will not be qualified. */
20200 parser->scope = NULL_TREE;
20201 parser->qualifying_scope = NULL_TREE;
20202 parser->object_scope = NULL_TREE;
20203 /* Look for optional c++11 attributes. */
20204 attrs = cp_parser_std_attribute_spec_seq (parser);
20205 if (attributes != NULL)
20206 *attributes = attrs;
20207 /* Look for the optional cv-qualifier-seq. */
20208 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20211 /* If that didn't work we don't have a ptr-operator. */
20212 if (!cp_parser_parse_definitely (parser))
20213 cp_parser_error (parser, "expected ptr-operator");
20216 return code;
20219 /* Parse an (optional) cv-qualifier-seq.
20221 cv-qualifier-seq:
20222 cv-qualifier cv-qualifier-seq [opt]
20224 cv-qualifier:
20225 const
20226 volatile
20228 GNU Extension:
20230 cv-qualifier:
20231 __restrict__
20233 Returns a bitmask representing the cv-qualifiers. */
20235 static cp_cv_quals
20236 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20238 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20240 while (true)
20242 cp_token *token;
20243 cp_cv_quals cv_qualifier;
20245 /* Peek at the next token. */
20246 token = cp_lexer_peek_token (parser->lexer);
20247 /* See if it's a cv-qualifier. */
20248 switch (token->keyword)
20250 case RID_CONST:
20251 cv_qualifier = TYPE_QUAL_CONST;
20252 break;
20254 case RID_VOLATILE:
20255 cv_qualifier = TYPE_QUAL_VOLATILE;
20256 break;
20258 case RID_RESTRICT:
20259 cv_qualifier = TYPE_QUAL_RESTRICT;
20260 break;
20262 default:
20263 cv_qualifier = TYPE_UNQUALIFIED;
20264 break;
20267 if (!cv_qualifier)
20268 break;
20270 if (cv_quals & cv_qualifier)
20272 gcc_rich_location richloc (token->location);
20273 richloc.add_fixit_remove ();
20274 error_at_rich_loc (&richloc, "duplicate cv-qualifier");
20275 cp_lexer_purge_token (parser->lexer);
20277 else
20279 cp_lexer_consume_token (parser->lexer);
20280 cv_quals |= cv_qualifier;
20284 return cv_quals;
20287 /* Parse an (optional) ref-qualifier
20289 ref-qualifier:
20293 Returns cp_ref_qualifier representing ref-qualifier. */
20295 static cp_ref_qualifier
20296 cp_parser_ref_qualifier_opt (cp_parser* parser)
20298 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20300 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20301 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20302 return ref_qual;
20304 while (true)
20306 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20307 cp_token *token = cp_lexer_peek_token (parser->lexer);
20309 switch (token->type)
20311 case CPP_AND:
20312 curr_ref_qual = REF_QUAL_LVALUE;
20313 break;
20315 case CPP_AND_AND:
20316 curr_ref_qual = REF_QUAL_RVALUE;
20317 break;
20319 default:
20320 curr_ref_qual = REF_QUAL_NONE;
20321 break;
20324 if (!curr_ref_qual)
20325 break;
20326 else if (ref_qual)
20328 error_at (token->location, "multiple ref-qualifiers");
20329 cp_lexer_purge_token (parser->lexer);
20331 else
20333 ref_qual = curr_ref_qual;
20334 cp_lexer_consume_token (parser->lexer);
20338 return ref_qual;
20341 /* Parse an optional tx-qualifier.
20343 tx-qualifier:
20344 transaction_safe
20345 transaction_safe_dynamic */
20347 static tree
20348 cp_parser_tx_qualifier_opt (cp_parser *parser)
20350 cp_token *token = cp_lexer_peek_token (parser->lexer);
20351 if (token->type == CPP_NAME)
20353 tree name = token->u.value;
20354 const char *p = IDENTIFIER_POINTER (name);
20355 const int len = strlen ("transaction_safe");
20356 if (!strncmp (p, "transaction_safe", len))
20358 p += len;
20359 if (*p == '\0'
20360 || !strcmp (p, "_dynamic"))
20362 cp_lexer_consume_token (parser->lexer);
20363 if (!flag_tm)
20365 error ("%E requires %<-fgnu-tm%>", name);
20366 return NULL_TREE;
20368 else
20369 return name;
20373 return NULL_TREE;
20376 /* Parse an (optional) virt-specifier-seq.
20378 virt-specifier-seq:
20379 virt-specifier virt-specifier-seq [opt]
20381 virt-specifier:
20382 override
20383 final
20385 Returns a bitmask representing the virt-specifiers. */
20387 static cp_virt_specifiers
20388 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20390 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20392 while (true)
20394 cp_token *token;
20395 cp_virt_specifiers virt_specifier;
20397 /* Peek at the next token. */
20398 token = cp_lexer_peek_token (parser->lexer);
20399 /* See if it's a virt-specifier-qualifier. */
20400 if (token->type != CPP_NAME)
20401 break;
20402 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
20404 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20405 virt_specifier = VIRT_SPEC_OVERRIDE;
20407 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
20409 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20410 virt_specifier = VIRT_SPEC_FINAL;
20412 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
20414 virt_specifier = VIRT_SPEC_FINAL;
20416 else
20417 break;
20419 if (virt_specifiers & virt_specifier)
20421 gcc_rich_location richloc (token->location);
20422 richloc.add_fixit_remove ();
20423 error_at_rich_loc (&richloc, "duplicate virt-specifier");
20424 cp_lexer_purge_token (parser->lexer);
20426 else
20428 cp_lexer_consume_token (parser->lexer);
20429 virt_specifiers |= virt_specifier;
20432 return virt_specifiers;
20435 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20436 is in scope even though it isn't real. */
20438 void
20439 inject_this_parameter (tree ctype, cp_cv_quals quals)
20441 tree this_parm;
20443 if (current_class_ptr)
20445 /* We don't clear this between NSDMIs. Is it already what we want? */
20446 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20447 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
20448 && cp_type_quals (type) == quals)
20449 return;
20452 this_parm = build_this_parm (ctype, quals);
20453 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20454 current_class_ptr = NULL_TREE;
20455 current_class_ref
20456 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20457 current_class_ptr = this_parm;
20460 /* Return true iff our current scope is a non-static data member
20461 initializer. */
20463 bool
20464 parsing_nsdmi (void)
20466 /* We recognize NSDMI context by the context-less 'this' pointer set up
20467 by the function above. */
20468 if (current_class_ptr
20469 && TREE_CODE (current_class_ptr) == PARM_DECL
20470 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20471 return true;
20472 return false;
20475 /* Return true iff our current scope is a default capturing generic lambda
20476 defined within a template. FIXME: This is part of a workaround (see
20477 semantics.c) to handle building lambda closure types correctly in templates
20478 which we ultimately want to defer to instantiation time. */
20480 bool
20481 parsing_default_capturing_generic_lambda_in_template (void)
20483 if (!processing_template_decl || !current_class_type)
20484 return false;
20486 tree lam = CLASSTYPE_LAMBDA_EXPR (current_class_type);
20487 if (!lam || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
20488 return false;
20490 tree callop = lambda_function (lam);
20491 if (!callop)
20492 return false;
20494 return (DECL_TEMPLATE_INFO (callop)
20495 && (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (callop)) == callop)
20496 && ((current_nonlambda_class_type ()
20497 && CLASSTYPE_TEMPLATE_INFO (current_nonlambda_class_type ()))
20498 || ((current_nonlambda_function ()
20499 && DECL_TEMPLATE_INFO (current_nonlambda_function ())))));
20502 /* Parse a late-specified return type, if any. This is not a separate
20503 non-terminal, but part of a function declarator, which looks like
20505 -> trailing-type-specifier-seq abstract-declarator(opt)
20507 Returns the type indicated by the type-id.
20509 In addition to this, parse any queued up #pragma omp declare simd
20510 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20511 #pragma acc routine clauses.
20513 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20514 function. */
20516 static tree
20517 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20518 tree& requires_clause, cp_cv_quals quals)
20520 cp_token *token;
20521 tree type = NULL_TREE;
20522 bool declare_simd_p = (parser->omp_declare_simd
20523 && declarator
20524 && declarator->kind == cdk_id);
20526 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20527 && declarator && declarator->kind == cdk_id);
20529 bool oacc_routine_p = (parser->oacc_routine
20530 && declarator
20531 && declarator->kind == cdk_id);
20533 /* Peek at the next token. */
20534 token = cp_lexer_peek_token (parser->lexer);
20535 /* A late-specified return type is indicated by an initial '->'. */
20536 if (token->type != CPP_DEREF
20537 && token->keyword != RID_REQUIRES
20538 && !(token->type == CPP_NAME
20539 && token->u.value == ridpointers[RID_REQUIRES])
20540 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20541 return NULL_TREE;
20543 tree save_ccp = current_class_ptr;
20544 tree save_ccr = current_class_ref;
20545 if (quals >= 0)
20547 /* DR 1207: 'this' is in scope in the trailing return type. */
20548 inject_this_parameter (current_class_type, quals);
20551 if (token->type == CPP_DEREF)
20553 /* Consume the ->. */
20554 cp_lexer_consume_token (parser->lexer);
20556 type = cp_parser_trailing_type_id (parser);
20559 /* Function declarations may be followed by a trailing
20560 requires-clause. */
20561 requires_clause = cp_parser_requires_clause_opt (parser);
20563 if (cilk_simd_fn_vector_p)
20564 declarator->attributes
20565 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20566 declarator->attributes);
20567 if (declare_simd_p)
20568 declarator->attributes
20569 = cp_parser_late_parsing_omp_declare_simd (parser,
20570 declarator->attributes);
20571 if (oacc_routine_p)
20572 declarator->attributes
20573 = cp_parser_late_parsing_oacc_routine (parser,
20574 declarator->attributes);
20576 if (quals >= 0)
20578 current_class_ptr = save_ccp;
20579 current_class_ref = save_ccr;
20582 return type;
20585 /* Parse a declarator-id.
20587 declarator-id:
20588 id-expression
20589 :: [opt] nested-name-specifier [opt] type-name
20591 In the `id-expression' case, the value returned is as for
20592 cp_parser_id_expression if the id-expression was an unqualified-id.
20593 If the id-expression was a qualified-id, then a SCOPE_REF is
20594 returned. The first operand is the scope (either a NAMESPACE_DECL
20595 or TREE_TYPE), but the second is still just a representation of an
20596 unqualified-id. */
20598 static tree
20599 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20601 tree id;
20602 /* The expression must be an id-expression. Assume that qualified
20603 names are the names of types so that:
20605 template <class T>
20606 int S<T>::R::i = 3;
20608 will work; we must treat `S<T>::R' as the name of a type.
20609 Similarly, assume that qualified names are templates, where
20610 required, so that:
20612 template <class T>
20613 int S<T>::R<T>::i = 3;
20615 will work, too. */
20616 id = cp_parser_id_expression (parser,
20617 /*template_keyword_p=*/false,
20618 /*check_dependency_p=*/false,
20619 /*template_p=*/NULL,
20620 /*declarator_p=*/true,
20621 optional_p);
20622 if (id && BASELINK_P (id))
20623 id = BASELINK_FUNCTIONS (id);
20624 return id;
20627 /* Parse a type-id.
20629 type-id:
20630 type-specifier-seq abstract-declarator [opt]
20632 Returns the TYPE specified. */
20634 static tree
20635 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20636 bool is_trailing_return)
20638 cp_decl_specifier_seq type_specifier_seq;
20639 cp_declarator *abstract_declarator;
20641 /* Parse the type-specifier-seq. */
20642 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20643 is_trailing_return,
20644 &type_specifier_seq);
20645 if (is_template_arg && type_specifier_seq.type
20646 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20647 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20648 /* A bare template name as a template argument is a template template
20649 argument, not a placeholder, so fail parsing it as a type argument. */
20651 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20652 cp_parser_simulate_error (parser);
20653 return error_mark_node;
20655 if (type_specifier_seq.type == error_mark_node)
20656 return error_mark_node;
20658 /* There might or might not be an abstract declarator. */
20659 cp_parser_parse_tentatively (parser);
20660 /* Look for the declarator. */
20661 abstract_declarator
20662 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20663 /*parenthesized_p=*/NULL,
20664 /*member_p=*/false,
20665 /*friend_p=*/false);
20666 /* Check to see if there really was a declarator. */
20667 if (!cp_parser_parse_definitely (parser))
20668 abstract_declarator = NULL;
20670 if (type_specifier_seq.type
20671 /* The concepts TS allows 'auto' as a type-id. */
20672 && (!flag_concepts || parser->in_type_id_in_expr_p)
20673 /* None of the valid uses of 'auto' in C++14 involve the type-id
20674 nonterminal, but it is valid in a trailing-return-type. */
20675 && !(cxx_dialect >= cxx14 && is_trailing_return))
20676 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20678 /* A type-id with type 'auto' is only ok if the abstract declarator
20679 is a function declarator with a late-specified return type.
20681 A type-id with 'auto' is also valid in a trailing-return-type
20682 in a compound-requirement. */
20683 if (abstract_declarator
20684 && abstract_declarator->kind == cdk_function
20685 && abstract_declarator->u.function.late_return_type)
20686 /* OK */;
20687 else if (parser->in_result_type_constraint_p)
20688 /* OK */;
20689 else
20691 location_t loc = type_specifier_seq.locations[ds_type_spec];
20692 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20694 error_at (loc, "missing template arguments after %qT",
20695 auto_node);
20696 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20697 tmpl);
20699 else
20700 error_at (loc, "invalid use of %qT", auto_node);
20701 return error_mark_node;
20705 return groktypename (&type_specifier_seq, abstract_declarator,
20706 is_template_arg);
20709 static tree
20710 cp_parser_type_id (cp_parser *parser)
20712 return cp_parser_type_id_1 (parser, false, false);
20715 static tree
20716 cp_parser_template_type_arg (cp_parser *parser)
20718 tree r;
20719 const char *saved_message = parser->type_definition_forbidden_message;
20720 parser->type_definition_forbidden_message
20721 = G_("types may not be defined in template arguments");
20722 r = cp_parser_type_id_1 (parser, true, false);
20723 parser->type_definition_forbidden_message = saved_message;
20724 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20726 error ("invalid use of %<auto%> in template argument");
20727 r = error_mark_node;
20729 return r;
20732 static tree
20733 cp_parser_trailing_type_id (cp_parser *parser)
20735 return cp_parser_type_id_1 (parser, false, true);
20738 /* Parse a type-specifier-seq.
20740 type-specifier-seq:
20741 type-specifier type-specifier-seq [opt]
20743 GNU extension:
20745 type-specifier-seq:
20746 attributes type-specifier-seq [opt]
20748 If IS_DECLARATION is true, we are at the start of a "condition" or
20749 exception-declaration, so we might be followed by a declarator-id.
20751 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20752 i.e. we've just seen "->".
20754 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20756 static void
20757 cp_parser_type_specifier_seq (cp_parser* parser,
20758 bool is_declaration,
20759 bool is_trailing_return,
20760 cp_decl_specifier_seq *type_specifier_seq)
20762 bool seen_type_specifier = false;
20763 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20764 cp_token *start_token = NULL;
20766 /* Clear the TYPE_SPECIFIER_SEQ. */
20767 clear_decl_specs (type_specifier_seq);
20769 /* In the context of a trailing return type, enum E { } is an
20770 elaborated-type-specifier followed by a function-body, not an
20771 enum-specifier. */
20772 if (is_trailing_return)
20773 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20775 /* Parse the type-specifiers and attributes. */
20776 while (true)
20778 tree type_specifier;
20779 bool is_cv_qualifier;
20781 /* Check for attributes first. */
20782 if (cp_next_tokens_can_be_attribute_p (parser))
20784 type_specifier_seq->attributes =
20785 chainon (type_specifier_seq->attributes,
20786 cp_parser_attributes_opt (parser));
20787 continue;
20790 /* record the token of the beginning of the type specifier seq,
20791 for error reporting purposes*/
20792 if (!start_token)
20793 start_token = cp_lexer_peek_token (parser->lexer);
20795 /* Look for the type-specifier. */
20796 type_specifier = cp_parser_type_specifier (parser,
20797 flags,
20798 type_specifier_seq,
20799 /*is_declaration=*/false,
20800 NULL,
20801 &is_cv_qualifier);
20802 if (!type_specifier)
20804 /* If the first type-specifier could not be found, this is not a
20805 type-specifier-seq at all. */
20806 if (!seen_type_specifier)
20808 /* Set in_declarator_p to avoid skipping to the semicolon. */
20809 int in_decl = parser->in_declarator_p;
20810 parser->in_declarator_p = true;
20812 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20813 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20814 cp_parser_error (parser, "expected type-specifier");
20816 parser->in_declarator_p = in_decl;
20818 type_specifier_seq->type = error_mark_node;
20819 return;
20821 /* If subsequent type-specifiers could not be found, the
20822 type-specifier-seq is complete. */
20823 break;
20826 seen_type_specifier = true;
20827 /* The standard says that a condition can be:
20829 type-specifier-seq declarator = assignment-expression
20831 However, given:
20833 struct S {};
20834 if (int S = ...)
20836 we should treat the "S" as a declarator, not as a
20837 type-specifier. The standard doesn't say that explicitly for
20838 type-specifier-seq, but it does say that for
20839 decl-specifier-seq in an ordinary declaration. Perhaps it
20840 would be clearer just to allow a decl-specifier-seq here, and
20841 then add a semantic restriction that if any decl-specifiers
20842 that are not type-specifiers appear, the program is invalid. */
20843 if (is_declaration && !is_cv_qualifier)
20844 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20848 /* Return whether the function currently being declared has an associated
20849 template parameter list. */
20851 static bool
20852 function_being_declared_is_template_p (cp_parser* parser)
20854 if (!current_template_parms || processing_template_parmlist)
20855 return false;
20857 if (parser->implicit_template_scope)
20858 return true;
20860 if (at_class_scope_p ()
20861 && TYPE_BEING_DEFINED (current_class_type))
20862 return parser->num_template_parameter_lists != 0;
20864 return ((int) parser->num_template_parameter_lists > template_class_depth
20865 (current_class_type));
20868 /* Parse a parameter-declaration-clause.
20870 parameter-declaration-clause:
20871 parameter-declaration-list [opt] ... [opt]
20872 parameter-declaration-list , ...
20874 Returns a representation for the parameter declarations. A return
20875 value of NULL indicates a parameter-declaration-clause consisting
20876 only of an ellipsis. */
20878 static tree
20879 cp_parser_parameter_declaration_clause (cp_parser* parser)
20881 tree parameters;
20882 cp_token *token;
20883 bool ellipsis_p;
20884 bool is_error;
20886 struct cleanup {
20887 cp_parser* parser;
20888 int auto_is_implicit_function_template_parm_p;
20889 ~cleanup() {
20890 parser->auto_is_implicit_function_template_parm_p
20891 = auto_is_implicit_function_template_parm_p;
20893 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20895 (void) cleanup;
20897 if (!processing_specialization
20898 && !processing_template_parmlist
20899 && !processing_explicit_instantiation)
20900 if (!current_function_decl
20901 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20902 parser->auto_is_implicit_function_template_parm_p = true;
20904 /* Peek at the next token. */
20905 token = cp_lexer_peek_token (parser->lexer);
20906 /* Check for trivial parameter-declaration-clauses. */
20907 if (token->type == CPP_ELLIPSIS)
20909 /* Consume the `...' token. */
20910 cp_lexer_consume_token (parser->lexer);
20911 return NULL_TREE;
20913 else if (token->type == CPP_CLOSE_PAREN)
20914 /* There are no parameters. */
20916 #ifndef NO_IMPLICIT_EXTERN_C
20917 if (in_system_header_at (input_location)
20918 && current_class_type == NULL
20919 && current_lang_name == lang_name_c)
20920 return NULL_TREE;
20921 else
20922 #endif
20923 return void_list_node;
20925 /* Check for `(void)', too, which is a special case. */
20926 else if (token->keyword == RID_VOID
20927 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20928 == CPP_CLOSE_PAREN))
20930 /* Consume the `void' token. */
20931 cp_lexer_consume_token (parser->lexer);
20932 /* There are no parameters. */
20933 return void_list_node;
20936 /* Parse the parameter-declaration-list. */
20937 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20938 /* If a parse error occurred while parsing the
20939 parameter-declaration-list, then the entire
20940 parameter-declaration-clause is erroneous. */
20941 if (is_error)
20942 return NULL;
20944 /* Peek at the next token. */
20945 token = cp_lexer_peek_token (parser->lexer);
20946 /* If it's a `,', the clause should terminate with an ellipsis. */
20947 if (token->type == CPP_COMMA)
20949 /* Consume the `,'. */
20950 cp_lexer_consume_token (parser->lexer);
20951 /* Expect an ellipsis. */
20952 ellipsis_p
20953 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20955 /* It might also be `...' if the optional trailing `,' was
20956 omitted. */
20957 else if (token->type == CPP_ELLIPSIS)
20959 /* Consume the `...' token. */
20960 cp_lexer_consume_token (parser->lexer);
20961 /* And remember that we saw it. */
20962 ellipsis_p = true;
20964 else
20965 ellipsis_p = false;
20967 /* Finish the parameter list. */
20968 if (!ellipsis_p)
20969 parameters = chainon (parameters, void_list_node);
20971 return parameters;
20974 /* Parse a parameter-declaration-list.
20976 parameter-declaration-list:
20977 parameter-declaration
20978 parameter-declaration-list , parameter-declaration
20980 Returns a representation of the parameter-declaration-list, as for
20981 cp_parser_parameter_declaration_clause. However, the
20982 `void_list_node' is never appended to the list. Upon return,
20983 *IS_ERROR will be true iff an error occurred. */
20985 static tree
20986 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20988 tree parameters = NULL_TREE;
20989 tree *tail = &parameters;
20990 bool saved_in_unbraced_linkage_specification_p;
20991 int index = 0;
20993 /* Assume all will go well. */
20994 *is_error = false;
20995 /* The special considerations that apply to a function within an
20996 unbraced linkage specifications do not apply to the parameters
20997 to the function. */
20998 saved_in_unbraced_linkage_specification_p
20999 = parser->in_unbraced_linkage_specification_p;
21000 parser->in_unbraced_linkage_specification_p = false;
21002 /* Look for more parameters. */
21003 while (true)
21005 cp_parameter_declarator *parameter;
21006 tree decl = error_mark_node;
21007 bool parenthesized_p = false;
21008 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21009 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21010 (current_template_parms)) : 0);
21012 /* Parse the parameter. */
21013 parameter
21014 = cp_parser_parameter_declaration (parser,
21015 /*template_parm_p=*/false,
21016 &parenthesized_p);
21018 /* We don't know yet if the enclosing context is deprecated, so wait
21019 and warn in grokparms if appropriate. */
21020 deprecated_state = DEPRECATED_SUPPRESS;
21022 if (parameter)
21024 /* If a function parameter pack was specified and an implicit template
21025 parameter was introduced during cp_parser_parameter_declaration,
21026 change any implicit parameters introduced into packs. */
21027 if (parser->implicit_template_parms
21028 && parameter->declarator
21029 && parameter->declarator->parameter_pack_p)
21031 int latest_template_parm_idx = TREE_VEC_LENGTH
21032 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21034 if (latest_template_parm_idx != template_parm_idx)
21035 parameter->decl_specifiers.type = convert_generic_types_to_packs
21036 (parameter->decl_specifiers.type,
21037 template_parm_idx, latest_template_parm_idx);
21040 decl = grokdeclarator (parameter->declarator,
21041 &parameter->decl_specifiers,
21042 PARM,
21043 parameter->default_argument != NULL_TREE,
21044 &parameter->decl_specifiers.attributes);
21047 deprecated_state = DEPRECATED_NORMAL;
21049 /* If a parse error occurred parsing the parameter declaration,
21050 then the entire parameter-declaration-list is erroneous. */
21051 if (decl == error_mark_node)
21053 *is_error = true;
21054 parameters = error_mark_node;
21055 break;
21058 if (parameter->decl_specifiers.attributes)
21059 cplus_decl_attributes (&decl,
21060 parameter->decl_specifiers.attributes,
21062 if (DECL_NAME (decl))
21063 decl = pushdecl (decl);
21065 if (decl != error_mark_node)
21067 retrofit_lang_decl (decl);
21068 DECL_PARM_INDEX (decl) = ++index;
21069 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21072 /* Add the new parameter to the list. */
21073 *tail = build_tree_list (parameter->default_argument, decl);
21074 tail = &TREE_CHAIN (*tail);
21076 /* Peek at the next token. */
21077 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21078 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21079 /* These are for Objective-C++ */
21080 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21081 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21082 /* The parameter-declaration-list is complete. */
21083 break;
21084 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21086 cp_token *token;
21088 /* Peek at the next token. */
21089 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21090 /* If it's an ellipsis, then the list is complete. */
21091 if (token->type == CPP_ELLIPSIS)
21092 break;
21093 /* Otherwise, there must be more parameters. Consume the
21094 `,'. */
21095 cp_lexer_consume_token (parser->lexer);
21096 /* When parsing something like:
21098 int i(float f, double d)
21100 we can tell after seeing the declaration for "f" that we
21101 are not looking at an initialization of a variable "i",
21102 but rather at the declaration of a function "i".
21104 Due to the fact that the parsing of template arguments
21105 (as specified to a template-id) requires backtracking we
21106 cannot use this technique when inside a template argument
21107 list. */
21108 if (!parser->in_template_argument_list_p
21109 && !parser->in_type_id_in_expr_p
21110 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21111 /* However, a parameter-declaration of the form
21112 "float(f)" (which is a valid declaration of a
21113 parameter "f") can also be interpreted as an
21114 expression (the conversion of "f" to "float"). */
21115 && !parenthesized_p)
21116 cp_parser_commit_to_tentative_parse (parser);
21118 else
21120 cp_parser_error (parser, "expected %<,%> or %<...%>");
21121 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21122 cp_parser_skip_to_closing_parenthesis (parser,
21123 /*recovering=*/true,
21124 /*or_comma=*/false,
21125 /*consume_paren=*/false);
21126 break;
21130 parser->in_unbraced_linkage_specification_p
21131 = saved_in_unbraced_linkage_specification_p;
21133 /* Reset implicit_template_scope if we are about to leave the function
21134 parameter list that introduced it. Note that for out-of-line member
21135 definitions, there will be one or more class scopes before we get to
21136 the template parameter scope. */
21138 if (cp_binding_level *its = parser->implicit_template_scope)
21139 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21141 while (maybe_its->kind == sk_class)
21142 maybe_its = maybe_its->level_chain;
21143 if (maybe_its == its)
21145 parser->implicit_template_parms = 0;
21146 parser->implicit_template_scope = 0;
21150 return parameters;
21153 /* Parse a parameter declaration.
21155 parameter-declaration:
21156 decl-specifier-seq ... [opt] declarator
21157 decl-specifier-seq declarator = assignment-expression
21158 decl-specifier-seq ... [opt] abstract-declarator [opt]
21159 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21161 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21162 declares a template parameter. (In that case, a non-nested `>'
21163 token encountered during the parsing of the assignment-expression
21164 is not interpreted as a greater-than operator.)
21166 Returns a representation of the parameter, or NULL if an error
21167 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21168 true iff the declarator is of the form "(p)". */
21170 static cp_parameter_declarator *
21171 cp_parser_parameter_declaration (cp_parser *parser,
21172 bool template_parm_p,
21173 bool *parenthesized_p)
21175 int declares_class_or_enum;
21176 cp_decl_specifier_seq decl_specifiers;
21177 cp_declarator *declarator;
21178 tree default_argument;
21179 cp_token *token = NULL, *declarator_token_start = NULL;
21180 const char *saved_message;
21181 bool template_parameter_pack_p = false;
21183 /* In a template parameter, `>' is not an operator.
21185 [temp.param]
21187 When parsing a default template-argument for a non-type
21188 template-parameter, the first non-nested `>' is taken as the end
21189 of the template parameter-list rather than a greater-than
21190 operator. */
21192 /* Type definitions may not appear in parameter types. */
21193 saved_message = parser->type_definition_forbidden_message;
21194 parser->type_definition_forbidden_message
21195 = G_("types may not be defined in parameter types");
21197 /* Parse the declaration-specifiers. */
21198 cp_parser_decl_specifier_seq (parser,
21199 CP_PARSER_FLAGS_NONE,
21200 &decl_specifiers,
21201 &declares_class_or_enum);
21203 /* Complain about missing 'typename' or other invalid type names. */
21204 if (!decl_specifiers.any_type_specifiers_p
21205 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21206 decl_specifiers.type = error_mark_node;
21208 /* If an error occurred, there's no reason to attempt to parse the
21209 rest of the declaration. */
21210 if (cp_parser_error_occurred (parser))
21212 parser->type_definition_forbidden_message = saved_message;
21213 return NULL;
21216 /* Peek at the next token. */
21217 token = cp_lexer_peek_token (parser->lexer);
21219 /* If the next token is a `)', `,', `=', `>', or `...', then there
21220 is no declarator. However, when variadic templates are enabled,
21221 there may be a declarator following `...'. */
21222 if (token->type == CPP_CLOSE_PAREN
21223 || token->type == CPP_COMMA
21224 || token->type == CPP_EQ
21225 || token->type == CPP_GREATER)
21227 declarator = NULL;
21228 if (parenthesized_p)
21229 *parenthesized_p = false;
21231 /* Otherwise, there should be a declarator. */
21232 else
21234 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21235 parser->default_arg_ok_p = false;
21237 /* After seeing a decl-specifier-seq, if the next token is not a
21238 "(", there is no possibility that the code is a valid
21239 expression. Therefore, if parsing tentatively, we commit at
21240 this point. */
21241 if (!parser->in_template_argument_list_p
21242 /* In an expression context, having seen:
21244 (int((char ...
21246 we cannot be sure whether we are looking at a
21247 function-type (taking a "char" as a parameter) or a cast
21248 of some object of type "char" to "int". */
21249 && !parser->in_type_id_in_expr_p
21250 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21251 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21252 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21253 cp_parser_commit_to_tentative_parse (parser);
21254 /* Parse the declarator. */
21255 declarator_token_start = token;
21256 declarator = cp_parser_declarator (parser,
21257 CP_PARSER_DECLARATOR_EITHER,
21258 /*ctor_dtor_or_conv_p=*/NULL,
21259 parenthesized_p,
21260 /*member_p=*/false,
21261 /*friend_p=*/false);
21262 parser->default_arg_ok_p = saved_default_arg_ok_p;
21263 /* After the declarator, allow more attributes. */
21264 decl_specifiers.attributes
21265 = chainon (decl_specifiers.attributes,
21266 cp_parser_attributes_opt (parser));
21268 /* If the declarator is a template parameter pack, remember that and
21269 clear the flag in the declarator itself so we don't get errors
21270 from grokdeclarator. */
21271 if (template_parm_p && declarator && declarator->parameter_pack_p)
21273 declarator->parameter_pack_p = false;
21274 template_parameter_pack_p = true;
21278 /* If the next token is an ellipsis, and we have not seen a declarator
21279 name, and if either the type of the declarator contains parameter
21280 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21281 for, eg, abbreviated integral type names), then we actually have a
21282 parameter pack expansion expression. Otherwise, leave the ellipsis
21283 for a C-style variadic function. */
21284 token = cp_lexer_peek_token (parser->lexer);
21285 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21287 tree type = decl_specifiers.type;
21289 if (type && DECL_P (type))
21290 type = TREE_TYPE (type);
21292 if (((type
21293 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21294 && (template_parm_p || uses_parameter_packs (type)))
21295 || (!type && template_parm_p))
21296 && declarator_can_be_parameter_pack (declarator))
21298 /* Consume the `...'. */
21299 cp_lexer_consume_token (parser->lexer);
21300 maybe_warn_variadic_templates ();
21302 /* Build a pack expansion type */
21303 if (template_parm_p)
21304 template_parameter_pack_p = true;
21305 else if (declarator)
21306 declarator->parameter_pack_p = true;
21307 else
21308 decl_specifiers.type = make_pack_expansion (type);
21312 /* The restriction on defining new types applies only to the type
21313 of the parameter, not to the default argument. */
21314 parser->type_definition_forbidden_message = saved_message;
21316 /* If the next token is `=', then process a default argument. */
21317 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21319 tree type = decl_specifiers.type;
21320 token = cp_lexer_peek_token (parser->lexer);
21321 /* If we are defining a class, then the tokens that make up the
21322 default argument must be saved and processed later. */
21323 if (!template_parm_p && at_class_scope_p ()
21324 && TYPE_BEING_DEFINED (current_class_type)
21325 && !LAMBDA_TYPE_P (current_class_type))
21326 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21328 // A constrained-type-specifier may declare a type template-parameter.
21329 else if (declares_constrained_type_template_parameter (type))
21330 default_argument
21331 = cp_parser_default_type_template_argument (parser);
21333 // A constrained-type-specifier may declare a template-template-parameter.
21334 else if (declares_constrained_template_template_parameter (type))
21335 default_argument
21336 = cp_parser_default_template_template_argument (parser);
21338 /* Outside of a class definition, we can just parse the
21339 assignment-expression. */
21340 else
21341 default_argument
21342 = cp_parser_default_argument (parser, template_parm_p);
21344 if (!parser->default_arg_ok_p)
21346 permerror (token->location,
21347 "default arguments are only "
21348 "permitted for function parameters");
21350 else if ((declarator && declarator->parameter_pack_p)
21351 || template_parameter_pack_p
21352 || (decl_specifiers.type
21353 && PACK_EXPANSION_P (decl_specifiers.type)))
21355 /* Find the name of the parameter pack. */
21356 cp_declarator *id_declarator = declarator;
21357 while (id_declarator && id_declarator->kind != cdk_id)
21358 id_declarator = id_declarator->declarator;
21360 if (id_declarator && id_declarator->kind == cdk_id)
21361 error_at (declarator_token_start->location,
21362 template_parm_p
21363 ? G_("template parameter pack %qD "
21364 "cannot have a default argument")
21365 : G_("parameter pack %qD cannot have "
21366 "a default argument"),
21367 id_declarator->u.id.unqualified_name);
21368 else
21369 error_at (declarator_token_start->location,
21370 template_parm_p
21371 ? G_("template parameter pack cannot have "
21372 "a default argument")
21373 : G_("parameter pack cannot have a "
21374 "default argument"));
21376 default_argument = NULL_TREE;
21379 else
21380 default_argument = NULL_TREE;
21382 return make_parameter_declarator (&decl_specifiers,
21383 declarator,
21384 default_argument,
21385 template_parameter_pack_p);
21388 /* Parse a default argument and return it.
21390 TEMPLATE_PARM_P is true if this is a default argument for a
21391 non-type template parameter. */
21392 static tree
21393 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21395 tree default_argument = NULL_TREE;
21396 bool saved_greater_than_is_operator_p;
21397 bool saved_local_variables_forbidden_p;
21398 bool non_constant_p, is_direct_init;
21400 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21401 set correctly. */
21402 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21403 parser->greater_than_is_operator_p = !template_parm_p;
21404 /* Local variable names (and the `this' keyword) may not
21405 appear in a default argument. */
21406 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21407 parser->local_variables_forbidden_p = true;
21408 /* Parse the assignment-expression. */
21409 if (template_parm_p)
21410 push_deferring_access_checks (dk_no_deferred);
21411 tree saved_class_ptr = NULL_TREE;
21412 tree saved_class_ref = NULL_TREE;
21413 /* The "this" pointer is not valid in a default argument. */
21414 if (cfun)
21416 saved_class_ptr = current_class_ptr;
21417 cp_function_chain->x_current_class_ptr = NULL_TREE;
21418 saved_class_ref = current_class_ref;
21419 cp_function_chain->x_current_class_ref = NULL_TREE;
21421 default_argument
21422 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21423 /* Restore the "this" pointer. */
21424 if (cfun)
21426 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21427 cp_function_chain->x_current_class_ref = saved_class_ref;
21429 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21430 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21431 if (template_parm_p)
21432 pop_deferring_access_checks ();
21433 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21434 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21436 return default_argument;
21439 /* Parse a function-body.
21441 function-body:
21442 compound_statement */
21444 static void
21445 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21447 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21448 ? BCS_TRY_BLOCK : BCS_NORMAL),
21449 true);
21452 /* Parse a ctor-initializer-opt followed by a function-body. Return
21453 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21454 is true we are parsing a function-try-block. */
21456 static bool
21457 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21458 bool in_function_try_block)
21460 tree body, list;
21461 bool ctor_initializer_p;
21462 const bool check_body_p =
21463 DECL_CONSTRUCTOR_P (current_function_decl)
21464 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21465 tree last = NULL;
21467 /* Begin the function body. */
21468 body = begin_function_body ();
21469 /* Parse the optional ctor-initializer. */
21470 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
21472 /* If we're parsing a constexpr constructor definition, we need
21473 to check that the constructor body is indeed empty. However,
21474 before we get to cp_parser_function_body lot of junk has been
21475 generated, so we can't just check that we have an empty block.
21476 Rather we take a snapshot of the outermost block, and check whether
21477 cp_parser_function_body changed its state. */
21478 if (check_body_p)
21480 list = cur_stmt_list;
21481 if (STATEMENT_LIST_TAIL (list))
21482 last = STATEMENT_LIST_TAIL (list)->stmt;
21484 /* Parse the function-body. */
21485 cp_parser_function_body (parser, in_function_try_block);
21486 if (check_body_p)
21487 check_constexpr_ctor_body (last, list, /*complain=*/true);
21488 /* Finish the function body. */
21489 finish_function_body (body);
21491 return ctor_initializer_p;
21494 /* Parse an initializer.
21496 initializer:
21497 = initializer-clause
21498 ( expression-list )
21500 Returns an expression representing the initializer. If no
21501 initializer is present, NULL_TREE is returned.
21503 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21504 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21505 set to TRUE if there is no initializer present. If there is an
21506 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21507 is set to true; otherwise it is set to false. */
21509 static tree
21510 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21511 bool* non_constant_p)
21513 cp_token *token;
21514 tree init;
21516 /* Peek at the next token. */
21517 token = cp_lexer_peek_token (parser->lexer);
21519 /* Let our caller know whether or not this initializer was
21520 parenthesized. */
21521 *is_direct_init = (token->type != CPP_EQ);
21522 /* Assume that the initializer is constant. */
21523 *non_constant_p = false;
21525 if (token->type == CPP_EQ)
21527 /* Consume the `='. */
21528 cp_lexer_consume_token (parser->lexer);
21529 /* Parse the initializer-clause. */
21530 init = cp_parser_initializer_clause (parser, non_constant_p);
21532 else if (token->type == CPP_OPEN_PAREN)
21534 vec<tree, va_gc> *vec;
21535 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21536 /*cast_p=*/false,
21537 /*allow_expansion_p=*/true,
21538 non_constant_p);
21539 if (vec == NULL)
21540 return error_mark_node;
21541 init = build_tree_list_vec (vec);
21542 release_tree_vector (vec);
21544 else if (token->type == CPP_OPEN_BRACE)
21546 cp_lexer_set_source_position (parser->lexer);
21547 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21548 init = cp_parser_braced_list (parser, non_constant_p);
21549 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21551 else
21553 /* Anything else is an error. */
21554 cp_parser_error (parser, "expected initializer");
21555 init = error_mark_node;
21558 if (check_for_bare_parameter_packs (init))
21559 init = error_mark_node;
21561 return init;
21564 /* Parse an initializer-clause.
21566 initializer-clause:
21567 assignment-expression
21568 braced-init-list
21570 Returns an expression representing the initializer.
21572 If the `assignment-expression' production is used the value
21573 returned is simply a representation for the expression.
21575 Otherwise, calls cp_parser_braced_list. */
21577 static cp_expr
21578 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21580 cp_expr initializer;
21582 /* Assume the expression is constant. */
21583 *non_constant_p = false;
21585 /* If it is not a `{', then we are looking at an
21586 assignment-expression. */
21587 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21589 initializer
21590 = cp_parser_constant_expression (parser,
21591 /*allow_non_constant_p=*/true,
21592 non_constant_p);
21594 else
21595 initializer = cp_parser_braced_list (parser, non_constant_p);
21597 return initializer;
21600 /* Parse a brace-enclosed initializer list.
21602 braced-init-list:
21603 { initializer-list , [opt] }
21606 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21607 the elements of the initializer-list (or NULL, if the last
21608 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21609 NULL_TREE. There is no way to detect whether or not the optional
21610 trailing `,' was provided. NON_CONSTANT_P is as for
21611 cp_parser_initializer. */
21613 static cp_expr
21614 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21616 tree initializer;
21617 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21619 /* Consume the `{' token. */
21620 cp_lexer_consume_token (parser->lexer);
21621 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21622 initializer = make_node (CONSTRUCTOR);
21623 /* If it's not a `}', then there is a non-trivial initializer. */
21624 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21626 /* Parse the initializer list. */
21627 CONSTRUCTOR_ELTS (initializer)
21628 = cp_parser_initializer_list (parser, non_constant_p);
21629 /* A trailing `,' token is allowed. */
21630 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21631 cp_lexer_consume_token (parser->lexer);
21633 else
21634 *non_constant_p = false;
21635 /* Now, there should be a trailing `}'. */
21636 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21637 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21638 TREE_TYPE (initializer) = init_list_type_node;
21640 cp_expr result (initializer);
21641 /* Build a location of the form:
21642 { ... }
21643 ^~~~~~~
21644 with caret==start at the open brace, finish at the close brace. */
21645 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21646 result.set_location (combined_loc);
21647 return result;
21650 /* Consume tokens up to, and including, the next non-nested closing `]'.
21651 Returns true iff we found a closing `]'. */
21653 static bool
21654 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21656 unsigned square_depth = 0;
21658 while (true)
21660 cp_token * token = cp_lexer_peek_token (parser->lexer);
21662 switch (token->type)
21664 case CPP_EOF:
21665 case CPP_PRAGMA_EOL:
21666 /* If we've run out of tokens, then there is no closing `]'. */
21667 return false;
21669 case CPP_OPEN_SQUARE:
21670 ++square_depth;
21671 break;
21673 case CPP_CLOSE_SQUARE:
21674 if (!square_depth--)
21676 cp_lexer_consume_token (parser->lexer);
21677 return true;
21679 break;
21681 default:
21682 break;
21685 /* Consume the token. */
21686 cp_lexer_consume_token (parser->lexer);
21690 /* Return true if we are looking at an array-designator, false otherwise. */
21692 static bool
21693 cp_parser_array_designator_p (cp_parser *parser)
21695 /* Consume the `['. */
21696 cp_lexer_consume_token (parser->lexer);
21698 cp_lexer_save_tokens (parser->lexer);
21700 /* Skip tokens until the next token is a closing square bracket.
21701 If we find the closing `]', and the next token is a `=', then
21702 we are looking at an array designator. */
21703 bool array_designator_p
21704 = (cp_parser_skip_to_closing_square_bracket (parser)
21705 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21707 /* Roll back the tokens we skipped. */
21708 cp_lexer_rollback_tokens (parser->lexer);
21710 return array_designator_p;
21713 /* Parse an initializer-list.
21715 initializer-list:
21716 initializer-clause ... [opt]
21717 initializer-list , initializer-clause ... [opt]
21719 GNU Extension:
21721 initializer-list:
21722 designation initializer-clause ...[opt]
21723 initializer-list , designation initializer-clause ...[opt]
21725 designation:
21726 . identifier =
21727 identifier :
21728 [ constant-expression ] =
21730 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21731 for the initializer. If the INDEX of the elt is non-NULL, it is the
21732 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21733 as for cp_parser_initializer. */
21735 static vec<constructor_elt, va_gc> *
21736 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21738 vec<constructor_elt, va_gc> *v = NULL;
21740 /* Assume all of the expressions are constant. */
21741 *non_constant_p = false;
21743 /* Parse the rest of the list. */
21744 while (true)
21746 cp_token *token;
21747 tree designator;
21748 tree initializer;
21749 bool clause_non_constant_p;
21751 /* If the next token is an identifier and the following one is a
21752 colon, we are looking at the GNU designated-initializer
21753 syntax. */
21754 if (cp_parser_allow_gnu_extensions_p (parser)
21755 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21756 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21758 /* Warn the user that they are using an extension. */
21759 pedwarn (input_location, OPT_Wpedantic,
21760 "ISO C++ does not allow designated initializers");
21761 /* Consume the identifier. */
21762 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21763 /* Consume the `:'. */
21764 cp_lexer_consume_token (parser->lexer);
21766 /* Also handle the C99 syntax, '. id ='. */
21767 else if (cp_parser_allow_gnu_extensions_p (parser)
21768 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21769 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21770 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21772 /* Warn the user that they are using an extension. */
21773 pedwarn (input_location, OPT_Wpedantic,
21774 "ISO C++ does not allow C99 designated initializers");
21775 /* Consume the `.'. */
21776 cp_lexer_consume_token (parser->lexer);
21777 /* Consume the identifier. */
21778 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21779 /* Consume the `='. */
21780 cp_lexer_consume_token (parser->lexer);
21782 /* Also handle C99 array designators, '[ const ] ='. */
21783 else if (cp_parser_allow_gnu_extensions_p (parser)
21784 && !c_dialect_objc ()
21785 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21787 /* In C++11, [ could start a lambda-introducer. */
21788 bool non_const = false;
21790 cp_parser_parse_tentatively (parser);
21792 if (!cp_parser_array_designator_p (parser))
21794 cp_parser_simulate_error (parser);
21795 designator = NULL_TREE;
21797 else
21799 designator = cp_parser_constant_expression (parser, true,
21800 &non_const);
21801 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21802 cp_parser_require (parser, CPP_EQ, RT_EQ);
21805 if (!cp_parser_parse_definitely (parser))
21806 designator = NULL_TREE;
21807 else if (non_const)
21808 require_potential_rvalue_constant_expression (designator);
21810 else
21811 designator = NULL_TREE;
21813 /* Parse the initializer. */
21814 initializer = cp_parser_initializer_clause (parser,
21815 &clause_non_constant_p);
21816 /* If any clause is non-constant, so is the entire initializer. */
21817 if (clause_non_constant_p)
21818 *non_constant_p = true;
21820 /* If we have an ellipsis, this is an initializer pack
21821 expansion. */
21822 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21824 /* Consume the `...'. */
21825 cp_lexer_consume_token (parser->lexer);
21827 /* Turn the initializer into an initializer expansion. */
21828 initializer = make_pack_expansion (initializer);
21831 /* Add it to the vector. */
21832 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21834 /* If the next token is not a comma, we have reached the end of
21835 the list. */
21836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21837 break;
21839 /* Peek at the next token. */
21840 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21841 /* If the next token is a `}', then we're still done. An
21842 initializer-clause can have a trailing `,' after the
21843 initializer-list and before the closing `}'. */
21844 if (token->type == CPP_CLOSE_BRACE)
21845 break;
21847 /* Consume the `,' token. */
21848 cp_lexer_consume_token (parser->lexer);
21851 return v;
21854 /* Classes [gram.class] */
21856 /* Parse a class-name.
21858 class-name:
21859 identifier
21860 template-id
21862 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21863 to indicate that names looked up in dependent types should be
21864 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21865 keyword has been used to indicate that the name that appears next
21866 is a template. TAG_TYPE indicates the explicit tag given before
21867 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21868 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21869 is the class being defined in a class-head. If ENUM_OK is TRUE,
21870 enum-names are also accepted.
21872 Returns the TYPE_DECL representing the class. */
21874 static tree
21875 cp_parser_class_name (cp_parser *parser,
21876 bool typename_keyword_p,
21877 bool template_keyword_p,
21878 enum tag_types tag_type,
21879 bool check_dependency_p,
21880 bool class_head_p,
21881 bool is_declaration,
21882 bool enum_ok)
21884 tree decl;
21885 tree scope;
21886 bool typename_p;
21887 cp_token *token;
21888 tree identifier = NULL_TREE;
21890 /* All class-names start with an identifier. */
21891 token = cp_lexer_peek_token (parser->lexer);
21892 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21894 cp_parser_error (parser, "expected class-name");
21895 return error_mark_node;
21898 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21899 to a template-id, so we save it here. */
21900 scope = parser->scope;
21901 if (scope == error_mark_node)
21902 return error_mark_node;
21904 /* Any name names a type if we're following the `typename' keyword
21905 in a qualified name where the enclosing scope is type-dependent. */
21906 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21907 && dependent_type_p (scope));
21908 /* Handle the common case (an identifier, but not a template-id)
21909 efficiently. */
21910 if (token->type == CPP_NAME
21911 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21913 cp_token *identifier_token;
21914 bool ambiguous_p;
21916 /* Look for the identifier. */
21917 identifier_token = cp_lexer_peek_token (parser->lexer);
21918 ambiguous_p = identifier_token->error_reported;
21919 identifier = cp_parser_identifier (parser);
21920 /* If the next token isn't an identifier, we are certainly not
21921 looking at a class-name. */
21922 if (identifier == error_mark_node)
21923 decl = error_mark_node;
21924 /* If we know this is a type-name, there's no need to look it
21925 up. */
21926 else if (typename_p)
21927 decl = identifier;
21928 else
21930 tree ambiguous_decls;
21931 /* If we already know that this lookup is ambiguous, then
21932 we've already issued an error message; there's no reason
21933 to check again. */
21934 if (ambiguous_p)
21936 cp_parser_simulate_error (parser);
21937 return error_mark_node;
21939 /* If the next token is a `::', then the name must be a type
21940 name.
21942 [basic.lookup.qual]
21944 During the lookup for a name preceding the :: scope
21945 resolution operator, object, function, and enumerator
21946 names are ignored. */
21947 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21948 tag_type = scope_type;
21949 /* Look up the name. */
21950 decl = cp_parser_lookup_name (parser, identifier,
21951 tag_type,
21952 /*is_template=*/false,
21953 /*is_namespace=*/false,
21954 check_dependency_p,
21955 &ambiguous_decls,
21956 identifier_token->location);
21957 if (ambiguous_decls)
21959 if (cp_parser_parsing_tentatively (parser))
21960 cp_parser_simulate_error (parser);
21961 return error_mark_node;
21965 else
21967 /* Try a template-id. */
21968 decl = cp_parser_template_id (parser, template_keyword_p,
21969 check_dependency_p,
21970 tag_type,
21971 is_declaration);
21972 if (decl == error_mark_node)
21973 return error_mark_node;
21976 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21978 /* If this is a typename, create a TYPENAME_TYPE. */
21979 if (typename_p && decl != error_mark_node)
21981 decl = make_typename_type (scope, decl, typename_type,
21982 /*complain=*/tf_error);
21983 if (decl != error_mark_node)
21984 decl = TYPE_NAME (decl);
21987 decl = strip_using_decl (decl);
21989 /* Check to see that it is really the name of a class. */
21990 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21991 && identifier_p (TREE_OPERAND (decl, 0))
21992 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21993 /* Situations like this:
21995 template <typename T> struct A {
21996 typename T::template X<int>::I i;
21999 are problematic. Is `T::template X<int>' a class-name? The
22000 standard does not seem to be definitive, but there is no other
22001 valid interpretation of the following `::'. Therefore, those
22002 names are considered class-names. */
22004 decl = make_typename_type (scope, decl, tag_type, tf_error);
22005 if (decl != error_mark_node)
22006 decl = TYPE_NAME (decl);
22008 else if (TREE_CODE (decl) != TYPE_DECL
22009 || TREE_TYPE (decl) == error_mark_node
22010 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22011 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22012 /* In Objective-C 2.0, a classname followed by '.' starts a
22013 dot-syntax expression, and it's not a type-name. */
22014 || (c_dialect_objc ()
22015 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22016 && objc_is_class_name (decl)))
22017 decl = error_mark_node;
22019 if (decl == error_mark_node)
22020 cp_parser_error (parser, "expected class-name");
22021 else if (identifier && !parser->scope)
22022 maybe_note_name_used_in_class (identifier, decl);
22024 return decl;
22027 /* Parse a class-specifier.
22029 class-specifier:
22030 class-head { member-specification [opt] }
22032 Returns the TREE_TYPE representing the class. */
22034 static tree
22035 cp_parser_class_specifier_1 (cp_parser* parser)
22037 tree type;
22038 tree attributes = NULL_TREE;
22039 bool nested_name_specifier_p;
22040 unsigned saved_num_template_parameter_lists;
22041 bool saved_in_function_body;
22042 unsigned char in_statement;
22043 bool in_switch_statement_p;
22044 bool saved_in_unbraced_linkage_specification_p;
22045 tree old_scope = NULL_TREE;
22046 tree scope = NULL_TREE;
22047 cp_token *closing_brace;
22049 push_deferring_access_checks (dk_no_deferred);
22051 /* Parse the class-head. */
22052 type = cp_parser_class_head (parser,
22053 &nested_name_specifier_p);
22054 /* If the class-head was a semantic disaster, skip the entire body
22055 of the class. */
22056 if (!type)
22058 cp_parser_skip_to_end_of_block_or_statement (parser);
22059 pop_deferring_access_checks ();
22060 return error_mark_node;
22063 /* Look for the `{'. */
22064 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22066 pop_deferring_access_checks ();
22067 return error_mark_node;
22070 cp_ensure_no_omp_declare_simd (parser);
22071 cp_ensure_no_oacc_routine (parser);
22073 /* Issue an error message if type-definitions are forbidden here. */
22074 cp_parser_check_type_definition (parser);
22075 /* Remember that we are defining one more class. */
22076 ++parser->num_classes_being_defined;
22077 /* Inside the class, surrounding template-parameter-lists do not
22078 apply. */
22079 saved_num_template_parameter_lists
22080 = parser->num_template_parameter_lists;
22081 parser->num_template_parameter_lists = 0;
22082 /* We are not in a function body. */
22083 saved_in_function_body = parser->in_function_body;
22084 parser->in_function_body = false;
22085 /* Or in a loop. */
22086 in_statement = parser->in_statement;
22087 parser->in_statement = 0;
22088 /* Or in a switch. */
22089 in_switch_statement_p = parser->in_switch_statement_p;
22090 parser->in_switch_statement_p = false;
22091 /* We are not immediately inside an extern "lang" block. */
22092 saved_in_unbraced_linkage_specification_p
22093 = parser->in_unbraced_linkage_specification_p;
22094 parser->in_unbraced_linkage_specification_p = false;
22096 // Associate constraints with the type.
22097 if (flag_concepts)
22098 type = associate_classtype_constraints (type);
22100 /* Start the class. */
22101 if (nested_name_specifier_p)
22103 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22104 old_scope = push_inner_scope (scope);
22106 type = begin_class_definition (type);
22108 if (type == error_mark_node)
22109 /* If the type is erroneous, skip the entire body of the class. */
22110 cp_parser_skip_to_closing_brace (parser);
22111 else
22112 /* Parse the member-specification. */
22113 cp_parser_member_specification_opt (parser);
22115 /* Look for the trailing `}'. */
22116 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22117 /* Look for trailing attributes to apply to this class. */
22118 if (cp_parser_allow_gnu_extensions_p (parser))
22119 attributes = cp_parser_gnu_attributes_opt (parser);
22120 if (type != error_mark_node)
22121 type = finish_struct (type, attributes);
22122 if (nested_name_specifier_p)
22123 pop_inner_scope (old_scope, scope);
22125 /* We've finished a type definition. Check for the common syntax
22126 error of forgetting a semicolon after the definition. We need to
22127 be careful, as we can't just check for not-a-semicolon and be done
22128 with it; the user might have typed:
22130 class X { } c = ...;
22131 class X { } *p = ...;
22133 and so forth. Instead, enumerate all the possible tokens that
22134 might follow this production; if we don't see one of them, then
22135 complain and silently insert the semicolon. */
22137 cp_token *token = cp_lexer_peek_token (parser->lexer);
22138 bool want_semicolon = true;
22140 if (cp_next_tokens_can_be_std_attribute_p (parser))
22141 /* Don't try to parse c++11 attributes here. As per the
22142 grammar, that should be a task for
22143 cp_parser_decl_specifier_seq. */
22144 want_semicolon = false;
22146 switch (token->type)
22148 case CPP_NAME:
22149 case CPP_SEMICOLON:
22150 case CPP_MULT:
22151 case CPP_AND:
22152 case CPP_OPEN_PAREN:
22153 case CPP_CLOSE_PAREN:
22154 case CPP_COMMA:
22155 want_semicolon = false;
22156 break;
22158 /* While it's legal for type qualifiers and storage class
22159 specifiers to follow type definitions in the grammar, only
22160 compiler testsuites contain code like that. Assume that if
22161 we see such code, then what we're really seeing is a case
22162 like:
22164 class X { }
22165 const <type> var = ...;
22169 class Y { }
22170 static <type> func (...) ...
22172 i.e. the qualifier or specifier applies to the next
22173 declaration. To do so, however, we need to look ahead one
22174 more token to see if *that* token is a type specifier.
22176 This code could be improved to handle:
22178 class Z { }
22179 static const <type> var = ...; */
22180 case CPP_KEYWORD:
22181 if (keyword_is_decl_specifier (token->keyword))
22183 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22185 /* Handling user-defined types here would be nice, but very
22186 tricky. */
22187 want_semicolon
22188 = (lookahead->type == CPP_KEYWORD
22189 && keyword_begins_type_specifier (lookahead->keyword));
22191 break;
22192 default:
22193 break;
22196 /* If we don't have a type, then something is very wrong and we
22197 shouldn't try to do anything clever. Likewise for not seeing the
22198 closing brace. */
22199 if (closing_brace && TYPE_P (type) && want_semicolon)
22201 /* Locate the closing brace. */
22202 cp_token_position prev
22203 = cp_lexer_previous_token_position (parser->lexer);
22204 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22205 location_t loc = prev_token->location;
22207 /* We want to suggest insertion of a ';' immediately *after* the
22208 closing brace, so, if we can, offset the location by 1 column. */
22209 location_t next_loc = loc;
22210 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22211 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22213 rich_location richloc (line_table, next_loc);
22215 /* If we successfully offset the location, suggest the fix-it. */
22216 if (next_loc != loc)
22217 richloc.add_fixit_insert_before (next_loc, ";");
22219 if (CLASSTYPE_DECLARED_CLASS (type))
22220 error_at_rich_loc (&richloc,
22221 "expected %<;%> after class definition");
22222 else if (TREE_CODE (type) == RECORD_TYPE)
22223 error_at_rich_loc (&richloc,
22224 "expected %<;%> after struct definition");
22225 else if (TREE_CODE (type) == UNION_TYPE)
22226 error_at_rich_loc (&richloc,
22227 "expected %<;%> after union definition");
22228 else
22229 gcc_unreachable ();
22231 /* Unget one token and smash it to look as though we encountered
22232 a semicolon in the input stream. */
22233 cp_lexer_set_token_position (parser->lexer, prev);
22234 token = cp_lexer_peek_token (parser->lexer);
22235 token->type = CPP_SEMICOLON;
22236 token->keyword = RID_MAX;
22240 /* If this class is not itself within the scope of another class,
22241 then we need to parse the bodies of all of the queued function
22242 definitions. Note that the queued functions defined in a class
22243 are not always processed immediately following the
22244 class-specifier for that class. Consider:
22246 struct A {
22247 struct B { void f() { sizeof (A); } };
22250 If `f' were processed before the processing of `A' were
22251 completed, there would be no way to compute the size of `A'.
22252 Note that the nesting we are interested in here is lexical --
22253 not the semantic nesting given by TYPE_CONTEXT. In particular,
22254 for:
22256 struct A { struct B; };
22257 struct A::B { void f() { } };
22259 there is no need to delay the parsing of `A::B::f'. */
22260 if (--parser->num_classes_being_defined == 0)
22262 tree decl;
22263 tree class_type = NULL_TREE;
22264 tree pushed_scope = NULL_TREE;
22265 unsigned ix;
22266 cp_default_arg_entry *e;
22267 tree save_ccp, save_ccr;
22269 /* In a first pass, parse default arguments to the functions.
22270 Then, in a second pass, parse the bodies of the functions.
22271 This two-phased approach handles cases like:
22273 struct S {
22274 void f() { g(); }
22275 void g(int i = 3);
22279 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22281 decl = e->decl;
22282 /* If there are default arguments that have not yet been processed,
22283 take care of them now. */
22284 if (class_type != e->class_type)
22286 if (pushed_scope)
22287 pop_scope (pushed_scope);
22288 class_type = e->class_type;
22289 pushed_scope = push_scope (class_type);
22291 /* Make sure that any template parameters are in scope. */
22292 maybe_begin_member_template_processing (decl);
22293 /* Parse the default argument expressions. */
22294 cp_parser_late_parsing_default_args (parser, decl);
22295 /* Remove any template parameters from the symbol table. */
22296 maybe_end_member_template_processing ();
22298 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22299 /* Now parse any NSDMIs. */
22300 save_ccp = current_class_ptr;
22301 save_ccr = current_class_ref;
22302 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22304 if (class_type != DECL_CONTEXT (decl))
22306 if (pushed_scope)
22307 pop_scope (pushed_scope);
22308 class_type = DECL_CONTEXT (decl);
22309 pushed_scope = push_scope (class_type);
22311 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22312 cp_parser_late_parsing_nsdmi (parser, decl);
22314 vec_safe_truncate (unparsed_nsdmis, 0);
22315 current_class_ptr = save_ccp;
22316 current_class_ref = save_ccr;
22317 if (pushed_scope)
22318 pop_scope (pushed_scope);
22320 /* Now do some post-NSDMI bookkeeping. */
22321 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22322 after_nsdmi_defaulted_late_checks (class_type);
22323 vec_safe_truncate (unparsed_classes, 0);
22324 after_nsdmi_defaulted_late_checks (type);
22326 /* Now parse the body of the functions. */
22327 if (flag_openmp)
22329 /* OpenMP UDRs need to be parsed before all other functions. */
22330 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22331 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22332 cp_parser_late_parsing_for_member (parser, decl);
22333 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22334 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22335 cp_parser_late_parsing_for_member (parser, decl);
22337 else
22338 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22339 cp_parser_late_parsing_for_member (parser, decl);
22340 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22342 else
22343 vec_safe_push (unparsed_classes, type);
22345 /* Put back any saved access checks. */
22346 pop_deferring_access_checks ();
22348 /* Restore saved state. */
22349 parser->in_switch_statement_p = in_switch_statement_p;
22350 parser->in_statement = in_statement;
22351 parser->in_function_body = saved_in_function_body;
22352 parser->num_template_parameter_lists
22353 = saved_num_template_parameter_lists;
22354 parser->in_unbraced_linkage_specification_p
22355 = saved_in_unbraced_linkage_specification_p;
22357 return type;
22360 static tree
22361 cp_parser_class_specifier (cp_parser* parser)
22363 tree ret;
22364 timevar_push (TV_PARSE_STRUCT);
22365 ret = cp_parser_class_specifier_1 (parser);
22366 timevar_pop (TV_PARSE_STRUCT);
22367 return ret;
22370 /* Parse a class-head.
22372 class-head:
22373 class-key identifier [opt] base-clause [opt]
22374 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22375 class-key nested-name-specifier [opt] template-id
22376 base-clause [opt]
22378 class-virt-specifier:
22379 final
22381 GNU Extensions:
22382 class-key attributes identifier [opt] base-clause [opt]
22383 class-key attributes nested-name-specifier identifier base-clause [opt]
22384 class-key attributes nested-name-specifier [opt] template-id
22385 base-clause [opt]
22387 Upon return BASES is initialized to the list of base classes (or
22388 NULL, if there are none) in the same form returned by
22389 cp_parser_base_clause.
22391 Returns the TYPE of the indicated class. Sets
22392 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22393 involving a nested-name-specifier was used, and FALSE otherwise.
22395 Returns error_mark_node if this is not a class-head.
22397 Returns NULL_TREE if the class-head is syntactically valid, but
22398 semantically invalid in a way that means we should skip the entire
22399 body of the class. */
22401 static tree
22402 cp_parser_class_head (cp_parser* parser,
22403 bool* nested_name_specifier_p)
22405 tree nested_name_specifier;
22406 enum tag_types class_key;
22407 tree id = NULL_TREE;
22408 tree type = NULL_TREE;
22409 tree attributes;
22410 tree bases;
22411 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22412 bool template_id_p = false;
22413 bool qualified_p = false;
22414 bool invalid_nested_name_p = false;
22415 bool invalid_explicit_specialization_p = false;
22416 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22417 tree pushed_scope = NULL_TREE;
22418 unsigned num_templates;
22419 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22420 /* Assume no nested-name-specifier will be present. */
22421 *nested_name_specifier_p = false;
22422 /* Assume no template parameter lists will be used in defining the
22423 type. */
22424 num_templates = 0;
22425 parser->colon_corrects_to_scope_p = false;
22427 /* Look for the class-key. */
22428 class_key = cp_parser_class_key (parser);
22429 if (class_key == none_type)
22430 return error_mark_node;
22432 location_t class_head_start_location = input_location;
22434 /* Parse the attributes. */
22435 attributes = cp_parser_attributes_opt (parser);
22437 /* If the next token is `::', that is invalid -- but sometimes
22438 people do try to write:
22440 struct ::S {};
22442 Handle this gracefully by accepting the extra qualifier, and then
22443 issuing an error about it later if this really is a
22444 class-head. If it turns out just to be an elaborated type
22445 specifier, remain silent. */
22446 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22447 qualified_p = true;
22449 push_deferring_access_checks (dk_no_check);
22451 /* Determine the name of the class. Begin by looking for an
22452 optional nested-name-specifier. */
22453 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22454 nested_name_specifier
22455 = cp_parser_nested_name_specifier_opt (parser,
22456 /*typename_keyword_p=*/false,
22457 /*check_dependency_p=*/false,
22458 /*type_p=*/true,
22459 /*is_declaration=*/false);
22460 /* If there was a nested-name-specifier, then there *must* be an
22461 identifier. */
22462 if (nested_name_specifier)
22464 type_start_token = cp_lexer_peek_token (parser->lexer);
22465 /* Although the grammar says `identifier', it really means
22466 `class-name' or `template-name'. You are only allowed to
22467 define a class that has already been declared with this
22468 syntax.
22470 The proposed resolution for Core Issue 180 says that wherever
22471 you see `class T::X' you should treat `X' as a type-name.
22473 It is OK to define an inaccessible class; for example:
22475 class A { class B; };
22476 class A::B {};
22478 We do not know if we will see a class-name, or a
22479 template-name. We look for a class-name first, in case the
22480 class-name is a template-id; if we looked for the
22481 template-name first we would stop after the template-name. */
22482 cp_parser_parse_tentatively (parser);
22483 type = cp_parser_class_name (parser,
22484 /*typename_keyword_p=*/false,
22485 /*template_keyword_p=*/false,
22486 class_type,
22487 /*check_dependency_p=*/false,
22488 /*class_head_p=*/true,
22489 /*is_declaration=*/false);
22490 /* If that didn't work, ignore the nested-name-specifier. */
22491 if (!cp_parser_parse_definitely (parser))
22493 invalid_nested_name_p = true;
22494 type_start_token = cp_lexer_peek_token (parser->lexer);
22495 id = cp_parser_identifier (parser);
22496 if (id == error_mark_node)
22497 id = NULL_TREE;
22499 /* If we could not find a corresponding TYPE, treat this
22500 declaration like an unqualified declaration. */
22501 if (type == error_mark_node)
22502 nested_name_specifier = NULL_TREE;
22503 /* Otherwise, count the number of templates used in TYPE and its
22504 containing scopes. */
22505 else
22507 tree scope;
22509 for (scope = TREE_TYPE (type);
22510 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22511 scope = get_containing_scope (scope))
22512 if (TYPE_P (scope)
22513 && CLASS_TYPE_P (scope)
22514 && CLASSTYPE_TEMPLATE_INFO (scope)
22515 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22516 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22517 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22518 ++num_templates;
22521 /* Otherwise, the identifier is optional. */
22522 else
22524 /* We don't know whether what comes next is a template-id,
22525 an identifier, or nothing at all. */
22526 cp_parser_parse_tentatively (parser);
22527 /* Check for a template-id. */
22528 type_start_token = cp_lexer_peek_token (parser->lexer);
22529 id = cp_parser_template_id (parser,
22530 /*template_keyword_p=*/false,
22531 /*check_dependency_p=*/true,
22532 class_key,
22533 /*is_declaration=*/true);
22534 /* If that didn't work, it could still be an identifier. */
22535 if (!cp_parser_parse_definitely (parser))
22537 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22539 type_start_token = cp_lexer_peek_token (parser->lexer);
22540 id = cp_parser_identifier (parser);
22542 else
22543 id = NULL_TREE;
22545 else
22547 template_id_p = true;
22548 ++num_templates;
22552 pop_deferring_access_checks ();
22554 if (id)
22556 cp_parser_check_for_invalid_template_id (parser, id,
22557 class_key,
22558 type_start_token->location);
22560 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22562 /* If it's not a `:' or a `{' then we can't really be looking at a
22563 class-head, since a class-head only appears as part of a
22564 class-specifier. We have to detect this situation before calling
22565 xref_tag, since that has irreversible side-effects. */
22566 if (!cp_parser_next_token_starts_class_definition_p (parser))
22568 cp_parser_error (parser, "expected %<{%> or %<:%>");
22569 type = error_mark_node;
22570 goto out;
22573 /* At this point, we're going ahead with the class-specifier, even
22574 if some other problem occurs. */
22575 cp_parser_commit_to_tentative_parse (parser);
22576 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22578 cp_parser_error (parser,
22579 "cannot specify %<override%> for a class");
22580 type = error_mark_node;
22581 goto out;
22583 /* Issue the error about the overly-qualified name now. */
22584 if (qualified_p)
22586 cp_parser_error (parser,
22587 "global qualification of class name is invalid");
22588 type = error_mark_node;
22589 goto out;
22591 else if (invalid_nested_name_p)
22593 cp_parser_error (parser,
22594 "qualified name does not name a class");
22595 type = error_mark_node;
22596 goto out;
22598 else if (nested_name_specifier)
22600 tree scope;
22602 /* Reject typedef-names in class heads. */
22603 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22605 error_at (type_start_token->location,
22606 "invalid class name in declaration of %qD",
22607 type);
22608 type = NULL_TREE;
22609 goto done;
22612 /* Figure out in what scope the declaration is being placed. */
22613 scope = current_scope ();
22614 /* If that scope does not contain the scope in which the
22615 class was originally declared, the program is invalid. */
22616 if (scope && !is_ancestor (scope, nested_name_specifier))
22618 if (at_namespace_scope_p ())
22619 error_at (type_start_token->location,
22620 "declaration of %qD in namespace %qD which does not "
22621 "enclose %qD",
22622 type, scope, nested_name_specifier);
22623 else
22624 error_at (type_start_token->location,
22625 "declaration of %qD in %qD which does not enclose %qD",
22626 type, scope, nested_name_specifier);
22627 type = NULL_TREE;
22628 goto done;
22630 /* [dcl.meaning]
22632 A declarator-id shall not be qualified except for the
22633 definition of a ... nested class outside of its class
22634 ... [or] the definition or explicit instantiation of a
22635 class member of a namespace outside of its namespace. */
22636 if (scope == nested_name_specifier)
22638 permerror (nested_name_specifier_token_start->location,
22639 "extra qualification not allowed");
22640 nested_name_specifier = NULL_TREE;
22641 num_templates = 0;
22644 /* An explicit-specialization must be preceded by "template <>". If
22645 it is not, try to recover gracefully. */
22646 if (at_namespace_scope_p ()
22647 && parser->num_template_parameter_lists == 0
22648 && !processing_template_parmlist
22649 && template_id_p)
22651 /* Build a location of this form:
22652 struct typename <ARGS>
22653 ^~~~~~~~~~~~~~~~~~~~~~
22654 with caret==start at the start token, and
22655 finishing at the end of the type. */
22656 location_t reported_loc
22657 = make_location (class_head_start_location,
22658 class_head_start_location,
22659 get_finish (type_start_token->location));
22660 rich_location richloc (line_table, reported_loc);
22661 richloc.add_fixit_insert_before (class_head_start_location,
22662 "template <> ");
22663 error_at_rich_loc
22664 (&richloc,
22665 "an explicit specialization must be preceded by %<template <>%>");
22666 invalid_explicit_specialization_p = true;
22667 /* Take the same action that would have been taken by
22668 cp_parser_explicit_specialization. */
22669 ++parser->num_template_parameter_lists;
22670 begin_specialization ();
22672 /* There must be no "return" statements between this point and the
22673 end of this function; set "type "to the correct return value and
22674 use "goto done;" to return. */
22675 /* Make sure that the right number of template parameters were
22676 present. */
22677 if (!cp_parser_check_template_parameters (parser, num_templates,
22678 type_start_token->location,
22679 /*declarator=*/NULL))
22681 /* If something went wrong, there is no point in even trying to
22682 process the class-definition. */
22683 type = NULL_TREE;
22684 goto done;
22687 /* Look up the type. */
22688 if (template_id_p)
22690 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22691 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22692 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22694 error_at (type_start_token->location,
22695 "function template %qD redeclared as a class template", id);
22696 type = error_mark_node;
22698 else
22700 type = TREE_TYPE (id);
22701 type = maybe_process_partial_specialization (type);
22703 /* Check the scope while we still know whether or not we had a
22704 nested-name-specifier. */
22705 if (type != error_mark_node)
22706 check_unqualified_spec_or_inst (type, type_start_token->location);
22708 if (nested_name_specifier)
22709 pushed_scope = push_scope (nested_name_specifier);
22711 else if (nested_name_specifier)
22713 tree class_type;
22715 /* Given:
22717 template <typename T> struct S { struct T };
22718 template <typename T> struct S<T>::T { };
22720 we will get a TYPENAME_TYPE when processing the definition of
22721 `S::T'. We need to resolve it to the actual type before we
22722 try to define it. */
22723 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22725 class_type = resolve_typename_type (TREE_TYPE (type),
22726 /*only_current_p=*/false);
22727 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22728 type = TYPE_NAME (class_type);
22729 else
22731 cp_parser_error (parser, "could not resolve typename type");
22732 type = error_mark_node;
22736 if (maybe_process_partial_specialization (TREE_TYPE (type))
22737 == error_mark_node)
22739 type = NULL_TREE;
22740 goto done;
22743 class_type = current_class_type;
22744 /* Enter the scope indicated by the nested-name-specifier. */
22745 pushed_scope = push_scope (nested_name_specifier);
22746 /* Get the canonical version of this type. */
22747 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22748 /* Call push_template_decl if it seems like we should be defining a
22749 template either from the template headers or the type we're
22750 defining, so that we diagnose both extra and missing headers. */
22751 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22752 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22753 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22755 type = push_template_decl (type);
22756 if (type == error_mark_node)
22758 type = NULL_TREE;
22759 goto done;
22763 type = TREE_TYPE (type);
22764 *nested_name_specifier_p = true;
22766 else /* The name is not a nested name. */
22768 /* If the class was unnamed, create a dummy name. */
22769 if (!id)
22770 id = make_anon_name ();
22771 tag_scope tag_scope = (parser->in_type_id_in_expr_p
22772 ? ts_within_enclosing_non_class
22773 : ts_current);
22774 type = xref_tag (class_key, id, tag_scope,
22775 parser->num_template_parameter_lists);
22778 /* Indicate whether this class was declared as a `class' or as a
22779 `struct'. */
22780 if (TREE_CODE (type) == RECORD_TYPE)
22781 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22782 cp_parser_check_class_key (class_key, type);
22784 /* If this type was already complete, and we see another definition,
22785 that's an error. */
22786 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22788 error_at (type_start_token->location, "redefinition of %q#T",
22789 type);
22790 inform (location_of (type), "previous definition of %q#T",
22791 type);
22792 type = NULL_TREE;
22793 goto done;
22795 else if (type == error_mark_node)
22796 type = NULL_TREE;
22798 if (type)
22800 /* Apply attributes now, before any use of the class as a template
22801 argument in its base list. */
22802 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22803 fixup_attribute_variants (type);
22806 /* We will have entered the scope containing the class; the names of
22807 base classes should be looked up in that context. For example:
22809 struct A { struct B {}; struct C; };
22810 struct A::C : B {};
22812 is valid. */
22814 /* Get the list of base-classes, if there is one. */
22815 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22817 /* PR59482: enter the class scope so that base-specifiers are looked
22818 up correctly. */
22819 if (type)
22820 pushclass (type);
22821 bases = cp_parser_base_clause (parser);
22822 /* PR59482: get out of the previously pushed class scope so that the
22823 subsequent pops pop the right thing. */
22824 if (type)
22825 popclass ();
22827 else
22828 bases = NULL_TREE;
22830 /* If we're really defining a class, process the base classes.
22831 If they're invalid, fail. */
22832 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22833 xref_basetypes (type, bases);
22835 done:
22836 /* Leave the scope given by the nested-name-specifier. We will
22837 enter the class scope itself while processing the members. */
22838 if (pushed_scope)
22839 pop_scope (pushed_scope);
22841 if (invalid_explicit_specialization_p)
22843 end_specialization ();
22844 --parser->num_template_parameter_lists;
22847 if (type)
22848 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22849 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22850 CLASSTYPE_FINAL (type) = 1;
22851 out:
22852 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22853 return type;
22856 /* Parse a class-key.
22858 class-key:
22859 class
22860 struct
22861 union
22863 Returns the kind of class-key specified, or none_type to indicate
22864 error. */
22866 static enum tag_types
22867 cp_parser_class_key (cp_parser* parser)
22869 cp_token *token;
22870 enum tag_types tag_type;
22872 /* Look for the class-key. */
22873 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22874 if (!token)
22875 return none_type;
22877 /* Check to see if the TOKEN is a class-key. */
22878 tag_type = cp_parser_token_is_class_key (token);
22879 if (!tag_type)
22880 cp_parser_error (parser, "expected class-key");
22881 return tag_type;
22884 /* Parse a type-parameter-key.
22886 type-parameter-key:
22887 class
22888 typename
22891 static void
22892 cp_parser_type_parameter_key (cp_parser* parser)
22894 /* Look for the type-parameter-key. */
22895 enum tag_types tag_type = none_type;
22896 cp_token *token = cp_lexer_peek_token (parser->lexer);
22897 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22899 cp_lexer_consume_token (parser->lexer);
22900 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22901 /* typename is not allowed in a template template parameter
22902 by the standard until C++1Z. */
22903 pedwarn (token->location, OPT_Wpedantic,
22904 "ISO C++ forbids typename key in template template parameter;"
22905 " use -std=c++1z or -std=gnu++1z");
22907 else
22908 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22910 return;
22913 /* Parse an (optional) member-specification.
22915 member-specification:
22916 member-declaration member-specification [opt]
22917 access-specifier : member-specification [opt] */
22919 static void
22920 cp_parser_member_specification_opt (cp_parser* parser)
22922 while (true)
22924 cp_token *token;
22925 enum rid keyword;
22927 /* Peek at the next token. */
22928 token = cp_lexer_peek_token (parser->lexer);
22929 /* If it's a `}', or EOF then we've seen all the members. */
22930 if (token->type == CPP_CLOSE_BRACE
22931 || token->type == CPP_EOF
22932 || token->type == CPP_PRAGMA_EOL)
22933 break;
22935 /* See if this token is a keyword. */
22936 keyword = token->keyword;
22937 switch (keyword)
22939 case RID_PUBLIC:
22940 case RID_PROTECTED:
22941 case RID_PRIVATE:
22942 /* Consume the access-specifier. */
22943 cp_lexer_consume_token (parser->lexer);
22944 /* Remember which access-specifier is active. */
22945 current_access_specifier = token->u.value;
22946 /* Look for the `:'. */
22947 cp_parser_require (parser, CPP_COLON, RT_COLON);
22948 break;
22950 default:
22951 /* Accept #pragmas at class scope. */
22952 if (token->type == CPP_PRAGMA)
22954 cp_parser_pragma (parser, pragma_member, NULL);
22955 break;
22958 /* Otherwise, the next construction must be a
22959 member-declaration. */
22960 cp_parser_member_declaration (parser);
22965 /* Parse a member-declaration.
22967 member-declaration:
22968 decl-specifier-seq [opt] member-declarator-list [opt] ;
22969 function-definition ; [opt]
22970 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22971 using-declaration
22972 template-declaration
22973 alias-declaration
22975 member-declarator-list:
22976 member-declarator
22977 member-declarator-list , member-declarator
22979 member-declarator:
22980 declarator pure-specifier [opt]
22981 declarator constant-initializer [opt]
22982 identifier [opt] : constant-expression
22984 GNU Extensions:
22986 member-declaration:
22987 __extension__ member-declaration
22989 member-declarator:
22990 declarator attributes [opt] pure-specifier [opt]
22991 declarator attributes [opt] constant-initializer [opt]
22992 identifier [opt] attributes [opt] : constant-expression
22994 C++0x Extensions:
22996 member-declaration:
22997 static_assert-declaration */
22999 static void
23000 cp_parser_member_declaration (cp_parser* parser)
23002 cp_decl_specifier_seq decl_specifiers;
23003 tree prefix_attributes;
23004 tree decl;
23005 int declares_class_or_enum;
23006 bool friend_p;
23007 cp_token *token = NULL;
23008 cp_token *decl_spec_token_start = NULL;
23009 cp_token *initializer_token_start = NULL;
23010 int saved_pedantic;
23011 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23013 /* Check for the `__extension__' keyword. */
23014 if (cp_parser_extension_opt (parser, &saved_pedantic))
23016 /* Recurse. */
23017 cp_parser_member_declaration (parser);
23018 /* Restore the old value of the PEDANTIC flag. */
23019 pedantic = saved_pedantic;
23021 return;
23024 /* Check for a template-declaration. */
23025 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23027 /* An explicit specialization here is an error condition, and we
23028 expect the specialization handler to detect and report this. */
23029 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23030 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23031 cp_parser_explicit_specialization (parser);
23032 else
23033 cp_parser_template_declaration (parser, /*member_p=*/true);
23035 return;
23037 /* Check for a template introduction. */
23038 else if (cp_parser_template_declaration_after_export (parser, true))
23039 return;
23041 /* Check for a using-declaration. */
23042 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23044 if (cxx_dialect < cxx11)
23046 /* Parse the using-declaration. */
23047 cp_parser_using_declaration (parser,
23048 /*access_declaration_p=*/false);
23049 return;
23051 else
23053 tree decl;
23054 bool alias_decl_expected;
23055 cp_parser_parse_tentatively (parser);
23056 decl = cp_parser_alias_declaration (parser);
23057 /* Note that if we actually see the '=' token after the
23058 identifier, cp_parser_alias_declaration commits the
23059 tentative parse. In that case, we really expect an
23060 alias-declaration. Otherwise, we expect a using
23061 declaration. */
23062 alias_decl_expected =
23063 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23064 cp_parser_parse_definitely (parser);
23066 if (alias_decl_expected)
23067 finish_member_declaration (decl);
23068 else
23069 cp_parser_using_declaration (parser,
23070 /*access_declaration_p=*/false);
23071 return;
23075 /* Check for @defs. */
23076 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23078 tree ivar, member;
23079 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23080 ivar = ivar_chains;
23081 while (ivar)
23083 member = ivar;
23084 ivar = TREE_CHAIN (member);
23085 TREE_CHAIN (member) = NULL_TREE;
23086 finish_member_declaration (member);
23088 return;
23091 /* If the next token is `static_assert' we have a static assertion. */
23092 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23094 cp_parser_static_assert (parser, /*member_p=*/true);
23095 return;
23098 parser->colon_corrects_to_scope_p = false;
23100 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23101 goto out;
23103 /* Parse the decl-specifier-seq. */
23104 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23105 cp_parser_decl_specifier_seq (parser,
23106 CP_PARSER_FLAGS_OPTIONAL,
23107 &decl_specifiers,
23108 &declares_class_or_enum);
23109 /* Check for an invalid type-name. */
23110 if (!decl_specifiers.any_type_specifiers_p
23111 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23112 goto out;
23113 /* If there is no declarator, then the decl-specifier-seq should
23114 specify a type. */
23115 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23117 /* If there was no decl-specifier-seq, and the next token is a
23118 `;', then we have something like:
23120 struct S { ; };
23122 [class.mem]
23124 Each member-declaration shall declare at least one member
23125 name of the class. */
23126 if (!decl_specifiers.any_specifiers_p)
23128 cp_token *token = cp_lexer_peek_token (parser->lexer);
23129 if (!in_system_header_at (token->location))
23130 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
23132 else
23134 tree type;
23136 /* See if this declaration is a friend. */
23137 friend_p = cp_parser_friend_p (&decl_specifiers);
23138 /* If there were decl-specifiers, check to see if there was
23139 a class-declaration. */
23140 type = check_tag_decl (&decl_specifiers,
23141 /*explicit_type_instantiation_p=*/false);
23142 /* Nested classes have already been added to the class, but
23143 a `friend' needs to be explicitly registered. */
23144 if (friend_p)
23146 /* If the `friend' keyword was present, the friend must
23147 be introduced with a class-key. */
23148 if (!declares_class_or_enum && cxx_dialect < cxx11)
23149 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23150 "in C++03 a class-key must be used "
23151 "when declaring a friend");
23152 /* In this case:
23154 template <typename T> struct A {
23155 friend struct A<T>::B;
23158 A<T>::B will be represented by a TYPENAME_TYPE, and
23159 therefore not recognized by check_tag_decl. */
23160 if (!type)
23162 type = decl_specifiers.type;
23163 if (type && TREE_CODE (type) == TYPE_DECL)
23164 type = TREE_TYPE (type);
23166 if (!type || !TYPE_P (type))
23167 error_at (decl_spec_token_start->location,
23168 "friend declaration does not name a class or "
23169 "function");
23170 else
23171 make_friend_class (current_class_type, type,
23172 /*complain=*/true);
23174 /* If there is no TYPE, an error message will already have
23175 been issued. */
23176 else if (!type || type == error_mark_node)
23178 /* An anonymous aggregate has to be handled specially; such
23179 a declaration really declares a data member (with a
23180 particular type), as opposed to a nested class. */
23181 else if (ANON_AGGR_TYPE_P (type))
23183 /* C++11 9.5/6. */
23184 if (decl_specifiers.storage_class != sc_none)
23185 error_at (decl_spec_token_start->location,
23186 "a storage class on an anonymous aggregate "
23187 "in class scope is not allowed");
23189 /* Remove constructors and such from TYPE, now that we
23190 know it is an anonymous aggregate. */
23191 fixup_anonymous_aggr (type);
23192 /* And make the corresponding data member. */
23193 decl = build_decl (decl_spec_token_start->location,
23194 FIELD_DECL, NULL_TREE, type);
23195 /* Add it to the class. */
23196 finish_member_declaration (decl);
23198 else
23199 cp_parser_check_access_in_redeclaration
23200 (TYPE_NAME (type),
23201 decl_spec_token_start->location);
23204 else
23206 bool assume_semicolon = false;
23208 /* Clear attributes from the decl_specifiers but keep them
23209 around as prefix attributes that apply them to the entity
23210 being declared. */
23211 prefix_attributes = decl_specifiers.attributes;
23212 decl_specifiers.attributes = NULL_TREE;
23214 /* See if these declarations will be friends. */
23215 friend_p = cp_parser_friend_p (&decl_specifiers);
23217 /* Keep going until we hit the `;' at the end of the
23218 declaration. */
23219 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23221 tree attributes = NULL_TREE;
23222 tree first_attribute;
23224 /* Peek at the next token. */
23225 token = cp_lexer_peek_token (parser->lexer);
23227 /* Check for a bitfield declaration. */
23228 if (token->type == CPP_COLON
23229 || (token->type == CPP_NAME
23230 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
23231 == CPP_COLON))
23233 tree identifier;
23234 tree width;
23236 /* Get the name of the bitfield. Note that we cannot just
23237 check TOKEN here because it may have been invalidated by
23238 the call to cp_lexer_peek_nth_token above. */
23239 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
23240 identifier = cp_parser_identifier (parser);
23241 else
23242 identifier = NULL_TREE;
23244 /* Consume the `:' token. */
23245 cp_lexer_consume_token (parser->lexer);
23246 /* Get the width of the bitfield. */
23247 width
23248 = cp_parser_constant_expression (parser);
23250 /* Look for attributes that apply to the bitfield. */
23251 attributes = cp_parser_attributes_opt (parser);
23252 /* Remember which attributes are prefix attributes and
23253 which are not. */
23254 first_attribute = attributes;
23255 /* Combine the attributes. */
23256 attributes = chainon (prefix_attributes, attributes);
23258 /* Create the bitfield declaration. */
23259 decl = grokbitfield (identifier
23260 ? make_id_declarator (NULL_TREE,
23261 identifier,
23262 sfk_none)
23263 : NULL,
23264 &decl_specifiers,
23265 width,
23266 attributes);
23268 else
23270 cp_declarator *declarator;
23271 tree initializer;
23272 tree asm_specification;
23273 int ctor_dtor_or_conv_p;
23275 /* Parse the declarator. */
23276 declarator
23277 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23278 &ctor_dtor_or_conv_p,
23279 /*parenthesized_p=*/NULL,
23280 /*member_p=*/true,
23281 friend_p);
23283 /* If something went wrong parsing the declarator, make sure
23284 that we at least consume some tokens. */
23285 if (declarator == cp_error_declarator)
23287 /* Skip to the end of the statement. */
23288 cp_parser_skip_to_end_of_statement (parser);
23289 /* If the next token is not a semicolon, that is
23290 probably because we just skipped over the body of
23291 a function. So, we consume a semicolon if
23292 present, but do not issue an error message if it
23293 is not present. */
23294 if (cp_lexer_next_token_is (parser->lexer,
23295 CPP_SEMICOLON))
23296 cp_lexer_consume_token (parser->lexer);
23297 goto out;
23300 if (declares_class_or_enum & 2)
23301 cp_parser_check_for_definition_in_return_type
23302 (declarator, decl_specifiers.type,
23303 decl_specifiers.locations[ds_type_spec]);
23305 /* Look for an asm-specification. */
23306 asm_specification = cp_parser_asm_specification_opt (parser);
23307 /* Look for attributes that apply to the declaration. */
23308 attributes = cp_parser_attributes_opt (parser);
23309 /* Remember which attributes are prefix attributes and
23310 which are not. */
23311 first_attribute = attributes;
23312 /* Combine the attributes. */
23313 attributes = chainon (prefix_attributes, attributes);
23315 /* If it's an `=', then we have a constant-initializer or a
23316 pure-specifier. It is not correct to parse the
23317 initializer before registering the member declaration
23318 since the member declaration should be in scope while
23319 its initializer is processed. However, the rest of the
23320 front end does not yet provide an interface that allows
23321 us to handle this correctly. */
23322 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23324 /* In [class.mem]:
23326 A pure-specifier shall be used only in the declaration of
23327 a virtual function.
23329 A member-declarator can contain a constant-initializer
23330 only if it declares a static member of integral or
23331 enumeration type.
23333 Therefore, if the DECLARATOR is for a function, we look
23334 for a pure-specifier; otherwise, we look for a
23335 constant-initializer. When we call `grokfield', it will
23336 perform more stringent semantics checks. */
23337 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23338 if (function_declarator_p (declarator)
23339 || (decl_specifiers.type
23340 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23341 && declarator->kind == cdk_id
23342 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23343 == FUNCTION_TYPE)))
23344 initializer = cp_parser_pure_specifier (parser);
23345 else if (decl_specifiers.storage_class != sc_static)
23346 initializer = cp_parser_save_nsdmi (parser);
23347 else if (cxx_dialect >= cxx11)
23349 bool nonconst;
23350 /* Don't require a constant rvalue in C++11, since we
23351 might want a reference constant. We'll enforce
23352 constancy later. */
23353 cp_lexer_consume_token (parser->lexer);
23354 /* Parse the initializer. */
23355 initializer = cp_parser_initializer_clause (parser,
23356 &nonconst);
23358 else
23359 /* Parse the initializer. */
23360 initializer = cp_parser_constant_initializer (parser);
23362 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23363 && !function_declarator_p (declarator))
23365 bool x;
23366 if (decl_specifiers.storage_class != sc_static)
23367 initializer = cp_parser_save_nsdmi (parser);
23368 else
23369 initializer = cp_parser_initializer (parser, &x, &x);
23371 /* Otherwise, there is no initializer. */
23372 else
23373 initializer = NULL_TREE;
23375 /* See if we are probably looking at a function
23376 definition. We are certainly not looking at a
23377 member-declarator. Calling `grokfield' has
23378 side-effects, so we must not do it unless we are sure
23379 that we are looking at a member-declarator. */
23380 if (cp_parser_token_starts_function_definition_p
23381 (cp_lexer_peek_token (parser->lexer)))
23383 /* The grammar does not allow a pure-specifier to be
23384 used when a member function is defined. (It is
23385 possible that this fact is an oversight in the
23386 standard, since a pure function may be defined
23387 outside of the class-specifier. */
23388 if (initializer && initializer_token_start)
23389 error_at (initializer_token_start->location,
23390 "pure-specifier on function-definition");
23391 decl = cp_parser_save_member_function_body (parser,
23392 &decl_specifiers,
23393 declarator,
23394 attributes);
23395 if (parser->fully_implicit_function_template_p)
23396 decl = finish_fully_implicit_template (parser, decl);
23397 /* If the member was not a friend, declare it here. */
23398 if (!friend_p)
23399 finish_member_declaration (decl);
23400 /* Peek at the next token. */
23401 token = cp_lexer_peek_token (parser->lexer);
23402 /* If the next token is a semicolon, consume it. */
23403 if (token->type == CPP_SEMICOLON)
23405 location_t semicolon_loc
23406 = cp_lexer_consume_token (parser->lexer)->location;
23407 gcc_rich_location richloc (semicolon_loc);
23408 richloc.add_fixit_remove ();
23409 warning_at_rich_loc (&richloc, OPT_Wextra_semi,
23410 "extra %<;%> after in-class "
23411 "function definition");
23413 goto out;
23415 else
23416 if (declarator->kind == cdk_function)
23417 declarator->id_loc = token->location;
23418 /* Create the declaration. */
23419 decl = grokfield (declarator, &decl_specifiers,
23420 initializer, /*init_const_expr_p=*/true,
23421 asm_specification, attributes);
23422 if (parser->fully_implicit_function_template_p)
23424 if (friend_p)
23425 finish_fully_implicit_template (parser, 0);
23426 else
23427 decl = finish_fully_implicit_template (parser, decl);
23431 cp_finalize_omp_declare_simd (parser, decl);
23432 cp_finalize_oacc_routine (parser, decl, false);
23434 /* Reset PREFIX_ATTRIBUTES. */
23435 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23436 attributes = TREE_CHAIN (attributes);
23437 if (attributes)
23438 TREE_CHAIN (attributes) = NULL_TREE;
23440 /* If there is any qualification still in effect, clear it
23441 now; we will be starting fresh with the next declarator. */
23442 parser->scope = NULL_TREE;
23443 parser->qualifying_scope = NULL_TREE;
23444 parser->object_scope = NULL_TREE;
23445 /* If it's a `,', then there are more declarators. */
23446 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23448 cp_lexer_consume_token (parser->lexer);
23449 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23451 cp_token *token = cp_lexer_previous_token (parser->lexer);
23452 error_at (token->location,
23453 "stray %<,%> at end of member declaration");
23456 /* If the next token isn't a `;', then we have a parse error. */
23457 else if (cp_lexer_next_token_is_not (parser->lexer,
23458 CPP_SEMICOLON))
23460 /* The next token might be a ways away from where the
23461 actual semicolon is missing. Find the previous token
23462 and use that for our error position. */
23463 cp_token *token = cp_lexer_previous_token (parser->lexer);
23464 error_at (token->location,
23465 "expected %<;%> at end of member declaration");
23467 /* Assume that the user meant to provide a semicolon. If
23468 we were to cp_parser_skip_to_end_of_statement, we might
23469 skip to a semicolon inside a member function definition
23470 and issue nonsensical error messages. */
23471 assume_semicolon = true;
23474 if (decl)
23476 /* Add DECL to the list of members. */
23477 if (!friend_p
23478 /* Explicitly include, eg, NSDMIs, for better error
23479 recovery (c++/58650). */
23480 || !DECL_DECLARES_FUNCTION_P (decl))
23481 finish_member_declaration (decl);
23483 if (TREE_CODE (decl) == FUNCTION_DECL)
23484 cp_parser_save_default_args (parser, decl);
23485 else if (TREE_CODE (decl) == FIELD_DECL
23486 && !DECL_C_BIT_FIELD (decl)
23487 && DECL_INITIAL (decl))
23488 /* Add DECL to the queue of NSDMI to be parsed later. */
23489 vec_safe_push (unparsed_nsdmis, decl);
23492 if (assume_semicolon)
23493 goto out;
23497 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23498 out:
23499 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23502 /* Parse a pure-specifier.
23504 pure-specifier:
23507 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23508 Otherwise, ERROR_MARK_NODE is returned. */
23510 static tree
23511 cp_parser_pure_specifier (cp_parser* parser)
23513 cp_token *token;
23515 /* Look for the `=' token. */
23516 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23517 return error_mark_node;
23518 /* Look for the `0' token. */
23519 token = cp_lexer_peek_token (parser->lexer);
23521 if (token->type == CPP_EOF
23522 || token->type == CPP_PRAGMA_EOL)
23523 return error_mark_node;
23525 cp_lexer_consume_token (parser->lexer);
23527 /* Accept = default or = delete in c++0x mode. */
23528 if (token->keyword == RID_DEFAULT
23529 || token->keyword == RID_DELETE)
23531 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23532 return token->u.value;
23535 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23536 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23538 cp_parser_error (parser,
23539 "invalid pure specifier (only %<= 0%> is allowed)");
23540 cp_parser_skip_to_end_of_statement (parser);
23541 return error_mark_node;
23543 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23545 error_at (token->location, "templates may not be %<virtual%>");
23546 return error_mark_node;
23549 return integer_zero_node;
23552 /* Parse a constant-initializer.
23554 constant-initializer:
23555 = constant-expression
23557 Returns a representation of the constant-expression. */
23559 static tree
23560 cp_parser_constant_initializer (cp_parser* parser)
23562 /* Look for the `=' token. */
23563 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23564 return error_mark_node;
23566 /* It is invalid to write:
23568 struct S { static const int i = { 7 }; };
23571 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23573 cp_parser_error (parser,
23574 "a brace-enclosed initializer is not allowed here");
23575 /* Consume the opening brace. */
23576 cp_lexer_consume_token (parser->lexer);
23577 /* Skip the initializer. */
23578 cp_parser_skip_to_closing_brace (parser);
23579 /* Look for the trailing `}'. */
23580 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23582 return error_mark_node;
23585 return cp_parser_constant_expression (parser);
23588 /* Derived classes [gram.class.derived] */
23590 /* Parse a base-clause.
23592 base-clause:
23593 : base-specifier-list
23595 base-specifier-list:
23596 base-specifier ... [opt]
23597 base-specifier-list , base-specifier ... [opt]
23599 Returns a TREE_LIST representing the base-classes, in the order in
23600 which they were declared. The representation of each node is as
23601 described by cp_parser_base_specifier.
23603 In the case that no bases are specified, this function will return
23604 NULL_TREE, not ERROR_MARK_NODE. */
23606 static tree
23607 cp_parser_base_clause (cp_parser* parser)
23609 tree bases = NULL_TREE;
23611 /* Look for the `:' that begins the list. */
23612 cp_parser_require (parser, CPP_COLON, RT_COLON);
23614 /* Scan the base-specifier-list. */
23615 while (true)
23617 cp_token *token;
23618 tree base;
23619 bool pack_expansion_p = false;
23621 /* Look for the base-specifier. */
23622 base = cp_parser_base_specifier (parser);
23623 /* Look for the (optional) ellipsis. */
23624 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23626 /* Consume the `...'. */
23627 cp_lexer_consume_token (parser->lexer);
23629 pack_expansion_p = true;
23632 /* Add BASE to the front of the list. */
23633 if (base && base != error_mark_node)
23635 if (pack_expansion_p)
23636 /* Make this a pack expansion type. */
23637 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23639 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23641 TREE_CHAIN (base) = bases;
23642 bases = base;
23645 /* Peek at the next token. */
23646 token = cp_lexer_peek_token (parser->lexer);
23647 /* If it's not a comma, then the list is complete. */
23648 if (token->type != CPP_COMMA)
23649 break;
23650 /* Consume the `,'. */
23651 cp_lexer_consume_token (parser->lexer);
23654 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23655 base class had a qualified name. However, the next name that
23656 appears is certainly not qualified. */
23657 parser->scope = NULL_TREE;
23658 parser->qualifying_scope = NULL_TREE;
23659 parser->object_scope = NULL_TREE;
23661 return nreverse (bases);
23664 /* Parse a base-specifier.
23666 base-specifier:
23667 :: [opt] nested-name-specifier [opt] class-name
23668 virtual access-specifier [opt] :: [opt] nested-name-specifier
23669 [opt] class-name
23670 access-specifier virtual [opt] :: [opt] nested-name-specifier
23671 [opt] class-name
23673 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23674 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23675 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23676 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23678 static tree
23679 cp_parser_base_specifier (cp_parser* parser)
23681 cp_token *token;
23682 bool done = false;
23683 bool virtual_p = false;
23684 bool duplicate_virtual_error_issued_p = false;
23685 bool duplicate_access_error_issued_p = false;
23686 bool class_scope_p, template_p;
23687 tree access = access_default_node;
23688 tree type;
23690 /* Process the optional `virtual' and `access-specifier'. */
23691 while (!done)
23693 /* Peek at the next token. */
23694 token = cp_lexer_peek_token (parser->lexer);
23695 /* Process `virtual'. */
23696 switch (token->keyword)
23698 case RID_VIRTUAL:
23699 /* If `virtual' appears more than once, issue an error. */
23700 if (virtual_p && !duplicate_virtual_error_issued_p)
23702 cp_parser_error (parser,
23703 "%<virtual%> specified more than once in base-specified");
23704 duplicate_virtual_error_issued_p = true;
23707 virtual_p = true;
23709 /* Consume the `virtual' token. */
23710 cp_lexer_consume_token (parser->lexer);
23712 break;
23714 case RID_PUBLIC:
23715 case RID_PROTECTED:
23716 case RID_PRIVATE:
23717 /* If more than one access specifier appears, issue an
23718 error. */
23719 if (access != access_default_node
23720 && !duplicate_access_error_issued_p)
23722 cp_parser_error (parser,
23723 "more than one access specifier in base-specified");
23724 duplicate_access_error_issued_p = true;
23727 access = ridpointers[(int) token->keyword];
23729 /* Consume the access-specifier. */
23730 cp_lexer_consume_token (parser->lexer);
23732 break;
23734 default:
23735 done = true;
23736 break;
23739 /* It is not uncommon to see programs mechanically, erroneously, use
23740 the 'typename' keyword to denote (dependent) qualified types
23741 as base classes. */
23742 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23744 token = cp_lexer_peek_token (parser->lexer);
23745 if (!processing_template_decl)
23746 error_at (token->location,
23747 "keyword %<typename%> not allowed outside of templates");
23748 else
23749 error_at (token->location,
23750 "keyword %<typename%> not allowed in this context "
23751 "(the base class is implicitly a type)");
23752 cp_lexer_consume_token (parser->lexer);
23755 /* Look for the optional `::' operator. */
23756 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23757 /* Look for the nested-name-specifier. The simplest way to
23758 implement:
23760 [temp.res]
23762 The keyword `typename' is not permitted in a base-specifier or
23763 mem-initializer; in these contexts a qualified name that
23764 depends on a template-parameter is implicitly assumed to be a
23765 type name.
23767 is to pretend that we have seen the `typename' keyword at this
23768 point. */
23769 cp_parser_nested_name_specifier_opt (parser,
23770 /*typename_keyword_p=*/true,
23771 /*check_dependency_p=*/true,
23772 /*type_p=*/true,
23773 /*is_declaration=*/true);
23774 /* If the base class is given by a qualified name, assume that names
23775 we see are type names or templates, as appropriate. */
23776 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23777 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23779 if (!parser->scope
23780 && cp_lexer_next_token_is_decltype (parser->lexer))
23781 /* DR 950 allows decltype as a base-specifier. */
23782 type = cp_parser_decltype (parser);
23783 else
23785 /* Otherwise, look for the class-name. */
23786 type = cp_parser_class_name (parser,
23787 class_scope_p,
23788 template_p,
23789 typename_type,
23790 /*check_dependency_p=*/true,
23791 /*class_head_p=*/false,
23792 /*is_declaration=*/true);
23793 type = TREE_TYPE (type);
23796 if (type == error_mark_node)
23797 return error_mark_node;
23799 return finish_base_specifier (type, access, virtual_p);
23802 /* Exception handling [gram.exception] */
23804 /* Parse an (optional) noexcept-specification.
23806 noexcept-specification:
23807 noexcept ( constant-expression ) [opt]
23809 If no noexcept-specification is present, returns NULL_TREE.
23810 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23811 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23812 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23813 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23814 in which case a boolean condition is returned instead. */
23816 static tree
23817 cp_parser_noexcept_specification_opt (cp_parser* parser,
23818 bool require_constexpr,
23819 bool* consumed_expr,
23820 bool return_cond)
23822 cp_token *token;
23823 const char *saved_message;
23825 /* Peek at the next token. */
23826 token = cp_lexer_peek_token (parser->lexer);
23828 /* Is it a noexcept-specification? */
23829 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23831 tree expr;
23832 cp_lexer_consume_token (parser->lexer);
23834 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23836 cp_lexer_consume_token (parser->lexer);
23838 if (require_constexpr)
23840 /* Types may not be defined in an exception-specification. */
23841 saved_message = parser->type_definition_forbidden_message;
23842 parser->type_definition_forbidden_message
23843 = G_("types may not be defined in an exception-specification");
23845 expr = cp_parser_constant_expression (parser);
23847 /* Restore the saved message. */
23848 parser->type_definition_forbidden_message = saved_message;
23850 else
23852 expr = cp_parser_expression (parser);
23853 *consumed_expr = true;
23856 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23858 else
23860 expr = boolean_true_node;
23861 if (!require_constexpr)
23862 *consumed_expr = false;
23865 /* We cannot build a noexcept-spec right away because this will check
23866 that expr is a constexpr. */
23867 if (!return_cond)
23868 return build_noexcept_spec (expr, tf_warning_or_error);
23869 else
23870 return expr;
23872 else
23873 return NULL_TREE;
23876 /* Parse an (optional) exception-specification.
23878 exception-specification:
23879 throw ( type-id-list [opt] )
23881 Returns a TREE_LIST representing the exception-specification. The
23882 TREE_VALUE of each node is a type. */
23884 static tree
23885 cp_parser_exception_specification_opt (cp_parser* parser)
23887 cp_token *token;
23888 tree type_id_list;
23889 const char *saved_message;
23891 /* Peek at the next token. */
23892 token = cp_lexer_peek_token (parser->lexer);
23894 /* Is it a noexcept-specification? */
23895 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
23896 false);
23897 if (type_id_list != NULL_TREE)
23898 return type_id_list;
23900 /* If it's not `throw', then there's no exception-specification. */
23901 if (!cp_parser_is_keyword (token, RID_THROW))
23902 return NULL_TREE;
23904 location_t loc = token->location;
23906 /* Consume the `throw'. */
23907 cp_lexer_consume_token (parser->lexer);
23909 /* Look for the `('. */
23910 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23912 /* Peek at the next token. */
23913 token = cp_lexer_peek_token (parser->lexer);
23914 /* If it's not a `)', then there is a type-id-list. */
23915 if (token->type != CPP_CLOSE_PAREN)
23917 /* Types may not be defined in an exception-specification. */
23918 saved_message = parser->type_definition_forbidden_message;
23919 parser->type_definition_forbidden_message
23920 = G_("types may not be defined in an exception-specification");
23921 /* Parse the type-id-list. */
23922 type_id_list = cp_parser_type_id_list (parser);
23923 /* Restore the saved message. */
23924 parser->type_definition_forbidden_message = saved_message;
23926 if (cxx_dialect >= cxx1z)
23928 error_at (loc, "ISO C++1z does not allow dynamic exception "
23929 "specifications");
23930 type_id_list = NULL_TREE;
23932 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
23933 warning_at (loc, OPT_Wdeprecated,
23934 "dynamic exception specifications are deprecated in "
23935 "C++11");
23937 /* In C++17, throw() is equivalent to noexcept (true). throw()
23938 is deprecated in C++11 and above as well, but is still widely used,
23939 so don't warn about it yet. */
23940 else if (cxx_dialect >= cxx1z)
23941 type_id_list = noexcept_true_spec;
23942 else
23943 type_id_list = empty_except_spec;
23945 /* Look for the `)'. */
23946 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23948 return type_id_list;
23951 /* Parse an (optional) type-id-list.
23953 type-id-list:
23954 type-id ... [opt]
23955 type-id-list , type-id ... [opt]
23957 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23958 in the order that the types were presented. */
23960 static tree
23961 cp_parser_type_id_list (cp_parser* parser)
23963 tree types = NULL_TREE;
23965 while (true)
23967 cp_token *token;
23968 tree type;
23970 token = cp_lexer_peek_token (parser->lexer);
23972 /* Get the next type-id. */
23973 type = cp_parser_type_id (parser);
23974 /* Check for invalid 'auto'. */
23975 if (flag_concepts && type_uses_auto (type))
23977 error_at (token->location,
23978 "invalid use of %<auto%> in exception-specification");
23979 type = error_mark_node;
23981 /* Parse the optional ellipsis. */
23982 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23984 /* Consume the `...'. */
23985 cp_lexer_consume_token (parser->lexer);
23987 /* Turn the type into a pack expansion expression. */
23988 type = make_pack_expansion (type);
23990 /* Add it to the list. */
23991 types = add_exception_specifier (types, type, /*complain=*/1);
23992 /* Peek at the next token. */
23993 token = cp_lexer_peek_token (parser->lexer);
23994 /* If it is not a `,', we are done. */
23995 if (token->type != CPP_COMMA)
23996 break;
23997 /* Consume the `,'. */
23998 cp_lexer_consume_token (parser->lexer);
24001 return nreverse (types);
24004 /* Parse a try-block.
24006 try-block:
24007 try compound-statement handler-seq */
24009 static tree
24010 cp_parser_try_block (cp_parser* parser)
24012 tree try_block;
24014 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24015 if (parser->in_function_body
24016 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24017 error ("%<try%> in %<constexpr%> function");
24019 try_block = begin_try_block ();
24020 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24021 finish_try_block (try_block);
24022 cp_parser_handler_seq (parser);
24023 finish_handler_sequence (try_block);
24025 return try_block;
24028 /* Parse a function-try-block.
24030 function-try-block:
24031 try ctor-initializer [opt] function-body handler-seq */
24033 static bool
24034 cp_parser_function_try_block (cp_parser* parser)
24036 tree compound_stmt;
24037 tree try_block;
24038 bool ctor_initializer_p;
24040 /* Look for the `try' keyword. */
24041 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24042 return false;
24043 /* Let the rest of the front end know where we are. */
24044 try_block = begin_function_try_block (&compound_stmt);
24045 /* Parse the function-body. */
24046 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24047 (parser, /*in_function_try_block=*/true);
24048 /* We're done with the `try' part. */
24049 finish_function_try_block (try_block);
24050 /* Parse the handlers. */
24051 cp_parser_handler_seq (parser);
24052 /* We're done with the handlers. */
24053 finish_function_handler_sequence (try_block, compound_stmt);
24055 return ctor_initializer_p;
24058 /* Parse a handler-seq.
24060 handler-seq:
24061 handler handler-seq [opt] */
24063 static void
24064 cp_parser_handler_seq (cp_parser* parser)
24066 while (true)
24068 cp_token *token;
24070 /* Parse the handler. */
24071 cp_parser_handler (parser);
24072 /* Peek at the next token. */
24073 token = cp_lexer_peek_token (parser->lexer);
24074 /* If it's not `catch' then there are no more handlers. */
24075 if (!cp_parser_is_keyword (token, RID_CATCH))
24076 break;
24080 /* Parse a handler.
24082 handler:
24083 catch ( exception-declaration ) compound-statement */
24085 static void
24086 cp_parser_handler (cp_parser* parser)
24088 tree handler;
24089 tree declaration;
24091 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24092 handler = begin_handler ();
24093 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24094 declaration = cp_parser_exception_declaration (parser);
24095 finish_handler_parms (declaration, handler);
24096 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24097 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24098 finish_handler (handler);
24101 /* Parse an exception-declaration.
24103 exception-declaration:
24104 type-specifier-seq declarator
24105 type-specifier-seq abstract-declarator
24106 type-specifier-seq
24109 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24110 ellipsis variant is used. */
24112 static tree
24113 cp_parser_exception_declaration (cp_parser* parser)
24115 cp_decl_specifier_seq type_specifiers;
24116 cp_declarator *declarator;
24117 const char *saved_message;
24119 /* If it's an ellipsis, it's easy to handle. */
24120 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24122 /* Consume the `...' token. */
24123 cp_lexer_consume_token (parser->lexer);
24124 return NULL_TREE;
24127 /* Types may not be defined in exception-declarations. */
24128 saved_message = parser->type_definition_forbidden_message;
24129 parser->type_definition_forbidden_message
24130 = G_("types may not be defined in exception-declarations");
24132 /* Parse the type-specifier-seq. */
24133 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24134 /*is_trailing_return=*/false,
24135 &type_specifiers);
24136 /* If it's a `)', then there is no declarator. */
24137 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24138 declarator = NULL;
24139 else
24140 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24141 /*ctor_dtor_or_conv_p=*/NULL,
24142 /*parenthesized_p=*/NULL,
24143 /*member_p=*/false,
24144 /*friend_p=*/false);
24146 /* Restore the saved message. */
24147 parser->type_definition_forbidden_message = saved_message;
24149 if (!type_specifiers.any_specifiers_p)
24150 return error_mark_node;
24152 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24155 /* Parse a throw-expression.
24157 throw-expression:
24158 throw assignment-expression [opt]
24160 Returns a THROW_EXPR representing the throw-expression. */
24162 static tree
24163 cp_parser_throw_expression (cp_parser* parser)
24165 tree expression;
24166 cp_token* token;
24168 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24169 token = cp_lexer_peek_token (parser->lexer);
24170 /* Figure out whether or not there is an assignment-expression
24171 following the "throw" keyword. */
24172 if (token->type == CPP_COMMA
24173 || token->type == CPP_SEMICOLON
24174 || token->type == CPP_CLOSE_PAREN
24175 || token->type == CPP_CLOSE_SQUARE
24176 || token->type == CPP_CLOSE_BRACE
24177 || token->type == CPP_COLON)
24178 expression = NULL_TREE;
24179 else
24180 expression = cp_parser_assignment_expression (parser);
24182 return build_throw (expression);
24185 /* GNU Extensions */
24187 /* Parse an (optional) asm-specification.
24189 asm-specification:
24190 asm ( string-literal )
24192 If the asm-specification is present, returns a STRING_CST
24193 corresponding to the string-literal. Otherwise, returns
24194 NULL_TREE. */
24196 static tree
24197 cp_parser_asm_specification_opt (cp_parser* parser)
24199 cp_token *token;
24200 tree asm_specification;
24202 /* Peek at the next token. */
24203 token = cp_lexer_peek_token (parser->lexer);
24204 /* If the next token isn't the `asm' keyword, then there's no
24205 asm-specification. */
24206 if (!cp_parser_is_keyword (token, RID_ASM))
24207 return NULL_TREE;
24209 /* Consume the `asm' token. */
24210 cp_lexer_consume_token (parser->lexer);
24211 /* Look for the `('. */
24212 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24214 /* Look for the string-literal. */
24215 asm_specification = cp_parser_string_literal (parser, false, false);
24217 /* Look for the `)'. */
24218 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24220 return asm_specification;
24223 /* Parse an asm-operand-list.
24225 asm-operand-list:
24226 asm-operand
24227 asm-operand-list , asm-operand
24229 asm-operand:
24230 string-literal ( expression )
24231 [ string-literal ] string-literal ( expression )
24233 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24234 each node is the expression. The TREE_PURPOSE is itself a
24235 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24236 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24237 is a STRING_CST for the string literal before the parenthesis. Returns
24238 ERROR_MARK_NODE if any of the operands are invalid. */
24240 static tree
24241 cp_parser_asm_operand_list (cp_parser* parser)
24243 tree asm_operands = NULL_TREE;
24244 bool invalid_operands = false;
24246 while (true)
24248 tree string_literal;
24249 tree expression;
24250 tree name;
24252 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24254 /* Consume the `[' token. */
24255 cp_lexer_consume_token (parser->lexer);
24256 /* Read the operand name. */
24257 name = cp_parser_identifier (parser);
24258 if (name != error_mark_node)
24259 name = build_string (IDENTIFIER_LENGTH (name),
24260 IDENTIFIER_POINTER (name));
24261 /* Look for the closing `]'. */
24262 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24264 else
24265 name = NULL_TREE;
24266 /* Look for the string-literal. */
24267 string_literal = cp_parser_string_literal (parser, false, false);
24269 /* Look for the `('. */
24270 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24271 /* Parse the expression. */
24272 expression = cp_parser_expression (parser);
24273 /* Look for the `)'. */
24274 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24276 if (name == error_mark_node
24277 || string_literal == error_mark_node
24278 || expression == error_mark_node)
24279 invalid_operands = true;
24281 /* Add this operand to the list. */
24282 asm_operands = tree_cons (build_tree_list (name, string_literal),
24283 expression,
24284 asm_operands);
24285 /* If the next token is not a `,', there are no more
24286 operands. */
24287 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24288 break;
24289 /* Consume the `,'. */
24290 cp_lexer_consume_token (parser->lexer);
24293 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24296 /* Parse an asm-clobber-list.
24298 asm-clobber-list:
24299 string-literal
24300 asm-clobber-list , string-literal
24302 Returns a TREE_LIST, indicating the clobbers in the order that they
24303 appeared. The TREE_VALUE of each node is a STRING_CST. */
24305 static tree
24306 cp_parser_asm_clobber_list (cp_parser* parser)
24308 tree clobbers = NULL_TREE;
24310 while (true)
24312 tree string_literal;
24314 /* Look for the string literal. */
24315 string_literal = cp_parser_string_literal (parser, false, false);
24316 /* Add it to the list. */
24317 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24318 /* If the next token is not a `,', then the list is
24319 complete. */
24320 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24321 break;
24322 /* Consume the `,' token. */
24323 cp_lexer_consume_token (parser->lexer);
24326 return clobbers;
24329 /* Parse an asm-label-list.
24331 asm-label-list:
24332 identifier
24333 asm-label-list , identifier
24335 Returns a TREE_LIST, indicating the labels in the order that they
24336 appeared. The TREE_VALUE of each node is a label. */
24338 static tree
24339 cp_parser_asm_label_list (cp_parser* parser)
24341 tree labels = NULL_TREE;
24343 while (true)
24345 tree identifier, label, name;
24347 /* Look for the identifier. */
24348 identifier = cp_parser_identifier (parser);
24349 if (!error_operand_p (identifier))
24351 label = lookup_label (identifier);
24352 if (TREE_CODE (label) == LABEL_DECL)
24354 TREE_USED (label) = 1;
24355 check_goto (label);
24356 name = build_string (IDENTIFIER_LENGTH (identifier),
24357 IDENTIFIER_POINTER (identifier));
24358 labels = tree_cons (name, label, labels);
24361 /* If the next token is not a `,', then the list is
24362 complete. */
24363 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24364 break;
24365 /* Consume the `,' token. */
24366 cp_lexer_consume_token (parser->lexer);
24369 return nreverse (labels);
24372 /* Return TRUE iff the next tokens in the stream are possibly the
24373 beginning of a GNU extension attribute. */
24375 static bool
24376 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24378 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24381 /* Return TRUE iff the next tokens in the stream are possibly the
24382 beginning of a standard C++-11 attribute specifier. */
24384 static bool
24385 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24387 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24390 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24391 beginning of a standard C++-11 attribute specifier. */
24393 static bool
24394 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24396 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24398 return (cxx_dialect >= cxx11
24399 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24400 || (token->type == CPP_OPEN_SQUARE
24401 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24402 && token->type == CPP_OPEN_SQUARE)));
24405 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24406 beginning of a GNU extension attribute. */
24408 static bool
24409 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24411 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24413 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24416 /* Return true iff the next tokens can be the beginning of either a
24417 GNU attribute list, or a standard C++11 attribute sequence. */
24419 static bool
24420 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24422 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24423 || cp_next_tokens_can_be_std_attribute_p (parser));
24426 /* Return true iff the next Nth tokens can be the beginning of either
24427 a GNU attribute list, or a standard C++11 attribute sequence. */
24429 static bool
24430 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24432 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24433 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24436 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24437 of GNU attributes, or return NULL. */
24439 static tree
24440 cp_parser_attributes_opt (cp_parser *parser)
24442 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24443 return cp_parser_gnu_attributes_opt (parser);
24444 return cp_parser_std_attribute_spec_seq (parser);
24447 #define CILK_SIMD_FN_CLAUSE_MASK \
24448 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24449 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24450 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24451 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24452 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24454 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24455 vector [(<clauses>)] */
24457 static void
24458 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24460 bool first_p = parser->cilk_simd_fn_info == NULL;
24461 cp_token *token = v_token;
24462 if (first_p)
24464 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24465 parser->cilk_simd_fn_info->error_seen = false;
24466 parser->cilk_simd_fn_info->fndecl_seen = false;
24467 parser->cilk_simd_fn_info->tokens = vNULL;
24468 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24470 int paren_scope = 0;
24471 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24473 cp_lexer_consume_token (parser->lexer);
24474 v_token = cp_lexer_peek_token (parser->lexer);
24475 paren_scope++;
24477 while (paren_scope > 0)
24479 token = cp_lexer_peek_token (parser->lexer);
24480 if (token->type == CPP_OPEN_PAREN)
24481 paren_scope++;
24482 else if (token->type == CPP_CLOSE_PAREN)
24483 paren_scope--;
24484 /* Do not push the last ')' */
24485 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24486 cp_lexer_consume_token (parser->lexer);
24489 token->type = CPP_PRAGMA_EOL;
24490 parser->lexer->next_token = token;
24491 cp_lexer_consume_token (parser->lexer);
24493 struct cp_token_cache *cp
24494 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24495 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24498 /* Parse an (optional) series of attributes.
24500 attributes:
24501 attributes attribute
24503 attribute:
24504 __attribute__ (( attribute-list [opt] ))
24506 The return value is as for cp_parser_gnu_attribute_list. */
24508 static tree
24509 cp_parser_gnu_attributes_opt (cp_parser* parser)
24511 tree attributes = NULL_TREE;
24513 while (true)
24515 cp_token *token;
24516 tree attribute_list;
24517 bool ok = true;
24519 /* Peek at the next token. */
24520 token = cp_lexer_peek_token (parser->lexer);
24521 /* If it's not `__attribute__', then we're done. */
24522 if (token->keyword != RID_ATTRIBUTE)
24523 break;
24525 /* Consume the `__attribute__' keyword. */
24526 cp_lexer_consume_token (parser->lexer);
24527 /* Look for the two `(' tokens. */
24528 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24529 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24531 /* Peek at the next token. */
24532 token = cp_lexer_peek_token (parser->lexer);
24533 if (token->type != CPP_CLOSE_PAREN)
24534 /* Parse the attribute-list. */
24535 attribute_list = cp_parser_gnu_attribute_list (parser);
24536 else
24537 /* If the next token is a `)', then there is no attribute
24538 list. */
24539 attribute_list = NULL;
24541 /* Look for the two `)' tokens. */
24542 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24543 ok = false;
24544 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24545 ok = false;
24546 if (!ok)
24547 cp_parser_skip_to_end_of_statement (parser);
24549 /* Add these new attributes to the list. */
24550 attributes = chainon (attributes, attribute_list);
24553 return attributes;
24556 /* Parse a GNU attribute-list.
24558 attribute-list:
24559 attribute
24560 attribute-list , attribute
24562 attribute:
24563 identifier
24564 identifier ( identifier )
24565 identifier ( identifier , expression-list )
24566 identifier ( expression-list )
24568 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24569 to an attribute. The TREE_PURPOSE of each node is the identifier
24570 indicating which attribute is in use. The TREE_VALUE represents
24571 the arguments, if any. */
24573 static tree
24574 cp_parser_gnu_attribute_list (cp_parser* parser)
24576 tree attribute_list = NULL_TREE;
24577 bool save_translate_strings_p = parser->translate_strings_p;
24579 parser->translate_strings_p = false;
24580 while (true)
24582 cp_token *token;
24583 tree identifier;
24584 tree attribute;
24586 /* Look for the identifier. We also allow keywords here; for
24587 example `__attribute__ ((const))' is legal. */
24588 token = cp_lexer_peek_token (parser->lexer);
24589 if (token->type == CPP_NAME
24590 || token->type == CPP_KEYWORD)
24592 tree arguments = NULL_TREE;
24594 /* Consume the token, but save it since we need it for the
24595 SIMD enabled function parsing. */
24596 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24598 /* Save away the identifier that indicates which attribute
24599 this is. */
24600 identifier = (token->type == CPP_KEYWORD)
24601 /* For keywords, use the canonical spelling, not the
24602 parsed identifier. */
24603 ? ridpointers[(int) token->keyword]
24604 : id_token->u.value;
24606 attribute = build_tree_list (identifier, NULL_TREE);
24608 /* Peek at the next token. */
24609 token = cp_lexer_peek_token (parser->lexer);
24610 /* If it's an `(', then parse the attribute arguments. */
24611 if (token->type == CPP_OPEN_PAREN)
24613 vec<tree, va_gc> *vec;
24614 int attr_flag = (attribute_takes_identifier_p (identifier)
24615 ? id_attr : normal_attr);
24616 if (is_cilkplus_vector_p (identifier))
24618 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24619 continue;
24621 else
24622 vec = cp_parser_parenthesized_expression_list
24623 (parser, attr_flag, /*cast_p=*/false,
24624 /*allow_expansion_p=*/false,
24625 /*non_constant_p=*/NULL);
24626 if (vec == NULL)
24627 arguments = error_mark_node;
24628 else
24630 arguments = build_tree_list_vec (vec);
24631 release_tree_vector (vec);
24633 /* Save the arguments away. */
24634 TREE_VALUE (attribute) = arguments;
24636 else if (is_cilkplus_vector_p (identifier))
24638 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24639 continue;
24642 if (arguments != error_mark_node)
24644 /* Add this attribute to the list. */
24645 TREE_CHAIN (attribute) = attribute_list;
24646 attribute_list = attribute;
24649 token = cp_lexer_peek_token (parser->lexer);
24651 /* Now, look for more attributes. If the next token isn't a
24652 `,', we're done. */
24653 if (token->type != CPP_COMMA)
24654 break;
24656 /* Consume the comma and keep going. */
24657 cp_lexer_consume_token (parser->lexer);
24659 parser->translate_strings_p = save_translate_strings_p;
24661 /* We built up the list in reverse order. */
24662 return nreverse (attribute_list);
24665 /* Parse a standard C++11 attribute.
24667 The returned representation is a TREE_LIST which TREE_PURPOSE is
24668 the scoped name of the attribute, and the TREE_VALUE is its
24669 arguments list.
24671 Note that the scoped name of the attribute is itself a TREE_LIST
24672 which TREE_PURPOSE is the namespace of the attribute, and
24673 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24674 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24675 and which TREE_PURPOSE is directly the attribute name.
24677 Clients of the attribute code should use get_attribute_namespace
24678 and get_attribute_name to get the actual namespace and name of
24679 attributes, regardless of their being GNU or C++11 attributes.
24681 attribute:
24682 attribute-token attribute-argument-clause [opt]
24684 attribute-token:
24685 identifier
24686 attribute-scoped-token
24688 attribute-scoped-token:
24689 attribute-namespace :: identifier
24691 attribute-namespace:
24692 identifier
24694 attribute-argument-clause:
24695 ( balanced-token-seq )
24697 balanced-token-seq:
24698 balanced-token [opt]
24699 balanced-token-seq balanced-token
24701 balanced-token:
24702 ( balanced-token-seq )
24703 [ balanced-token-seq ]
24704 { balanced-token-seq }. */
24706 static tree
24707 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24709 tree attribute, attr_id = NULL_TREE, arguments;
24710 cp_token *token;
24712 /* First, parse name of the attribute, a.k.a attribute-token. */
24714 token = cp_lexer_peek_token (parser->lexer);
24715 if (token->type == CPP_NAME)
24716 attr_id = token->u.value;
24717 else if (token->type == CPP_KEYWORD)
24718 attr_id = ridpointers[(int) token->keyword];
24719 else if (token->flags & NAMED_OP)
24720 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24722 if (attr_id == NULL_TREE)
24723 return NULL_TREE;
24725 cp_lexer_consume_token (parser->lexer);
24727 token = cp_lexer_peek_token (parser->lexer);
24728 if (token->type == CPP_SCOPE)
24730 /* We are seeing a scoped attribute token. */
24732 cp_lexer_consume_token (parser->lexer);
24733 if (attr_ns)
24734 error_at (token->location, "attribute using prefix used together "
24735 "with scoped attribute token");
24736 attr_ns = attr_id;
24738 token = cp_lexer_consume_token (parser->lexer);
24739 if (token->type == CPP_NAME)
24740 attr_id = token->u.value;
24741 else if (token->type == CPP_KEYWORD)
24742 attr_id = ridpointers[(int) token->keyword];
24743 else if (token->flags & NAMED_OP)
24744 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24745 else
24747 error_at (token->location,
24748 "expected an identifier for the attribute name");
24749 return error_mark_node;
24751 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24752 NULL_TREE);
24753 token = cp_lexer_peek_token (parser->lexer);
24755 else if (attr_ns)
24756 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24757 NULL_TREE);
24758 else
24760 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24761 NULL_TREE);
24762 /* C++11 noreturn attribute is equivalent to GNU's. */
24763 if (is_attribute_p ("noreturn", attr_id))
24764 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24765 /* C++14 deprecated attribute is equivalent to GNU's. */
24766 else if (is_attribute_p ("deprecated", attr_id))
24767 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24768 /* C++17 fallthrough attribute is equivalent to GNU's. */
24769 else if (is_attribute_p ("fallthrough", attr_id))
24770 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24771 /* Transactional Memory TS optimize_for_synchronized attribute is
24772 equivalent to GNU transaction_callable. */
24773 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24774 TREE_PURPOSE (attribute)
24775 = get_identifier ("transaction_callable");
24776 /* Transactional Memory attributes are GNU attributes. */
24777 else if (tm_attr_to_mask (attr_id))
24778 TREE_PURPOSE (attribute) = attr_id;
24781 /* Now parse the optional argument clause of the attribute. */
24783 if (token->type != CPP_OPEN_PAREN)
24784 return attribute;
24787 vec<tree, va_gc> *vec;
24788 int attr_flag = normal_attr;
24790 if (attr_ns == get_identifier ("gnu")
24791 && attribute_takes_identifier_p (attr_id))
24792 /* A GNU attribute that takes an identifier in parameter. */
24793 attr_flag = id_attr;
24795 vec = cp_parser_parenthesized_expression_list
24796 (parser, attr_flag, /*cast_p=*/false,
24797 /*allow_expansion_p=*/true,
24798 /*non_constant_p=*/NULL);
24799 if (vec == NULL)
24800 arguments = error_mark_node;
24801 else
24803 arguments = build_tree_list_vec (vec);
24804 release_tree_vector (vec);
24807 if (arguments == error_mark_node)
24808 attribute = error_mark_node;
24809 else
24810 TREE_VALUE (attribute) = arguments;
24813 return attribute;
24816 /* Check that the attribute ATTRIBUTE appears at most once in the
24817 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24818 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24819 isn't implemented yet in GCC. */
24821 static void
24822 cp_parser_check_std_attribute (tree attributes, tree attribute)
24824 if (attributes)
24826 tree name = get_attribute_name (attribute);
24827 if (is_attribute_p ("noreturn", name)
24828 && lookup_attribute ("noreturn", attributes))
24829 error ("attribute %<noreturn%> can appear at most once "
24830 "in an attribute-list");
24831 else if (is_attribute_p ("deprecated", name)
24832 && lookup_attribute ("deprecated", attributes))
24833 error ("attribute %<deprecated%> can appear at most once "
24834 "in an attribute-list");
24838 /* Parse a list of standard C++-11 attributes.
24840 attribute-list:
24841 attribute [opt]
24842 attribute-list , attribute[opt]
24843 attribute ...
24844 attribute-list , attribute ...
24847 static tree
24848 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
24850 tree attributes = NULL_TREE, attribute = NULL_TREE;
24851 cp_token *token = NULL;
24853 while (true)
24855 attribute = cp_parser_std_attribute (parser, attr_ns);
24856 if (attribute == error_mark_node)
24857 break;
24858 if (attribute != NULL_TREE)
24860 cp_parser_check_std_attribute (attributes, attribute);
24861 TREE_CHAIN (attribute) = attributes;
24862 attributes = attribute;
24864 token = cp_lexer_peek_token (parser->lexer);
24865 if (token->type == CPP_ELLIPSIS)
24867 cp_lexer_consume_token (parser->lexer);
24868 if (attribute == NULL_TREE)
24869 error_at (token->location,
24870 "expected attribute before %<...%>");
24871 else
24873 tree pack = make_pack_expansion (TREE_VALUE (attribute));
24874 if (pack == error_mark_node)
24875 return error_mark_node;
24876 TREE_VALUE (attribute) = pack;
24878 token = cp_lexer_peek_token (parser->lexer);
24880 if (token->type != CPP_COMMA)
24881 break;
24882 cp_lexer_consume_token (parser->lexer);
24884 attributes = nreverse (attributes);
24885 return attributes;
24888 /* Parse a standard C++-11 attribute specifier.
24890 attribute-specifier:
24891 [ [ attribute-using-prefix [opt] attribute-list ] ]
24892 alignment-specifier
24894 attribute-using-prefix:
24895 using attribute-namespace :
24897 alignment-specifier:
24898 alignas ( type-id ... [opt] )
24899 alignas ( alignment-expression ... [opt] ). */
24901 static tree
24902 cp_parser_std_attribute_spec (cp_parser *parser)
24904 tree attributes = NULL_TREE;
24905 cp_token *token = cp_lexer_peek_token (parser->lexer);
24907 if (token->type == CPP_OPEN_SQUARE
24908 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24910 tree attr_ns = NULL_TREE;
24912 cp_lexer_consume_token (parser->lexer);
24913 cp_lexer_consume_token (parser->lexer);
24915 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24917 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24918 if (token->type == CPP_NAME)
24919 attr_ns = token->u.value;
24920 else if (token->type == CPP_KEYWORD)
24921 attr_ns = ridpointers[(int) token->keyword];
24922 else if (token->flags & NAMED_OP)
24923 attr_ns = get_identifier (cpp_type2name (token->type,
24924 token->flags));
24925 if (attr_ns
24926 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
24928 if (cxx_dialect < cxx1z
24929 && !in_system_header_at (input_location))
24930 pedwarn (input_location, 0,
24931 "attribute using prefix only available "
24932 "with -std=c++1z or -std=gnu++1z");
24934 cp_lexer_consume_token (parser->lexer);
24935 cp_lexer_consume_token (parser->lexer);
24936 cp_lexer_consume_token (parser->lexer);
24938 else
24939 attr_ns = NULL_TREE;
24942 attributes = cp_parser_std_attribute_list (parser, attr_ns);
24944 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24945 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24946 cp_parser_skip_to_end_of_statement (parser);
24947 else
24948 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24949 when we are sure that we have actually parsed them. */
24950 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24952 else
24954 tree alignas_expr;
24956 /* Look for an alignment-specifier. */
24958 token = cp_lexer_peek_token (parser->lexer);
24960 if (token->type != CPP_KEYWORD
24961 || token->keyword != RID_ALIGNAS)
24962 return NULL_TREE;
24964 cp_lexer_consume_token (parser->lexer);
24965 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24967 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24969 cp_parser_error (parser, "expected %<(%>");
24970 return error_mark_node;
24973 cp_parser_parse_tentatively (parser);
24974 alignas_expr = cp_parser_type_id (parser);
24976 if (!cp_parser_parse_definitely (parser))
24978 alignas_expr = cp_parser_assignment_expression (parser);
24979 if (alignas_expr == error_mark_node)
24980 cp_parser_skip_to_end_of_statement (parser);
24981 if (alignas_expr == NULL_TREE
24982 || alignas_expr == error_mark_node)
24983 return alignas_expr;
24986 alignas_expr = cxx_alignas_expr (alignas_expr);
24987 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24989 /* Handle alignas (pack...). */
24990 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24992 cp_lexer_consume_token (parser->lexer);
24993 alignas_expr = make_pack_expansion (alignas_expr);
24996 /* Something went wrong, so don't build the attribute. */
24997 if (alignas_expr == error_mark_node)
24998 return error_mark_node;
25000 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
25002 cp_parser_error (parser, "expected %<)%>");
25003 return error_mark_node;
25006 /* Build the C++-11 representation of an 'aligned'
25007 attribute. */
25008 attributes =
25009 build_tree_list (build_tree_list (get_identifier ("gnu"),
25010 get_identifier ("aligned")),
25011 alignas_expr);
25014 return attributes;
25017 /* Parse a standard C++-11 attribute-specifier-seq.
25019 attribute-specifier-seq:
25020 attribute-specifier-seq [opt] attribute-specifier
25023 static tree
25024 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25026 tree attr_specs = NULL_TREE;
25027 tree attr_last = NULL_TREE;
25029 while (true)
25031 tree attr_spec = cp_parser_std_attribute_spec (parser);
25032 if (attr_spec == NULL_TREE)
25033 break;
25034 if (attr_spec == error_mark_node)
25035 return error_mark_node;
25037 if (attr_last)
25038 TREE_CHAIN (attr_last) = attr_spec;
25039 else
25040 attr_specs = attr_last = attr_spec;
25041 attr_last = tree_last (attr_last);
25044 return attr_specs;
25047 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25048 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25049 current value of the PEDANTIC flag, regardless of whether or not
25050 the `__extension__' keyword is present. The caller is responsible
25051 for restoring the value of the PEDANTIC flag. */
25053 static bool
25054 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25056 /* Save the old value of the PEDANTIC flag. */
25057 *saved_pedantic = pedantic;
25059 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25061 /* Consume the `__extension__' token. */
25062 cp_lexer_consume_token (parser->lexer);
25063 /* We're not being pedantic while the `__extension__' keyword is
25064 in effect. */
25065 pedantic = 0;
25067 return true;
25070 return false;
25073 /* Parse a label declaration.
25075 label-declaration:
25076 __label__ label-declarator-seq ;
25078 label-declarator-seq:
25079 identifier , label-declarator-seq
25080 identifier */
25082 static void
25083 cp_parser_label_declaration (cp_parser* parser)
25085 /* Look for the `__label__' keyword. */
25086 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25088 while (true)
25090 tree identifier;
25092 /* Look for an identifier. */
25093 identifier = cp_parser_identifier (parser);
25094 /* If we failed, stop. */
25095 if (identifier == error_mark_node)
25096 break;
25097 /* Declare it as a label. */
25098 finish_label_decl (identifier);
25099 /* If the next token is a `;', stop. */
25100 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25101 break;
25102 /* Look for the `,' separating the label declarations. */
25103 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25106 /* Look for the final `;'. */
25107 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25110 // -------------------------------------------------------------------------- //
25111 // Requires Clause
25113 // Parse a requires clause.
25115 // requires-clause:
25116 // 'requires' logical-or-expression
25118 // The required logical-or-expression must be a constant expression. Note
25119 // that we don't check that the expression is constepxr here. We defer until
25120 // we analyze constraints and then, we only check atomic constraints.
25121 static tree
25122 cp_parser_requires_clause (cp_parser *parser)
25124 // Parse the requires clause so that it is not automatically folded.
25125 ++processing_template_decl;
25126 tree expr = cp_parser_binary_expression (parser, false, false,
25127 PREC_NOT_OPERATOR, NULL);
25128 if (check_for_bare_parameter_packs (expr))
25129 expr = error_mark_node;
25130 --processing_template_decl;
25131 return expr;
25134 // Optionally parse a requires clause:
25135 static tree
25136 cp_parser_requires_clause_opt (cp_parser *parser)
25138 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25139 if (tok->keyword != RID_REQUIRES)
25141 if (!flag_concepts && tok->type == CPP_NAME
25142 && tok->u.value == ridpointers[RID_REQUIRES])
25144 error_at (cp_lexer_peek_token (parser->lexer)->location,
25145 "%<requires%> only available with -fconcepts");
25146 /* Parse and discard the requires-clause. */
25147 cp_lexer_consume_token (parser->lexer);
25148 cp_parser_requires_clause (parser);
25150 return NULL_TREE;
25152 cp_lexer_consume_token (parser->lexer);
25153 return cp_parser_requires_clause (parser);
25157 /*---------------------------------------------------------------------------
25158 Requires expressions
25159 ---------------------------------------------------------------------------*/
25161 /* Parse a requires expression
25163 requirement-expression:
25164 'requires' requirement-parameter-list [opt] requirement-body */
25165 static tree
25166 cp_parser_requires_expression (cp_parser *parser)
25168 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25169 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25171 /* A requires-expression shall appear only within a concept
25172 definition or a requires-clause.
25174 TODO: Implement this diagnostic correctly. */
25175 if (!processing_template_decl)
25177 error_at (loc, "a requires expression cannot appear outside a template");
25178 cp_parser_skip_to_end_of_statement (parser);
25179 return error_mark_node;
25182 tree parms, reqs;
25184 /* Local parameters are delared as variables within the scope
25185 of the expression. They are not visible past the end of
25186 the expression. Expressions within the requires-expression
25187 are unevaluated. */
25188 struct scope_sentinel
25190 scope_sentinel ()
25192 ++cp_unevaluated_operand;
25193 begin_scope (sk_block, NULL_TREE);
25196 ~scope_sentinel ()
25198 pop_bindings_and_leave_scope ();
25199 --cp_unevaluated_operand;
25201 } s;
25203 /* Parse the optional parameter list. */
25204 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25206 parms = cp_parser_requirement_parameter_list (parser);
25207 if (parms == error_mark_node)
25208 return error_mark_node;
25210 else
25211 parms = NULL_TREE;
25213 /* Parse the requirement body. */
25214 reqs = cp_parser_requirement_body (parser);
25215 if (reqs == error_mark_node)
25216 return error_mark_node;
25219 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25220 the parm chain. */
25221 grokparms (parms, &parms);
25222 return finish_requires_expr (parms, reqs);
25225 /* Parse a parameterized requirement.
25227 requirement-parameter-list:
25228 '(' parameter-declaration-clause ')' */
25229 static tree
25230 cp_parser_requirement_parameter_list (cp_parser *parser)
25232 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25233 return error_mark_node;
25235 tree parms = cp_parser_parameter_declaration_clause (parser);
25237 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25238 return error_mark_node;
25240 return parms;
25243 /* Parse the body of a requirement.
25245 requirement-body:
25246 '{' requirement-list '}' */
25247 static tree
25248 cp_parser_requirement_body (cp_parser *parser)
25250 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25251 return error_mark_node;
25253 tree reqs = cp_parser_requirement_list (parser);
25255 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25256 return error_mark_node;
25258 return reqs;
25261 /* Parse a list of requirements.
25263 requirement-list:
25264 requirement
25265 requirement-list ';' requirement[opt] */
25266 static tree
25267 cp_parser_requirement_list (cp_parser *parser)
25269 tree result = NULL_TREE;
25270 while (true)
25272 tree req = cp_parser_requirement (parser);
25273 if (req == error_mark_node)
25274 return error_mark_node;
25276 result = tree_cons (NULL_TREE, req, result);
25278 /* If we see a semi-colon, consume it. */
25279 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25280 cp_lexer_consume_token (parser->lexer);
25282 /* Stop processing at the end of the list. */
25283 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25284 break;
25287 /* Reverse the order of requirements so they are analyzed in
25288 declaration order. */
25289 return nreverse (result);
25292 /* Parse a syntactic requirement or type requirement.
25294 requirement:
25295 simple-requirement
25296 compound-requirement
25297 type-requirement
25298 nested-requirement */
25299 static tree
25300 cp_parser_requirement (cp_parser *parser)
25302 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25303 return cp_parser_compound_requirement (parser);
25304 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25305 return cp_parser_type_requirement (parser);
25306 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25307 return cp_parser_nested_requirement (parser);
25308 else
25309 return cp_parser_simple_requirement (parser);
25312 /* Parse a simple requirement.
25314 simple-requirement:
25315 expression ';' */
25316 static tree
25317 cp_parser_simple_requirement (cp_parser *parser)
25319 tree expr = cp_parser_expression (parser, NULL, false, false);
25320 if (!expr || expr == error_mark_node)
25321 return error_mark_node;
25323 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25324 return error_mark_node;
25326 return finish_simple_requirement (expr);
25329 /* Parse a type requirement
25331 type-requirement
25332 nested-name-specifier [opt] required-type-name ';'
25334 required-type-name:
25335 type-name
25336 'template' [opt] simple-template-id */
25337 static tree
25338 cp_parser_type_requirement (cp_parser *parser)
25340 cp_lexer_consume_token (parser->lexer);
25342 // Save the scope before parsing name specifiers.
25343 tree saved_scope = parser->scope;
25344 tree saved_object_scope = parser->object_scope;
25345 tree saved_qualifying_scope = parser->qualifying_scope;
25346 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25347 cp_parser_nested_name_specifier_opt (parser,
25348 /*typename_keyword_p=*/true,
25349 /*check_dependency_p=*/false,
25350 /*type_p=*/true,
25351 /*is_declaration=*/false);
25353 tree type;
25354 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25356 cp_lexer_consume_token (parser->lexer);
25357 type = cp_parser_template_id (parser,
25358 /*template_keyword_p=*/true,
25359 /*check_dependency=*/false,
25360 /*tag_type=*/none_type,
25361 /*is_declaration=*/false);
25362 type = make_typename_type (parser->scope, type, typename_type,
25363 /*complain=*/tf_error);
25365 else
25366 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25368 if (TREE_CODE (type) == TYPE_DECL)
25369 type = TREE_TYPE (type);
25371 parser->scope = saved_scope;
25372 parser->object_scope = saved_object_scope;
25373 parser->qualifying_scope = saved_qualifying_scope;
25375 if (type == error_mark_node)
25376 cp_parser_skip_to_end_of_statement (parser);
25378 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25379 return error_mark_node;
25380 if (type == error_mark_node)
25381 return error_mark_node;
25383 return finish_type_requirement (type);
25386 /* Parse a compound requirement
25388 compound-requirement:
25389 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25390 static tree
25391 cp_parser_compound_requirement (cp_parser *parser)
25393 /* Parse an expression enclosed in '{ }'s. */
25394 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25395 return error_mark_node;
25397 tree expr = cp_parser_expression (parser, NULL, false, false);
25398 if (!expr || expr == error_mark_node)
25399 return error_mark_node;
25401 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25402 return error_mark_node;
25404 /* Parse the optional noexcept. */
25405 bool noexcept_p = false;
25406 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25408 cp_lexer_consume_token (parser->lexer);
25409 noexcept_p = true;
25412 /* Parse the optional trailing return type. */
25413 tree type = NULL_TREE;
25414 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25416 cp_lexer_consume_token (parser->lexer);
25417 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25418 parser->in_result_type_constraint_p = true;
25419 type = cp_parser_trailing_type_id (parser);
25420 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25421 if (type == error_mark_node)
25422 return error_mark_node;
25425 return finish_compound_requirement (expr, type, noexcept_p);
25428 /* Parse a nested requirement. This is the same as a requires clause.
25430 nested-requirement:
25431 requires-clause */
25432 static tree
25433 cp_parser_nested_requirement (cp_parser *parser)
25435 cp_lexer_consume_token (parser->lexer);
25436 tree req = cp_parser_requires_clause (parser);
25437 if (req == error_mark_node)
25438 return error_mark_node;
25439 return finish_nested_requirement (req);
25442 /* Support Functions */
25444 /* Return the appropriate prefer_type argument for lookup_name_real based on
25445 tag_type and template_mem_access. */
25447 static inline int
25448 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25450 /* DR 141: When looking in the current enclosing context for a template-name
25451 after -> or ., only consider class templates. */
25452 if (template_mem_access)
25453 return 2;
25454 switch (tag_type)
25456 case none_type: return 0; // No preference.
25457 case scope_type: return 1; // Type or namespace.
25458 default: return 2; // Type only.
25462 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25463 NAME should have one of the representations used for an
25464 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25465 is returned. If PARSER->SCOPE is a dependent type, then a
25466 SCOPE_REF is returned.
25468 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25469 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25470 was formed. Abstractly, such entities should not be passed to this
25471 function, because they do not need to be looked up, but it is
25472 simpler to check for this special case here, rather than at the
25473 call-sites.
25475 In cases not explicitly covered above, this function returns a
25476 DECL, OVERLOAD, or baselink representing the result of the lookup.
25477 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25478 is returned.
25480 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25481 (e.g., "struct") that was used. In that case bindings that do not
25482 refer to types are ignored.
25484 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25485 ignored.
25487 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25488 are ignored.
25490 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25491 types.
25493 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25494 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25495 NULL_TREE otherwise. */
25497 static cp_expr
25498 cp_parser_lookup_name (cp_parser *parser, tree name,
25499 enum tag_types tag_type,
25500 bool is_template,
25501 bool is_namespace,
25502 bool check_dependency,
25503 tree *ambiguous_decls,
25504 location_t name_location)
25506 tree decl;
25507 tree object_type = parser->context->object_type;
25509 /* Assume that the lookup will be unambiguous. */
25510 if (ambiguous_decls)
25511 *ambiguous_decls = NULL_TREE;
25513 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25514 no longer valid. Note that if we are parsing tentatively, and
25515 the parse fails, OBJECT_TYPE will be automatically restored. */
25516 parser->context->object_type = NULL_TREE;
25518 if (name == error_mark_node)
25519 return error_mark_node;
25521 /* A template-id has already been resolved; there is no lookup to
25522 do. */
25523 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25524 return name;
25525 if (BASELINK_P (name))
25527 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25528 == TEMPLATE_ID_EXPR);
25529 return name;
25532 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25533 it should already have been checked to make sure that the name
25534 used matches the type being destroyed. */
25535 if (TREE_CODE (name) == BIT_NOT_EXPR)
25537 tree type;
25539 /* Figure out to which type this destructor applies. */
25540 if (parser->scope)
25541 type = parser->scope;
25542 else if (object_type)
25543 type = object_type;
25544 else
25545 type = current_class_type;
25546 /* If that's not a class type, there is no destructor. */
25547 if (!type || !CLASS_TYPE_P (type))
25548 return error_mark_node;
25549 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25550 lazily_declare_fn (sfk_destructor, type);
25551 if (!CLASSTYPE_DESTRUCTORS (type))
25552 return error_mark_node;
25553 /* If it was a class type, return the destructor. */
25554 return CLASSTYPE_DESTRUCTORS (type);
25557 /* By this point, the NAME should be an ordinary identifier. If
25558 the id-expression was a qualified name, the qualifying scope is
25559 stored in PARSER->SCOPE at this point. */
25560 gcc_assert (identifier_p (name));
25562 /* Perform the lookup. */
25563 if (parser->scope)
25565 bool dependent_p;
25567 if (parser->scope == error_mark_node)
25568 return error_mark_node;
25570 /* If the SCOPE is dependent, the lookup must be deferred until
25571 the template is instantiated -- unless we are explicitly
25572 looking up names in uninstantiated templates. Even then, we
25573 cannot look up the name if the scope is not a class type; it
25574 might, for example, be a template type parameter. */
25575 dependent_p = (TYPE_P (parser->scope)
25576 && dependent_scope_p (parser->scope));
25577 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25578 && dependent_p)
25579 /* Defer lookup. */
25580 decl = error_mark_node;
25581 else
25583 tree pushed_scope = NULL_TREE;
25585 /* If PARSER->SCOPE is a dependent type, then it must be a
25586 class type, and we must not be checking dependencies;
25587 otherwise, we would have processed this lookup above. So
25588 that PARSER->SCOPE is not considered a dependent base by
25589 lookup_member, we must enter the scope here. */
25590 if (dependent_p)
25591 pushed_scope = push_scope (parser->scope);
25593 /* If the PARSER->SCOPE is a template specialization, it
25594 may be instantiated during name lookup. In that case,
25595 errors may be issued. Even if we rollback the current
25596 tentative parse, those errors are valid. */
25597 decl = lookup_qualified_name (parser->scope, name,
25598 prefer_type_arg (tag_type),
25599 /*complain=*/true);
25601 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25602 lookup result and the nested-name-specifier nominates a class C:
25603 * if the name specified after the nested-name-specifier, when
25604 looked up in C, is the injected-class-name of C (Clause 9), or
25605 * if the name specified after the nested-name-specifier is the
25606 same as the identifier or the simple-template-id's template-
25607 name in the last component of the nested-name-specifier,
25608 the name is instead considered to name the constructor of
25609 class C. [ Note: for example, the constructor is not an
25610 acceptable lookup result in an elaborated-type-specifier so
25611 the constructor would not be used in place of the
25612 injected-class-name. --end note ] Such a constructor name
25613 shall be used only in the declarator-id of a declaration that
25614 names a constructor or in a using-declaration. */
25615 if (tag_type == none_type
25616 && DECL_SELF_REFERENCE_P (decl)
25617 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25618 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25619 prefer_type_arg (tag_type),
25620 /*complain=*/true);
25622 /* If we have a single function from a using decl, pull it out. */
25623 if (TREE_CODE (decl) == OVERLOAD
25624 && !really_overloaded_fn (decl))
25625 decl = OVL_FUNCTION (decl);
25627 if (pushed_scope)
25628 pop_scope (pushed_scope);
25631 /* If the scope is a dependent type and either we deferred lookup or
25632 we did lookup but didn't find the name, rememeber the name. */
25633 if (decl == error_mark_node && TYPE_P (parser->scope)
25634 && dependent_type_p (parser->scope))
25636 if (tag_type)
25638 tree type;
25640 /* The resolution to Core Issue 180 says that `struct
25641 A::B' should be considered a type-name, even if `A'
25642 is dependent. */
25643 type = make_typename_type (parser->scope, name, tag_type,
25644 /*complain=*/tf_error);
25645 if (type != error_mark_node)
25646 decl = TYPE_NAME (type);
25648 else if (is_template
25649 && (cp_parser_next_token_ends_template_argument_p (parser)
25650 || cp_lexer_next_token_is (parser->lexer,
25651 CPP_CLOSE_PAREN)))
25652 decl = make_unbound_class_template (parser->scope,
25653 name, NULL_TREE,
25654 /*complain=*/tf_error);
25655 else
25656 decl = build_qualified_name (/*type=*/NULL_TREE,
25657 parser->scope, name,
25658 is_template);
25660 parser->qualifying_scope = parser->scope;
25661 parser->object_scope = NULL_TREE;
25663 else if (object_type)
25665 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25666 OBJECT_TYPE is not a class. */
25667 if (CLASS_TYPE_P (object_type))
25668 /* If the OBJECT_TYPE is a template specialization, it may
25669 be instantiated during name lookup. In that case, errors
25670 may be issued. Even if we rollback the current tentative
25671 parse, those errors are valid. */
25672 decl = lookup_member (object_type,
25673 name,
25674 /*protect=*/0,
25675 prefer_type_arg (tag_type),
25676 tf_warning_or_error);
25677 else
25678 decl = NULL_TREE;
25680 if (!decl)
25681 /* Look it up in the enclosing context. DR 141: When looking for a
25682 template-name after -> or ., only consider class templates. */
25683 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25684 /*nonclass=*/0,
25685 /*block_p=*/true, is_namespace, 0);
25686 if (object_type == unknown_type_node)
25687 /* The object is type-dependent, so we can't look anything up; we used
25688 this to get the DR 141 behavior. */
25689 object_type = NULL_TREE;
25690 parser->object_scope = object_type;
25691 parser->qualifying_scope = NULL_TREE;
25693 else
25695 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25696 /*nonclass=*/0,
25697 /*block_p=*/true, is_namespace, 0);
25698 parser->qualifying_scope = NULL_TREE;
25699 parser->object_scope = NULL_TREE;
25702 /* If the lookup failed, let our caller know. */
25703 if (!decl || decl == error_mark_node)
25704 return error_mark_node;
25706 /* Pull out the template from an injected-class-name (or multiple). */
25707 if (is_template)
25708 decl = maybe_get_template_decl_from_type_decl (decl);
25710 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25711 if (TREE_CODE (decl) == TREE_LIST)
25713 if (ambiguous_decls)
25714 *ambiguous_decls = decl;
25715 /* The error message we have to print is too complicated for
25716 cp_parser_error, so we incorporate its actions directly. */
25717 if (!cp_parser_simulate_error (parser))
25719 error_at (name_location, "reference to %qD is ambiguous",
25720 name);
25721 print_candidates (decl);
25723 return error_mark_node;
25726 gcc_assert (DECL_P (decl)
25727 || TREE_CODE (decl) == OVERLOAD
25728 || TREE_CODE (decl) == SCOPE_REF
25729 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25730 || BASELINK_P (decl));
25732 /* If we have resolved the name of a member declaration, check to
25733 see if the declaration is accessible. When the name resolves to
25734 set of overloaded functions, accessibility is checked when
25735 overload resolution is done.
25737 During an explicit instantiation, access is not checked at all,
25738 as per [temp.explicit]. */
25739 if (DECL_P (decl))
25740 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25742 maybe_record_typedef_use (decl);
25744 return cp_expr (decl, name_location);
25747 /* Like cp_parser_lookup_name, but for use in the typical case where
25748 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25749 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25751 static tree
25752 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25754 return cp_parser_lookup_name (parser, name,
25755 none_type,
25756 /*is_template=*/false,
25757 /*is_namespace=*/false,
25758 /*check_dependency=*/true,
25759 /*ambiguous_decls=*/NULL,
25760 location);
25763 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25764 the current context, return the TYPE_DECL. If TAG_NAME_P is
25765 true, the DECL indicates the class being defined in a class-head,
25766 or declared in an elaborated-type-specifier.
25768 Otherwise, return DECL. */
25770 static tree
25771 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25773 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25774 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25776 struct A {
25777 template <typename T> struct B;
25780 template <typename T> struct A::B {};
25782 Similarly, in an elaborated-type-specifier:
25784 namespace N { struct X{}; }
25786 struct A {
25787 template <typename T> friend struct N::X;
25790 However, if the DECL refers to a class type, and we are in
25791 the scope of the class, then the name lookup automatically
25792 finds the TYPE_DECL created by build_self_reference rather
25793 than a TEMPLATE_DECL. For example, in:
25795 template <class T> struct S {
25796 S s;
25799 there is no need to handle such case. */
25801 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25802 return DECL_TEMPLATE_RESULT (decl);
25804 return decl;
25807 /* If too many, or too few, template-parameter lists apply to the
25808 declarator, issue an error message. Returns TRUE if all went well,
25809 and FALSE otherwise. */
25811 static bool
25812 cp_parser_check_declarator_template_parameters (cp_parser* parser,
25813 cp_declarator *declarator,
25814 location_t declarator_location)
25816 switch (declarator->kind)
25818 case cdk_id:
25820 unsigned num_templates = 0;
25821 tree scope = declarator->u.id.qualifying_scope;
25823 if (scope)
25824 num_templates = num_template_headers_for_class (scope);
25825 else if (TREE_CODE (declarator->u.id.unqualified_name)
25826 == TEMPLATE_ID_EXPR)
25827 /* If the DECLARATOR has the form `X<y>' then it uses one
25828 additional level of template parameters. */
25829 ++num_templates;
25831 return cp_parser_check_template_parameters
25832 (parser, num_templates, declarator_location, declarator);
25835 case cdk_function:
25836 case cdk_array:
25837 case cdk_pointer:
25838 case cdk_reference:
25839 case cdk_ptrmem:
25840 return (cp_parser_check_declarator_template_parameters
25841 (parser, declarator->declarator, declarator_location));
25843 case cdk_decomp:
25844 case cdk_error:
25845 return true;
25847 default:
25848 gcc_unreachable ();
25850 return false;
25853 /* NUM_TEMPLATES were used in the current declaration. If that is
25854 invalid, return FALSE and issue an error messages. Otherwise,
25855 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25856 declarator and we can print more accurate diagnostics. */
25858 static bool
25859 cp_parser_check_template_parameters (cp_parser* parser,
25860 unsigned num_templates,
25861 location_t location,
25862 cp_declarator *declarator)
25864 /* If there are the same number of template classes and parameter
25865 lists, that's OK. */
25866 if (parser->num_template_parameter_lists == num_templates)
25867 return true;
25868 /* If there are more, but only one more, then we are referring to a
25869 member template. That's OK too. */
25870 if (parser->num_template_parameter_lists == num_templates + 1)
25871 return true;
25872 /* If there are more template classes than parameter lists, we have
25873 something like:
25875 template <class T> void S<T>::R<T>::f (); */
25876 if (parser->num_template_parameter_lists < num_templates)
25878 if (declarator && !current_function_decl)
25879 error_at (location, "specializing member %<%T::%E%> "
25880 "requires %<template<>%> syntax",
25881 declarator->u.id.qualifying_scope,
25882 declarator->u.id.unqualified_name);
25883 else if (declarator)
25884 error_at (location, "invalid declaration of %<%T::%E%>",
25885 declarator->u.id.qualifying_scope,
25886 declarator->u.id.unqualified_name);
25887 else
25888 error_at (location, "too few template-parameter-lists");
25889 return false;
25891 /* Otherwise, there are too many template parameter lists. We have
25892 something like:
25894 template <class T> template <class U> void S::f(); */
25895 error_at (location, "too many template-parameter-lists");
25896 return false;
25899 /* Parse an optional `::' token indicating that the following name is
25900 from the global namespace. If so, PARSER->SCOPE is set to the
25901 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25902 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25903 Returns the new value of PARSER->SCOPE, if the `::' token is
25904 present, and NULL_TREE otherwise. */
25906 static tree
25907 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25909 cp_token *token;
25911 /* Peek at the next token. */
25912 token = cp_lexer_peek_token (parser->lexer);
25913 /* If we're looking at a `::' token then we're starting from the
25914 global namespace, not our current location. */
25915 if (token->type == CPP_SCOPE)
25917 /* Consume the `::' token. */
25918 cp_lexer_consume_token (parser->lexer);
25919 /* Set the SCOPE so that we know where to start the lookup. */
25920 parser->scope = global_namespace;
25921 parser->qualifying_scope = global_namespace;
25922 parser->object_scope = NULL_TREE;
25924 return parser->scope;
25926 else if (!current_scope_valid_p)
25928 parser->scope = NULL_TREE;
25929 parser->qualifying_scope = NULL_TREE;
25930 parser->object_scope = NULL_TREE;
25933 return NULL_TREE;
25936 /* Returns TRUE if the upcoming token sequence is the start of a
25937 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
25938 declarator is preceded by the `friend' specifier. */
25940 static bool
25941 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25943 bool constructor_p;
25944 bool outside_class_specifier_p;
25945 tree nested_name_specifier;
25946 cp_token *next_token;
25948 /* The common case is that this is not a constructor declarator, so
25949 try to avoid doing lots of work if at all possible. It's not
25950 valid declare a constructor at function scope. */
25951 if (parser->in_function_body)
25952 return false;
25953 /* And only certain tokens can begin a constructor declarator. */
25954 next_token = cp_lexer_peek_token (parser->lexer);
25955 if (next_token->type != CPP_NAME
25956 && next_token->type != CPP_SCOPE
25957 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25958 && next_token->type != CPP_TEMPLATE_ID)
25959 return false;
25961 /* Parse tentatively; we are going to roll back all of the tokens
25962 consumed here. */
25963 cp_parser_parse_tentatively (parser);
25964 /* Assume that we are looking at a constructor declarator. */
25965 constructor_p = true;
25967 /* Look for the optional `::' operator. */
25968 cp_parser_global_scope_opt (parser,
25969 /*current_scope_valid_p=*/false);
25970 /* Look for the nested-name-specifier. */
25971 nested_name_specifier
25972 = (cp_parser_nested_name_specifier_opt (parser,
25973 /*typename_keyword_p=*/false,
25974 /*check_dependency_p=*/false,
25975 /*type_p=*/false,
25976 /*is_declaration=*/false));
25978 outside_class_specifier_p = (!at_class_scope_p ()
25979 || !TYPE_BEING_DEFINED (current_class_type)
25980 || friend_p);
25982 /* Outside of a class-specifier, there must be a
25983 nested-name-specifier. Except in C++17 mode, where we
25984 might be declaring a guiding declaration. */
25985 if (!nested_name_specifier && outside_class_specifier_p
25986 && cxx_dialect < cxx1z)
25987 constructor_p = false;
25988 else if (nested_name_specifier == error_mark_node)
25989 constructor_p = false;
25991 /* If we have a class scope, this is easy; DR 147 says that S::S always
25992 names the constructor, and no other qualified name could. */
25993 if (constructor_p && nested_name_specifier
25994 && CLASS_TYPE_P (nested_name_specifier))
25996 tree id = cp_parser_unqualified_id (parser,
25997 /*template_keyword_p=*/false,
25998 /*check_dependency_p=*/false,
25999 /*declarator_p=*/true,
26000 /*optional_p=*/false);
26001 if (is_overloaded_fn (id))
26002 id = DECL_NAME (get_first_fn (id));
26003 if (!constructor_name_p (id, nested_name_specifier))
26004 constructor_p = false;
26006 /* If we still think that this might be a constructor-declarator,
26007 look for a class-name. */
26008 else if (constructor_p)
26010 /* If we have:
26012 template <typename T> struct S {
26013 S();
26016 we must recognize that the nested `S' names a class. */
26017 if (cxx_dialect >= cxx1z)
26018 cp_parser_parse_tentatively (parser);
26020 tree type_decl;
26021 type_decl = cp_parser_class_name (parser,
26022 /*typename_keyword_p=*/false,
26023 /*template_keyword_p=*/false,
26024 none_type,
26025 /*check_dependency_p=*/false,
26026 /*class_head_p=*/false,
26027 /*is_declaration=*/false);
26029 if (cxx_dialect >= cxx1z
26030 && !cp_parser_parse_definitely (parser))
26032 type_decl = NULL_TREE;
26033 tree tmpl = cp_parser_template_name (parser,
26034 /*template_keyword*/false,
26035 /*check_dependency_p*/false,
26036 /*is_declaration*/false,
26037 none_type,
26038 /*is_identifier*/NULL);
26039 if (DECL_CLASS_TEMPLATE_P (tmpl)
26040 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26041 /* It's a deduction guide, return true. */;
26042 else
26043 cp_parser_simulate_error (parser);
26046 /* If there was no class-name, then this is not a constructor.
26047 Otherwise, if we are in a class-specifier and we aren't
26048 handling a friend declaration, check that its type matches
26049 current_class_type (c++/38313). Note: error_mark_node
26050 is left alone for error recovery purposes. */
26051 constructor_p = (!cp_parser_error_occurred (parser)
26052 && (outside_class_specifier_p
26053 || type_decl == NULL_TREE
26054 || type_decl == error_mark_node
26055 || same_type_p (current_class_type,
26056 TREE_TYPE (type_decl))));
26058 /* If we're still considering a constructor, we have to see a `(',
26059 to begin the parameter-declaration-clause, followed by either a
26060 `)', an `...', or a decl-specifier. We need to check for a
26061 type-specifier to avoid being fooled into thinking that:
26063 S (f) (int);
26065 is a constructor. (It is actually a function named `f' that
26066 takes one parameter (of type `int') and returns a value of type
26067 `S'. */
26068 if (constructor_p
26069 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26070 constructor_p = false;
26072 if (constructor_p
26073 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26074 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26075 /* A parameter declaration begins with a decl-specifier,
26076 which is either the "attribute" keyword, a storage class
26077 specifier, or (usually) a type-specifier. */
26078 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26080 tree type;
26081 tree pushed_scope = NULL_TREE;
26082 unsigned saved_num_template_parameter_lists;
26084 /* Names appearing in the type-specifier should be looked up
26085 in the scope of the class. */
26086 if (current_class_type)
26087 type = NULL_TREE;
26088 else if (type_decl)
26090 type = TREE_TYPE (type_decl);
26091 if (TREE_CODE (type) == TYPENAME_TYPE)
26093 type = resolve_typename_type (type,
26094 /*only_current_p=*/false);
26095 if (TREE_CODE (type) == TYPENAME_TYPE)
26097 cp_parser_abort_tentative_parse (parser);
26098 return false;
26101 pushed_scope = push_scope (type);
26104 /* Inside the constructor parameter list, surrounding
26105 template-parameter-lists do not apply. */
26106 saved_num_template_parameter_lists
26107 = parser->num_template_parameter_lists;
26108 parser->num_template_parameter_lists = 0;
26110 /* Look for the type-specifier. */
26111 cp_parser_type_specifier (parser,
26112 CP_PARSER_FLAGS_NONE,
26113 /*decl_specs=*/NULL,
26114 /*is_declarator=*/true,
26115 /*declares_class_or_enum=*/NULL,
26116 /*is_cv_qualifier=*/NULL);
26118 parser->num_template_parameter_lists
26119 = saved_num_template_parameter_lists;
26121 /* Leave the scope of the class. */
26122 if (pushed_scope)
26123 pop_scope (pushed_scope);
26125 constructor_p = !cp_parser_error_occurred (parser);
26129 /* We did not really want to consume any tokens. */
26130 cp_parser_abort_tentative_parse (parser);
26132 return constructor_p;
26135 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26136 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26137 they must be performed once we are in the scope of the function.
26139 Returns the function defined. */
26141 static tree
26142 cp_parser_function_definition_from_specifiers_and_declarator
26143 (cp_parser* parser,
26144 cp_decl_specifier_seq *decl_specifiers,
26145 tree attributes,
26146 const cp_declarator *declarator)
26148 tree fn;
26149 bool success_p;
26151 /* Begin the function-definition. */
26152 success_p = start_function (decl_specifiers, declarator, attributes);
26154 /* The things we're about to see are not directly qualified by any
26155 template headers we've seen thus far. */
26156 reset_specialization ();
26158 /* If there were names looked up in the decl-specifier-seq that we
26159 did not check, check them now. We must wait until we are in the
26160 scope of the function to perform the checks, since the function
26161 might be a friend. */
26162 perform_deferred_access_checks (tf_warning_or_error);
26164 if (success_p)
26166 cp_finalize_omp_declare_simd (parser, current_function_decl);
26167 parser->omp_declare_simd = NULL;
26168 cp_finalize_oacc_routine (parser, current_function_decl, true);
26169 parser->oacc_routine = NULL;
26172 if (!success_p)
26174 /* Skip the entire function. */
26175 cp_parser_skip_to_end_of_block_or_statement (parser);
26176 fn = error_mark_node;
26178 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26180 /* Seen already, skip it. An error message has already been output. */
26181 cp_parser_skip_to_end_of_block_or_statement (parser);
26182 fn = current_function_decl;
26183 current_function_decl = NULL_TREE;
26184 /* If this is a function from a class, pop the nested class. */
26185 if (current_class_name)
26186 pop_nested_class ();
26188 else
26190 timevar_id_t tv;
26191 if (DECL_DECLARED_INLINE_P (current_function_decl))
26192 tv = TV_PARSE_INLINE;
26193 else
26194 tv = TV_PARSE_FUNC;
26195 timevar_push (tv);
26196 fn = cp_parser_function_definition_after_declarator (parser,
26197 /*inline_p=*/false);
26198 timevar_pop (tv);
26201 return fn;
26204 /* Parse the part of a function-definition that follows the
26205 declarator. INLINE_P is TRUE iff this function is an inline
26206 function defined within a class-specifier.
26208 Returns the function defined. */
26210 static tree
26211 cp_parser_function_definition_after_declarator (cp_parser* parser,
26212 bool inline_p)
26214 tree fn;
26215 bool ctor_initializer_p = false;
26216 bool saved_in_unbraced_linkage_specification_p;
26217 bool saved_in_function_body;
26218 unsigned saved_num_template_parameter_lists;
26219 cp_token *token;
26220 bool fully_implicit_function_template_p
26221 = parser->fully_implicit_function_template_p;
26222 parser->fully_implicit_function_template_p = false;
26223 tree implicit_template_parms
26224 = parser->implicit_template_parms;
26225 parser->implicit_template_parms = 0;
26226 cp_binding_level* implicit_template_scope
26227 = parser->implicit_template_scope;
26228 parser->implicit_template_scope = 0;
26230 saved_in_function_body = parser->in_function_body;
26231 parser->in_function_body = true;
26232 /* If the next token is `return', then the code may be trying to
26233 make use of the "named return value" extension that G++ used to
26234 support. */
26235 token = cp_lexer_peek_token (parser->lexer);
26236 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26238 /* Consume the `return' keyword. */
26239 cp_lexer_consume_token (parser->lexer);
26240 /* Look for the identifier that indicates what value is to be
26241 returned. */
26242 cp_parser_identifier (parser);
26243 /* Issue an error message. */
26244 error_at (token->location,
26245 "named return values are no longer supported");
26246 /* Skip tokens until we reach the start of the function body. */
26247 while (true)
26249 cp_token *token = cp_lexer_peek_token (parser->lexer);
26250 if (token->type == CPP_OPEN_BRACE
26251 || token->type == CPP_EOF
26252 || token->type == CPP_PRAGMA_EOL)
26253 break;
26254 cp_lexer_consume_token (parser->lexer);
26257 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26258 anything declared inside `f'. */
26259 saved_in_unbraced_linkage_specification_p
26260 = parser->in_unbraced_linkage_specification_p;
26261 parser->in_unbraced_linkage_specification_p = false;
26262 /* Inside the function, surrounding template-parameter-lists do not
26263 apply. */
26264 saved_num_template_parameter_lists
26265 = parser->num_template_parameter_lists;
26266 parser->num_template_parameter_lists = 0;
26268 start_lambda_scope (current_function_decl);
26270 /* If the next token is `try', `__transaction_atomic', or
26271 `__transaction_relaxed`, then we are looking at either function-try-block
26272 or function-transaction-block. Note that all of these include the
26273 function-body. */
26274 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26275 ctor_initializer_p = cp_parser_function_transaction (parser,
26276 RID_TRANSACTION_ATOMIC);
26277 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26278 RID_TRANSACTION_RELAXED))
26279 ctor_initializer_p = cp_parser_function_transaction (parser,
26280 RID_TRANSACTION_RELAXED);
26281 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26282 ctor_initializer_p = cp_parser_function_try_block (parser);
26283 else
26284 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
26285 (parser, /*in_function_try_block=*/false);
26287 finish_lambda_scope ();
26289 /* Finish the function. */
26290 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
26291 (inline_p ? 2 : 0));
26292 /* Generate code for it, if necessary. */
26293 expand_or_defer_fn (fn);
26294 /* Restore the saved values. */
26295 parser->in_unbraced_linkage_specification_p
26296 = saved_in_unbraced_linkage_specification_p;
26297 parser->num_template_parameter_lists
26298 = saved_num_template_parameter_lists;
26299 parser->in_function_body = saved_in_function_body;
26301 parser->fully_implicit_function_template_p
26302 = fully_implicit_function_template_p;
26303 parser->implicit_template_parms
26304 = implicit_template_parms;
26305 parser->implicit_template_scope
26306 = implicit_template_scope;
26308 if (parser->fully_implicit_function_template_p)
26309 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26311 return fn;
26314 /* Parse a template-declaration body (following argument list). */
26316 static void
26317 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26318 tree parameter_list,
26319 bool member_p)
26321 tree decl = NULL_TREE;
26322 bool friend_p = false;
26324 /* We just processed one more parameter list. */
26325 ++parser->num_template_parameter_lists;
26327 /* Get the deferred access checks from the parameter list. These
26328 will be checked once we know what is being declared, as for a
26329 member template the checks must be performed in the scope of the
26330 class containing the member. */
26331 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26333 /* Tentatively parse for a new template parameter list, which can either be
26334 the template keyword or a template introduction. */
26335 if (cp_parser_template_declaration_after_export (parser, member_p))
26336 /* OK */;
26337 else if (cxx_dialect >= cxx11
26338 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26339 decl = cp_parser_alias_declaration (parser);
26340 else
26342 /* There are no access checks when parsing a template, as we do not
26343 know if a specialization will be a friend. */
26344 push_deferring_access_checks (dk_no_check);
26345 cp_token *token = cp_lexer_peek_token (parser->lexer);
26346 decl = cp_parser_single_declaration (parser,
26347 checks,
26348 member_p,
26349 /*explicit_specialization_p=*/false,
26350 &friend_p);
26351 pop_deferring_access_checks ();
26353 /* If this is a member template declaration, let the front
26354 end know. */
26355 if (member_p && !friend_p && decl)
26357 if (TREE_CODE (decl) == TYPE_DECL)
26358 cp_parser_check_access_in_redeclaration (decl, token->location);
26360 decl = finish_member_template_decl (decl);
26362 else if (friend_p && decl
26363 && DECL_DECLARES_TYPE_P (decl))
26364 make_friend_class (current_class_type, TREE_TYPE (decl),
26365 /*complain=*/true);
26367 /* We are done with the current parameter list. */
26368 --parser->num_template_parameter_lists;
26370 pop_deferring_access_checks ();
26372 /* Finish up. */
26373 finish_template_decl (parameter_list);
26375 /* Check the template arguments for a literal operator template. */
26376 if (decl
26377 && DECL_DECLARES_FUNCTION_P (decl)
26378 && UDLIT_OPER_P (DECL_NAME (decl)))
26380 bool ok = true;
26381 if (parameter_list == NULL_TREE)
26382 ok = false;
26383 else
26385 int num_parms = TREE_VEC_LENGTH (parameter_list);
26386 if (num_parms == 1)
26388 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26389 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26390 if (TREE_TYPE (parm) != char_type_node
26391 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26392 ok = false;
26394 else if (num_parms == 2 && cxx_dialect >= cxx14)
26396 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26397 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26398 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26399 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26400 if (parm == error_mark_node
26401 || TREE_TYPE (parm) != TREE_TYPE (type)
26402 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26403 ok = false;
26405 else
26406 ok = false;
26408 if (!ok)
26410 if (cxx_dialect >= cxx14)
26411 error ("literal operator template %qD has invalid parameter list."
26412 " Expected non-type template argument pack <char...>"
26413 " or <typename CharT, CharT...>",
26414 decl);
26415 else
26416 error ("literal operator template %qD has invalid parameter list."
26417 " Expected non-type template argument pack <char...>",
26418 decl);
26422 /* Register member declarations. */
26423 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26424 finish_member_declaration (decl);
26425 /* If DECL is a function template, we must return to parse it later.
26426 (Even though there is no definition, there might be default
26427 arguments that need handling.) */
26428 if (member_p && decl
26429 && DECL_DECLARES_FUNCTION_P (decl))
26430 vec_safe_push (unparsed_funs_with_definitions, decl);
26433 /* Parse a template introduction header for a template-declaration. Returns
26434 false if tentative parse fails. */
26436 static bool
26437 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26439 cp_parser_parse_tentatively (parser);
26441 tree saved_scope = parser->scope;
26442 tree saved_object_scope = parser->object_scope;
26443 tree saved_qualifying_scope = parser->qualifying_scope;
26445 /* Look for the optional `::' operator. */
26446 cp_parser_global_scope_opt (parser,
26447 /*current_scope_valid_p=*/false);
26448 /* Look for the nested-name-specifier. */
26449 cp_parser_nested_name_specifier_opt (parser,
26450 /*typename_keyword_p=*/false,
26451 /*check_dependency_p=*/true,
26452 /*type_p=*/false,
26453 /*is_declaration=*/false);
26455 cp_token *token = cp_lexer_peek_token (parser->lexer);
26456 tree concept_name = cp_parser_identifier (parser);
26458 /* Look up the concept for which we will be matching
26459 template parameters. */
26460 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26461 token->location);
26462 parser->scope = saved_scope;
26463 parser->object_scope = saved_object_scope;
26464 parser->qualifying_scope = saved_qualifying_scope;
26466 if (concept_name == error_mark_node)
26467 cp_parser_simulate_error (parser);
26469 /* Look for opening brace for introduction. */
26470 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
26472 if (!cp_parser_parse_definitely (parser))
26473 return false;
26475 push_deferring_access_checks (dk_deferred);
26477 /* Build vector of placeholder parameters and grab
26478 matching identifiers. */
26479 tree introduction_list = cp_parser_introduction_list (parser);
26481 /* The introduction-list shall not be empty. */
26482 int nargs = TREE_VEC_LENGTH (introduction_list);
26483 if (nargs == 0)
26485 error ("empty introduction-list");
26486 return true;
26489 /* Look for closing brace for introduction. */
26490 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
26491 return true;
26493 if (tmpl_decl == error_mark_node)
26495 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26496 token->location);
26497 return true;
26500 /* Build and associate the constraint. */
26501 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26502 if (parms && parms != error_mark_node)
26504 cp_parser_template_declaration_after_parameters (parser, parms,
26505 member_p);
26506 return true;
26509 error_at (token->location, "no matching concept for template-introduction");
26510 return true;
26513 /* Parse a normal template-declaration following the template keyword. */
26515 static void
26516 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26518 tree parameter_list;
26519 bool need_lang_pop;
26520 location_t location = input_location;
26522 /* Look for the `<' token. */
26523 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26524 return;
26525 if (at_class_scope_p () && current_function_decl)
26527 /* 14.5.2.2 [temp.mem]
26529 A local class shall not have member templates. */
26530 error_at (location,
26531 "invalid declaration of member template in local class");
26532 cp_parser_skip_to_end_of_block_or_statement (parser);
26533 return;
26535 /* [temp]
26537 A template ... shall not have C linkage. */
26538 if (current_lang_name == lang_name_c)
26540 error_at (location, "template with C linkage");
26541 /* Give it C++ linkage to avoid confusing other parts of the
26542 front end. */
26543 push_lang_context (lang_name_cplusplus);
26544 need_lang_pop = true;
26546 else
26547 need_lang_pop = false;
26549 /* We cannot perform access checks on the template parameter
26550 declarations until we know what is being declared, just as we
26551 cannot check the decl-specifier list. */
26552 push_deferring_access_checks (dk_deferred);
26554 /* If the next token is `>', then we have an invalid
26555 specialization. Rather than complain about an invalid template
26556 parameter, issue an error message here. */
26557 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26559 cp_parser_error (parser, "invalid explicit specialization");
26560 begin_specialization ();
26561 parameter_list = NULL_TREE;
26563 else
26565 /* Parse the template parameters. */
26566 parameter_list = cp_parser_template_parameter_list (parser);
26569 /* Look for the `>'. */
26570 cp_parser_skip_to_end_of_template_parameter_list (parser);
26572 /* Manage template requirements */
26573 if (flag_concepts)
26575 tree reqs = get_shorthand_constraints (current_template_parms);
26576 if (tree r = cp_parser_requires_clause_opt (parser))
26577 reqs = conjoin_constraints (reqs, normalize_expression (r));
26578 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26581 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26582 member_p);
26584 /* For the erroneous case of a template with C linkage, we pushed an
26585 implicit C++ linkage scope; exit that scope now. */
26586 if (need_lang_pop)
26587 pop_lang_context ();
26590 /* Parse a template-declaration, assuming that the `export' (and
26591 `extern') keywords, if present, has already been scanned. MEMBER_P
26592 is as for cp_parser_template_declaration. */
26594 static bool
26595 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26597 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26599 cp_lexer_consume_token (parser->lexer);
26600 cp_parser_explicit_template_declaration (parser, member_p);
26601 return true;
26603 else if (flag_concepts)
26604 return cp_parser_template_introduction (parser, member_p);
26606 return false;
26609 /* Perform the deferred access checks from a template-parameter-list.
26610 CHECKS is a TREE_LIST of access checks, as returned by
26611 get_deferred_access_checks. */
26613 static void
26614 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26616 ++processing_template_parmlist;
26617 perform_access_checks (checks, tf_warning_or_error);
26618 --processing_template_parmlist;
26621 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26622 `function-definition' sequence that follows a template header.
26623 If MEMBER_P is true, this declaration appears in a class scope.
26625 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26626 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26628 static tree
26629 cp_parser_single_declaration (cp_parser* parser,
26630 vec<deferred_access_check, va_gc> *checks,
26631 bool member_p,
26632 bool explicit_specialization_p,
26633 bool* friend_p)
26635 int declares_class_or_enum;
26636 tree decl = NULL_TREE;
26637 cp_decl_specifier_seq decl_specifiers;
26638 bool function_definition_p = false;
26639 cp_token *decl_spec_token_start;
26641 /* This function is only used when processing a template
26642 declaration. */
26643 gcc_assert (innermost_scope_kind () == sk_template_parms
26644 || innermost_scope_kind () == sk_template_spec);
26646 /* Defer access checks until we know what is being declared. */
26647 push_deferring_access_checks (dk_deferred);
26649 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26650 alternative. */
26651 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26652 cp_parser_decl_specifier_seq (parser,
26653 CP_PARSER_FLAGS_OPTIONAL,
26654 &decl_specifiers,
26655 &declares_class_or_enum);
26656 if (friend_p)
26657 *friend_p = cp_parser_friend_p (&decl_specifiers);
26659 /* There are no template typedefs. */
26660 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26662 error_at (decl_spec_token_start->location,
26663 "template declaration of %<typedef%>");
26664 decl = error_mark_node;
26667 /* Gather up the access checks that occurred the
26668 decl-specifier-seq. */
26669 stop_deferring_access_checks ();
26671 /* Check for the declaration of a template class. */
26672 if (declares_class_or_enum)
26674 if (cp_parser_declares_only_class_p (parser)
26675 || (declares_class_or_enum & 2))
26677 // If this is a declaration, but not a definition, associate
26678 // any constraints with the type declaration. Constraints
26679 // are associated with definitions in cp_parser_class_specifier.
26680 if (declares_class_or_enum == 1)
26681 associate_classtype_constraints (decl_specifiers.type);
26683 decl = shadow_tag (&decl_specifiers);
26685 /* In this case:
26687 struct C {
26688 friend template <typename T> struct A<T>::B;
26691 A<T>::B will be represented by a TYPENAME_TYPE, and
26692 therefore not recognized by shadow_tag. */
26693 if (friend_p && *friend_p
26694 && !decl
26695 && decl_specifiers.type
26696 && TYPE_P (decl_specifiers.type))
26697 decl = decl_specifiers.type;
26699 if (decl && decl != error_mark_node)
26700 decl = TYPE_NAME (decl);
26701 else
26702 decl = error_mark_node;
26704 /* Perform access checks for template parameters. */
26705 cp_parser_perform_template_parameter_access_checks (checks);
26707 /* Give a helpful diagnostic for
26708 template <class T> struct A { } a;
26709 if we aren't already recovering from an error. */
26710 if (!cp_parser_declares_only_class_p (parser)
26711 && !seen_error ())
26713 error_at (cp_lexer_peek_token (parser->lexer)->location,
26714 "a class template declaration must not declare "
26715 "anything else");
26716 cp_parser_skip_to_end_of_block_or_statement (parser);
26717 goto out;
26722 /* Complain about missing 'typename' or other invalid type names. */
26723 if (!decl_specifiers.any_type_specifiers_p
26724 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26726 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26727 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26728 the rest of this declaration. */
26729 decl = error_mark_node;
26730 goto out;
26733 /* If it's not a template class, try for a template function. If
26734 the next token is a `;', then this declaration does not declare
26735 anything. But, if there were errors in the decl-specifiers, then
26736 the error might well have come from an attempted class-specifier.
26737 In that case, there's no need to warn about a missing declarator. */
26738 if (!decl
26739 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26740 || decl_specifiers.type != error_mark_node))
26742 decl = cp_parser_init_declarator (parser,
26743 &decl_specifiers,
26744 checks,
26745 /*function_definition_allowed_p=*/true,
26746 member_p,
26747 declares_class_or_enum,
26748 &function_definition_p,
26749 NULL, NULL, NULL);
26751 /* 7.1.1-1 [dcl.stc]
26753 A storage-class-specifier shall not be specified in an explicit
26754 specialization... */
26755 if (decl
26756 && explicit_specialization_p
26757 && decl_specifiers.storage_class != sc_none)
26759 error_at (decl_spec_token_start->location,
26760 "explicit template specialization cannot have a storage class");
26761 decl = error_mark_node;
26764 if (decl && VAR_P (decl))
26765 check_template_variable (decl);
26768 /* Look for a trailing `;' after the declaration. */
26769 if (!function_definition_p
26770 && (decl == error_mark_node
26771 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26772 cp_parser_skip_to_end_of_block_or_statement (parser);
26774 out:
26775 pop_deferring_access_checks ();
26777 /* Clear any current qualification; whatever comes next is the start
26778 of something new. */
26779 parser->scope = NULL_TREE;
26780 parser->qualifying_scope = NULL_TREE;
26781 parser->object_scope = NULL_TREE;
26783 return decl;
26786 /* Parse a cast-expression that is not the operand of a unary "&". */
26788 static cp_expr
26789 cp_parser_simple_cast_expression (cp_parser *parser)
26791 return cp_parser_cast_expression (parser, /*address_p=*/false,
26792 /*cast_p=*/false, /*decltype*/false, NULL);
26795 /* Parse a functional cast to TYPE. Returns an expression
26796 representing the cast. */
26798 static cp_expr
26799 cp_parser_functional_cast (cp_parser* parser, tree type)
26801 vec<tree, va_gc> *vec;
26802 tree expression_list;
26803 cp_expr cast;
26804 bool nonconst_p;
26806 location_t start_loc = input_location;
26808 if (!type)
26809 type = error_mark_node;
26811 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26813 cp_lexer_set_source_position (parser->lexer);
26814 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26815 expression_list = cp_parser_braced_list (parser, &nonconst_p);
26816 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
26817 if (TREE_CODE (type) == TYPE_DECL)
26818 type = TREE_TYPE (type);
26820 cast = finish_compound_literal (type, expression_list,
26821 tf_warning_or_error);
26822 /* Create a location of the form:
26823 type_name{i, f}
26824 ^~~~~~~~~~~~~~~
26825 with caret == start at the start of the type name,
26826 finishing at the closing brace. */
26827 location_t finish_loc
26828 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26829 location_t combined_loc = make_location (start_loc, start_loc,
26830 finish_loc);
26831 cast.set_location (combined_loc);
26832 return cast;
26836 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26837 /*cast_p=*/true,
26838 /*allow_expansion_p=*/true,
26839 /*non_constant_p=*/NULL);
26840 if (vec == NULL)
26841 expression_list = error_mark_node;
26842 else
26844 expression_list = build_tree_list_vec (vec);
26845 release_tree_vector (vec);
26848 cast = build_functional_cast (type, expression_list,
26849 tf_warning_or_error);
26850 /* [expr.const]/1: In an integral constant expression "only type
26851 conversions to integral or enumeration type can be used". */
26852 if (TREE_CODE (type) == TYPE_DECL)
26853 type = TREE_TYPE (type);
26854 if (cast != error_mark_node
26855 && !cast_valid_in_integral_constant_expression_p (type)
26856 && cp_parser_non_integral_constant_expression (parser,
26857 NIC_CONSTRUCTOR))
26858 return error_mark_node;
26860 /* Create a location of the form:
26861 float(i)
26862 ^~~~~~~~
26863 with caret == start at the start of the type name,
26864 finishing at the closing paren. */
26865 location_t finish_loc
26866 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26867 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26868 cast.set_location (combined_loc);
26869 return cast;
26872 /* Save the tokens that make up the body of a member function defined
26873 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26874 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26875 specifiers applied to the declaration. Returns the FUNCTION_DECL
26876 for the member function. */
26878 static tree
26879 cp_parser_save_member_function_body (cp_parser* parser,
26880 cp_decl_specifier_seq *decl_specifiers,
26881 cp_declarator *declarator,
26882 tree attributes)
26884 cp_token *first;
26885 cp_token *last;
26886 tree fn;
26887 bool function_try_block = false;
26889 /* Create the FUNCTION_DECL. */
26890 fn = grokmethod (decl_specifiers, declarator, attributes);
26891 cp_finalize_omp_declare_simd (parser, fn);
26892 cp_finalize_oacc_routine (parser, fn, true);
26893 /* If something went badly wrong, bail out now. */
26894 if (fn == error_mark_node)
26896 /* If there's a function-body, skip it. */
26897 if (cp_parser_token_starts_function_definition_p
26898 (cp_lexer_peek_token (parser->lexer)))
26899 cp_parser_skip_to_end_of_block_or_statement (parser);
26900 return error_mark_node;
26903 /* Remember it, if there default args to post process. */
26904 cp_parser_save_default_args (parser, fn);
26906 /* Save away the tokens that make up the body of the
26907 function. */
26908 first = parser->lexer->next_token;
26910 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26911 cp_lexer_consume_token (parser->lexer);
26912 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26913 RID_TRANSACTION_ATOMIC))
26915 cp_lexer_consume_token (parser->lexer);
26916 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26917 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26918 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26919 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26920 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26921 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26922 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26924 cp_lexer_consume_token (parser->lexer);
26925 cp_lexer_consume_token (parser->lexer);
26926 cp_lexer_consume_token (parser->lexer);
26927 cp_lexer_consume_token (parser->lexer);
26928 cp_lexer_consume_token (parser->lexer);
26930 else
26931 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
26932 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
26934 cp_lexer_consume_token (parser->lexer);
26935 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26936 break;
26940 /* Handle function try blocks. */
26941 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26943 cp_lexer_consume_token (parser->lexer);
26944 function_try_block = true;
26946 /* We can have braced-init-list mem-initializers before the fn body. */
26947 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26949 cp_lexer_consume_token (parser->lexer);
26950 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26952 /* cache_group will stop after an un-nested { } pair, too. */
26953 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26954 break;
26956 /* variadic mem-inits have ... after the ')'. */
26957 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26958 cp_lexer_consume_token (parser->lexer);
26961 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26962 /* Handle function try blocks. */
26963 if (function_try_block)
26964 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26965 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26966 last = parser->lexer->next_token;
26968 /* Save away the inline definition; we will process it when the
26969 class is complete. */
26970 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26971 DECL_PENDING_INLINE_P (fn) = 1;
26973 /* We need to know that this was defined in the class, so that
26974 friend templates are handled correctly. */
26975 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26977 /* Add FN to the queue of functions to be parsed later. */
26978 vec_safe_push (unparsed_funs_with_definitions, fn);
26980 return fn;
26983 /* Save the tokens that make up the in-class initializer for a non-static
26984 data member. Returns a DEFAULT_ARG. */
26986 static tree
26987 cp_parser_save_nsdmi (cp_parser* parser)
26989 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26992 /* Parse a template-argument-list, as well as the trailing ">" (but
26993 not the opening "<"). See cp_parser_template_argument_list for the
26994 return value. */
26996 static tree
26997 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26999 tree arguments;
27000 tree saved_scope;
27001 tree saved_qualifying_scope;
27002 tree saved_object_scope;
27003 bool saved_greater_than_is_operator_p;
27004 int saved_unevaluated_operand;
27005 int saved_inhibit_evaluation_warnings;
27007 /* [temp.names]
27009 When parsing a template-id, the first non-nested `>' is taken as
27010 the end of the template-argument-list rather than a greater-than
27011 operator. */
27012 saved_greater_than_is_operator_p
27013 = parser->greater_than_is_operator_p;
27014 parser->greater_than_is_operator_p = false;
27015 /* Parsing the argument list may modify SCOPE, so we save it
27016 here. */
27017 saved_scope = parser->scope;
27018 saved_qualifying_scope = parser->qualifying_scope;
27019 saved_object_scope = parser->object_scope;
27020 /* We need to evaluate the template arguments, even though this
27021 template-id may be nested within a "sizeof". */
27022 saved_unevaluated_operand = cp_unevaluated_operand;
27023 cp_unevaluated_operand = 0;
27024 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27025 c_inhibit_evaluation_warnings = 0;
27026 /* Parse the template-argument-list itself. */
27027 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27028 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27029 arguments = NULL_TREE;
27030 else
27031 arguments = cp_parser_template_argument_list (parser);
27032 /* Look for the `>' that ends the template-argument-list. If we find
27033 a '>>' instead, it's probably just a typo. */
27034 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27036 if (cxx_dialect != cxx98)
27038 /* In C++0x, a `>>' in a template argument list or cast
27039 expression is considered to be two separate `>'
27040 tokens. So, change the current token to a `>', but don't
27041 consume it: it will be consumed later when the outer
27042 template argument list (or cast expression) is parsed.
27043 Note that this replacement of `>' for `>>' is necessary
27044 even if we are parsing tentatively: in the tentative
27045 case, after calling
27046 cp_parser_enclosed_template_argument_list we will always
27047 throw away all of the template arguments and the first
27048 closing `>', either because the template argument list
27049 was erroneous or because we are replacing those tokens
27050 with a CPP_TEMPLATE_ID token. The second `>' (which will
27051 not have been thrown away) is needed either to close an
27052 outer template argument list or to complete a new-style
27053 cast. */
27054 cp_token *token = cp_lexer_peek_token (parser->lexer);
27055 token->type = CPP_GREATER;
27057 else if (!saved_greater_than_is_operator_p)
27059 /* If we're in a nested template argument list, the '>>' has
27060 to be a typo for '> >'. We emit the error message, but we
27061 continue parsing and we push a '>' as next token, so that
27062 the argument list will be parsed correctly. Note that the
27063 global source location is still on the token before the
27064 '>>', so we need to say explicitly where we want it. */
27065 cp_token *token = cp_lexer_peek_token (parser->lexer);
27066 gcc_rich_location richloc (token->location);
27067 richloc.add_fixit_replace ("> >");
27068 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
27069 "within a nested template argument list");
27071 token->type = CPP_GREATER;
27073 else
27075 /* If this is not a nested template argument list, the '>>'
27076 is a typo for '>'. Emit an error message and continue.
27077 Same deal about the token location, but here we can get it
27078 right by consuming the '>>' before issuing the diagnostic. */
27079 cp_token *token = cp_lexer_consume_token (parser->lexer);
27080 error_at (token->location,
27081 "spurious %<>>%>, use %<>%> to terminate "
27082 "a template argument list");
27085 else
27086 cp_parser_skip_to_end_of_template_parameter_list (parser);
27087 /* The `>' token might be a greater-than operator again now. */
27088 parser->greater_than_is_operator_p
27089 = saved_greater_than_is_operator_p;
27090 /* Restore the SAVED_SCOPE. */
27091 parser->scope = saved_scope;
27092 parser->qualifying_scope = saved_qualifying_scope;
27093 parser->object_scope = saved_object_scope;
27094 cp_unevaluated_operand = saved_unevaluated_operand;
27095 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27097 return arguments;
27100 /* MEMBER_FUNCTION is a member function, or a friend. If default
27101 arguments, or the body of the function have not yet been parsed,
27102 parse them now. */
27104 static void
27105 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27107 timevar_push (TV_PARSE_INMETH);
27108 /* If this member is a template, get the underlying
27109 FUNCTION_DECL. */
27110 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27111 member_function = DECL_TEMPLATE_RESULT (member_function);
27113 /* There should not be any class definitions in progress at this
27114 point; the bodies of members are only parsed outside of all class
27115 definitions. */
27116 gcc_assert (parser->num_classes_being_defined == 0);
27117 /* While we're parsing the member functions we might encounter more
27118 classes. We want to handle them right away, but we don't want
27119 them getting mixed up with functions that are currently in the
27120 queue. */
27121 push_unparsed_function_queues (parser);
27123 /* Make sure that any template parameters are in scope. */
27124 maybe_begin_member_template_processing (member_function);
27126 /* If the body of the function has not yet been parsed, parse it
27127 now. */
27128 if (DECL_PENDING_INLINE_P (member_function))
27130 tree function_scope;
27131 cp_token_cache *tokens;
27133 /* The function is no longer pending; we are processing it. */
27134 tokens = DECL_PENDING_INLINE_INFO (member_function);
27135 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27136 DECL_PENDING_INLINE_P (member_function) = 0;
27138 /* If this is a local class, enter the scope of the containing
27139 function. */
27140 function_scope = current_function_decl;
27141 if (function_scope)
27142 push_function_context ();
27144 /* Push the body of the function onto the lexer stack. */
27145 cp_parser_push_lexer_for_tokens (parser, tokens);
27147 /* Let the front end know that we going to be defining this
27148 function. */
27149 start_preparsed_function (member_function, NULL_TREE,
27150 SF_PRE_PARSED | SF_INCLASS_INLINE);
27152 /* Don't do access checking if it is a templated function. */
27153 if (processing_template_decl)
27154 push_deferring_access_checks (dk_no_check);
27156 /* #pragma omp declare reduction needs special parsing. */
27157 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27159 parser->lexer->in_pragma = true;
27160 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27161 finish_function (/*inline*/2);
27162 cp_check_omp_declare_reduction (member_function);
27164 else
27165 /* Now, parse the body of the function. */
27166 cp_parser_function_definition_after_declarator (parser,
27167 /*inline_p=*/true);
27169 if (processing_template_decl)
27170 pop_deferring_access_checks ();
27172 /* Leave the scope of the containing function. */
27173 if (function_scope)
27174 pop_function_context ();
27175 cp_parser_pop_lexer (parser);
27178 /* Remove any template parameters from the symbol table. */
27179 maybe_end_member_template_processing ();
27181 /* Restore the queue. */
27182 pop_unparsed_function_queues (parser);
27183 timevar_pop (TV_PARSE_INMETH);
27186 /* If DECL contains any default args, remember it on the unparsed
27187 functions queue. */
27189 static void
27190 cp_parser_save_default_args (cp_parser* parser, tree decl)
27192 tree probe;
27194 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27195 probe;
27196 probe = TREE_CHAIN (probe))
27197 if (TREE_PURPOSE (probe))
27199 cp_default_arg_entry entry = {current_class_type, decl};
27200 vec_safe_push (unparsed_funs_with_default_args, entry);
27201 break;
27205 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27206 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27207 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27208 from the parameter-type-list. */
27210 static tree
27211 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27212 tree default_arg, tree parmtype)
27214 cp_token_cache *tokens;
27215 tree parsed_arg;
27216 bool dummy;
27218 if (default_arg == error_mark_node)
27219 return error_mark_node;
27221 /* Push the saved tokens for the default argument onto the parser's
27222 lexer stack. */
27223 tokens = DEFARG_TOKENS (default_arg);
27224 cp_parser_push_lexer_for_tokens (parser, tokens);
27226 start_lambda_scope (decl);
27228 /* Parse the default argument. */
27229 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27230 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27231 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27233 finish_lambda_scope ();
27235 if (parsed_arg == error_mark_node)
27236 cp_parser_skip_to_end_of_statement (parser);
27238 if (!processing_template_decl)
27240 /* In a non-template class, check conversions now. In a template,
27241 we'll wait and instantiate these as needed. */
27242 if (TREE_CODE (decl) == PARM_DECL)
27243 parsed_arg = check_default_argument (parmtype, parsed_arg,
27244 tf_warning_or_error);
27245 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27246 parsed_arg = error_mark_node;
27247 else
27248 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
27251 /* If the token stream has not been completely used up, then
27252 there was extra junk after the end of the default
27253 argument. */
27254 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27256 if (TREE_CODE (decl) == PARM_DECL)
27257 cp_parser_error (parser, "expected %<,%>");
27258 else
27259 cp_parser_error (parser, "expected %<;%>");
27262 /* Revert to the main lexer. */
27263 cp_parser_pop_lexer (parser);
27265 return parsed_arg;
27268 /* FIELD is a non-static data member with an initializer which we saved for
27269 later; parse it now. */
27271 static void
27272 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27274 tree def;
27276 maybe_begin_member_template_processing (field);
27278 push_unparsed_function_queues (parser);
27279 def = cp_parser_late_parse_one_default_arg (parser, field,
27280 DECL_INITIAL (field),
27281 NULL_TREE);
27282 pop_unparsed_function_queues (parser);
27284 maybe_end_member_template_processing ();
27286 DECL_INITIAL (field) = def;
27289 /* FN is a FUNCTION_DECL which may contains a parameter with an
27290 unparsed DEFAULT_ARG. Parse the default args now. This function
27291 assumes that the current scope is the scope in which the default
27292 argument should be processed. */
27294 static void
27295 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27297 bool saved_local_variables_forbidden_p;
27298 tree parm, parmdecl;
27300 /* While we're parsing the default args, we might (due to the
27301 statement expression extension) encounter more classes. We want
27302 to handle them right away, but we don't want them getting mixed
27303 up with default args that are currently in the queue. */
27304 push_unparsed_function_queues (parser);
27306 /* Local variable names (and the `this' keyword) may not appear
27307 in a default argument. */
27308 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27309 parser->local_variables_forbidden_p = true;
27311 push_defarg_context (fn);
27313 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27314 parmdecl = DECL_ARGUMENTS (fn);
27315 parm && parm != void_list_node;
27316 parm = TREE_CHAIN (parm),
27317 parmdecl = DECL_CHAIN (parmdecl))
27319 tree default_arg = TREE_PURPOSE (parm);
27320 tree parsed_arg;
27321 vec<tree, va_gc> *insts;
27322 tree copy;
27323 unsigned ix;
27325 if (!default_arg)
27326 continue;
27328 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27329 /* This can happen for a friend declaration for a function
27330 already declared with default arguments. */
27331 continue;
27333 parsed_arg
27334 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27335 default_arg,
27336 TREE_VALUE (parm));
27337 if (parsed_arg == error_mark_node)
27339 continue;
27342 TREE_PURPOSE (parm) = parsed_arg;
27344 /* Update any instantiations we've already created. */
27345 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27346 vec_safe_iterate (insts, ix, &copy); ix++)
27347 TREE_PURPOSE (copy) = parsed_arg;
27350 pop_defarg_context ();
27352 /* Make sure no default arg is missing. */
27353 check_default_args (fn);
27355 /* Restore the state of local_variables_forbidden_p. */
27356 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27358 /* Restore the queue. */
27359 pop_unparsed_function_queues (parser);
27362 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27364 sizeof ... ( identifier )
27366 where the 'sizeof' token has already been consumed. */
27368 static tree
27369 cp_parser_sizeof_pack (cp_parser *parser)
27371 /* Consume the `...'. */
27372 cp_lexer_consume_token (parser->lexer);
27373 maybe_warn_variadic_templates ();
27375 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27376 if (paren)
27377 cp_lexer_consume_token (parser->lexer);
27378 else
27379 permerror (cp_lexer_peek_token (parser->lexer)->location,
27380 "%<sizeof...%> argument must be surrounded by parentheses");
27382 cp_token *token = cp_lexer_peek_token (parser->lexer);
27383 tree name = cp_parser_identifier (parser);
27384 if (name == error_mark_node)
27385 return error_mark_node;
27386 /* The name is not qualified. */
27387 parser->scope = NULL_TREE;
27388 parser->qualifying_scope = NULL_TREE;
27389 parser->object_scope = NULL_TREE;
27390 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27391 if (expr == error_mark_node)
27392 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27393 token->location);
27394 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27395 expr = TREE_TYPE (expr);
27396 else if (TREE_CODE (expr) == CONST_DECL)
27397 expr = DECL_INITIAL (expr);
27398 expr = make_pack_expansion (expr);
27399 PACK_EXPANSION_SIZEOF_P (expr) = true;
27401 if (paren)
27402 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27404 return expr;
27407 /* Parse the operand of `sizeof' (or a similar operator). Returns
27408 either a TYPE or an expression, depending on the form of the
27409 input. The KEYWORD indicates which kind of expression we have
27410 encountered. */
27412 static tree
27413 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27415 tree expr = NULL_TREE;
27416 const char *saved_message;
27417 char *tmp;
27418 bool saved_integral_constant_expression_p;
27419 bool saved_non_integral_constant_expression_p;
27421 /* If it's a `...', then we are computing the length of a parameter
27422 pack. */
27423 if (keyword == RID_SIZEOF
27424 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27425 return cp_parser_sizeof_pack (parser);
27427 /* Types cannot be defined in a `sizeof' expression. Save away the
27428 old message. */
27429 saved_message = parser->type_definition_forbidden_message;
27430 /* And create the new one. */
27431 tmp = concat ("types may not be defined in %<",
27432 IDENTIFIER_POINTER (ridpointers[keyword]),
27433 "%> expressions", NULL);
27434 parser->type_definition_forbidden_message = tmp;
27436 /* The restrictions on constant-expressions do not apply inside
27437 sizeof expressions. */
27438 saved_integral_constant_expression_p
27439 = parser->integral_constant_expression_p;
27440 saved_non_integral_constant_expression_p
27441 = parser->non_integral_constant_expression_p;
27442 parser->integral_constant_expression_p = false;
27444 /* Do not actually evaluate the expression. */
27445 ++cp_unevaluated_operand;
27446 ++c_inhibit_evaluation_warnings;
27447 /* If it's a `(', then we might be looking at the type-id
27448 construction. */
27449 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27451 tree type = NULL_TREE;
27453 /* We can't be sure yet whether we're looking at a type-id or an
27454 expression. */
27455 cp_parser_parse_tentatively (parser);
27456 /* Note: as a GNU Extension, compound literals are considered
27457 postfix-expressions as they are in C99, so they are valid
27458 arguments to sizeof. See comment in cp_parser_cast_expression
27459 for details. */
27460 if (cp_parser_compound_literal_p (parser))
27461 cp_parser_simulate_error (parser);
27462 else
27464 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27465 parser->in_type_id_in_expr_p = true;
27466 /* Look for the type-id. */
27467 type = cp_parser_type_id (parser);
27468 /* Look for the closing `)'. */
27469 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27470 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27473 /* If all went well, then we're done. */
27474 if (cp_parser_parse_definitely (parser))
27476 cp_decl_specifier_seq decl_specs;
27478 /* Build a trivial decl-specifier-seq. */
27479 clear_decl_specs (&decl_specs);
27480 decl_specs.type = type;
27482 /* Call grokdeclarator to figure out what type this is. */
27483 expr = grokdeclarator (NULL,
27484 &decl_specs,
27485 TYPENAME,
27486 /*initialized=*/0,
27487 /*attrlist=*/NULL);
27491 /* If the type-id production did not work out, then we must be
27492 looking at the unary-expression production. */
27493 if (!expr)
27494 expr = cp_parser_unary_expression (parser);
27496 /* Go back to evaluating expressions. */
27497 --cp_unevaluated_operand;
27498 --c_inhibit_evaluation_warnings;
27500 /* Free the message we created. */
27501 free (tmp);
27502 /* And restore the old one. */
27503 parser->type_definition_forbidden_message = saved_message;
27504 parser->integral_constant_expression_p
27505 = saved_integral_constant_expression_p;
27506 parser->non_integral_constant_expression_p
27507 = saved_non_integral_constant_expression_p;
27509 return expr;
27512 /* If the current declaration has no declarator, return true. */
27514 static bool
27515 cp_parser_declares_only_class_p (cp_parser *parser)
27517 /* If the next token is a `;' or a `,' then there is no
27518 declarator. */
27519 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27520 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27523 /* Update the DECL_SPECS to reflect the storage class indicated by
27524 KEYWORD. */
27526 static void
27527 cp_parser_set_storage_class (cp_parser *parser,
27528 cp_decl_specifier_seq *decl_specs,
27529 enum rid keyword,
27530 cp_token *token)
27532 cp_storage_class storage_class;
27534 if (parser->in_unbraced_linkage_specification_p)
27536 error_at (token->location, "invalid use of %qD in linkage specification",
27537 ridpointers[keyword]);
27538 return;
27540 else if (decl_specs->storage_class != sc_none)
27542 decl_specs->conflicting_specifiers_p = true;
27543 return;
27546 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27547 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27548 && decl_specs->gnu_thread_keyword_p)
27550 pedwarn (decl_specs->locations[ds_thread], 0,
27551 "%<__thread%> before %qD", ridpointers[keyword]);
27554 switch (keyword)
27556 case RID_AUTO:
27557 storage_class = sc_auto;
27558 break;
27559 case RID_REGISTER:
27560 storage_class = sc_register;
27561 break;
27562 case RID_STATIC:
27563 storage_class = sc_static;
27564 break;
27565 case RID_EXTERN:
27566 storage_class = sc_extern;
27567 break;
27568 case RID_MUTABLE:
27569 storage_class = sc_mutable;
27570 break;
27571 default:
27572 gcc_unreachable ();
27574 decl_specs->storage_class = storage_class;
27575 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27577 /* A storage class specifier cannot be applied alongside a typedef
27578 specifier. If there is a typedef specifier present then set
27579 conflicting_specifiers_p which will trigger an error later
27580 on in grokdeclarator. */
27581 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27582 decl_specs->conflicting_specifiers_p = true;
27585 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27586 is true, the type is a class or enum definition. */
27588 static void
27589 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27590 tree type_spec,
27591 cp_token *token,
27592 bool type_definition_p)
27594 decl_specs->any_specifiers_p = true;
27596 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27597 (with, for example, in "typedef int wchar_t;") we remember that
27598 this is what happened. In system headers, we ignore these
27599 declarations so that G++ can work with system headers that are not
27600 C++-safe. */
27601 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27602 && !type_definition_p
27603 && (type_spec == boolean_type_node
27604 || type_spec == char16_type_node
27605 || type_spec == char32_type_node
27606 || type_spec == wchar_type_node)
27607 && (decl_specs->type
27608 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27609 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27610 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27611 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27613 decl_specs->redefined_builtin_type = type_spec;
27614 set_and_check_decl_spec_loc (decl_specs,
27615 ds_redefined_builtin_type_spec,
27616 token);
27617 if (!decl_specs->type)
27619 decl_specs->type = type_spec;
27620 decl_specs->type_definition_p = false;
27621 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27624 else if (decl_specs->type)
27625 decl_specs->multiple_types_p = true;
27626 else
27628 decl_specs->type = type_spec;
27629 decl_specs->type_definition_p = type_definition_p;
27630 decl_specs->redefined_builtin_type = NULL_TREE;
27631 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27635 /* True iff TOKEN is the GNU keyword __thread. */
27637 static bool
27638 token_is__thread (cp_token *token)
27640 gcc_assert (token->keyword == RID_THREAD);
27641 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
27644 /* Set the location for a declarator specifier and check if it is
27645 duplicated.
27647 DECL_SPECS is the sequence of declarator specifiers onto which to
27648 set the location.
27650 DS is the single declarator specifier to set which location is to
27651 be set onto the existing sequence of declarators.
27653 LOCATION is the location for the declarator specifier to
27654 consider. */
27656 static void
27657 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27658 cp_decl_spec ds, cp_token *token)
27660 gcc_assert (ds < ds_last);
27662 if (decl_specs == NULL)
27663 return;
27665 source_location location = token->location;
27667 if (decl_specs->locations[ds] == 0)
27669 decl_specs->locations[ds] = location;
27670 if (ds == ds_thread)
27671 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27673 else
27675 if (ds == ds_long)
27677 if (decl_specs->locations[ds_long_long] != 0)
27678 error_at (location,
27679 "%<long long long%> is too long for GCC");
27680 else
27682 decl_specs->locations[ds_long_long] = location;
27683 pedwarn_cxx98 (location,
27684 OPT_Wlong_long,
27685 "ISO C++ 1998 does not support %<long long%>");
27688 else if (ds == ds_thread)
27690 bool gnu = token_is__thread (token);
27691 if (gnu != decl_specs->gnu_thread_keyword_p)
27692 error_at (location,
27693 "both %<__thread%> and %<thread_local%> specified");
27694 else
27696 gcc_rich_location richloc (location);
27697 richloc.add_fixit_remove ();
27698 error_at_rich_loc (&richloc, "duplicate %qD", token->u.value);
27701 else
27703 static const char *const decl_spec_names[] = {
27704 "signed",
27705 "unsigned",
27706 "short",
27707 "long",
27708 "const",
27709 "volatile",
27710 "restrict",
27711 "inline",
27712 "virtual",
27713 "explicit",
27714 "friend",
27715 "typedef",
27716 "using",
27717 "constexpr",
27718 "__complex"
27720 gcc_rich_location richloc (location);
27721 richloc.add_fixit_remove ();
27722 error_at_rich_loc (&richloc, "duplicate %qs", decl_spec_names[ds]);
27727 /* Return true iff the declarator specifier DS is present in the
27728 sequence of declarator specifiers DECL_SPECS. */
27730 bool
27731 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27732 cp_decl_spec ds)
27734 gcc_assert (ds < ds_last);
27736 if (decl_specs == NULL)
27737 return false;
27739 return decl_specs->locations[ds] != 0;
27742 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27743 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27745 static bool
27746 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27748 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27751 /* Issue an error message indicating that TOKEN_DESC was expected.
27752 If KEYWORD is true, it indicated this function is called by
27753 cp_parser_require_keword and the required token can only be
27754 a indicated keyword. */
27756 static void
27757 cp_parser_required_error (cp_parser *parser,
27758 required_token token_desc,
27759 bool keyword)
27761 switch (token_desc)
27763 case RT_NEW:
27764 cp_parser_error (parser, "expected %<new%>");
27765 return;
27766 case RT_DELETE:
27767 cp_parser_error (parser, "expected %<delete%>");
27768 return;
27769 case RT_RETURN:
27770 cp_parser_error (parser, "expected %<return%>");
27771 return;
27772 case RT_WHILE:
27773 cp_parser_error (parser, "expected %<while%>");
27774 return;
27775 case RT_EXTERN:
27776 cp_parser_error (parser, "expected %<extern%>");
27777 return;
27778 case RT_STATIC_ASSERT:
27779 cp_parser_error (parser, "expected %<static_assert%>");
27780 return;
27781 case RT_DECLTYPE:
27782 cp_parser_error (parser, "expected %<decltype%>");
27783 return;
27784 case RT_OPERATOR:
27785 cp_parser_error (parser, "expected %<operator%>");
27786 return;
27787 case RT_CLASS:
27788 cp_parser_error (parser, "expected %<class%>");
27789 return;
27790 case RT_TEMPLATE:
27791 cp_parser_error (parser, "expected %<template%>");
27792 return;
27793 case RT_NAMESPACE:
27794 cp_parser_error (parser, "expected %<namespace%>");
27795 return;
27796 case RT_USING:
27797 cp_parser_error (parser, "expected %<using%>");
27798 return;
27799 case RT_ASM:
27800 cp_parser_error (parser, "expected %<asm%>");
27801 return;
27802 case RT_TRY:
27803 cp_parser_error (parser, "expected %<try%>");
27804 return;
27805 case RT_CATCH:
27806 cp_parser_error (parser, "expected %<catch%>");
27807 return;
27808 case RT_THROW:
27809 cp_parser_error (parser, "expected %<throw%>");
27810 return;
27811 case RT_LABEL:
27812 cp_parser_error (parser, "expected %<__label__%>");
27813 return;
27814 case RT_AT_TRY:
27815 cp_parser_error (parser, "expected %<@try%>");
27816 return;
27817 case RT_AT_SYNCHRONIZED:
27818 cp_parser_error (parser, "expected %<@synchronized%>");
27819 return;
27820 case RT_AT_THROW:
27821 cp_parser_error (parser, "expected %<@throw%>");
27822 return;
27823 case RT_TRANSACTION_ATOMIC:
27824 cp_parser_error (parser, "expected %<__transaction_atomic%>");
27825 return;
27826 case RT_TRANSACTION_RELAXED:
27827 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
27828 return;
27829 default:
27830 break;
27832 if (!keyword)
27834 switch (token_desc)
27836 case RT_SEMICOLON:
27837 cp_parser_error (parser, "expected %<;%>");
27838 return;
27839 case RT_OPEN_PAREN:
27840 cp_parser_error (parser, "expected %<(%>");
27841 return;
27842 case RT_CLOSE_BRACE:
27843 cp_parser_error (parser, "expected %<}%>");
27844 return;
27845 case RT_OPEN_BRACE:
27846 cp_parser_error (parser, "expected %<{%>");
27847 return;
27848 case RT_CLOSE_SQUARE:
27849 cp_parser_error (parser, "expected %<]%>");
27850 return;
27851 case RT_OPEN_SQUARE:
27852 cp_parser_error (parser, "expected %<[%>");
27853 return;
27854 case RT_COMMA:
27855 cp_parser_error (parser, "expected %<,%>");
27856 return;
27857 case RT_SCOPE:
27858 cp_parser_error (parser, "expected %<::%>");
27859 return;
27860 case RT_LESS:
27861 cp_parser_error (parser, "expected %<<%>");
27862 return;
27863 case RT_GREATER:
27864 cp_parser_error (parser, "expected %<>%>");
27865 return;
27866 case RT_EQ:
27867 cp_parser_error (parser, "expected %<=%>");
27868 return;
27869 case RT_ELLIPSIS:
27870 cp_parser_error (parser, "expected %<...%>");
27871 return;
27872 case RT_MULT:
27873 cp_parser_error (parser, "expected %<*%>");
27874 return;
27875 case RT_COMPL:
27876 cp_parser_error (parser, "expected %<~%>");
27877 return;
27878 case RT_COLON:
27879 cp_parser_error (parser, "expected %<:%>");
27880 return;
27881 case RT_COLON_SCOPE:
27882 cp_parser_error (parser, "expected %<:%> or %<::%>");
27883 return;
27884 case RT_CLOSE_PAREN:
27885 cp_parser_error (parser, "expected %<)%>");
27886 return;
27887 case RT_COMMA_CLOSE_PAREN:
27888 cp_parser_error (parser, "expected %<,%> or %<)%>");
27889 return;
27890 case RT_PRAGMA_EOL:
27891 cp_parser_error (parser, "expected end of line");
27892 return;
27893 case RT_NAME:
27894 cp_parser_error (parser, "expected identifier");
27895 return;
27896 case RT_SELECT:
27897 cp_parser_error (parser, "expected selection-statement");
27898 return;
27899 case RT_INTERATION:
27900 cp_parser_error (parser, "expected iteration-statement");
27901 return;
27902 case RT_JUMP:
27903 cp_parser_error (parser, "expected jump-statement");
27904 return;
27905 case RT_CLASS_KEY:
27906 cp_parser_error (parser, "expected class-key");
27907 return;
27908 case RT_CLASS_TYPENAME_TEMPLATE:
27909 cp_parser_error (parser,
27910 "expected %<class%>, %<typename%>, or %<template%>");
27911 return;
27912 default:
27913 gcc_unreachable ();
27916 else
27917 gcc_unreachable ();
27922 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27923 issue an error message indicating that TOKEN_DESC was expected.
27925 Returns the token consumed, if the token had the appropriate type.
27926 Otherwise, returns NULL. */
27928 static cp_token *
27929 cp_parser_require (cp_parser* parser,
27930 enum cpp_ttype type,
27931 required_token token_desc)
27933 if (cp_lexer_next_token_is (parser->lexer, type))
27934 return cp_lexer_consume_token (parser->lexer);
27935 else
27937 /* Output the MESSAGE -- unless we're parsing tentatively. */
27938 if (!cp_parser_simulate_error (parser))
27939 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27940 return NULL;
27944 /* An error message is produced if the next token is not '>'.
27945 All further tokens are skipped until the desired token is
27946 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27948 static void
27949 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27951 /* Current level of '< ... >'. */
27952 unsigned level = 0;
27953 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27954 unsigned nesting_depth = 0;
27956 /* Are we ready, yet? If not, issue error message. */
27957 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27958 return;
27960 /* Skip tokens until the desired token is found. */
27961 while (true)
27963 /* Peek at the next token. */
27964 switch (cp_lexer_peek_token (parser->lexer)->type)
27966 case CPP_LESS:
27967 if (!nesting_depth)
27968 ++level;
27969 break;
27971 case CPP_RSHIFT:
27972 if (cxx_dialect == cxx98)
27973 /* C++0x views the `>>' operator as two `>' tokens, but
27974 C++98 does not. */
27975 break;
27976 else if (!nesting_depth && level-- == 0)
27978 /* We've hit a `>>' where the first `>' closes the
27979 template argument list, and the second `>' is
27980 spurious. Just consume the `>>' and stop; we've
27981 already produced at least one error. */
27982 cp_lexer_consume_token (parser->lexer);
27983 return;
27985 /* Fall through for C++0x, so we handle the second `>' in
27986 the `>>'. */
27987 gcc_fallthrough ();
27989 case CPP_GREATER:
27990 if (!nesting_depth && level-- == 0)
27992 /* We've reached the token we want, consume it and stop. */
27993 cp_lexer_consume_token (parser->lexer);
27994 return;
27996 break;
27998 case CPP_OPEN_PAREN:
27999 case CPP_OPEN_SQUARE:
28000 ++nesting_depth;
28001 break;
28003 case CPP_CLOSE_PAREN:
28004 case CPP_CLOSE_SQUARE:
28005 if (nesting_depth-- == 0)
28006 return;
28007 break;
28009 case CPP_EOF:
28010 case CPP_PRAGMA_EOL:
28011 case CPP_SEMICOLON:
28012 case CPP_OPEN_BRACE:
28013 case CPP_CLOSE_BRACE:
28014 /* The '>' was probably forgotten, don't look further. */
28015 return;
28017 default:
28018 break;
28021 /* Consume this token. */
28022 cp_lexer_consume_token (parser->lexer);
28026 /* If the next token is the indicated keyword, consume it. Otherwise,
28027 issue an error message indicating that TOKEN_DESC was expected.
28029 Returns the token consumed, if the token had the appropriate type.
28030 Otherwise, returns NULL. */
28032 static cp_token *
28033 cp_parser_require_keyword (cp_parser* parser,
28034 enum rid keyword,
28035 required_token token_desc)
28037 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28039 if (token && token->keyword != keyword)
28041 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
28042 return NULL;
28045 return token;
28048 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28049 function-definition. */
28051 static bool
28052 cp_parser_token_starts_function_definition_p (cp_token* token)
28054 return (/* An ordinary function-body begins with an `{'. */
28055 token->type == CPP_OPEN_BRACE
28056 /* A ctor-initializer begins with a `:'. */
28057 || token->type == CPP_COLON
28058 /* A function-try-block begins with `try'. */
28059 || token->keyword == RID_TRY
28060 /* A function-transaction-block begins with `__transaction_atomic'
28061 or `__transaction_relaxed'. */
28062 || token->keyword == RID_TRANSACTION_ATOMIC
28063 || token->keyword == RID_TRANSACTION_RELAXED
28064 /* The named return value extension begins with `return'. */
28065 || token->keyword == RID_RETURN);
28068 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28069 definition. */
28071 static bool
28072 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28074 cp_token *token;
28076 token = cp_lexer_peek_token (parser->lexer);
28077 return (token->type == CPP_OPEN_BRACE
28078 || (token->type == CPP_COLON
28079 && !parser->colon_doesnt_start_class_def_p));
28082 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28083 C++0x) ending a template-argument. */
28085 static bool
28086 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28088 cp_token *token;
28090 token = cp_lexer_peek_token (parser->lexer);
28091 return (token->type == CPP_COMMA
28092 || token->type == CPP_GREATER
28093 || token->type == CPP_ELLIPSIS
28094 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28097 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28098 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28100 static bool
28101 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28102 size_t n)
28104 cp_token *token;
28106 token = cp_lexer_peek_nth_token (parser->lexer, n);
28107 if (token->type == CPP_LESS)
28108 return true;
28109 /* Check for the sequence `<::' in the original code. It would be lexed as
28110 `[:', where `[' is a digraph, and there is no whitespace before
28111 `:'. */
28112 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28114 cp_token *token2;
28115 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28116 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28117 return true;
28119 return false;
28122 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28123 or none_type otherwise. */
28125 static enum tag_types
28126 cp_parser_token_is_class_key (cp_token* token)
28128 switch (token->keyword)
28130 case RID_CLASS:
28131 return class_type;
28132 case RID_STRUCT:
28133 return record_type;
28134 case RID_UNION:
28135 return union_type;
28137 default:
28138 return none_type;
28142 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28143 or none_type otherwise or if the token is null. */
28145 static enum tag_types
28146 cp_parser_token_is_type_parameter_key (cp_token* token)
28148 if (!token)
28149 return none_type;
28151 switch (token->keyword)
28153 case RID_CLASS:
28154 return class_type;
28155 case RID_TYPENAME:
28156 return typename_type;
28158 default:
28159 return none_type;
28163 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28165 static void
28166 cp_parser_check_class_key (enum tag_types class_key, tree type)
28168 if (type == error_mark_node)
28169 return;
28170 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28172 if (permerror (input_location, "%qs tag used in naming %q#T",
28173 class_key == union_type ? "union"
28174 : class_key == record_type ? "struct" : "class",
28175 type))
28176 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28177 "%q#T was previously declared here", type);
28181 /* Issue an error message if DECL is redeclared with different
28182 access than its original declaration [class.access.spec/3].
28183 This applies to nested classes, nested class templates and
28184 enumerations [class.mem/1]. */
28186 static void
28187 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28189 if (!decl
28190 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28191 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28192 return;
28194 if ((TREE_PRIVATE (decl)
28195 != (current_access_specifier == access_private_node))
28196 || (TREE_PROTECTED (decl)
28197 != (current_access_specifier == access_protected_node)))
28198 error_at (location, "%qD redeclared with different access", decl);
28201 /* Look for the `template' keyword, as a syntactic disambiguator.
28202 Return TRUE iff it is present, in which case it will be
28203 consumed. */
28205 static bool
28206 cp_parser_optional_template_keyword (cp_parser *parser)
28208 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28210 /* In C++98 the `template' keyword can only be used within templates;
28211 outside templates the parser can always figure out what is a
28212 template and what is not. In C++11, per the resolution of DR 468,
28213 `template' is allowed in cases where it is not strictly necessary. */
28214 if (!processing_template_decl
28215 && pedantic && cxx_dialect == cxx98)
28217 cp_token *token = cp_lexer_peek_token (parser->lexer);
28218 pedwarn (token->location, OPT_Wpedantic,
28219 "in C++98 %<template%> (as a disambiguator) is only "
28220 "allowed within templates");
28221 /* If this part of the token stream is rescanned, the same
28222 error message would be generated. So, we purge the token
28223 from the stream. */
28224 cp_lexer_purge_token (parser->lexer);
28225 return false;
28227 else
28229 /* Consume the `template' keyword. */
28230 cp_lexer_consume_token (parser->lexer);
28231 return true;
28234 return false;
28237 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28238 set PARSER->SCOPE, and perform other related actions. */
28240 static void
28241 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28243 struct tree_check *check_value;
28245 /* Get the stored value. */
28246 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28247 /* Set the scope from the stored value. */
28248 parser->scope = saved_checks_value (check_value);
28249 parser->qualifying_scope = check_value->qualifying_scope;
28250 parser->object_scope = NULL_TREE;
28253 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28254 encounter the end of a block before what we were looking for. */
28256 static bool
28257 cp_parser_cache_group (cp_parser *parser,
28258 enum cpp_ttype end,
28259 unsigned depth)
28261 while (true)
28263 cp_token *token = cp_lexer_peek_token (parser->lexer);
28265 /* Abort a parenthesized expression if we encounter a semicolon. */
28266 if ((end == CPP_CLOSE_PAREN || depth == 0)
28267 && token->type == CPP_SEMICOLON)
28268 return true;
28269 /* If we've reached the end of the file, stop. */
28270 if (token->type == CPP_EOF
28271 || (end != CPP_PRAGMA_EOL
28272 && token->type == CPP_PRAGMA_EOL))
28273 return true;
28274 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28275 /* We've hit the end of an enclosing block, so there's been some
28276 kind of syntax error. */
28277 return true;
28279 /* Consume the token. */
28280 cp_lexer_consume_token (parser->lexer);
28281 /* See if it starts a new group. */
28282 if (token->type == CPP_OPEN_BRACE)
28284 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28285 /* In theory this should probably check end == '}', but
28286 cp_parser_save_member_function_body needs it to exit
28287 after either '}' or ')' when called with ')'. */
28288 if (depth == 0)
28289 return false;
28291 else if (token->type == CPP_OPEN_PAREN)
28293 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28294 if (depth == 0 && end == CPP_CLOSE_PAREN)
28295 return false;
28297 else if (token->type == CPP_PRAGMA)
28298 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28299 else if (token->type == end)
28300 return false;
28304 /* Like above, for caching a default argument or NSDMI. Both of these are
28305 terminated by a non-nested comma, but it can be unclear whether or not a
28306 comma is nested in a template argument list unless we do more parsing.
28307 In order to handle this ambiguity, when we encounter a ',' after a '<'
28308 we try to parse what follows as a parameter-declaration-list (in the
28309 case of a default argument) or a member-declarator (in the case of an
28310 NSDMI). If that succeeds, then we stop caching. */
28312 static tree
28313 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28315 unsigned depth = 0;
28316 int maybe_template_id = 0;
28317 cp_token *first_token;
28318 cp_token *token;
28319 tree default_argument;
28321 /* Add tokens until we have processed the entire default
28322 argument. We add the range [first_token, token). */
28323 first_token = cp_lexer_peek_token (parser->lexer);
28324 if (first_token->type == CPP_OPEN_BRACE)
28326 /* For list-initialization, this is straightforward. */
28327 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28328 token = cp_lexer_peek_token (parser->lexer);
28330 else while (true)
28332 bool done = false;
28334 /* Peek at the next token. */
28335 token = cp_lexer_peek_token (parser->lexer);
28336 /* What we do depends on what token we have. */
28337 switch (token->type)
28339 /* In valid code, a default argument must be
28340 immediately followed by a `,' `)', or `...'. */
28341 case CPP_COMMA:
28342 if (depth == 0 && maybe_template_id)
28344 /* If we've seen a '<', we might be in a
28345 template-argument-list. Until Core issue 325 is
28346 resolved, we don't know how this situation ought
28347 to be handled, so try to DTRT. We check whether
28348 what comes after the comma is a valid parameter
28349 declaration list. If it is, then the comma ends
28350 the default argument; otherwise the default
28351 argument continues. */
28352 bool error = false;
28353 cp_token *peek;
28355 /* Set ITALP so cp_parser_parameter_declaration_list
28356 doesn't decide to commit to this parse. */
28357 bool saved_italp = parser->in_template_argument_list_p;
28358 parser->in_template_argument_list_p = true;
28360 cp_parser_parse_tentatively (parser);
28362 if (nsdmi)
28364 /* Parse declarators until we reach a non-comma or
28365 somthing that cannot be an initializer.
28366 Just checking whether we're looking at a single
28367 declarator is insufficient. Consider:
28368 int var = tuple<T,U>::x;
28369 The template parameter 'U' looks exactly like a
28370 declarator. */
28373 int ctor_dtor_or_conv_p;
28374 cp_lexer_consume_token (parser->lexer);
28375 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28376 &ctor_dtor_or_conv_p,
28377 /*parenthesized_p=*/NULL,
28378 /*member_p=*/true,
28379 /*friend_p=*/false);
28380 peek = cp_lexer_peek_token (parser->lexer);
28381 if (cp_parser_error_occurred (parser))
28382 break;
28384 while (peek->type == CPP_COMMA);
28385 /* If we met an '=' or ';' then the original comma
28386 was the end of the NSDMI. Otherwise assume
28387 we're still in the NSDMI. */
28388 error = (peek->type != CPP_EQ
28389 && peek->type != CPP_SEMICOLON);
28391 else
28393 cp_lexer_consume_token (parser->lexer);
28394 begin_scope (sk_function_parms, NULL_TREE);
28395 cp_parser_parameter_declaration_list (parser, &error);
28396 pop_bindings_and_leave_scope ();
28398 if (!cp_parser_error_occurred (parser) && !error)
28399 done = true;
28400 cp_parser_abort_tentative_parse (parser);
28402 parser->in_template_argument_list_p = saved_italp;
28403 break;
28405 /* FALLTHRU */
28406 case CPP_CLOSE_PAREN:
28407 case CPP_ELLIPSIS:
28408 /* If we run into a non-nested `;', `}', or `]',
28409 then the code is invalid -- but the default
28410 argument is certainly over. */
28411 case CPP_SEMICOLON:
28412 case CPP_CLOSE_BRACE:
28413 case CPP_CLOSE_SQUARE:
28414 if (depth == 0
28415 /* Handle correctly int n = sizeof ... ( p ); */
28416 && token->type != CPP_ELLIPSIS)
28417 done = true;
28418 /* Update DEPTH, if necessary. */
28419 else if (token->type == CPP_CLOSE_PAREN
28420 || token->type == CPP_CLOSE_BRACE
28421 || token->type == CPP_CLOSE_SQUARE)
28422 --depth;
28423 break;
28425 case CPP_OPEN_PAREN:
28426 case CPP_OPEN_SQUARE:
28427 case CPP_OPEN_BRACE:
28428 ++depth;
28429 break;
28431 case CPP_LESS:
28432 if (depth == 0)
28433 /* This might be the comparison operator, or it might
28434 start a template argument list. */
28435 ++maybe_template_id;
28436 break;
28438 case CPP_RSHIFT:
28439 if (cxx_dialect == cxx98)
28440 break;
28441 /* Fall through for C++0x, which treats the `>>'
28442 operator like two `>' tokens in certain
28443 cases. */
28444 gcc_fallthrough ();
28446 case CPP_GREATER:
28447 if (depth == 0)
28449 /* This might be an operator, or it might close a
28450 template argument list. But if a previous '<'
28451 started a template argument list, this will have
28452 closed it, so we can't be in one anymore. */
28453 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28454 if (maybe_template_id < 0)
28455 maybe_template_id = 0;
28457 break;
28459 /* If we run out of tokens, issue an error message. */
28460 case CPP_EOF:
28461 case CPP_PRAGMA_EOL:
28462 error_at (token->location, "file ends in default argument");
28463 return error_mark_node;
28465 case CPP_NAME:
28466 case CPP_SCOPE:
28467 /* In these cases, we should look for template-ids.
28468 For example, if the default argument is
28469 `X<int, double>()', we need to do name lookup to
28470 figure out whether or not `X' is a template; if
28471 so, the `,' does not end the default argument.
28473 That is not yet done. */
28474 break;
28476 default:
28477 break;
28480 /* If we've reached the end, stop. */
28481 if (done)
28482 break;
28484 /* Add the token to the token block. */
28485 token = cp_lexer_consume_token (parser->lexer);
28488 /* Create a DEFAULT_ARG to represent the unparsed default
28489 argument. */
28490 default_argument = make_node (DEFAULT_ARG);
28491 DEFARG_TOKENS (default_argument)
28492 = cp_token_cache_new (first_token, token);
28493 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28495 return default_argument;
28498 /* Begin parsing tentatively. We always save tokens while parsing
28499 tentatively so that if the tentative parsing fails we can restore the
28500 tokens. */
28502 static void
28503 cp_parser_parse_tentatively (cp_parser* parser)
28505 /* Enter a new parsing context. */
28506 parser->context = cp_parser_context_new (parser->context);
28507 /* Begin saving tokens. */
28508 cp_lexer_save_tokens (parser->lexer);
28509 /* In order to avoid repetitive access control error messages,
28510 access checks are queued up until we are no longer parsing
28511 tentatively. */
28512 push_deferring_access_checks (dk_deferred);
28515 /* Commit to the currently active tentative parse. */
28517 static void
28518 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28520 cp_parser_context *context;
28521 cp_lexer *lexer;
28523 /* Mark all of the levels as committed. */
28524 lexer = parser->lexer;
28525 for (context = parser->context; context->next; context = context->next)
28527 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28528 break;
28529 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28530 while (!cp_lexer_saving_tokens (lexer))
28531 lexer = lexer->next;
28532 cp_lexer_commit_tokens (lexer);
28536 /* Commit to the topmost currently active tentative parse.
28538 Note that this function shouldn't be called when there are
28539 irreversible side-effects while in a tentative state. For
28540 example, we shouldn't create a permanent entry in the symbol
28541 table, or issue an error message that might not apply if the
28542 tentative parse is aborted. */
28544 static void
28545 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28547 cp_parser_context *context = parser->context;
28548 cp_lexer *lexer = parser->lexer;
28550 if (context)
28552 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28553 return;
28554 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28556 while (!cp_lexer_saving_tokens (lexer))
28557 lexer = lexer->next;
28558 cp_lexer_commit_tokens (lexer);
28562 /* Abort the currently active tentative parse. All consumed tokens
28563 will be rolled back, and no diagnostics will be issued. */
28565 static void
28566 cp_parser_abort_tentative_parse (cp_parser* parser)
28568 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28569 || errorcount > 0);
28570 cp_parser_simulate_error (parser);
28571 /* Now, pretend that we want to see if the construct was
28572 successfully parsed. */
28573 cp_parser_parse_definitely (parser);
28576 /* Stop parsing tentatively. If a parse error has occurred, restore the
28577 token stream. Otherwise, commit to the tokens we have consumed.
28578 Returns true if no error occurred; false otherwise. */
28580 static bool
28581 cp_parser_parse_definitely (cp_parser* parser)
28583 bool error_occurred;
28584 cp_parser_context *context;
28586 /* Remember whether or not an error occurred, since we are about to
28587 destroy that information. */
28588 error_occurred = cp_parser_error_occurred (parser);
28589 /* Remove the topmost context from the stack. */
28590 context = parser->context;
28591 parser->context = context->next;
28592 /* If no parse errors occurred, commit to the tentative parse. */
28593 if (!error_occurred)
28595 /* Commit to the tokens read tentatively, unless that was
28596 already done. */
28597 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28598 cp_lexer_commit_tokens (parser->lexer);
28600 pop_to_parent_deferring_access_checks ();
28602 /* Otherwise, if errors occurred, roll back our state so that things
28603 are just as they were before we began the tentative parse. */
28604 else
28606 cp_lexer_rollback_tokens (parser->lexer);
28607 pop_deferring_access_checks ();
28609 /* Add the context to the front of the free list. */
28610 context->next = cp_parser_context_free_list;
28611 cp_parser_context_free_list = context;
28613 return !error_occurred;
28616 /* Returns true if we are parsing tentatively and are not committed to
28617 this tentative parse. */
28619 static bool
28620 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28622 return (cp_parser_parsing_tentatively (parser)
28623 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28626 /* Returns nonzero iff an error has occurred during the most recent
28627 tentative parse. */
28629 static bool
28630 cp_parser_error_occurred (cp_parser* parser)
28632 return (cp_parser_parsing_tentatively (parser)
28633 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28636 /* Returns nonzero if GNU extensions are allowed. */
28638 static bool
28639 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28641 return parser->allow_gnu_extensions_p;
28644 /* Objective-C++ Productions */
28647 /* Parse an Objective-C expression, which feeds into a primary-expression
28648 above.
28650 objc-expression:
28651 objc-message-expression
28652 objc-string-literal
28653 objc-encode-expression
28654 objc-protocol-expression
28655 objc-selector-expression
28657 Returns a tree representation of the expression. */
28659 static cp_expr
28660 cp_parser_objc_expression (cp_parser* parser)
28662 /* Try to figure out what kind of declaration is present. */
28663 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28665 switch (kwd->type)
28667 case CPP_OPEN_SQUARE:
28668 return cp_parser_objc_message_expression (parser);
28670 case CPP_OBJC_STRING:
28671 kwd = cp_lexer_consume_token (parser->lexer);
28672 return objc_build_string_object (kwd->u.value);
28674 case CPP_KEYWORD:
28675 switch (kwd->keyword)
28677 case RID_AT_ENCODE:
28678 return cp_parser_objc_encode_expression (parser);
28680 case RID_AT_PROTOCOL:
28681 return cp_parser_objc_protocol_expression (parser);
28683 case RID_AT_SELECTOR:
28684 return cp_parser_objc_selector_expression (parser);
28686 default:
28687 break;
28689 default:
28690 error_at (kwd->location,
28691 "misplaced %<@%D%> Objective-C++ construct",
28692 kwd->u.value);
28693 cp_parser_skip_to_end_of_block_or_statement (parser);
28696 return error_mark_node;
28699 /* Parse an Objective-C message expression.
28701 objc-message-expression:
28702 [ objc-message-receiver objc-message-args ]
28704 Returns a representation of an Objective-C message. */
28706 static tree
28707 cp_parser_objc_message_expression (cp_parser* parser)
28709 tree receiver, messageargs;
28711 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28712 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
28713 receiver = cp_parser_objc_message_receiver (parser);
28714 messageargs = cp_parser_objc_message_args (parser);
28715 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
28716 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28718 tree result = objc_build_message_expr (receiver, messageargs);
28720 /* Construct a location e.g.
28721 [self func1:5]
28722 ^~~~~~~~~~~~~~
28723 ranging from the '[' to the ']', with the caret at the start. */
28724 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
28725 protected_set_expr_location (result, combined_loc);
28727 return result;
28730 /* Parse an objc-message-receiver.
28732 objc-message-receiver:
28733 expression
28734 simple-type-specifier
28736 Returns a representation of the type or expression. */
28738 static tree
28739 cp_parser_objc_message_receiver (cp_parser* parser)
28741 tree rcv;
28743 /* An Objective-C message receiver may be either (1) a type
28744 or (2) an expression. */
28745 cp_parser_parse_tentatively (parser);
28746 rcv = cp_parser_expression (parser);
28748 /* If that worked out, fine. */
28749 if (cp_parser_parse_definitely (parser))
28750 return rcv;
28752 cp_parser_parse_tentatively (parser);
28753 rcv = cp_parser_simple_type_specifier (parser,
28754 /*decl_specs=*/NULL,
28755 CP_PARSER_FLAGS_NONE);
28757 if (cp_parser_parse_definitely (parser))
28758 return objc_get_class_reference (rcv);
28760 cp_parser_error (parser, "objective-c++ message receiver expected");
28761 return error_mark_node;
28764 /* Parse the arguments and selectors comprising an Objective-C message.
28766 objc-message-args:
28767 objc-selector
28768 objc-selector-args
28769 objc-selector-args , objc-comma-args
28771 objc-selector-args:
28772 objc-selector [opt] : assignment-expression
28773 objc-selector-args objc-selector [opt] : assignment-expression
28775 objc-comma-args:
28776 assignment-expression
28777 objc-comma-args , assignment-expression
28779 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28780 selector arguments and TREE_VALUE containing a list of comma
28781 arguments. */
28783 static tree
28784 cp_parser_objc_message_args (cp_parser* parser)
28786 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
28787 bool maybe_unary_selector_p = true;
28788 cp_token *token = cp_lexer_peek_token (parser->lexer);
28790 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28792 tree selector = NULL_TREE, arg;
28794 if (token->type != CPP_COLON)
28795 selector = cp_parser_objc_selector (parser);
28797 /* Detect if we have a unary selector. */
28798 if (maybe_unary_selector_p
28799 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28800 return build_tree_list (selector, NULL_TREE);
28802 maybe_unary_selector_p = false;
28803 cp_parser_require (parser, CPP_COLON, RT_COLON);
28804 arg = cp_parser_assignment_expression (parser);
28806 sel_args
28807 = chainon (sel_args,
28808 build_tree_list (selector, arg));
28810 token = cp_lexer_peek_token (parser->lexer);
28813 /* Handle non-selector arguments, if any. */
28814 while (token->type == CPP_COMMA)
28816 tree arg;
28818 cp_lexer_consume_token (parser->lexer);
28819 arg = cp_parser_assignment_expression (parser);
28821 addl_args
28822 = chainon (addl_args,
28823 build_tree_list (NULL_TREE, arg));
28825 token = cp_lexer_peek_token (parser->lexer);
28828 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
28830 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
28831 return build_tree_list (error_mark_node, error_mark_node);
28834 return build_tree_list (sel_args, addl_args);
28837 /* Parse an Objective-C encode expression.
28839 objc-encode-expression:
28840 @encode objc-typename
28842 Returns an encoded representation of the type argument. */
28844 static cp_expr
28845 cp_parser_objc_encode_expression (cp_parser* parser)
28847 tree type;
28848 cp_token *token;
28849 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28851 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
28852 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28853 token = cp_lexer_peek_token (parser->lexer);
28854 type = complete_type (cp_parser_type_id (parser));
28855 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28857 if (!type)
28859 error_at (token->location,
28860 "%<@encode%> must specify a type as an argument");
28861 return error_mark_node;
28864 /* This happens if we find @encode(T) (where T is a template
28865 typename or something dependent on a template typename) when
28866 parsing a template. In that case, we can't compile it
28867 immediately, but we rather create an AT_ENCODE_EXPR which will
28868 need to be instantiated when the template is used.
28870 if (dependent_type_p (type))
28872 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28873 TREE_READONLY (value) = 1;
28874 return value;
28878 /* Build a location of the form:
28879 @encode(int)
28880 ^~~~~~~~~~~~
28881 with caret==start at the @ token, finishing at the close paren. */
28882 location_t combined_loc
28883 = make_location (start_loc, start_loc,
28884 cp_lexer_previous_token (parser->lexer)->location);
28886 return cp_expr (objc_build_encode_expr (type), combined_loc);
28889 /* Parse an Objective-C @defs expression. */
28891 static tree
28892 cp_parser_objc_defs_expression (cp_parser *parser)
28894 tree name;
28896 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
28897 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28898 name = cp_parser_identifier (parser);
28899 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28901 return objc_get_class_ivars (name);
28904 /* Parse an Objective-C protocol expression.
28906 objc-protocol-expression:
28907 @protocol ( identifier )
28909 Returns a representation of the protocol expression. */
28911 static tree
28912 cp_parser_objc_protocol_expression (cp_parser* parser)
28914 tree proto;
28915 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28917 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28918 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28919 proto = cp_parser_identifier (parser);
28920 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28922 /* Build a location of the form:
28923 @protocol(prot)
28924 ^~~~~~~~~~~~~~~
28925 with caret==start at the @ token, finishing at the close paren. */
28926 location_t combined_loc
28927 = make_location (start_loc, start_loc,
28928 cp_lexer_previous_token (parser->lexer)->location);
28929 tree result = objc_build_protocol_expr (proto);
28930 protected_set_expr_location (result, combined_loc);
28931 return result;
28934 /* Parse an Objective-C selector expression.
28936 objc-selector-expression:
28937 @selector ( objc-method-signature )
28939 objc-method-signature:
28940 objc-selector
28941 objc-selector-seq
28943 objc-selector-seq:
28944 objc-selector :
28945 objc-selector-seq objc-selector :
28947 Returns a representation of the method selector. */
28949 static tree
28950 cp_parser_objc_selector_expression (cp_parser* parser)
28952 tree sel_seq = NULL_TREE;
28953 bool maybe_unary_selector_p = true;
28954 cp_token *token;
28955 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28957 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28958 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28959 token = cp_lexer_peek_token (parser->lexer);
28961 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28962 || token->type == CPP_SCOPE)
28964 tree selector = NULL_TREE;
28966 if (token->type != CPP_COLON
28967 || token->type == CPP_SCOPE)
28968 selector = cp_parser_objc_selector (parser);
28970 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28971 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28973 /* Detect if we have a unary selector. */
28974 if (maybe_unary_selector_p)
28976 sel_seq = selector;
28977 goto finish_selector;
28979 else
28981 cp_parser_error (parser, "expected %<:%>");
28984 maybe_unary_selector_p = false;
28985 token = cp_lexer_consume_token (parser->lexer);
28987 if (token->type == CPP_SCOPE)
28989 sel_seq
28990 = chainon (sel_seq,
28991 build_tree_list (selector, NULL_TREE));
28992 sel_seq
28993 = chainon (sel_seq,
28994 build_tree_list (NULL_TREE, NULL_TREE));
28996 else
28997 sel_seq
28998 = chainon (sel_seq,
28999 build_tree_list (selector, NULL_TREE));
29001 token = cp_lexer_peek_token (parser->lexer);
29004 finish_selector:
29005 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29008 /* Build a location of the form:
29009 @selector(func)
29010 ^~~~~~~~~~~~~~~
29011 with caret==start at the @ token, finishing at the close paren. */
29012 location_t combined_loc
29013 = make_location (loc, loc,
29014 cp_lexer_previous_token (parser->lexer)->location);
29015 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29016 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29017 protected_set_expr_location (result, combined_loc);
29018 return result;
29021 /* Parse a list of identifiers.
29023 objc-identifier-list:
29024 identifier
29025 objc-identifier-list , identifier
29027 Returns a TREE_LIST of identifier nodes. */
29029 static tree
29030 cp_parser_objc_identifier_list (cp_parser* parser)
29032 tree identifier;
29033 tree list;
29034 cp_token *sep;
29036 identifier = cp_parser_identifier (parser);
29037 if (identifier == error_mark_node)
29038 return error_mark_node;
29040 list = build_tree_list (NULL_TREE, identifier);
29041 sep = cp_lexer_peek_token (parser->lexer);
29043 while (sep->type == CPP_COMMA)
29045 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29046 identifier = cp_parser_identifier (parser);
29047 if (identifier == error_mark_node)
29048 return list;
29050 list = chainon (list, build_tree_list (NULL_TREE,
29051 identifier));
29052 sep = cp_lexer_peek_token (parser->lexer);
29055 return list;
29058 /* Parse an Objective-C alias declaration.
29060 objc-alias-declaration:
29061 @compatibility_alias identifier identifier ;
29063 This function registers the alias mapping with the Objective-C front end.
29064 It returns nothing. */
29066 static void
29067 cp_parser_objc_alias_declaration (cp_parser* parser)
29069 tree alias, orig;
29071 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29072 alias = cp_parser_identifier (parser);
29073 orig = cp_parser_identifier (parser);
29074 objc_declare_alias (alias, orig);
29075 cp_parser_consume_semicolon_at_end_of_statement (parser);
29078 /* Parse an Objective-C class forward-declaration.
29080 objc-class-declaration:
29081 @class objc-identifier-list ;
29083 The function registers the forward declarations with the Objective-C
29084 front end. It returns nothing. */
29086 static void
29087 cp_parser_objc_class_declaration (cp_parser* parser)
29089 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29090 while (true)
29092 tree id;
29094 id = cp_parser_identifier (parser);
29095 if (id == error_mark_node)
29096 break;
29098 objc_declare_class (id);
29100 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29101 cp_lexer_consume_token (parser->lexer);
29102 else
29103 break;
29105 cp_parser_consume_semicolon_at_end_of_statement (parser);
29108 /* Parse a list of Objective-C protocol references.
29110 objc-protocol-refs-opt:
29111 objc-protocol-refs [opt]
29113 objc-protocol-refs:
29114 < objc-identifier-list >
29116 Returns a TREE_LIST of identifiers, if any. */
29118 static tree
29119 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29121 tree protorefs = NULL_TREE;
29123 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29125 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29126 protorefs = cp_parser_objc_identifier_list (parser);
29127 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29130 return protorefs;
29133 /* Parse a Objective-C visibility specification. */
29135 static void
29136 cp_parser_objc_visibility_spec (cp_parser* parser)
29138 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29140 switch (vis->keyword)
29142 case RID_AT_PRIVATE:
29143 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29144 break;
29145 case RID_AT_PROTECTED:
29146 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29147 break;
29148 case RID_AT_PUBLIC:
29149 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29150 break;
29151 case RID_AT_PACKAGE:
29152 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29153 break;
29154 default:
29155 return;
29158 /* Eat '@private'/'@protected'/'@public'. */
29159 cp_lexer_consume_token (parser->lexer);
29162 /* Parse an Objective-C method type. Return 'true' if it is a class
29163 (+) method, and 'false' if it is an instance (-) method. */
29165 static inline bool
29166 cp_parser_objc_method_type (cp_parser* parser)
29168 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29169 return true;
29170 else
29171 return false;
29174 /* Parse an Objective-C protocol qualifier. */
29176 static tree
29177 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29179 tree quals = NULL_TREE, node;
29180 cp_token *token = cp_lexer_peek_token (parser->lexer);
29182 node = token->u.value;
29184 while (node && identifier_p (node)
29185 && (node == ridpointers [(int) RID_IN]
29186 || node == ridpointers [(int) RID_OUT]
29187 || node == ridpointers [(int) RID_INOUT]
29188 || node == ridpointers [(int) RID_BYCOPY]
29189 || node == ridpointers [(int) RID_BYREF]
29190 || node == ridpointers [(int) RID_ONEWAY]))
29192 quals = tree_cons (NULL_TREE, node, quals);
29193 cp_lexer_consume_token (parser->lexer);
29194 token = cp_lexer_peek_token (parser->lexer);
29195 node = token->u.value;
29198 return quals;
29201 /* Parse an Objective-C typename. */
29203 static tree
29204 cp_parser_objc_typename (cp_parser* parser)
29206 tree type_name = NULL_TREE;
29208 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29210 tree proto_quals, cp_type = NULL_TREE;
29212 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29213 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29215 /* An ObjC type name may consist of just protocol qualifiers, in which
29216 case the type shall default to 'id'. */
29217 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29219 cp_type = cp_parser_type_id (parser);
29221 /* If the type could not be parsed, an error has already
29222 been produced. For error recovery, behave as if it had
29223 not been specified, which will use the default type
29224 'id'. */
29225 if (cp_type == error_mark_node)
29227 cp_type = NULL_TREE;
29228 /* We need to skip to the closing parenthesis as
29229 cp_parser_type_id() does not seem to do it for
29230 us. */
29231 cp_parser_skip_to_closing_parenthesis (parser,
29232 /*recovering=*/true,
29233 /*or_comma=*/false,
29234 /*consume_paren=*/false);
29238 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29239 type_name = build_tree_list (proto_quals, cp_type);
29242 return type_name;
29245 /* Check to see if TYPE refers to an Objective-C selector name. */
29247 static bool
29248 cp_parser_objc_selector_p (enum cpp_ttype type)
29250 return (type == CPP_NAME || type == CPP_KEYWORD
29251 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29252 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29253 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29254 || type == CPP_XOR || type == CPP_XOR_EQ);
29257 /* Parse an Objective-C selector. */
29259 static tree
29260 cp_parser_objc_selector (cp_parser* parser)
29262 cp_token *token = cp_lexer_consume_token (parser->lexer);
29264 if (!cp_parser_objc_selector_p (token->type))
29266 error_at (token->location, "invalid Objective-C++ selector name");
29267 return error_mark_node;
29270 /* C++ operator names are allowed to appear in ObjC selectors. */
29271 switch (token->type)
29273 case CPP_AND_AND: return get_identifier ("and");
29274 case CPP_AND_EQ: return get_identifier ("and_eq");
29275 case CPP_AND: return get_identifier ("bitand");
29276 case CPP_OR: return get_identifier ("bitor");
29277 case CPP_COMPL: return get_identifier ("compl");
29278 case CPP_NOT: return get_identifier ("not");
29279 case CPP_NOT_EQ: return get_identifier ("not_eq");
29280 case CPP_OR_OR: return get_identifier ("or");
29281 case CPP_OR_EQ: return get_identifier ("or_eq");
29282 case CPP_XOR: return get_identifier ("xor");
29283 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29284 default: return token->u.value;
29288 /* Parse an Objective-C params list. */
29290 static tree
29291 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29293 tree params = NULL_TREE;
29294 bool maybe_unary_selector_p = true;
29295 cp_token *token = cp_lexer_peek_token (parser->lexer);
29297 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29299 tree selector = NULL_TREE, type_name, identifier;
29300 tree parm_attr = NULL_TREE;
29302 if (token->keyword == RID_ATTRIBUTE)
29303 break;
29305 if (token->type != CPP_COLON)
29306 selector = cp_parser_objc_selector (parser);
29308 /* Detect if we have a unary selector. */
29309 if (maybe_unary_selector_p
29310 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29312 params = selector; /* Might be followed by attributes. */
29313 break;
29316 maybe_unary_selector_p = false;
29317 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29319 /* Something went quite wrong. There should be a colon
29320 here, but there is not. Stop parsing parameters. */
29321 break;
29323 type_name = cp_parser_objc_typename (parser);
29324 /* New ObjC allows attributes on parameters too. */
29325 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29326 parm_attr = cp_parser_attributes_opt (parser);
29327 identifier = cp_parser_identifier (parser);
29329 params
29330 = chainon (params,
29331 objc_build_keyword_decl (selector,
29332 type_name,
29333 identifier,
29334 parm_attr));
29336 token = cp_lexer_peek_token (parser->lexer);
29339 if (params == NULL_TREE)
29341 cp_parser_error (parser, "objective-c++ method declaration is expected");
29342 return error_mark_node;
29345 /* We allow tail attributes for the method. */
29346 if (token->keyword == RID_ATTRIBUTE)
29348 *attributes = cp_parser_attributes_opt (parser);
29349 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29350 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29351 return params;
29352 cp_parser_error (parser,
29353 "method attributes must be specified at the end");
29354 return error_mark_node;
29357 if (params == NULL_TREE)
29359 cp_parser_error (parser, "objective-c++ method declaration is expected");
29360 return error_mark_node;
29362 return params;
29365 /* Parse the non-keyword Objective-C params. */
29367 static tree
29368 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29369 tree* attributes)
29371 tree params = make_node (TREE_LIST);
29372 cp_token *token = cp_lexer_peek_token (parser->lexer);
29373 *ellipsisp = false; /* Initially, assume no ellipsis. */
29375 while (token->type == CPP_COMMA)
29377 cp_parameter_declarator *parmdecl;
29378 tree parm;
29380 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29381 token = cp_lexer_peek_token (parser->lexer);
29383 if (token->type == CPP_ELLIPSIS)
29385 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29386 *ellipsisp = true;
29387 token = cp_lexer_peek_token (parser->lexer);
29388 break;
29391 /* TODO: parse attributes for tail parameters. */
29392 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29393 parm = grokdeclarator (parmdecl->declarator,
29394 &parmdecl->decl_specifiers,
29395 PARM, /*initialized=*/0,
29396 /*attrlist=*/NULL);
29398 chainon (params, build_tree_list (NULL_TREE, parm));
29399 token = cp_lexer_peek_token (parser->lexer);
29402 /* We allow tail attributes for the method. */
29403 if (token->keyword == RID_ATTRIBUTE)
29405 if (*attributes == NULL_TREE)
29407 *attributes = cp_parser_attributes_opt (parser);
29408 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29409 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29410 return params;
29412 else
29413 /* We have an error, but parse the attributes, so that we can
29414 carry on. */
29415 *attributes = cp_parser_attributes_opt (parser);
29417 cp_parser_error (parser,
29418 "method attributes must be specified at the end");
29419 return error_mark_node;
29422 return params;
29425 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29427 static void
29428 cp_parser_objc_interstitial_code (cp_parser* parser)
29430 cp_token *token = cp_lexer_peek_token (parser->lexer);
29432 /* If the next token is `extern' and the following token is a string
29433 literal, then we have a linkage specification. */
29434 if (token->keyword == RID_EXTERN
29435 && cp_parser_is_pure_string_literal
29436 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29437 cp_parser_linkage_specification (parser);
29438 /* Handle #pragma, if any. */
29439 else if (token->type == CPP_PRAGMA)
29440 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29441 /* Allow stray semicolons. */
29442 else if (token->type == CPP_SEMICOLON)
29443 cp_lexer_consume_token (parser->lexer);
29444 /* Mark methods as optional or required, when building protocols. */
29445 else if (token->keyword == RID_AT_OPTIONAL)
29447 cp_lexer_consume_token (parser->lexer);
29448 objc_set_method_opt (true);
29450 else if (token->keyword == RID_AT_REQUIRED)
29452 cp_lexer_consume_token (parser->lexer);
29453 objc_set_method_opt (false);
29455 else if (token->keyword == RID_NAMESPACE)
29456 cp_parser_namespace_definition (parser);
29457 /* Other stray characters must generate errors. */
29458 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29460 cp_lexer_consume_token (parser->lexer);
29461 error ("stray %qs between Objective-C++ methods",
29462 token->type == CPP_OPEN_BRACE ? "{" : "}");
29464 /* Finally, try to parse a block-declaration, or a function-definition. */
29465 else
29466 cp_parser_block_declaration (parser, /*statement_p=*/false);
29469 /* Parse a method signature. */
29471 static tree
29472 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29474 tree rettype, kwdparms, optparms;
29475 bool ellipsis = false;
29476 bool is_class_method;
29478 is_class_method = cp_parser_objc_method_type (parser);
29479 rettype = cp_parser_objc_typename (parser);
29480 *attributes = NULL_TREE;
29481 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29482 if (kwdparms == error_mark_node)
29483 return error_mark_node;
29484 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29485 if (optparms == error_mark_node)
29486 return error_mark_node;
29488 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29491 static bool
29492 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29494 tree tattr;
29495 cp_lexer_save_tokens (parser->lexer);
29496 tattr = cp_parser_attributes_opt (parser);
29497 gcc_assert (tattr) ;
29499 /* If the attributes are followed by a method introducer, this is not allowed.
29500 Dump the attributes and flag the situation. */
29501 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29502 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29503 return true;
29505 /* Otherwise, the attributes introduce some interstitial code, possibly so
29506 rewind to allow that check. */
29507 cp_lexer_rollback_tokens (parser->lexer);
29508 return false;
29511 /* Parse an Objective-C method prototype list. */
29513 static void
29514 cp_parser_objc_method_prototype_list (cp_parser* parser)
29516 cp_token *token = cp_lexer_peek_token (parser->lexer);
29518 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29520 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29522 tree attributes, sig;
29523 bool is_class_method;
29524 if (token->type == CPP_PLUS)
29525 is_class_method = true;
29526 else
29527 is_class_method = false;
29528 sig = cp_parser_objc_method_signature (parser, &attributes);
29529 if (sig == error_mark_node)
29531 cp_parser_skip_to_end_of_block_or_statement (parser);
29532 token = cp_lexer_peek_token (parser->lexer);
29533 continue;
29535 objc_add_method_declaration (is_class_method, sig, attributes);
29536 cp_parser_consume_semicolon_at_end_of_statement (parser);
29538 else if (token->keyword == RID_AT_PROPERTY)
29539 cp_parser_objc_at_property_declaration (parser);
29540 else if (token->keyword == RID_ATTRIBUTE
29541 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29542 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29543 OPT_Wattributes,
29544 "prefix attributes are ignored for methods");
29545 else
29546 /* Allow for interspersed non-ObjC++ code. */
29547 cp_parser_objc_interstitial_code (parser);
29549 token = cp_lexer_peek_token (parser->lexer);
29552 if (token->type != CPP_EOF)
29553 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29554 else
29555 cp_parser_error (parser, "expected %<@end%>");
29557 objc_finish_interface ();
29560 /* Parse an Objective-C method definition list. */
29562 static void
29563 cp_parser_objc_method_definition_list (cp_parser* parser)
29565 cp_token *token = cp_lexer_peek_token (parser->lexer);
29567 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29569 tree meth;
29571 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29573 cp_token *ptk;
29574 tree sig, attribute;
29575 bool is_class_method;
29576 if (token->type == CPP_PLUS)
29577 is_class_method = true;
29578 else
29579 is_class_method = false;
29580 push_deferring_access_checks (dk_deferred);
29581 sig = cp_parser_objc_method_signature (parser, &attribute);
29582 if (sig == error_mark_node)
29584 cp_parser_skip_to_end_of_block_or_statement (parser);
29585 token = cp_lexer_peek_token (parser->lexer);
29586 continue;
29588 objc_start_method_definition (is_class_method, sig, attribute,
29589 NULL_TREE);
29591 /* For historical reasons, we accept an optional semicolon. */
29592 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29593 cp_lexer_consume_token (parser->lexer);
29595 ptk = cp_lexer_peek_token (parser->lexer);
29596 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29597 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29599 perform_deferred_access_checks (tf_warning_or_error);
29600 stop_deferring_access_checks ();
29601 meth = cp_parser_function_definition_after_declarator (parser,
29602 false);
29603 pop_deferring_access_checks ();
29604 objc_finish_method_definition (meth);
29607 /* The following case will be removed once @synthesize is
29608 completely implemented. */
29609 else if (token->keyword == RID_AT_PROPERTY)
29610 cp_parser_objc_at_property_declaration (parser);
29611 else if (token->keyword == RID_AT_SYNTHESIZE)
29612 cp_parser_objc_at_synthesize_declaration (parser);
29613 else if (token->keyword == RID_AT_DYNAMIC)
29614 cp_parser_objc_at_dynamic_declaration (parser);
29615 else if (token->keyword == RID_ATTRIBUTE
29616 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29617 warning_at (token->location, OPT_Wattributes,
29618 "prefix attributes are ignored for methods");
29619 else
29620 /* Allow for interspersed non-ObjC++ code. */
29621 cp_parser_objc_interstitial_code (parser);
29623 token = cp_lexer_peek_token (parser->lexer);
29626 if (token->type != CPP_EOF)
29627 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29628 else
29629 cp_parser_error (parser, "expected %<@end%>");
29631 objc_finish_implementation ();
29634 /* Parse Objective-C ivars. */
29636 static void
29637 cp_parser_objc_class_ivars (cp_parser* parser)
29639 cp_token *token = cp_lexer_peek_token (parser->lexer);
29641 if (token->type != CPP_OPEN_BRACE)
29642 return; /* No ivars specified. */
29644 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29645 token = cp_lexer_peek_token (parser->lexer);
29647 while (token->type != CPP_CLOSE_BRACE
29648 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29650 cp_decl_specifier_seq declspecs;
29651 int decl_class_or_enum_p;
29652 tree prefix_attributes;
29654 cp_parser_objc_visibility_spec (parser);
29656 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29657 break;
29659 cp_parser_decl_specifier_seq (parser,
29660 CP_PARSER_FLAGS_OPTIONAL,
29661 &declspecs,
29662 &decl_class_or_enum_p);
29664 /* auto, register, static, extern, mutable. */
29665 if (declspecs.storage_class != sc_none)
29667 cp_parser_error (parser, "invalid type for instance variable");
29668 declspecs.storage_class = sc_none;
29671 /* thread_local. */
29672 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29674 cp_parser_error (parser, "invalid type for instance variable");
29675 declspecs.locations[ds_thread] = 0;
29678 /* typedef. */
29679 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29681 cp_parser_error (parser, "invalid type for instance variable");
29682 declspecs.locations[ds_typedef] = 0;
29685 prefix_attributes = declspecs.attributes;
29686 declspecs.attributes = NULL_TREE;
29688 /* Keep going until we hit the `;' at the end of the
29689 declaration. */
29690 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29692 tree width = NULL_TREE, attributes, first_attribute, decl;
29693 cp_declarator *declarator = NULL;
29694 int ctor_dtor_or_conv_p;
29696 /* Check for a (possibly unnamed) bitfield declaration. */
29697 token = cp_lexer_peek_token (parser->lexer);
29698 if (token->type == CPP_COLON)
29699 goto eat_colon;
29701 if (token->type == CPP_NAME
29702 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
29703 == CPP_COLON))
29705 /* Get the name of the bitfield. */
29706 declarator = make_id_declarator (NULL_TREE,
29707 cp_parser_identifier (parser),
29708 sfk_none);
29710 eat_colon:
29711 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29712 /* Get the width of the bitfield. */
29713 width
29714 = cp_parser_constant_expression (parser);
29716 else
29718 /* Parse the declarator. */
29719 declarator
29720 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29721 &ctor_dtor_or_conv_p,
29722 /*parenthesized_p=*/NULL,
29723 /*member_p=*/false,
29724 /*friend_p=*/false);
29727 /* Look for attributes that apply to the ivar. */
29728 attributes = cp_parser_attributes_opt (parser);
29729 /* Remember which attributes are prefix attributes and
29730 which are not. */
29731 first_attribute = attributes;
29732 /* Combine the attributes. */
29733 attributes = chainon (prefix_attributes, attributes);
29735 if (width)
29736 /* Create the bitfield declaration. */
29737 decl = grokbitfield (declarator, &declspecs,
29738 width,
29739 attributes);
29740 else
29741 decl = grokfield (declarator, &declspecs,
29742 NULL_TREE, /*init_const_expr_p=*/false,
29743 NULL_TREE, attributes);
29745 /* Add the instance variable. */
29746 if (decl != error_mark_node && decl != NULL_TREE)
29747 objc_add_instance_variable (decl);
29749 /* Reset PREFIX_ATTRIBUTES. */
29750 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29751 attributes = TREE_CHAIN (attributes);
29752 if (attributes)
29753 TREE_CHAIN (attributes) = NULL_TREE;
29755 token = cp_lexer_peek_token (parser->lexer);
29757 if (token->type == CPP_COMMA)
29759 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29760 continue;
29762 break;
29765 cp_parser_consume_semicolon_at_end_of_statement (parser);
29766 token = cp_lexer_peek_token (parser->lexer);
29769 if (token->keyword == RID_AT_END)
29770 cp_parser_error (parser, "expected %<}%>");
29772 /* Do not consume the RID_AT_END, so it will be read again as terminating
29773 the @interface of @implementation. */
29774 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
29775 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
29777 /* For historical reasons, we accept an optional semicolon. */
29778 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29779 cp_lexer_consume_token (parser->lexer);
29782 /* Parse an Objective-C protocol declaration. */
29784 static void
29785 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
29787 tree proto, protorefs;
29788 cp_token *tok;
29790 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29791 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29793 tok = cp_lexer_peek_token (parser->lexer);
29794 error_at (tok->location, "identifier expected after %<@protocol%>");
29795 cp_parser_consume_semicolon_at_end_of_statement (parser);
29796 return;
29799 /* See if we have a forward declaration or a definition. */
29800 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
29802 /* Try a forward declaration first. */
29803 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
29805 while (true)
29807 tree id;
29809 id = cp_parser_identifier (parser);
29810 if (id == error_mark_node)
29811 break;
29813 objc_declare_protocol (id, attributes);
29815 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29816 cp_lexer_consume_token (parser->lexer);
29817 else
29818 break;
29820 cp_parser_consume_semicolon_at_end_of_statement (parser);
29823 /* Ok, we got a full-fledged definition (or at least should). */
29824 else
29826 proto = cp_parser_identifier (parser);
29827 protorefs = cp_parser_objc_protocol_refs_opt (parser);
29828 objc_start_protocol (proto, protorefs, attributes);
29829 cp_parser_objc_method_prototype_list (parser);
29833 /* Parse an Objective-C superclass or category. */
29835 static void
29836 cp_parser_objc_superclass_or_category (cp_parser *parser,
29837 bool iface_p,
29838 tree *super,
29839 tree *categ, bool *is_class_extension)
29841 cp_token *next = cp_lexer_peek_token (parser->lexer);
29843 *super = *categ = NULL_TREE;
29844 *is_class_extension = false;
29845 if (next->type == CPP_COLON)
29847 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29848 *super = cp_parser_identifier (parser);
29850 else if (next->type == CPP_OPEN_PAREN)
29852 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29854 /* If there is no category name, and this is an @interface, we
29855 have a class extension. */
29856 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29858 *categ = NULL_TREE;
29859 *is_class_extension = true;
29861 else
29862 *categ = cp_parser_identifier (parser);
29864 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29868 /* Parse an Objective-C class interface. */
29870 static void
29871 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29873 tree name, super, categ, protos;
29874 bool is_class_extension;
29876 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
29877 name = cp_parser_identifier (parser);
29878 if (name == error_mark_node)
29880 /* It's hard to recover because even if valid @interface stuff
29881 is to follow, we can't compile it (or validate it) if we
29882 don't even know which class it refers to. Let's assume this
29883 was a stray '@interface' token in the stream and skip it.
29885 return;
29887 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29888 &is_class_extension);
29889 protos = cp_parser_objc_protocol_refs_opt (parser);
29891 /* We have either a class or a category on our hands. */
29892 if (categ || is_class_extension)
29893 objc_start_category_interface (name, categ, protos, attributes);
29894 else
29896 objc_start_class_interface (name, super, protos, attributes);
29897 /* Handle instance variable declarations, if any. */
29898 cp_parser_objc_class_ivars (parser);
29899 objc_continue_interface ();
29902 cp_parser_objc_method_prototype_list (parser);
29905 /* Parse an Objective-C class implementation. */
29907 static void
29908 cp_parser_objc_class_implementation (cp_parser* parser)
29910 tree name, super, categ;
29911 bool is_class_extension;
29913 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
29914 name = cp_parser_identifier (parser);
29915 if (name == error_mark_node)
29917 /* It's hard to recover because even if valid @implementation
29918 stuff is to follow, we can't compile it (or validate it) if
29919 we don't even know which class it refers to. Let's assume
29920 this was a stray '@implementation' token in the stream and
29921 skip it.
29923 return;
29925 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29926 &is_class_extension);
29928 /* We have either a class or a category on our hands. */
29929 if (categ)
29930 objc_start_category_implementation (name, categ);
29931 else
29933 objc_start_class_implementation (name, super);
29934 /* Handle instance variable declarations, if any. */
29935 cp_parser_objc_class_ivars (parser);
29936 objc_continue_implementation ();
29939 cp_parser_objc_method_definition_list (parser);
29942 /* Consume the @end token and finish off the implementation. */
29944 static void
29945 cp_parser_objc_end_implementation (cp_parser* parser)
29947 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29948 objc_finish_implementation ();
29951 /* Parse an Objective-C declaration. */
29953 static void
29954 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29956 /* Try to figure out what kind of declaration is present. */
29957 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29959 if (attributes)
29960 switch (kwd->keyword)
29962 case RID_AT_ALIAS:
29963 case RID_AT_CLASS:
29964 case RID_AT_END:
29965 error_at (kwd->location, "attributes may not be specified before"
29966 " the %<@%D%> Objective-C++ keyword",
29967 kwd->u.value);
29968 attributes = NULL;
29969 break;
29970 case RID_AT_IMPLEMENTATION:
29971 warning_at (kwd->location, OPT_Wattributes,
29972 "prefix attributes are ignored before %<@%D%>",
29973 kwd->u.value);
29974 attributes = NULL;
29975 default:
29976 break;
29979 switch (kwd->keyword)
29981 case RID_AT_ALIAS:
29982 cp_parser_objc_alias_declaration (parser);
29983 break;
29984 case RID_AT_CLASS:
29985 cp_parser_objc_class_declaration (parser);
29986 break;
29987 case RID_AT_PROTOCOL:
29988 cp_parser_objc_protocol_declaration (parser, attributes);
29989 break;
29990 case RID_AT_INTERFACE:
29991 cp_parser_objc_class_interface (parser, attributes);
29992 break;
29993 case RID_AT_IMPLEMENTATION:
29994 cp_parser_objc_class_implementation (parser);
29995 break;
29996 case RID_AT_END:
29997 cp_parser_objc_end_implementation (parser);
29998 break;
29999 default:
30000 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30001 kwd->u.value);
30002 cp_parser_skip_to_end_of_block_or_statement (parser);
30006 /* Parse an Objective-C try-catch-finally statement.
30008 objc-try-catch-finally-stmt:
30009 @try compound-statement objc-catch-clause-seq [opt]
30010 objc-finally-clause [opt]
30012 objc-catch-clause-seq:
30013 objc-catch-clause objc-catch-clause-seq [opt]
30015 objc-catch-clause:
30016 @catch ( objc-exception-declaration ) compound-statement
30018 objc-finally-clause:
30019 @finally compound-statement
30021 objc-exception-declaration:
30022 parameter-declaration
30023 '...'
30025 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30027 Returns NULL_TREE.
30029 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30030 for C. Keep them in sync. */
30032 static tree
30033 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30035 location_t location;
30036 tree stmt;
30038 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30039 location = cp_lexer_peek_token (parser->lexer)->location;
30040 objc_maybe_warn_exceptions (location);
30041 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30042 node, lest it get absorbed into the surrounding block. */
30043 stmt = push_stmt_list ();
30044 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30045 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30047 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30049 cp_parameter_declarator *parm;
30050 tree parameter_declaration = error_mark_node;
30051 bool seen_open_paren = false;
30053 cp_lexer_consume_token (parser->lexer);
30054 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30055 seen_open_paren = true;
30056 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30058 /* We have "@catch (...)" (where the '...' are literally
30059 what is in the code). Skip the '...'.
30060 parameter_declaration is set to NULL_TREE, and
30061 objc_being_catch_clauses() knows that that means
30062 '...'. */
30063 cp_lexer_consume_token (parser->lexer);
30064 parameter_declaration = NULL_TREE;
30066 else
30068 /* We have "@catch (NSException *exception)" or something
30069 like that. Parse the parameter declaration. */
30070 parm = cp_parser_parameter_declaration (parser, false, NULL);
30071 if (parm == NULL)
30072 parameter_declaration = error_mark_node;
30073 else
30074 parameter_declaration = grokdeclarator (parm->declarator,
30075 &parm->decl_specifiers,
30076 PARM, /*initialized=*/0,
30077 /*attrlist=*/NULL);
30079 if (seen_open_paren)
30080 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30081 else
30083 /* If there was no open parenthesis, we are recovering from
30084 an error, and we are trying to figure out what mistake
30085 the user has made. */
30087 /* If there is an immediate closing parenthesis, the user
30088 probably forgot the opening one (ie, they typed "@catch
30089 NSException *e)". Parse the closing parenthesis and keep
30090 going. */
30091 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30092 cp_lexer_consume_token (parser->lexer);
30094 /* If these is no immediate closing parenthesis, the user
30095 probably doesn't know that parenthesis are required at
30096 all (ie, they typed "@catch NSException *e"). So, just
30097 forget about the closing parenthesis and keep going. */
30099 objc_begin_catch_clause (parameter_declaration);
30100 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30101 objc_finish_catch_clause ();
30103 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30105 cp_lexer_consume_token (parser->lexer);
30106 location = cp_lexer_peek_token (parser->lexer)->location;
30107 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30108 node, lest it get absorbed into the surrounding block. */
30109 stmt = push_stmt_list ();
30110 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30111 objc_build_finally_clause (location, pop_stmt_list (stmt));
30114 return objc_finish_try_stmt ();
30117 /* Parse an Objective-C synchronized statement.
30119 objc-synchronized-stmt:
30120 @synchronized ( expression ) compound-statement
30122 Returns NULL_TREE. */
30124 static tree
30125 cp_parser_objc_synchronized_statement (cp_parser *parser)
30127 location_t location;
30128 tree lock, stmt;
30130 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30132 location = cp_lexer_peek_token (parser->lexer)->location;
30133 objc_maybe_warn_exceptions (location);
30134 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30135 lock = cp_parser_expression (parser);
30136 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30138 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30139 node, lest it get absorbed into the surrounding block. */
30140 stmt = push_stmt_list ();
30141 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30143 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30146 /* Parse an Objective-C throw statement.
30148 objc-throw-stmt:
30149 @throw assignment-expression [opt] ;
30151 Returns a constructed '@throw' statement. */
30153 static tree
30154 cp_parser_objc_throw_statement (cp_parser *parser)
30156 tree expr = NULL_TREE;
30157 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30159 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30161 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30162 expr = cp_parser_expression (parser);
30164 cp_parser_consume_semicolon_at_end_of_statement (parser);
30166 return objc_build_throw_stmt (loc, expr);
30169 /* Parse an Objective-C statement. */
30171 static tree
30172 cp_parser_objc_statement (cp_parser * parser)
30174 /* Try to figure out what kind of declaration is present. */
30175 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30177 switch (kwd->keyword)
30179 case RID_AT_TRY:
30180 return cp_parser_objc_try_catch_finally_statement (parser);
30181 case RID_AT_SYNCHRONIZED:
30182 return cp_parser_objc_synchronized_statement (parser);
30183 case RID_AT_THROW:
30184 return cp_parser_objc_throw_statement (parser);
30185 default:
30186 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30187 kwd->u.value);
30188 cp_parser_skip_to_end_of_block_or_statement (parser);
30191 return error_mark_node;
30194 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30195 look ahead to see if an objc keyword follows the attributes. This
30196 is to detect the use of prefix attributes on ObjC @interface and
30197 @protocol. */
30199 static bool
30200 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30202 cp_lexer_save_tokens (parser->lexer);
30203 *attrib = cp_parser_attributes_opt (parser);
30204 gcc_assert (*attrib);
30205 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30207 cp_lexer_commit_tokens (parser->lexer);
30208 return true;
30210 cp_lexer_rollback_tokens (parser->lexer);
30211 return false;
30214 /* This routine is a minimal replacement for
30215 c_parser_struct_declaration () used when parsing the list of
30216 types/names or ObjC++ properties. For example, when parsing the
30217 code
30219 @property (readonly) int a, b, c;
30221 this function is responsible for parsing "int a, int b, int c" and
30222 returning the declarations as CHAIN of DECLs.
30224 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30225 similar parsing. */
30226 static tree
30227 cp_parser_objc_struct_declaration (cp_parser *parser)
30229 tree decls = NULL_TREE;
30230 cp_decl_specifier_seq declspecs;
30231 int decl_class_or_enum_p;
30232 tree prefix_attributes;
30234 cp_parser_decl_specifier_seq (parser,
30235 CP_PARSER_FLAGS_NONE,
30236 &declspecs,
30237 &decl_class_or_enum_p);
30239 if (declspecs.type == error_mark_node)
30240 return error_mark_node;
30242 /* auto, register, static, extern, mutable. */
30243 if (declspecs.storage_class != sc_none)
30245 cp_parser_error (parser, "invalid type for property");
30246 declspecs.storage_class = sc_none;
30249 /* thread_local. */
30250 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30252 cp_parser_error (parser, "invalid type for property");
30253 declspecs.locations[ds_thread] = 0;
30256 /* typedef. */
30257 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30259 cp_parser_error (parser, "invalid type for property");
30260 declspecs.locations[ds_typedef] = 0;
30263 prefix_attributes = declspecs.attributes;
30264 declspecs.attributes = NULL_TREE;
30266 /* Keep going until we hit the `;' at the end of the declaration. */
30267 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30269 tree attributes, first_attribute, decl;
30270 cp_declarator *declarator;
30271 cp_token *token;
30273 /* Parse the declarator. */
30274 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30275 NULL, NULL, false, false);
30277 /* Look for attributes that apply to the ivar. */
30278 attributes = cp_parser_attributes_opt (parser);
30279 /* Remember which attributes are prefix attributes and
30280 which are not. */
30281 first_attribute = attributes;
30282 /* Combine the attributes. */
30283 attributes = chainon (prefix_attributes, attributes);
30285 decl = grokfield (declarator, &declspecs,
30286 NULL_TREE, /*init_const_expr_p=*/false,
30287 NULL_TREE, attributes);
30289 if (decl == error_mark_node || decl == NULL_TREE)
30290 return error_mark_node;
30292 /* Reset PREFIX_ATTRIBUTES. */
30293 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30294 attributes = TREE_CHAIN (attributes);
30295 if (attributes)
30296 TREE_CHAIN (attributes) = NULL_TREE;
30298 DECL_CHAIN (decl) = decls;
30299 decls = decl;
30301 token = cp_lexer_peek_token (parser->lexer);
30302 if (token->type == CPP_COMMA)
30304 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30305 continue;
30307 else
30308 break;
30310 return decls;
30313 /* Parse an Objective-C @property declaration. The syntax is:
30315 objc-property-declaration:
30316 '@property' objc-property-attributes[opt] struct-declaration ;
30318 objc-property-attributes:
30319 '(' objc-property-attribute-list ')'
30321 objc-property-attribute-list:
30322 objc-property-attribute
30323 objc-property-attribute-list, objc-property-attribute
30325 objc-property-attribute
30326 'getter' = identifier
30327 'setter' = identifier
30328 'readonly'
30329 'readwrite'
30330 'assign'
30331 'retain'
30332 'copy'
30333 'nonatomic'
30335 For example:
30336 @property NSString *name;
30337 @property (readonly) id object;
30338 @property (retain, nonatomic, getter=getTheName) id name;
30339 @property int a, b, c;
30341 PS: This function is identical to
30342 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30343 static void
30344 cp_parser_objc_at_property_declaration (cp_parser *parser)
30346 /* The following variables hold the attributes of the properties as
30347 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30348 seen. When we see an attribute, we set them to 'true' (if they
30349 are boolean properties) or to the identifier (if they have an
30350 argument, ie, for getter and setter). Note that here we only
30351 parse the list of attributes, check the syntax and accumulate the
30352 attributes that we find. objc_add_property_declaration() will
30353 then process the information. */
30354 bool property_assign = false;
30355 bool property_copy = false;
30356 tree property_getter_ident = NULL_TREE;
30357 bool property_nonatomic = false;
30358 bool property_readonly = false;
30359 bool property_readwrite = false;
30360 bool property_retain = false;
30361 tree property_setter_ident = NULL_TREE;
30363 /* 'properties' is the list of properties that we read. Usually a
30364 single one, but maybe more (eg, in "@property int a, b, c;" there
30365 are three). */
30366 tree properties;
30367 location_t loc;
30369 loc = cp_lexer_peek_token (parser->lexer)->location;
30371 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30373 /* Parse the optional attribute list... */
30374 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30376 /* Eat the '('. */
30377 cp_lexer_consume_token (parser->lexer);
30379 while (true)
30381 bool syntax_error = false;
30382 cp_token *token = cp_lexer_peek_token (parser->lexer);
30383 enum rid keyword;
30385 if (token->type != CPP_NAME)
30387 cp_parser_error (parser, "expected identifier");
30388 break;
30390 keyword = C_RID_CODE (token->u.value);
30391 cp_lexer_consume_token (parser->lexer);
30392 switch (keyword)
30394 case RID_ASSIGN: property_assign = true; break;
30395 case RID_COPY: property_copy = true; break;
30396 case RID_NONATOMIC: property_nonatomic = true; break;
30397 case RID_READONLY: property_readonly = true; break;
30398 case RID_READWRITE: property_readwrite = true; break;
30399 case RID_RETAIN: property_retain = true; break;
30401 case RID_GETTER:
30402 case RID_SETTER:
30403 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30405 if (keyword == RID_GETTER)
30406 cp_parser_error (parser,
30407 "missing %<=%> (after %<getter%> attribute)");
30408 else
30409 cp_parser_error (parser,
30410 "missing %<=%> (after %<setter%> attribute)");
30411 syntax_error = true;
30412 break;
30414 cp_lexer_consume_token (parser->lexer); /* eat the = */
30415 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30417 cp_parser_error (parser, "expected identifier");
30418 syntax_error = true;
30419 break;
30421 if (keyword == RID_SETTER)
30423 if (property_setter_ident != NULL_TREE)
30425 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30426 cp_lexer_consume_token (parser->lexer);
30428 else
30429 property_setter_ident = cp_parser_objc_selector (parser);
30430 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30431 cp_parser_error (parser, "setter name must terminate with %<:%>");
30432 else
30433 cp_lexer_consume_token (parser->lexer);
30435 else
30437 if (property_getter_ident != NULL_TREE)
30439 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30440 cp_lexer_consume_token (parser->lexer);
30442 else
30443 property_getter_ident = cp_parser_objc_selector (parser);
30445 break;
30446 default:
30447 cp_parser_error (parser, "unknown property attribute");
30448 syntax_error = true;
30449 break;
30452 if (syntax_error)
30453 break;
30455 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30456 cp_lexer_consume_token (parser->lexer);
30457 else
30458 break;
30461 /* FIXME: "@property (setter, assign);" will generate a spurious
30462 "error: expected ‘)’ before ‘,’ token". This is because
30463 cp_parser_require, unlike the C counterpart, will produce an
30464 error even if we are in error recovery. */
30465 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30467 cp_parser_skip_to_closing_parenthesis (parser,
30468 /*recovering=*/true,
30469 /*or_comma=*/false,
30470 /*consume_paren=*/true);
30474 /* ... and the property declaration(s). */
30475 properties = cp_parser_objc_struct_declaration (parser);
30477 if (properties == error_mark_node)
30479 cp_parser_skip_to_end_of_statement (parser);
30480 /* If the next token is now a `;', consume it. */
30481 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30482 cp_lexer_consume_token (parser->lexer);
30483 return;
30486 if (properties == NULL_TREE)
30487 cp_parser_error (parser, "expected identifier");
30488 else
30490 /* Comma-separated properties are chained together in
30491 reverse order; add them one by one. */
30492 properties = nreverse (properties);
30494 for (; properties; properties = TREE_CHAIN (properties))
30495 objc_add_property_declaration (loc, copy_node (properties),
30496 property_readonly, property_readwrite,
30497 property_assign, property_retain,
30498 property_copy, property_nonatomic,
30499 property_getter_ident, property_setter_ident);
30502 cp_parser_consume_semicolon_at_end_of_statement (parser);
30505 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30507 objc-synthesize-declaration:
30508 @synthesize objc-synthesize-identifier-list ;
30510 objc-synthesize-identifier-list:
30511 objc-synthesize-identifier
30512 objc-synthesize-identifier-list, objc-synthesize-identifier
30514 objc-synthesize-identifier
30515 identifier
30516 identifier = identifier
30518 For example:
30519 @synthesize MyProperty;
30520 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30522 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30523 for C. Keep them in sync.
30525 static void
30526 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30528 tree list = NULL_TREE;
30529 location_t loc;
30530 loc = cp_lexer_peek_token (parser->lexer)->location;
30532 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30533 while (true)
30535 tree property, ivar;
30536 property = cp_parser_identifier (parser);
30537 if (property == error_mark_node)
30539 cp_parser_consume_semicolon_at_end_of_statement (parser);
30540 return;
30542 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30544 cp_lexer_consume_token (parser->lexer);
30545 ivar = cp_parser_identifier (parser);
30546 if (ivar == error_mark_node)
30548 cp_parser_consume_semicolon_at_end_of_statement (parser);
30549 return;
30552 else
30553 ivar = NULL_TREE;
30554 list = chainon (list, build_tree_list (ivar, property));
30555 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30556 cp_lexer_consume_token (parser->lexer);
30557 else
30558 break;
30560 cp_parser_consume_semicolon_at_end_of_statement (parser);
30561 objc_add_synthesize_declaration (loc, list);
30564 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30566 objc-dynamic-declaration:
30567 @dynamic identifier-list ;
30569 For example:
30570 @dynamic MyProperty;
30571 @dynamic MyProperty, AnotherProperty;
30573 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30574 for C. Keep them in sync.
30576 static void
30577 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30579 tree list = NULL_TREE;
30580 location_t loc;
30581 loc = cp_lexer_peek_token (parser->lexer)->location;
30583 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30584 while (true)
30586 tree property;
30587 property = cp_parser_identifier (parser);
30588 if (property == error_mark_node)
30590 cp_parser_consume_semicolon_at_end_of_statement (parser);
30591 return;
30593 list = chainon (list, build_tree_list (NULL, property));
30594 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30595 cp_lexer_consume_token (parser->lexer);
30596 else
30597 break;
30599 cp_parser_consume_semicolon_at_end_of_statement (parser);
30600 objc_add_dynamic_declaration (loc, list);
30604 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30606 /* Returns name of the next clause.
30607 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30608 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30609 returned and the token is consumed. */
30611 static pragma_omp_clause
30612 cp_parser_omp_clause_name (cp_parser *parser)
30614 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30616 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30617 result = PRAGMA_OACC_CLAUSE_AUTO;
30618 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30619 result = PRAGMA_OMP_CLAUSE_IF;
30620 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30621 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30622 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30623 result = PRAGMA_OACC_CLAUSE_DELETE;
30624 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30625 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30626 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30627 result = PRAGMA_OMP_CLAUSE_FOR;
30628 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30630 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30631 const char *p = IDENTIFIER_POINTER (id);
30633 switch (p[0])
30635 case 'a':
30636 if (!strcmp ("aligned", p))
30637 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30638 else if (!strcmp ("async", p))
30639 result = PRAGMA_OACC_CLAUSE_ASYNC;
30640 break;
30641 case 'c':
30642 if (!strcmp ("collapse", p))
30643 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30644 else if (!strcmp ("copy", p))
30645 result = PRAGMA_OACC_CLAUSE_COPY;
30646 else if (!strcmp ("copyin", p))
30647 result = PRAGMA_OMP_CLAUSE_COPYIN;
30648 else if (!strcmp ("copyout", p))
30649 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30650 else if (!strcmp ("copyprivate", p))
30651 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30652 else if (!strcmp ("create", p))
30653 result = PRAGMA_OACC_CLAUSE_CREATE;
30654 break;
30655 case 'd':
30656 if (!strcmp ("defaultmap", p))
30657 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30658 else if (!strcmp ("depend", p))
30659 result = PRAGMA_OMP_CLAUSE_DEPEND;
30660 else if (!strcmp ("device", p))
30661 result = PRAGMA_OMP_CLAUSE_DEVICE;
30662 else if (!strcmp ("deviceptr", p))
30663 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30664 else if (!strcmp ("device_resident", p))
30665 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30666 else if (!strcmp ("dist_schedule", p))
30667 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30668 break;
30669 case 'f':
30670 if (!strcmp ("final", p))
30671 result = PRAGMA_OMP_CLAUSE_FINAL;
30672 else if (!strcmp ("firstprivate", p))
30673 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30674 else if (!strcmp ("from", p))
30675 result = PRAGMA_OMP_CLAUSE_FROM;
30676 break;
30677 case 'g':
30678 if (!strcmp ("gang", p))
30679 result = PRAGMA_OACC_CLAUSE_GANG;
30680 else if (!strcmp ("grainsize", p))
30681 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
30682 break;
30683 case 'h':
30684 if (!strcmp ("hint", p))
30685 result = PRAGMA_OMP_CLAUSE_HINT;
30686 else if (!strcmp ("host", p))
30687 result = PRAGMA_OACC_CLAUSE_HOST;
30688 break;
30689 case 'i':
30690 if (!strcmp ("inbranch", p))
30691 result = PRAGMA_OMP_CLAUSE_INBRANCH;
30692 else if (!strcmp ("independent", p))
30693 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
30694 else if (!strcmp ("is_device_ptr", p))
30695 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
30696 break;
30697 case 'l':
30698 if (!strcmp ("lastprivate", p))
30699 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
30700 else if (!strcmp ("linear", p))
30701 result = PRAGMA_OMP_CLAUSE_LINEAR;
30702 else if (!strcmp ("link", p))
30703 result = PRAGMA_OMP_CLAUSE_LINK;
30704 break;
30705 case 'm':
30706 if (!strcmp ("map", p))
30707 result = PRAGMA_OMP_CLAUSE_MAP;
30708 else if (!strcmp ("mergeable", p))
30709 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
30710 else if (flag_cilkplus && !strcmp ("mask", p))
30711 result = PRAGMA_CILK_CLAUSE_MASK;
30712 break;
30713 case 'n':
30714 if (!strcmp ("nogroup", p))
30715 result = PRAGMA_OMP_CLAUSE_NOGROUP;
30716 else if (!strcmp ("notinbranch", p))
30717 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
30718 else if (!strcmp ("nowait", p))
30719 result = PRAGMA_OMP_CLAUSE_NOWAIT;
30720 else if (flag_cilkplus && !strcmp ("nomask", p))
30721 result = PRAGMA_CILK_CLAUSE_NOMASK;
30722 else if (!strcmp ("num_gangs", p))
30723 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
30724 else if (!strcmp ("num_tasks", p))
30725 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
30726 else if (!strcmp ("num_teams", p))
30727 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
30728 else if (!strcmp ("num_threads", p))
30729 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
30730 else if (!strcmp ("num_workers", p))
30731 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
30732 break;
30733 case 'o':
30734 if (!strcmp ("ordered", p))
30735 result = PRAGMA_OMP_CLAUSE_ORDERED;
30736 break;
30737 case 'p':
30738 if (!strcmp ("parallel", p))
30739 result = PRAGMA_OMP_CLAUSE_PARALLEL;
30740 else if (!strcmp ("present", p))
30741 result = PRAGMA_OACC_CLAUSE_PRESENT;
30742 else if (!strcmp ("present_or_copy", p)
30743 || !strcmp ("pcopy", p))
30744 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
30745 else if (!strcmp ("present_or_copyin", p)
30746 || !strcmp ("pcopyin", p))
30747 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
30748 else if (!strcmp ("present_or_copyout", p)
30749 || !strcmp ("pcopyout", p))
30750 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
30751 else if (!strcmp ("present_or_create", p)
30752 || !strcmp ("pcreate", p))
30753 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
30754 else if (!strcmp ("priority", p))
30755 result = PRAGMA_OMP_CLAUSE_PRIORITY;
30756 else if (!strcmp ("proc_bind", p))
30757 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
30758 break;
30759 case 'r':
30760 if (!strcmp ("reduction", p))
30761 result = PRAGMA_OMP_CLAUSE_REDUCTION;
30762 break;
30763 case 's':
30764 if (!strcmp ("safelen", p))
30765 result = PRAGMA_OMP_CLAUSE_SAFELEN;
30766 else if (!strcmp ("schedule", p))
30767 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
30768 else if (!strcmp ("sections", p))
30769 result = PRAGMA_OMP_CLAUSE_SECTIONS;
30770 else if (!strcmp ("self", p))
30771 result = PRAGMA_OACC_CLAUSE_SELF;
30772 else if (!strcmp ("seq", p))
30773 result = PRAGMA_OACC_CLAUSE_SEQ;
30774 else if (!strcmp ("shared", p))
30775 result = PRAGMA_OMP_CLAUSE_SHARED;
30776 else if (!strcmp ("simd", p))
30777 result = PRAGMA_OMP_CLAUSE_SIMD;
30778 else if (!strcmp ("simdlen", p))
30779 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
30780 break;
30781 case 't':
30782 if (!strcmp ("taskgroup", p))
30783 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
30784 else if (!strcmp ("thread_limit", p))
30785 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
30786 else if (!strcmp ("threads", p))
30787 result = PRAGMA_OMP_CLAUSE_THREADS;
30788 else if (!strcmp ("tile", p))
30789 result = PRAGMA_OACC_CLAUSE_TILE;
30790 else if (!strcmp ("to", p))
30791 result = PRAGMA_OMP_CLAUSE_TO;
30792 break;
30793 case 'u':
30794 if (!strcmp ("uniform", p))
30795 result = PRAGMA_OMP_CLAUSE_UNIFORM;
30796 else if (!strcmp ("untied", p))
30797 result = PRAGMA_OMP_CLAUSE_UNTIED;
30798 else if (!strcmp ("use_device", p))
30799 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
30800 else if (!strcmp ("use_device_ptr", p))
30801 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
30802 break;
30803 case 'v':
30804 if (!strcmp ("vector", p))
30805 result = PRAGMA_OACC_CLAUSE_VECTOR;
30806 else if (!strcmp ("vector_length", p))
30807 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
30808 else if (flag_cilkplus && !strcmp ("vectorlength", p))
30809 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
30810 break;
30811 case 'w':
30812 if (!strcmp ("wait", p))
30813 result = PRAGMA_OACC_CLAUSE_WAIT;
30814 else if (!strcmp ("worker", p))
30815 result = PRAGMA_OACC_CLAUSE_WORKER;
30816 break;
30820 if (result != PRAGMA_OMP_CLAUSE_NONE)
30821 cp_lexer_consume_token (parser->lexer);
30823 return result;
30826 /* Validate that a clause of the given type does not already exist. */
30828 static void
30829 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
30830 const char *name, location_t location)
30832 tree c;
30834 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30835 if (OMP_CLAUSE_CODE (c) == code)
30837 error_at (location, "too many %qs clauses", name);
30838 break;
30842 /* OpenMP 2.5:
30843 variable-list:
30844 identifier
30845 variable-list , identifier
30847 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30848 colon). An opening parenthesis will have been consumed by the caller.
30850 If KIND is nonzero, create the appropriate node and install the decl
30851 in OMP_CLAUSE_DECL and add the node to the head of the list.
30853 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30854 return the list created.
30856 COLON can be NULL if only closing parenthesis should end the list,
30857 or pointer to bool which will receive false if the list is terminated
30858 by closing parenthesis or true if the list is terminated by colon. */
30860 static tree
30861 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30862 tree list, bool *colon)
30864 cp_token *token;
30865 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30866 if (colon)
30868 parser->colon_corrects_to_scope_p = false;
30869 *colon = false;
30871 while (1)
30873 tree name, decl;
30875 token = cp_lexer_peek_token (parser->lexer);
30876 if (kind != 0
30877 && current_class_ptr
30878 && cp_parser_is_keyword (token, RID_THIS))
30880 decl = finish_this_expr ();
30881 if (TREE_CODE (decl) == NON_LVALUE_EXPR
30882 || CONVERT_EXPR_P (decl))
30883 decl = TREE_OPERAND (decl, 0);
30884 cp_lexer_consume_token (parser->lexer);
30886 else
30888 name = cp_parser_id_expression (parser, /*template_p=*/false,
30889 /*check_dependency_p=*/true,
30890 /*template_p=*/NULL,
30891 /*declarator_p=*/false,
30892 /*optional_p=*/false);
30893 if (name == error_mark_node)
30894 goto skip_comma;
30896 decl = cp_parser_lookup_name_simple (parser, name, token->location);
30897 if (decl == error_mark_node)
30898 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30899 token->location);
30901 if (decl == error_mark_node)
30903 else if (kind != 0)
30905 switch (kind)
30907 case OMP_CLAUSE__CACHE_:
30908 /* The OpenACC cache directive explicitly only allows "array
30909 elements or subarrays". */
30910 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30912 error_at (token->location, "expected %<[%>");
30913 decl = error_mark_node;
30914 break;
30916 /* FALLTHROUGH. */
30917 case OMP_CLAUSE_MAP:
30918 case OMP_CLAUSE_FROM:
30919 case OMP_CLAUSE_TO:
30920 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30922 location_t loc
30923 = cp_lexer_peek_token (parser->lexer)->location;
30924 cp_id_kind idk = CP_ID_KIND_NONE;
30925 cp_lexer_consume_token (parser->lexer);
30926 decl = convert_from_reference (decl);
30927 decl
30928 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30929 decl, false,
30930 &idk, loc);
30932 /* FALLTHROUGH. */
30933 case OMP_CLAUSE_DEPEND:
30934 case OMP_CLAUSE_REDUCTION:
30935 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30937 tree low_bound = NULL_TREE, length = NULL_TREE;
30939 parser->colon_corrects_to_scope_p = false;
30940 cp_lexer_consume_token (parser->lexer);
30941 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30942 low_bound = cp_parser_expression (parser);
30943 if (!colon)
30944 parser->colon_corrects_to_scope_p
30945 = saved_colon_corrects_to_scope_p;
30946 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30947 length = integer_one_node;
30948 else
30950 /* Look for `:'. */
30951 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30952 goto skip_comma;
30953 if (!cp_lexer_next_token_is (parser->lexer,
30954 CPP_CLOSE_SQUARE))
30955 length = cp_parser_expression (parser);
30957 /* Look for the closing `]'. */
30958 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30959 RT_CLOSE_SQUARE))
30960 goto skip_comma;
30962 decl = tree_cons (low_bound, length, decl);
30964 break;
30965 default:
30966 break;
30969 tree u = build_omp_clause (token->location, kind);
30970 OMP_CLAUSE_DECL (u) = decl;
30971 OMP_CLAUSE_CHAIN (u) = list;
30972 list = u;
30974 else
30975 list = tree_cons (decl, NULL_TREE, list);
30977 get_comma:
30978 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30979 break;
30980 cp_lexer_consume_token (parser->lexer);
30983 if (colon)
30984 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30986 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30988 *colon = true;
30989 cp_parser_require (parser, CPP_COLON, RT_COLON);
30990 return list;
30993 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30995 int ending;
30997 /* Try to resync to an unnested comma. Copied from
30998 cp_parser_parenthesized_expression_list. */
30999 skip_comma:
31000 if (colon)
31001 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31002 ending = cp_parser_skip_to_closing_parenthesis (parser,
31003 /*recovering=*/true,
31004 /*or_comma=*/true,
31005 /*consume_paren=*/true);
31006 if (ending < 0)
31007 goto get_comma;
31010 return list;
31013 /* Similarly, but expect leading and trailing parenthesis. This is a very
31014 common case for omp clauses. */
31016 static tree
31017 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31019 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31020 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31021 return list;
31024 /* OpenACC 2.0:
31025 copy ( variable-list )
31026 copyin ( variable-list )
31027 copyout ( variable-list )
31028 create ( variable-list )
31029 delete ( variable-list )
31030 present ( variable-list )
31031 present_or_copy ( variable-list )
31032 pcopy ( variable-list )
31033 present_or_copyin ( variable-list )
31034 pcopyin ( variable-list )
31035 present_or_copyout ( variable-list )
31036 pcopyout ( variable-list )
31037 present_or_create ( variable-list )
31038 pcreate ( variable-list ) */
31040 static tree
31041 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31042 tree list)
31044 enum gomp_map_kind kind;
31045 switch (c_kind)
31047 case PRAGMA_OACC_CLAUSE_COPY:
31048 kind = GOMP_MAP_FORCE_TOFROM;
31049 break;
31050 case PRAGMA_OACC_CLAUSE_COPYIN:
31051 kind = GOMP_MAP_FORCE_TO;
31052 break;
31053 case PRAGMA_OACC_CLAUSE_COPYOUT:
31054 kind = GOMP_MAP_FORCE_FROM;
31055 break;
31056 case PRAGMA_OACC_CLAUSE_CREATE:
31057 kind = GOMP_MAP_FORCE_ALLOC;
31058 break;
31059 case PRAGMA_OACC_CLAUSE_DELETE:
31060 kind = GOMP_MAP_DELETE;
31061 break;
31062 case PRAGMA_OACC_CLAUSE_DEVICE:
31063 kind = GOMP_MAP_FORCE_TO;
31064 break;
31065 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31066 kind = GOMP_MAP_DEVICE_RESIDENT;
31067 break;
31068 case PRAGMA_OACC_CLAUSE_HOST:
31069 case PRAGMA_OACC_CLAUSE_SELF:
31070 kind = GOMP_MAP_FORCE_FROM;
31071 break;
31072 case PRAGMA_OACC_CLAUSE_LINK:
31073 kind = GOMP_MAP_LINK;
31074 break;
31075 case PRAGMA_OACC_CLAUSE_PRESENT:
31076 kind = GOMP_MAP_FORCE_PRESENT;
31077 break;
31078 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31079 kind = GOMP_MAP_TOFROM;
31080 break;
31081 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31082 kind = GOMP_MAP_TO;
31083 break;
31084 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31085 kind = GOMP_MAP_FROM;
31086 break;
31087 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31088 kind = GOMP_MAP_ALLOC;
31089 break;
31090 default:
31091 gcc_unreachable ();
31093 tree nl, c;
31094 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31096 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31097 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31099 return nl;
31102 /* OpenACC 2.0:
31103 deviceptr ( variable-list ) */
31105 static tree
31106 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31108 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31109 tree vars, t;
31111 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31112 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31113 variable-list must only allow for pointer variables. */
31114 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31115 for (t = vars; t; t = TREE_CHAIN (t))
31117 tree v = TREE_PURPOSE (t);
31118 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31119 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31120 OMP_CLAUSE_DECL (u) = v;
31121 OMP_CLAUSE_CHAIN (u) = list;
31122 list = u;
31125 return list;
31128 /* OpenACC 2.0:
31129 auto
31130 independent
31131 nohost
31132 seq */
31134 static tree
31135 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31136 enum omp_clause_code code,
31137 tree list, location_t location)
31139 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31140 tree c = build_omp_clause (location, code);
31141 OMP_CLAUSE_CHAIN (c) = list;
31142 return c;
31145 /* OpenACC:
31146 num_gangs ( expression )
31147 num_workers ( expression )
31148 vector_length ( expression ) */
31150 static tree
31151 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31152 const char *str, tree list)
31154 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31156 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31157 return list;
31159 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31161 if (t == error_mark_node
31162 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31164 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31165 /*or_comma=*/false,
31166 /*consume_paren=*/true);
31167 return list;
31170 check_no_duplicate_clause (list, code, str, loc);
31172 tree c = build_omp_clause (loc, code);
31173 OMP_CLAUSE_OPERAND (c, 0) = t;
31174 OMP_CLAUSE_CHAIN (c) = list;
31175 return c;
31178 /* OpenACC:
31180 gang [( gang-arg-list )]
31181 worker [( [num:] int-expr )]
31182 vector [( [length:] int-expr )]
31184 where gang-arg is one of:
31186 [num:] int-expr
31187 static: size-expr
31189 and size-expr may be:
31192 int-expr
31195 static tree
31196 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31197 const char *str, tree list)
31199 const char *id = "num";
31200 cp_lexer *lexer = parser->lexer;
31201 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31202 location_t loc = cp_lexer_peek_token (lexer)->location;
31204 if (kind == OMP_CLAUSE_VECTOR)
31205 id = "length";
31207 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31209 cp_lexer_consume_token (lexer);
31213 cp_token *next = cp_lexer_peek_token (lexer);
31214 int idx = 0;
31216 /* Gang static argument. */
31217 if (kind == OMP_CLAUSE_GANG
31218 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31220 cp_lexer_consume_token (lexer);
31222 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31223 goto cleanup_error;
31225 idx = 1;
31226 if (ops[idx] != NULL)
31228 cp_parser_error (parser, "too many %<static%> arguments");
31229 goto cleanup_error;
31232 /* Check for the '*' argument. */
31233 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31234 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31235 || cp_lexer_nth_token_is (parser->lexer, 2,
31236 CPP_CLOSE_PAREN)))
31238 cp_lexer_consume_token (lexer);
31239 ops[idx] = integer_minus_one_node;
31241 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31243 cp_lexer_consume_token (lexer);
31244 continue;
31246 else break;
31249 /* Worker num: argument and vector length: arguments. */
31250 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31251 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
31252 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31254 cp_lexer_consume_token (lexer); /* id */
31255 cp_lexer_consume_token (lexer); /* ':' */
31258 /* Now collect the actual argument. */
31259 if (ops[idx] != NULL_TREE)
31261 cp_parser_error (parser, "unexpected argument");
31262 goto cleanup_error;
31265 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31266 false);
31267 if (expr == error_mark_node)
31268 goto cleanup_error;
31270 mark_exp_read (expr);
31271 ops[idx] = expr;
31273 if (kind == OMP_CLAUSE_GANG
31274 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31276 cp_lexer_consume_token (lexer);
31277 continue;
31279 break;
31281 while (1);
31283 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31284 goto cleanup_error;
31287 check_no_duplicate_clause (list, kind, str, loc);
31289 c = build_omp_clause (loc, kind);
31291 if (ops[1])
31292 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31294 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31295 OMP_CLAUSE_CHAIN (c) = list;
31297 return c;
31299 cleanup_error:
31300 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31301 return list;
31304 /* OpenACC 2.0:
31305 tile ( size-expr-list ) */
31307 static tree
31308 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31310 tree c, expr = error_mark_node;
31311 tree tile = NULL_TREE;
31313 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31314 so, but the spec authors never considered such a case and have
31315 differing opinions on what it might mean, including 'not
31316 allowed'.) */
31317 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31318 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31319 clause_loc);
31321 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31322 return list;
31326 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31327 return list;
31329 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31330 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31331 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31333 cp_lexer_consume_token (parser->lexer);
31334 expr = integer_zero_node;
31336 else
31337 expr = cp_parser_constant_expression (parser);
31339 tile = tree_cons (NULL_TREE, expr, tile);
31341 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31343 /* Consume the trailing ')'. */
31344 cp_lexer_consume_token (parser->lexer);
31346 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31347 tile = nreverse (tile);
31348 OMP_CLAUSE_TILE_LIST (c) = tile;
31349 OMP_CLAUSE_CHAIN (c) = list;
31350 return c;
31353 /* OpenACC 2.0
31354 Parse wait clause or directive parameters. */
31356 static tree
31357 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31359 vec<tree, va_gc> *args;
31360 tree t, args_tree;
31362 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31363 /*cast_p=*/false,
31364 /*allow_expansion_p=*/true,
31365 /*non_constant_p=*/NULL);
31367 if (args == NULL || args->length () == 0)
31369 cp_parser_error (parser, "expected integer expression before ')'");
31370 if (args != NULL)
31371 release_tree_vector (args);
31372 return list;
31375 args_tree = build_tree_list_vec (args);
31377 release_tree_vector (args);
31379 for (t = args_tree; t; t = TREE_CHAIN (t))
31381 tree targ = TREE_VALUE (t);
31383 if (targ != error_mark_node)
31385 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31386 error ("%<wait%> expression must be integral");
31387 else
31389 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31391 mark_rvalue_use (targ);
31392 OMP_CLAUSE_DECL (c) = targ;
31393 OMP_CLAUSE_CHAIN (c) = list;
31394 list = c;
31399 return list;
31402 /* OpenACC:
31403 wait ( int-expr-list ) */
31405 static tree
31406 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31408 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31410 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31411 return list;
31413 list = cp_parser_oacc_wait_list (parser, location, list);
31415 return list;
31418 /* OpenMP 3.0:
31419 collapse ( constant-expression ) */
31421 static tree
31422 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31424 tree c, num;
31425 location_t loc;
31426 HOST_WIDE_INT n;
31428 loc = cp_lexer_peek_token (parser->lexer)->location;
31429 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31430 return list;
31432 num = cp_parser_constant_expression (parser);
31434 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31435 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31436 /*or_comma=*/false,
31437 /*consume_paren=*/true);
31439 if (num == error_mark_node)
31440 return list;
31441 num = fold_non_dependent_expr (num);
31442 if (!tree_fits_shwi_p (num)
31443 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31444 || (n = tree_to_shwi (num)) <= 0
31445 || (int) n != n)
31447 error_at (loc, "collapse argument needs positive constant integer expression");
31448 return list;
31451 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31452 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31453 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31454 OMP_CLAUSE_CHAIN (c) = list;
31455 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31457 return c;
31460 /* OpenMP 2.5:
31461 default ( shared | none )
31463 OpenACC 2.0
31464 default (none) */
31466 static tree
31467 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31468 location_t location, bool is_oacc)
31470 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31471 tree c;
31473 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31474 return list;
31475 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31477 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31478 const char *p = IDENTIFIER_POINTER (id);
31480 switch (p[0])
31482 case 'n':
31483 if (strcmp ("none", p) != 0)
31484 goto invalid_kind;
31485 kind = OMP_CLAUSE_DEFAULT_NONE;
31486 break;
31488 case 's':
31489 if (strcmp ("shared", p) != 0 || is_oacc)
31490 goto invalid_kind;
31491 kind = OMP_CLAUSE_DEFAULT_SHARED;
31492 break;
31494 default:
31495 goto invalid_kind;
31498 cp_lexer_consume_token (parser->lexer);
31500 else
31502 invalid_kind:
31503 if (is_oacc)
31504 cp_parser_error (parser, "expected %<none%>");
31505 else
31506 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31509 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31510 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31511 /*or_comma=*/false,
31512 /*consume_paren=*/true);
31514 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31515 return list;
31517 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31518 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31519 OMP_CLAUSE_CHAIN (c) = list;
31520 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31522 return c;
31525 /* OpenMP 3.1:
31526 final ( expression ) */
31528 static tree
31529 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31531 tree t, c;
31533 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31534 return list;
31536 t = cp_parser_condition (parser);
31538 if (t == error_mark_node
31539 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31540 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31541 /*or_comma=*/false,
31542 /*consume_paren=*/true);
31544 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31546 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31547 OMP_CLAUSE_FINAL_EXPR (c) = t;
31548 OMP_CLAUSE_CHAIN (c) = list;
31550 return c;
31553 /* OpenMP 2.5:
31554 if ( expression )
31556 OpenMP 4.5:
31557 if ( directive-name-modifier : expression )
31559 directive-name-modifier:
31560 parallel | task | taskloop | target data | target | target update
31561 | target enter data | target exit data */
31563 static tree
31564 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31565 bool is_omp)
31567 tree t, c;
31568 enum tree_code if_modifier = ERROR_MARK;
31570 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31571 return list;
31573 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31575 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31576 const char *p = IDENTIFIER_POINTER (id);
31577 int n = 2;
31579 if (strcmp ("parallel", p) == 0)
31580 if_modifier = OMP_PARALLEL;
31581 else if (strcmp ("task", p) == 0)
31582 if_modifier = OMP_TASK;
31583 else if (strcmp ("taskloop", p) == 0)
31584 if_modifier = OMP_TASKLOOP;
31585 else if (strcmp ("target", p) == 0)
31587 if_modifier = OMP_TARGET;
31588 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31590 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31591 p = IDENTIFIER_POINTER (id);
31592 if (strcmp ("data", p) == 0)
31593 if_modifier = OMP_TARGET_DATA;
31594 else if (strcmp ("update", p) == 0)
31595 if_modifier = OMP_TARGET_UPDATE;
31596 else if (strcmp ("enter", p) == 0)
31597 if_modifier = OMP_TARGET_ENTER_DATA;
31598 else if (strcmp ("exit", p) == 0)
31599 if_modifier = OMP_TARGET_EXIT_DATA;
31600 if (if_modifier != OMP_TARGET)
31601 n = 3;
31602 else
31604 location_t loc
31605 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31606 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31607 "or %<exit%>");
31608 if_modifier = ERROR_MARK;
31610 if (if_modifier == OMP_TARGET_ENTER_DATA
31611 || if_modifier == OMP_TARGET_EXIT_DATA)
31613 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31615 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31616 p = IDENTIFIER_POINTER (id);
31617 if (strcmp ("data", p) == 0)
31618 n = 4;
31620 if (n != 4)
31622 location_t loc
31623 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31624 error_at (loc, "expected %<data%>");
31625 if_modifier = ERROR_MARK;
31630 if (if_modifier != ERROR_MARK)
31632 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31634 while (n-- > 0)
31635 cp_lexer_consume_token (parser->lexer);
31637 else
31639 if (n > 2)
31641 location_t loc
31642 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31643 error_at (loc, "expected %<:%>");
31645 if_modifier = ERROR_MARK;
31650 t = cp_parser_condition (parser);
31652 if (t == error_mark_node
31653 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31654 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31655 /*or_comma=*/false,
31656 /*consume_paren=*/true);
31658 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31659 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31661 if (if_modifier != ERROR_MARK
31662 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31664 const char *p = NULL;
31665 switch (if_modifier)
31667 case OMP_PARALLEL: p = "parallel"; break;
31668 case OMP_TASK: p = "task"; break;
31669 case OMP_TASKLOOP: p = "taskloop"; break;
31670 case OMP_TARGET_DATA: p = "target data"; break;
31671 case OMP_TARGET: p = "target"; break;
31672 case OMP_TARGET_UPDATE: p = "target update"; break;
31673 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
31674 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
31675 default: gcc_unreachable ();
31677 error_at (location, "too many %<if%> clauses with %qs modifier",
31679 return list;
31681 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31683 if (!is_omp)
31684 error_at (location, "too many %<if%> clauses");
31685 else
31686 error_at (location, "too many %<if%> clauses without modifier");
31687 return list;
31689 else if (if_modifier == ERROR_MARK
31690 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
31692 error_at (location, "if any %<if%> clause has modifier, then all "
31693 "%<if%> clauses have to use modifier");
31694 return list;
31698 c = build_omp_clause (location, OMP_CLAUSE_IF);
31699 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
31700 OMP_CLAUSE_IF_EXPR (c) = t;
31701 OMP_CLAUSE_CHAIN (c) = list;
31703 return c;
31706 /* OpenMP 3.1:
31707 mergeable */
31709 static tree
31710 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
31711 tree list, location_t location)
31713 tree c;
31715 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
31716 location);
31718 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
31719 OMP_CLAUSE_CHAIN (c) = list;
31720 return c;
31723 /* OpenMP 2.5:
31724 nowait */
31726 static tree
31727 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
31728 tree list, location_t location)
31730 tree c;
31732 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
31734 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
31735 OMP_CLAUSE_CHAIN (c) = list;
31736 return c;
31739 /* OpenMP 2.5:
31740 num_threads ( expression ) */
31742 static tree
31743 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
31744 location_t location)
31746 tree t, c;
31748 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31749 return list;
31751 t = cp_parser_expression (parser);
31753 if (t == error_mark_node
31754 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31755 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31756 /*or_comma=*/false,
31757 /*consume_paren=*/true);
31759 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
31760 "num_threads", location);
31762 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
31763 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
31764 OMP_CLAUSE_CHAIN (c) = list;
31766 return c;
31769 /* OpenMP 4.5:
31770 num_tasks ( expression ) */
31772 static tree
31773 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
31774 location_t location)
31776 tree t, c;
31778 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31779 return list;
31781 t = cp_parser_expression (parser);
31783 if (t == error_mark_node
31784 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31785 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31786 /*or_comma=*/false,
31787 /*consume_paren=*/true);
31789 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
31790 "num_tasks", location);
31792 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
31793 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
31794 OMP_CLAUSE_CHAIN (c) = list;
31796 return c;
31799 /* OpenMP 4.5:
31800 grainsize ( expression ) */
31802 static tree
31803 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
31804 location_t location)
31806 tree t, c;
31808 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31809 return list;
31811 t = cp_parser_expression (parser);
31813 if (t == error_mark_node
31814 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31815 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31816 /*or_comma=*/false,
31817 /*consume_paren=*/true);
31819 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
31820 "grainsize", location);
31822 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
31823 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
31824 OMP_CLAUSE_CHAIN (c) = list;
31826 return c;
31829 /* OpenMP 4.5:
31830 priority ( expression ) */
31832 static tree
31833 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
31834 location_t location)
31836 tree t, c;
31838 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31839 return list;
31841 t = cp_parser_expression (parser);
31843 if (t == error_mark_node
31844 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31845 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31846 /*or_comma=*/false,
31847 /*consume_paren=*/true);
31849 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
31850 "priority", location);
31852 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
31853 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
31854 OMP_CLAUSE_CHAIN (c) = list;
31856 return c;
31859 /* OpenMP 4.5:
31860 hint ( expression ) */
31862 static tree
31863 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
31864 location_t location)
31866 tree t, c;
31868 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31869 return list;
31871 t = cp_parser_expression (parser);
31873 if (t == error_mark_node
31874 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31875 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31876 /*or_comma=*/false,
31877 /*consume_paren=*/true);
31879 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31881 c = build_omp_clause (location, OMP_CLAUSE_HINT);
31882 OMP_CLAUSE_HINT_EXPR (c) = t;
31883 OMP_CLAUSE_CHAIN (c) = list;
31885 return c;
31888 /* OpenMP 4.5:
31889 defaultmap ( tofrom : scalar ) */
31891 static tree
31892 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31893 location_t location)
31895 tree c, id;
31896 const char *p;
31898 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31899 return list;
31901 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31903 cp_parser_error (parser, "expected %<tofrom%>");
31904 goto out_err;
31906 id = cp_lexer_peek_token (parser->lexer)->u.value;
31907 p = IDENTIFIER_POINTER (id);
31908 if (strcmp (p, "tofrom") != 0)
31910 cp_parser_error (parser, "expected %<tofrom%>");
31911 goto out_err;
31913 cp_lexer_consume_token (parser->lexer);
31914 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31915 goto out_err;
31917 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31919 cp_parser_error (parser, "expected %<scalar%>");
31920 goto out_err;
31922 id = cp_lexer_peek_token (parser->lexer)->u.value;
31923 p = IDENTIFIER_POINTER (id);
31924 if (strcmp (p, "scalar") != 0)
31926 cp_parser_error (parser, "expected %<scalar%>");
31927 goto out_err;
31929 cp_lexer_consume_token (parser->lexer);
31930 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31931 goto out_err;
31933 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31934 location);
31936 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31937 OMP_CLAUSE_CHAIN (c) = list;
31938 return c;
31940 out_err:
31941 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31942 /*or_comma=*/false,
31943 /*consume_paren=*/true);
31944 return list;
31947 /* OpenMP 2.5:
31948 ordered
31950 OpenMP 4.5:
31951 ordered ( constant-expression ) */
31953 static tree
31954 cp_parser_omp_clause_ordered (cp_parser *parser,
31955 tree list, location_t location)
31957 tree c, num = NULL_TREE;
31958 HOST_WIDE_INT n;
31960 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31961 "ordered", location);
31963 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31965 cp_lexer_consume_token (parser->lexer);
31967 num = cp_parser_constant_expression (parser);
31969 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31970 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31971 /*or_comma=*/false,
31972 /*consume_paren=*/true);
31974 if (num == error_mark_node)
31975 return list;
31976 num = fold_non_dependent_expr (num);
31977 if (!tree_fits_shwi_p (num)
31978 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31979 || (n = tree_to_shwi (num)) <= 0
31980 || (int) n != n)
31982 error_at (location,
31983 "ordered argument needs positive constant integer "
31984 "expression");
31985 return list;
31989 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31990 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31991 OMP_CLAUSE_CHAIN (c) = list;
31992 return c;
31995 /* OpenMP 2.5:
31996 reduction ( reduction-operator : variable-list )
31998 reduction-operator:
31999 One of: + * - & ^ | && ||
32001 OpenMP 3.1:
32003 reduction-operator:
32004 One of: + * - & ^ | && || min max
32006 OpenMP 4.0:
32008 reduction-operator:
32009 One of: + * - & ^ | && ||
32010 id-expression */
32012 static tree
32013 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32015 enum tree_code code = ERROR_MARK;
32016 tree nlist, c, id = NULL_TREE;
32018 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32019 return list;
32021 switch (cp_lexer_peek_token (parser->lexer)->type)
32023 case CPP_PLUS: code = PLUS_EXPR; break;
32024 case CPP_MULT: code = MULT_EXPR; break;
32025 case CPP_MINUS: code = MINUS_EXPR; break;
32026 case CPP_AND: code = BIT_AND_EXPR; break;
32027 case CPP_XOR: code = BIT_XOR_EXPR; break;
32028 case CPP_OR: code = BIT_IOR_EXPR; break;
32029 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32030 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32031 default: break;
32034 if (code != ERROR_MARK)
32035 cp_lexer_consume_token (parser->lexer);
32036 else
32038 bool saved_colon_corrects_to_scope_p;
32039 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32040 parser->colon_corrects_to_scope_p = false;
32041 id = cp_parser_id_expression (parser, /*template_p=*/false,
32042 /*check_dependency_p=*/true,
32043 /*template_p=*/NULL,
32044 /*declarator_p=*/false,
32045 /*optional_p=*/false);
32046 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32047 if (identifier_p (id))
32049 const char *p = IDENTIFIER_POINTER (id);
32051 if (strcmp (p, "min") == 0)
32052 code = MIN_EXPR;
32053 else if (strcmp (p, "max") == 0)
32054 code = MAX_EXPR;
32055 else if (id == cp_operator_id (PLUS_EXPR))
32056 code = PLUS_EXPR;
32057 else if (id == cp_operator_id (MULT_EXPR))
32058 code = MULT_EXPR;
32059 else if (id == cp_operator_id (MINUS_EXPR))
32060 code = MINUS_EXPR;
32061 else if (id == cp_operator_id (BIT_AND_EXPR))
32062 code = BIT_AND_EXPR;
32063 else if (id == cp_operator_id (BIT_IOR_EXPR))
32064 code = BIT_IOR_EXPR;
32065 else if (id == cp_operator_id (BIT_XOR_EXPR))
32066 code = BIT_XOR_EXPR;
32067 else if (id == cp_operator_id (TRUTH_ANDIF_EXPR))
32068 code = TRUTH_ANDIF_EXPR;
32069 else if (id == cp_operator_id (TRUTH_ORIF_EXPR))
32070 code = TRUTH_ORIF_EXPR;
32071 id = omp_reduction_id (code, id, NULL_TREE);
32072 tree scope = parser->scope;
32073 if (scope)
32074 id = build_qualified_name (NULL_TREE, scope, id, false);
32075 parser->scope = NULL_TREE;
32076 parser->qualifying_scope = NULL_TREE;
32077 parser->object_scope = NULL_TREE;
32079 else
32081 error ("invalid reduction-identifier");
32082 resync_fail:
32083 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32084 /*or_comma=*/false,
32085 /*consume_paren=*/true);
32086 return list;
32090 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32091 goto resync_fail;
32093 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32094 NULL);
32095 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32097 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32098 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32101 return nlist;
32104 /* OpenMP 2.5:
32105 schedule ( schedule-kind )
32106 schedule ( schedule-kind , expression )
32108 schedule-kind:
32109 static | dynamic | guided | runtime | auto
32111 OpenMP 4.5:
32112 schedule ( schedule-modifier : schedule-kind )
32113 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32115 schedule-modifier:
32116 simd
32117 monotonic
32118 nonmonotonic */
32120 static tree
32121 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32123 tree c, t;
32124 int modifiers = 0, nmodifiers = 0;
32126 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32127 return list;
32129 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32131 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32133 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32134 const char *p = IDENTIFIER_POINTER (id);
32135 if (strcmp ("simd", p) == 0)
32136 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32137 else if (strcmp ("monotonic", p) == 0)
32138 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32139 else if (strcmp ("nonmonotonic", p) == 0)
32140 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32141 else
32142 break;
32143 cp_lexer_consume_token (parser->lexer);
32144 if (nmodifiers++ == 0
32145 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32146 cp_lexer_consume_token (parser->lexer);
32147 else
32149 cp_parser_require (parser, CPP_COLON, RT_COLON);
32150 break;
32154 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32156 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32157 const char *p = IDENTIFIER_POINTER (id);
32159 switch (p[0])
32161 case 'd':
32162 if (strcmp ("dynamic", p) != 0)
32163 goto invalid_kind;
32164 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32165 break;
32167 case 'g':
32168 if (strcmp ("guided", p) != 0)
32169 goto invalid_kind;
32170 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32171 break;
32173 case 'r':
32174 if (strcmp ("runtime", p) != 0)
32175 goto invalid_kind;
32176 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32177 break;
32179 default:
32180 goto invalid_kind;
32183 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32184 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32185 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32186 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32187 else
32188 goto invalid_kind;
32189 cp_lexer_consume_token (parser->lexer);
32191 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32192 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32193 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32194 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32196 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32197 "specified");
32198 modifiers = 0;
32201 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32203 cp_token *token;
32204 cp_lexer_consume_token (parser->lexer);
32206 token = cp_lexer_peek_token (parser->lexer);
32207 t = cp_parser_assignment_expression (parser);
32209 if (t == error_mark_node)
32210 goto resync_fail;
32211 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32212 error_at (token->location, "schedule %<runtime%> does not take "
32213 "a %<chunk_size%> parameter");
32214 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32215 error_at (token->location, "schedule %<auto%> does not take "
32216 "a %<chunk_size%> parameter");
32217 else
32218 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32220 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32221 goto resync_fail;
32223 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32224 goto resync_fail;
32226 OMP_CLAUSE_SCHEDULE_KIND (c)
32227 = (enum omp_clause_schedule_kind)
32228 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32230 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32231 OMP_CLAUSE_CHAIN (c) = list;
32232 return c;
32234 invalid_kind:
32235 cp_parser_error (parser, "invalid schedule kind");
32236 resync_fail:
32237 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32238 /*or_comma=*/false,
32239 /*consume_paren=*/true);
32240 return list;
32243 /* OpenMP 3.0:
32244 untied */
32246 static tree
32247 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32248 tree list, location_t location)
32250 tree c;
32252 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32254 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32255 OMP_CLAUSE_CHAIN (c) = list;
32256 return c;
32259 /* OpenMP 4.0:
32260 inbranch
32261 notinbranch */
32263 static tree
32264 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32265 tree list, location_t location)
32267 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32268 tree c = build_omp_clause (location, code);
32269 OMP_CLAUSE_CHAIN (c) = list;
32270 return c;
32273 /* OpenMP 4.0:
32274 parallel
32276 sections
32277 taskgroup */
32279 static tree
32280 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32281 enum omp_clause_code code,
32282 tree list, location_t location)
32284 tree c = build_omp_clause (location, code);
32285 OMP_CLAUSE_CHAIN (c) = list;
32286 return c;
32289 /* OpenMP 4.5:
32290 nogroup */
32292 static tree
32293 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32294 tree list, location_t location)
32296 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32297 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32298 OMP_CLAUSE_CHAIN (c) = list;
32299 return c;
32302 /* OpenMP 4.5:
32303 simd
32304 threads */
32306 static tree
32307 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32308 enum omp_clause_code code,
32309 tree list, location_t location)
32311 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32312 tree c = build_omp_clause (location, code);
32313 OMP_CLAUSE_CHAIN (c) = list;
32314 return c;
32317 /* OpenMP 4.0:
32318 num_teams ( expression ) */
32320 static tree
32321 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32322 location_t location)
32324 tree t, c;
32326 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32327 return list;
32329 t = cp_parser_expression (parser);
32331 if (t == error_mark_node
32332 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32333 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32334 /*or_comma=*/false,
32335 /*consume_paren=*/true);
32337 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32338 "num_teams", location);
32340 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32341 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32342 OMP_CLAUSE_CHAIN (c) = list;
32344 return c;
32347 /* OpenMP 4.0:
32348 thread_limit ( expression ) */
32350 static tree
32351 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32352 location_t location)
32354 tree t, c;
32356 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32357 return list;
32359 t = cp_parser_expression (parser);
32361 if (t == error_mark_node
32362 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32363 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32364 /*or_comma=*/false,
32365 /*consume_paren=*/true);
32367 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32368 "thread_limit", location);
32370 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32371 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32372 OMP_CLAUSE_CHAIN (c) = list;
32374 return c;
32377 /* OpenMP 4.0:
32378 aligned ( variable-list )
32379 aligned ( variable-list : constant-expression ) */
32381 static tree
32382 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32384 tree nlist, c, alignment = NULL_TREE;
32385 bool colon;
32387 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32388 return list;
32390 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32391 &colon);
32393 if (colon)
32395 alignment = cp_parser_constant_expression (parser);
32397 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32398 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32399 /*or_comma=*/false,
32400 /*consume_paren=*/true);
32402 if (alignment == error_mark_node)
32403 alignment = NULL_TREE;
32406 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32407 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32409 return nlist;
32412 /* OpenMP 4.0:
32413 linear ( variable-list )
32414 linear ( variable-list : expression )
32416 OpenMP 4.5:
32417 linear ( modifier ( variable-list ) )
32418 linear ( modifier ( variable-list ) : expression ) */
32420 static tree
32421 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32422 bool is_cilk_simd_fn, bool declare_simd)
32424 tree nlist, c, step = integer_one_node;
32425 bool colon;
32426 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32428 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32429 return list;
32431 if (!is_cilk_simd_fn
32432 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32434 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32435 const char *p = IDENTIFIER_POINTER (id);
32437 if (strcmp ("ref", p) == 0)
32438 kind = OMP_CLAUSE_LINEAR_REF;
32439 else if (strcmp ("val", p) == 0)
32440 kind = OMP_CLAUSE_LINEAR_VAL;
32441 else if (strcmp ("uval", p) == 0)
32442 kind = OMP_CLAUSE_LINEAR_UVAL;
32443 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32444 cp_lexer_consume_token (parser->lexer);
32445 else
32446 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32449 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32450 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32451 &colon);
32452 else
32454 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32455 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32456 if (colon)
32457 cp_parser_require (parser, CPP_COLON, RT_COLON);
32458 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32459 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32460 /*or_comma=*/false,
32461 /*consume_paren=*/true);
32464 if (colon)
32466 step = NULL_TREE;
32467 if (declare_simd
32468 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32469 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32471 cp_token *token = cp_lexer_peek_token (parser->lexer);
32472 cp_parser_parse_tentatively (parser);
32473 step = cp_parser_id_expression (parser, /*template_p=*/false,
32474 /*check_dependency_p=*/true,
32475 /*template_p=*/NULL,
32476 /*declarator_p=*/false,
32477 /*optional_p=*/false);
32478 if (step != error_mark_node)
32479 step = cp_parser_lookup_name_simple (parser, step, token->location);
32480 if (step == error_mark_node)
32482 step = NULL_TREE;
32483 cp_parser_abort_tentative_parse (parser);
32485 else if (!cp_parser_parse_definitely (parser))
32486 step = NULL_TREE;
32488 if (!step)
32489 step = cp_parser_expression (parser);
32491 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
32493 sorry ("using parameters for %<linear%> step is not supported yet");
32494 step = integer_one_node;
32496 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32497 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32498 /*or_comma=*/false,
32499 /*consume_paren=*/true);
32501 if (step == error_mark_node)
32502 return list;
32505 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32507 OMP_CLAUSE_LINEAR_STEP (c) = step;
32508 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32511 return nlist;
32514 /* OpenMP 4.0:
32515 safelen ( constant-expression ) */
32517 static tree
32518 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32519 location_t location)
32521 tree t, c;
32523 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32524 return list;
32526 t = cp_parser_constant_expression (parser);
32528 if (t == error_mark_node
32529 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32530 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32531 /*or_comma=*/false,
32532 /*consume_paren=*/true);
32534 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32536 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32537 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32538 OMP_CLAUSE_CHAIN (c) = list;
32540 return c;
32543 /* OpenMP 4.0:
32544 simdlen ( constant-expression ) */
32546 static tree
32547 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32548 location_t location)
32550 tree t, c;
32552 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32553 return list;
32555 t = cp_parser_constant_expression (parser);
32557 if (t == error_mark_node
32558 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32559 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32560 /*or_comma=*/false,
32561 /*consume_paren=*/true);
32563 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32565 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32566 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32567 OMP_CLAUSE_CHAIN (c) = list;
32569 return c;
32572 /* OpenMP 4.5:
32573 vec:
32574 identifier [+/- integer]
32575 vec , identifier [+/- integer]
32578 static tree
32579 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32580 tree list)
32582 tree vec = NULL;
32584 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32586 cp_parser_error (parser, "expected identifier");
32587 return list;
32590 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32592 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32593 tree t, identifier = cp_parser_identifier (parser);
32594 tree addend = NULL;
32596 if (identifier == error_mark_node)
32597 t = error_mark_node;
32598 else
32600 t = cp_parser_lookup_name_simple
32601 (parser, identifier,
32602 cp_lexer_peek_token (parser->lexer)->location);
32603 if (t == error_mark_node)
32604 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32605 id_loc);
32608 bool neg = false;
32609 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32610 neg = true;
32611 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32613 addend = integer_zero_node;
32614 goto add_to_vector;
32616 cp_lexer_consume_token (parser->lexer);
32618 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32620 cp_parser_error (parser, "expected integer");
32621 return list;
32624 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32625 if (TREE_CODE (addend) != INTEGER_CST)
32627 cp_parser_error (parser, "expected integer");
32628 return list;
32630 cp_lexer_consume_token (parser->lexer);
32632 add_to_vector:
32633 if (t != error_mark_node)
32635 vec = tree_cons (addend, t, vec);
32636 if (neg)
32637 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32640 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32641 break;
32643 cp_lexer_consume_token (parser->lexer);
32646 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32648 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32649 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32650 OMP_CLAUSE_DECL (u) = nreverse (vec);
32651 OMP_CLAUSE_CHAIN (u) = list;
32652 return u;
32654 return list;
32657 /* OpenMP 4.0:
32658 depend ( depend-kind : variable-list )
32660 depend-kind:
32661 in | out | inout
32663 OpenMP 4.5:
32664 depend ( source )
32666 depend ( sink : vec ) */
32668 static tree
32669 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
32671 tree nlist, c;
32672 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
32674 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32675 return list;
32677 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32679 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32680 const char *p = IDENTIFIER_POINTER (id);
32682 if (strcmp ("in", p) == 0)
32683 kind = OMP_CLAUSE_DEPEND_IN;
32684 else if (strcmp ("inout", p) == 0)
32685 kind = OMP_CLAUSE_DEPEND_INOUT;
32686 else if (strcmp ("out", p) == 0)
32687 kind = OMP_CLAUSE_DEPEND_OUT;
32688 else if (strcmp ("source", p) == 0)
32689 kind = OMP_CLAUSE_DEPEND_SOURCE;
32690 else if (strcmp ("sink", p) == 0)
32691 kind = OMP_CLAUSE_DEPEND_SINK;
32692 else
32693 goto invalid_kind;
32695 else
32696 goto invalid_kind;
32698 cp_lexer_consume_token (parser->lexer);
32700 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
32702 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
32703 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32704 OMP_CLAUSE_DECL (c) = NULL_TREE;
32705 OMP_CLAUSE_CHAIN (c) = list;
32706 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32707 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32708 /*or_comma=*/false,
32709 /*consume_paren=*/true);
32710 return c;
32713 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32714 goto resync_fail;
32716 if (kind == OMP_CLAUSE_DEPEND_SINK)
32717 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
32718 else
32720 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
32721 list, NULL);
32723 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32724 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32726 return nlist;
32728 invalid_kind:
32729 cp_parser_error (parser, "invalid depend kind");
32730 resync_fail:
32731 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32732 /*or_comma=*/false,
32733 /*consume_paren=*/true);
32734 return list;
32737 /* OpenMP 4.0:
32738 map ( map-kind : variable-list )
32739 map ( variable-list )
32741 map-kind:
32742 alloc | to | from | tofrom
32744 OpenMP 4.5:
32745 map-kind:
32746 alloc | to | from | tofrom | release | delete
32748 map ( always [,] map-kind: variable-list ) */
32750 static tree
32751 cp_parser_omp_clause_map (cp_parser *parser, tree list)
32753 tree nlist, c;
32754 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
32755 bool always = false;
32757 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32758 return list;
32760 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32762 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32763 const char *p = IDENTIFIER_POINTER (id);
32765 if (strcmp ("always", p) == 0)
32767 int nth = 2;
32768 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
32769 nth++;
32770 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
32771 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
32772 == RID_DELETE))
32773 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
32774 == CPP_COLON))
32776 always = true;
32777 cp_lexer_consume_token (parser->lexer);
32778 if (nth == 3)
32779 cp_lexer_consume_token (parser->lexer);
32784 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32785 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32787 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32788 const char *p = IDENTIFIER_POINTER (id);
32790 if (strcmp ("alloc", p) == 0)
32791 kind = GOMP_MAP_ALLOC;
32792 else if (strcmp ("to", p) == 0)
32793 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
32794 else if (strcmp ("from", p) == 0)
32795 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
32796 else if (strcmp ("tofrom", p) == 0)
32797 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
32798 else if (strcmp ("release", p) == 0)
32799 kind = GOMP_MAP_RELEASE;
32800 else
32802 cp_parser_error (parser, "invalid map kind");
32803 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32804 /*or_comma=*/false,
32805 /*consume_paren=*/true);
32806 return list;
32808 cp_lexer_consume_token (parser->lexer);
32809 cp_lexer_consume_token (parser->lexer);
32811 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
32812 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32814 kind = GOMP_MAP_DELETE;
32815 cp_lexer_consume_token (parser->lexer);
32816 cp_lexer_consume_token (parser->lexer);
32819 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
32820 NULL);
32822 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32823 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32825 return nlist;
32828 /* OpenMP 4.0:
32829 device ( expression ) */
32831 static tree
32832 cp_parser_omp_clause_device (cp_parser *parser, tree list,
32833 location_t location)
32835 tree t, c;
32837 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32838 return list;
32840 t = cp_parser_expression (parser);
32842 if (t == error_mark_node
32843 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32844 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32845 /*or_comma=*/false,
32846 /*consume_paren=*/true);
32848 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
32849 "device", location);
32851 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
32852 OMP_CLAUSE_DEVICE_ID (c) = t;
32853 OMP_CLAUSE_CHAIN (c) = list;
32855 return c;
32858 /* OpenMP 4.0:
32859 dist_schedule ( static )
32860 dist_schedule ( static , expression ) */
32862 static tree
32863 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
32864 location_t location)
32866 tree c, t;
32868 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32869 return list;
32871 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32873 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32874 goto invalid_kind;
32875 cp_lexer_consume_token (parser->lexer);
32877 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32879 cp_lexer_consume_token (parser->lexer);
32881 t = cp_parser_assignment_expression (parser);
32883 if (t == error_mark_node)
32884 goto resync_fail;
32885 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32887 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32888 goto resync_fail;
32890 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32891 goto resync_fail;
32893 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32894 location);
32895 OMP_CLAUSE_CHAIN (c) = list;
32896 return c;
32898 invalid_kind:
32899 cp_parser_error (parser, "invalid dist_schedule kind");
32900 resync_fail:
32901 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32902 /*or_comma=*/false,
32903 /*consume_paren=*/true);
32904 return list;
32907 /* OpenMP 4.0:
32908 proc_bind ( proc-bind-kind )
32910 proc-bind-kind:
32911 master | close | spread */
32913 static tree
32914 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32915 location_t location)
32917 tree c;
32918 enum omp_clause_proc_bind_kind kind;
32920 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32921 return list;
32923 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32925 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32926 const char *p = IDENTIFIER_POINTER (id);
32928 if (strcmp ("master", p) == 0)
32929 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32930 else if (strcmp ("close", p) == 0)
32931 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32932 else if (strcmp ("spread", p) == 0)
32933 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32934 else
32935 goto invalid_kind;
32937 else
32938 goto invalid_kind;
32940 cp_lexer_consume_token (parser->lexer);
32941 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32942 goto resync_fail;
32944 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32945 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32946 location);
32947 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32948 OMP_CLAUSE_CHAIN (c) = list;
32949 return c;
32951 invalid_kind:
32952 cp_parser_error (parser, "invalid depend kind");
32953 resync_fail:
32954 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32955 /*or_comma=*/false,
32956 /*consume_paren=*/true);
32957 return list;
32960 /* OpenACC:
32961 async [( int-expr )] */
32963 static tree
32964 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32966 tree c, t;
32967 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32969 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32971 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32973 cp_lexer_consume_token (parser->lexer);
32975 t = cp_parser_expression (parser);
32976 if (t == error_mark_node
32977 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32978 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32979 /*or_comma=*/false,
32980 /*consume_paren=*/true);
32983 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32985 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32986 OMP_CLAUSE_ASYNC_EXPR (c) = t;
32987 OMP_CLAUSE_CHAIN (c) = list;
32988 list = c;
32990 return list;
32993 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32994 is a bitmask in MASK. Return the list of clauses found. */
32996 static tree
32997 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32998 const char *where, cp_token *pragma_tok,
32999 bool finish_p = true)
33001 tree clauses = NULL;
33002 bool first = true;
33004 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33006 location_t here;
33007 pragma_omp_clause c_kind;
33008 omp_clause_code code;
33009 const char *c_name;
33010 tree prev = clauses;
33012 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33013 cp_lexer_consume_token (parser->lexer);
33015 here = cp_lexer_peek_token (parser->lexer)->location;
33016 c_kind = cp_parser_omp_clause_name (parser);
33018 switch (c_kind)
33020 case PRAGMA_OACC_CLAUSE_ASYNC:
33021 clauses = cp_parser_oacc_clause_async (parser, clauses);
33022 c_name = "async";
33023 break;
33024 case PRAGMA_OACC_CLAUSE_AUTO:
33025 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33026 clauses, here);
33027 c_name = "auto";
33028 break;
33029 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33030 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33031 c_name = "collapse";
33032 break;
33033 case PRAGMA_OACC_CLAUSE_COPY:
33034 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33035 c_name = "copy";
33036 break;
33037 case PRAGMA_OACC_CLAUSE_COPYIN:
33038 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33039 c_name = "copyin";
33040 break;
33041 case PRAGMA_OACC_CLAUSE_COPYOUT:
33042 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33043 c_name = "copyout";
33044 break;
33045 case PRAGMA_OACC_CLAUSE_CREATE:
33046 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33047 c_name = "create";
33048 break;
33049 case PRAGMA_OACC_CLAUSE_DELETE:
33050 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33051 c_name = "delete";
33052 break;
33053 case PRAGMA_OMP_CLAUSE_DEFAULT:
33054 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33055 c_name = "default";
33056 break;
33057 case PRAGMA_OACC_CLAUSE_DEVICE:
33058 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33059 c_name = "device";
33060 break;
33061 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33062 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33063 c_name = "deviceptr";
33064 break;
33065 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33066 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33067 c_name = "device_resident";
33068 break;
33069 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33070 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33071 clauses);
33072 c_name = "firstprivate";
33073 break;
33074 case PRAGMA_OACC_CLAUSE_GANG:
33075 c_name = "gang";
33076 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33077 c_name, clauses);
33078 break;
33079 case PRAGMA_OACC_CLAUSE_HOST:
33080 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33081 c_name = "host";
33082 break;
33083 case PRAGMA_OACC_CLAUSE_IF:
33084 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33085 c_name = "if";
33086 break;
33087 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33088 clauses = cp_parser_oacc_simple_clause (parser,
33089 OMP_CLAUSE_INDEPENDENT,
33090 clauses, here);
33091 c_name = "independent";
33092 break;
33093 case PRAGMA_OACC_CLAUSE_LINK:
33094 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33095 c_name = "link";
33096 break;
33097 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33098 code = OMP_CLAUSE_NUM_GANGS;
33099 c_name = "num_gangs";
33100 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33101 clauses);
33102 break;
33103 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33104 c_name = "num_workers";
33105 code = OMP_CLAUSE_NUM_WORKERS;
33106 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33107 clauses);
33108 break;
33109 case PRAGMA_OACC_CLAUSE_PRESENT:
33110 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33111 c_name = "present";
33112 break;
33113 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33114 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33115 c_name = "present_or_copy";
33116 break;
33117 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33118 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33119 c_name = "present_or_copyin";
33120 break;
33121 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33122 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33123 c_name = "present_or_copyout";
33124 break;
33125 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33126 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33127 c_name = "present_or_create";
33128 break;
33129 case PRAGMA_OACC_CLAUSE_PRIVATE:
33130 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33131 clauses);
33132 c_name = "private";
33133 break;
33134 case PRAGMA_OACC_CLAUSE_REDUCTION:
33135 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33136 c_name = "reduction";
33137 break;
33138 case PRAGMA_OACC_CLAUSE_SELF:
33139 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33140 c_name = "self";
33141 break;
33142 case PRAGMA_OACC_CLAUSE_SEQ:
33143 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33144 clauses, here);
33145 c_name = "seq";
33146 break;
33147 case PRAGMA_OACC_CLAUSE_TILE:
33148 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33149 c_name = "tile";
33150 break;
33151 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33152 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33153 clauses);
33154 c_name = "use_device";
33155 break;
33156 case PRAGMA_OACC_CLAUSE_VECTOR:
33157 c_name = "vector";
33158 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33159 c_name, clauses);
33160 break;
33161 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33162 c_name = "vector_length";
33163 code = OMP_CLAUSE_VECTOR_LENGTH;
33164 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33165 clauses);
33166 break;
33167 case PRAGMA_OACC_CLAUSE_WAIT:
33168 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33169 c_name = "wait";
33170 break;
33171 case PRAGMA_OACC_CLAUSE_WORKER:
33172 c_name = "worker";
33173 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33174 c_name, clauses);
33175 break;
33176 default:
33177 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33178 goto saw_error;
33181 first = false;
33183 if (((mask >> c_kind) & 1) == 0)
33185 /* Remove the invalid clause(s) from the list to avoid
33186 confusing the rest of the compiler. */
33187 clauses = prev;
33188 error_at (here, "%qs is not valid for %qs", c_name, where);
33192 saw_error:
33193 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33195 if (finish_p)
33196 return finish_omp_clauses (clauses, C_ORT_ACC);
33198 return clauses;
33201 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33202 is a bitmask in MASK. Return the list of clauses found; the result
33203 of clause default goes in *pdefault. */
33205 static tree
33206 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33207 const char *where, cp_token *pragma_tok,
33208 bool finish_p = true)
33210 tree clauses = NULL;
33211 bool first = true;
33212 cp_token *token = NULL;
33214 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33216 pragma_omp_clause c_kind;
33217 const char *c_name;
33218 tree prev = clauses;
33220 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33221 cp_lexer_consume_token (parser->lexer);
33223 token = cp_lexer_peek_token (parser->lexer);
33224 c_kind = cp_parser_omp_clause_name (parser);
33226 switch (c_kind)
33228 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33229 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33230 token->location);
33231 c_name = "collapse";
33232 break;
33233 case PRAGMA_OMP_CLAUSE_COPYIN:
33234 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33235 c_name = "copyin";
33236 break;
33237 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33238 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33239 clauses);
33240 c_name = "copyprivate";
33241 break;
33242 case PRAGMA_OMP_CLAUSE_DEFAULT:
33243 clauses = cp_parser_omp_clause_default (parser, clauses,
33244 token->location, false);
33245 c_name = "default";
33246 break;
33247 case PRAGMA_OMP_CLAUSE_FINAL:
33248 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33249 c_name = "final";
33250 break;
33251 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33252 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33253 clauses);
33254 c_name = "firstprivate";
33255 break;
33256 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33257 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33258 token->location);
33259 c_name = "grainsize";
33260 break;
33261 case PRAGMA_OMP_CLAUSE_HINT:
33262 clauses = cp_parser_omp_clause_hint (parser, clauses,
33263 token->location);
33264 c_name = "hint";
33265 break;
33266 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33267 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33268 token->location);
33269 c_name = "defaultmap";
33270 break;
33271 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33272 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33273 clauses);
33274 c_name = "use_device_ptr";
33275 break;
33276 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33277 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33278 clauses);
33279 c_name = "is_device_ptr";
33280 break;
33281 case PRAGMA_OMP_CLAUSE_IF:
33282 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33283 true);
33284 c_name = "if";
33285 break;
33286 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33287 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33288 clauses);
33289 c_name = "lastprivate";
33290 break;
33291 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33292 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33293 token->location);
33294 c_name = "mergeable";
33295 break;
33296 case PRAGMA_OMP_CLAUSE_NOWAIT:
33297 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33298 c_name = "nowait";
33299 break;
33300 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33301 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33302 token->location);
33303 c_name = "num_tasks";
33304 break;
33305 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33306 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33307 token->location);
33308 c_name = "num_threads";
33309 break;
33310 case PRAGMA_OMP_CLAUSE_ORDERED:
33311 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33312 token->location);
33313 c_name = "ordered";
33314 break;
33315 case PRAGMA_OMP_CLAUSE_PRIORITY:
33316 clauses = cp_parser_omp_clause_priority (parser, clauses,
33317 token->location);
33318 c_name = "priority";
33319 break;
33320 case PRAGMA_OMP_CLAUSE_PRIVATE:
33321 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33322 clauses);
33323 c_name = "private";
33324 break;
33325 case PRAGMA_OMP_CLAUSE_REDUCTION:
33326 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33327 c_name = "reduction";
33328 break;
33329 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33330 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33331 token->location);
33332 c_name = "schedule";
33333 break;
33334 case PRAGMA_OMP_CLAUSE_SHARED:
33335 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33336 clauses);
33337 c_name = "shared";
33338 break;
33339 case PRAGMA_OMP_CLAUSE_UNTIED:
33340 clauses = cp_parser_omp_clause_untied (parser, clauses,
33341 token->location);
33342 c_name = "untied";
33343 break;
33344 case PRAGMA_OMP_CLAUSE_INBRANCH:
33345 case PRAGMA_CILK_CLAUSE_MASK:
33346 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33347 clauses, token->location);
33348 c_name = "inbranch";
33349 break;
33350 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33351 case PRAGMA_CILK_CLAUSE_NOMASK:
33352 clauses = cp_parser_omp_clause_branch (parser,
33353 OMP_CLAUSE_NOTINBRANCH,
33354 clauses, token->location);
33355 c_name = "notinbranch";
33356 break;
33357 case PRAGMA_OMP_CLAUSE_PARALLEL:
33358 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33359 clauses, token->location);
33360 c_name = "parallel";
33361 if (!first)
33363 clause_not_first:
33364 error_at (token->location, "%qs must be the first clause of %qs",
33365 c_name, where);
33366 clauses = prev;
33368 break;
33369 case PRAGMA_OMP_CLAUSE_FOR:
33370 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33371 clauses, token->location);
33372 c_name = "for";
33373 if (!first)
33374 goto clause_not_first;
33375 break;
33376 case PRAGMA_OMP_CLAUSE_SECTIONS:
33377 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33378 clauses, token->location);
33379 c_name = "sections";
33380 if (!first)
33381 goto clause_not_first;
33382 break;
33383 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33384 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33385 clauses, token->location);
33386 c_name = "taskgroup";
33387 if (!first)
33388 goto clause_not_first;
33389 break;
33390 case PRAGMA_OMP_CLAUSE_LINK:
33391 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33392 c_name = "to";
33393 break;
33394 case PRAGMA_OMP_CLAUSE_TO:
33395 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33396 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33397 clauses);
33398 else
33399 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33400 c_name = "to";
33401 break;
33402 case PRAGMA_OMP_CLAUSE_FROM:
33403 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33404 c_name = "from";
33405 break;
33406 case PRAGMA_OMP_CLAUSE_UNIFORM:
33407 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33408 clauses);
33409 c_name = "uniform";
33410 break;
33411 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33412 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33413 token->location);
33414 c_name = "num_teams";
33415 break;
33416 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33417 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33418 token->location);
33419 c_name = "thread_limit";
33420 break;
33421 case PRAGMA_OMP_CLAUSE_ALIGNED:
33422 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33423 c_name = "aligned";
33424 break;
33425 case PRAGMA_OMP_CLAUSE_LINEAR:
33427 bool cilk_simd_fn = false, declare_simd = false;
33428 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33429 cilk_simd_fn = true;
33430 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33431 declare_simd = true;
33432 clauses = cp_parser_omp_clause_linear (parser, clauses,
33433 cilk_simd_fn, declare_simd);
33435 c_name = "linear";
33436 break;
33437 case PRAGMA_OMP_CLAUSE_DEPEND:
33438 clauses = cp_parser_omp_clause_depend (parser, clauses,
33439 token->location);
33440 c_name = "depend";
33441 break;
33442 case PRAGMA_OMP_CLAUSE_MAP:
33443 clauses = cp_parser_omp_clause_map (parser, clauses);
33444 c_name = "map";
33445 break;
33446 case PRAGMA_OMP_CLAUSE_DEVICE:
33447 clauses = cp_parser_omp_clause_device (parser, clauses,
33448 token->location);
33449 c_name = "device";
33450 break;
33451 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33452 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33453 token->location);
33454 c_name = "dist_schedule";
33455 break;
33456 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33457 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33458 token->location);
33459 c_name = "proc_bind";
33460 break;
33461 case PRAGMA_OMP_CLAUSE_SAFELEN:
33462 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33463 token->location);
33464 c_name = "safelen";
33465 break;
33466 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33467 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33468 token->location);
33469 c_name = "simdlen";
33470 break;
33471 case PRAGMA_OMP_CLAUSE_NOGROUP:
33472 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33473 token->location);
33474 c_name = "nogroup";
33475 break;
33476 case PRAGMA_OMP_CLAUSE_THREADS:
33477 clauses
33478 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33479 clauses, token->location);
33480 c_name = "threads";
33481 break;
33482 case PRAGMA_OMP_CLAUSE_SIMD:
33483 clauses
33484 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33485 clauses, token->location);
33486 c_name = "simd";
33487 break;
33488 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
33489 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
33490 c_name = "simdlen";
33491 break;
33492 default:
33493 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33494 goto saw_error;
33497 first = false;
33499 if (((mask >> c_kind) & 1) == 0)
33501 /* Remove the invalid clause(s) from the list to avoid
33502 confusing the rest of the compiler. */
33503 clauses = prev;
33504 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33507 saw_error:
33508 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33509 no reason to skip to the end. */
33510 if (!(flag_cilkplus && pragma_tok == NULL))
33511 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33512 if (finish_p)
33514 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33515 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33516 else
33517 return finish_omp_clauses (clauses, C_ORT_OMP);
33519 return clauses;
33522 /* OpenMP 2.5:
33523 structured-block:
33524 statement
33526 In practice, we're also interested in adding the statement to an
33527 outer node. So it is convenient if we work around the fact that
33528 cp_parser_statement calls add_stmt. */
33530 static unsigned
33531 cp_parser_begin_omp_structured_block (cp_parser *parser)
33533 unsigned save = parser->in_statement;
33535 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33536 This preserves the "not within loop or switch" style error messages
33537 for nonsense cases like
33538 void foo() {
33539 #pragma omp single
33540 break;
33543 if (parser->in_statement)
33544 parser->in_statement = IN_OMP_BLOCK;
33546 return save;
33549 static void
33550 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33552 parser->in_statement = save;
33555 static tree
33556 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33558 tree stmt = begin_omp_structured_block ();
33559 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33561 cp_parser_statement (parser, NULL_TREE, false, if_p);
33563 cp_parser_end_omp_structured_block (parser, save);
33564 return finish_omp_structured_block (stmt);
33567 /* OpenMP 2.5:
33568 # pragma omp atomic new-line
33569 expression-stmt
33571 expression-stmt:
33572 x binop= expr | x++ | ++x | x-- | --x
33573 binop:
33574 +, *, -, /, &, ^, |, <<, >>
33576 where x is an lvalue expression with scalar type.
33578 OpenMP 3.1:
33579 # pragma omp atomic new-line
33580 update-stmt
33582 # pragma omp atomic read new-line
33583 read-stmt
33585 # pragma omp atomic write new-line
33586 write-stmt
33588 # pragma omp atomic update new-line
33589 update-stmt
33591 # pragma omp atomic capture new-line
33592 capture-stmt
33594 # pragma omp atomic capture new-line
33595 capture-block
33597 read-stmt:
33598 v = x
33599 write-stmt:
33600 x = expr
33601 update-stmt:
33602 expression-stmt | x = x binop expr
33603 capture-stmt:
33604 v = expression-stmt
33605 capture-block:
33606 { v = x; update-stmt; } | { update-stmt; v = x; }
33608 OpenMP 4.0:
33609 update-stmt:
33610 expression-stmt | x = x binop expr | x = expr binop x
33611 capture-stmt:
33612 v = update-stmt
33613 capture-block:
33614 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33616 where x and v are lvalue expressions with scalar type. */
33618 static void
33619 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33621 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33622 tree rhs1 = NULL_TREE, orig_lhs;
33623 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33624 bool structured_block = false;
33625 bool seq_cst = false;
33627 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33629 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33630 const char *p = IDENTIFIER_POINTER (id);
33632 if (!strcmp (p, "seq_cst"))
33634 seq_cst = true;
33635 cp_lexer_consume_token (parser->lexer);
33636 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33637 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33638 cp_lexer_consume_token (parser->lexer);
33641 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33643 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33644 const char *p = IDENTIFIER_POINTER (id);
33646 if (!strcmp (p, "read"))
33647 code = OMP_ATOMIC_READ;
33648 else if (!strcmp (p, "write"))
33649 code = NOP_EXPR;
33650 else if (!strcmp (p, "update"))
33651 code = OMP_ATOMIC;
33652 else if (!strcmp (p, "capture"))
33653 code = OMP_ATOMIC_CAPTURE_NEW;
33654 else
33655 p = NULL;
33656 if (p)
33657 cp_lexer_consume_token (parser->lexer);
33659 if (!seq_cst)
33661 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33662 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33663 cp_lexer_consume_token (parser->lexer);
33665 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33667 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33668 const char *p = IDENTIFIER_POINTER (id);
33670 if (!strcmp (p, "seq_cst"))
33672 seq_cst = true;
33673 cp_lexer_consume_token (parser->lexer);
33677 cp_parser_require_pragma_eol (parser, pragma_tok);
33679 switch (code)
33681 case OMP_ATOMIC_READ:
33682 case NOP_EXPR: /* atomic write */
33683 v = cp_parser_unary_expression (parser);
33684 if (v == error_mark_node)
33685 goto saw_error;
33686 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33687 goto saw_error;
33688 if (code == NOP_EXPR)
33689 lhs = cp_parser_expression (parser);
33690 else
33691 lhs = cp_parser_unary_expression (parser);
33692 if (lhs == error_mark_node)
33693 goto saw_error;
33694 if (code == NOP_EXPR)
33696 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33697 opcode. */
33698 code = OMP_ATOMIC;
33699 rhs = lhs;
33700 lhs = v;
33701 v = NULL_TREE;
33703 goto done;
33704 case OMP_ATOMIC_CAPTURE_NEW:
33705 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33707 cp_lexer_consume_token (parser->lexer);
33708 structured_block = true;
33710 else
33712 v = cp_parser_unary_expression (parser);
33713 if (v == error_mark_node)
33714 goto saw_error;
33715 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33716 goto saw_error;
33718 default:
33719 break;
33722 restart:
33723 lhs = cp_parser_unary_expression (parser);
33724 orig_lhs = lhs;
33725 switch (TREE_CODE (lhs))
33727 case ERROR_MARK:
33728 goto saw_error;
33730 case POSTINCREMENT_EXPR:
33731 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33732 code = OMP_ATOMIC_CAPTURE_OLD;
33733 /* FALLTHROUGH */
33734 case PREINCREMENT_EXPR:
33735 lhs = TREE_OPERAND (lhs, 0);
33736 opcode = PLUS_EXPR;
33737 rhs = integer_one_node;
33738 break;
33740 case POSTDECREMENT_EXPR:
33741 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33742 code = OMP_ATOMIC_CAPTURE_OLD;
33743 /* FALLTHROUGH */
33744 case PREDECREMENT_EXPR:
33745 lhs = TREE_OPERAND (lhs, 0);
33746 opcode = MINUS_EXPR;
33747 rhs = integer_one_node;
33748 break;
33750 case COMPOUND_EXPR:
33751 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
33752 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
33753 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
33754 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
33755 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33756 (TREE_OPERAND (lhs, 1), 0), 0)))
33757 == BOOLEAN_TYPE)
33758 /* Undo effects of boolean_increment for post {in,de}crement. */
33759 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
33760 /* FALLTHRU */
33761 case MODIFY_EXPR:
33762 if (TREE_CODE (lhs) == MODIFY_EXPR
33763 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
33765 /* Undo effects of boolean_increment. */
33766 if (integer_onep (TREE_OPERAND (lhs, 1)))
33768 /* This is pre or post increment. */
33769 rhs = TREE_OPERAND (lhs, 1);
33770 lhs = TREE_OPERAND (lhs, 0);
33771 opcode = NOP_EXPR;
33772 if (code == OMP_ATOMIC_CAPTURE_NEW
33773 && !structured_block
33774 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
33775 code = OMP_ATOMIC_CAPTURE_OLD;
33776 break;
33779 /* FALLTHRU */
33780 default:
33781 switch (cp_lexer_peek_token (parser->lexer)->type)
33783 case CPP_MULT_EQ:
33784 opcode = MULT_EXPR;
33785 break;
33786 case CPP_DIV_EQ:
33787 opcode = TRUNC_DIV_EXPR;
33788 break;
33789 case CPP_PLUS_EQ:
33790 opcode = PLUS_EXPR;
33791 break;
33792 case CPP_MINUS_EQ:
33793 opcode = MINUS_EXPR;
33794 break;
33795 case CPP_LSHIFT_EQ:
33796 opcode = LSHIFT_EXPR;
33797 break;
33798 case CPP_RSHIFT_EQ:
33799 opcode = RSHIFT_EXPR;
33800 break;
33801 case CPP_AND_EQ:
33802 opcode = BIT_AND_EXPR;
33803 break;
33804 case CPP_OR_EQ:
33805 opcode = BIT_IOR_EXPR;
33806 break;
33807 case CPP_XOR_EQ:
33808 opcode = BIT_XOR_EXPR;
33809 break;
33810 case CPP_EQ:
33811 enum cp_parser_prec oprec;
33812 cp_token *token;
33813 cp_lexer_consume_token (parser->lexer);
33814 cp_parser_parse_tentatively (parser);
33815 rhs1 = cp_parser_simple_cast_expression (parser);
33816 if (rhs1 == error_mark_node)
33818 cp_parser_abort_tentative_parse (parser);
33819 cp_parser_simple_cast_expression (parser);
33820 goto saw_error;
33822 token = cp_lexer_peek_token (parser->lexer);
33823 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
33825 cp_parser_abort_tentative_parse (parser);
33826 cp_parser_parse_tentatively (parser);
33827 rhs = cp_parser_binary_expression (parser, false, true,
33828 PREC_NOT_OPERATOR, NULL);
33829 if (rhs == error_mark_node)
33831 cp_parser_abort_tentative_parse (parser);
33832 cp_parser_binary_expression (parser, false, true,
33833 PREC_NOT_OPERATOR, NULL);
33834 goto saw_error;
33836 switch (TREE_CODE (rhs))
33838 case MULT_EXPR:
33839 case TRUNC_DIV_EXPR:
33840 case RDIV_EXPR:
33841 case PLUS_EXPR:
33842 case MINUS_EXPR:
33843 case LSHIFT_EXPR:
33844 case RSHIFT_EXPR:
33845 case BIT_AND_EXPR:
33846 case BIT_IOR_EXPR:
33847 case BIT_XOR_EXPR:
33848 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
33850 if (cp_parser_parse_definitely (parser))
33852 opcode = TREE_CODE (rhs);
33853 rhs1 = TREE_OPERAND (rhs, 0);
33854 rhs = TREE_OPERAND (rhs, 1);
33855 goto stmt_done;
33857 else
33858 goto saw_error;
33860 break;
33861 default:
33862 break;
33864 cp_parser_abort_tentative_parse (parser);
33865 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
33867 rhs = cp_parser_expression (parser);
33868 if (rhs == error_mark_node)
33869 goto saw_error;
33870 opcode = NOP_EXPR;
33871 rhs1 = NULL_TREE;
33872 goto stmt_done;
33874 cp_parser_error (parser,
33875 "invalid form of %<#pragma omp atomic%>");
33876 goto saw_error;
33878 if (!cp_parser_parse_definitely (parser))
33879 goto saw_error;
33880 switch (token->type)
33882 case CPP_SEMICOLON:
33883 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33885 code = OMP_ATOMIC_CAPTURE_OLD;
33886 v = lhs;
33887 lhs = NULL_TREE;
33888 lhs1 = rhs1;
33889 rhs1 = NULL_TREE;
33890 cp_lexer_consume_token (parser->lexer);
33891 goto restart;
33893 else if (structured_block)
33895 opcode = NOP_EXPR;
33896 rhs = rhs1;
33897 rhs1 = NULL_TREE;
33898 goto stmt_done;
33900 cp_parser_error (parser,
33901 "invalid form of %<#pragma omp atomic%>");
33902 goto saw_error;
33903 case CPP_MULT:
33904 opcode = MULT_EXPR;
33905 break;
33906 case CPP_DIV:
33907 opcode = TRUNC_DIV_EXPR;
33908 break;
33909 case CPP_PLUS:
33910 opcode = PLUS_EXPR;
33911 break;
33912 case CPP_MINUS:
33913 opcode = MINUS_EXPR;
33914 break;
33915 case CPP_LSHIFT:
33916 opcode = LSHIFT_EXPR;
33917 break;
33918 case CPP_RSHIFT:
33919 opcode = RSHIFT_EXPR;
33920 break;
33921 case CPP_AND:
33922 opcode = BIT_AND_EXPR;
33923 break;
33924 case CPP_OR:
33925 opcode = BIT_IOR_EXPR;
33926 break;
33927 case CPP_XOR:
33928 opcode = BIT_XOR_EXPR;
33929 break;
33930 default:
33931 cp_parser_error (parser,
33932 "invalid operator for %<#pragma omp atomic%>");
33933 goto saw_error;
33935 oprec = TOKEN_PRECEDENCE (token);
33936 gcc_assert (oprec != PREC_NOT_OPERATOR);
33937 if (commutative_tree_code (opcode))
33938 oprec = (enum cp_parser_prec) (oprec - 1);
33939 cp_lexer_consume_token (parser->lexer);
33940 rhs = cp_parser_binary_expression (parser, false, false,
33941 oprec, NULL);
33942 if (rhs == error_mark_node)
33943 goto saw_error;
33944 goto stmt_done;
33945 /* FALLTHROUGH */
33946 default:
33947 cp_parser_error (parser,
33948 "invalid operator for %<#pragma omp atomic%>");
33949 goto saw_error;
33951 cp_lexer_consume_token (parser->lexer);
33953 rhs = cp_parser_expression (parser);
33954 if (rhs == error_mark_node)
33955 goto saw_error;
33956 break;
33958 stmt_done:
33959 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33961 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33962 goto saw_error;
33963 v = cp_parser_unary_expression (parser);
33964 if (v == error_mark_node)
33965 goto saw_error;
33966 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33967 goto saw_error;
33968 lhs1 = cp_parser_unary_expression (parser);
33969 if (lhs1 == error_mark_node)
33970 goto saw_error;
33972 if (structured_block)
33974 cp_parser_consume_semicolon_at_end_of_statement (parser);
33975 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33977 done:
33978 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33979 if (!structured_block)
33980 cp_parser_consume_semicolon_at_end_of_statement (parser);
33981 return;
33983 saw_error:
33984 cp_parser_skip_to_end_of_block_or_statement (parser);
33985 if (structured_block)
33987 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33988 cp_lexer_consume_token (parser->lexer);
33989 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33991 cp_parser_skip_to_end_of_block_or_statement (parser);
33992 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33993 cp_lexer_consume_token (parser->lexer);
33999 /* OpenMP 2.5:
34000 # pragma omp barrier new-line */
34002 static void
34003 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34005 cp_parser_require_pragma_eol (parser, pragma_tok);
34006 finish_omp_barrier ();
34009 /* OpenMP 2.5:
34010 # pragma omp critical [(name)] new-line
34011 structured-block
34013 OpenMP 4.5:
34014 # pragma omp critical [(name) [hint(expression)]] new-line
34015 structured-block */
34017 #define OMP_CRITICAL_CLAUSE_MASK \
34018 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34020 static tree
34021 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34023 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34025 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34027 cp_lexer_consume_token (parser->lexer);
34029 name = cp_parser_identifier (parser);
34031 if (name == error_mark_node
34032 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34033 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34034 /*or_comma=*/false,
34035 /*consume_paren=*/true);
34036 if (name == error_mark_node)
34037 name = NULL;
34039 clauses = cp_parser_omp_all_clauses (parser,
34040 OMP_CRITICAL_CLAUSE_MASK,
34041 "#pragma omp critical", pragma_tok);
34043 else
34044 cp_parser_require_pragma_eol (parser, pragma_tok);
34046 stmt = cp_parser_omp_structured_block (parser, if_p);
34047 return c_finish_omp_critical (input_location, stmt, name, clauses);
34050 /* OpenMP 2.5:
34051 # pragma omp flush flush-vars[opt] new-line
34053 flush-vars:
34054 ( variable-list ) */
34056 static void
34057 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34059 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34060 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34061 cp_parser_require_pragma_eol (parser, pragma_tok);
34063 finish_omp_flush ();
34066 /* Helper function, to parse omp for increment expression. */
34068 static tree
34069 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34071 tree cond = cp_parser_binary_expression (parser, false, true,
34072 PREC_NOT_OPERATOR, NULL);
34073 if (cond == error_mark_node
34074 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34076 cp_parser_skip_to_end_of_statement (parser);
34077 return error_mark_node;
34080 switch (TREE_CODE (cond))
34082 case GT_EXPR:
34083 case GE_EXPR:
34084 case LT_EXPR:
34085 case LE_EXPR:
34086 break;
34087 case NE_EXPR:
34088 if (code == CILK_SIMD || code == CILK_FOR)
34089 break;
34090 /* Fall through: OpenMP disallows NE_EXPR. */
34091 gcc_fallthrough ();
34092 default:
34093 return error_mark_node;
34096 /* If decl is an iterator, preserve LHS and RHS of the relational
34097 expr until finish_omp_for. */
34098 if (decl
34099 && (type_dependent_expression_p (decl)
34100 || CLASS_TYPE_P (TREE_TYPE (decl))))
34101 return cond;
34103 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34104 TREE_CODE (cond),
34105 TREE_OPERAND (cond, 0), ERROR_MARK,
34106 TREE_OPERAND (cond, 1), ERROR_MARK,
34107 /*overload=*/NULL, tf_warning_or_error);
34110 /* Helper function, to parse omp for increment expression. */
34112 static tree
34113 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34115 cp_token *token = cp_lexer_peek_token (parser->lexer);
34116 enum tree_code op;
34117 tree lhs, rhs;
34118 cp_id_kind idk;
34119 bool decl_first;
34121 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34123 op = (token->type == CPP_PLUS_PLUS
34124 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34125 cp_lexer_consume_token (parser->lexer);
34126 lhs = cp_parser_simple_cast_expression (parser);
34127 if (lhs != decl
34128 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34129 return error_mark_node;
34130 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34133 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34134 if (lhs != decl
34135 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34136 return error_mark_node;
34138 token = cp_lexer_peek_token (parser->lexer);
34139 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34141 op = (token->type == CPP_PLUS_PLUS
34142 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34143 cp_lexer_consume_token (parser->lexer);
34144 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34147 op = cp_parser_assignment_operator_opt (parser);
34148 if (op == ERROR_MARK)
34149 return error_mark_node;
34151 if (op != NOP_EXPR)
34153 rhs = cp_parser_assignment_expression (parser);
34154 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34155 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34158 lhs = cp_parser_binary_expression (parser, false, false,
34159 PREC_ADDITIVE_EXPRESSION, NULL);
34160 token = cp_lexer_peek_token (parser->lexer);
34161 decl_first = (lhs == decl
34162 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34163 if (decl_first)
34164 lhs = NULL_TREE;
34165 if (token->type != CPP_PLUS
34166 && token->type != CPP_MINUS)
34167 return error_mark_node;
34171 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34172 cp_lexer_consume_token (parser->lexer);
34173 rhs = cp_parser_binary_expression (parser, false, false,
34174 PREC_ADDITIVE_EXPRESSION, NULL);
34175 token = cp_lexer_peek_token (parser->lexer);
34176 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34178 if (lhs == NULL_TREE)
34180 if (op == PLUS_EXPR)
34181 lhs = rhs;
34182 else
34183 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34184 tf_warning_or_error);
34186 else
34187 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34188 ERROR_MARK, NULL, tf_warning_or_error);
34191 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34193 if (!decl_first)
34195 if ((rhs != decl
34196 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34197 || op == MINUS_EXPR)
34198 return error_mark_node;
34199 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34201 else
34202 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34204 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34207 /* Parse the initialization statement of either an OpenMP for loop or
34208 a Cilk Plus for loop.
34210 Return true if the resulting construct should have an
34211 OMP_CLAUSE_PRIVATE added to it. */
34213 static tree
34214 cp_parser_omp_for_loop_init (cp_parser *parser,
34215 enum tree_code code,
34216 tree &this_pre_body,
34217 vec<tree, va_gc> *for_block,
34218 tree &init,
34219 tree &orig_init,
34220 tree &decl,
34221 tree &real_decl)
34223 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34224 return NULL_TREE;
34226 tree add_private_clause = NULL_TREE;
34228 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34230 init-expr:
34231 var = lb
34232 integer-type var = lb
34233 random-access-iterator-type var = lb
34234 pointer-type var = lb
34236 cp_decl_specifier_seq type_specifiers;
34238 /* First, try to parse as an initialized declaration. See
34239 cp_parser_condition, from whence the bulk of this is copied. */
34241 cp_parser_parse_tentatively (parser);
34242 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34243 /*is_trailing_return=*/false,
34244 &type_specifiers);
34245 if (cp_parser_parse_definitely (parser))
34247 /* If parsing a type specifier seq succeeded, then this
34248 MUST be a initialized declaration. */
34249 tree asm_specification, attributes;
34250 cp_declarator *declarator;
34252 declarator = cp_parser_declarator (parser,
34253 CP_PARSER_DECLARATOR_NAMED,
34254 /*ctor_dtor_or_conv_p=*/NULL,
34255 /*parenthesized_p=*/NULL,
34256 /*member_p=*/false,
34257 /*friend_p=*/false);
34258 attributes = cp_parser_attributes_opt (parser);
34259 asm_specification = cp_parser_asm_specification_opt (parser);
34261 if (declarator == cp_error_declarator)
34262 cp_parser_skip_to_end_of_statement (parser);
34264 else
34266 tree pushed_scope, auto_node;
34268 decl = start_decl (declarator, &type_specifiers,
34269 SD_INITIALIZED, attributes,
34270 /*prefix_attributes=*/NULL_TREE,
34271 &pushed_scope);
34273 auto_node = type_uses_auto (TREE_TYPE (decl));
34274 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34276 if (cp_lexer_next_token_is (parser->lexer,
34277 CPP_OPEN_PAREN))
34279 if (code != CILK_SIMD && code != CILK_FOR)
34280 error ("parenthesized initialization is not allowed in "
34281 "OpenMP %<for%> loop");
34282 else
34283 error ("parenthesized initialization is "
34284 "not allowed in for-loop");
34286 else
34287 /* Trigger an error. */
34288 cp_parser_require (parser, CPP_EQ, RT_EQ);
34290 init = error_mark_node;
34291 cp_parser_skip_to_end_of_statement (parser);
34293 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34294 || type_dependent_expression_p (decl)
34295 || auto_node)
34297 bool is_direct_init, is_non_constant_init;
34299 init = cp_parser_initializer (parser,
34300 &is_direct_init,
34301 &is_non_constant_init);
34303 if (auto_node)
34305 TREE_TYPE (decl)
34306 = do_auto_deduction (TREE_TYPE (decl), init,
34307 auto_node);
34309 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34310 && !type_dependent_expression_p (decl))
34311 goto non_class;
34314 cp_finish_decl (decl, init, !is_non_constant_init,
34315 asm_specification,
34316 LOOKUP_ONLYCONVERTING);
34317 orig_init = init;
34318 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34320 vec_safe_push (for_block, this_pre_body);
34321 init = NULL_TREE;
34323 else
34325 init = pop_stmt_list (this_pre_body);
34326 if (init && TREE_CODE (init) == STATEMENT_LIST)
34328 tree_stmt_iterator i = tsi_start (init);
34329 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34330 while (!tsi_end_p (i))
34332 tree t = tsi_stmt (i);
34333 if (TREE_CODE (t) == DECL_EXPR
34334 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34336 tsi_delink (&i);
34337 vec_safe_push (for_block, t);
34338 continue;
34340 break;
34342 if (tsi_one_before_end_p (i))
34344 tree t = tsi_stmt (i);
34345 tsi_delink (&i);
34346 free_stmt_list (init);
34347 init = t;
34351 this_pre_body = NULL_TREE;
34353 else
34355 /* Consume '='. */
34356 cp_lexer_consume_token (parser->lexer);
34357 init = cp_parser_assignment_expression (parser);
34359 non_class:
34360 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34361 init = error_mark_node;
34362 else
34363 cp_finish_decl (decl, NULL_TREE,
34364 /*init_const_expr_p=*/false,
34365 asm_specification,
34366 LOOKUP_ONLYCONVERTING);
34369 if (pushed_scope)
34370 pop_scope (pushed_scope);
34373 else
34375 cp_id_kind idk;
34376 /* If parsing a type specifier sequence failed, then
34377 this MUST be a simple expression. */
34378 if (code == CILK_FOR)
34379 error ("%<_Cilk_for%> allows expression instead of declaration only "
34380 "in C, not in C++");
34381 cp_parser_parse_tentatively (parser);
34382 decl = cp_parser_primary_expression (parser, false, false,
34383 false, &idk);
34384 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34385 if (!cp_parser_error_occurred (parser)
34386 && decl
34387 && (TREE_CODE (decl) == COMPONENT_REF
34388 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34390 cp_parser_abort_tentative_parse (parser);
34391 cp_parser_parse_tentatively (parser);
34392 cp_token *token = cp_lexer_peek_token (parser->lexer);
34393 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34394 /*check_dependency_p=*/true,
34395 /*template_p=*/NULL,
34396 /*declarator_p=*/false,
34397 /*optional_p=*/false);
34398 if (name != error_mark_node
34399 && last_tok == cp_lexer_peek_token (parser->lexer))
34401 decl = cp_parser_lookup_name_simple (parser, name,
34402 token->location);
34403 if (TREE_CODE (decl) == FIELD_DECL)
34404 add_private_clause = omp_privatize_field (decl, false);
34406 cp_parser_abort_tentative_parse (parser);
34407 cp_parser_parse_tentatively (parser);
34408 decl = cp_parser_primary_expression (parser, false, false,
34409 false, &idk);
34411 if (!cp_parser_error_occurred (parser)
34412 && decl
34413 && DECL_P (decl)
34414 && CLASS_TYPE_P (TREE_TYPE (decl)))
34416 tree rhs;
34418 cp_parser_parse_definitely (parser);
34419 cp_parser_require (parser, CPP_EQ, RT_EQ);
34420 rhs = cp_parser_assignment_expression (parser);
34421 orig_init = rhs;
34422 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34423 decl, NOP_EXPR,
34424 rhs,
34425 tf_warning_or_error));
34426 if (!add_private_clause)
34427 add_private_clause = decl;
34429 else
34431 decl = NULL;
34432 cp_parser_abort_tentative_parse (parser);
34433 init = cp_parser_expression (parser);
34434 if (init)
34436 if (TREE_CODE (init) == MODIFY_EXPR
34437 || TREE_CODE (init) == MODOP_EXPR)
34438 real_decl = TREE_OPERAND (init, 0);
34442 return add_private_clause;
34445 /* Parse the restricted form of the for statement allowed by OpenMP. */
34447 static tree
34448 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34449 tree *cclauses, bool *if_p)
34451 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34452 tree real_decl, initv, condv, incrv, declv;
34453 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34454 location_t loc_first;
34455 bool collapse_err = false;
34456 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34457 vec<tree, va_gc> *for_block = make_tree_vector ();
34458 auto_vec<tree, 4> orig_inits;
34459 bool tiling = false;
34461 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34462 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34463 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34464 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34466 tiling = true;
34467 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34469 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34470 && OMP_CLAUSE_ORDERED_EXPR (cl))
34472 ordered_cl = cl;
34473 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34476 if (ordered && ordered < collapse)
34478 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34479 "%<ordered%> clause parameter is less than %<collapse%>");
34480 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34481 = build_int_cst (NULL_TREE, collapse);
34482 ordered = collapse;
34484 if (ordered)
34486 for (tree *pc = &clauses; *pc; )
34487 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34489 error_at (OMP_CLAUSE_LOCATION (*pc),
34490 "%<linear%> clause may not be specified together "
34491 "with %<ordered%> clause with a parameter");
34492 *pc = OMP_CLAUSE_CHAIN (*pc);
34494 else
34495 pc = &OMP_CLAUSE_CHAIN (*pc);
34498 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34499 count = ordered ? ordered : collapse;
34501 declv = make_tree_vec (count);
34502 initv = make_tree_vec (count);
34503 condv = make_tree_vec (count);
34504 incrv = make_tree_vec (count);
34506 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34508 for (i = 0; i < count; i++)
34510 int bracecount = 0;
34511 tree add_private_clause = NULL_TREE;
34512 location_t loc;
34514 if (code != CILK_FOR
34515 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34517 if (!collapse_err)
34518 cp_parser_error (parser, "for statement expected");
34519 return NULL;
34521 if (code == CILK_FOR
34522 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34524 if (!collapse_err)
34525 cp_parser_error (parser, "_Cilk_for statement expected");
34526 return NULL;
34528 loc = cp_lexer_consume_token (parser->lexer)->location;
34530 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34531 return NULL;
34533 init = orig_init = decl = real_decl = NULL;
34534 this_pre_body = push_stmt_list ();
34536 add_private_clause
34537 = cp_parser_omp_for_loop_init (parser, code,
34538 this_pre_body, for_block,
34539 init, orig_init, decl, real_decl);
34541 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34542 if (this_pre_body)
34544 this_pre_body = pop_stmt_list (this_pre_body);
34545 if (pre_body)
34547 tree t = pre_body;
34548 pre_body = push_stmt_list ();
34549 add_stmt (t);
34550 add_stmt (this_pre_body);
34551 pre_body = pop_stmt_list (pre_body);
34553 else
34554 pre_body = this_pre_body;
34557 if (decl)
34558 real_decl = decl;
34559 if (cclauses != NULL
34560 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34561 && real_decl != NULL_TREE)
34563 tree *c;
34564 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34565 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34566 && OMP_CLAUSE_DECL (*c) == real_decl)
34568 error_at (loc, "iteration variable %qD"
34569 " should not be firstprivate", real_decl);
34570 *c = OMP_CLAUSE_CHAIN (*c);
34572 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34573 && OMP_CLAUSE_DECL (*c) == real_decl)
34575 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34576 tree l = *c;
34577 *c = OMP_CLAUSE_CHAIN (*c);
34578 if (code == OMP_SIMD)
34580 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34581 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34583 else
34585 OMP_CLAUSE_CHAIN (l) = clauses;
34586 clauses = l;
34588 add_private_clause = NULL_TREE;
34590 else
34592 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34593 && OMP_CLAUSE_DECL (*c) == real_decl)
34594 add_private_clause = NULL_TREE;
34595 c = &OMP_CLAUSE_CHAIN (*c);
34599 if (add_private_clause)
34601 tree c;
34602 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34604 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34605 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34606 && OMP_CLAUSE_DECL (c) == decl)
34607 break;
34608 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34609 && OMP_CLAUSE_DECL (c) == decl)
34610 error_at (loc, "iteration variable %qD "
34611 "should not be firstprivate",
34612 decl);
34613 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34614 && OMP_CLAUSE_DECL (c) == decl)
34615 error_at (loc, "iteration variable %qD should not be reduction",
34616 decl);
34618 if (c == NULL)
34620 if (code != OMP_SIMD)
34621 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34622 else if (collapse == 1)
34623 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34624 else
34625 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34626 OMP_CLAUSE_DECL (c) = add_private_clause;
34627 c = finish_omp_clauses (c, C_ORT_OMP);
34628 if (c)
34630 OMP_CLAUSE_CHAIN (c) = clauses;
34631 clauses = c;
34632 /* For linear, signal that we need to fill up
34633 the so far unknown linear step. */
34634 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34635 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34640 cond = NULL;
34641 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34642 cond = cp_parser_omp_for_cond (parser, decl, code);
34643 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34645 incr = NULL;
34646 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34648 /* If decl is an iterator, preserve the operator on decl
34649 until finish_omp_for. */
34650 if (real_decl
34651 && ((processing_template_decl
34652 && (TREE_TYPE (real_decl) == NULL_TREE
34653 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34654 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34655 incr = cp_parser_omp_for_incr (parser, real_decl);
34656 else
34657 incr = cp_parser_expression (parser);
34658 if (!EXPR_HAS_LOCATION (incr))
34659 protected_set_expr_location (incr, input_location);
34662 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34663 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34664 /*or_comma=*/false,
34665 /*consume_paren=*/true);
34667 TREE_VEC_ELT (declv, i) = decl;
34668 TREE_VEC_ELT (initv, i) = init;
34669 TREE_VEC_ELT (condv, i) = cond;
34670 TREE_VEC_ELT (incrv, i) = incr;
34671 if (orig_init)
34673 orig_inits.safe_grow_cleared (i + 1);
34674 orig_inits[i] = orig_init;
34677 if (i == count - 1)
34678 break;
34680 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34681 in between the collapsed for loops to be still considered perfectly
34682 nested. Hopefully the final version clarifies this.
34683 For now handle (multiple) {'s and empty statements. */
34684 cp_parser_parse_tentatively (parser);
34685 for (;;)
34687 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34688 break;
34689 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34691 cp_lexer_consume_token (parser->lexer);
34692 bracecount++;
34694 else if (bracecount
34695 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34696 cp_lexer_consume_token (parser->lexer);
34697 else
34699 loc = cp_lexer_peek_token (parser->lexer)->location;
34700 error_at (loc, "not enough for loops to collapse");
34701 collapse_err = true;
34702 cp_parser_abort_tentative_parse (parser);
34703 declv = NULL_TREE;
34704 break;
34708 if (declv)
34710 cp_parser_parse_definitely (parser);
34711 nbraces += bracecount;
34715 if (nbraces)
34716 if_p = NULL;
34718 /* Note that we saved the original contents of this flag when we entered
34719 the structured block, and so we don't need to re-save it here. */
34720 if (code == CILK_SIMD || code == CILK_FOR)
34721 parser->in_statement = IN_CILK_SIMD_FOR;
34722 else
34723 parser->in_statement = IN_OMP_FOR;
34725 /* Note that the grammar doesn't call for a structured block here,
34726 though the loop as a whole is a structured block. */
34727 body = push_stmt_list ();
34728 cp_parser_statement (parser, NULL_TREE, false, if_p);
34729 body = pop_stmt_list (body);
34731 if (declv == NULL_TREE)
34732 ret = NULL_TREE;
34733 else
34734 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
34735 body, pre_body, &orig_inits, clauses);
34737 while (nbraces)
34739 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34741 cp_lexer_consume_token (parser->lexer);
34742 nbraces--;
34744 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34745 cp_lexer_consume_token (parser->lexer);
34746 else
34748 if (!collapse_err)
34750 error_at (cp_lexer_peek_token (parser->lexer)->location,
34751 "collapsed loops not perfectly nested");
34753 collapse_err = true;
34754 cp_parser_statement_seq_opt (parser, NULL);
34755 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34756 break;
34760 while (!for_block->is_empty ())
34762 tree t = for_block->pop ();
34763 if (TREE_CODE (t) == STATEMENT_LIST)
34764 add_stmt (pop_stmt_list (t));
34765 else
34766 add_stmt (t);
34768 release_tree_vector (for_block);
34770 return ret;
34773 /* Helper function for OpenMP parsing, split clauses and call
34774 finish_omp_clauses on each of the set of clauses afterwards. */
34776 static void
34777 cp_omp_split_clauses (location_t loc, enum tree_code code,
34778 omp_clause_mask mask, tree clauses, tree *cclauses)
34780 int i;
34781 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
34782 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
34783 if (cclauses[i])
34784 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
34787 /* OpenMP 4.0:
34788 #pragma omp simd simd-clause[optseq] new-line
34789 for-loop */
34791 #define OMP_SIMD_CLAUSE_MASK \
34792 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34801 static tree
34802 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
34803 char *p_name, omp_clause_mask mask, tree *cclauses,
34804 bool *if_p)
34806 tree clauses, sb, ret;
34807 unsigned int save;
34808 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34810 strcat (p_name, " simd");
34811 mask |= OMP_SIMD_CLAUSE_MASK;
34813 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34814 cclauses == NULL);
34815 if (cclauses)
34817 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
34818 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
34819 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
34820 OMP_CLAUSE_ORDERED);
34821 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
34823 error_at (OMP_CLAUSE_LOCATION (c),
34824 "%<ordered%> clause with parameter may not be specified "
34825 "on %qs construct", p_name);
34826 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
34830 sb = begin_omp_structured_block ();
34831 save = cp_parser_begin_omp_structured_block (parser);
34833 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
34835 cp_parser_end_omp_structured_block (parser, save);
34836 add_stmt (finish_omp_structured_block (sb));
34838 return ret;
34841 /* OpenMP 2.5:
34842 #pragma omp for for-clause[optseq] new-line
34843 for-loop
34845 OpenMP 4.0:
34846 #pragma omp for simd for-simd-clause[optseq] new-line
34847 for-loop */
34849 #define OMP_FOR_CLAUSE_MASK \
34850 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34860 static tree
34861 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
34862 char *p_name, omp_clause_mask mask, tree *cclauses,
34863 bool *if_p)
34865 tree clauses, sb, ret;
34866 unsigned int save;
34867 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34869 strcat (p_name, " for");
34870 mask |= OMP_FOR_CLAUSE_MASK;
34871 /* parallel for{, simd} disallows nowait clause, but for
34872 target {teams distribute ,}parallel for{, simd} it should be accepted. */
34873 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
34874 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34875 /* Composite distribute parallel for{, simd} disallows ordered clause. */
34876 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34877 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
34879 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34881 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34882 const char *p = IDENTIFIER_POINTER (id);
34884 if (strcmp (p, "simd") == 0)
34886 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34887 if (cclauses == NULL)
34888 cclauses = cclauses_buf;
34890 cp_lexer_consume_token (parser->lexer);
34891 if (!flag_openmp) /* flag_openmp_simd */
34892 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34893 cclauses, if_p);
34894 sb = begin_omp_structured_block ();
34895 save = cp_parser_begin_omp_structured_block (parser);
34896 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34897 cclauses, if_p);
34898 cp_parser_end_omp_structured_block (parser, save);
34899 tree body = finish_omp_structured_block (sb);
34900 if (ret == NULL)
34901 return ret;
34902 ret = make_node (OMP_FOR);
34903 TREE_TYPE (ret) = void_type_node;
34904 OMP_FOR_BODY (ret) = body;
34905 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34906 SET_EXPR_LOCATION (ret, loc);
34907 add_stmt (ret);
34908 return ret;
34911 if (!flag_openmp) /* flag_openmp_simd */
34913 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34914 return NULL_TREE;
34917 /* Composite distribute parallel for disallows linear clause. */
34918 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34919 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
34921 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34922 cclauses == NULL);
34923 if (cclauses)
34925 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
34926 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34929 sb = begin_omp_structured_block ();
34930 save = cp_parser_begin_omp_structured_block (parser);
34932 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
34934 cp_parser_end_omp_structured_block (parser, save);
34935 add_stmt (finish_omp_structured_block (sb));
34937 return ret;
34940 /* OpenMP 2.5:
34941 # pragma omp master new-line
34942 structured-block */
34944 static tree
34945 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34947 cp_parser_require_pragma_eol (parser, pragma_tok);
34948 return c_finish_omp_master (input_location,
34949 cp_parser_omp_structured_block (parser, if_p));
34952 /* OpenMP 2.5:
34953 # pragma omp ordered new-line
34954 structured-block
34956 OpenMP 4.5:
34957 # pragma omp ordered ordered-clauses new-line
34958 structured-block */
34960 #define OMP_ORDERED_CLAUSE_MASK \
34961 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34964 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34965 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34967 static bool
34968 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34969 enum pragma_context context, bool *if_p)
34971 location_t loc = pragma_tok->location;
34973 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34975 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34976 const char *p = IDENTIFIER_POINTER (id);
34978 if (strcmp (p, "depend") == 0)
34980 if (context == pragma_stmt)
34982 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34983 "%<depend%> clause may only be used in compound "
34984 "statements");
34985 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34986 return false;
34988 tree clauses
34989 = cp_parser_omp_all_clauses (parser,
34990 OMP_ORDERED_DEPEND_CLAUSE_MASK,
34991 "#pragma omp ordered", pragma_tok);
34992 c_finish_omp_ordered (loc, clauses, NULL_TREE);
34993 return false;
34997 tree clauses
34998 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34999 "#pragma omp ordered", pragma_tok);
35000 c_finish_omp_ordered (loc, clauses,
35001 cp_parser_omp_structured_block (parser, if_p));
35002 return true;
35005 /* OpenMP 2.5:
35007 section-scope:
35008 { section-sequence }
35010 section-sequence:
35011 section-directive[opt] structured-block
35012 section-sequence section-directive structured-block */
35014 static tree
35015 cp_parser_omp_sections_scope (cp_parser *parser)
35017 tree stmt, substmt;
35018 bool error_suppress = false;
35019 cp_token *tok;
35021 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
35022 return NULL_TREE;
35024 stmt = push_stmt_list ();
35026 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35027 != PRAGMA_OMP_SECTION)
35029 substmt = cp_parser_omp_structured_block (parser, NULL);
35030 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35031 add_stmt (substmt);
35034 while (1)
35036 tok = cp_lexer_peek_token (parser->lexer);
35037 if (tok->type == CPP_CLOSE_BRACE)
35038 break;
35039 if (tok->type == CPP_EOF)
35040 break;
35042 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35044 cp_lexer_consume_token (parser->lexer);
35045 cp_parser_require_pragma_eol (parser, tok);
35046 error_suppress = false;
35048 else if (!error_suppress)
35050 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35051 error_suppress = true;
35054 substmt = cp_parser_omp_structured_block (parser, NULL);
35055 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35056 add_stmt (substmt);
35058 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35060 substmt = pop_stmt_list (stmt);
35062 stmt = make_node (OMP_SECTIONS);
35063 TREE_TYPE (stmt) = void_type_node;
35064 OMP_SECTIONS_BODY (stmt) = substmt;
35066 add_stmt (stmt);
35067 return stmt;
35070 /* OpenMP 2.5:
35071 # pragma omp sections sections-clause[optseq] newline
35072 sections-scope */
35074 #define OMP_SECTIONS_CLAUSE_MASK \
35075 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35081 static tree
35082 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35083 char *p_name, omp_clause_mask mask, tree *cclauses)
35085 tree clauses, ret;
35086 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35088 strcat (p_name, " sections");
35089 mask |= OMP_SECTIONS_CLAUSE_MASK;
35090 if (cclauses)
35091 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35093 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35094 cclauses == NULL);
35095 if (cclauses)
35097 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35098 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35101 ret = cp_parser_omp_sections_scope (parser);
35102 if (ret)
35103 OMP_SECTIONS_CLAUSES (ret) = clauses;
35105 return ret;
35108 /* OpenMP 2.5:
35109 # pragma omp parallel parallel-clause[optseq] new-line
35110 structured-block
35111 # pragma omp parallel for parallel-for-clause[optseq] new-line
35112 structured-block
35113 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35114 structured-block
35116 OpenMP 4.0:
35117 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35118 structured-block */
35120 #define OMP_PARALLEL_CLAUSE_MASK \
35121 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35131 static tree
35132 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35133 char *p_name, omp_clause_mask mask, tree *cclauses,
35134 bool *if_p)
35136 tree stmt, clauses, block;
35137 unsigned int save;
35138 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35140 strcat (p_name, " parallel");
35141 mask |= OMP_PARALLEL_CLAUSE_MASK;
35142 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35143 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35144 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35145 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35147 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35149 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35150 if (cclauses == NULL)
35151 cclauses = cclauses_buf;
35153 cp_lexer_consume_token (parser->lexer);
35154 if (!flag_openmp) /* flag_openmp_simd */
35155 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35156 if_p);
35157 block = begin_omp_parallel ();
35158 save = cp_parser_begin_omp_structured_block (parser);
35159 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35160 if_p);
35161 cp_parser_end_omp_structured_block (parser, save);
35162 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35163 block);
35164 if (ret == NULL_TREE)
35165 return ret;
35166 OMP_PARALLEL_COMBINED (stmt) = 1;
35167 return stmt;
35169 /* When combined with distribute, parallel has to be followed by for.
35170 #pragma omp target parallel is allowed though. */
35171 else if (cclauses
35172 && (mask & (OMP_CLAUSE_MASK_1
35173 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35175 error_at (loc, "expected %<for%> after %qs", p_name);
35176 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35177 return NULL_TREE;
35179 else if (!flag_openmp) /* flag_openmp_simd */
35181 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35182 return NULL_TREE;
35184 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35186 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35187 const char *p = IDENTIFIER_POINTER (id);
35188 if (strcmp (p, "sections") == 0)
35190 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35191 cclauses = cclauses_buf;
35193 cp_lexer_consume_token (parser->lexer);
35194 block = begin_omp_parallel ();
35195 save = cp_parser_begin_omp_structured_block (parser);
35196 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35197 cp_parser_end_omp_structured_block (parser, save);
35198 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35199 block);
35200 OMP_PARALLEL_COMBINED (stmt) = 1;
35201 return stmt;
35205 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35206 cclauses == NULL);
35207 if (cclauses)
35209 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35210 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35213 block = begin_omp_parallel ();
35214 save = cp_parser_begin_omp_structured_block (parser);
35215 cp_parser_statement (parser, NULL_TREE, false, if_p);
35216 cp_parser_end_omp_structured_block (parser, save);
35217 stmt = finish_omp_parallel (clauses, block);
35218 return stmt;
35221 /* OpenMP 2.5:
35222 # pragma omp single single-clause[optseq] new-line
35223 structured-block */
35225 #define OMP_SINGLE_CLAUSE_MASK \
35226 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35231 static tree
35232 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35234 tree stmt = make_node (OMP_SINGLE);
35235 TREE_TYPE (stmt) = void_type_node;
35237 OMP_SINGLE_CLAUSES (stmt)
35238 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35239 "#pragma omp single", pragma_tok);
35240 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35242 return add_stmt (stmt);
35245 /* OpenMP 3.0:
35246 # pragma omp task task-clause[optseq] new-line
35247 structured-block */
35249 #define OMP_TASK_CLAUSE_MASK \
35250 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35261 static tree
35262 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35264 tree clauses, block;
35265 unsigned int save;
35267 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35268 "#pragma omp task", pragma_tok);
35269 block = begin_omp_task ();
35270 save = cp_parser_begin_omp_structured_block (parser);
35271 cp_parser_statement (parser, NULL_TREE, false, if_p);
35272 cp_parser_end_omp_structured_block (parser, save);
35273 return finish_omp_task (clauses, block);
35276 /* OpenMP 3.0:
35277 # pragma omp taskwait new-line */
35279 static void
35280 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35282 cp_parser_require_pragma_eol (parser, pragma_tok);
35283 finish_omp_taskwait ();
35286 /* OpenMP 3.1:
35287 # pragma omp taskyield new-line */
35289 static void
35290 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35292 cp_parser_require_pragma_eol (parser, pragma_tok);
35293 finish_omp_taskyield ();
35296 /* OpenMP 4.0:
35297 # pragma omp taskgroup new-line
35298 structured-block */
35300 static tree
35301 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35303 cp_parser_require_pragma_eol (parser, pragma_tok);
35304 return c_finish_omp_taskgroup (input_location,
35305 cp_parser_omp_structured_block (parser,
35306 if_p));
35310 /* OpenMP 2.5:
35311 # pragma omp threadprivate (variable-list) */
35313 static void
35314 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35316 tree vars;
35318 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35319 cp_parser_require_pragma_eol (parser, pragma_tok);
35321 finish_omp_threadprivate (vars);
35324 /* OpenMP 4.0:
35325 # pragma omp cancel cancel-clause[optseq] new-line */
35327 #define OMP_CANCEL_CLAUSE_MASK \
35328 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35334 static void
35335 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35337 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35338 "#pragma omp cancel", pragma_tok);
35339 finish_omp_cancel (clauses);
35342 /* OpenMP 4.0:
35343 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35345 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35346 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35351 static void
35352 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35353 enum pragma_context context)
35355 tree clauses;
35356 bool point_seen = false;
35358 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35360 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35361 const char *p = IDENTIFIER_POINTER (id);
35363 if (strcmp (p, "point") == 0)
35365 cp_lexer_consume_token (parser->lexer);
35366 point_seen = true;
35369 if (!point_seen)
35371 cp_parser_error (parser, "expected %<point%>");
35372 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35373 return;
35376 if (context != pragma_compound)
35378 if (context == pragma_stmt)
35379 error_at (pragma_tok->location,
35380 "%<#pragma %s%> may only be used in compound statements",
35381 "omp cancellation point");
35382 else
35383 cp_parser_error (parser, "expected declaration specifiers");
35384 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35385 return;
35388 clauses = cp_parser_omp_all_clauses (parser,
35389 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35390 "#pragma omp cancellation point",
35391 pragma_tok);
35392 finish_omp_cancellation_point (clauses);
35395 /* OpenMP 4.0:
35396 #pragma omp distribute distribute-clause[optseq] new-line
35397 for-loop */
35399 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35400 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35406 static tree
35407 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35408 char *p_name, omp_clause_mask mask, tree *cclauses,
35409 bool *if_p)
35411 tree clauses, sb, ret;
35412 unsigned int save;
35413 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35415 strcat (p_name, " distribute");
35416 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35418 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35420 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35421 const char *p = IDENTIFIER_POINTER (id);
35422 bool simd = false;
35423 bool parallel = false;
35425 if (strcmp (p, "simd") == 0)
35426 simd = true;
35427 else
35428 parallel = strcmp (p, "parallel") == 0;
35429 if (parallel || simd)
35431 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35432 if (cclauses == NULL)
35433 cclauses = cclauses_buf;
35434 cp_lexer_consume_token (parser->lexer);
35435 if (!flag_openmp) /* flag_openmp_simd */
35437 if (simd)
35438 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35439 cclauses, if_p);
35440 else
35441 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35442 cclauses, if_p);
35444 sb = begin_omp_structured_block ();
35445 save = cp_parser_begin_omp_structured_block (parser);
35446 if (simd)
35447 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35448 cclauses, if_p);
35449 else
35450 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35451 cclauses, if_p);
35452 cp_parser_end_omp_structured_block (parser, save);
35453 tree body = finish_omp_structured_block (sb);
35454 if (ret == NULL)
35455 return ret;
35456 ret = make_node (OMP_DISTRIBUTE);
35457 TREE_TYPE (ret) = void_type_node;
35458 OMP_FOR_BODY (ret) = body;
35459 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35460 SET_EXPR_LOCATION (ret, loc);
35461 add_stmt (ret);
35462 return ret;
35465 if (!flag_openmp) /* flag_openmp_simd */
35467 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35468 return NULL_TREE;
35471 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35472 cclauses == NULL);
35473 if (cclauses)
35475 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35476 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35479 sb = begin_omp_structured_block ();
35480 save = cp_parser_begin_omp_structured_block (parser);
35482 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35484 cp_parser_end_omp_structured_block (parser, save);
35485 add_stmt (finish_omp_structured_block (sb));
35487 return ret;
35490 /* OpenMP 4.0:
35491 # pragma omp teams teams-clause[optseq] new-line
35492 structured-block */
35494 #define OMP_TEAMS_CLAUSE_MASK \
35495 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35503 static tree
35504 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35505 char *p_name, omp_clause_mask mask, tree *cclauses,
35506 bool *if_p)
35508 tree clauses, sb, ret;
35509 unsigned int save;
35510 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35512 strcat (p_name, " teams");
35513 mask |= OMP_TEAMS_CLAUSE_MASK;
35515 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35517 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35518 const char *p = IDENTIFIER_POINTER (id);
35519 if (strcmp (p, "distribute") == 0)
35521 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35522 if (cclauses == NULL)
35523 cclauses = cclauses_buf;
35525 cp_lexer_consume_token (parser->lexer);
35526 if (!flag_openmp) /* flag_openmp_simd */
35527 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35528 cclauses, if_p);
35529 sb = begin_omp_structured_block ();
35530 save = cp_parser_begin_omp_structured_block (parser);
35531 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35532 cclauses, if_p);
35533 cp_parser_end_omp_structured_block (parser, save);
35534 tree body = finish_omp_structured_block (sb);
35535 if (ret == NULL)
35536 return ret;
35537 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35538 ret = make_node (OMP_TEAMS);
35539 TREE_TYPE (ret) = void_type_node;
35540 OMP_TEAMS_CLAUSES (ret) = clauses;
35541 OMP_TEAMS_BODY (ret) = body;
35542 OMP_TEAMS_COMBINED (ret) = 1;
35543 SET_EXPR_LOCATION (ret, loc);
35544 return add_stmt (ret);
35547 if (!flag_openmp) /* flag_openmp_simd */
35549 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35550 return NULL_TREE;
35553 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35554 cclauses == NULL);
35555 if (cclauses)
35557 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35558 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35561 tree stmt = make_node (OMP_TEAMS);
35562 TREE_TYPE (stmt) = void_type_node;
35563 OMP_TEAMS_CLAUSES (stmt) = clauses;
35564 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35565 SET_EXPR_LOCATION (stmt, loc);
35567 return add_stmt (stmt);
35570 /* OpenMP 4.0:
35571 # pragma omp target data target-data-clause[optseq] new-line
35572 structured-block */
35574 #define OMP_TARGET_DATA_CLAUSE_MASK \
35575 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35580 static tree
35581 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35583 tree clauses
35584 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35585 "#pragma omp target data", pragma_tok);
35586 int map_seen = 0;
35587 for (tree *pc = &clauses; *pc;)
35589 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35590 switch (OMP_CLAUSE_MAP_KIND (*pc))
35592 case GOMP_MAP_TO:
35593 case GOMP_MAP_ALWAYS_TO:
35594 case GOMP_MAP_FROM:
35595 case GOMP_MAP_ALWAYS_FROM:
35596 case GOMP_MAP_TOFROM:
35597 case GOMP_MAP_ALWAYS_TOFROM:
35598 case GOMP_MAP_ALLOC:
35599 map_seen = 3;
35600 break;
35601 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35602 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35603 case GOMP_MAP_ALWAYS_POINTER:
35604 break;
35605 default:
35606 map_seen |= 1;
35607 error_at (OMP_CLAUSE_LOCATION (*pc),
35608 "%<#pragma omp target data%> with map-type other "
35609 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35610 "on %<map%> clause");
35611 *pc = OMP_CLAUSE_CHAIN (*pc);
35612 continue;
35614 pc = &OMP_CLAUSE_CHAIN (*pc);
35617 if (map_seen != 3)
35619 if (map_seen == 0)
35620 error_at (pragma_tok->location,
35621 "%<#pragma omp target data%> must contain at least "
35622 "one %<map%> clause");
35623 return NULL_TREE;
35626 tree stmt = make_node (OMP_TARGET_DATA);
35627 TREE_TYPE (stmt) = void_type_node;
35628 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35630 keep_next_level (true);
35631 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35633 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35634 return add_stmt (stmt);
35637 /* OpenMP 4.5:
35638 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35639 structured-block */
35641 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35642 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35648 static tree
35649 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35650 enum pragma_context context)
35652 bool data_seen = false;
35653 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35655 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35656 const char *p = IDENTIFIER_POINTER (id);
35658 if (strcmp (p, "data") == 0)
35660 cp_lexer_consume_token (parser->lexer);
35661 data_seen = true;
35664 if (!data_seen)
35666 cp_parser_error (parser, "expected %<data%>");
35667 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35668 return NULL_TREE;
35671 if (context == pragma_stmt)
35673 error_at (pragma_tok->location,
35674 "%<#pragma %s%> may only be used in compound statements",
35675 "omp target enter data");
35676 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35677 return NULL_TREE;
35680 tree clauses
35681 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
35682 "#pragma omp target enter data", pragma_tok);
35683 int map_seen = 0;
35684 for (tree *pc = &clauses; *pc;)
35686 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35687 switch (OMP_CLAUSE_MAP_KIND (*pc))
35689 case GOMP_MAP_TO:
35690 case GOMP_MAP_ALWAYS_TO:
35691 case GOMP_MAP_ALLOC:
35692 map_seen = 3;
35693 break;
35694 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35695 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35696 case GOMP_MAP_ALWAYS_POINTER:
35697 break;
35698 default:
35699 map_seen |= 1;
35700 error_at (OMP_CLAUSE_LOCATION (*pc),
35701 "%<#pragma omp target enter data%> with map-type other "
35702 "than %<to%> or %<alloc%> on %<map%> clause");
35703 *pc = OMP_CLAUSE_CHAIN (*pc);
35704 continue;
35706 pc = &OMP_CLAUSE_CHAIN (*pc);
35709 if (map_seen != 3)
35711 if (map_seen == 0)
35712 error_at (pragma_tok->location,
35713 "%<#pragma omp target enter data%> must contain at least "
35714 "one %<map%> clause");
35715 return NULL_TREE;
35718 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
35719 TREE_TYPE (stmt) = void_type_node;
35720 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
35721 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35722 return add_stmt (stmt);
35725 /* OpenMP 4.5:
35726 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35727 structured-block */
35729 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35730 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35736 static tree
35737 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
35738 enum pragma_context context)
35740 bool data_seen = false;
35741 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35743 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35744 const char *p = IDENTIFIER_POINTER (id);
35746 if (strcmp (p, "data") == 0)
35748 cp_lexer_consume_token (parser->lexer);
35749 data_seen = true;
35752 if (!data_seen)
35754 cp_parser_error (parser, "expected %<data%>");
35755 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35756 return NULL_TREE;
35759 if (context == pragma_stmt)
35761 error_at (pragma_tok->location,
35762 "%<#pragma %s%> may only be used in compound statements",
35763 "omp target exit data");
35764 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35765 return NULL_TREE;
35768 tree clauses
35769 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
35770 "#pragma omp target exit data", pragma_tok);
35771 int map_seen = 0;
35772 for (tree *pc = &clauses; *pc;)
35774 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35775 switch (OMP_CLAUSE_MAP_KIND (*pc))
35777 case GOMP_MAP_FROM:
35778 case GOMP_MAP_ALWAYS_FROM:
35779 case GOMP_MAP_RELEASE:
35780 case GOMP_MAP_DELETE:
35781 map_seen = 3;
35782 break;
35783 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35784 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35785 case GOMP_MAP_ALWAYS_POINTER:
35786 break;
35787 default:
35788 map_seen |= 1;
35789 error_at (OMP_CLAUSE_LOCATION (*pc),
35790 "%<#pragma omp target exit data%> with map-type other "
35791 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35792 " clause");
35793 *pc = OMP_CLAUSE_CHAIN (*pc);
35794 continue;
35796 pc = &OMP_CLAUSE_CHAIN (*pc);
35799 if (map_seen != 3)
35801 if (map_seen == 0)
35802 error_at (pragma_tok->location,
35803 "%<#pragma omp target exit data%> must contain at least "
35804 "one %<map%> clause");
35805 return NULL_TREE;
35808 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
35809 TREE_TYPE (stmt) = void_type_node;
35810 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
35811 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35812 return add_stmt (stmt);
35815 /* OpenMP 4.0:
35816 # pragma omp target update target-update-clause[optseq] new-line */
35818 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
35819 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35826 static bool
35827 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
35828 enum pragma_context context)
35830 if (context == pragma_stmt)
35832 error_at (pragma_tok->location,
35833 "%<#pragma %s%> may only be used in compound statements",
35834 "omp target update");
35835 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35836 return false;
35839 tree clauses
35840 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
35841 "#pragma omp target update", pragma_tok);
35842 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
35843 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
35845 error_at (pragma_tok->location,
35846 "%<#pragma omp target update%> must contain at least one "
35847 "%<from%> or %<to%> clauses");
35848 return false;
35851 tree stmt = make_node (OMP_TARGET_UPDATE);
35852 TREE_TYPE (stmt) = void_type_node;
35853 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
35854 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35855 add_stmt (stmt);
35856 return false;
35859 /* OpenMP 4.0:
35860 # pragma omp target target-clause[optseq] new-line
35861 structured-block */
35863 #define OMP_TARGET_CLAUSE_MASK \
35864 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
35872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
35874 static bool
35875 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
35876 enum pragma_context context, bool *if_p)
35878 tree *pc = NULL, stmt;
35880 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35882 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35883 const char *p = IDENTIFIER_POINTER (id);
35884 enum tree_code ccode = ERROR_MARK;
35886 if (strcmp (p, "teams") == 0)
35887 ccode = OMP_TEAMS;
35888 else if (strcmp (p, "parallel") == 0)
35889 ccode = OMP_PARALLEL;
35890 else if (strcmp (p, "simd") == 0)
35891 ccode = OMP_SIMD;
35892 if (ccode != ERROR_MARK)
35894 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
35895 char p_name[sizeof ("#pragma omp target teams distribute "
35896 "parallel for simd")];
35898 cp_lexer_consume_token (parser->lexer);
35899 strcpy (p_name, "#pragma omp target");
35900 if (!flag_openmp) /* flag_openmp_simd */
35902 tree stmt;
35903 switch (ccode)
35905 case OMP_TEAMS:
35906 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
35907 OMP_TARGET_CLAUSE_MASK,
35908 cclauses, if_p);
35909 break;
35910 case OMP_PARALLEL:
35911 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35912 OMP_TARGET_CLAUSE_MASK,
35913 cclauses, if_p);
35914 break;
35915 case OMP_SIMD:
35916 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35917 OMP_TARGET_CLAUSE_MASK,
35918 cclauses, if_p);
35919 break;
35920 default:
35921 gcc_unreachable ();
35923 return stmt != NULL_TREE;
35925 keep_next_level (true);
35926 tree sb = begin_omp_structured_block (), ret;
35927 unsigned save = cp_parser_begin_omp_structured_block (parser);
35928 switch (ccode)
35930 case OMP_TEAMS:
35931 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
35932 OMP_TARGET_CLAUSE_MASK, cclauses,
35933 if_p);
35934 break;
35935 case OMP_PARALLEL:
35936 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35937 OMP_TARGET_CLAUSE_MASK, cclauses,
35938 if_p);
35939 break;
35940 case OMP_SIMD:
35941 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35942 OMP_TARGET_CLAUSE_MASK, cclauses,
35943 if_p);
35944 break;
35945 default:
35946 gcc_unreachable ();
35948 cp_parser_end_omp_structured_block (parser, save);
35949 tree body = finish_omp_structured_block (sb);
35950 if (ret == NULL_TREE)
35951 return false;
35952 if (ccode == OMP_TEAMS && !processing_template_decl)
35954 /* For combined target teams, ensure the num_teams and
35955 thread_limit clause expressions are evaluated on the host,
35956 before entering the target construct. */
35957 tree c;
35958 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35959 c; c = OMP_CLAUSE_CHAIN (c))
35960 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35961 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35962 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35964 tree expr = OMP_CLAUSE_OPERAND (c, 0);
35965 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35966 if (expr == error_mark_node)
35967 continue;
35968 tree tmp = TARGET_EXPR_SLOT (expr);
35969 add_stmt (expr);
35970 OMP_CLAUSE_OPERAND (c, 0) = expr;
35971 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35972 OMP_CLAUSE_FIRSTPRIVATE);
35973 OMP_CLAUSE_DECL (tc) = tmp;
35974 OMP_CLAUSE_CHAIN (tc)
35975 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35976 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35979 tree stmt = make_node (OMP_TARGET);
35980 TREE_TYPE (stmt) = void_type_node;
35981 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35982 OMP_TARGET_BODY (stmt) = body;
35983 OMP_TARGET_COMBINED (stmt) = 1;
35984 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35985 add_stmt (stmt);
35986 pc = &OMP_TARGET_CLAUSES (stmt);
35987 goto check_clauses;
35989 else if (!flag_openmp) /* flag_openmp_simd */
35991 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35992 return false;
35994 else if (strcmp (p, "data") == 0)
35996 cp_lexer_consume_token (parser->lexer);
35997 cp_parser_omp_target_data (parser, pragma_tok, if_p);
35998 return true;
36000 else if (strcmp (p, "enter") == 0)
36002 cp_lexer_consume_token (parser->lexer);
36003 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36004 return false;
36006 else if (strcmp (p, "exit") == 0)
36008 cp_lexer_consume_token (parser->lexer);
36009 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36010 return false;
36012 else if (strcmp (p, "update") == 0)
36014 cp_lexer_consume_token (parser->lexer);
36015 return cp_parser_omp_target_update (parser, pragma_tok, context);
36018 if (!flag_openmp) /* flag_openmp_simd */
36020 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36021 return false;
36024 stmt = make_node (OMP_TARGET);
36025 TREE_TYPE (stmt) = void_type_node;
36027 OMP_TARGET_CLAUSES (stmt)
36028 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36029 "#pragma omp target", pragma_tok);
36030 pc = &OMP_TARGET_CLAUSES (stmt);
36031 keep_next_level (true);
36032 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36034 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36035 add_stmt (stmt);
36037 check_clauses:
36038 while (*pc)
36040 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36041 switch (OMP_CLAUSE_MAP_KIND (*pc))
36043 case GOMP_MAP_TO:
36044 case GOMP_MAP_ALWAYS_TO:
36045 case GOMP_MAP_FROM:
36046 case GOMP_MAP_ALWAYS_FROM:
36047 case GOMP_MAP_TOFROM:
36048 case GOMP_MAP_ALWAYS_TOFROM:
36049 case GOMP_MAP_ALLOC:
36050 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36051 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36052 case GOMP_MAP_ALWAYS_POINTER:
36053 break;
36054 default:
36055 error_at (OMP_CLAUSE_LOCATION (*pc),
36056 "%<#pragma omp target%> with map-type other "
36057 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36058 "on %<map%> clause");
36059 *pc = OMP_CLAUSE_CHAIN (*pc);
36060 continue;
36062 pc = &OMP_CLAUSE_CHAIN (*pc);
36064 return true;
36067 /* OpenACC 2.0:
36068 # pragma acc cache (variable-list) new-line
36071 static tree
36072 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36074 tree stmt, clauses;
36076 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36077 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36079 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36081 stmt = make_node (OACC_CACHE);
36082 TREE_TYPE (stmt) = void_type_node;
36083 OACC_CACHE_CLAUSES (stmt) = clauses;
36084 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36085 add_stmt (stmt);
36087 return stmt;
36090 /* OpenACC 2.0:
36091 # pragma acc data oacc-data-clause[optseq] new-line
36092 structured-block */
36094 #define OACC_DATA_CLAUSE_MASK \
36095 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36107 static tree
36108 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36110 tree stmt, clauses, block;
36111 unsigned int save;
36113 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36114 "#pragma acc data", pragma_tok);
36116 block = begin_omp_parallel ();
36117 save = cp_parser_begin_omp_structured_block (parser);
36118 cp_parser_statement (parser, NULL_TREE, false, if_p);
36119 cp_parser_end_omp_structured_block (parser, save);
36120 stmt = finish_oacc_data (clauses, block);
36121 return stmt;
36124 /* OpenACC 2.0:
36125 # pragma acc host_data <clauses> new-line
36126 structured-block */
36128 #define OACC_HOST_DATA_CLAUSE_MASK \
36129 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36131 static tree
36132 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36134 tree stmt, clauses, block;
36135 unsigned int save;
36137 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36138 "#pragma acc host_data", pragma_tok);
36140 block = begin_omp_parallel ();
36141 save = cp_parser_begin_omp_structured_block (parser);
36142 cp_parser_statement (parser, NULL_TREE, false, if_p);
36143 cp_parser_end_omp_structured_block (parser, save);
36144 stmt = finish_oacc_host_data (clauses, block);
36145 return stmt;
36148 /* OpenACC 2.0:
36149 # pragma acc declare oacc-data-clause[optseq] new-line
36152 #define OACC_DECLARE_CLAUSE_MASK \
36153 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36166 static tree
36167 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36169 tree clauses, stmt;
36170 bool error = false;
36172 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36173 "#pragma acc declare", pragma_tok, true);
36176 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36178 error_at (pragma_tok->location,
36179 "no valid clauses specified in %<#pragma acc declare%>");
36180 return NULL_TREE;
36183 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36185 location_t loc = OMP_CLAUSE_LOCATION (t);
36186 tree decl = OMP_CLAUSE_DECL (t);
36187 if (!DECL_P (decl))
36189 error_at (loc, "array section in %<#pragma acc declare%>");
36190 error = true;
36191 continue;
36193 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36194 switch (OMP_CLAUSE_MAP_KIND (t))
36196 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36197 case GOMP_MAP_FORCE_ALLOC:
36198 case GOMP_MAP_FORCE_TO:
36199 case GOMP_MAP_FORCE_DEVICEPTR:
36200 case GOMP_MAP_DEVICE_RESIDENT:
36201 break;
36203 case GOMP_MAP_LINK:
36204 if (!global_bindings_p ()
36205 && (TREE_STATIC (decl)
36206 || !DECL_EXTERNAL (decl)))
36208 error_at (loc,
36209 "%qD must be a global variable in "
36210 "%<#pragma acc declare link%>",
36211 decl);
36212 error = true;
36213 continue;
36215 break;
36217 default:
36218 if (global_bindings_p ())
36220 error_at (loc, "invalid OpenACC clause at file scope");
36221 error = true;
36222 continue;
36224 if (DECL_EXTERNAL (decl))
36226 error_at (loc,
36227 "invalid use of %<extern%> variable %qD "
36228 "in %<#pragma acc declare%>", decl);
36229 error = true;
36230 continue;
36232 else if (TREE_PUBLIC (decl))
36234 error_at (loc,
36235 "invalid use of %<global%> variable %qD "
36236 "in %<#pragma acc declare%>", decl);
36237 error = true;
36238 continue;
36240 break;
36243 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36244 || lookup_attribute ("omp declare target link",
36245 DECL_ATTRIBUTES (decl)))
36247 error_at (loc, "variable %qD used more than once with "
36248 "%<#pragma acc declare%>", decl);
36249 error = true;
36250 continue;
36253 if (!error)
36255 tree id;
36257 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36258 id = get_identifier ("omp declare target link");
36259 else
36260 id = get_identifier ("omp declare target");
36262 DECL_ATTRIBUTES (decl)
36263 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36264 if (global_bindings_p ())
36266 symtab_node *node = symtab_node::get (decl);
36267 if (node != NULL)
36269 node->offloadable = 1;
36270 if (ENABLE_OFFLOADING)
36272 g->have_offload = true;
36273 if (is_a <varpool_node *> (node))
36274 vec_safe_push (offload_vars, decl);
36281 if (error || global_bindings_p ())
36282 return NULL_TREE;
36284 stmt = make_node (OACC_DECLARE);
36285 TREE_TYPE (stmt) = void_type_node;
36286 OACC_DECLARE_CLAUSES (stmt) = clauses;
36287 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36289 add_stmt (stmt);
36291 return NULL_TREE;
36294 /* OpenACC 2.0:
36295 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36299 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36301 LOC is the location of the #pragma token.
36304 #define OACC_ENTER_DATA_CLAUSE_MASK \
36305 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36307 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36313 #define OACC_EXIT_DATA_CLAUSE_MASK \
36314 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36320 static tree
36321 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36322 bool enter)
36324 location_t loc = pragma_tok->location;
36325 tree stmt, clauses;
36326 const char *p = "";
36328 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36329 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36331 if (strcmp (p, "data") != 0)
36333 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36334 enter ? "enter" : "exit");
36335 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36336 return NULL_TREE;
36339 cp_lexer_consume_token (parser->lexer);
36341 if (enter)
36342 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36343 "#pragma acc enter data", pragma_tok);
36344 else
36345 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36346 "#pragma acc exit data", pragma_tok);
36348 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36350 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36351 enter ? "enter" : "exit");
36352 return NULL_TREE;
36355 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36356 TREE_TYPE (stmt) = void_type_node;
36357 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36358 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36359 add_stmt (stmt);
36360 return stmt;
36363 /* OpenACC 2.0:
36364 # pragma acc loop oacc-loop-clause[optseq] new-line
36365 structured-block */
36367 #define OACC_LOOP_CLAUSE_MASK \
36368 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36379 static tree
36380 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36381 omp_clause_mask mask, tree *cclauses, bool *if_p)
36383 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36385 strcat (p_name, " loop");
36386 mask |= OACC_LOOP_CLAUSE_MASK;
36388 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36389 cclauses == NULL);
36390 if (cclauses)
36392 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36393 if (*cclauses)
36394 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36395 if (clauses)
36396 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36399 tree block = begin_omp_structured_block ();
36400 int save = cp_parser_begin_omp_structured_block (parser);
36401 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36402 cp_parser_end_omp_structured_block (parser, save);
36403 add_stmt (finish_omp_structured_block (block));
36405 return stmt;
36408 /* OpenACC 2.0:
36409 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36410 structured-block
36414 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36415 structured-block
36418 #define OACC_KERNELS_CLAUSE_MASK \
36419 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36434 #define OACC_PARALLEL_CLAUSE_MASK \
36435 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36456 static tree
36457 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36458 char *p_name, bool *if_p)
36460 omp_clause_mask mask;
36461 enum tree_code code;
36462 switch (cp_parser_pragma_kind (pragma_tok))
36464 case PRAGMA_OACC_KERNELS:
36465 strcat (p_name, " kernels");
36466 mask = OACC_KERNELS_CLAUSE_MASK;
36467 code = OACC_KERNELS;
36468 break;
36469 case PRAGMA_OACC_PARALLEL:
36470 strcat (p_name, " parallel");
36471 mask = OACC_PARALLEL_CLAUSE_MASK;
36472 code = OACC_PARALLEL;
36473 break;
36474 default:
36475 gcc_unreachable ();
36478 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36480 const char *p
36481 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36482 if (strcmp (p, "loop") == 0)
36484 cp_lexer_consume_token (parser->lexer);
36485 tree block = begin_omp_parallel ();
36486 tree clauses;
36487 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36488 if_p);
36489 return finish_omp_construct (code, block, clauses);
36493 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36495 tree block = begin_omp_parallel ();
36496 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36497 cp_parser_statement (parser, NULL_TREE, false, if_p);
36498 cp_parser_end_omp_structured_block (parser, save);
36499 return finish_omp_construct (code, block, clauses);
36502 /* OpenACC 2.0:
36503 # pragma acc update oacc-update-clause[optseq] new-line
36506 #define OACC_UPDATE_CLAUSE_MASK \
36507 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36514 static tree
36515 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36517 tree stmt, clauses;
36519 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36520 "#pragma acc update", pragma_tok);
36522 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36524 error_at (pragma_tok->location,
36525 "%<#pragma acc update%> must contain at least one "
36526 "%<device%> or %<host%> or %<self%> clause");
36527 return NULL_TREE;
36530 stmt = make_node (OACC_UPDATE);
36531 TREE_TYPE (stmt) = void_type_node;
36532 OACC_UPDATE_CLAUSES (stmt) = clauses;
36533 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36534 add_stmt (stmt);
36535 return stmt;
36538 /* OpenACC 2.0:
36539 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36541 LOC is the location of the #pragma token.
36544 #define OACC_WAIT_CLAUSE_MASK \
36545 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36547 static tree
36548 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36550 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36551 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36553 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36554 list = cp_parser_oacc_wait_list (parser, loc, list);
36556 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36557 "#pragma acc wait", pragma_tok);
36559 stmt = c_finish_oacc_wait (loc, list, clauses);
36560 stmt = finish_expr_stmt (stmt);
36562 return stmt;
36565 /* OpenMP 4.0:
36566 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36568 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36569 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36576 static void
36577 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36578 enum pragma_context context)
36580 bool first_p = parser->omp_declare_simd == NULL;
36581 cp_omp_declare_simd_data data;
36582 if (first_p)
36584 data.error_seen = false;
36585 data.fndecl_seen = false;
36586 data.tokens = vNULL;
36587 data.clauses = NULL_TREE;
36588 /* It is safe to take the address of a local variable; it will only be
36589 used while this scope is live. */
36590 parser->omp_declare_simd = &data;
36593 /* Store away all pragma tokens. */
36594 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36595 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36596 cp_lexer_consume_token (parser->lexer);
36597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36598 parser->omp_declare_simd->error_seen = true;
36599 cp_parser_require_pragma_eol (parser, pragma_tok);
36600 struct cp_token_cache *cp
36601 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36602 parser->omp_declare_simd->tokens.safe_push (cp);
36604 if (first_p)
36606 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36607 cp_parser_pragma (parser, context, NULL);
36608 switch (context)
36610 case pragma_external:
36611 cp_parser_declaration (parser);
36612 break;
36613 case pragma_member:
36614 cp_parser_member_declaration (parser);
36615 break;
36616 case pragma_objc_icode:
36617 cp_parser_block_declaration (parser, /*statement_p=*/false);
36618 break;
36619 default:
36620 cp_parser_declaration_statement (parser);
36621 break;
36623 if (parser->omp_declare_simd
36624 && !parser->omp_declare_simd->error_seen
36625 && !parser->omp_declare_simd->fndecl_seen)
36626 error_at (pragma_tok->location,
36627 "%<#pragma omp declare simd%> not immediately followed by "
36628 "function declaration or definition");
36629 data.tokens.release ();
36630 parser->omp_declare_simd = NULL;
36634 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36635 This function is modelled similar to the late parsing of omp declare
36636 simd. */
36638 static tree
36639 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
36641 struct cp_token_cache *ce;
36642 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
36643 int ii = 0;
36645 if (parser->omp_declare_simd != NULL
36646 || lookup_attribute ("simd", attrs))
36648 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36649 "used in the same function marked as a Cilk Plus SIMD-enabled "
36650 "function");
36651 parser->cilk_simd_fn_info->tokens.release ();
36652 XDELETE (parser->cilk_simd_fn_info);
36653 parser->cilk_simd_fn_info = NULL;
36654 return attrs;
36656 if (!info->error_seen && info->fndecl_seen)
36658 error ("vector attribute not immediately followed by a single function"
36659 " declaration or definition");
36660 info->error_seen = true;
36662 if (info->error_seen)
36663 return attrs;
36665 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
36667 tree c, cl;
36669 cp_parser_push_lexer_for_tokens (parser, ce);
36670 parser->lexer->in_pragma = true;
36671 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
36672 "SIMD-enabled functions attribute",
36673 NULL);
36674 cp_parser_pop_lexer (parser);
36675 if (cl)
36676 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36678 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
36679 TREE_CHAIN (c) = attrs;
36680 attrs = c;
36682 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36683 TREE_CHAIN (c) = attrs;
36684 if (processing_template_decl)
36685 ATTR_IS_DEPENDENT (c) = 1;
36686 attrs = c;
36688 info->fndecl_seen = true;
36689 parser->cilk_simd_fn_info->tokens.release ();
36690 XDELETE (parser->cilk_simd_fn_info);
36691 parser->cilk_simd_fn_info = NULL;
36692 return attrs;
36695 /* Finalize #pragma omp declare simd clauses after direct declarator has
36696 been parsed, and put that into "omp declare simd" attribute. */
36698 static tree
36699 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
36701 struct cp_token_cache *ce;
36702 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
36703 int i;
36705 if (!data->error_seen && data->fndecl_seen)
36707 error ("%<#pragma omp declare simd%> not immediately followed by "
36708 "a single function declaration or definition");
36709 data->error_seen = true;
36711 if (data->error_seen)
36712 return attrs;
36714 FOR_EACH_VEC_ELT (data->tokens, i, ce)
36716 tree c, cl;
36718 cp_parser_push_lexer_for_tokens (parser, ce);
36719 parser->lexer->in_pragma = true;
36720 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36721 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36722 cp_lexer_consume_token (parser->lexer);
36723 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
36724 "#pragma omp declare simd", pragma_tok);
36725 cp_parser_pop_lexer (parser);
36726 if (cl)
36727 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36728 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36729 TREE_CHAIN (c) = attrs;
36730 if (processing_template_decl)
36731 ATTR_IS_DEPENDENT (c) = 1;
36732 attrs = c;
36735 data->fndecl_seen = true;
36736 return attrs;
36740 /* OpenMP 4.0:
36741 # pragma omp declare target new-line
36742 declarations and definitions
36743 # pragma omp end declare target new-line
36745 OpenMP 4.5:
36746 # pragma omp declare target ( extended-list ) new-line
36748 # pragma omp declare target declare-target-clauses[seq] new-line */
36750 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36751 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36754 static void
36755 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
36757 tree clauses = NULL_TREE;
36758 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36759 clauses
36760 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
36761 "#pragma omp declare target", pragma_tok);
36762 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36764 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36765 clauses);
36766 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36767 cp_parser_require_pragma_eol (parser, pragma_tok);
36769 else
36771 cp_parser_require_pragma_eol (parser, pragma_tok);
36772 scope_chain->omp_declare_target_attribute++;
36773 return;
36775 if (scope_chain->omp_declare_target_attribute)
36776 error_at (pragma_tok->location,
36777 "%<#pragma omp declare target%> with clauses in between "
36778 "%<#pragma omp declare target%> without clauses and "
36779 "%<#pragma omp end declare target%>");
36780 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
36782 tree t = OMP_CLAUSE_DECL (c), id;
36783 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
36784 tree at2 = lookup_attribute ("omp declare target link",
36785 DECL_ATTRIBUTES (t));
36786 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
36788 id = get_identifier ("omp declare target link");
36789 std::swap (at1, at2);
36791 else
36792 id = get_identifier ("omp declare target");
36793 if (at2)
36795 error_at (OMP_CLAUSE_LOCATION (c),
36796 "%qD specified both in declare target %<link%> and %<to%>"
36797 " clauses", t);
36798 continue;
36800 if (!at1)
36802 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
36803 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
36804 continue;
36806 symtab_node *node = symtab_node::get (t);
36807 if (node != NULL)
36809 node->offloadable = 1;
36810 if (ENABLE_OFFLOADING)
36812 g->have_offload = true;
36813 if (is_a <varpool_node *> (node))
36814 vec_safe_push (offload_vars, t);
36821 static void
36822 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
36824 const char *p = "";
36825 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36827 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36828 p = IDENTIFIER_POINTER (id);
36830 if (strcmp (p, "declare") == 0)
36832 cp_lexer_consume_token (parser->lexer);
36833 p = "";
36834 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36836 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36837 p = IDENTIFIER_POINTER (id);
36839 if (strcmp (p, "target") == 0)
36840 cp_lexer_consume_token (parser->lexer);
36841 else
36843 cp_parser_error (parser, "expected %<target%>");
36844 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36845 return;
36848 else
36850 cp_parser_error (parser, "expected %<declare%>");
36851 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36852 return;
36854 cp_parser_require_pragma_eol (parser, pragma_tok);
36855 if (!scope_chain->omp_declare_target_attribute)
36856 error_at (pragma_tok->location,
36857 "%<#pragma omp end declare target%> without corresponding "
36858 "%<#pragma omp declare target%>");
36859 else
36860 scope_chain->omp_declare_target_attribute--;
36863 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
36864 expression and optional initializer clause of
36865 #pragma omp declare reduction. We store the expression(s) as
36866 either 3, 6 or 7 special statements inside of the artificial function's
36867 body. The first two statements are DECL_EXPRs for the artificial
36868 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
36869 expression that uses those variables.
36870 If there was any INITIALIZER clause, this is followed by further statements,
36871 the fourth and fifth statements are DECL_EXPRs for the artificial
36872 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
36873 constructor variant (first token after open paren is not omp_priv),
36874 then the sixth statement is a statement with the function call expression
36875 that uses the OMP_PRIV and optionally OMP_ORIG variable.
36876 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
36877 to initialize the OMP_PRIV artificial variable and there is seventh
36878 statement, a DECL_EXPR of the OMP_PRIV statement again. */
36880 static bool
36881 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
36883 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
36884 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
36885 type = TREE_TYPE (type);
36886 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
36887 DECL_ARTIFICIAL (omp_out) = 1;
36888 pushdecl (omp_out);
36889 add_decl_expr (omp_out);
36890 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
36891 DECL_ARTIFICIAL (omp_in) = 1;
36892 pushdecl (omp_in);
36893 add_decl_expr (omp_in);
36894 tree combiner;
36895 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
36897 keep_next_level (true);
36898 tree block = begin_omp_structured_block ();
36899 combiner = cp_parser_expression (parser);
36900 finish_expr_stmt (combiner);
36901 block = finish_omp_structured_block (block);
36902 add_stmt (block);
36904 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36905 return false;
36907 const char *p = "";
36908 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36910 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36911 p = IDENTIFIER_POINTER (id);
36914 if (strcmp (p, "initializer") == 0)
36916 cp_lexer_consume_token (parser->lexer);
36917 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36918 return false;
36920 p = "";
36921 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36923 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36924 p = IDENTIFIER_POINTER (id);
36927 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
36928 DECL_ARTIFICIAL (omp_priv) = 1;
36929 pushdecl (omp_priv);
36930 add_decl_expr (omp_priv);
36931 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
36932 DECL_ARTIFICIAL (omp_orig) = 1;
36933 pushdecl (omp_orig);
36934 add_decl_expr (omp_orig);
36936 keep_next_level (true);
36937 block = begin_omp_structured_block ();
36939 bool ctor = false;
36940 if (strcmp (p, "omp_priv") == 0)
36942 bool is_direct_init, is_non_constant_init;
36943 ctor = true;
36944 cp_lexer_consume_token (parser->lexer);
36945 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36946 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36947 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36948 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36949 == CPP_CLOSE_PAREN
36950 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36951 == CPP_CLOSE_PAREN))
36953 finish_omp_structured_block (block);
36954 error ("invalid initializer clause");
36955 return false;
36957 initializer = cp_parser_initializer (parser, &is_direct_init,
36958 &is_non_constant_init);
36959 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36960 NULL_TREE, LOOKUP_ONLYCONVERTING);
36962 else
36964 cp_parser_parse_tentatively (parser);
36965 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36966 /*check_dependency_p=*/true,
36967 /*template_p=*/NULL,
36968 /*declarator_p=*/false,
36969 /*optional_p=*/false);
36970 vec<tree, va_gc> *args;
36971 if (fn_name == error_mark_node
36972 || cp_parser_error_occurred (parser)
36973 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36974 || ((args = cp_parser_parenthesized_expression_list
36975 (parser, non_attr, /*cast_p=*/false,
36976 /*allow_expansion_p=*/true,
36977 /*non_constant_p=*/NULL)),
36978 cp_parser_error_occurred (parser)))
36980 finish_omp_structured_block (block);
36981 cp_parser_abort_tentative_parse (parser);
36982 cp_parser_error (parser, "expected id-expression (arguments)");
36983 return false;
36985 unsigned int i;
36986 tree arg;
36987 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36988 if (arg == omp_priv
36989 || (TREE_CODE (arg) == ADDR_EXPR
36990 && TREE_OPERAND (arg, 0) == omp_priv))
36991 break;
36992 cp_parser_abort_tentative_parse (parser);
36993 if (arg == NULL_TREE)
36994 error ("one of the initializer call arguments should be %<omp_priv%>"
36995 " or %<&omp_priv%>");
36996 initializer = cp_parser_postfix_expression (parser, false, false, false,
36997 false, NULL);
36998 finish_expr_stmt (initializer);
37001 block = finish_omp_structured_block (block);
37002 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37003 add_stmt (block);
37005 if (ctor)
37006 add_decl_expr (omp_orig);
37008 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37009 return false;
37012 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37013 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
37015 return true;
37018 /* OpenMP 4.0
37019 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37020 initializer-clause[opt] new-line
37022 initializer-clause:
37023 initializer (omp_priv initializer)
37024 initializer (function-name (argument-list)) */
37026 static void
37027 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37028 enum pragma_context)
37030 auto_vec<tree> types;
37031 enum tree_code reduc_code = ERROR_MARK;
37032 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37033 unsigned int i;
37034 cp_token *first_token;
37035 cp_token_cache *cp;
37036 int errs;
37037 void *p;
37039 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37040 p = obstack_alloc (&declarator_obstack, 0);
37042 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37043 goto fail;
37045 switch (cp_lexer_peek_token (parser->lexer)->type)
37047 case CPP_PLUS:
37048 reduc_code = PLUS_EXPR;
37049 break;
37050 case CPP_MULT:
37051 reduc_code = MULT_EXPR;
37052 break;
37053 case CPP_MINUS:
37054 reduc_code = MINUS_EXPR;
37055 break;
37056 case CPP_AND:
37057 reduc_code = BIT_AND_EXPR;
37058 break;
37059 case CPP_XOR:
37060 reduc_code = BIT_XOR_EXPR;
37061 break;
37062 case CPP_OR:
37063 reduc_code = BIT_IOR_EXPR;
37064 break;
37065 case CPP_AND_AND:
37066 reduc_code = TRUTH_ANDIF_EXPR;
37067 break;
37068 case CPP_OR_OR:
37069 reduc_code = TRUTH_ORIF_EXPR;
37070 break;
37071 case CPP_NAME:
37072 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37073 break;
37074 default:
37075 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37076 "%<|%>, %<&&%>, %<||%> or identifier");
37077 goto fail;
37080 if (reduc_code != ERROR_MARK)
37081 cp_lexer_consume_token (parser->lexer);
37083 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37084 if (reduc_id == error_mark_node)
37085 goto fail;
37087 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37088 goto fail;
37090 /* Types may not be defined in declare reduction type list. */
37091 const char *saved_message;
37092 saved_message = parser->type_definition_forbidden_message;
37093 parser->type_definition_forbidden_message
37094 = G_("types may not be defined in declare reduction type list");
37095 bool saved_colon_corrects_to_scope_p;
37096 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37097 parser->colon_corrects_to_scope_p = false;
37098 bool saved_colon_doesnt_start_class_def_p;
37099 saved_colon_doesnt_start_class_def_p
37100 = parser->colon_doesnt_start_class_def_p;
37101 parser->colon_doesnt_start_class_def_p = true;
37103 while (true)
37105 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37106 type = cp_parser_type_id (parser);
37107 if (type == error_mark_node)
37109 else if (ARITHMETIC_TYPE_P (type)
37110 && (orig_reduc_id == NULL_TREE
37111 || (TREE_CODE (type) != COMPLEX_TYPE
37112 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37113 "min") == 0
37114 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37115 "max") == 0))))
37116 error_at (loc, "predeclared arithmetic type %qT in "
37117 "%<#pragma omp declare reduction%>", type);
37118 else if (TREE_CODE (type) == FUNCTION_TYPE
37119 || TREE_CODE (type) == METHOD_TYPE
37120 || TREE_CODE (type) == ARRAY_TYPE)
37121 error_at (loc, "function or array type %qT in "
37122 "%<#pragma omp declare reduction%>", type);
37123 else if (TREE_CODE (type) == REFERENCE_TYPE)
37124 error_at (loc, "reference type %qT in "
37125 "%<#pragma omp declare reduction%>", type);
37126 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37127 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37128 "%<#pragma omp declare reduction%>", type);
37129 else
37130 types.safe_push (type);
37132 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37133 cp_lexer_consume_token (parser->lexer);
37134 else
37135 break;
37138 /* Restore the saved message. */
37139 parser->type_definition_forbidden_message = saved_message;
37140 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37141 parser->colon_doesnt_start_class_def_p
37142 = saved_colon_doesnt_start_class_def_p;
37144 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37145 || types.is_empty ())
37147 fail:
37148 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37149 goto done;
37152 first_token = cp_lexer_peek_token (parser->lexer);
37153 cp = NULL;
37154 errs = errorcount;
37155 FOR_EACH_VEC_ELT (types, i, type)
37157 tree fntype
37158 = build_function_type_list (void_type_node,
37159 cp_build_reference_type (type, false),
37160 NULL_TREE);
37161 tree this_reduc_id = reduc_id;
37162 if (!dependent_type_p (type))
37163 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37164 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37165 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37166 DECL_ARTIFICIAL (fndecl) = 1;
37167 DECL_EXTERNAL (fndecl) = 1;
37168 DECL_DECLARED_INLINE_P (fndecl) = 1;
37169 DECL_IGNORED_P (fndecl) = 1;
37170 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37171 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37172 DECL_ATTRIBUTES (fndecl)
37173 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37174 DECL_ATTRIBUTES (fndecl));
37175 if (processing_template_decl)
37176 fndecl = push_template_decl (fndecl);
37177 bool block_scope = false;
37178 tree block = NULL_TREE;
37179 if (current_function_decl)
37181 block_scope = true;
37182 DECL_CONTEXT (fndecl) = global_namespace;
37183 if (!processing_template_decl)
37184 pushdecl (fndecl);
37186 else if (current_class_type)
37188 if (cp == NULL)
37190 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37191 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37192 cp_lexer_consume_token (parser->lexer);
37193 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37194 goto fail;
37195 cp = cp_token_cache_new (first_token,
37196 cp_lexer_peek_nth_token (parser->lexer,
37197 2));
37199 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37200 finish_member_declaration (fndecl);
37201 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37202 DECL_PENDING_INLINE_P (fndecl) = 1;
37203 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37204 continue;
37206 else
37208 DECL_CONTEXT (fndecl) = current_namespace;
37209 pushdecl (fndecl);
37211 if (!block_scope)
37212 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37213 else
37214 block = begin_omp_structured_block ();
37215 if (cp)
37217 cp_parser_push_lexer_for_tokens (parser, cp);
37218 parser->lexer->in_pragma = true;
37220 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37222 if (!block_scope)
37223 finish_function (0);
37224 else
37225 DECL_CONTEXT (fndecl) = current_function_decl;
37226 if (cp)
37227 cp_parser_pop_lexer (parser);
37228 goto fail;
37230 if (cp)
37231 cp_parser_pop_lexer (parser);
37232 if (!block_scope)
37233 finish_function (0);
37234 else
37236 DECL_CONTEXT (fndecl) = current_function_decl;
37237 block = finish_omp_structured_block (block);
37238 if (TREE_CODE (block) == BIND_EXPR)
37239 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37240 else if (TREE_CODE (block) == STATEMENT_LIST)
37241 DECL_SAVED_TREE (fndecl) = block;
37242 if (processing_template_decl)
37243 add_decl_expr (fndecl);
37245 cp_check_omp_declare_reduction (fndecl);
37246 if (cp == NULL && types.length () > 1)
37247 cp = cp_token_cache_new (first_token,
37248 cp_lexer_peek_nth_token (parser->lexer, 2));
37249 if (errs != errorcount)
37250 break;
37253 cp_parser_require_pragma_eol (parser, pragma_tok);
37255 done:
37256 /* Free any declarators allocated. */
37257 obstack_free (&declarator_obstack, p);
37260 /* OpenMP 4.0
37261 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37262 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37263 initializer-clause[opt] new-line
37264 #pragma omp declare target new-line */
37266 static void
37267 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37268 enum pragma_context context)
37270 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37272 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37273 const char *p = IDENTIFIER_POINTER (id);
37275 if (strcmp (p, "simd") == 0)
37277 cp_lexer_consume_token (parser->lexer);
37278 cp_parser_omp_declare_simd (parser, pragma_tok,
37279 context);
37280 return;
37282 cp_ensure_no_omp_declare_simd (parser);
37283 if (strcmp (p, "reduction") == 0)
37285 cp_lexer_consume_token (parser->lexer);
37286 cp_parser_omp_declare_reduction (parser, pragma_tok,
37287 context);
37288 return;
37290 if (!flag_openmp) /* flag_openmp_simd */
37292 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37293 return;
37295 if (strcmp (p, "target") == 0)
37297 cp_lexer_consume_token (parser->lexer);
37298 cp_parser_omp_declare_target (parser, pragma_tok);
37299 return;
37302 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37303 "or %<target%>");
37304 cp_parser_require_pragma_eol (parser, pragma_tok);
37307 /* OpenMP 4.5:
37308 #pragma omp taskloop taskloop-clause[optseq] new-line
37309 for-loop
37311 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37312 for-loop */
37314 #define OMP_TASKLOOP_CLAUSE_MASK \
37315 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37330 static tree
37331 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37332 char *p_name, omp_clause_mask mask, tree *cclauses,
37333 bool *if_p)
37335 tree clauses, sb, ret;
37336 unsigned int save;
37337 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37339 strcat (p_name, " taskloop");
37340 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37342 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37344 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37345 const char *p = IDENTIFIER_POINTER (id);
37347 if (strcmp (p, "simd") == 0)
37349 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37350 if (cclauses == NULL)
37351 cclauses = cclauses_buf;
37353 cp_lexer_consume_token (parser->lexer);
37354 if (!flag_openmp) /* flag_openmp_simd */
37355 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37356 cclauses, if_p);
37357 sb = begin_omp_structured_block ();
37358 save = cp_parser_begin_omp_structured_block (parser);
37359 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37360 cclauses, if_p);
37361 cp_parser_end_omp_structured_block (parser, save);
37362 tree body = finish_omp_structured_block (sb);
37363 if (ret == NULL)
37364 return ret;
37365 ret = make_node (OMP_TASKLOOP);
37366 TREE_TYPE (ret) = void_type_node;
37367 OMP_FOR_BODY (ret) = body;
37368 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37369 SET_EXPR_LOCATION (ret, loc);
37370 add_stmt (ret);
37371 return ret;
37374 if (!flag_openmp) /* flag_openmp_simd */
37376 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37377 return NULL_TREE;
37380 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37381 cclauses == NULL);
37382 if (cclauses)
37384 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37385 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37388 sb = begin_omp_structured_block ();
37389 save = cp_parser_begin_omp_structured_block (parser);
37391 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37392 if_p);
37394 cp_parser_end_omp_structured_block (parser, save);
37395 add_stmt (finish_omp_structured_block (sb));
37397 return ret;
37401 /* OpenACC 2.0:
37402 # pragma acc routine oacc-routine-clause[optseq] new-line
37403 function-definition
37405 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37408 #define OACC_ROUTINE_CLAUSE_MASK \
37409 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37415 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37416 component, which must resolve to a declared namespace-scope
37417 function. The clauses are either processed directly (for a named
37418 function), or defered until the immediatley following declaration
37419 is parsed. */
37421 static void
37422 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37423 enum pragma_context context)
37425 gcc_checking_assert (context == pragma_external);
37426 /* The checking for "another pragma following this one" in the "no optional
37427 '( name )'" case makes sure that we dont re-enter. */
37428 gcc_checking_assert (parser->oacc_routine == NULL);
37430 cp_oacc_routine_data data;
37431 data.error_seen = false;
37432 data.fndecl_seen = false;
37433 data.tokens = vNULL;
37434 data.clauses = NULL_TREE;
37435 data.loc = pragma_tok->location;
37436 /* It is safe to take the address of a local variable; it will only be
37437 used while this scope is live. */
37438 parser->oacc_routine = &data;
37440 /* Look for optional '( name )'. */
37441 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37443 cp_lexer_consume_token (parser->lexer); /* '(' */
37445 /* We parse the name as an id-expression. If it resolves to
37446 anything other than a non-overloaded function at namespace
37447 scope, it's an error. */
37448 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37449 tree name = cp_parser_id_expression (parser,
37450 /*template_keyword_p=*/false,
37451 /*check_dependency_p=*/false,
37452 /*template_p=*/NULL,
37453 /*declarator_p=*/false,
37454 /*optional_p=*/false);
37455 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37456 if (name != error_mark_node && decl == error_mark_node)
37457 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37459 if (decl == error_mark_node
37460 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37462 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37463 parser->oacc_routine = NULL;
37464 return;
37467 data.clauses
37468 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37469 "#pragma acc routine",
37470 cp_lexer_peek_token (parser->lexer));
37472 if (decl && is_overloaded_fn (decl)
37473 && (TREE_CODE (decl) != FUNCTION_DECL
37474 || DECL_FUNCTION_TEMPLATE_P (decl)))
37476 error_at (name_loc,
37477 "%<#pragma acc routine%> names a set of overloads");
37478 parser->oacc_routine = NULL;
37479 return;
37482 /* Perhaps we should use the same rule as declarations in different
37483 namespaces? */
37484 if (!DECL_NAMESPACE_SCOPE_P (decl))
37486 error_at (name_loc,
37487 "%qD does not refer to a namespace scope function", decl);
37488 parser->oacc_routine = NULL;
37489 return;
37492 if (TREE_CODE (decl) != FUNCTION_DECL)
37494 error_at (name_loc, "%qD does not refer to a function", decl);
37495 parser->oacc_routine = NULL;
37496 return;
37499 cp_finalize_oacc_routine (parser, decl, false);
37500 parser->oacc_routine = NULL;
37502 else /* No optional '( name )'. */
37504 /* Store away all pragma tokens. */
37505 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37506 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37507 cp_lexer_consume_token (parser->lexer);
37508 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37509 parser->oacc_routine->error_seen = true;
37510 cp_parser_require_pragma_eol (parser, pragma_tok);
37511 struct cp_token_cache *cp
37512 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37513 parser->oacc_routine->tokens.safe_push (cp);
37515 /* Emit a helpful diagnostic if there's another pragma following this
37516 one. */
37517 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37519 cp_ensure_no_oacc_routine (parser);
37520 data.tokens.release ();
37521 /* ..., and then just keep going. */
37522 return;
37525 /* We only have to consider the pragma_external case here. */
37526 cp_parser_declaration (parser);
37527 if (parser->oacc_routine
37528 && !parser->oacc_routine->fndecl_seen)
37529 cp_ensure_no_oacc_routine (parser);
37530 else
37531 parser->oacc_routine = NULL;
37532 data.tokens.release ();
37536 /* Finalize #pragma acc routine clauses after direct declarator has
37537 been parsed. */
37539 static tree
37540 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37542 struct cp_token_cache *ce;
37543 cp_oacc_routine_data *data = parser->oacc_routine;
37545 if (!data->error_seen && data->fndecl_seen)
37547 error_at (data->loc,
37548 "%<#pragma acc routine%> not immediately followed by "
37549 "a single function declaration or definition");
37550 data->error_seen = true;
37552 if (data->error_seen)
37553 return attrs;
37555 gcc_checking_assert (data->tokens.length () == 1);
37556 ce = data->tokens[0];
37558 cp_parser_push_lexer_for_tokens (parser, ce);
37559 parser->lexer->in_pragma = true;
37560 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37562 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37563 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37564 parser->oacc_routine->clauses
37565 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37566 "#pragma acc routine", pragma_tok);
37567 cp_parser_pop_lexer (parser);
37568 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37569 fndecl_seen. */
37571 return attrs;
37574 /* Apply any saved OpenACC routine clauses to a just-parsed
37575 declaration. */
37577 static void
37578 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37580 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37582 /* Keep going if we're in error reporting mode. */
37583 if (parser->oacc_routine->error_seen
37584 || fndecl == error_mark_node)
37585 return;
37587 if (parser->oacc_routine->fndecl_seen)
37589 error_at (parser->oacc_routine->loc,
37590 "%<#pragma acc routine%> not immediately followed by"
37591 " a single function declaration or definition");
37592 parser->oacc_routine = NULL;
37593 return;
37595 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37597 cp_ensure_no_oacc_routine (parser);
37598 return;
37601 if (oacc_get_fn_attrib (fndecl))
37603 error_at (parser->oacc_routine->loc,
37604 "%<#pragma acc routine%> already applied to %qD", fndecl);
37605 parser->oacc_routine = NULL;
37606 return;
37609 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37611 error_at (parser->oacc_routine->loc,
37612 TREE_USED (fndecl)
37613 ? G_("%<#pragma acc routine%> must be applied before use")
37614 : G_("%<#pragma acc routine%> must be applied before "
37615 "definition"));
37616 parser->oacc_routine = NULL;
37617 return;
37620 /* Process the routine's dimension clauses. */
37621 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37622 oacc_replace_fn_attrib (fndecl, dims);
37624 /* Add an "omp declare target" attribute. */
37625 DECL_ATTRIBUTES (fndecl)
37626 = tree_cons (get_identifier ("omp declare target"),
37627 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37629 /* Don't unset parser->oacc_routine here: we may still need it to
37630 diagnose wrong usage. But, remember that we've used this "#pragma acc
37631 routine". */
37632 parser->oacc_routine->fndecl_seen = true;
37636 /* Main entry point to OpenMP statement pragmas. */
37638 static void
37639 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37641 tree stmt;
37642 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37643 omp_clause_mask mask (0);
37645 switch (cp_parser_pragma_kind (pragma_tok))
37647 case PRAGMA_OACC_ATOMIC:
37648 cp_parser_omp_atomic (parser, pragma_tok);
37649 return;
37650 case PRAGMA_OACC_CACHE:
37651 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37652 break;
37653 case PRAGMA_OACC_DATA:
37654 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37655 break;
37656 case PRAGMA_OACC_ENTER_DATA:
37657 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37658 break;
37659 case PRAGMA_OACC_EXIT_DATA:
37660 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37661 break;
37662 case PRAGMA_OACC_HOST_DATA:
37663 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37664 break;
37665 case PRAGMA_OACC_KERNELS:
37666 case PRAGMA_OACC_PARALLEL:
37667 strcpy (p_name, "#pragma acc");
37668 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37669 if_p);
37670 break;
37671 case PRAGMA_OACC_LOOP:
37672 strcpy (p_name, "#pragma acc");
37673 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37674 if_p);
37675 break;
37676 case PRAGMA_OACC_UPDATE:
37677 stmt = cp_parser_oacc_update (parser, pragma_tok);
37678 break;
37679 case PRAGMA_OACC_WAIT:
37680 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37681 break;
37682 case PRAGMA_OMP_ATOMIC:
37683 cp_parser_omp_atomic (parser, pragma_tok);
37684 return;
37685 case PRAGMA_OMP_CRITICAL:
37686 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37687 break;
37688 case PRAGMA_OMP_DISTRIBUTE:
37689 strcpy (p_name, "#pragma omp");
37690 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
37691 if_p);
37692 break;
37693 case PRAGMA_OMP_FOR:
37694 strcpy (p_name, "#pragma omp");
37695 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
37696 if_p);
37697 break;
37698 case PRAGMA_OMP_MASTER:
37699 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
37700 break;
37701 case PRAGMA_OMP_PARALLEL:
37702 strcpy (p_name, "#pragma omp");
37703 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
37704 if_p);
37705 break;
37706 case PRAGMA_OMP_SECTIONS:
37707 strcpy (p_name, "#pragma omp");
37708 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
37709 break;
37710 case PRAGMA_OMP_SIMD:
37711 strcpy (p_name, "#pragma omp");
37712 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
37713 if_p);
37714 break;
37715 case PRAGMA_OMP_SINGLE:
37716 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
37717 break;
37718 case PRAGMA_OMP_TASK:
37719 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
37720 break;
37721 case PRAGMA_OMP_TASKGROUP:
37722 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
37723 break;
37724 case PRAGMA_OMP_TASKLOOP:
37725 strcpy (p_name, "#pragma omp");
37726 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
37727 if_p);
37728 break;
37729 case PRAGMA_OMP_TEAMS:
37730 strcpy (p_name, "#pragma omp");
37731 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
37732 if_p);
37733 break;
37734 default:
37735 gcc_unreachable ();
37738 protected_set_expr_location (stmt, pragma_tok->location);
37741 /* Transactional Memory parsing routines. */
37743 /* Parse a transaction attribute.
37745 txn-attribute:
37746 attribute
37747 [ [ identifier ] ]
37749 We use this instead of cp_parser_attributes_opt for transactions to avoid
37750 the pedwarn in C++98 mode. */
37752 static tree
37753 cp_parser_txn_attribute_opt (cp_parser *parser)
37755 cp_token *token;
37756 tree attr_name, attr = NULL;
37758 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
37759 return cp_parser_attributes_opt (parser);
37761 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
37762 return NULL_TREE;
37763 cp_lexer_consume_token (parser->lexer);
37764 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
37765 goto error1;
37767 token = cp_lexer_peek_token (parser->lexer);
37768 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
37770 token = cp_lexer_consume_token (parser->lexer);
37772 attr_name = (token->type == CPP_KEYWORD
37773 /* For keywords, use the canonical spelling,
37774 not the parsed identifier. */
37775 ? ridpointers[(int) token->keyword]
37776 : token->u.value);
37777 attr = build_tree_list (attr_name, NULL_TREE);
37779 else
37780 cp_parser_error (parser, "expected identifier");
37782 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37783 error1:
37784 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37785 return attr;
37788 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37790 transaction-statement:
37791 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37792 compound-statement
37793 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37796 static tree
37797 cp_parser_transaction (cp_parser *parser, cp_token *token)
37799 unsigned char old_in = parser->in_transaction;
37800 unsigned char this_in = 1, new_in;
37801 enum rid keyword = token->keyword;
37802 tree stmt, attrs, noex;
37804 cp_lexer_consume_token (parser->lexer);
37806 if (keyword == RID_TRANSACTION_RELAXED
37807 || keyword == RID_SYNCHRONIZED)
37808 this_in |= TM_STMT_ATTR_RELAXED;
37809 else
37811 attrs = cp_parser_txn_attribute_opt (parser);
37812 if (attrs)
37813 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37816 /* Parse a noexcept specification. */
37817 if (keyword == RID_ATOMIC_NOEXCEPT)
37818 noex = boolean_true_node;
37819 else if (keyword == RID_ATOMIC_CANCEL)
37821 /* cancel-and-throw is unimplemented. */
37822 sorry ("atomic_cancel");
37823 noex = NULL_TREE;
37825 else
37826 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
37828 /* Keep track if we're in the lexical scope of an outer transaction. */
37829 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
37831 stmt = begin_transaction_stmt (token->location, NULL, this_in);
37833 parser->in_transaction = new_in;
37834 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
37835 parser->in_transaction = old_in;
37837 finish_transaction_stmt (stmt, NULL, this_in, noex);
37839 return stmt;
37842 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37844 transaction-expression:
37845 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37846 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37849 static tree
37850 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
37852 unsigned char old_in = parser->in_transaction;
37853 unsigned char this_in = 1;
37854 cp_token *token;
37855 tree expr, noex;
37856 bool noex_expr;
37857 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37859 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37860 || keyword == RID_TRANSACTION_RELAXED);
37862 if (!flag_tm)
37863 error_at (loc,
37864 keyword == RID_TRANSACTION_RELAXED
37865 ? G_("%<__transaction_relaxed%> without transactional memory "
37866 "support enabled")
37867 : G_("%<__transaction_atomic%> without transactional memory "
37868 "support enabled"));
37870 token = cp_parser_require_keyword (parser, keyword,
37871 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37872 : RT_TRANSACTION_RELAXED));
37873 gcc_assert (token != NULL);
37875 if (keyword == RID_TRANSACTION_RELAXED)
37876 this_in |= TM_STMT_ATTR_RELAXED;
37878 /* Set this early. This might mean that we allow transaction_cancel in
37879 an expression that we find out later actually has to be a constexpr.
37880 However, we expect that cxx_constant_value will be able to deal with
37881 this; also, if the noexcept has no constexpr, then what we parse next
37882 really is a transaction's body. */
37883 parser->in_transaction = this_in;
37885 /* Parse a noexcept specification. */
37886 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37887 true);
37889 if (!noex || !noex_expr
37890 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37892 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37894 expr = cp_parser_expression (parser);
37895 expr = finish_parenthesized_expr (expr);
37897 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37899 else
37901 /* The only expression that is available got parsed for the noexcept
37902 already. noexcept is true then. */
37903 expr = noex;
37904 noex = boolean_true_node;
37907 expr = build_transaction_expr (token->location, expr, this_in, noex);
37908 parser->in_transaction = old_in;
37910 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37911 return error_mark_node;
37913 return (flag_tm ? expr : error_mark_node);
37916 /* Parse a function-transaction-block.
37918 function-transaction-block:
37919 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37920 function-body
37921 __transaction_atomic txn-attribute[opt] function-try-block
37922 __transaction_relaxed ctor-initializer[opt] function-body
37923 __transaction_relaxed function-try-block
37926 static bool
37927 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37929 unsigned char old_in = parser->in_transaction;
37930 unsigned char new_in = 1;
37931 tree compound_stmt, stmt, attrs;
37932 bool ctor_initializer_p;
37933 cp_token *token;
37935 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37936 || keyword == RID_TRANSACTION_RELAXED);
37937 token = cp_parser_require_keyword (parser, keyword,
37938 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37939 : RT_TRANSACTION_RELAXED));
37940 gcc_assert (token != NULL);
37942 if (keyword == RID_TRANSACTION_RELAXED)
37943 new_in |= TM_STMT_ATTR_RELAXED;
37944 else
37946 attrs = cp_parser_txn_attribute_opt (parser);
37947 if (attrs)
37948 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37951 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37953 parser->in_transaction = new_in;
37955 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37956 ctor_initializer_p = cp_parser_function_try_block (parser);
37957 else
37958 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37959 (parser, /*in_function_try_block=*/false);
37961 parser->in_transaction = old_in;
37963 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37965 return ctor_initializer_p;
37968 /* Parse a __transaction_cancel statement.
37970 cancel-statement:
37971 __transaction_cancel txn-attribute[opt] ;
37972 __transaction_cancel txn-attribute[opt] throw-expression ;
37974 ??? Cancel and throw is not yet implemented. */
37976 static tree
37977 cp_parser_transaction_cancel (cp_parser *parser)
37979 cp_token *token;
37980 bool is_outer = false;
37981 tree stmt, attrs;
37983 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37984 RT_TRANSACTION_CANCEL);
37985 gcc_assert (token != NULL);
37987 attrs = cp_parser_txn_attribute_opt (parser);
37988 if (attrs)
37989 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37991 /* ??? Parse cancel-and-throw here. */
37993 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37995 if (!flag_tm)
37997 error_at (token->location, "%<__transaction_cancel%> without "
37998 "transactional memory support enabled");
37999 return error_mark_node;
38001 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38003 error_at (token->location, "%<__transaction_cancel%> within a "
38004 "%<__transaction_relaxed%>");
38005 return error_mark_node;
38007 else if (is_outer)
38009 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38010 && !is_tm_may_cancel_outer (current_function_decl))
38012 error_at (token->location, "outer %<__transaction_cancel%> not "
38013 "within outer %<__transaction_atomic%>");
38014 error_at (token->location,
38015 " or a %<transaction_may_cancel_outer%> function");
38016 return error_mark_node;
38019 else if (parser->in_transaction == 0)
38021 error_at (token->location, "%<__transaction_cancel%> not within "
38022 "%<__transaction_atomic%>");
38023 return error_mark_node;
38026 stmt = build_tm_abort_call (token->location, is_outer);
38027 add_stmt (stmt);
38029 return stmt;
38032 /* The parser. */
38034 static GTY (()) cp_parser *the_parser;
38037 /* Special handling for the first token or line in the file. The first
38038 thing in the file might be #pragma GCC pch_preprocess, which loads a
38039 PCH file, which is a GC collection point. So we need to handle this
38040 first pragma without benefit of an existing lexer structure.
38042 Always returns one token to the caller in *FIRST_TOKEN. This is
38043 either the true first token of the file, or the first token after
38044 the initial pragma. */
38046 static void
38047 cp_parser_initial_pragma (cp_token *first_token)
38049 tree name = NULL;
38051 cp_lexer_get_preprocessor_token (NULL, first_token);
38052 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38053 return;
38055 cp_lexer_get_preprocessor_token (NULL, first_token);
38056 if (first_token->type == CPP_STRING)
38058 name = first_token->u.value;
38060 cp_lexer_get_preprocessor_token (NULL, first_token);
38061 if (first_token->type != CPP_PRAGMA_EOL)
38062 error_at (first_token->location,
38063 "junk at end of %<#pragma GCC pch_preprocess%>");
38065 else
38066 error_at (first_token->location, "expected string literal");
38068 /* Skip to the end of the pragma. */
38069 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38070 cp_lexer_get_preprocessor_token (NULL, first_token);
38072 /* Now actually load the PCH file. */
38073 if (name)
38074 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38076 /* Read one more token to return to our caller. We have to do this
38077 after reading the PCH file in, since its pointers have to be
38078 live. */
38079 cp_lexer_get_preprocessor_token (NULL, first_token);
38082 /* Parses the grainsize pragma for the _Cilk_for statement.
38083 Syntax:
38084 #pragma cilk grainsize = <VALUE>. */
38086 static void
38087 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38089 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38091 tree exp = cp_parser_binary_expression (parser, false, false,
38092 PREC_NOT_OPERATOR, NULL);
38093 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38094 if (!exp || exp == error_mark_node)
38096 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38097 return;
38100 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38101 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38102 cp_parser_cilk_for (parser, exp, if_p);
38103 else
38104 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38105 "%<#pragma cilk grainsize%> is not followed by "
38106 "%<_Cilk_for%>");
38107 return;
38109 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38112 /* Normal parsing of a pragma token. Here we can (and must) use the
38113 regular lexer. */
38115 static bool
38116 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38118 cp_token *pragma_tok;
38119 unsigned int id;
38120 tree stmt;
38121 bool ret;
38123 pragma_tok = cp_lexer_consume_token (parser->lexer);
38124 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38125 parser->lexer->in_pragma = true;
38127 id = cp_parser_pragma_kind (pragma_tok);
38128 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38129 cp_ensure_no_omp_declare_simd (parser);
38130 switch (id)
38132 case PRAGMA_GCC_PCH_PREPROCESS:
38133 error_at (pragma_tok->location,
38134 "%<#pragma GCC pch_preprocess%> must be first");
38135 break;
38137 case PRAGMA_OMP_BARRIER:
38138 switch (context)
38140 case pragma_compound:
38141 cp_parser_omp_barrier (parser, pragma_tok);
38142 return false;
38143 case pragma_stmt:
38144 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38145 "used in compound statements", "omp barrier");
38146 break;
38147 default:
38148 goto bad_stmt;
38150 break;
38152 case PRAGMA_OMP_FLUSH:
38153 switch (context)
38155 case pragma_compound:
38156 cp_parser_omp_flush (parser, pragma_tok);
38157 return false;
38158 case pragma_stmt:
38159 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38160 "used in compound statements", "omp flush");
38161 break;
38162 default:
38163 goto bad_stmt;
38165 break;
38167 case PRAGMA_OMP_TASKWAIT:
38168 switch (context)
38170 case pragma_compound:
38171 cp_parser_omp_taskwait (parser, pragma_tok);
38172 return false;
38173 case pragma_stmt:
38174 error_at (pragma_tok->location,
38175 "%<#pragma %s%> may only be used in compound statements",
38176 "omp taskwait");
38177 break;
38178 default:
38179 goto bad_stmt;
38181 break;
38183 case PRAGMA_OMP_TASKYIELD:
38184 switch (context)
38186 case pragma_compound:
38187 cp_parser_omp_taskyield (parser, pragma_tok);
38188 return false;
38189 case pragma_stmt:
38190 error_at (pragma_tok->location,
38191 "%<#pragma %s%> may only be used in compound statements",
38192 "omp taskyield");
38193 break;
38194 default:
38195 goto bad_stmt;
38197 break;
38199 case PRAGMA_OMP_CANCEL:
38200 switch (context)
38202 case pragma_compound:
38203 cp_parser_omp_cancel (parser, pragma_tok);
38204 return false;
38205 case pragma_stmt:
38206 error_at (pragma_tok->location,
38207 "%<#pragma %s%> may only be used in compound statements",
38208 "omp cancel");
38209 break;
38210 default:
38211 goto bad_stmt;
38213 break;
38215 case PRAGMA_OMP_CANCELLATION_POINT:
38216 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38217 return false;
38219 case PRAGMA_OMP_THREADPRIVATE:
38220 cp_parser_omp_threadprivate (parser, pragma_tok);
38221 return false;
38223 case PRAGMA_OMP_DECLARE:
38224 cp_parser_omp_declare (parser, pragma_tok, context);
38225 return false;
38227 case PRAGMA_OACC_DECLARE:
38228 cp_parser_oacc_declare (parser, pragma_tok);
38229 return false;
38231 case PRAGMA_OACC_ENTER_DATA:
38232 if (context == pragma_stmt)
38234 error_at (pragma_tok->location,
38235 "%<#pragma %s%> may only be used in compound statements",
38236 "acc enter data");
38237 break;
38239 else if (context != pragma_compound)
38240 goto bad_stmt;
38241 cp_parser_omp_construct (parser, pragma_tok, if_p);
38242 return true;
38244 case PRAGMA_OACC_EXIT_DATA:
38245 if (context == pragma_stmt)
38247 error_at (pragma_tok->location,
38248 "%<#pragma %s%> may only be used in compound statements",
38249 "acc exit data");
38250 break;
38252 else if (context != pragma_compound)
38253 goto bad_stmt;
38254 cp_parser_omp_construct (parser, pragma_tok, if_p);
38255 return true;
38257 case PRAGMA_OACC_ROUTINE:
38258 if (context != pragma_external)
38260 error_at (pragma_tok->location,
38261 "%<#pragma acc routine%> must be at file scope");
38262 break;
38264 cp_parser_oacc_routine (parser, pragma_tok, context);
38265 return false;
38267 case PRAGMA_OACC_UPDATE:
38268 if (context == pragma_stmt)
38270 error_at (pragma_tok->location,
38271 "%<#pragma %s%> may only be used in compound statements",
38272 "acc update");
38273 break;
38275 else if (context != pragma_compound)
38276 goto bad_stmt;
38277 cp_parser_omp_construct (parser, pragma_tok, if_p);
38278 return true;
38280 case PRAGMA_OACC_WAIT:
38281 if (context == pragma_stmt)
38283 error_at (pragma_tok->location,
38284 "%<#pragma %s%> may only be used in compound statements",
38285 "acc wait");
38286 break;
38288 else if (context != pragma_compound)
38289 goto bad_stmt;
38290 cp_parser_omp_construct (parser, pragma_tok, if_p);
38291 return true;
38293 case PRAGMA_OACC_ATOMIC:
38294 case PRAGMA_OACC_CACHE:
38295 case PRAGMA_OACC_DATA:
38296 case PRAGMA_OACC_HOST_DATA:
38297 case PRAGMA_OACC_KERNELS:
38298 case PRAGMA_OACC_PARALLEL:
38299 case PRAGMA_OACC_LOOP:
38300 case PRAGMA_OMP_ATOMIC:
38301 case PRAGMA_OMP_CRITICAL:
38302 case PRAGMA_OMP_DISTRIBUTE:
38303 case PRAGMA_OMP_FOR:
38304 case PRAGMA_OMP_MASTER:
38305 case PRAGMA_OMP_PARALLEL:
38306 case PRAGMA_OMP_SECTIONS:
38307 case PRAGMA_OMP_SIMD:
38308 case PRAGMA_OMP_SINGLE:
38309 case PRAGMA_OMP_TASK:
38310 case PRAGMA_OMP_TASKGROUP:
38311 case PRAGMA_OMP_TASKLOOP:
38312 case PRAGMA_OMP_TEAMS:
38313 if (context != pragma_stmt && context != pragma_compound)
38314 goto bad_stmt;
38315 stmt = push_omp_privatization_clauses (false);
38316 cp_parser_omp_construct (parser, pragma_tok, if_p);
38317 pop_omp_privatization_clauses (stmt);
38318 return true;
38320 case PRAGMA_OMP_ORDERED:
38321 if (context != pragma_stmt && context != pragma_compound)
38322 goto bad_stmt;
38323 stmt = push_omp_privatization_clauses (false);
38324 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38325 pop_omp_privatization_clauses (stmt);
38326 return ret;
38328 case PRAGMA_OMP_TARGET:
38329 if (context != pragma_stmt && context != pragma_compound)
38330 goto bad_stmt;
38331 stmt = push_omp_privatization_clauses (false);
38332 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38333 pop_omp_privatization_clauses (stmt);
38334 return ret;
38336 case PRAGMA_OMP_END_DECLARE_TARGET:
38337 cp_parser_omp_end_declare_target (parser, pragma_tok);
38338 return false;
38340 case PRAGMA_OMP_SECTION:
38341 error_at (pragma_tok->location,
38342 "%<#pragma omp section%> may only be used in "
38343 "%<#pragma omp sections%> construct");
38344 break;
38346 case PRAGMA_IVDEP:
38348 if (context == pragma_external)
38350 error_at (pragma_tok->location,
38351 "%<#pragma GCC ivdep%> must be inside a function");
38352 break;
38354 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38355 cp_token *tok;
38356 tok = cp_lexer_peek_token (the_parser->lexer);
38357 if (tok->type != CPP_KEYWORD
38358 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38359 && tok->keyword != RID_DO))
38361 cp_parser_error (parser, "for, while or do statement expected");
38362 return false;
38364 cp_parser_iteration_statement (parser, if_p, true);
38365 return true;
38368 case PRAGMA_CILK_SIMD:
38369 if (context == pragma_external)
38371 error_at (pragma_tok->location,
38372 "%<#pragma simd%> must be inside a function");
38373 break;
38375 stmt = push_omp_privatization_clauses (false);
38376 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38377 pop_omp_privatization_clauses (stmt);
38378 return true;
38380 case PRAGMA_CILK_GRAINSIZE:
38381 if (context == pragma_external)
38383 error_at (pragma_tok->location,
38384 "%<#pragma cilk grainsize%> must be inside a function");
38385 break;
38388 /* Ignore the pragma if Cilk Plus is not enabled. */
38389 if (flag_cilkplus)
38391 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38392 return true;
38394 else
38396 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38397 "%<#pragma cilk grainsize%>");
38398 break;
38401 default:
38402 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38403 c_invoke_pragma_handler (id);
38404 break;
38406 bad_stmt:
38407 cp_parser_error (parser, "expected declaration specifiers");
38408 break;
38411 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38412 return false;
38415 /* The interface the pragma parsers have to the lexer. */
38417 enum cpp_ttype
38418 pragma_lex (tree *value, location_t *loc)
38420 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38421 enum cpp_ttype ret = tok->type;
38423 *value = tok->u.value;
38424 if (loc)
38425 *loc = tok->location;
38427 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38428 ret = CPP_EOF;
38429 else if (ret == CPP_STRING)
38430 *value = cp_parser_string_literal (the_parser, false, false);
38431 else
38433 if (ret == CPP_KEYWORD)
38434 ret = CPP_NAME;
38435 cp_lexer_consume_token (the_parser->lexer);
38438 return ret;
38442 /* External interface. */
38444 /* Parse one entire translation unit. */
38446 void
38447 c_parse_file (void)
38449 static bool already_called = false;
38451 if (already_called)
38452 fatal_error (input_location,
38453 "inter-module optimizations not implemented for C++");
38454 already_called = true;
38456 the_parser = cp_parser_new ();
38457 push_deferring_access_checks (flag_access_control
38458 ? dk_no_deferred : dk_no_check);
38459 cp_parser_translation_unit (the_parser);
38460 the_parser = NULL;
38463 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38464 vectorlength clause:
38465 Syntax:
38466 vectorlength ( constant-expression ) */
38468 static tree
38469 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
38470 bool is_simd_fn)
38472 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38473 tree expr;
38474 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38475 safelen clause. Thus, vectorlength is represented as OMP 4.0
38476 safelen. For SIMD-enabled function it is represented by OMP 4.0
38477 simdlen. */
38478 if (!is_simd_fn)
38479 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
38480 loc);
38481 else
38482 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
38483 loc);
38485 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38486 return error_mark_node;
38488 expr = cp_parser_constant_expression (parser);
38489 expr = maybe_constant_value (expr);
38491 /* If expr == error_mark_node, then don't emit any errors nor
38492 create a clause. if any of the above functions returns
38493 error mark node then they would have emitted an error message. */
38494 if (expr == error_mark_node)
38496 else if (!TREE_TYPE (expr)
38497 || !TREE_CONSTANT (expr)
38498 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
38499 error_at (loc, "vectorlength must be an integer constant");
38500 else if (TREE_CONSTANT (expr)
38501 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
38502 error_at (loc, "vectorlength must be a power of 2");
38503 else
38505 tree c;
38506 if (!is_simd_fn)
38508 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
38509 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
38510 OMP_CLAUSE_CHAIN (c) = clauses;
38511 clauses = c;
38513 else
38515 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
38516 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
38517 OMP_CLAUSE_CHAIN (c) = clauses;
38518 clauses = c;
38522 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38523 return error_mark_node;
38524 return clauses;
38527 /* Handles the Cilk Plus #pragma simd linear clause.
38528 Syntax:
38529 linear ( simd-linear-variable-list )
38531 simd-linear-variable-list:
38532 simd-linear-variable
38533 simd-linear-variable-list , simd-linear-variable
38535 simd-linear-variable:
38536 id-expression
38537 id-expression : simd-linear-step
38539 simd-linear-step:
38540 conditional-expression */
38542 static tree
38543 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
38545 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38547 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38548 return clauses;
38549 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38551 cp_parser_error (parser, "expected identifier");
38552 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38553 return error_mark_node;
38556 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38557 parser->colon_corrects_to_scope_p = false;
38558 while (1)
38560 cp_token *token = cp_lexer_peek_token (parser->lexer);
38561 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38563 cp_parser_error (parser, "expected variable-name");
38564 clauses = error_mark_node;
38565 break;
38568 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
38569 false, false);
38570 tree decl = cp_parser_lookup_name_simple (parser, var_name,
38571 token->location);
38572 if (decl == error_mark_node)
38574 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
38575 token->location);
38576 clauses = error_mark_node;
38578 else
38580 tree e = NULL_TREE;
38581 tree step_size = integer_one_node;
38583 /* If present, parse the linear step. Otherwise, assume the default
38584 value of 1. */
38585 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
38587 cp_lexer_consume_token (parser->lexer);
38589 e = cp_parser_assignment_expression (parser);
38590 e = maybe_constant_value (e);
38592 if (e == error_mark_node)
38594 /* If an error has occurred, then the whole pragma is
38595 considered ill-formed. Thus, no reason to keep
38596 parsing. */
38597 clauses = error_mark_node;
38598 break;
38600 else if (type_dependent_expression_p (e)
38601 || value_dependent_expression_p (e)
38602 || (TREE_TYPE (e)
38603 && INTEGRAL_TYPE_P (TREE_TYPE (e))
38604 && (TREE_CONSTANT (e)
38605 || DECL_P (e))))
38606 step_size = e;
38607 else
38608 cp_parser_error (parser,
38609 "step size must be an integer constant "
38610 "expression or an integer variable");
38613 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38614 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38615 OMP_CLAUSE_DECL (l) = decl;
38616 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
38617 OMP_CLAUSE_CHAIN (l) = clauses;
38618 clauses = l;
38620 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38621 cp_lexer_consume_token (parser->lexer);
38622 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38623 break;
38624 else
38626 error_at (cp_lexer_peek_token (parser->lexer)->location,
38627 "expected %<,%> or %<)%> after %qE", decl);
38628 clauses = error_mark_node;
38629 break;
38632 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38633 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38634 return clauses;
38637 /* Returns the name of the next clause. If the clause is not
38638 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38639 token is not consumed. Otherwise, the appropriate enum from the
38640 pragma_simd_clause is returned and the token is consumed. */
38642 static pragma_omp_clause
38643 cp_parser_cilk_simd_clause_name (cp_parser *parser)
38645 pragma_omp_clause clause_type;
38646 cp_token *token = cp_lexer_peek_token (parser->lexer);
38648 if (token->keyword == RID_PRIVATE)
38649 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
38650 else if (!token->u.value || token->type != CPP_NAME)
38651 return PRAGMA_CILK_CLAUSE_NONE;
38652 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
38653 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
38654 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
38655 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
38656 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
38657 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
38658 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
38659 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
38660 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
38661 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
38662 else
38663 return PRAGMA_CILK_CLAUSE_NONE;
38665 cp_lexer_consume_token (parser->lexer);
38666 return clause_type;
38669 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38671 static tree
38672 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
38674 tree clauses = NULL_TREE;
38676 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38677 && clauses != error_mark_node)
38679 pragma_omp_clause c_kind;
38680 c_kind = cp_parser_cilk_simd_clause_name (parser);
38681 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
38682 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
38683 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
38684 clauses = cp_parser_cilk_simd_linear (parser, clauses);
38685 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
38686 /* Use the OpenMP 4.0 equivalent function. */
38687 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
38688 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
38689 /* Use the OpenMP 4.0 equivalent function. */
38690 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38691 clauses);
38692 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
38693 /* Use the OMP 4.0 equivalent function. */
38694 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
38695 clauses);
38696 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
38697 /* Use the OMP 4.0 equivalent function. */
38698 clauses = cp_parser_omp_clause_reduction (parser, clauses);
38699 else
38701 clauses = error_mark_node;
38702 cp_parser_error (parser, "expected %<#pragma simd%> clause");
38703 break;
38707 cp_parser_skip_to_pragma_eol (parser, pragma_token);
38709 if (clauses == error_mark_node)
38710 return error_mark_node;
38711 else
38712 return finish_omp_clauses (clauses, C_ORT_CILK);
38715 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38717 static void
38718 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
38720 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
38722 if (clauses == error_mark_node)
38723 return;
38725 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
38727 error_at (cp_lexer_peek_token (parser->lexer)->location,
38728 "for statement expected");
38729 return;
38732 tree sb = begin_omp_structured_block ();
38733 int save = cp_parser_begin_omp_structured_block (parser);
38734 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
38735 if (ret)
38736 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
38737 cp_parser_end_omp_structured_block (parser, save);
38738 add_stmt (finish_omp_structured_block (sb));
38741 /* Main entry-point for parsing Cilk Plus _Cilk_for
38742 loops. The return value is error_mark_node
38743 when errors happen and CILK_FOR tree on success. */
38745 static tree
38746 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
38748 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
38749 gcc_unreachable ();
38751 tree sb = begin_omp_structured_block ();
38752 int save = cp_parser_begin_omp_structured_block (parser);
38754 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
38755 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
38756 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
38757 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
38759 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
38760 if (ret)
38761 cpp_validate_cilk_plus_loop (ret);
38762 else
38763 ret = error_mark_node;
38765 cp_parser_end_omp_structured_block (parser, save);
38766 add_stmt (finish_omp_structured_block (sb));
38767 return ret;
38770 /* Create an identifier for a generic parameter type (a synthesized
38771 template parameter implied by `auto' or a concept identifier). */
38773 static GTY(()) int generic_parm_count;
38774 static tree
38775 make_generic_type_name ()
38777 char buf[32];
38778 sprintf (buf, "auto:%d", ++generic_parm_count);
38779 return get_identifier (buf);
38782 /* Predicate that behaves as is_auto_or_concept but matches the parent
38783 node of the generic type rather than the generic type itself. This
38784 allows for type transformation in add_implicit_template_parms. */
38786 static inline bool
38787 tree_type_is_auto_or_concept (const_tree t)
38789 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
38792 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38793 (creating a new template parameter list if necessary). Returns the newly
38794 created template type parm. */
38796 static tree
38797 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38799 gcc_assert (current_binding_level->kind == sk_function_parms);
38801 /* Before committing to modifying any scope, if we're in an
38802 implicit template scope, and we're trying to synthesize a
38803 constrained parameter, try to find a previous parameter with
38804 the same name. This is the same-type rule for abbreviated
38805 function templates.
38807 NOTE: We can generate implicit parameters when tentatively
38808 parsing a nested name specifier, only to reject that parse
38809 later. However, matching the same template-id as part of a
38810 direct-declarator should generate an identical template
38811 parameter, so this rule will merge them. */
38812 if (parser->implicit_template_scope && constr)
38814 tree t = parser->implicit_template_parms;
38815 while (t)
38817 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38819 tree d = TREE_VALUE (t);
38820 if (TREE_CODE (d) == PARM_DECL)
38821 /* Return the TEMPLATE_PARM_INDEX. */
38822 d = DECL_INITIAL (d);
38823 return d;
38825 t = TREE_CHAIN (t);
38829 /* We are either continuing a function template that already contains implicit
38830 template parameters, creating a new fully-implicit function template, or
38831 extending an existing explicit function template with implicit template
38832 parameters. */
38834 cp_binding_level *const entry_scope = current_binding_level;
38836 bool become_template = false;
38837 cp_binding_level *parent_scope = 0;
38839 if (parser->implicit_template_scope)
38841 gcc_assert (parser->implicit_template_parms);
38843 current_binding_level = parser->implicit_template_scope;
38845 else
38847 /* Roll back to the existing template parameter scope (in the case of
38848 extending an explicit function template) or introduce a new template
38849 parameter scope ahead of the function parameter scope (or class scope
38850 in the case of out-of-line member definitions). The function scope is
38851 added back after template parameter synthesis below. */
38853 cp_binding_level *scope = entry_scope;
38855 while (scope->kind == sk_function_parms)
38857 parent_scope = scope;
38858 scope = scope->level_chain;
38860 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38862 /* If not defining a class, then any class scope is a scope level in
38863 an out-of-line member definition. In this case simply wind back
38864 beyond the first such scope to inject the template parameter list.
38865 Otherwise wind back to the class being defined. The latter can
38866 occur in class member friend declarations such as:
38868 class A {
38869 void foo (auto);
38871 class B {
38872 friend void A::foo (auto);
38875 The template parameter list synthesized for the friend declaration
38876 must be injected in the scope of 'B'. This can also occur in
38877 erroneous cases such as:
38879 struct A {
38880 struct B {
38881 void foo (auto);
38883 void B::foo (auto) {}
38886 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38887 but, nevertheless, the template parameter list synthesized for the
38888 declarator should be injected into the scope of 'A' as if the
38889 ill-formed template was specified explicitly. */
38891 while (scope->kind == sk_class && !scope->defining_class_p)
38893 parent_scope = scope;
38894 scope = scope->level_chain;
38898 current_binding_level = scope;
38900 if (scope->kind != sk_template_parms
38901 || !function_being_declared_is_template_p (parser))
38903 /* Introduce a new template parameter list for implicit template
38904 parameters. */
38906 become_template = true;
38908 parser->implicit_template_scope
38909 = begin_scope (sk_template_parms, NULL);
38911 ++processing_template_decl;
38913 parser->fully_implicit_function_template_p = true;
38914 ++parser->num_template_parameter_lists;
38916 else
38918 /* Synthesize implicit template parameters at the end of the explicit
38919 template parameter list. */
38921 gcc_assert (current_template_parms);
38923 parser->implicit_template_scope = scope;
38925 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38926 parser->implicit_template_parms
38927 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38931 /* Synthesize a new template parameter and track the current template
38932 parameter chain with implicit_template_parms. */
38934 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38935 tree synth_id = make_generic_type_name ();
38936 tree synth_tmpl_parm;
38937 bool non_type = false;
38939 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38940 synth_tmpl_parm
38941 = finish_template_type_parm (class_type_node, synth_id);
38942 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38943 synth_tmpl_parm
38944 = finish_constrained_template_template_parm (proto, synth_id);
38945 else
38947 synth_tmpl_parm = copy_decl (proto);
38948 DECL_NAME (synth_tmpl_parm) = synth_id;
38949 non_type = true;
38952 // Attach the constraint to the parm before processing.
38953 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38954 TREE_TYPE (node) = constr;
38955 tree new_parm
38956 = process_template_parm (parser->implicit_template_parms,
38957 input_location,
38958 node,
38959 /*non_type=*/non_type,
38960 /*param_pack=*/false);
38962 // Chain the new parameter to the list of implicit parameters.
38963 if (parser->implicit_template_parms)
38964 parser->implicit_template_parms
38965 = TREE_CHAIN (parser->implicit_template_parms);
38966 else
38967 parser->implicit_template_parms = new_parm;
38969 tree new_decl = getdecls ();
38970 if (non_type)
38971 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38972 new_decl = DECL_INITIAL (new_decl);
38974 /* If creating a fully implicit function template, start the new implicit
38975 template parameter list with this synthesized type, otherwise grow the
38976 current template parameter list. */
38978 if (become_template)
38980 parent_scope->level_chain = current_binding_level;
38982 tree new_parms = make_tree_vec (1);
38983 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38984 current_template_parms = tree_cons (size_int (processing_template_decl),
38985 new_parms, current_template_parms);
38987 else
38989 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38990 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38991 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38992 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38995 // If the new parameter was constrained, we need to add that to the
38996 // constraints in the template parameter list.
38997 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38999 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39000 reqs = conjoin_constraints (reqs, req);
39001 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39004 current_binding_level = entry_scope;
39006 return new_decl;
39009 /* Finish the declaration of a fully implicit function template. Such a
39010 template has no explicit template parameter list so has not been through the
39011 normal template head and tail processing. synthesize_implicit_template_parm
39012 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39013 provided if the declaration is a class member such that its template
39014 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39015 form is returned. Otherwise NULL_TREE is returned. */
39017 static tree
39018 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39020 gcc_assert (parser->fully_implicit_function_template_p);
39022 if (member_decl_opt && member_decl_opt != error_mark_node
39023 && DECL_VIRTUAL_P (member_decl_opt))
39025 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39026 "implicit templates may not be %<virtual%>");
39027 DECL_VIRTUAL_P (member_decl_opt) = false;
39030 if (member_decl_opt)
39031 member_decl_opt = finish_member_template_decl (member_decl_opt);
39032 end_template_decl ();
39034 parser->fully_implicit_function_template_p = false;
39035 --parser->num_template_parameter_lists;
39037 return member_decl_opt;
39040 #include "gt-cp-parser.h"