PR c++/80384 - ICE with dependent noexcept-specifier
[official-gcc.git] / gcc / cp / parser.c
blob891341d1e8132784cdd1bdb8ce24a77c1bfe7ddd
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);
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;
4065 decl = lookup_name (name);
4066 if (!decl || !is_overloaded_fn (decl))
4067 return error_mark_node;
4069 for (lkp_iterator iter (decl); iter; ++iter)
4071 unsigned int ix;
4072 bool found = true;
4073 tree fn = *iter;
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);
4154 TREE_VEC_ELT (argvec, 0) = argpack;
4156 return argvec;
4159 /* A subroutine of cp_parser_userdef_numeric_literal to
4160 create a char... template parameter pack from a string node. */
4162 static tree
4163 make_string_pack (tree value)
4165 tree charvec;
4166 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4167 const unsigned char *str
4168 = (const unsigned char *) TREE_STRING_POINTER (value);
4169 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4170 int len = TREE_STRING_LENGTH (value) / sz - 1;
4171 tree argvec = make_tree_vec (2);
4173 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4174 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4176 /* First template parm is character type. */
4177 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4179 /* Fill in CHARVEC with all of the parameters. */
4180 charvec = make_tree_vec (len);
4181 for (int i = 0; i < len; ++i)
4182 TREE_VEC_ELT (charvec, i)
4183 = double_int_to_tree (str_char_type_node,
4184 double_int::from_buffer (str + i * sz, sz));
4186 /* Build the argument packs. */
4187 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4189 TREE_VEC_ELT (argvec, 1) = argpack;
4191 return argvec;
4194 /* Parse a user-defined numeric constant. returns a call to a user-defined
4195 literal operator. */
4197 static cp_expr
4198 cp_parser_userdef_numeric_literal (cp_parser *parser)
4200 cp_token *token = cp_lexer_consume_token (parser->lexer);
4201 tree literal = token->u.value;
4202 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4203 tree value = USERDEF_LITERAL_VALUE (literal);
4204 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4205 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4206 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4207 tree decl, result;
4208 vec<tree, va_gc> *args;
4210 /* Look for a literal operator taking the exact type of numeric argument
4211 as the literal value. */
4212 args = make_tree_vector ();
4213 vec_safe_push (args, value);
4214 decl = lookup_literal_operator (name, args);
4215 if (decl && decl != error_mark_node)
4217 result = finish_call_expr (decl, &args, false, true,
4218 tf_warning_or_error);
4220 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4222 warning_at (token->location, OPT_Woverflow,
4223 "integer literal exceeds range of %qT type",
4224 long_long_unsigned_type_node);
4226 else
4228 if (overflow > 0)
4229 warning_at (token->location, OPT_Woverflow,
4230 "floating literal exceeds range of %qT type",
4231 long_double_type_node);
4232 else if (overflow < 0)
4233 warning_at (token->location, OPT_Woverflow,
4234 "floating literal truncated to zero");
4237 release_tree_vector (args);
4238 return result;
4240 release_tree_vector (args);
4242 /* If the numeric argument didn't work, look for a raw literal
4243 operator taking a const char* argument consisting of the number
4244 in string format. */
4245 args = make_tree_vector ();
4246 vec_safe_push (args, num_string);
4247 decl = lookup_literal_operator (name, args);
4248 if (decl && decl != error_mark_node)
4250 result = finish_call_expr (decl, &args, false, true,
4251 tf_warning_or_error);
4252 release_tree_vector (args);
4253 return result;
4255 release_tree_vector (args);
4257 /* If the raw literal didn't work, look for a non-type template
4258 function with parameter pack char.... Call the function with
4259 template parameter characters representing the number. */
4260 args = make_tree_vector ();
4261 decl = lookup_literal_operator (name, args);
4262 if (decl && decl != error_mark_node)
4264 tree tmpl_args = make_char_string_pack (num_string);
4265 decl = lookup_template_function (decl, tmpl_args);
4266 result = finish_call_expr (decl, &args, false, true,
4267 tf_warning_or_error);
4268 release_tree_vector (args);
4269 return result;
4272 release_tree_vector (args);
4274 error ("unable to find numeric literal operator %qD", name);
4275 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4276 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4277 "to enable more built-in suffixes");
4278 return error_mark_node;
4281 /* Parse a user-defined string constant. Returns a call to a user-defined
4282 literal operator taking a character pointer and the length of the string
4283 as arguments. */
4285 static tree
4286 cp_parser_userdef_string_literal (tree literal)
4288 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4289 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4290 tree value = USERDEF_LITERAL_VALUE (literal);
4291 int len = TREE_STRING_LENGTH (value)
4292 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4293 tree decl, result;
4294 vec<tree, va_gc> *args;
4296 /* Build up a call to the user-defined operator. */
4297 /* Lookup the name we got back from the id-expression. */
4298 args = make_tree_vector ();
4299 vec_safe_push (args, value);
4300 vec_safe_push (args, build_int_cst (size_type_node, len));
4301 decl = lookup_literal_operator (name, args);
4303 if (decl && decl != error_mark_node)
4305 result = finish_call_expr (decl, &args, false, true,
4306 tf_warning_or_error);
4307 release_tree_vector (args);
4308 return result;
4310 release_tree_vector (args);
4312 /* Look for a template function with typename parameter CharT
4313 and parameter pack CharT... Call the function with
4314 template parameter characters representing the string. */
4315 args = make_tree_vector ();
4316 decl = lookup_literal_operator (name, args);
4317 if (decl && decl != error_mark_node)
4319 tree tmpl_args = make_string_pack (value);
4320 decl = lookup_template_function (decl, tmpl_args);
4321 result = finish_call_expr (decl, &args, false, true,
4322 tf_warning_or_error);
4323 release_tree_vector (args);
4324 return result;
4326 release_tree_vector (args);
4328 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4329 name, TREE_TYPE (value), size_type_node);
4330 return error_mark_node;
4334 /* Basic concepts [gram.basic] */
4336 /* Parse a translation-unit.
4338 translation-unit:
4339 declaration-seq [opt]
4341 Returns TRUE if all went well. */
4343 static bool
4344 cp_parser_translation_unit (cp_parser* parser)
4346 /* The address of the first non-permanent object on the declarator
4347 obstack. */
4348 static void *declarator_obstack_base;
4350 bool success;
4352 /* Create the declarator obstack, if necessary. */
4353 if (!cp_error_declarator)
4355 gcc_obstack_init (&declarator_obstack);
4356 /* Create the error declarator. */
4357 cp_error_declarator = make_declarator (cdk_error);
4358 /* Create the empty parameter list. */
4359 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4360 /* Remember where the base of the declarator obstack lies. */
4361 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4364 cp_parser_declaration_seq_opt (parser);
4366 /* If there are no tokens left then all went well. */
4367 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4369 /* Get rid of the token array; we don't need it any more. */
4370 cp_lexer_destroy (parser->lexer);
4371 parser->lexer = NULL;
4373 /* This file might have been a context that's implicitly extern
4374 "C". If so, pop the lang context. (Only relevant for PCH.) */
4375 if (parser->implicit_extern_c)
4377 pop_lang_context ();
4378 parser->implicit_extern_c = false;
4381 /* Finish up. */
4382 finish_translation_unit ();
4384 success = true;
4386 else
4388 cp_parser_error (parser, "expected declaration");
4389 success = false;
4392 /* Make sure the declarator obstack was fully cleaned up. */
4393 gcc_assert (obstack_next_free (&declarator_obstack)
4394 == declarator_obstack_base);
4396 /* All went well. */
4397 return success;
4400 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4401 decltype context. */
4403 static inline tsubst_flags_t
4404 complain_flags (bool decltype_p)
4406 tsubst_flags_t complain = tf_warning_or_error;
4407 if (decltype_p)
4408 complain |= tf_decltype;
4409 return complain;
4412 /* We're about to parse a collection of statements. If we're currently
4413 parsing tentatively, set up a firewall so that any nested
4414 cp_parser_commit_to_tentative_parse won't affect the current context. */
4416 static cp_token_position
4417 cp_parser_start_tentative_firewall (cp_parser *parser)
4419 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4420 return 0;
4422 cp_parser_parse_tentatively (parser);
4423 cp_parser_commit_to_topmost_tentative_parse (parser);
4424 return cp_lexer_token_position (parser->lexer, false);
4427 /* We've finished parsing the collection of statements. Wrap up the
4428 firewall and replace the relevant tokens with the parsed form. */
4430 static void
4431 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4432 tree expr)
4434 if (!start)
4435 return;
4437 /* Finish the firewall level. */
4438 cp_parser_parse_definitely (parser);
4439 /* And remember the result of the parse for when we try again. */
4440 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4441 token->type = CPP_PREPARSED_EXPR;
4442 token->u.value = expr;
4443 token->keyword = RID_MAX;
4444 cp_lexer_purge_tokens_after (parser->lexer, start);
4447 /* Like the above functions, but let the user modify the tokens. Used by
4448 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4449 later parses, so it makes sense to localize the effects of
4450 cp_parser_commit_to_tentative_parse. */
4452 struct tentative_firewall
4454 cp_parser *parser;
4455 bool set;
4457 tentative_firewall (cp_parser *p): parser(p)
4459 /* If we're currently parsing tentatively, start a committed level as a
4460 firewall and then an inner tentative parse. */
4461 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4463 cp_parser_parse_tentatively (parser);
4464 cp_parser_commit_to_topmost_tentative_parse (parser);
4465 cp_parser_parse_tentatively (parser);
4469 ~tentative_firewall()
4471 if (set)
4473 /* Finish the inner tentative parse and the firewall, propagating any
4474 uncommitted error state to the outer tentative parse. */
4475 bool err = cp_parser_error_occurred (parser);
4476 cp_parser_parse_definitely (parser);
4477 cp_parser_parse_definitely (parser);
4478 if (err)
4479 cp_parser_simulate_error (parser);
4484 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4485 enclosing parentheses. */
4487 static cp_expr
4488 cp_parser_statement_expr (cp_parser *parser)
4490 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4492 /* Consume the '('. */
4493 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4494 cp_lexer_consume_token (parser->lexer);
4495 /* Start the statement-expression. */
4496 tree expr = begin_stmt_expr ();
4497 /* Parse the compound-statement. */
4498 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4499 /* Finish up. */
4500 expr = finish_stmt_expr (expr, false);
4501 /* Consume the ')'. */
4502 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4503 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4504 cp_parser_skip_to_end_of_statement (parser);
4506 cp_parser_end_tentative_firewall (parser, start, expr);
4507 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4508 return cp_expr (expr, combined_loc);
4511 /* Expressions [gram.expr] */
4513 /* Parse a fold-operator.
4515 fold-operator:
4516 - * / % ^ & | = < > << >>
4517 = -= *= /= %= ^= &= |= <<= >>=
4518 == != <= >= && || , .* ->*
4520 This returns the tree code corresponding to the matched operator
4521 as an int. When the current token matches a compound assignment
4522 opertor, the resulting tree code is the negative value of the
4523 non-assignment operator. */
4525 static int
4526 cp_parser_fold_operator (cp_token *token)
4528 switch (token->type)
4530 case CPP_PLUS: return PLUS_EXPR;
4531 case CPP_MINUS: return MINUS_EXPR;
4532 case CPP_MULT: return MULT_EXPR;
4533 case CPP_DIV: return TRUNC_DIV_EXPR;
4534 case CPP_MOD: return TRUNC_MOD_EXPR;
4535 case CPP_XOR: return BIT_XOR_EXPR;
4536 case CPP_AND: return BIT_AND_EXPR;
4537 case CPP_OR: return BIT_IOR_EXPR;
4538 case CPP_LSHIFT: return LSHIFT_EXPR;
4539 case CPP_RSHIFT: return RSHIFT_EXPR;
4541 case CPP_EQ: return -NOP_EXPR;
4542 case CPP_PLUS_EQ: return -PLUS_EXPR;
4543 case CPP_MINUS_EQ: return -MINUS_EXPR;
4544 case CPP_MULT_EQ: return -MULT_EXPR;
4545 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4546 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4547 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4548 case CPP_AND_EQ: return -BIT_AND_EXPR;
4549 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4550 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4551 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4553 case CPP_EQ_EQ: return EQ_EXPR;
4554 case CPP_NOT_EQ: return NE_EXPR;
4555 case CPP_LESS: return LT_EXPR;
4556 case CPP_GREATER: return GT_EXPR;
4557 case CPP_LESS_EQ: return LE_EXPR;
4558 case CPP_GREATER_EQ: return GE_EXPR;
4560 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4561 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4563 case CPP_COMMA: return COMPOUND_EXPR;
4565 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4566 case CPP_DEREF_STAR: return MEMBER_REF;
4568 default: return ERROR_MARK;
4572 /* Returns true if CODE indicates a binary expression, which is not allowed in
4573 the LHS of a fold-expression. More codes will need to be added to use this
4574 function in other contexts. */
4576 static bool
4577 is_binary_op (tree_code code)
4579 switch (code)
4581 case PLUS_EXPR:
4582 case POINTER_PLUS_EXPR:
4583 case MINUS_EXPR:
4584 case MULT_EXPR:
4585 case TRUNC_DIV_EXPR:
4586 case TRUNC_MOD_EXPR:
4587 case BIT_XOR_EXPR:
4588 case BIT_AND_EXPR:
4589 case BIT_IOR_EXPR:
4590 case LSHIFT_EXPR:
4591 case RSHIFT_EXPR:
4593 case MODOP_EXPR:
4595 case EQ_EXPR:
4596 case NE_EXPR:
4597 case LE_EXPR:
4598 case GE_EXPR:
4599 case LT_EXPR:
4600 case GT_EXPR:
4602 case TRUTH_ANDIF_EXPR:
4603 case TRUTH_ORIF_EXPR:
4605 case COMPOUND_EXPR:
4607 case DOTSTAR_EXPR:
4608 case MEMBER_REF:
4609 return true;
4611 default:
4612 return false;
4616 /* If the next token is a suitable fold operator, consume it and return as
4617 the function above. */
4619 static int
4620 cp_parser_fold_operator (cp_parser *parser)
4622 cp_token* token = cp_lexer_peek_token (parser->lexer);
4623 int code = cp_parser_fold_operator (token);
4624 if (code != ERROR_MARK)
4625 cp_lexer_consume_token (parser->lexer);
4626 return code;
4629 /* Parse a fold-expression.
4631 fold-expression:
4632 ( ... folding-operator cast-expression)
4633 ( cast-expression folding-operator ... )
4634 ( cast-expression folding operator ... folding-operator cast-expression)
4636 Note that the '(' and ')' are matched in primary expression. */
4638 static cp_expr
4639 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4641 cp_id_kind pidk;
4643 // Left fold.
4644 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4646 cp_lexer_consume_token (parser->lexer);
4647 int op = cp_parser_fold_operator (parser);
4648 if (op == ERROR_MARK)
4650 cp_parser_error (parser, "expected binary operator");
4651 return error_mark_node;
4654 tree expr = cp_parser_cast_expression (parser, false, false,
4655 false, &pidk);
4656 if (expr == error_mark_node)
4657 return error_mark_node;
4658 return finish_left_unary_fold_expr (expr, op);
4661 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4662 int op = cp_parser_fold_operator (parser);
4663 if (op == ERROR_MARK)
4665 cp_parser_error (parser, "expected binary operator");
4666 return error_mark_node;
4669 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4671 cp_parser_error (parser, "expected ...");
4672 return error_mark_node;
4674 cp_lexer_consume_token (parser->lexer);
4676 /* The operands of a fold-expression are cast-expressions, so binary or
4677 conditional expressions are not allowed. We check this here to avoid
4678 tentative parsing. */
4679 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4680 /* OK, the expression was parenthesized. */;
4681 else if (is_binary_op (TREE_CODE (expr1)))
4682 error_at (location_of (expr1),
4683 "binary expression in operand of fold-expression");
4684 else if (TREE_CODE (expr1) == COND_EXPR)
4685 error_at (location_of (expr1),
4686 "conditional expression in operand of fold-expression");
4688 // Right fold.
4689 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4690 return finish_right_unary_fold_expr (expr1, op);
4692 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4694 cp_parser_error (parser, "mismatched operator in fold-expression");
4695 return error_mark_node;
4697 cp_lexer_consume_token (parser->lexer);
4699 // Binary left or right fold.
4700 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4701 if (expr2 == error_mark_node)
4702 return error_mark_node;
4703 return finish_binary_fold_expr (expr1, expr2, op);
4706 /* Parse a primary-expression.
4708 primary-expression:
4709 literal
4710 this
4711 ( expression )
4712 id-expression
4713 lambda-expression (C++11)
4715 GNU Extensions:
4717 primary-expression:
4718 ( compound-statement )
4719 __builtin_va_arg ( assignment-expression , type-id )
4720 __builtin_offsetof ( type-id , offsetof-expression )
4722 C++ Extensions:
4723 __has_nothrow_assign ( type-id )
4724 __has_nothrow_constructor ( type-id )
4725 __has_nothrow_copy ( type-id )
4726 __has_trivial_assign ( type-id )
4727 __has_trivial_constructor ( type-id )
4728 __has_trivial_copy ( type-id )
4729 __has_trivial_destructor ( type-id )
4730 __has_virtual_destructor ( type-id )
4731 __is_abstract ( type-id )
4732 __is_base_of ( type-id , type-id )
4733 __is_class ( type-id )
4734 __is_empty ( type-id )
4735 __is_enum ( type-id )
4736 __is_final ( type-id )
4737 __is_literal_type ( type-id )
4738 __is_pod ( type-id )
4739 __is_polymorphic ( type-id )
4740 __is_std_layout ( type-id )
4741 __is_trivial ( type-id )
4742 __is_union ( type-id )
4744 Objective-C++ Extension:
4746 primary-expression:
4747 objc-expression
4749 literal:
4750 __null
4752 ADDRESS_P is true iff this expression was immediately preceded by
4753 "&" and therefore might denote a pointer-to-member. CAST_P is true
4754 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4755 true iff this expression is a template argument.
4757 Returns a representation of the expression. Upon return, *IDK
4758 indicates what kind of id-expression (if any) was present. */
4760 static cp_expr
4761 cp_parser_primary_expression (cp_parser *parser,
4762 bool address_p,
4763 bool cast_p,
4764 bool template_arg_p,
4765 bool decltype_p,
4766 cp_id_kind *idk)
4768 cp_token *token = NULL;
4770 /* Assume the primary expression is not an id-expression. */
4771 *idk = CP_ID_KIND_NONE;
4773 /* Peek at the next token. */
4774 token = cp_lexer_peek_token (parser->lexer);
4775 switch ((int) token->type)
4777 /* literal:
4778 integer-literal
4779 character-literal
4780 floating-literal
4781 string-literal
4782 boolean-literal
4783 pointer-literal
4784 user-defined-literal */
4785 case CPP_CHAR:
4786 case CPP_CHAR16:
4787 case CPP_CHAR32:
4788 case CPP_WCHAR:
4789 case CPP_UTF8CHAR:
4790 case CPP_NUMBER:
4791 case CPP_PREPARSED_EXPR:
4792 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4793 return cp_parser_userdef_numeric_literal (parser);
4794 token = cp_lexer_consume_token (parser->lexer);
4795 if (TREE_CODE (token->u.value) == FIXED_CST)
4797 error_at (token->location,
4798 "fixed-point types not supported in C++");
4799 return error_mark_node;
4801 /* Floating-point literals are only allowed in an integral
4802 constant expression if they are cast to an integral or
4803 enumeration type. */
4804 if (TREE_CODE (token->u.value) == REAL_CST
4805 && parser->integral_constant_expression_p
4806 && pedantic)
4808 /* CAST_P will be set even in invalid code like "int(2.7 +
4809 ...)". Therefore, we have to check that the next token
4810 is sure to end the cast. */
4811 if (cast_p)
4813 cp_token *next_token;
4815 next_token = cp_lexer_peek_token (parser->lexer);
4816 if (/* The comma at the end of an
4817 enumerator-definition. */
4818 next_token->type != CPP_COMMA
4819 /* The curly brace at the end of an enum-specifier. */
4820 && next_token->type != CPP_CLOSE_BRACE
4821 /* The end of a statement. */
4822 && next_token->type != CPP_SEMICOLON
4823 /* The end of the cast-expression. */
4824 && next_token->type != CPP_CLOSE_PAREN
4825 /* The end of an array bound. */
4826 && next_token->type != CPP_CLOSE_SQUARE
4827 /* The closing ">" in a template-argument-list. */
4828 && (next_token->type != CPP_GREATER
4829 || parser->greater_than_is_operator_p)
4830 /* C++0x only: A ">>" treated like two ">" tokens,
4831 in a template-argument-list. */
4832 && (next_token->type != CPP_RSHIFT
4833 || (cxx_dialect == cxx98)
4834 || parser->greater_than_is_operator_p))
4835 cast_p = false;
4838 /* If we are within a cast, then the constraint that the
4839 cast is to an integral or enumeration type will be
4840 checked at that point. If we are not within a cast, then
4841 this code is invalid. */
4842 if (!cast_p)
4843 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4845 return cp_expr (token->u.value, token->location);
4847 case CPP_CHAR_USERDEF:
4848 case CPP_CHAR16_USERDEF:
4849 case CPP_CHAR32_USERDEF:
4850 case CPP_WCHAR_USERDEF:
4851 case CPP_UTF8CHAR_USERDEF:
4852 return cp_parser_userdef_char_literal (parser);
4854 case CPP_STRING:
4855 case CPP_STRING16:
4856 case CPP_STRING32:
4857 case CPP_WSTRING:
4858 case CPP_UTF8STRING:
4859 case CPP_STRING_USERDEF:
4860 case CPP_STRING16_USERDEF:
4861 case CPP_STRING32_USERDEF:
4862 case CPP_WSTRING_USERDEF:
4863 case CPP_UTF8STRING_USERDEF:
4864 /* ??? Should wide strings be allowed when parser->translate_strings_p
4865 is false (i.e. in attributes)? If not, we can kill the third
4866 argument to cp_parser_string_literal. */
4867 return cp_parser_string_literal (parser,
4868 parser->translate_strings_p,
4869 true);
4871 case CPP_OPEN_PAREN:
4872 /* If we see `( { ' then we are looking at the beginning of
4873 a GNU statement-expression. */
4874 if (cp_parser_allow_gnu_extensions_p (parser)
4875 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4877 /* Statement-expressions are not allowed by the standard. */
4878 pedwarn (token->location, OPT_Wpedantic,
4879 "ISO C++ forbids braced-groups within expressions");
4881 /* And they're not allowed outside of a function-body; you
4882 cannot, for example, write:
4884 int i = ({ int j = 3; j + 1; });
4886 at class or namespace scope. */
4887 if (!parser->in_function_body
4888 || parser->in_template_argument_list_p)
4890 error_at (token->location,
4891 "statement-expressions are not allowed outside "
4892 "functions nor in template-argument lists");
4893 cp_parser_skip_to_end_of_block_or_statement (parser);
4894 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4895 cp_lexer_consume_token (parser->lexer);
4896 return error_mark_node;
4898 else
4899 return cp_parser_statement_expr (parser);
4901 /* Otherwise it's a normal parenthesized expression. */
4903 cp_expr expr;
4904 bool saved_greater_than_is_operator_p;
4906 location_t open_paren_loc = token->location;
4908 /* Consume the `('. */
4909 cp_lexer_consume_token (parser->lexer);
4910 /* Within a parenthesized expression, a `>' token is always
4911 the greater-than operator. */
4912 saved_greater_than_is_operator_p
4913 = parser->greater_than_is_operator_p;
4914 parser->greater_than_is_operator_p = true;
4916 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4917 /* Left fold expression. */
4918 expr = NULL_TREE;
4919 else
4920 /* Parse the parenthesized expression. */
4921 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4923 token = cp_lexer_peek_token (parser->lexer);
4924 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4926 expr = cp_parser_fold_expression (parser, expr);
4927 if (expr != error_mark_node
4928 && cxx_dialect < cxx1z
4929 && !in_system_header_at (input_location))
4930 pedwarn (input_location, 0, "fold-expressions only available "
4931 "with -std=c++1z or -std=gnu++1z");
4933 else
4934 /* Let the front end know that this expression was
4935 enclosed in parentheses. This matters in case, for
4936 example, the expression is of the form `A::B', since
4937 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4938 not. */
4939 expr = finish_parenthesized_expr (expr);
4941 /* DR 705: Wrapping an unqualified name in parentheses
4942 suppresses arg-dependent lookup. We want to pass back
4943 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4944 (c++/37862), but none of the others. */
4945 if (*idk != CP_ID_KIND_QUALIFIED)
4946 *idk = CP_ID_KIND_NONE;
4948 /* The `>' token might be the end of a template-id or
4949 template-parameter-list now. */
4950 parser->greater_than_is_operator_p
4951 = saved_greater_than_is_operator_p;
4953 /* Consume the `)'. */
4954 token = cp_lexer_peek_token (parser->lexer);
4955 location_t close_paren_loc = token->location;
4956 expr.set_range (open_paren_loc, close_paren_loc);
4957 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4958 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4959 cp_parser_skip_to_end_of_statement (parser);
4961 return expr;
4964 case CPP_OPEN_SQUARE:
4966 if (c_dialect_objc ())
4968 /* We might have an Objective-C++ message. */
4969 cp_parser_parse_tentatively (parser);
4970 tree msg = cp_parser_objc_message_expression (parser);
4971 /* If that works out, we're done ... */
4972 if (cp_parser_parse_definitely (parser))
4973 return msg;
4974 /* ... else, fall though to see if it's a lambda. */
4976 cp_expr lam = cp_parser_lambda_expression (parser);
4977 /* Don't warn about a failed tentative parse. */
4978 if (cp_parser_error_occurred (parser))
4979 return error_mark_node;
4980 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4981 return lam;
4984 case CPP_OBJC_STRING:
4985 if (c_dialect_objc ())
4986 /* We have an Objective-C++ string literal. */
4987 return cp_parser_objc_expression (parser);
4988 cp_parser_error (parser, "expected primary-expression");
4989 return error_mark_node;
4991 case CPP_KEYWORD:
4992 switch (token->keyword)
4994 /* These two are the boolean literals. */
4995 case RID_TRUE:
4996 cp_lexer_consume_token (parser->lexer);
4997 return cp_expr (boolean_true_node, token->location);
4998 case RID_FALSE:
4999 cp_lexer_consume_token (parser->lexer);
5000 return cp_expr (boolean_false_node, token->location);
5002 /* The `__null' literal. */
5003 case RID_NULL:
5004 cp_lexer_consume_token (parser->lexer);
5005 return cp_expr (null_node, token->location);
5007 /* The `nullptr' literal. */
5008 case RID_NULLPTR:
5009 cp_lexer_consume_token (parser->lexer);
5010 return cp_expr (nullptr_node, token->location);
5012 /* Recognize the `this' keyword. */
5013 case RID_THIS:
5014 cp_lexer_consume_token (parser->lexer);
5015 if (parser->local_variables_forbidden_p)
5017 error_at (token->location,
5018 "%<this%> may not be used in this context");
5019 return error_mark_node;
5021 /* Pointers cannot appear in constant-expressions. */
5022 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5023 return error_mark_node;
5024 return cp_expr (finish_this_expr (), token->location);
5026 /* The `operator' keyword can be the beginning of an
5027 id-expression. */
5028 case RID_OPERATOR:
5029 goto id_expression;
5031 case RID_FUNCTION_NAME:
5032 case RID_PRETTY_FUNCTION_NAME:
5033 case RID_C99_FUNCTION_NAME:
5035 non_integral_constant name;
5037 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5038 __func__ are the names of variables -- but they are
5039 treated specially. Therefore, they are handled here,
5040 rather than relying on the generic id-expression logic
5041 below. Grammatically, these names are id-expressions.
5043 Consume the token. */
5044 token = cp_lexer_consume_token (parser->lexer);
5046 switch (token->keyword)
5048 case RID_FUNCTION_NAME:
5049 name = NIC_FUNC_NAME;
5050 break;
5051 case RID_PRETTY_FUNCTION_NAME:
5052 name = NIC_PRETTY_FUNC;
5053 break;
5054 case RID_C99_FUNCTION_NAME:
5055 name = NIC_C99_FUNC;
5056 break;
5057 default:
5058 gcc_unreachable ();
5061 if (cp_parser_non_integral_constant_expression (parser, name))
5062 return error_mark_node;
5064 /* Look up the name. */
5065 return finish_fname (token->u.value);
5068 case RID_VA_ARG:
5070 tree expression;
5071 tree type;
5072 source_location type_location;
5073 location_t start_loc
5074 = cp_lexer_peek_token (parser->lexer)->location;
5075 /* The `__builtin_va_arg' construct is used to handle
5076 `va_arg'. Consume the `__builtin_va_arg' token. */
5077 cp_lexer_consume_token (parser->lexer);
5078 /* Look for the opening `('. */
5079 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5080 /* Now, parse the assignment-expression. */
5081 expression = cp_parser_assignment_expression (parser);
5082 /* Look for the `,'. */
5083 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5084 type_location = cp_lexer_peek_token (parser->lexer)->location;
5085 /* Parse the type-id. */
5087 type_id_in_expr_sentinel s (parser);
5088 type = cp_parser_type_id (parser);
5090 /* Look for the closing `)'. */
5091 location_t finish_loc
5092 = cp_lexer_peek_token (parser->lexer)->location;
5093 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5094 /* Using `va_arg' in a constant-expression is not
5095 allowed. */
5096 if (cp_parser_non_integral_constant_expression (parser,
5097 NIC_VA_ARG))
5098 return error_mark_node;
5099 /* Construct a location of the form:
5100 __builtin_va_arg (v, int)
5101 ~~~~~~~~~~~~~~~~~~~~~^~~~
5102 with the caret at the type, ranging from the start of the
5103 "__builtin_va_arg" token to the close paren. */
5104 location_t combined_loc
5105 = make_location (type_location, start_loc, finish_loc);
5106 return build_x_va_arg (combined_loc, expression, type);
5109 case RID_OFFSETOF:
5110 return cp_parser_builtin_offsetof (parser);
5112 case RID_HAS_NOTHROW_ASSIGN:
5113 case RID_HAS_NOTHROW_CONSTRUCTOR:
5114 case RID_HAS_NOTHROW_COPY:
5115 case RID_HAS_TRIVIAL_ASSIGN:
5116 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5117 case RID_HAS_TRIVIAL_COPY:
5118 case RID_HAS_TRIVIAL_DESTRUCTOR:
5119 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5120 case RID_HAS_VIRTUAL_DESTRUCTOR:
5121 case RID_IS_ABSTRACT:
5122 case RID_IS_AGGREGATE:
5123 case RID_IS_BASE_OF:
5124 case RID_IS_CLASS:
5125 case RID_IS_EMPTY:
5126 case RID_IS_ENUM:
5127 case RID_IS_FINAL:
5128 case RID_IS_LITERAL_TYPE:
5129 case RID_IS_POD:
5130 case RID_IS_POLYMORPHIC:
5131 case RID_IS_SAME_AS:
5132 case RID_IS_STD_LAYOUT:
5133 case RID_IS_TRIVIAL:
5134 case RID_IS_TRIVIALLY_ASSIGNABLE:
5135 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5136 case RID_IS_TRIVIALLY_COPYABLE:
5137 case RID_IS_UNION:
5138 case RID_IS_ASSIGNABLE:
5139 case RID_IS_CONSTRUCTIBLE:
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 gcc_rich_location richloc (token->location);
5935 richloc.add_fixit_replace ("::");
5936 error_at_rich_loc (&richloc,
5937 "found %<:%> in nested-name-specifier, "
5938 "expected %<::%>");
5939 token->type = CPP_SCOPE;
5942 if (token->type != CPP_SCOPE
5943 && !cp_parser_nth_token_starts_template_argument_list_p
5944 (parser, 2))
5945 break;
5948 /* The nested-name-specifier is optional, so we parse
5949 tentatively. */
5950 cp_parser_parse_tentatively (parser);
5952 /* Look for the optional `template' keyword, if this isn't the
5953 first time through the loop. */
5954 if (success)
5955 template_keyword_p = cp_parser_optional_template_keyword (parser);
5956 else
5957 template_keyword_p = false;
5959 /* Save the old scope since the name lookup we are about to do
5960 might destroy it. */
5961 old_scope = parser->scope;
5962 saved_qualifying_scope = parser->qualifying_scope;
5963 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5964 look up names in "X<T>::I" in order to determine that "Y" is
5965 a template. So, if we have a typename at this point, we make
5966 an effort to look through it. */
5967 if (is_declaration
5968 && !typename_keyword_p
5969 && parser->scope
5970 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5971 parser->scope = resolve_typename_type (parser->scope,
5972 /*only_current_p=*/false);
5973 /* Parse the qualifying entity. */
5974 new_scope
5975 = cp_parser_qualifying_entity (parser,
5976 typename_keyword_p,
5977 template_keyword_p,
5978 check_dependency_p,
5979 type_p,
5980 is_declaration);
5981 /* Look for the `::' token. */
5982 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5984 /* If we found what we wanted, we keep going; otherwise, we're
5985 done. */
5986 if (!cp_parser_parse_definitely (parser))
5988 bool error_p = false;
5990 /* Restore the OLD_SCOPE since it was valid before the
5991 failed attempt at finding the last
5992 class-or-namespace-name. */
5993 parser->scope = old_scope;
5994 parser->qualifying_scope = saved_qualifying_scope;
5996 /* If the next token is a decltype, and the one after that is a
5997 `::', then the decltype has failed to resolve to a class or
5998 enumeration type. Give this error even when parsing
5999 tentatively since it can't possibly be valid--and we're going
6000 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6001 won't get another chance.*/
6002 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6003 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6004 == CPP_SCOPE))
6006 token = cp_lexer_consume_token (parser->lexer);
6007 error_at (token->location, "decltype evaluates to %qT, "
6008 "which is not a class or enumeration type",
6009 token->u.tree_check_value->value);
6010 parser->scope = error_mark_node;
6011 error_p = true;
6012 /* As below. */
6013 success = true;
6014 cp_lexer_consume_token (parser->lexer);
6017 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6018 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6020 /* If we have a non-type template-id followed by ::, it can't
6021 possibly be valid. */
6022 token = cp_lexer_peek_token (parser->lexer);
6023 tree tid = token->u.tree_check_value->value;
6024 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6025 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6027 tree tmpl = NULL_TREE;
6028 if (is_overloaded_fn (tid))
6030 tree fns = get_fns (tid);
6031 if (OVL_SINGLE_P (fns))
6032 tmpl = OVL_FIRST (fns);
6033 error_at (token->location, "function template-id %qD "
6034 "in nested-name-specifier", tid);
6036 else
6038 /* Variable template. */
6039 tmpl = TREE_OPERAND (tid, 0);
6040 gcc_assert (variable_template_p (tmpl));
6041 error_at (token->location, "variable template-id %qD "
6042 "in nested-name-specifier", tid);
6044 if (tmpl)
6045 inform (DECL_SOURCE_LOCATION (tmpl),
6046 "%qD declared here", tmpl);
6048 parser->scope = error_mark_node;
6049 error_p = true;
6050 /* As below. */
6051 success = true;
6052 cp_lexer_consume_token (parser->lexer);
6053 cp_lexer_consume_token (parser->lexer);
6057 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6058 break;
6059 /* If the next token is an identifier, and the one after
6060 that is a `::', then any valid interpretation would have
6061 found a class-or-namespace-name. */
6062 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6063 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6064 == CPP_SCOPE)
6065 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6066 != CPP_COMPL))
6068 token = cp_lexer_consume_token (parser->lexer);
6069 if (!error_p)
6071 if (!token->error_reported)
6073 tree decl;
6074 tree ambiguous_decls;
6076 decl = cp_parser_lookup_name (parser, token->u.value,
6077 none_type,
6078 /*is_template=*/false,
6079 /*is_namespace=*/false,
6080 /*check_dependency=*/true,
6081 &ambiguous_decls,
6082 token->location);
6083 if (TREE_CODE (decl) == TEMPLATE_DECL)
6084 error_at (token->location,
6085 "%qD used without template parameters",
6086 decl);
6087 else if (ambiguous_decls)
6089 // cp_parser_lookup_name has the same diagnostic,
6090 // thus make sure to emit it at most once.
6091 if (cp_parser_uncommitted_to_tentative_parse_p
6092 (parser))
6094 error_at (token->location,
6095 "reference to %qD is ambiguous",
6096 token->u.value);
6097 print_candidates (ambiguous_decls);
6099 decl = error_mark_node;
6101 else
6103 if (cxx_dialect != cxx98)
6104 cp_parser_name_lookup_error
6105 (parser, token->u.value, decl, NLE_NOT_CXX98,
6106 token->location);
6107 else
6108 cp_parser_name_lookup_error
6109 (parser, token->u.value, decl, NLE_CXX98,
6110 token->location);
6113 parser->scope = error_mark_node;
6114 error_p = true;
6115 /* Treat this as a successful nested-name-specifier
6116 due to:
6118 [basic.lookup.qual]
6120 If the name found is not a class-name (clause
6121 _class_) or namespace-name (_namespace.def_), the
6122 program is ill-formed. */
6123 success = true;
6125 cp_lexer_consume_token (parser->lexer);
6127 break;
6129 /* We've found one valid nested-name-specifier. */
6130 success = true;
6131 /* Name lookup always gives us a DECL. */
6132 if (TREE_CODE (new_scope) == TYPE_DECL)
6133 new_scope = TREE_TYPE (new_scope);
6134 /* Uses of "template" must be followed by actual templates. */
6135 if (template_keyword_p
6136 && !(CLASS_TYPE_P (new_scope)
6137 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6138 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6139 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6140 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6141 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6142 == TEMPLATE_ID_EXPR)))
6143 permerror (input_location, TYPE_P (new_scope)
6144 ? G_("%qT is not a template")
6145 : G_("%qD is not a template"),
6146 new_scope);
6147 /* If it is a class scope, try to complete it; we are about to
6148 be looking up names inside the class. */
6149 if (TYPE_P (new_scope)
6150 /* Since checking types for dependency can be expensive,
6151 avoid doing it if the type is already complete. */
6152 && !COMPLETE_TYPE_P (new_scope)
6153 /* Do not try to complete dependent types. */
6154 && !dependent_type_p (new_scope))
6156 new_scope = complete_type (new_scope);
6157 /* If it is a typedef to current class, use the current
6158 class instead, as the typedef won't have any names inside
6159 it yet. */
6160 if (!COMPLETE_TYPE_P (new_scope)
6161 && currently_open_class (new_scope))
6162 new_scope = TYPE_MAIN_VARIANT (new_scope);
6164 /* Make sure we look in the right scope the next time through
6165 the loop. */
6166 parser->scope = new_scope;
6169 /* If parsing tentatively, replace the sequence of tokens that makes
6170 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6171 token. That way, should we re-parse the token stream, we will
6172 not have to repeat the effort required to do the parse, nor will
6173 we issue duplicate error messages. */
6174 if (success && start)
6176 cp_token *token;
6178 token = cp_lexer_token_at (parser->lexer, start);
6179 /* Reset the contents of the START token. */
6180 token->type = CPP_NESTED_NAME_SPECIFIER;
6181 /* Retrieve any deferred checks. Do not pop this access checks yet
6182 so the memory will not be reclaimed during token replacing below. */
6183 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6184 token->u.tree_check_value->value = parser->scope;
6185 token->u.tree_check_value->checks = get_deferred_access_checks ();
6186 token->u.tree_check_value->qualifying_scope =
6187 parser->qualifying_scope;
6188 token->keyword = RID_MAX;
6190 /* Purge all subsequent tokens. */
6191 cp_lexer_purge_tokens_after (parser->lexer, start);
6194 if (start)
6195 pop_to_parent_deferring_access_checks ();
6197 return success ? parser->scope : NULL_TREE;
6200 /* Parse a nested-name-specifier. See
6201 cp_parser_nested_name_specifier_opt for details. This function
6202 behaves identically, except that it will an issue an error if no
6203 nested-name-specifier is present. */
6205 static tree
6206 cp_parser_nested_name_specifier (cp_parser *parser,
6207 bool typename_keyword_p,
6208 bool check_dependency_p,
6209 bool type_p,
6210 bool is_declaration)
6212 tree scope;
6214 /* Look for the nested-name-specifier. */
6215 scope = cp_parser_nested_name_specifier_opt (parser,
6216 typename_keyword_p,
6217 check_dependency_p,
6218 type_p,
6219 is_declaration);
6220 /* If it was not present, issue an error message. */
6221 if (!scope)
6223 cp_parser_error (parser, "expected nested-name-specifier");
6224 parser->scope = NULL_TREE;
6227 return scope;
6230 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6231 this is either a class-name or a namespace-name (which corresponds
6232 to the class-or-namespace-name production in the grammar). For
6233 C++0x, it can also be a type-name that refers to an enumeration
6234 type or a simple-template-id.
6236 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6237 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6238 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6239 TYPE_P is TRUE iff the next name should be taken as a class-name,
6240 even the same name is declared to be another entity in the same
6241 scope.
6243 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6244 specified by the class-or-namespace-name. If neither is found the
6245 ERROR_MARK_NODE is returned. */
6247 static tree
6248 cp_parser_qualifying_entity (cp_parser *parser,
6249 bool typename_keyword_p,
6250 bool template_keyword_p,
6251 bool check_dependency_p,
6252 bool type_p,
6253 bool is_declaration)
6255 tree saved_scope;
6256 tree saved_qualifying_scope;
6257 tree saved_object_scope;
6258 tree scope;
6259 bool only_class_p;
6260 bool successful_parse_p;
6262 /* DR 743: decltype can appear in a nested-name-specifier. */
6263 if (cp_lexer_next_token_is_decltype (parser->lexer))
6265 scope = cp_parser_decltype (parser);
6266 if (TREE_CODE (scope) != ENUMERAL_TYPE
6267 && !MAYBE_CLASS_TYPE_P (scope))
6269 cp_parser_simulate_error (parser);
6270 return error_mark_node;
6272 if (TYPE_NAME (scope))
6273 scope = TYPE_NAME (scope);
6274 return scope;
6277 /* Before we try to parse the class-name, we must save away the
6278 current PARSER->SCOPE since cp_parser_class_name will destroy
6279 it. */
6280 saved_scope = parser->scope;
6281 saved_qualifying_scope = parser->qualifying_scope;
6282 saved_object_scope = parser->object_scope;
6283 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6284 there is no need to look for a namespace-name. */
6285 only_class_p = template_keyword_p
6286 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6287 if (!only_class_p)
6288 cp_parser_parse_tentatively (parser);
6289 scope = cp_parser_class_name (parser,
6290 typename_keyword_p,
6291 template_keyword_p,
6292 type_p ? class_type : none_type,
6293 check_dependency_p,
6294 /*class_head_p=*/false,
6295 is_declaration,
6296 /*enum_ok=*/cxx_dialect > cxx98);
6297 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6298 /* If that didn't work, try for a namespace-name. */
6299 if (!only_class_p && !successful_parse_p)
6301 /* Restore the saved scope. */
6302 parser->scope = saved_scope;
6303 parser->qualifying_scope = saved_qualifying_scope;
6304 parser->object_scope = saved_object_scope;
6305 /* If we are not looking at an identifier followed by the scope
6306 resolution operator, then this is not part of a
6307 nested-name-specifier. (Note that this function is only used
6308 to parse the components of a nested-name-specifier.) */
6309 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6310 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6311 return error_mark_node;
6312 scope = cp_parser_namespace_name (parser);
6315 return scope;
6318 /* Return true if we are looking at a compound-literal, false otherwise. */
6320 static bool
6321 cp_parser_compound_literal_p (cp_parser *parser)
6323 /* Consume the `('. */
6324 cp_lexer_consume_token (parser->lexer);
6326 cp_lexer_save_tokens (parser->lexer);
6328 /* Skip tokens until the next token is a closing parenthesis.
6329 If we find the closing `)', and the next token is a `{', then
6330 we are looking at a compound-literal. */
6331 bool compound_literal_p
6332 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6333 /*consume_paren=*/true)
6334 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6336 /* Roll back the tokens we skipped. */
6337 cp_lexer_rollback_tokens (parser->lexer);
6339 return compound_literal_p;
6342 /* Parse a postfix-expression.
6344 postfix-expression:
6345 primary-expression
6346 postfix-expression [ expression ]
6347 postfix-expression ( expression-list [opt] )
6348 simple-type-specifier ( expression-list [opt] )
6349 typename :: [opt] nested-name-specifier identifier
6350 ( expression-list [opt] )
6351 typename :: [opt] nested-name-specifier template [opt] template-id
6352 ( expression-list [opt] )
6353 postfix-expression . template [opt] id-expression
6354 postfix-expression -> template [opt] id-expression
6355 postfix-expression . pseudo-destructor-name
6356 postfix-expression -> pseudo-destructor-name
6357 postfix-expression ++
6358 postfix-expression --
6359 dynamic_cast < type-id > ( expression )
6360 static_cast < type-id > ( expression )
6361 reinterpret_cast < type-id > ( expression )
6362 const_cast < type-id > ( expression )
6363 typeid ( expression )
6364 typeid ( type-id )
6366 GNU Extension:
6368 postfix-expression:
6369 ( type-id ) { initializer-list , [opt] }
6371 This extension is a GNU version of the C99 compound-literal
6372 construct. (The C99 grammar uses `type-name' instead of `type-id',
6373 but they are essentially the same concept.)
6375 If ADDRESS_P is true, the postfix expression is the operand of the
6376 `&' operator. CAST_P is true if this expression is the target of a
6377 cast.
6379 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6380 class member access expressions [expr.ref].
6382 Returns a representation of the expression. */
6384 static cp_expr
6385 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6386 bool member_access_only_p, bool decltype_p,
6387 cp_id_kind * pidk_return)
6389 cp_token *token;
6390 location_t loc;
6391 enum rid keyword;
6392 cp_id_kind idk = CP_ID_KIND_NONE;
6393 cp_expr postfix_expression = NULL_TREE;
6394 bool is_member_access = false;
6395 int saved_in_statement = -1;
6397 /* Peek at the next token. */
6398 token = cp_lexer_peek_token (parser->lexer);
6399 loc = token->location;
6400 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6402 /* Some of the productions are determined by keywords. */
6403 keyword = token->keyword;
6404 switch (keyword)
6406 case RID_DYNCAST:
6407 case RID_STATCAST:
6408 case RID_REINTCAST:
6409 case RID_CONSTCAST:
6411 tree type;
6412 cp_expr expression;
6413 const char *saved_message;
6414 bool saved_in_type_id_in_expr_p;
6416 /* All of these can be handled in the same way from the point
6417 of view of parsing. Begin by consuming the token
6418 identifying the cast. */
6419 cp_lexer_consume_token (parser->lexer);
6421 /* New types cannot be defined in the cast. */
6422 saved_message = parser->type_definition_forbidden_message;
6423 parser->type_definition_forbidden_message
6424 = G_("types may not be defined in casts");
6426 /* Look for the opening `<'. */
6427 cp_parser_require (parser, CPP_LESS, RT_LESS);
6428 /* Parse the type to which we are casting. */
6429 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6430 parser->in_type_id_in_expr_p = true;
6431 type = cp_parser_type_id (parser);
6432 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6433 /* Look for the closing `>'. */
6434 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6435 /* Restore the old message. */
6436 parser->type_definition_forbidden_message = saved_message;
6438 bool saved_greater_than_is_operator_p
6439 = parser->greater_than_is_operator_p;
6440 parser->greater_than_is_operator_p = true;
6442 /* And the expression which is being cast. */
6443 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6444 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6445 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6446 RT_CLOSE_PAREN);
6447 location_t end_loc = close_paren ?
6448 close_paren->location : UNKNOWN_LOCATION;
6450 parser->greater_than_is_operator_p
6451 = saved_greater_than_is_operator_p;
6453 /* Only type conversions to integral or enumeration types
6454 can be used in constant-expressions. */
6455 if (!cast_valid_in_integral_constant_expression_p (type)
6456 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6458 postfix_expression = error_mark_node;
6459 break;
6462 switch (keyword)
6464 case RID_DYNCAST:
6465 postfix_expression
6466 = build_dynamic_cast (type, expression, tf_warning_or_error);
6467 break;
6468 case RID_STATCAST:
6469 postfix_expression
6470 = build_static_cast (type, expression, tf_warning_or_error);
6471 break;
6472 case RID_REINTCAST:
6473 postfix_expression
6474 = build_reinterpret_cast (type, expression,
6475 tf_warning_or_error);
6476 break;
6477 case RID_CONSTCAST:
6478 postfix_expression
6479 = build_const_cast (type, expression, tf_warning_or_error);
6480 break;
6481 default:
6482 gcc_unreachable ();
6485 /* Construct a location e.g. :
6486 reinterpret_cast <int *> (expr)
6487 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6488 ranging from the start of the "*_cast" token to the final closing
6489 paren, with the caret at the start. */
6490 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6491 postfix_expression.set_location (cp_cast_loc);
6493 break;
6495 case RID_TYPEID:
6497 tree type;
6498 const char *saved_message;
6499 bool saved_in_type_id_in_expr_p;
6501 /* Consume the `typeid' token. */
6502 cp_lexer_consume_token (parser->lexer);
6503 /* Look for the `(' token. */
6504 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6505 /* Types cannot be defined in a `typeid' expression. */
6506 saved_message = parser->type_definition_forbidden_message;
6507 parser->type_definition_forbidden_message
6508 = G_("types may not be defined in a %<typeid%> expression");
6509 /* We can't be sure yet whether we're looking at a type-id or an
6510 expression. */
6511 cp_parser_parse_tentatively (parser);
6512 /* Try a type-id first. */
6513 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6514 parser->in_type_id_in_expr_p = true;
6515 type = cp_parser_type_id (parser);
6516 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6517 /* Look for the `)' token. Otherwise, we can't be sure that
6518 we're not looking at an expression: consider `typeid (int
6519 (3))', for example. */
6520 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6521 /* If all went well, simply lookup the type-id. */
6522 if (cp_parser_parse_definitely (parser))
6523 postfix_expression = get_typeid (type, tf_warning_or_error);
6524 /* Otherwise, fall back to the expression variant. */
6525 else
6527 tree expression;
6529 /* Look for an expression. */
6530 expression = cp_parser_expression (parser, & idk);
6531 /* Compute its typeid. */
6532 postfix_expression = build_typeid (expression, tf_warning_or_error);
6533 /* Look for the `)' token. */
6534 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6536 /* Restore the saved message. */
6537 parser->type_definition_forbidden_message = saved_message;
6538 /* `typeid' may not appear in an integral constant expression. */
6539 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6540 postfix_expression = error_mark_node;
6542 break;
6544 case RID_TYPENAME:
6546 tree type;
6547 /* The syntax permitted here is the same permitted for an
6548 elaborated-type-specifier. */
6549 ++parser->prevent_constrained_type_specifiers;
6550 type = cp_parser_elaborated_type_specifier (parser,
6551 /*is_friend=*/false,
6552 /*is_declaration=*/false);
6553 --parser->prevent_constrained_type_specifiers;
6554 postfix_expression = cp_parser_functional_cast (parser, type);
6556 break;
6558 case RID_CILK_SPAWN:
6560 location_t cilk_spawn_loc
6561 = cp_lexer_peek_token (parser->lexer)->location;
6562 cp_lexer_consume_token (parser->lexer);
6563 token = cp_lexer_peek_token (parser->lexer);
6564 if (token->type == CPP_SEMICOLON)
6566 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6567 "an expression");
6568 postfix_expression = error_mark_node;
6569 break;
6571 else if (!current_function_decl)
6573 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6574 "inside a function");
6575 postfix_expression = error_mark_node;
6576 break;
6578 else
6580 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6581 saved_in_statement = parser->in_statement;
6582 parser->in_statement |= IN_CILK_SPAWN;
6584 cfun->calls_cilk_spawn = 1;
6585 postfix_expression =
6586 cp_parser_postfix_expression (parser, false, false,
6587 false, false, &idk);
6588 if (!flag_cilkplus)
6590 error_at (token->location, "-fcilkplus must be enabled to use"
6591 " %<_Cilk_spawn%>");
6592 cfun->calls_cilk_spawn = 0;
6594 else if (saved_in_statement & IN_CILK_SPAWN)
6596 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6597 "are not permitted");
6598 postfix_expression = error_mark_node;
6599 cfun->calls_cilk_spawn = 0;
6601 else
6603 location_t loc = postfix_expression.get_location ();
6604 postfix_expression = build_cilk_spawn (token->location,
6605 postfix_expression);
6606 /* Build a location of the form:
6607 _Cilk_spawn expr
6608 ~~~~~~~~~~~~^~~~
6609 with caret at the expr, ranging from the start of the
6610 _Cilk_spawn token to the end of the expression. */
6611 location_t combined_loc =
6612 make_location (loc, cilk_spawn_loc, get_finish (loc));
6613 postfix_expression.set_location (combined_loc);
6614 if (postfix_expression != error_mark_node)
6615 SET_EXPR_LOCATION (postfix_expression, input_location);
6616 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6618 break;
6621 case RID_ADDRESSOF:
6622 case RID_BUILTIN_SHUFFLE:
6623 case RID_BUILTIN_LAUNDER:
6625 vec<tree, va_gc> *vec;
6626 unsigned int i;
6627 tree p;
6629 cp_lexer_consume_token (parser->lexer);
6630 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6631 /*cast_p=*/false, /*allow_expansion_p=*/true,
6632 /*non_constant_p=*/NULL);
6633 if (vec == NULL)
6635 postfix_expression = error_mark_node;
6636 break;
6639 FOR_EACH_VEC_ELT (*vec, i, p)
6640 mark_exp_read (p);
6642 switch (keyword)
6644 case RID_ADDRESSOF:
6645 if (vec->length () == 1)
6646 postfix_expression
6647 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6648 else
6650 error_at (loc, "wrong number of arguments to "
6651 "%<__builtin_addressof%>");
6652 postfix_expression = error_mark_node;
6654 break;
6656 case RID_BUILTIN_LAUNDER:
6657 if (vec->length () == 1)
6658 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6659 tf_warning_or_error);
6660 else
6662 error_at (loc, "wrong number of arguments to "
6663 "%<__builtin_launder%>");
6664 postfix_expression = error_mark_node;
6666 break;
6668 case RID_BUILTIN_SHUFFLE:
6669 if (vec->length () == 2)
6670 postfix_expression
6671 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6672 (*vec)[1], tf_warning_or_error);
6673 else if (vec->length () == 3)
6674 postfix_expression
6675 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6676 (*vec)[2], tf_warning_or_error);
6677 else
6679 error_at (loc, "wrong number of arguments to "
6680 "%<__builtin_shuffle%>");
6681 postfix_expression = error_mark_node;
6683 break;
6685 default:
6686 gcc_unreachable ();
6688 break;
6691 default:
6693 tree type;
6695 /* If the next thing is a simple-type-specifier, we may be
6696 looking at a functional cast. We could also be looking at
6697 an id-expression. So, we try the functional cast, and if
6698 that doesn't work we fall back to the primary-expression. */
6699 cp_parser_parse_tentatively (parser);
6700 /* Look for the simple-type-specifier. */
6701 ++parser->prevent_constrained_type_specifiers;
6702 type = cp_parser_simple_type_specifier (parser,
6703 /*decl_specs=*/NULL,
6704 CP_PARSER_FLAGS_NONE);
6705 --parser->prevent_constrained_type_specifiers;
6706 /* Parse the cast itself. */
6707 if (!cp_parser_error_occurred (parser))
6708 postfix_expression
6709 = cp_parser_functional_cast (parser, type);
6710 /* If that worked, we're done. */
6711 if (cp_parser_parse_definitely (parser))
6712 break;
6714 /* If the functional-cast didn't work out, try a
6715 compound-literal. */
6716 if (cp_parser_allow_gnu_extensions_p (parser)
6717 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6719 cp_expr initializer = NULL_TREE;
6721 cp_parser_parse_tentatively (parser);
6723 /* Avoid calling cp_parser_type_id pointlessly, see comment
6724 in cp_parser_cast_expression about c++/29234. */
6725 if (!cp_parser_compound_literal_p (parser))
6726 cp_parser_simulate_error (parser);
6727 else
6729 /* Parse the type. */
6730 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6731 parser->in_type_id_in_expr_p = true;
6732 type = cp_parser_type_id (parser);
6733 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6734 /* Look for the `)'. */
6735 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6738 /* If things aren't going well, there's no need to
6739 keep going. */
6740 if (!cp_parser_error_occurred (parser))
6742 bool non_constant_p;
6743 /* Parse the brace-enclosed initializer list. */
6744 initializer = cp_parser_braced_list (parser,
6745 &non_constant_p);
6747 /* If that worked, we're definitely looking at a
6748 compound-literal expression. */
6749 if (cp_parser_parse_definitely (parser))
6751 /* Warn the user that a compound literal is not
6752 allowed in standard C++. */
6753 pedwarn (input_location, OPT_Wpedantic,
6754 "ISO C++ forbids compound-literals");
6755 /* For simplicity, we disallow compound literals in
6756 constant-expressions. We could
6757 allow compound literals of integer type, whose
6758 initializer was a constant, in constant
6759 expressions. Permitting that usage, as a further
6760 extension, would not change the meaning of any
6761 currently accepted programs. (Of course, as
6762 compound literals are not part of ISO C++, the
6763 standard has nothing to say.) */
6764 if (cp_parser_non_integral_constant_expression (parser,
6765 NIC_NCC))
6767 postfix_expression = error_mark_node;
6768 break;
6770 /* Form the representation of the compound-literal. */
6771 postfix_expression
6772 = finish_compound_literal (type, initializer,
6773 tf_warning_or_error, fcl_c99);
6774 postfix_expression.set_location (initializer.get_location ());
6775 break;
6779 /* It must be a primary-expression. */
6780 postfix_expression
6781 = cp_parser_primary_expression (parser, address_p, cast_p,
6782 /*template_arg_p=*/false,
6783 decltype_p,
6784 &idk);
6786 break;
6789 /* Note that we don't need to worry about calling build_cplus_new on a
6790 class-valued CALL_EXPR in decltype when it isn't the end of the
6791 postfix-expression; unary_complex_lvalue will take care of that for
6792 all these cases. */
6794 /* Keep looping until the postfix-expression is complete. */
6795 while (true)
6797 if (idk == CP_ID_KIND_UNQUALIFIED
6798 && identifier_p (postfix_expression)
6799 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6800 /* It is not a Koenig lookup function call. */
6801 postfix_expression
6802 = unqualified_name_lookup_error (postfix_expression);
6804 /* Peek at the next token. */
6805 token = cp_lexer_peek_token (parser->lexer);
6807 switch (token->type)
6809 case CPP_OPEN_SQUARE:
6810 if (cp_next_tokens_can_be_std_attribute_p (parser))
6812 cp_parser_error (parser,
6813 "two consecutive %<[%> shall "
6814 "only introduce an attribute");
6815 return error_mark_node;
6817 postfix_expression
6818 = cp_parser_postfix_open_square_expression (parser,
6819 postfix_expression,
6820 false,
6821 decltype_p);
6822 postfix_expression.set_range (start_loc,
6823 postfix_expression.get_location ());
6825 idk = CP_ID_KIND_NONE;
6826 is_member_access = false;
6827 break;
6829 case CPP_OPEN_PAREN:
6830 /* postfix-expression ( expression-list [opt] ) */
6832 bool koenig_p;
6833 bool is_builtin_constant_p;
6834 bool saved_integral_constant_expression_p = false;
6835 bool saved_non_integral_constant_expression_p = false;
6836 tsubst_flags_t complain = complain_flags (decltype_p);
6837 vec<tree, va_gc> *args;
6838 location_t close_paren_loc = UNKNOWN_LOCATION;
6840 is_member_access = false;
6842 is_builtin_constant_p
6843 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6844 if (is_builtin_constant_p)
6846 /* The whole point of __builtin_constant_p is to allow
6847 non-constant expressions to appear as arguments. */
6848 saved_integral_constant_expression_p
6849 = parser->integral_constant_expression_p;
6850 saved_non_integral_constant_expression_p
6851 = parser->non_integral_constant_expression_p;
6852 parser->integral_constant_expression_p = false;
6854 args = (cp_parser_parenthesized_expression_list
6855 (parser, non_attr,
6856 /*cast_p=*/false, /*allow_expansion_p=*/true,
6857 /*non_constant_p=*/NULL,
6858 /*close_paren_loc=*/&close_paren_loc));
6859 if (is_builtin_constant_p)
6861 parser->integral_constant_expression_p
6862 = saved_integral_constant_expression_p;
6863 parser->non_integral_constant_expression_p
6864 = saved_non_integral_constant_expression_p;
6867 if (args == NULL)
6869 postfix_expression = error_mark_node;
6870 break;
6873 /* Function calls are not permitted in
6874 constant-expressions. */
6875 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6876 && cp_parser_non_integral_constant_expression (parser,
6877 NIC_FUNC_CALL))
6879 postfix_expression = error_mark_node;
6880 release_tree_vector (args);
6881 break;
6884 koenig_p = false;
6885 if (idk == CP_ID_KIND_UNQUALIFIED
6886 || idk == CP_ID_KIND_TEMPLATE_ID)
6888 if (identifier_p (postfix_expression))
6890 if (!args->is_empty ())
6892 koenig_p = true;
6893 if (!any_type_dependent_arguments_p (args))
6894 postfix_expression
6895 = perform_koenig_lookup (postfix_expression, args,
6896 complain);
6898 else
6899 postfix_expression
6900 = unqualified_fn_lookup_error (postfix_expression);
6902 /* We do not perform argument-dependent lookup if
6903 normal lookup finds a non-function, in accordance
6904 with the expected resolution of DR 218. */
6905 else if (!args->is_empty ()
6906 && is_overloaded_fn (postfix_expression))
6908 tree fn = get_first_fn (postfix_expression);
6909 fn = STRIP_TEMPLATE (fn);
6911 /* Do not do argument dependent lookup if regular
6912 lookup finds a member function or a block-scope
6913 function declaration. [basic.lookup.argdep]/3 */
6914 if (!DECL_FUNCTION_MEMBER_P (fn)
6915 && !DECL_LOCAL_FUNCTION_P (fn))
6917 koenig_p = true;
6918 if (!any_type_dependent_arguments_p (args))
6919 postfix_expression
6920 = perform_koenig_lookup (postfix_expression, args,
6921 complain);
6926 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6927 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6928 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6929 && vec_safe_length (args) == 3)
6931 tree arg0 = (*args)[0];
6932 tree arg1 = (*args)[1];
6933 tree arg2 = (*args)[2];
6934 int literal_mask = ((!!integer_zerop (arg1) << 1)
6935 | (!!integer_zerop (arg2) << 2));
6936 if (TREE_CODE (arg2) == CONST_DECL)
6937 arg2 = DECL_INITIAL (arg2);
6938 warn_for_memset (input_location, arg0, arg2, literal_mask);
6941 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6943 tree instance = TREE_OPERAND (postfix_expression, 0);
6944 tree fn = TREE_OPERAND (postfix_expression, 1);
6946 if (processing_template_decl
6947 && (type_dependent_object_expression_p (instance)
6948 || (!BASELINK_P (fn)
6949 && TREE_CODE (fn) != FIELD_DECL)
6950 || type_dependent_expression_p (fn)
6951 || any_type_dependent_arguments_p (args)))
6953 maybe_generic_this_capture (instance, fn);
6954 postfix_expression
6955 = build_min_nt_call_vec (postfix_expression, args);
6956 release_tree_vector (args);
6957 break;
6960 if (BASELINK_P (fn))
6962 postfix_expression
6963 = (build_new_method_call
6964 (instance, fn, &args, NULL_TREE,
6965 (idk == CP_ID_KIND_QUALIFIED
6966 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6967 : LOOKUP_NORMAL),
6968 /*fn_p=*/NULL,
6969 complain));
6971 else
6972 postfix_expression
6973 = finish_call_expr (postfix_expression, &args,
6974 /*disallow_virtual=*/false,
6975 /*koenig_p=*/false,
6976 complain);
6978 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6979 || TREE_CODE (postfix_expression) == MEMBER_REF
6980 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6981 postfix_expression = (build_offset_ref_call_from_tree
6982 (postfix_expression, &args,
6983 complain));
6984 else if (idk == CP_ID_KIND_QUALIFIED)
6985 /* A call to a static class member, or a namespace-scope
6986 function. */
6987 postfix_expression
6988 = finish_call_expr (postfix_expression, &args,
6989 /*disallow_virtual=*/true,
6990 koenig_p,
6991 complain);
6992 else
6993 /* All other function calls. */
6994 postfix_expression
6995 = finish_call_expr (postfix_expression, &args,
6996 /*disallow_virtual=*/false,
6997 koenig_p,
6998 complain);
7000 if (close_paren_loc != UNKNOWN_LOCATION)
7002 location_t combined_loc = make_location (token->location,
7003 start_loc,
7004 close_paren_loc);
7005 postfix_expression.set_location (combined_loc);
7008 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7009 idk = CP_ID_KIND_NONE;
7011 release_tree_vector (args);
7013 break;
7015 case CPP_DOT:
7016 case CPP_DEREF:
7017 /* postfix-expression . template [opt] id-expression
7018 postfix-expression . pseudo-destructor-name
7019 postfix-expression -> template [opt] id-expression
7020 postfix-expression -> pseudo-destructor-name */
7022 /* Consume the `.' or `->' operator. */
7023 cp_lexer_consume_token (parser->lexer);
7025 postfix_expression
7026 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7027 postfix_expression,
7028 false, &idk, loc);
7030 is_member_access = true;
7031 break;
7033 case CPP_PLUS_PLUS:
7034 /* postfix-expression ++ */
7035 /* Consume the `++' token. */
7036 cp_lexer_consume_token (parser->lexer);
7037 /* Generate a representation for the complete expression. */
7038 postfix_expression
7039 = finish_increment_expr (postfix_expression,
7040 POSTINCREMENT_EXPR);
7041 /* Increments may not appear in constant-expressions. */
7042 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7043 postfix_expression = error_mark_node;
7044 idk = CP_ID_KIND_NONE;
7045 is_member_access = false;
7046 break;
7048 case CPP_MINUS_MINUS:
7049 /* postfix-expression -- */
7050 /* Consume the `--' token. */
7051 cp_lexer_consume_token (parser->lexer);
7052 /* Generate a representation for the complete expression. */
7053 postfix_expression
7054 = finish_increment_expr (postfix_expression,
7055 POSTDECREMENT_EXPR);
7056 /* Decrements may not appear in constant-expressions. */
7057 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7058 postfix_expression = error_mark_node;
7059 idk = CP_ID_KIND_NONE;
7060 is_member_access = false;
7061 break;
7063 default:
7064 if (pidk_return != NULL)
7065 * pidk_return = idk;
7066 if (member_access_only_p)
7067 return is_member_access
7068 ? postfix_expression
7069 : cp_expr (error_mark_node);
7070 else
7071 return postfix_expression;
7075 /* We should never get here. */
7076 gcc_unreachable ();
7077 return error_mark_node;
7080 /* This function parses Cilk Plus array notations. If a normal array expr. is
7081 parsed then the array index is passed back to the caller through *INIT_INDEX
7082 and the function returns a NULL_TREE. If array notation expr. is parsed,
7083 then *INIT_INDEX is ignored by the caller and the function returns
7084 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7085 error_mark_node. */
7087 static tree
7088 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7089 tree array_value)
7091 cp_token *token = NULL;
7092 tree length_index, stride = NULL_TREE, value_tree, array_type;
7093 if (!array_value || array_value == error_mark_node)
7095 cp_parser_skip_to_end_of_statement (parser);
7096 return error_mark_node;
7099 array_type = TREE_TYPE (array_value);
7101 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7102 parser->colon_corrects_to_scope_p = false;
7103 token = cp_lexer_peek_token (parser->lexer);
7105 if (!token)
7107 cp_parser_error (parser, "expected %<:%> or numeral");
7108 return error_mark_node;
7110 else if (token->type == CPP_COLON)
7112 /* Consume the ':'. */
7113 cp_lexer_consume_token (parser->lexer);
7115 /* If we are here, then we have a case like this A[:]. */
7116 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7118 cp_parser_error (parser, "expected %<]%>");
7119 cp_parser_skip_to_end_of_statement (parser);
7120 return error_mark_node;
7122 *init_index = NULL_TREE;
7123 stride = NULL_TREE;
7124 length_index = NULL_TREE;
7126 else
7128 /* If we are here, then there are three valid possibilities:
7129 1. ARRAY [ EXP ]
7130 2. ARRAY [ EXP : EXP ]
7131 3. ARRAY [ EXP : EXP : EXP ] */
7133 *init_index = cp_parser_expression (parser);
7134 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7136 /* This indicates that we have a normal array expression. */
7137 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7138 return NULL_TREE;
7141 /* Consume the ':'. */
7142 cp_lexer_consume_token (parser->lexer);
7143 length_index = cp_parser_expression (parser);
7144 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7146 cp_lexer_consume_token (parser->lexer);
7147 stride = cp_parser_expression (parser);
7150 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7152 if (*init_index == error_mark_node || length_index == error_mark_node
7153 || stride == error_mark_node || array_type == error_mark_node)
7155 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7156 cp_lexer_consume_token (parser->lexer);
7157 return error_mark_node;
7159 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7161 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7162 length_index, stride, array_type);
7163 return value_tree;
7166 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7167 by cp_parser_builtin_offsetof. We're looking for
7169 postfix-expression [ expression ]
7170 postfix-expression [ braced-init-list ] (C++11)
7172 FOR_OFFSETOF is set if we're being called in that context, which
7173 changes how we deal with integer constant expressions. */
7175 static tree
7176 cp_parser_postfix_open_square_expression (cp_parser *parser,
7177 tree postfix_expression,
7178 bool for_offsetof,
7179 bool decltype_p)
7181 tree index = NULL_TREE;
7182 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7183 bool saved_greater_than_is_operator_p;
7185 /* Consume the `[' token. */
7186 cp_lexer_consume_token (parser->lexer);
7188 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7189 parser->greater_than_is_operator_p = true;
7191 /* Parse the index expression. */
7192 /* ??? For offsetof, there is a question of what to allow here. If
7193 offsetof is not being used in an integral constant expression context,
7194 then we *could* get the right answer by computing the value at runtime.
7195 If we are in an integral constant expression context, then we might
7196 could accept any constant expression; hard to say without analysis.
7197 Rather than open the barn door too wide right away, allow only integer
7198 constant expressions here. */
7199 if (for_offsetof)
7200 index = cp_parser_constant_expression (parser);
7201 else
7203 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7205 bool expr_nonconst_p;
7206 cp_lexer_set_source_position (parser->lexer);
7207 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7208 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7209 if (flag_cilkplus
7210 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7212 error_at (cp_lexer_peek_token (parser->lexer)->location,
7213 "braced list index is not allowed with array "
7214 "notation");
7215 cp_parser_skip_to_end_of_statement (parser);
7216 return error_mark_node;
7219 else if (flag_cilkplus)
7221 /* Here are have these two options:
7222 ARRAY[EXP : EXP] - Array notation expr with default
7223 stride of 1.
7224 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7225 stride. */
7226 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7227 postfix_expression);
7228 if (an_exp)
7229 return an_exp;
7231 else
7232 index = cp_parser_expression (parser);
7235 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7237 /* Look for the closing `]'. */
7238 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7240 /* Build the ARRAY_REF. */
7241 postfix_expression = grok_array_decl (loc, postfix_expression,
7242 index, decltype_p);
7244 /* When not doing offsetof, array references are not permitted in
7245 constant-expressions. */
7246 if (!for_offsetof
7247 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7248 postfix_expression = error_mark_node;
7250 return postfix_expression;
7253 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7254 by cp_parser_builtin_offsetof. We're looking for
7256 postfix-expression . template [opt] id-expression
7257 postfix-expression . pseudo-destructor-name
7258 postfix-expression -> template [opt] id-expression
7259 postfix-expression -> pseudo-destructor-name
7261 FOR_OFFSETOF is set if we're being called in that context. That sorta
7262 limits what of the above we'll actually accept, but nevermind.
7263 TOKEN_TYPE is the "." or "->" token, which will already have been
7264 removed from the stream. */
7266 static tree
7267 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7268 enum cpp_ttype token_type,
7269 cp_expr postfix_expression,
7270 bool for_offsetof, cp_id_kind *idk,
7271 location_t location)
7273 tree name;
7274 bool dependent_p;
7275 bool pseudo_destructor_p;
7276 tree scope = NULL_TREE;
7277 location_t start_loc = postfix_expression.get_start ();
7279 /* If this is a `->' operator, dereference the pointer. */
7280 if (token_type == CPP_DEREF)
7281 postfix_expression = build_x_arrow (location, postfix_expression,
7282 tf_warning_or_error);
7283 /* Check to see whether or not the expression is type-dependent and
7284 not the current instantiation. */
7285 dependent_p = type_dependent_object_expression_p (postfix_expression);
7286 /* The identifier following the `->' or `.' is not qualified. */
7287 parser->scope = NULL_TREE;
7288 parser->qualifying_scope = NULL_TREE;
7289 parser->object_scope = NULL_TREE;
7290 *idk = CP_ID_KIND_NONE;
7292 /* Enter the scope corresponding to the type of the object
7293 given by the POSTFIX_EXPRESSION. */
7294 if (!dependent_p)
7296 scope = TREE_TYPE (postfix_expression);
7297 /* According to the standard, no expression should ever have
7298 reference type. Unfortunately, we do not currently match
7299 the standard in this respect in that our internal representation
7300 of an expression may have reference type even when the standard
7301 says it does not. Therefore, we have to manually obtain the
7302 underlying type here. */
7303 scope = non_reference (scope);
7304 /* The type of the POSTFIX_EXPRESSION must be complete. */
7305 /* Unlike the object expression in other contexts, *this is not
7306 required to be of complete type for purposes of class member
7307 access (5.2.5) outside the member function body. */
7308 if (postfix_expression != current_class_ref
7309 && scope != error_mark_node
7310 && !(processing_template_decl
7311 && current_class_type
7312 && (same_type_ignoring_top_level_qualifiers_p
7313 (scope, current_class_type))))
7315 scope = complete_type (scope);
7316 if (!COMPLETE_TYPE_P (scope)
7317 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7318 && EXPR_P (postfix_expression))
7320 /* In a template, be permissive by treating an object expression
7321 of incomplete type as dependent (after a pedwarn). */
7322 diagnostic_t kind = (processing_template_decl
7323 ? DK_PEDWARN
7324 : DK_ERROR);
7325 cxx_incomplete_type_diagnostic
7326 (location_of (postfix_expression),
7327 postfix_expression, scope, kind);
7328 if (processing_template_decl)
7330 dependent_p = true;
7331 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7336 if (!dependent_p)
7338 /* Let the name lookup machinery know that we are processing a
7339 class member access expression. */
7340 parser->context->object_type = scope;
7341 /* If something went wrong, we want to be able to discern that case,
7342 as opposed to the case where there was no SCOPE due to the type
7343 of expression being dependent. */
7344 if (!scope)
7345 scope = error_mark_node;
7346 /* If the SCOPE was erroneous, make the various semantic analysis
7347 functions exit quickly -- and without issuing additional error
7348 messages. */
7349 if (scope == error_mark_node)
7350 postfix_expression = error_mark_node;
7354 if (dependent_p)
7355 /* Tell cp_parser_lookup_name that there was an object, even though it's
7356 type-dependent. */
7357 parser->context->object_type = unknown_type_node;
7359 /* Assume this expression is not a pseudo-destructor access. */
7360 pseudo_destructor_p = false;
7362 /* If the SCOPE is a scalar type, then, if this is a valid program,
7363 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7364 is type dependent, it can be pseudo-destructor-name or something else.
7365 Try to parse it as pseudo-destructor-name first. */
7366 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7368 tree s;
7369 tree type;
7371 cp_parser_parse_tentatively (parser);
7372 /* Parse the pseudo-destructor-name. */
7373 s = NULL_TREE;
7374 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7375 &s, &type);
7376 if (dependent_p
7377 && (cp_parser_error_occurred (parser)
7378 || !SCALAR_TYPE_P (type)))
7379 cp_parser_abort_tentative_parse (parser);
7380 else if (cp_parser_parse_definitely (parser))
7382 pseudo_destructor_p = true;
7383 postfix_expression
7384 = finish_pseudo_destructor_expr (postfix_expression,
7385 s, type, location);
7389 if (!pseudo_destructor_p)
7391 /* If the SCOPE is not a scalar type, we are looking at an
7392 ordinary class member access expression, rather than a
7393 pseudo-destructor-name. */
7394 bool template_p;
7395 cp_token *token = cp_lexer_peek_token (parser->lexer);
7396 /* Parse the id-expression. */
7397 name = (cp_parser_id_expression
7398 (parser,
7399 cp_parser_optional_template_keyword (parser),
7400 /*check_dependency_p=*/true,
7401 &template_p,
7402 /*declarator_p=*/false,
7403 /*optional_p=*/false));
7404 /* In general, build a SCOPE_REF if the member name is qualified.
7405 However, if the name was not dependent and has already been
7406 resolved; there is no need to build the SCOPE_REF. For example;
7408 struct X { void f(); };
7409 template <typename T> void f(T* t) { t->X::f(); }
7411 Even though "t" is dependent, "X::f" is not and has been resolved
7412 to a BASELINK; there is no need to include scope information. */
7414 /* But we do need to remember that there was an explicit scope for
7415 virtual function calls. */
7416 if (parser->scope)
7417 *idk = CP_ID_KIND_QUALIFIED;
7419 /* If the name is a template-id that names a type, we will get a
7420 TYPE_DECL here. That is invalid code. */
7421 if (TREE_CODE (name) == TYPE_DECL)
7423 error_at (token->location, "invalid use of %qD", name);
7424 postfix_expression = error_mark_node;
7426 else
7428 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7430 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7432 error_at (token->location, "%<%D::%D%> is not a class member",
7433 parser->scope, name);
7434 postfix_expression = error_mark_node;
7436 else
7437 name = build_qualified_name (/*type=*/NULL_TREE,
7438 parser->scope,
7439 name,
7440 template_p);
7441 parser->scope = NULL_TREE;
7442 parser->qualifying_scope = NULL_TREE;
7443 parser->object_scope = NULL_TREE;
7445 if (parser->scope && name && BASELINK_P (name))
7446 adjust_result_of_qualified_name_lookup
7447 (name, parser->scope, scope);
7448 postfix_expression
7449 = finish_class_member_access_expr (postfix_expression, name,
7450 template_p,
7451 tf_warning_or_error);
7452 /* Build a location e.g.:
7453 ptr->access_expr
7454 ~~~^~~~~~~~~~~~~
7455 where the caret is at the deref token, ranging from
7456 the start of postfix_expression to the end of the access expr. */
7457 location_t end_loc
7458 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7459 location_t combined_loc
7460 = make_location (input_location, start_loc, end_loc);
7461 protected_set_expr_location (postfix_expression, combined_loc);
7465 /* We no longer need to look up names in the scope of the object on
7466 the left-hand side of the `.' or `->' operator. */
7467 parser->context->object_type = NULL_TREE;
7469 /* Outside of offsetof, these operators may not appear in
7470 constant-expressions. */
7471 if (!for_offsetof
7472 && (cp_parser_non_integral_constant_expression
7473 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7474 postfix_expression = error_mark_node;
7476 return postfix_expression;
7479 /* Parse a parenthesized expression-list.
7481 expression-list:
7482 assignment-expression
7483 expression-list, assignment-expression
7485 attribute-list:
7486 expression-list
7487 identifier
7488 identifier, expression-list
7490 CAST_P is true if this expression is the target of a cast.
7492 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7493 argument pack.
7495 Returns a vector of trees. Each element is a representation of an
7496 assignment-expression. NULL is returned if the ( and or ) are
7497 missing. An empty, but allocated, vector is returned on no
7498 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7499 if we are parsing an attribute list for an attribute that wants a
7500 plain identifier argument, normal_attr for an attribute that wants
7501 an expression, or non_attr if we aren't parsing an attribute list. If
7502 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7503 not all of the expressions in the list were constant.
7504 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7505 will be written to with the location of the closing parenthesis. If
7506 an error occurs, it may or may not be written to. */
7508 static vec<tree, va_gc> *
7509 cp_parser_parenthesized_expression_list (cp_parser* parser,
7510 int is_attribute_list,
7511 bool cast_p,
7512 bool allow_expansion_p,
7513 bool *non_constant_p,
7514 location_t *close_paren_loc)
7516 vec<tree, va_gc> *expression_list;
7517 bool fold_expr_p = is_attribute_list != non_attr;
7518 tree identifier = NULL_TREE;
7519 bool saved_greater_than_is_operator_p;
7521 /* Assume all the expressions will be constant. */
7522 if (non_constant_p)
7523 *non_constant_p = false;
7525 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7526 return NULL;
7528 expression_list = make_tree_vector ();
7530 /* Within a parenthesized expression, a `>' token is always
7531 the greater-than operator. */
7532 saved_greater_than_is_operator_p
7533 = parser->greater_than_is_operator_p;
7534 parser->greater_than_is_operator_p = true;
7536 /* Consume expressions until there are no more. */
7537 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7538 while (true)
7540 tree expr;
7542 /* At the beginning of attribute lists, check to see if the
7543 next token is an identifier. */
7544 if (is_attribute_list == id_attr
7545 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7547 cp_token *token;
7549 /* Consume the identifier. */
7550 token = cp_lexer_consume_token (parser->lexer);
7551 /* Save the identifier. */
7552 identifier = token->u.value;
7554 else
7556 bool expr_non_constant_p;
7558 /* Parse the next assignment-expression. */
7559 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7561 /* A braced-init-list. */
7562 cp_lexer_set_source_position (parser->lexer);
7563 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7564 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7565 if (non_constant_p && expr_non_constant_p)
7566 *non_constant_p = true;
7568 else if (non_constant_p)
7570 expr = (cp_parser_constant_expression
7571 (parser, /*allow_non_constant_p=*/true,
7572 &expr_non_constant_p));
7573 if (expr_non_constant_p)
7574 *non_constant_p = true;
7576 else
7577 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7578 cast_p);
7580 if (fold_expr_p)
7581 expr = instantiate_non_dependent_expr (expr);
7583 /* If we have an ellipsis, then this is an expression
7584 expansion. */
7585 if (allow_expansion_p
7586 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7588 /* Consume the `...'. */
7589 cp_lexer_consume_token (parser->lexer);
7591 /* Build the argument pack. */
7592 expr = make_pack_expansion (expr);
7595 /* Add it to the list. We add error_mark_node
7596 expressions to the list, so that we can still tell if
7597 the correct form for a parenthesized expression-list
7598 is found. That gives better errors. */
7599 vec_safe_push (expression_list, expr);
7601 if (expr == error_mark_node)
7602 goto skip_comma;
7605 /* After the first item, attribute lists look the same as
7606 expression lists. */
7607 is_attribute_list = non_attr;
7609 get_comma:;
7610 /* If the next token isn't a `,', then we are done. */
7611 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7612 break;
7614 /* Otherwise, consume the `,' and keep going. */
7615 cp_lexer_consume_token (parser->lexer);
7618 if (close_paren_loc)
7619 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7621 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7623 int ending;
7625 skip_comma:;
7626 /* We try and resync to an unnested comma, as that will give the
7627 user better diagnostics. */
7628 ending = cp_parser_skip_to_closing_parenthesis (parser,
7629 /*recovering=*/true,
7630 /*or_comma=*/true,
7631 /*consume_paren=*/true);
7632 if (ending < 0)
7633 goto get_comma;
7634 if (!ending)
7636 parser->greater_than_is_operator_p
7637 = saved_greater_than_is_operator_p;
7638 return NULL;
7642 parser->greater_than_is_operator_p
7643 = saved_greater_than_is_operator_p;
7645 if (identifier)
7646 vec_safe_insert (expression_list, 0, identifier);
7648 return expression_list;
7651 /* Parse a pseudo-destructor-name.
7653 pseudo-destructor-name:
7654 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7655 :: [opt] nested-name-specifier template template-id :: ~ type-name
7656 :: [opt] nested-name-specifier [opt] ~ type-name
7658 If either of the first two productions is used, sets *SCOPE to the
7659 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7660 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7661 or ERROR_MARK_NODE if the parse fails. */
7663 static void
7664 cp_parser_pseudo_destructor_name (cp_parser* parser,
7665 tree object,
7666 tree* scope,
7667 tree* type)
7669 bool nested_name_specifier_p;
7671 /* Handle ~auto. */
7672 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7673 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7674 && !type_dependent_expression_p (object))
7676 if (cxx_dialect < cxx14)
7677 pedwarn (input_location, 0,
7678 "%<~auto%> only available with "
7679 "-std=c++14 or -std=gnu++14");
7680 cp_lexer_consume_token (parser->lexer);
7681 cp_lexer_consume_token (parser->lexer);
7682 *scope = NULL_TREE;
7683 *type = TREE_TYPE (object);
7684 return;
7687 /* Assume that things will not work out. */
7688 *type = error_mark_node;
7690 /* Look for the optional `::' operator. */
7691 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7692 /* Look for the optional nested-name-specifier. */
7693 nested_name_specifier_p
7694 = (cp_parser_nested_name_specifier_opt (parser,
7695 /*typename_keyword_p=*/false,
7696 /*check_dependency_p=*/true,
7697 /*type_p=*/false,
7698 /*is_declaration=*/false)
7699 != NULL_TREE);
7700 /* Now, if we saw a nested-name-specifier, we might be doing the
7701 second production. */
7702 if (nested_name_specifier_p
7703 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7705 /* Consume the `template' keyword. */
7706 cp_lexer_consume_token (parser->lexer);
7707 /* Parse the template-id. */
7708 cp_parser_template_id (parser,
7709 /*template_keyword_p=*/true,
7710 /*check_dependency_p=*/false,
7711 class_type,
7712 /*is_declaration=*/true);
7713 /* Look for the `::' token. */
7714 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7716 /* If the next token is not a `~', then there might be some
7717 additional qualification. */
7718 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7720 /* At this point, we're looking for "type-name :: ~". The type-name
7721 must not be a class-name, since this is a pseudo-destructor. So,
7722 it must be either an enum-name, or a typedef-name -- both of which
7723 are just identifiers. So, we peek ahead to check that the "::"
7724 and "~" tokens are present; if they are not, then we can avoid
7725 calling type_name. */
7726 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7727 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7728 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7730 cp_parser_error (parser, "non-scalar type");
7731 return;
7734 /* Look for the type-name. */
7735 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7736 if (*scope == error_mark_node)
7737 return;
7739 /* Look for the `::' token. */
7740 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7742 else
7743 *scope = NULL_TREE;
7745 /* Look for the `~'. */
7746 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7748 /* Once we see the ~, this has to be a pseudo-destructor. */
7749 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7750 cp_parser_commit_to_topmost_tentative_parse (parser);
7752 /* Look for the type-name again. We are not responsible for
7753 checking that it matches the first type-name. */
7754 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7757 /* Parse a unary-expression.
7759 unary-expression:
7760 postfix-expression
7761 ++ cast-expression
7762 -- cast-expression
7763 unary-operator cast-expression
7764 sizeof unary-expression
7765 sizeof ( type-id )
7766 alignof ( type-id ) [C++0x]
7767 new-expression
7768 delete-expression
7770 GNU Extensions:
7772 unary-expression:
7773 __extension__ cast-expression
7774 __alignof__ unary-expression
7775 __alignof__ ( type-id )
7776 alignof unary-expression [C++0x]
7777 __real__ cast-expression
7778 __imag__ cast-expression
7779 && identifier
7780 sizeof ( type-id ) { initializer-list , [opt] }
7781 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7782 __alignof__ ( type-id ) { initializer-list , [opt] }
7784 ADDRESS_P is true iff the unary-expression is appearing as the
7785 operand of the `&' operator. CAST_P is true if this expression is
7786 the target of a cast.
7788 Returns a representation of the expression. */
7790 static cp_expr
7791 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7792 bool address_p, bool cast_p, bool decltype_p)
7794 cp_token *token;
7795 enum tree_code unary_operator;
7797 /* Peek at the next token. */
7798 token = cp_lexer_peek_token (parser->lexer);
7799 /* Some keywords give away the kind of expression. */
7800 if (token->type == CPP_KEYWORD)
7802 enum rid keyword = token->keyword;
7804 switch (keyword)
7806 case RID_ALIGNOF:
7807 case RID_SIZEOF:
7809 tree operand, ret;
7810 enum tree_code op;
7811 location_t start_loc = token->location;
7813 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7814 /* Consume the token. */
7815 cp_lexer_consume_token (parser->lexer);
7816 /* Parse the operand. */
7817 operand = cp_parser_sizeof_operand (parser, keyword);
7819 if (TYPE_P (operand))
7820 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7821 else
7823 /* ISO C++ defines alignof only with types, not with
7824 expressions. So pedwarn if alignof is used with a non-
7825 type expression. However, __alignof__ is ok. */
7826 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7827 pedwarn (token->location, OPT_Wpedantic,
7828 "ISO C++ does not allow %<alignof%> "
7829 "with a non-type");
7831 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7833 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7834 SIZEOF_EXPR with the original operand. */
7835 if (op == SIZEOF_EXPR && ret != error_mark_node)
7837 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7839 if (!processing_template_decl && TYPE_P (operand))
7841 ret = build_min (SIZEOF_EXPR, size_type_node,
7842 build1 (NOP_EXPR, operand,
7843 error_mark_node));
7844 SIZEOF_EXPR_TYPE_P (ret) = 1;
7846 else
7847 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7848 TREE_SIDE_EFFECTS (ret) = 0;
7849 TREE_READONLY (ret) = 1;
7853 /* Construct a location e.g. :
7854 alignof (expr)
7855 ^~~~~~~~~~~~~~
7856 with start == caret at the start of the "alignof"/"sizeof"
7857 token, with the endpoint at the final closing paren. */
7858 location_t finish_loc
7859 = cp_lexer_previous_token (parser->lexer)->location;
7860 location_t compound_loc
7861 = make_location (start_loc, start_loc, finish_loc);
7863 cp_expr ret_expr (ret);
7864 ret_expr.set_location (compound_loc);
7865 return ret_expr;
7868 case RID_NEW:
7869 return cp_parser_new_expression (parser);
7871 case RID_DELETE:
7872 return cp_parser_delete_expression (parser);
7874 case RID_EXTENSION:
7876 /* The saved value of the PEDANTIC flag. */
7877 int saved_pedantic;
7878 tree expr;
7880 /* Save away the PEDANTIC flag. */
7881 cp_parser_extension_opt (parser, &saved_pedantic);
7882 /* Parse the cast-expression. */
7883 expr = cp_parser_simple_cast_expression (parser);
7884 /* Restore the PEDANTIC flag. */
7885 pedantic = saved_pedantic;
7887 return expr;
7890 case RID_REALPART:
7891 case RID_IMAGPART:
7893 tree expression;
7895 /* Consume the `__real__' or `__imag__' token. */
7896 cp_lexer_consume_token (parser->lexer);
7897 /* Parse the cast-expression. */
7898 expression = cp_parser_simple_cast_expression (parser);
7899 /* Create the complete representation. */
7900 return build_x_unary_op (token->location,
7901 (keyword == RID_REALPART
7902 ? REALPART_EXPR : IMAGPART_EXPR),
7903 expression,
7904 tf_warning_or_error);
7906 break;
7908 case RID_TRANSACTION_ATOMIC:
7909 case RID_TRANSACTION_RELAXED:
7910 return cp_parser_transaction_expression (parser, keyword);
7912 case RID_NOEXCEPT:
7914 tree expr;
7915 const char *saved_message;
7916 bool saved_integral_constant_expression_p;
7917 bool saved_non_integral_constant_expression_p;
7918 bool saved_greater_than_is_operator_p;
7920 cp_lexer_consume_token (parser->lexer);
7921 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7923 saved_message = parser->type_definition_forbidden_message;
7924 parser->type_definition_forbidden_message
7925 = G_("types may not be defined in %<noexcept%> expressions");
7927 saved_integral_constant_expression_p
7928 = parser->integral_constant_expression_p;
7929 saved_non_integral_constant_expression_p
7930 = parser->non_integral_constant_expression_p;
7931 parser->integral_constant_expression_p = false;
7933 saved_greater_than_is_operator_p
7934 = parser->greater_than_is_operator_p;
7935 parser->greater_than_is_operator_p = true;
7937 ++cp_unevaluated_operand;
7938 ++c_inhibit_evaluation_warnings;
7939 ++cp_noexcept_operand;
7940 expr = cp_parser_expression (parser);
7941 --cp_noexcept_operand;
7942 --c_inhibit_evaluation_warnings;
7943 --cp_unevaluated_operand;
7945 parser->greater_than_is_operator_p
7946 = saved_greater_than_is_operator_p;
7948 parser->integral_constant_expression_p
7949 = saved_integral_constant_expression_p;
7950 parser->non_integral_constant_expression_p
7951 = saved_non_integral_constant_expression_p;
7953 parser->type_definition_forbidden_message = saved_message;
7955 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7956 return finish_noexcept_expr (expr, tf_warning_or_error);
7959 default:
7960 break;
7964 /* Look for the `:: new' and `:: delete', which also signal the
7965 beginning of a new-expression, or delete-expression,
7966 respectively. If the next token is `::', then it might be one of
7967 these. */
7968 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7970 enum rid keyword;
7972 /* See if the token after the `::' is one of the keywords in
7973 which we're interested. */
7974 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7975 /* If it's `new', we have a new-expression. */
7976 if (keyword == RID_NEW)
7977 return cp_parser_new_expression (parser);
7978 /* Similarly, for `delete'. */
7979 else if (keyword == RID_DELETE)
7980 return cp_parser_delete_expression (parser);
7983 /* Look for a unary operator. */
7984 unary_operator = cp_parser_unary_operator (token);
7985 /* The `++' and `--' operators can be handled similarly, even though
7986 they are not technically unary-operators in the grammar. */
7987 if (unary_operator == ERROR_MARK)
7989 if (token->type == CPP_PLUS_PLUS)
7990 unary_operator = PREINCREMENT_EXPR;
7991 else if (token->type == CPP_MINUS_MINUS)
7992 unary_operator = PREDECREMENT_EXPR;
7993 /* Handle the GNU address-of-label extension. */
7994 else if (cp_parser_allow_gnu_extensions_p (parser)
7995 && token->type == CPP_AND_AND)
7997 tree identifier;
7998 tree expression;
7999 location_t start_loc = token->location;
8001 /* Consume the '&&' token. */
8002 cp_lexer_consume_token (parser->lexer);
8003 /* Look for the identifier. */
8004 location_t finish_loc
8005 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8006 identifier = cp_parser_identifier (parser);
8007 /* Construct a location of the form:
8008 &&label
8009 ^~~~~~~
8010 with caret==start at the "&&", finish at the end of the label. */
8011 location_t combined_loc
8012 = make_location (start_loc, start_loc, finish_loc);
8013 /* Create an expression representing the address. */
8014 expression = finish_label_address_expr (identifier, combined_loc);
8015 if (cp_parser_non_integral_constant_expression (parser,
8016 NIC_ADDR_LABEL))
8017 expression = error_mark_node;
8018 return expression;
8021 if (unary_operator != ERROR_MARK)
8023 cp_expr cast_expression;
8024 cp_expr expression = error_mark_node;
8025 non_integral_constant non_constant_p = NIC_NONE;
8026 location_t loc = token->location;
8027 tsubst_flags_t complain = complain_flags (decltype_p);
8029 /* Consume the operator token. */
8030 token = cp_lexer_consume_token (parser->lexer);
8031 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8033 /* Parse the cast-expression. */
8034 cast_expression
8035 = cp_parser_cast_expression (parser,
8036 unary_operator == ADDR_EXPR,
8037 /*cast_p=*/false,
8038 /*decltype*/false,
8039 pidk);
8041 /* Make a location:
8042 OP_TOKEN CAST_EXPRESSION
8043 ^~~~~~~~~~~~~~~~~~~~~~~~~
8044 with start==caret at the operator token, and
8045 extending to the end of the cast_expression. */
8046 loc = make_location (loc, loc, cast_expression.get_finish ());
8048 /* Now, build an appropriate representation. */
8049 switch (unary_operator)
8051 case INDIRECT_REF:
8052 non_constant_p = NIC_STAR;
8053 expression = build_x_indirect_ref (loc, cast_expression,
8054 RO_UNARY_STAR,
8055 complain);
8056 /* TODO: build_x_indirect_ref does not always honor the
8057 location, so ensure it is set. */
8058 expression.set_location (loc);
8059 break;
8061 case ADDR_EXPR:
8062 non_constant_p = NIC_ADDR;
8063 /* Fall through. */
8064 case BIT_NOT_EXPR:
8065 expression = build_x_unary_op (loc, unary_operator,
8066 cast_expression,
8067 complain);
8068 /* TODO: build_x_unary_op does not always honor the location,
8069 so ensure it is set. */
8070 expression.set_location (loc);
8071 break;
8073 case PREINCREMENT_EXPR:
8074 case PREDECREMENT_EXPR:
8075 non_constant_p = unary_operator == PREINCREMENT_EXPR
8076 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8077 /* Fall through. */
8078 case NEGATE_EXPR:
8079 /* Immediately fold negation of a constant, unless the constant is 0
8080 (since -0 == 0) or it would overflow. */
8081 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8082 && CONSTANT_CLASS_P (cast_expression)
8083 && !integer_zerop (cast_expression)
8084 && !TREE_OVERFLOW (cast_expression))
8086 tree folded = fold_build1 (unary_operator,
8087 TREE_TYPE (cast_expression),
8088 cast_expression);
8089 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8091 expression = cp_expr (folded, loc);
8092 break;
8095 /* Fall through. */
8096 case UNARY_PLUS_EXPR:
8097 case TRUTH_NOT_EXPR:
8098 expression = finish_unary_op_expr (loc, unary_operator,
8099 cast_expression, complain);
8100 break;
8102 default:
8103 gcc_unreachable ();
8106 if (non_constant_p != NIC_NONE
8107 && cp_parser_non_integral_constant_expression (parser,
8108 non_constant_p))
8109 expression = error_mark_node;
8111 return expression;
8114 return cp_parser_postfix_expression (parser, address_p, cast_p,
8115 /*member_access_only_p=*/false,
8116 decltype_p,
8117 pidk);
8120 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8121 unary-operator, the corresponding tree code is returned. */
8123 static enum tree_code
8124 cp_parser_unary_operator (cp_token* token)
8126 switch (token->type)
8128 case CPP_MULT:
8129 return INDIRECT_REF;
8131 case CPP_AND:
8132 return ADDR_EXPR;
8134 case CPP_PLUS:
8135 return UNARY_PLUS_EXPR;
8137 case CPP_MINUS:
8138 return NEGATE_EXPR;
8140 case CPP_NOT:
8141 return TRUTH_NOT_EXPR;
8143 case CPP_COMPL:
8144 return BIT_NOT_EXPR;
8146 default:
8147 return ERROR_MARK;
8151 /* Parse a new-expression.
8153 new-expression:
8154 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8155 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8157 Returns a representation of the expression. */
8159 static tree
8160 cp_parser_new_expression (cp_parser* parser)
8162 bool global_scope_p;
8163 vec<tree, va_gc> *placement;
8164 tree type;
8165 vec<tree, va_gc> *initializer;
8166 tree nelts = NULL_TREE;
8167 tree ret;
8169 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8171 /* Look for the optional `::' operator. */
8172 global_scope_p
8173 = (cp_parser_global_scope_opt (parser,
8174 /*current_scope_valid_p=*/false)
8175 != NULL_TREE);
8176 /* Look for the `new' operator. */
8177 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8178 /* There's no easy way to tell a new-placement from the
8179 `( type-id )' construct. */
8180 cp_parser_parse_tentatively (parser);
8181 /* Look for a new-placement. */
8182 placement = cp_parser_new_placement (parser);
8183 /* If that didn't work out, there's no new-placement. */
8184 if (!cp_parser_parse_definitely (parser))
8186 if (placement != NULL)
8187 release_tree_vector (placement);
8188 placement = NULL;
8191 /* If the next token is a `(', then we have a parenthesized
8192 type-id. */
8193 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8195 cp_token *token;
8196 const char *saved_message = parser->type_definition_forbidden_message;
8198 /* Consume the `('. */
8199 cp_lexer_consume_token (parser->lexer);
8201 /* Parse the type-id. */
8202 parser->type_definition_forbidden_message
8203 = G_("types may not be defined in a new-expression");
8205 type_id_in_expr_sentinel s (parser);
8206 type = cp_parser_type_id (parser);
8208 parser->type_definition_forbidden_message = saved_message;
8210 /* Look for the closing `)'. */
8211 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8212 token = cp_lexer_peek_token (parser->lexer);
8213 /* There should not be a direct-new-declarator in this production,
8214 but GCC used to allowed this, so we check and emit a sensible error
8215 message for this case. */
8216 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8218 error_at (token->location,
8219 "array bound forbidden after parenthesized type-id");
8220 inform (token->location,
8221 "try removing the parentheses around the type-id");
8222 cp_parser_direct_new_declarator (parser);
8225 /* Otherwise, there must be a new-type-id. */
8226 else
8227 type = cp_parser_new_type_id (parser, &nelts);
8229 /* If the next token is a `(' or '{', then we have a new-initializer. */
8230 cp_token *token = cp_lexer_peek_token (parser->lexer);
8231 if (token->type == CPP_OPEN_PAREN
8232 || token->type == CPP_OPEN_BRACE)
8233 initializer = cp_parser_new_initializer (parser);
8234 else
8235 initializer = NULL;
8237 /* A new-expression may not appear in an integral constant
8238 expression. */
8239 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8240 ret = error_mark_node;
8241 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8242 of a new-type-id or type-id of a new-expression, the new-expression shall
8243 contain a new-initializer of the form ( assignment-expression )".
8244 Additionally, consistently with the spirit of DR 1467, we want to accept
8245 'new auto { 2 }' too. */
8246 else if ((ret = type_uses_auto (type))
8247 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8248 && (vec_safe_length (initializer) != 1
8249 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8250 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8252 error_at (token->location,
8253 "initialization of new-expression for type %<auto%> "
8254 "requires exactly one element");
8255 ret = error_mark_node;
8257 else
8259 /* Construct a location e.g.:
8260 ptr = new int[100]
8261 ^~~~~~~~~~~~
8262 with caret == start at the start of the "new" token, and the end
8263 at the end of the final token we consumed. */
8264 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8265 location_t end_loc = get_finish (end_tok->location);
8266 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8268 /* Create a representation of the new-expression. */
8269 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8270 tf_warning_or_error);
8271 protected_set_expr_location (ret, combined_loc);
8274 if (placement != NULL)
8275 release_tree_vector (placement);
8276 if (initializer != NULL)
8277 release_tree_vector (initializer);
8279 return ret;
8282 /* Parse a new-placement.
8284 new-placement:
8285 ( expression-list )
8287 Returns the same representation as for an expression-list. */
8289 static vec<tree, va_gc> *
8290 cp_parser_new_placement (cp_parser* parser)
8292 vec<tree, va_gc> *expression_list;
8294 /* Parse the expression-list. */
8295 expression_list = (cp_parser_parenthesized_expression_list
8296 (parser, non_attr, /*cast_p=*/false,
8297 /*allow_expansion_p=*/true,
8298 /*non_constant_p=*/NULL));
8300 if (expression_list && expression_list->is_empty ())
8301 error ("expected expression-list or type-id");
8303 return expression_list;
8306 /* Parse a new-type-id.
8308 new-type-id:
8309 type-specifier-seq new-declarator [opt]
8311 Returns the TYPE allocated. If the new-type-id indicates an array
8312 type, *NELTS is set to the number of elements in the last array
8313 bound; the TYPE will not include the last array bound. */
8315 static tree
8316 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8318 cp_decl_specifier_seq type_specifier_seq;
8319 cp_declarator *new_declarator;
8320 cp_declarator *declarator;
8321 cp_declarator *outer_declarator;
8322 const char *saved_message;
8324 /* The type-specifier sequence must not contain type definitions.
8325 (It cannot contain declarations of new types either, but if they
8326 are not definitions we will catch that because they are not
8327 complete.) */
8328 saved_message = parser->type_definition_forbidden_message;
8329 parser->type_definition_forbidden_message
8330 = G_("types may not be defined in a new-type-id");
8331 /* Parse the type-specifier-seq. */
8332 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8333 /*is_trailing_return=*/false,
8334 &type_specifier_seq);
8335 /* Restore the old message. */
8336 parser->type_definition_forbidden_message = saved_message;
8338 if (type_specifier_seq.type == error_mark_node)
8339 return error_mark_node;
8341 /* Parse the new-declarator. */
8342 new_declarator = cp_parser_new_declarator_opt (parser);
8344 /* Determine the number of elements in the last array dimension, if
8345 any. */
8346 *nelts = NULL_TREE;
8347 /* Skip down to the last array dimension. */
8348 declarator = new_declarator;
8349 outer_declarator = NULL;
8350 while (declarator && (declarator->kind == cdk_pointer
8351 || declarator->kind == cdk_ptrmem))
8353 outer_declarator = declarator;
8354 declarator = declarator->declarator;
8356 while (declarator
8357 && declarator->kind == cdk_array
8358 && declarator->declarator
8359 && declarator->declarator->kind == cdk_array)
8361 outer_declarator = declarator;
8362 declarator = declarator->declarator;
8365 if (declarator && declarator->kind == cdk_array)
8367 *nelts = declarator->u.array.bounds;
8368 if (*nelts == error_mark_node)
8369 *nelts = integer_one_node;
8371 if (outer_declarator)
8372 outer_declarator->declarator = declarator->declarator;
8373 else
8374 new_declarator = NULL;
8377 return groktypename (&type_specifier_seq, new_declarator, false);
8380 /* Parse an (optional) new-declarator.
8382 new-declarator:
8383 ptr-operator new-declarator [opt]
8384 direct-new-declarator
8386 Returns the declarator. */
8388 static cp_declarator *
8389 cp_parser_new_declarator_opt (cp_parser* parser)
8391 enum tree_code code;
8392 tree type, std_attributes = NULL_TREE;
8393 cp_cv_quals cv_quals;
8395 /* We don't know if there's a ptr-operator next, or not. */
8396 cp_parser_parse_tentatively (parser);
8397 /* Look for a ptr-operator. */
8398 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8399 /* If that worked, look for more new-declarators. */
8400 if (cp_parser_parse_definitely (parser))
8402 cp_declarator *declarator;
8404 /* Parse another optional declarator. */
8405 declarator = cp_parser_new_declarator_opt (parser);
8407 declarator = cp_parser_make_indirect_declarator
8408 (code, type, cv_quals, declarator, std_attributes);
8410 return declarator;
8413 /* If the next token is a `[', there is a direct-new-declarator. */
8414 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8415 return cp_parser_direct_new_declarator (parser);
8417 return NULL;
8420 /* Parse a direct-new-declarator.
8422 direct-new-declarator:
8423 [ expression ]
8424 direct-new-declarator [constant-expression]
8428 static cp_declarator *
8429 cp_parser_direct_new_declarator (cp_parser* parser)
8431 cp_declarator *declarator = NULL;
8433 while (true)
8435 tree expression;
8436 cp_token *token;
8438 /* Look for the opening `['. */
8439 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8441 token = cp_lexer_peek_token (parser->lexer);
8442 expression = cp_parser_expression (parser);
8443 /* The standard requires that the expression have integral
8444 type. DR 74 adds enumeration types. We believe that the
8445 real intent is that these expressions be handled like the
8446 expression in a `switch' condition, which also allows
8447 classes with a single conversion to integral or
8448 enumeration type. */
8449 if (!processing_template_decl)
8451 expression
8452 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8453 expression,
8454 /*complain=*/true);
8455 if (!expression)
8457 error_at (token->location,
8458 "expression in new-declarator must have integral "
8459 "or enumeration type");
8460 expression = error_mark_node;
8464 /* Look for the closing `]'. */
8465 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8467 /* Add this bound to the declarator. */
8468 declarator = make_array_declarator (declarator, expression);
8470 /* If the next token is not a `[', then there are no more
8471 bounds. */
8472 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8473 break;
8476 return declarator;
8479 /* Parse a new-initializer.
8481 new-initializer:
8482 ( expression-list [opt] )
8483 braced-init-list
8485 Returns a representation of the expression-list. */
8487 static vec<tree, va_gc> *
8488 cp_parser_new_initializer (cp_parser* parser)
8490 vec<tree, va_gc> *expression_list;
8492 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8494 tree t;
8495 bool expr_non_constant_p;
8496 cp_lexer_set_source_position (parser->lexer);
8497 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8498 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8499 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8500 expression_list = make_tree_vector_single (t);
8502 else
8503 expression_list = (cp_parser_parenthesized_expression_list
8504 (parser, non_attr, /*cast_p=*/false,
8505 /*allow_expansion_p=*/true,
8506 /*non_constant_p=*/NULL));
8508 return expression_list;
8511 /* Parse a delete-expression.
8513 delete-expression:
8514 :: [opt] delete cast-expression
8515 :: [opt] delete [ ] cast-expression
8517 Returns a representation of the expression. */
8519 static tree
8520 cp_parser_delete_expression (cp_parser* parser)
8522 bool global_scope_p;
8523 bool array_p;
8524 tree expression;
8526 /* Look for the optional `::' operator. */
8527 global_scope_p
8528 = (cp_parser_global_scope_opt (parser,
8529 /*current_scope_valid_p=*/false)
8530 != NULL_TREE);
8531 /* Look for the `delete' keyword. */
8532 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8533 /* See if the array syntax is in use. */
8534 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8536 /* Consume the `[' token. */
8537 cp_lexer_consume_token (parser->lexer);
8538 /* Look for the `]' token. */
8539 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8540 /* Remember that this is the `[]' construct. */
8541 array_p = true;
8543 else
8544 array_p = false;
8546 /* Parse the cast-expression. */
8547 expression = cp_parser_simple_cast_expression (parser);
8549 /* A delete-expression may not appear in an integral constant
8550 expression. */
8551 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8552 return error_mark_node;
8554 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8555 tf_warning_or_error);
8558 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8559 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8560 0 otherwise. */
8562 static int
8563 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8565 cp_token *token = cp_lexer_peek_token (parser->lexer);
8566 switch (token->type)
8568 case CPP_COMMA:
8569 case CPP_SEMICOLON:
8570 case CPP_QUERY:
8571 case CPP_COLON:
8572 case CPP_CLOSE_SQUARE:
8573 case CPP_CLOSE_PAREN:
8574 case CPP_CLOSE_BRACE:
8575 case CPP_OPEN_BRACE:
8576 case CPP_DOT:
8577 case CPP_DOT_STAR:
8578 case CPP_DEREF:
8579 case CPP_DEREF_STAR:
8580 case CPP_DIV:
8581 case CPP_MOD:
8582 case CPP_LSHIFT:
8583 case CPP_RSHIFT:
8584 case CPP_LESS:
8585 case CPP_GREATER:
8586 case CPP_LESS_EQ:
8587 case CPP_GREATER_EQ:
8588 case CPP_EQ_EQ:
8589 case CPP_NOT_EQ:
8590 case CPP_EQ:
8591 case CPP_MULT_EQ:
8592 case CPP_DIV_EQ:
8593 case CPP_MOD_EQ:
8594 case CPP_PLUS_EQ:
8595 case CPP_MINUS_EQ:
8596 case CPP_RSHIFT_EQ:
8597 case CPP_LSHIFT_EQ:
8598 case CPP_AND_EQ:
8599 case CPP_XOR_EQ:
8600 case CPP_OR_EQ:
8601 case CPP_XOR:
8602 case CPP_OR:
8603 case CPP_OR_OR:
8604 case CPP_EOF:
8605 case CPP_ELLIPSIS:
8606 return 0;
8608 case CPP_OPEN_PAREN:
8609 /* In ((type ()) () the last () isn't a valid cast-expression,
8610 so the whole must be parsed as postfix-expression. */
8611 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8612 != CPP_CLOSE_PAREN;
8614 case CPP_OPEN_SQUARE:
8615 /* '[' may start a primary-expression in obj-c++ and in C++11,
8616 as a lambda-expression, eg, '(void)[]{}'. */
8617 if (cxx_dialect >= cxx11)
8618 return -1;
8619 return c_dialect_objc ();
8621 case CPP_PLUS_PLUS:
8622 case CPP_MINUS_MINUS:
8623 /* '++' and '--' may or may not start a cast-expression:
8625 struct T { void operator++(int); };
8626 void f() { (T())++; }
8630 int a;
8631 (int)++a; */
8632 return -1;
8634 default:
8635 return 1;
8639 /* Parse a cast-expression.
8641 cast-expression:
8642 unary-expression
8643 ( type-id ) cast-expression
8645 ADDRESS_P is true iff the unary-expression is appearing as the
8646 operand of the `&' operator. CAST_P is true if this expression is
8647 the target of a cast.
8649 Returns a representation of the expression. */
8651 static cp_expr
8652 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8653 bool decltype_p, cp_id_kind * pidk)
8655 /* If it's a `(', then we might be looking at a cast. */
8656 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8658 tree type = NULL_TREE;
8659 cp_expr expr (NULL_TREE);
8660 int cast_expression = 0;
8661 const char *saved_message;
8663 /* There's no way to know yet whether or not this is a cast.
8664 For example, `(int (3))' is a unary-expression, while `(int)
8665 3' is a cast. So, we resort to parsing tentatively. */
8666 cp_parser_parse_tentatively (parser);
8667 /* Types may not be defined in a cast. */
8668 saved_message = parser->type_definition_forbidden_message;
8669 parser->type_definition_forbidden_message
8670 = G_("types may not be defined in casts");
8671 /* Consume the `('. */
8672 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8673 location_t open_paren_loc = open_paren->location;
8675 /* A very tricky bit is that `(struct S) { 3 }' is a
8676 compound-literal (which we permit in C++ as an extension).
8677 But, that construct is not a cast-expression -- it is a
8678 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8679 is legal; if the compound-literal were a cast-expression,
8680 you'd need an extra set of parentheses.) But, if we parse
8681 the type-id, and it happens to be a class-specifier, then we
8682 will commit to the parse at that point, because we cannot
8683 undo the action that is done when creating a new class. So,
8684 then we cannot back up and do a postfix-expression.
8686 Another tricky case is the following (c++/29234):
8688 struct S { void operator () (); };
8690 void foo ()
8692 ( S()() );
8695 As a type-id we parse the parenthesized S()() as a function
8696 returning a function, groktypename complains and we cannot
8697 back up in this case either.
8699 Therefore, we scan ahead to the closing `)', and check to see
8700 if the tokens after the `)' can start a cast-expression. Otherwise
8701 we are dealing with an unary-expression, a postfix-expression
8702 or something else.
8704 Yet another tricky case, in C++11, is the following (c++/54891):
8706 (void)[]{};
8708 The issue is that usually, besides the case of lambda-expressions,
8709 the parenthesized type-id cannot be followed by '[', and, eg, we
8710 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8711 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8712 we don't commit, we try a cast-expression, then an unary-expression.
8714 Save tokens so that we can put them back. */
8715 cp_lexer_save_tokens (parser->lexer);
8717 /* We may be looking at a cast-expression. */
8718 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8719 /*consume_paren=*/true))
8720 cast_expression
8721 = cp_parser_tokens_start_cast_expression (parser);
8723 /* Roll back the tokens we skipped. */
8724 cp_lexer_rollback_tokens (parser->lexer);
8725 /* If we aren't looking at a cast-expression, simulate an error so
8726 that the call to cp_parser_error_occurred below returns true. */
8727 if (!cast_expression)
8728 cp_parser_simulate_error (parser);
8729 else
8731 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8732 parser->in_type_id_in_expr_p = true;
8733 /* Look for the type-id. */
8734 type = cp_parser_type_id (parser);
8735 /* Look for the closing `)'. */
8736 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8737 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8740 /* Restore the saved message. */
8741 parser->type_definition_forbidden_message = saved_message;
8743 /* At this point this can only be either a cast or a
8744 parenthesized ctor such as `(T ())' that looks like a cast to
8745 function returning T. */
8746 if (!cp_parser_error_occurred (parser))
8748 /* Only commit if the cast-expression doesn't start with
8749 '++', '--', or '[' in C++11. */
8750 if (cast_expression > 0)
8751 cp_parser_commit_to_topmost_tentative_parse (parser);
8753 expr = cp_parser_cast_expression (parser,
8754 /*address_p=*/false,
8755 /*cast_p=*/true,
8756 /*decltype_p=*/false,
8757 pidk);
8759 if (cp_parser_parse_definitely (parser))
8761 /* Warn about old-style casts, if so requested. */
8762 if (warn_old_style_cast
8763 && !in_system_header_at (input_location)
8764 && !VOID_TYPE_P (type)
8765 && current_lang_name != lang_name_c)
8766 warning (OPT_Wold_style_cast,
8767 "use of old-style cast to %qT", type);
8769 /* Only type conversions to integral or enumeration types
8770 can be used in constant-expressions. */
8771 if (!cast_valid_in_integral_constant_expression_p (type)
8772 && cp_parser_non_integral_constant_expression (parser,
8773 NIC_CAST))
8774 return error_mark_node;
8776 /* Perform the cast. */
8777 /* Make a location:
8778 (TYPE) EXPR
8779 ^~~~~~~~~~~
8780 with start==caret at the open paren, extending to the
8781 end of "expr". */
8782 location_t cast_loc = make_location (open_paren_loc,
8783 open_paren_loc,
8784 expr.get_finish ());
8785 expr = build_c_cast (cast_loc, type, expr);
8786 return expr;
8789 else
8790 cp_parser_abort_tentative_parse (parser);
8793 /* If we get here, then it's not a cast, so it must be a
8794 unary-expression. */
8795 return cp_parser_unary_expression (parser, pidk, address_p,
8796 cast_p, decltype_p);
8799 /* Parse a binary expression of the general form:
8801 pm-expression:
8802 cast-expression
8803 pm-expression .* cast-expression
8804 pm-expression ->* cast-expression
8806 multiplicative-expression:
8807 pm-expression
8808 multiplicative-expression * pm-expression
8809 multiplicative-expression / pm-expression
8810 multiplicative-expression % pm-expression
8812 additive-expression:
8813 multiplicative-expression
8814 additive-expression + multiplicative-expression
8815 additive-expression - multiplicative-expression
8817 shift-expression:
8818 additive-expression
8819 shift-expression << additive-expression
8820 shift-expression >> additive-expression
8822 relational-expression:
8823 shift-expression
8824 relational-expression < shift-expression
8825 relational-expression > shift-expression
8826 relational-expression <= shift-expression
8827 relational-expression >= shift-expression
8829 GNU Extension:
8831 relational-expression:
8832 relational-expression <? shift-expression
8833 relational-expression >? shift-expression
8835 equality-expression:
8836 relational-expression
8837 equality-expression == relational-expression
8838 equality-expression != relational-expression
8840 and-expression:
8841 equality-expression
8842 and-expression & equality-expression
8844 exclusive-or-expression:
8845 and-expression
8846 exclusive-or-expression ^ and-expression
8848 inclusive-or-expression:
8849 exclusive-or-expression
8850 inclusive-or-expression | exclusive-or-expression
8852 logical-and-expression:
8853 inclusive-or-expression
8854 logical-and-expression && inclusive-or-expression
8856 logical-or-expression:
8857 logical-and-expression
8858 logical-or-expression || logical-and-expression
8860 All these are implemented with a single function like:
8862 binary-expression:
8863 simple-cast-expression
8864 binary-expression <token> binary-expression
8866 CAST_P is true if this expression is the target of a cast.
8868 The binops_by_token map is used to get the tree codes for each <token> type.
8869 binary-expressions are associated according to a precedence table. */
8871 #define TOKEN_PRECEDENCE(token) \
8872 (((token->type == CPP_GREATER \
8873 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8874 && !parser->greater_than_is_operator_p) \
8875 ? PREC_NOT_OPERATOR \
8876 : binops_by_token[token->type].prec)
8878 static cp_expr
8879 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8880 bool no_toplevel_fold_p,
8881 bool decltype_p,
8882 enum cp_parser_prec prec,
8883 cp_id_kind * pidk)
8885 cp_parser_expression_stack stack;
8886 cp_parser_expression_stack_entry *sp = &stack[0];
8887 cp_parser_expression_stack_entry current;
8888 cp_expr rhs;
8889 cp_token *token;
8890 enum tree_code rhs_type;
8891 enum cp_parser_prec new_prec, lookahead_prec;
8892 tree overload;
8894 /* Parse the first expression. */
8895 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8896 ? TRUTH_NOT_EXPR : ERROR_MARK);
8897 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8898 cast_p, decltype_p, pidk);
8899 current.prec = prec;
8901 if (cp_parser_error_occurred (parser))
8902 return error_mark_node;
8904 for (;;)
8906 /* Get an operator token. */
8907 token = cp_lexer_peek_token (parser->lexer);
8909 if (warn_cxx11_compat
8910 && token->type == CPP_RSHIFT
8911 && !parser->greater_than_is_operator_p)
8913 if (warning_at (token->location, OPT_Wc__11_compat,
8914 "%<>>%> operator is treated"
8915 " as two right angle brackets in C++11"))
8916 inform (token->location,
8917 "suggest parentheses around %<>>%> expression");
8920 new_prec = TOKEN_PRECEDENCE (token);
8921 if (new_prec != PREC_NOT_OPERATOR
8922 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8923 /* This is a fold-expression; handle it later. */
8924 new_prec = PREC_NOT_OPERATOR;
8926 /* Popping an entry off the stack means we completed a subexpression:
8927 - either we found a token which is not an operator (`>' where it is not
8928 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8929 will happen repeatedly;
8930 - or, we found an operator which has lower priority. This is the case
8931 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8932 parsing `3 * 4'. */
8933 if (new_prec <= current.prec)
8935 if (sp == stack)
8936 break;
8937 else
8938 goto pop;
8941 get_rhs:
8942 current.tree_type = binops_by_token[token->type].tree_type;
8943 current.loc = token->location;
8945 /* We used the operator token. */
8946 cp_lexer_consume_token (parser->lexer);
8948 /* For "false && x" or "true || x", x will never be executed;
8949 disable warnings while evaluating it. */
8950 if (current.tree_type == TRUTH_ANDIF_EXPR)
8951 c_inhibit_evaluation_warnings +=
8952 cp_fully_fold (current.lhs) == truthvalue_false_node;
8953 else if (current.tree_type == TRUTH_ORIF_EXPR)
8954 c_inhibit_evaluation_warnings +=
8955 cp_fully_fold (current.lhs) == truthvalue_true_node;
8957 /* Extract another operand. It may be the RHS of this expression
8958 or the LHS of a new, higher priority expression. */
8959 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8960 ? TRUTH_NOT_EXPR : ERROR_MARK);
8961 rhs = cp_parser_simple_cast_expression (parser);
8963 /* Get another operator token. Look up its precedence to avoid
8964 building a useless (immediately popped) stack entry for common
8965 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8966 token = cp_lexer_peek_token (parser->lexer);
8967 lookahead_prec = TOKEN_PRECEDENCE (token);
8968 if (lookahead_prec != PREC_NOT_OPERATOR
8969 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8970 lookahead_prec = PREC_NOT_OPERATOR;
8971 if (lookahead_prec > new_prec)
8973 /* ... and prepare to parse the RHS of the new, higher priority
8974 expression. Since precedence levels on the stack are
8975 monotonically increasing, we do not have to care about
8976 stack overflows. */
8977 *sp = current;
8978 ++sp;
8979 current.lhs = rhs;
8980 current.lhs_type = rhs_type;
8981 current.prec = new_prec;
8982 new_prec = lookahead_prec;
8983 goto get_rhs;
8985 pop:
8986 lookahead_prec = new_prec;
8987 /* If the stack is not empty, we have parsed into LHS the right side
8988 (`4' in the example above) of an expression we had suspended.
8989 We can use the information on the stack to recover the LHS (`3')
8990 from the stack together with the tree code (`MULT_EXPR'), and
8991 the precedence of the higher level subexpression
8992 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8993 which will be used to actually build the additive expression. */
8994 rhs = current.lhs;
8995 rhs_type = current.lhs_type;
8996 --sp;
8997 current = *sp;
9000 /* Undo the disabling of warnings done above. */
9001 if (current.tree_type == TRUTH_ANDIF_EXPR)
9002 c_inhibit_evaluation_warnings -=
9003 cp_fully_fold (current.lhs) == truthvalue_false_node;
9004 else if (current.tree_type == TRUTH_ORIF_EXPR)
9005 c_inhibit_evaluation_warnings -=
9006 cp_fully_fold (current.lhs) == truthvalue_true_node;
9008 if (warn_logical_not_paren
9009 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9010 && current.lhs_type == TRUTH_NOT_EXPR
9011 /* Avoid warning for !!x == y. */
9012 && (TREE_CODE (current.lhs) != NE_EXPR
9013 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9014 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9015 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9016 /* Avoid warning for !b == y where b is boolean. */
9017 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9018 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9019 != BOOLEAN_TYPE))))
9020 /* Avoid warning for !!b == y where b is boolean. */
9021 && (!DECL_P (current.lhs)
9022 || TREE_TYPE (current.lhs) == NULL_TREE
9023 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9024 warn_logical_not_parentheses (current.loc, current.tree_type,
9025 current.lhs, maybe_constant_value (rhs));
9027 overload = NULL;
9029 location_t combined_loc = make_location (current.loc,
9030 current.lhs.get_start (),
9031 rhs.get_finish ());
9033 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9034 ERROR_MARK for everything that is not a binary expression.
9035 This makes warn_about_parentheses miss some warnings that
9036 involve unary operators. For unary expressions we should
9037 pass the correct tree_code unless the unary expression was
9038 surrounded by parentheses.
9040 if (no_toplevel_fold_p
9041 && lookahead_prec <= current.prec
9042 && sp == stack)
9043 current.lhs = build2_loc (combined_loc,
9044 current.tree_type,
9045 TREE_CODE_CLASS (current.tree_type)
9046 == tcc_comparison
9047 ? boolean_type_node : TREE_TYPE (current.lhs),
9048 current.lhs, rhs);
9049 else
9051 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9052 current.lhs, current.lhs_type,
9053 rhs, rhs_type, &overload,
9054 complain_flags (decltype_p));
9055 /* TODO: build_x_binary_op doesn't always honor the location. */
9056 current.lhs.set_location (combined_loc);
9058 current.lhs_type = current.tree_type;
9060 /* If the binary operator required the use of an overloaded operator,
9061 then this expression cannot be an integral constant-expression.
9062 An overloaded operator can be used even if both operands are
9063 otherwise permissible in an integral constant-expression if at
9064 least one of the operands is of enumeration type. */
9066 if (overload
9067 && cp_parser_non_integral_constant_expression (parser,
9068 NIC_OVERLOADED))
9069 return error_mark_node;
9072 return current.lhs;
9075 static cp_expr
9076 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9077 bool no_toplevel_fold_p,
9078 enum cp_parser_prec prec,
9079 cp_id_kind * pidk)
9081 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9082 /*decltype*/false, prec, pidk);
9085 /* Parse the `? expression : assignment-expression' part of a
9086 conditional-expression. The LOGICAL_OR_EXPR is the
9087 logical-or-expression that started the conditional-expression.
9088 Returns a representation of the entire conditional-expression.
9090 This routine is used by cp_parser_assignment_expression.
9092 ? expression : assignment-expression
9094 GNU Extensions:
9096 ? : assignment-expression */
9098 static tree
9099 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9101 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9102 cp_expr assignment_expr;
9103 struct cp_token *token;
9104 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9106 /* Consume the `?' token. */
9107 cp_lexer_consume_token (parser->lexer);
9108 token = cp_lexer_peek_token (parser->lexer);
9109 if (cp_parser_allow_gnu_extensions_p (parser)
9110 && token->type == CPP_COLON)
9112 pedwarn (token->location, OPT_Wpedantic,
9113 "ISO C++ does not allow ?: with omitted middle operand");
9114 /* Implicit true clause. */
9115 expr = NULL_TREE;
9116 c_inhibit_evaluation_warnings +=
9117 folded_logical_or_expr == truthvalue_true_node;
9118 warn_for_omitted_condop (token->location, logical_or_expr);
9120 else
9122 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9123 parser->colon_corrects_to_scope_p = false;
9124 /* Parse the expression. */
9125 c_inhibit_evaluation_warnings +=
9126 folded_logical_or_expr == truthvalue_false_node;
9127 expr = cp_parser_expression (parser);
9128 c_inhibit_evaluation_warnings +=
9129 ((folded_logical_or_expr == truthvalue_true_node)
9130 - (folded_logical_or_expr == truthvalue_false_node));
9131 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9134 /* The next token should be a `:'. */
9135 cp_parser_require (parser, CPP_COLON, RT_COLON);
9136 /* Parse the assignment-expression. */
9137 assignment_expr = cp_parser_assignment_expression (parser);
9138 c_inhibit_evaluation_warnings -=
9139 folded_logical_or_expr == truthvalue_true_node;
9141 /* Make a location:
9142 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9143 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9144 with the caret at the "?", ranging from the start of
9145 the logical_or_expr to the end of the assignment_expr. */
9146 loc = make_location (loc,
9147 logical_or_expr.get_start (),
9148 assignment_expr.get_finish ());
9150 /* Build the conditional-expression. */
9151 return build_x_conditional_expr (loc, logical_or_expr,
9152 expr,
9153 assignment_expr,
9154 tf_warning_or_error);
9157 /* Parse an assignment-expression.
9159 assignment-expression:
9160 conditional-expression
9161 logical-or-expression assignment-operator assignment_expression
9162 throw-expression
9164 CAST_P is true if this expression is the target of a cast.
9165 DECLTYPE_P is true if this expression is the operand of decltype.
9167 Returns a representation for the expression. */
9169 static cp_expr
9170 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9171 bool cast_p, bool decltype_p)
9173 cp_expr expr;
9175 /* If the next token is the `throw' keyword, then we're looking at
9176 a throw-expression. */
9177 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9178 expr = cp_parser_throw_expression (parser);
9179 /* Otherwise, it must be that we are looking at a
9180 logical-or-expression. */
9181 else
9183 /* Parse the binary expressions (logical-or-expression). */
9184 expr = cp_parser_binary_expression (parser, cast_p, false,
9185 decltype_p,
9186 PREC_NOT_OPERATOR, pidk);
9187 /* If the next token is a `?' then we're actually looking at a
9188 conditional-expression. */
9189 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9190 return cp_parser_question_colon_clause (parser, expr);
9191 else
9193 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9195 /* If it's an assignment-operator, we're using the second
9196 production. */
9197 enum tree_code assignment_operator
9198 = cp_parser_assignment_operator_opt (parser);
9199 if (assignment_operator != ERROR_MARK)
9201 bool non_constant_p;
9203 /* Parse the right-hand side of the assignment. */
9204 cp_expr rhs = cp_parser_initializer_clause (parser,
9205 &non_constant_p);
9207 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9208 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9210 /* An assignment may not appear in a
9211 constant-expression. */
9212 if (cp_parser_non_integral_constant_expression (parser,
9213 NIC_ASSIGNMENT))
9214 return error_mark_node;
9215 /* Build the assignment expression. Its default
9216 location:
9217 LHS = RHS
9218 ~~~~^~~~~
9219 is the location of the '=' token as the
9220 caret, ranging from the start of the lhs to the
9221 end of the rhs. */
9222 loc = make_location (loc,
9223 expr.get_start (),
9224 rhs.get_finish ());
9225 expr = build_x_modify_expr (loc, expr,
9226 assignment_operator,
9227 rhs,
9228 complain_flags (decltype_p));
9229 /* TODO: build_x_modify_expr doesn't honor the location,
9230 so we must set it here. */
9231 expr.set_location (loc);
9236 return expr;
9239 /* Parse an (optional) assignment-operator.
9241 assignment-operator: one of
9242 = *= /= %= += -= >>= <<= &= ^= |=
9244 GNU Extension:
9246 assignment-operator: one of
9247 <?= >?=
9249 If the next token is an assignment operator, the corresponding tree
9250 code is returned, and the token is consumed. For example, for
9251 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9252 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9253 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9254 operator, ERROR_MARK is returned. */
9256 static enum tree_code
9257 cp_parser_assignment_operator_opt (cp_parser* parser)
9259 enum tree_code op;
9260 cp_token *token;
9262 /* Peek at the next token. */
9263 token = cp_lexer_peek_token (parser->lexer);
9265 switch (token->type)
9267 case CPP_EQ:
9268 op = NOP_EXPR;
9269 break;
9271 case CPP_MULT_EQ:
9272 op = MULT_EXPR;
9273 break;
9275 case CPP_DIV_EQ:
9276 op = TRUNC_DIV_EXPR;
9277 break;
9279 case CPP_MOD_EQ:
9280 op = TRUNC_MOD_EXPR;
9281 break;
9283 case CPP_PLUS_EQ:
9284 op = PLUS_EXPR;
9285 break;
9287 case CPP_MINUS_EQ:
9288 op = MINUS_EXPR;
9289 break;
9291 case CPP_RSHIFT_EQ:
9292 op = RSHIFT_EXPR;
9293 break;
9295 case CPP_LSHIFT_EQ:
9296 op = LSHIFT_EXPR;
9297 break;
9299 case CPP_AND_EQ:
9300 op = BIT_AND_EXPR;
9301 break;
9303 case CPP_XOR_EQ:
9304 op = BIT_XOR_EXPR;
9305 break;
9307 case CPP_OR_EQ:
9308 op = BIT_IOR_EXPR;
9309 break;
9311 default:
9312 /* Nothing else is an assignment operator. */
9313 op = ERROR_MARK;
9316 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9317 if (op != ERROR_MARK
9318 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9319 op = ERROR_MARK;
9321 /* If it was an assignment operator, consume it. */
9322 if (op != ERROR_MARK)
9323 cp_lexer_consume_token (parser->lexer);
9325 return op;
9328 /* Parse an expression.
9330 expression:
9331 assignment-expression
9332 expression , assignment-expression
9334 CAST_P is true if this expression is the target of a cast.
9335 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9336 except possibly parenthesized or on the RHS of a comma (N3276).
9338 Returns a representation of the expression. */
9340 static cp_expr
9341 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9342 bool cast_p, bool decltype_p)
9344 cp_expr expression = NULL_TREE;
9345 location_t loc = UNKNOWN_LOCATION;
9347 while (true)
9349 cp_expr assignment_expression;
9351 /* Parse the next assignment-expression. */
9352 assignment_expression
9353 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9355 /* We don't create a temporary for a call that is the immediate operand
9356 of decltype or on the RHS of a comma. But when we see a comma, we
9357 need to create a temporary for a call on the LHS. */
9358 if (decltype_p && !processing_template_decl
9359 && TREE_CODE (assignment_expression) == CALL_EXPR
9360 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9361 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9362 assignment_expression
9363 = build_cplus_new (TREE_TYPE (assignment_expression),
9364 assignment_expression, tf_warning_or_error);
9366 /* If this is the first assignment-expression, we can just
9367 save it away. */
9368 if (!expression)
9369 expression = assignment_expression;
9370 else
9372 /* Create a location with caret at the comma, ranging
9373 from the start of the LHS to the end of the RHS. */
9374 loc = make_location (loc,
9375 expression.get_start (),
9376 assignment_expression.get_finish ());
9377 expression = build_x_compound_expr (loc, expression,
9378 assignment_expression,
9379 complain_flags (decltype_p));
9380 expression.set_location (loc);
9382 /* If the next token is not a comma, or we're in a fold-expression, then
9383 we are done with the expression. */
9384 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9385 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9386 break;
9387 /* Consume the `,'. */
9388 loc = cp_lexer_peek_token (parser->lexer)->location;
9389 cp_lexer_consume_token (parser->lexer);
9390 /* A comma operator cannot appear in a constant-expression. */
9391 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9392 expression = error_mark_node;
9395 return expression;
9398 /* Parse a constant-expression.
9400 constant-expression:
9401 conditional-expression
9403 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9404 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9405 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9406 is false, NON_CONSTANT_P should be NULL. */
9408 static cp_expr
9409 cp_parser_constant_expression (cp_parser* parser,
9410 bool allow_non_constant_p,
9411 bool *non_constant_p)
9413 bool saved_integral_constant_expression_p;
9414 bool saved_allow_non_integral_constant_expression_p;
9415 bool saved_non_integral_constant_expression_p;
9416 cp_expr expression;
9418 /* It might seem that we could simply parse the
9419 conditional-expression, and then check to see if it were
9420 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9421 one that the compiler can figure out is constant, possibly after
9422 doing some simplifications or optimizations. The standard has a
9423 precise definition of constant-expression, and we must honor
9424 that, even though it is somewhat more restrictive.
9426 For example:
9428 int i[(2, 3)];
9430 is not a legal declaration, because `(2, 3)' is not a
9431 constant-expression. The `,' operator is forbidden in a
9432 constant-expression. However, GCC's constant-folding machinery
9433 will fold this operation to an INTEGER_CST for `3'. */
9435 /* Save the old settings. */
9436 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9437 saved_allow_non_integral_constant_expression_p
9438 = parser->allow_non_integral_constant_expression_p;
9439 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9440 /* We are now parsing a constant-expression. */
9441 parser->integral_constant_expression_p = true;
9442 parser->allow_non_integral_constant_expression_p
9443 = (allow_non_constant_p || cxx_dialect >= cxx11);
9444 parser->non_integral_constant_expression_p = false;
9445 /* Although the grammar says "conditional-expression", we parse an
9446 "assignment-expression", which also permits "throw-expression"
9447 and the use of assignment operators. In the case that
9448 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9449 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9450 actually essential that we look for an assignment-expression.
9451 For example, cp_parser_initializer_clauses uses this function to
9452 determine whether a particular assignment-expression is in fact
9453 constant. */
9454 expression = cp_parser_assignment_expression (parser);
9455 /* Restore the old settings. */
9456 parser->integral_constant_expression_p
9457 = saved_integral_constant_expression_p;
9458 parser->allow_non_integral_constant_expression_p
9459 = saved_allow_non_integral_constant_expression_p;
9460 if (cxx_dialect >= cxx11)
9462 /* Require an rvalue constant expression here; that's what our
9463 callers expect. Reference constant expressions are handled
9464 separately in e.g. cp_parser_template_argument. */
9465 bool is_const = potential_rvalue_constant_expression (expression);
9466 parser->non_integral_constant_expression_p = !is_const;
9467 if (!is_const && !allow_non_constant_p)
9468 require_potential_rvalue_constant_expression (expression);
9470 if (allow_non_constant_p)
9471 *non_constant_p = parser->non_integral_constant_expression_p;
9472 parser->non_integral_constant_expression_p
9473 = saved_non_integral_constant_expression_p;
9475 return expression;
9478 /* Parse __builtin_offsetof.
9480 offsetof-expression:
9481 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9483 offsetof-member-designator:
9484 id-expression
9485 | offsetof-member-designator "." id-expression
9486 | offsetof-member-designator "[" expression "]"
9487 | offsetof-member-designator "->" id-expression */
9489 static cp_expr
9490 cp_parser_builtin_offsetof (cp_parser *parser)
9492 int save_ice_p, save_non_ice_p;
9493 tree type;
9494 cp_expr expr;
9495 cp_id_kind dummy;
9496 cp_token *token;
9497 location_t finish_loc;
9499 /* We're about to accept non-integral-constant things, but will
9500 definitely yield an integral constant expression. Save and
9501 restore these values around our local parsing. */
9502 save_ice_p = parser->integral_constant_expression_p;
9503 save_non_ice_p = parser->non_integral_constant_expression_p;
9505 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9507 /* Consume the "__builtin_offsetof" token. */
9508 cp_lexer_consume_token (parser->lexer);
9509 /* Consume the opening `('. */
9510 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9511 /* Parse the type-id. */
9512 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9513 type = cp_parser_type_id (parser);
9514 /* Look for the `,'. */
9515 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9516 token = cp_lexer_peek_token (parser->lexer);
9518 /* Build the (type *)null that begins the traditional offsetof macro. */
9519 tree object_ptr
9520 = build_static_cast (build_pointer_type (type), null_pointer_node,
9521 tf_warning_or_error);
9523 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9524 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9525 true, &dummy, token->location);
9526 while (true)
9528 token = cp_lexer_peek_token (parser->lexer);
9529 switch (token->type)
9531 case CPP_OPEN_SQUARE:
9532 /* offsetof-member-designator "[" expression "]" */
9533 expr = cp_parser_postfix_open_square_expression (parser, expr,
9534 true, false);
9535 break;
9537 case CPP_DEREF:
9538 /* offsetof-member-designator "->" identifier */
9539 expr = grok_array_decl (token->location, expr,
9540 integer_zero_node, false);
9541 /* FALLTHRU */
9543 case CPP_DOT:
9544 /* offsetof-member-designator "." identifier */
9545 cp_lexer_consume_token (parser->lexer);
9546 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9547 expr, true, &dummy,
9548 token->location);
9549 break;
9551 case CPP_CLOSE_PAREN:
9552 /* Consume the ")" token. */
9553 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9554 cp_lexer_consume_token (parser->lexer);
9555 goto success;
9557 default:
9558 /* Error. We know the following require will fail, but
9559 that gives the proper error message. */
9560 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9561 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9562 expr = error_mark_node;
9563 goto failure;
9567 success:
9568 /* Make a location of the form:
9569 __builtin_offsetof (struct s, f)
9570 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9571 with caret at the type-id, ranging from the start of the
9572 "_builtin_offsetof" token to the close paren. */
9573 loc = make_location (loc, start_loc, finish_loc);
9574 /* The result will be an INTEGER_CST, so we need to explicitly
9575 preserve the location. */
9576 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9578 failure:
9579 parser->integral_constant_expression_p = save_ice_p;
9580 parser->non_integral_constant_expression_p = save_non_ice_p;
9582 return expr;
9585 /* Parse a trait expression.
9587 Returns a representation of the expression, the underlying type
9588 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9590 static tree
9591 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9593 cp_trait_kind kind;
9594 tree type1, type2 = NULL_TREE;
9595 bool binary = false;
9596 bool variadic = false;
9598 switch (keyword)
9600 case RID_HAS_NOTHROW_ASSIGN:
9601 kind = CPTK_HAS_NOTHROW_ASSIGN;
9602 break;
9603 case RID_HAS_NOTHROW_CONSTRUCTOR:
9604 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9605 break;
9606 case RID_HAS_NOTHROW_COPY:
9607 kind = CPTK_HAS_NOTHROW_COPY;
9608 break;
9609 case RID_HAS_TRIVIAL_ASSIGN:
9610 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9611 break;
9612 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9613 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9614 break;
9615 case RID_HAS_TRIVIAL_COPY:
9616 kind = CPTK_HAS_TRIVIAL_COPY;
9617 break;
9618 case RID_HAS_TRIVIAL_DESTRUCTOR:
9619 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9620 break;
9621 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9622 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9623 break;
9624 case RID_HAS_VIRTUAL_DESTRUCTOR:
9625 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9626 break;
9627 case RID_IS_ABSTRACT:
9628 kind = CPTK_IS_ABSTRACT;
9629 break;
9630 case RID_IS_AGGREGATE:
9631 kind = CPTK_IS_AGGREGATE;
9632 break;
9633 case RID_IS_BASE_OF:
9634 kind = CPTK_IS_BASE_OF;
9635 binary = true;
9636 break;
9637 case RID_IS_CLASS:
9638 kind = CPTK_IS_CLASS;
9639 break;
9640 case RID_IS_EMPTY:
9641 kind = CPTK_IS_EMPTY;
9642 break;
9643 case RID_IS_ENUM:
9644 kind = CPTK_IS_ENUM;
9645 break;
9646 case RID_IS_FINAL:
9647 kind = CPTK_IS_FINAL;
9648 break;
9649 case RID_IS_LITERAL_TYPE:
9650 kind = CPTK_IS_LITERAL_TYPE;
9651 break;
9652 case RID_IS_POD:
9653 kind = CPTK_IS_POD;
9654 break;
9655 case RID_IS_POLYMORPHIC:
9656 kind = CPTK_IS_POLYMORPHIC;
9657 break;
9658 case RID_IS_SAME_AS:
9659 kind = CPTK_IS_SAME_AS;
9660 binary = true;
9661 break;
9662 case RID_IS_STD_LAYOUT:
9663 kind = CPTK_IS_STD_LAYOUT;
9664 break;
9665 case RID_IS_TRIVIAL:
9666 kind = CPTK_IS_TRIVIAL;
9667 break;
9668 case RID_IS_TRIVIALLY_ASSIGNABLE:
9669 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9670 binary = true;
9671 break;
9672 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9673 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9674 variadic = true;
9675 break;
9676 case RID_IS_TRIVIALLY_COPYABLE:
9677 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9678 break;
9679 case RID_IS_UNION:
9680 kind = CPTK_IS_UNION;
9681 break;
9682 case RID_UNDERLYING_TYPE:
9683 kind = CPTK_UNDERLYING_TYPE;
9684 break;
9685 case RID_BASES:
9686 kind = CPTK_BASES;
9687 break;
9688 case RID_DIRECT_BASES:
9689 kind = CPTK_DIRECT_BASES;
9690 break;
9691 case RID_IS_ASSIGNABLE:
9692 kind = CPTK_IS_ASSIGNABLE;
9693 binary = true;
9694 break;
9695 case RID_IS_CONSTRUCTIBLE:
9696 kind = CPTK_IS_CONSTRUCTIBLE;
9697 variadic = true;
9698 break;
9699 default:
9700 gcc_unreachable ();
9703 /* Consume the token. */
9704 cp_lexer_consume_token (parser->lexer);
9706 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9709 type_id_in_expr_sentinel s (parser);
9710 type1 = cp_parser_type_id (parser);
9713 if (type1 == error_mark_node)
9714 return error_mark_node;
9716 if (binary)
9718 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9721 type_id_in_expr_sentinel s (parser);
9722 type2 = cp_parser_type_id (parser);
9725 if (type2 == error_mark_node)
9726 return error_mark_node;
9728 else if (variadic)
9730 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9732 cp_lexer_consume_token (parser->lexer);
9733 tree elt = cp_parser_type_id (parser);
9734 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9736 cp_lexer_consume_token (parser->lexer);
9737 elt = make_pack_expansion (elt);
9739 if (elt == error_mark_node)
9740 return error_mark_node;
9741 type2 = tree_cons (NULL_TREE, elt, type2);
9745 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9747 /* Complete the trait expression, which may mean either processing
9748 the trait expr now or saving it for template instantiation. */
9749 switch (kind)
9751 case CPTK_UNDERLYING_TYPE:
9752 return finish_underlying_type (type1);
9753 case CPTK_BASES:
9754 return finish_bases (type1, false);
9755 case CPTK_DIRECT_BASES:
9756 return finish_bases (type1, true);
9757 default:
9758 return finish_trait_expr (kind, type1, type2);
9762 /* Lambdas that appear in variable initializer or default argument scope
9763 get that in their mangling, so we need to record it. We might as well
9764 use the count for function and namespace scopes as well. */
9765 static GTY(()) tree lambda_scope;
9766 static GTY(()) int lambda_count;
9767 struct GTY(()) tree_int
9769 tree t;
9770 int i;
9772 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9774 static void
9775 start_lambda_scope (tree decl)
9777 tree_int ti;
9778 gcc_assert (decl);
9779 /* Once we're inside a function, we ignore other scopes and just push
9780 the function again so that popping works properly. */
9781 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9782 decl = current_function_decl;
9783 ti.t = lambda_scope;
9784 ti.i = lambda_count;
9785 vec_safe_push (lambda_scope_stack, ti);
9786 if (lambda_scope != decl)
9788 /* Don't reset the count if we're still in the same function. */
9789 lambda_scope = decl;
9790 lambda_count = 0;
9794 static void
9795 record_lambda_scope (tree lambda)
9797 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9798 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9801 static void
9802 finish_lambda_scope (void)
9804 tree_int *p = &lambda_scope_stack->last ();
9805 if (lambda_scope != p->t)
9807 lambda_scope = p->t;
9808 lambda_count = p->i;
9810 lambda_scope_stack->pop ();
9813 /* Parse a lambda expression.
9815 lambda-expression:
9816 lambda-introducer lambda-declarator [opt] compound-statement
9818 Returns a representation of the expression. */
9820 static cp_expr
9821 cp_parser_lambda_expression (cp_parser* parser)
9823 tree lambda_expr = build_lambda_expr ();
9824 tree type;
9825 bool ok = true;
9826 cp_token *token = cp_lexer_peek_token (parser->lexer);
9827 cp_token_position start = 0;
9829 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9831 if (cp_unevaluated_operand)
9833 if (!token->error_reported)
9835 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9836 "lambda-expression in unevaluated context");
9837 token->error_reported = true;
9839 ok = false;
9841 else if (parser->in_template_argument_list_p)
9843 if (!token->error_reported)
9845 error_at (token->location, "lambda-expression in template-argument");
9846 token->error_reported = true;
9848 ok = false;
9851 /* We may be in the middle of deferred access check. Disable
9852 it now. */
9853 push_deferring_access_checks (dk_no_deferred);
9855 cp_parser_lambda_introducer (parser, lambda_expr);
9857 type = begin_lambda_type (lambda_expr);
9858 if (type == error_mark_node)
9859 return error_mark_node;
9861 record_lambda_scope (lambda_expr);
9863 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9864 determine_visibility (TYPE_NAME (type));
9866 /* Now that we've started the type, add the capture fields for any
9867 explicit captures. */
9868 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9871 /* Inside the class, surrounding template-parameter-lists do not apply. */
9872 unsigned int saved_num_template_parameter_lists
9873 = parser->num_template_parameter_lists;
9874 unsigned char in_statement = parser->in_statement;
9875 bool in_switch_statement_p = parser->in_switch_statement_p;
9876 bool fully_implicit_function_template_p
9877 = parser->fully_implicit_function_template_p;
9878 tree implicit_template_parms = parser->implicit_template_parms;
9879 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9880 bool auto_is_implicit_function_template_parm_p
9881 = parser->auto_is_implicit_function_template_parm_p;
9883 parser->num_template_parameter_lists = 0;
9884 parser->in_statement = 0;
9885 parser->in_switch_statement_p = false;
9886 parser->fully_implicit_function_template_p = false;
9887 parser->implicit_template_parms = 0;
9888 parser->implicit_template_scope = 0;
9889 parser->auto_is_implicit_function_template_parm_p = false;
9891 /* By virtue of defining a local class, a lambda expression has access to
9892 the private variables of enclosing classes. */
9894 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9896 if (ok && cp_parser_error_occurred (parser))
9897 ok = false;
9899 if (ok)
9901 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9902 && cp_parser_start_tentative_firewall (parser))
9903 start = token;
9904 cp_parser_lambda_body (parser, lambda_expr);
9906 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9908 if (cp_parser_skip_to_closing_brace (parser))
9909 cp_lexer_consume_token (parser->lexer);
9912 /* The capture list was built up in reverse order; fix that now. */
9913 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9914 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9916 if (ok)
9917 maybe_add_lambda_conv_op (type);
9919 type = finish_struct (type, /*attributes=*/NULL_TREE);
9921 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9922 parser->in_statement = in_statement;
9923 parser->in_switch_statement_p = in_switch_statement_p;
9924 parser->fully_implicit_function_template_p
9925 = fully_implicit_function_template_p;
9926 parser->implicit_template_parms = implicit_template_parms;
9927 parser->implicit_template_scope = implicit_template_scope;
9928 parser->auto_is_implicit_function_template_parm_p
9929 = auto_is_implicit_function_template_parm_p;
9932 /* This field is only used during parsing of the lambda. */
9933 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9935 /* This lambda shouldn't have any proxies left at this point. */
9936 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9937 /* And now that we're done, push proxies for an enclosing lambda. */
9938 insert_pending_capture_proxies ();
9940 if (ok)
9941 lambda_expr = build_lambda_object (lambda_expr);
9942 else
9943 lambda_expr = error_mark_node;
9945 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9947 pop_deferring_access_checks ();
9949 return lambda_expr;
9952 /* Parse the beginning of a lambda expression.
9954 lambda-introducer:
9955 [ lambda-capture [opt] ]
9957 LAMBDA_EXPR is the current representation of the lambda expression. */
9959 static void
9960 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9962 /* Need commas after the first capture. */
9963 bool first = true;
9965 /* Eat the leading `['. */
9966 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9968 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9969 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9970 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9971 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9972 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9973 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9975 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9977 cp_lexer_consume_token (parser->lexer);
9978 first = false;
9981 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9983 cp_token* capture_token;
9984 tree capture_id;
9985 tree capture_init_expr;
9986 cp_id_kind idk = CP_ID_KIND_NONE;
9987 bool explicit_init_p = false;
9989 enum capture_kind_type
9991 BY_COPY,
9992 BY_REFERENCE
9994 enum capture_kind_type capture_kind = BY_COPY;
9996 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9998 error ("expected end of capture-list");
9999 return;
10002 if (first)
10003 first = false;
10004 else
10005 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10007 /* Possibly capture `this'. */
10008 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10010 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10011 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10012 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10013 "with by-copy capture default");
10014 cp_lexer_consume_token (parser->lexer);
10015 add_capture (lambda_expr,
10016 /*id=*/this_identifier,
10017 /*initializer=*/finish_this_expr (),
10018 /*by_reference_p=*/true,
10019 explicit_init_p);
10020 continue;
10023 /* Possibly capture `*this'. */
10024 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10025 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10027 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10028 if (cxx_dialect < cxx1z)
10029 pedwarn (loc, 0, "%<*this%> capture only available with "
10030 "-std=c++1z or -std=gnu++1z");
10031 cp_lexer_consume_token (parser->lexer);
10032 cp_lexer_consume_token (parser->lexer);
10033 add_capture (lambda_expr,
10034 /*id=*/this_identifier,
10035 /*initializer=*/finish_this_expr (),
10036 /*by_reference_p=*/false,
10037 explicit_init_p);
10038 continue;
10041 /* Remember whether we want to capture as a reference or not. */
10042 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10044 capture_kind = BY_REFERENCE;
10045 cp_lexer_consume_token (parser->lexer);
10048 /* Get the identifier. */
10049 capture_token = cp_lexer_peek_token (parser->lexer);
10050 capture_id = cp_parser_identifier (parser);
10052 if (capture_id == error_mark_node)
10053 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10054 delimiters, but I modified this to stop on unnested ']' as well. It
10055 was already changed to stop on unnested '}', so the
10056 "closing_parenthesis" name is no more misleading with my change. */
10058 cp_parser_skip_to_closing_parenthesis (parser,
10059 /*recovering=*/true,
10060 /*or_comma=*/true,
10061 /*consume_paren=*/true);
10062 break;
10065 /* Find the initializer for this capture. */
10066 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10067 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10068 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10070 bool direct, non_constant;
10071 /* An explicit initializer exists. */
10072 if (cxx_dialect < cxx14)
10073 pedwarn (input_location, 0,
10074 "lambda capture initializers "
10075 "only available with -std=c++14 or -std=gnu++14");
10076 capture_init_expr = cp_parser_initializer (parser, &direct,
10077 &non_constant);
10078 explicit_init_p = true;
10079 if (capture_init_expr == NULL_TREE)
10081 error ("empty initializer for lambda init-capture");
10082 capture_init_expr = error_mark_node;
10085 else
10087 const char* error_msg;
10089 /* Turn the identifier into an id-expression. */
10090 capture_init_expr
10091 = cp_parser_lookup_name_simple (parser, capture_id,
10092 capture_token->location);
10094 if (capture_init_expr == error_mark_node)
10096 unqualified_name_lookup_error (capture_id);
10097 continue;
10099 else if (DECL_P (capture_init_expr)
10100 && (!VAR_P (capture_init_expr)
10101 && TREE_CODE (capture_init_expr) != PARM_DECL))
10103 error_at (capture_token->location,
10104 "capture of non-variable %qD ",
10105 capture_init_expr);
10106 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10107 "%q#D declared here", capture_init_expr);
10108 continue;
10110 if (VAR_P (capture_init_expr)
10111 && decl_storage_duration (capture_init_expr) != dk_auto)
10113 if (pedwarn (capture_token->location, 0, "capture of variable "
10114 "%qD with non-automatic storage duration",
10115 capture_init_expr))
10116 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10117 "%q#D declared here", capture_init_expr);
10118 continue;
10121 capture_init_expr
10122 = finish_id_expression
10123 (capture_id,
10124 capture_init_expr,
10125 parser->scope,
10126 &idk,
10127 /*integral_constant_expression_p=*/false,
10128 /*allow_non_integral_constant_expression_p=*/false,
10129 /*non_integral_constant_expression_p=*/NULL,
10130 /*template_p=*/false,
10131 /*done=*/true,
10132 /*address_p=*/false,
10133 /*template_arg_p=*/false,
10134 &error_msg,
10135 capture_token->location);
10137 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10139 cp_lexer_consume_token (parser->lexer);
10140 capture_init_expr = make_pack_expansion (capture_init_expr);
10142 else
10143 check_for_bare_parameter_packs (capture_init_expr);
10146 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10147 && !explicit_init_p)
10149 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10150 && capture_kind == BY_COPY)
10151 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10152 "of %qD redundant with by-copy capture default",
10153 capture_id);
10154 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10155 && capture_kind == BY_REFERENCE)
10156 pedwarn (capture_token->location, 0, "explicit by-reference "
10157 "capture of %qD redundant with by-reference capture "
10158 "default", capture_id);
10161 add_capture (lambda_expr,
10162 capture_id,
10163 capture_init_expr,
10164 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10165 explicit_init_p);
10168 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10171 /* Parse the (optional) middle of a lambda expression.
10173 lambda-declarator:
10174 < template-parameter-list [opt] >
10175 ( parameter-declaration-clause [opt] )
10176 attribute-specifier [opt]
10177 decl-specifier-seq [opt]
10178 exception-specification [opt]
10179 lambda-return-type-clause [opt]
10181 LAMBDA_EXPR is the current representation of the lambda expression. */
10183 static bool
10184 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10186 /* 5.1.1.4 of the standard says:
10187 If a lambda-expression does not include a lambda-declarator, it is as if
10188 the lambda-declarator were ().
10189 This means an empty parameter list, no attributes, and no exception
10190 specification. */
10191 tree param_list = void_list_node;
10192 tree attributes = NULL_TREE;
10193 tree exception_spec = NULL_TREE;
10194 tree template_param_list = NULL_TREE;
10195 tree tx_qual = NULL_TREE;
10196 cp_decl_specifier_seq lambda_specs;
10197 clear_decl_specs (&lambda_specs);
10199 /* The template-parameter-list is optional, but must begin with
10200 an opening angle if present. */
10201 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10203 if (cxx_dialect < cxx14)
10204 pedwarn (parser->lexer->next_token->location, 0,
10205 "lambda templates are only available with "
10206 "-std=c++14 or -std=gnu++14");
10207 else
10208 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10209 "ISO C++ does not support lambda templates");
10211 cp_lexer_consume_token (parser->lexer);
10213 template_param_list = cp_parser_template_parameter_list (parser);
10215 cp_parser_skip_to_end_of_template_parameter_list (parser);
10217 /* We just processed one more parameter list. */
10218 ++parser->num_template_parameter_lists;
10221 /* The parameter-declaration-clause is optional (unless
10222 template-parameter-list was given), but must begin with an
10223 opening parenthesis if present. */
10224 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10226 cp_lexer_consume_token (parser->lexer);
10228 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10230 /* Parse parameters. */
10231 param_list = cp_parser_parameter_declaration_clause (parser);
10233 /* Default arguments shall not be specified in the
10234 parameter-declaration-clause of a lambda-declarator. */
10235 if (cxx_dialect < cxx14)
10236 for (tree t = param_list; t; t = TREE_CHAIN (t))
10237 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10238 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10239 "default argument specified for lambda parameter");
10241 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10243 attributes = cp_parser_attributes_opt (parser);
10245 /* In the decl-specifier-seq of the lambda-declarator, each
10246 decl-specifier shall either be mutable or constexpr. */
10247 int declares_class_or_enum;
10248 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10249 cp_parser_decl_specifier_seq (parser,
10250 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10251 &lambda_specs, &declares_class_or_enum);
10252 if (lambda_specs.storage_class == sc_mutable)
10254 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10255 if (lambda_specs.conflicting_specifiers_p)
10256 error_at (lambda_specs.locations[ds_storage_class],
10257 "duplicate %<mutable%>");
10260 tx_qual = cp_parser_tx_qualifier_opt (parser);
10262 /* Parse optional exception specification. */
10263 exception_spec = cp_parser_exception_specification_opt (parser);
10265 /* Parse optional trailing return type. */
10266 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10268 cp_lexer_consume_token (parser->lexer);
10269 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10270 = cp_parser_trailing_type_id (parser);
10273 /* The function parameters must be in scope all the way until after the
10274 trailing-return-type in case of decltype. */
10275 pop_bindings_and_leave_scope ();
10277 else if (template_param_list != NULL_TREE) // generate diagnostic
10278 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10280 /* Create the function call operator.
10282 Messing with declarators like this is no uglier than building up the
10283 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10284 other code. */
10286 cp_decl_specifier_seq return_type_specs;
10287 cp_declarator* declarator;
10288 tree fco;
10289 int quals;
10290 void *p;
10292 clear_decl_specs (&return_type_specs);
10293 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10294 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10295 else
10296 /* Maybe we will deduce the return type later. */
10297 return_type_specs.type = make_auto ();
10299 if (lambda_specs.locations[ds_constexpr])
10301 if (cxx_dialect >= cxx1z)
10302 return_type_specs.locations[ds_constexpr]
10303 = lambda_specs.locations[ds_constexpr];
10304 else
10305 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10306 "lambda only available with -std=c++1z or -std=gnu++1z");
10309 p = obstack_alloc (&declarator_obstack, 0);
10311 declarator = make_id_declarator (NULL_TREE, cp_operator_id (CALL_EXPR),
10312 sfk_none);
10314 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10315 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10316 declarator = make_call_declarator (declarator, param_list, quals,
10317 VIRT_SPEC_UNSPECIFIED,
10318 REF_QUAL_NONE,
10319 tx_qual,
10320 exception_spec,
10321 /*late_return_type=*/NULL_TREE,
10322 /*requires_clause*/NULL_TREE);
10323 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10325 fco = grokmethod (&return_type_specs,
10326 declarator,
10327 attributes);
10328 if (fco != error_mark_node)
10330 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10331 DECL_ARTIFICIAL (fco) = 1;
10332 /* Give the object parameter a different name. */
10333 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10334 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10335 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10337 if (template_param_list)
10339 fco = finish_member_template_decl (fco);
10340 finish_template_decl (template_param_list);
10341 --parser->num_template_parameter_lists;
10343 else if (parser->fully_implicit_function_template_p)
10344 fco = finish_fully_implicit_template (parser, fco);
10346 finish_member_declaration (fco);
10348 obstack_free (&declarator_obstack, p);
10350 return (fco != error_mark_node);
10354 /* Parse the body of a lambda expression, which is simply
10356 compound-statement
10358 but which requires special handling.
10359 LAMBDA_EXPR is the current representation of the lambda expression. */
10361 static void
10362 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10364 bool nested = (current_function_decl != NULL_TREE);
10365 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10366 if (nested)
10367 push_function_context ();
10368 else
10369 /* Still increment function_depth so that we don't GC in the
10370 middle of an expression. */
10371 ++function_depth;
10372 vec<tree> omp_privatization_save;
10373 save_omp_privatization_clauses (omp_privatization_save);
10374 /* Clear this in case we're in the middle of a default argument. */
10375 parser->local_variables_forbidden_p = false;
10377 /* Finish the function call operator
10378 - class_specifier
10379 + late_parsing_for_member
10380 + function_definition_after_declarator
10381 + ctor_initializer_opt_and_function_body */
10383 tree fco = lambda_function (lambda_expr);
10384 tree body;
10385 bool done = false;
10386 tree compound_stmt;
10387 tree cap;
10389 /* Let the front end know that we are going to be defining this
10390 function. */
10391 start_preparsed_function (fco,
10392 NULL_TREE,
10393 SF_PRE_PARSED | SF_INCLASS_INLINE);
10395 start_lambda_scope (fco);
10396 body = begin_function_body ();
10398 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10399 goto out;
10401 /* Push the proxies for any explicit captures. */
10402 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10403 cap = TREE_CHAIN (cap))
10404 build_capture_proxy (TREE_PURPOSE (cap));
10406 compound_stmt = begin_compound_stmt (0);
10408 /* 5.1.1.4 of the standard says:
10409 If a lambda-expression does not include a trailing-return-type, it
10410 is as if the trailing-return-type denotes the following type:
10411 * if the compound-statement is of the form
10412 { return attribute-specifier [opt] expression ; }
10413 the type of the returned expression after lvalue-to-rvalue
10414 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10415 (_conv.array_ 4.2), and function-to-pointer conversion
10416 (_conv.func_ 4.3);
10417 * otherwise, void. */
10419 /* In a lambda that has neither a lambda-return-type-clause
10420 nor a deducible form, errors should be reported for return statements
10421 in the body. Since we used void as the placeholder return type, parsing
10422 the body as usual will give such desired behavior. */
10423 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10424 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10425 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10427 tree expr = NULL_TREE;
10428 cp_id_kind idk = CP_ID_KIND_NONE;
10430 /* Parse tentatively in case there's more after the initial return
10431 statement. */
10432 cp_parser_parse_tentatively (parser);
10434 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10436 expr = cp_parser_expression (parser, &idk);
10438 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10439 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10441 if (cp_parser_parse_definitely (parser))
10443 if (!processing_template_decl)
10445 tree type = lambda_return_type (expr);
10446 apply_deduced_return_type (fco, type);
10447 if (type == error_mark_node)
10448 expr = error_mark_node;
10451 /* Will get error here if type not deduced yet. */
10452 finish_return_stmt (expr);
10454 done = true;
10458 if (!done)
10460 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10461 cp_parser_label_declaration (parser);
10462 cp_parser_statement_seq_opt (parser, NULL_TREE);
10463 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10466 finish_compound_stmt (compound_stmt);
10468 out:
10469 finish_function_body (body);
10470 finish_lambda_scope ();
10472 /* Finish the function and generate code for it if necessary. */
10473 tree fn = finish_function (/*inline*/2);
10475 /* Only expand if the call op is not a template. */
10476 if (!DECL_TEMPLATE_INFO (fco))
10477 expand_or_defer_fn (fn);
10480 restore_omp_privatization_clauses (omp_privatization_save);
10481 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10482 if (nested)
10483 pop_function_context();
10484 else
10485 --function_depth;
10488 /* Statements [gram.stmt.stmt] */
10490 /* Parse a statement.
10492 statement:
10493 labeled-statement
10494 expression-statement
10495 compound-statement
10496 selection-statement
10497 iteration-statement
10498 jump-statement
10499 declaration-statement
10500 try-block
10502 C++11:
10504 statement:
10505 labeled-statement
10506 attribute-specifier-seq (opt) expression-statement
10507 attribute-specifier-seq (opt) compound-statement
10508 attribute-specifier-seq (opt) selection-statement
10509 attribute-specifier-seq (opt) iteration-statement
10510 attribute-specifier-seq (opt) jump-statement
10511 declaration-statement
10512 attribute-specifier-seq (opt) try-block
10514 init-statement:
10515 expression-statement
10516 simple-declaration
10518 TM Extension:
10520 statement:
10521 atomic-statement
10523 IN_COMPOUND is true when the statement is nested inside a
10524 cp_parser_compound_statement; this matters for certain pragmas.
10526 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10527 is a (possibly labeled) if statement which is not enclosed in braces
10528 and has an else clause. This is used to implement -Wparentheses.
10530 CHAIN is a vector of if-else-if conditions. */
10532 static void
10533 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10534 bool in_compound, bool *if_p, vec<tree> *chain)
10536 tree statement, std_attrs = NULL_TREE;
10537 cp_token *token;
10538 location_t statement_location, attrs_location;
10540 restart:
10541 if (if_p != NULL)
10542 *if_p = false;
10543 /* There is no statement yet. */
10544 statement = NULL_TREE;
10546 saved_token_sentinel saved_tokens (parser->lexer);
10547 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10548 if (c_dialect_objc ())
10549 /* In obj-c++, seeing '[[' might be the either the beginning of
10550 c++11 attributes, or a nested objc-message-expression. So
10551 let's parse the c++11 attributes tentatively. */
10552 cp_parser_parse_tentatively (parser);
10553 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10554 if (c_dialect_objc ())
10556 if (!cp_parser_parse_definitely (parser))
10557 std_attrs = NULL_TREE;
10560 /* Peek at the next token. */
10561 token = cp_lexer_peek_token (parser->lexer);
10562 /* Remember the location of the first token in the statement. */
10563 statement_location = token->location;
10564 /* If this is a keyword, then that will often determine what kind of
10565 statement we have. */
10566 if (token->type == CPP_KEYWORD)
10568 enum rid keyword = token->keyword;
10570 switch (keyword)
10572 case RID_CASE:
10573 case RID_DEFAULT:
10574 /* Looks like a labeled-statement with a case label.
10575 Parse the label, and then use tail recursion to parse
10576 the statement. */
10577 cp_parser_label_for_labeled_statement (parser, std_attrs);
10578 in_compound = false;
10579 goto restart;
10581 case RID_IF:
10582 case RID_SWITCH:
10583 statement = cp_parser_selection_statement (parser, if_p, chain);
10584 break;
10586 case RID_WHILE:
10587 case RID_DO:
10588 case RID_FOR:
10589 statement = cp_parser_iteration_statement (parser, if_p, false);
10590 break;
10592 case RID_CILK_FOR:
10593 if (!flag_cilkplus)
10595 error_at (cp_lexer_peek_token (parser->lexer)->location,
10596 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10597 cp_lexer_consume_token (parser->lexer);
10598 statement = error_mark_node;
10600 else
10601 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10602 break;
10604 case RID_BREAK:
10605 case RID_CONTINUE:
10606 case RID_RETURN:
10607 case RID_GOTO:
10608 statement = cp_parser_jump_statement (parser);
10609 break;
10611 case RID_CILK_SYNC:
10612 cp_lexer_consume_token (parser->lexer);
10613 if (flag_cilkplus)
10615 tree sync_expr = build_cilk_sync ();
10616 SET_EXPR_LOCATION (sync_expr,
10617 token->location);
10618 statement = finish_expr_stmt (sync_expr);
10620 else
10622 error_at (token->location, "-fcilkplus must be enabled to use"
10623 " %<_Cilk_sync%>");
10624 statement = error_mark_node;
10626 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10627 break;
10629 /* Objective-C++ exception-handling constructs. */
10630 case RID_AT_TRY:
10631 case RID_AT_CATCH:
10632 case RID_AT_FINALLY:
10633 case RID_AT_SYNCHRONIZED:
10634 case RID_AT_THROW:
10635 statement = cp_parser_objc_statement (parser);
10636 break;
10638 case RID_TRY:
10639 statement = cp_parser_try_block (parser);
10640 break;
10642 case RID_NAMESPACE:
10643 /* This must be a namespace alias definition. */
10644 cp_parser_declaration_statement (parser);
10645 return;
10647 case RID_TRANSACTION_ATOMIC:
10648 case RID_TRANSACTION_RELAXED:
10649 case RID_SYNCHRONIZED:
10650 case RID_ATOMIC_NOEXCEPT:
10651 case RID_ATOMIC_CANCEL:
10652 statement = cp_parser_transaction (parser, token);
10653 break;
10654 case RID_TRANSACTION_CANCEL:
10655 statement = cp_parser_transaction_cancel (parser);
10656 break;
10658 default:
10659 /* It might be a keyword like `int' that can start a
10660 declaration-statement. */
10661 break;
10664 else if (token->type == CPP_NAME)
10666 /* If the next token is a `:', then we are looking at a
10667 labeled-statement. */
10668 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10669 if (token->type == CPP_COLON)
10671 /* Looks like a labeled-statement with an ordinary label.
10672 Parse the label, and then use tail recursion to parse
10673 the statement. */
10675 cp_parser_label_for_labeled_statement (parser, std_attrs);
10676 in_compound = false;
10677 goto restart;
10680 /* Anything that starts with a `{' must be a compound-statement. */
10681 else if (token->type == CPP_OPEN_BRACE)
10682 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10683 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10684 a statement all its own. */
10685 else if (token->type == CPP_PRAGMA)
10687 /* Only certain OpenMP pragmas are attached to statements, and thus
10688 are considered statements themselves. All others are not. In
10689 the context of a compound, accept the pragma as a "statement" and
10690 return so that we can check for a close brace. Otherwise we
10691 require a real statement and must go back and read one. */
10692 if (in_compound)
10693 cp_parser_pragma (parser, pragma_compound, if_p);
10694 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10695 goto restart;
10696 return;
10698 else if (token->type == CPP_EOF)
10700 cp_parser_error (parser, "expected statement");
10701 return;
10704 /* Everything else must be a declaration-statement or an
10705 expression-statement. Try for the declaration-statement
10706 first, unless we are looking at a `;', in which case we know that
10707 we have an expression-statement. */
10708 if (!statement)
10710 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10712 if (std_attrs != NULL_TREE)
10714 /* Attributes should be parsed as part of the the
10715 declaration, so let's un-parse them. */
10716 saved_tokens.rollback();
10717 std_attrs = NULL_TREE;
10720 cp_parser_parse_tentatively (parser);
10721 /* Try to parse the declaration-statement. */
10722 cp_parser_declaration_statement (parser);
10723 /* If that worked, we're done. */
10724 if (cp_parser_parse_definitely (parser))
10725 return;
10727 /* Look for an expression-statement instead. */
10728 statement = cp_parser_expression_statement (parser, in_statement_expr);
10730 /* Handle [[fallthrough]];. */
10731 if (attribute_fallthrough_p (std_attrs))
10733 /* The next token after the fallthrough attribute is ';'. */
10734 if (statement == NULL_TREE)
10736 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10737 statement = build_call_expr_internal_loc (statement_location,
10738 IFN_FALLTHROUGH,
10739 void_type_node, 0);
10740 finish_expr_stmt (statement);
10742 else
10743 warning_at (statement_location, OPT_Wattributes,
10744 "%<fallthrough%> attribute not followed by %<;%>");
10745 std_attrs = NULL_TREE;
10749 /* Set the line number for the statement. */
10750 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10751 SET_EXPR_LOCATION (statement, statement_location);
10753 /* Allow "[[fallthrough]];", but warn otherwise. */
10754 if (std_attrs != NULL_TREE)
10755 warning_at (attrs_location,
10756 OPT_Wattributes,
10757 "attributes at the beginning of statement are ignored");
10760 /* Parse the label for a labeled-statement, i.e.
10762 identifier :
10763 case constant-expression :
10764 default :
10766 GNU Extension:
10767 case constant-expression ... constant-expression : statement
10769 When a label is parsed without errors, the label is added to the
10770 parse tree by the finish_* functions, so this function doesn't
10771 have to return the label. */
10773 static void
10774 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10776 cp_token *token;
10777 tree label = NULL_TREE;
10778 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10780 /* The next token should be an identifier. */
10781 token = cp_lexer_peek_token (parser->lexer);
10782 if (token->type != CPP_NAME
10783 && token->type != CPP_KEYWORD)
10785 cp_parser_error (parser, "expected labeled-statement");
10786 return;
10789 /* Remember whether this case or a user-defined label is allowed to fall
10790 through to. */
10791 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10793 parser->colon_corrects_to_scope_p = false;
10794 switch (token->keyword)
10796 case RID_CASE:
10798 tree expr, expr_hi;
10799 cp_token *ellipsis;
10801 /* Consume the `case' token. */
10802 cp_lexer_consume_token (parser->lexer);
10803 /* Parse the constant-expression. */
10804 expr = cp_parser_constant_expression (parser);
10805 if (check_for_bare_parameter_packs (expr))
10806 expr = error_mark_node;
10808 ellipsis = cp_lexer_peek_token (parser->lexer);
10809 if (ellipsis->type == CPP_ELLIPSIS)
10811 /* Consume the `...' token. */
10812 cp_lexer_consume_token (parser->lexer);
10813 expr_hi = cp_parser_constant_expression (parser);
10814 if (check_for_bare_parameter_packs (expr_hi))
10815 expr_hi = error_mark_node;
10817 /* We don't need to emit warnings here, as the common code
10818 will do this for us. */
10820 else
10821 expr_hi = NULL_TREE;
10823 if (parser->in_switch_statement_p)
10825 tree l = finish_case_label (token->location, expr, expr_hi);
10826 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10827 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10829 else
10830 error_at (token->location,
10831 "case label %qE not within a switch statement",
10832 expr);
10834 break;
10836 case RID_DEFAULT:
10837 /* Consume the `default' token. */
10838 cp_lexer_consume_token (parser->lexer);
10840 if (parser->in_switch_statement_p)
10842 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10843 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10844 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10846 else
10847 error_at (token->location, "case label not within a switch statement");
10848 break;
10850 default:
10851 /* Anything else must be an ordinary label. */
10852 label = finish_label_stmt (cp_parser_identifier (parser));
10853 if (label && TREE_CODE (label) == LABEL_DECL)
10854 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
10855 break;
10858 /* Require the `:' token. */
10859 cp_parser_require (parser, CPP_COLON, RT_COLON);
10861 /* An ordinary label may optionally be followed by attributes.
10862 However, this is only permitted if the attributes are then
10863 followed by a semicolon. This is because, for backward
10864 compatibility, when parsing
10865 lab: __attribute__ ((unused)) int i;
10866 we want the attribute to attach to "i", not "lab". */
10867 if (label != NULL_TREE
10868 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10870 tree attrs;
10871 cp_parser_parse_tentatively (parser);
10872 attrs = cp_parser_gnu_attributes_opt (parser);
10873 if (attrs == NULL_TREE
10874 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10875 cp_parser_abort_tentative_parse (parser);
10876 else if (!cp_parser_parse_definitely (parser))
10878 else
10879 attributes = chainon (attributes, attrs);
10882 if (attributes != NULL_TREE)
10883 cplus_decl_attributes (&label, attributes, 0);
10885 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10888 /* Parse an expression-statement.
10890 expression-statement:
10891 expression [opt] ;
10893 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10894 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10895 indicates whether this expression-statement is part of an
10896 expression statement. */
10898 static tree
10899 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10901 tree statement = NULL_TREE;
10902 cp_token *token = cp_lexer_peek_token (parser->lexer);
10903 location_t loc = token->location;
10905 /* There might be attribute fallthrough. */
10906 tree attr = cp_parser_gnu_attributes_opt (parser);
10908 /* If the next token is a ';', then there is no expression
10909 statement. */
10910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10912 statement = cp_parser_expression (parser);
10913 if (statement == error_mark_node
10914 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10916 cp_parser_skip_to_end_of_block_or_statement (parser);
10917 return error_mark_node;
10921 /* Handle [[fallthrough]];. */
10922 if (attribute_fallthrough_p (attr))
10924 /* The next token after the fallthrough attribute is ';'. */
10925 if (statement == NULL_TREE)
10926 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10927 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
10928 void_type_node, 0);
10929 else
10930 warning_at (loc, OPT_Wattributes,
10931 "%<fallthrough%> attribute not followed by %<;%>");
10932 attr = NULL_TREE;
10935 /* Allow "[[fallthrough]];", but warn otherwise. */
10936 if (attr != NULL_TREE)
10937 warning_at (loc, OPT_Wattributes,
10938 "attributes at the beginning of statement are ignored");
10940 /* Give a helpful message for "A<T>::type t;" and the like. */
10941 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10942 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10944 if (TREE_CODE (statement) == SCOPE_REF)
10945 error_at (token->location, "need %<typename%> before %qE because "
10946 "%qT is a dependent scope",
10947 statement, TREE_OPERAND (statement, 0));
10948 else if (is_overloaded_fn (statement)
10949 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10951 /* A::A a; */
10952 tree fn = get_first_fn (statement);
10953 error_at (token->location,
10954 "%<%T::%D%> names the constructor, not the type",
10955 DECL_CONTEXT (fn), DECL_NAME (fn));
10959 /* Consume the final `;'. */
10960 cp_parser_consume_semicolon_at_end_of_statement (parser);
10962 if (in_statement_expr
10963 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10964 /* This is the final expression statement of a statement
10965 expression. */
10966 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10967 else if (statement)
10968 statement = finish_expr_stmt (statement);
10970 return statement;
10973 /* Parse a compound-statement.
10975 compound-statement:
10976 { statement-seq [opt] }
10978 GNU extension:
10980 compound-statement:
10981 { label-declaration-seq [opt] statement-seq [opt] }
10983 label-declaration-seq:
10984 label-declaration
10985 label-declaration-seq label-declaration
10987 Returns a tree representing the statement. */
10989 static tree
10990 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10991 int bcs_flags, bool function_body)
10993 tree compound_stmt;
10995 /* Consume the `{'. */
10996 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10997 return error_mark_node;
10998 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10999 && !function_body && cxx_dialect < cxx14)
11000 pedwarn (input_location, OPT_Wpedantic,
11001 "compound-statement in constexpr function");
11002 /* Begin the compound-statement. */
11003 compound_stmt = begin_compound_stmt (bcs_flags);
11004 /* If the next keyword is `__label__' we have a label declaration. */
11005 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11006 cp_parser_label_declaration (parser);
11007 /* Parse an (optional) statement-seq. */
11008 cp_parser_statement_seq_opt (parser, in_statement_expr);
11009 /* Finish the compound-statement. */
11010 finish_compound_stmt (compound_stmt);
11011 /* Consume the `}'. */
11012 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11014 return compound_stmt;
11017 /* Parse an (optional) statement-seq.
11019 statement-seq:
11020 statement
11021 statement-seq [opt] statement */
11023 static void
11024 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11026 /* Scan statements until there aren't any more. */
11027 while (true)
11029 cp_token *token = cp_lexer_peek_token (parser->lexer);
11031 /* If we are looking at a `}', then we have run out of
11032 statements; the same is true if we have reached the end
11033 of file, or have stumbled upon a stray '@end'. */
11034 if (token->type == CPP_CLOSE_BRACE
11035 || token->type == CPP_EOF
11036 || token->type == CPP_PRAGMA_EOL
11037 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11038 break;
11040 /* If we are in a compound statement and find 'else' then
11041 something went wrong. */
11042 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11044 if (parser->in_statement & IN_IF_STMT)
11045 break;
11046 else
11048 token = cp_lexer_consume_token (parser->lexer);
11049 error_at (token->location, "%<else%> without a previous %<if%>");
11053 /* Parse the statement. */
11054 cp_parser_statement (parser, in_statement_expr, true, NULL);
11058 /* Return true if we're looking at (init; cond), false otherwise. */
11060 static bool
11061 cp_parser_init_statement_p (cp_parser *parser)
11063 /* Save tokens so that we can put them back. */
11064 cp_lexer_save_tokens (parser->lexer);
11066 /* Look for ';' that is not nested in () or {}. */
11067 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11068 /*recovering=*/false,
11069 CPP_SEMICOLON,
11070 /*consume_paren=*/false);
11072 /* Roll back the tokens we skipped. */
11073 cp_lexer_rollback_tokens (parser->lexer);
11075 return ret == -1;
11078 /* Parse a selection-statement.
11080 selection-statement:
11081 if ( init-statement [opt] condition ) statement
11082 if ( init-statement [opt] condition ) statement else statement
11083 switch ( init-statement [opt] condition ) statement
11085 Returns the new IF_STMT or SWITCH_STMT.
11087 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11088 is a (possibly labeled) if statement which is not enclosed in
11089 braces and has an else clause. This is used to implement
11090 -Wparentheses.
11092 CHAIN is a vector of if-else-if conditions. This is used to implement
11093 -Wduplicated-cond. */
11095 static tree
11096 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11097 vec<tree> *chain)
11099 cp_token *token;
11100 enum rid keyword;
11101 token_indent_info guard_tinfo;
11103 if (if_p != NULL)
11104 *if_p = false;
11106 /* Peek at the next token. */
11107 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11108 guard_tinfo = get_token_indent_info (token);
11110 /* See what kind of keyword it is. */
11111 keyword = token->keyword;
11112 switch (keyword)
11114 case RID_IF:
11115 case RID_SWITCH:
11117 tree statement;
11118 tree condition;
11120 bool cx = false;
11121 if (keyword == RID_IF
11122 && cp_lexer_next_token_is_keyword (parser->lexer,
11123 RID_CONSTEXPR))
11125 cx = true;
11126 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11127 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
11128 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11129 "with -std=c++1z or -std=gnu++1z");
11132 /* Look for the `('. */
11133 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11135 cp_parser_skip_to_end_of_statement (parser);
11136 return error_mark_node;
11139 /* Begin the selection-statement. */
11140 if (keyword == RID_IF)
11142 statement = begin_if_stmt ();
11143 IF_STMT_CONSTEXPR_P (statement) = cx;
11145 else
11146 statement = begin_switch_stmt ();
11148 /* Parse the optional init-statement. */
11149 if (cp_parser_init_statement_p (parser))
11151 tree decl;
11152 if (cxx_dialect < cxx1z)
11153 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11154 "init-statement in selection statements only available "
11155 "with -std=c++1z or -std=gnu++1z");
11156 cp_parser_init_statement (parser, &decl);
11159 /* Parse the condition. */
11160 condition = cp_parser_condition (parser);
11161 /* Look for the `)'. */
11162 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11163 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11164 /*consume_paren=*/true);
11166 if (keyword == RID_IF)
11168 bool nested_if;
11169 unsigned char in_statement;
11171 /* Add the condition. */
11172 condition = finish_if_stmt_cond (condition, statement);
11174 if (warn_duplicated_cond)
11175 warn_duplicated_cond_add_or_warn (token->location, condition,
11176 &chain);
11178 /* Parse the then-clause. */
11179 in_statement = parser->in_statement;
11180 parser->in_statement |= IN_IF_STMT;
11182 /* Outside a template, the non-selected branch of a constexpr
11183 if is a 'discarded statement', i.e. unevaluated. */
11184 bool was_discarded = in_discarded_stmt;
11185 bool discard_then = (cx && !processing_template_decl
11186 && integer_zerop (condition));
11187 if (discard_then)
11189 in_discarded_stmt = true;
11190 ++c_inhibit_evaluation_warnings;
11193 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11194 guard_tinfo);
11196 parser->in_statement = in_statement;
11198 finish_then_clause (statement);
11200 if (discard_then)
11202 THEN_CLAUSE (statement) = NULL_TREE;
11203 in_discarded_stmt = was_discarded;
11204 --c_inhibit_evaluation_warnings;
11207 /* If the next token is `else', parse the else-clause. */
11208 if (cp_lexer_next_token_is_keyword (parser->lexer,
11209 RID_ELSE))
11211 bool discard_else = (cx && !processing_template_decl
11212 && integer_nonzerop (condition));
11213 if (discard_else)
11215 in_discarded_stmt = true;
11216 ++c_inhibit_evaluation_warnings;
11219 guard_tinfo
11220 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11221 /* Consume the `else' keyword. */
11222 cp_lexer_consume_token (parser->lexer);
11223 if (warn_duplicated_cond)
11225 if (cp_lexer_next_token_is_keyword (parser->lexer,
11226 RID_IF)
11227 && chain == NULL)
11229 /* We've got "if (COND) else if (COND2)". Start
11230 the condition chain and add COND as the first
11231 element. */
11232 chain = new vec<tree> ();
11233 if (!CONSTANT_CLASS_P (condition)
11234 && !TREE_SIDE_EFFECTS (condition))
11236 /* Wrap it in a NOP_EXPR so that we can set the
11237 location of the condition. */
11238 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11239 condition);
11240 SET_EXPR_LOCATION (e, token->location);
11241 chain->safe_push (e);
11244 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11245 RID_IF))
11247 /* This is if-else without subsequent if. Zap the
11248 condition chain; we would have already warned at
11249 this point. */
11250 delete chain;
11251 chain = NULL;
11254 begin_else_clause (statement);
11255 /* Parse the else-clause. */
11256 cp_parser_implicitly_scoped_statement (parser, NULL,
11257 guard_tinfo, chain);
11259 finish_else_clause (statement);
11261 /* If we are currently parsing a then-clause, then
11262 IF_P will not be NULL. We set it to true to
11263 indicate that this if statement has an else clause.
11264 This may trigger the Wparentheses warning below
11265 when we get back up to the parent if statement. */
11266 if (if_p != NULL)
11267 *if_p = true;
11269 if (discard_else)
11271 ELSE_CLAUSE (statement) = NULL_TREE;
11272 in_discarded_stmt = was_discarded;
11273 --c_inhibit_evaluation_warnings;
11276 else
11278 /* This if statement does not have an else clause. If
11279 NESTED_IF is true, then the then-clause has an if
11280 statement which does have an else clause. We warn
11281 about the potential ambiguity. */
11282 if (nested_if)
11283 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11284 "suggest explicit braces to avoid ambiguous"
11285 " %<else%>");
11286 if (warn_duplicated_cond)
11288 /* We don't need the condition chain anymore. */
11289 delete chain;
11290 chain = NULL;
11294 /* Now we're all done with the if-statement. */
11295 finish_if_stmt (statement);
11297 else
11299 bool in_switch_statement_p;
11300 unsigned char in_statement;
11302 /* Add the condition. */
11303 finish_switch_cond (condition, statement);
11305 /* Parse the body of the switch-statement. */
11306 in_switch_statement_p = parser->in_switch_statement_p;
11307 in_statement = parser->in_statement;
11308 parser->in_switch_statement_p = true;
11309 parser->in_statement |= IN_SWITCH_STMT;
11310 cp_parser_implicitly_scoped_statement (parser, if_p,
11311 guard_tinfo);
11312 parser->in_switch_statement_p = in_switch_statement_p;
11313 parser->in_statement = in_statement;
11315 /* Now we're all done with the switch-statement. */
11316 finish_switch_stmt (statement);
11319 return statement;
11321 break;
11323 default:
11324 cp_parser_error (parser, "expected selection-statement");
11325 return error_mark_node;
11329 /* Parse a condition.
11331 condition:
11332 expression
11333 type-specifier-seq declarator = initializer-clause
11334 type-specifier-seq declarator braced-init-list
11336 GNU Extension:
11338 condition:
11339 type-specifier-seq declarator asm-specification [opt]
11340 attributes [opt] = assignment-expression
11342 Returns the expression that should be tested. */
11344 static tree
11345 cp_parser_condition (cp_parser* parser)
11347 cp_decl_specifier_seq type_specifiers;
11348 const char *saved_message;
11349 int declares_class_or_enum;
11351 /* Try the declaration first. */
11352 cp_parser_parse_tentatively (parser);
11353 /* New types are not allowed in the type-specifier-seq for a
11354 condition. */
11355 saved_message = parser->type_definition_forbidden_message;
11356 parser->type_definition_forbidden_message
11357 = G_("types may not be defined in conditions");
11358 /* Parse the type-specifier-seq. */
11359 cp_parser_decl_specifier_seq (parser,
11360 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11361 &type_specifiers,
11362 &declares_class_or_enum);
11363 /* Restore the saved message. */
11364 parser->type_definition_forbidden_message = saved_message;
11365 /* If all is well, we might be looking at a declaration. */
11366 if (!cp_parser_error_occurred (parser))
11368 tree decl;
11369 tree asm_specification;
11370 tree attributes;
11371 cp_declarator *declarator;
11372 tree initializer = NULL_TREE;
11374 /* Parse the declarator. */
11375 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11376 /*ctor_dtor_or_conv_p=*/NULL,
11377 /*parenthesized_p=*/NULL,
11378 /*member_p=*/false,
11379 /*friend_p=*/false);
11380 /* Parse the attributes. */
11381 attributes = cp_parser_attributes_opt (parser);
11382 /* Parse the asm-specification. */
11383 asm_specification = cp_parser_asm_specification_opt (parser);
11384 /* If the next token is not an `=' or '{', then we might still be
11385 looking at an expression. For example:
11387 if (A(a).x)
11389 looks like a decl-specifier-seq and a declarator -- but then
11390 there is no `=', so this is an expression. */
11391 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11392 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11393 cp_parser_simulate_error (parser);
11395 /* If we did see an `=' or '{', then we are looking at a declaration
11396 for sure. */
11397 if (cp_parser_parse_definitely (parser))
11399 tree pushed_scope;
11400 bool non_constant_p;
11401 int flags = LOOKUP_ONLYCONVERTING;
11403 /* Create the declaration. */
11404 decl = start_decl (declarator, &type_specifiers,
11405 /*initialized_p=*/true,
11406 attributes, /*prefix_attributes=*/NULL_TREE,
11407 &pushed_scope);
11409 /* Parse the initializer. */
11410 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11412 initializer = cp_parser_braced_list (parser, &non_constant_p);
11413 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11414 flags = 0;
11416 else
11418 /* Consume the `='. */
11419 cp_parser_require (parser, CPP_EQ, RT_EQ);
11420 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11422 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11423 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11425 /* Process the initializer. */
11426 cp_finish_decl (decl,
11427 initializer, !non_constant_p,
11428 asm_specification,
11429 flags);
11431 if (pushed_scope)
11432 pop_scope (pushed_scope);
11434 return convert_from_reference (decl);
11437 /* If we didn't even get past the declarator successfully, we are
11438 definitely not looking at a declaration. */
11439 else
11440 cp_parser_abort_tentative_parse (parser);
11442 /* Otherwise, we are looking at an expression. */
11443 return cp_parser_expression (parser);
11446 /* Parses a for-statement or range-for-statement until the closing ')',
11447 not included. */
11449 static tree
11450 cp_parser_for (cp_parser *parser, bool ivdep)
11452 tree init, scope, decl;
11453 bool is_range_for;
11455 /* Begin the for-statement. */
11456 scope = begin_for_scope (&init);
11458 /* Parse the initialization. */
11459 is_range_for = cp_parser_init_statement (parser, &decl);
11461 if (is_range_for)
11462 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11463 else
11464 return cp_parser_c_for (parser, scope, init, ivdep);
11467 static tree
11468 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11470 /* Normal for loop */
11471 tree condition = NULL_TREE;
11472 tree expression = NULL_TREE;
11473 tree stmt;
11475 stmt = begin_for_stmt (scope, init);
11476 /* The init-statement has already been parsed in
11477 cp_parser_init_statement, so no work is needed here. */
11478 finish_init_stmt (stmt);
11480 /* If there's a condition, process it. */
11481 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11482 condition = cp_parser_condition (parser);
11483 else if (ivdep)
11485 cp_parser_error (parser, "missing loop condition in loop with "
11486 "%<GCC ivdep%> pragma");
11487 condition = error_mark_node;
11489 finish_for_cond (condition, stmt, ivdep);
11490 /* Look for the `;'. */
11491 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11493 /* If there's an expression, process it. */
11494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11495 expression = cp_parser_expression (parser);
11496 finish_for_expr (expression, stmt);
11498 return stmt;
11501 /* Tries to parse a range-based for-statement:
11503 range-based-for:
11504 decl-specifier-seq declarator : expression
11506 The decl-specifier-seq declarator and the `:' are already parsed by
11507 cp_parser_init_statement. If processing_template_decl it returns a
11508 newly created RANGE_FOR_STMT; if not, it is converted to a
11509 regular FOR_STMT. */
11511 static tree
11512 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11513 bool ivdep)
11515 tree stmt, range_expr;
11516 auto_vec <cxx_binding *, 16> bindings;
11517 auto_vec <tree, 16> names;
11518 tree decomp_first_name = NULL_TREE;
11519 unsigned int decomp_cnt = 0;
11521 /* Get the range declaration momentarily out of the way so that
11522 the range expression doesn't clash with it. */
11523 if (range_decl != error_mark_node)
11525 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11527 tree v = DECL_VALUE_EXPR (range_decl);
11528 /* For decomposition declaration get all of the corresponding
11529 declarations out of the way. */
11530 if (TREE_CODE (v) == ARRAY_REF
11531 && VAR_P (TREE_OPERAND (v, 0))
11532 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11534 tree d = range_decl;
11535 range_decl = TREE_OPERAND (v, 0);
11536 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11537 decomp_first_name = d;
11538 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11540 tree name = DECL_NAME (d);
11541 names.safe_push (name);
11542 bindings.safe_push (IDENTIFIER_BINDING (name));
11543 IDENTIFIER_BINDING (name)
11544 = IDENTIFIER_BINDING (name)->previous;
11548 if (names.is_empty ())
11550 tree name = DECL_NAME (range_decl);
11551 names.safe_push (name);
11552 bindings.safe_push (IDENTIFIER_BINDING (name));
11553 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11557 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11559 bool expr_non_constant_p;
11560 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11562 else
11563 range_expr = cp_parser_expression (parser);
11565 /* Put the range declaration(s) back into scope. */
11566 for (unsigned int i = 0; i < names.length (); i++)
11568 cxx_binding *binding = bindings[i];
11569 binding->previous = IDENTIFIER_BINDING (names[i]);
11570 IDENTIFIER_BINDING (names[i]) = binding;
11573 /* If in template, STMT is converted to a normal for-statement
11574 at instantiation. If not, it is done just ahead. */
11575 if (processing_template_decl)
11577 if (check_for_bare_parameter_packs (range_expr))
11578 range_expr = error_mark_node;
11579 stmt = begin_range_for_stmt (scope, init);
11580 if (ivdep)
11581 RANGE_FOR_IVDEP (stmt) = 1;
11582 finish_range_for_decl (stmt, range_decl, range_expr);
11583 if (!type_dependent_expression_p (range_expr)
11584 /* do_auto_deduction doesn't mess with template init-lists. */
11585 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11586 do_range_for_auto_deduction (range_decl, range_expr);
11588 else
11590 stmt = begin_for_stmt (scope, init);
11591 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11592 decomp_first_name, decomp_cnt, ivdep);
11594 return stmt;
11597 /* Subroutine of cp_convert_range_for: given the initializer expression,
11598 builds up the range temporary. */
11600 static tree
11601 build_range_temp (tree range_expr)
11603 tree range_type, range_temp;
11605 /* Find out the type deduced by the declaration
11606 `auto &&__range = range_expr'. */
11607 range_type = cp_build_reference_type (make_auto (), true);
11608 range_type = do_auto_deduction (range_type, range_expr,
11609 type_uses_auto (range_type));
11611 /* Create the __range variable. */
11612 range_temp = build_decl (input_location, VAR_DECL,
11613 get_identifier ("__for_range"), range_type);
11614 TREE_USED (range_temp) = 1;
11615 DECL_ARTIFICIAL (range_temp) = 1;
11617 return range_temp;
11620 /* Used by cp_parser_range_for in template context: we aren't going to
11621 do a full conversion yet, but we still need to resolve auto in the
11622 type of the for-range-declaration if present. This is basically
11623 a shortcut version of cp_convert_range_for. */
11625 static void
11626 do_range_for_auto_deduction (tree decl, tree range_expr)
11628 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11629 if (auto_node)
11631 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11632 range_temp = convert_from_reference (build_range_temp (range_expr));
11633 iter_type = (cp_parser_perform_range_for_lookup
11634 (range_temp, &begin_dummy, &end_dummy));
11635 if (iter_type)
11637 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11638 iter_type);
11639 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11640 tf_warning_or_error);
11641 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11642 iter_decl, auto_node);
11647 /* Converts a range-based for-statement into a normal
11648 for-statement, as per the definition.
11650 for (RANGE_DECL : RANGE_EXPR)
11651 BLOCK
11653 should be equivalent to:
11656 auto &&__range = RANGE_EXPR;
11657 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11658 __begin != __end;
11659 ++__begin)
11661 RANGE_DECL = *__begin;
11662 BLOCK
11666 If RANGE_EXPR is an array:
11667 BEGIN_EXPR = __range
11668 END_EXPR = __range + ARRAY_SIZE(__range)
11669 Else if RANGE_EXPR has a member 'begin' or 'end':
11670 BEGIN_EXPR = __range.begin()
11671 END_EXPR = __range.end()
11672 Else:
11673 BEGIN_EXPR = begin(__range)
11674 END_EXPR = end(__range);
11676 If __range has a member 'begin' but not 'end', or vice versa, we must
11677 still use the second alternative (it will surely fail, however).
11678 When calling begin()/end() in the third alternative we must use
11679 argument dependent lookup, but always considering 'std' as an associated
11680 namespace. */
11682 tree
11683 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11684 tree decomp_first_name, unsigned int decomp_cnt,
11685 bool ivdep)
11687 tree begin, end;
11688 tree iter_type, begin_expr, end_expr;
11689 tree condition, expression;
11691 if (range_decl == error_mark_node || range_expr == error_mark_node)
11692 /* If an error happened previously do nothing or else a lot of
11693 unhelpful errors would be issued. */
11694 begin_expr = end_expr = iter_type = error_mark_node;
11695 else
11697 tree range_temp;
11699 if (VAR_P (range_expr)
11700 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11701 /* Can't bind a reference to an array of runtime bound. */
11702 range_temp = range_expr;
11703 else
11705 range_temp = build_range_temp (range_expr);
11706 pushdecl (range_temp);
11707 cp_finish_decl (range_temp, range_expr,
11708 /*is_constant_init*/false, NULL_TREE,
11709 LOOKUP_ONLYCONVERTING);
11710 range_temp = convert_from_reference (range_temp);
11712 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11713 &begin_expr, &end_expr);
11716 /* The new for initialization statement. */
11717 begin = build_decl (input_location, VAR_DECL,
11718 get_identifier ("__for_begin"), iter_type);
11719 TREE_USED (begin) = 1;
11720 DECL_ARTIFICIAL (begin) = 1;
11721 pushdecl (begin);
11722 cp_finish_decl (begin, begin_expr,
11723 /*is_constant_init*/false, NULL_TREE,
11724 LOOKUP_ONLYCONVERTING);
11726 if (cxx_dialect >= cxx1z)
11727 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11728 end = build_decl (input_location, VAR_DECL,
11729 get_identifier ("__for_end"), iter_type);
11730 TREE_USED (end) = 1;
11731 DECL_ARTIFICIAL (end) = 1;
11732 pushdecl (end);
11733 cp_finish_decl (end, end_expr,
11734 /*is_constant_init*/false, NULL_TREE,
11735 LOOKUP_ONLYCONVERTING);
11737 finish_init_stmt (statement);
11739 /* The new for condition. */
11740 condition = build_x_binary_op (input_location, NE_EXPR,
11741 begin, ERROR_MARK,
11742 end, ERROR_MARK,
11743 NULL, tf_warning_or_error);
11744 finish_for_cond (condition, statement, ivdep);
11746 /* The new increment expression. */
11747 expression = finish_unary_op_expr (input_location,
11748 PREINCREMENT_EXPR, begin,
11749 tf_warning_or_error);
11750 finish_for_expr (expression, statement);
11752 /* The declaration is initialized with *__begin inside the loop body. */
11753 cp_finish_decl (range_decl,
11754 build_x_indirect_ref (input_location, begin, RO_NULL,
11755 tf_warning_or_error),
11756 /*is_constant_init*/false, NULL_TREE,
11757 LOOKUP_ONLYCONVERTING);
11758 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11759 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11761 return statement;
11764 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11765 We need to solve both at the same time because the method used
11766 depends on the existence of members begin or end.
11767 Returns the type deduced for the iterator expression. */
11769 static tree
11770 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11772 if (error_operand_p (range))
11774 *begin = *end = error_mark_node;
11775 return error_mark_node;
11778 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11780 error ("range-based %<for%> expression of type %qT "
11781 "has incomplete type", TREE_TYPE (range));
11782 *begin = *end = error_mark_node;
11783 return error_mark_node;
11785 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11787 /* If RANGE is an array, we will use pointer arithmetic. */
11788 *begin = decay_conversion (range, tf_warning_or_error);
11789 *end = build_binary_op (input_location, PLUS_EXPR,
11790 range,
11791 array_type_nelts_top (TREE_TYPE (range)),
11793 return TREE_TYPE (*begin);
11795 else
11797 /* If it is not an array, we must do a bit of magic. */
11798 tree id_begin, id_end;
11799 tree member_begin, member_end;
11801 *begin = *end = error_mark_node;
11803 id_begin = get_identifier ("begin");
11804 id_end = get_identifier ("end");
11805 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11806 /*protect=*/2, /*want_type=*/false,
11807 tf_warning_or_error);
11808 member_end = lookup_member (TREE_TYPE (range), id_end,
11809 /*protect=*/2, /*want_type=*/false,
11810 tf_warning_or_error);
11812 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11814 /* Use the member functions. */
11815 if (member_begin != NULL_TREE)
11816 *begin = cp_parser_range_for_member_function (range, id_begin);
11817 else
11818 error ("range-based %<for%> expression of type %qT has an "
11819 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11821 if (member_end != NULL_TREE)
11822 *end = cp_parser_range_for_member_function (range, id_end);
11823 else
11824 error ("range-based %<for%> expression of type %qT has a "
11825 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11827 else
11829 /* Use global functions with ADL. */
11830 vec<tree, va_gc> *vec;
11831 vec = make_tree_vector ();
11833 vec_safe_push (vec, range);
11835 member_begin = perform_koenig_lookup (id_begin, vec,
11836 tf_warning_or_error);
11837 *begin = finish_call_expr (member_begin, &vec, false, true,
11838 tf_warning_or_error);
11839 member_end = perform_koenig_lookup (id_end, vec,
11840 tf_warning_or_error);
11841 *end = finish_call_expr (member_end, &vec, false, true,
11842 tf_warning_or_error);
11844 release_tree_vector (vec);
11847 /* Last common checks. */
11848 if (*begin == error_mark_node || *end == error_mark_node)
11850 /* If one of the expressions is an error do no more checks. */
11851 *begin = *end = error_mark_node;
11852 return error_mark_node;
11854 else if (type_dependent_expression_p (*begin)
11855 || type_dependent_expression_p (*end))
11856 /* Can happen, when, eg, in a template context, Koenig lookup
11857 can't resolve begin/end (c++/58503). */
11858 return NULL_TREE;
11859 else
11861 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11862 /* The unqualified type of the __begin and __end temporaries should
11863 be the same, as required by the multiple auto declaration. */
11864 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11866 if (cxx_dialect >= cxx1z
11867 && (build_x_binary_op (input_location, NE_EXPR,
11868 *begin, ERROR_MARK,
11869 *end, ERROR_MARK,
11870 NULL, tf_none)
11871 != error_mark_node))
11872 /* P0184R0 allows __begin and __end to have different types,
11873 but make sure they are comparable so we can give a better
11874 diagnostic. */;
11875 else
11876 error ("inconsistent begin/end types in range-based %<for%> "
11877 "statement: %qT and %qT",
11878 TREE_TYPE (*begin), TREE_TYPE (*end));
11880 return iter_type;
11885 /* Helper function for cp_parser_perform_range_for_lookup.
11886 Builds a tree for RANGE.IDENTIFIER(). */
11888 static tree
11889 cp_parser_range_for_member_function (tree range, tree identifier)
11891 tree member, res;
11892 vec<tree, va_gc> *vec;
11894 member = finish_class_member_access_expr (range, identifier,
11895 false, tf_warning_or_error);
11896 if (member == error_mark_node)
11897 return error_mark_node;
11899 vec = make_tree_vector ();
11900 res = finish_call_expr (member, &vec,
11901 /*disallow_virtual=*/false,
11902 /*koenig_p=*/false,
11903 tf_warning_or_error);
11904 release_tree_vector (vec);
11905 return res;
11908 /* Parse an iteration-statement.
11910 iteration-statement:
11911 while ( condition ) statement
11912 do statement while ( expression ) ;
11913 for ( init-statement condition [opt] ; expression [opt] )
11914 statement
11916 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11918 static tree
11919 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11921 cp_token *token;
11922 enum rid keyword;
11923 tree statement;
11924 unsigned char in_statement;
11925 token_indent_info guard_tinfo;
11927 /* Peek at the next token. */
11928 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11929 if (!token)
11930 return error_mark_node;
11932 guard_tinfo = get_token_indent_info (token);
11934 /* Remember whether or not we are already within an iteration
11935 statement. */
11936 in_statement = parser->in_statement;
11938 /* See what kind of keyword it is. */
11939 keyword = token->keyword;
11940 switch (keyword)
11942 case RID_WHILE:
11944 tree condition;
11946 /* Begin the while-statement. */
11947 statement = begin_while_stmt ();
11948 /* Look for the `('. */
11949 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11950 /* Parse the condition. */
11951 condition = cp_parser_condition (parser);
11952 finish_while_stmt_cond (condition, statement, ivdep);
11953 /* Look for the `)'. */
11954 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11955 /* Parse the dependent statement. */
11956 parser->in_statement = IN_ITERATION_STMT;
11957 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11958 parser->in_statement = in_statement;
11959 /* We're done with the while-statement. */
11960 finish_while_stmt (statement);
11962 break;
11964 case RID_DO:
11966 tree expression;
11968 /* Begin the do-statement. */
11969 statement = begin_do_stmt ();
11970 /* Parse the body of the do-statement. */
11971 parser->in_statement = IN_ITERATION_STMT;
11972 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11973 parser->in_statement = in_statement;
11974 finish_do_body (statement);
11975 /* Look for the `while' keyword. */
11976 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11977 /* Look for the `('. */
11978 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11979 /* Parse the expression. */
11980 expression = cp_parser_expression (parser);
11981 /* We're done with the do-statement. */
11982 finish_do_stmt (expression, statement, ivdep);
11983 /* Look for the `)'. */
11984 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11985 /* Look for the `;'. */
11986 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11988 break;
11990 case RID_FOR:
11992 /* Look for the `('. */
11993 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11995 statement = cp_parser_for (parser, ivdep);
11997 /* Look for the `)'. */
11998 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12000 /* Parse the body of the for-statement. */
12001 parser->in_statement = IN_ITERATION_STMT;
12002 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12003 parser->in_statement = in_statement;
12005 /* We're done with the for-statement. */
12006 finish_for_stmt (statement);
12008 break;
12010 default:
12011 cp_parser_error (parser, "expected iteration-statement");
12012 statement = error_mark_node;
12013 break;
12016 return statement;
12019 /* Parse a init-statement or the declarator of a range-based-for.
12020 Returns true if a range-based-for declaration is seen.
12022 init-statement:
12023 expression-statement
12024 simple-declaration */
12026 static bool
12027 cp_parser_init_statement (cp_parser* parser, tree *decl)
12029 /* If the next token is a `;', then we have an empty
12030 expression-statement. Grammatically, this is also a
12031 simple-declaration, but an invalid one, because it does not
12032 declare anything. Therefore, if we did not handle this case
12033 specially, we would issue an error message about an invalid
12034 declaration. */
12035 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12037 bool is_range_for = false;
12038 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12040 /* A colon is used in range-based for. */
12041 parser->colon_corrects_to_scope_p = false;
12043 /* We're going to speculatively look for a declaration, falling back
12044 to an expression, if necessary. */
12045 cp_parser_parse_tentatively (parser);
12046 /* Parse the declaration. */
12047 cp_parser_simple_declaration (parser,
12048 /*function_definition_allowed_p=*/false,
12049 decl);
12050 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12051 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12053 /* It is a range-for, consume the ':' */
12054 cp_lexer_consume_token (parser->lexer);
12055 is_range_for = true;
12056 if (cxx_dialect < cxx11)
12058 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12059 "range-based %<for%> loops only available with "
12060 "-std=c++11 or -std=gnu++11");
12061 *decl = error_mark_node;
12064 else
12065 /* The ';' is not consumed yet because we told
12066 cp_parser_simple_declaration not to. */
12067 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12069 if (cp_parser_parse_definitely (parser))
12070 return is_range_for;
12071 /* If the tentative parse failed, then we shall need to look for an
12072 expression-statement. */
12074 /* If we are here, it is an expression-statement. */
12075 cp_parser_expression_statement (parser, NULL_TREE);
12076 return false;
12079 /* Parse a jump-statement.
12081 jump-statement:
12082 break ;
12083 continue ;
12084 return expression [opt] ;
12085 return braced-init-list ;
12086 goto identifier ;
12088 GNU extension:
12090 jump-statement:
12091 goto * expression ;
12093 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12095 static tree
12096 cp_parser_jump_statement (cp_parser* parser)
12098 tree statement = error_mark_node;
12099 cp_token *token;
12100 enum rid keyword;
12101 unsigned char in_statement;
12103 /* Peek at the next token. */
12104 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12105 if (!token)
12106 return error_mark_node;
12108 /* See what kind of keyword it is. */
12109 keyword = token->keyword;
12110 switch (keyword)
12112 case RID_BREAK:
12113 in_statement = parser->in_statement & ~IN_IF_STMT;
12114 switch (in_statement)
12116 case 0:
12117 error_at (token->location, "break statement not within loop or switch");
12118 break;
12119 default:
12120 gcc_assert ((in_statement & IN_SWITCH_STMT)
12121 || in_statement == IN_ITERATION_STMT);
12122 statement = finish_break_stmt ();
12123 if (in_statement == IN_ITERATION_STMT)
12124 break_maybe_infinite_loop ();
12125 break;
12126 case IN_OMP_BLOCK:
12127 error_at (token->location, "invalid exit from OpenMP structured block");
12128 break;
12129 case IN_OMP_FOR:
12130 error_at (token->location, "break statement used with OpenMP for loop");
12131 break;
12132 case IN_CILK_SIMD_FOR:
12133 error_at (token->location, "break statement used with Cilk Plus for loop");
12134 break;
12136 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12137 break;
12139 case RID_CONTINUE:
12140 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12142 case 0:
12143 error_at (token->location, "continue statement not within a loop");
12144 break;
12145 case IN_CILK_SIMD_FOR:
12146 error_at (token->location,
12147 "continue statement within %<#pragma simd%> loop body");
12148 /* Fall through. */
12149 case IN_ITERATION_STMT:
12150 case IN_OMP_FOR:
12151 statement = finish_continue_stmt ();
12152 break;
12153 case IN_OMP_BLOCK:
12154 error_at (token->location, "invalid exit from OpenMP structured block");
12155 break;
12156 default:
12157 gcc_unreachable ();
12159 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12160 break;
12162 case RID_RETURN:
12164 tree expr;
12165 bool expr_non_constant_p;
12167 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12169 cp_lexer_set_source_position (parser->lexer);
12170 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12171 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12173 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12174 expr = cp_parser_expression (parser);
12175 else
12176 /* If the next token is a `;', then there is no
12177 expression. */
12178 expr = NULL_TREE;
12179 /* Build the return-statement. */
12180 if (current_function_auto_return_pattern && in_discarded_stmt)
12181 /* Don't deduce from a discarded return statement. */;
12182 else
12183 statement = finish_return_stmt (expr);
12184 /* Look for the final `;'. */
12185 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12187 break;
12189 case RID_GOTO:
12190 if (parser->in_function_body
12191 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12193 error ("%<goto%> in %<constexpr%> function");
12194 cp_function_chain->invalid_constexpr = true;
12197 /* Create the goto-statement. */
12198 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12200 /* Issue a warning about this use of a GNU extension. */
12201 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12202 /* Consume the '*' token. */
12203 cp_lexer_consume_token (parser->lexer);
12204 /* Parse the dependent expression. */
12205 finish_goto_stmt (cp_parser_expression (parser));
12207 else
12208 finish_goto_stmt (cp_parser_identifier (parser));
12209 /* Look for the final `;'. */
12210 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12211 break;
12213 default:
12214 cp_parser_error (parser, "expected jump-statement");
12215 break;
12218 return statement;
12221 /* Parse a declaration-statement.
12223 declaration-statement:
12224 block-declaration */
12226 static void
12227 cp_parser_declaration_statement (cp_parser* parser)
12229 void *p;
12231 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12232 p = obstack_alloc (&declarator_obstack, 0);
12234 /* Parse the block-declaration. */
12235 cp_parser_block_declaration (parser, /*statement_p=*/true);
12237 /* Free any declarators allocated. */
12238 obstack_free (&declarator_obstack, p);
12241 /* Some dependent statements (like `if (cond) statement'), are
12242 implicitly in their own scope. In other words, if the statement is
12243 a single statement (as opposed to a compound-statement), it is
12244 none-the-less treated as if it were enclosed in braces. Any
12245 declarations appearing in the dependent statement are out of scope
12246 after control passes that point. This function parses a statement,
12247 but ensures that is in its own scope, even if it is not a
12248 compound-statement.
12250 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12251 is a (possibly labeled) if statement which is not enclosed in
12252 braces and has an else clause. This is used to implement
12253 -Wparentheses.
12255 CHAIN is a vector of if-else-if conditions. This is used to implement
12256 -Wduplicated-cond.
12258 Returns the new statement. */
12260 static tree
12261 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12262 const token_indent_info &guard_tinfo,
12263 vec<tree> *chain)
12265 tree statement;
12266 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12267 token_indent_info body_tinfo
12268 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12270 if (if_p != NULL)
12271 *if_p = false;
12273 /* Mark if () ; with a special NOP_EXPR. */
12274 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12276 cp_lexer_consume_token (parser->lexer);
12277 statement = add_stmt (build_empty_stmt (body_loc));
12279 if (guard_tinfo.keyword == RID_IF
12280 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12281 warning_at (body_loc, OPT_Wempty_body,
12282 "suggest braces around empty body in an %<if%> statement");
12283 else if (guard_tinfo.keyword == RID_ELSE)
12284 warning_at (body_loc, OPT_Wempty_body,
12285 "suggest braces around empty body in an %<else%> statement");
12287 /* if a compound is opened, we simply parse the statement directly. */
12288 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12289 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12290 /* If the token is not a `{', then we must take special action. */
12291 else
12293 /* Create a compound-statement. */
12294 statement = begin_compound_stmt (0);
12295 /* Parse the dependent-statement. */
12296 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
12297 /* Finish the dummy compound-statement. */
12298 finish_compound_stmt (statement);
12301 token_indent_info next_tinfo
12302 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12303 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12305 /* Return the statement. */
12306 return statement;
12309 /* For some dependent statements (like `while (cond) statement'), we
12310 have already created a scope. Therefore, even if the dependent
12311 statement is a compound-statement, we do not want to create another
12312 scope. */
12314 static void
12315 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12316 const token_indent_info &guard_tinfo)
12318 /* If the token is a `{', then we must take special action. */
12319 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12321 token_indent_info body_tinfo
12322 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12324 cp_parser_statement (parser, NULL_TREE, false, if_p);
12325 token_indent_info next_tinfo
12326 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12327 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12329 else
12331 /* Avoid calling cp_parser_compound_statement, so that we
12332 don't create a new scope. Do everything else by hand. */
12333 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12334 /* If the next keyword is `__label__' we have a label declaration. */
12335 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12336 cp_parser_label_declaration (parser);
12337 /* Parse an (optional) statement-seq. */
12338 cp_parser_statement_seq_opt (parser, NULL_TREE);
12339 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12343 /* Declarations [gram.dcl.dcl] */
12345 /* Parse an optional declaration-sequence.
12347 declaration-seq:
12348 declaration
12349 declaration-seq declaration */
12351 static void
12352 cp_parser_declaration_seq_opt (cp_parser* parser)
12354 while (true)
12356 cp_token *token;
12358 token = cp_lexer_peek_token (parser->lexer);
12360 if (token->type == CPP_CLOSE_BRACE
12361 || token->type == CPP_EOF
12362 || token->type == CPP_PRAGMA_EOL)
12363 break;
12365 if (token->type == CPP_SEMICOLON)
12367 /* A declaration consisting of a single semicolon is
12368 invalid. Allow it unless we're being pedantic. */
12369 cp_lexer_consume_token (parser->lexer);
12370 if (!in_system_header_at (input_location))
12371 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12372 continue;
12375 /* If we're entering or exiting a region that's implicitly
12376 extern "C", modify the lang context appropriately. */
12377 if (!parser->implicit_extern_c && token->implicit_extern_c)
12379 push_lang_context (lang_name_c);
12380 parser->implicit_extern_c = true;
12382 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12384 pop_lang_context ();
12385 parser->implicit_extern_c = false;
12388 if (token->type == CPP_PRAGMA)
12390 /* A top-level declaration can consist solely of a #pragma.
12391 A nested declaration cannot, so this is done here and not
12392 in cp_parser_declaration. (A #pragma at block scope is
12393 handled in cp_parser_statement.) */
12394 cp_parser_pragma (parser, pragma_external, NULL);
12395 continue;
12398 /* Parse the declaration itself. */
12399 cp_parser_declaration (parser);
12403 /* Parse a declaration.
12405 declaration:
12406 block-declaration
12407 function-definition
12408 template-declaration
12409 explicit-instantiation
12410 explicit-specialization
12411 linkage-specification
12412 namespace-definition
12414 C++17:
12415 deduction-guide
12417 GNU extension:
12419 declaration:
12420 __extension__ declaration */
12422 static void
12423 cp_parser_declaration (cp_parser* parser)
12425 cp_token token1;
12426 cp_token token2;
12427 int saved_pedantic;
12428 void *p;
12429 tree attributes = NULL_TREE;
12431 /* Check for the `__extension__' keyword. */
12432 if (cp_parser_extension_opt (parser, &saved_pedantic))
12434 /* Parse the qualified declaration. */
12435 cp_parser_declaration (parser);
12436 /* Restore the PEDANTIC flag. */
12437 pedantic = saved_pedantic;
12439 return;
12442 /* Try to figure out what kind of declaration is present. */
12443 token1 = *cp_lexer_peek_token (parser->lexer);
12445 if (token1.type != CPP_EOF)
12446 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12447 else
12449 token2.type = CPP_EOF;
12450 token2.keyword = RID_MAX;
12453 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12454 p = obstack_alloc (&declarator_obstack, 0);
12456 /* If the next token is `extern' and the following token is a string
12457 literal, then we have a linkage specification. */
12458 if (token1.keyword == RID_EXTERN
12459 && cp_parser_is_pure_string_literal (&token2))
12460 cp_parser_linkage_specification (parser);
12461 /* If the next token is `template', then we have either a template
12462 declaration, an explicit instantiation, or an explicit
12463 specialization. */
12464 else if (token1.keyword == RID_TEMPLATE)
12466 /* `template <>' indicates a template specialization. */
12467 if (token2.type == CPP_LESS
12468 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12469 cp_parser_explicit_specialization (parser);
12470 /* `template <' indicates a template declaration. */
12471 else if (token2.type == CPP_LESS)
12472 cp_parser_template_declaration (parser, /*member_p=*/false);
12473 /* Anything else must be an explicit instantiation. */
12474 else
12475 cp_parser_explicit_instantiation (parser);
12477 /* If the next token is `export', then we have a template
12478 declaration. */
12479 else if (token1.keyword == RID_EXPORT)
12480 cp_parser_template_declaration (parser, /*member_p=*/false);
12481 /* If the next token is `extern', 'static' or 'inline' and the one
12482 after that is `template', we have a GNU extended explicit
12483 instantiation directive. */
12484 else if (cp_parser_allow_gnu_extensions_p (parser)
12485 && (token1.keyword == RID_EXTERN
12486 || token1.keyword == RID_STATIC
12487 || token1.keyword == RID_INLINE)
12488 && token2.keyword == RID_TEMPLATE)
12489 cp_parser_explicit_instantiation (parser);
12490 /* If the next token is `namespace', check for a named or unnamed
12491 namespace definition. */
12492 else if (token1.keyword == RID_NAMESPACE
12493 && (/* A named namespace definition. */
12494 (token2.type == CPP_NAME
12495 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12496 != CPP_EQ))
12497 || (token2.type == CPP_OPEN_SQUARE
12498 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12499 == CPP_OPEN_SQUARE)
12500 /* An unnamed namespace definition. */
12501 || token2.type == CPP_OPEN_BRACE
12502 || token2.keyword == RID_ATTRIBUTE))
12503 cp_parser_namespace_definition (parser);
12504 /* An inline (associated) namespace definition. */
12505 else if (token1.keyword == RID_INLINE
12506 && token2.keyword == RID_NAMESPACE)
12507 cp_parser_namespace_definition (parser);
12508 /* Objective-C++ declaration/definition. */
12509 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12510 cp_parser_objc_declaration (parser, NULL_TREE);
12511 else if (c_dialect_objc ()
12512 && token1.keyword == RID_ATTRIBUTE
12513 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12514 cp_parser_objc_declaration (parser, attributes);
12515 /* At this point we may have a template declared by a concept
12516 introduction. */
12517 else if (flag_concepts
12518 && cp_parser_template_declaration_after_export (parser,
12519 /*member_p=*/false))
12520 /* We did. */;
12521 else
12522 /* Try to parse a block-declaration, or a function-definition. */
12523 cp_parser_block_declaration (parser, /*statement_p=*/false);
12525 /* Free any declarators allocated. */
12526 obstack_free (&declarator_obstack, p);
12529 /* Parse a block-declaration.
12531 block-declaration:
12532 simple-declaration
12533 asm-definition
12534 namespace-alias-definition
12535 using-declaration
12536 using-directive
12538 GNU Extension:
12540 block-declaration:
12541 __extension__ block-declaration
12543 C++0x Extension:
12545 block-declaration:
12546 static_assert-declaration
12548 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12549 part of a declaration-statement. */
12551 static void
12552 cp_parser_block_declaration (cp_parser *parser,
12553 bool statement_p)
12555 cp_token *token1;
12556 int saved_pedantic;
12558 /* Check for the `__extension__' keyword. */
12559 if (cp_parser_extension_opt (parser, &saved_pedantic))
12561 /* Parse the qualified declaration. */
12562 cp_parser_block_declaration (parser, statement_p);
12563 /* Restore the PEDANTIC flag. */
12564 pedantic = saved_pedantic;
12566 return;
12569 /* Peek at the next token to figure out which kind of declaration is
12570 present. */
12571 token1 = cp_lexer_peek_token (parser->lexer);
12573 /* If the next keyword is `asm', we have an asm-definition. */
12574 if (token1->keyword == RID_ASM)
12576 if (statement_p)
12577 cp_parser_commit_to_tentative_parse (parser);
12578 cp_parser_asm_definition (parser);
12580 /* If the next keyword is `namespace', we have a
12581 namespace-alias-definition. */
12582 else if (token1->keyword == RID_NAMESPACE)
12583 cp_parser_namespace_alias_definition (parser);
12584 /* If the next keyword is `using', we have a
12585 using-declaration, a using-directive, or an alias-declaration. */
12586 else if (token1->keyword == RID_USING)
12588 cp_token *token2;
12590 if (statement_p)
12591 cp_parser_commit_to_tentative_parse (parser);
12592 /* If the token after `using' is `namespace', then we have a
12593 using-directive. */
12594 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12595 if (token2->keyword == RID_NAMESPACE)
12596 cp_parser_using_directive (parser);
12597 /* If the second token after 'using' is '=', then we have an
12598 alias-declaration. */
12599 else if (cxx_dialect >= cxx11
12600 && token2->type == CPP_NAME
12601 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12602 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12603 cp_parser_alias_declaration (parser);
12604 /* Otherwise, it's a using-declaration. */
12605 else
12606 cp_parser_using_declaration (parser,
12607 /*access_declaration_p=*/false);
12609 /* If the next keyword is `__label__' we have a misplaced label
12610 declaration. */
12611 else if (token1->keyword == RID_LABEL)
12613 cp_lexer_consume_token (parser->lexer);
12614 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12615 cp_parser_skip_to_end_of_statement (parser);
12616 /* If the next token is now a `;', consume it. */
12617 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12618 cp_lexer_consume_token (parser->lexer);
12620 /* If the next token is `static_assert' we have a static assertion. */
12621 else if (token1->keyword == RID_STATIC_ASSERT)
12622 cp_parser_static_assert (parser, /*member_p=*/false);
12623 /* Anything else must be a simple-declaration. */
12624 else
12625 cp_parser_simple_declaration (parser, !statement_p,
12626 /*maybe_range_for_decl*/NULL);
12629 /* Parse a simple-declaration.
12631 simple-declaration:
12632 decl-specifier-seq [opt] init-declarator-list [opt] ;
12633 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12634 brace-or-equal-initializer ;
12636 init-declarator-list:
12637 init-declarator
12638 init-declarator-list , init-declarator
12640 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12641 function-definition as a simple-declaration.
12643 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12644 parsed declaration if it is an uninitialized single declarator not followed
12645 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12646 if present, will not be consumed. */
12648 static void
12649 cp_parser_simple_declaration (cp_parser* parser,
12650 bool function_definition_allowed_p,
12651 tree *maybe_range_for_decl)
12653 cp_decl_specifier_seq decl_specifiers;
12654 int declares_class_or_enum;
12655 bool saw_declarator;
12656 location_t comma_loc = UNKNOWN_LOCATION;
12657 location_t init_loc = UNKNOWN_LOCATION;
12659 if (maybe_range_for_decl)
12660 *maybe_range_for_decl = NULL_TREE;
12662 /* Defer access checks until we know what is being declared; the
12663 checks for names appearing in the decl-specifier-seq should be
12664 done as if we were in the scope of the thing being declared. */
12665 push_deferring_access_checks (dk_deferred);
12667 /* Parse the decl-specifier-seq. We have to keep track of whether
12668 or not the decl-specifier-seq declares a named class or
12669 enumeration type, since that is the only case in which the
12670 init-declarator-list is allowed to be empty.
12672 [dcl.dcl]
12674 In a simple-declaration, the optional init-declarator-list can be
12675 omitted only when declaring a class or enumeration, that is when
12676 the decl-specifier-seq contains either a class-specifier, an
12677 elaborated-type-specifier, or an enum-specifier. */
12678 cp_parser_decl_specifier_seq (parser,
12679 CP_PARSER_FLAGS_OPTIONAL,
12680 &decl_specifiers,
12681 &declares_class_or_enum);
12682 /* We no longer need to defer access checks. */
12683 stop_deferring_access_checks ();
12685 /* In a block scope, a valid declaration must always have a
12686 decl-specifier-seq. By not trying to parse declarators, we can
12687 resolve the declaration/expression ambiguity more quickly. */
12688 if (!function_definition_allowed_p
12689 && !decl_specifiers.any_specifiers_p)
12691 cp_parser_error (parser, "expected declaration");
12692 goto done;
12695 /* If the next two tokens are both identifiers, the code is
12696 erroneous. The usual cause of this situation is code like:
12698 T t;
12700 where "T" should name a type -- but does not. */
12701 if (!decl_specifiers.any_type_specifiers_p
12702 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12704 /* If parsing tentatively, we should commit; we really are
12705 looking at a declaration. */
12706 cp_parser_commit_to_tentative_parse (parser);
12707 /* Give up. */
12708 goto done;
12711 /* If we have seen at least one decl-specifier, and the next token
12712 is not a parenthesis, then we must be looking at a declaration.
12713 (After "int (" we might be looking at a functional cast.) */
12714 if (decl_specifiers.any_specifiers_p
12715 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12716 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12717 && !cp_parser_error_occurred (parser))
12718 cp_parser_commit_to_tentative_parse (parser);
12720 /* Look for C++17 decomposition declaration. */
12721 for (size_t n = 1; ; n++)
12722 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12723 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12724 continue;
12725 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12726 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12727 && decl_specifiers.any_specifiers_p)
12729 tree decl
12730 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12731 maybe_range_for_decl,
12732 &init_loc);
12734 /* The next token should be either a `,' or a `;'. */
12735 cp_token *token = cp_lexer_peek_token (parser->lexer);
12736 /* If it's a `;', we are done. */
12737 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12738 goto finish;
12739 /* Anything else is an error. */
12740 else
12742 /* If we have already issued an error message we don't need
12743 to issue another one. */
12744 if ((decl != error_mark_node
12745 && DECL_INITIAL (decl) != error_mark_node)
12746 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12747 cp_parser_error (parser, "expected %<,%> or %<;%>");
12748 /* Skip tokens until we reach the end of the statement. */
12749 cp_parser_skip_to_end_of_statement (parser);
12750 /* If the next token is now a `;', consume it. */
12751 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12752 cp_lexer_consume_token (parser->lexer);
12753 goto done;
12756 else
12757 break;
12759 tree last_type;
12760 bool auto_specifier_p;
12761 /* NULL_TREE if both variable and function declaration are allowed,
12762 error_mark_node if function declaration are not allowed and
12763 a FUNCTION_DECL that should be diagnosed if it is followed by
12764 variable declarations. */
12765 tree auto_function_declaration;
12767 last_type = NULL_TREE;
12768 auto_specifier_p
12769 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12770 auto_function_declaration = NULL_TREE;
12772 /* Keep going until we hit the `;' at the end of the simple
12773 declaration. */
12774 saw_declarator = false;
12775 while (cp_lexer_next_token_is_not (parser->lexer,
12776 CPP_SEMICOLON))
12778 cp_token *token;
12779 bool function_definition_p;
12780 tree decl;
12781 tree auto_result = NULL_TREE;
12783 if (saw_declarator)
12785 /* If we are processing next declarator, comma is expected */
12786 token = cp_lexer_peek_token (parser->lexer);
12787 gcc_assert (token->type == CPP_COMMA);
12788 cp_lexer_consume_token (parser->lexer);
12789 if (maybe_range_for_decl)
12791 *maybe_range_for_decl = error_mark_node;
12792 if (comma_loc == UNKNOWN_LOCATION)
12793 comma_loc = token->location;
12796 else
12797 saw_declarator = true;
12799 /* Parse the init-declarator. */
12800 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12801 /*checks=*/NULL,
12802 function_definition_allowed_p,
12803 /*member_p=*/false,
12804 declares_class_or_enum,
12805 &function_definition_p,
12806 maybe_range_for_decl,
12807 &init_loc,
12808 &auto_result);
12809 /* If an error occurred while parsing tentatively, exit quickly.
12810 (That usually happens when in the body of a function; each
12811 statement is treated as a declaration-statement until proven
12812 otherwise.) */
12813 if (cp_parser_error_occurred (parser))
12814 goto done;
12816 if (auto_specifier_p && cxx_dialect >= cxx14)
12818 /* If the init-declarator-list contains more than one
12819 init-declarator, they shall all form declarations of
12820 variables. */
12821 if (auto_function_declaration == NULL_TREE)
12822 auto_function_declaration
12823 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
12824 else if (TREE_CODE (decl) == FUNCTION_DECL
12825 || auto_function_declaration != error_mark_node)
12827 error_at (decl_specifiers.locations[ds_type_spec],
12828 "non-variable %qD in declaration with more than one "
12829 "declarator with placeholder type",
12830 TREE_CODE (decl) == FUNCTION_DECL
12831 ? decl : auto_function_declaration);
12832 auto_function_declaration = error_mark_node;
12836 if (auto_result
12837 && (!processing_template_decl || !type_uses_auto (auto_result)))
12839 if (last_type
12840 && last_type != error_mark_node
12841 && !same_type_p (auto_result, last_type))
12843 /* If the list of declarators contains more than one declarator,
12844 the type of each declared variable is determined as described
12845 above. If the type deduced for the template parameter U is not
12846 the same in each deduction, the program is ill-formed. */
12847 error_at (decl_specifiers.locations[ds_type_spec],
12848 "inconsistent deduction for %qT: %qT and then %qT",
12849 decl_specifiers.type, last_type, auto_result);
12850 last_type = error_mark_node;
12852 else
12853 last_type = auto_result;
12856 /* Handle function definitions specially. */
12857 if (function_definition_p)
12859 /* If the next token is a `,', then we are probably
12860 processing something like:
12862 void f() {}, *p;
12864 which is erroneous. */
12865 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12867 cp_token *token = cp_lexer_peek_token (parser->lexer);
12868 error_at (token->location,
12869 "mixing"
12870 " declarations and function-definitions is forbidden");
12872 /* Otherwise, we're done with the list of declarators. */
12873 else
12875 pop_deferring_access_checks ();
12876 return;
12879 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12880 *maybe_range_for_decl = decl;
12881 /* The next token should be either a `,' or a `;'. */
12882 token = cp_lexer_peek_token (parser->lexer);
12883 /* If it's a `,', there are more declarators to come. */
12884 if (token->type == CPP_COMMA)
12885 /* will be consumed next time around */;
12886 /* If it's a `;', we are done. */
12887 else if (token->type == CPP_SEMICOLON)
12888 break;
12889 else if (maybe_range_for_decl)
12891 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
12892 permerror (decl_specifiers.locations[ds_type_spec],
12893 "types may not be defined in a for-range-declaration");
12894 break;
12896 /* Anything else is an error. */
12897 else
12899 /* If we have already issued an error message we don't need
12900 to issue another one. */
12901 if ((decl != error_mark_node
12902 && DECL_INITIAL (decl) != error_mark_node)
12903 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12904 cp_parser_error (parser, "expected %<,%> or %<;%>");
12905 /* Skip tokens until we reach the end of the statement. */
12906 cp_parser_skip_to_end_of_statement (parser);
12907 /* If the next token is now a `;', consume it. */
12908 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12909 cp_lexer_consume_token (parser->lexer);
12910 goto done;
12912 /* After the first time around, a function-definition is not
12913 allowed -- even if it was OK at first. For example:
12915 int i, f() {}
12917 is not valid. */
12918 function_definition_allowed_p = false;
12921 /* Issue an error message if no declarators are present, and the
12922 decl-specifier-seq does not itself declare a class or
12923 enumeration: [dcl.dcl]/3. */
12924 if (!saw_declarator)
12926 if (cp_parser_declares_only_class_p (parser))
12928 if (!declares_class_or_enum
12929 && decl_specifiers.type
12930 && OVERLOAD_TYPE_P (decl_specifiers.type))
12931 /* Ensure an error is issued anyway when finish_decltype_type,
12932 called via cp_parser_decl_specifier_seq, returns a class or
12933 an enumeration (c++/51786). */
12934 decl_specifiers.type = NULL_TREE;
12935 shadow_tag (&decl_specifiers);
12937 /* Perform any deferred access checks. */
12938 perform_deferred_access_checks (tf_warning_or_error);
12941 /* Consume the `;'. */
12942 finish:
12943 if (!maybe_range_for_decl)
12944 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12945 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12947 if (init_loc != UNKNOWN_LOCATION)
12948 error_at (init_loc, "initializer in range-based %<for%> loop");
12949 if (comma_loc != UNKNOWN_LOCATION)
12950 error_at (comma_loc,
12951 "multiple declarations in range-based %<for%> loop");
12954 done:
12955 pop_deferring_access_checks ();
12958 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
12959 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12960 initializer ; */
12962 static tree
12963 cp_parser_decomposition_declaration (cp_parser *parser,
12964 cp_decl_specifier_seq *decl_specifiers,
12965 tree *maybe_range_for_decl,
12966 location_t *init_loc)
12968 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
12969 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12970 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
12972 /* Parse the identifier-list. */
12973 auto_vec<cp_expr, 10> v;
12974 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12975 while (true)
12977 cp_expr e = cp_parser_identifier (parser);
12978 if (e.get_value () == error_mark_node)
12979 break;
12980 v.safe_push (e);
12981 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12982 break;
12983 cp_lexer_consume_token (parser->lexer);
12986 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
12987 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
12989 end_loc = UNKNOWN_LOCATION;
12990 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
12991 false);
12992 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12993 cp_lexer_consume_token (parser->lexer);
12994 else
12996 cp_parser_skip_to_end_of_statement (parser);
12997 return error_mark_node;
13001 if (cxx_dialect < cxx1z)
13002 pedwarn (loc, 0, "decomposition declaration only available with "
13003 "-std=c++1z or -std=gnu++1z");
13005 tree pushed_scope;
13006 cp_declarator *declarator = make_declarator (cdk_decomp);
13007 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13008 declarator->id_loc = loc;
13009 if (ref_qual != REF_QUAL_NONE)
13010 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13011 ref_qual == REF_QUAL_RVALUE,
13012 NULL_TREE);
13013 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13014 NULL_TREE, decl_specifiers->attributes,
13015 &pushed_scope);
13016 tree orig_decl = decl;
13018 unsigned int i;
13019 cp_expr e;
13020 cp_decl_specifier_seq decl_specs;
13021 clear_decl_specs (&decl_specs);
13022 decl_specs.type = make_auto ();
13023 tree prev = decl;
13024 FOR_EACH_VEC_ELT (v, i, e)
13026 if (i == 0)
13027 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13028 else
13029 declarator->u.id.unqualified_name = e.get_value ();
13030 declarator->id_loc = e.get_location ();
13031 tree elt_pushed_scope;
13032 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13033 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13034 if (decl2 == error_mark_node)
13035 decl = error_mark_node;
13036 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13038 /* Ensure we've diagnosed redeclaration if we aren't creating
13039 a new VAR_DECL. */
13040 gcc_assert (errorcount);
13041 decl = error_mark_node;
13043 else
13044 prev = decl2;
13045 if (elt_pushed_scope)
13046 pop_scope (elt_pushed_scope);
13049 if (v.is_empty ())
13051 error_at (loc, "empty decomposition declaration");
13052 decl = error_mark_node;
13055 if (maybe_range_for_decl == NULL
13056 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13058 bool non_constant_p = false, is_direct_init = false;
13059 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13060 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13061 &non_constant_p);
13063 if (decl != error_mark_node)
13065 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13066 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13067 cp_finish_decomp (decl, prev, v.length ());
13070 else if (decl != error_mark_node)
13072 *maybe_range_for_decl = prev;
13073 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13074 the underlying DECL. */
13075 cp_finish_decomp (decl, prev, v.length ());
13078 if (pushed_scope)
13079 pop_scope (pushed_scope);
13081 if (decl == error_mark_node && DECL_P (orig_decl))
13083 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13084 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13087 return decl;
13090 /* Parse a decl-specifier-seq.
13092 decl-specifier-seq:
13093 decl-specifier-seq [opt] decl-specifier
13094 decl-specifier attribute-specifier-seq [opt] (C++11)
13096 decl-specifier:
13097 storage-class-specifier
13098 type-specifier
13099 function-specifier
13100 friend
13101 typedef
13103 GNU Extension:
13105 decl-specifier:
13106 attributes
13108 Concepts Extension:
13110 decl-specifier:
13111 concept
13113 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13115 The parser flags FLAGS is used to control type-specifier parsing.
13117 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13118 flags:
13120 1: one of the decl-specifiers is an elaborated-type-specifier
13121 (i.e., a type declaration)
13122 2: one of the decl-specifiers is an enum-specifier or a
13123 class-specifier (i.e., a type definition)
13127 static void
13128 cp_parser_decl_specifier_seq (cp_parser* parser,
13129 cp_parser_flags flags,
13130 cp_decl_specifier_seq *decl_specs,
13131 int* declares_class_or_enum)
13133 bool constructor_possible_p = !parser->in_declarator_p;
13134 bool found_decl_spec = false;
13135 cp_token *start_token = NULL;
13136 cp_decl_spec ds;
13138 /* Clear DECL_SPECS. */
13139 clear_decl_specs (decl_specs);
13141 /* Assume no class or enumeration type is declared. */
13142 *declares_class_or_enum = 0;
13144 /* Keep reading specifiers until there are no more to read. */
13145 while (true)
13147 bool constructor_p;
13148 cp_token *token;
13149 ds = ds_last;
13151 /* Peek at the next token. */
13152 token = cp_lexer_peek_token (parser->lexer);
13154 /* Save the first token of the decl spec list for error
13155 reporting. */
13156 if (!start_token)
13157 start_token = token;
13158 /* Handle attributes. */
13159 if (cp_next_tokens_can_be_attribute_p (parser))
13161 /* Parse the attributes. */
13162 tree attrs = cp_parser_attributes_opt (parser);
13164 /* In a sequence of declaration specifiers, c++11 attributes
13165 appertain to the type that precede them. In that case
13166 [dcl.spec]/1 says:
13168 The attribute-specifier-seq affects the type only for
13169 the declaration it appears in, not other declarations
13170 involving the same type.
13172 But for now let's force the user to position the
13173 attribute either at the beginning of the declaration or
13174 after the declarator-id, which would clearly mean that it
13175 applies to the declarator. */
13176 if (cxx11_attribute_p (attrs))
13178 if (!found_decl_spec)
13179 /* The c++11 attribute is at the beginning of the
13180 declaration. It appertains to the entity being
13181 declared. */;
13182 else
13184 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13186 /* This is an attribute following a
13187 class-specifier. */
13188 if (decl_specs->type_definition_p)
13189 warn_misplaced_attr_for_class_type (token->location,
13190 decl_specs->type);
13191 attrs = NULL_TREE;
13193 else
13195 decl_specs->std_attributes
13196 = chainon (decl_specs->std_attributes,
13197 attrs);
13198 if (decl_specs->locations[ds_std_attribute] == 0)
13199 decl_specs->locations[ds_std_attribute] = token->location;
13201 continue;
13205 decl_specs->attributes
13206 = chainon (decl_specs->attributes,
13207 attrs);
13208 if (decl_specs->locations[ds_attribute] == 0)
13209 decl_specs->locations[ds_attribute] = token->location;
13210 continue;
13212 /* Assume we will find a decl-specifier keyword. */
13213 found_decl_spec = true;
13214 /* If the next token is an appropriate keyword, we can simply
13215 add it to the list. */
13216 switch (token->keyword)
13218 /* decl-specifier:
13219 friend
13220 constexpr */
13221 case RID_FRIEND:
13222 if (!at_class_scope_p ())
13224 error_at (token->location, "%<friend%> used outside of class");
13225 cp_lexer_purge_token (parser->lexer);
13227 else
13229 ds = ds_friend;
13230 /* Consume the token. */
13231 cp_lexer_consume_token (parser->lexer);
13233 break;
13235 case RID_CONSTEXPR:
13236 ds = ds_constexpr;
13237 cp_lexer_consume_token (parser->lexer);
13238 break;
13240 case RID_CONCEPT:
13241 ds = ds_concept;
13242 cp_lexer_consume_token (parser->lexer);
13243 break;
13245 /* function-specifier:
13246 inline
13247 virtual
13248 explicit */
13249 case RID_INLINE:
13250 case RID_VIRTUAL:
13251 case RID_EXPLICIT:
13252 cp_parser_function_specifier_opt (parser, decl_specs);
13253 break;
13255 /* decl-specifier:
13256 typedef */
13257 case RID_TYPEDEF:
13258 ds = ds_typedef;
13259 /* Consume the token. */
13260 cp_lexer_consume_token (parser->lexer);
13261 /* A constructor declarator cannot appear in a typedef. */
13262 constructor_possible_p = false;
13263 /* The "typedef" keyword can only occur in a declaration; we
13264 may as well commit at this point. */
13265 cp_parser_commit_to_tentative_parse (parser);
13267 if (decl_specs->storage_class != sc_none)
13268 decl_specs->conflicting_specifiers_p = true;
13269 break;
13271 /* storage-class-specifier:
13272 auto
13273 register
13274 static
13275 extern
13276 mutable
13278 GNU Extension:
13279 thread */
13280 case RID_AUTO:
13281 if (cxx_dialect == cxx98)
13283 /* Consume the token. */
13284 cp_lexer_consume_token (parser->lexer);
13286 /* Complain about `auto' as a storage specifier, if
13287 we're complaining about C++0x compatibility. */
13288 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
13289 " changes meaning in C++11; please remove it");
13291 /* Set the storage class anyway. */
13292 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13293 token);
13295 else
13296 /* C++0x auto type-specifier. */
13297 found_decl_spec = false;
13298 break;
13300 case RID_REGISTER:
13301 case RID_STATIC:
13302 case RID_EXTERN:
13303 case RID_MUTABLE:
13304 /* Consume the token. */
13305 cp_lexer_consume_token (parser->lexer);
13306 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13307 token);
13308 break;
13309 case RID_THREAD:
13310 /* Consume the token. */
13311 ds = ds_thread;
13312 cp_lexer_consume_token (parser->lexer);
13313 break;
13315 default:
13316 /* We did not yet find a decl-specifier yet. */
13317 found_decl_spec = false;
13318 break;
13321 if (found_decl_spec
13322 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13323 && token->keyword != RID_CONSTEXPR)
13324 error ("decl-specifier invalid in condition");
13326 if (found_decl_spec
13327 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13328 && token->keyword != RID_MUTABLE
13329 && token->keyword != RID_CONSTEXPR)
13330 error_at (token->location, "%qD invalid in lambda",
13331 ridpointers[token->keyword]);
13333 if (ds != ds_last)
13334 set_and_check_decl_spec_loc (decl_specs, ds, token);
13336 /* Constructors are a special case. The `S' in `S()' is not a
13337 decl-specifier; it is the beginning of the declarator. */
13338 constructor_p
13339 = (!found_decl_spec
13340 && constructor_possible_p
13341 && (cp_parser_constructor_declarator_p
13342 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13344 /* If we don't have a DECL_SPEC yet, then we must be looking at
13345 a type-specifier. */
13346 if (!found_decl_spec && !constructor_p)
13348 int decl_spec_declares_class_or_enum;
13349 bool is_cv_qualifier;
13350 tree type_spec;
13352 type_spec
13353 = cp_parser_type_specifier (parser, flags,
13354 decl_specs,
13355 /*is_declaration=*/true,
13356 &decl_spec_declares_class_or_enum,
13357 &is_cv_qualifier);
13358 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13360 /* If this type-specifier referenced a user-defined type
13361 (a typedef, class-name, etc.), then we can't allow any
13362 more such type-specifiers henceforth.
13364 [dcl.spec]
13366 The longest sequence of decl-specifiers that could
13367 possibly be a type name is taken as the
13368 decl-specifier-seq of a declaration. The sequence shall
13369 be self-consistent as described below.
13371 [dcl.type]
13373 As a general rule, at most one type-specifier is allowed
13374 in the complete decl-specifier-seq of a declaration. The
13375 only exceptions are the following:
13377 -- const or volatile can be combined with any other
13378 type-specifier.
13380 -- signed or unsigned can be combined with char, long,
13381 short, or int.
13383 -- ..
13385 Example:
13387 typedef char* Pc;
13388 void g (const int Pc);
13390 Here, Pc is *not* part of the decl-specifier seq; it's
13391 the declarator. Therefore, once we see a type-specifier
13392 (other than a cv-qualifier), we forbid any additional
13393 user-defined types. We *do* still allow things like `int
13394 int' to be considered a decl-specifier-seq, and issue the
13395 error message later. */
13396 if (type_spec && !is_cv_qualifier)
13397 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13398 /* A constructor declarator cannot follow a type-specifier. */
13399 if (type_spec)
13401 constructor_possible_p = false;
13402 found_decl_spec = true;
13403 if (!is_cv_qualifier)
13404 decl_specs->any_type_specifiers_p = true;
13408 /* If we still do not have a DECL_SPEC, then there are no more
13409 decl-specifiers. */
13410 if (!found_decl_spec)
13411 break;
13413 decl_specs->any_specifiers_p = true;
13414 /* After we see one decl-specifier, further decl-specifiers are
13415 always optional. */
13416 flags |= CP_PARSER_FLAGS_OPTIONAL;
13419 /* Don't allow a friend specifier with a class definition. */
13420 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13421 && (*declares_class_or_enum & 2))
13422 error_at (decl_specs->locations[ds_friend],
13423 "class definition may not be declared a friend");
13426 /* Parse an (optional) storage-class-specifier.
13428 storage-class-specifier:
13429 auto
13430 register
13431 static
13432 extern
13433 mutable
13435 GNU Extension:
13437 storage-class-specifier:
13438 thread
13440 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13442 static tree
13443 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13445 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13447 case RID_AUTO:
13448 if (cxx_dialect != cxx98)
13449 return NULL_TREE;
13450 /* Fall through for C++98. */
13451 gcc_fallthrough ();
13453 case RID_REGISTER:
13454 case RID_STATIC:
13455 case RID_EXTERN:
13456 case RID_MUTABLE:
13457 case RID_THREAD:
13458 /* Consume the token. */
13459 return cp_lexer_consume_token (parser->lexer)->u.value;
13461 default:
13462 return NULL_TREE;
13466 /* Parse an (optional) function-specifier.
13468 function-specifier:
13469 inline
13470 virtual
13471 explicit
13473 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13474 Updates DECL_SPECS, if it is non-NULL. */
13476 static tree
13477 cp_parser_function_specifier_opt (cp_parser* parser,
13478 cp_decl_specifier_seq *decl_specs)
13480 cp_token *token = cp_lexer_peek_token (parser->lexer);
13481 switch (token->keyword)
13483 case RID_INLINE:
13484 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13485 break;
13487 case RID_VIRTUAL:
13488 /* 14.5.2.3 [temp.mem]
13490 A member function template shall not be virtual. */
13491 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13492 && current_class_type)
13493 error_at (token->location, "templates may not be %<virtual%>");
13494 else
13495 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13496 break;
13498 case RID_EXPLICIT:
13499 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13500 break;
13502 default:
13503 return NULL_TREE;
13506 /* Consume the token. */
13507 return cp_lexer_consume_token (parser->lexer)->u.value;
13510 /* Parse a linkage-specification.
13512 linkage-specification:
13513 extern string-literal { declaration-seq [opt] }
13514 extern string-literal declaration */
13516 static void
13517 cp_parser_linkage_specification (cp_parser* parser)
13519 tree linkage;
13521 /* Look for the `extern' keyword. */
13522 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13524 /* Look for the string-literal. */
13525 linkage = cp_parser_string_literal (parser, false, false);
13527 /* Transform the literal into an identifier. If the literal is a
13528 wide-character string, or contains embedded NULs, then we can't
13529 handle it as the user wants. */
13530 if (strlen (TREE_STRING_POINTER (linkage))
13531 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13533 cp_parser_error (parser, "invalid linkage-specification");
13534 /* Assume C++ linkage. */
13535 linkage = lang_name_cplusplus;
13537 else
13538 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13540 /* We're now using the new linkage. */
13541 push_lang_context (linkage);
13543 /* If the next token is a `{', then we're using the first
13544 production. */
13545 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13547 cp_ensure_no_omp_declare_simd (parser);
13548 cp_ensure_no_oacc_routine (parser);
13550 /* Consume the `{' token. */
13551 cp_lexer_consume_token (parser->lexer);
13552 /* Parse the declarations. */
13553 cp_parser_declaration_seq_opt (parser);
13554 /* Look for the closing `}'. */
13555 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13557 /* Otherwise, there's just one declaration. */
13558 else
13560 bool saved_in_unbraced_linkage_specification_p;
13562 saved_in_unbraced_linkage_specification_p
13563 = parser->in_unbraced_linkage_specification_p;
13564 parser->in_unbraced_linkage_specification_p = true;
13565 cp_parser_declaration (parser);
13566 parser->in_unbraced_linkage_specification_p
13567 = saved_in_unbraced_linkage_specification_p;
13570 /* We're done with the linkage-specification. */
13571 pop_lang_context ();
13574 /* Parse a static_assert-declaration.
13576 static_assert-declaration:
13577 static_assert ( constant-expression , string-literal ) ;
13578 static_assert ( constant-expression ) ; (C++1Z)
13580 If MEMBER_P, this static_assert is a class member. */
13582 static void
13583 cp_parser_static_assert(cp_parser *parser, bool member_p)
13585 tree condition;
13586 tree message;
13587 cp_token *token;
13588 location_t saved_loc;
13589 bool dummy;
13591 /* Peek at the `static_assert' token so we can keep track of exactly
13592 where the static assertion started. */
13593 token = cp_lexer_peek_token (parser->lexer);
13594 saved_loc = token->location;
13596 /* Look for the `static_assert' keyword. */
13597 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13598 RT_STATIC_ASSERT))
13599 return;
13601 /* We know we are in a static assertion; commit to any tentative
13602 parse. */
13603 if (cp_parser_parsing_tentatively (parser))
13604 cp_parser_commit_to_tentative_parse (parser);
13606 /* Parse the `(' starting the static assertion condition. */
13607 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13609 /* Parse the constant-expression. Allow a non-constant expression
13610 here in order to give better diagnostics in finish_static_assert. */
13611 condition =
13612 cp_parser_constant_expression (parser,
13613 /*allow_non_constant_p=*/true,
13614 /*non_constant_p=*/&dummy);
13616 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13618 if (cxx_dialect < cxx1z)
13619 pedwarn (input_location, OPT_Wpedantic,
13620 "static_assert without a message "
13621 "only available with -std=c++1z or -std=gnu++1z");
13622 /* Eat the ')' */
13623 cp_lexer_consume_token (parser->lexer);
13624 message = build_string (1, "");
13625 TREE_TYPE (message) = char_array_type_node;
13626 fix_string_type (message);
13628 else
13630 /* Parse the separating `,'. */
13631 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13633 /* Parse the string-literal message. */
13634 message = cp_parser_string_literal (parser,
13635 /*translate=*/false,
13636 /*wide_ok=*/true);
13638 /* A `)' completes the static assertion. */
13639 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13640 cp_parser_skip_to_closing_parenthesis (parser,
13641 /*recovering=*/true,
13642 /*or_comma=*/false,
13643 /*consume_paren=*/true);
13646 /* A semicolon terminates the declaration. */
13647 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13649 /* Complete the static assertion, which may mean either processing
13650 the static assert now or saving it for template instantiation. */
13651 finish_static_assert (condition, message, saved_loc, member_p);
13654 /* Parse the expression in decltype ( expression ). */
13656 static tree
13657 cp_parser_decltype_expr (cp_parser *parser,
13658 bool &id_expression_or_member_access_p)
13660 cp_token *id_expr_start_token;
13661 tree expr;
13663 /* Since we're going to preserve any side-effects from this parse, set up a
13664 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13665 in the expression. */
13666 tentative_firewall firewall (parser);
13668 /* First, try parsing an id-expression. */
13669 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13670 cp_parser_parse_tentatively (parser);
13671 expr = cp_parser_id_expression (parser,
13672 /*template_keyword_p=*/false,
13673 /*check_dependency_p=*/true,
13674 /*template_p=*/NULL,
13675 /*declarator_p=*/false,
13676 /*optional_p=*/false);
13678 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13680 bool non_integral_constant_expression_p = false;
13681 tree id_expression = expr;
13682 cp_id_kind idk;
13683 const char *error_msg;
13685 if (identifier_p (expr))
13686 /* Lookup the name we got back from the id-expression. */
13687 expr = cp_parser_lookup_name_simple (parser, expr,
13688 id_expr_start_token->location);
13690 if (expr
13691 && expr != error_mark_node
13692 && TREE_CODE (expr) != TYPE_DECL
13693 && (TREE_CODE (expr) != BIT_NOT_EXPR
13694 || !TYPE_P (TREE_OPERAND (expr, 0)))
13695 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13697 /* Complete lookup of the id-expression. */
13698 expr = (finish_id_expression
13699 (id_expression, expr, parser->scope, &idk,
13700 /*integral_constant_expression_p=*/false,
13701 /*allow_non_integral_constant_expression_p=*/true,
13702 &non_integral_constant_expression_p,
13703 /*template_p=*/false,
13704 /*done=*/true,
13705 /*address_p=*/false,
13706 /*template_arg_p=*/false,
13707 &error_msg,
13708 id_expr_start_token->location));
13710 if (expr == error_mark_node)
13711 /* We found an id-expression, but it was something that we
13712 should not have found. This is an error, not something
13713 we can recover from, so note that we found an
13714 id-expression and we'll recover as gracefully as
13715 possible. */
13716 id_expression_or_member_access_p = true;
13719 if (expr
13720 && expr != error_mark_node
13721 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13722 /* We have an id-expression. */
13723 id_expression_or_member_access_p = true;
13726 if (!id_expression_or_member_access_p)
13728 /* Abort the id-expression parse. */
13729 cp_parser_abort_tentative_parse (parser);
13731 /* Parsing tentatively, again. */
13732 cp_parser_parse_tentatively (parser);
13734 /* Parse a class member access. */
13735 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13736 /*cast_p=*/false, /*decltype*/true,
13737 /*member_access_only_p=*/true, NULL);
13739 if (expr
13740 && expr != error_mark_node
13741 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13742 /* We have an id-expression. */
13743 id_expression_or_member_access_p = true;
13746 if (id_expression_or_member_access_p)
13747 /* We have parsed the complete id-expression or member access. */
13748 cp_parser_parse_definitely (parser);
13749 else
13751 /* Abort our attempt to parse an id-expression or member access
13752 expression. */
13753 cp_parser_abort_tentative_parse (parser);
13755 /* Parse a full expression. */
13756 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13757 /*decltype_p=*/true);
13760 return expr;
13763 /* Parse a `decltype' type. Returns the type.
13765 simple-type-specifier:
13766 decltype ( expression )
13767 C++14 proposal:
13768 decltype ( auto ) */
13770 static tree
13771 cp_parser_decltype (cp_parser *parser)
13773 tree expr;
13774 bool id_expression_or_member_access_p = false;
13775 const char *saved_message;
13776 bool saved_integral_constant_expression_p;
13777 bool saved_non_integral_constant_expression_p;
13778 bool saved_greater_than_is_operator_p;
13779 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13781 if (start_token->type == CPP_DECLTYPE)
13783 /* Already parsed. */
13784 cp_lexer_consume_token (parser->lexer);
13785 return saved_checks_value (start_token->u.tree_check_value);
13788 /* Look for the `decltype' token. */
13789 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13790 return error_mark_node;
13792 /* Parse the opening `('. */
13793 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13794 return error_mark_node;
13796 /* decltype (auto) */
13797 if (cxx_dialect >= cxx14
13798 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13800 cp_lexer_consume_token (parser->lexer);
13801 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13802 return error_mark_node;
13803 expr = make_decltype_auto ();
13804 AUTO_IS_DECLTYPE (expr) = true;
13805 goto rewrite;
13808 /* Types cannot be defined in a `decltype' expression. Save away the
13809 old message. */
13810 saved_message = parser->type_definition_forbidden_message;
13812 /* And create the new one. */
13813 parser->type_definition_forbidden_message
13814 = G_("types may not be defined in %<decltype%> expressions");
13816 /* The restrictions on constant-expressions do not apply inside
13817 decltype expressions. */
13818 saved_integral_constant_expression_p
13819 = parser->integral_constant_expression_p;
13820 saved_non_integral_constant_expression_p
13821 = parser->non_integral_constant_expression_p;
13822 parser->integral_constant_expression_p = false;
13824 /* Within a parenthesized expression, a `>' token is always
13825 the greater-than operator. */
13826 saved_greater_than_is_operator_p
13827 = parser->greater_than_is_operator_p;
13828 parser->greater_than_is_operator_p = true;
13830 /* Do not actually evaluate the expression. */
13831 ++cp_unevaluated_operand;
13833 /* Do not warn about problems with the expression. */
13834 ++c_inhibit_evaluation_warnings;
13836 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13838 /* Go back to evaluating expressions. */
13839 --cp_unevaluated_operand;
13840 --c_inhibit_evaluation_warnings;
13842 /* The `>' token might be the end of a template-id or
13843 template-parameter-list now. */
13844 parser->greater_than_is_operator_p
13845 = saved_greater_than_is_operator_p;
13847 /* Restore the old message and the integral constant expression
13848 flags. */
13849 parser->type_definition_forbidden_message = saved_message;
13850 parser->integral_constant_expression_p
13851 = saved_integral_constant_expression_p;
13852 parser->non_integral_constant_expression_p
13853 = saved_non_integral_constant_expression_p;
13855 /* Parse to the closing `)'. */
13856 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13858 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13859 /*consume_paren=*/true);
13860 return error_mark_node;
13863 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13864 tf_warning_or_error);
13866 rewrite:
13867 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13868 it again. */
13869 start_token->type = CPP_DECLTYPE;
13870 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13871 start_token->u.tree_check_value->value = expr;
13872 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13873 start_token->keyword = RID_MAX;
13874 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13876 return expr;
13879 /* Special member functions [gram.special] */
13881 /* Parse a conversion-function-id.
13883 conversion-function-id:
13884 operator conversion-type-id
13886 Returns an IDENTIFIER_NODE representing the operator. */
13888 static tree
13889 cp_parser_conversion_function_id (cp_parser* parser)
13891 tree type;
13892 tree saved_scope;
13893 tree saved_qualifying_scope;
13894 tree saved_object_scope;
13895 tree pushed_scope = NULL_TREE;
13897 /* Look for the `operator' token. */
13898 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13899 return error_mark_node;
13900 /* When we parse the conversion-type-id, the current scope will be
13901 reset. However, we need that information in able to look up the
13902 conversion function later, so we save it here. */
13903 saved_scope = parser->scope;
13904 saved_qualifying_scope = parser->qualifying_scope;
13905 saved_object_scope = parser->object_scope;
13906 /* We must enter the scope of the class so that the names of
13907 entities declared within the class are available in the
13908 conversion-type-id. For example, consider:
13910 struct S {
13911 typedef int I;
13912 operator I();
13915 S::operator I() { ... }
13917 In order to see that `I' is a type-name in the definition, we
13918 must be in the scope of `S'. */
13919 if (saved_scope)
13920 pushed_scope = push_scope (saved_scope);
13921 /* Parse the conversion-type-id. */
13922 type = cp_parser_conversion_type_id (parser);
13923 /* Leave the scope of the class, if any. */
13924 if (pushed_scope)
13925 pop_scope (pushed_scope);
13926 /* Restore the saved scope. */
13927 parser->scope = saved_scope;
13928 parser->qualifying_scope = saved_qualifying_scope;
13929 parser->object_scope = saved_object_scope;
13930 /* If the TYPE is invalid, indicate failure. */
13931 if (type == error_mark_node)
13932 return error_mark_node;
13933 return mangle_conv_op_name_for_type (type);
13936 /* Parse a conversion-type-id:
13938 conversion-type-id:
13939 type-specifier-seq conversion-declarator [opt]
13941 Returns the TYPE specified. */
13943 static tree
13944 cp_parser_conversion_type_id (cp_parser* parser)
13946 tree attributes;
13947 cp_decl_specifier_seq type_specifiers;
13948 cp_declarator *declarator;
13949 tree type_specified;
13950 const char *saved_message;
13952 /* Parse the attributes. */
13953 attributes = cp_parser_attributes_opt (parser);
13955 saved_message = parser->type_definition_forbidden_message;
13956 parser->type_definition_forbidden_message
13957 = G_("types may not be defined in a conversion-type-id");
13959 /* Parse the type-specifiers. */
13960 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13961 /*is_trailing_return=*/false,
13962 &type_specifiers);
13964 parser->type_definition_forbidden_message = saved_message;
13966 /* If that didn't work, stop. */
13967 if (type_specifiers.type == error_mark_node)
13968 return error_mark_node;
13969 /* Parse the conversion-declarator. */
13970 declarator = cp_parser_conversion_declarator_opt (parser);
13972 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13973 /*initialized=*/0, &attributes);
13974 if (attributes)
13975 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13977 /* Don't give this error when parsing tentatively. This happens to
13978 work because we always parse this definitively once. */
13979 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13980 && type_uses_auto (type_specified))
13982 if (cxx_dialect < cxx14)
13984 error ("invalid use of %<auto%> in conversion operator");
13985 return error_mark_node;
13987 else if (template_parm_scope_p ())
13988 warning (0, "use of %<auto%> in member template "
13989 "conversion operator can never be deduced");
13992 return type_specified;
13995 /* Parse an (optional) conversion-declarator.
13997 conversion-declarator:
13998 ptr-operator conversion-declarator [opt]
14002 static cp_declarator *
14003 cp_parser_conversion_declarator_opt (cp_parser* parser)
14005 enum tree_code code;
14006 tree class_type, std_attributes = NULL_TREE;
14007 cp_cv_quals cv_quals;
14009 /* We don't know if there's a ptr-operator next, or not. */
14010 cp_parser_parse_tentatively (parser);
14011 /* Try the ptr-operator. */
14012 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14013 &std_attributes);
14014 /* If it worked, look for more conversion-declarators. */
14015 if (cp_parser_parse_definitely (parser))
14017 cp_declarator *declarator;
14019 /* Parse another optional declarator. */
14020 declarator = cp_parser_conversion_declarator_opt (parser);
14022 declarator = cp_parser_make_indirect_declarator
14023 (code, class_type, cv_quals, declarator, std_attributes);
14025 return declarator;
14028 return NULL;
14031 /* Parse an (optional) ctor-initializer.
14033 ctor-initializer:
14034 : mem-initializer-list
14036 Returns TRUE iff the ctor-initializer was actually present. */
14038 static bool
14039 cp_parser_ctor_initializer_opt (cp_parser* parser)
14041 /* If the next token is not a `:', then there is no
14042 ctor-initializer. */
14043 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14045 /* Do default initialization of any bases and members. */
14046 if (DECL_CONSTRUCTOR_P (current_function_decl))
14047 finish_mem_initializers (NULL_TREE);
14049 return false;
14052 /* Consume the `:' token. */
14053 cp_lexer_consume_token (parser->lexer);
14054 /* And the mem-initializer-list. */
14055 cp_parser_mem_initializer_list (parser);
14057 return true;
14060 /* Parse a mem-initializer-list.
14062 mem-initializer-list:
14063 mem-initializer ... [opt]
14064 mem-initializer ... [opt] , mem-initializer-list */
14066 static void
14067 cp_parser_mem_initializer_list (cp_parser* parser)
14069 tree mem_initializer_list = NULL_TREE;
14070 tree target_ctor = error_mark_node;
14071 cp_token *token = cp_lexer_peek_token (parser->lexer);
14073 /* Let the semantic analysis code know that we are starting the
14074 mem-initializer-list. */
14075 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14076 error_at (token->location,
14077 "only constructors take member initializers");
14079 /* Loop through the list. */
14080 while (true)
14082 tree mem_initializer;
14084 token = cp_lexer_peek_token (parser->lexer);
14085 /* Parse the mem-initializer. */
14086 mem_initializer = cp_parser_mem_initializer (parser);
14087 /* If the next token is a `...', we're expanding member initializers. */
14088 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14090 /* Consume the `...'. */
14091 cp_lexer_consume_token (parser->lexer);
14093 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14094 can be expanded but members cannot. */
14095 if (mem_initializer != error_mark_node
14096 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14098 error_at (token->location,
14099 "cannot expand initializer for member %qD",
14100 TREE_PURPOSE (mem_initializer));
14101 mem_initializer = error_mark_node;
14104 /* Construct the pack expansion type. */
14105 if (mem_initializer != error_mark_node)
14106 mem_initializer = make_pack_expansion (mem_initializer);
14108 if (target_ctor != error_mark_node
14109 && mem_initializer != error_mark_node)
14111 error ("mem-initializer for %qD follows constructor delegation",
14112 TREE_PURPOSE (mem_initializer));
14113 mem_initializer = error_mark_node;
14115 /* Look for a target constructor. */
14116 if (mem_initializer != error_mark_node
14117 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14118 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14120 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14121 if (mem_initializer_list)
14123 error ("constructor delegation follows mem-initializer for %qD",
14124 TREE_PURPOSE (mem_initializer_list));
14125 mem_initializer = error_mark_node;
14127 target_ctor = mem_initializer;
14129 /* Add it to the list, unless it was erroneous. */
14130 if (mem_initializer != error_mark_node)
14132 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14133 mem_initializer_list = mem_initializer;
14135 /* If the next token is not a `,', we're done. */
14136 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14137 break;
14138 /* Consume the `,' token. */
14139 cp_lexer_consume_token (parser->lexer);
14142 /* Perform semantic analysis. */
14143 if (DECL_CONSTRUCTOR_P (current_function_decl))
14144 finish_mem_initializers (mem_initializer_list);
14147 /* Parse a mem-initializer.
14149 mem-initializer:
14150 mem-initializer-id ( expression-list [opt] )
14151 mem-initializer-id braced-init-list
14153 GNU extension:
14155 mem-initializer:
14156 ( expression-list [opt] )
14158 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14159 class) or FIELD_DECL (for a non-static data member) to initialize;
14160 the TREE_VALUE is the expression-list. An empty initialization
14161 list is represented by void_list_node. */
14163 static tree
14164 cp_parser_mem_initializer (cp_parser* parser)
14166 tree mem_initializer_id;
14167 tree expression_list;
14168 tree member;
14169 cp_token *token = cp_lexer_peek_token (parser->lexer);
14171 /* Find out what is being initialized. */
14172 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14174 permerror (token->location,
14175 "anachronistic old-style base class initializer");
14176 mem_initializer_id = NULL_TREE;
14178 else
14180 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14181 if (mem_initializer_id == error_mark_node)
14182 return mem_initializer_id;
14184 member = expand_member_init (mem_initializer_id);
14185 if (member && !DECL_P (member))
14186 in_base_initializer = 1;
14188 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14190 bool expr_non_constant_p;
14191 cp_lexer_set_source_position (parser->lexer);
14192 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14193 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14194 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14195 expression_list = build_tree_list (NULL_TREE, expression_list);
14197 else
14199 vec<tree, va_gc> *vec;
14200 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14201 /*cast_p=*/false,
14202 /*allow_expansion_p=*/true,
14203 /*non_constant_p=*/NULL);
14204 if (vec == NULL)
14205 return error_mark_node;
14206 expression_list = build_tree_list_vec (vec);
14207 release_tree_vector (vec);
14210 if (expression_list == error_mark_node)
14211 return error_mark_node;
14212 if (!expression_list)
14213 expression_list = void_type_node;
14215 in_base_initializer = 0;
14217 return member ? build_tree_list (member, expression_list) : error_mark_node;
14220 /* Parse a mem-initializer-id.
14222 mem-initializer-id:
14223 :: [opt] nested-name-specifier [opt] class-name
14224 decltype-specifier (C++11)
14225 identifier
14227 Returns a TYPE indicating the class to be initialized for the first
14228 production (and the second in C++11). Returns an IDENTIFIER_NODE
14229 indicating the data member to be initialized for the last production. */
14231 static tree
14232 cp_parser_mem_initializer_id (cp_parser* parser)
14234 bool global_scope_p;
14235 bool nested_name_specifier_p;
14236 bool template_p = false;
14237 tree id;
14239 cp_token *token = cp_lexer_peek_token (parser->lexer);
14241 /* `typename' is not allowed in this context ([temp.res]). */
14242 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14244 error_at (token->location,
14245 "keyword %<typename%> not allowed in this context (a qualified "
14246 "member initializer is implicitly a type)");
14247 cp_lexer_consume_token (parser->lexer);
14249 /* Look for the optional `::' operator. */
14250 global_scope_p
14251 = (cp_parser_global_scope_opt (parser,
14252 /*current_scope_valid_p=*/false)
14253 != NULL_TREE);
14254 /* Look for the optional nested-name-specifier. The simplest way to
14255 implement:
14257 [temp.res]
14259 The keyword `typename' is not permitted in a base-specifier or
14260 mem-initializer; in these contexts a qualified name that
14261 depends on a template-parameter is implicitly assumed to be a
14262 type name.
14264 is to assume that we have seen the `typename' keyword at this
14265 point. */
14266 nested_name_specifier_p
14267 = (cp_parser_nested_name_specifier_opt (parser,
14268 /*typename_keyword_p=*/true,
14269 /*check_dependency_p=*/true,
14270 /*type_p=*/true,
14271 /*is_declaration=*/true)
14272 != NULL_TREE);
14273 if (nested_name_specifier_p)
14274 template_p = cp_parser_optional_template_keyword (parser);
14275 /* If there is a `::' operator or a nested-name-specifier, then we
14276 are definitely looking for a class-name. */
14277 if (global_scope_p || nested_name_specifier_p)
14278 return cp_parser_class_name (parser,
14279 /*typename_keyword_p=*/true,
14280 /*template_keyword_p=*/template_p,
14281 typename_type,
14282 /*check_dependency_p=*/true,
14283 /*class_head_p=*/false,
14284 /*is_declaration=*/true);
14285 /* Otherwise, we could also be looking for an ordinary identifier. */
14286 cp_parser_parse_tentatively (parser);
14287 if (cp_lexer_next_token_is_decltype (parser->lexer))
14288 /* Try a decltype-specifier. */
14289 id = cp_parser_decltype (parser);
14290 else
14291 /* Otherwise, try a class-name. */
14292 id = cp_parser_class_name (parser,
14293 /*typename_keyword_p=*/true,
14294 /*template_keyword_p=*/false,
14295 none_type,
14296 /*check_dependency_p=*/true,
14297 /*class_head_p=*/false,
14298 /*is_declaration=*/true);
14299 /* If we found one, we're done. */
14300 if (cp_parser_parse_definitely (parser))
14301 return id;
14302 /* Otherwise, look for an ordinary identifier. */
14303 return cp_parser_identifier (parser);
14306 /* Overloading [gram.over] */
14308 /* Parse an operator-function-id.
14310 operator-function-id:
14311 operator operator
14313 Returns an IDENTIFIER_NODE for the operator which is a
14314 human-readable spelling of the identifier, e.g., `operator +'. */
14316 static cp_expr
14317 cp_parser_operator_function_id (cp_parser* parser)
14319 /* Look for the `operator' keyword. */
14320 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14321 return error_mark_node;
14322 /* And then the name of the operator itself. */
14323 return cp_parser_operator (parser);
14326 /* Return an identifier node for a user-defined literal operator.
14327 The suffix identifier is chained to the operator name identifier. */
14329 tree
14330 cp_literal_operator_id (const char* name)
14332 tree identifier;
14333 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14334 + strlen (name) + 10);
14335 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14336 identifier = get_identifier (buffer);
14338 return identifier;
14341 /* Parse an operator.
14343 operator:
14344 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14345 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14346 || ++ -- , ->* -> () []
14348 GNU Extensions:
14350 operator:
14351 <? >? <?= >?=
14353 Returns an IDENTIFIER_NODE for the operator which is a
14354 human-readable spelling of the identifier, e.g., `operator +'. */
14356 static cp_expr
14357 cp_parser_operator (cp_parser* parser)
14359 tree id = NULL_TREE;
14360 cp_token *token;
14361 bool utf8 = false;
14363 /* Peek at the next token. */
14364 token = cp_lexer_peek_token (parser->lexer);
14366 location_t start_loc = token->location;
14368 /* Figure out which operator we have. */
14369 switch (token->type)
14371 case CPP_KEYWORD:
14373 enum tree_code op;
14375 /* The keyword should be either `new' or `delete'. */
14376 if (token->keyword == RID_NEW)
14377 op = NEW_EXPR;
14378 else if (token->keyword == RID_DELETE)
14379 op = DELETE_EXPR;
14380 else
14381 break;
14383 /* Consume the `new' or `delete' token. */
14384 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14386 /* Peek at the next token. */
14387 token = cp_lexer_peek_token (parser->lexer);
14388 /* If it's a `[' token then this is the array variant of the
14389 operator. */
14390 if (token->type == CPP_OPEN_SQUARE)
14392 /* Consume the `[' token. */
14393 cp_lexer_consume_token (parser->lexer);
14394 /* Look for the `]' token. */
14395 if (cp_token *close_token
14396 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14397 end_loc = close_token->location;
14398 id = cp_operator_id (op == NEW_EXPR
14399 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14401 /* Otherwise, we have the non-array variant. */
14402 else
14403 id = cp_operator_id (op);
14405 location_t loc = make_location (start_loc, start_loc, end_loc);
14407 return cp_expr (id, loc);
14410 case CPP_PLUS:
14411 id = cp_operator_id (PLUS_EXPR);
14412 break;
14414 case CPP_MINUS:
14415 id = cp_operator_id (MINUS_EXPR);
14416 break;
14418 case CPP_MULT:
14419 id = cp_operator_id (MULT_EXPR);
14420 break;
14422 case CPP_DIV:
14423 id = cp_operator_id (TRUNC_DIV_EXPR);
14424 break;
14426 case CPP_MOD:
14427 id = cp_operator_id (TRUNC_MOD_EXPR);
14428 break;
14430 case CPP_XOR:
14431 id = cp_operator_id (BIT_XOR_EXPR);
14432 break;
14434 case CPP_AND:
14435 id = cp_operator_id (BIT_AND_EXPR);
14436 break;
14438 case CPP_OR:
14439 id = cp_operator_id (BIT_IOR_EXPR);
14440 break;
14442 case CPP_COMPL:
14443 id = cp_operator_id (BIT_NOT_EXPR);
14444 break;
14446 case CPP_NOT:
14447 id = cp_operator_id (TRUTH_NOT_EXPR);
14448 break;
14450 case CPP_EQ:
14451 id = cp_assignment_operator_id (NOP_EXPR);
14452 break;
14454 case CPP_LESS:
14455 id = cp_operator_id (LT_EXPR);
14456 break;
14458 case CPP_GREATER:
14459 id = cp_operator_id (GT_EXPR);
14460 break;
14462 case CPP_PLUS_EQ:
14463 id = cp_assignment_operator_id (PLUS_EXPR);
14464 break;
14466 case CPP_MINUS_EQ:
14467 id = cp_assignment_operator_id (MINUS_EXPR);
14468 break;
14470 case CPP_MULT_EQ:
14471 id = cp_assignment_operator_id (MULT_EXPR);
14472 break;
14474 case CPP_DIV_EQ:
14475 id = cp_assignment_operator_id (TRUNC_DIV_EXPR);
14476 break;
14478 case CPP_MOD_EQ:
14479 id = cp_assignment_operator_id (TRUNC_MOD_EXPR);
14480 break;
14482 case CPP_XOR_EQ:
14483 id = cp_assignment_operator_id (BIT_XOR_EXPR);
14484 break;
14486 case CPP_AND_EQ:
14487 id = cp_assignment_operator_id (BIT_AND_EXPR);
14488 break;
14490 case CPP_OR_EQ:
14491 id = cp_assignment_operator_id (BIT_IOR_EXPR);
14492 break;
14494 case CPP_LSHIFT:
14495 id = cp_operator_id (LSHIFT_EXPR);
14496 break;
14498 case CPP_RSHIFT:
14499 id = cp_operator_id (RSHIFT_EXPR);
14500 break;
14502 case CPP_LSHIFT_EQ:
14503 id = cp_assignment_operator_id (LSHIFT_EXPR);
14504 break;
14506 case CPP_RSHIFT_EQ:
14507 id = cp_assignment_operator_id (RSHIFT_EXPR);
14508 break;
14510 case CPP_EQ_EQ:
14511 id = cp_operator_id (EQ_EXPR);
14512 break;
14514 case CPP_NOT_EQ:
14515 id = cp_operator_id (NE_EXPR);
14516 break;
14518 case CPP_LESS_EQ:
14519 id = cp_operator_id (LE_EXPR);
14520 break;
14522 case CPP_GREATER_EQ:
14523 id = cp_operator_id (GE_EXPR);
14524 break;
14526 case CPP_AND_AND:
14527 id = cp_operator_id (TRUTH_ANDIF_EXPR);
14528 break;
14530 case CPP_OR_OR:
14531 id = cp_operator_id (TRUTH_ORIF_EXPR);
14532 break;
14534 case CPP_PLUS_PLUS:
14535 id = cp_operator_id (POSTINCREMENT_EXPR);
14536 break;
14538 case CPP_MINUS_MINUS:
14539 id = cp_operator_id (PREDECREMENT_EXPR);
14540 break;
14542 case CPP_COMMA:
14543 id = cp_operator_id (COMPOUND_EXPR);
14544 break;
14546 case CPP_DEREF_STAR:
14547 id = cp_operator_id (MEMBER_REF);
14548 break;
14550 case CPP_DEREF:
14551 id = cp_operator_id (COMPONENT_REF);
14552 break;
14554 case CPP_OPEN_PAREN:
14555 /* Consume the `('. */
14556 cp_lexer_consume_token (parser->lexer);
14557 /* Look for the matching `)'. */
14558 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14559 return cp_operator_id (CALL_EXPR);
14561 case CPP_OPEN_SQUARE:
14562 /* Consume the `['. */
14563 cp_lexer_consume_token (parser->lexer);
14564 /* Look for the matching `]'. */
14565 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14566 return cp_operator_id (ARRAY_REF);
14568 case CPP_UTF8STRING:
14569 case CPP_UTF8STRING_USERDEF:
14570 utf8 = true;
14571 /* FALLTHRU */
14572 case CPP_STRING:
14573 case CPP_WSTRING:
14574 case CPP_STRING16:
14575 case CPP_STRING32:
14576 case CPP_STRING_USERDEF:
14577 case CPP_WSTRING_USERDEF:
14578 case CPP_STRING16_USERDEF:
14579 case CPP_STRING32_USERDEF:
14581 tree str, string_tree;
14582 int sz, len;
14584 if (cxx_dialect == cxx98)
14585 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14587 /* Consume the string. */
14588 str = cp_parser_string_literal (parser, /*translate=*/true,
14589 /*wide_ok=*/true, /*lookup_udlit=*/false);
14590 if (str == error_mark_node)
14591 return error_mark_node;
14592 else if (TREE_CODE (str) == USERDEF_LITERAL)
14594 string_tree = USERDEF_LITERAL_VALUE (str);
14595 id = USERDEF_LITERAL_SUFFIX_ID (str);
14597 else
14599 string_tree = str;
14600 /* Look for the suffix identifier. */
14601 token = cp_lexer_peek_token (parser->lexer);
14602 if (token->type == CPP_NAME)
14603 id = cp_parser_identifier (parser);
14604 else if (token->type == CPP_KEYWORD)
14606 error ("unexpected keyword;"
14607 " remove space between quotes and suffix identifier");
14608 return error_mark_node;
14610 else
14612 error ("expected suffix identifier");
14613 return error_mark_node;
14616 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14617 (TREE_TYPE (TREE_TYPE (string_tree))));
14618 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14619 if (len != 0)
14621 error ("expected empty string after %<operator%> keyword");
14622 return error_mark_node;
14624 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14625 != char_type_node)
14627 error ("invalid encoding prefix in literal operator");
14628 return error_mark_node;
14630 if (id != error_mark_node)
14632 const char *name = IDENTIFIER_POINTER (id);
14633 id = cp_literal_operator_id (name);
14635 return id;
14638 default:
14639 /* Anything else is an error. */
14640 break;
14643 /* If we have selected an identifier, we need to consume the
14644 operator token. */
14645 if (id)
14646 cp_lexer_consume_token (parser->lexer);
14647 /* Otherwise, no valid operator name was present. */
14648 else
14650 cp_parser_error (parser, "expected operator");
14651 id = error_mark_node;
14654 return cp_expr (id, start_loc);
14657 /* Parse a template-declaration.
14659 template-declaration:
14660 export [opt] template < template-parameter-list > declaration
14662 If MEMBER_P is TRUE, this template-declaration occurs within a
14663 class-specifier.
14665 The grammar rule given by the standard isn't correct. What
14666 is really meant is:
14668 template-declaration:
14669 export [opt] template-parameter-list-seq
14670 decl-specifier-seq [opt] init-declarator [opt] ;
14671 export [opt] template-parameter-list-seq
14672 function-definition
14674 template-parameter-list-seq:
14675 template-parameter-list-seq [opt]
14676 template < template-parameter-list >
14678 Concept Extensions:
14680 template-parameter-list-seq:
14681 template < template-parameter-list > requires-clause [opt]
14683 requires-clause:
14684 requires logical-or-expression */
14686 static void
14687 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14689 /* Check for `export'. */
14690 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14692 /* Consume the `export' token. */
14693 cp_lexer_consume_token (parser->lexer);
14694 /* Warn that we do not support `export'. */
14695 warning (0, "keyword %<export%> not implemented, and will be ignored");
14698 cp_parser_template_declaration_after_export (parser, member_p);
14701 /* Parse a template-parameter-list.
14703 template-parameter-list:
14704 template-parameter
14705 template-parameter-list , template-parameter
14707 Returns a TREE_LIST. Each node represents a template parameter.
14708 The nodes are connected via their TREE_CHAINs. */
14710 static tree
14711 cp_parser_template_parameter_list (cp_parser* parser)
14713 tree parameter_list = NULL_TREE;
14715 begin_template_parm_list ();
14717 /* The loop below parses the template parms. We first need to know
14718 the total number of template parms to be able to compute proper
14719 canonical types of each dependent type. So after the loop, when
14720 we know the total number of template parms,
14721 end_template_parm_list computes the proper canonical types and
14722 fixes up the dependent types accordingly. */
14723 while (true)
14725 tree parameter;
14726 bool is_non_type;
14727 bool is_parameter_pack;
14728 location_t parm_loc;
14730 /* Parse the template-parameter. */
14731 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14732 parameter = cp_parser_template_parameter (parser,
14733 &is_non_type,
14734 &is_parameter_pack);
14735 /* Add it to the list. */
14736 if (parameter != error_mark_node)
14737 parameter_list = process_template_parm (parameter_list,
14738 parm_loc,
14739 parameter,
14740 is_non_type,
14741 is_parameter_pack);
14742 else
14744 tree err_parm = build_tree_list (parameter, parameter);
14745 parameter_list = chainon (parameter_list, err_parm);
14748 /* If the next token is not a `,', we're done. */
14749 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14750 break;
14751 /* Otherwise, consume the `,' token. */
14752 cp_lexer_consume_token (parser->lexer);
14755 return end_template_parm_list (parameter_list);
14758 /* Parse a introduction-list.
14760 introduction-list:
14761 introduced-parameter
14762 introduction-list , introduced-parameter
14764 introduced-parameter:
14765 ...[opt] identifier
14767 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14768 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14769 WILDCARD_DECL will also have DECL_NAME set and token location in
14770 DECL_SOURCE_LOCATION. */
14772 static tree
14773 cp_parser_introduction_list (cp_parser *parser)
14775 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14777 while (true)
14779 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14780 if (is_pack)
14781 cp_lexer_consume_token (parser->lexer);
14783 /* Build placeholder. */
14784 tree parm = build_nt (WILDCARD_DECL);
14785 DECL_SOURCE_LOCATION (parm)
14786 = cp_lexer_peek_token (parser->lexer)->location;
14787 DECL_NAME (parm) = cp_parser_identifier (parser);
14788 WILDCARD_PACK_P (parm) = is_pack;
14789 vec_safe_push (introduction_vec, parm);
14791 /* If the next token is not a `,', we're done. */
14792 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14793 break;
14794 /* Otherwise, consume the `,' token. */
14795 cp_lexer_consume_token (parser->lexer);
14798 /* Convert the vec into a TREE_VEC. */
14799 tree introduction_list = make_tree_vec (introduction_vec->length ());
14800 unsigned int n;
14801 tree parm;
14802 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14803 TREE_VEC_ELT (introduction_list, n) = parm;
14805 release_tree_vector (introduction_vec);
14806 return introduction_list;
14809 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14810 is an abstract declarator. */
14812 static inline cp_declarator*
14813 get_id_declarator (cp_declarator *declarator)
14815 cp_declarator *d = declarator;
14816 while (d && d->kind != cdk_id)
14817 d = d->declarator;
14818 return d;
14821 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14822 is an abstract declarator. */
14824 static inline tree
14825 get_unqualified_id (cp_declarator *declarator)
14827 declarator = get_id_declarator (declarator);
14828 if (declarator)
14829 return declarator->u.id.unqualified_name;
14830 else
14831 return NULL_TREE;
14834 /* Returns true if DECL represents a constrained-parameter. */
14836 static inline bool
14837 is_constrained_parameter (tree decl)
14839 return (decl
14840 && TREE_CODE (decl) == TYPE_DECL
14841 && CONSTRAINED_PARM_CONCEPT (decl)
14842 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14845 /* Returns true if PARM declares a constrained-parameter. */
14847 static inline bool
14848 is_constrained_parameter (cp_parameter_declarator *parm)
14850 return is_constrained_parameter (parm->decl_specifiers.type);
14853 /* Check that the type parameter is only a declarator-id, and that its
14854 type is not cv-qualified. */
14856 bool
14857 cp_parser_check_constrained_type_parm (cp_parser *parser,
14858 cp_parameter_declarator *parm)
14860 if (!parm->declarator)
14861 return true;
14863 if (parm->declarator->kind != cdk_id)
14865 cp_parser_error (parser, "invalid constrained type parameter");
14866 return false;
14869 /* Don't allow cv-qualified type parameters. */
14870 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14871 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14873 cp_parser_error (parser, "cv-qualified type parameter");
14874 return false;
14877 return true;
14880 /* Finish parsing/processing a template type parameter and checking
14881 various restrictions. */
14883 static inline tree
14884 cp_parser_constrained_type_template_parm (cp_parser *parser,
14885 tree id,
14886 cp_parameter_declarator* parmdecl)
14888 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14889 return finish_template_type_parm (class_type_node, id);
14890 else
14891 return error_mark_node;
14894 static tree
14895 finish_constrained_template_template_parm (tree proto, tree id)
14897 /* FIXME: This should probably be copied, and we may need to adjust
14898 the template parameter depths. */
14899 tree saved_parms = current_template_parms;
14900 begin_template_parm_list ();
14901 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14902 end_template_parm_list ();
14904 tree parm = finish_template_template_parm (class_type_node, id);
14905 current_template_parms = saved_parms;
14907 return parm;
14910 /* Finish parsing/processing a template template parameter by borrowing
14911 the template parameter list from the prototype parameter. */
14913 static tree
14914 cp_parser_constrained_template_template_parm (cp_parser *parser,
14915 tree proto,
14916 tree id,
14917 cp_parameter_declarator *parmdecl)
14919 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14920 return error_mark_node;
14921 return finish_constrained_template_template_parm (proto, id);
14924 /* Create a new non-type template parameter from the given PARM
14925 declarator. */
14927 static tree
14928 constrained_non_type_template_parm (bool *is_non_type,
14929 cp_parameter_declarator *parm)
14931 *is_non_type = true;
14932 cp_declarator *decl = parm->declarator;
14933 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14934 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14935 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14938 /* Build a constrained template parameter based on the PARMDECL
14939 declarator. The type of PARMDECL is the constrained type, which
14940 refers to the prototype template parameter that ultimately
14941 specifies the type of the declared parameter. */
14943 static tree
14944 finish_constrained_parameter (cp_parser *parser,
14945 cp_parameter_declarator *parmdecl,
14946 bool *is_non_type,
14947 bool *is_parameter_pack)
14949 tree decl = parmdecl->decl_specifiers.type;
14950 tree id = get_unqualified_id (parmdecl->declarator);
14951 tree def = parmdecl->default_argument;
14952 tree proto = DECL_INITIAL (decl);
14954 /* A template parameter constrained by a variadic concept shall also
14955 be declared as a template parameter pack. */
14956 bool is_variadic = template_parameter_pack_p (proto);
14957 if (is_variadic && !*is_parameter_pack)
14958 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14960 /* Build the parameter. Return an error if the declarator was invalid. */
14961 tree parm;
14962 if (TREE_CODE (proto) == TYPE_DECL)
14963 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14964 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14965 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14966 parmdecl);
14967 else
14968 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14969 if (parm == error_mark_node)
14970 return error_mark_node;
14972 /* Finish the parameter decl and create a node attaching the
14973 default argument and constraint. */
14974 parm = build_tree_list (def, parm);
14975 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14977 return parm;
14980 /* Returns true if the parsed type actually represents the declaration
14981 of a type template-parameter. */
14983 static inline bool
14984 declares_constrained_type_template_parameter (tree type)
14986 return (is_constrained_parameter (type)
14987 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14991 /* Returns true if the parsed type actually represents the declaration of
14992 a template template-parameter. */
14994 static bool
14995 declares_constrained_template_template_parameter (tree type)
14997 return (is_constrained_parameter (type)
14998 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15001 /* Parse a default argument for a type template-parameter.
15002 Note that diagnostics are handled in cp_parser_template_parameter. */
15004 static tree
15005 cp_parser_default_type_template_argument (cp_parser *parser)
15007 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15009 /* Consume the `=' token. */
15010 cp_lexer_consume_token (parser->lexer);
15012 cp_token *token = cp_lexer_peek_token (parser->lexer);
15014 /* Parse the default-argument. */
15015 push_deferring_access_checks (dk_no_deferred);
15016 tree default_argument = cp_parser_type_id (parser);
15017 pop_deferring_access_checks ();
15019 if (flag_concepts && type_uses_auto (default_argument))
15021 error_at (token->location,
15022 "invalid use of %<auto%> in default template argument");
15023 return error_mark_node;
15026 return default_argument;
15029 /* Parse a default argument for a template template-parameter. */
15031 static tree
15032 cp_parser_default_template_template_argument (cp_parser *parser)
15034 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15036 bool is_template;
15038 /* Consume the `='. */
15039 cp_lexer_consume_token (parser->lexer);
15040 /* Parse the id-expression. */
15041 push_deferring_access_checks (dk_no_deferred);
15042 /* save token before parsing the id-expression, for error
15043 reporting */
15044 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15045 tree default_argument
15046 = cp_parser_id_expression (parser,
15047 /*template_keyword_p=*/false,
15048 /*check_dependency_p=*/true,
15049 /*template_p=*/&is_template,
15050 /*declarator_p=*/false,
15051 /*optional_p=*/false);
15052 if (TREE_CODE (default_argument) == TYPE_DECL)
15053 /* If the id-expression was a template-id that refers to
15054 a template-class, we already have the declaration here,
15055 so no further lookup is needed. */
15057 else
15058 /* Look up the name. */
15059 default_argument
15060 = cp_parser_lookup_name (parser, default_argument,
15061 none_type,
15062 /*is_template=*/is_template,
15063 /*is_namespace=*/false,
15064 /*check_dependency=*/true,
15065 /*ambiguous_decls=*/NULL,
15066 token->location);
15067 /* See if the default argument is valid. */
15068 default_argument = check_template_template_default_arg (default_argument);
15069 pop_deferring_access_checks ();
15070 return default_argument;
15073 /* Parse a template-parameter.
15075 template-parameter:
15076 type-parameter
15077 parameter-declaration
15079 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15080 the parameter. The TREE_PURPOSE is the default value, if any.
15081 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15082 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15083 set to true iff this parameter is a parameter pack. */
15085 static tree
15086 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15087 bool *is_parameter_pack)
15089 cp_token *token;
15090 cp_parameter_declarator *parameter_declarator;
15091 tree parm;
15093 /* Assume it is a type parameter or a template parameter. */
15094 *is_non_type = false;
15095 /* Assume it not a parameter pack. */
15096 *is_parameter_pack = false;
15097 /* Peek at the next token. */
15098 token = cp_lexer_peek_token (parser->lexer);
15099 /* If it is `template', we have a type-parameter. */
15100 if (token->keyword == RID_TEMPLATE)
15101 return cp_parser_type_parameter (parser, is_parameter_pack);
15102 /* If it is `class' or `typename' we do not know yet whether it is a
15103 type parameter or a non-type parameter. Consider:
15105 template <typename T, typename T::X X> ...
15109 template <class C, class D*> ...
15111 Here, the first parameter is a type parameter, and the second is
15112 a non-type parameter. We can tell by looking at the token after
15113 the identifier -- if it is a `,', `=', or `>' then we have a type
15114 parameter. */
15115 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15117 /* Peek at the token after `class' or `typename'. */
15118 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15119 /* If it's an ellipsis, we have a template type parameter
15120 pack. */
15121 if (token->type == CPP_ELLIPSIS)
15122 return cp_parser_type_parameter (parser, is_parameter_pack);
15123 /* If it's an identifier, skip it. */
15124 if (token->type == CPP_NAME)
15125 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15126 /* Now, see if the token looks like the end of a template
15127 parameter. */
15128 if (token->type == CPP_COMMA
15129 || token->type == CPP_EQ
15130 || token->type == CPP_GREATER)
15131 return cp_parser_type_parameter (parser, is_parameter_pack);
15134 /* Otherwise, it is a non-type parameter or a constrained parameter.
15136 [temp.param]
15138 When parsing a default template-argument for a non-type
15139 template-parameter, the first non-nested `>' is taken as the end
15140 of the template parameter-list rather than a greater-than
15141 operator. */
15142 parameter_declarator
15143 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15144 /*parenthesized_p=*/NULL);
15146 if (!parameter_declarator)
15147 return error_mark_node;
15149 /* If the parameter declaration is marked as a parameter pack, set
15150 *IS_PARAMETER_PACK to notify the caller. */
15151 if (parameter_declarator->template_parameter_pack_p)
15152 *is_parameter_pack = true;
15154 if (parameter_declarator->default_argument)
15156 /* Can happen in some cases of erroneous input (c++/34892). */
15157 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15158 /* Consume the `...' for better error recovery. */
15159 cp_lexer_consume_token (parser->lexer);
15162 // The parameter may have been constrained.
15163 if (is_constrained_parameter (parameter_declarator))
15164 return finish_constrained_parameter (parser,
15165 parameter_declarator,
15166 is_non_type,
15167 is_parameter_pack);
15169 // Now we're sure that the parameter is a non-type parameter.
15170 *is_non_type = true;
15172 parm = grokdeclarator (parameter_declarator->declarator,
15173 &parameter_declarator->decl_specifiers,
15174 TPARM, /*initialized=*/0,
15175 /*attrlist=*/NULL);
15176 if (parm == error_mark_node)
15177 return error_mark_node;
15179 return build_tree_list (parameter_declarator->default_argument, parm);
15182 /* Parse a type-parameter.
15184 type-parameter:
15185 class identifier [opt]
15186 class identifier [opt] = type-id
15187 typename identifier [opt]
15188 typename identifier [opt] = type-id
15189 template < template-parameter-list > class identifier [opt]
15190 template < template-parameter-list > class identifier [opt]
15191 = id-expression
15193 GNU Extension (variadic templates):
15195 type-parameter:
15196 class ... identifier [opt]
15197 typename ... identifier [opt]
15199 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15200 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15201 the declaration of the parameter.
15203 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15205 static tree
15206 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15208 cp_token *token;
15209 tree parameter;
15211 /* Look for a keyword to tell us what kind of parameter this is. */
15212 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15213 if (!token)
15214 return error_mark_node;
15216 switch (token->keyword)
15218 case RID_CLASS:
15219 case RID_TYPENAME:
15221 tree identifier;
15222 tree default_argument;
15224 /* If the next token is an ellipsis, we have a template
15225 argument pack. */
15226 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15228 /* Consume the `...' token. */
15229 cp_lexer_consume_token (parser->lexer);
15230 maybe_warn_variadic_templates ();
15232 *is_parameter_pack = true;
15235 /* If the next token is an identifier, then it names the
15236 parameter. */
15237 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15238 identifier = cp_parser_identifier (parser);
15239 else
15240 identifier = NULL_TREE;
15242 /* Create the parameter. */
15243 parameter = finish_template_type_parm (class_type_node, identifier);
15245 /* If the next token is an `=', we have a default argument. */
15246 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15248 default_argument
15249 = cp_parser_default_type_template_argument (parser);
15251 /* Template parameter packs cannot have default
15252 arguments. */
15253 if (*is_parameter_pack)
15255 if (identifier)
15256 error_at (token->location,
15257 "template parameter pack %qD cannot have a "
15258 "default argument", identifier);
15259 else
15260 error_at (token->location,
15261 "template parameter packs cannot have "
15262 "default arguments");
15263 default_argument = NULL_TREE;
15265 else if (check_for_bare_parameter_packs (default_argument))
15266 default_argument = error_mark_node;
15268 else
15269 default_argument = NULL_TREE;
15271 /* Create the combined representation of the parameter and the
15272 default argument. */
15273 parameter = build_tree_list (default_argument, parameter);
15275 break;
15277 case RID_TEMPLATE:
15279 tree identifier;
15280 tree default_argument;
15282 /* Look for the `<'. */
15283 cp_parser_require (parser, CPP_LESS, RT_LESS);
15284 /* Parse the template-parameter-list. */
15285 cp_parser_template_parameter_list (parser);
15286 /* Look for the `>'. */
15287 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15289 // If template requirements are present, parse them.
15290 if (flag_concepts)
15292 tree reqs = get_shorthand_constraints (current_template_parms);
15293 if (tree r = cp_parser_requires_clause_opt (parser))
15294 reqs = conjoin_constraints (reqs, normalize_expression (r));
15295 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15298 /* Look for the `class' or 'typename' keywords. */
15299 cp_parser_type_parameter_key (parser);
15300 /* If the next token is an ellipsis, we have a template
15301 argument pack. */
15302 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15304 /* Consume the `...' token. */
15305 cp_lexer_consume_token (parser->lexer);
15306 maybe_warn_variadic_templates ();
15308 *is_parameter_pack = true;
15310 /* If the next token is an `=', then there is a
15311 default-argument. If the next token is a `>', we are at
15312 the end of the parameter-list. If the next token is a `,',
15313 then we are at the end of this parameter. */
15314 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15315 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15316 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15318 identifier = cp_parser_identifier (parser);
15319 /* Treat invalid names as if the parameter were nameless. */
15320 if (identifier == error_mark_node)
15321 identifier = NULL_TREE;
15323 else
15324 identifier = NULL_TREE;
15326 /* Create the template parameter. */
15327 parameter = finish_template_template_parm (class_type_node,
15328 identifier);
15330 /* If the next token is an `=', then there is a
15331 default-argument. */
15332 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15334 default_argument
15335 = cp_parser_default_template_template_argument (parser);
15337 /* Template parameter packs cannot have default
15338 arguments. */
15339 if (*is_parameter_pack)
15341 if (identifier)
15342 error_at (token->location,
15343 "template parameter pack %qD cannot "
15344 "have a default argument",
15345 identifier);
15346 else
15347 error_at (token->location, "template parameter packs cannot "
15348 "have default arguments");
15349 default_argument = NULL_TREE;
15352 else
15353 default_argument = NULL_TREE;
15355 /* Create the combined representation of the parameter and the
15356 default argument. */
15357 parameter = build_tree_list (default_argument, parameter);
15359 break;
15361 default:
15362 gcc_unreachable ();
15363 break;
15366 return parameter;
15369 /* Parse a template-id.
15371 template-id:
15372 template-name < template-argument-list [opt] >
15374 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15375 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15376 returned. Otherwise, if the template-name names a function, or set
15377 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15378 names a class, returns a TYPE_DECL for the specialization.
15380 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15381 uninstantiated templates. */
15383 static tree
15384 cp_parser_template_id (cp_parser *parser,
15385 bool template_keyword_p,
15386 bool check_dependency_p,
15387 enum tag_types tag_type,
15388 bool is_declaration)
15390 tree templ;
15391 tree arguments;
15392 tree template_id;
15393 cp_token_position start_of_id = 0;
15394 cp_token *next_token = NULL, *next_token_2 = NULL;
15395 bool is_identifier;
15397 /* If the next token corresponds to a template-id, there is no need
15398 to reparse it. */
15399 next_token = cp_lexer_peek_token (parser->lexer);
15400 if (next_token->type == CPP_TEMPLATE_ID)
15402 cp_lexer_consume_token (parser->lexer);
15403 return saved_checks_value (next_token->u.tree_check_value);
15406 /* Avoid performing name lookup if there is no possibility of
15407 finding a template-id. */
15408 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
15409 || (next_token->type == CPP_NAME
15410 && !cp_parser_nth_token_starts_template_argument_list_p
15411 (parser, 2)))
15413 cp_parser_error (parser, "expected template-id");
15414 return error_mark_node;
15417 /* Remember where the template-id starts. */
15418 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15419 start_of_id = cp_lexer_token_position (parser->lexer, false);
15421 push_deferring_access_checks (dk_deferred);
15423 /* Parse the template-name. */
15424 is_identifier = false;
15425 templ = cp_parser_template_name (parser, template_keyword_p,
15426 check_dependency_p,
15427 is_declaration,
15428 tag_type,
15429 &is_identifier);
15430 if (templ == error_mark_node || is_identifier)
15432 pop_deferring_access_checks ();
15433 return templ;
15436 /* Since we're going to preserve any side-effects from this parse, set up a
15437 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15438 in the template arguments. */
15439 tentative_firewall firewall (parser);
15441 /* If we find the sequence `[:' after a template-name, it's probably
15442 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15443 parse correctly the argument list. */
15444 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15445 == CPP_OPEN_SQUARE)
15446 && next_token->flags & DIGRAPH
15447 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15448 == CPP_COLON)
15449 && !(next_token_2->flags & PREV_WHITE))
15451 cp_parser_parse_tentatively (parser);
15452 /* Change `:' into `::'. */
15453 next_token_2->type = CPP_SCOPE;
15454 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15455 CPP_LESS. */
15456 cp_lexer_consume_token (parser->lexer);
15458 /* Parse the arguments. */
15459 arguments = cp_parser_enclosed_template_argument_list (parser);
15460 if (!cp_parser_parse_definitely (parser))
15462 /* If we couldn't parse an argument list, then we revert our changes
15463 and return simply an error. Maybe this is not a template-id
15464 after all. */
15465 next_token_2->type = CPP_COLON;
15466 cp_parser_error (parser, "expected %<<%>");
15467 pop_deferring_access_checks ();
15468 return error_mark_node;
15470 /* Otherwise, emit an error about the invalid digraph, but continue
15471 parsing because we got our argument list. */
15472 if (permerror (next_token->location,
15473 "%<<::%> cannot begin a template-argument list"))
15475 static bool hint = false;
15476 inform (next_token->location,
15477 "%<<:%> is an alternate spelling for %<[%>."
15478 " Insert whitespace between %<<%> and %<::%>");
15479 if (!hint && !flag_permissive)
15481 inform (next_token->location, "(if you use %<-fpermissive%> "
15482 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15483 "accept your code)");
15484 hint = true;
15488 else
15490 /* Look for the `<' that starts the template-argument-list. */
15491 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15493 pop_deferring_access_checks ();
15494 return error_mark_node;
15496 /* Parse the arguments. */
15497 arguments = cp_parser_enclosed_template_argument_list (parser);
15500 /* Build a representation of the specialization. */
15501 if (identifier_p (templ))
15502 template_id = build_min_nt_loc (next_token->location,
15503 TEMPLATE_ID_EXPR,
15504 templ, arguments);
15505 else if (DECL_TYPE_TEMPLATE_P (templ)
15506 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15508 bool entering_scope;
15509 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15510 template (rather than some instantiation thereof) only if
15511 is not nested within some other construct. For example, in
15512 "template <typename T> void f(T) { A<T>::", A<T> is just an
15513 instantiation of A. */
15514 entering_scope = (template_parm_scope_p ()
15515 && cp_lexer_next_token_is (parser->lexer,
15516 CPP_SCOPE));
15517 template_id
15518 = finish_template_type (templ, arguments, entering_scope);
15520 /* A template-like identifier may be a partial concept id. */
15521 else if (flag_concepts
15522 && (template_id = (cp_parser_maybe_partial_concept_id
15523 (parser, templ, arguments))))
15524 return template_id;
15525 else if (variable_template_p (templ))
15527 template_id = lookup_template_variable (templ, arguments);
15528 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15529 SET_EXPR_LOCATION (template_id, next_token->location);
15531 else
15533 /* If it's not a class-template or a template-template, it should be
15534 a function-template. */
15535 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15536 || TREE_CODE (templ) == OVERLOAD
15537 || BASELINK_P (templ)));
15539 template_id = lookup_template_function (templ, arguments);
15540 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15541 SET_EXPR_LOCATION (template_id, next_token->location);
15544 /* If parsing tentatively, replace the sequence of tokens that makes
15545 up the template-id with a CPP_TEMPLATE_ID token. That way,
15546 should we re-parse the token stream, we will not have to repeat
15547 the effort required to do the parse, nor will we issue duplicate
15548 error messages about problems during instantiation of the
15549 template. */
15550 if (start_of_id
15551 /* Don't do this if we had a parse error in a declarator; re-parsing
15552 might succeed if a name changes meaning (60361). */
15553 && !(cp_parser_error_occurred (parser)
15554 && cp_parser_parsing_tentatively (parser)
15555 && parser->in_declarator_p))
15557 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
15559 /* Reset the contents of the START_OF_ID token. */
15560 token->type = CPP_TEMPLATE_ID;
15562 /* Update the location to be of the form:
15563 template-name < template-argument-list [opt] >
15564 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15565 with caret == start at the start of the template-name,
15566 ranging until the closing '>'. */
15567 location_t finish_loc
15568 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15569 location_t combined_loc
15570 = make_location (token->location, token->location, finish_loc);
15571 token->location = combined_loc;
15573 /* We must mark the lookup as kept, so we don't throw it away on
15574 the first parse. */
15575 if (is_overloaded_fn (template_id))
15576 lookup_keep (get_fns (template_id), true);
15578 /* Retrieve any deferred checks. Do not pop this access checks yet
15579 so the memory will not be reclaimed during token replacing below. */
15580 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15581 token->u.tree_check_value->value = template_id;
15582 token->u.tree_check_value->checks = get_deferred_access_checks ();
15583 token->keyword = RID_MAX;
15585 /* Purge all subsequent tokens. */
15586 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15588 /* ??? Can we actually assume that, if template_id ==
15589 error_mark_node, we will have issued a diagnostic to the
15590 user, as opposed to simply marking the tentative parse as
15591 failed? */
15592 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15593 error_at (token->location, "parse error in template argument list");
15596 pop_to_parent_deferring_access_checks ();
15597 return template_id;
15600 /* Parse a template-name.
15602 template-name:
15603 identifier
15605 The standard should actually say:
15607 template-name:
15608 identifier
15609 operator-function-id
15611 A defect report has been filed about this issue.
15613 A conversion-function-id cannot be a template name because they cannot
15614 be part of a template-id. In fact, looking at this code:
15616 a.operator K<int>()
15618 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15619 It is impossible to call a templated conversion-function-id with an
15620 explicit argument list, since the only allowed template parameter is
15621 the type to which it is converting.
15623 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15624 `template' keyword, in a construction like:
15626 T::template f<3>()
15628 In that case `f' is taken to be a template-name, even though there
15629 is no way of knowing for sure.
15631 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15632 name refers to a set of overloaded functions, at least one of which
15633 is a template, or an IDENTIFIER_NODE with the name of the template,
15634 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15635 names are looked up inside uninstantiated templates. */
15637 static tree
15638 cp_parser_template_name (cp_parser* parser,
15639 bool template_keyword_p,
15640 bool check_dependency_p,
15641 bool is_declaration,
15642 enum tag_types tag_type,
15643 bool *is_identifier)
15645 tree identifier;
15646 tree decl;
15647 cp_token *token = cp_lexer_peek_token (parser->lexer);
15649 /* If the next token is `operator', then we have either an
15650 operator-function-id or a conversion-function-id. */
15651 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15653 /* We don't know whether we're looking at an
15654 operator-function-id or a conversion-function-id. */
15655 cp_parser_parse_tentatively (parser);
15656 /* Try an operator-function-id. */
15657 identifier = cp_parser_operator_function_id (parser);
15658 /* If that didn't work, try a conversion-function-id. */
15659 if (!cp_parser_parse_definitely (parser))
15661 cp_parser_error (parser, "expected template-name");
15662 return error_mark_node;
15665 /* Look for the identifier. */
15666 else
15667 identifier = cp_parser_identifier (parser);
15669 /* If we didn't find an identifier, we don't have a template-id. */
15670 if (identifier == error_mark_node)
15671 return error_mark_node;
15673 /* If the name immediately followed the `template' keyword, then it
15674 is a template-name. However, if the next token is not `<', then
15675 we do not treat it as a template-name, since it is not being used
15676 as part of a template-id. This enables us to handle constructs
15677 like:
15679 template <typename T> struct S { S(); };
15680 template <typename T> S<T>::S();
15682 correctly. We would treat `S' as a template -- if it were `S<T>'
15683 -- but we do not if there is no `<'. */
15685 if (processing_template_decl
15686 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15688 /* In a declaration, in a dependent context, we pretend that the
15689 "template" keyword was present in order to improve error
15690 recovery. For example, given:
15692 template <typename T> void f(T::X<int>);
15694 we want to treat "X<int>" as a template-id. */
15695 if (is_declaration
15696 && !template_keyword_p
15697 && parser->scope && TYPE_P (parser->scope)
15698 && check_dependency_p
15699 && dependent_scope_p (parser->scope)
15700 /* Do not do this for dtors (or ctors), since they never
15701 need the template keyword before their name. */
15702 && !constructor_name_p (identifier, parser->scope))
15704 cp_token_position start = 0;
15706 /* Explain what went wrong. */
15707 error_at (token->location, "non-template %qD used as template",
15708 identifier);
15709 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15710 parser->scope, identifier);
15711 /* If parsing tentatively, find the location of the "<" token. */
15712 if (cp_parser_simulate_error (parser))
15713 start = cp_lexer_token_position (parser->lexer, true);
15714 /* Parse the template arguments so that we can issue error
15715 messages about them. */
15716 cp_lexer_consume_token (parser->lexer);
15717 cp_parser_enclosed_template_argument_list (parser);
15718 /* Skip tokens until we find a good place from which to
15719 continue parsing. */
15720 cp_parser_skip_to_closing_parenthesis (parser,
15721 /*recovering=*/true,
15722 /*or_comma=*/true,
15723 /*consume_paren=*/false);
15724 /* If parsing tentatively, permanently remove the
15725 template argument list. That will prevent duplicate
15726 error messages from being issued about the missing
15727 "template" keyword. */
15728 if (start)
15729 cp_lexer_purge_tokens_after (parser->lexer, start);
15730 if (is_identifier)
15731 *is_identifier = true;
15732 parser->context->object_type = NULL_TREE;
15733 return identifier;
15736 /* If the "template" keyword is present, then there is generally
15737 no point in doing name-lookup, so we just return IDENTIFIER.
15738 But, if the qualifying scope is non-dependent then we can
15739 (and must) do name-lookup normally. */
15740 if (template_keyword_p
15741 && (!parser->scope
15742 || (TYPE_P (parser->scope)
15743 && dependent_type_p (parser->scope))))
15745 /* We're optimizing away the call to cp_parser_lookup_name, but we
15746 still need to do this. */
15747 parser->context->object_type = NULL_TREE;
15748 return identifier;
15752 /* Look up the name. */
15753 decl = cp_parser_lookup_name (parser, identifier,
15754 tag_type,
15755 /*is_template=*/true,
15756 /*is_namespace=*/false,
15757 check_dependency_p,
15758 /*ambiguous_decls=*/NULL,
15759 token->location);
15761 decl = strip_using_decl (decl);
15763 /* If DECL is a template, then the name was a template-name. */
15764 if (TREE_CODE (decl) == TEMPLATE_DECL)
15766 if (TREE_DEPRECATED (decl)
15767 && deprecated_state != DEPRECATED_SUPPRESS)
15768 warn_deprecated_use (decl, NULL_TREE);
15770 else
15772 /* The standard does not explicitly indicate whether a name that
15773 names a set of overloaded declarations, some of which are
15774 templates, is a template-name. However, such a name should
15775 be a template-name; otherwise, there is no way to form a
15776 template-id for the overloaded templates. */
15777 bool found = false;
15779 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
15780 !found && iter; ++iter)
15781 if (TREE_CODE (*iter) == TEMPLATE_DECL)
15782 found = true;
15784 if (!found)
15786 /* The name does not name a template. */
15787 cp_parser_error (parser, "expected template-name");
15788 return error_mark_node;
15792 /* If DECL is dependent, and refers to a function, then just return
15793 its name; we will look it up again during template instantiation. */
15794 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15796 tree scope = ovl_scope (decl);
15797 if (TYPE_P (scope) && dependent_type_p (scope))
15798 return identifier;
15801 return decl;
15804 /* Parse a template-argument-list.
15806 template-argument-list:
15807 template-argument ... [opt]
15808 template-argument-list , template-argument ... [opt]
15810 Returns a TREE_VEC containing the arguments. */
15812 static tree
15813 cp_parser_template_argument_list (cp_parser* parser)
15815 tree fixed_args[10];
15816 unsigned n_args = 0;
15817 unsigned alloced = 10;
15818 tree *arg_ary = fixed_args;
15819 tree vec;
15820 bool saved_in_template_argument_list_p;
15821 bool saved_ice_p;
15822 bool saved_non_ice_p;
15824 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15825 parser->in_template_argument_list_p = true;
15826 /* Even if the template-id appears in an integral
15827 constant-expression, the contents of the argument list do
15828 not. */
15829 saved_ice_p = parser->integral_constant_expression_p;
15830 parser->integral_constant_expression_p = false;
15831 saved_non_ice_p = parser->non_integral_constant_expression_p;
15832 parser->non_integral_constant_expression_p = false;
15834 /* Parse the arguments. */
15837 tree argument;
15839 if (n_args)
15840 /* Consume the comma. */
15841 cp_lexer_consume_token (parser->lexer);
15843 /* Parse the template-argument. */
15844 argument = cp_parser_template_argument (parser);
15846 /* If the next token is an ellipsis, we're expanding a template
15847 argument pack. */
15848 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15850 if (argument == error_mark_node)
15852 cp_token *token = cp_lexer_peek_token (parser->lexer);
15853 error_at (token->location,
15854 "expected parameter pack before %<...%>");
15856 /* Consume the `...' token. */
15857 cp_lexer_consume_token (parser->lexer);
15859 /* Make the argument into a TYPE_PACK_EXPANSION or
15860 EXPR_PACK_EXPANSION. */
15861 argument = make_pack_expansion (argument);
15864 if (n_args == alloced)
15866 alloced *= 2;
15868 if (arg_ary == fixed_args)
15870 arg_ary = XNEWVEC (tree, alloced);
15871 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15873 else
15874 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15876 arg_ary[n_args++] = argument;
15878 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15880 vec = make_tree_vec (n_args);
15882 while (n_args--)
15883 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15885 if (arg_ary != fixed_args)
15886 free (arg_ary);
15887 parser->non_integral_constant_expression_p = saved_non_ice_p;
15888 parser->integral_constant_expression_p = saved_ice_p;
15889 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15890 if (CHECKING_P)
15891 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15892 return vec;
15895 /* Parse a template-argument.
15897 template-argument:
15898 assignment-expression
15899 type-id
15900 id-expression
15902 The representation is that of an assignment-expression, type-id, or
15903 id-expression -- except that the qualified id-expression is
15904 evaluated, so that the value returned is either a DECL or an
15905 OVERLOAD.
15907 Although the standard says "assignment-expression", it forbids
15908 throw-expressions or assignments in the template argument.
15909 Therefore, we use "conditional-expression" instead. */
15911 static tree
15912 cp_parser_template_argument (cp_parser* parser)
15914 tree argument;
15915 bool template_p;
15916 bool address_p;
15917 bool maybe_type_id = false;
15918 cp_token *token = NULL, *argument_start_token = NULL;
15919 location_t loc = 0;
15920 cp_id_kind idk;
15922 /* There's really no way to know what we're looking at, so we just
15923 try each alternative in order.
15925 [temp.arg]
15927 In a template-argument, an ambiguity between a type-id and an
15928 expression is resolved to a type-id, regardless of the form of
15929 the corresponding template-parameter.
15931 Therefore, we try a type-id first. */
15932 cp_parser_parse_tentatively (parser);
15933 argument = cp_parser_template_type_arg (parser);
15934 /* If there was no error parsing the type-id but the next token is a
15935 '>>', our behavior depends on which dialect of C++ we're
15936 parsing. In C++98, we probably found a typo for '> >'. But there
15937 are type-id which are also valid expressions. For instance:
15939 struct X { int operator >> (int); };
15940 template <int V> struct Foo {};
15941 Foo<X () >> 5> r;
15943 Here 'X()' is a valid type-id of a function type, but the user just
15944 wanted to write the expression "X() >> 5". Thus, we remember that we
15945 found a valid type-id, but we still try to parse the argument as an
15946 expression to see what happens.
15948 In C++0x, the '>>' will be considered two separate '>'
15949 tokens. */
15950 if (!cp_parser_error_occurred (parser)
15951 && cxx_dialect == cxx98
15952 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15954 maybe_type_id = true;
15955 cp_parser_abort_tentative_parse (parser);
15957 else
15959 /* If the next token isn't a `,' or a `>', then this argument wasn't
15960 really finished. This means that the argument is not a valid
15961 type-id. */
15962 if (!cp_parser_next_token_ends_template_argument_p (parser))
15963 cp_parser_error (parser, "expected template-argument");
15964 /* If that worked, we're done. */
15965 if (cp_parser_parse_definitely (parser))
15966 return argument;
15968 /* We're still not sure what the argument will be. */
15969 cp_parser_parse_tentatively (parser);
15970 /* Try a template. */
15971 argument_start_token = cp_lexer_peek_token (parser->lexer);
15972 argument = cp_parser_id_expression (parser,
15973 /*template_keyword_p=*/false,
15974 /*check_dependency_p=*/true,
15975 &template_p,
15976 /*declarator_p=*/false,
15977 /*optional_p=*/false);
15978 /* If the next token isn't a `,' or a `>', then this argument wasn't
15979 really finished. */
15980 if (!cp_parser_next_token_ends_template_argument_p (parser))
15981 cp_parser_error (parser, "expected template-argument");
15982 if (!cp_parser_error_occurred (parser))
15984 /* Figure out what is being referred to. If the id-expression
15985 was for a class template specialization, then we will have a
15986 TYPE_DECL at this point. There is no need to do name lookup
15987 at this point in that case. */
15988 if (TREE_CODE (argument) != TYPE_DECL)
15989 argument = cp_parser_lookup_name (parser, argument,
15990 none_type,
15991 /*is_template=*/template_p,
15992 /*is_namespace=*/false,
15993 /*check_dependency=*/true,
15994 /*ambiguous_decls=*/NULL,
15995 argument_start_token->location);
15996 /* Handle a constrained-type-specifier for a non-type template
15997 parameter. */
15998 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15999 argument = decl;
16000 else if (TREE_CODE (argument) != TEMPLATE_DECL
16001 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16002 cp_parser_error (parser, "expected template-name");
16004 if (cp_parser_parse_definitely (parser))
16006 if (TREE_DEPRECATED (argument))
16007 warn_deprecated_use (argument, NULL_TREE);
16008 return argument;
16010 /* It must be a non-type argument. In C++17 any constant-expression is
16011 allowed. */
16012 if (cxx_dialect > cxx14)
16013 goto general_expr;
16015 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16017 -- an integral constant-expression of integral or enumeration
16018 type; or
16020 -- the name of a non-type template-parameter; or
16022 -- the name of an object or function with external linkage...
16024 -- the address of an object or function with external linkage...
16026 -- a pointer to member... */
16027 /* Look for a non-type template parameter. */
16028 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16030 cp_parser_parse_tentatively (parser);
16031 argument = cp_parser_primary_expression (parser,
16032 /*address_p=*/false,
16033 /*cast_p=*/false,
16034 /*template_arg_p=*/true,
16035 &idk);
16036 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16037 || !cp_parser_next_token_ends_template_argument_p (parser))
16038 cp_parser_simulate_error (parser);
16039 if (cp_parser_parse_definitely (parser))
16040 return argument;
16043 /* If the next token is "&", the argument must be the address of an
16044 object or function with external linkage. */
16045 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16046 if (address_p)
16048 loc = cp_lexer_peek_token (parser->lexer)->location;
16049 cp_lexer_consume_token (parser->lexer);
16051 /* See if we might have an id-expression. */
16052 token = cp_lexer_peek_token (parser->lexer);
16053 if (token->type == CPP_NAME
16054 || token->keyword == RID_OPERATOR
16055 || token->type == CPP_SCOPE
16056 || token->type == CPP_TEMPLATE_ID
16057 || token->type == CPP_NESTED_NAME_SPECIFIER)
16059 cp_parser_parse_tentatively (parser);
16060 argument = cp_parser_primary_expression (parser,
16061 address_p,
16062 /*cast_p=*/false,
16063 /*template_arg_p=*/true,
16064 &idk);
16065 if (cp_parser_error_occurred (parser)
16066 || !cp_parser_next_token_ends_template_argument_p (parser))
16067 cp_parser_abort_tentative_parse (parser);
16068 else
16070 tree probe;
16072 if (INDIRECT_REF_P (argument))
16074 /* Strip the dereference temporarily. */
16075 gcc_assert (REFERENCE_REF_P (argument));
16076 argument = TREE_OPERAND (argument, 0);
16079 /* If we're in a template, we represent a qualified-id referring
16080 to a static data member as a SCOPE_REF even if the scope isn't
16081 dependent so that we can check access control later. */
16082 probe = argument;
16083 if (TREE_CODE (probe) == SCOPE_REF)
16084 probe = TREE_OPERAND (probe, 1);
16085 if (VAR_P (probe))
16087 /* A variable without external linkage might still be a
16088 valid constant-expression, so no error is issued here
16089 if the external-linkage check fails. */
16090 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16091 cp_parser_simulate_error (parser);
16093 else if (is_overloaded_fn (argument))
16094 /* All overloaded functions are allowed; if the external
16095 linkage test does not pass, an error will be issued
16096 later. */
16098 else if (address_p
16099 && (TREE_CODE (argument) == OFFSET_REF
16100 || TREE_CODE (argument) == SCOPE_REF))
16101 /* A pointer-to-member. */
16103 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16105 else
16106 cp_parser_simulate_error (parser);
16108 if (cp_parser_parse_definitely (parser))
16110 if (address_p)
16111 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16112 tf_warning_or_error);
16113 else
16114 argument = convert_from_reference (argument);
16115 return argument;
16119 /* If the argument started with "&", there are no other valid
16120 alternatives at this point. */
16121 if (address_p)
16123 cp_parser_error (parser, "invalid non-type template argument");
16124 return error_mark_node;
16127 general_expr:
16128 /* If the argument wasn't successfully parsed as a type-id followed
16129 by '>>', the argument can only be a constant expression now.
16130 Otherwise, we try parsing the constant-expression tentatively,
16131 because the argument could really be a type-id. */
16132 if (maybe_type_id)
16133 cp_parser_parse_tentatively (parser);
16135 if (cxx_dialect <= cxx14)
16136 argument = cp_parser_constant_expression (parser);
16137 else
16139 /* With C++17 generalized non-type template arguments we need to handle
16140 lvalue constant expressions, too. */
16141 argument = cp_parser_assignment_expression (parser);
16142 require_potential_constant_expression (argument);
16145 if (!maybe_type_id)
16146 return argument;
16147 if (!cp_parser_next_token_ends_template_argument_p (parser))
16148 cp_parser_error (parser, "expected template-argument");
16149 if (cp_parser_parse_definitely (parser))
16150 return argument;
16151 /* We did our best to parse the argument as a non type-id, but that
16152 was the only alternative that matched (albeit with a '>' after
16153 it). We can assume it's just a typo from the user, and a
16154 diagnostic will then be issued. */
16155 return cp_parser_template_type_arg (parser);
16158 /* Parse an explicit-instantiation.
16160 explicit-instantiation:
16161 template declaration
16163 Although the standard says `declaration', what it really means is:
16165 explicit-instantiation:
16166 template decl-specifier-seq [opt] declarator [opt] ;
16168 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16169 supposed to be allowed. A defect report has been filed about this
16170 issue.
16172 GNU Extension:
16174 explicit-instantiation:
16175 storage-class-specifier template
16176 decl-specifier-seq [opt] declarator [opt] ;
16177 function-specifier template
16178 decl-specifier-seq [opt] declarator [opt] ; */
16180 static void
16181 cp_parser_explicit_instantiation (cp_parser* parser)
16183 int declares_class_or_enum;
16184 cp_decl_specifier_seq decl_specifiers;
16185 tree extension_specifier = NULL_TREE;
16187 timevar_push (TV_TEMPLATE_INST);
16189 /* Look for an (optional) storage-class-specifier or
16190 function-specifier. */
16191 if (cp_parser_allow_gnu_extensions_p (parser))
16193 extension_specifier
16194 = cp_parser_storage_class_specifier_opt (parser);
16195 if (!extension_specifier)
16196 extension_specifier
16197 = cp_parser_function_specifier_opt (parser,
16198 /*decl_specs=*/NULL);
16201 /* Look for the `template' keyword. */
16202 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16203 /* Let the front end know that we are processing an explicit
16204 instantiation. */
16205 begin_explicit_instantiation ();
16206 /* [temp.explicit] says that we are supposed to ignore access
16207 control while processing explicit instantiation directives. */
16208 push_deferring_access_checks (dk_no_check);
16209 /* Parse a decl-specifier-seq. */
16210 cp_parser_decl_specifier_seq (parser,
16211 CP_PARSER_FLAGS_OPTIONAL,
16212 &decl_specifiers,
16213 &declares_class_or_enum);
16214 /* If there was exactly one decl-specifier, and it declared a class,
16215 and there's no declarator, then we have an explicit type
16216 instantiation. */
16217 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16219 tree type;
16221 type = check_tag_decl (&decl_specifiers,
16222 /*explicit_type_instantiation_p=*/true);
16223 /* Turn access control back on for names used during
16224 template instantiation. */
16225 pop_deferring_access_checks ();
16226 if (type)
16227 do_type_instantiation (type, extension_specifier,
16228 /*complain=*/tf_error);
16230 else
16232 cp_declarator *declarator;
16233 tree decl;
16235 /* Parse the declarator. */
16236 declarator
16237 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16238 /*ctor_dtor_or_conv_p=*/NULL,
16239 /*parenthesized_p=*/NULL,
16240 /*member_p=*/false,
16241 /*friend_p=*/false);
16242 if (declares_class_or_enum & 2)
16243 cp_parser_check_for_definition_in_return_type (declarator,
16244 decl_specifiers.type,
16245 decl_specifiers.locations[ds_type_spec]);
16246 if (declarator != cp_error_declarator)
16248 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16249 permerror (decl_specifiers.locations[ds_inline],
16250 "explicit instantiation shall not use"
16251 " %<inline%> specifier");
16252 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16253 permerror (decl_specifiers.locations[ds_constexpr],
16254 "explicit instantiation shall not use"
16255 " %<constexpr%> specifier");
16257 decl = grokdeclarator (declarator, &decl_specifiers,
16258 NORMAL, 0, &decl_specifiers.attributes);
16259 /* Turn access control back on for names used during
16260 template instantiation. */
16261 pop_deferring_access_checks ();
16262 /* Do the explicit instantiation. */
16263 do_decl_instantiation (decl, extension_specifier);
16265 else
16267 pop_deferring_access_checks ();
16268 /* Skip the body of the explicit instantiation. */
16269 cp_parser_skip_to_end_of_statement (parser);
16272 /* We're done with the instantiation. */
16273 end_explicit_instantiation ();
16275 cp_parser_consume_semicolon_at_end_of_statement (parser);
16277 timevar_pop (TV_TEMPLATE_INST);
16280 /* Parse an explicit-specialization.
16282 explicit-specialization:
16283 template < > declaration
16285 Although the standard says `declaration', what it really means is:
16287 explicit-specialization:
16288 template <> decl-specifier [opt] init-declarator [opt] ;
16289 template <> function-definition
16290 template <> explicit-specialization
16291 template <> template-declaration */
16293 static void
16294 cp_parser_explicit_specialization (cp_parser* parser)
16296 bool need_lang_pop;
16297 cp_token *token = cp_lexer_peek_token (parser->lexer);
16299 /* Look for the `template' keyword. */
16300 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16301 /* Look for the `<'. */
16302 cp_parser_require (parser, CPP_LESS, RT_LESS);
16303 /* Look for the `>'. */
16304 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16305 /* We have processed another parameter list. */
16306 ++parser->num_template_parameter_lists;
16307 /* [temp]
16309 A template ... explicit specialization ... shall not have C
16310 linkage. */
16311 if (current_lang_name == lang_name_c)
16313 error_at (token->location, "template specialization with C linkage");
16314 /* Give it C++ linkage to avoid confusing other parts of the
16315 front end. */
16316 push_lang_context (lang_name_cplusplus);
16317 need_lang_pop = true;
16319 else
16320 need_lang_pop = false;
16321 /* Let the front end know that we are beginning a specialization. */
16322 if (!begin_specialization ())
16324 end_specialization ();
16325 return;
16328 /* If the next keyword is `template', we need to figure out whether
16329 or not we're looking a template-declaration. */
16330 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16332 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16333 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16334 cp_parser_template_declaration_after_export (parser,
16335 /*member_p=*/false);
16336 else
16337 cp_parser_explicit_specialization (parser);
16339 else
16340 /* Parse the dependent declaration. */
16341 cp_parser_single_declaration (parser,
16342 /*checks=*/NULL,
16343 /*member_p=*/false,
16344 /*explicit_specialization_p=*/true,
16345 /*friend_p=*/NULL);
16346 /* We're done with the specialization. */
16347 end_specialization ();
16348 /* For the erroneous case of a template with C linkage, we pushed an
16349 implicit C++ linkage scope; exit that scope now. */
16350 if (need_lang_pop)
16351 pop_lang_context ();
16352 /* We're done with this parameter list. */
16353 --parser->num_template_parameter_lists;
16356 /* Parse a type-specifier.
16358 type-specifier:
16359 simple-type-specifier
16360 class-specifier
16361 enum-specifier
16362 elaborated-type-specifier
16363 cv-qualifier
16365 GNU Extension:
16367 type-specifier:
16368 __complex__
16370 Returns a representation of the type-specifier. For a
16371 class-specifier, enum-specifier, or elaborated-type-specifier, a
16372 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16374 The parser flags FLAGS is used to control type-specifier parsing.
16376 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16377 in a decl-specifier-seq.
16379 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16380 class-specifier, enum-specifier, or elaborated-type-specifier, then
16381 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16382 if a type is declared; 2 if it is defined. Otherwise, it is set to
16383 zero.
16385 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16386 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16387 is set to FALSE. */
16389 static tree
16390 cp_parser_type_specifier (cp_parser* parser,
16391 cp_parser_flags flags,
16392 cp_decl_specifier_seq *decl_specs,
16393 bool is_declaration,
16394 int* declares_class_or_enum,
16395 bool* is_cv_qualifier)
16397 tree type_spec = NULL_TREE;
16398 cp_token *token;
16399 enum rid keyword;
16400 cp_decl_spec ds = ds_last;
16402 /* Assume this type-specifier does not declare a new type. */
16403 if (declares_class_or_enum)
16404 *declares_class_or_enum = 0;
16405 /* And that it does not specify a cv-qualifier. */
16406 if (is_cv_qualifier)
16407 *is_cv_qualifier = false;
16408 /* Peek at the next token. */
16409 token = cp_lexer_peek_token (parser->lexer);
16411 /* If we're looking at a keyword, we can use that to guide the
16412 production we choose. */
16413 keyword = token->keyword;
16414 switch (keyword)
16416 case RID_ENUM:
16417 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16418 goto elaborated_type_specifier;
16420 /* Look for the enum-specifier. */
16421 type_spec = cp_parser_enum_specifier (parser);
16422 /* If that worked, we're done. */
16423 if (type_spec)
16425 if (declares_class_or_enum)
16426 *declares_class_or_enum = 2;
16427 if (decl_specs)
16428 cp_parser_set_decl_spec_type (decl_specs,
16429 type_spec,
16430 token,
16431 /*type_definition_p=*/true);
16432 return type_spec;
16434 else
16435 goto elaborated_type_specifier;
16437 /* Any of these indicate either a class-specifier, or an
16438 elaborated-type-specifier. */
16439 case RID_CLASS:
16440 case RID_STRUCT:
16441 case RID_UNION:
16442 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16443 goto elaborated_type_specifier;
16445 /* Parse tentatively so that we can back up if we don't find a
16446 class-specifier. */
16447 cp_parser_parse_tentatively (parser);
16448 /* Look for the class-specifier. */
16449 type_spec = cp_parser_class_specifier (parser);
16450 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16451 /* If that worked, we're done. */
16452 if (cp_parser_parse_definitely (parser))
16454 if (declares_class_or_enum)
16455 *declares_class_or_enum = 2;
16456 if (decl_specs)
16457 cp_parser_set_decl_spec_type (decl_specs,
16458 type_spec,
16459 token,
16460 /*type_definition_p=*/true);
16461 return type_spec;
16464 /* Fall through. */
16465 elaborated_type_specifier:
16466 /* We're declaring (not defining) a class or enum. */
16467 if (declares_class_or_enum)
16468 *declares_class_or_enum = 1;
16470 /* Fall through. */
16471 case RID_TYPENAME:
16472 /* Look for an elaborated-type-specifier. */
16473 type_spec
16474 = (cp_parser_elaborated_type_specifier
16475 (parser,
16476 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16477 is_declaration));
16478 if (decl_specs)
16479 cp_parser_set_decl_spec_type (decl_specs,
16480 type_spec,
16481 token,
16482 /*type_definition_p=*/false);
16483 return type_spec;
16485 case RID_CONST:
16486 ds = ds_const;
16487 if (is_cv_qualifier)
16488 *is_cv_qualifier = true;
16489 break;
16491 case RID_VOLATILE:
16492 ds = ds_volatile;
16493 if (is_cv_qualifier)
16494 *is_cv_qualifier = true;
16495 break;
16497 case RID_RESTRICT:
16498 ds = ds_restrict;
16499 if (is_cv_qualifier)
16500 *is_cv_qualifier = true;
16501 break;
16503 case RID_COMPLEX:
16504 /* The `__complex__' keyword is a GNU extension. */
16505 ds = ds_complex;
16506 break;
16508 default:
16509 break;
16512 /* Handle simple keywords. */
16513 if (ds != ds_last)
16515 if (decl_specs)
16517 set_and_check_decl_spec_loc (decl_specs, ds, token);
16518 decl_specs->any_specifiers_p = true;
16520 return cp_lexer_consume_token (parser->lexer)->u.value;
16523 /* If we do not already have a type-specifier, assume we are looking
16524 at a simple-type-specifier. */
16525 type_spec = cp_parser_simple_type_specifier (parser,
16526 decl_specs,
16527 flags);
16529 /* If we didn't find a type-specifier, and a type-specifier was not
16530 optional in this context, issue an error message. */
16531 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16533 cp_parser_error (parser, "expected type specifier");
16534 return error_mark_node;
16537 return type_spec;
16540 /* Parse a simple-type-specifier.
16542 simple-type-specifier:
16543 :: [opt] nested-name-specifier [opt] type-name
16544 :: [opt] nested-name-specifier template template-id
16545 char
16546 wchar_t
16547 bool
16548 short
16550 long
16551 signed
16552 unsigned
16553 float
16554 double
16555 void
16557 C++11 Extension:
16559 simple-type-specifier:
16560 auto
16561 decltype ( expression )
16562 char16_t
16563 char32_t
16564 __underlying_type ( type-id )
16566 C++17 extension:
16568 nested-name-specifier(opt) template-name
16570 GNU Extension:
16572 simple-type-specifier:
16573 __int128
16574 __typeof__ unary-expression
16575 __typeof__ ( type-id )
16576 __typeof__ ( type-id ) { initializer-list , [opt] }
16578 Concepts Extension:
16580 simple-type-specifier:
16581 constrained-type-specifier
16583 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16584 appropriately updated. */
16586 static tree
16587 cp_parser_simple_type_specifier (cp_parser* parser,
16588 cp_decl_specifier_seq *decl_specs,
16589 cp_parser_flags flags)
16591 tree type = NULL_TREE;
16592 cp_token *token;
16593 int idx;
16595 /* Peek at the next token. */
16596 token = cp_lexer_peek_token (parser->lexer);
16598 /* If we're looking at a keyword, things are easy. */
16599 switch (token->keyword)
16601 case RID_CHAR:
16602 if (decl_specs)
16603 decl_specs->explicit_char_p = true;
16604 type = char_type_node;
16605 break;
16606 case RID_CHAR16:
16607 type = char16_type_node;
16608 break;
16609 case RID_CHAR32:
16610 type = char32_type_node;
16611 break;
16612 case RID_WCHAR:
16613 type = wchar_type_node;
16614 break;
16615 case RID_BOOL:
16616 type = boolean_type_node;
16617 break;
16618 case RID_SHORT:
16619 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16620 type = short_integer_type_node;
16621 break;
16622 case RID_INT:
16623 if (decl_specs)
16624 decl_specs->explicit_int_p = true;
16625 type = integer_type_node;
16626 break;
16627 case RID_INT_N_0:
16628 case RID_INT_N_1:
16629 case RID_INT_N_2:
16630 case RID_INT_N_3:
16631 idx = token->keyword - RID_INT_N_0;
16632 if (! int_n_enabled_p [idx])
16633 break;
16634 if (decl_specs)
16636 decl_specs->explicit_intN_p = true;
16637 decl_specs->int_n_idx = idx;
16639 type = int_n_trees [idx].signed_type;
16640 break;
16641 case RID_LONG:
16642 if (decl_specs)
16643 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16644 type = long_integer_type_node;
16645 break;
16646 case RID_SIGNED:
16647 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16648 type = integer_type_node;
16649 break;
16650 case RID_UNSIGNED:
16651 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16652 type = unsigned_type_node;
16653 break;
16654 case RID_FLOAT:
16655 type = float_type_node;
16656 break;
16657 case RID_DOUBLE:
16658 type = double_type_node;
16659 break;
16660 case RID_VOID:
16661 type = void_type_node;
16662 break;
16664 case RID_AUTO:
16665 maybe_warn_cpp0x (CPP0X_AUTO);
16666 if (parser->auto_is_implicit_function_template_parm_p)
16668 /* The 'auto' might be the placeholder return type for a function decl
16669 with trailing return type. */
16670 bool have_trailing_return_fn_decl = false;
16672 cp_parser_parse_tentatively (parser);
16673 cp_lexer_consume_token (parser->lexer);
16674 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16675 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16676 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16677 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16679 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16681 cp_lexer_consume_token (parser->lexer);
16682 cp_parser_skip_to_closing_parenthesis (parser,
16683 /*recovering*/false,
16684 /*or_comma*/false,
16685 /*consume_paren*/true);
16686 continue;
16689 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16691 have_trailing_return_fn_decl = true;
16692 break;
16695 cp_lexer_consume_token (parser->lexer);
16697 cp_parser_abort_tentative_parse (parser);
16699 if (have_trailing_return_fn_decl)
16701 type = make_auto ();
16702 break;
16705 if (cxx_dialect >= cxx14)
16707 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16708 type = TREE_TYPE (type);
16710 else
16711 type = error_mark_node;
16713 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16715 if (cxx_dialect < cxx14)
16716 error_at (token->location,
16717 "use of %<auto%> in lambda parameter declaration "
16718 "only available with "
16719 "-std=c++14 or -std=gnu++14");
16721 else if (cxx_dialect < cxx14)
16722 error_at (token->location,
16723 "use of %<auto%> in parameter declaration "
16724 "only available with "
16725 "-std=c++14 or -std=gnu++14");
16726 else if (!flag_concepts)
16727 pedwarn (token->location, OPT_Wpedantic,
16728 "ISO C++ forbids use of %<auto%> in parameter "
16729 "declaration");
16731 else
16732 type = make_auto ();
16733 break;
16735 case RID_DECLTYPE:
16736 /* Since DR 743, decltype can either be a simple-type-specifier by
16737 itself or begin a nested-name-specifier. Parsing it will replace
16738 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16739 handling below decide what to do. */
16740 cp_parser_decltype (parser);
16741 cp_lexer_set_token_position (parser->lexer, token);
16742 break;
16744 case RID_TYPEOF:
16745 /* Consume the `typeof' token. */
16746 cp_lexer_consume_token (parser->lexer);
16747 /* Parse the operand to `typeof'. */
16748 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16749 /* If it is not already a TYPE, take its type. */
16750 if (!TYPE_P (type))
16751 type = finish_typeof (type);
16753 if (decl_specs)
16754 cp_parser_set_decl_spec_type (decl_specs, type,
16755 token,
16756 /*type_definition_p=*/false);
16758 return type;
16760 case RID_UNDERLYING_TYPE:
16761 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16762 if (decl_specs)
16763 cp_parser_set_decl_spec_type (decl_specs, type,
16764 token,
16765 /*type_definition_p=*/false);
16767 return type;
16769 case RID_BASES:
16770 case RID_DIRECT_BASES:
16771 type = cp_parser_trait_expr (parser, token->keyword);
16772 if (decl_specs)
16773 cp_parser_set_decl_spec_type (decl_specs, type,
16774 token,
16775 /*type_definition_p=*/false);
16776 return type;
16777 default:
16778 break;
16781 /* If token is an already-parsed decltype not followed by ::,
16782 it's a simple-type-specifier. */
16783 if (token->type == CPP_DECLTYPE
16784 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16786 type = saved_checks_value (token->u.tree_check_value);
16787 if (decl_specs)
16789 cp_parser_set_decl_spec_type (decl_specs, type,
16790 token,
16791 /*type_definition_p=*/false);
16792 /* Remember that we are handling a decltype in order to
16793 implement the resolution of DR 1510 when the argument
16794 isn't instantiation dependent. */
16795 decl_specs->decltype_p = true;
16797 cp_lexer_consume_token (parser->lexer);
16798 return type;
16801 /* If the type-specifier was for a built-in type, we're done. */
16802 if (type)
16804 /* Record the type. */
16805 if (decl_specs
16806 && (token->keyword != RID_SIGNED
16807 && token->keyword != RID_UNSIGNED
16808 && token->keyword != RID_SHORT
16809 && token->keyword != RID_LONG))
16810 cp_parser_set_decl_spec_type (decl_specs,
16811 type,
16812 token,
16813 /*type_definition_p=*/false);
16814 if (decl_specs)
16815 decl_specs->any_specifiers_p = true;
16817 /* Consume the token. */
16818 cp_lexer_consume_token (parser->lexer);
16820 if (type == error_mark_node)
16821 return error_mark_node;
16823 /* There is no valid C++ program where a non-template type is
16824 followed by a "<". That usually indicates that the user thought
16825 that the type was a template. */
16826 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16827 token->location);
16829 return TYPE_NAME (type);
16832 /* The type-specifier must be a user-defined type. */
16833 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16835 bool qualified_p;
16836 bool global_p;
16838 /* Don't gobble tokens or issue error messages if this is an
16839 optional type-specifier. */
16840 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16841 cp_parser_parse_tentatively (parser);
16843 token = cp_lexer_peek_token (parser->lexer);
16845 /* Look for the optional `::' operator. */
16846 global_p
16847 = (cp_parser_global_scope_opt (parser,
16848 /*current_scope_valid_p=*/false)
16849 != NULL_TREE);
16850 /* Look for the nested-name specifier. */
16851 qualified_p
16852 = (cp_parser_nested_name_specifier_opt (parser,
16853 /*typename_keyword_p=*/false,
16854 /*check_dependency_p=*/true,
16855 /*type_p=*/false,
16856 /*is_declaration=*/false)
16857 != NULL_TREE);
16858 /* If we have seen a nested-name-specifier, and the next token
16859 is `template', then we are using the template-id production. */
16860 if (parser->scope
16861 && cp_parser_optional_template_keyword (parser))
16863 /* Look for the template-id. */
16864 type = cp_parser_template_id (parser,
16865 /*template_keyword_p=*/true,
16866 /*check_dependency_p=*/true,
16867 none_type,
16868 /*is_declaration=*/false);
16869 /* If the template-id did not name a type, we are out of
16870 luck. */
16871 if (TREE_CODE (type) != TYPE_DECL)
16873 cp_parser_error (parser, "expected template-id for type");
16874 type = NULL_TREE;
16877 /* Otherwise, look for a type-name. */
16878 else
16879 type = cp_parser_type_name (parser);
16880 /* Keep track of all name-lookups performed in class scopes. */
16881 if (type
16882 && !global_p
16883 && !qualified_p
16884 && TREE_CODE (type) == TYPE_DECL
16885 && identifier_p (DECL_NAME (type)))
16886 maybe_note_name_used_in_class (DECL_NAME (type), type);
16887 /* If it didn't work out, we don't have a TYPE. */
16888 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16889 && !cp_parser_parse_definitely (parser))
16890 type = NULL_TREE;
16891 if (!type && cxx_dialect >= cxx1z)
16893 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16894 cp_parser_parse_tentatively (parser);
16896 cp_parser_global_scope_opt (parser,
16897 /*current_scope_valid_p=*/false);
16898 cp_parser_nested_name_specifier_opt (parser,
16899 /*typename_keyword_p=*/false,
16900 /*check_dependency_p=*/true,
16901 /*type_p=*/false,
16902 /*is_declaration=*/false);
16903 tree name = cp_parser_identifier (parser);
16904 if (name && TREE_CODE (name) == IDENTIFIER_NODE
16905 && parser->scope != error_mark_node)
16907 tree tmpl = cp_parser_lookup_name (parser, name,
16908 none_type,
16909 /*is_template=*/false,
16910 /*is_namespace=*/false,
16911 /*check_dependency=*/true,
16912 /*ambiguous_decls=*/NULL,
16913 token->location);
16914 if (tmpl && tmpl != error_mark_node
16915 && (DECL_CLASS_TEMPLATE_P (tmpl)
16916 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
16917 type = make_template_placeholder (tmpl);
16918 else
16920 type = error_mark_node;
16921 if (!cp_parser_simulate_error (parser))
16922 cp_parser_name_lookup_error (parser, name, tmpl,
16923 NLE_TYPE, token->location);
16926 else
16927 type = error_mark_node;
16929 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16930 && !cp_parser_parse_definitely (parser))
16931 type = NULL_TREE;
16933 if (type && decl_specs)
16934 cp_parser_set_decl_spec_type (decl_specs, type,
16935 token,
16936 /*type_definition_p=*/false);
16939 /* If we didn't get a type-name, issue an error message. */
16940 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16942 cp_parser_error (parser, "expected type-name");
16943 return error_mark_node;
16946 if (type && type != error_mark_node)
16948 /* See if TYPE is an Objective-C type, and if so, parse and
16949 accept any protocol references following it. Do this before
16950 the cp_parser_check_for_invalid_template_id() call, because
16951 Objective-C types can be followed by '<...>' which would
16952 enclose protocol names rather than template arguments, and so
16953 everything is fine. */
16954 if (c_dialect_objc () && !parser->scope
16955 && (objc_is_id (type) || objc_is_class_name (type)))
16957 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16958 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16960 /* Clobber the "unqualified" type previously entered into
16961 DECL_SPECS with the new, improved protocol-qualified version. */
16962 if (decl_specs)
16963 decl_specs->type = qual_type;
16965 return qual_type;
16968 /* There is no valid C++ program where a non-template type is
16969 followed by a "<". That usually indicates that the user
16970 thought that the type was a template. */
16971 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16972 none_type,
16973 token->location);
16976 return type;
16979 /* Parse a type-name.
16981 type-name:
16982 class-name
16983 enum-name
16984 typedef-name
16985 simple-template-id [in c++0x]
16987 enum-name:
16988 identifier
16990 typedef-name:
16991 identifier
16993 Concepts:
16995 type-name:
16996 concept-name
16997 partial-concept-id
16999 concept-name:
17000 identifier
17002 Returns a TYPE_DECL for the type. */
17004 static tree
17005 cp_parser_type_name (cp_parser* parser)
17007 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17010 /* See above. */
17011 static tree
17012 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17014 tree type_decl;
17016 /* We can't know yet whether it is a class-name or not. */
17017 cp_parser_parse_tentatively (parser);
17018 /* Try a class-name. */
17019 type_decl = cp_parser_class_name (parser,
17020 typename_keyword_p,
17021 /*template_keyword_p=*/false,
17022 none_type,
17023 /*check_dependency_p=*/true,
17024 /*class_head_p=*/false,
17025 /*is_declaration=*/false);
17026 /* If it's not a class-name, keep looking. */
17027 if (!cp_parser_parse_definitely (parser))
17029 if (cxx_dialect < cxx11)
17030 /* It must be a typedef-name or an enum-name. */
17031 return cp_parser_nonclass_name (parser);
17033 cp_parser_parse_tentatively (parser);
17034 /* It is either a simple-template-id representing an
17035 instantiation of an alias template... */
17036 type_decl = cp_parser_template_id (parser,
17037 /*template_keyword_p=*/false,
17038 /*check_dependency_p=*/true,
17039 none_type,
17040 /*is_declaration=*/false);
17041 /* Note that this must be an instantiation of an alias template
17042 because [temp.names]/6 says:
17044 A template-id that names an alias template specialization
17045 is a type-name.
17047 Whereas [temp.names]/7 says:
17049 A simple-template-id that names a class template
17050 specialization is a class-name.
17052 With concepts, this could also be a partial-concept-id that
17053 declares a non-type template parameter. */
17054 if (type_decl != NULL_TREE
17055 && TREE_CODE (type_decl) == TYPE_DECL
17056 && TYPE_DECL_ALIAS_P (type_decl))
17057 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17058 else if (is_constrained_parameter (type_decl))
17059 /* Don't do anything. */ ;
17060 else
17061 cp_parser_simulate_error (parser);
17063 if (!cp_parser_parse_definitely (parser))
17064 /* ... Or a typedef-name or an enum-name. */
17065 return cp_parser_nonclass_name (parser);
17068 return type_decl;
17071 /* Check if DECL and ARGS can form a constrained-type-specifier.
17072 If ARGS is non-null, we try to form a concept check of the
17073 form DECL<?, ARGS> where ? is a wildcard that matches any
17074 kind of template argument. If ARGS is NULL, then we try to
17075 form a concept check of the form DECL<?>. */
17077 static tree
17078 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17079 tree decl, tree args)
17081 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17083 /* If we a constrained-type-specifier cannot be deduced. */
17084 if (parser->prevent_constrained_type_specifiers)
17085 return NULL_TREE;
17087 /* A constrained type specifier can only be found in an
17088 overload set or as a reference to a template declaration.
17090 FIXME: This might be masking a bug. It's possible that
17091 that the deduction below is causing template specializations
17092 to be formed with the wildcard as an argument. */
17093 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17094 return NULL_TREE;
17096 /* Try to build a call expression that evaluates the
17097 concept. This can fail if the overload set refers
17098 only to non-templates. */
17099 tree placeholder = build_nt (WILDCARD_DECL);
17100 tree check = build_concept_check (decl, placeholder, args);
17101 if (check == error_mark_node)
17102 return NULL_TREE;
17104 /* Deduce the checked constraint and the prototype parameter.
17106 FIXME: In certain cases, failure to deduce should be a
17107 diagnosable error. */
17108 tree conc;
17109 tree proto;
17110 if (!deduce_constrained_parameter (check, conc, proto))
17111 return NULL_TREE;
17113 /* In template parameter scope, this results in a constrained
17114 parameter. Return a descriptor of that parm. */
17115 if (processing_template_parmlist)
17116 return build_constrained_parameter (conc, proto, args);
17118 /* In a parameter-declaration-clause, constrained-type
17119 specifiers result in invented template parameters. */
17120 if (parser->auto_is_implicit_function_template_parm_p)
17122 tree x = build_constrained_parameter (conc, proto, args);
17123 return synthesize_implicit_template_parm (parser, x);
17125 else
17127 /* Otherwise, we're in a context where the constrained
17128 type name is deduced and the constraint applies
17129 after deduction. */
17130 return make_constrained_auto (conc, args);
17133 return NULL_TREE;
17136 /* If DECL refers to a concept, return a TYPE_DECL representing
17137 the result of using the constrained type specifier in the
17138 current context. DECL refers to a concept if
17140 - it is an overload set containing a function concept taking a single
17141 type argument, or
17143 - it is a variable concept taking a single type argument. */
17145 static tree
17146 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17148 if (flag_concepts
17149 && (TREE_CODE (decl) == OVERLOAD
17150 || BASELINK_P (decl)
17151 || variable_concept_p (decl)))
17152 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17153 else
17154 return NULL_TREE;
17157 /* Check if DECL and ARGS form a partial-concept-id. If so,
17158 assign ID to the resulting constrained placeholder.
17160 Returns true if the partial-concept-id designates a placeholder
17161 and false otherwise. Note that *id is set to NULL_TREE in
17162 this case. */
17164 static tree
17165 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17167 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17170 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17171 or a concept-name.
17173 enum-name:
17174 identifier
17176 typedef-name:
17177 identifier
17179 concept-name:
17180 identifier
17182 Returns a TYPE_DECL for the type. */
17184 static tree
17185 cp_parser_nonclass_name (cp_parser* parser)
17187 tree type_decl;
17188 tree identifier;
17190 cp_token *token = cp_lexer_peek_token (parser->lexer);
17191 identifier = cp_parser_identifier (parser);
17192 if (identifier == error_mark_node)
17193 return error_mark_node;
17195 /* Look up the type-name. */
17196 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17198 type_decl = strip_using_decl (type_decl);
17200 /* If we found an overload set, then it may refer to a concept-name. */
17201 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17202 type_decl = decl;
17204 if (TREE_CODE (type_decl) != TYPE_DECL
17205 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17207 /* See if this is an Objective-C type. */
17208 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17209 tree type = objc_get_protocol_qualified_type (identifier, protos);
17210 if (type)
17211 type_decl = TYPE_NAME (type);
17214 /* Issue an error if we did not find a type-name. */
17215 if (TREE_CODE (type_decl) != TYPE_DECL
17216 /* In Objective-C, we have the complication that class names are
17217 normally type names and start declarations (eg, the
17218 "NSObject" in "NSObject *object;"), but can be used in an
17219 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17220 is an expression. So, a classname followed by a dot is not a
17221 valid type-name. */
17222 || (objc_is_class_name (TREE_TYPE (type_decl))
17223 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17225 if (!cp_parser_simulate_error (parser))
17226 cp_parser_name_lookup_error (parser, identifier, type_decl,
17227 NLE_TYPE, token->location);
17228 return error_mark_node;
17230 /* Remember that the name was used in the definition of the
17231 current class so that we can check later to see if the
17232 meaning would have been different after the class was
17233 entirely defined. */
17234 else if (type_decl != error_mark_node
17235 && !parser->scope)
17236 maybe_note_name_used_in_class (identifier, type_decl);
17238 return type_decl;
17241 /* Parse an elaborated-type-specifier. Note that the grammar given
17242 here incorporates the resolution to DR68.
17244 elaborated-type-specifier:
17245 class-key :: [opt] nested-name-specifier [opt] identifier
17246 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17247 enum-key :: [opt] nested-name-specifier [opt] identifier
17248 typename :: [opt] nested-name-specifier identifier
17249 typename :: [opt] nested-name-specifier template [opt]
17250 template-id
17252 GNU extension:
17254 elaborated-type-specifier:
17255 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17256 class-key attributes :: [opt] nested-name-specifier [opt]
17257 template [opt] template-id
17258 enum attributes :: [opt] nested-name-specifier [opt] identifier
17260 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17261 declared `friend'. If IS_DECLARATION is TRUE, then this
17262 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17263 something is being declared.
17265 Returns the TYPE specified. */
17267 static tree
17268 cp_parser_elaborated_type_specifier (cp_parser* parser,
17269 bool is_friend,
17270 bool is_declaration)
17272 enum tag_types tag_type;
17273 tree identifier;
17274 tree type = NULL_TREE;
17275 tree attributes = NULL_TREE;
17276 tree globalscope;
17277 cp_token *token = NULL;
17279 /* See if we're looking at the `enum' keyword. */
17280 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17282 /* Consume the `enum' token. */
17283 cp_lexer_consume_token (parser->lexer);
17284 /* Remember that it's an enumeration type. */
17285 tag_type = enum_type;
17286 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17287 enums) is used here. */
17288 cp_token *token = cp_lexer_peek_token (parser->lexer);
17289 if (cp_parser_is_keyword (token, RID_CLASS)
17290 || cp_parser_is_keyword (token, RID_STRUCT))
17292 gcc_rich_location richloc (token->location);
17293 richloc.add_range (input_location, false);
17294 richloc.add_fixit_remove ();
17295 pedwarn_at_rich_loc (&richloc, 0, "elaborated-type-specifier for "
17296 "a scoped enum must not use the %qD keyword",
17297 token->u.value);
17298 /* Consume the `struct' or `class' and parse it anyway. */
17299 cp_lexer_consume_token (parser->lexer);
17301 /* Parse the attributes. */
17302 attributes = cp_parser_attributes_opt (parser);
17304 /* Or, it might be `typename'. */
17305 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17306 RID_TYPENAME))
17308 /* Consume the `typename' token. */
17309 cp_lexer_consume_token (parser->lexer);
17310 /* Remember that it's a `typename' type. */
17311 tag_type = typename_type;
17313 /* Otherwise it must be a class-key. */
17314 else
17316 tag_type = cp_parser_class_key (parser);
17317 if (tag_type == none_type)
17318 return error_mark_node;
17319 /* Parse the attributes. */
17320 attributes = cp_parser_attributes_opt (parser);
17323 /* Look for the `::' operator. */
17324 globalscope = cp_parser_global_scope_opt (parser,
17325 /*current_scope_valid_p=*/false);
17326 /* Look for the nested-name-specifier. */
17327 tree nested_name_specifier;
17328 if (tag_type == typename_type && !globalscope)
17330 nested_name_specifier
17331 = cp_parser_nested_name_specifier (parser,
17332 /*typename_keyword_p=*/true,
17333 /*check_dependency_p=*/true,
17334 /*type_p=*/true,
17335 is_declaration);
17336 if (!nested_name_specifier)
17337 return error_mark_node;
17339 else
17340 /* Even though `typename' is not present, the proposed resolution
17341 to Core Issue 180 says that in `class A<T>::B', `B' should be
17342 considered a type-name, even if `A<T>' is dependent. */
17343 nested_name_specifier
17344 = cp_parser_nested_name_specifier_opt (parser,
17345 /*typename_keyword_p=*/true,
17346 /*check_dependency_p=*/true,
17347 /*type_p=*/true,
17348 is_declaration);
17349 /* For everything but enumeration types, consider a template-id.
17350 For an enumeration type, consider only a plain identifier. */
17351 if (tag_type != enum_type)
17353 bool template_p = false;
17354 tree decl;
17356 /* Allow the `template' keyword. */
17357 template_p = cp_parser_optional_template_keyword (parser);
17358 /* If we didn't see `template', we don't know if there's a
17359 template-id or not. */
17360 if (!template_p)
17361 cp_parser_parse_tentatively (parser);
17362 /* Parse the template-id. */
17363 token = cp_lexer_peek_token (parser->lexer);
17364 decl = cp_parser_template_id (parser, template_p,
17365 /*check_dependency_p=*/true,
17366 tag_type,
17367 is_declaration);
17368 /* If we didn't find a template-id, look for an ordinary
17369 identifier. */
17370 if (!template_p && !cp_parser_parse_definitely (parser))
17372 /* We can get here when cp_parser_template_id, called by
17373 cp_parser_class_name with tag_type == none_type, succeeds
17374 and caches a BASELINK. Then, when called again here,
17375 instead of failing and returning an error_mark_node
17376 returns it (see template/typename17.C in C++11).
17377 ??? Could we diagnose this earlier? */
17378 else if (tag_type == typename_type && BASELINK_P (decl))
17380 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17381 type = error_mark_node;
17383 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17384 in effect, then we must assume that, upon instantiation, the
17385 template will correspond to a class. */
17386 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17387 && tag_type == typename_type)
17388 type = make_typename_type (parser->scope, decl,
17389 typename_type,
17390 /*complain=*/tf_error);
17391 /* If the `typename' keyword is in effect and DECL is not a type
17392 decl, then type is non existent. */
17393 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17395 else if (TREE_CODE (decl) == TYPE_DECL)
17397 type = check_elaborated_type_specifier (tag_type, decl,
17398 /*allow_template_p=*/true);
17400 /* If the next token is a semicolon, this must be a specialization,
17401 instantiation, or friend declaration. Check the scope while we
17402 still know whether or not we had a nested-name-specifier. */
17403 if (type != error_mark_node
17404 && !nested_name_specifier && !is_friend
17405 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17406 check_unqualified_spec_or_inst (type, token->location);
17408 else if (decl == error_mark_node)
17409 type = error_mark_node;
17412 if (!type)
17414 token = cp_lexer_peek_token (parser->lexer);
17415 identifier = cp_parser_identifier (parser);
17417 if (identifier == error_mark_node)
17419 parser->scope = NULL_TREE;
17420 return error_mark_node;
17423 /* For a `typename', we needn't call xref_tag. */
17424 if (tag_type == typename_type
17425 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17426 return cp_parser_make_typename_type (parser, identifier,
17427 token->location);
17429 /* Template parameter lists apply only if we are not within a
17430 function parameter list. */
17431 bool template_parm_lists_apply
17432 = parser->num_template_parameter_lists;
17433 if (template_parm_lists_apply)
17434 for (cp_binding_level *s = current_binding_level;
17435 s && s->kind != sk_template_parms;
17436 s = s->level_chain)
17437 if (s->kind == sk_function_parms)
17438 template_parm_lists_apply = false;
17440 /* Look up a qualified name in the usual way. */
17441 if (parser->scope)
17443 tree decl;
17444 tree ambiguous_decls;
17446 decl = cp_parser_lookup_name (parser, identifier,
17447 tag_type,
17448 /*is_template=*/false,
17449 /*is_namespace=*/false,
17450 /*check_dependency=*/true,
17451 &ambiguous_decls,
17452 token->location);
17454 /* If the lookup was ambiguous, an error will already have been
17455 issued. */
17456 if (ambiguous_decls)
17457 return error_mark_node;
17459 /* If we are parsing friend declaration, DECL may be a
17460 TEMPLATE_DECL tree node here. However, we need to check
17461 whether this TEMPLATE_DECL results in valid code. Consider
17462 the following example:
17464 namespace N {
17465 template <class T> class C {};
17467 class X {
17468 template <class T> friend class N::C; // #1, valid code
17470 template <class T> class Y {
17471 friend class N::C; // #2, invalid code
17474 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17475 name lookup of `N::C'. We see that friend declaration must
17476 be template for the code to be valid. Note that
17477 processing_template_decl does not work here since it is
17478 always 1 for the above two cases. */
17480 decl = (cp_parser_maybe_treat_template_as_class
17481 (decl, /*tag_name_p=*/is_friend
17482 && template_parm_lists_apply));
17484 if (TREE_CODE (decl) != TYPE_DECL)
17486 cp_parser_diagnose_invalid_type_name (parser,
17487 identifier,
17488 token->location);
17489 return error_mark_node;
17492 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17494 bool allow_template = (template_parm_lists_apply
17495 || DECL_SELF_REFERENCE_P (decl));
17496 type = check_elaborated_type_specifier (tag_type, decl,
17497 allow_template);
17499 if (type == error_mark_node)
17500 return error_mark_node;
17503 /* Forward declarations of nested types, such as
17505 class C1::C2;
17506 class C1::C2::C3;
17508 are invalid unless all components preceding the final '::'
17509 are complete. If all enclosing types are complete, these
17510 declarations become merely pointless.
17512 Invalid forward declarations of nested types are errors
17513 caught elsewhere in parsing. Those that are pointless arrive
17514 here. */
17516 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17517 && !is_friend && !processing_explicit_instantiation)
17518 warning (0, "declaration %qD does not declare anything", decl);
17520 type = TREE_TYPE (decl);
17522 else
17524 /* An elaborated-type-specifier sometimes introduces a new type and
17525 sometimes names an existing type. Normally, the rule is that it
17526 introduces a new type only if there is not an existing type of
17527 the same name already in scope. For example, given:
17529 struct S {};
17530 void f() { struct S s; }
17532 the `struct S' in the body of `f' is the same `struct S' as in
17533 the global scope; the existing definition is used. However, if
17534 there were no global declaration, this would introduce a new
17535 local class named `S'.
17537 An exception to this rule applies to the following code:
17539 namespace N { struct S; }
17541 Here, the elaborated-type-specifier names a new type
17542 unconditionally; even if there is already an `S' in the
17543 containing scope this declaration names a new type.
17544 This exception only applies if the elaborated-type-specifier
17545 forms the complete declaration:
17547 [class.name]
17549 A declaration consisting solely of `class-key identifier ;' is
17550 either a redeclaration of the name in the current scope or a
17551 forward declaration of the identifier as a class name. It
17552 introduces the name into the current scope.
17554 We are in this situation precisely when the next token is a `;'.
17556 An exception to the exception is that a `friend' declaration does
17557 *not* name a new type; i.e., given:
17559 struct S { friend struct T; };
17561 `T' is not a new type in the scope of `S'.
17563 Also, `new struct S' or `sizeof (struct S)' never results in the
17564 definition of a new type; a new type can only be declared in a
17565 declaration context. */
17567 tag_scope ts;
17568 bool template_p;
17570 if (is_friend)
17571 /* Friends have special name lookup rules. */
17572 ts = ts_within_enclosing_non_class;
17573 else if (is_declaration
17574 && cp_lexer_next_token_is (parser->lexer,
17575 CPP_SEMICOLON))
17576 /* This is a `class-key identifier ;' */
17577 ts = ts_current;
17578 else
17579 ts = ts_global;
17581 template_p =
17582 (template_parm_lists_apply
17583 && (cp_parser_next_token_starts_class_definition_p (parser)
17584 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17585 /* An unqualified name was used to reference this type, so
17586 there were no qualifying templates. */
17587 if (template_parm_lists_apply
17588 && !cp_parser_check_template_parameters (parser,
17589 /*num_templates=*/0,
17590 token->location,
17591 /*declarator=*/NULL))
17592 return error_mark_node;
17593 type = xref_tag (tag_type, identifier, ts, template_p);
17597 if (type == error_mark_node)
17598 return error_mark_node;
17600 /* Allow attributes on forward declarations of classes. */
17601 if (attributes)
17603 if (TREE_CODE (type) == TYPENAME_TYPE)
17604 warning (OPT_Wattributes,
17605 "attributes ignored on uninstantiated type");
17606 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17607 && ! processing_explicit_instantiation)
17608 warning (OPT_Wattributes,
17609 "attributes ignored on template instantiation");
17610 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17611 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17612 else
17613 warning (OPT_Wattributes,
17614 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17617 if (tag_type != enum_type)
17619 /* Indicate whether this class was declared as a `class' or as a
17620 `struct'. */
17621 if (CLASS_TYPE_P (type))
17622 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17623 cp_parser_check_class_key (tag_type, type);
17626 /* A "<" cannot follow an elaborated type specifier. If that
17627 happens, the user was probably trying to form a template-id. */
17628 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17629 token->location);
17631 return type;
17634 /* Parse an enum-specifier.
17636 enum-specifier:
17637 enum-head { enumerator-list [opt] }
17638 enum-head { enumerator-list , } [C++0x]
17640 enum-head:
17641 enum-key identifier [opt] enum-base [opt]
17642 enum-key nested-name-specifier identifier enum-base [opt]
17644 enum-key:
17645 enum
17646 enum class [C++0x]
17647 enum struct [C++0x]
17649 enum-base: [C++0x]
17650 : type-specifier-seq
17652 opaque-enum-specifier:
17653 enum-key identifier enum-base [opt] ;
17655 GNU Extensions:
17656 enum-key attributes[opt] identifier [opt] enum-base [opt]
17657 { enumerator-list [opt] }attributes[opt]
17658 enum-key attributes[opt] identifier [opt] enum-base [opt]
17659 { enumerator-list, }attributes[opt] [C++0x]
17661 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17662 if the token stream isn't an enum-specifier after all. */
17664 static tree
17665 cp_parser_enum_specifier (cp_parser* parser)
17667 tree identifier;
17668 tree type = NULL_TREE;
17669 tree prev_scope;
17670 tree nested_name_specifier = NULL_TREE;
17671 tree attributes;
17672 bool scoped_enum_p = false;
17673 bool has_underlying_type = false;
17674 bool nested_being_defined = false;
17675 bool new_value_list = false;
17676 bool is_new_type = false;
17677 bool is_unnamed = false;
17678 tree underlying_type = NULL_TREE;
17679 cp_token *type_start_token = NULL;
17680 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17682 parser->colon_corrects_to_scope_p = false;
17684 /* Parse tentatively so that we can back up if we don't find a
17685 enum-specifier. */
17686 cp_parser_parse_tentatively (parser);
17688 /* Caller guarantees that the current token is 'enum', an identifier
17689 possibly follows, and the token after that is an opening brace.
17690 If we don't have an identifier, fabricate an anonymous name for
17691 the enumeration being defined. */
17692 cp_lexer_consume_token (parser->lexer);
17694 /* Parse the "class" or "struct", which indicates a scoped
17695 enumeration type in C++0x. */
17696 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17697 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17699 if (cxx_dialect < cxx11)
17700 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17702 /* Consume the `struct' or `class' token. */
17703 cp_lexer_consume_token (parser->lexer);
17705 scoped_enum_p = true;
17708 attributes = cp_parser_attributes_opt (parser);
17710 /* Clear the qualification. */
17711 parser->scope = NULL_TREE;
17712 parser->qualifying_scope = NULL_TREE;
17713 parser->object_scope = NULL_TREE;
17715 /* Figure out in what scope the declaration is being placed. */
17716 prev_scope = current_scope ();
17718 type_start_token = cp_lexer_peek_token (parser->lexer);
17720 push_deferring_access_checks (dk_no_check);
17721 nested_name_specifier
17722 = cp_parser_nested_name_specifier_opt (parser,
17723 /*typename_keyword_p=*/true,
17724 /*check_dependency_p=*/false,
17725 /*type_p=*/false,
17726 /*is_declaration=*/false);
17728 if (nested_name_specifier)
17730 tree name;
17732 identifier = cp_parser_identifier (parser);
17733 name = cp_parser_lookup_name (parser, identifier,
17734 enum_type,
17735 /*is_template=*/false,
17736 /*is_namespace=*/false,
17737 /*check_dependency=*/true,
17738 /*ambiguous_decls=*/NULL,
17739 input_location);
17740 if (name && name != error_mark_node)
17742 type = TREE_TYPE (name);
17743 if (TREE_CODE (type) == TYPENAME_TYPE)
17745 /* Are template enums allowed in ISO? */
17746 if (template_parm_scope_p ())
17747 pedwarn (type_start_token->location, OPT_Wpedantic,
17748 "%qD is an enumeration template", name);
17749 /* ignore a typename reference, for it will be solved by name
17750 in start_enum. */
17751 type = NULL_TREE;
17754 else if (nested_name_specifier == error_mark_node)
17755 /* We already issued an error. */;
17756 else
17758 error_at (type_start_token->location,
17759 "%qD does not name an enumeration in %qT",
17760 identifier, nested_name_specifier);
17761 nested_name_specifier = error_mark_node;
17764 else
17766 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17767 identifier = cp_parser_identifier (parser);
17768 else
17770 identifier = make_anon_name ();
17771 is_unnamed = true;
17772 if (scoped_enum_p)
17773 error_at (type_start_token->location,
17774 "unnamed scoped enum is not allowed");
17777 pop_deferring_access_checks ();
17779 /* Check for the `:' that denotes a specified underlying type in C++0x.
17780 Note that a ':' could also indicate a bitfield width, however. */
17781 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17783 cp_decl_specifier_seq type_specifiers;
17785 /* Consume the `:'. */
17786 cp_lexer_consume_token (parser->lexer);
17788 /* Parse the type-specifier-seq. */
17789 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17790 /*is_trailing_return=*/false,
17791 &type_specifiers);
17793 /* At this point this is surely not elaborated type specifier. */
17794 if (!cp_parser_parse_definitely (parser))
17795 return NULL_TREE;
17797 if (cxx_dialect < cxx11)
17798 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17800 has_underlying_type = true;
17802 /* If that didn't work, stop. */
17803 if (type_specifiers.type != error_mark_node)
17805 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17806 /*initialized=*/0, NULL);
17807 if (underlying_type == error_mark_node
17808 || check_for_bare_parameter_packs (underlying_type))
17809 underlying_type = NULL_TREE;
17813 /* Look for the `{' but don't consume it yet. */
17814 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17816 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17818 cp_parser_error (parser, "expected %<{%>");
17819 if (has_underlying_type)
17821 type = NULL_TREE;
17822 goto out;
17825 /* An opaque-enum-specifier must have a ';' here. */
17826 if ((scoped_enum_p || underlying_type)
17827 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17829 cp_parser_error (parser, "expected %<;%> or %<{%>");
17830 if (has_underlying_type)
17832 type = NULL_TREE;
17833 goto out;
17838 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17839 return NULL_TREE;
17841 if (nested_name_specifier)
17843 if (CLASS_TYPE_P (nested_name_specifier))
17845 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17846 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17847 push_scope (nested_name_specifier);
17849 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17851 push_nested_namespace (nested_name_specifier);
17855 /* Issue an error message if type-definitions are forbidden here. */
17856 if (!cp_parser_check_type_definition (parser))
17857 type = error_mark_node;
17858 else
17859 /* Create the new type. We do this before consuming the opening
17860 brace so the enum will be recorded as being on the line of its
17861 tag (or the 'enum' keyword, if there is no tag). */
17862 type = start_enum (identifier, type, underlying_type,
17863 attributes, scoped_enum_p, &is_new_type);
17865 /* If the next token is not '{' it is an opaque-enum-specifier or an
17866 elaborated-type-specifier. */
17867 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17869 timevar_push (TV_PARSE_ENUM);
17870 if (nested_name_specifier
17871 && nested_name_specifier != error_mark_node)
17873 /* The following catches invalid code such as:
17874 enum class S<int>::E { A, B, C }; */
17875 if (!processing_specialization
17876 && CLASS_TYPE_P (nested_name_specifier)
17877 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17878 error_at (type_start_token->location, "cannot add an enumerator "
17879 "list to a template instantiation");
17881 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17883 error_at (type_start_token->location,
17884 "%<%T::%E%> has not been declared",
17885 TYPE_CONTEXT (nested_name_specifier),
17886 nested_name_specifier);
17887 type = error_mark_node;
17889 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17890 && !CLASS_TYPE_P (nested_name_specifier))
17892 error_at (type_start_token->location, "nested name specifier "
17893 "%qT for enum declaration does not name a class "
17894 "or namespace", nested_name_specifier);
17895 type = error_mark_node;
17897 /* If that scope does not contain the scope in which the
17898 class was originally declared, the program is invalid. */
17899 else if (prev_scope && !is_ancestor (prev_scope,
17900 nested_name_specifier))
17902 if (at_namespace_scope_p ())
17903 error_at (type_start_token->location,
17904 "declaration of %qD in namespace %qD which does not "
17905 "enclose %qD",
17906 type, prev_scope, nested_name_specifier);
17907 else
17908 error_at (type_start_token->location,
17909 "declaration of %qD in %qD which does not "
17910 "enclose %qD",
17911 type, prev_scope, nested_name_specifier);
17912 type = error_mark_node;
17914 /* If that scope is the scope where the declaration is being placed
17915 the program is invalid. */
17916 else if (CLASS_TYPE_P (nested_name_specifier)
17917 && CLASS_TYPE_P (prev_scope)
17918 && same_type_p (nested_name_specifier, prev_scope))
17920 permerror (type_start_token->location,
17921 "extra qualification not allowed");
17922 nested_name_specifier = NULL_TREE;
17926 if (scoped_enum_p)
17927 begin_scope (sk_scoped_enum, type);
17929 /* Consume the opening brace. */
17930 cp_lexer_consume_token (parser->lexer);
17932 if (type == error_mark_node)
17933 ; /* Nothing to add */
17934 else if (OPAQUE_ENUM_P (type)
17935 || (cxx_dialect > cxx98 && processing_specialization))
17937 new_value_list = true;
17938 SET_OPAQUE_ENUM_P (type, false);
17939 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17941 else
17943 error_at (type_start_token->location,
17944 "multiple definition of %q#T", type);
17945 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17946 "previous definition here");
17947 type = error_mark_node;
17950 if (type == error_mark_node)
17951 cp_parser_skip_to_end_of_block_or_statement (parser);
17952 /* If the next token is not '}', then there are some enumerators. */
17953 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17955 if (is_unnamed && !scoped_enum_p)
17956 pedwarn (type_start_token->location, OPT_Wpedantic,
17957 "ISO C++ forbids empty unnamed enum");
17959 else
17960 cp_parser_enumerator_list (parser, type);
17962 /* Consume the final '}'. */
17963 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17965 if (scoped_enum_p)
17966 finish_scope ();
17967 timevar_pop (TV_PARSE_ENUM);
17969 else
17971 /* If a ';' follows, then it is an opaque-enum-specifier
17972 and additional restrictions apply. */
17973 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17975 if (is_unnamed)
17976 error_at (type_start_token->location,
17977 "opaque-enum-specifier without name");
17978 else if (nested_name_specifier)
17979 error_at (type_start_token->location,
17980 "opaque-enum-specifier must use a simple identifier");
17984 /* Look for trailing attributes to apply to this enumeration, and
17985 apply them if appropriate. */
17986 if (cp_parser_allow_gnu_extensions_p (parser))
17988 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17989 cplus_decl_attributes (&type,
17990 trailing_attr,
17991 (int) ATTR_FLAG_TYPE_IN_PLACE);
17994 /* Finish up the enumeration. */
17995 if (type != error_mark_node)
17997 if (new_value_list)
17998 finish_enum_value_list (type);
17999 if (is_new_type)
18000 finish_enum (type);
18003 if (nested_name_specifier)
18005 if (CLASS_TYPE_P (nested_name_specifier))
18007 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18008 pop_scope (nested_name_specifier);
18010 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18012 pop_nested_namespace (nested_name_specifier);
18015 out:
18016 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18017 return type;
18020 /* Parse an enumerator-list. The enumerators all have the indicated
18021 TYPE.
18023 enumerator-list:
18024 enumerator-definition
18025 enumerator-list , enumerator-definition */
18027 static void
18028 cp_parser_enumerator_list (cp_parser* parser, tree type)
18030 while (true)
18032 /* Parse an enumerator-definition. */
18033 cp_parser_enumerator_definition (parser, type);
18035 /* If the next token is not a ',', we've reached the end of
18036 the list. */
18037 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18038 break;
18039 /* Otherwise, consume the `,' and keep going. */
18040 cp_lexer_consume_token (parser->lexer);
18041 /* If the next token is a `}', there is a trailing comma. */
18042 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18044 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18045 pedwarn (input_location, OPT_Wpedantic,
18046 "comma at end of enumerator list");
18047 break;
18052 /* Parse an enumerator-definition. The enumerator has the indicated
18053 TYPE.
18055 enumerator-definition:
18056 enumerator
18057 enumerator = constant-expression
18059 enumerator:
18060 identifier
18062 GNU Extensions:
18064 enumerator-definition:
18065 enumerator attributes [opt]
18066 enumerator attributes [opt] = constant-expression */
18068 static void
18069 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18071 tree identifier;
18072 tree value;
18073 location_t loc;
18075 /* Save the input location because we are interested in the location
18076 of the identifier and not the location of the explicit value. */
18077 loc = cp_lexer_peek_token (parser->lexer)->location;
18079 /* Look for the identifier. */
18080 identifier = cp_parser_identifier (parser);
18081 if (identifier == error_mark_node)
18082 return;
18084 /* Parse any specified attributes. */
18085 tree attrs = cp_parser_attributes_opt (parser);
18087 /* If the next token is an '=', then there is an explicit value. */
18088 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18090 /* Consume the `=' token. */
18091 cp_lexer_consume_token (parser->lexer);
18092 /* Parse the value. */
18093 value = cp_parser_constant_expression (parser);
18095 else
18096 value = NULL_TREE;
18098 /* If we are processing a template, make sure the initializer of the
18099 enumerator doesn't contain any bare template parameter pack. */
18100 if (check_for_bare_parameter_packs (value))
18101 value = error_mark_node;
18103 /* Create the enumerator. */
18104 build_enumerator (identifier, value, type, attrs, loc);
18107 /* Parse a namespace-name.
18109 namespace-name:
18110 original-namespace-name
18111 namespace-alias
18113 Returns the NAMESPACE_DECL for the namespace. */
18115 static tree
18116 cp_parser_namespace_name (cp_parser* parser)
18118 tree identifier;
18119 tree namespace_decl;
18121 cp_token *token = cp_lexer_peek_token (parser->lexer);
18123 /* Get the name of the namespace. */
18124 identifier = cp_parser_identifier (parser);
18125 if (identifier == error_mark_node)
18126 return error_mark_node;
18128 /* Look up the identifier in the currently active scope. Look only
18129 for namespaces, due to:
18131 [basic.lookup.udir]
18133 When looking up a namespace-name in a using-directive or alias
18134 definition, only namespace names are considered.
18136 And:
18138 [basic.lookup.qual]
18140 During the lookup of a name preceding the :: scope resolution
18141 operator, object, function, and enumerator names are ignored.
18143 (Note that cp_parser_qualifying_entity only calls this
18144 function if the token after the name is the scope resolution
18145 operator.) */
18146 namespace_decl = cp_parser_lookup_name (parser, identifier,
18147 none_type,
18148 /*is_template=*/false,
18149 /*is_namespace=*/true,
18150 /*check_dependency=*/true,
18151 /*ambiguous_decls=*/NULL,
18152 token->location);
18153 /* If it's not a namespace, issue an error. */
18154 if (namespace_decl == error_mark_node
18155 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18157 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18158 error_at (token->location, "%qD is not a namespace-name", identifier);
18159 cp_parser_error (parser, "expected namespace-name");
18160 namespace_decl = error_mark_node;
18163 return namespace_decl;
18166 /* Parse a namespace-definition.
18168 namespace-definition:
18169 named-namespace-definition
18170 unnamed-namespace-definition
18172 named-namespace-definition:
18173 original-namespace-definition
18174 extension-namespace-definition
18176 original-namespace-definition:
18177 namespace identifier { namespace-body }
18179 extension-namespace-definition:
18180 namespace original-namespace-name { namespace-body }
18182 unnamed-namespace-definition:
18183 namespace { namespace-body } */
18185 static void
18186 cp_parser_namespace_definition (cp_parser* parser)
18188 tree identifier;
18189 int nested_definition_count = 0;
18191 cp_ensure_no_omp_declare_simd (parser);
18192 cp_ensure_no_oacc_routine (parser);
18194 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18196 if (is_inline)
18198 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18199 cp_lexer_consume_token (parser->lexer);
18202 /* Look for the `namespace' keyword. */
18203 cp_token* token
18204 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18206 /* Parse any specified attributes before the identifier. */
18207 tree attribs = cp_parser_attributes_opt (parser);
18209 for (;;)
18211 identifier = NULL_TREE;
18213 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18215 identifier = cp_parser_identifier (parser);
18217 /* Parse any attributes specified after the identifier. */
18218 attribs = chainon (attribs, cp_parser_attributes_opt (parser));
18221 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18222 break;
18224 if (!nested_definition_count && cxx_dialect < cxx1z)
18225 pedwarn (input_location, OPT_Wpedantic,
18226 "nested namespace definitions only available with "
18227 "-std=c++1z or -std=gnu++1z");
18229 /* Nested namespace names can create new namespaces (unlike
18230 other qualified-ids). */
18231 if (int count = identifier ? push_namespace (identifier) : 0)
18232 nested_definition_count += count;
18233 else
18234 cp_parser_error (parser, "nested namespace name required");
18235 cp_lexer_consume_token (parser->lexer);
18238 if (nested_definition_count && !identifier)
18239 cp_parser_error (parser, "namespace name required");
18241 if (nested_definition_count && attribs)
18242 error_at (token->location,
18243 "a nested namespace definition cannot have attributes");
18244 if (nested_definition_count && is_inline)
18245 error_at (token->location,
18246 "a nested namespace definition cannot be inline");
18248 /* Start the namespace. */
18249 nested_definition_count += push_namespace (identifier, is_inline);
18251 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18253 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18255 /* Look for the `{' to validate starting the namespace. */
18256 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
18258 /* Parse the body of the namespace. */
18259 cp_parser_namespace_body (parser);
18261 /* Look for the final `}'. */
18262 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18264 if (has_visibility)
18265 pop_visibility (1);
18267 /* Pop the nested namespace definitions. */
18268 while (nested_definition_count--)
18269 pop_namespace ();
18272 /* Parse a namespace-body.
18274 namespace-body:
18275 declaration-seq [opt] */
18277 static void
18278 cp_parser_namespace_body (cp_parser* parser)
18280 cp_parser_declaration_seq_opt (parser);
18283 /* Parse a namespace-alias-definition.
18285 namespace-alias-definition:
18286 namespace identifier = qualified-namespace-specifier ; */
18288 static void
18289 cp_parser_namespace_alias_definition (cp_parser* parser)
18291 tree identifier;
18292 tree namespace_specifier;
18294 cp_token *token = cp_lexer_peek_token (parser->lexer);
18296 /* Look for the `namespace' keyword. */
18297 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18298 /* Look for the identifier. */
18299 identifier = cp_parser_identifier (parser);
18300 if (identifier == error_mark_node)
18301 return;
18302 /* Look for the `=' token. */
18303 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18304 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18306 error_at (token->location, "%<namespace%> definition is not allowed here");
18307 /* Skip the definition. */
18308 cp_lexer_consume_token (parser->lexer);
18309 if (cp_parser_skip_to_closing_brace (parser))
18310 cp_lexer_consume_token (parser->lexer);
18311 return;
18313 cp_parser_require (parser, CPP_EQ, RT_EQ);
18314 /* Look for the qualified-namespace-specifier. */
18315 namespace_specifier
18316 = cp_parser_qualified_namespace_specifier (parser);
18317 /* Look for the `;' token. */
18318 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18320 /* Register the alias in the symbol table. */
18321 do_namespace_alias (identifier, namespace_specifier);
18324 /* Parse a qualified-namespace-specifier.
18326 qualified-namespace-specifier:
18327 :: [opt] nested-name-specifier [opt] namespace-name
18329 Returns a NAMESPACE_DECL corresponding to the specified
18330 namespace. */
18332 static tree
18333 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18335 /* Look for the optional `::'. */
18336 cp_parser_global_scope_opt (parser,
18337 /*current_scope_valid_p=*/false);
18339 /* Look for the optional nested-name-specifier. */
18340 cp_parser_nested_name_specifier_opt (parser,
18341 /*typename_keyword_p=*/false,
18342 /*check_dependency_p=*/true,
18343 /*type_p=*/false,
18344 /*is_declaration=*/true);
18346 return cp_parser_namespace_name (parser);
18349 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18350 access declaration.
18352 using-declaration:
18353 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18354 using :: unqualified-id ;
18356 access-declaration:
18357 qualified-id ;
18361 static bool
18362 cp_parser_using_declaration (cp_parser* parser,
18363 bool access_declaration_p)
18365 cp_token *token;
18366 bool typename_p = false;
18367 bool global_scope_p;
18368 tree decl;
18369 tree identifier;
18370 tree qscope;
18371 int oldcount = errorcount;
18372 cp_token *diag_token = NULL;
18374 if (access_declaration_p)
18376 diag_token = cp_lexer_peek_token (parser->lexer);
18377 cp_parser_parse_tentatively (parser);
18379 else
18381 /* Look for the `using' keyword. */
18382 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18384 again:
18385 /* Peek at the next token. */
18386 token = cp_lexer_peek_token (parser->lexer);
18387 /* See if it's `typename'. */
18388 if (token->keyword == RID_TYPENAME)
18390 /* Remember that we've seen it. */
18391 typename_p = true;
18392 /* Consume the `typename' token. */
18393 cp_lexer_consume_token (parser->lexer);
18397 /* Look for the optional global scope qualification. */
18398 global_scope_p
18399 = (cp_parser_global_scope_opt (parser,
18400 /*current_scope_valid_p=*/false)
18401 != NULL_TREE);
18403 /* If we saw `typename', or didn't see `::', then there must be a
18404 nested-name-specifier present. */
18405 if (typename_p || !global_scope_p)
18407 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18408 /*check_dependency_p=*/true,
18409 /*type_p=*/false,
18410 /*is_declaration=*/true);
18411 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18413 cp_parser_skip_to_end_of_block_or_statement (parser);
18414 return false;
18417 /* Otherwise, we could be in either of the two productions. In that
18418 case, treat the nested-name-specifier as optional. */
18419 else
18420 qscope = cp_parser_nested_name_specifier_opt (parser,
18421 /*typename_keyword_p=*/false,
18422 /*check_dependency_p=*/true,
18423 /*type_p=*/false,
18424 /*is_declaration=*/true);
18425 if (!qscope)
18426 qscope = global_namespace;
18427 else if (UNSCOPED_ENUM_P (qscope))
18428 qscope = CP_TYPE_CONTEXT (qscope);
18430 if (access_declaration_p && cp_parser_error_occurred (parser))
18431 /* Something has already gone wrong; there's no need to parse
18432 further. Since an error has occurred, the return value of
18433 cp_parser_parse_definitely will be false, as required. */
18434 return cp_parser_parse_definitely (parser);
18436 token = cp_lexer_peek_token (parser->lexer);
18437 /* Parse the unqualified-id. */
18438 identifier = cp_parser_unqualified_id (parser,
18439 /*template_keyword_p=*/false,
18440 /*check_dependency_p=*/true,
18441 /*declarator_p=*/true,
18442 /*optional_p=*/false);
18444 if (access_declaration_p)
18446 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18447 cp_parser_simulate_error (parser);
18448 if (!cp_parser_parse_definitely (parser))
18449 return false;
18451 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18453 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18454 if (cxx_dialect < cxx1z
18455 && !in_system_header_at (ell->location))
18456 pedwarn (ell->location, 0,
18457 "pack expansion in using-declaration only available "
18458 "with -std=c++1z or -std=gnu++1z");
18459 qscope = make_pack_expansion (qscope);
18462 /* The function we call to handle a using-declaration is different
18463 depending on what scope we are in. */
18464 if (qscope == error_mark_node || identifier == error_mark_node)
18466 else if (!identifier_p (identifier)
18467 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18468 /* [namespace.udecl]
18470 A using declaration shall not name a template-id. */
18471 error_at (token->location,
18472 "a template-id may not appear in a using-declaration");
18473 else
18475 if (at_class_scope_p ())
18477 /* Create the USING_DECL. */
18478 decl = do_class_using_decl (qscope, identifier);
18480 if (decl && typename_p)
18481 USING_DECL_TYPENAME_P (decl) = 1;
18483 if (check_for_bare_parameter_packs (decl))
18485 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18486 return false;
18488 else
18489 /* Add it to the list of members in this class. */
18490 finish_member_declaration (decl);
18492 else
18494 decl = cp_parser_lookup_name_simple (parser,
18495 identifier,
18496 token->location);
18497 if (decl == error_mark_node)
18498 cp_parser_name_lookup_error (parser, identifier,
18499 decl, NLE_NULL,
18500 token->location);
18501 else if (check_for_bare_parameter_packs (decl))
18503 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18504 return false;
18506 else if (!at_namespace_scope_p ())
18507 finish_local_using_decl (decl, qscope, identifier);
18508 else
18509 finish_namespace_using_decl (decl, qscope, identifier);
18513 if (!access_declaration_p
18514 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18516 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18517 if (cxx_dialect < cxx1z)
18518 pedwarn (comma->location, 0,
18519 "comma-separated list in using-declaration only available "
18520 "with -std=c++1z or -std=gnu++1z");
18521 goto again;
18524 /* Look for the final `;'. */
18525 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18527 if (access_declaration_p && errorcount == oldcount)
18528 warning_at (diag_token->location, OPT_Wdeprecated,
18529 "access declarations are deprecated "
18530 "in favour of using-declarations; "
18531 "suggestion: add the %<using%> keyword");
18533 return true;
18536 /* Parse an alias-declaration.
18538 alias-declaration:
18539 using identifier attribute-specifier-seq [opt] = type-id */
18541 static tree
18542 cp_parser_alias_declaration (cp_parser* parser)
18544 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18545 location_t id_location;
18546 cp_declarator *declarator;
18547 cp_decl_specifier_seq decl_specs;
18548 bool member_p;
18549 const char *saved_message = NULL;
18551 /* Look for the `using' keyword. */
18552 cp_token *using_token
18553 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18554 if (using_token == NULL)
18555 return error_mark_node;
18557 id_location = cp_lexer_peek_token (parser->lexer)->location;
18558 id = cp_parser_identifier (parser);
18559 if (id == error_mark_node)
18560 return error_mark_node;
18562 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18563 attributes = cp_parser_attributes_opt (parser);
18564 if (attributes == error_mark_node)
18565 return error_mark_node;
18567 cp_parser_require (parser, CPP_EQ, RT_EQ);
18569 if (cp_parser_error_occurred (parser))
18570 return error_mark_node;
18572 cp_parser_commit_to_tentative_parse (parser);
18574 /* Now we are going to parse the type-id of the declaration. */
18577 [dcl.type]/3 says:
18579 "A type-specifier-seq shall not define a class or enumeration
18580 unless it appears in the type-id of an alias-declaration (7.1.3) that
18581 is not the declaration of a template-declaration."
18583 In other words, if we currently are in an alias template, the
18584 type-id should not define a type.
18586 So let's set parser->type_definition_forbidden_message in that
18587 case; cp_parser_check_type_definition (called by
18588 cp_parser_class_specifier) will then emit an error if a type is
18589 defined in the type-id. */
18590 if (parser->num_template_parameter_lists)
18592 saved_message = parser->type_definition_forbidden_message;
18593 parser->type_definition_forbidden_message =
18594 G_("types may not be defined in alias template declarations");
18597 type = cp_parser_type_id (parser);
18599 /* Restore the error message if need be. */
18600 if (parser->num_template_parameter_lists)
18601 parser->type_definition_forbidden_message = saved_message;
18603 if (type == error_mark_node
18604 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18606 cp_parser_skip_to_end_of_block_or_statement (parser);
18607 return error_mark_node;
18610 /* A typedef-name can also be introduced by an alias-declaration. The
18611 identifier following the using keyword becomes a typedef-name. It has
18612 the same semantics as if it were introduced by the typedef
18613 specifier. In particular, it does not define a new type and it shall
18614 not appear in the type-id. */
18616 clear_decl_specs (&decl_specs);
18617 decl_specs.type = type;
18618 if (attributes != NULL_TREE)
18620 decl_specs.attributes = attributes;
18621 set_and_check_decl_spec_loc (&decl_specs,
18622 ds_attribute,
18623 attrs_token);
18625 set_and_check_decl_spec_loc (&decl_specs,
18626 ds_typedef,
18627 using_token);
18628 set_and_check_decl_spec_loc (&decl_specs,
18629 ds_alias,
18630 using_token);
18632 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18633 declarator->id_loc = id_location;
18635 member_p = at_class_scope_p ();
18636 if (member_p)
18637 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18638 NULL_TREE, attributes);
18639 else
18640 decl = start_decl (declarator, &decl_specs, 0,
18641 attributes, NULL_TREE, &pushed_scope);
18642 if (decl == error_mark_node)
18643 return decl;
18645 // Attach constraints to the alias declaration.
18646 if (flag_concepts && current_template_parms)
18648 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18649 tree constr = build_constraints (reqs, NULL_TREE);
18650 set_constraints (decl, constr);
18653 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18655 if (pushed_scope)
18656 pop_scope (pushed_scope);
18658 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18659 added into the symbol table; otherwise, return the TYPE_DECL. */
18660 if (DECL_LANG_SPECIFIC (decl)
18661 && DECL_TEMPLATE_INFO (decl)
18662 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18664 decl = DECL_TI_TEMPLATE (decl);
18665 if (member_p)
18666 check_member_template (decl);
18669 return decl;
18672 /* Parse a using-directive.
18674 using-directive:
18675 using namespace :: [opt] nested-name-specifier [opt]
18676 namespace-name ; */
18678 static void
18679 cp_parser_using_directive (cp_parser* parser)
18681 tree namespace_decl;
18682 tree attribs;
18684 /* Look for the `using' keyword. */
18685 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18686 /* And the `namespace' keyword. */
18687 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18688 /* Look for the optional `::' operator. */
18689 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18690 /* And the optional nested-name-specifier. */
18691 cp_parser_nested_name_specifier_opt (parser,
18692 /*typename_keyword_p=*/false,
18693 /*check_dependency_p=*/true,
18694 /*type_p=*/false,
18695 /*is_declaration=*/true);
18696 /* Get the namespace being used. */
18697 namespace_decl = cp_parser_namespace_name (parser);
18698 /* And any specified attributes. */
18699 attribs = cp_parser_attributes_opt (parser);
18701 /* Update the symbol table. */
18702 if (namespace_bindings_p ())
18703 finish_namespace_using_directive (namespace_decl, attribs);
18704 else
18705 finish_local_using_directive (namespace_decl, attribs);
18707 /* Look for the final `;'. */
18708 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18711 /* Parse an asm-definition.
18713 asm-definition:
18714 asm ( string-literal ) ;
18716 GNU Extension:
18718 asm-definition:
18719 asm volatile [opt] ( string-literal ) ;
18720 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18721 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18722 : asm-operand-list [opt] ) ;
18723 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18724 : asm-operand-list [opt]
18725 : asm-clobber-list [opt] ) ;
18726 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18727 : asm-clobber-list [opt]
18728 : asm-goto-list ) ; */
18730 static void
18731 cp_parser_asm_definition (cp_parser* parser)
18733 tree string;
18734 tree outputs = NULL_TREE;
18735 tree inputs = NULL_TREE;
18736 tree clobbers = NULL_TREE;
18737 tree labels = NULL_TREE;
18738 tree asm_stmt;
18739 bool volatile_p = false;
18740 bool extended_p = false;
18741 bool invalid_inputs_p = false;
18742 bool invalid_outputs_p = false;
18743 bool goto_p = false;
18744 required_token missing = RT_NONE;
18746 /* Look for the `asm' keyword. */
18747 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18749 if (parser->in_function_body
18750 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18752 error ("%<asm%> in %<constexpr%> function");
18753 cp_function_chain->invalid_constexpr = true;
18756 /* See if the next token is `volatile'. */
18757 if (cp_parser_allow_gnu_extensions_p (parser)
18758 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18760 /* Remember that we saw the `volatile' keyword. */
18761 volatile_p = true;
18762 /* Consume the token. */
18763 cp_lexer_consume_token (parser->lexer);
18765 if (cp_parser_allow_gnu_extensions_p (parser)
18766 && parser->in_function_body
18767 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18769 /* Remember that we saw the `goto' keyword. */
18770 goto_p = true;
18771 /* Consume the token. */
18772 cp_lexer_consume_token (parser->lexer);
18774 /* Look for the opening `('. */
18775 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18776 return;
18777 /* Look for the string. */
18778 string = cp_parser_string_literal (parser, false, false);
18779 if (string == error_mark_node)
18781 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18782 /*consume_paren=*/true);
18783 return;
18786 /* If we're allowing GNU extensions, check for the extended assembly
18787 syntax. Unfortunately, the `:' tokens need not be separated by
18788 a space in C, and so, for compatibility, we tolerate that here
18789 too. Doing that means that we have to treat the `::' operator as
18790 two `:' tokens. */
18791 if (cp_parser_allow_gnu_extensions_p (parser)
18792 && parser->in_function_body
18793 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18794 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18796 bool inputs_p = false;
18797 bool clobbers_p = false;
18798 bool labels_p = false;
18800 /* The extended syntax was used. */
18801 extended_p = true;
18803 /* Look for outputs. */
18804 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18806 /* Consume the `:'. */
18807 cp_lexer_consume_token (parser->lexer);
18808 /* Parse the output-operands. */
18809 if (cp_lexer_next_token_is_not (parser->lexer,
18810 CPP_COLON)
18811 && cp_lexer_next_token_is_not (parser->lexer,
18812 CPP_SCOPE)
18813 && cp_lexer_next_token_is_not (parser->lexer,
18814 CPP_CLOSE_PAREN)
18815 && !goto_p)
18817 outputs = cp_parser_asm_operand_list (parser);
18818 if (outputs == error_mark_node)
18819 invalid_outputs_p = true;
18822 /* If the next token is `::', there are no outputs, and the
18823 next token is the beginning of the inputs. */
18824 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18825 /* The inputs are coming next. */
18826 inputs_p = true;
18828 /* Look for inputs. */
18829 if (inputs_p
18830 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18832 /* Consume the `:' or `::'. */
18833 cp_lexer_consume_token (parser->lexer);
18834 /* Parse the output-operands. */
18835 if (cp_lexer_next_token_is_not (parser->lexer,
18836 CPP_COLON)
18837 && cp_lexer_next_token_is_not (parser->lexer,
18838 CPP_SCOPE)
18839 && cp_lexer_next_token_is_not (parser->lexer,
18840 CPP_CLOSE_PAREN))
18842 inputs = cp_parser_asm_operand_list (parser);
18843 if (inputs == error_mark_node)
18844 invalid_inputs_p = true;
18847 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18848 /* The clobbers are coming next. */
18849 clobbers_p = true;
18851 /* Look for clobbers. */
18852 if (clobbers_p
18853 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18855 clobbers_p = true;
18856 /* Consume the `:' or `::'. */
18857 cp_lexer_consume_token (parser->lexer);
18858 /* Parse the clobbers. */
18859 if (cp_lexer_next_token_is_not (parser->lexer,
18860 CPP_COLON)
18861 && cp_lexer_next_token_is_not (parser->lexer,
18862 CPP_CLOSE_PAREN))
18863 clobbers = cp_parser_asm_clobber_list (parser);
18865 else if (goto_p
18866 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18867 /* The labels are coming next. */
18868 labels_p = true;
18870 /* Look for labels. */
18871 if (labels_p
18872 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18874 labels_p = true;
18875 /* Consume the `:' or `::'. */
18876 cp_lexer_consume_token (parser->lexer);
18877 /* Parse the labels. */
18878 labels = cp_parser_asm_label_list (parser);
18881 if (goto_p && !labels_p)
18882 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18884 else if (goto_p)
18885 missing = RT_COLON_SCOPE;
18887 /* Look for the closing `)'. */
18888 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18889 missing ? missing : RT_CLOSE_PAREN))
18890 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18891 /*consume_paren=*/true);
18892 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18894 if (!invalid_inputs_p && !invalid_outputs_p)
18896 /* Create the ASM_EXPR. */
18897 if (parser->in_function_body)
18899 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18900 inputs, clobbers, labels);
18901 /* If the extended syntax was not used, mark the ASM_EXPR. */
18902 if (!extended_p)
18904 tree temp = asm_stmt;
18905 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18906 temp = TREE_OPERAND (temp, 0);
18908 ASM_INPUT_P (temp) = 1;
18911 else
18912 symtab->finalize_toplevel_asm (string);
18916 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18917 type that comes from the decl-specifier-seq. */
18919 static tree
18920 strip_declarator_types (tree type, cp_declarator *declarator)
18922 for (cp_declarator *d = declarator; d;)
18923 switch (d->kind)
18925 case cdk_id:
18926 case cdk_decomp:
18927 case cdk_error:
18928 d = NULL;
18929 break;
18931 default:
18932 if (TYPE_PTRMEMFUNC_P (type))
18933 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18934 type = TREE_TYPE (type);
18935 d = d->declarator;
18936 break;
18939 return type;
18942 /* Declarators [gram.dcl.decl] */
18944 /* Parse an init-declarator.
18946 init-declarator:
18947 declarator initializer [opt]
18949 GNU Extension:
18951 init-declarator:
18952 declarator asm-specification [opt] attributes [opt] initializer [opt]
18954 function-definition:
18955 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18956 function-body
18957 decl-specifier-seq [opt] declarator function-try-block
18959 GNU Extension:
18961 function-definition:
18962 __extension__ function-definition
18964 TM Extension:
18966 function-definition:
18967 decl-specifier-seq [opt] declarator function-transaction-block
18969 The DECL_SPECIFIERS apply to this declarator. Returns a
18970 representation of the entity declared. If MEMBER_P is TRUE, then
18971 this declarator appears in a class scope. The new DECL created by
18972 this declarator is returned.
18974 The CHECKS are access checks that should be performed once we know
18975 what entity is being declared (and, therefore, what classes have
18976 befriended it).
18978 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18979 for a function-definition here as well. If the declarator is a
18980 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18981 be TRUE upon return. By that point, the function-definition will
18982 have been completely parsed.
18984 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18985 is FALSE.
18987 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18988 parsed declaration if it is an uninitialized single declarator not followed
18989 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18990 if present, will not be consumed. If returned, this declarator will be
18991 created with SD_INITIALIZED but will not call cp_finish_decl.
18993 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18994 and there is an initializer, the pointed location_t is set to the
18995 location of the '=' or `(', or '{' in C++11 token introducing the
18996 initializer. */
18998 static tree
18999 cp_parser_init_declarator (cp_parser* parser,
19000 cp_decl_specifier_seq *decl_specifiers,
19001 vec<deferred_access_check, va_gc> *checks,
19002 bool function_definition_allowed_p,
19003 bool member_p,
19004 int declares_class_or_enum,
19005 bool* function_definition_p,
19006 tree* maybe_range_for_decl,
19007 location_t* init_loc,
19008 tree* auto_result)
19010 cp_token *token = NULL, *asm_spec_start_token = NULL,
19011 *attributes_start_token = NULL;
19012 cp_declarator *declarator;
19013 tree prefix_attributes;
19014 tree attributes = NULL;
19015 tree asm_specification;
19016 tree initializer;
19017 tree decl = NULL_TREE;
19018 tree scope;
19019 int is_initialized;
19020 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19021 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19022 "(...)". */
19023 enum cpp_ttype initialization_kind;
19024 bool is_direct_init = false;
19025 bool is_non_constant_init;
19026 int ctor_dtor_or_conv_p;
19027 bool friend_p = cp_parser_friend_p (decl_specifiers);
19028 tree pushed_scope = NULL_TREE;
19029 bool range_for_decl_p = false;
19030 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19031 location_t tmp_init_loc = UNKNOWN_LOCATION;
19033 /* Gather the attributes that were provided with the
19034 decl-specifiers. */
19035 prefix_attributes = decl_specifiers->attributes;
19037 /* Assume that this is not the declarator for a function
19038 definition. */
19039 if (function_definition_p)
19040 *function_definition_p = false;
19042 /* Default arguments are only permitted for function parameters. */
19043 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19044 parser->default_arg_ok_p = false;
19046 /* Defer access checks while parsing the declarator; we cannot know
19047 what names are accessible until we know what is being
19048 declared. */
19049 resume_deferring_access_checks ();
19051 token = cp_lexer_peek_token (parser->lexer);
19053 /* Parse the declarator. */
19054 declarator
19055 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19056 &ctor_dtor_or_conv_p,
19057 /*parenthesized_p=*/NULL,
19058 member_p, friend_p);
19059 /* Gather up the deferred checks. */
19060 stop_deferring_access_checks ();
19062 parser->default_arg_ok_p = saved_default_arg_ok_p;
19064 /* If the DECLARATOR was erroneous, there's no need to go
19065 further. */
19066 if (declarator == cp_error_declarator)
19067 return error_mark_node;
19069 /* Check that the number of template-parameter-lists is OK. */
19070 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19071 token->location))
19072 return error_mark_node;
19074 if (declares_class_or_enum & 2)
19075 cp_parser_check_for_definition_in_return_type (declarator,
19076 decl_specifiers->type,
19077 decl_specifiers->locations[ds_type_spec]);
19079 /* Figure out what scope the entity declared by the DECLARATOR is
19080 located in. `grokdeclarator' sometimes changes the scope, so
19081 we compute it now. */
19082 scope = get_scope_of_declarator (declarator);
19084 /* Perform any lookups in the declared type which were thought to be
19085 dependent, but are not in the scope of the declarator. */
19086 decl_specifiers->type
19087 = maybe_update_decl_type (decl_specifiers->type, scope);
19089 /* If we're allowing GNU extensions, look for an
19090 asm-specification. */
19091 if (cp_parser_allow_gnu_extensions_p (parser))
19093 /* Look for an asm-specification. */
19094 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19095 asm_specification = cp_parser_asm_specification_opt (parser);
19097 else
19098 asm_specification = NULL_TREE;
19100 /* Look for attributes. */
19101 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19102 attributes = cp_parser_attributes_opt (parser);
19104 /* Peek at the next token. */
19105 token = cp_lexer_peek_token (parser->lexer);
19107 bool bogus_implicit_tmpl = false;
19109 if (function_declarator_p (declarator))
19111 /* Handle C++17 deduction guides. */
19112 if (!decl_specifiers->type
19113 && ctor_dtor_or_conv_p <= 0
19114 && cxx_dialect >= cxx1z)
19116 cp_declarator *id = get_id_declarator (declarator);
19117 tree name = id->u.id.unqualified_name;
19118 parser->scope = id->u.id.qualifying_scope;
19119 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19120 if (tmpl
19121 && (DECL_CLASS_TEMPLATE_P (tmpl)
19122 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19124 id->u.id.unqualified_name = dguide_name (tmpl);
19125 id->u.id.sfk = sfk_deduction_guide;
19126 ctor_dtor_or_conv_p = 1;
19130 /* Check to see if the token indicates the start of a
19131 function-definition. */
19132 if (cp_parser_token_starts_function_definition_p (token))
19134 if (!function_definition_allowed_p)
19136 /* If a function-definition should not appear here, issue an
19137 error message. */
19138 cp_parser_error (parser,
19139 "a function-definition is not allowed here");
19140 return error_mark_node;
19143 location_t func_brace_location
19144 = cp_lexer_peek_token (parser->lexer)->location;
19146 /* Neither attributes nor an asm-specification are allowed
19147 on a function-definition. */
19148 if (asm_specification)
19149 error_at (asm_spec_start_token->location,
19150 "an asm-specification is not allowed "
19151 "on a function-definition");
19152 if (attributes)
19153 error_at (attributes_start_token->location,
19154 "attributes are not allowed "
19155 "on a function-definition");
19156 /* This is a function-definition. */
19157 *function_definition_p = true;
19159 /* Parse the function definition. */
19160 if (member_p)
19161 decl = cp_parser_save_member_function_body (parser,
19162 decl_specifiers,
19163 declarator,
19164 prefix_attributes);
19165 else
19166 decl =
19167 (cp_parser_function_definition_from_specifiers_and_declarator
19168 (parser, decl_specifiers, prefix_attributes, declarator));
19170 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19172 /* This is where the prologue starts... */
19173 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19174 = func_brace_location;
19177 return decl;
19180 else if (parser->fully_implicit_function_template_p)
19182 /* A non-template declaration involving a function parameter list
19183 containing an implicit template parameter will be made into a
19184 template. If the resulting declaration is not going to be an
19185 actual function then finish the template scope here to prevent it.
19186 An error message will be issued once we have a decl to talk about.
19188 FIXME probably we should do type deduction rather than create an
19189 implicit template, but the standard currently doesn't allow it. */
19190 bogus_implicit_tmpl = true;
19191 finish_fully_implicit_template (parser, NULL_TREE);
19194 /* [dcl.dcl]
19196 Only in function declarations for constructors, destructors, type
19197 conversions, and deduction guides can the decl-specifier-seq be omitted.
19199 We explicitly postpone this check past the point where we handle
19200 function-definitions because we tolerate function-definitions
19201 that are missing their return types in some modes. */
19202 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19204 cp_parser_error (parser,
19205 "expected constructor, destructor, or type conversion");
19206 return error_mark_node;
19209 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19210 if (token->type == CPP_EQ
19211 || token->type == CPP_OPEN_PAREN
19212 || token->type == CPP_OPEN_BRACE)
19214 is_initialized = SD_INITIALIZED;
19215 initialization_kind = token->type;
19216 if (maybe_range_for_decl)
19217 *maybe_range_for_decl = error_mark_node;
19218 tmp_init_loc = token->location;
19219 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19220 *init_loc = tmp_init_loc;
19222 if (token->type == CPP_EQ
19223 && function_declarator_p (declarator))
19225 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19226 if (t2->keyword == RID_DEFAULT)
19227 is_initialized = SD_DEFAULTED;
19228 else if (t2->keyword == RID_DELETE)
19229 is_initialized = SD_DELETED;
19232 else
19234 /* If the init-declarator isn't initialized and isn't followed by a
19235 `,' or `;', it's not a valid init-declarator. */
19236 if (token->type != CPP_COMMA
19237 && token->type != CPP_SEMICOLON)
19239 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19240 range_for_decl_p = true;
19241 else
19243 if (!maybe_range_for_decl)
19244 cp_parser_error (parser, "expected initializer");
19245 return error_mark_node;
19248 is_initialized = SD_UNINITIALIZED;
19249 initialization_kind = CPP_EOF;
19252 /* Because start_decl has side-effects, we should only call it if we
19253 know we're going ahead. By this point, we know that we cannot
19254 possibly be looking at any other construct. */
19255 cp_parser_commit_to_tentative_parse (parser);
19257 /* Enter the newly declared entry in the symbol table. If we're
19258 processing a declaration in a class-specifier, we wait until
19259 after processing the initializer. */
19260 if (!member_p)
19262 if (parser->in_unbraced_linkage_specification_p)
19263 decl_specifiers->storage_class = sc_extern;
19264 decl = start_decl (declarator, decl_specifiers,
19265 range_for_decl_p? SD_INITIALIZED : is_initialized,
19266 attributes, prefix_attributes, &pushed_scope);
19267 cp_finalize_omp_declare_simd (parser, decl);
19268 cp_finalize_oacc_routine (parser, decl, false);
19269 /* Adjust location of decl if declarator->id_loc is more appropriate:
19270 set, and decl wasn't merged with another decl, in which case its
19271 location would be different from input_location, and more accurate. */
19272 if (DECL_P (decl)
19273 && declarator->id_loc != UNKNOWN_LOCATION
19274 && DECL_SOURCE_LOCATION (decl) == input_location)
19275 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19277 else if (scope)
19278 /* Enter the SCOPE. That way unqualified names appearing in the
19279 initializer will be looked up in SCOPE. */
19280 pushed_scope = push_scope (scope);
19282 /* Perform deferred access control checks, now that we know in which
19283 SCOPE the declared entity resides. */
19284 if (!member_p && decl)
19286 tree saved_current_function_decl = NULL_TREE;
19288 /* If the entity being declared is a function, pretend that we
19289 are in its scope. If it is a `friend', it may have access to
19290 things that would not otherwise be accessible. */
19291 if (TREE_CODE (decl) == FUNCTION_DECL)
19293 saved_current_function_decl = current_function_decl;
19294 current_function_decl = decl;
19297 /* Perform access checks for template parameters. */
19298 cp_parser_perform_template_parameter_access_checks (checks);
19300 /* Perform the access control checks for the declarator and the
19301 decl-specifiers. */
19302 perform_deferred_access_checks (tf_warning_or_error);
19304 /* Restore the saved value. */
19305 if (TREE_CODE (decl) == FUNCTION_DECL)
19306 current_function_decl = saved_current_function_decl;
19309 /* Parse the initializer. */
19310 initializer = NULL_TREE;
19311 is_direct_init = false;
19312 is_non_constant_init = true;
19313 if (is_initialized)
19315 if (function_declarator_p (declarator))
19317 if (initialization_kind == CPP_EQ)
19318 initializer = cp_parser_pure_specifier (parser);
19319 else
19321 /* If the declaration was erroneous, we don't really
19322 know what the user intended, so just silently
19323 consume the initializer. */
19324 if (decl != error_mark_node)
19325 error_at (tmp_init_loc, "initializer provided for function");
19326 cp_parser_skip_to_closing_parenthesis (parser,
19327 /*recovering=*/true,
19328 /*or_comma=*/false,
19329 /*consume_paren=*/true);
19332 else
19334 /* We want to record the extra mangling scope for in-class
19335 initializers of class members and initializers of static data
19336 member templates. The former involves deferring
19337 parsing of the initializer until end of class as with default
19338 arguments. So right here we only handle the latter. */
19339 if (!member_p && processing_template_decl)
19340 start_lambda_scope (decl);
19341 initializer = cp_parser_initializer (parser,
19342 &is_direct_init,
19343 &is_non_constant_init);
19344 if (!member_p && processing_template_decl)
19345 finish_lambda_scope ();
19346 if (initializer == error_mark_node)
19347 cp_parser_skip_to_end_of_statement (parser);
19351 /* The old parser allows attributes to appear after a parenthesized
19352 initializer. Mark Mitchell proposed removing this functionality
19353 on the GCC mailing lists on 2002-08-13. This parser accepts the
19354 attributes -- but ignores them. */
19355 if (cp_parser_allow_gnu_extensions_p (parser)
19356 && initialization_kind == CPP_OPEN_PAREN)
19357 if (cp_parser_attributes_opt (parser))
19358 warning (OPT_Wattributes,
19359 "attributes after parenthesized initializer ignored");
19361 /* And now complain about a non-function implicit template. */
19362 if (bogus_implicit_tmpl && decl != error_mark_node)
19363 error_at (DECL_SOURCE_LOCATION (decl),
19364 "non-function %qD declared as implicit template", decl);
19366 /* For an in-class declaration, use `grokfield' to create the
19367 declaration. */
19368 if (member_p)
19370 if (pushed_scope)
19372 pop_scope (pushed_scope);
19373 pushed_scope = NULL_TREE;
19375 decl = grokfield (declarator, decl_specifiers,
19376 initializer, !is_non_constant_init,
19377 /*asmspec=*/NULL_TREE,
19378 chainon (attributes, prefix_attributes));
19379 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19380 cp_parser_save_default_args (parser, decl);
19381 cp_finalize_omp_declare_simd (parser, decl);
19382 cp_finalize_oacc_routine (parser, decl, false);
19385 /* Finish processing the declaration. But, skip member
19386 declarations. */
19387 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19389 cp_finish_decl (decl,
19390 initializer, !is_non_constant_init,
19391 asm_specification,
19392 /* If the initializer is in parentheses, then this is
19393 a direct-initialization, which means that an
19394 `explicit' constructor is OK. Otherwise, an
19395 `explicit' constructor cannot be used. */
19396 ((is_direct_init || !is_initialized)
19397 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19399 else if ((cxx_dialect != cxx98) && friend_p
19400 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19401 /* Core issue #226 (C++0x only): A default template-argument
19402 shall not be specified in a friend class template
19403 declaration. */
19404 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19405 /*is_partial=*/false, /*is_friend_decl=*/1);
19407 if (!friend_p && pushed_scope)
19408 pop_scope (pushed_scope);
19410 if (function_declarator_p (declarator)
19411 && parser->fully_implicit_function_template_p)
19413 if (member_p)
19414 decl = finish_fully_implicit_template (parser, decl);
19415 else
19416 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19419 if (auto_result && is_initialized && decl_specifiers->type
19420 && type_uses_auto (decl_specifiers->type))
19421 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19423 return decl;
19426 /* Parse a declarator.
19428 declarator:
19429 direct-declarator
19430 ptr-operator declarator
19432 abstract-declarator:
19433 ptr-operator abstract-declarator [opt]
19434 direct-abstract-declarator
19436 GNU Extensions:
19438 declarator:
19439 attributes [opt] direct-declarator
19440 attributes [opt] ptr-operator declarator
19442 abstract-declarator:
19443 attributes [opt] ptr-operator abstract-declarator [opt]
19444 attributes [opt] direct-abstract-declarator
19446 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19447 detect constructors, destructors, deduction guides, or conversion operators.
19448 It is set to -1 if the declarator is a name, and +1 if it is a
19449 function. Otherwise it is set to zero. Usually you just want to
19450 test for >0, but internally the negative value is used.
19452 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19453 a decl-specifier-seq unless it declares a constructor, destructor,
19454 or conversion. It might seem that we could check this condition in
19455 semantic analysis, rather than parsing, but that makes it difficult
19456 to handle something like `f()'. We want to notice that there are
19457 no decl-specifiers, and therefore realize that this is an
19458 expression, not a declaration.)
19460 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19461 the declarator is a direct-declarator of the form "(...)".
19463 MEMBER_P is true iff this declarator is a member-declarator.
19465 FRIEND_P is true iff this declarator is a friend. */
19467 static cp_declarator *
19468 cp_parser_declarator (cp_parser* parser,
19469 cp_parser_declarator_kind dcl_kind,
19470 int* ctor_dtor_or_conv_p,
19471 bool* parenthesized_p,
19472 bool member_p, bool friend_p)
19474 cp_declarator *declarator;
19475 enum tree_code code;
19476 cp_cv_quals cv_quals;
19477 tree class_type;
19478 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19480 /* Assume this is not a constructor, destructor, or type-conversion
19481 operator. */
19482 if (ctor_dtor_or_conv_p)
19483 *ctor_dtor_or_conv_p = 0;
19485 if (cp_parser_allow_gnu_extensions_p (parser))
19486 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19488 /* Check for the ptr-operator production. */
19489 cp_parser_parse_tentatively (parser);
19490 /* Parse the ptr-operator. */
19491 code = cp_parser_ptr_operator (parser,
19492 &class_type,
19493 &cv_quals,
19494 &std_attributes);
19496 /* If that worked, then we have a ptr-operator. */
19497 if (cp_parser_parse_definitely (parser))
19499 /* If a ptr-operator was found, then this declarator was not
19500 parenthesized. */
19501 if (parenthesized_p)
19502 *parenthesized_p = true;
19503 /* The dependent declarator is optional if we are parsing an
19504 abstract-declarator. */
19505 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19506 cp_parser_parse_tentatively (parser);
19508 /* Parse the dependent declarator. */
19509 declarator = cp_parser_declarator (parser, dcl_kind,
19510 /*ctor_dtor_or_conv_p=*/NULL,
19511 /*parenthesized_p=*/NULL,
19512 /*member_p=*/false,
19513 friend_p);
19515 /* If we are parsing an abstract-declarator, we must handle the
19516 case where the dependent declarator is absent. */
19517 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19518 && !cp_parser_parse_definitely (parser))
19519 declarator = NULL;
19521 declarator = cp_parser_make_indirect_declarator
19522 (code, class_type, cv_quals, declarator, std_attributes);
19524 /* Everything else is a direct-declarator. */
19525 else
19527 if (parenthesized_p)
19528 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19529 CPP_OPEN_PAREN);
19530 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19531 ctor_dtor_or_conv_p,
19532 member_p, friend_p);
19535 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19536 declarator->attributes = gnu_attributes;
19537 return declarator;
19540 /* Parse a direct-declarator or direct-abstract-declarator.
19542 direct-declarator:
19543 declarator-id
19544 direct-declarator ( parameter-declaration-clause )
19545 cv-qualifier-seq [opt]
19546 ref-qualifier [opt]
19547 exception-specification [opt]
19548 direct-declarator [ constant-expression [opt] ]
19549 ( declarator )
19551 direct-abstract-declarator:
19552 direct-abstract-declarator [opt]
19553 ( parameter-declaration-clause )
19554 cv-qualifier-seq [opt]
19555 ref-qualifier [opt]
19556 exception-specification [opt]
19557 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19558 ( abstract-declarator )
19560 Returns a representation of the declarator. DCL_KIND is
19561 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19562 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19563 we are parsing a direct-declarator. It is
19564 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19565 of ambiguity we prefer an abstract declarator, as per
19566 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19567 as for cp_parser_declarator. */
19569 static cp_declarator *
19570 cp_parser_direct_declarator (cp_parser* parser,
19571 cp_parser_declarator_kind dcl_kind,
19572 int* ctor_dtor_or_conv_p,
19573 bool member_p, bool friend_p)
19575 cp_token *token;
19576 cp_declarator *declarator = NULL;
19577 tree scope = NULL_TREE;
19578 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19579 bool saved_in_declarator_p = parser->in_declarator_p;
19580 bool first = true;
19581 tree pushed_scope = NULL_TREE;
19583 while (true)
19585 /* Peek at the next token. */
19586 token = cp_lexer_peek_token (parser->lexer);
19587 if (token->type == CPP_OPEN_PAREN)
19589 /* This is either a parameter-declaration-clause, or a
19590 parenthesized declarator. When we know we are parsing a
19591 named declarator, it must be a parenthesized declarator
19592 if FIRST is true. For instance, `(int)' is a
19593 parameter-declaration-clause, with an omitted
19594 direct-abstract-declarator. But `((*))', is a
19595 parenthesized abstract declarator. Finally, when T is a
19596 template parameter `(T)' is a
19597 parameter-declaration-clause, and not a parenthesized
19598 named declarator.
19600 We first try and parse a parameter-declaration-clause,
19601 and then try a nested declarator (if FIRST is true).
19603 It is not an error for it not to be a
19604 parameter-declaration-clause, even when FIRST is
19605 false. Consider,
19607 int i (int);
19608 int i (3);
19610 The first is the declaration of a function while the
19611 second is the definition of a variable, including its
19612 initializer.
19614 Having seen only the parenthesis, we cannot know which of
19615 these two alternatives should be selected. Even more
19616 complex are examples like:
19618 int i (int (a));
19619 int i (int (3));
19621 The former is a function-declaration; the latter is a
19622 variable initialization.
19624 Thus again, we try a parameter-declaration-clause, and if
19625 that fails, we back out and return. */
19627 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19629 tree params;
19630 bool is_declarator = false;
19632 /* In a member-declarator, the only valid interpretation
19633 of a parenthesis is the start of a
19634 parameter-declaration-clause. (It is invalid to
19635 initialize a static data member with a parenthesized
19636 initializer; only the "=" form of initialization is
19637 permitted.) */
19638 if (!member_p)
19639 cp_parser_parse_tentatively (parser);
19641 /* Consume the `('. */
19642 cp_lexer_consume_token (parser->lexer);
19643 if (first)
19645 /* If this is going to be an abstract declarator, we're
19646 in a declarator and we can't have default args. */
19647 parser->default_arg_ok_p = false;
19648 parser->in_declarator_p = true;
19651 begin_scope (sk_function_parms, NULL_TREE);
19653 /* Parse the parameter-declaration-clause. */
19654 params = cp_parser_parameter_declaration_clause (parser);
19656 /* Consume the `)'. */
19657 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19659 /* If all went well, parse the cv-qualifier-seq,
19660 ref-qualifier and the exception-specification. */
19661 if (member_p || cp_parser_parse_definitely (parser))
19663 cp_cv_quals cv_quals;
19664 cp_virt_specifiers virt_specifiers;
19665 cp_ref_qualifier ref_qual;
19666 tree exception_specification;
19667 tree late_return;
19668 tree attrs;
19669 bool memfn = (member_p || (pushed_scope
19670 && CLASS_TYPE_P (pushed_scope)));
19672 is_declarator = true;
19674 if (ctor_dtor_or_conv_p)
19675 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19676 first = false;
19678 /* Parse the cv-qualifier-seq. */
19679 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19680 /* Parse the ref-qualifier. */
19681 ref_qual = cp_parser_ref_qualifier_opt (parser);
19682 /* Parse the tx-qualifier. */
19683 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19684 /* And the exception-specification. */
19685 exception_specification
19686 = cp_parser_exception_specification_opt (parser);
19688 attrs = cp_parser_std_attribute_spec_seq (parser);
19690 /* In here, we handle cases where attribute is used after
19691 the function declaration. For example:
19692 void func (int x) __attribute__((vector(..))); */
19693 tree gnu_attrs = NULL_TREE;
19694 if (flag_cilkplus
19695 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19697 cp_parser_parse_tentatively (parser);
19698 tree attr = cp_parser_gnu_attributes_opt (parser);
19699 if (cp_lexer_next_token_is_not (parser->lexer,
19700 CPP_SEMICOLON)
19701 && cp_lexer_next_token_is_not (parser->lexer,
19702 CPP_OPEN_BRACE))
19703 cp_parser_abort_tentative_parse (parser);
19704 else if (!cp_parser_parse_definitely (parser))
19706 else
19707 gnu_attrs = attr;
19709 tree requires_clause = NULL_TREE;
19710 late_return = (cp_parser_late_return_type_opt
19711 (parser, declarator, requires_clause,
19712 memfn ? cv_quals : -1));
19714 /* Parse the virt-specifier-seq. */
19715 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19717 /* Create the function-declarator. */
19718 declarator = make_call_declarator (declarator,
19719 params,
19720 cv_quals,
19721 virt_specifiers,
19722 ref_qual,
19723 tx_qual,
19724 exception_specification,
19725 late_return,
19726 requires_clause);
19727 declarator->std_attributes = attrs;
19728 declarator->attributes = gnu_attrs;
19729 /* Any subsequent parameter lists are to do with
19730 return type, so are not those of the declared
19731 function. */
19732 parser->default_arg_ok_p = false;
19735 /* Remove the function parms from scope. */
19736 pop_bindings_and_leave_scope ();
19738 if (is_declarator)
19739 /* Repeat the main loop. */
19740 continue;
19743 /* If this is the first, we can try a parenthesized
19744 declarator. */
19745 if (first)
19747 bool saved_in_type_id_in_expr_p;
19749 parser->default_arg_ok_p = saved_default_arg_ok_p;
19750 parser->in_declarator_p = saved_in_declarator_p;
19752 /* Consume the `('. */
19753 cp_lexer_consume_token (parser->lexer);
19754 /* Parse the nested declarator. */
19755 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19756 parser->in_type_id_in_expr_p = true;
19757 declarator
19758 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19759 /*parenthesized_p=*/NULL,
19760 member_p, friend_p);
19761 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19762 first = false;
19763 /* Expect a `)'. */
19764 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19765 declarator = cp_error_declarator;
19766 if (declarator == cp_error_declarator)
19767 break;
19769 goto handle_declarator;
19771 /* Otherwise, we must be done. */
19772 else
19773 break;
19775 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19776 && token->type == CPP_OPEN_SQUARE
19777 && !cp_next_tokens_can_be_attribute_p (parser))
19779 /* Parse an array-declarator. */
19780 tree bounds, attrs;
19782 if (ctor_dtor_or_conv_p)
19783 *ctor_dtor_or_conv_p = 0;
19785 first = false;
19786 parser->default_arg_ok_p = false;
19787 parser->in_declarator_p = true;
19788 /* Consume the `['. */
19789 cp_lexer_consume_token (parser->lexer);
19790 /* Peek at the next token. */
19791 token = cp_lexer_peek_token (parser->lexer);
19792 /* If the next token is `]', then there is no
19793 constant-expression. */
19794 if (token->type != CPP_CLOSE_SQUARE)
19796 bool non_constant_p;
19797 bounds
19798 = cp_parser_constant_expression (parser,
19799 /*allow_non_constant=*/true,
19800 &non_constant_p);
19801 if (!non_constant_p)
19802 /* OK */;
19803 else if (error_operand_p (bounds))
19804 /* Already gave an error. */;
19805 else if (!parser->in_function_body
19806 || current_binding_level->kind == sk_function_parms)
19808 /* Normally, the array bound must be an integral constant
19809 expression. However, as an extension, we allow VLAs
19810 in function scopes as long as they aren't part of a
19811 parameter declaration. */
19812 cp_parser_error (parser,
19813 "array bound is not an integer constant");
19814 bounds = error_mark_node;
19816 else if (processing_template_decl
19817 && !type_dependent_expression_p (bounds))
19819 /* Remember this wasn't a constant-expression. */
19820 bounds = build_nop (TREE_TYPE (bounds), bounds);
19821 TREE_SIDE_EFFECTS (bounds) = 1;
19824 else
19825 bounds = NULL_TREE;
19826 /* Look for the closing `]'. */
19827 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19829 declarator = cp_error_declarator;
19830 break;
19833 attrs = cp_parser_std_attribute_spec_seq (parser);
19834 declarator = make_array_declarator (declarator, bounds);
19835 declarator->std_attributes = attrs;
19837 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19840 tree qualifying_scope;
19841 tree unqualified_name;
19842 tree attrs;
19843 special_function_kind sfk;
19844 bool abstract_ok;
19845 bool pack_expansion_p = false;
19846 cp_token *declarator_id_start_token;
19848 /* Parse a declarator-id */
19849 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19850 if (abstract_ok)
19852 cp_parser_parse_tentatively (parser);
19854 /* If we see an ellipsis, we should be looking at a
19855 parameter pack. */
19856 if (token->type == CPP_ELLIPSIS)
19858 /* Consume the `...' */
19859 cp_lexer_consume_token (parser->lexer);
19861 pack_expansion_p = true;
19865 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19866 unqualified_name
19867 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19868 qualifying_scope = parser->scope;
19869 if (abstract_ok)
19871 bool okay = false;
19873 if (!unqualified_name && pack_expansion_p)
19875 /* Check whether an error occurred. */
19876 okay = !cp_parser_error_occurred (parser);
19878 /* We already consumed the ellipsis to mark a
19879 parameter pack, but we have no way to report it,
19880 so abort the tentative parse. We will be exiting
19881 immediately anyway. */
19882 cp_parser_abort_tentative_parse (parser);
19884 else
19885 okay = cp_parser_parse_definitely (parser);
19887 if (!okay)
19888 unqualified_name = error_mark_node;
19889 else if (unqualified_name
19890 && (qualifying_scope
19891 || (!identifier_p (unqualified_name))))
19893 cp_parser_error (parser, "expected unqualified-id");
19894 unqualified_name = error_mark_node;
19898 if (!unqualified_name)
19899 return NULL;
19900 if (unqualified_name == error_mark_node)
19902 declarator = cp_error_declarator;
19903 pack_expansion_p = false;
19904 declarator->parameter_pack_p = false;
19905 break;
19908 attrs = cp_parser_std_attribute_spec_seq (parser);
19910 if (qualifying_scope && at_namespace_scope_p ()
19911 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19913 /* In the declaration of a member of a template class
19914 outside of the class itself, the SCOPE will sometimes
19915 be a TYPENAME_TYPE. For example, given:
19917 template <typename T>
19918 int S<T>::R::i = 3;
19920 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19921 this context, we must resolve S<T>::R to an ordinary
19922 type, rather than a typename type.
19924 The reason we normally avoid resolving TYPENAME_TYPEs
19925 is that a specialization of `S' might render
19926 `S<T>::R' not a type. However, if `S' is
19927 specialized, then this `i' will not be used, so there
19928 is no harm in resolving the types here. */
19929 tree type;
19931 /* Resolve the TYPENAME_TYPE. */
19932 type = resolve_typename_type (qualifying_scope,
19933 /*only_current_p=*/false);
19934 /* If that failed, the declarator is invalid. */
19935 if (TREE_CODE (type) == TYPENAME_TYPE)
19937 if (typedef_variant_p (type))
19938 error_at (declarator_id_start_token->location,
19939 "cannot define member of dependent typedef "
19940 "%qT", type);
19941 else
19942 error_at (declarator_id_start_token->location,
19943 "%<%T::%E%> is not a type",
19944 TYPE_CONTEXT (qualifying_scope),
19945 TYPE_IDENTIFIER (qualifying_scope));
19947 qualifying_scope = type;
19950 sfk = sfk_none;
19952 if (unqualified_name)
19954 tree class_type;
19956 if (qualifying_scope
19957 && CLASS_TYPE_P (qualifying_scope))
19958 class_type = qualifying_scope;
19959 else
19960 class_type = current_class_type;
19962 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19964 tree name_type = TREE_TYPE (unqualified_name);
19965 if (class_type && same_type_p (name_type, class_type))
19967 if (qualifying_scope
19968 && CLASSTYPE_USE_TEMPLATE (name_type))
19970 error_at (declarator_id_start_token->location,
19971 "invalid use of constructor as a template");
19972 inform (declarator_id_start_token->location,
19973 "use %<%T::%D%> instead of %<%T::%D%> to "
19974 "name the constructor in a qualified name",
19975 class_type,
19976 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19977 class_type, name_type);
19978 declarator = cp_error_declarator;
19979 break;
19981 else
19982 unqualified_name = constructor_name (class_type);
19984 else
19986 /* We do not attempt to print the declarator
19987 here because we do not have enough
19988 information about its original syntactic
19989 form. */
19990 cp_parser_error (parser, "invalid declarator");
19991 declarator = cp_error_declarator;
19992 break;
19996 if (class_type)
19998 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19999 sfk = sfk_destructor;
20000 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
20001 sfk = sfk_conversion;
20002 else if (/* There's no way to declare a constructor
20003 for an unnamed type, even if the type
20004 got a name for linkage purposes. */
20005 !TYPE_WAS_UNNAMED (class_type)
20006 /* Handle correctly (c++/19200):
20008 struct S {
20009 struct T{};
20010 friend void S(T);
20013 and also:
20015 namespace N {
20016 void S();
20019 struct S {
20020 friend void N::S();
20021 }; */
20022 && !(friend_p
20023 && class_type != qualifying_scope)
20024 && constructor_name_p (unqualified_name,
20025 class_type))
20027 unqualified_name = constructor_name (class_type);
20028 sfk = sfk_constructor;
20030 else if (is_overloaded_fn (unqualified_name)
20031 && DECL_CONSTRUCTOR_P (get_first_fn
20032 (unqualified_name)))
20033 sfk = sfk_constructor;
20035 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20036 *ctor_dtor_or_conv_p = -1;
20039 declarator = make_id_declarator (qualifying_scope,
20040 unqualified_name,
20041 sfk);
20042 declarator->std_attributes = attrs;
20043 declarator->id_loc = token->location;
20044 declarator->parameter_pack_p = pack_expansion_p;
20046 if (pack_expansion_p)
20047 maybe_warn_variadic_templates ();
20050 handle_declarator:;
20051 scope = get_scope_of_declarator (declarator);
20052 if (scope)
20054 /* Any names that appear after the declarator-id for a
20055 member are looked up in the containing scope. */
20056 if (at_function_scope_p ())
20058 /* But declarations with qualified-ids can't appear in a
20059 function. */
20060 cp_parser_error (parser, "qualified-id in declaration");
20061 declarator = cp_error_declarator;
20062 break;
20064 pushed_scope = push_scope (scope);
20066 parser->in_declarator_p = true;
20067 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20068 || (declarator && declarator->kind == cdk_id))
20069 /* Default args are only allowed on function
20070 declarations. */
20071 parser->default_arg_ok_p = saved_default_arg_ok_p;
20072 else
20073 parser->default_arg_ok_p = false;
20075 first = false;
20077 /* We're done. */
20078 else
20079 break;
20082 /* For an abstract declarator, we might wind up with nothing at this
20083 point. That's an error; the declarator is not optional. */
20084 if (!declarator)
20085 cp_parser_error (parser, "expected declarator");
20087 /* If we entered a scope, we must exit it now. */
20088 if (pushed_scope)
20089 pop_scope (pushed_scope);
20091 parser->default_arg_ok_p = saved_default_arg_ok_p;
20092 parser->in_declarator_p = saved_in_declarator_p;
20094 return declarator;
20097 /* Parse a ptr-operator.
20099 ptr-operator:
20100 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20101 * cv-qualifier-seq [opt]
20103 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20104 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20106 GNU Extension:
20108 ptr-operator:
20109 & cv-qualifier-seq [opt]
20111 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20112 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20113 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20114 filled in with the TYPE containing the member. *CV_QUALS is
20115 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20116 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20117 Note that the tree codes returned by this function have nothing
20118 to do with the types of trees that will be eventually be created
20119 to represent the pointer or reference type being parsed. They are
20120 just constants with suggestive names. */
20121 static enum tree_code
20122 cp_parser_ptr_operator (cp_parser* parser,
20123 tree* type,
20124 cp_cv_quals *cv_quals,
20125 tree *attributes)
20127 enum tree_code code = ERROR_MARK;
20128 cp_token *token;
20129 tree attrs = NULL_TREE;
20131 /* Assume that it's not a pointer-to-member. */
20132 *type = NULL_TREE;
20133 /* And that there are no cv-qualifiers. */
20134 *cv_quals = TYPE_UNQUALIFIED;
20136 /* Peek at the next token. */
20137 token = cp_lexer_peek_token (parser->lexer);
20139 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20140 if (token->type == CPP_MULT)
20141 code = INDIRECT_REF;
20142 else if (token->type == CPP_AND)
20143 code = ADDR_EXPR;
20144 else if ((cxx_dialect != cxx98) &&
20145 token->type == CPP_AND_AND) /* C++0x only */
20146 code = NON_LVALUE_EXPR;
20148 if (code != ERROR_MARK)
20150 /* Consume the `*', `&' or `&&'. */
20151 cp_lexer_consume_token (parser->lexer);
20153 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20154 `&', if we are allowing GNU extensions. (The only qualifier
20155 that can legally appear after `&' is `restrict', but that is
20156 enforced during semantic analysis. */
20157 if (code == INDIRECT_REF
20158 || cp_parser_allow_gnu_extensions_p (parser))
20159 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20161 attrs = cp_parser_std_attribute_spec_seq (parser);
20162 if (attributes != NULL)
20163 *attributes = attrs;
20165 else
20167 /* Try the pointer-to-member case. */
20168 cp_parser_parse_tentatively (parser);
20169 /* Look for the optional `::' operator. */
20170 cp_parser_global_scope_opt (parser,
20171 /*current_scope_valid_p=*/false);
20172 /* Look for the nested-name specifier. */
20173 token = cp_lexer_peek_token (parser->lexer);
20174 cp_parser_nested_name_specifier (parser,
20175 /*typename_keyword_p=*/false,
20176 /*check_dependency_p=*/true,
20177 /*type_p=*/false,
20178 /*is_declaration=*/false);
20179 /* If we found it, and the next token is a `*', then we are
20180 indeed looking at a pointer-to-member operator. */
20181 if (!cp_parser_error_occurred (parser)
20182 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20184 /* Indicate that the `*' operator was used. */
20185 code = INDIRECT_REF;
20187 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20188 error_at (token->location, "%qD is a namespace", parser->scope);
20189 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20190 error_at (token->location, "cannot form pointer to member of "
20191 "non-class %q#T", parser->scope);
20192 else
20194 /* The type of which the member is a member is given by the
20195 current SCOPE. */
20196 *type = parser->scope;
20197 /* The next name will not be qualified. */
20198 parser->scope = NULL_TREE;
20199 parser->qualifying_scope = NULL_TREE;
20200 parser->object_scope = NULL_TREE;
20201 /* Look for optional c++11 attributes. */
20202 attrs = cp_parser_std_attribute_spec_seq (parser);
20203 if (attributes != NULL)
20204 *attributes = attrs;
20205 /* Look for the optional cv-qualifier-seq. */
20206 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20209 /* If that didn't work we don't have a ptr-operator. */
20210 if (!cp_parser_parse_definitely (parser))
20211 cp_parser_error (parser, "expected ptr-operator");
20214 return code;
20217 /* Parse an (optional) cv-qualifier-seq.
20219 cv-qualifier-seq:
20220 cv-qualifier cv-qualifier-seq [opt]
20222 cv-qualifier:
20223 const
20224 volatile
20226 GNU Extension:
20228 cv-qualifier:
20229 __restrict__
20231 Returns a bitmask representing the cv-qualifiers. */
20233 static cp_cv_quals
20234 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20236 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20238 while (true)
20240 cp_token *token;
20241 cp_cv_quals cv_qualifier;
20243 /* Peek at the next token. */
20244 token = cp_lexer_peek_token (parser->lexer);
20245 /* See if it's a cv-qualifier. */
20246 switch (token->keyword)
20248 case RID_CONST:
20249 cv_qualifier = TYPE_QUAL_CONST;
20250 break;
20252 case RID_VOLATILE:
20253 cv_qualifier = TYPE_QUAL_VOLATILE;
20254 break;
20256 case RID_RESTRICT:
20257 cv_qualifier = TYPE_QUAL_RESTRICT;
20258 break;
20260 default:
20261 cv_qualifier = TYPE_UNQUALIFIED;
20262 break;
20265 if (!cv_qualifier)
20266 break;
20268 if (cv_quals & cv_qualifier)
20270 gcc_rich_location richloc (token->location);
20271 richloc.add_fixit_remove ();
20272 error_at_rich_loc (&richloc, "duplicate cv-qualifier");
20273 cp_lexer_purge_token (parser->lexer);
20275 else
20277 cp_lexer_consume_token (parser->lexer);
20278 cv_quals |= cv_qualifier;
20282 return cv_quals;
20285 /* Parse an (optional) ref-qualifier
20287 ref-qualifier:
20291 Returns cp_ref_qualifier representing ref-qualifier. */
20293 static cp_ref_qualifier
20294 cp_parser_ref_qualifier_opt (cp_parser* parser)
20296 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20298 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20299 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20300 return ref_qual;
20302 while (true)
20304 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20305 cp_token *token = cp_lexer_peek_token (parser->lexer);
20307 switch (token->type)
20309 case CPP_AND:
20310 curr_ref_qual = REF_QUAL_LVALUE;
20311 break;
20313 case CPP_AND_AND:
20314 curr_ref_qual = REF_QUAL_RVALUE;
20315 break;
20317 default:
20318 curr_ref_qual = REF_QUAL_NONE;
20319 break;
20322 if (!curr_ref_qual)
20323 break;
20324 else if (ref_qual)
20326 error_at (token->location, "multiple ref-qualifiers");
20327 cp_lexer_purge_token (parser->lexer);
20329 else
20331 ref_qual = curr_ref_qual;
20332 cp_lexer_consume_token (parser->lexer);
20336 return ref_qual;
20339 /* Parse an optional tx-qualifier.
20341 tx-qualifier:
20342 transaction_safe
20343 transaction_safe_dynamic */
20345 static tree
20346 cp_parser_tx_qualifier_opt (cp_parser *parser)
20348 cp_token *token = cp_lexer_peek_token (parser->lexer);
20349 if (token->type == CPP_NAME)
20351 tree name = token->u.value;
20352 const char *p = IDENTIFIER_POINTER (name);
20353 const int len = strlen ("transaction_safe");
20354 if (!strncmp (p, "transaction_safe", len))
20356 p += len;
20357 if (*p == '\0'
20358 || !strcmp (p, "_dynamic"))
20360 cp_lexer_consume_token (parser->lexer);
20361 if (!flag_tm)
20363 error ("%qE requires %<-fgnu-tm%>", name);
20364 return NULL_TREE;
20366 else
20367 return name;
20371 return NULL_TREE;
20374 /* Parse an (optional) virt-specifier-seq.
20376 virt-specifier-seq:
20377 virt-specifier virt-specifier-seq [opt]
20379 virt-specifier:
20380 override
20381 final
20383 Returns a bitmask representing the virt-specifiers. */
20385 static cp_virt_specifiers
20386 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20388 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20390 while (true)
20392 cp_token *token;
20393 cp_virt_specifiers virt_specifier;
20395 /* Peek at the next token. */
20396 token = cp_lexer_peek_token (parser->lexer);
20397 /* See if it's a virt-specifier-qualifier. */
20398 if (token->type != CPP_NAME)
20399 break;
20400 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
20402 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20403 virt_specifier = VIRT_SPEC_OVERRIDE;
20405 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
20407 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20408 virt_specifier = VIRT_SPEC_FINAL;
20410 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
20412 virt_specifier = VIRT_SPEC_FINAL;
20414 else
20415 break;
20417 if (virt_specifiers & virt_specifier)
20419 gcc_rich_location richloc (token->location);
20420 richloc.add_fixit_remove ();
20421 error_at_rich_loc (&richloc, "duplicate virt-specifier");
20422 cp_lexer_purge_token (parser->lexer);
20424 else
20426 cp_lexer_consume_token (parser->lexer);
20427 virt_specifiers |= virt_specifier;
20430 return virt_specifiers;
20433 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20434 is in scope even though it isn't real. */
20436 void
20437 inject_this_parameter (tree ctype, cp_cv_quals quals)
20439 tree this_parm;
20441 if (current_class_ptr)
20443 /* We don't clear this between NSDMIs. Is it already what we want? */
20444 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20445 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
20446 && cp_type_quals (type) == quals)
20447 return;
20450 this_parm = build_this_parm (ctype, quals);
20451 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20452 current_class_ptr = NULL_TREE;
20453 current_class_ref
20454 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20455 current_class_ptr = this_parm;
20458 /* Return true iff our current scope is a non-static data member
20459 initializer. */
20461 bool
20462 parsing_nsdmi (void)
20464 /* We recognize NSDMI context by the context-less 'this' pointer set up
20465 by the function above. */
20466 if (current_class_ptr
20467 && TREE_CODE (current_class_ptr) == PARM_DECL
20468 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20469 return true;
20470 return false;
20473 /* Return true iff our current scope is a default capturing generic lambda
20474 defined within a template. FIXME: This is part of a workaround (see
20475 semantics.c) to handle building lambda closure types correctly in templates
20476 which we ultimately want to defer to instantiation time. */
20478 bool
20479 parsing_default_capturing_generic_lambda_in_template (void)
20481 if (!processing_template_decl || !current_class_type)
20482 return false;
20484 tree lam = CLASSTYPE_LAMBDA_EXPR (current_class_type);
20485 if (!lam || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
20486 return false;
20488 tree callop = lambda_function (lam);
20489 if (!callop)
20490 return false;
20492 return (DECL_TEMPLATE_INFO (callop)
20493 && (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (callop)) == callop)
20494 && ((current_nonlambda_class_type ()
20495 && CLASSTYPE_TEMPLATE_INFO (current_nonlambda_class_type ()))
20496 || ((current_nonlambda_function ()
20497 && DECL_TEMPLATE_INFO (current_nonlambda_function ())))));
20500 /* Parse a late-specified return type, if any. This is not a separate
20501 non-terminal, but part of a function declarator, which looks like
20503 -> trailing-type-specifier-seq abstract-declarator(opt)
20505 Returns the type indicated by the type-id.
20507 In addition to this, parse any queued up #pragma omp declare simd
20508 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20509 #pragma acc routine clauses.
20511 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20512 function. */
20514 static tree
20515 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20516 tree& requires_clause, cp_cv_quals quals)
20518 cp_token *token;
20519 tree type = NULL_TREE;
20520 bool declare_simd_p = (parser->omp_declare_simd
20521 && declarator
20522 && declarator->kind == cdk_id);
20524 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20525 && declarator && declarator->kind == cdk_id);
20527 bool oacc_routine_p = (parser->oacc_routine
20528 && declarator
20529 && declarator->kind == cdk_id);
20531 /* Peek at the next token. */
20532 token = cp_lexer_peek_token (parser->lexer);
20533 /* A late-specified return type is indicated by an initial '->'. */
20534 if (token->type != CPP_DEREF
20535 && token->keyword != RID_REQUIRES
20536 && !(token->type == CPP_NAME
20537 && token->u.value == ridpointers[RID_REQUIRES])
20538 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20539 return NULL_TREE;
20541 tree save_ccp = current_class_ptr;
20542 tree save_ccr = current_class_ref;
20543 if (quals >= 0)
20545 /* DR 1207: 'this' is in scope in the trailing return type. */
20546 inject_this_parameter (current_class_type, quals);
20549 if (token->type == CPP_DEREF)
20551 /* Consume the ->. */
20552 cp_lexer_consume_token (parser->lexer);
20554 type = cp_parser_trailing_type_id (parser);
20557 /* Function declarations may be followed by a trailing
20558 requires-clause. */
20559 requires_clause = cp_parser_requires_clause_opt (parser);
20561 if (cilk_simd_fn_vector_p)
20562 declarator->attributes
20563 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20564 declarator->attributes);
20565 if (declare_simd_p)
20566 declarator->attributes
20567 = cp_parser_late_parsing_omp_declare_simd (parser,
20568 declarator->attributes);
20569 if (oacc_routine_p)
20570 declarator->attributes
20571 = cp_parser_late_parsing_oacc_routine (parser,
20572 declarator->attributes);
20574 if (quals >= 0)
20576 current_class_ptr = save_ccp;
20577 current_class_ref = save_ccr;
20580 return type;
20583 /* Parse a declarator-id.
20585 declarator-id:
20586 id-expression
20587 :: [opt] nested-name-specifier [opt] type-name
20589 In the `id-expression' case, the value returned is as for
20590 cp_parser_id_expression if the id-expression was an unqualified-id.
20591 If the id-expression was a qualified-id, then a SCOPE_REF is
20592 returned. The first operand is the scope (either a NAMESPACE_DECL
20593 or TREE_TYPE), but the second is still just a representation of an
20594 unqualified-id. */
20596 static tree
20597 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20599 tree id;
20600 /* The expression must be an id-expression. Assume that qualified
20601 names are the names of types so that:
20603 template <class T>
20604 int S<T>::R::i = 3;
20606 will work; we must treat `S<T>::R' as the name of a type.
20607 Similarly, assume that qualified names are templates, where
20608 required, so that:
20610 template <class T>
20611 int S<T>::R<T>::i = 3;
20613 will work, too. */
20614 id = cp_parser_id_expression (parser,
20615 /*template_keyword_p=*/false,
20616 /*check_dependency_p=*/false,
20617 /*template_p=*/NULL,
20618 /*declarator_p=*/true,
20619 optional_p);
20620 if (id && BASELINK_P (id))
20621 id = BASELINK_FUNCTIONS (id);
20622 return id;
20625 /* Parse a type-id.
20627 type-id:
20628 type-specifier-seq abstract-declarator [opt]
20630 Returns the TYPE specified. */
20632 static tree
20633 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20634 bool is_trailing_return)
20636 cp_decl_specifier_seq type_specifier_seq;
20637 cp_declarator *abstract_declarator;
20639 /* Parse the type-specifier-seq. */
20640 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20641 is_trailing_return,
20642 &type_specifier_seq);
20643 if (is_template_arg && type_specifier_seq.type
20644 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20645 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20646 /* A bare template name as a template argument is a template template
20647 argument, not a placeholder, so fail parsing it as a type argument. */
20649 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20650 cp_parser_simulate_error (parser);
20651 return error_mark_node;
20653 if (type_specifier_seq.type == error_mark_node)
20654 return error_mark_node;
20656 /* There might or might not be an abstract declarator. */
20657 cp_parser_parse_tentatively (parser);
20658 /* Look for the declarator. */
20659 abstract_declarator
20660 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20661 /*parenthesized_p=*/NULL,
20662 /*member_p=*/false,
20663 /*friend_p=*/false);
20664 /* Check to see if there really was a declarator. */
20665 if (!cp_parser_parse_definitely (parser))
20666 abstract_declarator = NULL;
20668 if (type_specifier_seq.type
20669 /* The concepts TS allows 'auto' as a type-id. */
20670 && (!flag_concepts || parser->in_type_id_in_expr_p)
20671 /* None of the valid uses of 'auto' in C++14 involve the type-id
20672 nonterminal, but it is valid in a trailing-return-type. */
20673 && !(cxx_dialect >= cxx14 && is_trailing_return))
20674 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20676 /* A type-id with type 'auto' is only ok if the abstract declarator
20677 is a function declarator with a late-specified return type.
20679 A type-id with 'auto' is also valid in a trailing-return-type
20680 in a compound-requirement. */
20681 if (abstract_declarator
20682 && abstract_declarator->kind == cdk_function
20683 && abstract_declarator->u.function.late_return_type)
20684 /* OK */;
20685 else if (parser->in_result_type_constraint_p)
20686 /* OK */;
20687 else
20689 location_t loc = type_specifier_seq.locations[ds_type_spec];
20690 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20692 error_at (loc, "missing template arguments after %qT",
20693 auto_node);
20694 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20695 tmpl);
20697 else
20698 error_at (loc, "invalid use of %qT", auto_node);
20699 return error_mark_node;
20703 return groktypename (&type_specifier_seq, abstract_declarator,
20704 is_template_arg);
20707 static tree
20708 cp_parser_type_id (cp_parser *parser)
20710 return cp_parser_type_id_1 (parser, false, false);
20713 static tree
20714 cp_parser_template_type_arg (cp_parser *parser)
20716 tree r;
20717 const char *saved_message = parser->type_definition_forbidden_message;
20718 parser->type_definition_forbidden_message
20719 = G_("types may not be defined in template arguments");
20720 r = cp_parser_type_id_1 (parser, true, false);
20721 parser->type_definition_forbidden_message = saved_message;
20722 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20724 error ("invalid use of %<auto%> in template argument");
20725 r = error_mark_node;
20727 return r;
20730 static tree
20731 cp_parser_trailing_type_id (cp_parser *parser)
20733 return cp_parser_type_id_1 (parser, false, true);
20736 /* Parse a type-specifier-seq.
20738 type-specifier-seq:
20739 type-specifier type-specifier-seq [opt]
20741 GNU extension:
20743 type-specifier-seq:
20744 attributes type-specifier-seq [opt]
20746 If IS_DECLARATION is true, we are at the start of a "condition" or
20747 exception-declaration, so we might be followed by a declarator-id.
20749 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20750 i.e. we've just seen "->".
20752 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20754 static void
20755 cp_parser_type_specifier_seq (cp_parser* parser,
20756 bool is_declaration,
20757 bool is_trailing_return,
20758 cp_decl_specifier_seq *type_specifier_seq)
20760 bool seen_type_specifier = false;
20761 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20762 cp_token *start_token = NULL;
20764 /* Clear the TYPE_SPECIFIER_SEQ. */
20765 clear_decl_specs (type_specifier_seq);
20767 /* In the context of a trailing return type, enum E { } is an
20768 elaborated-type-specifier followed by a function-body, not an
20769 enum-specifier. */
20770 if (is_trailing_return)
20771 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20773 /* Parse the type-specifiers and attributes. */
20774 while (true)
20776 tree type_specifier;
20777 bool is_cv_qualifier;
20779 /* Check for attributes first. */
20780 if (cp_next_tokens_can_be_attribute_p (parser))
20782 type_specifier_seq->attributes =
20783 chainon (type_specifier_seq->attributes,
20784 cp_parser_attributes_opt (parser));
20785 continue;
20788 /* record the token of the beginning of the type specifier seq,
20789 for error reporting purposes*/
20790 if (!start_token)
20791 start_token = cp_lexer_peek_token (parser->lexer);
20793 /* Look for the type-specifier. */
20794 type_specifier = cp_parser_type_specifier (parser,
20795 flags,
20796 type_specifier_seq,
20797 /*is_declaration=*/false,
20798 NULL,
20799 &is_cv_qualifier);
20800 if (!type_specifier)
20802 /* If the first type-specifier could not be found, this is not a
20803 type-specifier-seq at all. */
20804 if (!seen_type_specifier)
20806 /* Set in_declarator_p to avoid skipping to the semicolon. */
20807 int in_decl = parser->in_declarator_p;
20808 parser->in_declarator_p = true;
20810 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20811 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20812 cp_parser_error (parser, "expected type-specifier");
20814 parser->in_declarator_p = in_decl;
20816 type_specifier_seq->type = error_mark_node;
20817 return;
20819 /* If subsequent type-specifiers could not be found, the
20820 type-specifier-seq is complete. */
20821 break;
20824 seen_type_specifier = true;
20825 /* The standard says that a condition can be:
20827 type-specifier-seq declarator = assignment-expression
20829 However, given:
20831 struct S {};
20832 if (int S = ...)
20834 we should treat the "S" as a declarator, not as a
20835 type-specifier. The standard doesn't say that explicitly for
20836 type-specifier-seq, but it does say that for
20837 decl-specifier-seq in an ordinary declaration. Perhaps it
20838 would be clearer just to allow a decl-specifier-seq here, and
20839 then add a semantic restriction that if any decl-specifiers
20840 that are not type-specifiers appear, the program is invalid. */
20841 if (is_declaration && !is_cv_qualifier)
20842 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20846 /* Return whether the function currently being declared has an associated
20847 template parameter list. */
20849 static bool
20850 function_being_declared_is_template_p (cp_parser* parser)
20852 if (!current_template_parms || processing_template_parmlist)
20853 return false;
20855 if (parser->implicit_template_scope)
20856 return true;
20858 if (at_class_scope_p ()
20859 && TYPE_BEING_DEFINED (current_class_type))
20860 return parser->num_template_parameter_lists != 0;
20862 return ((int) parser->num_template_parameter_lists > template_class_depth
20863 (current_class_type));
20866 /* Parse a parameter-declaration-clause.
20868 parameter-declaration-clause:
20869 parameter-declaration-list [opt] ... [opt]
20870 parameter-declaration-list , ...
20872 Returns a representation for the parameter declarations. A return
20873 value of NULL indicates a parameter-declaration-clause consisting
20874 only of an ellipsis. */
20876 static tree
20877 cp_parser_parameter_declaration_clause (cp_parser* parser)
20879 tree parameters;
20880 cp_token *token;
20881 bool ellipsis_p;
20882 bool is_error;
20884 struct cleanup {
20885 cp_parser* parser;
20886 int auto_is_implicit_function_template_parm_p;
20887 ~cleanup() {
20888 parser->auto_is_implicit_function_template_parm_p
20889 = auto_is_implicit_function_template_parm_p;
20891 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20893 (void) cleanup;
20895 if (!processing_specialization
20896 && !processing_template_parmlist
20897 && !processing_explicit_instantiation)
20898 if (!current_function_decl
20899 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20900 parser->auto_is_implicit_function_template_parm_p = true;
20902 /* Peek at the next token. */
20903 token = cp_lexer_peek_token (parser->lexer);
20904 /* Check for trivial parameter-declaration-clauses. */
20905 if (token->type == CPP_ELLIPSIS)
20907 /* Consume the `...' token. */
20908 cp_lexer_consume_token (parser->lexer);
20909 return NULL_TREE;
20911 else if (token->type == CPP_CLOSE_PAREN)
20912 /* There are no parameters. */
20914 #ifndef NO_IMPLICIT_EXTERN_C
20915 if (in_system_header_at (input_location)
20916 && current_class_type == NULL
20917 && current_lang_name == lang_name_c)
20918 return NULL_TREE;
20919 else
20920 #endif
20921 return void_list_node;
20923 /* Check for `(void)', too, which is a special case. */
20924 else if (token->keyword == RID_VOID
20925 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20926 == CPP_CLOSE_PAREN))
20928 /* Consume the `void' token. */
20929 cp_lexer_consume_token (parser->lexer);
20930 /* There are no parameters. */
20931 return void_list_node;
20934 /* Parse the parameter-declaration-list. */
20935 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20936 /* If a parse error occurred while parsing the
20937 parameter-declaration-list, then the entire
20938 parameter-declaration-clause is erroneous. */
20939 if (is_error)
20940 return NULL;
20942 /* Peek at the next token. */
20943 token = cp_lexer_peek_token (parser->lexer);
20944 /* If it's a `,', the clause should terminate with an ellipsis. */
20945 if (token->type == CPP_COMMA)
20947 /* Consume the `,'. */
20948 cp_lexer_consume_token (parser->lexer);
20949 /* Expect an ellipsis. */
20950 ellipsis_p
20951 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20953 /* It might also be `...' if the optional trailing `,' was
20954 omitted. */
20955 else if (token->type == CPP_ELLIPSIS)
20957 /* Consume the `...' token. */
20958 cp_lexer_consume_token (parser->lexer);
20959 /* And remember that we saw it. */
20960 ellipsis_p = true;
20962 else
20963 ellipsis_p = false;
20965 /* Finish the parameter list. */
20966 if (!ellipsis_p)
20967 parameters = chainon (parameters, void_list_node);
20969 return parameters;
20972 /* Parse a parameter-declaration-list.
20974 parameter-declaration-list:
20975 parameter-declaration
20976 parameter-declaration-list , parameter-declaration
20978 Returns a representation of the parameter-declaration-list, as for
20979 cp_parser_parameter_declaration_clause. However, the
20980 `void_list_node' is never appended to the list. Upon return,
20981 *IS_ERROR will be true iff an error occurred. */
20983 static tree
20984 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20986 tree parameters = NULL_TREE;
20987 tree *tail = &parameters;
20988 bool saved_in_unbraced_linkage_specification_p;
20989 int index = 0;
20991 /* Assume all will go well. */
20992 *is_error = false;
20993 /* The special considerations that apply to a function within an
20994 unbraced linkage specifications do not apply to the parameters
20995 to the function. */
20996 saved_in_unbraced_linkage_specification_p
20997 = parser->in_unbraced_linkage_specification_p;
20998 parser->in_unbraced_linkage_specification_p = false;
21000 /* Look for more parameters. */
21001 while (true)
21003 cp_parameter_declarator *parameter;
21004 tree decl = error_mark_node;
21005 bool parenthesized_p = false;
21006 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21007 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21008 (current_template_parms)) : 0);
21010 /* Parse the parameter. */
21011 parameter
21012 = cp_parser_parameter_declaration (parser,
21013 /*template_parm_p=*/false,
21014 &parenthesized_p);
21016 /* We don't know yet if the enclosing context is deprecated, so wait
21017 and warn in grokparms if appropriate. */
21018 deprecated_state = DEPRECATED_SUPPRESS;
21020 if (parameter)
21022 /* If a function parameter pack was specified and an implicit template
21023 parameter was introduced during cp_parser_parameter_declaration,
21024 change any implicit parameters introduced into packs. */
21025 if (parser->implicit_template_parms
21026 && parameter->declarator
21027 && parameter->declarator->parameter_pack_p)
21029 int latest_template_parm_idx = TREE_VEC_LENGTH
21030 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21032 if (latest_template_parm_idx != template_parm_idx)
21033 parameter->decl_specifiers.type = convert_generic_types_to_packs
21034 (parameter->decl_specifiers.type,
21035 template_parm_idx, latest_template_parm_idx);
21038 decl = grokdeclarator (parameter->declarator,
21039 &parameter->decl_specifiers,
21040 PARM,
21041 parameter->default_argument != NULL_TREE,
21042 &parameter->decl_specifiers.attributes);
21045 deprecated_state = DEPRECATED_NORMAL;
21047 /* If a parse error occurred parsing the parameter declaration,
21048 then the entire parameter-declaration-list is erroneous. */
21049 if (decl == error_mark_node)
21051 *is_error = true;
21052 parameters = error_mark_node;
21053 break;
21056 if (parameter->decl_specifiers.attributes)
21057 cplus_decl_attributes (&decl,
21058 parameter->decl_specifiers.attributes,
21060 if (DECL_NAME (decl))
21061 decl = pushdecl (decl);
21063 if (decl != error_mark_node)
21065 retrofit_lang_decl (decl);
21066 DECL_PARM_INDEX (decl) = ++index;
21067 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21070 /* Add the new parameter to the list. */
21071 *tail = build_tree_list (parameter->default_argument, decl);
21072 tail = &TREE_CHAIN (*tail);
21074 /* Peek at the next token. */
21075 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21076 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21077 /* These are for Objective-C++ */
21078 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21079 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21080 /* The parameter-declaration-list is complete. */
21081 break;
21082 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21084 cp_token *token;
21086 /* Peek at the next token. */
21087 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21088 /* If it's an ellipsis, then the list is complete. */
21089 if (token->type == CPP_ELLIPSIS)
21090 break;
21091 /* Otherwise, there must be more parameters. Consume the
21092 `,'. */
21093 cp_lexer_consume_token (parser->lexer);
21094 /* When parsing something like:
21096 int i(float f, double d)
21098 we can tell after seeing the declaration for "f" that we
21099 are not looking at an initialization of a variable "i",
21100 but rather at the declaration of a function "i".
21102 Due to the fact that the parsing of template arguments
21103 (as specified to a template-id) requires backtracking we
21104 cannot use this technique when inside a template argument
21105 list. */
21106 if (!parser->in_template_argument_list_p
21107 && !parser->in_type_id_in_expr_p
21108 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21109 /* However, a parameter-declaration of the form
21110 "float(f)" (which is a valid declaration of a
21111 parameter "f") can also be interpreted as an
21112 expression (the conversion of "f" to "float"). */
21113 && !parenthesized_p)
21114 cp_parser_commit_to_tentative_parse (parser);
21116 else
21118 cp_parser_error (parser, "expected %<,%> or %<...%>");
21119 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21120 cp_parser_skip_to_closing_parenthesis (parser,
21121 /*recovering=*/true,
21122 /*or_comma=*/false,
21123 /*consume_paren=*/false);
21124 break;
21128 parser->in_unbraced_linkage_specification_p
21129 = saved_in_unbraced_linkage_specification_p;
21131 /* Reset implicit_template_scope if we are about to leave the function
21132 parameter list that introduced it. Note that for out-of-line member
21133 definitions, there will be one or more class scopes before we get to
21134 the template parameter scope. */
21136 if (cp_binding_level *its = parser->implicit_template_scope)
21137 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21139 while (maybe_its->kind == sk_class)
21140 maybe_its = maybe_its->level_chain;
21141 if (maybe_its == its)
21143 parser->implicit_template_parms = 0;
21144 parser->implicit_template_scope = 0;
21148 return parameters;
21151 /* Parse a parameter declaration.
21153 parameter-declaration:
21154 decl-specifier-seq ... [opt] declarator
21155 decl-specifier-seq declarator = assignment-expression
21156 decl-specifier-seq ... [opt] abstract-declarator [opt]
21157 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21159 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21160 declares a template parameter. (In that case, a non-nested `>'
21161 token encountered during the parsing of the assignment-expression
21162 is not interpreted as a greater-than operator.)
21164 Returns a representation of the parameter, or NULL if an error
21165 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21166 true iff the declarator is of the form "(p)". */
21168 static cp_parameter_declarator *
21169 cp_parser_parameter_declaration (cp_parser *parser,
21170 bool template_parm_p,
21171 bool *parenthesized_p)
21173 int declares_class_or_enum;
21174 cp_decl_specifier_seq decl_specifiers;
21175 cp_declarator *declarator;
21176 tree default_argument;
21177 cp_token *token = NULL, *declarator_token_start = NULL;
21178 const char *saved_message;
21179 bool template_parameter_pack_p = false;
21181 /* In a template parameter, `>' is not an operator.
21183 [temp.param]
21185 When parsing a default template-argument for a non-type
21186 template-parameter, the first non-nested `>' is taken as the end
21187 of the template parameter-list rather than a greater-than
21188 operator. */
21190 /* Type definitions may not appear in parameter types. */
21191 saved_message = parser->type_definition_forbidden_message;
21192 parser->type_definition_forbidden_message
21193 = G_("types may not be defined in parameter types");
21195 /* Parse the declaration-specifiers. */
21196 cp_parser_decl_specifier_seq (parser,
21197 CP_PARSER_FLAGS_NONE,
21198 &decl_specifiers,
21199 &declares_class_or_enum);
21201 /* Complain about missing 'typename' or other invalid type names. */
21202 if (!decl_specifiers.any_type_specifiers_p
21203 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21204 decl_specifiers.type = error_mark_node;
21206 /* If an error occurred, there's no reason to attempt to parse the
21207 rest of the declaration. */
21208 if (cp_parser_error_occurred (parser))
21210 parser->type_definition_forbidden_message = saved_message;
21211 return NULL;
21214 /* Peek at the next token. */
21215 token = cp_lexer_peek_token (parser->lexer);
21217 /* If the next token is a `)', `,', `=', `>', or `...', then there
21218 is no declarator. However, when variadic templates are enabled,
21219 there may be a declarator following `...'. */
21220 if (token->type == CPP_CLOSE_PAREN
21221 || token->type == CPP_COMMA
21222 || token->type == CPP_EQ
21223 || token->type == CPP_GREATER)
21225 declarator = NULL;
21226 if (parenthesized_p)
21227 *parenthesized_p = false;
21229 /* Otherwise, there should be a declarator. */
21230 else
21232 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21233 parser->default_arg_ok_p = false;
21235 /* After seeing a decl-specifier-seq, if the next token is not a
21236 "(", there is no possibility that the code is a valid
21237 expression. Therefore, if parsing tentatively, we commit at
21238 this point. */
21239 if (!parser->in_template_argument_list_p
21240 /* In an expression context, having seen:
21242 (int((char ...
21244 we cannot be sure whether we are looking at a
21245 function-type (taking a "char" as a parameter) or a cast
21246 of some object of type "char" to "int". */
21247 && !parser->in_type_id_in_expr_p
21248 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21249 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21250 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21251 cp_parser_commit_to_tentative_parse (parser);
21252 /* Parse the declarator. */
21253 declarator_token_start = token;
21254 declarator = cp_parser_declarator (parser,
21255 CP_PARSER_DECLARATOR_EITHER,
21256 /*ctor_dtor_or_conv_p=*/NULL,
21257 parenthesized_p,
21258 /*member_p=*/false,
21259 /*friend_p=*/false);
21260 parser->default_arg_ok_p = saved_default_arg_ok_p;
21261 /* After the declarator, allow more attributes. */
21262 decl_specifiers.attributes
21263 = chainon (decl_specifiers.attributes,
21264 cp_parser_attributes_opt (parser));
21266 /* If the declarator is a template parameter pack, remember that and
21267 clear the flag in the declarator itself so we don't get errors
21268 from grokdeclarator. */
21269 if (template_parm_p && declarator && declarator->parameter_pack_p)
21271 declarator->parameter_pack_p = false;
21272 template_parameter_pack_p = true;
21276 /* If the next token is an ellipsis, and we have not seen a declarator
21277 name, and if either the type of the declarator contains parameter
21278 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21279 for, eg, abbreviated integral type names), then we actually have a
21280 parameter pack expansion expression. Otherwise, leave the ellipsis
21281 for a C-style variadic function. */
21282 token = cp_lexer_peek_token (parser->lexer);
21283 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21285 tree type = decl_specifiers.type;
21287 if (type && DECL_P (type))
21288 type = TREE_TYPE (type);
21290 if (((type
21291 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21292 && (template_parm_p || uses_parameter_packs (type)))
21293 || (!type && template_parm_p))
21294 && declarator_can_be_parameter_pack (declarator))
21296 /* Consume the `...'. */
21297 cp_lexer_consume_token (parser->lexer);
21298 maybe_warn_variadic_templates ();
21300 /* Build a pack expansion type */
21301 if (template_parm_p)
21302 template_parameter_pack_p = true;
21303 else if (declarator)
21304 declarator->parameter_pack_p = true;
21305 else
21306 decl_specifiers.type = make_pack_expansion (type);
21310 /* The restriction on defining new types applies only to the type
21311 of the parameter, not to the default argument. */
21312 parser->type_definition_forbidden_message = saved_message;
21314 /* If the next token is `=', then process a default argument. */
21315 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21317 tree type = decl_specifiers.type;
21318 token = cp_lexer_peek_token (parser->lexer);
21319 /* If we are defining a class, then the tokens that make up the
21320 default argument must be saved and processed later. */
21321 if (!template_parm_p && at_class_scope_p ()
21322 && TYPE_BEING_DEFINED (current_class_type)
21323 && !LAMBDA_TYPE_P (current_class_type))
21324 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21326 // A constrained-type-specifier may declare a type template-parameter.
21327 else if (declares_constrained_type_template_parameter (type))
21328 default_argument
21329 = cp_parser_default_type_template_argument (parser);
21331 // A constrained-type-specifier may declare a template-template-parameter.
21332 else if (declares_constrained_template_template_parameter (type))
21333 default_argument
21334 = cp_parser_default_template_template_argument (parser);
21336 /* Outside of a class definition, we can just parse the
21337 assignment-expression. */
21338 else
21339 default_argument
21340 = cp_parser_default_argument (parser, template_parm_p);
21342 if (!parser->default_arg_ok_p)
21344 permerror (token->location,
21345 "default arguments are only "
21346 "permitted for function parameters");
21348 else if ((declarator && declarator->parameter_pack_p)
21349 || template_parameter_pack_p
21350 || (decl_specifiers.type
21351 && PACK_EXPANSION_P (decl_specifiers.type)))
21353 /* Find the name of the parameter pack. */
21354 cp_declarator *id_declarator = declarator;
21355 while (id_declarator && id_declarator->kind != cdk_id)
21356 id_declarator = id_declarator->declarator;
21358 if (id_declarator && id_declarator->kind == cdk_id)
21359 error_at (declarator_token_start->location,
21360 template_parm_p
21361 ? G_("template parameter pack %qD "
21362 "cannot have a default argument")
21363 : G_("parameter pack %qD cannot have "
21364 "a default argument"),
21365 id_declarator->u.id.unqualified_name);
21366 else
21367 error_at (declarator_token_start->location,
21368 template_parm_p
21369 ? G_("template parameter pack cannot have "
21370 "a default argument")
21371 : G_("parameter pack cannot have a "
21372 "default argument"));
21374 default_argument = NULL_TREE;
21377 else
21378 default_argument = NULL_TREE;
21380 return make_parameter_declarator (&decl_specifiers,
21381 declarator,
21382 default_argument,
21383 template_parameter_pack_p);
21386 /* Parse a default argument and return it.
21388 TEMPLATE_PARM_P is true if this is a default argument for a
21389 non-type template parameter. */
21390 static tree
21391 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21393 tree default_argument = NULL_TREE;
21394 bool saved_greater_than_is_operator_p;
21395 bool saved_local_variables_forbidden_p;
21396 bool non_constant_p, is_direct_init;
21398 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21399 set correctly. */
21400 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21401 parser->greater_than_is_operator_p = !template_parm_p;
21402 /* Local variable names (and the `this' keyword) may not
21403 appear in a default argument. */
21404 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21405 parser->local_variables_forbidden_p = true;
21406 /* Parse the assignment-expression. */
21407 if (template_parm_p)
21408 push_deferring_access_checks (dk_no_deferred);
21409 tree saved_class_ptr = NULL_TREE;
21410 tree saved_class_ref = NULL_TREE;
21411 /* The "this" pointer is not valid in a default argument. */
21412 if (cfun)
21414 saved_class_ptr = current_class_ptr;
21415 cp_function_chain->x_current_class_ptr = NULL_TREE;
21416 saved_class_ref = current_class_ref;
21417 cp_function_chain->x_current_class_ref = NULL_TREE;
21419 default_argument
21420 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21421 /* Restore the "this" pointer. */
21422 if (cfun)
21424 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21425 cp_function_chain->x_current_class_ref = saved_class_ref;
21427 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21428 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21429 if (template_parm_p)
21430 pop_deferring_access_checks ();
21431 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21432 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21434 return default_argument;
21437 /* Parse a function-body.
21439 function-body:
21440 compound_statement */
21442 static void
21443 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21445 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21446 ? BCS_TRY_BLOCK : BCS_NORMAL),
21447 true);
21450 /* Parse a ctor-initializer-opt followed by a function-body. Return
21451 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21452 is true we are parsing a function-try-block. */
21454 static bool
21455 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21456 bool in_function_try_block)
21458 tree body, list;
21459 bool ctor_initializer_p;
21460 const bool check_body_p =
21461 DECL_CONSTRUCTOR_P (current_function_decl)
21462 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21463 tree last = NULL;
21465 /* Begin the function body. */
21466 body = begin_function_body ();
21467 /* Parse the optional ctor-initializer. */
21468 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
21470 /* If we're parsing a constexpr constructor definition, we need
21471 to check that the constructor body is indeed empty. However,
21472 before we get to cp_parser_function_body lot of junk has been
21473 generated, so we can't just check that we have an empty block.
21474 Rather we take a snapshot of the outermost block, and check whether
21475 cp_parser_function_body changed its state. */
21476 if (check_body_p)
21478 list = cur_stmt_list;
21479 if (STATEMENT_LIST_TAIL (list))
21480 last = STATEMENT_LIST_TAIL (list)->stmt;
21482 /* Parse the function-body. */
21483 cp_parser_function_body (parser, in_function_try_block);
21484 if (check_body_p)
21485 check_constexpr_ctor_body (last, list, /*complain=*/true);
21486 /* Finish the function body. */
21487 finish_function_body (body);
21489 return ctor_initializer_p;
21492 /* Parse an initializer.
21494 initializer:
21495 = initializer-clause
21496 ( expression-list )
21498 Returns an expression representing the initializer. If no
21499 initializer is present, NULL_TREE is returned.
21501 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21502 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21503 set to TRUE if there is no initializer present. If there is an
21504 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21505 is set to true; otherwise it is set to false. */
21507 static tree
21508 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21509 bool* non_constant_p)
21511 cp_token *token;
21512 tree init;
21514 /* Peek at the next token. */
21515 token = cp_lexer_peek_token (parser->lexer);
21517 /* Let our caller know whether or not this initializer was
21518 parenthesized. */
21519 *is_direct_init = (token->type != CPP_EQ);
21520 /* Assume that the initializer is constant. */
21521 *non_constant_p = false;
21523 if (token->type == CPP_EQ)
21525 /* Consume the `='. */
21526 cp_lexer_consume_token (parser->lexer);
21527 /* Parse the initializer-clause. */
21528 init = cp_parser_initializer_clause (parser, non_constant_p);
21530 else if (token->type == CPP_OPEN_PAREN)
21532 vec<tree, va_gc> *vec;
21533 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21534 /*cast_p=*/false,
21535 /*allow_expansion_p=*/true,
21536 non_constant_p);
21537 if (vec == NULL)
21538 return error_mark_node;
21539 init = build_tree_list_vec (vec);
21540 release_tree_vector (vec);
21542 else if (token->type == CPP_OPEN_BRACE)
21544 cp_lexer_set_source_position (parser->lexer);
21545 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21546 init = cp_parser_braced_list (parser, non_constant_p);
21547 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21549 else
21551 /* Anything else is an error. */
21552 cp_parser_error (parser, "expected initializer");
21553 init = error_mark_node;
21556 if (check_for_bare_parameter_packs (init))
21557 init = error_mark_node;
21559 return init;
21562 /* Parse an initializer-clause.
21564 initializer-clause:
21565 assignment-expression
21566 braced-init-list
21568 Returns an expression representing the initializer.
21570 If the `assignment-expression' production is used the value
21571 returned is simply a representation for the expression.
21573 Otherwise, calls cp_parser_braced_list. */
21575 static cp_expr
21576 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21578 cp_expr initializer;
21580 /* Assume the expression is constant. */
21581 *non_constant_p = false;
21583 /* If it is not a `{', then we are looking at an
21584 assignment-expression. */
21585 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21587 initializer
21588 = cp_parser_constant_expression (parser,
21589 /*allow_non_constant_p=*/true,
21590 non_constant_p);
21592 else
21593 initializer = cp_parser_braced_list (parser, non_constant_p);
21595 return initializer;
21598 /* Parse a brace-enclosed initializer list.
21600 braced-init-list:
21601 { initializer-list , [opt] }
21604 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21605 the elements of the initializer-list (or NULL, if the last
21606 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21607 NULL_TREE. There is no way to detect whether or not the optional
21608 trailing `,' was provided. NON_CONSTANT_P is as for
21609 cp_parser_initializer. */
21611 static cp_expr
21612 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21614 tree initializer;
21615 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21617 /* Consume the `{' token. */
21618 cp_lexer_consume_token (parser->lexer);
21619 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21620 initializer = make_node (CONSTRUCTOR);
21621 /* If it's not a `}', then there is a non-trivial initializer. */
21622 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21624 /* Parse the initializer list. */
21625 CONSTRUCTOR_ELTS (initializer)
21626 = cp_parser_initializer_list (parser, non_constant_p);
21627 /* A trailing `,' token is allowed. */
21628 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21629 cp_lexer_consume_token (parser->lexer);
21631 else
21632 *non_constant_p = false;
21633 /* Now, there should be a trailing `}'. */
21634 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21635 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21636 TREE_TYPE (initializer) = init_list_type_node;
21638 cp_expr result (initializer);
21639 /* Build a location of the form:
21640 { ... }
21641 ^~~~~~~
21642 with caret==start at the open brace, finish at the close brace. */
21643 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21644 result.set_location (combined_loc);
21645 return result;
21648 /* Consume tokens up to, and including, the next non-nested closing `]'.
21649 Returns true iff we found a closing `]'. */
21651 static bool
21652 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21654 unsigned square_depth = 0;
21656 while (true)
21658 cp_token * token = cp_lexer_peek_token (parser->lexer);
21660 switch (token->type)
21662 case CPP_EOF:
21663 case CPP_PRAGMA_EOL:
21664 /* If we've run out of tokens, then there is no closing `]'. */
21665 return false;
21667 case CPP_OPEN_SQUARE:
21668 ++square_depth;
21669 break;
21671 case CPP_CLOSE_SQUARE:
21672 if (!square_depth--)
21674 cp_lexer_consume_token (parser->lexer);
21675 return true;
21677 break;
21679 default:
21680 break;
21683 /* Consume the token. */
21684 cp_lexer_consume_token (parser->lexer);
21688 /* Return true if we are looking at an array-designator, false otherwise. */
21690 static bool
21691 cp_parser_array_designator_p (cp_parser *parser)
21693 /* Consume the `['. */
21694 cp_lexer_consume_token (parser->lexer);
21696 cp_lexer_save_tokens (parser->lexer);
21698 /* Skip tokens until the next token is a closing square bracket.
21699 If we find the closing `]', and the next token is a `=', then
21700 we are looking at an array designator. */
21701 bool array_designator_p
21702 = (cp_parser_skip_to_closing_square_bracket (parser)
21703 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21705 /* Roll back the tokens we skipped. */
21706 cp_lexer_rollback_tokens (parser->lexer);
21708 return array_designator_p;
21711 /* Parse an initializer-list.
21713 initializer-list:
21714 initializer-clause ... [opt]
21715 initializer-list , initializer-clause ... [opt]
21717 GNU Extension:
21719 initializer-list:
21720 designation initializer-clause ...[opt]
21721 initializer-list , designation initializer-clause ...[opt]
21723 designation:
21724 . identifier =
21725 identifier :
21726 [ constant-expression ] =
21728 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21729 for the initializer. If the INDEX of the elt is non-NULL, it is the
21730 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21731 as for cp_parser_initializer. */
21733 static vec<constructor_elt, va_gc> *
21734 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21736 vec<constructor_elt, va_gc> *v = NULL;
21738 /* Assume all of the expressions are constant. */
21739 *non_constant_p = false;
21741 /* Parse the rest of the list. */
21742 while (true)
21744 cp_token *token;
21745 tree designator;
21746 tree initializer;
21747 bool clause_non_constant_p;
21749 /* If the next token is an identifier and the following one is a
21750 colon, we are looking at the GNU designated-initializer
21751 syntax. */
21752 if (cp_parser_allow_gnu_extensions_p (parser)
21753 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21754 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21756 /* Warn the user that they are using an extension. */
21757 pedwarn (input_location, OPT_Wpedantic,
21758 "ISO C++ does not allow designated initializers");
21759 /* Consume the identifier. */
21760 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21761 /* Consume the `:'. */
21762 cp_lexer_consume_token (parser->lexer);
21764 /* Also handle the C99 syntax, '. id ='. */
21765 else if (cp_parser_allow_gnu_extensions_p (parser)
21766 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21767 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21768 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21770 /* Warn the user that they are using an extension. */
21771 pedwarn (input_location, OPT_Wpedantic,
21772 "ISO C++ does not allow C99 designated initializers");
21773 /* Consume the `.'. */
21774 cp_lexer_consume_token (parser->lexer);
21775 /* Consume the identifier. */
21776 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21777 /* Consume the `='. */
21778 cp_lexer_consume_token (parser->lexer);
21780 /* Also handle C99 array designators, '[ const ] ='. */
21781 else if (cp_parser_allow_gnu_extensions_p (parser)
21782 && !c_dialect_objc ()
21783 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21785 /* In C++11, [ could start a lambda-introducer. */
21786 bool non_const = false;
21788 cp_parser_parse_tentatively (parser);
21790 if (!cp_parser_array_designator_p (parser))
21792 cp_parser_simulate_error (parser);
21793 designator = NULL_TREE;
21795 else
21797 designator = cp_parser_constant_expression (parser, true,
21798 &non_const);
21799 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21800 cp_parser_require (parser, CPP_EQ, RT_EQ);
21803 if (!cp_parser_parse_definitely (parser))
21804 designator = NULL_TREE;
21805 else if (non_const)
21806 require_potential_rvalue_constant_expression (designator);
21808 else
21809 designator = NULL_TREE;
21811 /* Parse the initializer. */
21812 initializer = cp_parser_initializer_clause (parser,
21813 &clause_non_constant_p);
21814 /* If any clause is non-constant, so is the entire initializer. */
21815 if (clause_non_constant_p)
21816 *non_constant_p = true;
21818 /* If we have an ellipsis, this is an initializer pack
21819 expansion. */
21820 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21822 /* Consume the `...'. */
21823 cp_lexer_consume_token (parser->lexer);
21825 /* Turn the initializer into an initializer expansion. */
21826 initializer = make_pack_expansion (initializer);
21829 /* Add it to the vector. */
21830 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21832 /* If the next token is not a comma, we have reached the end of
21833 the list. */
21834 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21835 break;
21837 /* Peek at the next token. */
21838 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21839 /* If the next token is a `}', then we're still done. An
21840 initializer-clause can have a trailing `,' after the
21841 initializer-list and before the closing `}'. */
21842 if (token->type == CPP_CLOSE_BRACE)
21843 break;
21845 /* Consume the `,' token. */
21846 cp_lexer_consume_token (parser->lexer);
21849 return v;
21852 /* Classes [gram.class] */
21854 /* Parse a class-name.
21856 class-name:
21857 identifier
21858 template-id
21860 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21861 to indicate that names looked up in dependent types should be
21862 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21863 keyword has been used to indicate that the name that appears next
21864 is a template. TAG_TYPE indicates the explicit tag given before
21865 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21866 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21867 is the class being defined in a class-head. If ENUM_OK is TRUE,
21868 enum-names are also accepted.
21870 Returns the TYPE_DECL representing the class. */
21872 static tree
21873 cp_parser_class_name (cp_parser *parser,
21874 bool typename_keyword_p,
21875 bool template_keyword_p,
21876 enum tag_types tag_type,
21877 bool check_dependency_p,
21878 bool class_head_p,
21879 bool is_declaration,
21880 bool enum_ok)
21882 tree decl;
21883 tree scope;
21884 bool typename_p;
21885 cp_token *token;
21886 tree identifier = NULL_TREE;
21888 /* All class-names start with an identifier. */
21889 token = cp_lexer_peek_token (parser->lexer);
21890 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21892 cp_parser_error (parser, "expected class-name");
21893 return error_mark_node;
21896 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21897 to a template-id, so we save it here. */
21898 scope = parser->scope;
21899 if (scope == error_mark_node)
21900 return error_mark_node;
21902 /* Any name names a type if we're following the `typename' keyword
21903 in a qualified name where the enclosing scope is type-dependent. */
21904 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21905 && dependent_type_p (scope));
21906 /* Handle the common case (an identifier, but not a template-id)
21907 efficiently. */
21908 if (token->type == CPP_NAME
21909 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21911 cp_token *identifier_token;
21912 bool ambiguous_p;
21914 /* Look for the identifier. */
21915 identifier_token = cp_lexer_peek_token (parser->lexer);
21916 ambiguous_p = identifier_token->error_reported;
21917 identifier = cp_parser_identifier (parser);
21918 /* If the next token isn't an identifier, we are certainly not
21919 looking at a class-name. */
21920 if (identifier == error_mark_node)
21921 decl = error_mark_node;
21922 /* If we know this is a type-name, there's no need to look it
21923 up. */
21924 else if (typename_p)
21925 decl = identifier;
21926 else
21928 tree ambiguous_decls;
21929 /* If we already know that this lookup is ambiguous, then
21930 we've already issued an error message; there's no reason
21931 to check again. */
21932 if (ambiguous_p)
21934 cp_parser_simulate_error (parser);
21935 return error_mark_node;
21937 /* If the next token is a `::', then the name must be a type
21938 name.
21940 [basic.lookup.qual]
21942 During the lookup for a name preceding the :: scope
21943 resolution operator, object, function, and enumerator
21944 names are ignored. */
21945 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21946 tag_type = scope_type;
21947 /* Look up the name. */
21948 decl = cp_parser_lookup_name (parser, identifier,
21949 tag_type,
21950 /*is_template=*/false,
21951 /*is_namespace=*/false,
21952 check_dependency_p,
21953 &ambiguous_decls,
21954 identifier_token->location);
21955 if (ambiguous_decls)
21957 if (cp_parser_parsing_tentatively (parser))
21958 cp_parser_simulate_error (parser);
21959 return error_mark_node;
21963 else
21965 /* Try a template-id. */
21966 decl = cp_parser_template_id (parser, template_keyword_p,
21967 check_dependency_p,
21968 tag_type,
21969 is_declaration);
21970 if (decl == error_mark_node)
21971 return error_mark_node;
21974 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21976 /* If this is a typename, create a TYPENAME_TYPE. */
21977 if (typename_p && decl != error_mark_node)
21979 decl = make_typename_type (scope, decl, typename_type,
21980 /*complain=*/tf_error);
21981 if (decl != error_mark_node)
21982 decl = TYPE_NAME (decl);
21985 decl = strip_using_decl (decl);
21987 /* Check to see that it is really the name of a class. */
21988 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21989 && identifier_p (TREE_OPERAND (decl, 0))
21990 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21991 /* Situations like this:
21993 template <typename T> struct A {
21994 typename T::template X<int>::I i;
21997 are problematic. Is `T::template X<int>' a class-name? The
21998 standard does not seem to be definitive, but there is no other
21999 valid interpretation of the following `::'. Therefore, those
22000 names are considered class-names. */
22002 decl = make_typename_type (scope, decl, tag_type, tf_error);
22003 if (decl != error_mark_node)
22004 decl = TYPE_NAME (decl);
22006 else if (TREE_CODE (decl) != TYPE_DECL
22007 || TREE_TYPE (decl) == error_mark_node
22008 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22009 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22010 /* In Objective-C 2.0, a classname followed by '.' starts a
22011 dot-syntax expression, and it's not a type-name. */
22012 || (c_dialect_objc ()
22013 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22014 && objc_is_class_name (decl)))
22015 decl = error_mark_node;
22017 if (decl == error_mark_node)
22018 cp_parser_error (parser, "expected class-name");
22019 else if (identifier && !parser->scope)
22020 maybe_note_name_used_in_class (identifier, decl);
22022 return decl;
22025 /* Parse a class-specifier.
22027 class-specifier:
22028 class-head { member-specification [opt] }
22030 Returns the TREE_TYPE representing the class. */
22032 static tree
22033 cp_parser_class_specifier_1 (cp_parser* parser)
22035 tree type;
22036 tree attributes = NULL_TREE;
22037 bool nested_name_specifier_p;
22038 unsigned saved_num_template_parameter_lists;
22039 bool saved_in_function_body;
22040 unsigned char in_statement;
22041 bool in_switch_statement_p;
22042 bool saved_in_unbraced_linkage_specification_p;
22043 tree old_scope = NULL_TREE;
22044 tree scope = NULL_TREE;
22045 cp_token *closing_brace;
22047 push_deferring_access_checks (dk_no_deferred);
22049 /* Parse the class-head. */
22050 type = cp_parser_class_head (parser,
22051 &nested_name_specifier_p);
22052 /* If the class-head was a semantic disaster, skip the entire body
22053 of the class. */
22054 if (!type)
22056 cp_parser_skip_to_end_of_block_or_statement (parser);
22057 pop_deferring_access_checks ();
22058 return error_mark_node;
22061 /* Look for the `{'. */
22062 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22064 pop_deferring_access_checks ();
22065 return error_mark_node;
22068 cp_ensure_no_omp_declare_simd (parser);
22069 cp_ensure_no_oacc_routine (parser);
22071 /* Issue an error message if type-definitions are forbidden here. */
22072 cp_parser_check_type_definition (parser);
22073 /* Remember that we are defining one more class. */
22074 ++parser->num_classes_being_defined;
22075 /* Inside the class, surrounding template-parameter-lists do not
22076 apply. */
22077 saved_num_template_parameter_lists
22078 = parser->num_template_parameter_lists;
22079 parser->num_template_parameter_lists = 0;
22080 /* We are not in a function body. */
22081 saved_in_function_body = parser->in_function_body;
22082 parser->in_function_body = false;
22083 /* Or in a loop. */
22084 in_statement = parser->in_statement;
22085 parser->in_statement = 0;
22086 /* Or in a switch. */
22087 in_switch_statement_p = parser->in_switch_statement_p;
22088 parser->in_switch_statement_p = false;
22089 /* We are not immediately inside an extern "lang" block. */
22090 saved_in_unbraced_linkage_specification_p
22091 = parser->in_unbraced_linkage_specification_p;
22092 parser->in_unbraced_linkage_specification_p = false;
22094 // Associate constraints with the type.
22095 if (flag_concepts)
22096 type = associate_classtype_constraints (type);
22098 /* Start the class. */
22099 if (nested_name_specifier_p)
22101 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22102 old_scope = push_inner_scope (scope);
22104 type = begin_class_definition (type);
22106 if (type == error_mark_node)
22107 /* If the type is erroneous, skip the entire body of the class. */
22108 cp_parser_skip_to_closing_brace (parser);
22109 else
22110 /* Parse the member-specification. */
22111 cp_parser_member_specification_opt (parser);
22113 /* Look for the trailing `}'. */
22114 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22115 /* Look for trailing attributes to apply to this class. */
22116 if (cp_parser_allow_gnu_extensions_p (parser))
22117 attributes = cp_parser_gnu_attributes_opt (parser);
22118 if (type != error_mark_node)
22119 type = finish_struct (type, attributes);
22120 if (nested_name_specifier_p)
22121 pop_inner_scope (old_scope, scope);
22123 /* We've finished a type definition. Check for the common syntax
22124 error of forgetting a semicolon after the definition. We need to
22125 be careful, as we can't just check for not-a-semicolon and be done
22126 with it; the user might have typed:
22128 class X { } c = ...;
22129 class X { } *p = ...;
22131 and so forth. Instead, enumerate all the possible tokens that
22132 might follow this production; if we don't see one of them, then
22133 complain and silently insert the semicolon. */
22135 cp_token *token = cp_lexer_peek_token (parser->lexer);
22136 bool want_semicolon = true;
22138 if (cp_next_tokens_can_be_std_attribute_p (parser))
22139 /* Don't try to parse c++11 attributes here. As per the
22140 grammar, that should be a task for
22141 cp_parser_decl_specifier_seq. */
22142 want_semicolon = false;
22144 switch (token->type)
22146 case CPP_NAME:
22147 case CPP_SEMICOLON:
22148 case CPP_MULT:
22149 case CPP_AND:
22150 case CPP_OPEN_PAREN:
22151 case CPP_CLOSE_PAREN:
22152 case CPP_COMMA:
22153 want_semicolon = false;
22154 break;
22156 /* While it's legal for type qualifiers and storage class
22157 specifiers to follow type definitions in the grammar, only
22158 compiler testsuites contain code like that. Assume that if
22159 we see such code, then what we're really seeing is a case
22160 like:
22162 class X { }
22163 const <type> var = ...;
22167 class Y { }
22168 static <type> func (...) ...
22170 i.e. the qualifier or specifier applies to the next
22171 declaration. To do so, however, we need to look ahead one
22172 more token to see if *that* token is a type specifier.
22174 This code could be improved to handle:
22176 class Z { }
22177 static const <type> var = ...; */
22178 case CPP_KEYWORD:
22179 if (keyword_is_decl_specifier (token->keyword))
22181 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22183 /* Handling user-defined types here would be nice, but very
22184 tricky. */
22185 want_semicolon
22186 = (lookahead->type == CPP_KEYWORD
22187 && keyword_begins_type_specifier (lookahead->keyword));
22189 break;
22190 default:
22191 break;
22194 /* If we don't have a type, then something is very wrong and we
22195 shouldn't try to do anything clever. Likewise for not seeing the
22196 closing brace. */
22197 if (closing_brace && TYPE_P (type) && want_semicolon)
22199 /* Locate the closing brace. */
22200 cp_token_position prev
22201 = cp_lexer_previous_token_position (parser->lexer);
22202 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22203 location_t loc = prev_token->location;
22205 /* We want to suggest insertion of a ';' immediately *after* the
22206 closing brace, so, if we can, offset the location by 1 column. */
22207 location_t next_loc = loc;
22208 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22209 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22211 rich_location richloc (line_table, next_loc);
22213 /* If we successfully offset the location, suggest the fix-it. */
22214 if (next_loc != loc)
22215 richloc.add_fixit_insert_before (next_loc, ";");
22217 if (CLASSTYPE_DECLARED_CLASS (type))
22218 error_at_rich_loc (&richloc,
22219 "expected %<;%> after class definition");
22220 else if (TREE_CODE (type) == RECORD_TYPE)
22221 error_at_rich_loc (&richloc,
22222 "expected %<;%> after struct definition");
22223 else if (TREE_CODE (type) == UNION_TYPE)
22224 error_at_rich_loc (&richloc,
22225 "expected %<;%> after union definition");
22226 else
22227 gcc_unreachable ();
22229 /* Unget one token and smash it to look as though we encountered
22230 a semicolon in the input stream. */
22231 cp_lexer_set_token_position (parser->lexer, prev);
22232 token = cp_lexer_peek_token (parser->lexer);
22233 token->type = CPP_SEMICOLON;
22234 token->keyword = RID_MAX;
22238 /* If this class is not itself within the scope of another class,
22239 then we need to parse the bodies of all of the queued function
22240 definitions. Note that the queued functions defined in a class
22241 are not always processed immediately following the
22242 class-specifier for that class. Consider:
22244 struct A {
22245 struct B { void f() { sizeof (A); } };
22248 If `f' were processed before the processing of `A' were
22249 completed, there would be no way to compute the size of `A'.
22250 Note that the nesting we are interested in here is lexical --
22251 not the semantic nesting given by TYPE_CONTEXT. In particular,
22252 for:
22254 struct A { struct B; };
22255 struct A::B { void f() { } };
22257 there is no need to delay the parsing of `A::B::f'. */
22258 if (--parser->num_classes_being_defined == 0)
22260 tree decl;
22261 tree class_type = NULL_TREE;
22262 tree pushed_scope = NULL_TREE;
22263 unsigned ix;
22264 cp_default_arg_entry *e;
22265 tree save_ccp, save_ccr;
22267 /* In a first pass, parse default arguments to the functions.
22268 Then, in a second pass, parse the bodies of the functions.
22269 This two-phased approach handles cases like:
22271 struct S {
22272 void f() { g(); }
22273 void g(int i = 3);
22277 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22279 decl = e->decl;
22280 /* If there are default arguments that have not yet been processed,
22281 take care of them now. */
22282 if (class_type != e->class_type)
22284 if (pushed_scope)
22285 pop_scope (pushed_scope);
22286 class_type = e->class_type;
22287 pushed_scope = push_scope (class_type);
22289 /* Make sure that any template parameters are in scope. */
22290 maybe_begin_member_template_processing (decl);
22291 /* Parse the default argument expressions. */
22292 cp_parser_late_parsing_default_args (parser, decl);
22293 /* Remove any template parameters from the symbol table. */
22294 maybe_end_member_template_processing ();
22296 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22297 /* Now parse any NSDMIs. */
22298 save_ccp = current_class_ptr;
22299 save_ccr = current_class_ref;
22300 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22302 if (class_type != DECL_CONTEXT (decl))
22304 if (pushed_scope)
22305 pop_scope (pushed_scope);
22306 class_type = DECL_CONTEXT (decl);
22307 pushed_scope = push_scope (class_type);
22309 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22310 cp_parser_late_parsing_nsdmi (parser, decl);
22312 vec_safe_truncate (unparsed_nsdmis, 0);
22313 current_class_ptr = save_ccp;
22314 current_class_ref = save_ccr;
22315 if (pushed_scope)
22316 pop_scope (pushed_scope);
22318 /* Now do some post-NSDMI bookkeeping. */
22319 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22320 after_nsdmi_defaulted_late_checks (class_type);
22321 vec_safe_truncate (unparsed_classes, 0);
22322 after_nsdmi_defaulted_late_checks (type);
22324 /* Now parse the body of the functions. */
22325 if (flag_openmp)
22327 /* OpenMP UDRs need to be parsed before all other functions. */
22328 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22329 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22330 cp_parser_late_parsing_for_member (parser, decl);
22331 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22332 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22333 cp_parser_late_parsing_for_member (parser, decl);
22335 else
22336 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22337 cp_parser_late_parsing_for_member (parser, decl);
22338 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22340 else
22341 vec_safe_push (unparsed_classes, type);
22343 /* Put back any saved access checks. */
22344 pop_deferring_access_checks ();
22346 /* Restore saved state. */
22347 parser->in_switch_statement_p = in_switch_statement_p;
22348 parser->in_statement = in_statement;
22349 parser->in_function_body = saved_in_function_body;
22350 parser->num_template_parameter_lists
22351 = saved_num_template_parameter_lists;
22352 parser->in_unbraced_linkage_specification_p
22353 = saved_in_unbraced_linkage_specification_p;
22355 return type;
22358 static tree
22359 cp_parser_class_specifier (cp_parser* parser)
22361 tree ret;
22362 timevar_push (TV_PARSE_STRUCT);
22363 ret = cp_parser_class_specifier_1 (parser);
22364 timevar_pop (TV_PARSE_STRUCT);
22365 return ret;
22368 /* Parse a class-head.
22370 class-head:
22371 class-key identifier [opt] base-clause [opt]
22372 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22373 class-key nested-name-specifier [opt] template-id
22374 base-clause [opt]
22376 class-virt-specifier:
22377 final
22379 GNU Extensions:
22380 class-key attributes identifier [opt] base-clause [opt]
22381 class-key attributes nested-name-specifier identifier base-clause [opt]
22382 class-key attributes nested-name-specifier [opt] template-id
22383 base-clause [opt]
22385 Upon return BASES is initialized to the list of base classes (or
22386 NULL, if there are none) in the same form returned by
22387 cp_parser_base_clause.
22389 Returns the TYPE of the indicated class. Sets
22390 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22391 involving a nested-name-specifier was used, and FALSE otherwise.
22393 Returns error_mark_node if this is not a class-head.
22395 Returns NULL_TREE if the class-head is syntactically valid, but
22396 semantically invalid in a way that means we should skip the entire
22397 body of the class. */
22399 static tree
22400 cp_parser_class_head (cp_parser* parser,
22401 bool* nested_name_specifier_p)
22403 tree nested_name_specifier;
22404 enum tag_types class_key;
22405 tree id = NULL_TREE;
22406 tree type = NULL_TREE;
22407 tree attributes;
22408 tree bases;
22409 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22410 bool template_id_p = false;
22411 bool qualified_p = false;
22412 bool invalid_nested_name_p = false;
22413 bool invalid_explicit_specialization_p = false;
22414 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22415 tree pushed_scope = NULL_TREE;
22416 unsigned num_templates;
22417 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22418 /* Assume no nested-name-specifier will be present. */
22419 *nested_name_specifier_p = false;
22420 /* Assume no template parameter lists will be used in defining the
22421 type. */
22422 num_templates = 0;
22423 parser->colon_corrects_to_scope_p = false;
22425 /* Look for the class-key. */
22426 class_key = cp_parser_class_key (parser);
22427 if (class_key == none_type)
22428 return error_mark_node;
22430 location_t class_head_start_location = input_location;
22432 /* Parse the attributes. */
22433 attributes = cp_parser_attributes_opt (parser);
22435 /* If the next token is `::', that is invalid -- but sometimes
22436 people do try to write:
22438 struct ::S {};
22440 Handle this gracefully by accepting the extra qualifier, and then
22441 issuing an error about it later if this really is a
22442 class-head. If it turns out just to be an elaborated type
22443 specifier, remain silent. */
22444 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22445 qualified_p = true;
22447 push_deferring_access_checks (dk_no_check);
22449 /* Determine the name of the class. Begin by looking for an
22450 optional nested-name-specifier. */
22451 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22452 nested_name_specifier
22453 = cp_parser_nested_name_specifier_opt (parser,
22454 /*typename_keyword_p=*/false,
22455 /*check_dependency_p=*/false,
22456 /*type_p=*/true,
22457 /*is_declaration=*/false);
22458 /* If there was a nested-name-specifier, then there *must* be an
22459 identifier. */
22460 if (nested_name_specifier)
22462 type_start_token = cp_lexer_peek_token (parser->lexer);
22463 /* Although the grammar says `identifier', it really means
22464 `class-name' or `template-name'. You are only allowed to
22465 define a class that has already been declared with this
22466 syntax.
22468 The proposed resolution for Core Issue 180 says that wherever
22469 you see `class T::X' you should treat `X' as a type-name.
22471 It is OK to define an inaccessible class; for example:
22473 class A { class B; };
22474 class A::B {};
22476 We do not know if we will see a class-name, or a
22477 template-name. We look for a class-name first, in case the
22478 class-name is a template-id; if we looked for the
22479 template-name first we would stop after the template-name. */
22480 cp_parser_parse_tentatively (parser);
22481 type = cp_parser_class_name (parser,
22482 /*typename_keyword_p=*/false,
22483 /*template_keyword_p=*/false,
22484 class_type,
22485 /*check_dependency_p=*/false,
22486 /*class_head_p=*/true,
22487 /*is_declaration=*/false);
22488 /* If that didn't work, ignore the nested-name-specifier. */
22489 if (!cp_parser_parse_definitely (parser))
22491 invalid_nested_name_p = true;
22492 type_start_token = cp_lexer_peek_token (parser->lexer);
22493 id = cp_parser_identifier (parser);
22494 if (id == error_mark_node)
22495 id = NULL_TREE;
22497 /* If we could not find a corresponding TYPE, treat this
22498 declaration like an unqualified declaration. */
22499 if (type == error_mark_node)
22500 nested_name_specifier = NULL_TREE;
22501 /* Otherwise, count the number of templates used in TYPE and its
22502 containing scopes. */
22503 else
22505 tree scope;
22507 for (scope = TREE_TYPE (type);
22508 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22509 scope = get_containing_scope (scope))
22510 if (TYPE_P (scope)
22511 && CLASS_TYPE_P (scope)
22512 && CLASSTYPE_TEMPLATE_INFO (scope)
22513 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22514 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22515 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22516 ++num_templates;
22519 /* Otherwise, the identifier is optional. */
22520 else
22522 /* We don't know whether what comes next is a template-id,
22523 an identifier, or nothing at all. */
22524 cp_parser_parse_tentatively (parser);
22525 /* Check for a template-id. */
22526 type_start_token = cp_lexer_peek_token (parser->lexer);
22527 id = cp_parser_template_id (parser,
22528 /*template_keyword_p=*/false,
22529 /*check_dependency_p=*/true,
22530 class_key,
22531 /*is_declaration=*/true);
22532 /* If that didn't work, it could still be an identifier. */
22533 if (!cp_parser_parse_definitely (parser))
22535 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22537 type_start_token = cp_lexer_peek_token (parser->lexer);
22538 id = cp_parser_identifier (parser);
22540 else
22541 id = NULL_TREE;
22543 else
22545 template_id_p = true;
22546 ++num_templates;
22550 pop_deferring_access_checks ();
22552 if (id)
22554 cp_parser_check_for_invalid_template_id (parser, id,
22555 class_key,
22556 type_start_token->location);
22558 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22560 /* If it's not a `:' or a `{' then we can't really be looking at a
22561 class-head, since a class-head only appears as part of a
22562 class-specifier. We have to detect this situation before calling
22563 xref_tag, since that has irreversible side-effects. */
22564 if (!cp_parser_next_token_starts_class_definition_p (parser))
22566 cp_parser_error (parser, "expected %<{%> or %<:%>");
22567 type = error_mark_node;
22568 goto out;
22571 /* At this point, we're going ahead with the class-specifier, even
22572 if some other problem occurs. */
22573 cp_parser_commit_to_tentative_parse (parser);
22574 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22576 cp_parser_error (parser,
22577 "cannot specify %<override%> for a class");
22578 type = error_mark_node;
22579 goto out;
22581 /* Issue the error about the overly-qualified name now. */
22582 if (qualified_p)
22584 cp_parser_error (parser,
22585 "global qualification of class name is invalid");
22586 type = error_mark_node;
22587 goto out;
22589 else if (invalid_nested_name_p)
22591 cp_parser_error (parser,
22592 "qualified name does not name a class");
22593 type = error_mark_node;
22594 goto out;
22596 else if (nested_name_specifier)
22598 tree scope;
22600 /* Reject typedef-names in class heads. */
22601 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22603 error_at (type_start_token->location,
22604 "invalid class name in declaration of %qD",
22605 type);
22606 type = NULL_TREE;
22607 goto done;
22610 /* Figure out in what scope the declaration is being placed. */
22611 scope = current_scope ();
22612 /* If that scope does not contain the scope in which the
22613 class was originally declared, the program is invalid. */
22614 if (scope && !is_ancestor (scope, nested_name_specifier))
22616 if (at_namespace_scope_p ())
22617 error_at (type_start_token->location,
22618 "declaration of %qD in namespace %qD which does not "
22619 "enclose %qD",
22620 type, scope, nested_name_specifier);
22621 else
22622 error_at (type_start_token->location,
22623 "declaration of %qD in %qD which does not enclose %qD",
22624 type, scope, nested_name_specifier);
22625 type = NULL_TREE;
22626 goto done;
22628 /* [dcl.meaning]
22630 A declarator-id shall not be qualified except for the
22631 definition of a ... nested class outside of its class
22632 ... [or] the definition or explicit instantiation of a
22633 class member of a namespace outside of its namespace. */
22634 if (scope == nested_name_specifier)
22636 permerror (nested_name_specifier_token_start->location,
22637 "extra qualification not allowed");
22638 nested_name_specifier = NULL_TREE;
22639 num_templates = 0;
22642 /* An explicit-specialization must be preceded by "template <>". If
22643 it is not, try to recover gracefully. */
22644 if (at_namespace_scope_p ()
22645 && parser->num_template_parameter_lists == 0
22646 && !processing_template_parmlist
22647 && template_id_p)
22649 /* Build a location of this form:
22650 struct typename <ARGS>
22651 ^~~~~~~~~~~~~~~~~~~~~~
22652 with caret==start at the start token, and
22653 finishing at the end of the type. */
22654 location_t reported_loc
22655 = make_location (class_head_start_location,
22656 class_head_start_location,
22657 get_finish (type_start_token->location));
22658 rich_location richloc (line_table, reported_loc);
22659 richloc.add_fixit_insert_before (class_head_start_location,
22660 "template <> ");
22661 error_at_rich_loc
22662 (&richloc,
22663 "an explicit specialization must be preceded by %<template <>%>");
22664 invalid_explicit_specialization_p = true;
22665 /* Take the same action that would have been taken by
22666 cp_parser_explicit_specialization. */
22667 ++parser->num_template_parameter_lists;
22668 begin_specialization ();
22670 /* There must be no "return" statements between this point and the
22671 end of this function; set "type "to the correct return value and
22672 use "goto done;" to return. */
22673 /* Make sure that the right number of template parameters were
22674 present. */
22675 if (!cp_parser_check_template_parameters (parser, num_templates,
22676 type_start_token->location,
22677 /*declarator=*/NULL))
22679 /* If something went wrong, there is no point in even trying to
22680 process the class-definition. */
22681 type = NULL_TREE;
22682 goto done;
22685 /* Look up the type. */
22686 if (template_id_p)
22688 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22689 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22690 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22692 error_at (type_start_token->location,
22693 "function template %qD redeclared as a class template", id);
22694 type = error_mark_node;
22696 else
22698 type = TREE_TYPE (id);
22699 type = maybe_process_partial_specialization (type);
22701 /* Check the scope while we still know whether or not we had a
22702 nested-name-specifier. */
22703 if (type != error_mark_node)
22704 check_unqualified_spec_or_inst (type, type_start_token->location);
22706 if (nested_name_specifier)
22707 pushed_scope = push_scope (nested_name_specifier);
22709 else if (nested_name_specifier)
22711 tree class_type;
22713 /* Given:
22715 template <typename T> struct S { struct T };
22716 template <typename T> struct S<T>::T { };
22718 we will get a TYPENAME_TYPE when processing the definition of
22719 `S::T'. We need to resolve it to the actual type before we
22720 try to define it. */
22721 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22723 class_type = resolve_typename_type (TREE_TYPE (type),
22724 /*only_current_p=*/false);
22725 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22726 type = TYPE_NAME (class_type);
22727 else
22729 cp_parser_error (parser, "could not resolve typename type");
22730 type = error_mark_node;
22734 if (maybe_process_partial_specialization (TREE_TYPE (type))
22735 == error_mark_node)
22737 type = NULL_TREE;
22738 goto done;
22741 class_type = current_class_type;
22742 /* Enter the scope indicated by the nested-name-specifier. */
22743 pushed_scope = push_scope (nested_name_specifier);
22744 /* Get the canonical version of this type. */
22745 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22746 /* Call push_template_decl if it seems like we should be defining a
22747 template either from the template headers or the type we're
22748 defining, so that we diagnose both extra and missing headers. */
22749 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22750 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22751 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22753 type = push_template_decl (type);
22754 if (type == error_mark_node)
22756 type = NULL_TREE;
22757 goto done;
22761 type = TREE_TYPE (type);
22762 *nested_name_specifier_p = true;
22764 else /* The name is not a nested name. */
22766 /* If the class was unnamed, create a dummy name. */
22767 if (!id)
22768 id = make_anon_name ();
22769 tag_scope tag_scope = (parser->in_type_id_in_expr_p
22770 ? ts_within_enclosing_non_class
22771 : ts_current);
22772 type = xref_tag (class_key, id, tag_scope,
22773 parser->num_template_parameter_lists);
22776 /* Indicate whether this class was declared as a `class' or as a
22777 `struct'. */
22778 if (TREE_CODE (type) == RECORD_TYPE)
22779 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22780 cp_parser_check_class_key (class_key, type);
22782 /* If this type was already complete, and we see another definition,
22783 that's an error. */
22784 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22786 error_at (type_start_token->location, "redefinition of %q#T",
22787 type);
22788 inform (location_of (type), "previous definition of %q#T",
22789 type);
22790 type = NULL_TREE;
22791 goto done;
22793 else if (type == error_mark_node)
22794 type = NULL_TREE;
22796 if (type)
22798 /* Apply attributes now, before any use of the class as a template
22799 argument in its base list. */
22800 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22801 fixup_attribute_variants (type);
22804 /* We will have entered the scope containing the class; the names of
22805 base classes should be looked up in that context. For example:
22807 struct A { struct B {}; struct C; };
22808 struct A::C : B {};
22810 is valid. */
22812 /* Get the list of base-classes, if there is one. */
22813 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22815 /* PR59482: enter the class scope so that base-specifiers are looked
22816 up correctly. */
22817 if (type)
22818 pushclass (type);
22819 bases = cp_parser_base_clause (parser);
22820 /* PR59482: get out of the previously pushed class scope so that the
22821 subsequent pops pop the right thing. */
22822 if (type)
22823 popclass ();
22825 else
22826 bases = NULL_TREE;
22828 /* If we're really defining a class, process the base classes.
22829 If they're invalid, fail. */
22830 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22831 xref_basetypes (type, bases);
22833 done:
22834 /* Leave the scope given by the nested-name-specifier. We will
22835 enter the class scope itself while processing the members. */
22836 if (pushed_scope)
22837 pop_scope (pushed_scope);
22839 if (invalid_explicit_specialization_p)
22841 end_specialization ();
22842 --parser->num_template_parameter_lists;
22845 if (type)
22846 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22847 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22848 CLASSTYPE_FINAL (type) = 1;
22849 out:
22850 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22851 return type;
22854 /* Parse a class-key.
22856 class-key:
22857 class
22858 struct
22859 union
22861 Returns the kind of class-key specified, or none_type to indicate
22862 error. */
22864 static enum tag_types
22865 cp_parser_class_key (cp_parser* parser)
22867 cp_token *token;
22868 enum tag_types tag_type;
22870 /* Look for the class-key. */
22871 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22872 if (!token)
22873 return none_type;
22875 /* Check to see if the TOKEN is a class-key. */
22876 tag_type = cp_parser_token_is_class_key (token);
22877 if (!tag_type)
22878 cp_parser_error (parser, "expected class-key");
22879 return tag_type;
22882 /* Parse a type-parameter-key.
22884 type-parameter-key:
22885 class
22886 typename
22889 static void
22890 cp_parser_type_parameter_key (cp_parser* parser)
22892 /* Look for the type-parameter-key. */
22893 enum tag_types tag_type = none_type;
22894 cp_token *token = cp_lexer_peek_token (parser->lexer);
22895 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22897 cp_lexer_consume_token (parser->lexer);
22898 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22899 /* typename is not allowed in a template template parameter
22900 by the standard until C++1Z. */
22901 pedwarn (token->location, OPT_Wpedantic,
22902 "ISO C++ forbids typename key in template template parameter;"
22903 " use -std=c++1z or -std=gnu++1z");
22905 else
22906 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22908 return;
22911 /* Parse an (optional) member-specification.
22913 member-specification:
22914 member-declaration member-specification [opt]
22915 access-specifier : member-specification [opt] */
22917 static void
22918 cp_parser_member_specification_opt (cp_parser* parser)
22920 while (true)
22922 cp_token *token;
22923 enum rid keyword;
22925 /* Peek at the next token. */
22926 token = cp_lexer_peek_token (parser->lexer);
22927 /* If it's a `}', or EOF then we've seen all the members. */
22928 if (token->type == CPP_CLOSE_BRACE
22929 || token->type == CPP_EOF
22930 || token->type == CPP_PRAGMA_EOL)
22931 break;
22933 /* See if this token is a keyword. */
22934 keyword = token->keyword;
22935 switch (keyword)
22937 case RID_PUBLIC:
22938 case RID_PROTECTED:
22939 case RID_PRIVATE:
22940 /* Consume the access-specifier. */
22941 cp_lexer_consume_token (parser->lexer);
22942 /* Remember which access-specifier is active. */
22943 current_access_specifier = token->u.value;
22944 /* Look for the `:'. */
22945 cp_parser_require (parser, CPP_COLON, RT_COLON);
22946 break;
22948 default:
22949 /* Accept #pragmas at class scope. */
22950 if (token->type == CPP_PRAGMA)
22952 cp_parser_pragma (parser, pragma_member, NULL);
22953 break;
22956 /* Otherwise, the next construction must be a
22957 member-declaration. */
22958 cp_parser_member_declaration (parser);
22963 /* Parse a member-declaration.
22965 member-declaration:
22966 decl-specifier-seq [opt] member-declarator-list [opt] ;
22967 function-definition ; [opt]
22968 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22969 using-declaration
22970 template-declaration
22971 alias-declaration
22973 member-declarator-list:
22974 member-declarator
22975 member-declarator-list , member-declarator
22977 member-declarator:
22978 declarator pure-specifier [opt]
22979 declarator constant-initializer [opt]
22980 identifier [opt] : constant-expression
22982 GNU Extensions:
22984 member-declaration:
22985 __extension__ member-declaration
22987 member-declarator:
22988 declarator attributes [opt] pure-specifier [opt]
22989 declarator attributes [opt] constant-initializer [opt]
22990 identifier [opt] attributes [opt] : constant-expression
22992 C++0x Extensions:
22994 member-declaration:
22995 static_assert-declaration */
22997 static void
22998 cp_parser_member_declaration (cp_parser* parser)
23000 cp_decl_specifier_seq decl_specifiers;
23001 tree prefix_attributes;
23002 tree decl;
23003 int declares_class_or_enum;
23004 bool friend_p;
23005 cp_token *token = NULL;
23006 cp_token *decl_spec_token_start = NULL;
23007 cp_token *initializer_token_start = NULL;
23008 int saved_pedantic;
23009 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23011 /* Check for the `__extension__' keyword. */
23012 if (cp_parser_extension_opt (parser, &saved_pedantic))
23014 /* Recurse. */
23015 cp_parser_member_declaration (parser);
23016 /* Restore the old value of the PEDANTIC flag. */
23017 pedantic = saved_pedantic;
23019 return;
23022 /* Check for a template-declaration. */
23023 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23025 /* An explicit specialization here is an error condition, and we
23026 expect the specialization handler to detect and report this. */
23027 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23028 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23029 cp_parser_explicit_specialization (parser);
23030 else
23031 cp_parser_template_declaration (parser, /*member_p=*/true);
23033 return;
23035 /* Check for a template introduction. */
23036 else if (cp_parser_template_declaration_after_export (parser, true))
23037 return;
23039 /* Check for a using-declaration. */
23040 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23042 if (cxx_dialect < cxx11)
23044 /* Parse the using-declaration. */
23045 cp_parser_using_declaration (parser,
23046 /*access_declaration_p=*/false);
23047 return;
23049 else
23051 tree decl;
23052 bool alias_decl_expected;
23053 cp_parser_parse_tentatively (parser);
23054 decl = cp_parser_alias_declaration (parser);
23055 /* Note that if we actually see the '=' token after the
23056 identifier, cp_parser_alias_declaration commits the
23057 tentative parse. In that case, we really expect an
23058 alias-declaration. Otherwise, we expect a using
23059 declaration. */
23060 alias_decl_expected =
23061 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23062 cp_parser_parse_definitely (parser);
23064 if (alias_decl_expected)
23065 finish_member_declaration (decl);
23066 else
23067 cp_parser_using_declaration (parser,
23068 /*access_declaration_p=*/false);
23069 return;
23073 /* Check for @defs. */
23074 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23076 tree ivar, member;
23077 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23078 ivar = ivar_chains;
23079 while (ivar)
23081 member = ivar;
23082 ivar = TREE_CHAIN (member);
23083 TREE_CHAIN (member) = NULL_TREE;
23084 finish_member_declaration (member);
23086 return;
23089 /* If the next token is `static_assert' we have a static assertion. */
23090 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23092 cp_parser_static_assert (parser, /*member_p=*/true);
23093 return;
23096 parser->colon_corrects_to_scope_p = false;
23098 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23099 goto out;
23101 /* Parse the decl-specifier-seq. */
23102 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23103 cp_parser_decl_specifier_seq (parser,
23104 CP_PARSER_FLAGS_OPTIONAL,
23105 &decl_specifiers,
23106 &declares_class_or_enum);
23107 /* Check for an invalid type-name. */
23108 if (!decl_specifiers.any_type_specifiers_p
23109 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23110 goto out;
23111 /* If there is no declarator, then the decl-specifier-seq should
23112 specify a type. */
23113 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23115 /* If there was no decl-specifier-seq, and the next token is a
23116 `;', then we have something like:
23118 struct S { ; };
23120 [class.mem]
23122 Each member-declaration shall declare at least one member
23123 name of the class. */
23124 if (!decl_specifiers.any_specifiers_p)
23126 cp_token *token = cp_lexer_peek_token (parser->lexer);
23127 if (!in_system_header_at (token->location))
23129 gcc_rich_location richloc (token->location);
23130 richloc.add_fixit_remove ();
23131 pedwarn_at_rich_loc (&richloc, OPT_Wpedantic, "extra %<;%>");
23134 else
23136 tree type;
23138 /* See if this declaration is a friend. */
23139 friend_p = cp_parser_friend_p (&decl_specifiers);
23140 /* If there were decl-specifiers, check to see if there was
23141 a class-declaration. */
23142 type = check_tag_decl (&decl_specifiers,
23143 /*explicit_type_instantiation_p=*/false);
23144 /* Nested classes have already been added to the class, but
23145 a `friend' needs to be explicitly registered. */
23146 if (friend_p)
23148 /* If the `friend' keyword was present, the friend must
23149 be introduced with a class-key. */
23150 if (!declares_class_or_enum && cxx_dialect < cxx11)
23151 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23152 "in C++03 a class-key must be used "
23153 "when declaring a friend");
23154 /* In this case:
23156 template <typename T> struct A {
23157 friend struct A<T>::B;
23160 A<T>::B will be represented by a TYPENAME_TYPE, and
23161 therefore not recognized by check_tag_decl. */
23162 if (!type)
23164 type = decl_specifiers.type;
23165 if (type && TREE_CODE (type) == TYPE_DECL)
23166 type = TREE_TYPE (type);
23168 if (!type || !TYPE_P (type))
23169 error_at (decl_spec_token_start->location,
23170 "friend declaration does not name a class or "
23171 "function");
23172 else
23173 make_friend_class (current_class_type, type,
23174 /*complain=*/true);
23176 /* If there is no TYPE, an error message will already have
23177 been issued. */
23178 else if (!type || type == error_mark_node)
23180 /* An anonymous aggregate has to be handled specially; such
23181 a declaration really declares a data member (with a
23182 particular type), as opposed to a nested class. */
23183 else if (ANON_AGGR_TYPE_P (type))
23185 /* C++11 9.5/6. */
23186 if (decl_specifiers.storage_class != sc_none)
23187 error_at (decl_spec_token_start->location,
23188 "a storage class on an anonymous aggregate "
23189 "in class scope is not allowed");
23191 /* Remove constructors and such from TYPE, now that we
23192 know it is an anonymous aggregate. */
23193 fixup_anonymous_aggr (type);
23194 /* And make the corresponding data member. */
23195 decl = build_decl (decl_spec_token_start->location,
23196 FIELD_DECL, NULL_TREE, type);
23197 /* Add it to the class. */
23198 finish_member_declaration (decl);
23200 else
23201 cp_parser_check_access_in_redeclaration
23202 (TYPE_NAME (type),
23203 decl_spec_token_start->location);
23206 else
23208 bool assume_semicolon = false;
23210 /* Clear attributes from the decl_specifiers but keep them
23211 around as prefix attributes that apply them to the entity
23212 being declared. */
23213 prefix_attributes = decl_specifiers.attributes;
23214 decl_specifiers.attributes = NULL_TREE;
23216 /* See if these declarations will be friends. */
23217 friend_p = cp_parser_friend_p (&decl_specifiers);
23219 /* Keep going until we hit the `;' at the end of the
23220 declaration. */
23221 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23223 tree attributes = NULL_TREE;
23224 tree first_attribute;
23226 /* Peek at the next token. */
23227 token = cp_lexer_peek_token (parser->lexer);
23229 /* Check for a bitfield declaration. */
23230 if (token->type == CPP_COLON
23231 || (token->type == CPP_NAME
23232 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
23233 == CPP_COLON))
23235 tree identifier;
23236 tree width;
23238 /* Get the name of the bitfield. Note that we cannot just
23239 check TOKEN here because it may have been invalidated by
23240 the call to cp_lexer_peek_nth_token above. */
23241 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
23242 identifier = cp_parser_identifier (parser);
23243 else
23244 identifier = NULL_TREE;
23246 /* Consume the `:' token. */
23247 cp_lexer_consume_token (parser->lexer);
23248 /* Get the width of the bitfield. */
23249 width
23250 = cp_parser_constant_expression (parser);
23252 /* Look for attributes that apply to the bitfield. */
23253 attributes = cp_parser_attributes_opt (parser);
23254 /* Remember which attributes are prefix attributes and
23255 which are not. */
23256 first_attribute = attributes;
23257 /* Combine the attributes. */
23258 attributes = chainon (prefix_attributes, attributes);
23260 /* Create the bitfield declaration. */
23261 decl = grokbitfield (identifier
23262 ? make_id_declarator (NULL_TREE,
23263 identifier,
23264 sfk_none)
23265 : NULL,
23266 &decl_specifiers,
23267 width,
23268 attributes);
23270 else
23272 cp_declarator *declarator;
23273 tree initializer;
23274 tree asm_specification;
23275 int ctor_dtor_or_conv_p;
23277 /* Parse the declarator. */
23278 declarator
23279 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23280 &ctor_dtor_or_conv_p,
23281 /*parenthesized_p=*/NULL,
23282 /*member_p=*/true,
23283 friend_p);
23285 /* If something went wrong parsing the declarator, make sure
23286 that we at least consume some tokens. */
23287 if (declarator == cp_error_declarator)
23289 /* Skip to the end of the statement. */
23290 cp_parser_skip_to_end_of_statement (parser);
23291 /* If the next token is not a semicolon, that is
23292 probably because we just skipped over the body of
23293 a function. So, we consume a semicolon if
23294 present, but do not issue an error message if it
23295 is not present. */
23296 if (cp_lexer_next_token_is (parser->lexer,
23297 CPP_SEMICOLON))
23298 cp_lexer_consume_token (parser->lexer);
23299 goto out;
23302 if (declares_class_or_enum & 2)
23303 cp_parser_check_for_definition_in_return_type
23304 (declarator, decl_specifiers.type,
23305 decl_specifiers.locations[ds_type_spec]);
23307 /* Look for an asm-specification. */
23308 asm_specification = cp_parser_asm_specification_opt (parser);
23309 /* Look for attributes that apply to the declaration. */
23310 attributes = cp_parser_attributes_opt (parser);
23311 /* Remember which attributes are prefix attributes and
23312 which are not. */
23313 first_attribute = attributes;
23314 /* Combine the attributes. */
23315 attributes = chainon (prefix_attributes, attributes);
23317 /* If it's an `=', then we have a constant-initializer or a
23318 pure-specifier. It is not correct to parse the
23319 initializer before registering the member declaration
23320 since the member declaration should be in scope while
23321 its initializer is processed. However, the rest of the
23322 front end does not yet provide an interface that allows
23323 us to handle this correctly. */
23324 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23326 /* In [class.mem]:
23328 A pure-specifier shall be used only in the declaration of
23329 a virtual function.
23331 A member-declarator can contain a constant-initializer
23332 only if it declares a static member of integral or
23333 enumeration type.
23335 Therefore, if the DECLARATOR is for a function, we look
23336 for a pure-specifier; otherwise, we look for a
23337 constant-initializer. When we call `grokfield', it will
23338 perform more stringent semantics checks. */
23339 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23340 if (function_declarator_p (declarator)
23341 || (decl_specifiers.type
23342 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23343 && declarator->kind == cdk_id
23344 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23345 == FUNCTION_TYPE)))
23346 initializer = cp_parser_pure_specifier (parser);
23347 else if (decl_specifiers.storage_class != sc_static)
23348 initializer = cp_parser_save_nsdmi (parser);
23349 else if (cxx_dialect >= cxx11)
23351 bool nonconst;
23352 /* Don't require a constant rvalue in C++11, since we
23353 might want a reference constant. We'll enforce
23354 constancy later. */
23355 cp_lexer_consume_token (parser->lexer);
23356 /* Parse the initializer. */
23357 initializer = cp_parser_initializer_clause (parser,
23358 &nonconst);
23360 else
23361 /* Parse the initializer. */
23362 initializer = cp_parser_constant_initializer (parser);
23364 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23365 && !function_declarator_p (declarator))
23367 bool x;
23368 if (decl_specifiers.storage_class != sc_static)
23369 initializer = cp_parser_save_nsdmi (parser);
23370 else
23371 initializer = cp_parser_initializer (parser, &x, &x);
23373 /* Otherwise, there is no initializer. */
23374 else
23375 initializer = NULL_TREE;
23377 /* See if we are probably looking at a function
23378 definition. We are certainly not looking at a
23379 member-declarator. Calling `grokfield' has
23380 side-effects, so we must not do it unless we are sure
23381 that we are looking at a member-declarator. */
23382 if (cp_parser_token_starts_function_definition_p
23383 (cp_lexer_peek_token (parser->lexer)))
23385 /* The grammar does not allow a pure-specifier to be
23386 used when a member function is defined. (It is
23387 possible that this fact is an oversight in the
23388 standard, since a pure function may be defined
23389 outside of the class-specifier. */
23390 if (initializer && initializer_token_start)
23391 error_at (initializer_token_start->location,
23392 "pure-specifier on function-definition");
23393 decl = cp_parser_save_member_function_body (parser,
23394 &decl_specifiers,
23395 declarator,
23396 attributes);
23397 if (parser->fully_implicit_function_template_p)
23398 decl = finish_fully_implicit_template (parser, decl);
23399 /* If the member was not a friend, declare it here. */
23400 if (!friend_p)
23401 finish_member_declaration (decl);
23402 /* Peek at the next token. */
23403 token = cp_lexer_peek_token (parser->lexer);
23404 /* If the next token is a semicolon, consume it. */
23405 if (token->type == CPP_SEMICOLON)
23407 location_t semicolon_loc
23408 = cp_lexer_consume_token (parser->lexer)->location;
23409 gcc_rich_location richloc (semicolon_loc);
23410 richloc.add_fixit_remove ();
23411 warning_at_rich_loc (&richloc, OPT_Wextra_semi,
23412 "extra %<;%> after in-class "
23413 "function definition");
23415 goto out;
23417 else
23418 if (declarator->kind == cdk_function)
23419 declarator->id_loc = token->location;
23420 /* Create the declaration. */
23421 decl = grokfield (declarator, &decl_specifiers,
23422 initializer, /*init_const_expr_p=*/true,
23423 asm_specification, attributes);
23424 if (parser->fully_implicit_function_template_p)
23426 if (friend_p)
23427 finish_fully_implicit_template (parser, 0);
23428 else
23429 decl = finish_fully_implicit_template (parser, decl);
23433 cp_finalize_omp_declare_simd (parser, decl);
23434 cp_finalize_oacc_routine (parser, decl, false);
23436 /* Reset PREFIX_ATTRIBUTES. */
23437 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23438 attributes = TREE_CHAIN (attributes);
23439 if (attributes)
23440 TREE_CHAIN (attributes) = NULL_TREE;
23442 /* If there is any qualification still in effect, clear it
23443 now; we will be starting fresh with the next declarator. */
23444 parser->scope = NULL_TREE;
23445 parser->qualifying_scope = NULL_TREE;
23446 parser->object_scope = NULL_TREE;
23447 /* If it's a `,', then there are more declarators. */
23448 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23450 cp_lexer_consume_token (parser->lexer);
23451 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23453 cp_token *token = cp_lexer_previous_token (parser->lexer);
23454 gcc_rich_location richloc (token->location);
23455 richloc.add_fixit_remove ();
23456 error_at_rich_loc (&richloc, "stray %<,%> at end of "
23457 "member declaration");
23460 /* If the next token isn't a `;', then we have a parse error. */
23461 else if (cp_lexer_next_token_is_not (parser->lexer,
23462 CPP_SEMICOLON))
23464 /* The next token might be a ways away from where the
23465 actual semicolon is missing. Find the previous token
23466 and use that for our error position. */
23467 cp_token *token = cp_lexer_previous_token (parser->lexer);
23468 gcc_rich_location richloc (token->location);
23469 richloc.add_fixit_insert_after (";");
23470 error_at_rich_loc (&richloc, "expected %<;%> at end of "
23471 "member declaration");
23473 /* Assume that the user meant to provide a semicolon. If
23474 we were to cp_parser_skip_to_end_of_statement, we might
23475 skip to a semicolon inside a member function definition
23476 and issue nonsensical error messages. */
23477 assume_semicolon = true;
23480 if (decl)
23482 /* Add DECL to the list of members. */
23483 if (!friend_p
23484 /* Explicitly include, eg, NSDMIs, for better error
23485 recovery (c++/58650). */
23486 || !DECL_DECLARES_FUNCTION_P (decl))
23487 finish_member_declaration (decl);
23489 if (TREE_CODE (decl) == FUNCTION_DECL)
23490 cp_parser_save_default_args (parser, decl);
23491 else if (TREE_CODE (decl) == FIELD_DECL
23492 && !DECL_C_BIT_FIELD (decl)
23493 && DECL_INITIAL (decl))
23494 /* Add DECL to the queue of NSDMI to be parsed later. */
23495 vec_safe_push (unparsed_nsdmis, decl);
23498 if (assume_semicolon)
23499 goto out;
23503 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23504 out:
23505 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23508 /* Parse a pure-specifier.
23510 pure-specifier:
23513 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23514 Otherwise, ERROR_MARK_NODE is returned. */
23516 static tree
23517 cp_parser_pure_specifier (cp_parser* parser)
23519 cp_token *token;
23521 /* Look for the `=' token. */
23522 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23523 return error_mark_node;
23524 /* Look for the `0' token. */
23525 token = cp_lexer_peek_token (parser->lexer);
23527 if (token->type == CPP_EOF
23528 || token->type == CPP_PRAGMA_EOL)
23529 return error_mark_node;
23531 cp_lexer_consume_token (parser->lexer);
23533 /* Accept = default or = delete in c++0x mode. */
23534 if (token->keyword == RID_DEFAULT
23535 || token->keyword == RID_DELETE)
23537 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23538 return token->u.value;
23541 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23542 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23544 cp_parser_error (parser,
23545 "invalid pure specifier (only %<= 0%> is allowed)");
23546 cp_parser_skip_to_end_of_statement (parser);
23547 return error_mark_node;
23549 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23551 error_at (token->location, "templates may not be %<virtual%>");
23552 return error_mark_node;
23555 return integer_zero_node;
23558 /* Parse a constant-initializer.
23560 constant-initializer:
23561 = constant-expression
23563 Returns a representation of the constant-expression. */
23565 static tree
23566 cp_parser_constant_initializer (cp_parser* parser)
23568 /* Look for the `=' token. */
23569 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23570 return error_mark_node;
23572 /* It is invalid to write:
23574 struct S { static const int i = { 7 }; };
23577 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23579 cp_parser_error (parser,
23580 "a brace-enclosed initializer is not allowed here");
23581 /* Consume the opening brace. */
23582 cp_lexer_consume_token (parser->lexer);
23583 /* Skip the initializer. */
23584 cp_parser_skip_to_closing_brace (parser);
23585 /* Look for the trailing `}'. */
23586 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23588 return error_mark_node;
23591 return cp_parser_constant_expression (parser);
23594 /* Derived classes [gram.class.derived] */
23596 /* Parse a base-clause.
23598 base-clause:
23599 : base-specifier-list
23601 base-specifier-list:
23602 base-specifier ... [opt]
23603 base-specifier-list , base-specifier ... [opt]
23605 Returns a TREE_LIST representing the base-classes, in the order in
23606 which they were declared. The representation of each node is as
23607 described by cp_parser_base_specifier.
23609 In the case that no bases are specified, this function will return
23610 NULL_TREE, not ERROR_MARK_NODE. */
23612 static tree
23613 cp_parser_base_clause (cp_parser* parser)
23615 tree bases = NULL_TREE;
23617 /* Look for the `:' that begins the list. */
23618 cp_parser_require (parser, CPP_COLON, RT_COLON);
23620 /* Scan the base-specifier-list. */
23621 while (true)
23623 cp_token *token;
23624 tree base;
23625 bool pack_expansion_p = false;
23627 /* Look for the base-specifier. */
23628 base = cp_parser_base_specifier (parser);
23629 /* Look for the (optional) ellipsis. */
23630 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23632 /* Consume the `...'. */
23633 cp_lexer_consume_token (parser->lexer);
23635 pack_expansion_p = true;
23638 /* Add BASE to the front of the list. */
23639 if (base && base != error_mark_node)
23641 if (pack_expansion_p)
23642 /* Make this a pack expansion type. */
23643 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23645 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23647 TREE_CHAIN (base) = bases;
23648 bases = base;
23651 /* Peek at the next token. */
23652 token = cp_lexer_peek_token (parser->lexer);
23653 /* If it's not a comma, then the list is complete. */
23654 if (token->type != CPP_COMMA)
23655 break;
23656 /* Consume the `,'. */
23657 cp_lexer_consume_token (parser->lexer);
23660 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23661 base class had a qualified name. However, the next name that
23662 appears is certainly not qualified. */
23663 parser->scope = NULL_TREE;
23664 parser->qualifying_scope = NULL_TREE;
23665 parser->object_scope = NULL_TREE;
23667 return nreverse (bases);
23670 /* Parse a base-specifier.
23672 base-specifier:
23673 :: [opt] nested-name-specifier [opt] class-name
23674 virtual access-specifier [opt] :: [opt] nested-name-specifier
23675 [opt] class-name
23676 access-specifier virtual [opt] :: [opt] nested-name-specifier
23677 [opt] class-name
23679 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23680 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23681 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23682 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23684 static tree
23685 cp_parser_base_specifier (cp_parser* parser)
23687 cp_token *token;
23688 bool done = false;
23689 bool virtual_p = false;
23690 bool duplicate_virtual_error_issued_p = false;
23691 bool duplicate_access_error_issued_p = false;
23692 bool class_scope_p, template_p;
23693 tree access = access_default_node;
23694 tree type;
23696 /* Process the optional `virtual' and `access-specifier'. */
23697 while (!done)
23699 /* Peek at the next token. */
23700 token = cp_lexer_peek_token (parser->lexer);
23701 /* Process `virtual'. */
23702 switch (token->keyword)
23704 case RID_VIRTUAL:
23705 /* If `virtual' appears more than once, issue an error. */
23706 if (virtual_p && !duplicate_virtual_error_issued_p)
23708 cp_parser_error (parser,
23709 "%<virtual%> specified more than once in base-specifier");
23710 duplicate_virtual_error_issued_p = true;
23713 virtual_p = true;
23715 /* Consume the `virtual' token. */
23716 cp_lexer_consume_token (parser->lexer);
23718 break;
23720 case RID_PUBLIC:
23721 case RID_PROTECTED:
23722 case RID_PRIVATE:
23723 /* If more than one access specifier appears, issue an
23724 error. */
23725 if (access != access_default_node
23726 && !duplicate_access_error_issued_p)
23728 cp_parser_error (parser,
23729 "more than one access specifier in base-specifier");
23730 duplicate_access_error_issued_p = true;
23733 access = ridpointers[(int) token->keyword];
23735 /* Consume the access-specifier. */
23736 cp_lexer_consume_token (parser->lexer);
23738 break;
23740 default:
23741 done = true;
23742 break;
23745 /* It is not uncommon to see programs mechanically, erroneously, use
23746 the 'typename' keyword to denote (dependent) qualified types
23747 as base classes. */
23748 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23750 token = cp_lexer_peek_token (parser->lexer);
23751 if (!processing_template_decl)
23752 error_at (token->location,
23753 "keyword %<typename%> not allowed outside of templates");
23754 else
23755 error_at (token->location,
23756 "keyword %<typename%> not allowed in this context "
23757 "(the base class is implicitly a type)");
23758 cp_lexer_consume_token (parser->lexer);
23761 /* Look for the optional `::' operator. */
23762 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23763 /* Look for the nested-name-specifier. The simplest way to
23764 implement:
23766 [temp.res]
23768 The keyword `typename' is not permitted in a base-specifier or
23769 mem-initializer; in these contexts a qualified name that
23770 depends on a template-parameter is implicitly assumed to be a
23771 type name.
23773 is to pretend that we have seen the `typename' keyword at this
23774 point. */
23775 cp_parser_nested_name_specifier_opt (parser,
23776 /*typename_keyword_p=*/true,
23777 /*check_dependency_p=*/true,
23778 /*type_p=*/true,
23779 /*is_declaration=*/true);
23780 /* If the base class is given by a qualified name, assume that names
23781 we see are type names or templates, as appropriate. */
23782 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23783 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23785 if (!parser->scope
23786 && cp_lexer_next_token_is_decltype (parser->lexer))
23787 /* DR 950 allows decltype as a base-specifier. */
23788 type = cp_parser_decltype (parser);
23789 else
23791 /* Otherwise, look for the class-name. */
23792 type = cp_parser_class_name (parser,
23793 class_scope_p,
23794 template_p,
23795 typename_type,
23796 /*check_dependency_p=*/true,
23797 /*class_head_p=*/false,
23798 /*is_declaration=*/true);
23799 type = TREE_TYPE (type);
23802 if (type == error_mark_node)
23803 return error_mark_node;
23805 return finish_base_specifier (type, access, virtual_p);
23808 /* Exception handling [gram.exception] */
23810 /* Parse an (optional) noexcept-specification.
23812 noexcept-specification:
23813 noexcept ( constant-expression ) [opt]
23815 If no noexcept-specification is present, returns NULL_TREE.
23816 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23817 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23818 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23819 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23820 in which case a boolean condition is returned instead. */
23822 static tree
23823 cp_parser_noexcept_specification_opt (cp_parser* parser,
23824 bool require_constexpr,
23825 bool* consumed_expr,
23826 bool return_cond)
23828 cp_token *token;
23829 const char *saved_message;
23831 /* Peek at the next token. */
23832 token = cp_lexer_peek_token (parser->lexer);
23834 /* Is it a noexcept-specification? */
23835 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23837 tree expr;
23838 cp_lexer_consume_token (parser->lexer);
23840 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23842 cp_lexer_consume_token (parser->lexer);
23844 if (require_constexpr)
23846 /* Types may not be defined in an exception-specification. */
23847 saved_message = parser->type_definition_forbidden_message;
23848 parser->type_definition_forbidden_message
23849 = G_("types may not be defined in an exception-specification");
23851 expr = cp_parser_constant_expression (parser);
23853 /* Restore the saved message. */
23854 parser->type_definition_forbidden_message = saved_message;
23856 else
23858 expr = cp_parser_expression (parser);
23859 *consumed_expr = true;
23862 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23864 else
23866 expr = boolean_true_node;
23867 if (!require_constexpr)
23868 *consumed_expr = false;
23871 /* We cannot build a noexcept-spec right away because this will check
23872 that expr is a constexpr. */
23873 if (!return_cond)
23874 return build_noexcept_spec (expr, tf_warning_or_error);
23875 else
23876 return expr;
23878 else
23879 return NULL_TREE;
23882 /* Parse an (optional) exception-specification.
23884 exception-specification:
23885 throw ( type-id-list [opt] )
23887 Returns a TREE_LIST representing the exception-specification. The
23888 TREE_VALUE of each node is a type. */
23890 static tree
23891 cp_parser_exception_specification_opt (cp_parser* parser)
23893 cp_token *token;
23894 tree type_id_list;
23895 const char *saved_message;
23897 /* Peek at the next token. */
23898 token = cp_lexer_peek_token (parser->lexer);
23900 /* Is it a noexcept-specification? */
23901 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
23902 false);
23903 if (type_id_list != NULL_TREE)
23904 return type_id_list;
23906 /* If it's not `throw', then there's no exception-specification. */
23907 if (!cp_parser_is_keyword (token, RID_THROW))
23908 return NULL_TREE;
23910 location_t loc = token->location;
23912 /* Consume the `throw'. */
23913 cp_lexer_consume_token (parser->lexer);
23915 /* Look for the `('. */
23916 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23918 /* Peek at the next token. */
23919 token = cp_lexer_peek_token (parser->lexer);
23920 /* If it's not a `)', then there is a type-id-list. */
23921 if (token->type != CPP_CLOSE_PAREN)
23923 /* Types may not be defined in an exception-specification. */
23924 saved_message = parser->type_definition_forbidden_message;
23925 parser->type_definition_forbidden_message
23926 = G_("types may not be defined in an exception-specification");
23927 /* Parse the type-id-list. */
23928 type_id_list = cp_parser_type_id_list (parser);
23929 /* Restore the saved message. */
23930 parser->type_definition_forbidden_message = saved_message;
23932 if (cxx_dialect >= cxx1z)
23934 error_at (loc, "ISO C++1z does not allow dynamic exception "
23935 "specifications");
23936 type_id_list = NULL_TREE;
23938 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
23939 warning_at (loc, OPT_Wdeprecated,
23940 "dynamic exception specifications are deprecated in "
23941 "C++11");
23943 /* In C++17, throw() is equivalent to noexcept (true). throw()
23944 is deprecated in C++11 and above as well, but is still widely used,
23945 so don't warn about it yet. */
23946 else if (cxx_dialect >= cxx1z)
23947 type_id_list = noexcept_true_spec;
23948 else
23949 type_id_list = empty_except_spec;
23951 /* Look for the `)'. */
23952 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23954 return type_id_list;
23957 /* Parse an (optional) type-id-list.
23959 type-id-list:
23960 type-id ... [opt]
23961 type-id-list , type-id ... [opt]
23963 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23964 in the order that the types were presented. */
23966 static tree
23967 cp_parser_type_id_list (cp_parser* parser)
23969 tree types = NULL_TREE;
23971 while (true)
23973 cp_token *token;
23974 tree type;
23976 token = cp_lexer_peek_token (parser->lexer);
23978 /* Get the next type-id. */
23979 type = cp_parser_type_id (parser);
23980 /* Check for invalid 'auto'. */
23981 if (flag_concepts && type_uses_auto (type))
23983 error_at (token->location,
23984 "invalid use of %<auto%> in exception-specification");
23985 type = error_mark_node;
23987 /* Parse the optional ellipsis. */
23988 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23990 /* Consume the `...'. */
23991 cp_lexer_consume_token (parser->lexer);
23993 /* Turn the type into a pack expansion expression. */
23994 type = make_pack_expansion (type);
23996 /* Add it to the list. */
23997 types = add_exception_specifier (types, type, /*complain=*/1);
23998 /* Peek at the next token. */
23999 token = cp_lexer_peek_token (parser->lexer);
24000 /* If it is not a `,', we are done. */
24001 if (token->type != CPP_COMMA)
24002 break;
24003 /* Consume the `,'. */
24004 cp_lexer_consume_token (parser->lexer);
24007 return nreverse (types);
24010 /* Parse a try-block.
24012 try-block:
24013 try compound-statement handler-seq */
24015 static tree
24016 cp_parser_try_block (cp_parser* parser)
24018 tree try_block;
24020 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24021 if (parser->in_function_body
24022 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24023 error ("%<try%> in %<constexpr%> function");
24025 try_block = begin_try_block ();
24026 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24027 finish_try_block (try_block);
24028 cp_parser_handler_seq (parser);
24029 finish_handler_sequence (try_block);
24031 return try_block;
24034 /* Parse a function-try-block.
24036 function-try-block:
24037 try ctor-initializer [opt] function-body handler-seq */
24039 static bool
24040 cp_parser_function_try_block (cp_parser* parser)
24042 tree compound_stmt;
24043 tree try_block;
24044 bool ctor_initializer_p;
24046 /* Look for the `try' keyword. */
24047 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24048 return false;
24049 /* Let the rest of the front end know where we are. */
24050 try_block = begin_function_try_block (&compound_stmt);
24051 /* Parse the function-body. */
24052 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24053 (parser, /*in_function_try_block=*/true);
24054 /* We're done with the `try' part. */
24055 finish_function_try_block (try_block);
24056 /* Parse the handlers. */
24057 cp_parser_handler_seq (parser);
24058 /* We're done with the handlers. */
24059 finish_function_handler_sequence (try_block, compound_stmt);
24061 return ctor_initializer_p;
24064 /* Parse a handler-seq.
24066 handler-seq:
24067 handler handler-seq [opt] */
24069 static void
24070 cp_parser_handler_seq (cp_parser* parser)
24072 while (true)
24074 cp_token *token;
24076 /* Parse the handler. */
24077 cp_parser_handler (parser);
24078 /* Peek at the next token. */
24079 token = cp_lexer_peek_token (parser->lexer);
24080 /* If it's not `catch' then there are no more handlers. */
24081 if (!cp_parser_is_keyword (token, RID_CATCH))
24082 break;
24086 /* Parse a handler.
24088 handler:
24089 catch ( exception-declaration ) compound-statement */
24091 static void
24092 cp_parser_handler (cp_parser* parser)
24094 tree handler;
24095 tree declaration;
24097 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24098 handler = begin_handler ();
24099 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24100 declaration = cp_parser_exception_declaration (parser);
24101 finish_handler_parms (declaration, handler);
24102 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24103 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24104 finish_handler (handler);
24107 /* Parse an exception-declaration.
24109 exception-declaration:
24110 type-specifier-seq declarator
24111 type-specifier-seq abstract-declarator
24112 type-specifier-seq
24115 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24116 ellipsis variant is used. */
24118 static tree
24119 cp_parser_exception_declaration (cp_parser* parser)
24121 cp_decl_specifier_seq type_specifiers;
24122 cp_declarator *declarator;
24123 const char *saved_message;
24125 /* If it's an ellipsis, it's easy to handle. */
24126 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24128 /* Consume the `...' token. */
24129 cp_lexer_consume_token (parser->lexer);
24130 return NULL_TREE;
24133 /* Types may not be defined in exception-declarations. */
24134 saved_message = parser->type_definition_forbidden_message;
24135 parser->type_definition_forbidden_message
24136 = G_("types may not be defined in exception-declarations");
24138 /* Parse the type-specifier-seq. */
24139 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24140 /*is_trailing_return=*/false,
24141 &type_specifiers);
24142 /* If it's a `)', then there is no declarator. */
24143 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24144 declarator = NULL;
24145 else
24146 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24147 /*ctor_dtor_or_conv_p=*/NULL,
24148 /*parenthesized_p=*/NULL,
24149 /*member_p=*/false,
24150 /*friend_p=*/false);
24152 /* Restore the saved message. */
24153 parser->type_definition_forbidden_message = saved_message;
24155 if (!type_specifiers.any_specifiers_p)
24156 return error_mark_node;
24158 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24161 /* Parse a throw-expression.
24163 throw-expression:
24164 throw assignment-expression [opt]
24166 Returns a THROW_EXPR representing the throw-expression. */
24168 static tree
24169 cp_parser_throw_expression (cp_parser* parser)
24171 tree expression;
24172 cp_token* token;
24174 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24175 token = cp_lexer_peek_token (parser->lexer);
24176 /* Figure out whether or not there is an assignment-expression
24177 following the "throw" keyword. */
24178 if (token->type == CPP_COMMA
24179 || token->type == CPP_SEMICOLON
24180 || token->type == CPP_CLOSE_PAREN
24181 || token->type == CPP_CLOSE_SQUARE
24182 || token->type == CPP_CLOSE_BRACE
24183 || token->type == CPP_COLON)
24184 expression = NULL_TREE;
24185 else
24186 expression = cp_parser_assignment_expression (parser);
24188 return build_throw (expression);
24191 /* GNU Extensions */
24193 /* Parse an (optional) asm-specification.
24195 asm-specification:
24196 asm ( string-literal )
24198 If the asm-specification is present, returns a STRING_CST
24199 corresponding to the string-literal. Otherwise, returns
24200 NULL_TREE. */
24202 static tree
24203 cp_parser_asm_specification_opt (cp_parser* parser)
24205 cp_token *token;
24206 tree asm_specification;
24208 /* Peek at the next token. */
24209 token = cp_lexer_peek_token (parser->lexer);
24210 /* If the next token isn't the `asm' keyword, then there's no
24211 asm-specification. */
24212 if (!cp_parser_is_keyword (token, RID_ASM))
24213 return NULL_TREE;
24215 /* Consume the `asm' token. */
24216 cp_lexer_consume_token (parser->lexer);
24217 /* Look for the `('. */
24218 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24220 /* Look for the string-literal. */
24221 asm_specification = cp_parser_string_literal (parser, false, false);
24223 /* Look for the `)'. */
24224 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24226 return asm_specification;
24229 /* Parse an asm-operand-list.
24231 asm-operand-list:
24232 asm-operand
24233 asm-operand-list , asm-operand
24235 asm-operand:
24236 string-literal ( expression )
24237 [ string-literal ] string-literal ( expression )
24239 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24240 each node is the expression. The TREE_PURPOSE is itself a
24241 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24242 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24243 is a STRING_CST for the string literal before the parenthesis. Returns
24244 ERROR_MARK_NODE if any of the operands are invalid. */
24246 static tree
24247 cp_parser_asm_operand_list (cp_parser* parser)
24249 tree asm_operands = NULL_TREE;
24250 bool invalid_operands = false;
24252 while (true)
24254 tree string_literal;
24255 tree expression;
24256 tree name;
24258 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24260 /* Consume the `[' token. */
24261 cp_lexer_consume_token (parser->lexer);
24262 /* Read the operand name. */
24263 name = cp_parser_identifier (parser);
24264 if (name != error_mark_node)
24265 name = build_string (IDENTIFIER_LENGTH (name),
24266 IDENTIFIER_POINTER (name));
24267 /* Look for the closing `]'. */
24268 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24270 else
24271 name = NULL_TREE;
24272 /* Look for the string-literal. */
24273 string_literal = cp_parser_string_literal (parser, false, false);
24275 /* Look for the `('. */
24276 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24277 /* Parse the expression. */
24278 expression = cp_parser_expression (parser);
24279 /* Look for the `)'. */
24280 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24282 if (name == error_mark_node
24283 || string_literal == error_mark_node
24284 || expression == error_mark_node)
24285 invalid_operands = true;
24287 /* Add this operand to the list. */
24288 asm_operands = tree_cons (build_tree_list (name, string_literal),
24289 expression,
24290 asm_operands);
24291 /* If the next token is not a `,', there are no more
24292 operands. */
24293 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24294 break;
24295 /* Consume the `,'. */
24296 cp_lexer_consume_token (parser->lexer);
24299 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24302 /* Parse an asm-clobber-list.
24304 asm-clobber-list:
24305 string-literal
24306 asm-clobber-list , string-literal
24308 Returns a TREE_LIST, indicating the clobbers in the order that they
24309 appeared. The TREE_VALUE of each node is a STRING_CST. */
24311 static tree
24312 cp_parser_asm_clobber_list (cp_parser* parser)
24314 tree clobbers = NULL_TREE;
24316 while (true)
24318 tree string_literal;
24320 /* Look for the string literal. */
24321 string_literal = cp_parser_string_literal (parser, false, false);
24322 /* Add it to the list. */
24323 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24324 /* If the next token is not a `,', then the list is
24325 complete. */
24326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24327 break;
24328 /* Consume the `,' token. */
24329 cp_lexer_consume_token (parser->lexer);
24332 return clobbers;
24335 /* Parse an asm-label-list.
24337 asm-label-list:
24338 identifier
24339 asm-label-list , identifier
24341 Returns a TREE_LIST, indicating the labels in the order that they
24342 appeared. The TREE_VALUE of each node is a label. */
24344 static tree
24345 cp_parser_asm_label_list (cp_parser* parser)
24347 tree labels = NULL_TREE;
24349 while (true)
24351 tree identifier, label, name;
24353 /* Look for the identifier. */
24354 identifier = cp_parser_identifier (parser);
24355 if (!error_operand_p (identifier))
24357 label = lookup_label (identifier);
24358 if (TREE_CODE (label) == LABEL_DECL)
24360 TREE_USED (label) = 1;
24361 check_goto (label);
24362 name = build_string (IDENTIFIER_LENGTH (identifier),
24363 IDENTIFIER_POINTER (identifier));
24364 labels = tree_cons (name, label, labels);
24367 /* If the next token is not a `,', then the list is
24368 complete. */
24369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24370 break;
24371 /* Consume the `,' token. */
24372 cp_lexer_consume_token (parser->lexer);
24375 return nreverse (labels);
24378 /* Return TRUE iff the next tokens in the stream are possibly the
24379 beginning of a GNU extension attribute. */
24381 static bool
24382 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24384 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24387 /* Return TRUE iff the next tokens in the stream are possibly the
24388 beginning of a standard C++-11 attribute specifier. */
24390 static bool
24391 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24393 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24396 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24397 beginning of a standard C++-11 attribute specifier. */
24399 static bool
24400 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24402 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24404 return (cxx_dialect >= cxx11
24405 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24406 || (token->type == CPP_OPEN_SQUARE
24407 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24408 && token->type == CPP_OPEN_SQUARE)));
24411 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24412 beginning of a GNU extension attribute. */
24414 static bool
24415 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24417 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24419 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24422 /* Return true iff the next tokens can be the beginning of either a
24423 GNU attribute list, or a standard C++11 attribute sequence. */
24425 static bool
24426 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24428 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24429 || cp_next_tokens_can_be_std_attribute_p (parser));
24432 /* Return true iff the next Nth tokens can be the beginning of either
24433 a GNU attribute list, or a standard C++11 attribute sequence. */
24435 static bool
24436 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24438 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24439 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24442 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24443 of GNU attributes, or return NULL. */
24445 static tree
24446 cp_parser_attributes_opt (cp_parser *parser)
24448 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24449 return cp_parser_gnu_attributes_opt (parser);
24450 return cp_parser_std_attribute_spec_seq (parser);
24453 #define CILK_SIMD_FN_CLAUSE_MASK \
24454 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24455 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24456 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24457 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24458 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24460 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24461 vector [(<clauses>)] */
24463 static void
24464 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24466 bool first_p = parser->cilk_simd_fn_info == NULL;
24467 cp_token *token = v_token;
24468 if (first_p)
24470 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24471 parser->cilk_simd_fn_info->error_seen = false;
24472 parser->cilk_simd_fn_info->fndecl_seen = false;
24473 parser->cilk_simd_fn_info->tokens = vNULL;
24474 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24476 int paren_scope = 0;
24477 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24479 cp_lexer_consume_token (parser->lexer);
24480 v_token = cp_lexer_peek_token (parser->lexer);
24481 paren_scope++;
24483 while (paren_scope > 0)
24485 token = cp_lexer_peek_token (parser->lexer);
24486 if (token->type == CPP_OPEN_PAREN)
24487 paren_scope++;
24488 else if (token->type == CPP_CLOSE_PAREN)
24489 paren_scope--;
24490 /* Do not push the last ')' */
24491 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24492 cp_lexer_consume_token (parser->lexer);
24495 token->type = CPP_PRAGMA_EOL;
24496 parser->lexer->next_token = token;
24497 cp_lexer_consume_token (parser->lexer);
24499 struct cp_token_cache *cp
24500 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24501 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24504 /* Parse an (optional) series of attributes.
24506 attributes:
24507 attributes attribute
24509 attribute:
24510 __attribute__ (( attribute-list [opt] ))
24512 The return value is as for cp_parser_gnu_attribute_list. */
24514 static tree
24515 cp_parser_gnu_attributes_opt (cp_parser* parser)
24517 tree attributes = NULL_TREE;
24519 while (true)
24521 cp_token *token;
24522 tree attribute_list;
24523 bool ok = true;
24525 /* Peek at the next token. */
24526 token = cp_lexer_peek_token (parser->lexer);
24527 /* If it's not `__attribute__', then we're done. */
24528 if (token->keyword != RID_ATTRIBUTE)
24529 break;
24531 /* Consume the `__attribute__' keyword. */
24532 cp_lexer_consume_token (parser->lexer);
24533 /* Look for the two `(' tokens. */
24534 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24535 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24537 /* Peek at the next token. */
24538 token = cp_lexer_peek_token (parser->lexer);
24539 if (token->type != CPP_CLOSE_PAREN)
24540 /* Parse the attribute-list. */
24541 attribute_list = cp_parser_gnu_attribute_list (parser);
24542 else
24543 /* If the next token is a `)', then there is no attribute
24544 list. */
24545 attribute_list = NULL;
24547 /* Look for the two `)' tokens. */
24548 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24549 ok = false;
24550 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24551 ok = false;
24552 if (!ok)
24553 cp_parser_skip_to_end_of_statement (parser);
24555 /* Add these new attributes to the list. */
24556 attributes = chainon (attributes, attribute_list);
24559 return attributes;
24562 /* Parse a GNU attribute-list.
24564 attribute-list:
24565 attribute
24566 attribute-list , attribute
24568 attribute:
24569 identifier
24570 identifier ( identifier )
24571 identifier ( identifier , expression-list )
24572 identifier ( expression-list )
24574 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24575 to an attribute. The TREE_PURPOSE of each node is the identifier
24576 indicating which attribute is in use. The TREE_VALUE represents
24577 the arguments, if any. */
24579 static tree
24580 cp_parser_gnu_attribute_list (cp_parser* parser)
24582 tree attribute_list = NULL_TREE;
24583 bool save_translate_strings_p = parser->translate_strings_p;
24585 parser->translate_strings_p = false;
24586 while (true)
24588 cp_token *token;
24589 tree identifier;
24590 tree attribute;
24592 /* Look for the identifier. We also allow keywords here; for
24593 example `__attribute__ ((const))' is legal. */
24594 token = cp_lexer_peek_token (parser->lexer);
24595 if (token->type == CPP_NAME
24596 || token->type == CPP_KEYWORD)
24598 tree arguments = NULL_TREE;
24600 /* Consume the token, but save it since we need it for the
24601 SIMD enabled function parsing. */
24602 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24604 /* Save away the identifier that indicates which attribute
24605 this is. */
24606 identifier = (token->type == CPP_KEYWORD)
24607 /* For keywords, use the canonical spelling, not the
24608 parsed identifier. */
24609 ? ridpointers[(int) token->keyword]
24610 : id_token->u.value;
24612 attribute = build_tree_list (identifier, NULL_TREE);
24614 /* Peek at the next token. */
24615 token = cp_lexer_peek_token (parser->lexer);
24616 /* If it's an `(', then parse the attribute arguments. */
24617 if (token->type == CPP_OPEN_PAREN)
24619 vec<tree, va_gc> *vec;
24620 int attr_flag = (attribute_takes_identifier_p (identifier)
24621 ? id_attr : normal_attr);
24622 if (is_cilkplus_vector_p (identifier))
24624 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24625 continue;
24627 else
24628 vec = cp_parser_parenthesized_expression_list
24629 (parser, attr_flag, /*cast_p=*/false,
24630 /*allow_expansion_p=*/false,
24631 /*non_constant_p=*/NULL);
24632 if (vec == NULL)
24633 arguments = error_mark_node;
24634 else
24636 arguments = build_tree_list_vec (vec);
24637 release_tree_vector (vec);
24639 /* Save the arguments away. */
24640 TREE_VALUE (attribute) = arguments;
24642 else if (is_cilkplus_vector_p (identifier))
24644 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24645 continue;
24648 if (arguments != error_mark_node)
24650 /* Add this attribute to the list. */
24651 TREE_CHAIN (attribute) = attribute_list;
24652 attribute_list = attribute;
24655 token = cp_lexer_peek_token (parser->lexer);
24657 /* Now, look for more attributes. If the next token isn't a
24658 `,', we're done. */
24659 if (token->type != CPP_COMMA)
24660 break;
24662 /* Consume the comma and keep going. */
24663 cp_lexer_consume_token (parser->lexer);
24665 parser->translate_strings_p = save_translate_strings_p;
24667 /* We built up the list in reverse order. */
24668 return nreverse (attribute_list);
24671 /* Parse a standard C++11 attribute.
24673 The returned representation is a TREE_LIST which TREE_PURPOSE is
24674 the scoped name of the attribute, and the TREE_VALUE is its
24675 arguments list.
24677 Note that the scoped name of the attribute is itself a TREE_LIST
24678 which TREE_PURPOSE is the namespace of the attribute, and
24679 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24680 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24681 and which TREE_PURPOSE is directly the attribute name.
24683 Clients of the attribute code should use get_attribute_namespace
24684 and get_attribute_name to get the actual namespace and name of
24685 attributes, regardless of their being GNU or C++11 attributes.
24687 attribute:
24688 attribute-token attribute-argument-clause [opt]
24690 attribute-token:
24691 identifier
24692 attribute-scoped-token
24694 attribute-scoped-token:
24695 attribute-namespace :: identifier
24697 attribute-namespace:
24698 identifier
24700 attribute-argument-clause:
24701 ( balanced-token-seq )
24703 balanced-token-seq:
24704 balanced-token [opt]
24705 balanced-token-seq balanced-token
24707 balanced-token:
24708 ( balanced-token-seq )
24709 [ balanced-token-seq ]
24710 { balanced-token-seq }. */
24712 static tree
24713 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24715 tree attribute, attr_id = NULL_TREE, arguments;
24716 cp_token *token;
24718 /* First, parse name of the attribute, a.k.a attribute-token. */
24720 token = cp_lexer_peek_token (parser->lexer);
24721 if (token->type == CPP_NAME)
24722 attr_id = token->u.value;
24723 else if (token->type == CPP_KEYWORD)
24724 attr_id = ridpointers[(int) token->keyword];
24725 else if (token->flags & NAMED_OP)
24726 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24728 if (attr_id == NULL_TREE)
24729 return NULL_TREE;
24731 cp_lexer_consume_token (parser->lexer);
24733 token = cp_lexer_peek_token (parser->lexer);
24734 if (token->type == CPP_SCOPE)
24736 /* We are seeing a scoped attribute token. */
24738 cp_lexer_consume_token (parser->lexer);
24739 if (attr_ns)
24740 error_at (token->location, "attribute using prefix used together "
24741 "with scoped attribute token");
24742 attr_ns = attr_id;
24744 token = cp_lexer_consume_token (parser->lexer);
24745 if (token->type == CPP_NAME)
24746 attr_id = token->u.value;
24747 else if (token->type == CPP_KEYWORD)
24748 attr_id = ridpointers[(int) token->keyword];
24749 else if (token->flags & NAMED_OP)
24750 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24751 else
24753 error_at (token->location,
24754 "expected an identifier for the attribute name");
24755 return error_mark_node;
24757 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24758 NULL_TREE);
24759 token = cp_lexer_peek_token (parser->lexer);
24761 else if (attr_ns)
24762 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24763 NULL_TREE);
24764 else
24766 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24767 NULL_TREE);
24768 /* C++11 noreturn attribute is equivalent to GNU's. */
24769 if (is_attribute_p ("noreturn", attr_id))
24770 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24771 /* C++14 deprecated attribute is equivalent to GNU's. */
24772 else if (is_attribute_p ("deprecated", attr_id))
24773 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24774 /* C++17 fallthrough attribute is equivalent to GNU's. */
24775 else if (is_attribute_p ("fallthrough", attr_id))
24776 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24777 /* Transactional Memory TS optimize_for_synchronized attribute is
24778 equivalent to GNU transaction_callable. */
24779 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24780 TREE_PURPOSE (attribute)
24781 = get_identifier ("transaction_callable");
24782 /* Transactional Memory attributes are GNU attributes. */
24783 else if (tm_attr_to_mask (attr_id))
24784 TREE_PURPOSE (attribute) = attr_id;
24787 /* Now parse the optional argument clause of the attribute. */
24789 if (token->type != CPP_OPEN_PAREN)
24790 return attribute;
24793 vec<tree, va_gc> *vec;
24794 int attr_flag = normal_attr;
24796 if (attr_ns == get_identifier ("gnu")
24797 && attribute_takes_identifier_p (attr_id))
24798 /* A GNU attribute that takes an identifier in parameter. */
24799 attr_flag = id_attr;
24801 vec = cp_parser_parenthesized_expression_list
24802 (parser, attr_flag, /*cast_p=*/false,
24803 /*allow_expansion_p=*/true,
24804 /*non_constant_p=*/NULL);
24805 if (vec == NULL)
24806 arguments = error_mark_node;
24807 else
24809 arguments = build_tree_list_vec (vec);
24810 release_tree_vector (vec);
24813 if (arguments == error_mark_node)
24814 attribute = error_mark_node;
24815 else
24816 TREE_VALUE (attribute) = arguments;
24819 return attribute;
24822 /* Check that the attribute ATTRIBUTE appears at most once in the
24823 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24824 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24825 isn't implemented yet in GCC. */
24827 static void
24828 cp_parser_check_std_attribute (tree attributes, tree attribute)
24830 if (attributes)
24832 tree name = get_attribute_name (attribute);
24833 if (is_attribute_p ("noreturn", name)
24834 && lookup_attribute ("noreturn", attributes))
24835 error ("attribute %<noreturn%> can appear at most once "
24836 "in an attribute-list");
24837 else if (is_attribute_p ("deprecated", name)
24838 && lookup_attribute ("deprecated", attributes))
24839 error ("attribute %<deprecated%> can appear at most once "
24840 "in an attribute-list");
24844 /* Parse a list of standard C++-11 attributes.
24846 attribute-list:
24847 attribute [opt]
24848 attribute-list , attribute[opt]
24849 attribute ...
24850 attribute-list , attribute ...
24853 static tree
24854 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
24856 tree attributes = NULL_TREE, attribute = NULL_TREE;
24857 cp_token *token = NULL;
24859 while (true)
24861 attribute = cp_parser_std_attribute (parser, attr_ns);
24862 if (attribute == error_mark_node)
24863 break;
24864 if (attribute != NULL_TREE)
24866 cp_parser_check_std_attribute (attributes, attribute);
24867 TREE_CHAIN (attribute) = attributes;
24868 attributes = attribute;
24870 token = cp_lexer_peek_token (parser->lexer);
24871 if (token->type == CPP_ELLIPSIS)
24873 cp_lexer_consume_token (parser->lexer);
24874 if (attribute == NULL_TREE)
24875 error_at (token->location,
24876 "expected attribute before %<...%>");
24877 else
24879 tree pack = make_pack_expansion (TREE_VALUE (attribute));
24880 if (pack == error_mark_node)
24881 return error_mark_node;
24882 TREE_VALUE (attribute) = pack;
24884 token = cp_lexer_peek_token (parser->lexer);
24886 if (token->type != CPP_COMMA)
24887 break;
24888 cp_lexer_consume_token (parser->lexer);
24890 attributes = nreverse (attributes);
24891 return attributes;
24894 /* Parse a standard C++-11 attribute specifier.
24896 attribute-specifier:
24897 [ [ attribute-using-prefix [opt] attribute-list ] ]
24898 alignment-specifier
24900 attribute-using-prefix:
24901 using attribute-namespace :
24903 alignment-specifier:
24904 alignas ( type-id ... [opt] )
24905 alignas ( alignment-expression ... [opt] ). */
24907 static tree
24908 cp_parser_std_attribute_spec (cp_parser *parser)
24910 tree attributes = NULL_TREE;
24911 cp_token *token = cp_lexer_peek_token (parser->lexer);
24913 if (token->type == CPP_OPEN_SQUARE
24914 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24916 tree attr_ns = NULL_TREE;
24918 cp_lexer_consume_token (parser->lexer);
24919 cp_lexer_consume_token (parser->lexer);
24921 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24923 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24924 if (token->type == CPP_NAME)
24925 attr_ns = token->u.value;
24926 else if (token->type == CPP_KEYWORD)
24927 attr_ns = ridpointers[(int) token->keyword];
24928 else if (token->flags & NAMED_OP)
24929 attr_ns = get_identifier (cpp_type2name (token->type,
24930 token->flags));
24931 if (attr_ns
24932 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
24934 if (cxx_dialect < cxx1z
24935 && !in_system_header_at (input_location))
24936 pedwarn (input_location, 0,
24937 "attribute using prefix only available "
24938 "with -std=c++1z or -std=gnu++1z");
24940 cp_lexer_consume_token (parser->lexer);
24941 cp_lexer_consume_token (parser->lexer);
24942 cp_lexer_consume_token (parser->lexer);
24944 else
24945 attr_ns = NULL_TREE;
24948 attributes = cp_parser_std_attribute_list (parser, attr_ns);
24950 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24951 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24952 cp_parser_skip_to_end_of_statement (parser);
24953 else
24954 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24955 when we are sure that we have actually parsed them. */
24956 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24958 else
24960 tree alignas_expr;
24962 /* Look for an alignment-specifier. */
24964 token = cp_lexer_peek_token (parser->lexer);
24966 if (token->type != CPP_KEYWORD
24967 || token->keyword != RID_ALIGNAS)
24968 return NULL_TREE;
24970 cp_lexer_consume_token (parser->lexer);
24971 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24973 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24975 cp_parser_error (parser, "expected %<(%>");
24976 return error_mark_node;
24979 cp_parser_parse_tentatively (parser);
24980 alignas_expr = cp_parser_type_id (parser);
24982 if (!cp_parser_parse_definitely (parser))
24984 alignas_expr = cp_parser_assignment_expression (parser);
24985 if (alignas_expr == error_mark_node)
24986 cp_parser_skip_to_end_of_statement (parser);
24987 if (alignas_expr == NULL_TREE
24988 || alignas_expr == error_mark_node)
24989 return alignas_expr;
24992 alignas_expr = cxx_alignas_expr (alignas_expr);
24993 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24995 /* Handle alignas (pack...). */
24996 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24998 cp_lexer_consume_token (parser->lexer);
24999 alignas_expr = make_pack_expansion (alignas_expr);
25002 /* Something went wrong, so don't build the attribute. */
25003 if (alignas_expr == error_mark_node)
25004 return error_mark_node;
25006 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
25008 cp_parser_error (parser, "expected %<)%>");
25009 return error_mark_node;
25012 /* Build the C++-11 representation of an 'aligned'
25013 attribute. */
25014 attributes =
25015 build_tree_list (build_tree_list (get_identifier ("gnu"),
25016 get_identifier ("aligned")),
25017 alignas_expr);
25020 return attributes;
25023 /* Parse a standard C++-11 attribute-specifier-seq.
25025 attribute-specifier-seq:
25026 attribute-specifier-seq [opt] attribute-specifier
25029 static tree
25030 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25032 tree attr_specs = NULL_TREE;
25033 tree attr_last = NULL_TREE;
25035 while (true)
25037 tree attr_spec = cp_parser_std_attribute_spec (parser);
25038 if (attr_spec == NULL_TREE)
25039 break;
25040 if (attr_spec == error_mark_node)
25041 return error_mark_node;
25043 if (attr_last)
25044 TREE_CHAIN (attr_last) = attr_spec;
25045 else
25046 attr_specs = attr_last = attr_spec;
25047 attr_last = tree_last (attr_last);
25050 return attr_specs;
25053 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25054 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25055 current value of the PEDANTIC flag, regardless of whether or not
25056 the `__extension__' keyword is present. The caller is responsible
25057 for restoring the value of the PEDANTIC flag. */
25059 static bool
25060 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25062 /* Save the old value of the PEDANTIC flag. */
25063 *saved_pedantic = pedantic;
25065 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25067 /* Consume the `__extension__' token. */
25068 cp_lexer_consume_token (parser->lexer);
25069 /* We're not being pedantic while the `__extension__' keyword is
25070 in effect. */
25071 pedantic = 0;
25073 return true;
25076 return false;
25079 /* Parse a label declaration.
25081 label-declaration:
25082 __label__ label-declarator-seq ;
25084 label-declarator-seq:
25085 identifier , label-declarator-seq
25086 identifier */
25088 static void
25089 cp_parser_label_declaration (cp_parser* parser)
25091 /* Look for the `__label__' keyword. */
25092 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25094 while (true)
25096 tree identifier;
25098 /* Look for an identifier. */
25099 identifier = cp_parser_identifier (parser);
25100 /* If we failed, stop. */
25101 if (identifier == error_mark_node)
25102 break;
25103 /* Declare it as a label. */
25104 finish_label_decl (identifier);
25105 /* If the next token is a `;', stop. */
25106 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25107 break;
25108 /* Look for the `,' separating the label declarations. */
25109 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25112 /* Look for the final `;'. */
25113 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25116 // -------------------------------------------------------------------------- //
25117 // Requires Clause
25119 // Parse a requires clause.
25121 // requires-clause:
25122 // 'requires' logical-or-expression
25124 // The required logical-or-expression must be a constant expression. Note
25125 // that we don't check that the expression is constepxr here. We defer until
25126 // we analyze constraints and then, we only check atomic constraints.
25127 static tree
25128 cp_parser_requires_clause (cp_parser *parser)
25130 // Parse the requires clause so that it is not automatically folded.
25131 ++processing_template_decl;
25132 tree expr = cp_parser_binary_expression (parser, false, false,
25133 PREC_NOT_OPERATOR, NULL);
25134 if (check_for_bare_parameter_packs (expr))
25135 expr = error_mark_node;
25136 --processing_template_decl;
25137 return expr;
25140 // Optionally parse a requires clause:
25141 static tree
25142 cp_parser_requires_clause_opt (cp_parser *parser)
25144 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25145 if (tok->keyword != RID_REQUIRES)
25147 if (!flag_concepts && tok->type == CPP_NAME
25148 && tok->u.value == ridpointers[RID_REQUIRES])
25150 error_at (cp_lexer_peek_token (parser->lexer)->location,
25151 "%<requires%> only available with -fconcepts");
25152 /* Parse and discard the requires-clause. */
25153 cp_lexer_consume_token (parser->lexer);
25154 cp_parser_requires_clause (parser);
25156 return NULL_TREE;
25158 cp_lexer_consume_token (parser->lexer);
25159 return cp_parser_requires_clause (parser);
25163 /*---------------------------------------------------------------------------
25164 Requires expressions
25165 ---------------------------------------------------------------------------*/
25167 /* Parse a requires expression
25169 requirement-expression:
25170 'requires' requirement-parameter-list [opt] requirement-body */
25171 static tree
25172 cp_parser_requires_expression (cp_parser *parser)
25174 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25175 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25177 /* A requires-expression shall appear only within a concept
25178 definition or a requires-clause.
25180 TODO: Implement this diagnostic correctly. */
25181 if (!processing_template_decl)
25183 error_at (loc, "a requires expression cannot appear outside a template");
25184 cp_parser_skip_to_end_of_statement (parser);
25185 return error_mark_node;
25188 tree parms, reqs;
25190 /* Local parameters are delared as variables within the scope
25191 of the expression. They are not visible past the end of
25192 the expression. Expressions within the requires-expression
25193 are unevaluated. */
25194 struct scope_sentinel
25196 scope_sentinel ()
25198 ++cp_unevaluated_operand;
25199 begin_scope (sk_block, NULL_TREE);
25202 ~scope_sentinel ()
25204 pop_bindings_and_leave_scope ();
25205 --cp_unevaluated_operand;
25207 } s;
25209 /* Parse the optional parameter list. */
25210 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25212 parms = cp_parser_requirement_parameter_list (parser);
25213 if (parms == error_mark_node)
25214 return error_mark_node;
25216 else
25217 parms = NULL_TREE;
25219 /* Parse the requirement body. */
25220 reqs = cp_parser_requirement_body (parser);
25221 if (reqs == error_mark_node)
25222 return error_mark_node;
25225 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25226 the parm chain. */
25227 grokparms (parms, &parms);
25228 return finish_requires_expr (parms, reqs);
25231 /* Parse a parameterized requirement.
25233 requirement-parameter-list:
25234 '(' parameter-declaration-clause ')' */
25235 static tree
25236 cp_parser_requirement_parameter_list (cp_parser *parser)
25238 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25239 return error_mark_node;
25241 tree parms = cp_parser_parameter_declaration_clause (parser);
25243 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25244 return error_mark_node;
25246 return parms;
25249 /* Parse the body of a requirement.
25251 requirement-body:
25252 '{' requirement-list '}' */
25253 static tree
25254 cp_parser_requirement_body (cp_parser *parser)
25256 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25257 return error_mark_node;
25259 tree reqs = cp_parser_requirement_list (parser);
25261 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25262 return error_mark_node;
25264 return reqs;
25267 /* Parse a list of requirements.
25269 requirement-list:
25270 requirement
25271 requirement-list ';' requirement[opt] */
25272 static tree
25273 cp_parser_requirement_list (cp_parser *parser)
25275 tree result = NULL_TREE;
25276 while (true)
25278 tree req = cp_parser_requirement (parser);
25279 if (req == error_mark_node)
25280 return error_mark_node;
25282 result = tree_cons (NULL_TREE, req, result);
25284 /* If we see a semi-colon, consume it. */
25285 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25286 cp_lexer_consume_token (parser->lexer);
25288 /* Stop processing at the end of the list. */
25289 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25290 break;
25293 /* Reverse the order of requirements so they are analyzed in
25294 declaration order. */
25295 return nreverse (result);
25298 /* Parse a syntactic requirement or type requirement.
25300 requirement:
25301 simple-requirement
25302 compound-requirement
25303 type-requirement
25304 nested-requirement */
25305 static tree
25306 cp_parser_requirement (cp_parser *parser)
25308 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25309 return cp_parser_compound_requirement (parser);
25310 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25311 return cp_parser_type_requirement (parser);
25312 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25313 return cp_parser_nested_requirement (parser);
25314 else
25315 return cp_parser_simple_requirement (parser);
25318 /* Parse a simple requirement.
25320 simple-requirement:
25321 expression ';' */
25322 static tree
25323 cp_parser_simple_requirement (cp_parser *parser)
25325 tree expr = cp_parser_expression (parser, NULL, false, false);
25326 if (!expr || expr == error_mark_node)
25327 return error_mark_node;
25329 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25330 return error_mark_node;
25332 return finish_simple_requirement (expr);
25335 /* Parse a type requirement
25337 type-requirement
25338 nested-name-specifier [opt] required-type-name ';'
25340 required-type-name:
25341 type-name
25342 'template' [opt] simple-template-id */
25343 static tree
25344 cp_parser_type_requirement (cp_parser *parser)
25346 cp_lexer_consume_token (parser->lexer);
25348 // Save the scope before parsing name specifiers.
25349 tree saved_scope = parser->scope;
25350 tree saved_object_scope = parser->object_scope;
25351 tree saved_qualifying_scope = parser->qualifying_scope;
25352 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25353 cp_parser_nested_name_specifier_opt (parser,
25354 /*typename_keyword_p=*/true,
25355 /*check_dependency_p=*/false,
25356 /*type_p=*/true,
25357 /*is_declaration=*/false);
25359 tree type;
25360 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25362 cp_lexer_consume_token (parser->lexer);
25363 type = cp_parser_template_id (parser,
25364 /*template_keyword_p=*/true,
25365 /*check_dependency=*/false,
25366 /*tag_type=*/none_type,
25367 /*is_declaration=*/false);
25368 type = make_typename_type (parser->scope, type, typename_type,
25369 /*complain=*/tf_error);
25371 else
25372 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25374 if (TREE_CODE (type) == TYPE_DECL)
25375 type = TREE_TYPE (type);
25377 parser->scope = saved_scope;
25378 parser->object_scope = saved_object_scope;
25379 parser->qualifying_scope = saved_qualifying_scope;
25381 if (type == error_mark_node)
25382 cp_parser_skip_to_end_of_statement (parser);
25384 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25385 return error_mark_node;
25386 if (type == error_mark_node)
25387 return error_mark_node;
25389 return finish_type_requirement (type);
25392 /* Parse a compound requirement
25394 compound-requirement:
25395 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25396 static tree
25397 cp_parser_compound_requirement (cp_parser *parser)
25399 /* Parse an expression enclosed in '{ }'s. */
25400 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25401 return error_mark_node;
25403 tree expr = cp_parser_expression (parser, NULL, false, false);
25404 if (!expr || expr == error_mark_node)
25405 return error_mark_node;
25407 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25408 return error_mark_node;
25410 /* Parse the optional noexcept. */
25411 bool noexcept_p = false;
25412 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25414 cp_lexer_consume_token (parser->lexer);
25415 noexcept_p = true;
25418 /* Parse the optional trailing return type. */
25419 tree type = NULL_TREE;
25420 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25422 cp_lexer_consume_token (parser->lexer);
25423 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25424 parser->in_result_type_constraint_p = true;
25425 type = cp_parser_trailing_type_id (parser);
25426 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25427 if (type == error_mark_node)
25428 return error_mark_node;
25431 return finish_compound_requirement (expr, type, noexcept_p);
25434 /* Parse a nested requirement. This is the same as a requires clause.
25436 nested-requirement:
25437 requires-clause */
25438 static tree
25439 cp_parser_nested_requirement (cp_parser *parser)
25441 cp_lexer_consume_token (parser->lexer);
25442 tree req = cp_parser_requires_clause (parser);
25443 if (req == error_mark_node)
25444 return error_mark_node;
25445 return finish_nested_requirement (req);
25448 /* Support Functions */
25450 /* Return the appropriate prefer_type argument for lookup_name_real based on
25451 tag_type and template_mem_access. */
25453 static inline int
25454 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25456 /* DR 141: When looking in the current enclosing context for a template-name
25457 after -> or ., only consider class templates. */
25458 if (template_mem_access)
25459 return 2;
25460 switch (tag_type)
25462 case none_type: return 0; // No preference.
25463 case scope_type: return 1; // Type or namespace.
25464 default: return 2; // Type only.
25468 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25469 NAME should have one of the representations used for an
25470 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25471 is returned. If PARSER->SCOPE is a dependent type, then a
25472 SCOPE_REF is returned.
25474 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25475 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25476 was formed. Abstractly, such entities should not be passed to this
25477 function, because they do not need to be looked up, but it is
25478 simpler to check for this special case here, rather than at the
25479 call-sites.
25481 In cases not explicitly covered above, this function returns a
25482 DECL, OVERLOAD, or baselink representing the result of the lookup.
25483 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25484 is returned.
25486 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25487 (e.g., "struct") that was used. In that case bindings that do not
25488 refer to types are ignored.
25490 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25491 ignored.
25493 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25494 are ignored.
25496 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25497 types.
25499 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25500 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25501 NULL_TREE otherwise. */
25503 static cp_expr
25504 cp_parser_lookup_name (cp_parser *parser, tree name,
25505 enum tag_types tag_type,
25506 bool is_template,
25507 bool is_namespace,
25508 bool check_dependency,
25509 tree *ambiguous_decls,
25510 location_t name_location)
25512 tree decl;
25513 tree object_type = parser->context->object_type;
25515 /* Assume that the lookup will be unambiguous. */
25516 if (ambiguous_decls)
25517 *ambiguous_decls = NULL_TREE;
25519 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25520 no longer valid. Note that if we are parsing tentatively, and
25521 the parse fails, OBJECT_TYPE will be automatically restored. */
25522 parser->context->object_type = NULL_TREE;
25524 if (name == error_mark_node)
25525 return error_mark_node;
25527 /* A template-id has already been resolved; there is no lookup to
25528 do. */
25529 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25530 return name;
25531 if (BASELINK_P (name))
25533 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25534 == TEMPLATE_ID_EXPR);
25535 return name;
25538 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25539 it should already have been checked to make sure that the name
25540 used matches the type being destroyed. */
25541 if (TREE_CODE (name) == BIT_NOT_EXPR)
25543 tree type;
25545 /* Figure out to which type this destructor applies. */
25546 if (parser->scope)
25547 type = parser->scope;
25548 else if (object_type)
25549 type = object_type;
25550 else
25551 type = current_class_type;
25552 /* If that's not a class type, there is no destructor. */
25553 if (!type || !CLASS_TYPE_P (type))
25554 return error_mark_node;
25555 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25556 lazily_declare_fn (sfk_destructor, type);
25557 if (!CLASSTYPE_DESTRUCTORS (type))
25558 return error_mark_node;
25559 /* If it was a class type, return the destructor. */
25560 return CLASSTYPE_DESTRUCTORS (type);
25563 /* By this point, the NAME should be an ordinary identifier. If
25564 the id-expression was a qualified name, the qualifying scope is
25565 stored in PARSER->SCOPE at this point. */
25566 gcc_assert (identifier_p (name));
25568 /* Perform the lookup. */
25569 if (parser->scope)
25571 bool dependent_p;
25573 if (parser->scope == error_mark_node)
25574 return error_mark_node;
25576 /* If the SCOPE is dependent, the lookup must be deferred until
25577 the template is instantiated -- unless we are explicitly
25578 looking up names in uninstantiated templates. Even then, we
25579 cannot look up the name if the scope is not a class type; it
25580 might, for example, be a template type parameter. */
25581 dependent_p = (TYPE_P (parser->scope)
25582 && dependent_scope_p (parser->scope));
25583 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25584 && dependent_p)
25585 /* Defer lookup. */
25586 decl = error_mark_node;
25587 else
25589 tree pushed_scope = NULL_TREE;
25591 /* If PARSER->SCOPE is a dependent type, then it must be a
25592 class type, and we must not be checking dependencies;
25593 otherwise, we would have processed this lookup above. So
25594 that PARSER->SCOPE is not considered a dependent base by
25595 lookup_member, we must enter the scope here. */
25596 if (dependent_p)
25597 pushed_scope = push_scope (parser->scope);
25599 /* If the PARSER->SCOPE is a template specialization, it
25600 may be instantiated during name lookup. In that case,
25601 errors may be issued. Even if we rollback the current
25602 tentative parse, those errors are valid. */
25603 decl = lookup_qualified_name (parser->scope, name,
25604 prefer_type_arg (tag_type),
25605 /*complain=*/true);
25607 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25608 lookup result and the nested-name-specifier nominates a class C:
25609 * if the name specified after the nested-name-specifier, when
25610 looked up in C, is the injected-class-name of C (Clause 9), or
25611 * if the name specified after the nested-name-specifier is the
25612 same as the identifier or the simple-template-id's template-
25613 name in the last component of the nested-name-specifier,
25614 the name is instead considered to name the constructor of
25615 class C. [ Note: for example, the constructor is not an
25616 acceptable lookup result in an elaborated-type-specifier so
25617 the constructor would not be used in place of the
25618 injected-class-name. --end note ] Such a constructor name
25619 shall be used only in the declarator-id of a declaration that
25620 names a constructor or in a using-declaration. */
25621 if (tag_type == none_type
25622 && DECL_SELF_REFERENCE_P (decl)
25623 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25624 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25625 prefer_type_arg (tag_type),
25626 /*complain=*/true);
25628 /* If we have a single function from a using decl, pull it out. */
25629 if (TREE_CODE (decl) == OVERLOAD
25630 && !really_overloaded_fn (decl))
25631 decl = OVL_FUNCTION (decl);
25633 if (pushed_scope)
25634 pop_scope (pushed_scope);
25637 /* If the scope is a dependent type and either we deferred lookup or
25638 we did lookup but didn't find the name, rememeber the name. */
25639 if (decl == error_mark_node && TYPE_P (parser->scope)
25640 && dependent_type_p (parser->scope))
25642 if (tag_type)
25644 tree type;
25646 /* The resolution to Core Issue 180 says that `struct
25647 A::B' should be considered a type-name, even if `A'
25648 is dependent. */
25649 type = make_typename_type (parser->scope, name, tag_type,
25650 /*complain=*/tf_error);
25651 if (type != error_mark_node)
25652 decl = TYPE_NAME (type);
25654 else if (is_template
25655 && (cp_parser_next_token_ends_template_argument_p (parser)
25656 || cp_lexer_next_token_is (parser->lexer,
25657 CPP_CLOSE_PAREN)))
25658 decl = make_unbound_class_template (parser->scope,
25659 name, NULL_TREE,
25660 /*complain=*/tf_error);
25661 else
25662 decl = build_qualified_name (/*type=*/NULL_TREE,
25663 parser->scope, name,
25664 is_template);
25666 parser->qualifying_scope = parser->scope;
25667 parser->object_scope = NULL_TREE;
25669 else if (object_type)
25671 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25672 OBJECT_TYPE is not a class. */
25673 if (CLASS_TYPE_P (object_type))
25674 /* If the OBJECT_TYPE is a template specialization, it may
25675 be instantiated during name lookup. In that case, errors
25676 may be issued. Even if we rollback the current tentative
25677 parse, those errors are valid. */
25678 decl = lookup_member (object_type,
25679 name,
25680 /*protect=*/0,
25681 prefer_type_arg (tag_type),
25682 tf_warning_or_error);
25683 else
25684 decl = NULL_TREE;
25686 if (!decl)
25687 /* Look it up in the enclosing context. DR 141: When looking for a
25688 template-name after -> or ., only consider class templates. */
25689 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25690 /*nonclass=*/0,
25691 /*block_p=*/true, is_namespace, 0);
25692 if (object_type == unknown_type_node)
25693 /* The object is type-dependent, so we can't look anything up; we used
25694 this to get the DR 141 behavior. */
25695 object_type = NULL_TREE;
25696 parser->object_scope = object_type;
25697 parser->qualifying_scope = NULL_TREE;
25699 else
25701 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25702 /*nonclass=*/0,
25703 /*block_p=*/true, is_namespace, 0);
25704 parser->qualifying_scope = NULL_TREE;
25705 parser->object_scope = NULL_TREE;
25708 /* If the lookup failed, let our caller know. */
25709 if (!decl || decl == error_mark_node)
25710 return error_mark_node;
25712 /* Pull out the template from an injected-class-name (or multiple). */
25713 if (is_template)
25714 decl = maybe_get_template_decl_from_type_decl (decl);
25716 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25717 if (TREE_CODE (decl) == TREE_LIST)
25719 if (ambiguous_decls)
25720 *ambiguous_decls = decl;
25721 /* The error message we have to print is too complicated for
25722 cp_parser_error, so we incorporate its actions directly. */
25723 if (!cp_parser_simulate_error (parser))
25725 error_at (name_location, "reference to %qD is ambiguous",
25726 name);
25727 print_candidates (decl);
25729 return error_mark_node;
25732 gcc_assert (DECL_P (decl)
25733 || TREE_CODE (decl) == OVERLOAD
25734 || TREE_CODE (decl) == SCOPE_REF
25735 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25736 || BASELINK_P (decl));
25738 /* If we have resolved the name of a member declaration, check to
25739 see if the declaration is accessible. When the name resolves to
25740 set of overloaded functions, accessibility is checked when
25741 overload resolution is done.
25743 During an explicit instantiation, access is not checked at all,
25744 as per [temp.explicit]. */
25745 if (DECL_P (decl))
25746 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25748 maybe_record_typedef_use (decl);
25750 return cp_expr (decl, name_location);
25753 /* Like cp_parser_lookup_name, but for use in the typical case where
25754 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25755 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25757 static tree
25758 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25760 return cp_parser_lookup_name (parser, name,
25761 none_type,
25762 /*is_template=*/false,
25763 /*is_namespace=*/false,
25764 /*check_dependency=*/true,
25765 /*ambiguous_decls=*/NULL,
25766 location);
25769 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25770 the current context, return the TYPE_DECL. If TAG_NAME_P is
25771 true, the DECL indicates the class being defined in a class-head,
25772 or declared in an elaborated-type-specifier.
25774 Otherwise, return DECL. */
25776 static tree
25777 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25779 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25780 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25782 struct A {
25783 template <typename T> struct B;
25786 template <typename T> struct A::B {};
25788 Similarly, in an elaborated-type-specifier:
25790 namespace N { struct X{}; }
25792 struct A {
25793 template <typename T> friend struct N::X;
25796 However, if the DECL refers to a class type, and we are in
25797 the scope of the class, then the name lookup automatically
25798 finds the TYPE_DECL created by build_self_reference rather
25799 than a TEMPLATE_DECL. For example, in:
25801 template <class T> struct S {
25802 S s;
25805 there is no need to handle such case. */
25807 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25808 return DECL_TEMPLATE_RESULT (decl);
25810 return decl;
25813 /* If too many, or too few, template-parameter lists apply to the
25814 declarator, issue an error message. Returns TRUE if all went well,
25815 and FALSE otherwise. */
25817 static bool
25818 cp_parser_check_declarator_template_parameters (cp_parser* parser,
25819 cp_declarator *declarator,
25820 location_t declarator_location)
25822 switch (declarator->kind)
25824 case cdk_id:
25826 unsigned num_templates = 0;
25827 tree scope = declarator->u.id.qualifying_scope;
25829 if (scope)
25830 num_templates = num_template_headers_for_class (scope);
25831 else if (TREE_CODE (declarator->u.id.unqualified_name)
25832 == TEMPLATE_ID_EXPR)
25833 /* If the DECLARATOR has the form `X<y>' then it uses one
25834 additional level of template parameters. */
25835 ++num_templates;
25837 return cp_parser_check_template_parameters
25838 (parser, num_templates, declarator_location, declarator);
25841 case cdk_function:
25842 case cdk_array:
25843 case cdk_pointer:
25844 case cdk_reference:
25845 case cdk_ptrmem:
25846 return (cp_parser_check_declarator_template_parameters
25847 (parser, declarator->declarator, declarator_location));
25849 case cdk_decomp:
25850 case cdk_error:
25851 return true;
25853 default:
25854 gcc_unreachable ();
25856 return false;
25859 /* NUM_TEMPLATES were used in the current declaration. If that is
25860 invalid, return FALSE and issue an error messages. Otherwise,
25861 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25862 declarator and we can print more accurate diagnostics. */
25864 static bool
25865 cp_parser_check_template_parameters (cp_parser* parser,
25866 unsigned num_templates,
25867 location_t location,
25868 cp_declarator *declarator)
25870 /* If there are the same number of template classes and parameter
25871 lists, that's OK. */
25872 if (parser->num_template_parameter_lists == num_templates)
25873 return true;
25874 /* If there are more, but only one more, then we are referring to a
25875 member template. That's OK too. */
25876 if (parser->num_template_parameter_lists == num_templates + 1)
25877 return true;
25878 /* If there are more template classes than parameter lists, we have
25879 something like:
25881 template <class T> void S<T>::R<T>::f (); */
25882 if (parser->num_template_parameter_lists < num_templates)
25884 if (declarator && !current_function_decl)
25885 error_at (location, "specializing member %<%T::%E%> "
25886 "requires %<template<>%> syntax",
25887 declarator->u.id.qualifying_scope,
25888 declarator->u.id.unqualified_name);
25889 else if (declarator)
25890 error_at (location, "invalid declaration of %<%T::%E%>",
25891 declarator->u.id.qualifying_scope,
25892 declarator->u.id.unqualified_name);
25893 else
25894 error_at (location, "too few template-parameter-lists");
25895 return false;
25897 /* Otherwise, there are too many template parameter lists. We have
25898 something like:
25900 template <class T> template <class U> void S::f(); */
25901 error_at (location, "too many template-parameter-lists");
25902 return false;
25905 /* Parse an optional `::' token indicating that the following name is
25906 from the global namespace. If so, PARSER->SCOPE is set to the
25907 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25908 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25909 Returns the new value of PARSER->SCOPE, if the `::' token is
25910 present, and NULL_TREE otherwise. */
25912 static tree
25913 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25915 cp_token *token;
25917 /* Peek at the next token. */
25918 token = cp_lexer_peek_token (parser->lexer);
25919 /* If we're looking at a `::' token then we're starting from the
25920 global namespace, not our current location. */
25921 if (token->type == CPP_SCOPE)
25923 /* Consume the `::' token. */
25924 cp_lexer_consume_token (parser->lexer);
25925 /* Set the SCOPE so that we know where to start the lookup. */
25926 parser->scope = global_namespace;
25927 parser->qualifying_scope = global_namespace;
25928 parser->object_scope = NULL_TREE;
25930 return parser->scope;
25932 else if (!current_scope_valid_p)
25934 parser->scope = NULL_TREE;
25935 parser->qualifying_scope = NULL_TREE;
25936 parser->object_scope = NULL_TREE;
25939 return NULL_TREE;
25942 /* Returns TRUE if the upcoming token sequence is the start of a
25943 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
25944 declarator is preceded by the `friend' specifier. */
25946 static bool
25947 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25949 bool constructor_p;
25950 bool outside_class_specifier_p;
25951 tree nested_name_specifier;
25952 cp_token *next_token;
25954 /* The common case is that this is not a constructor declarator, so
25955 try to avoid doing lots of work if at all possible. It's not
25956 valid declare a constructor at function scope. */
25957 if (parser->in_function_body)
25958 return false;
25959 /* And only certain tokens can begin a constructor declarator. */
25960 next_token = cp_lexer_peek_token (parser->lexer);
25961 if (next_token->type != CPP_NAME
25962 && next_token->type != CPP_SCOPE
25963 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25964 && next_token->type != CPP_TEMPLATE_ID)
25965 return false;
25967 /* Parse tentatively; we are going to roll back all of the tokens
25968 consumed here. */
25969 cp_parser_parse_tentatively (parser);
25970 /* Assume that we are looking at a constructor declarator. */
25971 constructor_p = true;
25973 /* Look for the optional `::' operator. */
25974 cp_parser_global_scope_opt (parser,
25975 /*current_scope_valid_p=*/false);
25976 /* Look for the nested-name-specifier. */
25977 nested_name_specifier
25978 = (cp_parser_nested_name_specifier_opt (parser,
25979 /*typename_keyword_p=*/false,
25980 /*check_dependency_p=*/false,
25981 /*type_p=*/false,
25982 /*is_declaration=*/false));
25984 outside_class_specifier_p = (!at_class_scope_p ()
25985 || !TYPE_BEING_DEFINED (current_class_type)
25986 || friend_p);
25988 /* Outside of a class-specifier, there must be a
25989 nested-name-specifier. Except in C++17 mode, where we
25990 might be declaring a guiding declaration. */
25991 if (!nested_name_specifier && outside_class_specifier_p
25992 && cxx_dialect < cxx1z)
25993 constructor_p = false;
25994 else if (nested_name_specifier == error_mark_node)
25995 constructor_p = false;
25997 /* If we have a class scope, this is easy; DR 147 says that S::S always
25998 names the constructor, and no other qualified name could. */
25999 if (constructor_p && nested_name_specifier
26000 && CLASS_TYPE_P (nested_name_specifier))
26002 tree id = cp_parser_unqualified_id (parser,
26003 /*template_keyword_p=*/false,
26004 /*check_dependency_p=*/false,
26005 /*declarator_p=*/true,
26006 /*optional_p=*/false);
26007 if (is_overloaded_fn (id))
26008 id = DECL_NAME (get_first_fn (id));
26009 if (!constructor_name_p (id, nested_name_specifier))
26010 constructor_p = false;
26012 /* If we still think that this might be a constructor-declarator,
26013 look for a class-name. */
26014 else if (constructor_p)
26016 /* If we have:
26018 template <typename T> struct S {
26019 S();
26022 we must recognize that the nested `S' names a class. */
26023 if (cxx_dialect >= cxx1z)
26024 cp_parser_parse_tentatively (parser);
26026 tree type_decl;
26027 type_decl = cp_parser_class_name (parser,
26028 /*typename_keyword_p=*/false,
26029 /*template_keyword_p=*/false,
26030 none_type,
26031 /*check_dependency_p=*/false,
26032 /*class_head_p=*/false,
26033 /*is_declaration=*/false);
26035 if (cxx_dialect >= cxx1z
26036 && !cp_parser_parse_definitely (parser))
26038 type_decl = NULL_TREE;
26039 tree tmpl = cp_parser_template_name (parser,
26040 /*template_keyword*/false,
26041 /*check_dependency_p*/false,
26042 /*is_declaration*/false,
26043 none_type,
26044 /*is_identifier*/NULL);
26045 if (DECL_CLASS_TEMPLATE_P (tmpl)
26046 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26047 /* It's a deduction guide, return true. */;
26048 else
26049 cp_parser_simulate_error (parser);
26052 /* If there was no class-name, then this is not a constructor.
26053 Otherwise, if we are in a class-specifier and we aren't
26054 handling a friend declaration, check that its type matches
26055 current_class_type (c++/38313). Note: error_mark_node
26056 is left alone for error recovery purposes. */
26057 constructor_p = (!cp_parser_error_occurred (parser)
26058 && (outside_class_specifier_p
26059 || type_decl == NULL_TREE
26060 || type_decl == error_mark_node
26061 || same_type_p (current_class_type,
26062 TREE_TYPE (type_decl))));
26064 /* If we're still considering a constructor, we have to see a `(',
26065 to begin the parameter-declaration-clause, followed by either a
26066 `)', an `...', or a decl-specifier. We need to check for a
26067 type-specifier to avoid being fooled into thinking that:
26069 S (f) (int);
26071 is a constructor. (It is actually a function named `f' that
26072 takes one parameter (of type `int') and returns a value of type
26073 `S'. */
26074 if (constructor_p
26075 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26076 constructor_p = false;
26078 if (constructor_p
26079 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26080 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26081 /* A parameter declaration begins with a decl-specifier,
26082 which is either the "attribute" keyword, a storage class
26083 specifier, or (usually) a type-specifier. */
26084 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26086 tree type;
26087 tree pushed_scope = NULL_TREE;
26088 unsigned saved_num_template_parameter_lists;
26090 /* Names appearing in the type-specifier should be looked up
26091 in the scope of the class. */
26092 if (current_class_type)
26093 type = NULL_TREE;
26094 else if (type_decl)
26096 type = TREE_TYPE (type_decl);
26097 if (TREE_CODE (type) == TYPENAME_TYPE)
26099 type = resolve_typename_type (type,
26100 /*only_current_p=*/false);
26101 if (TREE_CODE (type) == TYPENAME_TYPE)
26103 cp_parser_abort_tentative_parse (parser);
26104 return false;
26107 pushed_scope = push_scope (type);
26110 /* Inside the constructor parameter list, surrounding
26111 template-parameter-lists do not apply. */
26112 saved_num_template_parameter_lists
26113 = parser->num_template_parameter_lists;
26114 parser->num_template_parameter_lists = 0;
26116 /* Look for the type-specifier. */
26117 cp_parser_type_specifier (parser,
26118 CP_PARSER_FLAGS_NONE,
26119 /*decl_specs=*/NULL,
26120 /*is_declarator=*/true,
26121 /*declares_class_or_enum=*/NULL,
26122 /*is_cv_qualifier=*/NULL);
26124 parser->num_template_parameter_lists
26125 = saved_num_template_parameter_lists;
26127 /* Leave the scope of the class. */
26128 if (pushed_scope)
26129 pop_scope (pushed_scope);
26131 constructor_p = !cp_parser_error_occurred (parser);
26135 /* We did not really want to consume any tokens. */
26136 cp_parser_abort_tentative_parse (parser);
26138 return constructor_p;
26141 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26142 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26143 they must be performed once we are in the scope of the function.
26145 Returns the function defined. */
26147 static tree
26148 cp_parser_function_definition_from_specifiers_and_declarator
26149 (cp_parser* parser,
26150 cp_decl_specifier_seq *decl_specifiers,
26151 tree attributes,
26152 const cp_declarator *declarator)
26154 tree fn;
26155 bool success_p;
26157 /* Begin the function-definition. */
26158 success_p = start_function (decl_specifiers, declarator, attributes);
26160 /* The things we're about to see are not directly qualified by any
26161 template headers we've seen thus far. */
26162 reset_specialization ();
26164 /* If there were names looked up in the decl-specifier-seq that we
26165 did not check, check them now. We must wait until we are in the
26166 scope of the function to perform the checks, since the function
26167 might be a friend. */
26168 perform_deferred_access_checks (tf_warning_or_error);
26170 if (success_p)
26172 cp_finalize_omp_declare_simd (parser, current_function_decl);
26173 parser->omp_declare_simd = NULL;
26174 cp_finalize_oacc_routine (parser, current_function_decl, true);
26175 parser->oacc_routine = NULL;
26178 if (!success_p)
26180 /* Skip the entire function. */
26181 cp_parser_skip_to_end_of_block_or_statement (parser);
26182 fn = error_mark_node;
26184 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26186 /* Seen already, skip it. An error message has already been output. */
26187 cp_parser_skip_to_end_of_block_or_statement (parser);
26188 fn = current_function_decl;
26189 current_function_decl = NULL_TREE;
26190 /* If this is a function from a class, pop the nested class. */
26191 if (current_class_name)
26192 pop_nested_class ();
26194 else
26196 timevar_id_t tv;
26197 if (DECL_DECLARED_INLINE_P (current_function_decl))
26198 tv = TV_PARSE_INLINE;
26199 else
26200 tv = TV_PARSE_FUNC;
26201 timevar_push (tv);
26202 fn = cp_parser_function_definition_after_declarator (parser,
26203 /*inline_p=*/false);
26204 timevar_pop (tv);
26207 return fn;
26210 /* Parse the part of a function-definition that follows the
26211 declarator. INLINE_P is TRUE iff this function is an inline
26212 function defined within a class-specifier.
26214 Returns the function defined. */
26216 static tree
26217 cp_parser_function_definition_after_declarator (cp_parser* parser,
26218 bool inline_p)
26220 tree fn;
26221 bool ctor_initializer_p = false;
26222 bool saved_in_unbraced_linkage_specification_p;
26223 bool saved_in_function_body;
26224 unsigned saved_num_template_parameter_lists;
26225 cp_token *token;
26226 bool fully_implicit_function_template_p
26227 = parser->fully_implicit_function_template_p;
26228 parser->fully_implicit_function_template_p = false;
26229 tree implicit_template_parms
26230 = parser->implicit_template_parms;
26231 parser->implicit_template_parms = 0;
26232 cp_binding_level* implicit_template_scope
26233 = parser->implicit_template_scope;
26234 parser->implicit_template_scope = 0;
26236 saved_in_function_body = parser->in_function_body;
26237 parser->in_function_body = true;
26238 /* If the next token is `return', then the code may be trying to
26239 make use of the "named return value" extension that G++ used to
26240 support. */
26241 token = cp_lexer_peek_token (parser->lexer);
26242 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26244 /* Consume the `return' keyword. */
26245 cp_lexer_consume_token (parser->lexer);
26246 /* Look for the identifier that indicates what value is to be
26247 returned. */
26248 cp_parser_identifier (parser);
26249 /* Issue an error message. */
26250 error_at (token->location,
26251 "named return values are no longer supported");
26252 /* Skip tokens until we reach the start of the function body. */
26253 while (true)
26255 cp_token *token = cp_lexer_peek_token (parser->lexer);
26256 if (token->type == CPP_OPEN_BRACE
26257 || token->type == CPP_EOF
26258 || token->type == CPP_PRAGMA_EOL)
26259 break;
26260 cp_lexer_consume_token (parser->lexer);
26263 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26264 anything declared inside `f'. */
26265 saved_in_unbraced_linkage_specification_p
26266 = parser->in_unbraced_linkage_specification_p;
26267 parser->in_unbraced_linkage_specification_p = false;
26268 /* Inside the function, surrounding template-parameter-lists do not
26269 apply. */
26270 saved_num_template_parameter_lists
26271 = parser->num_template_parameter_lists;
26272 parser->num_template_parameter_lists = 0;
26274 start_lambda_scope (current_function_decl);
26276 /* If the next token is `try', `__transaction_atomic', or
26277 `__transaction_relaxed`, then we are looking at either function-try-block
26278 or function-transaction-block. Note that all of these include the
26279 function-body. */
26280 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26281 ctor_initializer_p = cp_parser_function_transaction (parser,
26282 RID_TRANSACTION_ATOMIC);
26283 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26284 RID_TRANSACTION_RELAXED))
26285 ctor_initializer_p = cp_parser_function_transaction (parser,
26286 RID_TRANSACTION_RELAXED);
26287 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26288 ctor_initializer_p = cp_parser_function_try_block (parser);
26289 else
26290 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
26291 (parser, /*in_function_try_block=*/false);
26293 finish_lambda_scope ();
26295 /* Finish the function. */
26296 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
26297 (inline_p ? 2 : 0));
26298 /* Generate code for it, if necessary. */
26299 expand_or_defer_fn (fn);
26300 /* Restore the saved values. */
26301 parser->in_unbraced_linkage_specification_p
26302 = saved_in_unbraced_linkage_specification_p;
26303 parser->num_template_parameter_lists
26304 = saved_num_template_parameter_lists;
26305 parser->in_function_body = saved_in_function_body;
26307 parser->fully_implicit_function_template_p
26308 = fully_implicit_function_template_p;
26309 parser->implicit_template_parms
26310 = implicit_template_parms;
26311 parser->implicit_template_scope
26312 = implicit_template_scope;
26314 if (parser->fully_implicit_function_template_p)
26315 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26317 return fn;
26320 /* Parse a template-declaration body (following argument list). */
26322 static void
26323 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26324 tree parameter_list,
26325 bool member_p)
26327 tree decl = NULL_TREE;
26328 bool friend_p = false;
26330 /* We just processed one more parameter list. */
26331 ++parser->num_template_parameter_lists;
26333 /* Get the deferred access checks from the parameter list. These
26334 will be checked once we know what is being declared, as for a
26335 member template the checks must be performed in the scope of the
26336 class containing the member. */
26337 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26339 /* Tentatively parse for a new template parameter list, which can either be
26340 the template keyword or a template introduction. */
26341 if (cp_parser_template_declaration_after_export (parser, member_p))
26342 /* OK */;
26343 else if (cxx_dialect >= cxx11
26344 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26345 decl = cp_parser_alias_declaration (parser);
26346 else
26348 /* There are no access checks when parsing a template, as we do not
26349 know if a specialization will be a friend. */
26350 push_deferring_access_checks (dk_no_check);
26351 cp_token *token = cp_lexer_peek_token (parser->lexer);
26352 decl = cp_parser_single_declaration (parser,
26353 checks,
26354 member_p,
26355 /*explicit_specialization_p=*/false,
26356 &friend_p);
26357 pop_deferring_access_checks ();
26359 /* If this is a member template declaration, let the front
26360 end know. */
26361 if (member_p && !friend_p && decl)
26363 if (TREE_CODE (decl) == TYPE_DECL)
26364 cp_parser_check_access_in_redeclaration (decl, token->location);
26366 decl = finish_member_template_decl (decl);
26368 else if (friend_p && decl
26369 && DECL_DECLARES_TYPE_P (decl))
26370 make_friend_class (current_class_type, TREE_TYPE (decl),
26371 /*complain=*/true);
26373 /* We are done with the current parameter list. */
26374 --parser->num_template_parameter_lists;
26376 pop_deferring_access_checks ();
26378 /* Finish up. */
26379 finish_template_decl (parameter_list);
26381 /* Check the template arguments for a literal operator template. */
26382 if (decl
26383 && DECL_DECLARES_FUNCTION_P (decl)
26384 && UDLIT_OPER_P (DECL_NAME (decl)))
26386 bool ok = true;
26387 if (parameter_list == NULL_TREE)
26388 ok = false;
26389 else
26391 int num_parms = TREE_VEC_LENGTH (parameter_list);
26392 if (num_parms == 1)
26394 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26395 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26396 if (TREE_TYPE (parm) != char_type_node
26397 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26398 ok = false;
26400 else if (num_parms == 2 && cxx_dialect >= cxx14)
26402 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26403 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26404 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26405 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26406 if (parm == error_mark_node
26407 || TREE_TYPE (parm) != TREE_TYPE (type)
26408 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26409 ok = false;
26411 else
26412 ok = false;
26414 if (!ok)
26416 if (cxx_dialect >= cxx14)
26417 error ("literal operator template %qD has invalid parameter list."
26418 " Expected non-type template argument pack <char...>"
26419 " or <typename CharT, CharT...>",
26420 decl);
26421 else
26422 error ("literal operator template %qD has invalid parameter list."
26423 " Expected non-type template argument pack <char...>",
26424 decl);
26428 /* Register member declarations. */
26429 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26430 finish_member_declaration (decl);
26431 /* If DECL is a function template, we must return to parse it later.
26432 (Even though there is no definition, there might be default
26433 arguments that need handling.) */
26434 if (member_p && decl
26435 && DECL_DECLARES_FUNCTION_P (decl))
26436 vec_safe_push (unparsed_funs_with_definitions, decl);
26439 /* Parse a template introduction header for a template-declaration. Returns
26440 false if tentative parse fails. */
26442 static bool
26443 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26445 cp_parser_parse_tentatively (parser);
26447 tree saved_scope = parser->scope;
26448 tree saved_object_scope = parser->object_scope;
26449 tree saved_qualifying_scope = parser->qualifying_scope;
26451 /* Look for the optional `::' operator. */
26452 cp_parser_global_scope_opt (parser,
26453 /*current_scope_valid_p=*/false);
26454 /* Look for the nested-name-specifier. */
26455 cp_parser_nested_name_specifier_opt (parser,
26456 /*typename_keyword_p=*/false,
26457 /*check_dependency_p=*/true,
26458 /*type_p=*/false,
26459 /*is_declaration=*/false);
26461 cp_token *token = cp_lexer_peek_token (parser->lexer);
26462 tree concept_name = cp_parser_identifier (parser);
26464 /* Look up the concept for which we will be matching
26465 template parameters. */
26466 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26467 token->location);
26468 parser->scope = saved_scope;
26469 parser->object_scope = saved_object_scope;
26470 parser->qualifying_scope = saved_qualifying_scope;
26472 if (concept_name == error_mark_node)
26473 cp_parser_simulate_error (parser);
26475 /* Look for opening brace for introduction. */
26476 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
26478 if (!cp_parser_parse_definitely (parser))
26479 return false;
26481 push_deferring_access_checks (dk_deferred);
26483 /* Build vector of placeholder parameters and grab
26484 matching identifiers. */
26485 tree introduction_list = cp_parser_introduction_list (parser);
26487 /* The introduction-list shall not be empty. */
26488 int nargs = TREE_VEC_LENGTH (introduction_list);
26489 if (nargs == 0)
26491 error ("empty introduction-list");
26492 return true;
26495 /* Look for closing brace for introduction. */
26496 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
26497 return true;
26499 if (tmpl_decl == error_mark_node)
26501 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26502 token->location);
26503 return true;
26506 /* Build and associate the constraint. */
26507 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26508 if (parms && parms != error_mark_node)
26510 cp_parser_template_declaration_after_parameters (parser, parms,
26511 member_p);
26512 return true;
26515 error_at (token->location, "no matching concept for template-introduction");
26516 return true;
26519 /* Parse a normal template-declaration following the template keyword. */
26521 static void
26522 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26524 tree parameter_list;
26525 bool need_lang_pop;
26526 location_t location = input_location;
26528 /* Look for the `<' token. */
26529 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26530 return;
26531 if (at_class_scope_p () && current_function_decl)
26533 /* 14.5.2.2 [temp.mem]
26535 A local class shall not have member templates. */
26536 error_at (location,
26537 "invalid declaration of member template in local class");
26538 cp_parser_skip_to_end_of_block_or_statement (parser);
26539 return;
26541 /* [temp]
26543 A template ... shall not have C linkage. */
26544 if (current_lang_name == lang_name_c)
26546 error_at (location, "template with C linkage");
26547 /* Give it C++ linkage to avoid confusing other parts of the
26548 front end. */
26549 push_lang_context (lang_name_cplusplus);
26550 need_lang_pop = true;
26552 else
26553 need_lang_pop = false;
26555 /* We cannot perform access checks on the template parameter
26556 declarations until we know what is being declared, just as we
26557 cannot check the decl-specifier list. */
26558 push_deferring_access_checks (dk_deferred);
26560 /* If the next token is `>', then we have an invalid
26561 specialization. Rather than complain about an invalid template
26562 parameter, issue an error message here. */
26563 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26565 cp_parser_error (parser, "invalid explicit specialization");
26566 begin_specialization ();
26567 parameter_list = NULL_TREE;
26569 else
26571 /* Parse the template parameters. */
26572 parameter_list = cp_parser_template_parameter_list (parser);
26575 /* Look for the `>'. */
26576 cp_parser_skip_to_end_of_template_parameter_list (parser);
26578 /* Manage template requirements */
26579 if (flag_concepts)
26581 tree reqs = get_shorthand_constraints (current_template_parms);
26582 if (tree r = cp_parser_requires_clause_opt (parser))
26583 reqs = conjoin_constraints (reqs, normalize_expression (r));
26584 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26587 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26588 member_p);
26590 /* For the erroneous case of a template with C linkage, we pushed an
26591 implicit C++ linkage scope; exit that scope now. */
26592 if (need_lang_pop)
26593 pop_lang_context ();
26596 /* Parse a template-declaration, assuming that the `export' (and
26597 `extern') keywords, if present, has already been scanned. MEMBER_P
26598 is as for cp_parser_template_declaration. */
26600 static bool
26601 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26603 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26605 cp_lexer_consume_token (parser->lexer);
26606 cp_parser_explicit_template_declaration (parser, member_p);
26607 return true;
26609 else if (flag_concepts)
26610 return cp_parser_template_introduction (parser, member_p);
26612 return false;
26615 /* Perform the deferred access checks from a template-parameter-list.
26616 CHECKS is a TREE_LIST of access checks, as returned by
26617 get_deferred_access_checks. */
26619 static void
26620 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26622 ++processing_template_parmlist;
26623 perform_access_checks (checks, tf_warning_or_error);
26624 --processing_template_parmlist;
26627 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26628 `function-definition' sequence that follows a template header.
26629 If MEMBER_P is true, this declaration appears in a class scope.
26631 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26632 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26634 static tree
26635 cp_parser_single_declaration (cp_parser* parser,
26636 vec<deferred_access_check, va_gc> *checks,
26637 bool member_p,
26638 bool explicit_specialization_p,
26639 bool* friend_p)
26641 int declares_class_or_enum;
26642 tree decl = NULL_TREE;
26643 cp_decl_specifier_seq decl_specifiers;
26644 bool function_definition_p = false;
26645 cp_token *decl_spec_token_start;
26647 /* This function is only used when processing a template
26648 declaration. */
26649 gcc_assert (innermost_scope_kind () == sk_template_parms
26650 || innermost_scope_kind () == sk_template_spec);
26652 /* Defer access checks until we know what is being declared. */
26653 push_deferring_access_checks (dk_deferred);
26655 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26656 alternative. */
26657 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26658 cp_parser_decl_specifier_seq (parser,
26659 CP_PARSER_FLAGS_OPTIONAL,
26660 &decl_specifiers,
26661 &declares_class_or_enum);
26662 if (friend_p)
26663 *friend_p = cp_parser_friend_p (&decl_specifiers);
26665 /* There are no template typedefs. */
26666 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26668 error_at (decl_spec_token_start->location,
26669 "template declaration of %<typedef%>");
26670 decl = error_mark_node;
26673 /* Gather up the access checks that occurred the
26674 decl-specifier-seq. */
26675 stop_deferring_access_checks ();
26677 /* Check for the declaration of a template class. */
26678 if (declares_class_or_enum)
26680 if (cp_parser_declares_only_class_p (parser)
26681 || (declares_class_or_enum & 2))
26683 // If this is a declaration, but not a definition, associate
26684 // any constraints with the type declaration. Constraints
26685 // are associated with definitions in cp_parser_class_specifier.
26686 if (declares_class_or_enum == 1)
26687 associate_classtype_constraints (decl_specifiers.type);
26689 decl = shadow_tag (&decl_specifiers);
26691 /* In this case:
26693 struct C {
26694 friend template <typename T> struct A<T>::B;
26697 A<T>::B will be represented by a TYPENAME_TYPE, and
26698 therefore not recognized by shadow_tag. */
26699 if (friend_p && *friend_p
26700 && !decl
26701 && decl_specifiers.type
26702 && TYPE_P (decl_specifiers.type))
26703 decl = decl_specifiers.type;
26705 if (decl && decl != error_mark_node)
26706 decl = TYPE_NAME (decl);
26707 else
26708 decl = error_mark_node;
26710 /* Perform access checks for template parameters. */
26711 cp_parser_perform_template_parameter_access_checks (checks);
26713 /* Give a helpful diagnostic for
26714 template <class T> struct A { } a;
26715 if we aren't already recovering from an error. */
26716 if (!cp_parser_declares_only_class_p (parser)
26717 && !seen_error ())
26719 error_at (cp_lexer_peek_token (parser->lexer)->location,
26720 "a class template declaration must not declare "
26721 "anything else");
26722 cp_parser_skip_to_end_of_block_or_statement (parser);
26723 goto out;
26728 /* Complain about missing 'typename' or other invalid type names. */
26729 if (!decl_specifiers.any_type_specifiers_p
26730 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26732 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26733 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26734 the rest of this declaration. */
26735 decl = error_mark_node;
26736 goto out;
26739 /* If it's not a template class, try for a template function. If
26740 the next token is a `;', then this declaration does not declare
26741 anything. But, if there were errors in the decl-specifiers, then
26742 the error might well have come from an attempted class-specifier.
26743 In that case, there's no need to warn about a missing declarator. */
26744 if (!decl
26745 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26746 || decl_specifiers.type != error_mark_node))
26748 decl = cp_parser_init_declarator (parser,
26749 &decl_specifiers,
26750 checks,
26751 /*function_definition_allowed_p=*/true,
26752 member_p,
26753 declares_class_or_enum,
26754 &function_definition_p,
26755 NULL, NULL, NULL);
26757 /* 7.1.1-1 [dcl.stc]
26759 A storage-class-specifier shall not be specified in an explicit
26760 specialization... */
26761 if (decl
26762 && explicit_specialization_p
26763 && decl_specifiers.storage_class != sc_none)
26765 error_at (decl_spec_token_start->location,
26766 "explicit template specialization cannot have a storage class");
26767 decl = error_mark_node;
26770 if (decl && VAR_P (decl))
26771 check_template_variable (decl);
26774 /* Look for a trailing `;' after the declaration. */
26775 if (!function_definition_p
26776 && (decl == error_mark_node
26777 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26778 cp_parser_skip_to_end_of_block_or_statement (parser);
26780 out:
26781 pop_deferring_access_checks ();
26783 /* Clear any current qualification; whatever comes next is the start
26784 of something new. */
26785 parser->scope = NULL_TREE;
26786 parser->qualifying_scope = NULL_TREE;
26787 parser->object_scope = NULL_TREE;
26789 return decl;
26792 /* Parse a cast-expression that is not the operand of a unary "&". */
26794 static cp_expr
26795 cp_parser_simple_cast_expression (cp_parser *parser)
26797 return cp_parser_cast_expression (parser, /*address_p=*/false,
26798 /*cast_p=*/false, /*decltype*/false, NULL);
26801 /* Parse a functional cast to TYPE. Returns an expression
26802 representing the cast. */
26804 static cp_expr
26805 cp_parser_functional_cast (cp_parser* parser, tree type)
26807 vec<tree, va_gc> *vec;
26808 tree expression_list;
26809 cp_expr cast;
26810 bool nonconst_p;
26812 location_t start_loc = input_location;
26814 if (!type)
26815 type = error_mark_node;
26817 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26819 cp_lexer_set_source_position (parser->lexer);
26820 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26821 expression_list = cp_parser_braced_list (parser, &nonconst_p);
26822 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
26823 if (TREE_CODE (type) == TYPE_DECL)
26824 type = TREE_TYPE (type);
26826 cast = finish_compound_literal (type, expression_list,
26827 tf_warning_or_error, fcl_functional);
26828 /* Create a location of the form:
26829 type_name{i, f}
26830 ^~~~~~~~~~~~~~~
26831 with caret == start at the start of the type name,
26832 finishing at the closing brace. */
26833 location_t finish_loc
26834 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26835 location_t combined_loc = make_location (start_loc, start_loc,
26836 finish_loc);
26837 cast.set_location (combined_loc);
26838 return cast;
26842 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26843 /*cast_p=*/true,
26844 /*allow_expansion_p=*/true,
26845 /*non_constant_p=*/NULL);
26846 if (vec == NULL)
26847 expression_list = error_mark_node;
26848 else
26850 expression_list = build_tree_list_vec (vec);
26851 release_tree_vector (vec);
26854 cast = build_functional_cast (type, expression_list,
26855 tf_warning_or_error);
26856 /* [expr.const]/1: In an integral constant expression "only type
26857 conversions to integral or enumeration type can be used". */
26858 if (TREE_CODE (type) == TYPE_DECL)
26859 type = TREE_TYPE (type);
26860 if (cast != error_mark_node
26861 && !cast_valid_in_integral_constant_expression_p (type)
26862 && cp_parser_non_integral_constant_expression (parser,
26863 NIC_CONSTRUCTOR))
26864 return error_mark_node;
26866 /* Create a location of the form:
26867 float(i)
26868 ^~~~~~~~
26869 with caret == start at the start of the type name,
26870 finishing at the closing paren. */
26871 location_t finish_loc
26872 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26873 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26874 cast.set_location (combined_loc);
26875 return cast;
26878 /* Save the tokens that make up the body of a member function defined
26879 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26880 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26881 specifiers applied to the declaration. Returns the FUNCTION_DECL
26882 for the member function. */
26884 static tree
26885 cp_parser_save_member_function_body (cp_parser* parser,
26886 cp_decl_specifier_seq *decl_specifiers,
26887 cp_declarator *declarator,
26888 tree attributes)
26890 cp_token *first;
26891 cp_token *last;
26892 tree fn;
26893 bool function_try_block = false;
26895 /* Create the FUNCTION_DECL. */
26896 fn = grokmethod (decl_specifiers, declarator, attributes);
26897 cp_finalize_omp_declare_simd (parser, fn);
26898 cp_finalize_oacc_routine (parser, fn, true);
26899 /* If something went badly wrong, bail out now. */
26900 if (fn == error_mark_node)
26902 /* If there's a function-body, skip it. */
26903 if (cp_parser_token_starts_function_definition_p
26904 (cp_lexer_peek_token (parser->lexer)))
26905 cp_parser_skip_to_end_of_block_or_statement (parser);
26906 return error_mark_node;
26909 /* Remember it, if there default args to post process. */
26910 cp_parser_save_default_args (parser, fn);
26912 /* Save away the tokens that make up the body of the
26913 function. */
26914 first = parser->lexer->next_token;
26916 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26917 cp_lexer_consume_token (parser->lexer);
26918 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26919 RID_TRANSACTION_ATOMIC))
26921 cp_lexer_consume_token (parser->lexer);
26922 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26923 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26924 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26925 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26926 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26927 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26928 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26930 cp_lexer_consume_token (parser->lexer);
26931 cp_lexer_consume_token (parser->lexer);
26932 cp_lexer_consume_token (parser->lexer);
26933 cp_lexer_consume_token (parser->lexer);
26934 cp_lexer_consume_token (parser->lexer);
26936 else
26937 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
26938 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
26940 cp_lexer_consume_token (parser->lexer);
26941 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26942 break;
26946 /* Handle function try blocks. */
26947 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26949 cp_lexer_consume_token (parser->lexer);
26950 function_try_block = true;
26952 /* We can have braced-init-list mem-initializers before the fn body. */
26953 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26955 cp_lexer_consume_token (parser->lexer);
26956 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26958 /* cache_group will stop after an un-nested { } pair, too. */
26959 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26960 break;
26962 /* variadic mem-inits have ... after the ')'. */
26963 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26964 cp_lexer_consume_token (parser->lexer);
26967 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26968 /* Handle function try blocks. */
26969 if (function_try_block)
26970 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26971 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26972 last = parser->lexer->next_token;
26974 /* Save away the inline definition; we will process it when the
26975 class is complete. */
26976 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26977 DECL_PENDING_INLINE_P (fn) = 1;
26979 /* We need to know that this was defined in the class, so that
26980 friend templates are handled correctly. */
26981 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26983 /* Add FN to the queue of functions to be parsed later. */
26984 vec_safe_push (unparsed_funs_with_definitions, fn);
26986 return fn;
26989 /* Save the tokens that make up the in-class initializer for a non-static
26990 data member. Returns a DEFAULT_ARG. */
26992 static tree
26993 cp_parser_save_nsdmi (cp_parser* parser)
26995 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26998 /* Parse a template-argument-list, as well as the trailing ">" (but
26999 not the opening "<"). See cp_parser_template_argument_list for the
27000 return value. */
27002 static tree
27003 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27005 tree arguments;
27006 tree saved_scope;
27007 tree saved_qualifying_scope;
27008 tree saved_object_scope;
27009 bool saved_greater_than_is_operator_p;
27010 int saved_unevaluated_operand;
27011 int saved_inhibit_evaluation_warnings;
27013 /* [temp.names]
27015 When parsing a template-id, the first non-nested `>' is taken as
27016 the end of the template-argument-list rather than a greater-than
27017 operator. */
27018 saved_greater_than_is_operator_p
27019 = parser->greater_than_is_operator_p;
27020 parser->greater_than_is_operator_p = false;
27021 /* Parsing the argument list may modify SCOPE, so we save it
27022 here. */
27023 saved_scope = parser->scope;
27024 saved_qualifying_scope = parser->qualifying_scope;
27025 saved_object_scope = parser->object_scope;
27026 /* We need to evaluate the template arguments, even though this
27027 template-id may be nested within a "sizeof". */
27028 saved_unevaluated_operand = cp_unevaluated_operand;
27029 cp_unevaluated_operand = 0;
27030 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27031 c_inhibit_evaluation_warnings = 0;
27032 /* Parse the template-argument-list itself. */
27033 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27034 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27035 arguments = NULL_TREE;
27036 else
27037 arguments = cp_parser_template_argument_list (parser);
27038 /* Look for the `>' that ends the template-argument-list. If we find
27039 a '>>' instead, it's probably just a typo. */
27040 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27042 if (cxx_dialect != cxx98)
27044 /* In C++0x, a `>>' in a template argument list or cast
27045 expression is considered to be two separate `>'
27046 tokens. So, change the current token to a `>', but don't
27047 consume it: it will be consumed later when the outer
27048 template argument list (or cast expression) is parsed.
27049 Note that this replacement of `>' for `>>' is necessary
27050 even if we are parsing tentatively: in the tentative
27051 case, after calling
27052 cp_parser_enclosed_template_argument_list we will always
27053 throw away all of the template arguments and the first
27054 closing `>', either because the template argument list
27055 was erroneous or because we are replacing those tokens
27056 with a CPP_TEMPLATE_ID token. The second `>' (which will
27057 not have been thrown away) is needed either to close an
27058 outer template argument list or to complete a new-style
27059 cast. */
27060 cp_token *token = cp_lexer_peek_token (parser->lexer);
27061 token->type = CPP_GREATER;
27063 else if (!saved_greater_than_is_operator_p)
27065 /* If we're in a nested template argument list, the '>>' has
27066 to be a typo for '> >'. We emit the error message, but we
27067 continue parsing and we push a '>' as next token, so that
27068 the argument list will be parsed correctly. Note that the
27069 global source location is still on the token before the
27070 '>>', so we need to say explicitly where we want it. */
27071 cp_token *token = cp_lexer_peek_token (parser->lexer);
27072 gcc_rich_location richloc (token->location);
27073 richloc.add_fixit_replace ("> >");
27074 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
27075 "within a nested template argument list");
27077 token->type = CPP_GREATER;
27079 else
27081 /* If this is not a nested template argument list, the '>>'
27082 is a typo for '>'. Emit an error message and continue.
27083 Same deal about the token location, but here we can get it
27084 right by consuming the '>>' before issuing the diagnostic. */
27085 cp_token *token = cp_lexer_consume_token (parser->lexer);
27086 error_at (token->location,
27087 "spurious %<>>%>, use %<>%> to terminate "
27088 "a template argument list");
27091 else
27092 cp_parser_skip_to_end_of_template_parameter_list (parser);
27093 /* The `>' token might be a greater-than operator again now. */
27094 parser->greater_than_is_operator_p
27095 = saved_greater_than_is_operator_p;
27096 /* Restore the SAVED_SCOPE. */
27097 parser->scope = saved_scope;
27098 parser->qualifying_scope = saved_qualifying_scope;
27099 parser->object_scope = saved_object_scope;
27100 cp_unevaluated_operand = saved_unevaluated_operand;
27101 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27103 return arguments;
27106 /* MEMBER_FUNCTION is a member function, or a friend. If default
27107 arguments, or the body of the function have not yet been parsed,
27108 parse them now. */
27110 static void
27111 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27113 timevar_push (TV_PARSE_INMETH);
27114 /* If this member is a template, get the underlying
27115 FUNCTION_DECL. */
27116 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27117 member_function = DECL_TEMPLATE_RESULT (member_function);
27119 /* There should not be any class definitions in progress at this
27120 point; the bodies of members are only parsed outside of all class
27121 definitions. */
27122 gcc_assert (parser->num_classes_being_defined == 0);
27123 /* While we're parsing the member functions we might encounter more
27124 classes. We want to handle them right away, but we don't want
27125 them getting mixed up with functions that are currently in the
27126 queue. */
27127 push_unparsed_function_queues (parser);
27129 /* Make sure that any template parameters are in scope. */
27130 maybe_begin_member_template_processing (member_function);
27132 /* If the body of the function has not yet been parsed, parse it
27133 now. */
27134 if (DECL_PENDING_INLINE_P (member_function))
27136 tree function_scope;
27137 cp_token_cache *tokens;
27139 /* The function is no longer pending; we are processing it. */
27140 tokens = DECL_PENDING_INLINE_INFO (member_function);
27141 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27142 DECL_PENDING_INLINE_P (member_function) = 0;
27144 /* If this is a local class, enter the scope of the containing
27145 function. */
27146 function_scope = current_function_decl;
27147 if (function_scope)
27148 push_function_context ();
27150 /* Push the body of the function onto the lexer stack. */
27151 cp_parser_push_lexer_for_tokens (parser, tokens);
27153 /* Let the front end know that we going to be defining this
27154 function. */
27155 start_preparsed_function (member_function, NULL_TREE,
27156 SF_PRE_PARSED | SF_INCLASS_INLINE);
27158 /* Don't do access checking if it is a templated function. */
27159 if (processing_template_decl)
27160 push_deferring_access_checks (dk_no_check);
27162 /* #pragma omp declare reduction needs special parsing. */
27163 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27165 parser->lexer->in_pragma = true;
27166 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27167 finish_function (/*inline*/2);
27168 cp_check_omp_declare_reduction (member_function);
27170 else
27171 /* Now, parse the body of the function. */
27172 cp_parser_function_definition_after_declarator (parser,
27173 /*inline_p=*/true);
27175 if (processing_template_decl)
27176 pop_deferring_access_checks ();
27178 /* Leave the scope of the containing function. */
27179 if (function_scope)
27180 pop_function_context ();
27181 cp_parser_pop_lexer (parser);
27184 /* Remove any template parameters from the symbol table. */
27185 maybe_end_member_template_processing ();
27187 /* Restore the queue. */
27188 pop_unparsed_function_queues (parser);
27189 timevar_pop (TV_PARSE_INMETH);
27192 /* If DECL contains any default args, remember it on the unparsed
27193 functions queue. */
27195 static void
27196 cp_parser_save_default_args (cp_parser* parser, tree decl)
27198 tree probe;
27200 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27201 probe;
27202 probe = TREE_CHAIN (probe))
27203 if (TREE_PURPOSE (probe))
27205 cp_default_arg_entry entry = {current_class_type, decl};
27206 vec_safe_push (unparsed_funs_with_default_args, entry);
27207 break;
27211 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27212 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27213 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27214 from the parameter-type-list. */
27216 static tree
27217 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27218 tree default_arg, tree parmtype)
27220 cp_token_cache *tokens;
27221 tree parsed_arg;
27222 bool dummy;
27224 if (default_arg == error_mark_node)
27225 return error_mark_node;
27227 /* Push the saved tokens for the default argument onto the parser's
27228 lexer stack. */
27229 tokens = DEFARG_TOKENS (default_arg);
27230 cp_parser_push_lexer_for_tokens (parser, tokens);
27232 start_lambda_scope (decl);
27234 /* Parse the default argument. */
27235 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27236 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27237 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27239 finish_lambda_scope ();
27241 if (parsed_arg == error_mark_node)
27242 cp_parser_skip_to_end_of_statement (parser);
27244 if (!processing_template_decl)
27246 /* In a non-template class, check conversions now. In a template,
27247 we'll wait and instantiate these as needed. */
27248 if (TREE_CODE (decl) == PARM_DECL)
27249 parsed_arg = check_default_argument (parmtype, parsed_arg,
27250 tf_warning_or_error);
27251 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27252 parsed_arg = error_mark_node;
27253 else
27254 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
27257 /* If the token stream has not been completely used up, then
27258 there was extra junk after the end of the default
27259 argument. */
27260 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27262 if (TREE_CODE (decl) == PARM_DECL)
27263 cp_parser_error (parser, "expected %<,%>");
27264 else
27265 cp_parser_error (parser, "expected %<;%>");
27268 /* Revert to the main lexer. */
27269 cp_parser_pop_lexer (parser);
27271 return parsed_arg;
27274 /* FIELD is a non-static data member with an initializer which we saved for
27275 later; parse it now. */
27277 static void
27278 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27280 tree def;
27282 maybe_begin_member_template_processing (field);
27284 push_unparsed_function_queues (parser);
27285 def = cp_parser_late_parse_one_default_arg (parser, field,
27286 DECL_INITIAL (field),
27287 NULL_TREE);
27288 pop_unparsed_function_queues (parser);
27290 maybe_end_member_template_processing ();
27292 DECL_INITIAL (field) = def;
27295 /* FN is a FUNCTION_DECL which may contains a parameter with an
27296 unparsed DEFAULT_ARG. Parse the default args now. This function
27297 assumes that the current scope is the scope in which the default
27298 argument should be processed. */
27300 static void
27301 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27303 bool saved_local_variables_forbidden_p;
27304 tree parm, parmdecl;
27306 /* While we're parsing the default args, we might (due to the
27307 statement expression extension) encounter more classes. We want
27308 to handle them right away, but we don't want them getting mixed
27309 up with default args that are currently in the queue. */
27310 push_unparsed_function_queues (parser);
27312 /* Local variable names (and the `this' keyword) may not appear
27313 in a default argument. */
27314 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27315 parser->local_variables_forbidden_p = true;
27317 push_defarg_context (fn);
27319 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27320 parmdecl = DECL_ARGUMENTS (fn);
27321 parm && parm != void_list_node;
27322 parm = TREE_CHAIN (parm),
27323 parmdecl = DECL_CHAIN (parmdecl))
27325 tree default_arg = TREE_PURPOSE (parm);
27326 tree parsed_arg;
27327 vec<tree, va_gc> *insts;
27328 tree copy;
27329 unsigned ix;
27331 if (!default_arg)
27332 continue;
27334 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27335 /* This can happen for a friend declaration for a function
27336 already declared with default arguments. */
27337 continue;
27339 parsed_arg
27340 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27341 default_arg,
27342 TREE_VALUE (parm));
27343 if (parsed_arg == error_mark_node)
27345 continue;
27348 TREE_PURPOSE (parm) = parsed_arg;
27350 /* Update any instantiations we've already created. */
27351 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27352 vec_safe_iterate (insts, ix, &copy); ix++)
27353 TREE_PURPOSE (copy) = parsed_arg;
27356 pop_defarg_context ();
27358 /* Make sure no default arg is missing. */
27359 check_default_args (fn);
27361 /* Restore the state of local_variables_forbidden_p. */
27362 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27364 /* Restore the queue. */
27365 pop_unparsed_function_queues (parser);
27368 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27370 sizeof ... ( identifier )
27372 where the 'sizeof' token has already been consumed. */
27374 static tree
27375 cp_parser_sizeof_pack (cp_parser *parser)
27377 /* Consume the `...'. */
27378 cp_lexer_consume_token (parser->lexer);
27379 maybe_warn_variadic_templates ();
27381 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27382 if (paren)
27383 cp_lexer_consume_token (parser->lexer);
27384 else
27385 permerror (cp_lexer_peek_token (parser->lexer)->location,
27386 "%<sizeof...%> argument must be surrounded by parentheses");
27388 cp_token *token = cp_lexer_peek_token (parser->lexer);
27389 tree name = cp_parser_identifier (parser);
27390 if (name == error_mark_node)
27391 return error_mark_node;
27392 /* The name is not qualified. */
27393 parser->scope = NULL_TREE;
27394 parser->qualifying_scope = NULL_TREE;
27395 parser->object_scope = NULL_TREE;
27396 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27397 if (expr == error_mark_node)
27398 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27399 token->location);
27400 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27401 expr = TREE_TYPE (expr);
27402 else if (TREE_CODE (expr) == CONST_DECL)
27403 expr = DECL_INITIAL (expr);
27404 expr = make_pack_expansion (expr);
27405 PACK_EXPANSION_SIZEOF_P (expr) = true;
27407 if (paren)
27408 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27410 return expr;
27413 /* Parse the operand of `sizeof' (or a similar operator). Returns
27414 either a TYPE or an expression, depending on the form of the
27415 input. The KEYWORD indicates which kind of expression we have
27416 encountered. */
27418 static tree
27419 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27421 tree expr = NULL_TREE;
27422 const char *saved_message;
27423 char *tmp;
27424 bool saved_integral_constant_expression_p;
27425 bool saved_non_integral_constant_expression_p;
27427 /* If it's a `...', then we are computing the length of a parameter
27428 pack. */
27429 if (keyword == RID_SIZEOF
27430 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27431 return cp_parser_sizeof_pack (parser);
27433 /* Types cannot be defined in a `sizeof' expression. Save away the
27434 old message. */
27435 saved_message = parser->type_definition_forbidden_message;
27436 /* And create the new one. */
27437 tmp = concat ("types may not be defined in %<",
27438 IDENTIFIER_POINTER (ridpointers[keyword]),
27439 "%> expressions", NULL);
27440 parser->type_definition_forbidden_message = tmp;
27442 /* The restrictions on constant-expressions do not apply inside
27443 sizeof expressions. */
27444 saved_integral_constant_expression_p
27445 = parser->integral_constant_expression_p;
27446 saved_non_integral_constant_expression_p
27447 = parser->non_integral_constant_expression_p;
27448 parser->integral_constant_expression_p = false;
27450 /* Do not actually evaluate the expression. */
27451 ++cp_unevaluated_operand;
27452 ++c_inhibit_evaluation_warnings;
27453 /* If it's a `(', then we might be looking at the type-id
27454 construction. */
27455 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27457 tree type = NULL_TREE;
27459 /* We can't be sure yet whether we're looking at a type-id or an
27460 expression. */
27461 cp_parser_parse_tentatively (parser);
27462 /* Note: as a GNU Extension, compound literals are considered
27463 postfix-expressions as they are in C99, so they are valid
27464 arguments to sizeof. See comment in cp_parser_cast_expression
27465 for details. */
27466 if (cp_parser_compound_literal_p (parser))
27467 cp_parser_simulate_error (parser);
27468 else
27470 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27471 parser->in_type_id_in_expr_p = true;
27472 /* Look for the type-id. */
27473 type = cp_parser_type_id (parser);
27474 /* Look for the closing `)'. */
27475 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27476 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27479 /* If all went well, then we're done. */
27480 if (cp_parser_parse_definitely (parser))
27482 cp_decl_specifier_seq decl_specs;
27484 /* Build a trivial decl-specifier-seq. */
27485 clear_decl_specs (&decl_specs);
27486 decl_specs.type = type;
27488 /* Call grokdeclarator to figure out what type this is. */
27489 expr = grokdeclarator (NULL,
27490 &decl_specs,
27491 TYPENAME,
27492 /*initialized=*/0,
27493 /*attrlist=*/NULL);
27497 /* If the type-id production did not work out, then we must be
27498 looking at the unary-expression production. */
27499 if (!expr)
27500 expr = cp_parser_unary_expression (parser);
27502 /* Go back to evaluating expressions. */
27503 --cp_unevaluated_operand;
27504 --c_inhibit_evaluation_warnings;
27506 /* Free the message we created. */
27507 free (tmp);
27508 /* And restore the old one. */
27509 parser->type_definition_forbidden_message = saved_message;
27510 parser->integral_constant_expression_p
27511 = saved_integral_constant_expression_p;
27512 parser->non_integral_constant_expression_p
27513 = saved_non_integral_constant_expression_p;
27515 return expr;
27518 /* If the current declaration has no declarator, return true. */
27520 static bool
27521 cp_parser_declares_only_class_p (cp_parser *parser)
27523 /* If the next token is a `;' or a `,' then there is no
27524 declarator. */
27525 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27526 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27529 /* Update the DECL_SPECS to reflect the storage class indicated by
27530 KEYWORD. */
27532 static void
27533 cp_parser_set_storage_class (cp_parser *parser,
27534 cp_decl_specifier_seq *decl_specs,
27535 enum rid keyword,
27536 cp_token *token)
27538 cp_storage_class storage_class;
27540 if (parser->in_unbraced_linkage_specification_p)
27542 error_at (token->location, "invalid use of %qD in linkage specification",
27543 ridpointers[keyword]);
27544 return;
27546 else if (decl_specs->storage_class != sc_none)
27548 decl_specs->conflicting_specifiers_p = true;
27549 return;
27552 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27553 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27554 && decl_specs->gnu_thread_keyword_p)
27556 pedwarn (decl_specs->locations[ds_thread], 0,
27557 "%<__thread%> before %qD", ridpointers[keyword]);
27560 switch (keyword)
27562 case RID_AUTO:
27563 storage_class = sc_auto;
27564 break;
27565 case RID_REGISTER:
27566 storage_class = sc_register;
27567 break;
27568 case RID_STATIC:
27569 storage_class = sc_static;
27570 break;
27571 case RID_EXTERN:
27572 storage_class = sc_extern;
27573 break;
27574 case RID_MUTABLE:
27575 storage_class = sc_mutable;
27576 break;
27577 default:
27578 gcc_unreachable ();
27580 decl_specs->storage_class = storage_class;
27581 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27583 /* A storage class specifier cannot be applied alongside a typedef
27584 specifier. If there is a typedef specifier present then set
27585 conflicting_specifiers_p which will trigger an error later
27586 on in grokdeclarator. */
27587 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27588 decl_specs->conflicting_specifiers_p = true;
27591 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27592 is true, the type is a class or enum definition. */
27594 static void
27595 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27596 tree type_spec,
27597 cp_token *token,
27598 bool type_definition_p)
27600 decl_specs->any_specifiers_p = true;
27602 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27603 (with, for example, in "typedef int wchar_t;") we remember that
27604 this is what happened. In system headers, we ignore these
27605 declarations so that G++ can work with system headers that are not
27606 C++-safe. */
27607 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27608 && !type_definition_p
27609 && (type_spec == boolean_type_node
27610 || type_spec == char16_type_node
27611 || type_spec == char32_type_node
27612 || type_spec == wchar_type_node)
27613 && (decl_specs->type
27614 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27615 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27616 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27617 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27619 decl_specs->redefined_builtin_type = type_spec;
27620 set_and_check_decl_spec_loc (decl_specs,
27621 ds_redefined_builtin_type_spec,
27622 token);
27623 if (!decl_specs->type)
27625 decl_specs->type = type_spec;
27626 decl_specs->type_definition_p = false;
27627 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27630 else if (decl_specs->type)
27631 decl_specs->multiple_types_p = true;
27632 else
27634 decl_specs->type = type_spec;
27635 decl_specs->type_definition_p = type_definition_p;
27636 decl_specs->redefined_builtin_type = NULL_TREE;
27637 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27641 /* True iff TOKEN is the GNU keyword __thread. */
27643 static bool
27644 token_is__thread (cp_token *token)
27646 gcc_assert (token->keyword == RID_THREAD);
27647 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
27650 /* Set the location for a declarator specifier and check if it is
27651 duplicated.
27653 DECL_SPECS is the sequence of declarator specifiers onto which to
27654 set the location.
27656 DS is the single declarator specifier to set which location is to
27657 be set onto the existing sequence of declarators.
27659 LOCATION is the location for the declarator specifier to
27660 consider. */
27662 static void
27663 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27664 cp_decl_spec ds, cp_token *token)
27666 gcc_assert (ds < ds_last);
27668 if (decl_specs == NULL)
27669 return;
27671 source_location location = token->location;
27673 if (decl_specs->locations[ds] == 0)
27675 decl_specs->locations[ds] = location;
27676 if (ds == ds_thread)
27677 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27679 else
27681 if (ds == ds_long)
27683 if (decl_specs->locations[ds_long_long] != 0)
27684 error_at (location,
27685 "%<long long long%> is too long for GCC");
27686 else
27688 decl_specs->locations[ds_long_long] = location;
27689 pedwarn_cxx98 (location,
27690 OPT_Wlong_long,
27691 "ISO C++ 1998 does not support %<long long%>");
27694 else if (ds == ds_thread)
27696 bool gnu = token_is__thread (token);
27697 if (gnu != decl_specs->gnu_thread_keyword_p)
27698 error_at (location,
27699 "both %<__thread%> and %<thread_local%> specified");
27700 else
27702 gcc_rich_location richloc (location);
27703 richloc.add_fixit_remove ();
27704 error_at_rich_loc (&richloc, "duplicate %qD", token->u.value);
27707 else
27709 static const char *const decl_spec_names[] = {
27710 "signed",
27711 "unsigned",
27712 "short",
27713 "long",
27714 "const",
27715 "volatile",
27716 "restrict",
27717 "inline",
27718 "virtual",
27719 "explicit",
27720 "friend",
27721 "typedef",
27722 "using",
27723 "constexpr",
27724 "__complex"
27726 gcc_rich_location richloc (location);
27727 richloc.add_fixit_remove ();
27728 error_at_rich_loc (&richloc, "duplicate %qs", decl_spec_names[ds]);
27733 /* Return true iff the declarator specifier DS is present in the
27734 sequence of declarator specifiers DECL_SPECS. */
27736 bool
27737 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27738 cp_decl_spec ds)
27740 gcc_assert (ds < ds_last);
27742 if (decl_specs == NULL)
27743 return false;
27745 return decl_specs->locations[ds] != 0;
27748 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27749 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27751 static bool
27752 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27754 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27757 /* Issue an error message indicating that TOKEN_DESC was expected.
27758 If KEYWORD is true, it indicated this function is called by
27759 cp_parser_require_keword and the required token can only be
27760 a indicated keyword. */
27762 static void
27763 cp_parser_required_error (cp_parser *parser,
27764 required_token token_desc,
27765 bool keyword)
27767 switch (token_desc)
27769 case RT_NEW:
27770 cp_parser_error (parser, "expected %<new%>");
27771 return;
27772 case RT_DELETE:
27773 cp_parser_error (parser, "expected %<delete%>");
27774 return;
27775 case RT_RETURN:
27776 cp_parser_error (parser, "expected %<return%>");
27777 return;
27778 case RT_WHILE:
27779 cp_parser_error (parser, "expected %<while%>");
27780 return;
27781 case RT_EXTERN:
27782 cp_parser_error (parser, "expected %<extern%>");
27783 return;
27784 case RT_STATIC_ASSERT:
27785 cp_parser_error (parser, "expected %<static_assert%>");
27786 return;
27787 case RT_DECLTYPE:
27788 cp_parser_error (parser, "expected %<decltype%>");
27789 return;
27790 case RT_OPERATOR:
27791 cp_parser_error (parser, "expected %<operator%>");
27792 return;
27793 case RT_CLASS:
27794 cp_parser_error (parser, "expected %<class%>");
27795 return;
27796 case RT_TEMPLATE:
27797 cp_parser_error (parser, "expected %<template%>");
27798 return;
27799 case RT_NAMESPACE:
27800 cp_parser_error (parser, "expected %<namespace%>");
27801 return;
27802 case RT_USING:
27803 cp_parser_error (parser, "expected %<using%>");
27804 return;
27805 case RT_ASM:
27806 cp_parser_error (parser, "expected %<asm%>");
27807 return;
27808 case RT_TRY:
27809 cp_parser_error (parser, "expected %<try%>");
27810 return;
27811 case RT_CATCH:
27812 cp_parser_error (parser, "expected %<catch%>");
27813 return;
27814 case RT_THROW:
27815 cp_parser_error (parser, "expected %<throw%>");
27816 return;
27817 case RT_LABEL:
27818 cp_parser_error (parser, "expected %<__label__%>");
27819 return;
27820 case RT_AT_TRY:
27821 cp_parser_error (parser, "expected %<@try%>");
27822 return;
27823 case RT_AT_SYNCHRONIZED:
27824 cp_parser_error (parser, "expected %<@synchronized%>");
27825 return;
27826 case RT_AT_THROW:
27827 cp_parser_error (parser, "expected %<@throw%>");
27828 return;
27829 case RT_TRANSACTION_ATOMIC:
27830 cp_parser_error (parser, "expected %<__transaction_atomic%>");
27831 return;
27832 case RT_TRANSACTION_RELAXED:
27833 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
27834 return;
27835 default:
27836 break;
27838 if (!keyword)
27840 switch (token_desc)
27842 case RT_SEMICOLON:
27843 cp_parser_error (parser, "expected %<;%>");
27844 return;
27845 case RT_OPEN_PAREN:
27846 cp_parser_error (parser, "expected %<(%>");
27847 return;
27848 case RT_CLOSE_BRACE:
27849 cp_parser_error (parser, "expected %<}%>");
27850 return;
27851 case RT_OPEN_BRACE:
27852 cp_parser_error (parser, "expected %<{%>");
27853 return;
27854 case RT_CLOSE_SQUARE:
27855 cp_parser_error (parser, "expected %<]%>");
27856 return;
27857 case RT_OPEN_SQUARE:
27858 cp_parser_error (parser, "expected %<[%>");
27859 return;
27860 case RT_COMMA:
27861 cp_parser_error (parser, "expected %<,%>");
27862 return;
27863 case RT_SCOPE:
27864 cp_parser_error (parser, "expected %<::%>");
27865 return;
27866 case RT_LESS:
27867 cp_parser_error (parser, "expected %<<%>");
27868 return;
27869 case RT_GREATER:
27870 cp_parser_error (parser, "expected %<>%>");
27871 return;
27872 case RT_EQ:
27873 cp_parser_error (parser, "expected %<=%>");
27874 return;
27875 case RT_ELLIPSIS:
27876 cp_parser_error (parser, "expected %<...%>");
27877 return;
27878 case RT_MULT:
27879 cp_parser_error (parser, "expected %<*%>");
27880 return;
27881 case RT_COMPL:
27882 cp_parser_error (parser, "expected %<~%>");
27883 return;
27884 case RT_COLON:
27885 cp_parser_error (parser, "expected %<:%>");
27886 return;
27887 case RT_COLON_SCOPE:
27888 cp_parser_error (parser, "expected %<:%> or %<::%>");
27889 return;
27890 case RT_CLOSE_PAREN:
27891 cp_parser_error (parser, "expected %<)%>");
27892 return;
27893 case RT_COMMA_CLOSE_PAREN:
27894 cp_parser_error (parser, "expected %<,%> or %<)%>");
27895 return;
27896 case RT_PRAGMA_EOL:
27897 cp_parser_error (parser, "expected end of line");
27898 return;
27899 case RT_NAME:
27900 cp_parser_error (parser, "expected identifier");
27901 return;
27902 case RT_SELECT:
27903 cp_parser_error (parser, "expected selection-statement");
27904 return;
27905 case RT_INTERATION:
27906 cp_parser_error (parser, "expected iteration-statement");
27907 return;
27908 case RT_JUMP:
27909 cp_parser_error (parser, "expected jump-statement");
27910 return;
27911 case RT_CLASS_KEY:
27912 cp_parser_error (parser, "expected class-key");
27913 return;
27914 case RT_CLASS_TYPENAME_TEMPLATE:
27915 cp_parser_error (parser,
27916 "expected %<class%>, %<typename%>, or %<template%>");
27917 return;
27918 default:
27919 gcc_unreachable ();
27922 else
27923 gcc_unreachable ();
27928 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27929 issue an error message indicating that TOKEN_DESC was expected.
27931 Returns the token consumed, if the token had the appropriate type.
27932 Otherwise, returns NULL. */
27934 static cp_token *
27935 cp_parser_require (cp_parser* parser,
27936 enum cpp_ttype type,
27937 required_token token_desc)
27939 if (cp_lexer_next_token_is (parser->lexer, type))
27940 return cp_lexer_consume_token (parser->lexer);
27941 else
27943 /* Output the MESSAGE -- unless we're parsing tentatively. */
27944 if (!cp_parser_simulate_error (parser))
27945 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27946 return NULL;
27950 /* An error message is produced if the next token is not '>'.
27951 All further tokens are skipped until the desired token is
27952 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27954 static void
27955 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27957 /* Current level of '< ... >'. */
27958 unsigned level = 0;
27959 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27960 unsigned nesting_depth = 0;
27962 /* Are we ready, yet? If not, issue error message. */
27963 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27964 return;
27966 /* Skip tokens until the desired token is found. */
27967 while (true)
27969 /* Peek at the next token. */
27970 switch (cp_lexer_peek_token (parser->lexer)->type)
27972 case CPP_LESS:
27973 if (!nesting_depth)
27974 ++level;
27975 break;
27977 case CPP_RSHIFT:
27978 if (cxx_dialect == cxx98)
27979 /* C++0x views the `>>' operator as two `>' tokens, but
27980 C++98 does not. */
27981 break;
27982 else if (!nesting_depth && level-- == 0)
27984 /* We've hit a `>>' where the first `>' closes the
27985 template argument list, and the second `>' is
27986 spurious. Just consume the `>>' and stop; we've
27987 already produced at least one error. */
27988 cp_lexer_consume_token (parser->lexer);
27989 return;
27991 /* Fall through for C++0x, so we handle the second `>' in
27992 the `>>'. */
27993 gcc_fallthrough ();
27995 case CPP_GREATER:
27996 if (!nesting_depth && level-- == 0)
27998 /* We've reached the token we want, consume it and stop. */
27999 cp_lexer_consume_token (parser->lexer);
28000 return;
28002 break;
28004 case CPP_OPEN_PAREN:
28005 case CPP_OPEN_SQUARE:
28006 ++nesting_depth;
28007 break;
28009 case CPP_CLOSE_PAREN:
28010 case CPP_CLOSE_SQUARE:
28011 if (nesting_depth-- == 0)
28012 return;
28013 break;
28015 case CPP_EOF:
28016 case CPP_PRAGMA_EOL:
28017 case CPP_SEMICOLON:
28018 case CPP_OPEN_BRACE:
28019 case CPP_CLOSE_BRACE:
28020 /* The '>' was probably forgotten, don't look further. */
28021 return;
28023 default:
28024 break;
28027 /* Consume this token. */
28028 cp_lexer_consume_token (parser->lexer);
28032 /* If the next token is the indicated keyword, consume it. Otherwise,
28033 issue an error message indicating that TOKEN_DESC was expected.
28035 Returns the token consumed, if the token had the appropriate type.
28036 Otherwise, returns NULL. */
28038 static cp_token *
28039 cp_parser_require_keyword (cp_parser* parser,
28040 enum rid keyword,
28041 required_token token_desc)
28043 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28045 if (token && token->keyword != keyword)
28047 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
28048 return NULL;
28051 return token;
28054 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28055 function-definition. */
28057 static bool
28058 cp_parser_token_starts_function_definition_p (cp_token* token)
28060 return (/* An ordinary function-body begins with an `{'. */
28061 token->type == CPP_OPEN_BRACE
28062 /* A ctor-initializer begins with a `:'. */
28063 || token->type == CPP_COLON
28064 /* A function-try-block begins with `try'. */
28065 || token->keyword == RID_TRY
28066 /* A function-transaction-block begins with `__transaction_atomic'
28067 or `__transaction_relaxed'. */
28068 || token->keyword == RID_TRANSACTION_ATOMIC
28069 || token->keyword == RID_TRANSACTION_RELAXED
28070 /* The named return value extension begins with `return'. */
28071 || token->keyword == RID_RETURN);
28074 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28075 definition. */
28077 static bool
28078 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28080 cp_token *token;
28082 token = cp_lexer_peek_token (parser->lexer);
28083 return (token->type == CPP_OPEN_BRACE
28084 || (token->type == CPP_COLON
28085 && !parser->colon_doesnt_start_class_def_p));
28088 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28089 C++0x) ending a template-argument. */
28091 static bool
28092 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28094 cp_token *token;
28096 token = cp_lexer_peek_token (parser->lexer);
28097 return (token->type == CPP_COMMA
28098 || token->type == CPP_GREATER
28099 || token->type == CPP_ELLIPSIS
28100 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28103 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28104 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28106 static bool
28107 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28108 size_t n)
28110 cp_token *token;
28112 token = cp_lexer_peek_nth_token (parser->lexer, n);
28113 if (token->type == CPP_LESS)
28114 return true;
28115 /* Check for the sequence `<::' in the original code. It would be lexed as
28116 `[:', where `[' is a digraph, and there is no whitespace before
28117 `:'. */
28118 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28120 cp_token *token2;
28121 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28122 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28123 return true;
28125 return false;
28128 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28129 or none_type otherwise. */
28131 static enum tag_types
28132 cp_parser_token_is_class_key (cp_token* token)
28134 switch (token->keyword)
28136 case RID_CLASS:
28137 return class_type;
28138 case RID_STRUCT:
28139 return record_type;
28140 case RID_UNION:
28141 return union_type;
28143 default:
28144 return none_type;
28148 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28149 or none_type otherwise or if the token is null. */
28151 static enum tag_types
28152 cp_parser_token_is_type_parameter_key (cp_token* token)
28154 if (!token)
28155 return none_type;
28157 switch (token->keyword)
28159 case RID_CLASS:
28160 return class_type;
28161 case RID_TYPENAME:
28162 return typename_type;
28164 default:
28165 return none_type;
28169 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28171 static void
28172 cp_parser_check_class_key (enum tag_types class_key, tree type)
28174 if (type == error_mark_node)
28175 return;
28176 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28178 if (permerror (input_location, "%qs tag used in naming %q#T",
28179 class_key == union_type ? "union"
28180 : class_key == record_type ? "struct" : "class",
28181 type))
28182 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28183 "%q#T was previously declared here", type);
28187 /* Issue an error message if DECL is redeclared with different
28188 access than its original declaration [class.access.spec/3].
28189 This applies to nested classes, nested class templates and
28190 enumerations [class.mem/1]. */
28192 static void
28193 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28195 if (!decl
28196 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28197 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28198 return;
28200 if ((TREE_PRIVATE (decl)
28201 != (current_access_specifier == access_private_node))
28202 || (TREE_PROTECTED (decl)
28203 != (current_access_specifier == access_protected_node)))
28204 error_at (location, "%qD redeclared with different access", decl);
28207 /* Look for the `template' keyword, as a syntactic disambiguator.
28208 Return TRUE iff it is present, in which case it will be
28209 consumed. */
28211 static bool
28212 cp_parser_optional_template_keyword (cp_parser *parser)
28214 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28216 /* In C++98 the `template' keyword can only be used within templates;
28217 outside templates the parser can always figure out what is a
28218 template and what is not. In C++11, per the resolution of DR 468,
28219 `template' is allowed in cases where it is not strictly necessary. */
28220 if (!processing_template_decl
28221 && pedantic && cxx_dialect == cxx98)
28223 cp_token *token = cp_lexer_peek_token (parser->lexer);
28224 pedwarn (token->location, OPT_Wpedantic,
28225 "in C++98 %<template%> (as a disambiguator) is only "
28226 "allowed within templates");
28227 /* If this part of the token stream is rescanned, the same
28228 error message would be generated. So, we purge the token
28229 from the stream. */
28230 cp_lexer_purge_token (parser->lexer);
28231 return false;
28233 else
28235 /* Consume the `template' keyword. */
28236 cp_lexer_consume_token (parser->lexer);
28237 return true;
28240 return false;
28243 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28244 set PARSER->SCOPE, and perform other related actions. */
28246 static void
28247 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28249 struct tree_check *check_value;
28251 /* Get the stored value. */
28252 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28253 /* Set the scope from the stored value. */
28254 parser->scope = saved_checks_value (check_value);
28255 parser->qualifying_scope = check_value->qualifying_scope;
28256 parser->object_scope = NULL_TREE;
28259 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28260 encounter the end of a block before what we were looking for. */
28262 static bool
28263 cp_parser_cache_group (cp_parser *parser,
28264 enum cpp_ttype end,
28265 unsigned depth)
28267 while (true)
28269 cp_token *token = cp_lexer_peek_token (parser->lexer);
28271 /* Abort a parenthesized expression if we encounter a semicolon. */
28272 if ((end == CPP_CLOSE_PAREN || depth == 0)
28273 && token->type == CPP_SEMICOLON)
28274 return true;
28275 /* If we've reached the end of the file, stop. */
28276 if (token->type == CPP_EOF
28277 || (end != CPP_PRAGMA_EOL
28278 && token->type == CPP_PRAGMA_EOL))
28279 return true;
28280 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28281 /* We've hit the end of an enclosing block, so there's been some
28282 kind of syntax error. */
28283 return true;
28285 /* Consume the token. */
28286 cp_lexer_consume_token (parser->lexer);
28287 /* See if it starts a new group. */
28288 if (token->type == CPP_OPEN_BRACE)
28290 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28291 /* In theory this should probably check end == '}', but
28292 cp_parser_save_member_function_body needs it to exit
28293 after either '}' or ')' when called with ')'. */
28294 if (depth == 0)
28295 return false;
28297 else if (token->type == CPP_OPEN_PAREN)
28299 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28300 if (depth == 0 && end == CPP_CLOSE_PAREN)
28301 return false;
28303 else if (token->type == CPP_PRAGMA)
28304 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28305 else if (token->type == end)
28306 return false;
28310 /* Like above, for caching a default argument or NSDMI. Both of these are
28311 terminated by a non-nested comma, but it can be unclear whether or not a
28312 comma is nested in a template argument list unless we do more parsing.
28313 In order to handle this ambiguity, when we encounter a ',' after a '<'
28314 we try to parse what follows as a parameter-declaration-list (in the
28315 case of a default argument) or a member-declarator (in the case of an
28316 NSDMI). If that succeeds, then we stop caching. */
28318 static tree
28319 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28321 unsigned depth = 0;
28322 int maybe_template_id = 0;
28323 cp_token *first_token;
28324 cp_token *token;
28325 tree default_argument;
28327 /* Add tokens until we have processed the entire default
28328 argument. We add the range [first_token, token). */
28329 first_token = cp_lexer_peek_token (parser->lexer);
28330 if (first_token->type == CPP_OPEN_BRACE)
28332 /* For list-initialization, this is straightforward. */
28333 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28334 token = cp_lexer_peek_token (parser->lexer);
28336 else while (true)
28338 bool done = false;
28340 /* Peek at the next token. */
28341 token = cp_lexer_peek_token (parser->lexer);
28342 /* What we do depends on what token we have. */
28343 switch (token->type)
28345 /* In valid code, a default argument must be
28346 immediately followed by a `,' `)', or `...'. */
28347 case CPP_COMMA:
28348 if (depth == 0 && maybe_template_id)
28350 /* If we've seen a '<', we might be in a
28351 template-argument-list. Until Core issue 325 is
28352 resolved, we don't know how this situation ought
28353 to be handled, so try to DTRT. We check whether
28354 what comes after the comma is a valid parameter
28355 declaration list. If it is, then the comma ends
28356 the default argument; otherwise the default
28357 argument continues. */
28358 bool error = false;
28359 cp_token *peek;
28361 /* Set ITALP so cp_parser_parameter_declaration_list
28362 doesn't decide to commit to this parse. */
28363 bool saved_italp = parser->in_template_argument_list_p;
28364 parser->in_template_argument_list_p = true;
28366 cp_parser_parse_tentatively (parser);
28368 if (nsdmi)
28370 /* Parse declarators until we reach a non-comma or
28371 somthing that cannot be an initializer.
28372 Just checking whether we're looking at a single
28373 declarator is insufficient. Consider:
28374 int var = tuple<T,U>::x;
28375 The template parameter 'U' looks exactly like a
28376 declarator. */
28379 int ctor_dtor_or_conv_p;
28380 cp_lexer_consume_token (parser->lexer);
28381 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28382 &ctor_dtor_or_conv_p,
28383 /*parenthesized_p=*/NULL,
28384 /*member_p=*/true,
28385 /*friend_p=*/false);
28386 peek = cp_lexer_peek_token (parser->lexer);
28387 if (cp_parser_error_occurred (parser))
28388 break;
28390 while (peek->type == CPP_COMMA);
28391 /* If we met an '=' or ';' then the original comma
28392 was the end of the NSDMI. Otherwise assume
28393 we're still in the NSDMI. */
28394 error = (peek->type != CPP_EQ
28395 && peek->type != CPP_SEMICOLON);
28397 else
28399 cp_lexer_consume_token (parser->lexer);
28400 begin_scope (sk_function_parms, NULL_TREE);
28401 cp_parser_parameter_declaration_list (parser, &error);
28402 pop_bindings_and_leave_scope ();
28404 if (!cp_parser_error_occurred (parser) && !error)
28405 done = true;
28406 cp_parser_abort_tentative_parse (parser);
28408 parser->in_template_argument_list_p = saved_italp;
28409 break;
28411 /* FALLTHRU */
28412 case CPP_CLOSE_PAREN:
28413 case CPP_ELLIPSIS:
28414 /* If we run into a non-nested `;', `}', or `]',
28415 then the code is invalid -- but the default
28416 argument is certainly over. */
28417 case CPP_SEMICOLON:
28418 case CPP_CLOSE_BRACE:
28419 case CPP_CLOSE_SQUARE:
28420 if (depth == 0
28421 /* Handle correctly int n = sizeof ... ( p ); */
28422 && token->type != CPP_ELLIPSIS)
28423 done = true;
28424 /* Update DEPTH, if necessary. */
28425 else if (token->type == CPP_CLOSE_PAREN
28426 || token->type == CPP_CLOSE_BRACE
28427 || token->type == CPP_CLOSE_SQUARE)
28428 --depth;
28429 break;
28431 case CPP_OPEN_PAREN:
28432 case CPP_OPEN_SQUARE:
28433 case CPP_OPEN_BRACE:
28434 ++depth;
28435 break;
28437 case CPP_LESS:
28438 if (depth == 0)
28439 /* This might be the comparison operator, or it might
28440 start a template argument list. */
28441 ++maybe_template_id;
28442 break;
28444 case CPP_RSHIFT:
28445 if (cxx_dialect == cxx98)
28446 break;
28447 /* Fall through for C++0x, which treats the `>>'
28448 operator like two `>' tokens in certain
28449 cases. */
28450 gcc_fallthrough ();
28452 case CPP_GREATER:
28453 if (depth == 0)
28455 /* This might be an operator, or it might close a
28456 template argument list. But if a previous '<'
28457 started a template argument list, this will have
28458 closed it, so we can't be in one anymore. */
28459 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28460 if (maybe_template_id < 0)
28461 maybe_template_id = 0;
28463 break;
28465 /* If we run out of tokens, issue an error message. */
28466 case CPP_EOF:
28467 case CPP_PRAGMA_EOL:
28468 error_at (token->location, "file ends in default argument");
28469 return error_mark_node;
28471 case CPP_NAME:
28472 case CPP_SCOPE:
28473 /* In these cases, we should look for template-ids.
28474 For example, if the default argument is
28475 `X<int, double>()', we need to do name lookup to
28476 figure out whether or not `X' is a template; if
28477 so, the `,' does not end the default argument.
28479 That is not yet done. */
28480 break;
28482 default:
28483 break;
28486 /* If we've reached the end, stop. */
28487 if (done)
28488 break;
28490 /* Add the token to the token block. */
28491 token = cp_lexer_consume_token (parser->lexer);
28494 /* Create a DEFAULT_ARG to represent the unparsed default
28495 argument. */
28496 default_argument = make_node (DEFAULT_ARG);
28497 DEFARG_TOKENS (default_argument)
28498 = cp_token_cache_new (first_token, token);
28499 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28501 return default_argument;
28504 /* Begin parsing tentatively. We always save tokens while parsing
28505 tentatively so that if the tentative parsing fails we can restore the
28506 tokens. */
28508 static void
28509 cp_parser_parse_tentatively (cp_parser* parser)
28511 /* Enter a new parsing context. */
28512 parser->context = cp_parser_context_new (parser->context);
28513 /* Begin saving tokens. */
28514 cp_lexer_save_tokens (parser->lexer);
28515 /* In order to avoid repetitive access control error messages,
28516 access checks are queued up until we are no longer parsing
28517 tentatively. */
28518 push_deferring_access_checks (dk_deferred);
28521 /* Commit to the currently active tentative parse. */
28523 static void
28524 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28526 cp_parser_context *context;
28527 cp_lexer *lexer;
28529 /* Mark all of the levels as committed. */
28530 lexer = parser->lexer;
28531 for (context = parser->context; context->next; context = context->next)
28533 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28534 break;
28535 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28536 while (!cp_lexer_saving_tokens (lexer))
28537 lexer = lexer->next;
28538 cp_lexer_commit_tokens (lexer);
28542 /* Commit to the topmost currently active tentative parse.
28544 Note that this function shouldn't be called when there are
28545 irreversible side-effects while in a tentative state. For
28546 example, we shouldn't create a permanent entry in the symbol
28547 table, or issue an error message that might not apply if the
28548 tentative parse is aborted. */
28550 static void
28551 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28553 cp_parser_context *context = parser->context;
28554 cp_lexer *lexer = parser->lexer;
28556 if (context)
28558 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28559 return;
28560 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28562 while (!cp_lexer_saving_tokens (lexer))
28563 lexer = lexer->next;
28564 cp_lexer_commit_tokens (lexer);
28568 /* Abort the currently active tentative parse. All consumed tokens
28569 will be rolled back, and no diagnostics will be issued. */
28571 static void
28572 cp_parser_abort_tentative_parse (cp_parser* parser)
28574 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28575 || errorcount > 0);
28576 cp_parser_simulate_error (parser);
28577 /* Now, pretend that we want to see if the construct was
28578 successfully parsed. */
28579 cp_parser_parse_definitely (parser);
28582 /* Stop parsing tentatively. If a parse error has occurred, restore the
28583 token stream. Otherwise, commit to the tokens we have consumed.
28584 Returns true if no error occurred; false otherwise. */
28586 static bool
28587 cp_parser_parse_definitely (cp_parser* parser)
28589 bool error_occurred;
28590 cp_parser_context *context;
28592 /* Remember whether or not an error occurred, since we are about to
28593 destroy that information. */
28594 error_occurred = cp_parser_error_occurred (parser);
28595 /* Remove the topmost context from the stack. */
28596 context = parser->context;
28597 parser->context = context->next;
28598 /* If no parse errors occurred, commit to the tentative parse. */
28599 if (!error_occurred)
28601 /* Commit to the tokens read tentatively, unless that was
28602 already done. */
28603 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28604 cp_lexer_commit_tokens (parser->lexer);
28606 pop_to_parent_deferring_access_checks ();
28608 /* Otherwise, if errors occurred, roll back our state so that things
28609 are just as they were before we began the tentative parse. */
28610 else
28612 cp_lexer_rollback_tokens (parser->lexer);
28613 pop_deferring_access_checks ();
28615 /* Add the context to the front of the free list. */
28616 context->next = cp_parser_context_free_list;
28617 cp_parser_context_free_list = context;
28619 return !error_occurred;
28622 /* Returns true if we are parsing tentatively and are not committed to
28623 this tentative parse. */
28625 static bool
28626 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28628 return (cp_parser_parsing_tentatively (parser)
28629 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28632 /* Returns nonzero iff an error has occurred during the most recent
28633 tentative parse. */
28635 static bool
28636 cp_parser_error_occurred (cp_parser* parser)
28638 return (cp_parser_parsing_tentatively (parser)
28639 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28642 /* Returns nonzero if GNU extensions are allowed. */
28644 static bool
28645 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28647 return parser->allow_gnu_extensions_p;
28650 /* Objective-C++ Productions */
28653 /* Parse an Objective-C expression, which feeds into a primary-expression
28654 above.
28656 objc-expression:
28657 objc-message-expression
28658 objc-string-literal
28659 objc-encode-expression
28660 objc-protocol-expression
28661 objc-selector-expression
28663 Returns a tree representation of the expression. */
28665 static cp_expr
28666 cp_parser_objc_expression (cp_parser* parser)
28668 /* Try to figure out what kind of declaration is present. */
28669 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28671 switch (kwd->type)
28673 case CPP_OPEN_SQUARE:
28674 return cp_parser_objc_message_expression (parser);
28676 case CPP_OBJC_STRING:
28677 kwd = cp_lexer_consume_token (parser->lexer);
28678 return objc_build_string_object (kwd->u.value);
28680 case CPP_KEYWORD:
28681 switch (kwd->keyword)
28683 case RID_AT_ENCODE:
28684 return cp_parser_objc_encode_expression (parser);
28686 case RID_AT_PROTOCOL:
28687 return cp_parser_objc_protocol_expression (parser);
28689 case RID_AT_SELECTOR:
28690 return cp_parser_objc_selector_expression (parser);
28692 default:
28693 break;
28695 default:
28696 error_at (kwd->location,
28697 "misplaced %<@%D%> Objective-C++ construct",
28698 kwd->u.value);
28699 cp_parser_skip_to_end_of_block_or_statement (parser);
28702 return error_mark_node;
28705 /* Parse an Objective-C message expression.
28707 objc-message-expression:
28708 [ objc-message-receiver objc-message-args ]
28710 Returns a representation of an Objective-C message. */
28712 static tree
28713 cp_parser_objc_message_expression (cp_parser* parser)
28715 tree receiver, messageargs;
28717 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28718 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
28719 receiver = cp_parser_objc_message_receiver (parser);
28720 messageargs = cp_parser_objc_message_args (parser);
28721 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
28722 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28724 tree result = objc_build_message_expr (receiver, messageargs);
28726 /* Construct a location e.g.
28727 [self func1:5]
28728 ^~~~~~~~~~~~~~
28729 ranging from the '[' to the ']', with the caret at the start. */
28730 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
28731 protected_set_expr_location (result, combined_loc);
28733 return result;
28736 /* Parse an objc-message-receiver.
28738 objc-message-receiver:
28739 expression
28740 simple-type-specifier
28742 Returns a representation of the type or expression. */
28744 static tree
28745 cp_parser_objc_message_receiver (cp_parser* parser)
28747 tree rcv;
28749 /* An Objective-C message receiver may be either (1) a type
28750 or (2) an expression. */
28751 cp_parser_parse_tentatively (parser);
28752 rcv = cp_parser_expression (parser);
28754 /* If that worked out, fine. */
28755 if (cp_parser_parse_definitely (parser))
28756 return rcv;
28758 cp_parser_parse_tentatively (parser);
28759 rcv = cp_parser_simple_type_specifier (parser,
28760 /*decl_specs=*/NULL,
28761 CP_PARSER_FLAGS_NONE);
28763 if (cp_parser_parse_definitely (parser))
28764 return objc_get_class_reference (rcv);
28766 cp_parser_error (parser, "objective-c++ message receiver expected");
28767 return error_mark_node;
28770 /* Parse the arguments and selectors comprising an Objective-C message.
28772 objc-message-args:
28773 objc-selector
28774 objc-selector-args
28775 objc-selector-args , objc-comma-args
28777 objc-selector-args:
28778 objc-selector [opt] : assignment-expression
28779 objc-selector-args objc-selector [opt] : assignment-expression
28781 objc-comma-args:
28782 assignment-expression
28783 objc-comma-args , assignment-expression
28785 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28786 selector arguments and TREE_VALUE containing a list of comma
28787 arguments. */
28789 static tree
28790 cp_parser_objc_message_args (cp_parser* parser)
28792 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
28793 bool maybe_unary_selector_p = true;
28794 cp_token *token = cp_lexer_peek_token (parser->lexer);
28796 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28798 tree selector = NULL_TREE, arg;
28800 if (token->type != CPP_COLON)
28801 selector = cp_parser_objc_selector (parser);
28803 /* Detect if we have a unary selector. */
28804 if (maybe_unary_selector_p
28805 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28806 return build_tree_list (selector, NULL_TREE);
28808 maybe_unary_selector_p = false;
28809 cp_parser_require (parser, CPP_COLON, RT_COLON);
28810 arg = cp_parser_assignment_expression (parser);
28812 sel_args
28813 = chainon (sel_args,
28814 build_tree_list (selector, arg));
28816 token = cp_lexer_peek_token (parser->lexer);
28819 /* Handle non-selector arguments, if any. */
28820 while (token->type == CPP_COMMA)
28822 tree arg;
28824 cp_lexer_consume_token (parser->lexer);
28825 arg = cp_parser_assignment_expression (parser);
28827 addl_args
28828 = chainon (addl_args,
28829 build_tree_list (NULL_TREE, arg));
28831 token = cp_lexer_peek_token (parser->lexer);
28834 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
28836 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
28837 return build_tree_list (error_mark_node, error_mark_node);
28840 return build_tree_list (sel_args, addl_args);
28843 /* Parse an Objective-C encode expression.
28845 objc-encode-expression:
28846 @encode objc-typename
28848 Returns an encoded representation of the type argument. */
28850 static cp_expr
28851 cp_parser_objc_encode_expression (cp_parser* parser)
28853 tree type;
28854 cp_token *token;
28855 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28857 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
28858 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28859 token = cp_lexer_peek_token (parser->lexer);
28860 type = complete_type (cp_parser_type_id (parser));
28861 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28863 if (!type)
28865 error_at (token->location,
28866 "%<@encode%> must specify a type as an argument");
28867 return error_mark_node;
28870 /* This happens if we find @encode(T) (where T is a template
28871 typename or something dependent on a template typename) when
28872 parsing a template. In that case, we can't compile it
28873 immediately, but we rather create an AT_ENCODE_EXPR which will
28874 need to be instantiated when the template is used.
28876 if (dependent_type_p (type))
28878 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28879 TREE_READONLY (value) = 1;
28880 return value;
28884 /* Build a location of the form:
28885 @encode(int)
28886 ^~~~~~~~~~~~
28887 with caret==start at the @ token, finishing at the close paren. */
28888 location_t combined_loc
28889 = make_location (start_loc, start_loc,
28890 cp_lexer_previous_token (parser->lexer)->location);
28892 return cp_expr (objc_build_encode_expr (type), combined_loc);
28895 /* Parse an Objective-C @defs expression. */
28897 static tree
28898 cp_parser_objc_defs_expression (cp_parser *parser)
28900 tree name;
28902 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
28903 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28904 name = cp_parser_identifier (parser);
28905 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28907 return objc_get_class_ivars (name);
28910 /* Parse an Objective-C protocol expression.
28912 objc-protocol-expression:
28913 @protocol ( identifier )
28915 Returns a representation of the protocol expression. */
28917 static tree
28918 cp_parser_objc_protocol_expression (cp_parser* parser)
28920 tree proto;
28921 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28923 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28924 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28925 proto = cp_parser_identifier (parser);
28926 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28928 /* Build a location of the form:
28929 @protocol(prot)
28930 ^~~~~~~~~~~~~~~
28931 with caret==start at the @ token, finishing at the close paren. */
28932 location_t combined_loc
28933 = make_location (start_loc, start_loc,
28934 cp_lexer_previous_token (parser->lexer)->location);
28935 tree result = objc_build_protocol_expr (proto);
28936 protected_set_expr_location (result, combined_loc);
28937 return result;
28940 /* Parse an Objective-C selector expression.
28942 objc-selector-expression:
28943 @selector ( objc-method-signature )
28945 objc-method-signature:
28946 objc-selector
28947 objc-selector-seq
28949 objc-selector-seq:
28950 objc-selector :
28951 objc-selector-seq objc-selector :
28953 Returns a representation of the method selector. */
28955 static tree
28956 cp_parser_objc_selector_expression (cp_parser* parser)
28958 tree sel_seq = NULL_TREE;
28959 bool maybe_unary_selector_p = true;
28960 cp_token *token;
28961 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28963 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28964 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28965 token = cp_lexer_peek_token (parser->lexer);
28967 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28968 || token->type == CPP_SCOPE)
28970 tree selector = NULL_TREE;
28972 if (token->type != CPP_COLON
28973 || token->type == CPP_SCOPE)
28974 selector = cp_parser_objc_selector (parser);
28976 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28977 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28979 /* Detect if we have a unary selector. */
28980 if (maybe_unary_selector_p)
28982 sel_seq = selector;
28983 goto finish_selector;
28985 else
28987 cp_parser_error (parser, "expected %<:%>");
28990 maybe_unary_selector_p = false;
28991 token = cp_lexer_consume_token (parser->lexer);
28993 if (token->type == CPP_SCOPE)
28995 sel_seq
28996 = chainon (sel_seq,
28997 build_tree_list (selector, NULL_TREE));
28998 sel_seq
28999 = chainon (sel_seq,
29000 build_tree_list (NULL_TREE, NULL_TREE));
29002 else
29003 sel_seq
29004 = chainon (sel_seq,
29005 build_tree_list (selector, NULL_TREE));
29007 token = cp_lexer_peek_token (parser->lexer);
29010 finish_selector:
29011 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29014 /* Build a location of the form:
29015 @selector(func)
29016 ^~~~~~~~~~~~~~~
29017 with caret==start at the @ token, finishing at the close paren. */
29018 location_t combined_loc
29019 = make_location (loc, loc,
29020 cp_lexer_previous_token (parser->lexer)->location);
29021 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29022 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29023 protected_set_expr_location (result, combined_loc);
29024 return result;
29027 /* Parse a list of identifiers.
29029 objc-identifier-list:
29030 identifier
29031 objc-identifier-list , identifier
29033 Returns a TREE_LIST of identifier nodes. */
29035 static tree
29036 cp_parser_objc_identifier_list (cp_parser* parser)
29038 tree identifier;
29039 tree list;
29040 cp_token *sep;
29042 identifier = cp_parser_identifier (parser);
29043 if (identifier == error_mark_node)
29044 return error_mark_node;
29046 list = build_tree_list (NULL_TREE, identifier);
29047 sep = cp_lexer_peek_token (parser->lexer);
29049 while (sep->type == CPP_COMMA)
29051 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29052 identifier = cp_parser_identifier (parser);
29053 if (identifier == error_mark_node)
29054 return list;
29056 list = chainon (list, build_tree_list (NULL_TREE,
29057 identifier));
29058 sep = cp_lexer_peek_token (parser->lexer);
29061 return list;
29064 /* Parse an Objective-C alias declaration.
29066 objc-alias-declaration:
29067 @compatibility_alias identifier identifier ;
29069 This function registers the alias mapping with the Objective-C front end.
29070 It returns nothing. */
29072 static void
29073 cp_parser_objc_alias_declaration (cp_parser* parser)
29075 tree alias, orig;
29077 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29078 alias = cp_parser_identifier (parser);
29079 orig = cp_parser_identifier (parser);
29080 objc_declare_alias (alias, orig);
29081 cp_parser_consume_semicolon_at_end_of_statement (parser);
29084 /* Parse an Objective-C class forward-declaration.
29086 objc-class-declaration:
29087 @class objc-identifier-list ;
29089 The function registers the forward declarations with the Objective-C
29090 front end. It returns nothing. */
29092 static void
29093 cp_parser_objc_class_declaration (cp_parser* parser)
29095 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29096 while (true)
29098 tree id;
29100 id = cp_parser_identifier (parser);
29101 if (id == error_mark_node)
29102 break;
29104 objc_declare_class (id);
29106 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29107 cp_lexer_consume_token (parser->lexer);
29108 else
29109 break;
29111 cp_parser_consume_semicolon_at_end_of_statement (parser);
29114 /* Parse a list of Objective-C protocol references.
29116 objc-protocol-refs-opt:
29117 objc-protocol-refs [opt]
29119 objc-protocol-refs:
29120 < objc-identifier-list >
29122 Returns a TREE_LIST of identifiers, if any. */
29124 static tree
29125 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29127 tree protorefs = NULL_TREE;
29129 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29131 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29132 protorefs = cp_parser_objc_identifier_list (parser);
29133 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29136 return protorefs;
29139 /* Parse a Objective-C visibility specification. */
29141 static void
29142 cp_parser_objc_visibility_spec (cp_parser* parser)
29144 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29146 switch (vis->keyword)
29148 case RID_AT_PRIVATE:
29149 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29150 break;
29151 case RID_AT_PROTECTED:
29152 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29153 break;
29154 case RID_AT_PUBLIC:
29155 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29156 break;
29157 case RID_AT_PACKAGE:
29158 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29159 break;
29160 default:
29161 return;
29164 /* Eat '@private'/'@protected'/'@public'. */
29165 cp_lexer_consume_token (parser->lexer);
29168 /* Parse an Objective-C method type. Return 'true' if it is a class
29169 (+) method, and 'false' if it is an instance (-) method. */
29171 static inline bool
29172 cp_parser_objc_method_type (cp_parser* parser)
29174 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29175 return true;
29176 else
29177 return false;
29180 /* Parse an Objective-C protocol qualifier. */
29182 static tree
29183 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29185 tree quals = NULL_TREE, node;
29186 cp_token *token = cp_lexer_peek_token (parser->lexer);
29188 node = token->u.value;
29190 while (node && identifier_p (node)
29191 && (node == ridpointers [(int) RID_IN]
29192 || node == ridpointers [(int) RID_OUT]
29193 || node == ridpointers [(int) RID_INOUT]
29194 || node == ridpointers [(int) RID_BYCOPY]
29195 || node == ridpointers [(int) RID_BYREF]
29196 || node == ridpointers [(int) RID_ONEWAY]))
29198 quals = tree_cons (NULL_TREE, node, quals);
29199 cp_lexer_consume_token (parser->lexer);
29200 token = cp_lexer_peek_token (parser->lexer);
29201 node = token->u.value;
29204 return quals;
29207 /* Parse an Objective-C typename. */
29209 static tree
29210 cp_parser_objc_typename (cp_parser* parser)
29212 tree type_name = NULL_TREE;
29214 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29216 tree proto_quals, cp_type = NULL_TREE;
29218 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29219 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29221 /* An ObjC type name may consist of just protocol qualifiers, in which
29222 case the type shall default to 'id'. */
29223 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29225 cp_type = cp_parser_type_id (parser);
29227 /* If the type could not be parsed, an error has already
29228 been produced. For error recovery, behave as if it had
29229 not been specified, which will use the default type
29230 'id'. */
29231 if (cp_type == error_mark_node)
29233 cp_type = NULL_TREE;
29234 /* We need to skip to the closing parenthesis as
29235 cp_parser_type_id() does not seem to do it for
29236 us. */
29237 cp_parser_skip_to_closing_parenthesis (parser,
29238 /*recovering=*/true,
29239 /*or_comma=*/false,
29240 /*consume_paren=*/false);
29244 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29245 type_name = build_tree_list (proto_quals, cp_type);
29248 return type_name;
29251 /* Check to see if TYPE refers to an Objective-C selector name. */
29253 static bool
29254 cp_parser_objc_selector_p (enum cpp_ttype type)
29256 return (type == CPP_NAME || type == CPP_KEYWORD
29257 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29258 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29259 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29260 || type == CPP_XOR || type == CPP_XOR_EQ);
29263 /* Parse an Objective-C selector. */
29265 static tree
29266 cp_parser_objc_selector (cp_parser* parser)
29268 cp_token *token = cp_lexer_consume_token (parser->lexer);
29270 if (!cp_parser_objc_selector_p (token->type))
29272 error_at (token->location, "invalid Objective-C++ selector name");
29273 return error_mark_node;
29276 /* C++ operator names are allowed to appear in ObjC selectors. */
29277 switch (token->type)
29279 case CPP_AND_AND: return get_identifier ("and");
29280 case CPP_AND_EQ: return get_identifier ("and_eq");
29281 case CPP_AND: return get_identifier ("bitand");
29282 case CPP_OR: return get_identifier ("bitor");
29283 case CPP_COMPL: return get_identifier ("compl");
29284 case CPP_NOT: return get_identifier ("not");
29285 case CPP_NOT_EQ: return get_identifier ("not_eq");
29286 case CPP_OR_OR: return get_identifier ("or");
29287 case CPP_OR_EQ: return get_identifier ("or_eq");
29288 case CPP_XOR: return get_identifier ("xor");
29289 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29290 default: return token->u.value;
29294 /* Parse an Objective-C params list. */
29296 static tree
29297 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29299 tree params = NULL_TREE;
29300 bool maybe_unary_selector_p = true;
29301 cp_token *token = cp_lexer_peek_token (parser->lexer);
29303 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29305 tree selector = NULL_TREE, type_name, identifier;
29306 tree parm_attr = NULL_TREE;
29308 if (token->keyword == RID_ATTRIBUTE)
29309 break;
29311 if (token->type != CPP_COLON)
29312 selector = cp_parser_objc_selector (parser);
29314 /* Detect if we have a unary selector. */
29315 if (maybe_unary_selector_p
29316 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29318 params = selector; /* Might be followed by attributes. */
29319 break;
29322 maybe_unary_selector_p = false;
29323 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29325 /* Something went quite wrong. There should be a colon
29326 here, but there is not. Stop parsing parameters. */
29327 break;
29329 type_name = cp_parser_objc_typename (parser);
29330 /* New ObjC allows attributes on parameters too. */
29331 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29332 parm_attr = cp_parser_attributes_opt (parser);
29333 identifier = cp_parser_identifier (parser);
29335 params
29336 = chainon (params,
29337 objc_build_keyword_decl (selector,
29338 type_name,
29339 identifier,
29340 parm_attr));
29342 token = cp_lexer_peek_token (parser->lexer);
29345 if (params == NULL_TREE)
29347 cp_parser_error (parser, "objective-c++ method declaration is expected");
29348 return error_mark_node;
29351 /* We allow tail attributes for the method. */
29352 if (token->keyword == RID_ATTRIBUTE)
29354 *attributes = cp_parser_attributes_opt (parser);
29355 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29356 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29357 return params;
29358 cp_parser_error (parser,
29359 "method attributes must be specified at the end");
29360 return error_mark_node;
29363 if (params == NULL_TREE)
29365 cp_parser_error (parser, "objective-c++ method declaration is expected");
29366 return error_mark_node;
29368 return params;
29371 /* Parse the non-keyword Objective-C params. */
29373 static tree
29374 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29375 tree* attributes)
29377 tree params = make_node (TREE_LIST);
29378 cp_token *token = cp_lexer_peek_token (parser->lexer);
29379 *ellipsisp = false; /* Initially, assume no ellipsis. */
29381 while (token->type == CPP_COMMA)
29383 cp_parameter_declarator *parmdecl;
29384 tree parm;
29386 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29387 token = cp_lexer_peek_token (parser->lexer);
29389 if (token->type == CPP_ELLIPSIS)
29391 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29392 *ellipsisp = true;
29393 token = cp_lexer_peek_token (parser->lexer);
29394 break;
29397 /* TODO: parse attributes for tail parameters. */
29398 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29399 parm = grokdeclarator (parmdecl->declarator,
29400 &parmdecl->decl_specifiers,
29401 PARM, /*initialized=*/0,
29402 /*attrlist=*/NULL);
29404 chainon (params, build_tree_list (NULL_TREE, parm));
29405 token = cp_lexer_peek_token (parser->lexer);
29408 /* We allow tail attributes for the method. */
29409 if (token->keyword == RID_ATTRIBUTE)
29411 if (*attributes == NULL_TREE)
29413 *attributes = cp_parser_attributes_opt (parser);
29414 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29415 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29416 return params;
29418 else
29419 /* We have an error, but parse the attributes, so that we can
29420 carry on. */
29421 *attributes = cp_parser_attributes_opt (parser);
29423 cp_parser_error (parser,
29424 "method attributes must be specified at the end");
29425 return error_mark_node;
29428 return params;
29431 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29433 static void
29434 cp_parser_objc_interstitial_code (cp_parser* parser)
29436 cp_token *token = cp_lexer_peek_token (parser->lexer);
29438 /* If the next token is `extern' and the following token is a string
29439 literal, then we have a linkage specification. */
29440 if (token->keyword == RID_EXTERN
29441 && cp_parser_is_pure_string_literal
29442 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29443 cp_parser_linkage_specification (parser);
29444 /* Handle #pragma, if any. */
29445 else if (token->type == CPP_PRAGMA)
29446 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29447 /* Allow stray semicolons. */
29448 else if (token->type == CPP_SEMICOLON)
29449 cp_lexer_consume_token (parser->lexer);
29450 /* Mark methods as optional or required, when building protocols. */
29451 else if (token->keyword == RID_AT_OPTIONAL)
29453 cp_lexer_consume_token (parser->lexer);
29454 objc_set_method_opt (true);
29456 else if (token->keyword == RID_AT_REQUIRED)
29458 cp_lexer_consume_token (parser->lexer);
29459 objc_set_method_opt (false);
29461 else if (token->keyword == RID_NAMESPACE)
29462 cp_parser_namespace_definition (parser);
29463 /* Other stray characters must generate errors. */
29464 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29466 cp_lexer_consume_token (parser->lexer);
29467 error ("stray %qs between Objective-C++ methods",
29468 token->type == CPP_OPEN_BRACE ? "{" : "}");
29470 /* Finally, try to parse a block-declaration, or a function-definition. */
29471 else
29472 cp_parser_block_declaration (parser, /*statement_p=*/false);
29475 /* Parse a method signature. */
29477 static tree
29478 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29480 tree rettype, kwdparms, optparms;
29481 bool ellipsis = false;
29482 bool is_class_method;
29484 is_class_method = cp_parser_objc_method_type (parser);
29485 rettype = cp_parser_objc_typename (parser);
29486 *attributes = NULL_TREE;
29487 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29488 if (kwdparms == error_mark_node)
29489 return error_mark_node;
29490 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29491 if (optparms == error_mark_node)
29492 return error_mark_node;
29494 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29497 static bool
29498 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29500 tree tattr;
29501 cp_lexer_save_tokens (parser->lexer);
29502 tattr = cp_parser_attributes_opt (parser);
29503 gcc_assert (tattr) ;
29505 /* If the attributes are followed by a method introducer, this is not allowed.
29506 Dump the attributes and flag the situation. */
29507 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29508 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29509 return true;
29511 /* Otherwise, the attributes introduce some interstitial code, possibly so
29512 rewind to allow that check. */
29513 cp_lexer_rollback_tokens (parser->lexer);
29514 return false;
29517 /* Parse an Objective-C method prototype list. */
29519 static void
29520 cp_parser_objc_method_prototype_list (cp_parser* parser)
29522 cp_token *token = cp_lexer_peek_token (parser->lexer);
29524 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29526 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29528 tree attributes, sig;
29529 bool is_class_method;
29530 if (token->type == CPP_PLUS)
29531 is_class_method = true;
29532 else
29533 is_class_method = false;
29534 sig = cp_parser_objc_method_signature (parser, &attributes);
29535 if (sig == error_mark_node)
29537 cp_parser_skip_to_end_of_block_or_statement (parser);
29538 token = cp_lexer_peek_token (parser->lexer);
29539 continue;
29541 objc_add_method_declaration (is_class_method, sig, attributes);
29542 cp_parser_consume_semicolon_at_end_of_statement (parser);
29544 else if (token->keyword == RID_AT_PROPERTY)
29545 cp_parser_objc_at_property_declaration (parser);
29546 else if (token->keyword == RID_ATTRIBUTE
29547 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29548 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29549 OPT_Wattributes,
29550 "prefix attributes are ignored for methods");
29551 else
29552 /* Allow for interspersed non-ObjC++ code. */
29553 cp_parser_objc_interstitial_code (parser);
29555 token = cp_lexer_peek_token (parser->lexer);
29558 if (token->type != CPP_EOF)
29559 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29560 else
29561 cp_parser_error (parser, "expected %<@end%>");
29563 objc_finish_interface ();
29566 /* Parse an Objective-C method definition list. */
29568 static void
29569 cp_parser_objc_method_definition_list (cp_parser* parser)
29571 cp_token *token = cp_lexer_peek_token (parser->lexer);
29573 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29575 tree meth;
29577 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29579 cp_token *ptk;
29580 tree sig, attribute;
29581 bool is_class_method;
29582 if (token->type == CPP_PLUS)
29583 is_class_method = true;
29584 else
29585 is_class_method = false;
29586 push_deferring_access_checks (dk_deferred);
29587 sig = cp_parser_objc_method_signature (parser, &attribute);
29588 if (sig == error_mark_node)
29590 cp_parser_skip_to_end_of_block_or_statement (parser);
29591 token = cp_lexer_peek_token (parser->lexer);
29592 continue;
29594 objc_start_method_definition (is_class_method, sig, attribute,
29595 NULL_TREE);
29597 /* For historical reasons, we accept an optional semicolon. */
29598 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29599 cp_lexer_consume_token (parser->lexer);
29601 ptk = cp_lexer_peek_token (parser->lexer);
29602 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29603 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29605 perform_deferred_access_checks (tf_warning_or_error);
29606 stop_deferring_access_checks ();
29607 meth = cp_parser_function_definition_after_declarator (parser,
29608 false);
29609 pop_deferring_access_checks ();
29610 objc_finish_method_definition (meth);
29613 /* The following case will be removed once @synthesize is
29614 completely implemented. */
29615 else if (token->keyword == RID_AT_PROPERTY)
29616 cp_parser_objc_at_property_declaration (parser);
29617 else if (token->keyword == RID_AT_SYNTHESIZE)
29618 cp_parser_objc_at_synthesize_declaration (parser);
29619 else if (token->keyword == RID_AT_DYNAMIC)
29620 cp_parser_objc_at_dynamic_declaration (parser);
29621 else if (token->keyword == RID_ATTRIBUTE
29622 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29623 warning_at (token->location, OPT_Wattributes,
29624 "prefix attributes are ignored for methods");
29625 else
29626 /* Allow for interspersed non-ObjC++ code. */
29627 cp_parser_objc_interstitial_code (parser);
29629 token = cp_lexer_peek_token (parser->lexer);
29632 if (token->type != CPP_EOF)
29633 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29634 else
29635 cp_parser_error (parser, "expected %<@end%>");
29637 objc_finish_implementation ();
29640 /* Parse Objective-C ivars. */
29642 static void
29643 cp_parser_objc_class_ivars (cp_parser* parser)
29645 cp_token *token = cp_lexer_peek_token (parser->lexer);
29647 if (token->type != CPP_OPEN_BRACE)
29648 return; /* No ivars specified. */
29650 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29651 token = cp_lexer_peek_token (parser->lexer);
29653 while (token->type != CPP_CLOSE_BRACE
29654 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29656 cp_decl_specifier_seq declspecs;
29657 int decl_class_or_enum_p;
29658 tree prefix_attributes;
29660 cp_parser_objc_visibility_spec (parser);
29662 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29663 break;
29665 cp_parser_decl_specifier_seq (parser,
29666 CP_PARSER_FLAGS_OPTIONAL,
29667 &declspecs,
29668 &decl_class_or_enum_p);
29670 /* auto, register, static, extern, mutable. */
29671 if (declspecs.storage_class != sc_none)
29673 cp_parser_error (parser, "invalid type for instance variable");
29674 declspecs.storage_class = sc_none;
29677 /* thread_local. */
29678 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29680 cp_parser_error (parser, "invalid type for instance variable");
29681 declspecs.locations[ds_thread] = 0;
29684 /* typedef. */
29685 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29687 cp_parser_error (parser, "invalid type for instance variable");
29688 declspecs.locations[ds_typedef] = 0;
29691 prefix_attributes = declspecs.attributes;
29692 declspecs.attributes = NULL_TREE;
29694 /* Keep going until we hit the `;' at the end of the
29695 declaration. */
29696 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29698 tree width = NULL_TREE, attributes, first_attribute, decl;
29699 cp_declarator *declarator = NULL;
29700 int ctor_dtor_or_conv_p;
29702 /* Check for a (possibly unnamed) bitfield declaration. */
29703 token = cp_lexer_peek_token (parser->lexer);
29704 if (token->type == CPP_COLON)
29705 goto eat_colon;
29707 if (token->type == CPP_NAME
29708 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
29709 == CPP_COLON))
29711 /* Get the name of the bitfield. */
29712 declarator = make_id_declarator (NULL_TREE,
29713 cp_parser_identifier (parser),
29714 sfk_none);
29716 eat_colon:
29717 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29718 /* Get the width of the bitfield. */
29719 width
29720 = cp_parser_constant_expression (parser);
29722 else
29724 /* Parse the declarator. */
29725 declarator
29726 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29727 &ctor_dtor_or_conv_p,
29728 /*parenthesized_p=*/NULL,
29729 /*member_p=*/false,
29730 /*friend_p=*/false);
29733 /* Look for attributes that apply to the ivar. */
29734 attributes = cp_parser_attributes_opt (parser);
29735 /* Remember which attributes are prefix attributes and
29736 which are not. */
29737 first_attribute = attributes;
29738 /* Combine the attributes. */
29739 attributes = chainon (prefix_attributes, attributes);
29741 if (width)
29742 /* Create the bitfield declaration. */
29743 decl = grokbitfield (declarator, &declspecs,
29744 width,
29745 attributes);
29746 else
29747 decl = grokfield (declarator, &declspecs,
29748 NULL_TREE, /*init_const_expr_p=*/false,
29749 NULL_TREE, attributes);
29751 /* Add the instance variable. */
29752 if (decl != error_mark_node && decl != NULL_TREE)
29753 objc_add_instance_variable (decl);
29755 /* Reset PREFIX_ATTRIBUTES. */
29756 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29757 attributes = TREE_CHAIN (attributes);
29758 if (attributes)
29759 TREE_CHAIN (attributes) = NULL_TREE;
29761 token = cp_lexer_peek_token (parser->lexer);
29763 if (token->type == CPP_COMMA)
29765 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29766 continue;
29768 break;
29771 cp_parser_consume_semicolon_at_end_of_statement (parser);
29772 token = cp_lexer_peek_token (parser->lexer);
29775 if (token->keyword == RID_AT_END)
29776 cp_parser_error (parser, "expected %<}%>");
29778 /* Do not consume the RID_AT_END, so it will be read again as terminating
29779 the @interface of @implementation. */
29780 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
29781 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
29783 /* For historical reasons, we accept an optional semicolon. */
29784 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29785 cp_lexer_consume_token (parser->lexer);
29788 /* Parse an Objective-C protocol declaration. */
29790 static void
29791 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
29793 tree proto, protorefs;
29794 cp_token *tok;
29796 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29797 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29799 tok = cp_lexer_peek_token (parser->lexer);
29800 error_at (tok->location, "identifier expected after %<@protocol%>");
29801 cp_parser_consume_semicolon_at_end_of_statement (parser);
29802 return;
29805 /* See if we have a forward declaration or a definition. */
29806 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
29808 /* Try a forward declaration first. */
29809 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
29811 while (true)
29813 tree id;
29815 id = cp_parser_identifier (parser);
29816 if (id == error_mark_node)
29817 break;
29819 objc_declare_protocol (id, attributes);
29821 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29822 cp_lexer_consume_token (parser->lexer);
29823 else
29824 break;
29826 cp_parser_consume_semicolon_at_end_of_statement (parser);
29829 /* Ok, we got a full-fledged definition (or at least should). */
29830 else
29832 proto = cp_parser_identifier (parser);
29833 protorefs = cp_parser_objc_protocol_refs_opt (parser);
29834 objc_start_protocol (proto, protorefs, attributes);
29835 cp_parser_objc_method_prototype_list (parser);
29839 /* Parse an Objective-C superclass or category. */
29841 static void
29842 cp_parser_objc_superclass_or_category (cp_parser *parser,
29843 bool iface_p,
29844 tree *super,
29845 tree *categ, bool *is_class_extension)
29847 cp_token *next = cp_lexer_peek_token (parser->lexer);
29849 *super = *categ = NULL_TREE;
29850 *is_class_extension = false;
29851 if (next->type == CPP_COLON)
29853 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29854 *super = cp_parser_identifier (parser);
29856 else if (next->type == CPP_OPEN_PAREN)
29858 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29860 /* If there is no category name, and this is an @interface, we
29861 have a class extension. */
29862 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29864 *categ = NULL_TREE;
29865 *is_class_extension = true;
29867 else
29868 *categ = cp_parser_identifier (parser);
29870 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29874 /* Parse an Objective-C class interface. */
29876 static void
29877 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29879 tree name, super, categ, protos;
29880 bool is_class_extension;
29882 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
29883 name = cp_parser_identifier (parser);
29884 if (name == error_mark_node)
29886 /* It's hard to recover because even if valid @interface stuff
29887 is to follow, we can't compile it (or validate it) if we
29888 don't even know which class it refers to. Let's assume this
29889 was a stray '@interface' token in the stream and skip it.
29891 return;
29893 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29894 &is_class_extension);
29895 protos = cp_parser_objc_protocol_refs_opt (parser);
29897 /* We have either a class or a category on our hands. */
29898 if (categ || is_class_extension)
29899 objc_start_category_interface (name, categ, protos, attributes);
29900 else
29902 objc_start_class_interface (name, super, protos, attributes);
29903 /* Handle instance variable declarations, if any. */
29904 cp_parser_objc_class_ivars (parser);
29905 objc_continue_interface ();
29908 cp_parser_objc_method_prototype_list (parser);
29911 /* Parse an Objective-C class implementation. */
29913 static void
29914 cp_parser_objc_class_implementation (cp_parser* parser)
29916 tree name, super, categ;
29917 bool is_class_extension;
29919 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
29920 name = cp_parser_identifier (parser);
29921 if (name == error_mark_node)
29923 /* It's hard to recover because even if valid @implementation
29924 stuff is to follow, we can't compile it (or validate it) if
29925 we don't even know which class it refers to. Let's assume
29926 this was a stray '@implementation' token in the stream and
29927 skip it.
29929 return;
29931 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29932 &is_class_extension);
29934 /* We have either a class or a category on our hands. */
29935 if (categ)
29936 objc_start_category_implementation (name, categ);
29937 else
29939 objc_start_class_implementation (name, super);
29940 /* Handle instance variable declarations, if any. */
29941 cp_parser_objc_class_ivars (parser);
29942 objc_continue_implementation ();
29945 cp_parser_objc_method_definition_list (parser);
29948 /* Consume the @end token and finish off the implementation. */
29950 static void
29951 cp_parser_objc_end_implementation (cp_parser* parser)
29953 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29954 objc_finish_implementation ();
29957 /* Parse an Objective-C declaration. */
29959 static void
29960 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29962 /* Try to figure out what kind of declaration is present. */
29963 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29965 if (attributes)
29966 switch (kwd->keyword)
29968 case RID_AT_ALIAS:
29969 case RID_AT_CLASS:
29970 case RID_AT_END:
29971 error_at (kwd->location, "attributes may not be specified before"
29972 " the %<@%D%> Objective-C++ keyword",
29973 kwd->u.value);
29974 attributes = NULL;
29975 break;
29976 case RID_AT_IMPLEMENTATION:
29977 warning_at (kwd->location, OPT_Wattributes,
29978 "prefix attributes are ignored before %<@%D%>",
29979 kwd->u.value);
29980 attributes = NULL;
29981 default:
29982 break;
29985 switch (kwd->keyword)
29987 case RID_AT_ALIAS:
29988 cp_parser_objc_alias_declaration (parser);
29989 break;
29990 case RID_AT_CLASS:
29991 cp_parser_objc_class_declaration (parser);
29992 break;
29993 case RID_AT_PROTOCOL:
29994 cp_parser_objc_protocol_declaration (parser, attributes);
29995 break;
29996 case RID_AT_INTERFACE:
29997 cp_parser_objc_class_interface (parser, attributes);
29998 break;
29999 case RID_AT_IMPLEMENTATION:
30000 cp_parser_objc_class_implementation (parser);
30001 break;
30002 case RID_AT_END:
30003 cp_parser_objc_end_implementation (parser);
30004 break;
30005 default:
30006 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30007 kwd->u.value);
30008 cp_parser_skip_to_end_of_block_or_statement (parser);
30012 /* Parse an Objective-C try-catch-finally statement.
30014 objc-try-catch-finally-stmt:
30015 @try compound-statement objc-catch-clause-seq [opt]
30016 objc-finally-clause [opt]
30018 objc-catch-clause-seq:
30019 objc-catch-clause objc-catch-clause-seq [opt]
30021 objc-catch-clause:
30022 @catch ( objc-exception-declaration ) compound-statement
30024 objc-finally-clause:
30025 @finally compound-statement
30027 objc-exception-declaration:
30028 parameter-declaration
30029 '...'
30031 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30033 Returns NULL_TREE.
30035 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30036 for C. Keep them in sync. */
30038 static tree
30039 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30041 location_t location;
30042 tree stmt;
30044 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30045 location = cp_lexer_peek_token (parser->lexer)->location;
30046 objc_maybe_warn_exceptions (location);
30047 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30048 node, lest it get absorbed into the surrounding block. */
30049 stmt = push_stmt_list ();
30050 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30051 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30053 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30055 cp_parameter_declarator *parm;
30056 tree parameter_declaration = error_mark_node;
30057 bool seen_open_paren = false;
30059 cp_lexer_consume_token (parser->lexer);
30060 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30061 seen_open_paren = true;
30062 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30064 /* We have "@catch (...)" (where the '...' are literally
30065 what is in the code). Skip the '...'.
30066 parameter_declaration is set to NULL_TREE, and
30067 objc_being_catch_clauses() knows that that means
30068 '...'. */
30069 cp_lexer_consume_token (parser->lexer);
30070 parameter_declaration = NULL_TREE;
30072 else
30074 /* We have "@catch (NSException *exception)" or something
30075 like that. Parse the parameter declaration. */
30076 parm = cp_parser_parameter_declaration (parser, false, NULL);
30077 if (parm == NULL)
30078 parameter_declaration = error_mark_node;
30079 else
30080 parameter_declaration = grokdeclarator (parm->declarator,
30081 &parm->decl_specifiers,
30082 PARM, /*initialized=*/0,
30083 /*attrlist=*/NULL);
30085 if (seen_open_paren)
30086 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30087 else
30089 /* If there was no open parenthesis, we are recovering from
30090 an error, and we are trying to figure out what mistake
30091 the user has made. */
30093 /* If there is an immediate closing parenthesis, the user
30094 probably forgot the opening one (ie, they typed "@catch
30095 NSException *e)". Parse the closing parenthesis and keep
30096 going. */
30097 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30098 cp_lexer_consume_token (parser->lexer);
30100 /* If these is no immediate closing parenthesis, the user
30101 probably doesn't know that parenthesis are required at
30102 all (ie, they typed "@catch NSException *e"). So, just
30103 forget about the closing parenthesis and keep going. */
30105 objc_begin_catch_clause (parameter_declaration);
30106 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30107 objc_finish_catch_clause ();
30109 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30111 cp_lexer_consume_token (parser->lexer);
30112 location = cp_lexer_peek_token (parser->lexer)->location;
30113 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30114 node, lest it get absorbed into the surrounding block. */
30115 stmt = push_stmt_list ();
30116 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30117 objc_build_finally_clause (location, pop_stmt_list (stmt));
30120 return objc_finish_try_stmt ();
30123 /* Parse an Objective-C synchronized statement.
30125 objc-synchronized-stmt:
30126 @synchronized ( expression ) compound-statement
30128 Returns NULL_TREE. */
30130 static tree
30131 cp_parser_objc_synchronized_statement (cp_parser *parser)
30133 location_t location;
30134 tree lock, stmt;
30136 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30138 location = cp_lexer_peek_token (parser->lexer)->location;
30139 objc_maybe_warn_exceptions (location);
30140 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30141 lock = cp_parser_expression (parser);
30142 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30144 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30145 node, lest it get absorbed into the surrounding block. */
30146 stmt = push_stmt_list ();
30147 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30149 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30152 /* Parse an Objective-C throw statement.
30154 objc-throw-stmt:
30155 @throw assignment-expression [opt] ;
30157 Returns a constructed '@throw' statement. */
30159 static tree
30160 cp_parser_objc_throw_statement (cp_parser *parser)
30162 tree expr = NULL_TREE;
30163 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30165 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30167 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30168 expr = cp_parser_expression (parser);
30170 cp_parser_consume_semicolon_at_end_of_statement (parser);
30172 return objc_build_throw_stmt (loc, expr);
30175 /* Parse an Objective-C statement. */
30177 static tree
30178 cp_parser_objc_statement (cp_parser * parser)
30180 /* Try to figure out what kind of declaration is present. */
30181 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30183 switch (kwd->keyword)
30185 case RID_AT_TRY:
30186 return cp_parser_objc_try_catch_finally_statement (parser);
30187 case RID_AT_SYNCHRONIZED:
30188 return cp_parser_objc_synchronized_statement (parser);
30189 case RID_AT_THROW:
30190 return cp_parser_objc_throw_statement (parser);
30191 default:
30192 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30193 kwd->u.value);
30194 cp_parser_skip_to_end_of_block_or_statement (parser);
30197 return error_mark_node;
30200 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30201 look ahead to see if an objc keyword follows the attributes. This
30202 is to detect the use of prefix attributes on ObjC @interface and
30203 @protocol. */
30205 static bool
30206 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30208 cp_lexer_save_tokens (parser->lexer);
30209 *attrib = cp_parser_attributes_opt (parser);
30210 gcc_assert (*attrib);
30211 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30213 cp_lexer_commit_tokens (parser->lexer);
30214 return true;
30216 cp_lexer_rollback_tokens (parser->lexer);
30217 return false;
30220 /* This routine is a minimal replacement for
30221 c_parser_struct_declaration () used when parsing the list of
30222 types/names or ObjC++ properties. For example, when parsing the
30223 code
30225 @property (readonly) int a, b, c;
30227 this function is responsible for parsing "int a, int b, int c" and
30228 returning the declarations as CHAIN of DECLs.
30230 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30231 similar parsing. */
30232 static tree
30233 cp_parser_objc_struct_declaration (cp_parser *parser)
30235 tree decls = NULL_TREE;
30236 cp_decl_specifier_seq declspecs;
30237 int decl_class_or_enum_p;
30238 tree prefix_attributes;
30240 cp_parser_decl_specifier_seq (parser,
30241 CP_PARSER_FLAGS_NONE,
30242 &declspecs,
30243 &decl_class_or_enum_p);
30245 if (declspecs.type == error_mark_node)
30246 return error_mark_node;
30248 /* auto, register, static, extern, mutable. */
30249 if (declspecs.storage_class != sc_none)
30251 cp_parser_error (parser, "invalid type for property");
30252 declspecs.storage_class = sc_none;
30255 /* thread_local. */
30256 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30258 cp_parser_error (parser, "invalid type for property");
30259 declspecs.locations[ds_thread] = 0;
30262 /* typedef. */
30263 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30265 cp_parser_error (parser, "invalid type for property");
30266 declspecs.locations[ds_typedef] = 0;
30269 prefix_attributes = declspecs.attributes;
30270 declspecs.attributes = NULL_TREE;
30272 /* Keep going until we hit the `;' at the end of the declaration. */
30273 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30275 tree attributes, first_attribute, decl;
30276 cp_declarator *declarator;
30277 cp_token *token;
30279 /* Parse the declarator. */
30280 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30281 NULL, NULL, false, false);
30283 /* Look for attributes that apply to the ivar. */
30284 attributes = cp_parser_attributes_opt (parser);
30285 /* Remember which attributes are prefix attributes and
30286 which are not. */
30287 first_attribute = attributes;
30288 /* Combine the attributes. */
30289 attributes = chainon (prefix_attributes, attributes);
30291 decl = grokfield (declarator, &declspecs,
30292 NULL_TREE, /*init_const_expr_p=*/false,
30293 NULL_TREE, attributes);
30295 if (decl == error_mark_node || decl == NULL_TREE)
30296 return error_mark_node;
30298 /* Reset PREFIX_ATTRIBUTES. */
30299 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30300 attributes = TREE_CHAIN (attributes);
30301 if (attributes)
30302 TREE_CHAIN (attributes) = NULL_TREE;
30304 DECL_CHAIN (decl) = decls;
30305 decls = decl;
30307 token = cp_lexer_peek_token (parser->lexer);
30308 if (token->type == CPP_COMMA)
30310 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30311 continue;
30313 else
30314 break;
30316 return decls;
30319 /* Parse an Objective-C @property declaration. The syntax is:
30321 objc-property-declaration:
30322 '@property' objc-property-attributes[opt] struct-declaration ;
30324 objc-property-attributes:
30325 '(' objc-property-attribute-list ')'
30327 objc-property-attribute-list:
30328 objc-property-attribute
30329 objc-property-attribute-list, objc-property-attribute
30331 objc-property-attribute
30332 'getter' = identifier
30333 'setter' = identifier
30334 'readonly'
30335 'readwrite'
30336 'assign'
30337 'retain'
30338 'copy'
30339 'nonatomic'
30341 For example:
30342 @property NSString *name;
30343 @property (readonly) id object;
30344 @property (retain, nonatomic, getter=getTheName) id name;
30345 @property int a, b, c;
30347 PS: This function is identical to
30348 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30349 static void
30350 cp_parser_objc_at_property_declaration (cp_parser *parser)
30352 /* The following variables hold the attributes of the properties as
30353 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30354 seen. When we see an attribute, we set them to 'true' (if they
30355 are boolean properties) or to the identifier (if they have an
30356 argument, ie, for getter and setter). Note that here we only
30357 parse the list of attributes, check the syntax and accumulate the
30358 attributes that we find. objc_add_property_declaration() will
30359 then process the information. */
30360 bool property_assign = false;
30361 bool property_copy = false;
30362 tree property_getter_ident = NULL_TREE;
30363 bool property_nonatomic = false;
30364 bool property_readonly = false;
30365 bool property_readwrite = false;
30366 bool property_retain = false;
30367 tree property_setter_ident = NULL_TREE;
30369 /* 'properties' is the list of properties that we read. Usually a
30370 single one, but maybe more (eg, in "@property int a, b, c;" there
30371 are three). */
30372 tree properties;
30373 location_t loc;
30375 loc = cp_lexer_peek_token (parser->lexer)->location;
30377 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30379 /* Parse the optional attribute list... */
30380 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30382 /* Eat the '('. */
30383 cp_lexer_consume_token (parser->lexer);
30385 while (true)
30387 bool syntax_error = false;
30388 cp_token *token = cp_lexer_peek_token (parser->lexer);
30389 enum rid keyword;
30391 if (token->type != CPP_NAME)
30393 cp_parser_error (parser, "expected identifier");
30394 break;
30396 keyword = C_RID_CODE (token->u.value);
30397 cp_lexer_consume_token (parser->lexer);
30398 switch (keyword)
30400 case RID_ASSIGN: property_assign = true; break;
30401 case RID_COPY: property_copy = true; break;
30402 case RID_NONATOMIC: property_nonatomic = true; break;
30403 case RID_READONLY: property_readonly = true; break;
30404 case RID_READWRITE: property_readwrite = true; break;
30405 case RID_RETAIN: property_retain = true; break;
30407 case RID_GETTER:
30408 case RID_SETTER:
30409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30411 if (keyword == RID_GETTER)
30412 cp_parser_error (parser,
30413 "missing %<=%> (after %<getter%> attribute)");
30414 else
30415 cp_parser_error (parser,
30416 "missing %<=%> (after %<setter%> attribute)");
30417 syntax_error = true;
30418 break;
30420 cp_lexer_consume_token (parser->lexer); /* eat the = */
30421 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30423 cp_parser_error (parser, "expected identifier");
30424 syntax_error = true;
30425 break;
30427 if (keyword == RID_SETTER)
30429 if (property_setter_ident != NULL_TREE)
30431 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30432 cp_lexer_consume_token (parser->lexer);
30434 else
30435 property_setter_ident = cp_parser_objc_selector (parser);
30436 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30437 cp_parser_error (parser, "setter name must terminate with %<:%>");
30438 else
30439 cp_lexer_consume_token (parser->lexer);
30441 else
30443 if (property_getter_ident != NULL_TREE)
30445 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30446 cp_lexer_consume_token (parser->lexer);
30448 else
30449 property_getter_ident = cp_parser_objc_selector (parser);
30451 break;
30452 default:
30453 cp_parser_error (parser, "unknown property attribute");
30454 syntax_error = true;
30455 break;
30458 if (syntax_error)
30459 break;
30461 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30462 cp_lexer_consume_token (parser->lexer);
30463 else
30464 break;
30467 /* FIXME: "@property (setter, assign);" will generate a spurious
30468 "error: expected ‘)’ before ‘,’ token". This is because
30469 cp_parser_require, unlike the C counterpart, will produce an
30470 error even if we are in error recovery. */
30471 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30473 cp_parser_skip_to_closing_parenthesis (parser,
30474 /*recovering=*/true,
30475 /*or_comma=*/false,
30476 /*consume_paren=*/true);
30480 /* ... and the property declaration(s). */
30481 properties = cp_parser_objc_struct_declaration (parser);
30483 if (properties == error_mark_node)
30485 cp_parser_skip_to_end_of_statement (parser);
30486 /* If the next token is now a `;', consume it. */
30487 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30488 cp_lexer_consume_token (parser->lexer);
30489 return;
30492 if (properties == NULL_TREE)
30493 cp_parser_error (parser, "expected identifier");
30494 else
30496 /* Comma-separated properties are chained together in
30497 reverse order; add them one by one. */
30498 properties = nreverse (properties);
30500 for (; properties; properties = TREE_CHAIN (properties))
30501 objc_add_property_declaration (loc, copy_node (properties),
30502 property_readonly, property_readwrite,
30503 property_assign, property_retain,
30504 property_copy, property_nonatomic,
30505 property_getter_ident, property_setter_ident);
30508 cp_parser_consume_semicolon_at_end_of_statement (parser);
30511 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30513 objc-synthesize-declaration:
30514 @synthesize objc-synthesize-identifier-list ;
30516 objc-synthesize-identifier-list:
30517 objc-synthesize-identifier
30518 objc-synthesize-identifier-list, objc-synthesize-identifier
30520 objc-synthesize-identifier
30521 identifier
30522 identifier = identifier
30524 For example:
30525 @synthesize MyProperty;
30526 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30528 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30529 for C. Keep them in sync.
30531 static void
30532 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30534 tree list = NULL_TREE;
30535 location_t loc;
30536 loc = cp_lexer_peek_token (parser->lexer)->location;
30538 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30539 while (true)
30541 tree property, ivar;
30542 property = cp_parser_identifier (parser);
30543 if (property == error_mark_node)
30545 cp_parser_consume_semicolon_at_end_of_statement (parser);
30546 return;
30548 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30550 cp_lexer_consume_token (parser->lexer);
30551 ivar = cp_parser_identifier (parser);
30552 if (ivar == error_mark_node)
30554 cp_parser_consume_semicolon_at_end_of_statement (parser);
30555 return;
30558 else
30559 ivar = NULL_TREE;
30560 list = chainon (list, build_tree_list (ivar, property));
30561 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30562 cp_lexer_consume_token (parser->lexer);
30563 else
30564 break;
30566 cp_parser_consume_semicolon_at_end_of_statement (parser);
30567 objc_add_synthesize_declaration (loc, list);
30570 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30572 objc-dynamic-declaration:
30573 @dynamic identifier-list ;
30575 For example:
30576 @dynamic MyProperty;
30577 @dynamic MyProperty, AnotherProperty;
30579 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30580 for C. Keep them in sync.
30582 static void
30583 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30585 tree list = NULL_TREE;
30586 location_t loc;
30587 loc = cp_lexer_peek_token (parser->lexer)->location;
30589 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30590 while (true)
30592 tree property;
30593 property = cp_parser_identifier (parser);
30594 if (property == error_mark_node)
30596 cp_parser_consume_semicolon_at_end_of_statement (parser);
30597 return;
30599 list = chainon (list, build_tree_list (NULL, property));
30600 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30601 cp_lexer_consume_token (parser->lexer);
30602 else
30603 break;
30605 cp_parser_consume_semicolon_at_end_of_statement (parser);
30606 objc_add_dynamic_declaration (loc, list);
30610 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30612 /* Returns name of the next clause.
30613 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30614 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30615 returned and the token is consumed. */
30617 static pragma_omp_clause
30618 cp_parser_omp_clause_name (cp_parser *parser)
30620 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30622 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30623 result = PRAGMA_OACC_CLAUSE_AUTO;
30624 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30625 result = PRAGMA_OMP_CLAUSE_IF;
30626 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30627 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30628 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30629 result = PRAGMA_OACC_CLAUSE_DELETE;
30630 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30631 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30632 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30633 result = PRAGMA_OMP_CLAUSE_FOR;
30634 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30636 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30637 const char *p = IDENTIFIER_POINTER (id);
30639 switch (p[0])
30641 case 'a':
30642 if (!strcmp ("aligned", p))
30643 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30644 else if (!strcmp ("async", p))
30645 result = PRAGMA_OACC_CLAUSE_ASYNC;
30646 break;
30647 case 'c':
30648 if (!strcmp ("collapse", p))
30649 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30650 else if (!strcmp ("copy", p))
30651 result = PRAGMA_OACC_CLAUSE_COPY;
30652 else if (!strcmp ("copyin", p))
30653 result = PRAGMA_OMP_CLAUSE_COPYIN;
30654 else if (!strcmp ("copyout", p))
30655 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30656 else if (!strcmp ("copyprivate", p))
30657 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30658 else if (!strcmp ("create", p))
30659 result = PRAGMA_OACC_CLAUSE_CREATE;
30660 break;
30661 case 'd':
30662 if (!strcmp ("defaultmap", p))
30663 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30664 else if (!strcmp ("depend", p))
30665 result = PRAGMA_OMP_CLAUSE_DEPEND;
30666 else if (!strcmp ("device", p))
30667 result = PRAGMA_OMP_CLAUSE_DEVICE;
30668 else if (!strcmp ("deviceptr", p))
30669 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30670 else if (!strcmp ("device_resident", p))
30671 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30672 else if (!strcmp ("dist_schedule", p))
30673 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30674 break;
30675 case 'f':
30676 if (!strcmp ("final", p))
30677 result = PRAGMA_OMP_CLAUSE_FINAL;
30678 else if (!strcmp ("firstprivate", p))
30679 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30680 else if (!strcmp ("from", p))
30681 result = PRAGMA_OMP_CLAUSE_FROM;
30682 break;
30683 case 'g':
30684 if (!strcmp ("gang", p))
30685 result = PRAGMA_OACC_CLAUSE_GANG;
30686 else if (!strcmp ("grainsize", p))
30687 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
30688 break;
30689 case 'h':
30690 if (!strcmp ("hint", p))
30691 result = PRAGMA_OMP_CLAUSE_HINT;
30692 else if (!strcmp ("host", p))
30693 result = PRAGMA_OACC_CLAUSE_HOST;
30694 break;
30695 case 'i':
30696 if (!strcmp ("inbranch", p))
30697 result = PRAGMA_OMP_CLAUSE_INBRANCH;
30698 else if (!strcmp ("independent", p))
30699 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
30700 else if (!strcmp ("is_device_ptr", p))
30701 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
30702 break;
30703 case 'l':
30704 if (!strcmp ("lastprivate", p))
30705 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
30706 else if (!strcmp ("linear", p))
30707 result = PRAGMA_OMP_CLAUSE_LINEAR;
30708 else if (!strcmp ("link", p))
30709 result = PRAGMA_OMP_CLAUSE_LINK;
30710 break;
30711 case 'm':
30712 if (!strcmp ("map", p))
30713 result = PRAGMA_OMP_CLAUSE_MAP;
30714 else if (!strcmp ("mergeable", p))
30715 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
30716 else if (flag_cilkplus && !strcmp ("mask", p))
30717 result = PRAGMA_CILK_CLAUSE_MASK;
30718 break;
30719 case 'n':
30720 if (!strcmp ("nogroup", p))
30721 result = PRAGMA_OMP_CLAUSE_NOGROUP;
30722 else if (!strcmp ("notinbranch", p))
30723 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
30724 else if (!strcmp ("nowait", p))
30725 result = PRAGMA_OMP_CLAUSE_NOWAIT;
30726 else if (flag_cilkplus && !strcmp ("nomask", p))
30727 result = PRAGMA_CILK_CLAUSE_NOMASK;
30728 else if (!strcmp ("num_gangs", p))
30729 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
30730 else if (!strcmp ("num_tasks", p))
30731 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
30732 else if (!strcmp ("num_teams", p))
30733 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
30734 else if (!strcmp ("num_threads", p))
30735 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
30736 else if (!strcmp ("num_workers", p))
30737 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
30738 break;
30739 case 'o':
30740 if (!strcmp ("ordered", p))
30741 result = PRAGMA_OMP_CLAUSE_ORDERED;
30742 break;
30743 case 'p':
30744 if (!strcmp ("parallel", p))
30745 result = PRAGMA_OMP_CLAUSE_PARALLEL;
30746 else if (!strcmp ("present", p))
30747 result = PRAGMA_OACC_CLAUSE_PRESENT;
30748 else if (!strcmp ("present_or_copy", p)
30749 || !strcmp ("pcopy", p))
30750 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
30751 else if (!strcmp ("present_or_copyin", p)
30752 || !strcmp ("pcopyin", p))
30753 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
30754 else if (!strcmp ("present_or_copyout", p)
30755 || !strcmp ("pcopyout", p))
30756 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
30757 else if (!strcmp ("present_or_create", p)
30758 || !strcmp ("pcreate", p))
30759 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
30760 else if (!strcmp ("priority", p))
30761 result = PRAGMA_OMP_CLAUSE_PRIORITY;
30762 else if (!strcmp ("proc_bind", p))
30763 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
30764 break;
30765 case 'r':
30766 if (!strcmp ("reduction", p))
30767 result = PRAGMA_OMP_CLAUSE_REDUCTION;
30768 break;
30769 case 's':
30770 if (!strcmp ("safelen", p))
30771 result = PRAGMA_OMP_CLAUSE_SAFELEN;
30772 else if (!strcmp ("schedule", p))
30773 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
30774 else if (!strcmp ("sections", p))
30775 result = PRAGMA_OMP_CLAUSE_SECTIONS;
30776 else if (!strcmp ("self", p))
30777 result = PRAGMA_OACC_CLAUSE_SELF;
30778 else if (!strcmp ("seq", p))
30779 result = PRAGMA_OACC_CLAUSE_SEQ;
30780 else if (!strcmp ("shared", p))
30781 result = PRAGMA_OMP_CLAUSE_SHARED;
30782 else if (!strcmp ("simd", p))
30783 result = PRAGMA_OMP_CLAUSE_SIMD;
30784 else if (!strcmp ("simdlen", p))
30785 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
30786 break;
30787 case 't':
30788 if (!strcmp ("taskgroup", p))
30789 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
30790 else if (!strcmp ("thread_limit", p))
30791 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
30792 else if (!strcmp ("threads", p))
30793 result = PRAGMA_OMP_CLAUSE_THREADS;
30794 else if (!strcmp ("tile", p))
30795 result = PRAGMA_OACC_CLAUSE_TILE;
30796 else if (!strcmp ("to", p))
30797 result = PRAGMA_OMP_CLAUSE_TO;
30798 break;
30799 case 'u':
30800 if (!strcmp ("uniform", p))
30801 result = PRAGMA_OMP_CLAUSE_UNIFORM;
30802 else if (!strcmp ("untied", p))
30803 result = PRAGMA_OMP_CLAUSE_UNTIED;
30804 else if (!strcmp ("use_device", p))
30805 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
30806 else if (!strcmp ("use_device_ptr", p))
30807 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
30808 break;
30809 case 'v':
30810 if (!strcmp ("vector", p))
30811 result = PRAGMA_OACC_CLAUSE_VECTOR;
30812 else if (!strcmp ("vector_length", p))
30813 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
30814 else if (flag_cilkplus && !strcmp ("vectorlength", p))
30815 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
30816 break;
30817 case 'w':
30818 if (!strcmp ("wait", p))
30819 result = PRAGMA_OACC_CLAUSE_WAIT;
30820 else if (!strcmp ("worker", p))
30821 result = PRAGMA_OACC_CLAUSE_WORKER;
30822 break;
30826 if (result != PRAGMA_OMP_CLAUSE_NONE)
30827 cp_lexer_consume_token (parser->lexer);
30829 return result;
30832 /* Validate that a clause of the given type does not already exist. */
30834 static void
30835 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
30836 const char *name, location_t location)
30838 tree c;
30840 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30841 if (OMP_CLAUSE_CODE (c) == code)
30843 error_at (location, "too many %qs clauses", name);
30844 break;
30848 /* OpenMP 2.5:
30849 variable-list:
30850 identifier
30851 variable-list , identifier
30853 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30854 colon). An opening parenthesis will have been consumed by the caller.
30856 If KIND is nonzero, create the appropriate node and install the decl
30857 in OMP_CLAUSE_DECL and add the node to the head of the list.
30859 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30860 return the list created.
30862 COLON can be NULL if only closing parenthesis should end the list,
30863 or pointer to bool which will receive false if the list is terminated
30864 by closing parenthesis or true if the list is terminated by colon. */
30866 static tree
30867 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30868 tree list, bool *colon)
30870 cp_token *token;
30871 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30872 if (colon)
30874 parser->colon_corrects_to_scope_p = false;
30875 *colon = false;
30877 while (1)
30879 tree name, decl;
30881 token = cp_lexer_peek_token (parser->lexer);
30882 if (kind != 0
30883 && current_class_ptr
30884 && cp_parser_is_keyword (token, RID_THIS))
30886 decl = finish_this_expr ();
30887 if (TREE_CODE (decl) == NON_LVALUE_EXPR
30888 || CONVERT_EXPR_P (decl))
30889 decl = TREE_OPERAND (decl, 0);
30890 cp_lexer_consume_token (parser->lexer);
30892 else
30894 name = cp_parser_id_expression (parser, /*template_p=*/false,
30895 /*check_dependency_p=*/true,
30896 /*template_p=*/NULL,
30897 /*declarator_p=*/false,
30898 /*optional_p=*/false);
30899 if (name == error_mark_node)
30900 goto skip_comma;
30902 decl = cp_parser_lookup_name_simple (parser, name, token->location);
30903 if (decl == error_mark_node)
30904 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30905 token->location);
30907 if (decl == error_mark_node)
30909 else if (kind != 0)
30911 switch (kind)
30913 case OMP_CLAUSE__CACHE_:
30914 /* The OpenACC cache directive explicitly only allows "array
30915 elements or subarrays". */
30916 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30918 error_at (token->location, "expected %<[%>");
30919 decl = error_mark_node;
30920 break;
30922 /* FALLTHROUGH. */
30923 case OMP_CLAUSE_MAP:
30924 case OMP_CLAUSE_FROM:
30925 case OMP_CLAUSE_TO:
30926 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30928 location_t loc
30929 = cp_lexer_peek_token (parser->lexer)->location;
30930 cp_id_kind idk = CP_ID_KIND_NONE;
30931 cp_lexer_consume_token (parser->lexer);
30932 decl = convert_from_reference (decl);
30933 decl
30934 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30935 decl, false,
30936 &idk, loc);
30938 /* FALLTHROUGH. */
30939 case OMP_CLAUSE_DEPEND:
30940 case OMP_CLAUSE_REDUCTION:
30941 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30943 tree low_bound = NULL_TREE, length = NULL_TREE;
30945 parser->colon_corrects_to_scope_p = false;
30946 cp_lexer_consume_token (parser->lexer);
30947 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30948 low_bound = cp_parser_expression (parser);
30949 if (!colon)
30950 parser->colon_corrects_to_scope_p
30951 = saved_colon_corrects_to_scope_p;
30952 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30953 length = integer_one_node;
30954 else
30956 /* Look for `:'. */
30957 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30958 goto skip_comma;
30959 if (!cp_lexer_next_token_is (parser->lexer,
30960 CPP_CLOSE_SQUARE))
30961 length = cp_parser_expression (parser);
30963 /* Look for the closing `]'. */
30964 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30965 RT_CLOSE_SQUARE))
30966 goto skip_comma;
30968 decl = tree_cons (low_bound, length, decl);
30970 break;
30971 default:
30972 break;
30975 tree u = build_omp_clause (token->location, kind);
30976 OMP_CLAUSE_DECL (u) = decl;
30977 OMP_CLAUSE_CHAIN (u) = list;
30978 list = u;
30980 else
30981 list = tree_cons (decl, NULL_TREE, list);
30983 get_comma:
30984 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30985 break;
30986 cp_lexer_consume_token (parser->lexer);
30989 if (colon)
30990 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30992 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30994 *colon = true;
30995 cp_parser_require (parser, CPP_COLON, RT_COLON);
30996 return list;
30999 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31001 int ending;
31003 /* Try to resync to an unnested comma. Copied from
31004 cp_parser_parenthesized_expression_list. */
31005 skip_comma:
31006 if (colon)
31007 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31008 ending = cp_parser_skip_to_closing_parenthesis (parser,
31009 /*recovering=*/true,
31010 /*or_comma=*/true,
31011 /*consume_paren=*/true);
31012 if (ending < 0)
31013 goto get_comma;
31016 return list;
31019 /* Similarly, but expect leading and trailing parenthesis. This is a very
31020 common case for omp clauses. */
31022 static tree
31023 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31025 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31026 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31027 return list;
31030 /* OpenACC 2.0:
31031 copy ( variable-list )
31032 copyin ( variable-list )
31033 copyout ( variable-list )
31034 create ( variable-list )
31035 delete ( variable-list )
31036 present ( variable-list )
31037 present_or_copy ( variable-list )
31038 pcopy ( variable-list )
31039 present_or_copyin ( variable-list )
31040 pcopyin ( variable-list )
31041 present_or_copyout ( variable-list )
31042 pcopyout ( variable-list )
31043 present_or_create ( variable-list )
31044 pcreate ( variable-list ) */
31046 static tree
31047 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31048 tree list)
31050 enum gomp_map_kind kind;
31051 switch (c_kind)
31053 case PRAGMA_OACC_CLAUSE_COPY:
31054 kind = GOMP_MAP_FORCE_TOFROM;
31055 break;
31056 case PRAGMA_OACC_CLAUSE_COPYIN:
31057 kind = GOMP_MAP_FORCE_TO;
31058 break;
31059 case PRAGMA_OACC_CLAUSE_COPYOUT:
31060 kind = GOMP_MAP_FORCE_FROM;
31061 break;
31062 case PRAGMA_OACC_CLAUSE_CREATE:
31063 kind = GOMP_MAP_FORCE_ALLOC;
31064 break;
31065 case PRAGMA_OACC_CLAUSE_DELETE:
31066 kind = GOMP_MAP_DELETE;
31067 break;
31068 case PRAGMA_OACC_CLAUSE_DEVICE:
31069 kind = GOMP_MAP_FORCE_TO;
31070 break;
31071 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31072 kind = GOMP_MAP_DEVICE_RESIDENT;
31073 break;
31074 case PRAGMA_OACC_CLAUSE_HOST:
31075 case PRAGMA_OACC_CLAUSE_SELF:
31076 kind = GOMP_MAP_FORCE_FROM;
31077 break;
31078 case PRAGMA_OACC_CLAUSE_LINK:
31079 kind = GOMP_MAP_LINK;
31080 break;
31081 case PRAGMA_OACC_CLAUSE_PRESENT:
31082 kind = GOMP_MAP_FORCE_PRESENT;
31083 break;
31084 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31085 kind = GOMP_MAP_TOFROM;
31086 break;
31087 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31088 kind = GOMP_MAP_TO;
31089 break;
31090 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31091 kind = GOMP_MAP_FROM;
31092 break;
31093 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31094 kind = GOMP_MAP_ALLOC;
31095 break;
31096 default:
31097 gcc_unreachable ();
31099 tree nl, c;
31100 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31102 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31103 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31105 return nl;
31108 /* OpenACC 2.0:
31109 deviceptr ( variable-list ) */
31111 static tree
31112 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31114 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31115 tree vars, t;
31117 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31118 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31119 variable-list must only allow for pointer variables. */
31120 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31121 for (t = vars; t; t = TREE_CHAIN (t))
31123 tree v = TREE_PURPOSE (t);
31124 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31125 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31126 OMP_CLAUSE_DECL (u) = v;
31127 OMP_CLAUSE_CHAIN (u) = list;
31128 list = u;
31131 return list;
31134 /* OpenACC 2.0:
31135 auto
31136 independent
31137 nohost
31138 seq */
31140 static tree
31141 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31142 enum omp_clause_code code,
31143 tree list, location_t location)
31145 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31146 tree c = build_omp_clause (location, code);
31147 OMP_CLAUSE_CHAIN (c) = list;
31148 return c;
31151 /* OpenACC:
31152 num_gangs ( expression )
31153 num_workers ( expression )
31154 vector_length ( expression ) */
31156 static tree
31157 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31158 const char *str, tree list)
31160 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31162 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31163 return list;
31165 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31167 if (t == error_mark_node
31168 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31170 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31171 /*or_comma=*/false,
31172 /*consume_paren=*/true);
31173 return list;
31176 check_no_duplicate_clause (list, code, str, loc);
31178 tree c = build_omp_clause (loc, code);
31179 OMP_CLAUSE_OPERAND (c, 0) = t;
31180 OMP_CLAUSE_CHAIN (c) = list;
31181 return c;
31184 /* OpenACC:
31186 gang [( gang-arg-list )]
31187 worker [( [num:] int-expr )]
31188 vector [( [length:] int-expr )]
31190 where gang-arg is one of:
31192 [num:] int-expr
31193 static: size-expr
31195 and size-expr may be:
31198 int-expr
31201 static tree
31202 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31203 const char *str, tree list)
31205 const char *id = "num";
31206 cp_lexer *lexer = parser->lexer;
31207 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31208 location_t loc = cp_lexer_peek_token (lexer)->location;
31210 if (kind == OMP_CLAUSE_VECTOR)
31211 id = "length";
31213 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31215 cp_lexer_consume_token (lexer);
31219 cp_token *next = cp_lexer_peek_token (lexer);
31220 int idx = 0;
31222 /* Gang static argument. */
31223 if (kind == OMP_CLAUSE_GANG
31224 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31226 cp_lexer_consume_token (lexer);
31228 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31229 goto cleanup_error;
31231 idx = 1;
31232 if (ops[idx] != NULL)
31234 cp_parser_error (parser, "too many %<static%> arguments");
31235 goto cleanup_error;
31238 /* Check for the '*' argument. */
31239 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31240 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31241 || cp_lexer_nth_token_is (parser->lexer, 2,
31242 CPP_CLOSE_PAREN)))
31244 cp_lexer_consume_token (lexer);
31245 ops[idx] = integer_minus_one_node;
31247 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31249 cp_lexer_consume_token (lexer);
31250 continue;
31252 else break;
31255 /* Worker num: argument and vector length: arguments. */
31256 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31257 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
31258 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31260 cp_lexer_consume_token (lexer); /* id */
31261 cp_lexer_consume_token (lexer); /* ':' */
31264 /* Now collect the actual argument. */
31265 if (ops[idx] != NULL_TREE)
31267 cp_parser_error (parser, "unexpected argument");
31268 goto cleanup_error;
31271 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31272 false);
31273 if (expr == error_mark_node)
31274 goto cleanup_error;
31276 mark_exp_read (expr);
31277 ops[idx] = expr;
31279 if (kind == OMP_CLAUSE_GANG
31280 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31282 cp_lexer_consume_token (lexer);
31283 continue;
31285 break;
31287 while (1);
31289 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31290 goto cleanup_error;
31293 check_no_duplicate_clause (list, kind, str, loc);
31295 c = build_omp_clause (loc, kind);
31297 if (ops[1])
31298 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31300 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31301 OMP_CLAUSE_CHAIN (c) = list;
31303 return c;
31305 cleanup_error:
31306 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31307 return list;
31310 /* OpenACC 2.0:
31311 tile ( size-expr-list ) */
31313 static tree
31314 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31316 tree c, expr = error_mark_node;
31317 tree tile = NULL_TREE;
31319 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31320 so, but the spec authors never considered such a case and have
31321 differing opinions on what it might mean, including 'not
31322 allowed'.) */
31323 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31324 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31325 clause_loc);
31327 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31328 return list;
31332 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31333 return list;
31335 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31336 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31337 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31339 cp_lexer_consume_token (parser->lexer);
31340 expr = integer_zero_node;
31342 else
31343 expr = cp_parser_constant_expression (parser);
31345 tile = tree_cons (NULL_TREE, expr, tile);
31347 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31349 /* Consume the trailing ')'. */
31350 cp_lexer_consume_token (parser->lexer);
31352 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31353 tile = nreverse (tile);
31354 OMP_CLAUSE_TILE_LIST (c) = tile;
31355 OMP_CLAUSE_CHAIN (c) = list;
31356 return c;
31359 /* OpenACC 2.0
31360 Parse wait clause or directive parameters. */
31362 static tree
31363 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31365 vec<tree, va_gc> *args;
31366 tree t, args_tree;
31368 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31369 /*cast_p=*/false,
31370 /*allow_expansion_p=*/true,
31371 /*non_constant_p=*/NULL);
31373 if (args == NULL || args->length () == 0)
31375 cp_parser_error (parser, "expected integer expression before ')'");
31376 if (args != NULL)
31377 release_tree_vector (args);
31378 return list;
31381 args_tree = build_tree_list_vec (args);
31383 release_tree_vector (args);
31385 for (t = args_tree; t; t = TREE_CHAIN (t))
31387 tree targ = TREE_VALUE (t);
31389 if (targ != error_mark_node)
31391 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31392 error ("%<wait%> expression must be integral");
31393 else
31395 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31397 mark_rvalue_use (targ);
31398 OMP_CLAUSE_DECL (c) = targ;
31399 OMP_CLAUSE_CHAIN (c) = list;
31400 list = c;
31405 return list;
31408 /* OpenACC:
31409 wait ( int-expr-list ) */
31411 static tree
31412 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31414 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31416 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31417 return list;
31419 list = cp_parser_oacc_wait_list (parser, location, list);
31421 return list;
31424 /* OpenMP 3.0:
31425 collapse ( constant-expression ) */
31427 static tree
31428 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31430 tree c, num;
31431 location_t loc;
31432 HOST_WIDE_INT n;
31434 loc = cp_lexer_peek_token (parser->lexer)->location;
31435 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31436 return list;
31438 num = cp_parser_constant_expression (parser);
31440 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31441 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31442 /*or_comma=*/false,
31443 /*consume_paren=*/true);
31445 if (num == error_mark_node)
31446 return list;
31447 num = fold_non_dependent_expr (num);
31448 if (!tree_fits_shwi_p (num)
31449 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31450 || (n = tree_to_shwi (num)) <= 0
31451 || (int) n != n)
31453 error_at (loc, "collapse argument needs positive constant integer expression");
31454 return list;
31457 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31458 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31459 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31460 OMP_CLAUSE_CHAIN (c) = list;
31461 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31463 return c;
31466 /* OpenMP 2.5:
31467 default ( none | shared )
31469 OpenACC:
31470 default ( none | present ) */
31472 static tree
31473 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31474 location_t location, bool is_oacc)
31476 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31477 tree c;
31479 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31480 return list;
31481 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31483 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31484 const char *p = IDENTIFIER_POINTER (id);
31486 switch (p[0])
31488 case 'n':
31489 if (strcmp ("none", p) != 0)
31490 goto invalid_kind;
31491 kind = OMP_CLAUSE_DEFAULT_NONE;
31492 break;
31494 case 'p':
31495 if (strcmp ("present", p) != 0 || !is_oacc)
31496 goto invalid_kind;
31497 kind = OMP_CLAUSE_DEFAULT_PRESENT;
31498 break;
31500 case 's':
31501 if (strcmp ("shared", p) != 0 || is_oacc)
31502 goto invalid_kind;
31503 kind = OMP_CLAUSE_DEFAULT_SHARED;
31504 break;
31506 default:
31507 goto invalid_kind;
31510 cp_lexer_consume_token (parser->lexer);
31512 else
31514 invalid_kind:
31515 if (is_oacc)
31516 cp_parser_error (parser, "expected %<none%> or %<present%>");
31517 else
31518 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31521 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
31522 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31523 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31524 /*or_comma=*/false,
31525 /*consume_paren=*/true);
31527 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31528 return list;
31530 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31531 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31532 OMP_CLAUSE_CHAIN (c) = list;
31533 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31535 return c;
31538 /* OpenMP 3.1:
31539 final ( expression ) */
31541 static tree
31542 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31544 tree t, c;
31546 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31547 return list;
31549 t = cp_parser_condition (parser);
31551 if (t == error_mark_node
31552 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31553 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31554 /*or_comma=*/false,
31555 /*consume_paren=*/true);
31557 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31559 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31560 OMP_CLAUSE_FINAL_EXPR (c) = t;
31561 OMP_CLAUSE_CHAIN (c) = list;
31563 return c;
31566 /* OpenMP 2.5:
31567 if ( expression )
31569 OpenMP 4.5:
31570 if ( directive-name-modifier : expression )
31572 directive-name-modifier:
31573 parallel | task | taskloop | target data | target | target update
31574 | target enter data | target exit data */
31576 static tree
31577 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31578 bool is_omp)
31580 tree t, c;
31581 enum tree_code if_modifier = ERROR_MARK;
31583 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31584 return list;
31586 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31588 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31589 const char *p = IDENTIFIER_POINTER (id);
31590 int n = 2;
31592 if (strcmp ("parallel", p) == 0)
31593 if_modifier = OMP_PARALLEL;
31594 else if (strcmp ("task", p) == 0)
31595 if_modifier = OMP_TASK;
31596 else if (strcmp ("taskloop", p) == 0)
31597 if_modifier = OMP_TASKLOOP;
31598 else if (strcmp ("target", p) == 0)
31600 if_modifier = OMP_TARGET;
31601 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31603 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31604 p = IDENTIFIER_POINTER (id);
31605 if (strcmp ("data", p) == 0)
31606 if_modifier = OMP_TARGET_DATA;
31607 else if (strcmp ("update", p) == 0)
31608 if_modifier = OMP_TARGET_UPDATE;
31609 else if (strcmp ("enter", p) == 0)
31610 if_modifier = OMP_TARGET_ENTER_DATA;
31611 else if (strcmp ("exit", p) == 0)
31612 if_modifier = OMP_TARGET_EXIT_DATA;
31613 if (if_modifier != OMP_TARGET)
31614 n = 3;
31615 else
31617 location_t loc
31618 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31619 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31620 "or %<exit%>");
31621 if_modifier = ERROR_MARK;
31623 if (if_modifier == OMP_TARGET_ENTER_DATA
31624 || if_modifier == OMP_TARGET_EXIT_DATA)
31626 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31628 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31629 p = IDENTIFIER_POINTER (id);
31630 if (strcmp ("data", p) == 0)
31631 n = 4;
31633 if (n != 4)
31635 location_t loc
31636 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31637 error_at (loc, "expected %<data%>");
31638 if_modifier = ERROR_MARK;
31643 if (if_modifier != ERROR_MARK)
31645 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31647 while (n-- > 0)
31648 cp_lexer_consume_token (parser->lexer);
31650 else
31652 if (n > 2)
31654 location_t loc
31655 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31656 error_at (loc, "expected %<:%>");
31658 if_modifier = ERROR_MARK;
31663 t = cp_parser_condition (parser);
31665 if (t == error_mark_node
31666 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31667 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31668 /*or_comma=*/false,
31669 /*consume_paren=*/true);
31671 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31672 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31674 if (if_modifier != ERROR_MARK
31675 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31677 const char *p = NULL;
31678 switch (if_modifier)
31680 case OMP_PARALLEL: p = "parallel"; break;
31681 case OMP_TASK: p = "task"; break;
31682 case OMP_TASKLOOP: p = "taskloop"; break;
31683 case OMP_TARGET_DATA: p = "target data"; break;
31684 case OMP_TARGET: p = "target"; break;
31685 case OMP_TARGET_UPDATE: p = "target update"; break;
31686 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
31687 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
31688 default: gcc_unreachable ();
31690 error_at (location, "too many %<if%> clauses with %qs modifier",
31692 return list;
31694 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31696 if (!is_omp)
31697 error_at (location, "too many %<if%> clauses");
31698 else
31699 error_at (location, "too many %<if%> clauses without modifier");
31700 return list;
31702 else if (if_modifier == ERROR_MARK
31703 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
31705 error_at (location, "if any %<if%> clause has modifier, then all "
31706 "%<if%> clauses have to use modifier");
31707 return list;
31711 c = build_omp_clause (location, OMP_CLAUSE_IF);
31712 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
31713 OMP_CLAUSE_IF_EXPR (c) = t;
31714 OMP_CLAUSE_CHAIN (c) = list;
31716 return c;
31719 /* OpenMP 3.1:
31720 mergeable */
31722 static tree
31723 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
31724 tree list, location_t location)
31726 tree c;
31728 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
31729 location);
31731 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
31732 OMP_CLAUSE_CHAIN (c) = list;
31733 return c;
31736 /* OpenMP 2.5:
31737 nowait */
31739 static tree
31740 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
31741 tree list, location_t location)
31743 tree c;
31745 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
31747 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
31748 OMP_CLAUSE_CHAIN (c) = list;
31749 return c;
31752 /* OpenMP 2.5:
31753 num_threads ( expression ) */
31755 static tree
31756 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
31757 location_t location)
31759 tree t, c;
31761 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31762 return list;
31764 t = cp_parser_expression (parser);
31766 if (t == error_mark_node
31767 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31768 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31769 /*or_comma=*/false,
31770 /*consume_paren=*/true);
31772 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
31773 "num_threads", location);
31775 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
31776 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
31777 OMP_CLAUSE_CHAIN (c) = list;
31779 return c;
31782 /* OpenMP 4.5:
31783 num_tasks ( expression ) */
31785 static tree
31786 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
31787 location_t location)
31789 tree t, c;
31791 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31792 return list;
31794 t = cp_parser_expression (parser);
31796 if (t == error_mark_node
31797 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31798 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31799 /*or_comma=*/false,
31800 /*consume_paren=*/true);
31802 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
31803 "num_tasks", location);
31805 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
31806 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
31807 OMP_CLAUSE_CHAIN (c) = list;
31809 return c;
31812 /* OpenMP 4.5:
31813 grainsize ( expression ) */
31815 static tree
31816 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
31817 location_t location)
31819 tree t, c;
31821 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31822 return list;
31824 t = cp_parser_expression (parser);
31826 if (t == error_mark_node
31827 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31828 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31829 /*or_comma=*/false,
31830 /*consume_paren=*/true);
31832 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
31833 "grainsize", location);
31835 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
31836 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
31837 OMP_CLAUSE_CHAIN (c) = list;
31839 return c;
31842 /* OpenMP 4.5:
31843 priority ( expression ) */
31845 static tree
31846 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
31847 location_t location)
31849 tree t, c;
31851 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31852 return list;
31854 t = cp_parser_expression (parser);
31856 if (t == error_mark_node
31857 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31858 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31859 /*or_comma=*/false,
31860 /*consume_paren=*/true);
31862 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
31863 "priority", location);
31865 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
31866 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
31867 OMP_CLAUSE_CHAIN (c) = list;
31869 return c;
31872 /* OpenMP 4.5:
31873 hint ( expression ) */
31875 static tree
31876 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
31877 location_t location)
31879 tree t, c;
31881 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31882 return list;
31884 t = cp_parser_expression (parser);
31886 if (t == error_mark_node
31887 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31888 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31889 /*or_comma=*/false,
31890 /*consume_paren=*/true);
31892 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31894 c = build_omp_clause (location, OMP_CLAUSE_HINT);
31895 OMP_CLAUSE_HINT_EXPR (c) = t;
31896 OMP_CLAUSE_CHAIN (c) = list;
31898 return c;
31901 /* OpenMP 4.5:
31902 defaultmap ( tofrom : scalar ) */
31904 static tree
31905 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31906 location_t location)
31908 tree c, id;
31909 const char *p;
31911 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31912 return list;
31914 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31916 cp_parser_error (parser, "expected %<tofrom%>");
31917 goto out_err;
31919 id = cp_lexer_peek_token (parser->lexer)->u.value;
31920 p = IDENTIFIER_POINTER (id);
31921 if (strcmp (p, "tofrom") != 0)
31923 cp_parser_error (parser, "expected %<tofrom%>");
31924 goto out_err;
31926 cp_lexer_consume_token (parser->lexer);
31927 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31928 goto out_err;
31930 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31932 cp_parser_error (parser, "expected %<scalar%>");
31933 goto out_err;
31935 id = cp_lexer_peek_token (parser->lexer)->u.value;
31936 p = IDENTIFIER_POINTER (id);
31937 if (strcmp (p, "scalar") != 0)
31939 cp_parser_error (parser, "expected %<scalar%>");
31940 goto out_err;
31942 cp_lexer_consume_token (parser->lexer);
31943 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31944 goto out_err;
31946 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31947 location);
31949 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31950 OMP_CLAUSE_CHAIN (c) = list;
31951 return c;
31953 out_err:
31954 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31955 /*or_comma=*/false,
31956 /*consume_paren=*/true);
31957 return list;
31960 /* OpenMP 2.5:
31961 ordered
31963 OpenMP 4.5:
31964 ordered ( constant-expression ) */
31966 static tree
31967 cp_parser_omp_clause_ordered (cp_parser *parser,
31968 tree list, location_t location)
31970 tree c, num = NULL_TREE;
31971 HOST_WIDE_INT n;
31973 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31974 "ordered", location);
31976 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31978 cp_lexer_consume_token (parser->lexer);
31980 num = cp_parser_constant_expression (parser);
31982 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31983 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31984 /*or_comma=*/false,
31985 /*consume_paren=*/true);
31987 if (num == error_mark_node)
31988 return list;
31989 num = fold_non_dependent_expr (num);
31990 if (!tree_fits_shwi_p (num)
31991 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31992 || (n = tree_to_shwi (num)) <= 0
31993 || (int) n != n)
31995 error_at (location,
31996 "ordered argument needs positive constant integer "
31997 "expression");
31998 return list;
32002 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32003 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32004 OMP_CLAUSE_CHAIN (c) = list;
32005 return c;
32008 /* OpenMP 2.5:
32009 reduction ( reduction-operator : variable-list )
32011 reduction-operator:
32012 One of: + * - & ^ | && ||
32014 OpenMP 3.1:
32016 reduction-operator:
32017 One of: + * - & ^ | && || min max
32019 OpenMP 4.0:
32021 reduction-operator:
32022 One of: + * - & ^ | && ||
32023 id-expression */
32025 static tree
32026 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32028 enum tree_code code = ERROR_MARK;
32029 tree nlist, c, id = NULL_TREE;
32031 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32032 return list;
32034 switch (cp_lexer_peek_token (parser->lexer)->type)
32036 case CPP_PLUS: code = PLUS_EXPR; break;
32037 case CPP_MULT: code = MULT_EXPR; break;
32038 case CPP_MINUS: code = MINUS_EXPR; break;
32039 case CPP_AND: code = BIT_AND_EXPR; break;
32040 case CPP_XOR: code = BIT_XOR_EXPR; break;
32041 case CPP_OR: code = BIT_IOR_EXPR; break;
32042 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32043 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32044 default: break;
32047 if (code != ERROR_MARK)
32048 cp_lexer_consume_token (parser->lexer);
32049 else
32051 bool saved_colon_corrects_to_scope_p;
32052 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32053 parser->colon_corrects_to_scope_p = false;
32054 id = cp_parser_id_expression (parser, /*template_p=*/false,
32055 /*check_dependency_p=*/true,
32056 /*template_p=*/NULL,
32057 /*declarator_p=*/false,
32058 /*optional_p=*/false);
32059 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32060 if (identifier_p (id))
32062 const char *p = IDENTIFIER_POINTER (id);
32064 if (strcmp (p, "min") == 0)
32065 code = MIN_EXPR;
32066 else if (strcmp (p, "max") == 0)
32067 code = MAX_EXPR;
32068 else if (id == cp_operator_id (PLUS_EXPR))
32069 code = PLUS_EXPR;
32070 else if (id == cp_operator_id (MULT_EXPR))
32071 code = MULT_EXPR;
32072 else if (id == cp_operator_id (MINUS_EXPR))
32073 code = MINUS_EXPR;
32074 else if (id == cp_operator_id (BIT_AND_EXPR))
32075 code = BIT_AND_EXPR;
32076 else if (id == cp_operator_id (BIT_IOR_EXPR))
32077 code = BIT_IOR_EXPR;
32078 else if (id == cp_operator_id (BIT_XOR_EXPR))
32079 code = BIT_XOR_EXPR;
32080 else if (id == cp_operator_id (TRUTH_ANDIF_EXPR))
32081 code = TRUTH_ANDIF_EXPR;
32082 else if (id == cp_operator_id (TRUTH_ORIF_EXPR))
32083 code = TRUTH_ORIF_EXPR;
32084 id = omp_reduction_id (code, id, NULL_TREE);
32085 tree scope = parser->scope;
32086 if (scope)
32087 id = build_qualified_name (NULL_TREE, scope, id, false);
32088 parser->scope = NULL_TREE;
32089 parser->qualifying_scope = NULL_TREE;
32090 parser->object_scope = NULL_TREE;
32092 else
32094 error ("invalid reduction-identifier");
32095 resync_fail:
32096 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32097 /*or_comma=*/false,
32098 /*consume_paren=*/true);
32099 return list;
32103 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32104 goto resync_fail;
32106 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32107 NULL);
32108 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32110 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32111 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32114 return nlist;
32117 /* OpenMP 2.5:
32118 schedule ( schedule-kind )
32119 schedule ( schedule-kind , expression )
32121 schedule-kind:
32122 static | dynamic | guided | runtime | auto
32124 OpenMP 4.5:
32125 schedule ( schedule-modifier : schedule-kind )
32126 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32128 schedule-modifier:
32129 simd
32130 monotonic
32131 nonmonotonic */
32133 static tree
32134 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32136 tree c, t;
32137 int modifiers = 0, nmodifiers = 0;
32139 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32140 return list;
32142 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32144 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32146 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32147 const char *p = IDENTIFIER_POINTER (id);
32148 if (strcmp ("simd", p) == 0)
32149 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32150 else if (strcmp ("monotonic", p) == 0)
32151 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32152 else if (strcmp ("nonmonotonic", p) == 0)
32153 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32154 else
32155 break;
32156 cp_lexer_consume_token (parser->lexer);
32157 if (nmodifiers++ == 0
32158 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32159 cp_lexer_consume_token (parser->lexer);
32160 else
32162 cp_parser_require (parser, CPP_COLON, RT_COLON);
32163 break;
32167 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32169 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32170 const char *p = IDENTIFIER_POINTER (id);
32172 switch (p[0])
32174 case 'd':
32175 if (strcmp ("dynamic", p) != 0)
32176 goto invalid_kind;
32177 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32178 break;
32180 case 'g':
32181 if (strcmp ("guided", p) != 0)
32182 goto invalid_kind;
32183 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32184 break;
32186 case 'r':
32187 if (strcmp ("runtime", p) != 0)
32188 goto invalid_kind;
32189 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32190 break;
32192 default:
32193 goto invalid_kind;
32196 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32197 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32198 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32199 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32200 else
32201 goto invalid_kind;
32202 cp_lexer_consume_token (parser->lexer);
32204 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32205 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32206 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32207 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32209 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32210 "specified");
32211 modifiers = 0;
32214 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32216 cp_token *token;
32217 cp_lexer_consume_token (parser->lexer);
32219 token = cp_lexer_peek_token (parser->lexer);
32220 t = cp_parser_assignment_expression (parser);
32222 if (t == error_mark_node)
32223 goto resync_fail;
32224 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32225 error_at (token->location, "schedule %<runtime%> does not take "
32226 "a %<chunk_size%> parameter");
32227 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32228 error_at (token->location, "schedule %<auto%> does not take "
32229 "a %<chunk_size%> parameter");
32230 else
32231 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32233 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32234 goto resync_fail;
32236 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32237 goto resync_fail;
32239 OMP_CLAUSE_SCHEDULE_KIND (c)
32240 = (enum omp_clause_schedule_kind)
32241 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32243 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32244 OMP_CLAUSE_CHAIN (c) = list;
32245 return c;
32247 invalid_kind:
32248 cp_parser_error (parser, "invalid schedule kind");
32249 resync_fail:
32250 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32251 /*or_comma=*/false,
32252 /*consume_paren=*/true);
32253 return list;
32256 /* OpenMP 3.0:
32257 untied */
32259 static tree
32260 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32261 tree list, location_t location)
32263 tree c;
32265 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32267 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32268 OMP_CLAUSE_CHAIN (c) = list;
32269 return c;
32272 /* OpenMP 4.0:
32273 inbranch
32274 notinbranch */
32276 static tree
32277 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32278 tree list, location_t location)
32280 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32281 tree c = build_omp_clause (location, code);
32282 OMP_CLAUSE_CHAIN (c) = list;
32283 return c;
32286 /* OpenMP 4.0:
32287 parallel
32289 sections
32290 taskgroup */
32292 static tree
32293 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32294 enum omp_clause_code code,
32295 tree list, location_t location)
32297 tree c = build_omp_clause (location, code);
32298 OMP_CLAUSE_CHAIN (c) = list;
32299 return c;
32302 /* OpenMP 4.5:
32303 nogroup */
32305 static tree
32306 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32307 tree list, location_t location)
32309 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32310 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32311 OMP_CLAUSE_CHAIN (c) = list;
32312 return c;
32315 /* OpenMP 4.5:
32316 simd
32317 threads */
32319 static tree
32320 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32321 enum omp_clause_code code,
32322 tree list, location_t location)
32324 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32325 tree c = build_omp_clause (location, code);
32326 OMP_CLAUSE_CHAIN (c) = list;
32327 return c;
32330 /* OpenMP 4.0:
32331 num_teams ( expression ) */
32333 static tree
32334 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32335 location_t location)
32337 tree t, c;
32339 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32340 return list;
32342 t = cp_parser_expression (parser);
32344 if (t == error_mark_node
32345 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32346 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32347 /*or_comma=*/false,
32348 /*consume_paren=*/true);
32350 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32351 "num_teams", location);
32353 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32354 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32355 OMP_CLAUSE_CHAIN (c) = list;
32357 return c;
32360 /* OpenMP 4.0:
32361 thread_limit ( expression ) */
32363 static tree
32364 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32365 location_t location)
32367 tree t, c;
32369 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32370 return list;
32372 t = cp_parser_expression (parser);
32374 if (t == error_mark_node
32375 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32376 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32377 /*or_comma=*/false,
32378 /*consume_paren=*/true);
32380 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32381 "thread_limit", location);
32383 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32384 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32385 OMP_CLAUSE_CHAIN (c) = list;
32387 return c;
32390 /* OpenMP 4.0:
32391 aligned ( variable-list )
32392 aligned ( variable-list : constant-expression ) */
32394 static tree
32395 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32397 tree nlist, c, alignment = NULL_TREE;
32398 bool colon;
32400 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32401 return list;
32403 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32404 &colon);
32406 if (colon)
32408 alignment = cp_parser_constant_expression (parser);
32410 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32411 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32412 /*or_comma=*/false,
32413 /*consume_paren=*/true);
32415 if (alignment == error_mark_node)
32416 alignment = NULL_TREE;
32419 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32420 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32422 return nlist;
32425 /* OpenMP 4.0:
32426 linear ( variable-list )
32427 linear ( variable-list : expression )
32429 OpenMP 4.5:
32430 linear ( modifier ( variable-list ) )
32431 linear ( modifier ( variable-list ) : expression ) */
32433 static tree
32434 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32435 bool is_cilk_simd_fn, bool declare_simd)
32437 tree nlist, c, step = integer_one_node;
32438 bool colon;
32439 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32441 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32442 return list;
32444 if (!is_cilk_simd_fn
32445 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32447 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32448 const char *p = IDENTIFIER_POINTER (id);
32450 if (strcmp ("ref", p) == 0)
32451 kind = OMP_CLAUSE_LINEAR_REF;
32452 else if (strcmp ("val", p) == 0)
32453 kind = OMP_CLAUSE_LINEAR_VAL;
32454 else if (strcmp ("uval", p) == 0)
32455 kind = OMP_CLAUSE_LINEAR_UVAL;
32456 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32457 cp_lexer_consume_token (parser->lexer);
32458 else
32459 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32462 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32463 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32464 &colon);
32465 else
32467 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32468 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32469 if (colon)
32470 cp_parser_require (parser, CPP_COLON, RT_COLON);
32471 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32472 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32473 /*or_comma=*/false,
32474 /*consume_paren=*/true);
32477 if (colon)
32479 step = NULL_TREE;
32480 if (declare_simd
32481 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32482 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32484 cp_token *token = cp_lexer_peek_token (parser->lexer);
32485 cp_parser_parse_tentatively (parser);
32486 step = cp_parser_id_expression (parser, /*template_p=*/false,
32487 /*check_dependency_p=*/true,
32488 /*template_p=*/NULL,
32489 /*declarator_p=*/false,
32490 /*optional_p=*/false);
32491 if (step != error_mark_node)
32492 step = cp_parser_lookup_name_simple (parser, step, token->location);
32493 if (step == error_mark_node)
32495 step = NULL_TREE;
32496 cp_parser_abort_tentative_parse (parser);
32498 else if (!cp_parser_parse_definitely (parser))
32499 step = NULL_TREE;
32501 if (!step)
32502 step = cp_parser_expression (parser);
32504 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
32506 sorry ("using parameters for %<linear%> step is not supported yet");
32507 step = integer_one_node;
32509 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32510 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32511 /*or_comma=*/false,
32512 /*consume_paren=*/true);
32514 if (step == error_mark_node)
32515 return list;
32518 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32520 OMP_CLAUSE_LINEAR_STEP (c) = step;
32521 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32524 return nlist;
32527 /* OpenMP 4.0:
32528 safelen ( constant-expression ) */
32530 static tree
32531 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32532 location_t location)
32534 tree t, c;
32536 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32537 return list;
32539 t = cp_parser_constant_expression (parser);
32541 if (t == error_mark_node
32542 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32543 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32544 /*or_comma=*/false,
32545 /*consume_paren=*/true);
32547 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32549 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32550 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32551 OMP_CLAUSE_CHAIN (c) = list;
32553 return c;
32556 /* OpenMP 4.0:
32557 simdlen ( constant-expression ) */
32559 static tree
32560 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32561 location_t location)
32563 tree t, c;
32565 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32566 return list;
32568 t = cp_parser_constant_expression (parser);
32570 if (t == error_mark_node
32571 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32572 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32573 /*or_comma=*/false,
32574 /*consume_paren=*/true);
32576 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32578 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32579 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32580 OMP_CLAUSE_CHAIN (c) = list;
32582 return c;
32585 /* OpenMP 4.5:
32586 vec:
32587 identifier [+/- integer]
32588 vec , identifier [+/- integer]
32591 static tree
32592 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32593 tree list)
32595 tree vec = NULL;
32597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32599 cp_parser_error (parser, "expected identifier");
32600 return list;
32603 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32605 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32606 tree t, identifier = cp_parser_identifier (parser);
32607 tree addend = NULL;
32609 if (identifier == error_mark_node)
32610 t = error_mark_node;
32611 else
32613 t = cp_parser_lookup_name_simple
32614 (parser, identifier,
32615 cp_lexer_peek_token (parser->lexer)->location);
32616 if (t == error_mark_node)
32617 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32618 id_loc);
32621 bool neg = false;
32622 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32623 neg = true;
32624 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32626 addend = integer_zero_node;
32627 goto add_to_vector;
32629 cp_lexer_consume_token (parser->lexer);
32631 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32633 cp_parser_error (parser, "expected integer");
32634 return list;
32637 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32638 if (TREE_CODE (addend) != INTEGER_CST)
32640 cp_parser_error (parser, "expected integer");
32641 return list;
32643 cp_lexer_consume_token (parser->lexer);
32645 add_to_vector:
32646 if (t != error_mark_node)
32648 vec = tree_cons (addend, t, vec);
32649 if (neg)
32650 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32653 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32654 break;
32656 cp_lexer_consume_token (parser->lexer);
32659 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32661 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32662 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32663 OMP_CLAUSE_DECL (u) = nreverse (vec);
32664 OMP_CLAUSE_CHAIN (u) = list;
32665 return u;
32667 return list;
32670 /* OpenMP 4.0:
32671 depend ( depend-kind : variable-list )
32673 depend-kind:
32674 in | out | inout
32676 OpenMP 4.5:
32677 depend ( source )
32679 depend ( sink : vec ) */
32681 static tree
32682 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
32684 tree nlist, c;
32685 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
32687 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32688 return list;
32690 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32692 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32693 const char *p = IDENTIFIER_POINTER (id);
32695 if (strcmp ("in", p) == 0)
32696 kind = OMP_CLAUSE_DEPEND_IN;
32697 else if (strcmp ("inout", p) == 0)
32698 kind = OMP_CLAUSE_DEPEND_INOUT;
32699 else if (strcmp ("out", p) == 0)
32700 kind = OMP_CLAUSE_DEPEND_OUT;
32701 else if (strcmp ("source", p) == 0)
32702 kind = OMP_CLAUSE_DEPEND_SOURCE;
32703 else if (strcmp ("sink", p) == 0)
32704 kind = OMP_CLAUSE_DEPEND_SINK;
32705 else
32706 goto invalid_kind;
32708 else
32709 goto invalid_kind;
32711 cp_lexer_consume_token (parser->lexer);
32713 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
32715 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
32716 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32717 OMP_CLAUSE_DECL (c) = NULL_TREE;
32718 OMP_CLAUSE_CHAIN (c) = list;
32719 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32720 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32721 /*or_comma=*/false,
32722 /*consume_paren=*/true);
32723 return c;
32726 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32727 goto resync_fail;
32729 if (kind == OMP_CLAUSE_DEPEND_SINK)
32730 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
32731 else
32733 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
32734 list, NULL);
32736 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32737 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32739 return nlist;
32741 invalid_kind:
32742 cp_parser_error (parser, "invalid depend kind");
32743 resync_fail:
32744 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32745 /*or_comma=*/false,
32746 /*consume_paren=*/true);
32747 return list;
32750 /* OpenMP 4.0:
32751 map ( map-kind : variable-list )
32752 map ( variable-list )
32754 map-kind:
32755 alloc | to | from | tofrom
32757 OpenMP 4.5:
32758 map-kind:
32759 alloc | to | from | tofrom | release | delete
32761 map ( always [,] map-kind: variable-list ) */
32763 static tree
32764 cp_parser_omp_clause_map (cp_parser *parser, tree list)
32766 tree nlist, c;
32767 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
32768 bool always = false;
32770 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32771 return list;
32773 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32775 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32776 const char *p = IDENTIFIER_POINTER (id);
32778 if (strcmp ("always", p) == 0)
32780 int nth = 2;
32781 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
32782 nth++;
32783 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
32784 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
32785 == RID_DELETE))
32786 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
32787 == CPP_COLON))
32789 always = true;
32790 cp_lexer_consume_token (parser->lexer);
32791 if (nth == 3)
32792 cp_lexer_consume_token (parser->lexer);
32797 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32798 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32800 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32801 const char *p = IDENTIFIER_POINTER (id);
32803 if (strcmp ("alloc", p) == 0)
32804 kind = GOMP_MAP_ALLOC;
32805 else if (strcmp ("to", p) == 0)
32806 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
32807 else if (strcmp ("from", p) == 0)
32808 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
32809 else if (strcmp ("tofrom", p) == 0)
32810 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
32811 else if (strcmp ("release", p) == 0)
32812 kind = GOMP_MAP_RELEASE;
32813 else
32815 cp_parser_error (parser, "invalid map kind");
32816 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32817 /*or_comma=*/false,
32818 /*consume_paren=*/true);
32819 return list;
32821 cp_lexer_consume_token (parser->lexer);
32822 cp_lexer_consume_token (parser->lexer);
32824 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
32825 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32827 kind = GOMP_MAP_DELETE;
32828 cp_lexer_consume_token (parser->lexer);
32829 cp_lexer_consume_token (parser->lexer);
32832 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
32833 NULL);
32835 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32836 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32838 return nlist;
32841 /* OpenMP 4.0:
32842 device ( expression ) */
32844 static tree
32845 cp_parser_omp_clause_device (cp_parser *parser, tree list,
32846 location_t location)
32848 tree t, c;
32850 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32851 return list;
32853 t = cp_parser_expression (parser);
32855 if (t == error_mark_node
32856 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32857 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32858 /*or_comma=*/false,
32859 /*consume_paren=*/true);
32861 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
32862 "device", location);
32864 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
32865 OMP_CLAUSE_DEVICE_ID (c) = t;
32866 OMP_CLAUSE_CHAIN (c) = list;
32868 return c;
32871 /* OpenMP 4.0:
32872 dist_schedule ( static )
32873 dist_schedule ( static , expression ) */
32875 static tree
32876 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
32877 location_t location)
32879 tree c, t;
32881 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32882 return list;
32884 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32886 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32887 goto invalid_kind;
32888 cp_lexer_consume_token (parser->lexer);
32890 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32892 cp_lexer_consume_token (parser->lexer);
32894 t = cp_parser_assignment_expression (parser);
32896 if (t == error_mark_node)
32897 goto resync_fail;
32898 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32900 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32901 goto resync_fail;
32903 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32904 goto resync_fail;
32906 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32907 location);
32908 OMP_CLAUSE_CHAIN (c) = list;
32909 return c;
32911 invalid_kind:
32912 cp_parser_error (parser, "invalid dist_schedule kind");
32913 resync_fail:
32914 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32915 /*or_comma=*/false,
32916 /*consume_paren=*/true);
32917 return list;
32920 /* OpenMP 4.0:
32921 proc_bind ( proc-bind-kind )
32923 proc-bind-kind:
32924 master | close | spread */
32926 static tree
32927 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32928 location_t location)
32930 tree c;
32931 enum omp_clause_proc_bind_kind kind;
32933 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32934 return list;
32936 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32938 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32939 const char *p = IDENTIFIER_POINTER (id);
32941 if (strcmp ("master", p) == 0)
32942 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32943 else if (strcmp ("close", p) == 0)
32944 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32945 else if (strcmp ("spread", p) == 0)
32946 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32947 else
32948 goto invalid_kind;
32950 else
32951 goto invalid_kind;
32953 cp_lexer_consume_token (parser->lexer);
32954 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32955 goto resync_fail;
32957 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32958 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32959 location);
32960 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32961 OMP_CLAUSE_CHAIN (c) = list;
32962 return c;
32964 invalid_kind:
32965 cp_parser_error (parser, "invalid depend kind");
32966 resync_fail:
32967 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32968 /*or_comma=*/false,
32969 /*consume_paren=*/true);
32970 return list;
32973 /* OpenACC:
32974 async [( int-expr )] */
32976 static tree
32977 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32979 tree c, t;
32980 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32982 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32984 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32986 cp_lexer_consume_token (parser->lexer);
32988 t = cp_parser_expression (parser);
32989 if (t == error_mark_node
32990 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32991 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32992 /*or_comma=*/false,
32993 /*consume_paren=*/true);
32996 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32998 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32999 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33000 OMP_CLAUSE_CHAIN (c) = list;
33001 list = c;
33003 return list;
33006 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33007 is a bitmask in MASK. Return the list of clauses found. */
33009 static tree
33010 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33011 const char *where, cp_token *pragma_tok,
33012 bool finish_p = true)
33014 tree clauses = NULL;
33015 bool first = true;
33017 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33019 location_t here;
33020 pragma_omp_clause c_kind;
33021 omp_clause_code code;
33022 const char *c_name;
33023 tree prev = clauses;
33025 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33026 cp_lexer_consume_token (parser->lexer);
33028 here = cp_lexer_peek_token (parser->lexer)->location;
33029 c_kind = cp_parser_omp_clause_name (parser);
33031 switch (c_kind)
33033 case PRAGMA_OACC_CLAUSE_ASYNC:
33034 clauses = cp_parser_oacc_clause_async (parser, clauses);
33035 c_name = "async";
33036 break;
33037 case PRAGMA_OACC_CLAUSE_AUTO:
33038 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33039 clauses, here);
33040 c_name = "auto";
33041 break;
33042 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33043 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33044 c_name = "collapse";
33045 break;
33046 case PRAGMA_OACC_CLAUSE_COPY:
33047 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33048 c_name = "copy";
33049 break;
33050 case PRAGMA_OACC_CLAUSE_COPYIN:
33051 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33052 c_name = "copyin";
33053 break;
33054 case PRAGMA_OACC_CLAUSE_COPYOUT:
33055 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33056 c_name = "copyout";
33057 break;
33058 case PRAGMA_OACC_CLAUSE_CREATE:
33059 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33060 c_name = "create";
33061 break;
33062 case PRAGMA_OACC_CLAUSE_DELETE:
33063 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33064 c_name = "delete";
33065 break;
33066 case PRAGMA_OMP_CLAUSE_DEFAULT:
33067 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33068 c_name = "default";
33069 break;
33070 case PRAGMA_OACC_CLAUSE_DEVICE:
33071 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33072 c_name = "device";
33073 break;
33074 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33075 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33076 c_name = "deviceptr";
33077 break;
33078 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33079 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33080 c_name = "device_resident";
33081 break;
33082 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33083 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33084 clauses);
33085 c_name = "firstprivate";
33086 break;
33087 case PRAGMA_OACC_CLAUSE_GANG:
33088 c_name = "gang";
33089 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33090 c_name, clauses);
33091 break;
33092 case PRAGMA_OACC_CLAUSE_HOST:
33093 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33094 c_name = "host";
33095 break;
33096 case PRAGMA_OACC_CLAUSE_IF:
33097 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33098 c_name = "if";
33099 break;
33100 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33101 clauses = cp_parser_oacc_simple_clause (parser,
33102 OMP_CLAUSE_INDEPENDENT,
33103 clauses, here);
33104 c_name = "independent";
33105 break;
33106 case PRAGMA_OACC_CLAUSE_LINK:
33107 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33108 c_name = "link";
33109 break;
33110 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33111 code = OMP_CLAUSE_NUM_GANGS;
33112 c_name = "num_gangs";
33113 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33114 clauses);
33115 break;
33116 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33117 c_name = "num_workers";
33118 code = OMP_CLAUSE_NUM_WORKERS;
33119 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33120 clauses);
33121 break;
33122 case PRAGMA_OACC_CLAUSE_PRESENT:
33123 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33124 c_name = "present";
33125 break;
33126 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33127 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33128 c_name = "present_or_copy";
33129 break;
33130 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33131 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33132 c_name = "present_or_copyin";
33133 break;
33134 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33135 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33136 c_name = "present_or_copyout";
33137 break;
33138 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33139 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33140 c_name = "present_or_create";
33141 break;
33142 case PRAGMA_OACC_CLAUSE_PRIVATE:
33143 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33144 clauses);
33145 c_name = "private";
33146 break;
33147 case PRAGMA_OACC_CLAUSE_REDUCTION:
33148 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33149 c_name = "reduction";
33150 break;
33151 case PRAGMA_OACC_CLAUSE_SELF:
33152 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33153 c_name = "self";
33154 break;
33155 case PRAGMA_OACC_CLAUSE_SEQ:
33156 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33157 clauses, here);
33158 c_name = "seq";
33159 break;
33160 case PRAGMA_OACC_CLAUSE_TILE:
33161 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33162 c_name = "tile";
33163 break;
33164 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33165 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33166 clauses);
33167 c_name = "use_device";
33168 break;
33169 case PRAGMA_OACC_CLAUSE_VECTOR:
33170 c_name = "vector";
33171 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33172 c_name, clauses);
33173 break;
33174 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33175 c_name = "vector_length";
33176 code = OMP_CLAUSE_VECTOR_LENGTH;
33177 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33178 clauses);
33179 break;
33180 case PRAGMA_OACC_CLAUSE_WAIT:
33181 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33182 c_name = "wait";
33183 break;
33184 case PRAGMA_OACC_CLAUSE_WORKER:
33185 c_name = "worker";
33186 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33187 c_name, clauses);
33188 break;
33189 default:
33190 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33191 goto saw_error;
33194 first = false;
33196 if (((mask >> c_kind) & 1) == 0)
33198 /* Remove the invalid clause(s) from the list to avoid
33199 confusing the rest of the compiler. */
33200 clauses = prev;
33201 error_at (here, "%qs is not valid for %qs", c_name, where);
33205 saw_error:
33206 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33208 if (finish_p)
33209 return finish_omp_clauses (clauses, C_ORT_ACC);
33211 return clauses;
33214 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33215 is a bitmask in MASK. Return the list of clauses found; the result
33216 of clause default goes in *pdefault. */
33218 static tree
33219 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33220 const char *where, cp_token *pragma_tok,
33221 bool finish_p = true)
33223 tree clauses = NULL;
33224 bool first = true;
33225 cp_token *token = NULL;
33227 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33229 pragma_omp_clause c_kind;
33230 const char *c_name;
33231 tree prev = clauses;
33233 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33234 cp_lexer_consume_token (parser->lexer);
33236 token = cp_lexer_peek_token (parser->lexer);
33237 c_kind = cp_parser_omp_clause_name (parser);
33239 switch (c_kind)
33241 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33242 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33243 token->location);
33244 c_name = "collapse";
33245 break;
33246 case PRAGMA_OMP_CLAUSE_COPYIN:
33247 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33248 c_name = "copyin";
33249 break;
33250 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33251 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33252 clauses);
33253 c_name = "copyprivate";
33254 break;
33255 case PRAGMA_OMP_CLAUSE_DEFAULT:
33256 clauses = cp_parser_omp_clause_default (parser, clauses,
33257 token->location, false);
33258 c_name = "default";
33259 break;
33260 case PRAGMA_OMP_CLAUSE_FINAL:
33261 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33262 c_name = "final";
33263 break;
33264 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33265 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33266 clauses);
33267 c_name = "firstprivate";
33268 break;
33269 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33270 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33271 token->location);
33272 c_name = "grainsize";
33273 break;
33274 case PRAGMA_OMP_CLAUSE_HINT:
33275 clauses = cp_parser_omp_clause_hint (parser, clauses,
33276 token->location);
33277 c_name = "hint";
33278 break;
33279 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33280 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33281 token->location);
33282 c_name = "defaultmap";
33283 break;
33284 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33285 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33286 clauses);
33287 c_name = "use_device_ptr";
33288 break;
33289 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33290 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33291 clauses);
33292 c_name = "is_device_ptr";
33293 break;
33294 case PRAGMA_OMP_CLAUSE_IF:
33295 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33296 true);
33297 c_name = "if";
33298 break;
33299 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33300 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33301 clauses);
33302 c_name = "lastprivate";
33303 break;
33304 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33305 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33306 token->location);
33307 c_name = "mergeable";
33308 break;
33309 case PRAGMA_OMP_CLAUSE_NOWAIT:
33310 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33311 c_name = "nowait";
33312 break;
33313 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33314 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33315 token->location);
33316 c_name = "num_tasks";
33317 break;
33318 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33319 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33320 token->location);
33321 c_name = "num_threads";
33322 break;
33323 case PRAGMA_OMP_CLAUSE_ORDERED:
33324 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33325 token->location);
33326 c_name = "ordered";
33327 break;
33328 case PRAGMA_OMP_CLAUSE_PRIORITY:
33329 clauses = cp_parser_omp_clause_priority (parser, clauses,
33330 token->location);
33331 c_name = "priority";
33332 break;
33333 case PRAGMA_OMP_CLAUSE_PRIVATE:
33334 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33335 clauses);
33336 c_name = "private";
33337 break;
33338 case PRAGMA_OMP_CLAUSE_REDUCTION:
33339 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33340 c_name = "reduction";
33341 break;
33342 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33343 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33344 token->location);
33345 c_name = "schedule";
33346 break;
33347 case PRAGMA_OMP_CLAUSE_SHARED:
33348 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33349 clauses);
33350 c_name = "shared";
33351 break;
33352 case PRAGMA_OMP_CLAUSE_UNTIED:
33353 clauses = cp_parser_omp_clause_untied (parser, clauses,
33354 token->location);
33355 c_name = "untied";
33356 break;
33357 case PRAGMA_OMP_CLAUSE_INBRANCH:
33358 case PRAGMA_CILK_CLAUSE_MASK:
33359 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33360 clauses, token->location);
33361 c_name = "inbranch";
33362 break;
33363 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33364 case PRAGMA_CILK_CLAUSE_NOMASK:
33365 clauses = cp_parser_omp_clause_branch (parser,
33366 OMP_CLAUSE_NOTINBRANCH,
33367 clauses, token->location);
33368 c_name = "notinbranch";
33369 break;
33370 case PRAGMA_OMP_CLAUSE_PARALLEL:
33371 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33372 clauses, token->location);
33373 c_name = "parallel";
33374 if (!first)
33376 clause_not_first:
33377 error_at (token->location, "%qs must be the first clause of %qs",
33378 c_name, where);
33379 clauses = prev;
33381 break;
33382 case PRAGMA_OMP_CLAUSE_FOR:
33383 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33384 clauses, token->location);
33385 c_name = "for";
33386 if (!first)
33387 goto clause_not_first;
33388 break;
33389 case PRAGMA_OMP_CLAUSE_SECTIONS:
33390 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33391 clauses, token->location);
33392 c_name = "sections";
33393 if (!first)
33394 goto clause_not_first;
33395 break;
33396 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33397 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33398 clauses, token->location);
33399 c_name = "taskgroup";
33400 if (!first)
33401 goto clause_not_first;
33402 break;
33403 case PRAGMA_OMP_CLAUSE_LINK:
33404 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33405 c_name = "to";
33406 break;
33407 case PRAGMA_OMP_CLAUSE_TO:
33408 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33409 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33410 clauses);
33411 else
33412 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33413 c_name = "to";
33414 break;
33415 case PRAGMA_OMP_CLAUSE_FROM:
33416 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33417 c_name = "from";
33418 break;
33419 case PRAGMA_OMP_CLAUSE_UNIFORM:
33420 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33421 clauses);
33422 c_name = "uniform";
33423 break;
33424 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33425 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33426 token->location);
33427 c_name = "num_teams";
33428 break;
33429 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33430 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33431 token->location);
33432 c_name = "thread_limit";
33433 break;
33434 case PRAGMA_OMP_CLAUSE_ALIGNED:
33435 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33436 c_name = "aligned";
33437 break;
33438 case PRAGMA_OMP_CLAUSE_LINEAR:
33440 bool cilk_simd_fn = false, declare_simd = false;
33441 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33442 cilk_simd_fn = true;
33443 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33444 declare_simd = true;
33445 clauses = cp_parser_omp_clause_linear (parser, clauses,
33446 cilk_simd_fn, declare_simd);
33448 c_name = "linear";
33449 break;
33450 case PRAGMA_OMP_CLAUSE_DEPEND:
33451 clauses = cp_parser_omp_clause_depend (parser, clauses,
33452 token->location);
33453 c_name = "depend";
33454 break;
33455 case PRAGMA_OMP_CLAUSE_MAP:
33456 clauses = cp_parser_omp_clause_map (parser, clauses);
33457 c_name = "map";
33458 break;
33459 case PRAGMA_OMP_CLAUSE_DEVICE:
33460 clauses = cp_parser_omp_clause_device (parser, clauses,
33461 token->location);
33462 c_name = "device";
33463 break;
33464 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33465 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33466 token->location);
33467 c_name = "dist_schedule";
33468 break;
33469 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33470 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33471 token->location);
33472 c_name = "proc_bind";
33473 break;
33474 case PRAGMA_OMP_CLAUSE_SAFELEN:
33475 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33476 token->location);
33477 c_name = "safelen";
33478 break;
33479 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33480 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33481 token->location);
33482 c_name = "simdlen";
33483 break;
33484 case PRAGMA_OMP_CLAUSE_NOGROUP:
33485 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33486 token->location);
33487 c_name = "nogroup";
33488 break;
33489 case PRAGMA_OMP_CLAUSE_THREADS:
33490 clauses
33491 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33492 clauses, token->location);
33493 c_name = "threads";
33494 break;
33495 case PRAGMA_OMP_CLAUSE_SIMD:
33496 clauses
33497 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33498 clauses, token->location);
33499 c_name = "simd";
33500 break;
33501 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
33502 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
33503 c_name = "simdlen";
33504 break;
33505 default:
33506 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33507 goto saw_error;
33510 first = false;
33512 if (((mask >> c_kind) & 1) == 0)
33514 /* Remove the invalid clause(s) from the list to avoid
33515 confusing the rest of the compiler. */
33516 clauses = prev;
33517 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33520 saw_error:
33521 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33522 no reason to skip to the end. */
33523 if (!(flag_cilkplus && pragma_tok == NULL))
33524 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33525 if (finish_p)
33527 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33528 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33529 else
33530 return finish_omp_clauses (clauses, C_ORT_OMP);
33532 return clauses;
33535 /* OpenMP 2.5:
33536 structured-block:
33537 statement
33539 In practice, we're also interested in adding the statement to an
33540 outer node. So it is convenient if we work around the fact that
33541 cp_parser_statement calls add_stmt. */
33543 static unsigned
33544 cp_parser_begin_omp_structured_block (cp_parser *parser)
33546 unsigned save = parser->in_statement;
33548 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33549 This preserves the "not within loop or switch" style error messages
33550 for nonsense cases like
33551 void foo() {
33552 #pragma omp single
33553 break;
33556 if (parser->in_statement)
33557 parser->in_statement = IN_OMP_BLOCK;
33559 return save;
33562 static void
33563 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33565 parser->in_statement = save;
33568 static tree
33569 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33571 tree stmt = begin_omp_structured_block ();
33572 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33574 cp_parser_statement (parser, NULL_TREE, false, if_p);
33576 cp_parser_end_omp_structured_block (parser, save);
33577 return finish_omp_structured_block (stmt);
33580 /* OpenMP 2.5:
33581 # pragma omp atomic new-line
33582 expression-stmt
33584 expression-stmt:
33585 x binop= expr | x++ | ++x | x-- | --x
33586 binop:
33587 +, *, -, /, &, ^, |, <<, >>
33589 where x is an lvalue expression with scalar type.
33591 OpenMP 3.1:
33592 # pragma omp atomic new-line
33593 update-stmt
33595 # pragma omp atomic read new-line
33596 read-stmt
33598 # pragma omp atomic write new-line
33599 write-stmt
33601 # pragma omp atomic update new-line
33602 update-stmt
33604 # pragma omp atomic capture new-line
33605 capture-stmt
33607 # pragma omp atomic capture new-line
33608 capture-block
33610 read-stmt:
33611 v = x
33612 write-stmt:
33613 x = expr
33614 update-stmt:
33615 expression-stmt | x = x binop expr
33616 capture-stmt:
33617 v = expression-stmt
33618 capture-block:
33619 { v = x; update-stmt; } | { update-stmt; v = x; }
33621 OpenMP 4.0:
33622 update-stmt:
33623 expression-stmt | x = x binop expr | x = expr binop x
33624 capture-stmt:
33625 v = update-stmt
33626 capture-block:
33627 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33629 where x and v are lvalue expressions with scalar type. */
33631 static void
33632 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33634 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33635 tree rhs1 = NULL_TREE, orig_lhs;
33636 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33637 bool structured_block = false;
33638 bool seq_cst = false;
33640 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33642 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33643 const char *p = IDENTIFIER_POINTER (id);
33645 if (!strcmp (p, "seq_cst"))
33647 seq_cst = true;
33648 cp_lexer_consume_token (parser->lexer);
33649 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33650 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33651 cp_lexer_consume_token (parser->lexer);
33654 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33656 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33657 const char *p = IDENTIFIER_POINTER (id);
33659 if (!strcmp (p, "read"))
33660 code = OMP_ATOMIC_READ;
33661 else if (!strcmp (p, "write"))
33662 code = NOP_EXPR;
33663 else if (!strcmp (p, "update"))
33664 code = OMP_ATOMIC;
33665 else if (!strcmp (p, "capture"))
33666 code = OMP_ATOMIC_CAPTURE_NEW;
33667 else
33668 p = NULL;
33669 if (p)
33670 cp_lexer_consume_token (parser->lexer);
33672 if (!seq_cst)
33674 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33675 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33676 cp_lexer_consume_token (parser->lexer);
33678 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33680 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33681 const char *p = IDENTIFIER_POINTER (id);
33683 if (!strcmp (p, "seq_cst"))
33685 seq_cst = true;
33686 cp_lexer_consume_token (parser->lexer);
33690 cp_parser_require_pragma_eol (parser, pragma_tok);
33692 switch (code)
33694 case OMP_ATOMIC_READ:
33695 case NOP_EXPR: /* atomic write */
33696 v = cp_parser_unary_expression (parser);
33697 if (v == error_mark_node)
33698 goto saw_error;
33699 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33700 goto saw_error;
33701 if (code == NOP_EXPR)
33702 lhs = cp_parser_expression (parser);
33703 else
33704 lhs = cp_parser_unary_expression (parser);
33705 if (lhs == error_mark_node)
33706 goto saw_error;
33707 if (code == NOP_EXPR)
33709 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33710 opcode. */
33711 code = OMP_ATOMIC;
33712 rhs = lhs;
33713 lhs = v;
33714 v = NULL_TREE;
33716 goto done;
33717 case OMP_ATOMIC_CAPTURE_NEW:
33718 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33720 cp_lexer_consume_token (parser->lexer);
33721 structured_block = true;
33723 else
33725 v = cp_parser_unary_expression (parser);
33726 if (v == error_mark_node)
33727 goto saw_error;
33728 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33729 goto saw_error;
33731 default:
33732 break;
33735 restart:
33736 lhs = cp_parser_unary_expression (parser);
33737 orig_lhs = lhs;
33738 switch (TREE_CODE (lhs))
33740 case ERROR_MARK:
33741 goto saw_error;
33743 case POSTINCREMENT_EXPR:
33744 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33745 code = OMP_ATOMIC_CAPTURE_OLD;
33746 /* FALLTHROUGH */
33747 case PREINCREMENT_EXPR:
33748 lhs = TREE_OPERAND (lhs, 0);
33749 opcode = PLUS_EXPR;
33750 rhs = integer_one_node;
33751 break;
33753 case POSTDECREMENT_EXPR:
33754 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33755 code = OMP_ATOMIC_CAPTURE_OLD;
33756 /* FALLTHROUGH */
33757 case PREDECREMENT_EXPR:
33758 lhs = TREE_OPERAND (lhs, 0);
33759 opcode = MINUS_EXPR;
33760 rhs = integer_one_node;
33761 break;
33763 case COMPOUND_EXPR:
33764 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
33765 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
33766 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
33767 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
33768 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33769 (TREE_OPERAND (lhs, 1), 0), 0)))
33770 == BOOLEAN_TYPE)
33771 /* Undo effects of boolean_increment for post {in,de}crement. */
33772 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
33773 /* FALLTHRU */
33774 case MODIFY_EXPR:
33775 if (TREE_CODE (lhs) == MODIFY_EXPR
33776 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
33778 /* Undo effects of boolean_increment. */
33779 if (integer_onep (TREE_OPERAND (lhs, 1)))
33781 /* This is pre or post increment. */
33782 rhs = TREE_OPERAND (lhs, 1);
33783 lhs = TREE_OPERAND (lhs, 0);
33784 opcode = NOP_EXPR;
33785 if (code == OMP_ATOMIC_CAPTURE_NEW
33786 && !structured_block
33787 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
33788 code = OMP_ATOMIC_CAPTURE_OLD;
33789 break;
33792 /* FALLTHRU */
33793 default:
33794 switch (cp_lexer_peek_token (parser->lexer)->type)
33796 case CPP_MULT_EQ:
33797 opcode = MULT_EXPR;
33798 break;
33799 case CPP_DIV_EQ:
33800 opcode = TRUNC_DIV_EXPR;
33801 break;
33802 case CPP_PLUS_EQ:
33803 opcode = PLUS_EXPR;
33804 break;
33805 case CPP_MINUS_EQ:
33806 opcode = MINUS_EXPR;
33807 break;
33808 case CPP_LSHIFT_EQ:
33809 opcode = LSHIFT_EXPR;
33810 break;
33811 case CPP_RSHIFT_EQ:
33812 opcode = RSHIFT_EXPR;
33813 break;
33814 case CPP_AND_EQ:
33815 opcode = BIT_AND_EXPR;
33816 break;
33817 case CPP_OR_EQ:
33818 opcode = BIT_IOR_EXPR;
33819 break;
33820 case CPP_XOR_EQ:
33821 opcode = BIT_XOR_EXPR;
33822 break;
33823 case CPP_EQ:
33824 enum cp_parser_prec oprec;
33825 cp_token *token;
33826 cp_lexer_consume_token (parser->lexer);
33827 cp_parser_parse_tentatively (parser);
33828 rhs1 = cp_parser_simple_cast_expression (parser);
33829 if (rhs1 == error_mark_node)
33831 cp_parser_abort_tentative_parse (parser);
33832 cp_parser_simple_cast_expression (parser);
33833 goto saw_error;
33835 token = cp_lexer_peek_token (parser->lexer);
33836 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
33838 cp_parser_abort_tentative_parse (parser);
33839 cp_parser_parse_tentatively (parser);
33840 rhs = cp_parser_binary_expression (parser, false, true,
33841 PREC_NOT_OPERATOR, NULL);
33842 if (rhs == error_mark_node)
33844 cp_parser_abort_tentative_parse (parser);
33845 cp_parser_binary_expression (parser, false, true,
33846 PREC_NOT_OPERATOR, NULL);
33847 goto saw_error;
33849 switch (TREE_CODE (rhs))
33851 case MULT_EXPR:
33852 case TRUNC_DIV_EXPR:
33853 case RDIV_EXPR:
33854 case PLUS_EXPR:
33855 case MINUS_EXPR:
33856 case LSHIFT_EXPR:
33857 case RSHIFT_EXPR:
33858 case BIT_AND_EXPR:
33859 case BIT_IOR_EXPR:
33860 case BIT_XOR_EXPR:
33861 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
33863 if (cp_parser_parse_definitely (parser))
33865 opcode = TREE_CODE (rhs);
33866 rhs1 = TREE_OPERAND (rhs, 0);
33867 rhs = TREE_OPERAND (rhs, 1);
33868 goto stmt_done;
33870 else
33871 goto saw_error;
33873 break;
33874 default:
33875 break;
33877 cp_parser_abort_tentative_parse (parser);
33878 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
33880 rhs = cp_parser_expression (parser);
33881 if (rhs == error_mark_node)
33882 goto saw_error;
33883 opcode = NOP_EXPR;
33884 rhs1 = NULL_TREE;
33885 goto stmt_done;
33887 cp_parser_error (parser,
33888 "invalid form of %<#pragma omp atomic%>");
33889 goto saw_error;
33891 if (!cp_parser_parse_definitely (parser))
33892 goto saw_error;
33893 switch (token->type)
33895 case CPP_SEMICOLON:
33896 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33898 code = OMP_ATOMIC_CAPTURE_OLD;
33899 v = lhs;
33900 lhs = NULL_TREE;
33901 lhs1 = rhs1;
33902 rhs1 = NULL_TREE;
33903 cp_lexer_consume_token (parser->lexer);
33904 goto restart;
33906 else if (structured_block)
33908 opcode = NOP_EXPR;
33909 rhs = rhs1;
33910 rhs1 = NULL_TREE;
33911 goto stmt_done;
33913 cp_parser_error (parser,
33914 "invalid form of %<#pragma omp atomic%>");
33915 goto saw_error;
33916 case CPP_MULT:
33917 opcode = MULT_EXPR;
33918 break;
33919 case CPP_DIV:
33920 opcode = TRUNC_DIV_EXPR;
33921 break;
33922 case CPP_PLUS:
33923 opcode = PLUS_EXPR;
33924 break;
33925 case CPP_MINUS:
33926 opcode = MINUS_EXPR;
33927 break;
33928 case CPP_LSHIFT:
33929 opcode = LSHIFT_EXPR;
33930 break;
33931 case CPP_RSHIFT:
33932 opcode = RSHIFT_EXPR;
33933 break;
33934 case CPP_AND:
33935 opcode = BIT_AND_EXPR;
33936 break;
33937 case CPP_OR:
33938 opcode = BIT_IOR_EXPR;
33939 break;
33940 case CPP_XOR:
33941 opcode = BIT_XOR_EXPR;
33942 break;
33943 default:
33944 cp_parser_error (parser,
33945 "invalid operator for %<#pragma omp atomic%>");
33946 goto saw_error;
33948 oprec = TOKEN_PRECEDENCE (token);
33949 gcc_assert (oprec != PREC_NOT_OPERATOR);
33950 if (commutative_tree_code (opcode))
33951 oprec = (enum cp_parser_prec) (oprec - 1);
33952 cp_lexer_consume_token (parser->lexer);
33953 rhs = cp_parser_binary_expression (parser, false, false,
33954 oprec, NULL);
33955 if (rhs == error_mark_node)
33956 goto saw_error;
33957 goto stmt_done;
33958 /* FALLTHROUGH */
33959 default:
33960 cp_parser_error (parser,
33961 "invalid operator for %<#pragma omp atomic%>");
33962 goto saw_error;
33964 cp_lexer_consume_token (parser->lexer);
33966 rhs = cp_parser_expression (parser);
33967 if (rhs == error_mark_node)
33968 goto saw_error;
33969 break;
33971 stmt_done:
33972 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33974 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33975 goto saw_error;
33976 v = cp_parser_unary_expression (parser);
33977 if (v == error_mark_node)
33978 goto saw_error;
33979 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33980 goto saw_error;
33981 lhs1 = cp_parser_unary_expression (parser);
33982 if (lhs1 == error_mark_node)
33983 goto saw_error;
33985 if (structured_block)
33987 cp_parser_consume_semicolon_at_end_of_statement (parser);
33988 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33990 done:
33991 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33992 if (!structured_block)
33993 cp_parser_consume_semicolon_at_end_of_statement (parser);
33994 return;
33996 saw_error:
33997 cp_parser_skip_to_end_of_block_or_statement (parser);
33998 if (structured_block)
34000 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34001 cp_lexer_consume_token (parser->lexer);
34002 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34004 cp_parser_skip_to_end_of_block_or_statement (parser);
34005 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34006 cp_lexer_consume_token (parser->lexer);
34012 /* OpenMP 2.5:
34013 # pragma omp barrier new-line */
34015 static void
34016 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34018 cp_parser_require_pragma_eol (parser, pragma_tok);
34019 finish_omp_barrier ();
34022 /* OpenMP 2.5:
34023 # pragma omp critical [(name)] new-line
34024 structured-block
34026 OpenMP 4.5:
34027 # pragma omp critical [(name) [hint(expression)]] new-line
34028 structured-block */
34030 #define OMP_CRITICAL_CLAUSE_MASK \
34031 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34033 static tree
34034 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34036 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34038 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34040 cp_lexer_consume_token (parser->lexer);
34042 name = cp_parser_identifier (parser);
34044 if (name == error_mark_node
34045 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34046 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34047 /*or_comma=*/false,
34048 /*consume_paren=*/true);
34049 if (name == error_mark_node)
34050 name = NULL;
34052 clauses = cp_parser_omp_all_clauses (parser,
34053 OMP_CRITICAL_CLAUSE_MASK,
34054 "#pragma omp critical", pragma_tok);
34056 else
34057 cp_parser_require_pragma_eol (parser, pragma_tok);
34059 stmt = cp_parser_omp_structured_block (parser, if_p);
34060 return c_finish_omp_critical (input_location, stmt, name, clauses);
34063 /* OpenMP 2.5:
34064 # pragma omp flush flush-vars[opt] new-line
34066 flush-vars:
34067 ( variable-list ) */
34069 static void
34070 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34072 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34073 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34074 cp_parser_require_pragma_eol (parser, pragma_tok);
34076 finish_omp_flush ();
34079 /* Helper function, to parse omp for increment expression. */
34081 static tree
34082 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34084 tree cond = cp_parser_binary_expression (parser, false, true,
34085 PREC_NOT_OPERATOR, NULL);
34086 if (cond == error_mark_node
34087 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34089 cp_parser_skip_to_end_of_statement (parser);
34090 return error_mark_node;
34093 switch (TREE_CODE (cond))
34095 case GT_EXPR:
34096 case GE_EXPR:
34097 case LT_EXPR:
34098 case LE_EXPR:
34099 break;
34100 case NE_EXPR:
34101 if (code == CILK_SIMD || code == CILK_FOR)
34102 break;
34103 /* Fall through: OpenMP disallows NE_EXPR. */
34104 gcc_fallthrough ();
34105 default:
34106 return error_mark_node;
34109 /* If decl is an iterator, preserve LHS and RHS of the relational
34110 expr until finish_omp_for. */
34111 if (decl
34112 && (type_dependent_expression_p (decl)
34113 || CLASS_TYPE_P (TREE_TYPE (decl))))
34114 return cond;
34116 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34117 TREE_CODE (cond),
34118 TREE_OPERAND (cond, 0), ERROR_MARK,
34119 TREE_OPERAND (cond, 1), ERROR_MARK,
34120 /*overload=*/NULL, tf_warning_or_error);
34123 /* Helper function, to parse omp for increment expression. */
34125 static tree
34126 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34128 cp_token *token = cp_lexer_peek_token (parser->lexer);
34129 enum tree_code op;
34130 tree lhs, rhs;
34131 cp_id_kind idk;
34132 bool decl_first;
34134 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34136 op = (token->type == CPP_PLUS_PLUS
34137 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34138 cp_lexer_consume_token (parser->lexer);
34139 lhs = cp_parser_simple_cast_expression (parser);
34140 if (lhs != decl
34141 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34142 return error_mark_node;
34143 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34146 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34147 if (lhs != decl
34148 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34149 return error_mark_node;
34151 token = cp_lexer_peek_token (parser->lexer);
34152 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34154 op = (token->type == CPP_PLUS_PLUS
34155 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34156 cp_lexer_consume_token (parser->lexer);
34157 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34160 op = cp_parser_assignment_operator_opt (parser);
34161 if (op == ERROR_MARK)
34162 return error_mark_node;
34164 if (op != NOP_EXPR)
34166 rhs = cp_parser_assignment_expression (parser);
34167 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34168 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34171 lhs = cp_parser_binary_expression (parser, false, false,
34172 PREC_ADDITIVE_EXPRESSION, NULL);
34173 token = cp_lexer_peek_token (parser->lexer);
34174 decl_first = (lhs == decl
34175 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34176 if (decl_first)
34177 lhs = NULL_TREE;
34178 if (token->type != CPP_PLUS
34179 && token->type != CPP_MINUS)
34180 return error_mark_node;
34184 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34185 cp_lexer_consume_token (parser->lexer);
34186 rhs = cp_parser_binary_expression (parser, false, false,
34187 PREC_ADDITIVE_EXPRESSION, NULL);
34188 token = cp_lexer_peek_token (parser->lexer);
34189 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34191 if (lhs == NULL_TREE)
34193 if (op == PLUS_EXPR)
34194 lhs = rhs;
34195 else
34196 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34197 tf_warning_or_error);
34199 else
34200 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34201 ERROR_MARK, NULL, tf_warning_or_error);
34204 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34206 if (!decl_first)
34208 if ((rhs != decl
34209 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34210 || op == MINUS_EXPR)
34211 return error_mark_node;
34212 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34214 else
34215 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34217 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34220 /* Parse the initialization statement of either an OpenMP for loop or
34221 a Cilk Plus for loop.
34223 Return true if the resulting construct should have an
34224 OMP_CLAUSE_PRIVATE added to it. */
34226 static tree
34227 cp_parser_omp_for_loop_init (cp_parser *parser,
34228 enum tree_code code,
34229 tree &this_pre_body,
34230 vec<tree, va_gc> *for_block,
34231 tree &init,
34232 tree &orig_init,
34233 tree &decl,
34234 tree &real_decl)
34236 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34237 return NULL_TREE;
34239 tree add_private_clause = NULL_TREE;
34241 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34243 init-expr:
34244 var = lb
34245 integer-type var = lb
34246 random-access-iterator-type var = lb
34247 pointer-type var = lb
34249 cp_decl_specifier_seq type_specifiers;
34251 /* First, try to parse as an initialized declaration. See
34252 cp_parser_condition, from whence the bulk of this is copied. */
34254 cp_parser_parse_tentatively (parser);
34255 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34256 /*is_trailing_return=*/false,
34257 &type_specifiers);
34258 if (cp_parser_parse_definitely (parser))
34260 /* If parsing a type specifier seq succeeded, then this
34261 MUST be a initialized declaration. */
34262 tree asm_specification, attributes;
34263 cp_declarator *declarator;
34265 declarator = cp_parser_declarator (parser,
34266 CP_PARSER_DECLARATOR_NAMED,
34267 /*ctor_dtor_or_conv_p=*/NULL,
34268 /*parenthesized_p=*/NULL,
34269 /*member_p=*/false,
34270 /*friend_p=*/false);
34271 attributes = cp_parser_attributes_opt (parser);
34272 asm_specification = cp_parser_asm_specification_opt (parser);
34274 if (declarator == cp_error_declarator)
34275 cp_parser_skip_to_end_of_statement (parser);
34277 else
34279 tree pushed_scope, auto_node;
34281 decl = start_decl (declarator, &type_specifiers,
34282 SD_INITIALIZED, attributes,
34283 /*prefix_attributes=*/NULL_TREE,
34284 &pushed_scope);
34286 auto_node = type_uses_auto (TREE_TYPE (decl));
34287 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34289 if (cp_lexer_next_token_is (parser->lexer,
34290 CPP_OPEN_PAREN))
34292 if (code != CILK_SIMD && code != CILK_FOR)
34293 error ("parenthesized initialization is not allowed in "
34294 "OpenMP %<for%> loop");
34295 else
34296 error ("parenthesized initialization is "
34297 "not allowed in for-loop");
34299 else
34300 /* Trigger an error. */
34301 cp_parser_require (parser, CPP_EQ, RT_EQ);
34303 init = error_mark_node;
34304 cp_parser_skip_to_end_of_statement (parser);
34306 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34307 || type_dependent_expression_p (decl)
34308 || auto_node)
34310 bool is_direct_init, is_non_constant_init;
34312 init = cp_parser_initializer (parser,
34313 &is_direct_init,
34314 &is_non_constant_init);
34316 if (auto_node)
34318 TREE_TYPE (decl)
34319 = do_auto_deduction (TREE_TYPE (decl), init,
34320 auto_node);
34322 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34323 && !type_dependent_expression_p (decl))
34324 goto non_class;
34327 cp_finish_decl (decl, init, !is_non_constant_init,
34328 asm_specification,
34329 LOOKUP_ONLYCONVERTING);
34330 orig_init = init;
34331 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34333 vec_safe_push (for_block, this_pre_body);
34334 init = NULL_TREE;
34336 else
34338 init = pop_stmt_list (this_pre_body);
34339 if (init && TREE_CODE (init) == STATEMENT_LIST)
34341 tree_stmt_iterator i = tsi_start (init);
34342 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34343 while (!tsi_end_p (i))
34345 tree t = tsi_stmt (i);
34346 if (TREE_CODE (t) == DECL_EXPR
34347 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34349 tsi_delink (&i);
34350 vec_safe_push (for_block, t);
34351 continue;
34353 break;
34355 if (tsi_one_before_end_p (i))
34357 tree t = tsi_stmt (i);
34358 tsi_delink (&i);
34359 free_stmt_list (init);
34360 init = t;
34364 this_pre_body = NULL_TREE;
34366 else
34368 /* Consume '='. */
34369 cp_lexer_consume_token (parser->lexer);
34370 init = cp_parser_assignment_expression (parser);
34372 non_class:
34373 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34374 init = error_mark_node;
34375 else
34376 cp_finish_decl (decl, NULL_TREE,
34377 /*init_const_expr_p=*/false,
34378 asm_specification,
34379 LOOKUP_ONLYCONVERTING);
34382 if (pushed_scope)
34383 pop_scope (pushed_scope);
34386 else
34388 cp_id_kind idk;
34389 /* If parsing a type specifier sequence failed, then
34390 this MUST be a simple expression. */
34391 if (code == CILK_FOR)
34392 error ("%<_Cilk_for%> allows expression instead of declaration only "
34393 "in C, not in C++");
34394 cp_parser_parse_tentatively (parser);
34395 decl = cp_parser_primary_expression (parser, false, false,
34396 false, &idk);
34397 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34398 if (!cp_parser_error_occurred (parser)
34399 && decl
34400 && (TREE_CODE (decl) == COMPONENT_REF
34401 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34403 cp_parser_abort_tentative_parse (parser);
34404 cp_parser_parse_tentatively (parser);
34405 cp_token *token = cp_lexer_peek_token (parser->lexer);
34406 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34407 /*check_dependency_p=*/true,
34408 /*template_p=*/NULL,
34409 /*declarator_p=*/false,
34410 /*optional_p=*/false);
34411 if (name != error_mark_node
34412 && last_tok == cp_lexer_peek_token (parser->lexer))
34414 decl = cp_parser_lookup_name_simple (parser, name,
34415 token->location);
34416 if (TREE_CODE (decl) == FIELD_DECL)
34417 add_private_clause = omp_privatize_field (decl, false);
34419 cp_parser_abort_tentative_parse (parser);
34420 cp_parser_parse_tentatively (parser);
34421 decl = cp_parser_primary_expression (parser, false, false,
34422 false, &idk);
34424 if (!cp_parser_error_occurred (parser)
34425 && decl
34426 && DECL_P (decl)
34427 && CLASS_TYPE_P (TREE_TYPE (decl)))
34429 tree rhs;
34431 cp_parser_parse_definitely (parser);
34432 cp_parser_require (parser, CPP_EQ, RT_EQ);
34433 rhs = cp_parser_assignment_expression (parser);
34434 orig_init = rhs;
34435 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34436 decl, NOP_EXPR,
34437 rhs,
34438 tf_warning_or_error));
34439 if (!add_private_clause)
34440 add_private_clause = decl;
34442 else
34444 decl = NULL;
34445 cp_parser_abort_tentative_parse (parser);
34446 init = cp_parser_expression (parser);
34447 if (init)
34449 if (TREE_CODE (init) == MODIFY_EXPR
34450 || TREE_CODE (init) == MODOP_EXPR)
34451 real_decl = TREE_OPERAND (init, 0);
34455 return add_private_clause;
34458 /* Parse the restricted form of the for statement allowed by OpenMP. */
34460 static tree
34461 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34462 tree *cclauses, bool *if_p)
34464 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34465 tree real_decl, initv, condv, incrv, declv;
34466 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34467 location_t loc_first;
34468 bool collapse_err = false;
34469 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34470 vec<tree, va_gc> *for_block = make_tree_vector ();
34471 auto_vec<tree, 4> orig_inits;
34472 bool tiling = false;
34474 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34475 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34476 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34477 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34479 tiling = true;
34480 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34482 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34483 && OMP_CLAUSE_ORDERED_EXPR (cl))
34485 ordered_cl = cl;
34486 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34489 if (ordered && ordered < collapse)
34491 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34492 "%<ordered%> clause parameter is less than %<collapse%>");
34493 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34494 = build_int_cst (NULL_TREE, collapse);
34495 ordered = collapse;
34497 if (ordered)
34499 for (tree *pc = &clauses; *pc; )
34500 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34502 error_at (OMP_CLAUSE_LOCATION (*pc),
34503 "%<linear%> clause may not be specified together "
34504 "with %<ordered%> clause with a parameter");
34505 *pc = OMP_CLAUSE_CHAIN (*pc);
34507 else
34508 pc = &OMP_CLAUSE_CHAIN (*pc);
34511 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34512 count = ordered ? ordered : collapse;
34514 declv = make_tree_vec (count);
34515 initv = make_tree_vec (count);
34516 condv = make_tree_vec (count);
34517 incrv = make_tree_vec (count);
34519 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34521 for (i = 0; i < count; i++)
34523 int bracecount = 0;
34524 tree add_private_clause = NULL_TREE;
34525 location_t loc;
34527 if (code != CILK_FOR
34528 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34530 if (!collapse_err)
34531 cp_parser_error (parser, "for statement expected");
34532 return NULL;
34534 if (code == CILK_FOR
34535 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34537 if (!collapse_err)
34538 cp_parser_error (parser, "_Cilk_for statement expected");
34539 return NULL;
34541 loc = cp_lexer_consume_token (parser->lexer)->location;
34543 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34544 return NULL;
34546 init = orig_init = decl = real_decl = NULL;
34547 this_pre_body = push_stmt_list ();
34549 add_private_clause
34550 = cp_parser_omp_for_loop_init (parser, code,
34551 this_pre_body, for_block,
34552 init, orig_init, decl, real_decl);
34554 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34555 if (this_pre_body)
34557 this_pre_body = pop_stmt_list (this_pre_body);
34558 if (pre_body)
34560 tree t = pre_body;
34561 pre_body = push_stmt_list ();
34562 add_stmt (t);
34563 add_stmt (this_pre_body);
34564 pre_body = pop_stmt_list (pre_body);
34566 else
34567 pre_body = this_pre_body;
34570 if (decl)
34571 real_decl = decl;
34572 if (cclauses != NULL
34573 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34574 && real_decl != NULL_TREE)
34576 tree *c;
34577 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34578 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34579 && OMP_CLAUSE_DECL (*c) == real_decl)
34581 error_at (loc, "iteration variable %qD"
34582 " should not be firstprivate", real_decl);
34583 *c = OMP_CLAUSE_CHAIN (*c);
34585 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34586 && OMP_CLAUSE_DECL (*c) == real_decl)
34588 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34589 tree l = *c;
34590 *c = OMP_CLAUSE_CHAIN (*c);
34591 if (code == OMP_SIMD)
34593 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34594 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34596 else
34598 OMP_CLAUSE_CHAIN (l) = clauses;
34599 clauses = l;
34601 add_private_clause = NULL_TREE;
34603 else
34605 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34606 && OMP_CLAUSE_DECL (*c) == real_decl)
34607 add_private_clause = NULL_TREE;
34608 c = &OMP_CLAUSE_CHAIN (*c);
34612 if (add_private_clause)
34614 tree c;
34615 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34617 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34618 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34619 && OMP_CLAUSE_DECL (c) == decl)
34620 break;
34621 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34622 && OMP_CLAUSE_DECL (c) == decl)
34623 error_at (loc, "iteration variable %qD "
34624 "should not be firstprivate",
34625 decl);
34626 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34627 && OMP_CLAUSE_DECL (c) == decl)
34628 error_at (loc, "iteration variable %qD should not be reduction",
34629 decl);
34631 if (c == NULL)
34633 if (code != OMP_SIMD)
34634 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34635 else if (collapse == 1)
34636 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34637 else
34638 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34639 OMP_CLAUSE_DECL (c) = add_private_clause;
34640 c = finish_omp_clauses (c, C_ORT_OMP);
34641 if (c)
34643 OMP_CLAUSE_CHAIN (c) = clauses;
34644 clauses = c;
34645 /* For linear, signal that we need to fill up
34646 the so far unknown linear step. */
34647 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34648 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34653 cond = NULL;
34654 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34655 cond = cp_parser_omp_for_cond (parser, decl, code);
34656 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34658 incr = NULL;
34659 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34661 /* If decl is an iterator, preserve the operator on decl
34662 until finish_omp_for. */
34663 if (real_decl
34664 && ((processing_template_decl
34665 && (TREE_TYPE (real_decl) == NULL_TREE
34666 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34667 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34668 incr = cp_parser_omp_for_incr (parser, real_decl);
34669 else
34670 incr = cp_parser_expression (parser);
34671 if (!EXPR_HAS_LOCATION (incr))
34672 protected_set_expr_location (incr, input_location);
34675 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34676 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34677 /*or_comma=*/false,
34678 /*consume_paren=*/true);
34680 TREE_VEC_ELT (declv, i) = decl;
34681 TREE_VEC_ELT (initv, i) = init;
34682 TREE_VEC_ELT (condv, i) = cond;
34683 TREE_VEC_ELT (incrv, i) = incr;
34684 if (orig_init)
34686 orig_inits.safe_grow_cleared (i + 1);
34687 orig_inits[i] = orig_init;
34690 if (i == count - 1)
34691 break;
34693 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34694 in between the collapsed for loops to be still considered perfectly
34695 nested. Hopefully the final version clarifies this.
34696 For now handle (multiple) {'s and empty statements. */
34697 cp_parser_parse_tentatively (parser);
34698 for (;;)
34700 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34701 break;
34702 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34704 cp_lexer_consume_token (parser->lexer);
34705 bracecount++;
34707 else if (bracecount
34708 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34709 cp_lexer_consume_token (parser->lexer);
34710 else
34712 loc = cp_lexer_peek_token (parser->lexer)->location;
34713 error_at (loc, "not enough for loops to collapse");
34714 collapse_err = true;
34715 cp_parser_abort_tentative_parse (parser);
34716 declv = NULL_TREE;
34717 break;
34721 if (declv)
34723 cp_parser_parse_definitely (parser);
34724 nbraces += bracecount;
34728 if (nbraces)
34729 if_p = NULL;
34731 /* Note that we saved the original contents of this flag when we entered
34732 the structured block, and so we don't need to re-save it here. */
34733 if (code == CILK_SIMD || code == CILK_FOR)
34734 parser->in_statement = IN_CILK_SIMD_FOR;
34735 else
34736 parser->in_statement = IN_OMP_FOR;
34738 /* Note that the grammar doesn't call for a structured block here,
34739 though the loop as a whole is a structured block. */
34740 body = push_stmt_list ();
34741 cp_parser_statement (parser, NULL_TREE, false, if_p);
34742 body = pop_stmt_list (body);
34744 if (declv == NULL_TREE)
34745 ret = NULL_TREE;
34746 else
34747 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
34748 body, pre_body, &orig_inits, clauses);
34750 while (nbraces)
34752 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34754 cp_lexer_consume_token (parser->lexer);
34755 nbraces--;
34757 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34758 cp_lexer_consume_token (parser->lexer);
34759 else
34761 if (!collapse_err)
34763 error_at (cp_lexer_peek_token (parser->lexer)->location,
34764 "collapsed loops not perfectly nested");
34766 collapse_err = true;
34767 cp_parser_statement_seq_opt (parser, NULL);
34768 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34769 break;
34773 while (!for_block->is_empty ())
34775 tree t = for_block->pop ();
34776 if (TREE_CODE (t) == STATEMENT_LIST)
34777 add_stmt (pop_stmt_list (t));
34778 else
34779 add_stmt (t);
34781 release_tree_vector (for_block);
34783 return ret;
34786 /* Helper function for OpenMP parsing, split clauses and call
34787 finish_omp_clauses on each of the set of clauses afterwards. */
34789 static void
34790 cp_omp_split_clauses (location_t loc, enum tree_code code,
34791 omp_clause_mask mask, tree clauses, tree *cclauses)
34793 int i;
34794 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
34795 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
34796 if (cclauses[i])
34797 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
34800 /* OpenMP 4.0:
34801 #pragma omp simd simd-clause[optseq] new-line
34802 for-loop */
34804 #define OMP_SIMD_CLAUSE_MASK \
34805 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34814 static tree
34815 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
34816 char *p_name, omp_clause_mask mask, tree *cclauses,
34817 bool *if_p)
34819 tree clauses, sb, ret;
34820 unsigned int save;
34821 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34823 strcat (p_name, " simd");
34824 mask |= OMP_SIMD_CLAUSE_MASK;
34826 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34827 cclauses == NULL);
34828 if (cclauses)
34830 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
34831 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
34832 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
34833 OMP_CLAUSE_ORDERED);
34834 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
34836 error_at (OMP_CLAUSE_LOCATION (c),
34837 "%<ordered%> clause with parameter may not be specified "
34838 "on %qs construct", p_name);
34839 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
34843 sb = begin_omp_structured_block ();
34844 save = cp_parser_begin_omp_structured_block (parser);
34846 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
34848 cp_parser_end_omp_structured_block (parser, save);
34849 add_stmt (finish_omp_structured_block (sb));
34851 return ret;
34854 /* OpenMP 2.5:
34855 #pragma omp for for-clause[optseq] new-line
34856 for-loop
34858 OpenMP 4.0:
34859 #pragma omp for simd for-simd-clause[optseq] new-line
34860 for-loop */
34862 #define OMP_FOR_CLAUSE_MASK \
34863 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34873 static tree
34874 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
34875 char *p_name, omp_clause_mask mask, tree *cclauses,
34876 bool *if_p)
34878 tree clauses, sb, ret;
34879 unsigned int save;
34880 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34882 strcat (p_name, " for");
34883 mask |= OMP_FOR_CLAUSE_MASK;
34884 /* parallel for{, simd} disallows nowait clause, but for
34885 target {teams distribute ,}parallel for{, simd} it should be accepted. */
34886 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
34887 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34888 /* Composite distribute parallel for{, simd} disallows ordered clause. */
34889 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34890 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
34892 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34894 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34895 const char *p = IDENTIFIER_POINTER (id);
34897 if (strcmp (p, "simd") == 0)
34899 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34900 if (cclauses == NULL)
34901 cclauses = cclauses_buf;
34903 cp_lexer_consume_token (parser->lexer);
34904 if (!flag_openmp) /* flag_openmp_simd */
34905 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34906 cclauses, if_p);
34907 sb = begin_omp_structured_block ();
34908 save = cp_parser_begin_omp_structured_block (parser);
34909 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34910 cclauses, if_p);
34911 cp_parser_end_omp_structured_block (parser, save);
34912 tree body = finish_omp_structured_block (sb);
34913 if (ret == NULL)
34914 return ret;
34915 ret = make_node (OMP_FOR);
34916 TREE_TYPE (ret) = void_type_node;
34917 OMP_FOR_BODY (ret) = body;
34918 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34919 SET_EXPR_LOCATION (ret, loc);
34920 add_stmt (ret);
34921 return ret;
34924 if (!flag_openmp) /* flag_openmp_simd */
34926 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34927 return NULL_TREE;
34930 /* Composite distribute parallel for disallows linear clause. */
34931 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34932 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
34934 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34935 cclauses == NULL);
34936 if (cclauses)
34938 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
34939 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34942 sb = begin_omp_structured_block ();
34943 save = cp_parser_begin_omp_structured_block (parser);
34945 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
34947 cp_parser_end_omp_structured_block (parser, save);
34948 add_stmt (finish_omp_structured_block (sb));
34950 return ret;
34953 /* OpenMP 2.5:
34954 # pragma omp master new-line
34955 structured-block */
34957 static tree
34958 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34960 cp_parser_require_pragma_eol (parser, pragma_tok);
34961 return c_finish_omp_master (input_location,
34962 cp_parser_omp_structured_block (parser, if_p));
34965 /* OpenMP 2.5:
34966 # pragma omp ordered new-line
34967 structured-block
34969 OpenMP 4.5:
34970 # pragma omp ordered ordered-clauses new-line
34971 structured-block */
34973 #define OMP_ORDERED_CLAUSE_MASK \
34974 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34977 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34978 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34980 static bool
34981 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34982 enum pragma_context context, bool *if_p)
34984 location_t loc = pragma_tok->location;
34986 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34988 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34989 const char *p = IDENTIFIER_POINTER (id);
34991 if (strcmp (p, "depend") == 0)
34993 if (context == pragma_stmt)
34995 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34996 "%<depend%> clause may only be used in compound "
34997 "statements");
34998 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34999 return false;
35001 tree clauses
35002 = cp_parser_omp_all_clauses (parser,
35003 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35004 "#pragma omp ordered", pragma_tok);
35005 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35006 return false;
35010 tree clauses
35011 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35012 "#pragma omp ordered", pragma_tok);
35013 c_finish_omp_ordered (loc, clauses,
35014 cp_parser_omp_structured_block (parser, if_p));
35015 return true;
35018 /* OpenMP 2.5:
35020 section-scope:
35021 { section-sequence }
35023 section-sequence:
35024 section-directive[opt] structured-block
35025 section-sequence section-directive structured-block */
35027 static tree
35028 cp_parser_omp_sections_scope (cp_parser *parser)
35030 tree stmt, substmt;
35031 bool error_suppress = false;
35032 cp_token *tok;
35034 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
35035 return NULL_TREE;
35037 stmt = push_stmt_list ();
35039 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35040 != PRAGMA_OMP_SECTION)
35042 substmt = cp_parser_omp_structured_block (parser, NULL);
35043 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35044 add_stmt (substmt);
35047 while (1)
35049 tok = cp_lexer_peek_token (parser->lexer);
35050 if (tok->type == CPP_CLOSE_BRACE)
35051 break;
35052 if (tok->type == CPP_EOF)
35053 break;
35055 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35057 cp_lexer_consume_token (parser->lexer);
35058 cp_parser_require_pragma_eol (parser, tok);
35059 error_suppress = false;
35061 else if (!error_suppress)
35063 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35064 error_suppress = true;
35067 substmt = cp_parser_omp_structured_block (parser, NULL);
35068 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35069 add_stmt (substmt);
35071 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35073 substmt = pop_stmt_list (stmt);
35075 stmt = make_node (OMP_SECTIONS);
35076 TREE_TYPE (stmt) = void_type_node;
35077 OMP_SECTIONS_BODY (stmt) = substmt;
35079 add_stmt (stmt);
35080 return stmt;
35083 /* OpenMP 2.5:
35084 # pragma omp sections sections-clause[optseq] newline
35085 sections-scope */
35087 #define OMP_SECTIONS_CLAUSE_MASK \
35088 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35094 static tree
35095 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35096 char *p_name, omp_clause_mask mask, tree *cclauses)
35098 tree clauses, ret;
35099 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35101 strcat (p_name, " sections");
35102 mask |= OMP_SECTIONS_CLAUSE_MASK;
35103 if (cclauses)
35104 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35106 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35107 cclauses == NULL);
35108 if (cclauses)
35110 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35111 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35114 ret = cp_parser_omp_sections_scope (parser);
35115 if (ret)
35116 OMP_SECTIONS_CLAUSES (ret) = clauses;
35118 return ret;
35121 /* OpenMP 2.5:
35122 # pragma omp parallel parallel-clause[optseq] new-line
35123 structured-block
35124 # pragma omp parallel for parallel-for-clause[optseq] new-line
35125 structured-block
35126 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35127 structured-block
35129 OpenMP 4.0:
35130 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35131 structured-block */
35133 #define OMP_PARALLEL_CLAUSE_MASK \
35134 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35144 static tree
35145 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35146 char *p_name, omp_clause_mask mask, tree *cclauses,
35147 bool *if_p)
35149 tree stmt, clauses, block;
35150 unsigned int save;
35151 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35153 strcat (p_name, " parallel");
35154 mask |= OMP_PARALLEL_CLAUSE_MASK;
35155 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35156 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35157 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35158 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35160 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35162 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35163 if (cclauses == NULL)
35164 cclauses = cclauses_buf;
35166 cp_lexer_consume_token (parser->lexer);
35167 if (!flag_openmp) /* flag_openmp_simd */
35168 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35169 if_p);
35170 block = begin_omp_parallel ();
35171 save = cp_parser_begin_omp_structured_block (parser);
35172 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35173 if_p);
35174 cp_parser_end_omp_structured_block (parser, save);
35175 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35176 block);
35177 if (ret == NULL_TREE)
35178 return ret;
35179 OMP_PARALLEL_COMBINED (stmt) = 1;
35180 return stmt;
35182 /* When combined with distribute, parallel has to be followed by for.
35183 #pragma omp target parallel is allowed though. */
35184 else if (cclauses
35185 && (mask & (OMP_CLAUSE_MASK_1
35186 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35188 error_at (loc, "expected %<for%> after %qs", p_name);
35189 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35190 return NULL_TREE;
35192 else if (!flag_openmp) /* flag_openmp_simd */
35194 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35195 return NULL_TREE;
35197 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35199 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35200 const char *p = IDENTIFIER_POINTER (id);
35201 if (strcmp (p, "sections") == 0)
35203 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35204 cclauses = cclauses_buf;
35206 cp_lexer_consume_token (parser->lexer);
35207 block = begin_omp_parallel ();
35208 save = cp_parser_begin_omp_structured_block (parser);
35209 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35210 cp_parser_end_omp_structured_block (parser, save);
35211 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35212 block);
35213 OMP_PARALLEL_COMBINED (stmt) = 1;
35214 return stmt;
35218 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35219 cclauses == NULL);
35220 if (cclauses)
35222 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35223 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35226 block = begin_omp_parallel ();
35227 save = cp_parser_begin_omp_structured_block (parser);
35228 cp_parser_statement (parser, NULL_TREE, false, if_p);
35229 cp_parser_end_omp_structured_block (parser, save);
35230 stmt = finish_omp_parallel (clauses, block);
35231 return stmt;
35234 /* OpenMP 2.5:
35235 # pragma omp single single-clause[optseq] new-line
35236 structured-block */
35238 #define OMP_SINGLE_CLAUSE_MASK \
35239 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35244 static tree
35245 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35247 tree stmt = make_node (OMP_SINGLE);
35248 TREE_TYPE (stmt) = void_type_node;
35250 OMP_SINGLE_CLAUSES (stmt)
35251 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35252 "#pragma omp single", pragma_tok);
35253 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35255 return add_stmt (stmt);
35258 /* OpenMP 3.0:
35259 # pragma omp task task-clause[optseq] new-line
35260 structured-block */
35262 #define OMP_TASK_CLAUSE_MASK \
35263 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35274 static tree
35275 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35277 tree clauses, block;
35278 unsigned int save;
35280 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35281 "#pragma omp task", pragma_tok);
35282 block = begin_omp_task ();
35283 save = cp_parser_begin_omp_structured_block (parser);
35284 cp_parser_statement (parser, NULL_TREE, false, if_p);
35285 cp_parser_end_omp_structured_block (parser, save);
35286 return finish_omp_task (clauses, block);
35289 /* OpenMP 3.0:
35290 # pragma omp taskwait new-line */
35292 static void
35293 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35295 cp_parser_require_pragma_eol (parser, pragma_tok);
35296 finish_omp_taskwait ();
35299 /* OpenMP 3.1:
35300 # pragma omp taskyield new-line */
35302 static void
35303 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35305 cp_parser_require_pragma_eol (parser, pragma_tok);
35306 finish_omp_taskyield ();
35309 /* OpenMP 4.0:
35310 # pragma omp taskgroup new-line
35311 structured-block */
35313 static tree
35314 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35316 cp_parser_require_pragma_eol (parser, pragma_tok);
35317 return c_finish_omp_taskgroup (input_location,
35318 cp_parser_omp_structured_block (parser,
35319 if_p));
35323 /* OpenMP 2.5:
35324 # pragma omp threadprivate (variable-list) */
35326 static void
35327 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35329 tree vars;
35331 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35332 cp_parser_require_pragma_eol (parser, pragma_tok);
35334 finish_omp_threadprivate (vars);
35337 /* OpenMP 4.0:
35338 # pragma omp cancel cancel-clause[optseq] new-line */
35340 #define OMP_CANCEL_CLAUSE_MASK \
35341 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35347 static void
35348 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35350 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35351 "#pragma omp cancel", pragma_tok);
35352 finish_omp_cancel (clauses);
35355 /* OpenMP 4.0:
35356 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35358 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35359 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35364 static void
35365 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35366 enum pragma_context context)
35368 tree clauses;
35369 bool point_seen = false;
35371 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35373 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35374 const char *p = IDENTIFIER_POINTER (id);
35376 if (strcmp (p, "point") == 0)
35378 cp_lexer_consume_token (parser->lexer);
35379 point_seen = true;
35382 if (!point_seen)
35384 cp_parser_error (parser, "expected %<point%>");
35385 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35386 return;
35389 if (context != pragma_compound)
35391 if (context == pragma_stmt)
35392 error_at (pragma_tok->location,
35393 "%<#pragma %s%> may only be used in compound statements",
35394 "omp cancellation point");
35395 else
35396 cp_parser_error (parser, "expected declaration specifiers");
35397 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35398 return;
35401 clauses = cp_parser_omp_all_clauses (parser,
35402 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35403 "#pragma omp cancellation point",
35404 pragma_tok);
35405 finish_omp_cancellation_point (clauses);
35408 /* OpenMP 4.0:
35409 #pragma omp distribute distribute-clause[optseq] new-line
35410 for-loop */
35412 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35413 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35419 static tree
35420 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35421 char *p_name, omp_clause_mask mask, tree *cclauses,
35422 bool *if_p)
35424 tree clauses, sb, ret;
35425 unsigned int save;
35426 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35428 strcat (p_name, " distribute");
35429 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35431 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35433 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35434 const char *p = IDENTIFIER_POINTER (id);
35435 bool simd = false;
35436 bool parallel = false;
35438 if (strcmp (p, "simd") == 0)
35439 simd = true;
35440 else
35441 parallel = strcmp (p, "parallel") == 0;
35442 if (parallel || simd)
35444 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35445 if (cclauses == NULL)
35446 cclauses = cclauses_buf;
35447 cp_lexer_consume_token (parser->lexer);
35448 if (!flag_openmp) /* flag_openmp_simd */
35450 if (simd)
35451 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35452 cclauses, if_p);
35453 else
35454 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35455 cclauses, if_p);
35457 sb = begin_omp_structured_block ();
35458 save = cp_parser_begin_omp_structured_block (parser);
35459 if (simd)
35460 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35461 cclauses, if_p);
35462 else
35463 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35464 cclauses, if_p);
35465 cp_parser_end_omp_structured_block (parser, save);
35466 tree body = finish_omp_structured_block (sb);
35467 if (ret == NULL)
35468 return ret;
35469 ret = make_node (OMP_DISTRIBUTE);
35470 TREE_TYPE (ret) = void_type_node;
35471 OMP_FOR_BODY (ret) = body;
35472 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35473 SET_EXPR_LOCATION (ret, loc);
35474 add_stmt (ret);
35475 return ret;
35478 if (!flag_openmp) /* flag_openmp_simd */
35480 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35481 return NULL_TREE;
35484 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35485 cclauses == NULL);
35486 if (cclauses)
35488 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35489 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35492 sb = begin_omp_structured_block ();
35493 save = cp_parser_begin_omp_structured_block (parser);
35495 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35497 cp_parser_end_omp_structured_block (parser, save);
35498 add_stmt (finish_omp_structured_block (sb));
35500 return ret;
35503 /* OpenMP 4.0:
35504 # pragma omp teams teams-clause[optseq] new-line
35505 structured-block */
35507 #define OMP_TEAMS_CLAUSE_MASK \
35508 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35516 static tree
35517 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35518 char *p_name, omp_clause_mask mask, tree *cclauses,
35519 bool *if_p)
35521 tree clauses, sb, ret;
35522 unsigned int save;
35523 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35525 strcat (p_name, " teams");
35526 mask |= OMP_TEAMS_CLAUSE_MASK;
35528 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35530 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35531 const char *p = IDENTIFIER_POINTER (id);
35532 if (strcmp (p, "distribute") == 0)
35534 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35535 if (cclauses == NULL)
35536 cclauses = cclauses_buf;
35538 cp_lexer_consume_token (parser->lexer);
35539 if (!flag_openmp) /* flag_openmp_simd */
35540 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35541 cclauses, if_p);
35542 sb = begin_omp_structured_block ();
35543 save = cp_parser_begin_omp_structured_block (parser);
35544 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35545 cclauses, if_p);
35546 cp_parser_end_omp_structured_block (parser, save);
35547 tree body = finish_omp_structured_block (sb);
35548 if (ret == NULL)
35549 return ret;
35550 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35551 ret = make_node (OMP_TEAMS);
35552 TREE_TYPE (ret) = void_type_node;
35553 OMP_TEAMS_CLAUSES (ret) = clauses;
35554 OMP_TEAMS_BODY (ret) = body;
35555 OMP_TEAMS_COMBINED (ret) = 1;
35556 SET_EXPR_LOCATION (ret, loc);
35557 return add_stmt (ret);
35560 if (!flag_openmp) /* flag_openmp_simd */
35562 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35563 return NULL_TREE;
35566 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35567 cclauses == NULL);
35568 if (cclauses)
35570 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35571 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35574 tree stmt = make_node (OMP_TEAMS);
35575 TREE_TYPE (stmt) = void_type_node;
35576 OMP_TEAMS_CLAUSES (stmt) = clauses;
35577 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35578 SET_EXPR_LOCATION (stmt, loc);
35580 return add_stmt (stmt);
35583 /* OpenMP 4.0:
35584 # pragma omp target data target-data-clause[optseq] new-line
35585 structured-block */
35587 #define OMP_TARGET_DATA_CLAUSE_MASK \
35588 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35593 static tree
35594 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35596 tree clauses
35597 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35598 "#pragma omp target data", pragma_tok);
35599 int map_seen = 0;
35600 for (tree *pc = &clauses; *pc;)
35602 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35603 switch (OMP_CLAUSE_MAP_KIND (*pc))
35605 case GOMP_MAP_TO:
35606 case GOMP_MAP_ALWAYS_TO:
35607 case GOMP_MAP_FROM:
35608 case GOMP_MAP_ALWAYS_FROM:
35609 case GOMP_MAP_TOFROM:
35610 case GOMP_MAP_ALWAYS_TOFROM:
35611 case GOMP_MAP_ALLOC:
35612 map_seen = 3;
35613 break;
35614 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35615 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35616 case GOMP_MAP_ALWAYS_POINTER:
35617 break;
35618 default:
35619 map_seen |= 1;
35620 error_at (OMP_CLAUSE_LOCATION (*pc),
35621 "%<#pragma omp target data%> with map-type other "
35622 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35623 "on %<map%> clause");
35624 *pc = OMP_CLAUSE_CHAIN (*pc);
35625 continue;
35627 pc = &OMP_CLAUSE_CHAIN (*pc);
35630 if (map_seen != 3)
35632 if (map_seen == 0)
35633 error_at (pragma_tok->location,
35634 "%<#pragma omp target data%> must contain at least "
35635 "one %<map%> clause");
35636 return NULL_TREE;
35639 tree stmt = make_node (OMP_TARGET_DATA);
35640 TREE_TYPE (stmt) = void_type_node;
35641 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35643 keep_next_level (true);
35644 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35646 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35647 return add_stmt (stmt);
35650 /* OpenMP 4.5:
35651 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35652 structured-block */
35654 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35655 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35661 static tree
35662 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35663 enum pragma_context context)
35665 bool data_seen = false;
35666 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35668 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35669 const char *p = IDENTIFIER_POINTER (id);
35671 if (strcmp (p, "data") == 0)
35673 cp_lexer_consume_token (parser->lexer);
35674 data_seen = true;
35677 if (!data_seen)
35679 cp_parser_error (parser, "expected %<data%>");
35680 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35681 return NULL_TREE;
35684 if (context == pragma_stmt)
35686 error_at (pragma_tok->location,
35687 "%<#pragma %s%> may only be used in compound statements",
35688 "omp target enter data");
35689 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35690 return NULL_TREE;
35693 tree clauses
35694 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
35695 "#pragma omp target enter data", pragma_tok);
35696 int map_seen = 0;
35697 for (tree *pc = &clauses; *pc;)
35699 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35700 switch (OMP_CLAUSE_MAP_KIND (*pc))
35702 case GOMP_MAP_TO:
35703 case GOMP_MAP_ALWAYS_TO:
35704 case GOMP_MAP_ALLOC:
35705 map_seen = 3;
35706 break;
35707 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35708 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35709 case GOMP_MAP_ALWAYS_POINTER:
35710 break;
35711 default:
35712 map_seen |= 1;
35713 error_at (OMP_CLAUSE_LOCATION (*pc),
35714 "%<#pragma omp target enter data%> with map-type other "
35715 "than %<to%> or %<alloc%> on %<map%> clause");
35716 *pc = OMP_CLAUSE_CHAIN (*pc);
35717 continue;
35719 pc = &OMP_CLAUSE_CHAIN (*pc);
35722 if (map_seen != 3)
35724 if (map_seen == 0)
35725 error_at (pragma_tok->location,
35726 "%<#pragma omp target enter data%> must contain at least "
35727 "one %<map%> clause");
35728 return NULL_TREE;
35731 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
35732 TREE_TYPE (stmt) = void_type_node;
35733 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
35734 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35735 return add_stmt (stmt);
35738 /* OpenMP 4.5:
35739 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35740 structured-block */
35742 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35743 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35749 static tree
35750 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
35751 enum pragma_context context)
35753 bool data_seen = false;
35754 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35756 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35757 const char *p = IDENTIFIER_POINTER (id);
35759 if (strcmp (p, "data") == 0)
35761 cp_lexer_consume_token (parser->lexer);
35762 data_seen = true;
35765 if (!data_seen)
35767 cp_parser_error (parser, "expected %<data%>");
35768 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35769 return NULL_TREE;
35772 if (context == pragma_stmt)
35774 error_at (pragma_tok->location,
35775 "%<#pragma %s%> may only be used in compound statements",
35776 "omp target exit data");
35777 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35778 return NULL_TREE;
35781 tree clauses
35782 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
35783 "#pragma omp target exit data", pragma_tok);
35784 int map_seen = 0;
35785 for (tree *pc = &clauses; *pc;)
35787 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35788 switch (OMP_CLAUSE_MAP_KIND (*pc))
35790 case GOMP_MAP_FROM:
35791 case GOMP_MAP_ALWAYS_FROM:
35792 case GOMP_MAP_RELEASE:
35793 case GOMP_MAP_DELETE:
35794 map_seen = 3;
35795 break;
35796 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35797 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35798 case GOMP_MAP_ALWAYS_POINTER:
35799 break;
35800 default:
35801 map_seen |= 1;
35802 error_at (OMP_CLAUSE_LOCATION (*pc),
35803 "%<#pragma omp target exit data%> with map-type other "
35804 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35805 " clause");
35806 *pc = OMP_CLAUSE_CHAIN (*pc);
35807 continue;
35809 pc = &OMP_CLAUSE_CHAIN (*pc);
35812 if (map_seen != 3)
35814 if (map_seen == 0)
35815 error_at (pragma_tok->location,
35816 "%<#pragma omp target exit data%> must contain at least "
35817 "one %<map%> clause");
35818 return NULL_TREE;
35821 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
35822 TREE_TYPE (stmt) = void_type_node;
35823 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
35824 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35825 return add_stmt (stmt);
35828 /* OpenMP 4.0:
35829 # pragma omp target update target-update-clause[optseq] new-line */
35831 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
35832 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35839 static bool
35840 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
35841 enum pragma_context context)
35843 if (context == pragma_stmt)
35845 error_at (pragma_tok->location,
35846 "%<#pragma %s%> may only be used in compound statements",
35847 "omp target update");
35848 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35849 return false;
35852 tree clauses
35853 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
35854 "#pragma omp target update", pragma_tok);
35855 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
35856 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
35858 error_at (pragma_tok->location,
35859 "%<#pragma omp target update%> must contain at least one "
35860 "%<from%> or %<to%> clauses");
35861 return false;
35864 tree stmt = make_node (OMP_TARGET_UPDATE);
35865 TREE_TYPE (stmt) = void_type_node;
35866 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
35867 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35868 add_stmt (stmt);
35869 return false;
35872 /* OpenMP 4.0:
35873 # pragma omp target target-clause[optseq] new-line
35874 structured-block */
35876 #define OMP_TARGET_CLAUSE_MASK \
35877 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
35885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
35887 static bool
35888 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
35889 enum pragma_context context, bool *if_p)
35891 tree *pc = NULL, stmt;
35893 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35895 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35896 const char *p = IDENTIFIER_POINTER (id);
35897 enum tree_code ccode = ERROR_MARK;
35899 if (strcmp (p, "teams") == 0)
35900 ccode = OMP_TEAMS;
35901 else if (strcmp (p, "parallel") == 0)
35902 ccode = OMP_PARALLEL;
35903 else if (strcmp (p, "simd") == 0)
35904 ccode = OMP_SIMD;
35905 if (ccode != ERROR_MARK)
35907 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
35908 char p_name[sizeof ("#pragma omp target teams distribute "
35909 "parallel for simd")];
35911 cp_lexer_consume_token (parser->lexer);
35912 strcpy (p_name, "#pragma omp target");
35913 if (!flag_openmp) /* flag_openmp_simd */
35915 tree stmt;
35916 switch (ccode)
35918 case OMP_TEAMS:
35919 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
35920 OMP_TARGET_CLAUSE_MASK,
35921 cclauses, if_p);
35922 break;
35923 case OMP_PARALLEL:
35924 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35925 OMP_TARGET_CLAUSE_MASK,
35926 cclauses, if_p);
35927 break;
35928 case OMP_SIMD:
35929 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35930 OMP_TARGET_CLAUSE_MASK,
35931 cclauses, if_p);
35932 break;
35933 default:
35934 gcc_unreachable ();
35936 return stmt != NULL_TREE;
35938 keep_next_level (true);
35939 tree sb = begin_omp_structured_block (), ret;
35940 unsigned save = cp_parser_begin_omp_structured_block (parser);
35941 switch (ccode)
35943 case OMP_TEAMS:
35944 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
35945 OMP_TARGET_CLAUSE_MASK, cclauses,
35946 if_p);
35947 break;
35948 case OMP_PARALLEL:
35949 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35950 OMP_TARGET_CLAUSE_MASK, cclauses,
35951 if_p);
35952 break;
35953 case OMP_SIMD:
35954 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35955 OMP_TARGET_CLAUSE_MASK, cclauses,
35956 if_p);
35957 break;
35958 default:
35959 gcc_unreachable ();
35961 cp_parser_end_omp_structured_block (parser, save);
35962 tree body = finish_omp_structured_block (sb);
35963 if (ret == NULL_TREE)
35964 return false;
35965 if (ccode == OMP_TEAMS && !processing_template_decl)
35967 /* For combined target teams, ensure the num_teams and
35968 thread_limit clause expressions are evaluated on the host,
35969 before entering the target construct. */
35970 tree c;
35971 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35972 c; c = OMP_CLAUSE_CHAIN (c))
35973 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35974 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35975 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35977 tree expr = OMP_CLAUSE_OPERAND (c, 0);
35978 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35979 if (expr == error_mark_node)
35980 continue;
35981 tree tmp = TARGET_EXPR_SLOT (expr);
35982 add_stmt (expr);
35983 OMP_CLAUSE_OPERAND (c, 0) = expr;
35984 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35985 OMP_CLAUSE_FIRSTPRIVATE);
35986 OMP_CLAUSE_DECL (tc) = tmp;
35987 OMP_CLAUSE_CHAIN (tc)
35988 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35989 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35992 tree stmt = make_node (OMP_TARGET);
35993 TREE_TYPE (stmt) = void_type_node;
35994 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35995 OMP_TARGET_BODY (stmt) = body;
35996 OMP_TARGET_COMBINED (stmt) = 1;
35997 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35998 add_stmt (stmt);
35999 pc = &OMP_TARGET_CLAUSES (stmt);
36000 goto check_clauses;
36002 else if (!flag_openmp) /* flag_openmp_simd */
36004 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36005 return false;
36007 else if (strcmp (p, "data") == 0)
36009 cp_lexer_consume_token (parser->lexer);
36010 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36011 return true;
36013 else if (strcmp (p, "enter") == 0)
36015 cp_lexer_consume_token (parser->lexer);
36016 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36017 return false;
36019 else if (strcmp (p, "exit") == 0)
36021 cp_lexer_consume_token (parser->lexer);
36022 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36023 return false;
36025 else if (strcmp (p, "update") == 0)
36027 cp_lexer_consume_token (parser->lexer);
36028 return cp_parser_omp_target_update (parser, pragma_tok, context);
36031 if (!flag_openmp) /* flag_openmp_simd */
36033 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36034 return false;
36037 stmt = make_node (OMP_TARGET);
36038 TREE_TYPE (stmt) = void_type_node;
36040 OMP_TARGET_CLAUSES (stmt)
36041 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36042 "#pragma omp target", pragma_tok);
36043 pc = &OMP_TARGET_CLAUSES (stmt);
36044 keep_next_level (true);
36045 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36047 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36048 add_stmt (stmt);
36050 check_clauses:
36051 while (*pc)
36053 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36054 switch (OMP_CLAUSE_MAP_KIND (*pc))
36056 case GOMP_MAP_TO:
36057 case GOMP_MAP_ALWAYS_TO:
36058 case GOMP_MAP_FROM:
36059 case GOMP_MAP_ALWAYS_FROM:
36060 case GOMP_MAP_TOFROM:
36061 case GOMP_MAP_ALWAYS_TOFROM:
36062 case GOMP_MAP_ALLOC:
36063 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36064 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36065 case GOMP_MAP_ALWAYS_POINTER:
36066 break;
36067 default:
36068 error_at (OMP_CLAUSE_LOCATION (*pc),
36069 "%<#pragma omp target%> with map-type other "
36070 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36071 "on %<map%> clause");
36072 *pc = OMP_CLAUSE_CHAIN (*pc);
36073 continue;
36075 pc = &OMP_CLAUSE_CHAIN (*pc);
36077 return true;
36080 /* OpenACC 2.0:
36081 # pragma acc cache (variable-list) new-line
36084 static tree
36085 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36087 tree stmt, clauses;
36089 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36090 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36092 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36094 stmt = make_node (OACC_CACHE);
36095 TREE_TYPE (stmt) = void_type_node;
36096 OACC_CACHE_CLAUSES (stmt) = clauses;
36097 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36098 add_stmt (stmt);
36100 return stmt;
36103 /* OpenACC 2.0:
36104 # pragma acc data oacc-data-clause[optseq] new-line
36105 structured-block */
36107 #define OACC_DATA_CLAUSE_MASK \
36108 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36120 static tree
36121 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36123 tree stmt, clauses, block;
36124 unsigned int save;
36126 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36127 "#pragma acc data", pragma_tok);
36129 block = begin_omp_parallel ();
36130 save = cp_parser_begin_omp_structured_block (parser);
36131 cp_parser_statement (parser, NULL_TREE, false, if_p);
36132 cp_parser_end_omp_structured_block (parser, save);
36133 stmt = finish_oacc_data (clauses, block);
36134 return stmt;
36137 /* OpenACC 2.0:
36138 # pragma acc host_data <clauses> new-line
36139 structured-block */
36141 #define OACC_HOST_DATA_CLAUSE_MASK \
36142 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36144 static tree
36145 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36147 tree stmt, clauses, block;
36148 unsigned int save;
36150 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36151 "#pragma acc host_data", pragma_tok);
36153 block = begin_omp_parallel ();
36154 save = cp_parser_begin_omp_structured_block (parser);
36155 cp_parser_statement (parser, NULL_TREE, false, if_p);
36156 cp_parser_end_omp_structured_block (parser, save);
36157 stmt = finish_oacc_host_data (clauses, block);
36158 return stmt;
36161 /* OpenACC 2.0:
36162 # pragma acc declare oacc-data-clause[optseq] new-line
36165 #define OACC_DECLARE_CLAUSE_MASK \
36166 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36179 static tree
36180 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36182 tree clauses, stmt;
36183 bool error = false;
36185 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36186 "#pragma acc declare", pragma_tok, true);
36189 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36191 error_at (pragma_tok->location,
36192 "no valid clauses specified in %<#pragma acc declare%>");
36193 return NULL_TREE;
36196 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36198 location_t loc = OMP_CLAUSE_LOCATION (t);
36199 tree decl = OMP_CLAUSE_DECL (t);
36200 if (!DECL_P (decl))
36202 error_at (loc, "array section in %<#pragma acc declare%>");
36203 error = true;
36204 continue;
36206 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36207 switch (OMP_CLAUSE_MAP_KIND (t))
36209 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36210 case GOMP_MAP_FORCE_ALLOC:
36211 case GOMP_MAP_FORCE_TO:
36212 case GOMP_MAP_FORCE_DEVICEPTR:
36213 case GOMP_MAP_DEVICE_RESIDENT:
36214 break;
36216 case GOMP_MAP_LINK:
36217 if (!global_bindings_p ()
36218 && (TREE_STATIC (decl)
36219 || !DECL_EXTERNAL (decl)))
36221 error_at (loc,
36222 "%qD must be a global variable in "
36223 "%<#pragma acc declare link%>",
36224 decl);
36225 error = true;
36226 continue;
36228 break;
36230 default:
36231 if (global_bindings_p ())
36233 error_at (loc, "invalid OpenACC clause at file scope");
36234 error = true;
36235 continue;
36237 if (DECL_EXTERNAL (decl))
36239 error_at (loc,
36240 "invalid use of %<extern%> variable %qD "
36241 "in %<#pragma acc declare%>", decl);
36242 error = true;
36243 continue;
36245 else if (TREE_PUBLIC (decl))
36247 error_at (loc,
36248 "invalid use of %<global%> variable %qD "
36249 "in %<#pragma acc declare%>", decl);
36250 error = true;
36251 continue;
36253 break;
36256 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36257 || lookup_attribute ("omp declare target link",
36258 DECL_ATTRIBUTES (decl)))
36260 error_at (loc, "variable %qD used more than once with "
36261 "%<#pragma acc declare%>", decl);
36262 error = true;
36263 continue;
36266 if (!error)
36268 tree id;
36270 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36271 id = get_identifier ("omp declare target link");
36272 else
36273 id = get_identifier ("omp declare target");
36275 DECL_ATTRIBUTES (decl)
36276 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36277 if (global_bindings_p ())
36279 symtab_node *node = symtab_node::get (decl);
36280 if (node != NULL)
36282 node->offloadable = 1;
36283 if (ENABLE_OFFLOADING)
36285 g->have_offload = true;
36286 if (is_a <varpool_node *> (node))
36287 vec_safe_push (offload_vars, decl);
36294 if (error || global_bindings_p ())
36295 return NULL_TREE;
36297 stmt = make_node (OACC_DECLARE);
36298 TREE_TYPE (stmt) = void_type_node;
36299 OACC_DECLARE_CLAUSES (stmt) = clauses;
36300 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36302 add_stmt (stmt);
36304 return NULL_TREE;
36307 /* OpenACC 2.0:
36308 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36312 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36314 LOC is the location of the #pragma token.
36317 #define OACC_ENTER_DATA_CLAUSE_MASK \
36318 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36326 #define OACC_EXIT_DATA_CLAUSE_MASK \
36327 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36333 static tree
36334 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36335 bool enter)
36337 location_t loc = pragma_tok->location;
36338 tree stmt, clauses;
36339 const char *p = "";
36341 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36342 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36344 if (strcmp (p, "data") != 0)
36346 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36347 enter ? "enter" : "exit");
36348 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36349 return NULL_TREE;
36352 cp_lexer_consume_token (parser->lexer);
36354 if (enter)
36355 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36356 "#pragma acc enter data", pragma_tok);
36357 else
36358 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36359 "#pragma acc exit data", pragma_tok);
36361 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36363 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36364 enter ? "enter" : "exit");
36365 return NULL_TREE;
36368 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36369 TREE_TYPE (stmt) = void_type_node;
36370 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36371 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36372 add_stmt (stmt);
36373 return stmt;
36376 /* OpenACC 2.0:
36377 # pragma acc loop oacc-loop-clause[optseq] new-line
36378 structured-block */
36380 #define OACC_LOOP_CLAUSE_MASK \
36381 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36392 static tree
36393 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36394 omp_clause_mask mask, tree *cclauses, bool *if_p)
36396 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36398 strcat (p_name, " loop");
36399 mask |= OACC_LOOP_CLAUSE_MASK;
36401 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36402 cclauses == NULL);
36403 if (cclauses)
36405 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36406 if (*cclauses)
36407 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36408 if (clauses)
36409 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36412 tree block = begin_omp_structured_block ();
36413 int save = cp_parser_begin_omp_structured_block (parser);
36414 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36415 cp_parser_end_omp_structured_block (parser, save);
36416 add_stmt (finish_omp_structured_block (block));
36418 return stmt;
36421 /* OpenACC 2.0:
36422 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36423 structured-block
36427 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36428 structured-block
36431 #define OACC_KERNELS_CLAUSE_MASK \
36432 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36450 #define OACC_PARALLEL_CLAUSE_MASK \
36451 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36472 static tree
36473 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36474 char *p_name, bool *if_p)
36476 omp_clause_mask mask;
36477 enum tree_code code;
36478 switch (cp_parser_pragma_kind (pragma_tok))
36480 case PRAGMA_OACC_KERNELS:
36481 strcat (p_name, " kernels");
36482 mask = OACC_KERNELS_CLAUSE_MASK;
36483 code = OACC_KERNELS;
36484 break;
36485 case PRAGMA_OACC_PARALLEL:
36486 strcat (p_name, " parallel");
36487 mask = OACC_PARALLEL_CLAUSE_MASK;
36488 code = OACC_PARALLEL;
36489 break;
36490 default:
36491 gcc_unreachable ();
36494 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36496 const char *p
36497 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36498 if (strcmp (p, "loop") == 0)
36500 cp_lexer_consume_token (parser->lexer);
36501 tree block = begin_omp_parallel ();
36502 tree clauses;
36503 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36504 if_p);
36505 return finish_omp_construct (code, block, clauses);
36509 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36511 tree block = begin_omp_parallel ();
36512 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36513 cp_parser_statement (parser, NULL_TREE, false, if_p);
36514 cp_parser_end_omp_structured_block (parser, save);
36515 return finish_omp_construct (code, block, clauses);
36518 /* OpenACC 2.0:
36519 # pragma acc update oacc-update-clause[optseq] new-line
36522 #define OACC_UPDATE_CLAUSE_MASK \
36523 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36530 static tree
36531 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36533 tree stmt, clauses;
36535 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36536 "#pragma acc update", pragma_tok);
36538 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36540 error_at (pragma_tok->location,
36541 "%<#pragma acc update%> must contain at least one "
36542 "%<device%> or %<host%> or %<self%> clause");
36543 return NULL_TREE;
36546 stmt = make_node (OACC_UPDATE);
36547 TREE_TYPE (stmt) = void_type_node;
36548 OACC_UPDATE_CLAUSES (stmt) = clauses;
36549 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36550 add_stmt (stmt);
36551 return stmt;
36554 /* OpenACC 2.0:
36555 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36557 LOC is the location of the #pragma token.
36560 #define OACC_WAIT_CLAUSE_MASK \
36561 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36563 static tree
36564 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36566 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36567 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36569 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36570 list = cp_parser_oacc_wait_list (parser, loc, list);
36572 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36573 "#pragma acc wait", pragma_tok);
36575 stmt = c_finish_oacc_wait (loc, list, clauses);
36576 stmt = finish_expr_stmt (stmt);
36578 return stmt;
36581 /* OpenMP 4.0:
36582 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36584 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36585 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36592 static void
36593 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36594 enum pragma_context context)
36596 bool first_p = parser->omp_declare_simd == NULL;
36597 cp_omp_declare_simd_data data;
36598 if (first_p)
36600 data.error_seen = false;
36601 data.fndecl_seen = false;
36602 data.tokens = vNULL;
36603 data.clauses = NULL_TREE;
36604 /* It is safe to take the address of a local variable; it will only be
36605 used while this scope is live. */
36606 parser->omp_declare_simd = &data;
36609 /* Store away all pragma tokens. */
36610 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36611 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36612 cp_lexer_consume_token (parser->lexer);
36613 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36614 parser->omp_declare_simd->error_seen = true;
36615 cp_parser_require_pragma_eol (parser, pragma_tok);
36616 struct cp_token_cache *cp
36617 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36618 parser->omp_declare_simd->tokens.safe_push (cp);
36620 if (first_p)
36622 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36623 cp_parser_pragma (parser, context, NULL);
36624 switch (context)
36626 case pragma_external:
36627 cp_parser_declaration (parser);
36628 break;
36629 case pragma_member:
36630 cp_parser_member_declaration (parser);
36631 break;
36632 case pragma_objc_icode:
36633 cp_parser_block_declaration (parser, /*statement_p=*/false);
36634 break;
36635 default:
36636 cp_parser_declaration_statement (parser);
36637 break;
36639 if (parser->omp_declare_simd
36640 && !parser->omp_declare_simd->error_seen
36641 && !parser->omp_declare_simd->fndecl_seen)
36642 error_at (pragma_tok->location,
36643 "%<#pragma omp declare simd%> not immediately followed by "
36644 "function declaration or definition");
36645 data.tokens.release ();
36646 parser->omp_declare_simd = NULL;
36650 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36651 This function is modelled similar to the late parsing of omp declare
36652 simd. */
36654 static tree
36655 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
36657 struct cp_token_cache *ce;
36658 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
36659 int ii = 0;
36661 if (parser->omp_declare_simd != NULL
36662 || lookup_attribute ("simd", attrs))
36664 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36665 "used in the same function marked as a Cilk Plus SIMD-enabled "
36666 "function");
36667 parser->cilk_simd_fn_info->tokens.release ();
36668 XDELETE (parser->cilk_simd_fn_info);
36669 parser->cilk_simd_fn_info = NULL;
36670 return attrs;
36672 if (!info->error_seen && info->fndecl_seen)
36674 error ("vector attribute not immediately followed by a single function"
36675 " declaration or definition");
36676 info->error_seen = true;
36678 if (info->error_seen)
36679 return attrs;
36681 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
36683 tree c, cl;
36685 cp_parser_push_lexer_for_tokens (parser, ce);
36686 parser->lexer->in_pragma = true;
36687 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
36688 "SIMD-enabled functions attribute",
36689 NULL);
36690 cp_parser_pop_lexer (parser);
36691 if (cl)
36692 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36694 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
36695 TREE_CHAIN (c) = attrs;
36696 attrs = c;
36698 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36699 TREE_CHAIN (c) = attrs;
36700 if (processing_template_decl)
36701 ATTR_IS_DEPENDENT (c) = 1;
36702 attrs = c;
36704 info->fndecl_seen = true;
36705 parser->cilk_simd_fn_info->tokens.release ();
36706 XDELETE (parser->cilk_simd_fn_info);
36707 parser->cilk_simd_fn_info = NULL;
36708 return attrs;
36711 /* Finalize #pragma omp declare simd clauses after direct declarator has
36712 been parsed, and put that into "omp declare simd" attribute. */
36714 static tree
36715 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
36717 struct cp_token_cache *ce;
36718 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
36719 int i;
36721 if (!data->error_seen && data->fndecl_seen)
36723 error ("%<#pragma omp declare simd%> not immediately followed by "
36724 "a single function declaration or definition");
36725 data->error_seen = true;
36727 if (data->error_seen)
36728 return attrs;
36730 FOR_EACH_VEC_ELT (data->tokens, i, ce)
36732 tree c, cl;
36734 cp_parser_push_lexer_for_tokens (parser, ce);
36735 parser->lexer->in_pragma = true;
36736 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36737 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36738 cp_lexer_consume_token (parser->lexer);
36739 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
36740 "#pragma omp declare simd", pragma_tok);
36741 cp_parser_pop_lexer (parser);
36742 if (cl)
36743 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36744 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36745 TREE_CHAIN (c) = attrs;
36746 if (processing_template_decl)
36747 ATTR_IS_DEPENDENT (c) = 1;
36748 attrs = c;
36751 data->fndecl_seen = true;
36752 return attrs;
36756 /* OpenMP 4.0:
36757 # pragma omp declare target new-line
36758 declarations and definitions
36759 # pragma omp end declare target new-line
36761 OpenMP 4.5:
36762 # pragma omp declare target ( extended-list ) new-line
36764 # pragma omp declare target declare-target-clauses[seq] new-line */
36766 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36767 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36770 static void
36771 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
36773 tree clauses = NULL_TREE;
36774 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36775 clauses
36776 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
36777 "#pragma omp declare target", pragma_tok);
36778 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36780 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36781 clauses);
36782 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36783 cp_parser_require_pragma_eol (parser, pragma_tok);
36785 else
36787 cp_parser_require_pragma_eol (parser, pragma_tok);
36788 scope_chain->omp_declare_target_attribute++;
36789 return;
36791 if (scope_chain->omp_declare_target_attribute)
36792 error_at (pragma_tok->location,
36793 "%<#pragma omp declare target%> with clauses in between "
36794 "%<#pragma omp declare target%> without clauses and "
36795 "%<#pragma omp end declare target%>");
36796 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
36798 tree t = OMP_CLAUSE_DECL (c), id;
36799 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
36800 tree at2 = lookup_attribute ("omp declare target link",
36801 DECL_ATTRIBUTES (t));
36802 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
36804 id = get_identifier ("omp declare target link");
36805 std::swap (at1, at2);
36807 else
36808 id = get_identifier ("omp declare target");
36809 if (at2)
36811 error_at (OMP_CLAUSE_LOCATION (c),
36812 "%qD specified both in declare target %<link%> and %<to%>"
36813 " clauses", t);
36814 continue;
36816 if (!at1)
36818 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
36819 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
36820 continue;
36822 symtab_node *node = symtab_node::get (t);
36823 if (node != NULL)
36825 node->offloadable = 1;
36826 if (ENABLE_OFFLOADING)
36828 g->have_offload = true;
36829 if (is_a <varpool_node *> (node))
36830 vec_safe_push (offload_vars, t);
36837 static void
36838 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
36840 const char *p = "";
36841 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36843 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36844 p = IDENTIFIER_POINTER (id);
36846 if (strcmp (p, "declare") == 0)
36848 cp_lexer_consume_token (parser->lexer);
36849 p = "";
36850 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36852 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36853 p = IDENTIFIER_POINTER (id);
36855 if (strcmp (p, "target") == 0)
36856 cp_lexer_consume_token (parser->lexer);
36857 else
36859 cp_parser_error (parser, "expected %<target%>");
36860 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36861 return;
36864 else
36866 cp_parser_error (parser, "expected %<declare%>");
36867 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36868 return;
36870 cp_parser_require_pragma_eol (parser, pragma_tok);
36871 if (!scope_chain->omp_declare_target_attribute)
36872 error_at (pragma_tok->location,
36873 "%<#pragma omp end declare target%> without corresponding "
36874 "%<#pragma omp declare target%>");
36875 else
36876 scope_chain->omp_declare_target_attribute--;
36879 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
36880 expression and optional initializer clause of
36881 #pragma omp declare reduction. We store the expression(s) as
36882 either 3, 6 or 7 special statements inside of the artificial function's
36883 body. The first two statements are DECL_EXPRs for the artificial
36884 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
36885 expression that uses those variables.
36886 If there was any INITIALIZER clause, this is followed by further statements,
36887 the fourth and fifth statements are DECL_EXPRs for the artificial
36888 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
36889 constructor variant (first token after open paren is not omp_priv),
36890 then the sixth statement is a statement with the function call expression
36891 that uses the OMP_PRIV and optionally OMP_ORIG variable.
36892 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
36893 to initialize the OMP_PRIV artificial variable and there is seventh
36894 statement, a DECL_EXPR of the OMP_PRIV statement again. */
36896 static bool
36897 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
36899 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
36900 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
36901 type = TREE_TYPE (type);
36902 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
36903 DECL_ARTIFICIAL (omp_out) = 1;
36904 pushdecl (omp_out);
36905 add_decl_expr (omp_out);
36906 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
36907 DECL_ARTIFICIAL (omp_in) = 1;
36908 pushdecl (omp_in);
36909 add_decl_expr (omp_in);
36910 tree combiner;
36911 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
36913 keep_next_level (true);
36914 tree block = begin_omp_structured_block ();
36915 combiner = cp_parser_expression (parser);
36916 finish_expr_stmt (combiner);
36917 block = finish_omp_structured_block (block);
36918 add_stmt (block);
36920 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36921 return false;
36923 const char *p = "";
36924 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36926 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36927 p = IDENTIFIER_POINTER (id);
36930 if (strcmp (p, "initializer") == 0)
36932 cp_lexer_consume_token (parser->lexer);
36933 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36934 return false;
36936 p = "";
36937 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36939 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36940 p = IDENTIFIER_POINTER (id);
36943 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
36944 DECL_ARTIFICIAL (omp_priv) = 1;
36945 pushdecl (omp_priv);
36946 add_decl_expr (omp_priv);
36947 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
36948 DECL_ARTIFICIAL (omp_orig) = 1;
36949 pushdecl (omp_orig);
36950 add_decl_expr (omp_orig);
36952 keep_next_level (true);
36953 block = begin_omp_structured_block ();
36955 bool ctor = false;
36956 if (strcmp (p, "omp_priv") == 0)
36958 bool is_direct_init, is_non_constant_init;
36959 ctor = true;
36960 cp_lexer_consume_token (parser->lexer);
36961 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36962 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36963 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36964 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36965 == CPP_CLOSE_PAREN
36966 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36967 == CPP_CLOSE_PAREN))
36969 finish_omp_structured_block (block);
36970 error ("invalid initializer clause");
36971 return false;
36973 initializer = cp_parser_initializer (parser, &is_direct_init,
36974 &is_non_constant_init);
36975 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36976 NULL_TREE, LOOKUP_ONLYCONVERTING);
36978 else
36980 cp_parser_parse_tentatively (parser);
36981 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36982 /*check_dependency_p=*/true,
36983 /*template_p=*/NULL,
36984 /*declarator_p=*/false,
36985 /*optional_p=*/false);
36986 vec<tree, va_gc> *args;
36987 if (fn_name == error_mark_node
36988 || cp_parser_error_occurred (parser)
36989 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36990 || ((args = cp_parser_parenthesized_expression_list
36991 (parser, non_attr, /*cast_p=*/false,
36992 /*allow_expansion_p=*/true,
36993 /*non_constant_p=*/NULL)),
36994 cp_parser_error_occurred (parser)))
36996 finish_omp_structured_block (block);
36997 cp_parser_abort_tentative_parse (parser);
36998 cp_parser_error (parser, "expected id-expression (arguments)");
36999 return false;
37001 unsigned int i;
37002 tree arg;
37003 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37004 if (arg == omp_priv
37005 || (TREE_CODE (arg) == ADDR_EXPR
37006 && TREE_OPERAND (arg, 0) == omp_priv))
37007 break;
37008 cp_parser_abort_tentative_parse (parser);
37009 if (arg == NULL_TREE)
37010 error ("one of the initializer call arguments should be %<omp_priv%>"
37011 " or %<&omp_priv%>");
37012 initializer = cp_parser_postfix_expression (parser, false, false, false,
37013 false, NULL);
37014 finish_expr_stmt (initializer);
37017 block = finish_omp_structured_block (block);
37018 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37019 add_stmt (block);
37021 if (ctor)
37022 add_decl_expr (omp_orig);
37024 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37025 return false;
37028 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37029 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
37031 return true;
37034 /* OpenMP 4.0
37035 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37036 initializer-clause[opt] new-line
37038 initializer-clause:
37039 initializer (omp_priv initializer)
37040 initializer (function-name (argument-list)) */
37042 static void
37043 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37044 enum pragma_context)
37046 auto_vec<tree> types;
37047 enum tree_code reduc_code = ERROR_MARK;
37048 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37049 unsigned int i;
37050 cp_token *first_token;
37051 cp_token_cache *cp;
37052 int errs;
37053 void *p;
37055 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37056 p = obstack_alloc (&declarator_obstack, 0);
37058 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37059 goto fail;
37061 switch (cp_lexer_peek_token (parser->lexer)->type)
37063 case CPP_PLUS:
37064 reduc_code = PLUS_EXPR;
37065 break;
37066 case CPP_MULT:
37067 reduc_code = MULT_EXPR;
37068 break;
37069 case CPP_MINUS:
37070 reduc_code = MINUS_EXPR;
37071 break;
37072 case CPP_AND:
37073 reduc_code = BIT_AND_EXPR;
37074 break;
37075 case CPP_XOR:
37076 reduc_code = BIT_XOR_EXPR;
37077 break;
37078 case CPP_OR:
37079 reduc_code = BIT_IOR_EXPR;
37080 break;
37081 case CPP_AND_AND:
37082 reduc_code = TRUTH_ANDIF_EXPR;
37083 break;
37084 case CPP_OR_OR:
37085 reduc_code = TRUTH_ORIF_EXPR;
37086 break;
37087 case CPP_NAME:
37088 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37089 break;
37090 default:
37091 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37092 "%<|%>, %<&&%>, %<||%> or identifier");
37093 goto fail;
37096 if (reduc_code != ERROR_MARK)
37097 cp_lexer_consume_token (parser->lexer);
37099 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37100 if (reduc_id == error_mark_node)
37101 goto fail;
37103 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37104 goto fail;
37106 /* Types may not be defined in declare reduction type list. */
37107 const char *saved_message;
37108 saved_message = parser->type_definition_forbidden_message;
37109 parser->type_definition_forbidden_message
37110 = G_("types may not be defined in declare reduction type list");
37111 bool saved_colon_corrects_to_scope_p;
37112 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37113 parser->colon_corrects_to_scope_p = false;
37114 bool saved_colon_doesnt_start_class_def_p;
37115 saved_colon_doesnt_start_class_def_p
37116 = parser->colon_doesnt_start_class_def_p;
37117 parser->colon_doesnt_start_class_def_p = true;
37119 while (true)
37121 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37122 type = cp_parser_type_id (parser);
37123 if (type == error_mark_node)
37125 else if (ARITHMETIC_TYPE_P (type)
37126 && (orig_reduc_id == NULL_TREE
37127 || (TREE_CODE (type) != COMPLEX_TYPE
37128 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37129 "min") == 0
37130 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37131 "max") == 0))))
37132 error_at (loc, "predeclared arithmetic type %qT in "
37133 "%<#pragma omp declare reduction%>", type);
37134 else if (TREE_CODE (type) == FUNCTION_TYPE
37135 || TREE_CODE (type) == METHOD_TYPE
37136 || TREE_CODE (type) == ARRAY_TYPE)
37137 error_at (loc, "function or array type %qT in "
37138 "%<#pragma omp declare reduction%>", type);
37139 else if (TREE_CODE (type) == REFERENCE_TYPE)
37140 error_at (loc, "reference type %qT in "
37141 "%<#pragma omp declare reduction%>", type);
37142 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37143 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37144 "%<#pragma omp declare reduction%>", type);
37145 else
37146 types.safe_push (type);
37148 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37149 cp_lexer_consume_token (parser->lexer);
37150 else
37151 break;
37154 /* Restore the saved message. */
37155 parser->type_definition_forbidden_message = saved_message;
37156 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37157 parser->colon_doesnt_start_class_def_p
37158 = saved_colon_doesnt_start_class_def_p;
37160 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37161 || types.is_empty ())
37163 fail:
37164 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37165 goto done;
37168 first_token = cp_lexer_peek_token (parser->lexer);
37169 cp = NULL;
37170 errs = errorcount;
37171 FOR_EACH_VEC_ELT (types, i, type)
37173 tree fntype
37174 = build_function_type_list (void_type_node,
37175 cp_build_reference_type (type, false),
37176 NULL_TREE);
37177 tree this_reduc_id = reduc_id;
37178 if (!dependent_type_p (type))
37179 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37180 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37181 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37182 DECL_ARTIFICIAL (fndecl) = 1;
37183 DECL_EXTERNAL (fndecl) = 1;
37184 DECL_DECLARED_INLINE_P (fndecl) = 1;
37185 DECL_IGNORED_P (fndecl) = 1;
37186 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37187 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37188 DECL_ATTRIBUTES (fndecl)
37189 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37190 DECL_ATTRIBUTES (fndecl));
37191 if (processing_template_decl)
37192 fndecl = push_template_decl (fndecl);
37193 bool block_scope = false;
37194 tree block = NULL_TREE;
37195 if (current_function_decl)
37197 block_scope = true;
37198 DECL_CONTEXT (fndecl) = global_namespace;
37199 if (!processing_template_decl)
37200 pushdecl (fndecl);
37202 else if (current_class_type)
37204 if (cp == NULL)
37206 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37207 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37208 cp_lexer_consume_token (parser->lexer);
37209 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37210 goto fail;
37211 cp = cp_token_cache_new (first_token,
37212 cp_lexer_peek_nth_token (parser->lexer,
37213 2));
37215 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37216 finish_member_declaration (fndecl);
37217 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37218 DECL_PENDING_INLINE_P (fndecl) = 1;
37219 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37220 continue;
37222 else
37224 DECL_CONTEXT (fndecl) = current_namespace;
37225 pushdecl (fndecl);
37227 if (!block_scope)
37228 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37229 else
37230 block = begin_omp_structured_block ();
37231 if (cp)
37233 cp_parser_push_lexer_for_tokens (parser, cp);
37234 parser->lexer->in_pragma = true;
37236 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37238 if (!block_scope)
37239 finish_function (0);
37240 else
37241 DECL_CONTEXT (fndecl) = current_function_decl;
37242 if (cp)
37243 cp_parser_pop_lexer (parser);
37244 goto fail;
37246 if (cp)
37247 cp_parser_pop_lexer (parser);
37248 if (!block_scope)
37249 finish_function (0);
37250 else
37252 DECL_CONTEXT (fndecl) = current_function_decl;
37253 block = finish_omp_structured_block (block);
37254 if (TREE_CODE (block) == BIND_EXPR)
37255 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37256 else if (TREE_CODE (block) == STATEMENT_LIST)
37257 DECL_SAVED_TREE (fndecl) = block;
37258 if (processing_template_decl)
37259 add_decl_expr (fndecl);
37261 cp_check_omp_declare_reduction (fndecl);
37262 if (cp == NULL && types.length () > 1)
37263 cp = cp_token_cache_new (first_token,
37264 cp_lexer_peek_nth_token (parser->lexer, 2));
37265 if (errs != errorcount)
37266 break;
37269 cp_parser_require_pragma_eol (parser, pragma_tok);
37271 done:
37272 /* Free any declarators allocated. */
37273 obstack_free (&declarator_obstack, p);
37276 /* OpenMP 4.0
37277 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37278 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37279 initializer-clause[opt] new-line
37280 #pragma omp declare target new-line */
37282 static void
37283 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37284 enum pragma_context context)
37286 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37288 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37289 const char *p = IDENTIFIER_POINTER (id);
37291 if (strcmp (p, "simd") == 0)
37293 cp_lexer_consume_token (parser->lexer);
37294 cp_parser_omp_declare_simd (parser, pragma_tok,
37295 context);
37296 return;
37298 cp_ensure_no_omp_declare_simd (parser);
37299 if (strcmp (p, "reduction") == 0)
37301 cp_lexer_consume_token (parser->lexer);
37302 cp_parser_omp_declare_reduction (parser, pragma_tok,
37303 context);
37304 return;
37306 if (!flag_openmp) /* flag_openmp_simd */
37308 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37309 return;
37311 if (strcmp (p, "target") == 0)
37313 cp_lexer_consume_token (parser->lexer);
37314 cp_parser_omp_declare_target (parser, pragma_tok);
37315 return;
37318 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37319 "or %<target%>");
37320 cp_parser_require_pragma_eol (parser, pragma_tok);
37323 /* OpenMP 4.5:
37324 #pragma omp taskloop taskloop-clause[optseq] new-line
37325 for-loop
37327 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37328 for-loop */
37330 #define OMP_TASKLOOP_CLAUSE_MASK \
37331 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37346 static tree
37347 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37348 char *p_name, omp_clause_mask mask, tree *cclauses,
37349 bool *if_p)
37351 tree clauses, sb, ret;
37352 unsigned int save;
37353 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37355 strcat (p_name, " taskloop");
37356 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37358 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37360 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37361 const char *p = IDENTIFIER_POINTER (id);
37363 if (strcmp (p, "simd") == 0)
37365 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37366 if (cclauses == NULL)
37367 cclauses = cclauses_buf;
37369 cp_lexer_consume_token (parser->lexer);
37370 if (!flag_openmp) /* flag_openmp_simd */
37371 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37372 cclauses, if_p);
37373 sb = begin_omp_structured_block ();
37374 save = cp_parser_begin_omp_structured_block (parser);
37375 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37376 cclauses, if_p);
37377 cp_parser_end_omp_structured_block (parser, save);
37378 tree body = finish_omp_structured_block (sb);
37379 if (ret == NULL)
37380 return ret;
37381 ret = make_node (OMP_TASKLOOP);
37382 TREE_TYPE (ret) = void_type_node;
37383 OMP_FOR_BODY (ret) = body;
37384 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37385 SET_EXPR_LOCATION (ret, loc);
37386 add_stmt (ret);
37387 return ret;
37390 if (!flag_openmp) /* flag_openmp_simd */
37392 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37393 return NULL_TREE;
37396 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37397 cclauses == NULL);
37398 if (cclauses)
37400 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37401 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37404 sb = begin_omp_structured_block ();
37405 save = cp_parser_begin_omp_structured_block (parser);
37407 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37408 if_p);
37410 cp_parser_end_omp_structured_block (parser, save);
37411 add_stmt (finish_omp_structured_block (sb));
37413 return ret;
37417 /* OpenACC 2.0:
37418 # pragma acc routine oacc-routine-clause[optseq] new-line
37419 function-definition
37421 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37424 #define OACC_ROUTINE_CLAUSE_MASK \
37425 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37431 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37432 component, which must resolve to a declared namespace-scope
37433 function. The clauses are either processed directly (for a named
37434 function), or defered until the immediatley following declaration
37435 is parsed. */
37437 static void
37438 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37439 enum pragma_context context)
37441 gcc_checking_assert (context == pragma_external);
37442 /* The checking for "another pragma following this one" in the "no optional
37443 '( name )'" case makes sure that we dont re-enter. */
37444 gcc_checking_assert (parser->oacc_routine == NULL);
37446 cp_oacc_routine_data data;
37447 data.error_seen = false;
37448 data.fndecl_seen = false;
37449 data.tokens = vNULL;
37450 data.clauses = NULL_TREE;
37451 data.loc = pragma_tok->location;
37452 /* It is safe to take the address of a local variable; it will only be
37453 used while this scope is live. */
37454 parser->oacc_routine = &data;
37456 /* Look for optional '( name )'. */
37457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37459 cp_lexer_consume_token (parser->lexer); /* '(' */
37461 /* We parse the name as an id-expression. If it resolves to
37462 anything other than a non-overloaded function at namespace
37463 scope, it's an error. */
37464 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37465 tree name = cp_parser_id_expression (parser,
37466 /*template_keyword_p=*/false,
37467 /*check_dependency_p=*/false,
37468 /*template_p=*/NULL,
37469 /*declarator_p=*/false,
37470 /*optional_p=*/false);
37471 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37472 if (name != error_mark_node && decl == error_mark_node)
37473 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37475 if (decl == error_mark_node
37476 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37478 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37479 parser->oacc_routine = NULL;
37480 return;
37483 data.clauses
37484 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37485 "#pragma acc routine",
37486 cp_lexer_peek_token (parser->lexer));
37488 if (decl && is_overloaded_fn (decl)
37489 && (TREE_CODE (decl) != FUNCTION_DECL
37490 || DECL_FUNCTION_TEMPLATE_P (decl)))
37492 error_at (name_loc,
37493 "%<#pragma acc routine%> names a set of overloads");
37494 parser->oacc_routine = NULL;
37495 return;
37498 /* Perhaps we should use the same rule as declarations in different
37499 namespaces? */
37500 if (!DECL_NAMESPACE_SCOPE_P (decl))
37502 error_at (name_loc,
37503 "%qD does not refer to a namespace scope function", decl);
37504 parser->oacc_routine = NULL;
37505 return;
37508 if (TREE_CODE (decl) != FUNCTION_DECL)
37510 error_at (name_loc, "%qD does not refer to a function", decl);
37511 parser->oacc_routine = NULL;
37512 return;
37515 cp_finalize_oacc_routine (parser, decl, false);
37516 parser->oacc_routine = NULL;
37518 else /* No optional '( name )'. */
37520 /* Store away all pragma tokens. */
37521 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37522 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37523 cp_lexer_consume_token (parser->lexer);
37524 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37525 parser->oacc_routine->error_seen = true;
37526 cp_parser_require_pragma_eol (parser, pragma_tok);
37527 struct cp_token_cache *cp
37528 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37529 parser->oacc_routine->tokens.safe_push (cp);
37531 /* Emit a helpful diagnostic if there's another pragma following this
37532 one. */
37533 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37535 cp_ensure_no_oacc_routine (parser);
37536 data.tokens.release ();
37537 /* ..., and then just keep going. */
37538 return;
37541 /* We only have to consider the pragma_external case here. */
37542 cp_parser_declaration (parser);
37543 if (parser->oacc_routine
37544 && !parser->oacc_routine->fndecl_seen)
37545 cp_ensure_no_oacc_routine (parser);
37546 else
37547 parser->oacc_routine = NULL;
37548 data.tokens.release ();
37552 /* Finalize #pragma acc routine clauses after direct declarator has
37553 been parsed. */
37555 static tree
37556 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37558 struct cp_token_cache *ce;
37559 cp_oacc_routine_data *data = parser->oacc_routine;
37561 if (!data->error_seen && data->fndecl_seen)
37563 error_at (data->loc,
37564 "%<#pragma acc routine%> not immediately followed by "
37565 "a single function declaration or definition");
37566 data->error_seen = true;
37568 if (data->error_seen)
37569 return attrs;
37571 gcc_checking_assert (data->tokens.length () == 1);
37572 ce = data->tokens[0];
37574 cp_parser_push_lexer_for_tokens (parser, ce);
37575 parser->lexer->in_pragma = true;
37576 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37578 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37579 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37580 parser->oacc_routine->clauses
37581 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37582 "#pragma acc routine", pragma_tok);
37583 cp_parser_pop_lexer (parser);
37584 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37585 fndecl_seen. */
37587 return attrs;
37590 /* Apply any saved OpenACC routine clauses to a just-parsed
37591 declaration. */
37593 static void
37594 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37596 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37598 /* Keep going if we're in error reporting mode. */
37599 if (parser->oacc_routine->error_seen
37600 || fndecl == error_mark_node)
37601 return;
37603 if (parser->oacc_routine->fndecl_seen)
37605 error_at (parser->oacc_routine->loc,
37606 "%<#pragma acc routine%> not immediately followed by"
37607 " a single function declaration or definition");
37608 parser->oacc_routine = NULL;
37609 return;
37611 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37613 cp_ensure_no_oacc_routine (parser);
37614 return;
37617 if (oacc_get_fn_attrib (fndecl))
37619 error_at (parser->oacc_routine->loc,
37620 "%<#pragma acc routine%> already applied to %qD", fndecl);
37621 parser->oacc_routine = NULL;
37622 return;
37625 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37627 error_at (parser->oacc_routine->loc,
37628 TREE_USED (fndecl)
37629 ? G_("%<#pragma acc routine%> must be applied before use")
37630 : G_("%<#pragma acc routine%> must be applied before "
37631 "definition"));
37632 parser->oacc_routine = NULL;
37633 return;
37636 /* Process the routine's dimension clauses. */
37637 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37638 oacc_replace_fn_attrib (fndecl, dims);
37640 /* Add an "omp declare target" attribute. */
37641 DECL_ATTRIBUTES (fndecl)
37642 = tree_cons (get_identifier ("omp declare target"),
37643 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37645 /* Don't unset parser->oacc_routine here: we may still need it to
37646 diagnose wrong usage. But, remember that we've used this "#pragma acc
37647 routine". */
37648 parser->oacc_routine->fndecl_seen = true;
37652 /* Main entry point to OpenMP statement pragmas. */
37654 static void
37655 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37657 tree stmt;
37658 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37659 omp_clause_mask mask (0);
37661 switch (cp_parser_pragma_kind (pragma_tok))
37663 case PRAGMA_OACC_ATOMIC:
37664 cp_parser_omp_atomic (parser, pragma_tok);
37665 return;
37666 case PRAGMA_OACC_CACHE:
37667 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37668 break;
37669 case PRAGMA_OACC_DATA:
37670 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37671 break;
37672 case PRAGMA_OACC_ENTER_DATA:
37673 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37674 break;
37675 case PRAGMA_OACC_EXIT_DATA:
37676 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37677 break;
37678 case PRAGMA_OACC_HOST_DATA:
37679 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37680 break;
37681 case PRAGMA_OACC_KERNELS:
37682 case PRAGMA_OACC_PARALLEL:
37683 strcpy (p_name, "#pragma acc");
37684 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37685 if_p);
37686 break;
37687 case PRAGMA_OACC_LOOP:
37688 strcpy (p_name, "#pragma acc");
37689 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37690 if_p);
37691 break;
37692 case PRAGMA_OACC_UPDATE:
37693 stmt = cp_parser_oacc_update (parser, pragma_tok);
37694 break;
37695 case PRAGMA_OACC_WAIT:
37696 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37697 break;
37698 case PRAGMA_OMP_ATOMIC:
37699 cp_parser_omp_atomic (parser, pragma_tok);
37700 return;
37701 case PRAGMA_OMP_CRITICAL:
37702 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37703 break;
37704 case PRAGMA_OMP_DISTRIBUTE:
37705 strcpy (p_name, "#pragma omp");
37706 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
37707 if_p);
37708 break;
37709 case PRAGMA_OMP_FOR:
37710 strcpy (p_name, "#pragma omp");
37711 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
37712 if_p);
37713 break;
37714 case PRAGMA_OMP_MASTER:
37715 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
37716 break;
37717 case PRAGMA_OMP_PARALLEL:
37718 strcpy (p_name, "#pragma omp");
37719 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
37720 if_p);
37721 break;
37722 case PRAGMA_OMP_SECTIONS:
37723 strcpy (p_name, "#pragma omp");
37724 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
37725 break;
37726 case PRAGMA_OMP_SIMD:
37727 strcpy (p_name, "#pragma omp");
37728 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
37729 if_p);
37730 break;
37731 case PRAGMA_OMP_SINGLE:
37732 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
37733 break;
37734 case PRAGMA_OMP_TASK:
37735 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
37736 break;
37737 case PRAGMA_OMP_TASKGROUP:
37738 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
37739 break;
37740 case PRAGMA_OMP_TASKLOOP:
37741 strcpy (p_name, "#pragma omp");
37742 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
37743 if_p);
37744 break;
37745 case PRAGMA_OMP_TEAMS:
37746 strcpy (p_name, "#pragma omp");
37747 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
37748 if_p);
37749 break;
37750 default:
37751 gcc_unreachable ();
37754 protected_set_expr_location (stmt, pragma_tok->location);
37757 /* Transactional Memory parsing routines. */
37759 /* Parse a transaction attribute.
37761 txn-attribute:
37762 attribute
37763 [ [ identifier ] ]
37765 We use this instead of cp_parser_attributes_opt for transactions to avoid
37766 the pedwarn in C++98 mode. */
37768 static tree
37769 cp_parser_txn_attribute_opt (cp_parser *parser)
37771 cp_token *token;
37772 tree attr_name, attr = NULL;
37774 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
37775 return cp_parser_attributes_opt (parser);
37777 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
37778 return NULL_TREE;
37779 cp_lexer_consume_token (parser->lexer);
37780 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
37781 goto error1;
37783 token = cp_lexer_peek_token (parser->lexer);
37784 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
37786 token = cp_lexer_consume_token (parser->lexer);
37788 attr_name = (token->type == CPP_KEYWORD
37789 /* For keywords, use the canonical spelling,
37790 not the parsed identifier. */
37791 ? ridpointers[(int) token->keyword]
37792 : token->u.value);
37793 attr = build_tree_list (attr_name, NULL_TREE);
37795 else
37796 cp_parser_error (parser, "expected identifier");
37798 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37799 error1:
37800 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37801 return attr;
37804 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37806 transaction-statement:
37807 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37808 compound-statement
37809 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37812 static tree
37813 cp_parser_transaction (cp_parser *parser, cp_token *token)
37815 unsigned char old_in = parser->in_transaction;
37816 unsigned char this_in = 1, new_in;
37817 enum rid keyword = token->keyword;
37818 tree stmt, attrs, noex;
37820 cp_lexer_consume_token (parser->lexer);
37822 if (keyword == RID_TRANSACTION_RELAXED
37823 || keyword == RID_SYNCHRONIZED)
37824 this_in |= TM_STMT_ATTR_RELAXED;
37825 else
37827 attrs = cp_parser_txn_attribute_opt (parser);
37828 if (attrs)
37829 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37832 /* Parse a noexcept specification. */
37833 if (keyword == RID_ATOMIC_NOEXCEPT)
37834 noex = boolean_true_node;
37835 else if (keyword == RID_ATOMIC_CANCEL)
37837 /* cancel-and-throw is unimplemented. */
37838 sorry ("atomic_cancel");
37839 noex = NULL_TREE;
37841 else
37842 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
37844 /* Keep track if we're in the lexical scope of an outer transaction. */
37845 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
37847 stmt = begin_transaction_stmt (token->location, NULL, this_in);
37849 parser->in_transaction = new_in;
37850 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
37851 parser->in_transaction = old_in;
37853 finish_transaction_stmt (stmt, NULL, this_in, noex);
37855 return stmt;
37858 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37860 transaction-expression:
37861 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37862 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37865 static tree
37866 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
37868 unsigned char old_in = parser->in_transaction;
37869 unsigned char this_in = 1;
37870 cp_token *token;
37871 tree expr, noex;
37872 bool noex_expr;
37873 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37875 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37876 || keyword == RID_TRANSACTION_RELAXED);
37878 if (!flag_tm)
37879 error_at (loc,
37880 keyword == RID_TRANSACTION_RELAXED
37881 ? G_("%<__transaction_relaxed%> without transactional memory "
37882 "support enabled")
37883 : G_("%<__transaction_atomic%> without transactional memory "
37884 "support enabled"));
37886 token = cp_parser_require_keyword (parser, keyword,
37887 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37888 : RT_TRANSACTION_RELAXED));
37889 gcc_assert (token != NULL);
37891 if (keyword == RID_TRANSACTION_RELAXED)
37892 this_in |= TM_STMT_ATTR_RELAXED;
37894 /* Set this early. This might mean that we allow transaction_cancel in
37895 an expression that we find out later actually has to be a constexpr.
37896 However, we expect that cxx_constant_value will be able to deal with
37897 this; also, if the noexcept has no constexpr, then what we parse next
37898 really is a transaction's body. */
37899 parser->in_transaction = this_in;
37901 /* Parse a noexcept specification. */
37902 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37903 true);
37905 if (!noex || !noex_expr
37906 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37908 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37910 expr = cp_parser_expression (parser);
37911 expr = finish_parenthesized_expr (expr);
37913 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37915 else
37917 /* The only expression that is available got parsed for the noexcept
37918 already. noexcept is true then. */
37919 expr = noex;
37920 noex = boolean_true_node;
37923 expr = build_transaction_expr (token->location, expr, this_in, noex);
37924 parser->in_transaction = old_in;
37926 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37927 return error_mark_node;
37929 return (flag_tm ? expr : error_mark_node);
37932 /* Parse a function-transaction-block.
37934 function-transaction-block:
37935 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37936 function-body
37937 __transaction_atomic txn-attribute[opt] function-try-block
37938 __transaction_relaxed ctor-initializer[opt] function-body
37939 __transaction_relaxed function-try-block
37942 static bool
37943 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37945 unsigned char old_in = parser->in_transaction;
37946 unsigned char new_in = 1;
37947 tree compound_stmt, stmt, attrs;
37948 bool ctor_initializer_p;
37949 cp_token *token;
37951 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37952 || keyword == RID_TRANSACTION_RELAXED);
37953 token = cp_parser_require_keyword (parser, keyword,
37954 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37955 : RT_TRANSACTION_RELAXED));
37956 gcc_assert (token != NULL);
37958 if (keyword == RID_TRANSACTION_RELAXED)
37959 new_in |= TM_STMT_ATTR_RELAXED;
37960 else
37962 attrs = cp_parser_txn_attribute_opt (parser);
37963 if (attrs)
37964 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37967 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37969 parser->in_transaction = new_in;
37971 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37972 ctor_initializer_p = cp_parser_function_try_block (parser);
37973 else
37974 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37975 (parser, /*in_function_try_block=*/false);
37977 parser->in_transaction = old_in;
37979 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37981 return ctor_initializer_p;
37984 /* Parse a __transaction_cancel statement.
37986 cancel-statement:
37987 __transaction_cancel txn-attribute[opt] ;
37988 __transaction_cancel txn-attribute[opt] throw-expression ;
37990 ??? Cancel and throw is not yet implemented. */
37992 static tree
37993 cp_parser_transaction_cancel (cp_parser *parser)
37995 cp_token *token;
37996 bool is_outer = false;
37997 tree stmt, attrs;
37999 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38000 RT_TRANSACTION_CANCEL);
38001 gcc_assert (token != NULL);
38003 attrs = cp_parser_txn_attribute_opt (parser);
38004 if (attrs)
38005 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38007 /* ??? Parse cancel-and-throw here. */
38009 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38011 if (!flag_tm)
38013 error_at (token->location, "%<__transaction_cancel%> without "
38014 "transactional memory support enabled");
38015 return error_mark_node;
38017 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38019 error_at (token->location, "%<__transaction_cancel%> within a "
38020 "%<__transaction_relaxed%>");
38021 return error_mark_node;
38023 else if (is_outer)
38025 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38026 && !is_tm_may_cancel_outer (current_function_decl))
38028 error_at (token->location, "outer %<__transaction_cancel%> not "
38029 "within outer %<__transaction_atomic%>");
38030 error_at (token->location,
38031 " or a %<transaction_may_cancel_outer%> function");
38032 return error_mark_node;
38035 else if (parser->in_transaction == 0)
38037 error_at (token->location, "%<__transaction_cancel%> not within "
38038 "%<__transaction_atomic%>");
38039 return error_mark_node;
38042 stmt = build_tm_abort_call (token->location, is_outer);
38043 add_stmt (stmt);
38045 return stmt;
38048 /* The parser. */
38050 static GTY (()) cp_parser *the_parser;
38053 /* Special handling for the first token or line in the file. The first
38054 thing in the file might be #pragma GCC pch_preprocess, which loads a
38055 PCH file, which is a GC collection point. So we need to handle this
38056 first pragma without benefit of an existing lexer structure.
38058 Always returns one token to the caller in *FIRST_TOKEN. This is
38059 either the true first token of the file, or the first token after
38060 the initial pragma. */
38062 static void
38063 cp_parser_initial_pragma (cp_token *first_token)
38065 tree name = NULL;
38067 cp_lexer_get_preprocessor_token (NULL, first_token);
38068 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38069 return;
38071 cp_lexer_get_preprocessor_token (NULL, first_token);
38072 if (first_token->type == CPP_STRING)
38074 name = first_token->u.value;
38076 cp_lexer_get_preprocessor_token (NULL, first_token);
38077 if (first_token->type != CPP_PRAGMA_EOL)
38078 error_at (first_token->location,
38079 "junk at end of %<#pragma GCC pch_preprocess%>");
38081 else
38082 error_at (first_token->location, "expected string literal");
38084 /* Skip to the end of the pragma. */
38085 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38086 cp_lexer_get_preprocessor_token (NULL, first_token);
38088 /* Now actually load the PCH file. */
38089 if (name)
38090 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38092 /* Read one more token to return to our caller. We have to do this
38093 after reading the PCH file in, since its pointers have to be
38094 live. */
38095 cp_lexer_get_preprocessor_token (NULL, first_token);
38098 /* Parses the grainsize pragma for the _Cilk_for statement.
38099 Syntax:
38100 #pragma cilk grainsize = <VALUE>. */
38102 static void
38103 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38105 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38107 tree exp = cp_parser_binary_expression (parser, false, false,
38108 PREC_NOT_OPERATOR, NULL);
38109 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38110 if (!exp || exp == error_mark_node)
38112 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38113 return;
38116 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38117 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38118 cp_parser_cilk_for (parser, exp, if_p);
38119 else
38120 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38121 "%<#pragma cilk grainsize%> is not followed by "
38122 "%<_Cilk_for%>");
38123 return;
38125 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38128 /* Normal parsing of a pragma token. Here we can (and must) use the
38129 regular lexer. */
38131 static bool
38132 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38134 cp_token *pragma_tok;
38135 unsigned int id;
38136 tree stmt;
38137 bool ret;
38139 pragma_tok = cp_lexer_consume_token (parser->lexer);
38140 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38141 parser->lexer->in_pragma = true;
38143 id = cp_parser_pragma_kind (pragma_tok);
38144 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38145 cp_ensure_no_omp_declare_simd (parser);
38146 switch (id)
38148 case PRAGMA_GCC_PCH_PREPROCESS:
38149 error_at (pragma_tok->location,
38150 "%<#pragma GCC pch_preprocess%> must be first");
38151 break;
38153 case PRAGMA_OMP_BARRIER:
38154 switch (context)
38156 case pragma_compound:
38157 cp_parser_omp_barrier (parser, pragma_tok);
38158 return false;
38159 case pragma_stmt:
38160 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38161 "used in compound statements", "omp barrier");
38162 break;
38163 default:
38164 goto bad_stmt;
38166 break;
38168 case PRAGMA_OMP_FLUSH:
38169 switch (context)
38171 case pragma_compound:
38172 cp_parser_omp_flush (parser, pragma_tok);
38173 return false;
38174 case pragma_stmt:
38175 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38176 "used in compound statements", "omp flush");
38177 break;
38178 default:
38179 goto bad_stmt;
38181 break;
38183 case PRAGMA_OMP_TASKWAIT:
38184 switch (context)
38186 case pragma_compound:
38187 cp_parser_omp_taskwait (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 taskwait");
38193 break;
38194 default:
38195 goto bad_stmt;
38197 break;
38199 case PRAGMA_OMP_TASKYIELD:
38200 switch (context)
38202 case pragma_compound:
38203 cp_parser_omp_taskyield (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 taskyield");
38209 break;
38210 default:
38211 goto bad_stmt;
38213 break;
38215 case PRAGMA_OMP_CANCEL:
38216 switch (context)
38218 case pragma_compound:
38219 cp_parser_omp_cancel (parser, pragma_tok);
38220 return false;
38221 case pragma_stmt:
38222 error_at (pragma_tok->location,
38223 "%<#pragma %s%> may only be used in compound statements",
38224 "omp cancel");
38225 break;
38226 default:
38227 goto bad_stmt;
38229 break;
38231 case PRAGMA_OMP_CANCELLATION_POINT:
38232 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38233 return false;
38235 case PRAGMA_OMP_THREADPRIVATE:
38236 cp_parser_omp_threadprivate (parser, pragma_tok);
38237 return false;
38239 case PRAGMA_OMP_DECLARE:
38240 cp_parser_omp_declare (parser, pragma_tok, context);
38241 return false;
38243 case PRAGMA_OACC_DECLARE:
38244 cp_parser_oacc_declare (parser, pragma_tok);
38245 return false;
38247 case PRAGMA_OACC_ENTER_DATA:
38248 if (context == pragma_stmt)
38250 error_at (pragma_tok->location,
38251 "%<#pragma %s%> may only be used in compound statements",
38252 "acc enter data");
38253 break;
38255 else if (context != pragma_compound)
38256 goto bad_stmt;
38257 cp_parser_omp_construct (parser, pragma_tok, if_p);
38258 return true;
38260 case PRAGMA_OACC_EXIT_DATA:
38261 if (context == pragma_stmt)
38263 error_at (pragma_tok->location,
38264 "%<#pragma %s%> may only be used in compound statements",
38265 "acc exit data");
38266 break;
38268 else if (context != pragma_compound)
38269 goto bad_stmt;
38270 cp_parser_omp_construct (parser, pragma_tok, if_p);
38271 return true;
38273 case PRAGMA_OACC_ROUTINE:
38274 if (context != pragma_external)
38276 error_at (pragma_tok->location,
38277 "%<#pragma acc routine%> must be at file scope");
38278 break;
38280 cp_parser_oacc_routine (parser, pragma_tok, context);
38281 return false;
38283 case PRAGMA_OACC_UPDATE:
38284 if (context == pragma_stmt)
38286 error_at (pragma_tok->location,
38287 "%<#pragma %s%> may only be used in compound statements",
38288 "acc update");
38289 break;
38291 else if (context != pragma_compound)
38292 goto bad_stmt;
38293 cp_parser_omp_construct (parser, pragma_tok, if_p);
38294 return true;
38296 case PRAGMA_OACC_WAIT:
38297 if (context == pragma_stmt)
38299 error_at (pragma_tok->location,
38300 "%<#pragma %s%> may only be used in compound statements",
38301 "acc wait");
38302 break;
38304 else if (context != pragma_compound)
38305 goto bad_stmt;
38306 cp_parser_omp_construct (parser, pragma_tok, if_p);
38307 return true;
38309 case PRAGMA_OACC_ATOMIC:
38310 case PRAGMA_OACC_CACHE:
38311 case PRAGMA_OACC_DATA:
38312 case PRAGMA_OACC_HOST_DATA:
38313 case PRAGMA_OACC_KERNELS:
38314 case PRAGMA_OACC_PARALLEL:
38315 case PRAGMA_OACC_LOOP:
38316 case PRAGMA_OMP_ATOMIC:
38317 case PRAGMA_OMP_CRITICAL:
38318 case PRAGMA_OMP_DISTRIBUTE:
38319 case PRAGMA_OMP_FOR:
38320 case PRAGMA_OMP_MASTER:
38321 case PRAGMA_OMP_PARALLEL:
38322 case PRAGMA_OMP_SECTIONS:
38323 case PRAGMA_OMP_SIMD:
38324 case PRAGMA_OMP_SINGLE:
38325 case PRAGMA_OMP_TASK:
38326 case PRAGMA_OMP_TASKGROUP:
38327 case PRAGMA_OMP_TASKLOOP:
38328 case PRAGMA_OMP_TEAMS:
38329 if (context != pragma_stmt && context != pragma_compound)
38330 goto bad_stmt;
38331 stmt = push_omp_privatization_clauses (false);
38332 cp_parser_omp_construct (parser, pragma_tok, if_p);
38333 pop_omp_privatization_clauses (stmt);
38334 return true;
38336 case PRAGMA_OMP_ORDERED:
38337 if (context != pragma_stmt && context != pragma_compound)
38338 goto bad_stmt;
38339 stmt = push_omp_privatization_clauses (false);
38340 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38341 pop_omp_privatization_clauses (stmt);
38342 return ret;
38344 case PRAGMA_OMP_TARGET:
38345 if (context != pragma_stmt && context != pragma_compound)
38346 goto bad_stmt;
38347 stmt = push_omp_privatization_clauses (false);
38348 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38349 pop_omp_privatization_clauses (stmt);
38350 return ret;
38352 case PRAGMA_OMP_END_DECLARE_TARGET:
38353 cp_parser_omp_end_declare_target (parser, pragma_tok);
38354 return false;
38356 case PRAGMA_OMP_SECTION:
38357 error_at (pragma_tok->location,
38358 "%<#pragma omp section%> may only be used in "
38359 "%<#pragma omp sections%> construct");
38360 break;
38362 case PRAGMA_IVDEP:
38364 if (context == pragma_external)
38366 error_at (pragma_tok->location,
38367 "%<#pragma GCC ivdep%> must be inside a function");
38368 break;
38370 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38371 cp_token *tok;
38372 tok = cp_lexer_peek_token (the_parser->lexer);
38373 if (tok->type != CPP_KEYWORD
38374 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38375 && tok->keyword != RID_DO))
38377 cp_parser_error (parser, "for, while or do statement expected");
38378 return false;
38380 cp_parser_iteration_statement (parser, if_p, true);
38381 return true;
38384 case PRAGMA_CILK_SIMD:
38385 if (context == pragma_external)
38387 error_at (pragma_tok->location,
38388 "%<#pragma simd%> must be inside a function");
38389 break;
38391 stmt = push_omp_privatization_clauses (false);
38392 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38393 pop_omp_privatization_clauses (stmt);
38394 return true;
38396 case PRAGMA_CILK_GRAINSIZE:
38397 if (context == pragma_external)
38399 error_at (pragma_tok->location,
38400 "%<#pragma cilk grainsize%> must be inside a function");
38401 break;
38404 /* Ignore the pragma if Cilk Plus is not enabled. */
38405 if (flag_cilkplus)
38407 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38408 return true;
38410 else
38412 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38413 "%<#pragma cilk grainsize%>");
38414 break;
38417 default:
38418 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38419 c_invoke_pragma_handler (id);
38420 break;
38422 bad_stmt:
38423 cp_parser_error (parser, "expected declaration specifiers");
38424 break;
38427 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38428 return false;
38431 /* The interface the pragma parsers have to the lexer. */
38433 enum cpp_ttype
38434 pragma_lex (tree *value, location_t *loc)
38436 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38437 enum cpp_ttype ret = tok->type;
38439 *value = tok->u.value;
38440 if (loc)
38441 *loc = tok->location;
38443 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38444 ret = CPP_EOF;
38445 else if (ret == CPP_STRING)
38446 *value = cp_parser_string_literal (the_parser, false, false);
38447 else
38449 if (ret == CPP_KEYWORD)
38450 ret = CPP_NAME;
38451 cp_lexer_consume_token (the_parser->lexer);
38454 return ret;
38458 /* External interface. */
38460 /* Parse one entire translation unit. */
38462 void
38463 c_parse_file (void)
38465 static bool already_called = false;
38467 if (already_called)
38468 fatal_error (input_location,
38469 "inter-module optimizations not implemented for C++");
38470 already_called = true;
38472 the_parser = cp_parser_new ();
38473 push_deferring_access_checks (flag_access_control
38474 ? dk_no_deferred : dk_no_check);
38475 cp_parser_translation_unit (the_parser);
38476 the_parser = NULL;
38479 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38480 vectorlength clause:
38481 Syntax:
38482 vectorlength ( constant-expression ) */
38484 static tree
38485 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
38486 bool is_simd_fn)
38488 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38489 tree expr;
38490 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38491 safelen clause. Thus, vectorlength is represented as OMP 4.0
38492 safelen. For SIMD-enabled function it is represented by OMP 4.0
38493 simdlen. */
38494 if (!is_simd_fn)
38495 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
38496 loc);
38497 else
38498 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
38499 loc);
38501 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38502 return error_mark_node;
38504 expr = cp_parser_constant_expression (parser);
38505 expr = maybe_constant_value (expr);
38507 /* If expr == error_mark_node, then don't emit any errors nor
38508 create a clause. if any of the above functions returns
38509 error mark node then they would have emitted an error message. */
38510 if (expr == error_mark_node)
38512 else if (!TREE_TYPE (expr)
38513 || !TREE_CONSTANT (expr)
38514 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
38515 error_at (loc, "vectorlength must be an integer constant");
38516 else if (TREE_CONSTANT (expr)
38517 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
38518 error_at (loc, "vectorlength must be a power of 2");
38519 else
38521 tree c;
38522 if (!is_simd_fn)
38524 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
38525 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
38526 OMP_CLAUSE_CHAIN (c) = clauses;
38527 clauses = c;
38529 else
38531 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
38532 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
38533 OMP_CLAUSE_CHAIN (c) = clauses;
38534 clauses = c;
38538 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38539 return error_mark_node;
38540 return clauses;
38543 /* Handles the Cilk Plus #pragma simd linear clause.
38544 Syntax:
38545 linear ( simd-linear-variable-list )
38547 simd-linear-variable-list:
38548 simd-linear-variable
38549 simd-linear-variable-list , simd-linear-variable
38551 simd-linear-variable:
38552 id-expression
38553 id-expression : simd-linear-step
38555 simd-linear-step:
38556 conditional-expression */
38558 static tree
38559 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
38561 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38563 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38564 return clauses;
38565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38567 cp_parser_error (parser, "expected identifier");
38568 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38569 return error_mark_node;
38572 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38573 parser->colon_corrects_to_scope_p = false;
38574 while (1)
38576 cp_token *token = cp_lexer_peek_token (parser->lexer);
38577 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38579 cp_parser_error (parser, "expected variable-name");
38580 clauses = error_mark_node;
38581 break;
38584 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
38585 false, false);
38586 tree decl = cp_parser_lookup_name_simple (parser, var_name,
38587 token->location);
38588 if (decl == error_mark_node)
38590 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
38591 token->location);
38592 clauses = error_mark_node;
38594 else
38596 tree e = NULL_TREE;
38597 tree step_size = integer_one_node;
38599 /* If present, parse the linear step. Otherwise, assume the default
38600 value of 1. */
38601 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
38603 cp_lexer_consume_token (parser->lexer);
38605 e = cp_parser_assignment_expression (parser);
38606 e = maybe_constant_value (e);
38608 if (e == error_mark_node)
38610 /* If an error has occurred, then the whole pragma is
38611 considered ill-formed. Thus, no reason to keep
38612 parsing. */
38613 clauses = error_mark_node;
38614 break;
38616 else if (type_dependent_expression_p (e)
38617 || value_dependent_expression_p (e)
38618 || (TREE_TYPE (e)
38619 && INTEGRAL_TYPE_P (TREE_TYPE (e))
38620 && (TREE_CONSTANT (e)
38621 || DECL_P (e))))
38622 step_size = e;
38623 else
38624 cp_parser_error (parser,
38625 "step size must be an integer constant "
38626 "expression or an integer variable");
38629 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38630 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38631 OMP_CLAUSE_DECL (l) = decl;
38632 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
38633 OMP_CLAUSE_CHAIN (l) = clauses;
38634 clauses = l;
38636 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38637 cp_lexer_consume_token (parser->lexer);
38638 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38639 break;
38640 else
38642 error_at (cp_lexer_peek_token (parser->lexer)->location,
38643 "expected %<,%> or %<)%> after %qE", decl);
38644 clauses = error_mark_node;
38645 break;
38648 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38649 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38650 return clauses;
38653 /* Returns the name of the next clause. If the clause is not
38654 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38655 token is not consumed. Otherwise, the appropriate enum from the
38656 pragma_simd_clause is returned and the token is consumed. */
38658 static pragma_omp_clause
38659 cp_parser_cilk_simd_clause_name (cp_parser *parser)
38661 pragma_omp_clause clause_type;
38662 cp_token *token = cp_lexer_peek_token (parser->lexer);
38664 if (token->keyword == RID_PRIVATE)
38665 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
38666 else if (!token->u.value || token->type != CPP_NAME)
38667 return PRAGMA_CILK_CLAUSE_NONE;
38668 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
38669 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
38670 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
38671 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
38672 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
38673 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
38674 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
38675 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
38676 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
38677 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
38678 else
38679 return PRAGMA_CILK_CLAUSE_NONE;
38681 cp_lexer_consume_token (parser->lexer);
38682 return clause_type;
38685 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38687 static tree
38688 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
38690 tree clauses = NULL_TREE;
38692 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38693 && clauses != error_mark_node)
38695 pragma_omp_clause c_kind;
38696 c_kind = cp_parser_cilk_simd_clause_name (parser);
38697 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
38698 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
38699 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
38700 clauses = cp_parser_cilk_simd_linear (parser, clauses);
38701 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
38702 /* Use the OpenMP 4.0 equivalent function. */
38703 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
38704 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
38705 /* Use the OpenMP 4.0 equivalent function. */
38706 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38707 clauses);
38708 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
38709 /* Use the OMP 4.0 equivalent function. */
38710 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
38711 clauses);
38712 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
38713 /* Use the OMP 4.0 equivalent function. */
38714 clauses = cp_parser_omp_clause_reduction (parser, clauses);
38715 else
38717 clauses = error_mark_node;
38718 cp_parser_error (parser, "expected %<#pragma simd%> clause");
38719 break;
38723 cp_parser_skip_to_pragma_eol (parser, pragma_token);
38725 if (clauses == error_mark_node)
38726 return error_mark_node;
38727 else
38728 return finish_omp_clauses (clauses, C_ORT_CILK);
38731 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38733 static void
38734 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
38736 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
38738 if (clauses == error_mark_node)
38739 return;
38741 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
38743 error_at (cp_lexer_peek_token (parser->lexer)->location,
38744 "for statement expected");
38745 return;
38748 tree sb = begin_omp_structured_block ();
38749 int save = cp_parser_begin_omp_structured_block (parser);
38750 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
38751 if (ret)
38752 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
38753 cp_parser_end_omp_structured_block (parser, save);
38754 add_stmt (finish_omp_structured_block (sb));
38757 /* Main entry-point for parsing Cilk Plus _Cilk_for
38758 loops. The return value is error_mark_node
38759 when errors happen and CILK_FOR tree on success. */
38761 static tree
38762 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
38764 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
38765 gcc_unreachable ();
38767 tree sb = begin_omp_structured_block ();
38768 int save = cp_parser_begin_omp_structured_block (parser);
38770 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
38771 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
38772 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
38773 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
38775 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
38776 if (ret)
38777 cpp_validate_cilk_plus_loop (ret);
38778 else
38779 ret = error_mark_node;
38781 cp_parser_end_omp_structured_block (parser, save);
38782 add_stmt (finish_omp_structured_block (sb));
38783 return ret;
38786 /* Create an identifier for a generic parameter type (a synthesized
38787 template parameter implied by `auto' or a concept identifier). */
38789 static GTY(()) int generic_parm_count;
38790 static tree
38791 make_generic_type_name ()
38793 char buf[32];
38794 sprintf (buf, "auto:%d", ++generic_parm_count);
38795 return get_identifier (buf);
38798 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38799 (creating a new template parameter list if necessary). Returns the newly
38800 created template type parm. */
38802 static tree
38803 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38805 gcc_assert (current_binding_level->kind == sk_function_parms);
38807 /* Before committing to modifying any scope, if we're in an
38808 implicit template scope, and we're trying to synthesize a
38809 constrained parameter, try to find a previous parameter with
38810 the same name. This is the same-type rule for abbreviated
38811 function templates.
38813 NOTE: We can generate implicit parameters when tentatively
38814 parsing a nested name specifier, only to reject that parse
38815 later. However, matching the same template-id as part of a
38816 direct-declarator should generate an identical template
38817 parameter, so this rule will merge them. */
38818 if (parser->implicit_template_scope && constr)
38820 tree t = parser->implicit_template_parms;
38821 while (t)
38823 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38825 tree d = TREE_VALUE (t);
38826 if (TREE_CODE (d) == PARM_DECL)
38827 /* Return the TEMPLATE_PARM_INDEX. */
38828 d = DECL_INITIAL (d);
38829 return d;
38831 t = TREE_CHAIN (t);
38835 /* We are either continuing a function template that already contains implicit
38836 template parameters, creating a new fully-implicit function template, or
38837 extending an existing explicit function template with implicit template
38838 parameters. */
38840 cp_binding_level *const entry_scope = current_binding_level;
38842 bool become_template = false;
38843 cp_binding_level *parent_scope = 0;
38845 if (parser->implicit_template_scope)
38847 gcc_assert (parser->implicit_template_parms);
38849 current_binding_level = parser->implicit_template_scope;
38851 else
38853 /* Roll back to the existing template parameter scope (in the case of
38854 extending an explicit function template) or introduce a new template
38855 parameter scope ahead of the function parameter scope (or class scope
38856 in the case of out-of-line member definitions). The function scope is
38857 added back after template parameter synthesis below. */
38859 cp_binding_level *scope = entry_scope;
38861 while (scope->kind == sk_function_parms)
38863 parent_scope = scope;
38864 scope = scope->level_chain;
38866 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38868 /* If not defining a class, then any class scope is a scope level in
38869 an out-of-line member definition. In this case simply wind back
38870 beyond the first such scope to inject the template parameter list.
38871 Otherwise wind back to the class being defined. The latter can
38872 occur in class member friend declarations such as:
38874 class A {
38875 void foo (auto);
38877 class B {
38878 friend void A::foo (auto);
38881 The template parameter list synthesized for the friend declaration
38882 must be injected in the scope of 'B'. This can also occur in
38883 erroneous cases such as:
38885 struct A {
38886 struct B {
38887 void foo (auto);
38889 void B::foo (auto) {}
38892 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38893 but, nevertheless, the template parameter list synthesized for the
38894 declarator should be injected into the scope of 'A' as if the
38895 ill-formed template was specified explicitly. */
38897 while (scope->kind == sk_class && !scope->defining_class_p)
38899 parent_scope = scope;
38900 scope = scope->level_chain;
38904 current_binding_level = scope;
38906 if (scope->kind != sk_template_parms
38907 || !function_being_declared_is_template_p (parser))
38909 /* Introduce a new template parameter list for implicit template
38910 parameters. */
38912 become_template = true;
38914 parser->implicit_template_scope
38915 = begin_scope (sk_template_parms, NULL);
38917 ++processing_template_decl;
38919 parser->fully_implicit_function_template_p = true;
38920 ++parser->num_template_parameter_lists;
38922 else
38924 /* Synthesize implicit template parameters at the end of the explicit
38925 template parameter list. */
38927 gcc_assert (current_template_parms);
38929 parser->implicit_template_scope = scope;
38931 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38932 parser->implicit_template_parms
38933 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38937 /* Synthesize a new template parameter and track the current template
38938 parameter chain with implicit_template_parms. */
38940 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38941 tree synth_id = make_generic_type_name ();
38942 tree synth_tmpl_parm;
38943 bool non_type = false;
38945 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38946 synth_tmpl_parm
38947 = finish_template_type_parm (class_type_node, synth_id);
38948 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38949 synth_tmpl_parm
38950 = finish_constrained_template_template_parm (proto, synth_id);
38951 else
38953 synth_tmpl_parm = copy_decl (proto);
38954 DECL_NAME (synth_tmpl_parm) = synth_id;
38955 non_type = true;
38958 // Attach the constraint to the parm before processing.
38959 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38960 TREE_TYPE (node) = constr;
38961 tree new_parm
38962 = process_template_parm (parser->implicit_template_parms,
38963 input_location,
38964 node,
38965 /*non_type=*/non_type,
38966 /*param_pack=*/false);
38968 // Chain the new parameter to the list of implicit parameters.
38969 if (parser->implicit_template_parms)
38970 parser->implicit_template_parms
38971 = TREE_CHAIN (parser->implicit_template_parms);
38972 else
38973 parser->implicit_template_parms = new_parm;
38975 tree new_decl = get_local_decls ();
38976 if (non_type)
38977 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38978 new_decl = DECL_INITIAL (new_decl);
38980 /* If creating a fully implicit function template, start the new implicit
38981 template parameter list with this synthesized type, otherwise grow the
38982 current template parameter list. */
38984 if (become_template)
38986 parent_scope->level_chain = current_binding_level;
38988 tree new_parms = make_tree_vec (1);
38989 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38990 current_template_parms = tree_cons (size_int (processing_template_decl),
38991 new_parms, current_template_parms);
38993 else
38995 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38996 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38997 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38998 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39001 // If the new parameter was constrained, we need to add that to the
39002 // constraints in the template parameter list.
39003 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39005 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39006 reqs = conjoin_constraints (reqs, req);
39007 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39010 current_binding_level = entry_scope;
39012 return new_decl;
39015 /* Finish the declaration of a fully implicit function template. Such a
39016 template has no explicit template parameter list so has not been through the
39017 normal template head and tail processing. synthesize_implicit_template_parm
39018 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39019 provided if the declaration is a class member such that its template
39020 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39021 form is returned. Otherwise NULL_TREE is returned. */
39023 static tree
39024 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39026 gcc_assert (parser->fully_implicit_function_template_p);
39028 if (member_decl_opt && member_decl_opt != error_mark_node
39029 && DECL_VIRTUAL_P (member_decl_opt))
39031 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39032 "implicit templates may not be %<virtual%>");
39033 DECL_VIRTUAL_P (member_decl_opt) = false;
39036 if (member_decl_opt)
39037 member_decl_opt = finish_member_template_decl (member_decl_opt);
39038 end_template_decl ();
39040 parser->fully_implicit_function_template_p = false;
39041 --parser->num_template_parameter_lists;
39043 return member_decl_opt;
39046 #include "gt-cp-parser.h"